Ignore:
Timestamp:
Apr 12, 2017, 3:54:28 PM (5 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, 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
Message:

thesis updates based on Peter's feedback

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/rob_thesis/variadic.tex

    reaa2f3a1 r0eb18557  
    1212In addition, C requires the programmer to hard code all of the possible expected types.
    1313As 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,
     14For example, a simple function to sum $N$ @int@s,
    1515\begin{cfacode}
    1616int sum(int N, ...) {
     
    2727sum(3, 10, 20, 30);  // need to keep counter in sync
    2828\end{cfacode}
    29 The @va_list@ type is a special C data type that abstracts variadic argument manipulation.
     29The @va_list@ type is a special C data type that abstracts variadic-argument manipulation.
    3030The @va_start@ macro initializes a @va_list@, given the last named parameter.
    3131Each use of the @va_arg@ macro allows access to the next variadic argument, given a type.
     
    3434In 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}.
    3535Furthermore, 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 generally unsafe.
     36Since they rely on programmer convention rather than compile-time checks, variadic functions are unsafe.
    3737
    3838In 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 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.
     39For 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.
     40Unfortunately, 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.
    4141
    4242As a result, C's variadic functions are a deficient language feature.
     
    7979Similarly, 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.
    8080
    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.
     81It should be otherwise noted that the addition of an exact matching rule only affects the outcome for polymorphic type-binding when tuples are involved.
    8282For non-tuple arguments, exact matching and flattening and structuring are equivalent.
    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.
     83For 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.
    8484Thus 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.
    8585
     
    161161  return x+y;
    162162}
    163 forall(otype T1, otype T2, otype T3, ttype Params, otype R
     163forall(otype T1, otype T2, otype T3, otype R, ttype Params
    164164  | summable(T1, T2, T3)
    165165  | { R sum(T3, Params); })
     
    184184\CFA does not need an ellipsis in either case, since the type class @ttype@ is only used for variadics.
    185185An 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.
     186This 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.
    187187\begin{cppcode}
    188188template<typename... Args>
     
    224224Array * x = new(1, 2, 3);
    225225\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.
     226In 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
     228The @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.
    227229This 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.
    230230
    231231\section{Implementation}
     
    240240}
    241241\end{cfacode}
    242 Generates the following
     242generates the following
    243243\begin{cfacode}
    244244void *malloc(long unsigned int _sizeof_T, long unsigned int _alignof_T);
     
    267267\end{cfacode}
    268268The 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@.
     269The variable that is allocated and constructed is then returned from @new@.
    270270
    271271A call to @new@
     
    337337}
    338338\end{cfacode}
    339 Generates
     339generates the following
    340340\begin{cfacode}
    341341void print_variadic(
     
    382382print("x = ", 123, ".\n");
    383383\end{cfacode}
    384 Generates the following
     384generates the following
    385385\begin{cfacode}
    386386void print_string(const char *x){
Note: See TracChangeset for help on using the changeset viewer.