| [7493339] | 1 | %======================================================================
 | 
|---|
 | 2 | \chapter{Variadic Functions}
 | 
|---|
 | 3 | %======================================================================
 | 
|---|
 | 4 | 
 | 
|---|
| [f92aa32] | 5 | \section{Design Criteria} % TODO: better section name???
 | 
|---|
| [7493339] | 6 | C provides variadic functions through the manipulation of @va_list@ objects.
 | 
|---|
| [12d3187] | 7 | In C, a variadic function is one which contains at least one parameter, followed by @...@ as the last token in the parameter list.
 | 
|---|
 | 8 | In particular, some form of \emph{argument descriptor} or \emph{sentinel value} is needed to inform the function of the number of arguments and their types.
 | 
|---|
| [7493339] | 9 | Two common argument descriptors are format strings or counter parameters.
 | 
|---|
| [12d3187] | 10 | It is important to note that both of these mechanisms are inherently redundant, because they require the user to explicitly specify information that the compiler already knows \footnote{While format specifiers can convey some information the compiler does not know, such as whether to print a number in decimal or hexadecimal, the number of arguments is wholly redundant.}.
 | 
|---|
| [7493339] | 11 | This required repetition is error prone, because it is easy for the user to add or remove arguments without updating the argument descriptor.
 | 
|---|
 | 12 | In addition, C requires the programmer to hard code all of the possible expected types.
 | 
|---|
 | 13 | As a result, it is cumbersome to write a function that is open to extension.
 | 
|---|
| [0eb18557] | 14 | For example, a simple function to sum $N$ @int@s,
 | 
|---|
| [7493339] | 15 | \begin{cfacode}
 | 
|---|
 | 16 | int sum(int N, ...) {
 | 
|---|
 | 17 |   va_list args;
 | 
|---|
 | 18 |   va_start(args, N);
 | 
|---|
 | 19 |   int ret = 0;
 | 
|---|
 | 20 |   while(N) {
 | 
|---|
 | 21 |     ret += va_arg(args, int);  // have to specify type
 | 
|---|
 | 22 |     N--;
 | 
|---|
 | 23 |   }
 | 
|---|
 | 24 |   va_end(args);
 | 
|---|
 | 25 |   return ret;
 | 
|---|
 | 26 | }
 | 
|---|
 | 27 | sum(3, 10, 20, 30);  // need to keep counter in sync
 | 
|---|
 | 28 | \end{cfacode}
 | 
|---|
| [0eb18557] | 29 | The @va_list@ type is a special C data type that abstracts variadic-argument manipulation.
 | 
|---|
| [7493339] | 30 | The @va_start@ macro initializes a @va_list@, given the last named parameter.
 | 
|---|
 | 31 | Each use of the @va_arg@ macro allows access to the next variadic argument, given a type.
 | 
|---|
 | 32 | Since the function signature does not provide any information on what types can be passed to a variadic function, the compiler does not perform any error checks on a variadic call.
 | 
|---|
 | 33 | As such, it is possible to pass any value to the @sum@ function, including pointers, floating-point numbers, and structures.
 | 
|---|
 | 34 | In the case where the provided type is not compatible with the argument's actual type after default argument promotions, or if too many arguments are accessed, the behaviour is undefined \cite[p.~81]{C11}.
 | 
|---|
 | 35 | Furthermore, there is no way to perform the necessary error checks in the @sum@ function at run-time, since type information is not carried into the function body.
 | 
|---|
| [0eb18557] | 36 | Since they rely on programmer convention rather than compile-time checks, variadic functions are unsafe.
 | 
|---|
| [7493339] | 37 | 
 | 
|---|
 | 38 | In practice, compilers can provide warnings to help mitigate some of the problems.
 | 
|---|
| [0eb18557] | 39 | For example, GCC provides the @format@ attribute to specify that a function uses a format string, which allows the compiler to perform some checks related to the standard format-specifiers.
 | 
|---|
 | 40 | Unfortunately, this approach does not permit extensions to the format-string syntax, so a programmer cannot extend the attribute to warn for mismatches with custom types.
 | 
|---|
| [7493339] | 41 | 
 | 
|---|
 | 42 | As a result, C's variadic functions are a deficient language feature.
 | 
|---|
 | 43 | Two options were examined to provide better, type-safe variadic functions in \CFA.
 | 
|---|
 | 44 | \subsection{Whole Tuple Matching}
 | 
|---|
 | 45 | Option 1 is to change the argument matching algorithm, so that type parameters can match whole tuples, rather than just their components.
 | 
|---|
 | 46 | This option could be implemented with two phases of argument matching when a function contains type parameters and the argument list contains tuple arguments.
 | 
|---|
 | 47 | If flattening and structuring fail to produce a match, a second attempt at matching the function and argument combination is made where tuple arguments are not expanded and structure must match exactly, modulo non-tuple implicit conversions.
 | 
|---|
 | 48 | For example:
 | 
|---|
 | 49 | \begin{cfacode}
 | 
|---|
 | 50 |   forall(otype T, otype U | { T g(U); })
 | 
|---|
 | 51 |   void f(T, U);
 | 
|---|
 | 52 | 
 | 
|---|
 | 53 |   [int, int] g([int, int, int, int]);
 | 
|---|
 | 54 | 
 | 
|---|
 | 55 |   f([1, 2], [3, 4, 5, 6]);
 | 
|---|
 | 56 | \end{cfacode}
 | 
|---|
 | 57 | With flattening and structuring, the call is first transformed into @f(1, 2, 3, 4, 5, 6)@.
 | 
