Ignore:
Timestamp:
Jun 29, 2021, 5:35:19 PM (3 years ago)
Author:
caparsons <caparson@…>
Branches:
ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
dcad80a
Parents:
5a46e09 (diff), d02e547 (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

    r5a46e09 r660665f  
    1 \chapter{\CFA Existing Features}
     1\chapter{\CFA{} Existing Features}
    22\label{c:existing}
    33
     
    99existing C code-base allowing programmers to learn \CFA on an as-needed basis.
    1010
    11 Only those \CFA features pertaining to this thesis are discussed.  Many of the
    12 \CFA syntactic and semantic features used in the thesis should be fairly
    13 obvious to the reader.
     11Only those \CFA features pertaining to this thesis are discussed.
     12Also, only new features of \CFA will be discussed, a familiarity with
     13C or C-like languages is assumed.
    1414
    1515\section{Overloading and \lstinline{extern}}
     
    2929// name mangling on by default
    3030int i; // _X1ii_1
    31 @extern "C"@ {  // disables name mangling
     31extern "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        }
     
    4747Reference-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, \eg:
    50 
     49this includes cv-qualifiers and multiple levels of reference.
     50
     51Generally, references act like pointers with an implicate dereferencing
     52operation added to each use of the variable.
     53These automatic dereferences may be disabled with the address-of operator
     54(@&@).
     55
     56% Check to see if these are generating errors.
    5157\begin{minipage}{0,5\textwidth}
    5258With references:
     
    5662int && rri = ri;
    5763rri = 3;
    58 &ri = &j; // reference assignment
     64&ri = &j;
    5965ri = 5;
    6066\end{cfa}
     
    6773int ** ppi = &pi;
    6874**ppi = 3;
    69 pi = &j; // pointer assignment
     75pi = &j;
    7076*pi = 5;
    7177\end{cfa}
    7278\end{minipage}
    7379
    74 References are intended for cases where you would want to use pointers but would
     80References are intended to be used when you would use pointers but would
    7581be dereferencing them (almost) every usage.
    76 In most cases a reference can just be thought of as a pointer that
    77 automatically puts a dereference in front of each of its uses (per-level of
    78 reference).
    79 The address-of operator (@&@) acts as an escape and removes one of the
    80 automatic dereference operations.
    81 Mutable references may be assigned by converting them to a pointer
    82 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above.
     82Mutable references may be assigned to by converting them to a pointer
     83with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above
    8384
    8485\section{Operators}
    8586
    86 In general, operator names in \CFA are constructed by bracketing an operator
    87 token with @?@, which indicates the position of the arguments. For example,
     87\CFA implements operator overloading by providing special names.
     88Operator uses are translated into function calls using these names.
     89These names are created by taking the operator symbols and joining them with
     90@?@s to show where the arguments go.
     91For example,
    8892infixed multiplication is @?*?@ while prefix dereference is @*?@.
    8993This syntax make it easy to tell the difference between prefix operations
    9094(such as @++?@) and post-fix operations (@?++@).
    9195
    92 An operator name may describe any function signature (it is just a name) but
    93 only certain signatures may be called in operator form.
    94 \begin{cfa}
    95 int ?+?( int i, int j, int k ) { return i + j + k; }
    96 {
    97         sout | ?+?( 3, 4, 5 ); // no infix form
    98 }
    99 \end{cfa}
    100 Some ``near-misses" for unary/binary operator prototypes generate warnings.
     96\begin{cfa}
     97point ?+?(point a, point b) { return point{a.x + b.x, a.y + b.y}; }
     98bool ?==?(point a, point b) { return a.x == b.x && a.y == b.y; }
     99{
     100        assert(point{1, 2} + point{3, 4} == point{4, 6});
     101}
     102\end{cfa}
     103Note that these special names are not limited to just being used for these
     104operator functions, and may be used name other declarations.
     105Some ``near misses", that will not match an operator form but looks like
     106it may have been supposed to, will generate wantings but otherwise they are
     107left alone.
     108
     109%\subsection{Constructors and Destructors}
    101110
    102111Both constructors and destructors are operators, which means they are
    103112functions with special operator names rather than type names in \Cpp. The
    104 special operator names may be used to call the functions explicitly (not
    105 allowed in \Cpp for constructors).
    106 
    107 The special name for a constructor is @?{}@, where the name @{}@ comes from the
    108 initialization 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,
    111 passing the initialization arguments to the constructor.
    112 \begin{cfa}
    113 struct Structure { ... };
    114 void ?{}(Structure & this) { ... }
    115 {
    116         Structure a;
    117         Structure b = {};
    118 }
    119 void ?{}(Structure & this, char first, int num) { ... }
    120 {
    121         Structure c = {'a', 2};
    122 }
    123 \end{cfa}
    124 Both @a@ and @b@ are initialized with the first constructor,
    125 while @c@ is initialized with the second.
    126 Currently, there is no general way to skip initialization.
     113special operator names may be used to call the functions explicitly.
     114% Placement new means that this is actually equivant to C++.
     115
     116The special name for a constructor is @?{}@, which comes from the
     117initialization syntax in C, \eg @Example e = { ... }@.
     118\CFA will generate a constructor call each time a variable is declared,
     119passing the initialization arguments to the constructort.
     120\begin{cfa}
     121struct Example { ... };
     122void ?{}(Example & this) { ... }
     123{
     124        Example a;
     125        Example b = {};
     126}
     127void ?{}(Example & this, char first, int num) { ... }
     128{
     129        Example c = {'a', 2};
     130}
     131\end{cfa}
     132Both @a@ and @b@ will be initalized with the first constructor,
     133while @c@ will be initalized with the second.
     134Currently, there is no general way to skip initialation.
    127135
    128136% I don't like the \^{} symbol but $^\wedge$ isn't better.
    129 Similarly, destructors use the special name @^?{}@ (the @^@ has no special
    130 meaning).  Normally, they are implicitly called on a variable when it goes out
    131 of scope but they can be called explicitly as well.
    132 \begin{cfa}
    133 void ^?{}(Structure & this) { ... }
    134 {
    135         Structure d;
     137Similarly destructors use the special name @^?{}@ (the @^@ has no special
     138meaning).
     139These are a normally called implicitly called on a variable when it goes out
     140of scope. They can be called explicitly as well.
     141\begin{cfa}
     142void ^?{}(Example & this) { ... }
     143{
     144        Example d;
    136145} // <- implicit destructor call
    137146\end{cfa}
    138147
    139 Whenever a type is defined, \CFA creates a default zero-argument
     148Whenever a type is defined, \CFA will create a default zero-argument
    140149constructor, a copy constructor, a series of argument-per-field constructors
    141150and a destructor. All user constructors are defined after this.
     
    198207void do_once(double y) { ... }
    199208int quadruple(int 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
     209        void do_once(int & y) { y = y * 2; }
     210        do_twice(x);
    203211        return x;
    204212}
    205213\end{cfa}
    206214Specifically, the complier deduces that @do_twice@'s T is an integer from the
    207 argument @x@. It then looks for the most \emph{specific} definition matching the
     215argument @x@. It then looks for the most specific definition matching the
    208216assertion, which is the nested integral @do_once@ defined within the
    209217function. The matched assertion function is then passed as a function pointer
    210 to @do_twice@ and called within it.  The global definition of @do_once@ is used
    211 for the second call because the float-point argument is a better match.
     218to @do_twice@ and called within it.
     219The global definition of @do_once@ is ignored, however if quadruple took a
     220@double@ argument then the global definition would be used instead as it
     221would be a better match.
     222% Aaron's thesis might be a good reference here.
    212223
    213224To avoid typing long lists of assertions, constraints can be collect into
     
    279290Each coroutine has a @main@ function, which takes a reference to a coroutine
    280291object and returns @void@.
    281 \begin{cfa}[numbers=left]
     292%[numbers=left] Why numbers on this one?
     293\begin{cfa}
    282294void main(CountUp & this) {
    283295        for (unsigned int next = 0 ; true ; ++next) {
Note: See TracChangeset for help on using the changeset viewer.