source: doc/user/user.tex @ 0d10090

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

modify C-sharp macro

  • Property mode set to 100644
File size: 201.2 KB
Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%%
2%%
3%% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
4%%
5%% The contents of this file are covered under the licence agreement in the
6%% file "LICENCE" distributed with Cforall.
7%%
8%% user.tex --
9%%
10%% Author           : Peter A. Buhr
11%% Created On       : Wed Apr  6 14:53:29 2016
12%% Last Modified By : Peter A. Buhr
13%% Last Modified On : Wed Apr 12 12:18:58 2017
14%% Update Count     : 1415
15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17% requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
18
19\documentclass[twoside,11pt]{article}
20
21%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
22
23% Latex packages used in the document.
24\usepackage[T1]{fontenc}                                % allow Latin1 (extended ASCII) characters
25\usepackage{textcomp}
26\usepackage[latin1]{inputenc}
27
28\usepackage{fullpage,times,comment}
29\usepackage{epic,eepic}
30\usepackage{upquote}                                                                    % switch curled `'" to straight
31\usepackage{calc}
32\usepackage{xspace}
33\usepackage{graphicx}
34\usepackage{varioref}                                                                   % extended references
35\usepackage{listings}                                                                   % format program code
36\usepackage[flushmargin]{footmisc}                                              % support label/reference in footnote
37\usepackage{latexsym}                                   % \Box glyph
38\usepackage{mathptmx}                                   % better math font with "times"
39\usepackage[usenames]{color}
40\usepackage[pagewise]{lineno}
41\renewcommand{\linenumberfont}{\scriptsize\sffamily}
42\input{common}                                          % bespoke macros used in the document
43\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
44\usepackage{breakurl}
45\renewcommand{\UrlFont}{\small\sf}
46
47\setlength{\topmargin}{-0.45in}                                                 % move running title into header
48\setlength{\headsep}{0.25in}
49
50%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51
52\CFAStyle                                                                                               % use default CFA format-style
53
54% inline code ©...© (copyright symbol) emacs: C-q M-)
55% red highlighting ®...® (registered trademark symbol) emacs: C-q M-.
56% blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_
57% green highlighting ¢...¢ (cent symbol) emacs: C-q M-"
58% LaTex escape §...§ (section symbol) emacs: C-q M-'
59% keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^
60% math escape $...$ (dollar symbol)
61
62%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
63
64% Names used in the document.
65\newcommand{\Version}{\input{../../version}}
66\newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}}
67\newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}}
68\newcommand{\R}[1]{\Textbf{#1}}
69\newcommand{\B}[1]{{\Textbf[blue]{#1}}}
70\newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}}
71
72\newsavebox{\LstBox}
73
74%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
75
76\setcounter{secnumdepth}{3}                             % number subsubsections
77\setcounter{tocdepth}{3}                                % subsubsections in table of contents
78\makeindex
79
80%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81
82\title{\Huge
83\vspace*{1in}
84\CFA (\CFL) User Manual                         \\
85Version 1.0                                                     \\
86\vspace*{0.25in}
87\huge``describe not prescribe''
88\vspace*{1in}
89}% title
90
91\author{
92\huge \CFA Team \medskip \\
93\Large Peter A. Buhr, Richard Bilson, Thierry Delisle, \smallskip \\
94\Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Rob Schluntz
95}% author
96
97\date{
98DRAFT \\ \today
99}% date
100
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102
103\begin{document}
104\pagestyle{headings}
105% changed after setting pagestyle
106\renewcommand{\sectionmark}[1]{\markboth{\thesection\quad #1}{\thesection\quad #1}}
107\renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}}
108\pagenumbering{roman}
109\linenumbers                                            % comment out to turn off line numbering
110
111\maketitle
112\thispagestyle{empty}
113\vspace*{\fill}
114\noindent
115\copyright\,2016 \CFA Project \\ \\
116\noindent
117This work is licensed under the Creative Commons Attribution 4.0 International License.
118To view a copy of this license, visit {\small\url{http://creativecommons.org/licenses/by/4.0}}.
119\vspace*{1in}
120
121\clearpage
122\thispagestyle{plain}
123\pdfbookmark[1]{Contents}{section}
124\tableofcontents
125
126\clearpage
127\thispagestyle{plain}
128\pagenumbering{arabic}
129
130
131\section{Introduction}
132
133\CFA\footnote{Pronounced ``C-for-all'', and written \CFA, CFA, or \CFL.} is a modern general-purpose programming-language, designed as an evolutionary step forward from the C programming language.
134The syntax of the \CFA language builds from C, and should look immediately familiar to C/\Index*[C++]{\CC} programmers.
135% Any language feature that is not described here can be assumed to be using the standard C11 syntax.
136\CFA adds many modern programming-language features that directly lead to increased \emph{safety} and \emph{productivity}, while maintaining interoperability with existing C programs and achieving C performance.
137Like C, \CFA is a statically typed, procedural language with a low-overhead runtime, meaning there is no global garbage-collection.
138The primary new features include parametric-polymorphic routines and types, exceptions, concurrency, and modules.
139
140One of the main design philosophies of \CFA is to ``describe not prescribe'', which means \CFA tries to provide a pathway from low-level C programming to high-level \CFA programming, but it does not force programmers to ``do the right thing''.
141Programmers can cautiously add \CFA extensions to their C programs in any order and at any time to incrementally move towards safer, higher-level programming features.
142A programmer is always free to reach back to C from \CFA for any reason, and in many cases, new \CFA features have a fallback to a C mechanism.
143There is no notion or requirement for rewriting a legacy C program in \CFA;
144instead, a programmer evolves an existing C program into \CFA by incrementally incorporating \CFA features.
145New programs can be written in \CFA using a combination of C and \CFA features.
146\Index*[C++]{\CC} had a similar goal 30 years ago, but has struggled over the intervening time to incorporate modern programming-language features because of early design choices.
147\CFA has 30 years of hindsight and a clean starting point.
148
149Like \Index*[C++]{\CC}, there may be both an old and new ways to achieve the same effect.
150For example, the following programs compare the \CFA and C I/O mechanisms.
151\begin{quote2}
152\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
153\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
154\begin{cfa}
155#include <fstream>
156int main( void ) {
157        int x = 0, y = 1, z = 2;
158        ®sout | x | y | z | endl;®
159}
160\end{cfa}
161&
162\begin{lstlisting}
163#include <stdio.h>
164int main( void ) {
165        int x = 0, y = 1, z = 2;
166        ®printf( "%d %d %d\n", x, y, z );®
167}
168\end{lstlisting}
169\end{tabular}
170\end{quote2}
171Both programs output the same result.
172While the \CFA I/O looks similar to the \Index*[C++]{\CC} output style, there are important differences, such as automatic spacing between variables as in \Index*{Python} (see also~\VRef{s:IOLibrary}).
173
174This document is a user manual for the \CFA programming language, targeted at \CFA programmers.
175Implementers may refer to the \CFA Programming Language Specification for details about the language syntax and semantics.
176In its current state, this document covers the intended core features of the language.
177Changes to the syntax and additional features are expected to be included in later revisions.
178% For additional information, see \url{http://wiki.do-lang.org}.
179
180
181\section{History}
182
183The \CFA project started with K-W C~\cite{Buhr94a,Till89}, which extended C with new declaration syntax, multiple return values from routines, and extended assignment capabilities using the notion of tuples.
184(See~\cite{Werther96} for some similar work, but for \Index*[C++]{\CC}.)
185The original \CFA project~\cite{Ditchfield92} extended the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC} approach of object-oriented extensions to the C type-system.
186A first implementation of the core Cforall language was created~\cite{Bilson03,Esteves04}, but at the time there was little interesting in extending C, so work did not continue.
187As the saying goes, ``What goes around, comes around.'', and there is now renewed interest in the C programming language because of legacy code-bases, so the \CFA project has been restarted.
188
189
190\section{Why fix C?}
191
192Even with all its problems, C is a very popular programming language because it allows writing software at virtually any level in a computer system without restriction.
193For system programming, where direct access to hardware and dealing with real-time issues is a requirement, C is usually the language of choice.
194As well, there are millions of lines of C legacy code, forming the base for many software development projects (especially on UNIX systems).
195The TIOBE index (\url{http://www.tiobe.com/tiobe_index}) for March 2016 shows programming-language popularity, with \Index*{Java} 20.5\%, C 14.5\%, \Index*[C++]{\CC} 6.7\%, \Csharp 4.3\%, \Index*{Python} 4.3\%, and all other programming languages below 3\%.
196As well, for 30 years, C has been the number 1 and 2 most popular programming language:
197\begin{center}
198\setlength{\tabcolsep}{1.5ex}
199\begin{tabular}{@{}r|c|c|c|c|c|c|c@{}}
200Ranking & 2016  & 2011  & 2006  & 2001  & 1996  & 1991  & 1986          \\
201\hline
202Java    & 1             & 1             & 1             & 3             & 29    & -             & -                     \\
203\hline
204\R{C}   & \R{2} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} & \R{1}         \\
205\hline
206\CC             & 3             & 3             & 3             & 2             & 2             & 2             & 7                     \\
207\end{tabular}
208\end{center}
209Hence, C is still an extremely important programming language, with double the usage of \Index*[C++]{\CC}, where \CC itself is largely C code.
210Finally, love it or hate it, C has been an important and influential part of computer science for 40 years and it appears it will continue to be for many more years.
211Unfortunately, C has too many problems and omissions to make it an acceptable programming language for modern needs.
212
213The goal of this project is to engineer modern language features into C in an evolutionary rather than revolutionary way.
214\CC~\cite{c++,ANSI14:C++} is an example of a similar project;
215however, it largely extended the language, and did not address many existing problems.\footnote{%
216Two important existing problems addressed were changing the type of character literals from ©int© to ©char© and enumerator from ©int© to the type of its enumerators.}
217\Index*{Fortran}~\cite{Fortran08}, \Index*{Ada}~\cite{Ada12}, and \Index*{Cobol}~\cite{Cobol14} are examples of programming languages that took an evolutionary approach, where modern language features (\eg objects, concurrency) are added and problems fixed within the framework of the existing language.
218\Index*{Java}~\cite{Java8}, \Index*{Go}~\cite{Go}, \Index*{Rust}~\cite{Rust} and \Index*{D}~\cite{D} are examples of the revolutionary approach for modernizing C/\CC, resulting in a new language rather than an extension of the descendent.
219These languages have different syntax and semantics from C, and do not interoperate directly with C, largely because of garbage collection.
220As a result, there is a significant learning curve to move to these languages, and C legacy-code must be rewritten.
221These costs can be prohibitive for many companies with a large software base in C/\CC, and a significant number of programmers requiring retraining to a new programming language.
222
223The result of this project is a language that is largely backwards compatible with C11~\cite{C11}, but fixing some of the well known C problems and containing many modern language features.
224Without significant extension to the C programming language, it is becoming unable to cope with the needs of modern programming problems and programmers;
225as a result, it will fade into disuse.
226Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language.
227While C11 made a few simple extensions to the language, nothing was added to address existing problems in the language or to augment the language with modern language features.
228While some may argue that modern language features may make C complex and inefficient, it is clear a language without modern capabilities is insufficient for the advanced programming problems existing today.
229
230
231\section{Interoperability}
232\label{s:Interoperability}
233
234\CFA is designed to integrate well with existing C programs and libraries.
235The most important feature of interoperability is to use the same calling conventions, so there is no overhead to call existing C routines.
236This feature allows users of \CFA to take advantage of the existing panoply of C libraries from inside their \CFA code.
237In fact, one of the biggest issues for any new programming language is establishing a minimum level of library code to support a large body of activities.
238Language developers often state that adequate library support takes more work than designing and implementing the language itself.
239Like \Index*[C++]{\CC}, \CFA starts with immediate access to all exiting C libraries, and in many cases, can easily wrap library routines with simpler and safer interfaces, at very low cost.
240Hence, \CFA begins by leveraging the large repository of C libraries with little cost.
241
242However, it is necessary to differentiate between C and \CFA code because of name overloading, as for \CC.
243For example, the C math-library provides the following routines for computing the absolute value of the basic types: ©abs©, ©labs©, ©llabs©, ©fabs©, ©fabsf©, ©fabsl©, ©cabsf©, ©cabs©, and ©cabsl©.
244Whereas, \CFA wraps each of these routines into ones with the common name ©abs©:
245\begin{cfa}
246char abs( char );
247®extern "C" {®
248int abs( int );                                 §\C{// use default C routine for int}§
249®}® // extern "C"
250long int abs( long int );
251long long int abs( long long int );
252float abs( float );
253double abs( double );
254long double abs( long double );
255float _Complex abs( float _Complex );
256double _Complex abs( double _Complex );
257long double _Complex abs( long double _Complex );
258\end{cfa}
259The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©.
260Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}.
261Then overloading polymorphism uses a mechanism called \newterm{name mangling}\index{mangling!name} to create unique names that are different from C names, which are not mangled.
262Hence, there is the same need as in \CC, to know if a name is a C or \CFA name, so it can be correctly formed.
263There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type.
264This example strongly illustrates a core idea in \CFA: \emph{the power of a name}.
265The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value.
266Hence, knowing the name ©abs© should be sufficient to apply it to any type where it is applicable.
267The time savings and safety of using one name uniformly versus $N$ unique names should not be underestimated.
268
269
270\section[Compiling CFA Program]{Compiling \CFA Program}
271
272The command ©cfa© is used to compile \CFA program(s), and is based on the GNU \Indexc{gcc} command, \eg:
273\begin{cfa}
274cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] C/§\CFA§-files [ assembler/loader-files ]
275\end{cfa}
276\CFA programs having the following ©gcc© flags turned on:
277\begin{description}
278\item
279\Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{©-std=gnu99©}}
280The 1999 C standard plus GNU extensions.
281\item
282\Indexc{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{©-fgnu89-inline©}}
283Use the traditional GNU semantics for inline routines in C99 mode, which allows inline routines in header files.
284\end{description}
285The following new \CFA options are available:
286\begin{description}
287\item
288\Indexc{-CFA}\index{compilation option!-CFA@©-CFA©}
289Only the C preprocessor and the \CFA translator steps are performed and the transformed program is written to standard output, which makes it possible to examine the code generated by the \CFA translator.
290The generated code started with the standard \CFA prelude.
291
292\item
293\Indexc{-debug}\index{compilation option!-debug@©-debug©}
294The program is linked with the debugging version of the runtime system.
295The debug version performs runtime checks to help during the debugging phase of a \CFA program, but substantially slows the execution of the program.
296The runtime checks should only be removed after the program is completely debugged.
297\textbf{This option is the default.}
298
299\item
300\Indexc{-nodebug}\index{compilation option!-nodebug@©-nodebug©}
301The program is linked with the non-debugging version of the runtime system, so the execution of the program is faster.
302\Emph{However, no runtime checks or ©assert©s are performed so errors usually result in abnormal program termination.}
303
304\item
305\Indexc{-help}\index{compilation option!-help@©-help©}
306Information about the set of \CFA compilation flags is printed.
307
308\item
309\Indexc{-nohelp}\index{compilation option!-nohelp@©-nohelp©}
310Information about the set of \CFA compilation flags is not printed.
311\textbf{This option is the default.}
312
313\item
314\Indexc{-quiet}\index{compilation option!-quiet@©-quiet©}
315The \CFA compilation message is not printed at the beginning of a compilation.
316
317\item
318\Indexc{-noquiet}\index{compilation option!-noquiet@©-noquiet©}
319The \CFA compilation message is printed at the beginning of a compilation.
320\textbf{This option is the default.}
321
322\item
323\Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@©-no-include-stdhdr©}
324Do not supply ©extern "C"© wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}).
325\textbf{This option is \emph{not} the default.}
326\end{description}
327
328The following preprocessor variables are available:
329\begin{description}
330\item
331\Indexc{__CFA_MAJOR__}\index{preprocessor variables!__CFA__@{©__CFA__©}}
332is available during preprocessing and its value is the major \Index{version number} of \CFA.\footnote{
333The C preprocessor allows only integer values in a preprocessor variable so a value like ``\Version'' is not allowed.
334Hence, the need to have three variables for the major, minor and patch version number.}
335
336\item
337\Indexc{__CFA_MINOR__}\index{preprocessor variables!__CFA_MINOR__@{©__CFA_MINOR__©}}
338is available during preprocessing and its value is the minor \Index{version number} of \CFA.
339
340\item
341\Indexc{__CFA_PATCH__}\index{preprocessor variables!__CFA_PATCH____CFA_PATCH__©}
342is available during preprocessing and its value is the patch \Index{level number} of \CFA.
343
344\item
345\Indexc{__CFA__}\index{preprocessor variables!__CFA____CFA__©},
346\Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL____CFORALL__©} and
347\Indexc{__cforall}\index{preprocessor variables!__cforall@©__cforall©}
348are always available during preprocessing and have no value.
349\end{description}
350These preprocessor variables allow conditional compilation of programs that must work differently in these situations.
351For example, to toggle between C and \CFA extensions, using the following:
352\begin{cfa}
353#ifndef __CFORALL__
354#include <stdio.h>                              §\C{// C header file}§
355#else
356#include <fstream>                              §\C{// \CFA header file}§
357#endif
358\end{cfa}
359which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}.
360
361
362\section{Constants Underscores}
363
364Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, \eg:
365\begin{cfa}
366_®147®_®483®_®648;                    §\C{// decimal constant}§
36756_ul;                                                  §\C{// decimal unsigned long constant}§
3680_377;                                                  §\C{// octal constant}§
3690x_ff_ff;                                               §\C{// hexadecimal constant}§
3700x_ef3d_aa5c;                                   §\C{// hexadecimal constant}§
3713.141_592_654;                                  §\C{// floating point constant}§
37210_e_+1_00;                                             §\C{// floating point constant}§
3730x_ff_ff_p_3;                                   §\C{// hexadecimal floating point}§
3740x_1.ffff_ffff_p_128_l;                 §\C{// hexadecimal floating point long constant}§
375L_"\x_ff_ee";                                   §\C{// wide character constant}§
376\end{cfa}
377The rules for placement of underscores is as follows:
378\begin{enumerate}
379\item
380A sequence of underscores is disallowed, \eg ©12__34© is invalid.
381\item
382Underscores may only appear within a sequence of digits (regardless of the digit radix).
383In other words, an underscore cannot start or end a sequence of digits, \eg ©_1©, ©1_© and ©_1_© are invalid (actually, the 1st and 3rd examples are identifier names).
384\item
385A numeric prefix may end with an underscore;
386a numeric infix may begin and/or end with an underscore;
387a numeric suffix may begin with an underscore.
388For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©;
389the exponent infix ©E© may start or end with an underscore ©1.0_E10©, ©1.0E_10© or ©1.0_E_10©;
390the type suffixes ©U©, ©L©, etc. may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.
391\end{enumerate}
392It is significantly easier to read and enter long constants when they are broken up into smaller groupings (most cultures use comma or period among digits for the same purpose).
393This extension is backwards compatible, matches with the use of underscore in variable names, and appears in \Index*{Ada} and \Index*{Java} 8.
394
395
396\section{Backquote Identifiers}
397\label{s:BackquoteIdentifiers}
398
399\CFA accommodates keyword clashes by syntactic transformations using the \CFA backquote escape-mechanism:
400\begin{cfa}
401int ®`®otype®`® = 3;                    §\C{// make keyword an identifier}§
402double ®`®choose®`® = 3.5;
403\end{cfa}
404Programs can be converted easily by enclosing keyword identifiers in backquotes, and the backquotes can be removed later when the identifier name is changed to an non-keyword name.
405Clashes in C header files (see~\VRef{s:StandardHeaders}) can be handled automatically using the preprocessor, ©#include_next© and ©-I filename©:
406\begin{cfa}
407// include file uses the CFA keyword "otype".
408#if ! defined( otype )                  §\C{// nesting ?}§
409#define otype `otype`
410#define __CFA_BFD_H__
411#endif // ! otype
412
413#include_next <bfd.h>                   §\C{// must have internal check for multiple expansion}§
414
415#if defined( otype ) && defined( __CFA_BFD_H__ )        §\C{// reset only if set}§
416#undef otype
417#undef __CFA_BFD_H__
418#endif // otype && __CFA_BFD_H__
419\end{cfa}
420
421
422\section{Declarations}
423\label{s:Declarations}
424
425C declaration syntax is notoriously confusing and error prone.
426For example, many C programmers are confused by a declaration as simple as:
427\begin{quote2}
428\begin{tabular}{@{}ll@{}}
429\begin{cfa}
430int *x[5]
431\end{cfa}
432&
433\raisebox{-0.75\totalheight}{\input{Cdecl}}
434\end{tabular}
435\end{quote2}
436Is this an array of 5 pointers to integers or a \Index{pointer} to an array of 5 integers?
437The fact this declaration is unclear to many C programmers means there are \Index{productivity} and \Index{safety} issues even for basic programs.
438Another 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.
439For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way:
440\begin{cfa}
441int (*f())[5] {...};                    §\C{}§
442... (*f())[3] += 1;
443\end{cfa}
444Essentially, the return type is wrapped around the routine name in successive layers (like an onion).
445While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice.
446
447\CFA provides its own type, variable and routine declarations, using a different syntax.
448The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type.
449In the following example, \R{red} is for the base type and \B{blue} is for the qualifiers.
450The \CFA declarations move the qualifiers to the left of the base type, i.e., 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.
451\begin{quote2}
452\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
453\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
454\begin{cfa}
455ß[5] *ß ®int® x1;
456ß* [5]ß ®int® x2;
457ß[* [5] int]ß f®( int p )®;
458\end{cfa}
459&
460\begin{cfa}
461®int® ß*ß x1 ß[5]ß;
462®int® ß(*ßx2ß)[5]ß;
463ßint (*ßf®( int p )®ß)[5]ß;
464\end{cfa}
465\end{tabular}
466\end{quote2}
467The only exception is bit field specification, which always appear to the right of the base type.
468% 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.
469However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list.
470For instance, variables ©x© and ©y© of type \Index{pointer} to integer are defined in \CFA as follows:
471\begin{quote2}
472\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
473\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
474\begin{cfa}
475®*® int x, y;
476\end{cfa}
477&
478\begin{cfa}
479int ®*®x, ®*®y;
480\end{cfa}
481\end{tabular}
482\end{quote2}
483The downside of this semantics is the need to separate regular and \Index{pointer} declarations:
484\begin{quote2}
485\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
486\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
487\begin{cfa}
488®*® int x;
489int y;
490\end{cfa}
491&
492\begin{cfa}
493int ®*®x, y;
494
495\end{cfa}
496\end{tabular}
497\end{quote2}
498which is \Index{prescribing} a safety benefit.
499Other examples are:
500\begin{quote2}
501\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
502\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
503\begin{cfa}
504[ 5 ] int z;
505[ 5 ] * char w;
506* [ 5 ] double v;
507struct s {
508        int f0:3;
509        * int f1;
510        [ 5 ] * int f2;
511};
512\end{cfa}
513&
514\begin{cfa}
515int z[ 5 ];
516char *w[ 5 ];
517double (*v)[ 5 ];
518struct s {
519        int f0:3;
520        int *f1;
521        int *f2[ 5 ]
522};
523\end{cfa}
524&
525\begin{cfa}
526// array of 5 integers
527// array of 5 pointers to char
528// pointer to array of 5 doubles
529
530// common bit field syntax
531
532
533
534\end{cfa}
535\end{tabular}
536\end{quote2}
537
538All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg:
539\begin{quote2}
540\begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}}
541\multicolumn{1}{c@{\hspace{1em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{1em}}}{\textbf{C}} \\
542\begin{cfa}
543const * const int x;
544const * [ 5 ] const int y;
545\end{cfa}
546&
547\begin{cfa}
548int const * const x;
549const int (* const y)[ 5 ]
550\end{cfa}
551&
552\begin{cfa}
553// const pointer to const integer
554// const pointer to array of 5 const integers
555\end{cfa}
556\end{tabular}
557\end{quote2}
558All 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}
559The 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:
560\begin{quote2}
561\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
562\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
563\begin{cfa}
564extern [ 5 ] int x;
565static * const int y;
566\end{cfa}
567&
568\begin{cfa}
569int extern x[ 5 ];
570const int static *y;
571\end{cfa}
572&
573\begin{cfa}
574// externally visible array of 5 integers
575// internally visible pointer to constant int
576\end{cfa}
577\end{tabular}
578\end{quote2}
579
580Unsupported are K\&R C declarations where the base type defaults to ©int©, if no type is specified,\footnote{
581At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each structure declaration and type name~\cite[\S~6.7.2(2)]{C11}}
582\eg:
583\begin{cfa}
584x;                                                              §\C{// int x}§
585*y;                                                             §\C{// int *y}§
586f( p1, p2 );                                    §\C{// int f( int p1, int p2 );}§
587f( p1, p2 ) {}                                  §\C{// int f( int p1, int p2 ) {}}§
588\end{cfa}
589
590Finally, new \CFA declarations may appear together with C declarations in the same program block, but cannot be mixed within a specific declaration.
591Therefore, a programmer has the option of either continuing to use traditional C declarations or take advantage of the new style.
592Clearly, both styles need to be supported for some time due to existing C-style header-files, particularly for UNIX systems.
593
594
595\section{Pointer / Reference}
596
597C provides a \newterm{pointer type};
598\CFA adds a \newterm{reference type}.
599Both types contain an \newterm{address}, which is normally a location in memory.
600Special addresses are used to denote certain states or access co-processor memory.
601By convention, no variable is placed at address 0, so addresses like 0, 1, 2, 3 are often used to denote no-value or other special states.
602Often dereferencing a special state causes a \Index{memory fault}, so checking is necessary during execution.
603If the programming language assigns addresses, a program's execution is \Index{sound}, i.e., all addresses are to valid memory locations.
604C allows programmers to assign addresses, so there is the potential for incorrect addresses, both inside and outside of the computer address-space.
605
606Program variables are implicit pointers to memory locations generated by the compiler and automatically dereferenced, as in:
607\begin{quote2}
608\begin{tabular}{@{}lll@{}}
609\begin{cfa}
610int x;
611x = 3;
612int y;
613y = x;
614\end{cfa}
615&
616\raisebox{-0.45\totalheight}{\input{pointer1}}
617&
618\begin{cfa}
619int * ®const® x = (int *)100
620*x = 3;                 // implicit dereference
621int * ®const® y = (int *)104;
622*y = *x;                // implicit dereference
623\end{cfa}
624\end{tabular}
625\end{quote2}
626where the right example is how the compiler logically interprets the variables in the left example.
627Since a variable name only points to one location during its lifetime, it is an \Index{immutable} \Index{pointer};
628hence, variables ©x© and ©y© are constant pointers in the compiler interpretation.
629In general, variable addresses are stored in instructions instead of loaded independently, so an instruction fetch implicitly loads a variable's address.
630\begin{quote2}
631\begin{tabular}{@{}l|l@{}}
632\begin{cfa}
633lda             r1,100                  // load address of x
634ld              r2,(r1)                   // load value of x
635lda             r3,104                  // load address of y
636st              r2,(r3)                   // store x into y
637\end{cfa}
638&
639\begin{cfa}
640
641ld              r2,(100)                // load value of x
642
643st              r2,(104)                // store x into y
644\end{cfa}
645\end{tabular}
646\end{quote2}
647Finally, the immutable nature of a variable's address and the fact that there is no storage for a variable address means pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible.
648Therefore, the expression ©x = y© only has one meaning, ©*x = *y©, i.e., manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of instruction decoding.
649
650A \Index{pointer}/\Index{reference} is a generalization of a variable name, i.e., a mutable address that can point to more than one memory location during its lifetime.
651(Similarly, an integer variable can contain multiple integer literals during its lifetime versus an integer constant representing a single literal during its lifetime and may not occupy storage as the literal is embedded directly into instructions.)
652Hence, a pointer occupies memory to store its current address, and the pointer's value is loaded by dereferencing, \eg:
653\begin{quote2}
654\begin{tabular}{@{}ll@{}}
655\begin{cfa}
656int x, y, ®*® p1, ®*® p2, ®**® p3;
657p1 = ®&®x;               // p1 points to x
658p2 = p1;                 // p2 points to x
659p1 = ®&®y;               // p1 points to y
660p3 = &p2;               // p3 points to p2
661\end{cfa}
662&
663\raisebox{-0.45\totalheight}{\input{pointer2.pstex_t}}
664\end{tabular}
665\end{quote2}
666
667Notice, an address has a duality\index{address!duality}: a location in memory or the value at that location.
668In many cases, a compiler might be able to infer the meaning:
669\begin{cfa}
670p2 = p1 + x;                                    §\C{// compiler infers *p2 = *p1 + x;}§
671\end{cfa}
672because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation.
673\Index*{Algol68}~\cite{Algol68} inferences pointer dereferencing to select the best meaning for each pointer usage.
674However, in C, the following cases are ambiguous, especially with pointer arithmetic:
675\begin{cfa}
676p1 = p2;                                                §\C{// p1 = p2\ \ or\ \ *p1 = *p2}§
677p1 = p1 + 1;                                    §\C{// p1 = p1 + 1\ \ or\ \ *p1 = *p1 + 1}§
678\end{cfa}
679
680Most languages pick one meaning as the default and the programmer explicitly indicates the other meaning to resolve the address-duality ambiguity\index{address! ambiguity}.
681In C, the default meaning for pointers is to manipulate the pointer's address and the pointed-to value is explicitly accessed by the dereference operator ©*©.
682\begin{cfa}
683p1 = p2;                                                §\C{// pointer address assignment}§
684*p1 = *p1 + 1;                                  §\C{// pointed-to value assignment / operation}§
685\end{cfa}
686which works well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©).
687
688However, in most other situations, the pointed-to value is requested more often than the pointer address.
689\begin{cfa}
690*p2 = ((*p1 + *p2) * (**p3 - *p1)) / (**p3 - 15);
691\end{cfa}
692In this case, it is tedious to explicitly write the dereferencing, and error prone when pointer arithmetic is allowed.
693It is better to have the compiler generate the dereferencing and have no implicit pointer arithmetic:
694\begin{cfa}
695p2 = ((p1 + p2) * (p3 - p1)) / (p3 - 15);
696\end{cfa}
697
698To switch the default meaning for an address requires a new kind of pointer, called a \newterm{reference} denoted by ©&©.
699\begin{cfa}
700int x, y, ®&® r1, ®&® r2, ®&&® r3;
701®&®r1 = &x;                                             §\C{// r1 points to x}§
702®&®r2 = &r1;                                    §\C{// r2 points to x}§
703®&®r1 = &y;                                             §\C{// r1 points to y}§
704®&&®r3 = ®&®&r2;                                §\C{// r3 points to r2}§
705r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§
706\end{cfa}
707Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example.
708Hence, a reference behaves like the variable name for the current variable it is pointing-to.
709The simplest way to understand a reference is to imagine the compiler inserting a dereference operator before the reference variable for each reference qualifier in a declaration, \eg:
710\begin{cfa}
711r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15);
712\end{cfa}
713is rewritten as:
714\begin{cfa}
715®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15);
716\end{cfa}
717When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out.\footnote{
718The unary ©&© operator yields the address of its operand.
719If the operand has type ``type'', the result has type ``pointer to type''.
720If the operand is the result of a unary ©*© operator, neither that operator nor the ©&© operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.~\cite[\S~6.5.3.2--3]{C11}}
721Hence, assigning to a reference requires the address of the reference variable (\Index{lvalue}):
722\begin{cfa}
723(&®*®)r1 = &x;                                  §\C{// (\&*) cancel giving variable r1 not variable pointed-to by r1}§
724\end{cfa}
725Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}):
726\begin{cfa}
727(&(&®*®)®*®)r3 = &(&®*®)r2;             §\C{// (\&*) cancel giving address of r2, (\&(\&*)*) cancel giving variable r3}§
728\end{cfa}
729Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth, and pointer and reference values are interchangeable because both contain addresses.
730\begin{cfa}
731int x, *p1 = &x, **p2 = &p1, ***p3 = &p2,
732                 &r1 = x,    &&r2 = r1,   &&&r3 = r2;
733***p3 = 3;                                              §\C{// change x}§
734r3 = 3;                                                 §\C{// change x, ***r3}§
735**p3 = ...;                                             §\C{// change p1}§
736&r3 = ...;                                              §\C{// change r1, (\&*)**r3, 1 cancellation}§
737*p3 = ...;                                              §\C{// change p2}§
738&&r3 = ...;                                             §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§
739&&&r3 = p3;                                             §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§
740\end{cfa}
741Finally, implicit dereferencing and cancellation are a static (compilation) phenomenon not a dynamic one.
742That is, all implicit dereferencing and any cancellation is carried out prior to the start of the program, so reference performance is equivalent to pointer performance.
743A programmer selects a pointer or reference type solely on whether the address is dereferenced frequently or infrequently, which dictates the amount of direct aid from the compiler;
744otherwise, everything else is equal.
745
746Interestingly, \Index*[C++]{\CC} deals with the address duality by making the pointed-to value the default, and prevent\-ing changes to the reference address, which eliminates half of the duality.
747\Index*{Java} deals with the address duality by making address assignment the default and requiring field assignment (direct or indirect via methods), i.e., there is no builtin bit-wise or method-wise assignment, which eliminates half of the duality.
748
749As for a pointer, a reference may have qualifiers:
750\begin{cfa}
751const int cx = 5;                               §\C{// cannot change cx;}§
752const int & cr = cx;                    §\C{// cannot change what cr points to}§
753®&®cr = &cx;                                    §\C{// can change cr}§
754cr = 7;                                                 §\C{// error, cannot change cx}§
755int & const rc = x;                             §\C{// must be initialized, \CC reference}§
756®&®rc = &x;                                             §\C{// error, cannot change rc}§
757const int & const crc = cx;             §\C{// must be initialized, \CC reference}§
758crc = 7;                                                §\C{// error, cannot change cx}§
759®&®crc = &cx;                                   §\C{// error, cannot change crc}§
760\end{cfa}
761Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be ©0© unless an arbitrary pointer is assigned to the reference}, \eg:
762\begin{cfa}
763int & const r = *0;                             §\C{// where 0 is the int * zero}§
764\end{cfa}
765Otherwise, the compiler is managing the addresses for type ©& const© not the programmer, and by a programming discipline of only using references with references, address errors can be prevented.
766Finally, the position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers.
767The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations;
768\CFA-style declarations attempt to address this issue:
769\begin{quote2}
770\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
771\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
772\begin{cfa}
773®const® * ®const® * const int ccp;
774®const® & ®const® & const int ccr;
775\end{cfa}
776&
777\begin{cfa}
778const int * ®const® * ®const® ccp;
779
780\end{cfa}
781\end{tabular}
782\end{quote2}
783where the \CFA declaration is read left-to-right (see \VRef{s:Declarations}).
784
785\Index{Initialization} is different than \Index{assignment} because initialization occurs on the empty (uninitialized) storage on an object, while assignment occurs on possibly initialized storage of an object.
786There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding.
787For reference initialization (like pointer), the initializing value must be an address (\Index{lvalue}) not a value (\Index{rvalue}).
788\begin{cfa}
789int * p = &x;                                   §\C{// both \&x and x are possible interpretations}§
790int & r = x;                                    §\C{// x unlikely interpretation, because of auto-dereferencing}§
791\end{cfa}
792Hence, the compiler implicitly inserts a reference operator, ©&©, before the initialization expression.
793Similarly, when a reference is used for a parameter/return type, the call-site argument does not require a reference operator.
794\begin{cfa}
795int & f( int & rp );                    §\C{// reference parameter and return}§
796z = f( x ) + f( y );                    §\C{// reference operator added, temporaries needed for call results}§
797\end{cfa}
798Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©rp© can be locally reassigned within ©f©.
799Since ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references.
800
801When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions.
802\begin{cfa}
803void f( ®const® int & crp );
804void g( ®const® int * cpp );
805f( 3 );                   g( &3 );
806f( x + y );             g( &(x + y) );
807\end{cfa}
808Here, the compiler passes the address to the literal 3 or the temporary for the expression ©x + y©, knowing the argument cannot be changed through the parameter.
809(The ©&© is necessary for the pointer parameter to make the types match, and is a common requirement for a C programmer.)
810\CFA \emph{extends} this semantics to a mutable pointer/reference parameter, and the compiler implicitly creates the necessary temporary (copying the argument), which is subsequently pointed-to by the reference parameter and can be changed.
811\begin{cfa}
812void f( int & rp );
813void g( int * pp );
814f( 3 );                   g( &3 );              §\C{// compiler implicit generates temporaries}§
815f( x + y );             g( &(x + y) );  §\C{// compiler implicit generates temporaries}§
816\end{cfa}
817Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{
818This conversion attempts to address the \newterm{const hell} problem, when the innocent addition of a ©const© qualifier causes a cascade of type failures, requiring an unknown number of additional ©const© qualifiers, until it is discovered a ©const© qualifier cannot be added and all the ©const© qualifiers must be removed.}
819The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call.
820
821While \CFA attempts to handle pointers and references in a uniform, symmetric manner, C handles routine variables in an inconsistent way: a routine variable is both a pointer and a reference (particle and wave).
822\begin{cfa}
823void f( int p ) {...}
824void (*fp)( int ) = &f;                 §\C{// pointer initialization}§
825void (*fp)( int ) = f;                  §\C{// reference initialization}§
826(*fp)(3);                                               §\C{// pointer invocation}§
827fp(3);                                                  §\C{// reference invocation}§
828\end{cfa}
829A routine variable is best described by a ©const© reference:
830\begin{cfa}
831const void (&fp)( int ) = f;
832fp( 3 );
833fp = ...                                                §\C{// error, cannot change code}§
834&fp = ...;                                              §\C{// changing routine reference}§
835\end{cfa}
836because the value of the routine variable is a routine literal, i.e., the routine code is normally immutable during execution.\footnote{
837Dynamic code rewriting is possible but only in special circumstances.}
838\CFA allows this additional use of references for routine variables in an attempt to give a more consistent meaning for them.
839
840
841\section{Type Operators}
842
843The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine ©sizeof©:
844\begin{quote2}
845\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
846\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
847\begin{cfa}
848y = (®* int®)x;
849i = sizeof(®[ 5 ] * int®);
850\end{cfa}
851&
852\begin{cfa}
853y = (®int *®)x;
854i = sizeof(®int *[ 5 ]®);
855\end{cfa}
856\end{tabular}
857\end{quote2}
858
859
860\section{Routine Definition}
861
862\CFA also supports a new syntax for routine definition, as well as ISO C and K\&R routine syntax.
863The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg:
864\begin{cfa}
865®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) {
866        §\emph{routine body}§
867}
868\end{cfa}
869where routine ©f© has three output (return values) and three input parameters.
870Existing C syntax cannot be extended with multiple return types because it is impossible to embed a single routine name within multiple return type specifications.
871
872In detail, the brackets, ©[]©, enclose the result type, where each return value is named and that name is a local variable of the particular return type.\footnote{
873\Index*{Michael Tiemann}, with help from \Index*{Doug Lea}, provided named return values in g++, circa 1989.}
874The value of each local return variable is automatically returned at routine termination.
875Declaration qualifiers can only appear at the start of a routine definition, \eg:
876\begin{cfa}
877®extern® [ int x ] g( int y ) {§\,§}
878\end{cfa}
879Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified;
880in both cases the type is assumed to be void as opposed to old style C defaults of int return type and unknown parameter types, respectively, as in:
881\begin{cfa}
882\,§] g();                                             §\C{// no input or output parameters}§
883[ void ] g( void );                             §\C{// no input or output parameters}§
884\end{cfa}
885
886Routine f is called as follows:
887\begin{cfa}
888[ i, j, ch ] = f( 3, 'a', ch );
889\end{cfa}
890The list of return values from f and the grouping on the left-hand side of the assignment is called a \newterm{return list} and discussed in Section 12.
891
892\CFA style declarations cannot be used to declare parameters for K\&R style routine definitions because of the following ambiguity:
893\begin{cfa}
894int (*f(x))[ 5 ] int x; {}
895\end{cfa}
896The string ``©int (*f(x))[ 5 ]©'' declares a K\&R style routine of type returning a pointer to an array of 5 integers, while the string ``©[ 5 ] int x©'' declares a \CFA style parameter x of type array of 5 integers.
897Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string.
898As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity:
899\begin{cfa}
900typedef int foo;
901int f( int (* foo) );                   §\C{// foo is redefined as a parameter name}§
902\end{cfa}
903The string ``©int (* foo)©'' declares a C-style named-parameter of type pointer to an integer (the parenthesis are superfluous), while the same string declares a \CFA style unnamed parameter of type routine returning integer with unnamed parameter of type pointer to foo.
904The redefinition of a type name in a parameter list is the only context in C where the character ©*© can appear to the left of a type name, and \CFA relies on all type qualifier characters appearing to the right of the type name.
905The inability to use \CFA declarations in these two contexts is probably a blessing because it precludes programmers from arbitrarily switching between declarations forms within a declaration contexts.
906
907C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg:
908\begin{cfa}
909[ int ] f( * int, int * );              §\C{// returns an integer, accepts 2 pointers to integers}§
910[ * int, int * ] f( int );              §\C{// returns 2 pointers to integers, accepts an integer}§
911\end{cfa}
912The reason for allowing both declaration styles in the new context is for backwards compatibility with existing preprocessor macros that generate C-style declaration-syntax, as in:
913\begin{cfa}
914#define ptoa( n, d ) int (*n)[ d ]
915int f( ptoa( p, 5 ) ) ...               §\C{// expands to int f( int (*p)[ 5 ] )}§
916[ int ] f( ptoa( p, 5 ) ) ...   §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§
917\end{cfa}
918Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms.
919
920
921\subsection{Named Return Values}
922
923\Index{Named return values} handle the case where it is necessary to define a local variable whose value is then returned in a ©return© statement, as in:
924\begin{cfa}
925int f() {
926        int x;
927        ... x = 0; ... x = y; ...
928        return x;
929}
930\end{cfa}
931Because the value in the return variable is automatically returned when a \CFA routine terminates, the ©return© statement \emph{does not} contain an expression, as in:
932\newline
933\begin{minipage}{\linewidth}
934\begin{cfa}
935®[ int x, int y ]® f() {
936        int z;
937        ... x = 0; ... y = z; ...
938        ®return;® §\C{// implicitly return x, y}§
939}
940\end{cfa}
941\end{minipage}
942\newline
943When the return is encountered, the current values of ©x© and ©y© are returned to the calling routine.
944As well, ``falling off the end'' of a routine without a ©return© statement is permitted, as in:
945\begin{cfa}
946[ int x, int y ] f() {
947        ...
948} §\C{// implicitly return x, y}§
949\end{cfa}
950In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered.
951
952
953\subsection{Routine Prototype}
954
955The syntax of the new routine prototype declaration follows directly from the new routine definition syntax;
956as well, parameter names are optional, \eg:
957\begin{cfa}
958[ int x ] f ();                                 §\C{// returning int with no parameters}§
959[ * int ] g (int y);                    §\C{// returning pointer to int with int parameter}§
960[ ] h (int,char);                               §\C{// returning no result with int and char parameters}§
961[ * int,int ] j (int);                  §\C{// returning pointer to int and int, with int parameter}§
962\end{cfa}
963This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa).
964It is possible to declare multiple routine-prototypes in a single declaration, but the entire type specification is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:Declarations}), \eg:
965\begin{quote2}
966\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
967\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
968\begin{cfa}
969[ int ] f(int), g;
970\end{cfa}
971&
972\begin{cfa}
973int f(int), g(int);
974\end{cfa}
975\end{tabular}
976\end{quote2}
977Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg:
978\begin{cfa}
979extern [ int ] f (int);
980static [ int ] g (int);
981\end{cfa}
982
983
984\section{Routine Pointers}
985
986The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg:
987\begin{cfa}
988* [ int x ] () fp;                      §\C{// pointer to routine returning int with no parameters}§
989* [ * int ] (int y) gp;         §\C{// pointer to routine returning pointer to int with int parameter}§
990* [ ] (int,char) hp;            §\C{// pointer to routine returning no result with int and char parameters}§
991* [ * int,int ] (int) jp;       §\C{// pointer to routine returning pointer to int and int, with int parameter}§
992\end{cfa}
993While parameter names are optional, \emph{a routine name cannot be specified};
994for example, the following is incorrect:
995\begin{cfa}
996* [ int x ] f () fp;            §\C{// routine name "f" is not allowed}§
997\end{cfa}
998
999
1000\section{Named and Default Arguments}
1001
1002Named and default arguments~\cite{Hardgrave76}\footnote{
1003Francez~\cite{Francez77} proposed a further extension to the named-parameter passing style, which specifies what type of communication (by value, by reference, by name) the argument is passed to the routine.}
1004are two mechanisms to simplify routine call.
1005Both mechanisms are discussed with respect to \CFA.
1006\begin{description}
1007\item[Named (or Keyword) Arguments:]
1008provide the ability to specify an argument to a routine call using the parameter name rather than the position of the parameter.
1009For example, given the routine:
1010\begin{cfa}
1011void p( int x, int y, int z ) {...}
1012\end{cfa}
1013a positional call is:
1014\begin{cfa}
1015p( 4, 7, 3 );
1016\end{cfa}
1017whereas a named (keyword) call may be:
1018\begin{cfa}
1019p( z : 3, x : 4, y : 7 );       §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§
1020\end{cfa}
1021Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters.
1022The compiler rewrites a named call into a positional call.
1023The advantages of named parameters are:
1024\begin{itemize}
1025\item
1026Remembering the names of the parameters may be easier than the order in the routine definition.
1027\item
1028Parameter names provide documentation at the call site (assuming the names are descriptive).
1029\item
1030Changes can be made to the order or number of parameters without affecting the call (although the call must still be recompiled).
1031\end{itemize}
1032
1033Unfortunately, named arguments do not work in C-style programming-languages because a routine prototype is not required to specify parameter names, nor do the names in the prototype have to match with the actual definition.
1034For example, the following routine prototypes and definition are all valid.
1035\begin{cfa}
1036void p( int, int, int );                        §\C{// equivalent prototypes}§
1037void p( int x, int y, int z );
1038void p( int y, int x, int z );
1039void p( int z, int y, int x );
1040void p( int q, int r, int s ) {}        §\C{// match with this definition}§
1041\end{cfa}
1042Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming.
1043Alternatively, prototype definitions can be eliminated by using a two-pass compilation, and implicitly creating header files for exports.
1044The former is easy to do, while the latter is more complex.
1045
1046Furthermore, named arguments do not work well in a \CFA-style programming-languages because they potentially introduces a new criteria for type matching.
1047For example, it is technically possible to disambiguate between these two overloaded definitions of ©f© based on named arguments at the call site:
1048\begin{cfa}
1049int f( int i, int j );
1050int f( int x, double y );
1051
1052f( j : 3, i : 4 );                              §\C{// 1st f}§
1053f( x : 7, y : 8.1 );                    §\C{// 2nd f}§
1054f( 4, 5 );                                              §\C{// ambiguous call}§
1055\end{cfa}
1056However, named arguments compound routine resolution in conjunction with conversions:
1057\begin{cfa}
1058f( i : 3, 5.7 );                                §\C{// ambiguous call ?}§
1059\end{cfa}
1060Depending on the cost associated with named arguments, this call could be resolvable or ambiguous.
1061Adding named argument into the routine resolution algorithm does not seem worth the complexity.
1062Therefore, \CFA does \emph{not} attempt to support named arguments.
1063
1064\item[Default Arguments]
1065provide the ability to associate a default value with a parameter so it can be optionally specified in the argument list.
1066For example, given the routine:
1067\begin{cfa}
1068void p( int x = 1, int y = 2, int z = 3 ) {...}
1069\end{cfa}
1070the allowable positional calls are:
1071\begin{cfa}
1072p();                                                    §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§
1073p( 4 );                                                 §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§
1074p( 4, 4 );                                              §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§
1075p( 4, 4, 4 );                                   §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§
1076// empty arguments
1077p(  , 4, 4 );                                   §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§
1078p( 4,  , 4 );                                   §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§
1079p( 4, 4,   );                                   §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§
1080p( 4,  ,   );                                   §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§
1081p(  , 4,   );                                   §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§
1082p(  ,  , 4 );                                   §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§
1083p(  ,  ,   );                                   §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§
1084\end{cfa}
1085Here the missing arguments are inserted from the default values in the parameter list.
1086The compiler rewrites missing default values into explicit positional arguments.
1087The advantages of default values are:
1088\begin{itemize}
1089\item
1090Routines with a large number of parameters are often very generalized, giving a programmer a number of different options on how a computation is performed.
1091For many of these kinds of routines, there are standard or default settings that work for the majority of computations.
1092Without default values for parameters, a programmer is forced to specify these common values all the time, resulting in long argument lists that are error prone.
1093\item
1094When a routine's interface is augmented with new parameters, it extends the interface providing generalizability\footnote{
1095``It should be possible for the implementor of an abstraction to increase its generality.
1096So long as the modified abstraction is a generalization of the original, existing uses of the abstraction will not require change.
1097It might be possible to modify an abstraction in a manner which is not a generalization without affecting existing uses, but, without inspecting the modules in which the uses occur, this possibility cannot be determined.
1098This criterion precludes the addition of parameters, unless these parameters have default or inferred values that are valid for all possible existing applications.''~\cite[p.~128]{Cormack90}}
1099(somewhat like the generalization provided by inheritance for classes).
1100That is, all existing calls are still valid, although the call must still be recompiled.
1101\end{itemize}
1102The only disadvantage of default arguments is that unintentional omission of an argument may not result in a compiler-time error.
1103Instead, a default value is used, which may not be the programmer's intent.
1104
1105Default values may only appear in a prototype versus definition context:
1106\begin{cfa}
1107void p( int x, int y = 2, int z = 3 );          §\C{// prototype: allowed}§
1108void p( int, int = 2, int = 3 );                        §\C{// prototype: allowed}§
1109void p( int x, int y = 2, int z = 3 ) {}        §\C{// definition: not allowed}§
1110\end{cfa}
1111The reason for this restriction is to allow separate compilation.
1112Multiple prototypes with different default values is an error.
1113\end{description}
1114
1115Ellipse (``...'') arguments present problems when used with default arguments.
1116The conflict occurs because both named and ellipse arguments must appear after positional arguments, giving two possibilities:
1117\begin{cfa}
1118p( /* positional */, ... , /* named */ );
1119p( /* positional */, /* named */, ... );
1120\end{cfa}
1121While it is possible to implement both approaches, the first possibly is more complex than the second, \eg:
1122\begin{cfa}
1123p( int x, int y, int z, ... );
1124p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§
1125p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§
1126\end{cfa}
1127In the first call, it is necessary for the programmer to conceptually rewrite the call, changing named arguments into positional, before knowing where the ellipse arguments begin.
1128Hence, this approach seems significantly more difficult, and hence, confusing and error prone.
1129In the second call, the named arguments separate the positional and ellipse arguments, making it trivial to read the call.
1130
1131The problem is exacerbated with default arguments, \eg:
1132\begin{cfa}
1133void p( int x, int y = 2, int z = 3... );
1134p( 1, 4, 5, 6, z : 3 );         §\C{// assume p( /* positional */, ... , /* named */ );}§
1135p( 1, z : 3, 4, 5, 6 );         §\C{// assume p( /* positional */, /* named */, ... );}§
1136\end{cfa}
1137The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments;
1138therefore, argument 5 subsequently conflicts with the named argument z : 3.
1139In the second call, the default value for y is implicitly inserted after argument 1 and the named arguments separate the positional and ellipse arguments, making it trivial to read the call.
1140For these reasons, \CFA requires named arguments before ellipse arguments.
1141Finally, while ellipse arguments are needed for a small set of existing C routines, like printf, the extended \CFA type system largely eliminates the need for ellipse arguments (see Section 24), making much of this discussion moot.
1142
1143Default arguments and overloading (see Section 24) are complementary.
1144While in theory default arguments can be simulated with overloading, as in:
1145\begin{quote2}
1146\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1147\multicolumn{1}{c@{\hspace{3em}}}{\textbf{default arguments}}   & \multicolumn{1}{c}{\textbf{overloading}}      \\
1148\begin{cfa}
1149void p( int x, int y = 2, int z = 3 ) {...}
1150
1151
1152\end{cfa}
1153&
1154\begin{cfa}
1155void p( int x, int y, int z ) {...}
1156void p( int x ) { p( x, 2, 3 ); }
1157void p( int x, int y ) { p( x, y, 3 ); }
1158\end{cfa}
1159\end{tabular}
1160\end{quote2}
1161the number of required overloaded routines is linear in the number of default values, which is unacceptable growth.
1162In general, overloading should only be used over default arguments if the body of the routine is significantly different.
1163Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as:
1164\begin{cfa}
1165p( 1, /* default */, 5 );               §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§
1166\end{cfa}
1167
1168Given the \CFA restrictions above, both named and default arguments are backwards compatible.
1169\Index*[C++]{\CC} only supports default arguments;
1170\Index*{Ada} supports both named and default arguments.
1171
1172
1173\section{Type/Routine Nesting}
1174
1175Nesting of types and routines is useful for controlling name visibility (\newterm{name hiding}).
1176
1177
1178\subsection{Type Nesting}
1179
1180\CFA allows \Index{type nesting}, and type qualification of the nested typres (see \VRef[Figure]{f:TypeNestingQualification}), where as C hoists\index{type hoisting} (refactors) nested types into the enclosing scope and has no type qualification.
1181\begin{figure}
1182\centering
1183\begin{tabular}{@{}l@{\hspace{3em}}l|l@{}}
1184\multicolumn{1}{c@{\hspace{3em}}}{\textbf{C Type Nesting}}      & \multicolumn{1}{c}{\textbf{C Implicit Hoisting}}      & \multicolumn{1}{|c}{\textbf{\CFA}}    \\
1185\hline
1186\begin{cfa}
1187struct S {
1188        enum C { R, G, B };
1189        struct T {
1190                union U { int i, j; };
1191                enum C c;
1192                short int i, j;
1193        };
1194        struct T t;
1195} s;
1196
1197int fred() {
1198        s.t.c = R;
1199        struct T t = { R, 1, 2 };
1200        enum C c;
1201        union U u;
1202}
1203\end{cfa}
1204&
1205\begin{cfa}
1206enum C { R, G, B };
1207union U { int i, j; };
1208struct T {
1209        enum C c;
1210        short int i, j;
1211};
1212struct S {
1213        struct T t;
1214} s;
1215       
1216
1217
1218
1219
1220
1221
1222\end{cfa}
1223&
1224\begin{cfa}
1225struct S {
1226        enum C { R, G, B };
1227        struct T {
1228                union U { int i, j; };
1229                enum C c;
1230                short int i, j;
1231        };
1232        struct T t;
1233} s;
1234
1235int fred() {
1236        s.t.c = ®S.®R;  // type qualification
1237        struct ®S.®T t = { ®S.®R, 1, 2 };
1238        enum ®S.®C c;
1239        union ®S.T.®U u;
1240}
1241\end{cfa}
1242\end{tabular}
1243\caption{Type Nesting / Qualification}
1244\label{f:TypeNestingQualification}
1245\end{figure}
1246In the left example in C, types ©C©, ©U© and ©T© are implicitly hoisted outside of type ©S© into the containing block scope.
1247In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``©.©'' for type qualification, as does \Index*{Java}, rather than the \CC type-selection operator ``©::©''.
1248
1249
1250\subsection{Routine Nesting}
1251
1252While \CFA does not provide object programming by putting routines into structures, it does rely heavily on locally nested routines to redefine operations at or close to a call site.
1253For example, the C quick-sort is wrapped into the following polymorphic \CFA routine:
1254\begin{cfa}
1255forall( otype T | { int ?<?( T, T ); } )
1256void qsort( const T * arr, size_t dimension );
1257\end{cfa}
1258which can be used to sort in ascending and descending order by locally redefining the less-than operator into greater-than.
1259\begin{cfa}
1260const unsigned int size = 5;
1261int ia[size];
1262...                                             §\C{// assign values to array ia}§
1263qsort( ia, size );              §\C{// sort ascending order using builtin ?<?}§
1264{
1265        ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§
1266        qsort( ia, size );      §\C{// sort descending order by local redefinition}§
1267}
1268\end{cfa}
1269
1270Nested routines are not first-class, meaning a nested routine cannot be returned if it has references to variables in its enclosing blocks;
1271the only exception is references to the external block of the translation unit, as these variables persist for the duration of the program.
1272The following program in undefined in \CFA (and Indexc{gcc})
1273\begin{cfa}
1274[* [int]( int )] foo() {                §\C{// int (*foo())( int )}§
1275        int ®i® = 7;
1276        int bar( int p ) {
1277                ®i® += 1;                               §\C{// dependent on local variable}§
1278                sout | ®i® | endl;
1279        }
1280        return bar;                                     §\C{// undefined because of local dependence}§
1281}
1282int main() {
1283        * [int](int) fp = foo();        §\C{// int (*fp)(int)}§
1284    sout | fp( 3 ) | endl;
1285}
1286\end{cfa}
1287because
1288
1289Currently, there are no \Index{lambda} expressions, i.e., unnamed routines because routine names are very important to properly select the correct routine.
1290
1291
1292\section{Lexical List}
1293
1294In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call.
1295(More contexts are added shortly.)
1296A list of such elements is called a \newterm{lexical list}.
1297The general syntax of a lexical list is:
1298\begin{cfa}
1299[ §\emph{exprlist}§ ]
1300\end{cfa}
1301where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas.
1302The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator.
1303The following are examples of lexical lists:
1304\begin{cfa}
1305[ x, y, z ]
1306[ 2 ]
1307[ v+w, x*y, 3.14159, f() ]
1308\end{cfa}
1309Tuples are permitted to contain sub-tuples (i.e., nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple.
1310Note, a tuple is not a record (structure);
1311a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1).
1312In essence, tuples are largely a compile time phenomenon, having little or no runtime presence.
1313
1314Tuples can be organized into compile-time tuple variables;
1315these variables are of \newterm{tuple type}.
1316Tuple variables and types can be used anywhere lists of conventional variables and types can be used.
1317The general syntax of a tuple type is:
1318\begin{cfa}
1319[ §\emph{typelist}§ ]
1320\end{cfa}
1321where ©$\emph{typelist}$© is a list of one or more legal \CFA or C type specifications separated by commas, which may include other tuple type specifications.
1322Examples of tuple types include:
1323\begin{cfa}
1324[ unsigned int, char ]
1325[ double, double, double ]
1326[ * int, int * ]                §\C{// mix of CFA and ANSI}§
1327[ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ]
1328\end{cfa}
1329Like tuples, tuple types may be nested, such as ©[ [ int, int ], int ]©, which is a 2-element tuple type whose first element is itself a tuple type.
1330
1331Examples of declarations using tuple types are:
1332\begin{cfa}
1333[ int, int ] x;                 §\C{// 2 element tuple, each element of type int}§
1334* [ char, char ] y;             §\C{// pointer to a 2 element tuple}§
1335[ [ int, int ] ] z ([ int, int ]);
1336\end{cfa}
1337The last example declares an external routine that expects a 2 element tuple as an input parameter and returns a 2 element tuple as its result.
1338
1339As mentioned, tuples can appear in contexts requiring a list of value, such as an argument list of a routine call.
1340In unambiguous situations, the tuple brackets may be omitted, \eg a tuple that appears as an argument may have its
1341square brackets omitted for convenience; therefore, the following routine invocations are equivalent:
1342\begin{cfa}
1343f( [ 1, x+2, fred() ] );
1344f( 1, x+2, fred() );
1345\end{cfa}
1346Also, a tuple or a tuple variable may be used to supply all or part of an argument list for a routine expecting multiple input parameters or for a routine expecting a tuple as an input parameter.
1347For example, the following are all legal:
1348\begin{cfa}
1349[ int, int ] w1;
1350[ int, int, int ] w2;
1351[ void ] f (int, int, int); /* three input parameters of type int */
1352[ void ] g ([ int, int, int ]); /* 3 element tuple as input */
1353f( [ 1, 2, 3 ] );
1354f( w1, 3 );
1355f( 1, w1 );
1356f( w2 );
1357g( [ 1, 2, 3 ] );
1358g( w1, 3 );
1359g( 1, w1 );
1360g( w2 );
1361\end{cfa}
1362Note, in all cases 3 arguments are supplied even though the syntax may appear to supply less than 3. As mentioned, a
1363tuple does not have structure like a record; a tuple is simply converted into a list of components.
1364\begin{rationale}
1365The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; i.e., a statement such as ©g( f() )© is not supported.
1366Using a temporary variable to store the  results of the inner routine and then passing this variable to the outer routine works, however.
1367\end{rationale}
1368
1369A tuple can contain a C comma expression, provided the expression containing the comma operator is enclosed in parentheses.
1370For instance, the following tuples are equivalent:
1371\begin{cfa}
1372[ 1, 3, 5 ]
1373[ 1, (2, 3), 5 ]
1374\end{cfa}
1375The second element of the second tuple is the expression (2, 3), which yields the result 3.
1376This requirement is the same as for comma expressions in argument lists.
1377
1378Type qualifiers, i.e., const and volatile, may modify a tuple type.
1379The meaning is the same as for a type qualifier modifying an aggregate type [Int99, x 6.5.2.3(7),x 6.7.3(11)], i.e., the qualifier is distributed across all of the types in the tuple, \eg:
1380\begin{cfa}
1381const volatile [ int, float, const int ] x;
1382\end{cfa}
1383is equivalent to:
1384\begin{cfa}
1385[ const volatile int, const volatile float, const volatile int ] x;
1386\end{cfa}
1387Declaration qualifiers can only appear at the start of a \CFA tuple declaration4, \eg:
1388\begin{cfa}
1389extern [ int, int ] w1;
1390static [ int, int, int ] w2;
1391\end{cfa}
1392\begin{rationale}
1393Unfortunately, C's syntax for subscripts precluded treating them as tuples.
1394The C subscript list has the form ©[i][j]...© and not ©[i, j, ...]©.
1395Therefore, there is no syntactic way for a routine returning multiple values to specify the different subscript values, \eg ©f[g()]© always means a single subscript value because there is only one set of brackets.
1396Fixing this requires a major change to C because the syntactic form ©M[i, j, k]© already has a particular meaning: ©i, j, k© is a comma expression.
1397\end{rationale}
1398
1399
1400\subsection{Tuple Coercions}
1401
1402There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring.
1403In addition, the coercion of dereferencing can be performed on a tuple variable to yield its value(s), as for other variables.
1404A \newterm{closing coercion} takes a set of values and converts it into a tuple value, which is a contiguous set of values, as in:
1405\begin{cfa}
1406[ int, int, int, int ] w;
1407w = [ 1, 2, 3, 4 ];
1408\end{cfa}
1409First the right-hand tuple is closed into a tuple value and then the tuple value is assigned.
1410
1411An \newterm{opening coercion} is the opposite of closing; a tuple value is converted into a tuple of values, as in:
1412\begin{cfa}
1413[ a, b, c, d ] = w
1414\end{cfa}
1415©w© is implicitly opened to yield a tuple of four values, which are then assigned individually.
1416
1417A \newterm{flattening coercion} coerces a nested tuple, i.e., a tuple with one or more components, which are themselves tuples, into a flattened tuple, which is a tuple whose components are not tuples, as in:
1418\begin{cfa}
1419[ a, b, c, d ] = [ 1, [ 2, 3 ], 4 ];
1420\end{cfa}
1421First the right-hand tuple is flattened and then the values are assigned individually.
1422Flattening is also performed on tuple types.
1423For example, the type ©[ int, [ int, int ], int ]© can be coerced, using flattening, into the type ©[ int, int, int, int ]©.
1424
1425A \newterm{structuring coercion} is the opposite of flattening;
1426a tuple is structured into a more complex nested tuple.
1427For example, structuring the tuple ©[ 1, 2, 3, 4 ]© into the tuple ©[ 1, [ 2, 3 ], 4 ]© or the tuple type ©[ int, int, int, int ]© into the tuple type ©[ int, [ int, int ], int ]©.
1428In the following example, the last assignment illustrates all the tuple coercions:
1429\begin{cfa}
1430[ int, int, int, int ] w = [ 1, 2, 3, 4 ];
1431int x = 5;
1432[ x, w ] = [ w, x ];            §\C{// all four tuple coercions}§
1433\end{cfa}
1434Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values;
1435therefore, the right-hand tuple is now the tuple ©[ [ 1, 2, 3, 4 ], 5 ]©.
1436This tuple is then flattened, yielding ©[ 1, 2, 3, 4, 5 ]©, which is structured into ©[ 1, [ 2, 3, 4, 5 ] ]© to match the tuple type of the left-hand side.
1437The tuple ©[ 2, 3, 4, 5 ]© is then closed to create a tuple value.
1438Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using multiple assignment (see Section 14).
1439\begin{rationale}
1440A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple.
1441\end{rationale}
1442
1443
1444\section{Mass Assignment}
1445
1446\CFA permits assignment to several variables at once using mass assignment~\cite{CLU}.
1447Mass assignment has the following form:
1448\begin{cfa}
1449[ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§;
1450\end{cfa}
1451\index{lvalue}
1452The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, i.e., any data object that can appear on the left-hand side of a conventional assignment statement.
1453©$\emph{expr}$© is any standard arithmetic expression.
1454Clearly, the types of the entities being assigned must be type compatible with the value of the expression.
1455
1456Mass assignment has parallel semantics, \eg the statement:
1457\begin{cfa}
1458[ x, y, z ] = 1.5;
1459\end{cfa}
1460is equivalent to:
1461\begin{cfa}
1462x = 1.5; y = 1.5; z = 1.5;
1463\end{cfa}
1464This semantics is not the same as the following in C:
1465\begin{cfa}
1466x = y = z = 1.5;
1467\end{cfa}
1468as conversions between intermediate assignments may lose information.
1469A more complex example is:
1470\begin{cfa}
1471[ i, y[i], z ] = a + b;
1472\end{cfa}
1473which is equivalent to:
1474\begin{cfa}
1475t = a + b;
1476a1 = &i; a2 = &y[i]; a3 = &z;
1477*a1 = t; *a2 = t; *a3 = t;
1478\end{cfa}
1479The temporary ©t© is necessary to store the value of the expression to eliminate conversion issues.
1480The temporaries for the addresses are needed so that locations on the left-hand side do not change as the values are assigned.
1481In this case, ©y[i]© uses the previous value of ©i© and not the new value set at the beginning of the mass assignment.
1482
1483
1484\section{Multiple Assignment}
1485
1486\CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}.
1487Multiple assignment has the following form:
1488\begin{cfa}
1489[ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ];
1490\end{cfa}
1491\index{lvalue}
1492The left-hand side is a tuple of \emph{lvalues}, and the right-hand side is a tuple of \emph{expr}s.
1493Each \emph{expr} appearing on the righthand side of a multiple assignment statement is assigned to the corresponding \emph{lvalues} on the left-hand side of the statement using parallel semantics for each assignment.
1494An example of multiple assignment is:
1495\begin{cfa}
1496[ x, y, z ] = [ 1, 2, 3 ];
1497\end{cfa}
1498Here, the values ©1©, ©2© and ©3© are assigned, respectively, to the variables ©x©, ©y© and ©z©.
1499 A more complex example is:
1500\begin{cfa}
1501[ i, y[ i ], z ] = [ 1, i, a + b ];
1502\end{cfa}
1503Here, the values ©1©, ©i© and ©a + b© are assigned to the variables ©i©, ©y[i]© and ©z©, respectively.
1504 Note, the parallel semantics of
1505multiple assignment ensures:
1506\begin{cfa}
1507[ x, y ] = [ y, x ];
1508\end{cfa}
1509correctly interchanges (swaps) the values stored in ©x© and ©y©.
1510The following cases are errors:
1511\begin{cfa}
1512[ a, b, c ] = [ 1, 2, 3, 4 ];
1513[ a, b, c ] = [ 1, 2 ];
1514\end{cfa}
1515because the number of entities in the left-hand tuple is unequal with the right-hand tuple.
1516
1517As for all tuple contexts in C, side effects should not be used because C does not define an ordering for the evaluation of the elements of a tuple;
1518both these examples produce indeterminate results:
1519\begin{cfa}
1520f( x++, x++ );                          §\C{// C routine call with side effects in arguments}§
1521[ v1, v2 ] = [ x++, x++ ];      §\C{// side effects in righthand side of multiple assignment}§
1522\end{cfa}
1523
1524
1525\section{Cascade Assignment}
1526
1527As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment.
1528Cascade assignment has the following form:
1529\begin{cfa}
1530§\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§;
1531\end{cfa}
1532and it has the same parallel semantics as for mass and multiple assignment.
1533Some examples of cascade assignment are:
1534\begin{cfa}
1535x1 = y1 = x2 = y2 = 0;
1536[ x1, y1 ] = [ x2, y2 ] = [ x3, y3 ];
1537[ x1, y1 ] = [ x2, y2 ] = 0;
1538[ x1, y1 ] = z = 0;
1539\end{cfa}
1540As in C, the rightmost assignment is performed first, i.e., assignment parses right to left.
1541
1542
1543\section{Unnamed Structure Fields}
1544
1545C requires each field of a structure to have a name, except for a bit field associated with a basic type, \eg:
1546\begin{cfa}
1547struct {
1548        int f1;                                 §\C{// named field}§
1549        int f2 : 4;                             §\C{// named field with bit field size}§
1550        int : 3;                                §\C{// unnamed field for basic type with bit field size}§
1551        int ;                                   §\C{// disallowed, unnamed field}§
1552        int *;                                  §\C{// disallowed, unnamed field}§
1553        int (*)(int);                   §\C{// disallowed, unnamed field}§
1554};
1555\end{cfa}
1556This requirement is relaxed by making the field name optional for all field declarations; therefore, all the field declarations in the example are allowed.
1557As for unnamed bit fields, an unnamed field is used for padding a structure to a particular size.
1558A list of unnamed fields is also supported, \eg:
1559\begin{cfa}
1560struct {
1561        int , , ;                               §\C{// 3 unnamed fields}§
1562}
1563\end{cfa}
1564
1565
1566\section{Field Tuples}
1567
1568Tuples may be used to select multiple fields of a record by field name.
1569Its general form is:
1570\begin{cfa}
1571§\emph{expr}§ . [ §\emph{fieldlist}§ ]
1572§\emph{expr}§ -> [ §\emph{fieldlist}§ ]
1573\end{cfa}
1574\emph{expr} is any expression yielding a value of type record, \eg ©struct©, ©union©.
1575Each element of \emph{ fieldlist} is an element of the record specified by \emph{expr}.
1576A record-field tuple may be used anywhere a tuple can be used. An example of the use of a record-field tuple is
1577the following:
1578\begin{cfa}
1579struct s {
1580        int f1, f2;
1581        char f3;
1582        double f4;
1583} v;
1584v.[ f3, f1, f2 ] = ['x', 11, 17 ];      §\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}§
1585f( v.[ f3, f1, f2 ] );                          §\C{// equivalent to f( v.f3, v.f1, v.f2 )}§
1586\end{cfa}
1587Note, the fields appearing in a record-field tuple may be specified in any order;
1588also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple.
1589
1590If a field of a ©struct© is itself another ©struct©, multiple fields of this subrecord can be specified using a nested record-field tuple, as in the following example:
1591\begin{cfa}
1592struct inner {
1593        int f2, f3;
1594};
1595struct outer {
1596        int f1;
1597        struct inner i;
1598        double f4;
1599} o;
1600
1601o.[ f1, i.[ f2, f3 ], f4 ] = [ 11, 12, 13, 3.14159 ];
1602\end{cfa}
1603
1604
1605\section{Labelled Continue / Break}
1606
1607While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure.
1608Unfortunately, this restriction forces programmers to use ©goto© to achieve the equivalent control-flow for more than one level of nesting.
1609To prevent having to switch to the ©goto©, \CFA extends the ©continue©\index{continue@©continue©}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and ©break©\index{break@©break©}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85,Java}.
1610For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement;
1611for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement.
1612
1613The following example shows the labelled ©continue© specifying which control structure is the target for the next loop iteration:
1614\begin{quote2}
1615\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1616\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1617\begin{cfa}
1618®L1:® do {
1619        ®L2:® while ( ... ) {
1620                ®L3:® for ( ... ) {
1621                        ... continue ®L1®; ...  // continue do
1622                        ... continue ®L2®; ...  // continue while
1623                        ... continue ®L3®; ...  // continue for
1624                } // for
1625        } // while
1626} while ( ... );
1627\end{cfa}
1628&
1629\begin{cfa}
1630do {
1631        while ( ... ) {
1632                for ( ... ) {
1633                        ... goto L1; ...
1634                        ... goto L2; ...
1635                        ... goto L3; ...
1636                L3: ; }
1637        L2: ; }
1638L1: ; } while ( ... );
1639\end{cfa}
1640\end{tabular}
1641\end{quote2}
1642The innermost loop has three restart points, which cause the next loop iteration to begin.
1643
1644The following example shows the labelled ©break© specifying which control structure is the target for exit:
1645\begin{quote2}
1646\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1647\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{C}}        \\
1648\begin{cfa}
1649®L1:® {
1650        ... §declarations§ ...
1651        ®L2:® switch ( ... ) {
1652          case 3:
1653            ®L3:® if ( ... ) {
1654                        ®L4:® for ( ... ) {
1655                                ... break ®L1®; ...     // exit compound statement
1656                                ... break ®L2®; ...     // exit switch
1657                                ... break ®L3®; ...     // exit if
1658                                ... break ®L4®; ...     // exit loop
1659                        } // for
1660                } else {
1661                        ... break ®L3®; ...             // exit if
1662                } // if
1663        } // switch
1664} // compound
1665\end{cfa}
1666&
1667\begin{cfa}
1668{
1669        ... §declarations§ ...
1670        switch ( ... ) {
1671          case 3:
1672            if ( ... ) {
1673                        for ( ... ) {
1674                                ... goto L1; ...
1675                                ... goto L2; ...
1676                                ... goto L3; ...
1677                                ... goto L4; ...
1678                        } L4: ;
1679                } else {
1680                        ... goto L3; ...
1681                } L3: ;
1682        } L2: ;
1683} L1: ;
1684\end{cfa}
1685\end{tabular}
1686\end{quote2}
1687The innermost loop has four exit points, which cause termination of one or more of the four \Index{nested control structure}s.
1688
1689Both ©continue© and ©break© with target labels are simply a ©goto©\index{goto@©goto©!restricted} restricted in the following ways:
1690\begin{itemize}
1691\item
1692They cannot be used to create a loop.
1693This means that only the looping construct can be used to create a loop.
1694This restriction is important since all situations that can result in repeated execution of statements in a program are clearly delineated.
1695\item
1696Since they always transfer out of containing control structures, they cannot be used to branch into a control structure.
1697\end{itemize}
1698The 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.
1699Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader that complex control-flow is occurring in the body of the control structure.
1700With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.
1701Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs.
1702The implicit targets of the current ©continue© and ©break©, i.e., the closest enclosing loop or ©switch©, change as certain constructs are added or removed.
1703
1704
1705\section{Switch Statement}
1706
1707C allows a number of questionable forms for the ©switch© statement:
1708\begin{enumerate}
1709\item
1710By default, the end of a ©case© clause\footnote{
1711In this section, the term \emph{case clause} refers to either a ©case© or ©default© clause.}
1712\emph{falls through} to the next ©case© clause in the ©switch© statement;
1713to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©:
1714\begin{cfa}
1715switch ( i ) {
1716  case 1:
1717        ...
1718        // fall-through
1719  case 2:
1720        ...
1721        break;  // exit switch statement
1722}
1723\end{cfa}
1724The ability to fall-through to the next clause \emph{is} a useful form of control flow, specifically when a sequence of case actions compound:
1725\begin{quote2}
1726\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
1727\begin{cfa}
1728switch ( argc ) {
1729  case 3:
1730        // open output file
1731        // fall-through
1732  case 2:
1733        // open input file
1734        break;  // exit switch statement
1735  default:
1736        // usage message
1737}
1738\end{cfa}
1739&
1740\begin{cfa}
1741
1742if ( argc == 3 ) {
1743        // open output file
1744        ®// open input file
1745®} else if ( argc == 2 ) {
1746        ®// open input file
1747
1748®} else {
1749        // usage message
1750}
1751\end{cfa}
1752\end{tabular}
1753\end{quote2}
1754In this example, case 2 is always done if case 3 is done.
1755This control flow is difficult to simulate with if statements or a ©switch© statement without fall-through as code must be duplicated or placed in a separate routine.
1756C also uses fall-through to handle multiple case-values resulting in the same action:
1757\begin{cfa}
1758switch ( i ) {
1759  case 1: case 3: case 5:       // odd values
1760        // same action
1761        break;
1762  case 2: case 4: case 6:       // even values
1763        // same action
1764        break;
1765}
1766\end{cfa}
1767However, this situation is handled in other languages without fall-through by allowing a list of case values.
1768While fall-through itself is not a problem, the problem occurs when fall-through is the default, as this semantics is unintuitive to many programmers and is different from virtually all other programming languages with a ©switch© statement.
1769Hence, default fall-through semantics results in a large number of programming errors as programmers often forget the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through.
1770
1771\item
1772It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch© statement:
1773\begin{cfa}
1774switch ( i ) {
1775  case 0:
1776        if ( j < k ) {
1777                ...
1778          ®case 1:®             // transfer into "if" statement
1779                ...
1780        } // if
1781  case 2:
1782        while ( j < 5 ) {
1783                ...
1784          ®case 3:®             // transfer into "while" statement
1785                ...
1786        } // while
1787} // switch
1788\end{cfa}
1789The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties.
1790The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it.
1791The technical problem results from the inability to ensure allocation and initialization of variables when blocks are not entered at the beginning.
1792Often transferring into a block can bypass variable declaration and/or its initialization, which results in subsequent errors.
1793There are virtually no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it.
1794Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}:
1795\begin{cfa}
1796register int n = (count + 7) / 8;
1797switch ( count % 8 ) {
1798case 0: do{ *to = *from++;
1799case 7:         *to = *from++;
1800case 6:         *to = *from++;
1801case 5:         *to = *from++;
1802case 4:         *to = *from++;
1803case 3:         *to = *from++;
1804case 2:         *to = *from++;
1805case 1:         *to = *from++;
1806                } while ( --n > 0 );
1807}
1808\end{cfa}
1809which unrolls a loop N times (N = 8 above) and uses the ©switch© statement to deal with any iterations not a multiple of N.
1810While efficient, this sort of special purpose usage is questionable:
1811\begin{quote}
1812Disgusting, no? But it compiles and runs just fine. I feel a combination of pride and revulsion at this
1813discovery.~\cite{Duff83}
1814\end{quote}
1815\item
1816It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch© statement, rather than only at the end.
1817Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list.
1818The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected;
1819hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics.
1820This physical placement can be compared to the physical placement of an ©else© clause at the end of a series of connected ©if©/©else© statements.
1821
1822\item
1823It is possible to place unreachable code at the start of a ©switch© statement, as in:
1824\begin{cfa}
1825switch ( x ) {
1826        ®int y = 1;®                            §\C{// unreachable initialization}§
1827        ®x = 7;®                                        §\C{// unreachable code without label/branch}§
1828  case 3: ...
1829        ...
1830        ®int z = 0;®                            §\C{// unreachable initialization, cannot appear after case}§
1831        z = 2;
1832  case 3:
1833        ®x = z;®                                        §\C{// without fall through, z is uninitialized}§
1834}
1835\end{cfa}
1836While the declaration of the local variable ©y© is useful with a scope across all ©case© clauses, the initialization for such a variable is defined to never be executed because control always transfers over it.
1837Furthermore, any statements before the first ©case© clause can only be executed if labelled and transferred to using a ©goto©, either from outside or inside of the ©switch©, both of which are problematic.
1838As well, the declaration of ©z© cannot occur after the ©case© because a label can only be attached to a statement, and without a fall through to case 3, ©z© is uninitialized.
1839The key observation is that the ©switch© statement branches into control structure, i.e., there are multiple entry points into its statement body.
1840\end{enumerate}
1841
1842Before discussing potential language changes to deal with these problems, it is worth observing that in a typical C program:
1843\begin{itemize}
1844\item
1845the number of ©switch© statements is small,
1846\item
1847most ©switch© statements are well formed (i.e., no \Index*{Duff's device}),
1848\item
1849the ©default© clause is usually written as the last case-clause,
1850\item
1851and there is only a medium amount of fall-through from one ©case© clause to the next, and most of these result from a list of case values executing common code, rather than a sequence of case actions that compound.
1852\end{itemize}
1853These observations help to put the \CFA changes to the ©switch© into perspective.
1854\begin{enumerate}
1855\item
1856Eliminating default fall-through has the greatest potential for affecting existing code.
1857However, even if fall-through is removed, most ©switch© statements would continue to work because of the explicit transfers already present at the end of each ©case© clause, the common placement of the ©default© clause at the end of the case list, and the most common use of fall-through, i.e., a list of ©case© clauses executing common code, \eg:
1858\begin{cfa}
1859case 1:  case 2:  case 3: ...
1860\end{cfa}
1861still works.
1862Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing shell arguments.
1863Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called ©choose©, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword ©fallthrough©/©fallthru©, e.g.:
1864\begin{cfa}
1865®choose® ( i ) {
1866  case 1:  case 2:  case 3:
1867        ...
1868        ®// implicit end of switch (break)
1869  ®case 5:
1870        ...
1871        ®fallthru®;                                     §\C{// explicit fall through}§
1872  case 7:
1873        ...
1874        ®break®                                         §\C{// explicit end of switch}§
1875  default:
1876        j = 3;
1877}
1878\end{cfa}
1879Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses;
1880the implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause.
1881The explicit ©fallthru© is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the ©choose© statement.
1882As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers.
1883\item
1884\Index*{Duff's device} is eliminated from both ©switch© and ©choose© statements, and only invalidates a small amount of very questionable code.
1885Hence, the ©case© clause must appear at the same nesting level as the ©switch©/©choose© body, as is done in most other programming languages with ©switch© statements.
1886\item
1887The issue of ©default© at locations other than at the end of the cause clause can be solved by using good programming style, and there are a few reasonable situations involving fall-through where the ©default© clause needs to appear is locations other than at the end.
1888Therefore, no change is made for this issue.
1889\item
1890Dealing with unreachable code in a ©switch©/©choose© body is solved by restricting declarations and associated initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate ©case© clause\footnote{
1891Essentially, these declarations are hoisted before the ©switch©/©choose© statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first ©case© clause.
1892Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound.
1893\begin{cfa}
1894switch ( x ) {
1895        ®int i = 0;®                            §\C{// allowed only at start}§
1896  case 0:
1897        ...
1898        ®int j = 0;®                            §\C{// disallowed}§
1899  case 1:
1900    {
1901                ®int k = 0;®                    §\C{// allowed at different nesting levels}§
1902                ...
1903        }
1904  ...
1905}
1906\end{cfa}
1907\end{enumerate}
1908
1909
1910\section{Case Clause}
1911
1912C restricts the ©case© clause of a ©switch© statement to a single value.
1913For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values.
1914Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C.
1915Therefore, the ©case© clause is extended with a list of values, as in:
1916\begin{quote2}
1917\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
1918\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\
1919\begin{cfa}
1920switch ( i ) {
1921  case ®1, 3, 5®:
1922        ...
1923  case ®2, 4, 6®:
1924        ...
1925}
1926\end{cfa}
1927&
1928\begin{cfa}
1929switch ( i ) {
1930  case 1: case 3 : case 5:
1931        ...
1932  case 2: case 4 : case 6:
1933        ...
1934}
1935\end{cfa}
1936&
1937\begin{cfa}
1938
1939// odd values
1940
1941// even values
1942
1943
1944\end{cfa}
1945\end{tabular}
1946\end{quote2}
1947In addition, two forms of subranges are allowed to specify case values: a new \CFA form and an existing GNU C form.\footnote{
1948The GNU C form \emph{requires} spaces around the ellipse.}
1949\begin{quote2}
1950\begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}}
1951\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{GNU C}}     \\
1952\begin{cfa}
1953switch ( i ) {
1954  case ®1~5:®
1955        ...
1956  case ®10~15:®
1957        ...
1958}
1959\end{cfa}
1960&
1961\begin{cfa}
1962switch ( i )
1963  case ®1 ... 5®:
1964        ...
1965  case ®10 ... 15®:
1966        ...
1967}
1968\end{cfa}
1969&
1970\begin{cfa}
1971
1972// 1, 2, 3, 4, 5
1973
1974// 10, 11, 12, 13, 14, 15
1975
1976
1977\end{cfa}
1978\end{tabular}
1979\end{quote2}
1980Lists of subranges are also allowed.
1981\begin{cfa}
1982case ®1~5, 12~21, 35~42®:
1983\end{cfa}
1984
1985
1986\section{Exception Handling}
1987
1988Exception handling provides two mechanism: change of control flow from a raise to a handler, and communication from the raise to the handler.
1989\begin{cfa}
1990exception void h( int i );
1991exception int h( int i, double d );
1992
1993void f(...) {
1994        ... throw h( 3 );
1995        ... i = resume h( 3, 5.1 );
1996}
1997
1998try {
1999        f(...);
2000} catch h( int w ) {
2001        // reset
2002} resume h( int p, double x ) {
2003        return 17;  // recover
2004} finally {
2005}
2006\end{cfa}
2007So the type raised would be the mangled name of the exception prototype and that name would be matched at the handler clauses by comparing the strings.
2008The arguments for the call would have to be packed in a message and unpacked at handler clause and then a call made to the handler.
2009
2010
2011\section{Types}
2012
2013\subsection{Type Definitions}
2014
2015\CFA allows users to define new types using the keyword type.
2016
2017\begin{cfa}
2018// SensorValue is a distinct type and represented as an int
2019type SensorValue = int;
2020\end{cfa}
2021
2022A type definition is different from a typedef in C because a typedef just creates an alias for a type,  while Do.s type definition creates a distinct type.
2023This means that users can define distinct function overloads for the new type (see Overloading for more information).
2024For example:
2025
2026\begin{cfa}
2027type SensorValue = int;
2028void printValue(int v) {...}
2029void printValue(SensorValue v) {...}
2030void process(int v) {...}
2031
2032SensorValue s = ...;
2033
2034printValue(s); // calls version with SensorValue argument
2035
2036printValue((int) s); // calls version with int argument
2037
2038process(s); // implicit conversion to int
2039\end{cfa}
2040
2041If SensorValue was defined with a typedef, then these two print functions would not have unique signatures.
2042This can be very useful to create a distinct type that has the same representation as another type.
2043
2044The compiler will assume it can safely convert from the old type to the new type, implicitly.
2045Users may override this and define a function that must be called to convert from one type to another.
2046
2047\begin{cfa}
2048type SensorValue = int;
2049// ()? is the overloaded conversion operator identifier
2050// This function converts an int to a SensorValue
2051SensorValue ()?(int val) {
2052        ...
2053}
2054void process(int v) {...}
2055
2056SensorValue s = ...;
2057process(s); // implicit call to conversion operator
2058\end{cfa}
2059
2060In many cases, it is not desired for the compiler to do this implicit conversion.
2061To avoid that, the user can use the explicit modifier on the conversion operator.
2062Any places where the conversion is needed but not explicit (with a cast), will result in a compile-time error.
2063
2064\begin{cfa}
2065type SensorValue = int;
2066
2067// conversion from int to SensorValue; must be explicit
2068explicit SensorValue ()?(int val) {
2069        ...
2070}
2071
2072void process(int v) {...}
2073
2074SensorValue s = ...;
2075process(s); // implicit cast to int: compile-time error
2076process((int) s); // explicit cast to int: calls conversion func
2077\end{cfa}
2078
2079The conversion may not require any code, but still need to be explicit; in that case, the syntax can be simplified to:
2080\begin{cfa}
2081type SensorValue = int;
2082explicit SensorValue ()?(int);
2083void process(int v) {...}
2084
2085SensorValue s = ...;
2086process(s); // compile-time error
2087process((int) s); // type is converted, no function is called
2088\end{cfa}
2089
2090
2091\subsection{Structures}
2092
2093Structures in \CFA are basically the same as structures in C.
2094A structure is defined with the same syntax as in C.
2095When referring to a structure in \CFA, users may omit the struct keyword.
2096\begin{cfa}
2097struct Point {
2098        double x;
2099        double y;
2100};
2101
2102Point p = {0.0, 0.0};
2103\end{cfa}
2104
2105\CFA does not support inheritance among types, but instead uses composition to enable reuse of structure fields.
2106Composition is achieved by embedding one type into another.
2107When type A is embedded in type B, an object with type B may be used as an object of type A, and the fields of type A are directly accessible.
2108Embedding types is achieved using anonymous members.
2109For example, using Point from above:
2110\begin{cfa}
2111void foo(Point p);
2112
2113struct ColoredPoint {
2114        Point; // anonymous member (no identifier)
2115        int Color;
2116};
2117...
2118        ColoredPoint cp = ...;
2119        cp.x = 10.3; // x from Point is accessed directly
2120        cp.color = 0x33aaff; // color is accessed normally
2121        foo(cp); // cp can be used directly as a Point
2122\end{cfa}
2123
2124
2125\subsection{Constructors and Destructors}
2126
2127\CFA supports C initialization of structures, but it also adds constructors for more advanced initialization.
2128Additionally, \CFA adds destructors that are called when a variable is de-allocated (variable goes out of scope or object is deleted).
2129These functions take a reference to the structure as a parameter (see References for more information).
2130
2131\begin{figure}
2132\begin{cfa}
2133struct Widget {
2134        int id;
2135        float size;
2136        Parts *optionalParts;
2137};
2138
2139// ?{} is the constructor operator identifier
2140// The first argument is a reference to the type to initialize
2141// Subsequent arguments can be specified for initialization
2142
2143void ?{}(Widget &w) { // default constructor
2144        w.id = -1;
2145        w.size = 0.0;
2146        w.optionalParts = 0;
2147}
2148
2149// constructor with values (does not need to include all fields)
2150void ?{}(Widget &w, int id, float size) {
2151        w.id = id;
2152        w.size = size;
2153        w.optionalParts = 0;
2154}
2155
2156// ^? is the destructor operator identifier
2157void ^?(Widget &w) { // destructor
2158        w.id = 0;
2159        w.size = 0.0;
2160        if (w.optionalParts != 0) {
2161        // This is the only pointer to optionalParts, free it
2162        free(w.optionalParts);
2163        w.optionalParts = 0;
2164        }
2165}
2166
2167Widget baz; // reserve space only
2168Widget foo{}; // calls default constructor
2169Widget bar{23, 2.45}; // calls constructor with values
2170baz{24, 0.91}; // calls constructor with values
2171?{}(baz, 24, 0.91}; // explicit call to constructor
2172^bar; // explicit call to destructor
2173^?(bar); // explicit call to destructor
2174\end{cfa}
2175\caption{Constructors and Destructors}
2176\end{figure}
2177
2178
2179\begin{comment}
2180\section{References}
2181
2182
2183By introducing references in parameter types, users are given an easy way to pass a value by reference, without the need for NULL pointer checks.
2184In structures, a reference can replace a pointer to an object that should always have a valid value.
2185When a structure contains a reference, all of its constructors must initialize the reference and all instances of this structure must initialize it upon definition.
2186
2187The syntax for using references in \CFA is the same as \CC with the exception of reference initialization.
2188Use ©&© to specify a reference, and access references just like regular objects, not like pointers (use dot notation to access fields).
2189When initializing a reference, \CFA uses a different syntax which differentiates reference initialization from assignment to a reference.
2190The ©&© is used on both sides of the expression to clarify that the address of the reference is being set to the address of the variable to which it refers.
2191\end{comment}
2192
2193
2194\section{Overloading}
2195
2196Overloading refers to the capability of a programmer to define and use multiple objects in a program with the same name.
2197In \CFA, a declaration may overload declarations from outer scopes with the same name, instead of hiding them as is the case in C.
2198This may cause identical C and \CFA programs to behave differently.
2199The compiler selects the appropriate object (overload resolution) based on context information at the place where it is used.
2200Overloading allows programmers to give functions with different signatures but similar semantics the same name, simplifying the interface to users.
2201Disadvantages of overloading are that it can be used to give functions with different semantics the same name, causing confusion, or that the compiler may resolve to a different function from what the programmer expected.
2202\CFA allows overloading of functions, operators, variables, and even the constants 0 and 1.
2203
2204The compiler follows some overload resolution rules to determine the best interpretation of all of these overloads.
2205The best valid interpretations are the valid interpretations that use the fewest unsafe conversions.
2206Of these, the best are those where the functions and objects involved are the least polymorphic.
2207Of these, the best have the lowest total conversion cost, including all implicit conversions in the argument expressions.
2208Of these, the best have the highest total conversion cost for the implicit conversions (if any) applied to the argument expressions.
2209If there is no single best valid interpretation, or if the best valid interpretation is ambiguous, then the resulting interpretation is ambiguous.
2210For details about type inference and overload resolution, please see the \CFA Language Specification.
2211\begin{cfa}
2212int foo(int a, int b) {
2213        float sum = 0.0;
2214        float special = 1.0;
2215        {
2216                int sum = 0;
2217                // both the float and int versions of sum are available
2218                float special = 4.0;
2219                // this inner special hides the outer version
2220                ...
2221        }
2222        ...
2223}
2224\end{cfa}
2225
2226
2227\subsection{Overloaded Constant}
2228
2229The constants 0 and 1 have special meaning.
2230In \CFA, as in C, all scalar types can be incremented and
2231decremented, which is defined in terms of adding or subtracting 1.
2232The operations ©&&©, ©||©, and ©!© can be applied to any scalar arguments and are defined in terms of comparison against 0 (ex. ©(a && b)© becomes ©(a != 0 && b != 0)©).
2233
2234In C, the integer constants 0 and 1 suffice because the integer promotion rules can convert them to any arithmetic type, and the rules for pointer expressions treat constant expressions evaluating to 0 as a special case.
2235However, user-defined arithmetic types often need the equivalent of a 1 or 0 for their functions or operators, polymorphic functions often need 0 and 1 constants of a type matching their polymorphic parameters, and user-defined pointer-like types may need a null value.
2236Defining special constants for a user-defined type is more efficient than defining a conversion to the type from ©_Bool©.
2237
2238Why just 0 and 1? Why not other integers? No other integers have special status in C.
2239A facility that let programmers declare specific constants..const Rational 12., for instance. would not be much of an improvement.
2240Some facility for defining the creation of values of programmer-defined types from arbitrary integer tokens would be needed.
2241The complexity of such a feature does not seem worth the gain.
2242
2243For example, to define the constants for a complex type, the programmer would define the following:
2244
2245\begin{cfa}
2246struct Complex {
2247        double real;
2248        double imaginary;
2249}
2250
2251const Complex 0 = {0, 0};
2252const Complex 1 = {1, 0};
2253...
2254
2255        Complex a = 0;
2256...
2257
2258        a++;
2259...
2260        if (a) { // same as if (a == 0)
2261...
2262}
2263\end{cfa}
2264
2265
2266\subsection{Variable Overloading}
2267
2268The overload rules of \CFA allow a programmer to define multiple variables with the same name, but different types.
2269Allowing overloading of variable names enables programmers to use the same name across multiple types, simplifying naming conventions and is compatible with the other overloading that is allowed.
2270For example, a developer may want to do the following:
2271\begin{cfa}
2272int pi = 3;
2273float pi = 3.14;
2274char pi = .p.;
2275\end{cfa}
2276
2277
2278\subsection{Function Overloading}
2279
2280Overloaded functions in \CFA are resolved based on the number and type of arguments, type of return value, and the level of specialization required (specialized functions are preferred over generic).
2281
2282The examples below give some basic intuition about how the resolution works.
2283\begin{cfa}
2284// Choose the one with less conversions
2285int doSomething(int value) {...} // option 1
2286int doSomething(short value) {...} // option 2
2287
2288int a, b = 4;
2289short c = 2;
2290
2291a = doSomething(b); // chooses option 1
2292a = doSomething(c); // chooses option 2
2293
2294// Choose the specialized version over the generic
2295
2296generic(type T)
2297T bar(T rhs, T lhs) {...} // option 3
2298float bar(float rhs, float lhs){...} // option 4
2299float a, b, c;
2300double d, e, f;
2301c = bar(a, b); // chooses option 4
2302
2303// specialization is preferred over unsafe conversions
2304
2305f = bar(d, e); // chooses option 5
2306\end{cfa}
2307
2308
2309\subsection{Operator Overloading}
2310
2311\CFA also allows operators to be overloaded, to simplify the use of user-defined types.
2312Overloading the operators allows the users to use the same syntax for their custom types that they use for built-in types, increasing readability and improving productivity.
2313\CFA uses the following special identifiers to name overloaded operators:
2314
2315\begin{table}[hbt]
2316\hfil
2317\begin{tabular}[t]{ll}
2318%identifier & operation \\ \hline
2319©?[?]© & subscripting \impl{?[?]}\\
2320©?()© & function call \impl{?()}\\
2321©?++© & postfix increment \impl{?++}\\
2322©?--© & postfix decrement \impl{?--}\\
2323©++?© & prefix increment \impl{++?}\\
2324©--?© & prefix decrement \impl{--?}\\
2325©*?© & dereference \impl{*?}\\
2326©+?© & unary plus \impl{+?}\\
2327©-?© & arithmetic negation \impl{-?}\\
2328©~?© & bitwise negation \impl{~?}\\
2329©!?© & logical complement \impl{"!?}\\
2330©?*?© & multiplication \impl{?*?}\\
2331©?/?© & division \impl{?/?}\\
2332\end{tabular}\hfil
2333\begin{tabular}[t]{ll}
2334%identifier & operation \\ \hline
2335©?%?© & remainder \impl{?%?}\\
2336©?+?© & addition \impl{?+?}\\
2337©?-?© & subtraction \impl{?-?}\\
2338©?<<?© & left shift \impl{?<<?}\\
2339©?>>?© & right shift \impl{?>>?}\\
2340©?<?© & less than \impl{?<?}\\
2341©?<=?© & less than or equal \impl{?<=?}\\
2342©?>=?© & greater than or equal \impl{?>=?}\\
2343©?>?© & greater than \impl{?>?}\\
2344©?==?© & equality \impl{?==?}\\
2345©?!=?© & inequality \impl{?"!=?}\\
2346©?&& bitwise AND \impl{?&?}\\
2347\end{tabular}\hfil
2348\begin{tabular}[t]{ll}
2349%identifier & operation \\ \hline
2350©?^& exclusive OR \impl{?^?}\\
2351©?|?© & inclusive OR \impl{?"|?}\\
2352©?=?© & simple assignment \impl{?=?}\\
2353©?*=?© & multiplication assignment \impl{?*=?}\\
2354©?/=?© & division assignment \impl{?/=?}\\
2355©?%=?© & remainder assignment \impl{?%=?}\\
2356©?+=?© & addition assignment \impl{?+=?}\\
2357©?-=?© & subtraction assignment \impl{?-=?}\\
2358©?<<=?© & left-shift assignment \impl{?<<=?}\\
2359©?>>=?© & right-shift assignment \impl{?>>=?}\\
2360©?&=?© & bitwise AND assignment \impl{?&=?}\\
2361©?^=?© & exclusive OR assignment \impl{?^=?}\\
2362©?|=?© & inclusive OR assignment \impl{?"|=?}\\
2363\end{tabular}
2364\hfil
2365\caption{Operator Identifiers}
2366\label{opids}
2367\end{table}
2368
2369These identifiers are defined such that the question marks in the name identify the location of the operands.
2370These operands represent the parameters to the functions, and define how the operands are mapped to the function call.
2371For example, ©a + b© becomes ©?+?(a, b)©.
2372
2373In the example below, a new type, myComplex, is defined with an overloaded constructor, + operator, and string operator.
2374These operators are called using the normal C syntax.
2375
2376\begin{cfa}
2377type Complex = struct { // define a Complex type
2378        double real;
2379        double imag;
2380}
2381
2382// Constructor with default values
2383
2384void ?{}(Complex &c, double real = 0.0, double imag = 0.0) {
2385        c.real = real;
2386        c.imag = imag;
2387}
2388
2389Complex ?+?(Complex lhs, Complex rhs) {
2390        Complex sum;
2391        sum.real = lhs.real + rhs.real;
2392        sum.imag = lhs.imag + rhs.imag;
2393        return sum;
2394}
2395
2396String ()?(const Complex c) {
2397        // use the string conversions for the structure members
2398        return (String)c.real + . + . + (String)c.imag + .i.;
2399}
2400...
2401
2402Complex a, b, c = {1.0}; // constructor for c w/ default imag
2403...
2404c = a + b;
2405print(.sum = . + c);
2406\end{cfa}
2407
2408
2409\section{Auto Type-Inferencing}
2410
2411Auto type-inferencing occurs in a declaration where a variable's type is inferred from its initialization expression type.
2412\begin{quote2}
2413\begin{tabular}{@{}l@{\hspace{3em}}ll@{}}
2414\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CC}} & \multicolumn{1}{c}{Indexc{gcc}} \\
2415\begin{cfa}
2416
2417auto j = 3.0 * 4;
2418int i;
2419auto k = i;
2420\end{cfa}
2421&
2422\begin{cfa}
2423#define expr 3.0 * i
2424typeof(expr) j = expr;
2425int i;
2426typeof(i) k = i;
2427\end{cfa}
2428&
2429\begin{cfa}
2430
2431// use type of initialization expression
2432
2433// use type of primary variable
2434\end{cfa}
2435\end{tabular}
2436\end{quote2}
2437The two important capabilities are:
2438\begin{itemize}
2439\item
2440preventing having to determine or write out long generic types,
2441\item
2442ensure secondary variables, related to a primary variable, always have the same type.
2443\end{itemize}
2444
2445In \CFA, ©typedef© provides a mechanism to alias long type names with short ones, both globally and locally, but not eliminate the use of the short name.
2446\Indexc{gcc} provides ©typeof© to declare a secondary variable from a primary variable.
2447\CFA also relies heavily on the specification of the left-hand side of assignment for type inferencing, so in many cases it is crucial to specify the type of the left-hand side to select the correct type of the right-hand expression.
2448Only for overloaded routines with the same return type is variable type-inferencing possible.
2449Finally, ©auto© presents the programming problem of tracking down a type when the type is actually needed.
2450For example, given
2451\begin{cfa}
2452auto j = ®...®
2453\end{cfa}
2454and the need to write a routine to compute using ©j©
2455\begin{cfa}
2456void rtn( ®...® parm );
2457rtn( j );
2458\end{cfa}
2459A programmer must work backwards to determine the type of ©j©'s initialization expression, reconstructing the possibly long generic type-name.
2460In this situation, having the type name or a short alias is very useful.
2461
2462There is also the conundrum in type inferencing of when to \emph{\Index{brand}} a type.
2463That is, when is the type of the variable more important than the type of its initialization expression.
2464For example, if a change is made in an initialization expression, it can cause hundreds or thousands of cascading type changes and/or errors.
2465At some point, a programmer wants the type of the variable to remain constant and the expression to be in error when it changes.
2466
2467Given ©typedef© and ©typeof© in \CFA, and the strong need to use the type of left-hand side in inferencing, auto type-inferencing is not supported at this time.
2468Should a significant need arise, this feature can be revisited.
2469
2470
2471\section{Generics}
2472
2473\CFA supports parametric polymorphism to allow users to define generic functions and types.
2474Generics allow programmers to use type variables in place of concrete types so that the code can be reused with multiple types.
2475The type parameters can be restricted to satisfy a set of constraints.
2476This enables \CFA to build fully compiled generic functions and types, unlike other languages like \Index*[C++]{\CC} where templates are expanded or must be explicitly instantiated.
2477
2478
2479\subsection{Generic Functions}
2480
2481Generic functions in \CFA are similar to template functions in \Index*[C++]{\CC}, and will sometimes be expanded into specialized versions, just like in \CC.
2482The difference, however, is that generic functions in \CFA can also be separately compiled, using function pointers for callers to pass in all needed functionality for the given type.
2483This means that compiled libraries can contain generic functions that can be used by programs linked with them (statically or dynamically).
2484Another advantage over \CC templates is unlike templates, generic functions are statically checked, even without being instantiated.
2485
2486A simple example of using Do.s parametric polymorphism to create a generic swap function would look like this:
2487
2488\begin{cfa}
2489generic(type T)
2490void swap(T &a, T &b) {
2491        T tmp = a;
2492        a = b;
2493        b = a;
2494}
2495
2496int a, b;
2497swap(a, b);
2498
2499Point p1, p2;
2500swap(p1, p2);
2501\end{cfa}
2502
2503Here, instead of specifying types for the parameters a and b, the function has a generic type parameter, type T.
2504This function can be called with any type, and the compiler will handle generating the proper code for that type, using call site inference to determine the appropriate value for T.
2505
2506
2507\subsection{Bounded Quantification}
2508
2509Some generic functions only work (or make sense) for any type that satisfies a given property.
2510For example, here is a function to pick the minimum of two values of some type.
2511\begin{cfa}
2512generic (type T | bool ?<?(T, T) )
2513
2514T min(T a, T b) {
2515        return a < b ? a : b;
2516}
2517\end{cfa}
2518
2519It only makes sense to call min with values of a type that has an ordering: a way to decide whether one value is less than another.
2520The ordering function used here is the less than operator, <.
2521The syntax used to reference the operator is discussed in further detail in Operator Overloading.
2522In \CFA, this assertion on the type of a generic is written as the bound, (type T | bool ?<?(T, T)).
2523The \CFA compiler enforces that minis only called with types for which the less than operator is defined, and reports a compile-time error otherwise.
2524
2525Bounds can also involve multiple types, and multiple requirements, as shown below:
2526\begin{cfa}
2527generic (type T, type U | { T foo(T, U); U bar(U); })
2528
2529T baz(T t, U u) {
2530        return foo(t, bar(u));
2531}
2532\end{cfa}
2533
2534
2535\subsection{Interfaces}
2536
2537Type bounds as shown above are not very informative, merely requiring that a function exists with the right name and type.
2538Suppose you try to call a polymorphic function and \CFA gives you an error that int combine(int, int) is not defined.
2539Can you define it? What is it supposed to do? Perhaps it should compute the sum, or the bitwise and, or the maximum, or the least common multiple; or perhaps it's an operation that can't be defined for integers.
2540The function signature doesn't say.
2541
2542Interfaces gather together a set of function signatures under a common name, which solves two problems.
2543First, an interface name can be used in type bounds instead of function signatures.
2544This avoids repetition when a bound is used in many functions.
2545Second, interfaces explicitly document the existence of a commonly used set of functionality, making programs easier to understand.
2546\begin{cfa}
2547generic (type T)
2548interface Orderable {
2549        bool ?<?(T, T);
2550};
2551
2552generic (type T | Orderable(T))
2553T min(T a, T b) {
2554        return a < b ? a : b;
2555}
2556\end{cfa}
2557
2558This definition of the interface Orderable makes the generic function min easier to read and understand.
2559Orderable can also be reused for other generic functions, max for example.
2560Interfaces can also build on top of other interfaces.
2561For example:
2562\begin{cfa}
2563generic (type T | Orderable(T)
2564interface FooBarable {
2565        int foo(T, T);
2566        int Bar(T, T);
2567};
2568\end{cfa}
2569
2570The FooBarable interface specifies all of the bounds of the Orderable interface, plus the additional bounds specified in its definition.
2571A type does not need to specify that it satisfies any interface, the compiler can figure this out at compile time.
2572For example, there is no need to add some special syntax to show that a type implements the Orderable interface, just define a ?<? operator and it is satisfied.
2573
2574
2575\subsection{Generic Typedefs}
2576
2577Type synonyms can be defined generically using the typedef keyword together with a generic type annotation.
2578These can be used to abbreviate complicated type expressions, especially in generic code.
2579\begin{cfa}
2580// typedef the generic function pointers for later use
2581
2582generic(type T)
2583typedef int (*predicate)(T);
2584generic(type Captured, type T)
2585typedef void (*callback)(Captured, T);
2586
2587generic(type T)
2588void find(int length, T *array,
2589        predicate(T) p, callback(void *, T)f) {
2590        int i;
2591        for (i = 0; i < length; i++)
2592        if (p(array[i])) f(NULL, array[i]);
2593}
2594\end{cfa}
2595
2596
2597\subsection{Generic Types}
2598
2599Generic types are defined using the same mechanisms as those described above for generic functions.
2600This feature allows users to create types that have one or more fields that use generic parameters as types, similar to a template classes in \Index*[C++]{\CC}.
2601For example, to make a generic linked list, a placeholder is created for the type of the elements, so that the specific type of the elements in the list need not be specified when defining the list.
2602In C, something like this would have to be done using void pointers and unsafe casting.
2603As in generic functions, Do.s generic types are different from \CC templates in that they can be fully compiled, while \CC templates are more like macro expansions.
2604This means that a \CFA generic type from a compiled library can be used with any type that satisfies the bounds.
2605
2606The syntax for defining a generic type looks very similar to that of a generic function.
2607Generic types support bounds and interfaces, using the same syntax as generic functions.
2608\begin{cfa}
2609generic (type T)
2610struct LinkedListElem {
2611        T elem;
2612        LinkedListElem(T) *next;
2613};
2614
2615LinkedListElem *++?(LinkedListElem **elem) {
2616        return *elem = elem->next;
2617}
2618
2619generic (type T)
2620struct LinkedList {
2621        LinkedListElem(T) *head;
2622        unsigned int size;
2623}
2624
2625generic (type T | bool ?==?(T, T))
2626bool contains(LinkedList(T) *list, T elem) {
2627        for(LinkedListElem *iter = list->head; iter != 0; ++iter) {
2628        if (iter->elem == elem) return true;
2629        }
2630        return false;
2631}
2632\end{cfa}
2633
2634
2635\section{Safety}
2636
2637Safety, along with productivity, is a key goal of Do.
2638This section discusses the safety features that have been included in \CFA to help programmers create more stable, reliable, and secure code.
2639
2640
2641\subsection{Exceptions}
2642
2643\CFA introduces support for exceptions as an easier way to recover from exceptional conditions that may be detected within a block of code.
2644In C, developers can use error codes and special return values to report to a caller that an error occurred in a function.
2645The major problem with error codes is that they can be easily ignored by the caller.
2646Failure to properly check for errors can result in the caller making incorrect assumptions about the current state or about the return value that are very likely to result in errors later on in the program, making the source of the problem more difficult to find when debugging.
2647An unhandled exception on the other hand will cause a crash, revealing the original source of the erroneous state.
2648
2649Exceptions in \CFA allow a different type of control flow.
2650Throwing an exception terminates execution of the current block, invokes the destructors of variables that are local to the block, and propagates the exception to the parent block.
2651The exception is immediately re-thrown from the parent block unless it is caught as described below.
2652\CFA uses keywords similar to \Index*[C++]{\CC} for exception handling.
2653An exception is thrown using a throw statement, which accepts one argument.
2654
2655\begin{cfa}
2656        ...
2657
2658        throw 13;
2659
2660        ...
2661\end{cfa}
2662
2663An exception can be caught using a catch statement, which specifies the type of the exception it can catch.
2664A catch is specified immediately after a guarded block to signify that it can catch an exception from that block.
2665A guarded block is specified using the try keyword, followed by a block of code inside of curly braces.
2666
2667\begin{cfa}
2668        ...
2669
2670        try {
2671                throw 13;
2672        }
2673        catch(int e) {
2674                printf(.caught an exception: %d\n., e);
2675        }
2676\end{cfa}
2677
2678
2679\subsection{Memory Management}
2680
2681
2682\subsubsection{Manual Memory Management}
2683
2684Using malloc and free to dynamically allocate memory exposes several potential, and common, errors.
2685First, malloc breaks type safety because it returns a pointer to void.
2686There is no relationship between the type that the returned pointer is cast to, and the amount of memory allocated.
2687This problem is solved with a type-safe malloc.
2688Do.s type-safe malloc does not take any arguments for size.
2689Instead, it infers the type based on the return value, and then allocates space for the inferred type.
2690
2691\begin{cfa}
2692float *f = malloc(); // allocates the size of a float
2693
2694struct S {
2695        int i, j, k;
2696};
2697
2698struct S *s = malloc(); // allocates the size of a struct S
2699\end{cfa}
2700
2701In addition to the improved malloc, \CFA also provides a technique for combining allocation and initialization into one step, using the new function.
2702For all constructors defined for a given type (see Operator Overloading), a corresponding call to new can be used to allocate and construct that type.
2703
2704\begin{cfa}
2705type Complex = struct {
2706        float real;
2707        float imag;
2708};
2709
2710// default constructor
2711
2712void ?{}(Complex &c) {
2713        c.real = 0.0;
2714        c.imag = 0.0;
2715}
2716
2717
2718
2719// 2 parameter constructor
2720
2721void ?{}(Complex &c, float real, float imag) {
2722        c.real = real;
2723        c.imag = imag;
2724}
2725
2726
2727int main() {
2728        Complex c1; // No constructor is called
2729        Complex c2{}; // Default constructor called
2730        Complex c3{1.0, -1.0}; // 2 parameter constructor is called
2731
2732        Complex *p1 = malloc(); // allocate
2733        Complex *p2 = new(); // allocate + default constructor
2734        Complex *p3 = new(0.5, 1.0); // allocate + 2 param constructor
2735}
2736
2737\end{cfa}
2738
2739
2740\subsubsection{Automatic Memory Management}
2741
2742\CFA may also support automatic memory management to further improve safety.
2743If the compiler can insert all of the code needed to manage dynamically allocated memory (automatic reference counting), then developers can avoid problems with dangling pointers, double frees, memory leaks, etc.
2744This feature requires further investigation.
2745\CFA will not have a garbage collector, but might use some kind of region-based memory management.
2746
2747
2748\subsection{Unsafe C Constructs}
2749
2750C programmers are able to access all of the low-level tricks that are sometimes needed for close-to-the-hardware programming.
2751Some of these practices however are often error-prone and difficult to read and maintain.
2752Since \CFA is designed to be safer than C, such constructs are disallowed in \CFA code.
2753If a programmer wants to use one of these unsafe C constructs, the unsafe code must be contained in a C linkage block (see Interoperability), which will be compiled like C code.
2754This block means that the user is telling the tools, .I know this is unsafe, but I.m going to do it anyway..
2755
2756The exact set of unsafe C constructs that will be disallowed in \CFA has not yet been decided, but is sure to include pointer arithmetic, pointer casting, etc.
2757Once the full set is decided, the rules will be listed here.
2758
2759
2760\section{Syntactic Anomalies}
2761
2762The number 0 and 1 are treated specially in \CFA, and can be redefined as variables.
2763One syntactic anomaly is when a field in an structure is names 0 or 1:
2764\begin{cfa}
2765struct S {
2766        int 0, 1;
2767} s;
2768\end{cfa}
2769The problem occurs in accessing these fields using the selection operation ``©.©'':
2770\begin{cfa}
2771s.0 = 0;        // ambiguity with floating constant .0
2772s.1 = 1;        // ambiguity with floating constant .1
2773\end{cfa}
2774To make this work, a space is required after the field selection:
2775\begin{cfa}
2776®s.§\textvisiblespace§0® = 0;
2777®s.§\textvisiblespace§1® = 1;
2778\end{cfa}
2779While this syntax is awkward, it is unlikely many programmers will name fields of a structure 0 or 1.
2780Like the \Index*[C++]{\CC} lexical problem with closing template-syntax, e.g, ©Foo<Bar<int®>>®©, this issue can be solved with a more powerful lexer/parser.
2781
2782There are several ambiguous cases with operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be lexed as ©*©/©?*?© or ©*?©/©*?©.
2783Since it is common practise to put a unary operator juxtaposed to an identifier, \eg ©*i©, users will be annoyed if they cannot do this with respect to operator identifiers.
2784Even with this special hack, there are 5 general cases that cannot be handled.
2785The first case is for the function-call identifier ©?()©:
2786\begin{cfa}
2787int *§\textvisiblespace§?()();  // declaration: space required after '*'
2788\textvisiblespace§?()();              // expression: space required after '*'
2789\end{cfa}
2790Without the space, the string ©*?()© is ambiguous without N character look ahead;
2791it requires scanning ahead to determine if there is a ©'('©, which is the start of an argument/parameter list.
2792
2793The 4 remaining cases occur in expressions:
2794\begin{cfa}
2795i++§\textvisiblespace§?i:0;             // space required before '?'
2796i--§\textvisiblespace§?i:0;             // space required before '?'
2797\textvisiblespace§?++i:0;             // space required after '?'
2798\textvisiblespace§?--i:0;             // space required after '?'
2799\end{cfa}
2800In the first two cases, the string ©i++?© is ambiguous, where this string can be lexed as ©i© / ©++?© or ©i++© / ©?©;
2801it requires scanning ahead to determine if there is a ©'('©, which is the start of an argument list.
2802In the second two cases, the string ©?++x© is ambiguous, where this string can be lexed as ©?++© / ©x© or ©?© / y©++x©;
2803it requires scanning ahead to determine if there is a ©'('©, which is the start of an argument list.
2804
2805
2806\section{Concurrency}
2807
2808Today's processors for nearly all use cases, ranging from embedded systems to large cloud computing servers, are composed of multiple cores, often heterogeneous.
2809As machines grow in complexity, it becomes more difficult for a program to make the most use of the hardware available.
2810\CFA includes built-in concurrency features to enable high performance and improve programmer productivity on these multi-/many-core machines.
2811
2812Concurrency support in \CFA is implemented on top of a highly efficient runtime system of light-weight, M:N, user level threads.
2813The model integrates concurrency features into the language by making the structure type the core unit of concurrency.
2814All communication occurs through method calls, where data is sent via method arguments, and received via the return value.
2815This enables a very familiar interface to all programmers, even those with no parallel programming experience.
2816It also allows the compiler to do static type checking of all communication, a very important safety feature.
2817This controlled communication with type safety has some similarities with channels in \Index*{Go}, and can actually implement
2818channels exactly, as well as create additional communication patterns that channels cannot.
2819Mutex objects, monitors, are used to contain mutual exclusion within an object and synchronization across concurrent threads.
2820
2821Three new keywords are added to support these features:
2822
2823monitor creates a structure with implicit locking when accessing fields
2824
2825mutex implies use of a monitor requiring the implicit locking
2826
2827task creates a type with implicit locking, separate stack, and a thread
2828
2829
2830\subsection{Monitors}
2831
2832A monitor is a structure in \CFA which includes implicit locking of its fields.
2833Users of a monitor interact with it just like any structure, but the compiler handles code as needed to ensure mutual exclusion.
2834An example of the definition of a monitor is shown here:
2835\begin{cfa}
2836type Account = monitor {
2837        const unsigned long number; // account number
2838        float balance; // account balance
2839};
2840\end{cfa}
2841
2842Since a monitor structure includes an implicit locking mechanism, it does not make sense to copy a monitor;
2843it is always passed by reference.
2844Users can specify to the compiler whether or not a function will require mutual exclusion of the monitor using the mutex modifier on the parameter.
2845When mutex is specified, the compiler inserts locking before executing the body of the function, and unlocking after the body.
2846This means that a function requiring mutual exclusion could block if the lock is already held by another thread.
2847Blocking on a monitor lock does not block the kernel thread, it simply blocks the user thread, which yields its kernel thread while waiting to obtain the lock.
2848If multiple mutex parameters are specified, they will be locked in parameter order (\ie first parameter is locked first) and unlocked in the
2849reverse order.
2850\begin{cfa}
2851// This function accesses a constant field, it does not require
2852// mutual exclusion
2853
2854export unsigned long getAccountNumber(Account &a) {
2855        return a.number;
2856}
2857
2858// This function accesses and modifies a shared field; it
2859// requires mutual exclusion
2860
2861export float withdrawal(mutex Account &a, float amount) {
2862        a.balance -= amount;
2863        return a.balance;
2864}
2865\end{cfa}
2866
2867Often, one function using a monitor will call another function using that same monitor.
2868If both require mutual exclusion, then the thread would be waiting for itself to release the lock when it calls the inner function.
2869This situation is resolved by allowing recursive entry (reentrant locks), meaning that if the lock is already held by the caller, it can be locked again.
2870It will still be unlocked the same number of times.
2871An example of this situation is shown below:
2872
2873\begin{cfa}
2874// deleting a job from a worker requires mutual exclusion
2875
2876void deleteJob(mutex Worker &w, Job &j) {
2877        ...
2878}
2879
2880// transferring requires mutual exclusion and calls deleteJob
2881
2882void transferJob(mutex Worker &from, Worker &to) {
2883        ...
2884        deleteJob(j);
2885        ...
2886}
2887\end{cfa}
2888
2889
2890\subsection{Tasks}
2891
2892\CFA also provides a simple mechanism for creating and utilizing user level threads.
2893A task provides mutual exclusion like a monitor, and also has its own execution state and a thread of control.
2894Similar to a monitor, a task is defined like a structure:
2895\begin{cfa}
2896type Adder = task {
2897        int *row;
2898        int size;
2899        int &subtotal;
2900}
2901\end{cfa}
2902
2903A task may define a constructor, which will be called upon allocation and run on the caller.s thread.
2904A destructor may also be defined, which is called at de-allocation (when a dynamic object is deleted or when a local object goes out of scope).
2905After a task is allocated and initialized, its thread is spawned implicitly and begins executing in its function call method.
2906All tasks must define this function call method, with a void return value and no additional parameters, or the compiler will report an error.
2907Below are example functions for the above Adder task, and its usage to sum up a matrix on multiple threads.
2908(Note that this example is designed to display the syntax and functionality, not the best method to solve this problem)
2909\begin{cfa}
2910void ?{}(Adder &a, int r[], int s, int &st) { // constructor
2911        a.row = r;
2912        a.size = s;
2913        a.subtotal = st;
2914}
2915
2916// implicitly spawn thread and begin execution here
2917
2918void ?()(Adder &a) {
2919        int c;
2920        subtotal = 0;
2921        for (c=0; c<a.size; ++c) {
2922        subtotal += row[c];
2923        }
2924}
2925
2926int main() {
2927        const int rows = 100, cols = 1000000;
2928        int matrix[rows][cols];
2929        int subtotals[rows];
2930        int total = 0;
2931        int r;
2932
2933        { // create a new scope here for our adders
2934        Adder adders[rows];
2935        // read in the matrix
2936        ...
2937        for (r=0; r<rows; ++r) {
2938        // tasks are initialized on this thread
2939        Adders[r] = {matrix[r], cols, subtotals[r]};
2940        Adders[r](); // spawn thread and begin execution
2941        }
2942        } // adders go out of scope; block here until they all finish
2943        total += subtotals[r];
2944        printf(.total is %d\n., total);
2945}
2946\end{cfa}
2947
2948
2949\subsection{Cooperative Scheduling}
2950
2951Tasks in \CFA are cooperatively scheduled, meaning that a task will not be interrupted by another task, except at specific yield points.
2952In Listing 31, there are no yield points, so each task runs to completion with no interruptions.
2953Places where a task could yield include waiting for a lock (explicitly or implicitly), waiting for I/O, or waiting for a specific function (or one of a set of functions) to be called.
2954This last option is introduced with the yield function. yield is used to indicate that this task should yield its thread until the specified function is called.
2955For example, the code below defines a monitor that maintains a generic list.
2956When a task tries to pop from the list, but it is empty, the task should yield until another task puts something into the list, with the push function.
2957Similarly, when a task tries to push something onto the list, but it is full, it will yield until another task frees some space with the pop function.
2958
2959\begin{cfa}
2960// type T is used as a generic type for all definitions inside
2961// the curly brackets
2962
2963generic(type T) {
2964        type Channel = monitor {
2965        List(T) list; // list is a simple generic list type
2966        };
2967
2968        T pop(mutex &Channel(T) ch) {
2969        if (ch.list.empty()) {
2970        // yield until push is called for this channel
2971        yield(push);
2972        }
2973        return ch.list.pop();
2974        }
2975
2976        void push(mutex &Channel(T)ch, T val) {
2977        if (ch.list.full()) {
2978        // yield until pop is called for this channel
2979        yield(pop);
2980        }
2981        ch.list.push(val);
2982        }
2983}
2984\end{cfa}
2985
2986A task can also yield indefinitely by calling yield with no arguments.
2987This will tell the scheduler to yield this task until it is resumed by some other task.
2988A task can resume another task by using its functional call operator.
2989The code below shows a simple ping-pong example, where two tasks yield back and forth to each other using these methods.
2990
2991\begin{cfa}
2992type Ping = task {
2993        Pong *partner;
2994};
2995
2996void ?{}(Ping &p, Pong *partner = 0) {
2997        p.partner = partner;
2998}
2999
3000void ?()(Ping &p) {
3001        for(;;) { // loop forever
3002        printf(.ping\n.);
3003        partner(); // resumes the partner task
3004        yield(); // yields this task
3005        }
3006}
3007
3008type Pong = task {
3009        Ping *partner;
3010};
3011
3012void ?{}(Pong &p, Ping *partner = 0) {
3013        p.partner = partner;
3014}
3015
3016void ?()(Pong &p) {
3017        for(;;) { // loop forever
3018        yield(); // yields this task
3019        printf(.pong/n.);
3020        partner(); // resumes the partner task
3021        }
3022}
3023
3024void main() {
3025        Ping ping; // allocate ping
3026        Pong pong{ping}; // allocate, initialize, and start pong
3027        Ping{pong}; // initialize and start ping
3028}
3029\end{cfa}
3030
3031The same functionality can be accomplished by providing functions to be called by the partner task.
3032\begin{cfa}
3033type Pingpong = task {
3034        String msg;
3035        Pingpong *partner;
3036};
3037
3038void ?{}(Pingpong &p, String msg, Pingpong *partner = 0) {
3039        p.msg = msg;
3040        p.partner = partner;
3041}
3042
3043void ?()(Pingpong &p) {
3044        for(;;) {
3045        yield(go);
3046        }
3047}
3048
3049void go(Pingpong &p) {
3050        print(.%(p.msg)\n.);
3051        go(p.partner);
3052}
3053
3054void main() {
3055        Pingpong ping = {.ping.};
3056        Pingpong pong = {.pong., ping};
3057        ping.partner = pong;
3058        go(ping);
3059}
3060\end{cfa}
3061
3062
3063\section{Modules and Packages }
3064
3065\begin{comment}
3066High-level encapsulation is useful for organizing code into reusable units, and accelerating compilation speed.
3067\CFA provides a convenient mechanism for creating, building and sharing groups of functionality that enhances productivity and improves compile time.
3068
3069There are two levels of encapsulation in \CFA, module and package.
3070A module is a logical grouping of functionality that can be easily pulled into another project, much like a module in \Index*{Python} or a package in \Index*{Go}.
3071A module forms a namespace to limit the visibility and prevent naming conflicts of variables.
3072Furthermore, a module is an independent translation unit, which can be compiled separately to accelerate the compilation speed.
3073
3074A package is a physical grouping of one or more modules that is used for code distribution and version management.
3075Package is also the level of granularity at which dependences are managed.
3076A package is similar to the Crate in \Index*{Rust}.
3077
3078
3079\subsection{No Declarations, No Header Files}
3080
3081In C and \Index*[C++]{\CC}, it is necessary to declare or define every global variable, global function, and type before it is used in each file.
3082Header files and a preprocessor are normally used to avoid repeating code.
3083Thus, many variables, functions, and types are described twice, which exposes an opportunity for errors and causes additional maintenance work.
3084Instead of following this model, the \CFA tools can extract all of the same information from the code automatically.
3085This information is then stored in the object files for each module, in a format that can quickly be read by the compiler, and stored at the top of the file, for quick access.
3086In addition to the user productivity improvements, this simple change also improves compile time, by saving the information in a simple machine readable format, instead of making the compiler parse the same information over and over from a header file.
3087This seems like a minor change, but according to (Pike, \Index*{Go} at Google: Language Design in the Service of Software Engineering), this simple change can cause massive reductions in compile time.
3088
3089In \CFA, multiple definitions are not necessary.
3090Within a module, all of the module's global definitions are visible throughout the module.
3091For example, the following code compiles, even though ©isOdd© was not declared before being called:
3092\begin{cfa}
3093bool isEven(unsigned int x) {
3094        if (x == 0) return true;
3095        else return !isOdd(x);
3096}
3097
3098bool isOdd(unsigned int x) {
3099        if (x == 1) return true;
3100        else return !isEven(x - 2);
3101}
3102\end{cfa}
3103
3104Header files in C are used to expose the declarations from a library, so that they can be used externally.
3105With \CFA, this functionality is replaced with module exports, discussed below.
3106When building a \CFA module which needs to be callable from C code, users can use the tools to generate a header file suitable for including in these C files with all of the needed declarations.
3107
3108In order to interoperate with existing C code, \CFA files can still include header files, the contents of which will be enclosed in a C linkage section to indicate C calling conventions (see Interoperability for more information).
3109
3110
3111\subsection{Modules}
3112
3113A module typically contains a set of related types and methods, with some objects accessible from outside the package, and some limited to use inside the module.
3114These modules can then be easily shared and reused in multiple projects.
3115As modules are intended to be distributed for reuse, they should generally have stable, well-defined interfaces.
3116
3117\CFA adds the following keywords to express the module systems: module, export, import, as.
3118
3119
3120\subsubsection{Module Declaration}
3121
3122The syntax to declare a module is module moduleName;.
3123
3124The module declaration must be at the beginning of a file, and each file can only belong to one module.
3125If there is no module declaration at the beginning of a file, the file belongs to the global module.
3126A module can span several files.
3127By convention, a module and the files belonging to the module have additional mapping relationship which is described in the Do-Lang Tooling documentation.
3128
3129The moduleName follows the same rules of a variable name, except that it can use slash "/" to indicate the module/sub-module relationship.
3130For example, container/vector is a valid module name, where container is the parent module name, and vector is the sub-module under container.
3131
3132Only the interfaces of a module are visible from outside, when the module is imported. export is a type decorator to declare a module interface.
3133A method, a global variable or a type can be declared as a module interface.
3134Types defined in a module and referenced by an exported function or a variable must be exported, too.
3135
3136The following code is a simple module declaration example.
3137\begin{cfa}
3138module M;
3139
3140//visible outside module M
3141
3142export int f(int i) { return i + 1; }
3143export double aCounter;
3144
3145//not visible outside module M
3146
3147int g(int i) { return i - 1; }
3148
3149double bCounter;
3150\end{cfa}
3151
3152export module moduleName; can be use to re-export all the visible (exported) names in moduleName from the current module.
3153
3154
3155\subsubsection{Module Import}
3156
3157The syntax to import a module is import moduleName; or import moduleName as anotherName;.
3158One package cannot be imported with both of the two types of syntax in one file.
3159A package imported in one file will only be visible in this file.
3160For example, two files, A and B belong to the same module.
3161If file A imports another module, M, the exported names in M are not visible in file B.
3162
3163All of the exported names are visible in the file that imports the module.
3164The exported names can be accessed within a namespace based on the module name in the first syntax (ex moduleName.foo).
3165If moduleName has several elements separated by '/' to describe a sub-module (ex. import container/vector;), the last element in the moduleName is used as the namespace to access the visible names in that module (ex vector.add(...);).
3166The as keyword is used to confine the imported names in a unique namespace (ex. anotherName.foo). anotherName must be a valid identifier (same rules as a variable name) which means it cannot have '/' in it.
3167Conflicts in namespaces will be reported by the compiler.
3168The second method can be used to solve conflicting name problems.
3169The following code snippets show the two situations.
3170
3171\begin{cfa}
3172module util/counter;
3173export int f(int i) { return i+1; }
3174
3175import util/counter;
3176
3177int main() {
3178        return counter.f(200); // f() from the package counter
3179}
3180
3181import util/counter as ct;
3182int main() {
3183        return ct.f(200); // f() from the package counter
3184}
3185\end{cfa}
3186
3187
3188Additionally, using the .as. syntax, a user can force the compiler to add the imported names into the current namespace using .as ..With these module rules, the following module definitions and imports can be achieved without any problem.
3189
3190\begin{cfa}
3191module M1;
3192export int f(int i) { return i+1;} // visible outside
3193
3194int g(int i) { return i-1;} // not visible outside
3195
3196module M2;
3197int f(int i) { return i * 2; } // not visible outside
3198export int g(int g) { return i / 2; } // visible outside
3199
3200import M1 as .;
3201
3202import M2 as .;
3203
3204
3205int main() {
3206        return f(3) + g(4); //f() from M1 and g() from M2;
3207}
3208\end{cfa}
3209
3210
3211\subsubsection{Sub-Module and Module Aggregation}
3212
3213Several modules can be organized in a parent module and sub-modules relationship.
3214The sub-module names are based on hierarchical naming, and use slash, "/", to indicate the relationship.
3215For example, std/vector and std/io are sub-modules of module std.
3216The exported names in a sub-module are NOT visible if the parent module is imported, which means the exported names in the sub-module are
3217not implicitly exported in the parent module.
3218
3219Aggregation is a mechanism to support components and simplified importing.
3220The mechanism is not based on naming but based on manual declaration.
3221For example, the following is the aggregated sequence module.
3222The export {...} is syntactic sugar for many lines of export module aModule;.
3223If an aggregated module is imported, all the included modules in the aggregation are imported.
3224
3225\begin{cfa}
3226module std/sequence;
3227
3228export {
3229        module std/vector;
3230        module std/list;
3231        module std/array;
3232        module std/deque;
3233        module std/forward_list;
3234        module std/queue;
3235        module std/stack;
3236};
3237\end{cfa}
3238
3239After importing the aggregated module, each individual name is still contained in the original name space.
3240For example, vector.add() and list.add() should be used to reference the add methods if there are add methods in both the vector module and the list module.
3241
3242
3243\subsubsection{Import from Repository}
3244
3245When a module is imported, the tools locate the module in the one of the accessible package paths (defined by command line flag or environment variable).
3246The tools also support retrieving modules of a package from external repositories.
3247See Listing 40: Package directory structure
3248
3249
3250\subsubsection{Package Import}
3251
3252Because packages are the places where the building tool looks for modules, there is no code required in the \CFA source file to import a package.
3253In order to use modules in a package, the programmer needs to guide the building tool to locate the right package by 1) Adding the package's parent path into \$DOPATH;
3254or 2) Adding the package dependence into the current project's Do.prj.
3255More details about locating a module in a package are explained in the next section.
3256
3257
3258\subsubsection{Package Versioning}
3259
3260A package must have a version number.
3261The version number is a string.
3262For example "1.0", "1.a", "A1", and "1ec5fab753eb979d3886a491845b8ae152d58c8f" are all valid version numbers.
3263By convention, a package is stored in a directory named packageName-packageVersion.
3264For example, the util package with version 1.1 is stored in a directory named util-1.1.
3265
3266The project description file can optionally specify the version of the package used in the current project.
3267If not defined, because the version number is a string, and all the different versions for the same package will be sorted in increasing order, the package with the largest version number will be used in the compilation.
3268The builder tool will record the specific package version used in the build in the project's "Do.lock" file to enable fully repeatable builds.
3269
3270
3271\subsection{Module and Package Organization}
3272
3273\CFA has two level of encapsulations, module and package.
3274This section explains the object model of modules, packages and other language concepts.
3275It also explains how programmers should organize their code, and the method used by the build tools to locate packages, and import modules for compilation.
3276
3277
3278\subsubsection{Object Model}
3279
3280There are several concepts in Do.
3281\begin{itemize}
3282\item
3283File: a \CFA source file
3284\item
3285Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside
3286\item
3287Package: a container to organize modules for distribution; It has attributes like name, author,
3288version, dependences, etc.
3289\item
3290Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc.
3291\end{itemize}
3292
3293The following rules summarize the object model of all the above concepts:
3294\begin{itemize}
3295\item
3296A module contains one or more files
3297\begin{itemize}
3298\item
3299One file can only belong to one module
3300\item
3301A module has its name and interfaces exported
3302\item
3303A file without a module declaration at the beginning belongs to the global module
3304\item
3305\end{itemize}
3306
3307\item
3308A package contains one or more modules
3309\begin{itemize}
3310\item
3311A package has additional meta info described in Do.prj file
3312\item
3313A package may be dependent on other packages.
3314\end{itemize}
3315
3316\item
3317A project contains one or more modules in its source code
3318\begin{itemize}
3319\item
3320A project has additional meta info described in Do.prj file
3321\item
3322A project may be dependent on other packages
3323\item
3324A project can be transformed into a package for distribution
3325\item
3326A project can generate one or more executable binaries
3327\end{itemize}
3328\end{itemize}
3329
3330
3331\subsubsection{Module File Organization}
3332
3333The rules of this section are the conventions to organize module files in one package.
3334
3335The file location of a module in a package must match the module/submodule naming hierarchy.
3336The names separated by slash "/" must match the directory levels.
3337If only one file is used to implement one module, there is no need to put the module implementation file inside a sub-directory.
3338The file can be put inside its parent module's sub-directory with the sub module's name as the file name.
3339
3340Here is an example of a package, util.
3341\begin{cfa}
3342+ util
3343Do.prj #package description file
3344        heap.do #Case 1: module heap;
3345        list.do #Case 1: mdoule list;
3346        ring.do #Case 1: module ring;
3347        + string #Case 2
3348        impl1.do #module string;
3349        + std
3350        vector.do
3351        list.do
3352        + array #Case 3
3353        array1.do #module std/array;
3354        array2.do #module std/array;
3355        sequence.do #Case 4, module std/sequence;
3356        test.do #Case 5
3357\end{cfa}
3358
3359\begin{itemize}
3360\item
3361Case 1: Each individual file implements a module
3362\item
3363Case 2: Put the implementation of a module under the sub-directory, but there is only one file
3364\item
3365Case 3: Put the implementation of a module under the sub-directory; There are several files to
3366implement one module
3367\item
3368Case 4: One file to express one aggregation
3369\item
3370Case 5: The file does not belong to any module; It is used for testing purpose
3371\end{itemize}
3372
3373The example only uses source code, ".do" files, to show the module file organization.
3374Other module packaging formats, like binary, must also follow the same rules.
3375
3376
3377\subsection{Module File Format}
3378
3379\CFA supports different types of module file formats.
3380
3381\begin{itemize}
3382\item
3383Pure source code format: The files should be organized following the previous section's definition.
3384\item
3385IR format (TBD): The \CFA compiler IR format, similar to the source code format
3386\item
3387Binary format, including ".a" static library or ".so" dynamic linkage library
3388\begin{itemize}
3389\item
3390The file's name must match the right level's module name defined in the previous section
3391\item
3392E.g. "util.so" includes all modules for the package util.
3393\item
3394E.g. "string.so" under the package directory to include files belonging to "module string;"
3395\end{itemize}
3396\item.
3397Archive format
3398\begin{itemize}
3399\item
3400The archive is named as ".dar", and is a zip archive of the source code or the binary for a package
3401\item
3402E.g. "util.dar" is the whole package for util package including the package direction file
3403\end{itemize}
3404\item
3405Hybrid format
3406\begin{itemize}
3407\item
3408A package can be distributed partly in source code, partly in binary format, and/or packaged in the archive format
3409\item
3410The only limitation is that the names of the files must match the module location names defined in previous section
3411\end{itemize}
3412\end{itemize}
3413Package and Module Locating and the \CFA Language Tooling documentation for more details.
3414
3415
3416\subsection{Packages}
3417
3418A package is synonymous with a library in other languages.
3419The intent of the package level encapsulation is to facilitate code distribution, version control, and dependence management.
3420A package is a physical grouping of one or more modules in a directory (an archive file for a directory).
3421The concept of a package is the convention for grouping code, and the contract between the language and the building tool to search for imported modules.
3422
3423
3424\subsubsection{Package Definition}
3425
3426A package is defined by putting a project description file, Do.prj, with one or more modules into a directory.
3427This project description file contains the package's meta data, including package name, author, version, dependences, etc.
3428It should be in the root of the package directory.
3429
3430The modules in the package could be either source code, or compiled binary format.
3431The location of the module files should follow the module name's path.
3432
3433Here is a simple example of the directory structure of a package, core.
3434It contains a module std and several sub-modules under std.
3435\begin{cfa}
3436+ core
3437        Do.prj
3438        + std
3439        + io
3440        file.do # module std/io/file;
3441        network.do #module std/io/network;
3442        + container
3443        vector.do #module std/container/vector;
3444        list.do #module std/container/list;
3445\end{cfa}
3446
3447
3448\subsubsection{Package Import}
3449
3450Because packages are the places where the building tool looks for modules, there is no code required in the \CFA source file to import a package.
3451In order to use modules in a package, the programmer needs to guide the building tool to locate the right package by 1) Adding the package's parent path into \$DOPATH; or 2) Adding the package dependence into the current project's Do.prj.
3452More details about locating a module in a package are explained in the next section.
3453
3454
3455\subsubsection{Package Versioning}
3456
3457A package must have a version number.
3458The version number is a string.
3459For example "1.0", "1.a", "A1", and "1ec5fab753eb979d3886a491845b8ae152d58c8f" are all valid version numbers.
3460By convention, a package is stored in a directory named packageName-packageVersion.
3461For example, the util package with version 1.1 is stored in a directory named util-1.1.
3462
3463The project description file can optionally specify the version of the package used in the current project.
3464If not defined, because the version number is a string, and all the different versions for the same package will be sorted in increasing order, the package with the largest version number will be used in the compilation.
3465The builder tool will record the specific package version used in the build in the project's "Do.lock" file to enable fully repeatable builds.
3466
3467
3468\subsection{Module and Package Organization}
3469
3470\CFA has two level of encapsulations, module and package.
3471This section explains the object model of modules, packages and other language concepts.
3472It also explains how programmers should organize their code, and the method used by the build tools to locate packages, and import modules for compilation.
3473
3474
3475\subsubsection{Object Model}
3476
3477There are several concepts in Do.
3478\begin{itemize}
3479\item
3480File: a \CFA source file
3481\item
3482Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside
3483\item
3484Package: a container to organize modules for distribution; It has attributes like name, author, version, dependences, etc.
3485\item
3486Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc.
3487\end{itemize}
3488
3489The following rules summarize the object model of all the above concepts:
3490\begin{itemize}
3491\item
3492A module contains one or more files
3493\begin{itemize}
3494\item
3495One file can only belong to one module
3496\item
3497A module has its name and interfaces exported
3498\item
3499A file without a module declaration at the beginning belongs to the global module
3500\end{itemize}
3501\item
3502A package contains one or more modules
3503\begin{itemize}
3504\item
3505A package has additional meta info described in Do.prj file
3506\item
3507A package may be dependent on other packages.
3508\end{itemize}
3509\item
3510A project contains one or more modules in its source code
3511\begin{itemize}
3512\item
3513A project has additional meta info described in Do.prj file
3514\item
3515A project may be dependent on other packages
3516\item
3517A project can be transformed into a package for distribution
3518\item
3519A project can generate one or more executable binaries
3520\end{itemize}
3521\end{itemize}
3522
3523
3524\subsubsection{Module File Organization}
3525
3526The rules of this section are the conventions to organize module files in one package.
3527
3528The file location of a module in a package must match the module/submodule naming hierarchy.
3529The names separated by slash "/" must match the directory levels.
3530If only one file is used to implement one module, there is no need to put the module implementation file inside a sub-directory.
3531The file can be put inside its parent module's sub-directory with the sub module's name as the file name.
3532
3533Here is an example of a package, util.
3534\begin{cfa}
3535+ util
3536        Do.prj #package description file
3537        heap.do #Case 1: module heap;
3538        list.do #Case 1: mdoule list;
3539        ring.do #Case 1: module ring;
3540        + string #Case 2
3541        impl1.do #module string;
3542        + std
3543        vector.do
3544        list.do
3545        + array #Case 3
3546        array1.do #module std/array;
3547        array2.do #module std/array;
3548        sequence.do #Case 4, module std/sequence;
3549        test.do #Case 5
3550\end{cfa}
3551
3552
3553\begin{itemize}
3554\item
3555Case 1: Each individual file implements a module
3556\item
3557Case 2: Put the implementation of a module under the sub-directory, but there is only one file
3558\item
3559Case 3: Put the implementation of a module under the sub-directory; There are several files to implement one module
3560\item
3561Case 4: One file to express one aggregation
3562\item
3563Case 5: The file does not belong to any module; It is used for testing purpose
3564\end{itemize}
3565
3566The example only uses source code, ".do" files, to show the module file organization.
3567Other module packaging formats, like binary, must also follow the same rules.
3568
3569
3570\subsubsection{Module File Format}
3571
3572\CFA supports different types of module file formats.
3573
3574\begin{itemize}
3575\item
3576Pure source code format: The files should be organized following the previous section's definition.
3577\item
3578IR format (TBD): The \CFA compiler IR format, similar to the source code format
3579\item
3580Binary format, including ".a" static library or ".so" dynamic linkage library
3581\begin{itemize}
3582\item
3583The file's name must match the right level's module name defined in the previous section
3584\item
3585E.g. "util.so" includes all modules for the package util.
3586\item
3587E.g. "string.so" under the package directory to include files belonging to "module string;"
3588\end{itemize}
3589\item
3590Archive format
3591\begin{itemize}
3592\item
3593The archive is named as ".dar", and is a zip archive of the source code or the binary for a package
3594\item
3595E.g. "util.dar" is the whole package for util package including the package direction file
3596\end{itemize}
3597\item
3598Hybrid format
3599\begin{itemize}
3600\item
3601A package can be distributed partly in source code, partly in binary format, and/or packaged in the archive format
3602\item
3603The only limitation is that the names of the files must match the module location names defined in previous section
3604\end{itemize}
3605\end{itemize}
3606
3607
3608\subsection{Package and Module Locating}
3609
3610The high-level build tools provided by \CFA will handle finding a package in your local filesystem or retrieving it from a repository if necessary, building it if necessary, and linking with it.
3611If a programmer prefers, one can directly call the compiler, docc to build the source files and create and link to static libraries.
3612
3613When a source file imports a module, the \CFA build tool and docc compiler will locate the module according to the following order:
3614
3615\begin{enumerate}
3616\item
3617This source file's directory tree, which is typically the project's src directory
3618\item
3619All of the dependent packages (in a directory or in an archive file) under the current \CFA project's pkg directory
3620\item
3621The dependent packages (in a directory or in an archive file) inside the paths defined in the DOPATH environment variable
3622\item
3623The dependent packages (in a directory or in an archive file) inside the global \CFA SDK installation's pkg directory
3624\item
3625If one dependent package is still not found, the builder tool will automatically retrieve it from the repository defined in the SDK installation's configuration, and store it in the SDK's pkg directory
3626\end{enumerate}
3627
3628The module found first in a package will shadow the modules with the same name in the later packages in the search sequence.
3629
3630
3631\subsubsection{Dependent Package}
3632
3633Dependent packages are those packages containing modules that the current project's source code will import from.
3634Dependent packages are defined implicitly or explicitly in one \CFA project.
3635All of the packages under the current project's pkg directory are implicitly dependent packages.
3636For others, the dependent packages must be defined in the project's Do.prj file.
3637
3638
3639\subsubsection{Package and Module Locating Example}
3640
3641\begin{cfa}
3642# A project's source code tree
3643
3644--------------------------------------
3645
3646+ testProject
3647        Do.prj
3648        + src
3649        main.do
3650        + pkg
3651        + security-1.1
3652        Do.prj
3653        security.do #module security
3654
3655--------------------------------------
3656
3657# Do.prj
3658
3659--------------------------------------
3660
3661[dependences]
3662std
3663util = "0.2"
3664
3665--------------------------------------
3666
3667# main.do
3668
3669---------------------------------------
3670
3671import security;
3672import std/vector;
3673import container;
3674
3675----------------------------------------
3676\end{cfa}
3677
3678
3679\begin{cfa}
3680# pkg directory's source code tree
3681
3682-----------------------------------------
3683
3684+ pkg
3685        + std-1.0
3686        Do.prj
3687        vector.do #module std/vector;
3688        queue.do #module std/queue;
3689        + std-1.1
3690        Do.prj
3691        vector.do #module std/vector;
3692        queue.do #module std/queue;
3693        list.do #module std/list;
3694        + util-0.1
3695        Do.prj
3696        container.do #module container;
3697        + security-1.0
3698        security.do #module security;
3699------------------------------------------
3700\end{cfa}
3701
3702
3703During the compiling of main.do file import security;
3704The security module appears in both the local security-1.1 package, and the global security-1.0 package.
3705According to the locating sequence, the local security module in security-1.1 will be used.
3706And because the security-1.1 package is under local's pkg directory.
3707No dependence description is required in the project Do.prj file.
3708
3709import std/vector;
3710
3711The std/vector package appears in two different versions' packages in the global path and the project dependence doesn't specify the version. std-1.1 is used in this case.
3712
3713import container;
3714
3715The Do.prj specifies the version 0.2 should be used to locate container module from util package but only version 0.1 is available in the local file system.
3716The builder tool then will try to retrieve it from the web and store it in the global pkg directory.
3717After that, the container module from the newly downloaded package will be used in the compilation.
3718\end{comment}
3719
3720
3721\section{Comparison with Other Languages}
3722
3723\CFA is one of many languages that attempts to improve upon C.
3724In developing \CFA, many other languages were consulted for ideas, constructs, and syntax.
3725Therefore, it is important to show how these languages each compare with Do.
3726In this section, \CFA is compared with what the writers of this document consider to be the closest competitors of Do: \Index*[C++]{\CC}, \Index*{Go}, \Index*{Rust}, and \Index*{D}.
3727
3728
3729\subsection[Comparing Key Features of CFA]{Comparing Key Features of \CFA}
3730
3731
3732{% local change to lstlising to reduce font size
3733
3734
3735\lstset{basicstyle=\linespread{0.9}\sf\relsize{-2}}
3736
3737
3738\subsubsection{Constructors and Destructors}
3739
3740\begin{flushleft}
3741\begin{tabular}{@{}l|l|l|l@{}}
3742\multicolumn{1}{c|}{\textbf{\CFA}}      & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}}      \\
3743\hline
3744\begin{cfa}
3745struct Line {
3746        float lnth;
3747}
3748// default constructor
3749void ?{}( Line * l ) {
3750        l->lnth = 0.0;
3751        sout | "default" | endl;
3752}
3753
3754
3755// constructor with length
3756void ?{}( Line * l, float lnth ) {
3757        l->lnth = lnth;
3758        sout | "lnth" | l->lnth | endl;
3759
3760}
3761
3762// destructor
3763void ^?() {
3764        sout | "destroyed" | endl;
3765        l.lnth = 0.0;
3766}
3767
3768// usage
3769Line line1;
3770Line line2 = { 3.4 };
3771\end{cfa}
3772&
3773\begin{lstlisting}[language=C++]
3774class Line {
3775        float lnth;
3776
3777        // default constructor
3778        Line() {
3779                cout << "default" << endl;
3780                lnth = 0.0;
3781        }
3782
3783
3784        // constructor with lnth
3785        Line( float l ) {
3786                cout << "length " << length
3787                         << endl;
3788                length = l;
3789        }
3790
3791        // destructor
3792        ~Line() {
3793                cout << "destroyed" << endl;
3794                length = 0.0;
3795        }
3796}
3797// usage
3798Line line1;
3799Line line2( 3.4 );
3800\end{lstlisting}
3801&
3802\begin{lstlisting}[language=Golang]
3803type Line struct {
3804        length float32
3805}
3806// default constructor
3807func makeLine() Line {
3808        fmt.PrintLn( "default" )
3809        return Line{0.0}
3810}
3811
3812
3813// constructor with length
3814func makeLine( length float32 ) Line {
3815        fmt.Printf( "length %v", length )
3816
3817        return Line{length}
3818}
3819
3820// no destructor
3821
3822
3823
3824
3825
3826// usage
3827line1 := makeLine()
3828line2 := makeLine( 3.4 )
3829\end{lstlisting}
3830&
3831\begin{cfa}
3832struct Line {
3833        length: f32
3834}
3835// default constructor
3836impl Default for Line {
3837        fn default () -> Line {
3838                println!( "default" );
3839                Line{ length: 0.0 }
3840        }
3841}
3842// constructor with length
3843impl Line {
3844        fn make( len: f32 ) -> Line {
3845                println!( "length: {}", len );
3846                Line{ length: len }
3847        }
3848}
3849// destructor
3850impl Drop for Line {
3851        fn drop( &mut self ) {
3852                self.length = 0.0
3853        }
3854}
3855// usage
3856let line1:Line = Default::default();
3857Line line2( 3.4 );
3858\end{cfa}
3859\end{tabular}
3860\end{flushleft}
3861
3862
3863\subsubsection{Operator Overloading}
3864
3865\begin{flushleft}
3866\begin{tabular}{@{}l|l|l|l@{}}
3867\multicolumn{1}{c|}{\textbf{\CFA}}      & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}}      \\
3868\hline
3869\begin{cfa}
3870struct Cpx {
3871        double re, im;
3872};
3873// overload addition operator
3874Cpx ?+?( Cpx l, const Cpx r ) {
3875        return (Cpx){l.re+l.im, l.im+r.im};
3876}
3877Cpx a, b, c;
3878c = a + b;
3879\end{cfa}
3880&
3881\begin{cfa}
3882struct Cpx {
3883        double re, im;
3884};
3885// overload addition operator
3886Cpx operator+( Cpx l, const Cpx r ) {
3887        return (Cpx){l.re+l.im, l.im+r.im};
3888}
3889Cpx a, b, c;
3890c = a + b;
3891\end{cfa}
3892&
3893\begin{cfa}
3894// no operator overloading
3895
3896
3897
3898
3899
3900
3901
3902\end{cfa}
3903&
3904\begin{cfa}
3905struct Cpx {
3906        re: f32,
3907        im: f32
3908}
3909// overload addition operator
3910impl Add for Cpx {
3911        type Output = Cpx
3912        fn add(self, r: Cpx) -> Cpx {
3913                let mut res = Cpx{re: 0.0, im: 0.0};
3914                res.re = self.re + r.re;
3915                res.im = self.im + r.im;
3916                return res
3917        }
3918}
3919let (a, b, mut c) = ...;
3920c = a + b
3921\end{cfa}
3922\end{tabular}
3923\end{flushleft}
3924
3925
3926\subsubsection{Calling C Functions}
3927
3928\begin{flushleft}
3929\begin{tabular}{@{}l|l|l@{}}
3930\multicolumn{1}{c|}{\textbf{\CFA/\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}}   \\
3931\hline
3932\begin{cfa}[boxpos=t]
3933extern "C" {
3934#include <sys/types.h>
3935#include <sys/stat.h>
3936#include <unistd.h>
3937}
3938size_t fileSize( const char *path ) {
3939        struct stat s;
3940        stat(path, &s);
3941        return s.st_size;
3942}
3943\end{cfa}
3944&
3945\begin{cfa}[boxpos=t]
3946/*
3947#cgo
3948#include <sys/types.h>
3949#include <sys/stat.h>
3950#include <unistd.h>
3951*/
3952import "C"
3953import "unsafe"
3954
3955func fileSize(path string) C.size_t {
3956        var buf C.struct_stat
3957        c_string := C.CString(path)
3958        C.stat(p, &buf)
3959        C.free(unsafe.Pointer(c_string))
3960        return buf._st_size
3961}
3962\end{cfa}
3963&
3964\begin{cfa}[boxpos=t]
3965use libc::{c_int, size_t};
3966// translated from sys/stat.h
3967#[repr(C)]
3968struct stat_t {
3969        ...
3970        st_size: size_t,
3971        ...
3972}
3973#[link(name = "libc")]
3974extern {
3975        fn stat(path: *const u8,
3976        buf: *mut stat_t) -> c_int;
3977}
3978fn fileSize(path: *const u8) -> size_t
3979{
3980        unsafe {
3981                let mut buf: stat_t = uninit();
3982                stat(path, &mut buf);
3983                buf.st_size
3984        }
3985}
3986\end{cfa}
3987\end{tabular}
3988\end{flushleft}
3989
3990
3991\subsubsection{Generic Functions}
3992
3993\begin{flushleft}
3994\begin{tabular}{@{}l|l|l|l@{}}
3995\multicolumn{1}{c|}{\textbf{\CFA}}      & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}}      \\
3996\hline
3997\begin{cfa}
3998generic(type T, type N |
3999        { int ?<?(N, N); })
4000T *maximize(N (*f)(const T&),
4001        int n, T *a) {
4002        T *bestX = NULL;
4003        N bestN;
4004        for (int i = 0; i < n; i++) {
4005        N curN = f(a[i]);
4006        if (bestX == NULL ||
4007        curN > bestN) {
4008        bestX = &a[i]; bestN = curN;
4009        }
4010        }
4011        return bestX;
4012}
4013
4014string *longest(int n, string *p)
4015{
4016        return maximize(length, n, p);
4017}
4018\end{cfa}
4019&
4020\begin{cfa}
4021template<typename T, typename F>
4022T *maximize(const F &f,
4023        int n, T *a) {
4024        typedef decltype(f(a[0])) N;
4025        T *bestX = NULL;
4026        N bestN;
4027        for (int i = 0; i < n; i++) {
4028        N curN = f(a[i]);
4029        if (bestX == NULL || curN > bestN)
4030        {
4031        bestX = &a[i]; bestN = curN;
4032        }
4033        }
4034        return bestX;
4035}
4036
4037string *longest(int n, string *p) {
4038        return maximize(
4039        [](const string &s) {
4040        return s.length();
4041        }, n, p);
4042}
4043\end{cfa}
4044&
4045\begin{cfa}
4046// Go does not support generics!
4047func maximize(
4048        gt func(interface{}, interface{}) bool,
4049        f func(interface{}) interface{},
4050        a []interface{}) interface{} {
4051        var bestX interface{} = nil
4052        var bestN interface{} = nil
4053        for _, x := range a {
4054        curN := f(x)
4055        if bestX == nil || gt(curN, bestN)
4056        {
4057        bestN = curN
4058        bestX = x
4059        }
4060        }
4061        return bestX
4062}
4063
4064func longest(
4065        a []interface{}) interface{} {
4066        return maximize(
4067        func(a, b interface{}) bool {
4068        return a.(int) > b.(int) },
4069        func(s interface{}) interface{} {
4070        return len(s.(string)) },
4071        a).(string)
4072}
4073\end{cfa}
4074&
4075\begin{cfa}
4076use std::cmp::Ordering;
4077
4078fn maximize<N: Ord + Copy, T, F:
4079Fn(&T) -> N>(f: F, a: &Vec<T>) ->
4080Option<&T> {
4081        let mut best_x: Option<&T> = None;
4082        let mut best_n: Option<N> = None;
4083        for x in a {
4084        let n = f(x);
4085        if (match best_n { None => true,
4086        Some(bn) =>
4087        n.cmp(&bn) == Ordering::Greater })
4088        {
4089        best_x = Some(x);
4090        best_n = Some(n);
4091        }
4092        }
4093        return best_x
4094}
4095
4096fn longest(a: &Vec<String>) ->
4097        Option<&String> {
4098        return
4099        maximize(|x: &String| x.len(), a)
4100}
4101\end{cfa}
4102\end{tabular}
4103\end{flushleft}
4104
4105
4106\begin{comment}
4107\subsubsection{Modules / Packages}
4108
4109\begin{cfa}
4110\CFA
4111\CC
4112
4113
4114module example/M;
4115
4116export int inc(int val) {
4117        return val + 1;
4118}
4119
4120
4121
4122
4123--------------------------------------
4124//Use the module in another file
4125import example/M;
4126int main() {
4127        print(M.inc(100));
4128        return 0;
4129}
4130// Using \CC17 module proposal
4131
4132module example.M;
4133
4134export {
4135        int inc(int val);
4136}
4137
4138int inc(inv val) {
4139        return val + 1;
4140}
4141--------------------------------------
4142// Use the module in another file
4143import example.M;
4144int main() {
4145        cout << inc(100) << endl;
4146        return 0;
4147}
4148
4149Go
4150Rust
4151package example/M;
4152
4153func Inc(val int32) int32 {
4154        // Capitalization indicates exported
4155        return val + 100
4156}
4157
4158
4159--------------------------------------
4160//Use the package in another file
4161package main
4162import .fmt.
4163import "example/M"
4164
4165func main() int32 {
4166        fmt.Printf(.%v., M.Inc(100))
4167}
4168pub mod example {
4169        pub mod M {
4170        pub inc(val i32) -> i32 {
4171        return val + 100;
4172        }
4173        }
4174}
4175
4176--------------------------------------
4177//Use the module in another file
4178use example::M;
4179
4180
4181
4182fn main() {
4183        println!(.{}., M::inc(100));
4184}
4185\end{cfa}
4186\end{comment}
4187
4188
4189\subsubsection{Parallel Tasks}
4190
4191\begin{flushleft}
4192\begin{tabular}{@{}l|l|l|l@{}}
4193\multicolumn{1}{c|}{\textbf{\CFA}}      & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}}      \\
4194\hline
4195\begin{cfa}
4196task Nonzero {
4197        int *data;
4198        int start;
4199        int end;
4200        int* res;
4201};
4202
4203void ?{}(Nonzero &a, int d[], int s,
4204        int e, int* subres) {
4205        // constructor
4206        a.data = d;
4207        a.start = s;
4208        a.end = e;
4209        a.res = subres;
4210}
4211
4212// implicitly spawn thread here
4213void ?()(NonzeroCounter &a) {
4214        int i;
4215        int nonzero = 0;
4216        for (i=start; c<end; ++i) {
4217        if(a.data[i]!=0){ nonzero++;}
4218        }
4219        *a.res = nonzero;
4220}
4221
4222int main() {
4223        int sz = ...
4224        int data[sz] = ...;
4225        int r1 = 0, r2=0;
4226        int res;
4227        { // create a scope for Nonzero
4228        Nonzero n1{data, 0, sz/2, &n1};
4229        Nonzero n2{data, sz/2, sz, &n2};
4230        n1();//spawn
4231        n2();//spawn
4232        }
4233        res = r1+r2;
4234        return res;
4235}
4236\end{cfa}
4237&
4238\begin{cfa}
4239#include <thread>
4240#include <mutex>
4241
4242std::mutex m;
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255void task(const vector<int>&v,
4256        int* res, size_t s,
4257        size_t e) {
4258        int non_zero = 0;
4259        for(size_t i = s; i < e; ++i){
4260        if(v[i]!=0) { non_zero++;}
4261        }
4262        std::unique_lock<mutex> lck {m};
4263        *res += non_zero;
4264}
4265
4266int main() {
4267        vector<int> data = ...; //data
4268        int res = 0;
4269        std::thread t1 {task, ref(data),
4270        &res, 0,
4271        data.size()/2};
4272        std::thread t2 {task, ref(data),
4273        &res, data.size()/2,
4274        data.size()};
4275        t1.join();
4276        t2.join();
4277        return res;
4278}
4279\end{cfa}
4280&
4281\begin{cfa}
4282package main
4283
4284import "fmt"
4285
4286func nonzero(data []int, c chan int) {
4287        nz := 0
4288        for _, v:=range data {
4289        if(v!=0) { nz := nz+1 }
4290        }
4291        c <- nz
4292}
4293
4294func main() {
4295        sz := ...
4296        data := make([]int, sz)
4297        ... // data init
4298        go nonzero(data[:len(data)/2], c)
4299        go nonzero(data[len(data)/2:], c)
4300        n1, n2 := <-c, <-c
4301        res := n1 + n2
4302        fmt.Println(res)
4303}
4304\end{cfa}
4305&
4306\begin{cfa}
4307use std::thread;
4308use std::sync:mpsc::channel;
4309
4310fn main() {
4311        let sz = ...;
4312        let mut data:Vec<i32> =
4313        Vec::with_capacity(sz as usize);
4314        ... //init data
4315        let (tx, rx) = channel();
4316        for i in 0..1 {
4317        let tx = tx.clone();
4318        let data = data.clone()
4319        thread::spawn(move|| {
4320        let mut nz := 0;
4321        let mut s = 0;
4322        let mut e = sz / 2;
4323        if i == 1 {
4324        s = sz/2;
4325        e = data.len();
4326        }
4327        for i in s..(e - 1) {
4328        if data[i] != 0 (
4329        nz = nz + 1
4330        }
4331        }
4332        tx.send(nz).unwrap();
4333        });
4334        }
4335        let res = rx.recv().unwrap() +
4336        rx.recv().unwrap();
4337        println!(.{}., res);
4338}
4339\end{cfa}
4340\end{tabular}
4341\end{flushleft}
4342
4343}% local change to lstlising to reduce font size
4344
4345
4346\subsection{Summary of Language Comparison}
4347
4348
4349\subsubsection[C++]{\CC}
4350
4351\Index*[C++]{\CC} is a general-purpose programming language.
4352It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation. (Wikipedia)
4353
4354The primary focus of \CC seems to be adding object-oriented programming to C, and this is the primary difference between \CC and Do.
4355\CC uses classes to encapsulate data and the functions that operate on that data, and to hide the internal representation of the data.
4356\CFA uses modules instead to perform these same tasks.
4357Classes in \CC also enable inheritance among types.
4358Instead of inheritance, \CFA embraces composition and interfaces to achieve the same goals with more flexibility.
4359There are many studies and articles comparing inheritance and composition (or is-a versus has-a relationships), so we will not go into more detail here (Venners, 1998) (Pike, \Index*{Go} at Google: Language Design in the Service of Software Engineering , 2012).
4360
4361Overloading in \CFA is very similar to overloading in \CC, with the exception of the additional use, in \CFA, of the return type to differentiate between overloaded functions.
4362References and exceptions in \CFA are heavily based on the same features from \CC.
4363The mechanism for interoperating with C code in \CFA is also borrowed from \CC.
4364
4365Both \CFA and \CC provide generics, and the syntax is quite similar.
4366The key difference between the two, is that in \CC templates are expanded at compile time for each type for which the template is instantiated, while in \CFA, function pointers are used to make the generic fully compilable.
4367This means that a generic function can be defined in a compiled library, and still be used as expected from source.
4368
4369
4370\subsubsection{Go}
4371
4372\Index*{Go}, also commonly referred to as golang, is a programming language developed at Google in 2007 [.].
4373It is a statically typed language with syntax loosely derived from that of C, adding garbage collection, type
4374safety, some structural typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library. (Wikipedia)
4375
4376Go and \CFA differ significantly in syntax and implementation, but the underlying core concepts of the two languages are aligned.
4377Both Go and \CFA use composition and interfaces as opposed to inheritance to enable encapsulation and abstraction.
4378Both languages (along with their tooling ecosystem) provide a simple packaging mechanism for building units of code for easy sharing and reuse.
4379Both languages also include built-in light weight, user level threading concurrency features that attempt to simplify the effort and thought process required for writing parallel programs while maintaining high performance.
4380
4381Go has a significant runtime which handles the scheduling of its light weight threads, and performs garbage collection, among other tasks.
4382\CFA uses a cooperative scheduling algorithm for its tasks, and uses automatic reference counting to enable advanced memory management without garbage collection.
4383This results in Go requiring significant overhead to interface with C libraries while \CFA has no overhead.
4384
4385
4386\subsubsection{Rust}
4387
4388\Index*{Rust} is a general-purpose, multi-paradigm, compiled programming language developed by Mozilla Research.
4389It is designed to be a "safe, concurrent, practical language", supporting pure-functional, concurrent-actor[dubious . discuss][citation needed], imperative-procedural, and object-oriented styles.
4390
4391The primary focus of Rust is in safety, especially in concurrent programs.
4392To enforce a high level of safety, Rust has added ownership as a core feature of the language to guarantee memory safety.
4393This safety comes at the cost of a difficult learning curve, a change in the thought model of the program, and often some runtime overhead.
4394
4395Aside from those key differences, Rust and \CFA also have several similarities.
4396Both languages support no overhead interoperability with C and have minimal runtimes.
4397Both languages support inheritance and polymorphism through the use of interfaces (traits).
4398
4399
4400\subsubsection{D}
4401
4402The \Index*{D} programming language is an object-oriented, imperative, multi-paradigm system programming
4403language created by Walter Bright of Digital Mars and released in 2001. [.]
4404Though it originated as a re-engineering of \CC, D is a distinct language, having redesigned some core \CC features while also taking inspiration from other languages, notably \Index*{Java}, \Index*{Python}, Ruby, C\#, and Eiffel.
4405
4406D and \CFA both start with C and add productivity features.
4407The obvious difference is that D uses classes and inheritance while \CFA uses composition and interfaces.
4408D is closer to \CFA than \CC since it is limited to single inheritance and also supports interfaces.
4409Like \CC, and unlike \CFA, D uses garbage collection and has compile-time expanded templates.
4410D does not have any built-in concurrency constructs in the
4411language, though it does have a standard library for concurrency which includes the low-level primitives for concurrency.
4412
4413
4414\appendix
4415
4416
4417\section{Incompatible}
4418
4419The following incompatibles exist between \CFA and C, and are similar to Annex C for \CC~\cite{ANSI14:C++}.
4420
4421\begin{enumerate}
4422\item
4423\begin{description}
4424\item[Change:] add new keywords \\
4425New keywords are added to \CFA (see~\VRef{s:NewKeywords}).
4426\item[Rationale:] keywords added to implement new semantics of \CFA.
4427\item[Effect on original feature:] change to semantics of well-defined feature. \\
4428Any ISO C programs using these keywords as identifiers are invalid \CFA programs.
4429\item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism (see~\VRef{s:BackquoteIdentifiers}):
4430\item[How widely used:] clashes among new \CFA keywords and existing identifiers are rare.
4431\end{description}
4432
4433\item
4434\begin{description}
4435\item[Change:] type of character literal ©int© to ©char© to allow more intuitive overloading:
4436\begin{cfa}
4437int rtn( int i );
4438int rtn( char c );
4439rtn( 'x' );                                             §\C{// programmer expects 2nd rtn to be called}§
4440\end{cfa}
4441\item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first.
4442In particular, output of ©char© variable now print a character rather than the decimal ASCII value of the character.
4443\begin{cfa}
4444sout | 'x' | " " | (int)'x' | endl;
4445x 120
4446\end{cfa}
4447Having to cast ©'x'© to ©char© is non-intuitive.
4448\item[Effect on original feature:] change to semantics of well-defined feature that depend on:
4449\begin{cfa}
4450sizeof( 'x' ) == sizeof( int )
4451\end{cfa}
4452no long work the same in \CFA programs.
4453\item[Difficulty of converting:] simple
4454\item[How widely used:] programs that depend upon ©sizeof( 'x' )© are rare and can be changed to ©sizeof(char)©.
4455\end{description}
4456
4457\item
4458\begin{description}
4459\item[Change:] make string literals ©const©:
4460\begin{cfa}
4461char * p = "abc";                               §\C{// valid in C, deprecated in \CFA}§
4462char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§
4463\end{cfa}
4464The type of a string literal is changed from ©[] char© to ©const [] char©.
4465Similarly, the type of a wide string literal is changed from ©[] wchar_t© to ©const [] wchar_t©.
4466\item[Rationale:] This change is a safety issue:
4467\begin{cfa}
4468char * p = "abc";
4469p[0] = 'w';                                             §\C{// segment fault or change constant literal}§
4470\end{cfa}
4471The same problem occurs when passing a string literal to a routine that changes its argument.
4472\item[Effect on original feature:] change to semantics of well-defined feature.
4473\item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to ©char *©.
4474\item[How widely used:] programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are rare.
4475\end{description}
4476
4477\item
4478\begin{description}
4479\item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope:
4480\begin{cfa}
4481int i;                                                  §\C{// forward definition}§
4482int *j = ®&i®;                                  §\C{// forward reference, valid in C, invalid in \CFA}§
4483int i = 0;                                              §\C{// definition}§
4484\end{cfa}
4485is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed.
4486This change makes it impossible to define mutually referential file-local static objects, if initializers are restricted to the syntactic forms of C. For example,
4487\begin{cfa}
4488struct X { int i; struct X *next; };
4489static struct X a;                              §\C{// forward definition}§
4490static struct X b = { 0, ®&};        §\C{// forward reference, valid in C, invalid in \CFA}§
4491static struct X a = { 1, &b };  §\C{// definition}§
4492\end{cfa}
4493\item[Rationale:] avoids having different initialization rules for builtin types and userdefined types.
4494\item[Effect on original feature:] change to semantics of well-defined feature.
4495\item[Difficulty of converting:] the initializer for one of a set of mutually-referential file-local static objects must invoke a routine call to achieve the initialization.
4496\item[How widely used:] seldom
4497\end{description}
4498
4499\item
4500\begin{description}
4501\item[Change:] have ©struct© introduce a scope for nested types:
4502\begin{cfa}
4503enum ®Colour® { R, G, B, Y, C, M };
4504struct Person {
4505        enum ®Colour® { R, G, B };      §\C{// nested type}§
4506        struct Face {                           §\C{// nested type}§
4507                ®Colour® Eyes, Hair;    §\C{// type defined outside (1 level)}§
4508        };
4509        ß.ß®Colour® shirt;                      §\C{// type defined outside (top level)}§
4510        ®Colour® pants;                         §\C{// type defined same level}§
4511        Face looks[10];                         §\C{// type defined same level}§
4512};
4513®Colour® c = R;                                 §\C{// type/enum defined same level}§
4514Personß.ß®Colour® pc = Personß.ßR;      §\C{// type/enum defined inside}§
4515Personß.ßFace pretty;                   §\C{// type defined inside}§
4516\end{cfa}
4517In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, i.e., the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing.
4518\CFA is C \emph{incompatible} on this issue, and provides semantics similar to \Index*[C++]{\CC}.
4519Nested types are not hoisted and can be referenced using the field selection operator ``©.©'', unlike the \CC scope-resolution operator ``©::©''.
4520\item[Rationale:] ©struct© scope is crucial to \CFA as an information structuring and hiding mechanism.
4521\item[Effect on original feature:] change to semantics of well-defined feature.
4522\item[Difficulty of converting:] Semantic transformation.
4523\item[How widely used:] C programs rarely have nest types because they are equivalent to the hoisted version.
4524\end{description}
4525
4526\item
4527\begin{description}
4528\item[Change:] In C++, the name of a nested class is local to its enclosing class.
4529\item[Rationale:] C++ classes have member functions which require that classes establish scopes.
4530\item[Difficulty of converting:] Semantic transformation. To make the struct type name visible in the scope of the enclosing struct, the struct tag could be declared in the scope of the enclosing struct, before the enclosing struct is defined. Example:
4531\begin{cfa}
4532struct Y;                                               §\C{// struct Y and struct X are at the same scope}§
4533struct X {
4534struct Y { /* ... */ } y;
4535};
4536\end{cfa}
4537All the definitions of C struct types enclosed in other struct definitions and accessed outside the scope of the enclosing struct could be exported to the scope of the enclosing struct.
4538Note: this is a consequence of the difference in scope rules, which is documented in 3.3.
4539\item[How widely used:] Seldom.
4540\end{description}
4541
4542\item
4543\begin{description}
4544\item[Change:] comma expression is disallowed as subscript
4545\item[Rationale:] safety issue to prevent subscripting error for multidimensional arrays: ©x[i,j]© instead of ©x[i][j]©, and this syntactic form then taken by \CFA for new style arrays.
4546\item[Effect on original feature:] change to semantics of well-defined feature.
4547\item[Difficulty of converting:] semantic transformation of ©x[i,j]© to ©x[(i,j)]©
4548\item[How widely used:] seldom.
4549\end{description}
4550\end{enumerate}
4551
4552
4553\section{New Keywords}
4554\label{s:NewKeywords}
4555
4556\begin{quote2}
4557\begin{tabular}{lll}
4558©catch©                 & ©fallthrough© & ©otype©               \\
4559©catchResume©   & ©fallthru©    & ©throw©               \\
4560©choose©                & ©finally©             & ©throwResume© \\
4561©disable©               & ©forall©              & ©trait©               \\
4562©dtype©                 & ©ftype©               & ©try©                 \\
4563©enable©                & ©lvalue©              &                               \\
4564\end{tabular}
4565\end{quote2}
4566
4567
4568\section{Standard Headers}
4569\label{s:StandardHeaders}
4570
4571C prescribes the following standard header-files~\cite[\S~7.1.2]{C11}:
4572\begin{quote2}
4573\begin{minipage}{\linewidth}
4574\begin{tabular}{lll}
4575assert.h        & math.h                & stdlib.h              \\
4576complex.h       & setjmp.h              & stdnoreturn.h \\
4577ctype.h         & signal.h              & string.h              \\
4578errno.h         & stdalign.h    & tgmath.h              \\
4579fenv.h          & stdarg.h              & threads.h             \\
4580float.h         & stdatomic.h   & time.h                \\
4581inttypes.h      & stdbool.h             & uchar.h               \\
4582iso646.h        & stddef.h              & wchar.h               \\
4583limits.h        & stdint.h              & wctype.h              \\
4584locale.h        & stdio.h               & unistd.h\footnote{\CFA extension}
4585\end{tabular}
4586\end{minipage}
4587\end{quote2}
4588For the prescribed head-files, \CFA implicitly wraps their includes in an ©extern "C"©;
4589hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}).
4590All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling.
4591
4592
4593\section{I/O Library}
4594\label{s:IOLibrary}
4595\index{input/output library}
4596
4597The goal for the \CFA I/O is to make I/O as simple as possible in the common cases, while fully supporting polymorphism and user defined types in a consistent way.
4598The common case is printing out a sequence of variables separated by whitespace.
4599\begin{quote2}
4600\begin{tabular}{@{}l@{\hspace{3em}}l@{}}
4601\multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}}        & \multicolumn{1}{c}{\textbf{\CC}}      \\
4602\begin{cfa}
4603int x = 0, y = 1, z = 2;
4604®sout® ®|® x ®|® y ®|® z ®| endl®;
4605\end{cfa}
4606&
4607\begin{cfa}
4608
4609cout << x << " " << y << " " << z << endl;
4610\end{cfa}
4611\end{tabular}
4612\end{quote2}
4613The \CFA form has half as many characters as the \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators.
4614
4615The logical-or operator is used because it is the lowest-priority overloadable operator, other than assignment.
4616Therefore, fewer output expressions require parenthesis.
4617\begin{quote2}
4618\begin{tabular}{@{}ll@{}}
4619\textbf{\CFA:}
4620&
4621\begin{cfa}
4622sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl;
4623\end{cfa}
4624\\
4625\textbf{\CC:}
4626&
4627\begin{cfa}
4628cout << x * 3 << y + 1 << (z << 2) << (x == y) << (x | y) << (x || y) << (x > z ? 1 : 2) << endl;
4629\end{cfa}
4630\end{tabular}
4631\end{quote2}
4632Finally, the logical-or operator has a link with the Shell pipe-operator for moving data, although data flows in the opposite direction.
4633
4634The implicit separator\index{I/O separator} character (space/blank) is a separator not a terminator.
4635The rules for implicitly adding the separator are:
4636\begin{enumerate}
4637\item
4638A separator does not appear at the start or end of a line.
4639\begin{cfa}[belowskip=0pt]
4640sout | 1 | 2 | 3 | endl;
4641\end{cfa}
4642\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
46431 2 3
4644\end{cfa}
4645\item
4646A separator does not appear before or after a character literal or variable.
4647\begin{cfa}
4648sout | '1' | '2' | '3' | endl;
4649123
4650\end{cfa}
4651\item
4652A separator does not appear before or after a null (empty) C string
4653\begin{cfa}
4654sout | 1 | "" | 2 | "" | 3 | endl;
4655123
4656\end{cfa}
4657which is a local mechanism to disable insertion of the separator character.
4658\item
4659A separator does not appear before a C string starting with the (extended) \Index{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off]@([{=$£¥¡¿«@
4660%$
4661\begin{cfa}[mathescape=off]
4662sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" | 7
4663         | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;
4664\end{cfa}
4665%$
4666\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
4667x (1 x [2 x {3 x =4 x $5 x £6 x ¥7 x ¡8 x ¿9 x «10
4668\end{cfa}
4669%$
4670\item
4671{\lstset{deletedelim=**[is][]{¢}{¢}}
4672A seperator does not appear after a C string ending with the (extended) \Index{ASCII}\index{ASCII!extended} characters: ©,.:;!?)]}%¢»©
4673\begin{cfa}[belowskip=0pt]
4674sout | 1 | ", x" | 2 | ". x" | 3 | ": x" | 4 | "; x" | 5 | "! x" | 6 | "? x" | 7 | "% x"
4675         | 8 | "¢ x" | 9 | "» x" | 10 | ") x" | 11 | "] x" | 12 | "} x" | endl;
4676\end{cfa}
4677\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
46781, x 2. x 3: x 4; x 5! x 6? x 7% x 8¢ x 9» x 10) x 11] x 12} x
4679\end{cfa}}%
4680\item
4681A seperator does not appear before or after a C string begining/ending with the \Index{ASCII} quote or whitespace characters: \lstinline[showspaces=true]@`'" \t\v\f\r\n@
4682\begin{cfa}[belowskip=0pt]
4683sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x" | "x " | 4 | " x" | "x\t" | 1 | "\tx" | endl;
4684\end{cfa}
4685\begin{cfa}[mathescape=off,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt]
4686x`1`x'2'x"3"x x 4 x x   1       x
4687\end{cfa}
4688\end{enumerate}
4689The following \CC-style \Index{manipulator}s allow further control over implicit seperation.
4690\begin{cfa}[mathescape=off,belowskip=0pt]
4691sout | sepOn | 1 | 2 | 3 | sepOn | endl;        §\C{// separator at start of line}§
4692\end{cfa}
4693\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
4694 1 2 3
4695\end{cfa}
4696\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
4697sout | 1 | sepOff | 2 | 3 | endl;                       §\C{// turn off implicit separator locally}§
4698\end{cfa}
4699\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
470012 3
4701\end{cfa}
4702\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
4703sout | sepDisable | 1 | 2 | 3 | endl;           §\C{// turn off implicit separation globally}§
4704\end{cfa}
4705\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
4706123
4707\end{cfa}
4708\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
4709sout | 1 | sepOn | 2 | 3 | endl;                        §\C{// turn on implicit separator locally}§
4710\end{cfa}
4711\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
47121 23
4713\end{cfa}
4714\begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt]
4715sout | sepEnable | 1 | 2 | 3 | endl;            §\C{// turn on implicit separation globally}§
4716\end{cfa}
4717\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]
4718 1 2 3
4719\end{cfa}
4720\begin{cfa}[mathescape=off,aboveskip=0pt,aboveskip=0pt,belowskip=0pt]
4721sepSet( sout, ", $" );                                          §\C{// change separator from " " to ", \$"}§
4722sout | 1 | 2 | 3 | endl;
4723\end{cfa}
4724%$
4725\begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt]
47261, $2, $3
4727\end{cfa}
4728%$
4729\begin{comment}
4730#include <fstream>
4731
4732int main() {
4733        int x = 0, y = 1, z = 2;
4734        sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl | endl;
4735        sout | 1 | 2 | 3 | endl;
4736        sout | '1' | '2' | '3' | endl;
4737        sout | 1 | "" | 2 | "" | 3 | endl;
4738        sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" | 7
4739                | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;
4740        sout | 1 | ", x" | 2 | ". x" | 3 | ": x" | 4 | "; x" | 5 | "! x" | 6 | "? x" | 7 | "% x"
4741                | 8 | "¢ x" | 9 | "» x" | 10 | ") x" | 11 | "] x" | 12 | "} x" | endl;
4742        sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x" | "x " | 4 | " x" | "x\t" | 1 | "\tx" | endl;
4743        sout | sepOn | 1 | 2 | 3 | sepOn | endl;        // separator at start of line
4744        sout | 1 | sepOff | 2 | 3 | endl;                       // turn off implicit separator temporarily
4745        sout | sepDisable | 1 | 2 | 3 | endl;           // turn off implicit separation, affects all subsequent prints
4746        sout | 1 | sepOn | 2 | 3 | endl;                        // turn on implicit separator temporarily
4747        sout | sepEnable | 1 | 2 | 3 | endl;            // turn on implicit separation, affects all subsequent prints
4748        sepSet( sout, ", $" );                                          // change separator from " " to ", $"
4749        sout | 1 | 2 | 3 | endl;
4750
4751}
4752
4753// Local Variables: //
4754// tab-width: 4 //
4755// End: //
4756\end{comment}
4757%$
4758
4759
4760\section{Standard Library}
4761\label{s:StandardLibrary}
4762
4763The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions.
4764
4765
4766\subsection{malloc}
4767
4768\leavevmode
4769\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4770forall( otype T ) T * malloc( void )\indexc{malloc}§
4771forall( otype T ) T * malloc( char fill );
4772forall( otype T ) T * malloc( T * ptr, size_t size );
4773forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill );
4774forall( otype T ) T * calloc( size_t nmemb )\indexc{calloc}§
4775forall( otype T ) T * realloc( T * ptr, size_t size )\indexc{ato}§
4776forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill );
4777
4778forall( otype T ) T * aligned_alloc( size_t alignment )\indexc{ato}§
4779forall( otype T ) T * memalign( size_t alignment );             // deprecated
4780forall( otype T ) int posix_memalign( T ** ptr, size_t alignment );
4781
4782forall( otype T ) T * memset( T * ptr, unsigned char fill ); // use default value '\0' for fill
4783forall( otype T ) T * memset( T * ptr );                                // remove when default value available
4784\end{cfa}
4785
4786
4787\subsection{ato / strto}
4788
4789\leavevmode
4790\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4791int ato( const char * ptr )\indexc{ato}§
4792unsigned int ato( const char * ptr );
4793long int ato( const char * ptr );
4794unsigned long int ato( const char * ptr );
4795long long int ato( const char * ptr );
4796unsigned long long int ato( const char * ptr );
4797float ato( const char * ptr );
4798double ato( const char * ptr );
4799long double ato( const char * ptr );
4800float _Complex ato( const char * ptr );
4801double _Complex ato( const char * ptr );
4802long double _Complex ato( const char * ptr );
4803
4804int strto( const char * sptr, char ** eptr, int base );
4805unsigned int strto( const char * sptr, char ** eptr, int base );
4806long int strto( const char * sptr, char ** eptr, int base );
4807unsigned long int strto( const char * sptr, char ** eptr, int base );
4808long long int strto( const char * sptr, char ** eptr, int base );
4809unsigned long long int strto( const char * sptr, char ** eptr, int base );
4810float strto( const char * sptr, char ** eptr );
4811double strto( const char * sptr, char ** eptr );
4812long double strto( const char * sptr, char ** eptr );
4813float _Complex strto( const char * sptr, char ** eptr );
4814double _Complex strto( const char * sptr, char ** eptr );
4815long double _Complex strto( const char * sptr, char ** eptr );
4816\end{cfa}
4817
4818
4819\subsection{bsearch / qsort}
4820
4821\leavevmode
4822\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4823forall( otype T | { int ?<?( T, T ); } )
4824T * bsearch( const T key, const T * arr, size_t dimension )\indexc{bsearch}§
4825
4826forall( otype T | { int ?<?( T, T ); } )
4827void qsort( const T * arr, size_t dimension )\indexc{qsort}§
4828\end{cfa}
4829
4830
4831\subsection{abs}
4832
4833\leavevmode
4834\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4835char abs( char )\indexc{abs}§
4836int abs( int );
4837long int abs( long int );
4838long long int abs( long long int );
4839float abs( float );
4840double abs( double );
4841long double abs( long double );
4842float abs( float _Complex );
4843double abs( double _Complex );
4844long double abs( long double _Complex );
4845\end{cfa}
4846
4847
4848\subsection{random}
4849
4850\leavevmode
4851\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4852void rand48seed( long int s )\indexc{rand48seed}§
4853char rand48()\indexc{rand48
4854int rand48();
4855unsigned int rand48();
4856long int rand48();
4857unsigned long int rand48();
4858float rand48();
4859double rand48();
4860float _Complex rand48();
4861double _Complex rand48();
4862long double _Complex rand48();
4863\end{cfa}
4864
4865
4866\subsection{min / max / clamp / swap}
4867
4868\leavevmode
4869\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4870forall( otype T | { int ?<?( T, T ); } )
4871T min( const T t1, const T t2 )\indexc{min}§
4872
4873forall( otype T | { int ?>?( T, T ); } )
4874T max( const T t1, const T t2 )\indexc{max}§
4875
4876forall( otype T | { T min( T, T ); T max( T, T ); } )
4877T clamp( T value, T min_val, T max_val )\indexc{clamp}§
4878
4879forall( otype T )
4880void swap( T * t1, T * t2 )\indexc{swap}§
4881\end{cfa}
4882
4883
4884\section{Math Library}
4885\label{s:Math Library}
4886
4887The goal of the \CFA math-library is to wrap many of the existing C math library-routines that are explicitly polymorphic into implicitly polymorphic versions.
4888
4889
4890\subsection{General}
4891
4892\leavevmode
4893\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4894float fabs( float )\indexc{fabs}§
4895double fabs( double );
4896long double fabs( long double );
4897float cabs( float _Complex );
4898double cabs( double _Complex );
4899long double cabs( long double _Complex );
4900
4901float ?%?( float, float );§\indexc{fmod}§
4902float fmod( float, float );
4903double ?%?( double, double );
4904double fmod( double, double );
4905long double ?%?( long double, long double );
4906long double fmod( long double, long double );
4907
4908float remainder( float, float )\indexc{remainder}§
4909double remainder( double, double );
4910long double remainder( long double, long double );
4911
4912[ int, float ] remquo( float, float )\indexc{remquo}§
4913float remquo( float, float, int * );
4914[ int, double ] remquo( double, double );
4915double remquo( double, double, int * );
4916[ int, long double ] remquo( long double, long double );
4917long double remquo( long double, long double, int * );
4918
4919[ int, float ] div( float, float );                                             // alternative name for remquo
4920float div( float, float, int * )\indexc{div}§
4921[ int, double ] div( double, double );
4922double div( double, double, int * );
4923[ int, long double ] div( long double, long double );
4924long double div( long double, long double, int * );
4925
4926float fma( float, float, float )\indexc{fma}§
4927double fma( double, double, double );
4928long double fma( long double, long double, long double );
4929
4930float fdim( float, float )\indexc{fdim}§
4931double fdim( double, double );
4932long double fdim( long double, long double );
4933
4934float nan( const char * )\indexc{nan}§
4935double nan( const char * );
4936long double nan( const char * );
4937\end{cfa}
4938
4939
4940\subsection{Exponential}
4941
4942\leavevmode
4943\begin{cfa}[aboveskip=0pt,belowskip=0pt]
4944float exp( float )\indexc{exp}§
4945double exp( double );
4946long double exp( long double );
4947float _Complex exp( float _Complex );
4948double _Complex exp( double _Complex );
4949long double _Complex exp( long double _Complex );
4950
4951float exp2( float )\indexc{exp2
4952double exp2( double );
4953long double exp2( long double );
4954float _Complex exp2( float _Complex );
4955double _Complex exp2( double _Complex );
4956long double _Complex exp2( long double _Complex );
4957
4958float expm1( float )\indexc{expm1
4959double expm1( double );
4960long double expm1( long double );
4961
4962float log( float )\indexc{log}§
4963double log( double );
4964long double log( long double );
4965float _Complex log( float _Complex );
4966double _Complex log( double _Complex );
4967long double _Complex log( long double _Complex );
4968
4969float log2( float )\indexc{log2
4970double log2( double );
4971long double log2( long double );
4972float _Complex log2( float _Complex );
4973double _Complex log2( double _Complex );
4974long double _Complex log2( long double _Complex );
4975
4976float log10( float )\indexc{log10
4977double log10( double );
4978long double log10( long double );
4979float _Complex log10( float _Complex );
4980double _Complex log10( double _Complex );
4981long double _Complex log10( long double _Complex );
4982
4983float log1p( float )\indexc{log1p}§
4984double log1p( double );
4985long double log1p( long double );
4986
4987int ilogb( float )\indexc{ilogb}§
4988int ilogb( double );
4989int ilogb( long double );
4990
4991float logb( float )\indexc{logb}§
4992double logb( double );
4993long double logb( long double );
4994\end{cfa}
4995
4996
4997\subsection{Power}
4998
4999\leavevmode
5000\begin{cfa}[aboveskip=0pt,belowskip=0pt]
5001float sqrt( float )\indexc{sqrt}§
5002double sqrt( double );
5003long double sqrt( long double );
5004float _Complex sqrt( float _Complex );
5005double _Complex sqrt( double _Complex );
5006long double _Complex sqrt( long double _Complex );
5007
5008float cbrt( float )\indexc{cbrt}§
5009double cbrt( double );
5010long double cbrt( long double );
5011
5012float hypot( float, float )\indexc{hypot}§
5013double hypot( double, double );
5014long double hypot( long double, long double );
5015
5016float pow( float, float )\indexc{pow}§
5017double pow( double, double );
5018long double pow( long double, long double );
5019float _Complex pow( float _Complex, float _Complex );
5020double _Complex pow( double _Complex, double _Complex );
5021long double _Complex pow( long double _Complex, long double _Complex );
5022\end{cfa}
5023
5024
5025\subsection{Trigonometric}
5026
5027\leavevmode
5028\begin{cfa}[aboveskip=0pt,belowskip=0pt]
5029float sin( float )\indexc{sin}§
5030double sin( double );
5031long double sin( long double );
5032float _Complex sin( float _Complex );
5033double _Complex sin( double _Complex );
5034long double _Complex sin( long double _Complex );
5035
5036float cos( float )\indexc{cos}§
5037double cos( double );
5038long double cos( long double );
5039float _Complex cos( float _Complex );
5040double _Complex cos( double _Complex );
5041long double _Complex cos( long double _Complex );
5042
5043float tan( float )\indexc{tan}§
5044double tan( double );
5045long double tan( long double );
5046float _Complex tan( float _Complex );
5047double _Complex tan( double _Complex );
5048long double _Complex tan( long double _Complex );
5049
5050float asin( float )\indexc{asin}§
5051double asin( double );
5052long double asin( long double );
5053float _Complex asin( float _Complex );
5054double _Complex asin( double _Complex );
5055long double _Complex asin( long double _Complex );
5056
5057float acos( float )\indexc{acos}§
5058double acos( double );
5059long double acos( long double );
5060float _Complex acos( float _Complex );
5061double _Complex acos( double _Complex );
5062long double _Complex acos( long double _Complex );
5063
5064float atan( float )\indexc{atan}§
5065double atan( double );
5066long double atan( long double );
5067float _Complex atan( float _Complex );
5068double _Complex atan( double _Complex );
5069long double _Complex atan( long double _Complex );
5070
5071float atan2( float, float )\indexc{atan2
5072double atan2( double, double );
5073long double atan2( long double, long double );
5074
5075float atan( float, float );                                                             // alternative name for atan2
5076double atan( double, double )\indexc{atan}§
5077long double atan( long double, long double );
5078\end{cfa}
5079
5080
5081\subsection{Hyperbolic}
5082
5083\leavevmode
5084\begin{cfa}[aboveskip=0pt,belowskip=0pt]
5085float sinh( float )\indexc{sinh}§
5086double sinh( double );
5087long double sinh( long double );
5088float _Complex sinh( float _Complex );
5089double _Complex sinh( double _Complex );
5090long double _Complex sinh( long double _Complex );
5091
5092float cosh( float )\indexc{cosh}§
5093double cosh( double );
5094long double cosh( long double );
5095float _Complex cosh( float _Complex );
5096double _Complex cosh( double _Complex );
5097long double _Complex cosh( long double _Complex );
5098
5099float tanh( float )\indexc{tanh}§
5100double tanh( double );
5101long double tanh( long double );
5102float _Complex tanh( float _Complex );
5103double _Complex tanh( double _Complex );
5104long double _Complex tanh( long double _Complex );
5105
5106float asinh( float )\indexc{asinh}§
5107double asinh( double );
5108long double asinh( long double );
5109float _Complex asinh( float _Complex );
5110double _Complex asinh( double _Complex );
5111long double _Complex asinh( long double _Complex );
5112
5113float acosh( float )\indexc{acosh}§
5114double acosh( double );
5115long double acosh( long double );
5116float _Complex acosh( float _Complex );
5117double _Complex acosh( double _Complex );
5118long double _Complex acosh( long double _Complex );
5119
5120float atanh( float )\indexc{atanh}§
5121double atanh( double );
5122long double atanh( long double );
5123float _Complex atanh( float _Complex );
5124double _Complex atanh( double _Complex );
5125long double _Complex atanh( long double _Complex );
5126\end{cfa}
5127
5128
5129\subsection{Error / Gamma}
5130
5131\leavevmode
5132\begin{cfa}[aboveskip=0pt,belowskip=0pt]
5133float erf( float )\indexc{erf}§
5134double erf( double );
5135long double erf( long double );
5136float _Complex erf( float _Complex );
5137double _Complex erf( double _Complex );
5138long double _Complex erf( long double _Complex );
5139
5140float erfc( float )\indexc{erfc}§
5141double erfc( double );
5142long double erfc( long double );
5143float _Complex erfc( float _Complex );
5144double _Complex erfc( double _Complex );
5145long double _Complex erfc( long double _Complex );
5146
5147float lgamma( float )\indexc{lgamma}§
5148double lgamma( double );
5149long double lgamma( long double );
5150float lgamma( float, int * );
5151double lgamma( double, int * );
5152long double lgamma( long double, int * );
5153
5154float tgamma( float )\indexc{tgamma}§
5155double tgamma( double );
5156long double tgamma( long double );
5157\end{cfa}
5158
5159
5160\subsection{Nearest Integer}
5161
5162\leavevmode
5163\begin{cfa}[aboveskip=0pt,belowskip=0pt]
5164float floor( float )\indexc{floor}§
5165double floor( double );
5166long double floor( long double );
5167
5168float ceil( float )\indexc{ceil}§
5169double ceil( double );
5170long double ceil( long double );
5171
5172float trunc( float )\indexc{trunc}§
5173double trunc( double );
5174long double trunc( long double );
5175
5176float rint( float )\indexc{rint}§
5177long double rint( long double );
5178long int rint( float );
5179long int rint( double );
5180long int rint( long double );
5181long long int rint( float );
5182long long int rint( double );
5183long long int rint( long double );
5184
5185long int lrint( float )\indexc{lrint}§
5186long int lrint( double );
5187long int lrint( long double );
5188long long int llrint( float );
5189long long int llrint( double );
5190long long int llrint( long double );
5191
5192float nearbyint( float )\indexc{nearbyint}§
5193double nearbyint( double );
5194long double nearbyint( long double );
5195
5196float round( float )\indexc{round}§
5197long double round( long double );
5198long int round( float );
5199long int round( double );
5200long int round( long double );
5201long long int round( float );
5202long long int round( double );
5203long long int round( long double );
5204
5205long int lround( float )\indexc{lround}§
5206long int lround( double );
5207long int lround( long double );
5208long long int llround( float );
5209long long int llround( double );
5210long long int llround( long double );
5211\end{cfa}
5212
5213
5214\subsection{Manipulation}
5215
5216\leavevmode
5217\begin{cfa}[aboveskip=0pt,belowskip=0pt]
5218float copysign( float, float )\indexc{copysign}§
5219double copysign( double, double );
5220long double copysign( long double, long double );
5221
5222float frexp( float, int * )\indexc{frexp}§
5223double frexp( double, int * );
5224long double frexp( long double, int * );
5225
5226float ldexp( float, int )\indexc{ldexp}§
5227double ldexp( double, int );
5228long double ldexp( long double, int );
5229
5230[ float, float ] modf( float )\indexc{modf}§
5231float modf( float, float * );
5232[ double, double ] modf( double );
5233double modf( double, double * );
5234[ long double, long double ] modf( long double );
5235long double modf( long double, long double * );
5236
5237float nextafter( float, float )\indexc{nextafter}§
5238double nextafter( double, double );
5239long double nextafter( long double, long double );
5240
5241float nexttoward( float, long double )\indexc{nexttoward}§
5242double nexttoward( double, long double );
5243long double nexttoward( long double, long double );
5244
5245float scalbn( float, int )\indexc{scalbn}§
5246double scalbn( double, int );
5247long double scalbn( long double, int );
5248
5249float scalbln( float, long int )\indexc{scalbln}§
5250double scalbln( double, long int );
5251long double scalbln( long double, long int );
5252\end{cfa}
5253
5254
5255\section{Rational Numbers}
5256\label{s:RationalNumbers}
5257
5258Rational numbers are numbers written as a ratio, \ie as a fraction, where the numerator (top number) and the denominator (bottom number) are whole numbers.
5259When creating and computing with rational numbers, results are constantly reduced to keep the numerator and denominator as small as possible.
5260
5261\begin{cfa}[belowskip=0pt]
5262// implementation
5263struct Rational {§\indexc{Rational}§
5264        long int numerator, denominator;                                        // invariant: denominator > 0
5265}; // Rational
5266
5267// constants
5268extern struct Rational 0;
5269extern struct Rational 1;
5270
5271// constructors
5272Rational rational();
5273Rational rational( long int n );
5274Rational rational( long int n, long int d );
5275
5276// getter/setter for numerator/denominator
5277long int numerator( Rational r );
5278long int numerator( Rational r, long int n );
5279long int denominator( Rational r );
5280long int denominator( Rational r, long int d );
5281
5282// comparison
5283int ?==?( Rational l, Rational r );
5284int ?!=?( Rational l, Rational r );
5285int ?<?( Rational l, Rational r );
5286int ?<=?( Rational l, Rational r );
5287int ?>?( Rational l, Rational r );
5288int ?>=?( Rational l, Rational r );
5289
5290// arithmetic
5291Rational -?( Rational r );
5292Rational ?+?( Rational l, Rational r );
5293Rational ?-?( Rational l, Rational r );
5294Rational ?*?( Rational l, Rational r );
5295Rational ?/?( Rational l, Rational r );
5296
5297// conversion
5298double widen( Rational r );
5299Rational narrow( double f, long int md );
5300
5301// I/O
5302forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * );
5303forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational );
5304\end{cfa}
5305
5306
5307\bibliographystyle{plain}
5308\bibliography{cfa}
5309
5310
5311\addcontentsline{toc}{section}{\indexname} % add index name to table of contents
5312\begin{theindex}
5313Italic page numbers give the location of the main entry for the referenced term.
5314Plain page numbers denote uses of the indexed term.
5315Entries for grammar non-terminals are italicized.
5316A typewriter font is used for grammar terminals and program identifiers.
5317\indexspace
5318\input{user.ind}
5319\end{theindex}
5320
5321
5322\end{document}
5323
5324% Local Variables: %
5325% tab-width: 4 %
5326% fill-column: 100 %
5327% compile-command: "make" %
5328% End: %
Note: See TracBrowser for help on using the repository browser.