[53ba273] | 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -*- Mode: Latex -*- %%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 2 | %% |
---|
| 3 | %% Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 4 | %% |
---|
| 5 | %% The contents of this file are covered under the licence agreement in the |
---|
| 6 | %% file "LICENCE" distributed with Cforall. |
---|
| 7 | %% |
---|
| 8 | %% user.tex -- |
---|
| 9 | %% |
---|
| 10 | %% Author : Peter A. Buhr |
---|
| 11 | %% Created On : Wed Apr 6 14:53:29 2016 |
---|
| 12 | %% Last Modified By : Peter A. Buhr |
---|
[e80ebe5] | 13 | %% Last Modified On : Tue Aug 2 17:39:02 2016 |
---|
| 14 | %% Update Count : 1286 |
---|
[53ba273] | 15 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 16 | |
---|
[6c91065] | 17 | % requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended |
---|
| 18 | |
---|
[0638c44] | 19 | % inline code ©...© (copyright symbol) emacs: C-q M-) |
---|
[45576af9] | 20 | % red highlighting ®...® (registered trademark symbol) emacs: C-q M-. |
---|
| 21 | % blue highlighting ß...ß (sharp s symbol) emacs: C-q M-_ |
---|
| 22 | % green highlighting ¢...¢ (cent symbol) emacs: C-q M-" |
---|
[9724df0] | 23 | % LaTex escape §...§ (section symbol) emacs: C-q M-' |
---|
[0638c44] | 24 | % keyword escape ¶...¶ (pilcrow symbol) emacs: C-q M-^ |
---|
[e945826] | 25 | % math escape $...$ (dollar symbol) |
---|
| 26 | |
---|
[45576af9] | 27 | \documentclass[twoside,11pt]{article} |
---|
[9724df0] | 28 | |
---|
[6c91065] | 29 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 30 | |
---|
| 31 | % Latex packages used in the document. |
---|
[b52d900] | 32 | \usepackage[T1]{fontenc} % allow Latin1 (extended ASCII) characters |
---|
[53ba273] | 33 | \usepackage{textcomp} |
---|
| 34 | \usepackage[latin1]{inputenc} |
---|
[e229c22] | 35 | \usepackage{fullpage,times,comment} |
---|
[6b6597c] | 36 | \usepackage{epic,eepic} |
---|
[9724df0] | 37 | \usepackage{upquote} % switch curled `'" to straight |
---|
[4096de0] | 38 | \usepackage{calc} |
---|
[6c91065] | 39 | \usepackage{xspace} |
---|
[4096de0] | 40 | \usepackage{graphicx} |
---|
[e229c22] | 41 | \usepackage{varioref} % extended references |
---|
| 42 | \usepackage{listings} % format program code |
---|
[45576af9] | 43 | \usepackage[flushmargin]{footmisc} % support label/reference in footnote |
---|
[e229c22] | 44 | \usepackage{latexsym} % \Box glyph |
---|
[b52d900] | 45 | \usepackage{mathptmx} % better math font with "times" |
---|
[45576af9] | 46 | \usepackage[usenames]{color} |
---|
[6c91065] | 47 | \usepackage[pagewise]{lineno} |
---|
| 48 | \renewcommand{\linenumberfont}{\scriptsize\sffamily} |
---|
[45576af9] | 49 | \input{common} % bespoke macros used in the document |
---|
[6c91065] | 50 | \usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref} |
---|
| 51 | \usepackage{breakurl} |
---|
| 52 | \renewcommand{\UrlFont}{\small\sf} |
---|
| 53 | |
---|
[4096de0] | 54 | \setlength{\topmargin}{-0.45in} % move running title into header |
---|
| 55 | \setlength{\headsep}{0.25in} |
---|
[9724df0] | 56 | |
---|
| 57 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 58 | |
---|
[6c91065] | 59 | % Names used in the document. |
---|
| 60 | |
---|
[6b6597c] | 61 | \newcommand{\Version}{1.0.0} |
---|
[6c91065] | 62 | \newcommand{\CS}{C\raisebox{-0.9ex}{\large$^\sharp$}\xspace} |
---|
| 63 | |
---|
[45576af9] | 64 | \newcommand{\Textbf}[2][red]{{\color{#1}{\textbf{#2}}}} |
---|
| 65 | \newcommand{\Emph}[2][red]{{\color{#1}\textbf{\emph{#2}}}} |
---|
| 66 | \newcommand{\R}[1]{\Textbf{#1}} |
---|
| 67 | \newcommand{\B}[1]{{\Textbf[blue]{#1}}} |
---|
| 68 | \newcommand{\G}[1]{{\Textbf[OliveGreen]{#1}}} |
---|
| 69 | |
---|
[9724df0] | 70 | \newsavebox{\LstBox} |
---|
| 71 | |
---|
[6c91065] | 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 73 | |
---|
[b52d900] | 74 | \setcounter{secnumdepth}{3} % number subsubsections |
---|
| 75 | \setcounter{tocdepth}{3} % subsubsections in table of contents |
---|
[6c91065] | 76 | \makeindex |
---|
| 77 | |
---|
| 78 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 79 | |
---|
| 80 | \title{\Huge |
---|
| 81 | \vspace*{1in} |
---|
| 82 | \CFA (\CFL) User Manual \\ |
---|
| 83 | Version 1.0 \\ |
---|
| 84 | \vspace*{0.25in} |
---|
[9724df0] | 85 | \huge``describe not prescribe'' |
---|
[6c91065] | 86 | \vspace*{1in} |
---|
| 87 | }% title |
---|
[9724df0] | 88 | |
---|
[6c91065] | 89 | \author{\huge |
---|
[9724df0] | 90 | Peter A. Buhr and ... |
---|
[6c91065] | 91 | }% author |
---|
[9724df0] | 92 | |
---|
[6c91065] | 93 | \date{ |
---|
[9724df0] | 94 | DRAFT \\ \today |
---|
[6c91065] | 95 | }% date |
---|
| 96 | |
---|
[9724df0] | 97 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
---|
| 98 | |
---|
| 99 | \begin{document} |
---|
| 100 | \pagestyle{headings} |
---|
[4096de0] | 101 | % changed after setting pagestyle |
---|
| 102 | \renewcommand{\sectionmark}[1]{\markboth{\thesection\quad #1}{\thesection\quad #1}} |
---|
| 103 | \renewcommand{\subsectionmark}[1]{\markboth{\thesubsection\quad #1}{\thesubsection\quad #1}} |
---|
[6c91065] | 104 | \pagenumbering{roman} |
---|
[9724df0] | 105 | \linenumbers % comment out to turn off line numbering |
---|
[6c91065] | 106 | |
---|
| 107 | \maketitle |
---|
| 108 | \thispagestyle{empty} |
---|
[4096de0] | 109 | \vspace*{\fill} |
---|
[6c91065] | 110 | \noindent |
---|
| 111 | \copyright\,2016 \CFA Project \\ \\ |
---|
| 112 | \noindent |
---|
| 113 | This work is licensed under the Creative Commons Attribution 4.0 International License. |
---|
| 114 | To view a copy of this license, visit {\small\url{http://creativecommons.org/licenses/by/4.0}}. |
---|
| 115 | \vspace*{1in} |
---|
| 116 | |
---|
| 117 | \clearpage |
---|
[4096de0] | 118 | \thispagestyle{plain} |
---|
[6c91065] | 119 | \pdfbookmark[1]{Contents}{section} |
---|
| 120 | \tableofcontents |
---|
| 121 | |
---|
| 122 | \clearpage |
---|
[4096de0] | 123 | \thispagestyle{plain} |
---|
[6c91065] | 124 | \pagenumbering{arabic} |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | \section{Introduction} |
---|
| 128 | |
---|
[07bc165] | 129 | \CFA\footnote{Pronounced ``C-for-all'', and written \CFA, CFA, or \CFL.} is a modern general-purpose programming-language, designed as an evolutionary step forward from the C programming language. |
---|
| 130 | The syntax of the \CFA language builds from C, and should look immediately familiar to C/\Index*[C++]{\CC} programmers. |
---|
[6c91065] | 131 | % Any language feature that is not described here can be assumed to be using the standard C11 syntax. |
---|
[45576af9] | 132 | \CFA adds many modern programming-language features that directly lead to increased \emph{safety} and \emph{productivity}, while maintaining interoperability with existing C programs and achieving C performance. |
---|
[6c91065] | 133 | Like C, \CFA is a statically typed, procedural language with a low-overhead runtime, meaning there is no global garbage-collection. |
---|
[07bc165] | 134 | The primary new features include parametric-polymorphic routines and types, exceptions, concurrency, and modules. |
---|
[6c91065] | 135 | |
---|
| 136 | One of the main design philosophies of \CFA is to ``describe not prescribe'', which means \CFA tries to provide a pathway from low-level C programming to high-level \CFA programming, but it does not force programmers to ``do the right thing''. |
---|
| 137 | Programmers can cautiously add \CFA extensions to their C programs in any order and at any time to incrementally move towards safer, higher-level programming features. |
---|
| 138 | A programmer is always free to reach back to C from \CFA for any reason, and in many cases, new \CFA features have a fallback to a C mechanism. |
---|
| 139 | There is no notion or requirement for rewriting a legacy C program in \CFA; |
---|
| 140 | instead, a programmer evolves an existing C program into \CFA by incrementally incorporating \CFA features. |
---|
| 141 | New programs can be written in \CFA using a combination of C and \CFA features. |
---|
[07bc165] | 142 | \Index*[C++]{\CC} had a similar goal 30 years ago, but has struggled over the intervening time to incorporate modern programming-language features because of early design choices. |
---|
[45576af9] | 143 | \CFA has 30 years of hindsight and a clean starting point. |
---|
[6c91065] | 144 | |
---|
[07bc165] | 145 | Like \Index*[C++]{\CC}, there may be both an old and new ways to achieve the same effect. |
---|
[6c91065] | 146 | For example, the following programs compare the \CFA and C I/O mechanisms. |
---|
| 147 | \begin{quote2} |
---|
[45576af9] | 148 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 149 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
[6c91065] | 150 | \begin{lstlisting} |
---|
| 151 | #include <fstream> |
---|
| 152 | int main( void ) { |
---|
| 153 | int x = 0, y = 1, z = 2; |
---|
[45576af9] | 154 | ®sout | x | y | z | endl;® |
---|
[6c91065] | 155 | } |
---|
| 156 | \end{lstlisting} |
---|
| 157 | & |
---|
| 158 | \begin{lstlisting} |
---|
| 159 | #include <stdio.h> |
---|
| 160 | int main( void ) { |
---|
| 161 | int x = 0, y = 1, z = 2; |
---|
[45576af9] | 162 | ®printf( "%d %d %d\n", x, y, z );® |
---|
[6c91065] | 163 | } |
---|
| 164 | \end{lstlisting} |
---|
| 165 | \end{tabular} |
---|
| 166 | \end{quote2} |
---|
| 167 | Both programs output the same result. |
---|
[07bc165] | 168 | 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 also~\VRef{s:IOLibrary}). |
---|
[6c91065] | 169 | |
---|
[45576af9] | 170 | This document is a user manual for the \CFA programming language, targeted at \CFA programmers. |
---|
[b52d900] | 171 | Implementers may refer to the \CFA Programming Language Specification for details about the language syntax and semantics. |
---|
[6c91065] | 172 | In its current state, this document covers the intended core features of the language. |
---|
| 173 | Changes to the syntax and additional features are expected to be included in later revisions. |
---|
| 174 | % For additional information, see \url{http://wiki.do-lang.org}. |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | \section{History} |
---|
| 178 | |
---|
[45576af9] | 179 | The \CFA project started with K-W C~\cite{Buhr94a,Till89}, which extended C with new declaration syntax, multiple return values from routines, and extended assignment capabilities using the notion of tuples. |
---|
[07bc165] | 180 | (See~\cite{Werther96} for some similar work, but for \Index*[C++]{\CC}.) |
---|
| 181 | The original \CFA project~\cite{Ditchfield92} extended the C type system with parametric polymorphism and overloading, as opposed to the \Index*[C++]{\CC} approach of object-oriented extensions to the C type-system. |
---|
[6c91065] | 182 | A first implementation of the core Cforall language was created~\cite{Bilson03,Esteves04}, but at the time there was little interesting in extending C, so work did not continue. |
---|
[45576af9] | 183 | As the saying goes, ``What goes around, comes around.'', and there is now renewed interest in the C programming language because of legacy code-bases, so the \CFA project has been restarted. |
---|
[6c91065] | 184 | |
---|
| 185 | |
---|
[b52d900] | 186 | \section{Why fix C?} |
---|
[6c91065] | 187 | |
---|
| 188 | Even with all its problems, C is a very popular programming language because it allows writing software at virtually any level in a computer system without restriction. |
---|
[45576af9] | 189 | For system programming, where direct access to hardware and dealing with real-time issues is a requirement, C is usually the language of choice. |
---|
[6c91065] | 190 | As well, there are millions of lines of C legacy code, forming the base for many software development projects (especially on UNIX systems). |
---|
[07bc165] | 191 | The TIOBE index (\url{http://www.tiobe.com/tiobe_index}) for March 2016 shows programming-language popularity, with \Index*{Java} 20.5\%, C 14.5\%, \Index*[C++]{\CC} 6.7\%, \CS 4.3\%, \Index*{Python} 4.3\%, and all other programming languages below 3\%. |
---|
[45576af9] | 192 | As well, for 30 years, C has been the number 1 and 2 most popular programming language: |
---|
| 193 | \begin{center} |
---|
| 194 | \setlength{\tabcolsep}{1.5ex} |
---|
| 195 | \begin{tabular}{@{}r|c|c|c|c|c|c|c@{}} |
---|
| 196 | Ranking & 2016 & 2011 & 2006 & 2001 & 1996 & 1991 & 1986 \\ |
---|
| 197 | \hline |
---|
| 198 | Java & 1 & 1 & 1 & 3 & 29 & - & - \\ |
---|
| 199 | \hline |
---|
| 200 | \R{C} & \R{2} & \R{2} & \R{2} & \R{1} & \R{1} & \R{1} & \R{1} \\ |
---|
| 201 | \hline |
---|
| 202 | \CC & 3 & 3 & 3 & 2 & 2 & 2 & 7 \\ |
---|
| 203 | \end{tabular} |
---|
| 204 | \end{center} |
---|
[07bc165] | 205 | Hence, C is still an extremely important programming language, with double the usage of \Index*[C++]{\CC}, where \CC itself is largely C code. |
---|
[6c91065] | 206 | Finally, love it or hate it, C has been an important and influential part of computer science for 40 years and it appears it will continue to be for many more years. |
---|
| 207 | Unfortunately, C has too many problems and omissions to make it an acceptable programming language for modern needs. |
---|
| 208 | |
---|
| 209 | The goal of this project is to engineer modern language features into C in an evolutionary rather than revolutionary way. |
---|
| 210 | \CC~\cite{c++,ANSI14:C++} is an example of a similar project; |
---|
[45576af9] | 211 | however, it largely extended the language, and did not address many existing problems.\footnote{% |
---|
[0638c44] | 212 | 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.} |
---|
[c45170a] | 213 | \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. |
---|
[07bc165] | 214 | \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. |
---|
[6c91065] | 215 | These languages have different syntax and semantics from C, and do not interoperate directly with C, largely because of garbage collection. |
---|
[b52d900] | 216 | As a result, there is a significant learning curve to move to these languages, and C legacy-code must be rewritten. |
---|
[45576af9] | 217 | These costs can be prohibitive for many companies with a large software base in C/\CC, and a significant number of programmers requiring retraining to a new programming language. |
---|
[6c91065] | 218 | |
---|
[45576af9] | 219 | The result of this project is a language that is largely backwards compatible with C11~\cite{C11}, but fixing some of the well known C problems and containing many modern language features. |
---|
| 220 | Without significant extension to the C programming language, it is becoming unable to cope with the needs of modern programming problems and programmers; |
---|
[6c91065] | 221 | as a result, it will fade into disuse. |
---|
| 222 | Considering the large body of existing C code and programmers, there is significant impetus to ensure C is transformed into a modern programming language. |
---|
| 223 | While C11 made a few simple extensions to the language, nothing was added to address existing problems in the language or to augment the language with modern language features. |
---|
| 224 | While some may argue that modern language features may make C complex and inefficient, it is clear a language without modern capabilities is insufficient for the advanced programming problems existing today. |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | \section{Interoperability} |
---|
[e80ebe5] | 228 | \label{s:Interoperability} |
---|
[6c91065] | 229 | |
---|
| 230 | \CFA is designed to integrate well with existing C programs and libraries. |
---|
| 231 | The most important feature of interoperability is to use the same calling conventions, so there is no overhead to call existing C routines. |
---|
| 232 | This feature allows users of \CFA to take advantage of the existing panoply of C libraries from inside their \CFA code. |
---|
| 233 | In fact, one of the biggest issues for any new programming language is establishing a minimum level of library code to support a large body of activities. |
---|
[45576af9] | 234 | Language developers often state that adequate library support takes more work than designing and implementing the language itself. |
---|
[07bc165] | 235 | Like \Index*[C++]{\CC}, \CFA starts with immediate access to all exiting C libraries, and in many cases, can easily wrap library routines with simpler and safer interfaces, at very low cost. |
---|
[45576af9] | 236 | Hence, \CFA begins by leveraging the large repository of C libraries with little cost. |
---|
[6c91065] | 237 | |
---|
| 238 | However, it is necessary to differentiate between C and \CFA code because of name overloading, as for \CC. |
---|
[45576af9] | 239 | 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©. |
---|
| 240 | Whereas, \CFA wraps each of these routines into ones with the common name ©abs©: |
---|
[6c91065] | 241 | \begin{lstlisting} |
---|
[b52d900] | 242 | char abs( char ); |
---|
[6c91065] | 243 | extern "C" { |
---|
[07bc165] | 244 | int abs( int ); §\C{// use default C routine for int}§ |
---|
[6c91065] | 245 | } // extern "C" |
---|
[b52d900] | 246 | long int abs( long int ); |
---|
[6c91065] | 247 | long long int abs( long long int ); |
---|
| 248 | float abs( float ); |
---|
| 249 | double abs( double ); |
---|
| 250 | long double abs( long double ); |
---|
| 251 | float _Complex abs( float _Complex ); |
---|
| 252 | double _Complex abs( double _Complex ); |
---|
| 253 | long double _Complex abs( long double _Complex ); |
---|
| 254 | \end{lstlisting} |
---|
[0638c44] | 255 | The problem is the name clash between the library routine ©abs© and the \CFA names ©abs©. |
---|
[07bc165] | 256 | Hence, names appearing in an ©extern "C"© block have \newterm*{C linkage}. |
---|
| 257 | 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. |
---|
[6c91065] | 258 | 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. |
---|
| 259 | There is no way around this problem, other than C's approach of creating unique names for each pairing of operation and type. |
---|
| 260 | This example strongly illustrates a core idea in \CFA: \emph{the power of a name}. |
---|
[0638c44] | 261 | The name ``©abs©'' evokes the notion of absolute value, and many mathematical types provide the notion of absolute value. |
---|
| 262 | Hence, knowing the name ©abs© should be sufficient to apply it to any type where it is applicable. |
---|
[b52d900] | 263 | The time savings and safety of using one name uniformly versus $N$ unique names should not be underestimated. |
---|
[6c91065] | 264 | |
---|
| 265 | |
---|
[b72bad4f] | 266 | \section[Compiling CFA Program]{Compiling \CFA Program} |
---|
[6c91065] | 267 | |
---|
[c45170a] | 268 | The command ©cfa© is used to compile \CFA program(s), and is based on the GNU \Indexc{gcc} command, \eg: |
---|
[6c91065] | 269 | \begin{lstlisting} |
---|
[0638c44] | 270 | cfa§\indexc{cfa}\index{compilation!cfa@©cfa©}§ [ gcc-options ] C/§\CFA§-files [ assembler/loader-files ] |
---|
[6c91065] | 271 | \end{lstlisting} |
---|
[07bc165] | 272 | \CFA programs having the following ©gcc© flags turned on: |
---|
[53ba273] | 273 | \begin{description} |
---|
[07bc165] | 274 | \item |
---|
| 275 | \Indexc{-std=gnu99}\index{compilation option!-std=gnu99@{©-std=gnu99©}} |
---|
[53ba273] | 276 | The 1999 C standard plus GNU extensions. |
---|
[07bc165] | 277 | \item |
---|
| 278 | \Indexc{-fgnu89-inline}\index{compilation option!-fgnu89-inline@{©-fgnu89-inline©}} |
---|
[e229c22] | 279 | Use the traditional GNU semantics for inline routines in C99 mode, which allows inline routines in header files. |
---|
[53ba273] | 280 | \end{description} |
---|
[07bc165] | 281 | The following new \CFA options are available: |
---|
[6c91065] | 282 | \begin{description} |
---|
[07bc165] | 283 | \item |
---|
| 284 | \Indexc{-CFA}\index{compilation option!-CFA@©-CFA©} |
---|
[6c91065] | 285 | 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. |
---|
[07bc165] | 286 | The generated code started with the standard \CFA prelude. |
---|
| 287 | |
---|
| 288 | \item |
---|
| 289 | \Indexc{-debug}\index{compilation option!-debug@©-debug©} |
---|
| 290 | The program is linked with the debugging version of the runtime system. |
---|
| 291 | The debug version performs runtime checks to help during the debugging phase of a \CFA program, but substantially slows the execution of the program. |
---|
| 292 | The runtime checks should only be removed after the program is completely debugged. |
---|
| 293 | \textbf{This option is the default.} |
---|
| 294 | |
---|
| 295 | \item |
---|
| 296 | \Indexc{-nodebug}\index{compilation option!-nodebug@©-nodebug©} |
---|
[5479e63] | 297 | The program is linked with the non-debugging version of the runtime system, so the execution of the program is faster. |
---|
[07bc165] | 298 | \Emph{However, no runtime checks or ©assert©s are performed so errors usually result in abnormal program termination.} |
---|
| 299 | |
---|
| 300 | \item |
---|
| 301 | \Indexc{-help}\index{compilation option!-help@©-help©} |
---|
| 302 | Information about the set of \CFA compilation flags is printed. |
---|
| 303 | |
---|
| 304 | \item |
---|
| 305 | \Indexc{-nohelp}\index{compilation option!-nohelp@©-nohelp©} |
---|
| 306 | Information about the set of \CFA compilation flags is not printed. |
---|
| 307 | \textbf{This option is the default.} |
---|
| 308 | |
---|
| 309 | \item |
---|
| 310 | \Indexc{-quiet}\index{compilation option!-quiet@©-quiet©} |
---|
| 311 | The \CFA compilation message is not printed at the beginning of a compilation. |
---|
| 312 | |
---|
| 313 | \item |
---|
| 314 | \Indexc{-noquiet}\index{compilation option!-noquiet@©-noquiet©} |
---|
| 315 | The \CFA compilation message is printed at the beginning of a compilation. |
---|
| 316 | \textbf{This option is the default.} |
---|
[e80ebe5] | 317 | |
---|
| 318 | \item |
---|
| 319 | \Indexc{-no-include-std}\index{compilation option!-no-include-std@©-no-include-std©} |
---|
| 320 | Do not supply ©extern "C"© wrappers for \Celeven standard include files (see~\VRef{s:StandardHeaders}). |
---|
| 321 | \textbf{This option is \emph{not} the default.} |
---|
[6c91065] | 322 | \end{description} |
---|
| 323 | |
---|
[6b6597c] | 324 | The following preprocessor variables are available: |
---|
| 325 | \begin{description} |
---|
[07bc165] | 326 | \item |
---|
| 327 | \Indexc{__CFA__}\index{preprocessor variables!__CFA__@{©__CFA__©}} |
---|
[6b6597c] | 328 | is always available during preprocessing and its value is the current major \Index{version number} of \CFA.\footnote{ |
---|
| 329 | The C preprocessor allows only integer values in a preprocessor variable so a value like ``\Version'' is not allowed. |
---|
| 330 | Hence, the need to have three variables for the major, minor and patch version number.} |
---|
| 331 | |
---|
[07bc165] | 332 | \item |
---|
| 333 | \Indexc{__CFA_MINOR__}\index{preprocessor variables!__CFA_MINOR__@{©__CFA_MINOR__©}} |
---|
[6b6597c] | 334 | is always available during preprocessing and its value is the current minor \Index{version number} of \CFA. |
---|
| 335 | |
---|
[07bc165] | 336 | \item |
---|
| 337 | \Indexc{__CFA_PATCH__}\index{preprocessor variables!__CFA_PATCH__@©__CFA_PATCH__©} |
---|
[6b6597c] | 338 | is always available during preprocessing and its value is the current patch \Index{version number} of \CFA. |
---|
| 339 | |
---|
[07bc165] | 340 | \item |
---|
| 341 | \Indexc{__CFORALL__}\index{preprocessor variables!__CFORALL__@©__CFORALL__©} |
---|
| 342 | is always available during preprocessing and has no value. |
---|
[6b6597c] | 343 | \end{description} |
---|
| 344 | These preprocessor variables allow conditional compilation of programs that must work differently in these situations. |
---|
| 345 | For example, to toggle between C and \CFA extensions, using the following: |
---|
| 346 | \begin{lstlisting} |
---|
| 347 | #ifndef __CFORALL__ |
---|
[4096de0] | 348 | #include <stdio.h> §\C{// C header file}§ |
---|
[6b6597c] | 349 | #else |
---|
[4096de0] | 350 | #include <fstream> §\C{// \CFA header file}§ |
---|
[6b6597c] | 351 | #endif |
---|
| 352 | \end{lstlisting} |
---|
[07bc165] | 353 | which conditionally includes the correct header file, if the program is compiled using \Indexc{gcc} or \Indexc{cfa}. |
---|
[6b6597c] | 354 | |
---|
[6c91065] | 355 | |
---|
| 356 | \section{Underscores in Constants} |
---|
| 357 | |
---|
[c45170a] | 358 | Numeric constants are extended to allow \Index{underscore}s within constants\index{constant!underscore}, \eg: |
---|
[6c91065] | 359 | \begin{lstlisting} |
---|
[4096de0] | 360 | 2®_®147®_®483®_®648; §\C{// decimal constant}§ |
---|
| 361 | 56_ul; §\C{// decimal unsigned long constant}§ |
---|
| 362 | 0_377; §\C{// octal constant}§ |
---|
| 363 | 0x_ff_ff; §\C{// hexadecimal constant}§ |
---|
| 364 | 0x_ef3d_aa5c; §\C{// hexadecimal constant}§ |
---|
| 365 | 3.141_592_654; §\C{// floating point constant}§ |
---|
| 366 | 10_e_+1_00; §\C{// floating point constant}§ |
---|
| 367 | 0x_ff_ff_p_3; §\C{// hexadecimal floating point}§ |
---|
| 368 | 0x_1.ffff_ffff_p_128_l; §\C{// hexadecimal floating point long constant}§ |
---|
| 369 | L_"\x_ff_ee"; §\C{// wide character constant}§ |
---|
[6c91065] | 370 | \end{lstlisting} |
---|
| 371 | The rules for placement of underscores is as follows: |
---|
| 372 | \begin{enumerate} |
---|
| 373 | \item |
---|
[c45170a] | 374 | A sequence of underscores is disallowed, \eg ©12__34© is invalid. |
---|
[6c91065] | 375 | \item |
---|
| 376 | Underscores may only appear within a sequence of digits (regardless of the digit radix). |
---|
[c45170a] | 377 | 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). |
---|
[6c91065] | 378 | \item |
---|
| 379 | A numeric prefix may end with an underscore; |
---|
| 380 | a numeric infix may begin and/or end with an underscore; |
---|
| 381 | a numeric suffix may begin with an underscore. |
---|
[0638c44] | 382 | For example, the octal ©0© or hexadecimal ©0x© prefix may end with an underscore ©0_377© or ©0x_ff©; |
---|
| 383 | the exponent infix ©E© may start or end with an underscore ©1.0_E10©, ©1.0E_10© or ©1.0_E_10©; |
---|
| 384 | the type suffixes ©U©, ©L©, etc. may start with an underscore ©1_U©, ©1_ll© or ©1.0E10_f©. |
---|
[6c91065] | 385 | \end{enumerate} |
---|
[b52d900] | 386 | It is significantly easier to read and enter long constants when they are broken up into smaller groupings (most cultures use comma or period among digits for the same purpose). |
---|
[07bc165] | 387 | This extension is backwards compatible, matches with the use of underscore in variable names, and appears in \Index*{Ada} and \Index*{Java} 8. |
---|
[6c91065] | 388 | |
---|
| 389 | |
---|
| 390 | \section{Declarations} |
---|
| 391 | \label{s:Declarations} |
---|
| 392 | |
---|
| 393 | C declaration syntax is notoriously confusing and error prone. |
---|
| 394 | For example, many C programmers are confused by a declaration as simple as: |
---|
[6b6597c] | 395 | \begin{quote2} |
---|
| 396 | \begin{tabular}{@{}ll@{}} |
---|
[45576af9] | 397 | \begin{lstlisting} |
---|
| 398 | int *x[5] |
---|
[6c91065] | 399 | \end{lstlisting} |
---|
[6b6597c] | 400 | & |
---|
| 401 | \raisebox{-0.75\totalheight}{\input{Cdecl}} |
---|
| 402 | \end{tabular} |
---|
| 403 | \end{quote2} |
---|
[07bc165] | 404 | Is this an array of 5 pointers to integers or a \Index{pointer} to an array of 5 integers? |
---|
[4096de0] | 405 | The fact this declaration is unclear to many C programmers means there are \Index{productivity} and \Index{safety} issues even for basic programs. |
---|
[6c91065] | 406 | Another example of confusion results from the fact that a routine name and its parameters are embedded within the return type, mimicking the way the return value is used at the routine's call site. |
---|
[07bc165] | 407 | For example, a routine returning a \Index{pointer} to an array of integers is defined and used in the following way: |
---|
[6c91065] | 408 | \begin{lstlisting} |
---|
[4096de0] | 409 | int (*f())[5] {...}; §\C{}§ |
---|
[45576af9] | 410 | ... (*f())[3] += 1; |
---|
[6c91065] | 411 | \end{lstlisting} |
---|
| 412 | Essentially, the return type is wrapped around the routine name in successive layers (like an onion). |
---|
[4096de0] | 413 | While attempting to make the two contexts consistent is a laudable goal, it has not worked out in practice. |
---|
[6c91065] | 414 | |
---|
[45576af9] | 415 | \CFA provides its own type, variable and routine declarations, using a different syntax. |
---|
| 416 | The new declarations place qualifiers to the left of the base type, while C declarations place qualifiers to the right of the base type. |
---|
| 417 | In the following example, \R{red} is for the base type and \B{blue} is for the qualifiers. |
---|
[07bc165] | 418 | The \CFA declarations move the qualifiers to the left of the base type, i.e., move the blue to the left of the red, while the qualifiers have the same meaning but are ordered left to right to specify a variable's type. |
---|
[45576af9] | 419 | \begin{quote2} |
---|
| 420 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 421 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
| 422 | \begin{lstlisting} |
---|
| 423 | ß[5] *ß ®int® x1; |
---|
| 424 | ß* [5]ß ®int® x2; |
---|
| 425 | ß[* [5] int]ß f®( int p )®; |
---|
| 426 | \end{lstlisting} |
---|
| 427 | & |
---|
| 428 | \begin{lstlisting} |
---|
| 429 | ®int® ß*ß x1 ß[5]ß; |
---|
| 430 | ®int® ß(*ßx2ß)[5]ß; |
---|
| 431 | ßint (*ßf®( int p )®ß)[5]ß; |
---|
| 432 | \end{lstlisting} |
---|
| 433 | \end{tabular} |
---|
| 434 | \end{quote2} |
---|
[6c91065] | 435 | The only exception is bit field specification, which always appear to the right of the base type. |
---|
[45576af9] | 436 | % 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. |
---|
| 437 | However, unlike C, \CFA type declaration tokens are distributed across all variables in the declaration list. |
---|
[07bc165] | 438 | For instance, variables ©x© and ©y© of type \Index{pointer} to integer are defined in \CFA as follows: |
---|
[6c91065] | 439 | \begin{quote2} |
---|
[45576af9] | 440 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 441 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
[6c91065] | 442 | \begin{lstlisting} |
---|
[45576af9] | 443 | ®*® int x, y; |
---|
[6c91065] | 444 | \end{lstlisting} |
---|
| 445 | & |
---|
| 446 | \begin{lstlisting} |
---|
[45576af9] | 447 | int ®*®x, ®*®y; |
---|
[6c91065] | 448 | \end{lstlisting} |
---|
| 449 | \end{tabular} |
---|
| 450 | \end{quote2} |
---|
[07bc165] | 451 | The downside of this semantics is the need to separate regular and \Index{pointer} declarations: |
---|
[4096de0] | 452 | \begin{quote2} |
---|
| 453 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 454 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
| 455 | \begin{lstlisting} |
---|
| 456 | ®*® int x; |
---|
| 457 | int y; |
---|
| 458 | \end{lstlisting} |
---|
| 459 | & |
---|
| 460 | \begin{lstlisting} |
---|
| 461 | int ®*®x, y; |
---|
| 462 | |
---|
| 463 | \end{lstlisting} |
---|
| 464 | \end{tabular} |
---|
| 465 | \end{quote2} |
---|
| 466 | which is \Index{prescribing} a safety benefit. |
---|
[6c91065] | 467 | Other examples are: |
---|
| 468 | \begin{quote2} |
---|
[45576af9] | 469 | \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} |
---|
| 470 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ |
---|
[6c91065] | 471 | \begin{lstlisting} |
---|
[6b6597c] | 472 | [ 5 ] int z; |
---|
| 473 | [ 5 ] * char w; |
---|
| 474 | * [ 5 ] double v; |
---|
[6c91065] | 475 | struct s { |
---|
[b52d900] | 476 | int f0:3; |
---|
[6c91065] | 477 | * int f1; |
---|
[6b6597c] | 478 | [ 5 ] * int f2; |
---|
[6c91065] | 479 | }; |
---|
| 480 | \end{lstlisting} |
---|
| 481 | & |
---|
| 482 | \begin{lstlisting} |
---|
[6b6597c] | 483 | int z[ 5 ]; |
---|
| 484 | char *w[ 5 ]; |
---|
| 485 | double (*v)[ 5 ]; |
---|
[6c91065] | 486 | struct s { |
---|
| 487 | int f0:3; |
---|
| 488 | int *f1; |
---|
[6b6597c] | 489 | int *f2[ 5 ] |
---|
[6c91065] | 490 | }; |
---|
| 491 | \end{lstlisting} |
---|
| 492 | & |
---|
| 493 | \begin{lstlisting} |
---|
[6b6597c] | 494 | // array of 5 integers |
---|
| 495 | // array of 5 pointers to char |
---|
| 496 | // pointer to array of 5 doubles |
---|
[6c91065] | 497 | |
---|
| 498 | // common bit field syntax |
---|
| 499 | |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | \end{lstlisting} |
---|
| 503 | \end{tabular} |
---|
| 504 | \end{quote2} |
---|
| 505 | |
---|
[c45170a] | 506 | All type qualifiers, \eg ©const©, ©volatile©, etc., are used in the normal way with the new declarations and also appear left to right, \eg: |
---|
[6c91065] | 507 | \begin{quote2} |
---|
[45576af9] | 508 | \begin{tabular}{@{}l@{\hspace{1em}}l@{\hspace{1em}}l@{}} |
---|
| 509 | \multicolumn{1}{c@{\hspace{1em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{1em}}}{\textbf{C}} \\ |
---|
[6c91065] | 510 | \begin{lstlisting} |
---|
| 511 | const * const int x; |
---|
[6b6597c] | 512 | const * [ 5 ] const int y; |
---|
[6c91065] | 513 | \end{lstlisting} |
---|
| 514 | & |
---|
| 515 | \begin{lstlisting} |
---|
| 516 | int const * const x; |
---|
[6b6597c] | 517 | const int (* const y)[ 5 ] |
---|
[6c91065] | 518 | \end{lstlisting} |
---|
| 519 | & |
---|
| 520 | \begin{lstlisting} |
---|
| 521 | // const pointer to const integer |
---|
[6b6597c] | 522 | // const pointer to array of 5 const integers |
---|
[6c91065] | 523 | \end{lstlisting} |
---|
| 524 | \end{tabular} |
---|
| 525 | \end{quote2} |
---|
[c45170a] | 526 | 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} |
---|
| 527 | 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: |
---|
[6c91065] | 528 | \begin{quote2} |
---|
[45576af9] | 529 | \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} |
---|
| 530 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ |
---|
[6c91065] | 531 | \begin{lstlisting} |
---|
[6b6597c] | 532 | extern [ 5 ] int x; |
---|
[6c91065] | 533 | static * const int y; |
---|
| 534 | \end{lstlisting} |
---|
| 535 | & |
---|
| 536 | \begin{lstlisting} |
---|
[6b6597c] | 537 | int extern x[ 5 ]; |
---|
[6c91065] | 538 | const int static *y; |
---|
| 539 | \end{lstlisting} |
---|
| 540 | & |
---|
| 541 | \begin{lstlisting} |
---|
[6b6597c] | 542 | // externally visible array of 5 integers |
---|
[6c91065] | 543 | // internally visible pointer to constant int |
---|
| 544 | \end{lstlisting} |
---|
| 545 | \end{tabular} |
---|
| 546 | \end{quote2} |
---|
| 547 | |
---|
[9724df0] | 548 | Unsupported are K\&R C declarations where the base type defaults to ©int©, if no type is specified,\footnote{ |
---|
| 549 | At least one type specifier shall be given in the declaration specifiers in each declaration, and in the specifier-qualifier list in each structure declaration and type name~\cite[\S~6.7.2(2)]{C11}} |
---|
[c45170a] | 550 | \eg: |
---|
[b52d900] | 551 | \begin{lstlisting} |
---|
[4096de0] | 552 | x; §\C{// int x}§ |
---|
| 553 | *y; §\C{// int *y}§ |
---|
| 554 | f( p1, p2 ); §\C{// int f( int p1, int p2 );}§ |
---|
| 555 | f( p1, p2 ) {} §\C{// int f( int p1, int p2 ) {}}§ |
---|
[b52d900] | 556 | \end{lstlisting} |
---|
| 557 | |
---|
[45576af9] | 558 | Finally, new \CFA declarations may appear together with C declarations in the same program block, but cannot be mixed within a specific declaration. |
---|
[b52d900] | 559 | Therefore, a programmer has the option of either continuing to use traditional C declarations or take advantage of the new style. |
---|
| 560 | Clearly, both styles need to be supported for some time due to existing C-style header-files, particularly for UNIX systems. |
---|
| 561 | |
---|
[6c91065] | 562 | |
---|
[4096de0] | 563 | \section{Pointer / Reference} |
---|
| 564 | |
---|
| 565 | C provides a \newterm{pointer type}; |
---|
| 566 | \CFA adds a \newterm{reference type}. |
---|
| 567 | Both types contain an \newterm{address}, which is normally a location in memory. |
---|
| 568 | Special addresses are used to denote certain states or access co-processor memory. |
---|
| 569 | By convention, no variable is placed at address 0, so addresses like 0, 1, 2, 3 are often used to denote no-value or other special states. |
---|
[a17e7b8] | 570 | Often dereferencing a special state causes a \Index{memory fault}, so checking is necessary during execution. |
---|
[4096de0] | 571 | If the programming language assigns addresses, a program's execution is \Index{sound}, i.e., all addresses are to valid memory locations. |
---|
| 572 | C allows programmers to assign addresses, so there is the potential for incorrect addresses, both inside and outside of the computer address-space. |
---|
[9724df0] | 573 | |
---|
| 574 | Program variables are implicit pointers to memory locations generated by the compiler and automatically dereferenced, as in: |
---|
| 575 | \begin{quote2} |
---|
[4096de0] | 576 | \begin{tabular}{@{}lll@{}} |
---|
[9724df0] | 577 | \begin{lstlisting} |
---|
[4096de0] | 578 | int x; |
---|
[9724df0] | 579 | x = 3; |
---|
[4096de0] | 580 | int y; |
---|
[9724df0] | 581 | y = x; |
---|
| 582 | \end{lstlisting} |
---|
| 583 | & |
---|
[4096de0] | 584 | \raisebox{-0.45\totalheight}{\input{pointer1}} |
---|
| 585 | & |
---|
[9724df0] | 586 | \begin{lstlisting} |
---|
[a17e7b8] | 587 | int * ®const® x = (int *)100 |
---|
[4096de0] | 588 | *x = 3; // implicit dereference |
---|
[a17e7b8] | 589 | int * ®const® y = (int *)104; |
---|
[4096de0] | 590 | *y = *x; // implicit dereference |
---|
[9724df0] | 591 | \end{lstlisting} |
---|
| 592 | \end{tabular} |
---|
| 593 | \end{quote2} |
---|
[07bc165] | 594 | where the right example is how the compiler logically interprets the variables in the left example. |
---|
| 595 | Since a variable name only points to one location during its lifetime, it is an \Index{immutable} \Index{pointer}; |
---|
[4096de0] | 596 | hence, variables ©x© and ©y© are constant pointers in the compiler interpretation. |
---|
| 597 | In general, variable addresses are stored in instructions instead of loaded independently, so an instruction fetch implicitly loads a variable's address. |
---|
[9724df0] | 598 | \begin{quote2} |
---|
| 599 | \begin{tabular}{@{}l|l@{}} |
---|
| 600 | \begin{lstlisting} |
---|
[4096de0] | 601 | lda r1,100 // load address of x |
---|
| 602 | ld r2,(r1) // load value of x |
---|
| 603 | lda r3,104 // load address of y |
---|
| 604 | st r2,(r3) // store x into y |
---|
[9724df0] | 605 | \end{lstlisting} |
---|
| 606 | & |
---|
| 607 | \begin{lstlisting} |
---|
[4096de0] | 608 | |
---|
| 609 | ld r2,(100) // load value of x |
---|
| 610 | |
---|
| 611 | st r2,(104) // store x into y |
---|
[9724df0] | 612 | \end{lstlisting} |
---|
| 613 | \end{tabular} |
---|
| 614 | \end{quote2} |
---|
[07bc165] | 615 | Finally, the immutable nature of a variable's address and the fact that there is no storage for a variable address means pointer assignment\index{pointer!assignment}\index{assignment!pointer} is impossible. |
---|
[4096de0] | 616 | Therefore, the expression ©x = y© only has one meaning, ©*x = *y©, i.e., manipulate values, which is why explicitly writing the dereferences is unnecessary even though it occurs implicitly as part of instruction decoding. |
---|
[9724df0] | 617 | |
---|
[07bc165] | 618 | A \Index{pointer}/\Index{reference} is a generalization of a variable name, i.e., a mutable address that can point to more than one memory location during its lifetime. |
---|
[4096de0] | 619 | (Similarly, an integer variable can contain multiple integer literals during its lifetime versus an integer constant representing a single literal during its lifetime and may not occupy storage as the literal is embedded directly into instructions.) |
---|
[c45170a] | 620 | Hence, a pointer occupies memory to store its current address, and the pointer's value is loaded by dereferencing, \eg: |
---|
[4096de0] | 621 | \begin{quote2} |
---|
| 622 | \begin{tabular}{@{}ll@{}} |
---|
[9724df0] | 623 | \begin{lstlisting} |
---|
[4096de0] | 624 | int x, y, ®*® p1, ®*® p2, ®**® p3; |
---|
| 625 | p1 = ®&®x; // p1 points to x |
---|
[07bc165] | 626 | p2 = p1; // p2 points to x |
---|
[4096de0] | 627 | p1 = ®&®y; // p1 points to y |
---|
| 628 | p3 = &p2; // p3 points to p2 |
---|
[9724df0] | 629 | \end{lstlisting} |
---|
[4096de0] | 630 | & |
---|
| 631 | \raisebox{-0.45\totalheight}{\input{pointer2.pstex_t}} |
---|
| 632 | \end{tabular} |
---|
| 633 | \end{quote2} |
---|
[9724df0] | 634 | |
---|
[4096de0] | 635 | Notice, an address has a duality\index{address!duality}: a location in memory or the value at that location. |
---|
[07bc165] | 636 | In many cases, a compiler might be able to infer the meaning: |
---|
[9724df0] | 637 | \begin{lstlisting} |
---|
[a17e7b8] | 638 | p2 = p1 + x; §\C{// compiler infers *p2 = *p1 + x;}§ |
---|
[9724df0] | 639 | \end{lstlisting} |
---|
[4096de0] | 640 | because adding the arbitrary integer value in ©x© to the address of ©p1© and storing the resulting address into ©p2© is an unlikely operation. |
---|
[07bc165] | 641 | \Index*{Algol68}~\cite{Algol68} inferences pointer dereferencing to select the best meaning for each pointer usage. |
---|
| 642 | However, in C, the following cases are ambiguous, especially with pointer arithmetic: |
---|
[9724df0] | 643 | \begin{lstlisting} |
---|
[a17e7b8] | 644 | p1 = p2; §\C{// p1 = p2\ \ or\ \ *p1 = *p2}§ |
---|
| 645 | p1 = p1 + 1; §\C{// p1 = p1 + 1\ \ or\ \ *p1 = *p1 + 1}§ |
---|
[9724df0] | 646 | \end{lstlisting} |
---|
| 647 | |
---|
[07bc165] | 648 | Most languages pick one meaning as the default and the programmer explicitly indicates the other meaning to resolve the address-duality ambiguity\index{address! ambiguity}. |
---|
[4096de0] | 649 | In C, the default meaning for pointers is to manipulate the pointer's address and the pointed-to value is explicitly accessed by the dereference operator ©*©. |
---|
[9724df0] | 650 | \begin{lstlisting} |
---|
[a17e7b8] | 651 | p1 = p2; §\C{// pointer address assignment}§ |
---|
| 652 | *p1 = *p1 + 1; §\C{// pointed-to value assignment / operation}§ |
---|
[9724df0] | 653 | \end{lstlisting} |
---|
[07bc165] | 654 | which works well for situations where manipulation of addresses is the primary meaning and data is rarely accessed, such as storage management (©malloc©/©free©). |
---|
[9724df0] | 655 | |
---|
[4096de0] | 656 | However, in most other situations, the pointed-to value is requested more often than the pointer address. |
---|
[9724df0] | 657 | \begin{lstlisting} |
---|
[4096de0] | 658 | *p2 = ((*p1 + *p2) * (**p3 - *p1)) / (**p3 - 15); |
---|
[9724df0] | 659 | \end{lstlisting} |
---|
[4096de0] | 660 | In this case, it is tedious to explicitly write the dereferencing, and error prone when pointer arithmetic is allowed. |
---|
| 661 | It is better to have the compiler generate the dereferencing and have no implicit pointer arithmetic: |
---|
[9724df0] | 662 | \begin{lstlisting} |
---|
[4096de0] | 663 | p2 = ((p1 + p2) * (p3 - p1)) / (p3 - 15); |
---|
[9724df0] | 664 | \end{lstlisting} |
---|
| 665 | |
---|
[a17e7b8] | 666 | To switch the default meaning for an address requires a new kind of pointer, called a \newterm{reference} denoted by ©&©. |
---|
[4096de0] | 667 | \begin{lstlisting} |
---|
| 668 | int x, y, ®&® r1, ®&® r2, ®&&® r3; |
---|
[a17e7b8] | 669 | ®&®r1 = &x; §\C{// r1 points to x}§ |
---|
[07bc165] | 670 | ®&®r2 = &r1; §\C{// r2 points to x}§ |
---|
| 671 | ®&®r1 = &y; §\C{// r1 points to y}§ |
---|
| 672 | ®&&®r3 = ®&®&r2; §\C{// r3 points to r2}§ |
---|
[4096de0] | 673 | r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); §\C{// implicit dereferencing}§ |
---|
| 674 | \end{lstlisting} |
---|
[a17e7b8] | 675 | Except for auto-dereferencing by the compiler, this reference example is the same as the previous pointer example. |
---|
[4096de0] | 676 | Hence, a reference behaves like the variable name for the current variable it is pointing-to. |
---|
[c45170a] | 677 | The simplest way to understand a reference is to imagine the compiler inserting a dereference operator before the reference variable for each reference qualifier in a declaration, \eg: |
---|
[9724df0] | 678 | \begin{lstlisting} |
---|
[4096de0] | 679 | r2 = ((r1 + r2) * (r3 - r1)) / (r3 - 15); |
---|
[9724df0] | 680 | \end{lstlisting} |
---|
[4096de0] | 681 | is rewritten as: |
---|
| 682 | \begin{lstlisting} |
---|
| 683 | ®*®r2 = ((®*®r1 + ®*®r2) ®*® (®**®r3 - ®*®r1)) / (®**®r3 - 15); |
---|
| 684 | \end{lstlisting} |
---|
[c45170a] | 685 | When a reference operation appears beside a dereference operation, \eg ©&*©, they cancel out.\footnote{ |
---|
[4096de0] | 686 | The unary ©&© operator yields the address of its operand. |
---|
| 687 | If the operand has type ``type'', the result has type ``pointer to type''. |
---|
| 688 | 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}} |
---|
| 689 | Hence, assigning to a reference requires the address of the reference variable (\Index{lvalue}): |
---|
| 690 | \begin{lstlisting} |
---|
[07bc165] | 691 | (&®*®)r1 = &x; §\C{// (\&*) cancel giving variable r1 not variable pointed-to by r1}§ |
---|
[4096de0] | 692 | \end{lstlisting} |
---|
| 693 | Similarly, the address of a reference can be obtained for assignment or computation (\Index{rvalue}): |
---|
| 694 | \begin{lstlisting} |
---|
[07bc165] | 695 | (&(&®*®)®*®)r3 = &(&®*®)r2; §\C{// (\&*) cancel giving address of r2, (\&(\&*)*) cancel giving variable r3}§ |
---|
[4096de0] | 696 | \end{lstlisting} |
---|
[07bc165] | 697 | Cancellation\index{cancellation!pointer/reference}\index{pointer!cancellation} works to arbitrary depth, and pointer and reference values are interchangeable because both contain addresses. |
---|
[4096de0] | 698 | \begin{lstlisting} |
---|
| 699 | int x, *p1 = &x, **p2 = &p1, ***p3 = &p2, |
---|
[07bc165] | 700 | &r1 = x, &&r2 = r1, &&&r3 = r2; |
---|
[a17e7b8] | 701 | ***p3 = 3; §\C{// change x}§ |
---|
| 702 | r3 = 3; §\C{// change x, ***r3}§ |
---|
| 703 | **p3 = ...; §\C{// change p1}§ |
---|
| 704 | &r3 = ...; §\C{// change r1, (\&*)**r3, 1 cancellation}§ |
---|
| 705 | *p3 = ...; §\C{// change p2}§ |
---|
| 706 | &&r3 = ...; §\C{// change r2, (\&(\&*)*)*r3, 2 cancellations}§ |
---|
| 707 | &&&r3 = p3; §\C{// change r3 to p3, (\&(\&(\&*)*)*)r3, 3 cancellations}§ |
---|
[4096de0] | 708 | \end{lstlisting} |
---|
| 709 | Finally, implicit dereferencing and cancellation are a static (compilation) phenomenon not a dynamic one. |
---|
| 710 | That is, all implicit dereferencing and any cancellation is carried out prior to the start of the program, so reference performance is equivalent to pointer performance. |
---|
[07bc165] | 711 | A programmer selects a pointer or reference type solely on whether the address is dereferenced frequently or infrequently, which dictates the amount of direct aid from the compiler; |
---|
[4096de0] | 712 | otherwise, everything else is equal. |
---|
[9724df0] | 713 | |
---|
[07bc165] | 714 | Interestingly, \Index*[C++]{\CC} deals with the address duality by making the pointed-to value the default, and prevent\-ing changes to the reference address, which eliminates half of the duality. |
---|
| 715 | \Index*{Java} deals with the address duality by making address assignment the default and requiring field assignment (direct or indirect via methods), i.e., there is no builtin bit-wise or method-wise assignment, which eliminates half of the duality. |
---|
[4096de0] | 716 | |
---|
[a17e7b8] | 717 | As for a pointer, a reference may have qualifiers: |
---|
[9724df0] | 718 | \begin{lstlisting} |
---|
[a17e7b8] | 719 | const int cx = 5; §\C{// cannot change cx;}§ |
---|
[07bc165] | 720 | const int & cr = cx; §\C{// cannot change what cr points to}§ |
---|
| 721 | ®&®cr = &cx; §\C{// can change cr}§ |
---|
| 722 | cr = 7; §\C{// error, cannot change cx}§ |
---|
| 723 | int & const rc = x; §\C{// must be initialized, \CC reference}§ |
---|
| 724 | ®&®rc = &x; §\C{// error, cannot change rc}§ |
---|
| 725 | const int & const crc = cx; §\C{// must be initialized, \CC reference}§ |
---|
| 726 | crc = 7; §\C{// error, cannot change cx}§ |
---|
| 727 | ®&®crc = &cx; §\C{// error, cannot change crc}§ |
---|
| 728 | \end{lstlisting} |
---|
[c45170a] | 729 | Hence, for type ©& const©, there is no pointer assignment, so ©&rc = &x© is disallowed, and \emph{the address value cannot be ©0© unless an arbitrary pointer is assigned to the reference}, \eg: |
---|
[5479e63] | 730 | \begin{lstlisting} |
---|
| 731 | int & const r = *0; §\C{// where 0 is the int * zero}§ |
---|
| 732 | \end{lstlisting} |
---|
| 733 | Otherwise, the compiler is managing the addresses for type ©& const© not the programmer, and by a programming discipline of only using references with references, address errors can be prevented. |
---|
[c45170a] | 734 | Finally, the position of the ©const© qualifier \emph{after} the pointer/reference qualifier causes confuse for C programmers. |
---|
| 735 | The ©const© qualifier cannot be moved before the pointer/reference qualifier for C style-declarations; |
---|
| 736 | \CFA-style declarations attempt to address this issue: |
---|
| 737 | \begin{quote2} |
---|
| 738 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 739 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
| 740 | \begin{lstlisting} |
---|
| 741 | ®const® * ®const® * const int ccp; |
---|
| 742 | ®const® & ®const® & const int ccr; |
---|
| 743 | \end{lstlisting} |
---|
| 744 | & |
---|
| 745 | \begin{lstlisting} |
---|
| 746 | const int * ®const® * ®const® ccp; |
---|
| 747 | |
---|
| 748 | \end{lstlisting} |
---|
| 749 | \end{tabular} |
---|
| 750 | \end{quote2} |
---|
| 751 | where the \CFA declaration is read left-to-right (see \VRef{s:Declarations}). |
---|
[07bc165] | 752 | |
---|
| 753 | \Index{Initialization} is different than \Index{assignment} because initialization occurs on the empty (uninitialized) storage on an object, while assignment occurs on possibly initialized storage of an object. |
---|
[4096de0] | 754 | There are three initialization contexts in \CFA: declaration initialization, argument/parameter binding, return/temporary binding. |
---|
[07bc165] | 755 | For reference initialization (like pointer), the initializing value must be an address (\Index{lvalue}) not a value (\Index{rvalue}). |
---|
[9724df0] | 756 | \begin{lstlisting} |
---|
[a17e7b8] | 757 | int * p = &x; §\C{// both \&x and x are possible interpretations}§ |
---|
| 758 | int & r = x; §\C{// x unlikely interpretation, because of auto-dereferencing}§ |
---|
[9724df0] | 759 | \end{lstlisting} |
---|
[07bc165] | 760 | Hence, the compiler implicitly inserts a reference operator, ©&©, before the initialization expression. |
---|
[a17e7b8] | 761 | Similarly, when a reference is used for a parameter/return type, the call-site argument does not require a reference operator. |
---|
[9724df0] | 762 | \begin{lstlisting} |
---|
[07bc165] | 763 | int & f( int & rp ); §\C{// reference parameter and return}§ |
---|
[5479e63] | 764 | z = f( x ) + f( y ); §\C{// reference operator added, temporaries needed for call results}§ |
---|
[9724df0] | 765 | \end{lstlisting} |
---|
[07bc165] | 766 | Within routine ©f©, it is possible to change the argument by changing the corresponding parameter, and parameter ©rp© can be locally reassigned within ©f©. |
---|
[5479e63] | 767 | Since ©?+?© 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. |
---|
[9724df0] | 768 | |
---|
[4096de0] | 769 | When a pointer/reference parameter has a ©const© value (immutable), it is possible to pass literals and expressions. |
---|
[9724df0] | 770 | \begin{lstlisting} |
---|
[07bc165] | 771 | void f( ®const® int & crp ); |
---|
| 772 | void g( ®const® int * cpp ); |
---|
[a17e7b8] | 773 | f( 3 ); g( &3 ); |
---|
[4096de0] | 774 | f( x + y ); g( &(x + y) ); |
---|
[9724df0] | 775 | \end{lstlisting} |
---|
[4096de0] | 776 | 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. |
---|
[07bc165] | 777 | (The ©&© is necessary for the pointer parameter to make the types match, and is a common requirement for a C programmer.) |
---|
| 778 | \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. |
---|
[a17e7b8] | 779 | \begin{lstlisting} |
---|
[07bc165] | 780 | void f( int & rp ); |
---|
| 781 | void g( int * pp ); |
---|
[a17e7b8] | 782 | f( 3 ); g( &3 ); §\C{// compiler implicit generates temporaries}§ |
---|
| 783 | f( x + y ); g( &(x + y) ); §\C{// compiler implicit generates temporaries}§ |
---|
| 784 | \end{lstlisting} |
---|
[07bc165] | 785 | Essentially, there is an implicit \Index{rvalue} to \Index{lvalue} conversion in this case.\footnote{ |
---|
| 786 | 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.} |
---|
| 787 | The implicit conversion allows seamless calls to any routine without having to explicitly name/copy the literal/expression to allow the call. |
---|
[4096de0] | 788 | |
---|
[07bc165] | 789 | While \CFA attempts to handle pointers and references in a uniform, symmetric manner, C handles routine variables in an inconsistent way: a routine variable is both a pointer and a reference (particle and wave). |
---|
[4096de0] | 790 | \begin{lstlisting} |
---|
| 791 | void f( int p ) {...} |
---|
[a17e7b8] | 792 | void (*fp)( int ) = &f; §\C{// pointer initialization}§ |
---|
| 793 | void (*fp)( int ) = f; §\C{// reference initialization}§ |
---|
| 794 | (*fp)(3); §\C{// pointer invocation}§ |
---|
| 795 | fp(3); §\C{// reference invocation}§ |
---|
[4096de0] | 796 | \end{lstlisting} |
---|
| 797 | A routine variable is best described by a ©const© reference: |
---|
| 798 | \begin{lstlisting} |
---|
| 799 | const void (&fp)( int ) = f; |
---|
| 800 | fp( 3 ); |
---|
[07bc165] | 801 | fp = ... §\C{// error, cannot change code}§ |
---|
| 802 | &fp = ...; §\C{// changing routine reference}§ |
---|
[4096de0] | 803 | \end{lstlisting} |
---|
| 804 | because the value of the routine variable is a routine literal, i.e., the routine code is normally immutable during execution.\footnote{ |
---|
| 805 | Dynamic code rewriting is possible but only in special circumstances.} |
---|
[07bc165] | 806 | \CFA allows this additional use of references for routine variables in an attempt to give a more consistent meaning for them. |
---|
[4096de0] | 807 | |
---|
[9724df0] | 808 | |
---|
[6c91065] | 809 | \section{Type Operators} |
---|
| 810 | |
---|
[c45170a] | 811 | The new declaration syntax can be used in other contexts where types are required, \eg casts and the pseudo-routine ©sizeof©: |
---|
[6c91065] | 812 | \begin{quote2} |
---|
[45576af9] | 813 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 814 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
[6c91065] | 815 | \begin{lstlisting} |
---|
[45576af9] | 816 | y = (®* int®)x; |
---|
| 817 | i = sizeof(®[ 5 ] * int®); |
---|
[6c91065] | 818 | \end{lstlisting} |
---|
| 819 | & |
---|
| 820 | \begin{lstlisting} |
---|
[45576af9] | 821 | y = (®int *®)x; |
---|
| 822 | i = sizeof(®int *[ 5 ]®); |
---|
[6c91065] | 823 | \end{lstlisting} |
---|
| 824 | \end{tabular} |
---|
| 825 | \end{quote2} |
---|
| 826 | |
---|
| 827 | |
---|
| 828 | \section{Routine Definition} |
---|
| 829 | |
---|
| 830 | \CFA also supports a new syntax for routine definition, as well as ISO C and K\&R routine syntax. |
---|
[c45170a] | 831 | The point of the new syntax is to allow returning multiple values from a routine~\cite{Galletly96,CLU}, \eg: |
---|
[6c91065] | 832 | \begin{lstlisting} |
---|
[e945826] | 833 | ®[ int o1, int o2, char o3 ]® f( int i1, char i2, char i3 ) { |
---|
| 834 | §\emph{routine body}§ |
---|
[6c91065] | 835 | } |
---|
| 836 | \end{lstlisting} |
---|
[0638c44] | 837 | where routine ©f© has three output (return values) and three input parameters. |
---|
[6c91065] | 838 | 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. |
---|
| 839 | |
---|
[0638c44] | 840 | 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{ |
---|
[07bc165] | 841 | \Index*{Michael Tiemann}, with help from \Index*{Doug Lea}, provided named return values in g++, circa 1989.} |
---|
[6c91065] | 842 | The value of each local return variable is automatically returned at routine termination. |
---|
[c45170a] | 843 | Declaration qualifiers can only appear at the start of a routine definition, \eg: |
---|
[6c91065] | 844 | \begin{lstlisting} |
---|
[45576af9] | 845 | ®extern® [ int x ] g( int y ) {§\,§} |
---|
[6c91065] | 846 | \end{lstlisting} |
---|
| 847 | Lastly, if there are no output parameters or input parameters, the brackets and/or parentheses must still be specified; |
---|
| 848 | 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: |
---|
| 849 | \begin{lstlisting} |
---|
[4096de0] | 850 | [§\,§] g(); §\C{// no input or output parameters}§ |
---|
| 851 | [ void ] g( void ); §\C{// no input or output parameters}§ |
---|
[6c91065] | 852 | \end{lstlisting} |
---|
| 853 | |
---|
| 854 | Routine f is called as follows: |
---|
| 855 | \begin{lstlisting} |
---|
| 856 | [ i, j, ch ] = f( 3, 'a', ch ); |
---|
| 857 | \end{lstlisting} |
---|
[45576af9] | 858 | The list of return values from f and the grouping on the left-hand side of the assignment is called a \newterm{return list} and discussed in Section 12. |
---|
[6c91065] | 859 | |
---|
| 860 | \CFA style declarations cannot be used to declare parameters for K\&R style routine definitions because of the following ambiguity: |
---|
| 861 | \begin{lstlisting} |
---|
[6b6597c] | 862 | int (*f(x))[ 5 ] int x; {} |
---|
[6c91065] | 863 | \end{lstlisting} |
---|
[0638c44] | 864 | 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. |
---|
| 865 | Since the strings overlap starting with the open bracket, ©[©, there is an ambiguous interpretation for the string. |
---|
[6c91065] | 866 | As well, \CFA-style declarations cannot be used to declare parameters for C-style routine-definitions because of the following ambiguity: |
---|
| 867 | \begin{lstlisting} |
---|
| 868 | typedef int foo; |
---|
[4096de0] | 869 | int f( int (* foo) ); §\C{// foo is redefined as a parameter name}§ |
---|
[6c91065] | 870 | \end{lstlisting} |
---|
[0638c44] | 871 | 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. |
---|
[45576af9] | 872 | 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. |
---|
[6c91065] | 873 | 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. |
---|
| 874 | |
---|
[c45170a] | 875 | C-style declarations can be used to declare parameters for \CFA style routine definitions, \eg: |
---|
[6c91065] | 876 | \begin{lstlisting} |
---|
[4096de0] | 877 | [ int ] f( * int, int * ); §\C{// returns an integer, accepts 2 pointers to integers}§ |
---|
| 878 | [ * int, int * ] f( int ); §\C{// returns 2 pointers to integers, accepts an integer}§ |
---|
[6c91065] | 879 | \end{lstlisting} |
---|
| 880 | 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: |
---|
| 881 | \begin{lstlisting} |
---|
| 882 | #define ptoa( n, d ) int (*n)[ d ] |
---|
[4096de0] | 883 | int f( ptoa( p, 5 ) ) ... §\C{// expands to int f( int (*p)[ 5 ] )}§ |
---|
| 884 | [ int ] f( ptoa( p, 5 ) ) ... §\C{// expands to [ int ] f( int (*p)[ 5 ] )}§ |
---|
[6c91065] | 885 | \end{lstlisting} |
---|
| 886 | Again, programmers are highly encouraged to use one declaration form or the other, rather than mixing the forms. |
---|
| 887 | |
---|
| 888 | |
---|
[45576af9] | 889 | \subsection{Named Return Values} |
---|
[6c91065] | 890 | |
---|
[45576af9] | 891 | \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: |
---|
[6c91065] | 892 | \begin{lstlisting} |
---|
| 893 | int f() { |
---|
| 894 | int x; |
---|
| 895 | ... x = 0; ... x = y; ... |
---|
| 896 | return x; |
---|
| 897 | } |
---|
| 898 | \end{lstlisting} |
---|
[0638c44] | 899 | 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: |
---|
[45576af9] | 900 | \newline |
---|
| 901 | \begin{minipage}{\linewidth} |
---|
[6c91065] | 902 | \begin{lstlisting} |
---|
[45576af9] | 903 | ®[ int x, int y ]® f() { |
---|
| 904 | int z; |
---|
| 905 | ... x = 0; ... y = z; ... |
---|
[4096de0] | 906 | ®return;® §\C{// implicitly return x, y}§ |
---|
[6c91065] | 907 | } |
---|
| 908 | \end{lstlisting} |
---|
[45576af9] | 909 | \end{minipage} |
---|
| 910 | \newline |
---|
| 911 | When the return is encountered, the current values of ©x© and ©y© are returned to the calling routine. |
---|
[0638c44] | 912 | As well, ``falling off the end'' of a routine without a ©return© statement is permitted, as in: |
---|
[6c91065] | 913 | \begin{lstlisting} |
---|
[45576af9] | 914 | [ int x, int y ] f() { |
---|
| 915 | ... |
---|
[4096de0] | 916 | } §\C{// implicitly return x, y}§ |
---|
[6c91065] | 917 | \end{lstlisting} |
---|
[45576af9] | 918 | In this case, the current values of ©x© and ©y© are returned to the calling routine just as if a ©return© had been encountered. |
---|
[6c91065] | 919 | |
---|
| 920 | |
---|
| 921 | \subsection{Routine Prototype} |
---|
| 922 | |
---|
| 923 | The syntax of the new routine prototype declaration follows directly from the new routine definition syntax; |
---|
[c45170a] | 924 | as well, parameter names are optional, \eg: |
---|
[6c91065] | 925 | \begin{lstlisting} |
---|
[4096de0] | 926 | [ int x ] f (); §\C{// returning int with no parameters}§ |
---|
| 927 | [ * int ] g (int y); §\C{// returning pointer to int with int parameter}§ |
---|
| 928 | [ ] h (int,char); §\C{// returning no result with int and char parameters}§ |
---|
| 929 | [ * int,int ] j (int); §\C{// returning pointer to int and int, with int parameter}§ |
---|
[6c91065] | 930 | \end{lstlisting} |
---|
| 931 | This syntax allows a prototype declaration to be created by cutting and pasting source text from the routine definition header (or vice versa). |
---|
[c45170a] | 932 | It is possible to declare multiple routine-prototypes in a single declaration, but the entire type specification is distributed across \emph{all} routine names in the declaration list (see~\VRef{s:Declarations}), \eg: |
---|
[6c91065] | 933 | \begin{quote2} |
---|
[45576af9] | 934 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 935 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
[6c91065] | 936 | \begin{lstlisting} |
---|
| 937 | [ int ] f(int), g; |
---|
| 938 | \end{lstlisting} |
---|
| 939 | & |
---|
| 940 | \begin{lstlisting} |
---|
| 941 | int f(int), g(int); |
---|
| 942 | \end{lstlisting} |
---|
| 943 | \end{tabular} |
---|
| 944 | \end{quote2} |
---|
[c45170a] | 945 | Declaration qualifiers can only appear at the start of a \CFA routine declaration,\footref{StorageClassSpecifier} \eg: |
---|
[6c91065] | 946 | \begin{lstlisting} |
---|
| 947 | extern [ int ] f (int); |
---|
| 948 | static [ int ] g (int); |
---|
| 949 | \end{lstlisting} |
---|
| 950 | |
---|
| 951 | |
---|
| 952 | \section{Routine Pointers} |
---|
| 953 | |
---|
[c45170a] | 954 | The syntax for pointers to \CFA routines specifies the pointer name on the right, \eg: |
---|
[6c91065] | 955 | \begin{lstlisting} |
---|
[4096de0] | 956 | * [ int x ] () fp; §\C{// pointer to routine returning int with no parameters}§ |
---|
| 957 | * [ * int ] (int y) gp; §\C{// pointer to routine returning pointer to int with int parameter}§ |
---|
| 958 | * [ ] (int,char) hp; §\C{// pointer to routine returning no result with int and char parameters}§ |
---|
| 959 | * [ * int,int ] (int) jp; §\C{// pointer to routine returning pointer to int and int, with int parameter}§ |
---|
[6c91065] | 960 | \end{lstlisting} |
---|
| 961 | While parameter names are optional, \emph{a routine name cannot be specified}; |
---|
| 962 | for example, the following is incorrect: |
---|
| 963 | \begin{lstlisting} |
---|
[4096de0] | 964 | * [ int x ] f () fp; §\C{// routine name "f" is not allowed}§ |
---|
[6c91065] | 965 | \end{lstlisting} |
---|
| 966 | |
---|
| 967 | |
---|
| 968 | \section{Named and Default Arguments} |
---|
| 969 | |
---|
[b52d900] | 970 | Named and default arguments~\cite{Hardgrave76}\footnote{ |
---|
[6c91065] | 971 | Francez~\cite{Francez77} proposed a further extension to the named-parameter passing style, which specifies what type of communication (by value, by reference, by name) the argument is passed to the routine.} |
---|
| 972 | are two mechanisms to simplify routine call. |
---|
| 973 | Both mechanisms are discussed with respect to \CFA. |
---|
| 974 | \begin{description} |
---|
| 975 | \item[Named (or Keyword) Arguments:] |
---|
| 976 | provide the ability to specify an argument to a routine call using the parameter name rather than the position of the parameter. |
---|
| 977 | For example, given the routine: |
---|
| 978 | \begin{lstlisting} |
---|
| 979 | void p( int x, int y, int z ) {...} |
---|
| 980 | \end{lstlisting} |
---|
| 981 | a positional call is: |
---|
| 982 | \begin{lstlisting} |
---|
| 983 | p( 4, 7, 3 ); |
---|
| 984 | \end{lstlisting} |
---|
| 985 | whereas a named (keyword) call may be: |
---|
| 986 | \begin{lstlisting} |
---|
[4096de0] | 987 | p( z : 3, x : 4, y : 7 ); §\C{// rewrite $\Rightarrow$ p( 4, 7, 3 )}§ |
---|
[6c91065] | 988 | \end{lstlisting} |
---|
| 989 | Here the order of the arguments is unimportant, and the names of the parameters are used to associate argument values with the corresponding parameters. |
---|
| 990 | The compiler rewrites a named call into a positional call. |
---|
| 991 | The advantages of named parameters are: |
---|
| 992 | \begin{itemize} |
---|
| 993 | \item |
---|
| 994 | Remembering the names of the parameters may be easier than the order in the routine definition. |
---|
| 995 | \item |
---|
| 996 | Parameter names provide documentation at the call site (assuming the names are descriptive). |
---|
| 997 | \item |
---|
| 998 | Changes can be made to the order or number of parameters without affecting the call (although the call must still be recompiled). |
---|
| 999 | \end{itemize} |
---|
| 1000 | |
---|
| 1001 | Unfortunately, named arguments do not work in C-style programming-languages because a routine prototype is not required to specify parameter names, nor do the names in the prototype have to match with the actual definition. |
---|
| 1002 | For example, the following routine prototypes and definition are all valid. |
---|
| 1003 | \begin{lstlisting} |
---|
[4096de0] | 1004 | void p( int, int, int ); §\C{// equivalent prototypes}§ |
---|
[6c91065] | 1005 | void p( int x, int y, int z ); |
---|
| 1006 | void p( int y, int x, int z ); |
---|
| 1007 | void p( int z, int y, int x ); |
---|
[4096de0] | 1008 | void p( int q, int r, int s ) {} §\C{// match with this definition}§ |
---|
[6c91065] | 1009 | \end{lstlisting} |
---|
| 1010 | Forcing matching parameter names in routine prototypes with corresponding routine definitions is possible, but goes against a strong tradition in C programming. |
---|
| 1011 | Alternatively, prototype definitions can be eliminated by using a two-pass compilation, and implicitly creating header files for exports. |
---|
| 1012 | The former is easy to do, while the latter is more complex. |
---|
| 1013 | Currently, \CFA does \emph{not} attempt to support named arguments. |
---|
| 1014 | |
---|
| 1015 | \item[Default Arguments] |
---|
| 1016 | provide the ability to associate a default value with a parameter so it can be optionally specified in the argument list. |
---|
| 1017 | For example, given the routine: |
---|
| 1018 | \begin{lstlisting} |
---|
| 1019 | void p( int x = 1, int y = 2, int z = 3 ) {...} |
---|
| 1020 | \end{lstlisting} |
---|
| 1021 | the allowable positional calls are: |
---|
| 1022 | \begin{lstlisting} |
---|
[4096de0] | 1023 | p(); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ |
---|
| 1024 | p( 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ |
---|
| 1025 | p( 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ |
---|
| 1026 | p( 4, 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 4 )}§ |
---|
[6c91065] | 1027 | // empty arguments |
---|
[4096de0] | 1028 | p( , 4, 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 4 )}§ |
---|
| 1029 | p( 4, , 4 ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 4 )}§ |
---|
| 1030 | p( 4, 4, ); §\C{// rewrite $\Rightarrow$ p( 4, 4, 3 )}§ |
---|
| 1031 | p( 4, , ); §\C{// rewrite $\Rightarrow$ p( 4, 2, 3 )}§ |
---|
| 1032 | p( , 4, ); §\C{// rewrite $\Rightarrow$ p( 1, 4, 3 )}§ |
---|
| 1033 | p( , , 4 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 4 )}§ |
---|
| 1034 | p( , , ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 3 )}§ |
---|
[6c91065] | 1035 | \end{lstlisting} |
---|
| 1036 | Here the missing arguments are inserted from the default values in the parameter list. |
---|
| 1037 | The compiler rewrites missing default values into explicit positional arguments. |
---|
| 1038 | The advantages of default values are: |
---|
| 1039 | \begin{itemize} |
---|
| 1040 | \item |
---|
| 1041 | Routines with a large number of parameters are often very generalized, giving a programmer a number of different options on how a computation is performed. |
---|
| 1042 | For many of these kinds of routines, there are standard or default settings that work for the majority of computations. |
---|
| 1043 | Without default values for parameters, a programmer is forced to specify these common values all the time, resulting in long argument lists that are error prone. |
---|
| 1044 | \item |
---|
| 1045 | When a routine's interface is augmented with new parameters, it extends the interface providing generalizability\footnote{ |
---|
| 1046 | ``It should be possible for the implementor of an abstraction to increase its generality. |
---|
| 1047 | So long as the modified abstraction is a generalization of the original, existing uses of the abstraction will not require change. |
---|
| 1048 | It might be possible to modify an abstraction in a manner which is not a generalization without affecting existing uses, but, without inspecting the modules in which the uses occur, this possibility cannot be determined. |
---|
| 1049 | This criterion precludes the addition of parameters, unless these parameters have default or inferred values that are valid for all possible existing applications.''~\cite[p.~128]{Cormack90}} |
---|
| 1050 | (somewhat like the generalization provided by inheritance for classes). |
---|
| 1051 | That is, all existing calls are still valid, although the call must still be recompiled. |
---|
| 1052 | \end{itemize} |
---|
| 1053 | The only disadvantage of default arguments is that unintentional omission of an argument may not result in a compiler-time error. |
---|
| 1054 | Instead, a default value is used, which may not be the programmer's intent. |
---|
| 1055 | |
---|
| 1056 | Default values may only appear in a prototype versus definition context: |
---|
| 1057 | \begin{lstlisting} |
---|
[4096de0] | 1058 | void p( int x, int y = 2, int z = 3 ); §\C{// prototype: allowed}§ |
---|
| 1059 | void p( int, int = 2, int = 3 ); §\C{// prototype: allowed}§ |
---|
| 1060 | void p( int x, int y = 2, int z = 3 ) {} §\C{// definition: not allowed}§ |
---|
[6c91065] | 1061 | \end{lstlisting} |
---|
| 1062 | The reason for this restriction is to allow separate compilation. |
---|
| 1063 | Multiple prototypes with different default values is an error. |
---|
| 1064 | \end{description} |
---|
| 1065 | |
---|
| 1066 | Ellipse (``...'') arguments present problems when used with default arguments. |
---|
| 1067 | The conflict occurs because both named and ellipse arguments must appear after positional arguments, giving two possibilities: |
---|
| 1068 | \begin{lstlisting} |
---|
| 1069 | p( /* positional */, . . ., /* named */ ); |
---|
| 1070 | p( /* positional */, /* named */, . . . ); |
---|
| 1071 | \end{lstlisting} |
---|
[c45170a] | 1072 | While it is possible to implement both approaches, the first possibly is more complex than the second, \eg: |
---|
[6c91065] | 1073 | \begin{lstlisting} |
---|
| 1074 | p( int x, int y, int z, . . . ); |
---|
[4096de0] | 1075 | p( 1, 4, 5, 6, z : 3, y : 2 ); §\C{// assume p( /* positional */, . . ., /* named */ );}§ |
---|
| 1076 | p( 1, z : 3, y : 2, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, . . . );}§ |
---|
[6c91065] | 1077 | \end{lstlisting} |
---|
| 1078 | 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. |
---|
| 1079 | Hence, this approach seems significantly more difficult, and hence, confusing and error prone. |
---|
| 1080 | In the second call, the named arguments separate the positional and ellipse arguments, making it trivial to read the call. |
---|
| 1081 | |
---|
[c45170a] | 1082 | The problem is exacerbated with default arguments, \eg: |
---|
[6c91065] | 1083 | \begin{lstlisting} |
---|
| 1084 | void p( int x, int y = 2, int z = 3. . . ); |
---|
[4096de0] | 1085 | p( 1, 4, 5, 6, z : 3 ); §\C{// assume p( /* positional */, . . ., /* named */ );}§ |
---|
| 1086 | p( 1, z : 3, 4, 5, 6 ); §\C{// assume p( /* positional */, /* named */, . . . );}§ |
---|
[6c91065] | 1087 | \end{lstlisting} |
---|
| 1088 | The first call is an error because arguments 4 and 5 are actually positional not ellipse arguments; |
---|
| 1089 | therefore, argument 5 subsequently conflicts with the named argument z : 3. |
---|
| 1090 | In the second call, the default value for y is implicitly inserted after argument 1 and the named arguments separate the positional and ellipse arguments, making it trivial to read the call. |
---|
| 1091 | For these reasons, \CFA requires named arguments before ellipse arguments. |
---|
| 1092 | Finally, while ellipse arguments are needed for a small set of existing C routines, like printf, the extended \CFA type system largely eliminates the need for ellipse arguments (see Section 24), making much of this discussion moot. |
---|
| 1093 | |
---|
| 1094 | Default arguments and overloading (see Section 24) are complementary. |
---|
| 1095 | While in theory default arguments can be simulated with overloading, as in: |
---|
| 1096 | \begin{quote2} |
---|
[45576af9] | 1097 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 1098 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{default arguments}} & \multicolumn{1}{c}{\textbf{overloading}} \\ |
---|
[6c91065] | 1099 | \begin{lstlisting} |
---|
| 1100 | void p( int x, int y = 2, int z = 3 ) {...} |
---|
| 1101 | |
---|
| 1102 | |
---|
| 1103 | \end{lstlisting} |
---|
| 1104 | & |
---|
| 1105 | \begin{lstlisting} |
---|
| 1106 | void p( int x, int y, int z ) {...} |
---|
| 1107 | void p( int x ) { p( x, 2, 3 ); } |
---|
| 1108 | void p( int x, int y ) { p( x, y, 3 ); } |
---|
| 1109 | \end{lstlisting} |
---|
| 1110 | \end{tabular} |
---|
| 1111 | \end{quote2} |
---|
| 1112 | the number of required overloaded routines is linear in the number of default values, which is unacceptable growth. |
---|
| 1113 | In general, overloading should only be used over default arguments if the body of the routine is significantly different. |
---|
| 1114 | Furthermore, overloading cannot handle accessing default arguments in the middle of a positional list, via a missing argument, such as: |
---|
| 1115 | \begin{lstlisting} |
---|
[4096de0] | 1116 | p( 1, /* default */, 5 ); §\C{// rewrite $\Rightarrow$ p( 1, 2, 5 )}§ |
---|
[6c91065] | 1117 | \end{lstlisting} |
---|
| 1118 | |
---|
| 1119 | Given the \CFA restrictions above, both named and default arguments are backwards compatible. |
---|
[07bc165] | 1120 | \Index*[C++]{\CC} only supports default arguments; |
---|
| 1121 | \Index*{Ada} supports both named and default arguments. |
---|
[6c91065] | 1122 | |
---|
| 1123 | |
---|
| 1124 | \section{Type/Routine Nesting} |
---|
| 1125 | |
---|
| 1126 | Nesting of types and routines is useful for controlling name visibility (\newterm{name hiding}). |
---|
| 1127 | |
---|
| 1128 | |
---|
| 1129 | \subsection{Type Nesting} |
---|
| 1130 | |
---|
[9724df0] | 1131 | \CFA allows \Index{type nesting}, and type qualification of the nested types (see \VRef[Figure]{f:TypeNestingQualification}), where as C hoists\index{type hoisting} (refactors) nested types into the enclosing scope and has no type qualification. |
---|
| 1132 | \begin{figure} |
---|
[45576af9] | 1133 | \begin{tabular}{@{}l@{\hspace{3em}}l|l@{}} |
---|
| 1134 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{C Type Nesting}} & \multicolumn{1}{c}{\textbf{C Implicit Hoisting}} & \multicolumn{1}{|c}{\textbf{\CFA}} \\ |
---|
[6c91065] | 1135 | \hline |
---|
| 1136 | \begin{lstlisting} |
---|
| 1137 | struct S { |
---|
| 1138 | enum C { R, G, B }; |
---|
| 1139 | struct T { |
---|
| 1140 | union U { int i, j; }; |
---|
| 1141 | enum C c; |
---|
| 1142 | short int i, j; |
---|
| 1143 | }; |
---|
| 1144 | struct T t; |
---|
| 1145 | } s; |
---|
| 1146 | |
---|
| 1147 | int fred() { |
---|
| 1148 | s.t.c = R; |
---|
| 1149 | struct T t = { R, 1, 2 }; |
---|
| 1150 | enum C c; |
---|
| 1151 | union U u; |
---|
| 1152 | } |
---|
| 1153 | \end{lstlisting} |
---|
| 1154 | & |
---|
| 1155 | \begin{lstlisting} |
---|
| 1156 | enum C { R, G, B }; |
---|
| 1157 | union U { int i, j; }; |
---|
| 1158 | struct T { |
---|
| 1159 | enum C c; |
---|
| 1160 | short int i, j; |
---|
| 1161 | }; |
---|
| 1162 | struct S { |
---|
| 1163 | struct T t; |
---|
| 1164 | } s; |
---|
| 1165 | |
---|
| 1166 | |
---|
| 1167 | |
---|
| 1168 | |
---|
| 1169 | |
---|
| 1170 | |
---|
| 1171 | |
---|
| 1172 | \end{lstlisting} |
---|
| 1173 | & |
---|
| 1174 | \begin{lstlisting} |
---|
| 1175 | struct S { |
---|
| 1176 | enum C { R, G, B }; |
---|
| 1177 | struct T { |
---|
| 1178 | union U { int i, j; }; |
---|
| 1179 | enum C c; |
---|
| 1180 | short int i, j; |
---|
| 1181 | }; |
---|
| 1182 | struct T t; |
---|
| 1183 | } s; |
---|
| 1184 | |
---|
| 1185 | int fred() { |
---|
[e945826] | 1186 | s.t.c = ®S.®R; // type qualification |
---|
| 1187 | struct ®S.®T t = { ®S.®R, 1, 2 }; |
---|
| 1188 | enum ®S.®C c; |
---|
| 1189 | union ®S.T.®U u; |
---|
[6c91065] | 1190 | } |
---|
| 1191 | \end{lstlisting} |
---|
| 1192 | \end{tabular} |
---|
[9724df0] | 1193 | \caption{Type Nesting / Qualification} |
---|
| 1194 | \label{f:TypeNestingQualification} |
---|
| 1195 | \end{figure} |
---|
[0638c44] | 1196 | In the left example in C, types ©C©, ©U© and ©T© are implicitly hoisted outside of type ©S© into the containing block scope. |
---|
[07bc165] | 1197 | 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 ``©::©''. |
---|
[6c91065] | 1198 | |
---|
| 1199 | |
---|
| 1200 | \subsection{Routine Nesting} |
---|
| 1201 | |
---|
| 1202 | While \CFA does not provide object programming by putting routines into structures, it does rely heavily on locally nested routines to redefine operations at or close to a call site. |
---|
| 1203 | For example, the C quick-sort is wrapped into the following polymorphic \CFA routine: |
---|
| 1204 | \begin{lstlisting} |
---|
| 1205 | forall( otype T | { int ?<?( T, T ); } ) |
---|
| 1206 | void qsort( const T * arr, size_t dimension ); |
---|
| 1207 | \end{lstlisting} |
---|
| 1208 | which can be used to sort in ascending and descending order by locally redefining the less-than operator into greater-than. |
---|
| 1209 | \begin{lstlisting} |
---|
[6b6597c] | 1210 | const unsigned int size = 5; |
---|
[53ba273] | 1211 | int ia[size]; |
---|
[4096de0] | 1212 | ... §\C{// assign values to array ia}§ |
---|
| 1213 | qsort( ia, size ); §\C{// sort ascending order using builtin ?<?}§ |
---|
[53ba273] | 1214 | { |
---|
[4096de0] | 1215 | ®int ?<?( int x, int y ) { return x > y; }® §\C{// nested routine}§ |
---|
| 1216 | qsort( ia, size ); §\C{// sort descending order by local redefinition}§ |
---|
[53ba273] | 1217 | } |
---|
| 1218 | \end{lstlisting} |
---|
[6c91065] | 1219 | |
---|
[53ba273] | 1220 | Nested routines are not first-class, meaning a nested routine cannot be returned if it has references to variables in its enclosing blocks; |
---|
| 1221 | the only exception is references to the external block of the translation unit, as these variables persist for the duration of the program. |
---|
[07bc165] | 1222 | The following program in undefined in \CFA (and Indexc{gcc}) |
---|
[53ba273] | 1223 | \begin{lstlisting} |
---|
[4096de0] | 1224 | [* [int]( int )] foo() { §\C{// int (*foo())( int )}§ |
---|
[e945826] | 1225 | int ®i® = 7; |
---|
[53ba273] | 1226 | int bar( int p ) { |
---|
[4096de0] | 1227 | ®i® += 1; §\C{// dependent on local variable}§ |
---|
[e945826] | 1228 | sout | ®i® | endl; |
---|
[53ba273] | 1229 | } |
---|
[4096de0] | 1230 | return bar; §\C{// undefined because of local dependence}§ |
---|
[53ba273] | 1231 | } |
---|
| 1232 | int main() { |
---|
[4096de0] | 1233 | * [int](int) fp = foo(); §\C{// int (*fp)(int)}§ |
---|
[53ba273] | 1234 | sout | fp( 3 ) | endl; |
---|
[6c91065] | 1235 | } |
---|
| 1236 | \end{lstlisting} |
---|
[53ba273] | 1237 | because |
---|
| 1238 | |
---|
| 1239 | Currently, there are no \Index{lambda} expressions, i.e., unnamed routines because routine names are very important to properly select the correct routine. |
---|
[6c91065] | 1240 | |
---|
| 1241 | |
---|
[45576af9] | 1242 | \section{Lexical List} |
---|
[6c91065] | 1243 | |
---|
| 1244 | In C and \CFA, lists of elements appear in several contexts, such as the parameter list for a routine call. |
---|
| 1245 | (More contexts are added shortly.) |
---|
[45576af9] | 1246 | A list of such elements is called a \newterm{lexical list}. |
---|
| 1247 | The general syntax of a lexical list is: |
---|
[6c91065] | 1248 | \begin{lstlisting} |
---|
[e945826] | 1249 | [ §\emph{exprlist}§ ] |
---|
[6c91065] | 1250 | \end{lstlisting} |
---|
[0638c44] | 1251 | where ©$\emph{exprlist}$© is a list of one or more expressions separated by commas. |
---|
[45576af9] | 1252 | The brackets, ©[]©, allow differentiating between lexical lists and expressions containing the C comma operator. |
---|
| 1253 | The following are examples of lexical lists: |
---|
[6c91065] | 1254 | \begin{lstlisting} |
---|
| 1255 | [ x, y, z ] |
---|
| 1256 | [ 2 ] |
---|
| 1257 | [ v+w, x*y, 3.14159, f() ] |
---|
| 1258 | \end{lstlisting} |
---|
[0638c44] | 1259 | Tuples are permitted to contain sub-tuples (i.e., nesting), such as ©[ [ 14, 21 ], 9 ]©, which is a 2-element tuple whose first element is itself a tuple. |
---|
[6c91065] | 1260 | Note, a tuple is not a record (structure); |
---|
| 1261 | a record denotes a single value with substructure, whereas a tuple is multiple values with no substructure (see flattening coercion in Section 12.1). |
---|
| 1262 | In essence, tuples are largely a compile time phenomenon, having little or no runtime presence. |
---|
| 1263 | |
---|
| 1264 | Tuples can be organized into compile-time tuple variables; |
---|
| 1265 | these variables are of \newterm{tuple type}. |
---|
| 1266 | Tuple variables and types can be used anywhere lists of conventional variables and types can be used. |
---|
| 1267 | The general syntax of a tuple type is: |
---|
| 1268 | \begin{lstlisting} |
---|
[e945826] | 1269 | [ §\emph{typelist}§ ] |
---|
[6c91065] | 1270 | \end{lstlisting} |
---|
[0638c44] | 1271 | 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. |
---|
[6c91065] | 1272 | Examples of tuple types include: |
---|
| 1273 | \begin{lstlisting} |
---|
| 1274 | [ unsigned int, char ] |
---|
| 1275 | [ double, double, double ] |
---|
[4096de0] | 1276 | [ * int, int * ] §\C{// mix of CFA and ANSI}§ |
---|
[6b6597c] | 1277 | [ * [ 5 ] int, * * char, * [ [ int, int ] ] (int, int) ] |
---|
[6c91065] | 1278 | \end{lstlisting} |
---|
[0638c44] | 1279 | 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. |
---|
[6c91065] | 1280 | |
---|
| 1281 | Examples of declarations using tuple types are: |
---|
| 1282 | \begin{lstlisting} |
---|
[4096de0] | 1283 | [ int, int ] x; §\C{// 2 element tuple, each element of type int}§ |
---|
| 1284 | * [ char, char ] y; §\C{// pointer to a 2 element tuple}§ |
---|
[6c91065] | 1285 | [ [ int, int ] ] z ([ int, int ]); |
---|
| 1286 | \end{lstlisting} |
---|
| 1287 | The last example declares an external routine that expects a 2 element tuple as an input parameter and returns a 2 element tuple as its result. |
---|
| 1288 | |
---|
| 1289 | As mentioned, tuples can appear in contexts requiring a list of value, such as an argument list of a routine call. |
---|
[c45170a] | 1290 | In unambiguous situations, the tuple brackets may be omitted, \eg a tuple that appears as an argument may have its |
---|
[6c91065] | 1291 | square brackets omitted for convenience; therefore, the following routine invocations are equivalent: |
---|
| 1292 | \begin{lstlisting} |
---|
| 1293 | f( [ 1, x+2, fred() ] ); |
---|
| 1294 | f( 1, x+2, fred() ); |
---|
| 1295 | \end{lstlisting} |
---|
| 1296 | Also, a tuple or a tuple variable may be used to supply all or part of an argument list for a routine expecting multiple input parameters or for a routine expecting a tuple as an input parameter. |
---|
| 1297 | For example, the following are all legal: |
---|
| 1298 | \begin{lstlisting} |
---|
| 1299 | [ int, int ] w1; |
---|
| 1300 | [ int, int, int ] w2; |
---|
| 1301 | [ void ] f (int, int, int); /* three input parameters of type int */ |
---|
| 1302 | [ void ] g ([ int, int, int ]); /* 3 element tuple as input */ |
---|
| 1303 | f( [ 1, 2, 3 ] ); |
---|
| 1304 | f( w1, 3 ); |
---|
| 1305 | f( 1, w1 ); |
---|
| 1306 | f( w2 ); |
---|
| 1307 | g( [ 1, 2, 3 ] ); |
---|
| 1308 | g( w1, 3 ); |
---|
| 1309 | g( 1, w1 ); |
---|
| 1310 | g( w2 ); |
---|
| 1311 | \end{lstlisting} |
---|
| 1312 | Note, in all cases 3 arguments are supplied even though the syntax may appear to supply less than 3. As mentioned, a |
---|
| 1313 | tuple does not have structure like a record; a tuple is simply converted into a list of components. |
---|
| 1314 | \begin{rationale} |
---|
[0638c44] | 1315 | The present implementation of \CFA does not support nested routine calls when the inner routine returns multiple values; i.e., a statement such as ©g( f() )© is not supported. |
---|
[6c91065] | 1316 | Using a temporary variable to store the results of the inner routine and then passing this variable to the outer routine works, however. |
---|
| 1317 | \end{rationale} |
---|
| 1318 | |
---|
| 1319 | A tuple can contain a C comma expression, provided the expression containing the comma operator is enclosed in parentheses. |
---|
| 1320 | For instance, the following tuples are equivalent: |
---|
| 1321 | \begin{lstlisting} |
---|
| 1322 | [ 1, 3, 5 ] |
---|
| 1323 | [ 1, (2, 3), 5 ] |
---|
| 1324 | \end{lstlisting} |
---|
| 1325 | The second element of the second tuple is the expression (2, 3), which yields the result 3. |
---|
| 1326 | This requirement is the same as for comma expressions in argument lists. |
---|
| 1327 | |
---|
| 1328 | Type qualifiers, i.e., const and volatile, may modify a tuple type. |
---|
[c45170a] | 1329 | The meaning is the same as for a type qualifier modifying an aggregate type [Int99, x 6.5.2.3(7),x 6.7.3(11)], i.e., the qualifier is distributed across all of the types in the tuple, \eg: |
---|
[6c91065] | 1330 | \begin{lstlisting} |
---|
| 1331 | const volatile [ int, float, const int ] x; |
---|
| 1332 | \end{lstlisting} |
---|
| 1333 | is equivalent to: |
---|
| 1334 | \begin{lstlisting} |
---|
| 1335 | [ const volatile int, const volatile float, const volatile int ] x; |
---|
| 1336 | \end{lstlisting} |
---|
[c45170a] | 1337 | Declaration qualifiers can only appear at the start of a \CFA tuple declaration4, \eg: |
---|
[6c91065] | 1338 | \begin{lstlisting} |
---|
| 1339 | extern [ int, int ] w1; |
---|
| 1340 | static [ int, int, int ] w2; |
---|
| 1341 | \end{lstlisting} |
---|
| 1342 | \begin{rationale} |
---|
| 1343 | Unfortunately, C's syntax for subscripts precluded treating them as tuples. |
---|
[0638c44] | 1344 | The C subscript list has the form ©[i][j]...© and not ©[i, j, ...]©. |
---|
[c45170a] | 1345 | 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. |
---|
[0638c44] | 1346 | 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. |
---|
[6c91065] | 1347 | \end{rationale} |
---|
| 1348 | |
---|
| 1349 | |
---|
| 1350 | \subsection{Tuple Coercions} |
---|
| 1351 | |
---|
| 1352 | There are four coercions that can be performed on tuples and tuple variables: closing, opening, flattening and structuring. |
---|
| 1353 | In addition, the coercion of dereferencing can be performed on a tuple variable to yield its value(s), as for other variables. |
---|
| 1354 | A \newterm{closing coercion} takes a set of values and converts it into a tuple value, which is a contiguous set of values, as in: |
---|
| 1355 | \begin{lstlisting} |
---|
| 1356 | [ int, int, int, int ] w; |
---|
| 1357 | w = [ 1, 2, 3, 4 ]; |
---|
| 1358 | \end{lstlisting} |
---|
| 1359 | First the right-hand tuple is closed into a tuple value and then the tuple value is assigned. |
---|
| 1360 | |
---|
| 1361 | An \newterm{opening coercion} is the opposite of closing; a tuple value is converted into a tuple of values, as in: |
---|
| 1362 | \begin{lstlisting} |
---|
| 1363 | [ a, b, c, d ] = w |
---|
| 1364 | \end{lstlisting} |
---|
[0638c44] | 1365 | ©w© is implicitly opened to yield a tuple of four values, which are then assigned individually. |
---|
[6c91065] | 1366 | |
---|
| 1367 | A \newterm{flattening coercion} coerces a nested tuple, i.e., 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: |
---|
| 1368 | \begin{lstlisting} |
---|
| 1369 | [ a, b, c, d ] = [ 1, [ 2, 3 ], 4 ]; |
---|
| 1370 | \end{lstlisting} |
---|
| 1371 | First the right-hand tuple is flattened and then the values are assigned individually. |
---|
| 1372 | Flattening is also performed on tuple types. |
---|
[0638c44] | 1373 | For example, the type ©[ int, [ int, int ], int ]© can be coerced, using flattening, into the type ©[ int, int, int, int ]©. |
---|
[6c91065] | 1374 | |
---|
| 1375 | A \newterm{structuring coercion} is the opposite of flattening; |
---|
| 1376 | a tuple is structured into a more complex nested tuple. |
---|
[0638c44] | 1377 | 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 ]©. |
---|
[6c91065] | 1378 | In the following example, the last assignment illustrates all the tuple coercions: |
---|
| 1379 | \begin{lstlisting} |
---|
| 1380 | [ int, int, int, int ] w = [ 1, 2, 3, 4 ]; |
---|
| 1381 | int x = 5; |
---|
[4096de0] | 1382 | [ x, w ] = [ w, x ]; §\C{// all four tuple coercions}§ |
---|
[6c91065] | 1383 | \end{lstlisting} |
---|
| 1384 | Starting on the right-hand tuple in the last assignment statement, w is opened, producing a tuple of four values; |
---|
[0638c44] | 1385 | therefore, the right-hand tuple is now the tuple ©[ [ 1, 2, 3, 4 ], 5 ]©. |
---|
| 1386 | 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. |
---|
| 1387 | The tuple ©[ 2, 3, 4, 5 ]© is then closed to create a tuple value. |
---|
| 1388 | Finally, ©x© is assigned ©1© and ©w© is assigned the tuple value using multiple assignment (see Section 14). |
---|
[6c91065] | 1389 | \begin{rationale} |
---|
| 1390 | A possible additional language extension is to use the structuring coercion for tuples to initialize a complex record with a tuple. |
---|
| 1391 | \end{rationale} |
---|
| 1392 | |
---|
| 1393 | |
---|
| 1394 | \section{Mass Assignment} |
---|
| 1395 | |
---|
| 1396 | \CFA permits assignment to several variables at once using mass assignment~\cite{CLU}. |
---|
| 1397 | Mass assignment has the following form: |
---|
| 1398 | \begin{lstlisting} |
---|
[e945826] | 1399 | [ §\emph{lvalue}§, ..., §\emph{lvalue}§ ] = §\emph{expr}§; |
---|
[6c91065] | 1400 | \end{lstlisting} |
---|
[07bc165] | 1401 | \index{lvalue} |
---|
[e945826] | 1402 | The left-hand side is a tuple of \emph{lvalues}, which is a list of expressions each yielding an address, i.e., any data object that can appear on the left-hand side of a conventional assignment statement. |
---|
[0638c44] | 1403 | ©$\emph{expr}$© is any standard arithmetic expression. |
---|
[6c91065] | 1404 | Clearly, the types of the entities being assigned must be type compatible with the value of the expression. |
---|
| 1405 | |
---|
[c45170a] | 1406 | Mass assignment has parallel semantics, \eg the statement: |
---|
[6c91065] | 1407 | \begin{lstlisting} |
---|
| 1408 | [ x, y, z ] = 1.5; |
---|
| 1409 | \end{lstlisting} |
---|
| 1410 | is equivalent to: |
---|
| 1411 | \begin{lstlisting} |
---|
| 1412 | x = 1.5; y = 1.5; z = 1.5; |
---|
| 1413 | \end{lstlisting} |
---|
| 1414 | This semantics is not the same as the following in C: |
---|
| 1415 | \begin{lstlisting} |
---|
| 1416 | x = y = z = 1.5; |
---|
| 1417 | \end{lstlisting} |
---|
| 1418 | as conversions between intermediate assignments may lose information. |
---|
| 1419 | A more complex example is: |
---|
| 1420 | \begin{lstlisting} |
---|
| 1421 | [ i, y[i], z ] = a + b; |
---|
| 1422 | \end{lstlisting} |
---|
| 1423 | which is equivalent to: |
---|
| 1424 | \begin{lstlisting} |
---|
| 1425 | t = a + b; |
---|
| 1426 | a1 = &i; a2 = &y[i]; a3 = &z; |
---|
| 1427 | *a1 = t; *a2 = t; *a3 = t; |
---|
| 1428 | \end{lstlisting} |
---|
[0638c44] | 1429 | The temporary ©t© is necessary to store the value of the expression to eliminate conversion issues. |
---|
[6c91065] | 1430 | The temporaries for the addresses are needed so that locations on the left-hand side do not change as the values are assigned. |
---|
[0638c44] | 1431 | In this case, ©y[i]© uses the previous value of ©i© and not the new value set at the beginning of the mass assignment. |
---|
[6c91065] | 1432 | |
---|
| 1433 | |
---|
| 1434 | \section{Multiple Assignment} |
---|
| 1435 | |
---|
| 1436 | \CFA also supports the assignment of several values at once, known as multiple assignment~\cite{CLU,Galletly96}. |
---|
| 1437 | Multiple assignment has the following form: |
---|
| 1438 | \begin{lstlisting} |
---|
[e945826] | 1439 | [ §\emph{lvalue}§, . . ., §\emph{lvalue}§ ] = [ §\emph{expr}§, . . ., §\emph{expr}§ ]; |
---|
[6c91065] | 1440 | \end{lstlisting} |
---|
[07bc165] | 1441 | \index{lvalue} |
---|
[e945826] | 1442 | The left-hand side is a tuple of \emph{lvalues}, and the right-hand side is a tuple of \emph{expr}s. |
---|
| 1443 | Each \emph{expr} appearing on the righthand side of a multiple assignment statement is assigned to the corresponding \emph{lvalues} on the left-hand side of the statement using parallel semantics for each assignment. |
---|
[6c91065] | 1444 | An example of multiple assignment is: |
---|
| 1445 | \begin{lstlisting} |
---|
| 1446 | [ x, y, z ] = [ 1, 2, 3 ]; |
---|
| 1447 | \end{lstlisting} |
---|
[0638c44] | 1448 | Here, the values ©1©, ©2© and ©3© are assigned, respectively, to the variables ©x©, ©y© and ©z©. |
---|
[6c91065] | 1449 | A more complex example is: |
---|
| 1450 | \begin{lstlisting} |
---|
| 1451 | [ i, y[ i ], z ] = [ 1, i, a + b ]; |
---|
| 1452 | \end{lstlisting} |
---|
[0638c44] | 1453 | Here, the values ©1©, ©i© and ©a + b© are assigned to the variables ©i©, ©y[i]© and ©z©, respectively. |
---|
[6c91065] | 1454 | Note, the parallel semantics of |
---|
| 1455 | multiple assignment ensures: |
---|
| 1456 | \begin{lstlisting} |
---|
| 1457 | [ x, y ] = [ y, x ]; |
---|
| 1458 | \end{lstlisting} |
---|
[0638c44] | 1459 | correctly interchanges (swaps) the values stored in ©x© and ©y©. |
---|
[6c91065] | 1460 | The following cases are errors: |
---|
| 1461 | \begin{lstlisting} |
---|
| 1462 | [ a, b, c ] = [ 1, 2, 3, 4 ]; |
---|
| 1463 | [ a, b, c ] = [ 1, 2 ]; |
---|
| 1464 | \end{lstlisting} |
---|
| 1465 | because the number of entities in the left-hand tuple is unequal with the right-hand tuple. |
---|
| 1466 | |
---|
| 1467 | As for all tuple contexts in C, side effects should not be used because C does not define an ordering for the evaluation of the elements of a tuple; |
---|
| 1468 | both these examples produce indeterminate results: |
---|
| 1469 | \begin{lstlisting} |
---|
[4096de0] | 1470 | f( x++, x++ ); §\C{// C routine call with side effects in arguments}§ |
---|
| 1471 | [ v1, v2 ] = [ x++, x++ ]; §\C{// side effects in righthand side of multiple assignment}§ |
---|
[6c91065] | 1472 | \end{lstlisting} |
---|
| 1473 | |
---|
| 1474 | |
---|
| 1475 | \section{Cascade Assignment} |
---|
| 1476 | |
---|
| 1477 | As in C, \CFA mass and multiple assignments can be cascaded, producing cascade assignment. |
---|
| 1478 | Cascade assignment has the following form: |
---|
| 1479 | \begin{lstlisting} |
---|
[e945826] | 1480 | §\emph{tuple}§ = §\emph{tuple}§ = ... = §\emph{tuple}§; |
---|
[6c91065] | 1481 | \end{lstlisting} |
---|
| 1482 | and it has the same parallel semantics as for mass and multiple assignment. |
---|
| 1483 | Some examples of cascade assignment are: |
---|
| 1484 | \begin{lstlisting} |
---|
| 1485 | x1 = y1 = x2 = y2 = 0; |
---|
| 1486 | [ x1, y1 ] = [ x2, y2 ] = [ x3, y3 ]; |
---|
| 1487 | [ x1, y1 ] = [ x2, y2 ] = 0; |
---|
| 1488 | [ x1, y1 ] = z = 0; |
---|
| 1489 | \end{lstlisting} |
---|
| 1490 | As in C, the rightmost assignment is performed first, i.e., assignment parses right to left. |
---|
| 1491 | |
---|
| 1492 | |
---|
[4096de0] | 1493 | \section{Unnamed Structure Fields} |
---|
| 1494 | |
---|
[c45170a] | 1495 | C requires each field of a structure to have a name, except for a bit field associated with a basic type, \eg: |
---|
[4096de0] | 1496 | \begin{lstlisting} |
---|
| 1497 | struct { |
---|
[c45170a] | 1498 | int f1; §\C{// named field}§ |
---|
| 1499 | int f2 : 4; §\C{// named field with bit field size}§ |
---|
| 1500 | int : 3; §\C{// unnamed field for basic type with bit field size}§ |
---|
| 1501 | int ; §\C{// disallowed, unnamed field}§ |
---|
| 1502 | int *; §\C{// disallowed, unnamed field}§ |
---|
| 1503 | int (*)(int); §\C{// disallowed, unnamed field}§ |
---|
[4096de0] | 1504 | }; |
---|
| 1505 | \end{lstlisting} |
---|
| 1506 | This requirement is relaxed by making the field name optional for all field declarations; therefore, all the field declarations in the example are allowed. |
---|
| 1507 | As for unnamed bit fields, an unnamed field is used for padding a structure to a particular size. |
---|
[c45170a] | 1508 | A list of unnamed fields is also supported, \eg: |
---|
[4096de0] | 1509 | \begin{lstlisting} |
---|
| 1510 | struct { |
---|
[c45170a] | 1511 | int , , ; §\C{// 3 unnamed fields}§ |
---|
[4096de0] | 1512 | } |
---|
| 1513 | \end{lstlisting} |
---|
| 1514 | |
---|
| 1515 | |
---|
[6c91065] | 1516 | \section{Field Tuples} |
---|
| 1517 | |
---|
| 1518 | Tuples may be used to select multiple fields of a record by field name. |
---|
| 1519 | Its general form is: |
---|
| 1520 | \begin{lstlisting} |
---|
[e945826] | 1521 | §\emph{expr}§ . [ §\emph{fieldlist}§ ] |
---|
| 1522 | §\emph{expr}§ -> [ §\emph{fieldlist}§ ] |
---|
[6c91065] | 1523 | \end{lstlisting} |
---|
[c45170a] | 1524 | \emph{expr} is any expression yielding a value of type record, \eg ©struct©, ©union©. |
---|
[e945826] | 1525 | Each element of \emph{ fieldlist} is an element of the record specified by \emph{expr}. |
---|
[6c91065] | 1526 | A record-field tuple may be used anywhere a tuple can be used. An example of the use of a record-field tuple is |
---|
| 1527 | the following: |
---|
| 1528 | \begin{lstlisting} |
---|
| 1529 | struct s { |
---|
| 1530 | int f1, f2; |
---|
| 1531 | char f3; |
---|
| 1532 | double f4; |
---|
| 1533 | } v; |
---|
[4096de0] | 1534 | v.[ f3, f1, f2 ] = ['x', 11, 17 ]; §\C{// equivalent to v.f3 = 'x', v.f1 = 11, v.f2 = 17}§ |
---|
| 1535 | f( v.[ f3, f1, f2 ] ); §\C{// equivalent to f( v.f3, v.f1, v.f2 )}§ |
---|
[6c91065] | 1536 | \end{lstlisting} |
---|
| 1537 | Note, the fields appearing in a record-field tuple may be specified in any order; |
---|
| 1538 | also, it is unnecessary to specify all the fields of a struct in a multiple record-field tuple. |
---|
| 1539 | |
---|
[0638c44] | 1540 | 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: |
---|
[6c91065] | 1541 | \begin{lstlisting} |
---|
| 1542 | struct inner { |
---|
| 1543 | int f2, f3; |
---|
| 1544 | }; |
---|
| 1545 | struct outer { |
---|
| 1546 | int f1; |
---|
| 1547 | struct inner i; |
---|
| 1548 | double f4; |
---|
| 1549 | } o; |
---|
| 1550 | |
---|
| 1551 | o.[ f1, i.[ f2, f3 ], f4 ] = [ 11, 12, 13, 3.14159 ]; |
---|
| 1552 | \end{lstlisting} |
---|
| 1553 | |
---|
| 1554 | |
---|
[4096de0] | 1555 | \section{Labelled Continue / Break} |
---|
[6c91065] | 1556 | |
---|
[4096de0] | 1557 | While C provides ©continue© and ©break© statements for altering control flow, both are restricted to one level of nesting for a particular control structure. |
---|
| 1558 | Unfortunately, this restriction forces programmers to use ©goto© to achieve the equivalent control-flow for more than one level of nesting. |
---|
| 1559 | To prevent having to switch to the ©goto©, \CFA extends the ©continue©\index{continue@©continue©}\index{continue@©continue©!labelled}\index{labelled!continue@©continue©} and ©break©\index{break@©break©}\index{break@©break©!labelled}\index{labelled!break@©break©} with a target label to support static multi-level exit\index{multi-level exit}\index{static multi-level exit}~\cite{Buhr85,Java}. |
---|
| 1560 | For both ©continue© and ©break©, the target label must be directly associated with a ©for©, ©while© or ©do© statement; |
---|
| 1561 | for ©break©, the target label can also be associated with a ©switch©, ©if© or compound (©{}©) statement. |
---|
[e229c22] | 1562 | |
---|
[4096de0] | 1563 | The following example shows the labelled ©continue© specifying which control structure is the target for the next loop iteration: |
---|
| 1564 | \begin{quote2} |
---|
[45576af9] | 1565 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 1566 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
[6c91065] | 1567 | \begin{lstlisting} |
---|
[4096de0] | 1568 | ®L1:® do { |
---|
| 1569 | ®L2:® while ( ... ) { |
---|
[e945826] | 1570 | ®L3:® for ( ... ) { |
---|
[4096de0] | 1571 | ... continue ®L1®; ... // continue do |
---|
| 1572 | ... continue ®L2®; ... // continue while |
---|
| 1573 | ... continue ®L3®; ... // continue for |
---|
| 1574 | } // for |
---|
| 1575 | } // while |
---|
| 1576 | } while ( ... ); |
---|
[6c91065] | 1577 | \end{lstlisting} |
---|
| 1578 | & |
---|
| 1579 | \begin{lstlisting} |
---|
[4096de0] | 1580 | do { |
---|
| 1581 | while ( ... ) { |
---|
[6c91065] | 1582 | for ( ... ) { |
---|
| 1583 | ... goto L1; ... |
---|
| 1584 | ... goto L2; ... |
---|
[4096de0] | 1585 | ... goto L3; ... |
---|
| 1586 | L3: ; } |
---|
| 1587 | L2: ; } |
---|
| 1588 | L1: ; } while ( ... ); |
---|
[6c91065] | 1589 | \end{lstlisting} |
---|
| 1590 | \end{tabular} |
---|
[4096de0] | 1591 | \end{quote2} |
---|
| 1592 | The innermost loop has three restart points, which cause the next loop iteration to begin. |
---|
[e229c22] | 1593 | |
---|
[4096de0] | 1594 | The following example shows the labelled ©break© specifying which control structure is the target for exit: |
---|
| 1595 | \begin{quote2} |
---|
[45576af9] | 1596 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 1597 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{C}} \\ |
---|
[6c91065] | 1598 | \begin{lstlisting} |
---|
[4096de0] | 1599 | ®L1:® { |
---|
| 1600 | ... §declarations§ ... |
---|
| 1601 | ®L2:® switch ( ... ) { |
---|
| 1602 | case 3: |
---|
| 1603 | ®L3:® if ( ... ) { |
---|
| 1604 | ®L4:® for ( ... ) { |
---|
| 1605 | ... break ®L1®; ... // exit compound statement |
---|
| 1606 | ... break ®L2®; ... // exit switch |
---|
| 1607 | ... break ®L3®; ... // exit if |
---|
| 1608 | ... break ®L4®; ... // exit loop |
---|
| 1609 | } // for |
---|
| 1610 | } else { |
---|
| 1611 | ... break ®L3®; ... // exit if |
---|
| 1612 | } // if |
---|
| 1613 | } // switch |
---|
| 1614 | } // compound |
---|
[6c91065] | 1615 | \end{lstlisting} |
---|
| 1616 | & |
---|
| 1617 | \begin{lstlisting} |
---|
[4096de0] | 1618 | { |
---|
| 1619 | ... §declarations§ ... |
---|
| 1620 | switch ( ... ) { |
---|
| 1621 | case 3: |
---|
| 1622 | if ( ... ) { |
---|
| 1623 | for ( ... ) { |
---|
| 1624 | ... goto L1; ... |
---|
| 1625 | ... goto L2; ... |
---|
| 1626 | ... goto L3; ... |
---|
| 1627 | ... goto L4; ... |
---|
| 1628 | } L4: ; |
---|
| 1629 | } else { |
---|
| 1630 | ... goto L3; ... |
---|
| 1631 | } L3: ; |
---|
| 1632 | } L2: ; |
---|
| 1633 | } L1: ; |
---|
[6c91065] | 1634 | \end{lstlisting} |
---|
| 1635 | \end{tabular} |
---|
[4096de0] | 1636 | \end{quote2} |
---|
| 1637 | The innermost loop has four exit points, which cause termination of one or more of the four \Index{nested control structure}s. |
---|
[e229c22] | 1638 | |
---|
[4096de0] | 1639 | Both ©continue© and ©break© with target labels are simply a ©goto©\index{goto@©goto©!restricted} restricted in the following ways: |
---|
[6c91065] | 1640 | \begin{itemize} |
---|
| 1641 | \item |
---|
| 1642 | They cannot be used to create a loop. |
---|
| 1643 | This means that only the looping construct can be used to create a loop. |
---|
| 1644 | This restriction is important since all situations that can result in repeated execution of statements in a program are clearly delineated. |
---|
| 1645 | \item |
---|
[4096de0] | 1646 | Since they always transfer out of containing control structures, they cannot be used to branch into a control structure. |
---|
[6c91065] | 1647 | \end{itemize} |
---|
[4096de0] | 1648 | 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. |
---|
| 1649 | 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. |
---|
| 1650 | With ©goto©, the label is at the end of the control structure, which fails to convey this important clue early enough to the reader. |
---|
| 1651 | 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. |
---|
| 1652 | The implicit targets of the current ©continue© and ©break©, i.e., the closest enclosing loop or ©switch©, change as certain constructs are added or removed. |
---|
[6c91065] | 1653 | |
---|
| 1654 | |
---|
| 1655 | \section{Switch Statement} |
---|
| 1656 | |
---|
[0638c44] | 1657 | C allows a number of questionable forms for the ©switch© statement: |
---|
[6c91065] | 1658 | \begin{enumerate} |
---|
| 1659 | \item |
---|
[0638c44] | 1660 | By default, the end of a ©case© clause\footnote{ |
---|
| 1661 | In this section, the term \emph{case clause} refers to either a ©case© or ©default© clause.} |
---|
| 1662 | \emph{falls through} to the next ©case© clause in the ©switch© statement; |
---|
[4096de0] | 1663 | to exit a ©switch© statement from a ©case© clause requires explicitly terminating the clause with a transfer statement, most commonly ©break©: |
---|
[6c91065] | 1664 | \begin{lstlisting} |
---|
| 1665 | switch ( i ) { |
---|
| 1666 | case 1: |
---|
| 1667 | ... |
---|
| 1668 | // fall-through |
---|
| 1669 | case 2: |
---|
| 1670 | ... |
---|
| 1671 | break; // exit switch statement |
---|
| 1672 | } |
---|
| 1673 | \end{lstlisting} |
---|
[4096de0] | 1674 | 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: |
---|
[5479e63] | 1675 | \begin{quote2} |
---|
| 1676 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
[6c91065] | 1677 | \begin{lstlisting} |
---|
| 1678 | switch ( argc ) { |
---|
| 1679 | case 3: |
---|
| 1680 | // open output file |
---|
| 1681 | // fall-through |
---|
| 1682 | case 2: |
---|
| 1683 | // open input file |
---|
| 1684 | break; // exit switch statement |
---|
| 1685 | default: |
---|
| 1686 | // usage message |
---|
| 1687 | } |
---|
| 1688 | \end{lstlisting} |
---|
[5479e63] | 1689 | & |
---|
| 1690 | \begin{lstlisting} |
---|
| 1691 | |
---|
| 1692 | if ( argc == 3 ) { |
---|
| 1693 | // open output file |
---|
| 1694 | ®// open input file |
---|
| 1695 | ®} else if ( argc == 2 ) { |
---|
| 1696 | ®// open input file |
---|
| 1697 | |
---|
| 1698 | ®} else { |
---|
| 1699 | // usage message |
---|
| 1700 | } |
---|
| 1701 | \end{lstlisting} |
---|
| 1702 | \end{tabular} |
---|
| 1703 | \end{quote2} |
---|
[6c91065] | 1704 | In this example, case 2 is always done if case 3 is done. |
---|
[0638c44] | 1705 | 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. |
---|
[4096de0] | 1706 | C also uses fall-through to handle multiple case-values resulting in the same action: |
---|
[6c91065] | 1707 | \begin{lstlisting} |
---|
| 1708 | switch ( i ) { |
---|
| 1709 | case 1: case 3: case 5: // odd values |
---|
| 1710 | // same action |
---|
| 1711 | break; |
---|
| 1712 | case 2: case 4: case 6: // even values |
---|
| 1713 | // same action |
---|
| 1714 | break; |
---|
| 1715 | } |
---|
| 1716 | \end{lstlisting} |
---|
| 1717 | However, this situation is handled in other languages without fall-through by allowing a list of case values. |
---|
[5479e63] | 1718 | 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. |
---|
[4096de0] | 1719 | 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. |
---|
[6c91065] | 1720 | |
---|
| 1721 | \item |
---|
[4096de0] | 1722 | It is possible to place ©case© clauses on statements nested \emph{within} the body of the ©switch© statement: |
---|
[6c91065] | 1723 | \begin{lstlisting} |
---|
| 1724 | switch ( i ) { |
---|
| 1725 | case 0: |
---|
| 1726 | if ( j < k ) { |
---|
| 1727 | ... |
---|
[5479e63] | 1728 | ®case 1:® // transfer into "if" statement |
---|
| 1729 | ... |
---|
| 1730 | } // if |
---|
| 1731 | case 2: |
---|
[6c91065] | 1732 | while ( j < 5 ) { |
---|
| 1733 | ... |
---|
[5479e63] | 1734 | ®case 3:® // transfer into "while" statement |
---|
[6c91065] | 1735 | ... |
---|
[5479e63] | 1736 | } // while |
---|
| 1737 | } // switch |
---|
[6c91065] | 1738 | \end{lstlisting} |
---|
| 1739 | The problem with this usage is branching into control structures, which is known to cause both comprehension and technical difficulties. |
---|
| 1740 | The comprehension problem occurs from the inability to determine how control reaches a particular point due to the number of branches leading to it. |
---|
| 1741 | The technical problem results from the inability to ensure allocation and initialization of variables when blocks are not entered at the beginning. |
---|
| 1742 | Often transferring into a block can bypass variable declaration and/or its initialization, which results in subsequent errors. |
---|
| 1743 | There are virtually no positive arguments for this kind of control flow, and therefore, there is a strong impetus to eliminate it. |
---|
[07bc165] | 1744 | Nevertheless, C does have an idiom where this capability is used, known as ``\Index*{Duff's device}''~\cite{Duff83}: |
---|
[6c91065] | 1745 | \begin{lstlisting} |
---|
| 1746 | register int n = (count + 7) / 8; |
---|
| 1747 | switch ( count % 8 ) { |
---|
| 1748 | case 0: do{ *to = *from++; |
---|
| 1749 | case 7: *to = *from++; |
---|
| 1750 | case 6: *to = *from++; |
---|
| 1751 | case 5: *to = *from++; |
---|
| 1752 | case 4: *to = *from++; |
---|
| 1753 | case 3: *to = *from++; |
---|
| 1754 | case 2: *to = *from++; |
---|
| 1755 | case 1: *to = *from++; |
---|
| 1756 | } while ( --n > 0 ); |
---|
| 1757 | } |
---|
| 1758 | \end{lstlisting} |
---|
[0638c44] | 1759 | which unrolls a loop N times (N = 8 above) and uses the ©switch© statement to deal with any iterations not a multiple of N. |
---|
[6c91065] | 1760 | While efficient, this sort of special purpose usage is questionable: |
---|
| 1761 | \begin{quote} |
---|
| 1762 | Disgusting, no? But it compiles and runs just fine. I feel a combination of pride and revulsion at this |
---|
| 1763 | discovery.~\cite{Duff83} |
---|
| 1764 | \end{quote} |
---|
| 1765 | \item |
---|
[0638c44] | 1766 | 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. |
---|
| 1767 | Virtually all programming languages with a ©switch© statement require the ©default© clause to appear last in the case-clause list. |
---|
| 1768 | The logic for this semantics is that after checking all the ©case© clauses without success, the ©default© clause is selected; |
---|
| 1769 | hence, physically placing the ©default© clause at the end of the ©case© clause list matches with this semantics. |
---|
| 1770 | 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. |
---|
[6c91065] | 1771 | |
---|
| 1772 | \item |
---|
[0638c44] | 1773 | It is possible to place unreachable code at the start of a ©switch© statement, as in: |
---|
[6c91065] | 1774 | \begin{lstlisting} |
---|
| 1775 | switch ( x ) { |
---|
[5479e63] | 1776 | ®int y = 1;® §\C{// unreachable initialization}§ |
---|
| 1777 | ®x = 7;® §\C{// unreachable code without label/branch}§ |
---|
[6c91065] | 1778 | case 3: ... |
---|
| 1779 | ... |
---|
[5479e63] | 1780 | ®int z = 0;® §\C{// unreachable initialization, cannot appear after case}§ |
---|
| 1781 | z = 2; |
---|
| 1782 | case 3: |
---|
| 1783 | ®x = z;® §\C{// without fall through, z is uninitialized}§ |
---|
[6c91065] | 1784 | } |
---|
| 1785 | \end{lstlisting} |
---|
[c45170a] | 1786 | 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. |
---|
| 1787 | 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. |
---|
| 1788 | 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. |
---|
| 1789 | The key observation is that the ©switch© statement branches into control structure, i.e., there are multiple entry points into its statement body. |
---|
[6c91065] | 1790 | \end{enumerate} |
---|
[5479e63] | 1791 | |
---|
[6c91065] | 1792 | Before discussing potential language changes to deal with these problems, it is worth observing that in a typical C program: |
---|
| 1793 | \begin{itemize} |
---|
| 1794 | \item |
---|
[0638c44] | 1795 | the number of ©switch© statements is small, |
---|
[6c91065] | 1796 | \item |
---|
[07bc165] | 1797 | most ©switch© statements are well formed (i.e., no \Index*{Duff's device}), |
---|
[6c91065] | 1798 | \item |
---|
[0638c44] | 1799 | the ©default© clause is usually written as the last case-clause, |
---|
[6c91065] | 1800 | \item |
---|
[0638c44] | 1801 | 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. |
---|
[6c91065] | 1802 | \end{itemize} |
---|
[08061589] | 1803 | These observations help to put the \CFA changes to the ©switch© into perspective. |
---|
[6c91065] | 1804 | \begin{enumerate} |
---|
| 1805 | \item |
---|
[4096de0] | 1806 | Eliminating default fall-through has the greatest potential for affecting existing code. |
---|
[c45170a] | 1807 | 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, i.e., a list of ©case© clauses executing common code, \eg: |
---|
| 1808 | \begin{lstlisting} |
---|
[6c91065] | 1809 | case 1: case 2: case 3: ... |
---|
| 1810 | \end{lstlisting} |
---|
[4096de0] | 1811 | still work. |
---|
[6c91065] | 1812 | 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. |
---|
[182fe1e] | 1813 | <<<<<<< HEAD |
---|
[c45170a] | 1814 | 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 ©fallthru©, \eg: |
---|
[182fe1e] | 1815 | ======= |
---|
[08061589] | 1816 | Therefore, to preserve backwards compatibility, it is necessary to introduce a new kind of ©switch© statement, called ©choose©, with no implicit fall-through semantics and an explicit fall-through if the last statement of a case-clause ends with the new keyword ©fallthrough©/©fallthru©, e.g.: |
---|
[182fe1e] | 1817 | >>>>>>> 080615890f586cb9954c252b55cab47f52c25758 |
---|
[6c91065] | 1818 | \begin{lstlisting} |
---|
[4096de0] | 1819 | ®choose® ( i ) { |
---|
[5479e63] | 1820 | case 1: case 2: case 3: |
---|
| 1821 | ... |
---|
| 1822 | ®// implicit end of switch (break) |
---|
| 1823 | ®case 5: |
---|
[4096de0] | 1824 | ... |
---|
[5479e63] | 1825 | ®fallthru®; §\C{// explicit fall through}§ |
---|
[4096de0] | 1826 | case 7: |
---|
| 1827 | ... |
---|
[5479e63] | 1828 | ®break® §\C{// explicit end of switch}§ |
---|
[6c91065] | 1829 | default: |
---|
| 1830 | j = 3; |
---|
| 1831 | } |
---|
| 1832 | \end{lstlisting} |
---|
[5479e63] | 1833 | Like the ©switch© statement, the ©choose© statement retains the fall-through semantics for a list of ©case© clauses; |
---|
| 1834 | the implicit ©break© is applied only at the end of the \emph{statements} following a ©case© clause. |
---|
| 1835 | 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. |
---|
| 1836 | As well, allowing an explicit ©break© from the ©choose© is a carry over from the ©switch© statement, and expected by C programmers. |
---|
[6c91065] | 1837 | \item |
---|
[5479e63] | 1838 | \Index*{Duff's device} is eliminated from both ©switch© and ©choose© statements, and only invalidates a small amount of very questionable code. |
---|
| 1839 | 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. |
---|
[6c91065] | 1840 | \item |
---|
[4096de0] | 1841 | 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. |
---|
[5479e63] | 1842 | Therefore, no change is made for this issue. |
---|
[6c91065] | 1843 | \item |
---|
[c45170a] | 1844 | 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{ |
---|
| 1845 | 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. |
---|
| 1846 | Further declarations at the same nesting level as the statement body are disallowed to ensure every transfer into the body is sound. |
---|
[6c91065] | 1847 | \begin{lstlisting} |
---|
[5479e63] | 1848 | switch ( x ) { |
---|
[c45170a] | 1849 | ®int i = 0;® §\C{// allowed only at start}§ |
---|
[6c91065] | 1850 | case 0: |
---|
| 1851 | ... |
---|
[c45170a] | 1852 | ®int j = 0;® §\C{// disallowed}§ |
---|
[5479e63] | 1853 | case 1: |
---|
| 1854 | { |
---|
[c45170a] | 1855 | ®int k = 0;® §\C{// allowed at different nesting levels}§ |
---|
[5479e63] | 1856 | ... |
---|
| 1857 | } |
---|
| 1858 | ... |
---|
[6c91065] | 1859 | } |
---|
| 1860 | \end{lstlisting} |
---|
| 1861 | \end{enumerate} |
---|
| 1862 | |
---|
| 1863 | |
---|
| 1864 | \section{Case Clause} |
---|
| 1865 | |
---|
[0638c44] | 1866 | C restricts the ©case© clause of a ©switch© statement to a single value. |
---|
| 1867 | For multiple ©case© clauses associated with the same statement, it is necessary to have multiple ©case© clauses rather than multiple values. |
---|
| 1868 | Requiring a ©case© clause for each value does not seem to be in the spirit of brevity normally associated with C. |
---|
| 1869 | Therefore, the ©case© clause is extended with a list of values, as in: |
---|
[6c91065] | 1870 | \begin{quote2} |
---|
[45576af9] | 1871 | \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} |
---|
| 1872 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{C}} \\ |
---|
[6c91065] | 1873 | \begin{lstlisting} |
---|
| 1874 | switch ( i ) { |
---|
[e945826] | 1875 | ®case 1, 3, 5®: |
---|
[6c91065] | 1876 | ... |
---|
[e945826] | 1877 | ®case 2, 4, 6®: |
---|
[6c91065] | 1878 | ... |
---|
| 1879 | } |
---|
| 1880 | \end{lstlisting} |
---|
| 1881 | & |
---|
| 1882 | \begin{lstlisting} |
---|
| 1883 | switch ( i ) { |
---|
| 1884 | case 1: case 3 : case 5: |
---|
| 1885 | ... |
---|
[53ba273] | 1886 | case 2: case 4 : case 6: |
---|
[6c91065] | 1887 | ... |
---|
| 1888 | } |
---|
| 1889 | \end{lstlisting} |
---|
| 1890 | & |
---|
| 1891 | \begin{lstlisting} |
---|
| 1892 | |
---|
| 1893 | // odd values |
---|
| 1894 | |
---|
| 1895 | // even values |
---|
| 1896 | |
---|
| 1897 | |
---|
| 1898 | \end{lstlisting} |
---|
| 1899 | \end{tabular} |
---|
| 1900 | \end{quote2} |
---|
[4096de0] | 1901 | In addition, two forms of subranges are allowed to specify case values: a new \CFA form and an existing GNU C form.\footnote{ |
---|
| 1902 | The GNU C form \emph{requires} spaces around the ellipse.} |
---|
[6c91065] | 1903 | \begin{quote2} |
---|
[45576af9] | 1904 | \begin{tabular}{@{}l@{\hspace{3em}}l@{\hspace{2em}}l@{}} |
---|
| 1905 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c@{\hspace{2em}}}{\textbf{GNU C}} \\ |
---|
[6c91065] | 1906 | \begin{lstlisting} |
---|
| 1907 | switch ( i ) { |
---|
[e945826] | 1908 | ®case 1~5:® |
---|
[6c91065] | 1909 | ... |
---|
[e945826] | 1910 | ®case 10~15:® |
---|
[6c91065] | 1911 | ... |
---|
| 1912 | } |
---|
| 1913 | \end{lstlisting} |
---|
| 1914 | & |
---|
| 1915 | \begin{lstlisting} |
---|
| 1916 | switch ( i ) |
---|
| 1917 | case 1 ... 5: |
---|
| 1918 | ... |
---|
| 1919 | case 10 ... 15: |
---|
| 1920 | ... |
---|
| 1921 | } |
---|
| 1922 | \end{lstlisting} |
---|
| 1923 | & |
---|
| 1924 | \begin{lstlisting} |
---|
[53ba273] | 1925 | |
---|
[6c91065] | 1926 | // 1, 2, 3, 4, 5 |
---|
| 1927 | |
---|
| 1928 | // 10, 11, 12, 13, 14, 15 |
---|
| 1929 | |
---|
| 1930 | |
---|
| 1931 | \end{lstlisting} |
---|
| 1932 | \end{tabular} |
---|
| 1933 | \end{quote2} |
---|
[4096de0] | 1934 | Lists of subranges are also allowed. |
---|
[6c91065] | 1935 | \begin{lstlisting} |
---|
[4096de0] | 1936 | case ®1~5, 12~21, 35~42®: |
---|
[6c91065] | 1937 | \end{lstlisting} |
---|
| 1938 | |
---|
| 1939 | |
---|
| 1940 | \section{Exception Handling} |
---|
| 1941 | |
---|
[5479e63] | 1942 | Exception handling provides two mechanism: change of control flow from a raise to a handler, and communication from the raise to the handler. |
---|
[6c91065] | 1943 | \begin{lstlisting} |
---|
| 1944 | exception void h( int i ); |
---|
| 1945 | exception int h( int i, double d ); |
---|
| 1946 | |
---|
| 1947 | void f(...) { |
---|
| 1948 | ... throw h( 3 ); |
---|
| 1949 | ... i = resume h( 3, 5.1 ); |
---|
| 1950 | } |
---|
| 1951 | |
---|
| 1952 | try { |
---|
| 1953 | f(...); |
---|
| 1954 | } catch h( int w ) { |
---|
| 1955 | // reset |
---|
| 1956 | } resume h( int p, double x ) { |
---|
| 1957 | return 17; // recover |
---|
| 1958 | } finally { |
---|
| 1959 | } |
---|
| 1960 | \end{lstlisting} |
---|
| 1961 | So the type raised would be the mangled name of the exception prototype and that name would be matched at the handler clauses by comparing the strings. |
---|
| 1962 | The arguments for the call would have to be packed in a message and unpacked at handler clause and then a call made to the handler. |
---|
| 1963 | |
---|
| 1964 | |
---|
| 1965 | \section{Types} |
---|
| 1966 | |
---|
| 1967 | \subsection{Type Definitions} |
---|
| 1968 | |
---|
| 1969 | \CFA allows users to define new types using the keyword type. |
---|
| 1970 | |
---|
| 1971 | \begin{lstlisting} |
---|
| 1972 | // SensorValue is a distinct type and represented as an int |
---|
| 1973 | type SensorValue = int; |
---|
| 1974 | \end{lstlisting} |
---|
| 1975 | |
---|
| 1976 | A type definition is different from a typedef in C because a typedef just creates an alias for a type, while Do.s type definition creates a distinct type. |
---|
| 1977 | This means that users can define distinct function overloads for the new type (see Overloading for more information). |
---|
| 1978 | For example: |
---|
| 1979 | |
---|
| 1980 | \begin{lstlisting} |
---|
| 1981 | type SensorValue = int; |
---|
| 1982 | void printValue(int v) {...} |
---|
| 1983 | void printValue(SensorValue v) {...} |
---|
| 1984 | void process(int v) {...} |
---|
| 1985 | |
---|
| 1986 | SensorValue s = ...; |
---|
| 1987 | |
---|
| 1988 | printValue(s); // calls version with SensorValue argument |
---|
| 1989 | |
---|
| 1990 | printValue((int) s); // calls version with int argument |
---|
| 1991 | |
---|
| 1992 | process(s); // implicit conversion to int |
---|
| 1993 | \end{lstlisting} |
---|
| 1994 | |
---|
| 1995 | If SensorValue was defined with a typedef, then these two print functions would not have unique signatures. |
---|
| 1996 | This can be very useful to create a distinct type that has the same representation as another type. |
---|
| 1997 | |
---|
| 1998 | The compiler will assume it can safely convert from the old type to the new type, implicitly. |
---|
| 1999 | Users may override this and define a function that must be called to convert from one type to another. |
---|
| 2000 | |
---|
| 2001 | \begin{lstlisting} |
---|
| 2002 | type SensorValue = int; |
---|
| 2003 | // ()? is the overloaded conversion operator identifier |
---|
| 2004 | // This function converts an int to a SensorValue |
---|
| 2005 | SensorValue ()?(int val) { |
---|
| 2006 | ... |
---|
| 2007 | } |
---|
| 2008 | void process(int v) {...} |
---|
| 2009 | |
---|
| 2010 | SensorValue s = ...; |
---|
| 2011 | process(s); // implicit call to conversion operator |
---|
| 2012 | \end{lstlisting} |
---|
| 2013 | |
---|
| 2014 | In many cases, it is not desired for the compiler to do this implicit conversion. |
---|
| 2015 | To avoid that, the user can use the explicit modifier on the conversion operator. |
---|
| 2016 | Any places where the conversion is needed but not explicit (with a cast), will result in a compile-time error. |
---|
| 2017 | |
---|
| 2018 | \begin{lstlisting} |
---|
| 2019 | type SensorValue = int; |
---|
| 2020 | |
---|
| 2021 | // conversion from int to SensorValue; must be explicit |
---|
| 2022 | explicit SensorValue ()?(int val) { |
---|
| 2023 | ... |
---|
| 2024 | } |
---|
| 2025 | |
---|
| 2026 | void process(int v) {...} |
---|
| 2027 | |
---|
| 2028 | SensorValue s = ...; |
---|
| 2029 | process(s); // implicit cast to int: compile-time error |
---|
| 2030 | process((int) s); // explicit cast to int: calls conversion func |
---|
| 2031 | \end{lstlisting} |
---|
| 2032 | |
---|
| 2033 | The conversion may not require any code, but still need to be explicit; in that case, the syntax can be simplified to: |
---|
| 2034 | \begin{lstlisting} |
---|
| 2035 | type SensorValue = int; |
---|
| 2036 | explicit SensorValue ()?(int); |
---|
| 2037 | void process(int v) {...} |
---|
| 2038 | |
---|
| 2039 | SensorValue s = ...; |
---|
| 2040 | process(s); // compile-time error |
---|
| 2041 | process((int) s); // type is converted, no function is called |
---|
| 2042 | \end{lstlisting} |
---|
| 2043 | |
---|
| 2044 | |
---|
| 2045 | \subsection{Structures} |
---|
| 2046 | |
---|
| 2047 | Structures in \CFA are basically the same as structures in C. |
---|
| 2048 | A structure is defined with the same syntax as in C. |
---|
| 2049 | When referring to a structure in \CFA, users may omit the struct keyword. |
---|
| 2050 | \begin{lstlisting} |
---|
| 2051 | struct Point { |
---|
| 2052 | double x; |
---|
| 2053 | double y; |
---|
| 2054 | }; |
---|
| 2055 | |
---|
| 2056 | Point p = {0.0, 0.0}; |
---|
| 2057 | \end{lstlisting} |
---|
| 2058 | |
---|
| 2059 | \CFA does not support inheritance among types, but instead uses composition to enable reuse of structure fields. |
---|
| 2060 | Composition is achieved by embedding one type into another. |
---|
| 2061 | When type A is embedded in type B, an object with type B may be used as an object of type A, and the fields of type A are directly accessible. |
---|
| 2062 | Embedding types is achieved using anonymous members. |
---|
| 2063 | For example, using Point from above: |
---|
| 2064 | \begin{lstlisting} |
---|
| 2065 | void foo(Point p); |
---|
| 2066 | |
---|
| 2067 | struct ColoredPoint { |
---|
| 2068 | Point; // anonymous member (no identifier) |
---|
| 2069 | int Color; |
---|
| 2070 | }; |
---|
| 2071 | ... |
---|
| 2072 | ColoredPoint cp = ...; |
---|
| 2073 | cp.x = 10.3; // x from Point is accessed directly |
---|
| 2074 | cp.color = 0x33aaff; // color is accessed normally |
---|
| 2075 | foo(cp); // cp can be used directly as a Point |
---|
| 2076 | \end{lstlisting} |
---|
| 2077 | |
---|
| 2078 | |
---|
| 2079 | \subsection{Constructors and Destructors} |
---|
| 2080 | |
---|
| 2081 | \CFA supports C initialization of structures, but it also adds constructors for more advanced initialization. |
---|
| 2082 | Additionally, \CFA adds destructors that are called when a variable is de-allocated (variable goes out of scope or object is deleted). |
---|
[07bc165] | 2083 | These functions take a reference to the structure as a parameter (see References for more information). |
---|
[6c91065] | 2084 | |
---|
| 2085 | \begin{figure} |
---|
| 2086 | \begin{lstlisting} |
---|
| 2087 | struct Widget { |
---|
| 2088 | int id; |
---|
| 2089 | float size; |
---|
| 2090 | Parts *optionalParts; |
---|
| 2091 | }; |
---|
| 2092 | |
---|
| 2093 | // ?{} is the constructor operator identifier |
---|
| 2094 | // The first argument is a reference to the type to initialize |
---|
| 2095 | // Subsequent arguments can be specified for initialization |
---|
| 2096 | |
---|
| 2097 | void ?{}(Widget &w) { // default constructor |
---|
| 2098 | w.id = -1; |
---|
| 2099 | w.size = 0.0; |
---|
| 2100 | w.optionalParts = 0; |
---|
| 2101 | } |
---|
| 2102 | |
---|
| 2103 | // constructor with values (does not need to include all fields) |
---|
| 2104 | void ?{}(Widget &w, int id, float size) { |
---|
| 2105 | w.id = id; |
---|
| 2106 | w.size = size; |
---|
| 2107 | w.optionalParts = 0; |
---|
| 2108 | } |
---|
| 2109 | |
---|
| 2110 | // ^? is the destructor operator identifier |
---|
| 2111 | void ^?(Widget &w) { // destructor |
---|
| 2112 | w.id = 0; |
---|
| 2113 | w.size = 0.0; |
---|
| 2114 | if (w.optionalParts != 0) { |
---|
| 2115 | // This is the only pointer to optionalParts, free it |
---|
| 2116 | free(w.optionalParts); |
---|
| 2117 | w.optionalParts = 0; |
---|
| 2118 | } |
---|
| 2119 | } |
---|
| 2120 | |
---|
| 2121 | Widget baz; // reserve space only |
---|
| 2122 | Widget foo{}; // calls default constructor |
---|
| 2123 | Widget bar{23, 2.45}; // calls constructor with values |
---|
| 2124 | baz{24, 0.91}; // calls constructor with values |
---|
| 2125 | ?{}(baz, 24, 0.91}; // explicit call to constructor |
---|
| 2126 | ^bar; // explicit call to destructor |
---|
| 2127 | ^?(bar); // explicit call to destructor |
---|
| 2128 | \end{lstlisting} |
---|
| 2129 | \caption{Constructors and Destructors} |
---|
| 2130 | \end{figure} |
---|
| 2131 | |
---|
| 2132 | |
---|
| 2133 | \begin{comment} |
---|
| 2134 | \section{References} |
---|
| 2135 | |
---|
[07bc165] | 2136 | |
---|
[6c91065] | 2137 | By introducing references in parameter types, users are given an easy way to pass a value by reference, without the need for NULL pointer checks. |
---|
| 2138 | In structures, a reference can replace a pointer to an object that should always have a valid value. |
---|
| 2139 | When a structure contains a reference, all of its constructors must initialize the reference and all instances of this structure must initialize it upon definition. |
---|
| 2140 | |
---|
| 2141 | The syntax for using references in \CFA is the same as \CC with the exception of reference initialization. |
---|
[0638c44] | 2142 | Use ©&© to specify a reference, and access references just like regular objects, not like pointers (use dot notation to access fields). |
---|
[6c91065] | 2143 | When initializing a reference, \CFA uses a different syntax which differentiates reference initialization from assignment to a reference. |
---|
[0638c44] | 2144 | The ©&© is used on both sides of the expression to clarify that the address of the reference is being set to the address of the variable to which it refers. |
---|
[6c91065] | 2145 | \end{comment} |
---|
| 2146 | |
---|
| 2147 | |
---|
| 2148 | \section{Overloading} |
---|
| 2149 | |
---|
| 2150 | Overloading refers to the capability of a programmer to define and use multiple objects in a program with the same name. |
---|
| 2151 | In \CFA, a declaration may overload declarations from outer scopes with the same name, instead of hiding them as is the case in C. |
---|
| 2152 | This may cause identical C and \CFA programs to behave differently. |
---|
| 2153 | The compiler selects the appropriate object (overload resolution) based on context information at the place where it is used. |
---|
| 2154 | Overloading allows programmers to give functions with different signatures but similar semantics the same name, simplifying the interface to users. |
---|
| 2155 | Disadvantages of overloading are that it can be used to give functions with different semantics the same name, causing confusion, or that the compiler may resolve to a different function from what the programmer expected. |
---|
| 2156 | \CFA allows overloading of functions, operators, variables, and even the constants 0 and 1. |
---|
| 2157 | |
---|
| 2158 | The compiler follows some overload resolution rules to determine the best interpretation of all of these overloads. |
---|
| 2159 | The best valid interpretations are the valid interpretations that use the fewest unsafe conversions. |
---|
| 2160 | Of these, the best are those where the functions and objects involved are the least polymorphic. |
---|
| 2161 | Of these, the best have the lowest total conversion cost, including all implicit conversions in the argument expressions. |
---|
| 2162 | Of these, the best have the highest total conversion cost for the implicit conversions (if any) applied to the argument expressions. |
---|
| 2163 | If there is no single best valid interpretation, or if the best valid interpretation is ambiguous, then the resulting interpretation is ambiguous. |
---|
| 2164 | For details about type inference and overload resolution, please see the \CFA Language Specification. |
---|
| 2165 | \begin{lstlisting} |
---|
| 2166 | int foo(int a, int b) { |
---|
| 2167 | float sum = 0.0; |
---|
| 2168 | float special = 1.0; |
---|
| 2169 | { |
---|
| 2170 | int sum = 0; |
---|
| 2171 | // both the float and int versions of sum are available |
---|
| 2172 | float special = 4.0; |
---|
| 2173 | // this inner special hides the outer version |
---|
| 2174 | ... |
---|
| 2175 | } |
---|
| 2176 | ... |
---|
| 2177 | } |
---|
| 2178 | \end{lstlisting} |
---|
| 2179 | |
---|
| 2180 | |
---|
| 2181 | \subsection{Overloaded Constant} |
---|
| 2182 | |
---|
| 2183 | The constants 0 and 1 have special meaning. |
---|
| 2184 | In \CFA, as in C, all scalar types can be incremented and |
---|
| 2185 | decremented, which is defined in terms of adding or subtracting 1. |
---|
[0638c44] | 2186 | 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)©). |
---|
[6c91065] | 2187 | |
---|
[07bc165] | 2188 | 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. |
---|
| 2189 | 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. |
---|
| 2190 | Defining special constants for a user-defined type is more efficient than defining a conversion to the type from ©_Bool©. |
---|
[6c91065] | 2191 | |
---|
| 2192 | Why just 0 and 1? Why not other integers? No other integers have special status in C. |
---|
| 2193 | A facility that let programmers declare specific constants..const Rational 12., for instance. would not be much of an improvement. |
---|
| 2194 | Some facility for defining the creation of values of programmer-defined types from arbitrary integer tokens would be needed. |
---|
[5479e63] | 2195 | The complexity of such a feature does not seem worth the gain. |
---|
[6c91065] | 2196 | |
---|
| 2197 | For example, to define the constants for a complex type, the programmer would define the following: |
---|
| 2198 | |
---|
| 2199 | \begin{lstlisting} |
---|
| 2200 | struct Complex { |
---|
| 2201 | double real; |
---|
| 2202 | double imaginary; |
---|
| 2203 | } |
---|
| 2204 | |
---|
| 2205 | const Complex 0 = {0, 0}; |
---|
| 2206 | const Complex 1 = {1, 0}; |
---|
| 2207 | ... |
---|
| 2208 | |
---|
| 2209 | Complex a = 0; |
---|
| 2210 | ... |
---|
| 2211 | |
---|
| 2212 | a++; |
---|
| 2213 | ... |
---|
| 2214 | if (a) { // same as if (a == 0) |
---|
| 2215 | ... |
---|
| 2216 | } |
---|
| 2217 | \end{lstlisting} |
---|
| 2218 | |
---|
| 2219 | |
---|
| 2220 | \subsection{Variable Overloading} |
---|
| 2221 | |
---|
| 2222 | The overload rules of \CFA allow a programmer to define multiple variables with the same name, but different types. |
---|
| 2223 | Allowing overloading of variable names enables programmers to use the same name across multiple types, simplifying naming conventions and is compatible with the other overloading that is allowed. |
---|
| 2224 | For example, a developer may want to do the following: |
---|
| 2225 | \begin{lstlisting} |
---|
| 2226 | int pi = 3; |
---|
| 2227 | float pi = 3.14; |
---|
| 2228 | char pi = .p.; |
---|
| 2229 | \end{lstlisting} |
---|
| 2230 | |
---|
| 2231 | |
---|
| 2232 | \subsection{Function Overloading} |
---|
| 2233 | |
---|
| 2234 | Overloaded functions in \CFA are resolved based on the number and type of arguments, type of return value, and the level of specialization required (specialized functions are preferred over generic). |
---|
| 2235 | |
---|
| 2236 | The examples below give some basic intuition about how the resolution works. |
---|
| 2237 | \begin{lstlisting} |
---|
| 2238 | // Choose the one with less conversions |
---|
| 2239 | int doSomething(int value) {...} // option 1 |
---|
| 2240 | int doSomething(short value) {...} // option 2 |
---|
| 2241 | |
---|
| 2242 | int a, b = 4; |
---|
| 2243 | short c = 2; |
---|
| 2244 | |
---|
| 2245 | a = doSomething(b); // chooses option 1 |
---|
| 2246 | a = doSomething(c); // chooses option 2 |
---|
| 2247 | |
---|
| 2248 | // Choose the specialized version over the generic |
---|
| 2249 | |
---|
| 2250 | generic(type T) |
---|
| 2251 | T bar(T rhs, T lhs) {...} // option 3 |
---|
| 2252 | float bar(float rhs, float lhs){...} // option 4 |
---|
| 2253 | float a, b, c; |
---|
| 2254 | double d, e, f; |
---|
| 2255 | c = bar(a, b); // chooses option 4 |
---|
| 2256 | |
---|
| 2257 | // specialization is preferred over unsafe conversions |
---|
| 2258 | |
---|
| 2259 | f = bar(d, e); // chooses option 5 |
---|
| 2260 | \end{lstlisting} |
---|
| 2261 | |
---|
| 2262 | |
---|
| 2263 | \subsection{Operator Overloading} |
---|
| 2264 | |
---|
| 2265 | \CFA also allows operators to be overloaded, to simplify the use of user-defined types. |
---|
| 2266 | Overloading the operators allows the users to use the same syntax for their custom types that they use for built-in types, increasing readability and improving productivity. |
---|
| 2267 | \CFA uses the following special identifiers to name overloaded operators: |
---|
| 2268 | |
---|
| 2269 | \begin{table}[hbt] |
---|
| 2270 | \hfil |
---|
| 2271 | \begin{tabular}[t]{ll} |
---|
| 2272 | %identifier & operation \\ \hline |
---|
[0638c44] | 2273 | ©?[?]© & subscripting \impl{?[?]}\\ |
---|
| 2274 | ©?()© & function call \impl{?()}\\ |
---|
| 2275 | ©?++© & postfix increment \impl{?++}\\ |
---|
| 2276 | ©?--© & postfix decrement \impl{?--}\\ |
---|
| 2277 | ©++?© & prefix increment \impl{++?}\\ |
---|
| 2278 | ©--?© & prefix decrement \impl{--?}\\ |
---|
| 2279 | ©*?© & dereference \impl{*?}\\ |
---|
| 2280 | ©+?© & unary plus \impl{+?}\\ |
---|
| 2281 | ©-?© & arithmetic negation \impl{-?}\\ |
---|
| 2282 | ©~?© & bitwise negation \impl{~?}\\ |
---|
| 2283 | ©!?© & logical complement \impl{"!?}\\ |
---|
| 2284 | ©?*?© & multiplication \impl{?*?}\\ |
---|
| 2285 | ©?/?© & division \impl{?/?}\\ |
---|
[6c91065] | 2286 | \end{tabular}\hfil |
---|
| 2287 | \begin{tabular}[t]{ll} |
---|
| 2288 | %identifier & operation \\ \hline |
---|
[0638c44] | 2289 | ©?%?© & remainder \impl{?%?}\\ |
---|
| 2290 | ©?+?© & addition \impl{?+?}\\ |
---|
| 2291 | ©?-?© & subtraction \impl{?-?}\\ |
---|
| 2292 | ©?<<?© & left shift \impl{?<<?}\\ |
---|
| 2293 | ©?>>?© & right shift \impl{?>>?}\\ |
---|
| 2294 | ©?<?© & less than \impl{?<?}\\ |
---|
| 2295 | ©?<=?© & less than or equal \impl{?<=?}\\ |
---|
| 2296 | ©?>=?© & greater than or equal \impl{?>=?}\\ |
---|
| 2297 | ©?>?© & greater than \impl{?>?}\\ |
---|
| 2298 | ©?==?© & equality \impl{?==?}\\ |
---|
| 2299 | ©?!=?© & inequality \impl{?"!=?}\\ |
---|
| 2300 | ©?&?© & bitwise AND \impl{?&?}\\ |
---|
[6c91065] | 2301 | \end{tabular}\hfil |
---|
| 2302 | \begin{tabular}[t]{ll} |
---|
| 2303 | %identifier & operation \\ \hline |
---|
[0638c44] | 2304 | ©?^?© & exclusive OR \impl{?^?}\\ |
---|
| 2305 | ©?|?© & inclusive OR \impl{?"|?}\\ |
---|
| 2306 | ©?=?© & simple assignment \impl{?=?}\\ |
---|
| 2307 | ©?*=?© & multiplication assignment \impl{?*=?}\\ |
---|
| 2308 | ©?/=?© & division assignment \impl{?/=?}\\ |
---|
| 2309 | ©?%=?© & remainder assignment \impl{?%=?}\\ |
---|
| 2310 | ©?+=?© & addition assignment \impl{?+=?}\\ |
---|
| 2311 | ©?-=?© & subtraction assignment \impl{?-=?}\\ |
---|
| 2312 | ©?<<=?© & left-shift assignment \impl{?<<=?}\\ |
---|
| 2313 | ©?>>=?© & right-shift assignment \impl{?>>=?}\\ |
---|
| 2314 | ©?&=?© & bitwise AND assignment \impl{?&=?}\\ |
---|
| 2315 | ©?^=?© & exclusive OR assignment \impl{?^=?}\\ |
---|
| 2316 | ©?|=?© & inclusive OR assignment \impl{?"|=?}\\ |
---|
[6c91065] | 2317 | \end{tabular} |
---|
| 2318 | \hfil |
---|
| 2319 | \caption{Operator Identifiers} |
---|
| 2320 | \label{opids} |
---|
| 2321 | \end{table} |
---|
| 2322 | |
---|
| 2323 | These identifiers are defined such that the question marks in the name identify the location of the operands. |
---|
| 2324 | These operands represent the parameters to the functions, and define how the operands are mapped to the function call. |
---|
[0638c44] | 2325 | For example, ©a + b© becomes ©?+?(a, b)©. |
---|
[6c91065] | 2326 | |
---|
| 2327 | In the example below, a new type, myComplex, is defined with an overloaded constructor, + operator, and string operator. |
---|
| 2328 | These operators are called using the normal C syntax. |
---|
| 2329 | |
---|
| 2330 | \begin{lstlisting} |
---|
| 2331 | type Complex = struct { // define a Complex type |
---|
| 2332 | double real; |
---|
| 2333 | double imag; |
---|
| 2334 | } |
---|
| 2335 | |
---|
| 2336 | // Constructor with default values |
---|
| 2337 | |
---|
| 2338 | void ?{}(Complex &c, double real = 0.0, double imag = 0.0) { |
---|
| 2339 | c.real = real; |
---|
| 2340 | c.imag = imag; |
---|
| 2341 | } |
---|
| 2342 | |
---|
| 2343 | Complex ?+?(Complex lhs, Complex rhs) { |
---|
| 2344 | Complex sum; |
---|
| 2345 | sum.real = lhs.real + rhs.real; |
---|
| 2346 | sum.imag = lhs.imag + rhs.imag; |
---|
| 2347 | return sum; |
---|
| 2348 | } |
---|
| 2349 | |
---|
| 2350 | String ()?(const Complex c) { |
---|
| 2351 | // use the string conversions for the structure members |
---|
| 2352 | return (String)c.real + . + . + (String)c.imag + .i.; |
---|
| 2353 | } |
---|
| 2354 | ... |
---|
| 2355 | |
---|
| 2356 | Complex a, b, c = {1.0}; // constructor for c w/ default imag |
---|
| 2357 | ... |
---|
| 2358 | c = a + b; |
---|
| 2359 | print(.sum = . + c); |
---|
| 2360 | \end{lstlisting} |
---|
| 2361 | |
---|
| 2362 | |
---|
[53ba273] | 2363 | \section{Auto Type-Inferencing} |
---|
| 2364 | |
---|
| 2365 | Auto type-inferencing occurs in a declaration where a variable's type is inferred from its initialization expression type. |
---|
| 2366 | \begin{quote2} |
---|
[45576af9] | 2367 | \begin{tabular}{@{}l@{\hspace{3em}}ll@{}} |
---|
[07bc165] | 2368 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CC}} & \multicolumn{1}{c}{Indexc{gcc}} \\ |
---|
[53ba273] | 2369 | \begin{lstlisting} |
---|
| 2370 | |
---|
| 2371 | auto j = 3.0 * 4; |
---|
| 2372 | int i; |
---|
| 2373 | auto k = i; |
---|
| 2374 | \end{lstlisting} |
---|
| 2375 | & |
---|
| 2376 | \begin{lstlisting} |
---|
| 2377 | #define expr 3.0 * i |
---|
| 2378 | typeof(expr) j = expr; |
---|
| 2379 | int i; |
---|
| 2380 | typeof(i) k = i; |
---|
| 2381 | \end{lstlisting} |
---|
| 2382 | & |
---|
| 2383 | \begin{lstlisting} |
---|
| 2384 | |
---|
| 2385 | // use type of initialization expression |
---|
| 2386 | |
---|
| 2387 | // use type of primary variable |
---|
| 2388 | \end{lstlisting} |
---|
| 2389 | \end{tabular} |
---|
| 2390 | \end{quote2} |
---|
| 2391 | The two important capabilities are: |
---|
| 2392 | \begin{itemize} |
---|
| 2393 | \item |
---|
| 2394 | preventing having to determine or write out long generic types, |
---|
| 2395 | \item |
---|
| 2396 | ensure secondary variables, related to a primary variable, always have the same type. |
---|
| 2397 | \end{itemize} |
---|
| 2398 | |
---|
[0638c44] | 2399 | 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. |
---|
[07bc165] | 2400 | \Indexc{gcc} provides ©typeof© to declare a secondary variable from a primary variable. |
---|
[53ba273] | 2401 | \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. |
---|
| 2402 | Only for overloaded routines with the same return type is variable type-inferencing possible. |
---|
[0638c44] | 2403 | Finally, ©auto© presents the programming problem of tracking down a type when the type is actually needed. |
---|
[53ba273] | 2404 | For example, given |
---|
| 2405 | \begin{lstlisting} |
---|
[e945826] | 2406 | auto j = ®...® |
---|
[53ba273] | 2407 | \end{lstlisting} |
---|
[0638c44] | 2408 | and the need to write a routine to compute using ©j© |
---|
[53ba273] | 2409 | \begin{lstlisting} |
---|
[e945826] | 2410 | void rtn( ®...® parm ); |
---|
[53ba273] | 2411 | rtn( j ); |
---|
| 2412 | \end{lstlisting} |
---|
[0638c44] | 2413 | A programmer must work backwards to determine the type of ©j©'s initialization expression, reconstructing the possibly long generic type-name. |
---|
[53ba273] | 2414 | In this situation, having the type name or a short alias is very useful. |
---|
| 2415 | |
---|
| 2416 | There is also the conundrum in type inferencing of when to \emph{\Index{brand}} a type. |
---|
| 2417 | That is, when is the type of the variable more important than the type of its initialization expression. |
---|
| 2418 | For example, if a change is made in an initialization expression, it can cause hundreds or thousands of cascading type changes and/or errors. |
---|
| 2419 | At some point, a programmer wants the type of the variable to remain constant and the expression to be in error when it changes. |
---|
| 2420 | |
---|
[0638c44] | 2421 | 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. |
---|
[53ba273] | 2422 | Should a significant need arise, this feature can be revisited. |
---|
| 2423 | |
---|
| 2424 | |
---|
| 2425 | \section{Generics} |
---|
[6c91065] | 2426 | |
---|
| 2427 | \CFA supports parametric polymorphism to allow users to define generic functions and types. |
---|
| 2428 | Generics allow programmers to use type variables in place of concrete types so that the code can be reused with multiple types. |
---|
| 2429 | The type parameters can be restricted to satisfy a set of constraints. |
---|
[07bc165] | 2430 | This enables \CFA to build fully compiled generic functions and types, unlike other languages like \Index*[C++]{\CC} where templates are expanded or must be explicitly instantiated. |
---|
[6c91065] | 2431 | |
---|
| 2432 | |
---|
| 2433 | \subsection{Generic Functions} |
---|
| 2434 | |
---|
[07bc165] | 2435 | Generic functions in \CFA are similar to template functions in \Index*[C++]{\CC}, and will sometimes be expanded into specialized versions, just like in \CC. |
---|
[6c91065] | 2436 | The difference, however, is that generic functions in \CFA can also be separately compiled, using function pointers for callers to pass in all needed functionality for the given type. |
---|
| 2437 | This means that compiled libraries can contain generic functions that can be used by programs linked with them (statically or dynamically). |
---|
| 2438 | Another advantage over \CC templates is unlike templates, generic functions are statically checked, even without being instantiated. |
---|
| 2439 | |
---|
| 2440 | A simple example of using Do.s parametric polymorphism to create a generic swap function would look like this: |
---|
| 2441 | |
---|
| 2442 | \begin{lstlisting} |
---|
| 2443 | generic(type T) |
---|
| 2444 | void swap(T &a, T &b) { |
---|
| 2445 | T tmp = a; |
---|
| 2446 | a = b; |
---|
| 2447 | b = a; |
---|
| 2448 | } |
---|
| 2449 | |
---|
| 2450 | int a, b; |
---|
| 2451 | swap(a, b); |
---|
| 2452 | |
---|
| 2453 | Point p1, p2; |
---|
| 2454 | swap(p1, p2); |
---|
| 2455 | \end{lstlisting} |
---|
| 2456 | |
---|
| 2457 | Here, instead of specifying types for the parameters a and b, the function has a generic type parameter, type T. |
---|
| 2458 | This function can be called with any type, and the compiler will handle generating the proper code for that type, using call site inference to determine the appropriate value for T. |
---|
| 2459 | |
---|
| 2460 | |
---|
| 2461 | \subsection{Bounded Quantification} |
---|
| 2462 | |
---|
| 2463 | Some generic functions only work (or make sense) for any type that satisfies a given property. |
---|
| 2464 | For example, here is a function to pick the minimum of two values of some type. |
---|
| 2465 | \begin{lstlisting} |
---|
| 2466 | generic (type T | bool ?<?(T, T) ) |
---|
| 2467 | |
---|
| 2468 | T min(T a, T b) { |
---|
| 2469 | return a < b ? a : b; |
---|
| 2470 | } |
---|
| 2471 | \end{lstlisting} |
---|
| 2472 | |
---|
| 2473 | It only makes sense to call min with values of a type that has an ordering: a way to decide whether one value is less than another. |
---|
| 2474 | The ordering function used here is the less than operator, <. |
---|
| 2475 | The syntax used to reference the operator is discussed in further detail in Operator Overloading. |
---|
| 2476 | In \CFA, this assertion on the type of a generic is written as the bound, (type T | bool ?<?(T, T)). |
---|
| 2477 | The \CFA compiler enforces that minis only called with types for which the less than operator is defined, and reports a compile-time error otherwise. |
---|
| 2478 | |
---|
| 2479 | Bounds can also involve multiple types, and multiple requirements, as shown below: |
---|
| 2480 | \begin{lstlisting} |
---|
| 2481 | generic (type T, type U | { T foo(T, U); U bar(U); }) |
---|
| 2482 | |
---|
| 2483 | T baz(T t, U u) { |
---|
| 2484 | return foo(t, bar(u)); |
---|
| 2485 | } |
---|
| 2486 | \end{lstlisting} |
---|
| 2487 | |
---|
| 2488 | |
---|
| 2489 | \subsection{Interfaces} |
---|
| 2490 | |
---|
| 2491 | Type bounds as shown above are not very informative, merely requiring that a function exists with the right name and type. |
---|
| 2492 | Suppose you try to call a polymorphic function and \CFA gives you an error that int combine(int, int) is not defined. |
---|
| 2493 | Can you define it? What is it supposed to do? Perhaps it should compute the sum, or the bitwise and, or the maximum, or the least common multiple; or perhaps it's an operation that can't be defined for integers. |
---|
| 2494 | The function signature doesn't say. |
---|
| 2495 | |
---|
| 2496 | Interfaces gather together a set of function signatures under a common name, which solves two problems. |
---|
| 2497 | First, an interface name can be used in type bounds instead of function signatures. |
---|
| 2498 | This avoids repetition when a bound is used in many functions. |
---|
| 2499 | Second, interfaces explicitly document the existence of a commonly used set of functionality, making programs easier to understand. |
---|
| 2500 | \begin{lstlisting} |
---|
| 2501 | generic (type T) |
---|
| 2502 | interface Orderable { |
---|
| 2503 | bool ?<?(T, T); |
---|
| 2504 | }; |
---|
| 2505 | |
---|
| 2506 | generic (type T | Orderable(T)) |
---|
| 2507 | T min(T a, T b) { |
---|
| 2508 | return a < b ? a : b; |
---|
| 2509 | } |
---|
| 2510 | \end{lstlisting} |
---|
| 2511 | |
---|
| 2512 | This definition of the interface Orderable makes the generic function min easier to read and understand. |
---|
| 2513 | Orderable can also be reused for other generic functions, max for example. |
---|
| 2514 | Interfaces can also build on top of other interfaces. |
---|
| 2515 | For example: |
---|
| 2516 | \begin{lstlisting} |
---|
| 2517 | generic (type T | Orderable(T) |
---|
| 2518 | interface FooBarable { |
---|
| 2519 | int foo(T, T); |
---|
| 2520 | int Bar(T, T); |
---|
| 2521 | }; |
---|
| 2522 | \end{lstlisting} |
---|
| 2523 | |
---|
| 2524 | The FooBarable interface specifies all of the bounds of the Orderable interface, plus the additional bounds specified in its definition. |
---|
| 2525 | A type does not need to specify that it satisfies any interface, the compiler can figure this out at compile time. |
---|
| 2526 | For example, there is no need to add some special syntax to show that a type implements the Orderable interface, just define a ?<? operator and it is satisfied. |
---|
| 2527 | |
---|
| 2528 | |
---|
| 2529 | \subsection{Generic Typedefs} |
---|
| 2530 | |
---|
| 2531 | Type synonyms can be defined generically using the typedef keyword together with a generic type annotation. |
---|
| 2532 | These can be used to abbreviate complicated type expressions, especially in generic code. |
---|
| 2533 | \begin{lstlisting} |
---|
| 2534 | // typedef the generic function pointers for later use |
---|
| 2535 | |
---|
| 2536 | generic(type T) |
---|
| 2537 | typedef int (*predicate)(T); |
---|
| 2538 | generic(type Captured, type T) |
---|
| 2539 | typedef void (*callback)(Captured, T); |
---|
| 2540 | |
---|
| 2541 | generic(type T) |
---|
| 2542 | void find(int length, T *array, |
---|
| 2543 | predicate(T) p, callback(void *, T)f) { |
---|
| 2544 | int i; |
---|
| 2545 | for (i = 0; i < length; i++) |
---|
| 2546 | if (p(array[i])) f(NULL, array[i]); |
---|
| 2547 | } |
---|
| 2548 | \end{lstlisting} |
---|
| 2549 | |
---|
| 2550 | |
---|
| 2551 | \subsection{Generic Types} |
---|
| 2552 | |
---|
| 2553 | Generic types are defined using the same mechanisms as those described above for generic functions. |
---|
[07bc165] | 2554 | This feature allows users to create types that have one or more fields that use generic parameters as types, similar to a template classes in \Index*[C++]{\CC}. |
---|
[6c91065] | 2555 | For example, to make a generic linked list, a placeholder is created for the type of the elements, so that the specific type of the elements in the list need not be specified when defining the list. |
---|
| 2556 | In C, something like this would have to be done using void pointers and unsafe casting. |
---|
| 2557 | As in generic functions, Do.s generic types are different from \CC templates in that they can be fully compiled, while \CC templates are more like macro expansions. |
---|
| 2558 | This means that a \CFA generic type from a compiled library can be used with any type that satisfies the bounds. |
---|
| 2559 | |
---|
| 2560 | The syntax for defining a generic type looks very similar to that of a generic function. |
---|
| 2561 | Generic types support bounds and interfaces, using the same syntax as generic functions. |
---|
| 2562 | \begin{lstlisting} |
---|
| 2563 | generic (type T) |
---|
| 2564 | struct LinkedListElem { |
---|
| 2565 | T elem; |
---|
| 2566 | LinkedListElem(T) *next; |
---|
| 2567 | }; |
---|
| 2568 | |
---|
| 2569 | LinkedListElem *++?(LinkedListElem **elem) { |
---|
| 2570 | return *elem = elem->next; |
---|
| 2571 | } |
---|
| 2572 | |
---|
| 2573 | generic (type T) |
---|
| 2574 | struct LinkedList { |
---|
| 2575 | LinkedListElem(T) *head; |
---|
| 2576 | unsigned int size; |
---|
| 2577 | } |
---|
| 2578 | |
---|
| 2579 | generic (type T | bool ?==?(T, T)) |
---|
| 2580 | bool contains(LinkedList(T) *list, T elem) { |
---|
| 2581 | for(LinkedListElem *iter = list->head; iter != 0; ++iter) { |
---|
| 2582 | if (iter->elem == elem) return true; |
---|
| 2583 | } |
---|
| 2584 | return false; |
---|
| 2585 | } |
---|
| 2586 | \end{lstlisting} |
---|
| 2587 | |
---|
| 2588 | |
---|
| 2589 | \section{Safety} |
---|
| 2590 | |
---|
| 2591 | Safety, along with productivity, is a key goal of Do. |
---|
| 2592 | This section discusses the safety features that have been included in \CFA to help programmers create more stable, reliable, and secure code. |
---|
| 2593 | |
---|
| 2594 | |
---|
| 2595 | \subsection{Exceptions} |
---|
| 2596 | |
---|
| 2597 | \CFA introduces support for exceptions as an easier way to recover from exceptional conditions that may be detected within a block of code. |
---|
| 2598 | In C, developers can use error codes and special return values to report to a caller that an error occurred in a function. |
---|
| 2599 | The major problem with error codes is that they can be easily ignored by the caller. |
---|
| 2600 | Failure to properly check for errors can result in the caller making incorrect assumptions about the current state or about the return value that are very likely to result in errors later on in the program, making the source of the problem more difficult to find when debugging. |
---|
| 2601 | An unhandled exception on the other hand will cause a crash, revealing the original source of the erroneous state. |
---|
| 2602 | |
---|
| 2603 | Exceptions in \CFA allow a different type of control flow. |
---|
| 2604 | Throwing an exception terminates execution of the current block, invokes the destructors of variables that are local to the block, and propagates the exception to the parent block. |
---|
| 2605 | The exception is immediately re-thrown from the parent block unless it is caught as described below. |
---|
[07bc165] | 2606 | \CFA uses keywords similar to \Index*[C++]{\CC} for exception handling. |
---|
[6c91065] | 2607 | An exception is thrown using a throw statement, which accepts one argument. |
---|
| 2608 | |
---|
| 2609 | \begin{lstlisting} |
---|
| 2610 | ... |
---|
| 2611 | |
---|
| 2612 | throw 13; |
---|
| 2613 | |
---|
| 2614 | ... |
---|
| 2615 | \end{lstlisting} |
---|
| 2616 | |
---|
| 2617 | An exception can be caught using a catch statement, which specifies the type of the exception it can catch. |
---|
| 2618 | A catch is specified immediately after a guarded block to signify that it can catch an exception from that block. |
---|
| 2619 | A guarded block is specified using the try keyword, followed by a block of code inside of curly braces. |
---|
| 2620 | |
---|
| 2621 | \begin{lstlisting} |
---|
| 2622 | ... |
---|
| 2623 | |
---|
| 2624 | try { |
---|
[6b6597c] | 2625 | throw 13; |
---|
[6c91065] | 2626 | } |
---|
| 2627 | catch(int e) { |
---|
[6b6597c] | 2628 | printf(.caught an exception: %d\n., e); |
---|
[6c91065] | 2629 | } |
---|
| 2630 | \end{lstlisting} |
---|
| 2631 | |
---|
| 2632 | |
---|
| 2633 | \subsection{Memory Management} |
---|
| 2634 | |
---|
| 2635 | |
---|
| 2636 | \subsubsection{Manual Memory Management} |
---|
| 2637 | |
---|
| 2638 | Using malloc and free to dynamically allocate memory exposes several potential, and common, errors. |
---|
| 2639 | First, malloc breaks type safety because it returns a pointer to void. |
---|
| 2640 | There is no relationship between the type that the returned pointer is cast to, and the amount of memory allocated. |
---|
| 2641 | This problem is solved with a type-safe malloc. |
---|
| 2642 | Do.s type-safe malloc does not take any arguments for size. |
---|
| 2643 | Instead, it infers the type based on the return value, and then allocates space for the inferred type. |
---|
| 2644 | |
---|
| 2645 | \begin{lstlisting} |
---|
| 2646 | float *f = malloc(); // allocates the size of a float |
---|
| 2647 | |
---|
| 2648 | struct S { |
---|
| 2649 | int i, j, k; |
---|
| 2650 | }; |
---|
| 2651 | |
---|
| 2652 | struct S *s = malloc(); // allocates the size of a struct S |
---|
| 2653 | \end{lstlisting} |
---|
| 2654 | |
---|
| 2655 | In addition to the improved malloc, \CFA also provides a technique for combining allocation and initialization into one step, using the new function. |
---|
| 2656 | For all constructors defined for a given type (see Operator Overloading), a corresponding call to new can be used to allocate and construct that type. |
---|
| 2657 | |
---|
| 2658 | \begin{lstlisting} |
---|
| 2659 | type Complex = struct { |
---|
| 2660 | float real; |
---|
| 2661 | float imag; |
---|
| 2662 | }; |
---|
| 2663 | |
---|
| 2664 | // default constructor |
---|
| 2665 | |
---|
| 2666 | void ?{}(Complex &c) { |
---|
| 2667 | c.real = 0.0; |
---|
| 2668 | c.imag = 0.0; |
---|
| 2669 | } |
---|
| 2670 | |
---|
| 2671 | |
---|
| 2672 | |
---|
| 2673 | // 2 parameter constructor |
---|
| 2674 | |
---|
| 2675 | void ?{}(Complex &c, float real, float imag) { |
---|
| 2676 | c.real = real; |
---|
| 2677 | c.imag = imag; |
---|
| 2678 | } |
---|
| 2679 | |
---|
| 2680 | |
---|
| 2681 | int main() { |
---|
| 2682 | Complex c1; // No constructor is called |
---|
| 2683 | Complex c2{}; // Default constructor called |
---|
| 2684 | Complex c3{1.0, -1.0}; // 2 parameter constructor is called |
---|
| 2685 | |
---|
| 2686 | Complex *p1 = malloc(); // allocate |
---|
| 2687 | Complex *p2 = new(); // allocate + default constructor |
---|
| 2688 | Complex *p3 = new(0.5, 1.0); // allocate + 2 param constructor |
---|
| 2689 | } |
---|
| 2690 | |
---|
| 2691 | \end{lstlisting} |
---|
| 2692 | |
---|
| 2693 | |
---|
| 2694 | \subsubsection{Automatic Memory Management} |
---|
| 2695 | |
---|
| 2696 | \CFA may also support automatic memory management to further improve safety. |
---|
| 2697 | If the compiler can insert all of the code needed to manage dynamically allocated memory (automatic reference counting), then developers can avoid problems with dangling pointers, double frees, memory leaks, etc. |
---|
| 2698 | This feature requires further investigation. |
---|
| 2699 | \CFA will not have a garbage collector, but might use some kind of region-based memory management. |
---|
| 2700 | |
---|
| 2701 | |
---|
| 2702 | \subsection{Unsafe C Constructs} |
---|
| 2703 | |
---|
| 2704 | C programmers are able to access all of the low-level tricks that are sometimes needed for close-to-the-hardware programming. |
---|
| 2705 | Some of these practices however are often error-prone and difficult to read and maintain. |
---|
| 2706 | Since \CFA is designed to be safer than C, such constructs are disallowed in \CFA code. |
---|
| 2707 | If a programmer wants to use one of these unsafe C constructs, the unsafe code must be contained in a C linkage block (see Interoperability), which will be compiled like C code. |
---|
| 2708 | This block means that the user is telling the tools, .I know this is unsafe, but I.m going to do it anyway.. |
---|
| 2709 | |
---|
| 2710 | The exact set of unsafe C constructs that will be disallowed in \CFA has not yet been decided, but is sure to include pointer arithmetic, pointer casting, etc. |
---|
| 2711 | Once the full set is decided, the rules will be listed here. |
---|
| 2712 | |
---|
| 2713 | |
---|
[53ba273] | 2714 | \section{Syntactic Anomalies} |
---|
[6c91065] | 2715 | |
---|
[53ba273] | 2716 | The number 0 and 1 are treated specially in \CFA, and can be redefined as variables. |
---|
| 2717 | One syntactic anomaly is when a field in an structure is names 0 or 1: |
---|
[6c91065] | 2718 | \begin{lstlisting} |
---|
[53ba273] | 2719 | struct S { |
---|
| 2720 | int 0, 1; |
---|
| 2721 | } s; |
---|
[6c91065] | 2722 | \end{lstlisting} |
---|
[5479e63] | 2723 | The problem occurs in accessing these fields using the selection operation ``©.©'': |
---|
[6c91065] | 2724 | \begin{lstlisting} |
---|
[53ba273] | 2725 | s.0 = 0; // ambiguity with floating constant .0 |
---|
| 2726 | s.1 = 1; // ambiguity with floating constant .1 |
---|
[6c91065] | 2727 | \end{lstlisting} |
---|
[53ba273] | 2728 | To make this work, a space is required after the field selection: |
---|
[6c91065] | 2729 | \begin{lstlisting} |
---|
[e945826] | 2730 | ®s.§\textvisiblespace§0® = 0; |
---|
| 2731 | ®s.§\textvisiblespace§1® = 1; |
---|
[6c91065] | 2732 | \end{lstlisting} |
---|
[5479e63] | 2733 | While this syntax is awkward, it is unlikely many programmers will name fields of a structure 0 or 1. |
---|
[07bc165] | 2734 | Like the \Index*[C++]{\CC} lexical problem with closing template-syntax, e.g, ©Foo<Bar<int®>>®©, this issue can be solved with a more powerful lexer/parser. |
---|
[6c91065] | 2735 | |
---|
[c45170a] | 2736 | There are several ambiguous cases with operator identifiers, \eg ©int *?*?()©, where the string ©*?*?© can be lexed as ©*©/©?*?© or ©*?©/©*?©. |
---|
| 2737 | Since it is common practise to put a unary operator juxtaposed to an identifier, \eg ©*i©, users will be annoyed if they cannot do this with respect to operator identifiers. |
---|
[53ba273] | 2738 | Even with this special hack, there are 5 general cases that cannot be handled. |
---|
[0638c44] | 2739 | The first case is for the function-call identifier ©?()©: |
---|
[6c91065] | 2740 | \begin{lstlisting} |
---|
[e945826] | 2741 | int *§\textvisiblespace§?()(); // declaration: space required after '*' |
---|
| 2742 | *§\textvisiblespace§?()(); // expression: space required after '*' |
---|
[6c91065] | 2743 | \end{lstlisting} |
---|
[0638c44] | 2744 | Without the space, the string ©*?()© is ambiguous without N character look ahead; |
---|
| 2745 | it requires scanning ahead to determine if there is a ©'('©, which is the start of an argument/parameter list. |
---|
[6c91065] | 2746 | |
---|
[53ba273] | 2747 | The 4 remaining cases occur in expressions: |
---|
[6c91065] | 2748 | \begin{lstlisting} |
---|
[e945826] | 2749 | i++§\textvisiblespace§?i:0; // space required before '?' |
---|
| 2750 | i--§\textvisiblespace§?i:0; // space required before '?' |
---|
| 2751 | i§\textvisiblespace§?++i:0; // space required after '?' |
---|
| 2752 | i§\textvisiblespace§?--i:0; // space required after '?' |
---|
[6c91065] | 2753 | \end{lstlisting} |
---|
[0638c44] | 2754 | In the first two cases, the string ©i++?© is ambiguous, where this string can be lexed as ©i© / ©++?© or ©i++© / ©?©; |
---|
| 2755 | it requires scanning ahead to determine if there is a ©'('©, which is the start of an argument list. |
---|
| 2756 | In the second two cases, the string ©?++x© is ambiguous, where this string can be lexed as ©?++© / ©x© or ©?© / y©++x©; |
---|
| 2757 | it requires scanning ahead to determine if there is a ©'('©, which is the start of an argument list. |
---|
[6c91065] | 2758 | |
---|
| 2759 | |
---|
| 2760 | \section{Concurrency} |
---|
| 2761 | |
---|
| 2762 | Today's processors for nearly all use cases, ranging from embedded systems to large cloud computing servers, are composed of multiple cores, often heterogeneous. |
---|
| 2763 | As machines grow in complexity, it becomes more difficult for a program to make the most use of the hardware available. |
---|
| 2764 | \CFA includes built-in concurrency features to enable high performance and improve programmer productivity on these multi-/many-core machines. |
---|
| 2765 | |
---|
| 2766 | Concurrency support in \CFA is implemented on top of a highly efficient runtime system of light-weight, M:N, user level threads. |
---|
| 2767 | The model integrates concurrency features into the language by making the structure type the core unit of concurrency. |
---|
| 2768 | All communication occurs through method calls, where data is sent via method arguments, and received via the return value. |
---|
| 2769 | This enables a very familiar interface to all programmers, even those with no parallel programming experience. |
---|
| 2770 | It also allows the compiler to do static type checking of all communication, a very important safety feature. |
---|
[07bc165] | 2771 | This controlled communication with type safety has some similarities with channels in \Index*{Go}, and can actually implement |
---|
[6c91065] | 2772 | channels exactly, as well as create additional communication patterns that channels cannot. |
---|
| 2773 | Mutex objects, monitors, are used to contain mutual exclusion within an object and synchronization across concurrent threads. |
---|
| 2774 | |
---|
| 2775 | Three new keywords are added to support these features: |
---|
| 2776 | |
---|
| 2777 | monitor creates a structure with implicit locking when accessing fields |
---|
| 2778 | |
---|
| 2779 | mutex implies use of a monitor requiring the implicit locking |
---|
| 2780 | |
---|
| 2781 | task creates a type with implicit locking, separate stack, and a thread |
---|
| 2782 | |
---|
[53ba273] | 2783 | |
---|
[6c91065] | 2784 | \subsection{Monitors} |
---|
| 2785 | |
---|
| 2786 | A monitor is a structure in \CFA which includes implicit locking of its fields. |
---|
| 2787 | Users of a monitor interact with it just like any structure, but the compiler handles code as needed to ensure mutual exclusion. |
---|
| 2788 | An example of the definition of a monitor is shown here: |
---|
| 2789 | \begin{lstlisting} |
---|
| 2790 | type Account = monitor { |
---|
| 2791 | const unsigned long number; // account number |
---|
| 2792 | float balance; // account balance |
---|
| 2793 | }; |
---|
| 2794 | \end{lstlisting} |
---|
| 2795 | |
---|
| 2796 | Since a monitor structure includes an implicit locking mechanism, it does not make sense to copy a monitor; |
---|
| 2797 | it is always passed by reference. |
---|
| 2798 | Users can specify to the compiler whether or not a function will require mutual exclusion of the monitor using the mutex modifier on the parameter. |
---|
| 2799 | When mutex is specified, the compiler inserts locking before executing the body of the function, and unlocking after the body. |
---|
| 2800 | This means that a function requiring mutual exclusion could block if the lock is already held by another thread. |
---|
| 2801 | Blocking on a monitor lock does not block the kernel thread, it simply blocks the user thread, which yields its kernel thread while waiting to obtain the lock. |
---|
[c45170a] | 2802 | If multiple mutex parameters are specified, they will be locked in parameter order (\ie first parameter is locked first) and unlocked in the |
---|
[6c91065] | 2803 | reverse order. |
---|
| 2804 | \begin{lstlisting} |
---|
| 2805 | // This function accesses a constant field, it does not require |
---|
| 2806 | // mutual exclusion |
---|
| 2807 | |
---|
| 2808 | export unsigned long getAccountNumber(Account &a) { |
---|
| 2809 | return a.number; |
---|
| 2810 | } |
---|
| 2811 | |
---|
| 2812 | // This function accesses and modifies a shared field; it |
---|
| 2813 | // requires mutual exclusion |
---|
| 2814 | |
---|
| 2815 | export float withdrawal(mutex Account &a, float amount) { |
---|
| 2816 | a.balance -= amount; |
---|
| 2817 | return a.balance; |
---|
| 2818 | } |
---|
| 2819 | \end{lstlisting} |
---|
| 2820 | |
---|
| 2821 | Often, one function using a monitor will call another function using that same monitor. |
---|
| 2822 | If both require mutual exclusion, then the thread would be waiting for itself to release the lock when it calls the inner function. |
---|
| 2823 | This situation is resolved by allowing recursive entry (reentrant locks), meaning that if the lock is already held by the caller, it can be locked again. |
---|
| 2824 | It will still be unlocked the same number of times. |
---|
| 2825 | An example of this situation is shown below: |
---|
| 2826 | |
---|
| 2827 | \begin{lstlisting} |
---|
| 2828 | // deleting a job from a worker requires mutual exclusion |
---|
| 2829 | |
---|
| 2830 | void deleteJob(mutex Worker &w, Job &j) { |
---|
| 2831 | ... |
---|
| 2832 | } |
---|
| 2833 | |
---|
| 2834 | // transferring requires mutual exclusion and calls deleteJob |
---|
| 2835 | |
---|
| 2836 | void transferJob(mutex Worker &from, Worker &to) { |
---|
| 2837 | ... |
---|
| 2838 | deleteJob(j); |
---|
| 2839 | ... |
---|
| 2840 | } |
---|
| 2841 | \end{lstlisting} |
---|
| 2842 | |
---|
| 2843 | |
---|
| 2844 | \subsection{Tasks} |
---|
| 2845 | |
---|
| 2846 | \CFA also provides a simple mechanism for creating and utilizing user level threads. |
---|
| 2847 | A task provides mutual exclusion like a monitor, and also has its own execution state and a thread of control. |
---|
| 2848 | Similar to a monitor, a task is defined like a structure: |
---|
| 2849 | \begin{lstlisting} |
---|
| 2850 | type Adder = task { |
---|
| 2851 | int *row; |
---|
| 2852 | int size; |
---|
| 2853 | int &subtotal; |
---|
| 2854 | } |
---|
| 2855 | \end{lstlisting} |
---|
| 2856 | |
---|
| 2857 | A task may define a constructor, which will be called upon allocation and run on the caller.s thread. |
---|
| 2858 | A destructor may also be defined, which is called at de-allocation (when a dynamic object is deleted or when a local object goes out of scope). |
---|
| 2859 | After a task is allocated and initialized, its thread is spawned implicitly and begins executing in its function call method. |
---|
| 2860 | All tasks must define this function call method, with a void return value and no additional parameters, or the compiler will report an error. |
---|
| 2861 | Below are example functions for the above Adder task, and its usage to sum up a matrix on multiple threads. |
---|
| 2862 | (Note that this example is designed to display the syntax and functionality, not the best method to solve this problem) |
---|
| 2863 | \begin{lstlisting} |
---|
| 2864 | void ?{}(Adder &a, int r[], int s, int &st) { // constructor |
---|
| 2865 | a.row = r; |
---|
| 2866 | a.size = s; |
---|
| 2867 | a.subtotal = st; |
---|
| 2868 | } |
---|
| 2869 | |
---|
| 2870 | // implicitly spawn thread and begin execution here |
---|
| 2871 | |
---|
| 2872 | void ?()(Adder &a) { |
---|
| 2873 | int c; |
---|
| 2874 | subtotal = 0; |
---|
| 2875 | for (c=0; c<a.size; ++c) { |
---|
| 2876 | subtotal += row[c]; |
---|
| 2877 | } |
---|
| 2878 | } |
---|
| 2879 | |
---|
| 2880 | int main() { |
---|
| 2881 | const int rows = 100, cols = 1000000; |
---|
| 2882 | int matrix[rows][cols]; |
---|
| 2883 | int subtotals[rows]; |
---|
| 2884 | int total = 0; |
---|
| 2885 | int r; |
---|
| 2886 | |
---|
| 2887 | { // create a new scope here for our adders |
---|
| 2888 | Adder adders[rows]; |
---|
| 2889 | // read in the matrix |
---|
| 2890 | ... |
---|
| 2891 | for (r=0; r<rows; ++r) { |
---|
| 2892 | // tasks are initialized on this thread |
---|
| 2893 | Adders[r] = {matrix[r], cols, subtotals[r]}; |
---|
| 2894 | Adders[r](); // spawn thread and begin execution |
---|
| 2895 | } |
---|
| 2896 | } // adders go out of scope; block here until they all finish |
---|
| 2897 | total += subtotals[r]; |
---|
| 2898 | printf(.total is %d\n., total); |
---|
| 2899 | } |
---|
| 2900 | \end{lstlisting} |
---|
| 2901 | |
---|
| 2902 | |
---|
| 2903 | \subsection{Cooperative Scheduling} |
---|
| 2904 | |
---|
| 2905 | Tasks in \CFA are cooperatively scheduled, meaning that a task will not be interrupted by another task, except at specific yield points. |
---|
| 2906 | In Listing 31, there are no yield points, so each task runs to completion with no interruptions. |
---|
| 2907 | Places where a task could yield include waiting for a lock (explicitly or implicitly), waiting for I/O, or waiting for a specific function (or one of a set of functions) to be called. |
---|
| 2908 | This last option is introduced with the yield function. yield is used to indicate that this task should yield its thread until the specified function is called. |
---|
| 2909 | For example, the code below defines a monitor that maintains a generic list. |
---|
| 2910 | When a task tries to pop from the list, but it is empty, the task should yield until another task puts something into the list, with the push function. |
---|
| 2911 | Similarly, when a task tries to push something onto the list, but it is full, it will yield until another task frees some space with the pop function. |
---|
| 2912 | |
---|
| 2913 | \begin{lstlisting} |
---|
| 2914 | // type T is used as a generic type for all definitions inside |
---|
| 2915 | // the curly brackets |
---|
| 2916 | |
---|
| 2917 | generic(type T) { |
---|
| 2918 | type Channel = monitor { |
---|
| 2919 | List(T) list; // list is a simple generic list type |
---|
| 2920 | }; |
---|
| 2921 | |
---|
| 2922 | T pop(mutex &Channel(T) ch) { |
---|
| 2923 | if (ch.list.empty()) { |
---|
| 2924 | // yield until push is called for this channel |
---|
| 2925 | yield(push); |
---|
| 2926 | } |
---|
| 2927 | return ch.list.pop(); |
---|
| 2928 | } |
---|
| 2929 | |
---|
| 2930 | void push(mutex &Channel(T)ch, T val) { |
---|
| 2931 | if (ch.list.full()) { |
---|
| 2932 | // yield until pop is called for this channel |
---|
| 2933 | yield(pop); |
---|
| 2934 | } |
---|
| 2935 | ch.list.push(val); |
---|
| 2936 | } |
---|
| 2937 | } |
---|
| 2938 | \end{lstlisting} |
---|
| 2939 | |
---|
| 2940 | A task can also yield indefinitely by calling yield with no arguments. |
---|
| 2941 | This will tell the scheduler to yield this task until it is resumed by some other task. |
---|
| 2942 | A task can resume another task by using its functional call operator. |
---|
| 2943 | The code below shows a simple ping-pong example, where two tasks yield back and forth to each other using these methods. |
---|
| 2944 | |
---|
| 2945 | \begin{lstlisting} |
---|
| 2946 | type Ping = task { |
---|
| 2947 | Pong *partner; |
---|
| 2948 | }; |
---|
| 2949 | |
---|
| 2950 | void ?{}(Ping &p, Pong *partner = 0) { |
---|
| 2951 | p.partner = partner; |
---|
| 2952 | } |
---|
| 2953 | |
---|
| 2954 | void ?()(Ping &p) { |
---|
| 2955 | for(;;) { // loop forever |
---|
| 2956 | printf(.ping\n.); |
---|
| 2957 | partner(); // resumes the partner task |
---|
| 2958 | yield(); // yields this task |
---|
| 2959 | } |
---|
| 2960 | } |
---|
| 2961 | |
---|
| 2962 | type Pong = task { |
---|
| 2963 | Ping *partner; |
---|
| 2964 | }; |
---|
| 2965 | |
---|
| 2966 | void ?{}(Pong &p, Ping *partner = 0) { |
---|
| 2967 | p.partner = partner; |
---|
| 2968 | } |
---|
| 2969 | |
---|
| 2970 | void ?()(Pong &p) { |
---|
| 2971 | for(;;) { // loop forever |
---|
| 2972 | yield(); // yields this task |
---|
| 2973 | printf(.pong/n.); |
---|
| 2974 | partner(); // resumes the partner task |
---|
| 2975 | } |
---|
| 2976 | } |
---|
| 2977 | |
---|
| 2978 | void main() { |
---|
| 2979 | Ping ping; // allocate ping |
---|
| 2980 | Pong pong{ping}; // allocate, initialize, and start pong |
---|
| 2981 | Ping{pong}; // initialize and start ping |
---|
| 2982 | } |
---|
| 2983 | \end{lstlisting} |
---|
| 2984 | |
---|
| 2985 | The same functionality can be accomplished by providing functions to be called by the partner task. |
---|
| 2986 | \begin{lstlisting} |
---|
| 2987 | type Pingpong = task { |
---|
| 2988 | String msg; |
---|
| 2989 | Pingpong *partner; |
---|
| 2990 | }; |
---|
| 2991 | |
---|
| 2992 | void ?{}(Pingpong &p, String msg, Pingpong *partner = 0) { |
---|
| 2993 | p.msg = msg; |
---|
| 2994 | p.partner = partner; |
---|
| 2995 | } |
---|
| 2996 | |
---|
| 2997 | void ?()(Pingpong &p) { |
---|
| 2998 | for(;;) { |
---|
| 2999 | yield(go); |
---|
| 3000 | } |
---|
| 3001 | } |
---|
| 3002 | |
---|
| 3003 | void go(Pingpong &p) { |
---|
| 3004 | print(.%(p.msg)\n.); |
---|
| 3005 | go(p.partner); |
---|
| 3006 | } |
---|
| 3007 | |
---|
| 3008 | void main() { |
---|
| 3009 | Pingpong ping = {.ping.}; |
---|
| 3010 | Pingpong pong = {.pong., ping}; |
---|
| 3011 | ping.partner = pong; |
---|
| 3012 | go(ping); |
---|
| 3013 | } |
---|
| 3014 | \end{lstlisting} |
---|
| 3015 | |
---|
| 3016 | |
---|
| 3017 | \section{Modules and Packages } |
---|
| 3018 | |
---|
| 3019 | \begin{comment} |
---|
| 3020 | High-level encapsulation is useful for organizing code into reusable units, and accelerating compilation speed. |
---|
| 3021 | \CFA provides a convenient mechanism for creating, building and sharing groups of functionality that enhances productivity and improves compile time. |
---|
| 3022 | |
---|
| 3023 | There are two levels of encapsulation in \CFA, module and package. |
---|
[07bc165] | 3024 | A module is a logical grouping of functionality that can be easily pulled into another project, much like a module in \Index*{Python} or a package in \Index*{Go}. |
---|
[6c91065] | 3025 | A module forms a namespace to limit the visibility and prevent naming conflicts of variables. |
---|
| 3026 | Furthermore, a module is an independent translation unit, which can be compiled separately to accelerate the compilation speed. |
---|
| 3027 | |
---|
| 3028 | A package is a physical grouping of one or more modules that is used for code distribution and version management. |
---|
| 3029 | Package is also the level of granularity at which dependences are managed. |
---|
[07bc165] | 3030 | A package is similar to the Crate in \Index*{Rust}. |
---|
[6c91065] | 3031 | |
---|
| 3032 | |
---|
| 3033 | \subsection{No Declarations, No Header Files} |
---|
| 3034 | |
---|
[07bc165] | 3035 | In C and \Index*[C++]{\CC}, it is necessary to declare or define every global variable, global function, and type before it is used in each file. |
---|
[6c91065] | 3036 | Header files and a preprocessor are normally used to avoid repeating code. |
---|
| 3037 | Thus, many variables, functions, and types are described twice, which exposes an opportunity for errors and causes additional maintenance work. |
---|
| 3038 | Instead of following this model, the \CFA tools can extract all of the same information from the code automatically. |
---|
| 3039 | This information is then stored in the object files for each module, in a format that can quickly be read by the compiler, and stored at the top of the file, for quick access. |
---|
| 3040 | In addition to the user productivity improvements, this simple change also improves compile time, by saving the information in a simple machine readable format, instead of making the compiler parse the same information over and over from a header file. |
---|
[07bc165] | 3041 | This seems like a minor change, but according to (Pike, \Index*{Go} at Google: Language Design in the Service of Software Engineering), this simple change can cause massive reductions in compile time. |
---|
[6c91065] | 3042 | |
---|
| 3043 | In \CFA, multiple definitions are not necessary. |
---|
| 3044 | Within a module, all of the module's global definitions are visible throughout the module. |
---|
[5479e63] | 3045 | For example, the following code compiles, even though ©isOdd© was not declared before being called: |
---|
[6c91065] | 3046 | \begin{lstlisting} |
---|
| 3047 | bool isEven(unsigned int x) { |
---|
| 3048 | if (x == 0) return true; |
---|
| 3049 | else return !isOdd(x); |
---|
| 3050 | } |
---|
| 3051 | |
---|
| 3052 | bool isOdd(unsigned int x) { |
---|
| 3053 | if (x == 1) return true; |
---|
| 3054 | else return !isEven(x - 2); |
---|
| 3055 | } |
---|
| 3056 | \end{lstlisting} |
---|
| 3057 | |
---|
| 3058 | Header files in C are used to expose the declarations from a library, so that they can be used externally. |
---|
| 3059 | With \CFA, this functionality is replaced with module exports, discussed below. |
---|
| 3060 | When building a \CFA module which needs to be callable from C code, users can use the tools to generate a header file suitable for including in these C files with all of the needed declarations. |
---|
| 3061 | |
---|
| 3062 | In order to interoperate with existing C code, \CFA files can still include header files, the contents of which will be enclosed in a C linkage section to indicate C calling conventions (see Interoperability for more information). |
---|
| 3063 | |
---|
| 3064 | |
---|
| 3065 | \subsection{Modules} |
---|
| 3066 | |
---|
| 3067 | A module typically contains a set of related types and methods, with some objects accessible from outside the package, and some limited to use inside the module. |
---|
| 3068 | These modules can then be easily shared and reused in multiple projects. |
---|
| 3069 | As modules are intended to be distributed for reuse, they should generally have stable, well-defined interfaces. |
---|
| 3070 | |
---|
| 3071 | \CFA adds the following keywords to express the module systems: module, export, import, as. |
---|
| 3072 | |
---|
| 3073 | |
---|
| 3074 | \subsubsection{Module Declaration} |
---|
| 3075 | |
---|
| 3076 | The syntax to declare a module is module moduleName;. |
---|
| 3077 | |
---|
| 3078 | The module declaration must be at the beginning of a file, and each file can only belong to one module. |
---|
| 3079 | If there is no module declaration at the beginning of a file, the file belongs to the global module. |
---|
| 3080 | A module can span several files. |
---|
| 3081 | By convention, a module and the files belonging to the module have additional mapping relationship which is described in the Do-Lang Tooling documentation. |
---|
| 3082 | |
---|
| 3083 | The moduleName follows the same rules of a variable name, except that it can use slash "/" to indicate the module/sub-module relationship. |
---|
| 3084 | For example, container/vector is a valid module name, where container is the parent module name, and vector is the sub-module under container. |
---|
| 3085 | |
---|
| 3086 | Only the interfaces of a module are visible from outside, when the module is imported. export is a type decorator to declare a module interface. |
---|
| 3087 | A method, a global variable or a type can be declared as a module interface. |
---|
| 3088 | Types defined in a module and referenced by an exported function or a variable must be exported, too. |
---|
| 3089 | |
---|
| 3090 | The following code is a simple module declaration example. |
---|
| 3091 | \begin{lstlisting} |
---|
| 3092 | module M; |
---|
| 3093 | |
---|
| 3094 | //visible outside module M |
---|
| 3095 | |
---|
| 3096 | export int f(int i) { return i + 1; } |
---|
| 3097 | export double aCounter; |
---|
| 3098 | |
---|
| 3099 | //not visible outside module M |
---|
| 3100 | |
---|
| 3101 | int g(int i) { return i - 1; } |
---|
| 3102 | |
---|
| 3103 | double bCounter; |
---|
| 3104 | \end{lstlisting} |
---|
| 3105 | |
---|
| 3106 | export module moduleName; can be use to re-export all the visible (exported) names in moduleName from the current module. |
---|
| 3107 | |
---|
| 3108 | |
---|
| 3109 | \subsubsection{Module Import} |
---|
| 3110 | |
---|
| 3111 | The syntax to import a module is import moduleName; or import moduleName as anotherName;. |
---|
| 3112 | One package cannot be imported with both of the two types of syntax in one file. |
---|
| 3113 | A package imported in one file will only be visible in this file. |
---|
| 3114 | For example, two files, A and B belong to the same module. |
---|
| 3115 | If file A imports another module, M, the exported names in M are not visible in file B. |
---|
| 3116 | |
---|
| 3117 | All of the exported names are visible in the file that imports the module. |
---|
| 3118 | The exported names can be accessed within a namespace based on the module name in the first syntax (ex moduleName.foo). |
---|
| 3119 | If moduleName has several elements separated by '/' to describe a sub-module (ex. import container/vector;), the last element in the moduleName is used as the namespace to access the visible names in that module (ex vector.add(...);). |
---|
| 3120 | The as keyword is used to confine the imported names in a unique namespace (ex. anotherName.foo). anotherName must be a valid identifier (same rules as a variable name) which means it cannot have '/' in it. |
---|
| 3121 | Conflicts in namespaces will be reported by the compiler. |
---|
| 3122 | The second method can be used to solve conflicting name problems. |
---|
| 3123 | The following code snippets show the two situations. |
---|
| 3124 | |
---|
| 3125 | \begin{lstlisting} |
---|
| 3126 | module util/counter; |
---|
| 3127 | export int f(int i) { return i+1; } |
---|
| 3128 | |
---|
| 3129 | import util/counter; |
---|
| 3130 | |
---|
| 3131 | int main() { |
---|
| 3132 | return counter.f(200); // f() from the package counter |
---|
| 3133 | } |
---|
| 3134 | |
---|
| 3135 | import util/counter as ct; |
---|
| 3136 | int main() { |
---|
| 3137 | return ct.f(200); // f() from the package counter |
---|
| 3138 | } |
---|
| 3139 | \end{lstlisting} |
---|
| 3140 | |
---|
| 3141 | |
---|
| 3142 | Additionally, using the .as. syntax, a user can force the compiler to add the imported names into the current namespace using .as ..With these module rules, the following module definitions and imports can be achieved without any problem. |
---|
| 3143 | |
---|
| 3144 | \begin{lstlisting} |
---|
| 3145 | module M1; |
---|
| 3146 | export int f(int i) { return i+1;} // visible outside |
---|
| 3147 | |
---|
| 3148 | int g(int i) { return i-1;} // not visible outside |
---|
| 3149 | |
---|
| 3150 | module M2; |
---|
| 3151 | int f(int i) { return i * 2; } // not visible outside |
---|
| 3152 | export int g(int g) { return i / 2; } // visible outside |
---|
| 3153 | |
---|
| 3154 | import M1 as .; |
---|
| 3155 | |
---|
| 3156 | import M2 as .; |
---|
| 3157 | |
---|
| 3158 | |
---|
| 3159 | int main() { |
---|
| 3160 | return f(3) + g(4); //f() from M1 and g() from M2; |
---|
| 3161 | } |
---|
| 3162 | \end{lstlisting} |
---|
| 3163 | |
---|
| 3164 | |
---|
| 3165 | \subsubsection{Sub-Module and Module Aggregation} |
---|
| 3166 | |
---|
| 3167 | Several modules can be organized in a parent module and sub-modules relationship. |
---|
| 3168 | The sub-module names are based on hierarchical naming, and use slash, "/", to indicate the relationship. |
---|
| 3169 | For example, std/vector and std/io are sub-modules of module std. |
---|
| 3170 | The exported names in a sub-module are NOT visible if the parent module is imported, which means the exported names in the sub-module are |
---|
| 3171 | not implicitly exported in the parent module. |
---|
| 3172 | |
---|
| 3173 | Aggregation is a mechanism to support components and simplified importing. |
---|
| 3174 | The mechanism is not based on naming but based on manual declaration. |
---|
| 3175 | For example, the following is the aggregated sequence module. |
---|
| 3176 | The export {...} is syntactic sugar for many lines of export module aModule;. |
---|
| 3177 | If an aggregated module is imported, all the included modules in the aggregation are imported. |
---|
| 3178 | |
---|
| 3179 | \begin{lstlisting} |
---|
| 3180 | module std/sequence; |
---|
| 3181 | |
---|
| 3182 | export { |
---|
| 3183 | module std/vector; |
---|
| 3184 | module std/list; |
---|
| 3185 | module std/array; |
---|
| 3186 | module std/deque; |
---|
| 3187 | module std/forward_list; |
---|
| 3188 | module std/queue; |
---|
| 3189 | module std/stack; |
---|
| 3190 | }; |
---|
| 3191 | \end{lstlisting} |
---|
| 3192 | |
---|
| 3193 | After importing the aggregated module, each individual name is still contained in the original name space. |
---|
| 3194 | For example, vector.add() and list.add() should be used to reference the add methods if there are add methods in both the vector module and the list module. |
---|
| 3195 | |
---|
| 3196 | |
---|
| 3197 | \subsubsection{Import from Repository} |
---|
| 3198 | |
---|
| 3199 | When a module is imported, the tools locate the module in the one of the accessible package paths (defined by command line flag or environment variable). |
---|
| 3200 | The tools also support retrieving modules of a package from external repositories. |
---|
| 3201 | See Listing 40: Package directory structure |
---|
| 3202 | |
---|
| 3203 | |
---|
| 3204 | \subsubsection{Package Import} |
---|
| 3205 | |
---|
| 3206 | Because packages are the places where the building tool looks for modules, there is no code required in the \CFA source file to import a package. |
---|
| 3207 | In order to use modules in a package, the programmer needs to guide the building tool to locate the right package by 1) Adding the package's parent path into \$DOPATH; |
---|
| 3208 | or 2) Adding the package dependence into the current project's Do.prj. |
---|
| 3209 | More details about locating a module in a package are explained in the next section. |
---|
| 3210 | |
---|
| 3211 | |
---|
| 3212 | \subsubsection{Package Versioning} |
---|
| 3213 | |
---|
| 3214 | A package must have a version number. |
---|
| 3215 | The version number is a string. |
---|
| 3216 | For example "1.0", "1.a", "A1", and "1ec5fab753eb979d3886a491845b8ae152d58c8f" are all valid version numbers. |
---|
| 3217 | By convention, a package is stored in a directory named packageName-packageVersion. |
---|
| 3218 | For example, the util package with version 1.1 is stored in a directory named util-1.1. |
---|
| 3219 | |
---|
| 3220 | The project description file can optionally specify the version of the package used in the current project. |
---|
| 3221 | If not defined, because the version number is a string, and all the different versions for the same package will be sorted in increasing order, the package with the largest version number will be used in the compilation. |
---|
| 3222 | The builder tool will record the specific package version used in the build in the project's "Do.lock" file to enable fully repeatable builds. |
---|
| 3223 | |
---|
| 3224 | |
---|
| 3225 | \subsection{Module and Package Organization} |
---|
| 3226 | |
---|
| 3227 | \CFA has two level of encapsulations, module and package. |
---|
| 3228 | This section explains the object model of modules, packages and other language concepts. |
---|
| 3229 | It also explains how programmers should organize their code, and the method used by the build tools to locate packages, and import modules for compilation. |
---|
| 3230 | |
---|
| 3231 | |
---|
| 3232 | \subsubsection{Object Model} |
---|
| 3233 | |
---|
| 3234 | There are several concepts in Do. |
---|
| 3235 | \begin{itemize} |
---|
| 3236 | \item |
---|
| 3237 | File: a \CFA source file |
---|
| 3238 | \item |
---|
| 3239 | Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside |
---|
| 3240 | \item |
---|
| 3241 | Package: a container to organize modules for distribution; It has attributes like name, author, |
---|
| 3242 | version, dependences, etc. |
---|
| 3243 | \item |
---|
| 3244 | Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc. |
---|
| 3245 | \end{itemize} |
---|
| 3246 | |
---|
| 3247 | The following rules summarize the object model of all the above concepts: |
---|
| 3248 | \begin{itemize} |
---|
| 3249 | \item |
---|
| 3250 | A module contains one or more files |
---|
| 3251 | \begin{itemize} |
---|
| 3252 | \item |
---|
| 3253 | One file can only belong to one module |
---|
| 3254 | \item |
---|
| 3255 | A module has its name and interfaces exported |
---|
| 3256 | \item |
---|
| 3257 | A file without a module declaration at the beginning belongs to the global module |
---|
| 3258 | \item |
---|
| 3259 | \end{itemize} |
---|
| 3260 | |
---|
| 3261 | \item |
---|
| 3262 | A package contains one or more modules |
---|
| 3263 | \begin{itemize} |
---|
| 3264 | \item |
---|
| 3265 | A package has additional meta info described in Do.prj file |
---|
| 3266 | \item |
---|
| 3267 | A package may be dependent on other packages. |
---|
| 3268 | \end{itemize} |
---|
| 3269 | |
---|
| 3270 | \item |
---|
| 3271 | A project contains one or more modules in its source code |
---|
| 3272 | \begin{itemize} |
---|
| 3273 | \item |
---|
| 3274 | A project has additional meta info described in Do.prj file |
---|
| 3275 | \item |
---|
| 3276 | A project may be dependent on other packages |
---|
| 3277 | \item |
---|
| 3278 | A project can be transformed into a package for distribution |
---|
| 3279 | \item |
---|
| 3280 | A project can generate one or more executable binaries |
---|
| 3281 | \end{itemize} |
---|
| 3282 | \end{itemize} |
---|
| 3283 | |
---|
| 3284 | |
---|
| 3285 | \subsubsection{Module File Organization} |
---|
| 3286 | |
---|
| 3287 | The rules of this section are the conventions to organize module files in one package. |
---|
| 3288 | |
---|
| 3289 | The file location of a module in a package must match the module/submodule naming hierarchy. |
---|
| 3290 | The names separated by slash "/" must match the directory levels. |
---|
| 3291 | If only one file is used to implement one module, there is no need to put the module implementation file inside a sub-directory. |
---|
| 3292 | The file can be put inside its parent module's sub-directory with the sub module's name as the file name. |
---|
| 3293 | |
---|
| 3294 | Here is an example of a package, util. |
---|
| 3295 | \begin{lstlisting} |
---|
| 3296 | + util |
---|
| 3297 | Do.prj #package description file |
---|
| 3298 | heap.do #Case 1: module heap; |
---|
| 3299 | list.do #Case 1: mdoule list; |
---|
| 3300 | ring.do #Case 1: module ring; |
---|
| 3301 | + string #Case 2 |
---|
| 3302 | impl1.do #module string; |
---|
| 3303 | + std |
---|
| 3304 | vector.do |
---|
| 3305 | list.do |
---|
| 3306 | + array #Case 3 |
---|
| 3307 | array1.do #module std/array; |
---|
| 3308 | array2.do #module std/array; |
---|
| 3309 | sequence.do #Case 4, module std/sequence; |
---|
| 3310 | test.do #Case 5 |
---|
| 3311 | \end{lstlisting} |
---|
| 3312 | |
---|
| 3313 | \begin{itemize} |
---|
| 3314 | \item |
---|
| 3315 | Case 1: Each individual file implements a module |
---|
| 3316 | \item |
---|
| 3317 | Case 2: Put the implementation of a module under the sub-directory, but there is only one file |
---|
| 3318 | \item |
---|
| 3319 | Case 3: Put the implementation of a module under the sub-directory; There are several files to |
---|
| 3320 | implement one module |
---|
| 3321 | \item |
---|
| 3322 | Case 4: One file to express one aggregation |
---|
| 3323 | \item |
---|
| 3324 | Case 5: The file does not belong to any module; It is used for testing purpose |
---|
| 3325 | \end{itemize} |
---|
| 3326 | |
---|
| 3327 | The example only uses source code, ".do" files, to show the module file organization. |
---|
| 3328 | Other module packaging formats, like binary, must also follow the same rules. |
---|
| 3329 | |
---|
| 3330 | |
---|
| 3331 | \subsection{Module File Format} |
---|
| 3332 | |
---|
| 3333 | \CFA supports different types of module file formats. |
---|
| 3334 | |
---|
| 3335 | \begin{itemize} |
---|
| 3336 | \item |
---|
| 3337 | Pure source code format: The files should be organized following the previous section's definition. |
---|
| 3338 | \item |
---|
| 3339 | IR format (TBD): The \CFA compiler IR format, similar to the source code format |
---|
| 3340 | \item |
---|
| 3341 | Binary format, including ".a" static library or ".so" dynamic linkage library |
---|
| 3342 | \begin{itemize} |
---|
| 3343 | \item |
---|
| 3344 | The file's name must match the right level's module name defined in the previous section |
---|
| 3345 | \item |
---|
| 3346 | E.g. "util.so" includes all modules for the package util. |
---|
| 3347 | \item |
---|
| 3348 | E.g. "string.so" under the package directory to include files belonging to "module string;" |
---|
| 3349 | \end{itemize} |
---|
| 3350 | \item. |
---|
| 3351 | Archive format |
---|
| 3352 | \begin{itemize} |
---|
| 3353 | \item |
---|
| 3354 | The archive is named as ".dar", and is a zip archive of the source code or the binary for a package |
---|
| 3355 | \item |
---|
| 3356 | E.g. "util.dar" is the whole package for util package including the package direction file |
---|
| 3357 | \end{itemize} |
---|
| 3358 | \item |
---|
| 3359 | Hybrid format |
---|
| 3360 | \begin{itemize} |
---|
| 3361 | \item |
---|
| 3362 | A package can be distributed partly in source code, partly in binary format, and/or packaged in the archive format |
---|
| 3363 | \item |
---|
| 3364 | The only limitation is that the names of the files must match the module location names defined in previous section |
---|
| 3365 | \end{itemize} |
---|
| 3366 | \end{itemize} |
---|
| 3367 | Package and Module Locating and the \CFA Language Tooling documentation for more details. |
---|
| 3368 | |
---|
| 3369 | |
---|
| 3370 | \subsection{Packages} |
---|
| 3371 | |
---|
| 3372 | A package is synonymous with a library in other languages. |
---|
| 3373 | The intent of the package level encapsulation is to facilitate code distribution, version control, and dependence management. |
---|
| 3374 | A package is a physical grouping of one or more modules in a directory (an archive file for a directory). |
---|
| 3375 | The concept of a package is the convention for grouping code, and the contract between the language and the building tool to search for imported modules. |
---|
| 3376 | |
---|
| 3377 | |
---|
| 3378 | \subsubsection{Package Definition} |
---|
| 3379 | |
---|
| 3380 | A package is defined by putting a project description file, Do.prj, with one or more modules into a directory. |
---|
| 3381 | This project description file contains the package's meta data, including package name, author, version, dependences, etc. |
---|
| 3382 | It should be in the root of the package directory. |
---|
| 3383 | |
---|
| 3384 | The modules in the package could be either source code, or compiled binary format. |
---|
| 3385 | The location of the module files should follow the module name's path. |
---|
| 3386 | |
---|
| 3387 | Here is a simple example of the directory structure of a package, core. |
---|
| 3388 | It contains a module std and several sub-modules under std. |
---|
| 3389 | \begin{lstlisting} |
---|
| 3390 | + core |
---|
| 3391 | Do.prj |
---|
| 3392 | + std |
---|
| 3393 | + io |
---|
| 3394 | file.do # module std/io/file; |
---|
| 3395 | network.do #module std/io/network; |
---|
| 3396 | + container |
---|
| 3397 | vector.do #module std/container/vector; |
---|
| 3398 | list.do #module std/container/list; |
---|
| 3399 | \end{lstlisting} |
---|
| 3400 | |
---|
| 3401 | |
---|
| 3402 | \subsubsection{Package Import} |
---|
| 3403 | |
---|
| 3404 | Because packages are the places where the building tool looks for modules, there is no code required in the \CFA source file to import a package. |
---|
| 3405 | In order to use modules in a package, the programmer needs to guide the building tool to locate the right package by 1) Adding the package's parent path into \$DOPATH; or 2) Adding the package dependence into the current project's Do.prj. |
---|
| 3406 | More details about locating a module in a package are explained in the next section. |
---|
| 3407 | |
---|
| 3408 | |
---|
| 3409 | \subsubsection{Package Versioning} |
---|
| 3410 | |
---|
| 3411 | A package must have a version number. |
---|
| 3412 | The version number is a string. |
---|
| 3413 | For example "1.0", "1.a", "A1", and "1ec5fab753eb979d3886a491845b8ae152d58c8f" are all valid version numbers. |
---|
| 3414 | By convention, a package is stored in a directory named packageName-packageVersion. |
---|
| 3415 | For example, the util package with version 1.1 is stored in a directory named util-1.1. |
---|
| 3416 | |
---|
| 3417 | The project description file can optionally specify the version of the package used in the current project. |
---|
| 3418 | If not defined, because the version number is a string, and all the different versions for the same package will be sorted in increasing order, the package with the largest version number will be used in the compilation. |
---|
| 3419 | The builder tool will record the specific package version used in the build in the project's "Do.lock" file to enable fully repeatable builds. |
---|
| 3420 | |
---|
| 3421 | |
---|
| 3422 | \subsection{Module and Package Organization} |
---|
| 3423 | |
---|
| 3424 | \CFA has two level of encapsulations, module and package. |
---|
| 3425 | This section explains the object model of modules, packages and other language concepts. |
---|
| 3426 | It also explains how programmers should organize their code, and the method used by the build tools to locate packages, and import modules for compilation. |
---|
| 3427 | |
---|
| 3428 | |
---|
| 3429 | \subsubsection{Object Model} |
---|
| 3430 | |
---|
| 3431 | There are several concepts in Do. |
---|
| 3432 | \begin{itemize} |
---|
| 3433 | \item |
---|
| 3434 | File: a \CFA source file |
---|
| 3435 | \item |
---|
| 3436 | Module: a container to organize a set of related types and methods; It has a module name, and several interfaces visible from outside |
---|
| 3437 | \item |
---|
| 3438 | Package: a container to organize modules for distribution; It has attributes like name, author, version, dependences, etc. |
---|
| 3439 | \item |
---|
| 3440 | Project: a working set for a \CFA project; It has attributes like name, author, version, dependences, etc. |
---|
| 3441 | \end{itemize} |
---|
| 3442 | |
---|
| 3443 | The following rules summarize the object model of all the above concepts: |
---|
| 3444 | \begin{itemize} |
---|
| 3445 | \item |
---|
| 3446 | A module contains one or more files |
---|
| 3447 | \begin{itemize} |
---|
| 3448 | \item |
---|
| 3449 | One file can only belong to one module |
---|
| 3450 | \item |
---|
| 3451 | A module has its name and interfaces exported |
---|
| 3452 | \item |
---|
| 3453 | A file without a module declaration at the beginning belongs to the global module |
---|
| 3454 | \end{itemize} |
---|
| 3455 | \item |
---|
| 3456 | A package contains one or more modules |
---|
| 3457 | \begin{itemize} |
---|
| 3458 | \item |
---|
| 3459 | A package has additional meta info described in Do.prj file |
---|
| 3460 | \item |
---|
| 3461 | A package may be dependent on other packages. |
---|
| 3462 | \end{itemize} |
---|
| 3463 | \item |
---|
| 3464 | A project contains one or more modules in its source code |
---|
| 3465 | \begin{itemize} |
---|
| 3466 | \item |
---|
| 3467 | A project has additional meta info described in Do.prj file |
---|
| 3468 | \item |
---|
| 3469 | A project may be dependent on other packages |
---|
| 3470 | \item |
---|
| 3471 | A project can be transformed into a package for distribution |
---|
| 3472 | \item |
---|
| 3473 | A project can generate one or more executable binaries |
---|
| 3474 | \end{itemize} |
---|
| 3475 | \end{itemize} |
---|
| 3476 | |
---|
| 3477 | |
---|
| 3478 | \subsubsection{Module File Organization} |
---|
| 3479 | |
---|
| 3480 | The rules of this section are the conventions to organize module files in one package. |
---|
| 3481 | |
---|
| 3482 | The file location of a module in a package must match the module/submodule naming hierarchy. |
---|
| 3483 | The names separated by slash "/" must match the directory levels. |
---|
| 3484 | If only one file is used to implement one module, there is no need to put the module implementation file inside a sub-directory. |
---|
| 3485 | The file can be put inside its parent module's sub-directory with the sub module's name as the file name. |
---|
| 3486 | |
---|
| 3487 | Here is an example of a package, util. |
---|
| 3488 | \begin{lstlisting} |
---|
| 3489 | + util |
---|
| 3490 | Do.prj #package description file |
---|
| 3491 | heap.do #Case 1: module heap; |
---|
| 3492 | list.do #Case 1: mdoule list; |
---|
| 3493 | ring.do #Case 1: module ring; |
---|
| 3494 | + string #Case 2 |
---|
| 3495 | impl1.do #module string; |
---|
| 3496 | + std |
---|
| 3497 | vector.do |
---|
| 3498 | list.do |
---|
| 3499 | + array #Case 3 |
---|
| 3500 | array1.do #module std/array; |
---|
| 3501 | array2.do #module std/array; |
---|
| 3502 | sequence.do #Case 4, module std/sequence; |
---|
| 3503 | test.do #Case 5 |
---|
| 3504 | \end{lstlisting} |
---|
| 3505 | |
---|
| 3506 | |
---|
| 3507 | \begin{itemize} |
---|
| 3508 | \item |
---|
| 3509 | Case 1: Each individual file implements a module |
---|
| 3510 | \item |
---|
| 3511 | Case 2: Put the implementation of a module under the sub-directory, but there is only one file |
---|
| 3512 | \item |
---|
| 3513 | Case 3: Put the implementation of a module under the sub-directory; There are several files to implement one module |
---|
| 3514 | \item |
---|
| 3515 | Case 4: One file to express one aggregation |
---|
| 3516 | \item |
---|
| 3517 | Case 5: The file does not belong to any module; It is used for testing purpose |
---|
| 3518 | \end{itemize} |
---|
| 3519 | |
---|
| 3520 | The example only uses source code, ".do" files, to show the module file organization. |
---|
| 3521 | Other module packaging formats, like binary, must also follow the same rules. |
---|
| 3522 | |
---|
| 3523 | |
---|
| 3524 | \subsubsection{Module File Format} |
---|
| 3525 | |
---|
| 3526 | \CFA supports different types of module file formats. |
---|
| 3527 | |
---|
| 3528 | \begin{itemize} |
---|
| 3529 | \item |
---|
| 3530 | Pure source code format: The files should be organized following the previous section's definition. |
---|
| 3531 | \item |
---|
| 3532 | IR format (TBD): The \CFA compiler IR format, similar to the source code format |
---|
| 3533 | \item |
---|
| 3534 | Binary format, including ".a" static library or ".so" dynamic linkage library |
---|
| 3535 | \begin{itemize} |
---|
| 3536 | \item |
---|
| 3537 | The file's name must match the right level's module name defined in the previous section |
---|
| 3538 | \item |
---|
| 3539 | E.g. "util.so" includes all modules for the package util. |
---|
| 3540 | \item |
---|
| 3541 | E.g. "string.so" under the package directory to include files belonging to "module string;" |
---|
| 3542 | \end{itemize} |
---|
| 3543 | \item |
---|
| 3544 | Archive format |
---|
| 3545 | \begin{itemize} |
---|
| 3546 | \item |
---|
| 3547 | The archive is named as ".dar", and is a zip archive of the source code or the binary for a package |
---|
| 3548 | \item |
---|
| 3549 | E.g. "util.dar" is the whole package for util package including the package direction file |
---|
| 3550 | \end{itemize} |
---|
| 3551 | \item |
---|
| 3552 | Hybrid format |
---|
| 3553 | \begin{itemize} |
---|
| 3554 | \item |
---|
| 3555 | A package can be distributed partly in source code, partly in binary format, and/or packaged in the archive format |
---|
| 3556 | \item |
---|
| 3557 | The only limitation is that the names of the files must match the module location names defined in previous section |
---|
| 3558 | \end{itemize} |
---|
| 3559 | \end{itemize} |
---|
| 3560 | |
---|
| 3561 | |
---|
| 3562 | \subsection{Package and Module Locating} |
---|
| 3563 | |
---|
| 3564 | The high-level build tools provided by \CFA will handle finding a package in your local filesystem or retrieving it from a repository if necessary, building it if necessary, and linking with it. |
---|
| 3565 | If a programmer prefers, one can directly call the compiler, docc to build the source files and create and link to static libraries. |
---|
| 3566 | |
---|
| 3567 | When a source file imports a module, the \CFA build tool and docc compiler will locate the module according to the following order: |
---|
| 3568 | |
---|
| 3569 | \begin{enumerate} |
---|
| 3570 | \item |
---|
| 3571 | This source file's directory tree, which is typically the project's src directory |
---|
| 3572 | \item |
---|
| 3573 | All of the dependent packages (in a directory or in an archive file) under the current \CFA project's pkg directory |
---|
| 3574 | \item |
---|
| 3575 | The dependent packages (in a directory or in an archive file) inside the paths defined in the DOPATH environment variable |
---|
| 3576 | \item |
---|
| 3577 | The dependent packages (in a directory or in an archive file) inside the global \CFA SDK installation's pkg directory |
---|
| 3578 | \item |
---|
| 3579 | If one dependent package is still not found, the builder tool will automatically retrieve it from the repository defined in the SDK installation's configuration, and store it in the SDK's pkg directory |
---|
| 3580 | \end{enumerate} |
---|
| 3581 | |
---|
| 3582 | The module found first in a package will shadow the modules with the same name in the later packages in the search sequence. |
---|
| 3583 | |
---|
| 3584 | |
---|
| 3585 | \subsubsection{Dependent Package} |
---|
| 3586 | |
---|
| 3587 | Dependent packages are those packages containing modules that the current project's source code will import from. |
---|
| 3588 | Dependent packages are defined implicitly or explicitly in one \CFA project. |
---|
| 3589 | All of the packages under the current project's pkg directory are implicitly dependent packages. |
---|
| 3590 | For others, the dependent packages must be defined in the project's Do.prj file. |
---|
| 3591 | |
---|
| 3592 | |
---|
| 3593 | \subsubsection{Package and Module Locating Example} |
---|
| 3594 | |
---|
| 3595 | \begin{lstlisting} |
---|
| 3596 | # A project's source code tree |
---|
| 3597 | |
---|
| 3598 | -------------------------------------- |
---|
| 3599 | |
---|
| 3600 | + testProject |
---|
| 3601 | Do.prj |
---|
| 3602 | + src |
---|
| 3603 | main.do |
---|
| 3604 | + pkg |
---|
| 3605 | + security-1.1 |
---|
| 3606 | Do.prj |
---|
| 3607 | security.do #module security |
---|
| 3608 | |
---|
| 3609 | -------------------------------------- |
---|
| 3610 | |
---|
| 3611 | # Do.prj |
---|
| 3612 | |
---|
| 3613 | -------------------------------------- |
---|
| 3614 | |
---|
| 3615 | [dependences] |
---|
| 3616 | std |
---|
| 3617 | util = "0.2" |
---|
| 3618 | |
---|
| 3619 | -------------------------------------- |
---|
| 3620 | |
---|
| 3621 | # main.do |
---|
| 3622 | |
---|
| 3623 | --------------------------------------- |
---|
| 3624 | |
---|
| 3625 | import security; |
---|
| 3626 | import std/vector; |
---|
| 3627 | import container; |
---|
| 3628 | |
---|
| 3629 | ---------------------------------------- |
---|
| 3630 | \end{lstlisting} |
---|
| 3631 | |
---|
| 3632 | |
---|
| 3633 | \begin{lstlisting} |
---|
| 3634 | # pkg directory's source code tree |
---|
| 3635 | |
---|
| 3636 | ----------------------------------------- |
---|
| 3637 | |
---|
| 3638 | + pkg |
---|
| 3639 | + std-1.0 |
---|
| 3640 | Do.prj |
---|
| 3641 | vector.do #module std/vector; |
---|
| 3642 | queue.do #module std/queue; |
---|
| 3643 | + std-1.1 |
---|
| 3644 | Do.prj |
---|
| 3645 | vector.do #module std/vector; |
---|
| 3646 | queue.do #module std/queue; |
---|
| 3647 | list.do #module std/list; |
---|
| 3648 | + util-0.1 |
---|
| 3649 | Do.prj |
---|
| 3650 | container.do #module container; |
---|
| 3651 | + security-1.0 |
---|
| 3652 | security.do #module security; |
---|
| 3653 | ------------------------------------------ |
---|
| 3654 | \end{lstlisting} |
---|
| 3655 | |
---|
| 3656 | |
---|
| 3657 | During the compiling of main.do file import security; |
---|
| 3658 | The security module appears in both the local security-1.1 package, and the global security-1.0 package. |
---|
| 3659 | According to the locating sequence, the local security module in security-1.1 will be used. |
---|
| 3660 | And because the security-1.1 package is under local's pkg directory. |
---|
| 3661 | No dependence description is required in the project Do.prj file. |
---|
| 3662 | |
---|
| 3663 | import std/vector; |
---|
| 3664 | |
---|
| 3665 | The std/vector package appears in two different versions' packages in the global path and the project dependence doesn't specify the version. std-1.1 is used in this case. |
---|
| 3666 | |
---|
| 3667 | import container; |
---|
| 3668 | |
---|
| 3669 | The Do.prj specifies the version 0.2 should be used to locate container module from util package but only version 0.1 is available in the local file system. |
---|
| 3670 | The builder tool then will try to retrieve it from the web and store it in the global pkg directory. |
---|
| 3671 | After that, the container module from the newly downloaded package will be used in the compilation. |
---|
| 3672 | \end{comment} |
---|
| 3673 | |
---|
| 3674 | |
---|
| 3675 | \section{Comparison with Other Languages} |
---|
| 3676 | |
---|
| 3677 | \CFA is one of many languages that attempts to improve upon C. |
---|
| 3678 | In developing \CFA, many other languages were consulted for ideas, constructs, and syntax. |
---|
| 3679 | Therefore, it is important to show how these languages each compare with Do. |
---|
[07bc165] | 3680 | In this section, \CFA is compared with what the writers of this document consider to be the closest competitors of Do: \Index*[C++]{\CC}, \Index*{Go}, \Index*{Rust}, and \Index*{D}. |
---|
[6c91065] | 3681 | |
---|
| 3682 | |
---|
[b72bad4f] | 3683 | \subsection[Comparing Key Features of CFA]{Comparing Key Features of \CFA} |
---|
[6c91065] | 3684 | |
---|
| 3685 | |
---|
[07bc165] | 3686 | {% local change to lstlising to reduce font size |
---|
[6c91065] | 3687 | |
---|
[07bc165] | 3688 | |
---|
| 3689 | \lstset{basicstyle=\linespread{0.9}\sf\relsize{-2}} |
---|
| 3690 | |
---|
| 3691 | |
---|
| 3692 | \subsubsection{Constructors and Destructors} |
---|
[6c91065] | 3693 | |
---|
| 3694 | \begin{flushleft} |
---|
| 3695 | \begin{tabular}{@{}l|l|l|l@{}} |
---|
| 3696 | \multicolumn{1}{c|}{\textbf{\CFA}} & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ |
---|
| 3697 | \hline |
---|
| 3698 | \begin{lstlisting} |
---|
| 3699 | struct Line { |
---|
[e229c22] | 3700 | float lnth; |
---|
[6c91065] | 3701 | } |
---|
| 3702 | // default constructor |
---|
| 3703 | void ?{}( Line * l ) { |
---|
[e229c22] | 3704 | l->lnth = 0.0; |
---|
[6c91065] | 3705 | sout | "default" | endl; |
---|
| 3706 | } |
---|
| 3707 | |
---|
| 3708 | |
---|
| 3709 | // constructor with length |
---|
[e229c22] | 3710 | void ?{}( Line * l, float lnth ) { |
---|
| 3711 | l->lnth = lnth; |
---|
| 3712 | sout | "lnth" | l->lnth | endl; |
---|
[6c91065] | 3713 | |
---|
| 3714 | } |
---|
| 3715 | |
---|
| 3716 | // destructor |
---|
[e229c22] | 3717 | void ^?() { |
---|
[6c91065] | 3718 | sout | "destroyed" | endl; |
---|
[e229c22] | 3719 | l.lnth = 0.0; |
---|
[6c91065] | 3720 | } |
---|
| 3721 | |
---|
| 3722 | // usage |
---|
| 3723 | Line line1; |
---|
[e229c22] | 3724 | Line line2 = { 3.4 }; |
---|
[6c91065] | 3725 | \end{lstlisting} |
---|
| 3726 | & |
---|
| 3727 | \begin{lstlisting}[language=C++] |
---|
| 3728 | class Line { |
---|
[e229c22] | 3729 | float lnth; |
---|
[6c91065] | 3730 | |
---|
| 3731 | // default constructor |
---|
| 3732 | Line() { |
---|
| 3733 | cout << "default" << endl; |
---|
[e229c22] | 3734 | lnth = 0.0; |
---|
[6c91065] | 3735 | } |
---|
| 3736 | |
---|
| 3737 | |
---|
[e229c22] | 3738 | // constructor with lnth |
---|
[6c91065] | 3739 | Line( float l ) { |
---|
| 3740 | cout << "length " << length |
---|
| 3741 | << endl; |
---|
| 3742 | length = l; |
---|
| 3743 | } |
---|
| 3744 | |
---|
| 3745 | // destructor |
---|
| 3746 | ~Line() { |
---|
| 3747 | cout << "destroyed" << endl; |
---|
| 3748 | length = 0.0; |
---|
| 3749 | } |
---|
| 3750 | } |
---|
| 3751 | // usage |
---|
| 3752 | Line line1; |
---|
| 3753 | Line line2( 3.4 ); |
---|
| 3754 | \end{lstlisting} |
---|
| 3755 | & |
---|
| 3756 | \begin{lstlisting}[language=Golang] |
---|
| 3757 | type Line struct { |
---|
| 3758 | length float32 |
---|
| 3759 | } |
---|
| 3760 | // default constructor |
---|
| 3761 | func makeLine() Line { |
---|
| 3762 | fmt.PrintLn( "default" ) |
---|
| 3763 | return Line{0.0} |
---|
| 3764 | } |
---|
| 3765 | |
---|
| 3766 | |
---|
| 3767 | // constructor with length |
---|
| 3768 | func makeLine( length float32 ) Line { |
---|
| 3769 | fmt.Printf( "length %v", length ) |
---|
| 3770 | |
---|
| 3771 | return Line{length} |
---|
| 3772 | } |
---|
| 3773 | |
---|
| 3774 | // no destructor |
---|
| 3775 | |
---|
| 3776 | |
---|
| 3777 | |
---|
| 3778 | |
---|
| 3779 | |
---|
| 3780 | // usage |
---|
| 3781 | line1 := makeLine() |
---|
| 3782 | line2 := makeLine( 3.4 ) |
---|
| 3783 | \end{lstlisting} |
---|
| 3784 | & |
---|
| 3785 | \begin{lstlisting} |
---|
| 3786 | struct Line { |
---|
| 3787 | length: f32 |
---|
| 3788 | } |
---|
| 3789 | // default constructor |
---|
| 3790 | impl Default for Line { |
---|
| 3791 | fn default () -> Line { |
---|
| 3792 | println!( "default" ); |
---|
| 3793 | Line{ length: 0.0 } |
---|
| 3794 | } |
---|
| 3795 | } |
---|
| 3796 | // constructor with length |
---|
| 3797 | impl Line { |
---|
| 3798 | fn make( len: f32 ) -> Line { |
---|
| 3799 | println!( "length: {}", len ); |
---|
| 3800 | Line{ length: len } |
---|
| 3801 | } |
---|
| 3802 | } |
---|
| 3803 | // destructor |
---|
| 3804 | impl Drop for Line { |
---|
| 3805 | fn drop( &mut self ) { |
---|
| 3806 | self.length = 0.0 |
---|
| 3807 | } |
---|
| 3808 | } |
---|
| 3809 | // usage |
---|
| 3810 | let line1:Line = Default::default(); |
---|
| 3811 | Line line2( 3.4 ); |
---|
| 3812 | \end{lstlisting} |
---|
| 3813 | \end{tabular} |
---|
| 3814 | \end{flushleft} |
---|
| 3815 | |
---|
| 3816 | |
---|
| 3817 | \subsubsection{Operator Overloading} |
---|
| 3818 | |
---|
| 3819 | \begin{flushleft} |
---|
| 3820 | \begin{tabular}{@{}l|l|l|l@{}} |
---|
| 3821 | \multicolumn{1}{c|}{\textbf{\CFA}} & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ |
---|
| 3822 | \hline |
---|
| 3823 | \begin{lstlisting} |
---|
| 3824 | struct Cpx { |
---|
| 3825 | double re, im; |
---|
| 3826 | }; |
---|
| 3827 | // overload addition operator |
---|
| 3828 | Cpx ?+?( Cpx l, const Cpx r ) { |
---|
| 3829 | return (Cpx){l.re+l.im, l.im+r.im}; |
---|
| 3830 | } |
---|
| 3831 | Cpx a, b, c; |
---|
| 3832 | c = a + b; |
---|
| 3833 | \end{lstlisting} |
---|
| 3834 | & |
---|
| 3835 | \begin{lstlisting} |
---|
| 3836 | struct Cpx { |
---|
| 3837 | double re, im; |
---|
| 3838 | }; |
---|
| 3839 | // overload addition operator |
---|
| 3840 | Cpx operator+( Cpx l, const Cpx r ) { |
---|
| 3841 | return (Cpx){l.re+l.im, l.im+r.im}; |
---|
| 3842 | } |
---|
| 3843 | Cpx a, b, c; |
---|
| 3844 | c = a + b; |
---|
| 3845 | \end{lstlisting} |
---|
| 3846 | & |
---|
| 3847 | \begin{lstlisting} |
---|
| 3848 | // no operator overloading |
---|
| 3849 | |
---|
| 3850 | |
---|
| 3851 | |
---|
| 3852 | |
---|
| 3853 | |
---|
| 3854 | |
---|
| 3855 | |
---|
| 3856 | \end{lstlisting} |
---|
| 3857 | & |
---|
| 3858 | \begin{lstlisting} |
---|
| 3859 | struct Cpx { |
---|
| 3860 | re: f32, |
---|
| 3861 | im: f32 |
---|
| 3862 | } |
---|
| 3863 | // overload addition operator |
---|
| 3864 | impl Add for Cpx { |
---|
| 3865 | type Output = Cpx |
---|
| 3866 | fn add(self, r: Cpx) -> Cpx { |
---|
| 3867 | let mut res = Cpx{re: 0.0, im: 0.0}; |
---|
| 3868 | res.re = self.re + r.re; |
---|
| 3869 | res.im = self.im + r.im; |
---|
| 3870 | return res |
---|
| 3871 | } |
---|
| 3872 | } |
---|
| 3873 | let (a, b, mut c) = ...; |
---|
| 3874 | c = a + b |
---|
| 3875 | \end{lstlisting} |
---|
| 3876 | \end{tabular} |
---|
| 3877 | \end{flushleft} |
---|
| 3878 | |
---|
| 3879 | |
---|
| 3880 | \subsubsection{Calling C Functions} |
---|
| 3881 | |
---|
| 3882 | \begin{flushleft} |
---|
| 3883 | \begin{tabular}{@{}l|l|l@{}} |
---|
| 3884 | \multicolumn{1}{c|}{\textbf{\CFA/\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ |
---|
| 3885 | \hline |
---|
[53ba273] | 3886 | \begin{lstlisting}[boxpos=t] |
---|
[6c91065] | 3887 | extern "C" { |
---|
| 3888 | #include <sys/types.h> |
---|
| 3889 | #include <sys/stat.h> |
---|
| 3890 | #include <unistd.h> |
---|
| 3891 | } |
---|
| 3892 | size_t fileSize( const char *path ) { |
---|
[53ba273] | 3893 | struct stat s; |
---|
[6c91065] | 3894 | stat(path, &s); |
---|
| 3895 | return s.st_size; |
---|
| 3896 | } |
---|
| 3897 | \end{lstlisting} |
---|
| 3898 | & |
---|
[53ba273] | 3899 | \begin{lstlisting}[boxpos=t] |
---|
[6c91065] | 3900 | /* |
---|
| 3901 | #cgo |
---|
| 3902 | #include <sys/types.h> |
---|
| 3903 | #include <sys/stat.h> |
---|
| 3904 | #include <unistd.h> |
---|
| 3905 | */ |
---|
| 3906 | import "C" |
---|
| 3907 | import "unsafe" |
---|
| 3908 | |
---|
| 3909 | func fileSize(path string) C.size_t { |
---|
| 3910 | var buf C.struct_stat |
---|
| 3911 | c_string := C.CString(path) |
---|
| 3912 | C.stat(p, &buf) |
---|
| 3913 | C.free(unsafe.Pointer(c_string)) |
---|
| 3914 | return buf._st_size |
---|
| 3915 | } |
---|
| 3916 | \end{lstlisting} |
---|
| 3917 | & |
---|
[53ba273] | 3918 | \begin{lstlisting}[boxpos=t] |
---|
[6c91065] | 3919 | use libc::{c_int, size_t}; |
---|
| 3920 | // translated from sys/stat.h |
---|
| 3921 | #[repr(C)] |
---|
| 3922 | struct stat_t { |
---|
| 3923 | ... |
---|
| 3924 | st_size: size_t, |
---|
| 3925 | ... |
---|
| 3926 | } |
---|
| 3927 | #[link(name = "libc")] |
---|
| 3928 | extern { |
---|
| 3929 | fn stat(path: *const u8, |
---|
| 3930 | buf: *mut stat_t) -> c_int; |
---|
| 3931 | } |
---|
| 3932 | fn fileSize(path: *const u8) -> size_t |
---|
| 3933 | { |
---|
| 3934 | unsafe { |
---|
[53ba273] | 3935 | let mut buf: stat_t = uninit(); |
---|
| 3936 | stat(path, &mut buf); |
---|
| 3937 | buf.st_size |
---|
[6c91065] | 3938 | } |
---|
| 3939 | } |
---|
| 3940 | \end{lstlisting} |
---|
| 3941 | \end{tabular} |
---|
| 3942 | \end{flushleft} |
---|
| 3943 | |
---|
| 3944 | |
---|
| 3945 | \subsubsection{Generic Functions} |
---|
| 3946 | |
---|
| 3947 | \begin{flushleft} |
---|
| 3948 | \begin{tabular}{@{}l|l|l|l@{}} |
---|
| 3949 | \multicolumn{1}{c|}{\textbf{\CFA}} & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ |
---|
| 3950 | \hline |
---|
| 3951 | \begin{lstlisting} |
---|
| 3952 | generic(type T, type N | |
---|
| 3953 | { int ?<?(N, N); }) |
---|
| 3954 | T *maximize(N (*f)(const T&), |
---|
| 3955 | int n, T *a) { |
---|
| 3956 | T *bestX = NULL; |
---|
| 3957 | N bestN; |
---|
| 3958 | for (int i = 0; i < n; i++) { |
---|
| 3959 | N curN = f(a[i]); |
---|
| 3960 | if (bestX == NULL || |
---|
| 3961 | curN > bestN) { |
---|
| 3962 | bestX = &a[i]; bestN = curN; |
---|
| 3963 | } |
---|
| 3964 | } |
---|
| 3965 | return bestX; |
---|
| 3966 | } |
---|
| 3967 | |
---|
| 3968 | string *longest(int n, string *p) |
---|
| 3969 | { |
---|
| 3970 | return maximize(length, n, p); |
---|
| 3971 | } |
---|
| 3972 | \end{lstlisting} |
---|
| 3973 | & |
---|
| 3974 | \begin{lstlisting} |
---|
| 3975 | template<typename T, typename F> |
---|
| 3976 | T *maximize(const F &f, |
---|
| 3977 | int n, T *a) { |
---|
| 3978 | typedef decltype(f(a[0])) N; |
---|
| 3979 | T *bestX = NULL; |
---|
| 3980 | N bestN; |
---|
| 3981 | for (int i = 0; i < n; i++) { |
---|
| 3982 | N curN = f(a[i]); |
---|
| 3983 | if (bestX == NULL || curN > bestN) |
---|
| 3984 | { |
---|
| 3985 | bestX = &a[i]; bestN = curN; |
---|
| 3986 | } |
---|
| 3987 | } |
---|
| 3988 | return bestX; |
---|
| 3989 | } |
---|
| 3990 | |
---|
| 3991 | string *longest(int n, string *p) { |
---|
| 3992 | return maximize( |
---|
| 3993 | [](const string &s) { |
---|
| 3994 | return s.length(); |
---|
| 3995 | }, n, p); |
---|
| 3996 | } |
---|
| 3997 | \end{lstlisting} |
---|
| 3998 | & |
---|
| 3999 | \begin{lstlisting} |
---|
| 4000 | // Go does not support generics! |
---|
| 4001 | func maximize( |
---|
| 4002 | gt func(interface{}, interface{}) bool, |
---|
| 4003 | f func(interface{}) interface{}, |
---|
| 4004 | a []interface{}) interface{} { |
---|
| 4005 | var bestX interface{} = nil |
---|
| 4006 | var bestN interface{} = nil |
---|
| 4007 | for _, x := range a { |
---|
| 4008 | curN := f(x) |
---|
| 4009 | if bestX == nil || gt(curN, bestN) |
---|
| 4010 | { |
---|
| 4011 | bestN = curN |
---|
| 4012 | bestX = x |
---|
| 4013 | } |
---|
| 4014 | } |
---|
| 4015 | return bestX |
---|
| 4016 | } |
---|
| 4017 | |
---|
| 4018 | func longest( |
---|
| 4019 | a []interface{}) interface{} { |
---|
| 4020 | return maximize( |
---|
| 4021 | func(a, b interface{}) bool { |
---|
| 4022 | return a.(int) > b.(int) }, |
---|
| 4023 | func(s interface{}) interface{} { |
---|
| 4024 | return len(s.(string)) }, |
---|
| 4025 | a).(string) |
---|
| 4026 | } |
---|
| 4027 | \end{lstlisting} |
---|
| 4028 | & |
---|
| 4029 | \begin{lstlisting} |
---|
| 4030 | use std::cmp::Ordering; |
---|
| 4031 | |
---|
| 4032 | fn maximize<N: Ord + Copy, T, F: |
---|
| 4033 | Fn(&T) -> N>(f: F, a: &Vec<T>) -> |
---|
| 4034 | Option<&T> { |
---|
| 4035 | let mut best_x: Option<&T> = None; |
---|
| 4036 | let mut best_n: Option<N> = None; |
---|
| 4037 | for x in a { |
---|
| 4038 | let n = f(x); |
---|
| 4039 | if (match best_n { None => true, |
---|
| 4040 | Some(bn) => |
---|
| 4041 | n.cmp(&bn) == Ordering::Greater }) |
---|
| 4042 | { |
---|
| 4043 | best_x = Some(x); |
---|
| 4044 | best_n = Some(n); |
---|
| 4045 | } |
---|
| 4046 | } |
---|
| 4047 | return best_x |
---|
| 4048 | } |
---|
| 4049 | |
---|
| 4050 | fn longest(a: &Vec<String>) -> |
---|
| 4051 | Option<&String> { |
---|
| 4052 | return |
---|
| 4053 | maximize(|x: &String| x.len(), a) |
---|
| 4054 | } |
---|
| 4055 | \end{lstlisting} |
---|
| 4056 | \end{tabular} |
---|
| 4057 | \end{flushleft} |
---|
| 4058 | |
---|
| 4059 | |
---|
[53ba273] | 4060 | \begin{comment} |
---|
[b72bad4f] | 4061 | \subsubsection{Modules / Packages} |
---|
[6c91065] | 4062 | |
---|
| 4063 | \begin{lstlisting} |
---|
| 4064 | \CFA |
---|
| 4065 | \CC |
---|
| 4066 | |
---|
| 4067 | |
---|
| 4068 | module example/M; |
---|
| 4069 | |
---|
| 4070 | export int inc(int val) { |
---|
| 4071 | return val + 1; |
---|
| 4072 | } |
---|
| 4073 | |
---|
| 4074 | |
---|
| 4075 | |
---|
| 4076 | |
---|
| 4077 | -------------------------------------- |
---|
| 4078 | //Use the module in another file |
---|
| 4079 | import example/M; |
---|
| 4080 | int main() { |
---|
| 4081 | print(M.inc(100)); |
---|
| 4082 | return 0; |
---|
| 4083 | } |
---|
| 4084 | // Using \CC17 module proposal |
---|
| 4085 | |
---|
| 4086 | module example.M; |
---|
| 4087 | |
---|
| 4088 | export { |
---|
| 4089 | int inc(int val); |
---|
| 4090 | } |
---|
| 4091 | |
---|
| 4092 | int inc(inv val) { |
---|
| 4093 | return val + 1; |
---|
| 4094 | } |
---|
| 4095 | -------------------------------------- |
---|
| 4096 | // Use the module in another file |
---|
| 4097 | import example.M; |
---|
| 4098 | int main() { |
---|
| 4099 | cout << inc(100) << endl; |
---|
| 4100 | return 0; |
---|
| 4101 | } |
---|
| 4102 | |
---|
| 4103 | Go |
---|
| 4104 | Rust |
---|
| 4105 | package example/M; |
---|
| 4106 | |
---|
| 4107 | func Inc(val int32) int32 { |
---|
| 4108 | // Capitalization indicates exported |
---|
| 4109 | return val + 100 |
---|
| 4110 | } |
---|
| 4111 | |
---|
| 4112 | |
---|
| 4113 | -------------------------------------- |
---|
| 4114 | //Use the package in another file |
---|
| 4115 | package main |
---|
| 4116 | import .fmt. |
---|
| 4117 | import "example/M" |
---|
| 4118 | |
---|
| 4119 | func main() int32 { |
---|
| 4120 | fmt.Printf(.%v., M.Inc(100)) |
---|
| 4121 | } |
---|
| 4122 | pub mod example { |
---|
| 4123 | pub mod M { |
---|
| 4124 | pub inc(val i32) -> i32 { |
---|
| 4125 | return val + 100; |
---|
| 4126 | } |
---|
| 4127 | } |
---|
| 4128 | } |
---|
| 4129 | |
---|
| 4130 | -------------------------------------- |
---|
| 4131 | //Use the module in another file |
---|
| 4132 | use example::M; |
---|
| 4133 | |
---|
| 4134 | |
---|
| 4135 | |
---|
| 4136 | fn main() { |
---|
| 4137 | println!(.{}., M::inc(100)); |
---|
| 4138 | } |
---|
| 4139 | \end{lstlisting} |
---|
[53ba273] | 4140 | \end{comment} |
---|
| 4141 | |
---|
[6c91065] | 4142 | |
---|
| 4143 | \subsubsection{Parallel Tasks} |
---|
| 4144 | |
---|
| 4145 | \begin{flushleft} |
---|
| 4146 | \begin{tabular}{@{}l|l|l|l@{}} |
---|
| 4147 | \multicolumn{1}{c|}{\textbf{\CFA}} & \multicolumn{1}{c|}{\textbf{\CC}} & \multicolumn{1}{c|}{\textbf{Go}} & \multicolumn{1}{c}{\textbf{Rust}} \\ |
---|
| 4148 | \hline |
---|
| 4149 | \begin{lstlisting} |
---|
| 4150 | task Nonzero { |
---|
| 4151 | int *data; |
---|
| 4152 | int start; |
---|
| 4153 | int end; |
---|
| 4154 | int* res; |
---|
| 4155 | }; |
---|
| 4156 | |
---|
| 4157 | void ?{}(Nonzero &a, int d[], int s, |
---|
| 4158 | int e, int* subres) { |
---|
| 4159 | // constructor |
---|
| 4160 | a.data = d; |
---|
| 4161 | a.start = s; |
---|
| 4162 | a.end = e; |
---|
| 4163 | a.res = subres; |
---|
| 4164 | } |
---|
| 4165 | |
---|
| 4166 | // implicitly spawn thread here |
---|
| 4167 | void ?()(NonzeroCounter &a) { |
---|
| 4168 | int i; |
---|
| 4169 | int nonzero = 0; |
---|
| 4170 | for (i=start; c<end; ++i) { |
---|
| 4171 | if(a.data[i]!=0){ nonzero++;} |
---|
| 4172 | } |
---|
| 4173 | *a.res = nonzero; |
---|
| 4174 | } |
---|
| 4175 | |
---|
| 4176 | int main() { |
---|
| 4177 | int sz = ... |
---|
| 4178 | int data[sz] = ...; |
---|
| 4179 | int r1 = 0, r2=0; |
---|
| 4180 | int res; |
---|
| 4181 | { // create a scope for Nonzero |
---|
| 4182 | Nonzero n1{data, 0, sz/2, &n1}; |
---|
| 4183 | Nonzero n2{data, sz/2, sz, &n2}; |
---|
| 4184 | n1();//spawn |
---|
| 4185 | n2();//spawn |
---|
| 4186 | } |
---|
| 4187 | res = r1+r2; |
---|
| 4188 | return res; |
---|
| 4189 | } |
---|
| 4190 | \end{lstlisting} |
---|
| 4191 | & |
---|
| 4192 | \begin{lstlisting} |
---|
| 4193 | #include <thread> |
---|
| 4194 | #include <mutex> |
---|
| 4195 | |
---|
| 4196 | std::mutex m; |
---|
| 4197 | |
---|
| 4198 | |
---|
| 4199 | |
---|
| 4200 | |
---|
| 4201 | |
---|
| 4202 | |
---|
| 4203 | |
---|
| 4204 | |
---|
| 4205 | |
---|
| 4206 | |
---|
| 4207 | |
---|
| 4208 | |
---|
| 4209 | void task(const vector<int>&v, |
---|
| 4210 | int* res, size_t s, |
---|
| 4211 | size_t e) { |
---|
| 4212 | int non_zero = 0; |
---|
| 4213 | for(size_t i = s; i < e; ++i){ |
---|
| 4214 | if(v[i]!=0) { non_zero++;} |
---|
| 4215 | } |
---|
| 4216 | std::unique_lock<mutex> lck {m}; |
---|
| 4217 | *res += non_zero; |
---|
| 4218 | } |
---|
| 4219 | |
---|
| 4220 | int main() { |
---|
| 4221 | vector<int> data = ...; //data |
---|
| 4222 | int res = 0; |
---|
| 4223 | std::thread t1 {task, ref(data), |
---|
| 4224 | &res, 0, |
---|
| 4225 | data.size()/2}; |
---|
| 4226 | std::thread t2 {task, ref(data), |
---|
| 4227 | &res, data.size()/2, |
---|
| 4228 | data.size()}; |
---|
| 4229 | t1.join(); |
---|
| 4230 | t2.join(); |
---|
| 4231 | return res; |
---|
| 4232 | } |
---|
| 4233 | \end{lstlisting} |
---|
| 4234 | & |
---|
| 4235 | \begin{lstlisting} |
---|
| 4236 | package main |
---|
| 4237 | |
---|
| 4238 | import "fmt" |
---|
| 4239 | |
---|
| 4240 | func nonzero(data []int, c chan int) { |
---|
| 4241 | nz := 0 |
---|
| 4242 | for _, v:=range data { |
---|
| 4243 | if(v!=0) { nz := nz+1 } |
---|
| 4244 | } |
---|
| 4245 | c <- nz |
---|
| 4246 | } |
---|
| 4247 | |
---|
| 4248 | func main() { |
---|
| 4249 | sz := ... |
---|
| 4250 | data := make([]int, sz) |
---|
| 4251 | ... // data init |
---|
| 4252 | go nonzero(data[:len(data)/2], c) |
---|
| 4253 | go nonzero(data[len(data)/2:], c) |
---|
| 4254 | n1, n2 := <-c, <-c |
---|
| 4255 | res := n1 + n2 |
---|
| 4256 | fmt.Println(res) |
---|
| 4257 | } |
---|
| 4258 | \end{lstlisting} |
---|
| 4259 | & |
---|
| 4260 | \begin{lstlisting} |
---|
| 4261 | use std::thread; |
---|
| 4262 | use std::sync:mpsc::channel; |
---|
| 4263 | |
---|
| 4264 | fn main() { |
---|
| 4265 | let sz = ...; |
---|
| 4266 | let mut data:Vec<i32> = |
---|
| 4267 | Vec::with_capacity(sz as usize); |
---|
| 4268 | ... //init data |
---|
| 4269 | let (tx, rx) = channel(); |
---|
| 4270 | for i in 0..1 { |
---|
| 4271 | let tx = tx.clone(); |
---|
| 4272 | let data = data.clone() |
---|
| 4273 | thread::spawn(move|| { |
---|
| 4274 | let mut nz := 0; |
---|
| 4275 | let mut s = 0; |
---|
| 4276 | let mut e = sz / 2; |
---|
| 4277 | if i == 1 { |
---|
| 4278 | s = sz/2; |
---|
| 4279 | e = data.len(); |
---|
| 4280 | } |
---|
| 4281 | for i in s..(e - 1) { |
---|
| 4282 | if data[i] != 0 ( |
---|
| 4283 | nz = nz + 1 |
---|
| 4284 | } |
---|
| 4285 | } |
---|
| 4286 | tx.send(nz).unwrap(); |
---|
| 4287 | }); |
---|
| 4288 | } |
---|
| 4289 | let res = rx.recv().unwrap() + |
---|
| 4290 | rx.recv().unwrap(); |
---|
| 4291 | println!(.{}., res); |
---|
| 4292 | } |
---|
| 4293 | \end{lstlisting} |
---|
| 4294 | \end{tabular} |
---|
| 4295 | \end{flushleft} |
---|
| 4296 | |
---|
[07bc165] | 4297 | }% local change to lstlising to reduce font size |
---|
[53ba273] | 4298 | |
---|
| 4299 | |
---|
[6c91065] | 4300 | \subsection{Summary of Language Comparison} |
---|
| 4301 | |
---|
| 4302 | |
---|
[b72bad4f] | 4303 | \subsubsection[C++]{\CC} |
---|
[6c91065] | 4304 | |
---|
[07bc165] | 4305 | \Index*[C++]{\CC} is a general-purpose programming language. |
---|
[6c91065] | 4306 | It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation. (Wikipedia) |
---|
| 4307 | |
---|
| 4308 | The primary focus of \CC seems to be adding object-oriented programming to C, and this is the primary difference between \CC and Do. |
---|
| 4309 | \CC uses classes to encapsulate data and the functions that operate on that data, and to hide the internal representation of the data. |
---|
| 4310 | \CFA uses modules instead to perform these same tasks. |
---|
| 4311 | Classes in \CC also enable inheritance among types. |
---|
| 4312 | Instead of inheritance, \CFA embraces composition and interfaces to achieve the same goals with more flexibility. |
---|
[07bc165] | 4313 | There are many studies and articles comparing inheritance and composition (or is-a versus has-a relationships), so we will not go into more detail here (Venners, 1998) (Pike, \Index*{Go} at Google: Language Design in the Service of Software Engineering , 2012). |
---|
[6c91065] | 4314 | |
---|
| 4315 | Overloading in \CFA is very similar to overloading in \CC, with the exception of the additional use, in \CFA, of the return type to differentiate between overloaded functions. |
---|
| 4316 | References and exceptions in \CFA are heavily based on the same features from \CC. |
---|
| 4317 | The mechanism for interoperating with C code in \CFA is also borrowed from \CC. |
---|
| 4318 | |
---|
| 4319 | Both \CFA and \CC provide generics, and the syntax is quite similar. |
---|
| 4320 | The key difference between the two, is that in \CC templates are expanded at compile time for each type for which the template is instantiated, while in \CFA, function pointers are used to make the generic fully compilable. |
---|
| 4321 | This means that a generic function can be defined in a compiled library, and still be used as expected from source. |
---|
| 4322 | |
---|
| 4323 | |
---|
| 4324 | \subsubsection{Go} |
---|
| 4325 | |
---|
[07bc165] | 4326 | \Index*{Go}, also commonly referred to as golang, is a programming language developed at Google in 2007 [.]. |
---|
[6c91065] | 4327 | It is a statically typed language with syntax loosely derived from that of C, adding garbage collection, type |
---|
| 4328 | safety, some structural typing capabilities, additional built-in types such as variable-length arrays and key-value maps, and a large standard library. (Wikipedia) |
---|
| 4329 | |
---|
| 4330 | Go and \CFA differ significantly in syntax and implementation, but the underlying core concepts of the two languages are aligned. |
---|
| 4331 | Both Go and \CFA use composition and interfaces as opposed to inheritance to enable encapsulation and abstraction. |
---|
| 4332 | Both languages (along with their tooling ecosystem) provide a simple packaging mechanism for building units of code for easy sharing and reuse. |
---|
| 4333 | Both languages also include built-in light weight, user level threading concurrency features that attempt to simplify the effort and thought process required for writing parallel programs while maintaining high performance. |
---|
| 4334 | |
---|
| 4335 | Go has a significant runtime which handles the scheduling of its light weight threads, and performs garbage collection, among other tasks. |
---|
| 4336 | \CFA uses a cooperative scheduling algorithm for its tasks, and uses automatic reference counting to enable advanced memory management without garbage collection. |
---|
| 4337 | This results in Go requiring significant overhead to interface with C libraries while \CFA has no overhead. |
---|
| 4338 | |
---|
| 4339 | |
---|
| 4340 | \subsubsection{Rust} |
---|
| 4341 | |
---|
[07bc165] | 4342 | \Index*{Rust} is a general-purpose, multi-paradigm, compiled programming language developed by Mozilla Research. |
---|
[6c91065] | 4343 | It is designed to be a "safe, concurrent, practical language", supporting pure-functional, concurrent-actor[dubious . discuss][citation needed], imperative-procedural, and object-oriented styles. |
---|
| 4344 | |
---|
| 4345 | The primary focus of Rust is in safety, especially in concurrent programs. |
---|
| 4346 | To enforce a high level of safety, Rust has added ownership as a core feature of the language to guarantee memory safety. |
---|
| 4347 | This safety comes at the cost of a difficult learning curve, a change in the thought model of the program, and often some runtime overhead. |
---|
| 4348 | |
---|
| 4349 | Aside from those key differences, Rust and \CFA also have several similarities. |
---|
| 4350 | Both languages support no overhead interoperability with C and have minimal runtimes. |
---|
| 4351 | Both languages support inheritance and polymorphism through the use of interfaces (traits). |
---|
| 4352 | |
---|
| 4353 | |
---|
| 4354 | \subsubsection{D} |
---|
| 4355 | |
---|
[07bc165] | 4356 | The \Index*{D} programming language is an object-oriented, imperative, multi-paradigm system programming |
---|
[6c91065] | 4357 | language created by Walter Bright of Digital Mars and released in 2001. [.] |
---|
[07bc165] | 4358 | Though it originated as a re-engineering of \CC, D is a distinct language, having redesigned some core \CC features while also taking inspiration from other languages, notably \Index*{Java}, \Index*{Python}, Ruby, C\#, and Eiffel. |
---|
[6c91065] | 4359 | |
---|
| 4360 | D and \CFA both start with C and add productivity features. |
---|
| 4361 | The obvious difference is that D uses classes and inheritance while \CFA uses composition and interfaces. |
---|
| 4362 | D is closer to \CFA than \CC since it is limited to single inheritance and also supports interfaces. |
---|
| 4363 | Like \CC, and unlike \CFA, D uses garbage collection and has compile-time expanded templates. |
---|
| 4364 | D does not have any built-in concurrency constructs in the |
---|
| 4365 | language, though it does have a standard library for concurrency which includes the low-level primitives for concurrency. |
---|
| 4366 | |
---|
| 4367 | |
---|
[53ba273] | 4368 | \appendix |
---|
| 4369 | |
---|
| 4370 | |
---|
[e80ebe5] | 4371 | \section{New Keywords} |
---|
| 4372 | \label{s:NewKeywords} |
---|
| 4373 | |
---|
| 4374 | \begin{quote2} |
---|
| 4375 | \begin{tabular}{ll} |
---|
| 4376 | ©catch© & ©lvalue© \\ |
---|
| 4377 | ©catchResume© & \\ |
---|
| 4378 | ©choose© & ©otype© \\ |
---|
| 4379 | & \\ |
---|
| 4380 | ©disable© & ©throw© \\ |
---|
| 4381 | ©dtype© & ©throwResume© \\ |
---|
| 4382 | & ©trait© \\ |
---|
| 4383 | ©enable© & ©try© \\ |
---|
| 4384 | & \\ |
---|
| 4385 | ©fallthrough© \\ |
---|
| 4386 | ©fallthru© \\ |
---|
| 4387 | ©finally© \\ |
---|
| 4388 | ©forall© \\ |
---|
| 4389 | ©ftype© \\ |
---|
| 4390 | \end{tabular} |
---|
| 4391 | \end{quote2} |
---|
| 4392 | |
---|
[08061589] | 4393 | |
---|
[e80ebe5] | 4394 | \section{Standard Headers} |
---|
| 4395 | \label{s:StandardHeaders} |
---|
| 4396 | |
---|
| 4397 | C prescribes the following standard header-files: |
---|
| 4398 | \begin{quote2} |
---|
| 4399 | \begin{minipage}{\linewidth} |
---|
| 4400 | \begin{tabular}{lll} |
---|
| 4401 | assert.h & math.h & stdlib.h \\ |
---|
| 4402 | complex.h & setjmp.h & stdnoreturn.h \\ |
---|
| 4403 | ctype.h & signal.h & string.h \\ |
---|
| 4404 | errno.h & stdalign.h & tgmath.h \\ |
---|
| 4405 | fenv.h & stdarg.h & threads.h \\ |
---|
| 4406 | float.h & stdatomic.h & time.h \\ |
---|
| 4407 | inttypes.h & stdbool.h & uchar.h \\ |
---|
| 4408 | iso646.h & stddef.h & wchar.h \\ |
---|
| 4409 | limits.h & stdint.h & wctype.h \\ |
---|
| 4410 | locale.h & stdio.h & unistd.h\footnote{\CFA extension} |
---|
| 4411 | \end{tabular} |
---|
| 4412 | \end{minipage} |
---|
| 4413 | \end{quote2} |
---|
| 4414 | For the prescribed head-files, \CFA implicit wraps their includes in an ©extern "C"©; |
---|
| 4415 | hence, names in these include files are not mangled\index{mangling!name} (see~\VRef{s:Interoperability}). |
---|
| 4416 | All other C header files must be explicitly wrapped in ©extern "C"© to prevent name mangling. |
---|
[08061589] | 4417 | |
---|
| 4418 | |
---|
[b52d900] | 4419 | \section{Incompatible} |
---|
| 4420 | |
---|
[4096de0] | 4421 | The following incompatibles exist between \CFA and C, and are similar to Annex C for \CC~\cite{ANSI14:C++}. |
---|
[b52d900] | 4422 | |
---|
| 4423 | \begin{enumerate} |
---|
| 4424 | \item |
---|
[4096de0] | 4425 | \begin{description} |
---|
[e80ebe5] | 4426 | \item[Change:] add new keywords (see~\VRef{s:NewKeywords}) \\ |
---|
[4096de0] | 4427 | New keywords are added to \CFA. |
---|
| 4428 | \item[Rationale:] keywords added to implement new semantics of \CFA. |
---|
| 4429 | \item[Effect on original feature:] change to semantics of well-defined feature. \\ |
---|
| 4430 | Any ISO C programs using these keywords as identifiers are invalid \CFA programs. |
---|
| 4431 | \item[Difficulty of converting:] keyword clashes are accommodated by syntactic transformations using the \CFA backquote escape-mechanism: |
---|
[b52d900] | 4432 | \begin{lstlisting} |
---|
[5479e63] | 4433 | int `otype` = 3; // make keyword an identifier |
---|
[4096de0] | 4434 | double `choose` = 3.5; |
---|
[b52d900] | 4435 | \end{lstlisting} |
---|
[4096de0] | 4436 | Programs can be converted automatically by enclosing keyword identifiers in backquotes, and the backquotes can be remove later when the identifier name is changed. |
---|
| 4437 | Clashes in C system libraries (include files) can be handled automatically using preprocessor, ©#include_next© and ©-Ifilename©: |
---|
[b52d900] | 4438 | \begin{lstlisting} |
---|
[4096de0] | 4439 | // include file uses the CFA keyword "otype". |
---|
| 4440 | #if ! defined( otype ) // nesting ? |
---|
| 4441 | #define otype `otype` |
---|
| 4442 | #define __CFA_BFD_H__ |
---|
| 4443 | #endif // ! otype |
---|
| 4444 | |
---|
| 4445 | #include_next <bfd.h> // must have internal check for multiple expansion |
---|
| 4446 | |
---|
| 4447 | #if defined( otype ) && defined( __CFA_BFD_H__ ) // reset only if set |
---|
| 4448 | #undef otype |
---|
| 4449 | #undef __CFA_BFD_H__ |
---|
| 4450 | #endif // otype && __CFA_BFD_H__ |
---|
[b52d900] | 4451 | \end{lstlisting} |
---|
[4096de0] | 4452 | \item[How widely used:] clashes among new \CFA keywords and existing identifiers are rare. |
---|
| 4453 | \end{description} |
---|
[b52d900] | 4454 | |
---|
| 4455 | \item |
---|
[4096de0] | 4456 | \begin{description} |
---|
| 4457 | \item[Change:] type of character literal ©int© to ©char© to allow more intuitive overloading: |
---|
[b52d900] | 4458 | \begin{lstlisting} |
---|
[4096de0] | 4459 | int rtn( int i ); |
---|
| 4460 | int rtn( char c ); |
---|
| 4461 | rtn( 'x' ); // programmer expects 2nd rtn to be called |
---|
[b52d900] | 4462 | \end{lstlisting} |
---|
[4096de0] | 4463 | \item[Rationale:] it is more intuitive for the call to ©rtn© to match the second version of definition of ©rtn© rather than the first. |
---|
| 4464 | In particular, output of ©char© variable now print a character rather than the decimal ASCII value of the character. |
---|
| 4465 | \begin{lstlisting} |
---|
| 4466 | sout | 'x' | " " | (int)'x' | endl; |
---|
| 4467 | x 120 |
---|
| 4468 | \end{lstlisting} |
---|
| 4469 | Having to cast ©'x'© to ©char© is non-intuitive. |
---|
| 4470 | \item[Effect on original feature:] change to semantics of well-defined feature that depend on: |
---|
| 4471 | \begin{lstlisting} |
---|
| 4472 | sizeof( 'x' ) == sizeof( int ) |
---|
| 4473 | \end{lstlisting} |
---|
| 4474 | no long work the same in \CFA programs. |
---|
| 4475 | \item[Difficulty of converting:] simple |
---|
| 4476 | \item[How widely used:] programs that depend upon ©sizeof( 'x' )© are rare and can be changed to ©sizeof(char)©. |
---|
| 4477 | \end{description} |
---|
[b52d900] | 4478 | |
---|
| 4479 | \item |
---|
[4096de0] | 4480 | \begin{description} |
---|
| 4481 | \item[Change:] make string literals ©const©: |
---|
[b52d900] | 4482 | \begin{lstlisting} |
---|
[4096de0] | 4483 | char * p = "abc"; // valid in C, deprecated in §\CFA§ |
---|
| 4484 | char * q = expr ? "abc" : "de"; // valid in C, invalid in §\CFA§ |
---|
[b52d900] | 4485 | \end{lstlisting} |
---|
[4096de0] | 4486 | The type of a string literal is changed from ©[] char© to ©const [] char©. |
---|
| 4487 | Similarly, the type of a wide string literal is changed from ©[] wchar_t© to ©const [] wchar_t©. |
---|
| 4488 | \item[Rationale:] This change is a safety issue: |
---|
[b52d900] | 4489 | \begin{lstlisting} |
---|
[4096de0] | 4490 | char * p = "abc"; |
---|
| 4491 | p[0] = 'w'; // segment fault or change constant literal |
---|
[b52d900] | 4492 | \end{lstlisting} |
---|
[4096de0] | 4493 | The same problem occurs when passing a string literal to a routine that changes its argument. |
---|
| 4494 | \item[Effect on original feature:] change to semantics of well-defined feature. |
---|
| 4495 | \item[Difficulty of converting:] simple syntactic transformation, because string literals can be converted to ©char *©. |
---|
| 4496 | \item[How widely used:] programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are rare. |
---|
| 4497 | \end{description} |
---|
[b52d900] | 4498 | |
---|
| 4499 | \item |
---|
[4096de0] | 4500 | \begin{description} |
---|
| 4501 | \item[Change:] remove \newterm{tentative definitions}, which only occurs at file scope: |
---|
| 4502 | \begin{lstlisting} |
---|
| 4503 | int i; // forward definition |
---|
| 4504 | int *j = ®&i®; // forward reference, valid in C, invalid in §\CFA§ |
---|
| 4505 | int i = 0; // definition |
---|
| 4506 | \end{lstlisting} |
---|
| 4507 | is valid in C, and invalid in \CFA because duplicate overloaded object definitions at the same scope level are disallowed. |
---|
| 4508 | This change makes it impossible to define mutually referential file-local static objects, if initializers are restricted to the syntactic forms of C. For example, |
---|
| 4509 | \begin{lstlisting} |
---|
| 4510 | struct X { int i; struct X *next; }; |
---|
| 4511 | static struct X a; // forward definition |
---|
| 4512 | static struct X b = { 0, ®&a® }; // forward reference, valid in C, invalid in §\CFA§ |
---|
| 4513 | static struct X a = { 1, &b }; // definition |
---|
| 4514 | \end{lstlisting} |
---|
| 4515 | \item[Rationale:] avoids having different initialization rules for builtin types and userdefined types. |
---|
| 4516 | \item[Effect on original feature:] change to semantics of well-defined feature. |
---|
| 4517 | \item[Difficulty of converting:] the initializer for one of a set of mutually-referential file-local static objects must invoke a routine call to achieve the initialization. |
---|
| 4518 | \item[How widely used:] seldom |
---|
| 4519 | \end{description} |
---|
[b52d900] | 4520 | |
---|
| 4521 | \item |
---|
[4096de0] | 4522 | \begin{description} |
---|
| 4523 | \item[Change:] have ©struct© introduce a scope for nested types |
---|
[e80ebe5] | 4524 | In C, the name of the nested types belongs to the same scope as the name of the outermost enclosing |
---|
[b52d900] | 4525 | Example: |
---|
| 4526 | \begin{lstlisting} |
---|
[4096de0] | 4527 | enum ®Colour® { R, G, B, Y, C, M }; |
---|
| 4528 | struct Person { |
---|
| 4529 | enum ®Colour® { R, G, B }; // nested type |
---|
| 4530 | struct Face { // nested type |
---|
| 4531 | ®Colour® Eyes, Hair; // type defined outside (1 level) |
---|
| 4532 | }; |
---|
| 4533 | ß.ß®Colour® shirt; // type defined outside (top level) |
---|
| 4534 | ®Colour® pants; // type defined same level |
---|
| 4535 | Face looks[10]; // type defined same level |
---|
[b52d900] | 4536 | }; |
---|
[4096de0] | 4537 | ®Colour® c = R; // type/enum defined same level |
---|
| 4538 | Personß.ß®Colour® pc = Personß.ßR; // type/enum defined inside |
---|
| 4539 | Personß.ßFace pretty; // type defined inside |
---|
[b52d900] | 4540 | \end{lstlisting} |
---|
[4096de0] | 4541 | \item[Rationale:] ©struct© scope is crucial to \CFA as an information structuring and hiding mechanism. |
---|
| 4542 | \item[Effect on original feature:] change to semantics of well-defined feature. |
---|
| 4543 | \item[Difficulty of converting:] Semantic transformation. |
---|
| 4544 | \item[How widely used:] C programs rarely have nest types because they are equivalent to the hoisted version. |
---|
| 4545 | |
---|
[07bc165] | 4546 | \CFA is C \emph{incompatible} on this issue, and provides semantics similar to \Index*[C++]{\CC}. |
---|
[4096de0] | 4547 | Nested types are not hoisted and can be referenced using the field selection operator ``©.©'', unlike the \CC scope-resolution operator ``©::©''. |
---|
[c45170a] | 4548 | Given that nested types in C are equivalent to not using them, \ie they are essentially useless, it is unlikely there are any realistic usages that break because of this incompatibility. |
---|
[4096de0] | 4549 | \end{description} |
---|
| 4550 | |
---|
| 4551 | \item |
---|
| 4552 | \begin{description} |
---|
| 4553 | \item[Change:] In C++, the name of a nested class is local to its enclosing class. |
---|
| 4554 | \item[Rationale:] C++ classes have member functions which require that classes establish scopes. |
---|
| 4555 | \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: |
---|
[b52d900] | 4556 | \begin{lstlisting} |
---|
| 4557 | struct Y; // struct Y and struct X are at the same scope |
---|
| 4558 | struct X { |
---|
| 4559 | struct Y { /* ... */ } y; |
---|
| 4560 | }; |
---|
| 4561 | \end{lstlisting} |
---|
[e945826] | 4562 | All the definitions of C struct types enclosed in other struct definitions and accessed outside the scope of the enclosing struct could be exported to the scope of the enclosing struct. |
---|
| 4563 | Note: this is a consequence of the difference in scope rules, which is documented in 3.3. |
---|
[4096de0] | 4564 | \item[How widely used:] Seldom. |
---|
| 4565 | \end{description} |
---|
| 4566 | |
---|
| 4567 | \item |
---|
| 4568 | \begin{description} |
---|
| 4569 | \item[Change:] comma expression is disallowed as subscript |
---|
[5479e63] | 4570 | \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. |
---|
[4096de0] | 4571 | \item[Effect on original feature:] change to semantics of well-defined feature. |
---|
| 4572 | \item[Difficulty of converting:] semantic transformation of ©x[i,j]© to ©x[(i,j)]© |
---|
| 4573 | \item[How widely used:] seldom. |
---|
| 4574 | \end{description} |
---|
[b52d900] | 4575 | \end{enumerate} |
---|
| 4576 | |
---|
| 4577 | |
---|
[53ba273] | 4578 | \section{I/O Library} |
---|
| 4579 | \label{s:IOLibrary} |
---|
| 4580 | \index{input/output library} |
---|
| 4581 | |
---|
[5479e63] | 4582 | The goal for the \CFA I/O is to make I/O as simple as possible in the common cases, while fully supporting polymorphism and user defined types in a consistent way. |
---|
[07bc165] | 4583 | The common case is printing out a sequence of variables separated by whitespace. |
---|
[53ba273] | 4584 | \begin{quote2} |
---|
[45576af9] | 4585 | \begin{tabular}{@{}l@{\hspace{3em}}l@{}} |
---|
| 4586 | \multicolumn{1}{c@{\hspace{3em}}}{\textbf{\CFA}} & \multicolumn{1}{c}{\textbf{\CC}} \\ |
---|
[53ba273] | 4587 | \begin{lstlisting} |
---|
| 4588 | int x = 0, y = 1, z = 2; |
---|
[e945826] | 4589 | ®sout® ®|® x ®|® y ®|® z ®| endl®; |
---|
[53ba273] | 4590 | \end{lstlisting} |
---|
| 4591 | & |
---|
| 4592 | \begin{lstlisting} |
---|
| 4593 | |
---|
| 4594 | cout << x << " " << y << " " << z << endl; |
---|
| 4595 | \end{lstlisting} |
---|
| 4596 | \end{tabular} |
---|
| 4597 | \end{quote2} |
---|
[07bc165] | 4598 | The \CFA form has half as many characters as the \CC form, and is similar to \Index*{Python} I/O with respect to implicit separators. |
---|
[53ba273] | 4599 | |
---|
| 4600 | The logical-or operator is used because it is the lowest-priority overloadable operator, other than assignment. |
---|
| 4601 | Therefore, fewer output expressions require parenthesis. |
---|
| 4602 | \begin{quote2} |
---|
| 4603 | \begin{tabular}{@{}ll@{}} |
---|
| 4604 | \textbf{\CFA:} |
---|
| 4605 | & |
---|
| 4606 | \begin{lstlisting} |
---|
| 4607 | sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl; |
---|
| 4608 | \end{lstlisting} |
---|
| 4609 | \\ |
---|
| 4610 | \textbf{\CC:} |
---|
| 4611 | & |
---|
| 4612 | \begin{lstlisting} |
---|
| 4613 | cout << x * 3 << y + 1 << (z << 2) << (x == y) << (x | y) << (x || y) << (x > z ? 1 : 2) << endl; |
---|
| 4614 | \end{lstlisting} |
---|
| 4615 | \end{tabular} |
---|
| 4616 | \end{quote2} |
---|
| 4617 | Finally, the logical-or operator has a link with the Shell pipe-operator for moving data, although data flows in the opposite direction. |
---|
| 4618 | |
---|
[5479e63] | 4619 | The implicit separator\index{I/O separator} character (space/blank) is a separator not a terminator. |
---|
[53ba273] | 4620 | The rules for implicitly adding the separator are: |
---|
| 4621 | \begin{enumerate} |
---|
| 4622 | \item |
---|
[5479e63] | 4623 | A separator does not appear at the start or end of a line. |
---|
[53ba273] | 4624 | \begin{lstlisting}[belowskip=0pt] |
---|
[b72bad4f] | 4625 | sout | 1 | 2 | 3 | endl; |
---|
[53ba273] | 4626 | \end{lstlisting} |
---|
| 4627 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
| 4628 | 1 2 3 |
---|
| 4629 | \end{lstlisting} |
---|
| 4630 | \item |
---|
[5479e63] | 4631 | A separator does not appear before or after a character literal or variable. |
---|
[53ba273] | 4632 | \begin{lstlisting} |
---|
| 4633 | sout | '1' | '2' | '3' | endl; |
---|
| 4634 | 123 |
---|
| 4635 | \end{lstlisting} |
---|
| 4636 | \item |
---|
[5479e63] | 4637 | A separator does not appear before or after a null (empty) C string |
---|
[53ba273] | 4638 | \begin{lstlisting} |
---|
| 4639 | sout | 1 | "" | 2 | "" | 3 | endl; |
---|
| 4640 | 123 |
---|
| 4641 | \end{lstlisting} |
---|
| 4642 | which is a local mechanism to disable insertion of the separator character. |
---|
| 4643 | \item |
---|
[5479e63] | 4644 | A separator does not appear before a C string starting with the (extended) \Index{ASCII}\index{ASCII!extended} characters: \lstinline[mathescape=off]@([{$£¥¡¿«@ |
---|
[53ba273] | 4645 | %$ |
---|
| 4646 | \begin{lstlisting}[mathescape=off] |
---|
[e80ebe5] | 4647 | sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x $" | 4 | "x £" | 5 | "x ¥" | 6 | "x ¡" | 7 |
---|
| 4648 | | "x ¿" | 8 | "x «" | 9 | endl; |
---|
[53ba273] | 4649 | \end{lstlisting} |
---|
| 4650 | %$ |
---|
| 4651 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4652 | x (1 x [2 x {3 x $4 x £5 x ¥6 x ¡7 x ¿8 x «9 |
---|
[53ba273] | 4653 | \end{lstlisting} |
---|
| 4654 | %$ |
---|
| 4655 | \item |
---|
[0638c44] | 4656 | A seperator does not appear after a C string ending with the (extended) \Index{ASCII}\index{ASCII!extended} characters: ©,.:;!?)]}%¢»© |
---|
[53ba273] | 4657 | \begin{lstlisting}[belowskip=0pt] |
---|
[e80ebe5] | 4658 | sout | 1 | ", x" | 2 | ". x" | 3 | ": x" | 4 | "; x" | 5 | "! x" | 6 | "? x" | 7 |
---|
| 4659 | | ") x" | 8 | "] x" | 9 | "} x" | 10 | "% x" | 11 | "¢ x" | 12 | "» x" | endl; |
---|
[53ba273] | 4660 | \end{lstlisting} |
---|
| 4661 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
| 4662 | 1, x 2. x 3: x 4; x 5! x 6? x 7) x 8] x 9} x 10% x 11¢ 12» |
---|
| 4663 | \end{lstlisting} |
---|
| 4664 | \item |
---|
[e945826] | 4665 | A seperator does not appear before or after a C string begining/ending with the \Index{ASCII} quote or whitespace characters: \lstinline[showspaces=true]@`'" \t\v\f\r\n@ |
---|
[53ba273] | 4666 | \begin{lstlisting}[belowskip=0pt] |
---|
[e945826] | 4667 | sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x" | "x " | 4 | " x" | "x\t" | 1 | "\tx" | endl; |
---|
[53ba273] | 4668 | \end{lstlisting} |
---|
[e945826] | 4669 | \begin{lstlisting}[mathescape=off,showspaces=true,showtabs=true,aboveskip=0pt,belowskip=0pt] |
---|
| 4670 | x`1`x'2'x"3"x x 4 x x 1 x |
---|
[53ba273] | 4671 | \end{lstlisting} |
---|
| 4672 | \end{enumerate} |
---|
| 4673 | The following \CC-style \Index{manipulator}s allow further control over implicit seperation. |
---|
| 4674 | \begin{lstlisting}[mathescape=off,belowskip=0pt] |
---|
[e80ebe5] | 4675 | sout | sepOn | 1 | 2 | 3 | sepOn | endl; §\C{// separator at start of line}§ |
---|
[53ba273] | 4676 | \end{lstlisting} |
---|
| 4677 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
| 4678 | 1 2 3 |
---|
| 4679 | \end{lstlisting} |
---|
| 4680 | \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] |
---|
[e80ebe5] | 4681 | sout | 1 | sepOff | 2 | 3 | endl; §\C{// turn off implicit separator temporarily}§ |
---|
[53ba273] | 4682 | \end{lstlisting} |
---|
| 4683 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
| 4684 | 12 3 |
---|
| 4685 | \end{lstlisting} |
---|
| 4686 | \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] |
---|
[e80ebe5] | 4687 | sout | sepDisable | 1 | 2 | 3 | endl; §\C{// turn off implicit separation, affects all subsequent prints}§ |
---|
[53ba273] | 4688 | \end{lstlisting} |
---|
| 4689 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
| 4690 | 123 |
---|
| 4691 | \end{lstlisting} |
---|
| 4692 | \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] |
---|
[e80ebe5] | 4693 | sout | 1 | sepOn | 2 | 3 | endl; §\C{// turn on implicit separator temporarily}§ |
---|
[53ba273] | 4694 | \end{lstlisting} |
---|
| 4695 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
| 4696 | 1 23 |
---|
| 4697 | \end{lstlisting} |
---|
| 4698 | \begin{lstlisting}[mathescape=off,aboveskip=0pt,belowskip=0pt] |
---|
[e80ebe5] | 4699 | sout | sepEnable | 1 | 2 | 3 | endl; §\C{// turn on implicit separation, affects all subsequent prints}§ |
---|
[53ba273] | 4700 | \end{lstlisting} |
---|
| 4701 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt,belowskip=0pt] |
---|
[b72bad4f] | 4702 | 1 2 3 |
---|
[53ba273] | 4703 | \end{lstlisting} |
---|
| 4704 | \begin{lstlisting}[mathescape=off,aboveskip=0pt,aboveskip=0pt,belowskip=0pt] |
---|
[e80ebe5] | 4705 | sepSet( sout, ", $" ); §\C{// change separator from " " to ", \$"}§ |
---|
[53ba273] | 4706 | sout | 1 | 2 | 3 | endl; |
---|
| 4707 | \end{lstlisting} |
---|
| 4708 | %$ |
---|
| 4709 | \begin{lstlisting}[mathescape=off,showspaces=true,aboveskip=0pt] |
---|
| 4710 | 1, $2, $3 |
---|
| 4711 | \end{lstlisting} |
---|
| 4712 | %$ |
---|
[b72bad4f] | 4713 | \begin{comment} |
---|
[53ba273] | 4714 | #include <fstream> |
---|
| 4715 | |
---|
| 4716 | int main() { |
---|
[b72bad4f] | 4717 | int x = 3, y = 5, z = 7; |
---|
| 4718 | sout | x * 3 | y + 1 | z << 2 | x == y | (x | y) | (x || y) | (x > z ? 1 : 2) | endl; |
---|
| 4719 | sout | 1 | 2 | 3 | endl; |
---|
| 4720 | sout | '1' | '2' | '3' | endl; |
---|
| 4721 | sout | 1 | "" | 2 | "" | 3 | endl; |
---|
[e945826] | 4722 | sout | "x (" | 1 | "x [" | 2 | "x {" | 3 | "x $" | 4 | "x £" | 5 | "x ¥" | 6 | "x ¡" | 7 | "x ¿" | 8 | "x «" | 9 | endl; |
---|
[b72bad4f] | 4723 | sout | 1 | ", x" | 2 | ". x" | 3 | ": x" | 4 | "; x" | 5 | "! x" | 6 | "? x" | 7 | ") x" | 8 | "] x" | 9 | "} x" |
---|
[8bc4ef8] | 4724 | | 10 | "% x" | 11 | "¢ x" | 12 | "» x" | endl; |
---|
[e945826] | 4725 | sout | "x`" | 1 | "`x'" | 2 | "'x\"" | 3 | "\"x" | "x " | 4 | " x" | "x\t" | 1 | "\tx" | endl; |
---|
[b72bad4f] | 4726 | sout | sepOn | 1 | 2 | 3 | sepOn | endl; // separator at start of line |
---|
| 4727 | sout | 1 | sepOff | 2 | 3 | endl; // turn off implicit separator temporarily |
---|
| 4728 | sout | sepDisable | 1 | 2 | 3 | endl; // turn off implicit separation, affects all subsequent prints |
---|
| 4729 | sout | 1 | sepOn | 2 | 3 | endl; // turn on implicit separator temporarily |
---|
| 4730 | sout | sepEnable | 1 | 2 | 3 | endl; // turn on implicit separation, affects all subsequent prints |
---|
| 4731 | sepSet( sout, ", $" ); // change separator from " " to ", $" |
---|
| 4732 | sout | 1 | 2 | 3 | endl; |
---|
| 4733 | |
---|
[53ba273] | 4734 | } |
---|
| 4735 | |
---|
[b72bad4f] | 4736 | // Local Variables: // |
---|
| 4737 | // tab-width: 4 // |
---|
| 4738 | // End: // |
---|
| 4739 | \end{comment} |
---|
| 4740 | %$ |
---|
[53ba273] | 4741 | |
---|
| 4742 | |
---|
| 4743 | \section{Standard Library} |
---|
| 4744 | \label{s:StandardLibrary} |
---|
| 4745 | |
---|
| 4746 | The goal of the \CFA standard-library is to wrap many of the existing C library-routines that are explicitly polymorphic into implicitly polymorphic versions. |
---|
| 4747 | |
---|
| 4748 | |
---|
| 4749 | \subsection{malloc} |
---|
| 4750 | |
---|
[07bc165] | 4751 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4752 | forall( otype T ) T * malloc( void );§\indexc{malloc}§ |
---|
[53ba273] | 4753 | forall( otype T ) T * malloc( char fill ); |
---|
| 4754 | forall( otype T ) T * malloc( T * ptr, size_t size ); |
---|
| 4755 | forall( otype T ) T * malloc( T * ptr, size_t size, unsigned char fill ); |
---|
[e945826] | 4756 | forall( otype T ) T * calloc( size_t nmemb );§\indexc{calloc}§ |
---|
| 4757 | forall( otype T ) T * realloc( T * ptr, size_t size );§\indexc{ato}§ |
---|
[53ba273] | 4758 | forall( otype T ) T * realloc( T * ptr, size_t size, unsigned char fill ); |
---|
| 4759 | |
---|
[e945826] | 4760 | forall( otype T ) T * aligned_alloc( size_t alignment );§\indexc{ato}§ |
---|
[53ba273] | 4761 | forall( otype T ) T * memalign( size_t alignment ); // deprecated |
---|
| 4762 | forall( otype T ) int posix_memalign( T ** ptr, size_t alignment ); |
---|
| 4763 | |
---|
| 4764 | forall( otype T ) T * memset( T * ptr, unsigned char fill ); // use default value '\0' for fill |
---|
| 4765 | forall( otype T ) T * memset( T * ptr ); // remove when default value available |
---|
| 4766 | \end{lstlisting} |
---|
[07bc165] | 4767 | \ |
---|
[53ba273] | 4768 | |
---|
[b72bad4f] | 4769 | \subsection{ato / strto} |
---|
[53ba273] | 4770 | |
---|
[07bc165] | 4771 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4772 | int ato( const char * ptr );§\indexc{ato}§ |
---|
[53ba273] | 4773 | unsigned int ato( const char * ptr ); |
---|
| 4774 | long int ato( const char * ptr ); |
---|
| 4775 | unsigned long int ato( const char * ptr ); |
---|
| 4776 | long long int ato( const char * ptr ); |
---|
| 4777 | unsigned long long int ato( const char * ptr ); |
---|
| 4778 | float ato( const char * ptr ); |
---|
| 4779 | double ato( const char * ptr ); |
---|
| 4780 | long double ato( const char * ptr ); |
---|
| 4781 | float _Complex ato( const char * ptr ); |
---|
| 4782 | double _Complex ato( const char * ptr ); |
---|
| 4783 | long double _Complex ato( const char * ptr ); |
---|
| 4784 | |
---|
| 4785 | int strto( const char * sptr, char ** eptr, int base ); |
---|
| 4786 | unsigned int strto( const char * sptr, char ** eptr, int base ); |
---|
| 4787 | long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 4788 | unsigned long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 4789 | long long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 4790 | unsigned long long int strto( const char * sptr, char ** eptr, int base ); |
---|
| 4791 | float strto( const char * sptr, char ** eptr ); |
---|
| 4792 | double strto( const char * sptr, char ** eptr ); |
---|
| 4793 | long double strto( const char * sptr, char ** eptr ); |
---|
| 4794 | float _Complex strto( const char * sptr, char ** eptr ); |
---|
| 4795 | double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 4796 | long double _Complex strto( const char * sptr, char ** eptr ); |
---|
| 4797 | \end{lstlisting} |
---|
[07bc165] | 4798 | \ |
---|
[53ba273] | 4799 | |
---|
| 4800 | |
---|
[b72bad4f] | 4801 | \subsection{bsearch / qsort} |
---|
[53ba273] | 4802 | |
---|
[07bc165] | 4803 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[53ba273] | 4804 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[e945826] | 4805 | T * bsearch( const T key, const T * arr, size_t dimension );§\indexc{bsearch}§ |
---|
[53ba273] | 4806 | |
---|
| 4807 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[e945826] | 4808 | void qsort( const T * arr, size_t dimension );§\indexc{qsort}§ |
---|
[53ba273] | 4809 | \end{lstlisting} |
---|
[07bc165] | 4810 | \ |
---|
[53ba273] | 4811 | |
---|
| 4812 | |
---|
| 4813 | \subsection{abs} |
---|
| 4814 | |
---|
[07bc165] | 4815 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4816 | char abs( char );§\indexc{abs}§ |
---|
[b72bad4f] | 4817 | int abs( int ); |
---|
[53ba273] | 4818 | long int abs( long int ); |
---|
| 4819 | long long int abs( long long int ); |
---|
| 4820 | float abs( float ); |
---|
| 4821 | double abs( double ); |
---|
| 4822 | long double abs( long double ); |
---|
[b72bad4f] | 4823 | float abs( float _Complex ); |
---|
| 4824 | double abs( double _Complex ); |
---|
| 4825 | long double abs( long double _Complex ); |
---|
[53ba273] | 4826 | \end{lstlisting} |
---|
[07bc165] | 4827 | \ |
---|
[53ba273] | 4828 | |
---|
| 4829 | |
---|
| 4830 | \subsection{random} |
---|
| 4831 | |
---|
[07bc165] | 4832 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4833 | void rand48seed( long int s );§\indexc{rand48seed}§ |
---|
| 4834 | char rand48();§\indexc{rand48}§ |
---|
[53ba273] | 4835 | int rand48(); |
---|
| 4836 | unsigned int rand48(); |
---|
| 4837 | long int rand48(); |
---|
| 4838 | unsigned long int rand48(); |
---|
| 4839 | float rand48(); |
---|
| 4840 | double rand48(); |
---|
| 4841 | float _Complex rand48(); |
---|
| 4842 | double _Complex rand48(); |
---|
| 4843 | long double _Complex rand48(); |
---|
| 4844 | \end{lstlisting} |
---|
[07bc165] | 4845 | \ |
---|
[53ba273] | 4846 | |
---|
| 4847 | |
---|
[5479e63] | 4848 | \subsection{min / max / clamp / swap} |
---|
[53ba273] | 4849 | |
---|
[07bc165] | 4850 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[53ba273] | 4851 | forall( otype T | { int ?<?( T, T ); } ) |
---|
[e945826] | 4852 | T min( const T t1, const T t2 );§\indexc{min}§ |
---|
[53ba273] | 4853 | |
---|
| 4854 | forall( otype T | { int ?>?( T, T ); } ) |
---|
[e945826] | 4855 | T max( const T t1, const T t2 );§\indexc{max}§ |
---|
[53ba273] | 4856 | |
---|
[5479e63] | 4857 | forall( otype T | { T min( T, T ); T max( T, T ); } ) |
---|
| 4858 | T clamp( T value, T min_val, T max_val );§\indexc{clamp}§ |
---|
| 4859 | |
---|
[53ba273] | 4860 | forall( otype T ) |
---|
[e945826] | 4861 | void swap( T * t1, T * t2 );§\indexc{swap}§ |
---|
[53ba273] | 4862 | \end{lstlisting} |
---|
[07bc165] | 4863 | \ |
---|
[53ba273] | 4864 | |
---|
| 4865 | |
---|
[b72bad4f] | 4866 | \section{Math Library} |
---|
| 4867 | \label{s:Math Library} |
---|
| 4868 | |
---|
| 4869 | The goal of the \CFA math-library is to wrap many of the existing C math library-routines that are explicitly polymorphic into implicitly polymorphic versions. |
---|
| 4870 | |
---|
| 4871 | |
---|
| 4872 | \subsection{General} |
---|
| 4873 | |
---|
[07bc165] | 4874 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4875 | float fabs( float );§\indexc{fabs}§ |
---|
[b72bad4f] | 4876 | double fabs( double ); |
---|
| 4877 | long double fabs( long double ); |
---|
| 4878 | float cabs( float _Complex ); |
---|
| 4879 | double cabs( double _Complex ); |
---|
| 4880 | long double cabs( long double _Complex ); |
---|
| 4881 | |
---|
[e945826] | 4882 | float ?%?( float, float );§\indexc{fmod}§ |
---|
[b72bad4f] | 4883 | float fmod( float, float ); |
---|
| 4884 | double ?%?( double, double ); |
---|
| 4885 | double fmod( double, double ); |
---|
| 4886 | long double ?%?( long double, long double ); |
---|
| 4887 | long double fmod( long double, long double ); |
---|
| 4888 | |
---|
[e945826] | 4889 | float remainder( float, float );§\indexc{remainder}§ |
---|
[b72bad4f] | 4890 | double remainder( double, double ); |
---|
| 4891 | long double remainder( long double, long double ); |
---|
| 4892 | |
---|
[e945826] | 4893 | [ int, float ] remquo( float, float );§\indexc{remquo}§ |
---|
[b72bad4f] | 4894 | float remquo( float, float, int * ); |
---|
| 4895 | [ int, double ] remquo( double, double ); |
---|
| 4896 | double remquo( double, double, int * ); |
---|
| 4897 | [ int, long double ] remquo( long double, long double ); |
---|
| 4898 | long double remquo( long double, long double, int * ); |
---|
| 4899 | |
---|
| 4900 | [ int, float ] div( float, float ); // alternative name for remquo |
---|
[e945826] | 4901 | float div( float, float, int * );§\indexc{div}§ |
---|
[b72bad4f] | 4902 | [ int, double ] div( double, double ); |
---|
| 4903 | double div( double, double, int * ); |
---|
| 4904 | [ int, long double ] div( long double, long double ); |
---|
| 4905 | long double div( long double, long double, int * ); |
---|
| 4906 | |
---|
[e945826] | 4907 | float fma( float, float, float );§\indexc{fma}§ |
---|
[b72bad4f] | 4908 | double fma( double, double, double ); |
---|
| 4909 | long double fma( long double, long double, long double ); |
---|
| 4910 | |
---|
[e945826] | 4911 | float fdim( float, float );§\indexc{fdim}§ |
---|
[b72bad4f] | 4912 | double fdim( double, double ); |
---|
| 4913 | long double fdim( long double, long double ); |
---|
| 4914 | |
---|
[e945826] | 4915 | float nan( const char * );§\indexc{nan}§ |
---|
[b72bad4f] | 4916 | double nan( const char * ); |
---|
| 4917 | long double nan( const char * ); |
---|
| 4918 | \end{lstlisting} |
---|
[07bc165] | 4919 | \ |
---|
[b72bad4f] | 4920 | |
---|
| 4921 | |
---|
| 4922 | \subsection{Exponential} |
---|
| 4923 | |
---|
[07bc165] | 4924 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4925 | float exp( float );§\indexc{exp}§ |
---|
[b72bad4f] | 4926 | double exp( double ); |
---|
| 4927 | long double exp( long double ); |
---|
| 4928 | float _Complex exp( float _Complex ); |
---|
| 4929 | double _Complex exp( double _Complex ); |
---|
| 4930 | long double _Complex exp( long double _Complex ); |
---|
| 4931 | |
---|
[e945826] | 4932 | float exp2( float );§\indexc{exp2}§ |
---|
[b72bad4f] | 4933 | double exp2( double ); |
---|
| 4934 | long double exp2( long double ); |
---|
[8bc4ef8] | 4935 | float _Complex exp2( float _Complex ); |
---|
| 4936 | double _Complex exp2( double _Complex ); |
---|
| 4937 | long double _Complex exp2( long double _Complex ); |
---|
[b72bad4f] | 4938 | |
---|
[e945826] | 4939 | float expm1( float );§\indexc{expm1}§ |
---|
[b72bad4f] | 4940 | double expm1( double ); |
---|
| 4941 | long double expm1( long double ); |
---|
| 4942 | |
---|
[e945826] | 4943 | float log( float );§\indexc{log}§ |
---|
[b72bad4f] | 4944 | double log( double ); |
---|
| 4945 | long double log( long double ); |
---|
| 4946 | float _Complex log( float _Complex ); |
---|
| 4947 | double _Complex log( double _Complex ); |
---|
| 4948 | long double _Complex log( long double _Complex ); |
---|
| 4949 | |
---|
[e945826] | 4950 | float log2( float );§\indexc{log2}§ |
---|
[b72bad4f] | 4951 | double log2( double ); |
---|
| 4952 | long double log2( long double ); |
---|
[8bc4ef8] | 4953 | float _Complex log2( float _Complex ); |
---|
| 4954 | double _Complex log2( double _Complex ); |
---|
| 4955 | long double _Complex log2( long double _Complex ); |
---|
[b72bad4f] | 4956 | |
---|
[e945826] | 4957 | float log10( float );§\indexc{log10}§ |
---|
[b72bad4f] | 4958 | double log10( double ); |
---|
| 4959 | long double log10( long double ); |
---|
[8bc4ef8] | 4960 | float _Complex log10( float _Complex ); |
---|
| 4961 | double _Complex log10( double _Complex ); |
---|
| 4962 | long double _Complex log10( long double _Complex ); |
---|
[b72bad4f] | 4963 | |
---|
[e945826] | 4964 | float log1p( float );§\indexc{log1p}§ |
---|
[b72bad4f] | 4965 | double log1p( double ); |
---|
| 4966 | long double log1p( long double ); |
---|
| 4967 | |
---|
[e945826] | 4968 | int ilogb( float );§\indexc{ilogb}§ |
---|
[b72bad4f] | 4969 | int ilogb( double ); |
---|
| 4970 | int ilogb( long double ); |
---|
| 4971 | |
---|
[e945826] | 4972 | float logb( float );§\indexc{logb}§ |
---|
[b72bad4f] | 4973 | double logb( double ); |
---|
| 4974 | long double logb( long double ); |
---|
| 4975 | \end{lstlisting} |
---|
[07bc165] | 4976 | \ |
---|
[b72bad4f] | 4977 | |
---|
| 4978 | |
---|
| 4979 | \subsection{Power} |
---|
| 4980 | |
---|
[07bc165] | 4981 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 4982 | float sqrt( float );§\indexc{sqrt}§ |
---|
[b72bad4f] | 4983 | double sqrt( double ); |
---|
| 4984 | long double sqrt( long double ); |
---|
| 4985 | float _Complex sqrt( float _Complex ); |
---|
| 4986 | double _Complex sqrt( double _Complex ); |
---|
| 4987 | long double _Complex sqrt( long double _Complex ); |
---|
| 4988 | |
---|
[e945826] | 4989 | float cbrt( float );§\indexc{cbrt}§ |
---|
[b72bad4f] | 4990 | double cbrt( double ); |
---|
| 4991 | long double cbrt( long double ); |
---|
| 4992 | |
---|
[e945826] | 4993 | float hypot( float, float );§\indexc{hypot}§ |
---|
[b72bad4f] | 4994 | double hypot( double, double ); |
---|
| 4995 | long double hypot( long double, long double ); |
---|
| 4996 | |
---|
[e945826] | 4997 | float pow( float, float );§\indexc{pow}§ |
---|
[b72bad4f] | 4998 | double pow( double, double ); |
---|
| 4999 | long double pow( long double, long double ); |
---|
| 5000 | float _Complex pow( float _Complex, float _Complex ); |
---|
| 5001 | double _Complex pow( double _Complex, double _Complex ); |
---|
| 5002 | long double _Complex pow( long double _Complex, long double _Complex ); |
---|
| 5003 | \end{lstlisting} |
---|
[07bc165] | 5004 | \ |
---|
[b72bad4f] | 5005 | |
---|
| 5006 | |
---|
| 5007 | \subsection{Trigonometric} |
---|
| 5008 | |
---|
[07bc165] | 5009 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 5010 | float sin( float );§\indexc{sin}§ |
---|
[b72bad4f] | 5011 | double sin( double ); |
---|
| 5012 | long double sin( long double ); |
---|
| 5013 | float _Complex sin( float _Complex ); |
---|
| 5014 | double _Complex sin( double _Complex ); |
---|
| 5015 | long double _Complex sin( long double _Complex ); |
---|
| 5016 | |
---|
[e945826] | 5017 | float cos( float );§\indexc{cos}§ |
---|
[b72bad4f] | 5018 | double cos( double ); |
---|
| 5019 | long double cos( long double ); |
---|
| 5020 | float _Complex cos( float _Complex ); |
---|
| 5021 | double _Complex cos( double _Complex ); |
---|
| 5022 | long double _Complex cos( long double _Complex ); |
---|
| 5023 | |
---|
[e945826] | 5024 | float tan( float );§\indexc{tan}§ |
---|
[b72bad4f] | 5025 | double tan( double ); |
---|
| 5026 | long double tan( long double ); |
---|
| 5027 | float _Complex tan( float _Complex ); |
---|
| 5028 | double _Complex tan( double _Complex ); |
---|
| 5029 | long double _Complex tan( long double _Complex ); |
---|
| 5030 | |
---|
[e945826] | 5031 | float asin( float );§\indexc{asin}§ |
---|
[b72bad4f] | 5032 | double asin( double ); |
---|
| 5033 | long double asin( long double ); |
---|
| 5034 | float _Complex asin( float _Complex ); |
---|
| 5035 | double _Complex asin( double _Complex ); |
---|
| 5036 | long double _Complex asin( long double _Complex ); |
---|
| 5037 | |
---|
[e945826] | 5038 | float acos( float );§\indexc{acos}§ |
---|
[b72bad4f] | 5039 | double acos( double ); |
---|
| 5040 | long double acos( long double ); |
---|
| 5041 | float _Complex acos( float _Complex ); |
---|
| 5042 | double _Complex acos( double _Complex ); |
---|
| 5043 | long double _Complex acos( long double _Complex ); |
---|
| 5044 | |
---|
[e945826] | 5045 | float atan( float );§\indexc{atan}§ |
---|
[b72bad4f] | 5046 | double atan( double ); |
---|
| 5047 | long double atan( long double ); |
---|
| 5048 | float _Complex atan( float _Complex ); |
---|
| 5049 | double _Complex atan( double _Complex ); |
---|
| 5050 | long double _Complex atan( long double _Complex ); |
---|
| 5051 | |
---|
[e945826] | 5052 | float atan2( float, float );§\indexc{atan2}§ |
---|
[b72bad4f] | 5053 | double atan2( double, double ); |
---|
| 5054 | long double atan2( long double, long double ); |
---|
| 5055 | |
---|
| 5056 | float atan( float, float ); // alternative name for atan2 |
---|
[e945826] | 5057 | double atan( double, double );§\indexc{atan}§ |
---|
[b72bad4f] | 5058 | long double atan( long double, long double ); |
---|
| 5059 | \end{lstlisting} |
---|
[07bc165] | 5060 | \ |
---|
[b72bad4f] | 5061 | |
---|
| 5062 | |
---|
| 5063 | \subsection{Hyperbolic} |
---|
| 5064 | |
---|
[07bc165] | 5065 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 5066 | float sinh( float );§\indexc{sinh}§ |
---|
[b72bad4f] | 5067 | double sinh( double ); |
---|
| 5068 | long double sinh( long double ); |
---|
| 5069 | float _Complex sinh( float _Complex ); |
---|
| 5070 | double _Complex sinh( double _Complex ); |
---|
| 5071 | long double _Complex sinh( long double _Complex ); |
---|
| 5072 | |
---|
[e945826] | 5073 | float cosh( float );§\indexc{cosh}§ |
---|
[b72bad4f] | 5074 | double cosh( double ); |
---|
| 5075 | long double cosh( long double ); |
---|
| 5076 | float _Complex cosh( float _Complex ); |
---|
| 5077 | double _Complex cosh( double _Complex ); |
---|
| 5078 | long double _Complex cosh( long double _Complex ); |
---|
| 5079 | |
---|
[e945826] | 5080 | float tanh( float );§\indexc{tanh}§ |
---|
[b72bad4f] | 5081 | double tanh( double ); |
---|
| 5082 | long double tanh( long double ); |
---|
| 5083 | float _Complex tanh( float _Complex ); |
---|
| 5084 | double _Complex tanh( double _Complex ); |
---|
| 5085 | long double _Complex tanh( long double _Complex ); |
---|
| 5086 | |
---|
[e945826] | 5087 | float asinh( float );§\indexc{asinh}§ |
---|
[b72bad4f] | 5088 | double asinh( double ); |
---|
| 5089 | long double asinh( long double ); |
---|
| 5090 | float _Complex asinh( float _Complex ); |
---|
| 5091 | double _Complex asinh( double _Complex ); |
---|
| 5092 | long double _Complex asinh( long double _Complex ); |
---|
| 5093 | |
---|
[e945826] | 5094 | float acosh( float );§\indexc{acosh}§ |
---|
[b72bad4f] | 5095 | double acosh( double ); |
---|
| 5096 | long double acosh( long double ); |
---|
| 5097 | float _Complex acosh( float _Complex ); |
---|
| 5098 | double _Complex acosh( double _Complex ); |
---|
| 5099 | long double _Complex acosh( long double _Complex ); |
---|
| 5100 | |
---|
[e945826] | 5101 | float atanh( float );§\indexc{atanh}§ |
---|
[b72bad4f] | 5102 | double atanh( double ); |
---|
| 5103 | long double atanh( long double ); |
---|
| 5104 | float _Complex atanh( float _Complex ); |
---|
| 5105 | double _Complex atanh( double _Complex ); |
---|
| 5106 | long double _Complex atanh( long double _Complex ); |
---|
| 5107 | \end{lstlisting} |
---|
[07bc165] | 5108 | \ |
---|
[b72bad4f] | 5109 | |
---|
| 5110 | |
---|
| 5111 | \subsection{Error / Gamma} |
---|
| 5112 | |
---|
[07bc165] | 5113 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 5114 | float erf( float );§\indexc{erf}§ |
---|
[b72bad4f] | 5115 | double erf( double ); |
---|
| 5116 | long double erf( long double ); |
---|
[8bc4ef8] | 5117 | float _Complex erf( float _Complex ); |
---|
| 5118 | double _Complex erf( double _Complex ); |
---|
| 5119 | long double _Complex erf( long double _Complex ); |
---|
[b72bad4f] | 5120 | |
---|
[e945826] | 5121 | float erfc( float );§\indexc{erfc}§ |
---|
[b72bad4f] | 5122 | double erfc( double ); |
---|
| 5123 | long double erfc( long double ); |
---|
[8bc4ef8] | 5124 | float _Complex erfc( float _Complex ); |
---|
| 5125 | double _Complex erfc( double _Complex ); |
---|
| 5126 | long double _Complex erfc( long double _Complex ); |
---|
[b72bad4f] | 5127 | |
---|
[e945826] | 5128 | float lgamma( float );§\indexc{lgamma}§ |
---|
[b72bad4f] | 5129 | double lgamma( double ); |
---|
| 5130 | long double lgamma( long double ); |
---|
| 5131 | float lgamma( float, int * ); |
---|
| 5132 | double lgamma( double, int * ); |
---|
| 5133 | long double lgamma( long double, int * ); |
---|
| 5134 | |
---|
[e945826] | 5135 | float tgamma( float );§\indexc{tgamma}§ |
---|
[b72bad4f] | 5136 | double tgamma( double ); |
---|
| 5137 | long double tgamma( long double ); |
---|
| 5138 | \end{lstlisting} |
---|
[07bc165] | 5139 | \ |
---|
[b72bad4f] | 5140 | |
---|
| 5141 | |
---|
| 5142 | \subsection{Nearest Integer} |
---|
| 5143 | |
---|
[07bc165] | 5144 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 5145 | float floor( float );§\indexc{floor}§ |
---|
[b72bad4f] | 5146 | double floor( double ); |
---|
| 5147 | long double floor( long double ); |
---|
| 5148 | |
---|
[e945826] | 5149 | float ceil( float );§\indexc{ceil}§ |
---|
[b72bad4f] | 5150 | double ceil( double ); |
---|
| 5151 | long double ceil( long double ); |
---|
| 5152 | |
---|
[e945826] | 5153 | float trunc( float );§\indexc{trunc}§ |
---|
[b72bad4f] | 5154 | double trunc( double ); |
---|
| 5155 | long double trunc( long double ); |
---|
| 5156 | |
---|
[e945826] | 5157 | float rint( float );§\indexc{rint}§ |
---|
[b72bad4f] | 5158 | long double rint( long double ); |
---|
| 5159 | long int rint( float ); |
---|
| 5160 | long int rint( double ); |
---|
| 5161 | long int rint( long double ); |
---|
| 5162 | long long int rint( float ); |
---|
| 5163 | long long int rint( double ); |
---|
| 5164 | long long int rint( long double ); |
---|
| 5165 | |
---|
[e945826] | 5166 | long int lrint( float );§\indexc{lrint}§ |
---|
[b72bad4f] | 5167 | long int lrint( double ); |
---|
| 5168 | long int lrint( long double ); |
---|
| 5169 | long long int llrint( float ); |
---|
| 5170 | long long int llrint( double ); |
---|
| 5171 | long long int llrint( long double ); |
---|
| 5172 | |
---|
[e945826] | 5173 | float nearbyint( float );§\indexc{nearbyint}§ |
---|
[b72bad4f] | 5174 | double nearbyint( double ); |
---|
| 5175 | long double nearbyint( long double ); |
---|
| 5176 | |
---|
[e945826] | 5177 | float round( float );§\indexc{round}§ |
---|
[b72bad4f] | 5178 | long double round( long double ); |
---|
| 5179 | long int round( float ); |
---|
| 5180 | long int round( double ); |
---|
| 5181 | long int round( long double ); |
---|
| 5182 | long long int round( float ); |
---|
| 5183 | long long int round( double ); |
---|
| 5184 | long long int round( long double ); |
---|
| 5185 | |
---|
[e945826] | 5186 | long int lround( float );§\indexc{lround}§ |
---|
[b72bad4f] | 5187 | long int lround( double ); |
---|
| 5188 | long int lround( long double ); |
---|
| 5189 | long long int llround( float ); |
---|
| 5190 | long long int llround( double ); |
---|
| 5191 | long long int llround( long double ); |
---|
| 5192 | \end{lstlisting} |
---|
[07bc165] | 5193 | \ |
---|
[b72bad4f] | 5194 | |
---|
| 5195 | |
---|
| 5196 | \subsection{Manipulation} |
---|
| 5197 | |
---|
[07bc165] | 5198 | \begin{lstlisting}[aboveskip=0pt,belowskip=0pt] |
---|
[e945826] | 5199 | float copysign( float, float );§\indexc{copysign}§ |
---|
[b72bad4f] | 5200 | double copysign( double, double ); |
---|
| 5201 | long double copysign( long double, long double ); |
---|
| 5202 | |
---|
[e945826] | 5203 | float frexp( float, int * );§\indexc{frexp}§ |
---|
[b72bad4f] | 5204 | double frexp( double, int * ); |
---|
| 5205 | long double frexp( long double, int * ); |
---|
| 5206 | |
---|
[e945826] | 5207 | float ldexp( float, int );§\indexc{ldexp}§ |
---|
[b72bad4f] | 5208 | double ldexp( double, int ); |
---|
| 5209 | long double ldexp( long double, int ); |
---|
| 5210 | |
---|
[e945826] | 5211 | [ float, float ] modf( float );§\indexc{modf}§ |
---|
[b72bad4f] | 5212 | float modf( float, float * ); |
---|
| 5213 | [ double, double ] modf( double ); |
---|
| 5214 | double modf( double, double * ); |
---|
| 5215 | [ long double, long double ] modf( long double ); |
---|
| 5216 | long double modf( long double, long double * ); |
---|
| 5217 | |
---|
[e945826] | 5218 | float nextafter( float, float );§\indexc{nextafter}§ |
---|
[b72bad4f] | 5219 | double nextafter( double, double ); |
---|
| 5220 | long double nextafter( long double, long double ); |
---|
| 5221 | |
---|
[e945826] | 5222 | float nexttoward( float, long double );§\indexc{nexttoward}§ |
---|
[b72bad4f] | 5223 | double nexttoward( double, long double ); |
---|
| 5224 | long double nexttoward( long double, long double ); |
---|
| 5225 | |
---|
[e945826] | 5226 | float scalbn( float, int );§\indexc{scalbn}§ |
---|
[b72bad4f] | 5227 | double scalbn( double, int ); |
---|
| 5228 | long double scalbn( long double, int ); |
---|
| 5229 | |
---|
[e945826] | 5230 | float scalbln( float, long int );§\indexc{scalbln}§ |
---|
[b72bad4f] | 5231 | double scalbln( double, long int ); |
---|
| 5232 | long double scalbln( long double, long int ); |
---|
| 5233 | \end{lstlisting} |
---|
[07bc165] | 5234 | \ |
---|
[b72bad4f] | 5235 | |
---|
| 5236 | |
---|
[e55ca05] | 5237 | \section{Rational Numbers} |
---|
| 5238 | \label{s:RationalNumbers} |
---|
| 5239 | |
---|
[c45170a] | 5240 | Rational numbers are numbers written as a ratio, \ie as a fraction, where the numerator (top number) and the denominator (bottom number) are whole numbers. |
---|
[e55ca05] | 5241 | When creating and computing with rational numbers, results are constantly reduced to keep the numerator and denominator as small as possible. |
---|
| 5242 | |
---|
[5479e63] | 5243 | \begin{lstlisting}[belowskip=0pt] |
---|
[e55ca05] | 5244 | // implementation |
---|
[e945826] | 5245 | struct Rational {§\indexc{Rational}§ |
---|
[e55ca05] | 5246 | long int numerator, denominator; // invariant: denominator > 0 |
---|
| 5247 | }; // Rational |
---|
| 5248 | |
---|
| 5249 | // constants |
---|
| 5250 | extern struct Rational 0; |
---|
| 5251 | extern struct Rational 1; |
---|
| 5252 | |
---|
| 5253 | // constructors |
---|
| 5254 | Rational rational(); |
---|
| 5255 | Rational rational( long int n ); |
---|
| 5256 | Rational rational( long int n, long int d ); |
---|
| 5257 | |
---|
| 5258 | // getter/setter for numerator/denominator |
---|
| 5259 | long int numerator( Rational r ); |
---|
| 5260 | long int numerator( Rational r, long int n ); |
---|
| 5261 | long int denominator( Rational r ); |
---|
| 5262 | long int denominator( Rational r, long int d ); |
---|
| 5263 | |
---|
| 5264 | // comparison |
---|
| 5265 | int ?==?( Rational l, Rational r ); |
---|
| 5266 | int ?!=?( Rational l, Rational r ); |
---|
| 5267 | int ?<?( Rational l, Rational r ); |
---|
| 5268 | int ?<=?( Rational l, Rational r ); |
---|
| 5269 | int ?>?( Rational l, Rational r ); |
---|
| 5270 | int ?>=?( Rational l, Rational r ); |
---|
| 5271 | |
---|
| 5272 | // arithmetic |
---|
| 5273 | Rational -?( Rational r ); |
---|
| 5274 | Rational ?+?( Rational l, Rational r ); |
---|
| 5275 | Rational ?-?( Rational l, Rational r ); |
---|
| 5276 | Rational ?*?( Rational l, Rational r ); |
---|
| 5277 | Rational ?/?( Rational l, Rational r ); |
---|
| 5278 | |
---|
| 5279 | // conversion |
---|
| 5280 | double widen( Rational r ); |
---|
| 5281 | Rational narrow( double f, long int md ); |
---|
| 5282 | |
---|
| 5283 | // I/O |
---|
| 5284 | forall( dtype istype | istream( istype ) ) istype * ?|?( istype *, Rational * ); |
---|
| 5285 | forall( dtype ostype | ostream( ostype ) ) ostype * ?|?( ostype *, Rational ); |
---|
| 5286 | \end{lstlisting} |
---|
| 5287 | |
---|
| 5288 | |
---|
[6c91065] | 5289 | \bibliographystyle{plain} |
---|
[e55ca05] | 5290 | \bibliography{cfa} |
---|
[6c91065] | 5291 | |
---|
| 5292 | |
---|
| 5293 | \addcontentsline{toc}{section}{\indexname} % add index name to table of contents |
---|
| 5294 | \begin{theindex} |
---|
| 5295 | Italic page numbers give the location of the main entry for the referenced term. |
---|
| 5296 | Plain page numbers denote uses of the indexed term. |
---|
| 5297 | Entries for grammar non-terminals are italicized. |
---|
| 5298 | A typewriter font is used for grammar terminals and program identifiers. |
---|
| 5299 | \indexspace |
---|
| 5300 | \input{user.ind} |
---|
| 5301 | \end{theindex} |
---|
| 5302 | |
---|
| 5303 | |
---|
| 5304 | \end{document} |
---|
| 5305 | |
---|
| 5306 | % Local Variables: % |
---|
| 5307 | % tab-width: 4 % |
---|
| 5308 | % fill-column: 100 % |
---|
| 5309 | % compile-command: "make" % |
---|
| 5310 | % End: % |
---|