Changeset 4ed7946e for doc/theses/andrew_beach_MMath/existing.tex
- Timestamp:
- Jun 4, 2021, 11:24:41 AM (3 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/existing.tex
r553f8abe r4ed7946e 2 2 \label{c:existing} 3 3 4 \CFA (C-for-all)~\cite{Cforall}is an open-source project extending ISO C with4 \CFA is an open-source project extending ISO C with 5 5 modern safety and productivity features, while still ensuring backwards 6 6 compatibility with C and its programmers. \CFA is designed to have an … … 9 9 existing C code-base allowing programmers to learn \CFA on an as-needed basis. 10 10 11 Only those \CFA features pert inentto this thesis are discussed. Many of the11 Only those \CFA features pertaining to this thesis are discussed. Many of the 12 12 \CFA syntactic and semantic features used in the thesis should be fairly 13 13 obvious to the reader. … … 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 } … … 45 45 \CFA adds a reference type to C as an auto-dereferencing pointer. 46 46 They work very similarly to pointers. 47 Reference-types are written the same way as a pointer-type isbut each47 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. 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} 49 this includes cv-qualifiers and multiple levels of reference, \eg: 50 51 \begin{minipage}{0,5\textwidth} 62 52 With references: 63 53 \begin{cfa} … … 66 56 int && rri = ri; 67 57 rri = 3; 68 &ri = &j; 58 &ri = &j; // reference assignment 69 59 ri = 5; 70 60 \end{cfa} 71 61 \end{minipage} 72 \begin{minipage}{0, 45\textwidth}62 \begin{minipage}{0,5\textwidth} 73 63 With pointers: 74 64 \begin{cfa} … … 77 67 int ** ppi = π 78 68 **ppi = 3; 79 pi = &j; 69 pi = &j; // pointer assignment 80 70 *pi = 5; 81 71 \end{cfa} 82 72 \end{minipage} 83 73 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). 74 References are intended for cases where you would want to use pointers but would 75 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. 83 84 \section{Operators} 90 85 91 86 In general, operator names in \CFA are constructed by bracketing an operator … … 95 90 (such as @++?@) and post-fix operations (@?++@). 96 91 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. 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. 101 102 Both constructors and destructors are operators, which means they are 103 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. 116 127 117 128 % I don't like the \^{} symbol but $^\wedge$ isn't better. 118 Similarly destructors use the special name @^?{}@ (the @^@ has no special119 meaning). They can be called explicatly as well but normally they are120 implicitly called on a variable when it goes out of scope.121 \begin{cfa} 122 void ^?{}( Example & this) { ... }123 { 124 Example d;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; 125 136 } // <- implicit destructor call 126 137 \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 139 Whenever a type is defined, \CFA creates a default zero-argument 132 140 constructor, a copy constructor, a series of argument-per-field constructors 133 141 and a destructor. All user constructors are defined after this. … … 153 161 char capital_a = identity( 'A' ); 154 162 \end{cfa} 155 Each use of a polymorphic declaration will resolveits polymorphic parameters163 Each use of a polymorphic declaration resolves its polymorphic parameters 156 164 (in this case, just @T@) to concrete types (@int@ in the first use and @char@ 157 165 in the second). … … 159 167 To allow a polymorphic function to be separately compiled, the type @T@ must be 160 168 constrained by the operations used on @T@ in the function body. The @forall@ 161 clause sis augmented with a list of polymorphic variables (local type names)169 clause is augmented with a list of polymorphic variables (local type names) 162 170 and assertions (constraints), which represent the required operations on those 163 171 types used in a function, \eg: 164 172 \begin{cfa} 165 forall( T | { void do_once(T); } )173 forall( T | { void do_once(T); } ) 166 174 void do_twice(T value) { 167 175 do_once(value); … … 190 198 void do_once(double y) { ... } 191 199 int 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 194 203 return x; 195 204 } 196 205 \end{cfa} 197 206 Specifically, the complier deduces that @do_twice@'s T is an integer from the 198 argument @x@. It then looks for the most specificdefinition matching the207 argument @x@. It then looks for the most \emph{specific} definition matching the 199 208 assertion, which is the nested integral @do_once@ defined within the 200 209 function. 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.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. 203 212 204 213 To avoid typing long lists of assertions, constraints can be collect into … … 270 279 Each coroutine has a @main@ function, which takes a reference to a coroutine 271 280 object and returns @void@. 272 \begin{cfa} 281 \begin{cfa}[numbers=left] 273 282 void main(CountUp & this) { 274 283 for (unsigned int next = 0 ; true ; ++next) { 275 284 next = up; 276 285 suspend;$\label{suspend}$
Note: See TracChangeset
for help on using the changeset viewer.