|---|
 | 58 | Since the first argument of type @T@ does not have a tuple type, unification decides that @T=int@ and @1@ is matched as the first parameter.
 | 
|---|
 | 59 | Likewise, @U@ does not have a tuple type, so @U=int@ and @2@ is accepted as the second parameter.
 | 
|---|
 | 60 | There are now no remaining formal parameters, but there are remaining arguments and the function is not variadic, so the match fails.
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 | With the addition of an exact matching attempt, @T=[int,int]@ and @U=[int,int,int,int]@, and so the arguments type check.
 | 
|---|
 | 63 | Likewise, when inferring assertion @g@, an exact match is found.
 | 
|---|
 | 64 | 
 | 
|---|
| [f92aa32] | 65 | This approach is strict with respect to argument structure, by nature, which makes it syntactically awkward to use in ways that the existing tuple design is not.
 | 
|---|
 | 66 | For example, consider a @new@ function that allocates memory using @malloc@, and constructs the result using arbitrary arguments.
 | 
|---|
| [7493339] | 67 | \begin{cfacode}
 | 
|---|
 | 68 | struct Array;
 | 
|---|
 | 69 | void ?{}(Array *, int, int, int);
 | 
|---|
 | 70 | 
 | 
|---|
 | 71 | forall(dtype T, otype Params | sized(T) | { void ?{}(T *, Params); })
 | 
|---|
 | 72 | T * new(Params p) {
 | 
|---|
 | 73 |   return malloc(){ p };
 | 
|---|
 | 74 | }
 | 
|---|
 | 75 | Array(int) * x = new([1, 2, 3]);
 | 
|---|
 | 76 | \end{cfacode}
 | 
|---|
 | 77 | The call to @new@ is not particularly appealing, since it requires the use of square brackets at the call-site, which is not required in any other function call.
 | 
|---|
 | 78 | This shifts the burden from the compiler to the programmer, which is almost always wrong, and creates an odd inconsistency within the language.
 | 
|---|
 | 79 | Similarly, in order to pass 0 variadic arguments, an explicit empty tuple must be passed into the argument list, otherwise the exact matching rule would not have an argument to bind against.
 | 
|---|
 | 80 | 
 | 
|---|
| [0eb18557] | 81 | It should be otherwise noted that the addition of an exact matching rule only affects the outcome for polymorphic type-binding when tuples are involved.
 | 
|---|
| [7493339] | 82 | For non-tuple arguments, exact matching and flattening and structuring are equivalent.
 | 
|---|
| [0eb18557] | 83 | For tuple arguments to a function without polymorphic formal-parameters, flattening and structuring work whenever an exact match would have worked, since the tuple is flattened and implicitly restructured to its original structure.
 | 
|---|
| [7493339] | 84 | Thus there is nothing to be gained from permitting the exact matching rule to take effect when a function does not contain polymorphism and none of the arguments are tuples.
 | 
|---|
 | 85 | 
 | 
|---|
 | 86 | Overall, this option takes a step in the right direction, but is contrary to the flexibility of the existing tuple design.
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 | \subsection{A New Typeclass}
 | 
|---|
 | 89 | A second option is the addition of another kind of type parameter, @ttype@.
 | 
|---|
 | 90 | Matching against a @ttype@ parameter consumes all remaining argument components and packages them into a tuple, binding to the resulting tuple of types.
 | 
|---|
 | 91 | In a given parameter list, there should be at most one @ttype@ parameter that must occur last, otherwise the call can never resolve, given the previous rule.
 | 
|---|
 | 92 | This idea essentially matches normal variadic semantics, with a strong feeling of similarity to \CCeleven variadic templates.
 | 
|---|
 | 93 | As such, @ttype@ variables are also referred to as argument packs.
 | 
|---|
 | 94 | This approach is the option that has been added to \CFA.
 | 
|---|
 | 95 | 
 | 
|---|
 | 96 | Like variadic templates, the main way to manipulate @ttype@ polymorphic functions is through recursion.
 | 
|---|
 | 97 | Since nothing is known about a parameter pack by default, assertion parameters are key to doing anything meaningful.
 | 
|---|
 | 98 | Unlike variadic templates, @ttype@ polymorphic functions can be separately compiled.
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 | For example, a simple translation of the C sum function using @ttype@ is
 | 
|---|
 | 101 | \begin{cfacode}
 | 
|---|
 | 102 | int sum(void){ return 0; }        // (0)
 | 
|---|
 | 103 | forall(ttype Params | { int sum(Params); })
 | 
|---|
 | 104 | int sum(int x, Params rest) { // (1)
 | 
|---|
 | 105 |   return x+sum(rest);
 | 
|---|
 | 106 | }
 | 
|---|
 | 107 | sum(10, 20, 30);
 | 
|---|
 | 108 | \end{cfacode}
 | 
|---|
 | 109 | Since (0) does not accept any arguments, it is not a valid candidate function for the call @sum(10, 20, 30)@.
 | 
|---|
 | 110 | In order to call (1), @10@ is matched with @x@, and the argument resolution moves on to the argument pack @rest@, which consumes the remainder of the argument list and @Params@ is bound to @[20, 30]@.
 | 
|---|
 | 111 | In order to finish the resolution of @sum@, an assertion parameter that matches @int sum(int, int)@ is required.
 | 
|---|
| [f92aa32] | 112 | Like in the previous iteration, (0) is not a valid candidate, so (1) is examined with @Params@ bound to @[int]@, requiring the assertion @int sum(int)@.
 | 
|---|
| [7493339] | 113 | Next, (0) fails, and to satisfy (1) @Params@ is bound to @[]@, requiring an assertion @int sum()@.
 | 
