Changeset 0eb18557 for doc/rob_thesis/variadic.tex
- Timestamp:
- Apr 12, 2017, 3:54:28 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- e869e434
- Parents:
- eaa2f3a1
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/rob_thesis/variadic.tex
reaa2f3a1 r0eb18557 12 12 In addition, C requires the programmer to hard code all of the possible expected types. 13 13 As a result, it is cumbersome to write a function that is open to extension. 14 For example, a simple function which sums$N$ @int@s,14 For example, a simple function to sum $N$ @int@s, 15 15 \begin{cfacode} 16 16 int sum(int N, ...) { … … 27 27 sum(3, 10, 20, 30); // need to keep counter in sync 28 28 \end{cfacode} 29 The @va_list@ type is a special C data type that abstracts variadic 29 The @va_list@ type is a special C data type that abstracts variadic-argument manipulation. 30 30 The @va_start@ macro initializes a @va_list@, given the last named parameter. 31 31 Each use of the @va_arg@ macro allows access to the next variadic argument, given a type. … … 34 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 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. 36 Since they rely on programmer convention rather than compile-time checks, variadic functions are generallyunsafe.36 Since they rely on programmer convention rather than compile-time checks, variadic functions are unsafe. 37 37 38 38 In practice, compilers can provide warnings to help mitigate some of the problems. 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 40 Unfortunately, this approach does not permit extensions to the format 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. 41 41 42 42 As a result, C's variadic functions are a deficient language feature. … … 79 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 80 81 It should be otherwise noted that the addition of an exact matching rule only affects the outcome for polymorphic type 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. 82 82 For non-tuple arguments, exact matching and flattening and structuring are equivalent. 83 For tuple arguments to a function without polymorphic formal 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. 84 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 85 … … 161 161 return x+y; 162 162 } 163 forall(otype T1, otype T2, otype T3, ttype Params, otype R163 forall(otype T1, otype T2, otype T3, otype R, ttype Params 164 164 | summable(T1, T2, T3) 165 165 | { R sum(T3, Params); }) … … 184 184 \CFA does not need an ellipsis in either case, since the type class @ttype@ is only used for variadics. 185 185 An alternative design is to use an ellipsis combined with an existing type class. 186 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, e.g., in fold expressions, which requires compile-time knowledge of the structure of the parameter pack, which is not available in \CFA.186 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. 187 187 \begin{cppcode} 188 188 template<typename... Args> … … 224 224 Array * x = new(1, 2, 3); 225 225 \end{cfacode} 226 The @new@ function provides the combination of type-safe @malloc@ with a constructor call, so that it becomes impossible to forget to construct dynamically allocated objects. 226 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. 227 228 The @new@ function provides the combination of type-safe @malloc@ with a constructor call, so that it becomes impossible to forget to construct dynamically-allocated objects. 227 229 This approach provides the type-safety of @new@ in \CC, without the need to specify the allocated type, thanks to return-type inference. 228 229 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.230 230 231 231 \section{Implementation} … … 240 240 } 241 241 \end{cfacode} 242 Generates the following242 generates the following 243 243 \begin{cfacode} 244 244 void *malloc(long unsigned int _sizeof_T, long unsigned int _alignof_T); … … 267 267 \end{cfacode} 268 268 The constructor for @T@ is called indirectly through the adapter function on the result of @malloc@ and the parameter pack. 269 The variable that was allocated and constructed is then returned from @new@.269 The variable that is allocated and constructed is then returned from @new@. 270 270 271 271 A call to @new@ … … 337 337 } 338 338 \end{cfacode} 339 Generates 339 generates the following 340 340 \begin{cfacode} 341 341 void print_variadic( … … 382 382 print("x = ", 123, ".\n"); 383 383 \end{cfacode} 384 Generates the following384 generates the following 385 385 \begin{cfacode} 386 386 void print_string(const char *x){
Note: See TracChangeset
for help on using the changeset viewer.