1 | \chapter{Background} |
---|
2 | \lstnewenvironment{clang}[1][]{\lstset{language=[ANSI]C,escapechar=\$,moredelim=**[is][\color{red}]{@}{@},}\lstset{#1}}{} |
---|
3 | |
---|
4 | \CFA is a backwards-compatible extension of the C programming language. |
---|
5 | Therefore, it must support C-style enumerations and any enumeration extensions must be intuitive to C programmers both in syntax and semantics. |
---|
6 | |
---|
7 | It is common for C programmers to ``believe'' there are three equivalent forms of named constants. |
---|
8 | \begin{clang} |
---|
9 | #define Mon 0 |
---|
10 | static const int Mon = 0; |
---|
11 | enum { Mon }; |
---|
12 | \end{clang} |
---|
13 | \begin{enumerate}[leftmargin=*] |
---|
14 | \item |
---|
15 | For @#define@, the programmer has to explicitly manage the constant name and value. |
---|
16 | Furthermore, these C preprocessor macro names are outside of the C type-system, and hence cannot be overloaded, and can incorrectly change random text in a program. |
---|
17 | \item |
---|
18 | The same explicit management is true for the @const@ declaration, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{ |
---|
19 | C allows variable-length array-declarations (VLA), so this case does work, but it fails in \CC, which does not support VLAs, unless it is \lstinline{g++}.} immediate operands of assembler instructions, and occupy storage. |
---|
20 | \begin{clang} |
---|
21 | $\$$ nm test.o |
---|
22 | 0000000000000018 r Mon |
---|
23 | \end{clang} |
---|
24 | \item |
---|
25 | Only the @enum@ form is managed by the compiler, is part of the language type-system, and works in all C constant-expression locations. |
---|
26 | \end{enumerate} |
---|
27 | |
---|
28 | |
---|
29 | \section{C \lstinline{const}} |
---|
30 | |
---|
31 | As noted, C has the equivalent of Pascal typed @const@ declarations \see{\VRef{s:Pascal}}, with static and dynamic initialization. |
---|
32 | \begin{clang} |
---|
33 | static const int one = 0 + 1; $\C{// static intialization}$ |
---|
34 | static const void * NIL = NULL; |
---|
35 | static const double PI = 3.14159; |
---|
36 | static const char Plus = '+'; |
---|
37 | static const char * Fred = "Fred"; |
---|
38 | static const int Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1, |
---|
39 | Sat = Fri + 1, Sun = Sat + 1; |
---|
40 | void foo() { |
---|
41 | const int r = random(); $\C{// dynamic intialization}$ |
---|
42 | int sa[Sun]; $\C{// VLA, local scope only}$ |
---|
43 | } |
---|
44 | \end{clang} |
---|
45 | Statically initialized identifiers may appear in any constant-expression context, \eg @case@. |
---|
46 | Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays. |
---|
47 | |
---|
48 | |
---|
49 | \section{C Enumeration} |
---|
50 | |
---|
51 | The C enumeration has the following syntax and semantics. |
---|
52 | \begin{clang} |
---|
53 | enum Weekday { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun, }; |
---|
54 | \end{clang} |
---|
55 | Enumerators without an explicitly designated constant value are \Newterm{auto-initialized} by the compiler: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@. |
---|
56 | For example, @Mon@ to @Wed@ are implicitly assigned with constants @0@--@2@, @Thu@ is explicitly set to constant @10@, and @Fri@ to @Sun@ are implicitly assigned with constants @11@--@13@. |
---|
57 | Initialization may occur in any order. |
---|
58 | \begin{clang} |
---|
59 | enum Weekday { Thu@ = 10@, Fri, Sat, Sun, Mon@ = 0@, Tue, Wed }; |
---|
60 | \end{clang} |
---|
61 | Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma. |
---|
62 | \begin{clang} |
---|
63 | enum Weekday { |
---|
64 | Thu = 10, Fri, Sat, Sun, |
---|
65 | Mon = 0, Tue, Wed@,@ // terminating comma |
---|
66 | }; |
---|
67 | \end{clang} |
---|
68 | This feature allow enumerator lines to be interchanged without moving a comma.\footnote{ |
---|
69 | A terminating comma appears in other C syntax, \eg the initializer list.} |
---|
70 | Finally, C enumerators are \Newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type. |
---|
71 | |
---|
72 | In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values. |
---|
73 | In practice, since integral constants are used, which have type @int@ (unless qualified with a size suffix), C uses @int@ as the underlying type for enumeration variables. |
---|
74 | Finally, there is an implicit bidirectional conversion between an enumeration and its integral type. |
---|
75 | \begin{clang} |
---|
76 | { |
---|
77 | enum Weekday { /* as above */ }; $\C{// enumerators implicitly projected into local scope}$ |
---|
78 | Weekday weekday = Mon; $\C{// weekday == 0}$ |
---|
79 | weekday = Fri; $\C{// weekday == 11}$ |
---|
80 | int i = Sun; $\C{// implicit conversion to int, i == 13}$ |
---|
81 | weekday = 10000; $\C{// UNDEFINED! implicit conversion to Weekday}$ |
---|
82 | } |
---|
83 | int j = Wed; $\C{// ERROR! Wed is not declared in this scope}$ |
---|
84 | \end{clang} |
---|
85 | The implicit conversion from @int@ to an enumeration type is an unnecessary source of error. |
---|