Changeset 382edbe
- Timestamp:
- Jun 8, 2021, 11:44:03 AM (3 years ago)
- Branches:
- ADT, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 471ff17
- Parents:
- 21f2e92
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/andrew_beach_MMath/existing.tex
r21f2e92 r382edbe 1 \chapter{\CFA Existing Features}1 \chapter{\CFA{} Existing Features} 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 inent 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 basic familiarity with 13 C or C-like languages is assumed. 14 14 15 15 \section{Overloading and \lstinline{extern}} … … 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 49 this includes cv-qualifiers and multiple levels of reference. 50 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} 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. 57 \begin{minipage}{0,5\textwidth} 62 58 With references: 63 59 \begin{cfa} … … 70 66 \end{cfa} 71 67 \end{minipage} 72 \begin{minipage}{0, 45\textwidth}68 \begin{minipage}{0,5\textwidth} 73 69 With pointers: 74 70 \begin{cfa} … … 82 78 \end{minipage} 83 79 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). 90 91 In general, operator names in \CFA are constructed by bracketing an operator 92 token with @?@, which indicates the position of the arguments. For example, 80 References are intended to be used when you would use pointers but would 81 be dereferencing them (almost) every usage. 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 84 85 \section{Operators} 86 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 @?@ where the arguments would go. 91 For example, 93 92 infixed multiplication is @?*?@ while prefix dereference is @*?@. 94 93 This syntax make it easy to tell the difference between prefix operations 95 94 (such as @++?@) and post-fix operations (@?++@). 96 95 96 \begin{cfa} 97 int ?+?(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} 110 111 Both constructors and destructors are operators, which means they are 112 functions with special operator names rather than type names in \Cpp. The 113 special operator names may be used to call the functions explicitly. 114 % Placement new means that this is actually equivant to C++. 115 97 116 The special name for a constructor is @?{}@, which comes from the 98 initialization syntax in C . That initialation syntax is also the operator99 form.\CFA will generate a constructor call each time a variable is declared,117 initialization syntax in C, \eg @Example e = { ... }@. 118 \CFA will generate a constructor call each time a variable is declared, 100 119 passing the initialization arguments to the constructort. 101 120 \begin{cfa} … … 111 130 } 112 131 \end{cfa} 113 Both @a@ and @b@ will be initalized with the first constructor (there is no114 general way to skip initialation) while @c@ will be initalized with the 115 second.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. 116 135 117 136 % I don't like the \^{} symbol but $^\wedge$ isn't better. 118 137 Similarly destructors use the special name @^?{}@ (the @^@ has no special 119 meaning). They can be called explicatly as well but normally they are 120 implicitly called on a variable when it goes out of scope. 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. 121 141 \begin{cfa} 122 142 void ^?{}(Example & this) { ... } 123 143 { 124 144 Example d; 125 145 } // <- implicit destructor call 126 146 \end{cfa} 127 No operator name is restricted in what function signatures they may be bound128 to although most of the forms cannot be called in operator form. Some129 ``near-misses" will generate warnings.130 147 131 148 Whenever a type is defined, \CFA will create a default zero-argument … … 153 170 char capital_a = identity( 'A' ); 154 171 \end{cfa} 155 Each use of a polymorphic declaration will resolveits polymorphic parameters172 Each use of a polymorphic declaration resolves its polymorphic parameters 156 173 (in this case, just @T@) to concrete types (@int@ in the first use and @char@ 157 174 in the second). … … 159 176 To allow a polymorphic function to be separately compiled, the type @T@ must be 160 177 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)178 clause is augmented with a list of polymorphic variables (local type names) 162 179 and assertions (constraints), which represent the required operations on those 163 180 types used in a function, \eg: 164 181 \begin{cfa} 165 forall( T | { void do_once(T); } )182 forall( T | { void do_once(T); } ) 166 183 void do_twice(T value) { 167 184 do_once(value); … … 190 207 void do_once(double y) { ... } 191 208 int quadruple(int x) { 192 void do_once(int y) { y = y * 2; }209 void do_once(int & y) { y = y * 2; } 193 210 do_twice(x); 194 211 return x; … … 200 217 function. The matched assertion function is then passed as a function pointer 201 218 to @do_twice@ and called within it. 202 The global definition of @do_once@ is ignored. 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. 203 223 204 224 To avoid typing long lists of assertions, constraints can be collect into … … 270 290 Each coroutine has a @main@ function, which takes a reference to a coroutine 271 291 object and returns @void@. 292 %[numbers=left] Why numbers on this one? 272 293 \begin{cfa} 273 294 void main(CountUp & this) { 274 295 for (unsigned int next = 0 ; true ; ++next) { 275 296 next = up; 276 297 suspend;$\label{suspend}$
Note: See TracChangeset
for help on using the changeset viewer.