|---|
 | 114 | Finally, (0) matches and (1) fails, which terminates the recursion.
 | 
|---|
 | 115 | Effectively, this traces as @sum(10, 20, 30)@ $\rightarrow$ @10+sum(20, 30)@ $\rightarrow$ @10+(20+sum(30))@ $\rightarrow$ @10+(20+(30+sum()))@ $\rightarrow$ @10+(20+(30+0))@.
 | 
|---|
 | 116 | 
 | 
|---|
 | 117 | Interestingly, this version does not require any form of argument descriptor, since the \CFA type system keeps track of all of these details.
 | 
|---|
 | 118 | It might be reasonable to take the @sum@ function a step further to enforce a minimum number of arguments, which could be done simply
 | 
|---|
 | 119 | \begin{cfacode}
 | 
|---|
 | 120 | int sum(int x, int y){
 | 
|---|
 | 121 |   return x+y;
 | 
|---|
 | 122 | }
 | 
|---|
 | 123 | forall(ttype Params | { int sum(int, Params); })
 | 
|---|
 | 124 | int sum(int x, int y, Params rest) {
 | 
|---|
 | 125 |   return sum(x+y, rest);
 | 
|---|
 | 126 | }
 | 
|---|
 | 127 | sum(10);          // invalid
 | 
|---|
 | 128 | sum(10, 20);      // valid
 | 
|---|
 | 129 | sum(10, 20, 30);  // valid
 | 
|---|
 | 130 | ...
 | 
|---|
 | 131 | \end{cfacode}
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 | One more iteration permits the summation of any summable type, as long as all arguments are the same type.
 | 
|---|
 | 134 | \begin{cfacode}
 | 
|---|
 | 135 | trait summable(otype T) {
 | 
|---|
 | 136 |   T ?+?(T, T);
 | 
|---|
 | 137 | };
 | 
|---|
 | 138 | forall(otype R | summable(R))
 | 
|---|
 | 139 | R sum(R x, R y){
 | 
|---|
 | 140 |   return x+y;
 | 
|---|
 | 141 | }
 | 
|---|
 | 142 | forall(otype R, ttype Params
 | 
|---|
 | 143 |   | summable(R)
 | 
|---|
 | 144 |   | { R sum(R, Params); })
 | 
|---|
 | 145 | R sum(R x, R y, Params rest) {
 | 
|---|
 | 146 |   return sum(x+y, rest);
 | 
|---|
 | 147 | }
 | 
|---|
 | 148 | sum(3, 10, 20, 30);
 | 
|---|
 | 149 | \end{cfacode}
 | 
|---|
 | 150 | Unlike C, it is not necessary to hard code the expected type.
 | 
|---|
 | 151 | This @sum@ function is naturally open to extension, in that any user-defined type with a @?+?@ operator is automatically able to be used with the @sum@ function.
 | 
|---|
 | 152 | That is to say, the programmer who writes @sum@ does not need full program knowledge of every possible data type, unlike what is necessary to write an equivalent function using the standard C mechanisms.
 | 
|---|
 | 153 | 
 | 
|---|
| [12d3187] | 154 | \begin{sloppypar}
 | 
|---|
| [7493339] | 155 | Going one last step, it is possible to achieve full generality in \CFA, allowing the summation of arbitrary lists of summable types.
 | 
|---|
 | 156 | \begin{cfacode}
 | 
|---|
 | 157 | trait summable(otype T1, otype T2, otype R) {
 | 
|---|
 | 158 |   R ?+?(T1, T2);
 | 
|---|
 | 159 | };
 | 
|---|
 | 160 | forall(otype T1, otype T2, otype R | summable(T1, T2, R))
 | 
|---|
 | 161 | R sum(T1 x, T2 y) {
 | 
|---|
 | 162 |   return x+y;
 | 
|---|
 | 163 | }
 | 
|---|
| [0eb18557] | 164 | forall(otype T1, otype T2, otype T3, otype R, ttype Params
 | 
|---|
| [7493339] | 165 |   | summable(T1, T2, T3)
 | 
|---|
 | 166 |   | { R sum(T3, Params); })
 | 
|---|
 | 167 | R sum(T1 x, T2 y, Params rest ) {
 | 
|---|
 | 168 |   return sum(x+y, rest);
 | 
|---|
 | 169 | }
 | 
|---|
 | 170 | sum(3, 10.5, 20, 30.3);
 | 
|---|
 | 171 | \end{cfacode}
 | 
|---|
 | 172 | The \CFA translator requires adding explicit @double ?+?(int, double)@ and @double ?+?(double, int)@ functions for this call to work, since implicit conversions are not supported for assertions.
 | 
|---|
| [12d3187] | 173 | \end{sloppypar}
 | 
|---|
| [7493339] | 174 | 
 | 
|---|
 | 175 | A notable limitation of this approach is that it heavily relies on recursive assertions.
 | 
|---|
 | 176 | The \CFA translator imposes a limitation on the depth of the recursion for assertion satisfaction.
 | 
|---|
| [f92aa32] | 177 | Currently, the limit is set to 4, which means that the first version of the @sum@ function is limited to at most 5 arguments, while the second version can support up to 6 arguments.
 | 
|---|
| [7493339] | 178 | The limit is set low due to inefficiencies in the current implementation of the \CFA expression resolver.
 | 
|---|
 | 179 | There is ongoing work to improve the performance of the resolver, and with noticeable gains, the limit can be relaxed to allow longer argument lists to @ttype@ functions.
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 | C variadic syntax and @ttype@ polymorphism probably should not be mixed, since it is not clear where to draw the line to decide which arguments belong where.
 | 
