Changeset 2d8a770 for doc/theses/andrew_beach_MMath/existing.tex
- Timestamp:
- Apr 29, 2021, 11:00:18 AM (4 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 3eb55f98
- Parents:
- 3ec79f7 (diff), a6c45c6 (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
r3ec79f7 r2d8a770 16 16 to be defined~\cite{Moss18}. 17 17 \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$18 char i; int i; double i; 19 int f(); double f(); 20 void g( int ); void g( double ); 21 21 \end{cfa} 22 22 This feature requires name mangling so the assembly symbols are unique for … … 26 26 mangling is: 27 27 \begin{cfa} 28 // name mangling 28 // name mangling on by default 29 29 int i; // _X1ii_1 30 @extern "C"@ { // noname mangling30 extern "C" { // disables name mangling 31 31 int j; // j 32 @extern "Cforall"@ { //name mangling32 extern "Cforall" { // enables name mangling 33 33 int k; // _X1ki_1 34 34 } 35 // no name mangling36 } 37 // name mangling35 // revert to no name mangling 36 } 37 // revert to name mangling 38 38 \end{cfa} 39 39 Both forms of @extern@ affect all the declarations within their nested lexical … … 50 50 \begin{cfa} 51 51 int i, j; 52 int @&@ ri = i, @&&@rri = ri;52 int & ri = i, && rri = ri; 53 53 rri = 3; // auto-dereference assign to i 54 @&@ri = @&@j; // rebindable54 &ri = &j; // rebindable 55 55 ri = 5; // assign to j 56 56 \end{cfa} … … 64 64 65 65 In general, operator names in \CFA are constructed by bracketing an operator 66 token with @?@, which indicates the position of the arguments. For example, infixed67 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 (@?++@).66 token with @?@, which indicates the position of the arguments. For example, 67 infixed multiplication is @?*?@ while prefix dereference is @*?@. 68 This syntax make it easy to tell the difference between prefix operations 69 (such as @++?@) and post-fix operations (@?++@). 70 70 71 71 The 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. 72 initialization syntax in C. That initialation syntax is also the operator 73 form. \CFA will generate a constructor call each time a variable is declared, 74 passing the initialization arguments to the constructort. 75 \begin{cfa} 76 struct Example { ... }; 77 void ?{}(Example & this) { ... } 78 { 79 Example a; 80 Example b = {}; 81 } 82 void ?{}(Example & this, char first, int num) { ... } 83 { 84 Example c = {'a', 2}; 85 } 86 \end{cfa} 87 Both @a@ and @b@ will be initalized with the first constructor (there is no 88 general way to skip initialation) while @c@ will be initalized with the 89 second. 90 74 91 % 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 92 Similarly destructors use the special name @^?{}@ (the @^@ has no special 93 meaning). They can be called explicatly as well but normally they are 94 implicitly called on a variable when it goes out of scope. 95 \begin{cfa} 96 void ^?{}(Example & this) { ... } 79 97 { 80 T s = @{@ ... @}@; // same constructor/initialization braces 81 } // destructor call automatically generated82 \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} 101 No operator name is restricted in what function signatures they may be bound 102 to although most of the forms cannot be called in operator form. Some 103 ``near-misses" will generate warnings. 104 105 Whenever a type is defined, \CFA will create a default zero-argument 106 constructor, a copy constructor, a series of argument-per-field constructors 107 and a destructor. All user constructors are defined after this. 108 Because operators are never part of the type definition they may be added 109 at any time, including on built-in types. 92 110 93 111 \section{Polymorphism} … … 105 123 works on any type @T@: 106 124 \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} 125 forall( T ) T identity( T val ) { return val; } 126 int forty_two = identity( 42 ); 127 char capital_a = identity( 'A' ); 128 \end{cfa} 129 Each 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@ 131 in the second). 110 132 111 133 To allow a polymorphic function to be separately compiled, the type @T@ must be … … 115 137 types used in a function, \eg: 116 138 \begin{cfa} 117 forall( T @| { void do_once(T); }@) // assertion139 forall( T | { void do_once(T); }) 118 140 void do_twice(T value) { 119 141 do_once(value); 120 142 do_once(value); 121 143 } 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} 128 145 129 146 A polymorphic function can be used in the same way as a normal function. The … … 132 149 all the variables replaced with the concrete types from the arguments) is 133 150 defined at a call site. 151 \begin{cfa} 152 void do_once(int i) { ... } 153 int i; 154 do_twice(i); 155 \end{cfa} 156 Any object with a type fulfilling the assertion may be passed as an argument to 157 a @do_twice@ call. 134 158 135 159 Note, a function named @do_once@ is not required in the scope of @do_twice@ to … … 138 162 call. 139 163 \begin{cfa} 140 void do_once(double y) { ... } // global164 void do_once(double y) { ... } 141 165 int quadruple(int x) { 142 void do_once(int y) { y = y * 2; } // local143 do_twice(x); // using local "do_once"166 void do_once(int y) { y = y * 2; } 167 do_twice(x); 144 168 return x; 145 169 } … … 150 174 function. The matched assertion function is then passed as a function pointer 151 175 to @do_twice@ and called within it. 176 The global definition of @do_once@ is ignored. 152 177 153 178 To avoid typing long lists of assertions, constraints can be collect into … … 161 186 and the @forall@ list in the previous example is replaced with the trait. 162 187 \begin{cfa} 163 forall(dtype T | @done_once(T)@)188 forall(dtype T | done_once(T)) 164 189 \end{cfa} 165 190 In general, a trait can contain an arbitrary number of assertions, both … … 172 197 declarations instead of parameters, returns, and local variable declarations. 173 198 \begin{cfa} 174 forall(dtype @T@)199 forall(dtype T) 175 200 struct node { 176 node( @T@) * next; // generic linked node177 @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 \Cpp182 template usage, a polymorphic -type usage must specify a type parameter.201 node(T) * next; // generic linked node 202 T * data; 203 } 204 node(int) inode; 205 \end{cfa} 206 The generic type @node(T)@ is an example of a polymorphic type usage. Like \Cpp 207 template usage, a polymorphic type usage must specify a type parameter. 183 208 184 209 There are many other polymorphism features in \CFA but these are the ones used … … 219 244 Each coroutine has a @main@ function, which takes a reference to a coroutine 220 245 object 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} 247 void main(CountUp & this) { 248 for (unsigned int next = 0 ; true ; ++next) { 249 next = up; 250 suspend;$\label{suspend}$ 228 251 } 229 252 } … … 254 277 @mutex@. 255 278 \begin{cfa} 256 void example(MonitorA & @mutex@ argA, MonitorB & @mutex@argB);279 void example(MonitorA & mutex argA, MonitorB & mutex argB); 257 280 \end{cfa} 258 281 When the function is called, it implicitly acquires the monitor lock for all of
Note: See TracChangeset
for help on using the changeset viewer.