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