Changes in doc/user/user.tex [c4e3419c:128c4e4]
- File:
-
- 1 edited
-
doc/user/user.tex (modified) (270 diffs)
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
rc4e3419c r128c4e4 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Feb 15 13:48:53 202114 %% Update Count : 445213 %% Last Modified On : Mon Oct 5 08:57:29 2020 14 %% Update Count : 3998 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 37 37 \usepackage{mathptmx} % better math font with "times" 38 38 \usepackage[usenames]{color} 39 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}40 \usepackage{breakurl}41 42 \renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt}43 44 \usepackage[pagewise]{lineno}45 \renewcommand{\linenumberfont}{\scriptsize\sffamily}46 \usepackage[firstpage]{draftwatermark}47 \SetWatermarkLightness{0.9}48 49 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore50 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR51 % AFTER HYPERREF.52 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}}53 54 \setlength{\topmargin}{-0.45in} % move running title into header55 \setlength{\headsep}{0.25in}56 57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%58 59 39 \newcommand{\CFALatin}{} 60 40 % inline code ©...© (copyright symbol) emacs: C-q M-) … … 66 46 % math escape $...$ (dollar symbol) 67 47 \input{common} % common CFA document macros 48 \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref} 49 \usepackage{breakurl} 50 51 \renewcommand\footnoterule{\kern -3pt\rule{0.3\linewidth}{0.15pt}\kern 2pt} 52 53 \usepackage[pagewise]{lineno} 54 \renewcommand{\linenumberfont}{\scriptsize\sffamily} 55 \usepackage[firstpage]{draftwatermark} 56 \SetWatermarkLightness{0.9} 57 58 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore 59 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR 60 % AFTER HYPERREF. 61 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}} 62 63 \setlength{\topmargin}{-0.45in} % move running title into header 64 \setlength{\headsep}{0.25in} 65 66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 67 68 68 \CFAStyle % use default CFA format-style 69 \lstset{language=CFA} % CFA default lnaguage70 69 \lstnewenvironment{C++}[1][] % use C++ style 71 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{ @}{@},#1}}70 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®},#1}} 72 71 {} 73 72 … … 82 81 \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}} 83 82 \newcommand{\R}[1]{\Textbf{#1}} 84 \newcommand{\RC}[1]{\Textbf{\LstBasicStyle{#1}}}85 83 \newcommand{\B}[1]{{\Textbf[blue]{#1}}} 86 84 \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}} … … 105 103 106 104 \author{ 107 \huge \CFA Team (past and present) \medskip \\ 108 \Large Andrew Beach, Richard Bilson, Michael Brooks, Peter A. Buhr, Thierry Delisle, \smallskip \\ 109 \Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Colby Parsons, Rob Schluntz, \smallskip \\ 110 \Large Fangren Yu, Mubeen Zulfiqar 105 \huge \CFA Team \medskip \\ 106 \Large Andrew Beach, Richard Bilson, Peter A. Buhr, Thierry Delisle, \smallskip \\ 107 \Large Glen Ditchfield, Rodolfo G. Esteves, Aaron Moss, Rob Schluntz 111 108 }% author 112 109 … … 129 126 \vspace*{\fill} 130 127 \noindent 131 \copyright\,2016 , 2018, 2021\CFA Project \\ \\128 \copyright\,2016 \CFA Project \\ \\ 132 129 \noindent 133 130 This work is licensed under the Creative Commons Attribution 4.0 International License. … … 147 144 \section{Introduction} 148 145 149 \CFA{}\index{cforall@\CFA}\footnote{Pronounced ``\Index*{C-for-all}'', and written \CFA, CFA, or \CFL.} is a modern general-purpose concurrentprogramming-language, designed as an evolutionary step forward for the C programming language.146 \CFA{}\index{cforall@\CFA}\footnote{Pronounced ``\Index*{C-for-all}'', and written \CFA, CFA, or \CFL.} is a modern general-purpose programming-language, designed as an evolutionary step forward for the C programming language. 150 147 The syntax of \CFA builds from C and should look immediately familiar to C/\Index*[C++]{\CC{}} programmers. 151 148 % Any language feature that is not described here can be assumed to be using the standard \Celeven syntax. 152 \CFA adds many modern features that directly lead to increased \emph{\Index{safety}} and \emph{\Index{productivity}}, while maintaining interoperability with existing C programs and achieving similar performance.149 \CFA adds many modern programming-language features that directly lead to increased \emph{\Index{safety}} and \emph{\Index{productivity}}, while maintaining interoperability with existing C programs and achieving similar performance. 153 150 Like C, \CFA is a statically typed, procedural (non-\Index{object-oriented}) language with a low-overhead runtime, meaning there is no global \Index{garbage-collection}, but \Index{regional garbage-collection}\index{garbage-collection!regional} is possible. 154 151 The primary new features include polymorphic routines and types, exceptions, concurrency, and modules. … … 160 157 instead, a programmer evolves a legacy program into \CFA by incrementally incorporating \CFA features. 161 158 As well, new programs can be written in \CFA using a combination of C and \CFA features. 162 In many ways, \CFA is to C as \Index{Scala}~\cite{Scala} is to Java, providing a vehicle for new typing and control-flow capabilities on top of a highly popular programming language allowing immediate dissemination.163 159 164 160 \Index*[C++]{\CC{}}~\cite{c++:v1} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C. … … 169 165 For example, the following programs compare the C, \CFA, and \CC I/O mechanisms, where the programs output the same result. 170 166 \begin{center} 171 \begin{tabular}{@{}l@{\hspace{1 em}}l@{\hspace{1em}}l@{}}172 \multicolumn{1}{c@{\hspace{1 em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\173 \begin{cfa} 174 #include <stdio.h> $\indexc{stdio.h}$167 \begin{tabular}{@{}l@{\hspace{1.5em}}l@{\hspace{1.5em}}l@{}} 168 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ 169 \begin{cfa} 170 #include <stdio.h>§\indexc{stdio.h}§ 175 171 176 172 int main( void ) { 177 173 int x = 0, y = 1, z = 2; 178 @printf( "%d %d %d\n", x, y, z );@174 ®printf( "%d %d %d\n", x, y, z );® 179 175 } 180 176 \end{cfa} 181 177 & 182 178 \begin{cfa} 183 #include <fstream> $\indexc{fstream}$179 #include <fstream>§\indexc{fstream}§ 184 180 185 181 int main( void ) { 186 182 int x = 0, y = 1, z = 2; 187 @sout | x | y | z;@$\indexc{sout}$183 ®sout | x | y | z;®§\indexc{sout}§ 188 184 } 189 185 \end{cfa} 190 186 & 191 187 \begin{cfa} 192 #include <iostream> $\indexc{iostream}$188 #include <iostream>§\indexc{iostream}§ 193 189 using namespace std; 194 190 int main() { 195 191 int x = 0, y = 1, z = 2; 196 @cout<<x<<" "<<y<<" "<<z<<endl;@192 ®cout<<x<<" "<<y<<" "<<z<<endl;® 197 193 } 198 194 \end{cfa} 199 195 \end{tabular} 200 196 \end{center} 201 While \CFA I/O \see{\VRef{s:StreamIOLibrary}} looks similar to \Index*[C++]{\CC{}}, there are important differences, such as automatic spacing between variables and an implicit newline at the end of the expression list, similar to \Index*{Python}~\cite{Python}.197 While 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~\VRef{s:IOLibrary}). 202 198 203 199 … … 214 210 \section{Why fix C?} 215 211 216 The C programming language is a foundational technology for modern computing with billions of lines of code implementing everything from hobby projects to commercial operating-systems.212 The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from hobby projects to commercial operating-systems. 217 213 This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more. 218 214 Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction. 219 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is the only language of choice.220 The TIOBE index~\cite{TIOBE} for February 202 1 ranks the top six most \emph{popular} programming languages as C 17.4\%, \Index*{Java} 12\%, Python 12\%, \Index*[C++]{\CC{}} 7.6\%, \Csharp 4\%, Visual Basic 3.8\% = 56.8\%, where the next 50 languages are less than 2\% each, with a long tail.215 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice. 216 The TIOBE index~\cite{TIOBE} for February 2020 ranks the top six most \emph{popular} programming languages as \Index*{Java} 17.4\%, C 16.8\%, Python 9.3\%, \Index*[C++]{\CC{}} 6.2\%, \Csharp 5.9\%, Visual Basic 5.9\% = 61.5\%, where the next 50 languages are less than 2\% each, with a long tail. 221 217 The top 4 rankings over the past 35 years are: 222 218 \begin{center} 223 219 \setlength{\tabcolsep}{10pt} 224 220 \begin{tabular}{@{}rcccccccc@{}} 225 & 202 1 & 2016 & 2011 & 2006 & 2001 & 1996 & 1991 & 1986\\ \hline226 \R{C} & \R{1} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} & \R{1} & \R{1}\\227 Java & 2 & 1 & 1 & 2 & 3 & 28 & - & -\\228 Python & 3 & 5 & 6 & 7 & 23 & 13& - & - \\229 \CC & 4 & 3 & 3 & 3 & 2 & 2 & 2 & 8\\221 & 2020 & 2015 & 2010 & 2005 & 2000 & 1995 & 1990 & 1985 \\ \hline 222 Java & 1 & 2 & 1 & 2 & 3 & - & - & - \\ 223 \R{C} & \R{2} & \R{1} & \R{2} & \R{1} & \R{1} & \R{2} & \R{1} & \R{1} \\ 224 Python & 3 & 7 & 6 & 6 & 22 & 21 & - & - \\ 225 \CC & 4 & 4 & 4 & 3 & 2 & 1 & 2 & 12 \\ 230 226 \end{tabular} 231 227 \end{center} … … 236 232 As stated, the goal of the \CFA project is to engineer modern language-features into C in an evolutionary rather than revolutionary way. 237 233 \CC~\cite{C++14,C++} is an example of a similar project; 238 however, it largely extended the C language, and did not address m anyof C's existing problems.\footnote{%234 however, it largely extended the C language, and did not address most of C's existing problems.\footnote{% 239 235 Two 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.} 240 236 \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. … … 245 241 246 242 The result of this project is a language that is largely backwards compatible with \Index*[C11]{\Celeven{}}~\cite{C11}, but fixes many of the well known C problems while adding modern language-features. 247 To achieve these goals required a significant engineering exercise, \ie ``thinking \emph{inside} the C box''. 248 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern language. 243 To achieve these goals required a significant engineering exercise, where we had to ``think inside the existing C box''. 244 Without these significant extension to C, it is unable to cope with the needs of modern programming problems and programmers; 245 as a result, it will fade into disuse. 246 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language. 249 247 While \Index*[C11]{\Celeven{}} 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. 250 248 While 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. … … 253 251 \section{History} 254 252 255 The \CFA project started with \Index*{Dave Till}\index{Till, Dave}'s \Index*{K-W C}~\cite{Buhr94a,Till89}, which extended C with new declaration syntax, multiple return values from routines, and advanced assignment capabilities using the notion of tuples \see{\cite{Werther96} for similar work in \Index*[C++]{\CC{}}}. 253 The \CFA project started with \Index*{Dave Till}\index{Till, Dave}'s \Index*{K-W C}~\cite{Buhr94a,Till89}, which extended C with new declaration syntax, multiple return values from routines, and advanced assignment capabilities using the notion of tuples. 254 (See~\cite{Werther96} for similar work in \Index*[C++]{\CC{}}.) 256 255 The first \CFA implementation of these extensions was by \Index*{Rodolfo Esteves}\index{Esteves, Rodolfo}~\cite{Esteves04}. 257 256 258 257 The signature feature of \CFA is \emph{\Index{overload}able} \Index{parametric-polymorphic} functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name): 259 258 \begin{cfa} 260 @forall( otype T )@T identity( T val ) { return val; }261 int forty_two = identity( 42 ); $\C{// T is bound to int, forty\_two == 42}$259 ®forall( otype T )® T identity( T val ) { return val; } 260 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§ 262 261 \end{cfa} 263 262 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. 264 263 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by \Index*{Glen Ditchfield}\index{Ditchfield, Glen}~\cite{Ditchfield92}, and first implemented by \Index*{Richard Bilson}\index{Bilson, Richard}~\cite{Bilson03}. 265 264 However, at that time, there was little interesting in extending C, so work did not continue. 266 As the saying goes, ``\Index*{What goes around, comes around.}'', and there is now renewed interest in the C programming language because of the legacy code-base, so the \CFA project was restarted in 2015.265 As the saying goes, ``\Index*{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. 267 266 268 267 … … 274 273 This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of external software features. 275 274 Language developers often state that adequate \Index{library support} takes more work than designing and implementing the language itself. 276 Fortunately, \CFA, like \Index*[C++]{\CC{}}, starts with immediate access to all exiting C libraries, and in many cases, can easily wrap library routines with simpler and safer interfaces, at zero orvery low cost.275 Fortunately, \CFA, like \Index*[C++]{\CC{}}, 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. 277 276 Hence, \CFA begins by leveraging the large repository of C libraries, and than allows programmers to incrementally augment their C programs with modern \Index{backward-compatible} features. 278 277 … … 287 286 288 287 double key = 5.0, vals[10] = { /* 10 sorted floating values */ }; 289 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); $\C{// search sorted array}$288 double * val = (double *)bsearch( &key, vals, 10, sizeof(vals[0]), comp ); §\C{// search sorted array}§ 290 289 \end{cfa} 291 290 which can be augmented simply with a polymorphic, type-safe, \CFA-overloaded wrappers: … … 296 295 297 296 forall( otype T | { int ?<?( T, T ); } ) unsigned int bsearch( T key, const T * arr, size_t size ) { 298 T * result = bsearch( key, arr, size ); $\C{// call first version}$299 return result ? result - arr : size; } $\C{// pointer subtraction includes sizeof(T)}$300 301 double * val = bsearch( 5.0, vals, 10 ); $\C{// selection based on return type}$297 T * result = bsearch( key, arr, size ); §\C{// call first version}§ 298 return result ? result - arr : size; } §\C{// pointer subtraction includes sizeof(T)}§ 299 300 double * val = bsearch( 5.0, vals, 10 ); §\C{// selection based on return type}§ 302 301 int posn = bsearch( 5.0, vals, 10 ); 303 302 \end{cfa} … … 311 310 \begin{cfa} 312 311 forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); } 313 int * ip = malloc(); $\C{// select type and size from left-hand side}$312 int * ip = malloc(); §\C{// select type and size from left-hand side}§ 314 313 double * dp = malloc(); 315 314 struct S {...} * sp = malloc(); … … 320 319 However, it is necessary to differentiate between C and \CFA code because of name \Index{overload}ing, as for \CC. 321 320 For 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©. 322 Whereas, \CFA wraps each of these routines into one overloaded name ©abs©: 323 \begin{cfa} 324 char @abs@( char ); 325 extern "C" { int @abs@( int ); } $\C{// use default C routine for int}$ 326 long int @abs@( long int ); 327 long long int @abs@( long long int ); 328 float @abs@( float ); 329 double @abs@( double ); 330 long double @abs@( long double ); 331 float _Complex @abs@( float _Complex ); 332 double _Complex @abs@( double _Complex ); 333 long double _Complex @abs@( long double _Complex ); 334 \end{cfa} 335 The problem is \Index{name clash} between the C name ©abs© and the \CFA names ©abs©, resulting in two name linkages\index{C linkage}: ©extern "C"© and ©extern "Cforall"© (default). 336 Overloaded names must use \newterm{name mangling}\index{mangling!name} to create unique names that are different from unmangled C names. 337 Hence, 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. 338 The only way around this problem is C's approach of creating unique names for each pairing of operation and type. 339 340 This example illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. 321 Whereas, \CFA wraps each of these routines into ones with the overloaded name ©abs©: 322 \begin{cfa} 323 char ®abs®( char ); 324 extern "C" { int ®abs®( int ); } §\C{// use default C routine for int}§ 325 long int ®abs®( long int ); 326 long long int ®abs®( long long int ); 327 float ®abs®( float ); 328 double ®abs®( double ); 329 long double ®abs®( long double ); 330 float _Complex ®abs®( float _Complex ); 331 double _Complex ®abs®( double _Complex ); 332 long double _Complex ®abs®( long double _Complex ); 333 \end{cfa} 334 The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©. 335 Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}. 336 Then 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. 337 Hence, 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. 338 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and types. 339 340 This example strongly illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. 341 341 The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value. 342 342 Hence, knowing the name ©abs© is sufficient to apply it to any type where it is applicable. … … 344 344 345 345 346 \section {\CFA Compilation}346 \section[Compiling a CFA Program]{Compiling a \CFA Program} 347 347 348 348 The command ©cfa© is used to compile a \CFA program and is based on the \Index{GNU} \Indexc{gcc} command, \eg: 349 349 \begin{cfa} 350 cfa$\indexc{cfa}\index{compilation!cfa@©cfa©}$ [ gcc/$\CFA{}$-options ] [ C/$\CFA{}$ source-files ] [ assembler/loader files ] 351 \end{cfa} 352 There is no ordering among options (flags) and files, unless an option has an argument, which must appear immediately after the option possibly with or without a space separating option and argument. 353 354 \CFA has the following ©gcc© flags turned on: 355 \begin{description}[topsep=0pt] 350 cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] [ C/§\CFA{}§ source-files ] [ assembler/loader files ] 351 \end{cfa} 352 \CFA programs having the following ©gcc© flags turned on: 353 \begin{description} 356 354 \item 357 355 \Indexc{-std=gnu11}\index{compilation option!-std=gnu11@{©-std=gnu11©}} … … 361 359 Use the traditional GNU semantics for inline routines in C11 mode, which allows inline routines in header files. 362 360 \end{description} 363 364 \CFA has the following new options: 365 \begin{description}[topsep=0pt] 361 The following new \CFA options are available: 362 \begin{description} 366 363 \item 367 364 \Indexc{-CFA}\index{compilation option!-CFA@©-CFA©} 368 Only the C preprocessor (flag ©-E©)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.365 Only 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. 369 366 The generated code starts with the standard \CFA \Index{prelude}. 370 371 \item372 \Indexc{-XCFA}\index{compilation option!-XCFA@©-XCFA©}373 Pass next flag as-is to the ©cfa-cpp© translator (see details below).374 367 375 368 \item 376 369 \Indexc{-debug}\index{compilation option!-debug@©-debug©} 377 370 The program is linked with the debugging version of the runtime system. 378 The debug version performs runtime checks to aidthe debugging phase of a \CFA program, but can substantially slow program execution.371 The debug version performs runtime checks to help during the debugging phase of a \CFA program, but can substantially slow program execution. 379 372 The runtime checks should only be removed after the program is completely debugged. 380 373 \textbf{This option is the default.} … … 406 399 \item 407 400 \Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@©-no-include-stdhdr©} 408 Do not supply ©extern "C"© wrappers for \Celeven standard include files \see{\VRef{s:StandardHeaders}}.401 Do not supply ©extern "C"© wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}). 409 402 \textbf{This option is \emph{not} the default.} 410 403 \end{comment} … … 437 430 \begin{cfa} 438 431 #ifndef __CFORALL__ 439 #include <stdio.h> $\indexc{stdio.h}$ $\C{// C header file}$432 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§ 440 433 #else 441 #include <fstream> $\indexc{fstream}$ $\C{// \CFA header file}$434 #include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§ 442 435 #endif 443 436 \end{cfa} … … 445 438 446 439 The \CFA translator has multiple steps. 447 The following flags control how the tran slator works, the stages run, and printing within a stage.440 The following flags control how the tranlator works, the stages run, and printing within a stage. 448 441 The majority of these flags are used by \CFA developers, but some are occasionally useful to programmers. 449 Each option must be escaped with \Indexc{-XCFA}\index{translator option!-XCFA@{©-XCFA©}} to direct it to the compiler step, similar to the ©-Xlinker© flag for the linker, \eg:450 \begin{lstlisting}[language=sh]451 cfa $test$.cfa -CFA -XCFA -p # print translated code without printing the standard prelude452 cfa $test$.cfa -XCFA -P -XCFA parse -XCFA -n # show program parse without prelude453 \end{lstlisting}454 442 \begin{description}[topsep=5pt,itemsep=0pt,parsep=0pt] 455 443 \item 456 \Indexc{-c}\index{translator option!-c@{©-c©}}, \Indexc{--colors}\index{translator option!--colors@{©--colors©}} \, diagnostic color: ©never©, ©always©, \lstinline[deletekeywords=auto]{auto} 457 \item 458 \Indexc{-g}\index{translator option!-g@{©-g©}}, \Indexc{--gdb}\index{translator option!--gdb@{©--gdb©}} \, wait for gdb to attach 459 \item 460 \Indexc{-h}\index{translator option!-h@{©-h©}}, \Indexc{--help}\index{translator option!--help@{©--help©}} \, print translator help message 461 \item 462 \Indexc{-l}\index{translator option!-l@{©-l©}}, \Indexc{--libcfa}\index{translator option!--libcfa@{©--libcfa©}} \, generate ©libcfa.c© 444 \Indexc{-h}\index{translator option!-h@{©-h©}}, \Indexc{--help}\index{translator option!--help@{©--help©}} \, print help message 445 \item 446 \Indexc{-l}\index{translator option!-l@{©-l©}}, \Indexc{--libcfa}\index{translator option!--libcfa@{©--libcfa©}} \, generate libcfa.c 463 447 \item 464 448 \Indexc{-L}\index{translator option!-L@{©-L©}}, \Indexc{--linemarks}\index{translator option!--linemarks@{©--linemarks©}} \, generate line marks … … 470 454 \Indexc{-n}\index{translator option!-n@{©-n©}}, \Indexc{--no-prelude}\index{translator option!--no-prelude@{©--no-prelude©}} \, do not read prelude 471 455 \item 472 \Indexc{-p}\index{translator option!-p@{©-p©}}, \Indexc{--prototypes}\index{translator option!--prototypes@{©--prototypes©}} \, do not generate prelude prototypes $\Rightarrow$ prelude not printed 473 \item 474 \Indexc{-d}\index{translator option!-d@{©-d©}}, \Indexc{--deterministic-out}\index{translator option!--deterministic-out@{©--deterministic-out©}} \, only print deterministic output 456 \Indexc{-p}\index{translator option!-p@{©-p©}}, \Indexc{--prototypes}\index{translator option!--prototypes@{©--prototypes©}} \, generate prototypes for prelude functions 475 457 \item 476 458 \Indexc{-P}\index{translator option!-P@{©-P©}}, \Indexc{--print}\index{translator option!--print@{©--print©}} \, one of: 477 459 \begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt] 478 460 \item 461 \Indexc{altexpr}\index{translator option!-P@{©-P©}!©altexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, alternatives for expressions 462 \item 479 463 \Indexc{ascodegen}\index{translator option!-P@{©-P©}!©ascodegen©}\index{translator option!--print@{©-print©}!©ascodegen©} \, as codegen rather than AST 480 464 \item 465 \Indexc{ast}\index{translator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, AST after parsing 466 \item 467 \Indexc{astdecl}\index{translator option!-P@{©-P©}!©astdecl©}\index{translator option!--print@{©-print©}!©astdecl©} \, AST after declaration validation pass 468 \item 481 469 \Indexc{asterr}\index{translator option!-P@{©-P©}!©asterr©}\index{translator option!--print@{©-print©}!©asterr©} \, AST on error 482 470 \item 471 \Indexc{astexpr}\index{translator option!-P@{©-P©}!©astexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, AST after expression analysis 472 \item 473 \Indexc{astgen}\index{translator option!-P@{©-P©}!©astgen©}\index{translator option!--print@{©-print©}!©astgen©} \, AST after instantiate generics 474 \item 475 \Indexc{box}\index{translator option!-P@{©-P©}!©box©}\index{translator option!--print@{©-print©}!©box©} \, before box step 476 \item 477 \Indexc{ctordtor}\index{translator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, after ctor/dtor are replaced 478 \item 479 \Indexc{codegen}\index{translator option!-P@{©-P©}!©codegen©}\index{translator option!--print@{©-print©}!©codegen©} \, before code generation 480 \item 483 481 \Indexc{declstats}\index{translator option!-P@{©-P©}!©declstats©}\index{translator option!--print@{©-print©}!©declstats©} \, code property statistics 484 482 \item 485 483 \Indexc{parse}\index{translator option!-P@{©-P©}!©parse©}\index{translator option!--print@{©-print©}!©parse©} \, yacc (parsing) debug information 486 484 \item 487 \Indexc{pretty}\index{translator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ©ascodegen© flag 485 \Indexc{pretty}\index{translator option!-P@{©-P©}!©pretty©}\index{translator option!--print@{©-print©}!©pretty©} \, prettyprint for ascodegen flag 486 \item 487 \Indexc{resolver}\index{translator option!-P@{©-P©}!©resolver©}\index{translator option!--print@{©-print©}!©resolver©} \, before resolver step 488 488 \item 489 489 \Indexc{rproto}\index{translator option!-P@{©-P©}!©rproto©}\index{translator option!--print@{©-print©}!©rproto©} \, resolver-proto instance … … 491 491 \Indexc{rsteps}\index{translator option!-P@{©-P©}!©rsteps©}\index{translator option!--print@{©-print©}!©rsteps©} \, resolver steps 492 492 \item 493 \Indexc{symevt}\index{translator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, symbol table events 494 \item 493 495 \Indexc{tree}\index{translator option!-P@{©-P©}!©tree©}\index{translator option!--print@{©-print©}!©tree©} \, parse tree 494 496 \item 495 \Indexc{ast}\index{translator option!-P@{©-P©}!©ast©}\index{translator option!--print@{©-print©}!©ast©} \, AST after parsing496 \item497 \Indexc{symevt}\index{translator option!-P@{©-P©}!©symevt©}\index{translator option!--print@{©-print©}!©symevt©} \, symbol table events498 \item499 \Indexc{altexpr}\index{translator option!-P@{©-P©}!©altexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, alternatives for expressions500 \item501 \Indexc{astdecl}\index{translator option!-P@{©-P©}!©astdecl©}\index{translator option!--print@{©-print©}!©astdecl©} \, AST after declaration validation pass502 \item503 \Indexc{resolver}\index{translator option!-P@{©-P©}!©resolver©}\index{translator option!--print@{©-print©}!©resolver©} \, before resolver step504 \item505 \Indexc{astexpr}\index{translator option!-P@{©-P©}!©astexpr©}\index{translator option!--print@{©-print©}!©altexpr©} \, AST after expression analysis506 \item507 \Indexc{ctordtor}\index{translator option!-P@{©-P©}!©ctordtor©}\index{translator option!--print@{©-print©}!©ctordtor©} \, after ctor/dtor are replaced508 \item509 497 \Indexc{tuple}\index{translator option!-P@{©-P©}!©tuple©}\index{translator option!--print@{©-print©}!©tuple©} \, after tuple expansion 510 \item511 \Indexc{astgen}\index{translator option!-P@{©-P©}!©astgen©}\index{translator option!--print@{©-print©}!©astgen©} \, AST after instantiate generics512 \item513 \Indexc{box}\index{translator option!-P@{©-P©}!©box©}\index{translator option!--print@{©-print©}!©box©} \, before box step514 \item515 \Indexc{codegen}\index{translator option!-P@{©-P©}!©codegen©}\index{translator option!--print@{©-print©}!©codegen©} \, before code generation516 498 \end{description} 517 499 \item 518 500 \Indexc{--prelude-dir} <directory> \, prelude directory for debug/nodebug 519 501 \item 520 \Indexc{-S}\index{translator option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{translator option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: ©counters©, ©heap©, ©time©, ©all©, ©none© 502 \Indexc{-S}\index{translator option!-S@{©-S©}!©counters,heap,time,all,none©}, \Indexc{--statistics}\index{translator option!--statistics@{©--statistics©}!©counters,heap,time,all,none©} <option-list> \, enable profiling information: 503 \begin{description}[topsep=0pt,itemsep=0pt,parsep=0pt] 504 \item 505 \Indexc{counters,heap,time,all,none} 506 \end{description} 521 507 \item 522 508 \Indexc{-t}\index{translator option!-t@{©-t©}}, \Indexc{--tree}\index{translator option!--tree@{©--tree©}} build in tree … … 527 513 \label{s:BackquoteIdentifiers} 528 514 529 \CFA introduces several new keywords \see{\VRef{s:CFAKeywords}}that can clash with existing C variable-names in legacy code.515 \CFA introduces several new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code. 530 516 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 531 517 \begin{cfa} 532 int @``@otype = 3; $\C{// make keyword an identifier}$533 double @``@forall = 3.5;518 int ®``®otype = 3; §\C{// make keyword an identifier}§ 519 double ®``®forall = 3.5; 534 520 \end{cfa} 535 521 536 522 Existing C programs with keyword clashes can be converted by enclosing keyword identifiers in backquotes, and eventually the identifier name can be changed to a non-keyword name. 537 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files \see{\VRef{s:StandardHeaders}}can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©.523 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©. 538 524 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience. 539 525 … … 541 527 \begin{cfa} 542 528 // include file uses the CFA keyword "with". 543 #if ! defined( with ) $\C{// nesting ?}$544 #define with @``@with $\C{// make keyword an identifier}$529 #if ! defined( with ) §\C{// nesting ?}§ 530 #define with ®``®with §\C{// make keyword an identifier}§ 545 531 #define __CFA_BFD_H__ 546 532 #endif 547 $\R{\#include\_next} <bfdlink.h>$ $\C{// must have internal check for multiple expansion}$ 548 #if defined( with ) && defined( __CFA_BFD_H__ ) $\C{// reset only if set}$533 §{\color{red}\#\textbf{include\_next} <bfdlink.h>}§ §\C{// must have internal check for multiple expansion}§ 534 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§ 549 535 #undef with 550 536 #undef __CFA_BFD_H__ … … 558 544 \section{Constant Underscores} 559 545 560 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore} as a separator, \eg:561 \begin{cfa} 562 2 @_@147@_@483@_@648; $\C{// decimal constant}$563 56 @_@ul; $\C{// decimal unsigned long constant}$564 0 @_@377; $\C{// octal constant}$565 0x @_@ff@_@ff; $\C{// hexadecimal constant}$566 0x @_@ef3d@_@aa5c; $\C{// hexadecimal constant}$567 3.141 @_@592@_@654; $\C{// floating constant}$568 10 @_@e@_@+1@_@00; $\C{// floating constant}$569 0x @_@ff@_@ff@_@p@_@3; $\C{// hexadecimal floating}$570 0x @_@1.ffff@_@ffff@_@p@_@128@_@l; $\C{// hexadecimal floating long constant}$571 L @_@$"\texttt{\textbackslash{x}}$@_@$\texttt{ff}$@_@$\texttt{ee}"$; $\C{// wide character constant}$546 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore}, \eg: 547 \begin{cfa} 548 2®_®147®_®483®_®648; §\C{// decimal constant}§ 549 56®_®ul; §\C{// decimal unsigned long constant}§ 550 0®_®377; §\C{// octal constant}§ 551 0x®_®ff®_®ff; §\C{// hexadecimal constant}§ 552 0x®_®ef3d®_®aa5c; §\C{// hexadecimal constant}§ 553 3.141®_®592®_®654; §\C{// floating constant}§ 554 10®_®e®_®+1®_®00; §\C{// floating constant}§ 555 0x®_®ff®_®ff®_®p®_®3; §\C{// hexadecimal floating}§ 556 0x®_®1.ffff®_®ffff®_®p®_®128®_®l; §\C{// hexadecimal floating long constant}§ 557 L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§ 572 558 \end{cfa} 573 559 The rules for placement of underscores are: … … 588 574 It is significantly easier to read and enter long constants when they are broken up into smaller groupings (many cultures use comma and/or period among digits for the same purpose). 589 575 This extension is backwards compatible, matches with the use of underscore in variable names, and appears in \Index*{Ada} and \Index*{Java} 8. 590 \CC uses the single quote (©'©) as a separator, restricted within a sequence of digits, \eg ©0xaa©©'©©ff©, ©3.141©©'©©592E1©©'©©1©.591 576 592 577 593 578 \section{Exponentiation Operator} 594 579 595 C, \CC, and Java (and other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation.596 \CFA extends the basic operators with the exponentiation operator ©? ©\R{©\\©}©?©\index{?\\?@©?@\@?©} and ©?©\R{©\\©}©=?©\index{?\\=?@©@\@=?©}, as in, ©x ©\R{©\\©}© y© and ©x ©\R{©\\©}©= y©, which means $x^y$ and $x \leftarrow x^y$.597 The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©( w * (((int)x) \ ((int)y))) * z©.580 C, \CC, and Java (and many other programming languages) have no exponentiation operator\index{exponentiation!operator}\index{operator!exponentiation}, \ie $x^y$, and instead use a routine, like \Indexc{pow(x,y)}, to perform the exponentiation operation. 581 \CFA extends the basic operators with the exponentiation operator ©?®\®?©\index{?\\?@©?®\®?©} and ©?\=?©\index{?\\=?@©®\®=?©}, as in, ©x ®\® y© and ©x ®\®= y©, which means $x^y$ and $x \leftarrow x^y$. 582 The priority of the exponentiation operator is between the cast and multiplicative operators, so that ©w * (int)x \ (int)y * z© is parenthesized as ©((w * (((int)x) \ ((int)y))) * z)©. 598 583 599 584 There are exponentiation operators for integral and floating types, including the builtin \Index{complex} types. … … 602 587 Floating exponentiation\index{exponentiation!floating} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the exponent cannot be negative. 603 588 \begin{cfa} 604 sout | 1 @\@ 0 | 1 @\@ 1 | 2 @\@ 8 | -4 @\@ 3 | 5 @\@ 3 | 5 @\@ 32 | 5L @\@ 32 | 5L @\@ 64 | -4 @\@ -3 | -4.0 @\@ -3 | 4.0 @\@2.1605 | (1.0f+2.0fi) @\@(3.0f+2.0fi);606 1 1 256 -64 125 @0@ 3273344365508751233 @0@ @0@-0.015625 18.3791736799526 0.264715-1.1922i589 sout | 1 ®\® 0 | 1 ®\® 1 | 2 ®\® 8 | -4 ®\® 3 | 5 ®\® 3 | 5 ®\® 32 | 5L ®\® 32 | 5L ®\® 64 | -4 ®\® -3 | -4.0 ®\® -3 | 4.0 ®\® 2.1 590 | (1.0f+2.0fi) ®\® (3.0f+2.0fi); 591 1 1 256 -64 125 ®0® 3273344365508751233 ®0® ®0® -0.015625 18.3791736799526 0.264715-1.1922i 607 592 \end{cfa} 608 593 Note, ©5 \ 32© and ©5L \ 64© overflow, and ©-4 \ -3© is a fraction but stored in an integer so all three computations generate an integral zero. 609 Because exponentiation has higher priority than ©+©, parenthesis are necessary for exponentiation of \Index{complex constant}s or the expression is parsed as ©1.0f+©\R{©(©}©2.0fi \ 3.0f©\R{©)©}©+2.0fi©, requiring \R{©(©}©1.0f+2.0fi©\R{©)©}© \ ©\R{©(©}©3.0f+2.0fi©\R{©)©}. 610 594 Parenthesis are necessary for complex constants or the expression is parsed as ©1.0f+®(®2.0fi \ 3.0f®)®+2.0fi©. 611 595 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation version is available. 612 596 \begin{cfa} 613 forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T,T ); } )614 T ?@\@?(T ep, unsigned int y );615 forall( otype T | { void ?{}( T & this, one_t ); T ?*?( T,T ); } )616 T ?@\@?(T ep, unsigned long int y );597 forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) 598 OT ?®\®?( OT ep, unsigned int y ); 599 forall( otype OT | { void ?{}( OT & this, one_t ); OT ?*?( OT, OT ); } ) 600 OT ?®\®?( OT ep, unsigned long int y ); 617 601 \end{cfa} 618 602 The user type ©T© must define multiplication, one (©1©), and ©*©. … … 625 609 626 610 %\subsection{\texorpdfstring{\protect\lstinline@if@/\protect\lstinline@while@ Statement}{if Statement}} 627 \subsection{\texorpdfstring{\LstKeywordStyle{if} / \LstKeywordStyle{while} Statement}{if / while Statement}} 628 629 The ©if©/©while© expression allows declarations, similar to ©for© declaration expression.\footnote{ 630 Declarations in the ©do©-©while© condition are not useful because they appear after the loop body.} 631 \begin{cfa} 632 if ( @int x = f()@ ) ... $\C{// x != 0}$ 633 if ( @int x = f(), y = g()@ ) ... $\C{// x != 0 \&\& y != 0}$ 634 if ( @int x = f(), y = g(); x < y@ ) ... $\C{// relational expression}$ 635 if ( @struct S { int i; } x = { f() }; x.i < 4@ ) $\C{// relational expression}$ 636 637 while ( @int x = f()@ ) ... $\C{// x != 0}$ 638 while ( @int x = f(), y = g()@ ) ... $\C{// x != 0 \&\& y != 0}$ 639 while ( @int x = f(), y = g(); x < y@ ) ... $\C{// relational expression}$ 640 while ( @struct S { int i; } x = { f() }; x.i < 4@ ) ... $\C{// relational expression}$ 641 \end{cfa} 642 Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if©/©while© expression, and the results are combined using the logical ©&&© operator. 643 The scope of the declaration(s) is local to the ©if© statement but exist within both the \emph{then} and \emph{else} clauses. 644 \CC only provides a single declaration always compared ©!=© to 0. 611 \subsection{\texorpdfstring{\LstKeywordStyle{if}/\LstKeywordStyle{while} Statement}{if/while Statement}} 612 613 The ©if©/©while© expression allows declarations, similar to ©for© declaration expression. 614 (Does not make sense for ©do©-©while©.) 615 \begin{cfa} 616 if ( ®int x = f()® ) ... §\C{// x != 0}§ 617 if ( ®int x = f(), y = g()® ) ... §\C{// x != 0 \&\& y != 0}§ 618 if ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§ 619 if ( ®struct S { int i; } x = { f() }; x.i < 4® ) §\C{// relational expression}§ 620 621 while ( ®int x = f()® ) ... §\C{// x != 0}§ 622 while ( ®int x = f(), y = g()® ) ... §\C{// x != 0 \&\& y != 0}§ 623 while ( ®int x = f(), y = g(); x < y® ) ... §\C{// relational expression}§ 624 while ( ®struct S { int i; } x = { f() }; x.i < 4® ) ... §\C{// relational expression}§ 625 \end{cfa} 626 Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if©/©while© expression, and the results are combined using the logical ©&&© operator.\footnote{\CC only provides a single declaration always compared not equal to 0.} 627 The scope of the declaration(s) is local to the @if@ statement but exist within both the ``then'' and ``else'' clauses. 645 628 646 629 647 630 %\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}} 648 631 \subsection{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}} 649 \label{s:caseClause}650 632 651 633 C restricts the ©case© clause of a ©switch© statement to a single value. … … 658 640 \begin{cfa} 659 641 switch ( i ) { 660 case @1, 3, 5@:642 case ®1, 3, 5®: 661 643 ... 662 case @2, 4, 6@:644 case ®2, 4, 6®: 663 645 ... 664 646 } … … 688 670 \begin{cfa} 689 671 switch ( i ) { 690 case @1~5:@ $\C{// 1, 2, 3, 4, 5}$672 case ®1~5:® §\C{// 1, 2, 3, 4, 5}§ 691 673 ... 692 case @10~15:@ $\C{// 10, 11, 12, 13, 14, 15}$674 case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§ 693 675 ... 694 676 } … … 696 678 Lists of subranges are also allowed. 697 679 \begin{cfa} 698 case @1~5, 12~21, 35~42@:680 case ®1~5, 12~21, 35~42®: 699 681 \end{cfa} 700 682 … … 740 722 if ( argc == 3 ) { 741 723 // open output file 742 @// open input file743 @} else if ( argc == 2 ) {744 @// open input file (duplicate)745 746 @} else {724 ®// open input file 725 ®} else if ( argc == 2 ) { 726 ®// open input file (duplicate) 727 728 ®} else { 747 729 // usage message 748 730 } … … 751 733 \end{cquote} 752 734 In this example, case 2 is always done if case 3 is done. 753 This 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.735 This 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. 754 736 C also uses fall-through to handle multiple case-values resulting in the same action: 755 737 \begin{cfa} 756 738 switch ( i ) { 757 @case 1: case 3: case 5:@// odd values739 ®case 1: case 3: case 5:® // odd values 758 740 // odd action 759 741 break; 760 @case 2: case 4: case 6:@// even values742 ®case 2: case 4: case 6:® // even values 761 743 // even action 762 744 break; 763 745 } 764 746 \end{cfa} 765 This situation better handled without fall-through by allowing a list of case values \see{\VRef{s:caseClause}}.766 While 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 mostprogramming languages with a ©switch© statement.747 However, this situation is handled in other languages without fall-through by allowing a list of case values. 748 While 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. 767 749 Hence, default fall-through semantics results in a large number of programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through. 768 750 … … 774 756 if ( j < k ) { 775 757 ... 776 @case 1:@// transfer into "if" statement758 ®case 1:® // transfer into "if" statement 777 759 ... 778 760 } // if … … 780 762 while ( j < 5 ) { 781 763 ... 782 @case 3:@// transfer into "while" statement764 ®case 3:® // transfer into "while" statement 783 765 ... 784 766 } // while 785 767 } // switch 786 768 \end{cfa} 787 Th is usage branchesinto control structures, which is known to cause both comprehension and technical difficulties.788 The comprehension problem results from the inability to determine how control reaches a particular point due to the number of branches leading to it.769 The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties. 770 The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it. 789 771 The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning. 790 There are fewarguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it.772 There are no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 791 773 Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: 792 774 \begin{cfa} … … 812 794 \item 813 795 It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch© statement, rather than only at the end. 814 Mostprogramming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list.796 Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list. 815 797 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected; 816 798 hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics. … … 821 803 \begin{cfa} 822 804 switch ( x ) { 823 @int y = 1;@ $\C{// unreachable initialization}$824 @x = 7;@ $\C{// unreachable code without label/branch}$805 ®int y = 1;® §\C{// unreachable initialization}§ 806 ®x = 7;® §\C{// unreachable code without label/branch}§ 825 807 case 0: ... 826 808 ... 827 @int z = 0;@ $\C{// unreachable initialization, cannot appear after case}$809 ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§ 828 810 z = 2; 829 811 case 1: 830 @x = z;@ $\C{// without fall through, z is uninitialized}$812 ®x = z;® §\C{// without fall through, z is uninitialized}§ 831 813 } 832 814 \end{cfa} 833 815 While 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. 834 Furthermore, 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©, where both are problematic.835 As 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.836 The key observation is that the ©switch© statement branches into acontrol structure, \ie there are multiple entry points into its statement body.816 Furthermore, 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. 817 As 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. 818 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body. 837 819 \end{enumerate} 838 820 … … 860 842 Therefore, 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©, \eg: 861 843 \begin{cfa} 862 @choose@( i ) {844 ®choose® ( i ) { 863 845 case 1: case 2: case 3: 864 846 ... 865 @// implicit end of switch (break)866 @case 5:847 ®// implicit end of switch (break) 848 ®case 5: 867 849 ... 868 @fallthru@; $\C{// explicit fall through}$850 ®fallthru®; §\C{// explicit fall through}§ 869 851 case 7: 870 852 ... 871 @break@ $\C{// explicit end of switch (redundant)}$853 ®break® §\C{// explicit end of switch (redundant)}§ 872 854 default: 873 855 j = 3; 874 856 } 875 857 \end{cfa} 876 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses .858 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses; 877 859 An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. 878 860 An 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. … … 890 872 \begin{cfa} 891 873 switch ( x ) { 892 @int i = 0;@ $\C{// allowed only at start}$874 ®int i = 0;® §\C{// allowed only at start}§ 893 875 case 0: 894 876 ... 895 @int j = 0;@ $\C{// disallowed}$877 ®int j = 0;® §\C{// disallowed}§ 896 878 case 1: 897 879 { 898 @int k = 0;@ $\C{// allowed at different nesting levels}$880 ®int k = 0;® §\C{// allowed at different nesting levels}§ 899 881 ... 900 @case 2:@ $\C{// disallow case in nested statements}$882 ®case 2:® §\C{// disallow case in nested statements}§ 901 883 } 902 884 ... … … 915 897 case 3: 916 898 if ( ... ) { 917 ... @fallthru;@// goto case 4899 ... ®fallthru;® // goto case 4 918 900 } else { 919 901 ... … … 930 912 choose ( ... ) { 931 913 case 3: 932 ... @fallthrough common;@914 ... ®fallthrough common;® 933 915 case 4: 934 ... @fallthrough common;@935 936 @common:@// below fallthrough916 ... ®fallthrough common;® 917 918 ®common:® // below fallthrough 937 919 // at case-clause level 938 920 ... // common code for cases 3/4 … … 950 932 for ( ... ) { 951 933 // multi-level transfer 952 ... @fallthru common;@934 ... ®fallthru common;® 953 935 } 954 936 ... 955 937 } 956 938 ... 957 @common:@// below fallthrough939 ®common:® // below fallthrough 958 940 // at case-clause level 959 941 \end{cfa} … … 966 948 967 949 \begin{figure} 968 \begin{tabular}{@{}l @{\hspace{25pt}}|l@{}}969 \multicolumn{1}{ @{}c@{\hspace{25pt}}|}{loop control} & \multicolumn{1}{c@{}}{output} \\950 \begin{tabular}{@{}l|l@{}} 951 \multicolumn{1}{c|}{loop control} & \multicolumn{1}{c}{output} \\ 970 952 \hline 971 \begin{cfa} 972 while @($\,$)@{ sout | "empty"; break; }973 do { sout | "empty"; break; } while @($\,$)@;974 for @($\,$)@{ sout | "empty"; break; }975 for ( @0@) { sout | "A"; } sout | "zero";976 for ( @1@) { sout | "A"; }977 for ( @10@) { sout | "A"; }978 for ( @= 10@) { sout | "A"; }979 for ( @1 ~= 10 ~ 2@) { sout | "B"; }980 for ( @10 -~= 1 ~ 2@) { sout | "C"; }981 for ( @0.5 ~ 5.5@) { sout | "D"; }982 for ( @5.5 -~ 0.5@) { sout | "E"; }983 for ( @i; 10@) { sout | i; }984 for ( @i; = 10@) { sout | i; }985 for ( @i; 1 ~= 10 ~ 2@) { sout | i; }986 for ( @i; 10 -~= 1 ~ 2@) { sout | i; }987 for ( @i; 0.5 ~ 5.5@) { sout | i; }988 for ( @i; 5.5 -~ 0.5@) { sout | i; }989 for ( @ui; 2u ~= 10u ~ 2u@) { sout | ui; }990 for ( @ui; 10u -~= 2u ~ 2u@) { sout | ui; }953 \begin{cfa}[xleftmargin=0pt] 954 while ®()® { sout | "empty"; break; } 955 do { sout | "empty"; break; } while ®()®; 956 for ®()® { sout | "empty"; break; } 957 for ( ®0® ) { sout | "A"; } sout | "zero"; 958 for ( ®1® ) { sout | "A"; } 959 for ( ®10® ) { sout | "A"; } 960 for ( ®= 10® ) { sout | "A"; } 961 for ( ®1 ~= 10 ~ 2® ) { sout | "B"; } 962 for ( ®10 -~= 1 ~ 2® ) { sout | "C"; } 963 for ( ®0.5 ~ 5.5® ) { sout | "D"; } 964 for ( ®5.5 -~ 0.5® ) { sout | "E"; } 965 for ( ®i; 10® ) { sout | i; } 966 for ( ®i; = 10® ) { sout | i; } 967 for ( ®i; 1 ~= 10 ~ 2® ) { sout | i; } 968 for ( ®i; 10 -~= 1 ~ 2® ) { sout | i; } 969 for ( ®i; 0.5 ~ 5.5® ) { sout | i; } 970 for ( ®i; 5.5 -~ 0.5® ) { sout | i; } 971 for ( ®ui; 2u ~= 10u ~ 2u® ) { sout | ui; } 972 for ( ®ui; 10u -~= 2u ~ 2u® ) { sout | ui; } 991 973 enum { N = 10 }; 992 for ( @N@) { sout | "N"; }993 for ( @i; N@) { sout | i; }994 for ( @i; N -~ 0@) { sout | i; }974 for ( ®N® ) { sout | "N"; } 975 for ( ®i; N® ) { sout | i; } 976 for ( ®i; N -~ 0® ) { sout | i; } 995 977 const int start = 3, comp = 10, inc = 2; 996 for ( @i; start ~ comp ~ inc + 1@) { sout | i; }997 for ( i; 1 ~ $\R{@}$) { if ( i > 10 ) break; sout | i; }998 for ( i; 10 -~ $\R{@}$) { if ( i < 0 ) break; sout | i; }999 for ( i; 2 ~ $\R{@}$~ 2 ) { if ( i > 10 ) break; sout | i; }1000 for ( i; 2.1 ~ $\R{@}$ ~ $\R{@}$) { if ( i > 10.5 ) break; sout | i; i += 1.7; }1001 for ( i; 10 -~ $\R{@}$~ 2 ) { if ( i < 0 ) break; sout | i; }1002 for ( i; 12.1 ~ $\R{@}$ ~ $\R{@}$) { if ( i < 2.5 ) break; sout | i; i -= 1.7; }1003 for ( i; 5 @:@ j; -5 ~ $@$) { sout | i | j; }1004 for ( i; 5 @:@ j; -5 -~ $@$) { sout | i | j; }1005 for ( i; 5 @:@ j; -5 ~ $@$~ 2 ) { sout | i | j; }1006 for ( i; 5 @:@ j; -5 -~ $@$~ 2 ) { sout | i | j; }1007 for ( i; 5 @:@ j; -5 ~ $@$) { sout | i | j; }1008 for ( i; 5 @:@ j; -5 -~ $@$) { sout | i | j; }1009 for ( i; 5 @:@ j; -5 ~ $@$~ 2 ) { sout | i | j; }1010 for ( i; 5 @:@ j; -5 -~ $@$~ 2 ) { sout | i | j; }1011 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$) { sout | i | j | k; }1012 for ( i; 5 @:@ j; -5 -~ $@$ ~ 2 @:@ k; 1.5 ~ $@$) { sout | i | j | k; }1013 for ( i; 5 @:@ k; 1.5 ~ $@$ @:@ j; -5 -~ $@$~ 2 ) { sout | i | j | k; }978 for ( ®i; start ~ comp ~ inc + 1® ) { sout | i; } 979 for ( i; 1 ~ ®@® ) { if ( i > 10 ) break; sout | i; } 980 for ( i; 10 -~ ®@® ) { if ( i < 0 ) break; sout | i; } 981 for ( i; 2 ~ ®@® ~ 2 ) { if ( i > 10 ) break; sout | i; } 982 for ( i; 2.1 ~ ®@® ~ ®@® ) { if ( i > 10.5 ) break; sout | i; i += 1.7; } 983 for ( i; 10 -~ ®@® ~ 2 ) { if ( i < 0 ) break; sout | i; } 984 for ( i; 12.1 ~ ®@® ~ ®@® ) { if ( i < 2.5 ) break; sout | i; i -= 1.7; } 985 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 986 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 987 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 988 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 989 for ( i; 5 ®:® j; -5 ~ @ ) { sout | i | j; } 990 for ( i; 5 ®:® j; -5 -~ @ ) { sout | i | j; } 991 for ( i; 5 ®:® j; -5 ~ @ ~ 2 ) { sout | i | j; } 992 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ) { sout | i | j; } 993 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 994 for ( i; 5 ®:® j; -5 -~ @ ~ 2 ®:® k; 1.5 ~ @ ) { sout | i | j | k; } 995 for ( i; 5 ®:® k; 1.5 ~ @ ®:® j; -5 -~ @ ~ 2 ) { sout | i | j | k; } 1014 996 \end{cfa} 1015 997 & … … 1074 1056 \subsection{Loop Control} 1075 1057 1076 Looping a fixed number of times, possibly with a loop index, occurs frequently. 1077 \CFA condenses simply looping to facilitate coding speed and safety. 1078 The ©for©/©while©/©do-while© loop-control is augmented as follows \see{examples in \VRef[Figure]{f:LoopControlExamples}}: 1079 \begin{itemize}[itemsep=0pt] 1058 The ©for©/©while©/©do-while© loop-control allows empty or simplified ranges (see Figure~\ref{f:LoopControlExamples}). 1059 \begin{itemize} 1060 \item 1061 The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M. 1062 \item 1063 An empty conditional implies comparison value of ©1© (true). 1064 \item 1065 A comparison N is implicit up-to exclusive range [0,N©®)®©. 1066 \item 1067 A comparison ©=© N is implicit up-to inclusive range [0,N©®]®©. 1068 \item 1069 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N©®)®©. 1070 \item 1071 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N©®]®©. 1072 \item 1073 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M©®)®©. 1074 \item 1075 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M©®]®©. 1080 1076 \item 1081 1077 ©0© is the implicit start value; … … 1087 1083 The down-to range uses operator ©-=© for decrement. 1088 1084 \item 1089 The loop index is polymorphic in the type of the comparison value N (when the start value is implicit) or the start value M.1090 \begin{cfa}1091 for ( i; @5@ ) $\C[2.5in]{// typeof(5) i; 5 is comparison value}$1092 for ( i; @1.5@~5.5~0.5 ) $\C{// typeof(1.5) i; 1.5 is start value}$1093 \end{cfa}1094 \item1095 An empty conditional implies comparison value of ©1© (true).1096 \begin{cfa}1097 while ( $\R{/*empty*/}$ ) $\C{// while ( true )}$1098 for ( $\R{/*empty*/}$ ) $\C{// for ( ; true; )}$1099 do ... while ( $\R{/*empty*/}$ ) $\C{// do ... while ( true )}$1100 \end{cfa}1101 \item1102 A comparison N is implicit up-to exclusive range [0,N\R{)}.1103 \begin{cfa}1104 for ( @5@ ) $\C{// for ( typeof(5) i; i < 5; i += 1 )}$1105 \end{cfa}1106 \item1107 A comparison ©=© N is implicit up-to inclusive range [0,N\R{]}.1108 \begin{cfa}1109 for ( @=@5 ) $\C{// for ( typeof(5) i; i <= 5; i += 1 )}$1110 \end{cfa}1111 \item1112 The up-to range M ©~©\index{~@©~©} N means exclusive range [M,N\R{)}.1113 \begin{cfa}1114 for ( 1@~@5 ) $\C{// for ( typeof(1) i = 1; i < 5; i += 1 )}$1115 \end{cfa}1116 \item1117 The up-to range M ©~=©\index{~=@©~=©} N means inclusive range [M,N\R{]}.1118 \begin{cfa}1119 for ( 1@~=@5 ) $\C{// for ( typeof(1) i = 1; i <= 5; i += 1 )}$1120 \end{cfa}1121 \item1122 The down-to range M ©-~©\index{-~@©-~©} N means exclusive range [N,M\R{)}.1123 \begin{cfa}1124 for ( 1@-~@5 ) $\C{// for ( typeof(1) i = 5; i > 0; i -= 1 )}$1125 \end{cfa}1126 \item1127 The down-to range M ©-~=©\index{-~=@©-~=©} N means inclusive range [N,M\R{]}.1128 \begin{cfa}1129 for ( 1@-~=@5 ) $\C{// for ( typeof(1) i = 5; i >= 0; i -= 1 )}$1130 \end{cfa}1131 \item1132 1085 ©@© means put nothing in this field. 1133 \begin{cfa}1134 for ( 1~$\R{@}$~2 ) $\C{// for ( typeof(1) i = 1; /*empty*/; i += 2 )}$1135 \end{cfa}1136 1086 \item 1137 1087 ©:© means start another index. 1138 \begin{cfa}1139 for ( i; 5 @:@ j; 2~12~3 ) $\C{// for ( typeof(i) i = 1, j = 2; i < 5 \&\& j < 12; i += 1, j += 3 )}\CRT$1140 \end{cfa}1141 1088 \end{itemize} 1142 1089 … … 1145 1092 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} 1146 1093 1147 C ©continue© and ©break© statements, for altering control flow,are restricted to one level of nesting for a particular control structure.1148 This restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting.1094 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. 1095 Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 1149 1096 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and \Indexc{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}, as in Java. 1150 1097 For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement; 1151 1098 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement. 1152 \VRef[Figure]{f:MultiLevelExit} shows a comparison between labelled ©continue© and ©break© and the corresponding C equivalent using©goto© and labels.1099 \VRef[Figure]{f:MultiLevelExit} shows ©continue© and ©break© indicating the specific control structure, and the corresponding C program using only ©goto© and labels. 1153 1100 The innermost loop has 8 exit points, which cause continuation or termination of one or more of the 7 \Index{nested control-structure}s. 1154 1101 … … 1157 1104 \begin{lrbox}{\myboxA} 1158 1105 \begin{cfa}[tabsize=3] 1159 @Compound:@{1160 @Try:@try {1161 @For:@for ( ... ) {1162 @While:@while ( ... ) {1163 @Do:@do {1164 @If:@if ( ... ) {1165 @Switch:@switch ( ... ) {1106 ®Compound:® { 1107 ®Try:® try { 1108 ®For:® for ( ... ) { 1109 ®While:® while ( ... ) { 1110 ®Do:® do { 1111 ®If:® if ( ... ) { 1112 ®Switch:® switch ( ... ) { 1166 1113 case 3: 1167 @break Compound@;1168 @break Try@;1169 @break For@; /* or */ @continue For@;1170 @break While@; /* or */ @continue While@;1171 @break Do@; /* or */ @continue Do@;1172 @break If@;1173 @break Switch@;1114 ®break Compound®; 1115 ®break Try®; 1116 ®break For®; /* or */ ®continue For®; 1117 ®break While®; /* or */ ®continue While®; 1118 ®break Do®; /* or */ ®continue Do®; 1119 ®break If®; 1120 ®break Switch®; 1174 1121 } // switch 1175 1122 } else { 1176 ... @break If@; ... // terminate if1123 ... ®break If®; ... // terminate if 1177 1124 } // if 1178 1125 } while ( ... ); // do 1179 1126 } // while 1180 1127 } // for 1181 } @finally@{ // always executed1128 } ®finally® { // always executed 1182 1129 } // try 1183 1130 } // compound … … 1189 1136 { 1190 1137 1191 @ForC:@for ( ... ) {1192 @WhileC:@while ( ... ) {1193 @DoC:@do {1138 ®ForC:® for ( ... ) { 1139 ®WhileC:® while ( ... ) { 1140 ®DoC:® do { 1194 1141 if ( ... ) { 1195 1142 switch ( ... ) { 1196 1143 case 3: 1197 @goto Compound@;1198 @goto Try@;1199 @goto ForB@; /* or */ @goto ForC@;1200 @goto WhileB@; /* or */ @goto WhileC@;1201 @goto DoB@; /* or */ @goto DoC@;1202 @goto If@;1203 @goto Switch@;1204 } @Switch:@;1144 ®goto Compound®; 1145 ®goto Try®; 1146 ®goto ForB®; /* or */ ®goto ForC®; 1147 ®goto WhileB®; /* or */ ®goto WhileC®; 1148 ®goto DoB®; /* or */ ®goto DoC®; 1149 ®goto If®; 1150 ®goto Switch®; 1151 } ®Switch:® ; 1205 1152 } else { 1206 ... @goto If@; ... // terminate if1207 } @If:@;1208 } while ( ... ); @DoB:@;1209 } @WhileB:@;1210 } @ForB:@;1211 1212 1213 } @Compound:@;1153 ... ®goto If®; ... // terminate if 1154 } ®If:®; 1155 } while ( ... ); ®DoB:® ; 1156 } ®WhileB:® ; 1157 } ®ForB:® ; 1158 1159 1160 } ®Compound:® ; 1214 1161 \end{cfa} 1215 1162 \end{lrbox} 1216 1163 1217 1164 \subfloat[\CFA]{\label{f:CFibonacci}\usebox\myboxA} 1218 \hspace{ 3pt}1165 \hspace{2pt} 1219 1166 \vrule 1220 \hspace{ 3pt}1167 \hspace{2pt} 1221 1168 \subfloat[C]{\label{f:CFAFibonacciGen}\usebox\myboxB} 1222 1169 \caption{Multi-level Exit} … … 1233 1180 This restriction prevents missing declarations and/or initializations at the start of a control structure resulting in undefined behaviour. 1234 1181 \end{itemize} 1235 The 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 via a label.1182 The 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. 1236 1183 Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader (\Index{eye candy}) that complex control-flow is occurring in the body of the control structure. 1237 1184 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. … … 1240 1187 1241 1188 1242 %\s ubsection{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}}1243 \s ubsection{\texorpdfstring{\LstKeywordStyle{with} Statement}{with Statement}}1189 %\section{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}} 1190 \section{\texorpdfstring{\LstKeywordStyle{with} Statement}{with Statement}} 1244 1191 \label{s:WithStatement} 1245 1192 1246 Grouping heterogeneous data into an \newterm{aggregate} (structure/union) is a common programming practice, and aggregates may be nested:1247 \begin{cfa} 1248 struct Person { $\C{// aggregate}$1249 struct Name { char first[20], last[20]; } name $\C{// nesting}$1250 struct Address { ... } address $\C{// nesting}$1251 int sex;1193 Grouping heterogeneous data into \newterm{aggregate}s (structure/union) is a common programming practice, and an aggregate can be further organized into more complex structures, such as arrays and containers: 1194 \begin{cfa} 1195 struct S { §\C{// aggregate}§ 1196 char c; §\C{// fields}§ 1197 int i; 1198 double d; 1252 1199 }; 1253 \end{cfa} 1254 Functions manipulating aggregates must repeat the aggregate name to access its containing fields. 1255 \begin{cfa} 1256 Person p 1257 @p.@name; @p.@address; @p.@sex; $\C{// access containing fields}$ 1258 \end{cfa} 1259 which extends to multiple levels of qualification for nested aggregates and multiple aggregates. 1260 \begin{cfa} 1261 struct Ticket { ... } t; 1262 @p.name@.first; @p.address@.street; $\C{// access nested fields}$ 1263 @t.@departure; @t.@cost; $\C{// access multiple aggregate}$ 1264 \end{cfa} 1265 Repeated aggregate qualification is tedious and makes code difficult to read. 1266 Therefore, reducing aggregate qualification is a useful language design goal. 1267 1268 C allows unnamed nested aggregates that open their scope into the containing aggregate. 1269 This feature is used to group fields for attributes and/or with ©union© aggregates. 1270 \begin{cfa} 1271 struct S { 1272 struct { int g, h; } __attribute__(( aligned(64) )); 1273 int tag; 1274 union { 1275 struct { char c1, c2; } __attribute__(( aligned(128) )); 1276 struct { int i1, i2; }; 1277 struct { double d1, d2; }; 1278 }; 1279 }; 1280 s.g; s.h; s.tag; s.c1; s.c2; s.i1; s.i2; s.d1; s.d2; 1281 \end{cfa} 1282 1283 Object-oriented languages reduce qualification for class variables within member functions, \eg \CC: 1200 S s, as[10]; 1201 \end{cfa} 1202 However, functions manipulating aggregates must repeat the aggregate name to access its containing fields: 1203 \begin{cfa} 1204 void f( S s ) { 1205 ®s.®c; ®s.®i; ®s.®d; §\C{// access containing fields}§ 1206 } 1207 \end{cfa} 1208 which extends to multiple levels of qualification for nested aggregates. 1209 A similar situation occurs in object-oriented programming, \eg \CC: 1284 1210 \begin{C++} 1285 1211 struct S { 1286 char @c@; int @i@; double @d@; 1287 void f( /* S * this */ ) { $\C{// implicit ``this'' parameter}$ 1288 @c@; @i@; @d@; $\C{// this->c; this->i; this->d;}$ 1212 char c; §\C{// fields}§ 1213 int i; 1214 double d; 1215 void f() { §\C{// implicit ``this'' aggregate}§ 1216 ®this->®c; ®this->®i; ®this->®d; §\C{// access containing fields}§ 1289 1217 } 1290 1218 } 1291 1219 \end{C++} 1292 In general, qualification is elided for the variables and functions in the lexical scopes visible from a member function. 1293 However, qualification is necessary for name shadowing and explicit aggregate parameters. 1294 \begin{cfa} 1295 struct T { 1296 char @m@; int @i@; double @n@; $\C{// derived class variables}$ 1297 }; 1298 struct S : public T { 1299 char @c@; int @i@; double @d@; $\C{// class variables}$ 1300 void g( double @d@, T & t ) { 1301 d; @t@.m; @t@.i; @t@.n; $\C{// function parameter}$ 1302 c; i; @this->@d; @S::@d; $\C{// class S variables}$ 1303 m; @T::@i; n; $\C{// class T variables}$ 1304 } 1305 }; 1306 \end{cfa} 1307 Note the three different forms of qualification syntax in \CC, ©.©, ©->©, ©::©, which is confusing. 1308 1309 Since \CFA in not object-oriented, it has no implicit parameter with its implicit qualification. 1310 Instead \CFA introduces a general mechanism using the ©with© statement \see{Pascal~\cite[\S~4.F]{Pascal}} to explicitly elide aggregate qualification by opening a scope containing the field identifiers. 1311 Hence, the qualified fields become variables with the side-effect that it is simpler to write, easier to read, and optimize field references in a block. 1312 \begin{cfa} 1313 void f( S & this ) @with ( this )@ { $\C{// with statement}$ 1314 @c@; @i@; @d@; $\C{// this.c, this.i, this.d}$ 1220 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++]@this->@ because of lexical scoping. 1221 However, for other aggregate parameters, qualification is necessary: 1222 \begin{cfa} 1223 struct T { double m, n; }; 1224 int S::f( T & t ) { §\C{// multiple aggregate parameters}§ 1225 c; i; d; §\C{\color{red}// this--{\textgreater}.c, this--{\textgreater}.i, this--{\textgreater}.d}§ 1226 ®t.®m; ®t.®n; §\C{// must qualify}§ 1227 } 1228 \end{cfa} 1229 1230 To simplify the programmer experience, \CFA provides a ©with© statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers. 1231 Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block. 1232 \begin{cfa} 1233 void f( S & this ) ®with ( this )® { §\C{// with statement}§ 1234 c; i; d; §\C{\color{red}// this.c, this.i, this.d}§ 1315 1235 } 1316 1236 \end{cfa} 1317 1237 with the generality of opening multiple aggregate-parameters: 1318 1238 \begin{cfa} 1319 void g( S & s, T & t ) @with ( s, t )@ { $\C{// multiple aggregate parameters}$ 1320 c; @s.@i; d; $\C{// s.c, s.i, s.d}$ 1321 m; @t.@i; n; $\C{// t.m, t.i, t.n}$ 1322 } 1323 \end{cfa} 1324 where qualification is only necessary to disambiguate the shadowed variable ©i©. 1325 1326 In detail, the ©with© statement may appear as the body of a function or nested within a function body. 1327 The ©with© clause takes a list of expressions, where each expression provides an aggregate type and object. 1239 void f( S & s, T & t ) ®with ( s, t )® { §\C{// multiple aggregate parameters}§ 1240 c; i; d; §\C{\color{red}// s.c, s.i, s.d}§ 1241 m; n; §\C{\color{red}// t.m, t.n}§ 1242 } 1243 \end{cfa} 1244 1245 In detail, the ©with© statement has the form: 1246 \begin{cfa} 1247 §\emph{with-statement}§: 1248 'with' '(' §\emph{expression-list}§ ')' §\emph{compound-statement}§ 1249 \end{cfa} 1250 and may appear as the body of a function or nested within a function body. 1251 Each expression in the expression-list provides a type and object. 1252 The type must be an aggregate type. 1328 1253 (Enumerations are already opened.) 1329 To open a pointer type, the pointer must be dereferenced to obtain a reference to the aggregate type. 1330 \begin{cfa} 1331 S * sp; 1332 with ( *sp ) { ... } 1333 \end{cfa} 1334 The expression object is the implicit qualifier for the open structure-fields. 1335 \CFA's ability to overload variables \see{\VRef{s:VariableOverload}} and use the left-side of assignment in type resolution means most fields with the same name but different types are automatically disambiguated, eliminating qualification. 1254 The object is the implicit qualifier for the open structure-fields. 1255 1336 1256 All expressions in the expression list are open in parallel within the compound statement. 1337 1257 This semantic is different from Pascal, which nests the openings from left to right. 1338 1258 The difference between parallel and nesting occurs for fields with the same name and type: 1339 1259 \begin{cfa} 1340 struct Q { int @i@; int k; int @m@; } q, w; 1341 struct R { int @i@; int j; double @m@; } r, w; 1342 with ( r, q ) { 1343 j + k; $\C{// unambiguous, r.j + q.k}$ 1344 m = 5.0; $\C{// unambiguous, q.m = 5.0}$ 1345 m = 1; $\C{// unambiguous, r.m = 1}$ 1346 int a = m; $\C{// unambiguous, a = r.i }$ 1347 double b = m; $\C{// unambiguous, b = q.m}$ 1348 int c = r.i + q.i; $\C{// disambiguate with qualification}$ 1349 (double)m; $\C{// disambiguate with cast}$ 1350 } 1351 \end{cfa} 1352 For parallel semantics, both ©r.i© and ©q.i© are visible, so ©i© is ambiguous without qualification; 1353 for nested semantics, ©q.i© hides ©r.i©, so ©i© implies ©q.i©. 1354 Pascal nested-semantics is possible by nesting ©with© statements. 1355 \begin{cfa} 1356 with ( r ) { 1357 i; $\C{// unambiguous, r.i}$ 1358 with ( q ) { 1359 i; $\C{// unambiguous, q.i}$ 1360 } 1361 } 1362 \end{cfa} 1363 A cast or qualification can be used to disambiguate variables within a ©with© \emph{statement}. 1364 A cast can be used to disambiguate among overload variables in a ©with© \emph{expression}: 1365 \begin{cfa} 1366 with ( w ) { ... } $\C{// ambiguous, same name and no context}$ 1367 with ( (Q)w ) { ... } $\C{// unambiguous, cast}$ 1368 \end{cfa} 1369 Because there is no left-side in the ©with© expression to implicitly disambiguate between the ©w© variables, it is necessary to explicitly disambiguate by casting ©w© to type ©Q© or ©R©. 1370 1371 Finally, there is an interesting problem between parameters and the function-body ©with©, \eg: 1372 \begin{cfa} 1373 void ?{}( S & s, int i ) with ( s ) { $\C{// constructor}$ 1374 @s.i = i;@ j = 3; m = 5.5; $\C{// initialize fields}$ 1260 struct S { int ®i®; int j; double m; } s, w; 1261 struct T { int ®i®; int k; int m; } t, w; 1262 with ( s, t ) { 1263 j + k; §\C{// unambiguous, s.j + t.k}§ 1264 m = 5.0; §\C{// unambiguous, t.m = 5.0}§ 1265 m = 1; §\C{// unambiguous, s.m = 1}§ 1266 int a = m; §\C{// unambiguous, a = s.i }§ 1267 double b = m; §\C{// unambiguous, b = t.m}§ 1268 int c = s.i + t.i; §\C{// unambiguous, qualification}§ 1269 (double)m; §\C{// unambiguous, cast}§ 1270 } 1271 \end{cfa} 1272 For parallel semantics, both ©s.i© and ©t.i© are visible, so ©i© is ambiguous without qualification; 1273 for nested semantics, ©t.i© hides ©s.i©, so ©i© implies ©t.i©. 1274 \CFA's ability to overload variables means fields with the same name but different types are automatically disambiguated, eliminating most qualification when opening multiple aggregates. 1275 Qualification or a cast is used to disambiguate. 1276 1277 There is an interesting problem between parameters and the function-body ©with©, \eg: 1278 \begin{cfa} 1279 void ?{}( S & s, int i ) with ( s ) { §\C{// constructor}§ 1280 ®s.i = i;® j = 3; m = 5.5; §\C{// initialize fields}§ 1375 1281 } 1376 1282 \end{cfa} … … 1385 1291 and implicitly opened \emph{after} a function-body open, to give them higher priority: 1386 1292 \begin{cfa} 1387 void ?{}( S & s, int @i@ ) with ( s ) @with( $\emph{\R{params}}$ )@ { // syntax not allowed, illustration only 1388 s.i = @i@; j = 3; m = 5.5; 1389 } 1390 \end{cfa} 1391 This implicit semantic matches with programmer expectation. 1392 1293 void ?{}( S & s, int ®i® ) with ( s ) ®with( §\emph{\color{red}params}§ )® { 1294 s.i = ®i®; j = 3; m = 5.5; 1295 } 1296 \end{cfa} 1297 Finally, a cast may be used to disambiguate among overload variables in a ©with© expression: 1298 \begin{cfa} 1299 with ( w ) { ... } §\C{// ambiguous, same name and no context}§ 1300 with ( (S)w ) { ... } §\C{// unambiguous, cast}§ 1301 \end{cfa} 1302 and ©with© expressions may be complex expressions with type reference (see Section~\ref{s:References}) to aggregate: 1303 % \begin{cfa} 1304 % struct S { int i, j; } sv; 1305 % with ( sv ) { §\C{// implicit reference}§ 1306 % S & sr = sv; 1307 % with ( sr ) { §\C{// explicit reference}§ 1308 % S * sp = &sv; 1309 % with ( *sp ) { §\C{// computed reference}§ 1310 % i = 3; j = 4; §\C{\color{red}// sp--{\textgreater}i, sp--{\textgreater}j}§ 1311 % } 1312 % i = 2; j = 3; §\C{\color{red}// sr.i, sr.j}§ 1313 % } 1314 % i = 1; j = 2; §\C{\color{red}// sv.i, sv.j}§ 1315 % } 1316 % \end{cfa} 1317 1318 In \Index{object-oriented} programming, there is an implicit first parameter, often names \textbf{©self©} or \textbf{©this©}, which is elided. 1319 \begin{C++} 1320 class C { 1321 int i, j; 1322 int mem() { §\C{\color{red}// implicit "this" parameter}§ 1323 i = 1; §\C{\color{red}// this->i}§ 1324 j = 2; §\C{\color{red}// this->j}§ 1325 } 1326 } 1327 \end{C++} 1328 Since \CFA is non-object-oriented, the equivalent object-oriented program looks like: 1329 \begin{cfa} 1330 struct S { int i, j; }; 1331 int mem( S & ®this® ) { §\C{// explicit "this" parameter}§ 1332 ®this.®i = 1; §\C{// "this" is not elided}§ 1333 ®this.®j = 2; 1334 } 1335 \end{cfa} 1336 but it is cumbersome having to write ``©this.©'' many times in a member. 1337 1338 \CFA provides a ©with© clause/statement (see Pascal~\cite[\S~4.F]{Pascal}) to elided the "©this.©" by opening a scope containing field identifiers, changing the qualified fields into variables and giving an opportunity for optimizing qualified references. 1339 \begin{cfa} 1340 int mem( S & this ) ®with( this )® { §\C{// with clause}§ 1341 i = 1; §\C{\color{red}// this.i}§ 1342 j = 2; §\C{\color{red}// this.j}§ 1343 } 1344 \end{cfa} 1345 which extends to multiple routine parameters: 1346 \begin{cfa} 1347 struct T { double m, n; }; 1348 int mem2( S & this1, T & this2 ) ®with( this1, this2 )® { 1349 i = 1; j = 2; 1350 m = 1.0; n = 2.0; 1351 } 1352 \end{cfa} 1353 1354 The statement form is used within a block: 1355 \begin{cfa} 1356 int foo() { 1357 struct S1 { ... } s1; 1358 struct S2 { ... } s2; 1359 ®with( s1 )® { §\C{// with statement}§ 1360 // access fields of s1 without qualification 1361 ®with s2® { §\C{// nesting}§ 1362 // access fields of s1 and s2 without qualification 1363 } 1364 } 1365 ®with s1, s2® { 1366 // access unambiguous fields of s1 and s2 without qualification 1367 } 1368 } 1369 \end{cfa} 1370 1371 When opening multiple structures, fields with the same name and type are ambiguous and must be fully qualified. 1372 For fields with the same name but different type, context/cast can be used to disambiguate. 1373 \begin{cfa} 1374 struct S { int i; int j; double m; } a, c; 1375 struct T { int i; int k; int m } b, c; 1376 with( a, b ) 1377 { 1378 } 1379 \end{cfa} 1380 1381 \begin{comment} 1382 The components in the "with" clause 1383 1384 with a, b, c { ... } 1385 1386 serve 2 purposes: each component provides a type and object. The type must be a 1387 structure type. Enumerations are already opened, and I think a union is opened 1388 to some extent, too. (Or is that just unnamed unions?) The object is the target 1389 that the naked structure-fields apply to. The components are open in "parallel" 1390 at the scope of the "with" clause/statement, so opening "a" does not affect 1391 opening "b", etc. This semantic is different from Pascal, which nests the 1392 openings. 1393 1394 Having said the above, it seems reasonable to allow a "with" component to be an 1395 expression. The type is the static expression-type and the object is the result 1396 of the expression. Again, the type must be an aggregate. Expressions require 1397 parenthesis around the components. 1398 1399 with( a, b, c ) { ... } 1400 1401 Does this now make sense? 1402 1403 Having written more CFA code, it is becoming clear to me that I *really* want 1404 the "with" to be implemented because I hate having to type all those object 1405 names for fields. It's a great way to drive people away from the language. 1406 \end{comment} 1393 1407 1394 1408 … … 1400 1414 Non-local transfer can cause stack unwinding, \ie non-local routine termination, depending on the kind of raise. 1401 1415 \begin{cfa} 1402 exception_t E {}; $\C{// exception type}$1416 exception_t E {}; §\C{// exception type}§ 1403 1417 void f(...) { 1404 ... throw E{}; ... $\C{// termination}$1405 ... throwResume E{}; ... $\C{// resumption}$1418 ... throw E{}; ... §\C{// termination}§ 1419 ... throwResume E{}; ... §\C{// resumption}§ 1406 1420 } 1407 1421 try { 1408 1422 f(...); 1409 } catch( E e ; $boolean-predicate$ ) { $\C{// termination handler}$1423 } catch( E e ; §boolean-predicate§ ) { §\C{// termination handler}§ 1410 1424 // recover and continue 1411 } catchResume( E e ; $boolean-predicate$ ) { $\C{// resumption handler}$1425 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}§ 1412 1426 // repair and return 1413 1427 } finally { … … 1416 1430 \end{cfa} 1417 1431 The kind of raise and handler match: ©throw© with ©catch© and ©throwResume© with ©catchResume©. 1418 Then the exception type must match along with any addit ional predicate must be true.1432 Then the exception type must match along with any additonal predicate must be true. 1419 1433 The ©catch© and ©catchResume© handlers may appear in any oder. 1420 1434 However, the ©finally© clause must appear at the end of the ©try© statement. … … 1469 1483 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 1470 1484 \begin{cfa} 1471 int @(*@f@())[@5@]@ {...}; $\C{// definition}$1472 ... @(*@f@())[@3@]@ += 1; $\C{// usage}$1485 int ®(*®f®())[®5®]® {...}; §\C{// definition}§ 1486 ... ®(*®f®())[®3®]® += 1; §\C{// usage}§ 1473 1487 \end{cfa} 1474 1488 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). … … 1485 1499 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1486 1500 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1487 \begin{cfa} [moredelim={**[is][\color{blue}]{\#}{\#}}]1488 #[5] *# @int@x1;1489 #* [5]# @int@x2;1490 #[* [5] int]# f@( int p )@;1501 \begin{cfa} 1502 ß[5] *ß ®int® x1; 1503 ß* [5]ß ®int® x2; 1504 ß[* [5] int]ß f®( int p )®; 1491 1505 \end{cfa} 1492 1506 & 1493 \begin{cfa} [moredelim={**[is][\color{blue}]{\#}{\#}}]1494 @int@ #*# x1 #[5]#;1495 @int@ #(*#x2#)[5]#;1496 #int (*#f@( int p )@#)[5]#;1507 \begin{cfa} 1508 ®int® ß*ß x1 ß[5]ß; 1509 ®int® ß(*ßx2ß)[5]ß; 1510 ßint (*ßf®( int p )®ß)[5]ß; 1497 1511 \end{cfa} 1498 1512 \end{tabular} … … 1506 1520 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1507 1521 \begin{cfa} 1508 @*@int x, y;1522 ®*® int x, y; 1509 1523 \end{cfa} 1510 1524 & 1511 1525 \begin{cfa} 1512 int @*@x, @*@y;1526 int ®*®x, ®*®y; 1513 1527 \end{cfa} 1514 1528 \end{tabular} … … 1519 1533 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1520 1534 \begin{cfa} 1521 @*@int x;1535 ®*® int x; 1522 1536 int y; 1523 1537 \end{cfa} 1524 1538 & 1525 1539 \begin{cfa} 1526 int @*@x, y;1540 int ®*®x, y; 1527 1541 1528 1542 \end{cfa} … … 1633 1647 1634 1648 \section{Pointer / Reference} 1635 \label{s:PointerReference}1636 1649 1637 1650 C provides a \newterm{pointer type}; … … 1660 1673 & 1661 1674 \begin{cfa} 1662 int * @const@x = (int *)1001675 int * ®const® x = (int *)100 1663 1676 *x = 3; // implicit dereference 1664 int * @const@y = (int *)104;1677 int * ®const® y = (int *)104; 1665 1678 *y = *x; // implicit dereference 1666 1679 \end{cfa} … … 1700 1713 \begin{tabular}{@{}l@{\hspace{2em}}l@{}} 1701 1714 \begin{cfa} 1702 int x, y, @*@ p1, @*@ p2, @**@p3;1703 p1 = @&@x; // p1 points to x1715 int x, y, ®*® p1, ®*® p2, ®**® p3; 1716 p1 = ®&®x; // p1 points to x 1704 1717 p2 = p1; // p2 points to x 1705 p1 = @&@y; // p1 points to y1718 p1 = ®&®y; // p1 points to y 1706 1719 p3 = &p2; // p3 points to p2 1707 1720 \end{cfa} … … 1715 1728 For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage 1716 1729 \begin{cfa} 1717 p2 = p1 + x; $\C{// compiler infers *p2 = *p1 + x;}$1730 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§ 1718 1731 \end{cfa} 1719 1732 Algol68 infers the following dereferencing ©*p2 = *p1 + x©, because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation. … … 1723 1736 In C, objects of pointer type always manipulate the pointer object's address: 1724 1737 \begin{cfa} 1725 p1 = p2; $\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}$1726 p2 = p1 + x; $\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}$1738 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§ 1739 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§ 1727 1740 \end{cfa} 1728 1741 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant: 1729 1742 \begin{cfa} 1730 p1 = p2; $\C{// pointer address assignment}$1731 @*@p2 = @*@p1 + x; $\C{// pointed-to value assignment / operation}$ 1743 p1 = p2; §\C{// pointer address assignment}§ 1744 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§ 1732 1745 \end{cfa} 1733 1746 The C semantics work well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©). … … 1745 1758 To support this common case, a reference type is introduced in \CFA, denoted by ©&©, which is the opposite dereference semantics to a pointer type, making the value at the pointed-to location the implicit semantics for dereferencing (similar but not the same as \CC \Index{reference type}s). 1746 1759 \begin{cfa} 1747 int x, y, @&@ r1, @&@ r2, @&&@r3;1748 @&@r1 = &x; $\C{// r1 points to x}$ 1749 @&@r2 = &r1; $\C{// r2 points to x}$ 1750 @&@r1 = &y; $\C{// r1 points to y}$ 1751 @&&@r3 = @&@&r2; $\C{// r3 points to r2}$ 1752 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); $\C{// implicit dereferencing}$1760 int x, y, ®&® r1, ®&® r2, ®&&® r3; 1761 ®&®r1 = &x; §\C{// r1 points to x}§ 1762 ®&®r2 = &r1; §\C{// r2 points to x}§ 1763 ®&®r1 = &y; §\C{// r1 points to y}§ 1764 ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§ 1765 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§ 1753 1766 \end{cfa} 1754 1767 Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example. … … 1756 1769 One way to conceptualize a reference is via a rewrite rule, where the compiler inserts a dereference operator before the reference variable for each reference qualifier in a declaration, so the previous example becomes: 1757 1770 \begin{cfa} 1758 @*@r2 = ((@*@r1 + @*@r2) @*@ (@**@r3 - @*@r1)) / (@**@r3 - 15);1771 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15); 1759 1772 \end{cfa} 1760 1773 When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out. … … 1765 1778 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 1766 1779 \begin{cfa} 1767 (& @*@)r1 = &x; $\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}$1780 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§ 1768 1781 \end{cfa} 1769 1782 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 1770 1783 \begin{cfa} 1771 (&(& @*@)@*@)r3 = &(&@*@)r2; $\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}$1784 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§ 1772 1785 \end{cfa} 1773 1786 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. … … 1777 1790 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 1778 1791 &r1 = x, &&r2 = r1, &&&r3 = r2; 1779 ***p3 = 3; $\C{// change x}$1780 r3 = 3; $\C{// change x, ***r3}$1781 **p3 = ...; $\C{// change p1}$1782 &r3 = ...; $\C{// change r1, (\&*)**r3, 1 cancellation}$1783 *p3 = ...; $\C{// change p2}$1784 &&r3 = ...; $\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}$1785 &&&r3 = p3; $\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}$1792 ***p3 = 3; §\C{// change x}§ 1793 r3 = 3; §\C{// change x, ***r3}§ 1794 **p3 = ...; §\C{// change p1}§ 1795 &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§ 1796 *p3 = ...; §\C{// change p2}§ 1797 &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§ 1798 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§ 1786 1799 \end{cfa} 1787 1800 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types. … … 1790 1803 As for a pointer type, a reference type may have qualifiers: 1791 1804 \begin{cfa} 1792 const int cx = 5; $\C{// cannot change cx;}$1793 const int & cr = cx; $\C{// cannot change what cr points to}$1794 @&@cr = &cx; $\C{// can change cr}$ 1795 cr = 7; $\C{// error, cannot change cx}$1796 int & const rc = x; $\C{// must be initialized}$1797 @&@rc = &x; $\C{// error, cannot change rc}$ 1798 const int & const crc = cx; $\C{// must be initialized}$1799 crc = 7; $\C{// error, cannot change cx}$1800 @&@crc = &cx; $\C{// error, cannot change crc}$ 1805 const int cx = 5; §\C{// cannot change cx;}§ 1806 const int & cr = cx; §\C{// cannot change what cr points to}§ 1807 ®&®cr = &cx; §\C{// can change cr}§ 1808 cr = 7; §\C{// error, cannot change cx}§ 1809 int & const rc = x; §\C{// must be initialized}§ 1810 ®&®rc = &x; §\C{// error, cannot change rc}§ 1811 const int & const crc = cx; §\C{// must be initialized}§ 1812 crc = 7; §\C{// error, cannot change cx}§ 1813 ®&®crc = &cx; §\C{// error, cannot change crc}§ 1801 1814 \end{cfa} 1802 1815 Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be the null pointer unless an arbitrary pointer is coerced\index{coercion} into the reference}: 1803 1816 \begin{cfa} 1804 int & const cr = *0; $\C{// where 0 is the int * zero}$1817 int & const cr = *0; §\C{// where 0 is the int * zero}§ 1805 1818 \end{cfa} 1806 1819 Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management: … … 1809 1822 cr = 5; 1810 1823 free( &cr ); 1811 cr = 7; $\C{// unsound pointer dereference}$1824 cr = 7; §\C{// unsound pointer dereference}§ 1812 1825 \end{cfa} 1813 1826 1814 1827 The position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers. 1815 1828 The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations; 1816 \CFA-style declarations \see{\VRef{s:AlternativeDeclarations}}attempt to address this issue:1829 \CFA-style declarations (see \VRef{s:AlternativeDeclarations}) attempt to address this issue: 1817 1830 \begin{cquote} 1818 1831 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1819 1832 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1820 1833 \begin{cfa} 1821 @const@ * @const@* const int ccp;1822 @const@ & @const@& const int ccr;1834 ®const® * ®const® * const int ccp; 1835 ®const® & ®const® & const int ccr; 1823 1836 \end{cfa} 1824 1837 & 1825 1838 \begin{cfa} 1826 const int * @const@ * @const@ccp;1839 const int * ®const® * ®const® ccp; 1827 1840 1828 1841 \end{cfa} … … 1833 1846 Finally, like pointers, references are usable and composable with other type operators and generators. 1834 1847 \begin{cfa} 1835 int w, x, y, z, & ar[3] = { x, y, z }; $\C{// initialize array of references}$1836 &ar[1] = &w; $\C{// change reference array element}$1837 typeof( ar[1] ) p; $\C{// (gcc) is int, \ie the type of referenced object}$1838 typeof( &ar[1] ) q; $\C{// (gcc) is int \&, \ie the type of reference}$1839 sizeof( ar[1] ) == sizeof( int ); $\C{// is true, \ie the size of referenced object}$1840 sizeof( &ar[1] ) == sizeof( int *) $\C{// is true, \ie the size of a reference}$1848 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§ 1849 &ar[1] = &w; §\C{// change reference array element}§ 1850 typeof( ar[1] ) p; §\C{// (gcc) is int, \ie the type of referenced object}§ 1851 typeof( &ar[1] ) q; §\C{// (gcc) is int \&, \ie the type of reference}§ 1852 sizeof( ar[1] ) == sizeof( int ); §\C{// is true, \ie the size of referenced object}§ 1853 sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, \ie the size of a reference}§ 1841 1854 \end{cfa} 1842 1855 1843 1856 In contrast to \CFA reference types, \Index*[C++]{\CC{}}'s reference types are all ©const© references, preventing changes to the reference address, so only value assignment is possible, which eliminates half of the \Index{address duality}. 1844 1857 Also, \CC does not allow \Index{array}s\index{array!reference} of reference\footnote{ 1845 The reason for disallowing arrays of reference is unknown, but possibly comes from references being ethereal (like a textual macro), and hence, replaceable by the refer ent object.}1858 The reason for disallowing arrays of reference is unknown, but possibly comes from references being ethereal (like a textual macro), and hence, replaceable by the referant object.} 1846 1859 \Index*{Java}'s reference types to objects (all Java objects are on the heap) are like C pointers, which always manipulate the address, and there is no (bit-wise) object assignment, so objects are explicitly cloned by shallow or deep copying, which eliminates half of the address duality. 1847 1860 … … 1855 1868 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 1856 1869 \begin{cfa} 1857 int * p = &x; $\C{// assign address of x}$1858 @int * p = x;@ $\C{// assign value of x}$ 1859 int & r = x; $\C{// must have address of x}$1870 int * p = &x; §\C{// assign address of x}§ 1871 ®int * p = x;® §\C{// assign value of x}§ 1872 int & r = x; §\C{// must have address of x}§ 1860 1873 \end{cfa} 1861 1874 Like the previous example with C pointer-arithmetic, it is unlikely assigning the value of ©x© into a pointer is meaningful (again, a warning is usually given). … … 1866 1879 Similarly, when a reference type is used for a parameter/return type, the call-site argument does not require a reference operator for the same reason. 1867 1880 \begin{cfa} 1868 int & f( int & r ); $\C{// reference parameter and return}$1869 z = f( x ) + f( y ); $\C{// reference operator added, temporaries needed for call results}$1881 int & f( int & r ); §\C{// reference parameter and return}§ 1882 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§ 1870 1883 \end{cfa} 1871 1884 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©. … … 1880 1893 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. 1881 1894 \begin{cfa} 1882 void f( @const@int & cr );1883 void g( @const@int * cp );1884 f( 3 ); g( @&@3 );1885 f( x + y ); g( @&@(x + y) );1895 void f( ®const® int & cr ); 1896 void g( ®const® int * cp ); 1897 f( 3 ); g( ®&®3 ); 1898 f( x + y ); g( ®&®(x + y) ); 1886 1899 \end{cfa} 1887 1900 Here, 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. … … 1894 1907 void f( int & r ); 1895 1908 void g( int * p ); 1896 f( 3 ); g( @&@3 ); $\C{// compiler implicit generates temporaries}$1897 f( x + y ); g( @&@(x + y) ); $\C{// compiler implicit generates temporaries}$1909 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§ 1910 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§ 1898 1911 \end{cfa} 1899 1912 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ … … 1906 1919 \begin{cfa} 1907 1920 void f( int i ); 1908 void (* fp)( int ); $\C{// routine pointer}$1909 fp = f; $\C{// reference initialization}$1910 fp = &f; $\C{// pointer initialization}$1911 fp = *f; $\C{// reference initialization}$1912 fp(3); $\C{// reference invocation}$1913 (*fp)(3); $\C{// pointer invocation}$1921 void (* fp)( int ); §\C{// routine pointer}§ 1922 fp = f; §\C{// reference initialization}§ 1923 fp = &f; §\C{// pointer initialization}§ 1924 fp = *f; §\C{// reference initialization}§ 1925 fp(3); §\C{// reference invocation}§ 1926 (*fp)(3); §\C{// pointer invocation}§ 1914 1927 \end{cfa} 1915 1928 While C's treatment of routine objects has similarity to inferring a reference type in initialization contexts, the examples are assignment not initialization, and all possible forms of assignment are possible (©f©, ©&f©, ©*f©) without regard for type. 1916 1929 Instead, a routine object should be referenced by a ©const© reference: 1917 1930 \begin{cfa} 1918 @const@ void (@&@ fr)( int ) = f; $\C{// routine reference}$ 1919 fr = ... $\C{// error, cannot change code}$1920 &fr = ...; $\C{// changing routine reference}$1921 fr( 3 ); $\C{// reference call to f}$1922 (*fr)(3); $\C{// error, incorrect type}$1931 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§ 1932 fr = ... §\C{// error, cannot change code}§ 1933 &fr = ...; §\C{// changing routine reference}§ 1934 fr( 3 ); §\C{// reference call to f}§ 1935 (*fr)(3); §\C{// error, incorrect type}§ 1923 1936 \end{cfa} 1924 1937 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ … … 1933 1946 \begin{itemize} 1934 1947 \item 1935 if ©R© is an \Index{rvalue} of type ©T &©$_1\cdots$ ©&©$_r$, where $r \ge 1$ references (©&© symbols), than ©&R© has type ©T ©\R{©*©}©&©\R{$_2$}$\cdots$ ©&©\R{$_r$}, \ie ©T© pointer with $r-1$ references (©&© symbols).1936 1937 \item 1938 if ©L© is an \Index{lvalue} of type ©T &©$_1\cdots$ ©&©$_l$, where $l \ge 0$ references (©&© symbols), than ©&L© has type ©T ©\R{©*©}©&©\R{$_1$}$\cdots$ ©&©\R{$_l$}, \ie ©T© pointer with $l$ references (©&© symbols).1948 if ©R© is an \Index{rvalue} of type ©T &©$_1\cdots$ ©&©$_r$, where $r \ge 1$ references (©&© symbols), than ©&R© has type ©T ®*®&©$_{\color{red}2}\cdots$ ©&©$_{\color{red}r}$, \ie ©T© pointer with $r-1$ references (©&© symbols). 1949 1950 \item 1951 if ©L© is an \Index{lvalue} of type ©T &©$_1\cdots$ ©&©$_l$, where $l \ge 0$ references (©&© symbols), than ©&L© has type ©T ®*®&©$_{\color{red}1}\cdots$ ©&©$_{\color{red}l}$, \ie ©T© pointer with $l$ references (©&© symbols). 1939 1952 \end{itemize} 1940 1953 The following example shows the first rule applied to different \Index{rvalue} contexts: … … 1942 1955 int x, * px, ** ppx, *** pppx, **** ppppx; 1943 1956 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 1944 x = rrrx; $\C[2.0in]{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$1945 px = &rrrx; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}$1946 ppx = &&rrrx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}$1947 pppx = &&&rrrx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}$1948 ppppx = &&&&rrrx; $\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}$1957 x = rrrx; §\C[2.0in]{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§ 1958 px = &rrrx; §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (\&x)}§ 1959 ppx = &&rrrx; §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (\&rx)}§ 1960 pppx = &&&rrrx; §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (\&rrx)}§ 1961 ppppx = &&&&rrrx; §\C{// starting from \&\&\&rrrx, \&\&\&\&rrrx is an rvalue with type int **** (\&rrrx)}§ 1949 1962 \end{cfa} 1950 1963 The following example shows the second rule applied to different \Index{lvalue} contexts: … … 1952 1965 int x, * px, ** ppx, *** pppx; 1953 1966 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 1954 rrrx = 2; $\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}$1955 &rrrx = px; $\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}$1956 &&rrrx = ppx; $\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}$1957 &&&rrrx = pppx; $\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}\CRT$1967 rrrx = 2; §\C{// rrrx is an lvalue with type int \&\&\& (equivalent to x)}§ 1968 &rrrx = px; §\C{// starting from rrrx, \&rrrx is an rvalue with type int *\&\&\& (rx)}§ 1969 &&rrrx = ppx; §\C{// starting from \&rrrx, \&\&rrrx is an rvalue with type int **\&\& (rrx)}§ 1970 &&&rrrx = pppx; §\C{// starting from \&\&rrrx, \&\&\&rrrx is an rvalue with type int ***\& (rrrx)}\CRT§ 1958 1971 \end{cfa} 1959 1972 … … 1968 1981 \begin{cfa} 1969 1982 int x; 1970 x + 1; $\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}$1983 x + 1; §\C[2.0in]{// lvalue variable (int) converts to rvalue for expression}§ 1971 1984 \end{cfa} 1972 1985 An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped. … … 1978 1991 \begin{cfa} 1979 1992 int x, &r = x, f( int p ); 1980 x = @r@ + f( @r@ ); $\C{// lvalue reference converts to rvalue}$1993 x = ®r® + f( ®r® ); §\C{// lvalue reference converts to rvalue}§ 1981 1994 \end{cfa} 1982 1995 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped. … … 1985 1998 lvalue to reference conversion: \lstinline[deletekeywords=lvalue]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references. 1986 1999 \begin{cfa} 1987 int x, &r = @x@, f( int & p ); $\C{// lvalue variable (int) convert to reference (int \&)}$1988 f( @x@ ); $\C{// lvalue variable (int) convert to reference (int \&)}$2000 int x, &r = ®x®, f( int & p ); §\C{// lvalue variable (int) convert to reference (int \&)}§ 2001 f( ®x® ); §\C{// lvalue variable (int) convert to reference (int \&)}§ 1989 2002 \end{cfa} 1990 2003 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost. … … 1996 2009 \begin{cfa} 1997 2010 int x, & f( int & p ); 1998 f( @x + 3@ ); $\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}$1999 @&f@(...) = &x; $\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT$ 2011 f( ®x + 3® ); §\C[1.5in]{// rvalue parameter (int) implicitly converts to lvalue temporary reference (int \&)}§ 2012 ®&f®(...) = &x; §\C{// rvalue result (int \&) implicitly converts to lvalue temporary reference (int \&)}\CRT§ 2000 2013 \end{cfa} 2001 2014 In both case, modifications to the temporary are inaccessible (\Index{warning}). … … 2169 2182 The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: 2170 2183 \begin{cfa} 2171 @[ int o1, int o2, char o3 ]@f( int i1, char i2, char i3 ) {2172 $\emph{routine body}$2184 ®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) { 2185 §\emph{routine body}§ 2173 2186 } 2174 2187 \end{cfa} … … 2181 2194 Declaration qualifiers can only appear at the start of a routine definition, \eg: 2182 2195 \begin{cfa} 2183 @extern@ [ int x ] g( int y ) {$\,$}2196 ®extern® [ int x ] g( int y ) {§\,§} 2184 2197 \end{cfa} 2185 2198 Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified; 2186 2199 in 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: 2187 2200 \begin{cfa} 2188 [ $\,$] g(); $\C{// no input or output parameters}$2189 [ void ] g( void ); $\C{// no input or output parameters}$2201 [§\,§] g(); §\C{// no input or output parameters}§ 2202 [ void ] g( void ); §\C{// no input or output parameters}§ 2190 2203 \end{cfa} 2191 2204 … … 2205 2218 \begin{cfa} 2206 2219 typedef int foo; 2207 int f( int (* foo) ); $\C{// foo is redefined as a parameter name}$2220 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 2208 2221 \end{cfa} 2209 2222 The 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. … … 2213 2226 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 2214 2227 \begin{cfa} 2215 [ int ] f( * int, int * ); $\C{// returns an integer, accepts 2 pointers to integers}$2216 [ * int, int * ] f( int ); $\C{// returns 2 pointers to integers, accepts an integer}$2228 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§ 2229 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§ 2217 2230 \end{cfa} 2218 2231 The 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: 2219 2232 \begin{cfa} 2220 2233 #define ptoa( n, d ) int (*n)[ d ] 2221 int f( ptoa( p, 5 ) ) ... $\C{// expands to int f( int (*p)[ 5 ] )}$2222 [ int ] f( ptoa( p, 5 ) ) ... $\C{// expands to [ int ] f( int (*p)[ 5 ] )}$2234 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§ 2235 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§ 2223 2236 \end{cfa} 2224 2237 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. … … 2239 2252 \begin{minipage}{\linewidth} 2240 2253 \begin{cfa} 2241 @[ int x, int y ]@f() {2254 ®[ int x, int y ]® f() { 2242 2255 int z; 2243 2256 ... x = 0; ... y = z; ... 2244 @return;@ $\C{// implicitly return x, y}$2257 ®return;® §\C{// implicitly return x, y}§ 2245 2258 } 2246 2259 \end{cfa} … … 2252 2265 [ int x, int y ] f() { 2253 2266 ... 2254 } $\C{// implicitly return x, y}$2267 } §\C{// implicitly return x, y}§ 2255 2268 \end{cfa} 2256 2269 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. … … 2261 2274 [ int x, int y ] f( int, x, int y ) { 2262 2275 ... 2263 } $\C{// implicitly return x, y}$2276 } §\C{// implicitly return x, y}§ 2264 2277 \end{cfa} 2265 2278 This notation allows the compiler to eliminate temporary variables in nested routine calls. 2266 2279 \begin{cfa} 2267 [ int x, int y ] f( int, x, int y ); $\C{// prototype declaration}$2280 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§ 2268 2281 int a, b; 2269 2282 [a, b] = f( f( f( a, b ) ) ); … … 2279 2292 as well, parameter names are optional, \eg: 2280 2293 \begin{cfa} 2281 [ int x ] f (); $\C{// returning int with no parameters}$2282 [ * int ] g (int y); $\C{// returning pointer to int with int parameter}$2283 [ ] h ( int, char ); $\C{// returning no result with int and char parameters}$2284 [ * int, int ] j ( int ); $\C{// returning pointer to int and int, with int parameter}$2294 [ int x ] f (); §\C{// returning int with no parameters}§ 2295 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§ 2296 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§ 2297 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§ 2285 2298 \end{cfa} 2286 2299 This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). 2287 Like C, it is possible to declare multiple routine-prototypes in a single declaration, where the return type is distributed across \emph{all} routine names in the declaration list \see{\VRef{s:AlternativeDeclarations}}, \eg:2300 Like C, it is possible to declare multiple routine-prototypes in a single declaration, where the return type is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:AlternativeDeclarations}), \eg: 2288 2301 \begin{cfa} 2289 2302 C : const double bar1(), bar2( int ), bar3( double ); 2290 $\CFA$: [const double] foo(), foo( int ), foo( double ) { return 3.0; }2303 §\CFA§: [const double] foo(), foo( int ), foo( double ) { return 3.0; } 2291 2304 \end{cfa} 2292 2305 \CFA allows the last routine in the list to define its body. … … 2303 2316 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 2304 2317 \begin{cfa} 2305 * [ int x ] () fp; $\C[2.25in]{// pointer to routine returning int with no parameters}$2306 * [ * int ] (int y) gp; $\C{// pointer to routine returning pointer to int with int parameter}$2307 * [ ] (int,char) hp; $\C{// pointer to routine returning no result with int and char parameters}$2308 * [ * int,int ] ( int ) jp; $\C{// pointer to routine returning pointer to int and int, with int parameter}\CRT$2318 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§ 2319 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§ 2320 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§ 2321 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§ 2309 2322 \end{cfa} 2310 2323 While parameter names are optional, \emph{a routine name cannot be specified}; 2311 2324 for example, the following is incorrect: 2312 2325 \begin{cfa} 2313 * [ int x ] f () fp; $\C{// routine name "f" is not allowed}$2326 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§ 2314 2327 \end{cfa} 2315 2328 … … 2334 2347 whereas a named (keyword) call may be: 2335 2348 \begin{cfa} 2336 p( z : 3, x : 4, y : 7 ); $\C{// rewrite \(\Rightarrow\) p( 4, 7, 3 )}$2349 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§ 2337 2350 \end{cfa} 2338 2351 Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. … … 2351 2364 For example, the following routine prototypes and definition are all valid. 2352 2365 \begin{cfa} 2353 void p( int, int, int ); $\C{// equivalent prototypes}$2366 void p( int, int, int ); §\C{// equivalent prototypes}§ 2354 2367 void p( int x, int y, int z ); 2355 2368 void p( int y, int x, int z ); 2356 2369 void p( int z, int y, int x ); 2357 void p( int q, int r, int s ) {} $\C{// match with this definition}$2370 void p( int q, int r, int s ) {} §\C{// match with this definition}§ 2358 2371 \end{cfa} 2359 2372 Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. … … 2367 2380 int f( int x, double y ); 2368 2381 2369 f( j : 3, i : 4 ); $\C{// 1st f}$2370 f( x : 7, y : 8.1 ); $\C{// 2nd f}$2371 f( 4, 5 ); $\C{// ambiguous call}$2382 f( j : 3, i : 4 ); §\C{// 1st f}§ 2383 f( x : 7, y : 8.1 ); §\C{// 2nd f}§ 2384 f( 4, 5 ); §\C{// ambiguous call}§ 2372 2385 \end{cfa} 2373 2386 However, named arguments compound routine resolution in conjunction with conversions: 2374 2387 \begin{cfa} 2375 f( i : 3, 5.7 ); $\C{// ambiguous call ?}$2388 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§ 2376 2389 \end{cfa} 2377 2390 Depending on the cost associated with named arguments, this call could be resolvable or ambiguous. … … 2387 2400 the allowable positional calls are: 2388 2401 \begin{cfa} 2389 p(); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$2390 p( 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$2391 p( 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$2392 p( 4, 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 4 )}$2402 p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 2403 p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 2404 p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 2405 p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§ 2393 2406 // empty arguments 2394 p( , 4, 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 4 )}$2395 p( 4, , 4 ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 4 )}$2396 p( 4, 4, ); $\C{// rewrite \(\Rightarrow\) p( 4, 4, 3 )}$2397 p( 4, , ); $\C{// rewrite \(\Rightarrow\) p( 4, 2, 3 )}$2398 p( , 4, ); $\C{// rewrite \(\Rightarrow\) p( 1, 4, 3 )}$2399 p( , , 4 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 4 )}$2400 p( , , ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 3 )}$2407 p( , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§ 2408 p( 4, , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§ 2409 p( 4, 4, ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 2410 p( 4, , ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 2411 p( , 4, ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§ 2412 p( , , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§ 2413 p( , , ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 2401 2414 \end{cfa} 2402 2415 Here the missing arguments are inserted from the default values in the parameter list. … … 2422 2435 Default values may only appear in a prototype versus definition context: 2423 2436 \begin{cfa} 2424 void p( int x, int y = 2, int z = 3 ); $\C{// prototype: allowed}$2425 void p( int, int = 2, int = 3 ); $\C{// prototype: allowed}$2426 void p( int x, int y = 2, int z = 3 ) {} $\C{// definition: not allowed}$2437 void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§ 2438 void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§ 2439 void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§ 2427 2440 \end{cfa} 2428 2441 The reason for this restriction is to allow separate compilation. … … 2439 2452 \begin{cfa} 2440 2453 p( int x, int y, int z, ... ); 2441 p( 1, 4, 5, 6, z : 3, y : 2 ); $\C{// assume p( /* positional */, ... , /* named */ );}$2442 p( 1, z : 3, y : 2, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$2454 p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§ 2455 p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§ 2443 2456 \end{cfa} 2444 2457 In 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. … … 2449 2462 \begin{cfa} 2450 2463 void p( int x, int y = 2, int z = 3... ); 2451 p( 1, 4, 5, 6, z : 3 ); $\C{// assume p( /* positional */, ... , /* named */ );}$2452 p( 1, z : 3, 4, 5, 6 ); $\C{// assume p( /* positional */, /* named */, ... );}$2464 p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§ 2465 p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§ 2453 2466 \end{cfa} 2454 2467 The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; … … 2456 2469 In 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. 2457 2470 For these reasons, \CFA requires named arguments before ellipse arguments. 2458 Finally, 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{\VRef{s:Overloading}}, making much of this discussion moot.2459 2460 Default arguments and overloading \see{\VRef{s:Overloading}}are complementary.2471 Finally, 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. 2472 2473 Default arguments and overloading (see Section 24) are complementary. 2461 2474 While in theory default arguments can be simulated with overloading, as in: 2462 2475 \begin{cquote} … … 2480 2493 Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: 2481 2494 \begin{cfa} 2482 p( 1, /* default */, 5 ); $\C{// rewrite \(\Rightarrow\) p( 1, 2, 5 )}$2495 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§ 2483 2496 \end{cfa} 2484 2497 … … 2493 2506 \begin{cfa} 2494 2507 struct { 2495 int f1; $\C{// named field}$2496 int f2 : 4; $\C{// named field with bit field size}$2497 int : 3; $\C{// unnamed field for basic type with bit field size}$2498 int ; $\C{// disallowed, unnamed field}$2499 int *; $\C{// disallowed, unnamed field}$2500 int (*)( int ); $\C{// disallowed, unnamed field}$2508 int f1; §\C{// named field}§ 2509 int f2 : 4; §\C{// named field with bit field size}§ 2510 int : 3; §\C{// unnamed field for basic type with bit field size}§ 2511 int ; §\C{// disallowed, unnamed field}§ 2512 int *; §\C{// disallowed, unnamed field}§ 2513 int (*)( int ); §\C{// disallowed, unnamed field}§ 2501 2514 }; 2502 2515 \end{cfa} … … 2506 2519 \begin{cfa} 2507 2520 struct { 2508 int , , ; $\C{// 3 unnamed fields}$2521 int , , ; §\C{// 3 unnamed fields}§ 2509 2522 } 2510 2523 \end{cfa} … … 2518 2531 \subsection{Type Nesting} 2519 2532 2520 \CFA allows \Index{type nesting}, and type qualification of the nested types \see{\VRef[Figure]{f:TypeNestingQualification}}, where as C hoists\index{type hoisting} (refactors) nested types into the enclosing scope and has no type qualification.2533 \CFA allows \Index{type nesting}, and type qualification of the nested types (see \VRef[Figure]{f:TypeNestingQualification}), where as C hoists\index{type hoisting} (refactors) nested types into the enclosing scope and has no type qualification. 2521 2534 \begin{figure} 2522 2535 \centering … … 2574 2587 2575 2588 int fred() { 2576 s.t.c = @S.@R; // type qualification2577 struct @S.@T t = { @S.@R, 1, 2 };2578 enum @S.@C c;2579 union @S.T.@U u;2589 s.t.c = ®S.®R; // type qualification 2590 struct ®S.®T t = { ®S.®R, 1, 2 }; 2591 enum ®S.®C c; 2592 union ®S.T.®U u; 2580 2593 } 2581 2594 \end{cfa} … … 2600 2613 const unsigned int size = 5; 2601 2614 int ia[size]; 2602 ... $\C{// assign values to array ia}$2603 qsort( ia, size ); $\C{// sort ascending order using builtin ?<?}$2615 ... §\C{// assign values to array ia}§ 2616 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§ 2604 2617 { 2605 @int ?<?( int x, int y ) { return x > y; }@ $\C{// nested routine}$2606 qsort( ia, size ); $\C{// sort descending order by local redefinition}$2618 ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§ 2619 qsort( ia, size ); §\C{// sort descending order by local redefinition}§ 2607 2620 } 2608 2621 \end{cfa} … … 2612 2625 The following program in undefined in \CFA (and Indexc{gcc}) 2613 2626 \begin{cfa} 2614 [* [int]( int )] foo() { $\C{// int (* foo())( int )}$2615 int @i@= 7;2627 [* [int]( int )] foo() { §\C{// int (* foo())( int )}§ 2628 int ®i® = 7; 2616 2629 int bar( int p ) { 2617 @i@ += 1; $\C{// dependent on local variable}$2618 sout | @i@;2630 ®i® += 1; §\C{// dependent on local variable}§ 2631 sout | ®i®; 2619 2632 } 2620 return bar; $\C{// undefined because of local dependence}$2633 return bar; §\C{// undefined because of local dependence}§ 2621 2634 } 2622 2635 int main() { 2623 * [int]( int ) fp = foo(); $\C{// int (* fp)( int )}$2636 * [int]( int ) fp = foo(); §\C{// int (* fp)( int )}§ 2624 2637 sout | fp( 3 ); 2625 2638 } … … 2634 2647 In C and \CFA, lists of elements appear in several contexts, such as the parameter list of a routine call. 2635 2648 \begin{cfa} 2636 f( @2, x, 3 + i@ ); $\C{// element list}$2649 f( ®2, x, 3 + i® ); §\C{// element list}§ 2637 2650 \end{cfa} 2638 2651 A list of elements is called a \newterm{tuple}, and is different from a \Index{comma expression}. … … 2643 2656 2644 2657 In C and most programming languages, functions return at most one value; 2645 however, many operations have multiple outcomes, some exceptional \see{\VRef{s:ExceptionHandling}}.2658 however, many operations have multiple outcomes, some exceptional (see~\VRef{s:ExceptionHandling}). 2646 2659 To emulate functions with multiple return values, \emph{\Index{aggregation}} and/or \emph{\Index{aliasing}} is used. 2647 2660 … … 2649 2662 For example, consider C's \Indexc{div} function, which returns the quotient and remainder for a division of an integer value. 2650 2663 \begin{cfa} 2651 typedef struct { int quot, rem; } div_t; $\C[7cm]{// from include stdlib.h}$2664 typedef struct { int quot, rem; } div_t; §\C[7cm]{// from include stdlib.h}§ 2652 2665 div_t div( int num, int den ); 2653 div_t qr = div( 13, 5 ); $\C{// return quotient/remainder aggregate}$2654 printf( "%d %d\n", qr.quot, qr.rem ); $\C{// print quotient/remainder}$2666 div_t qr = div( 13, 5 ); §\C{// return quotient/remainder aggregate}§ 2667 printf( "%d %d\n", qr.quot, qr.rem ); §\C{// print quotient/remainder}§ 2655 2668 \end{cfa} 2656 2669 This approach requires a name for the return type and fields, where \Index{naming} is a common programming-language issue. … … 2662 2675 For example, consider C's \Indexc{modf} function, which returns the integral and fractional part of a floating value. 2663 2676 \begin{cfa} 2664 double modf( double x, double * i ); $\C{// from include math.h}$2665 double intp, frac = modf( 13.5, &intp ); $\C{// return integral and fractional components}$2666 printf( "%g %g\n", intp, frac ); $\C{// print integral/fractional components}$2677 double modf( double x, double * i ); §\C{// from include math.h}§ 2678 double intp, frac = modf( 13.5, &intp ); §\C{// return integral and fractional components}§ 2679 printf( "%g %g\n", intp, frac ); §\C{// print integral/fractional components}§ 2667 2680 \end{cfa} 2668 2681 This approach requires allocating storage for the return values, which complicates the call site with a sequence of variable declarations leading to the call. … … 2691 2704 When a function call is passed as an argument to another call, the best match of actual arguments to formal parameters is evaluated given all possible expression interpretations in the current scope. 2692 2705 \begin{cfa} 2693 void g( int, int ); $\C{// 1}$2694 void g( double, double ); $\C{// 2}$2695 g( div( 13, 5 ) ); $\C{// select 1}$2696 g( modf( 13.5 ) ); $\C{// select 2}$2706 void g( int, int ); §\C{// 1}§ 2707 void g( double, double ); §\C{// 2}§ 2708 g( div( 13, 5 ) ); §\C{// select 1}§ 2709 g( modf( 13.5 ) ); §\C{// select 2}§ 2697 2710 \end{cfa} 2698 2711 In this case, there are two overloaded ©g© routines. … … 2703 2716 The previous examples can be rewritten passing the multiple returned-values directly to the ©printf© function call. 2704 2717 \begin{cfa} 2705 [ int, int ] div( int x, int y ); $\C{// from include stdlib}$2706 printf( "%d %d\n", div( 13, 5 ) ); $\C{// print quotient/remainder}$2707 2708 [ double, double ] modf( double x ); $\C{// from include math}$2709 printf( "%g %g\n", modf( 13.5 ) ); $\C{// print integral/fractional components}$2718 [ int, int ] div( int x, int y ); §\C{// from include stdlib}§ 2719 printf( "%d %d\n", div( 13, 5 ) ); §\C{// print quotient/remainder}§ 2720 2721 [ double, double ] modf( double x ); §\C{// from include math}§ 2722 printf( "%g %g\n", modf( 13.5 ) ); §\C{// print integral/fractional components}§ 2710 2723 \end{cfa} 2711 2724 This approach provides the benefits of compile-time checking for appropriate return statements as in aggregation, but without the required verbosity of declaring a new named type. … … 2717 2730 \begin{cfa} 2718 2731 int quot, rem; 2719 [ quot, rem ] = div( 13, 5 ); $\C{// assign multiple variables}$2720 printf( "%d %d\n", quot, rem ); $\C{// print quotient/remainder}\CRT$2732 [ quot, rem ] = div( 13, 5 ); §\C{// assign multiple variables}§ 2733 printf( "%d %d\n", quot, rem ); §\C{// print quotient/remainder}\CRT§ 2721 2734 \end{cfa} 2722 2735 Here, the multiple return-values are matched in much the same way as passing multiple return-values to multiple parameters in a call. … … 2747 2760 In \CFA, it is possible to overcome this restriction by declaring a \newterm{tuple variable}. 2748 2761 \begin{cfa} 2749 [int, int] @qr@ = div( 13, 5 ); $\C{// initialize tuple variable}$2750 printf( "%d %d\n", @qr@ ); $\C{// print quotient/remainder}$2762 [int, int] ®qr® = div( 13, 5 ); §\C{// initialize tuple variable}§ 2763 printf( "%d %d\n", ®qr® ); §\C{// print quotient/remainder}§ 2751 2764 \end{cfa} 2752 2765 It is now possible to match the multiple return-values to a single variable, in much the same way as \Index{aggregation}. … … 2754 2767 One way to access the individual components of a tuple variable is with assignment. 2755 2768 \begin{cfa} 2756 [ quot, rem ] = qr; $\C{// assign multiple variables}$2769 [ quot, rem ] = qr; §\C{// assign multiple variables}§ 2757 2770 \end{cfa} 2758 2771 … … 2777 2790 [int, double] * p; 2778 2791 2779 int y = x.0; $\C{// access int component of x}$2780 y = f().1; $\C{// access int component of f}$2781 p->0 = 5; $\C{// access int component of tuple pointed-to by p}$2782 g( x.1, x.0 ); $\C{// rearrange x to pass to g}$2783 double z = [ x, f() ].0.1; $\C{// access second component of first component of tuple expression}$2792 int y = x.0; §\C{// access int component of x}§ 2793 y = f().1; §\C{// access int component of f}§ 2794 p->0 = 5; §\C{// access int component of tuple pointed-to by p}§ 2795 g( x.1, x.0 ); §\C{// rearrange x to pass to g}§ 2796 double z = [ x, f() ].0.1; §\C{// access second component of first component of tuple expression}§ 2784 2797 \end{cfa} 2785 2798 Tuple-index expressions can occur on any tuple-typed expression, including tuple-returning functions, square-bracketed tuple expressions, and other tuple-index expressions, provided the retrieved component is also a tuple. … … 2788 2801 2789 2802 \subsection{Flattening and Structuring} 2790 \label{s:FlatteningStructuring}2791 2803 2792 2804 As evident in previous examples, tuples in \CFA do not have a rigid structure. … … 2849 2861 double y; 2850 2862 [int, double] z; 2851 [y, x] = 3.14; $\C{// mass assignment}$2852 [x, y] = z; $\C{// multiple assignment}$2853 z = 10; $\C{// mass assignment}$2854 z = [x, y]; $\C{// multiple assignment}$2863 [y, x] = 3.14; §\C{// mass assignment}§ 2864 [x, y] = z; §\C{// multiple assignment}§ 2865 z = 10; §\C{// mass assignment}§ 2866 z = [x, y]; §\C{// multiple assignment}§ 2855 2867 \end{cfa} 2856 2868 Let $L_i$ for $i$ in $[0, n)$ represent each component of the flattened left side, $R_i$ represent each component of the flattened right side of a multiple assignment, and $R$ represent the right side of a mass assignment. … … 2860 2872 \begin{cfa} 2861 2873 [ int, int ] x, y, z; 2862 [ x, y ] = z; $\C{// multiple assignment, invalid 4 != 2}$2874 [ x, y ] = z; §\C{// multiple assignment, invalid 4 != 2}§ 2863 2875 \end{cfa} 2864 2876 Multiple assignment assigns $R_i$ to $L_i$ for each $i$. … … 2896 2908 double c, d; 2897 2909 [ void ] f( [ int, int ] ); 2898 f( [ c, a ] = [ b, d ] = 1.5 ); $\C{// assignments in parameter list}$2910 f( [ c, a ] = [ b, d ] = 1.5 ); §\C{// assignments in parameter list}§ 2899 2911 \end{cfa} 2900 2912 The tuple expression begins with a mass assignment of ©1.5© into ©[b, d]©, which assigns ©1.5© into ©b©, which is truncated to ©1©, and ©1.5© into ©d©, producing the tuple ©[1, 1.5]© as a result. … … 2909 2921 \begin{cfa} 2910 2922 struct S; 2911 void ?{}(S *); $\C{// (1)}$2912 void ?{}(S *, int); $\C{// (2)}$2913 void ?{}(S * double); $\C{// (3)}$2914 void ?{}(S *, S); $\C{// (4)}$2915 2916 [S, S] x = [3, 6.28]; $\C{// uses (2), (3), specialized constructors}$2917 [S, S] y; $\C{// uses (1), (1), default constructor}$2918 [S, S] z = x.0; $\C{// uses (4), (4), copy constructor}$2923 void ?{}(S *); §\C{// (1)}§ 2924 void ?{}(S *, int); §\C{// (2)}§ 2925 void ?{}(S * double); §\C{// (3)}§ 2926 void ?{}(S *, S); §\C{// (4)}§ 2927 2928 [S, S] x = [3, 6.28]; §\C{// uses (2), (3), specialized constructors}§ 2929 [S, S] y; §\C{// uses (1), (1), default constructor}§ 2930 [S, S] z = x.0; §\C{// uses (4), (4), copy constructor}§ 2919 2931 \end{cfa} 2920 2932 In this example, ©x© is initialized by the multiple constructor calls ©?{}(&x.0, 3)© and ©?{}(&x.1, 6.28)©, while ©y© is initialized by two default constructor calls ©?{}(&y.0)© and ©?{}(&y.1)©. … … 2957 2969 A member-access tuple may be used anywhere a tuple can be used, \eg: 2958 2970 \begin{cfa} 2959 s.[ y, z, x ] = [ 3, 3.2, 'x' ]; $\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}$2960 f( s.[ y, z ] ); $\C{// equivalent to f( s.y, s.z )}$2971 s.[ y, z, x ] = [ 3, 3.2, 'x' ]; §\C{// equivalent to s.x = 'x', s.y = 3, s.z = 3.2}§ 2972 f( s.[ y, z ] ); §\C{// equivalent to f( s.y, s.z )}§ 2961 2973 \end{cfa} 2962 2974 Note, the fields appearing in a record-field tuple may be specified in any order; … … 2968 2980 void f( double, long ); 2969 2981 2970 f( x.[ 0, 3 ] ); $\C{// f( x.0, x.3 )}$2971 x.[ 0, 1 ] = x.[ 1, 0 ]; $\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}$2982 f( x.[ 0, 3 ] ); §\C{// f( x.0, x.3 )}§ 2983 x.[ 0, 1 ] = x.[ 1, 0 ]; §\C{// [ x.0, x.1 ] = [ x.1, x.0 ]}§ 2972 2984 [ long, int, long ] y = x.[ 2, 0, 2 ]; 2973 2985 \end{cfa} … … 2986 2998 \begin{cfa} 2987 2999 [ int, float, double ] f(); 2988 [ double, float ] x = f().[ 2, 1 ]; $\C{// f() called once}$3000 [ double, float ] x = f().[ 2, 1 ]; §\C{// f() called once}§ 2989 3001 \end{cfa} 2990 3002 … … 2999 3011 That is, a cast can be used to select the type of an expression when it is ambiguous, as in the call to an overloaded function. 3000 3012 \begin{cfa} 3001 int f(); $\C{// (1)}$3002 double f(); $\C{// (2)}$3003 3004 f(); $\C{// ambiguous - (1),(2) both equally viable}$3005 (int)f(); $\C{// choose (2)}$3013 int f(); §\C{// (1)}§ 3014 double f(); §\C{// (2)}§ 3015 3016 f(); §\C{// ambiguous - (1),(2) both equally viable}§ 3017 (int)f(); §\C{// choose (2)}§ 3006 3018 \end{cfa} 3007 3019 Since casting is a fundamental operation in \CFA, casts need to be given a meaningful interpretation in the context of tuples. … … 3011 3023 void g(); 3012 3024 3013 (void)f(); $\C{// valid, ignore results}$3014 (int)g(); $\C{// invalid, void cannot be converted to int}$3025 (void)f(); §\C{// valid, ignore results}§ 3026 (int)g(); §\C{// invalid, void cannot be converted to int}§ 3015 3027 3016 3028 struct A { int x; }; 3017 (struct A)f(); $\C{// invalid, int cannot be converted to A}$3029 (struct A)f(); §\C{// invalid, int cannot be converted to A}§ 3018 3030 \end{cfa} 3019 3031 In C, line 4 is a valid cast, which calls ©f© and discards its result. … … 3031 3043 [int, [int, int], int] g(); 3032 3044 3033 ([int, double])f(); $\C{// (1) valid}$3034 ([int, int, int])g(); $\C{// (2) valid}$3035 ([void, [int, int]])g(); $\C{// (3) valid}$3036 ([int, int, int, int])g(); $\C{// (4) invalid}$3037 ([int, [int, int, int]])g(); $\C{// (5) invalid}$3045 ([int, double])f(); §\C{// (1) valid}§ 3046 ([int, int, int])g(); §\C{// (2) valid}§ 3047 ([void, [int, int]])g(); §\C{// (3) valid}§ 3048 ([int, int, int, int])g(); §\C{// (4) invalid}§ 3049 ([int, [int, int, int]])g(); §\C{// (5) invalid}§ 3038 3050 \end{cfa} 3039 3051 … … 3095 3107 void f([int, int], int, int); 3096 3108 3097 f([0, 0], 0, 0); $\C{// no cost}$3098 f(0, 0, 0, 0); $\C{// cost for structuring}$3099 f([0, 0,], [0, 0]); $\C{// cost for flattening}$3100 f([0, 0, 0], 0); $\C{// cost for flattening and structuring}$3109 f([0, 0], 0, 0); §\C{// no cost}§ 3110 f(0, 0, 0, 0); §\C{// cost for structuring}§ 3111 f([0, 0,], [0, 0]); §\C{// cost for flattening}§ 3112 f([0, 0, 0], 0); §\C{// cost for flattening and structuring}§ 3101 3113 \end{cfa} 3102 3114 … … 3134 3146 The general syntax of a lexical list is: 3135 3147 \begin{cfa} 3136 [ $\emph{exprlist}$]3148 [ §\emph{exprlist}§ ] 3137 3149 \end{cfa} 3138 3150 where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas. … … 3146 3158 Tuples are permitted to contain sub-tuples (\ie nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple. 3147 3159 Note, a tuple is not a record (structure); 3148 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure \see{flattening coercion in \VRef{s:FlatteningStructuring}}.3160 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1). 3149 3161 In essence, tuples are largely a compile time phenomenon, having little or no runtime presence. 3150 3162 … … 3154 3166 The general syntax of a tuple type is: 3155 3167 \begin{cfa} 3156 [ $\emph{typelist}$]3168 [ §\emph{typelist}§ ] 3157 3169 \end{cfa} 3158 3170 where ©$\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. … … 3161 3173 [ unsigned int, char ] 3162 3174 [ double, double, double ] 3163 [ * int, int * ] $\C{// mix of CFA and ANSI}$3175 [ * int, int * ] §\C{// mix of CFA and ANSI}§ 3164 3176 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 3165 3177 \end{cfa} … … 3168 3180 Examples of declarations using tuple types are: 3169 3181 \begin{cfa} 3170 [ int, int ] x; $\C{// 2 element tuple, each element of type int}$3171 * [ char, char ] y; $\C{// pointer to a 2 element tuple}$3182 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§ 3183 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§ 3172 3184 [ [ int, int ] ] z ([ int, int ]); 3173 3185 \end{cfa} … … 3186 3198 [ int, int ] w1; 3187 3199 [ int, int, int ] w2; 3188 [ void ] f (int, int, int); $\C{// three input parameters of type int}$3189 [ void ] g ([ int, int, int ]); $\C{3 element tuple as input}$3200 [ void ] f (int, int, int); §\C{// three input parameters of type int}§ 3201 [ void ] g ([ int, int, int ]); §\C{3 element tuple as input}§ 3190 3202 f( [ 1, 2, 3 ] ); 3191 3203 f( w1, 3 ); … … 3267 3279 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 3268 3280 int x = 5; 3269 [ x, w ] = [ w, x ]; $\C{// all four tuple coercions}$3281 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§ 3270 3282 \end{cfa} 3271 3283 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; … … 3273 3285 This 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. 3274 3286 The tuple ©[ 2, 3, 4, 5 ]© is then closed to create a tuple value. 3275 Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using \Index{multiple assignment} \see{\VRef{s:TupleAssignment}}.3287 Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using multiple assignment (see Section 14). 3276 3288 \begin{rationale} 3277 3289 A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple. … … 3284 3296 Mass assignment has the following form: 3285 3297 \begin{cfa} 3286 [ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = $\emph{expr}$;3298 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§; 3287 3299 \end{cfa} 3288 3300 \index{lvalue} … … 3324 3336 Multiple assignment has the following form: 3325 3337 \begin{cfa} 3326 [ $\emph{lvalue}$, ... , $\emph{lvalue}$ ] = [ $\emph{expr}$, ... , $\emph{expr}$];3338 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ]; 3327 3339 \end{cfa} 3328 3340 \index{lvalue} … … 3355 3367 both these examples produce indeterminate results: 3356 3368 \begin{cfa} 3357 f( x++, x++ ); $\C{// C routine call with side effects in arguments}$3358 [ v1, v2 ] = [ x++, x++ ]; $\C{// side effects in right-hand side of multiple assignment}$3369 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§ 3370 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§ 3359 3371 \end{cfa} 3360 3372 … … 3365 3377 Cascade assignment has the following form: 3366 3378 \begin{cfa} 3367 $\emph{tuple}$ = $\emph{tuple}$ = ... = $\emph{tuple}$;3379 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§; 3368 3380 \end{cfa} 3369 3381 and it has the same parallel semantics as for mass and multiple assignment. … … 3412 3424 \begin{cfa} 3413 3425 int x = 1, y = 2, z = 3; 3414 sout | x @|@ y @|@z;3426 sout | x ®|® y ®|® z; 3415 3427 \end{cfa} 3416 3428 & 3417 3429 \begin{cfa} 3418 3430 3419 cout << x @<< " "@ << y @<< " "@<< z << endl;3431 cout << x ®<< " "® << y ®<< " "® << z << endl; 3420 3432 \end{cfa} 3421 3433 & … … 3426 3438 \\ 3427 3439 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3428 1 @ @2@ @33440 1® ®2® ®3 3429 3441 \end{cfa} 3430 3442 & 3431 3443 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3432 1 @ @2@ @33444 1® ®2® ®3 3433 3445 \end{cfa} 3434 3446 & 3435 3447 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3436 1 @ @2@ @33448 1® ®2® ®3 3437 3449 \end{cfa} 3438 3450 \end{tabular} … … 3442 3454 \begin{cfa} 3443 3455 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 3444 sout | t1 | t2; $\C{// print tuples}$3456 sout | t1 | t2; §\C{// print tuples}§ 3445 3457 \end{cfa} 3446 3458 \begin{cfa}[showspaces=true,aboveskip=0pt] 3447 1 @, @2@, @3 4@, @5@, @63459 1®, ®2®, ®3 4®, ®5®, ®6 3448 3460 \end{cfa} 3449 3461 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority \emph{overloadable} operator, other than assignment. … … 3454 3466 & 3455 3467 \begin{cfa} 3456 sout | x * 3 | y + 1 | z << 2 | x == y | @(@x | y@)@ | @(@x || y@)@ | @(@x > z ? 1 : 2@)@;3468 sout | x * 3 | y + 1 | z << 2 | x == y | ®(®x | y®)® | ®(®x || y®)® | ®(®x > z ? 1 : 2®)®; 3457 3469 \end{cfa} 3458 3470 \\ … … 3460 3472 & 3461 3473 \begin{cfa} 3462 cout << x * 3 << y + 1 << @(@z << 2@)@ << @(@x == y@)@ << @(@x | y@)@ << @(@x || y@)@ << @(@x > z ? 1 : 2@)@<< endl;3474 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)® << ®(®x | y®)® << ®(®x || y®)® << ®(®x > z ? 1 : 2®)® << endl; 3463 3475 \end{cfa} 3464 3476 \\ … … 3495 3507 \\ 3496 3508 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3497 @1@ @2.5@ @A@ 3509 ®1® ®2.5® ®A® 3498 3510 3499 3511 … … 3501 3513 & 3502 3514 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3503 @1@ @2.5@ @A@ 3515 ®1® ®2.5® ®A® 3504 3516 3505 3517 … … 3507 3519 & 3508 3520 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3509 @1@ 3510 @2.5@ 3511 @A@ 3521 ®1® 3522 ®2.5® 3523 ®A® 3512 3524 \end{cfa} 3513 3525 \end{tabular} … … 3545 3557 3546 3558 \item 3547 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \LstStringStyle{,.;!?)]\}\%\textcent\guillemotright}, where \LstStringStyle{\guillemotright} a closing citation mark. 3548 \begin{cfa} 3559 {\lstset{language=CFA,deletedelim=**[is][]{¢}{¢}} 3560 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%¢»@, where \lstinline[basicstyle=\tt]@»@ is a closing citation mark. 3561 \begin{cfa}[belowskip=0pt] 3549 3562 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 3550 | 7 | "$\LstStringStyle{\textcent}$ x" | 8 | "$\LstStringStyle{\guillemotright}$x" | 9 | ") x" | 10 | "] x" | 11 | "} x";3551 \end{cfa} 3552 \begin{cfa}[ showspaces=true]3553 1 @,@ x 2@.@ x 3@;@ x 4@!@ x 5@?@ x 6@%@ x 7$\R{\LstStringStyle{\textcent}}$ x 8$\R{\LstStringStyle{\guillemotright}}$ x 9@)@ x 10@]@ x 11@}@x3554 \end{cfa} 3555 3556 \item 3557 A separator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \ LstStringStyle{([\{=\$\textsterling\textyen\textexclamdown\textquestiondown\guillemotleft}, where \LstStringStyle{\textexclamdown\textquestiondown} are inverted opening exclamation and question marks, and \LstStringStyle{\guillemotleft}is an opening citation mark.3563 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x"; 3564 \end{cfa} 3565 \begin{cfa}[basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 3566 1®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7§\color{red}\textcent§ x 8®»® x 9®)® x 10®]® x 11®}® x 3567 \end{cfa}}% 3568 3569 \item 3570 A separator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$£¥¡¿«@, where \lstinline[basicstyle=\tt]@¡¿@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@«@ is an opening citation mark. 3558 3571 %$ 3559 \begin{cfa} 3560 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x $\LstStringStyle{\textsterling}$" | 6 | "x $\LstStringStyle{\textyen}$"3561 | 7 | "x $\LstStringStyle{\textexclamdown}$" | 8 | "x $\LstStringStyle{\textquestiondown}$" | 9 | "x $\LstStringStyle{\guillemotleft}$" | 10;3572 \begin{cfa}[mathescape=off] 3573 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" 3574 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10; 3562 3575 \end{cfa} 3563 3576 %$ 3564 \begin{cfa}[ showspaces=true]3565 x @(@1 x @[@2 x @{@3 x @=@4 x $\LstStringStyle{\textdollar}$5 x $\R{\LstStringStyle{\textsterling}}$6 x $\R{\LstStringStyle{\textyen}}$7 x $\R{\LstStringStyle{\textexclamdown}}$8 x $\R{\LstStringStyle{\textquestiondown}}$9 x $\R{\LstStringStyle{\guillemotleft}}$103577 \begin{cfa}[mathescape=off,basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 3578 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x ®$®5 x ®£®6 x ®¥®7 x ®¡®8 x ®¿®9 x ®«®10 3566 3579 \end{cfa} 3567 3580 %$ 3568 3581 3569 3582 \item 3570 A seperator does not appear before/after a C string starting/ending with the \Index*{ASCII} quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true] {`'": \t\v\f\r\n}3571 \begin{cfa} 3583 A seperator does not appear before/after a C string starting/ending with the \Index*{ASCII} quote or whitespace characters: \lstinline[basicstyle=\tt,showspaces=true]@`'": \t\v\f\r\n@ 3584 \begin{cfa}[belowskip=0pt] 3572 3585 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx"; 3573 3586 \end{cfa} 3574 \begin{cfa}[ showspaces=true,showtabs=true]3575 x @`@1@`@x$\R{\texttt{'}}$2$\R{\texttt{'}}$x$\R{\texttt{"}}$3$\R{\texttt{"}}$x@:@4@:@x@ @5@ @x@ @6@ @x3587 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] 3588 x®`®1®`®x§\color{red}\texttt{'}§2§\color{red}\texttt{'}§x§\color{red}\texttt{"}§3§\color{red}\texttt{"}§x®:®4®:®x® ®5® ®x® ®6® ®x 3576 3589 \end{cfa} 3577 3590 3578 3591 \item 3579 3592 If a space is desired before or after one of the special string start/end characters, simply insert a space. 3580 \begin{cfa} 3581 sout | "x ( $\R{\texttt{\textvisiblespace}}$" | 1 | "$\R{\texttt{\textvisiblespace}}$) x" | 2 | "$\R{\texttt{\textvisiblespace}}$, x" | 3 | "$\R{\texttt{\textvisiblespace}}$:x:$\R{\texttt{\textvisiblespace}}$" | 4;3582 \end{cfa} 3583 \begin{cfa}[ showspaces=true,showtabs=true]3584 x ( @ @1@ @) x 2@ @, x 3@ @:x:@ @43593 \begin{cfa}[belowskip=0pt] 3594 sout | "x (§\color{red}\texttt{\textvisiblespace}§" | 1 | "§\color{red}\texttt{\textvisiblespace}§) x" | 2 | "§\color{red}\texttt{\textvisiblespace}§, x" | 3 | "§\color{red}\texttt{\textvisiblespace}§:x:§\color{red}\texttt{\textvisiblespace}§" | 4; 3595 \end{cfa} 3596 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] 3597 x (® ®1® ®) x 2® ®, x 3® ®:x:® ®4 3585 3598 \end{cfa} 3586 3599 \end{enumerate} … … 3595 3608 \Indexc{sepSet}\index{manipulator!sepSet@©sepSet©} and \Indexc{sep}\index{manipulator!sep@©sep©}/\Indexc{sepGet}\index{manipulator!sepGet@©sepGet©} set and get the separator string. 3596 3609 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3597 \begin{cfa}[ escapechar=off,belowskip=0pt]3598 sepSet( sout, ", $" ); $\C{// set separator from " " to ", \$"}$3599 sout | 1 | 2 | 3 | " \"" | @sep@| "\"";3610 \begin{cfa}[mathescape=off,belowskip=0pt] 3611 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§ 3612 sout | 1 | 2 | 3 | " \"" | ®sep® | "\""; 3600 3613 \end{cfa} 3601 3614 %$ 3602 3615 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 3603 1 @, $@2@, $@3 @", $"@3616 1®, $®2®, $®3 ®", $"® 3604 3617 \end{cfa} 3605 3618 %$ 3606 3619 \begin{cfa}[belowskip=0pt] 3607 sepSet( sout, " " ); $\C{// reset separator to " "}$3608 sout | 1 | 2 | 3 | " \"" | @sepGet( sout )@| "\"";3620 sepSet( sout, " " ); §\C{// reset separator to " "}§ 3621 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\""; 3609 3622 \end{cfa} 3610 3623 \begin{cfa}[showspaces=true,aboveskip=0pt] 3611 1 @ @2@ @3 @" "@3624 1® ®2® ®3 ®" "® 3612 3625 \end{cfa} 3613 3626 ©sepGet© can be used to store a separator and then restore it: 3614 3627 \begin{cfa}[belowskip=0pt] 3615 char store[ @sepSize@]; $\C{// sepSize is the maximum separator size}$3616 strcpy( store, sepGet( sout ) ); $\C{// copy current separator}$3617 sepSet( sout, "_" ); $\C{// change separator to underscore}$3628 char store[®sepSize®]; §\C{// sepSize is the maximum separator size}§ 3629 strcpy( store, sepGet( sout ) ); §\C{// copy current separator}§ 3630 sepSet( sout, "_" ); §\C{// change separator to underscore}§ 3618 3631 sout | 1 | 2 | 3; 3619 3632 \end{cfa} 3620 3633 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3621 1 @_@2@_@33634 1®_®2®_®3 3622 3635 \end{cfa} 3623 3636 \begin{cfa}[belowskip=0pt] 3624 sepSet( sout, store ); $\C{// change separator back to original}$3637 sepSet( sout, store ); §\C{// change separator back to original}§ 3625 3638 sout | 1 | 2 | 3; 3626 3639 \end{cfa} 3627 3640 \begin{cfa}[showspaces=true,aboveskip=0pt] 3628 1 @ @2@ @33641 1® ®2® ®3 3629 3642 \end{cfa} 3630 3643 … … 3633 3646 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 3634 3647 \begin{cfa}[belowskip=0pt] 3635 sepSetTuple( sout, " " ); $\C{// set tuple separator from ", " to " "}$3636 sout | t1 | t2 | " \"" | @sepTuple@| "\"";3648 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§ 3649 sout | t1 | t2 | " \"" | ®sepTuple® | "\""; 3637 3650 \end{cfa} 3638 3651 \begin{cfa}[showspaces=true,aboveskip=0pt] 3639 1 2 3 4 5 6 @" "@3652 1 2 3 4 5 6 ®" "® 3640 3653 \end{cfa} 3641 3654 \begin{cfa}[belowskip=0pt] 3642 sepSetTuple( sout, ", " ); $\C{// reset tuple separator to ", "}$3643 sout | t1 | t2 | " \"" | @sepGetTuple( sout )@| "\"";3655 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§ 3656 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\""; 3644 3657 \end{cfa} 3645 3658 \begin{cfa}[showspaces=true,aboveskip=0pt] 3646 1, 2, 3 4, 5, 6 @", "@3659 1, 2, 3 4, 5, 6 ®", "® 3647 3660 \end{cfa} 3648 3661 As for ©sepGet©, ©sepGetTuple© can be use to store a tuple separator and then restore it. … … 3651 3664 \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} toggle printing the separator. 3652 3665 \begin{cfa}[belowskip=0pt] 3653 sout | sepDisable | 1 | 2 | 3; $\C{// turn off implicit separator}$3666 sout | sepDisable | 1 | 2 | 3; §\C{// turn off implicit separator}§ 3654 3667 \end{cfa} 3655 3668 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3657 3670 \end{cfa} 3658 3671 \begin{cfa}[belowskip=0pt] 3659 sout | sepEnable | 1 | 2 | 3; $\C{// turn on implicit separator}$3672 sout | sepEnable | 1 | 2 | 3; §\C{// turn on implicit separator}§ 3660 3673 \end{cfa} 3661 3674 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3666 3679 \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} toggle printing the separator with respect to the next printed item, and then return to the global seperator setting. 3667 3680 \begin{cfa}[belowskip=0pt] 3668 sout | 1 | sepOff | 2 | 3; $\C{// turn off implicit separator for the next item}$3681 sout | 1 | sepOff | 2 | 3; §\C{// turn off implicit separator for the next item}§ 3669 3682 \end{cfa} 3670 3683 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3672 3685 \end{cfa} 3673 3686 \begin{cfa}[belowskip=0pt] 3674 sout | sepDisable | 1 | sepOn | 2 | 3; $\C{// turn on implicit separator for the next item}$3687 sout | sepDisable | 1 | sepOn | 2 | 3; §\C{// turn on implicit separator for the next item}§ 3675 3688 \end{cfa} 3676 3689 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3679 3692 The tuple separator also responses to being turned on and off. 3680 3693 \begin{cfa}[belowskip=0pt] 3681 sout | t1 | sepOff | t2; $\C{// turn off implicit separator for the next item}$3694 sout | t1 | sepOff | t2; §\C{// turn off implicit separator for the next item}§ 3682 3695 \end{cfa} 3683 3696 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3687 3700 use ©sep© to accomplish this functionality. 3688 3701 \begin{cfa}[belowskip=0pt] 3689 sout | sepOn | 1 | 2 | 3 | sepOn; $\C{// sepOn does nothing at start/end of line}$3702 sout | sepOn | 1 | 2 | 3 | sepOn; §\C{// sepOn does nothing at start/end of line}§ 3690 3703 \end{cfa} 3691 3704 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] … … 3693 3706 \end{cfa} 3694 3707 \begin{cfa}[belowskip=0pt] 3695 sout | sep | 1 | 2 | 3 | sep ; $\C{// use sep to print separator at start/end of line}$3708 sout | sep | 1 | 2 | 3 | sep ; §\C{// use sep to print separator at start/end of line}§ 3696 3709 \end{cfa} 3697 3710 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3698 @ @1 2 3@ @ 3711 ® ®1 2 3® ® 3699 3712 \end{cfa} 3700 3713 \end{enumerate} … … 3708 3721 \begin{enumerate}[parsep=0pt] 3709 3722 \item 3710 \Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, \ieignore the remaining characters in the line.3723 \Indexc{nl}\index{manipulator!nl@©nl©} scans characters until the next newline character, i.e., ignore the remaining characters in the line. 3711 3724 \item 3712 3725 \Indexc{nlOn}\index{manipulator!nlOn@©nlOn©} reads the newline character, when reading single characters. … … 3716 3729 For example, in: 3717 3730 \begin{cfa} 3718 sin | i | @nl@| j;3719 1 @2@3731 sin | i | ®nl® | j; 3732 1 ®2® 3720 3733 3 3721 3734 \end{cfa} … … 3727 3740 \Indexc{nl}\index{manipulator!nl@©nl©} inserts a newline. 3728 3741 \begin{cfa} 3729 sout | nl; $\C{// only print newline}$3730 sout | 2; $\C{// implicit newline}$3731 sout | 3 | nl | 4 | nl; $\C{// terminating nl merged with implicit newline}$3732 sout | 5 | nl | nl; $\C{// again terminating nl merged with implicit newline}$3733 sout | 6; $\C{// implicit newline}$3742 sout | nl; §\C{// only print newline}§ 3743 sout | 2; §\C{// implicit newline}§ 3744 sout | 3 | nl | 4 | nl; §\C{// terminating nl merged with implicit newline}§ 3745 sout | 5 | nl | nl; §\C{// again terminating nl merged with implicit newline}§ 3746 sout | 6; §\C{// implicit newline}§ 3734 3747 3735 3748 2 … … 3758 3771 0b0 0b11011 0b11011 0b11011 0b11011 3759 3772 sout | bin( -27HH ) | bin( -27H ) | bin( -27 ) | bin( -27L ); 3760 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b @(58 1s)@1001013773 0b11100101 0b1111111111100101 0b11111111111111111111111111100101 0b®(58 1s)®100101 3761 3774 \end{cfa} 3762 3775 … … 3797 3810 \begin{cfa}[belowskip=0pt] 3798 3811 sout | upcase( bin( 27 ) ) | upcase( hex( 27 ) ) | upcase( 27.5e-10 ) | upcase( hex( 27.5 ) ); 3799 0 @B@11011 0@X@1@B@ 2.75@E@-09 0@X@1.@B@8@P@+43812 0®B®11011 0®X®1®B® 2.75®E®-09 0®X®1.®B®8®P®+4 3800 3813 \end{cfa} 3801 3814 … … 3813 3826 \begin{cfa}[belowskip=0pt] 3814 3827 sout | 0. | nodp( 0. ) | 27.0 | nodp( 27.0 ) | nodp( 27.5 ); 3815 0.0 @0@ 27.0 @27@27.53828 0.0 ®0® 27.0 ®27® 27.5 3816 3829 \end{cfa} 3817 3830 … … 3820 3833 \begin{cfa}[belowskip=0pt] 3821 3834 sout | sign( 27 ) | sign( -27 ) | sign( 27. ) | sign( -27. ) | sign( 27.5 ) | sign( -27.5 ); 3822 @+@27 -27 @+@27.0 -27.0 @+@27.5 -27.53835 ®+®27 -27 ®+®27.0 -27.0 ®+®27.5 -27.5 3823 3836 \end{cfa} 3824 3837 … … 3833 3846 \end{cfa} 3834 3847 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3835 @ @34 @ @34 343836 @ @4.000000 @ @4.000000 4.0000003837 @ @ab @ @ab ab3848 ® ®34 ® ®34 34 3849 ® ®4.000000 ® ®4.000000 4.000000 3850 ® ®ab ® ®ab ab 3838 3851 \end{cfa} 3839 3852 If the value is larger, it is printed without truncation, ignoring the ©minimum©. … … 3844 3857 \end{cfa} 3845 3858 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3846 3456 @7@ 345@67@ 34@567@3847 3456 @.@ 345@6.@ 34@56.@3848 abcd @e@ abc@de@ ab@cde@3859 3456®7® 345®67® 34®567® 3860 3456®.® 345®6.® 34®56.® 3861 abcd®e® abc®de® ab®cde® 3849 3862 \end{cfa} 3850 3863 … … 3855 3868 \end{cfa} 3856 3869 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3857 @0@34 @00@34 @00000000@343870 ®0®34 ®00®34 ®00000000®34 3858 3871 \end{cfa} 3859 3872 If the value is larger, it is printed without truncation, ignoring the ©precision©. … … 3870 3883 \end{cfa} 3871 3884 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3872 @ @ @00000000@343885 ® ® ®00000000®34 3873 3886 \end{cfa} 3874 3887 For floating-point types, ©precision© is the minimum number of digits after the decimal point. … … 3877 3890 \end{cfa} 3878 3891 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3879 27. @500@ 27.@5@ 28. 27.@50000000@3880 \end{cfa} 3881 For the C-string type, ©precision© is the maximum number of printed characters, so the string is trunca ted if it exceeds the maximum.3892 27.®500® 27.®5® 28. 27.®50000000® 3893 \end{cfa} 3894 For the C-string type, ©precision© is the maximum number of printed characters, so the string is truncared if it exceeds the maximum. 3882 3895 \begin{cfa}[belowskip=0pt] 3883 3896 sout | wd( 6,8, "abcd" ) | wd( 6,8, "abcdefghijk" ) | wd( 6,3, "abcd" ); … … 3895 3908 \end{cfa} 3896 3909 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3897 234.567 234.5 @7@ 234.@6@ 23@5@3910 234.567 234.5®7® 234.®6® 23®5® 3898 3911 \end{cfa} 3899 3912 If a value's magnitude is greater than ©significant©, the value is printed in scientific notation with the specified number of significant digits. … … 3902 3915 \end{cfa} 3903 3916 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3904 234567. 2.3457 @e+05@ 2.346@e+05@ 2.35@e+05@3917 234567. 2.3457®e+05® 2.346®e+05® 2.35®e+05® 3905 3918 \end{cfa} 3906 3919 If ©significant© is greater than ©minimum©, it defines the number of printed characters. … … 3918 3931 \end{cfa} 3919 3932 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 3920 27 @ @ 27.000000 27.500000 027 27.500@ @3933 27® ® 27.000000 27.500000 027 27.500® ® 3921 3934 \end{cfa} 3922 3935 … … 3925 3938 \begin{cfa}[belowskip=0pt] 3926 3939 sout | pad0( wd( 4, 27 ) ) | pad0( wd( 4,3, 27 ) ) | pad0( wd( 8,3, 27.5 ) ); 3927 @00@27 @0@27 @00@27.5003940 ®00®27 ®0®27 ®00®27.500 3928 3941 \end{cfa} 3929 3942 \end{enumerate} … … 4021 4034 \end{cfa} 4022 4035 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4023 @abc @ 4024 @abc @ 4025 @xx@ 4036 ®abc ® 4037 ®abc ® 4038 ®xx® 4026 4039 \end{cfa} 4027 4040 … … 4034 4047 \end{cfa} 4035 4048 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4036 @abcd1233.456E+2@ 4049 ®abcd1233.456E+2® 4037 4050 \end{cfa} 4038 4051 Note, input ©wdi© cannot be overloaded with output ©wd© because both have the same parameters but return different types. … … 4047 4060 \end{cfa} 4048 4061 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4049 @ -75.35e-4@254062 ® -75.35e-4® 25 4050 4063 \end{cfa} 4051 4064 … … 4059 4072 \end{cfa} 4060 4073 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4061 @bca@xyz4074 ®bca®xyz 4062 4075 \end{cfa} 4063 4076 … … 4071 4084 \end{cfa} 4072 4085 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 4073 @xyz@bca4086 ®xyz®bca 4074 4087 \end{cfa} 4075 4088 \end{enumerate} … … 4088 4101 4089 4102 A 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. 4090 This means that users can define distinct function overloads for the new type \see{\VRef{s:Overloading} for more information}.4103 This means that users can define distinct function overloads for the new type (see Overloading for more information). 4091 4104 For example: 4092 4105 … … 4194 4207 \CFA supports C initialization of structures, but it also adds constructors for more advanced initialization. 4195 4208 Additionally, \CFA adds destructors that are called when a variable is deallocated (variable goes out of scope or object is deleted). 4196 These functions take a reference to the structure as a parameter \see{\VRef{s:PointerReference} for more information}.4209 These functions take a reference to the structure as a parameter (see References for more information). 4197 4210 4198 4211 \begin{figure} … … 4245 4258 4246 4259 \section{Overloading} 4247 \label{s:Overloading}4248 4260 4249 4261 Overloading refers to the capability of a programmer to define and use multiple objects in a program with the same name. … … 4278 4290 4279 4291 4280 \subsection{ Constant}4292 \subsection{Overloaded Constant} 4281 4293 4282 4294 The constants 0 and 1 have special meaning. … … 4317 4329 4318 4330 4319 \subsection{Variable} 4320 \label{s:VariableOverload} 4331 \subsection{Variable Overloading} 4321 4332 4322 4333 The overload rules of \CFA allow a programmer to define multiple variables with the same name, but different types. … … 4361 4372 4362 4373 4363 \subsection{Operator }4374 \subsection{Operator Overloading} 4364 4375 4365 4376 \CFA also allows operators to be overloaded, to simplify the use of user-defined types. … … 4457 4468 For example, given 4458 4469 \begin{cfa} 4459 auto j = @...@4470 auto j = ®...® 4460 4471 \end{cfa} 4461 4472 and the need to write a routine to compute using ©j© 4462 4473 \begin{cfa} 4463 void rtn( @...@parm );4474 void rtn( ®...® parm ); 4464 4475 rtn( j ); 4465 4476 \end{cfa} … … 4702 4713 4703 4714 coroutine Fibonacci { 4704 int fn; $\C{// used for communication}$4715 int fn; §\C{// used for communication}§ 4705 4716 }; 4706 4717 void ?{}( Fibonacci * this ) { … … 4708 4719 } 4709 4720 void main( Fibonacci * this ) { 4710 int fn1, fn2; $\C{// retained between resumes}$4711 this->fn = 0; $\C{// case 0}$4721 int fn1, fn2; §\C{// retained between resumes}§ 4722 this->fn = 0; §\C{// case 0}§ 4712 4723 fn1 = this->fn; 4713 suspend(); $\C{// return to last resume}$4714 4715 this->fn = 1; $\C{// case 1}$4724 suspend(); §\C{// return to last resume}§ 4725 4726 this->fn = 1; §\C{// case 1}§ 4716 4727 fn2 = fn1; 4717 4728 fn1 = this->fn; 4718 suspend(); $\C{// return to last resume}$4719 4720 for ( ;; ) { $\C{// general case}$4729 suspend(); §\C{// return to last resume}§ 4730 4731 for ( ;; ) { §\C{// general case}§ 4721 4732 this->fn = fn1 + fn2; 4722 4733 fn2 = fn1; 4723 4734 fn1 = this->fn; 4724 suspend(); $\C{// return to last resume}$4735 suspend(); §\C{// return to last resume}§ 4725 4736 } // for 4726 4737 } 4727 4738 int next( Fibonacci * this ) { 4728 resume( this ); $\C{// transfer to last suspend}$4739 resume( this ); §\C{// transfer to last suspend}§ 4729 4740 return this->fn; 4730 4741 } … … 4953 4964 When 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. 4954 4965 4955 In 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{\VRef{s:Interoperability} for more information}.4966 In 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). 4956 4967 4957 4968 … … 5619 5630 \end{cfa} 5620 5631 & 5621 \begin{ C++}5632 \begin{lstlisting}[language=C++] 5622 5633 class Line { 5623 5634 float lnth; … … 5646 5657 Line line1; 5647 5658 Line line2( 3.4 ); 5648 \end{ C++}5659 \end{lstlisting} 5649 5660 & 5650 5661 \begin{lstlisting}[language=Golang] … … 6271 6282 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as: 6272 6283 \begin{cfa} 6273 *? $\R{\textvisiblespace}$*? $\C{// dereference operator, dereference operator}$6274 * $\R{\textvisiblespace}$?*? $\C{// dereference, multiplication operator}$6284 *?§\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§ 6285 *§\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§ 6275 6286 \end{cfa} 6276 6287 By default, the first interpretation is selected, which does not yield a meaningful parse. … … 6281 6292 The ambiguity occurs when the deference operator has no parameters: 6282 6293 \begin{cfa} 6283 *?() $\R{\textvisiblespace...}$;6284 *?() $\R{\textvisiblespace...}$(...) ;6294 *?()§\color{red}\textvisiblespace...§ ; 6295 *?()§\color{red}\textvisiblespace...§(...) ; 6285 6296 \end{cfa} 6286 6297 requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate. … … 6290 6301 The remaining cases are with the increment/decrement operators and conditional expression, \eg: 6291 6302 \begin{cfa} 6292 i++? $\R{\textvisiblespace...}$(...);6293 i?++ $\R{\textvisiblespace...}$(...);6303 i++?§\color{red}\textvisiblespace...§(...); 6304 i?++§\color{red}\textvisiblespace...§(...); 6294 6305 \end{cfa} 6295 6306 requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers). 6296 6307 Therefore, it is necessary to disambiguate these cases with a space: 6297 6308 \begin{cfa} 6298 i++ $\R{\textvisiblespace}$? i : 0;6299 i? $\R{\textvisiblespace}$++i : 0;6309 i++§\color{red}\textvisiblespace§? i : 0; 6310 i?§\color{red}\textvisiblespace§++i : 0; 6300 6311 \end{cfa} 6301 6312 … … 6310 6321 \begin{description} 6311 6322 \item[Change:] add new keywords \\ 6312 New keywords are added to \CFA \see{\VRef{s:CFAKeywords}}.6323 New keywords are added to \CFA (see~\VRef{s:CFAKeywords}). 6313 6324 \item[Rationale:] keywords added to implement new semantics of \CFA. 6314 6325 \item[Effect on original feature:] change to semantics of well-defined feature. \\ 6315 6326 Any \Celeven programs using these keywords as identifiers are invalid \CFA programs. 6316 \item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism \see{\VRef{s:BackquoteIdentifiers}}.6327 \item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism (see~\VRef{s:BackquoteIdentifiers}). 6317 6328 \item[How widely used:] clashes among new \CFA keywords and existing identifiers are rare. 6318 6329 \end{description} … … 6324 6335 \eg: 6325 6336 \begin{cfa} 6326 x; $\C{// int x}$6327 *y; $\C{// int *y}$6328 f( p1, p2 ); $\C{// int f( int p1, int p2 );}$6329 g( p1, p2 ) int p1, p2; $\C{// int g( int p1, int p2 );}$6337 x; §\C{// int x}§ 6338 *y; §\C{// int *y}§ 6339 f( p1, p2 ); §\C{// int f( int p1, int p2 );}§ 6340 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§ 6330 6341 \end{cfa} 6331 6342 \CFA continues to support K\&R routine definitions: 6332 6343 \begin{cfa} 6333 f( a, b, c ) $\C{// default int return}$6334 int a, b; char c $\C{// K\&R parameter declarations}$6344 f( a, b, c ) §\C{// default int return}§ 6345 int a, b; char c §\C{// K\&R parameter declarations}§ 6335 6346 { 6336 6347 ... … … 6351 6362 int rtn( int i ); 6352 6363 int rtn( char c ); 6353 rtn( 'x' ); $\C{// programmer expects 2nd rtn to be called}$6364 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§ 6354 6365 \end{cfa} 6355 6366 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first. … … 6373 6384 \item[Change:] make string literals ©const©: 6374 6385 \begin{cfa} 6375 char * p = "abc"; $\C{// valid in C, deprecated in \CFA}$6376 char * q = expr ? "abc" : "de"; $\C{// valid in C, invalid in \CFA}$6386 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§ 6387 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§ 6377 6388 \end{cfa} 6378 6389 The type of a string literal is changed from ©[] char© to ©const [] char©. … … 6381 6392 \begin{cfa} 6382 6393 char * p = "abc"; 6383 p[0] = 'w'; $\C{// segment fault or change constant literal}$6394 p[0] = 'w'; §\C{// segment fault or change constant literal}§ 6384 6395 \end{cfa} 6385 6396 The same problem occurs when passing a string literal to a routine that changes its argument. … … 6393 6404 \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: 6394 6405 \begin{cfa} 6395 int i; $\C{// forward definition}$6396 int *j = @&i@; $\C{// forward reference, valid in C, invalid in \CFA}$6397 int i = 0; $\C{// definition}$6406 int i; §\C{// forward definition}§ 6407 int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§ 6408 int i = 0; §\C{// definition}§ 6398 6409 \end{cfa} 6399 6410 is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. … … 6401 6412 \begin{cfa} 6402 6413 struct X { int i; struct X *next; }; 6403 static struct X a; $\C{// forward definition}$6404 static struct X b = { 0, @&a@ };$\C{// forward reference, valid in C, invalid in \CFA}$6405 static struct X a = { 1, &b }; $\C{// definition}$6414 static struct X a; §\C{// forward definition}§ 6415 static struct X b = { 0, ®&a® };§\C{// forward reference, valid in C, invalid in \CFA}§ 6416 static struct X a = { 1, &b }; §\C{// definition}§ 6406 6417 \end{cfa} 6407 6418 \item[Rationale:] avoids having different initialization rules for builtin types and user-defined types. … … 6415 6426 \item[Change:] have ©struct© introduce a scope for nested types: 6416 6427 \begin{cfa} 6417 enum @Colour@{ R, G, B, Y, C, M };6428 enum ®Colour® { R, G, B, Y, C, M }; 6418 6429 struct Person { 6419 enum @Colour@ { R, G, B }; $\C[7cm]{// nested type}$6420 struct Face { $\C{// nested type}$6421 @Colour@ Eyes, Hair; $\C{// type defined outside (1 level)}$6430 enum ®Colour® { R, G, B }; §\C[7cm]{// nested type}§ 6431 struct Face { §\C{// nested type}§ 6432 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§ 6422 6433 }; 6423 @.Colour@ shirt; $\C{// type defined outside (top level)}$6424 @Colour@ pants; $\C{// type defined same level}$6425 Face looks[10]; $\C{// type defined same level}$6434 ®.Colour® shirt; §\C{// type defined outside (top level)}§ 6435 ®Colour® pants; §\C{// type defined same level}§ 6436 Face looks[10]; §\C{// type defined same level}§ 6426 6437 }; 6427 @Colour@ c = R; $\C{// type/enum defined same level}$ 6428 Person @.Colour@ pc = Person@.@R;$\C{// type/enum defined inside}$6429 Person @.@Face pretty; $\C{// type defined inside}\CRT$6438 ®Colour® c = R; §\C{// type/enum defined same level}§ 6439 Person®.Colour® pc = Person®.®R;§\C{// type/enum defined inside}§ 6440 Person®.®Face pretty; §\C{// type defined inside}\CRT§ 6430 6441 \end{cfa} 6431 6442 In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing. … … 6444 6455 \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: 6445 6456 \begin{cfa} 6446 struct Y; $\C{// struct Y and struct X are at the same scope}$6457 struct Y; §\C{// struct Y and struct X are at the same scope}§ 6447 6458 struct X { 6448 6459 struct Y { /* ... */ } y; … … 6459 6470 \begin{cfa} 6460 6471 void foo() { 6461 int * b = malloc( sizeof(int) ); $\C{// implicitly convert void * to int *}$6462 char * c = b; $\C{// implicitly convert int * to void *, and then void * to char *}$6472 int * b = malloc( sizeof(int) ); §\C{// implicitly convert void * to int *}§ 6473 char * c = b; §\C{// implicitly convert int * to void *, and then void * to char *}§ 6463 6474 } 6464 6475 \end{cfa} 6465 6476 \item[Rationale:] increase type safety 6466 6477 \item[Effect on original feature:] deletion of semantically well-defined feature. 6467 \item[Difficulty of converting:] requires adding a cast \see{\VRef{s:StorageManagement} for better alternatives}:6478 \item[Difficulty of converting:] requires adding a cast (see \VRef{s:StorageManagement} for better alternatives): 6468 6479 \begin{cfa} 6469 6480 int * b = (int *)malloc( sizeof(int) ); … … 6575 6586 \end{cquote} 6576 6587 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©; 6577 hence, names in these include files are not mangled\index{mangling!name} \see{\VRef{s:Interoperability}}.6588 hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}). 6578 6589 All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling. 6579 6590 This approach is different from \Index*[C++]{\CC{}} where the name-mangling issue is handled internally in C header-files through checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"© qualifiers. … … 6638 6649 Type-safe allocation is provided for all C allocation routines and new \CFA allocation routines, \eg in 6639 6650 \begin{cfa} 6640 int * ip = (int *)malloc( sizeof(int) ); $\C{// C}$6641 int * ip = malloc(); $\C{// \CFA type-safe version of C malloc}$6642 int * ip = alloc(); $\C{// \CFA type-safe uniform alloc}$6651 int * ip = (int *)malloc( sizeof(int) ); §\C{// C}§ 6652 int * ip = malloc(); §\C{// \CFA type-safe version of C malloc}§ 6653 int * ip = alloc(); §\C{// \CFA type-safe uniform alloc}§ 6643 6654 \end{cfa} 6644 6655 the latter two allocations determine the allocation size from the type of ©p© (©int©) and cast the pointer to the allocated storage to ©int *©. … … 6647 6658 \begin{cfa} 6648 6659 struct S { int i; } __attribute__(( aligned( 128 ) )); // cache-line alignment 6649 S * sp = malloc(); $\C{// honour type alignment}$6660 S * sp = malloc(); §\C{// honour type alignment}§ 6650 6661 \end{cfa} 6651 6662 the storage allocation is implicitly aligned to 128 rather than the default 16. … … 6662 6673 \CFA memory management extends allocation to support constructors for initialization of allocated storage, \eg in 6663 6674 \begin{cfa} 6664 struct S { int i; }; $\C{// cache-line alignment}$6675 struct S { int i; }; §\C{// cache-line aglinment}§ 6665 6676 void ?{}( S & s, int i ) { s.i = i; } 6666 6677 // assume ?|? operator for printing an S 6667 6678 6668 S & sp = * @new@( 3 ); $\C{// call constructor after allocation}$6679 S & sp = *®new®( 3 ); §\C{// call constructor after allocation}§ 6669 6680 sout | sp.i; 6670 @delete@( &sp );6671 6672 S * spa = @anew@( 10, 5 ); $\C{// allocate array and initialize each array element}$6681 ®delete®( &sp ); 6682 6683 S * spa = ®anew®( 10, 5 ); §\C{// allocate array and initialize each array element}§ 6673 6684 for ( i; 10 ) sout | spa[i] | nonl; 6674 6685 sout | nl; 6675 @adelete@( 10, spa );6686 ®adelete®( 10, spa ); 6676 6687 \end{cfa} 6677 6688 Allocation routines ©new©/©anew© allocate a variable/array and initialize storage using the allocated type's constructor. … … 6682 6693 extern "C" { 6683 6694 // C unsafe allocation 6684 void * malloc( size_t size ); $\indexc{malloc}$6685 void * calloc( size_t dim, size_t size ); $\indexc{calloc}$6686 void * realloc( void * ptr, size_t size ); $\indexc{realloc}$6687 void * memalign( size_t align, size_t size ); $\indexc{memalign}$6688 void * aligned_alloc( size_t align, size_t size ); $\indexc{aligned_alloc}$6689 int posix_memalign( void ** ptr, size_t align, size_t size ); $\indexc{posix_memalign}$6690 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ); $\indexc{cmemalign}$// CFA6695 void * malloc( size_t size );§\indexc{malloc}§ 6696 void * calloc( size_t dim, size_t size );§\indexc{calloc}§ 6697 void * realloc( void * ptr, size_t size );§\indexc{realloc}§ 6698 void * memalign( size_t align, size_t size );§\indexc{memalign}§ 6699 void * aligned_alloc( size_t align, size_t size );§\indexc{aligned_alloc}§ 6700 int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§ 6701 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize );§\indexc{cmemalign}§ // CFA 6691 6702 6692 6703 // C unsafe initialization/copy 6693 void * memset( void * dest, int c, size_t size ); $\indexc{memset}$6694 void * memcpy( void * dest, const void * src, size_t size ); $\indexc{memcpy}$6704 void * memset( void * dest, int c, size_t size );§\indexc{memset}§ 6705 void * memcpy( void * dest, const void * src, size_t size );§\indexc{memcpy}§ 6695 6706 } 6696 6707 … … 6698 6709 6699 6710 forall( dtype T | sized(T) ) { 6700 // $\CFA$safe equivalents, i.e., implicit size specification6711 // §\CFA§ safe equivalents, i.e., implicit size specification 6701 6712 T * malloc( void ); 6702 6713 T * calloc( size_t dim ); … … 6707 6718 int posix_memalign( T ** ptr, size_t align ); 6708 6719 6709 // $\CFA$safe general allocation, fill, resize, alignment, array6710 T * alloc( void ); $\indexc{alloc}$ $\C[3.5in]{// variable, T size}$6711 T * alloc( size_t dim ); $\C{// array[dim], T size elements}$6712 T * alloc( T ptr[], size_t dim ); $\C{// realloc array[dim], T size elements}$6713 6714 T * alloc_set( char fill ); $\indexc{alloc_set}$ $\C{// variable, T size, fill bytes with value}$6715 T * alloc_set( T fill ); $\C{// variable, T size, fill with value}$6716 T * alloc_set( size_t dim, char fill ); $\C{// array[dim], T size elements, fill bytes with value}$6717 T * alloc_set( size_t dim, T fill ); $\C{// array[dim], T size elements, fill elements with value}$6718 T * alloc_set( size_t dim, const T fill[] ); $\C{// array[dim], T size elements, fill elements with array}$6719 T * alloc_set( T ptr[], size_t dim, char fill ); $\C{// realloc array[dim], T size elements, fill bytes with value}$6720 6721 T * alloc_align( size_t align ); $\C{// aligned variable, T size}$6722 T * alloc_align( size_t align, size_t dim ); $\C{// aligned array[dim], T size elements}$6723 T * alloc_align( T ptr[], size_t align ); $\C{// realloc new aligned array}$6724 T * alloc_align( T ptr[], size_t align, size_t dim ); $\C{// realloc new aligned array[dim]}$6725 6726 T * alloc_align_set( size_t align, char fill ); $\C{// aligned variable, T size, fill bytes with value}$6727 T * alloc_align_set( size_t align, T fill ); $\C{// aligned variable, T size, fill with value}$6728 T * alloc_align_set( size_t align, size_t dim, char fill ); $\C{// aligned array[dim], T size elements, fill bytes with value}$6729 T * alloc_align_set( size_t align, size_t dim, T fill ); $\C{// aligned array[dim], T size elements, fill elements with value}$6730 T * alloc_align_set( size_t align, size_t dim, const T fill[] ); $\C{// aligned array[dim], T size elements, fill elements with array}$6731 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); $\C{// realloc new aligned array[dim], fill new bytes with value}$6732 6733 // $\CFA$safe initialization/copy, i.e., implicit size specification6734 T * memset( T * dest, char fill ); $\indexc{memset}$6735 T * memcpy( T * dest, const T * src ); $\indexc{memcpy}$6736 6737 // $\CFA$safe initialization/copy, i.e., implicit size specification, array types6720 // §\CFA§ safe general allocation, fill, resize, alignment, array 6721 T * alloc( void );§\indexc{alloc}§ §\C[3.5in]{// variable, T size}§ 6722 T * alloc( size_t dim ); §\C{// array[dim], T size elements}§ 6723 T * alloc( T ptr[], size_t dim ); §\C{// realloc array[dim], T size elements}§ 6724 6725 T * alloc_set( char fill );§\indexc{alloc_set}§ §\C{// variable, T size, fill bytes with value}§ 6726 T * alloc_set( T fill ); §\C{// variable, T size, fill with value}§ 6727 T * alloc_set( size_t dim, char fill ); §\C{// array[dim], T size elements, fill bytes with value}§ 6728 T * alloc_set( size_t dim, T fill ); §\C{// array[dim], T size elements, fill elements with value}§ 6729 T * alloc_set( size_t dim, const T fill[] ); §\C{// array[dim], T size elements, fill elements with array}§ 6730 T * alloc_set( T ptr[], size_t dim, char fill ); §\C{// realloc array[dim], T size elements, fill bytes with value}§ 6731 6732 T * alloc_align( size_t align ); §\C{// aligned variable, T size}§ 6733 T * alloc_align( size_t align, size_t dim ); §\C{// aligned array[dim], T size elements}§ 6734 T * alloc_align( T ptr[], size_t align ); §\C{// realloc new aligned array}§ 6735 T * alloc_align( T ptr[], size_t align, size_t dim ); §\C{// realloc new aligned array[dim]}§ 6736 6737 T * alloc_align_set( size_t align, char fill ); §\C{// aligned variable, T size, fill bytes with value}§ 6738 T * alloc_align_set( size_t align, T fill ); §\C{// aligned variable, T size, fill with value}§ 6739 T * alloc_align_set( size_t align, size_t dim, char fill ); §\C{// aligned array[dim], T size elements, fill bytes with value}§ 6740 T * alloc_align_set( size_t align, size_t dim, T fill ); §\C{// aligned array[dim], T size elements, fill elements with value}§ 6741 T * alloc_align_set( size_t align, size_t dim, const T fill[] ); §\C{// aligned array[dim], T size elements, fill elements with array}§ 6742 T * alloc_align_set( T ptr[], size_t align, size_t dim, char fill ); §\C{// realloc new aligned array[dim], fill new bytes with value}§ 6743 6744 // §\CFA§ safe initialization/copy, i.e., implicit size specification 6745 T * memset( T * dest, char fill );§\indexc{memset}§ 6746 T * memcpy( T * dest, const T * src );§\indexc{memcpy}§ 6747 6748 // §\CFA§ safe initialization/copy, i.e., implicit size specification, array types 6738 6749 T * amemset( T dest[], char fill, size_t dim ); 6739 6750 T * amemcpy( T dest[], const T src[], size_t dim ); 6740 6751 } 6741 6752 6742 // $\CFA$allocation/deallocation and constructor/destructor, non-array types6743 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p ); $\indexc{new}$6744 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr ); $\indexc{delete}$6753 // §\CFA§ allocation/deallocation and constructor/destructor, non-array types 6754 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * new( Params p );§\indexc{new}§ 6755 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void delete( T * ptr );§\indexc{delete}§ 6745 6756 forall( dtype T, ttype Params | sized(T) | { void ^?{}( T & ); void delete( Params ); } ) 6746 6757 void delete( T * ptr, Params rest ); 6747 6758 6748 // $\CFA$allocation/deallocation and constructor/destructor, array types6749 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p ); $\indexc{anew}$6750 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] ); $\indexc{adelete}$6759 // §\CFA§ allocation/deallocation and constructor/destructor, array types 6760 forall( dtype T | sized(T), ttype Params | { void ?{}( T &, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§ 6761 forall( dtype T | sized(T) | { void ^?{}( T & ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§ 6751 6762 forall( dtype T | sized(T) | { void ^?{}( T & ); }, ttype Params | { void adelete( Params ); } ) 6752 6763 void adelete( size_t dim, T arr[], Params rest ); … … 6758 6769 \leavevmode 6759 6770 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6760 int ato( const char * ptr ); $\indexc{ato}$6771 int ato( const char * ptr );§\indexc{ato}§ 6761 6772 unsigned int ato( const char * ptr ); 6762 6773 long int ato( const char * ptr ); … … 6790 6801 \leavevmode 6791 6802 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6792 forall( otype T | { int ?<?( T, T ); } ) $\C{// location}$6793 T * bsearch( T key, const T * arr, size_t dim ); $\indexc{bsearch}$6794 6795 forall( otype T | { int ?<?( T, T ); } ) $\C{// position}$6803 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§ 6804 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§ 6805 6806 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§ 6796 6807 unsigned int bsearch( T key, const T * arr, size_t dim ); 6797 6808 6798 6809 forall( otype T | { int ?<?( T, T ); } ) 6799 void qsort( const T * arr, size_t dim ); $\indexc{qsort}$6810 void qsort( const T * arr, size_t dim );§\indexc{qsort}§ 6800 6811 6801 6812 forall( otype E | { int ?<?( E, E ); } ) { 6802 E * bsearch( E key, const E * vals, size_t dim ); $\indexc{bsearch}$ $\C{// location}$6803 size_t bsearch( E key, const E * vals, size_t dim ); $\C{// position}$6804 E * bsearchl( E key, const E * vals, size_t dim ); $\indexc{bsearchl}$6813 E * bsearch( E key, const E * vals, size_t dim );§\indexc{bsearch}§ §\C{// location}§ 6814 size_t bsearch( E key, const E * vals, size_t dim );§\C{// position}§ 6815 E * bsearchl( E key, const E * vals, size_t dim );§\indexc{bsearchl}§ 6805 6816 size_t bsearchl( E key, const E * vals, size_t dim ); 6806 E * bsearchu( E key, const E * vals, size_t dim ); $\indexc{bsearchu}$6817 E * bsearchu( E key, const E * vals, size_t dim );§\indexc{bsearchu}§ 6807 6818 size_t bsearchu( E key, const E * vals, size_t dim ); 6808 6819 } … … 6818 6829 6819 6830 forall( otype E | { int ?<?( E, E ); } ) { 6820 void qsort( E * vals, size_t dim ); $\indexc{qsort}$6831 void qsort( E * vals, size_t dim );§\indexc{qsort}§ 6821 6832 } 6822 6833 \end{cfa} … … 6827 6838 \leavevmode 6828 6839 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6829 unsigned char abs( signed char ); $\indexc{abs}$6840 unsigned char abs( signed char );§\indexc{abs}§ 6830 6841 int abs( int ); 6831 6842 unsigned long int abs( long int ); … … 6846 6857 \leavevmode 6847 6858 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6848 void srandom( unsigned int seed ); $\indexc{srandom}$6849 char random( void ); $\indexc{random}$6850 char random( char u ); $\C{// [0,u)}$6851 char random( char l, char u ); $\C{// [l,u)}$6859 void srandom( unsigned int seed );§\indexc{srandom}§ 6860 char random( void );§\indexc{random}§ 6861 char random( char u ); §\C{// [0,u)}§ 6862 char random( char l, char u ); §\C{// [l,u)}§ 6852 6863 int random( void ); 6853 int random( int u ); $\C{// [0,u)}$6854 int random( int l, int u ); $\C{// [l,u)}$6864 int random( int u ); §\C{// [0,u)}§ 6865 int random( int l, int u ); §\C{// [l,u)}§ 6855 6866 unsigned int random( void ); 6856 unsigned int random( unsigned int u ); $\C{// [0,u)}$6857 unsigned int random( unsigned int l, unsigned int u ); $\C{// [l,u)}$6867 unsigned int random( unsigned int u ); §\C{// [0,u)}§ 6868 unsigned int random( unsigned int l, unsigned int u ); §\C{// [l,u)}§ 6858 6869 long int random( void ); 6859 long int random( long int u ); $\C{// [0,u)}$6860 long int random( long int l, long int u ); $\C{// [l,u)}$6870 long int random( long int u ); §\C{// [0,u)}§ 6871 long int random( long int l, long int u ); §\C{// [l,u)}§ 6861 6872 unsigned long int random( void ); 6862 unsigned long int random( unsigned long int u ); $\C{// [0,u)}$6863 unsigned long int random( unsigned long int l, unsigned long int u ); $\C{// [l,u)}$6864 float random( void ); $\C{// [0.0, 1.0)}$6865 double random( void ); $\C{// [0.0, 1.0)}$6866 float _Complex random( void ); $\C{// [0.0, 1.0)+[0.0, 1.0)i}$6867 double _Complex random( void ); $\C{// [0.0, 1.0)+[0.0, 1.0)i}$6868 long double _Complex random( void ); $\C{// [0.0, 1.0)+[0.0, 1.0)i}$6873 unsigned long int random( unsigned long int u ); §\C{// [0,u)}§ 6874 unsigned long int random( unsigned long int l, unsigned long int u ); §\C{// [l,u)}§ 6875 float random( void ); §\C{// [0.0, 1.0)}§ 6876 double random( void ); §\C{// [0.0, 1.0)}§ 6877 float _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6878 double _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6879 long double _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6869 6880 \end{cfa} 6870 6881 … … 6874 6885 \leavevmode 6875 6886 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6876 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 ); $\indexc{min}$6877 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 ); $\indexc{max}$6878 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val ); $\indexc{clamp}$6879 forall( otype T ) void swap( T * t1, T * t2 ); $\indexc{swap}$6887 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );§\indexc{min}§ 6888 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );§\indexc{max}§ 6889 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );§\indexc{clamp}§ 6890 forall( otype T ) void swap( T * t1, T * t2 );§\indexc{swap}§ 6880 6891 \end{cfa} 6881 6892 … … 6891 6902 \leavevmode 6892 6903 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6893 float ?%?( float, float ); $\indexc{fmod}$6904 float ?%?( float, float );§\indexc{fmod}§ 6894 6905 float fmod( float, float ); 6895 6906 double ?%?( double, double ); … … 6898 6909 long double fmod( long double, long double ); 6899 6910 6900 float remainder( float, float ); $\indexc{remainder}$6911 float remainder( float, float );§\indexc{remainder}§ 6901 6912 double remainder( double, double ); 6902 6913 long double remainder( long double, long double ); 6903 6914 6904 float remquo( float, float, int * ); $\indexc{remquo}$6915 float remquo( float, float, int * );§\indexc{remquo}§ 6905 6916 double remquo( double, double, int * ); 6906 6917 long double remquo( long double, long double, int * ); … … 6909 6920 [ int, long double ] remquo( long double, long double ); 6910 6921 6911 float div( float, float, int * ); $\indexc{div}$ $\C{// alternative name for remquo}$6922 float div( float, float, int * );§\indexc{div}§ §\C{// alternative name for remquo}§ 6912 6923 double div( double, double, int * ); 6913 6924 long double div( long double, long double, int * ); … … 6916 6927 [ int, long double ] div( long double, long double ); 6917 6928 6918 float fma( float, float, float ); $\indexc{fma}$6929 float fma( float, float, float );§\indexc{fma}§ 6919 6930 double fma( double, double, double ); 6920 6931 long double fma( long double, long double, long double ); 6921 6932 6922 float fdim( float, float ); $\indexc{fdim}$6933 float fdim( float, float );§\indexc{fdim}§ 6923 6934 double fdim( double, double ); 6924 6935 long double fdim( long double, long double ); 6925 6936 6926 float nan( const char * ); $\indexc{nan}$6937 float nan( const char * );§\indexc{nan}§ 6927 6938 double nan( const char * ); 6928 6939 long double nan( const char * ); … … 6934 6945 \leavevmode 6935 6946 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6936 float exp( float ); $\indexc{exp}$6947 float exp( float );§\indexc{exp}§ 6937 6948 double exp( double ); 6938 6949 long double exp( long double ); … … 6941 6952 long double _Complex exp( long double _Complex ); 6942 6953 6943 float exp2( float ); $\indexc{exp2}$6954 float exp2( float );§\indexc{exp2}§ 6944 6955 double exp2( double ); 6945 6956 long double exp2( long double ); … … 6948 6959 // long double _Complex exp2( long double _Complex ); 6949 6960 6950 float expm1( float ); $\indexc{expm1}$6961 float expm1( float );§\indexc{expm1}§ 6951 6962 double expm1( double ); 6952 6963 long double expm1( long double ); 6953 6964 6954 float pow( float, float ); $\indexc{pow}$6965 float pow( float, float );§\indexc{pow}§ 6955 6966 double pow( double, double ); 6956 6967 long double pow( long double, long double ); … … 6965 6976 \leavevmode 6966 6977 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6967 float log( float ); $\indexc{log}$6978 float log( float );§\indexc{log}§ 6968 6979 double log( double ); 6969 6980 long double log( long double ); … … 6972 6983 long double _Complex log( long double _Complex ); 6973 6984 6974 float log2( float ); $\indexc{log2}$6985 float log2( float );§\indexc{log2}§ 6975 6986 double log2( double ); 6976 6987 long double log2( long double ); … … 6979 6990 // long double _Complex log2( long double _Complex ); 6980 6991 6981 float log10( float ); $\indexc{log10}$6992 float log10( float );§\indexc{log10}§ 6982 6993 double log10( double ); 6983 6994 long double log10( long double ); … … 6986 6997 // long double _Complex log10( long double _Complex ); 6987 6998 6988 float log1p( float ); $\indexc{log1p}$6999 float log1p( float );§\indexc{log1p}§ 6989 7000 double log1p( double ); 6990 7001 long double log1p( long double ); 6991 7002 6992 int ilogb( float ); $\indexc{ilogb}$7003 int ilogb( float );§\indexc{ilogb}§ 6993 7004 int ilogb( double ); 6994 7005 int ilogb( long double ); 6995 7006 6996 float logb( float ); $\indexc{logb}$7007 float logb( float );§\indexc{logb}§ 6997 7008 double logb( double ); 6998 7009 long double logb( long double ); 6999 7010 7000 float sqrt( float ); $\indexc{sqrt}$7011 float sqrt( float );§\indexc{sqrt}§ 7001 7012 double sqrt( double ); 7002 7013 long double sqrt( long double ); … … 7005 7016 long double _Complex sqrt( long double _Complex ); 7006 7017 7007 float cbrt( float ); $\indexc{cbrt}$7018 float cbrt( float );§\indexc{cbrt}§ 7008 7019 double cbrt( double ); 7009 7020 long double cbrt( long double ); 7010 7021 7011 float hypot( float, float ); $\indexc{hypot}$7022 float hypot( float, float );§\indexc{hypot}§ 7012 7023 double hypot( double, double ); 7013 7024 long double hypot( long double, long double ); … … 7019 7030 \leavevmode 7020 7031 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7021 float sin( float ); $\indexc{sin}$7032 float sin( float );§\indexc{sin}§ 7022 7033 double sin( double ); 7023 7034 long double sin( long double ); … … 7026 7037 long double _Complex sin( long double _Complex ); 7027 7038 7028 float cos( float ); $\indexc{cos}$7039 float cos( float );§\indexc{cos}§ 7029 7040 double cos( double ); 7030 7041 long double cos( long double ); … … 7033 7044 long double _Complex cos( long double _Complex ); 7034 7045 7035 float tan( float ); $\indexc{tan}$7046 float tan( float );§\indexc{tan}§ 7036 7047 double tan( double ); 7037 7048 long double tan( long double ); … … 7040 7051 long double _Complex tan( long double _Complex ); 7041 7052 7042 float asin( float ); $\indexc{asin}$7053 float asin( float );§\indexc{asin}§ 7043 7054 double asin( double ); 7044 7055 long double asin( long double ); … … 7047 7058 long double _Complex asin( long double _Complex ); 7048 7059 7049 float acos( float ); $\indexc{acos}$7060 float acos( float );§\indexc{acos}§ 7050 7061 double acos( double ); 7051 7062 long double acos( long double ); … … 7054 7065 long double _Complex acos( long double _Complex ); 7055 7066 7056 float atan( float ); $\indexc{atan}$7067 float atan( float );§\indexc{atan}§ 7057 7068 double atan( double ); 7058 7069 long double atan( long double ); … … 7061 7072 long double _Complex atan( long double _Complex ); 7062 7073 7063 float atan2( float, float ); $\indexc{atan2}$7074 float atan2( float, float );§\indexc{atan2}§ 7064 7075 double atan2( double, double ); 7065 7076 long double atan2( long double, long double ); 7066 7077 7067 float atan( float, float ); $\C{// alternative name for atan2}$7068 double atan( double, double ); $\indexc{atan}$7078 float atan( float, float ); §\C{// alternative name for atan2}§ 7079 double atan( double, double );§\indexc{atan}§ 7069 7080 long double atan( long double, long double ); 7070 7081 \end{cfa} … … 7075 7086 \leavevmode 7076 7087 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7077 float sinh( float ); $\indexc{sinh}$7088 float sinh( float );§\indexc{sinh}§ 7078 7089 double sinh( double ); 7079 7090 long double sinh( long double ); … … 7082 7093 long double _Complex sinh( long double _Complex ); 7083 7094 7084 float cosh( float ); $\indexc{cosh}$7095 float cosh( float );§\indexc{cosh}§ 7085 7096 double cosh( double ); 7086 7097 long double cosh( long double ); … … 7089 7100 long double _Complex cosh( long double _Complex ); 7090 7101 7091 float tanh( float ); $\indexc{tanh}$7102 float tanh( float );§\indexc{tanh}§ 7092 7103 double tanh( double ); 7093 7104 long double tanh( long double ); … … 7096 7107 long double _Complex tanh( long double _Complex ); 7097 7108 7098 float asinh( float ); $\indexc{asinh}$7109 float asinh( float );§\indexc{asinh}§ 7099 7110 double asinh( double ); 7100 7111 long double asinh( long double ); … … 7103 7114 long double _Complex asinh( long double _Complex ); 7104 7115 7105 float acosh( float ); $\indexc{acosh}$7116 float acosh( float );§\indexc{acosh}§ 7106 7117 double acosh( double ); 7107 7118 long double acosh( long double ); … … 7110 7121 long double _Complex acosh( long double _Complex ); 7111 7122 7112 float atanh( float ); $\indexc{atanh}$7123 float atanh( float );§\indexc{atanh}§ 7113 7124 double atanh( double ); 7114 7125 long double atanh( long double ); … … 7123 7134 \leavevmode 7124 7135 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7125 float erf( float ); $\indexc{erf}$7136 float erf( float );§\indexc{erf}§ 7126 7137 double erf( double ); 7127 7138 long double erf( long double ); … … 7130 7141 long double _Complex erf( long double _Complex ); 7131 7142 7132 float erfc( float ); $\indexc{erfc}$7143 float erfc( float );§\indexc{erfc}§ 7133 7144 double erfc( double ); 7134 7145 long double erfc( long double ); … … 7137 7148 long double _Complex erfc( long double _Complex ); 7138 7149 7139 float lgamma( float ); $\indexc{lgamma}$7150 float lgamma( float );§\indexc{lgamma}§ 7140 7151 double lgamma( double ); 7141 7152 long double lgamma( long double ); … … 7144 7155 long double lgamma( long double, int * ); 7145 7156 7146 float tgamma( float ); $\indexc{tgamma}$7157 float tgamma( float );§\indexc{tgamma}§ 7147 7158 double tgamma( double ); 7148 7159 long double tgamma( long double ); … … 7154 7165 \leavevmode 7155 7166 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7156 float floor( float ); $\indexc{floor}$7167 float floor( float );§\indexc{floor}§ 7157 7168 double floor( double ); 7158 7169 long double floor( long double ); 7159 7170 7160 float ceil( float ); $\indexc{ceil}$7171 float ceil( float );§\indexc{ceil}§ 7161 7172 double ceil( double ); 7162 7173 long double ceil( long double ); 7163 7174 7164 float trunc( float ); $\indexc{trunc}$7175 float trunc( float );§\indexc{trunc}§ 7165 7176 double trunc( double ); 7166 7177 long double trunc( long double ); 7167 7178 7168 float rint( float ); $\indexc{rint}$7179 float rint( float );§\indexc{rint}§ 7169 7180 long double rint( long double ); 7170 7181 long int rint( float ); … … 7175 7186 long long int rint( long double ); 7176 7187 7177 long int lrint( float ); $\indexc{lrint}$7188 long int lrint( float );§\indexc{lrint}§ 7178 7189 long int lrint( double ); 7179 7190 long int lrint( long double ); … … 7182 7193 long long int llrint( long double ); 7183 7194 7184 float nearbyint( float ); $\indexc{nearbyint}$7195 float nearbyint( float );§\indexc{nearbyint}§ 7185 7196 double nearbyint( double ); 7186 7197 long double nearbyint( long double ); 7187 7198 7188 float round( float ); $\indexc{round}$7199 float round( float );§\indexc{round}§ 7189 7200 long double round( long double ); 7190 7201 long int round( float ); … … 7195 7206 long long int round( long double ); 7196 7207 7197 long int lround( float ); $\indexc{lround}$7208 long int lround( float );§\indexc{lround}§ 7198 7209 long int lround( double ); 7199 7210 long int lround( long double ); … … 7208 7219 \leavevmode 7209 7220 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7210 float copysign( float, float ); $\indexc{copysign}$7221 float copysign( float, float );§\indexc{copysign}§ 7211 7222 double copysign( double, double ); 7212 7223 long double copysign( long double, long double ); 7213 7224 7214 float frexp( float, int * ); $\indexc{frexp}$7225 float frexp( float, int * );§\indexc{frexp}§ 7215 7226 double frexp( double, int * ); 7216 7227 long double frexp( long double, int * ); 7217 7228 7218 float ldexp( float, int ); $\indexc{ldexp}$7229 float ldexp( float, int );§\indexc{ldexp}§ 7219 7230 double ldexp( double, int ); 7220 7231 long double ldexp( long double, int ); 7221 7232 7222 [ float, float ] modf( float ); $\indexc{modf}$7233 [ float, float ] modf( float );§\indexc{modf}§ 7223 7234 float modf( float, float * ); 7224 7235 [ double, double ] modf( double ); … … 7227 7238 long double modf( long double, long double * ); 7228 7239 7229 float nextafter( float, float ); $\indexc{nextafter}$7240 float nextafter( float, float );§\indexc{nextafter}§ 7230 7241 double nextafter( double, double ); 7231 7242 long double nextafter( long double, long double ); 7232 7243 7233 float nexttoward( float, long double ); $\indexc{nexttoward}$7244 float nexttoward( float, long double );§\indexc{nexttoward}§ 7234 7245 double nexttoward( double, long double ); 7235 7246 long double nexttoward( long double, long double ); 7236 7247 7237 float scalbn( float, int ); $\indexc{scalbn}$7248 float scalbn( float, int );§\indexc{scalbn}§ 7238 7249 double scalbn( double, int ); 7239 7250 long double scalbn( long double, int ); 7240 7251 7241 float scalbln( float, long int ); $\indexc{scalbln}$7252 float scalbln( float, long int );§\indexc{scalbln}§ 7242 7253 double scalbln( double, long int ); 7243 7254 long double scalbln( long double, long int ); … … 7256 7267 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7257 7268 struct Duration { 7258 int64_t tv; $\C{// nanoseconds}$7269 int64_t tv; §\C{// nanoseconds}§ 7259 7270 }; 7260 7271 … … 7386 7397 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7387 7398 struct Time { 7388 uint64_t tv; $\C{// nanoseconds since UNIX epoch}$7399 uint64_t tv; §\C{// nanoseconds since UNIX epoch}§ 7389 7400 }; 7390 7401 … … 7457 7468 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 7458 7469 struct Clock { 7459 Duration offset; $\C{// for virtual clock: contains offset from real-time}$7460 int clocktype; $\C{// implementation only -1 (virtual), CLOCK\_REALTIME}$7470 Duration offset; §\C{// for virtual clock: contains offset from real-time}§ 7471 int clocktype; §\C{// implementation only -1 (virtual), CLOCK\_REALTIME}§ 7461 7472 }; 7462 7473 … … 7466 7477 void ?{}( Clock & clk, Duration adj ); 7467 7478 7468 Duration getResNsec(); $\C{// with nanoseconds}$7469 Duration getRes(); $\C{// without nanoseconds}$7470 7471 Time getTimeNsec(); $\C{// with nanoseconds}$7472 Time getTime(); $\C{// without nanoseconds}$7479 Duration getResNsec(); §\C{// with nanoseconds}§ 7480 Duration getRes(); §\C{// without nanoseconds}§ 7481 7482 Time getTimeNsec(); §\C{// with nanoseconds}§ 7483 Time getTime(); §\C{// without nanoseconds}§ 7473 7484 Time getTime( Clock & clk ); 7474 7485 Time ?()( Clock & clk ); … … 7486 7497 7487 7498 \begin{cfa} 7488 void ?{}( Int * this ); $\C{// constructor/destructor}$7499 void ?{}( Int * this ); §\C{// constructor/destructor}§ 7489 7500 void ?{}( Int * this, Int init ); 7490 7501 void ?{}( Int * this, zero_t ); … … 7495 7506 void ^?{}( Int * this ); 7496 7507 7497 Int ?=?( Int * lhs, Int rhs ); $\C{// assignment}$7508 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§ 7498 7509 Int ?=?( Int * lhs, long int rhs ); 7499 7510 Int ?=?( Int * lhs, unsigned long int rhs ); … … 7512 7523 unsigned long int narrow( Int val ); 7513 7524 7514 int ?==?( Int oper1, Int oper2 ); $\C{// comparison}$7525 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§ 7515 7526 int ?==?( Int oper1, long int oper2 ); 7516 7527 int ?==?( long int oper2, Int oper1 ); … … 7548 7559 int ?>=?( unsigned long int oper1, Int oper2 ); 7549 7560 7550 Int +?( Int oper ); $\C{// arithmetic}$7561 Int +?( Int oper ); §\C{// arithmetic}§ 7551 7562 Int -?( Int oper ); 7552 7563 Int ~?( Int oper ); … … 7630 7641 Int ?>>=?( Int * lhs, mp_bitcnt_t shift ); 7631 7642 7632 Int abs( Int oper ); $\C{// number functions}$7643 Int abs( Int oper ); §\C{// number functions}§ 7633 7644 Int fact( unsigned long int N ); 7634 7645 Int gcd( Int oper1, Int oper2 ); … … 7642 7653 Int sqrt( Int oper ); 7643 7654 7644 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); $\C{// I/O}$7655 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); §\C{// I/O}§ 7645 7656 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp ); 7646 7657 \end{cfa} … … 7653 7664 \hline 7654 7665 \begin{cfa} 7655 #include <gmp> $\indexc{gmp}$7666 #include <gmp>§\indexc{gmp}§ 7656 7667 int main( void ) { 7657 7668 sout | "Factorial Numbers"; … … 7667 7678 & 7668 7679 \begin{cfa} 7669 #include <gmp.h> $\indexc{gmp.h}$7680 #include <gmp.h>§\indexc{gmp.h}§ 7670 7681 int main( void ) { 7671 @gmp_printf@( "Factorial Numbers\n" );7672 @mpz_t@fact;7673 @mpz_init_set_ui@( fact, 1 );7674 @gmp_printf@( "%d %Zd\n", 0, fact );7682 ®gmp_printf®( "Factorial Numbers\n" ); 7683 ®mpz_t® fact; 7684 ®mpz_init_set_ui®( fact, 1 ); 7685 ®gmp_printf®( "%d %Zd\n", 0, fact ); 7675 7686 for ( unsigned int i = 1; i <= 40; i += 1 ) { 7676 @mpz_mul_ui@( fact, fact, i );7677 @gmp_printf@( "%d %Zd\n", i, fact );7687 ®mpz_mul_ui®( fact, fact, i ); 7688 ®gmp_printf®( "%d %Zd\n", i, fact ); 7678 7689 } 7679 7690 } … … 7740 7751 \begin{cfa}[belowskip=0pt] 7741 7752 // implementation 7742 struct Rational { $\indexc{Rational}$7743 long int numerator, denominator; $\C{// invariant: denominator > 0}$7753 struct Rational {§\indexc{Rational}§ 7754 long int numerator, denominator; §\C{// invariant: denominator > 0}§ 7744 7755 }; // Rational 7745 7756 7746 Rational rational(); $\C{// constructors}$7757 Rational rational(); §\C{// constructors}§ 7747 7758 Rational rational( long int n ); 7748 7759 Rational rational( long int n, long int d ); … … 7750 7761 void ?{}( Rational * r, one_t ); 7751 7762 7752 long int numerator( Rational r ); $\C{// numerator/denominator getter/setter}$7763 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§ 7753 7764 long int numerator( Rational r, long int n ); 7754 7765 long int denominator( Rational r ); 7755 7766 long int denominator( Rational r, long int d ); 7756 7767 7757 int ?==?( Rational l, Rational r ); $\C{// comparison}$7768 int ?==?( Rational l, Rational r ); §\C{// comparison}§ 7758 7769 int ?!=?( Rational l, Rational r ); 7759 7770 int ?<?( Rational l, Rational r ); … … 7762 7773 int ?>=?( Rational l, Rational r ); 7763 7774 7764 Rational -?( Rational r ); $\C{// arithmetic}$7775 Rational -?( Rational r ); §\C{// arithmetic}§ 7765 7776 Rational ?+?( Rational l, Rational r ); 7766 7777 Rational ?-?( Rational l, Rational r ); … … 7768 7779 Rational ?/?( Rational l, Rational r ); 7769 7780 7770 double widen( Rational r ); $\C{// conversion}$7781 double widen( Rational r ); §\C{// conversion}§ 7771 7782 Rational narrow( double f, long int md ); 7772 7783
Note:
See TracChangeset
for help on using the changeset viewer.