source: doc/refrat/refrat.tex@ 888cbe4

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 888cbe4 was 9724df0, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

update latex macros, and user and refrat manuals

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