source: doc/theses/jiada_liang_MMath/Cenum.tex @ d3b33d5

Last change on this file since d3b33d5 was 29c8675, checked in by JiadaL <j82liang@…>, 2 months ago

update thesis

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