source: doc/theses/jiada_liang_MMath/implementation.tex @ 54af365

Last change on this file since 54af365 was 7bb516f, checked in by Peter A. Buhr <pabuhr@…>, 9 months ago

more proofreading on enumeration thesis

  • Property mode set to 100644
File size: 29.6 KB
Line 
1\chapter{Enumeration Implementation}
2
3
4\section{Enumeration Variable}
5
6Although \CFA enumeration captures three different attributes, an enumeration instance does not store all this information.
7The @sizeof@ a \CFA enumeration instance is always 4 bytes, the same size as a C enumeration instance (@sizeof( int )@).
8It comes from the fact that:
9\begin{enumerate}
10\item
11a \CFA enumeration is always statically typed;
12\item
13it is always resolved as one of its attributes regarding real usage.
14\end{enumerate}
15When creating an enumeration instance @colour@ and assigning it with the enumerator @Color.Green@, the compiler allocates an integer variable and stores the position 1.
16The invocations of $positions()$, $value()$, and $label()$ turn into calls to special functions defined in the prelude:
17\begin{cfa}
18position( green );
19>>> position( Colour, 1 ) -> int
20value( green );
21>>> value( Colour, 1 ) -> T
22label( green );
23>>> label( Colour, 1) -> char *
24\end{cfa}
25@T@ represents the type declared in the \CFA enumeration defined and @char *@ in the example.
26These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters.
27
28
29\section{Enumeration Data}
30
31\begin{cfa}
32enum(T) E { ... };
33// backing data
34T * E_values;
35char ** E_labels;
36\end{cfa}
37Storing values and labels as arrays can sometimes help support enumeration features.
38However, the data structures are the overhead for the programs. We want to reduce the memory usage for enumeration support by:
39\begin{itemize}
40        \item Only generates the data array if necessary
41        \item The compilation units share the data structures.
42        No extra overhead if the data structures are requested multiple times.
43\end{itemize}
44
45
46\section{Unification}
47
48\section{Enumeration as Value}
49\label{section:enumeration_as_value}
50An \CFA enumeration with base type T can be used seamlessly as T, without explicitly calling the pseudo-function value.
51\begin{cfa}
52char * green_value = Colour.Green; // "G"
53// Is equivalent to
54// char * green_value = value( Color.Green ); "G"
55\end{cfa}
56
57
58\section{Unification Distance}
59
60\begin{cfa}
61T_2 Foo(T1);
62\end{cfa}
63The @Foo@ function expects a parameter with type @T1@. In C, only a value with the exact type T1 can be used as a parameter for @Foo@. In \CFA, @Foo@ accepts value with some type @T3@ as long as @distance(T1, T3)@ is not @Infinite@.
64
65@path(A, B)@ is a compiler concept that returns one of the following:
66\begin{itemize}
67        \item Zero or 0, if and only if $A == B$.
68        \item Safe, if B can be used as A without losing its precision, or B is a subtype of A.
69        \item Unsafe, if B loses its precision when used as A, or A is a subtype of B.
70        \item Infinite, if B cannot be used as A. A is not a subtype of B and B is not a subtype of A.
71\end{itemize}
72
73For example, @path(int, int)==Zero@, @path(int, char)==Safe@, @path(int, double)==Unsafe@, @path(int, struct S)@ is @Infinite@ for @struct S{}@.
74@distance(A, C)@ is the minimum sum of paths from A to C. For example, if @path(A, B)==i@, @path(B, C)==j@, and @path(A, C)=k@, then $$distance(A,C)==min(path(A,B), path(B,C))==i+j$$.
75
76(Skip over the distance matrix here because it is mostly irrelevant for enumeration discussion. In the actual implementation, distance( E, T ) is 1.)
77
78The arithmetic of distance is the following:
79\begin{itemize}
80        \item $Zero + v= v$, for some value v.
81        \item $Safe * k <  Unsafe$, for finite k.
82        \item $Unsafe * k < Infinite$, for finite k.
83        \item $Infinite + v = Infinite$, for some value v.
84\end{itemize}
85
86For @enum(T) E@, @path(T, E)==Safe@ and @path(E,T)==Infinite@. In other words, enumeration type E can be @safely@ used as type T, but type T cannot be used when the resolution context expects a variable with enumeration type @E@.
87
88
89\section{Variable Overloading and Parameter Unification}
90
91\CFA allows variable names to be overloaded. It is possible to overload a variable that has type T and an enumeration with type T.
92\begin{cfa}
93char * green = "Green";
94Colour green = Colour.Green; // "G"
95
96void bar(char * s) { return s; }
97void foo(Colour c) { return value( c ); }
98
99foo( green ); // "G"
100bar( green ); // "Green"
101\end{cfa}
102\CFA's conversion distance helps disambiguation in this overloading. For the function @bar@ which expects the parameter s to have type @char *@, $distance(char *,char *) == Zero$ while $distance(char *, Colour) == Safe$, the path from @char *@ to the enumeration with based type @char *@, \CFA chooses the @green@ with type @char *@ unambiguously. On the other hand, for the function @foo@, @distance(Colour, char *)@ is @Infinite@, @foo@ picks the @green@ with type @char *@.
103
104\section{Function Overloading}
105Similarly, functions can be overloaded with different signatures. \CFA picks the correct function entity based on the distance between parameter types and the arguments.
106\begin{cfa}
107Colour green = Colour.Green;
108void foo(Colour c) { sout | "It is an enum"; } // First foo
109void foo(char * s) { sout | "It is a string"; } // Second foo
110foo( green ); // "It is an enum"
111\end{cfa}
112Because @distance(Colour, Colour)@ is @Zero@ and @distance(char *, Colour)@ is @Safe@, \CFA determines the @foo( green )@ is a call to the first foo.
113
114\section{Attributes Functions}
115The pseudo-function @value()@ "unboxes" the enumeration and the type of the expression is the underlying type. Therefore, in the section~\ref{section:enumeration_as_value} when assigning @Colour.Green@ to variable typed @char *@, the resolution distance is @Safe@, while assigning @value(Color.Green) to @char *) has resolution distance @Zero@.
116
117\begin{cfa}
118int s1;
119\end{cfa}
120The generated code for an enumeration instance is simply an int. It is to hold the position of an enumeration. And usage of variable @s1@ will be converted to return one of its attributes: label, value, or position, concerning the @Unification@ rule
121
122% \section{Unification and Resolution (this implementation will probably not be used, safe as reference for now)}
123
124% \begin{cfa}
125% enum Colour( char * ) { Red = "R", Green = "G", Blue = "B"  };
126% \end{cfa}
127% The @EnumInstType@ is convertible to other types.
128% A \CFA enumeration expression is implicitly \emph{overloaded} with its three different attributes: value, position, and label.
129% The \CFA compilers need to resolve an @EnumInstType@ as one of its attributes based on the current context.
130
131% \begin{cfa}[caption={Null Context}, label=lst:null_context]
132% {
133%       Colour.Green;
134% }
135% \end{cfa}
136% In example~\ref{lst:null_context}, the environment gives no information to help with the resolution of @Colour.Green@.
137% In this case, any of the attributes is resolvable.
138% According to the \textit{precedence rule}, the expression with @EnumInstType@ resolves as @value( Colour.Green )@.
139% The @EnumInstType@ is converted to the type of the value, which is statically known to the compiler as @char *@.
140% When the compilation reaches the code generation, the compiler outputs code for type @char *@ with the value @"G"@.
141% \begin{cfa}[caption={Null Context Generated Code}, label=lst:null_context]
142% {
143%       "G";
144% }
145% \end{cfa}
146% \begin{cfa}[caption={int Context}, label=lst:int_context]
147% {
148%       int g = Colour.Green;
149% }
150% \end{cfa}
151% The assignment expression gives a context for the EnumInstType resolution.
152% The EnumInstType is used as an @int@, and \CFA needs to determine which of the attributes can be resolved as an @int@ type.
153% The functions $Unify( T1, T2 ): bool$ take two types as parameters and determine if one type can be used as another.
154% In example~\ref{lst:int_context}, the compiler is trying to unify @int@ and @EnumInstType@ of @Colour@.
155% $$Unification( int, EnumInstType<Colour> )$$ which turns into three Unification call
156% \begin{cfa}[label=lst:attr_resolution_1]
157% {
158%       Unify( int, char * ); // unify with the type of value
159%       Unify( int, int ); // unify with the type of position
160%       Unify( int, char * ); // unify with the type of label
161% }
162% \end{cfa}
163% \begin{cfa}[label=lst:attr_resolution_precedence]
164% {
165%       Unification( T1, EnumInstType<T2> ) {
166%               if ( Unify( T1, T2 ) ) return T2;
167%               if ( Unify( T1, int ) ) return int;
168%               if ( Unify( T1, char * ) ) return char *;
169%               Error: Cannot Unify T1 with EnumInstType<T2>;
170%       }
171% }
172% \end{cfa}
173% After the unification, @EnumInstType@ is replaced by its attributes.
174
175% \begin{cfa}[caption={Unification Functions}, label=lst:unification_func_call]
176% {
177%       T2 foo ( T1 ); // function take variable with T1 as a parameter
178%       foo( EnumInstType<T3> ); // Call foo with a variable has type EnumInstType<T3>
179%       >>>> Unification( T1, EnumInstType<T3> )
180% }
181% \end{cfa}
182% % The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType.
183% Backward conversion:
184% \begin{cfa}[caption={Unification Functions}, label=lst:unification_func_call]
185% {
186%       enum Colour colour = 1;
187% }
188% \end{cfa}
189
190% \begin{cfa}[caption={Unification Functions}, label=lst:unification_func_call]
191% {
192%       Unification( EnumInstType<Colour>, int ) >>> label
193% }
194% \end{cfa}
195% @int@ can be unified with the label of Colour.
196% @5@ is a constant expression $\Rightarrow$ Compiler knows the value during the compilation $\Rightarrow$ turns it into
197% \begin{cfa}
198% {
199%       enum Colour colour = Colour.Green;
200% }
201% \end{cfa}
202% Steps:
203% \begin{enumerate}
204% \item
205% identify @1@ as a constant expression with type @int@, and the value is statically known as @1@
206% \item
207% @unification( EnumInstType<Colour>, int )@: @position( EnumInstType< Colour > )@
208% \item
209% return the enumeration constant at position 1
210% \end{enumerate}
211% \begin{cfa}
212% {
213%       enum T (int) { ... } // Declaration
214%       enum T t = 1;
215% }
216% \end{cfa}
217% Steps:
218% \begin{enumerate}
219% \item
220% identify @1@ as a constant expression with type @int@, and the value is statically known as @1@
221% \item
222% @unification( EnumInstType<Colour>, int )@: @value( EnumInstType< Colour > )@
223% \item
224% return the FIRST enumeration constant that has the value 1, by searching through the values array
225% \end{enumerate}
226% 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
227
228% \section{Casting}
229% Casting an EnumInstType to some other type T works similarly to unify the EnumInstType with T. For example:
230% \begin{cfa}
231% enum( int ) Foo { A = 10, B = 100, C = 1000 };
232% (int) Foo.A;
233% \end{cfa}
234% 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.
235
236% \section{Value Conversion}
237% As discussed in section~\ref{lst:var_declaration}, \CFA only saves @position@ as the necessary information. It is necessary for \CFA to generate intermediate code to retrieve other attributes.
238
239% \begin{cfa}
240% Foo a; // int a;
241% int j = a;
242% char * s = a;
243% \end{cfa}
244% Assume stores a value x, which cannot be statically determined. When assigning a to j in line 2, the compiler @Unify@ j with a, and returns @value( a )@. The generated code for the second line will be
245% \begin{cfa}
246% int j = value( Foo, a )
247% \end{cfa}
248% Similarly, the generated code for the third line is
249% \begin{cfa}
250% char * j = label( Foo, a )
251% \end{cfa}
252
253
254\section{Enumerator Initialization}
255
256An enumerator must have a deterministic immutable value, either be explicitly initialized in the enumeration definition, or implicitly initialized by rules.
257
258
259\section{C Enumeration Rule}
260
261A 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$.
262
263
264\section{Auto Initialization}
265
266C auto-initialization works for the integral type @int@ with constant expressions.
267\begin{cfa}
268enum Alphabet ! {
269        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,
270        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
271};
272\end{cfa}
273The complexity of the constant expression depends on the level of runtime computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary.
274
275The notion of auto-initialization can be generalized in \CFA through the trait @AutoInitializable@.
276\begin{cfa}
277forall(T) @trait@ AutoInitializable {
278        void ?{}( T & o, T v );                         $\C{// initialization}$
279        void ?{}( T & t, zero_t );                      $\C{// 0}$
280        T ?++( T & t);                                          $\C{// increment}$
281};
282\end{cfa}
283In addition, there is an implicit enumeration counter, @ecnt@ of type @T@, managed by the compiler.
284For example, the type @Odd@ satisfies @AutoInitializable@:
285\begin{cfa}
286struct Odd { int i; };
287void ?{}( Odd & o, int v ) { if ( v & 1 ) o.i = v; else /* error not odd */ ; };
288void ?{}( Odd & o, zero_t ) { o.i = 1; };
289Odd ?++( Odd o ) { return (Odd){ o.i + 2 }; };
290\end{cfa}
291and implicit initialization is available.
292\begin{cfa}
293enum( Odd ) { A, B, C = 7, D };                 $\C{// 1, 3, 7, 9}$
294\end{cfa}
295where the compiler performs the following transformation and runs the code.
296\begin{cfa}
297enum( Odd ) {
298        ?{}( ecnt, @0@ }  ?{}( A, ecnt },       ?++( ecnt )  ?{}( B, ecnt ),
299        ?{}( ecnt, 7 )  ?{}( C, ecnt ), ?++( ecnt )  ?{}( D, ecnt )
300};
301\end{cfa}
302
303Unfortunately, auto-initialization is not implemented because \CFA is only a transpiler, relying on generated C code to perform the detail work.
304C does not have the equivalent of \CC \lstinline[language={[GNU]C++}]{constexpr}, and it is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler.
305Nevertheless, the necessary language concepts exist to support this feature.
306
307
308\section{Enumeration Features}
309
310
311\section{Iteration and Range}
312
313It is convenient to iterate over a \CFA enumeration value, \eg:
314\begin{cfa}[label=lst:range_functions]
315for ( Alphabet alph; Alphabet ) { sout | alph; }
316>>> A B C ... D
317\end{cfa}
318The for-loop uses the enumeration type @Alphabet@ its range, and iterates through all enumerators in the order defined in the enumeration.
319@alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule.
320
321\textbullet\ \CFA offers a shorthand for iterating all enumeration constants:
322\begin{cfa}[label=lst:range_functions]
323for ( Alphabet alph ) { sout | alph; }
324>>> A B C ... D
325\end{cfa}
326
327The following are examples for constructing for-control using an enumeration. Note that the type declaration of the iterating variable is optional, because \CFA can infer the type as EnumInstType based on the range expression, and possibly convert it to one of its attribute types.
328
329\textbullet\ H is implicit up-to exclusive range [0, H).
330\begin{cfa}[label=lst:range_function_1]
331for ( alph; Alphabet.D ) { sout | alph; }
332>>> A B C
333\end{cfa}
334
335\textbullet\ ~= H is implicit up-to inclusive range [0,H].
336\begin{cfa}[label=lst:range_function_2]
337for ( alph; ~= Alphabet.D ) { sout | alph; }
338>>> A B C D
339\end{cfa}
340
341\textbullet\ L ~ H is explicit up-to exclusive range [L,H).
342\begin{cfa}[label=lst:range_function_3]
343for ( alph; Alphabet.B ~ Alphabet.D  ) { sout | alph; }
344// for ( Alphabet alph = Alphabet.B; alph < Alphabet.D; alph += 1  ); 1 is one_t
345>>> B C
346\end{cfa}
347
348\textbullet\ L ~= H is explicit up-to inclusive range [L,H].
349\begin{cfa}[label=lst:range_function_4]
350for ( alph; Alphabet.B ~= Alphabet.D  ) { sout | alph; }
351>>> B C D
352\end{cfa}
353
354\textbullet\ L -~ H is explicit down-to exclusive range [H,L), where L and H are implicitly interchanged to make the range down-to.
355\begin{cfa}[label=lst:range_function_5]
356for ( alph; Alphabet.D -~ Alphabet.B  ) { sout | alph; }
357>>> D C
358\end{cfa}
359
360\textbullet\ L -~= H is explicit down-to exclusive range [H,L], where L and H are implicitly interchanged to make the range down-to.
361\begin{cfa}[label=lst:range_function_6]
362for ( alph; Alphabet.D -~= Alphabet.B  ) { sout | alph; }
363>>> D C B
364\end{cfa}
365
366A user can specify the ``step size'' of an iteration. There are two different stepping schemes of enumeration for-loop.
367\begin{cfa}[label=lst:range_function_stepping]
368enum(int) Sequence { A = 10, B = 12, C = 14, D = 16, D  = 18 };
369for ( s; Sequence.A ~= Sequence.D ~ 1  ) { sout | alph; }
370>>> 10 12 14 16 18
371for ( s; Sequence.A ~= Sequence.D; s+=1  ) { sout | alph; }
372>>> 10 11 12 13 14 15 16 17 18
373\end{cfa}
374The 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
375\begin{cfa}[label=lst:range_function_stepping_converted]
376for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1  ) { sout | alph; }
377>>> 10 11 12 13 14 15 16 17 18
378\end{cfa}
379
380% \PAB{Explain what each loop does.}
381
382It is also possible to iterate over an enumeration's labels, implicitly or explicitly:
383\begin{cfa}[label=lst:range_functions_label_implicit]
384for ( char * alph; Alphabet )
385\end{cfa}
386This 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.
387If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration.
388\begin{cfa}[label=lst:range_functions_label_implicit]
389for ( char * ch; labels( Alphabet ) )
390\end{cfa}
391
392
393% \section{Non-uniform Type}
394% TODO: Working in Progress, might need to change other sections. Conflict with the resolution right now.
395
396% \begin{cfa}
397% enum T( int, char * ) {
398%        a=42, b="Hello World"
399% };
400% \end{cfa}
401% The enum T declares two different types: int and char *. The enumerators of T hold values of one of the declared types.
402
403\section{Enumeration Inheritance}
404
405\begin{cfa}[label=lst:EnumInline]
406enum( char * ) Name { Jack = "Jack", Jill = "Jill" };
407enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
408\end{cfa}
409\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.
410\begin{cfa}[label=lst:EnumInline]
411Name Fred;
412void f( Name2 );
413f( Fred );
414\end{cfa}
415If enumeration A declares @inline B@ in its enumeration body, enumeration A is the "inlining enum" and enumeration B is the "inlined enum".
416
417An 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.
418\begin{cfa}[label=lst:EnumInline]
419enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" };
420// is equivalent to enum Name2 { Jack = "Jack", Jill="Jill", Sue = "Sue", Tom = "Tom" };
421\end{cfa}
422Name.Jack is equivalent to Name2.Jack. Their attributes are all identical. Opening both Name and Name2 in the same scope will not introduce ambiguity.
423\begin{cfa}[label=lst:EnumInline]
424with( Name, Name2 ) { Jack; } // Name.Jack and Name2.Jack are equivalent. No ambiguity
425\end{cfa}
426
427\section{Implementation}
428
429\section{Static Attribute Expression}
430\begin{cfa}[label=lst:static_attr]
431enum( char * ) Colour {
432        Red = "red", Blue = "blue", Green = "green"
433};
434\end{cfa}
435An 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.
436
437\section{Runtime Attribute Expression and Weak Referenced Data}
438\begin{cfa}[label=lst:dynamic_attr]
439Colour c;
440...
441value( c ); // or c
442\end{cfa}
443An 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.
444
445\CFA stores the variables and labels in @const@ arrays to provide runtime lookup for enumeration information.
446
447\begin{cfa}[label=lst:attr_array]
448const char * Colour_labels [3] = { "Red", "Blue", "Green" };
449const char * Colour_values [3] = { "red", "blue", "green" };
450\end{cfa}
451The \CFA compiles transforms the attribute expressions into array access.
452\begin{cfa}[label=lst:attr_array_access]
453position( c ) // c; an integer
454value( c ); // Colour_values[c]
455label( c ); // Colour_labels[c]
456\end{cfa}
457
458To 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.
459
460\section{Enum Prelude}
461
462\begin{cfa}[label=lst:enum_func_dec]
463forall( T ) {
464        unsigned position( unsigned );
465        T value( unsigned );
466        char * label( unsigned );
467}
468\end{cfa}
469\CFA loads the declaration of enumeration function from the enum.hfa.
470
471\section{Internal Representation}
472
473The definition of an enumeration is represented by an internal type called @EnumDecl@. At the minimum, it stores all the information needed to construct the companion object. Therefore, an @EnumDecl@ can be represented as the following:
474\begin{cfa}[label=lst:EnumDecl]
475forall(T)
476class EnumDecl {
477        T* values;
478        char** label;
479};
480\end{cfa}
481
482The internal representation of an enumeration constant is @EnumInstType@.
483An @EnumInstType@ has a reference to the \CFA-enumeration declaration and the position of the enumeration constant.
484\begin{cfa}[label=lst:EnumInstType]
485class EnumInstType {
486        EnumDecl enumDecl;
487        int position;
488};
489\end{cfa}
490In the later discussion, we will use @EnumDecl<T>@ to symbolize a @EnumDecl@ parameterized by type T, and @EnumInstType<T>@ is a declared instance of @EnumDecl<T>@.
491
492\begin{cfa}[caption={Enum Type Functions}, label=lst:cforall_enum_data]
493const T * const values;
494const char * label;
495int length;
496\end{cfa}
497Companion data are necessary information to represent an enumeration. They are stored as standalone pieces, rather than a structure. Those data will be loaded "on demand".
498Companion data are needed only if the according pseudo-functions are called. For example, the value of the enumeration Workday is loaded only if there is at least one compilation that has call $value(Workday)$. Once the values are loaded, all compilations share these values array to reduce memory usage.
499
500
501% \section{(Rework) Companion Object and Companion Function}
502
503% \begin{cfa}[caption={Enum Type Functions}, label=lst:cforall_enum_functions]
504% forall( T )
505% struct Companion {
506%       const T * const values;
507%                const char * label;
508%       int length;
509% };
510% \end{cfa}
511% \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@.
512
513% The companion object is singleton across the compilation (investigation).
514
515% \CFA generates the definition of companion functions.
516% Because \CFA implicitly stores an enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed.
517% Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively.
518% \begin{cfa}[label=lst:companion_definition]
519% int position( Companion o, int pos ) { return pos; }
520% T value( Companion o, int pos ) { return o.values[ pos ]; }
521% char * label( Companion o, int pos ) { return o.labels[ pos ]; }
522% \end{cfa}
523% Notably, the @Companion@ structure definition, and all companion objects, are visible to users.
524% 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@
525% \begin{cfa}[label=lst:companion_definition_values_labels]
526% Colour.values; // read the Companion's values
527% values( Colour ); // same as Colour.values
528% \end{cfa}
529
530\section{Companion Traits (experimental)}
531Not sure its semantics yet, and it might replace a companion object.
532\begin{cfa}[label=lst:companion_trait]
533forall(T1) {
534        trait Companion(otype T2<otype T1>) {
535                T1 value((otype T2<otype T1> const &);
536                int position(otype T2<otype T1> const &);
537                char * label(otype T2<otype T1> const &);
538        }
539}
540\end{cfa}
541All enumerations implicitly implement the Companion trait, an interface to access attributes. The Companion can be a data type because it fulfills to requirements to have concrete instances, which are:
542
543\begin{enumerate}
544  \item The instance of enumeration has a single polymorphic type.
545  \item Each assertion should use the type once as a parameter.
546\end{enumerate}
547
548\begin{cfa}
549enum(int) Weekday {
550        Monday=10, Tuesday, ...
551};
552
553T value( enum Weekday<T> & this);
554int position( enum Weekday<T> & this )
555char * label( enum Weekday<T> & this )
556
557trait Companion obj = (enum(int)) Workday.Weekday;
558value(obj); // 10
559\end{cfa}
560The enumeration comes with default implementation to the Companion traits functions. The usage of Companion functions would make \CFA allocates and initializes the necessary companion arrays, and return the data at the position represented by the enumeration.
561(...)
562
563\section{User Define Enumeration Functions}
564
565Companion objects make extending features for \CFA enumeration easy.
566\begin{cfa}[label=lst:companion_user_definition]
567char * charastic_string( Companion o, int position ) {
568        return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) );
569}
570printf( charactic_string ( Color, 1 ) );
571>>> Label: Green; Value: G
572\end{cfa}
573Defining a function takes a Companion object effectively defines functions for all \CFA enumeration.
574
575The \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.
576Therefore, a user can use the syntax with a user-defined enumeration function call:
577\begin{cfa}[label=lst:companion_user_definition]
578charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 )
579>>> Label: Green; Value: G
580\end{cfa}
581Similarly, the user can work with the enumeration type itself: (see section ref...)
582\begin{cfa}[ label=lst:companion_user_definition]
583void print_enumerators ( Companion o ) {
584        for ( c : Companion o ) {
585                sout | label (c) | value( c ) ;
586        }
587}
588print_enumerators( Colour );
589\end{cfa}
590
591
592\section{Declaration}
593
594The qualified enumeration syntax is dedicated to \CFA enumeration.
595\begin{cfa}[label=lst:range_functions]
596enum (type_declaration) name { enumerator = const_expr, enumerator = const_expr, ... }
597\end{cfa}
598A compiler stores the name, the underlying type, and all enumerators in an @enumeration table@.
599During the $Validation$ pass, the compiler links the type declaration to the type's definition.
600It 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.
601If the declared type is not @AutoInitializable@, \CFA rejects the enumeration definition.
602Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section)
603
604\begin{cfa}[label=lst:init]
605struct T { ... };
606void ?{}( T & t, zero_t ) { ... };
607void ?{}( T & t, one_t ) { ... };
608T ?+?( T & lhs, T & rhs ) { ... };
609
610enum (T) Sample {
611        Zero: 0 /* zero_t */,
612        One: Zero + 1 /* ?+?( Zero, one_t ) */ , ...
613};
614\end{cfa}
615Challenge: \\
616The value of an enumerator, or the initializer, requires @const_expr@.
617While 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.
618Might not be able to implement a \emph{correct} static check.
619
620\CFA $autogens$ a Companion object for the declared enumeration.
621\begin{cfa}[label=lst:companion]
622Companion( T ) Sample {
623        .values: { 0, 0+1, 0+1+1, 0+1+1+1, ... }, /* 0: zero_t, 1: one_t, +: ?+?{} */
624        .labels: { "Zero", "One", "Two", "Three", ...},
625        .length: /* number of enumerators */
626};
627\end{cfa}
628\CFA stores values as intermediate expressions because the result of the function call to the function @?+?{}(T&, T&)@ is statically unknown to \CFA.
629But the result is computed at run time, and the compiler ensures the @values@ are not changed.
630
631\section{Qualified Expression}
632
633\CFA uses qualified expression to address the scoping of \CFA-enumeration.
634\begin{cfa}[label=lst:qualified_expression]
635aggregation_name.field;
636\end{cfa}
637The qualified expression is not dedicated to \CFA enumeration.
638It is a feature that is supported by other aggregation in \CFA as well, including a C enumeration.
639When C enumerations are unscoped, the qualified expression syntax still helps to disambiguate names in the context.
640\CFA recognizes if the expression references a \CFA aggregation by searching the presence of @aggregation_name@ in the \CFA enumeration table.
641If the @aggregation_name@ is identified as a \CFA enumeration, the compiler checks if @field@ presents in the declared \CFA enumeration.
642
643\section{Instance Declaration}
644
645
646\begin{cfa}[label=lst:var_declaration]
647enum Sample s1;
648\end{cfa}
649
650The declaration \CFA-enumeration variable has the same syntax as the C-enumeration. Internally, such a variable will be represented as an EnumInstType.
Note: See TracBrowser for help on using the repository browser.