source: doc/refrat/refrat.tex @ a0fdbd5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a0fdbd5 was b52d900, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

extend user manual, update latex macros, and update reference manual

  • Property mode set to 100644
File size: 194.3 KB
Line 
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%% refrat.tex --
9%%
10%% Author           : Peter A. Buhr
11%% Created On       : Wed Apr  6 14:52:25 2016
12%% Last Modified By : Peter A. Buhr
13%% Last Modified On : Sat Apr  9 10:19:12 2016
14%% Update Count     : 8
15%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17% requires tex packages: texlive-base texlive-latex-base tex-common texlive-humanities texlive-latex-extra texlive-fonts-recommended
18
19\documentclass[openright,twoside]{report}
20%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
21
22% Latex packages used in the document.
23\usepackage{fullpage,times}
24\usepackage{xspace}
25\usepackage{varioref}
26\usepackage{listings}
27\usepackage{comment}
28\usepackage{latexsym}                                   % \Box
29\usepackage{mathptmx}                                   % better math font with "times"
30\usepackage[pagewise]{lineno}
31\renewcommand{\linenumberfont}{\scriptsize\sffamily}
32\usepackage[dvips,plainpages=false,pdfpagelabels,pdfpagemode=UseNone,colorlinks=true,pagebackref=true,linkcolor=blue,citecolor=blue,urlcolor=blue,pagebackref=true,breaklinks=true]{hyperref}
33\usepackage{breakurl}
34\renewcommand{\UrlFont}{\small\sf}
35
36%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37
38% Bespoke macros used in the document.
39\input{common}
40
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42
43\setcounter{secnumdepth}{3}                             % number subsubsections
44\setcounter{tocdepth}{3}                                % subsubsections in table of contents
45\makeindex
46
47%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
48
49\begin{document}
50\pagestyle{headings}
51\linenumbers                                            % comment out to turn off line numbering
52
53\title{\Huge
54\vspace*{1in}
55\CFA (\CFL) Reference Manual and Rationale
56}% title
57\author{\huge
58Glen Ditchfield and Peter A. Buhr
59}% author
60\date{
61DRAFT\\\today
62}% date
63
64\pagenumbering{roman}
65\pagestyle{plain}
66
67\maketitle
68
69\vspace*{\fill}
70\thispagestyle{empty}
71\noindent
72\copyright\,2015 Glen Ditchfield \\ \\
73\noindent
74This work is licensed under the Creative Commons Attribution 4.0 International License.
75To view a copy of this license, visit {\small\url{http://creativecommons.org/licenses/by/4.0}}.
76\vspace*{1in}
77
78\clearpage
79\pdfbookmark[1]{Contents}{section}
80\tableofcontents
81
82\clearpage
83\pagenumbering{arabic}
84
85
86\chapter*{Introduction}\addcontentsline{toc}{chapter}{Introduction}
87
88This document is a reference manual and rationale for \CFA, a polymorphic extension of the C programming language.
89It makes frequent reference to the {\c11} standard \cite{C11}, and occasionally compares \CFA to {\CC} \cite{C++}.
90
91The manual deliberately imitates the ordering of the {\c11} standard (although the section numbering differs).
92Unfortunately, this means the manual contains more ``forward references'' than usual, making it harder to follow if the reader does not have a copy of the {\c11} standard.
93For a simple introduction to \CFA, see the companion document ``An Overview of \CFA''
94\cite{Ditchfield96:Overview}.
95
96\begin{rationale}
97Commentary (like this) is quoted with quads.
98Commentary usually deals with subtle points, the rationale behind a rule, and design decisions.
99\end{rationale}
100
101% No ``Scope'' or ``Normative references'' chapters yet.
102
103
104\setcounter{chapter}{2}
105\chapter{Terms, definitions, and symbols}
106
107Terms from the {\c11} standard used in this document have the same meaning as in the {\c11} standard.
108
109% No ``Conformance'' or ``Environment'' chapters yet.
110
111
112\setcounter{chapter}{5}
113\chapter{Language}
114
115
116\section{Notation}
117The syntax notation used in this document is the same as in the {\c11} standard, with one exception: ellipsis in the definition of a nonterminal, as in ``\emph{declaration:} \ldots'', indicates that these rules extend a previous definition, which occurs in this document or in the {\c11} standard.
118
119
120\section{Concepts}
121
122
123\subsection{Scopes of identifiers}\index{scopes}
124
125\CFA's scope rules differ from C's in one major respect: a declaration of an identifier may overload\index{overloading} outer declarations of lexically identical identifiers in the same
126\Index{name space}, instead of hiding them.
127The outer declaration is hidden if the two declarations have \Index{compatible type}, or if one declares an array type and the other declares a pointer type and the element type and pointed-at type are compatible, or if one has function type and the other is a pointer to a compatible function type, or if one declaration is a \lstinline$type$\use{type} or
128\lstinline$typedef$\use{typedef} declaration and the other is not.  The outer declaration becomes
129\Index{visible} when the scope of the inner declaration terminates.
130\begin{rationale}
131Hence, a \CFA program can declare an \lstinline$int v$ and a \lstinline$float v$ in the same scope;
132a {\CC} program can not.
133\end{rationale}
134
135
136\subsection{Linkage of identifiers}
137\index{linkage}
138
139\CFA's linkage rules differ from C's in only one respect: instances of a particular identifier with external or internal linkage do not necessarily denote the same object or function.
140Instead, in the set of translation units and libraries that constitutes an entire program, any two instances of a particular identifier with \Index{external linkage} denote the same object or function if they have
141\Index{compatible type}s, or if one declares an array type and the other declares a pointer type and the element type and pointed-at type are compatible, or if one has function type and the other is a pointer to a compatible function type.
142Within one translation unit, each instance of an identifier with \Index{internal linkage} denotes the same object or function in the same circumstances.
143Identifiers with \Index{no linkage} always denote unique entities.
144\begin{rationale}
145A \CFA program can declare an \lstinline$extern int v$ and an \lstinline$extern float v$;
146a C program cannot.
147\end{rationale}
148
149
150\setcounter{subsection}{8}
151\subsection{Generic Types}
152
153
154\subsubsection{Semantics}
155
156\CFA provides a capability for generic types;
157using this capability a single "generic type generator" can be written that can represent multiple concrete type instantiations by substitution of the "type parameters" of the generic type for concrete types.
158Syntactically a generic type generator is represented by putting a forall specifier on a struct or union declaration, as defined in \VRef{forall}.
159An instantiation of the generic type is written by specifying the type parameters in parentheses after the name of the generic type generator:
160\begin{lstlisting}
161forall( otype T | sumable( T ) ) struct pair {
162        T x;
163        T y;
164};
165pair( int ) p = { 3, 14 };
166\end{lstlisting}
167
168The type parameters in an instantiation of a generic type must satisfy any constraints in the forall specifier on the type generator declaration, e.g., \lstinline$sumable$.
169The instantiation then has the semantics that would result if the type parameters were substituted into the type generator declaration by macro substitution.
170
171Polymorphic functions may have generic types as parameters, and those generic types may use type parameters of the polymorphic function as type parameters of the generic type:
172\begin{lstlisting}
173forall( otype T ) void swap( pair(T) *p ) {
174        T z = p->x;
175        p->x = p->y;
176        p->y = z;
177}
178\end{lstlisting}
179
180
181\subsubsection{Constraints}
182
183To avoid unduly constraining implementors, the generic type generator definition must be visible at any point where it is instantiated.
184Forward declarations of generic type generators are not forbidden, but the definition must be visible to instantiate the generic type.  Equivalently, instantiations of generic types are not allowed to be incomplete types.
185
186\examples
187\begin{lstlisting}
188forall( otype T ) struct A;
189
190forall( otype T ) struct B {
191        A(T) *a;                        // legal, but cannot instantiate B(T)
192};
193
194B(T) x;                                 // illegal, *x.a is of an incomplete generic type
195 
196forall( otype T ) struct A {
197        B( T ) *b;
198};
199
200B( T ) y;                               // legal, *x.a is now of a complete generic type
201
202// box.h:
203        forall( otype T ) struct box;
204        forall( otype T ) box( T ) *make_box( T );
205        forall( otype T ) void use_box( box( T ) *b );
206       
207// main.c:
208        box( int ) *b = make_box( 42 ); // illegal, definition of box not visible
209        use_box( b );           // illegal
210\end{lstlisting}
211
212
213\section{Conversions}
214\CFA defines situations where values of one type are automatically converted to another type.
215These conversions are called \define{implicit conversion}s.
216The programmer can request
217\define{explicit conversion}s using cast expressions.
218
219
220\subsection{Arithmetic operands}
221
222
223\setcounter{subsubsection}{8}
224\subsubsection{Safe arithmetic conversions}
225
226In C, a pattern of conversions known as the \define{usual arithmetic conversion}s is used with most binary arithmetic operators to convert the operands to a common type and determine the type of the operator's result.
227In \CFA, these conversions play a role in overload resolution, and collectively are called the \define{safe arithmetic conversion}s.
228
229Let \(int_r\) and \(unsigned_r\) be the signed and unsigned integer types with integer conversion rank\index{integer conversion rank}\index{rank|see{integer conversion rank}} $r$.
230Let \(unsigned_{mr}\) be the unsigned integer type with maximal rank.
231
232The following conversions are \emph{direct} safe arithmetic conversions.
233\begin{itemize}
234\item
235The \Index{integer promotion}s.
236\item
237For every rank $r$ greater than or equal to the rank of \lstinline$int$, conversion from \(int_r\) to \(unsigned_r\).
238\item
239For every rank $r$ greater than or equal to the rank of \lstinline$int$, where \(int_{r+1}\) exists and can represent all values of \(unsigned_r\), conversion from \(unsigned_r\) to \(int_{r+1}\).
240\item
241Conversion from \(unsigned_{mr}\) to \lstinline$float$.
242\item
243Conversion from an enumerated type to its compatible integer type.
244\item
245Conversion from \lstinline$float$ to \lstinline$double$, and from \lstinline$double$ to \lstinline$long double$.
246\item
247Conversion from \lstinline$float _Complex$ to \lstinline$double _Complex$, and from \lstinline$double _Complex$ to \lstinline$long double _Complex$.
248\begin{sloppypar}
249\item
250Conversion from \lstinline$float _Imaginary$ to \lstinline$double _Imaginary$, and from \lstinline$double _Imaginary$ to \lstinline$long double$ \lstinline$_Imaginary$, if the implementation supports imaginary types.
251\end{sloppypar}
252\end{itemize}
253
254If type \lstinline$T$ can be converted to type \lstinline$U$ by a safe direct arithmetic conversion and type \lstinline$U$ can be converted to type \lstinline$V$ by a safe arithmetic conversion, then the conversion from \lstinline$T$ to type \lstinline$V$ is an \emph{indirect} safe arithmetic conversion.
255
256\begin{rationale}
257Note that {\c11} does not include conversion from \Index{real type}s to \Index{complex type}s in the usual arithmetic conversions, and \CFA does not include them as safe conversions.
258\end{rationale}
259
260
261\subsection{Other operands}
262
263
264\setcounter{subsubsection}{3}
265\subsubsection{Anonymous structures and unions}
266\label{anon-conv}
267
268If an expression's type is a pointer to a structure or union type that has a member that is an
269\Index{anonymous structure} or an \Index{anonymous union}, it can be implicitly converted\index{implicit conversion} to a pointer to the anonymous structure's or anonymous union's type.
270The result of the conversion is a pointer to the member.
271
272\examples
273\begin{lstlisting}
274struct point {
275        int x, y;
276};
277void move_by( struct point * p1, struct point * p2 ) {@\impl{move_by}@
278        p1->x += p2.x;
279        p1->y += p2.y;
280}
281struct color_point {
282        enum { RED, BLUE, GREEN } color;
283        struct point;
284} cp1, cp2;
285move_to( &cp1, &cp2 );
286\end{lstlisting}
287Thanks to implicit conversion, the two arguments that \lstinline$move_by()$ receives are pointers to
288\lstinline$cp1$'s second member and \lstinline$cp2$'s second member.
289
290
291\subsubsection{Specialization}
292A function or value whose type is polymorphic may be implicitly converted to one whose type is \Index{less polymorphic} by binding values to one or more of its \Index{inferred parameter}.
293Any value that is legal for the inferred parameter may be used, including other inferred parameters.
294
295If, after the inferred parameter binding, an \Index{assertion parameter} has no inferred parameters in its type, then an object or function must be visible at the point of the specialization that has the same identifier as the assertion parameter and has a type that is compatible\index{compatible type} with or can be specialized to the type of the assertion parameter.
296The assertion parameter is bound to that object or function.
297
298The type of the specialization is the type of the original with the bound inferred parameters and the bound assertion parameters replaced by their bound values.
299
300\examples
301The type
302\begin{lstlisting}
303forall( otype T, otype U ) void (*)( T, U );
304\end{lstlisting}
305can be specialized to (among other things)
306\begin{lstlisting}
307forall( otype T ) void (*)( T, T );             // U bound to T
308forall( otype T ) void (*)( T, real );  // U bound to real
309forall( otype U ) void (*)( real, U );  // T bound to real
310void f( real, real );                                   // both bound to real
311\end{lstlisting}
312
313The type
314\begin{lstlisting}
315forall( otype T | T ?+?( T, T ) ) T (*)( T );
316\end{lstlisting}
317can be specialized to (among other things)
318\begin{lstlisting}
319int (*)( int );         // T bound to int, and T ?+?(T, T ) bound to int ?+?( int, int )
320\end{lstlisting}
321
322
323\subsubsection{Safe conversions}
324
325A \define{direct safe conversion} is one of the following conversions:
326\begin{itemize}
327\item
328a direct safe arithmetic conversion;
329\item
330from any object type or incomplete type to \lstinline$void$;
331\item
332from a pointer to any non-\lstinline$void$ type to a pointer to \lstinline$void$;
333\item
334from a pointer to any type to a pointer to a more qualified version of the type\index{qualified type};
335\item
336from a pointer to a structure or union type to a pointer to the type of a member of the structure or union that is an \Index{anonymous structure} or an \Index{anonymous union};
337\item
338within the scope of an initialized \Index{type declaration}, conversions between a type and its implementation or between a pointer to a type and a pointer to its implementation.
339\end{itemize}
340
341Conversions that are not safe conversions are \define{unsafe conversion}s.
342\begin{rationale}
343As in C, there is an implicit conversion from \lstinline$void *$ to any pointer type.
344This is clearly dangerous, and {\CC} does not have this implicit conversion.
345\CFA\index{deficiencies!void * conversion} keeps it, in the interest of remaining as pure a superset of C as possible, but discourages it by making it unsafe.
346\end{rationale}
347
348
349\subsection{Conversion cost}
350
351The \define{conversion cost} of a safe\index{safe conversion} conversion\footnote{Unsafe\index{unsafe conversion} conversions do not have defined conversion costs.} is a measure of how desirable or undesirable it is.
352It is defined as follows.
353\begin{itemize}
354\item
355The cost of a conversion from any type to itself is 0.
356
357\item
358The cost of a direct safe conversion is 1.
359
360\item
361The cost of an indirect safe arithmetic conversion is the smallest number of direct conversions needed to make up the conversion.
362\end{itemize}
363
364\examples
365In the following, assume an implementation that does not provide any extended integer types.
366
367\begin{itemize}
368\item
369The cost of an implicit conversion from \lstinline$int$ to \lstinline$long$ is 1.
370The cost of an implicit conversion from \lstinline$long$ to \lstinline$double$ is 3, because it is defined in terms of conversions from \lstinline$long$ to \lstinline$unsigned long$, then to \lstinline$float$, and then to \lstinline$double$.
371
372\item
373If \lstinline$int$ can represent all the values of \lstinline$unsigned short$, then the cost of an implicit conversion from \lstinline$unsigned short$ to \lstinline$unsigned$ is 2:
374\lstinline$unsigned short$ to \lstinline$int$ to \lstinline$unsigned$.
375Otherwise, \lstinline$unsigned short$ is converted directly to \lstinline$unsigned$, and the cost is 1.
376
377\item
378If \lstinline$long$ can represent all the values of \lstinline$unsigned$, then the conversion cost of \lstinline$unsigned$ to \lstinline$long$ is 1.
379Otherwise, the conversion is an unsafe conversion, and its conversion cost is undefined.
380\end{itemize}
381
382\section{Lexical elements}
383\subsection{Keywords}
384\begin{syntax}
385\oldlhs{keyword}
386        \rhs \lstinline$forall$
387        \rhs \lstinline$lvalue$
388        \rhs \lstinline$trait$
389        \rhs \lstinline$dtype$
390        \rhs \lstinline$ftype$
391        \rhs \lstinline$type$
392\end{syntax}
393
394
395\subsection{Identifiers}
396
397\CFA allows operator \Index{overloading} by associating operators with special function identifiers.
398Furthermore, the constants ``\lstinline$0$'' and ``\lstinline$1$'' have special status for many of C's data types (and for many programmer-defined data types as well), so \CFA treats them as overloadable identifiers.
399Programmers can use these identifiers to declare functions and objects that implement operators and constants for their own types.
400
401
402\setcounter{subsubsection}{2}
403\subsubsection{Constant identifiers}
404
405\begin{syntax}
406\oldlhs{identifier}
407\rhs \lstinline$0$
408\rhs \lstinline$1$
409\end{syntax}
410
411\index{constant identifiers}\index{identifiers!for constants} The tokens ``\lstinline$0$''\impl{0} and ``\lstinline$1$''\impl{1} are identifiers.
412No other tokens defined by the rules for integer constants are considered to be identifiers.
413\begin{rationale}
414Why ``\lstinline$0$'' and ``\lstinline$1$''? Those integers have special status in C.
415All scalar types can be incremented and decremented, which is defined in terms of adding or subtracting 1.
416The operations ``\lstinline$&&$'', ``\lstinline$||$'', and ``\lstinline$!$'' can be applied to any scalar arguments, and are defined in terms of comparison against 0.
417A \nonterm{constant-expression} that evaluates to 0 is effectively compatible with every pointer type.
418
419In 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.
420However, 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.
421Defining special constants for a user-defined type is more efficient than defining a conversion to the type from \lstinline$_Bool$.
422
423Why \emph{just} ``\lstinline$0$'' and ``\lstinline$1$''? Why not other integers? No other integers have special status in C.
424A facility that let programmers declare specific constants---``\lstinline$const Rational 12$'', for instance---would not be much of an improvement.
425Some facility for defining the creation of values of programmer-defined types from arbitrary integer tokens would be needed.
426The complexity of such a feature doesn't seem worth the gain.
427\end{rationale}
428
429
430\subsubsection{Operator identifiers}
431
432\index{operator identifiers}\index{identifiers!for operators} Table \ref{opids} lists the programmer-definable operator identifiers and the operations they are associated with.
433Functions that are declared with (or pointed at by function pointers that are declared with) these identifiers can be called by expressions that use the operator tokens and syntax, or the operator identifiers and ``function call'' syntax.
434The relationships between operators and function calls are discussed in descriptions of the operators.
435
436\begin{table}[hbt]
437\hfil
438\begin{tabular}[t]{ll}
439%identifier & operation \\ \hline
440\lstinline$?[?]$ & subscripting \impl{?[?]}\\
441\lstinline$?()$ & function call \impl{?()}\\
442\lstinline$?++$ & postfix increment \impl{?++}\\
443\lstinline$?--$ & postfix decrement \impl{?--}\\
444\lstinline$++?$ & prefix increment \impl{++?}\\
445\lstinline$--?$ & prefix decrement \impl{--?}\\
446\lstinline$*?$ & dereference \impl{*?}\\
447\lstinline$+?$ & unary plus \impl{+?}\\
448\lstinline$-?$ & arithmetic negation \impl{-?}\\
449\lstinline$~?$ & bitwise negation \impl{~?}\\
450\lstinline$!?$ & logical complement \impl{"!?}\\
451\lstinline$?*?$ & multiplication \impl{?*?}\\
452\lstinline$?/?$ & division \impl{?/?}\\
453\end{tabular}\hfil
454\begin{tabular}[t]{ll}
455%identifier & operation \\ \hline
456\lstinline$?%?$ & remainder \impl{?%?}\\
457\lstinline$?+?$ & addition \impl{?+?}\\
458\lstinline$?-?$ & subtraction \impl{?-?}\\
459\lstinline$?<<?$ & left shift \impl{?<<?}\\
460\lstinline$?>>?$ & right shift \impl{?>>?}\\
461\lstinline$?<?$ & less than \impl{?<?}\\
462\lstinline$?<=?$ & less than or equal \impl{?<=?}\\
463\lstinline$?>=?$ & greater than or equal \impl{?>=?}\\
464\lstinline$?>?$ & greater than \impl{?>?}\\
465\lstinline$?==?$ & equality \impl{?==?}\\
466\lstinline$?!=?$ & inequality \impl{?"!=?}\\
467\lstinline$?&?$ & bitwise AND \impl{?&?}\\
468\end{tabular}\hfil
469\begin{tabular}[t]{ll}
470%identifier & operation \\ \hline
471\lstinline$?^?$ & exclusive OR \impl{?^?}\\
472\lstinline$?|?$ & inclusive OR \impl{?"|?}\\
473\lstinline$?=?$ & simple assignment \impl{?=?}\\
474\lstinline$?*=?$ & multiplication assignment \impl{?*=?}\\
475\lstinline$?/=?$ & division assignment \impl{?/=?}\\
476\lstinline$?%=?$ & remainder assignment \impl{?%=?}\\
477\lstinline$?+=?$ & addition assignment \impl{?+=?}\\
478\lstinline$?-=?$ & subtraction assignment \impl{?-=?}\\
479\lstinline$?<<=?$ & left-shift assignment \impl{?<<=?}\\
480\lstinline$?>>=?$ & right-shift assignment \impl{?>>=?}\\
481\lstinline$?&=?$ & bitwise AND assignment \impl{?&=?}\\
482\lstinline$?^=?$ & exclusive OR assignment \impl{?^=?}\\
483\lstinline$?|=?$ & inclusive OR assignment \impl{?"|=?}\\
484\end{tabular}
485\hfil
486\caption{Operator Identifiers}
487\label{opids}
488\end{table}
489
490\begin{rationale}
491Operator identifiers are made up of the characters of the operator token, with question marks added to mark the positions of the arguments of operators.
492The question marks serve as mnemonic devices;
493programmers can not create new operators by arbitrarily mixing question marks and other non-alphabetic characters.
494Note that prefix and postfix versions of the increment and decrement operators are distinguished by the position of the question mark.
495\end{rationale}
496
497\begin{rationale}
498The use of ``\lstinline$?$'' in identifiers means that some C programs are not \CFA programs.  For instance, the sequence of characters ``\lstinline$(i < 0)?--i:i$'' is legal in a C program, but a
499\CFA compiler detects a syntax error because it treats ``\lstinline$?--$'' as an identifier, not as the two tokens ``\lstinline$?$'' and ``\lstinline$--$''.
500\end{rationale}
501
502\begin{rationale}
503Certain operators \emph{cannot} be defined by the programmer:
504\begin{itemize}
505\item
506The logical operators ``\lstinline$&&$'' and ``\lstinline$||$'', and the conditional operator
507``\lstinline$?:$''.
508These operators do not always evaluate their operands, and hence can not be properly defined by functions unless some mechanism like call-by-name is added to the language.
509Note that the definitions of ``\lstinline$&&$'' and ``\lstinline$||$'' say that they work by checking that their arguments are unequal to 0, so defining ``\lstinline$!=$'' and ``\lstinline$0$'' for user-defined types is enough to allow them to be used in logical expressions.
510
511\item
512The comma operator\index{comma expression}.
513It is a control-flow operator like those above.
514Changing its meaning seems pointless and confusing.
515
516\item
517The ``address of'' operator.
518It would seem useful to define a unary ``\lstinline$&$'' operator that returns values of some programmer-defined pointer-like type.
519The problem lies with the type of the operator.
520Consider the expression ``\lstinline$p = &x$'', where \lstinline$x$ is of type
521\lstinline$T$ and \lstinline$p$ has the programmer-defined type \lstinline$T_ptr$.
522The expression might be treated as a call to the unary function ``\lstinline$&?$''.
523Now what is the type of the function's parameter? It can not be \lstinline$T$, because then \lstinline$x$ would be passed by value, and there is no way to create a useful pointer-like result from a value.
524Hence the parameter must have type \lstinline$T *$.
525But then the expression must be rewritten as ``\lstinline$p = &?( &x )$''
526---which doesn't seem like progress!
527
528The rule for address-of expressions would have to be something like ``keep applying address-of functions until you get one that takes a pointer argument, then use the built-in operator and stop''.
529It seems simpler to define a conversion function from \lstinline$T *$ to \lstinline$T_ptr$.
530
531\item
532The \lstinline$sizeof$ operator.
533It is already defined for every object type, and intimately tied into the language's storage allocation model.
534Redefining it seems pointless.
535
536\item
537The ``member of'' operators ``\lstinline$.$'' and ``\lstinline$->$''.
538These are not really infix operators, since their right ``operand'' is not a value or object.
539
540\item
541Cast operators\index{cast expression}.
542Anything that can be done with an explicit cast can be done with a function call.
543The difference in syntax is small.
544\end{itemize}
545\end{rationale}
546
547
548\section{Expressions}
549
550\CFA allows operators and identifiers to be overloaded.
551Hence, each expression can have a number of \define{interpretation}s, each of which has a different type.
552The interpretations that are potentially executable are called \define{valid interpretation}s.
553The set of interpretations depends on the kind of expression and on the interpretations of the subexpressions that it contains.
554The rules for determining the valid interpretations of an expression are discussed below for each kind of expression.
555Eventually the context of the outermost expression chooses one interpretation of that expression.
556
557An \define{ambiguous interpretation} is an interpretation which does not specify the exact object or function denoted by every identifier in the expression.
558An expression can have some interpretations that are ambiguous and others that are unambiguous.
559An expression that is chosen to be executed shall not be ambiguous.
560
561The \define{best valid interpretations} are the valid interpretations that use the fewest unsafe\index{unsafe conversion} conversions.
562Of these, the best are those where the functions and objects involved are the least polymorphic\index{less polymorphic}.
563Of these, the best have the lowest total \Index{conversion cost}, including all implicit conversions in the argument expressions.
564Of these, the best have the highest total conversion cost for the implicit conversions
565(if any) applied to the argument expressions.
566If there is no single best valid interpretation, or if the best valid interpretation is ambiguous, then the resulting interpretation is ambiguous\index{ambiguous interpretation}.
567
568\begin{rationale}
569\CFA's rules for selecting the best interpretation are designed to allow overload resolution to mimic C's operator semantics.
570In C, the ``usual arithmetic conversions'' are applied to the operands of binary operators if necessary to convert the operands to types with a common real type.
571In \CFA, those conversions are ``safe''.
572The ``fewest unsafe conversions'' rule ensures that the usual conversions are done, if possible.
573The ``lowest total expression cost'' rule chooses the proper common type.
574The odd-looking ``highest argument conversion cost'' rule ensures that, when unary expressions must be converted, conversions of function results are preferred to conversion of function arguments: \lstinline$(double)-i$ will be preferred to \lstinline$-(double)i$.
575
576The ``least polymorphic'' rule reduces the number of polymorphic function calls, since such functions are presumably more expensive than monomorphic functions and since the more specific function is presumably more appropriate.
577It also gives preference to monomorphic values (such as the
578\lstinline$int$ \lstinline$0$) over polymorphic values (such as the \Index{null pointer}
579\lstinline$0$\use{0}).
580However, interpretations that call polymorphic functions are preferred to interpretations that perform unsafe conversions, because those conversions potentially lose accuracy or violate strong typing.
581
582There are two notable differences between \CFA's overload resolution rules and the rules for
583{\CC} defined in \cite{C++}.
584First, the result type of a function plays a role.
585In {\CC}, a function call must be completely resolved based on the arguments to the call in most circumstances.
586In \CFA, a function call may have several interpretations, each with a different result type, and the interpretations of the containing context choose among them.
587Second, safe conversions are used to choose among interpretations of all sorts of functions;
588in {\CC}, the ``usual arithmetic conversions'' are a separate set of rules that apply only to the built-in operators.
589\end{rationale}
590
591Expressions involving certain operators\index{operator identifiers} are considered to be equivalent to function calls.
592A transformation from ``operator'' syntax to ``function call'' syntax is defined by \define{rewrite rules}.
593Each operator has a set of predefined functions that overload its identifier.
594Overload resolution determines which member of the set is executed in a given expression.
595The functions have \Index{internal linkage} and are implicitly declared with \Index{file scope}.
596The predefined functions and rewrite rules are discussed below for each of these operators.
597\begin{rationale}
598Predefined functions and constants have internal linkage because that simplifies optimization in traditional compile-and-link environments.
599For instance, ``\lstinline$an_int + an_int$'' is equivalent to ``\lstinline$?+?(an_int, an_int)$''.
600If integer addition has not been redefined in the current scope, a compiler can generate code to perform the addition directly.
601If predefined functions had external linkage, this optimization would be difficult.
602\end{rationale}
603
604\begin{rationale}
605Since each subsection describes the interpretations of an expression in terms of the interpretations of its subexpressions, this chapter can be taken as describing an overload resolution algorithm that uses one bottom-up pass over an expression tree.
606Such an algorithm was first described (for Ada) by Baker~\cite{Bak:overload}.
607It is extended here to handle polymorphic functions and arithmetic conversions.
608The overload resolution rules and the predefined functions have been chosen so that, in programs that do not introduce overloaded declarations, expressions will have the same meaning in C and in \CFA.
609\end{rationale}
610
611\begin{rationale}
612Expression syntax is quoted from the {\c11} standard.
613The syntax itself defines the precedence and associativity of operators.
614The sections are arranged in decreasing order of precedence, with all operators in a section having the same precedence.
615\end{rationale}
616
617
618\subsection{Primary expressions}
619
620\begin{syntax}
621\lhs{primary-expression}
622\rhs \nonterm{identifier}
623\rhs \nonterm{constant}
624\rhs \nonterm{string-literal}
625\rhs \lstinline$($ \nonterm{expression} \lstinline$)$
626\rhs \nonterm{generic-selection}
627\end{syntax}
628
629\predefined
630\begin{lstlisting}
631const int 1;@\use{1}@
632const int 0;@\use{0}@
633forall( dtype DT ) DT * const 0;
634forall( ftype FT ) FT * const 0;
635\end{lstlisting}
636
637\semantics
638The \Index{valid interpretation} of an \nonterm{identifier} are given by the visible\index{visible} declarations of the identifier.
639
640A \nonterm{constant} or \nonterm{string-literal} has one valid interpretation, which has the type and value defined by {\c11}.
641The predefined integer identifiers ``\lstinline$1$'' and ``\lstinline$0$'' have the integer values 1 and 0, respectively.
642The other two predefined ``\lstinline$0$'' identifiers are bound to polymorphic pointer values that, when specialized\index{specialization} with a data type or function type respectively, produce a null pointer of that type.
643
644A parenthesised expression has the same interpretations as the contained \nonterm{expression}.
645
646\examples
647The expression \lstinline$(void *)0$\use{0} specializes the (polymorphic) null pointer to a null pointer to \lstinline$void$. \lstinline$(const void *)0$ does the same, and also uses a safe conversion from \lstinline$void *$ to \lstinline$const void *$.
648In each case, the null pointer conversion is better\index{best valid interpretations} than the unsafe conversion of the integer
649\lstinline$0$ to a pointer.
650
651\begin{rationale}
652Note that the predefined identifiers have addresses.
653
654\CFA does not have C's concept of ``null pointer constants'', which are not typed values but special strings of tokens.
655The C token ``\lstinline$0$'' is an expression of type \lstinline$int$ with the value ``zero'', and it \emph{also} is a null pointer constant.
656Similarly,
657``\lstinline$(void *)0$ is an expression of type \lstinline$(void *)$ whose value is a null pointer, and it also is a null pointer constant.
658However, in C, ``\lstinline$(void *)(void *)0$'' is
659\emph{not} a null pointer constant, even though it is null-valued, a pointer, and constant! The semantics of C expressions contain many special cases to deal with subexpressions that are null pointer constants.
660
661\CFA handles these cases through overload resolution.
662The declaration
663\begin{lstlisting}
664forall( dtype DT ) DT * const 0;
665\end{lstlisting} means that \lstinline$0$ is a polymorphic object, and contains a value that can have \emph{any} pointer-to-object type or pointer-to-incomplete type.
666The only such value is the null pointer.
667Therefore the type \emph{alone} is enough to identify a null pointer.
668Where C defines an operator with a special case for the null pointer constant, \CFA defines predefined functions with a polymorphic object parameter.
669\end{rationale}
670
671
672\subsubsection{Generic selection}
673
674\constraints The best interpretation of the controlling expression shall be unambiguous\index{ambiguous interpretation}, and shall have type compatible with at most one of the types named in its generic association list.
675If a generic selection has no \lstinline$default$ generic association, the best interpretation of its controlling expression shall have type compatible with exactly one of the types named in its generic association list.
676
677\semantics
678A generic selection has the same interpretations as its result expression.
679
680
681\subsection{Postfix operators}
682
683\begin{syntax}
684\lhs{postfix-expression}
685\rhs \nonterm{primary-expression}
686\rhs \nonterm{postfix-expression} \lstinline$[$ \nonterm{expression} \lstinline$]$
687\rhs \nonterm{postfix-expression} \lstinline$($ 
688         \nonterm{argument-expression-list}\opt \lstinline$)$
689\rhs \nonterm{postfix-expression} \lstinline$.$ \nonterm{identifier}
690\rhs \nonterm{postfix-expression} \lstinline$->$ \nonterm{identifier}
691\rhs \nonterm{postfix-expression} \lstinline$++$
692\rhs \nonterm{postfix-expression} \lstinline$--$
693\rhs \lstinline$($ \nonterm{type-name} \lstinline$)$ \lstinline${$ \nonterm{initializer-list} \lstinline$}$
694\rhs \lstinline$($ \nonterm{type-name} \lstinline$)$ \lstinline${$ \nonterm{initializer-list} \lstinline$,$ \lstinline$}$
695\lhs{argument-expression-list}
696\rhs \nonterm{assignment-expression}
697\rhs \nonterm{argument-expression-list} \lstinline$,$
698         \nonterm{assignment-expression}
699\end{syntax}
700
701\rewriterules
702\begin{lstlisting}
703a[b] @\rewrite@ ?[?]( b, a ) // if a has integer type@\use{?[?]}@
704a[b] @\rewrite@ ?[?]( a, b ) // otherwise
705a( @\emph{arguments}@ ) @\rewrite@ ?()( a, @\emph{arguments}@ )@\use{?()}@
706a++ @\rewrite@ ?++(&( a ))@\use{?++}@
707a-- @\rewrite@ ?--(&( a ))@\use{?--}@
708\end{lstlisting}
709
710
711\subsubsection{Array subscripting}
712
713\predefined
714\begin{lstlisting}
715forall( otype T ) lvalue T ?[?]( T *, ptrdiff_t );@\use{ptrdiff_t}@
716forall( otype T ) lvalue _Atomic T ?[?]( _Atomic T *, ptrdiff_t );
717forall( otype T ) lvalue const T ?[?]( const T *, ptrdiff_t );
718forall( otype T ) lvalue restrict T ?[?]( restrict T *, ptrdiff_t );
719forall( otype T ) lvalue volatile T ?[?]( volatile T *, ptrdiff_t );
720forall( otype T ) lvalue _Atomic const T ?[?]( _Atomic const T *, ptrdiff_t );
721forall( otype T ) lvalue _Atomic restrict T ?[?]( _Atomic restrict T *, ptrdiff_t );
722forall( otype T ) lvalue _Atomic volatile T ?[?]( _Atomic volatile T *, ptrdiff_t );
723forall( otype T ) lvalue const restrict T ?[?]( const restrict T *, ptrdiff_t );
724forall( otype T ) lvalue const volatile T ?[?]( const volatile T *, ptrdiff_t );
725forall( otype T ) lvalue restrict volatile T ?[?]( restrict volatile T *, ptrdiff_t );
726forall( otype T ) lvalue _Atomic const restrict T ?[?]( _Atomic const restrict T *, ptrdiff_t );
727forall( otype T ) lvalue _Atomic const volatile T ?[?]( _Atomic const volatile T *, ptrdiff_t );
728forall( otype T ) lvalue _Atomic restrict volatile T ?[?]( _Atomic restrict volatile T *, ptrdiff_t );
729forall( otype T ) lvalue const restrict volatile T ?[?]( const restrict volatile T *, ptrdiff_t );
730forall( otype T ) lvalue _Atomic const restrict volatile T ?[?]( _Atomic const restrict volatile T *, ptrdiff_t );
731\end{lstlisting}
732\semantics
733The interpretations of subscript expressions are the interpretations of the corresponding function call expressions.
734\begin{rationale}
735C defines subscripting as pointer arithmetic in a way that makes \lstinline$a[i]$ and
736\lstinline$i[a]$ equivalent. \CFA provides the equivalence through a rewrite rule to reduce the number of overloadings of \lstinline$?[?]$.
737
738Subscript expressions are rewritten as function calls that pass the first parameter by value.
739This is somewhat unfortunate, since array-like types tend to be large.
740The alternative is to use the rewrite rule ``\lstinline$a[b]$ \rewrite \lstinline$?[?](&(a), b)$''.
741However, C semantics forbid this approach: the \lstinline$a$ in ``\lstinline$a[b]$'' can be an arbitrary pointer value, which does not have an address.
742
743The repetitive form of the predefined identifiers shows up a deficiency\index{deficiencies!pointers
744 to qualified types} of \CFA's type system.
745Type qualifiers are not included in type values, so polymorphic functions that take pointers to arbitrary types often come in one flavor for each possible qualification of the pointed-at type.
746\end{rationale}
747
748
749\subsubsection{Function calls}
750
751\semantics
752A \define{function designator} is an interpretation of an expression that has function type.
753The
754\nonterm{postfix-expression} in a function call may have some interpretations that are function designators and some that are not.
755
756For those interpretations of the \nonterm{postfix-expression} that are not function designators, the expression is rewritten and becomes a call of a function named ``\lstinline$?()$''.
757The valid interpretations of the rewritten expression are determined in the manner described below.
758
759Each combination of function designators and argument interpretations is considered.
760For those interpretations of the \nonterm{postfix-expression} that are \Index{monomorphic function} designators, the combination has a \Index{valid interpretation} if the function designator accepts the number of arguments given, and each argument interpretation matches the corresponding explicit parameter:
761\begin{itemize}
762\item if the argument corresponds to a parameter in the function designator's prototype, the argument interpretation must have the same type as the corresponding parameter, or be implicitly convertible to the parameter's type
763\item if the function designator's type does not include a prototype or if the argument corresponds to
764``\lstinline$...$'' in a prototype, a \Index{default argument promotion} is applied to it.
765\end{itemize}
766The type of the valid interpretation is the return type of the function designator.
767
768For those combinations where the interpretation of the \nonterm{postfix-expression} is a
769\Index{polymorphic function} designator and the function designator accepts the number of arguments given, there shall be at least one set of \define{implicit argument}s for the implicit parameters such that
770\begin{itemize}
771\item
772If the declaration of the implicit parameter uses \Index{type-class} \lstinline$type$\use{type}, the implicit argument must be an object type;
773if it uses \lstinline$dtype$, the implicit argument must be an object type or an incomplete type;
774and if it uses \lstinline$ftype$, the implicit argument must be a function type.
775
776\item if an explicit parameter's type uses any implicit parameters, then the corresponding explicit argument must have a type that is (or can be safely converted\index{safe conversion} to) the type produced by substituting the implicit arguments for the implicit parameters in the explicit parameter type.
777
778\item the remaining explicit arguments must match the remaining explicit parameters, as described for monomorphic function designators.
779
780\item for each \Index{assertion parameter} in the function designator's type, there must be an object or function with the same identifier that is visible at the call site and whose type is compatible with or can be specialized to the type of the assertion declaration.
781\end{itemize}
782There is a valid interpretation for each such set of implicit parameters.
783The type of each valid interpretation is the return type of the function designator with implicit parameter values substituted for the implicit arguments.
784
785A valid interpretation is ambiguous\index{ambiguous interpretation} if the function designator or any of the argument interpretations is ambiguous.
786
787Every valid interpretation whose return type is not compatible with any other valid interpretation's return type is an interpretation of the function call expression.
788
789Every set of valid interpretations that have mutually compatible\index{compatible type} result types also produces an interpretation of the function call expression.
790The type of the interpretation is the \Index{composite type} of the types of the valid interpretations, and the value of the interpretation is that of the \Index{best valid interpretation}.
791\begin{rationale}
792One desirable property of a polymorphic programming language is \define{generalizability}: the ability to replace an abstraction with a more general but equivalent abstraction without requiring changes in any of the uses of the original\cite{Cormack90}.
793For instance, it should be possible to replace a function ``\lstinline$int f( int );$'' with ``\lstinline$forall( otype T ) T f( T );$'' without affecting any calls of \lstinline$f$.
794
795\CFA\index{deficiencies!generalizability} does not fully possess this property, because
796\Index{unsafe conversion} are not done when arguments are passed to polymorphic parameters.
797Consider
798\begin{lstlisting}
799float g( float, float );
800int i;
801float f;
802double d;
803f = g( f, f );          // (1)
804f = g( i, f );          // (2) (safe conversion to float)
805f = g( d, f );          // (3) (unsafe conversion to float)
806\end{lstlisting}
807If \lstinline$g$ was replaced by ``\lstinline$forall( otype T ) T g( T, T );$'', the first and second calls would be unaffected, but the third would change: \lstinline$f$ would be converted to
808\lstinline$double$, and the result would be a \lstinline$double$.
809
810Another example is the function ``\lstinline$void h( int *);$''.
811This function can be passed a
812\lstinline$void *$ argument, but the generalization ``\lstinline$forall( otype T ) void h( T *);$'' can not.
813In this case, \lstinline$void$ is not a valid value for \lstinline$T$ because it is not an object type.
814If unsafe conversions were allowed, \lstinline$T$ could be inferred to be \emph{any} object type, which is undesirable.
815\end{rationale}
816
817\examples
818A function called ``\lstinline$?()$'' might be part of a numerical differentiation package.
819\begin{lstlisting}
820extern otype Derivative;
821extern double ?()( Derivative, double );
822extern Derivative derivative_of( double (*f)( double ) );
823extern double sin( double );
824
825Derivative sin_dx = derivative_of( sin );
826double d;
827d = sin_dx( 12.9 );
828\end{lstlisting}
829Here, the only interpretation of \lstinline$sin_dx$ is as an object of type \lstinline$Derivative$.
830For that interpretation, the function call is treated as ``\lstinline$?()( sin_dx, 12.9 )$''.
831\begin{lstlisting}
832int f( long );          // (1)
833int f( int, int );      // (2)
834int f( int *);          // (3)
835int i = f( 5 );         // calls (1)
836\end{lstlisting}
837Function (1) provides a valid interpretation of ``\lstinline$f( 5 )$'', using an implicit \lstinline$int$ to \lstinline$long$ conversion.
838The other functions do not, since the second requires two arguments, and since there is no implicit conversion from \lstinline$int$ to \lstinline$int *$ that could be used with the third function.
839
840\begin{lstlisting}
841forall( otype T ) T h( T );
842double d = h( 1.5 );
843\end{lstlisting}
844``\lstinline$1.5$'' is a \lstinline$double$ constant, so \lstinline$T$ is inferred to be
845\lstinline$double$, and the result of the function call is a \lstinline$double$.
846
847\begin{lstlisting}
848forall( otype T, otype U ) void g( T, U );      // (4)
849forall( otype T ) void g( T, T );                       // (5)
850forall( otype T ) void g( T, long );                    // (6)
851void g( long, long );                                           // (7)
852double d;
853int i;
854int *p;
855g( d, d );                      // calls (5)
856g( d, i );                      // calls (6)
857g( i, i );                      // calls (7)
858g( i, p );                      // calls (4)
859\end{lstlisting}
860The first call has valid interpretations for all four versions of \lstinline$g$. (6) and (7) are discarded because they involve unsafe \lstinline$double$-to-\lstinline$long$ conversions. (5) is chosen because it is less polymorphic than (4).
861
862For the second call, (7) is again discarded.
863Of the remaining interpretations for (4), (5), and (6) (with \lstinline$i$ converted to \lstinline$long$), (6) is chosen because it is the least polymorphic.
864
865The third call has valid interpretations for all of the functions;
866(7) is chosen since it is not polymorphic at all.
867
868The fourth call has no interpretation for (5), because its arguments must have compatible type. (4) is chosen because it does not involve unsafe conversions.
869\begin{lstlisting}
870forall( otype T ) T min( T, T );
871double max( double, double );
872trait min_max( T ) {@\impl{min_max}@
873        T min( T, T );
874        T max( T, T );
875}
876forall( otype U | min_max( U ) ) void shuffle( U, U );
877shuffle( 9, 10 );
878\end{lstlisting}
879The only possibility for \lstinline$U$ is \lstinline$double$, because that is the type used in the only visible \lstinline$max$ function. 9 and 10 must be converted to \lstinline$double$, and
880\lstinline$min$ must be specialized with \lstinline$T$ bound to \lstinline$double$.
881\begin{lstlisting}
882extern void q( int );           // (8)
883extern void q( void * );        // (9)
884extern void r();
885q( 0 );
886r( 0 );
887\end{lstlisting}
888The \lstinline$int 0$ could be passed to (8), or the \lstinline$(void *)$ \Index{specialization} of the null pointer\index{null pointer} \lstinline$0$\use{0} could be passed to (9).
889The former is chosen because the \lstinline$int$ \lstinline$0$ is \Index{less polymorphic}.
890For the same reason, \lstinline$int$ \lstinline$0$ is passed to \lstinline$r()$, even though it has \emph{no} declared parameter types.
891
892
893\subsubsection{Structure and union members}
894
895\semantics In the member selection expression ``\lstinline$s$.\lstinline$m$'', there shall be at least one interpretation of \lstinline$s$ whose type is a structure type or union type containing a member named \lstinline$m$.
896If two or more interpretations of \lstinline$s$ have members named
897\lstinline$m$ with mutually compatible types, then the expression has an \Index{ambiguous interpretation} whose type is the composite type of the types of the members.
898If an interpretation of \lstinline$s$ has a member \lstinline$m$ whose type is not compatible with any other
899\lstinline$s$'s \lstinline$m$, then the expression has an interpretation with the member's type.
900The expression has no other interpretations.
901
902The expression ``\lstinline$p->m$'' has the same interpretations as the expression
903``\lstinline$(*p).m$''.
904
905
906\subsubsection{Postfix increment and decrement operators}
907
908\predefined
909\begin{lstlisting}
910_Bool ?++( volatile _Bool * ), ?++( _Atomic volatile _Bool * );
911char ?++( volatile char * ), ?++( _Atomic volatile char * );
912signed char ?++( volatile signed char * ), ?++( _Atomic volatile signed char * );
913unsigned char ?++( volatile signed char * ), ?++( _Atomic volatile signed char * );
914short int ?++( volatile short int * ), ?++( _Atomic volatile short int * );
915unsigned short int ?++( volatile unsigned short int * ), ?++( _Atomic volatile unsigned short int * );
916int ?++( volatile int * ), ?++( _Atomic volatile int * );
917unsigned int ?++( volatile unsigned int * ), ?++( _Atomic volatile unsigned int * );
918long int ?++( volatile long int * ), ?++( _Atomic volatile long int * );
919long unsigned int ?++( volatile long unsigned int * ), ?++( _Atomic volatile long unsigned int * );
920long long int ?++( volatile long long int * ), ?++( _Atomic volatile long long int * );
921long long unsigned ?++( volatile long long unsigned int * ), ?++( _Atomic volatile long long unsigned int * );
922float ?++( volatile float * ), ?++( _Atomic volatile float * );
923double ?++( volatile double * ), ?++( _Atomic volatile double * );
924long double ?++( volatile long double * ), ?++( _Atomic volatile long double * );
925
926forall( otype T ) T * ?++( T * restrict volatile * ), * ?++( T * _Atomic restrict volatile * );
927forall( otype T ) _Atomic T * ?++( _Atomic T * restrict volatile * ), * ?++( _Atomic T * _Atomic restrict volatile * );
928forall( otype T ) const T * ?++( const T * restrict volatile * ), * ?++( const T * _Atomic restrict volatile * );
929forall( otype T ) volatile T * ?++( volatile T * restrict volatile * ), * ?++( volatile T * _Atomic restrict volatile * );
930forall( otype T ) restrict T * ?++( restrict T * restrict volatile * ), * ?++( restrict T * _Atomic restrict volatile * );
931forall( otype T ) _Atomic const T * ?++( _Atomic const T * restrict volatile * ),
932        * ?++( _Atomic const T * _Atomic restrict volatile * );
933forall( otype T ) _Atomic restrict T * ?++( _Atomic restrict T * restrict volatile * ),
934        * ?++( _Atomic restrict T * _Atomic restrict volatile * );
935forall( otype T ) _Atomic volatile T * ?++( _Atomic volatile T * restrict volatile * ),
936        * ?++( _Atomic volatile T * _Atomic restrict volatile * );
937forall( otype T ) const restrict T * ?++( const restrict T * restrict volatile * ),
938        * ?++( const restrict T * _Atomic restrict volatile * );
939forall( otype T ) const volatile T * ?++( const volatile T * restrict volatile * ),
940        * ?++( const volatile T * _Atomic restrict volatile * );
941forall( otype T ) restrict volatile T * ?++( restrict volatile T * restrict volatile * ),
942        * ?++( restrict volatile T * _Atomic restrict volatile * );
943forall( otype T ) _Atomic const restrict T * ?++( _Atomic const restrict T * restrict volatile * ),
944        * ?++( _Atomic const restrict T * _Atomic restrict volatile * );
945forall( otype T ) _Atomic const volatile T * ?++( _Atomic const volatile T * restrict volatile * ),
946        * ?++( _Atomic const volatile T * _Atomic restrict volatile * );
947forall( otype T ) _Atomic restrict volatile T * ?++( _Atomic restrict volatile T * restrict volatile * ),
948        * ?++( _Atomic restrict volatile T * _Atomic restrict volatile * );
949forall( otype T ) const restrict volatile T * ?++( const restrict volatile T * restrict volatile * ),
950        * ?++( const restrict volatile T * _Atomic restrict volatile * );
951forall( otype T ) _Atomic const restrict volatile T * ?++( _Atomic const restrict volatile T * restrict volatile * ),
952        * ?++( _Atomic const restrict volatile T * _Atomic restrict volatile * );
953
954_Bool ?--( volatile _Bool * ), ?--( _Atomic volatile _Bool * );
955char ?--( volatile char * ), ?--( _Atomic volatile char * );
956signed char ?--( volatile signed char * ), ?--( _Atomic volatile signed char * );
957unsigned char ?--( volatile signed char * ), ?--( _Atomic volatile signed char * );
958short int ?--( volatile short int * ), ?--( _Atomic volatile short int * );
959unsigned short int ?--( volatile unsigned short int * ), ?--( _Atomic volatile unsigned short int * );
960int ?--( volatile int * ), ?--( _Atomic volatile int * );
961unsigned int ?--( volatile unsigned int * ), ?--( _Atomic volatile unsigned int * );
962long int ?--( volatile long int * ), ?--( _Atomic volatile long int * );
963long unsigned int ?--( volatile long unsigned int * ), ?--( _Atomic volatile long unsigned int * );
964long long int ?--( volatile long long int * ), ?--( _Atomic volatile long long int * );
965long long unsigned ?--( volatile long long unsigned int * ), ?--( _Atomic volatile long long unsigned int * );
966float ?--( volatile float * ), ?--( _Atomic volatile float * );
967double ?--( volatile double * ), ?--( _Atomic volatile double * );
968long double ?--( volatile long double * ), ?--( _Atomic volatile long double * );
969
970forall( otype T ) T * ?--( T * restrict volatile * ), * ?--( T * _Atomic restrict volatile * );
971forall( otype T ) _Atomic T * ?--( _Atomic T * restrict volatile * ), * ?--( _Atomic T * _Atomic restrict volatile * );
972forall( otype T ) const T * ?--( const T * restrict volatile * ), * ?--( const T * _Atomic restrict volatile * );
973forall( otype T ) volatile T * ?--( volatile T * restrict volatile * ), * ?--( volatile T * _Atomic restrict volatile * );
974forall( otype T ) restrict T * ?--( restrict T * restrict volatile * ), * ?--( restrict T * _Atomic restrict volatile * );
975forall( otype T ) _Atomic const T * ?--( _Atomic const T * restrict volatile * ),
976        * ?--( _Atomic const T * _Atomic restrict volatile * );
977forall( otype T ) _Atomic restrict T * ?--( _Atomic restrict T * restrict volatile * ),
978        * ?--( _Atomic restrict T * _Atomic restrict volatile * );
979forall( otype T ) _Atomic volatile T * ?--( _Atomic volatile T * restrict volatile * ),
980        * ?--( _Atomic volatile T * _Atomic restrict volatile * );
981forall( otype T ) const restrict T * ?--( const restrict T * restrict volatile * ),
982        * ?--( const restrict T * _Atomic restrict volatile * );
983forall( otype T ) const volatile T * ?--( const volatile T * restrict volatile * ),
984        * ?--( const volatile T * _Atomic restrict volatile * );
985forall( otype T ) restrict volatile T * ?--( restrict volatile T * restrict volatile * ),
986        * ?--( restrict volatile T * _Atomic restrict volatile * );
987forall( otype T ) _Atomic const restrict T * ?--( _Atomic const restrict T * restrict volatile * ),
988        * ?--( _Atomic const restrict T * _Atomic restrict volatile * );
989forall( otype T ) _Atomic const volatile T * ?--( _Atomic const volatile T * restrict volatile * ),
990        * ?--( _Atomic const volatile T * _Atomic restrict volatile * );
991forall( otype T ) _Atomic restrict volatile T * ?--( _Atomic restrict volatile T * restrict volatile * ),
992        * ?--( _Atomic restrict volatile T * _Atomic restrict volatile * );
993forall( otype T ) const restrict volatile T * ?--( const restrict volatile T * restrict volatile * ),
994        * ?--( const restrict volatile T * _Atomic restrict volatile * );
995forall( otype T ) _Atomic const restrict volatile T * ?--( _Atomic const restrict volatile T * restrict volatile * ),
996        * ?--( _Atomic const restrict volatile T * _Atomic restrict volatile * );
997\end{lstlisting}
998For every extended integer type \lstinline$X$ there exist
999% Don't use predefined: keep this out of prelude.cf.
1000\begin{lstlisting}
1001X ?++( volatile X * ), ?++( _Atomic volatile X * ),
1002  ?--( volatile X * ), ?--( _Atomic volatile X * );
1003\end{lstlisting}
1004For every complete enumerated type \lstinline$E$ there exist
1005% Don't use predefined: keep this out of prelude.cf.
1006\begin{lstlisting}
1007E ?++( volatile E * ), ?++( _Atomic volatile E * ),
1008  ?--( volatile E * ), ?--( _Atomic volatile E * );
1009\end{lstlisting}
1010
1011\begin{rationale}
1012Note that ``\lstinline$++$'' and ``\lstinline$--$'' are rewritten as function calls that are given a pointer to that operand. (This is true of all operators that modify an operand.) As Hamish Macdonald has pointed out, this forces the modified operand of such expressions to be an lvalue.
1013This partially enforces the C semantic rule that such operands must be \emph{modifiable} lvalues.
1014\end{rationale}
1015
1016\begin{rationale}
1017In C, a semantic rule requires that pointer operands of increment and decrement be pointers to object types.
1018Hence, \lstinline$void *$ objects cannot be incremented.
1019In \CFA, the restriction follows from the use of a \lstinline$type$ parameter in the predefined function definitions, as opposed to \lstinline$dtype$, since only object types can be inferred arguments corresponding to the type parameter \lstinline$T$.
1020\end{rationale}
1021
1022\semantics
1023First, each interpretation of the operand of an increment or decrement expression is considered separately.
1024For each interpretation that is a bit-field or is declared with the
1025\lstinline$register$\index{register@{\lstinline$register$}} \index{Itorage-class specifier}, the expression has one valid interpretation, with the type of the operand, and the expression is ambiguous if the operand is.
1026
1027For the remaining interpretations, the expression is rewritten, and the interpretations of the expression are the interpretations of the corresponding function call.
1028Finally, all interpretations of the expression produced for the different interpretations of the operand are combined to produce the interpretations of the expression as a whole; where interpretations have compatible result types, the best interpretations are selected in the manner described for function call expressions.
1029
1030\examples
1031\begin{lstlisting}
1032volatile short int vs;  vs++; // rewritten as ?++( &(vs) )
1033short int s;                    s++;
1034const short int cs;             cs++;
1035_Atomic short int as;   as++;
1036\end{lstlisting}
1037\begin{sloppypar}
1038Since \lstinline$&(vs)$ has type \lstinline$volatile short int *$, the best valid interpretation of
1039\lstinline$vs++$ calls the \lstinline$?++$ function with the \lstinline$volatile short *$ parameter.
1040\lstinline$s++$ does the same, applying the safe conversion from \lstinline$short int *$ to
1041\lstinline$volatile short int *$.
1042Note that there is no conversion that adds an \lstinline$_Atomic$ qualifier, so the \lstinline$_Atomic volatile short int$ overloading does not provide a valid interpretation.
1043\end{sloppypar}
1044
1045There is no safe conversion from \lstinline$const short int *$ to \lstinline$volatile short int *$, and no \lstinline$?++$ function that accepts a \lstinline$const *$ parameter, so \lstinline$cs++$ has no valid interpretations.
1046
1047The best valid interpretation of \lstinline$as++$ calls the \lstinline$short ?++$ function with the \lstinline$_Atomic volatile short int *$ parameter, applying a safe conversion to add the \lstinline$volatile$ qualifier.
1048\begin{lstlisting}
1049char * const restrict volatile * restrict volatile pqpc;
1050pqpc++
1051char * * restrict volatile ppc;
1052ppc++;
1053\end{lstlisting}
1054Since \lstinline$&(pqpc)$ has type \lstinline$char * const restrict volatile * restrict volatile *$, the best valid interpretation of \lstinline$pqpc++$ calls the polymorphic \lstinline$?++$ function with the \lstinline$const restrict volatile T * restrict volatile *$ parameter, inferring \lstinline$T$ to be \lstinline$char *$.
1055
1056\lstinline$ppc++$ calls the same function, again inferring \lstinline$T$ to be \lstinline$char *$, and using the safe conversions from \lstinline$T$ to \lstinline$T const$ \lstinline$restrict volatile$.
1057
1058\begin{rationale}
1059Increment and decrement expressions show up a deficiency of \CFA's type system.
1060There is no such thing as a pointer to a register object or bit-field\index{deficiencies!pointers to bit-fields}.
1061Therefore, there is no way to define a function that alters them, and hence no way to define increment and decrement functions for them.
1062As a result, the semantics of increment and decrement expressions must treat them specially.
1063This holds true for all of the operators that may modify such objects.
1064\end{rationale}
1065
1066\begin{rationale}
1067The polymorphic overloadings for pointer increment and decrement can be understood by considering increasingly complex types.
1068\begin{enumerate}
1069\item
1070``\lstinline$char * p; p++;$''.
1071The argument to \lstinline$?++$ has type \lstinline$char * *$, and the result has type \lstinline$char *$.
1072The expression would be valid if \lstinline$?++$ were declared by
1073\begin{lstlisting}
1074forall( otype T ) T * ?++( T * * );
1075\end{lstlisting} with \lstinline$T$ inferred to be \lstinline$char$.
1076
1077\item
1078``\lstinline$char *restrict volatile qp; qp++$''.
1079The result again has type \lstinline$char *$, but the argument now has type \lstinline$char *restrict volatile *$, so it cannot be passed to the hypothetical function declared in point 1.
1080Hence the actual predefined function is
1081\begin{lstlisting}
1082forall( otype T ) T * ?++( T * restrict volatile * );
1083\end{lstlisting} which also accepts a \lstinline$char * *$ argument, because of the safe conversions that add
1084\lstinline$volatile$ and \lstinline$restrict$ qualifiers. (The parameter is not const-qualified, so constant pointers cannot be incremented.)
1085
1086\item
1087``\lstinline$char *_Atomic ap; ap++$''.
1088The result again has type \lstinline$char *$, but no safe conversion adds an \lstinline$_Atomic$ qualifier, so the function in point 2 is not applicable.
1089A separate overloading of \lstinline$?++$ is required.
1090
1091\item
1092``\lstinline$char const volatile * pq; pq++$''.
1093Here the result has type
1094\lstinline$char const volatile *$, so a new overloading is needed:
1095\begin{lstlisting}
1096forall( otype T ) T const volatile * ?++( T const volatile *restrict volatile * );
1097\end{lstlisting}
1098One overloading is needed for each combination of qualifiers in the pointed-at type\index{deficiencies!pointers to qualified types}.
1099 
1100\item
1101``\lstinline$float *restrict * prp; prp++$''.
1102The \lstinline$restrict$ qualifier is handled just like \lstinline$const$ and \lstinline$volatile$ in the previous case:
1103\begin{lstlisting}
1104forall( otype T ) T restrict * ?++( T restrict *restrict volatile * );
1105\end{lstlisting} with \lstinline$T$ inferred to be \lstinline$float *$.
1106This looks odd, because {\c11} contains a constraint that requires restrict-qualified types to be pointer-to-object types, and \lstinline$T$ is not syntactically a pointer type. \CFA loosens the constraint.
1107\end{enumerate}
1108\end{rationale}
1109
1110
1111\subsubsection{Compound literals}
1112
1113\semantics 
1114A compound literal has one interpretation, with the type given by the \nonterm{type-name} of the compound literal.
1115
1116
1117\subsection{Unary operators}
1118
1119\begin{syntax}
1120\lhs{unary-expression}
1121\rhs \nonterm{postfix-expression}
1122\rhs \lstinline$++$ \nonterm{unary-expression}
1123\rhs \lstinline$--$ \nonterm{unary-expression}
1124\rhs \nonterm{unary-operator} \nonterm{cast-expression}
1125\rhs \lstinline$sizeof$ \nonterm{unary-expression}
1126\rhs \lstinline$sizeof$ \lstinline$($ \nonterm{type-name} \lstinline$)$
1127\lhs{unary-operator} one of \rhs \lstinline$&$ \lstinline$*$ \lstinline$+$ \lstinline$-$ \lstinline$~$ \lstinline$!$
1128\end{syntax}
1129
1130\rewriterules
1131\begin{lstlisting}
1132*a      @\rewrite@ *?( a ) @\use{*?}@
1133+a      @\rewrite@ +?( a ) @\use{+?}@
1134-a      @\rewrite@ -?( a ) @\use{-?}@
1135~a      @\rewrite@ ~?( a ) @\use{~?}@
1136!a      @\rewrite@ !?( a ) @\use{"!?}@
1137++a     @\rewrite@ ++?(&( a )) @\use{++?}@
1138--a     @\rewrite@ --?(&( a )) @\use{--?}@
1139\end{lstlisting}
1140
1141
1142\subsubsection{Prefix increment and decrement operators}
1143
1144\predefined
1145\begin{lstlisting}
1146_Bool ++?( volatile _Bool * ), ++?( _Atomic volatile _Bool * );
1147char ++?( volatile char * ), ++?( _Atomic volatile char * );
1148signed char ++?( volatile signed char * ), ++?( _Atomic volatile signed char * );
1149unsigned char ++?( volatile signed char * ), ++?( _Atomic volatile signed char * );
1150short int ++?( volatile short int * ), ++?( _Atomic volatile short int * );
1151unsigned short int ++?( volatile unsigned short int * ), ++?( _Atomic volatile unsigned short int * );
1152int ++?( volatile int * ), ++?( _Atomic volatile int * );
1153unsigned int ++?( volatile unsigned int * ), ++?( _Atomic volatile unsigned int * );
1154long int ++?( volatile long int * ), ++?( _Atomic volatile long int * );
1155long unsigned int ++?( volatile long unsigned int * ), ++?( _Atomic volatile long unsigned int * );
1156long long int ++?( volatile long long int * ), ++?( _Atomic volatile long long int * );
1157long long unsigned ++?( volatile long long unsigned int * ), ++?( _Atomic volatile long long unsigned int * );
1158float ++?( volatile float * ), ++?( _Atomic volatile float * );
1159double ++?( volatile double * ), ++?( _Atomic volatile double * );
1160long double ++?( volatile long double * ), ++?( _Atomic volatile long double * );
1161
1162forall( otype T ) T * ++?( T * restrict volatile * ), * ++?( T * _Atomic restrict volatile * );
1163forall( otype T ) _Atomic T * ++?( _Atomic T * restrict volatile * ), * ++?( _Atomic T * _Atomic restrict volatile * );
1164forall( otype T ) const T * ++?( const T * restrict volatile * ), * ++?( const T * _Atomic restrict volatile * );
1165forall( otype T ) volatile T * ++?( volatile T * restrict volatile * ), * ++?( volatile T * _Atomic restrict volatile * );
1166forall( otype T ) restrict T * ++?( restrict T * restrict volatile * ), * ++?( restrict T * _Atomic restrict volatile * );
1167forall( otype T ) _Atomic const T * ++?( _Atomic const T * restrict volatile * ),
1168        * ++?( _Atomic const T * _Atomic restrict volatile * );
1169forall( otype T ) _Atomic volatile T * ++?( _Atomic volatile T * restrict volatile * ),
1170        * ++?( _Atomic volatile T * _Atomic restrict volatile * );
1171forall( otype T ) _Atomic restrict T * ++?( _Atomic restrict T * restrict volatile * ),
1172        * ++?( _Atomic restrict T * _Atomic restrict volatile * );
1173forall( otype T ) const volatile T * ++?( const volatile T * restrict volatile * ),
1174        * ++?( const volatile T * _Atomic restrict volatile * );
1175forall( otype T ) const restrict T * ++?( const restrict T * restrict volatile * ),
1176        * ++?( const restrict T * _Atomic restrict volatile * );
1177forall( otype T ) restrict volatile T * ++?( restrict volatile T * restrict volatile * ),
1178        * ++?( restrict volatile T * _Atomic restrict volatile * );
1179forall( otype T ) _Atomic const volatile T * ++?( _Atomic const volatile T * restrict volatile * ),
1180        * ++?( _Atomic const volatile T * _Atomic restrict volatile * );
1181forall( otype T ) _Atomic const restrict T * ++?( _Atomic const restrict T * restrict volatile * ),
1182        * ++?( _Atomic const restrict T * _Atomic restrict volatile * );
1183forall( otype T ) _Atomic restrict volatile T * ++?( _Atomic restrict volatile T * restrict volatile * ),
1184        * ++?( _Atomic restrict volatile T * _Atomic restrict volatile * );
1185forall( otype T ) const restrict volatile T * ++?( const restrict volatile T * restrict volatile * ),
1186        * ++?( const restrict volatile T * _Atomic restrict volatile * );
1187forall( otype T ) _Atomic const restrict volatile T * ++?( _Atomic const restrict volatile T * restrict volatile * ),
1188        * ++?( _Atomic const restrict volatile T * _Atomic restrict volatile * );
1189
1190_Bool --?( volatile _Bool * ), --?( _Atomic volatile _Bool * );
1191char --?( volatile char * ), --?( _Atomic volatile char * );
1192signed char --?( volatile signed char * ), --?( _Atomic volatile signed char * );
1193unsigned char --?( volatile signed char * ), --?( _Atomic volatile signed char * );
1194short int --?( volatile short int * ), --?( _Atomic volatile short int * );
1195unsigned short int --?( volatile unsigned short int * ), --?( _Atomic volatile unsigned short int * );
1196int --?( volatile int * ), --?( _Atomic volatile int * );
1197unsigned int --?( volatile unsigned int * ), --?( _Atomic volatile unsigned int * );
1198long int --?( volatile long int * ), --?( _Atomic volatile long int * );
1199long unsigned int --?( volatile long unsigned int * ), --?( _Atomic volatile long unsigned int * );
1200long long int --?( volatile long long int * ), --?( _Atomic volatile long long int * );
1201long long unsigned --?( volatile long long unsigned int * ), --?( _Atomic volatile long long unsigned int * );
1202float --?( volatile float * ), --?( _Atomic volatile float * );
1203double --?( volatile double * ), --?( _Atomic volatile double * );
1204long double --?( volatile long double * ), --?( _Atomic volatile long double * );
1205
1206forall( otype T ) T * --?( T * restrict volatile * ), * --?( T * _Atomic restrict volatile * );
1207forall( otype T ) _Atomic T * --?( _Atomic T * restrict volatile * ), * --?( _Atomic T * _Atomic restrict volatile * );
1208forall( otype T ) const T * --?( const T * restrict volatile * ), * --?( const T * _Atomic restrict volatile * );
1209forall( otype T ) volatile T * --?( volatile T * restrict volatile * ), * --?( volatile T * _Atomic restrict volatile * );
1210forall( otype T ) restrict T * --?( restrict T * restrict volatile * ), * --?( restrict T * _Atomic restrict volatile * );
1211forall( otype T ) _Atomic const T * --?( _Atomic const T * restrict volatile * ),
1212        * --?( _Atomic const T * _Atomic restrict volatile * );
1213forall( otype T ) _Atomic volatile T * --?( _Atomic volatile T * restrict volatile * ),
1214        * --?( _Atomic volatile T * _Atomic restrict volatile * );
1215forall( otype T ) _Atomic restrict T * --?( _Atomic restrict T * restrict volatile * ),
1216        * --?( _Atomic restrict T * _Atomic restrict volatile * );
1217forall( otype T ) const volatile T * --?( const volatile T * restrict volatile * ),
1218        * --?( const volatile T * _Atomic restrict volatile * );
1219forall( otype T ) const restrict T * --?( const restrict T * restrict volatile * ),
1220        * --?( const restrict T * _Atomic restrict volatile * );
1221forall( otype T ) restrict volatile T * --?( restrict volatile T * restrict volatile * ),
1222        * --?( restrict volatile T * _Atomic restrict volatile * );
1223forall( otype T ) _Atomic const volatile T * --?( _Atomic const volatile T * restrict volatile * ),
1224        * --?( _Atomic const volatile T * _Atomic restrict volatile * );
1225forall( otype T ) _Atomic const restrict T * --?( _Atomic const restrict T * restrict volatile * ),
1226        * --?( _Atomic const restrict T * _Atomic restrict volatile * );
1227forall( otype T ) _Atomic restrict volatile T * --?( _Atomic restrict volatile T * restrict volatile * ),
1228        * --?( _Atomic restrict volatile T * _Atomic restrict volatile * );
1229forall( otype T ) const restrict volatile T * --?( const restrict volatile T * restrict volatile * ),
1230        * --?( const restrict volatile T * _Atomic restrict volatile * );
1231forall( otype T ) _Atomic const restrict volatile T * --?( _Atomic const restrict volatile T * restrict volatile * ),
1232        * --?( _Atomic const restrict volatile T * _Atomic restrict volatile * );
1233\end{lstlisting}
1234For every extended integer type \lstinline$X$ there exist
1235% Don't use predefined: keep this out of prelude.cf.
1236\begin{lstlisting}
1237X       ++?( volatile X * ),
1238        ++?( _Atomic volatile X * ),
1239        --?( volatile X * ),
1240        --?( _Atomic volatile X * );
1241\end{lstlisting}
1242For every complete enumerated type \lstinline$E$ there exist
1243% Don't use predefined: keep this out of prelude.cf.
1244\begin{lstlisting}
1245E ++?( volatile E * ),
1246        ++?( _Atomic volatile E * ),
1247        ?--( volatile E * ),
1248        ?--( _Atomic volatile E * );
1249\end{lstlisting}
1250
1251\semantics
1252The interpretations of prefix increment and decrement expressions are determined in the same way as the interpretations of postfix increment and decrement expressions.
1253
1254
1255\subsubsection{Address and indirection operators}
1256
1257\predefined
1258\begin{lstlisting}
1259forall( otype T ) lvalue T *?( T * );
1260forall( otype T ) _Atomic lvalue T *?( _Atomic T * );
1261forall( otype T ) const lvalue T *?( const T * );
1262forall( otype T ) volatile lvalue T *?( volatile T * );
1263forall( otype T ) restrict lvalue T *?( restrict T * );
1264forall( otype T ) _Atomic const lvalue T *?( _Atomic const T * );
1265forall( otype T ) _Atomic volatile lvalue T *?( _Atomic volatile T * );
1266forall( otype T ) _Atomic restrict lvalue T *?( _Atomic restrict T * );
1267forall( otype T ) const volatile lvalue T *?( const volatile T * );
1268forall( otype T ) const restrict lvalue T *?( const restrict T * );
1269forall( otype T ) restrict volatile lvalue T *?( restrict volatile T * );
1270forall( otype T ) _Atomic const volatile lvalue T *?( _Atomic const volatile T * );
1271forall( otype T ) _Atomic const restrict lvalue T *?( _Atomic const restrict T * );
1272forall( otype T ) _Atomic restrict volatile lvalue T *?( _Atomic restrict volatile T * );
1273forall( otype T ) const restrict volatile lvalue T *?( const restrict volatile T * );
1274forall( otype T ) _Atomic const restrict volatile lvalue T *?( _Atomic const restrict volatile T * );
1275forall( ftype FT ) FT *?( FT * );
1276\end{lstlisting}
1277
1278\constraints
1279The operand of the unary ``\lstinline$&$'' operator shall have exactly one
1280\Index{interpretation}\index{ambiguous interpretation}, which shall be unambiguous.
1281
1282\semantics
1283The ``\lstinline$&$'' expression has one interpretation which is of type \lstinline$T *$, where
1284\lstinline$T$ is the type of the operand.
1285
1286The interpretations of an indirection expression are the interpretations of the corresponding function call.
1287
1288
1289\subsubsection{Unary arithmetic operators}
1290
1291\predefined
1292\begin{lstlisting}
1293int     +?( int ), -?( int ), ~?( int );
1294unsigned int +?( unsigned int ), -?( unsigned int ), ~?( unsigned int );
1295long int +?( long int ), -?( long int ), ~?( long int );
1296long unsigned int +?( long unsigned int ), -?( long unsigned int ), ~?( long unsigned int );
1297long long int +?( long long int ), -?( long long int ), ~?( long long int );
1298long long unsigned int +?( long long unsigned int ), -?( long long unsigned int ), ~?( long long unsigned int );
1299float +?( float ), -?( float );
1300double +?( double ), -?( double );
1301long double +?( long double ), -?( long double );
1302_Complex float +?( _Complex float ), -?( _Complex float );
1303_Complex double +?( _Complex double ), -?( _Complex double );
1304_Complex long double +?( _Complex long double ), -?( _Complex long double );
1305int !?( int ), !?( unsigned int ), !?( long ), !?( long unsigned int ),
1306        !?( long long int ), !?( long long unsigned int ),
1307        !?( float ), !?( double ), !?( long double ),
1308        !?( _Complex float ), !?( _Complex double ), !?( _Complex long double );
1309forall( dtype DT ) int !?( const restrict volatile DT * );
1310forall( dtype DT ) int !?( _Atomic const restrict volatile DT * );
1311forall( ftype FT ) int !?( FT * );
1312\end{lstlisting}
1313For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1314% Don't use predefined: keep this out of prelude.cf.
1315\begin{lstlisting}
1316X +?( X ), -?( X ), ~?( X );
1317int !?( X );
1318\end{lstlisting}
1319
1320\semantics
1321The interpretations of a unary arithmetic expression are the interpretations of the corresponding function call.
1322
1323\examples
1324\begin{lstlisting}
1325long int li;
1326void eat_double( double );@\use{eat_double}@
1327eat_double(-li ); // @\rewrite@ eat_double( -?( li ) );
1328\end{lstlisting}
1329The valid interpretations of ``\lstinline$-li$'' (assuming no extended integer types exist) are
1330\begin{center}
1331\begin{tabular}{llc} interpretation & result type & expression conversion cost \\
1332\hline
1333\lstinline$-?( (int)li )$                                       & \lstinline$int$                                       & (unsafe) \\
1334\lstinline$-?( (unsigned)li)$                           & \lstinline$unsigned int$                      & (unsafe) \\
1335\lstinline$-?( (long)li)$                                       & \lstinline$long$                                      & 0 \\
1336\lstinline$-?( (long unsigned int)li)$          & \lstinline$long unsigned int$         & 1 \\
1337\lstinline$-?( (long long int)li)$                      & \lstinline$long long int$                     & 2 \\
1338\lstinline$-?( (long long unsigned int)li)$     & \lstinline$long long unsigned int$& 3 \\
1339\lstinline$-?( (float)li)$                                      & \lstinline$float$                                     & 4 \\
1340\lstinline$-?( (double)li)$                                     & \lstinline$double$                            & 5 \\
1341\lstinline$-?( (long double)li)$                        & \lstinline$long double$                       & 6 \\
1342\lstinline$-?( (_Complex float)li)$                     & \lstinline$float$                                     & (unsafe) \\
1343\lstinline$-?( (_Complex double)li)$            & \lstinline$double$                            & (unsafe) \\
1344\lstinline$-?( (_Complex long double)li)$       & \lstinline$long double$                       & (unsafe) \\
1345\end{tabular}
1346\end{center}
1347The valid interpretations of the \lstinline$eat_double$ call, with the cost of the argument conversion and the cost of the entire expression, are
1348\begin{center}
1349\begin{tabular}{lcc} interpretation & argument cost & expression cost \\
1350\hline
1351\lstinline$eat_double( (double)-?( (int)li) )$                                  & 7                     & (unsafe) \\
1352\lstinline$eat_double( (double)-?( (unsigned)li) )$                             & 6                     & (unsafe) \\
1353\lstinline$eat_double( (double)-?(li) )$                                                & 5                     & \(0+5=5\) \\
1354\lstinline$eat_double( (double)-?( (long unsigned int)li) )$    & 4                     & \(1+4=5\) \\
1355\lstinline$eat_double( (double)-?( (long long int)li) )$                & 3                     & \(2+3=5\) \\
1356\lstinline$eat_double( (double)-?( (long long unsigned int)li) )$& 2            & \(3+2=5\) \\
1357\lstinline$eat_double( (double)-?( (float)li) )$                                & 1                     & \(4+1=5\) \\
1358\lstinline$eat_double( (double)-?( (double)li) )$                               & 0                     & \(5+0=5\) \\
1359\lstinline$eat_double( (double)-?( (long double)li) )$                  & (unsafe)      & (unsafe) \\
1360\lstinline$eat_double( (double)-?( (_Complex float)li) )$               & (unsafe)      & (unsafe) \\
1361\lstinline$eat_double( (double)-?( (_Complex double)li) )$              & (unsafe)      & (unsafe) \\
1362\lstinline$eat_double( (double)-?( (_Complex long double)li) )$ & (unsafe)      & (unsafe) \\
1363\end{tabular}
1364\end{center}
1365Each has result type \lstinline$void$, so the best must be selected.
1366The interpretations involving unsafe conversions are discarded.
1367The remainder have equal expression conversion costs, so the
1368``highest argument conversion cost'' rule is invoked, and the chosen interpretation is
1369\lstinline$eat_double( (double)-?(li) )$.
1370
1371
1372\subsubsection{The \lstinline$sizeof$ and \lstinline$_Alignof$ operators}
1373
1374\constraints
1375The operand of \lstinline$sizeof$ or \lstinline$_Alignof$ shall not be \lstinline$type$,
1376\lstinline$dtype$, or \lstinline$ftype$.
1377
1378When the \lstinline$sizeof$\use{sizeof} operator is applied to an expression, the expression shall have exactly one \Index{interpretation}\index{ambiguous interpretation}, which shall be unambiguous. \semantics A \lstinline$sizeof$ or \lstinline$_Alignof$ expression has one interpretation, of type \lstinline$size_t$.
1379
1380When \lstinline$sizeof$ is applied to an identifier declared by a \nonterm{type-declaration} or a
1381\nonterm{type-parameter}, it yields the size in bytes of the type that implements the operand.
1382When the operand is an opaque type or an inferred type parameter\index{inferred parameter}, the expression is not a constant expression.
1383
1384When \lstinline$_Alignof$ is applied to an identifier declared by a \nonterm{type-declaration} or a
1385\nonterm{type-parameter}, it yields the alignment requirement of the type that implements the operand.
1386When the operand is an opaque type or an inferred type parameter\index{inferred parameter}, the expression is not a constant expression.
1387\begin{rationale}
1388\begin{lstlisting}
1389otype Pair = struct { int first, second; };
1390size_t p_size = sizeof(Pair);           // constant expression
1391extern otype Rational;@\use{Rational}@
1392size_t c_size = sizeof(Rational);       // non-constant expression
1393forall(type T) T f(T p1, T p2) {
1394        size_t t_size = sizeof(T);              // non-constant expression
1395        ...
1396}
1397\end{lstlisting}
1398``\lstinline$sizeof Rational$'', although not statically known, is fixed.
1399Within \lstinline$f()$,
1400``\lstinline$sizeof(T)$'' is fixed for each call of \lstinline$f()$, but may vary from call to call.
1401\end{rationale}
1402
1403
1404\subsection{Cast operators}
1405
1406\begin{syntax}
1407\lhs{cast-expression}
1408\rhs \nonterm{unary-expression}
1409\rhs \lstinline$($ \nonterm{type-name} \lstinline$)$ \nonterm{cast-expression}
1410\end{syntax}
1411
1412\constraints
1413The \nonterm{type-name} in a \nonterm{cast-expression} shall not be \lstinline$type$,
1414\lstinline$dtype$, or \lstinline$ftype$.
1415
1416\semantics
1417
1418In a \Index{cast expression} ``\lstinline$($\nonterm{type-name}\lstinline$)e$'', if
1419\nonterm{type-name} is the type of an interpretation of \lstinline$e$, then that interpretation is the only interpretation of the cast expression;
1420otherwise, \lstinline$e$ shall have some interpretation that can be converted to \nonterm{type-name}, and the interpretation of the cast expression is the cast of the interpretation that can be converted at the lowest cost.
1421The cast expression's interpretation is ambiguous\index{ambiguous interpretation} if more than one interpretation can be converted at the lowest cost or if the selected interpretation is ambiguous.
1422
1423\begin{rationale}
1424Casts can be used to eliminate ambiguity in expressions by selecting interpretations of subexpressions, and to specialize polymorphic functions and values.
1425\end{rationale}
1426
1427
1428\subsection{Multiplicative operators}
1429
1430\begin{syntax}
1431\lhs{multiplicative-expression}
1432\rhs \nonterm{cast-expression}
1433\rhs \nonterm{multiplicative-expression} \lstinline$*$ \nonterm{cast-expression}
1434\rhs \nonterm{multiplicative-expression} \lstinline$/$ \nonterm{cast-expression}
1435\rhs \nonterm{multiplicative-expression} \lstinline$%$ \nonterm{cast-expression}
1436\end{syntax}
1437
1438\rewriterules
1439\begin{lstlisting}
1440a * b @\rewrite@ ?*?( a, b )@\use{?*?}@
1441a / b @\rewrite@ ?/?( a, b )@\use{?/?}@
1442a % b @\rewrite@ ?%?( a, b )@\use{?%?}@
1443\end{lstlisting}
1444
1445\predefined
1446\begin{lstlisting}
1447int?*?( int, int ), ?/?( int, int ), ?%?( int, int );
1448unsigned int?*?( unsigned int, unsigned int ), ?/?( unsigned int, unsigned int ), ?%?( unsigned int, unsigned int );
1449long int?*?( long int, long int ), ?/?( long, long ), ?%?( long, long );
1450long unsigned int?*?( long unsigned int, long unsigned int ),
1451        ?/?( long unsigned int, long unsigned int ), ?%?( long unsigned int, long unsigned int );
1452long long int?*?( long long int, long long int ), ?/?( long long int, long long int ),
1453        ?%?( long long int, long long int );
1454long long unsigned int ?*?( long long unsigned int, long long unsigned int ),
1455        ?/?( long long unsigned int, long long unsigned int ), ?%?( long long unsigned int, long long unsigned int );
1456float?*?( float, float ), ?/?( float, float );
1457double?*?( double, double ), ?/?( double, double );
1458long double?*?( long double, long double ), ?/?( long double, long double );
1459_Complex float?*?( float, _Complex float ), ?/?( float, _Complex float ),
1460        ?*?( _Complex float, float ), ?/?( _Complex float, float ),
1461        ?*?( _Complex float, _Complex float ), ?/?( _Complex float, _Complex float );
1462_Complex double?*?( double, _Complex double ), ?/?( double, _Complex double ),
1463        ?*?( _Complex double, double ), ?/?( _Complex double, double ),
1464        ?*?( _Complex double, _Complex double ), ?/?( _Complex double, _Complex double );
1465_Complex long double?*?( long double, _Complex long double ), ?/?( long double, _Complex long double ),
1466        ?*?( _Complex long double, long double ), ?/?( _Complex long double, long double ),
1467        ?*?( _Complex long double, _Complex long double ), ?/?( _Complex long double, _Complex long double );
1468\end{lstlisting}
1469For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1470% Don't use predefined: keep this out of prelude.cf.
1471\begin{lstlisting}
1472X ?*?( X ), ?/?( X ), ?%?( X );
1473\end{lstlisting}
1474
1475\begin{rationale}
1476{\c11} does not include conversions from the \Index{real type}s to \Index{complex type}s in the
1477\Index{usual arithmetic conversion}s.  Instead it specifies conversion of the result of binary operations on arguments from mixed type domains. \CFA's predefined operators match that pattern.
1478\end{rationale}
1479
1480\semantics
1481The interpretations of multiplicative expressions are the interpretations of the corresponding function call.
1482
1483\examples
1484\begin{lstlisting}
1485int i;
1486long li;
1487void eat_double( double );@\use{eat_double}@
1488eat_double( li % i );
1489\end{lstlisting}
1490``\lstinline$li % i$'' is rewritten as ``\lstinline$?%?(li, i )$''.
1491The valid interpretations of \lstinline$?%?(li, i )$, the cost\index{conversion cost} of converting their arguments, and the cost of converting the result to \lstinline$double$ (assuming no extended integer types are present ) are
1492\begin{center}
1493\begin{tabular}{lcc} interpretation & argument cost & result cost \\
1494\hline 
1495\lstinline$ ?%?( (int)li, i )$                                                                          & (unsafe)      & 6     \\
1496\lstinline$ ?%?( (unsigned)li,(unsigned)i )$                                            & (unsafe)      & 5     \\
1497\lstinline$ ?%?( li, (long)i )$                                                                         & 1                     & 4     \\
1498\lstinline$ ?%?( (long unsigned)li,(long unsigned)i )$                          & 3                     & 3     \\
1499\lstinline$ ?%?( (long long)li,(long long)i )$                                          & 5                     & 2     \\
1500\lstinline$ ?%?( (long long unsigned)li, (long long unsigned)i )$       & 7                     & 1     \\
1501\end{tabular}
1502\end{center}
1503The best interpretation of \lstinline$eat_double( li, i )$ is
1504\lstinline$eat_double( (double)?%?(li, (long)i ))$, which has no unsafe conversions and the lowest total cost.
1505
1506\begin{rationale}
1507{\c11} defines most arithmetic operations to apply an \Index{integer promotion} to any argument that belongs to a type that has an \Index{integer conversion rank} less than that of \lstinline$int$.If
1508\lstinline$s$ is a \lstinline$short int$, ``\lstinline$s *s$'' does not have type \lstinline$short int$;
1509it is treated as ``\lstinline$( (int)s ) * ( (int)s )$'', and has type \lstinline$int$. \CFA matches that pattern;
1510it does not predefine ``\lstinline$short ?*?( short, short )$''.
1511
1512These ``missing'' operators limit polymorphism.
1513Consider
1514\begin{lstlisting}
1515forall( otype T | T ?*?( T, T ) ) T square( T );
1516short s;
1517square( s );
1518\end{lstlisting}
1519Since \CFA does not define a multiplication operator for \lstinline$short int$,
1520\lstinline$square( s )$ is treated as \lstinline$square( (int)s )$, and the result has type
1521\lstinline$int$.
1522This is mildly surprising, but it follows the {\c11} operator pattern.
1523
1524A more troubling example is
1525\begin{lstlisting}
1526forall( otype T | ?*?( T, T ) ) T product( T[], int n );
1527short sa[5];
1528product( sa, 5);
1529\end{lstlisting}
1530This has no valid interpretations, because \CFA has no conversion from ``array of
1531\lstinline$short int$'' to ``array of \lstinline$int$''.
1532The alternatives in such situations include
1533\begin{itemize}
1534\item
1535Defining monomorphic overloadings of \lstinline$product$ for \lstinline$short$ and the other
1536``small'' types.
1537\item
1538Defining ``\lstinline$short ?*?( short, short )$'' within the scope containing the call to
1539\lstinline$product$.
1540\item
1541Defining \lstinline$product$ to take as an argument a conversion function from the ``small'' type to the operator's argument type.
1542\end{itemize}
1543\end{rationale}
1544
1545
1546\subsection{Additive operators}
1547
1548\begin{syntax}
1549\lhs{additive-expression}
1550\rhs \nonterm{multiplicative-expression}
1551\rhs \nonterm{additive-expression} \lstinline$+$ \nonterm{multiplicative-expression}
1552\rhs \nonterm{additive-expression} \lstinline$-$ \nonterm{multiplicative-expression}
1553\end{syntax}
1554
1555\rewriterules
1556\begin{lstlisting}
1557a + b @\rewrite@ ?+?( a, b )@\use{?+?}@
1558a - b @\rewrite@ ?-?( a, b )@\use{?-?}@
1559\end{lstlisting}
1560
1561\predefined
1562\begin{lstlisting}
1563int?+?( int, int ), ?-?( int, int );
1564unsigned int?+?( unsigned int, unsigned int ), ?-?( unsigned int, unsigned int );
1565long int?+?( long int, long int ), ?-?( long int, long int );
1566long unsigned int?+?( long unsigned int, long unsigned int ), ?-?( long unsigned int, long unsigned int );
1567long long int?+?( long long int, long long int ), ?-?( long long int, long long int );
1568long long unsigned int ?+?( long long unsigned int, long long unsigned int ),
1569        ?-?( long long unsigned int, long long unsigned int );
1570float?+?( float, float ), ?-?( float, float );
1571double?+?( double, double ), ?-?( double, double );
1572long double?+?( long double, long double ), ?-?( long double, long double );
1573_Complex float?+?( _Complex float, float ), ?-?( _Complex float, float ),
1574        ?+?( float, _Complex float ), ?-?( float, _Complex float ),
1575        ?+?( _Complex float, _Complex float ), ?-?( _Complex float, _Complex float );
1576_Complex double?+?( _Complex double, double ), ?-?( _Complex double, double ),
1577        ?+?( double, _Complex double ), ?-?( double, _Complex double ),
1578        ?+?( _Complex double, _Complex double ), ?-?( _Complex double, _Complex double );
1579_Complex long double?+?( _Complex long double, long double ), ?-?( _Complex long double, long double ),
1580        ?+?( long double, _Complex long double ), ?-?( long double, _Complex long double ),
1581        ?+?( _Complex long double, _Complex long double ), ?-?( _Complex long double, _Complex long double );
1582
1583forall( otype T ) T * ?+?( T *, ptrdiff_t ), * ?+?( ptrdiff_t, T * ), * ?-?( T *, ptrdiff_t );
1584forall( otype T ) _Atomic T * ?+?( _Atomic T *, ptrdiff_t ), * ?+?( ptrdiff_t, _Atomic T * ),
1585        * ?-?( _Atomic T *, ptrdiff_t );
1586forall( otype T ) const T * ?+?( const T *, ptrdiff_t ), * ?+?( ptrdiff_t, const T * ),
1587        * ?-?( const T *, ptrdiff_t );
1588forall( otype T ) restrict T * ?+?( restrict T *, ptrdiff_t ), * ?+?( ptrdiff_t, restrict T * ),
1589        * ?-?( restrict T *, ptrdiff_t );
1590forall( otype T ) volatile T * ?+?( volatile T *, ptrdiff_t ), * ?+?( ptrdiff_t, volatile T * ),
1591        * ?-?( volatile T *, ptrdiff_t );
1592forall( otype T ) _Atomic const T * ?+?( _Atomic const T *, ptrdiff_t ), * ?+?( ptrdiff_t, _Atomic const T * ),
1593        * ?-?( _Atomic const T *, ptrdiff_t );
1594forall( otype T ) _Atomic restrict T * ?+?( _Atomic restrict T *, ptrdiff_t ), * ?+?( ptrdiff_t, _Atomic restrict T * ),
1595        * ?-?( _Atomic restrict T *, ptrdiff_t );
1596forall( otype T ) _Atomic volatile T * ?+?( _Atomic volatile T *, ptrdiff_t ), * ?+?( ptrdiff_t, _Atomic volatile T * ),
1597        * ?-?( _Atomic volatile T *, ptrdiff_t );
1598forall( otype T ) const restrict T * ?+?( const restrict T *, ptrdiff_t ), * ?+?( ptrdiff_t, const restrict T * ),
1599        * ?-?( const restrict T *, ptrdiff_t );
1600forall( otype T ) const volatile T * ?+?( const volatile T *, ptrdiff_t ), * ?+?( ptrdiff_t, const volatile T * ),
1601        * ?-?( const volatile T *, ptrdiff_t );
1602forall( otype T ) restrict volatile T * ?+?( restrict volatile T *, ptrdiff_t ), * ?+?( ptrdiff_t, restrict volatile T * ),
1603        * ?-?( restrict volatile T *, ptrdiff_t );
1604forall( otype T ) _Atomic const restrict T * ?+?( _Atomic const restrict T *, ptrdiff_t ),
1605        * ?+?( ptrdiff_t, _Atomic const restrict T * ),
1606        * ?-?( _Atomic const restrict T *, ptrdiff_t );
1607forall( otype T ) ptrdiff_t
1608        * ?-?( const restrict volatile T *, const restrict volatile T * ),
1609        * ?-?( _Atomic const restrict volatile T *, _Atomic const restrict volatile T * );
1610\end{lstlisting}
1611For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1612% Don't use predefined: keep this out of prelude.cf.
1613\begin{lstlisting}
1614X ?+?( X ), ?-?( X );
1615\end{lstlisting}
1616
1617\semantics
1618The interpretations of additive expressions are the interpretations of the corresponding function calls.
1619
1620\begin{rationale}
1621\lstinline$ptrdiff_t$ is an implementation-defined identifier defined in \lstinline$<stddef.h>$ that is synonymous with a signed integral type that is large enough to hold the difference between two pointers.
1622It seems reasonable to use it for pointer addition as well. (This is technically a difference between \CFA and C, which only specifies that pointer addition uses an \emph{integral} argument.) Hence it is also used for subscripting, which is defined in terms of pointer addition.
1623The {\c11} standard uses \lstinline$size_t$ in several cases where a library function takes an argument that is used as a subscript, but \lstinline$size_t$ is unsuitable here because it is an unsigned type.
1624\end{rationale}
1625
1626
1627\subsection{Bitwise shift operators}
1628
1629\begin{syntax}
1630\lhs{shift-expression}
1631\rhs \nonterm{additive-expression}
1632\rhs \nonterm{shift-expression} \lstinline$<<$ \nonterm{additive-expression}
1633\rhs \nonterm{shift-expression} \lstinline$>>$ \nonterm{additive-expression}
1634\end{syntax}
1635
1636\rewriterules \use{?>>?}%use{?<<?}
1637\begin{lstlisting}
1638a << b @\rewrite@ ?<<?( a, b )
1639a >> b @\rewrite@ ?>>?( a, b )
1640\end{lstlisting}
1641
1642\predefined
1643\begin{lstlisting}
1644int ?<<?( int, int ), ?>>?( int, int );
1645unsigned int ?<<?( unsigned int, int ), ?>>?( unsigned int, int );
1646long int ?<<?( long int, int ), ?>>?( long int, int );
1647long unsigned int ?<<?( long unsigned int, int ), ?>>?( long unsigned int, int );
1648long long int ?<<?( long long int, int ), ?>>?( long long int, int );
1649long long unsigned int ?<<?( long long unsigned int, int ), ?>>?( long long unsigned int, int);
1650\end{lstlisting}
1651For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1652% Don't use predefined: keep this out of prelude.cf.
1653\begin{lstlisting}
1654X ?<<?( X, int ), ?>>?( X, int );
1655\end{lstlisting}
1656
1657\begin{rationale}
1658The bitwise shift operators break the usual pattern: they do not convert both operands to a common type.
1659The right operand only undergoes \Index{integer promotion}.
1660\end{rationale}
1661
1662\semantics
1663The interpretations of a bitwise shift expression are the interpretations of the corresponding function calls.
1664
1665
1666\subsection{Relational operators}
1667
1668\begin{syntax}
1669\lhs{relational-expression}
1670\rhs \nonterm{shift-expression}
1671\rhs \nonterm{relational-expression} \lstinline$< $ \nonterm{shift-expression}
1672\rhs \nonterm{relational-expression} \lstinline$> $ \nonterm{shift-expression}
1673\rhs \nonterm{relational-expression} \lstinline$<=$ \nonterm{shift-expression}
1674\rhs \nonterm{relational-expression} \lstinline$>=$ \nonterm{shift-expression}
1675\end{syntax}
1676
1677\rewriterules\use{?>?}\use{?>=?}%use{?<?}%use{?<=?}
1678\begin{lstlisting}
1679a < b @\rewrite@ ?<?( a, b )
1680a > b @\rewrite@ ?>?( a, b )
1681a <= b @\rewrite@ ?<=?( a, b )
1682a >= b @\rewrite@ ?>=?( a, b )
1683\end{lstlisting}
1684
1685\predefined
1686\begin{lstlisting}
1687int ?<?( int, int ), ?<=?( int, int ),
1688        ?>?( int, int ), ?>=?( int, int );
1689int ?<?( unsigned int, unsigned int ), ?<=?( unsigned int, unsigned int ),
1690        ?>?( unsigned int, unsigned int ), ?>=?( unsigned int, unsigned int );
1691int ?<?( long int, long int ), ?<=?( long int, long int ),
1692        ?>?( long int, long int ), ?>=?( long int, long int );
1693int ?<?( long unsigned int, long unsigned ), ?<=?( long unsigned int, long unsigned ),
1694        ?>?( long unsigned int, long unsigned ), ?>=?( long unsigned int, long unsigned );
1695int ?<?( long long int, long long int ), ?<=?( long long int, long long int ),
1696        ?>?( long long int, long long int ), ?>=?( long long int, long long int );
1697int ?<?( long long unsigned int, long long unsigned ), ?<=?( long long unsigned int, long long unsigned ),
1698        ?>?( long long unsigned int, long long unsigned ), ?>=?( long long unsigned int, long long unsigned );
1699int ?<?( float, float ), ?<=?( float, float ),
1700        ?>?( float, float ), ?>=?( float, float );
1701int ?<?( double, double ), ?<=?( double, double ),
1702        ?>?( double, double ), ?>=?( double, double );
1703int ?<?( long double, long double ), ?<=?( long double, long double ),
1704        ?>?( long double, long double ), ?>=?( long double, long double );
1705forall( dtype DT ) int ?<?( const restrict volatile DT *, const restrict volatile DT * ),
1706        ?<?( _Atomic const restrict volatile DT *, _Atomic const restrict volatile DT * ),
1707        ?<=?( const restrict volatile DT *, const restrict volatile DT * ),
1708        ?<=?( _Atomic const restrict volatile DT *, _Atomic const restrict volatile DT * ),
1709        ?>?( const restrict volatile DT *, const restrict volatile DT * ),
1710        ?>?( _Atomic const restrict volatile DT *, _Atomic const restrict volatile DT * ),
1711        ?>=?( const restrict volatile DT *, const restrict volatile DT * ),
1712        ?>=?( _Atomic const restrict volatile DT *, _Atomic const restrict volatile DT * );
1713\end{lstlisting}
1714For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1715% Don't use predefined: keep this out of prelude.cf.
1716\begin{lstlisting}
1717int ?<?( X, X ),
1718        ?<=?( X, X ),
1719        ?<?( X, X ),
1720        ?>=?( X, X );
1721\end{lstlisting}
1722
1723\semantics
1724The interpretations of a relational expression are the interpretations of the corresponding function call.
1725
1726
1727\subsection{Equality operators}
1728
1729\begin{syntax}
1730\lhs{equality-expression}
1731\rhs \nonterm{relational-expression}
1732\rhs \nonterm{equality-expression} \lstinline$==$ \nonterm{relational-expression}
1733\rhs \nonterm{equality-expression} \lstinline$!=$ \nonterm{relational-expression}
1734\end{syntax}
1735
1736\rewriterules
1737\begin{lstlisting}
1738a == b @\rewrite@ ?==?( a, b )@\use{?==?}@
1739a != b @\rewrite@ ?!=?( a, b )@\use{?"!=?}@
1740\end{lstlisting}
1741
1742\predefined
1743\begin{lstlisting}
1744int ?==?( int, int ), ?!=?( int, int ),
1745        ?==?( unsigned int, unsigned int ), ?!=?( unsigned int, unsigned int ),
1746        ?==?( long int, long int ), ?!=?( long int, long int ),
1747        ?==?( long unsigned int, long unsigned int ), ?!=?( long unsigned int, long unsigned int ),
1748        ?==?( long long int, long long int ), ?!=?( long long int, long long int ),
1749        ?==?( long long unsigned int, long long unsigned int ), ?!=?( long long unsigned int, long long unsigned int ),
1750        ?==?( float, float ), ?!=?( float, float ),
1751        ?==?( _Complex float, float ), ?!=?( _Complex float, float ),
1752        ?==?( float, _Complex float ), ?!=?( float, _Complex float ),
1753        ?==?( _Complex float, _Complex float ), ?!=?( _Complex float, _Complex float ),
1754        ?==?( double, double ), ?!=?( double, double ),
1755        ?==?( _Complex double, double ), ?!=?( _Complex double, double ),
1756        ?==?( double, _Complex double ), ?!=?( double, _Complex double ),
1757        ?==?( _Complex double, _Complex double ), ?!=?( _Complex double, _Complex double ),
1758        ?==?( long double, long double ), ?!=?( long double, long double ),
1759        ?==?( _Complex long double, long double ), ?!=?( _Complex long double, long double ),
1760        ?==?( long double, _Complex long double ), ?!=?( long double, _Complex long double ),
1761        ?==?( _Complex long double, _Complex long double ), ?!=?( _Complex long double, _Complex long double );
1762forall( dtype DT ) int
1763        ?==?( const restrict volatile DT *, const restrict volatile DT * ),
1764        ?!=?( const restrict volatile DT *, const restrict volatile DT * ),
1765        ?==?( const restrict volatile DT *, const restrict volatile void * ),
1766        ?!=?( const restrict volatile DT *, const restrict volatile void * ),
1767        ?==?( const restrict volatile void *, const restrict volatile DT * ),
1768        ?!=?( const restrict volatile void *, const restrict volatile DT * ),
1769        ?==?( const restrict volatile DT *, forall( dtype DT2) const DT2 * ),
1770        ?!=?( const restrict volatile DT *, forall( dtype DT2) const DT2 * ),
1771        ?==?( forall( dtype DT2) const DT2*, const restrict volatile DT * ),
1772        ?!=?( forall( dtype DT2) const DT2*, const restrict volatile DT * ),
1773        ?==?( forall( dtype DT2) const DT2*, forall( dtype DT3) const DT3 * ),
1774        ?!=?( forall( dtype DT2) const DT2*, forall( dtype DT3) const DT3 * ),
1775
1776        ?==?( _Atomic const restrict volatile DT *, _Atomic const restrict volatile DT * ),
1777        ?!=?( _Atomic const restrict volatile DT *, _Atomic const restrict volatile DT * ),
1778        ?==?( _Atomic const restrict volatile DT *, const restrict volatile void * ),
1779        ?!=?( _Atomic const restrict volatile DT *, const restrict volatile void * ),
1780        ?==?( const restrict volatile void *, _Atomic const restrict volatile DT * ),
1781        ?!=?( const restrict volatile void *, _Atomic const restrict volatile DT * ),
1782        ?==?( _Atomic const restrict volatile DT *, forall( dtype DT2) const DT2 * ),
1783        ?!=?( _Atomic const restrict volatile DT *, forall( dtype DT2) const DT2 * ),
1784        ?==?( forall( dtype DT2) const DT2*, _Atomic const restrict volatile DT * ),
1785        ?!=?( forall( dtype DT2) const DT2*, _Atomic const restrict volatile DT * );
1786forall( ftype FT ) int
1787        ?==?( FT *, FT * ), ?!=?( FT *, FT * ),
1788        ?==?( FT *, forall( ftype FT2) FT2 * ), ?!=?( FT *, forall( ftype FT2) FT2 * ),
1789        ?==?( forall( ftype FT2) FT2*, FT * ), ?!=?( forall( ftype FT2) FT2*, FT * ),
1790        ?==?( forall( ftype FT2) FT2*, forall( ftype FT3) FT3 * ), ?!=?( forall( ftype FT2) FT2*, forall( ftype FT3) FT3 * );
1791\end{lstlisting}
1792For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1793% Don't use predefined: keep this out of prelude.cf.
1794\begin{lstlisting}
1795int ?==?( X, X ),
1796        ?!=?( X, X );
1797\end{lstlisting}
1798
1799\begin{rationale}
1800The polymorphic equality operations come in three styles: comparisons between pointers of compatible types, between pointers to \lstinline$void$ and pointers to object types or incomplete types, and between the \Index{null pointer} constant and pointers to any type.
1801In the last case, a special constraint rule for null pointer constant operands has been replaced by a consequence of the \CFA type system.
1802\end{rationale}
1803
1804\semantics
1805The interpretations of an equality expression are the interpretations of the corresponding function call.
1806
1807\begin{sloppypar}
1808The result of an equality comparison between two pointers to predefined functions or predefined values is implementation-defined.
1809\end{sloppypar}
1810\begin{rationale}
1811The implementation-defined status of equality comparisons allows implementations to use one library routine to implement many predefined functions.
1812These optimization are particularly important when the predefined functions are polymorphic, as is the case for most pointer operations
1813\end{rationale}
1814
1815
1816\subsection{Bitwise AND operator}
1817
1818\begin{syntax}
1819\lhs{AND-expression}
1820\rhs \nonterm{equality-expression}
1821\rhs \nonterm{AND-expression} \lstinline$&$ \nonterm{equality-expression}
1822\end{syntax}
1823
1824\rewriterules
1825\begin{lstlisting}
1826a & b @\rewrite@ ?&?( a, b )@\use{?&?}@
1827\end{lstlisting}
1828
1829\predefined
1830\begin{lstlisting}
1831int ?&?( int, int );
1832unsigned int ?&?( unsigned int, unsigned int );
1833long int ?&?( long int, long int );
1834long unsigned int ?&?( long unsigned int, long unsigned int );
1835long long int ?&?( long long int, long long int );
1836long long unsigned int ?&?( long long unsigned int, long long unsigned int );
1837\end{lstlisting}
1838For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1839% Don't use predefined: keep this out of prelude.cf.
1840\begin{lstlisting}
1841int ?&?( X, X );
1842\end{lstlisting}
1843
1844\semantics
1845The interpretations of a bitwise AND expression are the interpretations of the corresponding function call.
1846
1847
1848\subsection{Bitwise exclusive OR operator}
1849
1850\begin{syntax}
1851\lhs{exclusive-OR-expression}
1852\rhs \nonterm{AND-expression}
1853\rhs \nonterm{exclusive-OR-expression} \lstinline$^$ \nonterm{AND-expression}
1854\end{syntax}
1855
1856\rewriterules
1857\begin{lstlisting}
1858a ^ b @\rewrite@ ?^?( a, b )@\use{?^?}@
1859\end{lstlisting}
1860
1861\predefined
1862\begin{lstlisting}
1863int ?^?( int, int );
1864unsigned int ?^?( unsigned int, unsigned int );
1865long int ?^?( long int, long int );
1866long unsigned int ?^?( long unsigned int, long unsigned int );
1867long long int ?^?( long long int, long long int );
1868long long unsigned int ?^?( long long unsigned int, long long unsigned int );
1869\end{lstlisting}
1870For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1871% Don't use predefined: keep this out of prelude.cf.
1872\begin{lstlisting}
1873int ?^?( X, X );
1874\end{lstlisting}
1875
1876\semantics
1877The interpretations of a bitwise exclusive OR expression are the interpretations of the corresponding function call.
1878
1879
1880\subsection{Bitwise inclusive OR operator}
1881
1882\begin{syntax}
1883\lhs{inclusive-OR-expression}
1884\rhs \nonterm{exclusive-OR-expression}
1885\rhs \nonterm{inclusive-OR-expression} \lstinline$|$ \nonterm{exclusive-OR-expression}
1886\end{syntax}
1887
1888\rewriterules\use{?"|?}
1889\begin{lstlisting}
1890a | b @\rewrite@ ?|?( a, b )
1891\end{lstlisting}
1892
1893\predefined
1894\begin{lstlisting}
1895int ?|?( int, int );
1896unsigned int ?|?( unsigned int, unsigned int );
1897long int ?|?( long int, long int );
1898long unsigned int ?|?( long unsigned int, long unsigned int );
1899long long int ?|?( long long int, long long int );
1900long long unsigned int ?|?( long long unsigned int, long long unsigned int );
1901\end{lstlisting}
1902For every extended integer type \lstinline$X$ with \Index{integer conversion rank} greater than the rank of \lstinline$int$ there exist
1903% Don't use predefined: keep this out of prelude.cf.
1904\begin{lstlisting}
1905int ?|?( X, X );
1906\end{lstlisting}
1907
1908\semantics 
1909The interpretations of a bitwise inclusive OR expression are the interpretations of the corresponding function call.
1910
1911
1912\subsection{Logical AND operator}
1913
1914\begin{syntax}
1915\lhs{logical-AND-expression}
1916\rhs \nonterm{inclusive-OR-expression}
1917\rhs \nonterm{logical-AND-expression} \lstinline$&&$ \nonterm{inclusive-OR-expression}
1918\end{syntax}
1919
1920\semantics The operands of the expression ``\lstinline$a && b$'' are treated as
1921``\lstinline$(int)((a)!=0)$'' and ``\lstinline$(int)((b)!=0)$'', which shall both be unambiguous.
1922The expression has only one interpretation, which is of type \lstinline$int$.
1923\begin{rationale}
1924When the operands of a logical expression are values of built-in types, and ``\lstinline$!=$'' has not been redefined for those types, the compiler can optimize away the function calls.
1925
1926A common C idiom omits comparisons to \lstinline$0$ in the controlling expressions of loops and
1927\lstinline$if$ statements.
1928For instance, the loop below iterates as long as \lstinline$rp$ points at a \lstinline$Rational$ value that is non-zero.
1929
1930\begin{lstlisting}
1931extern otype Rational;@\use{Rational}@
1932extern const Rational 0;@\use{0}@
1933extern int ?!=?( Rational, Rational );
1934Rational *rp;
1935while ( rp && *rp ) { ... }
1936\end{lstlisting}
1937The logical expression calls the \lstinline$Rational$ inequality operator, passing it \lstinline$*rp$ and the \lstinline$Rational 0$, and getting a 1 or 0 as a result.
1938In contrast, {\CC} would apply a programmer-defined \lstinline$Rational$-to-\lstinline$int$ conversion to \lstinline$*rp$ in the equivalent situation.
1939The conversion to \lstinline$int$ would produce a general integer value, which is unfortunate, and possibly dangerous if the conversion was not written with this situation in mind.
1940\end{rationale}
1941
1942
1943\subsection{Logical OR operator}
1944
1945\begin{syntax}
1946\lhs{logical-OR-expression}
1947\rhs \nonterm{logical-AND-expression}
1948\rhs \nonterm{logical-OR-expression} \lstinline$||$ \nonterm{logical-AND-expression}
1949\end{syntax}
1950
1951\semantics
1952
1953The operands of the expression ``\lstinline$a || b$'' are treated as ``\lstinline$(int)((a)!=0)$'' and ``\lstinline$(int)((b))!=0)$'', which shall both be unambiguous.
1954The expression has only one interpretation, which is of type \lstinline$int$.
1955
1956
1957\subsection{Conditional operator}
1958
1959\begin{syntax}
1960\lhs{conditional-expression}
1961\rhs \nonterm{logical-OR-expression}
1962\rhs \nonterm{logical-OR-expression} \lstinline$?$ \nonterm{expression}
1963         \lstinline$:$ \nonterm{conditional-expression}
1964\end{syntax}
1965
1966\semantics
1967In the conditional expression\use{?:} ``\lstinline$a?b:c$'', if the second and third operands both have an interpretation with \lstinline$void$ type, then the expression has an interpretation with type \lstinline$void$, equivalent to
1968\begin{lstlisting}
1969( int)(( a)!=0) ? ( void)( b) : ( void)( c)
1970\end{lstlisting}
1971
1972If the second and third operands both have interpretations with non-\lstinline$void$ types, the expression is treated as if it were the call ``\lstinline$cond((a)!=0, b, c)$'', with \lstinline$cond$ declared as
1973\begin{lstlisting}
1974forall( otype T ) T cond( int, T, T );
1975forall( dtype D ) void * cond( int, D *, void * ), * cond( int, void *, D * );
1976forall( dtype D ) _atomic void * cond(
1977        int, _Atomic D *, _Atomic void * ), * cond( int, _Atomic void *, _Atomic D * );
1978forall( dtype D ) const void * cond(
1979        int, const D *, const void * ), * cond( int, const void *, const D * );
1980forall( dtype D ) restrict void * cond(
1981        int, restrict D *, restrict void * ), * cond( int, restrict void *, restrict D * );
1982forall( dtype D ) volatile void * cond(
1983        int, volatile D *, volatile void * ), * cond( int, volatile void *, volatile D * );
1984forall( dtype D ) _Atomic const void * cond(
1985        int, _Atomic const D *, _Atomic const void * ), * cond( int, _Atomic const void *, _Atomic const D * );
1986forall( dtype D ) _Atomic restrict void * cond(
1987        int, _Atomic restrict D *, _Atomic restrict void * ), * cond( int, _Atomic restrict void *, _Atomic restrict D * );
1988forall( dtype D ) _Atomic volatile void * cond(
1989        int, _Atomic volatile D *, _Atomic volatile void * ), * cond( int, _Atomic volatile void *, _Atomic volatile D * );
1990forall( dtype D ) const restrict void * cond(
1991        int, const restrict D *, const restrict void * ), * cond( int, const restrict void *, const restrict D * );
1992forall( dtype D ) const volatile void * cond(
1993        int, const volatile D *, const volatile void * ), * cond( int, const volatile void *, const volatile D * );
1994forall( dtype D ) restrict volatile void * cond(
1995        int, restrict volatile D *, restrict volatile void * ), * cond( int, restrict volatile void *, restrict volatile D * );
1996forall( dtype D ) _Atomic const restrict void * cond(
1997        int, _Atomic const restrict D *, _Atomic const restrict void * ),
1998        * cond( int, _Atomic const restrict void *, _Atomic const restrict D * );
1999forall( dtype D ) _Atomic const volatile void * cond(
2000        int, _Atomic const volatile D *, _Atomic const volatile void * ),
2001        * cond( int, _Atomic const volatile void *, _Atomic const volatile D * );
2002forall( dtype D ) _Atomic restrict volatile void * cond(
2003        int, _Atomic restrict volatile D *, _Atomic restrict volatile void * ),
2004        * cond( int, _Atomic restrict volatile void *, _Atomic restrict volatile D * );
2005forall( dtype D ) const restrict volatile void * cond(
2006        int, const restrict volatile D *, const restrict volatile void * ),
2007        * cond( int, const restrict volatile void *, const restrict volatile D * );
2008forall( dtype D ) _Atomic const restrict volatile void * cond(
2009        int, _Atomic const restrict volatile D *, _Atomic const restrict volatile void * ),
2010        * cond( int, _Atomic const restrict volatile void *, _Atomic const restrict volatile D * );
2011\end{lstlisting}
2012
2013\begin{rationale}
2014The object of the above is to apply the \Index{usual arithmetic conversion}s when the second and third operands have arithmetic type, and to combine the qualifiers of the second and third operands if they are pointers.
2015\end{rationale}
2016
2017\examples
2018\begin{lstlisting}
2019#include <stdlib.h>
2020int i;
2021long l;
2022rand() ? i : l;
2023\end{lstlisting}
2024The best interpretation infers the expression's type to be \lstinline$long$ and applies the safe
2025\lstinline$int$-to-\lstinline$long$ conversion to \lstinline$i$.
2026
2027\begin{lstlisting}
2028const int *cip;
2029volatile int *vip;
2030rand() ? cip : vip;
2031\end{lstlisting}
2032The expression has type \lstinline$const volatile int *$, with safe conversions applied to the second and third operands to add \lstinline$volatile$ and \lstinline$const$ qualifiers, respectively.
2033
2034\begin{lstlisting}
2035rand() ? cip : 0;
2036\end{lstlisting}
2037The expression has type \lstinline$const int *$, with a specialization conversion applied to
2038\lstinline$0$.
2039
2040
2041\subsection{Assignment operators}
2042
2043\begin{syntax}
2044\lhs{assignment-expression}
2045\rhs \nonterm{conditional-expression}
2046\rhs \nonterm{unary-expression} \nonterm{assignment-operator}
2047         \nonterm{assignment-expression}
2048\lhs{assignment-operator} one of
2049\rhs \lstinline$=$\ \ \lstinline$*=$\ \ \lstinline$/=$\ \ \lstinline$%=$\ \ \lstinline$+=$\ \ \lstinline$-=$\ \ 
2050         \lstinline$<<=$\ \ \lstinline$>>=$\ \ \lstinline$&=$\ \ \lstinline$^=$\ \ \lstinline$|=$
2051\end{syntax}
2052
2053\rewriterules
2054Let ``\(\leftarrow\)'' be any of the assignment operators.
2055Then
2056\use{?=?}\use{?*=?}\use{?/=?}\use{?%=?}\use{?+=?}\use{?-=?}
2057\use{?>>=?}\use{?&=?}\use{?^=?}\use{?"|=?}%use{?<<=?}
2058\begin{lstlisting}
2059a @$\leftarrow$@ b @\rewrite@ ?@$\leftarrow$@?( &( a ), b )
2060\end{lstlisting}
2061
2062\semantics
2063Each interpretation of the left operand of an assignment expression is considered separately.
2064For each interpretation that is a bit-field or is declared with the \lstinline$register$ storage class specifier, the expression has one valid interpretation, with the type of the left operand.
2065The right operand is cast to that type, and the assignment expression is ambiguous if either operand is.
2066For the remaining interpretations, the expression is rewritten, and the interpretations of the assignment expression are the interpretations of the corresponding function call.
2067Finally, all interpretations of the expression produced for the different interpretations of the left operand are combined to produce the interpretations of the expression as a whole;
2068where interpretations have compatible result types, the best interpretations are selected in the manner described for function call expressions.
2069
2070
2071\subsubsection{Simple assignment}
2072
2073\predefined
2074\begin{lstlisting}
2075_Bool
2076        ?=?( volatile _Bool *, _Bool ),
2077        ?=?( volatile _Bool *, forall( dtype D ) D * ),
2078        ?=?( volatile _Bool *, forall( ftype F ) F * ),
2079        ?=?( _Atomic volatile _Bool *, _Bool ),
2080        ?=?( _Atomic volatile _Bool *, forall( dtype D ) D * ),
2081        ?=?( _Atomic volatile _Bool *, forall( ftype F ) F * );
2082char
2083        ?=?( volatile char *, char ),
2084        ?=?( _Atomic volatile char *, char );
2085unsigned char
2086        ?=?( volatile unsigned char *, unsigned char ),
2087        ?=?( _Atomic volatile unsigned char *, unsigned char );
2088signed char
2089        ?=?( volatile signed char *, signed char ),
2090        ?=?( _Atomic volatile signed char *, signed char );
2091short int
2092        ?=?( volatile short int *, short int ),
2093        ?=?( _Atomic volatile short int *, short int );
2094unsigned short
2095        ?=?( volatile unsigned int *, unsigned int ),
2096        ?=?( _Atomic volatile unsigned int *, unsigned int );
2097int
2098        ?=?( volatile int *, int ),
2099        ?=?( _Atomic volatile int *, int );
2100unsigned int
2101        ?=?( volatile unsigned int *, unsigned int ),
2102        ?=?( _Atomic volatile unsigned int *, unsigned int );
2103long int
2104        ?=?( volatile long int *, long int ),
2105        ?=?( _Atomic volatile long int *, long int );
2106unsigned long int
2107        ?=?( volatile unsigned long int *, unsigned long int ),
2108        ?=?( _Atomic volatile unsigned long int *, unsigned long int );
2109long long int
2110        ?=?( volatile long long int *, long long int ),
2111        ?=?( _Atomic volatile long long int *, long long int );
2112unsigned long long int
2113        ?=?( volatile unsigned long long int *, unsigned long long int ),
2114        ?=?( _Atomic volatile unsigned long long int *, unsigned long long int );
2115float
2116        ?=?( volatile float *, float ),
2117        ?=?( _Atomic volatile float *, float );
2118double
2119        ?=?( volatile double *, double ),
2120        ?=?( _Atomic volatile double *, double );
2121long double
2122        ?=?( volatile long double *, long double ),
2123        ?=?( _Atomic volatile long double *, long double );
2124_Complex float
2125        ?=?( volatile float *, float ),
2126        ?=?( _Atomic volatile float *, float );
2127_Complex double
2128        ?=?( volatile double *, double ),
2129        ?=?( _Atomic volatile double *, double );
2130_Complex long double
2131        ?=?( volatile _Complex long double *, _Complex long double ),
2132        ?=?( _Atomic volatile _Complex long double *, _Atomic _Complex long double );
2133forall( ftype FT ) FT
2134        * ?=?( FT * volatile *, FT * ),
2135        * ?=?( FT * volatile *, forall( ftype F ) F * );
2136forall( ftype FT ) FT const
2137        * ?=?( FT const * volatile *, FT const * ),
2138        * ?=?( FT const * volatile *, forall( ftype F ) F * );
2139forall( ftype FT ) FT volatile
2140        * ?=?( FT volatile * volatile *, FT * ),
2141        * ?=?( FT volatile * volatile *, forall( ftype F ) F * );
2142forall( ftype FT ) FT const
2143        * ?=?( FT const volatile * volatile *, FT const * ),
2144        * ?=?( FT const volatile * volatile *, forall( ftype F ) F * );
2145forall( dtype DT ) DT
2146        * ?=?( DT * restrict volatile *, DT * ),
2147        * ?=?( DT * restrict volatile *, void * ),
2148        * ?=?( DT * restrict volatile *, forall( dtype D ) D * ),
2149        * ?=?( DT * _Atomic restrict volatile *, DT * ),
2150        * ?=?( DT * _Atomic restrict volatile *, void * ),
2151        * ?=?( DT * _Atomic restrict volatile *, forall( dtype D ) D * );
2152forall( dtype DT ) DT _Atomic
2153        * ?=?( _Atomic DT * restrict volatile *, DT _Atomic * ),
2154        * ?=?( _Atomic DT * restrict volatile *, void * ),
2155        * ?=?( _Atomic DT * restrict volatile *, forall( dtype D ) D * ),
2156        * ?=?( _Atomic DT * _Atomic restrict volatile *, DT _Atomic * ),
2157        * ?=?( _Atomic DT * _Atomic restrict volatile *, void * ),
2158        * ?=?( _Atomic DT * _Atomic restrict volatile *, forall( dtype D ) D * );
2159forall( dtype DT ) DT const
2160        * ?=?( DT const * restrict volatile *, DT const * ),
2161        * ?=?( DT const * restrict volatile *, void const * ),
2162        * ?=?( DT const * restrict volatile *, forall( dtype D ) D * ),
2163        * ?=?( DT const * _Atomic restrict volatile *, DT const * ),
2164        * ?=?( DT const * _Atomic restrict volatile *, void const * ),
2165        * ?=?( DT const * _Atomic restrict volatile *, forall( dtype D ) D * );
2166forall( dtype DT ) DT restrict
2167        * ?=?( restrict DT * restrict volatile *, DT restrict * ),
2168        * ?=?( restrict DT * restrict volatile *, void * ),
2169        * ?=?( restrict DT * restrict volatile *, forall( dtype D ) D * ),
2170        * ?=?( restrict DT * _Atomic restrict volatile *, DT restrict * ),
2171        * ?=?( restrict DT * _Atomic restrict volatile *, void * ),
2172        * ?=?( restrict DT * _Atomic restrict volatile *, forall( dtype D ) D * );
2173forall( dtype DT ) DT volatile
2174        * ?=?( DT volatile * restrict volatile *, DT volatile * ),
2175        * ?=?( DT volatile * restrict volatile *, void volatile * ),
2176        * ?=?( DT volatile * restrict volatile *, forall( dtype D ) D * ),
2177        * ?=?( DT volatile * _Atomic restrict volatile *, DT volatile * ),
2178        * ?=?( DT volatile * _Atomic restrict volatile *, void volatile * ),
2179        * ?=?( DT volatile * _Atomic restrict volatile *, forall( dtype D ) D * );
2180forall( dtype DT ) DT _Atomic const
2181        * ?=?( DT _Atomic const * restrict volatile *, DT _Atomic const * ),
2182        * ?=?( DT _Atomic const * restrict volatile *, void const * ),
2183        * ?=?( DT _Atomic const * restrict volatile *, forall( dtype D ) D * ),
2184        * ?=?( DT _Atomic const * _Atomic restrict volatile *, DT _Atomic const * ),
2185        * ?=?( DT _Atomic const * _Atomic restrict volatile *, void const * ),
2186        * ?=?( DT _Atomic const * _Atomic restrict volatile *, forall( dtype D ) D * );
2187forall( dtype DT ) DT _Atomic restrict
2188        * ?=?( _Atomic restrict DT * restrict volatile *, DT _Atomic restrict * ),
2189        * ?=?( _Atomic restrict DT * restrict volatile *, void * ),
2190        * ?=?( _Atomic restrict DT * restrict volatile *, forall( dtype D ) D * ),
2191        * ?=?( _Atomic restrict DT * _Atomic restrict volatile *, DT _Atomic restrict * ),
2192        * ?=?( _Atomic restrict DT * _Atomic restrict volatile *, void * ),
2193        * ?=?( _Atomic restrict DT * _Atomic restrict volatile *, forall( dtype D ) D * );
2194forall( dtype DT ) DT _Atomic volatile
2195        * ?=?( DT _Atomic volatile * restrict volatile *, DT _Atomic volatile * ),
2196        * ?=?( DT _Atomic volatile * restrict volatile *, void volatile * ),
2197        * ?=?( DT _Atomic volatile * restrict volatile *, forall( dtype D ) D * ),
2198        * ?=?( DT _Atomic volatile * _Atomic restrict volatile *, DT _Atomic volatile * ),
2199        * ?=?( DT _Atomic volatile * _Atomic restrict volatile *, void volatile * ),
2200        * ?=?( DT _Atomic volatile * _Atomic restrict volatile *, forall( dtype D ) D * );
2201forall( dtype DT ) DT const restrict
2202        * ?=?( DT const restrict * restrict volatile *, DT const restrict * ),
2203        * ?=?( DT const restrict * restrict volatile *, void const * ),
2204        * ?=?( DT const restrict * restrict volatile *, forall( dtype D ) D * ),
2205        * ?=?( DT const restrict * _Atomic restrict volatile *, DT const restrict * ),
2206        * ?=?( DT const restrict * _Atomic restrict volatile *, void const * ),
2207        * ?=?( DT const restrict * _Atomic restrict volatile *, forall( dtype D ) D * );
2208forall( dtype DT ) DT const volatile
2209        * ?=?( DT const volatile * restrict volatile *, DT const volatile * ),
2210        * ?=?( DT const volatile * restrict volatile *, void const volatile * ),
2211        * ?=?( DT const volatile * restrict volatile *, forall( dtype D ) D * ),
2212        * ?=?( DT const volatile * _Atomic restrict volatile *, DT const volatile * ),
2213        * ?=?( DT const volatile * _Atomic restrict volatile *, void const volatile * ),
2214        * ?=?( DT const volatile * _Atomic restrict volatile *, forall( dtype D ) D * );
2215forall( dtype DT ) DT restrict volatile
2216        * ?=?( DT restrict volatile * restrict volatile *, DT restrict volatile * ),
2217        * ?=?( DT restrict volatile * restrict volatile *, void volatile * ),
2218        * ?=?( DT restrict volatile * restrict volatile *, forall( dtype D ) D * ),
2219        * ?=?( DT restrict volatile * _Atomic restrict volatile *, DT restrict volatile * ),
2220        * ?=?( DT restrict volatile * _Atomic restrict volatile *, void volatile * ),
2221        * ?=?( DT restrict volatile * _Atomic restrict volatile *, forall( dtype D ) D * );
2222forall( dtype DT ) DT _Atomic const restrict
2223        * ?=?( DT _Atomic const restrict * restrict volatile *,
2224         DT _Atomic const restrict * ),
2225        * ?=?( DT _Atomic const restrict * restrict volatile *,
2226         void const * ),
2227        * ?=?( DT _Atomic const restrict * restrict volatile *,
2228         forall( dtype D ) D * ),
2229        * ?=?( DT _Atomic const restrict * _Atomic restrict volatile *,
2230         DT _Atomic const restrict * ),
2231        * ?=?( DT _Atomic const restrict * _Atomic restrict volatile *,
2232         void const * ),
2233        * ?=?( DT _Atomic const restrict * _Atomic restrict volatile *,
2234         forall( dtype D ) D * );
2235forall( dtype DT ) DT _Atomic const volatile
2236        * ?=?( DT _Atomic const volatile * restrict volatile *,
2237         DT _Atomic const volatile * ),
2238        * ?=?( DT _Atomic const volatile * restrict volatile *,
2239         void const volatile * ),
2240        * ?=?( DT _Atomic const volatile * restrict volatile *,
2241         forall( dtype D ) D * ),
2242        * ?=?( DT _Atomic const volatile * _Atomic restrict volatile *,
2243         DT _Atomic const volatile * ),
2244        * ?=?( DT _Atomic const volatile * _Atomic restrict volatile *,
2245         void const volatile * ),
2246        * ?=?( DT _Atomic const volatile * _Atomic restrict volatile *,
2247         forall( dtype D ) D * );
2248forall( dtype DT ) DT _Atomic restrict volatile
2249        * ?=?( DT _Atomic restrict volatile * restrict volatile *,
2250         DT _Atomic restrict volatile * ),
2251        * ?=?( DT _Atomic restrict volatile * restrict volatile *,
2252         void volatile * ),
2253        * ?=?( DT _Atomic restrict volatile * restrict volatile *,
2254         forall( dtype D ) D * ),
2255        * ?=?( DT _Atomic restrict volatile * _Atomic restrict volatile *,
2256         DT _Atomic restrict volatile * ),
2257        * ?=?( DT _Atomic restrict volatile * _Atomic restrict volatile *,
2258         void volatile * ),
2259        * ?=?( DT _Atomic restrict volatile * _Atomic restrict volatile *,
2260         forall( dtype D ) D * );
2261forall( dtype DT ) DT const restrict volatile
2262        * ?=?( DT const restrict volatile * restrict volatile *,
2263         DT const restrict volatile * ),
2264        * ?=?( DT const restrict volatile * restrict volatile *,
2265         void const volatile * ),
2266        * ?=?( DT const restrict volatile * restrict volatile *,
2267         forall( dtype D ) D * ),
2268        * ?=?( DT const restrict volatile * _Atomic restrict volatile *,
2269         DT const restrict volatile * ),
2270        * ?=?( DT const restrict volatile * _Atomic restrict volatile *,
2271         void const volatile * ),
2272        * ?=?( DT const restrict volatile * _Atomic restrict volatile *,
2273         forall( dtype D ) D * );
2274forall( dtype DT ) DT _Atomic const restrict volatile
2275        * ?=?( DT _Atomic const restrict volatile * restrict volatile *,
2276         DT _Atomic const restrict volatile * ),
2277        * ?=?( DT _Atomic const restrict volatile * restrict volatile *,
2278         void const volatile * ),
2279        * ?=?( DT _Atomic const restrict volatile * restrict volatile *,
2280         forall( dtype D ) D * ),
2281        * ?=?( DT _Atomic const restrict volatile * _Atomic restrict volatile *,
2282         DT _Atomic const restrict volatile * ),
2283        * ?=?( DT _Atomic const restrict volatile * _Atomic restrict volatile *,
2284         void const volatile * ),
2285        * ?=?( DT _Atomic const restrict volatile * _Atomic restrict volatile *,
2286         forall( dtype D ) D * );
2287forall( dtype DT ) void
2288        * ?=?( void * restrict volatile *, DT * );
2289forall( dtype DT ) void const
2290        * ?=?( void const * restrict volatile *, DT const * );
2291forall( dtype DT ) void volatile
2292        * ?=?( void volatile * restrict volatile *, DT volatile * );
2293forall( dtype DT ) void const volatile
2294        * ?=?( void const volatile * restrict volatile *, DT const volatile * );
2295\end{lstlisting}
2296\begin{rationale}
2297The pattern of overloadings for simple assignment resembles that of pointer increment and decrement, except that the polymorphic pointer assignment functions declare a \lstinline$dtype$ parameter, instead of a \lstinline$type$ parameter, because the left operand may be a pointer to an incomplete type.
2298\end{rationale}
2299
2300For every complete structure or union type \lstinline$S$ there exist
2301% Don't use predefined: keep this out of prelude.cf.
2302\begin{lstlisting}
2303S ?=?( S volatile *, S ), ?=?( S _Atomic volatile *, S );
2304\end{lstlisting}
2305
2306For every extended integer type \lstinline$X$ there exist
2307% Don't use predefined: keep this out of prelude.cf.
2308\begin{lstlisting}
2309X ?=?( X volatile *, X ), ?=?( X _Atomic volatile *, X );
2310\end{lstlisting}
2311
2312For every complete enumerated type \lstinline$E$ there exist
2313% Don't use predefined: keep this out of prelude.cf.
2314\begin{lstlisting}
2315E ?=?( E volatile *, int ), ?=?( E _Atomic volatile *, int );
2316\end{lstlisting}
2317\begin{rationale}
2318The right-hand argument is \lstinline$int$ because enumeration constants have type \lstinline$int$.
2319\end{rationale}
2320
2321\semantics
2322The structure assignment functions provide member-wise assignment;
2323each non-array member and each element of each array member of the right argument is assigned to the corresponding member or element of the left argument using the assignment function defined for its type.
2324All other assignment functions have the same effect as the corresponding C assignment expression.
2325\begin{rationale}
2326Note that, by default, union assignment\index{deficiencies!union assignment} uses C semantics---that is, bitwise copy---even if some of the union members have programmer-defined assignment functions.
2327\end{rationale}
2328
2329
2330\subsubsection{Compound assignment}
2331
2332\predefined
2333\begin{lstlisting}
2334forall( otype T ) T
2335        * ?+=?( T * restrict volatile *, ptrdiff_t ),
2336        * ?-=?( T * restrict volatile *, ptrdiff_t ),
2337        * ?+=?( T * _Atomic restrict volatile *, ptrdiff_t ),
2338        * ?-=?( T * _Atomic restrict volatile *, ptrdiff_t );
2339forall( otype T ) T _Atomic
2340        * ?+=?( T _Atomic * restrict volatile *, ptrdiff_t ),
2341        * ?-=?( T _Atomic * restrict volatile *, ptrdiff_t ),
2342        * ?+=?( T _Atomic * _Atomic restrict volatile *, ptrdiff_t ),
2343        * ?-=?( T _Atomic * _Atomic restrict volatile *, ptrdiff_t );
2344forall( otype T ) T const
2345        * ?+=?( T const * restrict volatile *, ptrdiff_t ),
2346        * ?-=?( T const * restrict volatile *, ptrdiff_t ),
2347        * ?+=?( T const * _Atomic restrict volatile *, ptrdiff_t ),
2348        * ?-=?( T const * _Atomic restrict volatile *, ptrdiff_t );
2349forall( otype T ) T restrict
2350        * ?+=?( T restrict * restrict volatile *, ptrdiff_t ),
2351        * ?-=?( T restrict * restrict volatile *, ptrdiff_t ),
2352        * ?+=?( T restrict * _Atomic restrict volatile *, ptrdiff_t ),
2353        * ?-=?( T restrict * _Atomic restrict volatile *, ptrdiff_t );
2354forall( otype T ) T volatile
2355        * ?+=?( T volatile * restrict volatile *, ptrdiff_t ),
2356        * ?-=?( T volatile * restrict volatile *, ptrdiff_t ),
2357        * ?+=?( T volatile * _Atomic restrict volatile *, ptrdiff_t ),
2358        * ?-=?( T volatile * _Atomic restrict volatile *, ptrdiff_t );
2359forall( otype T ) T _Atomic const
2360        * ?+=?( T _Atomic const restrict volatile *, ptrdiff_t ),
2361        * ?-=?( T _Atomic const restrict volatile *, ptrdiff_t ),
2362        * ?+=?( T _Atomic const _Atomic restrict volatile *, ptrdiff_t ),
2363        * ?-=?( T _Atomic const _Atomic restrict volatile *, ptrdiff_t );
2364forall( otype T ) T _Atomic restrict
2365        * ?+=?( T _Atomic restrict * restrict volatile *, ptrdiff_t ),
2366        * ?-=?( T _Atomic restrict * restrict volatile *, ptrdiff_t ),
2367        * ?+=?( T _Atomic restrict * _Atomic restrict volatile *, ptrdiff_t ),
2368        * ?-=?( T _Atomic restrict * _Atomic restrict volatile *, ptrdiff_t );
2369forall( otype T ) T _Atomic volatile
2370        * ?+=?( T _Atomic volatile * restrict volatile *, ptrdiff_t ),
2371        * ?-=?( T _Atomic volatile * restrict volatile *, ptrdiff_t ),
2372        * ?+=?( T _Atomic volatile * _Atomic restrict volatile *, ptrdiff_t ),
2373        * ?-=?( T _Atomic volatile * _Atomic restrict volatile *, ptrdiff_t );
2374forall( otype T ) T const restrict
2375        * ?+=?( T const restrict * restrict volatile *, ptrdiff_t ),
2376        * ?-=?( T const restrict * restrict volatile *, ptrdiff_t ),
2377        * ?+=?( T const restrict * _Atomic restrict volatile *, ptrdiff_t ),
2378        * ?-=?( T const restrict * _Atomic restrict volatile *, ptrdiff_t );
2379forall( otype T ) T const volatile
2380        * ?+=?( T const volatile * restrict volatile *, ptrdiff_t ),
2381        * ?-=?( T const volatile * restrict volatile *, ptrdiff_t ),
2382        * ?+=?( T const volatile * _Atomic restrict volatile *, ptrdiff_t ),
2383        * ?-=?( T const volatile * _Atomic restrict volatile *, ptrdiff_t );
2384forall( otype T ) T restrict volatile
2385        * ?+=?( T restrict volatile * restrict volatile *, ptrdiff_t ),
2386        * ?-=?( T restrict volatile * restrict volatile *, ptrdiff_t ),
2387        * ?+=?( T restrict volatile * _Atomic restrict volatile *, ptrdiff_t ),
2388        * ?-=?( T restrict volatile * _Atomic restrict volatile *, ptrdiff_t );
2389forall( otype T ) T _Atomic const restrict
2390        * ?+=?( T _Atomic const restrict * restrict volatile *, ptrdiff_t ),
2391        * ?-=?( T _Atomic const restrict * restrict volatile *, ptrdiff_t ),
2392        * ?+=?( T _Atomic const restrict * _Atomic restrict volatile *, ptrdiff_t ),
2393        * ?-=?( T _Atomic const restrict * _Atomic restrict volatile *, ptrdiff_t );
2394forall( otype T ) T _Atomic const volatile
2395        * ?+=?( T _Atomic const volatile * restrict volatile *, ptrdiff_t ),
2396        * ?-=?( T _Atomic const volatile * restrict volatile *, ptrdiff_t ),
2397        * ?+=?( T _Atomic const volatile * _Atomic restrict volatile *, ptrdiff_t ),
2398        * ?-=?( T _Atomic const volatile * _Atomic restrict volatile *, ptrdiff_t );
2399forall( otype T ) T _Atomic restrict volatile
2400        * ?+=?( T _Atomic restrict volatile * restrict volatile *, ptrdiff_t ),
2401        * ?-=?( T _Atomic restrict volatile * restrict volatile *, ptrdiff_t ),
2402        * ?+=?( T _Atomic restrict volatile * _Atomic restrict volatile *, ptrdiff_t ),
2403        * ?-=?( T _Atomic restrict volatile * _Atomic restrict volatile *, ptrdiff_t );
2404forall( otype T ) T const restrict volatile
2405        * ?+=?( T const restrict volatile * restrict volatile *, ptrdiff_t ),
2406        * ?-=?( T const restrict volatile * restrict volatile *, ptrdiff_t ),
2407        * ?+=?( T const restrict volatile * _Atomic restrict volatile *, ptrdiff_t ),
2408        * ?-=?( T const restrict volatile * _Atomic restrict volatile *, ptrdiff_t );
2409forall( otype T ) T _Atomic const restrict volatile
2410        * ?+=?( T _Atomic const restrict volatile * restrict volatile *, ptrdiff_t ),
2411        * ?-=?( T _Atomic const restrict volatile * restrict volatile *, ptrdiff_t ),
2412        * ?+=?( T _Atomic const restrict volatile * _Atomic restrict volatile *, ptrdiff_t ),
2413        * ?-=?( T _Atomic const restrict volatile * _Atomic restrict volatile *, ptrdiff_t );
2414
2415_Bool
2416        ?*=?( _Bool volatile *, _Bool ),
2417        ?/=?( _Bool volatile *, _Bool ),
2418        ?+=?( _Bool volatile *, _Bool ),
2419        ?-=?( _Bool volatile *, _Bool ),
2420        ?%=?( _Bool volatile *, _Bool ),
2421        ?<<=?( _Bool volatile *, int ),
2422        ?>>=?( _Bool volatile *, int ),
2423        ?&=?( _Bool volatile *, _Bool ),
2424        ?^=?( _Bool volatile *, _Bool ),
2425        ?|=?( _Bool volatile *, _Bool );
2426char
2427        ?*=?( char volatile *, char ),
2428        ?/=?( char volatile *, char ),
2429        ?+=?( char volatile *, char ),
2430        ?-=?( char volatile *, char ),
2431        ?%=?( char volatile *, char ),
2432        ?<<=?( char volatile *, int ),
2433        ?>>=?( char volatile *, int ),
2434        ?&=?( char volatile *, char ),
2435        ?^=?( char volatile *, char ),
2436        ?|=?( char volatile *, char );
2437unsigned char
2438        ?*=?( unsigned char volatile *, unsigned char ),
2439        ?/=?( unsigned char volatile *, unsigned char ),
2440        ?+=?( unsigned char volatile *, unsigned char ),
2441        ?-=?( unsigned char volatile *, unsigned char ),
2442        ?%=?( unsigned char volatile *, unsigned char ),
2443        ?<<=?( unsigned char volatile *, int ),
2444        ?>>=?( unsigned char volatile *, int ),
2445        ?&=?( unsigned char volatile *, unsigned char ),
2446        ?^=?( unsigned char volatile *, unsigned char ),
2447        ?|=?( unsigned char volatile *, unsigned char );
2448signed char
2449        ?*=?( signed char volatile *, signed char ),
2450        ?/=?( signed char volatile *, signed char ),
2451        ?+=?( signed char volatile *, signed char ),
2452        ?-=?( signed char volatile *, signed char ),
2453        ?%=?( signed char volatile *, signed char ),
2454        ?<<=?( signed char volatile *, int ),
2455        ?>>=?( signed char volatile *, int ),
2456        ?&=?( signed char volatile *, signed char ),
2457        ?^=?( signed char volatile *, signed char ),
2458        ?|=?( signed char volatile *, signed char );
2459short int
2460        ?*=?( short int volatile *, short int ),
2461        ?/=?( short int volatile *, short int ),
2462        ?+=?( short int volatile *, short int ),
2463        ?-=?( short int volatile *, short int ),
2464        ?%=?( short int volatile *, short int ),
2465        ?<<=?( short int volatile *, int ),
2466        ?>>=?( short int volatile *, int ),
2467        ?&=?( short int volatile *, short int ),
2468        ?^=?( short int volatile *, short int ),
2469        ?|=?( short int volatile *, short int );
2470unsigned short int
2471        ?*=?( unsigned short int volatile *, unsigned short int ),
2472        ?/=?( unsigned short int volatile *, unsigned short int ),
2473        ?+=?( unsigned short int volatile *, unsigned short int ),
2474        ?-=?( unsigned short int volatile *, unsigned short int ),
2475        ?%=?( unsigned short int volatile *, unsigned short int ),
2476        ?<<=?( unsigned short int volatile *, int ),
2477        ?>>=?( unsigned short int volatile *, int ),
2478        ?&=?( unsigned short int volatile *, unsigned short int ),
2479        ?^=?( unsigned short int volatile *, unsigned short int ),
2480        ?|=?( unsigned short int volatile *, unsigned short int );
2481int
2482        ?*=?( int volatile *, int ),
2483        ?/=?( int volatile *, int ),
2484        ?+=?( int volatile *, int ),
2485        ?-=?( int volatile *, int ),
2486        ?%=?( int volatile *, int ),
2487        ?<<=?( int volatile *, int ),
2488        ?>>=?( int volatile *, int ),
2489        ?&=?( int volatile *, int ),
2490        ?^=?( int volatile *, int ),
2491        ?|=?( int volatile *, int );
2492unsigned int
2493        ?*=?( unsigned int volatile *, unsigned int ),
2494        ?/=?( unsigned int volatile *, unsigned int ),
2495        ?+=?( unsigned int volatile *, unsigned int ),
2496        ?-=?( unsigned int volatile *, unsigned int ),
2497        ?%=?( unsigned int volatile *, unsigned int ),
2498        ?<<=?( unsigned int volatile *, int ),
2499        ?>>=?( unsigned int volatile *, int ),
2500        ?&=?( unsigned int volatile *, unsigned int ),
2501        ?^=?( unsigned int volatile *, unsigned int ),
2502        ?|=?( unsigned int volatile *, unsigned int );
2503long int
2504        ?*=?( long int volatile *, long int ),
2505        ?/=?( long int volatile *, long int ),
2506        ?+=?( long int volatile *, long int ),
2507        ?-=?( long int volatile *, long int ),
2508        ?%=?( long int volatile *, long int ),
2509        ?<<=?( long int volatile *, int ),
2510        ?>>=?( long int volatile *, int ),
2511        ?&=?( long int volatile *, long int ),
2512        ?^=?( long int volatile *, long int ),
2513        ?|=?( long int volatile *, long int );
2514unsigned long int
2515        ?*=?( unsigned long int volatile *, unsigned long int ),
2516        ?/=?( unsigned long int volatile *, unsigned long int ),
2517        ?+=?( unsigned long int volatile *, unsigned long int ),
2518        ?-=?( unsigned long int volatile *, unsigned long int ),
2519        ?%=?( unsigned long int volatile *, unsigned long int ),
2520        ?<<=?( unsigned long int volatile *, int ),
2521        ?>>=?( unsigned long int volatile *, int ),
2522        ?&=?( unsigned long int volatile *, unsigned long int ),
2523        ?^=?( unsigned long int volatile *, unsigned long int ),
2524        ?|=?( unsigned long int volatile *, unsigned long int );
2525long long int
2526        ?*=?( long long int volatile *, long long int ),
2527        ?/=?( long long int volatile *, long long int ),
2528        ?+=?( long long int volatile *, long long int ),
2529        ?-=?( long long int volatile *, long long int ),
2530        ?%=?( long long int volatile *, long long int ),
2531        ?<<=?( long long int volatile *, int ),
2532        ?>>=?( long long int volatile *, int ),
2533        ?&=?( long long int volatile *, long long int ),
2534        ?^=?( long long int volatile *, long long int ),
2535        ?|=?( long long int volatile *, long long int );
2536unsigned long long int
2537        ?*=?( unsigned long long int volatile *, unsigned long long int ),
2538        ?/=?( unsigned long long int volatile *, unsigned long long int ),
2539        ?+=?( unsigned long long int volatile *, unsigned long long int ),
2540        ?-=?( unsigned long long int volatile *, unsigned long long int ),
2541        ?%=?( unsigned long long int volatile *, unsigned long long int ),
2542        ?<<=?( unsigned long long int volatile *, int ),
2543        ?>>=?( unsigned long long int volatile *, int ),
2544        ?&=?( unsigned long long int volatile *, unsigned long long int ),
2545        ?^=?( unsigned long long int volatile *, unsigned long long int ),
2546        ?|=?( unsigned long long int volatile *, unsigned long long int );
2547float
2548        ?*=?( float volatile *, float ),
2549        ?/=?( float volatile *, float ),
2550        ?+=?( float volatile *, float ),
2551        ?-=?( float volatile *, float );
2552double
2553        ?*=?( double volatile *, double ),
2554        ?/=?( double volatile *, double ),
2555        ?+=?( double volatile *, double ),
2556        ?-=?( double volatile *, double );
2557long double
2558        ?*=?( long double volatile *, long double ),
2559        ?/=?( long double volatile *, long double ),
2560        ?+=?( long double volatile *, long double ),
2561        ?-=?( long double volatile *, long double );
2562_Complex float
2563        ?*=?( _Complex float volatile *, _Complex float ),
2564        ?/=?( _Complex float volatile *, _Complex float ),
2565        ?+=?( _Complex float volatile *, _Complex float ),
2566        ?-=?( _Complex float volatile *, _Complex float );
2567_Complex double
2568        ?*=?( _Complex double volatile *, _Complex double ),
2569        ?/=?( _Complex double volatile *, _Complex double ),
2570        ?+=?( _Complex double volatile *, _Complex double ),
2571        ?-=?( _Complex double volatile *, _Complex double );
2572_Complex long double
2573        ?*=?( _Complex long double volatile *, _Complex long double ),
2574        ?/=?( _Complex long double volatile *, _Complex long double ),
2575        ?+=?( _Complex long double volatile *, _Complex long double ),
2576        ?-=?( _Complex long double volatile *, _Complex long double );
2577\end{lstlisting}
2578
2579For every extended integer type \lstinline$X$ there exist
2580% Don't use predefined: keep this out of prelude.cf.
2581\begin{lstlisting}
2582?*=?( X volatile *, X ),
2583?/=?( X volatile *, X ),
2584?+=?( X volatile *, X ),
2585?-=?( X volatile *, X ),
2586?%=?( X volatile *, X ),
2587?<<=?( X volatile *, int ),
2588?>>=?( X volatile *, int ),
2589?&=?( X volatile *, X ),
2590?^=?( X volatile *, X ),
2591?|=?( X volatile *, X );
2592\end{lstlisting}
2593
2594For every complete enumerated type \lstinline$E$ there exist
2595% Don't use predefined: keep this out of prelude.cf.
2596\begin{lstlisting}
2597?*=?( E volatile *, E ),
2598?/=?( E volatile *, E ),
2599?+=?( E volatile *, E ),
2600?-=?( E volatile *, E ),
2601?%=?( E volatile *, E ),
2602?<<=?( E volatile *, int ),
2603?>>=?( E volatile *, int ),
2604?&=?( E volatile *, E ),
2605?^=?( E volatile *, E ),
2606?|=?( E volatile *, E );
2607\end{lstlisting}
2608
2609
2610\subsection{Comma operator}
2611
2612\begin{syntax}
2613\lhs{expression}
2614\rhs \nonterm{assignment-expression}
2615\rhs \nonterm{expression} \lstinline$,$ \nonterm{assignment-expression}
2616\end{syntax}
2617
2618\semantics
2619In the comma expression ``\lstinline$a, b$'', the first operand is interpreted as
2620``\lstinline$( void )(a)$'', which shall be unambiguous\index{ambiguous interpretation}.
2621The interpretations of the expression are the interpretations of the second operand.
2622
2623
2624\section{Constant expressions}
2625
2626
2627\section{Declarations}
2628
2629\begin{syntax}
2630\oldlhs{declaration}
2631\rhs \nonterm{type-declaration}
2632\rhs \nonterm{spec-definition}
2633\end{syntax}
2634
2635\constraints
2636If an identifier has \Index{no linkage}, there shall be no more than one declaration of the identifier ( in a declarator or type specifier ) with compatible types in the same scope and in the same name space, except that:
2637\begin{itemize}
2638\item a typedef name may be redefined to denote the same type as it currently does, provided that type is not a variably modified type;
2639\item tags may be redeclared as specified in section 6.7.2.3 of the {\c11} standard.
2640\end{itemize}
2641\begin{rationale}
2642This constraint adds the phrase ``with compatible types'' to the {\c11} constraint, to allow overloading.
2643\end{rationale}
2644
2645An identifier declared by a type declaration shall not be redeclared as a parameter in a function definition whose declarator includes an identifier list.
2646\begin{rationale}
2647This restriction echos {\c11}'s ban on the redeclaration of typedef names as parameters.
2648This avoids an ambiguity between old-style function declarations and new-style function prototypes:
2649\begin{lstlisting}
2650void f( Complex,        // ... 3000 characters ...
2651void g( Complex,        // ... 3000 characters ...
2652int Complex;
2653{ ... }
2654\end{lstlisting}
2655Without the rule, \lstinline$Complex$ would be a type in the first case, and a parameter name in the second.
2656\end{rationale}
2657
2658
2659\setcounter{subsection}{1}
2660\subsection{Type specifiers}
2661
2662\begin{syntax}
2663\oldlhs{type-specifier}
2664\rhs \nonterm{forall-specifier}
2665\end{syntax}
2666
2667\semantics
2668Forall specifiers are discussed in \VRef{forall}.
2669
2670
2671\subsubsection{Structure and union specifiers}
2672
2673\semantics 
2674\CFA extends the {\c11} definition of \define{anonymous structure} to include structure specifiers with tags, and extends the {\c11} definition of \define{anonymous union} to include union specifiers with tags.
2675\begin{rationale}
2676This extension imitates an extension in the Plan 9 C compiler \cite{Thompson90new}.
2677\end{rationale}
2678
2679\examples
2680\begin{lstlisting}
2681struct point {@\impl{point}@
2682        int x, y;
2683};
2684struct color_point {@\impl{color_point}@
2685        enum { RED, BLUE, GREEN } color;
2686        struct point;
2687};
2688struct color_point cp;
2689cp.x = 0;
2690cp.color = RED;
2691struct literal {@\impl{literal}@
2692        enum { NUMBER, STRING } tag;
2693        union {
2694                double n;
2695                char *s;
2696        };
2697};
2698struct literal *next;
2699int length;
2700extern int strlen( const char * );
2701...
2702if ( next->tag == STRING ) length = strlen( next->s );
2703\end{lstlisting}
2704
2705
2706\setcounter{subsubsection}{4}
2707\subsubsection{Forall specifiers}
2708\label{forall}
2709
2710\begin{syntax}
2711\lhs{forall-specifier}
2712\rhs \lstinline$forall$ \lstinline$($ \nonterm{type-parameter-list} \lstinline$)$
2713\end{syntax}
2714
2715\begin{comment}
2716\constraints
2717If the \nonterm{declaration-specifiers} of a declaration that contains a \nonterm{forall-specifier} declares a structure or union tag, the types of the members of the structure or union shall not use any of the type identifiers declared by the \nonterm{type-parameter-list}.
2718\begin{rationale}
2719This sort of declaration is illegal because the scope of the type identifiers ends at the end of the declaration, but the scope of the structure tag does not.
2720\begin{lstlisting}
2721forall( otype T ) struct Pair { T a, b;
2722} mkPair( T, T ); // illegal
2723\end{lstlisting}
2724If an instance of \lstinline$struct Pair$ was declared later in the current scope, what would the members' type be?
2725\end{rationale}
2726\end{comment}
2727
2728\semantics
2729The \nonterm{type-parameter-list}s and assertions of the \nonterm{forall-specifier}s declare type identifiers, function and object identifiers with \Index{no linkage}.
2730
2731If, in the declaration ``\lstinline$T D$'', \lstinline$T$ contains \nonterm{forall-specifier}s and
2732\lstinline$D$ has the form
2733\begin{lstlisting}
2734D( @\normalsize\nonterm{parameter-type-list}@ )
2735\end{lstlisting} then a type identifier declared by one of the \nonterm{forall-specifier}s is an \define{inferred parameter} of the function declarator if and only if it is not an inferred parameter of a function declarator in \lstinline$D$, and it is used in the type of a parameter in the following
2736\nonterm{type-parameter-list} or it and an inferred parameter are used as arguments of a
2737\Index{specification} in one of the \nonterm{forall-specifier}s.
2738The identifiers declared by assertions that use an inferred parameter of a function declarator are \Index{assertion parameter}s of that function declarator.
2739
2740\begin{comment}
2741\begin{rationale}
2742Since every inferred parameter is used by some parameter, inference can be understood as a single bottom-up pass over the expression tree, that only needs to apply local reasoning at each node.
2743
2744If this restriction were lifted, it would be possible to write
2745\begin{lstlisting}
2746forall( otype T ) T * alloc( void );@\use{alloc}@ int *p = alloc();
2747\end{lstlisting}
2748Here \lstinline$alloc()$ would receive \lstinline$int$ as an inferred argument, and return an
2749\lstinline$int *$.
2750In general, if a call to \lstinline$alloc()$ is a subexpression of an expression involving polymorphic functions and overloaded identifiers, there could be considerable distance between the call and the subexpression that causes \lstinline$T$ to be bound.
2751
2752With the current restriction, \lstinline$alloc()$ must be given an argument that determines
2753\lstinline$T$:
2754\begin{lstlisting}
2755forall( otype T ) T * alloc( T initial_value );@\use{alloc}@
2756\end{lstlisting}
2757\end{rationale}
2758\end{comment}
2759
2760If a function declarator is part of a function definition, its inferred parameters and assertion parameters have \Index{block scope};
2761otherwise, identifiers declared by assertions have a
2762\define{declaration scope}, which terminates at the end of the \nonterm{declaration}.
2763
2764A function type that has at least one inferred parameter is a \define{polymorphic function} type.
2765Function types with no inferred parameters are \define{monomorphic function} types.
2766One function type is \define{less polymorphic} than another if it has fewer inferred parameters, or if it has the same number of inferred parameters and fewer of its explicit parameters have types that depend on an inferred parameter.
2767
2768The names of inferred parameters and the order of identifiers in forall specifiers are not relevant to polymorphic function type compatibility.
2769Let $f$ and $g$ be two polymorphic function types with the same number of inferred parameters, and let $f_i$ and $g_i$ be the inferred parameters of $f$ and $g$ in their order of occurance in the function types' \nonterm{parameter-type-list}s.
2770Let $f'$ be $f$ with every occurrence of $f_i$ replaced by $g_i$, for all $i$.
2771Then $f$ and $g$ are
2772\Index{compatible type}s if $f'$'s and $g$'s return types and parameter lists are compatible, and if for every assertion parameter of $f'$ there is an assertion parameter in $g$ with the same identifier and compatible type, and vice versa.
2773
2774\examples
2775Consider these analogous monomorphic and polymorphic declarations.
2776\begin{lstlisting}
2777int fi( int );
2778forall( otype T ) T fT( T );
2779\end{lstlisting}
2780\lstinline$fi()$ takes an \lstinline$int$ and returns an \lstinline$int$. \lstinline$fT()$ takes a
2781\lstinline$T$ and returns a \lstinline$T$, for any type \lstinline$T$.
2782\begin{lstlisting}
2783int (*pfi )( int ) = fi;
2784forall( otype T ) T (*pfT )( T ) = fT;
2785\end{lstlisting}
2786\lstinline$pfi$ and \lstinline$pfT$ are pointers to functions. \lstinline$pfT$ is not polymorphic, but the function it points at is.
2787\begin{lstlisting}
2788int (*fvpfi( void ))( int ) {
2789        return pfi;
2790}
2791forall( otype T ) T (*fvpfT( void ))( T ) {
2792        return pfT;
2793}
2794\end{lstlisting}
2795\lstinline$fvpfi()$ and \lstinline$fvpfT()$ are functions taking no arguments and returning pointers to functions. \lstinline$fvpfT()$ is monomorphic, but the function that its return value points at is polymorphic.
2796\begin{lstlisting}
2797forall( otype T ) int ( *fTpfi( T ) )( int );
2798forall( otype T ) T ( *fTpfT( T ) )( T );
2799forall( otype T, otype U ) U ( *fTpfU( T ) )( U );
2800\end{lstlisting}
2801\lstinline$fTpfi()$ is a polymorphic function that returns a pointer to a monomorphic function taking an integer and returning an integer.
2802It could return \lstinline$pfi$. \lstinline$fTpfT()$ is subtle: it is a polymorphic function returning a \emph{monomorphic} function taking and returning
2803\lstinline$T$, where \lstinline$T$ is an inferred parameter of \lstinline$fTpfT()$.
2804For instance, in the expression ``\lstinline$fTpfT(17)$'', \lstinline$T$ is inferred to be \lstinline$int$, and the returned value would have type \lstinline$int ( * )( int )$. ``\lstinline$fTpfT(17)(13)$'' and
2805``\lstinline$fTpfT("yes")("no")$'' are legal, but ``\lstinline$fTpfT(17)("no")$'' is illegal.
2806\lstinline$fTpfU()$ is polymorphic ( in type \lstinline$T$), and returns a pointer to a function that is polymorphic ( in type \lstinline$U$). ``\lstinline$f5(17)("no")$'' is a legal expression of type
2807\lstinline$char *$.
2808\begin{lstlisting}
2809forall( otype T, otype U, otype V ) U * f( T *, U, V * const );
2810forall( otype U, otype V, otype W ) U * g( V *, U, W * const );
2811\end{lstlisting}
2812The functions \lstinline$f()$ and \lstinline$g()$ have compatible types.
2813Let \(f\) and \(g\) be their types;
2814then \(f_1\) = \lstinline$T$, \(f_2\) = \lstinline$U$, \(f_3\) = \lstinline$V$, \(g_1\)
2815= \lstinline$V$, \(g_2\) = \lstinline$U$, and \(g_3\) = \lstinline$W$.
2816Replacing every \(f_i\) by \(g_i\) in \(f\) gives
2817\begin{lstlisting}
2818forall( otype V, otype U, otype W ) U * f( V *, U, W * const );
2819\end{lstlisting} which has a return type and parameter list that is compatible with \(g\).
2820\begin{rationale}
2821The word ``\lstinline$type$'' in a forall specifier is redundant at the moment, but I want to leave room for inferred parameters of ordinary types in case parameterized types get added one day.
2822
2823Even without parameterized types, I might try to allow
2824\begin{lstlisting}
2825forall( int n ) int sum( int vector[n] );
2826\end{lstlisting} but C currently rewrites array parameters as pointer parameters, so the effects of such a change require more thought.
2827\end{rationale}
2828
2829\begin{rationale}
2830A polymorphic declaration must do two things: it must introduce type parameters, and it must apply assertions to those types.
2831Adding this to existing C declaration syntax and semantics was delicate, and not entirely successful.
2832
2833C depends on declaration-before-use, so a forall specifier must introduce type names before they can be used in the declaration specifiers.
2834This could be done by making the forall specifier part of the declaration specifiers, or by making it a new introductory clause of declarations.
2835
2836Assertions are also part of polymorphic function types, because it must be clear which functions have access to the assertion parameters declared by the assertions.
2837All attempts to put assertions inside an introductory clause produced complex semantics and confusing code.
2838Building them into the declaration specifiers could be done by placing them in the function's parameter list, or in a forall specifier that is a declaration specifier.
2839Assertions are also used with type parameters of specifications, and by type declarations.
2840For consistency's sake it seems best to attach assertions to the type declarations in forall specifiers, which means that forall specifiers must be declaration specifiers.
2841\end{rationale}
2842%HERE
2843
2844
2845\subsection{Type qualifiers}
2846
2847\CFA defines a new type qualifier \lstinline$lvalue$\impl{lvalue}\index{lvalue}.
2848\begin{syntax}
2849\oldlhs{type-qualifier}
2850\rhs \lstinline$lvalue$
2851\end{syntax}
2852
2853\constraints
2854\lstinline$restrict$\index{register@{\lstinline$restrict$}} Types other than type parameters and pointer types whose referenced type is an object type shall not be restrict-qualified.
2855
2856\semantics
2857An object's type may be a restrict-qualified type parameter. \lstinline$restrict$ does not establish any special semantics in that case.
2858
2859\begin{rationale}
2860\CFA loosens the constraint on the restrict qualifier so that restrict-qualified pointers may be passed to polymorphic functions.
2861\end{rationale}
2862
2863\lstinline$lvalue$ may be used to qualify the return type of a function type.
2864Let \lstinline$T$ be an unqualified version of a type;
2865then the result of calling a function with return type
2866\lstinline$lvalue T$ is a \Index{modifiable lvalue} of type \lstinline$T$.
2867\lstinline$const$\use{const} and \lstinline$volatile$\use{volatile} qualifiers may also be added to indicate that the function result is a constant or volatile lvalue.
2868\begin{rationale}
2869The \lstinline$const$ and \lstinline$volatile$ qualifiers can only be sensibly used to qualify the return type of a function if the \lstinline$lvalue$ qualifier is also used.
2870\end{rationale}
2871
2872An {lvalue}-qualified type may be used in a \Index{cast expression} if the operand is an lvalue;
2873the result of the expression is an lvalue.
2874
2875\begin{rationale}
2876\lstinline$lvalue$ provides some of the functionality of {\CC}'s ``\lstinline$T&$'' ( reference to object of type \lstinline$T$) type.
2877Reference types have four uses in {\CC}.
2878\begin{itemize}
2879\item
2880They are necessary for user-defined operators that return lvalues, such as ``subscript'' and
2881``dereference''.
2882
2883\item
2884A reference can be used to define an alias for a complicated lvalue expression, as a way of getting some of the functionality of the Pascal \lstinline$with$ statement.
2885The following {\CC} code gives an example.
2886\begin{lstlisting}
2887{
2888        char &code = long_name.some_field[i].data->code;
2889        code = toupper( code );
2890}
2891\end{lstlisting}
2892This is not very useful.
2893
2894\item
2895A reference parameter can be used to allow a function to modify an argument without forcing the caller to pass the address of the argument.
2896This is most useful for user-defined assignment operators.
2897In {\CC}, plain assignment is done by a function called ``\lstinline$operator=$'', and the two expressions
2898\begin{lstlisting}
2899a = b;
2900operator=( a, b );
2901\end{lstlisting} are equivalent.
2902If \lstinline$a$ and \lstinline$b$ are of type \lstinline$T$, then the first parameter of \lstinline$operator=$ must have type ``\lstinline$T&$''.
2903It cannot have type
2904\lstinline$T$, because then assignment couldn't alter the variable, and it can't have type
2905``\lstinline$T *$'', because the assignment would have to be written ``\lstinline$&a = b;$''.
2906
2907In the case of user-defined operators, this could just as well be handled by using pointer types and by changing the rewrite rules so that ``\lstinline$a = b;$'' is equivalent to
2908``\lstinline$operator=(&( a), b )$''.
2909Reference parameters of ``normal'' functions are Bad Things, because they remove a useful property of C function calls: an argument can only be modified by a function if it is preceded by ``\lstinline$&$''.
2910
2911\item
2912References to \Index{const-qualified} types can be used instead of value parameters.  Given the
2913{\CC} function call ``\lstinline$fiddle( a_thing )$'', where the type of \lstinline$a_thing$ is
2914\lstinline$Thing$, the type of \lstinline$fiddle$ could be either of
2915\begin{lstlisting}
2916void fiddle( Thing );
2917void fiddle( const Thing & );
2918\end{lstlisting}
2919If the second form is used, then constructors and destructors are not invoked to create a temporary variable at the call site ( and it is bad style for the caller to make any assumptions about such things), and within \lstinline$fiddle$ the parameter is subject to the usual problems caused by aliases.
2920The reference form might be chosen for efficiency's sake if \lstinline$Thing$s are too large or their constructors or destructors are too expensive.
2921An implementation may switch between them without causing trouble for well-behaved clients.
2922This leaves the implementor to define ``too large'' and ``too expensive''.
2923
2924I propose to push this job onto the compiler by allowing it to implement
2925\begin{lstlisting}
2926void fiddle( const volatile Thing );
2927\end{lstlisting} with call-by-reference.
2928Since it knows all about the size of \lstinline$Thing$s and the parameter passing mechanism, it should be able to come up with a better definition of ``too large'', and may be able to make a good guess at ``too expensive''.
2929\end{itemize}
2930
2931In summary, since references are only really necessary for returning lvalues, I'll only provide lvalue functions.
2932\end{rationale}
2933
2934
2935\setcounter{subsection}{8}
2936\subsection{Initialization}
2937
2938An expression that is used as an \nonterm{initializer} is treated as being cast to the type of the object being initialized.
2939An expression used in an \nonterm{initializer-list} is treated as being cast to the type of the aggregate member that it initializes.
2940In either case the cast must have a single unambiguous \Index{interpretation}.
2941
2942
2943\setcounter{subsection}{10}
2944\subsection{Specification definitions}
2945
2946\begin{syntax}
2947\lhs{spec-definition}
2948\rhs \lstinline$spec$ \nonterm{identifier} 
2949        \lstinline$($ \nonterm{type-parameter-list} \lstinline$)$
2950        \lstinline${$ \nonterm{spec-declaration-list}\opt \lstinline$}$
2951\lhs{spec-declaration-list}
2952\rhs \nonterm{spec-declaration} \lstinline$;$
2953\rhs \nonterm{spec-declaration-list} \nonterm{spec-declaration} \lstinline$;$
2954\lhs{spec-declaration}
2955\rhs \nonterm{specifier-qualifier-list} \nonterm{declarator-list}
2956\lhs{declarator-list}
2957\rhs \nonterm{declarator}
2958\rhs \nonterm{declarator-list} \lstinline$,$ \nonterm{declarator}
2959\end{syntax}
2960\begin{rationale}
2961The declarations allowed in a specification are much the same as those allowed in a structure, except that bit fields are not allowed, and \Index{incomplete type}s and function types are allowed.
2962\end{rationale}
2963
2964\semantics
2965A \define{specification definition} defines a name for a \define{specification}: a parameterized collection of object and function declarations.
2966
2967The declarations in a specification consist of the declarations in the
2968\nonterm{spec-declaration-list} and declarations produced by any assertions in the
2969\nonterm{spec-parameter-list}.
2970If the collection contains two declarations that declare the same identifier and have compatible types, they are combined into one declaration with the composite type constructed from the two types.
2971
2972
2973\subsubsection{Assertions}
2974
2975\begin{syntax}
2976\lhs{assertion-list}
2977\rhs \nonterm{assertion}
2978\rhs \nonterm{assertion-list} \nonterm{assertion}
2979\lhs{assertion}
2980\rhs \lstinline$|$ \nonterm{identifier} \lstinline$($ \nonterm{type-name-list} \lstinline$)$
2981\rhs \lstinline$|$ \nonterm{spec-declaration}
2982\lhs{type-name-list}
2983\rhs \nonterm{type-name}
2984\rhs \nonterm{type-name-list} \lstinline$,$ \nonterm{type-name}
2985\end{syntax}
2986
2987\constraints
2988The \nonterm{identifier} in an assertion that is not a \nonterm{spec-declaration} shall be the name of a specification.
2989The \nonterm{type-name-list} shall contain one \nonterm{type-name} argument for each \nonterm{type-parameter} in that specification's \nonterm{spec-parameter-list}.
2990If the
2991\nonterm{type-parameter} uses type-class \lstinline$type$\use{type}, the argument shall be the type name of an \Index{object type};
2992if it uses \lstinline$dtype$, the argument shall be the type name of an object type or an \Index{incomplete type};
2993and if it uses \lstinline$ftype$, the argument shall be the type name of a \Index{function type}.
2994
2995\semantics
2996An \define{assertion} is a declaration of a collection of objects and functions, called
2997\define{assertion parameters}.
2998
2999The assertion parameters produced by an assertion that applies the name of a specification to type arguments are found by taking the declarations specified in the specification and treating each of the specification's parameters as a synonym for the corresponding \nonterm{type-name} argument.
3000
3001The collection of assertion parameters produced by the \nonterm{assertion-list} are found by combining the declarations produced by each assertion.
3002If the collection contains two declarations that declare the same identifier and have compatible types, they are combined into one declaration with the \Index{composite type} constructed from the two types.
3003
3004\examples
3005\begin{lstlisting}
3006forall( otype T | T ?*?( T, T ))@\use{?*?}@
3007T square( T val ) {@\impl{square}@
3008        return val + val;
3009}
3010trait summable( otype T ) {@\impl{summable}@
3011        T ?+=?( T *, T );@\use{?+=?}@
3012        const T 0;@\use{0}@
3013};
3014trait list_of( otype List, otype Element ) {@\impl{list_of}@
3015        Element car( List );
3016        List cdr( List );
3017        List cons( Element, List );
3018        List nil;
3019        int is_nil( List );
3020};
3021trait sum_list( otype List, otype Element | summable( Element ) | list_of( List, Element ) ) {};
3022\end{lstlisting}
3023\lstinline$sum_list$ contains seven declarations, which describe a list whose elements can be added up.
3024The assertion ``\lstinline$|sum_list( i_list, int )$''\use{sum_list} produces the assertion parameters
3025\begin{lstlisting}
3026int ?+=?( int *, int );
3027const int 0;
3028int car( i_list );
3029i_list cdr( i_list );
3030i_list cons( int, i_list );
3031i_list nil;
3032int is_nil;
3033\end{lstlisting}
3034
3035
3036\subsection{Type declarations}
3037
3038\begin{syntax}
3039\lhs{type-parameter-list}
3040\rhs \nonterm{type-parameter}
3041\rhs \nonterm{type-parameter-list} \lstinline$,$ \nonterm{type-parameter}
3042\lhs{type-parameter}
3043\rhs \nonterm{type-class} \nonterm{identifier} \nonterm{assertion-list}\opt
3044\lhs{type-class}
3045\rhs \lstinline$type$
3046\rhs \lstinline$dtype$
3047\rhs \lstinline$ftype$
3048\lhs{type-declaration}
3049\rhs \nonterm{storage-class-specifier}\opt \lstinline$type$ \nonterm{type-declarator-list} \verb|;|
3050\lhs{type-declarator-list}
3051\rhs \nonterm{type-declarator}
3052\rhs \nonterm{type-declarator-list} \lstinline$,$ \nonterm{type-declarator}
3053\lhs{type-declarator}
3054\rhs \nonterm{identifier} \nonterm{assertion-list}\opt \lstinline$=$ \nonterm{type-name}
3055\rhs \nonterm{identifier} \nonterm{assertion-list}\opt
3056\end{syntax}
3057
3058\constraints
3059If a type declaration has block scope, and the declared identifier has external or internal linkage, the declaration shall have no initializer for the identifier.
3060
3061\semantics
3062A \nonterm{type-parameter} or a \nonterm{type-declarator} declares an identifier to be a \Index{type name} for a type incompatible with all other types.
3063
3064An identifier declared by a \nonterm{type-parameter} has \Index{no linkage}.
3065Identifiers declared with type-class \lstinline$type$\use{type} are \Index{object type}s;
3066those declared with type-class
3067\lstinline$dtype$\use{dtype} are \Index{incomplete type}s;
3068and those declared with type-class
3069\lstinline$ftype$\use{ftype} are \Index{function type}s.
3070The identifier has \Index{block scope} that terminates at the end of the \nonterm{spec-declaration-list} or polymorphic function that contains the \nonterm{type-parameter}.
3071
3072A \nonterm{type-declarator} with an \Index{initializer} is a \define{type definition}.  The declared identifier is an \Index{incomplete type} within the initializer, and an \Index{object type} after the end of the initializer.
3073The type in the initializer is called the \define{implementation
3074  type}.
3075Within the scope of the declaration, \Index{implicit conversion}s can be performed between the defined type and the implementation type, and between pointers to the defined type and pointers to the implementation type.
3076
3077A type declaration without an \Index{initializer} and without a \Index{storage-class specifier} or with storage-class specifier \lstinline$static$\use{static} defines an \Index{incomplete type}.
3078If a
3079\Index{translation unit} or \Index{block} contains one or more such declarations for an identifier, it must contain exactly one definition of the identifier ( but not in an enclosed block, which would define a new type known only within that block).
3080\begin{rationale}
3081Incomplete type declarations allow compact mutually-recursive types.
3082\begin{lstlisting}
3083otype t1; // incomplete type declaration
3084otype t2 = struct { t1 * p; ... };
3085otype t1 = struct { t2 * p; ... };
3086\end{lstlisting}
3087Without them, mutual recursion could be handled by declaring mutually recursive structures, then initializing the types to those structures.
3088\begin{lstlisting}
3089struct s1;
3090otype t2 = struct s2 { struct s1 * p; ... };
3091otype t1 = struct s1 { struct s2 * p; ... };
3092\end{lstlisting}
3093This introduces extra names, and may force the programmer to cast between the types and their implementations.
3094\end{rationale}
3095
3096A type declaration without an initializer and with \Index{storage-class specifier}
3097\lstinline$extern$\use{extern} is an \define{opaque type declaration}.
3098Opaque types are
3099\Index{object type}s.
3100An opaque type is not a \nonterm{constant-expression};
3101neither is a structure or union that has a member whose type is not a \nonterm{constant-expression}.  Every other
3102\Index{object type} is a \nonterm{constant-expression}.
3103Objects with static storage duration shall be declared with a type that is a \nonterm{constant-expression}.
3104\begin{rationale}
3105Type declarations can declare identifiers with external linkage, whereas typedef declarations declare identifiers that only exist within a translation unit.
3106These opaque types can be used in declarations, but the implementation of the type is not visible.
3107
3108Static objects can not have opaque types because space for them would have to be allocated at program start-up.
3109This is a deficiency\index{deficiencies!static opaque objects}, but I don't want to deal with ``module initialization'' code just now.
3110\end{rationale}
3111
3112An \Index{incomplete type} which is not a qualified version\index{qualified type} of a type is a value of \Index{type-class} \lstinline$dtype$.
3113An object type\index{object types} which is not a qualified version of a type is a value of type-classes \lstinline$type$ and \lstinline$dtype$.
3114A
3115\Index{function type} is a value of type-class \lstinline$ftype$.
3116\begin{rationale}
3117Syntactically, a type value is a \nonterm{type-name}, which is a declaration for an object which omits the identifier being declared.
3118
3119Object types are precisely the types that can be instantiated.
3120Type qualifiers are not included in type values because the compiler needs the information they provide at compile time to detect illegal statements or to produce efficient machine instructions.
3121For instance, the code that a compiler must generate to manipulate an object that has volatile-qualified type may be different from the code to manipulate an ordinary object.
3122
3123Type qualifiers are a weak point of C's type system.
3124Consider the standard library function
3125\lstinline$strchr()$ which, given a string and a character, returns a pointer to the first occurrence of the character in the string.
3126\begin{lstlisting}
3127char *strchr( const char *s, int c ) {@\impl{strchr}@
3128        char real_c = c; // done because c was declared as int.
3129        for ( ; *s != real_c; s++ )
3130                if ( *s == '\0' ) return NULL;
3131        return ( char * )s;
3132}
3133\end{lstlisting}
3134The parameter \lstinline$s$ must be \lstinline$const char *$, because \lstinline$strchr()$ might be used to search a constant string, but the return type must be \lstinline$char *$, because the result might be used to modify a non-constant string.
3135Hence the body must perform a cast, and ( even worse)
3136\lstinline$strchr()$ provides a type-safe way to attempt to modify constant strings.
3137What is needed is some way to say that \lstinline$s$'s type might contain qualifiers, and the result type has exactly the same qualifiers.
3138Polymorphic functions do not provide a fix for this deficiency\index{deficiencies!pointers to qualified types}, because type qualifiers are not part of type values.
3139Instead, overloading can be used to define \lstinline$strchr()$ for each combination of qualifiers.
3140\end{rationale}
3141
3142\begin{rationale}
3143Since \Index{incomplete type}s are not type values, they can not be used as the initializer in a type declaration, or as the type of a structure or union member.
3144This prevents the declaration of types that contain each other.
3145\begin{lstlisting}
3146otype t1;
3147otype t2 = t1; // illegal: incomplete type t1
3148otype t1 = t2;
3149\end{lstlisting}
3150
3151The initializer in a file-scope declaration must be a constant expression.
3152This means type declarations can not build on opaque types, which is a deficiency\index{deficiencies!nesting opaque
3153 types}.
3154\begin{lstlisting}
3155extern otype Huge; // extended-precision integer type
3156otype Rational = struct {
3157        Huge numerator, denominator;    // illegal
3158};
3159struct Pair {
3160        Huge first, second;                             // legal
3161};
3162\end{lstlisting}
3163Without this restriction, \CFA might require ``module initialization'' code ( since
3164\lstinline$Rational$ has external linkage, it must be created before any other translation unit instantiates it), and would force an ordering on the initialization of the translation unit that defines \lstinline$Huge$ and the translation that declares \lstinline$Rational$.
3165
3166A benefit of the restriction is that it prevents the declaration in separate translation units of types that contain each other, which would be hard to prevent otherwise.
3167\begin{lstlisting}
3168//  File a.c:
3169        extern type t1;
3170        type t2 = struct { t1 f1; ... } // illegal
3171//  File b.c:
3172        extern type t2;
3173        type t1 = struct { t2 f2; ... } // illegal
3174\end{lstlisting}
3175\end{rationale}
3176
3177\begin{rationale}
3178Since a \nonterm{type-declaration} is a \nonterm{declaration} and not a
3179\nonterm{struct-declaration}, type declarations can not be structure members.
3180The form of
3181\nonterm{type-declaration} forbids arrays of, pointers to, and functions returning \lstinline$type$.
3182Hence the syntax of \nonterm{type-specifier} does not have to be extended to allow type-valued expressions.
3183It also side-steps the problem of type-valued expressions producing different values in different declarations.
3184
3185Since a type declaration is not a \nonterm{parameter-declaration}, functions can not have explicit type parameters.
3186This may be too restrictive, but it attempts to make compilation simpler.
3187Recall that when traditional C scanners read in an identifier, they look it up in the symbol table to determine whether or not it is a typedef name, and return a ``type'' or ``identifier'' token depending on what they find.
3188A type parameter would add a type name to the current scope.
3189The scope manipulations involved in parsing the declaration of a function that takes function pointer parameters and returns a function pointer may just be too complicated.
3190
3191Explicit type parameters don't seem to be very useful, anyway, because their scope would not include the return type of the function.
3192Consider the following attempt to define a type-safe memory allocation function.
3193\begin{lstlisting}
3194#include <stdlib.h>
3195T * new( otype T ) { return ( T * )malloc( sizeof( T) ); };
3196@\ldots@ int * ip = new( int );
3197\end{lstlisting}
3198This looks sensible, but \CFA's declaration-before-use rules mean that ``\lstinline$T$'' in the function body refers to the parameter, but the ``\lstinline$T$'' in the return type refers to the meaning of \lstinline$T$ in the scope that contains \lstinline$new$;
3199it could be undefined, or a type name, or a function or variable name.
3200Nothing good can result from such a situation.
3201\end{rationale}
3202
3203\examples
3204Since type declarations create new types, instances of types are always passed by value.
3205\begin{lstlisting}
3206otype A1 = int[2];
3207void f1( A1 a ) { a[0] = 0; };
3208otypedef int A2[2];
3209void f2( A2 a ) { a[0] = 0; };
3210A1 v1;
3211A2 v2;
3212f1( v1 );
3213f2( v2 );
3214\end{lstlisting}
3215\lstinline$V1$ is passed by value, so \lstinline$f1()$'s assignment to \lstinline$a[0]$ does not modify v1.  \lstinline$V2$ is converted to a pointer, so \lstinline$f2()$ modifies \lstinline$v2[0]$.
3216
3217A translation unit containing the declarations
3218\begin{lstlisting}
3219extern type Complex;@\use{Complex}@ // opaque type declaration
3220extern float abs( Complex );@\use{abs}@
3221\end{lstlisting} can contain declarations of complex numbers, which can be passed to \lstinline$abs$.
3222Some other translation unit must implement \lstinline$Complex$ and \lstinline$abs$.
3223That unit might contain the declarations
3224\begin{lstlisting}
3225otype Complex = struct { float re, im; };@\impl{Complex}@
3226Complex cplx_i = { 0.0, 1.0 };@\impl{cplx_i}@
3227float abs( Complex c ) {@\impl{abs( Complex )}@
3228        return sqrt( c.re * c.re + c.im * c.im );
3229}
3230\end{lstlisting}
3231Note that \lstinline$c$ is implicitly converted to a \lstinline$struct$ so that its components can be retrieved.
3232
3233\begin{lstlisting}
3234otype Time_of_day = int;@\impl{Time_of_day}@ // seconds since midnight.
3235Time_of_day ?+?( Time_of_day t1, int seconds ) {@\impl{?+?}@
3236        return (( int)t1 + seconds ) % 86400;
3237}
3238\end{lstlisting}
3239\lstinline$t1$ must be cast to its implementation type to prevent infinite recursion.
3240
3241\begin{rationale}
3242Within the scope of a type definition, an instance of the type can be viewed as having that type or as having the implementation type.
3243In the \lstinline$Time_of_day$ example, the difference is important.
3244Different languages have treated the distinction between the abstraction and the implementation in different ways.
3245\begin{itemize}
3246\item
3247Inside a Clu cluster \cite{CLU}, the declaration of an instance states which view applies.
3248Two primitives called \lstinline$up$ and \lstinline$down$ can be used to convert between the views.
3249\item
3250The Simula class \cite{SIMULA87} is essentially a record type.
3251Since the only operations on a record are member selection and assignment, which can not be overloaded, there is never any ambiguity as to whether the abstraction or the implementation view is being used.
3252In {\CC}
3253\cite{C++}, operations on class instances include assignment and ``\lstinline$&$'', which can be overloaded.
3254A ``scope resolution'' operator can be used inside the class to specify whether the abstract or implementation version of the operation should be used.
3255\item
3256An Ada derived type definition \cite{Ada} creates a new type from an old type, and also implicitly declares derived subprograms that correspond to the existing subprograms that use the old type as a parameter type or result type.
3257The derived subprograms are clones of the existing subprograms with the old type replaced by the derived type.
3258Literals and aggregates of the old type are also cloned.
3259In other words, the abstract view provides exactly the same operations as the implementation view.
3260This allows the abstract view to be used in all cases.
3261
3262The derived subprograms can be replaced by programmer-specified subprograms.
3263This is an exception to the normal scope rules, which forbid duplicate definitions of a subprogram in a scope.
3264In this case, explicit conversions between the derived type and the old type can be used.
3265\end{itemize}
3266\CFA's rules are like Clu's, except that implicit conversions and conversion costs allow it to do away with most uses of \lstinline$up$ and \lstinline$down$.
3267\end{rationale}
3268
3269
3270\subsubsection{Default functions and objects}
3271
3272A declaration\index{type declaration} of a type identifier \lstinline$T$ with type-class
3273\lstinline$type$ implicitly declares a \define{default assignment} function
3274\lstinline$T ?=?( T *, T )$\use{?=?}, with the same \Index{scope} and \Index{linkage} as the identifier \lstinline$T$.
3275\begin{rationale}
3276Assignment is central to C's imperative programming style, and every existing C object type has assignment defined for it ( except for array types, which are treated as pointer types for purposes of assignment).
3277Without this rule, nearly every inferred type parameter would need an accompanying assignment assertion parameter.
3278If a type parameter should not have an assignment operation,
3279\lstinline$dtype$ should be used.
3280If a type should not have assignment defined, the user can define an assignment function that causes a run-time error, or provide an external declaration but no definition and thus cause a link-time error.
3281\end{rationale}
3282
3283A definition\index{type definition} of a type identifier \lstinline$T$ with \Index{implementation type} \lstinline$I$ and type-class \lstinline$type$ implicitly defines a default assignment function.
3284A definition\index{type definition} of a type identifier \lstinline$T$ with implementation type \lstinline$I$ and an assertion list implicitly defines \define{default function}s and
3285\define{default object}s as declared by the assertion declarations.
3286The default objects and functions have the same \Index{scope} and \Index{linkage} as the identifier \lstinline$T$.
3287Their values are determined as follows:
3288\begin{itemize}
3289\item
3290If at the definition of \lstinline$T$ there is visible a declaration of an object with the same name as the default object, and if the type of that object with all occurrence of \lstinline$I$ replaced by \lstinline$T$ is compatible with the type of the default object, then the default object is initialized with that object.
3291Otherwise the scope of the declaration of \lstinline$T$ must contain a definition of the default object.
3292
3293\item 
3294If at the definition of \lstinline$T$ there is visible a declaration of a function with the same name as the default function, and if the type of that function with all occurrence of \lstinline$I$ replaced by \lstinline$T$ is compatible with the type of the default function, then the default function calls that function after converting its arguments and returns the converted result.
3295
3296Otherwise, if \lstinline$I$ contains exactly one anonymous member\index{anonymous member} such that at the definition of \lstinline$T$ there is visible a declaration of a function with the same name as the default function, and the type of that function with all occurrences of the anonymous member's type in its parameter list replaced by \lstinline$T$ is compatible with the type of the default function, then the default function calls that function after converting its arguments and returns the result.
3297
3298Otherwise the scope of the declaration of \lstinline$T$ must contain a definition of the default function.
3299\end{itemize}
3300\begin{rationale}
3301Note that a pointer to a default function will not compare as equal to a pointer to the inherited function.
3302\end{rationale}
3303
3304A function or object with the same type and name as a default function or object that is declared within the scope of the definition of \lstinline$T$ replaces the default function or object.
3305
3306\examples
3307\begin{lstlisting}
3308trait s( otype T ) {
3309        T a, b;
3310} struct impl { int left, right; } a = { 0, 0 };
3311otype Pair | s( Pair ) = struct impl;
3312Pair b = { 1, 1 };
3313\end{lstlisting}
3314The definition of \lstinline$Pair$ implicitly defines two objects \lstinline$a$ and \lstinline$b$.
3315\lstinline$Pair a$ inherits its value from the \lstinline$struct impl a$.
3316The definition of
3317\lstinline$Pair b$ is compulsory because there is no \lstinline$struct impl b$ to construct a value from.
3318\begin{lstlisting}
3319trait ss( otype T ) {
3320        T clone( T );
3321        void munge( T * );
3322}
3323otype Whatsit | ss( Whatsit );@\use{Whatsit}@
3324otype Doodad | ss( Doodad ) = struct doodad {@\use{Doodad}@
3325        Whatsit; // anonymous member
3326        int extra;
3327};
3328Doodad clone( Doodad ) { ... }
3329\end{lstlisting}
3330The definition of \lstinline$Doodad$ implicitly defines three functions:
3331\begin{lstlisting}
3332Doodad ?=?( Doodad *, Doodad );
3333Doodad clone( Doodad );
3334void munge( Doodad * );
3335\end{lstlisting}
3336The assignment function inherits \lstinline$struct doodad$'s assignment function because the types match when \lstinline$struct doodad$ is replaced by \lstinline$Doodad$ throughout.
3337\lstinline$munge()$ inherits \lstinline$Whatsit$'s \lstinline$munge()$ because the types match when
3338\lstinline$Whatsit$ is replaced by \lstinline$Doodad$ in the parameter list. \lstinline$clone()$ does \emph{not} inherit \lstinline$Whatsit$'s \lstinline$clone()$: replacement in the parameter list yields ``\lstinline$Whatsit clone( Doodad )$'', which is not compatible with
3339\lstinline$Doodad$'s \lstinline$clone()$'s type.
3340Hence the definition of
3341``\lstinline$Doodad clone( Doodad )$'' is necessary.
3342
3343Default functions and objects are subject to the normal scope rules.
3344\begin{lstlisting}
3345otype T = @\ldots@;
3346T a_T = @\ldots@;               // Default assignment used.
3347T ?=?( T *, T );
3348T a_T = @\ldots@;               // Programmer-defined assignment called.
3349\end{lstlisting}
3350\begin{rationale}
3351A compiler warning would be helpful in this situation.
3352\end{rationale}
3353
3354\begin{rationale}
3355The \emph{class} construct of object-oriented programming languages performs three independent functions.
3356It \emph{encapsulates} a data structure;
3357it defines a \emph{subtype} relationship, whereby instances of one class may be used in contexts that require instances of another;
3358and it allows one class to \emph{inherit} the implementation of another.
3359
3360In \CFA, encapsulation is provided by opaque types and the scope rules, and subtyping is provided by specifications and assertions.
3361Inheritance is provided by default functions and objects.
3362\end{rationale}
3363
3364
3365\section{Statements and blocks}
3366
3367\begin{syntax}
3368\oldlhs{statement}
3369\rhs \nonterm{exception-statement}
3370\end{syntax}
3371
3372Many statements contain expressions, which may have more than one interpretation.
3373The following sections describe how the \CFA translator selects an interpretation.
3374In all cases the result of the selection shall be a single unambiguous \Index{interpretation}.
3375
3376
3377\subsection{Labeled statements}
3378
3379\begin{syntax}
3380\oldlhs{labeled-statement}
3381\rhs \lstinline$case$ \nonterm{case-value-list} : \nonterm{statement}
3382\lhs{case-value-list}
3383\rhs \nonterm{case-value}
3384\rhs \nonterm{case-value-list} \lstinline$,$ \nonterm{case-value}
3385\lhs{case-value}
3386\rhs \nonterm{constant-expression}
3387\rhs \nonterm{subrange}
3388\lhs{subrange}
3389\rhs \nonterm{constant-expression} \lstinline$~$ \nonterm{constant-expression}
3390\end{syntax}
3391
3392The following have identical meaning:
3393\begin{lstlisting}
3394case 1:  case 2:  case 3:  case 4:  case 5:
3395case 1, 2, 3, 4, 5:
3396case 1~5:
3397\end{lstlisting}
3398Multiple subranges are allowed:
3399\begin{lstlisting}
3400case 1~4, 9~14, 27~32:
3401\end{lstlisting}
3402The \lstinline$case$ and \lstinline$default$ clauses are restricted within the \lstinline$switch$ and \lstinline$choose$ statements, precluding Duff's device.
3403
3404
3405\subsection{Expression and null statements}
3406
3407The expression in an expression statement is treated as being cast to \lstinline$void$.
3408
3409
3410\subsection{Selection statements}
3411
3412\begin{syntax}
3413\oldlhs{selection-statement}
3414\rhs \lstinline$choose$ \lstinline$($ \nonterm{expression} \lstinline$)$ \nonterm{statement}
3415\end{syntax}
3416
3417The controlling expression \lstinline$E$ in the \lstinline$switch$ and \lstinline$choose$ statement:
3418\begin{lstlisting}
3419switch ( E ) ...
3420choose ( E ) ...
3421\end{lstlisting} may have more than one interpretation, but it shall have only one interpretation with an integral type.
3422An \Index{integer promotion} is performed on the expression if necessary.
3423The constant expressions in \lstinline$case$ statements with the switch are converted to the promoted type.
3424
3425
3426\setcounter{subsubsection}{3}
3427\subsubsection{The \lstinline$choose$ statement}
3428
3429The \lstinline$choose$ statement is the same as the \lstinline$switch$ statement except control transfers to the end of the \lstinline$choose$ statement at a \lstinline$case$ or \lstinline$default$ labeled statement.
3430The \lstinline$fallthru$ statement is used to fall through to the next \lstinline$case$ or \lstinline$default$ labeled statement.
3431The following have identical meaning:
3432\begin{flushleft}
3433\begin{tabular}{@{\hspace{2em}}l@{\hspace{2em}}l@{}}
3434\begin{lstlisting}
3435switch (...) {
3436  case 1: ... ; break;
3437  case 2: ... ; break;
3438  case 3: ... ; // fall through
3439  case 4: ... ; // fall through
3440  default: ... break;
3441}
3442\end{lstlisting}
3443&
3444\begin{lstlisting}
3445choose (...) {
3446  case 1: ... ; // exit
3447  case 2: ... ; // exit
3448  case 3: ... ; fallthru;
3449  case 4: ... ; fallthru;
3450  default: ... ; // exit
3451}
3452\end{lstlisting}
3453\end{tabular}
3454\end{flushleft}
3455The \lstinline$choose$ statement addresses the problem of accidental fall-through associated with the \lstinline$switch$ statement.
3456
3457
3458\subsection{Iteration statements}
3459
3460The controlling expression \lstinline$E$ in the loops
3461\begin{lstlisting}
3462if ( E ) ...
3463while ( E ) ...
3464do ... while ( E );
3465\end{lstlisting} is treated as ``\lstinline$( int )((E)!=0)$''.
3466
3467The statement
3468\begin{lstlisting}
3469for ( a; b; c ) @\ldots@
3470\end{lstlisting} is treated as
3471\begin{lstlisting}
3472for ( ( void )( a ); ( int )(( b )!=0); ( void )( c ) ) ...
3473\end{lstlisting}
3474
3475
3476\subsection{Jump statements}
3477
3478\begin{syntax}
3479\oldlhs{jump-statement}
3480\rhs \lstinline$continue$ \nonterm{identifier}\opt
3481\rhs \lstinline$break$ \nonterm{identifier}\opt
3482\rhs \ldots
3483\rhs \lstinline$throw$ \nonterm{assignment-expression}\opt
3484\rhs \lstinline$throwResume$ \nonterm{assignment-expression}\opt \nonterm{at-expression}\opt
3485\lhs{at-expression} \lstinline$_At$ \nonterm{assignment-expression}
3486\end{syntax}
3487
3488Labeled \lstinline$continue$ and \lstinline$break$ allow useful but restricted control-flow that reduces the need for the \lstinline$goto$ statement for exiting multiple nested control-structures.
3489\begin{lstlisting}
3490L1: {                                                   // compound
3491  L2: switch ( ... ) {                  // switch
3492          case ...:
3493          L3: for ( ;; ) {                      // outer for
3494                L4: for ( ;; ) {                // inner for
3495                                continue L1;    // error: not enclosing iteration
3496                                continue L2;    // error: not enclosing iteration
3497                                continue L3;    // next iteration of outer for
3498                                continue L4;    // next iteration of inner for
3499                                break L1;               // exit compound
3500                                break L2;               // exit switch
3501                                break L3;               // exit outer for
3502                                break L4;               // exit inner for
3503                        } // for
3504                } // for
3505                break;                                  // exit switch
3506          default:
3507                break L1;                               // exit compound
3508        } // switch
3509        ...
3510} // compound
3511\end{lstlisting}
3512
3513
3514\setcounter{subsubsection}{1}
3515\subsubsection{The \lstinline$continue$ statement}
3516
3517The identifier in a \lstinline$continue$ statement shall name a label located on an enclosing iteration statement.
3518
3519
3520\subsubsection{The \lstinline$break$ statement}
3521
3522The identifier in a \lstinline$break$ statement shall name a label located on an enclosing compound, selection or iteration statement.
3523
3524
3525\subsubsection{The \lstinline$return$ statement}
3526
3527An expression in a \lstinline$return$ statement is treated as being cast to the result type of the function.
3528
3529
3530\subsubsection{The \lstinline$throw$ statement}
3531
3532When an exception is raised, \Index{propagation} directs control from a raise in the source execution to a handler in the faulting execution.
3533
3534
3535\subsubsection{The \lstinline$throwResume$ statement}
3536
3537
3538\subsection{Exception statements}
3539
3540\begin{syntax}
3541\lhs{exception-statement}
3542\rhs \lstinline$try$ \nonterm{compound-statement} \nonterm{handler-list}
3543\rhs \lstinline$try$ \nonterm{compound-statement} \nonterm{finally-clause}
3544\rhs \lstinline$try$ \nonterm{compound-statement} \nonterm{handler-list} \nonterm{finally-clause}
3545\lhs{handler-list}
3546\rhs \nonterm{handler-clause}
3547\rhs \lstinline$catch$ \lstinline$($ \ldots \lstinline$)$ \nonterm{compound-statement}
3548\rhs \nonterm{handler-clause} \lstinline$catch$ \lstinline$($ \ldots \lstinline$)$ \nonterm{compound-statement}
3549\rhs \lstinline$catchResume$ \lstinline$($ \ldots \lstinline$)$ \nonterm{compound-statement}
3550\rhs \nonterm{handler-clause} \lstinline$catchResume$ \lstinline$($ \ldots \lstinline$)$ \nonterm{compound-statement}
3551\lhs{handler-clause}
3552\rhs \lstinline$catch$ \lstinline$($ \nonterm{exception-declaration} \lstinline$)$ \nonterm{compound-statement}
3553\rhs \nonterm{handler-clause} \lstinline$catch$ \lstinline$($ \nonterm{exception-declaration} \lstinline$)$ \nonterm{compound-statement}
3554\rhs \lstinline$catchResume$ \lstinline$($ \nonterm{exception-declaration} \lstinline$)$ \nonterm{compound-statement}
3555\rhs \nonterm{handler-clause} \lstinline$catchResume$ \lstinline$($ \nonterm{exception-declaration} \lstinline$)$ \nonterm{compound-statement}
3556\lhs{finally-clause}
3557\rhs \lstinline$finally$ \nonterm{compound-statement}
3558\lhs{exception-declaration}
3559\rhs \nonterm{type-specifier}
3560\rhs \nonterm{type-specifier} \nonterm{declarator}
3561\rhs \nonterm{type-specifier} \nonterm{abstract-declarator}
3562\rhs \nonterm{new-abstract-declarator-tuple} \nonterm{identifier}
3563\rhs \nonterm{new-abstract-declarator-tuple}
3564\lhs{asynchronous-statement}
3565\rhs \lstinline$enable$ \nonterm{identifier-list} \nonterm{compound-statement}
3566\rhs \lstinline$disable$ \nonterm{identifier-list} \nonterm{compound-statement}
3567\end{syntax}
3568
3569\Index{Exception statement}s allow a dynamic call to a handler for \Index{recovery} (\Index{termination}) or \Index{correction} (\Index{resumption}) of an \Index{abnormal event}.
3570
3571
3572\subsubsection{The \lstinline$try$ statement}
3573
3574The \lstinline$try$ statement is a block with associated handlers, called a \Index{guarded block};
3575all other blocks are \Index{unguarded block}s.
3576A \lstinline$goto$, \lstinline$break$, \lstinline$return$, or \lstinline$continue$ statement can be used to transfer control out of a try block or handler, but not into one.
3577
3578
3579\subsubsection{The \lstinline$enable$/\lstinline$disable$ statements}
3580
3581The \lstinline$enable$/\lstinline$disable$ statements toggle delivery of \Index{asynchronous exception}s.
3582
3583
3584\setcounter{section}{9}
3585\section{Preprocessing directives}
3586
3587
3588\setcounter{subsection}{7}
3589\subsection{Predefined macro names}
3590
3591The implementation shall define the macro names \lstinline$__LINE__$, \lstinline$__FILE__$,
3592\lstinline$__DATE__$, and \lstinline$__TIME__$, as in the {\c11} standard.
3593It shall not define the macro name \lstinline$__STDC__$.
3594
3595In addition, the implementation shall define the macro name \lstinline$__CFORALL__$ to be the decimal constant 1.
3596
3597
3598\appendix
3599
3600
3601\chapter{Examples}
3602
3603
3604\section{C types}
3605This section gives example specifications for some groups of types that are important in the C language, in terms of the predefined operations that can be applied to those types.
3606
3607
3608\subsection{Scalar, arithmetic, and integral types}
3609
3610The pointer, integral, and floating-point types are all \define{scalar types}.
3611All of these types can be logically negated and compared.
3612The assertion ``\lstinline$scalar( Complex )$'' should be read as ``type \lstinline$Complex$ is scalar''.
3613\begin{lstlisting}
3614trait scalar( otype T ) {@\impl{scalar}@
3615        int !?( T );
3616        int ?<?( T, T ), ?<=?( T, T ), ?==?( T, T ), ?>=?( T, T ), ?>?( T, T ), ?!=?( T, T );
3617};
3618\end{lstlisting}
3619
3620The integral and floating-point types are \define{arithmetic types}, which support the basic arithmetic operators.
3621The use of an assertion in the \nonterm{spec-parameter-list} declares that, in order to be arithmetic, a type must also be scalar ( and hence that scalar operations are available ).
3622This is equivalent to inheritance of specifications.
3623\begin{lstlisting}
3624trait arithmetic( otype T | scalar( T ) ) {@\impl{arithmetic}@@\use{scalar}@
3625        T +?( T ), -?( T );
3626        T ?*?( T, T ), ?/?( T, T ), ?+?( T, T ), ?-?( T, T );
3627};
3628\end{lstlisting}
3629
3630The various flavors of \lstinline$char$ and \lstinline$int$ and the enumerated types make up the
3631\define{integral types}.
3632\begin{lstlisting}
3633trait integral( otype T | arithmetic( T ) ) {@\impl{integral}@@\use{arithmetic}@
3634        T ~?( T );
3635        T ?&?( T, T ), ?|?( T, T ), ?^?( T, T );
3636        T ?%?( T, T );
3637        T ?<<?( T, T ), ?>>?( T, T );
3638};
3639\end{lstlisting}
3640
3641
3642\subsection{Modifiable types}
3643\index{modifiable lvalue}
3644
3645The only operation that can be applied to all modifiable lvalues is simple assignment.
3646\begin{lstlisting}
3647trait m_lvalue( otype T ) {@\impl{m_lvalue}@
3648        T ?=?( T *, T );
3649};
3650\end{lstlisting}
3651
3652Modifiable scalar lvalues are scalars and are modifiable lvalues, and assertions in the
3653\nonterm{spec-parameter-list} reflect those relationships.
3654This is equivalent to multiple inheritance of specifications.
3655Scalars can also be incremented and decremented.
3656\begin{lstlisting}
3657trait m_l_scalar( otype T | scalar( T ) | m_lvalue( T ) ) {@\impl{m_l_scalar}@
3658        T ?++( T * ), ?--( T * );@\use{scalar}@@\use{m_lvalue}@
3659        T ++?( T * ), --?( T * );
3660};
3661\end{lstlisting}
3662
3663Modifiable arithmetic lvalues are both modifiable scalar lvalues and arithmetic.
3664Note that this results in the ``inheritance'' of \lstinline$scalar$ along both paths.
3665\begin{lstlisting}
3666trait m_l_arithmetic( otype T | m_l_scalar( T ) | arithmetic( T ) ) {@\impl{m_l_arithmetic}@
3667        T ?/=?( T *, T ), ?*=?( T *, T );@\use{m_l_scalar}@@\use{arithmetic}@
3668        T ?+=?( T *, T ), ?-=?( T *, T );
3669};
3670trait m_l_integral( otype T | m_l_arithmetic( T ) | integral( T ) ) {@\impl{m_l_integral}@
3671        T ?&=?( T *, T ), ?|=?( T *, T ), ?^=?( T *, T );@\use{m_l_arithmetic}@
3672        T ?%=?( T *, T ), ?<<=?( T *, T ), ?>>=?( T *, T );@\use{integral}@
3673};
3674\end{lstlisting}
3675
3676
3677\subsection{Pointer and array types}
3678
3679Array types can barely be said to exist in {\c11}, since in most cases an array name is treated as a constant pointer to the first element of the array, and the subscript expression
3680``\lstinline$a[i]$'' is equivalent to the dereferencing expression ``\lstinline$(*( a+( i )))$''.
3681Technically, pointer arithmetic and pointer comparisons other than ``\lstinline$==$'' and
3682``\lstinline$!=$'' are only defined for pointers to array elements, but the type system does not enforce those restrictions.
3683Consequently, there is no need for a separate ``array type'' specification.
3684
3685Pointer types are scalar types.
3686Like other scalar types, they have ``\lstinline$+$'' and
3687``\lstinline$-$'' operators, but the types do not match the types of the operations in
3688\lstinline$arithmetic$, so these operators cannot be consolidated in \lstinline$scalar$.
3689\begin{lstlisting}
3690trait pointer( type P | scalar( P ) ) {@\impl{pointer}@@\use{scalar}@
3691        P ?+?( P, long int ), ?+?( long int, P ), ?-?( P, long int );
3692        ptrdiff_t ?-?( P, P );
3693};
3694trait m_l_pointer( type P | pointer( P ) | m_l_scalar( P ) ) {@\impl{m_l_pointer}@
3695        P ?+=?( P *, long int ), ?-=?( P *, long int );
3696        P ?=?( P *, void * );
3697        void * ?=?( void **, P );
3698};
3699\end{lstlisting}
3700
3701Specifications that define the dereference operator ( or subscript operator ) require two parameters, one for the pointer type and one for the pointed-at ( or element ) type.
3702Different specifications are needed for each set of \Index{type qualifier}s, because qualifiers are not included in types.
3703The assertion ``\lstinline$|ptr_to( Safe_pointer, int )$'' should be read as
3704``\lstinline$Safe_pointer$ acts like a pointer to \lstinline$int$''.
3705\begin{lstlisting}
3706trait ptr_to( otype P | pointer( P ), otype T ) {@\impl{ptr_to}@@\use{pointer}@
3707        lvalue T *?( P );
3708        lvalue T ?[?]( P, long int );
3709};
3710trait ptr_to_const( otype P | pointer( P ), otype T ) {@\impl{ptr_to_const}@
3711        const lvalue T *?( P );
3712        const lvalue T ?[?]( P, long int );@\use{pointer}@
3713};
3714trait ptr_to_volatile( otype P | pointer( P ), otype T ) }@\impl{ptr_to_volatile}@
3715        volatile lvalue T *?( P );
3716        volatile lvalue T ?[?]( P, long int );@\use{pointer}@
3717};
3718trait ptr_to_const_volatile( otype P | pointer( P ), otype T ) }@\impl{ptr_to_const_volatile}@
3719        const volatile lvalue T *?( P );@\use{pointer}@
3720        const volatile lvalue T ?[?]( P, long int );
3721};
3722\end{lstlisting}
3723
3724Assignment to pointers is more complicated than is the case with other types, because the target's type can have extra type qualifiers in the pointed-at type: a ``\lstinline$T *$'' can be assigned to a ``\lstinline$const T *$'', a ``\lstinline$volatile T *$'', and a ``\lstinline$const volatile T *$''.
3725Again, the pointed-at type is passed in, so that assertions can connect these specifications to the
3726``\lstinline$ptr_to$'' specifications.
3727\begin{lstlisting}
3728trait m_l_ptr_to( otype P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to}@ otype T | ptr_to( P, T )@\use{ptr_to}@ {
3729        P ?=?( P *, T * );
3730        T * ?=?( T **, P );
3731};
3732trait m_l_ptr_to_const( otype P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to_const}@ otype T | ptr_to_const( P, T )@\use{ptr_to_const}@) {
3733        P ?=?( P *, const T * );
3734        const T * ?=?( const T **, P );
3735};
3736trait m_l_ptr_to_volatile( otype P | m_l_pointer( P ),@\use{m_l_pointer}@@\impl{m_l_ptr_to_volatile}@ otype T | ptr_to_volatile( P, T )) {@\use{ptr_to_volatile}@
3737        P ?=?( P *, volatile T * );
3738        volatile T * ?=?( volatile T **, P );
3739};
3740trait m_l_ptr_to_const_volatile( otype P | ptr_to_const_volatile( P ),@\use{ptr_to_const_volatile}@@\impl{m_l_ptr_to_const_volatile}@
3741                otype T | m_l_ptr_to_volatile( P, T ) | m_l_ptr_to_const( P )) {@\use{m_l_ptr_to_const}@@\use{m_l_ptr_to_volatile}@
3742        P ?=?( P *, const volatile T * );
3743        const volatile T * ?=?( const volatile T **, P );
3744};
3745\end{lstlisting}
3746
3747Note the regular manner in which type qualifiers appear in those specifications.
3748An alternative specification can make use of the fact that qualification of the pointed-at type is part of a pointer type to capture that regularity.
3749\begin{lstlisting}
3750trait m_l_ptr_like( type MyP | m_l_pointer( MyP ),@\use{m_l_pointer}@@\impl{m_l_ptr_like}@ type CP | m_l_pointer( CP ) ) {
3751        MyP ?=?( MyP *, CP );
3752        CP ?=?( CP *, MyP );
3753};
3754\end{lstlisting}
3755The assertion ``\lstinline$| m_l_ptr_like( Safe_ptr, const int * )$'' should be read as
3756``\lstinline$Safe_ptr$ is a pointer type like \lstinline$const int *$''.
3757This specification has two defects, compared to the original four: there is no automatic assertion that dereferencing a
3758\lstinline$MyP$ produces an lvalue of the type that \lstinline$CP$ points at, and the
3759``\lstinline$|m_l_pointer( CP )$'' assertion provides only a weak assurance that the argument passed to \lstinline$CP$ really is a pointer type.
3760
3761
3762\section{Relationships between operations}
3763
3764Different operators often have related meanings;
3765for instance, in C, ``\lstinline$+$'',
3766``\lstinline$+=$'', and the two versions of ``\lstinline$++$'' perform variations of addition.
3767Languages like {\CC} and Ada allow programmers to define operators for new types, but do not require that these relationships be preserved, or even that all of the operators be implemented.
3768Completeness and consistency is left to the good taste and discretion of the programmer.
3769It is possible to encourage these attributes by providing generic operator functions, or member functions of abstract classes, that are defined in terms of other, related operators.
3770
3771In \CFA, polymorphic functions provide the equivalent of these generic operators, and specifications explicitly define the minimal implementation that a programmer should provide.
3772This section shows a few examples.
3773
3774
3775\subsection{Relational and equality operators}
3776
3777The different comparison operators have obvious relationships, but there is no obvious subset of the operations to use in the implementation of the others.
3778However, it is usually convenient to implement a single comparison function that returns a negative integer, 0, or a positive integer if its first argument is respectively less than, equal to, or greater than its second argument;
3779the library function \lstinline$strcmp$ is an example.
3780
3781C and \CFA have an extra, non-obvious comparison operator: ``\lstinline$!$'', logical negation, returns 1 if its operand compares equal to 0, and 0 otherwise.
3782\begin{lstlisting}
3783trait comparable( otype T ) {
3784        const T 0;
3785        int compare( T, T );
3786}
3787forall( otype T | comparable( T ) ) int ?<?( T l, T r ) {
3788        return compare( l, r ) < 0;
3789}
3790// ... similarly for <=, ==, >=, >, and !=.
3791forall( otype T | comparable( T ) ) int !?( T operand ) {
3792        return !compare( operand, 0 );
3793}
3794\end{lstlisting}
3795
3796
3797\subsection{Arithmetic and integer operations}
3798
3799A complete arithmetic type would provide the arithmetic operators and the corresponding assignment operators.
3800Of these, the assignment operators are more likely to be implemented directly, because it is usually more efficient to alter the contents of an existing object than to create and return a new one.
3801Similarly, a complete integral type would provide integral operations based on integral assignment operations.
3802\begin{lstlisting}
3803trait arith_base( otype T ) {
3804        const T 1;
3805        T ?+=?( T *, T ), ?-=?( T *, T ), ?*=?( T *, T ), ?/=?( T *, T );
3806}
3807forall( otype T | arith_base( T ) ) T ?+?( T l, T r ) {
3808        return l += r;
3809}
3810forall( otype T | arith_base( T ) ) T ?++( T * operand ) {
3811        T temporary = *operand;
3812        *operand += 1;
3813        return temporary;
3814}
3815forall( otype T | arith_base( T ) ) T ++?( T * operand ) {
3816        return *operand += 1;
3817}
3818// ... similarly for -, --, *, and /.
3819trait int_base( otype T ) {
3820        T ?&=?( T *, T ), ?|=?( T *, T ), ?^=?( T *, T );
3821        T ?%=?( T *, T ), ?<<=?( T *, T ), ?>>=?( T *, T );
3822}
3823forall( otype T | int_base( T ) ) T ?&?( T l, T r ) {
3824        return l &= r;
3825}
3826// ... similarly for |, ^, %, <<, and >>.
3827\end{lstlisting}
3828
3829Note that, although an arithmetic type would certainly provide comparison functions, and an integral type would provide arithmetic operations, there does not have to be any relationship among
3830\lstinline$int_base$, \lstinline$arith_base$ and \lstinline$comparable$.
3831Note also that these declarations provide guidance and assistance, but they do not define an absolutely minimal set of requirements.
3832A truly minimal implementation of an arithmetic type might only provide
3833\lstinline$0$, \lstinline$1$, and \lstinline$?-=?$, which would be used by polymorphic
3834\lstinline$?+=?$, \lstinline$?*=?$, and \lstinline$?/=?$ functions.
3835
3836Note also that \lstinline$short$ is an integer type in C11 terms, but has no operations!
3837
3838
3839\chapter{TODO}
3840Review index entries.
3841
3842Restrict allowed to qualify anything, or type/dtype parameters, but only affects pointers.
3843This gets into \lstinline$noalias$ territory.
3844Qualifying anything (``\lstinline$short restrict rs$'') means pointer parameters of \lstinline$?++$, etc, would need restrict qualifiers.
3845
3846Enumerated types.
3847Constants are not ints.
3848Overloading.
3849Definition should be ``representable as an integer type'', not ``as an int''.
3850C11 usual conversions freely convert to and from ordinary integer types via assignment, which works between any integer types.
3851Does enum Color ?*?( enum
3852Color, enum Color ) really make sense? ?++ does, but it adds (int)1.
3853
3854Operators on {,signed,unsigned} char and other small types. ?<? harmless;
3855?*? questionable for chars.
3856Generic selections make these choices visible.
3857Safe conversion operators? Predefined
3858``promotion'' function?
3859
3860\lstinline$register$ assignment might be handled as assignment to a temporary with copying back and forth, but copying must not be done by assignment.
3861
3862Don't use ptrdiff\_t by name in the predefineds.
3863
3864Polymorphic objects.
3865Polymorphic typedefs and type declarations.
3866
3867
3868\bibliographystyle{plain}
3869\bibliography{cfa}
3870
3871
3872\addcontentsline{toc}{chapter}{\indexname} % add index name to table of contents
3873\begin{theindex}
3874Italic page numbers give the location of the main entry for the referenced term.
3875Plain page numbers denote uses of the indexed term.
3876Entries for grammar non-terminals are italicized.
3877A typewriter font is used for grammar terminals and program identifiers.
3878\indexspace
3879\input{refrat.ind}
3880\end{theindex}
3881
3882\end{document}
3883
3884% Local Variables: %
3885% tab-width: 4 %
3886% fill-column: 100 %
3887% compile-command: "make" %
3888% End: %
Note: See TracBrowser for help on using the repository browser.