Changeset b826e6b for doc/user/user.tex
- Timestamp:
- Jul 19, 2017, 11:49:33 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 9cc0472
- Parents:
- fea3faa (diff), a57cb58 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
rfea3faa rb826e6b 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Fri Jun 16 12:00:01201714 %% Update Count : 2 43313 %% Last Modified On : Mon Jul 17 13:06:40 2017 14 %% Update Count : 2825 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 25 25 \usepackage{textcomp} 26 26 \usepackage[latin1]{inputenc} 27 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore28 % removes it as a variable-name character so keyworks in variables are highlighted29 \DeclareTextCommandDefault{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.1ex}}}30 31 27 32 28 \usepackage{fullpage,times,comment} … … 48 44 \renewcommand{\UrlFont}{\small\sf} 49 45 46 % Default underscore is too low and wide. Cannot use lstlisting "literate" as replacing underscore 47 % removes it as a variable-name character so keywords in variables are highlighted. MUST APPEAR 48 % AFTER HYPERREF. 49 \renewcommand{\_}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}} 50 \renewcommand{\textunderscore}{\leavevmode\makebox[1.2ex][c]{\rule{1ex}{0.075ex}}} 51 50 52 \setlength{\topmargin}{-0.45in} % move running title into header 51 53 \setlength{\headsep}{0.25in} … … 54 56 55 57 \CFAStyle % use default CFA format-style 58 59 \lstnewenvironment{C++}[1][] 60 {\lstset{language=C++,moredelim=**[is][\protect\color{red}]{®}{®}#1}} 61 {} 56 62 57 63 % inline code ©...© (copyright symbol) emacs: C-q M-) … … 110 116 \renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}} 111 117 \pagenumbering{roman} 112 %\linenumbers % comment out to turn off line numbering118 \linenumbers % comment out to turn off line numbering 113 119 114 120 \maketitle … … 135 141 136 142 \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. 137 The syntax of the \CFA language builds from C,and should look immediately familiar to C/\Index*[C++]{\CC{}} programmers.143 The syntax of \CFA builds from C and should look immediately familiar to C/\Index*[C++]{\CC{}} programmers. 138 144 % Any language feature that is not described here can be assumed to be using the standard \Celeven syntax. 139 \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 Cperformance.140 Like C, \CFA is a statically typed, procedural 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.145 \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. 146 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. 141 147 The primary new features include parametric-polymorphic routines and types, exceptions, concurrency, and modules. 142 148 143 One of the main design philosophies of \CFA is to ``describe not prescribe'', which means \CFA tries to provide a pathway from low-level C programming to high-level \CFA programming, but it does not force programmers to ``do the right thing''. 144 Programmers can cautiously add \CFA extensions to their C programs in any order and at any time to incrementally move towards safer, higher-level programming features. 145 A programmer is always free to reach back to C from \CFA for any reason, and in many cases, new \CFA features have a fallback to a C mechanism. 146 There is no notion or requirement for rewriting a legacy C program in \CFA; 147 instead, a programmer evolves an existing C program into \CFA by incrementally incorporating \CFA features. 148 New programs can be written in \CFA using a combination of C and \CFA features. 149 \Index*[C++]{\CC{}} had a similar goal 30 years ago, but currently has the disadvantages of multiple legacy design-choices that cannot be updated and active divergence of the language model from C, requiring significant effort and training to incrementally add \CC to a C-based project. 149 One of the main design philosophies of \CFA is to ``\Index{describe not prescribe}'', which means \CFA tries to provide a pathway from low-level C programming to high-level \CFA programming, but it does not force programmers to ``do the right thing''. 150 Programmers can cautiously add \CFA extensions to their C programs in any order and at any time to incrementally move towards safer, higher-level programming. 151 A programmer is always free to reach back to C from \CFA, for any reason, and in many cases, new \CFA features can be locally switched back to there C counterpart. 152 There is no notion or requirement for \emph{rewriting} a legacy C program in \CFA; 153 instead, a programmer evolves a legacy program into \CFA by incrementally incorporating \CFA features. 154 As well, new programs can be written in \CFA using a combination of C and \CFA features. 155 156 \Index*[C++]{\CC{}} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C. 157 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that cannot be updated, and active divergence of the language model from C, all of which requires significant effort and training to incrementally add \CC to a C-based project. 150 158 In contrast, \CFA has 30 years of hindsight and a clean starting point. 151 159 … … 154 162 \begin{quote2} 155 163 \begin{tabular}{@{}l@{\hspace{1.5em}}l@{\hspace{1.5em}}l@{}} 156 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CC}} \\ 157 \begin{cfa} 158 #include <fstream>§\indexc{fstream}§ 159 160 int main( void ) { 161 int x = 0, y = 1, z = 2; 162 ®sout | x | y | z | endl;® 163 } 164 \end{cfa} 165 & 166 \begin{lstlisting} 164 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ 165 \begin{cfa} 167 166 #include <stdio.h>§\indexc{stdio.h}§ 168 167 … … 171 170 ®printf( "%d %d %d\n", x, y, z );® 172 171 } 173 \end{ lstlisting}172 \end{cfa} 174 173 & 175 \begin{lstlisting} 174 \begin{cfa} 175 #include <fstream>§\indexc{fstream}§ 176 177 int main( void ) { 178 int x = 0, y = 1, z = 2; 179 ®sout | x | y | z | endl;®§\indexc{sout}§ 180 } 181 \end{cfa} 182 & 183 \begin{cfa} 176 184 #include <iostream>§\indexc{iostream}§ 177 185 using namespace std; … … 180 188 ®cout<<x<<" "<<y<<" "<<z<<endl;® 181 189 } 182 \end{ lstlisting}190 \end{cfa} 183 191 \end{tabular} 184 192 \end{quote2} 185 193 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}). 186 194 195 \subsection{Background} 196 187 197 This document is a programmer reference-manual for the \CFA programming language. 188 198 The manual covers the core features of the language and runtime-system, with simple examples illustrating syntax and semantics of each feature. 189 199 The manual does not teach programming, i.e., how to combine the new constructs to build complex programs. 190 A reader should already have an intermediate knowledge of control flow, data structures, and concurrency issues to understand the ideas presented as well as some experience programming in C/\CC.191 Implementers mayrefer to the \CFA Programming Language Specification for details about the language syntax and semantics.200 A reader should already have an intermediate knowledge of control flow, data structures, and concurrency issues to understand the ideas presented, as well as some experience programming in C/\CC. 201 Implementers should refer to the \CFA Programming Language Specification for details about the language syntax and semantics. 192 202 Changes to the syntax and additional features are expected to be included in later revisions. 193 203 … … 198 208 This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more. 199 209 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. 200 For system programming, where direct access to hardware and dealing with real-time issues is a requirement, C is usually thelanguage of choice.210 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. 201 211 The TIOBE index~\cite{TIOBE} for March 2016 showed the following programming-language popularity: \Index*{Java} 20.5\%, C 14.5\%, \Index*[C++]{\CC{}} 6.7\%, \Csharp 4.3\%, \Index*{Python} 4.3\%, where the next 50 languages are less than 3\% each with a long tail. 202 212 As well, for 30 years, C has been the number 1 and 2 most popular programming language: … … 214 224 \end{center} 215 225 Hence, C is still an extremely important programming language, with double the usage of \Index*[C++]{\CC{}}; in many cases, \CC is often used solely as a better C. 216 Love it or hate it, C has been an important and influential part of computer science for 40 years and sitappeal is not diminishing.217 Unfortunately, C has too many problems and omissions to make it anacceptable programming language for modern needs.218 219 As stated, the goal of the \CFA project is to engineer modern language 226 Love it or hate it, C has been an important and influential part of computer science for 40 years and its appeal is not diminishing. 227 Unfortunately, C has many problems and omissions that make it an unacceptable programming language for modern needs. 228 229 As stated, the goal of the \CFA project is to engineer modern language-features into C in an evolutionary rather than revolutionary way. 220 230 \CC~\cite{C++14,C++} is an example of a similar project; 221 however, it largely extended the language, and did not address manyexisting problems.\footnote{%231 however, it largely extended the C language, and did not address most of C's existing problems.\footnote{% 222 232 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.} 223 \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 233 \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. 224 234 \Index*{Java}~\cite{Java8}, \Index*{Go}~\cite{Go}, \Index*{Rust}~\cite{Rust} and \Index*{D}~\cite{D} are examples of the revolutionary approach for modernizing C/\CC, resulting in a new language rather than an extension of the descendent. 225 These languages have different syntax and semantics from C, and do not interoperate directly with C, largely because ofgarbage collection.235 These languages have different syntax and semantics from C, do not interoperate directly with C, and are not systems languages because of restrictive memory-management or garbage collection. 226 236 As a result, there is a significant learning curve to move to these languages, and C legacy-code must be rewritten. 227 These costs can be prohibitive for many companies with a large software base in C/\CC, and a significant number of programmers requiring retraining to anew programming language.228 229 The result of this project is a language that is largely backwards compatible with \Index*[C11]{\Celeven{}}~\cite{C11}, but fix ing some of the well known C problems and containing many modern languagefeatures.237 These costs can be prohibitive for many companies with a large software-base in C/\CC, and a significant number of programmers require retraining to the new programming language. 238 239 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 containing modern language-features. 230 240 Without significant extension to the C programming language, it is becoming unable to cope with the needs of modern programming problems and programmers; 231 241 as a result, it will fade into disuse. 232 242 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language. 233 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 234 While some may argue that modern language 243 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. 244 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. 235 245 236 246 237 247 \section{History} 238 248 239 The \CFA project started with \Index*{K-W C}~\cite{Buhr94a,Till89}, which extended C with new declaration syntax, multiple return values from routines, and extended assignment capabilities using the notion of tuples.249 The \CFA project started with \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. 240 250 (See~\cite{Werther96} for similar work in \Index*[C++]{\CC{}}.) 241 A first \CFA implementation of these extensions was by Esteves~\cite{Esteves04}. 242 The signature feature of \CFA is parametric-polymorphic functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name): 251 The first \CFA implementation of these extensions was by Esteves~\cite{Esteves04}. 252 253 The signature feature of \CFA is \Index{overload}able \Index{parametric-polymorphic} functions~\cite{forceone:impl,Cormack90,Duggan96} with functions generalized using a ©forall© clause (giving the language its name): 243 254 \begin{lstlisting} 244 255 ®forall( otype T )® T identity( T val ) { return val; } … … 248 259 \CFA{}\hspace{1pt}'s polymorphism was originally formalized by Ditchfiled~\cite{Ditchfield92}, and first implemented by Bilson~\cite{Bilson03}. 249 260 However, at that time, there was little interesting in extending C, so work did not continue. 250 As the saying goes, `` What goes around, comes around.'', and there is now renewed interest in the C programming language because of legacy code-bases, so the \CFA project has been restarted.261 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. 251 262 252 263 … … 255 266 256 267 \CFA is designed to integrate directly with existing C programs and libraries. 257 The most important feature of \Index{interoperability} is using the same \Index{calling convention}s, so there is no overhead to call existing C routines.268 The most important feature of \Index{interoperability} is using the same \Index{calling convention}s, so there is no complex interface or overhead to call existing C routines. 258 269 This feature allows \CFA programmers to take advantage of the existing panoply of C libraries to access thousands of external software features. 259 270 Language developers often state that adequate \Index{library support} takes more work than designing and implementing the language itself. 260 271 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. 261 Hence, \CFA begins by leveraging the large repository of C libraries withlittle cost.272 Hence, \CFA begins by leveraging the large repository of C libraries at little cost. 262 273 263 274 \begin{comment} … … 302 313 \end{comment} 303 314 304 However, it is necessary to differentiate between C and \CFA code because of name overloading, as for \CC.315 However, it is necessary to differentiate between C and \CFA code because of name \Index{overload}ing, as for \CC. 305 316 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©. 306 Whereas, \CFA wraps each of these routines into ones with the commonname ©abs©:317 Whereas, \CFA wraps each of these routines into ones with the overloaded name ©abs©: 307 318 \begin{cfa} 308 319 char abs( char ); … … 324 335 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. 325 336 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type. 326 This example strongly illustrates a core idea in \CFA: \emph{the power of a name}.337 This example strongly illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. 327 338 The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value. 328 Hence, knowing the name ©abs© should besufficient to apply it to any type where it is applicable.329 The time savings and safety of using one name uniformly versus $N$ unique names shouldnot be underestimated.330 331 332 \section[Compiling CFA Program]{Compiling\CFA Program}339 Hence, knowing the name ©abs© is sufficient to apply it to any type where it is applicable. 340 The time savings and safety of using one name uniformly versus $N$ unique names cannot be underestimated. 341 342 343 \section[Compiling a CFA Program]{Compiling a \CFA Program} 333 344 334 345 The command ©cfa© is used to compile \CFA program(s), and is based on the GNU \Indexc{gcc} command, \eg: 335 346 \begin{cfa} 336 cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] C/§\CFA §-files [ assembler/loader-files ]347 cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] C/§\CFA{}§-files [ assembler/loader-files ] 337 348 \end{cfa} 338 349 \CFA programs having the following ©gcc© flags turned on: … … 342 353 The 1999 C standard plus GNU extensions. 343 354 \item 344 {\lstset{deletekeywords={inline}} 345 \Indexc{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{©-fgnu89-inline©}} 355 \Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline]$-fgnu89-inline$}} 346 356 Use the traditional GNU semantics for inline routines in C99 mode, which allows inline routines in header files. 347 }%348 357 \end{description} 349 358 The following new \CFA options are available: … … 357 366 \Indexc{-debug}\index{compilation option!-debug@©-debug©} 358 367 The program is linked with the debugging version of the runtime system. 359 The debug version performs runtime checks to help during the debugging phase of a \CFA program, but substantially slows the execution of the program.368 The debug version performs runtime checks to help during the debugging phase of a \CFA program, but can substantially slow program execution. 360 369 The runtime checks should only be removed after the program is completely debugged. 361 370 \textbf{This option is the default.} … … 413 422 \end{description} 414 423 These preprocessor variables allow conditional compilation of programs that must work differently in these situations. 415 For example, to toggle between C and \CFA extensions, us ingthe following:424 For example, to toggle between C and \CFA extensions, use the following: 416 425 \begin{cfa} 417 426 #ifndef __CFORALL__ … … 424 433 425 434 426 \section{Constant sUnderscores}427 428 Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, \eg:435 \section{Constant Underscores} 436 437 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore}, \eg: 429 438 \begin{cfa} 430 439 2®_®147®_®483®_®648; §\C{// decimal constant}§ … … 439 448 L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§ 440 449 \end{cfa} 441 The rules for placement of underscores is as follows:442 \begin{enumerate} 450 The rules for placement of underscores are: 451 \begin{enumerate}[topsep=5pt,itemsep=5pt,parsep=0pt] 443 452 \item 444 453 A sequence of underscores is disallowed, \eg ©12__34© is invalid. … … 461 470 \label{s:BackquoteIdentifiers} 462 471 463 \CFA accommodates keyword clashes with existing C variable-names by syntactic transformations using the \CFA backquote escape-mechanism: 472 \CFA introduces in new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code. 473 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 464 474 \begin{cfa} 465 475 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§ … … 467 477 \end{cfa} 468 478 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. 469 \VRef[Figure]{f: InterpositionHeaderFile} shows how clashes in C header files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©:479 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in C header files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©: 470 480 471 481 \begin{figure} … … 475 485 #define otype ®`®otype®`® §\C{// make keyword an identifier}§ 476 486 #define __CFA_BFD_H__ 477 #endif // ! otype478 479 #include_next <bfd.h> §\C{// must have internal check for multiple expansion}§480 487 #endif 488 489 ®#include_next <bfd.h> §\C{// must have internal check for multiple expansion}§ 490 ® 481 491 #if defined( otype ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§ 482 492 #undef otype 483 493 #undef __CFA_BFD_H__ 484 #endif // otype && __CFA_BFD_H__485 \end{cfa} 486 \caption{ Interposition of Header File}487 \label{f: InterpositionHeaderFile}494 #endif 495 \end{cfa} 496 \caption{Header-File Interposition} 497 \label{f:HeaderFileInterposition} 488 498 \end{figure} 489 499 490 500 491 \section{Declarations} 492 \label{s:Declarations} 493 494 C declaration syntax is notoriously confusing and error prone. 495 For example, many C programmers are confused by a declaration as simple as: 496 \begin{quote2} 497 \begin{tabular}{@{}ll@{}} 498 \begin{cfa} 499 int * x[5] 500 \end{cfa} 501 & 502 \raisebox{-0.75\totalheight}{\input{Cdecl}} 503 \end{tabular} 504 \end{quote2} 505 Is this an array of 5 pointers to integers or a \Index{pointer} to an array of 5 integers? 506 The fact this declaration is unclear to many C programmers means there are \Index{productivity} and \Index{safety} issues even for basic programs. 507 Another example of confusion results from the fact that a routine name and its parameters are embedded within the return type, mimicking the way the return value is used at the routine's call site. 508 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 509 \begin{cfa} 510 int ®(*®f®())[®5®]® {...}; §\C{definition}§ 511 ... ®(*®f®())[®3®]® += 1; §\C{usage}§ 512 \end{cfa} 513 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). 514 While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice. 515 516 \CFA provides its own type, variable and routine declarations, using a different syntax. 517 The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type. 518 In the following example, \R{red} is the base type and \B{blue} is qualifiers. 519 The \CFA declarations move the qualifiers to the left of the base type, \ie move the blue to the left of the red, while the qualifiers have the same meaning but are ordered left to right to specify a variable's type. 520 \begin{quote2} 521 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 522 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 523 \begin{cfa} 524 ß[5] *ß ®int® x1; 525 ß* [5]ß ®int® x2; 526 ß[* [5] int]ß f®( int p )®; 527 \end{cfa} 528 & 529 \begin{cfa} 530 ®int® ß*ß x1 ß[5]ß; 531 ®int® ß(*ßx2ß)[5]ß; 532 ßint (*ßf®( int p )®ß)[5]ß; 533 \end{cfa} 534 \end{tabular} 535 \end{quote2} 536 The only exception is \Index{bit field} specification, which always appear to the right of the base type. 537 % Specifically, the character ©*© is used to indicate a pointer, square brackets ©[©\,©]© are used to represent an array or function return value, and parentheses ©()© are used to indicate a routine parameter. 538 However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list. 539 For instance, variables ©x© and ©y© of type \Index{pointer} to integer are defined in \CFA as follows: 540 \begin{quote2} 541 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 542 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 543 \begin{cfa} 544 ®*® int x, y; 545 \end{cfa} 546 & 547 \begin{cfa} 548 int ®*®x, ®*®y; 549 \end{cfa} 550 \end{tabular} 551 \end{quote2} 552 The downside of this semantics is the need to separate regular and \Index{pointer} declarations: 553 \begin{quote2} 554 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 555 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 556 \begin{cfa} 557 ®*® int x; 558 int y; 559 \end{cfa} 560 & 561 \begin{cfa} 562 int ®*®x, y; 563 564 \end{cfa} 565 \end{tabular} 566 \end{quote2} 567 which is \Index{prescribing} a safety benefit. 568 Other examples are: 569 \begin{quote2} 570 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 571 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 572 \begin{cfa} 573 [ 5 ] int z; 574 [ 5 ] * char w; 575 * [ 5 ] double v; 576 struct s { 577 int f0:3; 578 * int f1; 579 [ 5 ] * int f2; 580 }; 581 \end{cfa} 582 & 583 \begin{cfa} 584 int z[ 5 ]; 585 char * w[ 5 ]; 586 double (* v)[ 5 ]; 587 struct s { 588 int f0:3; 589 int * f1; 590 int * f2[ 5 ] 591 }; 592 \end{cfa} 593 & 594 \begin{cfa} 595 // array of 5 integers 596 // array of 5 pointers to char 597 // pointer to array of 5 doubles 598 599 // common bit field syntax 600 601 602 603 \end{cfa} 604 \end{tabular} 605 \end{quote2} 606 607 All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg: 608 \begin{quote2} 609 \begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}} 610 \multicolumn{1}{c@{\hspace{1em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{1em}}}{\textbf{C}} \\ 611 \begin{cfa} 612 const * const int x; 613 const * [ 5 ] const int y; 614 \end{cfa} 615 & 616 \begin{cfa} 617 int const * const x; 618 const int (* const y)[ 5 ] 619 \end{cfa} 620 & 621 \begin{cfa} 622 // const pointer to const integer 623 // const pointer to array of 5 const integers 624 \end{cfa} 625 \end{tabular} 626 \end{quote2} 627 All declaration qualifiers, \eg ©extern©, ©static©, etc., are used in the normal way with the new declarations but can only appear at the start of a \CFA routine declaration,\footnote{\label{StorageClassSpecifier} 628 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.~\cite[\S~6.11.5(1)]{C11}} \eg: 629 \begin{quote2} 630 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 631 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 632 \begin{cfa} 633 extern [ 5 ] int x; 634 static * const int y; 635 \end{cfa} 636 & 637 \begin{cfa} 638 int extern x[ 5 ]; 639 const int static * y; 640 \end{cfa} 641 & 642 \begin{cfa} 643 // externally visible array of 5 integers 644 // internally visible pointer to constant int 645 \end{cfa} 646 \end{tabular} 647 \end{quote2} 648 649 The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine ©sizeof©: 650 \begin{quote2} 651 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 652 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 653 \begin{cfa} 654 y = (®* int®)x; 655 i = sizeof(®[ 5 ] * int®); 656 \end{cfa} 657 & 658 \begin{cfa} 659 y = (®int *®)x; 660 i = sizeof(®int * [ 5 ]®); 661 \end{cfa} 662 \end{tabular} 663 \end{quote2} 664 665 Finally, new \CFA declarations may appear together with C declarations in the same program block, but cannot be mixed within a specific declaration. 666 Therefore, a programmer has the option of either continuing to use traditional C declarations or take advantage of the new style. 667 Clearly, both styles need to be supported for some time due to existing C-style header-files, particularly for UNIX systems. 668 669 670 \section{Pointer/Reference} 671 672 C provides a \newterm{pointer type}; 673 \CFA adds a \newterm{reference type}. 674 These types may be derived from an object or routine type, called the \newterm{referenced type}. 675 Objects of these types contain an \newterm{address}, which is normally a location in memory, but may also address memory-mapped registers in hardware devices. 676 An integer constant expression with the value 0, or such an expression cast to type ©void *©, is called a \newterm{null-pointer constant}.\footnote{ 677 One way to conceptualize the null pointer is that no variable is placed at this address, so the null-pointer address can be used to denote an uninitialized pointer/reference object; 678 \ie the null pointer is guaranteed to compare unequal to a pointer to any object or routine.} 679 An address is \newterm{sound}, if it points to a valid memory location in scope, \ie within the program's execution-environment and has not been freed. 680 Dereferencing an \newterm{unsound} address, including the null pointer, is \Index{undefined}, often resulting in a \Index{memory fault}. 681 682 A program \newterm{object} is a region of data storage in the execution environment, the contents of which can represent values. 683 In most cases, objects are located in memory at an address, and the variable name for an object is an implicit address to the object generated by the compiler and automatically dereferenced, as in: 684 \begin{quote2} 685 \begin{tabular}{@{}ll@{\hspace{2em}}l@{}} 686 \begin{cfa} 687 int x; 688 x = 3; 689 int y; 690 y = x; 691 \end{cfa} 692 & 693 \raisebox{-0.45\totalheight}{\input{pointer1}} 694 & 695 \begin{cfa} 696 int * ®const® x = (int *)100 697 *x = 3; // implicit dereference 698 int * ®const® y = (int *)104; 699 *y = *x; // implicit dereference 700 \end{cfa} 701 \end{tabular} 702 \end{quote2} 703 where the right example is how the compiler logically interprets the variables in the left example. 704 Since a variable name only points to one address during its lifetime, it is an \Index{immutable} \Index{pointer}; 705 hence, the implicit type of pointer variables ©x© and ©y© are constant pointers in the compiler interpretation. 706 In general, variable addresses are stored in instructions instead of loaded from memory, and hence may not occupy storage. 707 These approaches are contrasted in the following: 708 \begin{quote2} 709 \begin{tabular}{@{}l|l@{}} 710 \multicolumn{1}{c|}{explicit variable address} & \multicolumn{1}{c}{implicit variable address} \\ 711 \hline 712 \begin{cfa} 713 lda r1,100 // load address of x 714 ld r2,(r1) // load value of x 715 lda r3,104 // load address of y 716 st r2,(r3) // store x into y 717 \end{cfa} 718 & 719 \begin{cfa} 720 721 ld r2,(100) // load value of x 722 723 st r2,(104) // store x into y 724 \end{cfa} 725 \end{tabular} 726 \end{quote2} 727 Finally, the immutable nature of a variable's address and the fact that there is no storage for the variable pointer means pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible. 728 Therefore, the expression ©x = y© has only one meaning, ©*x = *y©, \ie manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of \Index{instruction decoding}. 729 730 A \Index{pointer}/\Index{reference} object is a generalization of an object variable-name, \ie a mutable address that can point to more than one memory location during its lifetime. 731 (Similarly, an integer variable can contain multiple integer literals during its lifetime versus an integer constant representing a single literal during its lifetime, and like a variable name, may not occupy storage if the literal is embedded directly into instructions.) 732 Hence, a pointer occupies memory to store its current address, and the pointer's value is loaded by dereferencing, \eg: 733 \begin{quote2} 734 \begin{tabular}{@{}l@{\hspace{2em}}l@{}} 735 \begin{cfa} 736 int x, y, ®*® p1, ®*® p2, ®**® p3; 737 p1 = ®&®x; // p1 points to x 738 p2 = p1; // p2 points to x 739 p1 = ®&®y; // p1 points to y 740 p3 = &p2; // p3 points to p2 741 \end{cfa} 742 & 743 \raisebox{-0.5\totalheight}{\input{pointer2.pstex_t}} 744 \end{tabular} 745 \end{quote2} 746 747 Notice, an address has a \Index{duality}\index{address!duality}: a location in memory or the value at that location. 748 In many cases, a compiler might be able to infer the best meaning for these two cases. 749 For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage 750 \begin{cfa} 751 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§ 752 \end{cfa} 753 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. 754 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 755 756 Rather than inferring dereference, most programming languages pick one implicit dereferencing semantics, and the programmer explicitly indicates the other to resolve address-duality. 757 In C, objects of pointer type always manipulate the pointer object's address: 758 \begin{cfa} 759 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§ 760 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§ 761 \end{cfa} 762 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant: 763 \begin{cfa} 764 p1 = p2; §\C{// pointer address assignment}§ 765 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§ 766 \end{cfa} 767 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©). 768 769 However, in most other situations, the pointed-to value is requested more often than the pointer address. 770 \begin{cfa} 771 *p2 = ((*p1 + *p2) * (**p3 - *p1)) / (**p3 - 15); 772 \end{cfa} 773 In this case, it is tedious to explicitly write the dereferencing, and error prone when pointer arithmetic is allowed. 774 It is better to have the compiler generate the dereferencing and have no implicit pointer arithmetic: 775 \begin{cfa} 776 p2 = ((p1 + p2) * (p3 - p1)) / (p3 - 15); 777 \end{cfa} 778 779 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). 780 \begin{cfa} 781 int x, y, ®&® r1, ®&® r2, ®&&® r3; 782 ®&®r1 = &x; §\C{// r1 points to x}§ 783 ®&®r2 = &r1; §\C{// r2 points to x}§ 784 ®&®r1 = &y; §\C{// r1 points to y}§ 785 ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§ 786 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§ 787 \end{cfa} 788 Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example. 789 Hence, a reference behaves like the variable name for the current variable it is pointing-to. 790 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: 791 \begin{cfa} 792 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15); 793 \end{cfa} 794 When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out. 795 However, in C, the cancellation always yields a value (\Index{rvalue}).\footnote{ 796 The unary ©&© operator yields the address of its operand. 797 If the operand has type ``type'', the result has type ``pointer to type''. 798 If the operand is the result of a unary ©*© operator, neither that operator nor the ©&© operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.~\cite[\S~6.5.3.2--3]{C11}} 799 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 800 \begin{cfa} 801 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§ 802 \end{cfa} 803 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 804 \begin{cfa} 805 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§ 806 \end{cfa} 807 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. 808 809 Fundamentally, pointer and reference objects are functionally interchangeable because both contain addresses. 810 \begin{cfa} 811 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 812 &r1 = x, &&r2 = r1, &&&r3 = r2; 813 ***p3 = 3; §\C{// change x}§ 814 r3 = 3; §\C{// change x, ***r3}§ 815 **p3 = ...; §\C{// change p1}§ 816 &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§ 817 *p3 = ...; §\C{// change p2}§ 818 &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§ 819 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§ 820 \end{cfa} 821 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types. 822 Therefore, the choice between them is based solely on whether the address is dereferenced frequently or infrequently, which dictates the amount of implicit dereferencing aid from the compiler. 823 824 As for a pointer type, a reference type may have qualifiers: 825 \begin{cfa} 826 const int cx = 5; §\C{// cannot change cx;}§ 827 const int & cr = cx; §\C{// cannot change what cr points to}§ 828 ®&®cr = &cx; §\C{// can change cr}§ 829 cr = 7; §\C{// error, cannot change cx}§ 830 int & const rc = x; §\C{// must be initialized}§ 831 ®&®rc = &x; §\C{// error, cannot change rc}§ 832 const int & const crc = cx; §\C{// must be initialized}§ 833 crc = 7; §\C{// error, cannot change cx}§ 834 ®&®crc = &cx; §\C{// error, cannot change crc}§ 835 \end{cfa} 836 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}: 837 \begin{cfa} 838 int & const cr = *0; §\C{// where 0 is the int * zero}§ 839 \end{cfa} 840 Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management: 841 \begin{cfa} 842 int & const cr = *malloc(); 843 cr = 5; 844 free( &cr ); 845 cr = 7; §\C{// unsound pointer dereference}§ 846 \end{cfa} 847 848 The position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers. 849 The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations; 850 \CFA-style declarations (see \VRef{s:Declarations}) attempt to address this issue: 851 \begin{quote2} 852 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 853 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 854 \begin{cfa} 855 ®const® * ®const® * const int ccp; 856 ®const® & ®const® & const int ccr; 857 \end{cfa} 858 & 859 \begin{cfa} 860 const int * ®const® * ®const® ccp; 861 862 \end{cfa} 863 \end{tabular} 864 \end{quote2} 865 where the \CFA declaration is read left-to-right. 866 867 Finally, like pointers, references are usable and composable with other type operators and generators. 868 \begin{cfa} 869 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§ 870 &ar[1] = &w; §\C{// change reference array element}§ 871 typeof( ar[1] ) p; §\C{// (gcc) is int, i.e., the type of referenced object}§ 872 typeof( &ar[1] ) q; §\C{// (gcc) is int \&, i.e., the type of reference}§ 873 sizeof( ar[1] ) == sizeof( int ); §\C{// is true, i.e., the size of referenced object}§ 874 sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, i.e., the size of a reference}§ 875 \end{cfa} 876 877 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}. 878 Also, \CC does not allow \Index{array}s\index{array!reference} of reference\footnote{ 879 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.} 880 \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. 881 882 883 \subsection{Initialization} 884 885 \Index{Initialization} is different than \Index{assignment} because initialization occurs on the empty (uninitialized) storage on an object, while assignment occurs on possibly initialized storage of an object. 886 There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding. 887 Because the object being initialized has no value, there is only one meaningful semantics with respect to address duality: it must mean address as there is no pointed-to value. 888 In contrast, the left-hand side of assignment has an address that has a duality. 889 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 890 \begin{cfa} 891 int * p = &x; §\C{// assign address of x}§ 892 ®int * p = x;® §\C{// assign value of x}§ 893 int & r = x; §\C{// must have address of x}§ 894 \end{cfa} 895 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). 896 Therefore, for safety, this context requires an address, so it is superfluous to require explicitly taking the address of the initialization object, even though the type is incorrect. 897 Note, this is strictly a convenience and safety feature for a programmer. 898 Hence, \CFA allows ©r© to be assigned ©x© because it infers a reference for ©x©, by implicitly inserting a address-of operator, ©&©, and it is an error to put an ©&© because the types no longer match due to the implicit dereference. 899 Unfortunately, C allows ©p© to be assigned with ©&x© (address) or ©x© (value), but most compilers warn about the latter assignment as being potentially incorrect. 900 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. 901 \begin{cfa} 902 int & f( int & r ); §\C{// reference parameter and return}§ 903 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§ 904 \end{cfa} 905 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©. 906 Since operator routine ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references. 907 \begin{cfa} 908 int temp1 = f( x ), temp2 = f( y ); 909 z = temp1 + temp2; 910 \end{cfa} 911 This \Index{implicit referencing} is crucial for reducing the syntactic burden for programmers when using references; 912 otherwise references have the same syntactic burden as pointers in these contexts. 913 914 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. 915 \begin{cfa} 916 void f( ®const® int & cr ); 917 void g( ®const® int * cp ); 918 f( 3 ); g( ®&®3 ); 919 f( x + y ); g( ®&®(x + y) ); 920 \end{cfa} 921 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. 922 The ©&© before the constant/expression for the pointer-type parameter (©g©) is a \CFA extension necessary to type match and is a common requirement before a variable in C (\eg ©scanf©). 923 Importantly, ©&3© may not be equal to ©&3©, where the references occur across calls because the temporaries maybe different on each call. 924 925 \CFA \emph{extends} this semantics to a mutable pointer/reference parameter, and the compiler implicitly creates the necessary temporary (copying the argument), which is subsequently pointed-to by the reference parameter and can be changed.\footnote{ 926 If whole program analysis is possible, and shows the parameter is not assigned, \ie it is ©const©, the temporary is unnecessary.} 927 \begin{cfa} 928 void f( int & r ); 929 void g( int * p ); 930 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§ 931 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§ 932 \end{cfa} 933 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ 934 This conversion attempts to address the \newterm{const hell} problem, when the innocent addition of a ©const© qualifier causes a cascade of type failures, requiring an unknown number of additional ©const© qualifiers, until it is discovered a ©const© qualifier cannot be added and all the ©const© qualifiers must be removed.} 935 The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. 936 937 %\CFA attempts to handle pointers and references in a uniform, symmetric manner. 938 Finally, C handles \Index{routine object}s in an inconsistent way. 939 A routine object is both a pointer and a reference (\Index{particle and wave}). 940 \begin{cfa} 941 void f( int i ); 942 void (*fp)( int ); §\C{// routine pointer}§ 943 fp = f; §\C{// reference initialization}§ 944 fp = &f; §\C{// pointer initialization}§ 945 fp = *f; §\C{// reference initialization}§ 946 fp(3); §\C{// reference invocation}§ 947 (*fp)(3); §\C{// pointer invocation}§ 948 \end{cfa} 949 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. 950 Instead, a routine object should be referenced by a ©const© reference: 951 \begin{cfa} 952 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§ 953 fr = ... §\C{// error, cannot change code}§ 954 &fr = ...; §\C{// changing routine reference}§ 955 fr( 3 ); §\C{// reference call to f}§ 956 (*fr)(3); §\C{// error, incorrect type}§ 957 \end{cfa} 958 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ 959 Dynamic code rewriting is possible but only in special circumstances.} 960 \CFA allows this additional use of references for routine objects in an attempt to give a more consistent meaning for them. 961 962 963 \subsection{Address-of Semantics} 964 965 In C, ©&E© is an rvalue for any expression ©E©. 966 \CFA extends the ©&© (address-of) operator as follows: 967 \begin{itemize} 968 \item 969 if ©R© is an \Index{rvalue} of type ©T &$_1$...&$_r$© where $r \ge 1$ references (©&© symbols) than ©&R© has type ©T ®*®&$_{\color{red}2}$...&$_{\color{red}r}$©, \ie ©T© pointer with $r-1$ references (©&© symbols). 970 971 \item 972 if ©L© is an \Index{lvalue} of type ©T &$_1$...&$_l$© where $l \ge 0$ references (©&© symbols) then ©&L© has type ©T ®*®&$_{\color{red}1}$...&$_{\color{red}l}$©, \ie ©T© pointer with $l$ references (©&© symbols). 973 \end{itemize} 974 The following example shows the first rule applied to different \Index{rvalue} contexts: 975 \begin{cfa} 976 int x, * px, ** ppx, *** pppx, **** ppppx; 977 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 978 x = rrrx; // rrrx is an lvalue with type int &&& (equivalent to x) 979 px = &rrrx; // starting from rrrx, &rrrx is an rvalue with type int *&&& (&x) 980 ppx = &&rrrx; // starting from &rrrx, &&rrrx is an rvalue with type int **&& (&rx) 981 pppx = &&&rrrx; // starting from &&rrrx, &&&rrrx is an rvalue with type int ***& (&rrx) 982 ppppx = &&&&rrrx; // starting from &&&rrrx, &&&&rrrx is an rvalue with type int **** (&rrrx) 983 \end{cfa} 984 The following example shows the second rule applied to different \Index{lvalue} contexts: 985 \begin{cfa} 986 int x, * px, ** ppx, *** pppx; 987 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 988 rrrx = 2; // rrrx is an lvalue with type int &&& (equivalent to x) 989 &rrrx = px; // starting from rrrx, &rrrx is an rvalue with type int *&&& (rx) 990 &&rrrx = ppx; // starting from &rrrx, &&rrrx is an rvalue with type int **&& (rrx) 991 &&&rrrx = pppx; // starting from &&rrrx, &&&rrrx is an rvalue with type int ***& (rrrx) 992 \end{cfa} 993 994 995 \subsection{Conversions} 996 997 C provides a basic implicit conversion to simplify variable usage: 998 \begin{enumerate} 999 \setcounter{enumi}{-1} 1000 \item 1001 lvalue to rvalue conversion: ©cv T© converts to ©T©, which allows implicit variable dereferencing. 1002 \begin{cfa} 1003 int x; 1004 x + 1; // lvalue variable (int) converts to rvalue for expression 1005 \end{cfa} 1006 An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped. 1007 \end{enumerate} 1008 \CFA provides three new implicit conversion for reference types to simplify reference usage. 1009 \begin{enumerate} 1010 \item 1011 reference to rvalue conversion: ©cv T &© converts to ©T©, which allows implicit reference dereferencing. 1012 \begin{cfa} 1013 int x, &r = x, f( int p ); 1014 x = ®r® + f( ®r® ); // lvalue reference converts to rvalue 1015 \end{cfa} 1016 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped. 1017 1018 \item 1019 lvalue to reference conversion: \lstinline[deletekeywords={lvalue}]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references. 1020 \begin{cfa} 1021 int x, &r = ®x®, f( int & p ); // lvalue variable (int) convert to reference (int &) 1022 f( ®x® ); // lvalue variable (int) convert to reference (int &) 1023 \end{cfa} 1024 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost. 1025 Conversion can expand a type, where ©cv1© $>$ ©cv2©, \eg passing a ©const volatile int© to an ©int &©, which has high cost (\Index{warning}); 1026 furthermore, if ©cv1© has ©const© but not ©cv2©, a temporary variable is created to preserve the immutable lvalue. 1027 1028 \item 1029 rvalue to reference conversion: ©T© converts to ©cv T &©, which allows binding references to temporaries. 1030 \begin{cfa} 1031 int x, & f( int & p ); 1032 f( ®x + 3® ); // rvalue parameter (int) implicitly converts to lvalue temporary reference (int &) 1033 ®&f®(...) = &x; // rvalue result (int &) implicitly converts to lvalue temporary reference (int &) 1034 \end{cfa} 1035 In both case, modifications to the temporary are inaccessible (\Index{warning}). 1036 Conversion expands the temporary-type with ©cv©, which is low cost since the temporary is inaccessible. 1037 \end{enumerate} 1038 1039 1040 \begin{comment} 1041 From: Richard Bilson <rcbilson@gmail.com> 1042 Date: Wed, 13 Jul 2016 01:58:58 +0000 1043 Subject: Re: pointers / references 1044 To: "Peter A. Buhr" <pabuhr@plg2.cs.uwaterloo.ca> 1045 1046 As a general comment I would say that I found the section confusing, as you move back and forth 1047 between various real and imagined programming languages. If it were me I would rewrite into two 1048 subsections, one that specifies precisely the syntax and semantics of reference variables and 1049 another that provides the rationale. 1050 1051 I don't see any obvious problems with the syntax or semantics so far as I understand them. It's not 1052 obvious that the description you're giving is complete, but I'm sure you'll find the special cases 1053 as you do the implementation. 1054 1055 My big gripes are mostly that you're not being as precise as you need to be in your terminology, and 1056 that you say a few things that aren't actually true even though I generally know what you mean. 1057 1058 20 C provides a pointer type; CFA adds a reference type. Both types contain an address, which is normally a 1059 21 location in memory. 1060 1061 An address is not a location in memory; an address refers to a location in memory. Furthermore it 1062 seems weird to me to say that a type "contains" an address; rather, objects of that type do. 1063 1064 21 Special addresses are used to denote certain states or access co-processor memory. By 1065 22 convention, no variable is placed at address 0, so addresses like 0, 1, 2, 3 are often used to denote no-value 1066 23 or other special states. 1067 1068 This isn't standard C at all. There has to be one null pointer representation, but it doesn't have 1069 to be a literal zero representation and there doesn't have to be more than one such representation. 1070 1071 23 Often dereferencing a special state causes a memory fault, so checking is necessary 1072 24 during execution. 1073 1074 I don't see the connection between the two clauses here. I feel like if a bad pointer will not cause 1075 a memory fault then I need to do more checking, not less. 1076 1077 24 If the programming language assigns addresses, a program's execution is sound, \ie all 1078 25 addresses are to valid memory locations. 1079 1080 You haven't said what it means to "assign" an address, but if I use my intuitive understanding of 1081 the term I don't see how this can be true unless you're assuming automatic storage management. 1082 1083 1 Program variables are implicit pointers to memory locations generated by the compiler and automatically 1084 2 dereferenced, as in: 1085 1086 There is no reason why a variable needs to have a location in memory, and indeed in a typical 1087 program many variables will not. In standard terminology an object identifier refers to data in the 1088 execution environment, but not necessarily in memory. 1089 1090 13 A pointer/reference is a generalization of a variable name, \ie a mutable address that can point to more 1091 14 than one memory location during its lifetime. 1092 1093 I feel like you're off the reservation here. In my world there are objects of pointer type, which 1094 seem to be what you're describing here, but also pointer values, which can be stored in an object of 1095 pointer type but don't necessarily have to be. For example, how would you describe the value denoted 1096 by "&main" in a C program? I would call it a (function) pointer, but that doesn't satisfy your 1097 definition. 1098 1099 16 not occupy storage as the literal is embedded directly into instructions.) Hence, a pointer occupies memory 1100 17 to store its current address, and the pointer's value is loaded by dereferencing, e.g.: 1101 1102 As with my general objection regarding your definition of variables, there is no reason why a 1103 pointer variable (object of pointer type) needs to occupy memory. 1104 1105 21 p2 = p1 + x; // compiler infers *p2 = *p1 + x; 1106 1107 What language are we in now? 1108 1109 24 pointer usage. However, in C, the following cases are ambiguous, especially with pointer arithmetic: 1110 25 p1 = p2; // p1 = p2 or *p1 = *p2 1111 1112 This isn't ambiguous. it's defined to be the first option. 1113 1114 26 p1 = p1 + 1; // p1 = p1 + 1 or *p1 = *p1 + 1 1115 1116 Again, this statement is not ambiguous. 1117 1118 13 example. Hence, a reference behaves like the variable name for the current variable it is pointing-to. The 1119 14 simplest way to understand a reference is to imagine the compiler inserting a dereference operator before 1120 15 the reference variable for each reference qualifier in a declaration, e.g.: 1121 1122 It's hard for me to understand who the audience for this part is. I think a practical programmer is 1123 likely to be satisfied with "a reference behaves like the variable name for the current variable it 1124 is pointing-to," maybe with some examples. Your "simplest way" doesn't strike me as simpler than 1125 that. It feels like you're trying to provide a more precise definition for the semantics of 1126 references, but it isn't actually precise enough to be a formal specification. If you want to 1127 express the semantics of references using rewrite rules that's a great way to do it, but lay the 1128 rules out clearly, and when you're showing an example of rewriting keep your 1129 references/pointers/values separate (right now, you use \eg "r3" to mean a reference, a pointer, 1130 and a value). 1131 1132 24 Cancellation works to arbitrary depth, and pointer and reference values are interchangeable because both 1133 25 contain addresses. 1134 1135 Except they're not interchangeable, because they have different and incompatible types. 1136 1137 40 Interestingly, C++ deals with the address duality by making the pointed-to value the default, and prevent- 1138 41 ing changes to the reference address, which eliminates half of the duality. Java deals with the address duality 1139 42 by making address assignment the default and requiring field assignment (direct or indirect via methods), 1140 43 \ie there is no builtin bit-wise or method-wise assignment, which eliminates half of the duality. 1141 1142 I can follow this but I think that's mostly because I already understand what you're trying to 1143 say. I don't think I've ever heard the term "method-wise assignment" and I don't see you defining 1144 it. Furthermore Java does have value assignment of basic (non-class) types, so your summary here 1145 feels incomplete. (If it were me I'd drop this paragraph rather than try to save it.) 1146 1147 11 Hence, for type & const, there is no pointer assignment, so &rc = &x is disallowed, and the address value 1148 12 cannot be 0 unless an arbitrary pointer is assigned to the reference. 1149 1150 Given the pains you've taken to motivate every little bit of the semantics up until now, this last 1151 clause ("the address value cannot be 0") comes out of the blue. It seems like you could have 1152 perfectly reasonable semantics that allowed the initialization of null references. 1153 1154 12 In effect, the compiler is managing the 1155 13 addresses for type & const not the programmer, and by a programming discipline of only using references 1156 14 with references, address errors can be prevented. 1157 1158 Again, is this assuming automatic storage management? 1159 1160 18 rary binding. For reference initialization (like pointer), the initializing value must be an address (lvalue) not 1161 19 a value (rvalue). 1162 1163 This sentence appears to suggest that an address and an lvalue are the same thing. 1164 1165 20 int * p = &x; // both &x and x are possible interpretations 1166 1167 Are you saying that we should be considering "x" as a possible interpretation of the initializer 1168 "&x"? It seems to me that this expression has only one legitimate interpretation in context. 1169 1170 21 int & r = x; // x unlikely interpretation, because of auto-dereferencing 1171 1172 You mean, we can initialize a reference using an integer value? Surely we would need some sort of 1173 cast to induce that interpretation, no? 1174 1175 22 Hence, the compiler implicitly inserts a reference operator, &, before the initialization expression. 1176 1177 But then the expression would have pointer type, which wouldn't be compatible with the type of r. 1178 1179 22 Similarly, 1180 23 when a reference is used for a parameter/return type, the call-site argument does not require a reference 1181 24 operator. 1182 1183 Furthermore, it would not be correct to use a reference operator. 1184 1185 45 The implicit conversion allows 1186 1 seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. 1187 2 While C' attempts to handle pointers and references in a uniform, symmetric manner, C handles routine 1188 3 variables in an inconsistent way: a routine variable is both a pointer and a reference (particle and wave). 1189 1190 After all this talk of how expressions can have both pointer and value interpretations, you're 1191 disparaging C because it has expressions that have both pointer and value interpretations? 1192 1193 On Sat, Jul 9, 2016 at 4:18 PM Peter A. Buhr <pabuhr@plg.uwaterloo.ca> wrote: 1194 > Aaron discovered a few places where "&"s are missing and where there are too many "&", which are 1195 > corrected in the attached updated. None of the text has changed, if you have started reading 1196 > already. 1197 \end{comment} 1198 1199 1200 \section{Routine Definition} 1201 1202 \CFA also supports a new syntax for routine definition, as well as \Celeven and K\&R routine syntax. 1203 The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: 1204 \begin{cfa} 1205 ®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) { 1206 §\emph{routine body}§ 1207 } 1208 \end{cfa} 1209 where routine ©f© has three output (return values) and three input parameters. 1210 Existing C syntax cannot be extended with multiple return types because it is impossible to embed a single routine name within multiple return type specifications. 1211 1212 In detail, the brackets, ©[]©, enclose the result type, where each return value is named and that name is a local variable of the particular return type.\footnote{ 1213 \Index*{Michael Tiemann}, with help from \Index*{Doug Lea}, provided named return values in g++, circa 1989.} 1214 The value of each local return variable is automatically returned at routine termination. 1215 Declaration qualifiers can only appear at the start of a routine definition, \eg: 1216 \begin{cfa} 1217 ®extern® [ int x ] g( int y ) {§\,§} 1218 \end{cfa} 1219 Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified; 1220 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: 1221 \begin{cfa} 1222 [§\,§] g(); §\C{// no input or output parameters}§ 1223 [ void ] g( void ); §\C{// no input or output parameters}§ 1224 \end{cfa} 1225 1226 Routine f is called as follows: 1227 \begin{cfa} 1228 [ i, j, ch ] = f( 3, 'a', ch ); 1229 \end{cfa} 1230 The list of return values from f and the grouping on the left-hand side of the assignment is called a \newterm{return list} and discussed in Section 12. 1231 1232 \CFA style declarations cannot be used to declare parameters for K\&R style routine definitions because of the following ambiguity: 1233 \begin{cfa} 1234 int (*f(x))[ 5 ] int x; {} 1235 \end{cfa} 1236 The string ``©int (*f(x))[ 5 ]©'' declares a K\&R style routine of type returning a pointer to an array of 5 integers, while the string ``©[ 5 ] int x©'' declares a \CFA style parameter x of type array of 5 integers. 1237 Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string. 1238 As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity: 1239 \begin{cfa} 1240 typedef int foo; 1241 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 1242 \end{cfa} 1243 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. 1244 The redefinition of a type name in a parameter list is the only context in C where the character ©*© can appear to the left of a type name, and \CFA relies on all type qualifier characters appearing to the right of the type name. 1245 The inability to use \CFA declarations in these two contexts is probably a blessing because it precludes programmers from arbitrarily switching between declarations forms within a declaration contexts. 1246 1247 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 1248 \begin{cfa} 1249 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§ 1250 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§ 1251 \end{cfa} 1252 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: 1253 \begin{cfa} 1254 #define ptoa( n, d ) int (*n)[ d ] 1255 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§ 1256 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§ 1257 \end{cfa} 1258 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. 1259 1260 1261 \subsection{Named Return Values} 1262 1263 \Index{Named return values} handle the case where it is necessary to define a local variable whose value is then returned in a ©return© statement, as in: 1264 \begin{cfa} 1265 int f() { 1266 int x; 1267 ... x = 0; ... x = y; ... 1268 return x; 1269 } 1270 \end{cfa} 1271 Because the value in the return variable is automatically returned when a \CFA routine terminates, the ©return© statement \emph{does not} contain an expression, as in: 1272 \newline 1273 \begin{minipage}{\linewidth} 1274 \begin{cfa} 1275 ®[ int x, int y ]® f() { 1276 int z; 1277 ... x = 0; ... y = z; ... 1278 ®return;® §\C{// implicitly return x, y}§ 1279 } 1280 \end{cfa} 1281 \end{minipage} 1282 \newline 1283 When the return is encountered, the current values of ©x© and ©y© are returned to the calling routine. 1284 As well, ``falling off the end'' of a routine without a ©return© statement is permitted, as in: 1285 \begin{cfa} 1286 [ int x, int y ] f() { 1287 ... 1288 } §\C{// implicitly return x, y}§ 1289 \end{cfa} 1290 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. 1291 1292 Named return values may be used in conjunction with named parameter values; 1293 specifically, a return and parameter can have the same name. 1294 \begin{cfa} 1295 [ int x, int y ] f( int, x, int y ) { 1296 ... 1297 } §\C{// implicitly return x, y}§ 1298 \end{cfa} 1299 This notation allows the compiler to eliminate temporary variables in nested routine calls. 1300 \begin{cfa} 1301 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§ 1302 int a, b; 1303 [a, b] = f( f( f( a, b ) ) ); 1304 \end{cfa} 1305 While the compiler normally ignores parameters names in prototype declarations, here they are used to eliminate temporary return-values by inferring that the results of each call are the inputs of the next call, and ultimately, the left-hand side of the assignment. 1306 Hence, even without the body of routine ©f© (separate compilation), it is possible to perform a global optimization across routine calls. 1307 The compiler warns about naming inconsistencies between routine prototype and definition in this case, and behaviour is \Index{undefined} if the programmer is inconsistent. 1308 1309 1310 \subsection{Routine Prototype} 1311 1312 The syntax of the new routine prototype declaration follows directly from the new routine definition syntax; 1313 as well, parameter names are optional, \eg: 1314 \begin{cfa} 1315 [ int x ] f (); §\C{// returning int with no parameters}§ 1316 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§ 1317 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§ 1318 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§ 1319 \end{cfa} 1320 This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). 1321 It is possible to declare multiple routine-prototypes in a single declaration, but the entire type specification is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:Declarations}), \eg: 1322 \begin{quote2} 1323 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1324 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1325 \begin{cfa} 1326 [ int ] f( int ), g; 1327 \end{cfa} 1328 & 1329 \begin{cfa} 1330 int f( int ), g( int ); 1331 \end{cfa} 1332 \end{tabular} 1333 \end{quote2} 1334 Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg: 1335 \begin{cfa} 1336 extern [ int ] f ( int ); 1337 static [ int ] g ( int ); 1338 \end{cfa} 1339 1340 1341 \section{Routine Pointers} 1342 1343 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 1344 \begin{cfa} 1345 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§ 1346 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§ 1347 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§ 1348 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§ 1349 \end{cfa} 1350 While parameter names are optional, \emph{a routine name cannot be specified}; 1351 for example, the following is incorrect: 1352 \begin{cfa} 1353 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§ 1354 \end{cfa} 1355 1356 1357 \section{Named and Default Arguments} 1358 1359 Named\index{named arguments}\index{arguments!named} and default\index{default arguments}\index{arguments!default} arguments~\cite{Hardgrave76}\footnote{ 1360 Francez~\cite{Francez77} proposed a further extension to the named-parameter passing style, which specifies what type of communication (by value, by reference, by name) the argument is passed to the routine.} 1361 are two mechanisms to simplify routine call. 1362 Both mechanisms are discussed with respect to \CFA. 1363 \begin{description} 1364 \item[Named (or Keyword) Arguments:] 1365 provide the ability to specify an argument to a routine call using the parameter name rather than the position of the parameter. 1366 For example, given the routine: 1367 \begin{cfa} 1368 void p( int x, int y, int z ) {...} 1369 \end{cfa} 1370 a positional call is: 1371 \begin{cfa} 1372 p( 4, 7, 3 ); 1373 \end{cfa} 1374 whereas a named (keyword) call may be: 1375 \begin{cfa} 1376 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§ 1377 \end{cfa} 1378 Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. 1379 The compiler rewrites a named call into a positional call. 1380 The advantages of named parameters are: 1381 \begin{itemize} 1382 \item 1383 Remembering the names of the parameters may be easier than the order in the routine definition. 1384 \item 1385 Parameter names provide documentation at the call site (assuming the names are descriptive). 1386 \item 1387 Changes can be made to the order or number of parameters without affecting the call (although the call must still be recompiled). 1388 \end{itemize} 1389 1390 Unfortunately, named arguments do not work in C-style programming-languages because a routine prototype is not required to specify parameter names, nor do the names in the prototype have to match with the actual definition. 1391 For example, the following routine prototypes and definition are all valid. 1392 \begin{cfa} 1393 void p( int, int, int ); §\C{// equivalent prototypes}§ 1394 void p( int x, int y, int z ); 1395 void p( int y, int x, int z ); 1396 void p( int z, int y, int x ); 1397 void p( int q, int r, int s ) {} §\C{// match with this definition}§ 1398 \end{cfa} 1399 Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. 1400 Alternatively, prototype definitions can be eliminated by using a two-pass compilation, and implicitly creating header files for exports. 1401 The former is easy to do, while the latter is more complex. 1402 1403 Furthermore, named arguments do not work well in a \CFA-style programming-languages because they potentially introduces a new criteria for type matching. 1404 For example, it is technically possible to disambiguate between these two overloaded definitions of ©f© based on named arguments at the call site: 1405 \begin{cfa} 1406 int f( int i, int j ); 1407 int f( int x, double y ); 1408 1409 f( j : 3, i : 4 ); §\C{// 1st f}§ 1410 f( x : 7, y : 8.1 ); §\C{// 2nd f}§ 1411 f( 4, 5 ); §\C{// ambiguous call}§ 1412 \end{cfa} 1413 However, named arguments compound routine resolution in conjunction with conversions: 1414 \begin{cfa} 1415 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§ 1416 \end{cfa} 1417 Depending on the cost associated with named arguments, this call could be resolvable or ambiguous. 1418 Adding named argument into the routine resolution algorithm does not seem worth the complexity. 1419 Therefore, \CFA does \emph{not} attempt to support named arguments. 1420 1421 \item[Default Arguments] 1422 provide the ability to associate a default value with a parameter so it can be optionally specified in the argument list. 1423 For example, given the routine: 1424 \begin{cfa} 1425 void p( int x = 1, int y = 2, int z = 3 ) {...} 1426 \end{cfa} 1427 the allowable positional calls are: 1428 \begin{cfa} 1429 p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 1430 p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 1431 p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 1432 p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§ 1433 // empty arguments 1434 p( , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§ 1435 p( 4, , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§ 1436 p( 4, 4, ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 1437 p( 4, , ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 1438 p( , 4, ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§ 1439 p( , , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§ 1440 p( , , ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 1441 \end{cfa} 1442 Here the missing arguments are inserted from the default values in the parameter list. 1443 The compiler rewrites missing default values into explicit positional arguments. 1444 The advantages of default values are: 1445 \begin{itemize} 1446 \item 1447 Routines with a large number of parameters are often very generalized, giving a programmer a number of different options on how a computation is performed. 1448 For many of these kinds of routines, there are standard or default settings that work for the majority of computations. 1449 Without default values for parameters, a programmer is forced to specify these common values all the time, resulting in long argument lists that are error prone. 1450 \item 1451 When a routine's interface is augmented with new parameters, it extends the interface providing generalizability\footnote{ 1452 ``It should be possible for the implementor of an abstraction to increase its generality. 1453 So long as the modified abstraction is a generalization of the original, existing uses of the abstraction will not require change. 1454 It might be possible to modify an abstraction in a manner which is not a generalization without affecting existing uses, but, without inspecting the modules in which the uses occur, this possibility cannot be determined. 1455 This criterion precludes the addition of parameters, unless these parameters have default or inferred values that are valid for all possible existing applications.''~\cite[p.~128]{Cormack90}} 1456 (somewhat like the generalization provided by inheritance for classes). 1457 That is, all existing calls are still valid, although the call must still be recompiled. 1458 \end{itemize} 1459 The only disadvantage of default arguments is that unintentional omission of an argument may not result in a compiler-time error. 1460 Instead, a default value is used, which may not be the programmer's intent. 1461 1462 Default values may only appear in a prototype versus definition context: 1463 \begin{cfa} 1464 void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§ 1465 void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§ 1466 void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§ 1467 \end{cfa} 1468 The reason for this restriction is to allow separate compilation. 1469 Multiple prototypes with different default values is an error. 1470 \end{description} 1471 1472 Ellipse (``...'') arguments present problems when used with default arguments. 1473 The conflict occurs because both named and ellipse arguments must appear after positional arguments, giving two possibilities: 1474 \begin{cfa} 1475 p( /* positional */, ... , /* named */ ); 1476 p( /* positional */, /* named */, ... ); 1477 \end{cfa} 1478 While it is possible to implement both approaches, the first possibly is more complex than the second, \eg: 1479 \begin{cfa} 1480 p( int x, int y, int z, ... ); 1481 p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§ 1482 p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§ 1483 \end{cfa} 1484 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. 1485 Hence, this approach seems significantly more difficult, and hence, confusing and error prone. 1486 In the second call, the named arguments separate the positional and ellipse arguments, making it trivial to read the call. 1487 1488 The problem is exacerbated with default arguments, \eg: 1489 \begin{cfa} 1490 void p( int x, int y = 2, int z = 3... ); 1491 p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§ 1492 p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§ 1493 \end{cfa} 1494 The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; 1495 therefore, argument 5 subsequently conflicts with the named argument z : 3. 1496 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. 1497 For these reasons, \CFA requires named arguments before ellipse arguments. 1498 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. 1499 1500 Default arguments and overloading (see Section 24) are complementary. 1501 While in theory default arguments can be simulated with overloading, as in: 1502 \begin{quote2} 1503 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1504 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{default arguments}} & \multicolumn{1}{c}{\textbf{overloading}} \\ 1505 \begin{cfa} 1506 void p( int x, int y = 2, int z = 3 ) {...} 1507 1508 1509 \end{cfa} 1510 & 1511 \begin{cfa} 1512 void p( int x, int y, int z ) {...} 1513 void p( int x ) { p( x, 2, 3 ); } 1514 void p( int x, int y ) { p( x, y, 3 ); } 1515 \end{cfa} 1516 \end{tabular} 1517 \end{quote2} 1518 the number of required overloaded routines is linear in the number of default values, which is unacceptable growth. 1519 In general, overloading should only be used over default arguments if the body of the routine is significantly different. 1520 Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: 1521 \begin{cfa} 1522 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§ 1523 \end{cfa} 1524 1525 Given the \CFA restrictions above, both named and default arguments are backwards compatible. 1526 \Index*[C++]{\CC{}} only supports default arguments; 1527 \Index*{Ada} supports both named and default arguments. 1528 1529 1530 \section{Unnamed Structure Fields} 1531 1532 C requires each field of a structure to have a name, except for a bit field associated with a basic type, \eg: 1533 \begin{cfa} 1534 struct { 1535 int f1; §\C{// named field}§ 1536 int f2 : 4; §\C{// named field with bit field size}§ 1537 int : 3; §\C{// unnamed field for basic type with bit field size}§ 1538 int ; §\C{// disallowed, unnamed field}§ 1539 int *; §\C{// disallowed, unnamed field}§ 1540 int (*)( int ); §\C{// disallowed, unnamed field}§ 1541 }; 1542 \end{cfa} 1543 This requirement is relaxed by making the field name optional for all field declarations; therefore, all the field declarations in the example are allowed. 1544 As for unnamed bit fields, an unnamed field is used for padding a structure to a particular size. 1545 A list of unnamed fields is also supported, \eg: 1546 \begin{cfa} 1547 struct { 1548 int , , ; §\C{// 3 unnamed fields}§ 1549 } 1550 \end{cfa} 1551 1552 1553 \section{Nesting} 1554 1555 Nesting of types and routines is useful for controlling name visibility (\newterm{name hiding}). 1556 1557 1558 \subsection{Type Nesting} 1559 1560 \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. 1561 \begin{figure} 1562 \centering 1563 \begin{tabular}{@{}l@{\hspace{3em}}l|l@{}} 1564 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{C Type Nesting}} & \multicolumn{1}{c}{\textbf{C Implicit Hoisting}} & \multicolumn{1}{|c}{\textbf{\CFA}} \\ 1565 \hline 1566 \begin{cfa} 1567 struct S { 1568 enum C { R, G, B }; 1569 struct T { 1570 union U { int i, j; }; 1571 enum C c; 1572 short int i, j; 1573 }; 1574 struct T t; 1575 } s; 1576 1577 int fred() { 1578 s.t.c = R; 1579 struct T t = { R, 1, 2 }; 1580 enum C c; 1581 union U u; 1582 } 1583 \end{cfa} 1584 & 1585 \begin{cfa} 1586 enum C { R, G, B }; 1587 union U { int i, j; }; 1588 struct T { 1589 enum C c; 1590 short int i, j; 1591 }; 1592 struct S { 1593 struct T t; 1594 } s; 1595 1596 1597 1598 1599 1600 1601 1602 \end{cfa} 1603 & 1604 \begin{cfa} 1605 struct S { 1606 enum C { R, G, B }; 1607 struct T { 1608 union U { int i, j; }; 1609 enum C c; 1610 short int i, j; 1611 }; 1612 struct T t; 1613 } s; 1614 1615 int fred() { 1616 s.t.c = ®S.®R; // type qualification 1617 struct ®S.®T t = { ®S.®R, 1, 2 }; 1618 enum ®S.®C c; 1619 union ®S.T.®U u; 1620 } 1621 \end{cfa} 1622 \end{tabular} 1623 \caption{Type Nesting / Qualification} 1624 \label{f:TypeNestingQualification} 1625 \end{figure} 1626 In the left example in C, types ©C©, ©U© and ©T© are implicitly hoisted outside of type ©S© into the containing block scope. 1627 In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``©.©'' for type qualification, as does \Index*{Java}, rather than the \CC type-selection operator ``©::©''. 1628 1629 1630 \subsection{Routine Nesting} 1631 1632 While \CFA does not provide object programming by putting routines into structures, it does rely heavily on locally nested routines to redefine operations at or close to a call site. 1633 For example, the C quick-sort is wrapped into the following polymorphic \CFA routine: 1634 \begin{cfa} 1635 forall( otype T | { int ?<?( T, T ); } ) 1636 void qsort( const T * arr, size_t dimension ); 1637 \end{cfa} 1638 which can be used to sort in ascending and descending order by locally redefining the less-than operator into greater-than. 1639 \begin{cfa} 1640 const unsigned int size = 5; 1641 int ia[size]; 1642 ... §\C{// assign values to array ia}§ 1643 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§ 1644 { 1645 ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§ 1646 qsort( ia, size ); §\C{// sort descending order by local redefinition}§ 1647 } 1648 \end{cfa} 1649 1650 Nested routines are not first-class, meaning a nested routine cannot be returned if it has references to variables in its enclosing blocks; 1651 the only exception is references to the external block of the translation unit, as these variables persist for the duration of the program. 1652 The following program in undefined in \CFA (and Indexc{gcc}) 1653 \begin{cfa} 1654 [* [int]( int )] foo() { §\C{// int (*foo())( int )}§ 1655 int ®i® = 7; 1656 int bar( int p ) { 1657 ®i® += 1; §\C{// dependent on local variable}§ 1658 sout | ®i® | endl; 1659 } 1660 return bar; §\C{// undefined because of local dependence}§ 1661 } 1662 int main() { 1663 * [int]( int ) fp = foo(); §\C{// int (*fp)( int )}§ 1664 sout | fp( 3 ) | endl; 1665 } 1666 \end{cfa} 1667 because 1668 1669 Currently, there are no \Index{lambda} expressions, \ie unnamed routines because routine names are very important to properly select the correct routine. 1670 1671 1672 \section{Tuples} 1673 1674 In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call. 1675 (More contexts are added shortly.) 1676 A list of such elements is called a \newterm{lexical list}. 1677 The general syntax of a lexical list is: 1678 \begin{cfa} 1679 [ §\emph{exprlist}§ ] 1680 \end{cfa} 1681 where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas. 1682 The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator. 1683 The following are examples of lexical lists: 1684 \begin{cfa} 1685 [ x, y, z ] 1686 [ 2 ] 1687 [ v+w, x*y, 3.14159, f() ] 1688 \end{cfa} 1689 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. 1690 Note, a tuple is not a record (structure); 1691 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1). 1692 In essence, tuples are largely a compile time phenomenon, having little or no runtime presence. 1693 1694 Tuples can be organized into compile-time tuple variables; 1695 these variables are of \newterm{tuple type}. 1696 Tuple variables and types can be used anywhere lists of conventional variables and types can be used. 1697 The general syntax of a tuple type is: 1698 \begin{cfa} 1699 [ §\emph{typelist}§ ] 1700 \end{cfa} 1701 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. 1702 Examples of tuple types include: 1703 \begin{cfa} 1704 [ unsigned int, char ] 1705 [ double, double, double ] 1706 [ * int, int * ] §\C{// mix of CFA and ANSI}§ 1707 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 1708 \end{cfa} 1709 Like tuples, tuple types may be nested, such as ©[ [ int, int ], int ]©, which is a 2-element tuple type whose first element is itself a tuple type. 1710 1711 Examples of declarations using tuple types are: 1712 \begin{cfa} 1713 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§ 1714 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§ 1715 [ [ int, int ] ] z ([ int, int ]); 1716 \end{cfa} 1717 The last example declares an external routine that expects a 2 element tuple as an input parameter and returns a 2 element tuple as its result. 1718 1719 As mentioned, tuples can appear in contexts requiring a list of value, such as an argument list of a routine call. 1720 In unambiguous situations, the tuple brackets may be omitted, \eg a tuple that appears as an argument may have its 1721 square brackets omitted for convenience; therefore, the following routine invocations are equivalent: 1722 \begin{cfa} 1723 f( [ 1, x+2, fred() ] ); 1724 f( 1, x+2, fred() ); 1725 \end{cfa} 1726 Also, a tuple or a tuple variable may be used to supply all or part of an argument list for a routine expecting multiple input parameters or for a routine expecting a tuple as an input parameter. 1727 For example, the following are all legal: 1728 \begin{cfa} 1729 [ int, int ] w1; 1730 [ int, int, int ] w2; 1731 [ void ] f (int, int, int); /* three input parameters of type int */ 1732 [ void ] g ([ int, int, int ]); /* 3 element tuple as input */ 1733 f( [ 1, 2, 3 ] ); 1734 f( w1, 3 ); 1735 f( 1, w1 ); 1736 f( w2 ); 1737 g( [ 1, 2, 3 ] ); 1738 g( w1, 3 ); 1739 g( 1, w1 ); 1740 g( w2 ); 1741 \end{cfa} 1742 Note, in all cases 3 arguments are supplied even though the syntax may appear to supply less than 3. As mentioned, a 1743 tuple does not have structure like a record; a tuple is simply converted into a list of components. 1744 \begin{rationale} 1745 The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; \ie a statement such as ©g( f() )© is not supported. 1746 Using a temporary variable to store the results of the inner routine and then passing this variable to the outer routine works, however. 1747 \end{rationale} 1748 1749 A tuple can contain a C comma expression, provided the expression containing the comma operator is enclosed in parentheses. 1750 For instance, the following tuples are equivalent: 1751 \begin{cfa} 1752 [ 1, 3, 5 ] 1753 [ 1, (2, 3), 5 ] 1754 \end{cfa} 1755 The second element of the second tuple is the expression (2, 3), which yields the result 3. 1756 This requirement is the same as for comma expressions in argument lists. 1757 1758 Type qualifiers, \ie const and volatile, may modify a tuple type. 1759 The meaning is the same as for a type qualifier modifying an aggregate type [Int99, x 6.5.2.3(7),x 6.7.3(11)], \ie the qualifier is distributed across all of the types in the tuple, \eg: 1760 \begin{cfa} 1761 const volatile [ int, float, const int ] x; 1762 \end{cfa} 1763 is equivalent to: 1764 \begin{cfa} 1765 [ const volatile int, const volatile float, const volatile int ] x; 1766 \end{cfa} 1767 Declaration qualifiers can only appear at the start of a \CFA tuple declaration4, \eg: 1768 \begin{cfa} 1769 extern [ int, int ] w1; 1770 static [ int, int, int ] w2; 1771 \end{cfa} 1772 \begin{rationale} 1773 Unfortunately, C's syntax for subscripts precluded treating them as tuples. 1774 The C subscript list has the form ©[i][j]...© and not ©[i, j, ...]©. 1775 Therefore, there is no syntactic way for a routine returning multiple values to specify the different subscript values, \eg ©f[g()]© always means a single subscript value because there is only one set of brackets. 1776 Fixing this requires a major change to C because the syntactic form ©M[i, j, k]© already has a particular meaning: ©i, j, k© is a comma expression. 1777 \end{rationale} 1778 1779 1780 \subsection{Tuple Coercions} 1781 1782 There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring. 1783 In addition, the coercion of dereferencing can be performed on a tuple variable to yield its value(s), as for other variables. 1784 A \newterm{closing coercion} takes a set of values and converts it into a tuple value, which is a contiguous set of values, as in: 1785 \begin{cfa} 1786 [ int, int, int, int ] w; 1787 w = [ 1, 2, 3, 4 ]; 1788 \end{cfa} 1789 First the right-hand tuple is closed into a tuple value and then the tuple value is assigned. 1790 1791 An \newterm{opening coercion} is the opposite of closing; a tuple value is converted into a tuple of values, as in: 1792 \begin{cfa} 1793 [ a, b, c, d ] = w 1794 \end{cfa} 1795 ©w© is implicitly opened to yield a tuple of four values, which are then assigned individually. 1796 1797 A \newterm{flattening coercion} coerces a nested tuple, \ie a tuple with one or more components, which are themselves tuples, into a flattened tuple, which is a tuple whose components are not tuples, as in: 1798 \begin{cfa} 1799 [ a, b, c, d ] = [ 1, [ 2, 3 ], 4 ]; 1800 \end{cfa} 1801 First the right-hand tuple is flattened and then the values are assigned individually. 1802 Flattening is also performed on tuple types. 1803 For example, the type ©[ int, [ int, int ], int ]© can be coerced, using flattening, into the type ©[ int, int, int, int ]©. 1804 1805 A \newterm{structuring coercion} is the opposite of flattening; 1806 a tuple is structured into a more complex nested tuple. 1807 For example, structuring the tuple ©[ 1, 2, 3, 4 ]© into the tuple ©[ 1, [ 2, 3 ], 4 ]© or the tuple type ©[ int, int, int, int ]© into the tuple type ©[ int, [ int, int ], int ]©. 1808 In the following example, the last assignment illustrates all the tuple coercions: 1809 \begin{cfa} 1810 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 1811 int x = 5; 1812 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§ 1813 \end{cfa} 1814 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; 1815 therefore, the right-hand tuple is now the tuple ©[ [ 1, 2, 3, 4 ], 5 ]©. 1816 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. 1817 The tuple ©[ 2, 3, 4, 5 ]© is then closed to create a tuple value. 1818 Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using multiple assignment (see Section 14). 1819 \begin{rationale} 1820 A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple. 1821 \end{rationale} 1822 1823 1824 \section{Mass Assignment} 1825 1826 \CFA permits assignment to several variables at once using mass assignment~\cite{CLU}. 1827 Mass assignment has the following form: 1828 \begin{cfa} 1829 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§; 1830 \end{cfa} 1831 \index{lvalue} 1832 The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement. 1833 ©$\emph{expr}$© is any standard arithmetic expression. 1834 Clearly, the types of the entities being assigned must be type compatible with the value of the expression. 1835 1836 Mass assignment has parallel semantics, \eg the statement: 1837 \begin{cfa} 1838 [ x, y, z ] = 1.5; 1839 \end{cfa} 1840 is equivalent to: 1841 \begin{cfa} 1842 x = 1.5; y = 1.5; z = 1.5; 1843 \end{cfa} 1844 This semantics is not the same as the following in C: 1845 \begin{cfa} 1846 x = y = z = 1.5; 1847 \end{cfa} 1848 as conversions between intermediate assignments may lose information. 1849 A more complex example is: 1850 \begin{cfa} 1851 [ i, y[i], z ] = a + b; 1852 \end{cfa} 1853 which is equivalent to: 1854 \begin{cfa} 1855 t = a + b; 1856 a1 = &i; a2 = &y[i]; a3 = &z; 1857 *a1 = t; *a2 = t; *a3 = t; 1858 \end{cfa} 1859 The temporary ©t© is necessary to store the value of the expression to eliminate conversion issues. 1860 The temporaries for the addresses are needed so that locations on the left-hand side do not change as the values are assigned. 1861 In this case, ©y[i]© uses the previous value of ©i© and not the new value set at the beginning of the mass assignment. 1862 1863 1864 \section{Multiple Assignment} 1865 1866 \CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}. 1867 Multiple assignment has the following form: 1868 \begin{cfa} 1869 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ]; 1870 \end{cfa} 1871 \index{lvalue} 1872 The left-hand side is a tuple of \emph{lvalues}, and the right-hand side is a tuple of \emph{expr}s. 1873 Each \emph{expr} appearing on the right-hand side of a multiple assignment statement is assigned to the corresponding \emph{lvalues} on the left-hand side of the statement using parallel semantics for each assignment. 1874 An example of multiple assignment is: 1875 \begin{cfa} 1876 [ x, y, z ] = [ 1, 2, 3 ]; 1877 \end{cfa} 1878 Here, the values ©1©, ©2© and ©3© are assigned, respectively, to the variables ©x©, ©y© and ©z©. 1879 A more complex example is: 1880 \begin{cfa} 1881 [ i, y[ i ], z ] = [ 1, i, a + b ]; 1882 \end{cfa} 1883 Here, the values ©1©, ©i© and ©a + b© are assigned to the variables ©i©, ©y[i]© and ©z©, respectively. 1884 Note, the parallel semantics of 1885 multiple assignment ensures: 1886 \begin{cfa} 1887 [ x, y ] = [ y, x ]; 1888 \end{cfa} 1889 correctly interchanges (swaps) the values stored in ©x© and ©y©. 1890 The following cases are errors: 1891 \begin{cfa} 1892 [ a, b, c ] = [ 1, 2, 3, 4 ]; 1893 [ a, b, c ] = [ 1, 2 ]; 1894 \end{cfa} 1895 because the number of entities in the left-hand tuple is unequal with the right-hand tuple. 1896 1897 As for all tuple contexts in C, side effects should not be used because C does not define an ordering for the evaluation of the elements of a tuple; 1898 both these examples produce indeterminate results: 1899 \begin{cfa} 1900 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§ 1901 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§ 1902 \end{cfa} 1903 1904 1905 \section{Cascade Assignment} 1906 1907 As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment. 1908 Cascade assignment has the following form: 1909 \begin{cfa} 1910 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§; 1911 \end{cfa} 1912 and it has the same parallel semantics as for mass and multiple assignment. 1913 Some examples of cascade assignment are: 1914 \begin{cfa} 1915 x1 = y1 = x2 = y2 = 0; 1916 [ x1, y1 ] = [ x2, y2 ] = [ x3, y3 ]; 1917 [ x1, y1 ] = [ x2, y2 ] = 0; 1918 [ x1, y1 ] = z = 0; 1919 \end{cfa} 1920 As in C, the rightmost assignment is performed first, \ie assignment parses right to left. 1921 1922 1923 \section{Field Tuples} 1924 1925 Tuples may be used to select multiple fields of a record by field name. 1926 Its general form is: 1927 \begin{cfa} 1928 §\emph{expr}§ . [ §\emph{fieldlist}§ ] 1929 §\emph{expr}§ -> [ §\emph{fieldlist}§ ] 1930 \end{cfa} 1931 \emph{expr} is any expression yielding a value of type record, \eg ©struct©, ©union©. 1932 Each element of \emph{ fieldlist} is an element of the record specified by \emph{expr}. 1933 A record-field tuple may be used anywhere a tuple can be used. An example of the use of a record-field tuple is 1934 the following: 1935 \begin{cfa} 1936 struct s { 1937 int f1, f2; 1938 char f3; 1939 double f4; 1940 } v; 1941 v.[ f3, f1, f2 ] = ['x', 11, 17 ]; §\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}§ 1942 f( v.[ f3, f1, f2 ] ); §\C{// equivalent to f( v.f3, v.f1, v.f2 )}§ 1943 \end{cfa} 1944 Note, the fields appearing in a record-field tuple may be specified in any order; 1945 also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. 1946 1947 If a field of a ©struct© is itself another ©struct©, multiple fields of this subrecord can be specified using a nested record-field tuple, as in the following example: 1948 \begin{cfa} 1949 struct inner { 1950 int f2, f3; 1951 }; 1952 struct outer { 1953 int f1; 1954 struct inner i; 1955 double f4; 1956 } o; 1957 1958 o.[ f1, i.[ f2, f3 ], f4 ] = [ 11, 12, 13, 3.14159 ]; 1959 \end{cfa} 1960 1961 1962 \section{Labelled Continue/Break} 501 \section{Exponentiation Operator} 502 503 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}, to perform the exponentiation operation. 504 \CFA extends the basic operators with the exponentiation operator ©?\?©\index{?\\?@\lstinline$?\?$} and ©?\=?©\index{?\\=?@\lstinline$?\=?$}, as in, ©x \ y© and ©x \= y©, which means $x^y$ and $x \leftarrow x^y$. 505 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)©. 506 507 As for \Index{division}, there are exponentiation operators for integral and floating-point types, including the builtin \Index{complex} types. 508 Unsigned integral exponentiation\index{exponentiation!unsigned integral} is performed with repeated multiplication (or shifting if the base is 2). 509 Signed integral exponentiation\index{exponentiation!signed integral} is performed with repeated multiplication (or shifting if the base is 2), but yields a floating-point result because $b^{-e}=1/b^e$. 510 Hence, it is important to designate exponent integral-constants as unsigned or signed: ©3 \ 3u© return an integral result, while ©3 \ 3© returns a floating-point result. 511 Floating-point exponentiation\index{exponentiation!floating point} is performed using \Index{logarithm}s\index{exponentiation!logarithm}, so the base cannot be negative. 512 \begin{cfa} 513 sout | 2 ®\® 8u | 4 ®\® 3u | -4 ®\® 3u | 4 ®\® -3 | -4 ®\® -3 | 4.0 ®\® 2.1 | (1.0f+2.0fi) ®\® (3.0f+2.0fi) | endl; 514 256 64 -64 0.015625 -0.015625 18.3791736799526 0.264715-1.1922i 515 \end{cfa} 516 Parenthesis are necessary for the complex constants or the expresion is parsed as ©1.0f+(2.0fi \ 3.0f)+2.0fi©. 517 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral version is available, if the user type defines multiplication, ©*©, and one, ©1©. 518 519 520 \section{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break}}{Labelled continue / break}} 1963 521 1964 522 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. 1965 523 Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 1966 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@\lstinline $continue$!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@\lstinline $break$!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85 ,Java}.524 To prevent having to switch to the ©goto©, \CFA extends the \Indexc{continue}\index{continue@\lstinline $continue$!labelled}\index{labelled!continue@©continue©} and \Indexc{break}\index{break@\lstinline $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}. 1967 525 For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement; 1968 526 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement. 1969 527 \VRef[Figure]{f:MultiLevelResumeTermination} shows the labelled ©continue© and ©break©, specifying which control structure is the target for exit, and the corresponding C program using only ©goto©. 1970 528 The innermost loop has 7 exit points, which cause resumption or termination of one or more of the 7 \Index{nested control-structure}s. 529 Java supports both labelled ©continue© and ©break© statements. 1971 530 1972 531 \begin{figure} … … 2091 650 2092 651 2093 \section{ Switch Statement}652 \section{\texorpdfstring{\LstKeywordStyle{switch} Statement}{switch Statement}} 2094 653 2095 654 C allows a number of questionable forms for the ©switch© statement: … … 2132 691 ®// open input file 2133 692 ®} else if ( argc == 2 ) { 2134 ®// open input file 693 ®// open input file (duplicate) 2135 694 2136 695 ®} else { … … 2145 704 \begin{cfa} 2146 705 switch ( i ) { 2147 case 1: case 3: case 5:// odd values2148 // sameaction706 ®case 1: case 3: case 5:® // odd values 707 // odd action 2149 708 break; 2150 case 2: case 4: case 6:// even values2151 // sameaction709 ®case 2: case 4: case 6:® // even values 710 // even action 2152 711 break; 2153 712 } … … 2155 714 However, this situation is handled in other languages without fall-through by allowing a list of case values. 2156 715 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. 2157 Hence, default fall-through semantics results in a large number of programming errors as programmers often forgetthe ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through.716 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. 2158 717 2159 718 \item … … 2239 798 and there is only a medium amount of fall-through from one ©case© clause to the next, and most of these result from a list of case values executing common code, rather than a sequence of case actions that compound. 2240 799 \end{itemize} 2241 These observations help to put the \CFA changes to the ©switch© into perspective.800 These observations put into perspective the \CFA changes to the ©switch©. 2242 801 \begin{enumerate} 2243 802 \item … … 2249 808 still works. 2250 809 Nevertheless, reversing the default action would have a non-trivial effect on case actions that compound, such as the above example of processing shell arguments. 2251 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©, e.g.:810 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: 2252 811 \begin{cfa} 2253 812 ®choose® ( i ) { … … 2260 819 case 7: 2261 820 ... 2262 ®break® §\C{// explicit end of switch}§821 ®break® §\C{// redundant explicit end of switch}§ 2263 822 default: 2264 823 j = 3; … … 2266 825 \end{cfa} 2267 826 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses; 2268 theimplicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause.2269 Theexplicit ©fallthru© is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the ©choose© statement.827 An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. 828 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. 2270 829 As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers. 2271 830 \item … … 2296 855 2297 856 2298 \section{ Case Clause}857 \section{\texorpdfstring{\LstKeywordStyle{case} Clause}{case Clause}} 2299 858 2300 859 C restricts the ©case© clause of a ©switch© statement to a single value. … … 2372 931 2373 932 933 \section{\texorpdfstring{\LstKeywordStyle{with} Clause / Statement}{with Clause / Statement}} 934 \label{s:WithClauseStatement} 935 936 In \Index{object-oriented} programming, there is an implicit first parameter, often names \textbf{©self©} or \textbf{©this©}, which is elided. 937 \begin{C++} 938 class C { 939 int i, j; 940 int mem() { ®// implicit "this" parameter 941 ® i = 1; ®// this->i 942 ® j = 3; ®// this->j 943 ® } 944 } 945 \end{C++} 946 Since CFA is non-object-oriented, the equivalent object-oriented program looks like: 947 \begin{cfa} 948 struct S { int i, j; }; 949 int mem( S &this ) { // explicit "this" parameter 950 ®this.®i = 1; // "this" is not elided 951 ®this.®j = 2; 952 } 953 \end{cfa} 954 but it is cumbersome having to write "©this.©" many times in a member. 955 956 \CFA provides a ©with© clause/statement to elided the "©this.©" by opening a scope containing field identifiers and changing the qualified fields into variables, giving an opportunity for optimizing qualified references.\footnote{ 957 The ©with© statement comes from Pascal~\cite[\S~4.F]{Pascal}.} 958 \begin{cfa} 959 int mem( S &this ) ®with this® { 960 i = 1; ®// this.i 961 ® j = 2; ®// this.j 962 ®} 963 \end{cfa} 964 which extends to multiple routine parameters: 965 \begin{cfa} 966 struct T { double m, n; }; 967 int mem2( S &this1, T &this2 ) ®with this1, this2® { 968 i = 1; j = 2; 969 m = 1.0; n = 2.0; 970 } 971 \end{cfa} 972 973 The statement form is used within a block: 974 \begin{cfa} 975 int foo() { 976 struct S1 { ... } s1; 977 struct S2 { ... } s2; 978 ®with s1® { 979 // access fields of s1 without qualification 980 ®with s2® { // nesting 981 // access fields of s1 and s2 without qualification 982 } 983 } 984 ®with s1, s2® { 985 // access unambiguous fields of s1 and s2 without qualification 986 } 987 } 988 \end{cfa} 989 990 When opening multiple structures, fields with the same name and type are ambiguous and must be fully qualified. 991 For fields with the same name but different type, context/cast can be used to disambiguate. 992 \begin{cfa} 993 struct S { int i; int j; double m; } a, c; 994 struct T { int i; int k; int m } b, c; 995 ®with a, b® { 996 j + k; §\C{// unambiguous, unique names define unique type}§ 997 i; §\C{// ambiguous, same name and type}§ 998 a.i + b.i; §\C{// unambiguous, qualification defines unique type}§ 999 m; §\C{// ambiguous, no context to define unique type}§ 1000 m = 5.0; §\C{// unambiguous, context defines unique type}§ 1001 m = 1; §\C{// unambiguous, context defines unique type}§ 1002 } 1003 ®with c® { ... } §\C{// ambiguous, no context}§ 1004 ®with (S)c® { ... } §\C{// unambiguous, cast defines unique type}§ 1005 \end{cfa} 1006 1007 2374 1008 \section{Exception Handling} 1009 \label{s:ExceptionHandling} 2375 1010 2376 1011 Exception handling provides two mechanism: change of control flow from a raise to a handler, and communication from the raise to the handler. 2377 \begin{cfa} 2378 exception void h( int i ); 2379 exception int h( int i, double d ); 2380 1012 Transfer of control can be local, within a routine, or non-local, among routines. 1013 Non-local transfer can cause stack unwinding, i.e., non-local routine termination, depending on the kind of raise. 1014 \begin{cfa} 1015 exception_t E {}; §\C{// exception type}§ 2381 1016 void f(...) { 2382 ... throw h( 3 ); 2383 ... i = resume h( 3, 5.1 ); 2384 } 2385 1017 ... throw E{}; ... §\C{// termination}§ 1018 ... throwResume E{}; ... §\C{// resumption}§ 1019 } 2386 1020 try { 2387 1021 f(...); 2388 } catch h( int w ) {2389 // re set2390 } resume h( int p, double x ) {2391 return 17; // recover1022 } catch( E e : §boolean-predicate§ ) { §\C{// termination handler}§ 1023 // recover and continue 1024 } catchResume( E e : §boolean-predicate§ ) { §\C{// resumption handler}§ 1025 // repair and return 2392 1026 } finally { 2393 } 2394 \end{cfa} 2395 So the type raised would be the mangled name of the exception prototype and that name would be matched at the handler clauses by comparing the strings. 2396 The arguments for the call would have to be packed in a message and unpacked at handler clause and then a call made to the handler. 1027 // always executed 1028 } 1029 \end{cfa} 1030 The kind of raise and handler match: ©throw© with ©catch© and @throwResume@ with @catchResume@. 1031 Then the exception type must match along with any additonal predicate must be true. 1032 The ©catch© and ©catchResume© handlers may appear in any oder. 1033 However, the ©finally© clause must 1034 1035 1036 \subsection{Exception Hierarchy} 1037 1038 An exception type can be derived from another exception type, just like deriving a subclass from a class, providing a kind of polymorphism among exception types. 1039 The exception-type hierarchy that is created is used to organize exception types, similar to a class hierarchy in object-oriented languages, \eg: 1040 \begin{center} 1041 \input{EHMHierarchy} 1042 \end{center} 1043 A programmer can then choose to handle an exception at different degrees of specificity along the hierarchy; 1044 derived exception-types support a more flexible programming style. 1045 For example, higher-level code should catch general exceptions to reduce coupling to the specific implementation at the lower levels; 1046 unnecessary coupling may force changes in higher-level code when low-level code changes. 1047 A consequence of derived exception-types is that multiple exceptions may match, \eg: 1048 \begin{cfa} 1049 catch( Arithmetic ) 1050 \end{cfa} 1051 matches all three derived exception-types: ©DivideByZero©, ©Overflow©, and ©Underflow©. 1052 Because the propagation mechanisms perform a simple linear search of the handler clause for a guarded block, and selects the first matching handler, the order of catch clauses in the handler clause becomes important, \eg: 1053 \begin{cfa} 1054 try { 1055 ... 1056 } catch( Overflow ) { // must appear first 1057 // handle overflow 1058 } catch( Arithmetic ) 1059 // handle other arithmetic issues 1060 } 1061 \end{cfa} 1062 \newterm{Multiple derivation} among exception is not supported. 1063 1064 1065 \section{Declarations} 1066 \label{s:Declarations} 1067 1068 C declaration syntax is notoriously confusing and error prone. 1069 For example, many C programmers are confused by a declaration as simple as: 1070 \begin{quote2} 1071 \begin{tabular}{@{}ll@{}} 1072 \begin{cfa} 1073 int * x[5] 1074 \end{cfa} 1075 & 1076 \raisebox{-0.75\totalheight}{\input{Cdecl}} 1077 \end{tabular} 1078 \end{quote2} 1079 Is this an array of 5 pointers to integers or a \Index{pointer} to an array of 5 integers? 1080 The fact this declaration is unclear to many C programmers means there are \Index{productivity} and \Index{safety} issues even for basic programs. 1081 Another example of confusion results from the fact that a routine name and its parameters are embedded within the return type, mimicking the way the return value is used at the routine's call site. 1082 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 1083 \begin{cfa} 1084 int ®(*®f®())[®5®]® {...}; §\C{definition}§ 1085 ... ®(*®f®())[®3®]® += 1; §\C{usage}§ 1086 \end{cfa} 1087 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). 1088 While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice. 1089 1090 \CFA provides its own type, variable and routine declarations, using a different syntax. 1091 The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type. 1092 In the following example, \R{red} is the base type and \B{blue} is qualifiers. 1093 The \CFA declarations move the qualifiers to the left of the base type, \ie move the blue to the left of the red, while the qualifiers have the same meaning but are ordered left to right to specify a variable's type. 1094 \begin{quote2} 1095 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1096 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1097 \begin{cfa} 1098 ß[5] *ß ®int® x1; 1099 ß* [5]ß ®int® x2; 1100 ß[* [5] int]ß f®( int p )®; 1101 \end{cfa} 1102 & 1103 \begin{cfa} 1104 ®int® ß*ß x1 ß[5]ß; 1105 ®int® ß(*ßx2ß)[5]ß; 1106 ßint (*ßf®( int p )®ß)[5]ß; 1107 \end{cfa} 1108 \end{tabular} 1109 \end{quote2} 1110 The only exception is \Index{bit field} specification, which always appear to the right of the base type. 1111 % Specifically, the character ©*© is used to indicate a pointer, square brackets ©[©\,©]© are used to represent an array or function return value, and parentheses ©()© are used to indicate a routine parameter. 1112 However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list. 1113 For instance, variables ©x© and ©y© of type \Index{pointer} to integer are defined in \CFA as follows: 1114 \begin{quote2} 1115 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1116 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1117 \begin{cfa} 1118 ®*® int x, y; 1119 \end{cfa} 1120 & 1121 \begin{cfa} 1122 int ®*®x, ®*®y; 1123 \end{cfa} 1124 \end{tabular} 1125 \end{quote2} 1126 The downside of this semantics is the need to separate regular and \Index{pointer} declarations: 1127 \begin{quote2} 1128 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1129 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1130 \begin{cfa} 1131 ®*® int x; 1132 int y; 1133 \end{cfa} 1134 & 1135 \begin{cfa} 1136 int ®*®x, y; 1137 1138 \end{cfa} 1139 \end{tabular} 1140 \end{quote2} 1141 which is \Index{prescribing} a safety benefit. 1142 Other examples are: 1143 \begin{quote2} 1144 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 1145 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 1146 \begin{cfa} 1147 [ 5 ] int z; 1148 [ 5 ] * char w; 1149 * [ 5 ] double v; 1150 struct s { 1151 int f0:3; 1152 * int f1; 1153 [ 5 ] * int f2; 1154 }; 1155 \end{cfa} 1156 & 1157 \begin{cfa} 1158 int z[ 5 ]; 1159 char * w[ 5 ]; 1160 double (* v)[ 5 ]; 1161 struct s { 1162 int f0:3; 1163 int * f1; 1164 int * f2[ 5 ] 1165 }; 1166 \end{cfa} 1167 & 1168 \begin{cfa} 1169 // array of 5 integers 1170 // array of 5 pointers to char 1171 // pointer to array of 5 doubles 1172 1173 // common bit field syntax 1174 1175 1176 1177 \end{cfa} 1178 \end{tabular} 1179 \end{quote2} 1180 1181 All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg: 1182 \begin{quote2} 1183 \begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}} 1184 \multicolumn{1}{c@{\hspace{1em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{1em}}}{\textbf{C}} \\ 1185 \begin{cfa} 1186 const * const int x; 1187 const * [ 5 ] const int y; 1188 \end{cfa} 1189 & 1190 \begin{cfa} 1191 int const * const x; 1192 const int (* const y)[ 5 ] 1193 \end{cfa} 1194 & 1195 \begin{cfa} 1196 // const pointer to const integer 1197 // const pointer to array of 5 const integers 1198 \end{cfa} 1199 \end{tabular} 1200 \end{quote2} 1201 All declaration qualifiers, \eg ©extern©, ©static©, etc., are used in the normal way with the new declarations but can only appear at the start of a \CFA routine declaration,\footnote{\label{StorageClassSpecifier} 1202 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.~\cite[\S~6.11.5(1)]{C11}} \eg: 1203 \begin{quote2} 1204 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 1205 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 1206 \begin{cfa} 1207 extern [ 5 ] int x; 1208 static * const int y; 1209 \end{cfa} 1210 & 1211 \begin{cfa} 1212 int extern x[ 5 ]; 1213 const int static * y; 1214 \end{cfa} 1215 & 1216 \begin{cfa} 1217 // externally visible array of 5 integers 1218 // internally visible pointer to constant int 1219 \end{cfa} 1220 \end{tabular} 1221 \end{quote2} 1222 1223 The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine ©sizeof©: 1224 \begin{quote2} 1225 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1226 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1227 \begin{cfa} 1228 y = (®* int®)x; 1229 i = sizeof(®[ 5 ] * int®); 1230 \end{cfa} 1231 & 1232 \begin{cfa} 1233 y = (®int *®)x; 1234 i = sizeof(®int * [ 5 ]®); 1235 \end{cfa} 1236 \end{tabular} 1237 \end{quote2} 1238 1239 Finally, new \CFA declarations may appear together with C declarations in the same program block, but cannot be mixed within a specific declaration. 1240 Therefore, a programmer has the option of either continuing to use traditional C declarations or take advantage of the new style. 1241 Clearly, both styles need to be supported for some time due to existing C-style header-files, particularly for UNIX systems. 1242 1243 1244 \section{Pointer / Reference} 1245 1246 C provides a \newterm{pointer type}; 1247 \CFA adds a \newterm{reference type}. 1248 These types may be derived from an object or routine type, called the \newterm{referenced type}. 1249 Objects of these types contain an \newterm{address}, which is normally a location in memory, but may also address memory-mapped registers in hardware devices. 1250 An integer constant expression with the value 0, or such an expression cast to type ©void *©, is called a \newterm{null-pointer constant}.\footnote{ 1251 One way to conceptualize the null pointer is that no variable is placed at this address, so the null-pointer address can be used to denote an uninitialized pointer/reference object; 1252 \ie the null pointer is guaranteed to compare unequal to a pointer to any object or routine.} 1253 An address is \newterm{sound}, if it points to a valid memory location in scope, \ie within the program's execution-environment and has not been freed. 1254 Dereferencing an \newterm{unsound} address, including the null pointer, is \Index{undefined}, often resulting in a \Index{memory fault}. 1255 1256 A program \newterm{object} is a region of data storage in the execution environment, the contents of which can represent values. 1257 In most cases, objects are located in memory at an address, and the variable name for an object is an implicit address to the object generated by the compiler and automatically dereferenced, as in: 1258 \begin{quote2} 1259 \begin{tabular}{@{}ll@{\hspace{2em}}l@{}} 1260 \begin{cfa} 1261 int x; 1262 x = 3; 1263 int y; 1264 y = x; 1265 \end{cfa} 1266 & 1267 \raisebox{-0.45\totalheight}{\input{pointer1}} 1268 & 1269 \begin{cfa} 1270 int * ®const® x = (int *)100 1271 *x = 3; // implicit dereference 1272 int * ®const® y = (int *)104; 1273 *y = *x; // implicit dereference 1274 \end{cfa} 1275 \end{tabular} 1276 \end{quote2} 1277 where the right example is how the compiler logically interprets the variables in the left example. 1278 Since a variable name only points to one address during its lifetime, it is an \Index{immutable} \Index{pointer}; 1279 hence, the implicit type of pointer variables ©x© and ©y© are constant pointers in the compiler interpretation. 1280 In general, variable addresses are stored in instructions instead of loaded from memory, and hence may not occupy storage. 1281 These approaches are contrasted in the following: 1282 \begin{quote2} 1283 \begin{tabular}{@{}l|l@{}} 1284 \multicolumn{1}{c|}{explicit variable address} & \multicolumn{1}{c}{implicit variable address} \\ 1285 \hline 1286 \begin{cfa} 1287 lda r1,100 // load address of x 1288 ld r2,(r1) // load value of x 1289 lda r3,104 // load address of y 1290 st r2,(r3) // store x into y 1291 \end{cfa} 1292 & 1293 \begin{cfa} 1294 1295 ld r2,(100) // load value of x 1296 1297 st r2,(104) // store x into y 1298 \end{cfa} 1299 \end{tabular} 1300 \end{quote2} 1301 Finally, the immutable nature of a variable's address and the fact that there is no storage for the variable pointer means pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible. 1302 Therefore, the expression ©x = y© has only one meaning, ©*x = *y©, \ie manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of \Index{instruction decoding}. 1303 1304 A \Index{pointer}/\Index{reference} object is a generalization of an object variable-name, \ie a mutable address that can point to more than one memory location during its lifetime. 1305 (Similarly, an integer variable can contain multiple integer literals during its lifetime versus an integer constant representing a single literal during its lifetime, and like a variable name, may not occupy storage if the literal is embedded directly into instructions.) 1306 Hence, a pointer occupies memory to store its current address, and the pointer's value is loaded by dereferencing, \eg: 1307 \begin{quote2} 1308 \begin{tabular}{@{}l@{\hspace{2em}}l@{}} 1309 \begin{cfa} 1310 int x, y, ®*® p1, ®*® p2, ®**® p3; 1311 p1 = ®&®x; // p1 points to x 1312 p2 = p1; // p2 points to x 1313 p1 = ®&®y; // p1 points to y 1314 p3 = &p2; // p3 points to p2 1315 \end{cfa} 1316 & 1317 \raisebox{-0.5\totalheight}{\input{pointer2.pstex_t}} 1318 \end{tabular} 1319 \end{quote2} 1320 1321 Notice, an address has a \Index{duality}\index{address!duality}: a location in memory or the value at that location. 1322 In many cases, a compiler might be able to infer the best meaning for these two cases. 1323 For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage 1324 \begin{cfa} 1325 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§ 1326 \end{cfa} 1327 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. 1328 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 1329 1330 Rather than inferring dereference, most programming languages pick one implicit dereferencing semantics, and the programmer explicitly indicates the other to resolve address-duality. 1331 In C, objects of pointer type always manipulate the pointer object's address: 1332 \begin{cfa} 1333 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§ 1334 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§ 1335 \end{cfa} 1336 even though the assignment to ©p2© is likely incorrect, and the programmer probably meant: 1337 \begin{cfa} 1338 p1 = p2; §\C{// pointer address assignment}§ 1339 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§ 1340 \end{cfa} 1341 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©). 1342 1343 However, in most other situations, the pointed-to value is requested more often than the pointer address. 1344 \begin{cfa} 1345 *p2 = ((*p1 + *p2) * (**p3 - *p1)) / (**p3 - 15); 1346 \end{cfa} 1347 In this case, it is tedious to explicitly write the dereferencing, and error prone when pointer arithmetic is allowed. 1348 It is better to have the compiler generate the dereferencing and have no implicit pointer arithmetic: 1349 \begin{cfa} 1350 p2 = ((p1 + p2) * (p3 - p1)) / (p3 - 15); 1351 \end{cfa} 1352 1353 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). 1354 \begin{cfa} 1355 int x, y, ®&® r1, ®&® r2, ®&&® r3; 1356 ®&®r1 = &x; §\C{// r1 points to x}§ 1357 ®&®r2 = &r1; §\C{// r2 points to x}§ 1358 ®&®r1 = &y; §\C{// r1 points to y}§ 1359 ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§ 1360 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§ 1361 \end{cfa} 1362 Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example. 1363 Hence, a reference behaves like the variable name for the current variable it is pointing-to. 1364 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: 1365 \begin{cfa} 1366 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15); 1367 \end{cfa} 1368 When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out. 1369 However, in C, the cancellation always yields a value (\Index{rvalue}).\footnote{ 1370 The unary ©&© operator yields the address of its operand. 1371 If the operand has type ``type'', the result has type ``pointer to type''. 1372 If the operand is the result of a unary ©*© operator, neither that operator nor the ©&© operator is evaluated and the result is as if both were omitted, except that the constraints on the operators still apply and the result is not an lvalue.~\cite[\S~6.5.3.2--3]{C11}} 1373 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 1374 \begin{cfa} 1375 (&®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§ 1376 \end{cfa} 1377 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 1378 \begin{cfa} 1379 (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§ 1380 \end{cfa} 1381 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. 1382 1383 Fundamentally, pointer and reference objects are functionally interchangeable because both contain addresses. 1384 \begin{cfa} 1385 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 1386 &r1 = x, &&r2 = r1, &&&r3 = r2; 1387 ***p3 = 3; §\C{// change x}§ 1388 r3 = 3; §\C{// change x, ***r3}§ 1389 **p3 = ...; §\C{// change p1}§ 1390 &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§ 1391 *p3 = ...; §\C{// change p2}§ 1392 &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§ 1393 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§ 1394 \end{cfa} 1395 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types. 1396 Therefore, the choice between them is based solely on whether the address is dereferenced frequently or infrequently, which dictates the amount of implicit dereferencing aid from the compiler. 1397 1398 As for a pointer type, a reference type may have qualifiers: 1399 \begin{cfa} 1400 const int cx = 5; §\C{// cannot change cx;}§ 1401 const int & cr = cx; §\C{// cannot change what cr points to}§ 1402 ®&®cr = &cx; §\C{// can change cr}§ 1403 cr = 7; §\C{// error, cannot change cx}§ 1404 int & const rc = x; §\C{// must be initialized}§ 1405 ®&®rc = &x; §\C{// error, cannot change rc}§ 1406 const int & const crc = cx; §\C{// must be initialized}§ 1407 crc = 7; §\C{// error, cannot change cx}§ 1408 ®&®crc = &cx; §\C{// error, cannot change crc}§ 1409 \end{cfa} 1410 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}: 1411 \begin{cfa} 1412 int & const cr = *0; §\C{// where 0 is the int * zero}§ 1413 \end{cfa} 1414 Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management: 1415 \begin{cfa} 1416 int & const cr = *malloc(); 1417 cr = 5; 1418 free( &cr ); 1419 cr = 7; §\C{// unsound pointer dereference}§ 1420 \end{cfa} 1421 1422 The position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers. 1423 The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations; 1424 \CFA-style declarations (see \VRef{s:Declarations}) attempt to address this issue: 1425 \begin{quote2} 1426 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1427 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1428 \begin{cfa} 1429 ®const® * ®const® * const int ccp; 1430 ®const® & ®const® & const int ccr; 1431 \end{cfa} 1432 & 1433 \begin{cfa} 1434 const int * ®const® * ®const® ccp; 1435 1436 \end{cfa} 1437 \end{tabular} 1438 \end{quote2} 1439 where the \CFA declaration is read left-to-right. 1440 1441 Finally, like pointers, references are usable and composable with other type operators and generators. 1442 \begin{cfa} 1443 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§ 1444 &ar[1] = &w; §\C{// change reference array element}§ 1445 typeof( ar[1] ) p; §\C{// (gcc) is int, i.e., the type of referenced object}§ 1446 typeof( &ar[1] ) q; §\C{// (gcc) is int \&, i.e., the type of reference}§ 1447 sizeof( ar[1] ) == sizeof( int ); §\C{// is true, i.e., the size of referenced object}§ 1448 sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, i.e., the size of a reference}§ 1449 \end{cfa} 1450 1451 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}. 1452 Also, \CC does not allow \Index{array}s\index{array!reference} of reference\footnote{ 1453 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.} 1454 \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. 1455 1456 1457 \subsection{Initialization} 1458 1459 \Index{Initialization} is different than \Index{assignment} because initialization occurs on the empty (uninitialized) storage on an object, while assignment occurs on possibly initialized storage of an object. 1460 There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding. 1461 Because the object being initialized has no value, there is only one meaningful semantics with respect to address duality: it must mean address as there is no pointed-to value. 1462 In contrast, the left-hand side of assignment has an address that has a duality. 1463 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 1464 \begin{cfa} 1465 int * p = &x; §\C{// assign address of x}§ 1466 ®int * p = x;® §\C{// assign value of x}§ 1467 int & r = x; §\C{// must have address of x}§ 1468 \end{cfa} 1469 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). 1470 Therefore, for safety, this context requires an address, so it is superfluous to require explicitly taking the address of the initialization object, even though the type is incorrect. 1471 Note, this is strictly a convenience and safety feature for a programmer. 1472 Hence, \CFA allows ©r© to be assigned ©x© because it infers a reference for ©x©, by implicitly inserting a address-of operator, ©&©, and it is an error to put an ©&© because the types no longer match due to the implicit dereference. 1473 Unfortunately, C allows ©p© to be assigned with ©&x© (address) or ©x© (value), but most compilers warn about the latter assignment as being potentially incorrect. 1474 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. 1475 \begin{cfa} 1476 int & f( int & r ); §\C{// reference parameter and return}§ 1477 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§ 1478 \end{cfa} 1479 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©. 1480 Since operator routine ©?+?© takes its arguments by value, the references returned from ©f© are used to initialize compiler generated temporaries with value semantics that copy from the references. 1481 \begin{cfa} 1482 int temp1 = f( x ), temp2 = f( y ); 1483 z = temp1 + temp2; 1484 \end{cfa} 1485 This \Index{implicit referencing} is crucial for reducing the syntactic burden for programmers when using references; 1486 otherwise references have the same syntactic burden as pointers in these contexts. 1487 1488 When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. 1489 \begin{cfa} 1490 void f( ®const® int & cr ); 1491 void g( ®const® int * cp ); 1492 f( 3 ); g( ®&®3 ); 1493 f( x + y ); g( ®&®(x + y) ); 1494 \end{cfa} 1495 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. 1496 The ©&© before the constant/expression for the pointer-type parameter (©g©) is a \CFA extension necessary to type match and is a common requirement before a variable in C (\eg ©scanf©). 1497 Importantly, ©&3© may not be equal to ©&3©, where the references occur across calls because the temporaries maybe different on each call. 1498 1499 \CFA \emph{extends} this semantics to a mutable pointer/reference parameter, and the compiler implicitly creates the necessary temporary (copying the argument), which is subsequently pointed-to by the reference parameter and can be changed.\footnote{ 1500 If whole program analysis is possible, and shows the parameter is not assigned, \ie it is ©const©, the temporary is unnecessary.} 1501 \begin{cfa} 1502 void f( int & r ); 1503 void g( int * p ); 1504 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§ 1505 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§ 1506 \end{cfa} 1507 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ 1508 This conversion attempts to address the \newterm{const hell} problem, when the innocent addition of a ©const© qualifier causes a cascade of type failures, requiring an unknown number of additional ©const© qualifiers, until it is discovered a ©const© qualifier cannot be added and all the ©const© qualifiers must be removed.} 1509 The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. 1510 1511 %\CFA attempts to handle pointers and references in a uniform, symmetric manner. 1512 Finally, C handles \Index{routine object}s in an inconsistent way. 1513 A routine object is both a pointer and a reference (\Index{particle and wave}). 1514 \begin{cfa} 1515 void f( int i ); 1516 void (*fp)( int ); §\C{// routine pointer}§ 1517 fp = f; §\C{// reference initialization}§ 1518 fp = &f; §\C{// pointer initialization}§ 1519 fp = *f; §\C{// reference initialization}§ 1520 fp(3); §\C{// reference invocation}§ 1521 (*fp)(3); §\C{// pointer invocation}§ 1522 \end{cfa} 1523 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. 1524 Instead, a routine object should be referenced by a ©const© reference: 1525 \begin{cfa} 1526 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§ 1527 fr = ... §\C{// error, cannot change code}§ 1528 &fr = ...; §\C{// changing routine reference}§ 1529 fr( 3 ); §\C{// reference call to f}§ 1530 (*fr)(3); §\C{// error, incorrect type}§ 1531 \end{cfa} 1532 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ 1533 Dynamic code rewriting is possible but only in special circumstances.} 1534 \CFA allows this additional use of references for routine objects in an attempt to give a more consistent meaning for them. 1535 1536 1537 \subsection{Address-of Semantics} 1538 1539 In C, ©&E© is an rvalue for any expression ©E©. 1540 \CFA extends the ©&© (address-of) operator as follows: 1541 \begin{itemize} 1542 \item 1543 if ©R© is an \Index{rvalue} of type ©T &$_1$...&$_r$© where $r \ge 1$ references (©&© symbols) than ©&R© has type ©T ®*®&$_{\color{red}2}$...&$_{\color{red}r}$©, \ie ©T© pointer with $r-1$ references (©&© symbols). 1544 1545 \item 1546 if ©L© is an \Index{lvalue} of type ©T &$_1$...&$_l$© where $l \ge 0$ references (©&© symbols) then ©&L© has type ©T ®*®&$_{\color{red}1}$...&$_{\color{red}l}$©, \ie ©T© pointer with $l$ references (©&© symbols). 1547 \end{itemize} 1548 The following example shows the first rule applied to different \Index{rvalue} contexts: 1549 \begin{cfa} 1550 int x, * px, ** ppx, *** pppx, **** ppppx; 1551 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 1552 x = rrrx; // rrrx is an lvalue with type int &&& (equivalent to x) 1553 px = &rrrx; // starting from rrrx, &rrrx is an rvalue with type int *&&& (&x) 1554 ppx = &&rrrx; // starting from &rrrx, &&rrrx is an rvalue with type int **&& (&rx) 1555 pppx = &&&rrrx; // starting from &&rrrx, &&&rrrx is an rvalue with type int ***& (&rrx) 1556 ppppx = &&&&rrrx; // starting from &&&rrrx, &&&&rrrx is an rvalue with type int **** (&rrrx) 1557 \end{cfa} 1558 The following example shows the second rule applied to different \Index{lvalue} contexts: 1559 \begin{cfa} 1560 int x, * px, ** ppx, *** pppx; 1561 int & rx = x, && rrx = rx, &&& rrrx = rrx ; 1562 rrrx = 2; // rrrx is an lvalue with type int &&& (equivalent to x) 1563 &rrrx = px; // starting from rrrx, &rrrx is an rvalue with type int *&&& (rx) 1564 &&rrrx = ppx; // starting from &rrrx, &&rrrx is an rvalue with type int **&& (rrx) 1565 &&&rrrx = pppx; // starting from &&rrrx, &&&rrrx is an rvalue with type int ***& (rrrx) 1566 \end{cfa} 1567 1568 1569 \subsection{Conversions} 1570 1571 C provides a basic implicit conversion to simplify variable usage: 1572 \begin{enumerate} 1573 \setcounter{enumi}{-1} 1574 \item 1575 lvalue to rvalue conversion: ©cv T© converts to ©T©, which allows implicit variable dereferencing. 1576 \begin{cfa} 1577 int x; 1578 x + 1; // lvalue variable (int) converts to rvalue for expression 1579 \end{cfa} 1580 An rvalue has no type qualifiers (©cv©), so the lvalue qualifiers are dropped. 1581 \end{enumerate} 1582 \CFA provides three new implicit conversion for reference types to simplify reference usage. 1583 \begin{enumerate} 1584 \item 1585 reference to rvalue conversion: ©cv T &© converts to ©T©, which allows implicit reference dereferencing. 1586 \begin{cfa} 1587 int x, &r = x, f( int p ); 1588 x = ®r® + f( ®r® ); // lvalue reference converts to rvalue 1589 \end{cfa} 1590 An rvalue has no type qualifiers (©cv©), so the reference qualifiers are dropped. 1591 1592 \item 1593 lvalue to reference conversion: \lstinline[deletekeywords=lvalue]$lvalue-type cv1 T$ converts to ©cv2 T &©, which allows implicitly converting variables to references. 1594 \begin{cfa} 1595 int x, &r = ®x®, f( int & p ); // lvalue variable (int) convert to reference (int &) 1596 f( ®x® ); // lvalue variable (int) convert to reference (int &) 1597 \end{cfa} 1598 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost. 1599 Conversion can expand a type, where ©cv1© $>$ ©cv2©, \eg passing a ©const volatile int© to an ©int &©, which has high cost (\Index{warning}); 1600 furthermore, if ©cv1© has ©const© but not ©cv2©, a temporary variable is created to preserve the immutable lvalue. 1601 1602 \item 1603 rvalue to reference conversion: ©T© converts to ©cv T &©, which allows binding references to temporaries. 1604 \begin{cfa} 1605 int x, & f( int & p ); 1606 f( ®x + 3® ); // rvalue parameter (int) implicitly converts to lvalue temporary reference (int &) 1607 ®&f®(...) = &x; // rvalue result (int &) implicitly converts to lvalue temporary reference (int &) 1608 \end{cfa} 1609 In both case, modifications to the temporary are inaccessible (\Index{warning}). 1610 Conversion expands the temporary-type with ©cv©, which is low cost since the temporary is inaccessible. 1611 \end{enumerate} 1612 1613 1614 \begin{comment} 1615 From: Richard Bilson <rcbilson@gmail.com> 1616 Date: Wed, 13 Jul 2016 01:58:58 +0000 1617 Subject: Re: pointers / references 1618 To: "Peter A. Buhr" <pabuhr@plg2.cs.uwaterloo.ca> 1619 1620 As a general comment I would say that I found the section confusing, as you move back and forth 1621 between various real and imagined programming languages. If it were me I would rewrite into two 1622 subsections, one that specifies precisely the syntax and semantics of reference variables and 1623 another that provides the rationale. 1624 1625 I don't see any obvious problems with the syntax or semantics so far as I understand them. It's not 1626 obvious that the description you're giving is complete, but I'm sure you'll find the special cases 1627 as you do the implementation. 1628 1629 My big gripes are mostly that you're not being as precise as you need to be in your terminology, and 1630 that you say a few things that aren't actually true even though I generally know what you mean. 1631 1632 20 C provides a pointer type; CFA adds a reference type. Both types contain an address, which is normally a 1633 21 location in memory. 1634 1635 An address is not a location in memory; an address refers to a location in memory. Furthermore it 1636 seems weird to me to say that a type "contains" an address; rather, objects of that type do. 1637 1638 21 Special addresses are used to denote certain states or access co-processor memory. By 1639 22 convention, no variable is placed at address 0, so addresses like 0, 1, 2, 3 are often used to denote no-value 1640 23 or other special states. 1641 1642 This isn't standard C at all. There has to be one null pointer representation, but it doesn't have 1643 to be a literal zero representation and there doesn't have to be more than one such representation. 1644 1645 23 Often dereferencing a special state causes a memory fault, so checking is necessary 1646 24 during execution. 1647 1648 I don't see the connection between the two clauses here. I feel like if a bad pointer will not cause 1649 a memory fault then I need to do more checking, not less. 1650 1651 24 If the programming language assigns addresses, a program's execution is sound, \ie all 1652 25 addresses are to valid memory locations. 1653 1654 You haven't said what it means to "assign" an address, but if I use my intuitive understanding of 1655 the term I don't see how this can be true unless you're assuming automatic storage management. 1656 1657 1 Program variables are implicit pointers to memory locations generated by the compiler and automatically 1658 2 dereferenced, as in: 1659 1660 There is no reason why a variable needs to have a location in memory, and indeed in a typical 1661 program many variables will not. In standard terminology an object identifier refers to data in the 1662 execution environment, but not necessarily in memory. 1663 1664 13 A pointer/reference is a generalization of a variable name, \ie a mutable address that can point to more 1665 14 than one memory location during its lifetime. 1666 1667 I feel like you're off the reservation here. In my world there are objects of pointer type, which 1668 seem to be what you're describing here, but also pointer values, which can be stored in an object of 1669 pointer type but don't necessarily have to be. For example, how would you describe the value denoted 1670 by "&main" in a C program? I would call it a (function) pointer, but that doesn't satisfy your 1671 definition. 1672 1673 16 not occupy storage as the literal is embedded directly into instructions.) Hence, a pointer occupies memory 1674 17 to store its current address, and the pointer's value is loaded by dereferencing, \eg: 1675 1676 As with my general objection regarding your definition of variables, there is no reason why a 1677 pointer variable (object of pointer type) needs to occupy memory. 1678 1679 21 p2 = p1 + x; // compiler infers *p2 = *p1 + x; 1680 1681 What language are we in now? 1682 1683 24 pointer usage. However, in C, the following cases are ambiguous, especially with pointer arithmetic: 1684 25 p1 = p2; // p1 = p2 or *p1 = *p2 1685 1686 This isn't ambiguous. it's defined to be the first option. 1687 1688 26 p1 = p1 + 1; // p1 = p1 + 1 or *p1 = *p1 + 1 1689 1690 Again, this statement is not ambiguous. 1691 1692 13 example. Hence, a reference behaves like the variable name for the current variable it is pointing-to. The 1693 14 simplest way to understand a reference is to imagine the compiler inserting a dereference operator before 1694 15 the reference variable for each reference qualifier in a declaration, \eg: 1695 1696 It's hard for me to understand who the audience for this part is. I think a practical programmer is 1697 likely to be satisfied with "a reference behaves like the variable name for the current variable it 1698 is pointing-to," maybe with some examples. Your "simplest way" doesn't strike me as simpler than 1699 that. It feels like you're trying to provide a more precise definition for the semantics of 1700 references, but it isn't actually precise enough to be a formal specification. If you want to 1701 express the semantics of references using rewrite rules that's a great way to do it, but lay the 1702 rules out clearly, and when you're showing an example of rewriting keep your 1703 references/pointers/values separate (right now, you use \eg "r3" to mean a reference, a pointer, 1704 and a value). 1705 1706 24 Cancellation works to arbitrary depth, and pointer and reference values are interchangeable because both 1707 25 contain addresses. 1708 1709 Except they're not interchangeable, because they have different and incompatible types. 1710 1711 40 Interestingly, C++ deals with the address duality by making the pointed-to value the default, and prevent- 1712 41 ing changes to the reference address, which eliminates half of the duality. Java deals with the address duality 1713 42 by making address assignment the default and requiring field assignment (direct or indirect via methods), 1714 43 \ie there is no builtin bit-wise or method-wise assignment, which eliminates half of the duality. 1715 1716 I can follow this but I think that's mostly because I already understand what you're trying to 1717 say. I don't think I've ever heard the term "method-wise assignment" and I don't see you defining 1718 it. Furthermore Java does have value assignment of basic (non-class) types, so your summary here 1719 feels incomplete. (If it were me I'd drop this paragraph rather than try to save it.) 1720 1721 11 Hence, for type & const, there is no pointer assignment, so &rc = &x is disallowed, and the address value 1722 12 cannot be 0 unless an arbitrary pointer is assigned to the reference. 1723 1724 Given the pains you've taken to motivate every little bit of the semantics up until now, this last 1725 clause ("the address value cannot be 0") comes out of the blue. It seems like you could have 1726 perfectly reasonable semantics that allowed the initialization of null references. 1727 1728 12 In effect, the compiler is managing the 1729 13 addresses for type & const not the programmer, and by a programming discipline of only using references 1730 14 with references, address errors can be prevented. 1731 1732 Again, is this assuming automatic storage management? 1733 1734 18 rary binding. For reference initialization (like pointer), the initializing value must be an address (lvalue) not 1735 19 a value (rvalue). 1736 1737 This sentence appears to suggest that an address and an lvalue are the same thing. 1738 1739 20 int * p = &x; // both &x and x are possible interpretations 1740 1741 Are you saying that we should be considering "x" as a possible interpretation of the initializer 1742 "&x"? It seems to me that this expression has only one legitimate interpretation in context. 1743 1744 21 int & r = x; // x unlikely interpretation, because of auto-dereferencing 1745 1746 You mean, we can initialize a reference using an integer value? Surely we would need some sort of 1747 cast to induce that interpretation, no? 1748 1749 22 Hence, the compiler implicitly inserts a reference operator, &, before the initialization expression. 1750 1751 But then the expression would have pointer type, which wouldn't be compatible with the type of r. 1752 1753 22 Similarly, 1754 23 when a reference is used for a parameter/return type, the call-site argument does not require a reference 1755 24 operator. 1756 1757 Furthermore, it would not be correct to use a reference operator. 1758 1759 45 The implicit conversion allows 1760 1 seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. 1761 2 While C' attempts to handle pointers and references in a uniform, symmetric manner, C handles routine 1762 3 variables in an inconsistent way: a routine variable is both a pointer and a reference (particle and wave). 1763 1764 After all this talk of how expressions can have both pointer and value interpretations, you're 1765 disparaging C because it has expressions that have both pointer and value interpretations? 1766 1767 On Sat, Jul 9, 2016 at 4:18 PM Peter A. Buhr <pabuhr@plg.uwaterloo.ca> wrote: 1768 > Aaron discovered a few places where "&"s are missing and where there are too many "&", which are 1769 > corrected in the attached updated. None of the text has changed, if you have started reading 1770 > already. 1771 \end{comment} 1772 1773 1774 \section{Routine Definition} 1775 1776 \CFA also supports a new syntax for routine definition, as well as \Celeven and K\&R routine syntax. 1777 The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: 1778 \begin{cfa} 1779 ®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) { 1780 §\emph{routine body}§ 1781 } 1782 \end{cfa} 1783 where routine ©f© has three output (return values) and three input parameters. 1784 Existing C syntax cannot be extended with multiple return types because it is impossible to embed a single routine name within multiple return type specifications. 1785 1786 In detail, the brackets, ©[]©, enclose the result type, where each return value is named and that name is a local variable of the particular return type.\footnote{ 1787 \Index*{Michael Tiemann}, with help from \Index*{Doug Lea}, provided named return values in g++, circa 1989.} 1788 The value of each local return variable is automatically returned at routine termination. 1789 Declaration qualifiers can only appear at the start of a routine definition, \eg: 1790 \begin{cfa} 1791 ®extern® [ int x ] g( int y ) {§\,§} 1792 \end{cfa} 1793 Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified; 1794 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: 1795 \begin{cfa} 1796 [§\,§] g(); §\C{// no input or output parameters}§ 1797 [ void ] g( void ); §\C{// no input or output parameters}§ 1798 \end{cfa} 1799 1800 Routine f is called as follows: 1801 \begin{cfa} 1802 [ i, j, ch ] = f( 3, 'a', ch ); 1803 \end{cfa} 1804 The list of return values from f and the grouping on the left-hand side of the assignment is called a \newterm{return list} and discussed in Section 12. 1805 1806 \CFA style declarations cannot be used to declare parameters for K\&R style routine definitions because of the following ambiguity: 1807 \begin{cfa} 1808 int (*f(x))[ 5 ] int x; {} 1809 \end{cfa} 1810 The string ``©int (*f(x))[ 5 ]©'' declares a K\&R style routine of type returning a pointer to an array of 5 integers, while the string ``©[ 5 ] int x©'' declares a \CFA style parameter x of type array of 5 integers. 1811 Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string. 1812 As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity: 1813 \begin{cfa} 1814 typedef int foo; 1815 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ 1816 \end{cfa} 1817 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. 1818 The redefinition of a type name in a parameter list is the only context in C where the character ©*© can appear to the left of a type name, and \CFA relies on all type qualifier characters appearing to the right of the type name. 1819 The inability to use \CFA declarations in these two contexts is probably a blessing because it precludes programmers from arbitrarily switching between declarations forms within a declaration contexts. 1820 1821 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 1822 \begin{cfa} 1823 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§ 1824 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§ 1825 \end{cfa} 1826 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: 1827 \begin{cfa} 1828 #define ptoa( n, d ) int (*n)[ d ] 1829 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§ 1830 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§ 1831 \end{cfa} 1832 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. 1833 1834 1835 \subsection{Named Return Values} 1836 1837 \Index{Named return values} handle the case where it is necessary to define a local variable whose value is then returned in a ©return© statement, as in: 1838 \begin{cfa} 1839 int f() { 1840 int x; 1841 ... x = 0; ... x = y; ... 1842 return x; 1843 } 1844 \end{cfa} 1845 Because the value in the return variable is automatically returned when a \CFA routine terminates, the ©return© statement \emph{does not} contain an expression, as in: 1846 \newline 1847 \begin{minipage}{\linewidth} 1848 \begin{cfa} 1849 ®[ int x, int y ]® f() { 1850 int z; 1851 ... x = 0; ... y = z; ... 1852 ®return;® §\C{// implicitly return x, y}§ 1853 } 1854 \end{cfa} 1855 \end{minipage} 1856 \newline 1857 When the return is encountered, the current values of ©x© and ©y© are returned to the calling routine. 1858 As well, ``falling off the end'' of a routine without a ©return© statement is permitted, as in: 1859 \begin{cfa} 1860 [ int x, int y ] f() { 1861 ... 1862 } §\C{// implicitly return x, y}§ 1863 \end{cfa} 1864 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. 1865 1866 Named return values may be used in conjunction with named parameter values; 1867 specifically, a return and parameter can have the same name. 1868 \begin{cfa} 1869 [ int x, int y ] f( int, x, int y ) { 1870 ... 1871 } §\C{// implicitly return x, y}§ 1872 \end{cfa} 1873 This notation allows the compiler to eliminate temporary variables in nested routine calls. 1874 \begin{cfa} 1875 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§ 1876 int a, b; 1877 [a, b] = f( f( f( a, b ) ) ); 1878 \end{cfa} 1879 While the compiler normally ignores parameters names in prototype declarations, here they are used to eliminate temporary return-values by inferring that the results of each call are the inputs of the next call, and ultimately, the left-hand side of the assignment. 1880 Hence, even without the body of routine ©f© (separate compilation), it is possible to perform a global optimization across routine calls. 1881 The compiler warns about naming inconsistencies between routine prototype and definition in this case, and behaviour is \Index{undefined} if the programmer is inconsistent. 1882 1883 1884 \subsection{Routine Prototype} 1885 1886 The syntax of the new routine prototype declaration follows directly from the new routine definition syntax; 1887 as well, parameter names are optional, \eg: 1888 \begin{cfa} 1889 [ int x ] f (); §\C{// returning int with no parameters}§ 1890 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§ 1891 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§ 1892 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§ 1893 \end{cfa} 1894 This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). 1895 It is possible to declare multiple routine-prototypes in a single declaration, but the entire type specification is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:Declarations}), \eg: 1896 \begin{quote2} 1897 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1898 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1899 \begin{cfa} 1900 [ int ] f( int ), g; 1901 \end{cfa} 1902 & 1903 \begin{cfa} 1904 int f( int ), g( int ); 1905 \end{cfa} 1906 \end{tabular} 1907 \end{quote2} 1908 Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg: 1909 \begin{cfa} 1910 extern [ int ] f ( int ); 1911 static [ int ] g ( int ); 1912 \end{cfa} 1913 1914 1915 \section{Routine Pointers} 1916 1917 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 1918 \begin{cfa} 1919 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§ 1920 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§ 1921 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§ 1922 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§ 1923 \end{cfa} 1924 While parameter names are optional, \emph{a routine name cannot be specified}; 1925 for example, the following is incorrect: 1926 \begin{cfa} 1927 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§ 1928 \end{cfa} 1929 1930 1931 \section{Named and Default Arguments} 1932 1933 Named\index{named arguments}\index{arguments!named} and default\index{default arguments}\index{arguments!default} arguments~\cite{Hardgrave76}\footnote{ 1934 Francez~\cite{Francez77} proposed a further extension to the named-parameter passing style, which specifies what type of communication (by value, by reference, by name) the argument is passed to the routine.} 1935 are two mechanisms to simplify routine call. 1936 Both mechanisms are discussed with respect to \CFA. 1937 \begin{description} 1938 \item[Named (or Keyword) Arguments:] 1939 provide the ability to specify an argument to a routine call using the parameter name rather than the position of the parameter. 1940 For example, given the routine: 1941 \begin{cfa} 1942 void p( int x, int y, int z ) {...} 1943 \end{cfa} 1944 a positional call is: 1945 \begin{cfa} 1946 p( 4, 7, 3 ); 1947 \end{cfa} 1948 whereas a named (keyword) call may be: 1949 \begin{cfa} 1950 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§ 1951 \end{cfa} 1952 Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. 1953 The compiler rewrites a named call into a positional call. 1954 The advantages of named parameters are: 1955 \begin{itemize} 1956 \item 1957 Remembering the names of the parameters may be easier than the order in the routine definition. 1958 \item 1959 Parameter names provide documentation at the call site (assuming the names are descriptive). 1960 \item 1961 Changes can be made to the order or number of parameters without affecting the call (although the call must still be recompiled). 1962 \end{itemize} 1963 1964 Unfortunately, named arguments do not work in C-style programming-languages because a routine prototype is not required to specify parameter names, nor do the names in the prototype have to match with the actual definition. 1965 For example, the following routine prototypes and definition are all valid. 1966 \begin{cfa} 1967 void p( int, int, int ); §\C{// equivalent prototypes}§ 1968 void p( int x, int y, int z ); 1969 void p( int y, int x, int z ); 1970 void p( int z, int y, int x ); 1971 void p( int q, int r, int s ) {} §\C{// match with this definition}§ 1972 \end{cfa} 1973 Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. 1974 Alternatively, prototype definitions can be eliminated by using a two-pass compilation, and implicitly creating header files for exports. 1975 The former is easy to do, while the latter is more complex. 1976 1977 Furthermore, named arguments do not work well in a \CFA-style programming-languages because they potentially introduces a new criteria for type matching. 1978 For example, it is technically possible to disambiguate between these two overloaded definitions of ©f© based on named arguments at the call site: 1979 \begin{cfa} 1980 int f( int i, int j ); 1981 int f( int x, double y ); 1982 1983 f( j : 3, i : 4 ); §\C{// 1st f}§ 1984 f( x : 7, y : 8.1 ); §\C{// 2nd f}§ 1985 f( 4, 5 ); §\C{// ambiguous call}§ 1986 \end{cfa} 1987 However, named arguments compound routine resolution in conjunction with conversions: 1988 \begin{cfa} 1989 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§ 1990 \end{cfa} 1991 Depending on the cost associated with named arguments, this call could be resolvable or ambiguous. 1992 Adding named argument into the routine resolution algorithm does not seem worth the complexity. 1993 Therefore, \CFA does \emph{not} attempt to support named arguments. 1994 1995 \item[Default Arguments] 1996 provide the ability to associate a default value with a parameter so it can be optionally specified in the argument list. 1997 For example, given the routine: 1998 \begin{cfa} 1999 void p( int x = 1, int y = 2, int z = 3 ) {...} 2000 \end{cfa} 2001 the allowable positional calls are: 2002 \begin{cfa} 2003 p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 2004 p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 2005 p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 2006 p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§ 2007 // empty arguments 2008 p( , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§ 2009 p( 4, , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§ 2010 p( 4, 4, ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ 2011 p( 4, , ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ 2012 p( , 4, ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§ 2013 p( , , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§ 2014 p( , , ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ 2015 \end{cfa} 2016 Here the missing arguments are inserted from the default values in the parameter list. 2017 The compiler rewrites missing default values into explicit positional arguments. 2018 The advantages of default values are: 2019 \begin{itemize} 2020 \item 2021 Routines with a large number of parameters are often very generalized, giving a programmer a number of different options on how a computation is performed. 2022 For many of these kinds of routines, there are standard or default settings that work for the majority of computations. 2023 Without default values for parameters, a programmer is forced to specify these common values all the time, resulting in long argument lists that are error prone. 2024 \item 2025 When a routine's interface is augmented with new parameters, it extends the interface providing generalizability\footnote{ 2026 ``It should be possible for the implementor of an abstraction to increase its generality. 2027 So long as the modified abstraction is a generalization of the original, existing uses of the abstraction will not require change. 2028 It might be possible to modify an abstraction in a manner which is not a generalization without affecting existing uses, but, without inspecting the modules in which the uses occur, this possibility cannot be determined. 2029 This criterion precludes the addition of parameters, unless these parameters have default or inferred values that are valid for all possible existing applications.''~\cite[p.~128]{Cormack90}} 2030 (somewhat like the generalization provided by inheritance for classes). 2031 That is, all existing calls are still valid, although the call must still be recompiled. 2032 \end{itemize} 2033 The only disadvantage of default arguments is that unintentional omission of an argument may not result in a compiler-time error. 2034 Instead, a default value is used, which may not be the programmer's intent. 2035 2036 Default values may only appear in a prototype versus definition context: 2037 \begin{cfa} 2038 void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§ 2039 void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§ 2040 void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§ 2041 \end{cfa} 2042 The reason for this restriction is to allow separate compilation. 2043 Multiple prototypes with different default values is an error. 2044 \end{description} 2045 2046 Ellipse (``...'') arguments present problems when used with default arguments. 2047 The conflict occurs because both named and ellipse arguments must appear after positional arguments, giving two possibilities: 2048 \begin{cfa} 2049 p( /* positional */, ... , /* named */ ); 2050 p( /* positional */, /* named */, ... ); 2051 \end{cfa} 2052 While it is possible to implement both approaches, the first possibly is more complex than the second, \eg: 2053 \begin{cfa} 2054 p( int x, int y, int z, ... ); 2055 p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§ 2056 p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§ 2057 \end{cfa} 2058 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. 2059 Hence, this approach seems significantly more difficult, and hence, confusing and error prone. 2060 In the second call, the named arguments separate the positional and ellipse arguments, making it trivial to read the call. 2061 2062 The problem is exacerbated with default arguments, \eg: 2063 \begin{cfa} 2064 void p( int x, int y = 2, int z = 3... ); 2065 p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§ 2066 p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§ 2067 \end{cfa} 2068 The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; 2069 therefore, argument 5 subsequently conflicts with the named argument z : 3. 2070 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. 2071 For these reasons, \CFA requires named arguments before ellipse arguments. 2072 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. 2073 2074 Default arguments and overloading (see Section 24) are complementary. 2075 While in theory default arguments can be simulated with overloading, as in: 2076 \begin{quote2} 2077 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 2078 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{default arguments}} & \multicolumn{1}{c}{\textbf{overloading}} \\ 2079 \begin{cfa} 2080 void p( int x, int y = 2, int z = 3 ) {...} 2081 2082 2083 \end{cfa} 2084 & 2085 \begin{cfa} 2086 void p( int x, int y, int z ) {...} 2087 void p( int x ) { p( x, 2, 3 ); } 2088 void p( int x, int y ) { p( x, y, 3 ); } 2089 \end{cfa} 2090 \end{tabular} 2091 \end{quote2} 2092 the number of required overloaded routines is linear in the number of default values, which is unacceptable growth. 2093 In general, overloading should only be used over default arguments if the body of the routine is significantly different. 2094 Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: 2095 \begin{cfa} 2096 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§ 2097 \end{cfa} 2098 2099 Given the \CFA restrictions above, both named and default arguments are backwards compatible. 2100 \Index*[C++]{\CC{}} only supports default arguments; 2101 \Index*{Ada} supports both named and default arguments. 2102 2103 2104 \section{Unnamed Structure Fields} 2105 2106 C requires each field of a structure to have a name, except for a bit field associated with a basic type, \eg: 2107 \begin{cfa} 2108 struct { 2109 int f1; §\C{// named field}§ 2110 int f2 : 4; §\C{// named field with bit field size}§ 2111 int : 3; §\C{// unnamed field for basic type with bit field size}§ 2112 int ; §\C{// disallowed, unnamed field}§ 2113 int *; §\C{// disallowed, unnamed field}§ 2114 int (*)( int ); §\C{// disallowed, unnamed field}§ 2115 }; 2116 \end{cfa} 2117 This requirement is relaxed by making the field name optional for all field declarations; therefore, all the field declarations in the example are allowed. 2118 As for unnamed bit fields, an unnamed field is used for padding a structure to a particular size. 2119 A list of unnamed fields is also supported, \eg: 2120 \begin{cfa} 2121 struct { 2122 int , , ; §\C{// 3 unnamed fields}§ 2123 } 2124 \end{cfa} 2125 2126 2127 \section{Nesting} 2128 2129 Nesting of types and routines is useful for controlling name visibility (\newterm{name hiding}). 2130 2131 2132 \subsection{Type Nesting} 2133 2134 \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. 2135 \begin{figure} 2136 \centering 2137 \begin{tabular}{@{}l@{\hspace{3em}}l|l@{}} 2138 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{C Type Nesting}} & \multicolumn{1}{c}{\textbf{C Implicit Hoisting}} & \multicolumn{1}{|c}{\textbf{\CFA}} \\ 2139 \hline 2140 \begin{cfa} 2141 struct S { 2142 enum C { R, G, B }; 2143 struct T { 2144 union U { int i, j; }; 2145 enum C c; 2146 short int i, j; 2147 }; 2148 struct T t; 2149 } s; 2150 2151 int fred() { 2152 s.t.c = R; 2153 struct T t = { R, 1, 2 }; 2154 enum C c; 2155 union U u; 2156 } 2157 \end{cfa} 2158 & 2159 \begin{cfa} 2160 enum C { R, G, B }; 2161 union U { int i, j; }; 2162 struct T { 2163 enum C c; 2164 short int i, j; 2165 }; 2166 struct S { 2167 struct T t; 2168 } s; 2169 2170 2171 2172 2173 2174 2175 2176 \end{cfa} 2177 & 2178 \begin{cfa} 2179 struct S { 2180 enum C { R, G, B }; 2181 struct T { 2182 union U { int i, j; }; 2183 enum C c; 2184 short int i, j; 2185 }; 2186 struct T t; 2187 } s; 2188 2189 int fred() { 2190 s.t.c = ®S.®R; // type qualification 2191 struct ®S.®T t = { ®S.®R, 1, 2 }; 2192 enum ®S.®C c; 2193 union ®S.T.®U u; 2194 } 2195 \end{cfa} 2196 \end{tabular} 2197 \caption{Type Nesting / Qualification} 2198 \label{f:TypeNestingQualification} 2199 \end{figure} 2200 In the left example in C, types ©C©, ©U© and ©T© are implicitly hoisted outside of type ©S© into the containing block scope. 2201 In the right example in \CFA, the types are not hoisted and accessed using the field-selection operator ``©.©'' for type qualification, as does \Index*{Java}, rather than the \CC type-selection operator ``©::©''. 2202 2203 2204 \subsection{Routine Nesting} 2205 2206 While \CFA does not provide object programming by putting routines into structures, it does rely heavily on locally nested routines to redefine operations at or close to a call site. 2207 For example, the C quick-sort is wrapped into the following polymorphic \CFA routine: 2208 \begin{cfa} 2209 forall( otype T | { int ?<?( T, T ); } ) 2210 void qsort( const T * arr, size_t dimension ); 2211 \end{cfa} 2212 which can be used to sort in ascending and descending order by locally redefining the less-than operator into greater-than. 2213 \begin{cfa} 2214 const unsigned int size = 5; 2215 int ia[size]; 2216 ... §\C{// assign values to array ia}§ 2217 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§ 2218 { 2219 ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§ 2220 qsort( ia, size ); §\C{// sort descending order by local redefinition}§ 2221 } 2222 \end{cfa} 2223 2224 Nested routines are not first-class, meaning a nested routine cannot be returned if it has references to variables in its enclosing blocks; 2225 the only exception is references to the external block of the translation unit, as these variables persist for the duration of the program. 2226 The following program in undefined in \CFA (and Indexc{gcc}) 2227 \begin{cfa} 2228 [* [int]( int )] foo() { §\C{// int (*foo())( int )}§ 2229 int ®i® = 7; 2230 int bar( int p ) { 2231 ®i® += 1; §\C{// dependent on local variable}§ 2232 sout | ®i® | endl; 2233 } 2234 return bar; §\C{// undefined because of local dependence}§ 2235 } 2236 int main() { 2237 * [int]( int ) fp = foo(); §\C{// int (*fp)( int )}§ 2238 sout | fp( 3 ) | endl; 2239 } 2240 \end{cfa} 2241 because 2242 2243 Currently, there are no \Index{lambda} expressions, \ie unnamed routines because routine names are very important to properly select the correct routine. 2244 2245 2246 \section{Tuples} 2247 2248 In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call. 2249 (More contexts are added shortly.) 2250 A list of such elements is called a \newterm{lexical list}. 2251 The general syntax of a lexical list is: 2252 \begin{cfa} 2253 [ §\emph{exprlist}§ ] 2254 \end{cfa} 2255 where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas. 2256 The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator. 2257 The following are examples of lexical lists: 2258 \begin{cfa} 2259 [ x, y, z ] 2260 [ 2 ] 2261 [ v+w, x*y, 3.14159, f() ] 2262 \end{cfa} 2263 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. 2264 Note, a tuple is not a record (structure); 2265 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1). 2266 In essence, tuples are largely a compile time phenomenon, having little or no runtime presence. 2267 2268 Tuples can be organized into compile-time tuple variables; 2269 these variables are of \newterm{tuple type}. 2270 Tuple variables and types can be used anywhere lists of conventional variables and types can be used. 2271 The general syntax of a tuple type is: 2272 \begin{cfa} 2273 [ §\emph{typelist}§ ] 2274 \end{cfa} 2275 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. 2276 Examples of tuple types include: 2277 \begin{cfa} 2278 [ unsigned int, char ] 2279 [ double, double, double ] 2280 [ * int, int * ] §\C{// mix of CFA and ANSI}§ 2281 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 2282 \end{cfa} 2283 Like tuples, tuple types may be nested, such as ©[ [ int, int ], int ]©, which is a 2-element tuple type whose first element is itself a tuple type. 2284 2285 Examples of declarations using tuple types are: 2286 \begin{cfa} 2287 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§ 2288 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§ 2289 [ [ int, int ] ] z ([ int, int ]); 2290 \end{cfa} 2291 The last example declares an external routine that expects a 2 element tuple as an input parameter and returns a 2 element tuple as its result. 2292 2293 As mentioned, tuples can appear in contexts requiring a list of value, such as an argument list of a routine call. 2294 In unambiguous situations, the tuple brackets may be omitted, \eg a tuple that appears as an argument may have its 2295 square brackets omitted for convenience; therefore, the following routine invocations are equivalent: 2296 \begin{cfa} 2297 f( [ 1, x+2, fred() ] ); 2298 f( 1, x+2, fred() ); 2299 \end{cfa} 2300 Also, a tuple or a tuple variable may be used to supply all or part of an argument list for a routine expecting multiple input parameters or for a routine expecting a tuple as an input parameter. 2301 For example, the following are all legal: 2302 \begin{cfa} 2303 [ int, int ] w1; 2304 [ int, int, int ] w2; 2305 [ void ] f (int, int, int); /* three input parameters of type int */ 2306 [ void ] g ([ int, int, int ]); /* 3 element tuple as input */ 2307 f( [ 1, 2, 3 ] ); 2308 f( w1, 3 ); 2309 f( 1, w1 ); 2310 f( w2 ); 2311 g( [ 1, 2, 3 ] ); 2312 g( w1, 3 ); 2313 g( 1, w1 ); 2314 g( w2 ); 2315 \end{cfa} 2316 Note, in all cases 3 arguments are supplied even though the syntax may appear to supply less than 3. As mentioned, a 2317 tuple does not have structure like a record; a tuple is simply converted into a list of components. 2318 \begin{rationale} 2319 The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; \ie a statement such as ©g( f() )© is not supported. 2320 Using a temporary variable to store the results of the inner routine and then passing this variable to the outer routine works, however. 2321 \end{rationale} 2322 2323 A tuple can contain a C comma expression, provided the expression containing the comma operator is enclosed in parentheses. 2324 For instance, the following tuples are equivalent: 2325 \begin{cfa} 2326 [ 1, 3, 5 ] 2327 [ 1, (2, 3), 5 ] 2328 \end{cfa} 2329 The second element of the second tuple is the expression (2, 3), which yields the result 3. 2330 This requirement is the same as for comma expressions in argument lists. 2331 2332 Type qualifiers, \ie const and volatile, may modify a tuple type. 2333 The meaning is the same as for a type qualifier modifying an aggregate type [Int99, x 6.5.2.3(7),x 6.7.3(11)], \ie the qualifier is distributed across all of the types in the tuple, \eg: 2334 \begin{cfa} 2335 const volatile [ int, float, const int ] x; 2336 \end{cfa} 2337 is equivalent to: 2338 \begin{cfa} 2339 [ const volatile int, const volatile float, const volatile int ] x; 2340 \end{cfa} 2341 Declaration qualifiers can only appear at the start of a \CFA tuple declaration4, \eg: 2342 \begin{cfa} 2343 extern [ int, int ] w1; 2344 static [ int, int, int ] w2; 2345 \end{cfa} 2346 \begin{rationale} 2347 Unfortunately, C's syntax for subscripts precluded treating them as tuples. 2348 The C subscript list has the form ©[i][j]...© and not ©[i, j, ...]©. 2349 Therefore, there is no syntactic way for a routine returning multiple values to specify the different subscript values, \eg ©f[g()]© always means a single subscript value because there is only one set of brackets. 2350 Fixing this requires a major change to C because the syntactic form ©M[i, j, k]© already has a particular meaning: ©i, j, k© is a comma expression. 2351 \end{rationale} 2352 2353 2354 \subsection{Tuple Coercions} 2355 2356 There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring. 2357 In addition, the coercion of dereferencing can be performed on a tuple variable to yield its value(s), as for other variables. 2358 A \newterm{closing coercion} takes a set of values and converts it into a tuple value, which is a contiguous set of values, as in: 2359 \begin{cfa} 2360 [ int, int, int, int ] w; 2361 w = [ 1, 2, 3, 4 ]; 2362 \end{cfa} 2363 First the right-hand tuple is closed into a tuple value and then the tuple value is assigned. 2364 2365 An \newterm{opening coercion} is the opposite of closing; a tuple value is converted into a tuple of values, as in: 2366 \begin{cfa} 2367 [ a, b, c, d ] = w 2368 \end{cfa} 2369 ©w© is implicitly opened to yield a tuple of four values, which are then assigned individually. 2370 2371 A \newterm{flattening coercion} coerces a nested tuple, \ie a tuple with one or more components, which are themselves tuples, into a flattened tuple, which is a tuple whose components are not tuples, as in: 2372 \begin{cfa} 2373 [ a, b, c, d ] = [ 1, [ 2, 3 ], 4 ]; 2374 \end{cfa} 2375 First the right-hand tuple is flattened and then the values are assigned individually. 2376 Flattening is also performed on tuple types. 2377 For example, the type ©[ int, [ int, int ], int ]© can be coerced, using flattening, into the type ©[ int, int, int, int ]©. 2378 2379 A \newterm{structuring coercion} is the opposite of flattening; 2380 a tuple is structured into a more complex nested tuple. 2381 For example, structuring the tuple ©[ 1, 2, 3, 4 ]© into the tuple ©[ 1, [ 2, 3 ], 4 ]© or the tuple type ©[ int, int, int, int ]© into the tuple type ©[ int, [ int, int ], int ]©. 2382 In the following example, the last assignment illustrates all the tuple coercions: 2383 \begin{cfa} 2384 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 2385 int x = 5; 2386 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§ 2387 \end{cfa} 2388 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; 2389 therefore, the right-hand tuple is now the tuple ©[ [ 1, 2, 3, 4 ], 5 ]©. 2390 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. 2391 The tuple ©[ 2, 3, 4, 5 ]© is then closed to create a tuple value. 2392 Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using multiple assignment (see Section 14). 2393 \begin{rationale} 2394 A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple. 2395 \end{rationale} 2396 2397 2398 \section{Mass Assignment} 2399 2400 \CFA permits assignment to several variables at once using mass assignment~\cite{CLU}. 2401 Mass assignment has the following form: 2402 \begin{cfa} 2403 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§; 2404 \end{cfa} 2405 \index{lvalue} 2406 The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, \ie any data object that can appear on the left-hand side of a conventional assignment statement. 2407 ©$\emph{expr}$© is any standard arithmetic expression. 2408 Clearly, the types of the entities being assigned must be type compatible with the value of the expression. 2409 2410 Mass assignment has parallel semantics, \eg the statement: 2411 \begin{cfa} 2412 [ x, y, z ] = 1.5; 2413 \end{cfa} 2414 is equivalent to: 2415 \begin{cfa} 2416 x = 1.5; y = 1.5; z = 1.5; 2417 \end{cfa} 2418 This semantics is not the same as the following in C: 2419 \begin{cfa} 2420 x = y = z = 1.5; 2421 \end{cfa} 2422 as conversions between intermediate assignments may lose information. 2423 A more complex example is: 2424 \begin{cfa} 2425 [ i, y[i], z ] = a + b; 2426 \end{cfa} 2427 which is equivalent to: 2428 \begin{cfa} 2429 t = a + b; 2430 a1 = &i; a2 = &y[i]; a3 = &z; 2431 *a1 = t; *a2 = t; *a3 = t; 2432 \end{cfa} 2433 The temporary ©t© is necessary to store the value of the expression to eliminate conversion issues. 2434 The temporaries for the addresses are needed so that locations on the left-hand side do not change as the values are assigned. 2435 In this case, ©y[i]© uses the previous value of ©i© and not the new value set at the beginning of the mass assignment. 2436 2437 2438 \section{Multiple Assignment} 2439 2440 \CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}. 2441 Multiple assignment has the following form: 2442 \begin{cfa} 2443 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§ ]; 2444 \end{cfa} 2445 \index{lvalue} 2446 The left-hand side is a tuple of \emph{lvalues}, and the right-hand side is a tuple of \emph{expr}s. 2447 Each \emph{expr} appearing on the right-hand side of a multiple assignment statement is assigned to the corresponding \emph{lvalues} on the left-hand side of the statement using parallel semantics for each assignment. 2448 An example of multiple assignment is: 2449 \begin{cfa} 2450 [ x, y, z ] = [ 1, 2, 3 ]; 2451 \end{cfa} 2452 Here, the values ©1©, ©2© and ©3© are assigned, respectively, to the variables ©x©, ©y© and ©z©. 2453 A more complex example is: 2454 \begin{cfa} 2455 [ i, y[ i ], z ] = [ 1, i, a + b ]; 2456 \end{cfa} 2457 Here, the values ©1©, ©i© and ©a + b© are assigned to the variables ©i©, ©y[i]© and ©z©, respectively. 2458 Note, the parallel semantics of 2459 multiple assignment ensures: 2460 \begin{cfa} 2461 [ x, y ] = [ y, x ]; 2462 \end{cfa} 2463 correctly interchanges (swaps) the values stored in ©x© and ©y©. 2464 The following cases are errors: 2465 \begin{cfa} 2466 [ a, b, c ] = [ 1, 2, 3, 4 ]; 2467 [ a, b, c ] = [ 1, 2 ]; 2468 \end{cfa} 2469 because the number of entities in the left-hand tuple is unequal with the right-hand tuple. 2470 2471 As for all tuple contexts in C, side effects should not be used because C does not define an ordering for the evaluation of the elements of a tuple; 2472 both these examples produce indeterminate results: 2473 \begin{cfa} 2474 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§ 2475 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§ 2476 \end{cfa} 2477 2478 2479 \section{Cascade Assignment} 2480 2481 As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment. 2482 Cascade assignment has the following form: 2483 \begin{cfa} 2484 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§; 2485 \end{cfa} 2486 and it has the same parallel semantics as for mass and multiple assignment. 2487 Some examples of cascade assignment are: 2488 \begin{cfa} 2489 x1 = y1 = x2 = y2 = 0; 2490 [ x1, y1 ] = [ x2, y2 ] = [ x3, y3 ]; 2491 [ x1, y1 ] = [ x2, y2 ] = 0; 2492 [ x1, y1 ] = z = 0; 2493 \end{cfa} 2494 As in C, the rightmost assignment is performed first, \ie assignment parses right to left. 2495 2496 2497 \section{Field Tuples} 2498 2499 Tuples may be used to select multiple fields of a record by field name. 2500 Its general form is: 2501 \begin{cfa} 2502 §\emph{expr}§ . [ §\emph{fieldlist}§ ] 2503 §\emph{expr}§ -> [ §\emph{fieldlist}§ ] 2504 \end{cfa} 2505 \emph{expr} is any expression yielding a value of type record, \eg ©struct©, ©union©. 2506 Each element of \emph{ fieldlist} is an element of the record specified by \emph{expr}. 2507 A record-field tuple may be used anywhere a tuple can be used. An example of the use of a record-field tuple is 2508 the following: 2509 \begin{cfa} 2510 struct s { 2511 int f1, f2; 2512 char f3; 2513 double f4; 2514 } v; 2515 v.[ f3, f1, f2 ] = ['x', 11, 17 ]; §\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}§ 2516 f( v.[ f3, f1, f2 ] ); §\C{// equivalent to f( v.f3, v.f1, v.f2 )}§ 2517 \end{cfa} 2518 Note, the fields appearing in a record-field tuple may be specified in any order; 2519 also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. 2520 2521 If a field of a ©struct© is itself another ©struct©, multiple fields of this subrecord can be specified using a nested record-field tuple, as in the following example: 2522 \begin{cfa} 2523 struct inner { 2524 int f2, f3; 2525 }; 2526 struct outer { 2527 int f1; 2528 struct inner i; 2529 double f4; 2530 } o; 2531 2532 o.[ f1, i.[ f2, f3 ], f4 ] = [ 11, 12, 13, 3.14159 ]; 2533 \end{cfa} 2397 2534 2398 2535 … … 2402 2539 2403 2540 The goal of \CFA I/O is to simplify the common cases\index{I/O!common case}, while fully supporting polymorphism and user defined types in a consistent way. 2541 The approach combines ideas from \CC and Python. 2404 2542 The \CFA header file for the I/O library is \Indexc{fstream}. 2405 2543 … … 2418 2556 \end{cfa} 2419 2557 \\ 2420 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2421 1 232558 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2559 1® ®2® ®3 2422 2560 \end{cfa} 2423 2561 & 2424 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2562 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2425 2563 1 2 3 2426 2564 \end{cfa} 2427 2565 \end{tabular} 2428 2566 \end{quote2} 2429 The \CFA form has half as many characters asthe \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators.2430 A tuple prints all the tuple's values, each separated by ©", "©.2431 \begin{cfa} [mathescape=off,aboveskip=0pt,belowskip=0pt]2432 [int, int] t1 = [1, 2], t2 = [3, 4];2567 The \CFA form has half the characters of the \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators. 2568 Similar simplification occurs for \Index{tuple} I/O, which prints all tuple values separated by ``\lstinline[showspaces=true]@, @''. 2569 \begin{cfa} 2570 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 2433 2571 sout | t1 | t2 | endl; §\C{// print tuples}§ 2434 2572 \end{cfa} 2435 \begin{cfa}[ mathescape=off,showspaces=true,belowskip=0pt]2436 1 , 2, 3, 42437 \end{cfa} 2438 \CFA uses the logical-or operator for I/O becauseit is the lowest-priority overloadable operator, other than assignment.2573 \begin{cfa}[showspaces=true,aboveskip=0pt] 2574 1®, ®2®, ®3 4®, ®5®, ®6 2575 \end{cfa} 2576 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority overloadable operator, other than assignment. 2439 2577 Therefore, fewer output expressions require parenthesis. 2440 2578 \begin{quote2} … … 2453 2591 \\ 2454 2592 & 2455 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2593 \begin{cfa}[showspaces=true,aboveskip=0pt] 2456 2594 3 3 12 0 3 1 2 2457 2595 \end{cfa} 2458 2596 \end{tabular} 2459 2597 \end{quote2} 2460 Finally, the logical-or operator has a link with the Shell pipe-operator for moving data, where data flows in the correct direction for input but the opposite direction for output. 2461 2462 2463 The implicit separator\index{I/O!separator} character (space/blank) is a separator not a terminator. 2598 There is a weak similarity between the \CFA logical-or operator and the Shell pipe-operator for moving data, where data flows in the correct direction for input but the opposite direction for output. 2599 2600 2601 \subsection{Implicit Separator} 2602 2603 The \Index{implicit separator}\index{I/O!separator} character (space/blank) is a separator not a terminator. 2464 2604 The rules for implicitly adding the separator are: 2465 2605 \begin{enumerate} … … 2469 2609 sout | 1 | 2 | 3 | endl; 2470 2610 \end{cfa} 2471 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2611 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2472 2612 1 2 3 2473 2613 \end{cfa} … … 2533 2673 \end{enumerate} 2534 2674 2535 The following routines and \CC-style \Index{manipulator}s control implicit seperation. 2675 2676 \subsection{Manipulator} 2677 2678 The following \CC-style \Index{manipulator}s and routines control implicit seperation. 2536 2679 \begin{enumerate} 2537 2680 \item 2538 Routines \Indexc{sepSet}\index{manipulator!sepSet@©sepSet©} and \Indexc{sep Get}\index{manipulator!sepGet@©sepGet©} set and get the separator string.2681 Routines \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. 2539 2682 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 2540 \begin{cfa}[mathescape=off, aboveskip=0pt,belowskip=0pt]2683 \begin{cfa}[mathescape=off,belowskip=0pt] 2541 2684 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§ 2542 sout | 1 | 2 | 3 | " \"" | ®sep Get( sout )® | "\"" | endl;2685 sout | 1 | 2 | 3 | " \"" | ®sep® | "\"" | endl; 2543 2686 \end{cfa} 2544 2687 %$ 2545 2688 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2546 1 , $2, $3 ®", $"®2689 1®, $®2®, $®3 ®", $"® 2547 2690 \end{cfa} 2548 2691 %$ 2549 \begin{cfa}[ mathescape=off,aboveskip=0pt,belowskip=0pt]2692 \begin{cfa}[belowskip=0pt] 2550 2693 sepSet( sout, " " ); §\C{// reset separator to " "}§ 2551 2694 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"" | endl; 2552 2695 \end{cfa} 2553 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2554 1 2 3 ®" "® 2555 \end{cfa} 2556 2557 \item 2558 Manipulators \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} \emph{locally} toggle printing the separator, \ie the seperator is adjusted only with respect to the next printed item. 2559 \begin{cfa}[mathescape=off,belowskip=0pt] 2560 sout | sepOn | 1 | 2 | 3 | sepOn | endl; §\C{// separator at start of line}§ 2561 \end{cfa} 2562 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2563 1 2 3 2564 \end{cfa} 2565 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2566 sout | 1 | sepOff | 2 | 3 | endl; §\C{// locally turn off implicit separator}§ 2567 \end{cfa} 2568 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2569 12 3 2570 \end{cfa} 2571 2572 \item 2573 Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} \emph{globally} toggle printing the separator, \ie the seperator is adjusted with respect to all subsequent printed items, unless locally adjusted. 2574 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2575 sout | sepDisable | 1 | 2 | 3 | endl; §\C{// globally turn off implicit separation}§ 2576 \end{cfa} 2577 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2696 \begin{cfa}[showspaces=true,aboveskip=0pt] 2697 1® ®2® ®3 ®" "® 2698 \end{cfa} 2699 ©sepGet© can be used to store a separator and then restore it: 2700 \begin{cfa}[belowskip=0pt] 2701 char store[®sepSize®]; §\C{// sepSize is the maximum separator size}§ 2702 strcpy( store, sepGet( sout ) ); §\C{// copy current separator}§ 2703 sepSet( sout, "_" ); §\C{// change separator to underscore}§ 2704 sout | 1 | 2 | 3 | endl; 2705 \end{cfa} 2706 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2707 1®_®2®_®3 2708 \end{cfa} 2709 \begin{cfa}[belowskip=0pt] 2710 sepSet( sout, store ); §\C{// change separator back to original}§ 2711 sout | 1 | 2 | 3 | endl; 2712 \end{cfa} 2713 \begin{cfa}[showspaces=true,aboveskip=0pt] 2714 1® ®2® ®3 2715 \end{cfa} 2716 2717 \item 2718 Routine \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@©sepSetTuple©} and \Indexc{sepTuple}\index{manipulator!sepTuple@©sepTuple©}/\Indexc{sepGetTuple}\index{manipulator!sepGetTuple@©sepGetTuple©} get and set the tuple separator-string. 2719 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 2720 \begin{cfa}[belowskip=0pt] 2721 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§ 2722 sout | t1 | t2 | " \"" | ®sepTuple® | "\"" | endl; 2723 \end{cfa} 2724 \begin{cfa}[showspaces=true,aboveskip=0pt] 2725 1 2 3 4 5 6 ®" "® 2726 \end{cfa} 2727 \begin{cfa}[belowskip=0pt] 2728 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§ 2729 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl; 2730 \end{cfa} 2731 \begin{cfa}[showspaces=true,aboveskip=0pt] 2732 1, 2, 3 4, 5, 6 ®", "® 2733 \end{cfa} 2734 As for ©sepGet©, ©sepGetTuple© can be use to store a tuple separator and then restore it. 2735 2736 \item 2737 Manipulators \Indexc{sepDisable}\index{manipulator!sepDisable@©sepDisable©} and \Indexc{sepEnable}\index{manipulator!sepEnable@©sepEnable©} \emph{globally} toggle printing the separator, \ie the seperator is adjusted with respect to all subsequent printed items. 2738 \begin{cfa}[belowskip=0pt] 2739 sout | sepDisable | 1 | 2 | 3 | endl; §\C{// globally turn off implicit separator}§ 2740 \end{cfa} 2741 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2578 2742 123 2579 2743 \end{cfa} 2580 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2581 sout | 1 | sepOn | 2 | 3 | endl; §\C{// locally turn on implicit separator}§ 2582 \end{cfa} 2583 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2584 1 23 2585 \end{cfa} 2586 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2587 sout | sepEnable | 1 | 2 | 3 | endl; §\C{// globally turn on implicit separation}§ 2744 \begin{cfa}[belowskip=0pt] 2745 sout | sepEnable | 1 | 2 | 3 | endl; §\C{// globally turn on implicit separator}§ 2588 2746 \end{cfa} 2589 2747 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 2592 2750 2593 2751 \item 2594 Routine \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@©sepSetTuple©} and \Indexc{sepGetTuple}\index{manipulator!sepGetTuple@©sepGetTuple©} get and set the tuple separator-string. 2595 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 2596 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2597 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§ 2598 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl; 2599 \end{cfa} 2600 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2601 1 2 3 4 ®" "® 2602 \end{cfa} 2603 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2604 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§ 2605 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl; 2606 \end{cfa} 2607 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2608 1, 2, 3, 4 ®", "® 2609 \end{cfa} 2610 2611 \item 2612 The tuple separator can also be turned on and off. 2613 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2614 sout | sepOn | t1 | sepOff | t2 | endl; §\C{// locally turn on/off implicit separation}§ 2615 \end{cfa} 2616 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2617 , 1, 23, 4 2618 \end{cfa} 2619 Notice a tuple seperator starts the line because the next item is a tuple. 2752 Manipulators \Indexc{sepOn}\index{manipulator!sepOn@©sepOn©} and \Indexc{sepOff}\index{manipulator!sepOff@©sepOff©} \emph{locally} toggle printing the separator, \ie the seperator is adjusted only with respect to the next printed item. 2753 \begin{cfa}[belowskip=0pt] 2754 sout | 1 | sepOff | 2 | 3 | endl; §\C{// locally turn off implicit separator}§ 2755 \end{cfa} 2756 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2757 12 3 2758 \end{cfa} 2759 \begin{cfa}[belowskip=0pt] 2760 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; §\C{// locally turn on implicit separator}§ 2761 \end{cfa} 2762 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2763 1 23 2764 \end{cfa} 2765 The tuple separator also responses to being turned on and off. 2766 \begin{cfa}[belowskip=0pt] 2767 sout | t1 | sepOff | t2 | endl; §\C{// locally turn on/off implicit separator}§ 2768 \end{cfa} 2769 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2770 1, 2, 34, 5, 6 2771 \end{cfa} 2772 ©sepOn© \emph{cannot} be used to start/end a line with a separator because separators do not appear at the start/end of a line; 2773 use ©sep© to accomplish this functionality. 2774 \begin{cfa}[belowskip=0pt] 2775 sout | sepOn | 1 | 2 | 3 | sepOn | endl ; §\C{// sepOn does nothing at start/end of line}§ 2776 \end{cfa} 2777 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2778 1 2 3 2779 \end{cfa} 2780 \begin{cfa}[belowskip=0pt] 2781 sout | sep | 1 | 2 | 3 | sep | endl ; §\C{// use sep to print separator at start/end of line}§ 2782 \end{cfa} 2783 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2784 ® ®1 2 3® ® 2785 \end{cfa} 2620 2786 \end{enumerate} 2621 2787 2622 2788 \begin{comment} 2623 2789 #include <fstream> 2790 #include <string.h> // strcpy 2624 2791 2625 2792 int main( void ) { 2626 2793 int x = 1, y = 2, z = 3; 2627 2794 sout | x | y | z | endl; 2628 [int, int] t1 = [1, 2], t2 = [3, 4];2629 sout | t1 | t2 | endl; // print tuple 2795 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 2796 sout | t1 | t2 | endl; // print tuples 2630 2797 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl; 2631 2798 sout | 1 | 2 | 3 | endl; … … 2640 2807 2641 2808 sepSet( sout, ", $" ); // set separator from " " to ", $" 2642 sout | 1 | 2 | 3 | " \"" | sep Get( sout )| "\"" | endl;2809 sout | 1 | 2 | 3 | " \"" | sep | "\"" | endl; 2643 2810 sepSet( sout, " " ); // reset separator to " " 2644 2811 sout | 1 | 2 | 3 | " \"" | sepGet( sout ) | "\"" | endl; 2645 2812 2646 sout | sepOn | 1 | 2 | 3 | sepOn | endl; // separator at start of line2647 s out | 1 | sepOff | 2 | 3 | endl; // locally turn off implicit separator2648 2649 sout | sepDisable | 1 | 2 | 3 | endl; // globally turn off implicit separation2650 s out | 1 | sepOn | 2 | 3 | endl; // locally turn on implicit separator2651 sout | sepEnable | 1 | 2 | 3 | endl; // globally turn on implicit separation2813 char store[sepSize]; 2814 strcpy( store, sepGet( sout ) ); 2815 sepSet( sout, "_" ); 2816 sout | 1 | 2 | 3 | endl; 2817 sepSet( sout, store ); 2818 sout | 1 | 2 | 3 | endl; 2652 2819 2653 2820 sepSetTuple( sout, " " ); // set tuple separator from ", " to " " 2654 sout | t1 | t2 | " \"" | sep GetTuple( sout )| "\"" | endl;2821 sout | t1 | t2 | " \"" | sepTuple | "\"" | endl; 2655 2822 sepSetTuple( sout, ", " ); // reset tuple separator to ", " 2656 2823 sout | t1 | t2 | " \"" | sepGetTuple( sout ) | "\"" | endl; 2657 2824 2658 sout | t1 | t2 | endl; // print tuple 2659 sout | sepOn | t1 | sepOff | t2 | endl; // locally turn on/off implicit separation 2825 sout | sepDisable | 1 | 2 | 3 | endl; // globally turn off implicit separator 2826 sout | sepEnable | 1 | 2 | 3 | endl; // globally turn on implicit separator 2827 2828 sout | 1 | sepOff | 2 | 3 | endl; // locally turn on implicit separator 2829 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; // globally turn off implicit separator 2830 sout | sepEnable; 2831 sout | t1 | sepOff | t2 | endl; // locally turn on/off implicit separator 2832 2833 sout | sepOn | 1 | 2 | 3 | sepOn | endl ; // sepOn does nothing at start/end of line 2834 sout | sep | 1 | 2 | 3 | sep | endl ; // use sep to print separator at start/end of line 2660 2835 } 2661 2836 … … 5189 5364 5190 5365 5191 \section{\ protect\CFA Keywords}5366 \section{\texorpdfstring{\CFA Keywords}{Cforall Keywords}} 5192 5367 \label{s:CFAKeywords} 5193 5368 5369 \CFA introduces the following new keywords. 5370 5194 5371 \begin{quote2} 5195 \begin{tabular}{llll }5372 \begin{tabular}{lllll} 5196 5373 \begin{tabular}{@{}l@{}} 5197 ©_A T© \\5374 ©_At© \\ 5198 5375 ©catch© \\ 5199 5376 ©catchResume© \\ 5200 5377 ©choose© \\ 5201 5378 ©coroutine© \\ 5202 ©disable© \\5203 5379 \end{tabular} 5204 5380 & 5205 5381 \begin{tabular}{@{}l@{}} 5382 ©disable© \\ 5206 5383 ©dtype© \\ 5207 5384 ©enable© \\ 5208 5385 ©fallthrough© \\ 5209 5386 ©fallthru© \\ 5210 ©finally© \\5211 ©forall© \\5212 5387 \end{tabular} 5213 5388 & 5214 5389 \begin{tabular}{@{}l@{}} 5390 ©finally© \\ 5391 ©forall© \\ 5215 5392 ©ftype© \\ 5216 5393 ©lvalue© \\ 5217 5394 ©monitor© \\ 5395 \end{tabular} 5396 & 5397 \begin{tabular}{@{}l@{}} 5218 5398 ©mutex© \\ 5219 5399 ©one_t© \\ 5220 5400 ©otype© \\ 5401 ©throw© \\ 5402 ©throwResume© \\ 5221 5403 \end{tabular} 5222 5404 & 5223 5405 \begin{tabular}{@{}l@{}} 5224 ©throw© \\5225 ©throwResume© \\5226 5406 ©trait© \\ 5227 5407 ©try© \\ 5228 5408 ©ttype© \\ 5229 5409 ©zero_t© \\ 5410 \\ 5230 5411 \end{tabular} 5231 5412 \end{tabular} … … 5402 5583 \Celeven prescribes the following standard header-files~\cite[\S~7.1.2]{C11} and \CFA adds to this list: 5403 5584 \begin{quote2} 5404 \lstset{deletekeywords={float}} 5405 \begin{tabular}{@{}llll|l@{}} 5406 \multicolumn{4}{c|}{C11} & \multicolumn{1}{c}{\CFA} \\ 5585 \begin{tabular}{@{}lllll|l@{}} 5586 \multicolumn{5}{c|}{C11} & \multicolumn{1}{c}{\CFA} \\ 5407 5587 \hline 5408 5588 \begin{tabular}{@{}l@{}} … … 5412 5592 \Indexc{errno.h} \\ 5413 5593 \Indexc{fenv.h} \\ 5414 \Indexc{float.h} \\ 5415 \Indexc{inttypes.h} \\ 5416 \Indexc{iso646.h} \\ 5594 \Indexc[deletekeywords=float]{float.h} \\ 5417 5595 \end{tabular} 5418 5596 & 5419 5597 \begin{tabular}{@{}l@{}} 5598 \Indexc{inttypes.h} \\ 5599 \Indexc{iso646.h} \\ 5420 5600 \Indexc{limits.h} \\ 5421 5601 \Indexc{locale.h} \\ 5422 5602 \Indexc{math.h} \\ 5423 5603 \Indexc{setjmp.h} \\ 5604 \end{tabular} 5605 & 5606 \begin{tabular}{@{}l@{}} 5424 5607 \Indexc{signal.h} \\ 5425 5608 \Indexc{stdalign.h} \\ 5426 5609 \Indexc{stdarg.h} \\ 5427 5610 \Indexc{stdatomic.h} \\ 5611 \Indexc{stdbool.h} \\ 5612 \Indexc{stddef.h} \\ 5428 5613 \end{tabular} 5429 5614 & 5430 5615 \begin{tabular}{@{}l@{}} 5431 \Indexc{stdbool.h} \\5432 \Indexc{stddef.h} \\5433 5616 \Indexc{stdint.h} \\ 5434 5617 \Indexc{stdio.h} \\ … … 5446 5629 \Indexc{wctype.h} \\ 5447 5630 \\ 5448 \\5449 \\5450 5631 \end{tabular} 5451 5632 & … … 5453 5634 \Indexc{unistd.h} \\ 5454 5635 \Indexc{gmp.h} \\ 5455 \\5456 \\5457 5636 \\ 5458 5637 \\ … … 5487 5666 For an increase in storage size, new storage after the copied data may be filled. 5488 5667 \item[alignment] 5489 an allocation starts on a specified memory boundary, e.g., an address multiple of 64 or 128 for cache-line purposes.5668 an allocation starts on a specified memory boundary, \eg, an address multiple of 64 or 128 for cache-line purposes. 5490 5669 \item[array] 5491 5670 the allocation size is scaled to the specified number of array elements. … … 5494 5673 The table shows allocation routines supporting different combinations of storage-management capabilities: 5495 5674 \begin{center} 5496 \begin{tabular}{@{} lr|l|l|l|l@{}}5497 && \multicolumn{1}{c|}{fill} & resize & alignment & array \\5675 \begin{tabular}{@{}r|r|l|l|l|l@{}} 5676 \multicolumn{1}{c}{}& & \multicolumn{1}{c|}{fill} & resize & alignment & array \\ 5498 5677 \hline 5499 5678 C & ©malloc© & no & no & no & no \\ … … 5502 5681 & ©memalign© & no & no & yes & no \\ 5503 5682 & ©posix_memalign© & no & no & yes & no \\ 5683 \hline 5504 5684 C11 & ©aligned_alloc© & no & no & yes & no \\ 5685 \hline 5505 5686 \CFA & ©alloc© & no/copy/yes & no/yes & no & yes \\ 5506 5687 & ©align_alloc© & no/yes & no & yes & yes \\ … … 5513 5694 // C unsafe allocation 5514 5695 extern "C" { 5515 void * mall ac( size_t size );§\indexc{memset}§5696 void * malloc( size_t size );§\indexc{memset}§ 5516 5697 void * calloc( size_t dim, size_t size );§\indexc{calloc}§ 5517 5698 void * realloc( void * ptr, size_t size );§\indexc{realloc}§
Note:
See TracChangeset
for help on using the changeset viewer.