Ignore:
Timestamp:
Jun 4, 2021, 11:24:41 AM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
53692b3, 9e67e92
Parents:
553f8abe
Message:

proofread Andrew's thesis chapters

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/andrew_beach_MMath/existing.tex

    r553f8abe r4ed7946e  
    22\label{c:existing}
    33
    4 \CFA (C-for-all)~\cite{Cforall} is an open-source project extending ISO C with
     4\CFA is an open-source project extending ISO C with
    55modern safety and productivity features, while still ensuring backwards
    66compatibility with C and its programmers.  \CFA is designed to have an
     
    99existing C code-base allowing programmers to learn \CFA on an as-needed basis.
    1010
    11 Only those \CFA features pertinent to this thesis are discussed.  Many of the
     11Only those \CFA features pertaining to this thesis are discussed.  Many of the
    1212\CFA syntactic and semantic features used in the thesis should be fairly
    1313obvious to the reader.
     
    2929// name mangling on by default
    3030int i; // _X1ii_1
    31 extern "C" {  // disables name mangling
     31@extern "C"@ {  // disables name mangling
    3232        int j; // j
    33         extern "Cforall" {  // enables name mangling
     33        @extern "Cforall"@ {  // enables name mangling
    3434                int k; // _X1ki_1
    3535        }
     
    4545\CFA adds a reference type to C as an auto-dereferencing pointer.
    4646They work very similarly to pointers.
    47 Reference-types are written the same way as a pointer-type is but each
     47Reference-types are written the same way as a pointer-type but each
    4848asterisk (@*@) is replaced with a ampersand (@&@);
    49 this includes cv-qualifiers and multiple levels of reference.
    50 
    51 They are intended for cases where you would want to use pointers but would
    52 be dereferencing them (almost) every usage.
    53 In most cases a reference can just be thought of as a pointer that
    54 automatically puts a dereference infront of each of its uses (per-level of
    55 reference).
    56 The address-of operator (@&@) acts as an escape and removes one of the
    57 automatic dereference operations.
    58 Mutable references may be assigned to by converting them to a pointer
    59 with a @&@ and then assigning a pointer too them.
    60 
    61 \begin{minipage}{0,45\textwidth}
     49this includes cv-qualifiers and multiple levels of reference, \eg:
     50
     51\begin{minipage}{0,5\textwidth}
    6252With references:
    6353\begin{cfa}
     
    6656int && rri = ri;
    6757rri = 3;
    68 &ri = &j;
     58&ri = &j; // reference assignment
    6959ri = 5;
    7060\end{cfa}
    7161\end{minipage}
    72 \begin{minipage}{0,45\textwidth}
     62\begin{minipage}{0,5\textwidth}
    7363With pointers:
    7464\begin{cfa}
     
    7767int ** ppi = &pi;
    7868**ppi = 3;
    79 pi = &j;
     69pi = &j; // pointer assignment
    8070*pi = 5;
    8171\end{cfa}
    8272\end{minipage}
    8373
    84 \section{Constructors and Destructors}
    85 
    86 Both constructors and destructors are operators, which means they are
    87 functions with special operator names rather than type names in \Cpp. The
    88 special operator names may be used to call the functions explicitly (not
    89 allowed in \Cpp for constructors).
     74References are intended for cases where you would want to use pointers but would
     75be dereferencing them (almost) every usage.
     76In most cases a reference can just be thought of as a pointer that
     77automatically puts a dereference in front of each of its uses (per-level of
     78reference).
     79The address-of operator (@&@) acts as an escape and removes one of the
     80automatic dereference operations.
     81Mutable references may be assigned by converting them to a pointer
     82with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above.
     83
     84\section{Operators}
    9085
    9186In general, operator names in \CFA are constructed by bracketing an operator
     
    9590(such as @++?@) and post-fix operations (@?++@).
    9691
    97 The special name for a constructor is @?{}@, which comes from the
    98 initialization syntax in C. That initialation syntax is also the operator
    99 form. \CFA will generate a constructor call each time a variable is declared,
    100 passing the initialization arguments to the constructort.
    101 \begin{cfa}
    102 struct Example { ... };
    103 void ?{}(Example & this) { ... }
    104 {
    105         Example a;
    106         Example b = {};
    107 }
    108 void ?{}(Example & this, char first, int num) { ... }
    109 {
    110         Example c = {'a', 2};
    111 }
    112 \end{cfa}
    113 Both @a@ and @b@ will be initalized with the first constructor (there is no
    114 general way to skip initialation) while @c@ will be initalized with the
    115 second.
     92An operator name may describe any function signature (it is just a name) but
     93only certain signatures may be called in operator form.
     94\begin{cfa}
     95int ?+?( int i, int j, int k ) { return i + j + k; }
     96{
     97        sout | ?+?( 3, 4, 5 ); // no infix form
     98}
     99\end{cfa}
     100Some ``near-misses" for unary/binary operator prototypes generate warnings.
     101
     102Both constructors and destructors are operators, which means they are
     103functions with special operator names rather than type names in \Cpp. The
     104special operator names may be used to call the functions explicitly (not
     105allowed in \Cpp for constructors).
     106
     107The special name for a constructor is @?{}@, where the name @{}@ comes from the
     108initialization syntax in C, \eg @Structure s = {...}@.
     109% That initialization syntax is also the operator form.
     110\CFA generates a constructor call each time a variable is declared,
     111passing the initialization arguments to the constructor.
     112\begin{cfa}
     113struct Structure { ... };
     114void ?{}(Structure & this) { ... }
     115{
     116        Structure a;
     117        Structure b = {};
     118}
     119void ?{}(Structure & this, char first, int num) { ... }
     120{
     121        Structure c = {'a', 2};
     122}
     123\end{cfa}
     124Both @a@ and @b@ are initialized with the first constructor,
     125while @c@ is initialized with the second.
     126Currently, there is no general way to skip initialization.
    116127
    117128% I don't like the \^{} symbol but $^\wedge$ isn't better.
    118 Similarly destructors use the special name @^?{}@ (the @^@ has no special
    119 meaning). They can be called explicatly as well but normally they are
    120 implicitly called on a variable when it goes out of scope.
    121 \begin{cfa}
    122 void ^?{}(Example & this) { ... }
    123 {
    124     Example d;
     129Similarly, destructors use the special name @^?{}@ (the @^@ has no special
     130meaning).  Normally, they are implicitly called on a variable when it goes out
     131of scope but they can be called explicitly as well.
     132\begin{cfa}
     133void ^?{}(Structure & this) { ... }
     134{
     135        Structure d;
    125136} // <- implicit destructor call
    126137\end{cfa}
    127 No operator name is restricted in what function signatures they may be bound
    128 to although most of the forms cannot be called in operator form. Some
    129 ``near-misses" will generate warnings.
    130 
    131 Whenever a type is defined, \CFA will create a default zero-argument
     138
     139Whenever a type is defined, \CFA creates a default zero-argument
    132140constructor, a copy constructor, a series of argument-per-field constructors
    133141and a destructor. All user constructors are defined after this.
     
    153161char capital_a = identity( 'A' );
    154162\end{cfa}
    155 Each use of a polymorphic declaration will resolve its polymorphic parameters
     163Each use of a polymorphic declaration resolves its polymorphic parameters
    156164(in this case, just @T@) to concrete types (@int@ in the first use and @char@
    157165in the second).
     
    159167To allow a polymorphic function to be separately compiled, the type @T@ must be
    160168constrained by the operations used on @T@ in the function body. The @forall@
    161 clauses is augmented with a list of polymorphic variables (local type names)
     169clause is augmented with a list of polymorphic variables (local type names)
    162170and assertions (constraints), which represent the required operations on those
    163171types used in a function, \eg:
    164172\begin{cfa}
    165 forall( T | { void do_once(T); })
     173forall( T | { void do_once(T); } )
    166174void do_twice(T value) {
    167175        do_once(value);
     
    190198void do_once(double y) { ... }
    191199int quadruple(int x) {
    192         void do_once(int y) { y = y * 2; }
    193         do_twice(x);
     200        void do_once(int y) { y = y * 2; } // replace global do_once
     201        do_twice(x); // use local do_once
     202        do_twice(x + 1.5); // use global do_once
    194203        return x;
    195204}
    196205\end{cfa}
    197206Specifically, the complier deduces that @do_twice@'s T is an integer from the
    198 argument @x@. It then looks for the most specific definition matching the
     207argument @x@. It then looks for the most \emph{specific} definition matching the
    199208assertion, which is the nested integral @do_once@ defined within the
    200209function. The matched assertion function is then passed as a function pointer
    201 to @do_twice@ and called within it.
    202 The global definition of @do_once@ is ignored.
     210to @do_twice@ and called within it.  The global definition of @do_once@ is used
     211for the second call because the float-point argument is a better match.
    203212
    204213To avoid typing long lists of assertions, constraints can be collect into
     
    270279Each coroutine has a @main@ function, which takes a reference to a coroutine
    271280object and returns @void@.
    272 \begin{cfa}
     281\begin{cfa}[numbers=left]
    273282void main(CountUp & this) {
    274     for (unsigned int next = 0 ; true ; ++next) {
     283        for (unsigned int next = 0 ; true ; ++next) {
    275284                next = up;
    276285                suspend;$\label{suspend}$
Note: See TracChangeset for help on using the changeset viewer.