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