Changeset dd1ebb1 for doc/proposals


Ignore:
Timestamp:
Jan 27, 2024, 11:26:17 PM (6 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
765ee42
Parents:
d1551a5
Message:

some updates on the enum proposal

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enum.tex

    rd1551a5 rdd1ebb1  
    77\usepackage{graphics}
    88\usepackage{xspace}
     9\usepackage{calc}                                                                               % latex arithmetic
    910
    1011\makeatletter
     
    2223\newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
    2324\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}
    2440\makeatother
    2541
     
    5672
    5773\lstdefinestyle{CStyle}{
    58 %    backgroundcolor=\color{backgroundColour},   
     74%    backgroundcolor=\color{backgroundColour},
    5975%    commentstyle=\color{mGreen},
    6076%    keywordstyle=\color{magenta},
     
    6480    basicstyle=\small\linespread{0.9}\sf,       % reduce line spacing and use sanserif font
    6581%   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,
    7086        escapechar=\$,                                                  % LaTeX escape in CFA code
    71 %    numbers=left,                   
    72 %    numbersep=5pt,                 
     87%    numbers=left,
     88%    numbersep=5pt,
    7389%    numberstyle=\tiny\color{mGray},
    74 %    showspaces=false,               
     90%    showspaces=false,
    7591    showstringspaces=false,
    76 %    showtabs=false,                 
     92%    showtabs=false,
    7793        showlines=true,                                                 % show blank lines at end of code
    7894    tabsize=5,
     
    93109
    94110\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.
     111An enumeration is a type defining an ordered set of named constant values, where a name abstracts a value, e.g., @PI@ versus @3.145159@.
     112C 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.
     114Furthermore, \CFA adds other useful features for enumerations to support better software-engineering practices and simplify program development.
    98115\end{abstract}
    99116
     117\section{Background}
     118
     119Naming values is a common practice in mathematics and engineering, e.g., $\pi$, $\tau$ (2$\pi$), $\phi$ (golden ratio), MHz (1E6), etc.
     120Naming 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).
     121Many programming languages capture this important capability through a mechanism called an \newterm{enumeration}.
     122An 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.
     123Note, all enumeration names must be unique but different names can represent the same value (eight note, quaver), which are synonyms.
     124
     125Specifically, an enumerated type is a type whose values are restricted to a fixed set of named constants.
     126Fundamentally, 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.
     127However, the values for basic types are not named, other than the programming-language supplied constants.
     128
     129
    100130\section{C-Style Enum}
    101131
    102 \CFA supports the C-Style enumeration using the same syntax and semantics.
     132The C-Style enumeration has the following syntax and semantics.
    103133\begin{lstlisting}[label=lst:weekday]
    104 enum Weekday { Monday, Tuesday, Wednesday, Thursday=10, Friday, Saturday, Sunday };
     134enum Weekday { Monday, Tuesday, Wednesday, Thursday@=10@, Friday, Saturday, Sunday };
    105135                $\(\uparrow\)$                                                                      $\(\uparrow\)$
    106136    ${\rm \newterm{enumeration name}}$                                        ${\rm \newterm{enumerator names}}
    107137\end{lstlisting}
    108 The example defines an enumeration type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@.
     138Here, the enumeration type @Weekday@ defines the ordered \newterm{enumerator}s @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@.
    109139The 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.
     140A 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.
     141For 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.
    112142
    113143There are 3 attributes for an enumeration: \newterm{position}, \newterm{label}, and \newterm{value}:
     
    118148\it position            & 0                     & 1                     & 2                             & 3                             & 4                     & 5                     & 6                     \\
    119149\it label                       & Monday        & Tuesday       & Wednesday             & Thursday              & Friday        & Saturday      & Sunday        \\
    120 \it value                       & 0                     & 1                     & 2                             & 10                    & 11            & 12            & 13
     150\it value                       & 0                     & 1                     & 2                             & {\color{red}10}& 11           & 12            & 13
    121151\end{tabular}
    122152\end{cquote}
    123153
    124154The enumerators of an enumeration are unscoped, i.e., enumerators declared inside of an @enum@ are visible in the enclosing scope of the @enum@ type.
     155Furthermore, there is an implicit bidirectional conversion between an enumeration and integral types.
    125156\begin{lstlisting}[label=lst:enum_scope]
    126157{
    127         enum Weekday { ... };   // enumerators implicitly projected into local scope
     158        enum Weekday { ... };                           $\C{// enumerators implicitly projected into local scope}$
    128159        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}$
    131163}
    132 int j = Wednesday; // ERROR! Wednesday is not declared in this scope
     164int j = Wednesday;                                              $\C{// ERROR! Wednesday is not declared in this scope}$
    133165\end{lstlisting}
    134166
    135167\section{\CFA-Style Enum}
    136168
    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.
    139175\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}).
     176enum( @char@ ) Currency { Dollar = '$\textdollar$', Euro = '$\texteuro$', Pound = '$\textsterling$'  };
     177enum( @double@ ) Planet { Venus = 4.87, Earth = 5.97, Mars = 0.642  }; // mass
     178enum( @char *@ ) Colour { Red = "red", Green = "green", Blue = "blue"  };
     179enum( @Currency@ ) Europe { Euro = '$\texteuro$', Pound = '$\textsterling$' }; // intersection
     180\end{lstlisting}
     181The 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}).
    144183
    145184% An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name.
     
    158197% $$<qualified\_expression> := <enum\_type>.<enumerator>$$
    159198\begin{lstlisting}
    160 Colour colour = @Colour.@Red;   // qualification
     199Colour colour = @Colour.@Red;                   $\C{// qualification}$
    161200colour = @Colour.@Blue;
    162201\end{lstlisting}
    163202
    164 \section{Enumeration Pseudo-functions}
     203\subsection{Enumeration Pseudo-functions}
    165204Pseudo-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.
    166205
    167 \subsection{Enumerator Attributes}
    168 The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@. 
     206\subsubsection{Enumerator Attributes}
     207The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@.
    169208\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()}
     209int green_pos = @position@( Colour.Green );     $\C{// 1}$
     210char * green_value = @value@( Colour.Green ); $\C{// "G"}$
     211char * green_label = @label@( Colour.Green ); $\C{// "Green"}$
     212\end{lstlisting}
     213
     214Enumeration 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
    176218\begin{lstlisting}[label=lst:c_switch]
    177219enum(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.
     220int 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}
     229In the @C_ENUM@ example, @Third@ is an alias of @First@ and @Fourth@ is an alias of @Second@.
     230Programmers cannot make case branches for @Third@ and @Fourth@ because the switch statement matches cases by the enumerator's value.
     231Case @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.
    190234\begin{lstlisting}[label=lst:c_switch_enumerate]
    191235enum(double) C_ENUM { First, Second, Third = First, Fourth };
    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     };
     236C_ENUM variable_a = First, variable_b = Second, variable_c = Third, variable_d = Fourth;
     237int 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        };
    200244};
    201245p(variable_a); // 0
     
    205249\end{lstlisting}
    206250
     251
    207252\section{Enumeration Storage}
     253
    208254
    209255\subsection{Enumeration Variable}
     
    228274>>> label( Colour, 1) -> char *
    229275\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.
    231277These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters.
    232278
     279
    233280\subsection{Enumeration Data}
     281
    234282\begin{lstlisting}[label=lst:enumeration_backing_data]
    235283enum(T) E { ... };
    236284// 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:
     285T * E_values;
     286char ** E_labels;
     287\end{lstlisting}
     288Storing values and labels as arrays can sometimes help support enumeration features.
     289However, the data structures are the overhead for the programs. We want to reduce the memory usage for enumeration support by:
    241290\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.
    244294\end{itemize}
    245295
    246296
    247 \
    248297\section{Unification}
    249298
    250299\subsection{Enumeration as Value}
    251300\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. 
     301An \CFA enumeration with base type T can be used seamlessly as T, without explicitly calling the pseudo-function value.
    253302\begin{lstlisting}[label=lst:implicit_conversion]
    254303char * green_value = Colour.Green; // "G"
    255 // Is equivalent to 
     304// Is equivalent to
    256305// char * green_value = value( Color.Green ); "G"
    257306\end{lstlisting}
    258307
     308
    259309\subsection{Unification Distance}
     310
    260311\begin{lstlisting}[label=lst:unification_distance_example]
    261312T_2 Foo(T1);
     
    265316@path(A, B)@ is a compiler concept that returns one of the following:
    266317\begin{itemize}
    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.
     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.
    271322\end{itemize}
    272323
     
    278329The arithmetic of distance is the following:
    279330\begin{itemize}
    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.
     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.
    284335\end{itemize}
    285336
     
    288339
    289340\subsection{Variable Overloading and Parameter Unification}
     341
    290342\CFA allows variable names to be overloaded. It is possible to overload a variable that has type T and an enumeration with type T.
    291343\begin{lstlisting}[label=lst:variable_overload]
     
    304356Similarly, functions can be overloaded with different signatures. \CFA picks the correct function entity based on the distance between parameter types and the arguments.
    305357\begin{lstlisting}[label=lst:function_overload]
    306 Colour green = Colour.Green; 
     358Colour green = Colour.Green;
    307359void foo(Colour c) { sout | "It is an enum"; } // First foo
    308360void foo(char * s) { sout | "It is a string"; } // Second foo
     
    326378% The @EnumInstType@ is convertible to other types.
    327379% 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.
    329381
    330382% \begin{lstlisting}[caption={Null Context}, label=lst:null_context]
     
    379431% }
    380432% \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.
    382434% Backward conversion:
    383435% \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
     
    389441% \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
    390442% {
    391 %    Unification( EnumInstType<Colour>, int ) >>> label
     443%       Unification( EnumInstType<Colour>, int ) >>> label
    392444% }
    393445% \end{lstlisting}
    394446% @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
    396448% \begin{lstlisting}
    397449% {
    398 %    enum Colour colour = Colour.Green;
     450%       enum Colour colour = Colour.Green;
    399451% }
    400452% \end{lstlisting}
     
    411463% {
    412464%       enum T (int) { ... } // Declaration
    413 %       enum T t = 1; 
     465%       enum T t = 1;
    414466% }
    415467% \end{lstlisting}
     
    423475% return the FIRST enumeration constant that has the value 1, by searching through the values array
    424476% \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
    426478
    427479% \subsection{Casting}
     
    431483% (int) Foo.A;
    432484% \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.
    434486
    435487% \subsection{Value Conversion}
     
    445497% int j = value( Foo, a )
    446498% \end{lstlisting}
    447 % Similarly, the generated code for the third line is 
     499% Similarly, the generated code for the third line is
    448500% \begin{lstlisting}
    449501% char * j = label( Foo, a )
     
    455507
    456508\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$. 
     509A 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$.
    458510
    459511\subsection{Auto Initializable}
     
    478530Odd ?++( Odd t1 ) { return Odd( t1.i + 2); };
    479531\end{lstlisting}
    480 When the type of an enumeration is @AutoInitializable@, implicit initialization is available. 
     532When the type of an enumeration is @AutoInitializable@, implicit initialization is available.
    481533\begin{lstlisting}[label=lst:sample_auto_Initializable_usage]
    482534enum AutoInitUsage(Odd) {
     
    514566@alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule.
    515567
    516 \textbullet\ \CFA offers a shorthand for iterating all enumeration constants: 
     568\textbullet\ \CFA offers a shorthand for iterating all enumeration constants:
    517569\begin{lstlisting}[label=lst:range_functions]
    518570for ( Alphabet alph ) { sout | alph; }
     
    567619>>> 10 11 12 13 14 15 16 17 18
    568620\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 
     621The 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
    570622\begin{lstlisting}[label=lst:range_function_stepping_converted]
    571623for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1  ) { sout | alph; }
     
    579631for ( char * alph; Alphabet )
    580632\end{lstlisting}
    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.
     633This 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.
    582634If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration.
    583635\begin{lstlisting}[label=lst:range_functions_label_implicit]
     
    591643% \begin{lstlisting}
    592644% enum T( int, char * ) {
    593 %    a=42, b="Hello World"
     645%        a=42, b="Hello World"
    594646% };
    595647% \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.
    597649
    598650\subsection{Enumeration Inheritance}
     
    602654enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
    603655\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.
    605657\begin{lstlisting}[label=lst:EnumInline]
    606658Name Fred;
     
    610662If enumeration A declares @inline B@ in its enumeration body, enumeration A is the "inlining enum" and enumeration B is the "inlined enum".
    611663
    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. 
     664An 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.
    613665\begin{lstlisting}[label=lst:EnumInline]
    614666enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
     
    625677\begin{lstlisting}[label=lst:static_attr]
    626678enum( 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}
     682An 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.
    631683
    632684\subsection{Runtime Attribute Expression and Weak Referenced Data}
     
    638690An 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.
    639691
    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.
    641693
    642694\begin{lstlisting}[label=lst:attr_array]
     
    651703\end{lstlisting}
    652704
    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. 
     705To 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.
    654706
    655707\subsection{Enum Prelude}
     
    657709\begin{lstlisting}[label=lst:enum_func_dec]
    658710forall( T ) {
    659     unsigned position( unsigned );
    660     T value( unsigned );
    661     char * label( unsigned );
     711        unsigned position( unsigned );
     712        T value( unsigned );
     713        char * label( unsigned );
    662714}
    663715\end{lstlisting}
     
    670722forall(T)
    671723class EnumDecl {
    672     T* values;
    673     char** label;
     724        T* values;
     725        char** label;
    674726};
    675727\end{lstlisting}
     
    679731\begin{lstlisting}[label=lst:EnumInstType]
    680732class EnumInstType {
    681     EnumDecl enumDecl;
    682     int position;
     733        EnumDecl enumDecl;
     734        int position;
    683735};
    684736\end{lstlisting}
     
    700752% struct Companion {
    701753%       const T * const values;
    702 %        const char * label;
     754%                const char * label;
    703755%       int length;
    704756% };
     
    706758% \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@.
    707759
    708 % The companion object is singleton across the compilation (investigation). 
     760% The companion object is singleton across the compilation (investigation).
    709761
    710762% \CFA generates the definition of companion functions.
     
    727779\begin{lstlisting}[label=lst:companion_trait]
    728780forall(T1) {
    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     }
     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        }
    734786}
    735787\end{lstlisting}
     
    743795\begin{lstlisting}
    744796enum(int) Weekday {
    745     Monday=10, Tuesday, ...
     797        Monday=10, Tuesday, ...
    746798};
    747799
     
    758810\subsection{User Define Enumeration Functions}
    759811
    760 Companion objects make extending features for \CFA enumeration easy. 
     812Companion objects make extending features for \CFA enumeration easy.
    761813\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) ); 
     814char * charastic_string( Companion o, int position ) {
     815        return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );
    764816}
    765817printf( charactic_string ( Color, 1 ) );
     
    776828Similarly, the user can work with the enumeration type itself: (see section ref...)
    777829\begin{lstlisting}[ label=lst:companion_user_definition]
    778 void print_enumerators ( Companion o ) { 
     830void print_enumerators ( Companion o ) {
    779831        for ( c : Companion o ) {
    780832                sout | label (c) | value( c ) ;
    781         } 
     833        }
    782834}
    783835print_enumerators( Colour );
     
    795847It 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.
    796848If 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) 
     849Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section)
    798850
    799851\begin{lstlisting}[label=lst:init]
     
    803855T ?+?( T & lhs, T & rhs ) { ... };
    804856
    805 enum (T) Sample { 
    806         Zero: 0 /* zero_t */, 
     857enum (T) Sample {
     858        Zero: 0 /* zero_t */,
    807859        One: Zero + 1 /* ?+?( Zero, one_t ) */ , ...
    808860};
     
    826878\subsection{Qualified Expression}
    827879
    828 \CFA uses qualified expression to address the scoping of \CFA-enumeration. 
     880\CFA uses qualified expression to address the scoping of \CFA-enumeration.
    829881\begin{lstlisting}[label=lst:qualified_expression]
    830882aggregation_name.field;
     
    837889
    838890\subsection{\lstinline{with} Clause/Statement}
     891
    839892Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible.
    840893\begin{lstlisting}[label=lst:declaration]
     
    842895enum Animal( int ) { Cat=10, Dog=20 };
    843896with ( Color, Animal ) {
    844     char * red_string = Red; // value( Color.Red )
    845     int cat = Cat; // value( Animal.Cat )
     897        char * red_string = Red; // value( Color.Red )
     898        int cat = Cat; // value( Animal.Cat )
    846899}
    847900\end{lstlisting}
     
    851904enum RGB( int ) { Red=0, Green=1, Blue=2 };
    852905with ( Color, RGB ) {
    853     // int red = Red;
     906        // int red = Red;
    854907}
    855908\end{lstlisting}
     
    864917
    865918The 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}
    866921
    867922
Note: See TracChangeset for help on using the changeset viewer.