Changeset 660665f for doc/theses/andrew_beach_MMath/existing.tex
- Timestamp:
- Jun 29, 2021, 5:35:19 PM (3 years ago)
- 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. - 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} 2 2 \label{c:existing} 3 3 … … 9 9 existing C code-base allowing programmers to learn \CFA on an as-needed basis. 10 10 11 Only those \CFA features pertaining to this thesis are discussed. Many of the12 \CFA syntactic and semantic features used in the thesis should be fairly 13 obvious to the reader.11 Only those \CFA features pertaining to this thesis are discussed. 12 Also, only new features of \CFA will be discussed, a familiarity with 13 C or C-like languages is assumed. 14 14 15 15 \section{Overloading and \lstinline{extern}} … … 29 29 // name mangling on by default 30 30 int i; // _X1ii_1 31 @extern "C"@{ // disables name mangling31 extern "C" { // disables name mangling 32 32 int j; // j 33 @extern "Cforall"@{ // enables name mangling33 extern "Cforall" { // enables name mangling 34 34 int k; // _X1ki_1 35 35 } … … 47 47 Reference-types are written the same way as a pointer-type but each 48 48 asterisk (@*@) is replaced with a ampersand (@&@); 49 this includes cv-qualifiers and multiple levels of reference, \eg: 50 49 this includes cv-qualifiers and multiple levels of reference. 50 51 Generally, references act like pointers with an implicate dereferencing 52 operation added to each use of the variable. 53 These automatic dereferences may be disabled with the address-of operator 54 (@&@). 55 56 % Check to see if these are generating errors. 51 57 \begin{minipage}{0,5\textwidth} 52 58 With references: … … 56 62 int && rri = ri; 57 63 rri = 3; 58 &ri = &j; // reference assignment64 &ri = &j; 59 65 ri = 5; 60 66 \end{cfa} … … 67 73 int ** ppi = π 68 74 **ppi = 3; 69 pi = &j; // pointer assignment75 pi = &j; 70 76 *pi = 5; 71 77 \end{cfa} 72 78 \end{minipage} 73 79 74 References are intended for cases where you would want touse pointers but would80 References are intended to be used when you would use pointers but would 75 81 be 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. 82 Mutable references may be assigned to by converting them to a pointer 83 with a @&@ and then assigning a pointer to them, as in @&ri = &j;@ above 83 84 84 85 \section{Operators} 85 86 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. 88 Operator uses are translated into function calls using these names. 89 These names are created by taking the operator symbols and joining them with 90 @?@s to show where the arguments go. 91 For example, 88 92 infixed multiplication is @?*?@ while prefix dereference is @*?@. 89 93 This syntax make it easy to tell the difference between prefix operations 90 94 (such as @++?@) and post-fix operations (@?++@). 91 95 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} 97 point ?+?(point a, point b) { return point{a.x + b.x, a.y + b.y}; } 98 bool ?==?(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} 103 Note that these special names are not limited to just being used for these 104 operator functions, and may be used name other declarations. 105 Some ``near misses", that will not match an operator form but looks like 106 it may have been supposed to, will generate wantings but otherwise they are 107 left alone. 108 109 %\subsection{Constructors and Destructors} 101 110 102 111 Both constructors and destructors are operators, which means they are 103 112 functions 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. 113 special operator names may be used to call the functions explicitly. 114 % Placement new means that this is actually equivant to C++. 115 116 The special name for a constructor is @?{}@, which comes from the 117 initialization syntax in C, \eg @Example e = { ... }@. 118 \CFA will generate a constructor call each time a variable is declared, 119 passing the initialization arguments to the constructort. 120 \begin{cfa} 121 struct Example { ... }; 122 void ?{}(Example & this) { ... } 123 { 124 Example a; 125 Example b = {}; 126 } 127 void ?{}(Example & this, char first, int num) { ... } 128 { 129 Example c = {'a', 2}; 130 } 131 \end{cfa} 132 Both @a@ and @b@ will be initalized with the first constructor, 133 while @c@ will be initalized with the second. 134 Currently, there is no general way to skip initialation. 127 135 128 136 % 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; 137 Similarly destructors use the special name @^?{}@ (the @^@ has no special 138 meaning). 139 These are a normally called implicitly called on a variable when it goes out 140 of scope. They can be called explicitly as well. 141 \begin{cfa} 142 void ^?{}(Example & this) { ... } 143 { 144 Example d; 136 145 } // <- implicit destructor call 137 146 \end{cfa} 138 147 139 Whenever a type is defined, \CFA createsa default zero-argument148 Whenever a type is defined, \CFA will create a default zero-argument 140 149 constructor, a copy constructor, a series of argument-per-field constructors 141 150 and a destructor. All user constructors are defined after this. … … 198 207 void do_once(double y) { ... } 199 208 int 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); 203 211 return x; 204 212 } 205 213 \end{cfa} 206 214 Specifically, 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 the215 argument @x@. It then looks for the most specific definition matching the 208 216 assertion, which is the nested integral @do_once@ defined within the 209 217 function. 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. 218 to @do_twice@ and called within it. 219 The 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 221 would be a better match. 222 % Aaron's thesis might be a good reference here. 212 223 213 224 To avoid typing long lists of assertions, constraints can be collect into … … 279 290 Each coroutine has a @main@ function, which takes a reference to a coroutine 280 291 object and returns @void@. 281 \begin{cfa}[numbers=left] 292 %[numbers=left] Why numbers on this one? 293 \begin{cfa} 282 294 void main(CountUp & this) { 283 295 for (unsigned int next = 0 ; true ; ++next) {
Note: See TracChangeset
for help on using the changeset viewer.