|---|
| [f92aa32] | 182 | Furthermore, it might be desirable to disallow polymorphic functions to use C variadic syntax to encourage a \CFA style.
 | 
|---|
| [7493339] | 183 | Aside from calling C variadic functions, it is not obvious that there is anything that can be done with C variadics that could not also be done with @ttype@ parameters.
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 | Variadic templates in \CC require an ellipsis token to express that a parameter is a parameter pack and to expand a parameter pack.
 | 
|---|
 | 186 | \CFA does not need an ellipsis in either case, since the type class @ttype@ is only used for variadics.
 | 
|---|
 | 187 | An alternative design is to use an ellipsis combined with an existing type class.
 | 
|---|
| [0eb18557] | 188 | This approach was not taken because the largest benefit of the ellipsis token in \CC is the ability to expand a parameter pack within an expression, \eg, in fold expressions, which requires compile-time knowledge of the structure of the parameter pack, which is not available in \CFA.
 | 
|---|
| [7493339] | 189 | \begin{cppcode}
 | 
|---|
 | 190 | template<typename... Args>
 | 
|---|
 | 191 | void f(Args &... args) {
 | 
|---|
 | 192 |   g(&args...);  // expand to addresses of pack elements
 | 
|---|
 | 193 | }
 | 
|---|
 | 194 | \end{cppcode}
 | 
|---|
 | 195 | As such, the addition of an ellipsis token would be purely an aesthetic change in \CFA today.
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 | It is possible to write a type-safe variadic print routine, which can replace @printf@
 | 
|---|
 | 198 | \begin{cfacode}
 | 
|---|
 | 199 | struct S { int x, y; };
 | 
|---|
 | 200 | forall(otype T, ttype Params |
 | 
|---|
 | 201 |   { void print(T); void print(Params); })
 | 
|---|
 | 202 | void print(T arg, Params rest) {
 | 
|---|
 | 203 |   print(arg);
 | 
|---|
 | 204 |   print(rest);
 | 
|---|
 | 205 | }
 | 
|---|
 | 206 | void print(char * x) { printf("%s", x); }
 | 
|---|
 | 207 | void print(int x) { printf("%d", x);  }
 | 
|---|
 | 208 | void print(S s) { print("{ ", s.x, ",", s.y, " }"); }
 | 
|---|
 | 209 | print("s = ", (S){ 1, 2 }, "\n");
 | 
|---|
 | 210 | \end{cfacode}
 | 
|---|
 | 211 | This example routine showcases a variadic-template-like decomposition of the provided argument list.
 | 
|---|
 | 212 | The individual @print@ routines allow printing a single element of a type.
 | 
|---|
 | 213 | The polymorphic @print@ allows printing any list of types, as long as each individual type has a @print@ function.
 | 
|---|
 | 214 | The individual print functions can be used to build up more complicated @print@ routines, such as for @S@, which is something that cannot be done with @printf@ in C.
 | 
|---|
 | 215 | 
 | 
|---|
 | 216 | It is also possible to use @ttype@ polymorphism to provide arbitrary argument forwarding functions.
 | 
|---|
 | 217 | For example, it is possible to write @new@ as a library function.
 | 
|---|
 | 218 | \begin{cfacode}
 | 
|---|
 | 219 | struct Array;
 | 
|---|
 | 220 | void ?{}(Array *, int, int, int);
 | 
|---|
 | 221 | 
 | 
|---|
 | 222 | forall(dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); })
 | 
|---|
 | 223 | T * new(Params p) {
 | 
|---|
 | 224 |   return malloc(){ p }; // construct result of malloc
 | 
|---|
 | 225 | }
 | 
|---|
 | 226 | Array * x = new(1, 2, 3);
 | 
|---|
 | 227 | \end{cfacode}
 | 
|---|
 | 228 | In the call to @new@, @Array@ is selected to match @T@, and @Params@ is expanded to match @[int, int, int, int]@. To satisfy the assertions, a constructor with an interface compatible with @void ?{}(Array *, int, int, int)@ must exist in the current scope.
 | 
|---|
 | 229 | 
 | 
|---|
| [12d3187] | 230 | The @new@ function provides the combination of polymorphic @malloc@ with a constructor call, so that it becomes impossible to forget to construct dynamically-allocated objects.
 | 
|---|
| [0eb18557] | 231 | This approach provides the type-safety of @new@ in \CC, without the need to specify the allocated type, thanks to return-type inference.
 | 
|---|
 | 232 | 
 | 
|---|
| [7493339] | 233 | \section{Implementation}
 | 
|---|
 | 234 | 
 | 
|---|
 | 235 | The definition of @new@
 | 
|---|
 | 236 | \begin{cfacode}
 | 
|---|
 | 237 | forall(dtype T | sized(T)) T * malloc();
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 | forall(dtype T, ttype Params | sized(T) | { void ?{}(T *, Params); })
 | 
|---|
 | 240 | T * new(Params p) {
 | 
|---|
 | 241 |   return malloc(){ p }; // construct result of malloc
 | 
|---|
 | 242 | }
 | 
|---|
 | 243 | \end{cfacode}
 | 
|---|
| [0eb18557] | 244 | generates the following
 | 
|---|
| [7493339] | 245 | \begin{cfacode}
 | 
|---|
 | 246 | void *malloc(long unsigned int _sizeof_T, long unsigned int _alignof_T);
 | 
|---|
 | 247 | 
 | 
