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} |
---|
95 | An enumeration is a type that defines a list of named constant values in C (and other languages). |
---|
96 | C and \CC use an integral type as the underlying representation of an enumeration. |
---|
97 | \CFA extends C enumerations to allow all basic and custom types for the inner representation. |
---|
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] |
---|
104 | enum Weekday { Monday, Tuesday, Wednesday, Thursday=10, Friday, Saturday, Sunday }; |
---|
105 | $\(\uparrow\)$ $\(\uparrow\)$ |
---|
106 | ${\rm \newterm{enumeration name}}$ ${\rm \newterm{enumerator names}} |
---|
107 | \end{lstlisting} |
---|
108 | The example defines an enumeration type @Weekday@ with ordered enumerators @Monday@, @Tuesday@, @Wednesday@, @Thursday@, @Friday@, @Saturday@ and @Sunday@. |
---|
109 | The successor of @Tuesday@ is @Monday@ and the predecessor of @Tuesday@ is @Wednesday@. |
---|
110 | A C enumeration is an integral type, with consecutive enumerator values assigned by the compiler starting at zero or the next explicitly initialized value by the programmer. |
---|
111 | For example, @Monday@ to @Wednesday@ have values 0--2 implicitly set by the compiler, @Thursday@ is explicitly set to @10@ by the programmer, and @Friday@ to @Sunday@ have values 11--13 implicitly set by the compiler. |
---|
112 | |
---|
113 | There 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 | |
---|
124 | The 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 | } |
---|
132 | int j = Wednesday; // ERROR! Wednesday is not declared in this scope |
---|
133 | \end{lstlisting} |
---|
134 | |
---|
135 | \section{\CFA-Style Enum} |
---|
136 | |
---|
137 | A \CFA enumeration is parameterized by a type specifying each enumerator's type. |
---|
138 | \CFA allows any object type for the enumerators, and values assigned to enumerators must be from the declared type. |
---|
139 | \begin{lstlisting}[label=lst:color] |
---|
140 | enum Colour( @char *@ ) { Red = "R", Green = "G", Blue = "B" }; |
---|
141 | \end{lstlisting} |
---|
142 | The type of @Colour@ is @char *@ and each enumerator is initialized with a C string. |
---|
143 | Only types with a defined ordering can be automatically initialized (see Section~\ref{s:AutoInitializable}). |
---|
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 | |
---|
151 | A \CFA-enum can be scoped, meaning the enumerator constants are not projected into the enclosing scope. |
---|
152 | \begin{lstlisting} |
---|
153 | enum Colour( char * ) @!@ { ... }; |
---|
154 | \end{lstlisting} |
---|
155 | where the @'!'@ implies the enumerators are \emph{not} projected. |
---|
156 | The enumerators of a scoped enumeration are accessed using qualifications, 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} |
---|
160 | Colour colour = @Colour.@Red; // qualification |
---|
161 | colour = @Colour.@Blue; |
---|
162 | \end{lstlisting} |
---|
163 | |
---|
164 | \section{Enumeration Pseudo-functions} |
---|
165 | Pseudo-functions are function-like operators that do not result in any run-time computations, i.e., like @sizeof@. Instead, the call to functions will be substituted into other expressions in compilation time. |
---|
166 | |
---|
167 | \subsection{Enumerator Attributes} |
---|
168 | The attributes of an enumerator are accessed by pseudo-functions @position@, @value@, and @label@. |
---|
169 | \begin{lstlisting} |
---|
170 | int green_pos = @position@( Colour.Green ); // 1 |
---|
171 | char * green_value = @value@( Colour.Green ); / "G" |
---|
172 | char * green_label = @label@( Colour.Green ); // "Green" |
---|
173 | \end{lstlisting} |
---|
174 | |
---|
175 | \subsection{enumerate()} |
---|
176 | \begin{lstlisting}[label=lst:c_switch] |
---|
177 | enum(int) C_ENUM { First, Second, Third = First, Fourth }; |
---|
178 | int v(C_ENUM e) { |
---|
179 | switch( e ) { |
---|
180 | case First: return 0; break; |
---|
181 | case Second: return 1; break; |
---|
182 | // case Thrid: return 2; break; |
---|
183 | // case Fourth: return 3; break; |
---|
184 | }; |
---|
185 | }; |
---|
186 | \end{lstlisting} |
---|
187 | In the @C_ENUM@ example, @Third@ is an alias of @First@ and @Fourth@ is an alias of @Second@. Programmers cannot make case branches for @Third@ and @Fourth@ because the switch statement matches cases by the enumerator's value. Case First and Third, or Second and Fourth, has duplicate case values. |
---|
188 | |
---|
189 | @enumerate()@ is a pseudo-function that makes the switch statement match by an enumerator instead. |
---|
190 | \begin{lstlisting}[label=lst:c_switch_enumerate] |
---|
191 | enum(double) C_ENUM { First, Second, Third = First, Fourth }; |
---|
192 | C_ENUM variable_a = First, variable_b = Second, variable_c = Thrid, variable_d = Fourth; |
---|
193 | int v(C_ENUM e) { |
---|
194 | switch( enumeratate( e ) ) { |
---|
195 | case First: return e; break; |
---|
196 | case Second: return value( e ); break; |
---|
197 | case Thrid: return label( e ); break; |
---|
198 | case Fourth: return position( e ); break; |
---|
199 | }; |
---|
200 | }; |
---|
201 | p(variable_a); // 0 |
---|
202 | p(variable_b); // 1 |
---|
203 | p(variable_c); // "Third" |
---|
204 | p(variable_d); // 3 |
---|
205 | \end{lstlisting} |
---|
206 | |
---|
207 | \section{Enumeration Characteristic} |
---|
208 | |
---|
209 | \subsection{Enumerator Storage} |
---|
210 | |
---|
211 | Although \CFA enumeration captures three different attributes, an enumeration instance does not store all this information. |
---|
212 | The @sizeof@ a \CFA enumeration instance is always 4 bytes, the same size as a C enumeration instance (@sizeof( int )@). |
---|
213 | It comes from the fact that: |
---|
214 | \begin{enumerate} |
---|
215 | \item |
---|
216 | a \CFA enumeration is always statically typed; |
---|
217 | \item |
---|
218 | it is always resolved as one of its attributes regarding real usage. |
---|
219 | \end{enumerate} |
---|
220 | When creating an enumeration instance @colour@ and assigning it with the enumerator @Color.Green@, the compiler allocates an integer variable and stores the position 1. |
---|
221 | The invocations of $positions()$, $value()$, and $label()$ turn into calls to special functions defined in the prelude: |
---|
222 | \begin{lstlisting}[label=lst:companion_call] |
---|
223 | position( green ); |
---|
224 | >>> position( Colour, 1 ) -> int |
---|
225 | value( green ); |
---|
226 | >>> value( Colour, 1 ) -> T |
---|
227 | label( green ); |
---|
228 | >>> label( Colour, 1) -> char * |
---|
229 | \end{lstlisting} |
---|
230 | @T@ represents the type declared in the \CFA enumeration defined and @char *@ in the example. |
---|
231 | These generated functions are $Companion Functions$, they take an $companion$ object and the position as parameters. |
---|
232 | |
---|
233 | \subsection{Enumeration as Value} |
---|
234 | An \CFA enumeration with base type T can be used seamlessly as T. |
---|
235 | \begin{lstlisting}[label=lst:implicit_conversion] |
---|
236 | char * green_value = Colour.Green; // "G" |
---|
237 | // Is equivalent to |
---|
238 | char * green_value = value( Color.Green ); "G" |
---|
239 | \end{lstlisting} |
---|
240 | \CFA recognizes @Colour.Green@ as an Expression with enumeration type. [reference to resolution distance] An enumeration type can be safely converted into its value type T, @char *@ in the example. When assigning @Colour.Green@ to a reference @green_value@, which has type @char *@, the compiler adds the distance between an enumeration and type T, and the distance between type T and @char *@. If the distance is safe, \CFA will replace the expression @Colour.Green@ with @value( Colour.Green )@. |
---|
241 | |
---|
242 | \subsection{Variable Overloading} |
---|
243 | \begin{lstlisting}[label=lst:variable_overload] |
---|
244 | void foo(Colour c) { return value( c ); } |
---|
245 | void bar(char * s) { return s; } |
---|
246 | Colour green = Colour.Green; // "G" |
---|
247 | char * green = "Green"; |
---|
248 | foo( green ); // "G" |
---|
249 | bar( green ); // "Green" |
---|
250 | \end{lstlisting} |
---|
251 | |
---|
252 | \subsection{Function Overloading} |
---|
253 | \begin{lstlisting}[label=lst:function_overload] |
---|
254 | void foo(Colour c) { return "It is an enum"; } |
---|
255 | void foo(char * s) { return "It is a string"; } |
---|
256 | foo( green ); // "It is an enum" |
---|
257 | \end{lstlisting} |
---|
258 | |
---|
259 | As a consequence, the semantics of using \CFA enumeration as a flag for selection is identical to C enumeration. |
---|
260 | |
---|
261 | |
---|
262 | % \section{Enumeration Features} |
---|
263 | |
---|
264 | A trait is a collection of constraints in \CFA that can be used to describe types. |
---|
265 | The \CFA standard library defines traits to categorize types with related enumeration features. |
---|
266 | |
---|
267 | \section{Enumerator Initialization} |
---|
268 | An enumerator must have a deterministic immutable value, either be explicitly initialized in the enumeration definition, or implicitly initialized by rules. |
---|
269 | |
---|
270 | \subsection{C Enumeration Rule} |
---|
271 | A C enumeration has an integral type. If not initialized, the first enumerator implicitly has the integral value 0, and other enumerators have a value equal to its $predecessor + 1$. |
---|
272 | |
---|
273 | \subsection{Auto Initializable} |
---|
274 | \label{s:AutoInitializable} |
---|
275 | |
---|
276 | |
---|
277 | \CFA enumerations have the same rule in enumeration constant initialization. |
---|
278 | However, only \CFA types that have defined traits for @zero_t@, @one_t@, and an addition operator can be automatically initialized by \CFA. |
---|
279 | |
---|
280 | Specifically, a type is auto-initializable only if it satisfies the trait @AutoInitializable@: |
---|
281 | \begin{lstlisting} |
---|
282 | forall(T) |
---|
283 | trait AutoInitializable { |
---|
284 | void ?()( T & t, zero_t ); |
---|
285 | S ?++( T & t); |
---|
286 | }; |
---|
287 | \end{lstlisting} |
---|
288 | An example of a user-defined @AutoInitializable@ is: |
---|
289 | \begin{lstlisting}[label=lst:sample_auto_Initializable] |
---|
290 | struct Odd { int i; }; |
---|
291 | void ?()( Odd & t, zero_t ) { t.i = 1; }; |
---|
292 | Odd ?++( Odd t1 ) { return Odd( t1.i + 2); }; |
---|
293 | \end{lstlisting} |
---|
294 | When the type of an enumeration is @AutoInitializable@, implicit initialization is available. |
---|
295 | \begin{lstlisting}[label=lst:sample_auto_Initializable_usage] |
---|
296 | enum AutoInitUsage(Odd) { |
---|
297 | A, B, C = 7, D |
---|
298 | }; |
---|
299 | \end{lstlisting} |
---|
300 | In the example, no initializer is specified for the first enumeration constant @A@, so \CFA initializes it with the value of @zero_t@, which is 1. |
---|
301 | @B@ and @D@ have the values of their $predecessor++$, where @one_t@ has the value 2. |
---|
302 | Therefore, the enumeration is initialized as follows: |
---|
303 | \begin{lstlisting}[label=lst:sample_auto_Initializable_usage_gen] |
---|
304 | enum AutoInitUsage(Odd) { |
---|
305 | A = 1, B = 3, C = 7, D = 9 |
---|
306 | }; |
---|
307 | \end{lstlisting} |
---|
308 | Note that there is no mechanism to prevent an even value for the direct initialization, such as @C = 6@. |
---|
309 | |
---|
310 | In \CFA, character, integral, float, and imaginary types are all @AutoInitialiable@. |
---|
311 | \begin{lstlisting}[label=lst:letter] |
---|
312 | enum Alphabet( int ) { |
---|
313 | 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, |
---|
314 | 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 |
---|
315 | }; |
---|
316 | print( "%c, %c, %c", Alphabet.F, Alphabet.o, Alphabet.z ); |
---|
317 | >>> F, o, z |
---|
318 | \end{lstlisting} |
---|
319 | \section{Enumeration Features} |
---|
320 | \subsection{Iteration and Range} |
---|
321 | |
---|
322 | It is convenient to iterate over a \CFA enumeration value, e.g.: |
---|
323 | \begin{lstlisting}[label=lst:range_functions] |
---|
324 | for ( Alphabet alph; Alphabet ) { sout | alph; } |
---|
325 | >>> A B C ... D |
---|
326 | \end{lstlisting} |
---|
327 | The for-loop uses the enumeration type @Alphabet@ its range, and iterates through all enumerators in the order defined in the enumeration. |
---|
328 | @alph@ is the iterating enumeration object, which returns the value of an @Alphabet@ in this context according to the precedence rule. |
---|
329 | |
---|
330 | \textbullet\ \CFA offers a shorthand for iterating all enumeration constants: |
---|
331 | \begin{lstlisting}[label=lst:range_functions] |
---|
332 | for ( Alphabet alph ) { sout | alph; } |
---|
333 | >>> A B C ... D |
---|
334 | \end{lstlisting} |
---|
335 | |
---|
336 | The 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. |
---|
337 | |
---|
338 | \textbullet\ H is implicit up-to exclusive range [0, H). |
---|
339 | \begin{lstlisting}[label=lst:range_function_1] |
---|
340 | for ( alph; Alphabet.D ) { sout | alph; } |
---|
341 | >>> A B C |
---|
342 | \end{lstlisting} |
---|
343 | |
---|
344 | \textbullet\ ~= H is implicit up-to inclusive range [0,H]. |
---|
345 | \begin{lstlisting}[label=lst:range_function_2] |
---|
346 | for ( alph; ~= Alphabet.D ) { sout | alph; } |
---|
347 | >>> A B C D |
---|
348 | \end{lstlisting} |
---|
349 | |
---|
350 | \textbullet\ L ~ H is explicit up-to exclusive range [L,H). |
---|
351 | \begin{lstlisting}[label=lst:range_function_3] |
---|
352 | for ( alph; Alphabet.B ~ Alphabet.D ) { sout | alph; } |
---|
353 | // for ( Alphabet alph = Alphabet.B; alph < Alphabet.D; alph += 1 ); 1 is one_t |
---|
354 | >>> B C |
---|
355 | \end{lstlisting} |
---|
356 | |
---|
357 | \textbullet\ L ~= H is explicit up-to inclusive range [L,H]. |
---|
358 | \begin{lstlisting}[label=lst:range_function_4] |
---|
359 | for ( alph; Alphabet.B ~= Alphabet.D ) { sout | alph; } |
---|
360 | >>> B C D |
---|
361 | \end{lstlisting} |
---|
362 | |
---|
363 | \textbullet\ L -~ H is explicit down-to exclusive range [H,L), where L and H are implicitly interchanged to make the range down-to. |
---|
364 | \begin{lstlisting}[label=lst:range_function_5] |
---|
365 | for ( alph; Alphabet.D -~ Alphabet.B ) { sout | alph; } |
---|
366 | >>> D C |
---|
367 | \end{lstlisting} |
---|
368 | |
---|
369 | \textbullet\ L -~= H is explicit down-to exclusive range [H,L], where L and H are implicitly interchanged to make the range down-to. |
---|
370 | \begin{lstlisting}[label=lst:range_function_6] |
---|
371 | for ( alph; Alphabet.D -~= Alphabet.B ) { sout | alph; } |
---|
372 | >>> D C B |
---|
373 | \end{lstlisting} |
---|
374 | |
---|
375 | A user can specify the ``step size'' of an iteration. There are two different stepping schemes of enumeration for-loop. |
---|
376 | \begin{lstlisting}[label=lst:range_function_stepping] |
---|
377 | enum(int) Sequence { A = 10, B = 12, C = 14, D = 16, D = 18 }; |
---|
378 | for ( s; Sequence.A ~= Sequence.D ~ 1 ) { sout | alph; } |
---|
379 | >>> 10 12 14 16 18 |
---|
380 | for ( s; Sequence.A ~= Sequence.D; s+=1 ) { sout | alph; } |
---|
381 | >>> 10 11 12 13 14 15 16 17 18 |
---|
382 | \end{lstlisting} |
---|
383 | The first syntax is stepping to the next enumeration constant, which is the default stepping scheme if not explicitly specified. The second syntax, on the other hand, is to call @operator+=@ @one_type@ on the @value( s )@. Therefore, the second syntax is equivalent to |
---|
384 | \begin{lstlisting}[label=lst:range_function_stepping_converted] |
---|
385 | for ( typeof( value(Sequence.A) ) s=value( Sequence.A ); s <= Sequence.D; s+=1 ) { sout | alph; } |
---|
386 | >>> 10 11 12 13 14 15 16 17 18 |
---|
387 | \end{lstlisting} |
---|
388 | |
---|
389 | % \PAB{Explain what each loop does.} |
---|
390 | |
---|
391 | It is also possible to iterate over an enumeration's labels, implicitly or explicitly: |
---|
392 | \begin{lstlisting}[label=lst:range_functions_label_implicit] |
---|
393 | for ( char * alph; Alphabet ) |
---|
394 | \end{lstlisting} |
---|
395 | This for-loop implicitly iterates every label of the enumeration, because a label is the only valid resolution to the ch with type @char *@ in this case. |
---|
396 | If the value can also be resolved as the @char *@, you might iterate the labels explicitly with the array iteration. |
---|
397 | \begin{lstlisting}[label=lst:range_functions_label_implicit] |
---|
398 | for ( char * ch; labels( Alphabet ) ) |
---|
399 | \end{lstlisting} |
---|
400 | |
---|
401 | |
---|
402 | % \subsection{Non-uniform Type} |
---|
403 | % TODO: Working in Progress, might need to change other sections. Conflict with the resolution right now. |
---|
404 | |
---|
405 | % \begin{lstlisting} |
---|
406 | % enum T( int, char * ) { |
---|
407 | % a=42, b="Hello World" |
---|
408 | % }; |
---|
409 | % \end{lstlisting} |
---|
410 | % The enum T declares two different types: int and char *. The enumerators of T hold values of one of the declared types. |
---|
411 | |
---|
412 | \subsection{Enumeration Inheritance} |
---|
413 | |
---|
414 | \begin{lstlisting}[label=lst:EnumInline] |
---|
415 | enum( char * ) Name { Jack = "Jack", Jill = "Jill" }; |
---|
416 | enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" }; |
---|
417 | \end{lstlisting} |
---|
418 | \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. |
---|
419 | \begin{lstlisting}[label=lst:EnumInline] |
---|
420 | Name Fred; |
---|
421 | void f( Name2 ); |
---|
422 | f( Fred ); |
---|
423 | \end{lstlisting} |
---|
424 | If enumeration A declares @inline B@ in its enumeration body, enumeration A is the "inlining enum" and enumeration B is the "inlined enum". |
---|
425 | |
---|
426 | An enumeration can inline at most one other enumeration. The inline declaration must be placed before the first enumerator of the inlining enum. The inlining enum has all the enumerators from the inlined enum, with the same labels, values, and position. |
---|
427 | \begin{lstlisting}[label=lst:EnumInline] |
---|
428 | enum /* inferred */ Name2 { inline Name, Sue = "Sue", Tom = "Tom" }; |
---|
429 | // is equivalent to enum Name2 { Jack = "Jack", Jill="Jill", Sue = "Sue", Tom = "Tom" }; |
---|
430 | \end{lstlisting} |
---|
431 | Name.Jack is equivalent to Name2.Jack. Their attributes are all identical. Opening both Name and Name2 in the same scope will not introduce ambiguity. |
---|
432 | \begin{lstlisting}[label=lst:EnumInline] |
---|
433 | with( Name, Name2 ) { Jack; } // Name.Jack and Name2.Jack are equivalent. No ambiguity |
---|
434 | \end{lstlisting} |
---|
435 | |
---|
436 | \section{Implementation} |
---|
437 | |
---|
438 | \subsection{Compiler Representation} |
---|
439 | The 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: |
---|
440 | \begin{lstlisting}[label=lst:EnumDecl] |
---|
441 | forall(T) |
---|
442 | class EnumDecl { |
---|
443 | T* values; |
---|
444 | char** label; |
---|
445 | }; |
---|
446 | \end{lstlisting} |
---|
447 | |
---|
448 | The internal representation of an enumeration constant is @EnumInstType@. |
---|
449 | An @EnumInstType@ has a reference to the \CFA-enumeration declaration and the position of the enumeration constant. |
---|
450 | \begin{lstlisting}[label=lst:EnumInstType] |
---|
451 | class EnumInstType { |
---|
452 | EnumDecl enumDecl; |
---|
453 | int position; |
---|
454 | }; |
---|
455 | \end{lstlisting} |
---|
456 | In 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>@. |
---|
457 | |
---|
458 | \begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_data] |
---|
459 | const T * const values; |
---|
460 | const char * label; |
---|
461 | int length; |
---|
462 | \end{lstlisting} |
---|
463 | Companion 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". |
---|
464 | Companion 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. |
---|
465 | |
---|
466 | <Investiage: how to implement this is huge> |
---|
467 | |
---|
468 | \subsection{(Rework) Companion Object and Companion Function} |
---|
469 | |
---|
470 | \begin{lstlisting}[caption={Enum Type Functions}, label=lst:cforall_enum_functions] |
---|
471 | forall( T ) |
---|
472 | struct Companion { |
---|
473 | const T * const values; |
---|
474 | const char * label; |
---|
475 | int length; |
---|
476 | }; |
---|
477 | \end{lstlisting} |
---|
478 | \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@. |
---|
479 | |
---|
480 | The companion object is singleton across the compilation (investigation). |
---|
481 | |
---|
482 | \CFA generates the definition of companion functions. |
---|
483 | Because \CFA implicitly stores an enumeration instance as its position, the companion function @position@ does nothing but return the position it is passed. |
---|
484 | Companions function @value@ and @label@ return the array item at the given position of @values@ and @labels@, respectively. |
---|
485 | \begin{lstlisting}[label=lst:companion_definition] |
---|
486 | int position( Companion o, int pos ) { return pos; } |
---|
487 | T value( Companion o, int pos ) { return o.values[ pos ]; } |
---|
488 | char * label( Companion o, int pos ) { return o.labels[ pos ]; } |
---|
489 | \end{lstlisting} |
---|
490 | Notably, the @Companion@ structure definition, and all companion objects, are visible to users. |
---|
491 | 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@ |
---|
492 | \begin{lstlisting}[label=lst:companion_definition_values_labels] |
---|
493 | Colour.values; // read the Companion's values |
---|
494 | values( Colour ); // same as Colour.values |
---|
495 | \end{lstlisting} |
---|
496 | |
---|
497 | \subsection{Companion Traits (experimental)} |
---|
498 | Not sure its semantics yet, and it might replace a companion object. |
---|
499 | \begin{lstlisting}[label=lst:companion_trait] |
---|
500 | forall(T1) { |
---|
501 | trait Companion(otype T2<otype T1>) { |
---|
502 | T1 value((otype T2<otype T1> const &); |
---|
503 | int position(otype T2<otype T1> const &); |
---|
504 | char * label(otype T2<otype T1> const &); |
---|
505 | } |
---|
506 | } |
---|
507 | \end{lstlisting} |
---|
508 | All 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: |
---|
509 | |
---|
510 | \begin{enumerate} |
---|
511 | \item The instance of enumeration has a single polymorphic type. |
---|
512 | \item Each assertion should use the type once as a parameter. |
---|
513 | \end{enumerate} |
---|
514 | |
---|
515 | \begin{lstlisting} |
---|
516 | enum(int) Weekday { |
---|
517 | Monday=10, Tuesday, ... |
---|
518 | }; |
---|
519 | |
---|
520 | T value( enum Weekday<T> & this); |
---|
521 | int position( enum Weekday<T> & this ) |
---|
522 | char * label( enum Weekday<T> & this ) |
---|
523 | |
---|
524 | trait Companion obj = (enum(int)) Workday.Weekday; |
---|
525 | value(obj); // 10 |
---|
526 | \end{lstlisting} |
---|
527 | The 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. |
---|
528 | (...) |
---|
529 | |
---|
530 | \subsection{User Define Enumeration Functions} |
---|
531 | |
---|
532 | Companion objects make extending features for \CFA enumeration easy. |
---|
533 | \begin{lstlisting}[label=lst:companion_user_definition] |
---|
534 | char * charastic_string( Companion o, int position ) { |
---|
535 | return sprintf( "Label: %s; Value: %s", label( o, position ), value( o, position) ); |
---|
536 | } |
---|
537 | printf( charactic_string ( Color, 1 ) ); |
---|
538 | >>> Label: Green; Value: G |
---|
539 | \end{lstlisting} |
---|
540 | Defining a function takes a Companion object effectively defines functions for all \CFA enumeration. |
---|
541 | |
---|
542 | The \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. |
---|
543 | Therefore, a user can use the syntax with a user-defined enumeration function call: |
---|
544 | \begin{lstlisting}[label=lst:companion_user_definition] |
---|
545 | charactic_string( Color.Green ); // equivalent to charactic_string( Color, 1 ) |
---|
546 | >>> Label: Green; Value: G |
---|
547 | \end{lstlisting} |
---|
548 | Similarly, the user can work with the enumeration type itself: (see section ref...) |
---|
549 | \begin{lstlisting}[ label=lst:companion_user_definition] |
---|
550 | void print_enumerators ( Companion o ) { |
---|
551 | for ( c : Companion o ) { |
---|
552 | sout | label (c) | value( c ) ; |
---|
553 | } |
---|
554 | } |
---|
555 | print_enumerators( Colour ); |
---|
556 | \end{lstlisting} |
---|
557 | |
---|
558 | |
---|
559 | \subsection{Declaration} |
---|
560 | |
---|
561 | The qualified enumeration syntax is dedicated to \CFA enumeration. |
---|
562 | \begin{lstlisting}[label=lst:range_functions] |
---|
563 | enum (type_declaration) name { enumerator = const_expr, enumerator = const_expr, ... } |
---|
564 | \end{lstlisting} |
---|
565 | A compiler stores the name, the underlying type, and all enumerators in an @enumeration table@. |
---|
566 | During the $Validation$ pass, the compiler links the type declaration to the type's definition. |
---|
567 | 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. |
---|
568 | If the declared type is not @AutoInitializable@, \CFA rejects the enumeration definition. |
---|
569 | Otherwise, it attempts to initialize enumerators with the enumeration initialization pattern. (a reference to a future initialization pattern section) |
---|
570 | |
---|
571 | \begin{lstlisting}[label=lst:init] |
---|
572 | struct T { ... }; |
---|
573 | void ?{}( T & t, zero_t ) { ... }; |
---|
574 | void ?{}( T & t, one_t ) { ... }; |
---|
575 | T ?+?( T & lhs, T & rhs ) { ... }; |
---|
576 | |
---|
577 | enum (T) Sample { |
---|
578 | Zero: 0 /* zero_t */, |
---|
579 | One: Zero + 1 /* ?+?( Zero, one_t ) */ , ... |
---|
580 | }; |
---|
581 | \end{lstlisting} |
---|
582 | Challenge: \\ |
---|
583 | The value of an enumerator, or the initializer, requires @const_expr@. |
---|
584 | 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. |
---|
585 | Might not be able to implement a \emph{correct} static check. |
---|
586 | |
---|
587 | \CFA $autogens$ a Companion object for the declared enumeration. |
---|
588 | \begin{lstlisting}[label=lst:companion] |
---|
589 | Companion( T ) Sample { |
---|
590 | .values: { 0, 0+1, 0+1+1, 0+1+1+1, ... }, /* 0: zero_t, 1: one_t, +: ?+?{} */ |
---|
591 | .labels: { "Zero", "One", "Two", "Three", ...}, |
---|
592 | .length: /* number of enumerators */ |
---|
593 | }; |
---|
594 | \end{lstlisting} |
---|
595 | \CFA stores values as intermediate expressions because the result of the function call to the function @?+?{}(T&, T&)@ is statically unknown to \CFA. |
---|
596 | But the result is computed at run time, and the compiler ensures the @values@ are not changed. |
---|
597 | |
---|
598 | \subsection{Qualified Expression} |
---|
599 | |
---|
600 | \CFA uses qualified expression to address the scoping of \CFA-enumeration. |
---|
601 | \begin{lstlisting}[label=lst:qualified_expression] |
---|
602 | aggregation_name.field; |
---|
603 | \end{lstlisting} |
---|
604 | The qualified expression is not dedicated to \CFA enumeration. |
---|
605 | It is a feature that is supported by other aggregation in \CFA as well, including a C enumeration. |
---|
606 | When C enumerations are unscoped, the qualified expression syntax still helps to disambiguate names in the context. |
---|
607 | \CFA recognizes if the expression references a \CFA aggregation by searching the presence of @aggregation_name@ in the \CFA enumeration table. |
---|
608 | If the @aggregation_name@ is identified as a \CFA enumeration, the compiler checks if @field@ presents in the declared \CFA enumeration. |
---|
609 | |
---|
610 | \subsection{\lstinline{with} Clause/Statement} |
---|
611 | Instead of qualifying an enumeration expression every time, the @with@ can be used to expose enumerators to the current scope, making them directly accessible. |
---|
612 | \begin{lstlisting}[label=lst:declaration] |
---|
613 | enum Color( char * ) { Red="R", Green="G", Blue="B" }; |
---|
614 | enum Animal( int ) { Cat=10, Dog=20 }; |
---|
615 | with ( Color, Animal ) { |
---|
616 | char * red_string = Red; // value( Color.Red ) |
---|
617 | int cat = Cat; // value( Animal.Cat ) |
---|
618 | } |
---|
619 | \end{lstlisting} |
---|
620 | The \lstinline{with} might introduce ambiguity to a scope. Consider the example: |
---|
621 | \begin{lstlisting}[label=lst:declaration] |
---|
622 | enum Color( char * ) { Red="R", Green="G", Blue="B" }; |
---|
623 | enum RGB( int ) { Red=0, Green=1, Blue=2 }; |
---|
624 | with ( Color, RGB ) { |
---|
625 | // int red = Red; |
---|
626 | } |
---|
627 | \end{lstlisting} |
---|
628 | \CFA will not try to resolve the expression with ambiguity. It would report an error. In this case, it is necessary to qualify @Red@ even inside of the \lstinline{with} clause. |
---|
629 | |
---|
630 | \subsection{Instance Declaration} |
---|
631 | |
---|
632 | |
---|
633 | \begin{lstlisting}[label=lst:var_declaration] |
---|
634 | enum Sample s1; |
---|
635 | \end{lstlisting} |
---|
636 | |
---|
637 | The declaration \CFA-enumeration variable has the same syntax as the C-enumeration. Internally, such a variable will be represented as an EnumInstType. |
---|
638 | \begin{lstlisting}[label=lst:declaration_code] |
---|
639 | int s1; |
---|
640 | \end{lstlisting} |
---|
641 | The 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, with respect to the @Unification@ rule |
---|
642 | |
---|
643 | \subsection{Unification and Resolution } |
---|
644 | |
---|
645 | |
---|
646 | \begin{lstlisting} |
---|
647 | enum Colour( char * ) { Red = "R", Green = "G", Blue = "B" }; |
---|
648 | \end{lstlisting} |
---|
649 | The @EnumInstType@ is convertible to other types. |
---|
650 | A \CFA enumeration expression is implicitly \emph{overloaded} with its three different attributes: value, position, and label. |
---|
651 | The \CFA compilers need to resolve an @EnumInstType@ as one of its attributes based on the current context. |
---|
652 | |
---|
653 | \begin{lstlisting}[caption={Null Context}, label=lst:null_context] |
---|
654 | { |
---|
655 | Colour.Green; |
---|
656 | } |
---|
657 | \end{lstlisting} |
---|
658 | In example~\ref{lst:null_context}, the environment gives no information to help with the resolution of @Colour.Green@. |
---|
659 | In this case, any of the attributes is resolvable. |
---|
660 | According to the \textit{precedence rule}, the expression with @EnumInstType@ resolves as @value( Colour.Green )@. |
---|
661 | The @EnumInstType@ is converted to the type of the value, which is statically known to the compiler as @char *@. |
---|
662 | When the compilation reaches the code generation, the compiler outputs code for type @char *@ with the value @"G"@. |
---|
663 | \begin{lstlisting}[caption={Null Context Generated Code}, label=lst:null_context] |
---|
664 | { |
---|
665 | "G"; |
---|
666 | } |
---|
667 | \end{lstlisting} |
---|
668 | \begin{lstlisting}[caption={int Context}, label=lst:int_context] |
---|
669 | { |
---|
670 | int g = Colour.Green; |
---|
671 | } |
---|
672 | \end{lstlisting} |
---|
673 | The assignment expression gives a context for the EnumInstType resolution. |
---|
674 | The EnumInstType is used as an @int@, and \CFA needs to determine which of the attributes can be resolved as an @int@ type. |
---|
675 | The functions $Unify( T1, T2 ): bool$ take two types as parameters and determine if one type can be used as another. |
---|
676 | In example~\ref{lst:int_context}, the compiler is trying to unify @int@ and @EnumInstType@ of @Colour@. |
---|
677 | $$Unification( int, EnumInstType<Colour> )$$ which turns into three Unification call |
---|
678 | \begin{lstlisting}[label=lst:attr_resolution_1] |
---|
679 | { |
---|
680 | Unify( int, char * ); // unify with the type of value |
---|
681 | Unify( int, int ); // unify with the type of position |
---|
682 | Unify( int, char * ); // unify with the type of label |
---|
683 | } |
---|
684 | \end{lstlisting} |
---|
685 | \begin{lstlisting}[label=lst:attr_resolution_precedence] |
---|
686 | { |
---|
687 | Unification( T1, EnumInstType<T2> ) { |
---|
688 | if ( Unify( T1, T2 ) ) return T2; |
---|
689 | if ( Unify( T1, int ) ) return int; |
---|
690 | if ( Unify( T1, char * ) ) return char *; |
---|
691 | Error: Cannot Unify T1 with EnumInstType<T2>; |
---|
692 | } |
---|
693 | } |
---|
694 | \end{lstlisting} |
---|
695 | After the unification, @EnumInstType@ is replaced by its attributes. |
---|
696 | |
---|
697 | \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call] |
---|
698 | { |
---|
699 | T2 foo ( T1 ); // function take variable with T1 as a parameter |
---|
700 | foo( EnumInstType<T3> ); // Call foo with a variable has type EnumInstType<T3> |
---|
701 | >>>> Unification( T1, EnumInstType<T3> ) |
---|
702 | } |
---|
703 | \end{lstlisting} |
---|
704 | % The conversion can work backward: in restrictive cases, attributes of can be implicitly converted back to the EnumInstType. |
---|
705 | Backward conversion: |
---|
706 | \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call] |
---|
707 | { |
---|
708 | enum Colour colour = 1; |
---|
709 | } |
---|
710 | \end{lstlisting} |
---|
711 | |
---|
712 | \begin{lstlisting}[caption={Unification Functions}, label=lst:unification_func_call] |
---|
713 | { |
---|
714 | Unification( EnumInstType<Colour>, int ) >>> label |
---|
715 | } |
---|
716 | \end{lstlisting} |
---|
717 | @int@ can be unified with the label of Colour. |
---|
718 | @5@ is a constant expression $\Rightarrow$ Compiler knows the value during the compilation $\Rightarrow$ turns it into |
---|
719 | \begin{lstlisting} |
---|
720 | { |
---|
721 | enum Colour colour = Colour.Green; |
---|
722 | } |
---|
723 | \end{lstlisting} |
---|
724 | Steps: |
---|
725 | \begin{enumerate} |
---|
726 | \item |
---|
727 | identify @1@ as a constant expression with type @int@, and the value is statically known as @1@ |
---|
728 | \item |
---|
729 | @unification( EnumInstType<Colour>, int )@: @position( EnumInstType< Colour > )@ |
---|
730 | \item |
---|
731 | return the enumeration constant at position 1 |
---|
732 | \end{enumerate} |
---|
733 | \begin{lstlisting} |
---|
734 | { |
---|
735 | enum T (int) { ... } // Declaration |
---|
736 | enum T t = 1; |
---|
737 | } |
---|
738 | \end{lstlisting} |
---|
739 | Steps: |
---|
740 | \begin{enumerate} |
---|
741 | \item |
---|
742 | identify @1@ as a constant expression with type @int@, and the value is statically known as @1@ |
---|
743 | \item |
---|
744 | @unification( EnumInstType<Colour>, int )@: @value( EnumInstType< Colour > )@ |
---|
745 | \item |
---|
746 | return the FIRST enumeration constant that has the value 1, by searching through the values array |
---|
747 | \end{enumerate} |
---|
748 | 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 |
---|
749 | |
---|
750 | \subsection{Casting} |
---|
751 | Casting an EnumInstType to some other type T works similarly to unify the EnumInstType with T. For example: |
---|
752 | \begin{lstlisting} |
---|
753 | enum( int ) Foo { A = 10, B = 100, C = 1000 }; |
---|
754 | (int) Foo.A; |
---|
755 | \end{lstlisting} |
---|
756 | 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. |
---|
757 | |
---|
758 | \subsection{Value Conversion} |
---|
759 | 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. |
---|
760 | |
---|
761 | \begin{lstlisting} |
---|
762 | Foo a; // int a; |
---|
763 | int j = a; |
---|
764 | char * s = a; |
---|
765 | \end{lstlisting} |
---|
766 | 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 |
---|
767 | \begin{lstlisting} |
---|
768 | int j = value( Foo, a ) |
---|
769 | \end{lstlisting} |
---|
770 | Similarly, the generated code for the third line is |
---|
771 | \begin{lstlisting} |
---|
772 | char * j = label( Foo, a ) |
---|
773 | \end{lstlisting} |
---|
774 | |
---|
775 | |
---|
776 | \end{document} |
---|
777 | |
---|
778 | % Local Variables: % |
---|
779 | % tab-width: 4 % |
---|
780 | % compile-command: "pdflatex enum.tex" % |
---|
781 | % End: % |
---|