source: doc/papers/general/Paper.tex @ 8f67d44

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerresolv-newwith_gc
Last change on this file since 8f67d44 was 8f67d44, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

more text on new declaration syntax, start library section with stream I/O

  • Property mode set to 100644
File size: 128.4 KB
Line 
1\documentclass{article}
2
3\usepackage{fullpage}
4\usepackage{epic,eepic}
5\usepackage{xspace,calc,comment}
6\usepackage{upquote}                                            % switch curled `'" to straight
7\usepackage{listings}                                           % format program code
8\usepackage[flushmargin]{footmisc}                      % support label/reference in footnote
9\usepackage{rotating}
10\usepackage[usenames]{color}
11\usepackage{pslatex}                                            % reduce size of san serif font
12\usepackage[plainpages=false,pdfpagelabels,pdfpagemode=UseNone,pagebackref=true,breaklinks=true,colorlinks=true,linkcolor=blue,citecolor=blue,urlcolor=blue]{hyperref}
13
14\setlength{\textheight}{9in}
15%\oddsidemargin 0.0in
16\renewcommand{\topfraction}{0.8}                        % float must be greater than X of the page before it is forced onto its own page
17\renewcommand{\bottomfraction}{0.8}                     % float must be greater than X of the page before it is forced onto its own page
18\renewcommand{\floatpagefraction}{0.8}          % float must be greater than X of the page before it is forced onto its own page
19\renewcommand{\textfraction}{0.0}                       % the entire page maybe devoted to floats with no text on the page at all
20
21\lefthyphenmin=4                                                        % hyphen only after 4 characters
22\righthyphenmin=4
23
24% Names used in the document.
25
26\newcommand{\CFAIcon}{\textsf{C}\raisebox{\depth}{\rotatebox{180}{\textsf{A}}}\xspace} % Cforall symbolic name
27\newcommand{\CFA}{\protect\CFAIcon}             % safe for section/caption
28\newcommand{\CFL}{\textrm{Cforall}\xspace}      % Cforall symbolic name
29\newcommand{\Celeven}{\textrm{C11}\xspace}      % C11 symbolic name
30\newcommand{\CC}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}\xspace} % C++ symbolic name
31\newcommand{\CCeleven}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}11\xspace} % C++11 symbolic name
32\newcommand{\CCfourteen}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}14\xspace} % C++14 symbolic name
33\newcommand{\CCseventeen}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}17\xspace} % C++17 symbolic name
34\newcommand{\CCtwenty}{\textrm{C}\kern-.1em\hbox{+\kern-.25em+}20\xspace} % C++20 symbolic name
35\newcommand{\CCV}{\rm C\kern-.1em\hbox{+\kern-.25em+}obj\xspace} % C++ virtual symbolic name
36\newcommand{\Csharp}{C\raisebox{-0.7ex}{\Large$^\sharp$}\xspace} % C# symbolic name
37
38%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
39
40\newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}}
41\newcommand{\TODO}[1]{\textbf{TODO}: {\itshape #1}} % TODO included
42%\newcommand{\TODO}[1]{} % TODO elided
43
44% Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore
45% removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR
46% AFTER HYPERREF.
47%\DeclareTextCommandDefault{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}
48\renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}
49
50\makeatletter
51% parindent is relative, i.e., toggled on/off in environments like itemize, so store the value for
52% use rather than use \parident directly.
53\newlength{\parindentlnth}
54\setlength{\parindentlnth}{\parindent}
55
56\newcommand{\LstKeywordStyle}[1]{{\lst@basicstyle{\lst@keywordstyle{#1}}}}
57\newcommand{\LstCommentStyle}[1]{{\lst@basicstyle{\lst@commentstyle{#1}}}}
58
59\newlength{\gcolumnposn}                                        % temporary hack because lstlisting does not handle tabs correctly
60\newlength{\columnposn}
61\setlength{\gcolumnposn}{2.75in}
62\setlength{\columnposn}{\gcolumnposn}
63\newcommand{\C}[2][\@empty]{\ifx#1\@empty\else\global\setlength{\columnposn}{#1}\global\columnposn=\columnposn\fi\hfill\makebox[\textwidth-\columnposn][l]{\lst@basicstyle{\LstCommentStyle{#2}}}}
64\newcommand{\CRT}{\global\columnposn=\gcolumnposn}
65
66% Denote newterms in particular font and index them without particular font and in lowercase, e.g., \newterm{abc}.
67% The option parameter provides an index term different from the new term, e.g., \newterm[\texttt{abc}]{abc}
68% The star version does not lowercase the index information, e.g., \newterm*{IBM}.
69\newcommand{\newtermFontInline}{\emph}
70\newcommand{\newterm}{\@ifstar\@snewterm\@newterm}
71\newcommand{\@newterm}[2][\@empty]{\lowercase{\def\temp{#2}}{\newtermFontInline{#2}}\ifx#1\@empty\index{\temp}\else\index{#1@{\protect#2}}\fi}
72\newcommand{\@snewterm}[2][\@empty]{{\newtermFontInline{#2}}\ifx#1\@empty\index{#2}\else\index{#1@{\protect#2}}\fi}
73
74% Latin abbreviation
75\newcommand{\abbrevFont}{\textit}                       % set empty for no italics
76\newcommand{\EG}{\abbrevFont{e}.\abbrevFont{g}.}
77\newcommand*{\eg}{%
78        \@ifnextchar{,}{\EG}%
79                {\@ifnextchar{:}{\EG}%
80                        {\EG,\xspace}}%
81}%
82\newcommand{\IE}{\abbrevFont{i}.\abbrevFont{e}.}
83\newcommand*{\ie}{%
84        \@ifnextchar{,}{\IE}%
85                {\@ifnextchar{:}{\IE}%
86                        {\IE,\xspace}}%
87}%
88\newcommand{\ETC}{\abbrevFont{etc}}
89\newcommand*{\etc}{%
90        \@ifnextchar{.}{\ETC}%
91        {\ETC\xspace}%
92}%
93\newcommand{\ETAL}{\abbrevFont{et}\hspace{2pt}\abbrevFont{al}}
94\newcommand*{\etal}{%
95        \@ifnextchar{.}{\protect\ETAL}%
96                {\abbrevFont{\protect\ETAL}.\xspace}%
97}%
98\newcommand{\VIZ}{\abbrevFont{viz}}
99\newcommand*{\viz}{%
100        \@ifnextchar{.}{\VIZ}%
101                {\abbrevFont{\VIZ}.\xspace}%
102}%
103\makeatother
104
105\newenvironment{cquote}{%
106        \list{}{\lstset{resetmargins=true,aboveskip=0pt,belowskip=0pt}\topsep=4pt\parsep=0pt\leftmargin=\parindentlnth\rightmargin\leftmargin}%
107        \item\relax
108}{%
109        \endlist
110}% cquote
111
112% CFA programming language, based on ANSI C (with some gcc additions)
113\lstdefinelanguage{CFA}[ANSI]{C}{
114        morekeywords={
115                _Alignas, _Alignof, __alignof, __alignof__, asm, __asm, __asm__, _At, __attribute,
116                __attribute__, auto, _Bool, catch, catchResume, choose, _Complex, __complex, __complex__,
117                __const, __const__, disable, dtype, enable, __extension__, fallthrough, fallthru,
118                finally, forall, ftype, _Generic, _Imaginary, inline, __label__, lvalue, _Noreturn, one_t,
119                otype, restrict, _Static_assert, throw, throwResume, trait, try, ttype, typeof, __typeof,
120                __typeof__, virtual, with, zero_t},
121        moredirectives={defined,include_next}%
122}%
123
124\lstset{
125language=CFA,
126columns=fullflexible,
127basicstyle=\linespread{0.9}\sf,                                                 % reduce line spacing and use sanserif font
128stringstyle=\tt,                                                                                % use typewriter font
129tabsize=5,                                                                                              % N space tabbing
130xleftmargin=\parindentlnth,                                                             % indent code to paragraph indentation
131%mathescape=true,                                                                               % LaTeX math escape in CFA code $...$
132escapechar=\$,                                                                                  % LaTeX escape in CFA code
133keepspaces=true,                                                                                %
134showstringspaces=false,                                                                 % do not show spaces with cup
135showlines=true,                                                                                 % show blank lines at end of code
136aboveskip=4pt,                                                                                  % spacing above/below code block
137belowskip=3pt,
138% replace/adjust listing characters that look bad in sanserif
139literate={-}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.1ex}}}}1 {^}{\raisebox{0.6ex}{$\scriptscriptstyle\land\,$}}1
140        {~}{\raisebox{0.3ex}{$\scriptstyle\sim\,$}}1 % {`}{\ttfamily\upshape\hspace*{-0.1ex}`}1
141        {<-}{$\leftarrow$}2 {=>}{$\Rightarrow$}2 {->}{\makebox[1ex][c]{\raisebox{0.4ex}{\rule{0.8ex}{0.075ex}}}\kern-0.2ex\textgreater}2,
142moredelim=**[is][\color{red}]{`}{`},
143}% lstset
144
145% inline code @...@
146\lstMakeShortInline@%
147
148\lstnewenvironment{cfa}[1][]
149{\lstset{#1}}
150{}
151\lstnewenvironment{C++}[1][]                            % use C++ style
152{\lstset{language=C++,moredelim=**[is][\protect\color{red}]{`}{`},#1}\lstset{#1}}
153{}
154
155
156\title{Generic and Tuple Types with Efficient Dynamic Layout in \protect\CFA}
157
158\author{Aaron Moss, Robert Schluntz, Peter Buhr}
159% \email{a3moss@uwaterloo.ca}
160% \email{rschlunt@uwaterloo.ca}
161% \email{pabuhr@uwaterloo.ca}
162% \affiliation{%
163%       \institution{University of Waterloo}
164%       \department{David R. Cheriton School of Computer Science}
165%       \streetaddress{Davis Centre, University of Waterloo}
166%       \city{Waterloo}
167%       \state{ON}
168%       \postcode{N2L 3G1}
169%       \country{Canada}
170% }
171
172%\terms{generic, tuple, variadic, types}
173%\keywords{generic types, tuple types, variadic types, polymorphic functions, C, Cforall}
174
175\begin{document}
176\maketitle
177
178
179\begin{abstract}
180The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from commercial operating-systems to hobby projects.
181This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more.
182Nonetheless, C, first standardized over thirty years ago, lacks many features that make programming in more modern languages safer and more productive.
183The goal of the \CFA project is to create an extension of C that provides modern safety and productivity features while still ensuring strong backwards compatibility with C and its programmers.
184Prior projects have attempted similar goals but failed to honour C programming-style; for instance, adding object-oriented or functional programming with garbage collection is a non-starter for many C developers.
185Specifically, \CFA is designed to have an orthogonal feature-set based closely on the C programming paradigm, so that \CFA features can be added \emph{incrementally} to existing C code-bases, and C programmers can learn \CFA extensions on an as-needed basis, preserving investment in existing code and engineers.
186This paper describes two \CFA extensions, generic and tuple types, details how their design avoids shortcomings of similar features in C and other C-like languages, and presents experimental results validating the design.
187\end{abstract}
188
189
190\section{Introduction and Background}
191
192The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from commercial operating-systems to hobby projects.
193This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more.
194The TIOBE~\cite{TIOBE} ranks the top 5 most popular programming languages as: Java 16\%, \Textbf{C 7\%}, \Textbf{\CC 5\%}, \Csharp 4\%, Python 4\% = 36\%, where the next 50 languages are less than 3\% each with a long tail.
195The top 3 rankings over the past 30 years are:
196\begin{center}
197\setlength{\tabcolsep}{10pt}
198\lstDeleteShortInline@%
199\begin{tabular}{@{}rccccccc@{}}
200                & 2017  & 2012  & 2007  & 2002  & 1997  & 1992  & 1987          \\ \hline
201Java    & 1             & 1             & 1             & 1             & 12    & -             & -                     \\
202\Textbf{C}      & \Textbf{2}& \Textbf{2}& \Textbf{2}& \Textbf{2}& \Textbf{1}& \Textbf{1}& \Textbf{1}    \\
203\CC             & 3             & 3             & 3             & 3             & 2             & 2             & 4                     \\
204\end{tabular}
205\lstMakeShortInline@%
206\end{center}
207Love it or hate it, C is extremely popular, highly used, and one of the few systems languages.
208In many cases, \CC is often used solely as a better C.
209Nonetheless, C, first standardized over thirty years ago, lacks many features that make programming in more modern languages safer and more productive.
210
211\CFA (pronounced ``C-for-all'', and written \CFA or Cforall) is an evolutionary extension of the C programming language that aims to add modern language features to C while maintaining both source compatibility with C and a familiar programming model for programmers.
212The four key design goals for \CFA~\cite{Bilson03} are:
213(1) The behaviour of standard C code must remain the same when translated by a \CFA compiler as when translated by a C compiler;
214(2) Standard C code must be as fast and as small when translated by a \CFA compiler as when translated by a C compiler;
215(3) \CFA code must be at least as portable as standard C code;
216(4) Extensions introduced by \CFA must be translated in the most efficient way possible.
217These goals ensure existing C code-bases can be converted to \CFA incrementally with minimal effort, and C programmers can productively generate \CFA code without training beyond the features being used.
218\CC is used similarly, but has the disadvantages of multiple legacy design-choices that cannot be updated and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C-based project.
219
220\CFA is currently implemented as a source-to-source translator from \CFA to the GCC-dialect of C~\cite{GCCExtensions}, allowing it to leverage the portability and code optimizations provided by GCC, meeting goals (1)--(3).
221Ultimately, a compiler is necessary for advanced features and optimal performance.
222
223This paper identifies shortcomings in existing approaches to generic and variadic data types in C-like languages and presents a design for generic and variadic types avoiding those shortcomings.
224Specifically, the solution is both reusable and type-checked, as well as conforming to the design goals of \CFA with ergonomic use of existing C abstractions.
225The new constructs are empirically compared with both standard C and \CC; the results show the new design is comparable in performance.
226
227
228\subsection{Polymorphic Functions}
229\label{sec:poly-fns}
230
231\CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfield~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}.
232The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a @forall@ clause (giving the language its name):
233\begin{lstlisting}
234`forall( otype T )` T identity( T val ) { return val; }
235int forty_two = identity( 42 );                         $\C{// T is bound to int, forty\_two == 42}$
236\end{lstlisting}
237The @identity@ function above can be applied to any complete \newterm{object type} (or @otype@).
238The type variable @T@ is transformed into a set of additional implicit parameters encoding sufficient information about @T@ to create and return a variable of that type.
239The \CFA implementation passes the size and alignment of the type represented by an @otype@ parameter, as well as an assignment operator, constructor, copy constructor and destructor.
240If this extra information is not needed, \eg for a pointer, the type parameter can be declared as a \newterm{data type} (or @dtype@).
241
242In \CFA, the polymorphism runtime-cost is spread over each polymorphic call, due to passing more arguments to polymorphic functions;
243the experiments in Section~\ref{sec:eval} show this overhead is similar to \CC virtual-function calls.
244A design advantage is that, unlike \CC template-functions, \CFA polymorphic-functions are compatible with C \emph{separate compilation}, preventing compilation and code bloat.
245
246Since bare polymorphic-types provide a restricted set of available operations, \CFA provides a \newterm{type assertion}~\cite[pp.~37-44]{Alphard} mechanism to provide further type information, where type assertions may be variable or function declarations that depend on a polymorphic type-variable.
247For example, the function @twice@ can be defined using the \CFA syntax for operator overloading:
248\begin{lstlisting}
249forall( otype T `| { T ?+?(T, T); }` ) T twice( T x ) { return x + x; } $\C{// ? denotes operands}$
250int val = twice( twice( 3.7 ) );
251\end{lstlisting}
252which works for any type @T@ with a matching addition operator.
253The polymorphism is achieved by creating a wrapper function for calling @+@ with @T@ bound to @double@, then passing this function to the first call of @twice@.
254There is now the option of using the same @twice@ and converting the result to @int@ on assignment, or creating another @twice@ with type parameter @T@ bound to @int@ because \CFA uses the return type~\cite{Cormack81,Baker82,Ada}, in its type analysis.
255The first approach has a late conversion from @double@ to @int@ on the final assignment, while the second has an eager conversion to @int@.
256\CFA minimizes the number of conversions and their potential to lose information, so it selects the first approach, which corresponds with C-programmer intuition.
257
258Crucial to the design of a new programming language are the libraries to access thousands of external software features.
259Like \CC, \CFA inherits a massive compatible library-base, where other programming languages must rewrite or provide fragile inter-language communication with C.
260A simple example is leveraging the existing type-unsafe (@void *@) C @bsearch@ to binary search a sorted float array:
261\begin{lstlisting}
262void * bsearch( const void * key, const void * base, size_t nmemb, size_t size,
263                                int (* compar)( const void *, const void * ));
264int comp( const void * t1, const void * t2 ) { return *(double *)t1 < *(double *)t2 ? -1 :
265                                *(double *)t2 < *(double *)t1 ? 1 : 0; }
266double key = 5.0, vals[10] = { /* 10 sorted float values */ };
267double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp );      $\C{// search sorted array}$
268\end{lstlisting}
269which can be augmented simply with a generalized, type-safe, \CFA-overloaded wrappers:
270\begin{lstlisting}
271forall( otype T | { int ?<?( T, T ); } ) T * bsearch( T key, const T * arr, size_t size ) {
272        int comp( const void * t1, const void * t2 ) { /* as above with double changed to T */ }
273        return (T *)bsearch( &key, arr, size, sizeof(T), comp ); }
274forall( otype T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t size ) {
275        T * result = bsearch( key, arr, size ); $\C{// call first version}$
276        return result ? result - arr : size; }  $\C{// pointer subtraction includes sizeof(T)}$
277double * val = bsearch( 5.0, vals, 10 );        $\C{// selection based on return type}$
278int posn = bsearch( 5.0, vals, 10 );
279\end{lstlisting}
280The nested function @comp@ provides the hidden interface from typed \CFA to untyped (@void *@) C, plus the cast of the result.
281Providing a hidden @comp@ function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope.
282As well, an alternate kind of return is made available: position versus pointer to found element.
283\CC's type-system cannot disambiguate between the two versions of @bsearch@ because it does not use the return type in overload resolution, nor can \CC separately compile a templated @bsearch@.
284
285\CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations.
286For example, it is possible to write a type-safe \CFA wrapper @malloc@ based on the C @malloc@:
287\begin{lstlisting}
288forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); }
289int * ip = malloc();                                            $\C{// select type and size from left-hand side}$
290double * dp = malloc();
291struct S {...} * sp = malloc();
292\end{lstlisting}
293where the return type supplies the type/size of the allocation, which is impossible in most type systems.
294
295Call-site inferencing and nested functions provide a localized form of inheritance.
296For example, the \CFA @qsort@ only sorts in ascending order using @<@.
297However, it is trivial to locally change this behaviour:
298\begin{lstlisting}
299forall( otype T | { int ?<?( T, T ); } ) void qsort( const T * arr, size_t size ) { /* use C qsort */ }
300{       int ?<?( double x, double y ) { return x `>` y; }       $\C{// locally override behaviour}$
301        qsort( vals, size );                                    $\C{// descending sort}$
302}
303\end{lstlisting}
304Within the block, the nested version of @?<?@ performs @?>?@ and this local version overrides the built-in @?<?@ so it is passed to @qsort@.
305Hence, programmers can easily form local environments, adding and modifying appropriate functions, to maximize reuse of other existing functions and types.
306
307Finally, \CFA allows variable overloading:
308\begin{lstlisting}
309short int MAX = ...;   int MAX = ...;  double MAX = ...;
310short int s = MAX;    int i = MAX;    double d = MAX;   $\C{// select correct MAX}$
311\end{lstlisting}
312Here, the single name @MAX@ replaces all the C type-specific names: @SHRT_MAX@, @INT_MAX@, @DBL_MAX@.
313
314\subsection{Traits}
315
316\CFA provides \newterm{traits} to name a group of type assertions, where the trait name allows specifying the same set of assertions in multiple locations, preventing repetition mistakes at each function declaration:
317\begin{lstlisting}
318trait summable( otype T ) {
319        void ?{}( T *, zero_t );                                $\C{// constructor from 0 literal}$
320        T ?+?( T, T );                                                  $\C{// assortment of additions}$
321        T ?+=?( T *, T );
322        T ++?( T * );
323        T ?++( T * ); };
324forall( otype T `| summable( T )` ) T sum( T a[$\,$], size_t size ) {  // use trait
325        `T` total = { `0` };                                    $\C{// instantiate T from 0 by calling its constructor}$
326        for ( unsigned int i = 0; i < size; i += 1 ) total `+=` a[i]; $\C{// select appropriate +}$
327        return total; }
328\end{lstlisting}
329
330In fact, the set of @summable@ trait operators is incomplete, as it is missing assignment for type @T@, but @otype@ is syntactic sugar for the following implicit trait:
331\begin{lstlisting}
332trait otype( dtype T | sized(T) ) {  // sized is a pseudo-trait for types with known size and alignment
333        void ?{}( T * );                                                $\C{// default constructor}$
334        void ?{}( T *, T );                                             $\C{// copy constructor}$
335        void ?=?( T *, T );                                             $\C{// assignment operator}$
336        void ^?{}( T * ); };                                    $\C{// destructor}$
337\end{lstlisting}
338Given the information provided for an @otype@, variables of polymorphic type can be treated as if they were a complete type: stack-allocatable, default or copy-initialized, assigned, and deleted.
339
340In summation, the \CFA type-system uses \newterm{nominal typing} for concrete types, matching with the C type-system, and \newterm{structural typing} for polymorphic types.
341Hence, trait names play no part in type equivalence;
342the names are simply macros for a list of polymorphic assertions, which are expanded at usage sites.
343Nevertheless, trait names form a logical subtype-hierarchy with @dtype@ at the top, where traits often contain overlapping assertions, \eg operator @+@.
344Traits are used like interfaces in Java or abstract base-classes in \CC, but without the nominal inheritance-relationships.
345Instead, each polymorphic function (or generic type) defines the structural type needed for its execution (polymorphic type-key), and this key is fulfilled at each call site from the lexical environment, which is similar to Go~\cite{Go} interfaces.
346Hence, new lexical scopes and nested functions are used extensively to create local subtypes, as in the @qsort@ example, without having to manage a nominal-inheritance hierarchy.
347(Nominal inheritance can be approximated with traits using marker variables or functions, as is done in Go.)
348
349% Nominal inheritance can be simulated with traits using marker variables or functions:
350% \begin{lstlisting}
351% trait nominal(otype T) {
352%     T is_nominal;
353% };
354% int is_nominal;                                                               $\C{// int now satisfies the nominal trait}$
355% \end{lstlisting}
356%
357% Traits, however, are significantly more powerful than nominal-inheritance interfaces; most notably, traits may be used to declare a relationship \emph{among} multiple types, a property that may be difficult or impossible to represent in nominal-inheritance type systems:
358% \begin{lstlisting}
359% trait pointer_like(otype Ptr, otype El) {
360%     lvalue El *?(Ptr);                                                $\C{// Ptr can be dereferenced into a modifiable value of type El}$
361% }
362% struct list {
363%     int value;
364%     list * next;                                                              $\C{// may omit "struct" on type names as in \CC}$
365% };
366% typedef list * list_iterator;
367%
368% lvalue int *?( list_iterator it ) { return it->value; }
369% \end{lstlisting}
370% In the example above, @(list_iterator, int)@ satisfies @pointer_like@ by the user-defined dereference function, and @(list_iterator, list)@ also satisfies @pointer_like@ by the built-in dereference operator for pointers. Given a declaration @list_iterator it@, @*it@ can be either an @int@ or a @list@, with the meaning disambiguated by context (\eg @int x = *it;@ interprets @*it@ as an @int@, while @(*it).value = 42;@ interprets @*it@ as a @list@).
371% While a nominal-inheritance system with associated types could model one of those two relationships by making @El@ an associated type of @Ptr@ in the @pointer_like@ implementation, few such systems could model both relationships simultaneously.
372
373
374\section{Generic Types}
375
376One of the known shortcomings of standard C is that it does not provide reusable type-safe abstractions for generic data structures and algorithms.
377Broadly speaking, there are three approaches to implement abstract data-structures in C.
378One approach is to write bespoke data-structures for each context in which they are needed.
379While this approach is flexible and supports integration with the C type-checker and tooling, it is also tedious and error-prone, especially for more complex data structures.
380A second approach is to use @void *@--based polymorphism, \eg the C standard-library functions @bsearch@ and @qsort@; an approach which does allow reuse of code for common functionality.
381However, basing all polymorphism on @void *@ eliminates the type-checker's ability to ensure that argument types are properly matched, often requiring a number of extra function parameters, pointer indirection, and dynamic allocation that would not otherwise be needed.
382A third approach to generic code is to use preprocessor macros, which does allow the generated code to be both generic and type-checked, but errors may be difficult to interpret.
383Furthermore, writing and using preprocessor macros can be unnatural and inflexible.
384
385\CC, Java, and other languages use \newterm{generic types} to produce type-safe abstract data-types.
386\CFA also implements generic types that integrate efficiently and naturally with the existing polymorphic functions, while retaining backwards compatibility with C and providing separate compilation.
387However, for known concrete parameters, the generic-type definition can be inlined, like \CC templates.
388
389A generic type can be declared by placing a @forall@ specifier on a @struct@ or @union@ declaration, and instantiated using a parenthesized list of types after the type name:
390\begin{lstlisting}
391forall( otype R, otype S ) struct pair {
392        R first;
393        S second;
394};
395forall( otype T ) T value( pair( const char *, T ) p ) { return p.second; }
396forall( dtype F, otype T ) T value_p( pair( F *, T * ) p ) { return * p.second; }
397pair( const char *, int ) p = { "magic", 42 };
398int magic = value( p );
399pair( void *, int * ) q = { 0, &p.second };
400magic = value_p( q );
401double d = 1.0;
402pair( double *, double * ) r = { &d, &d };
403d = value_p( r );
404\end{lstlisting}
405
406\CFA classifies generic types as either \newterm{concrete} or \newterm{dynamic}.
407Concrete types have a fixed memory layout regardless of type parameters, while dynamic types vary in memory layout depending on their type parameters.
408A type may have polymorphic parameters but still be concrete, called \newterm{dtype-static}.
409Polymorphic pointers are an example of dtype-static types, \eg @forall(dtype T) T *@ is a polymorphic type, but for any @T@, @T *@  is a fixed-sized pointer, and therefore, can be represented by a @void *@ in code generation.
410
411\CFA generic types also allow checked argument-constraints.
412For example, the following declaration of a sorted set-type ensures the set key supports equality and relational comparison:
413\begin{lstlisting}
414forall( otype Key | { _Bool ?==?(Key, Key); _Bool ?<?(Key, Key); } ) struct sorted_set;
415\end{lstlisting}
416
417
418\subsection{Concrete Generic-Types}
419
420The \CFA translator template-expands concrete generic-types into new structure types, affording maximal inlining.
421To enable inter-operation among equivalent instantiations of a generic type, the translator saves the set of instantiations currently in scope and reuses the generated structure declarations where appropriate.
422A function declaration that accepts or returns a concrete generic-type produces a declaration for the instantiated structure in the same scope, which all callers may reuse.
423For example, the concrete instantiation for @pair( const char *, int )@ is:
424\begin{lstlisting}
425struct _pair_conc1 {
426        const char * first;
427        int second;
428};
429\end{lstlisting}
430
431A concrete generic-type with dtype-static parameters is also expanded to a structure type, but this type is used for all matching instantiations.
432In the above example, the @pair( F *, T * )@ parameter to @value_p@ is such a type; its expansion is below and it is used as the type of the variables @q@ and @r@ as well, with casts for member access where appropriate:
433\begin{lstlisting}
434struct _pair_conc0 {
435        void * first;
436        void * second;
437};
438\end{lstlisting}
439
440
441\subsection{Dynamic Generic-Types}
442
443Though \CFA implements concrete generic-types efficiently, it also has a fully general system for dynamic generic types.
444As mentioned in Section~\ref{sec:poly-fns}, @otype@ function parameters (in fact all @sized@ polymorphic parameters) come with implicit size and alignment parameters provided by the caller.
445Dynamic generic-types also have an \newterm{offset array} containing structure-member offsets.
446A dynamic generic-union needs no such offset array, as all members are at offset 0, but size and alignment are still necessary.
447Access to members of a dynamic structure is provided at runtime via base-displacement addressing with the structure pointer and the member offset (similar to the @offsetof@ macro), moving a compile-time offset calculation to runtime.
448
449The offset arrays are statically generated where possible.
450If a dynamic generic-type is declared to be passed or returned by value from a polymorphic function, the translator can safely assume the generic type is complete (\ie has a known layout) at any call-site, and the offset array is passed from the caller;
451if the generic type is concrete at the call site, the elements of this offset array can even be statically generated using the C @offsetof@ macro.
452As an example, @p.second@ in the @value@ function above is implemented as @*(p + _offsetof_pair[1])@, where @p@ is a @void *@, and @_offsetof_pair@ is the offset array passed into @value@ for @pair( const char *, T )@.
453The offset array @_offsetof_pair@ is generated at the call site as @size_t _offsetof_pair[] = { offsetof(_pair_conc1, first), offsetof(_pair_conc1, second) }@.
454
455In some cases the offset arrays cannot be statically generated.
456For instance, modularity is generally provided in C by including an opaque forward-declaration of a structure and associated accessor and mutator functions in a header file, with the actual implementations in a separately-compiled @.c@ file.
457\CFA supports this pattern for generic types, but the caller does not know the actual layout or size of the dynamic generic-type, and only holds it by a pointer.
458The \CFA translator automatically generates \newterm{layout functions} for cases where the size, alignment, and offset array of a generic struct cannot be passed into a function from that function's caller.
459These layout functions take as arguments pointers to size and alignment variables and a caller-allocated array of member offsets, as well as the size and alignment of all @sized@ parameters to the generic structure (un@sized@ parameters are forbidden from being used in a context that affects layout).
460Results of these layout functions are cached so that they are only computed once per type per function. %, as in the example below for @pair@.
461Layout functions also allow generic types to be used in a function definition without reflecting them in the function signature.
462For instance, a function that strips duplicate values from an unsorted @vector(T)@ would likely have a pointer to the vector as its only explicit parameter, but use some sort of @set(T)@ internally to test for duplicate values.
463This function could acquire the layout for @set(T)@ by calling its layout function with the layout of @T@ implicitly passed into the function.
464
465Whether a type is concrete, dtype-static, or dynamic is decided solely on the @forall@'s type parameters.
466This design allows opaque forward declarations of generic types, \eg @forall(otype T)@ @struct Box@ -- like in C, all uses of @Box(T)@ can be separately compiled, and callers from other translation units know the proper calling conventions to use.
467If the definition of a structure type is included in deciding whether a generic type is dynamic or concrete, some further types may be recognized as dtype-static (\eg @forall(otype T)@ @struct unique_ptr { T * p }@ does not depend on @T@ for its layout, but the existence of an @otype@ parameter means that it \emph{could}.), but preserving separate compilation (and the associated C compatibility) in the existing design is judged to be an appropriate trade-off.
468
469
470\subsection{Applications}
471\label{sec:generic-apps}
472
473The reuse of dtype-static structure instantiations enables useful programming patterns at zero runtime cost.
474The most important such pattern is using @forall(dtype T) T *@ as a type-checked replacement for @void *@, \eg creating a lexicographic comparison for pairs of pointers used by @bsearch@ or @qsort@:
475\begin{lstlisting}
476forall(dtype T) int lexcmp( pair( T *, T * ) * a, pair( T *, T * ) * b, int (* cmp)( T *, T * ) ) {
477        return cmp( a->first, b->first ) ? : cmp( a->second, b->second );
478}
479\end{lstlisting}
480Since @pair(T *, T * )@ is a concrete type, there are no implicit parameters passed to @lexcmp@, so the generated code is identical to a function written in standard C using @void *@, yet the \CFA version is type-checked to ensure the fields of both pairs and the arguments to the comparison function match in type.
481
482Another useful pattern enabled by reused dtype-static type instantiations is zero-cost \newterm{tag-structures}.
483Sometimes information is only used for type-checking and can be omitted at runtime, \eg:
484\begin{lstlisting}
485forall(dtype Unit) struct scalar { unsigned long value; };
486struct metres {};
487struct litres {};
488
489forall(dtype U) scalar(U) ?+?( scalar(U) a, scalar(U) b ) {
490        return (scalar(U)){ a.value + b.value };
491}
492scalar(metres) half_marathon = { 21093 };
493scalar(litres) swimming_pool = { 2500000 };
494scalar(metres) marathon = half_marathon + half_marathon;
495scalar(litres) two_pools = swimming_pool + swimming_pool;
496marathon + swimming_pool;                                       $\C{// compilation ERROR}$
497\end{lstlisting}
498@scalar@ is a dtype-static type, so all uses have a single structure definition, containing @unsigned long@, and can share the same implementations of common functions like @?+?@.
499These implementations may even be separately compiled, unlike \CC template functions.
500However, the \CFA type-checker ensures matching types are used by all calls to @?+?@, preventing nonsensical computations like adding a length to a volume.
501
502
503\section{Tuples}
504\label{sec:tuples}
505
506In many languages, functions can return at most one value;
507however, many operations have multiple outcomes, some exceptional.
508Consider C's @div@ and @remquo@ functions, which return the quotient and remainder for a division of integer and float values, respectively.
509\begin{lstlisting}
510typedef struct { int quo, rem; } div_t;         $\C{// from include stdlib.h}$
511div_t div( int num, int den );
512double remquo( double num, double den, int * quo );
513div_t qr = div( 13, 5 );                                        $\C{// return quotient/remainder aggregate}$
514int q;
515double r = remquo( 13.5, 5.2, &q );                     $\C{// return remainder, alias quotient}$
516\end{lstlisting}
517@div@ aggregates the quotient/remainder in a structure, while @remquo@ aliases a parameter to an argument.
518Both approaches are awkward.
519Alternatively, a programming language can directly support returning multiple values, \eg in \CFA:
520\begin{lstlisting}
521[ int, int ] div( int num, int den );           $\C{// return two integers}$
522[ double, double ] div( double num, double den ); $\C{// return two doubles}$
523int q, r;                                                                       $\C{// overloaded variable names}$
524double q, r;
525[ q, r ] = div( 13, 5 );                                        $\C{// select appropriate div and q, r}$
526[ q, r ] = div( 13.5, 5.2 );                            $\C{// assign into tuple}$
527\end{lstlisting}
528Clearly, this approach is straightforward to understand and use;
529therefore, why do few programming languages support this obvious feature or provide it awkwardly?
530The answer is that there are complex consequences that cascade through multiple aspects of the language, especially the type-system.
531This section show these consequences and how \CFA handles them.
532
533
534\subsection{Tuple Expressions}
535
536The addition of multiple-return-value functions (MRVF) are useless without a syntax for accepting multiple values at the call-site.
537The simplest mechanism for capturing the return values is variable assignment, allowing the values to be retrieved directly.
538As such, \CFA allows assigning multiple values from a function into multiple variables, using a square-bracketed list of lvalue expressions (as above), called a \newterm{tuple}.
539
540However, functions also use \newterm{composition} (nested calls), with the direct consequence that MRVFs must also support composition to be orthogonal with single-returning-value functions (SRVF), \eg:
541\begin{lstlisting}
542printf( "%d %d\n", div( 13, 5 ) );                      $\C{// return values seperated into arguments}$
543\end{lstlisting}
544Here, the values returned by @div@ are composed with the call to @printf@ by flattening the tuple into separate arguments.
545However, the \CFA type-system must support significantly more complex composition:
546\begin{lstlisting}
547[ int, int ] foo$\(_1\)$( int );                        $\C{// overloaded foo functions}$
548[ double ] foo$\(_2\)$( int );
549void bar( int, double, double );
550bar( foo( 3 ), foo( 3 ) );
551\end{lstlisting}
552The type-resolver only has the tuple return-types to resolve the call to @bar@ as the @foo@ parameters are identical, which involves unifying the possible @foo@ functions with @bar@'s parameter list.
553No combination of @foo@s are an exact match with @bar@'s parameters, so the resolver applies C conversions.
554The minimal cost is @bar( foo@$_1$@( 3 ), foo@$_2$@( 3 ) )@, giving (@int@, {\color{ForestGreen}@int@}, @double@) to (@int@, {\color{ForestGreen}@double@}, @double@) with one {\color{ForestGreen}safe} (widening) conversion from @int@ to @double@ versus ({\color{red}@double@}, {\color{ForestGreen}@int@}, {\color{ForestGreen}@int@}) to ({\color{red}@int@}, {\color{ForestGreen}@double@}, {\color{ForestGreen}@double@}) with one {\color{red}unsafe} (narrowing) conversion from @double@ to @int@ and two safe conversions.
555
556
557\subsection{Tuple Variables}
558
559An important observation from function composition is that new variable names are not required to initialize parameters from an MRVF.
560\CFA also allows declaration of tuple variables that can be initialized from an MRVF, since it can be awkward to declare multiple variables of different types, \eg:
561\begin{lstlisting}
562[ int, int ] qr = div( 13, 5 );                         $\C{// tuple-variable declaration and initialization}$
563[ double, double ] qr = div( 13.5, 5.2 );
564\end{lstlisting}
565where the tuple variable-name serves the same purpose as the parameter name(s).
566Tuple variables can be composed of any types, except for array types, since array sizes are generally unknown in C.
567
568One way to access the tuple-variable components is with assignment or composition:
569\begin{lstlisting}
570[ q, r ] = qr;                                                          $\C{// access tuple-variable components}$
571printf( "%d %d\n", qr );
572\end{lstlisting}
573\CFA also supports \newterm{tuple indexing} to access single components of a tuple expression:
574\begin{lstlisting}
575[int, int] * p = &qr;                                           $\C{// tuple pointer}$
576int rem = qr`.1`;                                                       $\C{// access remainder}$
577int quo = div( 13, 5 )`.0`;                                     $\C{// access quotient}$
578p`->0` = 5;                                                                     $\C{// change quotient}$
579bar( qr`.1`, qr );                                                      $\C{// pass remainder and quotient/remainder}$
580rem = [div( 13, 5 ), 42]`.0.1`;                         $\C{// access 2nd component of 1st component of tuple expression}$
581\end{lstlisting}
582
583
584\subsection{Flattening and Restructuring}
585
586In function call contexts, tuples support implicit flattening and restructuring conversions.
587Tuple flattening recursively expands a tuple into the list of its basic components.
588Tuple structuring packages a list of expressions into a value of tuple type, \eg:
589%\lstDeleteShortInline@%
590%\par\smallskip
591%\begin{tabular}{@{}l@{\hspace{1.5\parindent}}||@{\hspace{1.5\parindent}}l@{}}
592\begin{lstlisting}
593int f( int, int );
594int g( [int, int] );
595int h( int, [int, int] );
596[int, int] x;
597int y;
598f( x );                 $\C{// flatten}$
599g( y, 10 );             $\C{// structure}$
600h( x, y );              $\C{// flatten and structure}$
601\end{lstlisting}
602%\end{lstlisting}
603%&
604%\begin{lstlisting}
605%\end{tabular}
606%\smallskip\par\noindent
607%\lstMakeShortInline@%
608In the call to @f@, @x@ is implicitly flattened so the components of @x@ are passed as the two arguments.
609In the call to @g@, the values @y@ and @10@ are structured into a single argument of type @[int, int]@ to match the parameter type of @g@.
610Finally, in the call to @h@, @x@ is flattened to yield an argument list of length 3, of which the first component of @x@ is passed as the first parameter of @h@, and the second component of @x@ and @y@ are structured into the second argument of type @[int, int]@.
611The flexible structure of tuples permits a simple and expressive function call syntax to work seamlessly with both SRVF and MRVF, and with any number of arguments of arbitrarily complex structure.
612
613
614\subsection{Tuple Assignment}
615
616An assignment where the left side is a tuple type is called \newterm{tuple assignment}.
617There 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 \newterm{multiple} and \newterm{mass assignment}, respectively.
618%\lstDeleteShortInline@%
619%\par\smallskip
620%\begin{tabular}{@{}l@{\hspace{1.5\parindent}}||@{\hspace{1.5\parindent}}l@{}}
621\begin{lstlisting}
622int x = 10;
623double y = 3.5;
624[int, double] z;
625z = [x, y];                                                                     $\C{// multiple assignment}$
626[x, y] = z;                                                                     $\C{// multiple assignment}$
627z = 10;                                                                         $\C{// mass assignment}$
628[y, x] = 3.14;                                                          $\C{// mass assignment}$
629\end{lstlisting}
630%\end{lstlisting}
631%&
632%\begin{lstlisting}
633%\end{tabular}
634%\smallskip\par\noindent
635%\lstMakeShortInline@%
636Both kinds of tuple assignment have parallel semantics, so that each value on the left and right side is evaluated before any assignments occur.
637As a result, it is possible to swap the values in two variables without explicitly creating any temporary variables or calling a function, \eg, @[x, y] = [y, x]@.
638This semantics means mass assignment differs from C cascading assignment (\eg @a = b = c@) in that conversions are applied in each individual assignment, which prevents data loss from the chain of conversions that can happen during a cascading assignment.
639For example, @[y, x] = 3.14@ performs the assignments @y = 3.14@ and @x = 3.14@, yielding @y == 3.14@ and @x == 3@;
640whereas, C cascading assignment @y = x = 3.14@ performs the assignments @x = 3.14@ and @y = x@, yielding @3@ in @y@ and @x@.
641Finally, tuple assignment is an expression where the result type is the type of the left-hand side of the assignment, just like all other assignment expressions in C.
642This example shows mass, multiple, and cascading assignment used in one expression:
643\begin{lstlisting}
644void f( [int, int] );
645f( [x, y] = z = 1.5 );                                          $\C{// assignments in parameter list}$
646\end{lstlisting}
647
648
649\subsection{Member Access}
650
651It is also possible to access multiple fields from a single expression using a \newterm{member-access}.
652The result is a single tuple-valued expression whose type is the tuple of the types of the members, \eg:
653\begin{lstlisting}
654struct S { int x; double y; char * z; } s;
655s.[x, y, z] = 0;
656\end{lstlisting}
657Here, the mass assignment sets all members of @s@ to zero.
658Since 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, drop, and duplicate components).
659%\lstDeleteShortInline@%
660%\par\smallskip
661%\begin{tabular}{@{}l@{\hspace{1.5\parindent}}||@{\hspace{1.5\parindent}}l@{}}
662\begin{lstlisting}
663[int, int, long, double] x;
664void f( double, long );
665x.[0, 1] = x.[1, 0];                                            $\C{// rearrange: [x.0, x.1] = [x.1, x.0]}$
666f( x.[0, 3] );                                                          $\C{// drop: f(x.0, x.3)}$
667[int, int, int] y = x.[2, 0, 2];                        $\C{// duplicate: [y.0, y.1, y.2] = [x.2, x.0.x.2]}$
668\end{lstlisting}
669%\end{lstlisting}
670%&
671%\begin{lstlisting}
672%\end{tabular}
673%\smallskip\par\noindent
674%\lstMakeShortInline@%
675It is also possible for a member access to contain other member accesses, \eg:
676\begin{lstlisting}
677struct A { double i; int j; };
678struct B { int * k; short l; };
679struct C { int x; A y; B z; } v;
680v.[x, y.[i, j], z.k];                                           $\C{// [v.x, [v.y.i, v.y.j], v.z.k]}$
681\end{lstlisting}
682
683
684\begin{comment}
685\subsection{Casting}
686
687In C, the cast operator is used to explicitly convert between types.
688In \CFA, the cast operator has a secondary use as type ascription.
689That is, a cast can be used to select the type of an expression when it is ambiguous, as in the call to an overloaded function:
690\begin{lstlisting}
691int f();     // (1)
692double f()// (2)
693
694f();       // ambiguous - (1),(2) both equally viable
695(int)f()// choose (2)
696\end{lstlisting}
697
698Since casting is a fundamental operation in \CFA, casts should be given a meaningful interpretation in the context of tuples.
699Taking a look at standard C provides some guidance with respect to the way casts should work with tuples:
700\begin{lstlisting}
701int f();
702void g();
703
704(void)f()// (1)
705(int)g()// (2)
706\end{lstlisting}
707In C, (1) is a valid cast, which calls @f@ and discards its result.
708On the other hand, (2) is invalid, because @g@ does not produce a result, so requesting an @int@ to materialize from nothing is nonsensical.
709Generalizing these principles, any cast wherein the number of components increases as a result of the cast is invalid, while casts that have the same or fewer number of components may be valid.
710
711Formally, a cast to tuple type is valid when $T_n \leq S_m$, where $T_n$ is the number of components in the target type and $S_m$ is the number of components in the source type, and for each $i$ in $[0, n)$, $S_i$ can be cast to $T_i$.
712Excess elements ($S_j$ for all $j$ in $[n, m)$) are evaluated, but their values are discarded so that they are not included in the result expression.
713This approach follows naturally from the way that a cast to @void@ works in C.
714
715For example, in
716\begin{lstlisting}
717[int, int, int] f();
718[int, [int, int], int] g();
719
720([int, double])f();           $\C{// (1)}$
721([int, int, int])g();         $\C{// (2)}$
722([void, [int, int]])g();      $\C{// (3)}$
723([int, int, int, int])g();    $\C{// (4)}$
724([int, [int, int, int]])g()$\C{// (5)}$
725\end{lstlisting}
726
727(1) discards the last element of the return value and converts the second element to @double@.
728Since @int@ is effectively a 1-element tuple, (2) discards the second component of the second element of the return value of @g@.
729If @g@ is free of side effects, this expression is equivalent to @[(int)(g().0), (int)(g().1.0), (int)(g().2)]@.
730Since @void@ is effectively a 0-element tuple, (3) discards the first and third return values, which is effectively equivalent to @[(int)(g().1.0), (int)(g().1.1)]@).
731
732Note that a cast is not a function call in \CFA, so flattening and structuring conversions do not occur for cast expressions\footnote{User-defined conversions have been considered, but for compatibility with C and the existing use of casts as type ascription, any future design for such conversions would require more precise matching of types than allowed for function arguments and parameters.}.
733As such, (4) is invalid because the cast target type contains 4 components, while the source type contains only 3.
734Similarly, (5) is invalid because the cast @([int, int, int])(g().1)@ is invalid.
735That is, it is invalid to cast @[int, int]@ to @[int, int, int]@.
736\end{comment}
737
738
739\subsection{Polymorphism}
740
741Tuples also integrate with \CFA polymorphism as a kind of generic type.
742Due to the implicit flattening and structuring conversions involved in argument passing, @otype@ and @dtype@ parameters are restricted to matching only with non-tuple types, \eg:
743\begin{lstlisting}
744forall(otype T, dtype U) void f( T x, U * y );
745f( [5, "hello"] );
746\end{lstlisting}
747where @[5, "hello"]@ is flattened, giving argument list @5, "hello"@, and @T@ binds to @int@ and @U@ binds to @const char@.
748Tuples, however, may contain polymorphic components.
749For example, a plus operator can be written to add two triples together.
750\begin{lstlisting}
751forall(otype T | { T ?+?( T, T ); }) [T, T, T] ?+?( [T, T, T] x, [T, T, T] y ) {
752        return [x.0 + y.0, x.1 + y.1, x.2 + y.2];
753}
754[int, int, int] x;
755int i1, i2, i3;
756[i1, i2, i3] = x + ([10, 20, 30]);
757\end{lstlisting}
758
759Flattening and restructuring conversions are also applied to tuple types in polymorphic type assertions.
760\begin{lstlisting}
761int f( [int, double], double );
762forall(otype T, otype U | { T f( T, U, U ); }) void g( T, U );
763g( 5, 10.21 );
764\end{lstlisting}
765Hence, function parameter and return lists are flattened for the purposes of type unification allowing the example to pass expression resolution.
766This relaxation is possible by extending the thunk scheme described by Bilson~\cite{Bilson03}.
767Whenever a candidate's parameter structure does not exactly match the formal parameter's structure, a thunk is generated to specialize calls to the actual function:
768\begin{lstlisting}
769int _thunk( int _p0, double _p1, double _p2 ) { return f( [_p0, _p1], _p2 ); }
770\end{lstlisting}
771so the thunk provides flattening and structuring conversions to inferred functions, improving the compatibility of tuples and polymorphism.
772These thunks take advantage of GCC C nested-functions to produce closures that have the usual function-pointer signature.
773
774
775\subsection{Variadic Tuples}
776\label{sec:variadic-tuples}
777
778To define variadic functions, \CFA adds a new kind of type parameter, @ttype@ (tuple type).
779Matching against a @ttype@ parameter consumes all remaining argument components and packages them into a tuple, binding to the resulting tuple of types.
780In a given parameter list, there must be at most one @ttype@ parameter that occurs last, which matches normal variadic semantics, with a strong feeling of similarity to \CCeleven variadic templates.
781As such, @ttype@ variables are also called \newterm{argument packs}.
782
783Like variadic templates, the main way to manipulate @ttype@ polymorphic functions is via recursion.
784Since nothing is known about a parameter pack by default, assertion parameters are key to doing anything meaningful.
785Unlike variadic templates, @ttype@ polymorphic functions can be separately compiled.
786For example, a generalized @sum@ function written using @ttype@:
787\begin{lstlisting}
788int sum$\(_0\)$() { return 0; }
789forall(ttype Params | { int sum( Params ); } ) int sum$\(_1\)$( int x, Params rest ) {
790        return x + sum( rest );
791}
792sum( 10, 20, 30 );
793\end{lstlisting}
794Since @sum@\(_0\) does not accept any arguments, it is not a valid candidate function for the call @sum(10, 20, 30)@.
795In order to call @sum@\(_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]@.
796The process continues unitl @Params@ is bound to @[]@, requiring an assertion @int sum()@, which matches @sum@\(_0\) and terminates the recursion.
797Effectively, this algorithm 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))@.
798
799It is reasonable to take the @sum@ function a step further to enforce a minimum number of arguments:
800\begin{lstlisting}
801int sum( int x, int y ) { return x + y; }
802forall(ttype Params | { int sum( int, Params ); } ) int sum( int x, int y, Params rest ) {
803        return sum( x + y, rest );
804}
805\end{lstlisting}
806One more step permits the summation of any summable type with all arguments of the same type:
807\begin{lstlisting}
808trait summable(otype T) {
809        T ?+?( T, T );
810};
811forall(otype R | summable( R ) ) R sum( R x, R y ) {
812        return x + y;
813}
814forall(otype R, ttype Params | summable(R) | { R sum(R, Params); } ) R sum(R x, R y, Params rest) {
815        return sum( x + y, rest );
816}
817\end{lstlisting}
818Unlike C variadic functions, it is unnecessary to hard code the number and expected types.
819Furthermore, this code is extendable for any user-defined type with a @?+?@ operator.
820Summing arbitrary heterogeneous lists is possible with similar code by adding the appropriate type variables and addition operators.
821
822It is also possible to write a type-safe variadic print function to replace @printf@:
823\begin{lstlisting}
824struct S { int x, y; };
825forall(otype T, ttype Params | { void print(T); void print(Params); }) void print(T arg, Params rest) {
826        print(arg);  print(rest);
827}
828void print( char * x ) { printf( "%s", x ); }
829void print( int x ) { printf( "%d", x ); }
830void print( S s ) { print( "{ ", s.x, ",", s.y, " }" ); }
831print( "s = ", (S){ 1, 2 }, "\n" );
832\end{lstlisting}
833This example showcases a variadic-template-like decomposition of the provided argument list.
834The individual @print@ functions allow printing a single element of a type.
835The polymorphic @print@ allows printing any list of types, where as each individual type has a @print@ function.
836The individual print functions can be used to build up more complicated @print@ functions, such as @S@, which cannot be done with @printf@ in C.
837
838Finally, it is possible to use @ttype@ polymorphism to provide arbitrary argument forwarding functions.
839For example, it is possible to write @new@ as a library function:
840\begin{lstlisting}
841forall( otype R, otype S ) void ?{}( pair(R, S) *, R, S );
842forall( dtype T, ttype Params | sized(T) | { void ?{}( T *, Params ); } ) T * new( Params p ) {
843        return ((T *)malloc()){ p };                    $\C{// construct into result of malloc}$
844}
845pair( int, char ) * x = new( 42, '!' );
846\end{lstlisting}
847The @new@ function provides the combination of type-safe @malloc@ with a \CFA constructor call, making it impossible to forget constructing dynamically allocated objects.
848This function provides the type-safety of @new@ in \CC, without the need to specify the allocated type again, thanks to return-type inference.
849
850
851\subsection{Implementation}
852
853Tuples are implemented in the \CFA translator via a transformation into \newterm{generic types}.
854For each $N$, the first time an $N$-tuple is seen in a scope a generic type with $N$ type parameters is generated, \eg:
855\begin{lstlisting}
856[int, int] f() {
857        [double, double] x;
858        [int, double, int] y;
859}
860\end{lstlisting}
861is transformed into:
862\begin{lstlisting}
863forall(dtype T0, dtype T1 | sized(T0) | sized(T1)) struct _tuple2 {
864        T0 field_0;                                                             $\C{// generated before the first 2-tuple}$
865        T1 field_1;
866};
867_tuple2(int, int) f() {
868        _tuple2(double, double) x;
869        forall(dtype T0, dtype T1, dtype T2 | sized(T0) | sized(T1) | sized(T2)) struct _tuple3 {
870                T0 field_0;                                                     $\C{// generated before the first 3-tuple}$
871                T1 field_1;
872                T2 field_2;
873        };
874        _tuple3(int, double, int) y;
875}
876\end{lstlisting}
877\begin{sloppypar}
878Tuple expressions are then simply converted directly into compound literals, \eg @[5, 'x', 1.24]@ becomes @(_tuple3(int, char, double)){ 5, 'x', 1.24 }@.
879\end{sloppypar}
880
881\begin{comment}
882Since tuples are essentially structures, tuple indexing expressions are just field accesses:
883\begin{lstlisting}
884void f(int, [double, char]);
885[int, double] x;
886
887x.0+x.1;
888printf("%d %g\n", x);
889f(x, 'z');
890\end{lstlisting}
891Is transformed into:
892\begin{lstlisting}
893void f(int, _tuple2(double, char));
894_tuple2(int, double) x;
895
896x.field_0+x.field_1;
897printf("%d %g\n", x.field_0, x.field_1);
898f(x.field_0, (_tuple2){ x.field_1, 'z' });
899\end{lstlisting}
900Note that due to flattening, @x@ used in the argument position is converted into the list of its fields.
901In the call to @f@, the second and third argument components are structured into a tuple argument.
902Similarly, tuple member expressions are recursively expanded into a list of member access expressions.
903
904Expressions that may contain side effects are made into \newterm{unique expressions} before being expanded by the flattening conversion.
905Each unique expression is assigned an identifier and is guaranteed to be executed exactly once:
906\begin{lstlisting}
907void g(int, double);
908[int, double] h();
909g(h());
910\end{lstlisting}
911Internally, this expression is converted to two variables and an expression:
912\begin{lstlisting}
913void g(int, double);
914[int, double] h();
915
916_Bool _unq0_finished_ = 0;
917[int, double] _unq0;
918g(
919        (_unq0_finished_ ? _unq0 : (_unq0 = f(), _unq0_finished_ = 1, _unq0)).0,
920        (_unq0_finished_ ? _unq0 : (_unq0 = f(), _unq0_finished_ = 1, _unq0)).1,
921);
922\end{lstlisting}
923Since argument evaluation order is not specified by the C programming language, this scheme is built to work regardless of evaluation order.
924The first time a unique expression is executed, the actual expression is evaluated and the accompanying boolean is set to true.
925Every subsequent evaluation of the unique expression then results in an access to the stored result of the actual expression.
926Tuple member expressions also take advantage of unique expressions in the case of possible impurity.
927
928Currently, the \CFA translator has a very broad, imprecise definition of impurity, where any function call is assumed to be impure.
929This notion could be made more precise for certain intrinsic, auto-generated, and builtin functions, and could analyze function bodies when they are available to recursively detect impurity, to eliminate some unique expressions.
930
931The various kinds of tuple assignment, constructors, and destructors generate GNU C statement expressions.
932A variable is generated to store the value produced by a statement expression, since its fields may need to be constructed with a non-trivial constructor and it may need to be referred to multiple time, \eg in a unique expression.
933The use of statement expressions allows the translator to arbitrarily generate additional temporary variables as needed, but binds the implementation to a non-standard extension of the C language.
934However, there are other places where the \CFA translator makes use of GNU C extensions, such as its use of nested functions, so this restriction is not new.
935\end{comment}
936
937
938\section{Control Structures}
939
940\CFA identifies missing and problematic control structures in C, and extends and modifies these control structures to increase functionality and safety.
941
942
943\subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break}}{Labelled continue / break}}
944
945While C provides @continue@ and @break@ statements for altering control flow, both are restricted to one level of nesting for a particular control structure.
946Unfortunately, this restriction forces programmers to use @goto@ to achieve the equivalent control-flow for more than one level of nesting.
947To prevent having to switch to the @goto@, \CFA extends the @continue@ and @break@ with a target label to support static multi-level exit~\cite{Buhr85}, as in Java.
948For both @continue@ and @break@, the target label must be directly associated with a @for@, @while@ or @do@ statement;
949for @break@, the target label can also be associated with a @switch@, @if@ or compound (@{}@) statement.
950Figure~\ref{f:MultiLevelExit} shows @continue@ and @break@ indicating the specific control structure, and the corresponding C program using only @goto@ and labels.
951The innermost loop has 7 exit points, which cause continuation or termination of one or more of the 7 nested control-structures.
952
953\begin{figure}
954\lstDeleteShortInline@%
955\begin{tabular}{@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{\hspace{\parindentlnth}}l@{}}
956\multicolumn{1}{@{\hspace{\parindentlnth}}c@{\hspace{\parindentlnth}}}{\textbf{\CFA}}   & \multicolumn{1}{@{\hspace{\parindentlnth}}c}{\textbf{C}}      \\
957\begin{cfa}
958`LC:` {
959        ... $declarations$ ...
960        `LS:` switch ( ... ) {
961          case 3:
962                `LIF:` if ( ... ) {
963                        `LF:` for ( ... ) {
964                                `LW:` while ( ... ) {
965                                        ... break `LC`; ...
966                                        ... break `LS`; ...
967                                        ... break `LIF`; ...
968                                        ... continue `LF;` ...
969                                        ... break `LF`; ...
970                                        ... continue `LW`; ...
971                                        ... break `LW`; ...
972                                } // while
973                        } // for
974                } else {
975                        ... break `LIF`; ...
976                } // if
977        } // switch
978} // compound
979\end{cfa}
980&
981\begin{cfa}
982{
983        ... $declarations$ ...
984        switch ( ... ) {
985          case 3:
986                if ( ... ) {
987                        for ( ... ) {
988                                while ( ... ) {
989                                        ... goto `LC`; ...
990                                        ... goto `LS`; ...
991                                        ... goto `LIF`; ...
992                                        ... goto `LFC`; ...
993                                        ... goto `LFB`; ...
994                                        ... goto `LWC`; ...
995                                        ... goto `LWB`; ...
996                                  `LWC`: ; } `LWB:` ;
997                          `LFC:` ; } `LFB:` ;
998                } else {
999                        ... goto `LIF`; ...
1000                } `L3:` ;
1001        } `LS:` ;
1002} `LC:` ;
1003\end{cfa}
1004&
1005\begin{cfa}
1006
1007
1008
1009
1010
1011
1012
1013// terminate compound
1014// terminate switch
1015// terminate if
1016// continue loop
1017// terminate loop
1018// continue loop
1019// terminate loop
1020
1021
1022
1023// terminate if
1024
1025
1026
1027\end{cfa}
1028\end{tabular}
1029\lstMakeShortInline@%
1030\caption{Multi-level Exit}
1031\label{f:MultiLevelExit}
1032\end{figure}
1033
1034Both labelled @continue@ and @break@ are a @goto@ restricted in the following ways:
1035\begin{itemize}
1036\item
1037They cannot create a loop, which means only the looping constructs cause looping.
1038This restriction means all situations resulting in repeated execution are clearly delineated.
1039\item
1040They cannot branch into a control structure.
1041This restriction prevents missing declarations and/or initializations at the start of a control structure resulting in undefined behaviour.
1042\end{itemize}
1043The advantage of the labelled @continue@/@break@ is allowing static multi-level exits without having to use the @goto@ statement, and tying control flow to the target control structure rather than an arbitrary point in a program.
1044Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader (eye candy) that complex control-flow is occurring in the body of the control structure.
1045With @goto@, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.
1046Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs.
1047The implicit targets of the current @continue@ and @break@, \ie the closest enclosing loop or @switch@, change as certain constructs are added or removed.
1048
1049
1050\subsection{\texorpdfstring{Enhanced \LstKeywordStyle{switch} Statement}{Enhanced switch Statement}}
1051
1052There are a number of deficiencies with the C @switch@ statements: enumerating @case@ lists, placement of @case@ clauses, scope of the switch body, and fall through between case clauses.
1053
1054C has no shorthand for specifying a list of case values, whether the list is non-contiguous or contiguous\footnote{C provides this mechanism via fall through.}.
1055\CFA provides a shorthand for a non-contiguous list:
1056\begin{cquote}
1057\lstDeleteShortInline@%
1058\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1059\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1060\begin{cfa}
1061case 2, 10, 34, 42:
1062\end{cfa}
1063&
1064\begin{cfa}
1065case 2: case 10: case 34: case 42:
1066\end{cfa}
1067\end{tabular}
1068\lstMakeShortInline@%
1069\end{cquote}
1070for a contiguous list:\footnote{gcc provides the same mechanism with awkward syntax, \lstinline@2 ... 42@, where spaces are required around the ellipse.}
1071\begin{cquote}
1072\lstDeleteShortInline@%
1073\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1074\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1075\begin{cfa}
1076case 2~42:
1077\end{cfa}
1078&
1079\begin{cfa}
1080case 2: case 3: ... case 41: case 42:
1081\end{cfa}
1082\end{tabular}
1083\lstMakeShortInline@%
1084\end{cquote}
1085and a combination:
1086\begin{cfa}
1087case -12~-4, -1~5, 14~21, 34~42:
1088\end{cfa}
1089
1090C allows placement of @case@ clauses \emph{within} statements nested in the @switch@ body (see Duff's device~\cite{Duff83});
1091\begin{cfa}
1092switch ( i ) {
1093  case 0:
1094        for ( int i = 0; i < 10; i += 1 ) {
1095                ...
1096  `case 1:`             // no initialization of loop index
1097                ...
1098        }
1099}
1100\end{cfa}
1101\CFA precludes this form of transfer into a control structure because it causes undefined behaviour, especially with respect to missed initialization, and provides very limited functionality.
1102
1103C allows placement of declaration within the @switch@ body and unreachable code at the start, resulting in undefined behaviour:
1104\begin{cfa}
1105switch ( x ) {
1106        `int y = 1;`                            $\C{// unreachable initialization}$
1107        `x = 7;`                                        $\C{// unreachable code without label/branch}$
1108  case 0:
1109        ...
1110        `int z = 0;`                            $\C{// unreachable initialization, cannot appear after case}$
1111        z = 2;
1112  case 1:
1113        `x = z;`                                        $\C{// without fall through, z is undefined}$
1114}
1115\end{cfa}
1116\CFA allows the declaration of local variables, \eg @y@, at the start of the @switch@ with scope across the entire @switch@ body, \ie all @case@ clauses, but no statements.
1117\CFA disallows the declaration of local variable, \eg @z@, directly within the @switch@ body, because a declaration cannot occur immediately after a @case@ since a label can only be attached to a statement, and the use of @z@ is undefined in @case 1@ as neither storage allocation nor initialization may have occurred.
1118
1119C @switch@ provides multiple entry points into the statement body, but once an entry point is selected, control continues across \emph{all} @case@ clauses until the end of the @switch@ body, called \newterm{fall through};
1120@case@ clauses are made disjoint by the @break@ statement.
1121While the ability to fall through \emph{is} a useful form of control flow, it does not match well with programmer intuition, resulting in many errors from missing @break@ statements.
1122\CFA provides a new control structure, @choose@, which mimics @switch@, but reverses the meaning of fall through:
1123\begin{cquote}
1124\lstDeleteShortInline@%
1125\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1126\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1127\begin{cfa}
1128`choose` ( day ) {
1129  case Mon~Thu:
1130        // program
1131
1132  case Fri:
1133        // program
1134        wallet += pay;
1135        `fallthrough;`
1136  case Sat:
1137        // party
1138        wallet -= party;
1139
1140  case Sun:
1141        // rest
1142
1143  default:
1144        // error
1145}
1146\end{cfa}
1147&
1148\begin{cfa}
1149switch ( day ) {
1150  case Mon: case Tue: case Wed: case Thu:
1151        // program
1152        `break;`
1153  case Fri:
1154        // program
1155        wallet += pay;
1156
1157  case Sat:
1158        // party
1159        wallet -= party;
1160        `break;`
1161  case Sun:
1162        // rest
1163        `break;`
1164  default:
1165        // error
1166}
1167\end{cfa}
1168\end{tabular}
1169\lstMakeShortInline@%
1170\end{cquote}
1171Collectively, these enhancements reduce programmer burden and increase readability and safety.
1172
1173\begin{comment}
1174Forgotten @break@ statements at the end of @switch@ cases are a persistent sort of programmer error in C, and the @break@ statements themselves introduce visual clutter and an un-C-like keyword-based block delimiter.
1175\CFA addresses this error by introducing a @choose@ statement, which works identically to a @switch@ except that its default end-of-case behaviour is to break rather than to fall through for all non-empty cases.
1176Since empty cases like @case 7:@ in @case 7: case 11:@ still have fall-through semantics and explicit @break@ is still allowed at the end of a @choose@ case, many idiomatic uses of @switch@ in standard C can be converted to @choose@ statements by simply changing the keyword.
1177Where fall-through is desired for a non-empty case, it can be specified with the new @fallthrough@ statement, making @choose@ equivalently powerful to @switch@, but more concise in the common case where most non-empty cases end with a @break@ statement, as in the example below:
1178
1179\begin{cfa}
1180choose( i ) {
1181        case 2:
1182                printf("even ");
1183                fallthrough;
1184        case 3: case 5: case 7:
1185                printf("small prime\n");
1186        case 4,6,8,9:
1187                printf("small composite\n");
1188        case 13~19:
1189                printf("teen\n");
1190        default:
1191                printf("something else\n");
1192}
1193\end{cfa}
1194\end{comment}
1195
1196
1197\subsection{\texorpdfstring{\LstKeywordStyle{with} Clause / Statement}{with Clause / Statement}}
1198\label{s:WithClauseStatement}
1199
1200Grouping heterogenous data into \newterm{aggregate}s (structure/union) is a common programming practice, and an aggregate can be further organized into more complex structures, such as arrays and containers:
1201\begin{cfa}
1202struct S {                                                                      $\C{// aggregate}$
1203        char c;                                                                 $\C{// fields}$
1204        int i;
1205        double d;
1206};
1207S s, as[10];
1208\end{cfa}
1209However, routines manipulating aggregates must repeat the aggregate name to access its containing fields:
1210\begin{cfa}
1211void f( S s ) {
1212        `s.`c; `s.`i; `s.`d;                                    $\C{// access containing fields}$
1213}
1214\end{cfa}
1215A similar situation occurs in object-oriented programming, \eg \CC:
1216\begin{C++}
1217class C {
1218        char c;                                                                 $\C{// fields}$
1219        int i;
1220        double d;
1221        int mem() {                                                             $\C{// implicit "this" parameter}$
1222                `this->`c; `this->`i; `this->`d;        $\C{// access containing fields}$
1223        }
1224}
1225\end{C++}
1226Nesting of member routines in a \lstinline[language=C++]@class@ allows eliding \lstinline[language=C++]@this->@ because of lexical scoping.
1227However, for other aggregate parameters, qualification is necessary:
1228\begin{cfa}
1229struct T { double m, n; };
1230int C::mem( T & t ) {                                           $\C{// multiple aggregate parameters}$
1231        c; i; d;                                                                $\C{\color{red}// this-\textgreater.c, this-\textgreater.i, this-\textgreater.d}$
1232        `t.`m; `t.`n;                                                   $\C{// must qualify}$
1233}
1234\end{cfa}
1235
1236% In object-oriented programming, there is an implicit first parameter, often names @self@ or @this@, which is elided.
1237% In any programming language, some functions have a naturally close relationship with a particular data type.
1238% Object-oriented programming allows this close relationship to be codified in the language by making such functions \newterm{class methods} of their related data type.
1239% Class methods have certain privileges with respect to their associated data type, notably un-prefixed access to the fields of that data type.
1240% When writing C functions in an object-oriented style, this un-prefixed access is swiftly missed, as access to fields of a @Foo* f@ requires an extra three characters @f->@ every time, which disrupts coding flow and clutters the produced code.
1241%
1242% \TODO{Fill out section. Be sure to mention arbitrary expressions in with-blocks, recent change driven by Thierry to prioritize field name over parameters.}
1243
1244To simplify the programmer experience, \CFA provides a @with@ clause/statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers.
1245Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block.
1246\begin{cfa}
1247void f( S s ) `with( s )` {                                     $\C{// with clause}$
1248        c; i; d;                                                                $\C{\color{red}// s.c, s.i, s.d}$
1249}
1250\end{cfa}
1251and the equivalence for object-style programming is:
1252\begin{cfa}
1253int mem( S & this ) `with( this )` {            $\C{// with clause}$
1254        c; i; d;                                                                $\C{\color{red}// this.c, this.i, this.d}$
1255}
1256\end{cfa}
1257with the generality of opening multiple aggregate-parameters:
1258\begin{cfa}
1259int mem( S & s, T & t ) `with( s, t )` {        $\C{// multiple aggregate parameters}$
1260        c; i; d;                                                                $\C{\color{red}// s.c, s.i, s.d}$
1261        m; n;                                                                   $\C{\color{red}// t.m, t.n}$
1262}
1263\end{cfa}
1264
1265In detail, the @with@ clause/statement has the form:
1266\begin{cfa}
1267$\emph{with-statement}$:
1268        'with' '(' $\emph{expression-list}$ ')' $\emph{compound-statement}$
1269\end{cfa}
1270and may appear as the body of a routine or nested within a routine body.
1271Each expression in the expression-list provides a type and object.
1272The type must be an aggregate type.
1273(Enumerations are already opened.)
1274The object is the implicit qualifier for the open structure-fields.
1275
1276All expressions in the expression list are open in ``parallel'' within the compound statement.
1277This semantic is different from Pascal, which nests the openings.
1278The difference between parallel and nesting occurs for fields with the same name but different type:
1279\begin{cfa}
1280struct S { int i; int j; double m; } s, w;
1281struct T { int i; int k; int m } t, w;
1282with( s, t ) {
1283        j + k;                                                                  $\C{// unambiguous, s.j + t.m}$
1284        m = 5.0;                                                                $\C{// unambiguous, t.m = 5.0}$
1285        m = 1;                                                                  $\C{// unambiguous, s.m = 1}$
1286        int a = s.i + m;                                                $\C{// unambiguous, a = s.i + t.i}$
1287        int b = s.i + t.i;                                              $\C{// unambiguous, qualification}$
1288        sout | (double)m | endl;                                $\C{// unambiguous, cast}$
1289        i;                                                                              $\C{// ambiguous}$
1290}
1291\end{cfa}
1292\CFA's ability to overload variables means usages of field with the same names can be automatically disambiguated, eliminating most qualification.
1293Qualification or a cast is used to disambiguate.
1294A cast may be necessary to disambiguate between the overload variables in a @with@ expression:
1295\begin{cfa}
1296with( w ) { ... }                                                       $\C{// ambiguous, same name and no context}$
1297with( (S)w ) { ... }                                            $\C{// unambiguous}$
1298\end{cfa}
1299
1300\begin{cfa}
1301struct S { int i, j; } sv;
1302with( sv ) {
1303        S & sr = sv;
1304        with( sr ) {
1305                S * sp = &sv;
1306                with( *sp ) {
1307                        i = 3; j = 4;                                   $\C{\color{red}// sp-{\textgreater}i, sp-{\textgreater}j}$
1308                }
1309                i = 3; j = 4;                                           $\C{\color{red}// sr.i, sr.j}$
1310        }
1311        i = 3; j = 4;                                                   $\C{\color{red}// sv.i, sv.j}$
1312}
1313\end{cfa}
1314
1315The statement form is used within a block:
1316\begin{cfa}
1317int foo() {
1318        struct S1 { ... } s1;
1319        struct S2 { ... } s2;
1320        `with( s1 )` {                                                  $\C{// with statement}$
1321                // access fields of s1 without qualification
1322                `with( s2 )` {                                          $\C{// nesting}$
1323                        // access fields of s1 and s2 without qualification
1324                }
1325        }
1326        `with( s1, s2 )` {
1327                // access unambiguous fields of s1 and s2 without qualification
1328        }
1329}
1330\end{cfa}
1331
1332
1333\subsection{Exception Handling ???}
1334
1335
1336\section{Declarations}
1337
1338It is important to the design team that \CFA subjectively ``feel like'' C to user programmers.
1339An important part of this subjective feel is maintaining C's procedural programming paradigm, as opposed to the object-oriented paradigm of other systems languages such as \CC and Rust.
1340Maintaining this procedural paradigm means that coding patterns that work in C will remain not only functional but idiomatic in \CFA, reducing the mental burden of retraining C programmers and switching between C and \CFA development.
1341Nonetheless, some features of object-oriented languages are undeniably convienient, and the \CFA design team has attempted to adapt them to a procedural paradigm so as to incorporate their benefits into \CFA; two of these features are resource management and name scoping.
1342
1343
1344\subsection{Alternative Declaration Syntax}
1345
1346\newcommand{\R}[1]{\Textbf{#1}}
1347\newcommand{\B}[1]{{\Textbf[blue]{#1}}}
1348\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
1349
1350C declaration syntax is notoriously confusing and error prone.
1351For example, many C programmers are confused by a declaration as simple as:
1352\begin{cquote}
1353\lstDeleteShortInline@%
1354\begin{tabular}{@{}ll@{}}
1355\begin{cfa}
1356int * x[5]
1357\end{cfa}
1358&
1359\raisebox{-0.75\totalheight}{\input{Cdecl}}
1360\end{tabular}
1361\lstMakeShortInline@%
1362\end{cquote}
1363Is this an array of 5 pointers to integers or a pointer to an array of 5 integers?
1364If there is any doubt, it implies productivity and safety issues even for basic programs.
1365Another example of confusion results from the fact that a routine name and its parameters are embedded within the return type, mimicking the way the return value is used at the routine's call site.
1366For example, a routine returning a pointer to an array of integers is defined and used in the following way:
1367\begin{cfa}
1368int `(*`f`())[`5`]` {...};                              $\C{// definition}$
1369 ... `(*`f`())[`3`]` += 1;                              $\C{// usage}$
1370\end{cfa}
1371Essentially, the return type is wrapped around the routine name in successive layers (like an onion).
1372While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice.
1373
1374\CFA provides its own type, variable and routine declarations, using a different syntax.
1375The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type.
1376In the following example, \R{red} is the base type and \B{blue} is qualifiers.
1377The \CFA declarations move the qualifiers to the left of the base type, \ie move the blue to the left of the red, while the qualifiers have the same meaning but are ordered left to right to specify a variable's type.
1378\begin{cquote}
1379\lstDeleteShortInline@%
1380\lstset{moredelim=**[is][\color{blue}]{+}{+}}
1381\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1382\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1383\begin{cfa}
1384+[5] *+ `int` x1;
1385+* [5]+ `int` x2;
1386+[* [5] int]+ f`( int p )`;
1387\end{cfa}
1388&
1389\begin{cfa}
1390`int` +*+ x1 +[5]+;
1391`int` +(*+x2+)[5]+;
1392+int (*+f`( int p )`+)[5]+;
1393\end{cfa}
1394\end{tabular}
1395\lstMakeShortInline@%
1396\end{cquote}
1397The only exception is bit field specification, which always appear to the right of the base type.
1398% Specifically, the character @*@ is used to indicate a pointer, square brackets @[@\,@]@ are used to represent an array or function return value, and parentheses @()@ are used to indicate a routine parameter.
1399However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list.
1400For instance, variables @x@ and @y@ of type pointer to integer are defined in \CFA as follows:
1401\begin{cquote}
1402\lstDeleteShortInline@%
1403\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1404\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1405\begin{cfa}
1406`*` int x, y;
1407\end{cfa}
1408&
1409\begin{cfa}
1410int `*`x, `*`y;
1411\end{cfa}
1412\end{tabular}
1413\lstMakeShortInline@%
1414\end{cquote}
1415The downside of this semantics is the need to separate regular and pointer declarations:
1416\begin{cquote}
1417\lstDeleteShortInline@%
1418\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1419\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1420\begin{cfa}
1421`*` int x;
1422int y;
1423\end{cfa}
1424&
1425\begin{cfa}
1426int `*`x, y;
1427
1428\end{cfa}
1429\end{tabular}
1430\lstMakeShortInline@%
1431\end{cquote}
1432which is prescribing a safety benefit.
1433Other examples are:
1434\begin{cquote}
1435\lstDeleteShortInline@%
1436\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
1437\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
1438\begin{cfa}
1439[ 5 ] int z;
1440[ 5 ] * char w;
1441* [ 5 ] double v;
1442struct s {
1443        int f0:3;
1444        * int f1;
1445        [ 5 ] * int f2;
1446};
1447\end{cfa}
1448&
1449\begin{cfa}
1450int z[ 5 ];
1451char * w[ 5 ];
1452double (* v)[ 5 ];
1453struct s {
1454        int f0:3;
1455        int * f1;
1456        int * f2[ 5 ]
1457};
1458\end{cfa}
1459&
1460\begin{cfa}
1461// array of 5 integers
1462// array of 5 pointers to char
1463// pointer to array of 5 doubles
1464
1465// common bit field syntax
1466
1467
1468
1469\end{cfa}
1470\end{tabular}
1471\lstMakeShortInline@%
1472\end{cquote}
1473
1474All type qualifiers, \eg @const@, @volatile@, etc., are used in the normal way with the new declarations and also appear left to right, \eg:
1475\begin{cquote}
1476\lstDeleteShortInline@%
1477\begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}}
1478\multicolumn{1}{c@{\hspace{1em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{1em}}}{\textbf{C}} \\
1479\begin{cfa}
1480const * const int x;
1481const * [ 5 ] const int y;
1482\end{cfa}
1483&
1484\begin{cfa}
1485int const * const x;
1486const int (* const y)[ 5 ]
1487\end{cfa}
1488&
1489\begin{cfa}
1490// const pointer to const integer
1491// const pointer to array of 5 const integers
1492\end{cfa}
1493\end{tabular}
1494\lstMakeShortInline@%
1495\end{cquote}
1496All declaration qualifiers, \eg @extern@, @static@, etc., are used in the normal way with the new declarations but can only appear at the start of a \CFA routine declaration,\footnote{\label{StorageClassSpecifier}
1497The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.~\cite[\S~6.11.5(1)]{C11}} \eg:
1498\begin{cquote}
1499\lstDeleteShortInline@%
1500\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
1501\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
1502\begin{cfa}
1503extern [ 5 ] int x;
1504static * const int y;
1505\end{cfa}
1506&
1507\begin{cfa}
1508int extern x[ 5 ];
1509const int static * y;
1510\end{cfa}
1511&
1512\begin{cfa}
1513// externally visible array of 5 integers
1514// internally visible pointer to constant int
1515\end{cfa}
1516\end{tabular}
1517\lstMakeShortInline@%
1518\end{cquote}
1519
1520The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine @sizeof@:
1521\begin{cquote}
1522\lstDeleteShortInline@%
1523\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1524\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1525\begin{cfa}
1526y = (* int)x;
1527i = sizeof([ 5 ] * int);
1528\end{cfa}
1529&
1530\begin{cfa}
1531y = (int *)x;
1532i = sizeof(int * [ 5 ]);
1533\end{cfa}
1534\end{tabular}
1535\lstMakeShortInline@%
1536\end{cquote}
1537
1538Finally, new \CFA declarations may appear together with C declarations in the same program block, but cannot be mixed within a specific declaration.
1539Therefore, a programmer has the option of either continuing to use traditional C declarations or take advantage of the new style.
1540Clearly, both styles need to be supported for some time due to existing C-style header-files, particularly for UNIX-like systems.
1541
1542The syntax of the new routine prototype declaration follows directly from the new routine definition syntax;
1543as well, parameter names are optional, \eg:
1544\begin{cfa}
1545[ int x ] f ();                                                 $\C{// returning int with no parameters}$
1546[ * int ] g (int y);                                    $\C{// returning pointer to int with int parameter}$
1547[ ] h ( int, char );                                    $\C{// returning no result with int and char parameters}$
1548[ * int, int ] j ( int );                               $\C{// returning pointer to int and int, with int parameter}$
1549\end{cfa}
1550This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa).
1551Like 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, \eg:
1552\begin{cquote}
1553\lstDeleteShortInline@%
1554\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1555\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1556\begin{cfa}
1557[double] foo(), foo( int ), foo( double ) {...}
1558\end{cfa}
1559&
1560\begin{cfa}
1561double foo1(), foo2( int ), foo3( double );
1562\end{cfa}
1563\end{tabular}
1564\lstMakeShortInline@%
1565\end{cquote}
1566\CFA allows the last routine in the list to define its body.
1567
1568Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg:
1569\begin{cfa}
1570extern [ int ] f ( int );
1571static [ int ] g ( int );
1572\end{cfa}
1573
1574The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg:
1575\begin{cfa}
1576* [ int x ] () fp;                                              $\C{// pointer to routine returning int with no parameters}$
1577* [ * int ] (int y) gp;                                 $\C{// pointer to routine returning pointer to int with int parameter}$
1578* [ ] (int,char) hp;                                    $\C{// pointer to routine returning no result with int and char parameters}$
1579* [ * int,int ] ( int ) jp;                             $\C{// pointer to routine returning pointer to int and int, with int parameter}$
1580\end{cfa}
1581While parameter names are optional, \emph{a routine name cannot be specified};
1582for example, the following is incorrect:
1583\begin{cfa}
1584* [ int x ] f () fp;                                    $\C{// routine name "f" is not allowed}$
1585\end{cfa}
1586
1587
1588\subsection{References}
1589
1590All variables in C have an \newterm{address}, a \newterm{value}, and a \newterm{type};
1591at the position in the program's memory denoted by the address, there exists a sequence of bits (the value), with the length and semantic meaning of this bit sequence defined by the type.
1592The C type-system does not always track the relationship between a value and its address;
1593a value that does not have a corresponding address is called a \newterm{rvalue} (for ``right-hand value''), while a value that does have an address is called a \newterm{lvalue} (for ``left-hand value'').
1594For example, in @int x; x = 42;@ the variable expression @x@ on the left-hand-side of the assignment is a lvalue, while the constant expression @42@ on the right-hand-side of the assignment is a rvalue.
1595Despite the nomenclature of ``left-hand'' and ``right-hand'', an expression's classification as lvalue or rvalue is entirely dependent on whether it has an address or not; in imperative programming, the address of a value is used for both reading and writing (mutating) a value, and as such lvalues can be converted to rvalues and read from, but rvalues cannot be mutated because they lack a location to store the updated value.
1596
1597Within a lexical scope, lvalue expressions have an \newterm{address interpretation} for writing a value or a \newterm{value interpretation} to read a value.
1598For example, in @x = y@, @x@ has an address interpretation, while @y@ has a value interpretation.
1599Though this duality of interpretation is useful, C lacks a direct mechanism to pass lvalues between contexts, instead relying on \newterm{pointer types} to serve a similar purpose.
1600In C, for any type @T@ there is a pointer type @T *@, the value of which is the address of a value of type @T@.
1601A pointer rvalue can be explicitly \newterm{dereferenced} to the pointed-to lvalue with the dereference operator @*?@, while the rvalue representing the address of a lvalue can be obtained with the address-of operator @&?@.
1602
1603\begin{cfa}
1604int x = 1, y = 2, * p1, * p2, ** p3;
1605p1 = &x;                                                                $\C{// p1 points to x}$
1606p2 = &y;                                                                $\C{// p2 points to y}$
1607p3 = &p1;                                                               $\C{// p3 points to p1}$
1608*p2 = ((*p1 + *p2) * (**p3 - *p1)) / (**p3 - 15);
1609\end{cfa}
1610
1611Unfortunately, the dereference and address-of operators introduce a great deal of syntactic noise when dealing with pointed-to values rather than pointers, as well as the potential for subtle bugs.
1612For both brevity and clarity, it would be desirable to have the compiler figure out how to elide the dereference operators in a complex expression such as the assignment to @*p2@ above.
1613However, since C defines a number of forms of \newterm{pointer arithmetic}, two similar expressions involving pointers to arithmetic types (\eg @*p1 + x@ and @p1 + x@) may each have well-defined but distinct semantics, introducing the possibility that a user programmer may write one when they mean the other, and precluding any simple algorithm for elision of dereference operators.
1614To solve these problems, \CFA introduces reference types @T&@; a @T&@ has exactly the same value as a @T*@, but where the @T*@ takes the address interpretation by default, a @T&@ takes the value interpretation by default, as below:
1615
1616\begin{cfa}
1617int x = 1, y = 2, & r1, & r2, && r3;
1618&r1 = &x;  $\C{// r1 points to x}$
1619&r2 = &y;  $\C{// r2 points to y}$
1620&&r3 = &&r1;  $\C{// r3 points to r2}$
1621r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15);  $\C{// implicit dereferencing}$
1622\end{cfa}
1623
1624Except for auto-dereferencing by the compiler, this reference example is exactly the same as the previous pointer example.
1625Hence, a reference behaves like a variable name -- an lvalue expression which is interpreted as a value, but also has the type system track the address of that value.
1626One way to conceptualize a reference is via a rewrite rule, where the compiler inserts a dereference operator before the reference variable for each reference qualifier in the reference variable declaration, so the previous example implicitly acts like:
1627
1628\begin{cfa}
1629`*`r2 = ((`*`r1 + `*`r2) * (`**`r3 - `*`r1)) / (`**`r3 - 15);
1630\end{cfa}
1631
1632References in \CFA are similar to those in \CC, but with a couple important improvements, both of which can be seen in the example above.
1633Firstly, \CFA does not forbid references to references, unlike \CC.
1634This provides a much more orthogonal design for library implementors, obviating the need for workarounds such as @std::reference_wrapper@.
1635
1636Secondly, unlike the references in \CC which always point to a fixed address, \CFA references are rebindable.
1637This allows \CFA references to be default-initialized (\eg to a null pointer), and also to point to different addresses throughout their lifetime.
1638This rebinding is accomplished without adding any new syntax to \CFA, but simply by extending the existing semantics of the address-of operator in C.
1639
1640In C, the address of a lvalue is always a rvalue, as in general that address is not stored anywhere in memory, and does not itself have an address.
1641In \CFA, the address of a @T&@ is a lvalue @T*@, as the address of the underlying @T@ is stored in the reference, and can thus be mutated there.
1642The result of this rule is that any reference can be rebound using the existing pointer assignment semantics by assigning a compatible pointer into the address of the reference, \eg @&r1 = &x;@ above.
1643This rebinding can occur to an arbitrary depth of reference nesting; loosely speaking, nested address-of operators will produce an lvalue nested pointer up to as deep as the reference they're applied to.
1644These explicit address-of operators can be thought of as ``cancelling out'' the implicit dereference operators, \eg @(&`*`)r1 = &x@ or @(&(&`*`)`*`)r3 = &(&`*`)r1@ or even @(&`*`)r2 = (&`*`)`*`r3@ for @&r2 = &r3@.
1645More precisely:
1646\begin{itemize}
1647        \item
1648        if @R@ is an rvalue of type {@T &@$_1 \cdots$@ &@$_r$} where $r \ge 1$ references (@&@ symbols) than @&R@ has type {@T `*`&@$_{\color{red}2} \cdots$@ &@$_{\color{red}r}$}, \\ \ie @T@ pointer with $r-1$ references (@&@ symbols).
1649       
1650        \item
1651        if @L@ is an lvalue of type {@T &@$_1 \cdots$@ &@$_l$} where $l \ge 0$ references (@&@ symbols) then @&L@ has type {@T `*`&@$_{\color{red}1} \cdots$@ &@$_{\color{red}l}$}, \\ \ie @T@ pointer with $l$ references (@&@ symbols).
1652\end{itemize}
1653Since pointers and references share the same internal representation, code using either is equally performant; in fact the \CFA compiler converts references to pointers internally, and the choice between them in user code can be made based solely on convenience.
1654
1655By analogy to pointers, \CFA references also allow cv-qualifiers such as @const@:
1656
1657\begin{cfa}
1658const int cx = 5;               $\C{// cannot change cx}$
1659const int & cr = cx;    $\C{// cannot change cr's referred value}$
1660&cr = &cx;                              $\C{// rebinding cr allowed}$
1661cr = 7;                                 $\C{// ERROR, cannot change cr}$
1662int & const rc = x;             $\C{// must be initialized, like in \CC}$
1663&rc = &x;                               $\C{// ERROR, cannot rebind rc}$
1664rc = 7;                                 $\C{// x now equal to 7}$
1665\end{cfa}
1666
1667Given that a reference is meant to represent a lvalue, \CFA provides some syntactic shortcuts when initializing references.
1668There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, and return/temporary binding.
1669In each of these contexts, the address-of operator on the target lvalue may (in fact, must) be elided.
1670The syntactic motivation for this is clearest when considering overloaded operator-assignment, \eg @int ?+=?(int &, int)@; given @int x, y@, the expected call syntax is @x += y@, not @&x += y@.
1671
1672More generally, this initialization of references from lvalues rather than pointers is an instance of a ``lvalue-to-reference'' conversion rather than an elision of the address-of operator; this conversion can actually be used in any context in \CFA an implicit conversion would be allowed.
1673Similarly, use of a the value pointed to by a reference in an rvalue context can be thought of as a ``reference-to-rvalue'' conversion, and \CFA also includes a qualifier-adding ``reference-to-reference'' conversion, analogous to the @T *@ to @const T *@ conversion in standard C.
1674The final reference conversion included in \CFA is ``rvalue-to-reference'' conversion, implemented by means of an implicit temporary.
1675When an rvalue is used to initialize a reference, it is instead used to initialize a hidden temporary value with the same lexical scope as the reference, and the reference is initialized to the address of this temporary.
1676This allows complex values to be succinctly and efficiently passed to functions, without the syntactic overhead of explicit definition of a temporary variable or the runtime cost of pass-by-value.
1677\CC allows a similar binding, but only for @const@ references; the more general semantics of \CFA are an attempt to avoid the \newterm{const hell} problem, in which addition of a @const@ qualifier to one reference requires a cascading chain of added qualifiers.
1678
1679
1680\subsection{Constructors and Destructors}
1681
1682One of the strengths of C is the control over memory management it gives programmers, allowing resource release to be more consistent and precisely timed than is possible with garbage-collected memory management.
1683However, this manual approach to memory management is often verbose, and it is useful to manage resources other than memory (\eg file handles) using the same mechanism as memory.
1684\CC is well-known for an approach to manual memory management that addresses both these issues, Resource Aquisition Is Initialization (RAII), implemented by means of special \newterm{constructor} and \newterm{destructor} functions; we have implemented a similar feature in \CFA.
1685While RAII is a common feature of object-oriented programming languages, its inclusion in \CFA does not violate the design principle that \CFA retain the same procedural paradigm as C.
1686In particular, \CFA does not implement class-based encapsulation: neither the constructor nor any other function has privileged access to the implementation details of a type, except through the translation-unit-scope method of opaque structs provided by C.
1687
1688In \CFA, a constructor is a function named @?{}@, while a destructor is a function named @^?{}@; like other \CFA operators, these names represent the syntax used to call the constructor or destructor, \eg @x{ ... };@ or @^x{};@.
1689Every constructor and destructor must have a return type of @void@, and its first parameter must have a reference type whose base type is the type of the object the function constructs or destructs.
1690This first parameter is informally called the @this@ parameter, as in many object-oriented languages, though a programmer may give it an arbitrary name.
1691Destructors must have exactly one parameter, while constructors allow passing of zero or more additional arguments along with the @this@ parameter.
1692
1693\begin{cfa}
1694struct Array {
1695        int * data;
1696        int len;
1697};
1698
1699void ?{}( Array& arr ) {
1700        arr.len = 10;
1701        arr.data = calloc( arr.len, sizeof(int) );
1702}
1703
1704void ^?{}( Array& arr ) {
1705        free( arr.data );
1706}
1707
1708{
1709        Array x;
1710        `?{}(x);`       $\C{// implicitly compiler-generated}$
1711        // ... use x
1712        `^?{}(x);`      $\C{// implicitly compiler-generated}$
1713}
1714\end{cfa}
1715
1716In the example above, a \newterm{default constructor} (\ie one with no parameters besides the @this@ parameter) and destructor are defined for the @Array@ struct, a dynamic array of @int@.
1717@Array@ is an example of a \newterm{managed type} in \CFA, a type with a non-trivial constructor or destructor, or with a field of a managed type.
1718As in the example, all instances of managed types are implicitly constructed upon allocation, and destructed upon deallocation; this ensures proper initialization and cleanup of resources contained in managed types, in this case the @data@ array on the heap.
1719The exact details of the placement of these implicit constructor and destructor calls are omitted here for brevity, the interested reader should consult \cite{Schluntz17}.
1720
1721Constructor calls are intended to seamlessly integrate with existing C initialization syntax, providing a simple and familiar syntax to veteran C programmers and allowing constructor calls to be inserted into legacy C code with minimal code changes.
1722As such, \CFA also provides syntax for \newterm{copy initialization} and \newterm{initialization parameters}:
1723
1724\begin{cfa}
1725void ?{}( Array& arr, Array other );
1726
1727void ?{}( Array& arr, int size, int fill );
1728
1729Array y = { 20, 0xDEADBEEF }, z = y;
1730\end{cfa}
1731
1732Copy constructors have exactly two parameters, the second of which has the same type as the base type of the @this@ parameter; appropriate care is taken in the implementation to avoid recursive calls to the copy constructor when initializing this second parameter.
1733Other constructor calls look just like C initializers, except rather than using field-by-field initialization (as in C), an initialization which matches a defined constructor will call the constructor instead.
1734
1735In addition to initialization syntax, \CFA provides two ways to explicitly call constructors and destructors.
1736Explicit calls to constructors double as a placement syntax, useful for construction of member fields in user-defined constructors and reuse of large storage allocations.
1737While the existing function-call syntax works for explicit calls to constructors and destructors, \CFA also provides a more concise \newterm{operator syntax} for both:
1738
1739\begin{cfa}
1740Array a, b;
1741a{};                            $\C{// default construct}$
1742b{ a };                         $\C{// copy construct}$
1743^a{};                           $\C{// destruct}$
1744a{ 5, 0xFFFFFFFF };     $\C{// explicit constructor call}$
1745\end{cfa}
1746
1747To provide a uniform type interface for @otype@ polymorphism, the \CFA compiler automatically generates a default constructor, copy constructor, assignment operator, and destructor for all types.
1748These default functions can be overridden by user-generated versions of them.
1749For compatibility with the standard behaviour of C, the default constructor and destructor for all basic, pointer, and reference types do nothing, while the copy constructor and assignment operator are bitwise copies; if default zero-initialization is desired, the default constructors can be overridden.
1750For user-generated types, the four functions are also automatically generated.
1751@enum@ types are handled the same as their underlying integral type, and unions are also bitwise copied and no-op initialized and destructed.
1752For compatibility with C, a copy constructor from the first union member type is also defined.
1753For @struct@ types, each of the four functions are implicitly defined to call their corresponding functions on each member of the struct.
1754To better simulate the behaviour of C initializers, a set of \newterm{field constructors} is also generated for structures.
1755A constructor is generated for each non-empty prefix of a structure's member-list which copy-constructs the members passed as parameters and default-constructs the remaining members.
1756To allow users to limit the set of constructors available for a type, when a user declares any constructor or destructor, the corresponding generated function and all field constructors for that type are hidden from expression resolution; similarly, the generated default constructor is hidden upon declaration of any constructor.
1757These semantics closely mirror the rule for implicit declaration of constructors in \CC\cite[p.~186]{ANSI98:C++}.
1758
1759In rare situations user programmers may not wish to have constructors and destructors called; in these cases, \CFA provides an ``escape hatch'' to not call them.
1760If a variable is initialized using the syntax \lstinline|S x @= {}| it will be an \newterm{unmanaged object}, and will not have constructors or destructors called.
1761Any C initializer can be the right-hand side of an \lstinline|@=| initializer, \eg  \lstinline|Array a @= { 0, 0x0 }|, with the usual C initialization semantics.
1762In addition to the expressive power, \lstinline|@=| provides a simple path for migrating legacy C code to \CFA, by providing a mechanism to incrementally convert initializers; the \CFA design team decided to introduce a new syntax for this escape hatch because we believe that our RAII implementation will handle the vast majority of code in a desirable way, and we wished to maintain familiar syntax for this common case.
1763
1764\subsection{Default Parameters}
1765
1766
1767\section{Literals}
1768
1769C already includes limited polymorphism for literals -- @0@ can be either an integer or a pointer literal, depending on context, while the syntactic forms of literals of the various integer and float types are very similar, differing from each other only in suffix.
1770In keeping with the general \CFA approach of adding features while respecting ``the C way'' of doing things, we have extended both C's polymorphic zero and typed literal syntax to interoperate with user-defined types, while maintaining a backwards-compatible semantics.
1771
1772\subsection{0/1}
1773
1774In C, @0@ has the special property that it is the only ``false'' value; by the standard, any value which compares equal to @0@ is false, while any value that compares unequal to @0@ is true.
1775As such, an expression @x@ in any boolean context (such as the condition of an @if@ or @while@ statement, or the arguments to an @&&@, @||@, or ternary operator) can be rewritten as @x != 0@ without changing its semantics.
1776The operator overloading feature of \CFA provides a natural means to implement this truth value comparison for arbitrary types, but the C type system is not precise enough to distinguish an equality comparison with @0@ from an equality comparison with an arbitrary integer or pointer.
1777To provide this precision, \CFA introduces a new type @zero_t@ as type type of literal @0@ (somewhat analagous to @nullptr_t@ and @nullptr@ in \CCeleven); @zero_t@ can only take the value @0@, but has implicit conversions to the integer and pointer types so that standard C code involving @0@ continues to work properly.
1778With this addition, the \CFA compiler rewrites @if (x)@ and similar expressions to @if ((x) != 0)@ or the appropriate analogue, and any type @T@ can be made ``truthy'' by defining an operator overload @int ?!=?(T, zero_t)@.
1779\CC makes types truthy by adding a conversion to @bool@; prior to the addition of explicit cast operators in \CCeleven this approach had the pitfall of making truthy types transitively convertable to any numeric type; our design for \CFA avoids this issue.
1780
1781\CFA also includes a special type for @1@, @one_t@; like @zero_t@, @one_t@ has built-in implicit conversions to the various integral types so that @1@ maintains its expected semantics in legacy code.
1782The addition of @one_t@ allows generic algorithms to handle the unit value uniformly for types where that is meaningful.
1783\TODO{Make this sentence true} In particular, polymorphic functions in the \CFA prelude define @++x@ and @x++@ in terms of @x += 1@, allowing users to idiomatically define all forms of increment for a type @T@ by defining the single function @T& ?+=(T&, one_t)@; analogous overloads for the decrement operators are present as well.
1784
1785\subsection{Units}
1786
1787Alternative call syntax (literal argument before routine name) to convert basic literals into user literals.
1788
1789{\lstset{language=CFA,deletedelim=**[is][]{`}{`},moredelim=**[is][\color{red}]{@}{@}}
1790\begin{cfa}
1791struct Weight { double stones; };
1792
1793void ?{}( Weight & w ) { w.stones = 0; } $\C{// operations}$
1794void ?{}( Weight & w, double w ) { w.stones = w; }
1795Weight ?+?( Weight l, Weight r ) { return (Weight){ l.stones + r.stones }; }
1796
1797Weight @?`st@( double w ) { return (Weight){ w }; } $\C{// backquote for units}$
1798Weight @?`lb@( double w ) { return (Weight){ w / 14.0 }; }
1799Weight @?`kg@( double w ) { return (Weight) { w * 0.1575}; }
1800
1801int main() {
1802        Weight w, hw = { 14 };                  $\C{// 14 stone}$
1803        w = 11@`st@ + 1@`lb@;
1804        w = 70.3@`kg@;
1805        w = 155@`lb@;
1806        w = 0x_9b_u@`lb@;                               $\C{// hexadecimal unsigned weight (155)}$
1807        w = 0_233@`lb@;                                 $\C{// octal weight (155)}$
1808        w = 5@`st@ + 8@`kg@ + 25@`lb@ + hw;
1809}
1810\end{cfa}
1811}%
1812
1813
1814\section{Libraries}
1815
1816\subsection{I/O}
1817\label{s:IOLibrary}
1818
1819The goal of \CFA I/O is to simplify the common cases, while fully supporting polymorphism and user defined types in a consistent way.
1820The approach combines ideas from \CC and Python.
1821The \CFA header file for the I/O library is @fstream@.
1822
1823The common case is printing out a sequence of variables separated by whitespace.
1824\begin{cquote}
1825\lstDeleteShortInline@%
1826\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1827\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{\CC}}      \\
1828\begin{cfa}
1829int x = 1, y = 2, z = 3;
1830sout | x `|` y `|` z | endl;
1831\end{cfa}
1832&
1833\begin{cfa}
1834
1835cout << x `<< " "` << y `<< " "` << z << endl;
1836\end{cfa}
1837\\
1838\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
18391` `2` `3
1840\end{cfa}
1841&
1842\begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt]
18431 2 3
1844\end{cfa}
1845\end{tabular}
1846\lstMakeShortInline@%
1847\end{cquote}
1848The \CFA form has half the characters of the \CC form, and is similar to Python I/O with respect to implicit separators.
1849Similar simplification occurs for tuple I/O, which prints all tuple values separated by ``\lstinline[showspaces=true]@, @''.
1850\begin{cfa}
1851[int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ];
1852sout | t1 | t2 | endl;                                  $\C{// print tuples}$
1853\end{cfa}
1854\begin{cfa}[showspaces=true,aboveskip=0pt]
18551`, `2`, `3 4`, `5`, `6
1856\end{cfa}
1857Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority overloadable operator, other than assignment.
1858Therefore, fewer output expressions require parenthesis.
1859\begin{cquote}
1860\lstDeleteShortInline@%
1861\begin{tabular}{@{}ll@{}}
1862\textbf{\CFA:}
1863&
1864\begin{cfa}
1865sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;
1866\end{cfa}
1867\\
1868\textbf{\CC:}
1869&
1870\begin{cfa}
1871cout << x * 3 << y + 1 << `(`z << 2`)` << `(`x == y`)` << (x | y) << (x || y) << (x > z ? 1 : 2) << endl;
1872\end{cfa}
1873\\
1874\textbf{output:}
1875&
1876\begin{cfa}[showspaces=true,aboveskip=0pt]
18773 3 12 0 3 1 2
1878\end{cfa}
1879\end{tabular}
1880\lstMakeShortInline@%
1881\end{cquote}
1882There is a weak similarity between the \CFA logical-or operator and the Shell pipe-operator for moving data, where data flows in the correct direction for input but the opposite direction for output.
1883
1884The implicit separator character (space/blank) is a separator not a terminator.
1885The rules for implicitly adding the separator are:
1886\begin{list}{\footnotesize$\bullet$}{\itemsep=2pt\parsep=0pt}
1887\item
1888A separator does not appear at the start or end of a line.
1889\item
1890A separator does not appear before or after a character literal or variable.
1891\item
1892A separator does not appear before or after a null (empty) C string, which is a local mechanism to disable insertion of the separator character.
1893\item
1894A separator does not appear before a C string starting with the characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$@
1895\item
1896A seperator does not appear after a C string ending with the characters: \lstinline[basicstyle=\tt]@,.;!?)]}%@
1897\item
1898{\lstset{language=CFA,deletedelim=**[is][]{`}{`}}
1899A seperator does not appear before or after a C string begining/ending with the quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]@`'": \t\v\f\r\n@
1900}%
1901\item
1902There are routines to set and get the separator string, and manipulators to toggle separation on and off in the middle of output.
1903\end{list}
1904
1905
1906\section{Evaluation}
1907\label{sec:eval}
1908
1909Though \CFA provides significant added functionality over C, these features have a low runtime penalty.
1910In fact, \CFA's features for generic programming can enable faster runtime execution than idiomatic @void *@-based C code.
1911This claim is demonstrated through a set of generic-code-based micro-benchmarks in C, \CFA, and \CC (see stack implementations in Appendix~\ref{sec:BenchmarkStackImplementation}).
1912Since all these languages share a subset essentially comprising standard C, maximal-performance benchmarks would show little runtime variance, other than in length and clarity of source code.
1913A more illustrative benchmark measures the costs of idiomatic usage of each language's features.
1914Figure~\ref{fig:BenchmarkTest} shows the \CFA benchmark tests for a generic stack based on a singly linked-list, a generic pair-data-structure, and a variadic @print@ routine similar to that in Section~\ref{sec:variadic-tuples}.
1915The benchmark test is similar for C and \CC.
1916The experiment uses element types @int@ and @pair(_Bool, char)@, and pushes $N=40M$ elements on a generic stack, copies the stack, clears one of the stacks, finds the maximum value in the other stack, and prints $N/2$ (to reduce graph height) constants.
1917
1918\begin{figure}
1919\begin{lstlisting}[xleftmargin=3\parindentlnth,aboveskip=0pt,belowskip=0pt]
1920int main( int argc, char * argv[] ) {
1921        FILE * out = fopen( "cfa-out.txt", "w" );
1922        int maxi = 0, vali = 42;
1923        stack(int) si, ti;
1924
1925        REPEAT_TIMED( "push_int", N, push( &si, vali ); )
1926        TIMED( "copy_int", ti = si; )
1927        TIMED( "clear_int", clear( &si ); )
1928        REPEAT_TIMED( "pop_int", N,
1929                int xi = pop( &ti ); if ( xi > maxi ) { maxi = xi; } )
1930        REPEAT_TIMED( "print_int", N/2, print( out, vali, ":", vali, "\n" ); )
1931
1932        pair(_Bool, char) maxp = { (_Bool)0, '\0' }, valp = { (_Bool)1, 'a' };
1933        stack(pair(_Bool, char)) sp, tp;
1934
1935        REPEAT_TIMED( "push_pair", N, push( &sp, valp ); )
1936        TIMED( "copy_pair", tp = sp; )
1937        TIMED( "clear_pair", clear( &sp ); )
1938        REPEAT_TIMED( "pop_pair", N,
1939                pair(_Bool, char) xp = pop( &tp ); if ( xp > maxp ) { maxp = xp; } )
1940        REPEAT_TIMED( "print_pair", N/2, print( out, valp, ":", valp, "\n" ); )
1941        fclose(out);
1942}
1943\end{lstlisting}
1944\caption{\protect\CFA Benchmark Test}
1945\label{fig:BenchmarkTest}
1946\end{figure}
1947
1948The structure of each benchmark implemented is: C with @void *@-based polymorphism, \CFA with the presented features, \CC with templates, and \CC using only class inheritance for polymorphism, called \CCV.
1949The \CCV variant illustrates an alternative object-oriented idiom where all objects inherit from a base @object@ class, mimicking a Java-like interface;
1950hence runtime checks are necessary to safely down-cast objects.
1951The most notable difference among the implementations is in memory layout of generic types: \CFA and \CC inline the stack and pair elements into corresponding list and pair nodes, while C and \CCV lack such a capability and instead must store generic objects via pointers to separately-allocated objects.
1952For the print benchmark, idiomatic printing is used: the C and \CFA variants used @stdio.h@, while the \CC and \CCV variants used @iostream@; preliminary tests show this distinction has negligible runtime impact.
1953Note, the C benchmark uses unchecked casts as there is no runtime mechanism to perform such checks, while \CFA and \CC provide type-safety statically.
1954
1955Figure~\ref{fig:eval} and Table~\ref{tab:eval} show the results of running the benchmark in Figure~\ref{fig:BenchmarkTest} and its C, \CC, and \CCV equivalents.
1956The graph plots the median of 5 consecutive runs of each program, with an initial warm-up run omitted.
1957All code is compiled at \texttt{-O2} by GCC or G++ 6.2.0, with all \CC code compiled as \CCfourteen.
1958The benchmarks are run on an Ubuntu 16.04 workstation with 16 GB of RAM and a 6-core AMD FX-6300 CPU with 3.5 GHz maximum clock frequency.
1959
1960\begin{figure}
1961\centering
1962\input{timing}
1963\caption{Benchmark Timing Results (smaller is better)}
1964\label{fig:eval}
1965\end{figure}
1966
1967\begin{table}
1968\caption{Properties of benchmark code}
1969\label{tab:eval}
1970\newcommand{\CT}[1]{\multicolumn{1}{c}{#1}}
1971\begin{tabular}{rrrrr}
1972                                                                        & \CT{C}        & \CT{\CFA}     & \CT{\CC}      & \CT{\CCV}             \\ \hline
1973maximum memory usage (MB)                       & 10001         & 2502          & 2503          & 11253                 \\
1974source code size (lines)                        & 247           & 222           & 165           & 339                   \\
1975redundant type annotations (lines)      & 39            & 2                     & 2                     & 15                    \\
1976binary size (KB)                                        & 14            & 229           & 18            & 38                    \\
1977\end{tabular}
1978\end{table}
1979
1980The C and \CCV variants are generally the slowest with the largest memory footprint, because of their less-efficient memory layout and the pointer-indirection necessary to implement generic types;
1981this inefficiency is exacerbated by the second level of generic types in the pair-based benchmarks.
1982By contrast, the \CFA and \CC variants run in roughly equivalent time for both the integer and pair of @_Bool@ and @char@ because the storage layout is equivalent, with the inlined libraries (\ie no separate compilation) and greater maturity of the \CC compiler contributing to its lead.
1983\CCV is slower than C largely due to the cost of runtime type-checking of down-casts (implemented with @dynamic_cast@);
1984There are two outliers in the graph for \CFA: all prints and pop of @pair@.
1985Both of these cases result from the complexity of the C-generated polymorphic code, so that the GCC compiler is unable to optimize some dead code and condense nested calls.
1986A compiler designed for \CFA could easily perform these optimizations.
1987Finally, the binary size for \CFA is larger because of static linking with the \CFA libraries.
1988
1989\CFA is also competitive in terms of source code size, measured as a proxy for programmer effort. The line counts in Table~\ref{tab:eval} include implementations of @pair@ and @stack@ types for all four languages for purposes of direct comparison, though it should be noted that \CFA and \CC have pre-written data structures in their standard libraries that programmers would generally use instead. Use of these standard library types has minimal impact on the performance benchmarks, but shrinks the \CFA and \CC benchmarks to 73 and 54 lines, respectively.
1990On the other hand, C does not have a generic collections-library in its standard distribution, resulting in frequent reimplementation of such collection types by C programmers.
1991\CCV does not use the \CC standard template library by construction, and in fact includes the definition of @object@ and wrapper classes for @bool@, @char@, @int@, and @const char *@ in its line count, which inflates this count somewhat, as an actual object-oriented language would include these in the standard library;
1992with their omission, the \CCV line count is similar to C.
1993We justify the given line count by noting that many object-oriented languages do not allow implementing new interfaces on library types without subclassing or wrapper types, which may be similarly verbose.
1994
1995Raw line-count, however, is a fairly rough measure of code complexity;
1996another important factor is how much type information the programmer must manually specify, especially where that information is not checked by the compiler.
1997Such unchecked type information produces a heavier documentation burden and increased potential for runtime bugs, and is much less common in \CFA than C, with its manually specified function pointers arguments and format codes, or \CCV, with its extensive use of un-type-checked downcasts (\eg @object@ to @integer@ when popping a stack, or @object@ to @printable@ when printing the elements of a @pair@).
1998To quantify this, the ``redundant type annotations'' line in Table~\ref{tab:eval} counts the number of lines on which the type of a known variable is re-specified, either as a format specifier, explicit downcast, type-specific function, or by name in a @sizeof@, struct literal, or @new@ expression.
1999The \CC benchmark uses two redundant type annotations to create a new stack nodes, while the C and \CCV benchmarks have several such annotations spread throughout their code.
2000The two instances in which the \CFA benchmark still uses redundant type specifiers are to cast the result of a polymorphic @malloc@ call (the @sizeof@ argument is inferred by the compiler).
2001These uses are similar to the @new@ expressions in \CC, though the \CFA compiler's type resolver should shortly render even these type casts superfluous.
2002
2003
2004\section{Related Work}
2005
2006
2007\subsection{Polymorphism}
2008
2009\CC is the most similar language to \CFA;
2010both are extensions to C with source and runtime backwards compatibility.
2011The fundamental difference is in their engineering approach to C compatibility and programmer expectation.
2012While \CC provides good backwards compatibility with C, it has a steep learning curve for many of its extensions.
2013For example, polymorphism is provided via three disjoint mechanisms: overloading, inheritance, and templates.
2014The overloading is restricted because resolution does not use the return type, inheritance requires learning object-oriented programming and coping with a restricted nominal-inheritance hierarchy, templates cannot be separately compiled resulting in compilation/code bloat and poor error messages, and determining how these mechanisms interact and which to use is confusing.
2015In contrast, \CFA has a single facility for polymorphic code supporting type-safe separate-compilation of polymorphic functions and generic (opaque) types, which uniformly leverage the C procedural paradigm.
2016The key mechanism to support separate compilation is \CFA's \emph{explicit} use of assumed properties for a type.
2017Until \CC concepts~\cite{C++Concepts} are standardized (anticipated for \CCtwenty), \CC provides no way to specify the requirements of a generic function in code beyond compilation errors during template expansion;
2018furthermore, \CC concepts are restricted to template polymorphism.
2019
2020Cyclone~\cite{Grossman06} also provides capabilities for polymorphic functions and existential types, similar to \CFA's @forall@ functions and generic types.
2021Cyclone existential types can include function pointers in a construct similar to a virtual function-table, but these pointers must be explicitly initialized at some point in the code, a tedious and potentially error-prone process.
2022Furthermore, Cyclone's polymorphic functions and types are restricted to abstraction over types with the same layout and calling convention as @void *@, \ie only pointer types and @int@.
2023In \CFA terms, all Cyclone polymorphism must be dtype-static.
2024While the Cyclone design provides the efficiency benefits discussed in Section~\ref{sec:generic-apps} for dtype-static polymorphism, it is more restrictive than \CFA's general model.
2025Smith and Volpano~\cite{Smith98} present Polymorphic C, an ML dialect with polymorphic functions, C-like syntax, and pointer types; it lacks many of C's features, however, most notably structure types, and so is not a practical C replacement.
2026
2027Objective-C~\cite{obj-c-book} is an industrially successful extension to C.
2028However, Objective-C is a radical departure from C, using an object-oriented model with message-passing.
2029Objective-C did not support type-checked generics until recently \cite{xcode7}, historically using less-efficient runtime checking of object types.
2030The GObject~\cite{GObject} framework also adds object-oriented programming with runtime type-checking and reference-counting garbage-collection to C;
2031these features are more intrusive additions than those provided by \CFA, in addition to the runtime overhead of reference-counting.
2032Vala~\cite{Vala} compiles to GObject-based C, adding the burden of learning a separate language syntax to the aforementioned demerits of GObject as a modernization path for existing C code-bases.
2033Java~\cite{Java8} included generic types in Java~5, which are type-checked at compilation and type-erased at runtime, similar to \CFA's.
2034However, in Java, each object carries its own table of method pointers, while \CFA passes the method pointers separately to maintain a C-compatible layout.
2035Java is also a garbage-collected, object-oriented language, with the associated resource usage and C-interoperability burdens.
2036
2037D~\cite{D}, Go, and Rust~\cite{Rust} are modern, compiled languages with abstraction features similar to \CFA traits, \emph{interfaces} in D and Go and \emph{traits} in Rust.
2038However, each language represents a significant departure from C in terms of language model, and none has the same level of compatibility with C as \CFA.
2039D and Go are garbage-collected languages, imposing the associated runtime overhead.
2040The necessity of accounting for data transfer between managed runtimes and the unmanaged C runtime complicates foreign-function interfaces to C.
2041Furthermore, while generic types and functions are available in Go, they are limited to a small fixed set provided by the compiler, with no language facility to define more.
2042D restricts garbage collection to its own heap by default, while Rust is not garbage-collected, and thus has a lighter-weight runtime more interoperable with C.
2043Rust also possesses much more powerful abstraction capabilities for writing generic code than Go.
2044On the other hand, Rust's borrow-checker provides strong safety guarantees but is complex and difficult to learn and imposes a distinctly idiomatic programming style.
2045\CFA, with its more modest safety features, allows direct ports of C code while maintaining the idiomatic style of the original source.
2046
2047
2048\subsection{Tuples/Variadics}
2049
2050Many programming languages have some form of tuple construct and/or variadic functions, \eg SETL, C, KW-C, \CC, D, Go, Java, ML, and Scala.
2051SETL~\cite{SETL} is a high-level mathematical programming language, with tuples being one of the primary data types.
2052Tuples in SETL allow subscripting, dynamic expansion, and multiple assignment.
2053C provides variadic functions through @va_list@ objects, but the programmer is responsible for managing the number of arguments and their types, so the mechanism is type unsafe.
2054KW-C~\cite{Buhr94a}, a predecessor of \CFA, introduced tuples to C as an extension of the C syntax, taking much of its inspiration from SETL.
2055The main contributions of that work were adding MRVF, tuple mass and multiple assignment, and record-field access.
2056\CCeleven introduced @std::tuple@ as a library variadic template structure.
2057Tuples are a generalization of @std::pair@, in that they allow for arbitrary length, fixed-size aggregation of heterogeneous values.
2058Operations include @std::get<N>@ to extract values, @std::tie@ to create a tuple of references used for assignment, and lexicographic comparisons.
2059\CCseventeen proposes \emph{structured bindings}~\cite{Sutter15} to eliminate pre-declaring variables and use of @std::tie@ for binding the results.
2060This extension requires the use of @auto@ to infer the types of the new variables, so complicated expressions with a non-obvious type must be documented with some other mechanism.
2061Furthermore, structured bindings are not a full replacement for @std::tie@, as it always declares new variables.
2062Like \CC, D provides tuples through a library variadic-template structure.
2063Go does not have tuples but supports MRVF.
2064Java's variadic functions appear similar to C's but are type-safe using homogeneous arrays, which are less useful than \CFA's heterogeneously-typed variadic functions.
2065Tuples are a fundamental abstraction in most functional programming languages, such as Standard ML~\cite{sml} and~\cite{Scala}, which decompose tuples using pattern matching.
2066
2067
2068\section{Conclusion and Future Work}
2069
2070The goal of \CFA is to provide an evolutionary pathway for large C development-environments to be more productive and safer, while respecting the talent and skill of C programmers.
2071While other programming languages purport to be a better C, they are in fact new and interesting languages in their own right, but not C extensions.
2072The purpose of this paper is to introduce \CFA, and showcase language features that illustrate the \CFA type-system and approaches taken to achieve the goal of evolutionary C extension.
2073The contributions are a powerful type-system using parametric polymorphism and overloading, generic types, and tuples, which all have complex interactions.
2074The work is a challenging design, engineering, and implementation exercise.
2075On the surface, the project may appear as a rehash of similar mechanisms in \CC.
2076However, every \CFA feature is different than its \CC counterpart, often with extended functionality, better integration with C and its programmers, and always supporting separate compilation.
2077All of these new features are being used by the \CFA development-team to build the \CFA runtime-system.
2078Finally, we demonstrate that \CFA performance for some idiomatic cases is better than C and close to \CC, showing the design is practically applicable.
2079
2080There is ongoing work on a wide range of \CFA feature extensions, including arrays with size, exceptions, concurrent primitives, modules, and user-defined conversions.
2081(While all examples in the paper compile and run, a public beta-release of \CFA will take another 8--12 months to finalize these additional extensions.)
2082In addition, there are interesting future directions for the polymorphism design.
2083Notably, \CC template functions trade compile time and code bloat for optimal runtime of individual instantiations of polymorphic functions.
2084\CFA polymorphic functions use dynamic virtual-dispatch;
2085the runtime overhead of this approach is low, but not as low as inlining, and it may be beneficial to provide a mechanism for performance-sensitive code.
2086Two promising approaches are an @inline@ annotation at polymorphic function call sites to create a template-specialization of the function (provided the code is visible) or placing an @inline@ annotation on polymorphic function-definitions to instantiate a specialized version for some set of types (\CC template specialization).
2087These approaches are not mutually exclusive and allow performance optimizations to be applied only when necessary, without suffering global code-bloat.
2088In general, we believe separate compilation, producing smaller code, works well with loaded hardware-caches, which may offset the benefit of larger inlined-code.
2089
2090
2091\section{Acknowledgments}
2092
2093The authors would like to recognize the design assistance of Glen Ditchfield, Richard Bilson, and Thierry Delisle on the features described in this paper, and thank Magnus Madsen and the three anonymous reviewers for valuable feedback.
2094%This work is supported in part by a corporate partnership with \grantsponsor{Huawei}{Huawei Ltd.}{http://www.huawei.com}, and Aaron Moss and Peter Buhr are funded by the \grantsponsor{Natural Sciences and Engineering Research Council} of Canada.
2095% the first author's \grantsponsor{NSERC-PGS}{NSERC PGS D}{http://www.nserc-crsng.gc.ca/Students-Etudiants/PG-CS/BellandPostgrad-BelletSuperieures_eng.asp} scholarship.
2096
2097
2098\bibliographystyle{plain}
2099\bibliography{pl}
2100
2101
2102\appendix
2103
2104\section{Benchmark Stack Implementation}
2105\label{sec:BenchmarkStackImplementation}
2106
2107\lstset{basicstyle=\linespread{0.9}\sf\small}
2108
2109Throughout, @/***/@ designates a counted redundant type annotation.
2110
2111\smallskip\noindent
2112\CFA
2113\begin{lstlisting}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
2114forall(otype T) struct stack_node {
2115        T value;
2116        stack_node(T) * next;
2117};
2118forall(otype T) void ?{}(stack(T) * s) { (&s->head){ 0 }; }
2119forall(otype T) void ?{}(stack(T) * s, stack(T) t) {
2120        stack_node(T) ** crnt = &s->head;
2121        for ( stack_node(T) * next = t.head; next; next = next->next ) {
2122                *crnt = ((stack_node(T) *)malloc()){ next->value }; /***/
2123                stack_node(T) * acrnt = *crnt;
2124                crnt = &acrnt->next;
2125        }
2126        *crnt = 0;
2127}
2128forall(otype T) stack(T) ?=?(stack(T) * s, stack(T) t) {
2129        if ( s->head == t.head ) return *s;
2130        clear(s);
2131        s{ t };
2132        return *s;
2133}
2134forall(otype T) void ^?{}(stack(T) * s) { clear(s); }
2135forall(otype T) _Bool empty(const stack(T) * s) { return s->head == 0; }
2136forall(otype T) void push(stack(T) * s, T value) {
2137        s->head = ((stack_node(T) *)malloc()){ value, s->head }; /***/
2138}
2139forall(otype T) T pop(stack(T) * s) {
2140        stack_node(T) * n = s->head;
2141        s->head = n->next;
2142        T x = n->value;
2143        ^n{};
2144        free(n);
2145        return x;
2146}
2147forall(otype T) void clear(stack(T) * s) {
2148        for ( stack_node(T) * next = s->head; next; ) {
2149                stack_node(T) * crnt = next;
2150                next = crnt->next;
2151                delete(crnt);
2152        }
2153        s->head = 0;
2154}
2155\end{lstlisting}
2156
2157\medskip\noindent
2158\CC
2159\begin{lstlisting}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
2160template<typename T> class stack {
2161        struct node {
2162                T value;
2163                node * next;
2164                node( const T & v, node * n = nullptr ) : value(v), next(n) {}
2165        };
2166        node * head;
2167        void copy(const stack<T>& o) {
2168                node ** crnt = &head;
2169                for ( node * next = o.head;; next; next = next->next ) {
2170                        *crnt = new node{ next->value }; /***/
2171                        crnt = &(*crnt)->next;
2172                }
2173                *crnt = nullptr;
2174        }
2175  public:
2176        stack() : head(nullptr) {}
2177        stack(const stack<T>& o) { copy(o); }
2178        stack(stack<T> && o) : head(o.head) { o.head = nullptr; }
2179        ~stack() { clear(); }
2180        stack & operator= (const stack<T>& o) {
2181                if ( this == &o ) return *this;
2182                clear();
2183                copy(o);
2184                return *this;
2185        }
2186        stack & operator= (stack<T> && o) {
2187                if ( this == &o ) return *this;
2188                head = o.head;
2189                o.head = nullptr;
2190                return *this;
2191        }
2192        bool empty() const { return head == nullptr; }
2193        void push(const T & value) { head = new node{ value, head };  /***/ }
2194        T pop() {
2195                node * n = head;
2196                head = n->next;
2197                T x = std::move(n->value);
2198                delete n;
2199                return x;
2200        }
2201        void clear() {
2202                for ( node * next = head; next; ) {
2203                        node * crnt = next;
2204                        next = crnt->next;
2205                        delete crnt;
2206                }
2207                head = nullptr;
2208        }
2209};
2210\end{lstlisting}
2211
2212\medskip\noindent
2213C
2214\begin{lstlisting}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
2215struct stack_node {
2216        void * value;
2217        struct stack_node * next;
2218};
2219struct stack new_stack() { return (struct stack){ NULL }; /***/ }
2220void copy_stack(struct stack * s, const struct stack * t, void * (*copy)(const void *)) {
2221        struct stack_node ** crnt = &s->head;
2222        for ( struct stack_node * next = t->head; next; next = next->next ) {
2223                *crnt = malloc(sizeof(struct stack_node)); /***/
2224                **crnt = (struct stack_node){ copy(next->value) }; /***/
2225                crnt = &(*crnt)->next;
2226        }
2227        *crnt = 0;
2228}
2229_Bool stack_empty(const struct stack * s) { return s->head == NULL; }
2230void push_stack(struct stack * s, void * value) {
2231        struct stack_node * n = malloc(sizeof(struct stack_node)); /***/
2232        *n = (struct stack_node){ value, s->head }; /***/
2233        s->head = n;
2234}
2235void * pop_stack(struct stack * s) {
2236        struct stack_node * n = s->head;
2237        s->head = n->next;
2238        void * x = n->value;
2239        free(n);
2240        return x;
2241}
2242void clear_stack(struct stack * s, void (*free_el)(void *)) {
2243        for ( struct stack_node * next = s->head; next; ) {
2244                struct stack_node * crnt = next;
2245                next = crnt->next;
2246                free_el(crnt->value);
2247                free(crnt);
2248        }
2249        s->head = NULL;
2250}
2251\end{lstlisting}
2252
2253\medskip\noindent
2254\CCV
2255\begin{lstlisting}[xleftmargin=2\parindentlnth,aboveskip=0pt,belowskip=0pt]
2256stack::node::node( const object & v, node * n ) : value( v.new_copy() ), next( n ) {}
2257void stack::copy(const stack & o) {
2258        node ** crnt = &head;
2259        for ( node * next = o.head; next; next = next->next ) {
2260                *crnt = new node{ *next->value };
2261                crnt = &(*crnt)->next;
2262        }
2263        *crnt = nullptr;
2264}
2265stack::stack() : head(nullptr) {}
2266stack::stack(const stack & o) { copy(o); }
2267stack::stack(stack && o) : head(o.head) { o.head = nullptr; }
2268stack::~stack() { clear(); }
2269stack & stack::operator= (const stack & o) {
2270        if ( this == &o ) return *this;
2271        clear();
2272        copy(o);
2273        return *this;
2274}
2275stack & stack::operator= (stack && o) {
2276        if ( this == &o ) return *this;
2277        head = o.head;
2278        o.head = nullptr;
2279        return *this;
2280}
2281bool stack::empty() const { return head == nullptr; }
2282void stack::push(const object & value) { head = new node{ value, head }; /***/ }
2283ptr<object> stack::pop() {
2284        node * n = head;
2285        head = n->next;
2286        ptr<object> x = std::move(n->value);
2287        delete n;
2288        return x;
2289}
2290void stack::clear() {
2291        for ( node * next = head; next; ) {
2292                node * crnt = next;
2293                next = crnt->next;
2294                delete crnt;
2295        }
2296        head = nullptr;
2297}
2298\end{lstlisting}
2299
2300
2301\begin{comment}
2302
2303\subsubsection{bench.h}
2304(\texttt{bench.hpp} is similar.)
2305
2306\lstinputlisting{evaluation/bench.h}
2307
2308\subsection{C}
2309
2310\subsubsection{c-stack.h} ~
2311
2312\lstinputlisting{evaluation/c-stack.h}
2313
2314\subsubsection{c-stack.c} ~
2315
2316\lstinputlisting{evaluation/c-stack.c}
2317
2318\subsubsection{c-pair.h} ~
2319
2320\lstinputlisting{evaluation/c-pair.h}
2321
2322\subsubsection{c-pair.c} ~
2323
2324\lstinputlisting{evaluation/c-pair.c}
2325
2326\subsubsection{c-print.h} ~
2327
2328\lstinputlisting{evaluation/c-print.h}
2329
2330\subsubsection{c-print.c} ~
2331
2332\lstinputlisting{evaluation/c-print.c}
2333
2334\subsubsection{c-bench.c} ~
2335
2336\lstinputlisting{evaluation/c-bench.c}
2337
2338\subsection{\CFA}
2339
2340\subsubsection{cfa-stack.h} ~
2341
2342\lstinputlisting{evaluation/cfa-stack.h}
2343
2344\subsubsection{cfa-stack.c} ~
2345
2346\lstinputlisting{evaluation/cfa-stack.c}
2347
2348\subsubsection{cfa-print.h} ~
2349
2350\lstinputlisting{evaluation/cfa-print.h}
2351
2352\subsubsection{cfa-print.c} ~
2353
2354\lstinputlisting{evaluation/cfa-print.c}
2355
2356\subsubsection{cfa-bench.c} ~
2357
2358\lstinputlisting{evaluation/cfa-bench.c}
2359
2360\subsection{\CC}
2361
2362\subsubsection{cpp-stack.hpp} ~
2363
2364\lstinputlisting[language=c++]{evaluation/cpp-stack.hpp}
2365
2366\subsubsection{cpp-print.hpp} ~
2367
2368\lstinputlisting[language=c++]{evaluation/cpp-print.hpp}
2369
2370\subsubsection{cpp-bench.cpp} ~
2371
2372\lstinputlisting[language=c++]{evaluation/cpp-bench.cpp}
2373
2374\subsection{\CCV}
2375
2376\subsubsection{object.hpp} ~
2377
2378\lstinputlisting[language=c++]{evaluation/object.hpp}
2379
2380\subsubsection{cpp-vstack.hpp} ~
2381
2382\lstinputlisting[language=c++]{evaluation/cpp-vstack.hpp}
2383
2384\subsubsection{cpp-vstack.cpp} ~
2385
2386\lstinputlisting[language=c++]{evaluation/cpp-vstack.cpp}
2387
2388\subsubsection{cpp-vprint.hpp} ~
2389
2390\lstinputlisting[language=c++]{evaluation/cpp-vprint.hpp}
2391
2392\subsubsection{cpp-vbench.cpp} ~
2393
2394\lstinputlisting[language=c++]{evaluation/cpp-vbench.cpp}
2395\end{comment}
2396
2397\end{document}
2398
2399% Local Variables: %
2400% tab-width: 4 %
2401% compile-command: "make" %
2402% End: %
Note: See TracBrowser for help on using the repository browser.