Changeset 7568e5c for doc/theses/jiada_liang_MMath/CFAenum.tex
- Timestamp:
- Aug 8, 2024, 10:39:40 PM (13 hours ago)
- Branches:
- master
- Children:
- acab1bd
- Parents:
- c1c0efdb
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/CFAenum.tex
rc1c0efdb r7568e5c 1 1 \chapter{\texorpdfstring{\CFA}{Cforall} Enumeration} 2 2 3 \CFA extends C-Style enumeration by adding a number of new features that bring enumerations in line with other modern programming languages.4 Any enumeration extensions must be intuitive to C programmers bothin syntax and semantics.5 The following sections detail all ofmy new contributions to enumerations in \CFA.3 \CFA extends C-Style enumeration by adding a number of new features that bring enumerations in line with other modern programming languages. 4 Any enumeration extensions must be intuitive to C programmers in syntax and semantics. 5 The following sections detail my new contributions to enumerations in \CFA. 6 6 7 7 8 8 \section{Syntax} 9 9 10 \CFA extends the C enumeration declaration \see{\VRef{s:CEnumeration}} by parameterizing with a type (like a generic type) ,and adding Plan-9 inheritance \see{\VRef{s:CFAInheritance}} using an @inline@ to another enumeration type.10 \CFA extends the C enumeration declaration \see{\VRef{s:CEnumeration}} by parameterizing with a type (like a generic type) and adding Plan-9 inheritance \see{\VRef{s:CFAInheritance}} using an @inline@ to another enumeration type. 11 11 \begin{cfa}[identifierstyle=\linespread{0.9}\it] 12 12 $\it enum$-specifier: … … 43 43 A A @0@ 3 44 44 \end{cfa} 45 Finally, there is an additional enumeration pseudo-function @countof@ (like @sizeof@, @typeof@) that returns the number of enumerators in an enumeration.45 Finally, \CFA introduces an additional enumeration pseudo-function @countof@ (like @sizeof@, @typeof@) that returns the number of enumerators in an enumeration. 46 46 \begin{cfa} 47 47 enum(int) E { A, B, C, D } e; … … 49 49 countof( e ); // 4, variable argument 50 50 \end{cfa} 51 This buil din function replaces the C idiom for automatically computing the number of enumerators \see{\VRef{s:Usage}}.51 This built-in function replaces the C idiom for automatically computing the number of enumerators \see{\VRef{s:Usage}}. 52 52 \begin{cfa} 53 53 enum E { A, B, C, D, @N@ }; // N == 4 … … 56 56 The underlying representation of \CFA enumeration object is its position, saved as an integral type. 57 57 Therefore, the size of a \CFA enumeration is consistent with a C enumeration. 58 Attribute function @posn@ performs type substitution on an expression from \CFA type to integral type.59 The label and value of an enumerator isstored in a global data structure for each enumeration, where attribute functions @label@/@value@ map an \CFA enumeration object to the corresponding data.60 These operations do not apply to C Enums because backward scompatibility means the necessary backing data structures cannot be supplied.58 Attribute function @posn@ performs type substitution on an expression from \CFA type to an integral type. 59 The label and value of an enumerator are stored in a global data structure for each enumeration, where attribute functions @label@/@value@ map an \CFA enumeration object to the corresponding data. 60 These operations do not apply to C Enums because backward compatibility means the necessary backing data structures cannot be supplied. 61 61 62 62 … … 64 64 \label{s:OpaqueEnum} 65 65 66 When an enumeration type is empty is itan \newterm{opaque} enumeration.66 When an enumeration type is empty. it is an \newterm{opaque} enumeration. 67 67 \begin{cfa} 68 68 enum@()@ Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND }; 69 69 \end{cfa} 70 Here, the internal representation is chosen by the compiler andhidden, so the enumerators cannot be initialized.71 Compared to the C enum, opaque enums are more restrictive in terms oftyping and cannot be implicitly converted to integers.70 Here, the compiler chooses the internal representation, which is hidden, so the enumerators cannot be initialized. 71 Compared to the C enum, opaque enums are more restrictive regarding typing and cannot be implicitly converted to integers. 72 72 \begin{cfa} 73 73 Mode mode = O_RDONLY; 74 74 int www @=@ mode; $\C{// disallowed}$ 75 75 \end{cfa} 76 Opaque enumerations have only two attribute properties @label@ and @posn@.76 Opaque enumerations have only two attribute properties, @label@ and @posn@. 77 77 \begin{cfa} 78 78 char * s = label( O_TRUNC ); $\C{// "O\_TRUNC"}$ 79 79 int open = posn( O_WRONLY ); $\C{// 1}$ 80 80 \end{cfa} 81 The equality and relational operations are available.81 Equality and relational operations are available. 82 82 \begin{cfa} 83 83 if ( mode @==@ O_CREAT ) ... … … 89 89 \label{s:EnumeratorTyping} 90 90 91 When an enumeration type is specified, all enumerators have that type and can be initialized with constants of that type or compile-time convert able to that type.92 Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be use with an enumeration and each type's valuesused to set the enumerator constants.93 Note ,the use of the synonyms @Liz@ and @Beth@ in the last declaration.91 When an enumeration type is specified, all enumerators have that type and can be initialized with constants of that type or compile-time convertible to that type. 92 Figure~\ref{f:EumeratorTyping} shows a series of examples illustrating that all \CFA types can be used with an enumeration, and each type's values are used to set the enumerator constants. 93 Note the use of the synonyms @Liz@ and @Beth@ in the last declaration. 94 94 Because enumerators are constants, the enumeration type is implicitly @const@, so all the enumerator types in Figure~\ref{f:EumeratorTyping} are logically rewritten with @const@. 95 95 … … 132 132 }; 133 133 \end{cfa} 134 Note ,the enumeration type can be a structure (see @Person@ in Figure~\ref{f:EumeratorTyping}), so it is possible to have the equivalent of multiple arrays of companion data using an array of structures.134 Note that the enumeration type can be a structure (see @Person@ in Figure~\ref{f:EumeratorTyping}), so it is possible to have the equivalent of multiple arrays of companion data using an array of structures. 135 135 136 136 While the enumeration type can be any C aggregate, the aggregate's \CFA constructors are \emph{not} used to evaluate an enumerator's value. … … 164 164 bar( x ); $\C{// costs (1, 0, 0, 0, 0, 0, 0, 0) or (0, 1, 0, 0, 0, 0, 0, 0)}$ 165 165 \end{cfa} 166 Here, candidate (1) has a value conversion cost to convert to the base type, while candidate (2) has an unsafe conversion from @double@ to @int@. 166 Here, the candidate (1) has a @value@ conversion cost to convert to the base type, while the candidate (2) has an @unsafe@ conversion from @double@ to @int@, 167 which is a more expensive conversion. 167 168 Hence, @bar( x )@ resolves @x@ as type @Math@. 168 169 … … 177 178 178 179 179 %\section{Auto Initialization}180 % 180 \section{Auto Initialization} 181 \CFA implements auto-initialization for both C enumerations and \CFA enumerations. For the first category, the semantics is consistent with C: 181 182 % A partially implemented feature is auto-initialization, which works for the C integral type with constant expressions. 182 %\begin{cfa}183 %enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13184 %\end{cfa}183 \begin{cfa} 184 enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13 185 \end{cfa} 185 186 % The complexity of the constant expression depends on the level of computation the compiler implements, \eg \CC \lstinline[language={[GNU]C++}]{constexpr} provides complex compile-time computation across multiple types, which blurs the compilation/runtime boundary. 186 % 187 187 188 % If \CFA had powerful compilation expression evaluation, auto initialization would be implemented as follows. 188 % \begin{cfa} 189 % enum E(T) { A, B, C }; 190 % \end{cfa} 191 % \begin{enumerate} 192 % \item the first enumerator, @A@, is initialized with @T@'s @zero_t@. 193 % \item otherwise, the next enumerator is initialized with the previous enumerator's value using operator @?++@, where @?++( T )@ can be overloaded for any type @T@. 194 % \end{enumerate} 195 % 189 190 \begin{cfa} 191 struct S { int i; }; 192 S ?+?( S & s, one_t ) { return s.i++; } 193 void ?{}( S & s, zero_t ) { s.i = 0; } 194 enum(S) E { A, B, C, D }; 195 \end{cfa} 196 For \CFA enumeration, the semantics is the following: 197 \begin{enumerate} 198 \item the first enumerator, @A@, is initialized with @T@'s @zero_t@. 199 \item otherwise, the next enumerator is initialized with the previous enumerator's value using the operator @?+?(T, one_t)@, which can be overloaded for any type @T@. 200 \end{enumerate} 201 196 202 % Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work. 197 203 % It is currently beyond the scope of the \CFA project to implement a complex runtime interpreter in the transpiler to evaluate complex expressions across multiple builtin and user-defined type. … … 219 225 bar( A ); $\C{// {\color{red}disallowed}}$ 220 226 \end{cfa} 221 Hence, @Letter@ enumerators are not type compatible with the @Greek@ enumerationbut the reverse is true.227 Hence, @Letter@ enumerators are not type-compatible with the @Greek@ enumeration, but the reverse is true. 222 228 223 229 … … 241 247 242 248 Inheritance can be nested, and a \CFA enumeration can inline enumerators from more than one \CFA enumeration, forming a tree-like hierarchy. 243 However, the uniqueness of enumeration name applies to enumerators, including those from supertypes, meaning an enumeration cannot name enumerator with the same label as its subtype's members,or inherits244 from multiple enumeration that has overlapping enumerator label . As a consequence, a new type cannot inherits from both an enumeration and its supertype,or two enumerations with a245 common supertype (the diamond problem) ,since such would unavoidably introduce duplicate enumerator labels.249 However, the uniqueness of the enumeration name applies to enumerators, including those from supertypes, meaning an enumeration cannot name an enumerator with the same label as its subtype's members or inherits 250 from multiple enumeration that has overlapping enumerator labels. Consequently, a new type cannot inherit from an enumeration and its supertype or two enumerations with a 251 common supertype (the diamond problem) since such would unavoidably introduce duplicate enumerator labels. 246 252 247 253 The base type must be consistent between subtype and supertype. 248 When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ is auto 254 When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ is auto-initialized. 249 255 However, the position of the underlying representation is the order of the enumerator in the new enumeration. 250 256 \begin{cfa} … … 334 340 The algorithm takes two @CFAEnums@ parameters, @src@ and @dst@, with @src@ being the type of expression the conversion applies to, and @dst@ being the type the expression is cast to. 335 341 The algorithm iterates over the members in @dst@ to find @src@. 336 If a member is an enumerator of @dst@, the positions of all subsequent members is incrementby one.342 If a member is an enumerator of @dst@, the positions of all subsequent members are incremented by one. 337 343 If the current member is @dst@, the function returns true indicating \emph{found} and the accumulated offset. 338 344 Otherwise, the algorithm recurses into the current @CFAEnum@ @m@ to check if its @src@ is convertible to @m@. … … 467 473 \VRef[Figure]{f:EnumerationI/O} show \CFA enumeration input based on the enumerator labels. 468 474 When the enumerator labels are packed together in the input stream, the input algorithm scans for the longest matching string. 469 For basic types in \CFA, the rule is that the same constants used to initialize a variable in a program are available to initialize a variable using input, where string sconstants can be quoted or unquoted.475 For basic types in \CFA, the rule is that the same constants used to initialize a variable in a program are available to initialize a variable using input, where string constants can be quoted or unquoted. 470 476 471 477 \begin{figure}
Note: See TracChangeset
for help on using the changeset viewer.