source: doc/proposals/enum.tex @ 0030b508

Last change on this file since 0030b508 was 0030b508, checked in by JiadaL <j82liang@…>, 12 months ago

some updates

  • Property mode set to 100644
File size: 26.4 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\makeatother
17
18\newenvironment{cquote}{%
19        \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindent\rightmargin\leftmargin}%
20        \item\relax
21}{%
22        \endlist
23}% cquote
24
25\setlength{\topmargin}{-0.45in}                                                 % move running title into header
26\setlength{\headsep}{0.25in}
27\setlength{\textheight}{9.0in}
28
29\newcommand{\CFAIcon}{\textsf{C\raisebox{\depth}{\rotatebox{180}A}}} % Cforall icon
30\newcommand{\CFA}{\protect\CFAIcon\xspace}                              % CFA symbolic name
31\newcommand{\PAB}[1]{{\color{red}PAB: #1}}
32
33% \definecolor{mGreen}{rgb}{0,0.6,0}
34% \definecolor{mGray}{rgb}{0.5,0.5,0.5}
35% \definecolor{mPurple}{rgb}{0.58,0,0.82}
36% \definecolor{backgroundColour}{rgb}{0.95,0.95,0.92}
37
38\lstdefinestyle{CStyle}{
39%    backgroundcolor=\color{backgroundColour},   
40%    commentstyle=\color{mGreen},
41%    keywordstyle=\color{magenta},
42        stringstyle=\small\tt,                                  % use typewriter font
43%    stringstyle=\color{mPurple},
44    columns=fullflexible,
45    basicstyle=\small\linespread{0.9}\sf,       % reduce line spacing and use sanserif font
46%   basicstyle=\footnotesize,
47    breakatwhitespace=false,         
48%    breaklines=true,                 
49    captionpos=b,                   
50    keepspaces=true,                 
51%    numbers=left,                   
52%    numbersep=5pt,                 
53%    numberstyle=\tiny\color{mGray},
54%    showspaces=false,               
55    showstringspaces=false,
56%    showtabs=false,                 
57        showlines=true,                                                 % show blank lines at end of code
58    tabsize=5,
59    language=C,
60        aboveskip=4pt,                                                  % spacing above/below code block
61        belowskip=2pt,
62        xleftmargin=\parindent,                 % indent code to paragraph indentation
63}
64\lstset{style=CStyle,moredelim=**[is][\color{red}]{@}{@}}
65\lstMakeShortInline@                            % single-character for \lstinline
66
67\begin{document}
68
69\title{\vspace*{-0.5in}Enumeration in \CFA}
70\author{Jiada Liang}
71
72\maketitle
73
74\begin{abstract}
75An enumeration is a type that defines a list of named constant values in C (and other languages).
76C uses an integral type as the underlying representation of an enumeration.
77\CFA extends C enumerations to allow all basic and custom types for the inner representation.
78\end{abstract}
79
80\section{C-Style Enum}
81
82\CFA supports the C-Style enumeration using the same syntax and semantics.
83\begin{lstlisting}[label=lst:weekday]
84enum Weekday { Monday, Tuesday, Wednesday, Thursday=10, Friday, Saturday, Sunday };
85\end{lstlisting}
86The example defines an @enum@ type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@.
87The successor of @Tuesday@ is @Monday@ and the predecessor of @Tuesday@ is @Wednesday@.
88A C enumeration has an integral type, with consecutive enumerator values assigned by the compiler starting at zero or explicitly initialized by the programmer.
89For example, @Monday@ to @Wednesday@ have values 0--2, @Thursday@ is set to @10@, and after it, @Friday@ to @Sunday@ have values 11--13.
90
91There are 3 attributes for an enumeration: position, label, and value:
92\begin{cquote}
93\small\sf
94\begin{tabular}{rccccccccccc}
95enum Weekday \{ & Monday,       & Tuesday,      & Wednesday,    & Thursday=10,  & Friday,       & Saturday,     & Sunday \}; \\
96position                & 0                     & 1                     & 2                             & 3                             & 4                     & 5                     & 6                     \\
97label                   & Monday        & Tuesday       & Wednesday             & Thursday              & Friday        & Saturday      & Sunday        \\
98value                   & 0                     & 1                     & 2                             & 10                    & 11            & 12            & 13
99\end{tabular}
100\end{cquote}
101
102The enumerators of an enum are unscoped, i.e., enumerators declared inside of an enum are visible in the enclosing scope of the enum class.
103\begin{lstlisting}[label=lst:enum_scope]
104{
105        enum RGB { R, G, B };
106        int i = R  // i == 0
107}
108int j = G; // ERROR! G is not declared in this scope
109\end{lstlisting}
110
111\section{\CFA-Style Enum}
112
113A \CFA enumeration is parameterized by a type, which specifies the type for each enumerator.
114\CFA allows any object type for the enumerators, and values assigned to enumerators must be in the declared type.
115\begin{lstlisting}[label=lst:color]
116enum Colour( @char *@ ) { Red = "R", Green = "G", Blue = "B"  };
117\end{lstlisting}
118The type of @Colour@ is @char *@ and each enumerator is initialized with a C string.
119Only types have define an ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}).
120
121
122% An instance of \CFA-enum (denoted as @<enum_instance>@) is a label for the defined enum name.
123% The label can be retrieved by calling the function @label( <enum_instance> )@.
124% Similarly, the @value()@ function returns the value used to initialize the \CFA-enum.
125
126A \CFA-enum is scoped: enumeration constants are not automatically exposed to the global scope. Enumeration constant can be referenced using qualified expressions like an aggregate that supports qualified references to its fields. The syntax of $qualified_expression$ for \CFA-enum is the following:
127$$<qualified\_expression> := <enum\_type>.<enumerator>$$
128
129
130\subsection{enumeration instance}
131\begin{lstlisting}[label=lst:sample_cforall_enum_usage]
132Colour green = Colour.Green;
133\end{lstlisting}
134The ~\ref{lst:sample_cforall_enum_usage} example declares a $enumeration\ instance$ named \textit{red} and initializes it with $enumeration\ constant$ \textit{Color.Red}. An enumeration instance is a data structure that captures attributes of an enumeration constant, which can be retrieved by functions $position( enumeration\ instance )$, $value( enumeration\ instance )$, and $label( enumeration\ instance )$.
135
136\begin{lstlisting}
137int green_pos = position( green ); // 1
138char * green_value = value( green ); // "G"
139char * green_label = label( green ); // "Green"
140\end{lstlisting}
141
142An enumeration instance can be assigned to a variable or used as its position with type integer, its value with declared type T, or its label with type char *, and the compiler will resolve the usage as a type fits the context.
143
144\begin{lstlisting}[label=lst:enum_inst_assign_int]
145int green_pos = green; // equivalent to position( green );
146\end{lstlisting}
147
148A resolution of an enumeration constant is $unambigious$ if only one of the attributes has the resolvable type. In the example~\ref{lst:enum_inst_assign_int }, the right-hand side of the assignment expression expects integer type. The position of an enumeration is int, while the other two cannot be resolved as integers. The expression unambiguously returns the position of green.
149
150\begin{lstlisting}[label=lst:enum_inst_assign_string]
151char * green_value = green; // equivalent to value( green );
152\end{lstlisting}
153On the other hand, the resolution of an enumeration constant is $ambigious$ if multiple attributes have the expected type. In the example~\ref{lst:enum_inst_assign_string}, both value and label have the expected type char *. When a resolution is ambiguous, a \textit{resolution precedence} applies:
154$$value > position > label$$
155\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.
156
157\begin{lstlisting}[label=lst:enum_inst_precedence]
158enum(double) Foo { Bar };
159int tee = Foo.Bar; // value( Bar );
160\end{lstlisting}
161In 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.
162
163Although \CFA enumeration captures three different attributes, an instance of enumeration does not occupy extra memory. The $sizeof$ \CFA enumeration instance is always 4 bytes, the same amount of memory to store a C enumeration instance. It comes from the fact that: 1. a \CFA enumeration is always statically typed; 2. it is always resolved as one of its attributes in terms of real usage.
164
165When creating the enumeration instance green and assigns it with the enumeration constant $Color.Green$, the compilers essentially allocate an integer variables and store the position 1. The invocations of $positions()$, $value()$, and $label()$ turn into calls to special functions defined in the prelude:
166\begin{lstlisting}[label=lst:companion_call]
167position( green );
168>>> position( Colour, 1 ) -> int
169value( green );
170>>> value( Colour, 1 ) -> T
171label( green );
172>>> label( Colour, 1) -> char *
173\end{lstlisting}
174T represents the type declared in the \CFA enumeration defined and char * in the example.
175These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters.
176
177\subsection{Companion Object and Companion Function}
178\begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_functions]
179forall( T )  {
180        struct Companion {
181                const T * const values;
182                const char** const labels;
183            int length;
184        };
185}
186\end{lstlisting}
187\CFA creates an object of Companion for every \CFA-enumeration. A companion object has the same name as the enumeration is defined for. A companion object stores values and labels of enumeration constants, in the order of the constants defined in the enumeration.
188
189\CFA generates the definition of companion functions. Because \CFA implicitly stores enumeration instance as its position, the companion function $position$ does nothing but returns the position it passes it. Companions function $value$ and $label$ return the array item at the given position of $values$ and $labels$, respectively.
190
191\begin{lstlisting}[label=lst:companion_definition]
192int position( Companion o, int pos ) { return pos; }
193T value( Companion o, int pos ) { return o.values[ pos ]; }
194char * label( Companion o, int pos ) { return o.labels[ pos ]; }
195\end{lstlisting}
196
197Notably, the Companion structure definition, and all companion objects, are visible to the users. A 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$
198\begin{lstlisting}[label=lst:companion_definition_values_labels]
199Colour.values; // read the Companion's values
200values( Colour ); // Same as Colour.values
201\end{lstlisting}
202
203\subsection{User Define Enumeration Functions}
204The Companion objects make extending features for \CFA enumeration easy.
205
206\begin{lstlisting}[label=lst:companion_user_definition]
207char * charastic_string( Companion o, int position ) { 
208    return sprintf("Label: %s; Value: %s", label( o, position ), value( o, position) );
209}
210printf( charactic_string ( Color, 1 ) );
211>>> Label: G; Value: G
212\end{lstlisting}
213Defining a function takes a Companion object effectively defines functions for all \CFA enumeration.
214
215The \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. Therefore, a user can use the syntax with a user-defined enumeration function call:
216\begin{lstlisting}[label=lst:companion_user_definition]
217charactic_string ( Color.Green ); // equivalent to charactic_string ( Color, 1 )
218>>> Label: G; Value: G
219\end{lstlisting}
220
221Similarly, the user can work with the enumeration type itself: (see section ref...)
222\begin{lstlisting}[ label=lst:companion_user_definition]
223void print_enumerators ( Companion o ) { 
224    for ( c : Companion o ) {
225        sout | label (c) | value( c ) ;
226    } 
227}
228print_enumerators( Colour );
229\end{lstlisting}
230
231\subsection{Runtime Enumeration}
232The Companion structure definition is visible to users, and users can create an instance of Companion object themselves, which effectively constructs a \textit{Runtime Enumeration}.
233\begin{lstlisting}[ label=lst:runtime_enum ]
234const char values[] = { "Hello", "World" };
235const char labels[] = { "First", "Second" };
236Companion (char *) MyEnum = { .values: values, .labels: labels, .length: 2 };
237\end{lstlisting}
238A runtime enumeration can be used to call enumeration functions.
239\begin{lstlisting}[ label=lst:runtime_enum_usage ]
240sout | charatstic_string( MyEnum, 1 );
241>>> Label: Second; Value: World
242\end{lstlisting}
243However, a runtime enumeration cannot create an enumeration instance, and it does not support enum-qualified syntax.
244\begin{lstlisting}[ label=lst:runtime_enum_usage ]
245MyEnum e = MyEnum.First; // Does not work: cannot create an enumeration instance e,
246                        // and MyEnum.First is not recognizable
247\end{lstlisting}
248During the compilation, \CFA adds enumeration declarations to an enumeration symbol table and creates specialized function definitions for \CFA enumeration. \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.
249
250\section{Enumeration Features}
251A trait is a collection of constraints in \CFA, which can be used to describe types.
252The \CFA standard library defines traits to categorize types with related enumeration features.
253
254
255\subsection{Auto Initializable}
256\label{s:AutoInitializable}
257TODO: make the initialization rule a separate section.
258
259If no explicit initializer is given to an enumeration constant, C initializes the first enumeration constant with value 0, and the other enumeration constant has a value equal to its $predecessor+1$. \CFA enumerations have the same rule in enumeration constant initialization. However, not all types can be automatically initialized by \CFA because the meaning of $zero$, $one$, and addition operator may not be well-defined.
260
261A type is auto-initializable if it has defined $zero\_t$, $one\_t$, and an addition operator.
262\begin{lstlisting}
263forall(T)
264trait AutoInitializable {
265        void ?()( T & t, zero_t );
266        void ?()( T & t, one_t );
267        S ?+?( T & t, one_t );
268};
269\end{lstlisting}
270
271An example of user-defined @AutoInitializable@ would look like the following:
272\begin{lstlisting}[label=lst:sample_auto_initializable]
273struct Odd { int i; };
274void ?()( Odd & t, zero_t ) { t.i = 1; };
275void ?()( Odd & t, one_t ) { t.i = 2; };
276Odd ?+?( Odd t1, Odd t2 )
277    { return Odd( t1.i + t2.i); };
278\end{lstlisting}
279
280When an enumeration declares an AutoInitializable as its type, no explicit initialization is necessary.
281\begin{lstlisting}[label=lst:sample_auto_initializable_usage]
282enum AutoInitUsage(Odd) {
283    A, B, C = 6, D
284};
285\end{lstlisting}
286
287In the example~\ref{lst:sample_auto_initializable_usage}, because no initializer is specified for the first enumeration constant @A@, \CFA initializes it with the value of $zero_t$, which is 1. B and D have the values of their $predecessor + one_t$, while $one_t$ has the value 2. Therefore, the enumeration is initialized as the following:
288
289\begin{lstlisting}[label=lst:sample_auto_initializable_usage_gen]
290enum AutoInitUsage(Odd) {
291    A=1, B=3, C = 6, D=8
292};
293\end{lstlisting}
294
295In \CFA, integral types, float types, and imaginary numbers are example types that are AutoInitialiable.
296\begin{lstlisting}[label=lst:letter]
297enum Alphabet(int) {
298    A='A', B, C, D, E, F, G, H, I, J, K, L, M,
299    N, O, P, Q, R, S, T, U, V, W, X, Y, Z,
300    a='a', b, c, d, e, f, g, h, i, j, k, l, m,
301    n, o, p, q, r, s, t, u, v, w, x, y, z
302};
303print( "%c, %c, %c", Alphabet.F, Alphabet.o, Alphabet.o );
304>>> F, o, o
305\end{lstlisting}
306
307\subsection{Iteration and Range}
308
309It is convenient to iterate over a \CFA enumeration. Here is the basic usage:
310\begin{lstlisting}[label=lst:range_functions]
311for ( Alphabet ch; Alphabet; ) {
312    printf( "%d ", ch );
313}
314>>> A B C (...omit the rest)
315
316\end{lstlisting}
317The for-loop uses the enumeration type @Alphabet@ as range. When that happens, \CFA iterates all enumerators in the order they defined in the enumeration. 'ch' is the iterating enumerator, and it returns the value of an Alphabet in this context according to the precedence rule.
318
319\CFA offers a shorthand for iterating all enumeration constants:
320\begin{lstlisting}[label=lst:range_functions]
321for ( Alphabet ch ) {
322    printf( "%d ", ch );
323}
324>>> A B C (...omit the rest)
325\end{lstlisting}
326
327Enumeration supports the \CFA loop control syntax for for-loop.
328\begin{lstlisting}[label=lst:range_functions]
329for ( Alphabet.D )
330for ( ch; Alphabet.g ~ Alphabet.z )
331for ( Alphabet ch; Alphabet.R ~ Alphabet.X ~ 2 )
332\end{lstlisting}
333
334Notably, the meaning of "step" of iteration has changed for enumeration. Consider the following example:
335\begin{lstlisting}[label=lst:range_functions]
336enum(int) Sequence {
337    A = 10, B = 12, C = 14; 
338}
339for ( s; Sequence.A ~ Sequence.C ) {
340    printf( "%d ", s );
341}
342
343>>> 10 12 14
344
345for ( s; Sequence.A ~ Sequence.A ~ 2 ) {
346    printf( "%d ", s );
347}
348>>> 10 14
349\end{lstlisting}
350The range iteration of enumeration does not return the $current\_value++$ until it reaches the upper bound. The semantics is to return the next enumeration constant. If a stepping is specified, 2 for example, it returns the 2 enumeration constant after the current one, rather than the $current+2$
351
352It is also possible to iterate over an enumeration's labels, implicitly or explicitly:
353\begin{lstlisting}[label=lst:range_functions_label_implicit]
354for ( char * ch; Alphabet )
355\end{lstlisting}
356This 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. If the value can also be resolved as the char *, you might iterate the labels explicitly with the array iteration.
357\begin{lstlisting}[label=lst:range_functions_label_implicit]
358for ( char * ch; labels( Alphabet ) )
359\end{lstlisting}
360
361\section{Implementation}
362\CFA places the definition of Companion structure and non-parameterized Companion functions in the prelude, visible globally.
363
364\subsection{declaration}
365The qualified enumeration syntax is dedicated to \CFA enumeration.
366\begin{lstlisting}[label=lst:range_functions]
367enum (type_declaration) name { enumerator = const_expr, enumerator = const_expr, ... }
368\end{lstlisting}
369A compiler stores the name, the underlying type, and all enumerators in an @enumeration table@. During the $Validation$ pass, the compiler links the type declaration to the type's definition. It ensures that the name of an enumerator is unique within the enumeration body, and checks if all values of the enumerator have the declaration type. If the declared type is not @Auto Initializable@, \CFA rejects the enumeration definition. Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section)
370
371\begin{lstlisting}[label=lst:init]
372struct T { ... };
373void ?{}( T & t, zero_t ) { ... };
374void ?{}( T & t, one_t ) { ... };
375T ?+?( T & lhs, T & rhs ) { ... };
376
377enum (T) Sample { 
378    Zero: 0 /* zero_t */,
379    One: Zero + 1 /* ?+?( Zero, one_t ) */ , ...
380};
381\end{lstlisting}
382
383Challenge:
384The value of an enumerator, or the initializer, requires $const\_expr$. While 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. Might not be able to implement a *Correct* static check.
385
386\CFA $autogens$ a Companion object for the declared enumeration.
387\begin{lstlisting}[label=lst:companion]
388Companion( T ) Sample {
389    .values: { 0, 0+1, 0+1+1, 0+1+1+1, ... }, /* 0: zero_t, 1: one_t, +: ?+?{} */
390    .labels: { "Zero", "One", "Two", "Three", ...},
391    .length: /* number of enumerators */
392};
393\end{lstlisting}
394\CFA stores values as intermediate expressions because the result of the function call to the function $?+?{}(T\&, T\&)$ is statically unknown to \CFA. But the result will be computed in run time, and the compiler ensures the $values$ will not be changed.
395
396\subsection{qualified expression}
397\CFA uses qualified expression to address the scoping of \CFA-enumeration.
398\begin{lstlisting}[label=lst:qualified_expression]
399aggregation_name.field;
400\end{lstlisting}
401The qualified expression is not dedicated to \CFA enumeration. It is a feature that is supported by other aggregation in \CFA as well, including a C enumeration. When C enumerations are unscoped, the qualified expression syntax still helps to disambiguate names in the context. \CFA recognizes if the expression references a \CFA aggregation by searching the presence of $aggregation\_name$ in the \CFA enumeration table. If the $aggregation\_name$ is identified as a \CFA enumeration, the compiler checks if $field$ presents in the declared \CFA enumeration.
402
403\subsection{with statement/statement}
404@Working in Progress@
405Instead of qualifying an enumeration expression every time, one can use the $with$ to expose enumerators to the current scope so that they are directly accessible.
406
407\subsection{instance declaration}
408@Working in Progress@
409\begin{lstlisting}[label=lst:declaration]
410enum Sample s1;
411Sample s2;
412\end{lstlisting}
413A declaration of \CFA enumeration instance that has no difference than a C enumeration or other \CFA aggregation. The compiler recognizes the type of a variable declaration by searching the name in all possible types. The \textit{enum} keyword is not necessary but helps to disambiguate types (questionable). The generated code for a \CFA enumeration declaration is utterly an integer, which is meant to store the position.
414\begin{lstlisting}[label=lst:declaration]
415int s1;
416int s2;
417\end{lstlisting}
418
419\subsection{Compiler Representation}
420@Working in Progress@
421
422The internal representation of an enumeration constant is \textit{EnumInstType}. The minimum information an \textit{EnumInstType} stores is a reference to the \CFA-enumeration declaration and the position of the enumeration constant.
423\begin{lstlisting}[label=lst:EnumInstType]
424class EnumInstType {
425    EnumDecl enumDecl;
426    int position;
427};
428\end{lstlisting}
429
430
431\subsection{unification and resolution }
432@Working in Progress@
433
434\begin{lstlisting}
435enum Colour( char * ) { Red = "R", Green = "G", Blue = "B"  };
436\end{lstlisting}
437The EnumInstType is convertible to other types. A \CFA enumeration expression is implicitly "overloaded" with its three different attributes: value, position, and label. The \CFA compilers need to resolve an EnumInstType as one of its attributes based on the current context.
438
439\begin{lstlisting}[caption={Null Context}, label=lst:null_context]
440{
441    Colour.Green;
442}
443\end{lstlisting}
444In the example~\ref{lst:null_context}, the environment gives no information to help with the resolution of $Colour.Green$. In this case, any of the attributes is resolvable. According to the \textit{precedence rule}, the expression with EnumInstType will be resolved as $value( Colour.Green )$. The EnumInstType is converted to the type of the value, which is statically known to the compiler as char *. When the compilation reaches the code generation, the compiler outputs code for type char * with the value "G".
445\begin{lstlisting}[caption={Null Context Generated Code}, label=lst:null_context]
446{
447    "G";
448}
449\end{lstlisting}
450
451 
452\begin{lstlisting}[caption={int Context}, label=lst:int_context]
453{
454    int g = Colour.Green;
455}
456\end{lstlisting}
457
458The assignment expression gives a context for the EnumInstType resolution. The EnumInstType is used as an int, and \CFA needs to determine which of the attributes can be resolved as an int type.
459The functions $Unify( T1, T2 ): bool$ take two types as parameters and determine if one type can be used as another. In the example\ref{lst:int_context} example, the compiler is trying to unify int and EnumInstType of Colour.
460$$Unification( int, EnumInstType<Colour> )$$
461which turns into three Unification call
462\begin{lstlisting}[label=lst:attr_resolution_1]
463{
464    Unify( int, char * ); // unify with the type of value
465    Unify( int, int ); // unify with the type of position
466    Unify( int, char * ); // unify with the type of label
467}
468\end{lstlisting}
469
470\begin{lstlisting}[label=lst:attr_resolution_precedence]
471{
472    Unification( T1, EnumInstType<T2> ) {
473        if ( Unify( T1, T2 ) ) return T2;
474        if ( Unify( T1, int ) ) return int;
475        if ( Unify( T1, char * ) ) return char *;
476        Error: Cannot Unify T1 with EnumInstType<T2>;
477    }
478}
479\end{lstlisting}
480After the unification, EnumInstType will be replaced by its attributes.
481
482\begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
483{
484    T2 foo ( T1 ); // function take variable with T1 as a parameter
485    foo( EnumInstType<T3> ); // Call foo with a variable has type EnumInstType<T3>
486    >>>> Unification( T1, EnumInstType<T3> )
487}
488\end{lstlisting}
489
490% The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType.
491Backward conversion:
492\begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
493{
494    enum Colour colour = 1;
495}
496\end{lstlisting}
497
498\begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call]
499{
500   Unification( EnumInstType<Colour>, int ) >>> label
501}
502\end{lstlisting}
503int can be unified with the label of Colour. "5" is a constant expression -> Compiler knows the value during the compilation -> turns it into
504\begin{lstlisting}
505{
506   enum Colour colour = Colour.Green;
507}
508\end{lstlisting}
509Steps:
5101: identify "1" as a constant expression with type int, and the value is statically known as 1
5112. unification( EnumInstType<Colour>, int ): position( EnumInstType< Colour > )
5123. return the enumeration constant at the position 1
513
514\begin{lstlisting}
515{
516    enum T (int) { ... } // Declaration
517    enum T t = 1;
518}
519\end{lstlisting}
520Steps:
5211: identify "1" as a constant expression with type int, and the value is statically known as 1
5222. unification( EnumInstType<Colour>, int ): value( EnumInstType< Colour > )
5233. return the FIRST enumeration constant that has the value 1, by searching through the values array
524
525The downside of the precedence rule: EnumInstType -> int ( value ) -> EnumInstType may return a different EnumInstType because the value can be repeated and there is no way to know which one is expected -> want uniqueness
526
527\end{document}
528
529% Local Variables: %
530% tab-width: 4 %
531% compile-command: "pdflatex enum.tex" %
532% End: %
Note: See TracBrowser for help on using the repository browser.