Changes in doc/user/user.tex [a74503ff:48b9b36]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/user/user.tex
ra74503ff r48b9b36 11 11 %% Created On : Wed Apr 6 14:53:29 2016 12 12 %% Last Modified By : Peter A. Buhr 13 %% Last Modified On : Mon Jul 9 10:49:52201814 %% Update Count : 33 6113 %% Last Modified On : Sun May 6 10:33:53 2018 14 %% Update Count : 3319 15 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 16 … … 146 146 \CFA adds many modern programming-language features that directly lead to increased \emph{\Index{safety}} and \emph{\Index{productivity}}, while maintaining interoperability with existing C programs and achieving similar performance. 147 147 Like C, \CFA is a statically typed, procedural (non-\Index{object-oriented}) language with a low-overhead runtime, meaning there is no global \Index{garbage-collection}, but \Index{regional garbage-collection}\index{garbage-collection!regional} is possible. 148 The primary new features include p olymorphic routines and types, exceptions, concurrency, and modules.148 The primary new features include parametric-polymorphic routines and types, exceptions, concurrency, and modules. 149 149 150 150 One of the main design philosophies of \CFA is to ``\Index{describe not prescribe}'', which means \CFA tries to provide a pathway from low-level C programming to high-level \CFA programming, but it does not force programmers to ``do the right thing''. … … 155 155 As well, new programs can be written in \CFA using a combination of C and \CFA features. 156 156 157 \Index*[C++]{\CC{}} ~\cite{c++:v1}had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C.158 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that cannot be updated, and active divergence of the language model from C, requiringsignificant effort and training to incrementally add \CC to a C-based project.157 \Index*[C++]{\CC{}} had a similar goal 30 years ago, allowing object-oriented programming to be incrementally added to C. 158 However, \CC currently has the disadvantages of a strong object-oriented bias, multiple legacy design-choices that cannot be updated, and active divergence of the language model from C, all of which requires significant effort and training to incrementally add \CC to a C-based project. 159 159 In contrast, \CFA has 30 years of hindsight and a clean starting point. 160 160 161 161 Like \Index*[C++]{\CC{}}, there may be both an old and new ways to achieve the same effect. 162 For example, the following programs compare the C, \CFA, and \CC I/O mechanisms, where the programs output the same result.163 \begin{c enter}162 For example, the following programs compare the \CFA, C, and \CC I/O mechanisms, where the programs output the same result. 163 \begin{cquote} 164 164 \begin{tabular}{@{}l@{\hspace{1.5em}}l@{\hspace{1.5em}}l@{}} 165 165 \multicolumn{1}{c@{\hspace{1.5em}}}{\textbf{C}} & \multicolumn{1}{c}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ … … 191 191 \end{cfa} 192 192 \end{tabular} 193 \end{c enter}193 \end{cquote} 194 194 While the \CFA I/O looks similar to the \Index*[C++]{\CC{}} output style, there are important differences, such as automatic spacing between variables as in \Index*{Python} (see~\VRef{s:IOLibrary}). 195 195 … … 197 197 198 198 This document is a programmer reference-manual for the \CFA programming language. 199 The manual covers the core features of the language and runtime-system, with simple examples illustrating syntax and semantics of features.199 The manual covers the core features of the language and runtime-system, with simple examples illustrating syntax and semantics of each feature. 200 200 The manual does not teach programming, \ie how to combine the new constructs to build complex programs. 201 The reader musthave an intermediate knowledge of control flow, data structures, and concurrency issues to understand the ideas presented, as well as some experience programming in C/\CC.201 A reader should already have an intermediate knowledge of control flow, data structures, and concurrency issues to understand the ideas presented, as well as some experience programming in C/\CC. 202 202 Implementers should refer to the \CFA Programming Language Specification for details about the language syntax and semantics. 203 203 Changes to the syntax and additional features are expected to be included in later revisions. … … 206 206 \section{Why fix C?} 207 207 208 The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from hobby projects to commercial operating-systems.208 The C programming language is a foundational technology for modern computing with millions of lines of code implementing everything from commercial operating-systems (especially UNIX systems) to hobby projects. 209 209 This installation base and the programmers producing it represent a massive software-engineering investment spanning decades and likely to continue for decades more. 210 210 Even with all its problems, C continues to be popular because it allows writing software at virtually any level in a computer system without restriction. 211 211 For system programming, where direct access to hardware, storage management, and real-time issues are a requirement, C is usually the only language of choice. 212 The TIOBE index~\cite{TIOBE} for July 2018 ranks the top 5 most \emph{popular} programming languages as: \Index*{Java} 16\%, C 14\%, \Index*[C++]{\CC{}} 7.5\%, Python 6\%, Visual Basic 4\% = 47.5\%, where the next 50 languages are less than 4\% each,with a long tail.213 The top 3 rankings over the past 30 years are:212 The TIOBE index~\cite{TIOBE} for March 2016 showed the following programming-language popularity: \Index*{Java} 20.5\%, C 14.5\%, \Index*[C++]{\CC{}} 6.7\%, \Csharp 4.3\%, \Index*{Python} 4.3\%, where the next 50 languages are less than 3\% each with a long tail. 213 As well, for 30 years, C has been the number 1 and 2 most popular programming language: 214 214 \begin{center} 215 \setlength{\tabcolsep}{10pt} 216 \begin{tabular}{@{}rccccccc@{}} 217 & 2018 & 2013 & 2008 & 2003 & 1998 & 1993 & 1988 \\ \hline 218 Java & 1 & 2 & 1 & 1 & 16 & - & - \\ 219 \R{C} & \R{2} & \R{1} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} \\ 220 \CC & 3 & 4 & 3 & 3 & 2 & 2 & 5 \\ 215 \setlength{\tabcolsep}{1.5ex} 216 \begin{tabular}{@{}r|c|c|c|c|c|c|c@{}} 217 Ranking & 2016 & 2011 & 2006 & 2001 & 1996 & 1991 & 1986 \\ 218 \hline 219 Java & 1 & 1 & 1 & 3 & 29 & - & - \\ 220 \hline 221 \R{C} & \R{2} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} & \R{1} \\ 222 \hline 223 \CC & 3 & 3 & 3 & 2 & 2 & 2 & 7 \\ 221 224 \end{tabular} 222 225 \end{center} 223 226 Hence, C is still an extremely important programming language, with double the usage of \Index*[C++]{\CC{}}; in many cases, \CC is often used solely as a better C. 224 227 Love it or hate it, C has been an important and influential part of computer science for 40 years and its appeal is not diminishing. 225 Nevertheless, C has many problems and omissions that make it an unacceptable programming language for modern needs.228 Unfortunately, C has many problems and omissions that make it an unacceptable programming language for modern needs. 226 229 227 230 As stated, the goal of the \CFA project is to engineer modern language-features into C in an evolutionary rather than revolutionary way. … … 233 236 These languages have different syntax and semantics from C, do not interoperate directly with C, and are not systems languages because of restrictive memory-management or garbage collection. 234 237 As a result, there is a significant learning curve to move to these languages, and C legacy-code must be rewritten. 235 These costs can be prohibitive for many companies with a large software-base in C/\CC, and a significant number of programmers require retraining in the new programming language. 236 237 The result of this project is a language that is largely backwards compatible with \Index*[C11]{\Celeven{}}~\cite{C11}, but fixes many of the well known C problems while adding modern language-features. 238 To achieve these goals required a significant engineering exercise, where we had to ``think inside the existing C box''. 239 Without these significant extension to C, it is unable to cope with the needs of modern programming problems and programmers; 238 These costs can be prohibitive for many companies with a large software-base in C/\CC, and a significant number of programmers require retraining to the new programming language. 239 240 The result of this project is a language that is largely backwards compatible with \Index*[C11]{\Celeven{}}~\cite{C11}, but fixes many of the well known C problems while containing modern language-features. 241 Without significant extension to the C programming language, it is becoming unable to cope with the needs of modern programming problems and programmers; 240 242 as a result, it will fade into disuse. 241 243 Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language. … … 253 255 \begin{lstlisting} 254 256 ®forall( otype T )® T identity( T val ) { return val; } 255 int forty_two = identity( 42 ); 257 int forty_two = identity( 42 ); §\C{// T is bound to int, forty\_two == 42}§ 256 258 \end{lstlisting} 257 259 % extending the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC{}} approach of object-oriented extensions. … … 305 307 \begin{lstlisting} 306 308 forall( dtype T | sized(T) ) T * malloc( void ) { return (T *)malloc( sizeof(T) ); } 307 int * ip = malloc(); 309 int * ip = malloc(); §\C{// select type and size from left-hand side}§ 308 310 double * dp = malloc(); 309 311 struct S {...} * sp = malloc(); … … 316 318 Whereas, \CFA wraps each of these routines into ones with the overloaded name ©abs©: 317 319 \begin{cfa} 318 char ®abs®( char );319 extern "C" { int ®abs®( int ); }§\C{// use default C routine for int}§320 long int ®abs®( long int );321 long long int ®abs®( long long int );322 float ®abs®( float );323 double ®abs®( double );324 long double ®abs®( long double );325 float _Complex ®abs®( float _Complex );326 double _Complex ®abs®( double _Complex );327 long double _Complex ®abs®( long double _Complex );320 char abs( char ); 321 ®extern "C" {® int abs( int ); ®}® §\C{// use default C routine for int}§ 322 long int abs( long int ); 323 long long int abs( long long int ); 324 float abs( float ); 325 double abs( double ); 326 long double abs( long double ); 327 float _Complex abs( float _Complex ); 328 double _Complex abs( double _Complex ); 329 long double _Complex abs( long double _Complex ); 328 330 \end{cfa} 329 331 The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©. 330 332 Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}. 331 333 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. 332 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.333 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type s.334 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. 335 There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type. 334 336 335 337 This example strongly illustrates a core idea in \CFA: \emph{the \Index{power of a name}}. … … 348 350 \begin{description} 349 351 \item 350 \Indexc{-std=gnu 11}\index{compilation option!-std=gnu11@{©-std=gnu11©}}351 The 2011C standard plus GNU extensions.352 \Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{©-std=gnu99©}} 353 The 1999 C standard plus GNU extensions. 352 354 \item 353 355 \Indexc[deletekeywords=inline]{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{\lstinline[deletekeywords=inline]@-fgnu89-inline@}} 354 Use the traditional GNU semantics for inline routines in C 11mode, which allows inline routines in header files.356 Use the traditional GNU semantics for inline routines in C99 mode, which allows inline routines in header files. 355 357 \end{description} 356 358 The following new \CFA options are available: … … 425 427 \begin{cfa} 426 428 #ifndef __CFORALL__ 427 #include <stdio.h>§\indexc{stdio.h}§ 429 #include <stdio.h>§\indexc{stdio.h}§ §\C{// C header file}§ 428 430 #else 429 #include <fstream>§\indexc{fstream}§ 431 #include <fstream>§\indexc{fstream}§ §\C{// \CFA header file}§ 430 432 #endif 431 433 \end{cfa} … … 433 435 434 436 435 \section{Backquote Identifiers}436 \label{s:BackquoteIdentifiers}437 438 \CFA introduces several new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code.439 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism:440 \begin{cfa}441 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§442 double ®`®forall®`® = 3.5;443 \end{cfa}444 445 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.446 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©.447 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience.448 449 \begin{figure}450 \begin{cfa}451 // include file uses the CFA keyword "with".452 #if ! defined( with ) §\C{// nesting ?}§453 #define with ®`®with®`® §\C{// make keyword an identifier}§454 #define __CFA_BFD_H__455 #endif456 457 ®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§458 ®459 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§460 #undef with461 #undef __CFA_BFD_H__462 #endif463 \end{cfa}464 \caption{Header-File Interposition}465 \label{f:HeaderFileInterposition}466 \end{figure}467 468 469 437 \section{Constant Underscores} 470 438 471 439 Numeric constants are extended to allow \Index{underscore}s\index{constant!underscore}, \eg: 472 440 \begin{cfa} 473 2®_®147®_®483®_®648; 474 56®_®ul; 475 0®_®377; 476 0x®_®ff®_®ff; 477 0x®_®ef3d®_®aa5c; 478 3.141®_®592®_®654; 479 10®_®e®_®+1®_®00; 480 0x®_®ff®_®ff®_®p®_®3; 481 0x®_®1.ffff®_®ffff®_®p®_®128®_®l; 441 2®_®147®_®483®_®648; §\C{// decimal constant}§ 442 56®_®ul; §\C{// decimal unsigned long constant}§ 443 0®_®377; §\C{// octal constant}§ 444 0x®_®ff®_®ff; §\C{// hexadecimal constant}§ 445 0x®_®ef3d®_®aa5c; §\C{// hexadecimal constant}§ 446 3.141®_®592®_®654; §\C{// floating constant}§ 447 10®_®e®_®+1®_®00; §\C{// floating constant}§ 448 0x®_®ff®_®ff®_®p®_®3; §\C{// hexadecimal floating}§ 449 0x®_®1.ffff®_®ffff®_®p®_®128®_®l; §\C{// hexadecimal floating long constant}§ 482 450 L®_®§"\texttt{\textbackslash{x}}§®_®§\texttt{ff}§®_®§\texttt{ee}"§; §\C{// wide character constant}§ 483 451 \end{cfa} … … 501 469 502 470 471 \section{Backquote Identifiers} 472 \label{s:BackquoteIdentifiers} 473 474 \CFA introduces several new keywords (see \VRef{s:CFAKeywords}) that can clash with existing C variable-names in legacy code. 475 Keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: 476 \begin{cfa} 477 int ®`®otype®`® = 3; §\C{// make keyword an identifier}§ 478 double ®`®forall®`® = 3.5; 479 \end{cfa} 480 481 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. 482 \VRef[Figure]{f:HeaderFileInterposition} shows how clashes in existing C header-files (see~\VRef{s:StandardHeaders}) can be handled using preprocessor \newterm{interposition}: ©#include_next© and ©-I filename©. 483 Several common C header-files with keyword clashes are fixed in the standard \CFA header-library, so there is a seamless programming-experience. 484 485 \begin{figure} 486 \begin{cfa} 487 // include file uses the CFA keyword "with". 488 #if ! defined( with ) §\C{// nesting ?}§ 489 #define with ®`®with®`® §\C{// make keyword an identifier}§ 490 #define __CFA_BFD_H__ 491 #endif 492 493 ®#include_next <bfdlink.h> §\C{// must have internal check for multiple expansion}§ 494 ® 495 #if defined( with ) && defined( __CFA_BFD_H__ ) §\C{// reset only if set}§ 496 #undef with 497 #undef __CFA_BFD_H__ 498 #endif 499 \end{cfa} 500 \caption{Header-File Interposition} 501 \label{f:HeaderFileInterposition} 502 \end{figure} 503 504 503 505 \section{Exponentiation Operator} 504 506 … … 516 518 256 64 -64 0.015625 -0.015625 18.3791736799526 0.264715-1.1922i 517 519 \end{cfa} 518 Parenthesis are necessary for the complex constants or the expres sion is parsed as ©1.0f+(2.0fi \ 3.0f)+2.0fi©.520 Parenthesis are necessary for the complex constants or the expresion is parsed as ©1.0f+(2.0fi \ 3.0f)+2.0fi©. 519 521 The exponentiation operator is available for all the basic types, but for user-defined types, only the integral-computation versions are available. 520 522 For returning an integral value, the user type ©T© must define multiplication, ©*©, and one, ©1©; … … 522 524 523 525 524 \section{Control Structures} 525 526 \CFA identifies inconsistent, problematic, and missing control structures in C, and extends, modifies, and adds control structures to increase functionality and safety. 527 528 529 %\subsection{\texorpdfstring{\protect\lstinline@if@ Statement}{if Statement}} 530 \subsection{\texorpdfstring{\LstKeywordStyle{if} Statement}{if Statement}} 531 532 The ©if© expression allows declarations, similar to ©for© declaration expression: 533 \begin{cfa} 534 if ( int x = f() ) ... §\C{// x != 0}§ 535 if ( int x = f(), y = g() ) ... §\C{// x != 0 \&\& y != 0}§ 536 if ( int x = f(), y = g(); ®x < y® ) ... §\C{// relational expression}§ 537 \end{cfa} 538 Unless a relational expression is specified, each variable is compared not equal to 0, which is the standard semantics for the ©if© expression, and the results are combined using the logical ©&&© operator.\footnote{\CC only provides a single declaration always compared not equal to 0.} 539 The scope of the declaration(s) is local to the @if@ statement but exist within both the ``then'' and ``else'' clauses. 540 541 542 %\section{\texorpdfstring{\protect\lstinline@switch@ Statement}{switch Statement}} 543 \subsection{\texorpdfstring{\LstKeywordStyle{switch} Statement}{switch Statement}} 544 545 C allows a number of questionable forms for the ©switch© statement: 546 \begin{enumerate} 547 \item 548 By default, the end of a ©case© clause\footnote{ 549 In this section, the term \emph{case clause} refers to either a ©case© or ©default© clause.} 550 \emph{falls through} to the next ©case© clause in the ©switch© statement; 551 to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©: 552 \begin{cfa} 553 switch ( i ) { 554 case 1: 555 ... 556 // fall-through 557 case 2: 558 ... 559 break; // exit switch statement 560 } 561 \end{cfa} 562 The ability to fall-through to the next clause \emph{is} a useful form of control flow, specifically when a sequence of case actions compound: 563 \begin{cquote} 564 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 565 \begin{cfa} 566 switch ( argc ) { 567 case 3: 568 // open output file 569 // fall-through 570 case 2: 571 // open input file 572 break; // exit switch statement 573 default: 574 // usage message 575 } 576 \end{cfa} 577 & 578 \begin{cfa} 579 580 if ( argc == 3 ) { 581 // open output file 582 ®// open input file 583 ®} else if ( argc == 2 ) { 584 ®// open input file (duplicate) 585 586 ®} else { 587 // usage message 588 } 589 \end{cfa} 590 \end{tabular} 591 \end{cquote} 592 In this example, case 2 is always done if case 3 is done. 593 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. 594 C also uses fall-through to handle multiple case-values resulting in the same action: 595 \begin{cfa} 596 switch ( i ) { 597 ®case 1: case 3: case 5:® // odd values 598 // odd action 599 break; 600 ®case 2: case 4: case 6:® // even values 601 // even action 602 break; 603 } 604 \end{cfa} 605 However, this situation is handled in other languages without fall-through by allowing a list of case values. 606 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. 607 Hence, default fall-through semantics results in a large number of programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through. 608 609 \item 610 It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch© statement: 611 \begin{cfa} 612 switch ( i ) { 613 case 0: 614 if ( j < k ) { 615 ... 616 ®case 1:® // transfer into "if" statement 617 ... 618 } // if 619 case 2: 620 while ( j < 5 ) { 621 ... 622 ®case 3:® // transfer into "while" statement 623 ... 624 } // while 625 } // switch 626 \end{cfa} 627 The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties. 628 The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it. 629 The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning. 630 There are no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 631 Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: 632 \begin{cfa} 633 register int n = (count + 7) / 8; 634 switch ( count % 8 ) { 635 case 0: do{ *to = *from++; 636 case 7: *to = *from++; 637 case 6: *to = *from++; 638 case 5: *to = *from++; 639 case 4: *to = *from++; 640 case 3: *to = *from++; 641 case 2: *to = *from++; 642 case 1: *to = *from++; 643 } while ( --n > 0 ); 644 } 645 \end{cfa} 646 which unrolls a loop N times (N = 8 above) and uses the ©switch© statement to deal with any iterations not a multiple of N. 647 While efficient, this sort of special purpose usage is questionable: 648 \begin{quote} 649 Disgusting, no? But it compiles and runs just fine. I feel a combination of pride and revulsion at this 650 discovery.~\cite{Duff83} 651 \end{quote} 652 \item 653 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. 654 Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list. 655 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected; 656 hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics. 657 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. 658 659 \item 660 It is possible to place unreachable code at the start of a ©switch© statement, as in: 661 \begin{cfa} 662 switch ( x ) { 663 ®int y = 1;® §\C{// unreachable initialization}§ 664 ®x = 7;® §\C{// unreachable code without label/branch}§ 665 case 0: ... 666 ... 667 ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§ 668 z = 2; 669 case 1: 670 ®x = z;® §\C{// without fall through, z is uninitialized}§ 671 } 672 \end{cfa} 673 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. 674 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. 675 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. 676 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body. 677 \end{enumerate} 678 679 Before discussing potential language changes to deal with these problems, it is worth observing that in a typical C program: 680 \begin{itemize} 681 \item 682 the number of ©switch© statements is small, 683 \item 684 most ©switch© statements are well formed (\ie no \Index*{Duff's device}), 685 \item 686 the ©default© clause is usually written as the last case-clause, 687 \item 688 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. 689 \end{itemize} 690 These observations put into perspective the \CFA changes to the ©switch©. 691 \begin{enumerate} 692 \item 693 Eliminating default fall-through has the greatest potential for affecting existing code. 694 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: 695 \begin{cfa} 696 case 1: case 2: case 3: ... 697 \end{cfa} 698 still works. 699 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. 700 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: 701 \begin{cfa} 702 ®choose® ( i ) { 703 case 1: case 2: case 3: 704 ... 705 ®// implicit end of switch (break) 706 ®case 5: 707 ... 708 ®fallthru®; §\C{// explicit fall through}§ 709 case 7: 710 ... 711 ®break® §\C{// explicit end of switch (redundant)}§ 712 default: 713 j = 3; 714 } 715 \end{cfa} 716 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses; 717 An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. 718 An explicit ©fallthru© is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the ©choose© statement. 719 As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers. 720 \item 721 \Index*{Duff's device} is eliminated from both ©switch© and ©choose© statements, and only invalidates a small amount of very questionable code. 722 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. 723 \item 724 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. 725 Therefore, no change is made for this issue. 726 \item 727 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{ 728 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. 729 Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound. 730 \begin{cfa} 731 switch ( x ) { 732 ®int i = 0;® §\C{// allowed only at start}§ 733 case 0: 734 ... 735 ®int j = 0;® §\C{// disallowed}§ 736 case 1: 737 { 738 ®int k = 0;® §\C{// allowed at different nesting levels}§ 739 ... 740 ®case 2:® §\C{// disallow case in nested statements}§ 741 } 742 ... 743 } 744 \end{cfa} 745 \end{enumerate} 746 747 748 %\section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}} 749 \subsection{\texorpdfstring{\LstKeywordStyle{case} Statement}{case Statement}} 750 751 C restricts the ©case© clause of a ©switch© statement to a single value. 752 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values. 753 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C. 754 Therefore, the ©case© clause is extended with a list of values, as in: 755 \begin{cquote} 756 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 757 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 758 \begin{cfa} 759 switch ( i ) { 760 case ®1, 3, 5®: 761 ... 762 case ®2, 4, 6®: 763 ... 764 } 765 \end{cfa} 766 & 767 \begin{cfa} 768 switch ( i ) { 769 case 1: case 3 : case 5: 770 ... 771 case 2: case 4 : case 6: 772 ... 773 } 774 \end{cfa} 775 & 776 \begin{cfa} 777 778 // odd values 779 780 // even values 781 782 783 \end{cfa} 784 \end{tabular} 785 \end{cquote} 786 In addition, subranges are allowed to specify case values.\footnote{ 787 gcc has the same mechanism but awkward syntax, \lstinline@2 ...42@, because a space is required after a number, otherwise the period is a decimal point.} 788 \begin{cfa} 789 switch ( i ) { 790 case ®1~5:® §\C{// 1, 2, 3, 4, 5}§ 791 ... 792 case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§ 793 ... 794 } 795 \end{cfa} 796 Lists of subranges are also allowed. 797 \begin{cfa} 798 case ®1~5, 12~21, 35~42®: 799 \end{cfa} 800 801 802 %\subsection{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}} 803 \subsection{\texorpdfstring{Labelled \LstKeywordStyle{continue} / \LstKeywordStyle{break} Statement}{Labelled continue / break Statement}} 526 \section{\texorpdfstring{Labelled \protect\lstinline@continue@ / \protect\lstinline@break@}{Labelled continue / break}} 804 527 805 528 While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. … … 903 626 With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. 904 627 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. 905 Otherwise, the implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed. 906 907 908 %\section{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}} 909 \section{\texorpdfstring{\LstKeywordStyle{with} Statement}{with Statement}} 628 The implicit targets of the current ©continue© and ©break©, \ie the closest enclosing loop or ©switch©, change as certain constructs are added or removed. 629 630 631 \section{\texorpdfstring{\protect\lstinline@switch@ Statement}{switch Statement}} 632 633 C allows a number of questionable forms for the ©switch© statement: 634 \begin{enumerate} 635 \item 636 By default, the end of a ©case© clause\footnote{ 637 In this section, the term \emph{case clause} refers to either a ©case© or ©default© clause.} 638 \emph{falls through} to the next ©case© clause in the ©switch© statement; 639 to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©: 640 \begin{cfa} 641 switch ( i ) { 642 case 1: 643 ... 644 // fall-through 645 case 2: 646 ... 647 break; // exit switch statement 648 } 649 \end{cfa} 650 The ability to fall-through to the next clause \emph{is} a useful form of control flow, specifically when a sequence of case actions compound: 651 \begin{cquote} 652 \begin{tabular}{@{}l@{\hspace{3em}}l@{}} 653 \begin{cfa} 654 switch ( argc ) { 655 case 3: 656 // open output file 657 // fall-through 658 case 2: 659 // open input file 660 break; // exit switch statement 661 default: 662 // usage message 663 } 664 \end{cfa} 665 & 666 \begin{cfa} 667 668 if ( argc == 3 ) { 669 // open output file 670 ®// open input file 671 ®} else if ( argc == 2 ) { 672 ®// open input file (duplicate) 673 674 ®} else { 675 // usage message 676 } 677 \end{cfa} 678 \end{tabular} 679 \end{cquote} 680 In this example, case 2 is always done if case 3 is done. 681 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. 682 C also uses fall-through to handle multiple case-values resulting in the same action: 683 \begin{cfa} 684 switch ( i ) { 685 ®case 1: case 3: case 5:® // odd values 686 // odd action 687 break; 688 ®case 2: case 4: case 6:® // even values 689 // even action 690 break; 691 } 692 \end{cfa} 693 However, this situation is handled in other languages without fall-through by allowing a list of case values. 694 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. 695 Hence, default fall-through semantics results in a large number of programming errors as programmers often \emph{forget} the ©break© statement at the end of a ©case© clause, resulting in inadvertent fall-through. 696 697 \item 698 It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch© statement: 699 \begin{cfa} 700 switch ( i ) { 701 case 0: 702 if ( j < k ) { 703 ... 704 ®case 1:® // transfer into "if" statement 705 ... 706 } // if 707 case 2: 708 while ( j < 5 ) { 709 ... 710 ®case 3:® // transfer into "while" statement 711 ... 712 } // while 713 } // switch 714 \end{cfa} 715 The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties. 716 The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it. 717 The technical problem results from the inability to ensure declaration and initialization of variables when blocks are not entered at the beginning. 718 There are no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. 719 Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: 720 \begin{cfa} 721 register int n = (count + 7) / 8; 722 switch ( count % 8 ) { 723 case 0: do{ *to = *from++; 724 case 7: *to = *from++; 725 case 6: *to = *from++; 726 case 5: *to = *from++; 727 case 4: *to = *from++; 728 case 3: *to = *from++; 729 case 2: *to = *from++; 730 case 1: *to = *from++; 731 } while ( --n > 0 ); 732 } 733 \end{cfa} 734 which unrolls a loop N times (N = 8 above) and uses the ©switch© statement to deal with any iterations not a multiple of N. 735 While efficient, this sort of special purpose usage is questionable: 736 \begin{quote} 737 Disgusting, no? But it compiles and runs just fine. I feel a combination of pride and revulsion at this 738 discovery.~\cite{Duff83} 739 \end{quote} 740 \item 741 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. 742 Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list. 743 The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected; 744 hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics. 745 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. 746 747 \item 748 It is possible to place unreachable code at the start of a ©switch© statement, as in: 749 \begin{cfa} 750 switch ( x ) { 751 ®int y = 1;® §\C{// unreachable initialization}§ 752 ®x = 7;® §\C{// unreachable code without label/branch}§ 753 case 0: ... 754 ... 755 ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§ 756 z = 2; 757 case 1: 758 ®x = z;® §\C{// without fall through, z is uninitialized}§ 759 } 760 \end{cfa} 761 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. 762 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. 763 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. 764 The key observation is that the ©switch© statement branches into control structure, \ie there are multiple entry points into its statement body. 765 \end{enumerate} 766 767 Before discussing potential language changes to deal with these problems, it is worth observing that in a typical C program: 768 \begin{itemize} 769 \item 770 the number of ©switch© statements is small, 771 \item 772 most ©switch© statements are well formed (\ie no \Index*{Duff's device}), 773 \item 774 the ©default© clause is usually written as the last case-clause, 775 \item 776 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. 777 \end{itemize} 778 These observations put into perspective the \CFA changes to the ©switch©. 779 \begin{enumerate} 780 \item 781 Eliminating default fall-through has the greatest potential for affecting existing code. 782 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: 783 \begin{cfa} 784 case 1: case 2: case 3: ... 785 \end{cfa} 786 still works. 787 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. 788 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: 789 \begin{cfa} 790 ®choose® ( i ) { 791 case 1: case 2: case 3: 792 ... 793 ®// implicit end of switch (break) 794 ®case 5: 795 ... 796 ®fallthru®; §\C{// explicit fall through}§ 797 case 7: 798 ... 799 ®break® §\C{// explicit end of switch (redundant)}§ 800 default: 801 j = 3; 802 } 803 \end{cfa} 804 Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses; 805 An implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. 806 An explicit ©fallthru© is retained because it is a C-idiom most C programmers expect, and its absence might discourage programmers from using the ©choose© statement. 807 As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers. 808 \item 809 \Index*{Duff's device} is eliminated from both ©switch© and ©choose© statements, and only invalidates a small amount of very questionable code. 810 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. 811 \item 812 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. 813 Therefore, no change is made for this issue. 814 \item 815 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{ 816 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. 817 Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound. 818 \begin{cfa} 819 switch ( x ) { 820 ®int i = 0;® §\C{// allowed only at start}§ 821 case 0: 822 ... 823 ®int j = 0;® §\C{// disallowed}§ 824 case 1: 825 { 826 ®int k = 0;® §\C{// allowed at different nesting levels}§ 827 ... 828 ®case 2:® §\C{// disallow case in nested statements}§ 829 } 830 ... 831 } 832 \end{cfa} 833 \end{enumerate} 834 835 836 \section{\texorpdfstring{\protect\lstinline@case@ Clause}{case Clause}} 837 838 C restricts the ©case© clause of a ©switch© statement to a single value. 839 For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values. 840 Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C. 841 Therefore, the ©case© clause is extended with a list of values, as in: 842 \begin{cquote} 843 \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} 844 \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ 845 \begin{cfa} 846 switch ( i ) { 847 case ®1, 3, 5®: 848 ... 849 case ®2, 4, 6®: 850 ... 851 } 852 \end{cfa} 853 & 854 \begin{cfa} 855 switch ( i ) { 856 case 1: case 3 : case 5: 857 ... 858 case 2: case 4 : case 6: 859 ... 860 } 861 \end{cfa} 862 & 863 \begin{cfa} 864 865 // odd values 866 867 // even values 868 869 870 \end{cfa} 871 \end{tabular} 872 \end{cquote} 873 In addition, subranges are allowed to specify case values.\footnote{ 874 gcc has the same mechanism but awkward syntax, \lstinline@2 ...42@, because a space is required after a number, otherwise the period is a decimal point.} 875 \begin{cfa} 876 switch ( i ) { 877 case ®1~5:® §\C{// 1, 2, 3, 4, 5}§ 878 ... 879 case ®10~15:® §\C{// 10, 11, 12, 13, 14, 15}§ 880 ... 881 } 882 \end{cfa} 883 Lists of subranges are also allowed. 884 \begin{cfa} 885 case ®1~5, 12~21, 35~42®: 886 \end{cfa} 887 888 889 \section{\texorpdfstring{\protect\lstinline@with@ Statement}{with Statement}} 910 890 \label{s:WithStatement} 911 891 … … 922 902 \begin{cfa} 923 903 void f( S s ) { 924 ®s.®c; ®s.®i; ®s.®d; §\C{// access containing fields}§904 `s.`c; `s.`i; `s.`d; §\C{// access containing fields}§ 925 905 } 926 906 \end{cfa} … … 933 913 double d; 934 914 void f() { §\C{// implicit ``this'' aggregate}§ 935 ®this->®c; ®this->®i; ®this->®d; §\C{// access containing fields}§915 `this->`c; `this->`i; `this->`d; §\C{// access containing fields}§ 936 916 } 937 917 } 938 918 \end{C++} 939 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++] @this->@because of lexical scoping.919 Object-oriented nesting of member functions in a \lstinline[language=C++]@class/struct@ allows eliding \lstinline[language=C++]$this->$ because of lexical scoping. 940 920 However, for other aggregate parameters, qualification is necessary: 941 921 \begin{cfa} … … 943 923 int S::f( T & t ) { §\C{// multiple aggregate parameters}§ 944 924 c; i; d; §\C{\color{red}// this--{\textgreater}.c, this--{\textgreater}.i, this--{\textgreater}.d}§ 945 ®t.®m; ®t.®n; §\C{// must qualify}§946 } 947 \end{cfa} 948 949 To simplify the programmer experience, \CFA provides a ©with©statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers.925 `t.`m; `t.`n; §\C{// must qualify}§ 926 } 927 \end{cfa} 928 929 To simplify the programmer experience, \CFA provides a @with@ statement (see Pascal~\cite[\S~4.F]{Pascal}) to elide aggregate qualification to fields by opening a scope containing the field identifiers. 950 930 Hence, the qualified fields become variables with the side-effect that it is easier to optimizing field references in a block. 951 931 \begin{cfa} 952 void f( S & this ) ®with ( this )®{ §\C{// with statement}§932 void f( S & this ) `with ( this )` { §\C{// with statement}§ 953 933 c; i; d; §\C{\color{red}// this.c, this.i, this.d}§ 954 934 } … … 956 936 with the generality of opening multiple aggregate-parameters: 957 937 \begin{cfa} 958 void f( S & s, T & t ) ®with ( s, t )® {§\C{// multiple aggregate parameters}§938 void f( S & s, T & t ) `with ( s, t )` { §\C{// multiple aggregate parameters}§ 959 939 c; i; d; §\C{\color{red}// s.c, s.i, s.d}§ 960 940 m; n; §\C{\color{red}// t.m, t.n}§ … … 962 942 \end{cfa} 963 943 964 In detail, the ©with©statement has the form:944 In detail, the @with@ statement has the form: 965 945 \begin{cfa} 966 946 §\emph{with-statement}§: … … 977 957 The difference between parallel and nesting occurs for fields with the same name and type: 978 958 \begin{cfa} 979 struct S { int ®i®; int j; double m; } s, w;980 struct T { int ®i®; int k; int m; } t, w;959 struct S { int `i`; int j; double m; } s, w; 960 struct T { int `i`; int k; int m; } t, w; 981 961 with ( s, t ) { 982 962 j + k; §\C{// unambiguous, s.j + t.k}§ … … 989 969 } 990 970 \end{cfa} 991 For parallel semantics, both ©s.i© and ©t.i© are visible, so ©i©is ambiguous without qualification;992 for nested semantics, ©t.i© hides ©s.i©, so ©i© implies ©t.i©.971 For parallel semantics, both @s.i@ and @t.i@ are visible, so @i@ is ambiguous without qualification; 972 for nested semantics, @t.i@ hides @s.i@, so @i@ implies @t.i@. 993 973 \CFA's ability to overload variables means fields with the same name but different types are automatically disambiguated, eliminating most qualification when opening multiple aggregates. 994 974 Qualification or a cast is used to disambiguate. 995 975 996 There is an interesting problem between parameters and the function-body ©with©, \eg:976 There is an interesting problem between parameters and the function-body @with@, \eg: 997 977 \begin{cfa} 998 978 void ?{}( S & s, int i ) with ( s ) { §\C{// constructor}§ 999 ®s.i = i;®j = 3; m = 5.5; §\C{// initialize fields}§1000 } 1001 \end{cfa} 1002 Here, the assignment ©s.i = i© means ©s.i = s.i©, which is meaningless, and there is no mechanism to qualify the parameter ©i©, making the assignment impossible using the function-body ©with©.979 `s.i = i;` j = 3; m = 5.5; §\C{// initialize fields}§ 980 } 981 \end{cfa} 982 Here, the assignment @s.i = i@ means @s.i = s.i@, which is meaningless, and there is no mechanism to qualify the parameter @i@, making the assignment impossible using the function-body @with@. 1003 983 To solve this problem, parameters are treated like an initialized aggregate: 1004 984 \begin{cfa} … … 1010 990 and implicitly opened \emph{after} a function-body open, to give them higher priority: 1011 991 \begin{cfa} 1012 void ?{}( S & s, int ®i® ) with ( s ) ®with( §\emph{\color{red}params}§ )®{1013 s.i = ®i®; j = 3; m = 5.5;1014 } 1015 \end{cfa} 1016 Finally, a cast may be used to disambiguate among overload variables in a ©with©expression:992 void ?{}( S & s, int `i` ) with ( s ) `with( §\emph{\color{red}params}§ )` { 993 s.i = `i`; j = 3; m = 5.5; 994 } 995 \end{cfa} 996 Finally, a cast may be used to disambiguate among overload variables in a @with@ expression: 1017 997 \begin{cfa} 1018 998 with ( w ) { ... } §\C{// ambiguous, same name and no context}§ 1019 999 with ( (S)w ) { ... } §\C{// unambiguous, cast}§ 1020 1000 \end{cfa} 1021 and ©with©expressions may be complex expressions with type reference (see Section~\ref{s:References}) to aggregate:1001 and @with@ expressions may be complex expressions with type reference (see Section~\ref{s:References}) to aggregate: 1022 1002 % \begin{cfa} 1023 1003 % struct S { int i, j; } sv; … … 1039 1019 class C { 1040 1020 int i, j; 1041 int mem() { 1042 i = 1; 1043 j = 2; 1021 int mem() { §\C{\color{red}// implicit "this" parameter}§ 1022 i = 1; §\C{\color{red}// this->i}§ 1023 j = 2; §\C{\color{red}// this->j}§ 1044 1024 } 1045 1025 } … … 1048 1028 \begin{cfa} 1049 1029 struct S { int i, j; }; 1050 int mem( S & ®this® ) { 1051 ®this.®i = 1; 1030 int mem( S & ®this® ) { §\C{// explicit "this" parameter}§ 1031 ®this.®i = 1; §\C{// "this" is not elided}§ 1052 1032 ®this.®j = 2; 1053 1033 } … … 1057 1037 \CFA provides a ©with© clause/statement (see Pascal~\cite[\S~4.F]{Pascal}) to elided the "©this.©" by opening a scope containing field identifiers, changing the qualified fields into variables and giving an opportunity for optimizing qualified references. 1058 1038 \begin{cfa} 1059 int mem( S & this ) ®with( this )® { 1060 i = 1; 1061 j = 2; 1039 int mem( S & this ) ®with( this )® { §\C{// with clause}§ 1040 i = 1; §\C{\color{red}// this.i}§ 1041 j = 2; §\C{\color{red}// this.j}§ 1062 1042 } 1063 1043 \end{cfa} … … 1076 1056 struct S1 { ... } s1; 1077 1057 struct S2 { ... } s2; 1078 ®with( s1 )® { 1058 ®with( s1 )® { §\C{// with statement}§ 1079 1059 // access fields of s1 without qualification 1080 ®with s2® { 1060 ®with s2® { §\C{// nesting}§ 1081 1061 // access fields of s1 and s2 without qualification 1082 1062 } … … 1133 1113 Non-local transfer can cause stack unwinding, \ie non-local routine termination, depending on the kind of raise. 1134 1114 \begin{cfa} 1135 exception_t E {}; 1115 exception_t E {}; §\C{// exception type}§ 1136 1116 void f(...) { 1137 ... throw E{}; ... 1138 ... throwResume E{}; ... 1117 ... throw E{}; ... §\C{// termination}§ 1118 ... throwResume E{}; ... §\C{// resumption}§ 1139 1119 } 1140 1120 try { 1141 1121 f(...); 1142 } catch( E e ; §boolean-predicate§ ) { §\C[8cm]{// termination handler}§1122 } catch( E e ; §boolean-predicate§ ) { §\C[8cm]{// termination handler}§ 1143 1123 // recover and continue 1144 } catchResume( E e ; §boolean-predicate§ ) { 1124 } catchResume( E e ; §boolean-predicate§ ) { §\C{// resumption handler}\CRT§ 1145 1125 // repair and return 1146 1126 } finally { … … 1662 1642 \begin{itemize} 1663 1643 \item 1664 if ©R© is an \Index{rvalue} of type ©T & ©$_1\cdots$ ©&©$_r$, where $r \ge 1$ references (©&© symbols), than ©&R© has type ©T ®*®&©$_{\color{red}2}\cdots$ ©&©$_{\color{red}r}$, \ie ©T© pointer with $r-1$ references (©&© symbols).1665 1666 \item 1667 if ©L© is an \Index{lvalue} of type ©T & ©$_1\cdots$ ©&©$_l$, where $l \ge 0$ references (©&© symbols), than ©&L© has type ©T ®*®&©$_{\color{red}1}\cdots$ ©&©$_{\color{red}l}$, \ie ©T© pointer with $l$ references (©&© symbols).1644 if ©R© is an \Index{rvalue} of type ©T &$_1$...&$_r$© where $r \ge 1$ references (©&© symbols) then ©&R© has type ©T ®*®&$_{\color{red}2}$...&$_{\color{red}r}$©, \ie ©T© pointer with $r-1$ references (©&© symbols). 1645 1646 \item 1647 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). 1668 1648 \end{itemize} 1669 1649 The following example shows the first rule applied to different \Index{rvalue} contexts: … … 6019 5999 6020 6000 6021 \subsection{ String to ValueConversion}6001 \subsection{Conversion} 6022 6002 6023 6003 \leavevmode … … 6089 6069 \leavevmode 6090 6070 \begin{cfa}[aboveskip=0pt,belowskip=0pt] 6091 void srandom( unsigned int seed );§\indexc{srandom}§ 6092 char random( void );§\indexc{random}§ 6093 char random( char u ); §\C{// [0,u)}§ 6094 char random( char l, char u ); §\C{// [l,u)}§ 6095 int random( void ); 6096 int random( int u ); §\C{// [0,u)}§ 6097 int random( int l, int u ); §\C{// [l,u)}§ 6098 unsigned int random( void ); 6099 unsigned int random( unsigned int u ); §\C{// [0,u)}§ 6100 unsigned int random( unsigned int l, unsigned int u ); §\C{// [l,u)}§ 6101 long int random( void ); 6102 long int random( long int u ); §\C{// [0,u)}§ 6103 long int random( long int l, long int u ); §\C{// [l,u)}§ 6104 unsigned long int random( void ); 6105 unsigned long int random( unsigned long int u ); §\C{// [0,u)}§ 6106 unsigned long int random( unsigned long int l, unsigned long int u ); §\C{// [l,u)}§ 6107 float random( void ); §\C{// [0.0, 1.0)}§ 6108 double random( void ); §\C{// [0.0, 1.0)}§ 6109 float _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6110 double _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6111 long double _Complex random( void ); §\C{// [0.0, 1.0)+[0.0, 1.0)i}§ 6071 void rand48seed( long int s );§\indexc{rand48seed}§ 6072 char rand48();§\indexc{rand48}§ 6073 int rand48(); 6074 unsigned int rand48(); 6075 long int rand48(); 6076 unsigned long int rand48(); 6077 float rand48(); 6078 double rand48(); 6079 float _Complex rand48(); 6080 double _Complex rand48(); 6081 long double _Complex rand48(); 6112 6082 \end{cfa} 6113 6083 … … 6488 6458 6489 6459 6490 \section{Time Keeping}6491 \label{s:Time Keeping}6460 \section{Time} 6461 \label{s:TimeLib} 6492 6462 6493 6463 6494 6464 %\subsection{\texorpdfstring{\protect\lstinline@Duration@}{Duration}} 6495 \subsection{\texorpdfstring{\Lst BasicStyle{Duration}}{Duration}}6465 \subsection{\texorpdfstring{\LstKeywordStyle{\textmd{Duration}}}{Duration}} 6496 6466 \label{s:Duration} 6497 6467 … … 6542 6512 6543 6513 Duration abs( Duration rhs ); 6514 6515 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Duration dur ); 6544 6516 6545 6517 Duration ?`ns( int64_t nsec ); … … 6565 6537 int64_t ?`d( Duration dur ); 6566 6538 int64_t ?`w( Duration dur ); 6567 6568 Duration max( Duration lhs, Duration rhs );6569 Duration min( Duration lhs, Duration rhs );6570 6539 \end{cfa} 6571 6540 6572 6541 6573 6542 %\subsection{\texorpdfstring{\protect\lstinline@\timeval@}{timeval}} 6574 \subsection{\texorpdfstring{\Lst BasicStyle{timeval}}{timeval}}6543 \subsection{\texorpdfstring{\LstKeywordStyle{\textmd{timeval}}}{timeval}} 6575 6544 \label{s:timeval} 6576 6545 … … 6591 6560 6592 6561 6593 %\subsection{\texorpdfstring{\protect\lstinline@timespec@}{timespec}} 6594 \subsection{\texorpdfstring{\LstBasicStyle{timespec}}{timespec}} 6562 \subsection{\texorpdfstring{\protect\lstinline@timespec@}{timespec}} 6595 6563 \label{s:timespec} 6596 6564 … … 6611 6579 6612 6580 6613 %\subsection{\texorpdfstring{\protect\lstinline@itimerval@}{itimerval}} 6614 \subsection{\texorpdfstring{\LstBasicStyle{itimerval}}{itimerval}} 6581 \subsection{\texorpdfstring{\protect\lstinline@itimerval@}{itimerval}} 6615 6582 \label{s:itimerval} 6616 6583 … … 6622 6589 6623 6590 6624 %\subsection{\texorpdfstring{\protect\lstinline@Time@}{Time}} 6625 \subsection{\texorpdfstring{\LstBasicStyle{Time}}{Time}} 6591 \subsection{\texorpdfstring{\protect\lstinline@Time@}{Time}} 6626 6592 \label{s:Time} 6627 6593 … … 6634 6600 void ?{}( Time & time ); 6635 6601 void ?{}( Time & time, zero_t ); 6636 6602 void ?{}( Time & time, int year, int month = 0, int day = 0, int hour = 0, int min = 0, int sec = 0, int nsec = 0 ); 6637 6603 Time ?=?( Time & time, zero_t ); 6638 6604 … … 6643 6609 Time ?=?( Time & time, timespec t ); 6644 6610 6645 Time ?+?( Time & lhs, Duration rhs ); 6646 Time ?+?( Duration lhs, Time rhs ); 6647 Time ?+=?( Time & lhs, Duration rhs ); 6648 6649 Duration ?-?( Time lhs, Time rhs ); 6650 Time ?-?( Time lhs, Duration rhs ); 6651 Time ?-=?( Time & lhs, Duration rhs ); 6652 _Bool ?==?( Time lhs, Time rhs ); 6653 _Bool ?!=?( Time lhs, Time rhs ); 6654 _Bool ?<?( Time lhs, Time rhs ); 6655 _Bool ?<=?( Time lhs, Time rhs ); 6656 _Bool ?>?( Time lhs, Time rhs ); 6657 _Bool ?>=?( Time lhs, Time rhs ); 6611 Time ?+?( Time & lhs, Duration rhs ) { return (Time)@{ lhs.tv + rhs.tv }; } 6612 Time ?+?( Duration lhs, Time rhs ) { return rhs + lhs; } 6613 Time ?+=?( Time & lhs, Duration rhs ) { lhs = lhs + rhs; return lhs; } 6614 6615 Duration ?-?( Time lhs, Time rhs ) { return (Duration)@{ lhs.tv - rhs.tv }; } 6616 Time ?-?( Time lhs, Duration rhs ) { return (Time)@{ lhs.tv - rhs.tv }; } 6617 Time ?-=?( Time & lhs, Duration rhs ) { lhs = lhs - rhs; return lhs; } 6618 _Bool ?==?( Time lhs, Time rhs ) { return lhs.tv == rhs.tv; } 6619 _Bool ?!=?( Time lhs, Time rhs ) { return lhs.tv != rhs.tv; } 6620 _Bool ?<?( Time lhs, Time rhs ) { return lhs.tv < rhs.tv; } 6621 _Bool ?<=?( Time lhs, Time rhs ) { return lhs.tv <= rhs.tv; } 6622 _Bool ?>?( Time lhs, Time rhs ) { return lhs.tv > rhs.tv; } 6623 _Bool ?>=?( Time lhs, Time rhs ) { return lhs.tv >= rhs.tv; } 6624 6625 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time ); 6658 6626 6659 6627 char * yy_mm_dd( Time time, char * buf ); … … 6673 6641 6674 6642 size_t strftime( char * buf, size_t size, const char * fmt, Time time ); 6675 forall( dtype ostype | ostream( ostype ) ) ostype & ?|?( ostype & os, Time time );6676 6643 \end{cfa} 6677 6644 … … 6694 6661 6695 6662 %\subsection{\texorpdfstring{\protect\lstinline@Clock@}{Clock}} 6696 \subsection{\texorpdfstring{\Lst BasicStyle{Clock}}{Clock}}6663 \subsection{\texorpdfstring{\LstKeywordStyle{\textmd{Clock}}}{Clock}} 6697 6664 \label{s:Clock} 6698 6665 … … 6708 6675 void ?{}( Clock & clk ); 6709 6676 void ?{}( Clock & clk, Duration adj ); 6710 6711 Duration getResNsec(); §\C{// with nanoseconds}§ 6712 Duration getRes(); §\C{// without nanoseconds}§ 6713 6677 Duration getRes(); 6714 6678 Time getTimeNsec(); §\C{// with nanoseconds}§ 6715 6679 Time getTime(); §\C{// without nanoseconds}§
Note:
See TracChangeset
for help on using the changeset viewer.