|---|
 | 248 | void *new(
 | 
|---|
 | 249 |   void (*_adapter_)(void (*)(), void *, void *),
 | 
|---|
 | 250 |   long unsigned int _sizeof_T,
 | 
|---|
 | 251 |   long unsigned int _alignof_T,
 | 
|---|
 | 252 |   long unsigned int _sizeof_Params,
 | 
|---|
 | 253 |   long unsigned int _alignof_Params,
 | 
|---|
 | 254 |   void (* _ctor_T)(void *, void *),
 | 
|---|
 | 255 |   void *p
 | 
|---|
 | 256 | ){
 | 
|---|
 | 257 |   void *_retval_new;
 | 
|---|
 | 258 |   void *_tmp_cp_ret0;
 | 
|---|
 | 259 |   void *_tmp_ctor_expr0;
 | 
|---|
 | 260 |   _retval_new=
 | 
|---|
 | 261 |     (_adapter_(_ctor_T,
 | 
|---|
 | 262 |       (_tmp_ctor_expr0=(_tmp_cp_ret0=malloc(_sizeof_2tT, _alignof_2tT),
 | 
|---|
 | 263 |         _tmp_cp_ret0)),
 | 
|---|
 | 264 |       p),
 | 
|---|
 | 265 |     _tmp_ctor_expr0); // ?{}
 | 
|---|
 | 266 |   *(void **)&_tmp_cp_ret0; // ^?{}
 | 
|---|
 | 267 |   return _retval_new;
 | 
|---|
 | 268 | }
 | 
|---|
 | 269 | \end{cfacode}
 | 
|---|
 | 270 | The constructor for @T@ is called indirectly through the adapter function on the result of @malloc@ and the parameter pack.
 | 
|---|
| [0eb18557] | 271 | The variable that is allocated and constructed is then returned from @new@.
 | 
|---|
| [7493339] | 272 | 
 | 
|---|
 | 273 | A call to @new@
 | 
|---|
 | 274 | \begin{cfacode}
 | 
|---|
 | 275 | struct S { int x, y; };
 | 
|---|
 | 276 | void ?{}(S *, int, int);
 | 
|---|
 | 277 | 
 | 
|---|
 | 278 | S * s = new(3, 4);
 | 
|---|
 | 279 | \end{cfacode}
 | 
|---|
 | 280 | Generates the following
 | 
|---|
 | 281 | \begin{cfacode}
 | 
|---|
 | 282 | struct _tuple2_ {  // _tuple2_(T0, T1)
 | 
|---|
 | 283 |   void *field_0;
 | 
|---|
 | 284 |   void *field_1;
 | 
|---|
 | 285 | };
 | 
|---|
 | 286 | struct _conc__tuple2_0 {  // _tuple2_(int, int)
 | 
|---|
 | 287 |   int field_0;
 | 
|---|
 | 288 |   int field_1;
 | 
|---|
 | 289 | };
 | 
|---|
 | 290 | struct _conc__tuple2_0 _tmp_cp1;  // tuple argument to new
 | 
|---|
 | 291 | struct S *_tmp_cp_ret1;           // return value from new
 | 
|---|
 | 292 | void _thunk0(  // ?{}(S *, [int, int])
 | 
|---|
 | 293 |   struct S *_p0,
 | 
|---|
 | 294 |   struct _conc__tuple2_0 _p1
 | 
|---|
 | 295 | ){
 | 
|---|
 | 296 |   _ctor_S(_p0, _p1.field_0, _p1.field_1);  // restructure tuple parameter
 | 
|---|
 | 297 | }
 | 
|---|
 | 298 | void _adapter(void (*_adaptee)(), void *_p0, void *_p1){
 | 
|---|
 | 299 |   // apply adaptee to arguments after casting to actual types
 | 
|---|
 | 300 |   ((void (*)(struct S *, struct _conc__tuple2_0))_adaptee)(
 | 
|---|
 | 301 |     _p0,
 | 
|---|
 | 302 |     *(struct _conc__tuple2_0 *)_p1
 | 
|---|
 | 303 |   );
 | 
|---|
 | 304 | }
 | 
|---|
 | 305 | struct S *s = (struct S *)(_tmp_cp_ret1=
 | 
|---|
 | 306 |   new(
 | 
|---|
 | 307 |     _adapter,
 | 
|---|
 | 308 |     sizeof(struct S),
 | 
|---|
 | 309 |     __alignof__(struct S),
 | 
|---|
 | 310 |     sizeof(struct _conc__tuple2_0),
 | 
|---|
 | 311 |     __alignof__(struct _conc__tuple2_0),
 | 
|---|
 | 312 |     (void (*)(void *, void *))&_thunk0,
 | 
|---|
 | 313 |     (({ // copy construct tuple argument to new
 | 
|---|
 | 314 |       int *__multassign_L0 = (int *)&_tmp_cp1.field_0;
 | 
|---|
 | 315 |       int *__multassign_L1 = (int *)&_tmp_cp1.field_1;
 | 
|---|
 | 316 |       int __multassign_R0 = 3;
 | 
|---|
 | 317 |       int __multassign_R1 = 4;
 | 
|---|
 | 318 |       ((*__multassign_L0=__multassign_R0 /* ?{} */) ,
 | 
|---|
 | 319 |        (*__multassign_L1=__multassign_R1 /* ?{} */));
 | 
|---|
 | 320 |     }), &_tmp_cp1)
 | 
|---|
 | 321 |   ), _tmp_cp_ret1);
 | 
|---|
 | 322 | *(struct S **)&_tmp_cp_ret1; // ^?{}  // destroy return value from new
 | 
