Changeset dd1ebb1
- Timestamp:
- Jan 27, 2024, 11:26:17 PM (11 months ago)
- Branches:
- master
- Children:
- 765ee42
- Parents:
- d1551a5
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/proposals/enum.tex
rd1551a5 rdd1ebb1 7 7 \usepackage{graphics} 8 8 \usepackage{xspace} 9 \usepackage{calc} % latex arithmetic 9 10 10 11 \makeatletter … … 22 23 \newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi} 23 24 \newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi} 25 26 \newcommand{\LstBasicStyle}[1]{{\lst@basicstyle{#1}}} 27 \newcommand{\LstKeywordStyle}[1]{{\lst@basicstyle{\lst@keywordstyle{#1}}}} 28 \newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}} 29 \newcommand{\LstStringStyle}[1]{{\lst@basicstyle{\lst@stringstyle{#1}}}} 30 \newcommand{\LstNumberStyle}[1]{{\lst@basicstyle{\lst@numberstyle{#1}}}} 31 32 \newlength{\gcolumnposn} % temporary hack because lstlisting does not handle tabs correctly 33 \newlength{\columnposn} 34 \setlength{\gcolumnposn}{3in} 35 \setlength{\columnposn}{\gcolumnposn} 36 \newcommand{\setgcolumn}[1]{\global\gcolumnposn=#1\global\columnposn=\gcolumnposn} 37 \newcommand{\C}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\LstCommentStyle{#2}}} 38 \newcommand{\CD}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\LstBasicStyle{#2}}} 39 \newcommand{\CRT}{\global\columnposn=\gcolumnposn} 24 40 \makeatother 25 41 … … 56 72 57 73 \lstdefinestyle{CStyle}{ 58 % backgroundcolor=\color{backgroundColour}, 74 % backgroundcolor=\color{backgroundColour}, 59 75 % commentstyle=\color{mGreen}, 60 76 % keywordstyle=\color{magenta}, … … 64 80 basicstyle=\small\linespread{0.9}\sf, % reduce line spacing and use sanserif font 65 81 % basicstyle=\footnotesize, 66 breakatwhitespace=false, 67 % breaklines=true, 68 captionpos=b, 69 keepspaces=true, 82 breakatwhitespace=false, 83 % breaklines=true, 84 captionpos=b, 85 keepspaces=true, 70 86 escapechar=\$, % LaTeX escape in CFA code 71 % numbers=left, 72 % numbersep=5pt, 87 % numbers=left, 88 % numbersep=5pt, 73 89 % numberstyle=\tiny\color{mGray}, 74 % showspaces=false, 90 % showspaces=false, 75 91 showstringspaces=false, 76 % showtabs=false, 92 % showtabs=false, 77 93 showlines=true, % show blank lines at end of code 78 94 tabsize=5, … … 93 109 94 110 \begin{abstract} 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. 111 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@. 112 C and \CC restrict an enumeration type to the integral type @signed int@, meaning enumeration names bind to integer constants. 113 \CFA extends C enumerations to allow all basic and custom types for the enumeration type, like other modern programming languages. 114 Furthermore, \CFA adds other useful features for enumerations to support better software-engineering practices and simplify program development. 98 115 \end{abstract} 99 116 117 \section{Background} 118 119 Naming values is a common practice in mathematics and engineering, e.g., $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MHz (1E6), etc. 120 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). 121 Many programming languages capture this important capability through a mechanism called an \newterm{enumeration}. 122 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. 123 Note, all enumeration names must be unique but different names can represent the same value (eight note, quaver), which are synonyms. 124 125 Specifically, an enumerated type is a type whose values are restricted to a fixed set of named constants. 126 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. 127 However, the values for basic types are not named, other than the programming-language supplied constants. 128 129 100 130 \section{C-Style Enum} 101 131 102 \CFA supports the C-Style enumeration using the samesyntax and semantics.132 The C-Style enumeration has the following syntax and semantics. 103 133 \begin{lstlisting}[label=lst:weekday] 104 enum Weekday { Monday, Tuesday, Wednesday, Thursday =10, Friday, Saturday, Sunday };134 enum Weekday { Monday, Tuesday, Wednesday, Thursday@=10@, Friday, Saturday, Sunday }; 105 135 $\(\uparrow\)$ $\(\uparrow\)$ 106 136 ${\rm \newterm{enumeration name}}$ ${\rm \newterm{enumerator names}} 107 137 \end{lstlisting} 108 The example defines an enumeration type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@.138 Here, the enumeration type @Weekday@ defines the ordered \newterm{enumerator}s @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@. 109 139 The successor of @Tuesday@ is @Monday@ and the predecessor of @Tuesday@ is @Wednesday@. 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.140 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. 141 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. 112 142 113 143 There are 3 attributes for an enumeration: \newterm{position}, \newterm{label}, and \newterm{value}: … … 118 148 \it position & 0 & 1 & 2 & 3 & 4 & 5 & 6 \\ 119 149 \it label & Monday & Tuesday & Wednesday & Thursday & Friday & Saturday & Sunday \\ 120 \it value & 0 & 1 & 2 & 10& 11 & 12 & 13150 \it value & 0 & 1 & 2 & {\color{red}10}& 11 & 12 & 13 121 151 \end{tabular} 122 152 \end{cquote} 123 153 124 154 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. 155 Furthermore, there is an implicit bidirectional conversion between an enumeration and integral types. 125 156 \begin{lstlisting}[label=lst:enum_scope] 126 157 { 127 enum Weekday { ... }; // enumerators implicitly projected into local scope158 enum Weekday { ... }; $\C{// enumerators implicitly projected into local scope}$ 128 159 Weekday weekday = Monday; 129 weekday = Friday; 130 int i = Sunday // i == 13 160 weekday = Friday; $\C{// weekday == 11}$ 161 int i = Sunday $\C{// i == 13}$ 162 weekday = 10000; $\C{// undefined behaviour}$ 131 163 } 132 int j = Wednesday; // ERROR! Wednesday is not declared in this scope164 int j = Wednesday; $\C{// ERROR! Wednesday is not declared in this scope}$ 133 165 \end{lstlisting} 134 166 135 167 \section{\CFA-Style Enum} 136 168 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. 169 \CFA supports C-Style enumeration using the same syntax and semantics for backwards compatibility. 170 \CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages. 171 172 \subsection{Enumerator Typing} 173 174 \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. 139 175 \begin{lstlisting}[label=lst:color] 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}). 176 enum( @char@ ) Currency { Dollar = '$\textdollar$', Euro = '$\texteuro$', Pound = '$\textsterling$' }; 177 enum( @double@ ) Planet { Venus = 4.87, Earth = 5.97, Mars = 0.642 }; // mass 178 enum( @char *@ ) Colour { Red = "red", Green = "green", Blue = "blue" }; 179 enum( @Currency@ ) Europe { Euro = '$\texteuro$', Pound = '$\textsterling$' }; // intersection 180 \end{lstlisting} 181 The types of the enumerators are @char@, @double@, and @char *@ and each enumerator is initialized with corresponding type values. 182 % Only types with a defined ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}). 144 183 145 184 % An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name. … … 158 197 % $$<qualified\_expression> := <enum\_type>.<enumerator>$$ 159 198 \begin{lstlisting} 160 Colour colour = @Colour.@Red; // qualification199 Colour colour = @Colour.@Red; $\C{// qualification}$ 161 200 colour = @Colour.@Blue; 162 201 \end{lstlisting} 163 202 164 \s ection{Enumeration Pseudo-functions}203 \subsection{Enumeration Pseudo-functions} 165 204 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 205 167 \subs ection{Enumerator Attributes}168 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@. 206 \subsubsection{Enumerator Attributes} 207 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@. 169 208 \begin{lstlisting} 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()} 209 int green_pos = @position@( Colour.Green ); $\C{// 1}$ 210 char * green_value = @value@( Colour.Green ); $\C{// "G"}$ 211 char * green_label = @label@( Colour.Green ); $\C{// "Green"}$ 212 \end{lstlisting} 213 214 Enumeration Greek may have more or less enumerators than Letter, but the enumerator values must be from Letter. 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. 215 216 \subsubsection{\lstinline{enumerate()}} 217 176 218 \begin{lstlisting}[label=lst:c_switch] 177 219 enum(int) C_ENUM { First, Second, Third = First, Fourth }; 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. 220 int v( C_ENUM e ) { 221 switch( e ) { 222 case First: return 0; break; 223 case Second: return 1; break; 224 // case Third: return 2; break; 225 // case Fourth: return 3; break; 226 }; 227 }; 228 \end{lstlisting} 229 In the @C_ENUM@ example, @Third@ is an alias of @First@ and @Fourth@ is an alias of @Second@. 230 Programmers cannot make case branches for @Third@ and @Fourth@ because the switch statement matches cases by the enumerator's value. 231 Case @First@ and @Third@, or @Second@ and @Fourth@, has duplicate case values. 232 233 @enumerate()@ is a pseudo-function that makes the switch statement match by an enumerator instead. 190 234 \begin{lstlisting}[label=lst:c_switch_enumerate] 191 235 enum(double) C_ENUM { First, Second, Third = First, Fourth }; 192 C_ENUM variable_a = First, variable_b = Second, variable_c = Th rid, variable_d = Fourth;193 int v(C_ENUM e) { 194 195 196 197 case Thrid: return label( e ); break;198 199 236 C_ENUM variable_a = First, variable_b = Second, variable_c = Third, variable_d = Fourth; 237 int v(C_ENUM e) { 238 switch( enumeratate( e ) ) { 239 case First: return e; break; 240 case Second: return value( e ); break; 241 case Third: return label( e ); break; 242 case Fourth: return position( e ); break; 243 }; 200 244 }; 201 245 p(variable_a); // 0 … … 205 249 \end{lstlisting} 206 250 251 207 252 \section{Enumeration Storage} 253 208 254 209 255 \subsection{Enumeration Variable} … … 228 274 >>> label( Colour, 1) -> char * 229 275 \end{lstlisting} 230 @T@ represents the type declared in the \CFA enumeration defined and @char *@ in the example. 276 @T@ represents the type declared in the \CFA enumeration defined and @char *@ in the example. 231 277 These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters. 232 278 279 233 280 \subsection{Enumeration Data} 281 234 282 \begin{lstlisting}[label=lst:enumeration_backing_data] 235 283 enum(T) E { ... }; 236 284 // backing data 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: 285 T * E_values; 286 char ** E_labels; 287 \end{lstlisting} 288 Storing values and labels as arrays can sometimes help support enumeration features. 289 However, the data structures are the overhead for the programs. We want to reduce the memory usage for enumeration support by: 241 290 \begin{itemize} 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. 291 \item Only generates the data array if necessary 292 \item The compilation units share the data structures. 293 No extra overhead if the data structures are requested multiple times. 244 294 \end{itemize} 245 295 246 296 247 \248 297 \section{Unification} 249 298 250 299 \subsection{Enumeration as Value} 251 300 \label{section:enumeration_as_value} 252 An \CFA enumeration with base type T can be used seamlessly as T, without explicitly calling the pseudo-function value. 301 An \CFA enumeration with base type T can be used seamlessly as T, without explicitly calling the pseudo-function value. 253 302 \begin{lstlisting}[label=lst:implicit_conversion] 254 303 char * green_value = Colour.Green; // "G" 255 // Is equivalent to 304 // Is equivalent to 256 305 // char * green_value = value( Color.Green ); "G" 257 306 \end{lstlisting} 258 307 308 259 309 \subsection{Unification Distance} 310 260 311 \begin{lstlisting}[label=lst:unification_distance_example] 261 312 T_2 Foo(T1); … … 265 316 @path(A, B)@ is a compiler concept that returns one of the following: 266 317 \begin{itemize} 267 268 \item Safe, if B can be used as A without losing its precision, or B is a subtype of A. 269 270 318 \item Zero or 0, if and only if $A == B$. 319 \item Safe, if B can be used as A without losing its precision, or B is a subtype of A. 320 \item Unsafe, if B loses its precision when used as A, or A is a subtype of B. 321 \item Infinite, if B cannot be used as A. A is not a subtype of B and B is not a subtype of A. 271 322 \end{itemize} 272 323 … … 278 329 The arithmetic of distance is the following: 279 330 \begin{itemize} 280 281 \item $Safe * k < Unsafe$, for finite k. 282 283 331 \item $Zero + v= v$, for some value v. 332 \item $Safe * k < Unsafe$, for finite k. 333 \item $Unsafe * k < Infinite$, for finite k. 334 \item $Infinite + v = Infinite$, for some value v. 284 335 \end{itemize} 285 336 … … 288 339 289 340 \subsection{Variable Overloading and Parameter Unification} 341 290 342 \CFA allows variable names to be overloaded. It is possible to overload a variable that has type T and an enumeration with type T. 291 343 \begin{lstlisting}[label=lst:variable_overload] … … 304 356 Similarly, functions can be overloaded with different signatures. \CFA picks the correct function entity based on the distance between parameter types and the arguments. 305 357 \begin{lstlisting}[label=lst:function_overload] 306 Colour green = Colour.Green; 358 Colour green = Colour.Green; 307 359 void foo(Colour c) { sout | "It is an enum"; } // First foo 308 360 void foo(char * s) { sout | "It is a string"; } // Second foo … … 326 378 % The @EnumInstType@ is convertible to other types. 327 379 % A \CFA enumeration expression is implicitly \emph{overloaded} with its three different attributes: value, position, and label. 328 % The \CFA compilers need to resolve an @EnumInstType@ as one of its attributes based on the current context. 380 % The \CFA compilers need to resolve an @EnumInstType@ as one of its attributes based on the current context. 329 381 330 382 % \begin{lstlisting}[caption={Null Context}, label=lst:null_context] … … 379 431 % } 380 432 % \end{lstlisting} 381 % % The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType. 433 % % The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType. 382 434 % Backward conversion: 383 435 % \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call] … … 389 441 % \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call] 390 442 % { 391 % 443 % Unification( EnumInstType<Colour>, int ) >>> label 392 444 % } 393 445 % \end{lstlisting} 394 446 % @int@ can be unified with the label of Colour. 395 % @5@ is a constant expression $\Rightarrow$ Compiler knows the value during the compilation $\Rightarrow$ turns it into 447 % @5@ is a constant expression $\Rightarrow$ Compiler knows the value during the compilation $\Rightarrow$ turns it into 396 448 % \begin{lstlisting} 397 449 % { 398 % enum Colour colour = Colour.Green;450 % enum Colour colour = Colour.Green; 399 451 % } 400 452 % \end{lstlisting} … … 411 463 % { 412 464 % enum T (int) { ... } // Declaration 413 % enum T t = 1; 465 % enum T t = 1; 414 466 % } 415 467 % \end{lstlisting} … … 423 475 % return the FIRST enumeration constant that has the value 1, by searching through the values array 424 476 % \end{enumerate} 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 477 % 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 426 478 427 479 % \subsection{Casting} … … 431 483 % (int) Foo.A; 432 484 % \end{lstlisting} 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. 485 % 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. 434 486 435 487 % \subsection{Value Conversion} … … 445 497 % int j = value( Foo, a ) 446 498 % \end{lstlisting} 447 % Similarly, the generated code for the third line is 499 % Similarly, the generated code for the third line is 448 500 % \begin{lstlisting} 449 501 % char * j = label( Foo, a ) … … 455 507 456 508 \subsection{C Enumeration Rule} 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$. 509 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$. 458 510 459 511 \subsection{Auto Initializable} … … 478 530 Odd ?++( Odd t1 ) { return Odd( t1.i + 2); }; 479 531 \end{lstlisting} 480 When the type of an enumeration is @AutoInitializable@, implicit initialization is available. 532 When the type of an enumeration is @AutoInitializable@, implicit initialization is available. 481 533 \begin{lstlisting}[label=lst:sample_auto_Initializable_usage] 482 534 enum AutoInitUsage(Odd) { … … 514 566 @alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule. 515 567 516 \textbullet\ \CFA offers a shorthand for iterating all enumeration constants: 568 \textbullet\ \CFA offers a shorthand for iterating all enumeration constants: 517 569 \begin{lstlisting}[label=lst:range_functions] 518 570 for ( Alphabet alph ) { sout | alph; } … … 567 619 >>> 10 11 12 13 14 15 16 17 18 568 620 \end{lstlisting} 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 621 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 570 622 \begin{lstlisting}[label=lst:range_function_stepping_converted] 571 623 for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1 ) { sout | alph; } … … 579 631 for ( char * alph; Alphabet ) 580 632 \end{lstlisting} 581 This for-loop implicitly iterates every label of the enumeration, because a label is the only valid resolution to the chwith type @char *@ in this case.633 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. 582 634 If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration. 583 635 \begin{lstlisting}[label=lst:range_functions_label_implicit] … … 591 643 % \begin{lstlisting} 592 644 % enum T( int, char * ) { 593 % 645 % a=42, b="Hello World" 594 646 % }; 595 647 % \end{lstlisting} 596 % The enum T declares two different types: int and char *. The enumerators of T hold values of one of the declared types. 648 % The enum T declares two different types: int and char *. The enumerators of T hold values of one of the declared types. 597 649 598 650 \subsection{Enumeration Inheritance} … … 602 654 enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" }; 603 655 \end{lstlisting} 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. 656 \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. 605 657 \begin{lstlisting}[label=lst:EnumInline] 606 658 Name Fred; … … 610 662 If enumeration A declares @inline B@ in its enumeration body, enumeration A is the "inlining enum" and enumeration B is the "inlined enum". 611 663 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. 664 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. 613 665 \begin{lstlisting}[label=lst:EnumInline] 614 666 enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" }; … … 625 677 \begin{lstlisting}[label=lst:static_attr] 626 678 enum( char * ) Colour { 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. 679 Red = "red", Blue = "blue", Green = "green" 680 }; 681 \end{lstlisting} 682 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. 631 683 632 684 \subsection{Runtime Attribute Expression and Weak Referenced Data} … … 638 690 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. 639 691 640 \CFA stores the variables and labels in const arrays to provide runtime lookup for enumeration information.692 \CFA stores the variables and labels in @const@ arrays to provide runtime lookup for enumeration information. 641 693 642 694 \begin{lstlisting}[label=lst:attr_array] … … 651 703 \end{lstlisting} 652 704 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. 705 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. 654 706 655 707 \subsection{Enum Prelude} … … 657 709 \begin{lstlisting}[label=lst:enum_func_dec] 658 710 forall( T ) { 659 660 661 711 unsigned position( unsigned ); 712 T value( unsigned ); 713 char * label( unsigned ); 662 714 } 663 715 \end{lstlisting} … … 670 722 forall(T) 671 723 class EnumDecl { 672 673 724 T* values; 725 char** label; 674 726 }; 675 727 \end{lstlisting} … … 679 731 \begin{lstlisting}[label=lst:EnumInstType] 680 732 class EnumInstType { 681 682 733 EnumDecl enumDecl; 734 int position; 683 735 }; 684 736 \end{lstlisting} … … 700 752 % struct Companion { 701 753 % const T * const values; 702 % 754 % const char * label; 703 755 % int length; 704 756 % }; … … 706 758 % \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@. 707 759 708 % The companion object is singleton across the compilation (investigation). 760 % The companion object is singleton across the compilation (investigation). 709 761 710 762 % \CFA generates the definition of companion functions. … … 727 779 \begin{lstlisting}[label=lst:companion_trait] 728 780 forall(T1) { 729 730 731 732 733 781 trait Companion(otype T2<otype T1>) { 782 T1 value((otype T2<otype T1> const &); 783 int position(otype T2<otype T1> const &); 784 char * label(otype T2<otype T1> const &); 785 } 734 786 } 735 787 \end{lstlisting} … … 743 795 \begin{lstlisting} 744 796 enum(int) Weekday { 745 797 Monday=10, Tuesday, ... 746 798 }; 747 799 … … 758 810 \subsection{User Define Enumeration Functions} 759 811 760 Companion objects make extending features for \CFA enumeration easy. 812 Companion objects make extending features for \CFA enumeration easy. 761 813 \begin{lstlisting}[label=lst:companion_user_definition] 762 char * charastic_string( Companion o, int position ) { 763 return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) ); 814 char * charastic_string( Companion o, int position ) { 815 return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) ); 764 816 } 765 817 printf( charactic_string ( Color, 1 ) ); … … 776 828 Similarly, the user can work with the enumeration type itself: (see section ref...) 777 829 \begin{lstlisting}[ label=lst:companion_user_definition] 778 void print_enumerators ( Companion o ) { 830 void print_enumerators ( Companion o ) { 779 831 for ( c : Companion o ) { 780 832 sout | label (c) | value( c ) ; 781 } 833 } 782 834 } 783 835 print_enumerators( Colour ); … … 795 847 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. 796 848 If the declared type is not @AutoInitializable@, \CFA rejects the enumeration definition. 797 Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section) 849 Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section) 798 850 799 851 \begin{lstlisting}[label=lst:init] … … 803 855 T ?+?( T & lhs, T & rhs ) { ... }; 804 856 805 enum (T) Sample { 806 Zero: 0 /* zero_t */, 857 enum (T) Sample { 858 Zero: 0 /* zero_t */, 807 859 One: Zero + 1 /* ?+?( Zero, one_t ) */ , ... 808 860 }; … … 826 878 \subsection{Qualified Expression} 827 879 828 \CFA uses qualified expression to address the scoping of \CFA-enumeration. 880 \CFA uses qualified expression to address the scoping of \CFA-enumeration. 829 881 \begin{lstlisting}[label=lst:qualified_expression] 830 882 aggregation_name.field; … … 837 889 838 890 \subsection{\lstinline{with} Clause/Statement} 891 839 892 Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible. 840 893 \begin{lstlisting}[label=lst:declaration] … … 842 895 enum Animal( int ) { Cat=10, Dog=20 }; 843 896 with ( Color, Animal ) { 844 845 897 char * red_string = Red; // value( Color.Red ) 898 int cat = Cat; // value( Animal.Cat ) 846 899 } 847 900 \end{lstlisting} … … 851 904 enum RGB( int ) { Red=0, Green=1, Blue=2 }; 852 905 with ( Color, RGB ) { 853 // int red = Red; 906 // int red = Red; 854 907 } 855 908 \end{lstlisting} … … 864 917 865 918 The declaration \CFA-enumeration variable has the same syntax as the C-enumeration. Internally, such a variable will be represented as an EnumInstType. 919 920 \section{Related Work} 866 921 867 922
Note: See TracChangeset
for help on using the changeset viewer.