\chapter{Background} \vspace*{-8pt} \CFA is a backwards-compatible extension of the C programming language, therefore, it must support C-style enumerations. The following discussion covers C enumerations. As discussed in \VRef{s:Aliasing}, it is common for C programmers to ``believe'' there are three equivalent forms of named constants. \begin{clang} #define Mon 0 static const int Mon = 0; enum { Mon }; \end{clang} \begin{enumerate}[leftmargin=*] \item For @#define@, the programmer has to explicitly manage the constant name and value. Furthermore, these C preprocessor macro names are outside of the C type-system and can incorrectly change random text in a program. \item 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{ 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. \begin{clang} $\$$ nm test.o 0000000000000018 r Mon \end{clang} \item 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. \end{enumerate} \section{C \lstinline{const}} \label{s:Cconst} C can simulate the aliasing @const@ declarations \see{\VRef{s:Aliasing}}, with static and dynamic initialization. \begin{cquote} \begin{tabular}{@{}l@{}l@{}} \multicolumn{1}{@{}c@{}}{\textbf{static initialization}} & \multicolumn{1}{c@{}}{\textbf{dynamic intialization}} \\ \begin{clang} static const int one = 0 + 1; static const void * NIL = NULL; static const double PI = 3.14159; static const char Plus = '+'; static const char * Fred = "Fred"; static const int Mon = 0, Tue = Mon + 1, Wed = Tue + 1, Thu = Wed + 1, Fri = Thu + 1, Sat = Fri + 1, Sun = Sat + 1; \end{clang} & \begin{clang} void foo() { // auto scope only const int r = random() % 100; int va[r]; } \end{clang} \end{tabular} \end{cquote} However, statically initialized identifiers can not appear in constant-expression contexts, \eg @case@. Dynamically initialized identifiers may appear in initialization and array dimensions in @g++@, which allows variable-sized arrays on the stack. Again, this form of aliasing is not an enumeration. \section{C Enumeration} \label{s:CEnumeration} The C enumeration has the following syntax~\cite[\S~6.7.2.2]{C11}. \begin{clang}[identifierstyle=\linespread{0.9}\it] $\it enum$-specifier: enum identifier$\(_{opt}\)$ { enumerator-list } enum identifier$\(_{opt}\)$ { enumerator-list , } enum identifier enumerator-list: enumerator enumerator-list , enumerator enumerator: enumeration-constant enumeration-constant = constant-expression \end{clang} The terms \emph{enumeration} and \emph{enumerator} used in this work \see{\VRef{s:Terminology}} come from the grammar. The C enumeration semantics are discussed using examples. \subsection{Type Name} \label{s:TypeName} An \emph{unnamed} enumeration is used to provide aliasing \see{\VRef{s:Aliasing}} exactly like a @const@ declaration in other languages. However, it is restricted to integral values. \begin{clang} enum { Size = 20, Max = 10, MaxPlus10 = Max + 10, @Max10Plus1@, Fred = -7 }; \end{clang} Here, the aliased constants are: 20, 10, 20, 21, and -7. Direct initialization is by a compile-time expression generating a constant value. Indirect initialization (without initialization, @Max10Plus1@) is \newterm{auto-initialized}: from left to right, starting at zero or the next explicitly initialized constant, incrementing by @1@. Because multiple independent enumerators can be combined, enumerators with the same values can occur. The enumerators are rvalues, so assignment is disallowed. Finally, enumerators are \newterm{unscoped}, \ie enumerators declared inside of an @enum@ are visible (projected) into the enclosing scope of the @enum@ type. For unnamed enumerations, this semantic is required because there is no type name for scoped qualification. As noted, this kind of aliasing declaration is not an enumeration, even though it is declared using an @enum@ in C. While the semantics is misleading, this enumeration form matches with aggregate types: \begin{cfa} typedef struct @/* unnamed */@ { ... } S; struct @/* unnamed */@ { ... } x, y, z; $\C{// questionable}$ struct S { union @/* unnamed */@ { $\C{// unscoped fields}$ int i; double d ; char ch; }; }; \end{cfa} Hence, C programmers would expect this enumeration form to exist in harmony with the aggregate form. A \emph{named} enumeration is an enumeration: \begin{clang} enum @Week@ { Mon, Tue, Wed, Thu@ = 10@, Fri, Sat, Sun }; \end{clang} and adopts the same semantics with respect to direct and auto intialization. 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@. As well, initialization may occur in any order. \begin{clang} enum Week { Thu@ = 10@, Fri, Sat, Sun, Mon@ = 0@, Tue, Wed@,@ $\C{// terminating comma}$ }; \end{clang} Note, the comma in the enumerator list can be a terminator or a separator, allowing the list to end with a dangling comma.\footnote{ A terminating comma appears in other C syntax, \eg the initializer list.} This feature allow enumerator lines to be interchanged without moving a comma. Named enumerators are also unscoped. \subsection{Representation} C standard specifies enumeration \emph{variable} is an implementation-defined integral type large enough to hold all enumerator values. In practice, C uses @int@ as the underlying type for enumeration variables, because of the restriction to integral constants, which have type @int@ (unless qualified with a size suffix). \subsection{Usage} \label{s:Usage} C proves an implicit \emph{bidirectional} conversion between an enumeration and its integral type. \begin{clang} enum Week week = Mon; $\C{// week == 0}$ week = Fri; $\C{// week == 11}$ int i = Sun; $\C{// implicit conversion to int, i == 13}$ @week = 10000;@ $\C{// UNDEFINED! implicit conversion to Week}$ \end{clang} 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. Enumerators can appear in @switch@ and looping statements. \begin{cfa} enum Week { Mon, Tue, Wed, Thu, Fri, Sat, Sun }; switch ( week ) { case Mon: case Tue: case Wed: case Thu: case Fri: printf( "weekday\n" ); case Sat: case Sun: printf( "weekend\n" ); } for ( enum Week day = Mon; day <= Sun; day += 1 ) { // step of 1 printf( "day %d\n", day ); // 0-6 } \end{cfa} For iterating to make sense, the enumerator values \emph{must} have a consecutive ordering with a fixed step between values. For example, a gap introduced by @Thu = 10@, results in iterating over the values 0--13, where values 3--9 are not @Week@ values. 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@. For safety, \CC does not support the bidirectional conversion, and hence, an unsafe cast is necessary to increment @day@: @day = (Week)(day + 1)@. There is a C idiom to automatically compute the number of enumerators in an enumeration. \begin{cfa} enum E { A, B, C, D, @N@ }; // N == 4 for ( enum E e = A; e < @N@; e += 1 ) ... \end{cfa} Here, the auto-incrementing counts the number of enumerators and puts the total into the last enumerator @N@. @N@ is often used as the dimension for an array assocated with the enumeration. \begin{cfa} E array[@N@]; for ( enum E e = A; e < N; e += 1 ) { array[e] = e; } \end{cfa} However, for typed enumerations, \see{\VRef{f:EumeratorTyping}}, this idiom fails. This idiom leads to another C idiom using an enumeration with matching companion information. For example, an enumeration is linked with a companion array of printable strings. \begin{cfa} enum Integral_Type { chr, schar, uschar, sshort, ushort, sint, usint, ..., NO_OF_ITYPES }; char * Integral_Name[@NO_OF_ITYPES@] = { "char", "signed char", "unsigned char", "signed short int", "unsigned short int", "signed int", "unsigned int", ... }; enum Integral_Type integral_type = ... printf( "%s\n", Integral_Name[@integral_type@] ); // human readable type name \end{cfa} However, the companion idiom results in the \emph{harmonizing} problem because an update to the enumeration @Integral_Type@ often requires a corresponding update to the companion array \snake{Integral_Name}. The need to harmonize is at best indicated by a comment before the enumeration. This issue is exacerbated if enumeration and companion array are in different translation units. \bigskip While C provides a true enumeration, it is restricted, has unsafe semantics, and does provide useful enumeration features in other programming languages. \section{\CFA Polymorphism} \subsection{Function Overloading} Function overloading is programming languages feature wherein functions may share the same name, but with different function signatures. In both C++ and \CFA, function names can be overloaded with different entities as long as they are different in terms of the number and type of parameters. \begin{cfa} void f(); // (1) void f(int); // (2); Overloaded on the number of parameters void f(char); // (3); Overloaded on parameter type f('A'); \end{cfa} In this case, the name f is overloaded with a nullity function and two arity functions with different parameters types. Exactly which precedures being executed is determined based on the passing arguments. The last expression of the preceding example calls f with one arguments, narrowing the possible candidates down to (2) and (3). Between those, function argument 'A' is an exact match to the parameter expected by (3), while needing an @implicit conversion@ to call (2). The compiler determines (3) is the better candidates among and procedure (3) is being executed. \begin{cfa} int f(int); // (4); Overloaded on return type [int, int] f(int); // (5) Overloaded on the number of return value \end{cfa} The function declarations (4) and (5) show the ability of \CFA functions overloaded with different return value, a feature that is not shared by C++. \subsection{Operator Overloading} Operators in \CFA are specialized function and are overloadable by with specially-named functions represents the syntax used to call the operator. % For example, @bool ?==?T(T lhs, T rhs)@ overloads equality operator for type T, where @?@ is the placeholders for operands for the operator. \begin{cfa} enum Weekday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; bool ? days_can_fly(bird)) { fly(bird); } } } struct Robin {} r; int days_can_fly(Robin r) { return 23; } void fly(Robin r) {} bird_fly( r ); \end{cfa} Grouping type assertions into named trait effectively create a reusable interface for parametrics polymorphics types. \section{Expression Resolution} The overloading feature poses a challenge in \CFA expression resolution. Overloadeded identifiers can refer multiple candidates, with multiples being simultaneously valid. The main task of \CFA resolver is to identity a best candidate that involes less implicit conversion and polymorphism. \subsection{Conversion Cost} In C, functions argument and parameter type does not need to be exact match, and the compiler performs an @implicit conversion@ on argument. \begin{cfa} void foo(double i); foo(42); \end{cfa} The implicit conversion in C is relatively simple because of the abscence of overloading, with the exception of binary operators, for which the compiler needs to find a common type of both operands and the result. The pattern is known as "usual arithmetic conversions". \CFA generalizes C implicit conversion to function overloading as a concept of @conversion cost@. Initially designed by Bilson, conversion cost is a 3-tuple, @(unsafe, poly, safe)@, where unsafe is the number of narrowing conversion, poly is the count of polymorphics type binding, and safe is the sum of the degree of widening conversion. Every basic type in \CFA has been assigned with a @distance to Byte@, or @distance@, and the degree of widening conversion is the difference between two distances. Aaron extends conversion cost to a 7-tuple, @@(unsafe, poly, safe, sign, vars, specialization, reference)@@. The summary of Aaron's cost model is the following: \begin{itemize} \item Unsafe is the number of argument that implicitly convert to a type with high rank. \item Poly accounts for number of polymorphics binding in the function declaration. \item Safe is sum of distance (add reference/appendix later). \item Sign is the number of sign/unsign variable conversion. \item Vars is the number of polymorphics type declared in @forall@. \item Specialization is opposite number of function declared in @forall@. More function declared implies more constraint on polymorphics type, and therefore has the lower cost. \item Reference is number of lvalue-to-rvalue conversion. \end{itemize}