Changeset 67f2170
- Timestamp:
- Jul 7, 2017, 12:44:55 PM (7 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:
- 0322865c
- Parents:
- eaace25
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
reaace25 r67f2170 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 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Sun Jul 2 09:49:56201714 %% Update Count : 25 0313 %% Last Modified On : Fri Jul 7 10:36:39 2017 14 %% Update Count : 2547 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 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 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2453 1 � �2� �32452 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2453 1® ®2® ®3 2454 2454 \end{cfa} 2455 2455 & 2456 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2456 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2457 2457 1 2 3 2458 2458 \end{cfa} … … 2461 2461 The \CFA form has half the characters of the \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators. 2462 2462 Similar simplification occurs for \Index{tuple} I/O, which prints all tuple values separated by ``\lstinline[showspaces=true]@, @''. 2463 \begin{cfa} [mathescape=off,aboveskip=0pt,belowskip=0pt]2464 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 3, [ 4, 5] ];2465 sout | t1 | t2 | endl; �\C{// print tuples}�2466 \end{cfa} 2467 \begin{cfa}[ mathescape=off,showspaces=true,belowskip=0pt]2468 1 �, �2�, �3 3�, �4�, �52463 \begin{cfa} 2464 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 2465 sout | t1 | t2 | endl; §\C{// print tuples}§ 2466 \end{cfa} 2467 \begin{cfa}[showspaces=true,aboveskip=0pt] 2468 1®, ®2®, ®3 4®, ®5®, ®6 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 \\ 2486 2486 & 2487 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2487 \begin{cfa}[showspaces=true,aboveskip=0pt] 2488 2488 3 3 12 0 3 1 2 2489 2489 \end{cfa} … … 2503 2503 sout | 1 | 2 | 3 | endl; 2504 2504 \end{cfa} 2505 \begin{cfa}[ mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt]2505 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2506 2506 1 2 3 2507 2507 \end{cfa} … … 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} … … 2570 2570 \subsection{Manipulator} 2571 2571 2572 The following routines and \CC-style \Index{manipulator}s control implicit seperation.2572 The following \CC-style \Index{manipulator}s and routines control implicit seperation. 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).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;2575 Routines \Indexc{sepSet}\index{manipulator!sepSet@©sepSet©} and \Indexc{sep}\index{manipulator!sep@©sep©}/\Indexc{sepGet}\index{manipulator!sepGet@©sepGet©} set and get the separator string. 2576 The separator string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 2577 \begin{cfa}[mathescape=off,belowskip=0pt] 2578 sepSet( sout, ", $" ); §\C{// set separator from " " to ", \$"}§ 2579 sout | 1 | 2 | 3 | " \"" | ®sep® | "\"" | 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 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2587 sepSet( sout, " " ); �\C{// reset separator to " "}� 2588 sout | 1 | 2 | 3 | " \"" | �sepGet( sout )� | "\"" | endl; 2589 \end{cfa} 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). 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; 2600 \end{cfa} 2601 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt] 2602 1 2 3 4 �" "� 2603 \end{cfa} 2604 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2605 sepSetTuple( sout, ", " ); �\C{// reset tuple separator to ", "}� 2606 sout | t1 | t2 | " \"" | �sepGetTuple( sout )� | "\"" | endl; 2607 \end{cfa} 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. 2614 \begin{cfa}[mathescape=off,belowskip=0pt] 2615 sout | sepOn | 1 | 2 | 3 | sepOn | endl; �\C{// separator at start/end of line}� 2616 \end{cfa} 2617 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2618 � �1 2 3� � 2619 \end{cfa} 2620 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2621 sout | 1 | sepOff | 2 | 3 | endl; �\C{// locally turn off implicit separator}� 2622 \end{cfa} 2623 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2624 12 3 2625 \end{cfa} 2626 The tuple separator also responses to being turned on and off. 2627 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2628 sout | sepOn | t1 | sepOff | t2 | endl; �\C{// locally turn on/off implicit separation}� 2629 \end{cfa} 2630 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2631 , 1, 23, 4 2632 \end{cfa} 2633 Notice a tuple seperator starts the line because the next item is a tuple. 2634 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. 2637 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2638 sout | sepDisable | 1 | 2 | 3 | endl; �\C{// globally turn off implicit separation}� 2639 \end{cfa} 2640 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2586 \begin{cfa}[belowskip=0pt] 2587 sepSet( sout, " " ); §\C{// reset separator to " "}§ 2588 sout | 1 | 2 | 3 | " \"" | ®sepGet( sout )® | "\"" | endl; 2589 \end{cfa} 2590 \begin{cfa}[showspaces=true,aboveskip=0pt] 2591 1® ®2® ®3 ®" "® 2592 \end{cfa} 2593 ©sepGet© can be used to store a separator and then restore it: 2594 \begin{cfa}[belowskip=0pt] 2595 char store[®sepSize®]; §\C{// sepSize is the maximum separator size}§ 2596 strcpy( store, sepGet( sout ) ); 2597 sepSet( sout, "_" ); 2598 sout | 1 | 2 | 3 | endl; 2599 \end{cfa} 2600 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2601 1®_®2®_®3 2602 \end{cfa} 2603 \begin{cfa}[belowskip=0pt] 2604 sepSet( sout, store ); 2605 sout | 1 | 2 | 3 | endl; 2606 \end{cfa} 2607 \begin{cfa}[showspaces=true,aboveskip=0pt] 2608 1® ®2® ®3 2609 \end{cfa} 2610 2611 \item 2612 Routine \Indexc{sepSetTuple}\index{manipulator!sepSetTuple@©sepSetTuple©} and \Indexc{sepTuple}\index{manipulator!sepTuple@©sepTuple©}/\Indexc{sepGetTuple}\index{manipulator!sepGetTuple@©sepGetTuple©} get and set the tuple separator-string. 2613 The tuple separator-string can be at most 16 characters including the ©'\0'© string terminator (15 printable characters). 2614 \begin{cfa}[belowskip=0pt] 2615 sepSetTuple( sout, " " ); §\C{// set tuple separator from ", " to " "}§ 2616 sout | t1 | t2 | " \"" | ®sepTuple® | "\"" | endl; 2617 \end{cfa} 2618 \begin{cfa}[showspaces=true,aboveskip=0pt] 2619 1 2 3 4 5 6 ®" "® 2620 \end{cfa} 2621 \begin{cfa}[belowskip=0pt] 2622 sepSetTuple( sout, ", " ); §\C{// reset tuple separator to ", "}§ 2623 sout | t1 | t2 | " \"" | ®sepGetTuple( sout )® | "\"" | endl; 2624 \end{cfa} 2625 \begin{cfa}[showspaces=true,aboveskip=0pt] 2626 1, 2, 3 4, 5, 6 ®", "® 2627 \end{cfa} 2628 As for ©sepGet©, ©sepGetTuple© can be use to store a tuple separator and then restore it. 2629 2630 \item 2631 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. 2632 \begin{cfa}[belowskip=0pt] 2633 sout | sepDisable | 1 | 2 | 3 | endl; §\C{// globally turn off implicit separator}§ 2634 \end{cfa} 2635 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2641 2636 123 2642 2637 \end{cfa} 2643 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2644 sout | 1 | �sepOn� | 2 | 3 | endl; �\C{// locally turn on implicit separator}� 2645 \end{cfa} 2646 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2647 1� �23 2648 \end{cfa} 2649 \begin{cfa}[mathescape=off,aboveskip=0pt,belowskip=0pt] 2650 sout | sepEnable | 1 | 2 | 3 | endl; �\C{// globally turn on implicit separation}� 2638 \begin{cfa}[belowskip=0pt] 2639 sout | sepEnable | 1 | 2 | 3 | endl; §\C{// globally turn on implicit separator}§ 2651 2640 \end{cfa} 2652 2641 \begin{cfa}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] 2653 2642 1 2 3 2654 2643 \end{cfa} 2644 2645 \item 2646 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. 2647 \begin{cfa}[belowskip=0pt] 2648 sout | 1 | sepOff | 2 | 3 | endl; §\C{// locally turn off implicit separator}§ 2649 \end{cfa} 2650 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2651 12 3 2652 \end{cfa} 2653 \begin{cfa}[belowskip=0pt] 2654 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; §\C{// locally turn on implicit separator}§ 2655 \end{cfa} 2656 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2657 1 23 2658 \end{cfa} 2659 The tuple separator also responses to being turned on and off. 2660 \begin{cfa}[belowskip=0pt] 2661 sout | t1 | sepOff | t2 | endl; §\C{// locally turn on/off implicit separator}§ 2662 \end{cfa} 2663 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2664 1, 2, 34, 5, 6 2665 \end{cfa} 2666 ©sepOn© \emph{cannot} be used to start/end a line with a separator because separators do not appear at the start/end of a line; 2667 use ©sep© to accomplish this functionality. 2668 \begin{cfa}[belowskip=0pt] 2669 sout | sepOn | 1 | 2 | 3 | sepOn | endl ; §\C{// sepOn does nothing at start/end of line}§ 2670 \end{cfa} 2671 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2672 1 2 3 2673 \end{cfa} 2674 \begin{cfa}[belowskip=0pt] 2675 sout | sep | 1 | 2 | 3 | sep | endl ; §\C{// use sep to print separator at start/end of line}§ 2676 \end{cfa} 2677 \begin{cfa}[showspaces=true,aboveskip=0pt,belowskip=0pt] 2678 ® ®1 2 3® ® 2679 \end{cfa} 2655 2680 \end{enumerate} 2656 2681 2657 2682 \begin{comment} 2658 2683 #include <fstream> 2684 #include <string.h> // strcpy 2659 2685 2660 2686 int main( void ) { 2661 2687 int x = 1, y = 2, z = 3; 2662 2688 sout | x | y | z | endl; 2663 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 3, [ 4, 5] ];2689 [int, [ int, int ] ] t1 = [ 1, [ 2, 3 ] ], t2 = [ 4, [ 5, 6 ] ]; 2664 2690 sout | t1 | t2 | endl; // print tuples 2665 2691 sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl; … … 2667 2693 sout | '1' | '2' | '3' | endl; 2668 2694 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;2695 sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x =" | 4 | "x $" | 5 | "x £" | 6 | "x ¥" 2696 | 7 | "x ¡" | 8 | "x ¿" | 9 | "x «" | 10 | endl; 2671 2697 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;2698 | 7 | "¢ x" | 8 | "» x" | 9 | ") x" | 10 | "] x" | 11 | "} x" | endl; 2673 2699 sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x:" | 4 | ":x " | 5 | " x\t" | 6 | "\tx" | endl; 2674 2700 sout | "x ( " | 1 | " ) x" | 2 | " , x" | 3 | " :x: " | 4 | endl; 2675 2701 2676 2702 sepSet( sout, ", $" ); // set separator from " " to ", $" 2677 sout | 1 | 2 | 3 | " \"" | sep Get( sout )| "\"" | endl;2703 sout | 1 | 2 | 3 | " \"" | sep | "\"" | endl; 2678 2704 sepSet( sout, " " ); // reset separator to " " 2679 2705 sout | 1 | 2 | 3 | " \"" | sepGet( sout ) | "\"" | endl; 2680 2706 2681 sout | sepOn | 1 | 2 | 3 | sepOn | endl; // separator at start of line2682 s out | 1 | sepOff | 2 | 3 | endl; // locally turn off implicit separator2683 2684 sout | sepDisable | 1 | 2 | 3 | endl; // globally turn off implicit separation2685 s out | 1 | sepOn | 2 | 3 | endl; // locally turn on implicit separator2686 sout | sepEnable | 1 | 2 | 3 | endl; // globally turn on implicit separation2707 char store[sepSize]; 2708 strcpy( store, sepGet( sout ) ); 2709 sepSet( sout, "_" ); 2710 sout | 1 | 2 | 3 | endl; 2711 sepSet( sout, store ); 2712 sout | 1 | 2 | 3 | endl; 2687 2713 2688 2714 sepSetTuple( sout, " " ); // set tuple separator from ", " to " " 2689 sout | t1 | t2 | " \"" | sep GetTuple( sout )| "\"" | endl;2715 sout | t1 | t2 | " \"" | sepTuple | "\"" | endl; 2690 2716 sepSetTuple( sout, ", " ); // reset tuple separator to ", " 2691 2717 sout | t1 | t2 | " \"" | sepGetTuple( sout ) | "\"" | endl; 2692 2718 2693 sout | t1 | t2 | endl; // print tuple 2694 sout | sepOn | t1 | sepOff | t2 | endl; // locally turn on/off implicit separation 2719 sout | sepDisable | 1 | 2 | 3 | endl; // globally turn off implicit separator 2720 sout | sepEnable | 1 | 2 | 3 | endl; // globally turn on implicit separator 2721 2722 sout | 1 | sepOff | 2 | 3 | endl; // locally turn on implicit separator 2723 sout | sepDisable | 1 | sepOn | 2 | 3 | endl; // globally turn off implicit separator 2724 sout | sepEnable; 2725 sout | t1 | sepOff | t2 | endl; // locally turn on/off implicit separator 2726 2727 sout | sepOn | 1 | 2 | 3 | sepOn | endl ; // sepOn does nothing at start/end of line 2728 sout | sep | 1 | 2 | 3 | sep | endl ; // use sep to print separator at start/end of line 2695 2729 } 2696 2730 … … 2909 2943 In \CFA, as in C, all scalar types can be incremented and 2910 2944 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)�).2945 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 2946 2913 2947 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 2948 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�.2949 Defining special constants for a user-defined type is more efficient than defining a conversion to the type from ©_Bool©. 2916 2950 2917 2951 Why just 0 and 1? Why not other integers? No other integers have special status in C. … … 2996 3030 \begin{tabular}[t]{ll} 2997 3031 %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{?/?}\\3032 ©?[?]© & subscripting \impl{?[?]}\\ 3033 ©?()© & function call \impl{?()}\\ 3034 ©?++© & postfix increment \impl{?++}\\ 3035 ©?--© & postfix decrement \impl{?--}\\ 3036 ©++?© & prefix increment \impl{++?}\\ 3037 ©--?© & prefix decrement \impl{--?}\\ 3038 ©*?© & dereference \impl{*?}\\ 3039 ©+?© & unary plus \impl{+?}\\ 3040 ©-?© & arithmetic negation \impl{-?}\\ 3041 ©~?© & bitwise negation \impl{~?}\\ 3042 ©!?© & logical complement \impl{"!?}\\ 3043 ©?*?© & multiplication \impl{?*?}\\ 3044 ©?/?© & division \impl{?/?}\\ 3011 3045 \end{tabular}\hfil 3012 3046 \begin{tabular}[t]{ll} 3013 3047 %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{?&?}\\3048 ©?%?© & remainder \impl{?%?}\\ 3049 ©?+?© & addition \impl{?+?}\\ 3050 ©?-?© & subtraction \impl{?-?}\\ 3051 ©?<<?© & left shift \impl{?<<?}\\ 3052 ©?>>?© & right shift \impl{?>>?}\\ 3053 ©?<?© & less than \impl{?<?}\\ 3054 ©?<=?© & less than or equal \impl{?<=?}\\ 3055 ©?>=?© & greater than or equal \impl{?>=?}\\ 3056 ©?>?© & greater than \impl{?>?}\\ 3057 ©?==?© & equality \impl{?==?}\\ 3058 ©?!=?© & inequality \impl{?"!=?}\\ 3059 ©?&?© & bitwise AND \impl{?&?}\\ 3026 3060 \end{tabular}\hfil 3027 3061 \begin{tabular}[t]{ll} 3028 3062 %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{?"|=?}\\3063 ©?^?© & exclusive OR \impl{?^?}\\ 3064 ©?|?© & inclusive OR \impl{?"|?}\\ 3065 ©?=?© & simple assignment \impl{?=?}\\ 3066 ©?*=?© & multiplication assignment \impl{?*=?}\\ 3067 ©?/=?© & division assignment \impl{?/=?}\\ 3068 ©?%=?© & remainder assignment \impl{?%=?}\\ 3069 ©?+=?© & addition assignment \impl{?+=?}\\ 3070 ©?-=?© & subtraction assignment \impl{?-=?}\\ 3071 ©?<<=?© & left-shift assignment \impl{?<<=?}\\ 3072 ©?>>=?© & right-shift assignment \impl{?>>=?}\\ 3073 ©?&=?© & bitwise AND assignment \impl{?&=?}\\ 3074 ©?^=?© & exclusive OR assignment \impl{?^=?}\\ 3075 ©?|=?© & inclusive OR assignment \impl{?"|=?}\\ 3042 3076 \end{tabular} 3043 3077 \hfil … … 3048 3082 These identifiers are defined such that the question marks in the name identify the location of the operands. 3049 3083 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)�.3084 For example, ©a + b© becomes ©?+?(a, b)©. 3051 3085 3052 3086 In the example below, a new type, myComplex, is defined with an overloaded constructor, + operator, and string operator. … … 3122 3156 \end{itemize} 3123 3157 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.3158 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. 3159 \Indexc{gcc} provides ©typeof© to declare a secondary variable from a primary variable. 3126 3160 \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 3161 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.3162 Finally, ©auto© presents the programming problem of tracking down a type when the type is actually needed. 3129 3163 For example, given 3130 3164 \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 );3165 auto j = ®...® 3166 \end{cfa} 3167 and the need to write a routine to compute using ©j© 3168 \begin{cfa} 3169 void rtn( ®...® parm ); 3136 3170 rtn( j ); 3137 3171 \end{cfa} 3138 A programmer must work backwards to determine the type of �j�'s initialization expression, reconstructing the possibly long generic type-name.3172 A programmer must work backwards to determine the type of ©j©'s initialization expression, reconstructing the possibly long generic type-name. 3139 3173 In this situation, having the type name or a short alias is very useful. 3140 3174 … … 3144 3178 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 3179 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.3180 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 3181 Should a significant need arise, this feature can be revisited. 3148 3182 … … 3456 3490 3457 3491 coroutine Fibonacci { 3458 int fn; �\C{// used for communication}�3492 int fn; §\C{// used for communication}§ 3459 3493 }; 3460 3494 void ?{}( Fibonacci * this ) { … … 3462 3496 } 3463 3497 void main( Fibonacci * this ) { 3464 int fn1, fn2; �\C{// retained between resumes}�3465 this->fn = 0; �\C{// case 0}�3498 int fn1, fn2; §\C{// retained between resumes}§ 3499 this->fn = 0; §\C{// case 0}§ 3466 3500 fn1 = this->fn; 3467 suspend(); �\C{// return to last resume}�3468 3469 this->fn = 1; �\C{// case 1}�3501 suspend(); §\C{// return to last resume}§ 3502 3503 this->fn = 1; §\C{// case 1}§ 3470 3504 fn2 = fn1; 3471 3505 fn1 = this->fn; 3472 suspend(); �\C{// return to last resume}�3473 3474 for ( ;; ) { �\C{// general case}�3506 suspend(); §\C{// return to last resume}§ 3507 3508 for ( ;; ) { §\C{// general case}§ 3475 3509 this->fn = fn1 + fn2; 3476 3510 fn2 = fn1; 3477 3511 fn1 = this->fn; 3478 suspend(); �\C{// return to last resume}�3512 suspend(); §\C{// return to last resume}§ 3479 3513 } // for 3480 3514 } 3481 3515 int next( Fibonacci * this ) { 3482 resume( this ); �\C{// transfer to last suspend}�3516 resume( this ); §\C{// transfer to last suspend}§ 3483 3517 return this->fn; 3484 3518 } … … 3622 3656 #include <thread> 3623 3657 3624 thread First { s emaphore* lock; };3625 thread Second { s emaphore* lock; };3626 3627 void ?{}( First * this, s emaphore* lock ) { this->lock = lock; }3628 void ?{}( Second * this, s emaphore* lock ) { this->lock = lock; }3629 3630 void main( First* this) {3631 for (int i = 0; i < 10; i++) {3658 thread First { signal_once * lock; }; 3659 thread Second { signal_once * lock; }; 3660 3661 void ?{}( First * this, signal_once* lock ) { this->lock = lock; } 3662 void ?{}( Second * this, signal_once* lock ) { this->lock = lock; } 3663 3664 void main( First * this ) { 3665 for ( int i = 0; i < 10; i += 1 ) { 3632 3666 sout | "First : Suspend No." | i + 1 | endl; 3633 3667 yield(); 3634 3668 } 3635 V(this->lock);3636 } 3637 3638 void main( Second* this) {3639 P(this->lock);3640 for (int i = 0; i < 10; i++) {3669 signal( this->lock ); 3670 } 3671 3672 void main( Second * this ) { 3673 wait( this->lock ); 3674 for ( int i = 0; i < 10; i += 1 ) { 3641 3675 sout | "Second : Suspend No." | i + 1 | endl; 3642 3676 yield(); … … 3644 3678 } 3645 3679 3646 3647 int main(int argc, char* argv[]) { 3648 semaphore lock = { 0 }; 3680 int main( void ) { 3681 signal_once lock; 3649 3682 sout | "User main begin" | endl; 3650 3683 { … … 3860 3893 In \CFA, multiple definitions are not necessary. 3861 3894 Within a module, all of the module's global definitions are visible throughout the module. 3862 For example, the following code compiles, even though �isOdd�was not declared before being called:3895 For example, the following code compiles, even though ©isOdd© was not declared before being called: 3863 3896 \begin{cfa} 3864 3897 bool isEven(unsigned int x) { … … 5189 5222 5190 5223 C has a number of syntax ambiguities, which are resolved by taking the longest sequence of overlapping characters that constitute a token. 5191 For example, the program fragment �x+++++y� is parsed as \lstinline[showspaces=true]@x ++ ++ + y@ because operator tokens �++� and �+�overlap.5224 For example, the program fragment ©x+++++y© is parsed as \lstinline[showspaces=true]@x ++ ++ + y@ because operator tokens ©++© and ©+© overlap. 5192 5225 Unfortunately, the longest sequence violates a constraint on increment operators, even though the parse \lstinline[showspaces=true]@x ++ + ++ y@ might yield a correct expression. 5193 5226 Hence, C programmers are aware that spaces have to added to disambiguate certain syntactic cases. 5194 5227 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}�5228 In \CFA, there are ambiguous cases with dereference and operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be interpreted as: 5229 \begin{cfa} 5230 *?§\color{red}\textvisiblespace§*? §\C{// dereference operator, dereference operator}§ 5231 *§\color{red}\textvisiblespace§?*? §\C{// dereference, multiplication operator}§ 5199 5232 \end{cfa} 5200 5233 By default, the first interpretation is selected, which does not yield a meaningful parse. … … 5202 5235 Otherwise a space is needed between the unary operator and operator identifier to disambiguate this common case. 5203 5236 5204 A similar issue occurs with the dereference, �*?(...)�, and routine-call, �?()(...)�identifiers.5237 A similar issue occurs with the dereference, ©*?(...)©, and routine-call, ©?()(...)© identifiers. 5205 5238 The ambiguity occurs when the deference operator has no parameters: 5206 5239 \begin{cfa} 5207 *?() �\color{red}\textvisiblespace...�;5208 *?() �\color{red}\textvisiblespace...�(...) ;5240 *?()§\color{red}\textvisiblespace...§ ; 5241 *?()§\color{red}\textvisiblespace...§(...) ; 5209 5242 \end{cfa} 5210 5243 requiring arbitrary whitespace look-ahead for the routine-call parameter-list to disambiguate. 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.5244 However, the dereference operator \emph{must} have a parameter/argument to dereference ©*?(...)©. 5245 Hence, always interpreting the string ©*?()© as \lstinline[showspaces=true]@* ?()@ does not preclude any meaningful program. 5213 5246 5214 5247 The remaining cases are with the increment/decrement operators and conditional expression, \eg: 5215 5248 \begin{cfa} 5216 i++? �\color{red}\textvisiblespace...�(...);5217 i?++ �\color{red}\textvisiblespace...�(...);5249 i++?§\color{red}\textvisiblespace...§(...); 5250 i?++§\color{red}\textvisiblespace...§(...); 5218 5251 \end{cfa} 5219 5252 requiring arbitrary whitespace look-ahead for the operator parameter-list, even though that interpretation is an incorrect expression (juxtaposed identifiers). 5220 5253 Therefore, it is necessary to disambiguate these cases with a space: 5221 5254 \begin{cfa} 5222 i++ �\color{red}\textvisiblespace�? i : 0;5223 i? �\color{red}\textvisiblespace�++i : 0;5255 i++§\color{red}\textvisiblespace§? i : 0; 5256 i?§\color{red}\textvisiblespace§++i : 0; 5224 5257 \end{cfa} 5225 5258 … … 5231 5264 \begin{tabular}{llll} 5232 5265 \begin{tabular}{@{}l@{}} 5233 �_AT�\\5234 �catch�\\5235 �catchResume�\\5236 �choose�\\5237 �coroutine�\\5238 �disable�\\5266 ©_AT© \\ 5267 ©catch© \\ 5268 ©catchResume© \\ 5269 ©choose© \\ 5270 ©coroutine© \\ 5271 ©disable© \\ 5239 5272 \end{tabular} 5240 5273 & 5241 5274 \begin{tabular}{@{}l@{}} 5242 �dtype�\\5243 �enable�\\5244 �fallthrough�\\5245 �fallthru�\\5246 �finally�\\5247 �forall�\\5275 ©dtype© \\ 5276 ©enable© \\ 5277 ©fallthrough© \\ 5278 ©fallthru© \\ 5279 ©finally© \\ 5280 ©forall© \\ 5248 5281 \end{tabular} 5249 5282 & 5250 5283 \begin{tabular}{@{}l@{}} 5251 �ftype�\\5252 �lvalue�\\5253 �monitor�\\5254 �mutex�\\5255 �one_t�\\5256 �otype�\\5284 ©ftype© \\ 5285 ©lvalue© \\ 5286 ©monitor© \\ 5287 ©mutex© \\ 5288 ©one_t© \\ 5289 ©otype© \\ 5257 5290 \end{tabular} 5258 5291 & 5259 5292 \begin{tabular}{@{}l@{}} 5260 �throw�\\5261 �throwResume�\\5262 �trait�\\5263 �try�\\5264 �ttype�\\5265 �zero_t�\\5293 ©throw© \\ 5294 ©throwResume© \\ 5295 ©trait© \\ 5296 ©try© \\ 5297 ©ttype© \\ 5298 ©zero_t© \\ 5266 5299 \end{tabular} 5267 5300 \end{tabular} … … 5289 5322 \begin{description} 5290 5323 \item[Change:] drop K\&R C declarations \\ 5291 K\&R declarations allow an implicit base-type of �int�, if no type is specified, plus an alternate syntax for declaring parameters.5324 K\&R declarations allow an implicit base-type of ©int©, if no type is specified, plus an alternate syntax for declaring parameters. 5292 5325 \eg: 5293 5326 \begin{cfa} 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 );}�5327 x; §\C{// int x}§ 5328 *y; §\C{// int *y}§ 5329 f( p1, p2 ); §\C{// int f( int p1, int p2 );}§ 5330 g( p1, p2 ) int p1, p2; §\C{// int g( int p1, int p2 );}§ 5298 5331 \end{cfa} 5299 5332 \CFA supports K\&R routine definitions: 5300 5333 \begin{cfa} 5301 f( a, b, c ) �\C{// default int return}�5302 int a, b; char c �\C{// K\&R parameter declarations}�5334 f( a, b, c ) §\C{// default int return}§ 5335 int a, b; char c §\C{// K\&R parameter declarations}§ 5303 5336 { 5304 5337 ... … … 5315 5348 \item 5316 5349 \begin{description} 5317 \item[Change:] type of character literal �int� to �char�to allow more intuitive overloading:5350 \item[Change:] type of character literal ©int© to ©char© to allow more intuitive overloading: 5318 5351 \begin{cfa} 5319 5352 int rtn( int i ); 5320 5353 int rtn( char c ); 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.5354 rtn( 'x' ); §\C{// programmer expects 2nd rtn to be called}§ 5355 \end{cfa} 5356 \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first. 5357 In particular, output of ©char© variable now print a character rather than the decimal ASCII value of the character. 5325 5358 \begin{cfa} 5326 5359 sout | 'x' | " " | (int)'x' | endl; 5327 5360 x 120 5328 5361 \end{cfa} 5329 Having to cast �'x'� to �char�is non-intuitive.5362 Having to cast ©'x'© to ©char© is non-intuitive. 5330 5363 \item[Effect on original feature:] change to semantics of well-defined feature that depend on: 5331 5364 \begin{cfa} … … 5334 5367 no long work the same in \CFA programs. 5335 5368 \item[Difficulty of converting:] simple 5336 \item[How widely used:] programs that depend upon �sizeof( 'x' )� are rare and can be changed to �sizeof(char)�.5369 \item[How widely used:] programs that depend upon ©sizeof( 'x' )© are rare and can be changed to ©sizeof(char)©. 5337 5370 \end{description} 5338 5371 5339 5372 \item 5340 5373 \begin{description} 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�.5374 \item[Change:] make string literals ©const©: 5375 \begin{cfa} 5376 char * p = "abc"; §\C{// valid in C, deprecated in \CFA}§ 5377 char * q = expr ? "abc" : "de"; §\C{// valid in C, invalid in \CFA}§ 5378 \end{cfa} 5379 The type of a string literal is changed from ©[] char© to ©const [] char©. 5380 Similarly, the type of a wide string literal is changed from ©[] wchar_t© to ©const [] wchar_t©. 5348 5381 \item[Rationale:] This change is a safety issue: 5349 5382 \begin{cfa} 5350 5383 char * p = "abc"; 5351 p[0] = 'w'; �\C{// segment fault or change constant literal}�5384 p[0] = 'w'; §\C{// segment fault or change constant literal}§ 5352 5385 \end{cfa} 5353 5386 The same problem occurs when passing a string literal to a routine that changes its argument. 5354 5387 \item[Effect on original feature:] change to semantics of well-defined feature. 5355 \item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to �char *�.5388 \item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to ©char *©. 5356 5389 \item[How widely used:] programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are rare. 5357 5390 \end{description} … … 5361 5394 \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: 5362 5395 \begin{cfa} 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}�5396 int i; §\C{// forward definition}§ 5397 int *j = ®&i®; §\C{// forward reference, valid in C, invalid in \CFA}§ 5398 int i = 0; §\C{// definition}§ 5366 5399 \end{cfa} 5367 5400 is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. … … 5369 5402 \begin{cfa} 5370 5403 struct X { int i; struct X *next; }; 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}�5404 static struct X a; §\C{// forward definition}§ 5405 static struct X b = { 0, ®&a® }; §\C{// forward reference, valid in C, invalid in \CFA}§ 5406 static struct X a = { 1, &b }; §\C{// definition}§ 5374 5407 \end{cfa} 5375 5408 \item[Rationale:] avoids having different initialization rules for builtin types and user-defined types. … … 5381 5414 \item 5382 5415 \begin{description} 5383 \item[Change:] have �struct�introduce a scope for nested types:5384 \begin{cfa} 5385 enum �Colour�{ R, G, B, Y, C, M };5416 \item[Change:] have ©struct© introduce a scope for nested types: 5417 \begin{cfa} 5418 enum ®Colour® { R, G, B, Y, C, M }; 5386 5419 struct Person { 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)}�5420 enum ®Colour® { R, G, B }; §\C{// nested type}§ 5421 struct Face { §\C{// nested type}§ 5422 ®Colour® Eyes, Hair; §\C{// type defined outside (1 level)}§ 5390 5423 }; 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}�5424 ®.Colour® shirt; §\C{// type defined outside (top level)}§ 5425 ®Colour® pants; §\C{// type defined same level}§ 5426 Face looks[10]; §\C{// type defined same level}§ 5394 5427 }; 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}�5428 ®Colour® c = R; §\C{// type/enum defined same level}§ 5429 Person®.Colour® pc = Person®.®R; §\C{// type/enum defined inside}§ 5430 Person®.®Face pretty; §\C{// type defined inside}§ 5398 5431 \end{cfa} 5399 5432 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. 5400 5433 \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \Index*[C++]{\CC{}}. 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.5434 Nested types are not hoisted and can be referenced using the field selection operator ``©.©'', unlike the \CC scope-resolution operator ``©::©''. 5435 \item[Rationale:] ©struct© scope is crucial to \CFA as an information structuring and hiding mechanism. 5403 5436 \item[Effect on original feature:] change to semantics of well-defined feature. 5404 5437 \item[Difficulty of converting:] Semantic transformation. … … 5412 5445 \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: 5413 5446 \begin{cfa} 5414 struct Y; �\C{// struct Y and struct X are at the same scope}�5447 struct Y; §\C{// struct Y and struct X are at the same scope}§ 5415 5448 struct X { 5416 5449 struct Y { /* ... */ } y; … … 5425 5458 \begin{description} 5426 5459 \item[Change:] comma expression is disallowed as subscript 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.5460 \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. 5428 5461 \item[Effect on original feature:] change to semantics of well-defined feature. 5429 \item[Difficulty of converting:] semantic transformation of �x[i,j]� to �x[(i,j)]�5462 \item[Difficulty of converting:] semantic transformation of ©x[i,j]© to ©x[(i,j)]© 5430 5463 \item[How widely used:] seldom. 5431 5464 \end{description} … … 5498 5531 \end{tabular} 5499 5532 \end{quote2} 5500 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an �extern "C"�;5533 For the prescribed head-files, \CFA uses header interposition to wraps these includes in an ©extern "C"©; 5501 5534 hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}). 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.5535 All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling. 5536 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. 5504 5537 5505 5538 … … 5533 5566 & & \multicolumn{1}{c|}{fill} & resize & alignment & array \\ 5534 5567 \hline 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 \\5568 C & ©malloc© & no & no & no & no \\ 5569 & ©calloc© & yes (0 only) & no & no & yes \\ 5570 & ©realloc© & no/copy & yes & no & no \\ 5571 & ©memalign© & no & no & yes & no \\ 5572 & ©posix_memalign© & no & no & yes & no \\ 5573 C11 & ©aligned_alloc© & no & no & yes & no \\ 5574 \CFA & ©alloc© & no/copy/yes & no/yes & no & yes \\ 5575 & ©align_alloc© & no/yes & no & yes & yes \\ 5543 5576 \end{tabular} 5544 5577 \end{center} 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.5578 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. 5546 5579 5547 5580 \leavevmode … … 5549 5582 // C unsafe allocation 5550 5583 extern "C" { 5551 void * mall ac( 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 specification5584 void * malloc( size_t size );§\indexc{memset}§ 5585 void * calloc( size_t dim, size_t size );§\indexc{calloc}§ 5586 void * realloc( void * ptr, size_t size );§\indexc{realloc}§ 5587 void * memalign( size_t align, size_t size );§\indexc{memalign}§ 5588 int posix_memalign( void ** ptr, size_t align, size_t size );§\indexc{posix_memalign}§ 5589 } 5590 5591 // §\CFA§ safe equivalents, i.e., implicit size specification 5559 5592 forall( dtype T | sized(T) ) T * malloc( void ); 5560 5593 forall( dtype T | sized(T) ) T * calloc( size_t dim ); … … 5564 5597 forall( dtype T | sized(T) ) int posix_memalign( T ** ptr, size_t align ); 5565 5598 5566 // �\CFA�safe general allocation, fill, resize, array5567 forall( dtype T | sized(T) ) T * alloc( void ); �\indexc{alloc}�5599 // §\CFA§ safe general allocation, fill, resize, array 5600 forall( dtype T | sized(T) ) T * alloc( void );§\indexc{alloc}§ 5568 5601 forall( dtype T | sized(T) ) T * alloc( char fill ); 5569 5602 forall( dtype T | sized(T) ) T * alloc( size_t dim ); … … 5572 5605 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill ); 5573 5606 5574 // �\CFA�safe general allocation, align, fill, array5607 // §\CFA§ safe general allocation, align, fill, array 5575 5608 forall( dtype T | sized(T) ) T * align_alloc( size_t align ); 5576 5609 forall( dtype T | sized(T) ) T * align_alloc( size_t align, char fill ); … … 5584 5617 } 5585 5618 5586 // �\CFA�safe initialization/copy, i.e., implicit size specification5587 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 array5619 // §\CFA§ safe initialization/copy, i.e., implicit size specification 5620 forall( dtype T | sized(T) ) T * memset( T * dest, char c );§\indexc{memset}§ 5621 forall( dtype T | sized(T) ) T * memcpy( T * dest, const T * src );§\indexc{memcpy}§ 5622 5623 // §\CFA§ safe initialization/copy array 5591 5624 forall( dtype T | sized(T) ) T * memset( T dest[], size_t dim, char c ); 5592 5625 forall( dtype T | sized(T) ) T * memcpy( T dest[], const T src[], size_t dim ); 5593 5626 5594 // �\CFA�allocation/deallocation and constructor/destructor5595 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}�5627 // §\CFA§ allocation/deallocation and constructor/destructor 5628 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * new( Params p );§\indexc{new}§ 5629 forall( dtype T | { void ^?{}( T * ); } ) void delete( T * ptr );§\indexc{delete}§ 5597 5630 forall( dtype T, ttype Params | { void ^?{}( T * ); void delete( Params ); } ) 5598 5631 void delete( T * ptr, Params rest ); 5599 5632 5600 // �\CFA�allocation/deallocation and constructor/destructor, array5601 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}�5633 // §\CFA§ allocation/deallocation and constructor/destructor, array 5634 forall( dtype T | sized(T), ttype Params | { void ?{}( T *, Params ); } ) T * anew( size_t dim, Params p );§\indexc{anew}§ 5635 forall( dtype T | sized(T) | { void ^?{}( T * ); } ) void adelete( size_t dim, T arr[] );§\indexc{adelete}§ 5603 5636 forall( dtype T | sized(T) | { void ^?{}( T * ); }, ttype Params | { void adelete( Params ); } ) 5604 5637 void adelete( size_t dim, T arr[], Params rest ); … … 5610 5643 \leavevmode 5611 5644 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5612 int ato( const char * ptr ); �\indexc{ato}�5645 int ato( const char * ptr );§\indexc{ato}§ 5613 5646 unsigned int ato( const char * ptr ); 5614 5647 long int ato( const char * ptr ); … … 5642 5675 \leavevmode 5643 5676 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 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}�5677 forall( otype T | { int ?<?( T, T ); } ) §\C{// location}§ 5678 T * bsearch( T key, const T * arr, size_t dim );§\indexc{bsearch}§ 5679 5680 forall( otype T | { int ?<?( T, T ); } ) §\C{// position}§ 5648 5681 unsigned int bsearch( T key, const T * arr, size_t dim ); 5649 5682 5650 5683 forall( otype T | { int ?<?( T, T ); } ) 5651 void qsort( const T * arr, size_t dim ); �\indexc{qsort}�5684 void qsort( const T * arr, size_t dim );§\indexc{qsort}§ 5652 5685 \end{cfa} 5653 5686 … … 5657 5690 \leavevmode 5658 5691 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5659 unsigned char abs( signed char ); �\indexc{abs}�5692 unsigned char abs( signed char );§\indexc{abs}§ 5660 5693 int abs( int ); 5661 5694 unsigned long int abs( long int ); … … 5676 5709 \leavevmode 5677 5710 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5678 void rand48seed( long int s ); �\indexc{rand48seed}�5679 char rand48(); �\indexc{rand48}�5711 void rand48seed( long int s );§\indexc{rand48seed}§ 5712 char rand48();§\indexc{rand48}§ 5680 5713 int rand48(); 5681 5714 unsigned int rand48(); … … 5694 5727 \leavevmode 5695 5728 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 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}�5729 forall( otype T | { int ?<?( T, T ); } ) T min( T t1, T t2 );§\indexc{min}§ 5730 forall( otype T | { int ?>?( T, T ); } ) T max( T t1, T t2 );§\indexc{max}§ 5731 forall( otype T | { T min( T, T ); T max( T, T ); } ) T clamp( T value, T min_val, T max_val );§\indexc{clamp}§ 5732 forall( otype T ) void swap( T * t1, T * t2 );§\indexc{swap}§ 5700 5733 \end{cfa} 5701 5734 … … 5711 5744 \leavevmode 5712 5745 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5713 float ?%?( float, float ); �\indexc{fmod}�5746 float ?%?( float, float );§\indexc{fmod}§ 5714 5747 float fmod( float, float ); 5715 5748 double ?%?( double, double ); … … 5718 5751 long double fmod( long double, long double ); 5719 5752 5720 float remainder( float, float ); �\indexc{remainder}�5753 float remainder( float, float );§\indexc{remainder}§ 5721 5754 double remainder( double, double ); 5722 5755 long double remainder( long double, long double ); 5723 5756 5724 [ int, float ] remquo( float, float ); �\indexc{remquo}�5757 [ int, float ] remquo( float, float );§\indexc{remquo}§ 5725 5758 float remquo( float, float, int * ); 5726 5759 [ int, double ] remquo( double, double ); … … 5730 5763 5731 5764 [ int, float ] div( float, float ); // alternative name for remquo 5732 float div( float, float, int * ); �\indexc{div}�5765 float div( float, float, int * );§\indexc{div}§ 5733 5766 [ int, double ] div( double, double ); 5734 5767 double div( double, double, int * ); … … 5736 5769 long double div( long double, long double, int * ); 5737 5770 5738 float fma( float, float, float ); �\indexc{fma}�5771 float fma( float, float, float );§\indexc{fma}§ 5739 5772 double fma( double, double, double ); 5740 5773 long double fma( long double, long double, long double ); 5741 5774 5742 float fdim( float, float ); �\indexc{fdim}�5775 float fdim( float, float );§\indexc{fdim}§ 5743 5776 double fdim( double, double ); 5744 5777 long double fdim( long double, long double ); 5745 5778 5746 float nan( const char * ); �\indexc{nan}�5779 float nan( const char * );§\indexc{nan}§ 5747 5780 double nan( const char * ); 5748 5781 long double nan( const char * ); … … 5754 5787 \leavevmode 5755 5788 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5756 float exp( float ); �\indexc{exp}�5789 float exp( float );§\indexc{exp}§ 5757 5790 double exp( double ); 5758 5791 long double exp( long double ); … … 5761 5794 long double _Complex exp( long double _Complex ); 5762 5795 5763 float exp2( float ); �\indexc{exp2}�5796 float exp2( float );§\indexc{exp2}§ 5764 5797 double exp2( double ); 5765 5798 long double exp2( long double ); … … 5768 5801 long double _Complex exp2( long double _Complex ); 5769 5802 5770 float expm1( float ); �\indexc{expm1}�5803 float expm1( float );§\indexc{expm1}§ 5771 5804 double expm1( double ); 5772 5805 long double expm1( long double ); 5773 5806 5774 float log( float ); �\indexc{log}�5807 float log( float );§\indexc{log}§ 5775 5808 double log( double ); 5776 5809 long double log( long double ); … … 5779 5812 long double _Complex log( long double _Complex ); 5780 5813 5781 float log2( float ); �\indexc{log2}�5814 float log2( float );§\indexc{log2}§ 5782 5815 double log2( double ); 5783 5816 long double log2( long double ); … … 5786 5819 long double _Complex log2( long double _Complex ); 5787 5820 5788 float log10( float ); �\indexc{log10}�5821 float log10( float );§\indexc{log10}§ 5789 5822 double log10( double ); 5790 5823 long double log10( long double ); … … 5793 5826 long double _Complex log10( long double _Complex ); 5794 5827 5795 float log1p( float ); �\indexc{log1p}�5828 float log1p( float );§\indexc{log1p}§ 5796 5829 double log1p( double ); 5797 5830 long double log1p( long double ); 5798 5831 5799 int ilogb( float ); �\indexc{ilogb}�5832 int ilogb( float );§\indexc{ilogb}§ 5800 5833 int ilogb( double ); 5801 5834 int ilogb( long double ); 5802 5835 5803 float logb( float ); �\indexc{logb}�5836 float logb( float );§\indexc{logb}§ 5804 5837 double logb( double ); 5805 5838 long double logb( long double ); … … 5811 5844 \leavevmode 5812 5845 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5813 float sqrt( float ); �\indexc{sqrt}�5846 float sqrt( float );§\indexc{sqrt}§ 5814 5847 double sqrt( double ); 5815 5848 long double sqrt( long double ); … … 5818 5851 long double _Complex sqrt( long double _Complex ); 5819 5852 5820 float cbrt( float ); �\indexc{cbrt}�5853 float cbrt( float );§\indexc{cbrt}§ 5821 5854 double cbrt( double ); 5822 5855 long double cbrt( long double ); 5823 5856 5824 float hypot( float, float ); �\indexc{hypot}�5857 float hypot( float, float );§\indexc{hypot}§ 5825 5858 double hypot( double, double ); 5826 5859 long double hypot( long double, long double ); 5827 5860 5828 float pow( float, float ); �\indexc{pow}�5861 float pow( float, float );§\indexc{pow}§ 5829 5862 double pow( double, double ); 5830 5863 long double pow( long double, long double ); … … 5839 5872 \leavevmode 5840 5873 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5841 float sin( float ); �\indexc{sin}�5874 float sin( float );§\indexc{sin}§ 5842 5875 double sin( double ); 5843 5876 long double sin( long double ); … … 5846 5879 long double _Complex sin( long double _Complex ); 5847 5880 5848 float cos( float ); �\indexc{cos}�5881 float cos( float );§\indexc{cos}§ 5849 5882 double cos( double ); 5850 5883 long double cos( long double ); … … 5853 5886 long double _Complex cos( long double _Complex ); 5854 5887 5855 float tan( float ); �\indexc{tan}�5888 float tan( float );§\indexc{tan}§ 5856 5889 double tan( double ); 5857 5890 long double tan( long double ); … … 5860 5893 long double _Complex tan( long double _Complex ); 5861 5894 5862 float asin( float ); �\indexc{asin}�5895 float asin( float );§\indexc{asin}§ 5863 5896 double asin( double ); 5864 5897 long double asin( long double ); … … 5867 5900 long double _Complex asin( long double _Complex ); 5868 5901 5869 float acos( float ); �\indexc{acos}�5902 float acos( float );§\indexc{acos}§ 5870 5903 double acos( double ); 5871 5904 long double acos( long double ); … … 5874 5907 long double _Complex acos( long double _Complex ); 5875 5908 5876 float atan( float ); �\indexc{atan}�5909 float atan( float );§\indexc{atan}§ 5877 5910 double atan( double ); 5878 5911 long double atan( long double ); … … 5881 5914 long double _Complex atan( long double _Complex ); 5882 5915 5883 float atan2( float, float ); �\indexc{atan2}�5916 float atan2( float, float );§\indexc{atan2}§ 5884 5917 double atan2( double, double ); 5885 5918 long double atan2( long double, long double ); 5886 5919 5887 5920 float atan( float, float ); // alternative name for atan2 5888 double atan( double, double ); �\indexc{atan}�5921 double atan( double, double );§\indexc{atan}§ 5889 5922 long double atan( long double, long double ); 5890 5923 \end{cfa} … … 5895 5928 \leavevmode 5896 5929 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5897 float sinh( float ); �\indexc{sinh}�5930 float sinh( float );§\indexc{sinh}§ 5898 5931 double sinh( double ); 5899 5932 long double sinh( long double ); … … 5902 5935 long double _Complex sinh( long double _Complex ); 5903 5936 5904 float cosh( float ); �\indexc{cosh}�5937 float cosh( float );§\indexc{cosh}§ 5905 5938 double cosh( double ); 5906 5939 long double cosh( long double ); … … 5909 5942 long double _Complex cosh( long double _Complex ); 5910 5943 5911 float tanh( float ); �\indexc{tanh}�5944 float tanh( float );§\indexc{tanh}§ 5912 5945 double tanh( double ); 5913 5946 long double tanh( long double ); … … 5916 5949 long double _Complex tanh( long double _Complex ); 5917 5950 5918 float asinh( float ); �\indexc{asinh}�5951 float asinh( float );§\indexc{asinh}§ 5919 5952 double asinh( double ); 5920 5953 long double asinh( long double ); … … 5923 5956 long double _Complex asinh( long double _Complex ); 5924 5957 5925 float acosh( float ); �\indexc{acosh}�5958 float acosh( float );§\indexc{acosh}§ 5926 5959 double acosh( double ); 5927 5960 long double acosh( long double ); … … 5930 5963 long double _Complex acosh( long double _Complex ); 5931 5964 5932 float atanh( float ); �\indexc{atanh}�5965 float atanh( float );§\indexc{atanh}§ 5933 5966 double atanh( double ); 5934 5967 long double atanh( long double ); … … 5943 5976 \leavevmode 5944 5977 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5945 float erf( float ); �\indexc{erf}�5978 float erf( float );§\indexc{erf}§ 5946 5979 double erf( double ); 5947 5980 long double erf( long double ); … … 5950 5983 long double _Complex erf( long double _Complex ); 5951 5984 5952 float erfc( float ); �\indexc{erfc}�5985 float erfc( float );§\indexc{erfc}§ 5953 5986 double erfc( double ); 5954 5987 long double erfc( long double ); … … 5957 5990 long double _Complex erfc( long double _Complex ); 5958 5991 5959 float lgamma( float ); �\indexc{lgamma}�5992 float lgamma( float );§\indexc{lgamma}§ 5960 5993 double lgamma( double ); 5961 5994 long double lgamma( long double ); … … 5964 5997 long double lgamma( long double, int * ); 5965 5998 5966 float tgamma( float ); �\indexc{tgamma}�5999 float tgamma( float );§\indexc{tgamma}§ 5967 6000 double tgamma( double ); 5968 6001 long double tgamma( long double ); … … 5974 6007 \leavevmode 5975 6008 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 5976 float floor( float ); �\indexc{floor}�6009 float floor( float );§\indexc{floor}§ 5977 6010 double floor( double ); 5978 6011 long double floor( long double ); 5979 6012 5980 float ceil( float ); �\indexc{ceil}�6013 float ceil( float );§\indexc{ceil}§ 5981 6014 double ceil( double ); 5982 6015 long double ceil( long double ); 5983 6016 5984 float trunc( float ); �\indexc{trunc}�6017 float trunc( float );§\indexc{trunc}§ 5985 6018 double trunc( double ); 5986 6019 long double trunc( long double ); 5987 6020 5988 float rint( float ); �\indexc{rint}�6021 float rint( float );§\indexc{rint}§ 5989 6022 long double rint( long double ); 5990 6023 long int rint( float ); … … 5995 6028 long long int rint( long double ); 5996 6029 5997 long int lrint( float ); �\indexc{lrint}�6030 long int lrint( float );§\indexc{lrint}§ 5998 6031 long int lrint( double ); 5999 6032 long int lrint( long double ); … … 6002 6035 long long int llrint( long double ); 6003 6036 6004 float nearbyint( float ); �\indexc{nearbyint}�6037 float nearbyint( float );§\indexc{nearbyint}§ 6005 6038 double nearbyint( double ); 6006 6039 long double nearbyint( long double ); 6007 6040 6008 float round( float ); �\indexc{round}�6041 float round( float );§\indexc{round}§ 6009 6042 long double round( long double ); 6010 6043 long int round( float ); … … 6015 6048 long long int round( long double ); 6016 6049 6017 long int lround( float ); �\indexc{lround}�6050 long int lround( float );§\indexc{lround}§ 6018 6051 long int lround( double ); 6019 6052 long int lround( long double ); … … 6028 6061 \leavevmode 6029 6062 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6030 float copysign( float, float ); �\indexc{copysign}�6063 float copysign( float, float );§\indexc{copysign}§ 6031 6064 double copysign( double, double ); 6032 6065 long double copysign( long double, long double ); 6033 6066 6034 float frexp( float, int * ); �\indexc{frexp}�6067 float frexp( float, int * );§\indexc{frexp}§ 6035 6068 double frexp( double, int * ); 6036 6069 long double frexp( long double, int * ); 6037 6070 6038 float ldexp( float, int ); �\indexc{ldexp}�6071 float ldexp( float, int );§\indexc{ldexp}§ 6039 6072 double ldexp( double, int ); 6040 6073 long double ldexp( long double, int ); 6041 6074 6042 [ float, float ] modf( float ); �\indexc{modf}�6075 [ float, float ] modf( float );§\indexc{modf}§ 6043 6076 float modf( float, float * ); 6044 6077 [ double, double ] modf( double ); … … 6047 6080 long double modf( long double, long double * ); 6048 6081 6049 float nextafter( float, float ); �\indexc{nextafter}�6082 float nextafter( float, float );§\indexc{nextafter}§ 6050 6083 double nextafter( double, double ); 6051 6084 long double nextafter( long double, long double ); 6052 6085 6053 float nexttoward( float, long double ); �\indexc{nexttoward}�6086 float nexttoward( float, long double );§\indexc{nexttoward}§ 6054 6087 double nexttoward( double, long double ); 6055 6088 long double nexttoward( long double, long double ); 6056 6089 6057 float scalbn( float, int ); �\indexc{scalbn}�6090 float scalbn( float, int );§\indexc{scalbn}§ 6058 6091 double scalbn( double, int ); 6059 6092 long double scalbn( long double, int ); 6060 6093 6061 float scalbln( float, long int ); �\indexc{scalbln}�6094 float scalbln( float, long int );§\indexc{scalbln}§ 6062 6095 double scalbln( double, long int ); 6063 6096 long double scalbln( long double, long int ); … … 6073 6106 6074 6107 \begin{cfa} 6075 void ?{}( Int * this ); �\C{// constructor}�6108 void ?{}( Int * this ); §\C{// constructor}§ 6076 6109 void ?{}( Int * this, Int init ); 6077 6110 void ?{}( Int * this, zero_t ); … … 6082 6115 void ^?{}( Int * this ); 6083 6116 6084 Int ?=?( Int * lhs, Int rhs ); �\C{// assignment}�6117 Int ?=?( Int * lhs, Int rhs ); §\C{// assignment}§ 6085 6118 Int ?=?( Int * lhs, long int rhs ); 6086 6119 Int ?=?( Int * lhs, unsigned long int rhs ); … … 6099 6132 unsigned long int narrow( Int val ); 6100 6133 6101 int ?==?( Int oper1, Int oper2 ); �\C{// comparison}�6134 int ?==?( Int oper1, Int oper2 ); §\C{// comparison}§ 6102 6135 int ?==?( Int oper1, long int oper2 ); 6103 6136 int ?==?( long int oper2, Int oper1 ); … … 6135 6168 int ?>=?( unsigned long int oper1, Int oper2 ); 6136 6169 6137 Int +?( Int oper ); �\C{// arithmetic}�6170 Int +?( Int oper ); §\C{// arithmetic}§ 6138 6171 Int -?( Int oper ); 6139 6172 Int ~?( Int oper ); … … 6217 6250 Int ?>>=?( Int * lhs, mp_bitcnt_t shift ); 6218 6251 6219 Int abs( Int oper ); �\C{// number functions}�6252 Int abs( Int oper ); §\C{// number functions}§ 6220 6253 Int fact( unsigned long int N ); 6221 6254 Int gcd( Int oper1, Int oper2 ); … … 6229 6262 Int sqrt( Int oper ); 6230 6263 6231 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); �\C{// I/O}�6264 forall( dtype istype | istream( istype ) ) istype * ?|?( istype * is, Int * mp ); §\C{// I/O}§ 6232 6265 forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype * os, Int mp ); 6233 6266 \end{cfa} … … 6240 6273 \hline 6241 6274 \begin{cfa} 6242 #include <gmp> �\indexc{gmp}�6275 #include <gmp>§\indexc{gmp}§ 6243 6276 int main( void ) { 6244 6277 sout | "Factorial Numbers" | endl; … … 6254 6287 & 6255 6288 \begin{cfa} 6256 #include <gmp.h> �\indexc{gmp.h}�6289 #include <gmp.h>§\indexc{gmp.h}§ 6257 6290 int main( void ) { 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 );6291 ®gmp_printf®( "Factorial Numbers\n" ); 6292 ®mpz_t® fact; 6293 ®mpz_init_set_ui®( fact, 1 ); 6294 ®gmp_printf®( "%d %Zd\n", 0, fact ); 6262 6295 for ( unsigned int i = 1; i <= 40; i += 1 ) { 6263 �mpz_mul_ui�( fact, fact, i );6264 �gmp_printf�( "%d %Zd\n", i, fact );6296 ®mpz_mul_ui®( fact, fact, i ); 6297 ®gmp_printf®( "%d %Zd\n", i, fact ); 6265 6298 } 6266 6299 } … … 6327 6360 \begin{cfa}[belowskip=0pt] 6328 6361 // implementation 6329 struct Rational { �\indexc{Rational}�6362 struct Rational {§\indexc{Rational}§ 6330 6363 long int numerator, denominator; // invariant: denominator > 0 6331 6364 }; // Rational 6332 6365 6333 Rational rational(); �\C{// constructors}�6366 Rational rational(); §\C{// constructors}§ 6334 6367 Rational rational( long int n ); 6335 6368 Rational rational( long int n, long int d ); … … 6337 6370 void ?{}( Rational * r, one_t ); 6338 6371 6339 long int numerator( Rational r ); �\C{// numerator/denominator getter/setter}�6372 long int numerator( Rational r ); §\C{// numerator/denominator getter/setter}§ 6340 6373 long int numerator( Rational r, long int n ); 6341 6374 long int denominator( Rational r ); 6342 6375 long int denominator( Rational r, long int d ); 6343 6376 6344 int ?==?( Rational l, Rational r ); �\C{// comparison}�6377 int ?==?( Rational l, Rational r ); §\C{// comparison}§ 6345 6378 int ?!=?( Rational l, Rational r ); 6346 6379 int ?<?( Rational l, Rational r ); … … 6349 6382 int ?>=?( Rational l, Rational r ); 6350 6383 6351 Rational -?( Rational r ); �\C{// arithmetic}�6384 Rational -?( Rational r ); §\C{// arithmetic}§ 6352 6385 Rational ?+?( Rational l, Rational r ); 6353 6386 Rational ?-?( Rational l, Rational r ); … … 6355 6388 Rational ?/?( Rational l, Rational r ); 6356 6389 6357 double widen( Rational r ); �\C{// conversion}�6390 double widen( Rational r ); §\C{// conversion}§ 6358 6391 Rational narrow( double f, long int md ); 6359 6392
Note: See TracChangeset
for help on using the changeset viewer.