Debian Codegraph @

source_2005/04b.mss

raw
1 @Part(04, Root="ada.mss")
2
3 @Comment{$Date: 2006/10/18 00:25:24 $}
4
5 @Comment{$Source: e:\\cvsroot/ARM/Source/04b.mss,v $}
6 @Comment{$Revision: 1.33 $}
7
8 @LabeledClause{Type Conversions}
9
10 @begin{Intro}
11 @Redundant[Explicit type conversions, both
12 value conversions and view conversions, are
13 allowed between closely related
14 types as defined below. This clause also
15 defines rules for value and view
16 conversions to a particular subtype of a type,
17 both explicit ones and those implicit in other constructs.
18 @IndexSee{Term=[subtype conversion],See=(type conversion)}
19 @Defn{type conversion}
20 @Defn{conversion}
21 @IndexSee{Term=[cast],See=(type conversion)}]
22 @IndexSeeAlso{Term=[subtype conversion],See=(implicit subtype conversion)}
23 @IndexSee{Term=[type conversion, implicit],See=(implicit subtype conversion)}
24 @end{Intro}
25
26 @begin{Syntax}
27 @Syn{lhs=<type_conversion>,rhs="
28 @Syn2{subtype_mark}(@Syn2{expression})
29 | @Syn2{subtype_mark}(@Syn2{name})"}
30
31 @end{Syntax}
32
33 @begin{Intro}
34 @Defn2{Term=[target subtype], Sec=(of a @nt<type_conversion>)}
35 The @i(target subtype) of a @nt<type_conversion> is the subtype
36 denoted by the @nt{subtype_mark}.
37 @Defn2{Term=[operand], Sec=(of a @nt<type_conversion>)}
38 The @i(operand) of a @nt<type_conversion> is the
39 @nt{expression} or @nt{name} within the parentheses;
40 @Defn2{Term=[operand type], Sec=(of a @nt<type_conversion>)}
41 its type is the @i(operand type).
42
43 @Defn{convertible}
44 One type is @i(convertible) to a second type if a @nt<type_conversion>
45 with the first type as operand type and the second type as target type
46 is legal according to the rules of this clause.
47 Two types are convertible if each is convertible to the other.
48 @begin{Ramification}
49 Note that @lquotes@;convertible@rquotes@; is defined in terms of legality
50 of the conversion. Whether the conversion would raise an exception
51 at run time is irrelevant to this definition.
52 @end{Ramification}
53
54 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0017],ARef=[AI95-00184-01]}
55 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00330-01]}
56 @Defn{view conversion}
57 @Defn2{Term=[conversion],Sec=(view)}
58 A @nt{type_conversion} whose operand is the
59 @nt<name> of an object is called a @i(view conversion) if
60 @Chg{New=[both ],Old=[]}its target type
61 @Chg{New=[and operand type are],Old=[is]} tagged, or if it
62 appears@Chg{Version=[2],New=[ in a call],Old=[]} as an actual parameter of mode
63 @key[out] or @key[in out];
64 @Defn{value conversion}
65 @Defn2{Term=[conversion],Sec=(value)}
66 other @nt<type_conversion>s are called @i(value conversions).
67 @IndexSee{Term=[super],See=(view conversion)}
68 @begin{Ramification}
69 A view conversion to a tagged type can appear in
70 any context that requires an object @nt<name>, including in
71 an object renaming, the @nt<prefix> of a @nt<selected_component>,
72 and if the operand is a variable, on the left side of an
73 @nt<assignment_statement>. View conversions to other types only
74 occur as actual parameters. Allowing view conversions of untagged
75 types in all contexts seemed to incur an undue implementation burden.
76
77 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00330-01]}
78 @ChgAdded{Version=[2],Text=[A type conversion appearing as an @key{in out}
79 parameter in a generic instantiation is not a view conversion; the second
80 part of the rule only applies to subprogram calls, not instantiations.]}
81 @end{Ramification}
82 @end{Intro}
83
84 @begin{Resolution}
85 @PDefn2{Term=[expected type],
86 Sec=(type_conversion operand)}
87 The operand of a @nt<type_conversion> is expected to be of any type.
88 @begin{Discussion}
89 This replaces the "must be determinable" wording of Ada 83.
90 This is equivalent to (but hopefully more intuitive than) saying
91 that the operand of a @nt<type_conversion>
92 is a @lquotes@;complete context.@rquotes@;
93 @end{Discussion}
94
95 The operand of a view conversion
96 is interpreted only as a @nt<name>;
97 the operand of a value conversion
98 is interpreted as an @nt<expression>.
99 @begin{Reason}
100 This formally resolves the syntactic ambiguity between
101 the two forms of @nt<type_conversion>, not that it really matters.
102 @end{Reason}
103
104 @end{Resolution}
105
106 @begin{Legality}
107 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
108 @Chg{Version=[2],New=[In a view conversion for an untagged type,
109 the target type shall be convertible (back) to the operand type.],
110 Old=[@Defn2{Term=[type conversion],sec=(numeric)}
111 @Defn2{Term=[conversion],sec=(numeric)}
112 If the target type is a numeric type, then the operand type
113 shall be a numeric type.]}
114 @begin{Reason}
115 @ChgRef{Version=[2],Kind=[AddedNormal]}
116 @ChgAdded{Version=[2],Text=<Untagged view conversions appear only as [@key{in}]
117 @key{out}
118 parameters. Hence, the reverse conversion must be legal as well.
119 The forward conversion must be legal even for an @key{out} parameter,
120 because (for example) actual parameters of an access type are always
121 copied in anyway.>}
122 @end{Reason}
123 @begin{NotIso}
124 @ChgAdded{Version=[2],Noprefix=[T],Noparanum=[T],Text=[@Shrink{@i<Paragraphs 9
125 through 20 were reorganized and moved below.>}]}@Comment{This message should be
126 deleted if the paragraphs are ever renumbered.}
127 @end{NotIso}
128 @begin{Discussion}
129 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
130 @ChgAdded{Version=[2],Text=[The entire @LegalityTitle section has been
131 reorganized to eliminate an
132 unintentional incompatibility with Ada 83. In rare cases, a type conversion
133 between two types related by derivation is not allowed by Ada 95, while it is
134 allowed in Ada 83. The reorganization fixes this.
135 Much of the wording of the legality section is unchanged, but it is reordered
136 and reformatted. Because of the limitations of our tools, we had to delete and
137 replace nearly the entire section. The text of Ada 95 paragraphs 8 through 12,
138 14, 15, 17, 19, 20, and 24 are unchanged (just moved); these are now
139 24.1 through 24.5, 24.12, 24.13, 24.17, 24.19, 24.20, and 8.]}
140 @end{Discussion}
141
142 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
143 @ChgDeleted{Version=[2],Type=[Leading],Text=[@Defn2{Term=[type conversion],sec=(array)}
144 @Defn2{Term=[conversion],sec=(array)}
145 If the target type is an array type, then the operand type shall
146 be an array type. Further:]}
147 @begin(itemize)
148 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
149 @ChgDeleted{Version=[2],Text=[The types shall have the same dimensionality;]}
150
151 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0008],ARef=[AI95-00168-01]}
152 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
153 @ChgDeleted{Version=[2],Text=[Corresponding index types shall be convertible;@Chg{New=[],Old=[ and]}
154 @PDefn2{Term=[convertible],Sec=(required)}]}
155
156 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0008],ARef=[AI95-00168-01]}
157 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
158 @ChgDeleted{Version=[2],Text=[The component subtypes shall statically match@Chg{New=[; and],Old=[.]}
159 @PDefn2{Term=[statically matching],Sec=(required)}]}
160
161 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0008],ARef=[AI95-00168-01]}
162 @ChgRef{Version=[2],Kind=[DeletedAddedNoDelMsg],ARef=[AI95-00251-01]}
163 @ChgDeleted{Version=[2],Text=[@Chg{New=[In a view conversion, the target type and the operand type shall
164 both or neither have aliased components.],Old=[]}]}
165 @begin{Reason}
166 @ChgRef{Version=[1],Kind=[Added]}
167 @ChgRef{Version=[2],Kind=[DeletedAddedNoDelMsg]}
168 @ChgDeleted{Version=[2],Text=[@Chg{New=[Without this rule, it is possible to violate the constrained status
169 of aliased array components. Consider:],Old=[]}]}
170 @begin{Example}
171 @ChgRef{Version=[1],Kind=[Added]}
172 @ChgRef{Version=[2],Kind=[DeletedAddedNoDelMsg]}
173 @ChgDeleted{Version=[2],Text=[@Chg{New=[@key[package] P @key[is]
174 @key[type] T @key[is private];
175 A : @key[constant] T;
176 @key[type] A1 @key[is array] (1 .. 10) @key[of aliased] T;
177 @key[type] A2 @key[is array] (1 .. 10) @key[of] T;
178 @key[private]
179 @key[type] T (D : Integer := 0) @key[is null record];
180 A : @key[constant] T := (D => 1);
181 @key[end] P;],Old=[]}]}
182
183 @ChgRef{Version=[1],Kind=[Added]}
184 @ChgRef{Version=[2],Kind=[DeletedAddedNoDelMsg]}
185 @ChgDeleted{Version=[2],Text=[@Chg{New=[@key[with] P;
186 @key[procedure] Exam @key[is]
187 X : P.A1;
188 @key[procedure] S (Y : @key[in out] P.A2) @key[is]
189 @key[begin]
190 Y (1) := P.A;
191 @key[end];
192 @key[begin]
193 S (P.A2 (X)); -- This call will change the discriminant of X (1),
194 -- so we cannot allow the conversion.
195 @key[end];],Old=[]}]}
196 @end{Example}
197 @end{Reason}
198 @end(itemize)
199
200 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
201 @ChgDeleted{Version=[2],Type=[Leading],Text=[@Defn2{Term=[type conversion],sec=(access)}
202 @Defn2{Term=[conversion],sec=(access)}
203 If the target type is a general access type, then the operand type
204 shall be an access-to-object type. Further:]}
205 @begin{Discussion}
206 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
207 @ChgDeleted{Version=[2],Text=[The @LegalityTitle and @RunTimeTitle are worded
208 so that a @nt{type_conversion} T(X) (where T is an access type) is (almost)
209 equivalent to the @nt{attribute_reference} X.@key[all]'Access, where the
210 result is of type T. The @nt{type_conversion} accepts a null value, whereas
211 the @nt{attribute_reference} would raise Constraint_Error.]}
212 @end{Discussion}
213 @begin(itemize)
214 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
215 @ChgDeleted{Version=[2],Text=[If the target type is an access-to-variable
216 type, then the operand type shall be an access-to-variable type;]}
217 @begin{Ramification}
218 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
219 @ChgDeleted{Version=[2],Text=[If the target type is an access-to-constant
220 type, then the operand type
221 can be access-to-constant or access-to-variable.]}
222 @end{Ramification}
223
224 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
225 @ChgDeleted{Version=[2],Text=[If the target designated type is tagged,
226 then the operand designated type
227 shall be convertible to the target designated type;
228 @PDefn2{Term=[convertible],Sec=(required)}]}
229
230 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
231 @ChgDeleted{Version=[2],Text=[If the target designated type is not tagged,
232 then the designated types shall be the same,
233 and either the designated subtypes shall statically match or
234 the target designated subtype shall be discriminated and unconstrained; and
235 @PDefn2{Term=[statically matching],Sec=(required)}]}
236 @begin{Reason}
237 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
238 @ChgDeleted{Version=[2],Text=[These rules are designed to ensure that
239 aliased array objects only @i(need) "dope" if their nominal subtype is unconstrained,
240 but they can always @i(have) dope if required by the run-time model
241 (since no sliding is permitted as part of access type conversion).
242 By contrast, aliased discriminated objects will always @i(need) their
243 discriminants stored with them, even if nominally constrained.
244 (Here, we are assuming an implementation that represents
245 an access value as a single pointer.)]}
246 @end{Reason}
247
248 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
249 @ChgDeleted{Version=[2],Text=[@PDefn2{Term=[accessibility rule],Sec=(type conversion)}
250 The accessibility level of the operand type shall not be statically
251 deeper than that of the target type.
252 @PDefn{generic contract issue}
253 In addition to the places where @LegalityTitle normally apply
254 (see @RefSecNum{Generic Instantiation}),
255 this rule applies also in the private part of an
256 instance of a generic unit.]}
257 @begin{Ramification}
258 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
259 @ChgDeleted{Version=[2],Text=[The access parameter case is handled by a run-time check.
260 Run-time checks are also done in instance bodies.]}
261 @end{Ramification}
262 @end(itemize)
263
264 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
265 @ChgDeleted{Version=[2],Type=[Leading],Text=[@Defn2{Term=[type conversion],sec=(access)}
266 @Defn2{Term=[conversion],sec=(access)}
267 If the target type is an access-to-subprogram type, then the operand
268 type shall be an access-to-subprogram type. Further:]}
269 @begin(itemize)
270 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
271 @ChgDeleted{Version=[2],Text=[The designated profiles shall be
272 subtype-conformant.@Defn2{Term=[subtype conformance],Sec=(required)}]}
273
274 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00251-01]}
275 @ChgDeleted{Version=[2],Text=[@PDefn2{Term=[accessibility rule],Sec=(type conversion)}
276 The accessibility level of the operand type shall not be statically
277 deeper than that of the target type.
278 @PDefn{generic contract issue}
279 In addition to the places where @LegalityTitle normally apply
280 (see @RefSecNum{Generic Instantiation}),
281 this rule applies also in the private part of an
282 instance of a generic unit.
283 If the operand type is declared within a generic body,
284 the target type shall be declared within the generic body.]}
285
286 @begin{Reason}
287 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
288 @ChgDeleted{Version=[2],Text=[The reason it is illegal to convert from
289 an access-to-subprogram type
290 declared in a generic body to one declared outside that body
291 is that in an implementation that shares generic bodies,
292 procedures declared inside the generic need to have a different
293 calling convention @em they need an extra parameter pointing to the
294 data declared in the current instance.
295 For procedures declared in the spec,
296 that's OK, because the compiler can know about them at compile time of
297 the instantiation.]}
298 @end{Reason}
299 @end(itemize)
300
301 @Comment{We start the new text here, so we can modify the handful of rules
302 that are not reformatted. (Except the first rule is at the top.)}
303
304 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
305 @Leading@Defn2{Term=[type conversion],sec=[composite (non-array)]}
306 @Defn2{Term=[conversion],sec=[composite (non-array)]}
307 @Chg{Version=[2],New=[If there is a type that is an ancestor of both the target
308 type and the operand type, or both types are class-wide types, then at least
309 one of the following rules shall apply:],Old=[@Defn2{Term=[type conversion],sec=(enumeration)}
310 @Defn2{Term=[conversion],sec=(enumeration)}
311 If the target type is not included in any of the above four
312 cases, there shall be a type that is an ancestor of both
313 the target type and the operand type.
314 Further, if the target type is tagged, then either:]}
315 @begin(itemize)
316 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
317 @ChgAdded{Version=[2],Text=[@Defn2{Term=[type conversion],sec=(enumeration)}
318 @Defn2{Term=[conversion],sec=(enumeration)}The target type shall be
319 untagged; or]}
320
321 The operand type shall be covered by or
322 descended from the target type; or
323 @begin{Ramification}
324 This is a conversion
325 toward the root, which is always safe.
326 @end{Ramification}
327
328 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
329 The operand type shall be a class-wide type that covers the target
330 type@Chg{Version=[2],New=[; or],Old=[.]}
331 @begin{Ramification}
332 This is a conversion of a class-wide type toward the leaves,
333 which requires a tag check. See @RunTimeTitle.
334
335 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
336 These two rules imply that a conversion from @Chg{Version=[2],New=[an
337 ancestor],Old=[a parent]} type
338 to a type extension is not permitted, as this would require
339 specifying the values for additional components, in general,
340 and changing the tag. An @nt<extension_aggregate> has to be used
341 instead, constructing a new value, rather than converting an
342 existing value. However, a conversion
343 from the class-wide type rooted at @Chg{Version=[2],New=[an
344 ancestor],Old=[the parent]} type is permitted;
345 such a conversion just verifies that the operand's tag is
346 a descendant of the target.
347 @end{Ramification}
348
349 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
350 @ChgAdded{Version=[2],Text=[The operand and target types shall both be class-wide
351 types and the specific type associated with at least one of them shall be an
352 interface type.]}
353 @begin{Ramification}
354 @ChgRef{Version=[2],Kind=[AddedNormal]}
355 @ChgAdded{Version=[2],Text=[We allow converting any class-wide type T'Class to
356 or from a class-wide interface type even if the specific type T does not
357 have an appropriate
358 interface ancestor, because some extension of T might have the needed
359 ancestor. This is similar to a conversion of a class-wide type toward the
360 leaves of the tree, and we need to be consistent. Of course, there is
361 a run-time check that the actual object has the needed interface.]}
362 @end{Ramification}
363 @end(itemize)
364
365 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
366 @Chg{Version=[2],New=[If there is no type that is the ancestor of both the
367 target type and the operand type, and they are not both class-wide types, one
368 of the following rules shall apply:], Old=[In a view conversion for
369 an untagged type, the target type shall be convertible (back) to the operand type.]}
370 @begin{Reason}
371 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
372 @ChgDeleted{Version=[2],Text=<Untagged view conversions appear only as [in] out
373 parameters. Hence, the reverse conversion must be legal as well.
374 The forward conversion must be legal even if an out parameter,
375 because actual parameters of an access type are always
376 copied in anyway.>}
377 @end{Reason}
378
379 @begin(itemize)
380 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
381 @ChgAdded{Version=[2],Text=[@Defn2{Term=[type conversion],sec=(numeric)}
382 @Defn2{Term=[conversion],sec=(numeric)}
383 If the target type is a numeric type, then the operand type
384 shall be a numeric type.]}
385
386 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
387 @ChgAdded{Version=[2],Type=[Leading],Text=[@Defn2{Term=[type conversion],sec=(array)}
388 @Defn2{Term=[conversion],sec=(array)}
389 If the target type is an array type, then the operand type shall
390 be an array type. Further:]}
391
392 @begin(inneritemize)
393
394 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
395 @Chg{Version=[2],New=[The types shall have the same dimensionality;],Old=[]}
396
397 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
398 @Chg{Version=[2],New=[Corresponding index types shall be convertible;
399 @PDefn2{Term=[convertible],Sec=(required)}],Old=[]}
400
401 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
402 @Chg{Version=[2],New=[The component subtypes shall statically match;
403 @PDefn2{Term=[statically matching],Sec=(required)}],Old=[]}
404
405 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00392-01]}
406 @Chg{Version=[2],New=[If the component types are anonymous access types, then the
407 accessibility level of the operand type shall not be statically deeper
408 than that of the target type;
409 @PDefn2{Term=[accessibility rule],Sec=(type conversion, array components)}],Old=[]}
410 @begin{Reason}
411 @ChgRef{Version=[2],Kind=[AddedNormal]}
412 @ChgAdded{Version=[2],Text=[For unrelated array types, the component types
413 could have different accessibility, and we had better not allow a
414 conversion of a local type into a global type, in case the local type
415 points at local objects. We don't need a check for other types of
416 components; such components necessarily are for related types, and
417 either have the same accessibility or (for access discriminants) cannot
418 be changed so the discriminant check will prevent problems.]}
419 @end{Reason}
420
421 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00246-01]}
422 @ChgAdded{Version=[2],Text=[Neither the target type nor the operand type shall be
423 limited;]}
424 @begin{Reason}
425 @ChgRef{Version=[2],Kind=[AddedNormal]}
426 @ChgAdded{Version=[2],Text=[We cannot allow conversions between unrelated limited
427 types, as they may have different representations, and (since the types
428 are limited), a copy cannot be made to reconcile the representations.]}
429 @end{Reason}
430
431 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01],ARef=[AI95-00363-01]}
432 @ChgAdded{Version=[2],Text=[If the target type of a view conversion has
433 aliased components, then so shall the operand type; and]}
434 @begin{Reason}
435 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
436 @ChgAdded{Version=[2],Text=[We cannot allow a view conversion from an object
437 with unaliased components to an object with aliased components, because
438 that would effectively allow pointers to unaliased components. This rule
439 was missing from Ada 95.]}
440 @end{Reason}
441
442 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00246-01],ARef=[AI95-00251-01]}
443 @ChgAdded{Version=[2],Text=[The operand type of a view conversion shall not
444 have a tagged, private, or volatile subcomponent.]}
445 @begin{Reason}
446 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00246-01]}
447 @ChgAdded{Version=[2],Text=[We cannot allow view conversions between unrelated
448 might-be-by-reference types, as they may have different representations,
449 and a copy cannot be made to reconcile the representations.]}
450 @end{Reason}
451 @begin{Ramification}
452 @ChgRef{Version=[2],Kind=[AddedNormal]}
453 @ChgAdded{Version=[2],Text=[These rules only apply to unrelated array
454 conversions; different (weaker) rules apply to conversions between related
455 types.]}
456 @end{Ramification}
457 @end(inneritemize)
458
459 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00230-01]}
460 @Chg{Version=[2],New=[If the target type is @i<universal_access>, then the
461 operand type shall be an access type.],Old=[]}
462
463 @begin{Discussion}
464 @ChgRef{Version=[2],Kind=[AddedNormal]}
465 @ChgAdded{Version=[2],Text=[Such a conversion cannot be written explicitly,
466 of course, but it can be implicit (see below).]}
467 @end{Discussion}
468
469 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00230-01],ARef=[AI95-00251-01]}
470 @ChgAdded{Version=[2],Type=[Leading],Text=[@Defn2{Term=[type conversion],sec=(access)}
471 @Defn2{Term=[conversion],sec=(access)}If the target type is a general access-to-object type, then
472 the operand type shall be @i<universal_@!access> or an access-to-object type.
473 Further, if the operand type is not @i<universal_@!access>:]}
474
475 @begin{Discussion}
476 @ChgRef{Version=[2],Kind=[AddedNormal]}
477 @ChgAdded{Version=[2],Text=[The @LegalityTitle and @RunTimeTitle are worded
478 so that a @nt{type_conversion} T(X) (where T is an access type) is
479 (almost) equivalent to the @nt{attribute_reference}
480 X.@key[all]'Access, where the result is of type T.
481 The only difference is that the @nt{type_conversion} accepts a null value,
482 whereas the @nt{attribute_reference} would raise Constraint_Error.]}
483 @end{Discussion}
484
485 @begin(inneritemize)
486
487 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
488 @Chg{Version=[2],New=[If the target type is an access-to-variable
489 type, then the operand type shall be an access-to-variable type;],Old=[]}
490 @begin{Ramification}
491 @ChgRef{Version=[2],Kind=[AddedNormal]}
492 @ChgAdded{Version=[2],Text=[If the target type is an access-to-constant type,
493 then the operand type can be access-to-constant or access-to-variable.]}
494 @end{Ramification}
495
496 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
497 @ChgAdded{Version=[2],Text=[If the target designated type is tagged, then the
498 operand designated type shall be convertible to the target designated type;
499 @PDefn2{Term=[convertible],Sec=(required)}]}
500
501 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01],ARef=[AI95-00363-01]}
502 @ChgAdded{Version=[2],Type=[Leading],Text=[If the target designated type is
503 not tagged, then the designated types shall be the same, and either:]}
504
505 @begin(innerinneritemize)
506 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00363-01]}
507 @Chg{Version=[2],New=[the designated subtypes shall statically match;
508 or@PDefn2{Term=[statically matching],Sec=(required)}],Old=[]}
509
510 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00363-01],ARef=[AI95-00384-01]}
511 @Chg{Version=[2],New=[the designated type shall be discriminated in its
512 full view and unconstrained in any partial view, and one of the designated
513 subtypes shall be unconstrained;],Old=[]}
514
515 @begin{Ramification}
516 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
517 @ChgAdded{Version=[2],Text=[This does not require that types have a partial view
518 in order to allow the conversion, simply that any partial view that does
519 exist is unconstrained.]}
520
521 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00384-01]}
522 @ChgAdded{Version=[2],Text=[This allows conversions both ways (either subtype
523 can be unconstrained); while Ada 95 only allowed the conversion if the
524 target subtype is unconstrained. We generally want type conversions to be
525 symmetric; which type is the target shouldn't matter for legality.]}
526 @end{Ramification}
527
528 @begin{Reason}
529 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
530 @ChgAdded{Version=[2],Text=[If the visible partial view is constrained, we
531 do not allow conversion between unconstrained and constrained subtypes.
532 This means that whether the full type had discriminants is not visible
533 to clients of the partial view.]}
534 @end{Reason}
535
536 @end(innerinneritemize)
537 @begin{Reason}
538 @ChgRef{Version=[2],Kind=[AddedNormal]}
539 @ChgAdded{Version=[2],Text=[These rules are designed to ensure that aliased array objects
540 only @i(need) "dope" if their nominal subtype is unconstrained,
541 but they can always @i(have) dope if required by the run-time model
542 (since no sliding is permitted as part of access type conversion).
543 By contrast, aliased discriminated objects will always @i(need) their
544 discriminants stored with them, even if nominally constrained.
545 (Here, we are assuming an implementation that represents
546 an access value as a single pointer.)]}
547 @end{Reason}
548
549 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
550 @Chg{Version=[2],New=[@PDefn2{Term=[accessibility rule],Sec=(type conversion)}
551 The accessibility level of the operand type shall not be statically
552 deeper than that of the target type.
553 @PDefn{generic contract issue}
554 In addition to the places where @LegalityTitle normally apply
555 (see @RefSecNum{Generic Instantiation}),
556 this rule applies also in the private part of an
557 instance of a generic unit.],Old=[]}
558 @begin{Ramification}
559 @ChgRef{Version=[2],Kind=[AddedNormal]}
560 @ChgAdded{Version=[2],Text=[The access parameter case is handled by a run-time
561 check. Run-time checks are also done in instance bodies.]}
562 @end{Ramification}
563
564 @end(inneritemize)
565
566 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00230-01]}
567 @ChgAdded{Version=[2],Text=[@Defn2{Term=[type conversion],sec=(access)}
568 @Defn2{Term=[conversion],sec=(access)}If the target type is a pool-specific
569 access-to-object type, then
570 the operand type shall be @i<universal_access>.]}
571 @begin{Reason}
572 @ChgRef{Version=[2],Kind=[AddedNormal]}
573 @ChgAdded{Version=[2],Text=[This allows @b<null> to be converted to pool-specific
574 types. Without it, @b<null> could be converted to general access types but
575 not pool-specific ones, which would be too inconsistent. Remember that these
576 rules only apply to unrelated types, so we don't have to talk about
577 conversions to derived or other related types.]}
578 @end{Reason}
579
580 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00230-01],ARef=[AI95-00251-01]}
581 @ChgAdded{Version=[2],Type=[Leading],Text=[@Defn2{Term=[type conversion],sec=(access)}
582 @Defn2{Term=[conversion],sec=(access)}
583 If the target type is an access-to-subprogram type, then the operand type
584 shall be @i<universal_@!access> or an access-to-subprogram type. Further, if
585 the operand type is not @i<universal_@!access>:]}
586
587 @begin(inneritemize)
588
589 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
590 @Chg{Version=[2],New=[The designated profiles shall be subtype-conformant.
591 @Defn2{Term=[subtype conformance],Sec=(required)}],Old=[]}
592
593 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01]}
594 @Chg{Version=[2],New=[@PDefn2{Term=[accessibility rule],Sec=(type conversion)}
595 The accessibility level of the operand type shall not be statically
596 deeper than that of the target type.
597 @PDefn{generic contract issue}
598 In addition to the places where @LegalityTitle normally apply
599 (see @RefSecNum{Generic Instantiation}),
600 this rule applies also in the private part of an
601 instance of a generic unit.
602 If the operand type is declared within a generic body,
603 the target type shall be declared within the generic body.],Old=[]}
604
605 @begin{Reason}
606 @ChgRef{Version=[2],Kind=[AddedNormal]}
607 @ChgAdded{Version=[2],Text=[The reason it is illegal to convert from an
608 access-to-subprogram type declared in a generic body to one declared outside
609 that body is that in an implementation that shares generic bodies,
610 procedures declared inside the generic need to have a different
611 calling convention @em they need an extra parameter pointing to the
612 data declared in the current instance. For procedures declared in the spec,
613 that's OK, because the compiler can know about them at compile time of
614 the instantiation.]}
615 @end{Reason}
616 @end(inneritemize)
617
618 @end(itemize)
619 @end{Legality}
620
621 @begin{StaticSem}
622 A @nt{type_conversion} that is a value conversion denotes the value
623 that is
624 the result of converting the value of the operand to the target subtype.
625
626 A @nt{type_conversion} that is a view conversion
627 denotes a view of the object denoted by the operand. This
628 view is a variable of the target type if the operand denotes
629 a variable; otherwise it is a constant of
630 the target type.
631
632 @PDefn2{Term=[nominal subtype], Sec=(associated with a @nt<type_conversion>)}
633 The nominal subtype of a @nt<type_conversion> is its target subtype.
634 @end{StaticSem}
635
636 @begin{RunTime}
637 @Leading@PDefn2{Term=[evaluation], Sec=(value conversion)}
638 @Defn2{Term=[corresponding value],
639 Sec=(of the target type of a conversion)}
640 @Defn{conversion}
641 For the evaluation of a @nt<type_conversion> that is a value conversion,
642 the operand is evaluated, and then
643 the value of the operand is @i(converted) to a @i(corresponding)
644 value of the target type, if any.
645 @IndexCheck{Range_Check}
646 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
647 If there is no value
648 of the target type that corresponds to the operand value,
649 Constraint_Error is raised@Redundant[; this can only
650 happen on conversion to a modular type,
651 and only when the operand value is outside the base range of
652 the modular type.]
653 Additional rules follow:
654 @begin(itemize)
655 @Defn2{Term=[type conversion],sec=(numeric)}
656 @Defn2{Term=[conversion],sec=(numeric)}
657 Numeric Type Conversion
658 @begin(inneritemize)
659 If the target and the operand types are both integer types, then
660 the result is the value of the target type that corresponds
661 to the same mathematical integer as the operand.
662
663 If the target type is a decimal fixed point type, then the result is
664 truncated (toward 0) if the
665 value of the operand is not a multiple of the @i{small} of the target
666 type.
667
668 @Defn{accuracy}
669 If the target type is some other real type,
670 then the result is within the accuracy of the target type
671 (see @RefSec{Numeric Performance Requirements},
672 for implementations that support the Numerics Annex).
673 @begin(Discussion)
674 An integer type might have more bits of precision than a real type,
675 so on conversion (of a large integer), some precision might be lost.
676 @end(Discussion)
677
678 If the target type is an integer type and the operand type is real,
679 the result is rounded to the nearest integer (away from zero
680 if exactly halfway between two integers).
681 @begin{Discussion}
682 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00267-01]}
683 This was implementation defined in Ada 83.
684 There seems no reason to preserve the nonportability
685 in Ada 95. Round-away-from-zero is the conventional
686 definition of rounding, and standard Fortran and COBOL both specify
687 rounding away from zero, so for interoperability, it seems important
688 to pick this. This is also the most easily @lquotes@;undone@rquotes@; by hand.
689 Round-to-nearest-even is an alternative, but that is quite complicated
690 if not supported by the hardware. In any case, this operation is not
691 @Chg{Version=[2],New=[usually],Old=[expected to be]} part of an inner loop,
692 so predictability and portability are judged most important.
693 @Chg{Version=[2],New=[A],Old=[We anticipate that
694 a]} floating point attribute function Unbiased_Rounding @Chg{Version=[2],
695 New=[is],Old=[will be]} provided@Chg{Version=[2],
696 New=[ (see @RefSecNum{Attributes of Floating Point Types})],Old=[]}
697 for those applications that require round-to-nearest-even@Chg{Version=[2],
698 New=[, and a floating point attribute function Machine_Rounding (also see
699 @RefSecNum{Attributes of Floating Point Types}) is provided for those
700 applications that require the highest possible performance], Old=[]}.
701 @lquotes@;Deterministic@rquotes@; rounding is required for static
702 conversions to integer as well.
703 See @RefSecNum{Static Expressions and Static Subtypes}.
704 @end{Discussion}
705 @end(inneritemize)
706
707 @Defn2{Term=[type conversion],sec=(enumeration)}
708 @Defn2{Term=[conversion],sec=(enumeration)}
709 Enumeration Type Conversion
710 @begin(inneritemize)
711 The result is the value of the target type with the same
712 position number as that of the operand value.
713 @end(inneritemize)
714
715 @Defn2{Term=[type conversion],sec=(array)}
716 @Defn2{Term=[conversion],sec=(array)}
717 Array Type Conversion
718 @begin(inneritemize)
719 @IndexCheck{Length_Check}
720 If the target subtype is a constrained array subtype, then
721 a check is made that the length of each dimension of the value of
722 the operand equals the length of the corresponding dimension of the
723 target subtype.
724 The bounds of the result are those of the target subtype.
725
726 @IndexCheck{Range_Check}
727 If the target subtype is an unconstrained array subtype, then the
728 bounds of the result are obtained by converting each bound of the value
729 of the operand to the corresponding index type of the target type.
730 @PDefn2{Term=[implicit subtype conversion],Sec=(array bounds)}
731 For each nonnull index range, a check is made that the
732 bounds of the range belong to the corresponding index subtype.
733 @begin(Discussion)
734 Only nonnull index ranges are checked, per AI83-00313.
735 @end(Discussion)
736
737 In either array case, the value of each component of the result is that
738 of the matching component of the operand value
739 (see @RefSecNum{Relational Operators and Membership Tests}).
740 @begin{Ramification}
741 This applies whether or not the component is initialized.
742 @end{Ramification}
743
744 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00392-01]}
745 @ChgAdded{Version=[2],Text=[If the component types of the array types are
746 anonymous access types, then a check is made that the accessibility level
747 of the operand type is not deeper than that of the target type.
748 @IndexCheck{Accessibility_Check}]}
749 @begin{Reason}
750 @ChgRef{Version=[2],Kind=[AddedNormal]}
751 @ChgAdded{Version=[2],Text=[This check is needed for operands that are access
752 parameters and in instance bodies. Other cases are handled by the
753 legality rule given previously.]}
754 @end{Reason}
755
756 @end(inneritemize)
757
758 @Defn2{Term=[type conversion],sec=[composite (non-array)]}
759 @Defn2{Term=[conversion],sec=[composite (non-array)]}
760 Composite (Non-Array) Type Conversion
761 @begin(inneritemize)
762 The value of each nondiscriminant component of the result
763 is that of the matching component of the operand value.
764 @begin{Ramification}
765 This applies whether or not the component is initialized.
766 @end{Ramification}
767
768 @Redundant[The tag of the result is that of the operand.]
769 @IndexCheck{Tag_Check}
770 If the operand type is class-wide,
771 a check is made that the tag of the operand identifies
772 a (specific) type that is covered by or descended from
773 the target type.
774 @begin{Ramification}
775 This check is certain to succeed
776 if the operand type is itself covered by or descended from
777 the target type.
778 @end{Ramification}
779 @begin{TheProof}
780 The fact that a @nt{type_conversion} preserves the tag
781 is stated officially in @RefSec{Tagged Types and Type Extensions}
782 @end{TheProof}
783
784 For each discriminant of the target type that corresponds to
785 a discriminant of the operand type, its value is that of
786 the corresponding discriminant of the operand value;
787 @IndexCheck{Discriminant_Check}
788 if it corresponds
789 to more than one discriminant of the operand type, a check is made
790 that all these discriminants are equal in the operand value.
791
792 For each discriminant of the target type that corresponds to
793 a discriminant that is specified by the @nt<derived_type_definition>
794 for some ancestor of the operand type (or if class-wide,
795 some ancestor of the specific type identified by the tag
796 of the operand), its
797 value in the result is that specified by the @nt<derived_type_definition>.
798 @begin{Ramification}
799 It is a ramification of the rules for the discriminants of derived types
800 that each discriminant of the result is covered either by this
801 paragraph or the previous one. See @RefSecNum(Discriminants).
802 @end{Ramification}
803
804 @IndexCheck{Discriminant_Check}
805 For each discriminant of the operand type that corresponds
806 to a discriminant that is specified by the @nt<derived_type_definition>
807 for some ancestor of the target type,
808 a check is made that in the operand value it equals the value
809 specified for it.
810
811 @IndexCheck{Range_Check}
812 For each discriminant of the result, a check is made that its
813 value belongs to its subtype.
814 @end(inneritemize)
815
816 @Defn2{Term=[type conversion],sec=(access)}
817 @Defn2{Term=[conversion],sec=(access)}
818 Access Type Conversion
819 @begin(inneritemize)
820 For an access-to-object type,
821 a check is made that the accessibility level of the operand
822 type is not deeper than that of the target type.
823 @IndexCheck{Accessibility_Check}
824
825 @begin{Ramification}
826 This check is needed for operands that are access
827 parameters and in instance bodies.
828
829 Note that this check can never fail for the implicit conversion
830 to the anonymous type of an access parameter that is done when
831 calling a subprogram with an access parameter.
832 @end{Ramification}
833
834 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01],ARef=[AI95-00231-01]}
835 If the @Chg{Version=[2],New=[],Old=[target type is an anonymous access type,
836 a check is made that the value of the operand is not null;
837 if the target is not an anonymous access type, then the
838 result is null if the ]}operand value is null@Chg{Version=[2],New=[, the
839 result of the conversion is the null value of the target type.],Old=[.
840 @IndexCheck{Access_Check}]}
841 @begin{Ramification}
842 @ChgRef{Version=[2],Kind=[Revised]}
843 A conversion to an anonymous access type
844 happens implicitly as part of initializing
845 @Chg{Version=[2],New=[or assigning to an anonymous access object],
846 Old=[an access discriminant or access parameter]}.
847 @end{Ramification}
848 @begin{Reason}
849 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00231-01]}
850 @ChgDeleted{Version=[2],Text=[As explained in @RefSec{Access Types},
851 it is important that a value of an anonymous access type
852 can never be null.]}
853 @end{Reason}
854
855 If the operand value is not null, then
856 the result designates the same
857 object (or subprogram) as is designated by the operand value,
858 but viewed as being of the target designated subtype (or profile);
859 any checks associated with evaluating a conversion to
860 the target designated subtype are performed.
861 @begin{Ramification}
862 The checks are certain to succeed if
863 the target and operand designated subtypes statically match.
864 @end{Ramification}
865 @end(inneritemize)
866 @end(itemize)
867
868 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
869 @IndexCheck{Range_Check}
870 @IndexCheck{Discriminant_Check}
871 @IndexCheck{Index_Check}
872 @Chg{Version=[2],New=[@IndexCheck{Access_Check}],Old=[]}
873 After conversion of the value to the target type,
874 if the target subtype is constrained,
875 a check is performed that the value satisfies this constraint.
876 @Chg{Version=[2],New=[If the target subtype excludes null,
877 then a check is made that the value is not null.],Old=[]}
878 @begin{Ramification}
879 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
880 The @Chg{Version=[2],New=[first],Old=[above]} check
881 @Chg{Version=[2],New=[above ],Old=[]}is a Range_Check for scalar subtypes, a
882 Discriminant_Check or Index_Check for access subtypes, and a Discriminant_Check
883 for discriminated subtypes. The Length_Check for an array conversion is
884 performed as part of the conversion to the target type.
885 @Chg{Version=[2],New=[The check for exclusion of null is an Access_Check.],Old=[]}
886 @end{Ramification}
887
888 @PDefn2{Term=[evaluation], Sec=(view conversion)}
889 For the evaluation of a view conversion, the operand @nt<name> is
890 evaluated, and a new view of the
891 object denoted by the operand is created, whose type is the
892 target type;
893 @IndexCheck{Length_Check}
894 @IndexCheck{Tag_Check}
895 @IndexCheck{Discriminant_Check}
896 if the target type is composite, checks are
897 performed as above for a value conversion.
898
899 @Leading@;The properties of this new view are as follows:
900 @begin(itemize)
901 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0017],ARef=[AI95-00184-01]}
902 If the target type is composite, the bounds or discriminants (if any)
903 of the view are as defined above for a value conversion;
904 each nondiscriminant component of the view denotes the matching
905 component of the operand object; the
906 subtype of the view is constrained if either the target subtype
907 or the operand object is constrained,
908 @Chg{New=[or if the target subtype is indefinite,],Old=[]}
909 or if the operand type is a descendant of the target type@Chg{New=[],Old=[,]}
910 and has discriminants that were not inherited from
911 the target type;
912
913 If the target type is tagged, then an assignment to the
914 view assigns to the corresponding part of the object denoted
915 by the operand; otherwise, an assignment to the view
916 assigns to the object, after converting
917 the assigned value to the subtype of the object (which
918 might raise Constraint_Error);
919 @PDefn2{Term=[implicit subtype conversion],Sec=(assignment to view conversion)}
920
921 Reading the value of the view yields the result of converting
922 the value of the operand object to the target subtype
923 (which might raise Constraint_Error), except if the object
924 is of an access type and the view conversion is passed
925 as an @key(out) parameter;
926 in this latter case,
927 the value of the operand object is used to initialize
928 the formal parameter without
929 checking against any constraint of the target subtype
930 (see @RefSecNum(Parameter Associations)).
931 @PDefn2{Term=[implicit subtype conversion],Sec=(reading a view conversion)}
932 @begin(Reason)
933 This ensures that even an @key(out) parameter of
934 an access type is initialized reasonably.
935 @end(Reason)
936 @end(itemize)
937
938 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}
939 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
940 If an Accessibility_Check fails, Program_Error is raised.
941 Any other check associated with a conversion
942 raises Constraint_Error if it fails.
943
944 Conversion to a type is the same as conversion to an unconstrained
945 subtype of the type.
946 @begin{Reason}
947 This definition is needed because the semantics of various
948 constructs involves converting to a type,
949 whereas an explicit @nt{type_conversion} actually converts to a subtype.
950 For example, the evaluation of a @nt{range} is defined to convert the
951 values of the expressions to the type of the range.
952 @end{Reason}
953 @begin{Ramification}
954 A conversion to a scalar type, or, equivalently,
955 to an unconstrained scalar subtype,
956 can raise Constraint_Error if the value is outside the base range of the
957 type.
958 @end{Ramification}
959 @end{RunTime}
960
961 @begin{Notes}
962 @RootDefn{implicit subtype conversion}
963 In addition to explicit @nt<type_conversion>s,
964 type conversions are performed implicitly in situations where the
965 expected type and the actual type of a construct differ,
966 as is permitted by the type resolution rules
967 (see @RefSecNum(The Context of Overload Resolution)).
968 For example, an integer literal is
969 of the type @i(universal_integer), and is implicitly converted
970 when assigned to a target of some specific integer type.
971 Similarly, an actual parameter of a specific
972 tagged type is implicitly converted when the corresponding
973 formal parameter is of a class-wide type.
974
975 @NoPrefix@;@RootDefn{implicit subtype conversion}
976 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
977 Even when the expected and actual types are the same,
978 implicit subtype conversions are performed to adjust the array bounds (if any)
979 of an operand to match the desired target subtype, or to
980 raise Constraint_Error if the (possibly adjusted) value does not satisfy
981 the constraints of the target subtype.
982
983 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
984 A ramification of the
985 overload resolution rules is that the operand of
986 an (explicit) @nt<type_conversion> cannot be
987 @Chg{Version=[2],New=[],Old=[the literal @key(null), ]}an @nt<allocator>,
988 an @nt<aggregate>, a @nt<string_literal>,
989 a @nt<character_literal>, or an @nt<attribute_reference>
990 for an Access or Unchecked_Access attribute.
991 Similarly, such an @nt{expression} enclosed by parentheses is not
992 allowed. A @nt<qualified_expression> (see @RefSecNum(Qualified Expressions))
993 can be used instead of such a @nt<type_conversion>.
994
995 The constraint of the target subtype has no effect
996 for a @nt<type_conversion> of an elementary type passed
997 as an @key(out) parameter. Hence, it is recommended
998 that the first subtype be specified
999 as the target to minimize confusion
1000 (a similar recommendation applies to renaming and
1001 generic formal @key(in out) objects).
1002 @end{Notes}
1003
1004 @begin{Examples}
1005 @Leading@keepnext@i(Examples of numeric type conversion:)
1006 @begin{Example}
1007 Real(2*J) @RI[-- value is converted to floating point]
1008 Integer(1.6) @RI[-- value is 2]
1009 Integer(-0.4) @RI[-- value is 0]
1010 @end{Example}
1011
1012 @begin{Wide}
1013 @leading@keepnext@i(Example of conversion between derived types:)
1014 @end{Wide}
1015 @begin{Example}
1016 @key(type) A_Form @key(is) @key(new) B_Form;
1017
1018 X : A_Form;
1019 Y : B_Form;
1020
1021 X := A_Form(Y);
1022 Y := B_Form(X); @RI[-- the reverse conversion ]
1023 @end{Example}
1024
1025 @begin{Wide}
1026 @leading@keepnext@i(Examples of conversions between array types:)
1027 @end{Wide}
1028
1029 @begin{Example}
1030 @key(type) Sequence @key(is) @key(array) (Integer @key(range) <>) @key(of) Integer;
1031 @key(subtype) Dozen @key(is) Sequence(1 .. 12);
1032 Ledger : @key(array)(1 .. 100) @key(of) Integer;
1033
1034 Sequence(Ledger) @RI[-- bounds are those of Ledger]
1035 Sequence(Ledger(31 .. 42)) @RI[-- bounds are 31 and 42]
1036 Dozen(Ledger(31 .. 42)) @RI[-- bounds are those of Dozen ]
1037 @end{Example}
1038 @end{Examples}
1039
1040 @begin{Incompatible83}
1041 @Defn{incompatibilities with Ada 83}
1042 A @nt<character_literal> is not allowed as the
1043 operand of a @nt<type_conversion>,
1044 since there are now two character types in package Standard.
1045
1046 The component subtypes have to statically match in an array conversion,
1047 rather than being checked for matching constraints at run time.
1048
1049 Because sliding of array bounds is now provided for operations where it
1050 was not in Ada 83,
1051 programs that used to raise Constraint_Error might now
1052 continue executing and produce a reasonable result.
1053 This is likely to fix more bugs than it creates.
1054 @end{Incompatible83}
1055
1056 @begin{Extend83}
1057 @Defn{extensions to Ada 83}
1058 A @nt<type_conversion> is considered the name of an object
1059 in certain circumstances (such a @nt<type_conversion>
1060 is called a view conversion).
1061 In particular, as in Ada 83,
1062 a @nt<type_conversion> can appear as an @key(in out) or @key(out)
1063 actual parameter. In addition, if the target type is tagged
1064 and the operand is the @nt<name> of an object, then so
1065 is the @nt<type_conversion>, and it can be used as the @nt<prefix>
1066 to a @nt<selected_component>, in an @nt<object_renaming_declaration>, etc.
1067
1068 We no longer require type-mark conformance between
1069 a parameter of the form of a type conversion, and the corresponding
1070 formal parameter. This had caused some problems for
1071 inherited subprograms (since there isn't really a type-mark
1072 for converted formals), as well as for renamings, formal subprograms,
1073 etc. See AI83-00245, AI83-00318, AI83-00547.
1074
1075 We now specify @lquotes@;deterministic@rquotes@; rounding from real to integer types
1076 when the value of the operand is exactly between two integers
1077 (rounding is away from zero in this case).
1078
1079 @lquotes@;Sliding@rquotes@; of array bounds
1080 (which is part of conversion to an array subtype)
1081 is performed in more cases in Ada 95 than in Ada 83.
1082 Sliding is not performed on
1083 the operand of a membership test,
1084 nor on the operand of a @nt{qualified_expression}.
1085 It wouldn't make sense on a membership test,
1086 and we wish to retain a connection between subtype membership
1087 and subtype qualification. In general, a subtype membership test returns
1088 True if and only if a corresponding subtype qualification
1089 succeeds without raising an exception.
1090 Other operations that take arrays perform sliding.
1091 @end{Extend83}
1092
1093 @begin{DiffWord83}
1094 We no longer explicitly list the kinds of things that are not allowed
1095 as the operand of a @nt<type_conversion>, except in a NOTE.
1096
1097 The rules in this clause subsume the rules
1098 for "parameters of the form of a type conversion,"
1099 and have been generalized to cover the use of a type conversion
1100 as a @nt<name>.
1101 @end{DiffWord83}
1102
1103 @begin{Incompatible95}
1104 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00246-01]}
1105 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
1106 @b[Amendment Correction:] Conversions
1107 between unrelated array types that are limited or (for view conversions)
1108 might be by-reference types are now illegal. The representations of two such
1109 arrays may differ, making the conversions impossible. We make the check here,
1110 because legality should not be based on representation properties.
1111 Such conversions are likely to be rare, anyway. There is a potential that
1112 this change would make a working program illegal (if the types have the same
1113 representation).]}
1114
1115 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
1116 @ChgAdded{Version=[2],Text=[If a discriminated full type has a partial view
1117 (private type) that is constrained, we do not allow conversion between
1118 access-to-unconstrained and access-to-constrained subtypes designating the
1119 type. Ada 95 allowed this conversion and the declaration of various access
1120 subtypes, requiring that the designated object be constrained and thus making
1121 details of the implementation of the private type visible to the client of
1122 the private type. See @RefSecNum{Allocators} for more on this topic.]}
1123 @end{Incompatible95}
1124
1125 @begin{Extend95}
1126 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00230-01]}
1127 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}Conversion rules for
1128 @i<universal_access> were defined. These allow the use of anonymous access
1129 values in equality tests (see
1130 @RefSecNum{Relational Operators and Membership Tests}), and also allow the
1131 use of @b<null> in type conversions and other
1132 contexts that do not provide a single expected type.]}
1133
1134 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00384-01]}
1135 @ChgAdded{Version=[2],Text=[A type conversion from
1136 an access-to-discriminated and unconstrained object to an
1137 access-to-discriminated and constrained one is allowed. Ada 95 only allowed
1138 the reverse conversion, which was weird and asymmetric. Of course, a
1139 constraint check will be performed for this conversion.]}
1140 @end{Extend95}
1141
1142 @begin{DiffWord95}
1143 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0017],ARef=[AI95-00184-01]}
1144 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Wording was added to ensure that
1145 view conversions are constrained, and that a tagged view conversion has a
1146 tagged object. Both rules are needed to avoid having a way to change the
1147 discriminants of a constrained object.]}
1148
1149 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0008],ARef=[AI95-00168-01]}
1150 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Wording was added to ensure
1151 that the aliased status of array components cannot change in a view
1152 conversion. This rule was needed to avoid having a way to change the
1153 discriminants of an aliased object. This rule was repealed later, as
1154 Ada 2005 allows changing the discriminants of an aliased object.]}
1155
1156 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
1157 @ChgAdded{Version=[2],Text=[Wording was added to check subtypes that exclude
1158 null (see @RefSecNum{Access Types}).]}
1159
1160 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
1161 @ChgAdded{Version=[2],Text=[The organization of the legality rules was
1162 changed, both to make it clearer, and to eliminate an unintentional
1163 incompatibility with Ada 83. The old organization prevented type conversions
1164 between some types that were related by derivation (which Ada 83 always
1165 allowed).]}
1166
1167 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00330-01]}
1168 @ChgAdded{Version=[2],Text=[Clarified that an untagged type conversion
1169 appearing as a generic actual parameter for a generic @key{in out} formal
1170 parameter is not a view conversion (and thus is illegal). This confirms
1171 the ACATS tests, so all implementations already follow this intepretation.]}
1172
1173 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
1174 @ChgAdded{Version=[2],Text=[Rules added by the Corrigendum to eliminate
1175 problems with discriminants of aliased components changing were removed, as
1176 we now generally allow discriminants of aliased components to be changed.]}
1177
1178 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00392-01]}
1179 @ChgAdded{Version=[2],Text=[Accessibility checks on conversions involving
1180 types with anonymous access components were added. These components have
1181 the level of the type, and conversions can be between types at different
1182 levels, which could cause dangling access values in the absence of such
1183 checks.]}
1184 @end{DiffWord95}
1185
1186
1187 @LabeledClause{Qualified Expressions}
1188
1189 @begin{Intro}
1190 @Redundant[A @nt<qualified_expression> is used to state explicitly the type,
1191 and to verify the subtype, of an operand that is either an @nt<expression>
1192 or an @nt<aggregate>.
1193 @IndexSeeAlso{Term=[type conversion],See=(qualified_expression)}]
1194 @end{Intro}
1195
1196 @begin{Syntax}
1197 @Syn{lhs=<qualified_expression>,rhs="
1198 @Syn2{subtype_mark}@SingleQuote@;(@Syn2{expression}) | @Syn2{subtype_mark}@SingleQuote@Syn2{aggregate}"}
1199 @end{Syntax}
1200
1201 @begin{Resolution}
1202 @PDefn2{Term=[operand], Sec=(of a @nt{qualified_expression})}
1203 The @i(operand) (the @nt{expression} or @nt{aggregate})
1204 shall resolve to be of the type determined by the @nt{subtype_@!mark},
1205 or a universal type that covers it.
1206 @end{Resolution}
1207
1208 @begin{RunTime}
1209 @PDefn2{Term=[evaluation], Sec=(qualified_expression)}
1210 @IndexCheck{Range_Check}
1211 @IndexCheck{Discriminant_Check}
1212 @IndexCheck{Index_Check}
1213 The evaluation of a @nt{qualified_expression} evaluates the
1214 operand (and if of a universal type, converts it
1215 to the type determined by the @nt{subtype_mark})
1216 and checks that its value belongs to the subtype denoted by
1217 the @nt{subtype_mark}.
1218 @PDefn2{Term=[implicit subtype conversion],Sec=(qualified_expression)}
1219 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
1220 The exception Constraint_Error is raised if this check fails.
1221 @begin{Ramification}
1222 This is one of the few contexts in Ada 95 where implicit subtype conversion
1223 is not performed prior to a constraint check, and hence no
1224 @lquotes@;sliding@rquotes@; of array bounds is provided.
1225 @end{Ramification}
1226 @begin{Reason}
1227 Implicit subtype conversion is not provided because a
1228 @nt<qualified_expression> with a constrained target subtype is
1229 essentially an assertion about the subtype of the operand, rather
1230 than a request for conversion. An explicit @nt<type_conversion> can
1231 be used rather than a @nt<qualified_expression> if subtype
1232 conversion is desired.
1233 @end{Reason}
1234 @end{RunTime}
1235
1236 @begin{Notes}
1237 When a given context does not uniquely identify an expected type,
1238 a @nt<qualified_expression> can be used to do so.
1239 In particular, if an overloaded @nt<name> or
1240 @nt<aggregate> is passed to an overloaded subprogram, it
1241 might be necessary to qualify the operand to resolve its type.
1242 @end{Notes}
1243
1244 @begin{Examples}
1245 @Leading@keepnext@i(Examples of disambiguating expressions using qualification:)
1246 @begin{Example}
1247 @key(type) Mask @key(is) (Fix, Dec, Exp, Signif);
1248 @key(type) Code @key(is) (Fix, Cla, Dec, Tnz, Sub);
1249
1250 Print (Mask'(Dec)); @RI[-- Dec is of type Mask]
1251 Print (Code'(Dec)); @RI[-- Dec is of type Code ]
1252
1253 @key(for) J @key(in) Code'(Fix) .. Code'(Dec) @key(loop) ... @RI[-- qualification needed for either Fix or Dec]
1254 @key(for) J @key(in) Code @key(range) Fix .. Dec @key(loop) ... @RI[-- qualification unnecessary]
1255 @key(for) J @key(in) Code'(Fix) .. Dec @key(loop) ... @RI[-- qualification unnecessary for Dec]
1256
1257 Dozen'(1 | 3 | 5 | 7 => 2, @key(others) => 0) @RI[-- see @RefSecNum{Type Conversions} ]
1258 @end{Example}
1259 @end{Examples}
1260
1261 @LabeledClause{Allocators}
1262
1263 @begin{Intro}
1264 @Redundant[The evaluation of an @nt<allocator> creates an object and yields
1265 an access value that designates the object.
1266 @IndexSee{Term=[new],See=(allocator)}
1267 @IndexSee{Term=[malloc],See=(allocator)}
1268 @IndexSeeAlso{Term=[heap management],See=(allocator)}]
1269 @end{Intro}
1270
1271 @begin{Syntax}
1272 @Syn{lhs=<allocator>,rhs="
1273 @key{new} @Syn2{subtype_indication} | @key{new} @Syn2{qualified_expression}"}
1274 @end{Syntax}
1275
1276 @begin{Resolution}
1277 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0010],ARef=[AI95-00127-01]}
1278 @PDefn2{Term=[expected type],Sec=(allocator)}
1279 The expected type for an @nt<allocator> shall be a single access-to-object
1280 type @Chg{New=[with],Old=[whose]} designated type
1281 @Chg{New=[@i<D> such that either @i<D>],Old=[]} covers the type determined
1282 by the @nt<subtype_mark> of the @nt<subtype_@!indication> or
1283 @nt<qualified_@!expression>@Chg{New=[, or the expected type is anonymous and
1284 the determined type is @i<D>'Class],Old=[]}.
1285 @begin{Discussion}
1286 See @RefSec(The Context of Overload Resolution) for the meaning
1287 of @lquotes@;shall be a single ... type whose ...@rquotes@;
1288 @end{Discussion}
1289 @begin{Ramification}
1290 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0010],ARef=[AI95-00127-01]}
1291 @Chg{New=[An @nt{allocator} is allowed as a controlling parameter of a dispatching
1292 call (see @RefSecNum{Dispatching Operations of Tagged Types}).],Old=[]}
1293 @end{Ramification}
1294 @end{Resolution}
1295
1296 @begin{Legality}
1297 @Defn{initialized allocator}
1298 An @i(initialized) allocator is an @nt{allocator}
1299 with a @nt{qualified_expression}.
1300 @Defn{uninitialized allocator}
1301 An @i(uninitialized) allocator is one with
1302 a @nt{subtype_indication}.
1303 In the @nt<subtype_indication> of an uninitialized allocator, a
1304 @nt<constraint> is permitted only if the @nt<subtype_mark> denotes an
1305 @Redundant[unconstrained] composite subtype;
1306 if there is no @nt<constraint>, then the @nt<subtype_mark>
1307 shall denote a definite subtype.
1308 @IndexSee{Term=[constructor],See=[initialized allocator]}
1309 @begin{Ramification}
1310 For example, ... @key[new] S'Class ... (with no initialization
1311 expression) is illegal,
1312 but ... @key[new] S'Class'(X) ... is legal,
1313 and takes its tag and constraints from the initial value X.
1314 (Note that the former case cannot have a constraint.)
1315 @end{Ramification}
1316
1317 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
1318 If the type of the @nt<allocator> is an access-to-constant type,
1319 the @nt<allocator> shall be an initialized allocator.
1320 @Chg{Version=[2],New=[],Old=[If the designated type is limited,
1321 the @nt<allocator> shall be an uninitialized allocator.]}
1322 @begin{Ramification}
1323 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00287-01]}
1324 @ChgDeleted{Version=[2],Text=[For an access-to-constant type whose designated
1325 type is limited, @nt{allocator}s are illegal.
1326 The Access attribute is legal for such a type, however.]}
1327 @end{Ramification}
1328
1329 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00344-01]}
1330 @ChgAdded{Version=[2],Text=[If the designated type of the type of the
1331 @nt{allocator} is class-wide, the accessibility level of the type determined by the
1332 @nt{subtype_indication} or @nt{qualified_expression} shall not be statically
1333 deeper than that of the type of the @nt{allocator}.]}
1334 @begin{Reason}
1335 @ChgRef{Version=[2],Kind=[AddedNormal]}
1336 @ChgAdded{Version=[2],Text=[This prevents the allocated object from outliving
1337 its type.]}
1338 @end{Reason}
1339
1340 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00416-01]}
1341 @ChgAdded{Version=[2],Text=[If the designated subtype of the type of the
1342 @nt{allocator} has one or more unconstrained access discriminants, then the
1343 accessibility level of the anonymous access type of each access discriminant,
1344 as determined by the @nt{subtype_indication} or @nt{qualified_expression} of
1345 the @nt{allocator}, shall not be statically deeper than that of the type of the
1346 @nt{allocator} (see @RefSecNum{Operations of Access Types}).]}
1347 @begin{Reason}
1348 @ChgRef{Version=[2],Kind=[AddedNormal]}
1349 @ChgAdded{Version=[2],Text=[This prevents the allocated object from outliving
1350 its discriminants.]}
1351 @end{Reason}
1352
1353 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00366-01]}
1354 @ChgAdded{Version=[2],Text=[An @nt{allocator} shall not be of an access type
1355 for which the Storage_Size has been specified by a static expression with value
1356 zero or is defined by the language to be zero. @PDefn{generic contract issue}In
1357 addition to the places where @LegalityTitle normally apply
1358 (see @RefSecNum{Generic Instantiation}), this rule applies also in the private
1359 part of an instance of a generic unit. This rule does not apply in the body of
1360 a generic unit or within a body declared within the declarative region of a
1361 generic unit, if the type of the allocator is a descendant of a formal access
1362 type declared within the formal part of the generic unit.]}
1363
1364 @begin{Reason}
1365 @ChgRef{Version=[2],Kind=[AddedNormal]}
1366 @ChgAdded{Version=[2],Text=[An @nt{allocator} for an access type that has
1367 Storage_Size specified to be zero is required to raise Storage_Error anyway.
1368 It's better to detect the error at compile-time, as the @nt{allocator}
1369 might be executed infrequently. This also simplifies the rules for Pure
1370 units, where we do not want to allow any allocators for library-level access
1371 types, as they would represent state.]}
1372
1373 @ChgRef{Version=[2],Kind=[AddedNormal]}
1374 @ChgAdded{Version=[2],Text=[The last sentence covers the case of children of
1375 generics, and formal access types of formal packages of the generic unit.]}
1376 @end{Reason}
1377
1378
1379 @end{Legality}
1380
1381 @begin{StaticSem}
1382 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00363-01]}
1383 If the designated type of the type of the @nt<allocator> is elementary,
1384 then the subtype of the created object is the designated
1385 subtype.
1386 If the designated type is composite, then the
1387 @Chg{Version=[2],New=[subtype of the ],Old=[]}created object is
1388 @Chg{Version=[2],New=[the designated
1389 subtype when the designated subtype is constrained or there is a partial
1390 view of the designated type that is constrained; otherwise, the
1391 created],Old=[always constrained;
1392 if the designated subtype is constrained,
1393 then it provides the constraint of the created object;
1394 otherwise, the]} object is constrained by its initial value
1395 @Redundant[(even if the designated subtype is unconstrained with defaults)].
1396 @PDefn{constrained by its initial value}
1397 @begin{Discussion}
1398 See AI83-00331.
1399 @end{Discussion}
1400 @begin{Reason}
1401 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00363-01]}
1402 All objects created by an @Chg{Version=[2],New=[@nt{allocator}],Old=[allocator]}
1403 are aliased,
1404 and @Chg{Version=[2],New=[most],Old=[all]} aliased composite objects
1405 need to be constrained so that access subtypes work reasonably.
1406 @Chg{Version=[2],New=[Problematic access subtypes are prohibited for
1407 types with a constrained partial view.],Old=[]}
1408 @end{Reason}
1409 @begin{Discussion}
1410 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
1411 @ChgAdded{Version=[2],Text=[If there is a constrained partial view of the type, this
1412 allows the objects to be unconstrained. This eliminates privacy breaking
1413 (we don't want the objects to act differently simply because they're
1414 allocated). Such a created object is effectively constrained by its initial
1415 value if the access type is an access-to-constant type, or the designated
1416 type is limited (in all views), but we don't need to state that here. It is
1417 implicit in other rules. Note, however, that a value of an
1418 access-to-constant type can designate a variable object via 'Access or
1419 conversion, and the variable object might be assigned by some other access
1420 path, and that assignment might alter the discriminants.]}
1421 @end{Discussion}
1422 @end{StaticSem}
1423
1424 @begin{RunTime}
1425 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
1426 @PDefn2{Term=[evaluation], Sec=(allocator)}
1427 For the evaluation of an @Chg{Version=[2],New=[initialized allocator],
1428 Old=[@nt<allocator>]}, the @Chg{Version=[2],New=[],Old=[elaboration of
1429 the @nt<subtype_indication> or the ]}evaluation of the
1430 @nt<qualified_expression> is performed first.
1431 @PDefn2{Term=[evaluation], Sec=(initialized allocator)}
1432 @Defn2{Term=[assignment operation], Sec=(during evaluation of an
1433 initialized allocator)}
1434 @Chg{Version=[2],New=[An],Old=[For the evaluation of an initialized allocator,
1435 an]} object of the designated type is created and the value of the
1436 @nt<qualified_expression> is converted to the designated subtype
1437 and assigned to the object.
1438 @PDefn2{Term=[implicit subtype conversion],Sec=(initialization expression of allocator)}
1439 @begin{Ramification}
1440 The conversion might raise Constraint_Error.
1441 @end{Ramification}
1442
1443 @PDefn2{Term=[evaluation], Sec=(uninitialized allocator)}
1444 @Leading@keepnext@;@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
1445 For the evaluation of an uninitialized allocator@Chg{Version=[2],New=[, the
1446 elaboration of the @nt{subtype_indication} is performed first. Then],Old=[]}:
1447 @begin(itemize)
1448 @Defn2{Term=[assignment operation], Sec=(during evaluation of an
1449 uninitialized allocator)}
1450 If the designated type is elementary, an object of the
1451 designated subtype is created and any implicit initial value is assigned;
1452
1453 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0002],ARef=[AI95-00171-01]}
1454 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
1455 @Chg{Version=[2],New=[],Old=[@Defn2{Term=[assignment operation],
1456 Sec=(during evaluation of an uninitialized allocator)}]}
1457 If the designated type is composite, an object of the
1458 designated type is created with tag, if any, determined
1459 by the @nt<subtype_mark> of the @nt<subtype_indication>@Chg{Version=[2],New=[.
1460 This object is then initialized by default (see
1461 @RefSecNum{Object Declarations}) using],Old=[;
1462 any per-object constraints on subcomponents are elaborated
1463 @Chg{New=[(see @RefSecNum{Record Types}) ],Old=[]}and any implicit initial
1464 values for the subcomponents of the object are obtained as determined by]}
1465 the @nt<subtype_indication>
1466 @Chg{Version=[2],New=[to determine its nominal subtype],
1467 Old=[and assigned to the corresponding subcomponents]}.
1468 @IndexCheck{Index_Check}
1469 @IndexCheck{Discriminant_Check}
1470 A check is made that the value of the object belongs to the designated
1471 subtype.
1472 @Defn2{Term=[Constraint_Error],Sec=(raised by failure of run-time check)}
1473 Constraint_Error is raised if this check fails.
1474 This check and the initialization of the object are performed in
1475 an arbitrary order.
1476
1477 @begin{Discussion}
1478 AI83-00150.
1479 @end{Discussion}
1480 @end(itemize)
1481
1482 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00344-01],ARef=[AI95-00416-01]}
1483 @ChgAdded{Version=[2],Text=[For any @nt{allocator}, if the designated type of
1484 the type of the @nt{allocator}
1485 is class-wide, then a check is made that the accessibility level of the type
1486 determined by the @nt{subtype_indication}, or by the tag of the value of the
1487 @nt{qualified_expression}, is not
1488 deeper than that of the type of the @nt{allocator}. If the
1489 designated subtype of the @nt{allocator} has one or more unconstrained
1490 access discriminants, then a check is made that the accessibility
1491 level of the anonymous access type of each access discriminant is
1492 not deeper than that of the type of the @nt{allocator}.
1493 Program_Error is raised
1494 if either such check fails.@IndexCheck{Accessibility_Check}
1495 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}]}
1496 @begin{Reason}
1497 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00344-01]}
1498 @ChgAdded{Version=[2],Text=[The accessibility check on class-wide types
1499 prevents the allocated object from outliving its type. We need the run-time
1500 check in instance bodies, or when the type of the @nt{qualified_expression}
1501 is class-wide (other cases are statically detected).]}
1502
1503 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
1504 @ChgAdded{Version=[2],Text=[The accessibility check on access discriminants
1505 prevents the allocated object from outliving its discriminants.]}
1506 @end{Reason}
1507
1508 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00280-01]}
1509 @Chg{Version=[2],New=[If the object to be created by an @nt<allocator> has a
1510 controlled or protected part, and the finalization of the collection of the
1511 type of the @nt{allocator} (see @RefSecNum{Completion and Finalization}) has
1512 started, Program_Error is raised.@IndexCheck{Allocation_Check}
1513 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}],Old=[]}
1514 @begin{Reason}
1515 @ChgRef{Version=[2],Kind=[AddedNormal]}
1516 @ChgAdded{Version=[2],Text=[If the object has a controlled or protected part, its
1517 finalization is likely to be non-trivial. If the allocation was allowed,
1518 we could not know whether the finalization would actually be performed.
1519 That would be dangerous to otherwise safe abstractions, so we mandate
1520 a check here. On the other hand, if the finalization of the object will
1521 be trivial, we do not require (but allow) the check, as no real harm
1522 could come from late allocation.]}
1523 @end{Reason}
1524 @begin{Discussion}
1525 @ChgRef{Version=[2],Kind=[AddedNormal]}
1526 @ChgAdded{Version=[2],Text=[This check can only fail if an @nt{allocator}
1527 is evaluated in code reached from a Finalize routine for a type declared
1528 in the same master. That's highly unlikely; Finalize routines are much
1529 more likely to be deallocating objects than allocating them.]}
1530 @end{Discussion}
1531
1532 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00280-01]}
1533 @Chg{Version=[2],New=[If the object to be created by an @nt<allocator>
1534 contains any tasks, and the master of the type of the @nt<allocator> is
1535 completed, and all of the dependent tasks of the master are terminated
1536 (see @RefSecNum{Task Dependence - Termination of Tasks}), then
1537 Program_Error is raised.@IndexCheck{Allocation_Check}
1538 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}],Old=[]}
1539 @begin{Reason}
1540 @ChgRef{Version=[2],Kind=[AddedNormal]}
1541 @ChgAdded{Version=[2],Text=[A task created after waiting for tasks has
1542 finished could depend on freed data structures, and certainly would never
1543 be awaited.]}
1544 @end{Reason}
1545
1546 @Redundant[If the created object contains any tasks,
1547 they are activated
1548 (see @RefSecNum(Task Execution - Task Activation)).]
1549 Finally, an access value that designates the created object is returned.
1550 @end{RunTime}
1551
1552 @begin{Bounded}
1553 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00280-01]}
1554 @ChgAdded{Version=[2],Text=[@PDefn2{Term=(bounded error),Sec=(cause)} It is a
1555 bounded error if the finalization of the collection of the type (see
1556 @RefSecNum{Completion and Finalization}) of the @nt<allocator> has started. If
1557 the error is detected, Program_Error is raised. Otherwise, the allocation
1558 proceeds normally.]}
1559 @begin{Discussion}
1560 @ChgRef{Version=[2],Kind=[AddedNormal]}
1561 @ChgAdded{Version=[2],Text=[This check is required in some cases; see above.]}
1562 @end{Discussion}
1563 @end{Bounded}
1564
1565 @begin{Notes}
1566 Allocators cannot create objects of an abstract type.
1567 See @RefSecNum{Abstract Types and Subprograms}.
1568
1569 If any part of the created object is controlled, the initialization
1570 includes calls on corresponding Initialize or Adjust procedures.
1571 See @RefSecNum{User-Defined Assignment and Finalization}.
1572
1573 As explained in @RefSec{Storage Management},
1574 the storage for an object allocated by an @nt{allocator} comes from a
1575 storage pool (possibly user defined).
1576 @Defn2{Term=[Storage_Error],Sec=(raised by failure of run-time check)}
1577 The exception Storage_Error is raised by an @nt<allocator> if there
1578 is not enough storage.
1579 Instances of Unchecked_Deallocation may be used to explicitly reclaim
1580 storage.
1581
1582 Implementations are permitted, but not required,
1583 to provide garbage collection (see @RefSecNum{Pragma Controlled}).
1584 @begin{Ramification}
1585 Note that in an @nt<allocator>,
1586 the exception Constraint_Error can be
1587 raised by the evaluation of the @nt<qualified_expression>,
1588 by the elaboration of the @nt<subtype_indication>, or by the
1589 initialization.
1590 @end{Ramification}
1591 @begin{Discussion}
1592 By default, the implementation provides the storage pool.
1593 The user may exercise more control over storage management by
1594 associating a user-defined pool with an access type.
1595 @end{Discussion}
1596 @end{Notes}
1597
1598 @begin{Examples}
1599 @Leading@keepnext@i{Examples of allocators:}
1600 @begin{Example}
1601 @key(new) Cell'(0, @key(null), @key(null)) @RI[-- initialized explicitly, see @RefSecNum{Incomplete Type Declarations}]
1602 @key(new) Cell'(Value => 0, Succ => @key(null), Pred => @key(null)) @RI[-- initialized explicitly]
1603 @key(new) Cell @RI[-- not initialized]
1604
1605 @key(new) Matrix(1 .. 10, 1 .. 20) @RI[-- the bounds only are given]
1606 @key(new) Matrix'(1 .. 10 => (1 .. 20 => 0.0)) @RI[-- initialized explicitly]
1607
1608 @key(new) Buffer(100) @RI[-- the discriminant only is given]
1609 @key(new) Buffer'(Size => 80, Pos => 0, Value => (1 .. 80 => 'A')) @RI[-- initialized explicitly]
1610
1611 Expr_Ptr'(@key(new) Literal) @RI[-- allocator for access-to-class-wide type, see @RefSecNum{Type Extensions}]
1612 Expr_Ptr'(@key(new) Literal'(Expression @key[with] 3.5)) @RI[-- initialized explicitly]
1613
1614 @end{Example}
1615 @end{Examples}
1616
1617 @begin{Incompatible83}
1618 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00019}
1619 @Defn{incompatibilities with Ada 83}
1620 The @nt<subtype_indication> of an uninitialized allocator may
1621 not have an explicit @nt<constraint> if the designated type is an access type.
1622 In Ada 83, this was permitted even though the @nt<constraint> had
1623 no @Chg{New=[e],Old=[a]}ffect on the subtype of the created object.
1624 @end{Incompatible83}
1625
1626 @begin{Extend83}
1627 @Defn{extensions to Ada 83}
1628 Allocators creating objects of type @i(T)
1629 are now overloaded on access types designating
1630 @i(T')Class and all class-wide types that cover @i(T).
1631
1632 Implicit array subtype conversion (sliding) is now performed
1633 as part of an initialized allocator.
1634 @end{Extend83}
1635
1636 @begin{DiffWord83}
1637 We have used a new organization, inspired by the ACID
1638 document, that makes it clearer what is the subtype of
1639 the created object, and what subtype conversions take place.
1640
1641 Discussion of storage management issues,
1642 such as garbage collection and the raising of Storage_Error,
1643 has been moved to @RefSec{Storage Management}.
1644 @end{DiffWord83}
1645
1646 @begin{Inconsistent95}
1647 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
1648 @ChgAdded{Version=[2],Text=[@Defn{inconsistencies with Ada 95}If the
1649 designated type has a constrained partial view,
1650 the allocated object can be unconstrained. This might cause the object to
1651 take up a different amount of memory, and might cause the operations to work
1652 where they previously would have raised Constraint_Error. It's unlikely that
1653 the latter would actually matter in a real program (Constraint_Error usually
1654 indicates a bug that would be fixed, not left in a program.) The former
1655 might cause Storage_Error to be raised at a different time than in an Ada 95
1656 program.]}
1657 @end{Inconsistent95}
1658
1659 @begin{Incompatible95}
1660 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00366-01]}
1661 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}An
1662 @nt{allocator} for an access type that has Storage_Size specified to be
1663 zero is now illegal. Ada 95 allowed the @nt{allocator}, but it had to
1664 raise Storage_Error if executed. The primary impact of this change should
1665 be to detect bugs.]}
1666 @end{Incompatible95}
1667
1668 @begin{Extend95}
1669 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0010],ARef=[AI95-00127-01]}
1670 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95} @b<Corrigendum:> An
1671 @nt{allocator} can be a controlling parameter of a dispatching call. This
1672 was an oversight in Ada 95.]}
1673
1674 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
1675 @ChgAdded{Version=[2],Text=[Initialized @nt{allocator}s are allowed when
1676 the designated type is limited.]}
1677 @end{Extend95}
1678
1679 @begin{DiffWord95}
1680 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0002],ARef=[AI95-00171-01]}
1681 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Clarified the elaboration of
1682 per-object constraints for an uninitialized allocator.]}
1683
1684 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00280-01]}
1685 @ChgAdded{Version=[2],Text=[Program_Error is now raised if the @nt{allocator}
1686 occurs after the finalization of the collection or the waiting for tasks.
1687 This is not listed as an incompatibility as the Ada 95 behavior was
1688 unspecified, and Ada 95 implementations tend to generate programs that crash
1689 in this case.]}
1690
1691 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00344-01]}
1692 @ChgAdded{Version=[2],Text=[Added accessibility checks to class-wide
1693 @nt{allocator}s. These checks could not fail in Ada 95 (as all of the
1694 designated types had to be declared at the same level, so the access type
1695 would necessarily have been at the same level or more nested than the type
1696 of allocated object).]}
1697
1698 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00373-01]}
1699 @ChgAdded{Version=[2],Text=[Revised the description of evaluation of
1700 uninitialized allocators to use @lquotes@;initialized by default@rquotes
1701 so that the ordering requirements are the same for all kinds of objects
1702 that are default-initialized.]}
1703
1704 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00416-01]}
1705 @ChgAdded{Version=[2],Text=[Added accessibility checks to access
1706 discriminants of @nt{allocator}s. These checks could not fail in Ada 95
1707 as the discriminants always have the accessibility of the object.]}
1708 @end{DiffWord95}
1709
1710
1711 @LabeledClause{Static Expressions and Static Subtypes}
1712
1713 @begin{Intro}
1714 Certain expressions of a scalar or string type are defined to be static.
1715 Similarly, certain discrete ranges are defined to be static, and
1716 certain scalar and string subtypes are defined to be static subtypes.
1717 @Redundant[@Defn{static}
1718 @i(Static) means determinable at compile time,
1719 using the declared properties or values of the program entities.]
1720 @IndexSeeAlso{Term=[constant],See=(static)}
1721 @begin{Discussion}
1722 As opposed to more elaborate data flow analysis, etc.
1723 @end{Discussion}
1724 @end{Intro}
1725
1726 @begin{MetaRules}
1727 For an expression to be static,
1728 it has to be calculable at compile time.
1729
1730 Only scalar and string expressions are static.
1731
1732 To be static, an expression cannot have any nonscalar, nonstring
1733 subexpressions (though it can have nonscalar constituent @nt<name>s).
1734 A static scalar expression cannot have any nonscalar subexpressions.
1735 There is one exception @em a membership test for a string subtype
1736 can be static, and the result is scalar, even though a subexpression
1737 is nonscalar.
1738
1739 The rules for evaluating static expressions are designed
1740 to maximize portability of static calculations.
1741 @end{MetaRules}
1742
1743 @begin{Intro}
1744 @Leading@Defn2{Term=[static], Sec=(expression)}
1745 A static expression is
1746 @Redundant[a scalar or string expression that is]
1747 one of the following:
1748
1749 @begin{Itemize}
1750 a @nt{numeric_literal};
1751 @begin{Ramification}
1752 A @nt<numeric_literal> is always a static expression, even if
1753 its expected type is not that of a static subtype. However, if its value
1754 is explicitly converted to, or qualified by, a nonstatic subtype,
1755 the resulting expression is nonstatic.
1756 @end{Ramification}
1757
1758 a @nt{string_literal} of a static string subtype;
1759 @begin(Ramification)
1760 That is, the constrained subtype defined by the index range
1761 of the string is static. Note that elementary values don't
1762 generally have subtypes, while composite values do (since
1763 the bounds or discriminants are inherent in the value).
1764 @end(Ramification)
1765
1766 a @nt{name}
1767 that denotes the declaration
1768 of a named number or a static constant;
1769 @begin{Ramification}
1770 Note that enumeration
1771 literals are covered by the @nt{function_call} case.
1772 @end{Ramification}
1773
1774 a @nt{function_call}
1775 whose @SynI{function_}@nt{name} or
1776 @SynI{function_}@nt{prefix} statically denotes a static function,
1777 and whose actual parameters, if any (whether given explicitly or by default),
1778 are all static expressions;
1779 @begin{Ramification}
1780 This includes uses of operators that are equivalent to
1781 @nt{function_call}s.
1782 @end{Ramification}
1783
1784
1785 an @nt{attribute_reference} that denotes a scalar value,
1786 and whose @nt{prefix} denotes a static scalar subtype;
1787
1788 @begin{Ramification}
1789 Note that this does not include the case of an attribute
1790 that is a function;
1791 a reference to
1792 such an attribute is not even an expression.
1793 See above for function @i{calls}.
1794
1795 An implementation may define the staticness and other
1796 properties of implementation-defined attributes.
1797 @end{Ramification}
1798
1799 an @nt{attribute_reference} whose
1800 @nt{prefix} statically denotes a statically constrained array
1801 object or array subtype,
1802 and whose @nt<attribute_designator>
1803 is First, Last, or Length,
1804 with an optional dimension;
1805
1806 a @nt{type_conversion}
1807 whose @nt{subtype_mark} denotes a static scalar subtype,
1808 and whose operand is a static expression;
1809
1810 a @nt{qualified_expression}
1811 whose @nt{subtype_mark} denotes a
1812 static @Redundant[(scalar or string)] subtype,
1813 and whose operand is a static expression;
1814 @begin{Ramification}
1815 This rules out the @nt{subtype_mark}'@nt{aggregate} case.
1816 @end{Ramification}
1817 @begin{Reason}
1818 Adding qualification to an expression shouldn't make it nonstatic, even
1819 for strings.
1820 @end{Reason}
1821
1822 a membership test
1823 whose @nt{simple_expression} is a static expression,
1824 and whose @nt{range} is a static range
1825 or whose @nt{subtype_mark} denotes a
1826 static @Redundant[(scalar or string)] subtype;
1827 @begin{Reason}
1828 Clearly, we should allow membership tests in exactly the same cases
1829 where we allow @nt{qualified_expression}s.
1830 @end{Reason}
1831
1832 a short-circuit control form
1833 both of whose @nt{relation}s are static expressions;
1834
1835 a static expression enclosed in parentheses.
1836 @end{Itemize}
1837 @begin(Discussion)
1838 @Defn2{Term=[static], Sec=(value)}
1839 Informally, we talk about a @i(static value). When we do,
1840 we mean a value specified by a static expression.
1841 @end(Discussion)
1842 @begin{Ramification}
1843 The language requires a static
1844 expression in a @nt<number_declaration>,
1845 a numeric type definition, a @nt<discrete_choice> (sometimes),
1846 certain representation items, an @nt<attribute_designator>,
1847 and when specifying the value of a discriminant
1848 governing a @nt{variant_part}
1849 in a @nt<record_aggregate> or @nt<extension_aggregate>.
1850 @end{Ramification}
1851
1852 @Leading@Defn2{Term=[statically], Sec=(denote)}
1853 A @nt{name} @i(statically denotes) an entity if it
1854 denotes the entity and:
1855 @begin(itemize)
1856 It is a @nt<direct_name>, expanded name, or
1857 @nt{character_literal},
1858 and it denotes a declaration other than a @nt<renaming_declaration>;
1859 or
1860
1861 It is an @nt{attribute_reference} whose @nt{prefix} statically denotes
1862 some entity; or
1863
1864 It denotes a @nt<renaming_declaration> with a @nt<name> that
1865 statically denotes the renamed entity.
1866 @end(itemize)
1867 @begin{Ramification}
1868 @nt{Selected_component}s that are not expanded names
1869 and @nt{indexed_component}s do not statically denote things.
1870 @end{Ramification}
1871
1872 @Leading@Defn2{Term=[static], Sec=(function)}
1873 A @i{static function} is one of the following:
1874 @begin{Ramification}
1875 These are the functions whose calls can be static expressions.
1876 @end{Ramification}
1877 @begin{Itemize}
1878 a predefined operator whose parameter and result
1879 types are all scalar types none of which are descendants of
1880 formal scalar types;
1881
1882 a predefined concatenation operator whose result type is a string type;
1883
1884 an enumeration literal;
1885
1886 a language-defined attribute that is a function,
1887 if the @nt{prefix} denotes a static scalar subtype,
1888 and if the parameter and result types are scalar.
1889 @end{Itemize}
1890
1891 In any case, a generic formal subprogram is not a static function.
1892
1893 @Defn2{Term=[static], Sec=(constant)}
1894 A @i(static constant) is
1895 a constant view declared by a full constant declaration
1896 or an @nt<object_@!renaming_@!declaration> with a static nominal subtype,
1897 having a value defined by a static scalar expression or by
1898 a static string expression whose value has a length not exceeding
1899 the maximum length of a @nt{string_@!literal} in the implementation.
1900 @begin{Ramification}
1901 A deferred constant is not static;
1902 the view introduced by the corresponding full constant declaration
1903 can be static.
1904 @end{Ramification}
1905 @begin{Reason}
1906 The reason for restricting the length of static string constants is so
1907 that compilers don't have to store giant strings in their symbol tables.
1908 Since most string constants will be initialized
1909 from @nt{string_literal}s, the length limit seems pretty natural.
1910 The reason for avoiding nonstring types is also to save symbol table
1911 space.
1912 We're trying to keep it cheap and simple
1913 (from the implementer's viewpoint),
1914 while still allowing, for example,
1915 the link name of a pragma Import to contain
1916 a concatenation.
1917
1918 The length we're talking about is the maximum number of characters in
1919 the value represented by a @nt{string_literal},
1920 not the number of characters in the source representation;
1921 the quotes don't count.
1922
1923 @end{Reason}
1924
1925 @Defn2{Term=[static], Sec=(range)}
1926 A @i(static range) is a @nt{range} whose bounds are
1927 static expressions,
1928 @Redundant[or a @nt<range_@!attribute_@!reference> that is equivalent to
1929 such a @nt<range>.]
1930 @Defn2{Term=[static], Sec=(discrete_range)}
1931 A @i(static @nt<discrete_@!range>) is one that is a static range
1932 or is a @nt<subtype_@!indication> that defines a static scalar subtype.
1933 The base range of a scalar type is a static range, unless the
1934 type is a descendant of a formal scalar type.
1935
1936 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00263-01]}
1937 @Defn2{Term=[static], Sec=(subtype)}
1938 A @i(static subtype) is either a @i(static scalar subtype) or a
1939 @i(static string subtype).
1940 @Defn2{Term=[static], Sec=(scalar subtype)}
1941 A static scalar subtype is an unconstrained scalar subtype whose
1942 type is not a descendant of a
1943 formal @Chg{Version=[2],New=[],Old=[scalar ]}type, or
1944 a constrained scalar subtype formed by imposing a compatible
1945 static constraint on a static scalar subtype.
1946 @Defn2{Term=[static], Sec=(string subtype)}
1947 A static string subtype is an unconstrained string subtype
1948 whose index subtype and component subtype are
1949 static@Chg{Version=[2],New=[],Old=[ (and whose type is not a descendant
1950 of a formal array type)]},
1951 or a constrained string subtype formed by imposing a compatible static
1952 constraint on a static string subtype.
1953 In any case, the subtype of a generic formal object of mode @key[in out],
1954 and the result subtype of a generic formal function, are not static.
1955 @begin{Ramification}
1956 String subtypes are the only composite subtypes that can be static.
1957 @end{Ramification}
1958 @begin{Reason}
1959 @Leading@;The part about generic formal objects of mode @key[in out]
1960 is necessary because the subtype of the formal is not required
1961 to have anything to do with the subtype of the actual.
1962 For example:
1963 @begin{Example}
1964 @key[subtype] Int10 @key[is] Integer @key[range] 1..10;
1965
1966 @key[generic]
1967 F : @key[in] @key[out] Int10;
1968 @key[procedure] G;
1969
1970 @key[procedure] G @key[is]
1971 @key[begin]
1972 @key[case] F @key[is]
1973 @key[when] 1..10 => @key[null];
1974 --@RI{ Illegal!}
1975 @key[end] @key[case];
1976 @key[end] G;
1977
1978 X : Integer @key[range] 1..20;
1979 @key[procedure] I @key[is] @key[new] G(F => X); --@RI{ OK.}
1980 @end{Example}
1981
1982 The @nt{case_statement} is illegal, because the subtype of F is not
1983 static, so the choices have to cover all values of Integer,
1984 not just those in the range 1..10.
1985 A similar issue arises for generic formal functions,
1986 now that function calls are object names.
1987 @end{Reason}
1988
1989 @Leading@Defn2{Term=[static], Sec=(constraint)}
1990 The different kinds of @i(static constraint) are defined as follows:
1991 @begin(itemize)
1992 A null constraint is always static;
1993
1994 @Defn2{Term=[static], Sec=(range constraint)}
1995 @Defn2{Term=[static], Sec=(digits constraint)}
1996 @Defn2{Term=[static], Sec=(delta constraint)}
1997 A scalar constraint is static if it has no
1998 @nt<range_constraint>,
1999 or one with a static range;
2000
2001 @Defn2{Term=[static], Sec=(index constraint)}
2002 An index constraint is static if each
2003 @nt<discrete_range> is static, and each index subtype of the
2004 corresponding array type is static;
2005
2006 @Defn2{Term=[static], Sec=(discriminant constraint)}
2007 A discriminant constraint is static if
2008 each @nt<expression> of the constraint is static,
2009 and the subtype of each discriminant is static.
2010 @end(itemize)
2011
2012 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00311-01]}
2013 @Chg{Version=[2],New=[In any case, the constraint of the first subtype of a
2014 scalar formal type is neither static nor null.],Old=[]}
2015
2016 @Defn2{Term=[statically], Sec=(constrained)}
2017 A subtype is @i(statically constrained) if it is constrained,
2018 and its constraint is static.
2019 An object is @i(statically constrained) if its nominal subtype is
2020 statically constrained,
2021 or if it is a static string constant.
2022 @end{Intro}
2023
2024 @begin{Legality}
2025 @Leading@;A static expression is evaluated at compile time except when it is part
2026 of the right operand of a static short-circuit control form whose value
2027 is determined by its left operand.
2028 This evaluation is performed exactly,
2029 without performing Overflow_Checks.
2030 For a static expression that is evaluated:
2031 @begin{Itemize}
2032 The expression is illegal if its evaluation fails a language-defined
2033 check other than Overflow_@!Check.
2034
2035 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00269-01]}
2036 If the expression is not part of a larger static
2037 expression@Chg{Version=[2],New=[ and the expression is expected to be of a
2038 single specific type],Old=[]},
2039 then its value shall be within the base range of its expected type.
2040 Otherwise, the value may be arbitrarily large or small.
2041 @begin{Ramification}
2042 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00269-01]}
2043 @ChgAdded{Version=[2],Text=[If the expression is expected to be of a universal
2044 type, or of @lquotes@;any integer type@rquotes, there are no limits on the
2045 value of the expression.]}
2046 @end{Ramification}
2047
2048 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00269-01]}
2049 If the expression is of type @i<universal_real> and its expected type is
2050 a decimal fixed point type,
2051 then its value shall be a multiple of the @i<small> of the decimal
2052 type.@Chg{Version=[2],New=[ This restriction
2053 does not apply if the expected type is a
2054 descendant of a formal scalar type
2055 (or a corresponding actual type in an instance).],Old=[]}
2056 @begin{Ramification}
2057 This means that a @nt{numeric_literal} for a decimal type cannot have
2058 @lquotes@;extra@rquotes@; significant digits.
2059 @end{Ramification}
2060 @begin{Reason}
2061 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00269-01]}
2062 @ChgAdded{Version=[2],Text=[The small is not known for a generic formal
2063 type, so we have to exclude formal types from this check.]}
2064 @end{Reason}
2065 @end{Itemize}
2066
2067 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00269-01]}
2068 @Chg{Version=[2],New=[@PDefn{generic contract issue}
2069 In addition to the places where @LegalityTitle normally apply
2070 (see @RefSecNum{Generic Instantiation}),
2071 the above restrictions also apply in the private part of an
2072 instance of a generic unit.],Old=[The last two restrictions above
2073 do not apply if the expected type is a descendant of a formal scalar type
2074 (or a corresponding actual type in an instance).]}
2075
2076 @begin{Discussion}
2077 Values outside the base range are not permitted
2078 when crossing from the @lquotes@;static@rquotes@; domain to the @lquotes@;dynamic@rquotes@; domain.
2079 This rule is designed to enhance portability of programs
2080 containing static expressions.
2081 Note that this rule applies to the exact value,
2082 not the value after any rounding or truncation.
2083 (See below for the rounding and truncation requirements.)
2084
2085 @Leading@;Short-circuit control forms are a special case:
2086 @begin{Example}
2087 N: @key[constant] := 0.0;
2088 X: @key[constant] Boolean := (N = 0.0) @key[or] @key[else] (1.0/N > 0.5); --@RI{ Static.}
2089 @end{Example}
2090
2091 The declaration of X is legal, since the divide-by-zero part of the
2092 expression is not evaluated.
2093 X is a static constant equal to True.
2094
2095 @end{Discussion}
2096 @begin{Ramification}
2097 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00269-01]}
2098 @ChgDeleted{Version=[2],Text=[There is no requirement to recheck these rules
2099 in an instance; the base range check will generally be performed at run time
2100 anyway.]}
2101 @end{Ramification}
2102 @end{Legality}
2103
2104 @begin{ImplReq}
2105 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00268-01],ARef=[AI95-00269-01]}
2106 For a real static expression that is not part of a larger static
2107 expression,
2108 and whose expected type is not a descendant of a formal
2109 @Chg{Version=[2],New=[],Old=[scalar ]}type,
2110 the implementation shall round or truncate
2111 the value (according to the Machine_Rounds
2112 attribute of the expected type) to the nearest machine
2113 number of the expected type;
2114 if the value is exactly half-way between two machine
2115 numbers, @Chg{Version=[2],New=[the],Old=[any]} rounding
2116 @Chg{Version=[2],New=[],Old=[shall be ]}performed
2117 @Chg{Version=[2],New=[is implementation-defined],Old=[away from zero]}.
2118 If the expected type is a descendant of a formal
2119 @Chg{Version=[2],New=[],Old=[scalar ]}type,
2120 @Chg{Version=[2],New=[or if the static expression appears in
2121 the body of an instance of a generic unit and the corresponding expression is
2122 nonstatic in the corresponding generic body, then],Old=[]}
2123 no special rounding or truncating is required @em normal
2124 accuracy rules apply (see @RefSecNum(Numerics)).
2125 @ChgImplDef{Version=[2],Kind=[Added],Text=[@Chg{Version=[2],New=[Rounding of
2126 real static expressions which are exactly half-way between two machine numbers.],Old=[]}]}
2127 @begin{Reason}
2128 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00268-01]}
2129 Discarding extended precision enhances portability
2130 by ensuring that the value of a
2131 static constant of a real type is always a machine number of the type.
2132 @Chg{Version=[2],New=[],Old=[Deterministic rounding of exact halves also enhances portability.]}
2133
2134 When the expected type is a descendant of a formal floating point type,
2135 extended precision (beyond that of the machine numbers)
2136 can be retained when evaluating
2137 a static expression, to ease code sharing for generic
2138 instantiations. For similar reasons,
2139 normal (nondeterministic) rounding or truncating rules apply
2140 for descendants of a formal fixed point type.
2141
2142 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00269-01]}
2143 @ChgAdded{Version=[2],Text=[There is no requirement for exact evaluation or
2144 special rounding in an instance body (unless the expression is static in
2145 the generic body). This eliminates a potential contract issue where the
2146 exact value of a static expression depends on the actual parameters (which
2147 could then affect the legality of other code).]}
2148 @end{Reason}
2149 @begin{ImplNote}
2150
2151 Note that the implementation of static expressions has to keep track
2152 of plus and minus zero for a type whose Signed_Zeros attribute is
2153 True.
2154
2155 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00100-01]}
2156 Note that the only @Chg{Version=[2],New=[machine numbers],Old=[values]} of
2157 a fixed point type are the multiples of
2158 the small, so a static conversion to a fixed-point type, or division
2159 by an integer, must do truncation to a multiple of small.
2160 It is not correct for the implementation to do all static calculations
2161 in infinite precision.
2162
2163 @end{ImplNote}
2164
2165 @end{ImplReq}
2166
2167 @begin{ImplAdvice}
2168 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00268-01]}
2169 @ChgAdded{Version=[2],Text=[For a real static expression that is not part of a
2170 larger static expression, and whose expected type is not a descendant of a
2171 formal type, the rounding should be the same as the default rounding
2172 for the target system.]}
2173 @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],
2174 Text=[For a real static expression with a non-formal type that is not part of a
2175 larger static expression should be rounded the same as the target system.]}]}
2176 @end{ImplAdvice}
2177
2178 @begin{Notes}
2179 An expression can be static even if it occurs in a context where
2180 staticness is not required.
2181 @begin{Ramification}
2182
2183 @Leading@keepnext@;For example:
2184 @begin{Example}
2185 X : Float := Float'(1.0E+400) + 1.0 - Float'(1.0E+400);
2186 @end{Example}
2187
2188 The expression is static,
2189 which means that the value of X must be exactly 1.0,
2190 independent of the accuracy or range of the run-time floating point
2191 implementation.
2192
2193 The following kinds of expressions are never static:
2194 @nt{explicit_dereference},
2195 @nt{indexed_component},
2196 @nt{slice},
2197 @key{null},
2198 @nt{aggregate},
2199 @nt{allocator}.
2200 @end{Ramification}
2201
2202 A static (or run-time) @nt<type_conversion> from a real type to
2203 an integer type performs rounding. If the operand value is exactly half-way
2204 between two integers, the rounding is performed away from zero.
2205 @begin{Reason}
2206 We specify this for portability. The reason for not choosing
2207 round-to-nearest-even, for example, is that this method is easier
2208 to undo.
2209 @end{Reason}
2210 @begin{Ramification}
2211 The attribute Truncation
2212 (see @RefSecNum{Attributes of Floating Point Types})
2213 can be used to perform a (static) truncation prior to conversion,
2214 to prevent rounding.
2215 @end{Ramification}
2216 @begin{ImplNote}
2217
2218 The value of the literal
2219 0E999999999999999999999999999999999999999999999
2220 is zero.
2221 The implementation must take care to evaluate such literals properly.
2222
2223 @end{ImplNote}
2224 @end{Notes}
2225
2226 @begin{Examples}
2227 @Leading@keepnext@i(Examples of static expressions:)
2228 @begin{Example}
2229 1 + 1 @RI[-- 2]
2230 @key(abs)(-10)*3 @RI[-- 30]
2231
2232 Kilo : @key(constant) := 1000;
2233 Mega : @key(constant) := Kilo*Kilo; @RI[-- 1_000_000]
2234 Long : @key(constant) := Float'Digits*2;
2235
2236 Half_Pi : @key(constant) := Pi/2; @RI[-- see @RefSecNum(Number Declarations)]
2237 Deg_To_Rad : @key(constant) := Half_Pi/90;
2238 Rad_To_Deg : @key(constant) := 1.0/Deg_To_Rad; @RI[-- equivalent to 1.0/((3.14159_26536/2)/90)]
2239 @end{Example}
2240 @end{Examples}
2241
2242 @begin{Extend83}
2243 @Defn{extensions to Ada 83}
2244 The rules for static expressions and static subtypes are generalized
2245 to allow more kinds of compile-time-known expressions to be used
2246 where compile-time-known values are required, as follows:
2247 @begin(itemize)
2248 Membership tests and short-circuit control forms
2249 may appear in a static expression.
2250
2251 The bounds and length of
2252 statically constrained array objects or subtypes are static.
2253
2254 The Range attribute of a statically constrained array subtype or
2255 object gives a static range.
2256
2257 A @nt{type_conversion} is static if the @nt{subtype_mark} denotes a
2258 static scalar subtype and the operand is a static expression.
2259
2260 All numeric literals are now static, even if the expected
2261 type is a formal scalar type.
2262 This is useful in @nt{case_statement}s and @nt{variant_part}s,
2263 which both now allow a value of a formal scalar type to
2264 control the selection, to
2265 ease conversion of a package into a generic package.
2266 Similarly, named array aggregates are also permitted for array
2267 types with an index type that is a formal scalar type.
2268 @end(itemize)
2269
2270 The rules for the evaluation of static expressions
2271 are revised to require exact evaluation at compile time,
2272 and force a machine number result when crossing from the static realm
2273 to the dynamic realm,
2274 to enhance portability and predictability.
2275 Exact evaluation is not required for
2276 descendants of a formal scalar type,
2277 to simplify generic code sharing and to avoid generic
2278 contract model problems.
2279
2280 @Leading@;Static expressions are legal even if an intermediate
2281 in the expression goes outside the base range of the type. Therefore, the
2282 following will succeed in Ada 95, whereas it might raise an
2283 exception in Ada 83:
2284 @begin{Example}
2285 @key[type] Short_Int @key[is] @key[range] -32_768 .. 32_767;
2286 I : Short_Int := -32_768;
2287 @end{Example}
2288
2289 This might raise an exception in Ada 83 because "32_768" is out of range,
2290 even though "@en@;32_768" is not. In Ada 95, this will always succeed.
2291
2292
2293 Certain expressions involving string operations
2294 (in particular concatenation and membership tests)
2295 are considered static in Ada 95.
2296
2297
2298 The reason for this change is to simplify the rule requiring
2299 compile-time-known string expressions as the link name in an interfacing
2300 pragma, and to simplify the preelaborability rules.
2301 @end{Extend83}
2302
2303 @begin{Incompatible83}
2304 @Defn{incompatibilities with Ada 83}
2305 An Ada 83 program that uses an out-of-range static value
2306 is illegal in Ada 95, unless the expression is part of a larger
2307 static expression, or the expression is not evaluated due to being on
2308 the right-hand side of a short-circuit control form.
2309 @end{Incompatible83}
2310
2311 @begin{DiffWord83}
2312 This clause (and @RefSec{Multiplying Operators})
2313 subsumes the RM83 section on Universal Expressions.
2314
2315 The existence of static string expressions
2316 necessitated changing the definition of static subtype to
2317 include string subtypes.
2318 Most occurrences of "static subtype" have been changed to "static scalar
2319 subtype",
2320 in order to preserve the effect of the Ada 83 rules.
2321 This has the added benefit of clarifying the difference between "static
2322 subtype" and "statically constrained subtype", which has been a source
2323 of confusion.
2324 In cases where we allow static string subtypes,
2325 we explicitly use phrases like "static string subtype"
2326 or "static (scalar or string) subtype",
2327 in order to clarify the meaning for those who have gotten used to the
2328 Ada 83 terminology.
2329
2330
2331 @Leading@;In Ada 83, an expression was considered nonstatic if it raised an
2332 exception.
2333 Thus, for example:
2334 @begin{Example}
2335 Bad: @key[constant] := 1/0; --@RI{ Illegal!}
2336 @end{Example}
2337
2338 was illegal because 1/0 was not static.
2339 In Ada 95, the above example is still illegal,
2340 but for a different reason:
2341 1/0 is static, but there's a separate rule forbidding the exception
2342 raising.
2343
2344 @end{DiffWord83}
2345
2346 @begin{Inconsistent95}
2347 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00268-01]}
2348 @ChgAdded{Version=[2],Text=[@Defn{inconsistencies with Ada 95}
2349 @b[Amendment Correction:] Rounding of
2350 static real expressions is implementation-defined in Ada 2005, while it was
2351 specified as away from zero in (original) Ada 95. This could make subtle
2352 differences in programs. However, the original Ada 95 rule required rounding
2353 that (probably) differed from the target processor, thus creating anomalies
2354 where the value of a static expression was required to be different than the
2355 same expression evaluated at run-time.]}
2356 @end{Inconsistent95}
2357
2358 @begin{DiffWord95}
2359 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00263-01],ARef=[AI95-00268-01]}
2360 @ChgAdded{Version=[2],Text=[The Ada 95 wording that defined static subtypes
2361 unintentionally failed to exclude formal derived types that happen to be
2362 scalar (these aren't formal scalar types); and had a parenthetical remark
2363 excluding formal string types - but that was neither necessary nor
2364 parenthetical (it didn't follow from other wording). This issue also
2365 applies to the rounding rules for real static expressions.]}
2366
2367 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00269-01]}
2368 @ChgAdded{Version=[2],Text=[Ada 95 didn't clearly define the bounds of a value of
2369 a static expression for universal types and for "any integer/float/fixed
2370 type". We also make it clear that we do not intend exact evaluation of
2371 static expressions in an instance body if the expressions aren't static in the
2372 generic body.]}
2373
2374 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00311-01]}
2375 @ChgAdded{Version=[2],Text=[We clarify that the first subtype of a scalar
2376 formal type has a nonstatic, non-null constraint.]}
2377 @end{DiffWord95}
2378
2379
2380 @LabeledSubClause{Statically Matching Constraints and Subtypes}
2381
2382 @begin{StaticSem}
2383 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00311-01]}
2384 @Defn2{Term=[statically matching], Sec=(for constraints)}
2385 A constraint @i(statically matches) another
2386 constraint if@Chg{Version=[2],New=[:],Old=[ both are null constraints, both are
2387 static and have equal corresponding bounds or discriminant values,
2388 or both are nonstatic and result from the same elaboration of
2389 a @nt<constraint>
2390 of a @nt<subtype_@!indication> or the same evaluation of a @nt<range>
2391 of a @nt<discrete_@!subtype_@!definition>.]}
2392 @begin{Itemize}
2393 @ChgRef{Version=[2],Kind=[Added]}
2394 @Chg{Version=[2],New=[both are null constraints;],Old=[]}
2395
2396 @ChgRef{Version=[2],Kind=[Added]}
2397 @Chg{Version=[2],New=[both are static and have equal corresponding bounds or discriminant
2398 values;],Old=[]}
2399
2400 @ChgRef{Version=[2],Kind=[Added]}
2401 @Chg{Version=[2],New=[both are nonstatic and result from the same elaboration
2402 of a @nt<constraint>
2403 of a @nt<subtype_@!indication> or the same evaluation of a @nt<range>
2404 of a @nt<discrete_@!subtype_@!definition>; or],Old=[]}
2405
2406 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00311-01]}
2407 @Chg{Version=[2],New=[both are nonstatic and come from the same
2408 @nt{formal_type_declaration}.],Old=[]}
2409 @end{Itemize}
2410
2411 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01],ARef=[AI95-00254-01]}
2412 @Defn2{Term=[statically matching], Sec=(for subtypes)}
2413 A subtype @i(statically matches) another subtype of the same type
2414 if they have statically matching constraints@Chg{Version=[2],New=[, and, for
2415 access subtypes, either both or neither exclude null],Old=[]}.
2416 Two anonymous access@Chg{Version=[2],New=[-to-object],Old=[]} subtypes
2417 statically match if their designated subtypes statically
2418 match@Chg{Version=[2],New=[, and either both or neither
2419 exclude null, and either both or neither are access-to-constant. Two anonymous
2420 access-to-subprogram subtypes statically match if their designated profiles are
2421 subtype conformant, and either both or neither exclude null],Old=[]}.
2422 @begin{Ramification}
2423 Statically matching constraints and subtypes are the basis
2424 for subtype conformance of profiles (see @RefSecNum(Conformance Rules)).
2425 @end{Ramification}
2426 @begin{Reason}
2427 @ChgRef{Version=[2],Kind=[AddedNormal]}
2428 @ChgAdded{Version=[2],Text=[Even though anonymous access types always
2429 represent different types, they can statically match. That's important so
2430 that they can be used widely. For instance, if this wasn't true, access
2431 parameters and access discriminants could never conform, so they couldn't
2432 be used in separate specifications.]}
2433 @end{Reason}
2434
2435 @Defn2{Term=[statically matching], Sec=(for ranges)}
2436 Two ranges of the same type @i{statically match} if both result
2437 from the same evaluation of a @nt{range},
2438 or if both are static and have equal corresponding bounds.
2439 @begin{Ramification}
2440 The notion of static matching of ranges is used in
2441 @RefSec{Formal Array Types};
2442 the index ranges of formal and actual constrained array subtypes have to
2443 statically match.
2444 @end{Ramification}
2445
2446 @Defn2{Term=[statically compatible],
2447 Sec=(for a constraint and a scalar subtype)}
2448 A constraint is @i(statically compatible) with a scalar subtype if
2449 it statically matches the constraint of the subtype, or if both
2450 are static and the constraint is compatible with the subtype.
2451 @Defn2{Term=[statically compatible],
2452 Sec=(for a constraint and an access or composite subtype)}
2453 A constraint is @i(statically compatible) with an access or composite subtype
2454 if it statically matches the constraint of the subtype, or
2455 if the subtype is unconstrained.
2456 @Defn2{Term=[statically compatible],
2457 Sec=(for two subtypes)}
2458 One subtype is @i(statically compatible) with a second subtype if
2459 the constraint of the first is statically compatible with the
2460 second subtype.
2461 @begin{Discussion}
2462 Static compatibility is required when constraining a parent subtype
2463 with a discriminant from a new @nt<discriminant_part>.
2464 See @RefSecNum{Discriminants}. Static compatibility is also used
2465 in matching generic formal derived types.
2466
2467 Note that
2468 statically compatible with a subtype does not imply
2469 compatible with a type. It is OK since the terms are
2470 used in different contexts.
2471 @end{Discussion}
2472 @end{StaticSem}
2473
2474 @begin{DiffWord83}
2475 This subclause is new to Ada 95.
2476 @end{DiffWord83}
2477
2478 @begin{DiffWord95}
2479 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01],ARef=[AI95-00254-01]}
2480 @Chg{Version=[2],New=[Added static matching rules for null exclusions and
2481 anonymous access-to-subprogram types; both of these are new in
2482 Ada 2005.],Old=[]}
2483
2484 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00311-01]}
2485 @Chg{Version=[2],New=[We clarify that the constraint of the first subtype
2486 of a scalar formal type statically matches itself.],Old=[]}
2487 @end{DiffWord95}