Ignore:
Timestamp:
Aug 8, 2024, 10:39:40 PM (13 hours ago)
Author:
JiadaL <j82liang@…>
Branches:
master
Children:
acab1bd
Parents:
c1c0efdb
Message:

Minor update on the thesis (add auto initialization and update future work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/CFAenum.tex

    rc1c0efdb r7568e5c  
    11\chapter{\texorpdfstring{\CFA}{Cforall} Enumeration}
    22
    3 \CFA extends C-Style enumeration by adding a number of new features that bring enumerations inline with other modern programming languages.
    4 Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
    5 The following sections detail all of my 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.
     4Any enumeration extensions must be intuitive to C programmers in syntax and semantics.
     5The following sections detail my new contributions to enumerations in \CFA.
    66
    77
    88\section{Syntax}
    99
    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.
    1111\begin{cfa}[identifierstyle=\linespread{0.9}\it]
    1212$\it enum$-specifier:
     
    4343A A @0@ 3
    4444\end{cfa}
    45 Finally, there is an additional enumeration pseudo-function @countof@ (like @sizeof@, @typeof@) that returns the number of enumerators in an enumeration.
     45Finally, \CFA introduces an additional enumeration pseudo-function @countof@ (like @sizeof@, @typeof@) that returns the number of enumerators in an enumeration.
    4646\begin{cfa}
    4747enum(int) E { A, B, C, D } e;
     
    4949countof( e );  // 4, variable argument
    5050\end{cfa}
    51 This buildin function replaces the C idiom for automatically computing the number of enumerators \see{\VRef{s:Usage}}.
     51This built-in function replaces the C idiom for automatically computing the number of enumerators \see{\VRef{s:Usage}}.
    5252\begin{cfa}
    5353enum E { A, B, C, D, @N@ };  // N == 4
     
    5656The underlying representation of \CFA enumeration object is its position, saved as an integral type.
    5757Therefore, 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 is 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 backwards compatibility means the necessary backing data structures cannot be supplied.
     58Attribute function @posn@ performs type substitution on an expression from \CFA type to an integral type.
     59The 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.
     60These operations do not apply to C Enums because backward compatibility means the necessary backing data structures cannot be supplied.
    6161
    6262
     
    6464\label{s:OpaqueEnum}
    6565
    66 When an enumeration type is empty is it an \newterm{opaque} enumeration.
     66When an enumeration type is empty. it is an \newterm{opaque} enumeration.
    6767\begin{cfa}
    6868enum@()@ Mode { O_RDONLY, O_WRONLY, O_CREAT, O_TRUNC, O_APPEND };
    6969\end{cfa}
    70 Here, the internal representation is chosen by the compiler and hidden, so the enumerators cannot be initialized.
    71 Compared to the C enum, opaque enums are more restrictive in terms of typing and cannot be implicitly converted to integers.
     70Here, the compiler chooses the internal representation, which is hidden, so the enumerators cannot be initialized.
     71Compared to the C enum, opaque enums are more restrictive regarding typing and cannot be implicitly converted to integers.
    7272\begin{cfa}
    7373Mode mode = O_RDONLY;
    7474int www @=@ mode;                                               $\C{// disallowed}$
    7575\end{cfa}
    76 Opaque enumerations have only two attribute properties @label@ and @posn@.
     76Opaque enumerations have only two attribute properties, @label@ and @posn@.
    7777\begin{cfa}
    7878char * s = label( O_TRUNC );                    $\C{// "O\_TRUNC"}$
    7979int open = posn( O_WRONLY );                    $\C{// 1}$
    8080\end{cfa}
    81 The equality and relational operations are available.
     81Equality and relational operations are available.
    8282\begin{cfa}
    8383if ( mode @==@ O_CREAT ) ...
     
    8989\label{s:EnumeratorTyping}
    9090
    91 When an enumeration type is specified, all enumerators have that type and can be initialized with constants of that type or compile-time convertable 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 values used to set the enumerator constants.
    93 Note, the use of the synonyms @Liz@ and @Beth@ in the last declaration.
     91When 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.
     92Figure~\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.
     93Note the use of the synonyms @Liz@ and @Beth@ in the last declaration.
    9494Because enumerators are constants, the enumeration type is implicitly @const@, so all the enumerator types in Figure~\ref{f:EumeratorTyping} are logically rewritten with @const@.
    9595
     
    132132};
    133133\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.
     134Note 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.
    135135
    136136While the enumeration type can be any C aggregate, the aggregate's \CFA constructors are \emph{not} used to evaluate an enumerator's value.
     
    164164bar( x );                                       $\C{// costs (1, 0, 0, 0, 0, 0, 0, 0) or (0, 1, 0, 0, 0, 0, 0, 0)}$
    165165\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@.
     166Here, 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@,
     167which is a more expensive conversion.
    167168Hence, @bar( x )@ resolves @x@ as type @Math@.
    168169
     
    177178
    178179
    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:
    181182% 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-13
    184 % \end{cfa}
     183\begin{cfa}
     184enum Week { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; // 0-2, 10-13
     185\end{cfa}
    185186% 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
    187188% 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}
     191struct S { int i; };
     192S ?+?( S & s, one_t ) { return s.i++; }
     193void ?{}( S & s, zero_t ) { s.i = 0; }
     194enum(S) E { A, B, C, D };
     195\end{cfa}
     196For \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
    196202% Unfortunately, constant expressions in C are not powerful and \CFA is only a transpiler, relying on generated C code to perform the detail work.
    197203% 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.
     
    219225bar( A );                                                       $\C{// {\color{red}disallowed}}$
    220226\end{cfa}
    221 Hence, @Letter@ enumerators are not type compatible with the @Greek@ enumeration but the reverse is true.
     227Hence, @Letter@ enumerators are not type-compatible with the @Greek@ enumeration, but the reverse is true.
    222228
    223229
     
    241247
    242248Inheritance 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 inherits
    244 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 a
    245 common supertype (the diamond problem), since such would unavoidably introduce duplicate enumerator labels.
     249However, 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
     250from multiple enumeration that has overlapping enumerator labels. Consequently, a new type cannot inherit from an enumeration and its supertype or two enumerations with a
     251common supertype (the diamond problem) since such would unavoidably introduce duplicate enumerator labels.
    246252
    247253The 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 initialized.
     254When an enumeration inherits enumerators from another enumeration, it copies the enumerators' @value@ and @label@, even if the @value@ is auto-initialized.
    249255However, the position of the underlying representation is the order of the enumerator in the new enumeration.
    250256\begin{cfa}
     
    334340The 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.
    335341The 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 increment by one.
     342If a member is an enumerator of @dst@, the positions of all subsequent members are incremented by one.
    337343If the current member is @dst@, the function returns true indicating \emph{found} and the accumulated offset.
    338344Otherwise, the algorithm recurses into the current @CFAEnum@ @m@ to check if its @src@ is convertible to @m@.
     
    467473\VRef[Figure]{f:EnumerationI/O} show \CFA enumeration input based on the enumerator labels.
    468474When 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 strings constants can be quoted or unquoted.
     475For 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.
    470476
    471477\begin{figure}
Note: See TracChangeset for help on using the changeset viewer.