source: doc/refrat/refrat.tex @ 4328016

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationresolv-newwith_gc
Last change on this file since 4328016 was 4096de0, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

write pointer/reference section, update LaTeX macros

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