Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/proposals/enum.tex

    r2989d6f r367725d  
    77\usepackage{graphics}
    88\usepackage{xspace}
    9 \usepackage{relsize}                                                                    % must be after change to small or selects old size
    10 \usepackage{calc}                                                                               % latex arithmetic
    119
    1210\makeatletter
     
    2422\newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
    2523\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 correctly
    34 \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}
    4124\makeatother
    4225
     
    6548\newcommand{\CCIcon}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}} % C++ icon
    6649\newcommand{\CC}[1][]{\protect\CCIcon{#1}\xspace}               % C++ symbolic name
    67 \newcommand{\Csharp}{C\raisebox{-0.7ex}{\relsize{2}$^\sharp$}\xspace} % C# symbolic name
    6850\newcommand{\PAB}[1]{{\color{red}PAB: #1}}
    6951
     
    7456
    7557\lstdefinestyle{CStyle}{
    76 %    backgroundcolor=\color{backgroundColour},
     58%    backgroundcolor=\color{backgroundColour},   
    7759%    commentstyle=\color{mGreen},
    7860%    keywordstyle=\color{magenta},
     
    8264    basicstyle=\small\linespread{0.9}\sf,       % reduce line spacing and use sanserif font
    8365%   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,                 
    8870        escapechar=\$,                                                  % LaTeX escape in CFA code
    89 %    numbers=left,
    90 %    numbersep=5pt,
     71%    numbers=left,                   
     72%    numbersep=5pt,                 
    9173%    numberstyle=\tiny\color{mGray},
    92 %    showspaces=false,
     74%    showspaces=false,               
    9375    showstringspaces=false,
    94 %    showtabs=false,
     76%    showtabs=false,                 
    9577        showlines=true,                                                 % show blank lines at end of code
    9678    tabsize=5,
     
    11193
    11294\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.
     95An enumeration is a type that defines a list of named constant values in C (and other languages).
     96C 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.
    11798\end{abstract}
    11899
    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 
    132100\section{C-Style Enum}
    133101
    134 The C-Style enumeration has the following syntax and semantics.
     102\CFA supports the C-Style enumeration using the same syntax and semantics.
    135103\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@.
     104enum Weekday { Monday, Tuesday, Wednesday, Thursday=10, Friday, Saturday, Sunday };
     105                $\(\uparrow\)$                                                                      $\(\uparrow\)$
     106    ${\rm \newterm{enumeration name}}$                                        ${\rm \newterm{enumerator names}}
     107\end{lstlisting}
     108The example defines an enumeration type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@.
    141109The 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.
     110A 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.
     111For 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.
    144112
    145113There are 3 attributes for an enumeration: \newterm{position}, \newterm{label}, and \newterm{value}:
     
    150118\it position            & 0                     & 1                     & 2                             & 3                             & 4                     & 5                     & 6                     \\
    151119\it label                       & Monday        & Tuesday       & Wednesday             & Thursday              & Friday        & Saturday      & Sunday        \\
    152 \it value                       & 0                     & 1                     & 2                             & {\color{red}10}& 11           & 12            & 13
     120\it value                       & 0                     & 1                     & 2                             & 10                    & 11            & 12            & 13
    153121\end{tabular}
    154122\end{cquote}
    155123
    156124The 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.
    158125\begin{lstlisting}[label=lst:enum_scope]
    159126{
    160         enum Weekday { ... };                           $\C{// enumerators implicitly projected into local scope}$
     127        enum Weekday { ... };   // enumerators implicitly projected into local scope
    161128        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
    165131}
    166 int j = Wednesday;                                              $\C{// ERROR! Wednesday is not declared in this scope}$
     132int j = Wednesday; // ERROR! Wednesday is not declared in this scope
    167133\end{lstlisting}
    168134
    169135\section{\CFA-Style Enum}
    170136
    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.
     137A \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.
    177139\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}).
     140enum Colour( @char *@ ) { Red = "R", Green = "G", Blue = "B"  };
     141\end{lstlisting}
     142The type of @Colour@ is @char *@ and each enumerator is initialized with a C string.
     143Only types with a defined ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}).
    185144
    186145% An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name.
     
    192151A \CFA-enum can be scoped, meaning the enumerator constants are not projected into the enclosing scope.
    193152\begin{lstlisting}
    194 enum Weekday @!@ { /* as above */ };
    195 enum Colour( char * ) @!@ { /* as above */ };
     153enum Colour( char * ) @!@ { ... };
    196154\end{lstlisting}
    197155where the @'!'@ implies the enumerators are \emph{not} projected.
     
    200158% $$<qualified\_expression> := <enum\_type>.<enumerator>$$
    201159\begin{lstlisting}
    202 Weekday weekday = @Weekday.Monday@;             $\C{// qualification}$
    203 Colour colour = @Colour.@Red;
     160Colour colour = @Colour.@Red;   // qualification
    204161colour = @Colour.@Blue;
    205162\end{lstlisting}
    206163
    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}
     165Pseudo-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}
     168The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@.
    214169\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 
     170int green_pos = @position@( Colour.Green );     // 1
     171char * green_value = @value@( Colour.Green ); / "G"
     172char * green_label = @label@( Colour.Green ); // "Green"
     173\end{lstlisting}
     174
     175\subsection{enumerate()}
    225176\begin{lstlisting}[label=lst:c_switch]
    226177enum(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.
     178int 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}
     187In 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.
    241190\begin{lstlisting}[label=lst:c_switch_enumerate]
    242191enum(double) C_ENUM { First, Second, Third = First, Fourth };
    243 C_ENUM variable_a = First, variable_b = Second, variable_c = Third, 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         };
     192C_ENUM variable_a = First, variable_b = Second, variable_c = Thrid, variable_d = Fourth;
     193int 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    };
    251200};
    252201p(variable_a); // 0
     
    256205\end{lstlisting}
    257206
    258 
    259207\section{Enumeration Storage}
    260 
    261208
    262209\subsection{Enumeration Variable}
     
    281228>>> label( Colour, 1) -> char *
    282229\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. 
    284231These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters.
    285232
    286 
    287233\subsection{Enumeration Data}
    288 
    289234\begin{lstlisting}[label=lst:enumeration_backing_data]
    290235enum(T) E { ... };
    291236// 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:
     237T* E_values;
     238char** E_labels;
     239\end{lstlisting}
     240Storing 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:
    297241\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.
    301244\end{itemize}
    302245
    303246
     247\
    304248\section{Unification}
    305249
    306250\subsection{Enumeration as Value}
    307251\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.
     252An \CFA enumeration with base type T can be used seamlessly as T, without explicitly calling the pseudo-function value. 
    309253\begin{lstlisting}[label=lst:implicit_conversion]
    310254char * green_value = Colour.Green; // "G"
    311 // Is equivalent to
     255// Is equivalent to 
    312256// char * green_value = value( Color.Green ); "G"
    313257\end{lstlisting}
    314258
    315 
    316259\subsection{Unification Distance}
    317 
    318260\begin{lstlisting}[label=lst:unification_distance_example]
    319261T_2 Foo(T1);
     
    323265@path(A, B)@ is a compiler concept that returns one of the following:
    324266\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.
    329271\end{itemize}
    330272
     
    336278The arithmetic of distance is the following:
    337279\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.
    342284\end{itemize}
    343285
     
    346288
    347289\subsection{Variable Overloading and Parameter Unification}
    348 
    349290\CFA allows variable names to be overloaded. It is possible to overload a variable that has type T and an enumeration with type T.
    350291\begin{lstlisting}[label=lst:variable_overload]
     
    363304Similarly, functions can be overloaded with different signatures. \CFA picks the correct function entity based on the distance between parameter types and the arguments.
    364305\begin{lstlisting}[label=lst:function_overload]
    365 Colour green = Colour.Green;
     306Colour green = Colour.Green; 
    366307void foo(Colour c) { sout | "It is an enum"; } // First foo
    367308void foo(char * s) { sout | "It is a string"; } // Second foo
     
    385326% The @EnumInstType@ is convertible to other types.
    386327% 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. 
    388329
    389330% \begin{lstlisting}[caption={Null Context}, label=lst:null_context]
     
    438379% }
    439380% \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. 
    441382% Backward conversion:
    442383% \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
     
    448389% \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
    449390% {
    450 %       Unification( EnumInstType<Colour>, int ) >>> label
     391%    Unification( EnumInstType<Colour>, int ) >>> label
    451392% }
    452393% \end{lstlisting}
    453394% @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 
    455396% \begin{lstlisting}
    456397% {
    457 %       enum Colour colour = Colour.Green;
     398%    enum Colour colour = Colour.Green;
    458399% }
    459400% \end{lstlisting}
     
    470411% {
    471412%       enum T (int) { ... } // Declaration
    472 %       enum T t = 1;
     413%       enum T t = 1; 
    473414% }
    474415% \end{lstlisting}
     
    482423% return the FIRST enumeration constant that has the value 1, by searching through the values array
    483424% \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 
    485426
    486427% \subsection{Casting}
     
    490431% (int) Foo.A;
    491432% \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. 
    493434
    494435% \subsection{Value Conversion}
     
    504445% int j = value( Foo, a )
    505446% \end{lstlisting}
    506 % Similarly, the generated code for the third line is
     447% Similarly, the generated code for the third line is 
    507448% \begin{lstlisting}
    508449% char * j = label( Foo, a )
     
    514455
    515456\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$.
     457A 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$. 
    517458
    518459\subsection{Auto Initializable}
     
    537478Odd ?++( Odd t1 ) { return Odd( t1.i + 2); };
    538479\end{lstlisting}
    539 When the type of an enumeration is @AutoInitializable@, implicit initialization is available.
     480When the type of an enumeration is @AutoInitializable@, implicit initialization is available. 
    540481\begin{lstlisting}[label=lst:sample_auto_Initializable_usage]
    541482enum AutoInitUsage(Odd) {
     
    573514@alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule.
    574515
    575 \textbullet\ \CFA offers a shorthand for iterating all enumeration constants:
     516\textbullet\ \CFA offers a shorthand for iterating all enumeration constants: 
    576517\begin{lstlisting}[label=lst:range_functions]
    577518for ( Alphabet alph ) { sout | alph; }
     
    626567>>> 10 11 12 13 14 15 16 17 18
    627568\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
     569The 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 
    629570\begin{lstlisting}[label=lst:range_function_stepping_converted]
    630571for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1  ) { sout | alph; }
     
    638579for ( char * alph; Alphabet )
    639580\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.
     581This 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.
    641582If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration.
    642583\begin{lstlisting}[label=lst:range_functions_label_implicit]
     
    650591% \begin{lstlisting}
    651592% enum T( int, char * ) {
    652 %        a=42, b="Hello World"
     593%    a=42, b="Hello World"
    653594% };
    654595% \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. 
    656597
    657598\subsection{Enumeration Inheritance}
     
    661602enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
    662603\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. 
    664605\begin{lstlisting}[label=lst:EnumInline]
    665606Name Fred;
     
    669610If enumeration A declares @inline B@ in its enumeration body, enumeration A is the "inlining enum" and enumeration B is the "inlined enum".
    670611
    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.
     612An 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. 
    672613\begin{lstlisting}[label=lst:EnumInline]
    673614enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
     
    684625\begin{lstlisting}[label=lst:static_attr]
    685626enum( 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}
     630An 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. 
    690631
    691632\subsection{Runtime Attribute Expression and Weak Referenced Data}
     
    697638An 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.
    698639
    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.
    700641
    701642\begin{lstlisting}[label=lst:attr_array]
     
    710651\end{lstlisting}
    711652
    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.
     653To 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. 
    713654
    714655\subsection{Enum Prelude}
     
    716657\begin{lstlisting}[label=lst:enum_func_dec]
    717658forall( 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 );
    721662}
    722663\end{lstlisting}
     
    729670forall(T)
    730671class EnumDecl {
    731         T* values;
    732         char** label;
     672    T* values;
     673    char** label;
    733674};
    734675\end{lstlisting}
     
    738679\begin{lstlisting}[label=lst:EnumInstType]
    739680class EnumInstType {
    740         EnumDecl enumDecl;
    741         int position;
     681    EnumDecl enumDecl;
     682    int position;
    742683};
    743684\end{lstlisting}
     
    759700% struct Companion {
    760701%       const T * const values;
    761 %                const char * label;
     702%        const char * label;
    762703%       int length;
    763704% };
     
    765706% \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@.
    766707
    767 % The companion object is singleton across the compilation (investigation).
     708% The companion object is singleton across the compilation (investigation). 
    768709
    769710% \CFA generates the definition of companion functions.
     
    786727\begin{lstlisting}[label=lst:companion_trait]
    787728forall(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    }
    793734}
    794735\end{lstlisting}
     
    802743\begin{lstlisting}
    803744enum(int) Weekday {
    804         Monday=10, Tuesday, ...
     745    Monday=10, Tuesday, ...
    805746};
    806747
     
    817758\subsection{User Define Enumeration Functions}
    818759
    819 Companion objects make extending features for \CFA enumeration easy.
     760Companion objects make extending features for \CFA enumeration easy. 
    820761\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) );
     762char * charastic_string( Companion o, int position ) { 
     763        return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) ); 
    823764}
    824765printf( charactic_string ( Color, 1 ) );
     
    835776Similarly, the user can work with the enumeration type itself: (see section ref...)
    836777\begin{lstlisting}[ label=lst:companion_user_definition]
    837 void print_enumerators ( Companion o ) {
     778void print_enumerators ( Companion o ) { 
    838779        for ( c : Companion o ) {
    839780                sout | label (c) | value( c ) ;
    840         }
     781        } 
    841782}
    842783print_enumerators( Colour );
     
    854795It 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.
    855796If 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)
     797Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section) 
    857798
    858799\begin{lstlisting}[label=lst:init]
     
    862803T ?+?( T & lhs, T & rhs ) { ... };
    863804
    864 enum (T) Sample {
    865         Zero: 0 /* zero_t */,
     805enum (T) Sample { 
     806        Zero: 0 /* zero_t */, 
    866807        One: Zero + 1 /* ?+?( Zero, one_t ) */ , ...
    867808};
     
    885826\subsection{Qualified Expression}
    886827
    887 \CFA uses qualified expression to address the scoping of \CFA-enumeration.
     828\CFA uses qualified expression to address the scoping of \CFA-enumeration. 
    888829\begin{lstlisting}[label=lst:qualified_expression]
    889830aggregation_name.field;
     
    896837
    897838\subsection{\lstinline{with} Clause/Statement}
    898 
    899839Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible.
    900840\begin{lstlisting}[label=lst:declaration]
     
    902842enum Animal( int ) { Cat=10, Dog=20 };
    903843with ( 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 )
    906846}
    907847\end{lstlisting}
     
    911851enum RGB( int ) { Red=0, Green=1, Blue=2 };
    912852with ( Color, RGB ) {
    913         // int red = Red;
     853    // int red = Red;
    914854}
    915855\end{lstlisting}
     
    925865The declaration \CFA-enumeration variable has the same syntax as the C-enumeration. Internally, such a variable will be represented as an EnumInstType.
    926866
    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}
    967867
    968868\end{document}
Note: See TracChangeset for help on using the changeset viewer.