Changes in / [748877f:065de93]


Ignore:
Location:
doc/theses/jiada_liang_MMath
Files:
2 edited

Legend:

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

    r748877f r065de93  
    1 \chapter{C Enumeration in \CFA}
     1\chapter{C Enumeration in CFA}
    22
    33\CFA supports legacy C enumeration using the same syntax for backwards compatibility.
    4 A C-style enumeration in \CFA is called a \newterm{C Enum}.
    5 The semantics of the C Enum is mostly consistent with C with some restrictions.
    6 The following sections detail all of my new contributions to C Enums.
    7 
     4C-style Enumeration in \CFA language are called \newterm{C Enumeration} or \newterm{C Enum}.
     5The semantics of C Enumeration is mostly consistent with C with more restrictive typing.
     6\CFA also extends C Enumeration by adding a number of new features that bring enumerations aligns with other modern programming languages.
     7Any enumeration extensions must be intuitive to C programmers both in syntax and semantics.
     8The following sections detail all of my new contributions to enumerations in \CFA.
    89
    910\section{Enumerator Visibility}
     
    1112
    1213In C, unscoped enumerators present a \newterm{naming problem} when multiple enumeration types appear in the same scope with duplicate enumerator names.
     14There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system include files.
     15
     16The \CFA type-system allows extensive overloading, including enumerators.
     17Furthermore, \CFA uses the environment, such as the left-hand of assignment and function arguments, to pinpoint the best overloaded name.
     18\VRef[Figure]{f:EnumeratorVisibility} shows enumeration overloading and how qualification and casting are used to disambiguate ambiguous situations.
     19\CFA overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
     20Experience from \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names.
     21That is, it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded variables and functions, that requires disambiguation using qualification or casting.
     22
     23\begin{figure}
    1324\begin{cfa}
    1425enum E1 { First, Second, Third, Fourth };
    1526enum E2 { @Fourth@, @Third@, @Second@, @First@ }; $\C{// same enumerator names}$
    16 \end{cfa}
    17 There is no mechanism in C to resolve these naming conflicts other than renaming one of the duplicates, which may be impossible if the conflict comes from system include files.
    18 
    19 The \CFA type-system allows extensive overloading, including enumerators.
    20 Hence, most ambiguities among C enumerators are implicitly resolved by the \CFA type system, possibly without any programmer knowledge of the conflict.
    21 In addition, C Enum qualification is added, exactly like aggregate field-qualification, to disambiguate.
    22 \VRef[Figure]{f:EnumeratorVisibility} shows how resolution, qualification, and casting are used to disambiguate situations for enumerations @E1@ and @E2@.
    23 
    24 \begin{figure}
    25 \begin{cfa}
    2627E1 f() { return Third; }                                $\C{// overload functions with different return types}$
    2728E2 f() { return Fourth; }
     
    4142\end{figure}
    4243
     44\CFA overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
     45Experience from \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names, \ie it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded variables and functions.
     46Any ambiguity can be resolved using qualification or casting.
     47
    4348
    4449\section{Enumerator Scoping}
    4550
    46 A C Enum can be scoped, using @'!'@, so the enumerator constants are not projected into the enclosing scope.
     51An enumeration can be scoped, using @'!'@, so the enumerator constants are not projected into the enclosing scope.
    4752\begin{cfa}
    4853enum Week @!@ { Mon, Tue, Wed, Thu = 10, Fri, Sat, Sun };
     
    5661rgb = @RGB.@Blue;
    5762\end{cfa}
    58 % feature unimplemented
    59 It is possible to toggle back to unscoped using the \CFA @with@ auto-qualification clause/statement (see also \CC \lstinline[language=c++]{using enum} in Section~\ref{s:C++RelatedWork}).
     63{\color{red}@***@}It is possible to toggle back to unscoped using the \CFA @with@ clause/statement (see also \CC \lstinline[language=c++]{using enum} in Section~\ref{s:C++RelatedWork}).
    6064\begin{cfa}
    6165with ( @Week@, @RGB@ ) {                                $\C{// type names}$
     
    6670As in Section~\ref{s:EnumeratorVisibility}, opening multiple scoped enumerations in a @with@ can result in duplicate enumeration names, but \CFA implicit type resolution and explicit qualification/casting handle this localized scenario.
    6771
    68 
    6972\section{Type Safety}
    7073
    7174As in Section~\ref{s:Usage}, C's implicit bidirectional conversion between enumeration and integral type raises a safety concern.
    72 In \CFA, the conversion is changed to unidirectional: an enumeration can be implicitly converted into an integral type, with an associated @safe@ conversion cost.
    73 But an integral type cannot be implicitly converted into a C enumeration because the conversion cost is set to @infinity@.
     75In \CFA, the conversion is unidirectional:
     76% disallows an implicit conversion from integral type to enumeration, and conversion between different C enumeration type.
     77% It loses some degree of its backward compatibility to C, in exchange for type safety.
     78an enumeration can be implicitly converted into an integral type, with an associated @safe@ conversion cost.
     79But an integral type cannot be implicitly converted into a C enumeration. (Conversion Cost is Infinity.)
    7480\begin{cfa}
    75 enum Bird { Penguin, Robin, Eagle };
     81enum Bird { Pengin, Robin, Eagle };
    7682enum Fish { Shark, Salmon, Whale };
    7783
    78 int i = Robin;                                                  $\C{// allow, implicitly converts to 1}$
    79 enum Bird @bird = 1;@                                   $\C{// disallow }$
    80 enum Bird @bird = Shark;@                               $\C{// disallow }$
     84int i = Robin; $\C{// Allow, implicitly converts to 1}$
     85@enum Bird bird = Shark;@ $\C{// Disallow }$
     86@enum Bird bird = 1;@  $\C{// Disallow }$
    8187\end{cfa}
    82 It is now up to the programmer to insert an explicit cast to force the assignment.
     88As a workaround, \CFA allows an explicit cast to an enumeration, turning an integral type to an enumeration that can be used in assignment or function argument,
     89in which case \CFA treats C enuemration as its underlying integral type. In such cases, it is up to user to ensure program correctness.
    8390\begin{cfa}
    84 enum Bird bird = @(Bird)@1;
    85 enum Bird bird = @(Bird)@Shark
     91@enum Bird bird = (Bird) Shark@
     92@enum Bird bird = (Bird) 1;@
    8693\end{cfa}
    87 
    88 Note, \CC has the same safe restriction~\cite[C.1.5.7.2]{C++} and provides the same workaround cast.
    89 \begin{description}[parsep=0pt]
    90 \item[Change:] \CC objects of enumeration type can only be assigned values of the same enumeration type.
    91 In C, objects of enumeration type can be assigned values of any integral type.
    92 Example:
    93 \begin{cfa}
    94 enum color { red, blue, green };
    95 color c = 1;                            $\C{// valid C, invalid \CC}$
    96 \end{cfa}
    97 \item[Rationale:] The type-safe nature of \CC.
    98 \item[Effect on original feature:] Deletion of semantically well-defined feature.
    99 \item[Difficulty of converting:] Syntactic transformation. (The type error produced by the assignment can be
    100 automatically corrected by applying an explicit cast.)
    101 \item[How widely used:] Common.
    102 \end{description}
    103 
    104 \begin{comment}
    105 \begin{description}[parsep=0pt]
    106 \item[Change:] In \CC, the type of an enumerator is its enumeration.
    107 In C, the type of an enumerator is @int@.
    108 Example:
    109 \begin{cfa}
    110 enum e { A };
    111 sizeof(A) == sizeof(int)        $\C{// in C}$
    112 sizeof(A) == sizeof(e)          $\C{// in \CC}$
    113 /* and sizeof(int) is not necessary equal to sizeof(e) */
    114 \end{cfa}
    115 \item[Rationale:] In \CC, an enumeration is a distinct type.
    116 \item[Effect on original feature:] Change to semantics of well-defined feature.
    117 \item[Difficulty of converting:] Semantic transformation.
    118 \item[How widely used:] Seldom. The only time this affects existing C code is when the size of an enumerator is
    119 taken. Taking the size of an enumerator is not a common C coding practice.
    120 \end{description}
    121 \end{comment}
  • doc/theses/jiada_liang_MMath/background.tex

    r748877f r065de93  
    240240\section{\CFA}
    241241
    242 \CFA in \emph{not} an object-oriented programming-language, \ie functions cannot be nested in aggregate types, and hence, there is no \newterm{receiver} notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call and becomes an  implicit first (\lstinline[language=C++]{this}) parameter.
    243 The following sections provide short descriptions of \CFA features needed further in the thesis.
    244 Other \CFA features are presented in-situ with short explanations, or no explanation because the feature is obvious to C programmers.
    245 
    246 
    247 \subsection{Overloading}
    248 
    249 Overloading allows programmers to use the most meaningful names without fear of name clashes within a program or from external sources, like include files.
    250 \begin{quote}
    251 There are only two hard things in Computer Science: cache invalidation and naming things. --- Phil Karlton
    252 \end{quote}
    253 Experience from \CC and \CFA developers is that the type system implicitly and correctly disambiguates the majority of overloaded names, \ie it is rare to get an incorrect selection or ambiguity, even among hundreds of overloaded (variables and) functions.
    254 In many cases, a programmer has no idea there are name clashes, as they are silently resolved, simplifying the development process.
    255 Depending on the language, ambiguous cases are resolved using some form of qualification or casting.
     242\CFA in \emph{not} an object-oriented programming-language, \ie functions cannot be nested in aggregate types, and hence, there is no receive notation for calling functions, \eg @obj.method(...)@, where the first argument proceeds the call.
     243The following section provide short descriptions of \CFA features mentioned further in the thesis.
    256244
    257245
     
    285273Here, there is a perfect match for the call, @f( 'A' )@ with the number and parameter type of function (2).
    286274
    287 Ada, Scala, and \CFA type-systems also use the return type in resolving a call, to pinpoint the best overloaded name.
     275Ada, Scala, and \CFA type-systems also use the return type in resolving a call.
    288276\begin{cfa}
    289277int f( void );                  $\C[1.75in]{// (4); overloaded on return type}$
     
    308296\end{cfa}
    309297The \CFA type system simply treats overloaded variables as an overloaded function returning a value with no parameters.
    310 Hence, no significant effort is required to support this feature.
    311298
    312299
     
    321308
    322309The prototype for the constructor/destructor are @void ?{}( T &, ... )@ and @void ^?{}( T &, ... )@, respectively.
    323 The first parameter is logically, the \lstinline[language=C++]{this} or \lstinline[language=Python]{self} in other object-oriented languages, and implicitly passed.
    324 \VRef[Figure]{f:CFAConstructorDestructor} shows an example of creating and using a constructor and destructor.
    325 Both constructor and destructor can be explicitly called to reuse a variable.
    326 
    327 \begin{figure}
     310The first parameter is logically, the \lstinline[language=C++]{this} or \lstinline[language=java]{self} in other object-oriented languages, and implicitly passed.
    328311\begin{cfa}
    329312struct Employee {
     
    331314        double salary;
    332315};
    333 void @?{}@( Employee & emp, char * nname, double nsalary ) with( emp ) { // auto qualification
    334         name = aalloc( sizeof(nname) );
    335         strcpy( name, nname );
    336         salary = nsalary;
    337 }
    338 void @^?{}@( Employee & emp ) {
    339         free( emp.name );
     316void @?{}@( Employee & this, char * name, double salary ) {
     317        this.name = aalloc( sizeof(name) );
     318        strcpy( this.name, name );
     319        this.salary = salary;
     320}
     321void @^?{}@( Employee & this ) {
     322        free( this.name );
    340323}
    341324{
    342         Employee emp = { "Sara Schmidt", 20.5 }; $\C{// initialize with implicit constructor call}$
    343         ... // use emp
    344         ^?{}( emp ); $\C{// explicit de-initialize}$
    345         ?{}( emp, "Jack Smith", 10.5 ); $\C{// explicit re-initialize}$
    346         ... // use emp
    347 } $\C{// de-initialize with implicit destructor call}$
    348 \end{cfa}
    349 \caption{\CFA Constructor and Destructor}
    350 \label{f:CFAConstructorDestructor}
    351 \end{figure}
     325        Employee name = { "Sara Schmidt", 20.5 };
     326} // implicit destructor call
     327\end{cfa}
     328Both constructor and destructor can be explicitly called.
     329\begin{cfa}
     330        Employee name = { "Sara Schmidt", 20.5 };
     331        ... // use name
     332        ^?{}( name ); // de-initialize
     333        ?{}( name, "Jack Smith", 10.5 }; // re-initialize
     334        ... // use name
     335\end{cfa}
    352336
    353337
Note: See TracChangeset for help on using the changeset viewer.