source: doc/theses/jiada_liang_MMath/CEnum.tex @ dd78dbc

Last change on this file since dd78dbc was dd78dbc, checked in by JiadaL <j82liang@…>, 14 hours ago

Add CEnum.tex

  • Property mode set to 100644
File size: 4.9 KB
Line 
1\chapter{C Enum in CFA}
2
3\CFA supports C enumeration using the same syntax for backwards compatibility, and the semantics is mostly consistent with C with small difference in terms of typing.
4C style enums in \CFA language are called "C enum".
5\CFA also extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages.
6Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
7The following sections detail all of my new contributions to enumerations in \CFA.
8
9\section{Enumerator Visibility}
10\label{s:EnumeratorVisibility}
11
12In C, unscoped enumerators present a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.
13There 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.
14
15The \CFA type-system allows extensive overloading, including enumerators.
16Furthermore, \CFA uses the environment, such as the left-hand of assignment and function arguments, to pinpoint the best overloaded name.
17\VRef[Figure]{f:EnumeratorVisibility} shows enumeration overloading and how qualification and casting are used to disambiguate ambiguous situations.
18\CFA overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
19Experience from \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names.
20That is, it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded variables and functions, that requires disambiguation using qualification or casting.
21
22\begin{figure}
23\begin{cfa}
24enum E1 { First, Second, Third, Fourth };
25enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$
26E1 f() { return Third; }                                $\C{// overloaded functions, different return types}$
27E2 f() { return Fourth; }
28void g( E1 e );
29void h( E2 e );
30void 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
43\CFA overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
44Experience from \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names, \ie it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded variables and functions.
45Any ambiguity can be resolved using qualification or casting.
46
47
48\section{Enumerator Scoping}
49
50An enumeration can be scoped, using @'!'@, so the enumerator constants are not projected into the enclosing scope.
51\begin{cfa}
52enum Week @!@ { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
53enum RGB @!@ { Red, Green, Blue };
54\end{cfa}
55Now the enumerators \emph{must} be qualified with the associated enumeration type.
56\begin{cfa}
57Week week = @Week.@Mon;
58week = @Week.@Sat;
59RGB rgb = @RGB.@Red;
60rgb = @RGB.@Blue;
61\end{cfa}
62{\color{red}@***@}It is possible to toggle back to unscoped using the \CFA @with@ 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:EnumeratorVisibility}, 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
73As in Section~\ref{s:Usage}, C's implicit conversion between enumeration and integral type raises a safety concern.
74\CFA disallows an implicit conversion from integral type to enueration, and conversion between different C enumeration type.
75It loses some degree of its backward compatibility to C, in exchange for type safety.
76\begin{cfa}
77enum Bird { Pengin, Robin, Eagle };
78enum Fish { Shark, Salmon, Whale };
79
80int i = Robin; $\C{// Allow, implicitly converts to 1}$ 
81@enum Bird bird = Shark;@ $\C{// Disallow }$ 
82@enum Bird bird = 1;@  $\C{// Disallow }$ 
83
84@enum Bird bird = (Bird) Shark@
85@enum Bird bird = (Bird) 1;@
86\end{cfa}
87As a workaround, \CFA allows explicit cast to an enumeration, turning an integral type to an enumeration that can be used in assignment or function argument,
88in which case \CFA treats C enuemration as its underlying integral type. In such cases, it is up to user to ensure program correctness.
Note: See TracBrowser for help on using the repository browser.