|---|
 | 323 | ({  // destroy argument temporary
 | 
|---|
 | 324 |   int *__massassign_L0 = (int *)&_tmp_cp1.field_0;
 | 
|---|
 | 325 |   int *__massassign_L1 = (int *)&_tmp_cp1.field_1;
 | 
|---|
 | 326 |   ((*__massassign_L0 /* ^?{} */) , (*__massassign_L1 /* ^?{} */));
 | 
|---|
 | 327 | });
 | 
|---|
 | 328 | \end{cfacode}
 | 
|---|
 | 329 | Of note, @_thunk0@ is generated to translate calls to @?{}(S *, [int, int])@ into calls to @?{}(S *, int, int)@.
 | 
|---|
 | 330 | The call to @new@ constructs a tuple argument using the supplied arguments.
 | 
|---|
 | 331 | 
 | 
|---|
 | 332 | The @print@ function
 | 
|---|
 | 333 | \begin{cfacode}
 | 
|---|
 | 334 | forall(otype T, ttype Params |
 | 
|---|
 | 335 |   { void print(T); void print(Params); })
 | 
|---|
 | 336 | void print(T arg, Params rest) {
 | 
|---|
 | 337 |   print(arg);
 | 
|---|
 | 338 |   print(rest);
 | 
|---|
 | 339 | }
 | 
|---|
 | 340 | \end{cfacode}
 | 
|---|
| [0eb18557] | 341 | generates the following
 | 
|---|
| [7493339] | 342 | \begin{cfacode}
 | 
|---|
 | 343 | void print_variadic(
 | 
|---|
 | 344 |   void (*_adapterF_7tParams__P)(void (*)(), void *),
 | 
|---|
 | 345 |   void (*_adapterF_2tT__P)(void (*)(), void *),
 | 
|---|
 | 346 |   void (*_adapterF_P2tT2tT__MP)(void (*)(), void *, void *),
 | 
|---|
 | 347 |   void (*_adapterF2tT_P2tT2tT_P_MP)(void (*)(), void *, void *, void *),
 | 
|---|
 | 348 |   long unsigned int _sizeof_T,
 | 
|---|
 | 349 |   long unsigned int _alignof_T,
 | 
|---|
 | 350 |   long unsigned int _sizeof_Params,
 | 
|---|
 | 351 |   long unsigned int _alignof_Params,
 | 
|---|
 | 352 |   void *(*_assign_TT)(void *, void *),
 | 
|---|
 | 353 |   void (*_ctor_T)(void *),
 | 
|---|
 | 354 |   void (*_ctor_TT)(void *, void *),
 | 
|---|
 | 355 |   void (*_dtor_T)(void *),
 | 
|---|
 | 356 |   void (*print_T)(void *),
 | 
|---|
 | 357 |   void (*print_Params)(void *),
 | 
|---|
 | 358 |   void *arg,
 | 
|---|
 | 359 |   void *rest
 | 
|---|
 | 360 | ){
 | 
|---|
 | 361 |   void *_tmp_cp0 = __builtin_alloca(_sizeof_T);
 | 
|---|
 | 362 |   _adapterF_2tT__P(  // print(arg)
 | 
|---|
 | 363 |     ((void (*)())print_T),
 | 
|---|
 | 364 |     (_adapterF_P2tT2tT__MP( // copy construct argument
 | 
|---|
 | 365 |       ((void (*)())_ctor_TT),
 | 
|---|
 | 366 |       _tmp_cp0,
 | 
|---|
 | 367 |       arg
 | 
|---|
 | 368 |     ), _tmp_cp0)
 | 
|---|
 | 369 |   );
 | 
|---|
 | 370 |   _dtor_T(_tmp_cp0);  // destroy argument temporary
 | 
|---|
 | 371 |   _adapterF_7tParams__P(  // print(rest)
 | 
|---|
 | 372 |     ((void (*)())print_Params),
 | 
|---|
 | 373 |     rest
 | 
|---|
 | 374 |   );
 | 
|---|
 | 375 | }
 | 
|---|
 | 376 | \end{cfacode}
 | 
|---|
 | 377 | The @print_T@ routine is called indirectly through an adapter function with a copy constructed argument, followed by an indirect call to @print_Params@.
 | 
|---|
 | 378 | 
 | 
|---|
 | 379 | A call to print
 | 
|---|
 | 380 | \begin{cfacode}
 | 
|---|
 | 381 | void print(const char * x) { printf("%s", x); }
 | 
|---|
 | 382 | void print(int x) { printf("%d", x);  }
 | 
|---|
 | 383 | 
 | 
|---|
 | 384 | print("x = ", 123, ".\n");
 | 
|---|
 | 385 | \end{cfacode}
 | 
|---|
| [0eb18557] | 386 | generates the following
 | 
|---|
| [7493339] | 387 | \begin{cfacode}
 | 
|---|
 | 388 | void print_string(const char *x){
 | 
|---|
 | 389 |   int _tmp_cp_ret0;
 | 
|---|
 | 390 |   (_tmp_cp_ret0=printf("%s", x)) , _tmp_cp_ret0;
 | 
|---|
 | 391 |   *(int *)&_tmp_cp_ret0; // ^?{}
 | 
|---|
 | 392 | }
 | 
|---|
 | 393 | void print_int(int x){
 | 
|---|
 | 394 |   int _tmp_cp_ret1;
 | 
|---|
 | 395 |   (_tmp_cp_ret1=printf("%d", x)) , _tmp_cp_ret1;
 | 
|---|
 | 396 |   *(int *)&_tmp_cp_ret1; // ^?{}
 | 
|---|
 | 397 | }
 | 
