# Changeset c8771e9 for doc/user

Ignore:
Timestamp:
Nov 27, 2017, 9:56:47 PM (4 years ago)
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
481115f
Parents:
c029f4d
git-author:
Peter A. Buhr <pabuhr@…> (11/27/17 21:54:35)
git-committer:
Peter A. Buhr <pabuhr@…> (11/27/17 21:56:47)
Message:

 rc029f4d %% Created On       : Wed Apr  6 14:53:29 2016 %% Last Modified By : Peter A. Buhr %% Last Modified On : Sun Aug  6 10:24:21 2017 %% Update Count     : 3036 %% Last Modified On : Mon Nov 27 18:09:59 2017 %% Update Count     : 3143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \CFAStyle                                                                                               % use default CFA format-style \lstnewenvironment{C++}[1][]                            % use C++ style {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®}#1}} {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}} {} case 7: ... ®break®                                         §\C{// redundant explicit end of switch}§ ®break®                                         §\C{// explicit end of switch (redundant)}§ default: j = 3; ®int k = 0;®                    §\C{// allowed at different nesting levels}§ ... ®case 2:®                                     §\C{// disallow case in nested statements}§ } ... \end{cfa} The components in the "with" clause with a, b, c { ... } serve 2 purposes: each component provides a type and object. The type must be a structure type. Enumerations are already opened, and I think a union is opened to some extent, too. (Or is that just unnamed unions?) The object is the target that the naked structure-fields apply to. The components are open in "parallel" at the scope of the "with" clause/statement, so opening "a" does not affect opening "b", etc. This semantic is different from Pascal, which nests the openings. Having said the above, it seems reasonable to allow a "with" component to be an expression. The type is the static expression-type and the object is the result of the expression. Again, the type must be an aggregate. Expressions require parenthesis around the components. with( a, b, c ) { ... } Does this now make sense? Having written more CFA code, it is becoming clear to me that I *really* want the "with" to be implemented because I hate having to type all those object names for fields. It's a great way to drive people away from the language. \section{Exception Handling} try { f(...); } catch( E e : §boolean-predicate§ ) {                  §\C[8cm]{// termination handler}§ } catch( E e ; §boolean-predicate§ ) {                  §\C[8cm]{// termination handler}§ // recover and continue } catchResume( E e : §boolean-predicate§ ) {    §\C{// resumption handler}\CRT§ } catchResume( E e ; §boolean-predicate§ ) {    §\C{// resumption handler}\CRT§ // repair and return } finally { \end{cfa} This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). It is possible to declare multiple routine-prototypes in a single declaration, but the entire type specification is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:Declarations}), \eg: \begin{quote2} \begin{tabular}{@{}l@{\hspace{3em}}l@{}} \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\ \begin{cfa} [ int ] f( int ), g; \end{cfa} & \begin{cfa} int f( int ), g( int ); \end{cfa} \end{tabular} \end{quote2} Like C, it is possible to declare multiple routine-prototypes in a single declaration, where the return type is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:Declarations}), \eg: \begin{cfa} C :             const double bar1(), bar2( int ), bar3( double ); §\CFA§: [const double] foo(), foo( int ), foo( double ) { return 3.0; } \end{cfa} \CFA allows the last routine in the list to define its body. Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg: \begin{cfa} \label{s:MRV_Functions} In standard C, functions can return at most one value. In C and most programming languages, functions return at most one value; however, many operations have multiple outcomes, some exceptional (see~\VRef{s:ExceptionHandling}). To emulate functions with multiple return values, \emph{\Index{aggregation}} and/or \emph{\Index{aliasing}} is used. In the former situation, the function designer creates a record type that combines all of the return values into a single type. For example, consider a function returning the most frequently occurring letter in a string, and its frequency. This example is complex enough to illustrate that an array is insufficient, since arrays are homogeneous, and demonstrates a potential pitfall that exists with aliasing. \begin{cfa} struct mf_ret { int freq; char ch; }; struct mf_ret most_frequent(const char * str) { char freqs [26] = { 0 }; struct mf_ret ret = { 0, 'a' }; for (int i = 0; str[i] != '\0'; ++i) { if (isalpha(str[i])) {        // only count letters int ch = tolower(str[i]);   // convert to lower case int idx = ch-'a'; if (++freqs[idx] > ret.freq) {  // update on new max ret.freq = freqs[idx]; ret.ch = ch; } } } return ret; } const char * str = "hello world"; struct mf_ret ret = most_frequent(str); printf("%s -- %d %c\n", str, ret.freq, ret.ch); \end{cfa} Of note, the designer must come up with a name for the return type and for each of its fields. Unnecessary naming is a common programming language issue, introducing verbosity and a complication of the user's mental model. That is, adding another named type creates another association in the programmer's mind that needs to be kept track of when reading and writing code. As such, this technique is effective when used sparingly, but can quickly get out of hand if many functions need to return different combinations of types. In the latter approach, the designer simulates multiple return values by passing the additional return values as pointer parameters. The pointer parameters are assigned inside of the routine body to emulate a return. Using the same example, \begin{cfa} int most_frequent(const char * str, char * ret_ch) { char freqs [26] = { 0 }; int ret_freq = 0; for (int i = 0; str[i] != '\0'; ++i) { if (isalpha(str[i])) {        // only count letters int ch = tolower(str[i]);   // convert to lower case int idx = ch-'a'; if (++freqs[idx] > ret_freq) {  // update on new max ret_freq = freqs[idx]; *ret_ch = ch;   // assign to out parameter } } } return ret_freq;  // only one value returned directly } const char * str = "hello world"; char ch;                            // pre-allocate return value int freq = most_frequent(str, &ch); // pass return value as out parameter printf("%s -- %d %c\n", str, freq, ch); \end{cfa} Notably, using this approach, the caller is directly responsible for allocating storage for the additional temporary return values, which complicates the call site with a sequence of variable declarations leading up to the call. Also, while a disciplined use of ©const© can give clues about whether a pointer parameter is going to be used as an out parameter, it is not immediately obvious from only the routine signature whether the callee expects such a parameter to be initialized before the call. Furthermore, while many C routines that accept pointers are designed so that it is safe to pass ©NULL© as a parameter, there are many C routines that are not null-safe. On a related note, C does not provide a standard mechanism to state that a parameter is going to be used as an additional return value, which makes the job of ensuring that a value is returned more difficult for the compiler. Interestingly, there is a subtle bug in the previous example, in that ©ret_ch© is never assigned for a string that does not contain any letters, which can lead to undefined behaviour. In this particular case, it turns out that the frequency return value also doubles as an error code, where a frequency of 0 means the character return value should be ignored. In the former approach, a record type is created combining all of the return values. For example, consider C's \Indexc{div} function, which returns the quotient and remainder for a division of an integer value. \begin{cfa} typedef struct { int quot, rem; } div_t;        §\C[7cm]{// from include stdlib.h}§ div_t div( int num, int den ); div_t qr = div( 13, 5 );                                        §\C{// return quotient/remainder aggregate}§ printf( "%d %d\n", qr.quot, qr.rem );           §\C{// print quotient/remainder}§ \end{cfa} This approach requires a name for the return type and fields, where \Index{naming} is a common programming-language issue. That is, naming creates an association that must be managed when reading and writing code. While effective when used sparingly, this approach does not scale when functions need to return multiple combinations of types. In the latter approach, additional return values are passed as pointer parameters. A pointer parameter is assigned inside the routine to emulate a return. For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating-point value. \begin{cfa} double modf( double x, double * i );            §\C{// from include math.h}§ double intp, frac = modf( 13.5, &intp );        §\C{// return integral and fractional components}§ printf( "%g %g\n", intp, frac );                        §\C{// print integral/fractional components}§ \end{cfa} This approach requires allocating storage for the return values, which complicates the call site with a sequence of variable declarations leading to the call. Also, while a disciplined use of ©const© can give clues about whether a pointer parameter is used as an \Index{out parameter}, it is not obvious from the routine signature whether the callee expects such a parameter to be initialized before the call. Furthermore, while many C routines that accept pointers are safe for a ©NULL© argument, there are many C routines that are not null-safe. Finally, C does not provide a mechanism to state that a parameter is going to be used as an additional return value, which makes the job of ensuring that a value is returned more difficult for the compiler. Still, not every routine with multiple return values should be required to return an error code, and error codes are easily ignored, so this is not a satisfying solution. As with the previous approach, this technique can simulate multiple return values, but in practice it is verbose and error prone. In \CFA, functions can be declared to return multiple values with an extension to the function declaration syntax. \CFA allows functions to return multiple values by extending the function declaration syntax. Multiple return values are declared as a comma-separated list of types in square brackets in the same location that the return type appears in standard C function declarations. \begin{cfa} [ char, int, double ] f( ... ); \end{cfa} The ability to return multiple values from a function requires a new syntax for the return statement. For consistency, the return statement in \CFA accepts a comma-separated list of expressions in square brackets. The expression resolution phase of the \CFA translator ensures that the correct form is used depending on the values being returned and the return type of the current function. \begin{cfa} return [ c, i, d ]; \end{cfa} The expression resolution ensures the correct form is used depending on the values being returned and the return type of the current function. A multiple-returning function with return type ©T© can return any expression that is implicitly convertible to ©T©. Using the running example, the ©most_frequent© function can be written using multiple return values as such, \begin{cfa} [int, char] most_frequent(const char * str) { char freqs [26] = { 0 }; int ret_freq = 0; char ret_ch = 'a';  // arbitrary default value for consistent results for (int i = 0; str[i] != '\0'; ++i) { if (isalpha(str[i])) {        // only count letters int ch = tolower(str[i]);   // convert to lower case int idx = ch-'a'; if (++freqs[idx] > ret_freq) {  // update on new max ret_freq = freqs[idx]; ret_ch = ch; } } } return [ret_freq, ret_ch]; } \end{cfa} This approach provides the benefits of compile-time checking for appropriate return statements as in aggregation, but without the required verbosity of declaring a new named type, which precludes the bug seen with out-parameters. The addition of multiple-return-value functions necessitates a syntax for accepting multiple values at the call-site. A common use of a function's output is input to another function. \CFA allows this case, without any new syntax; a multiple-returning function can be used in any of the contexts where an expression is allowed. When a function call is passed as an argument to another call, the best match of actual arguments to formal parameters is evaluated given all possible expression interpretations in the current scope. \begin{cfa} void g( int, int );                                                     §\C{// 1}§ void g( double, double );                                       §\C{// 2}§ g( div( 13, 5 ) );                                                      §\C{// select 1}§ g( modf( 13.5 ) );                                                      §\C{// select 2}§ \end{cfa} In this case, there are two overloaded ©g© routines. Both calls to ©g© expect two arguments that are matched by the two return values from ©div© and ©modf©. respectively, which are fed directly to the first and second parameters of ©g©. As well, both calls to ©g© have exact type matches for the two different versions of ©g©, so these exact matches are chosen. When type matches are not exact, conversions are used to find a best match. The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call. \begin{cfa} [ int, int ] div( int x, int y );                       §\C{// from include stdlib}§ printf( "%d %d\n", div( 13, 5 ) );                      §\C{// print quotient/remainder}§ [ double, double ] modf( double x );            §\C{// from include math}§ printf( "%g %g\n", modf( 13.5 ) );                      §\C{// print integral/fractional components}§ \end{cfa} This approach provides the benefits of compile-time checking for appropriate return statements as in aggregation, but without the required verbosity of declaring a new named type. Finally, the addition of multiple-return-value functions necessitates a syntax for retaining the multiple values at the call-site versus their temporary existence during a call. The simplest mechanism for retaining a return value in C is variable assignment. By assigning the return value into a variable, its value can be retrieved later at any point in the program. By assigning the multiple return-values into multiple variables, the values can be retrieved later. As such, \CFA allows assigning multiple values from a function into multiple variables, using a square-bracketed list of lvalue expressions on the left side. \begin{cfa} const char * str = "hello world"; int freq; char ch; [freq, ch] = most_frequent(str);  // assign into multiple variables printf("%s -- %d %c\n", str, freq, ch); \end{cfa} It is also common to use a function's output as the input to another function. \CFA also allows this case, without any new syntax. When a function call is passed as an argument to another call, the expression resolver attempts to find the best match of actual arguments to formal parameters given all of the possible expression interpretations in the current scope \cite{Bilson03}. For example, \begin{cfa} void process(int);       // (1) void process(char);      // (2) void process(int, char); // (3) void process(char, int); // (4) process(most_frequent("hello world"));  // selects (3) \end{cfa} In this case, there is only one option for a function named ©most_frequent© that takes a string as input. This function returns two values, one ©int© and one ©char©. There are four options for a function named ©process©, but only two that accept two arguments, and of those the best match is (3), which is also an exact match. This expression first calls ©most_frequent("hello world")©, which produces the values ©3© and ©'l'©, which are fed directly to the first and second parameters of (3), respectively. \section{Tuple Expressions} int quot, rem; [ quot, rem ] = div( 13, 5 );                           §\C{// assign multiple variables}§ printf( "%d %d\n", quot, rem );                         §\C{// print quotient/remainder}\CRT§ \end{cfa} Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call. \subsection{Expressions} Multiple-return-value functions provide \CFA with a new syntax for expressing a combination of expressions in the return statement and a combination of types in a function signature. These notions can be generalized to provide \CFA with \emph{tuple expressions} and \emph{tuple types}. These notions are generalized to provide \CFA with \newterm{tuple expression}s and \newterm{tuple type}s. A tuple expression is an expression producing a fixed-size, ordered list of values of heterogeneous types. The type of a tuple expression is the tuple of the subexpression types, or a \emph{tuple type}. The type of a tuple expression is the tuple of the subexpression types, or a tuple type. In \CFA, a tuple expression is denoted by a comma-separated list of expressions enclosed in square brackets. For example, the expression ©[5, 'x', 10.5]© has type ©[int, char, double]©. The order of evaluation of the components in a tuple expression is unspecified, to allow a compiler the greatest flexibility for program optimization. It is, however, guaranteed that each component of a tuple expression is evaluated for side-effects, even if the result is not used. Multiple-return-value functions can equivalently be called \emph{tuple-returning functions}. \subsection{Tuple Variables} The call-site of the ©most_frequent© routine has a notable blemish, in that it required the preallocation of return variables in a manner similar to the aliasing example, since it is impossible to declare multiple variables of different types in the same declaration in standard C. In \CFA, it is possible to overcome this restriction by declaring a \emph{tuple variable}. \begin{cfa}[emph=ret, emphstyle=\color{red}] const char * str = "hello world"; [int, char] ret = most_frequent(str);  // initialize tuple variable printf("%s -- %d %c\n", str, ret); \end{cfa} It is now possible to accept multiple values into a single piece of storage, in much the same way that it was previously possible to pass multiple values from one function call to another. These variables can be used in any of the contexts where a tuple expression is allowed, such as in the ©printf© function call. As in the ©process© example, the components of the tuple value are passed as separate parameters to ©printf©, allowing very simple printing of tuple expressions. One way to access the individual components is with a simple assignment, as in previous examples. \begin{cfa} int freq; char ch; [freq, ch] = ret; \end{cfa} \begin{sloppypar} Multiple-return-value functions can equivalently be called \newterm{tuple-returning functions}. \subsection{Variables} The previous call of ©div© still requires the preallocation of multiple return-variables in a manner similar to the aliasing example. In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}. \begin{cfa} [int, int] ®qr® = div( 13, 5 );                 §\C{// initialize tuple variable}§ printf( "%d %d\n", ®qr® );                              §\C{// print quotient/remainder}§ \end{cfa} It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}. As well, the components of the tuple value are passed as separate parameters to ©printf©, allowing direct printing of tuple variables. One way to access the individual components of a tuple variable is with assignment. \begin{cfa} [ quot, rem ] = qr;                                             §\C{// assign multiple variables}§ \end{cfa} In addition to variables of tuple type, it is also possible to have pointers to tuples, and arrays of tuples. Tuple types can be composed of any types, except for array types, since array assignment is disallowed, which makes tuple assignment difficult when a tuple contains an array. \begin{cfa} [double, int] di; [double, int] * pdi [double, int] adi[10]; [ double, int ] di; [ double, int ] * pdi [ double, int ] adi[10]; \end{cfa} This examples declares a variable of type ©[double, int]©, a variable of type pointer to ©[double, int]©, and an array of ten ©[double, int]©. \end{sloppypar} \subsection{Tuple Indexing} At times, it is desirable to access a single component of a tuple-valued expression without creating unnecessary temporary variables to assign to. Given a tuple-valued expression ©e© and a compile-time constant integer $i$ where $0 \leq i < n$, where $n$ is the number of components in ©e©, ©e.i© accesses the $i$\textsuperscript{th} component of ©e©. For example, \subsection{Indexing} It is also possible to access a single component of a tuple-valued expression without creating temporary variables. Given a tuple-valued expression $e$np and a compile-time constant integer $i$ where $0 \leq i < n$, where $n$ is the number of components in $e$, $e.i$ accesses the $i^{\:th}$ component of $e$, \eg: \begin{cfa} [int, double] x; p->0 = 5;                                                               §\C{// access int component of tuple pointed-to by p}§ g( x.1, x.0 );                                                  §\C{// rearrange x to pass to g}§ double z = [x, f()].0.1;                                §\C{// access second component of first component of tuple expression}§ \end{cfa} As seen above, tuple-index expressions can occur on any tuple-typed expression, including tuple-returning functions, square-bracketed tuple expressions, and other tuple-index expressions, provided the retrieved component is also a tuple. double z = [ x, f() ].0.1;                              §\C{// access second component of first component of tuple expression}§ \end{cfa} Tuple-index expressions can occur on any tuple-typed expression, including tuple-returning functions, square-bracketed tuple expressions, and other tuple-index expressions, provided the retrieved component is also a tuple. This feature was proposed for \KWC but never implemented \cite[p.~45]{Till89}. \subsection{Flattening and Structuring} As evident in previous examples, tuples in \CFA do not have a rigid structure. In function call contexts, tuples support implicit flattening and restructuring conversions. There is only a single definition of ©f©, and 3 arguments with only single interpretations. First, the argument alternative list ©[5, 10.2], 4© is flattened to produce the argument list ©5, 10.2, 4©. Next, the parameter matching algorithm begins, with $P =$©int© and $A =$©int©, which unifies exactly. Moving to the next parameter and argument, $P =$©[double, int]© and $A =$©double©. This time, the parameter is a tuple type, so the algorithm applies recursively with $P' =$©double© and $A =$©double©, which unifies exactly. Then $P' =$©int© and $A =$©double©, which again unifies exactly. Next, the parameter matching algorithm begins, with $P =$© int© and $A =$© int©, which unifies exactly. Moving to the next parameter and argument, $P =$© [double, int]© and $A =$© double©. This time, the parameter is a tuple type, so the algorithm applies recursively with $P' =$© double© and $A =$© double©, which unifies exactly. Then $P' =$© int© and $A =$© double©, which again unifies exactly. At this point, the end of $P'$ has been reached, so the arguments ©10.2, 4© are structured into the tuple expression ©[10.2, 4]©. Finally, the end of the parameter list $P$ has also been reached, so the final expression is ©f(5, [10.2, 4])©. \section{Tuple Assignment} \subsection{Assignment} \label{s:TupleAssignment} An assignment where the left side of the assignment operator has a tuple type is called tuple assignment. There are two kinds of tuple assignment depending on whether the right side of the assignment operator has a tuple type or a non-tuple type, called \emph{Multiple} and \emph{Mass} Assignment, respectively. An assignment where the left side of the assignment operator has a tuple type is called \newterm{tuple assignment}. There are two kinds of tuple assignment depending on whether the right side of the assignment operator has a non-tuple or tuple type, called \newterm[mass assignment]{mass} and \newterm[multiple assignment]{multiple} assignment, respectively. \begin{cfa} int x; double y; [int, double] z; [y, x] = 3.14;  // mass assignment [x, y] = z;     // multiple assignment z = 10;         // mass assignment z = [x, y];     // multiple assignment [y, x] = 3.14;                                                  §\C{// mass assignment}§ [x, y] = z;                                                         §\C{// multiple assignment}§ z = 10;                                                         §\C{// mass assignment}§ z = [x, y];                                                             §\C{// multiple assignment}§ \end{cfa} Let $L_i$ for $i$ in $[0, n)$ represent each component of the flattened left side, $R_i$ represent each component of the flattened right side of a multiple assignment, and $R$ represent the right side of a mass assignment. For example, the following is invalid because the number of components on the left does not match the number of components on the right. \begin{cfa} [int, int] x, y, z; [x, y] = z;   // multiple assignment, invalid 4 != 2 [ int, int ] x, y, z; [ x, y ] = z;                                              §\C{// multiple assignment, invalid 4 != 2}§ \end{cfa} Multiple assignment assigns $R_i$ to $L_i$ for each $i$. \begin{cfa} int x = 10, y = 20; [x, y] = [y, x]; [ x, y ] = [ y, x ]; \end{cfa} After executing this code, ©x© has the value ©20© and ©y© has the value ©10©. int a, b; double c, d; [void] f([int, int]); f([c, a] = [b, d] = 1.5);  // assignments in parameter list [ void ] f( [ int, int ] ); f( [ c, a ] = [ b, d ] = 1.5 );  // assignments in parameter list \end{cfa} The tuple expression begins with a mass assignment of ©1.5© into ©[b, d]©, which assigns ©1.5© into ©b©, which is truncated to ©1©, and ©1.5© into ©d©, producing the tuple ©[1, 1.5]© as a result. Finally, the tuple ©[1, 1]© is used as an expression in the call to ©f©. \subsection{Tuple Construction} \subsection{Construction} Tuple construction and destruction follow the same rules and semantics as tuple assignment, except that in the case where there is no right side, the default constructor or destructor is called on each component of the tuple. As constructors and destructors did not exist in previous versions of \CFA or in \KWC, this is a primary contribution of this thesis to the design of tuples. The initialization of ©s© with ©t© works by default because ©t© is flattened into its components, which satisfies the generated field constructor ©?{}(S *, int, double)© to initialize the first two values. \section{Member-Access Tuple Expression} \subsection{Member-Access Expression} \label{s:MemberAccessTuple} It is possible to access multiple fields from a single expression using a \emph{Member-Access Tuple Expression}. Tuples may be used to select multiple fields of a record by field name. The result is a single tuple-valued expression whose type is the tuple of the types of the members. For example, \begin{cfa} struct S { int x; double y; char * z; } s; struct S { char x; int y; double z; } s; s.[x, y, z]; \end{cfa} Here, the type of ©s.[x, y, z]© is ©[int, double, char *]©. A member tuple expression has the form ©a.[x, y, z];© where ©a© is an expression with type ©T©, where ©T© supports member access expressions, and ©x, y, z© are all members of ©T© with types ©T$_x$©, ©T$_y$©, and ©T$_z$© respectively. Then the type of ©a.[x, y, z]© is ©[T_x, T_y, T_z]©. Since tuple index expressions are a form of member-access expression, it is possible to use tuple-index expressions in conjunction with member tuple expressions to manually restructure a tuple (\eg, rearrange components, drop components, duplicate components, etc.). \begin{cfa} [int, int, long, double] x; void f(double, long); f(x.[0, 3]);          // f(x.0, x.3) x.[0, 1] = x.[1, 0];  // [x.0, x.1] = [x.1, x.0] [long, int, long] y = x.[2, 0, 2]; \end{cfa} It is possible for a member tuple expression to contain other member access expressions. For example, Here, the type of ©s.[ x, y, z ]© is ©[ char, int, double ]©. A member tuple expression has the form \emph{e}©.[x, y, z];© where \emph{e} is an expression with type ©T©, where ©T© supports member access expressions, and ©x, y, z© are all members of ©T© with types ©T$_x$©, ©T$_y$©, and ©T$_z$© respectively. Then the type of \emph{e}©.[x, y, z]© is ©[T$_x$, T$_y$, T$_z$]©. A member-access tuple may be used anywhere a tuple can be used, \eg: \begin{cfa} s.[ y, z, x ] = [ 3, 3.2, 'x' ];                §\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}§ f( s.[ y, z ] );                                                §\C{// equivalent to f( s.y, s.z )}§ \end{cfa} Note, the fields appearing in a record-field tuple may be specified in any order; also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. Since tuple-index expressions are a form of member-access expression, it is possible to use tuple-index expressions in conjunction with member-access expressions to restructure a tuple (\eg, rearrange components, drop components, duplicate components, etc.). \begin{cfa} [ int, int, long, double ] x; void f( double, long ); f( x.[ 0, 3 ] );                                                §\C{// f( x.0, x.3 )}§ x.[ 0, 1 ] = x.[ 1, 0 ];                                §\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}§ [ long, int, long ] y = x.[ 2, 0, 2 ]; \end{cfa} It is possible for a member tuple expression to contain other member access expressions, \eg: \begin{cfa} struct A { double i; int j; }; struct B { int * k; short l; }; struct C { int x; A y; B z; } v; v.[x, y.[i, j], z.k]; \end{cfa} This expression is equivalent to ©[v.x, [v.y.i, v.y.j], v.z.k]©. That is, the aggregate expression is effectively distributed across the tuple, which allows simple and easy access to multiple components in an aggregate, without repetition. v.[ x, y.[ i, j ], z.k ]; \end{cfa} This expression is equivalent to ©[ v.x, [ v.y.i, v.y.j ], v.z.k ]©. That is, the aggregate expression is effectively distributed across the tuple allowing simple and easy access to multiple components in an aggregate without repetition. It is guaranteed that the aggregate expression to the left of the ©.© in a member tuple expression is evaluated exactly once. As such, it is safe to use member tuple expressions on the result of a side-effecting function. \begin{cfa} [int, float, double] f(); [double, float] x = f().[2, 1]; As such, it is safe to use member tuple expressions on the result of a function with side-effects. \begin{cfa} [ int, float, double ] f(); [ double, float ] x = f().[ 2, 1 ];             §\C{// f() called once}§ \end{cfa} Since \CFA permits these tuple-access expressions using structures, unions, and tuples, \emph{member tuple expression} or \emph{field tuple expression} is more appropriate. It is possible to extend member-access expressions further. Currently, a member-access expression whose member is a name requires that the aggregate is a structure or union, while a constant integer member requires the aggregate to be a tuple. In the interest of orthogonal design, \CFA could apply some meaning to the remaining combinations as well. For example, \begin{cfa} struct S { int x, y; } s; [S, S] z; s.x;  // access member z.0;  // access component s.1;  // ??? z.y;  // ??? \end{cfa} One possibility is for ©s.1© to select the second member of ©s©. Under this interpretation, it becomes possible to not only access members of a struct by name, but also by position. Likewise, it seems natural to open this mechanism to enumerations as well, wherein the left side would be a type, rather than an expression. One benefit of this interpretation is familiarity, since it is extremely reminiscent of tuple-index expressions. On the other hand, it could be argued that this interpretation is brittle in that changing the order of members or adding new members to a structure becomes a brittle operation. This problem is less of a concern with tuples, since modifying a tuple affects only the code that directly uses the tuple, whereas modifying a structure has far reaching consequences for every instance of the structure. As for ©z.y©, one interpretation is to extend the meaning of member tuple expressions. That is, currently the tuple must occur as the member, \ie to the right of the dot. Allowing tuples to the left of the dot could distribute the member across the elements of the tuple, in much the same way that member tuple expressions distribute the aggregate across the member tuple. In this example, ©z.y© expands to ©[z.0.y, z.1.y]©, allowing what is effectively a very limited compile-time field-sections map operation, where the argument must be a tuple containing only aggregates having a member named ©y©. It is questionable how useful this would actually be in practice, since structures often do not have names in common with other structures, and further this could cause maintainability issues in that it encourages programmers to adopt very simple naming conventions to maximize the amount of overlap between different types. Perhaps more useful would be to allow arrays on the left side of the dot, which would likewise allow mapping a field access across the entire array, producing an array of the contained fields. The immediate problem with this idea is that C arrays do not carry around their size, which would make it impossible to use this extension for anything other than a simple stack allocated array. Supposing this feature works as described, it would be necessary to specify an ordering for the expansion of member-access expressions versus member-tuple expressions. \begin{cfa} struct { int x, y; }; [S, S] z; z.[x, y];  // ??? // => [z.0, z.1].[x, y] // => [z.0.x, z.0.y, z.1.x, z.1.y] // or // => [z.x, z.y] // => [[z.0, z.1].x, [z.0, z.1].y] // => [z.0.x, z.1.x, z.0.y, z.1.y] \end{cfa} Depending on exactly how the two tuples are combined, different results can be achieved. As such, a specific ordering would need to be imposed to make this feature useful. Furthermore, this addition moves a member-tuple expression's meaning from being clear statically to needing resolver support, since the member name needs to be distributed appropriately over each member of the tuple, which could itself be a tuple. A second possibility is for \CFA to have named tuples, as they exist in Swift and D. \begin{cfa} typedef [int x, int y] Point2D; Point2D p1, p2; p1.x + p1.y + p2.x + p2.y; p1.0 + p1.1 + p2.0 + p2.1;  // equivalent \end{cfa} In this simpler interpretation, a tuple type carries with it a list of possibly empty identifiers. This approach fits naturally with the named return-value feature, and would likely go a long way towards implementing it. Ultimately, the first two extensions introduce complexity into the model, with relatively little perceived benefit, and so were dropped from consideration. Named tuples are a potentially useful addition to the language, provided they can be parsed with a reasonable syntax. \section{Casting} \subsection{Casting} In C, the cast operator is used to explicitly convert between types. In \CFA, the cast operator has a secondary use, which is type ascription, since it forces the expression resolution algorithm to choose the lowest cost conversion to the target type. That is, it is invalid to cast ©[int, int]© to ©[int, int, int]©. \section{Polymorphism} \subsection{Polymorphism} Due to the implicit flattening and structuring conversions involved in argument passing, ©otype© and ©dtype© parameters are restricted to matching only with non-tuple types. The integration of polymorphism, type assertions, and monomorphic specialization of tuple-assertions are a primary contribution of this thesis to the design of tuples. Until this point, it has been assumed that assertion arguments must match the parameter type exactly, modulo polymorphic specialization (\ie, no implicit conversions are applied to assertion arguments). This decision presents a conflict with the flexibility of tuples. \subsection{Assertion Inference} \subsubsection{Assertion Inference} \begin{cfa} int f([int, double], double); Unfortunately, C's syntax for subscripts precluded treating them as tuples. The C subscript list has the form ©[i][j]...© and not ©[i, j, ...]©. Therefore, there is no syntactic way for a routine returning multiple values to specify the different subscript values, \eg ©f[g()]© always means a single subscript value because there is only one set of brackets. Therefore, there is no syntactic way for a routine returning multiple values to specify the different subscript values, \eg ©f[ g() ]© always means a single subscript value because there is only one set of brackets. Fixing this requires a major change to C because the syntactic form ©M[i, j, k]© already has a particular meaning: ©i, j, k© is a comma expression. \end{rationale} \section{Mass Assignment} \subsection{Mass Assignment} \CFA permits assignment to several variables at once using mass assignment~\cite{CLU}. \section{Multiple Assignment} \subsection{Multiple Assignment} \CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}. \section{Cascade Assignment} \subsection{Cascade Assignment} As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment. \end{cfa} As in C, the rightmost assignment is performed first, \ie assignment parses right to left. \section{Field Tuples} Tuples may be used to select multiple fields of a record by field name. Its general form is: \begin{cfa} §\emph{expr}§ . [ §\emph{fieldlist}§ ] §\emph{expr}§ -> [ §\emph{fieldlist}§ ] \end{cfa} \emph{expr} is any expression yielding a value of type record, \eg ©struct©, ©union©. Each element of \emph{ fieldlist} is an element of the record specified by \emph{expr}. A record-field tuple may be used anywhere a tuple can be used. An example of the use of a record-field tuple is the following: \begin{cfa} struct s { int f1, f2; char f3; double f4; } v; v.[ f3, f1, f2 ] = ['x', 11, 17 ];      §\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}§ f( v.[ f3, f1, f2 ] );                          §\C{// equivalent to f( v.f3, v.f1, v.f2 )}§ \end{cfa} Note, the fields appearing in a record-field tuple may be specified in any order; also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. If a field of a ©struct© is itself another ©struct©, multiple fields of this subrecord can be specified using a nested record-field tuple, as in the following example: \begin{cfa} struct inner { int f2, f3; }; struct outer { int f1; struct inner i; double f4; } o; o.[ f1, i.[ f2, f3 ], f4 ] = [ 11, 12, 13, 3.14159 ]; \end{cfa}