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