Debian Codegraph @

source_2012/04b.mss

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