Changes in doc/proposals/enum.tex [2989d6f:367725d]
- File:
-
- 1 edited
-
doc/proposals/enum.tex (modified) (49 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/enum.tex
r2989d6f r367725d 7 7 \usepackage{graphics} 8 8 \usepackage{xspace} 9 \usepackage{relsize} % must be after change to small or selects old size10 \usepackage{calc} % latex arithmetic11 9 12 10 \makeatletter … … 24 22 \newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi} 25 23 \newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi} 26 27 \newcommand{\LstBasicStyle}[1]{{\lst@basicstyle{#1}}}28 \newcommand{\LstKeywordStyle}[1]{{\lst@basicstyle{\lst@keywordstyle{#1}}}}29 \newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}}30 \newcommand{\LstStringStyle}[1]{{\lst@basicstyle{\lst@stringstyle{#1}}}}31 \newcommand{\LstNumberStyle}[1]{{\lst@basicstyle{\lst@numberstyle{#1}}}}32 33 \newlength{\gcolumnposn} % temporary hack because lstlisting does not handle tabs correctly34 \newlength{\columnposn}35 \setlength{\gcolumnposn}{3in}36 \setlength{\columnposn}{\gcolumnposn}37 \newcommand{\setgcolumn}[1]{\global\gcolumnposn=#1\global\columnposn=\gcolumnposn}38 \newcommand{\C}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\LstCommentStyle{#2}}}39 \newcommand{\CD}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\LstBasicStyle{#2}}}40 \newcommand{\CRT}{\global\columnposn=\gcolumnposn}41 24 \makeatother 42 25 … … 65 48 \newcommand{\CCIcon}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}} % C++ icon 66 49 \newcommand{\CC}[1][]{\protect\CCIcon{#1}\xspace} % C++ symbolic name 67 \newcommand{\Csharp}{C\raisebox{-0.7ex}{\relsize{2}$^\sharp$}\xspace} % C# symbolic name68 50 \newcommand{\PAB}[1]{{\color{red}PAB: #1}} 69 51 … … 74 56 75 57 \lstdefinestyle{CStyle}{ 76 % backgroundcolor=\color{backgroundColour}, 58 % backgroundcolor=\color{backgroundColour}, 77 59 % commentstyle=\color{mGreen}, 78 60 % keywordstyle=\color{magenta}, … … 82 64 basicstyle=\small\linespread{0.9}\sf, % reduce line spacing and use sanserif font 83 65 % basicstyle=\footnotesize, 84 breakatwhitespace=false, 85 % breaklines=true, 86 captionpos=b, 87 keepspaces=true, 66 breakatwhitespace=false, 67 % breaklines=true, 68 captionpos=b, 69 keepspaces=true, 88 70 escapechar=\$, % LaTeX escape in CFA code 89 % numbers=left, 90 % numbersep=5pt, 71 % numbers=left, 72 % numbersep=5pt, 91 73 % numberstyle=\tiny\color{mGray}, 92 % showspaces=false, 74 % showspaces=false, 93 75 showstringspaces=false, 94 % showtabs=false, 76 % showtabs=false, 95 77 showlines=true, % show blank lines at end of code 96 78 tabsize=5, … … 111 93 112 94 \begin{abstract} 113 An enumeration is a type defining an ordered set of named constant values, where a name abstracts a value, e.g., @PI@ versus @3.145159@. 114 C restrict an enumeration type to the integral type @signed int@, which \CC support , meaning enumeration names bind to integer constants. 115 \CFA extends C enumerations to allow all basic and custom types for the enumeration type, like other modern programming languages. 116 Furthermore, \CFA adds other useful features for enumerations to support better software-engineering practices and simplify program development. 95 An enumeration is a type that defines a list of named constant values in C (and other languages). 96 C and \CC use an integral type as the underlying representation of an enumeration. 97 \CFA extends C enumerations to allow all basic and custom types for the inner representation. 117 98 \end{abstract} 118 99 119 \section{Background}120 121 Naming values is a common practice in mathematics and engineering, e.g., $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MHz (1E6), etc.122 Naming is also commonly used to represent many other numerical phenomenon, such as days of the week, months of a year, floors of a building (basement), time (noon, New Years).123 Many programming languages capture this important capability through a mechanism called an \newterm{enumeration}.124 An enumeration is similar to other programming-language types by providing a set of constrained values, but adds the ability to name \emph{all} the values in its set.125 Note, all enumeration names must be unique but different names can represent the same value (eight note, quaver), which are synonyms.126 127 Specifically, an enumerated type is a type whose values are restricted to a fixed set of named constants.128 Fundamentally, all types are restricted to a fixed set of values because of the underlying von Neumann architecture, and hence, to a corresponding set of constants, e.g., @3@, @3.5@, @3.5+2.1i@, @'c'@, @"abc"@, etc.129 However, the values for basic types are not named, other than the programming-language supplied constants.130 131 132 100 \section{C-Style Enum} 133 101 134 The C-Style enumeration has the followingsyntax and semantics.102 \CFA supports the C-Style enumeration using the same syntax and semantics. 135 103 \begin{lstlisting}[label=lst:weekday] 136 enum Weekday { Monday, Tuesday, Wednesday, Thursday @ = 10@, Friday, Saturday, Sunday };137 $\(\uparrow\)$ $\(\uparrow\)$138 ${\rm \newterm{enumeration name}}$ ${\rm \newterm{enumerator names}}139 \end{lstlisting} 140 Here, the enumeration type @Weekday@ defines the ordered \newterm{enumerator}s @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@.104 enum Weekday { Monday, Tuesday, Wednesday, Thursday=10, Friday, Saturday, Sunday }; 105 $\(\uparrow\)$ $\(\uparrow\)$ 106 ${\rm \newterm{enumeration name}}$ ${\rm \newterm{enumerator names}} 107 \end{lstlisting} 108 The example defines an enumeration type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@. 141 109 The successor of @Tuesday@ is @Monday@ and the predecessor of @Tuesday@ is @Wednesday@. 142 A C enumeration is implemented by an integral type, with consecutive enumerator values assigned by the compiler starting at zero or the next explicitly initialized value.143 For example, @Monday@ to @Wednesday@ have values 0--2 implicitly set by the compiler, @Thursday@ is explicitly set to @10@ , and @Friday@ to @Sunday@ have values 11--13 implicitly set by the compiler.110 A C enumeration is an integral type, with consecutive enumerator values assigned by the compiler starting at zero or the next explicitly initialized value by the programmer. 111 For example, @Monday@ to @Wednesday@ have values 0--2 implicitly set by the compiler, @Thursday@ is explicitly set to @10@ by the programmer, and @Friday@ to @Sunday@ have values 11--13 implicitly set by the compiler. 144 112 145 113 There are 3 attributes for an enumeration: \newterm{position}, \newterm{label}, and \newterm{value}: … … 150 118 \it position & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\ 151 119 \it label & Monday & Tuesday & Wednesday & Thursday & Friday & Saturday & Sunday \\ 152 \it value & 0 & 1 & 2 & {\color{red}10}& 11 & 12 & 13120 \it value & 0 & 1 & 2 & 10 & 11 & 12 & 13 153 121 \end{tabular} 154 122 \end{cquote} 155 123 156 124 The enumerators of an enumeration are unscoped, i.e., enumerators declared inside of an @enum@ are visible in the enclosing scope of the @enum@ type. 157 Furthermore, there is an implicit bidirectional conversion between an enumeration and integral types.158 125 \begin{lstlisting}[label=lst:enum_scope] 159 126 { 160 enum Weekday { ... }; $\C{// enumerators implicitly projected into local scope}$127 enum Weekday { ... }; // enumerators implicitly projected into local scope 161 128 Weekday weekday = Monday; 162 weekday = Friday; $\C{// weekday == 11}$ 163 int i = Sunday $\C{// i == 13}$ 164 weekday = 10000; $\C{// undefined behaviour}$ 129 weekday = Friday; 130 int i = Sunday // i == 13 165 131 } 166 int j = Wednesday; $\C{// ERROR! Wednesday is not declared in this scope}$132 int j = Wednesday; // ERROR! Wednesday is not declared in this scope 167 133 \end{lstlisting} 168 134 169 135 \section{\CFA-Style Enum} 170 136 171 \CFA supports C-Style enumeration using the same syntax and semantics for backwards compatibility. 172 \CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages. 173 174 \subsection{Enumerator Typing} 175 176 \CFA extends the enumeration by parameterizing the enumeration with a type for the enumerators, allowing enumerators to be assigned any values from the declared type. 137 A \CFA enumeration is parameterized by a type specifying each enumerator's type. 138 \CFA allows any object type for the enumerators, and values assigned to enumerators must be from the declared type. 177 139 \begin{lstlisting}[label=lst:color] 178 enum( @char@ ) Currency { Dollar = '$\textdollar$', Euro = '$\texteuro$', Pound = '$\textsterling$' }; 179 enum( @double@ ) Planet { Venus = 4.87, Earth = 5.97, Mars = 0.642 }; // mass 180 enum( @char *@ ) Colour { Red = "red", Green = "green", Blue = "blue" }; 181 enum( @Currency@ ) Europe { Euro = '$\texteuro$', Pound = '$\textsterling$' }; // intersection 182 \end{lstlisting} 183 The types of the enumerators are @char@, @double@, and @char *@ and each enumerator is initialized with corresponding type values. 184 % Only types with a defined ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}). 140 enum Colour( @char *@ ) { Red = "R", Green = "G", Blue = "B" }; 141 \end{lstlisting} 142 The type of @Colour@ is @char *@ and each enumerator is initialized with a C string. 143 Only types with a defined ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}). 185 144 186 145 % An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name. … … 192 151 A \CFA-enum can be scoped, meaning the enumerator constants are not projected into the enclosing scope. 193 152 \begin{lstlisting} 194 enum Weekday @!@ { /* as above */ }; 195 enum Colour( char * ) @!@ { /* as above */ }; 153 enum Colour( char * ) @!@ { ... }; 196 154 \end{lstlisting} 197 155 where the @'!'@ implies the enumerators are \emph{not} projected. … … 200 158 % $$<qualified\_expression> := <enum\_type>.<enumerator>$$ 201 159 \begin{lstlisting} 202 Weekday weekday = @Weekday.Monday@; $\C{// qualification}$ 203 Colour colour = @Colour.@Red; 160 Colour colour = @Colour.@Red; // qualification 204 161 colour = @Colour.@Blue; 205 162 \end{lstlisting} 206 163 207 \subsection{Enumeration Pseudo-functions} 208 209 Pseudo-functions are function-like operators that do not result in any run-time computations, i.e., like @sizeof@. 210 Often a call to a pseudo-function is substituted with information extracted from the symbol table at compilation time, like storage size or alignment associated with the underlying architecture.. 211 212 \subsubsection{Enumerator Attributes} 213 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@. 164 \section{Enumeration Pseudo-functions} 165 Pseudo-functions are function-like operators that do not result in any run-time computations, i.e., like @sizeof@. Instead, the call to functions will be substituted into other expressions in compilation time. 166 167 \subsection{Enumerator Attributes} 168 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@. 214 169 \begin{lstlisting} 215 int green_pos = @position@( Colour.Green ); $\C{// 1}$ 216 char * green_value = @value@( Colour.Green ); $\C{// "G"}$ 217 char * green_label = @label@( Colour.Green ); $\C{// "Green"}$ 218 \end{lstlisting} 219 220 Enumeration Greek may have more or less enumerators than Letter, but the enumerator values must be from Letter. 221 Therefore, Greek enumerators are a subset of type Letter and are type compatible with enumeration Letter, but Letter enumerators are not type compatible with enumeration Greek. 222 223 \subsubsection{\lstinline{enumerate()}} 224 170 int green_pos = @position@( Colour.Green ); // 1 171 char * green_value = @value@( Colour.Green ); / "G" 172 char * green_label = @label@( Colour.Green ); // "Green" 173 \end{lstlisting} 174 175 \subsection{enumerate()} 225 176 \begin{lstlisting}[label=lst:c_switch] 226 177 enum(int) C_ENUM { First, Second, Third = First, Fourth }; 227 int v( C_ENUM e ) { 228 switch( e ) { 229 case First: return 0; break; 230 case Second: return 1; break; 231 // case Third: return 2; break; 232 // case Fourth: return 3; break; 233 }; 234 }; 235 \end{lstlisting} 236 In the @C_ENUM@ example, @Third@ is an alias of @First@ and @Fourth@ is an alias of @Second@. 237 Programmers cannot make case branches for @Third@ and @Fourth@ because the switch statement matches cases by the enumerator's value. 238 Case @First@ and @Third@, or @Second@ and @Fourth@, has duplicate case values. 239 240 @enumerate()@ is a pseudo-function that makes the switch statement match by an enumerator instead. 178 int v(C_ENUM e) { 179 switch( e ) { 180 case First: return 0; break; 181 case Second: return 1; break; 182 // case Thrid: return 2; break; 183 // case Fourth: return 3; break; 184 }; 185 }; 186 \end{lstlisting} 187 In the @C_ENUM@ example, @Third@ is an alias of @First@ and @Fourth@ is an alias of @Second@. Programmers cannot make case branches for @Third@ and @Fourth@ because the switch statement matches cases by the enumerator's value. Case First and Third, or Second and Fourth, has duplicate case values. 188 189 @enumerate()@ is a pseudo-function that makes the switch statement match by an enumerator instead. 241 190 \begin{lstlisting}[label=lst:c_switch_enumerate] 242 191 enum(double) C_ENUM { First, Second, Third = First, Fourth }; 243 C_ENUM variable_a = First, variable_b = Second, variable_c = Th ird, variable_d = Fourth;244 int v(C_ENUM e) { 245 switch( enumeratate( e ) ) {246 case First: return e; break;247 case Second: return value( e ); break;248 case Third: return label( e ); break;249 case Fourth: return position( e ); break;250 };192 C_ENUM variable_a = First, variable_b = Second, variable_c = Thrid, variable_d = Fourth; 193 int v(C_ENUM e) { 194 switch( enumeratate( e ) ) { 195 case First: return e; break; 196 case Second: return value( e ); break; 197 case Thrid: return label( e ); break; 198 case Fourth: return position( e ); break; 199 }; 251 200 }; 252 201 p(variable_a); // 0 … … 256 205 \end{lstlisting} 257 206 258 259 207 \section{Enumeration Storage} 260 261 208 262 209 \subsection{Enumeration Variable} … … 281 228 >>> label( Colour, 1) -> char * 282 229 \end{lstlisting} 283 @T@ represents the type declared in the \CFA enumeration defined and @char *@ in the example. 230 @T@ represents the type declared in the \CFA enumeration defined and @char *@ in the example. 284 231 These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters. 285 232 286 287 233 \subsection{Enumeration Data} 288 289 234 \begin{lstlisting}[label=lst:enumeration_backing_data] 290 235 enum(T) E { ... }; 291 236 // backing data 292 T * E_values; 293 char ** E_labels; 294 \end{lstlisting} 295 Storing values and labels as arrays can sometimes help support enumeration features. 296 However, the data structures are the overhead for the programs. We want to reduce the memory usage for enumeration support by: 237 T* E_values; 238 char** E_labels; 239 \end{lstlisting} 240 Storing values and labels as arrays can sometimes help support enumeration features. However, the data structures are the overhead for the programs. We want to reduce the memory usage for enumeration support by: 297 241 \begin{itemize} 298 \item Only generates the data array if necessary 299 \item The compilation units share the data structures. 300 No extra overhead if the data structures are requested multiple times. 242 \item Only generates the data array if necessary 243 \item The compilation units share the data structures. No extra overhead if the data structures are requested multiple times. 301 244 \end{itemize} 302 245 303 246 247 \ 304 248 \section{Unification} 305 249 306 250 \subsection{Enumeration as Value} 307 251 \label{section:enumeration_as_value} 308 An \CFA enumeration with base type T can be used seamlessly as T, without explicitly calling the pseudo-function value. 252 An \CFA enumeration with base type T can be used seamlessly as T, without explicitly calling the pseudo-function value. 309 253 \begin{lstlisting}[label=lst:implicit_conversion] 310 254 char * green_value = Colour.Green; // "G" 311 // Is equivalent to 255 // Is equivalent to 312 256 // char * green_value = value( Color.Green ); "G" 313 257 \end{lstlisting} 314 258 315 316 259 \subsection{Unification Distance} 317 318 260 \begin{lstlisting}[label=lst:unification_distance_example] 319 261 T_2 Foo(T1); … … 323 265 @path(A, B)@ is a compiler concept that returns one of the following: 324 266 \begin{itemize} 325 \item Zero or 0, if and only if $A == B$.326 \item Safe, if B can be used as A without losing its precision, or B is a subtype of A. 327 \item Unsafe, if B loses its precision when used as A, or A is a subtype of B.328 \item Infinite, if B cannot be used as A. A is not a subtype of B and B is not a subtype of A.267 \item Zero or 0, if and only if $A == B$. 268 \item Safe, if B can be used as A without losing its precision, or B is a subtype of A. 269 \item Unsafe, if B loses its precision when used as A, or A is a subtype of B. 270 \item Infinite, if B cannot be used as A. A is not a subtype of B and B is not a subtype of A. 329 271 \end{itemize} 330 272 … … 336 278 The arithmetic of distance is the following: 337 279 \begin{itemize} 338 \item $Zero + v= v$, for some value v.339 \item $Safe * k < Unsafe$, for finite k. 340 \item $Unsafe * k < Infinite$, for finite k.341 \item $Infinite + v = Infinite$, for some value v.280 \item $Zero + v= v$, for some value v. 281 \item $Safe * k < Unsafe$, for finite k. 282 \item $Unsafe * k < Infinite$, for finite k. 283 \item $Infinite + v = Infinite$, for some value v. 342 284 \end{itemize} 343 285 … … 346 288 347 289 \subsection{Variable Overloading and Parameter Unification} 348 349 290 \CFA allows variable names to be overloaded. It is possible to overload a variable that has type T and an enumeration with type T. 350 291 \begin{lstlisting}[label=lst:variable_overload] … … 363 304 Similarly, functions can be overloaded with different signatures. \CFA picks the correct function entity based on the distance between parameter types and the arguments. 364 305 \begin{lstlisting}[label=lst:function_overload] 365 Colour green = Colour.Green; 306 Colour green = Colour.Green; 366 307 void foo(Colour c) { sout | "It is an enum"; } // First foo 367 308 void foo(char * s) { sout | "It is a string"; } // Second foo … … 385 326 % The @EnumInstType@ is convertible to other types. 386 327 % A \CFA enumeration expression is implicitly \emph{overloaded} with its three different attributes: value, position, and label. 387 % The \CFA compilers need to resolve an @EnumInstType@ as one of its attributes based on the current context. 328 % The \CFA compilers need to resolve an @EnumInstType@ as one of its attributes based on the current context. 388 329 389 330 % \begin{lstlisting}[caption={Null Context}, label=lst:null_context] … … 438 379 % } 439 380 % \end{lstlisting} 440 % % The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType. 381 % % The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType. 441 382 % Backward conversion: 442 383 % \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call] … … 448 389 % \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call] 449 390 % { 450 % Unification( EnumInstType<Colour>, int ) >>> label391 % Unification( EnumInstType<Colour>, int ) >>> label 451 392 % } 452 393 % \end{lstlisting} 453 394 % @int@ can be unified with the label of Colour. 454 % @5@ is a constant expression $\Rightarrow$ Compiler knows the value during the compilation $\Rightarrow$ turns it into 395 % @5@ is a constant expression $\Rightarrow$ Compiler knows the value during the compilation $\Rightarrow$ turns it into 455 396 % \begin{lstlisting} 456 397 % { 457 % enum Colour colour = Colour.Green;398 % enum Colour colour = Colour.Green; 458 399 % } 459 400 % \end{lstlisting} … … 470 411 % { 471 412 % enum T (int) { ... } // Declaration 472 % enum T t = 1; 413 % enum T t = 1; 473 414 % } 474 415 % \end{lstlisting} … … 482 423 % return the FIRST enumeration constant that has the value 1, by searching through the values array 483 424 % \end{enumerate} 484 % The downside of the precedence rule: @EnumInstType@ $\Rightarrow$ @int ( value )@ $\Rightarrow$ @EnumInstType@ may return a different @EnumInstType@ because the value can be repeated and there is no way to know which one is expected $\Rightarrow$ want uniqueness 425 % The downside of the precedence rule: @EnumInstType@ $\Rightarrow$ @int ( value )@ $\Rightarrow$ @EnumInstType@ may return a different @EnumInstType@ because the value can be repeated and there is no way to know which one is expected $\Rightarrow$ want uniqueness 485 426 486 427 % \subsection{Casting} … … 490 431 % (int) Foo.A; 491 432 % \end{lstlisting} 492 % The \CFA-compiler unifies @EnumInstType<int>@ with int, with returns @value( Foo.A )@, which has statically known value 10. In other words, \CFA-compiler is aware of a cast expression, and it forms the context for EnumInstType resolution. The expression with type @EnumInstType<int>@ can be replaced by the compile with a constant expression 10, and optionally discard the cast expression. 433 % The \CFA-compiler unifies @EnumInstType<int>@ with int, with returns @value( Foo.A )@, which has statically known value 10. In other words, \CFA-compiler is aware of a cast expression, and it forms the context for EnumInstType resolution. The expression with type @EnumInstType<int>@ can be replaced by the compile with a constant expression 10, and optionally discard the cast expression. 493 434 494 435 % \subsection{Value Conversion} … … 504 445 % int j = value( Foo, a ) 505 446 % \end{lstlisting} 506 % Similarly, the generated code for the third line is 447 % Similarly, the generated code for the third line is 507 448 % \begin{lstlisting} 508 449 % char * j = label( Foo, a ) … … 514 455 515 456 \subsection{C Enumeration Rule} 516 A C enumeration has an integral type. If not initialized, the first enumerator implicitly has the integral value 0, and other enumerators have a value equal to its $predecessor + 1$. 457 A C enumeration has an integral type. If not initialized, the first enumerator implicitly has the integral value 0, and other enumerators have a value equal to its $predecessor + 1$. 517 458 518 459 \subsection{Auto Initializable} … … 537 478 Odd ?++( Odd t1 ) { return Odd( t1.i + 2); }; 538 479 \end{lstlisting} 539 When the type of an enumeration is @AutoInitializable@, implicit initialization is available. 480 When the type of an enumeration is @AutoInitializable@, implicit initialization is available. 540 481 \begin{lstlisting}[label=lst:sample_auto_Initializable_usage] 541 482 enum AutoInitUsage(Odd) { … … 573 514 @alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule. 574 515 575 \textbullet\ \CFA offers a shorthand for iterating all enumeration constants: 516 \textbullet\ \CFA offers a shorthand for iterating all enumeration constants: 576 517 \begin{lstlisting}[label=lst:range_functions] 577 518 for ( Alphabet alph ) { sout | alph; } … … 626 567 >>> 10 11 12 13 14 15 16 17 18 627 568 \end{lstlisting} 628 The first syntax is stepping to the next enumeration constant, which is the default stepping scheme if not explicitly specified. The second syntax, on the other hand, is to call @operator+=@ @one_type@ on the @value( s )@. Therefore, the second syntax is equivalent to 569 The first syntax is stepping to the next enumeration constant, which is the default stepping scheme if not explicitly specified. The second syntax, on the other hand, is to call @operator+=@ @one_type@ on the @value( s )@. Therefore, the second syntax is equivalent to 629 570 \begin{lstlisting}[label=lst:range_function_stepping_converted] 630 571 for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1 ) { sout | alph; } … … 638 579 for ( char * alph; Alphabet ) 639 580 \end{lstlisting} 640 This for-loop implicitly iterates every label of the enumeration, because a label is the only valid resolution to @ch@with type @char *@ in this case.581 This for-loop implicitly iterates every label of the enumeration, because a label is the only valid resolution to the ch with type @char *@ in this case. 641 582 If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration. 642 583 \begin{lstlisting}[label=lst:range_functions_label_implicit] … … 650 591 % \begin{lstlisting} 651 592 % enum T( int, char * ) { 652 % a=42, b="Hello World"593 % a=42, b="Hello World" 653 594 % }; 654 595 % \end{lstlisting} 655 % The enum T declares two different types: int and char *. The enumerators of T hold values of one of the declared types. 596 % The enum T declares two different types: int and char *. The enumerators of T hold values of one of the declared types. 656 597 657 598 \subsection{Enumeration Inheritance} … … 661 602 enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" }; 662 603 \end{lstlisting} 663 \lstinline{Inline} allows Enumeration Name2 to inherit enumerators from Name1 by containment, and a Name enumeration is a subtype of enumeration Name2. An enumeration instance of type Name can be used where an instance of Name2 is expected. 604 \lstinline{Inline} allows Enumeration Name2 to inherit enumerators from Name1 by containment, and a Name enumeration is a subtype of enumeration Name2. An enumeration instance of type Name can be used where an instance of Name2 is expected. 664 605 \begin{lstlisting}[label=lst:EnumInline] 665 606 Name Fred; … … 669 610 If enumeration A declares @inline B@ in its enumeration body, enumeration A is the "inlining enum" and enumeration B is the "inlined enum". 670 611 671 An enumeration can inline at most one other enumeration. The inline declaration must be placed before the first enumerator of the inlining enum. The inlining enum has all the enumerators from the inlined enum, with the same labels, values, and position. 612 An enumeration can inline at most one other enumeration. The inline declaration must be placed before the first enumerator of the inlining enum. The inlining enum has all the enumerators from the inlined enum, with the same labels, values, and position. 672 613 \begin{lstlisting}[label=lst:EnumInline] 673 614 enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" }; … … 684 625 \begin{lstlisting}[label=lst:static_attr] 685 626 enum( char * ) Colour { 686 Red = "red", Blue = "blue", Green = "green" 687 }; 688 \end{lstlisting} 689 An enumerator expression returns its enumerator value as a constant expression with no runtime cost. For example, @Colour.Red@ is equivalent to the constant expression "red", and \CFA finishes the expression evaluation before generating the corresponding C code. Applying a pseudo-function to a constant enumerator expression results in a constant expression as well. @value( Colour.Red )@, @position( Colour. Red )@, and @label( Colour.Red )@ are equivalent to constant expression with char * value "red", int value 0, and char * value "Red", respectively. 627 Red = "red", Blue = "blue", Green = "green" 628 }; 629 \end{lstlisting} 630 An enumerator expression returns its enumerator value as a constant expression with no runtime cost. For example, @Colour.Red@ is equivalent to the constant expression "red", and \CFA finishes the expression evaluation before generating the corresponding C code. Applying a pseudo-function to a constant enumerator expression results in a constant expression as well. @value( Colour.Red )@, @position( Colour. Red )@, and @label( Colour.Red )@ are equivalent to constant expression with char * value "red", int value 0, and char * value "Red", respectively. 690 631 691 632 \subsection{Runtime Attribute Expression and Weak Referenced Data} … … 697 638 An enumeration variable c is equivalent to an integer variable with the value of @position( c )@ In Example~\ref{lst:dynamic_attr}, the value of enumeration variable c is unknown at compile time. In this case, the pseudo-function calls are reduced to expression that returns the enumerator values at runtime. 698 639 699 \CFA stores the variables and labels in @const@ arrays to provide runtime lookup for enumeration information.640 \CFA stores the variables and labels in const arrays to provide runtime lookup for enumeration information. 700 641 701 642 \begin{lstlisting}[label=lst:attr_array] … … 710 651 \end{lstlisting} 711 652 712 To avoid unnecessary memory usage, the labels and values array are only generated as needed, and only generate once across all compilation units. By default, \CFA defers the declaration of the label and value arrays until an call to attribute function with a dynamic value. If an attribute function is never called on a dynamic value of an enumerator, the array will never be allocated. Once the arrays are created, all compilation units share a weak reference to the allocation array. 653 To avoid unnecessary memory usage, the labels and values array are only generated as needed, and only generate once across all compilation units. By default, \CFA defers the declaration of the label and value arrays until an call to attribute function with a dynamic value. If an attribute function is never called on a dynamic value of an enumerator, the array will never be allocated. Once the arrays are created, all compilation units share a weak reference to the allocation array. 713 654 714 655 \subsection{Enum Prelude} … … 716 657 \begin{lstlisting}[label=lst:enum_func_dec] 717 658 forall( T ) { 718 unsigned position( unsigned );719 T value( unsigned );720 char * label( unsigned );659 unsigned position( unsigned ); 660 T value( unsigned ); 661 char * label( unsigned ); 721 662 } 722 663 \end{lstlisting} … … 729 670 forall(T) 730 671 class EnumDecl { 731 T* values;732 char** label;672 T* values; 673 char** label; 733 674 }; 734 675 \end{lstlisting} … … 738 679 \begin{lstlisting}[label=lst:EnumInstType] 739 680 class EnumInstType { 740 EnumDecl enumDecl;741 int position;681 EnumDecl enumDecl; 682 int position; 742 683 }; 743 684 \end{lstlisting} … … 759 700 % struct Companion { 760 701 % const T * const values; 761 % const char * label;702 % const char * label; 762 703 % int length; 763 704 % }; … … 765 706 % \CFA generates companion objects, an instance of structure that encloses @necessary@ data to represent an enumeration. The size of the companion is unknown at the compilation time, and it "grows" in size to compensate for the @usage@. 766 707 767 % The companion object is singleton across the compilation (investigation). 708 % The companion object is singleton across the compilation (investigation). 768 709 769 710 % \CFA generates the definition of companion functions. … … 786 727 \begin{lstlisting}[label=lst:companion_trait] 787 728 forall(T1) { 788 trait Companion(otype T2<otype T1>) {789 T1 value((otype T2<otype T1> const &);790 int position(otype T2<otype T1> const &);791 char * label(otype T2<otype T1> const &);792 }729 trait Companion(otype T2<otype T1>) { 730 T1 value((otype T2<otype T1> const &); 731 int position(otype T2<otype T1> const &); 732 char * label(otype T2<otype T1> const &); 733 } 793 734 } 794 735 \end{lstlisting} … … 802 743 \begin{lstlisting} 803 744 enum(int) Weekday { 804 Monday=10, Tuesday, ...745 Monday=10, Tuesday, ... 805 746 }; 806 747 … … 817 758 \subsection{User Define Enumeration Functions} 818 759 819 Companion objects make extending features for \CFA enumeration easy. 760 Companion objects make extending features for \CFA enumeration easy. 820 761 \begin{lstlisting}[label=lst:companion_user_definition] 821 char * charastic_string( Companion o, int position ) { 822 return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) ); 762 char * charastic_string( Companion o, int position ) { 763 return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) ); 823 764 } 824 765 printf( charactic_string ( Color, 1 ) ); … … 835 776 Similarly, the user can work with the enumeration type itself: (see section ref...) 836 777 \begin{lstlisting}[ label=lst:companion_user_definition] 837 void print_enumerators ( Companion o ) { 778 void print_enumerators ( Companion o ) { 838 779 for ( c : Companion o ) { 839 780 sout | label (c) | value( c ) ; 840 } 781 } 841 782 } 842 783 print_enumerators( Colour ); … … 854 795 It ensures that the name of an enumerator is unique within the enumeration body, and checks if all values of the enumerator have the declaration type. 855 796 If the declared type is not @AutoInitializable@, \CFA rejects the enumeration definition. 856 Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section) 797 Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section) 857 798 858 799 \begin{lstlisting}[label=lst:init] … … 862 803 T ?+?( T & lhs, T & rhs ) { ... }; 863 804 864 enum (T) Sample { 865 Zero: 0 /* zero_t */, 805 enum (T) Sample { 806 Zero: 0 /* zero_t */, 866 807 One: Zero + 1 /* ?+?( Zero, one_t ) */ , ... 867 808 }; … … 885 826 \subsection{Qualified Expression} 886 827 887 \CFA uses qualified expression to address the scoping of \CFA-enumeration. 828 \CFA uses qualified expression to address the scoping of \CFA-enumeration. 888 829 \begin{lstlisting}[label=lst:qualified_expression] 889 830 aggregation_name.field; … … 896 837 897 838 \subsection{\lstinline{with} Clause/Statement} 898 899 839 Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible. 900 840 \begin{lstlisting}[label=lst:declaration] … … 902 842 enum Animal( int ) { Cat=10, Dog=20 }; 903 843 with ( Color, Animal ) { 904 char * red_string = Red; // value( Color.Red )905 int cat = Cat; // value( Animal.Cat )844 char * red_string = Red; // value( Color.Red ) 845 int cat = Cat; // value( Animal.Cat ) 906 846 } 907 847 \end{lstlisting} … … 911 851 enum RGB( int ) { Red=0, Green=1, Blue=2 }; 912 852 with ( Color, RGB ) { 913 // int red = Red; 853 // int red = Red; 914 854 } 915 855 \end{lstlisting} … … 925 865 The declaration \CFA-enumeration variable has the same syntax as the C-enumeration. Internally, such a variable will be represented as an EnumInstType. 926 866 927 \section{Related Work}928 929 Enumerations exist in many popular programming languages, e.g., Pascal, Ada, \Csharp, \CC, Go, Java, Modula-3, Rust, Swift, Python, and Algebraic data type in functional programming.930 There are a large set of overlapping features for all the languages, but each language has its own unique restrictions and extensions.931 932 \subsection{Pascal}933 934 \subsection{Ada}935 936 \subsection{\Csharp}937 938 \subsection{\CC}939 940 Because \CC is backwards compatible with C, it inherited C's enumerations, except there is no implicit conversion from an integral value to an enumeration;941 hence, the values in a \CC enumeration can only be its enumerators.942 943 \CC{11} extended enumeration with a scoped enumeration, \lstinline[language=c++]{enum class} (or \lstinline[language=c++]{enum struct}), where the enumerators are local to the enumeration and are accessed using type qualification, e.g., @Weekday::Monday@.944 \CC{20} supports unscoped access with a \lstinline[language=c++]{using enum} declaration.945 946 For both unscoped and scoped enumerations, the underlying type is an implementation-defined integral type that is large enough to hold all enumerated values; it does not have to be the smallest possible type.947 The underlying integral type can be explicitly specified:948 \begin{lstlisting}[language=c++,{moredelim=**[is][\color{red}]{@}{@}}]949 enum class RGB : @long@ { Red, Green, Blue };950 enum class rgb : @char@ { Red = 'r', Green = 'g', Blue = 'b' };951 enum class srgb : @signed char@ { Red = -1, Green = 0, Blue = 1 };952 \end{lstlisting}953 954 \subsection{Go}955 956 \subsection{Java}957 958 \subsection{Modula-3}959 960 \subsection{Rust}961 962 \subsection{Swift}963 964 \subsection{Python}965 966 \subsection{Algebraic Data Type}967 867 968 868 \end{document}
Note:
See TracChangeset
for help on using the changeset viewer.