source: doc/refrat/refrat.tex @ f739788

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f739788 was c8771e9, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

many updates

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