source: doc/theses/jiada_liang_MMath/background.tex @ 710d0c8c

Last change on this file since 710d0c8c was 7d9a805b, checked in by Peter A. Buhr <pabuhr@…>, 3 months ago

more proofreading for enumerations

  • Property mode set to 100644
File size: 4.4 KB
Line 
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.
5Therefore, it must support C-style enumerations and any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
6
7It is common for C programmers to ``believe'' there are three equivalent forms of named constants.
8\begin{clang}
9#define Mon 0
10static const int Mon = 0;
11enum { Mon };
12\end{clang}
13\begin{enumerate}[leftmargin=*]
14\item
15For @#define@, the programmer has to explicitly manage the constant name and value.
16Furthermore, 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
18The 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{
19C 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
220000000000000018 r Mon
23\end{clang}
24\item
25Only 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
31As noted, C has the equivalent of Pascal typed @const@ declarations \see{\VRef{s:Pascal}}, with static and dynamic initialization.
32\begin{clang}
33static const int one = 0 + 1;                   $\C{// static intialization}$
34static const void * NIL = NULL;
35static const double PI = 3.14159;
36static const char Plus = '+';
37static const char * Fred = "Fred";
38static const int Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1,
39                                        Sat = Fri + 1, Sun = Sat + 1;
40void foo() {
41        const int r = random();                         $\C{// dynamic intialization}$
42        int sa[Sun];                                            $\C{// VLA, local scope only}$
43}
44\end{clang}
45Statically initialized identifiers may appear in any constant-expression context, \eg @case@.
46Dynamically initialized identifiers may appear as array dimensions in @g++@, which allows variable-sized arrays.
47
48
49\section{C Enumeration}
50
51The C enumeration has the following syntax and semantics.
52\begin{clang}
53enum Weekday { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun, };
54\end{clang}
55Enumerators 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@.
56For 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@.
57Initialization may occur in any order.
58\begin{clang}
59enum Weekday { Thu@ = 10@, Fri, Sat, Sun, Mon@ = 0@, Tue, Wed };
60\end{clang}
61Note, 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}
63enum Weekday {
64        Thu = 10, Fri, Sat, Sun,
65        Mon = 0, Tue, Wed@,@ // terminating comma
66};
67\end{clang}
68This feature allow enumerator lines to be interchanged without moving a comma.\footnote{
69A terminating comma appears in other C syntax, \eg the initializer list.}
70Finally, C enumerators are \Newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type.
71
72In theory, a C enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerated values.
73In 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.
74Finally, 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}
83int j = Wed;                                                    $\C{// ERROR! Wed is not declared in this scope}$
84\end{clang}
85The implicit conversion from @int@ to an enumeration type is an unnecessary source of error.
Note: See TracBrowser for help on using the repository browser.