- Timestamp:
- Jul 6, 2017, 4:18:58 PM (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:
- f3c1737
- Parents:
- bdeba0b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
rbdeba0b r955d27e9 1 1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 %% 2 %% 3 3 %% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo 4 4 %% 5 5 %% The contents of this file are covered under the licence agreement in the 6 6 %% file "LICENCE" distributed with Cforall. 7 %% 8 %% user.tex -- 9 %% 7 %% 8 %% user.tex -- 9 %% 10 10 %% Author : Peter A. Buhr 11 11 %% Created On : Wed Apr 6 14:53:29 2016 … … 57 57 \CFAStyle % use default CFA format-style 58 58 59 % inline code ©...©(copyright symbol) emacs: C-q M-)60 % red highlighting ®...®(registered trademark symbol) emacs: C-q M-.61 % blue highlighting ß...ß(sharp s symbol) emacs: C-q M-_62 % green highlighting ¢...¢(cent symbol) emacs: C-q M-"63 % LaTex escape §...§(section symbol) emacs: C-q M-'64 % keyword escape ¶...¶(pilcrow symbol) emacs: C-q M-^59 % inline code �...� (copyright symbol) emacs: C-q M-) 60 % red highlighting �...� (registered trademark symbol) emacs: C-q M-. 61 % blue highlighting �...� (sharp s symbol) emacs: C-q M-_ 62 % green highlighting �...� (cent symbol) emacs: C-q M-" 63 % LaTex escape �...� (section symbol) emacs: C-q M-' 64 % keyword escape �...� (pilcrow symbol) emacs: C-q M-^ 65 65 % math escape $...$ (dollar symbol) 66 66 … … 158 158 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CC}} \\ 159 159 \begin{cfa} 160 #include <fstream> §\indexc{fstream}§160 #include <fstream>�\indexc{fstream}� 161 161 162 162 int main( void ) { 163 163 int x = 0, y = 1, z = 2; 164 ®sout | x | y | z | endl;®164 �sout | x | y | z | endl;� 165 165 } 166 166 \end{cfa} 167 167 & 168 168 \begin{lstlisting} 169 #include <stdio.h> §\indexc{stdio.h}§169 #include <stdio.h>�\indexc{stdio.h}� 170 170 171 171 int main( void ) { 172 172 int x = 0, y = 1, z = 2; 173 ®printf( "%d %d %d\n", x, y, z );®173 �printf( "%d %d %d\n", x, y, z );� 174 174 } 175 175 \end{lstlisting} 176 176 & 177 177 \begin{lstlisting} 178 #include <iostream> §\indexc{iostream}§178 #include <iostream>�\indexc{iostream}� 179 179 using namespace std; 180 180 int main() { 181 181 int x = 0, y = 1, z = 2; 182 ®cout<<x<<" "<<y<<" "<<z<<endl;®182 �cout<<x<<" "<<y<<" "<<z<<endl;� 183 183 } 184 184 \end{lstlisting} … … 222 222 \CC~\cite{C++14,C++} is an example of a similar project; 223 223 however, it largely extended the language, and did not address many existing problems.\footnote{% 224 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.}224 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.} 225 225 \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. 226 226 \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. … … 242 242 (See~\cite{Werther96} for similar work in \Index*[C++]{\CC{}}.) 243 243 A first \CFA implementation of these extensions was by Esteves~\cite{Esteves04}. 244 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):244 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): 245 245 \begin{lstlisting} 246 ®forall( otype T )®T identity( T val ) { return val; }247 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§246 �forall( otype T )� T identity( T val ) { return val; } 247 int forty_two = identity( 42 ); �\C{// T is bound to int, forty\_two == 42}� 248 248 \end{lstlisting} 249 249 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. … … 264 264 265 265 \begin{comment} 266 A simple example is leveraging the existing type-unsafe ( ©void *©) C ©bsearch©to binary search a sorted floating-point array:266 A simple example is leveraging the existing type-unsafe (�void *�) C �bsearch� to binary search a sorted floating-point array: 267 267 \begin{lstlisting} 268 268 void * bsearch( const void * key, const void * base, size_t dim, size_t size, … … 288 288 int posn = bsearch( 5.0, vals, 10 ); 289 289 \end{lstlisting} 290 The nested function ©comp© provides the hidden interface from typed \CFA to untyped (©void *©) C, plus the cast of the result.291 Providing a hidden ©comp©function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope.290 The nested function �comp� provides the hidden interface from typed \CFA to untyped (�void *�) C, plus the cast of the result. 291 Providing a hidden �comp� function in \CC is awkward as lambdas do not use C calling-conventions and template declarations cannot appear at block scope. 292 292 As well, an alternate kind of return is made available: position versus pointer to found element. 293 \CC's type-system cannot disambiguate between the two versions of ©bsearch© because it does not use the return type in overload resolution, nor can \CC separately compile a templated ©bsearch©.293 \CC's type-system cannot disambiguate between the two versions of �bsearch� because it does not use the return type in overload resolution, nor can \CC separately compile a templated �bsearch�. 294 294 295 295 \CFA has replacement libraries condensing hundreds of existing C functions into tens of \CFA overloaded functions, all without rewriting the actual computations. 296 For example, it is possible to write a type-safe \CFA wrapper ©malloc© based on the C ©malloc©:296 For example, it is possible to write a type-safe \CFA wrapper �malloc� based on the C �malloc�: 297 297 \begin{lstlisting} 298 298 forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); } 299 int * ip = malloc(); §\C{// select type and size from left-hand side}§299 int * ip = malloc(); �\C{// select type and size from left-hand side}� 300 300 double * dp = malloc(); 301 301 struct S {...} * sp = malloc(); … … 305 305 306 306 However, it is necessary to differentiate between C and \CFA code because of name overloading, as for \CC. 307 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©.308 Whereas, \CFA wraps each of these routines into ones with the common name ©abs©:307 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�. 308 Whereas, \CFA wraps each of these routines into ones with the common name �abs�: 309 309 \begin{cfa} 310 310 char abs( char ); 311 ®extern "C" {® 312 int abs( int ); §\C{// use default C routine for int}§313 ®}®// extern "C"311 �extern "C" {� 312 int abs( int ); �\C{// use default C routine for int}� 313 �}� // extern "C" 314 314 long int abs( long int ); 315 315 long long int abs( long long int ); … … 321 321 long double _Complex abs( long double _Complex ); 322 322 \end{cfa} 323 The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©.324 Hence, names appearing in an ©extern "C"©block have \newterm*{C linkage}.323 The problem is the name clash between the library routine �abs� and the \CFA names �abs�. 324 Hence, names appearing in an �extern "C"� block have \newterm*{C linkage}. 325 325 Then overloading polymorphism uses a mechanism called \newterm{name mangling}\index{mangling!name} to create unique names that are different from C names, which are not mangled. 326 326 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. 327 327 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type. 328 328 This example strongly illustrates a core idea in \CFA: \emph{the power of a name}. 329 The name `` ©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value.330 Hence, knowing the name ©abs©should be sufficient to apply it to any type where it is applicable.329 The name ``�abs�'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value. 330 Hence, knowing the name �abs� should be sufficient to apply it to any type where it is applicable. 331 331 The time savings and safety of using one name uniformly versus $N$ unique names should not be underestimated. 332 332 … … 334 334 \section[Compiling CFA Program]{Compiling \CFA Program} 335 335 336 The command ©cfa©is used to compile \CFA program(s), and is based on the GNU \Indexc{gcc} command, \eg:337 \begin{cfa} 338 cfa §\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] C/§\CFA§-files [ assembler/loader-files ]339 \end{cfa} 340 \CFA programs having the following ©gcc©flags turned on:336 The command �cfa� is used to compile \CFA program(s), and is based on the GNU \Indexc{gcc} command, \eg: 337 \begin{cfa} 338 cfa�\indexc{cfa}\index{compilation!cfa@�cfa�}� [ gcc-options ] C/�\CFA�-files [ assembler/loader-files ] 339 \end{cfa} 340 \CFA programs having the following �gcc� flags turned on: 341 341 \begin{description} 342 342 \item 343 \Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{ ©-std=gnu99©}}343 \Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{�-std=gnu99�}} 344 344 The 1999 C standard plus GNU extensions. 345 345 \item 346 346 {\lstset{deletekeywords={inline}} 347 \Indexc{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{ ©-fgnu89-inline©}}347 \Indexc{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{�-fgnu89-inline�}} 348 348 Use the traditional GNU semantics for inline routines in C99 mode, which allows inline routines in header files. 349 349 }% … … 352 352 \begin{description} 353 353 \item 354 \Indexc{-CFA}\index{compilation option!-CFA@ ©-CFA©}354 \Indexc{-CFA}\index{compilation option!-CFA@�-CFA�} 355 355 Only the C preprocessor and the \CFA translator steps are performed and the transformed program is written to standard output, which makes it possible to examine the code generated by the \CFA translator. 356 356 The generated code started with the standard \CFA prelude. 357 357 358 358 \item 359 \Indexc{-debug}\index{compilation option!-debug@ ©-debug©}359 \Indexc{-debug}\index{compilation option!-debug@�-debug�} 360 360 The program is linked with the debugging version of the runtime system. 361 361 The debug version performs runtime checks to help during the debugging phase of a \CFA program, but substantially slows the execution of the program. … … 364 364 365 365 \item 366 \Indexc{-nodebug}\index{compilation option!-nodebug@ ©-nodebug©}366 \Indexc{-nodebug}\index{compilation option!-nodebug@�-nodebug�} 367 367 The program is linked with the non-debugging version of the runtime system, so the execution of the program is faster. 368 \Emph{However, no runtime checks or ©assert©s are performed so errors usually result in abnormal program termination.}369 370 \item 371 \Indexc{-help}\index{compilation option!-help@ ©-help©}368 \Emph{However, no runtime checks or �assert�s are performed so errors usually result in abnormal program termination.} 369 370 \item 371 \Indexc{-help}\index{compilation option!-help@�-help�} 372 372 Information about the set of \CFA compilation flags is printed. 373 373 374 374 \item 375 \Indexc{-nohelp}\index{compilation option!-nohelp@ ©-nohelp©}375 \Indexc{-nohelp}\index{compilation option!-nohelp@�-nohelp�} 376 376 Information about the set of \CFA compilation flags is not printed. 377 377 \textbf{This option is the default.} 378 378 379 379 \item 380 \Indexc{-quiet}\index{compilation option!-quiet@ ©-quiet©}380 \Indexc{-quiet}\index{compilation option!-quiet@�-quiet�} 381 381 The \CFA compilation message is not printed at the beginning of a compilation. 382 382 383 383 \item 384 \Indexc{-noquiet}\index{compilation option!-noquiet@ ©-noquiet©}384 \Indexc{-noquiet}\index{compilation option!-noquiet@�-noquiet�} 385 385 The \CFA compilation message is printed at the beginning of a compilation. 386 386 \textbf{This option is the default.} 387 387 388 388 \item 389 \Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@ ©-no-include-stdhdr©}390 Do not supply ©extern "C"©wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}).389 \Indexc{-no-include-stdhdr}\index{compilation option!-no-include-stdhdr@�-no-include-stdhdr�} 390 Do not supply �extern "C"� wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}). 391 391 \textbf{This option is \emph{not} the default.} 392 392 \end{description} … … 395 395 \begin{description} 396 396 \item 397 \Indexc{__CFA_MAJOR__}\index{preprocessor variables!__CFA__@{ ©__CFA__©}}397 \Indexc{__CFA_MAJOR__}\index{preprocessor variables!__CFA__@{�__CFA__�}} 398 398 is available during preprocessing and its value is the major \Index{version number} of \CFA.\footnote{ 399 399 The C preprocessor allows only integer values in a preprocessor variable so a value like ``\Version'' is not allowed. … … 401 401 402 402 \item 403 \Indexc{__CFA_MINOR__}\index{preprocessor variables!__CFA_MINOR__@{ ©__CFA_MINOR__©}}403 \Indexc{__CFA_MINOR__}\index{preprocessor variables!__CFA_MINOR__@{�__CFA_MINOR__�}} 404 404 is available during preprocessing and its value is the minor \Index{version number} of \CFA. 405 405 406 406 \item 407 \Indexc{__CFA_PATCH__}\index{preprocessor variables!__CFA_PATCH__@ ©__CFA_PATCH__©}407 \Indexc{__CFA_PATCH__}\index{preprocessor variables!__CFA_PATCH__@�__CFA_PATCH__�} 408 408 is available during preprocessing and its value is the patch \Index{level number} of \CFA. 409 409 410 410 \item 411 \Indexc{__CFA__}\index{preprocessor variables!__CFA__@ ©__CFA__©},412 \Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL__@ ©__CFORALL__©} and413 \Indexc{__cforall}\index{preprocessor variables!__cforall@ ©__cforall©}411 \Indexc{__CFA__}\index{preprocessor variables!__CFA__@�__CFA__�}, 412 \Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL__@�__CFORALL__�} and 413 \Indexc{__cforall}\index{preprocessor variables!__cforall@�__cforall�} 414 414 are always available during preprocessing and have no value. 415 415 \end{description} … … 418 418 \begin{cfa} 419 419 #ifndef __CFORALL__ 420 #include <stdio.h> §\indexc{stdio.h}§ §\C{// C header file}§420 #include <stdio.h>�\indexc{stdio.h}� �\C{// C header file}� 421 421 #else 422 #include <fstream> §\indexc{fstream}§ §\C{// \CFA header file}§422 #include <fstream>�\indexc{fstream}� �\C{// \CFA header file}� 423 423 #endif 424 424 \end{cfa} 425 which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. 425 which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. 426 426 427 427 … … 430 430 Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, \eg: 431 431 \begin{cfa} 432 2 ®_®147®_®483®_®648; §\C{// decimal constant}§433 56 ®_®ul; §\C{// decimal unsigned long constant}§434 0 ®_®377; §\C{// octal constant}§435 0x ®_®ff®_®ff; §\C{// hexadecimal constant}§436 0x ®_®ef3d®_®aa5c; §\C{// hexadecimal constant}§437 3.141 ®_®592®_®654; §\C{// floating point constant}§438 10 ®_®e®_®+1®_®00; §\C{// floating point constant}§439 0x ®_®ff®_®ff®_®p®_®3; §\C{// hexadecimal floating point}§440 0x ®_®1.ffff®_®ffff®_®p®_®128®_®l; §\C{// hexadecimal floating point long constant}§441 L ®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§432 2�_�147�_�483�_�648; �\C{// decimal constant}� 433 56�_�ul; �\C{// decimal unsigned long constant}� 434 0�_�377; �\C{// octal constant}� 435 0x�_�ff�_�ff; �\C{// hexadecimal constant}� 436 0x�_�ef3d�_�aa5c; �\C{// hexadecimal constant}� 437 3.141�_�592�_�654; �\C{// floating point constant}� 438 10�_�e�_�+1�_�00; �\C{// floating point constant}� 439 0x�_�ff�_�ff�_�p�_�3; �\C{// hexadecimal floating point}� 440 0x�_�1.ffff�_�ffff�_�p�_�128�_�l; �\C{// hexadecimal floating point long constant}� 441 L�_��"\texttt{\textbackslash{x}}��_��\texttt{ff}��_��\texttt{ee}"�; �\C{// wide character constant}� 442 442 \end{cfa} 443 443 The rules for placement of underscores is as follows: 444 444 \begin{enumerate} 445 445 \item 446 A sequence of underscores is disallowed, \eg ©12__34©is invalid.446 A sequence of underscores is disallowed, \eg �12__34� is invalid. 447 447 \item 448 448 Underscores may only appear within a sequence of digits (regardless of the digit radix). 449 In other words, an underscore cannot start or end a sequence of digits, \eg ©_1©, ©1_© and ©_1_©are invalid (actually, the 1st and 3rd examples are identifier names).449 In other words, an underscore cannot start or end a sequence of digits, \eg �_1�, �1_� and �_1_� are invalid (actually, the 1st and 3rd examples are identifier names). 450 450 \item 451 451 A numeric prefix may end with an underscore; 452 452 a numeric infix may begin and/or end with an underscore; 453 453 a numeric suffix may begin with an underscore. 454 For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©;455 the exponent infix ©E© may start or end with an underscore ©1.0_E10©, ©1.0E_10© or ©1.0_E_10©;456 the type suffixes ©U©, ©L©, etc. may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©.454 For example, the octal �0� or hexadecimal �0x� prefix may end with an underscore �0_377� or �0x_ff�; 455 the exponent infix �E� may start or end with an underscore �1.0_E10�, �1.0E_10� or �1.0_E_10�; 456 the type suffixes �U�, �L�, etc. may start with an underscore �1_U�, �1_ll� or �1.0E10_f�. 457 457 \end{enumerate} 458 458 It is significantly easier to read and enter long constants when they are broken up into smaller groupings (many cultures use comma and/or period among digits for the same purpose). … … 465 465 \CFA accommodates keyword clashes with existing C variable-names by syntactic transformations using the \CFA backquote escape-mechanism: 466 466 \begin{cfa} 467 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§468 double ®`®forall®`®= 3.5;467 int �`�otype�`� = 3; �\C{// make keyword an identifier}� 468 double �`�forall�`� = 3.5; 469 469 \end{cfa} 470 470 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. 471 \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©:471 \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�: 472 472 473 473 \begin{figure} 474 474 \begin{cfa} 475 475 // include file uses the CFA keyword "otype". 476 #if ! defined( otype ) §\C{// nesting ?}§477 #define otype ®`®otype®`® §\C{// make keyword an identifier}§476 #if ! defined( otype ) �\C{// nesting ?}� 477 #define otype �`�otype�`� �\C{// make keyword an identifier}� 478 478 #define __CFA_BFD_H__ 479 479 #endif // ! otype 480 480 481 # ®include_next® <bfd.h> §\C{// must have internal check for multiple expansion}§482 483 #if defined( otype ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§481 #�include_next� <bfd.h> �\C{// must have internal check for multiple expansion}� 482 483 #if defined( otype ) && defined( __CFA_BFD_H__ ) �\C{// reset only if set}� 484 484 #undef otype 485 485 #undef __CFA_BFD_H__ … … 493 493 \section{Labelled Continue/Break} 494 494 495 While C provides ©continue© and ©break©statements for altering control flow, both are restricted to one level of nesting for a particular control structure.495 While C provides �continue� and �break� statements for altering control flow, both are restricted to one level of nesting for a particular control structure. 496 496 Unfortunately, this restriction forces programmers to use \Indexc{goto} to achieve the equivalent control-flow for more than one level of nesting. 497 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}.498 For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do©statement;499 for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement.500 \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©.497 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}. 498 For both �continue� and �break�, the target label must be directly associated with a �for�, �while� or �do� statement; 499 for �break�, the target label can also be associated with a �switch�, �if� or compound (�{}�) statement. 500 \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�. 501 501 The innermost loop has 7 exit points, which cause resumption or termination of one or more of the 7 \Index{nested control-structure}s. 502 502 … … 505 505 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 506 506 \begin{cfa} 507 ®LC:®{508 ... §declarations§...509 ®LS:®switch ( ... ) {507 �LC:� { 508 ... �declarations� ... 509 �LS:� switch ( ... ) { 510 510 case 3: 511 ®LIF:®if ( ... ) {512 ®LF:®for ( ... ) {513 ®LW:®while ( ... ) {514 ... break ®LC®; ... // terminate compound515 ... break ®LS®; ... // terminate switch516 ... break ®LIF®; ... // terminate if517 ... continue ®LF;®... // resume loop518 ... break ®LF®; ... // terminate loop519 ... continue ®LW®; ... // resume loop520 ... break ®LW®; ... // terminate loop511 �LIF:� if ( ... ) { 512 �LF:� for ( ... ) { 513 �LW:� while ( ... ) { 514 ... break �LC�; ... // terminate compound 515 ... break �LS�; ... // terminate switch 516 ... break �LIF�; ... // terminate if 517 ... continue �LF;� ... // resume loop 518 ... break �LF�; ... // terminate loop 519 ... continue �LW�; ... // resume loop 520 ... break �LW�; ... // terminate loop 521 521 } // while 522 522 } // for 523 523 } else { 524 ... break ®LIF®; ... // terminate if524 ... break �LIF�; ... // terminate if 525 525 } // if 526 526 } // switch … … 530 530 \begin{cfa} 531 531 { 532 ... §declarations§...532 ... �declarations� ... 533 533 switch ( ... ) { 534 534 case 3: … … 536 536 for ( ... ) { 537 537 while ( ... ) { 538 ... goto ®LC®; ...539 ... goto ®LS®; ...540 ... goto ®LIF®; ...541 ... goto ®LFC®; ...542 ... goto ®LFB®; ...543 ... goto ®LWC®; ...544 ... goto ®LWB®; ...545 ®LWC®: ; } ®LWB:®;546 ®LFC:® ; } ®LFB:®;538 ... goto �LC�; ... 539 ... goto �LS�; ... 540 ... goto �LIF�; ... 541 ... goto �LFC�; ... 542 ... goto �LFB�; ... 543 ... goto �LWC�; ... 544 ... goto �LWB�; ... 545 �LWC�: ; } �LWB:� ; 546 �LFC:� ; } �LFB:� ; 547 547 } else { 548 ... goto ®LIF®; ...549 } ®L3:®;550 } ®LS:®;551 } ®LC:®;548 ... goto �LIF�; ... 549 } �L3:� ; 550 } �LS:� ; 551 } �LC:� ; 552 552 \end{cfa} 553 553 \end{tabular} … … 606 606 607 607 608 Both labelled ©continue© and ©break© are a ©goto©\index{goto@\lstinline $goto$!restricted} restricted in the following ways:608 Both labelled �continue� and �break� are a �goto�\index{goto@\lstinline $goto$!restricted} restricted in the following ways: 609 609 \begin{itemize} 610 610 \item … … 615 615 This restriction prevents missing initialization at the start of a control structure resulting in undefined behaviour. 616 616 \end{itemize} 617 The advantage of the labelled ©continue©/©break© is allowing static multi-level exits without having to use the ©goto©statement, and tying control flow to the target control structure rather than an arbitrary point in a program.617 The advantage of the labelled �continue�/�break� is allowing static multi-level exits without having to use the �goto� statement, and tying control flow to the target control structure rather than an arbitrary point in a program. 618 618 Furthermore, the location of the label at the \emph{beginning} of the target control structure informs the reader that complex control-flow is occurring in the body of the control structure. 619 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader.619 With �goto�, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. 620 620 Finally, using an explicit target for the transfer instead of an implicit target allows new constructs to be added or removed without affecting existing constructs. 621 The implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed.621 The implicit targets of the current �continue� and �break�, \ie the closest enclosing loop or �switch�, change as certain constructs are added or removed. 622 622 623 623 624 624 \section{Switch Statement} 625 625 626 C allows a number of questionable forms for the ©switch©statement:626 C allows a number of questionable forms for the �switch� statement: 627 627 \begin{enumerate} 628 628 \item 629 By default, the end of a ©case©clause\footnote{630 In this section, the term \emph{case clause} refers to either a ©case© or ©default©clause.}631 \emph{falls through} to the next ©case© clause in the ©switch©statement;632 to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©:629 By default, the end of a �case� clause\footnote{ 630 In this section, the term \emph{case clause} refers to either a �case� or �default� clause.} 631 \emph{falls through} to the next �case� clause in the �switch� statement; 632 to exit a �switch� statement from a �case� clause requires explicitly terminating the clause with a transfer statement, most commonly �break�: 633 633 \begin{cfa} 634 634 switch ( i ) { … … 661 661 if ( argc == 3 ) { 662 662 // open output file 663 ®// open input file664 ®} else if ( argc == 2 ) {665 ®// open input file666 667 ®} else {663 �// open input file 664 �} else if ( argc == 2 ) { 665 �// open input file 666 667 �} else { 668 668 // usage message 669 669 } … … 672 672 \end{quote2} 673 673 In this example, case 2 is always done if case 3 is done. 674 This control flow is difficult to simulate with if statements or a ©switch©statement without fall-through as code must be duplicated or placed in a separate routine.674 This control flow is difficult to simulate with if statements or a �switch� statement without fall-through as code must be duplicated or placed in a separate routine. 675 675 C also uses fall-through to handle multiple case-values resulting in the same action: 676 676 \begin{cfa} … … 685 685 \end{cfa} 686 686 However, this situation is handled in other languages without fall-through by allowing a list of case values. 687 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.688 Hence, default fall-through semantics results in a large number of programming errors as programmers often forget the ©break© statement at the end of a ©case©clause, resulting in inadvertent fall-through.689 690 \item 691 It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch©statement:687 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. 688 Hence, default fall-through semantics results in a large number of programming errors as programmers often forget the �break� statement at the end of a �case� clause, resulting in inadvertent fall-through. 689 690 \item 691 It is possible to place �case� clauses on statements nested \emph{within} the body of the �switch� statement: 692 692 \begin{cfa} 693 693 switch ( i ) { … … 695 695 if ( j < k ) { 696 696 ... 697 ®case 1:®// transfer into "if" statement697 �case 1:� // transfer into "if" statement 698 698 ... 699 699 } // if … … 701 701 while ( j < 5 ) { 702 702 ... 703 ®case 3:®// transfer into "while" statement703 �case 3:� // transfer into "while" statement 704 704 ... 705 705 } // while … … 726 726 } 727 727 \end{cfa} 728 which unrolls a loop N times (N = 8 above) and uses the ©switch©statement to deal with any iterations not a multiple of N.728 which unrolls a loop N times (N = 8 above) and uses the �switch� statement to deal with any iterations not a multiple of N. 729 729 While efficient, this sort of special purpose usage is questionable: 730 730 \begin{quote} … … 733 733 \end{quote} 734 734 \item 735 It is possible to place the ©default© clause anywhere in the list of labelled clauses for a ©switch©statement, rather than only at the end.736 Virtually all programming languages with a ©switch© statement require the ©default©clause to appear last in the case-clause list.737 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default©clause is selected;738 hence, physically placing the ©default© clause at the end of the ©case©clause list matches with this semantics.739 This physical placement can be compared to the physical placement of an ©else© clause at the end of a series of connected ©if©/©else©statements.740 741 \item 742 It is possible to place unreachable code at the start of a ©switch©statement, as in:735 It is possible to place the �default� clause anywhere in the list of labelled clauses for a �switch� statement, rather than only at the end. 736 Virtually all programming languages with a �switch� statement require the �default� clause to appear last in the case-clause list. 737 The logic for this semantics is that after checking all the �case� clauses without success, the �default� clause is selected; 738 hence, physically placing the �default� clause at the end of the �case� clause list matches with this semantics. 739 This physical placement can be compared to the physical placement of an �else� clause at the end of a series of connected �if�/�else� statements. 740 741 \item 742 It is possible to place unreachable code at the start of a �switch� statement, as in: 743 743 \begin{cfa} 744 744 switch ( x ) { 745 ®int y = 1;® §\C{// unreachable initialization}§746 ®x = 7;® §\C{// unreachable code without label/branch}§745 �int y = 1;� �\C{// unreachable initialization}� 746 �x = 7;� �\C{// unreachable code without label/branch}� 747 747 case 3: ... 748 748 ... 749 ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§749 �int z = 0;� �\C{// unreachable initialization, cannot appear after case}� 750 750 z = 2; 751 751 case 3: 752 ®x = z;® §\C{// without fall through, z is uninitialized}§753 } 754 \end{cfa} 755 While the declaration of the local variable ©y© is useful with a scope across all ©case©clauses, the initialization for such a variable is defined to never be executed because control always transfers over it.756 Furthermore, any statements before the first ©case© clause can only be executed if labelled and transferred to using a ©goto©, either from outside or inside of the ©switch©, both of which are problematic.757 As well, the declaration of ©z© cannot occur after the ©case© because a label can only be attached to a statement, and without a fall through to case 3, ©z©is uninitialized.758 The key observation is that the ©switch©statement branches into control structure, \ie there are multiple entry points into its statement body.752 �x = z;� �\C{// without fall through, z is uninitialized}� 753 } 754 \end{cfa} 755 While the declaration of the local variable �y� is useful with a scope across all �case� clauses, the initialization for such a variable is defined to never be executed because control always transfers over it. 756 Furthermore, any statements before the first �case� clause can only be executed if labelled and transferred to using a �goto�, either from outside or inside of the �switch�, both of which are problematic. 757 As well, the declaration of �z� cannot occur after the �case� because a label can only be attached to a statement, and without a fall through to case 3, �z� is uninitialized. 758 The key observation is that the �switch� statement branches into control structure, \ie there are multiple entry points into its statement body. 759 759 \end{enumerate} 760 760 … … 762 762 \begin{itemize} 763 763 \item 764 the number of ©switch©statements is small,765 \item 766 most ©switch©statements are well formed (\ie no \Index*{Duff's device}),767 \item 768 the ©default©clause is usually written as the last case-clause,769 \item 770 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.764 the number of �switch� statements is small, 765 \item 766 most �switch� statements are well formed (\ie no \Index*{Duff's device}), 767 \item 768 the �default� clause is usually written as the last case-clause, 769 \item 770 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. 771 771 \end{itemize} 772 These observations help to put the \CFA changes to the ©switch©into perspective.772 These observations help to put the \CFA changes to the �switch� into perspective. 773 773 \begin{enumerate} 774 774 \item 775 775 Eliminating default fall-through has the greatest potential for affecting existing code. 776 However, even if fall-through is removed, most ©switch© statements would continue to work because of the explicit transfers already present at the end of each ©case© clause, the common placement of the ©default© clause at the end of the case list, and the most common use of fall-through, \ie a list of ©case©clauses executing common code, \eg:776 However, even if fall-through is removed, most �switch� statements would continue to work because of the explicit transfers already present at the end of each �case� clause, the common placement of the �default� clause at the end of the case list, and the most common use of fall-through, \ie a list of �case� clauses executing common code, \eg: 777 777 \begin{cfa} 778 778 case 1: case 2: case 3: ... … … 780 780 still works. 781 781 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. 782 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:783 \begin{cfa} 784 ®choose®( i ) {782 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: 783 \begin{cfa} 784 �choose� ( i ) { 785 785 case 1: case 2: case 3: 786 786 ... 787 ®// implicit end of switch (break)788 ®case 5:787 �// implicit end of switch (break) 788 �case 5: 789 789 ... 790 ®fallthru®; §\C{// explicit fall through}§790 �fallthru�; �\C{// explicit fall through}� 791 791 case 7: 792 792 ... 793 ®break® §\C{// explicit end of switch}§793 �break� �\C{// explicit end of switch}� 794 794 default: 795 795 j = 3; 796 796 } 797 797 \end{cfa} 798 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case©clauses;799 the implicit ©break© is applied only at the end of the \emph{statements} following a ©case©clause.800 The 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.801 As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch©statement, and expected by C programmers.802 \item 803 \Index*{Duff's device} is eliminated from both ©switch© and ©choose©statements, and only invalidates a small amount of very questionable code.804 Hence, the ©case© clause must appear at the same nesting level as the ©switch©/©choose© body, as is done in most other programming languages with ©switch©statements.805 \item 806 The issue of ©default© at locations other than at the end of the cause clause can be solved by using good programming style, and there are a few reasonable situations involving fall-through where the ©default©clause needs to appear is locations other than at the end.798 Like the �switch� statement, the �choose� statement retains the fall-through semantics for a list of �case� clauses; 799 the implicit �break� is applied only at the end of the \emph{statements} following a �case� clause. 800 The 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. 801 As well, allowing an explicit �break� from the �choose� is a carry over from the �switch� statement, and expected by C programmers. 802 \item 803 \Index*{Duff's device} is eliminated from both �switch� and �choose� statements, and only invalidates a small amount of very questionable code. 804 Hence, the �case� clause must appear at the same nesting level as the �switch�/�choose� body, as is done in most other programming languages with �switch� statements. 805 \item 806 The issue of �default� at locations other than at the end of the cause clause can be solved by using good programming style, and there are a few reasonable situations involving fall-through where the �default� clause needs to appear is locations other than at the end. 807 807 Therefore, no change is made for this issue. 808 808 \item 809 Dealing with unreachable code in a ©switch©/©choose© body is solved by restricting declarations and associated initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate ©case©clause\footnote{810 Essentially, these declarations are hoisted before the ©switch©/©choose© statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first ©case©clause.809 Dealing with unreachable code in a �switch�/�choose� body is solved by restricting declarations and associated initialization to the start of statement body, which is executed \emph{before} the transfer to the appropriate �case� clause\footnote{ 810 Essentially, these declarations are hoisted before the �switch�/�choose� statement and both declarations and statement are surrounded by a compound statement.} and precluding statements before the first �case� clause. 811 811 Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound. 812 812 \begin{cfa} 813 813 switch ( x ) { 814 ®int i = 0;® §\C{// allowed only at start}§814 �int i = 0;� �\C{// allowed only at start}� 815 815 case 0: 816 816 ... 817 ®int j = 0;® §\C{// disallowed}§817 �int j = 0;� �\C{// disallowed}� 818 818 case 1: 819 819 { 820 ®int k = 0;® §\C{// allowed at different nesting levels}§820 �int k = 0;� �\C{// allowed at different nesting levels}� 821 821 ... 822 822 } … … 829 829 \section{Case Clause} 830 830 831 C restricts the ©case© clause of a ©switch©statement to a single value.832 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case©clauses rather than multiple values.833 Requiring a ©case©clause for each value does not seem to be in the spirit of brevity normally associated with C.834 Therefore, the ©case©clause is extended with a list of values, as in:831 C restricts the �case� clause of a �switch� statement to a single value. 832 For multiple �case� clauses associated with the same statement, it is necessary to have multiple �case� clauses rather than multiple values. 833 Requiring a �case� clause for each value does not seem to be in the spirit of brevity normally associated with C. 834 Therefore, the �case� clause is extended with a list of values, as in: 835 835 \begin{quote2} 836 836 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} … … 838 838 \begin{cfa} 839 839 switch ( i ) { 840 case ®1, 3, 5®:840 case �1, 3, 5�: 841 841 ... 842 case ®2, 4, 6®:842 case �2, 4, 6�: 843 843 ... 844 844 } … … 871 871 \begin{cfa} 872 872 switch ( i ) { 873 case ®1~5:®873 case �1~5:� 874 874 ... 875 case ®10~15:®875 case �10~15:� 876 876 ... 877 877 } … … 880 880 \begin{cfa} 881 881 switch ( i ) 882 case ®1 ... 5®:882 case �1 ... 5�: 883 883 ... 884 case ®10 ... 15®:884 case �10 ... 15�: 885 885 ... 886 886 } … … 899 899 Lists of subranges are also allowed. 900 900 \begin{cfa} 901 case ®1~5, 12~21, 35~42®:901 case �1~5, 12~21, 35~42�: 902 902 \end{cfa} 903 903 … … 943 943 catch( Arithmetic ) 944 944 \end{cfa} 945 matches all three derived exception-types: ©DivideByZero©, ©Overflow©, and ©Underflow©.945 matches all three derived exception-types: �DivideByZero�, �Overflow�, and �Underflow�. 946 946 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: 947 947 \begin{cfa} … … 976 976 For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: 977 977 \begin{cfa} 978 int ®(*®f®())[®5®]® {...}; §\C{definition}§979 ... ®(*®f®())[®3®]® += 1; §\C{usage}§978 int �(*�f�())[�5�]� {...}; �\C{definition}� 979 ... �(*�f�())[�3�]� += 1; �\C{usage}� 980 980 \end{cfa} 981 981 Essentially, the return type is wrapped around the routine name in successive layers (like an \Index{onion}). … … 990 990 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 991 991 \begin{cfa} 992 ß[5] *ß ®int®x1;993 ß* [5]ß ®int®x2;994 ß[* [5] int]ß f®( int p )®;992 �[5] *� �int� x1; 993 �* [5]� �int� x2; 994 �[* [5] int]� f�( int p )�; 995 995 \end{cfa} 996 996 & 997 997 \begin{cfa} 998 ®int® ß*ß x1 ß[5]ß;999 ®int® ß(*ßx2ß)[5]ß;1000 ßint (*ßf®( int p )®ß)[5]ß;998 �int� �*� x1 �[5]�; 999 �int� �(*�x2�)[5]�; 1000 �int (*�f�( int p )��)[5]�; 1001 1001 \end{cfa} 1002 1002 \end{tabular} 1003 1003 \end{quote2} 1004 1004 The only exception is \Index{bit field} specification, which always appear to the right of the base type. 1005 % 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.1005 % 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. 1006 1006 However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list. 1007 For instance, variables ©x© and ©y©of type \Index{pointer} to integer are defined in \CFA as follows:1007 For instance, variables �x� and �y� of type \Index{pointer} to integer are defined in \CFA as follows: 1008 1008 \begin{quote2} 1009 1009 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1010 1010 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1011 1011 \begin{cfa} 1012 ®*®int x, y;1012 �*� int x, y; 1013 1013 \end{cfa} 1014 1014 & 1015 1015 \begin{cfa} 1016 int ®*®x, ®*®y;1016 int �*�x, �*�y; 1017 1017 \end{cfa} 1018 1018 \end{tabular} … … 1023 1023 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1024 1024 \begin{cfa} 1025 ®*®int x;1025 �*� int x; 1026 1026 int y; 1027 1027 \end{cfa} 1028 1028 & 1029 1029 \begin{cfa} 1030 int ®*®x, y;1030 int �*�x, y; 1031 1031 1032 1032 \end{cfa} … … 1073 1073 \end{quote2} 1074 1074 1075 All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg:1075 All type qualifiers, \eg �const�, �volatile�, etc., are used in the normal way with the new declarations and also appear left to right, \eg: 1076 1076 \begin{quote2} 1077 1077 \begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}} … … 1093 1093 \end{tabular} 1094 1094 \end{quote2} 1095 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}1095 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} 1096 1096 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: 1097 1097 \begin{quote2} … … 1115 1115 \end{quote2} 1116 1116 1117 The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine ©sizeof©:1117 The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine �sizeof�: 1118 1118 \begin{quote2} 1119 1119 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 1120 1120 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1121 1121 \begin{cfa} 1122 y = ( ®* int®)x;1123 i = sizeof( ®[ 5 ] * int®);1122 y = (�* int�)x; 1123 i = sizeof(�[ 5 ] * int�); 1124 1124 \end{cfa} 1125 1125 & 1126 1126 \begin{cfa} 1127 y = ( ®int *®)x;1128 i = sizeof( ®int * [ 5 ]®);1127 y = (�int *�)x; 1128 i = sizeof(�int * [ 5 ]�); 1129 1129 \end{cfa} 1130 1130 \end{tabular} … … 1142 1142 These types may be derived from an object or routine type, called the \newterm{referenced type}. 1143 1143 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. 1144 An integer constant expression with the value 0, or such an expression cast to type ©void *©, is called a \newterm{null-pointer constant}.\footnote{1144 An integer constant expression with the value 0, or such an expression cast to type �void *�, is called a \newterm{null-pointer constant}.\footnote{ 1145 1145 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; 1146 1146 \ie the null pointer is guaranteed to compare unequal to a pointer to any object or routine.} … … 1162 1162 & 1163 1163 \begin{cfa} 1164 int * ®const®x = (int *)1001164 int * �const� x = (int *)100 1165 1165 *x = 3; // implicit dereference 1166 int * ®const®y = (int *)104;1166 int * �const� y = (int *)104; 1167 1167 *y = *x; // implicit dereference 1168 1168 \end{cfa} … … 1171 1171 where the right example is how the compiler logically interprets the variables in the left example. 1172 1172 Since a variable name only points to one address during its lifetime, it is an \Index{immutable} \Index{pointer}; 1173 hence, the implicit type of pointer variables ©x© and ©y©are constant pointers in the compiler interpretation.1173 hence, the implicit type of pointer variables �x� and �y� are constant pointers in the compiler interpretation. 1174 1174 In general, variable addresses are stored in instructions instead of loaded from memory, and hence may not occupy storage. 1175 1175 These approaches are contrasted in the following: … … 1194 1194 \end{quote2} 1195 1195 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. 1196 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}.1196 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}. 1197 1197 1198 1198 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. … … 1202 1202 \begin{tabular}{@{}l@{\hspace{2em}}l@{}} 1203 1203 \begin{cfa} 1204 int x, y, ®*® p1, ®*® p2, ®**®p3;1205 p1 = ®&®x; // p1 points to x1204 int x, y, �*� p1, �*� p2, �**� p3; 1205 p1 = �&�x; // p1 points to x 1206 1206 p2 = p1; // p2 points to x 1207 p1 = ®&®y; // p1 points to y1207 p1 = �&�y; // p1 points to y 1208 1208 p3 = &p2; // p3 points to p2 1209 1209 \end{cfa} … … 1217 1217 For example, \Index*{Algol68}~\cite{Algol68} infers pointer dereferencing to select the best meaning for each pointer usage 1218 1218 \begin{cfa} 1219 p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§1220 \end{cfa} 1221 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.1222 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 1219 p2 = p1 + x; �\C{// compiler infers *p2 = *p1 + x;}� 1220 \end{cfa} 1221 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. 1222 Unfortunately, automatic dereferencing does not work in all cases, and so some mechanism is necessary to fix incorrect choices. 1223 1223 1224 1224 Rather than inferring dereference, most programming languages pick one implicit dereferencing semantics, and the programmer explicitly indicates the other to resolve address-duality. 1225 1225 In C, objects of pointer type always manipulate the pointer object's address: 1226 1226 \begin{cfa} 1227 p1 = p2; §\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}§1228 p2 = p1 + x; §\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}§1229 \end{cfa} 1230 even though the assignment to ©p2©is likely incorrect, and the programmer probably meant:1231 \begin{cfa} 1232 p1 = p2; §\C{// pointer address assignment}§1233 ®*®p2 = ®*®p1 + x; §\C{// pointed-to value assignment / operation}§ 1234 \end{cfa} 1235 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©).1227 p1 = p2; �\C{// p1 = p2\ \ rather than\ \ *p1 = *p2}� 1228 p2 = p1 + x; �\C{// p2 = p1 + x\ \ rather than\ \ *p2 = *p1 + x}� 1229 \end{cfa} 1230 even though the assignment to �p2� is likely incorrect, and the programmer probably meant: 1231 \begin{cfa} 1232 p1 = p2; �\C{// pointer address assignment}� 1233 �*�p2 = �*�p1 + x; �\C{// pointed-to value assignment / operation}� 1234 \end{cfa} 1235 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�). 1236 1236 1237 1237 However, in most other situations, the pointed-to value is requested more often than the pointer address. … … 1245 1245 \end{cfa} 1246 1246 1247 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).1248 \begin{cfa} 1249 int x, y, ®&® r1, ®&® r2, ®&&®r3;1250 ®&®r1 = &x; §\C{// r1 points to x}§ 1251 ®&®r2 = &r1; §\C{// r2 points to x}§ 1252 ®&®r1 = &y; §\C{// r1 points to y}§ 1253 ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§ 1254 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§1247 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). 1248 \begin{cfa} 1249 int x, y, �&� r1, �&� r2, �&&� r3; 1250 �&�r1 = &x; �\C{// r1 points to x}� 1251 �&�r2 = &r1; �\C{// r2 points to x}� 1252 �&�r1 = &y; �\C{// r1 points to y}� 1253 �&&�r3 = �&�&r2; �\C{// r3 points to r2}� 1254 r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); �\C{// implicit dereferencing}� 1255 1255 \end{cfa} 1256 1256 Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example. … … 1258 1258 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: 1259 1259 \begin{cfa} 1260 ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15);1261 \end{cfa} 1262 When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out.1260 �*�r2 = ((�*�r1 + �*�r2) �*� (�**�r3 - �*�r1)) / (�**�r3 - 15); 1261 \end{cfa} 1262 When a reference operation appears beside a dereference operation, \eg �&*�, they cancel out. 1263 1263 However, in C, the cancellation always yields a value (\Index{rvalue}).\footnote{ 1264 The unary ©&©operator yields the address of its operand.1264 The unary �&� operator yields the address of its operand. 1265 1265 If the operand has type ``type'', the result has type ``pointer to type''. 1266 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}}1266 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}} 1267 1267 For a \CFA reference type, the cancellation on the left-hand side of assignment leaves the reference as an address (\Index{lvalue}): 1268 1268 \begin{cfa} 1269 (& ®*®)r1 = &x; §\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}§1269 (&�*�)r1 = &x; �\C{// (\&*) cancel giving address in r1 not variable pointed-to by r1}� 1270 1270 \end{cfa} 1271 1271 Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): 1272 1272 \begin{cfa} 1273 (&(& ®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}§1273 (&(&�*�)�*�)r3 = &(&�*�)r2; �\C{// (\&*) cancel giving address in r2, (\&(\&*)*) cancel giving address in r3}� 1274 1274 \end{cfa} 1275 1275 Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth. … … 1279 1279 int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, 1280 1280 &r1 = x, &&r2 = r1, &&&r3 = r2; 1281 ***p3 = 3; §\C{// change x}§1282 r3 = 3; §\C{// change x, ***r3}§1283 **p3 = ...; §\C{// change p1}§1284 &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§1285 *p3 = ...; §\C{// change p2}§1286 &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§1287 &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§1281 ***p3 = 3; �\C{// change x}� 1282 r3 = 3; �\C{// change x, ***r3}� 1283 **p3 = ...; �\C{// change p1}� 1284 &r3 = ...; �\C{// change r1, (\&*)**r3, 1 cancellation}� 1285 *p3 = ...; �\C{// change p2}� 1286 &&r3 = ...; �\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}� 1287 &&&r3 = p3; �\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}� 1288 1288 \end{cfa} 1289 1289 Furthermore, both types are equally performant, as the same amount of dereferencing occurs for both types. … … 1292 1292 As for a pointer type, a reference type may have qualifiers: 1293 1293 \begin{cfa} 1294 const int cx = 5; §\C{// cannot change cx;}§1295 const int & cr = cx; §\C{// cannot change what cr points to}§1296 ®&®cr = &cx; §\C{// can change cr}§ 1297 cr = 7; §\C{// error, cannot change cx}§1298 int & const rc = x; §\C{// must be initialized}§1299 ®&®rc = &x; §\C{// error, cannot change rc}§ 1300 const int & const crc = cx; §\C{// must be initialized}§1301 crc = 7; §\C{// error, cannot change cx}§1302 ®&®crc = &cx; §\C{// error, cannot change crc}§ 1303 \end{cfa} 1304 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}:1305 \begin{cfa} 1306 int & const cr = *0; §\C{// where 0 is the int * zero}§1294 const int cx = 5; �\C{// cannot change cx;}� 1295 const int & cr = cx; �\C{// cannot change what cr points to}� 1296 �&�cr = &cx; �\C{// can change cr}� 1297 cr = 7; �\C{// error, cannot change cx}� 1298 int & const rc = x; �\C{// must be initialized}� 1299 �&�rc = &x; �\C{// error, cannot change rc}� 1300 const int & const crc = cx; �\C{// must be initialized}� 1301 crc = 7; �\C{// error, cannot change cx}� 1302 �&�crc = &cx; �\C{// error, cannot change crc}� 1303 \end{cfa} 1304 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}: 1305 \begin{cfa} 1306 int & const cr = *0; �\C{// where 0 is the int * zero}� 1307 1307 \end{cfa} 1308 1308 Note, constant reference-types do not prevent \Index{addressing errors} because of explicit storage-management: … … 1311 1311 cr = 5; 1312 1312 free( &cr ); 1313 cr = 7; §\C{// unsound pointer dereference}§1314 \end{cfa} 1315 1316 The position of the ©const©qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers.1317 The ©const©qualifier cannot be moved before the pointer/reference qualifier for C style-declarations;1313 cr = 7; �\C{// unsound pointer dereference}� 1314 \end{cfa} 1315 1316 The position of the �const� qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers. 1317 The �const� qualifier cannot be moved before the pointer/reference qualifier for C style-declarations; 1318 1318 \CFA-style declarations (see \VRef{s:Declarations}) attempt to address this issue: 1319 1319 \begin{quote2} … … 1321 1321 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ 1322 1322 \begin{cfa} 1323 ®const® * ®const®* const int ccp;1324 ®const® & ®const®& const int ccr;1323 �const� * �const� * const int ccp; 1324 �const� & �const� & const int ccr; 1325 1325 \end{cfa} 1326 1326 & 1327 1327 \begin{cfa} 1328 const int * ®const® * ®const®ccp;1328 const int * �const� * �const� ccp; 1329 1329 1330 1330 \end{cfa} … … 1335 1335 Finally, like pointers, references are usable and composable with other type operators and generators. 1336 1336 \begin{cfa} 1337 int w, x, y, z, & ar[3] = { x, y, z }; §\C{// initialize array of references}§1338 &ar[1] = &w; §\C{// change reference array element}§1339 typeof( ar[1] ) p; §\C{// (gcc) is int, i.e., the type of referenced object}§1340 typeof( &ar[1] ) q; §\C{// (gcc) is int \&, i.e., the type of reference}§1341 sizeof( ar[1] ) == sizeof( int ); §\C{// is true, i.e., the size of referenced object}§1342 sizeof( &ar[1] ) == sizeof( int *) §\C{// is true, i.e., the size of a reference}§1343 \end{cfa} 1344 1345 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}.1337 int w, x, y, z, & ar[3] = { x, y, z }; �\C{// initialize array of references}� 1338 &ar[1] = &w; �\C{// change reference array element}� 1339 typeof( ar[1] ) p; �\C{// (gcc) is int, i.e., the type of referenced object}� 1340 typeof( &ar[1] ) q; �\C{// (gcc) is int \&, i.e., the type of reference}� 1341 sizeof( ar[1] ) == sizeof( int ); �\C{// is true, i.e., the size of referenced object}� 1342 sizeof( &ar[1] ) == sizeof( int *) �\C{// is true, i.e., the size of a reference}� 1343 \end{cfa} 1344 1345 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}. 1346 1346 Also, \CC does not allow \Index{array}s\index{array!reference} of reference\footnote{ 1347 1347 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.} … … 1357 1357 Therefore, for pointer/reference initialization, the initializing value must be an address not a value. 1358 1358 \begin{cfa} 1359 int * p = &x; §\C{// assign address of x}§1360 ®int * p = x;® §\C{// assign value of x}§ 1361 int & r = x; §\C{// must have address of x}§1362 \end{cfa} 1363 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).1359 int * p = &x; �\C{// assign address of x}� 1360 �int * p = x;� �\C{// assign value of x}� 1361 int & r = x; �\C{// must have address of x}� 1362 \end{cfa} 1363 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). 1364 1364 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. 1365 1365 Note, this is strictly a convenience and safety feature for a programmer. 1366 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.1367 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.1366 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. 1367 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. 1368 1368 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. 1369 1369 \begin{cfa} 1370 int & f( int & r ); §\C{// reference parameter and return}§1371 z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§1372 \end{cfa} 1373 Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©r© can be locally reassigned within ©f©.1374 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.1370 int & f( int & r ); �\C{// reference parameter and return}� 1371 z = f( x ) + f( y ); �\C{// reference operator added, temporaries needed for call results}� 1372 \end{cfa} 1373 Within routine �f�, it is possible to change the argument by changing the corresponding parameter, and parameter �r� can be locally reassigned within �f�. 1374 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. 1375 1375 \begin{cfa} 1376 1376 int temp1 = f( x ), temp2 = f( y ); … … 1380 1380 otherwise references have the same syntactic burden as pointers in these contexts. 1381 1381 1382 When a pointer/reference parameter has a ©const©value (immutable), it is possible to pass literals and expressions.1383 \begin{cfa} 1384 void f( ®const®int & cr );1385 void g( ®const®int * cp );1386 f( 3 ); g( ®&®3 );1387 f( x + y ); g( ®&®(x + y) );1388 \end{cfa} 1389 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.1390 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©).1391 Importantly, ©&3© may not be equal to ©&3©, where the references occur across calls because the temporaries maybe different on each call.1382 When a pointer/reference parameter has a �const� value (immutable), it is possible to pass literals and expressions. 1383 \begin{cfa} 1384 void f( �const� int & cr ); 1385 void g( �const� int * cp ); 1386 f( 3 ); g( �&�3 ); 1387 f( x + y ); g( �&�(x + y) ); 1388 \end{cfa} 1389 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. 1390 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�). 1391 Importantly, �&3� may not be equal to �&3�, where the references occur across calls because the temporaries maybe different on each call. 1392 1392 1393 1393 \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{ 1394 If whole program analysis is possible, and shows the parameter is not assigned, \ie it is ©const©, the temporary is unnecessary.}1394 If whole program analysis is possible, and shows the parameter is not assigned, \ie it is �const�, the temporary is unnecessary.} 1395 1395 \begin{cfa} 1396 1396 void f( int & r ); 1397 1397 void g( int * p ); 1398 f( 3 ); g( ®&®3 ); §\C{// compiler implicit generates temporaries}§1399 f( x + y ); g( ®&®(x + y) ); §\C{// compiler implicit generates temporaries}§1398 f( 3 ); g( �&�3 ); �\C{// compiler implicit generates temporaries}� 1399 f( x + y ); g( �&�(x + y) ); �\C{// compiler implicit generates temporaries}� 1400 1400 \end{cfa} 1401 1401 Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ 1402 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.}1402 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.} 1403 1403 The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. 1404 1404 … … 1408 1408 \begin{cfa} 1409 1409 void f( int i ); 1410 void (*fp)( int ); §\C{// routine pointer}§1411 fp = f; §\C{// reference initialization}§1412 fp = &f; §\C{// pointer initialization}§1413 fp = *f; §\C{// reference initialization}§1414 fp(3); §\C{// reference invocation}§1415 (*fp)(3); §\C{// pointer invocation}§1416 \end{cfa} 1417 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.1418 Instead, a routine object should be referenced by a ©const©reference:1419 \begin{cfa} 1420 ®const® void (®&® fr)( int ) = f; §\C{// routine reference}§ 1421 fr = ... §\C{// error, cannot change code}§1422 &fr = ...; §\C{// changing routine reference}§1423 fr( 3 ); §\C{// reference call to f}§1424 (*fr)(3); §\C{// error, incorrect type}§1410 void (*fp)( int ); �\C{// routine pointer}� 1411 fp = f; �\C{// reference initialization}� 1412 fp = &f; �\C{// pointer initialization}� 1413 fp = *f; �\C{// reference initialization}� 1414 fp(3); �\C{// reference invocation}� 1415 (*fp)(3); �\C{// pointer invocation}� 1416 \end{cfa} 1417 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. 1418 Instead, a routine object should be referenced by a �const� reference: 1419 \begin{cfa} 1420 �const� void (�&� fr)( int ) = f; �\C{// routine reference}� 1421 fr = ... �\C{// error, cannot change code}� 1422 &fr = ...; �\C{// changing routine reference}� 1423 fr( 3 ); �\C{// reference call to f}� 1424 (*fr)(3); �\C{// error, incorrect type}� 1425 1425 \end{cfa} 1426 1426 because the value of the routine object is a routine literal, \ie the routine code is normally immutable during execution.\footnote{ … … 1431 1431 \subsection{Address-of Semantics} 1432 1432 1433 In C, ©&E© is an rvalue for any expression ©E©.1434 \CFA extends the ©&©(address-of) operator as follows:1433 In C, �&E� is an rvalue for any expression �E�. 1434 \CFA extends the �&� (address-of) operator as follows: 1435 1435 \begin{itemize} 1436 1436 \item 1437 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).1438 1439 \item 1440 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).1437 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). 1438 1439 \item 1440 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). 1441 1441 \end{itemize} 1442 1442 The following example shows the first rule applied to different \Index{rvalue} contexts: … … 1467 1467 \setcounter{enumi}{-1} 1468 1468 \item 1469 lvalue to rvalue conversion: ©cv T© converts to ©T©, which allows implicit variable dereferencing.1469 lvalue to rvalue conversion: �cv T� converts to �T�, which allows implicit variable dereferencing. 1470 1470 \begin{cfa} 1471 1471 int x; 1472 1472 x + 1; // lvalue variable (int) converts to rvalue for expression 1473 1473 \end{cfa} 1474 An rvalue has no type qualifiers ( ©cv©), so the lvalue qualifiers are dropped.1474 An rvalue has no type qualifiers (�cv�), so the lvalue qualifiers are dropped. 1475 1475 \end{enumerate} 1476 1476 \CFA provides three new implicit conversion for reference types to simplify reference usage. 1477 1477 \begin{enumerate} 1478 1478 \item 1479 reference to rvalue conversion: ©cv T &© converts to ©T©, which allows implicit reference dereferencing.1479 reference to rvalue conversion: �cv T &� converts to �T�, which allows implicit reference dereferencing. 1480 1480 \begin{cfa} 1481 1481 int x, &r = x, f( int p ); 1482 x = ®r® + f( ®r®); // lvalue reference converts to rvalue1483 \end{cfa} 1484 An rvalue has no type qualifiers ( ©cv©), so the reference qualifiers are dropped.1485 1486 \item 1487 lvalue to reference conversion: \lstinline[deletekeywords={lvalue}]@lvalue-type cv1 T@ converts to ©cv2 T &©, which allows implicitly converting variables to references.1488 \begin{cfa} 1489 int x, &r = ®x®, f( int & p ); // lvalue variable (int) convert to reference (int &)1490 f( ®x®); // lvalue variable (int) convert to reference (int &)1491 \end{cfa} 1492 Conversion can restrict a type, where ©cv1© $\le$ ©cv2©, \eg passing an ©int© to a ©const volatile int &©, which has low cost.1493 Conversion can expand a type, where ©cv1© $>$ ©cv2©, \eg passing a ©const volatile int© to an ©int &©, which has high cost (\Index{warning});1494 furthermore, if ©cv1© has ©const© but not ©cv2©, a temporary variable is created to preserve the immutable lvalue.1495 1496 \item 1497 rvalue to reference conversion: ©T© converts to ©cv T &©, which allows binding references to temporaries.1482 x = �r� + f( �r� ); // lvalue reference converts to rvalue 1483 \end{cfa} 1484 An rvalue has no type qualifiers (�cv�), so the reference qualifiers are dropped. 1485 1486 \item 1487 lvalue to reference conversion: \lstinline[deletekeywords={lvalue}]@lvalue-type cv1 T@ converts to �cv2 T &�, which allows implicitly converting variables to references. 1488 \begin{cfa} 1489 int x, &r = �x�, f( int & p ); // lvalue variable (int) convert to reference (int &) 1490 f( �x� ); // lvalue variable (int) convert to reference (int &) 1491 \end{cfa} 1492 Conversion can restrict a type, where �cv1� $\le$ �cv2�, \eg passing an �int� to a �const volatile int &�, which has low cost. 1493 Conversion can expand a type, where �cv1� $>$ �cv2�, \eg passing a �const volatile int� to an �int &�, which has high cost (\Index{warning}); 1494 furthermore, if �cv1� has �const� but not �cv2�, a temporary variable is created to preserve the immutable lvalue. 1495 1496 \item 1497 rvalue to reference conversion: �T� converts to �cv T &�, which allows binding references to temporaries. 1498 1498 \begin{cfa} 1499 1499 int x, & f( int & p ); 1500 f( ®x + 3®); // rvalue parameter (int) implicitly converts to lvalue temporary reference (int &)1501 ®&f®(...) = &x; // rvalue result (int &) implicitly converts to lvalue temporary reference (int &)1500 f( �x + 3� ); // rvalue parameter (int) implicitly converts to lvalue temporary reference (int &) 1501 �&f�(...) = &x; // rvalue result (int &) implicitly converts to lvalue temporary reference (int &) 1502 1502 \end{cfa} 1503 1503 In both case, modifications to the temporary are inaccessible (\Index{warning}). 1504 Conversion expands the temporary-type with ©cv©, which is low cost since the temporary is inaccessible.1504 Conversion expands the temporary-type with �cv�, which is low cost since the temporary is inaccessible. 1505 1505 \end{enumerate} 1506 1506 … … 1671 1671 The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: 1672 1672 \begin{cfa} 1673 ®[ int o1, int o2, char o3 ]®f( int i1, char i2, char i3 ) {1674 §\emph{routine body}§1675 } 1676 \end{cfa} 1677 where routine ©f©has three output (return values) and three input parameters.1673 �[ int o1, int o2, char o3 ]� f( int i1, char i2, char i3 ) { 1674 �\emph{routine body}� 1675 } 1676 \end{cfa} 1677 where routine �f� has three output (return values) and three input parameters. 1678 1678 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. 1679 1679 1680 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{1680 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{ 1681 1681 \Index*{Michael Tiemann}, with help from \Index*{Doug Lea}, provided named return values in g++, circa 1989.} 1682 1682 The value of each local return variable is automatically returned at routine termination. 1683 1683 Declaration qualifiers can only appear at the start of a routine definition, \eg: 1684 1684 \begin{cfa} 1685 ®extern® [ int x ] g( int y ) {§\,§}1685 �extern� [ int x ] g( int y ) {�\,�} 1686 1686 \end{cfa} 1687 1687 Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified; 1688 1688 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: 1689 1689 \begin{cfa} 1690 [ §\,§] g(); §\C{// no input or output parameters}§1691 [ void ] g( void ); §\C{// no input or output parameters}§1690 [�\,�] g(); �\C{// no input or output parameters}� 1691 [ void ] g( void ); �\C{// no input or output parameters}� 1692 1692 \end{cfa} 1693 1693 … … 1702 1702 int (*f(x))[ 5 ] int x; {} 1703 1703 \end{cfa} 1704 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.1705 Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string.1704 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. 1705 Since the strings overlap starting with the open bracket, �[�, there is an ambiguous interpretation for the string. 1706 1706 As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity: 1707 1707 \begin{cfa} 1708 1708 typedef int foo; 1709 int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§1710 \end{cfa} 1711 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.1712 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.1709 int f( int (* foo) ); �\C{// foo is redefined as a parameter name}� 1710 \end{cfa} 1711 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. 1712 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. 1713 1713 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. 1714 1714 1715 1715 C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: 1716 1716 \begin{cfa} 1717 [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§1718 [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§1717 [ int ] f( * int, int * ); �\C{// returns an integer, accepts 2 pointers to integers}� 1718 [ * int, int * ] f( int ); �\C{// returns 2 pointers to integers, accepts an integer}� 1719 1719 \end{cfa} 1720 1720 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: 1721 1721 \begin{cfa} 1722 1722 #define ptoa( n, d ) int (*n)[ d ] 1723 int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§1724 [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§1723 int f( ptoa( p, 5 ) ) ... �\C{// expands to int f( int (*p)[ 5 ] )}� 1724 [ int ] f( ptoa( p, 5 ) ) ... �\C{// expands to [ int ] f( int (*p)[ 5 ] )}� 1725 1725 \end{cfa} 1726 1726 Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. … … 1729 1729 \subsection{Named Return Values} 1730 1730 1731 \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:1731 \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: 1732 1732 \begin{cfa} 1733 1733 int f() { … … 1737 1737 } 1738 1738 \end{cfa} 1739 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:1739 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: 1740 1740 \newline 1741 1741 \begin{minipage}{\linewidth} 1742 1742 \begin{cfa} 1743 ®[ int x, int y ]®f() {1743 �[ int x, int y ]� f() { 1744 1744 int z; 1745 1745 ... x = 0; ... y = z; ... 1746 ®return;® §\C{// implicitly return x, y}§1746 �return;� �\C{// implicitly return x, y}� 1747 1747 } 1748 1748 \end{cfa} 1749 1749 \end{minipage} 1750 1750 \newline 1751 When the return is encountered, the current values of ©x© and ©y©are returned to the calling routine.1752 As well, ``falling off the end'' of a routine without a ©return©statement is permitted, as in:1751 When the return is encountered, the current values of �x� and �y� are returned to the calling routine. 1752 As well, ``falling off the end'' of a routine without a �return� statement is permitted, as in: 1753 1753 \begin{cfa} 1754 1754 [ int x, int y ] f() { 1755 1755 ... 1756 } §\C{// implicitly return x, y}§1757 \end{cfa} 1758 In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return©had been encountered.1756 } �\C{// implicitly return x, y}� 1757 \end{cfa} 1758 In this case, the current values of �x� and �y� are returned to the calling routine just as if a �return� had been encountered. 1759 1759 1760 1760 Named return values may be used in conjunction with named parameter values; … … 1763 1763 [ int x, int y ] f( int, x, int y ) { 1764 1764 ... 1765 } §\C{// implicitly return x, y}§1765 } �\C{// implicitly return x, y}� 1766 1766 \end{cfa} 1767 1767 This notation allows the compiler to eliminate temporary variables in nested routine calls. 1768 1768 \begin{cfa} 1769 [ int x, int y ] f( int, x, int y ); §\C{// prototype declaration}§1769 [ int x, int y ] f( int, x, int y ); �\C{// prototype declaration}� 1770 1770 int a, b; 1771 1771 [a, b] = f( f( f( a, b ) ) ); 1772 1772 \end{cfa} 1773 1773 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. 1774 Hence, even without the body of routine ©f©(separate compilation), it is possible to perform a global optimization across routine calls.1774 Hence, even without the body of routine �f� (separate compilation), it is possible to perform a global optimization across routine calls. 1775 1775 The compiler warns about naming inconsistencies between routine prototype and definition in this case, and behaviour is \Index{undefined} if the programmer is inconsistent. 1776 1776 … … 1781 1781 as well, parameter names are optional, \eg: 1782 1782 \begin{cfa} 1783 [ int x ] f (); §\C{// returning int with no parameters}§1784 [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§1785 [ ] h ( int, char ); §\C{// returning no result with int and char parameters}§1786 [ * int, int ] j ( int ); §\C{// returning pointer to int and int, with int parameter}§1783 [ int x ] f (); �\C{// returning int with no parameters}� 1784 [ * int ] g (int y); �\C{// returning pointer to int with int parameter}� 1785 [ ] h ( int, char ); �\C{// returning no result with int and char parameters}� 1786 [ * int, int ] j ( int ); �\C{// returning pointer to int and int, with int parameter}� 1787 1787 \end{cfa} 1788 1788 This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). … … 1811 1811 The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: 1812 1812 \begin{cfa} 1813 * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§1814 * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§1815 * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§1816 * [ * int,int ] ( int ) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§1813 * [ int x ] () fp; �\C{// pointer to routine returning int with no parameters}� 1814 * [ * int ] (int y) gp; �\C{// pointer to routine returning pointer to int with int parameter}� 1815 * [ ] (int,char) hp; �\C{// pointer to routine returning no result with int and char parameters}� 1816 * [ * int,int ] ( int ) jp; �\C{// pointer to routine returning pointer to int and int, with int parameter}� 1817 1817 \end{cfa} 1818 1818 While parameter names are optional, \emph{a routine name cannot be specified}; 1819 1819 for example, the following is incorrect: 1820 1820 \begin{cfa} 1821 * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§1821 * [ int x ] f () fp; �\C{// routine name "f" is not allowed}� 1822 1822 \end{cfa} 1823 1823 … … 1842 1842 whereas a named (keyword) call may be: 1843 1843 \begin{cfa} 1844 p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§1844 p( z : 3, x : 4, y : 7 ); �\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}� 1845 1845 \end{cfa} 1846 1846 Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. … … 1859 1859 For example, the following routine prototypes and definition are all valid. 1860 1860 \begin{cfa} 1861 void p( int, int, int ); §\C{// equivalent prototypes}§1861 void p( int, int, int ); �\C{// equivalent prototypes}� 1862 1862 void p( int x, int y, int z ); 1863 1863 void p( int y, int x, int z ); 1864 1864 void p( int z, int y, int x ); 1865 void p( int q, int r, int s ) {} §\C{// match with this definition}§1865 void p( int q, int r, int s ) {} �\C{// match with this definition}� 1866 1866 \end{cfa} 1867 1867 Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. … … 1870 1870 1871 1871 Furthermore, named arguments do not work well in a \CFA-style programming-languages because they potentially introduces a new criteria for type matching. 1872 For example, it is technically possible to disambiguate between these two overloaded definitions of ©f©based on named arguments at the call site:1872 For example, it is technically possible to disambiguate between these two overloaded definitions of �f� based on named arguments at the call site: 1873 1873 \begin{cfa} 1874 1874 int f( int i, int j ); 1875 1875 int f( int x, double y ); 1876 1876 1877 f( j : 3, i : 4 ); §\C{// 1st f}§1878 f( x : 7, y : 8.1 ); §\C{// 2nd f}§1879 f( 4, 5 ); §\C{// ambiguous call}§1877 f( j : 3, i : 4 ); �\C{// 1st f}� 1878 f( x : 7, y : 8.1 ); �\C{// 2nd f}� 1879 f( 4, 5 ); �\C{// ambiguous call}� 1880 1880 \end{cfa} 1881 1881 However, named arguments compound routine resolution in conjunction with conversions: 1882 1882 \begin{cfa} 1883 f( i : 3, 5.7 ); §\C{// ambiguous call ?}§1883 f( i : 3, 5.7 ); �\C{// ambiguous call ?}� 1884 1884 \end{cfa} 1885 1885 Depending on the cost associated with named arguments, this call could be resolvable or ambiguous. … … 1895 1895 the allowable positional calls are: 1896 1896 \begin{cfa} 1897 p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§1898 p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§1899 p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§1900 p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§1897 p(); �\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}� 1898 p( 4 ); �\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}� 1899 p( 4, 4 ); �\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}� 1900 p( 4, 4, 4 ); �\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}� 1901 1901 // empty arguments 1902 p( , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§1903 p( 4, , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§1904 p( 4, 4, ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§1905 p( 4, , ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§1906 p( , 4, ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§1907 p( , , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§1908 p( , , ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§1902 p( , 4, 4 ); �\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}� 1903 p( 4, , 4 ); �\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}� 1904 p( 4, 4, ); �\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}� 1905 p( 4, , ); �\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}� 1906 p( , 4, ); �\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}� 1907 p( , , 4 ); �\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}� 1908 p( , , ); �\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}� 1909 1909 \end{cfa} 1910 1910 Here the missing arguments are inserted from the default values in the parameter list. … … 1930 1930 Default values may only appear in a prototype versus definition context: 1931 1931 \begin{cfa} 1932 void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§1933 void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§1934 void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§1932 void p( int x, int y = 2, int z = 3 ); �\C{// prototype: allowed}� 1933 void p( int, int = 2, int = 3 ); �\C{// prototype: allowed}� 1934 void p( int x, int y = 2, int z = 3 ) {} �\C{// definition: not allowed}� 1935 1935 \end{cfa} 1936 1936 The reason for this restriction is to allow separate compilation. … … 1947 1947 \begin{cfa} 1948 1948 p( int x, int y, int z, ... ); 1949 p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, ... , /* named */ );}§1950 p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§1949 p( 1, 4, 5, 6, z : 3, y : 2 ); �\C{// assume p( /* positional */, ... , /* named */ );}� 1950 p( 1, z : 3, y : 2, 4, 5, 6 ); �\C{// assume p( /* positional */, /* named */, ... );}� 1951 1951 \end{cfa} 1952 1952 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. … … 1957 1957 \begin{cfa} 1958 1958 void p( int x, int y = 2, int z = 3... ); 1959 p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, ... , /* named */ );}§1960 p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, ... );}§1959 p( 1, 4, 5, 6, z : 3 ); �\C{// assume p( /* positional */, ... , /* named */ );}� 1960 p( 1, z : 3, 4, 5, 6 ); �\C{// assume p( /* positional */, /* named */, ... );}� 1961 1961 \end{cfa} 1962 1962 The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; … … 1988 1988 Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: 1989 1989 \begin{cfa} 1990 p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§1990 p( 1, /* default */, 5 ); �\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}� 1991 1991 \end{cfa} 1992 1992 … … 2001 2001 \begin{cfa} 2002 2002 struct { 2003 int f1; §\C{// named field}§2004 int f2 : 4; §\C{// named field with bit field size}§2005 int : 3; §\C{// unnamed field for basic type with bit field size}§2006 int ; §\C{// disallowed, unnamed field}§2007 int *; §\C{// disallowed, unnamed field}§2008 int (*)( int ); §\C{// disallowed, unnamed field}§2003 int f1; �\C{// named field}� 2004 int f2 : 4; �\C{// named field with bit field size}� 2005 int : 3; �\C{// unnamed field for basic type with bit field size}� 2006 int ; �\C{// disallowed, unnamed field}� 2007 int *; �\C{// disallowed, unnamed field}� 2008 int (*)( int ); �\C{// disallowed, unnamed field}� 2009 2009 }; 2010 2010 \end{cfa} … … 2014 2014 \begin{cfa} 2015 2015 struct { 2016 int , , ; §\C{// 3 unnamed fields}§2016 int , , ; �\C{// 3 unnamed fields}� 2017 2017 } 2018 2018 \end{cfa} … … 2061 2061 struct T t; 2062 2062 } s; 2063 2063 2064 2064 2065 2065 … … 2082 2082 2083 2083 int fred() { 2084 s.t.c = ®S.®R; // type qualification2085 struct ®S.®T t = { ®S.®R, 1, 2 };2086 enum ®S.®C c;2087 union ®S.T.®U u;2084 s.t.c = �S.�R; // type qualification 2085 struct �S.�T t = { �S.�R, 1, 2 }; 2086 enum �S.�C c; 2087 union �S.T.�U u; 2088 2088 } 2089 2089 \end{cfa} … … 2092 2092 \label{f:TypeNestingQualification} 2093 2093 \end{figure} 2094 In the left example in C, types ©C©, ©U© and ©T© are implicitly hoisted outside of type ©S©into the containing block scope.2095 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 ``©::©''.2094 In the left example in C, types �C�, �U� and �T� are implicitly hoisted outside of type �S� into the containing block scope. 2095 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 ``�::�''. 2096 2096 2097 2097 … … 2108 2108 const unsigned int size = 5; 2109 2109 int ia[size]; 2110 ... §\C{// assign values to array ia}§2111 qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§2110 ... �\C{// assign values to array ia}� 2111 qsort( ia, size ); �\C{// sort ascending order using builtin ?<?}� 2112 2112 { 2113 ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§2114 qsort( ia, size ); §\C{// sort descending order by local redefinition}§2113 �int ?<?( int x, int y ) { return x > y; }� �\C{// nested routine}� 2114 qsort( ia, size ); �\C{// sort descending order by local redefinition}� 2115 2115 } 2116 2116 \end{cfa} … … 2120 2120 The following program in undefined in \CFA (and Indexc{gcc}) 2121 2121 \begin{cfa} 2122 [* [int]( int )] foo() { §\C{// int (*foo())( int )}§2123 int ®i®= 7;2122 [* [int]( int )] foo() { �\C{// int (*foo())( int )}� 2123 int �i� = 7; 2124 2124 int bar( int p ) { 2125 ®i® += 1; §\C{// dependent on local variable}§2126 sout | ®i®| endl;2125 �i� += 1; �\C{// dependent on local variable}� 2126 sout | �i� | endl; 2127 2127 } 2128 return bar; §\C{// undefined because of local dependence}§2128 return bar; �\C{// undefined because of local dependence}� 2129 2129 } 2130 2130 int main() { 2131 * [int]( int ) fp = foo(); §\C{// int (*fp)( int )}§2131 * [int]( int ) fp = foo(); �\C{// int (*fp)( int )}� 2132 2132 sout | fp( 3 ) | endl; 2133 2133 } 2134 2134 \end{cfa} 2135 because 2135 because 2136 2136 2137 2137 Currently, there are no \Index{lambda} expressions, \ie unnamed routines because routine names are very important to properly select the correct routine. … … 2145 2145 The general syntax of a lexical list is: 2146 2146 \begin{cfa} 2147 [ §\emph{exprlist}§]2148 \end{cfa} 2149 where ©$\emph{exprlist}$©is a list of one or more expressions separated by commas.2150 The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator.2147 [ �\emph{exprlist}� ] 2148 \end{cfa} 2149 where �$\emph{exprlist}$� is a list of one or more expressions separated by commas. 2150 The brackets, �[]�, allow differentiating between lexical lists and expressions containing the C comma operator. 2151 2151 The following are examples of lexical lists: 2152 2152 \begin{cfa} … … 2155 2155 [ v+w, x*y, 3.14159, f() ] 2156 2156 \end{cfa} 2157 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.2157 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. 2158 2158 Note, a tuple is not a record (structure); 2159 2159 a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1). … … 2165 2165 The general syntax of a tuple type is: 2166 2166 \begin{cfa} 2167 [ §\emph{typelist}§]2168 \end{cfa} 2169 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.2167 [ �\emph{typelist}� ] 2168 \end{cfa} 2169 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. 2170 2170 Examples of tuple types include: 2171 2171 \begin{cfa} 2172 2172 [ unsigned int, char ] 2173 2173 [ double, double, double ] 2174 [ * int, int * ] §\C{// mix of CFA and ANSI}§2174 [ * int, int * ] �\C{// mix of CFA and ANSI}� 2175 2175 [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] 2176 2176 \end{cfa} 2177 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.2177 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. 2178 2178 2179 2179 Examples of declarations using tuple types are: 2180 2180 \begin{cfa} 2181 [ int, int ] x; §\C{// 2 element tuple, each element of type int}§2182 * [ char, char ] y; §\C{// pointer to a 2 element tuple}§2181 [ int, int ] x; �\C{// 2 element tuple, each element of type int}� 2182 * [ char, char ] y; �\C{// pointer to a 2 element tuple}� 2183 2183 [ [ int, int ] ] z ([ int, int ]); 2184 2184 \end{cfa} … … 2211 2211 tuple does not have structure like a record; a tuple is simply converted into a list of components. 2212 2212 \begin{rationale} 2213 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.2213 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. 2214 2214 Using a temporary variable to store the results of the inner routine and then passing this variable to the outer routine works, however. 2215 2215 \end{rationale} … … 2240 2240 \begin{rationale} 2241 2241 Unfortunately, C's syntax for subscripts precluded treating them as tuples. 2242 The C subscript list has the form ©[i][j]...© and not ©[i, j, ...]©.2243 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.2244 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.2242 The C subscript list has the form �[i][j]...� and not �[i, j, ...]�. 2243 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. 2244 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. 2245 2245 \end{rationale} 2246 2246 … … 2261 2261 [ a, b, c, d ] = w 2262 2262 \end{cfa} 2263 ©w©is implicitly opened to yield a tuple of four values, which are then assigned individually.2263 �w� is implicitly opened to yield a tuple of four values, which are then assigned individually. 2264 2264 2265 2265 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: … … 2269 2269 First the right-hand tuple is flattened and then the values are assigned individually. 2270 2270 Flattening is also performed on tuple types. 2271 For example, the type ©[ int, [ int, int ], int ]© can be coerced, using flattening, into the type ©[ int, int, int, int ]©.2271 For example, the type �[ int, [ int, int ], int ]� can be coerced, using flattening, into the type �[ int, int, int, int ]�. 2272 2272 2273 2273 A \newterm{structuring coercion} is the opposite of flattening; 2274 2274 a tuple is structured into a more complex nested tuple. 2275 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 ]©.2275 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 ]�. 2276 2276 In the following example, the last assignment illustrates all the tuple coercions: 2277 2277 \begin{cfa} 2278 2278 [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; 2279 2279 int x = 5; 2280 [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§2280 [ x, w ] = [ w, x ]; �\C{// all four tuple coercions}� 2281 2281 \end{cfa} 2282 2282 Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; 2283 therefore, the right-hand tuple is now the tuple ©[ [ 1, 2, 3, 4 ], 5 ]©.2284 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.2285 The tuple ©[ 2, 3, 4, 5 ]©is then closed to create a tuple value.2286 Finally, ©x© is assigned ©1© and ©w©is assigned the tuple value using multiple assignment (see Section 14).2283 therefore, the right-hand tuple is now the tuple �[ [ 1, 2, 3, 4 ], 5 ]�. 2284 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. 2285 The tuple �[ 2, 3, 4, 5 ]� is then closed to create a tuple value. 2286 Finally, �x� is assigned �1� and �w� is assigned the tuple value using multiple assignment (see Section 14). 2287 2287 \begin{rationale} 2288 2288 A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple. … … 2295 2295 Mass assignment has the following form: 2296 2296 \begin{cfa} 2297 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = §\emph{expr}§;2297 [ �\emph{lvalue}�, ... , �\emph{lvalue}� ] = �\emph{expr}�; 2298 2298 \end{cfa} 2299 2299 \index{lvalue} 2300 2300 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. 2301 ©$\emph{expr}$©is any standard arithmetic expression.2301 �$\emph{expr}$� is any standard arithmetic expression. 2302 2302 Clearly, the types of the entities being assigned must be type compatible with the value of the expression. 2303 2303 … … 2325 2325 *a1 = t; *a2 = t; *a3 = t; 2326 2326 \end{cfa} 2327 The temporary ©t©is necessary to store the value of the expression to eliminate conversion issues.2327 The temporary �t� is necessary to store the value of the expression to eliminate conversion issues. 2328 2328 The temporaries for the addresses are needed so that locations on the left-hand side do not change as the values are assigned. 2329 In this case, ©y[i]© uses the previous value of ©i©and not the new value set at the beginning of the mass assignment.2329 In this case, �y[i]� uses the previous value of �i� and not the new value set at the beginning of the mass assignment. 2330 2330 2331 2331 … … 2335 2335 Multiple assignment has the following form: 2336 2336 \begin{cfa} 2337 [ §\emph{lvalue}§, ... , §\emph{lvalue}§ ] = [ §\emph{expr}§, ... , §\emph{expr}§];2337 [ �\emph{lvalue}�, ... , �\emph{lvalue}� ] = [ �\emph{expr}�, ... , �\emph{expr}� ]; 2338 2338 \end{cfa} 2339 2339 \index{lvalue} … … 2344 2344 [ x, y, z ] = [ 1, 2, 3 ]; 2345 2345 \end{cfa} 2346 Here, the values ©1©, ©2© and ©3© are assigned, respectively, to the variables ©x©, ©y© and ©z©.2346 Here, the values �1�, �2� and �3� are assigned, respectively, to the variables �x�, �y� and �z�. 2347 2347 A more complex example is: 2348 2348 \begin{cfa} 2349 2349 [ i, y[ i ], z ] = [ 1, i, a + b ]; 2350 2350 \end{cfa} 2351 Here, the values ©1©, ©i© and ©a + b© are assigned to the variables ©i©, ©y[i]© and ©z©, respectively.2351 Here, the values �1�, �i� and �a + b� are assigned to the variables �i�, �y[i]� and �z�, respectively. 2352 2352 Note, the parallel semantics of 2353 2353 multiple assignment ensures: … … 2355 2355 [ x, y ] = [ y, x ]; 2356 2356 \end{cfa} 2357 correctly interchanges (swaps) the values stored in ©x© and ©y©.2357 correctly interchanges (swaps) the values stored in �x� and �y�. 2358 2358 The following cases are errors: 2359 2359 \begin{cfa} … … 2366 2366 both these examples produce indeterminate results: 2367 2367 \begin{cfa} 2368 f( x++, x++ ); §\C{// C routine call with side effects in arguments}§2369 [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§2368 f( x++, x++ ); �\C{// C routine call with side effects in arguments}� 2369 [ v1, v2 ] = [ x++, x++ ]; �\C{// side effects in righthand side of multiple assignment}� 2370 2370 \end{cfa} 2371 2371 … … 2376 2376 Cascade assignment has the following form: 2377 2377 \begin{cfa} 2378 §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§;2378 �\emph{tuple}� = �\emph{tuple}� = ... = �\emph{tuple}�; 2379 2379 \end{cfa} 2380 2380 and it has the same parallel semantics as for mass and multiple assignment. … … 2394 2394 Its general form is: 2395 2395 \begin{cfa} 2396 §\emph{expr}§ . [ §\emph{fieldlist}§]2397 §\emph{expr}§ -> [ §\emph{fieldlist}§]2398 \end{cfa} 2399 \emph{expr} is any expression yielding a value of type record, \eg ©struct©, ©union©.2396 �\emph{expr}� . [ �\emph{fieldlist}� ] 2397 �\emph{expr}� -> [ �\emph{fieldlist}� ] 2398 \end{cfa} 2399 \emph{expr} is any expression yielding a value of type record, \eg �struct�, �union�. 2400 2400 Each element of \emph{ fieldlist} is an element of the record specified by \emph{expr}. 2401 2401 A record-field tuple may be used anywhere a tuple can be used. An example of the use of a record-field tuple is … … 2407 2407 double f4; 2408 2408 } v; 2409 v.[ f3, f1, f2 ] = ['x', 11, 17 ]; §\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}§2410 f( v.[ f3, f1, f2 ] ); §\C{// equivalent to f( v.f3, v.f1, v.f2 )}§2409 v.[ f3, f1, f2 ] = ['x', 11, 17 ]; �\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}� 2410 f( v.[ f3, f1, f2 ] ); �\C{// equivalent to f( v.f3, v.f1, v.f2 )}� 2411 2411 \end{cfa} 2412 2412 Note, the fields appearing in a record-field tuple may be specified in any order; 2413 2413 also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. 2414 2414 2415 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:2415 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: 2416 2416 \begin{cfa} 2417 2417 struct inner { … … 2442 2442 \begin{cfa} 2443 2443 int x = 1, y = 2, z = 3; 2444 sout | x ®|® y ®|®z | endl;2444 sout | x �|� y �|� z | endl; 2445 2445 \end{cfa} 2446 2446 & 2447 2447 \begin{cfa} 2448 2448 2449 cout << x ®<< " "® << y ®<< " "®<< z << endl;2449 cout << x �<< " "� << y �<< " "� << z << endl; 2450 2450 \end{cfa} 2451 2451 \\ 2452 2452 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2453 1 ® ®2® ®32453 1� �2� �3 2454 2454 \end{cfa} 2455 2455 & … … 2463 2463 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2464 2464 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 3, [ 4, 5 ] ]; 2465 sout | t1 | t2 | endl; §\C{// print tuples}§2465 sout | t1 | t2 | endl; �\C{// print tuples}� 2466 2466 \end{cfa} 2467 2467 \begin{cfa}[mathescape=off,showspaces=true,belowskip=0pt] 2468 1 ®, ®2®, ®3 3®, ®4®, ®52468 1�, �2�, �3 3�, �4�, �5 2469 2469 \end{cfa} 2470 2470 Finally, \CFA uses the logical-or operator for I/O as it is the lowest-priority overloadable operator, other than assignment. … … 2481 2481 & 2482 2482 \begin{cfa} 2483 cout << x * 3 << y + 1 << ®(®z << 2®)® << ®(®x == y®)®<< (x | y) << (x || y) << (x > z ? 1 : 2) << endl;2483 cout << x * 3 << y + 1 << �(�z << 2�)� << �(�x == y�)� << (x | y) << (x || y) << (x > z ? 1 : 2) << endl; 2484 2484 \end{cfa} 2485 2485 \\ … … 2523 2523 2524 2524 \item 2525 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$ £¥¡¿«@2525 A separator does not appear before a C string starting with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off,basicstyle=\tt]@([{=$�����@ 2526 2526 %$ 2527 2527 \begin{cfa}[mathescape=off] 2528 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥"2529 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;2528 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x �" | 6 | "x �" 2529 | 7 | "x �" | 8 | "x �" | 9 | "x �" | 10 | endl; 2530 2530 \end{cfa} 2531 2531 %$ 2532 2532 \begin{cfa}[mathescape=off,basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 2533 x ®(®1 x ®[®2 x ®{®3 x ®=®4 x ®$®5 x ®£®6 x ®¥®7 x ®¡®8 x ®¿®9 x ®«®102533 x �(�1 x �[�2 x �{�3 x �=�4 x �$�5 x ���6 x ���7 x ���8 x ���9 x ���10 2534 2534 \end{cfa} 2535 2535 %$ 2536 where \lstinline[basicstyle=\tt]@ ¡¿@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@«@ is an opening citation mark.2537 2538 \item 2539 {\lstset{language=CFA,deletedelim=**[is][]{ ¢}{¢}}2540 A seperator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}% ¢»@2536 where \lstinline[basicstyle=\tt]@��@ are inverted opening exclamation and question marks, and \lstinline[basicstyle=\tt]@�@ is an opening citation mark. 2537 2538 \item 2539 {\lstset{language=CFA,deletedelim=**[is][]{�}{�}} 2540 A seperator does not appear after a C string ending with the (extended) \Index*{ASCII}\index{ASCII!extended} characters: \lstinline[basicstyle=\tt]@,.;!?)]}%��@ 2541 2541 \begin{cfa}[belowskip=0pt] 2542 2542 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 2543 | 7 | " ¢ x" | 8 | "»x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl;2543 | 7 | "� x" | 8 | "� x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl; 2544 2544 \end{cfa} 2545 2545 \begin{cfa}[basicstyle=\tt,showspaces=true,aboveskip=0pt,belowskip=0pt] 2546 1 ®,® x 2®.® x 3®;® x 4®!® x 5®?® x 6®%® x 7§\color{red}\textcent§ x 8®»® x 9®)® x 10®]® x 11®}®x2546 1�,� x 2�.� x 3�;� x 4�!� x 5�?� x 6�%� x 7�\color{red}\textcent� x 8��� x 9�)� x 10�]� x 11�}� x 2547 2547 \end{cfa}}% 2548 where \lstinline[basicstyle=\tt]@ »@ is a closing citation mark.2548 where \lstinline[basicstyle=\tt]@�@ is a closing citation mark. 2549 2549 2550 2550 \item … … 2554 2554 \end{cfa} 2555 2555 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] 2556 x ®`®1®`®x§\color{red}\texttt{'}§2§\color{red}\texttt{'}§x§\color{red}\texttt{"}§3§\color{red}\texttt{"}§x®:®4®:®x® ®5® ®x® ®6® ®x2556 x�`�1�`�x�\color{red}\texttt{'}�2�\color{red}\texttt{'}�x�\color{red}\texttt{"}�3�\color{red}\texttt{"}�x�:�4�:�x� �5� �x� �6� �x 2557 2557 \end{cfa} 2558 2558 … … 2560 2560 If a space is desired before or after one of the special string start/end characters, simply insert a space. 2561 2561 \begin{cfa}[belowskip=0pt] 2562 sout | "x ( §\color{red}\texttt{\textvisiblespace}§" | 1 | "§\color{red}\texttt{\textvisiblespace}§) x" | 2 | "§\color{red}\texttt{\textvisiblespace}§, x" | 3 | "§\color{red}\texttt{\textvisiblespace}§:x:§\color{red}\texttt{\textvisiblespace}§" | 4 | endl;2562 sout | "x (�\color{red}\texttt{\textvisiblespace}�" | 1 | "�\color{red}\texttt{\textvisiblespace}�) x" | 2 | "�\color{red}\texttt{\textvisiblespace}�, x" | 3 | "�\color{red}\texttt{\textvisiblespace}�:x:�\color{red}\texttt{\textvisiblespace}�" | 4 | endl; 2563 2563 \end{cfa} 2564 2564 \begin{cfa}[basicstyle=\tt,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] 2565 x ( ® ®1® ®) x 2® ®, x 3® ®:x:® ®42565 x (� �1� �) x 2� �, x 3� �:x:� �4 2566 2566 \end{cfa} 2567 2567 \end{enumerate} … … 2573 2573 \begin{enumerate} 2574 2574 \item 2575 Routines \Indexc{sepSet}\index{manipulator!sepSet@ ©sepSet©} and \Indexc{sepGet}\index{manipulator!sepGet@©sepGet©} set and get the separator string.2576 The separator string can be at most 16 characters including the ©'\0'©string terminator (15 printable characters).2575 Routines \Indexc{sepSet}\index{manipulator!sepSet@�sepSet�} and \Indexc{sepGet}\index{manipulator!sepGet@�sepGet�} set and get the separator string. 2576 The separator string can be at most 16 characters including the �'\0'� string terminator (15 printable characters). 2577 2577 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2578 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§2579 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )®| "\"" | endl;2578 sepSet( sout, ", $" ); �\C{// set separator from " " to ", \$"}� 2579 sout | 1 | 2 | 3 | " \"" | �sepGet( sout )� | "\"" | endl; 2580 2580 \end{cfa} 2581 2581 %$ 2582 2582 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2583 1 ®, $®2®, $®3 ®", $"®2583 1�, $�2�, $�3 �", $"� 2584 2584 \end{cfa} 2585 2585 %$ 2586 2586 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2587 sepSet( sout, " " ); §\C{// reset separator to " "}§2588 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )®| "\"" | endl;2587 sepSet( sout, " " ); �\C{// reset separator to " "}� 2588 sout | 1 | 2 | 3 | " \"" | �sepGet( sout )� | "\"" | endl; 2589 2589 \end{cfa} 2590 2590 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2591 1 ® ®2® ®3 ®" "®2592 \end{cfa} 2593 2594 \item 2595 Routine \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@ ©sepSetTuple©} and \Indexc{sepGetTuple}\index{manipulator!sepGetTuple@©sepGetTuple©} get and set the tuple separator-string.2596 The tuple separator-string can be at most 16 characters including the ©'\0'©string terminator (15 printable characters).2591 1� �2� �3 �" "� 2592 \end{cfa} 2593 2594 \item 2595 Routine \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@�sepSetTuple�} and \Indexc{sepGetTuple}\index{manipulator!sepGetTuple@�sepGetTuple�} get and set the tuple separator-string. 2596 The tuple separator-string can be at most 16 characters including the �'\0'� string terminator (15 printable characters). 2597 2597 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2598 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§2599 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )®| "\"" | endl;2598 sepSetTuple( sout, " " ); �\C{// set tuple separator from ", " to " "}� 2599 sout | t1 | t2 | " \"" | �sepGetTuple( sout )� | "\"" | endl; 2600 2600 \end{cfa} 2601 2601 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2602 1 2 3 4 ®" "®2602 1 2 3 4 �" "� 2603 2603 \end{cfa} 2604 2604 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2605 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§2606 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )®| "\"" | endl;2605 sepSetTuple( sout, ", " ); �\C{// reset tuple separator to ", "}� 2606 sout | t1 | t2 | " \"" | �sepGetTuple( sout )� | "\"" | endl; 2607 2607 \end{cfa} 2608 2608 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2609 1, 2, 3, 4 ®", "®2610 \end{cfa} 2611 2612 \item 2613 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.2609 1, 2, 3, 4 �", "� 2610 \end{cfa} 2611 2612 \item 2613 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. 2614 2614 \begin{cfa}[mathescape=off,belowskip=0pt] 2615 sout | sepOn | 1 | 2 | 3 | sepOn | endl; §\C{// separator at start/end of line}§2615 sout | sepOn | 1 | 2 | 3 | sepOn | endl; �\C{// separator at start/end of line}� 2616 2616 \end{cfa} 2617 2617 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2618 ® ®1 2 3® ® 2618 � �1 2 3� � 2619 2619 \end{cfa} 2620 2620 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2621 sout | 1 | sepOff | 2 | 3 | endl; §\C{// locally turn off implicit separator}§2621 sout | 1 | sepOff | 2 | 3 | endl; �\C{// locally turn off implicit separator}� 2622 2622 \end{cfa} 2623 2623 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 2626 2626 The tuple separator also responses to being turned on and off. 2627 2627 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2628 sout | sepOn | t1 | sepOff | t2 | endl; §\C{// locally turn on/off implicit separation}§2628 sout | sepOn | t1 | sepOff | t2 | endl; �\C{// locally turn on/off implicit separation}� 2629 2629 \end{cfa} 2630 2630 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 2634 2634 2635 2635 \item 2636 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.2636 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. 2637 2637 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2638 sout | sepDisable | 1 | 2 | 3 | endl; §\C{// globally turn off implicit separation}§2638 sout | sepDisable | 1 | 2 | 3 | endl; �\C{// globally turn off implicit separation}� 2639 2639 \end{cfa} 2640 2640 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 2642 2642 \end{cfa} 2643 2643 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2644 sout | 1 | ®sepOn® | 2 | 3 | endl; §\C{// locally turn on implicit separator}§2644 sout | 1 | �sepOn� | 2 | 3 | endl; �\C{// locally turn on implicit separator}� 2645 2645 \end{cfa} 2646 2646 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2647 1 ® ®232647 1� �23 2648 2648 \end{cfa} 2649 2649 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2650 sout | sepEnable | 1 | 2 | 3 | endl; §\C{// globally turn on implicit separation}§2650 sout | sepEnable | 1 | 2 | 3 | endl; �\C{// globally turn on implicit separation}� 2651 2651 \end{cfa} 2652 2652 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] … … 2667 2667 sout | '1' | '2' | '3' | endl; 2668 2668 sout | 1 | "" | 2 | "" | 3 | endl; 2669 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥"2670 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl;2669 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x �" | 6 | "x �" 2670 | 7 | "x �" | 8 | "x �" | 9 | "x �" | 10 | endl; 2671 2671 sout | 1 | ", x" | 2 | ". x" | 3 | "; x" | 4 | "! x" | 5 | "? x" | 6 | "% x" 2672 | 7 | " ¢ x" | 8 | "»x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl;2672 | 7 | "� x" | 8 | "� x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl; 2673 2673 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx" | endl; 2674 2674 sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4 | endl; … … 2909 2909 In \CFA, as in C, all scalar types can be incremented and 2910 2910 decremented, which is defined in terms of adding or subtracting 1. 2911 The operations ©&&©, ©||©, and ©!© can be applied to any scalar arguments and are defined in terms of comparison against 0 (ex. ©(a && b)© becomes ©(a != 0 && b != 0)©).2911 The operations �&&�, �||�, and �!� can be applied to any scalar arguments and are defined in terms of comparison against 0 (ex. �(a && b)� becomes �(a != 0 && b != 0)�). 2912 2912 2913 2913 In C, the integer constants 0 and 1 suffice because the integer promotion rules can convert them to any arithmetic type, and the rules for pointer expressions treat constant expressions evaluating to 0 as a special case. 2914 2914 However, user-defined arithmetic types often need the equivalent of a 1 or 0 for their functions or operators, polymorphic functions often need 0 and 1 constants of a type matching their polymorphic parameters, and user-defined pointer-like types may need a null value. 2915 Defining special constants for a user-defined type is more efficient than defining a conversion to the type from ©_Bool©.2915 Defining special constants for a user-defined type is more efficient than defining a conversion to the type from �_Bool�. 2916 2916 2917 2917 Why just 0 and 1? Why not other integers? No other integers have special status in C. … … 2996 2996 \begin{tabular}[t]{ll} 2997 2997 %identifier & operation \\ \hline 2998 ©?[?]©& subscripting \impl{?[?]}\\2999 ©?()©& function call \impl{?()}\\3000 ©?++©& postfix increment \impl{?++}\\3001 ©?--©& postfix decrement \impl{?--}\\3002 ©++?©& prefix increment \impl{++?}\\3003 ©--?©& prefix decrement \impl{--?}\\3004 ©*?©& dereference \impl{*?}\\3005 ©+?©& unary plus \impl{+?}\\3006 ©-?©& arithmetic negation \impl{-?}\\3007 ©~?©& bitwise negation \impl{~?}\\3008 ©!?©& logical complement \impl{"!?}\\3009 ©?*?©& multiplication \impl{?*?}\\3010 ©?/?©& division \impl{?/?}\\2998 �?[?]� & subscripting \impl{?[?]}\\ 2999 �?()� & function call \impl{?()}\\ 3000 �?++� & postfix increment \impl{?++}\\ 3001 �?--� & postfix decrement \impl{?--}\\ 3002 �++?� & prefix increment \impl{++?}\\ 3003 �--?� & prefix decrement \impl{--?}\\ 3004 �*?� & dereference \impl{*?}\\ 3005 �+?� & unary plus \impl{+?}\\ 3006 �-?� & arithmetic negation \impl{-?}\\ 3007 �~?� & bitwise negation \impl{~?}\\ 3008 �!?� & logical complement \impl{"!?}\\ 3009 �?*?� & multiplication \impl{?*?}\\ 3010 �?/?� & division \impl{?/?}\\ 3011 3011 \end{tabular}\hfil 3012 3012 \begin{tabular}[t]{ll} 3013 3013 %identifier & operation \\ \hline 3014 ©?%?©& remainder \impl{?%?}\\3015 ©?+?©& addition \impl{?+?}\\3016 ©?-?©& subtraction \impl{?-?}\\3017 ©?<<?©& left shift \impl{?<<?}\\3018 ©?>>?©& right shift \impl{?>>?}\\3019 ©?<?©& less than \impl{?<?}\\3020 ©?<=?©& less than or equal \impl{?<=?}\\3021 ©?>=?©& greater than or equal \impl{?>=?}\\3022 ©?>?©& greater than \impl{?>?}\\3023 ©?==?©& equality \impl{?==?}\\3024 ©?!=?©& inequality \impl{?"!=?}\\3025 ©?&?©& bitwise AND \impl{?&?}\\3014 �?%?� & remainder \impl{?%?}\\ 3015 �?+?� & addition \impl{?+?}\\ 3016 �?-?� & subtraction \impl{?-?}\\ 3017 �?<<?� & left shift \impl{?<<?}\\ 3018 �?>>?� & right shift \impl{?>>?}\\ 3019 �?<?� & less than \impl{?<?}\\ 3020 �?<=?� & less than or equal \impl{?<=?}\\ 3021 �?>=?� & greater than or equal \impl{?>=?}\\ 3022 �?>?� & greater than \impl{?>?}\\ 3023 �?==?� & equality \impl{?==?}\\ 3024 �?!=?� & inequality \impl{?"!=?}\\ 3025 �?&?� & bitwise AND \impl{?&?}\\ 3026 3026 \end{tabular}\hfil 3027 3027 \begin{tabular}[t]{ll} 3028 3028 %identifier & operation \\ \hline 3029 ©?^?©& exclusive OR \impl{?^?}\\3030 ©?|?©& inclusive OR \impl{?"|?}\\3031 ©?=?©& simple assignment \impl{?=?}\\3032 ©?*=?©& multiplication assignment \impl{?*=?}\\3033 ©?/=?©& division assignment \impl{?/=?}\\3034 ©?%=?©& remainder assignment \impl{?%=?}\\3035 ©?+=?©& addition assignment \impl{?+=?}\\3036 ©?-=?©& subtraction assignment \impl{?-=?}\\3037 ©?<<=?©& left-shift assignment \impl{?<<=?}\\3038 ©?>>=?©& right-shift assignment \impl{?>>=?}\\3039 ©?&=?©& bitwise AND assignment \impl{?&=?}\\3040 ©?^=?©& exclusive OR assignment \impl{?^=?}\\3041 ©?|=?©& inclusive OR assignment \impl{?"|=?}\\3029 �?^?� & exclusive OR \impl{?^?}\\ 3030 �?|?� & inclusive OR \impl{?"|?}\\ 3031 �?=?� & simple assignment \impl{?=?}\\ 3032 �?*=?� & multiplication assignment \impl{?*=?}\\ 3033 �?/=?� & division assignment \impl{?/=?}\\ 3034 �?%=?� & remainder assignment \impl{?%=?}\\ 3035 �?+=?� & addition assignment \impl{?+=?}\\ 3036 �?-=?� & subtraction assignment \impl{?-=?}\\ 3037 �?<<=?� & left-shift assignment \impl{?<<=?}\\ 3038 �?>>=?� & right-shift assignment \impl{?>>=?}\\ 3039 �?&=?� & bitwise AND assignment \impl{?&=?}\\ 3040 �?^=?� & exclusive OR assignment \impl{?^=?}\\ 3041 �?|=?� & inclusive OR assignment \impl{?"|=?}\\ 3042 3042 \end{tabular} 3043 3043 \hfil … … 3048 3048 These identifiers are defined such that the question marks in the name identify the location of the operands. 3049 3049 These operands represent the parameters to the functions, and define how the operands are mapped to the function call. 3050 For example, ©a + b© becomes ©?+?(a, b)©.3050 For example, �a + b� becomes �?+?(a, b)�. 3051 3051 3052 3052 In the example below, a new type, myComplex, is defined with an overloaded constructor, + operator, and string operator. … … 3122 3122 \end{itemize} 3123 3123 3124 In \CFA, ©typedef©provides a mechanism to alias long type names with short ones, both globally and locally, but not eliminate the use of the short name.3125 \Indexc{gcc} provides ©typeof©to declare a secondary variable from a primary variable.3124 In \CFA, �typedef� provides a mechanism to alias long type names with short ones, both globally and locally, but not eliminate the use of the short name. 3125 \Indexc{gcc} provides �typeof� to declare a secondary variable from a primary variable. 3126 3126 \CFA also relies heavily on the specification of the left-hand side of assignment for type inferencing, so in many cases it is crucial to specify the type of the left-hand side to select the correct type of the right-hand expression. 3127 3127 Only for overloaded routines with the same return type is variable type-inferencing possible. 3128 Finally, ©auto©presents the programming problem of tracking down a type when the type is actually needed.3128 Finally, �auto� presents the programming problem of tracking down a type when the type is actually needed. 3129 3129 For example, given 3130 3130 \begin{cfa} 3131 auto j = ®...®3132 \end{cfa} 3133 and the need to write a routine to compute using ©j©3134 \begin{cfa} 3135 void rtn( ®...®parm );3131 auto j = �...� 3132 \end{cfa} 3133 and the need to write a routine to compute using �j� 3134 \begin{cfa} 3135 void rtn( �...� parm ); 3136 3136 rtn( j ); 3137 3137 \end{cfa} 3138 A programmer must work backwards to determine the type of ©j©'s initialization expression, reconstructing the possibly long generic type-name.3138 A programmer must work backwards to determine the type of �j�'s initialization expression, reconstructing the possibly long generic type-name. 3139 3139 In this situation, having the type name or a short alias is very useful. 3140 3140 … … 3144 3144 At some point, a programmer wants the type of the variable to remain constant and the expression to be in error when it changes. 3145 3145 3146 Given ©typedef© and ©typeof©in \CFA, and the strong need to use the type of left-hand side in inferencing, auto type-inferencing is not supported at this time.3146 Given �typedef� and �typeof� in \CFA, and the strong need to use the type of left-hand side in inferencing, auto type-inferencing is not supported at this time. 3147 3147 Should a significant need arise, this feature can be revisited. 3148 3148 … … 3456 3456 3457 3457 coroutine Fibonacci { 3458 int fn; §\C{// used for communication}§3458 int fn; �\C{// used for communication}� 3459 3459 }; 3460 3460 void ?{}( Fibonacci * this ) { … … 3462 3462 } 3463 3463 void main( Fibonacci * this ) { 3464 int fn1, fn2; §\C{// retained between resumes}§3465 this->fn = 0; §\C{// case 0}§3464 int fn1, fn2; �\C{// retained between resumes}� 3465 this->fn = 0; �\C{// case 0}� 3466 3466 fn1 = this->fn; 3467 suspend(); §\C{// return to last resume}§3468 3469 this->fn = 1; §\C{// case 1}§3467 suspend(); �\C{// return to last resume}� 3468 3469 this->fn = 1; �\C{// case 1}� 3470 3470 fn2 = fn1; 3471 3471 fn1 = this->fn; 3472 suspend(); §\C{// return to last resume}§3473 3474 for ( ;; ) { §\C{// general case}§3472 suspend(); �\C{// return to last resume}� 3473 3474 for ( ;; ) { �\C{// general case}� 3475 3475 this->fn = fn1 + fn2; 3476 3476 fn2 = fn1; 3477 3477 fn1 = this->fn; 3478 suspend(); §\C{// return to last resume}§3478 suspend(); �\C{// return to last resume}� 3479 3479 } // for 3480 3480 } 3481 3481 int next( Fibonacci * this ) { 3482 resume( this ); §\C{// transfer to last suspend}§3482 resume( this ); �\C{// transfer to last suspend}� 3483 3483 return this->fn; 3484 3484 } … … 3622 3622 #include <thread> 3623 3623 3624 thread First { s ignal_once* lock; };3625 thread Second { s ignal_once* lock; };3626 3627 void ?{}( First * this, s ignal_once* lock ) { this->lock = lock; }3628 void ?{}( Second * this, s ignal_once* lock ) { this->lock = lock; }3629 3630 void main( First * this) {3631 for ( int i = 0; i < 10; i += 1) {3624 thread First { semaphore* lock; }; 3625 thread Second { semaphore* lock; }; 3626 3627 void ?{}( First * this, semaphore* lock ) { this->lock = lock; } 3628 void ?{}( Second * this, semaphore* lock ) { this->lock = lock; } 3629 3630 void main(First* this) { 3631 for(int i = 0; i < 10; i++) { 3632 3632 sout | "First : Suspend No." | i + 1 | endl; 3633 3633 yield(); 3634 3634 } 3635 signal( this->lock);3636 } 3637 3638 void main( Second * this) {3639 wait( this->lock);3640 for ( int i = 0; i < 10; i += 1) {3635 V(this->lock); 3636 } 3637 3638 void main(Second* this) { 3639 P(this->lock); 3640 for(int i = 0; i < 10; i++) { 3641 3641 sout | "Second : Suspend No." | i + 1 | endl; 3642 3642 yield(); … … 3644 3644 } 3645 3645 3646 int main( void ) { 3647 signal_once lock; 3646 3647 int main(int argc, char* argv[]) { 3648 semaphore lock = { 0 }; 3648 3649 sout | "User main begin" | endl; 3649 3650 { … … 3859 3860 In \CFA, multiple definitions are not necessary. 3860 3861 Within a module, all of the module's global definitions are visible throughout the module. 3861 For example, the following code compiles, even though ©isOdd©was not declared before being called:3862 For example, the following code compiles, even though �isOdd� was not declared before being called: 3862 3863 \begin{cfa} 3863 3864 bool isEven(unsigned int x) { … … 5188 5189 5189 5190 C has a number of syntax ambiguities, which are resolved by taking the longest sequence of overlapping characters that constitute a token. 5190 For example, the program fragment ©x+++++y© is parsed as \lstinline[showspaces=true]@x ++ ++ + y@ because operator tokens ©++© and ©+©overlap.5191 For example, the program fragment �x+++++y� is parsed as \lstinline[showspaces=true]@x ++ ++ + y@ because operator tokens �++� and �+� overlap. 5191 5192 Unfortunately, the longest sequence violates a constraint on increment operators, even though the parse \lstinline[showspaces=true]@x ++ + ++ y@ might yield a correct expression. 5192 5193 Hence, C programmers are aware that spaces have to added to disambiguate certain syntactic cases. 5193 5194 5194 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?©can be interpreted as:5195 \begin{cfa} 5196 *? §\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§5197 * §\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§5195 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg �int *?*?()�, where the string �*?*?� can be interpreted as: 5196 \begin{cfa} 5197 *?�\color{red}\textvisiblespace�*? �\C{// dereference operator, dereference operator}� 5198 *�\color{red}\textvisiblespace�?*? �\C{// dereference, multiplication operator}� 5198 5199 \end{cfa} 5199 5200 By default, the first interpretation is selected, which does not yield a meaningful parse. … … 5201 5202 Otherwise a space is needed between the unary operator and operator identifier to disambiguate this common case. 5202 5203 5203 A similar issue occurs with the dereference, ©*?(...)©, and routine-call, ©?()(...)©identifiers.5204 A similar issue occurs with the dereference, �*?(...)�, and routine-call, �?()(...)� identifiers. 5204 5205 The ambiguity occurs when the deference operator has no parameters: 5205 5206 \begin{cfa} 5206 *?() §\color{red}\textvisiblespace...§;5207 *?() §\color{red}\textvisiblespace...§(...) ;5207 *?()�\color{red}\textvisiblespace...� ; 5208 *?()�\color{red}\textvisiblespace...�(...) ; 5208 5209 \end{cfa} 5209 5210 requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate. 5210 However, the dereference operator \emph{must} have a parameter/argument to dereference ©*?(...)©.5211 Hence, always interpreting the string ©*?()©as \lstinline[showspaces=true]@* ?()@ does not preclude any meaningful program.5211 However, the dereference operator \emph{must} have a parameter/argument to dereference �*?(...)�. 5212 Hence, always interpreting the string �*?()� as \lstinline[showspaces=true]@* ?()@ does not preclude any meaningful program. 5212 5213 5213 5214 The remaining cases are with the increment/decrement operators and conditional expression, \eg: 5214 5215 \begin{cfa} 5215 i++? §\color{red}\textvisiblespace...§(...);5216 i?++ §\color{red}\textvisiblespace...§(...);5216 i++?�\color{red}\textvisiblespace...�(...); 5217 i?++�\color{red}\textvisiblespace...�(...); 5217 5218 \end{cfa} 5218 5219 requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers). 5219 5220 Therefore, it is necessary to disambiguate these cases with a space: 5220 5221 \begin{cfa} 5221 i++ §\color{red}\textvisiblespace§? i : 0;5222 i? §\color{red}\textvisiblespace§++i : 0;5222 i++�\color{red}\textvisiblespace�? i : 0; 5223 i?�\color{red}\textvisiblespace�++i : 0; 5223 5224 \end{cfa} 5224 5225 … … 5230 5231 \begin{tabular}{llll} 5231 5232 \begin{tabular}{@{}l@{}} 5232 ©_AT©\\5233 ©catch©\\5234 ©catchResume©\\5235 ©choose©\\5236 ©coroutine©\\5237 ©disable©\\5233 �_AT� \\ 5234 �catch� \\ 5235 �catchResume� \\ 5236 �choose� \\ 5237 �coroutine� \\ 5238 �disable� \\ 5238 5239 \end{tabular} 5239 5240 & 5240 5241 \begin{tabular}{@{}l@{}} 5241 ©dtype©\\5242 ©enable©\\5243 ©fallthrough©\\5244 ©fallthru©\\5245 ©finally©\\5246 ©forall©\\5242 �dtype� \\ 5243 �enable� \\ 5244 �fallthrough� \\ 5245 �fallthru� \\ 5246 �finally� \\ 5247 �forall� \\ 5247 5248 \end{tabular} 5248 5249 & 5249 5250 \begin{tabular}{@{}l@{}} 5250 ©ftype©\\5251 ©lvalue©\\5252 ©monitor©\\5253 ©mutex©\\5254 ©one_t©\\5255 ©otype©\\5251 �ftype� \\ 5252 �lvalue� \\ 5253 �monitor� \\ 5254 �mutex� \\ 5255 �one_t� \\ 5256 �otype� \\ 5256 5257 \end{tabular} 5257 5258 & 5258 5259 \begin{tabular}{@{}l@{}} 5259 ©throw©\\5260 ©throwResume©\\5261 ©trait©\\5262 ©try©\\5263 ©ttype©\\5264 ©zero_t©\\5260 �throw� \\ 5261 �throwResume� \\ 5262 �trait� \\ 5263 �try� \\ 5264 �ttype� \\ 5265 �zero_t� \\ 5265 5266 \end{tabular} 5266 5267 \end{tabular} … … 5288 5289 \begin{description} 5289 5290 \item[Change:] drop K\&R C declarations \\ 5290 K\&R declarations allow an implicit base-type of ©int©, if no type is specified, plus an alternate syntax for declaring parameters.5291 K\&R declarations allow an implicit base-type of �int�, if no type is specified, plus an alternate syntax for declaring parameters. 5291 5292 \eg: 5292 5293 \begin{cfa} 5293 x; §\C{// int x}§5294 *y; §\C{// int *y}§5295 f( p1, p2 ); §\C{// int f( int p1, int p2 );}§5296 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§5294 x; �\C{// int x}� 5295 *y; �\C{// int *y}� 5296 f( p1, p2 ); �\C{// int f( int p1, int p2 );}� 5297 g( p1, p2 ) int p1, p2; �\C{// int g( int p1, int p2 );}� 5297 5298 \end{cfa} 5298 5299 \CFA supports K\&R routine definitions: 5299 5300 \begin{cfa} 5300 f( a, b, c ) §\C{// default int return}§5301 int a, b; char c §\C{// K\&R parameter declarations}§5301 f( a, b, c ) �\C{// default int return}� 5302 int a, b; char c �\C{// K\&R parameter declarations}� 5302 5303 { 5303 5304 ... … … 5314 5315 \item 5315 5316 \begin{description} 5316 \item[Change:] type of character literal ©int© to ©char©to allow more intuitive overloading:5317 \item[Change:] type of character literal �int� to �char� to allow more intuitive overloading: 5317 5318 \begin{cfa} 5318 5319 int rtn( int i ); 5319 5320 int rtn( char c ); 5320 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§5321 \end{cfa} 5322 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn©rather than the first.5323 In particular, output of ©char©variable now print a character rather than the decimal ASCII value of the character.5321 rtn( 'x' ); �\C{// programmer expects 2nd rtn to be called}� 5322 \end{cfa} 5323 \item[Rationale:] it is more intuitive for the call to �rtn� to match the second version of definition of �rtn� rather than the first. 5324 In particular, output of �char� variable now print a character rather than the decimal ASCII value of the character. 5324 5325 \begin{cfa} 5325 5326 sout | 'x' | " " | (int)'x' | endl; 5326 5327 x 120 5327 5328 \end{cfa} 5328 Having to cast ©'x'© to ©char©is non-intuitive.5329 Having to cast �'x'� to �char� is non-intuitive. 5329 5330 \item[Effect on original feature:] change to semantics of well-defined feature that depend on: 5330 5331 \begin{cfa} … … 5333 5334 no long work the same in \CFA programs. 5334 5335 \item[Difficulty of converting:] simple 5335 \item[How widely used:] programs that depend upon ©sizeof( 'x' )© are rare and can be changed to ©sizeof(char)©.5336 \item[How widely used:] programs that depend upon �sizeof( 'x' )� are rare and can be changed to �sizeof(char)�. 5336 5337 \end{description} 5337 5338 5338 5339 \item 5339 5340 \begin{description} 5340 \item[Change:] make string literals ©const©:5341 \begin{cfa} 5342 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§5343 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§5344 \end{cfa} 5345 The type of a string literal is changed from ©[] char© to ©const [] char©.5346 Similarly, the type of a wide string literal is changed from ©[] wchar_t© to ©const [] wchar_t©.5341 \item[Change:] make string literals �const�: 5342 \begin{cfa} 5343 char * p = "abc"; �\C{// valid in C, deprecated in \CFA}� 5344 char * q = expr ? "abc" : "de"; �\C{// valid in C, invalid in \CFA}� 5345 \end{cfa} 5346 The type of a string literal is changed from �[] char� to �const [] char�. 5347 Similarly, the type of a wide string literal is changed from �[] wchar_t� to �const [] wchar_t�. 5347 5348 \item[Rationale:] This change is a safety issue: 5348 5349 \begin{cfa} 5349 5350 char * p = "abc"; 5350 p[0] = 'w'; §\C{// segment fault or change constant literal}§5351 p[0] = 'w'; �\C{// segment fault or change constant literal}� 5351 5352 \end{cfa} 5352 5353 The same problem occurs when passing a string literal to a routine that changes its argument. 5353 5354 \item[Effect on original feature:] change to semantics of well-defined feature. 5354 \item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to ©char *©.5355 \item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to �char *�. 5355 5356 \item[How widely used:] programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are rare. 5356 5357 \end{description} … … 5360 5361 \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: 5361 5362 \begin{cfa} 5362 int i; §\C{// forward definition}§5363 int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§5364 int i = 0; §\C{// definition}§5363 int i; �\C{// forward definition}� 5364 int *j = �&i�; �\C{// forward reference, valid in C, invalid in \CFA}� 5365 int i = 0; �\C{// definition}� 5365 5366 \end{cfa} 5366 5367 is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. … … 5368 5369 \begin{cfa} 5369 5370 struct X { int i; struct X *next; }; 5370 static struct X a; §\C{// forward definition}§5371 static struct X b = { 0, ®&a® }; §\C{// forward reference, valid in C, invalid in \CFA}§5372 static struct X a = { 1, &b }; §\C{// definition}§5371 static struct X a; �\C{// forward definition}� 5372 static struct X b = { 0, �&a� }; �\C{// forward reference, valid in C, invalid in \CFA}� 5373 static struct X a = { 1, &b }; �\C{// definition}� 5373 5374 \end{cfa} 5374 5375 \item[Rationale:] avoids having different initialization rules for builtin types and user-defined types. … … 5380 5381 \item 5381 5382 \begin{description} 5382 \item[Change:] have ©struct©introduce a scope for nested types:5383 \begin{cfa} 5384 enum ®Colour®{ R, G, B, Y, C, M };5383 \item[Change:] have �struct� introduce a scope for nested types: 5384 \begin{cfa} 5385 enum �Colour� { R, G, B, Y, C, M }; 5385 5386 struct Person { 5386 enum ®Colour® { R, G, B }; §\C{// nested type}§5387 struct Face { §\C{// nested type}§5388 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§5387 enum �Colour� { R, G, B }; �\C{// nested type}� 5388 struct Face { �\C{// nested type}� 5389 �Colour� Eyes, Hair; �\C{// type defined outside (1 level)}� 5389 5390 }; 5390 ®.Colour® shirt; §\C{// type defined outside (top level)}§5391 ®Colour® pants; §\C{// type defined same level}§5392 Face looks[10]; §\C{// type defined same level}§5391 �.Colour� shirt; �\C{// type defined outside (top level)}� 5392 �Colour� pants; �\C{// type defined same level}� 5393 Face looks[10]; �\C{// type defined same level}� 5393 5394 }; 5394 ®Colour® c = R; §\C{// type/enum defined same level}§ 5395 Person ®.Colour® pc = Person®.®R; §\C{// type/enum defined inside}§5396 Person ®.®Face pretty; §\C{// type defined inside}§5395 �Colour� c = R; �\C{// type/enum defined same level}� 5396 Person�.Colour� pc = Person�.�R; �\C{// type/enum defined inside}� 5397 Person�.�Face pretty; �\C{// type defined inside}� 5397 5398 \end{cfa} 5398 5399 In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing structure, \ie the nested types are hoisted to the scope of the outer-most type, which is not useful and confusing. 5399 5400 \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \Index*[C++]{\CC{}}. 5400 Nested types are not hoisted and can be referenced using the field selection operator `` ©.©'', unlike the \CC scope-resolution operator ``©::©''.5401 \item[Rationale:] ©struct©scope is crucial to \CFA as an information structuring and hiding mechanism.5401 Nested types are not hoisted and can be referenced using the field selection operator ``�.�'', unlike the \CC scope-resolution operator ``�::�''. 5402 \item[Rationale:] �struct� scope is crucial to \CFA as an information structuring and hiding mechanism. 5402 5403 \item[Effect on original feature:] change to semantics of well-defined feature. 5403 5404 \item[Difficulty of converting:] Semantic transformation. … … 5411 5412 \item[Difficulty of converting:] Semantic transformation. To make the struct type name visible in the scope of the enclosing struct, the struct tag could be declared in the scope of the enclosing struct, before the enclosing struct is defined. Example: 5412 5413 \begin{cfa} 5413 struct Y; §\C{// struct Y and struct X are at the same scope}§5414 struct Y; �\C{// struct Y and struct X are at the same scope}� 5414 5415 struct X { 5415 5416 struct Y { /* ... */ } y; … … 5424 5425 \begin{description} 5425 5426 \item[Change:] comma expression is disallowed as subscript 5426 \item[Rationale:] safety issue to prevent subscripting error for multidimensional arrays: ©x[i,j]© instead of ©x[i][j]©, and this syntactic form then taken by \CFA for new style arrays.5427 \item[Rationale:] safety issue to prevent subscripting error for multidimensional arrays: �x[i,j]� instead of �x[i][j]�, and this syntactic form then taken by \CFA for new style arrays. 5427 5428 \item[Effect on original feature:] change to semantics of well-defined feature. 5428 \item[Difficulty of converting:] semantic transformation of ©x[i,j]© to ©x[(i,j)]©5429 \item[Difficulty of converting:] semantic transformation of �x[i,j]� to �x[(i,j)]� 5429 5430 \item[How widely used:] seldom. 5430 5431 \end{description} … … 5497 5498 \end{tabular} 5498 5499 \end{quote2} 5499 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©;5500 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an �extern "C"�; 5500 5501 hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}). 5501 All other C header files must be explicitly wrapped in ©extern "C"©to prevent name mangling.5502 For \Index*[C++]{\CC{}}, the name-mangling issue is handled implicitly because most C header-files are augmented with checks for preprocessor variable ©__cplusplus©, which adds appropriate ©extern "C"©qualifiers.5502 All other C header files must be explicitly wrapped in �extern "C"� to prevent name mangling. 5503 For \Index*[C++]{\CC{}}, the name-mangling issue is handled implicitly because most C header-files are augmented with checks for preprocessor variable �__cplusplus�, which adds appropriate �extern "C"� qualifiers. 5503 5504 5504 5505 … … 5532 5533 & & \multicolumn{1}{c|}{fill} & resize & alignment & array \\ 5533 5534 \hline 5534 C & ©malloc©& no & no & no & no \\5535 & ©calloc©& yes (0 only) & no & no & yes \\5536 & ©realloc©& no/copy & yes & no & no \\5537 & ©memalign©& no & no & yes & no \\5538 & ©posix_memalign©& no & no & yes & no \\5539 C11 & ©aligned_alloc©& no & no & yes & no \\5540 \CFA & ©alloc©& no/copy/yes & no/yes & no & yes \\5541 & ©align_alloc©& no/yes & no & yes & yes \\5535 C & �malloc� & no & no & no & no \\ 5536 & �calloc� & yes (0 only) & no & no & yes \\ 5537 & �realloc� & no/copy & yes & no & no \\ 5538 & �memalign� & no & no & yes & no \\ 5539 & �posix_memalign� & no & no & yes & no \\ 5540 C11 & �aligned_alloc� & no & no & yes & no \\ 5541 \CFA & �alloc� & no/copy/yes & no/yes & no & yes \\ 5542 & �align_alloc� & no/yes & no & yes & yes \\ 5542 5543 \end{tabular} 5543 5544 \end{center} 5544 It is impossible to resize with alignment because the underlying ©realloc©allocates storage if more space is needed, and it does not honour alignment from the original allocation.5545 It is impossible to resize with alignment because the underlying �realloc� allocates storage if more space is needed, and it does not honour alignment from the original allocation. 5545 5546 5546 5547 \leavevmode … … 5548 5549 // C unsafe allocation 5549 5550 extern "C" { 5550 void * mallac( size_t size ); §\indexc{memset}§5551 void * calloc( size_t dim, size_t size ); §\indexc{calloc}§5552 void * realloc( void * ptr, size_t size ); §\indexc{realloc}§5553 void * memalign( size_t align, size_t size ); §\indexc{memalign}§5554 int posix_memalign( void ** ptr, size_t align, size_t size ); §\indexc{posix_memalign}§5555 } 5556 5557 // §\CFA§safe equivalents, i.e., implicit size specification5551 void * mallac( size_t size );�\indexc{memset}� 5552 void * calloc( size_t dim, size_t size );�\indexc{calloc}� 5553 void * realloc( void * ptr, size_t size );�\indexc{realloc}� 5554 void * memalign( size_t align, size_t size );�\indexc{memalign}� 5555 int posix_memalign( void ** ptr, size_t align, size_t size );�\indexc{posix_memalign}� 5556 } 5557 5558 // �\CFA� safe equivalents, i.e., implicit size specification 5558 5559 forall( dtype T | sized(T) ) T * malloc( void ); 5559 5560 forall( dtype T | sized(T) ) T * calloc( size_t dim ); … … 5563 5564 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ); 5564 5565 5565 // §\CFA§safe general allocation, fill, resize, array5566 forall( dtype T | sized(T) ) T * alloc( void ); §\indexc{alloc}§5566 // �\CFA� safe general allocation, fill, resize, array 5567 forall( dtype T | sized(T) ) T * alloc( void );�\indexc{alloc}� 5567 5568 forall( dtype T | sized(T) ) T * alloc( char fill ); 5568 5569 forall( dtype T | sized(T) ) T * alloc( size_t dim ); … … 5571 5572 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); 5572 5573 5573 // §\CFA§safe general allocation, align, fill, array5574 // �\CFA� safe general allocation, align, fill, array 5574 5575 forall( dtype T | sized(T) ) T * align_alloc( size_t align ); 5575 5576 forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ); … … 5583 5584 } 5584 5585 5585 // §\CFA§safe initialization/copy, i.e., implicit size specification5586 forall( dtype T | sized(T) ) T * memset( T * dest, char c ); §\indexc{memset}§5587 forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src ); §\indexc{memcpy}§5588 5589 // §\CFA§safe initialization/copy array5586 // �\CFA� safe initialization/copy, i.e., implicit size specification 5587 forall( dtype T | sized(T) ) T * memset( T * dest, char c );�\indexc{memset}� 5588 forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src );�\indexc{memcpy}� 5589 5590 // �\CFA� safe initialization/copy array 5590 5591 forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ); 5591 5592 forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ); 5592 5593 5593 // §\CFA§allocation/deallocation and constructor/destructor5594 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p ); §\indexc{new}§5595 forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr ); §\indexc{delete}§5594 // �\CFA� allocation/deallocation and constructor/destructor 5595 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );�\indexc{new}� 5596 forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );�\indexc{delete}� 5596 5597 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) 5597 5598 void delete( T * ptr, Params rest ); 5598 5599 5599 // §\CFA§allocation/deallocation and constructor/destructor, array5600 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p ); §\indexc{anew}§5601 forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] ); §\indexc{adelete}§5600 // �\CFA� allocation/deallocation and constructor/destructor, array 5601 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );�\indexc{anew}� 5602 forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );�\indexc{adelete}� 5602 5603 forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } ) 5603 5604 void adelete( size_t dim, T arr[], Params rest ); … … 5609 5610 \leavevmode 5610 5611 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5611 int ato( const char * ptr ); §\indexc{ato}§5612 int ato( const char * ptr );�\indexc{ato}� 5612 5613 unsigned int ato( const char * ptr ); 5613 5614 long int ato( const char * ptr ); … … 5641 5642 \leavevmode 5642 5643 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5643 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§5644 T * bsearch( T key, const T * arr, size_t dim ); §\indexc{bsearch}§5645 5646 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§5644 forall( otype T | { int ?<?( T, T ); } ) �\C{// location}� 5645 T * bsearch( T key, const T * arr, size_t dim );�\indexc{bsearch}� 5646 5647 forall( otype T | { int ?<?( T, T ); } ) �\C{// position}� 5647 5648 unsigned int bsearch( T key, const T * arr, size_t dim ); 5648 5649 5649 5650 forall( otype T | { int ?<?( T, T ); } ) 5650 void qsort( const T * arr, size_t dim ); §\indexc{qsort}§5651 void qsort( const T * arr, size_t dim );�\indexc{qsort}� 5651 5652 \end{cfa} 5652 5653 … … 5656 5657 \leavevmode 5657 5658 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5658 unsigned char abs( signed char ); §\indexc{abs}§5659 unsigned char abs( signed char );�\indexc{abs}� 5659 5660 int abs( int ); 5660 5661 unsigned long int abs( long int ); … … 5675 5676 \leavevmode 5676 5677 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5677 void rand48seed( long int s ); §\indexc{rand48seed}§5678 char rand48(); §\indexc{rand48}§5678 void rand48seed( long int s );�\indexc{rand48seed}� 5679 char rand48();�\indexc{rand48}� 5679 5680 int rand48(); 5680 5681 unsigned int rand48(); … … 5693 5694 \leavevmode 5694 5695 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5695 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 ); §\indexc{min}§5696 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 ); §\indexc{max}§5697 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val ); §\indexc{clamp}§5698 forall( otype T ) void swap( T * t1, T * t2 ); §\indexc{swap}§5696 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );�\indexc{min}� 5697 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );�\indexc{max}� 5698 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );�\indexc{clamp}� 5699 forall( otype T ) void swap( T * t1, T * t2 );�\indexc{swap}� 5699 5700 \end{cfa} 5700 5701 … … 5710 5711 \leavevmode 5711 5712 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5712 float ?%?( float, float ); §\indexc{fmod}§5713 float ?%?( float, float );�\indexc{fmod}� 5713 5714 float fmod( float, float ); 5714 5715 double ?%?( double, double ); … … 5717 5718 long double fmod( long double, long double ); 5718 5719 5719 float remainder( float, float ); §\indexc{remainder}§5720 float remainder( float, float );�\indexc{remainder}� 5720 5721 double remainder( double, double ); 5721 5722 long double remainder( long double, long double ); 5722 5723 5723 [ int, float ] remquo( float, float ); §\indexc{remquo}§5724 [ int, float ] remquo( float, float );�\indexc{remquo}� 5724 5725 float remquo( float, float, int * ); 5725 5726 [ int, double ] remquo( double, double ); … … 5729 5730 5730 5731 [ int, float ] div( float, float ); // alternative name for remquo 5731 float div( float, float, int * ); §\indexc{div}§5732 float div( float, float, int * );�\indexc{div}� 5732 5733 [ int, double ] div( double, double ); 5733 5734 double div( double, double, int * ); … … 5735 5736 long double div( long double, long double, int * ); 5736 5737 5737 float fma( float, float, float ); §\indexc{fma}§5738 float fma( float, float, float );�\indexc{fma}� 5738 5739 double fma( double, double, double ); 5739 5740 long double fma( long double, long double, long double ); 5740 5741 5741 float fdim( float, float ); §\indexc{fdim}§5742 float fdim( float, float );�\indexc{fdim}� 5742 5743 double fdim( double, double ); 5743 5744 long double fdim( long double, long double ); 5744 5745 5745 float nan( const char * ); §\indexc{nan}§5746 float nan( const char * );�\indexc{nan}� 5746 5747 double nan( const char * ); 5747 5748 long double nan( const char * ); … … 5753 5754 \leavevmode 5754 5755 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5755 float exp( float ); §\indexc{exp}§5756 float exp( float );�\indexc{exp}� 5756 5757 double exp( double ); 5757 5758 long double exp( long double ); … … 5760 5761 long double _Complex exp( long double _Complex ); 5761 5762 5762 float exp2( float ); §\indexc{exp2}§5763 float exp2( float );�\indexc{exp2}� 5763 5764 double exp2( double ); 5764 5765 long double exp2( long double ); … … 5767 5768 long double _Complex exp2( long double _Complex ); 5768 5769 5769 float expm1( float ); §\indexc{expm1}§5770 float expm1( float );�\indexc{expm1}� 5770 5771 double expm1( double ); 5771 5772 long double expm1( long double ); 5772 5773 5773 float log( float ); §\indexc{log}§5774 float log( float );�\indexc{log}� 5774 5775 double log( double ); 5775 5776 long double log( long double ); … … 5778 5779 long double _Complex log( long double _Complex ); 5779 5780 5780 float log2( float ); §\indexc{log2}§5781 float log2( float );�\indexc{log2}� 5781 5782 double log2( double ); 5782 5783 long double log2( long double ); … … 5785 5786 long double _Complex log2( long double _Complex ); 5786 5787 5787 float log10( float ); §\indexc{log10}§5788 float log10( float );�\indexc{log10}� 5788 5789 double log10( double ); 5789 5790 long double log10( long double ); … … 5792 5793 long double _Complex log10( long double _Complex ); 5793 5794 5794 float log1p( float ); §\indexc{log1p}§5795 float log1p( float );�\indexc{log1p}� 5795 5796 double log1p( double ); 5796 5797 long double log1p( long double ); 5797 5798 5798 int ilogb( float ); §\indexc{ilogb}§5799 int ilogb( float );�\indexc{ilogb}� 5799 5800 int ilogb( double ); 5800 5801 int ilogb( long double ); 5801 5802 5802 float logb( float ); §\indexc{logb}§5803 float logb( float );�\indexc{logb}� 5803 5804 double logb( double ); 5804 5805 long double logb( long double ); … … 5810 5811 \leavevmode 5811 5812 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5812 float sqrt( float ); §\indexc{sqrt}§5813 float sqrt( float );�\indexc{sqrt}� 5813 5814 double sqrt( double ); 5814 5815 long double sqrt( long double ); … … 5817 5818 long double _Complex sqrt( long double _Complex ); 5818 5819 5819 float cbrt( float ); §\indexc{cbrt}§5820 float cbrt( float );�\indexc{cbrt}� 5820 5821 double cbrt( double ); 5821 5822 long double cbrt( long double ); 5822 5823 5823 float hypot( float, float ); §\indexc{hypot}§5824 float hypot( float, float );�\indexc{hypot}� 5824 5825 double hypot( double, double ); 5825 5826 long double hypot( long double, long double ); 5826 5827 5827 float pow( float, float ); §\indexc{pow}§5828 float pow( float, float );�\indexc{pow}� 5828 5829 double pow( double, double ); 5829 5830 long double pow( long double, long double ); … … 5838 5839 \leavevmode 5839 5840 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5840 float sin( float ); §\indexc{sin}§5841 float sin( float );�\indexc{sin}� 5841 5842 double sin( double ); 5842 5843 long double sin( long double ); … … 5845 5846 long double _Complex sin( long double _Complex ); 5846 5847 5847 float cos( float ); §\indexc{cos}§5848 float cos( float );�\indexc{cos}� 5848 5849 double cos( double ); 5849 5850 long double cos( long double ); … … 5852 5853 long double _Complex cos( long double _Complex ); 5853 5854 5854 float tan( float ); §\indexc{tan}§5855 float tan( float );�\indexc{tan}� 5855 5856 double tan( double ); 5856 5857 long double tan( long double ); … … 5859 5860 long double _Complex tan( long double _Complex ); 5860 5861 5861 float asin( float ); §\indexc{asin}§5862 float asin( float );�\indexc{asin}� 5862 5863 double asin( double ); 5863 5864 long double asin( long double ); … … 5866 5867 long double _Complex asin( long double _Complex ); 5867 5868 5868 float acos( float ); §\indexc{acos}§5869 float acos( float );�\indexc{acos}� 5869 5870 double acos( double ); 5870 5871 long double acos( long double ); … … 5873 5874 long double _Complex acos( long double _Complex ); 5874 5875 5875 float atan( float ); §\indexc{atan}§5876 float atan( float );�\indexc{atan}� 5876 5877 double atan( double ); 5877 5878 long double atan( long double ); … … 5880 5881 long double _Complex atan( long double _Complex ); 5881 5882 5882 float atan2( float, float ); §\indexc{atan2}§5883 float atan2( float, float );�\indexc{atan2}� 5883 5884 double atan2( double, double ); 5884 5885 long double atan2( long double, long double ); 5885 5886 5886 5887 float atan( float, float ); // alternative name for atan2 5887 double atan( double, double ); §\indexc{atan}§5888 double atan( double, double );�\indexc{atan}� 5888 5889 long double atan( long double, long double ); 5889 5890 \end{cfa} … … 5894 5895 \leavevmode 5895 5896 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5896 float sinh( float ); §\indexc{sinh}§5897 float sinh( float );�\indexc{sinh}� 5897 5898 double sinh( double ); 5898 5899 long double sinh( long double ); … … 5901 5902 long double _Complex sinh( long double _Complex ); 5902 5903 5903 float cosh( float ); §\indexc{cosh}§5904 float cosh( float );�\indexc{cosh}� 5904 5905 double cosh( double ); 5905 5906 long double cosh( long double ); … … 5908 5909 long double _Complex cosh( long double _Complex ); 5909 5910 5910 float tanh( float ); §\indexc{tanh}§5911 float tanh( float );�\indexc{tanh}� 5911 5912 double tanh( double ); 5912 5913 long double tanh( long double ); … … 5915 5916 long double _Complex tanh( long double _Complex ); 5916 5917 5917 float asinh( float ); §\indexc{asinh}§5918 float asinh( float );�\indexc{asinh}� 5918 5919 double asinh( double ); 5919 5920 long double asinh( long double ); … … 5922 5923 long double _Complex asinh( long double _Complex ); 5923 5924 5924 float acosh( float ); §\indexc{acosh}§5925 float acosh( float );�\indexc{acosh}� 5925 5926 double acosh( double ); 5926 5927 long double acosh( long double ); … … 5929 5930 long double _Complex acosh( long double _Complex ); 5930 5931 5931 float atanh( float ); §\indexc{atanh}§5932 float atanh( float );�\indexc{atanh}� 5932 5933 double atanh( double ); 5933 5934 long double atanh( long double ); … … 5942 5943 \leavevmode 5943 5944 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5944 float erf( float ); §\indexc{erf}§5945 float erf( float );�\indexc{erf}� 5945 5946 double erf( double ); 5946 5947 long double erf( long double ); … … 5949 5950 long double _Complex erf( long double _Complex ); 5950 5951 5951 float erfc( float ); §\indexc{erfc}§5952 float erfc( float );�\indexc{erfc}� 5952 5953 double erfc( double ); 5953 5954 long double erfc( long double ); … … 5956 5957 long double _Complex erfc( long double _Complex ); 5957 5958 5958 float lgamma( float ); §\indexc{lgamma}§5959 float lgamma( float );�\indexc{lgamma}� 5959 5960 double lgamma( double ); 5960 5961 long double lgamma( long double ); … … 5963 5964 long double lgamma( long double, int * ); 5964 5965 5965 float tgamma( float ); §\indexc{tgamma}§5966 float tgamma( float );�\indexc{tgamma}� 5966 5967 double tgamma( double ); 5967 5968 long double tgamma( long double ); … … 5973 5974 \leavevmode 5974 5975 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5975 float floor( float ); §\indexc{floor}§5976 float floor( float );�\indexc{floor}� 5976 5977 double floor( double ); 5977 5978 long double floor( long double ); 5978 5979 5979 float ceil( float ); §\indexc{ceil}§5980 float ceil( float );�\indexc{ceil}� 5980 5981 double ceil( double ); 5981 5982 long double ceil( long double ); 5982 5983 5983 float trunc( float ); §\indexc{trunc}§5984 float trunc( float );�\indexc{trunc}� 5984 5985 double trunc( double ); 5985 5986 long double trunc( long double ); 5986 5987 5987 float rint( float ); §\indexc{rint}§5988 float rint( float );�\indexc{rint}� 5988 5989 long double rint( long double ); 5989 5990 long int rint( float ); … … 5994 5995 long long int rint( long double ); 5995 5996 5996 long int lrint( float ); §\indexc{lrint}§5997 long int lrint( float );�\indexc{lrint}� 5997 5998 long int lrint( double ); 5998 5999 long int lrint( long double ); … … 6001 6002 long long int llrint( long double ); 6002 6003 6003 float nearbyint( float ); §\indexc{nearbyint}§6004 float nearbyint( float );�\indexc{nearbyint}� 6004 6005 double nearbyint( double ); 6005 6006 long double nearbyint( long double ); 6006 6007 6007 float round( float ); §\indexc{round}§6008 float round( float );�\indexc{round}� 6008 6009 long double round( long double ); 6009 6010 long int round( float ); … … 6014 6015 long long int round( long double ); 6015 6016 6016 long int lround( float ); §\indexc{lround}§6017 long int lround( float );�\indexc{lround}� 6017 6018 long int lround( double ); 6018 6019 long int lround( long double ); … … 6027 6028 \leavevmode 6028 6029 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6029 float copysign( float, float ); §\indexc{copysign}§6030 float copysign( float, float );�\indexc{copysign}� 6030 6031 double copysign( double, double ); 6031 6032 long double copysign( long double, long double ); 6032 6033 6033 float frexp( float, int * ); §\indexc{frexp}§6034 float frexp( float, int * );�\indexc{frexp}� 6034 6035 double frexp( double, int * ); 6035 6036 long double frexp( long double, int * ); 6036 6037 6037 float ldexp( float, int ); §\indexc{ldexp}§6038 float ldexp( float, int );�\indexc{ldexp}� 6038 6039 double ldexp( double, int ); 6039 6040 long double ldexp( long double, int ); 6040 6041 6041 [ float, float ] modf( float ); §\indexc{modf}§6042 [ float, float ] modf( float );�\indexc{modf}� 6042 6043 float modf( float, float * ); 6043 6044 [ double, double ] modf( double ); … … 6046 6047 long double modf( long double, long double * ); 6047 6048 6048 float nextafter( float, float ); §\indexc{nextafter}§6049 float nextafter( float, float );�\indexc{nextafter}� 6049 6050 double nextafter( double, double ); 6050 6051 long double nextafter( long double, long double ); 6051 6052 6052 float nexttoward( float, long double ); §\indexc{nexttoward}§6053 float nexttoward( float, long double );�\indexc{nexttoward}� 6053 6054 double nexttoward( double, long double ); 6054 6055 long double nexttoward( long double, long double ); 6055 6056 6056 float scalbn( float, int ); §\indexc{scalbn}§6057 float scalbn( float, int );�\indexc{scalbn}� 6057 6058 double scalbn( double, int ); 6058 6059 long double scalbn( long double, int ); 6059 6060 6060 float scalbln( float, long int ); §\indexc{scalbln}§6061 float scalbln( float, long int );�\indexc{scalbln}� 6061 6062 double scalbln( double, long int ); 6062 6063 long double scalbln( long double, long int ); … … 6072 6073 6073 6074 \begin{cfa} 6074 void ?{}( Int * this ); §\C{// constructor}§6075 void ?{}( Int * this ); �\C{// constructor}� 6075 6076 void ?{}( Int * this, Int init ); 6076 6077 void ?{}( Int * this, zero_t ); … … 6081 6082 void ^?{}( Int * this ); 6082 6083 6083 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§6084 Int ?=?( Int * lhs, Int rhs ); �\C{// assignment}� 6084 6085 Int ?=?( Int * lhs, long int rhs ); 6085 6086 Int ?=?( Int * lhs, unsigned long int rhs ); … … 6098 6099 unsigned long int narrow( Int val ); 6099 6100 6100 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§6101 int ?==?( Int oper1, Int oper2 ); �\C{// comparison}� 6101 6102 int ?==?( Int oper1, long int oper2 ); 6102 6103 int ?==?( long int oper2, Int oper1 ); … … 6134 6135 int ?>=?( unsigned long int oper1, Int oper2 ); 6135 6136 6136 Int +?( Int oper ); §\C{// arithmetic}§6137 Int +?( Int oper ); �\C{// arithmetic}� 6137 6138 Int -?( Int oper ); 6138 6139 Int ~?( Int oper ); … … 6216 6217 Int ?>>=?( Int * lhs, mp_bitcnt_t shift ); 6217 6218 6218 Int abs( Int oper ); §\C{// number functions}§6219 Int abs( Int oper ); �\C{// number functions}� 6219 6220 Int fact( unsigned long int N ); 6220 6221 Int gcd( Int oper1, Int oper2 ); … … 6228 6229 Int sqrt( Int oper ); 6229 6230 6230 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); §\C{// I/O}§6231 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); �\C{// I/O}� 6231 6232 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp ); 6232 6233 \end{cfa} … … 6239 6240 \hline 6240 6241 \begin{cfa} 6241 #include <gmp> §\indexc{gmp}§6242 #include <gmp>�\indexc{gmp}� 6242 6243 int main( void ) { 6243 6244 sout | "Factorial Numbers" | endl; … … 6253 6254 & 6254 6255 \begin{cfa} 6255 #include <gmp.h> §\indexc{gmp.h}§6256 #include <gmp.h>�\indexc{gmp.h}� 6256 6257 int main( void ) { 6257 ®gmp_printf®( "Factorial Numbers\n" );6258 ®mpz_t®fact;6259 ®mpz_init_set_ui®( fact, 1 );6260 ®gmp_printf®( "%d %Zd\n", 0, fact );6258 �gmp_printf�( "Factorial Numbers\n" ); 6259 �mpz_t� fact; 6260 �mpz_init_set_ui�( fact, 1 ); 6261 �gmp_printf�( "%d %Zd\n", 0, fact ); 6261 6262 for ( unsigned int i = 1; i <= 40; i += 1 ) { 6262 ®mpz_mul_ui®( fact, fact, i );6263 ®gmp_printf®( "%d %Zd\n", i, fact );6263 �mpz_mul_ui�( fact, fact, i ); 6264 �gmp_printf�( "%d %Zd\n", i, fact ); 6264 6265 } 6265 6266 } … … 6326 6327 \begin{cfa}[belowskip=0pt] 6327 6328 // implementation 6328 struct Rational { §\indexc{Rational}§6329 struct Rational {�\indexc{Rational}� 6329 6330 long int numerator, denominator; // invariant: denominator > 0 6330 6331 }; // Rational 6331 6332 6332 Rational rational(); §\C{// constructors}§6333 Rational rational(); �\C{// constructors}� 6333 6334 Rational rational( long int n ); 6334 6335 Rational rational( long int n, long int d ); … … 6336 6337 void ?{}( Rational * r, one_t ); 6337 6338 6338 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§6339 long int numerator( Rational r ); �\C{// numerator/denominator getter/setter}� 6339 6340 long int numerator( Rational r, long int n ); 6340 6341 long int denominator( Rational r ); 6341 6342 long int denominator( Rational r, long int d ); 6342 6343 6343 int ?==?( Rational l, Rational r ); §\C{// comparison}§6344 int ?==?( Rational l, Rational r ); �\C{// comparison}� 6344 6345 int ?!=?( Rational l, Rational r ); 6345 6346 int ?<?( Rational l, Rational r ); … … 6348 6349 int ?>=?( Rational l, Rational r ); 6349 6350 6350 Rational -?( Rational r ); §\C{// arithmetic}§6351 Rational -?( Rational r ); �\C{// arithmetic}� 6351 6352 Rational ?+?( Rational l, Rational r ); 6352 6353 Rational ?-?( Rational l, Rational r ); … … 6354 6355 Rational ?/?( Rational l, Rational r ); 6355 6356 6356 double widen( Rational r ); §\C{// conversion}§6357 double widen( Rational r ); �\C{// conversion}� 6357 6358 Rational narrow( double f, long int md ); 6358 6359
Note: See TracChangeset
for help on using the changeset viewer.