source: doc/proposals/enum.tex @ 66d92e3

Last change on this file since 66d92e3 was 66d92e3, checked in by Peter A. Buhr <pabuhr@…>, 6 months ago

proofread enumeration proposal

  • Property mode set to 100644
File size: 28.3 KB
Line 
1\documentclass[12pt]{article}
2\usepackage{fullpage,times}
3\usepackage{pslatex}                    % reduce size of san serif font
4\usepackage{xcolor}
5\usepackage{listings}
6%\usepackage{array}
7\usepackage{graphics}
8\usepackage{xspace}
9
10\makeatletter
11\renewcommand\section{\@startsection{section}{1}{\z@}{-3.0ex \@plus -1ex \@minus -.2ex}{1.5ex \@plus .2ex}{\normalfont\large\bfseries}}
12\renewcommand\subsection{\@startsection{subsection}{2}{\z@}{-2.75ex \@plus -1ex \@minus -.2ex}{1.25ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
13\renewcommand\subsubsection{\@startsection{subsubsection}{3}{\z@}{-2.5ex \@plus -1ex \@minus -.2ex}{1.0ex \@plus .2ex}{\normalfont\normalsize\bfseries}}
14\renewcommand\paragraph{\@startsection{paragraph}{4}{\z@}{-2.0ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries}}
15\renewcommand\subparagraph{\@startsection{subparagraph}{4}{\z@}{-1.5ex \@plus -1ex \@minus -.2ex}{-1em}{\normalfont\normalsize\bfseries\itshape}}
16
17% Denote newterms in particular font and index them without particular font and in lowercase, e.g., \newterm{abc}.
18% The option parameter provides an index term different from the new term, e.g., \newterm[\texttt{abc}]{abc}
19% The star version does not lowercase the index information, e.g., \newterm*{IBM}.
20\newcommand{\newtermFontInline}{\emph}
21\newcommand{\newterm}{\protect\@ifstar\@snewterm\@newterm}
22\newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
23\newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi}
24\makeatother
25
26\usepackage[ignoredisplayed]{enumitem}  % do not affect trivlist
27\setlist{labelsep=1ex}% global
28\setlist[itemize]{topsep=0.5ex,parsep=0.25ex,itemsep=0.25ex,listparindent=\parindent,leftmargin=\parindent}% global
29\setlist[itemize,1]{label=\textbullet}% local
30%\renewcommand{\labelitemi}{{\raisebox{0.25ex}{\footnotesize$\bullet$}}}
31\setlist[enumerate]{topsep=0.5ex,parsep=0.25ex,itemsep=0.25ex,listparindent=\parindent}% global
32\setlist[enumerate,2]{leftmargin=\parindent,labelsep=*,align=parleft,label=\alph*.}% local
33\setlist[description]{topsep=0.5ex,itemsep=0pt,listparindent=\parindent,leftmargin=\parindent,labelsep=1.5ex}
34
35\newenvironment{cquote}{%
36        \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindent\rightmargin\leftmargin}%
37        \item\relax
38}{%
39        \endlist
40}% cquote
41
42\setlength{\topmargin}{-0.45in}                                                 % move running title into header
43\setlength{\headsep}{0.25in}
44\setlength{\textheight}{9.0in}
45
46\newcommand{\CFAIcon}{\textsf{C\raisebox{\depth}{\rotatebox{180}A}}} % Cforall icon
47\newcommand{\CFA}{\protect\CFAIcon\xspace}                              % CFA symbolic name
48\newcommand{\CCIcon}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}} % C++ icon
49\newcommand{\CC}[1][]{\protect\CCIcon{#1}\xspace}               % C++ symbolic name
50\newcommand{\PAB}[1]{{\color{red}PAB: #1}}
51
52% \definecolor{mGreen}{rgb}{0,0.6,0}
53% \definecolor{mGray}{rgb}{0.5,0.5,0.5}
54% \definecolor{mPurple}{rgb}{0.58,0,0.82}
55% \definecolor{backgroundColour}{rgb}{0.95,0.95,0.92}
56
57\lstdefinestyle{CStyle}{
58%    backgroundcolor=\color{backgroundColour},   
59%    commentstyle=\color{mGreen},
60%    keywordstyle=\color{magenta},
61        stringstyle=\small\tt,                                  % use typewriter font
62%    stringstyle=\color{mPurple},
63    columns=fullflexible,
64    basicstyle=\small\linespread{0.9}\sf,       % reduce line spacing and use sanserif font
65%   basicstyle=\footnotesize,
66    breakatwhitespace=false,         
67%    breaklines=true,                 
68    captionpos=b,                   
69    keepspaces=true,                 
70        escapechar=\$,                                                  % LaTeX escape in CFA code
71%    numbers=left,                   
72%    numbersep=5pt,                 
73%    numberstyle=\tiny\color{mGray},
74%    showspaces=false,               
75    showstringspaces=false,
76%    showtabs=false,                 
77        showlines=true,                                                 % show blank lines at end of code
78    tabsize=5,
79    language=C,
80        aboveskip=4pt,                                                  % spacing above/below code block
81        belowskip=2pt,
82        xleftmargin=\parindent,                 % indent code to paragraph indentation
83}
84\lstset{style=CStyle,moredelim=**[is][\color{red}]{@}{@}}
85\lstMakeShortInline@                            % single-character for \lstinline
86
87\begin{document}
88
89\title{\vspace*{-0.5in}Enumeration in \CFA}
90\author{Jiada Liang}
91
92\maketitle
93
94\begin{abstract}
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.
98\end{abstract}
99
100\section{C-Style Enum}
101
102\CFA supports the C-Style enumeration using the same syntax and semantics.
103\begin{lstlisting}[label=lst:weekday]
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@.
109The successor of @Tuesday@ is @Monday@ and the predecessor of @Tuesday@ is @Wednesday@.
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.
112
113There are 3 attributes for an enumeration: \newterm{position}, \newterm{label}, and \newterm{value}:
114\begin{cquote}
115\small\sf\setlength{\tabcolsep}{3pt}
116\begin{tabular}{rccccccccccc}
117@enum@ Weekday \{       & Monday,       & Tuesday,      & Wednesday,    & Thursday=10,  & Friday,       & Saturday,     & Sunday \}; \\
118\it position            & 0                     & 1                     & 2                             & 3                             & 4                     & 5                     & 6                     \\
119\it label                       & Monday        & Tuesday       & Wednesday             & Thursday              & Friday        & Saturday      & Sunday        \\
120\it value                       & 0                     & 1                     & 2                             & 10                    & 11            & 12            & 13
121\end{tabular}
122\end{cquote}
123
124The enumerators of an enumeration are unscoped, i.e., enumerators declared inside of an @enum@ are visible in the enclosing scope of the @enum@ type.
125\begin{lstlisting}[label=lst:enum_scope]
126{
127        enum Weekday { ... };   // enumerators implicitly projected into local scope
128        Weekday weekday = Monday;
129        weekday = Friday;
130        int i = Sunday  // i == 13
131}
132int j = Wednesday; // ERROR! Wednesday is not declared in this scope
133\end{lstlisting}
134
135\section{\CFA-Style Enum}
136
137A \CFA enumeration is parameterized by a type, which specifies the type for each enumerator.
138\CFA allows any object type for the enumerators, and values assigned to enumerators must be from the declared type.
139\begin{lstlisting}[label=lst:color]
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}).
144
145% An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name.
146% The label can be retrieved by calling the function @label( <enum_instance> )@.
147% Similarly, the @value()@ function returns the value used to initialize the \CFA-enum.
148
149\subsection{Enumerator Scoping}
150
151A \CFA-enum can be scoped, meaning the enumerator constants are not projected into the enclosing scope.
152\begin{lstlisting}
153enum Colour( char * ) @!@ { ... };
154\end{lstlisting}
155where the @'!'@ implies the enumerators are \emph{not} projected.
156The enumerators of a scoped enumeration are accessed using qualification, like the fields of an aggregate.
157% The syntax of $qualified\_expression$ for \CFA-enum is the following:
158% $$<qualified\_expression> := <enum\_type>.<enumerator>$$
159\begin{lstlisting}
160Colour colour = @Colour.@Red;   // qualification
161colour = @Colour.@Blue;
162\end{lstlisting}
163
164\subsection{Enumerator Attributes}
165
166The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@, i.e., like @sizeof@.
167\begin{lstlisting}
168int green_pos = @position@( Colour.Green );     // 1
169char * green_value = @value@( Colour.Green );   // "G"
170char * green_label = @label@( Colour.Green );   // "Green"
171\end{lstlisting}
172There are implicit conversions from an enumerator to its attributes.
173\begin{lstlisting}[label=lst:enum_inst_assign_int]
174int green_pos = Colour.Green;  // 1
175char * green_value = Colour.Green;  // ambiguous
176char * green_label = Colour.Green;  // ambiguous
177\end{lstlisting}
178where a conversion is ambiguous, if the enumerator's type is same as an attribute's type.
179For example, @value( Colour.Green )@ and @label( Colour.Green )@ both have type @char *@.
180Further examples are:
181\begin{cquote}
182\begin{tabular}{ll}
183\begin{lstlisting}
184int monday_pos = Monday;  // ambiguous
185int monday_value = Monday;  // ambiguous
186char * monday_label = Monday;  // "Monday"
187
188\end{lstlisting}
189&
190\begin{lstlisting}
191enum(double) Math { PI = 3.14159, E = 2.718 };
192int pi_pos = PI;  // 0
193double pi_value = PI;  // 3.14159
194char * pi_label = PI;  // "PI"
195\end{lstlisting}
196\end{tabular}
197\end{cquote}
198Here, @position( Monday )@ and @value( Monday )@ both have type @int@, while all attribute types are unique for enumerator @PI@.
199
200When a resolution is ambiguous, a \textit{resolution precedence} applies: $$value > position > label$$
201\CFA uses resolution distance to describe if one type can be used as another. While \CFA calculates the resolution distance between the expected type and types of all three attributes, it would not choose the attribute with the closest distance. Instead, when resolving an enumeration constant, \CFA always chooses value whenever it is a possible resolution (resolution distance is not infinite), followed by position, then label.
202\begin{lstlisting}[label=lst:enum_inst_precedence]
203enum(double) Foo { Bar };
204int tee = Foo.Bar; // value( Bar );
205\end{lstlisting}
206In the example~\ref{lst:enum_inst_precedence}, while $position( Bar )$ has the closest resolution among the three attributes, $Foo.Bar$ is resolved as $value( Bar )$ because of the resolution precedence.
207
208\PAB{Not sure this is going to work.}
209
210\subsection{Enumerator Storage}
211
212Although \CFA enumeration captures three different attributes, an enumeration instance does not store all this information.
213The @sizeof@ a \CFA enumeration instance is always 4 bytes, the same size as a C enumeration instance (@sizeof( int )@).
214It comes from the fact that:
215\begin{enumerate}
216\item
217a \CFA enumeration is always statically typed;
218\item
219it is always resolved as one of its attributes in terms of real usage.
220\end{enumerate}
221When creating an enumeration instance @colour@ and assigning it with the enumerator @Color.Green@, the compiler allocates an integer variable and stores the position 1.
222The invocations of $positions()$, $value()$, and $label()$ turn into calls to special functions defined in the prelude:
223\begin{lstlisting}[label=lst:companion_call]
224position( green );
225>>> position( Colour, 1 ) -> int
226value( green );
227>>> value( Colour, 1 ) -> T
228label( green );
229>>> label( Colour, 1) -> char *
230\end{lstlisting}
231@T@ represents the type declared in the \CFA enumeration defined and @char *@ in the example.
232These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters.
233
234\subsection{Companion Object and Companion Function}
235
236\begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_functions]
237forall( T )
238struct Companion {
239        const T * const values;
240        const char ** const labels;
241        int length;
242};
243\end{lstlisting}
244\CFA creates a @Companion@ object for every \CFA enumeration.
245A companion object has the same name as the enumeration is defined for.
246A companion object stores values and labels of enumeration constants, in the order of the constants defined in the enumeration.
247
248\CFA generates the definition of companion functions.
249Because \CFA implicitly stores enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed.
250Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively.
251\begin{lstlisting}[label=lst:companion_definition]
252int position( Companion o, int pos ) { return pos; }
253T value( Companion o, int pos ) { return o.values[ pos ]; }
254char * label( Companion o, int pos ) { return o.labels[ pos ]; }
255\end{lstlisting}
256Notably, the @Companion@ structure definition, and all companion objects, are visible to users.
257A user can retrieve values and labels defined in an enumeration by accessing the values and labels directly, or indirectly by calling @Companion@ functions @values@ and @labels@
258\begin{lstlisting}[label=lst:companion_definition_values_labels]
259Colour.values; // read the Companion's values
260values( Colour ); // same as Colour.values
261\end{lstlisting}
262
263\subsection{User Define Enumeration Functions}
264
265Companion objects make extending features for \CFA enumeration easy.
266\begin{lstlisting}[label=lst:companion_user_definition]
267char * charastic_string( Companion o, int position ) { 
268        return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );
269}
270printf( charactic_string ( Color, 1 ) );
271>>> Label: Green; Value: G
272\end{lstlisting}
273Defining a function takes a Companion object effectively defines functions for all \CFA enumeration.
274
275The \CFA compiler turns a function call that takes an enumeration instance as a parameter into a function call with a companion object plus a position.
276Therefore, a user can use the syntax with a user-defined enumeration function call:
277\begin{lstlisting}[label=lst:companion_user_definition]
278charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 )
279>>> Label: Green; Value: G
280\end{lstlisting}
281Similarly, the user can work with the enumeration type itself: (see section ref...)
282\begin{lstlisting}[ label=lst:companion_user_definition]
283void print_enumerators ( Companion o ) { 
284        for ( c : Companion o ) {
285                sout | label (c) | value( c ) ;
286        } 
287}
288print_enumerators( Colour );
289\end{lstlisting}
290
291\subsection{Runtime Enumeration}
292
293The companion structure definition is visible to users, and users can create an instance of companion object themselves, which effectively constructs a \textit{Runtime Enumeration}.
294\begin{lstlisting}[ label=lst:runtime_enum ]
295const char values[$\,$] = { "Hello", "World" };
296const char labels[$\,$] = { "First", "Second" };
297Companion(char *) MyEnum = { .values: values, .labels: labels, .length: 2 };
298\end{lstlisting}
299A runtime enumeration can be used to call enumeration functions.
300\begin{lstlisting}[ label=lst:runtime_enum_usage ]
301sout | charatstic_string( MyEnum, 1 );
302>>> Label: Second; Value: World
303\end{lstlisting}
304However, a runtime enumeration cannot create an enumeration instance, and it does not support enum-qualified syntax.
305\begin{lstlisting}[ label=lst:runtime_enum_usage ]
306MyEnum e = MyEnum.First; // Does not work: cannot create an enumeration instance e,
307                                    // and MyEnum.First is not recognizable
308\end{lstlisting}
309During the compilation, \CFA adds enumeration declarations to an enumeration symbol table and creates specialized function definitions for \CFA enumeration.
310\CFA does not recognize runtime enumeration during compilation and would not add them to the enumeration symbol table, resulting in a lack of supports for runtime enumeration.
311
312\PAB{Not sure how useful this feature is.}
313
314\section{Enumeration Features}
315
316A trait is a collection of constraints in \CFA that can be used to describe types.
317The \CFA standard library defines traits to categorize types with related enumeration features.
318
319\subsection{Auto Initializable}
320\label{s:AutoInitializable}
321
322TODO: make the initialization rule a separate section.
323
324If no explicit initializer is given to an enumeration constant, C initializes the first enumeration constant with value 0, and the next enumeration constant has a value equal to its $predecessor + 1$.
325\CFA enumerations have the same rule in enumeration constant initialization.
326However, only \CFA types that have defined traits for @zero_t@, @one_t@, and an addition operator can be automatically initialized by \CFA.
327
328Specifically, a type is auto-initializable only if it satisfies the trait @AutoInitializable@:
329\begin{lstlisting}
330forall(T)
331trait AutoInitializable {
332        void ?()( T & t, zero_t );
333        void ?()( T & t, one_t );
334        S ?+?( T & t, one_t );
335};
336\end{lstlisting}
337An example of a user-defined @AutoInitializable@ is:
338\begin{lstlisting}[label=lst:sample_auto_Initializable]
339struct Odd { int i; };
340void ?()( Odd & t, zero_t ) { t.i = 1; };
341void ?()( Odd & t, one_t ) { t.i = 2; };
342Odd ?+?( Odd t1, Odd t2 ) { return Odd( t1.i + t2.i); };
343\end{lstlisting}
344When the type of an enumeration is @AutoInitializable@, implicit initialization is available.
345\begin{lstlisting}[label=lst:sample_auto_Initializable_usage]
346enum AutoInitUsage(Odd) {
347        A, B, C = 7, D
348};
349\end{lstlisting}
350In the example, there is no initializer specified for the first enumeration constant @A@, so \CFA initializes it with the value of @zero_t@, which is 1.
351@B@ and @D@ have the values of their $predecessor + one_t$, where @one_t@ has the value 2.
352Therefore, the enumeration is initialized as follows:
353\begin{lstlisting}[label=lst:sample_auto_Initializable_usage_gen]
354enum AutoInitUsage(Odd) {
355        A = 1, B = 3, C = 7, D = 9
356};
357\end{lstlisting}
358Note, there is no mechanism to prevent an even value for the direct initialization, such as @C = 6@.
359
360In \CFA, character, integral, float, and imaginary types are all @AutoInitialiable@.
361\begin{lstlisting}[label=lst:letter]
362enum Alphabet( int ) {
363        A = 'A', B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
364        a = 'a', b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z
365};
366print( "%c, %c, %c", Alphabet.F, Alphabet.o, Alphabet.z );
367>>> F, o, z
368\end{lstlisting}
369
370\subsection{Iteration and Range}
371
372It is convenient to iterate over a \CFA enumeration, e.g.:
373\begin{lstlisting}[label=lst:range_functions]
374for ( Alphabet alph; Alphabet ) {
375        printf( "%d ", alph );
376}
377>>> A B C ...
378\end{lstlisting}
379The for-loop uses the enumeration type @Alphabet@ its range, and iterates through all enumerators in the order defined in the enumeration.
380@alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule.
381
382\CFA offers a shorthand for iterating all enumeration constants:
383\begin{lstlisting}[label=lst:range_functions]
384for ( Alphabet alph ) {
385        printf( "%d ", alph );
386}
387>>> A B C ...
388\end{lstlisting}
389The following different loop-control syntax is supported:
390\begin{lstlisting}[label=lst:range_functions]
391for ( Alphabet.D )
392for ( alph; Alphabet.g ~ Alphabet.z )
393for ( Alphabet alph; Alphabet.R ~ Alphabet.X ~ 2 )
394\end{lstlisting}
395\PAB{Explain what each loop does.}
396Notably, the meaning of ``step'' for an iteration has changed for enumeration.
397Consider the following example:
398\begin{lstlisting}[label=lst:range_functions]
399enum(int) Sequence {
400        A = 10, B = 12, C = 14; 
401}
402for ( s; Sequence.A ~ Sequence.C ) {
403        printf( "%d ", s );
404}
405>>> 10 12 14
406
407for ( s; Sequence.A ~ Sequence.A ~ 2 ) {
408        printf( "%d ", s );
409}
410>>> 10 14
411\end{lstlisting}
412The range iteration of enumeration does not return the @current_value++@ until it reaches the upper bound.
413The semantics is to return the next enumeration constant.
414If a stepping is specified, 2 for example, it returns the 2 enumeration constant after the current one, rather than the @current+2@.
415
416It is also possible to iterate over an enumeration's labels, implicitly or explicitly:
417\begin{lstlisting}[label=lst:range_functions_label_implicit]
418for ( char * alph; Alphabet )
419\end{lstlisting}
420This 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.
421If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration.
422\begin{lstlisting}[label=lst:range_functions_label_implicit]
423for ( char * ch; labels( Alphabet ) )
424\end{lstlisting}
425
426\section{Implementation}
427
428\CFA places the definition of Companion structure and non-parameterized Companion functions in the prelude, visible globally.
429
430\subsection{Declaration}
431
432The qualified enumeration syntax is dedicated to \CFA enumeration.
433\begin{lstlisting}[label=lst:range_functions]
434enum (type_declaration) name { enumerator = const_expr, enumerator = const_expr, ... }
435\end{lstlisting}
436A compiler stores the name, the underlying type, and all enumerators in an @enumeration table@.
437During the $Validation$ pass, the compiler links the type declaration to the type's definition.
438It 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.
439If the declared type is not @AutoInitializable@, \CFA rejects the enumeration definition.
440Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section)
441
442\begin{lstlisting}[label=lst:init]
443struct T { ... };
444void ?{}( T & t, zero_t ) { ... };
445void ?{}( T & t, one_t ) { ... };
446T ?+?( T & lhs, T & rhs ) { ... };
447
448enum (T) Sample { 
449        Zero: 0 /* zero_t */,
450        One: Zero + 1 /* ?+?( Zero, one_t ) */ , ...
451};
452\end{lstlisting}
453Challenge: \\
454The value of an enumerator, or the initializer, requires @const_expr@.
455While previously getting around the issue by pushing it to the C compiler, it might not work anymore because of the user-defined types, user-defined @zero_t@, @one_t@, and addition operation.
456Might not be able to implement a \emph{correct} static check.
457
458\CFA $autogens$ a Companion object for the declared enumeration.
459\begin{lstlisting}[label=lst:companion]
460Companion( T ) Sample {
461        .values: { 0, 0+1, 0+1+1, 0+1+1+1, ... }, /* 0: zero_t, 1: one_t, +: ?+?{} */
462        .labels: { "Zero", "One", "Two", "Three", ...},
463        .length: /* number of enumerators */
464};
465\end{lstlisting}
466\CFA stores values as intermediate expressions because the result of the function call to the function @?+?{}(T&, T&)@ is statically unknown to \CFA.
467But the result is computed at run time, and the compiler ensures the @values@ are not changed.
468
469\subsection{Qualified Expression}
470
471\CFA uses qualified expression to address the scoping of \CFA-enumeration.
472\begin{lstlisting}[label=lst:qualified_expression]
473aggregation_name.field;
474\end{lstlisting}
475The qualified expression is not dedicated to \CFA enumeration.
476It is a feature that is supported by other aggregation in \CFA as well, including a C enumeration.
477When C enumerations are unscoped, the qualified expression syntax still helps to disambiguate names in the context.
478\CFA recognizes if the expression references a \CFA aggregation by searching the presence of @aggregation_name@ in the \CFA enumeration table.
479If the @aggregation_name@ is identified as a \CFA enumeration, the compiler checks if @field@ presents in the declared \CFA enumeration.
480
481\subsection{\lstinline{with} Statement}
482
483\emph{Work in Progress}
484
485Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible.
486
487\subsection{Instance Declaration}
488
489\emph{Work in Progress}
490
491\begin{lstlisting}[label=lst:declaration]
492enum Sample s1;
493Sample s2;
494\end{lstlisting}
495A declaration of \CFA enumeration instance that has no difference than a C enumeration or other \CFA aggregation.
496The compiler recognizes the type of a variable declaration by searching the name in all possible types.
497The @enum@ keyword is not necessary but helps to disambiguate types (questionable).
498The generated code for a \CFA enumeration declaration is utterly an integer, which is meant to store the position.
499\begin{lstlisting}[label=lst:declaration]
500int s1;
501int s2;
502\end{lstlisting}
503
504\subsection{Compiler Representation}
505
506\emph{Work in Progress}
507
508The internal representation of an enumeration constant is @EnumInstType@.
509The minimum information an @EnumInstType@ stores is a reference to the \CFA-enumeration declaration and the position of the enumeration constant.
510\begin{lstlisting}[label=lst:EnumInstType]
511class EnumInstType {
512        EnumDecl enumDecl;
513        int position;
514};
515\end{lstlisting}
516
517\subsection{Unification and Resolution }
518
519\emph{Work in Progress}
520
521\begin{lstlisting}
522enum Colour( char * ) { Red = "R", Green = "G", Blue = "B"  };
523\end{lstlisting}
524The @EnumInstType@ is convertible to other types.
525A \CFA enumeration expression is implicitly \emph{overloaded} with its three different attributes: value, position, and label.
526The \CFA compilers need to resolve an @EnumInstType@ as one of its attributes based on the current context.
527
528\begin{lstlisting}[caption={Null Context}, label=lst:null_context]
529{
530        Colour.Green;
531}
532\end{lstlisting}
533In example~\ref{lst:null_context}, the environment gives no information to help with the resolution of @Colour.Green@.
534In this case, any of the attributes is resolvable.
535According to the \textit{precedence rule}, the expression with @EnumInstType@ resolves as @value( Colour.Green )@.
536The @EnumInstType@ is converted to the type of the value, which is statically known to the compiler as @char *@.
537When the compilation reaches the code generation, the compiler outputs code for type @char *@ with the value @"G"@.
538\begin{lstlisting}[caption={Null Context Generated Code}, label=lst:null_context]
539{
540        "G";
541}
542\end{lstlisting}
543\begin{lstlisting}[caption={int Context}, label=lst:int_context]
544{
545        int g = Colour.Green;
546}
547\end{lstlisting}
548The assignment expression gives a context for the EnumInstType resolution.
549The EnumInstType is used as an @int@, and \CFA needs to determine which of the attributes can be resolved as an @int@ type.
550The functions $Unify( T1, T2 ): bool$ take two types as parameters and determine if one type can be used as another.
551In example~\ref{lst:int_context}, the compiler is trying to unify @int@ and @EnumInstType@ of @Colour@.
552$$Unification( int, EnumInstType<Colour> )$$ which turns into three Unification call
553\begin{lstlisting}[label=lst:attr_resolution_1]
554{
555        Unify( int, char * ); // unify with the type of value
556        Unify( int, int ); // unify with the type of position
557        Unify( int, char * ); // unify with the type of label
558}
559\end{lstlisting}
560\begin{lstlisting}[label=lst:attr_resolution_precedence]
561{
562        Unification( T1, EnumInstType<T2> ) {
563                if ( Unify( T1, T2 ) ) return T2;
564                if ( Unify( T1, int ) ) return int;
565                if ( Unify( T1, char * ) ) return char *;
566                Error: Cannot Unify T1 with EnumInstType<T2>;
567        }
568}
569\end{lstlisting}
570After the unification, @EnumInstType@ is replaced by its attributes.
571
572\begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
573{
574        T2 foo ( T1 ); // function take variable with T1 as a parameter
575        foo( EnumInstType<T3> ); // Call foo with a variable has type EnumInstType<T3>
576        >>>> Unification( T1, EnumInstType<T3> )
577}
578\end{lstlisting}
579% The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType.
580Backward conversion:
581\begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
582{
583        enum Colour colour = 1;
584}
585\end{lstlisting}
586
587\begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
588{
589   Unification( EnumInstType<Colour>, int ) >>> label
590}
591\end{lstlisting}
592@int@ can be unified with the label of Colour.
593@5@ is a constant expression $\Rightarrow$ Compiler knows the value during the compilation $\Rightarrow$ turns it into
594\begin{lstlisting}
595{
596   enum Colour colour = Colour.Green;
597}
598\end{lstlisting}
599Steps:
600\begin{enumerate}
601\item
602identify @1@ as a constant expression with type @int@, and the value is statically known as @1@
603\item
604@unification( EnumInstType<Colour>, int )@: @position( EnumInstType< Colour > )@
605\item
606return the enumeration constant at the position 1
607\end{enumerate}
608\begin{lstlisting}
609{
610        enum T (int) { ... } // Declaration
611        enum T t = 1;
612}
613\end{lstlisting}
614Steps:
615\begin{enumerate}
616\item
617identify @1@ as a constant expression with type @int@, and the value is statically known as @1@
618\item
619@unification( EnumInstType<Colour>, int )@: @value( EnumInstType< Colour > )@
620\item
621return the FIRST enumeration constant that has the value 1, by searching through the values array
622\end{enumerate}
623The 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
624
625\end{document}
626
627% Local Variables: %
628% tab-width: 4 %
629% compile-command: "pdflatex enum.tex" %
630% End: %
Note: See TracBrowser for help on using the repository browser.