|---|
 | 398 | 
 | 
|---|
 | 399 | struct _tuple2_ {  // _tuple2_(T0, T1)
 | 
|---|
 | 400 |   void *field_0;
 | 
|---|
 | 401 |   void *field_1;
 | 
|---|
 | 402 | };
 | 
|---|
 | 403 | struct _conc__tuple2_0 {  // _tuple2_(int, const char *)
 | 
|---|
 | 404 |   int field_0;
 | 
|---|
 | 405 |   const char *field_1;
 | 
|---|
 | 406 | };
 | 
|---|
 | 407 | struct _conc__tuple2_0 _tmp_cp6;  // _tuple2_(int, const char *)
 | 
|---|
 | 408 | const char *_thunk0(const char **_p0, const char *_p1){
 | 
|---|
 | 409 |         // const char * ?=?(const char **, const char *)
 | 
|---|
 | 410 |   return *_p0=_p1;
 | 
|---|
 | 411 | }
 | 
|---|
 | 412 | void _thunk1(const char **_p0){ // void ?{}(const char **)
 | 
|---|
 | 413 |   *_p0; // ?{}
 | 
|---|
 | 414 | }
 | 
|---|
 | 415 | void _thunk2(const char **_p0, const char *_p1){
 | 
|---|
 | 416 |         // void ?{}(const char **, const char *)
 | 
|---|
 | 417 |   *_p0=_p1; // ?{}
 | 
|---|
 | 418 | }
 | 
|---|
 | 419 | void _thunk3(const char **_p0){ // void ^?{}(const char **)
 | 
|---|
 | 420 |   *_p0; // ^?{}
 | 
|---|
 | 421 | }
 | 
|---|
| [0111dc7] | 422 | void _thunk4(struct _conc__tuple2_0 _p0){
 | 
|---|
 | 423 |         // void print([int, const char *])
 | 
|---|
| [7493339] | 424 |   struct _tuple1_ { // _tuple1_(T0)
 | 
|---|
 | 425 |     void *field_0;
 | 
|---|
 | 426 |   };
 | 
|---|
 | 427 |   struct _conc__tuple1_1 { // _tuple1_(const char *)
 | 
|---|
 | 428 |     const char *field_0;
 | 
|---|
 | 429 |   };
 | 
|---|
 | 430 |   void _thunk5(struct _conc__tuple1_1 _pp0){ // void print([const char *])
 | 
|---|
 | 431 |     print_string(_pp0.field_0);  // print(rest.0)
 | 
|---|
 | 432 |   }
 | 
|---|
| [0111dc7] | 433 |   void _adapter_i_pii_(
 | 
|---|
 | 434 |     void (*_adaptee)(),
 | 
|---|
 | 435 |     void *_ret,
 | 
|---|
 | 436 |     void *_p0,
 | 
|---|
 | 437 |     void *_p1
 | 
|---|
 | 438 |   ){
 | 
|---|
| [7493339] | 439 |     *(int *)_ret=((int (*)(int *, int))_adaptee)(_p0, *(int *)_p1);
 | 
|---|
 | 440 |   }
 | 
|---|
 | 441 |   void _adapter_pii_(void (*_adaptee)(), void *_p0, void *_p1){
 | 
|---|
 | 442 |     ((void (*)(int *, int ))_adaptee)(_p0, *(int *)_p1);
 | 
|---|
 | 443 |   }
 | 
|---|
 | 444 |   void _adapter_i_(void (*_adaptee)(), void *_p0){
 | 
|---|
 | 445 |     ((void (*)(int))_adaptee)(*(int *)_p0);
 | 
|---|
 | 446 |   }
 | 
|---|
 | 447 |   void _adapter_tuple1_5_(void (*_adaptee)(), void *_p0){
 | 
|---|
| [0111dc7] | 448 |     ((void (*)(struct _conc__tuple1_1 ))_adaptee)(
 | 
|---|
 | 449 |       *(struct _conc__tuple1_1 *)_p0
 | 
|---|
 | 450 |     );
 | 
|---|
| [7493339] | 451 |   }
 | 
|---|
 | 452 |   print_variadic(
 | 
|---|
 | 453 |     _adapter_tuple1_5,
 | 
|---|
 | 454 |     _adapter_i_,
 | 
|---|
 | 455 |     _adapter_pii_,
 | 
|---|
 | 456 |     _adapter_i_pii_,
 | 
|---|
 | 457 |     sizeof(int),
 | 
|---|
 | 458 |     __alignof__(int),
 | 
|---|
 | 459 |     sizeof(struct _conc__tuple1_1),
 | 
|---|
 | 460 |     __alignof__(struct _conc__tuple1_1),
 | 
|---|
| [0111dc7] | 461 |     (void *(*)(void *, void *))_assign_i,    // int ?=?(int *, int)
 | 
|---|
 | 462 |     (void (*)(void *))_ctor_i,               // void ?{}(int *)
 | 
|---|
 | 463 |     (void (*)(void *, void *))_ctor_ii,      // void ?{}(int *, int)
 | 
|---|
 | 464 |     (void (*)(void *))_dtor_ii,              // void ^?{}(int *)
 | 
|---|
 | 465 |     (void (*)(void *))print_int,             // void print(int)
 | 
|---|
 | 466 |     (void (*)(void *))&_thunk5,              // void print([const char *])
 | 
|---|
 | 467 |     &_p0.field_0,                            // rest.0
 | 
|---|
 | 468 |     &(struct _conc__tuple1_1 ){ _p0.field_1 }// [rest.1]
 | 
|---|
| [7493339] | 469 |   );
 | 
|---|
 | 470 | }
 | 
