Changeset 736a38d for doc/theses
- Timestamp:
- Jun 12, 2024, 9:16:50 AM (6 months ago)
- Branches:
- master
- Children:
- c033405
- Parents:
- 4c8f29ff
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
r4c8f29ff r736a38d 1 1 \chapter{Background} 2 2 3 \vspace*{-8pt} 4 3 5 \CFA is a backwards-compatible extension of the C programming language, therefore, it must support C-style enumerations. 4 The following covers C enumerations.6 The following discussion covers C enumerations. 5 7 6 8 As discussed in \VRef{s:Aliasing}, it is common for C programmers to ``believe'' there are three equivalent forms of named constants. … … 16 18 \item 17 19 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{ 18 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 oper ands of assembler instructions, and occupy storage.20 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 oper\-ands of assembler instructions, and occupy storage. 19 21 \begin{clang} 20 22 $\$$ nm test.o … … 22 24 \end{clang} 23 25 \item 24 Only the @enum@ form is managed by the compiler, is part of the language type-system, works in all C constant-expression locations, and might not occupy storage..26 Only the @enum@ form is managed by the compiler, is part of the language type-system, works in all C constant-expression locations, and normally does not occupy storage. 25 27 \end{enumerate} 26 28 … … 30 32 31 33 C can simulate the aliasing @const@ declarations \see{\VRef{s:Aliasing}}, with static and dynamic initialization. 34 \begin{cquote} 35 \begin{tabular}{@{}l@{}l@{}} 36 \multicolumn{1}{@{}c@{}}{\textbf{static initialization}} & \multicolumn{1}{c@{}}{\textbf{dynamic intialization}} \\ 32 37 \begin{clang} 33 static const int one = 0 + 1; $\C{// static initialization}$38 static const int one = 0 + 1; 34 39 static const void * NIL = NULL; 35 40 static const double PI = 3.14159; 36 41 static const char Plus = '+'; 37 42 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; 43 static const int Mon = 0, Tue = Mon + 1, Wed = Tue + 1, 44 Thu = Wed + 1, Fri = Thu + 1, Sat = Fri + 1, Sun = Sat + 1; 45 \end{clang} 46 & 47 \begin{clang} 40 48 void foo() { 41 const int r = random() % 100; $\C{// dynamic intialization}$ 42 int va[r]; $\C{// VLA, auto scope only}$ 49 // auto scope only 50 const int r = random() % 100; 51 int va[r]; 43 52 } 53 54 44 55 \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 on the stack. 56 \end{tabular} 57 \end{cquote} 58 However, statically initialized identifiers can not appear in constant-expression contexts, \eg @case@. 59 Dynamically initialized identifiers may appear in initialization and array dimensions in @g++@, which allows variable-sized arrays on the stack. 47 60 Again, this form of aliasing is not an enumeration. 48 61 … … 82 95 The enumerators are rvalues, so assignment is disallowed. 83 96 Finally, enumerators are \newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type. 84 For unnamed enumeration this semantic is required because there is no type name for scoped qualification.97 For unnamed enumerations, this semantic is required because there is no type name for scoped qualification. 85 98 86 99 As noted, this kind of aliasing declaration is not an enumeration, even though it is declared using an @enum@ in C. 87 100 While the semantics is misleading, this enumeration form matches with aggregate types: 88 101 \begin{cfa} 89 typedef struct /* unnamed */{ ... } S;90 struct /* unnamed */{ ... } x, y, z; $\C{// questionable}$102 typedef struct @/* unnamed */@ { ... } S; 103 struct @/* unnamed */@ { ... } x, y, z; $\C{// questionable}$ 91 104 struct S { 92 union /* unnamed */ {$\C{// unscoped fields}$105 union @/* unnamed */@ { $\C{// unscoped fields}$ 93 106 int i; double d ; char ch; 94 107 }; … … 132 145 @week = 10000;@ $\C{// UNDEFINED! implicit conversion to Week}$ 133 146 \end{clang} 134 While converting an enumerator to underlying type is useful, the implicit conversion from the base type to an enumeration type is a common source of error.147 While converting an enumerator to its underlying type is useful, the implicit conversion from the base type to an enumeration type is a common source of error. 135 148 136 149 Enumerators can appear in @switch@ and looping statements. … … 143 156 printf( "weekend\n" ); 144 157 } 145 for ( enum Week day = Mon; day <= Sun; day += 1 ) { 158 for ( enum Week day = Mon; day <= Sun; day += 1 ) { // step of 1 146 159 printf( "day %d\n", day ); // 0-6 147 160 } 148 161 \end{cfa} 149 For iterating, the enumerator values \emph{must} have a consecutive ordering with a fixed step between values. 162 For iterating to make sense, the enumerator values \emph{must} have a consecutive ordering with a fixed step between values. 163 For example, a gap introduced by @Thu = 10@, results in iterating over the values 0--13, where values 3--9 are not @Week@ values. 150 164 Note, it is the bidirectional conversion that allows incrementing @day@: @day@ is converted to @int@, integer @1@ is added, and the result is converted back to @Week@ for the assignment to @day@. 151 165 For safety, \CC does not support the bidirectional conversion, and hence, an unsafe cast is necessary to increment @day@: @day = (Week)(day + 1)@. 152 166 153 There is a C idiom to automatically knowthe number of enumerators in an enumeration.167 There is a C idiom to automatically compute the number of enumerators in an enumeration. 154 168 \begin{cfa} 155 169 enum E { A, B, C, D, @N@ }; // N == 4 … … 183 197 184 198 \bigskip 185 While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide enumeration features in other programming languages.199 While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide useful enumeration features in other programming languages.
Note: See TracChangeset
for help on using the changeset viewer.