Changeset 7d9a805b for doc/theses/jiada_liang_MMath/background.tex
- Timestamp:
- Mar 21, 2024, 9:34:28 PM (3 months ago)
- Branches:
- master
- Children:
- 6394ac6
- Parents:
- 0139351
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/background.tex
r0139351 r7d9a805b 1 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} 2 27 3 28 4 \section{C -Style Enum}29 \section{C \lstinline{const}} 5 30 6 The C-Style enumeration has the following syntax and semantics. 7 \begin{cfa} 8 enum Weekday { Monday, Tuesday, Wednesday, Thursday@ = 10@, Friday, Saturday, Sunday }; 9 \end{cfa} 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} 10 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@. 11 For example, @Mon day@ to @Wednesday@ are implicitly assigned with constants @0@--@2@, @Thursday@ is explicitly set to constant @10@, and @Friday@ to @Sunday@ are implicitly assigned with constants @11@--@13@.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@. 12 57 Initialization may occur in any order. 13 \begin{c fa}14 enum Weekday { Thu rsday@ = 10@, Friday, Saturday, Sunday, Monday@ = 0@, Tuesday, Wednesday};15 \end{c fa}58 \begin{clang} 59 enum Weekday { Thu@ = 10@, Fri, Sat, Sun, Mon@ = 0@, Tue, Wed }; 60 \end{clang} 16 61 Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma. 17 \begin{c fa}62 \begin{clang} 18 63 enum Weekday { 19 Thu rsday = 10, Friday, Saturday, Sunday,20 Mon day = 0, Tuesday, Wednesday@,@ // terminating comma64 Thu = 10, Fri, Sat, Sun, 65 Mon = 0, Tue, Wed@,@ // terminating comma 21 66 }; 22 \end{c fa}67 \end{clang} 23 68 This feature allow enumerator lines to be interchanged without moving a comma.\footnote{ 24 69 A terminating comma appears in other C syntax, \eg the initializer list.} … … 28 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. 29 74 Finally, there is an implicit bidirectional conversion between an enumeration and its integral type. 30 \begin{c fa}75 \begin{clang} 31 76 { 32 77 enum Weekday { /* as above */ }; $\C{// enumerators implicitly projected into local scope}$ 33 Weekday weekday = Mon day;$\C{// weekday == 0}$34 weekday = Fri day;$\C{// weekday == 11}$35 int i = Sun day; $\C{// implicit conversion to int, i == 13}$78 Weekday weekday = Mon; $\C{// weekday == 0}$ 79 weekday = Fri; $\C{// weekday == 11}$ 80 int i = Sun; $\C{// implicit conversion to int, i == 13}$ 36 81 weekday = 10000; $\C{// UNDEFINED! implicit conversion to Weekday}$ 37 82 } 38 int j = Wed nesday; $\C{// ERROR! Wednesdayis not declared in this scope}$39 \end{c fa}83 int j = Wed; $\C{// ERROR! Wed is not declared in this scope}$ 84 \end{clang} 40 85 The implicit conversion from @int@ to an enumeration type is an unnecessary source of error. 41 42 It is common for C programmers to ``believe'' there are 3 equivalent forms of constant enumeration.43 \begin{cfa}44 #define Monday 045 static const int Monday = 0;46 enum { Monday };47 \end{cfa}48 For @#define@, the programmer has to play compiler and explicitly manage the enumeration values;49 furthermore, these are independent constants outside of any language type mechanism.50 The same explicit management is true for @const@ declarations, and the @const@ variable cannot appear in constant-expression locations, like @case@ labels, array dimensions,\footnote{51 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++}.} and immediate operands of assembler instructions.52 Only the @enum@ form is managed by the compiler, is part of the language type-system, and works in all C constant-expression locations.
Note: See TracChangeset
for help on using the changeset viewer.