| [9d3a4cc] | 1 | \chapter{C Enumeration in \texorpdfstring{\CFA}{Cforall}} | 
|---|
| [dd78dbc] | 2 |  | 
|---|
| [c4aca65] | 3 | \CFA supports legacy C enumeration using the same syntax for backward compatibility. | 
|---|
| [a57ad8a] | 4 | A C-style enumeration in \CFA is called a \newterm{C Enum}. | 
|---|
| [0bda8d7] | 5 | The semantics of the C Enum are mostly consistent with C with some restrictions. | 
|---|
| [94643698] | 6 | The following sections detail all of my new contributions to enumerations in C. | 
|---|
| [a57ad8a] | 7 |  | 
|---|
| [dd78dbc] | 8 |  | 
|---|
| [0c51c8b4] | 9 | \section{Visibility} | 
|---|
|  | 10 | \label{s:CVisibility} | 
|---|
| [dd78dbc] | 11 |  | 
|---|
|  | 12 | In C, unscoped enumerators present a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names. | 
|---|
| [a57ad8a] | 13 | \begin{cfa} | 
|---|
|  | 14 | enum E1 { First, Second, Third, Fourth }; | 
|---|
|  | 15 | enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$ | 
|---|
|  | 16 | \end{cfa} | 
|---|
| [c1c0efdb] | 17 | There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system include-files. | 
|---|
| [dd78dbc] | 18 |  | 
|---|
| [29c8675] | 19 | The \CFA type-system allows extensive overloading, including enumerators. For example, enumerator First from E1 can exist at the scope as First from E2. | 
|---|
| [a57ad8a] | 20 | Hence, most ambiguities among C enumerators are implicitly resolved by the \CFA type system, possibly without any programmer knowledge of the conflict. | 
|---|
| [c1c0efdb] | 21 | In addition, C Enum qualification is added, exactly like aggregate field-qualification, to disambiguate. | 
|---|
| [a57ad8a] | 22 | \VRef[Figure]{f:EnumeratorVisibility} shows how resolution, qualification, and casting are used to disambiguate situations for enumerations @E1@ and @E2@. | 
|---|
| [dd78dbc] | 23 |  | 
|---|
|  | 24 | \begin{figure} | 
|---|
|  | 25 | \begin{cfa} | 
|---|
| [c141c09] | 26 | E1 f() { return Third; }                                $\C{// overload functions with different return types}$ | 
|---|
| [dd78dbc] | 27 | E2 f() { return Fourth; } | 
|---|
|  | 28 | void g( E1 e ); | 
|---|
|  | 29 | void h( E2 e ); | 
|---|
|  | 30 | void foo() {                                                    $\C{// different resolutions and dealing with ambiguities}$ | 
|---|
|  | 31 | E1 e1 = First;   E2 e2 = First;         $\C{// initialization}$ | 
|---|
|  | 32 | e1 = Second;   e2 = Second;                     $\C{// assignment}$ | 
|---|
|  | 33 | e1 = f();   e2 = f();                           $\C{// function return}$ | 
|---|
|  | 34 | g( First );   h( First );                       $\C{// function argument}$ | 
|---|
|  | 35 | int i = @E1.@First + @E2.@First;        $\C{// disambiguate with qualification}$ | 
|---|
|  | 36 | int j = @(E1)@First + @(E2)@First;      $\C{// disambiguate with cast}$ | 
|---|
|  | 37 | } | 
|---|
|  | 38 | \end{cfa} | 
|---|
|  | 39 | \caption{Enumerator Visibility and Disambiguating} | 
|---|
|  | 40 | \label{f:EnumeratorVisibility} | 
|---|
|  | 41 | \end{figure} | 
|---|
|  | 42 |  | 
|---|
| [dcfcf368] | 43 | Aside, name shadowing in \CFA only happens when a name has been redefined with the \emph{exact} same type. | 
|---|
|  | 44 | Because an enumeration define its type and enumerators in one definition, shadowing an enumerator is not possible, \ie it is impossible to have another @First@ with same type @E1@. | 
|---|
|  | 45 |  | 
|---|
| [dd78dbc] | 46 |  | 
|---|
| [0c51c8b4] | 47 | \section{Scoping} | 
|---|
| [dd78dbc] | 48 |  | 
|---|
| [a57ad8a] | 49 | A C Enum can be scoped, using @'!'@, so the enumerator constants are not projected into the enclosing scope. | 
|---|
| [dd78dbc] | 50 | \begin{cfa} | 
|---|
|  | 51 | enum Week @!@ { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun }; | 
|---|
|  | 52 | enum RGB @!@ { Red, Green, Blue }; | 
|---|
|  | 53 | \end{cfa} | 
|---|
| [0bda8d7] | 54 | Now, the enumerators \emph{must} be qualified with the associated enumeration type. | 
|---|
| [dd78dbc] | 55 | \begin{cfa} | 
|---|
|  | 56 | Week week = @Week.@Mon; | 
|---|
|  | 57 | week = @Week.@Sat; | 
|---|
|  | 58 | RGB rgb = @RGB.@Red; | 
|---|
|  | 59 | rgb = @RGB.@Blue; | 
|---|
|  | 60 | \end{cfa} | 
|---|
| [94643698] | 61 | % with feature unimplemented | 
|---|
| [29c8675] | 62 | It is possible to introduce enumerators from a scoped enumeration to a block scope using the \CFA @with@ auto-qualification clause/statement (see also \CC \lstinline[language=c++]{using enum} in Section~\ref{s:C++RelatedWork}). | 
|---|
| [dd78dbc] | 63 | \begin{cfa} | 
|---|
|  | 64 | with ( @Week@, @RGB@ ) {                                $\C{// type names}$ | 
|---|
|  | 65 | week = @Sun@;                                          $\C{// no qualification}$ | 
|---|
|  | 66 | rgb = @Green@; | 
|---|
|  | 67 | } | 
|---|
|  | 68 | \end{cfa} | 
|---|
| [0c51c8b4] | 69 | As in Section~\ref{s:CVisibility}, opening multiple scoped enumerations in a @with@ can result in duplicate enumeration names, but \CFA implicit type resolution and explicit qualification/casting handle this localized scenario. | 
|---|
| [dd78dbc] | 70 |  | 
|---|
| [dcfcf368] | 71 |  | 
|---|
| [dd78dbc] | 72 | \section{Type Safety} | 
|---|
| [c1c0efdb] | 73 | \label{s:TypeSafety} | 
|---|
| [dd78dbc] | 74 |  | 
|---|
| [c141c09] | 75 | As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern. | 
|---|
| [a57ad8a] | 76 | In \CFA, the conversion is changed to unidirectional: an enumeration can be implicitly converted into an integral type, with an associated @safe@ conversion cost. | 
|---|
| [c4aca65] | 77 | However, an integral type cannot be implicitly converted into a C enumeration because the conversion cost is set to @infinity@. | 
|---|
| [dd78dbc] | 78 | \begin{cfa} | 
|---|
| [a57ad8a] | 79 | enum Bird { Penguin, Robin, Eagle }; | 
|---|
| [dd78dbc] | 80 | enum Fish { Shark, Salmon, Whale }; | 
|---|
|  | 81 |  | 
|---|
| [94643698] | 82 | int i = Robin;                                                  $\C{// allow, implicitly converts to 1}$ | 
|---|
|  | 83 | enum Bird @bird = 1;@                                   $\C{// disallow }$ | 
|---|
|  | 84 | enum Bird @bird = Shark;@                               $\C{// disallow }$ | 
|---|
| [a57ad8a] | 85 | \end{cfa} | 
|---|
|  | 86 | It is now up to the programmer to insert an explicit cast to force the assignment. | 
|---|
|  | 87 | \begin{cfa} | 
|---|
|  | 88 | enum Bird bird = @(Bird)@1; | 
|---|
| [94643698] | 89 | enum Bird bird = @(Bird)@Shark | 
|---|
| [a57ad8a] | 90 | \end{cfa} | 
|---|
|  | 91 |  | 
|---|
| [508cff0] | 92 | Note, \CC has the same safe restriction and provides the same workaround cast: | 
|---|
|  | 93 | \begin{cquote} | 
|---|
|  | 94 | \begin{description}[leftmargin=*,topsep=0pt,itemsep=0pt,parsep=0pt] | 
|---|
| [a57ad8a] | 95 | \item[Change:] \CC objects of enumeration type can only be assigned values of the same enumeration type. | 
|---|
|  | 96 | In C, objects of enumeration type can be assigned values of any integral type. | 
|---|
|  | 97 | Example: | 
|---|
|  | 98 | \begin{cfa} | 
|---|
|  | 99 | enum color { red, blue, green }; | 
|---|
|  | 100 | color c = 1;                            $\C{// valid C, invalid \CC}$ | 
|---|
| [c141c09] | 101 | \end{cfa} | 
|---|
| [a57ad8a] | 102 | \item[Rationale:] The type-safe nature of \CC. | 
|---|
|  | 103 | \item[Effect on original feature:] Deletion of semantically well-defined feature. | 
|---|
|  | 104 | \item[Difficulty of converting:] Syntactic transformation. (The type error produced by the assignment can be | 
|---|
|  | 105 | automatically corrected by applying an explicit cast.) | 
|---|
|  | 106 | \item[How widely used:] Common. | 
|---|
|  | 107 | \end{description} | 
|---|
| [508cff0] | 108 | \hfill ISO/IEC 14882:1998 (\CC Programming Language Standard)~\cite[C.1.5.7.2.5]{ANSI98:C++} | 
|---|
|  | 109 | \end{cquote} | 
|---|