|---|
 | 471 | struct _tuple1_ {  // _tuple1_(T0)
 | 
|---|
 | 472 |   void *field_0;
 | 
|---|
 | 473 | };
 | 
|---|
 | 474 | struct _conc__tuple1_6 {  // _tuple_1(const char *)
 | 
|---|
 | 475 |   const char *field_0;
 | 
|---|
 | 476 | };
 | 
|---|
 | 477 | const char *_temp0;
 | 
|---|
 | 478 | _temp0="x = ";
 | 
|---|
 | 479 | void _adapter_pstring_pstring_string(
 | 
|---|
 | 480 |   void (*_adaptee)(),
 | 
|---|
 | 481 |   void *_ret,
 | 
|---|
 | 482 |   void *_p0,
 | 
|---|
 | 483 |   void *_p1
 | 
|---|
 | 484 | ){
 | 
|---|
 | 485 |   *(const char **)_ret=
 | 
|---|
 | 486 |     ((const char *(*)(const char **, const char *))_adaptee)(
 | 
|---|
 | 487 |       _p0,
 | 
|---|
 | 488 |       *(const char **)_p1
 | 
|---|
 | 489 |     );
 | 
|---|
 | 490 | }
 | 
|---|
 | 491 | void _adapter_pstring_string(void (*_adaptee)(), void *_p0, void *_p1){
 | 
|---|
| [0111dc7] | 492 |   ((void (*)(const char **, const char *))_adaptee)(
 | 
|---|
 | 493 |     _p0,
 | 
|---|
 | 494 |     *(const char **)_p1
 | 
|---|
 | 495 |   );
 | 
|---|
| [7493339] | 496 | }
 | 
|---|
 | 497 | void _adapter_string_(void (*_adaptee)(), void *_p0){
 | 
|---|
 | 498 |   ((void (*)(const char *))_adaptee)(*(const char **)_p0);
 | 
|---|
 | 499 | }
 | 
|---|
 | 500 | void _adapter_tuple2_0_(void (*_adaptee)(), void *_p0){
 | 
|---|
| [0111dc7] | 501 |   ((void (*)(struct _conc__tuple2_0 ))_adaptee)(
 | 
|---|
 | 502 |     *(struct _conc__tuple2_0 *)_p0
 | 
|---|
 | 503 |   );
 | 
|---|
| [7493339] | 504 | }
 | 
|---|
 | 505 | print_variadic(
 | 
|---|
 | 506 |   _adapter_tuple2_0_,
 | 
|---|
 | 507 |   _adapter_string_,
 | 
|---|
 | 508 |   _adapter_pstring_string_,
 | 
|---|
 | 509 |   _adapter_pstring_pstring_string_,
 | 
|---|
 | 510 |   sizeof(const char *),
 | 
|---|
 | 511 |   __alignof__(const char *),
 | 
|---|
 | 512 |   sizeof(struct _conc__tuple2_0 ),
 | 
|---|
 | 513 |   __alignof__(struct _conc__tuple2_0 ),
 | 
|---|
| [0111dc7] | 514 |   &_thunk0,     // const char * ?=?(const char **, const char *)
 | 
|---|
 | 515 |   &_thunk1,     // void ?{}(const char **)
 | 
|---|
 | 516 |   &_thunk2,     // void ?{}(const char **, const char *)
 | 
|---|
 | 517 |   &_thunk3,     // void ^?{}(const char **)
 | 
|---|
 | 518 |   print_string, // void print(const char *)
 | 
|---|
 | 519 |   &_thunk4,     // void print([int, const char *])
 | 
|---|
| [7493339] | 520 |   &_temp0,                             // "x = "
 | 
|---|
 | 521 |   (({  // copy construct tuple argument to print
 | 
|---|
 | 522 |     int *__multassign_L0 = (int *)&_tmp_cp6.field_0;
 | 
|---|
 | 523 |     const char **__multassign_L1 = (const char **)&_tmp_cp6.field_1;
 | 
|---|
 | 524 |     int __multassign_R0 = 123;
 | 
|---|
 | 525 |     const char *__multassign_R1 = ".\n";
 | 
|---|
 | 526 |     ((*__multassign_L0=__multassign_R0 /* ?{} */),
 | 
|---|
 | 527 |      (*__multassign_L1=__multassign_R1 /* ?{} */));
 | 
|---|
 | 528 |   }), &_tmp_cp6)                        // [123, ".\n"]
 | 
|---|
 | 529 | );
 | 
|---|
 | 530 | ({  // destroy argument temporary
 | 
|---|
 | 531 |   int *__massassign_L0 = (int *)&_tmp_cp6.field_0;
 | 
|---|
 | 532 |   const char **__massassign_L1 = (const char **)&_tmp_cp6.field_1;
 | 
|---|
 | 533 |   ((*__massassign_L0 /* ^?{} */) , (*__massassign_L1 /* ^?{} */));
 | 
|---|
 | 534 | });
 | 
|---|
 | 535 | \end{cfacode}
 | 
|---|
 | 536 | The type @_tuple2_@ is generated to allow passing the @rest@ argument to @print_variadic@.
 | 
|---|
 | 537 | Thunks 0 through 3 provide wrappers for the @otype@ parameters for @const char *@, while @_thunk4@ translates a call to @print([int, const char *])@ into a call to @print_variadic(int, [const char *])@.
 | 
|---|
 | 538 | This all builds to a call to @print_variadic@, with the appropriate copy construction of the tuple argument.
 | 
|---|