Ignore:
Timestamp:
Apr 29, 2021, 4:26:37 PM (3 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
34b2796, 63a4b92
Parents:
c993b15 (diff), 2d8a770 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

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

    rc993b15 r3eb55f98  
    1616to be defined~\cite{Moss18}.
    1717\begin{cfa}
    18 char i; int i; double i;                        $\C[3.75in]{// variable overload}$
    19 int f(); double f();                            $\C{// return overload}$
    20 void g( int ); void g( double );        $\C{// parameter overload}\CRT$
     18char i; int i; double i;
     19int f(); double f();
     20void g( int ); void g( double );
    2121\end{cfa}
    2222This feature requires name mangling so the assembly symbols are unique for
     
    2626mangling is:
    2727\begin{cfa}
    28 // name mangling
     28// name mangling on by default
    2929int i; // _X1ii_1
    30 @extern "C"@ {  // no name mangling
     30extern "C" {  // disables name mangling
    3131        int j; // j
    32         @extern "Cforall"@ {  // name mangling
     32        extern "Cforall" {  // enables name mangling
    3333                int k; // _X1ki_1
    3434        }
    35         // no name mangling
    36 }
    37 // name mangling
     35        // revert to no name mangling
     36}
     37// revert to name mangling
    3838\end{cfa}
    3939Both forms of @extern@ affect all the declarations within their nested lexical
     
    5050\begin{cfa}
    5151int i, j;
    52 int @&@ ri = i, @&&@ rri = ri;
     52int & ri = i, && rri = ri;
    5353rri = 3;  // auto-dereference assign to i
    54 @&@ri = @&@j; // rebindable
     54&ri = &j; // rebindable
    5555ri = 5;   // assign to j
    5656\end{cfa}
     
    6464
    6565In general, operator names in \CFA are constructed by bracketing an operator
    66 token with @?@, which indicates the position of the arguments. For example, infixed
    67 multiplication is @?*?@ while prefix dereference is @*?@. This syntax make it
    68 easy to tell the difference between prefix operations (such as @++?@) and
    69 post-fix operations (@?++@).
     66token with @?@, which indicates the position of the arguments. For example,
     67infixed multiplication is @?*?@ while prefix dereference is @*?@.
     68This syntax make it easy to tell the difference between prefix operations
     69(such as @++?@) and post-fix operations (@?++@).
    7070
    7171The special name for a constructor is @?{}@, which comes from the
    72 initialization syntax in C. The special name for a destructor is @^{}@, where
    73 the @^@ has no special meaning.
     72initialization syntax in C. That initialation syntax is also the operator
     73form. \CFA will generate a constructor call each time a variable is declared,
     74passing the initialization arguments to the constructort.
     75\begin{cfa}
     76struct Example { ... };
     77void ?{}(Example & this) { ... }
     78{
     79        Example a;
     80        Example b = {};
     81}
     82void ?{}(Example & this, char first, int num) { ... }
     83{
     84        Example c = {'a', 2};
     85}
     86\end{cfa}
     87Both @a@ and @b@ will be initalized with the first constructor (there is no
     88general way to skip initialation) while @c@ will be initalized with the
     89second.
     90
    7491% I don't like the \^{} symbol but $^\wedge$ isn't better.
    75 \begin{cfa}
    76 struct T { ... };
    77 void ?@{}@(@T &@ this, ...) { ... }  // constructor
    78 void ?@^{}@(@T &@ this, ...) { ... } // destructor
     92Similarly destructors use the special name @^?{}@ (the @^@ has no special
     93meaning). They can be called explicatly as well but normally they are
     94implicitly called on a variable when it goes out of scope.
     95\begin{cfa}
     96void ^?{}(Example & this) { ... }
    7997{
    80         T s = @{@ ... @}@;  // same constructor/initialization braces
    81 } // destructor call automatically generated
    82 \end{cfa}
    83 The first parameter is a reference parameter to the type for the
    84 constructor/destructor. Destructors may have multiple parameters.  The compiler
    85 implicitly matches an overloaded constructor @void ^?{}(T &, ...);@ to an
    86 object declaration with associated initialization, and generates a construction
    87 call after the object is allocated. When an object goes out of scope, the
    88 matching overloaded destructor @void ^?{}(T &);@ is called.  Without explicit
    89 definition, \CFA creates a default and copy constructor, destructor and
    90 assignment (like \Cpp). It is possible to define constructors/destructors for
    91 basic and existing types (unlike \Cpp).
     98    Example d;
     99} // <- implicit destructor call
     100\end{cfa}
     101No operator name is restricted in what function signatures they may be bound
     102to although most of the forms cannot be called in operator form. Some
     103``near-misses" will generate warnings.
     104
     105Whenever a type is defined, \CFA will create a default zero-argument
     106constructor, a copy constructor, a series of argument-per-field constructors
     107and a destructor. All user constructors are defined after this.
     108Because operators are never part of the type definition they may be added
     109at any time, including on built-in types.
    92110
    93111\section{Polymorphism}
     
    105123works on any type @T@:
    106124\begin{cfa}
    107 @forall( T )@ @T@ identity( @T@ val ) { return val; }
    108 int forty_two = identity( 42 ); // T bound to int, forty_two == 42
    109 \end{cfa}
     125forall( T ) T identity( T val ) { return val; }
     126int forty_two = identity( 42 );
     127char capital_a = identity( 'A' );
     128\end{cfa}
     129Each use of a polymorphic declaration will resolve its polymorphic parameters
     130(in this case, just @T@) to concrete types (@int@ in the first use and @char@
     131in the second).
    110132
    111133To allow a polymorphic function to be separately compiled, the type @T@ must be
     
    115137types used in a function, \eg:
    116138\begin{cfa}
    117 forall( T @| { void do_once(T); }@) // assertion
     139forall( T | { void do_once(T); })
    118140void do_twice(T value) {
    119141        do_once(value);
    120142        do_once(value);
    121143}
    122 void do_once(@int@ i) { ... }  // provide assertion
    123 @int@ i;
    124 do_twice(i); // implicitly pass assertion do_once to do_twice
    125 \end{cfa}
    126 Any object with a type fulfilling the assertion may be passed as an argument to
    127 a @do_twice@ call.
     144\end{cfa}
    128145
    129146A polymorphic function can be used in the same way as a normal function.  The
     
    132149all the variables replaced with the concrete types from the arguments) is
    133150defined at a call site.
     151\begin{cfa}
     152void do_once(int i) { ... }
     153int i;
     154do_twice(i);
     155\end{cfa}
     156Any object with a type fulfilling the assertion may be passed as an argument to
     157a @do_twice@ call.
    134158
    135159Note, a function named @do_once@ is not required in the scope of @do_twice@ to
     
    138162call.
    139163\begin{cfa}
    140 void do_once(double y) { ... } // global
     164void do_once(double y) { ... }
    141165int quadruple(int x) {
    142         void do_once(int y) { y = y * 2; } // local
    143         do_twice(x); // using local "do_once"
     166        void do_once(int y) { y = y * 2; }
     167        do_twice(x);
    144168        return x;
    145169}
     
    150174function. The matched assertion function is then passed as a function pointer
    151175to @do_twice@ and called within it.
     176The global definition of @do_once@ is ignored.
    152177
    153178To avoid typing long lists of assertions, constraints can be collect into
     
    161186and the @forall@ list in the previous example is replaced with the trait.
    162187\begin{cfa}
    163 forall(dtype T | @done_once(T)@)
     188forall(dtype T | done_once(T))
    164189\end{cfa}
    165190In general, a trait can contain an arbitrary number of assertions, both
     
    172197declarations instead of parameters, returns, and local variable declarations.
    173198\begin{cfa}
    174 forall(dtype @T@)
     199forall(dtype T)
    175200struct node {
    176         node(@T@) * next;  // generic linked node
    177         @T@ * data;
    178 }
    179 node(@int@) inode;
    180 \end{cfa}
    181 The generic type @node(T)@ is an example of a polymorphic-type usage.  Like \Cpp
    182 template usage, a polymorphic-type usage must specify a type parameter.
     201        node(T) * next;  // generic linked node
     202        T * data;
     203}
     204node(int) inode;
     205\end{cfa}
     206The generic type @node(T)@ is an example of a polymorphic type usage.  Like \Cpp
     207template usage, a polymorphic type usage must specify a type parameter.
    183208
    184209There are many other polymorphism features in \CFA but these are the ones used
     
    219244Each coroutine has a @main@ function, which takes a reference to a coroutine
    220245object and returns @void@.
    221 \begin{cfa}[numbers=left]
    222 void main(@CountUp & this@) { // argument matches trait is_coroutine
    223         unsigned int up = 0;  // retained between calls
    224         while (true) {
    225                 next = up; // make "up" available outside function
    226                 @suspend;@$\label{suspend}$
    227                 up += 1;
     246\begin{cfa}
     247void main(CountUp & this) {
     248    for (unsigned int next = 0 ; true ; ++next) {
     249                next = up;
     250                suspend;$\label{suspend}$
    228251        }
    229252}
     
    254277@mutex@.
    255278\begin{cfa}
    256 void example(MonitorA & @mutex@ argA, MonitorB & @mutex@ argB);
     279void example(MonitorA & mutex argA, MonitorB & mutex argB);
    257280\end{cfa}
    258281When the function is called, it implicitly acquires the monitor lock for all of
Note: See TracChangeset for help on using the changeset viewer.