Debian Codegraph @

source_2020/03b.mss

raw
1 @Part(03, Root="ada.mss")
2
3 @Comment{$Date: 2019/04/09 04:56:50 $}
4
5 @Comment{$Source: e:\\cvsroot/ARM/Source/03b.mss,v $}
6 @Comment{$Revision: 1.108 $}
7
8 @LabeledClause{Array Types}
9
10 @begin{Intro}
11 @Defn{array}
12 @Defn{array type}
13 An @i(array) object is a composite object consisting of components
14 which all have the same subtype.
15 The name for a component of an array uses one or more
16 index values belonging to specified discrete types.
17 The value of an array object is a composite
18 value consisting of the values of the components.
19 @end{Intro}
20
21 @begin{Syntax}
22 @Syn{lhs=<array_type_definition>,rhs="
23 @Syn2{unconstrained_array_definition} | @Syn2{constrained_array_definition}"}
24
25
26 @Syn{lhs=<unconstrained_array_definition>,rhs="
27 @key{array}(@Syn2{index_subtype_definition} {, @Syn2{index_subtype_definition}}) @key{of} @Syn2{component_definition}"}
28
29 @Syn{lhs=<index_subtype_definition>,rhs="@Syn2{subtype_mark} @key{range} <>"}
30
31 @Syn{lhs=<constrained_array_definition>,rhs="
32 @key{array} (@Syn2{discrete_subtype_definition} {, @Syn2{discrete_subtype_definition}}) @key{of} @Syn2{component_definition}"}
33
34 @Syn{lhs=<discrete_subtype_definition>,rhs="@SynI{discrete_}@Syn2{subtype_indication} | @Syn2{range}"}
35
36 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01],ARef=[AI95-00406-01]}
37 @Syn{lhs=<component_definition>,rhs="@Chg{Version=[2],New=<
38 >,Old=<>}[@key{aliased}] @Syn2{subtype_indication}@Chg{Version=[2],New=<
39 | [@key{aliased}] @Syn2{access_definition}>,Old=<>}"}
40 @end{Syntax}
41
42 @begin{Resolution}
43 @PDefn2{Term=[expected type], Sec=(discrete_subtype_definition range)}
44 For a @nt<discrete_subtype_definition> that is a @nt<range>, the
45 @nt<range> shall resolve to be of some specific discrete
46 type@Redundant[; which
47 discrete type shall be determined without using any context other than
48 the bounds of the @nt<range> itself (plus the preference
49 for @i(root_integer) @em
50 see @RefSecNum(The Context of Overload Resolution)).]
51 @end{Resolution}
52
53 @begin{Legality}
54 @Defn{index subtype}
55 Each @nt{index_subtype_definition}
56 or @nt{discrete_subtype_definition} in an @nt{array_type_definition}
57 defines an @i(index subtype);
58 @Defn{index type}
59 its type (the @i(index type)) shall be discrete.
60 @begin{Discussion}
61 @Defn2{Term=[index], Sec=(of an array)}
62 An @i(index) is a discrete quantity used to select along a given
63 dimension of an array. A component is selected by specifying corresponding
64 values for each of the indices.
65 @end{Discussion}
66
67 @Defn{component subtype}
68 The subtype defined by the @nt<subtype_indication> of a
69 @nt<component_definition> (the @i(component subtype)) shall be
70 a definite subtype.
71 @begin{Ramification}
72
73 This applies to all uses of @nt<component_definition>,
74 including in @nt<record_type_definition>s and @nt<protected_definition>s.@end{ramification}
75
76 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00363-01]}
77 @ChgDeleted{Version=[2],Text=[Within the definition of a
78 nonlimited composite type
79 (or a limited composite type that later in its immediate
80 scope becomes nonlimited @em see @RefSecNum{Private Operations}
81 and @RefSecNum{Limited Types}),
82 if a @nt{component_definition} contains the reserved word
83 @key[aliased] and the type of the component is discriminated,
84 then the nominal subtype of the component shall be constrained.]}
85 @begin{Reason}
86 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
87 @ChgNote{The rule's gone, we might as well clobber all the notes.}
88 @ChgDeleted{Version=[2],Text=[If we allowed the subtype to be unconstrained,
89 then the discriminants might change because of
90 an assignment to the containing (nonlimited) object,
91 thus causing a potential violation of an access subtype constraint
92 of an access value designating the aliased component.]}
93
94 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
95 @ChgDeleted{Version=[2],Text=[Note that the rule elsewhere defining all aliased
96 discriminated objects to be constrained does not help @em that rule prevents
97 assignments to the component itself from doing any harm, but not assignments to
98 the containing object.]}
99
100 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
101 @ChgDeleted{Version=[2],Type=[Leading],Text=[We allow this
102 for components within limited types since assignment to
103 the enclosing object is not a problem. Furthermore, it is
104 important to be able to use a default expression for a discriminant
105 in arrays of limited components, since that is the only way
106 to give the components different values for their discriminants.
107 For example:]}
108 @begin{Example}
109 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
110 @ChgDeleted{Version=[2],Text=[@key[protected] @key[type] Counter_Type(Initial_Value : Integer := 1) @key[is]
111 @key[procedure] Get_Next(Next_Value : @key[out] Integer);
112 --@RI{ Returns the next value on each call, bumping Count}
113 --@RI{ before returning.}
114 @key[private]
115 Count : Integer := Initial_Value;
116 @key[end] Counter_Type;
117 @key[protected] @key[body] Counter_Type @key[is] ...]}
118
119 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
120 @ChgDeleted{Version=[2],Text=[@key[function] Next_Id(Counter : @key[access] Counter_Type) @key[return] Integer @key[is]
121 Result : Integer;
122 @key[begin]
123 Counter.Get_Next(Result);
124 @key[return] Result;
125 @key[end] Next_Id;]}
126
127 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
128 @ChgDeleted{Version=[2],Text=[C : @key[aliased] Counter_Type;
129 @key[task] @key[type] T(Who_Am_I : Integer := Next_Id(C'Access));
130 @key[task] @key[body] T @key[is] ...]}
131
132 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
133 @ChgDeleted{Version=[2],Text=[Task_Array : @key[array](1..100) @key[of] @key[aliased] T;
134 --@RI{ Array of task elements, each with its own unique ID.}
135 --@RI{ We specify "aliased" so we can use Task_Array(I)'Access.}
136 --@RI{ This is safe because Task_Array is of a limited type,}
137 --@RI{ so there is no way an assignment to it could change}
138 --@RI{ the discriminants of one of its components.}]}
139 @end{Example}
140 @end{Reason}
141 @begin{Ramification}
142 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
143 @ChgDeleted{Version=[2],Text=[Note that this rule applies to array components
144 and record components, but not to protected type components (since
145 they are always limited).]}
146 @end{Ramification}
147 @end{Legality}
148
149 @begin{StaticSem}
150 @Defn2{Term=[dimensionality], Sec=(of an array)}
151 @Defn{one-dimensional array}
152 @Defn{multi-dimensional array}
153 An array is characterized by the number of indices
154 (the @i(dimensionality) of the array), the type and position
155 of each index, the lower and upper bounds for each index, and
156 the subtype of the components.
157 The order of the indices is significant.
158
159 A one-dimensional array has a distinct component for each possible index
160 value. A multidimensional array has a distinct component for each possible
161 sequence of index values that can be formed by selecting one value for
162 each index position (in the given order).
163 The possible values for a given index are all the values between the
164 lower and upper bounds, inclusive;
165 @Defn{index range}
166 this range of values is called the @i(index range).
167 @Defn2{Term=[bounds], Sec=(of an array)}
168 The @i(bounds) of an array are the bounds of its index ranges.
169 @Defn2{Term=[length], Sec=(of a dimension of an array)}
170 The @i(length) of a dimension of an array is
171 the number of values of the index range of the dimension (zero for
172 a null range).
173 @Defn2{Term=[length], Sec=(of a one-dimensional array)}
174 The @i(length) of a one-dimensional array is
175 the length of its only dimension.
176
177 An @nt<array_type_definition> defines an array type and its first subtype.
178 For each object of this array type, the number of indices, the type
179 and position of each index, and the subtype of the components
180 are as in the type definition@Redundant[; the values of the lower
181 and upper bounds for each index belong to
182 the corresponding index subtype of its type, except for null arrays
183 (see @RefSecNum(Index Constraints and Discrete Ranges))].
184
185 @Defn2{Term=[constrained], Sec=(subtype)}
186 @Defn2{Term=[unconstrained], Sec=(subtype)}
187 An @nt{unconstrained_array_definition} defines an array type with
188 an unconstrained first subtype.
189 Each @nt{index_@!subtype_@!definition} defines the
190 corresponding index subtype to be the subtype denoted
191 by the @nt{subtype_@!mark}.
192 @redundant[@PDefn2{Term=[box], Sec=(compound delimiter)}
193 The compound delimiter <> (called a @i(box)) of an
194 @nt<index_subtype_definition> stands for an undefined range
195 (different objects of the type need not have the same bounds).]
196
197 @Defn2{Term=[constrained], Sec=(subtype)}
198 @Defn2{Term=[unconstrained], Sec=(subtype)}
199 A @nt{constrained_array_definition} defines an array type with
200 a constrained first subtype.
201 Each @nt{discrete_@!subtype_@!definition}
202 defines the corresponding index subtype,
203 as well as the corresponding index range for the
204 constrained first subtype.
205 @PDefn2{Term=[constraint], Sec=(of a first array subtype)}
206 The @i(constraint) of the first subtype consists of the bounds
207 of the index ranges.
208 @begin{Discussion}
209 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
210 Although there is no @Chg{Version=[3],New=[nameable],Old=[namable]}
211 unconstrained array subtype in this case, the predefined slicing
212 and concatenation operations can operate on and yield
213 values that do not necessarily belong to the first array subtype.
214 This is also true for Ada 83.
215 @end{Discussion}
216
217 @Leading@;The discrete subtype defined by a @nt{discrete_@!subtype_@!definition} is
218 either that defined by the @nt{subtype_@!indication}, or a subtype
219 determined by the @nt{range} as follows:
220 @begin(itemize)
221 If the type of the @nt{range} resolves to @i(root_integer), then
222 the @nt{discrete_subtype_definition} defines a subtype of the
223 predefined type Integer with bounds given by a conversion to Integer
224 of the bounds of the @nt<range>;
225 @PDefn2{Term=[implicit subtype conversion],Sec=(bounds of a range)}
226 @begin{Reason}
227 This ensures that
228 indexing over the discrete subtype can be performed with
229 regular Integers, rather than only @i(universal_integer)s.
230 @end{Reason}
231 @begin{Discussion}
232 We considered doing this by simply creating
233 a @lquotes@;preference@rquotes@; for Integer when resolving the @nt<range>.
234 @PDefn{Beaujolais effect}
235 However, this can introduce @i(Beaujolais) effects when the
236 @nt<simple_expression>s involve calls on functions visible
237 due to @key(use) clauses.
238 @end{Discussion}
239
240 Otherwise, the @nt{discrete_subtype_definition} defines
241 a subtype of the type of the @nt{range}, with the bounds given
242 by the @nt<range>.
243 @end(itemize)
244
245 @PDefn2{Term=[nominal subtype], Sec=(of a component)}
246 The @nt{component_definition} of an @nt<array_type_definition>
247 defines the nominal subtype of the components.
248 If the reserved word @key(aliased) appears in the @nt{component_definition},
249 then each component of the array is aliased
250 (see @RefSecNum{Access Types}).
251 @begin(Ramification)
252 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00363-01]}
253 @ChgDeleted{Version=[2],Text=[In this case, the nominal subtype cannot be an
254 unconstrained discriminated subtype. See @RefSecNum{Record Types}.]}
255 @end(Ramification)
256
257 @end{StaticSem}
258
259 @begin{RunTime}
260 @PDefn2{Term=[elaboration], Sec=(array_type_definition)}
261 The elaboration of an @nt{array_type_definition}
262 creates the array type and its first subtype,
263 and consists of the elaboration of any @nt{discrete_@!subtype_@!definition}s
264 and the @nt{component_@!definition}.
265
266 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0002],ARef=[AI95-00171-01]}
267 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
268 @PDefn2{Term=[elaboration], Sec=(discrete_subtype_definition)}
269 The elaboration of a @nt{discrete_subtype_definition}
270 @Chg{New=[that does not contain any per-object expressions],Old=[]}
271 creates the discrete subtype, and consists
272 of the elaboration of the @nt{subtype_@!indication} or the
273 evaluation of the @nt{range}.
274 @Chg{New=[The elaboration of a @nt{discrete_subtype_definition} that contains
275 one or more per-object expressions is defined in @RefSecNum{Record Types}.],Old=[]}
276 @PDefn2{Term=[elaboration], Sec=(component_definition)}
277 The elaboration of a @nt{component_@!definition} in an
278 @nt{array_@!type_@!definition} consists of the elaboration
279 of the @nt{subtype_@!indication}@Chg{Version=[2],New=[ or @nt{access_definition}],Old=[]}.
280 The elaboration of any @nt{discrete_@!subtype_@!definition}s
281 and the elaboration of the
282 @nt{component_@!definition} are performed in an arbitrary order.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
283 @end{RunTime}
284
285 @begin{StaticSem}
286 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0228-1]}
287 @ChgAdded{Version=[3],Type=[Leading],Text=[For an array type with a
288 scalar component type, the following language-defined representation aspect
289 may be specified with an @nt{aspect_specification} (see
290 @RefSecNum{Aspect Specifications}):]}
291 @begin{Description}
292 @ChgRef{Version=[3],Kind=[Added]}
293 @ChgAdded{Version=[3],Text=[Default_Component_Value@\This aspect
294 shall be specified by a static expression, and that
295 expression shall be explicit, even if the aspect has a boolean type.
296 Default_Component_Value shall be specified only on
297 a @nt{full_type_declaration}.@AspectDefn{Default_Component_Value}]}
298 @begin{Reason}
299 @ChgRef{Version=[3],Kind=[AddedNormal]}
300 @ChgAdded{Version=[3],Text=[The part about requiring an explicit expression is
301 to disallow omitting the value for this aspect, which would otherwise be
302 allowed by the rules of @RefSecNum{Aspect Specifications}.]}
303
304 @ChgRef{Version=[3],Kind=[AddedNormal]}
305 @ChgAdded{Version=[3],Text=[This is a representation attribute in order to
306 disallow specifying it on a derived type that has inherited primitive
307 subprograms; that is necessary as the sizes of @key[out] parameters could be
308 different whether or not a Default_Value is specified (see
309 @RefSecNum{Parameter Associations}).]}
310 @end{Reason}
311
312 @ChgAspectDesc{Version=[3],Kind=[AddedNormal],Aspect=[Default_Component_Value],
313 Text=[@ChgAdded{Version=[3],Text=[Default value for the components of an
314 array-of-scalar subtype.]}]}
315 @end{Description}
316
317 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0228-1]}
318 @ChgAdded{Version=[3],Text=[If a derived type with no primitive subprograms
319 inherits a boolean Default_Component_Value aspect, the aspect may be specified to have any
320 value for the derived type.]}
321 @begin{Reason}
322 @ChgRef{Version=[3],Kind=[AddedNormal]}
323 @ChgAdded{Version=[3],Text=[This overrides the
324 @RefSecNum{Aspect Specifications} rule that says that a boolean aspect
325 with a value True cannot be changed.]}
326 @end{Reason}
327 @end{StaticSem}
328
329 @begin{Resolution}
330 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0228-1]}
331 @ChgAdded{Version=[3],Text=[The expected type for the @nt{expression}
332 specified for the Default_Component_Value aspect is the component type of the
333 array type defined by the
334 @nt{full_type_declaration} on which it
335 appears.@PDefn2{Term=[expected type],Sec=[@nt{expression} of a Default_Component_Value aspect]}]}
336 @end{Resolution}
337
338 @begin{Notes}
339 All components of an array have the same subtype. In particular, for an array
340 of components that are one-dimensional arrays, this means that all components
341 have the same bounds and hence the same length.
342
343 Each elaboration of an @nt<array_type_definition> creates
344 a distinct array type. A consequence of this is that each
345 object whose @nt<object_declaration> contains an @nt<array_type_definition>
346 is of its own unique type.
347 @end{Notes}
348
349 @begin{Examples}
350 @Leading@keepnext@i(Examples of type declarations with unconstrained array definitions: )
351 @begin(Example)
352 @key(type) Vector @key(is) @key(array)(Integer @key(range) <>) @key(of) Real;
353 @key(type) Matrix @key(is) @key(array)(Integer @key(range) <>, Integer @key(range) <>) @key(of) Real;
354 @key(type) Bit_Vector @key(is) @key(array)(Integer @key(range) <>) @key(of) Boolean;
355 @key(type) Roman @key(is) @key(array)(Positive @key(range) <>) @key(of) Roman_Digit; --@RI[ see @RefSecNum(Character Types)]
356 @end(Example)
357
358 @begin{WideAbove}
359 @leading@keepnext@i(Examples of type declarations with constrained array definitions: )
360 @end{WideAbove}
361 @begin(Example)
362 @key(type) Table @key(is) @key(array)(1 .. 10) @key(of) Integer;
363 @key(type) Schedule @key(is) @key(array)(Day) @key(of) Boolean;
364 @key(type) Line @key(is) @key(array)(1 .. Max_Line_Size) @key(of) Character;
365 @end(Example)
366
367 @begin{WideAbove}
368 @leading@keepnext@i(Examples of object declarations with array type definitions: )
369 @end{WideAbove}
370 @begin(Example)
371 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00433-01]}
372 Grid @Chg{Version=[2],New=[ ],Old=[]}: @key(array)(1 .. 80, 1 .. 100) @key(of) Boolean;
373 Mix @Chg{Version=[2],New=[ ],Old=[]}: @key(array)(Color @key(range) Red .. Green) @key(of) Boolean;@Chg{Version=[2],New=[
374 Msg_Table : @key(constant array)(Error_Code) @key(of access constant) String :=
375 (Too_Big => @key(new) String'("Result too big"), Too_Small => ...);],Old=[]}
376 Page @Chg{Version=[2],New=[ ],Old=[]}: @key(array)(Positive @key(range) <>) @key(of) Line := --@RI[ an array of arrays]
377 (1 | 50 => Line'(1 | Line'Last => '+', @key(others) => '-'), --@RI[ see @RefSecNum(Array Aggregates)]
378 2 .. 49 => Line'(1 | Line'Last => '|', @key(others) => ' '));
379 --@RI[ Page is constrained by its initial value to (1..50)]
380 @end(Example)
381 @end{Examples}
382
383 @begin{Extend83}
384 @Defn{extensions to Ada 83}
385 The syntax rule for @nt{component_definition} is modified to allow
386 the reserved word @key{aliased}.
387
388 The syntax rules for @nt{unconstrained_array_definition} and
389 @nt{constrained_array_definition} are modified to use
390 @nt{component_definition} (instead of
391 @i(component_)@nt{subtype_indication}). The effect of this change is to allow
392 the reserved word @key{aliased} before the
393 component @nt{subtype_indication}.
394
395 A @nt{range} in a @nt{discrete_subtype_definition}
396 may use arbitrary
397 universal expressions for each bound (e.g. @en@;1 .. 3+5), rather
398 than strictly "implicitly convertible" operands. The subtype
399 defined will still be a subtype of Integer.
400 @end{Extend83}
401
402 @begin{DiffWord83}
403 We introduce a new syntactic category, @nt{discrete_subtype_definition},
404 as distinct from @nt{discrete_range}. These two constructs have
405 the same syntax, but their semantics
406 are quite different (one defines a subtype, with
407 a preference for Integer subtypes, while the
408 other just selects a subrange of an existing subtype).
409 We use this new syntactic category in @key(for) loops
410 and entry families.
411
412 The syntax for @nt{index_constraint} and @nt{discrete_range}
413 have been moved to their own subclause, since they are no
414 longer used here.
415
416 The syntax rule for @nt{component_definition} (formerly
417 @ntf<component_subtype_definition>) is moved here from
418 RM83-3.7.
419 @end{DiffWord83}
420
421 @begin{Extend95}
422 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00230-01],ARef=[AI95-00406-01]}
423 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
424 Array components can have an anonymous access type.]}
425
426 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
427 @ChgAdded{Version=[2],Text=[The prohibition against unconstrained
428 discriminated aliased components has been lifted. It has been replaced
429 by a prohibition against the actual troublemakers: general access
430 discriminant constraints (see @RefSecNum{Discriminant Constraints}).]}
431 @end{Extend95}
432
433 @begin{DiffWord95}
434 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0002],ARef=[AI95-00171-01]}
435 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Added wording to allow
436 the elaboration of per-object constraints for constrained arrays.]}
437 @end{DiffWord95}
438
439 @begin{Extend2005}
440 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0228-1]}
441 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
442 The new aspect Default_Component_Value allows defining implicit initial
443 values (see @RefSecNum{Object Declarations}) for arrays of scalar types.]}
444 @end{Extend2005}
445
446
447 @LabeledSubClause{Index Constraints and Discrete Ranges}
448
449 @begin{Intro}
450 An @nt<index_constraint> determines the range of possible values for every
451 index of an array subtype, and thereby the corresponding array bounds.
452 @end{Intro}
453
454 @begin{Syntax}
455 @Syn{lhs=<index_constraint>,rhs=" (@Syn2{discrete_range} {, @Syn2{discrete_range}})"}
456
457 @Syn{lhs=<discrete_range>,rhs="@SynI{discrete_}@Syn2{subtype_indication} | @Syn2{range}"}
458 @end{Syntax}
459
460 @begin{Resolution}
461 @Defn{type of a @nt{discrete_range}}
462 The type of a @nt<discrete_range> is the
463 type of the subtype defined by the @nt<subtype_indication>,
464 or the type of the @nt<range>.
465 @PDefn2{Term=[expected type], Sec=(index_constraint discrete_range)}
466 For an @nt{index_constraint},
467 each @nt{discrete_range} shall resolve to be of the type of the
468 corresponding index.
469 @begin(Discussion)
470 In Ada 95, @nt{index_constraint}s only appear in
471 a @nt{subtype_indication}; they no longer appear in
472 @nt<constrained_array_definition>s.
473 @end(Discussion)
474
475 @end{Resolution}
476
477 @begin{Legality}
478 An @nt{index_constraint} shall appear only in a
479 @nt{subtype_indication} whose @nt{subtype_mark} denotes
480 either an unconstrained array subtype, or an
481 unconstrained access subtype whose designated subtype is
482 an unconstrained array subtype; in either case,
483 the @nt{index_constraint} shall provide a @nt{discrete_range}
484 for each index of the array type.
485 @end{Legality}
486
487 @begin{StaticSem}
488 @Defn2{Term=[bounds], Sec=(of a @nt<discrete_range>)}
489 A @nt{discrete_range} defines a range whose bounds are given
490 by the @nt{range}, or by the range of the subtype defined by the
491 @nt{subtype_indication}.
492 @end{StaticSem}
493
494 @begin{RunTime}
495 @PDefn2{Term=[compatibility], Sec=(index constraint with a subtype)}
496 An @nt{index_constraint} is @i(compatible) with an unconstrained array subtype
497 if and only if the index range defined by each @nt{discrete_range}
498 is compatible (see @RefSecNum(Scalar Types)) with the corresponding index subtype.
499 @Defn{null array}
500 If any of the @nt<discrete_range>s defines a null range, any array thus
501 constrained is a @i(null array), having no components.
502 @PDefn2{Term=[satisfies], Sec=(an index constraint)}
503 An array value @i(satisfies) an @nt{index_constraint} if at each index position
504 the array value and the @nt{index_constraint} have the same index
505 bounds.
506 @begin{Ramification}
507
508 There is no need to define compatibility with a constrained
509 array subtype, because one is not allowed to constrain it again.@end{ramification}
510
511 @PDefn2{Term=[elaboration], Sec=(index_constraint)}
512 The elaboration of an @nt{index_constraint} consists
513 of the evaluation of the @nt{discrete_range}(s), in an arbitrary order.
514 @PDefn2{Term=[evaluation], Sec=(discrete_range)}
515 The evaluation of a @nt{discrete_range}
516 consists of the elaboration of the @nt{subtype_indication}
517 or the evaluation of the @nt{range}.
518 @end{RunTime}
519
520 @begin{Notes}
521 The elaboration of a @nt<subtype_indication> consisting
522 of a @nt<subtype_mark> followed
523 by an @nt<index_constraint> checks the compatibility of the
524 @nt<index_constraint> with the @nt<subtype_mark>
525 (see @RefSecNum(Subtype Declarations)).
526
527 Even if an array value does not satisfy the index constraint
528 of an array subtype, Constraint_Error is not
529 raised on conversion to the array subtype, so long as
530 the length of each dimension of the array value and the
531 array subtype match. See @RefSecNum(Type Conversions).
532 @end{Notes}
533
534 @begin{Examples}
535 @leading@keepnext@i(Examples of array declarations including an index constraint: )
536 @begin(Example)
537 Board : Matrix(1 .. 8, 1 .. 8); --@RI[ see @RefSecNum(Array Types)]
538 Rectangle : Matrix(1 .. 20, 1 .. 30);
539 Inverse : Matrix(1 .. N, 1 .. N); --@RI[ N need not be static ]
540
541 Filter : Bit_Vector(0 .. 31);
542 @end(Example)
543
544 @begin{WideAbove}
545 @leading@keepnext@i(Example of array declaration with a constrained array subtype: )
546 @end{WideAbove}
547 @begin(Example)
548 My_Schedule : Schedule; --@RI[ all arrays of type Schedule have the same bounds]
549 @end(Example)
550
551 @begin{WideAbove}
552 @leading@keepnext@i(Example of record type with a component that is an array: )
553 @end{WideAbove}
554 @begin(Example)
555 @key(type) Var_Line(Length : Natural) @key(is)
556 @key(record)
557 Image : String(1 .. Length);
558 @key(end) @key(record);
559
560 Null_Line : Var_Line(0); --@RI[ Null_Line.Image is a null array]
561 @end(Example)
562 @end{Examples}
563
564 @begin{Extend83}
565 @Defn{extensions to Ada 83}
566 We allow the declaration of a variable with
567 a nominally unconstrained array subtype, so long as
568 it has an initialization expression to determine
569 its bounds.
570 @end{Extend83}
571
572 @begin{DiffWord83}
573 We have moved the syntax for @nt{index_constraint} and
574 @nt{discrete_range} here since they are no longer
575 used in @nt{constrained_array_definition}s.
576 We therefore also no longer have to describe the
577 (special) semantics of @nt{index_constraint}s and @nt{discrete_range}s
578 that appear in @nt{constrained_array_definition}s.
579
580 The rules given in RM83-3.6.1(5,7-10),
581 which define the bounds of an array object, are redundant
582 with rules given elsewhere, and so are not repeated here.
583 RM83-3.6.1(6), which requires that the (nominal) subtype of
584 an array variable be constrained, no longer applies,
585 so long as the variable is explicitly initialized.
586 @end{DiffWord83}
587
588 @LabeledSubClause{Operations of Array Types}
589
590 @begin{Legality}
591 @Redundant[The argument N used in the @nt<attribute_designator>s for
592 the N-th dimension of an array shall be a static @nt<expression> of
593 some integer type.] The value of N shall be positive (nonzero)
594 and no greater than the dimensionality of the array.
595 @end{Legality}
596
597 @begin{StaticSem}
598 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0006],ARef=[AI95-00030-01]}
599 The following attributes are defined for
600 @ChgPrefixType{Version=[1],Kind=[Revised],Text=[a @Chg{New=[@nt{prefix}],
601 Old=[prefix]} A that is of an array type
602 @Redundant[(after any implicit dereference)], or denotes
603 a constrained array subtype]}:
604 @begin{Ramification}
605 These attributes are not defined if A is a subtype-mark
606 for an access-to-array subtype. They are defined (by implicit
607 dereference) for access-to-array values.@end{ramification}
608 @begin(description)
609 @Attribute{Prefix=<A>, AttrName=<First>,
610 Text=[A'First denotes the lower bound of the first index range; its
611 type is the corresponding index type.]}
612
613 @Attribute{Prefix=<A>, AttrName=<First(N)>,
614 Text=[A'First(N) denotes the lower bound of the N-th index range; its
615 type is the corresponding index type.]}
616
617 @Attribute{Prefix=<A>, AttrName=<Last>,
618 Text=[A'Last denotes the upper bound of the first index range; its
619 type is the corresponding index type.]}
620
621 @Attribute{Prefix=<A>, AttrName=<Last(N)>,
622 Text=[A'Last(N) denotes the upper bound of the N-th index range; its
623 type is the corresponding index type.]}
624
625 @Attribute{Prefix=<A>, AttrName=<Range>,
626 Text=[A'Range is equivalent to the range A'First .. A'Last,
627 except that the @nt<prefix> A is only evaluated once.]}
628
629 @Attribute{Prefix=<A>, AttrName=<Range(N)>,
630 Text=[A'Range(N) is equivalent to the range A'First(N) .. A'Last(N),
631 except that the @nt<prefix> A is only evaluated once.]}
632
633 @Attribute{Prefix=<A>, AttrName=<Length>,
634 Text=[A'Length denotes the number of values of the first index
635 range (zero for a null range); its type is @i(universal_integer).]}
636
637 @Attribute{Prefix=<A>, AttrName=<Length(N)>,
638 Text=[A'Length(N) denotes the number of values of the N-th index
639 range (zero for a null range); its type is @i(universal_integer).]}
640 @end(description)
641 @EndPrefixType{}
642 @end{StaticSem}
643
644 @begin{ImplAdvice}
645 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
646 An implementation should normally represent
647 multidimensional arrays in row-major order, consistent with the notation used
648 for multidimensional array aggregates (see @RefSecNum(Array Aggregates)).
649 However, if @Chg{Version=[3],New=[convention ],Old=[a @key<pragma>
650 Convention(]}Fortran@Chg{Version=[3],New=[ is specified
651 for],Old=[, ...) applies to]} a
652 multidimensional array type, then column-major order should be used
653 instead (see @RefSec{Interfacing with Fortran}).
654 @ChgImplAdvice{Version=[2],Kind=[AddedNormal],Text=[@ChgAdded{Version=[2],
655 Text=[Multidimensional arrays should be represented in row-major order,
656 unless the array has convention Fortran.]}]}
657 @end{ImplAdvice}
658
659 @begin{Notes}
660 @Leading@;The @nt<attribute_reference>s A'First and A'First(1) denote the same value.
661 A similar relation exists for the @nt<attribute_reference>s A'Last,
662 A'Range, and A'Length. The following relation is satisfied (except
663 for a null array) by the above attributes if the index type is an
664 integer type:
665 @begin(example)
666 A'Length(N) = A'Last(N) - A'First(N) + 1
667 @end(example)
668
669 An array type is limited if its component type is limited
670 (see @RefSecNum(Limited Types)).
671
672 @PDefn2{Term=[predefined operations],Sec=(of an array type)}
673 The predefined operations of an array type include the
674 membership tests, qualification, and explicit conversion.
675 If the array type is not limited, they also include assignment
676 and the predefined
677 equality operators. For a one-dimensional array type,
678 they include the predefined concatenation operators (if nonlimited) and,
679 if the component type is discrete, the predefined relational operators;
680 if the component type is boolean, the predefined
681 logical operators are also included.
682
683 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
684 A component of an array can be named with an @nt<indexed_component>.
685 A value of an array type can be specified with an
686 @nt<array_aggregate>@Chg{Version=[2],New=[],Old=[, unless the array type
687 is limited]}.
688 For a one-dimensional array type, a slice of the array can be named;
689 also, string literals are defined if the component type is
690 a character type.
691 @end{Notes}
692
693 @begin{Examples}
694 @Leading@keepnext@i{Examples (using arrays declared in the examples of subclause @RefSecNum(Index Constraints and Discrete Ranges)):}
695 @begin(Example)
696 -- Filter'First = 0 Filter'Last = 31 Filter'Length = 32
697 -- Rectangle'Last(1) = 20 Rectangle'Last(2) = 30
698 @end(Example)
699
700 @end{Examples}
701
702 @LabeledSubClause{String Types}
703
704 @begin{StaticSem}
705 @Defn{string type}
706 A one-dimensional array type whose component type is a character type
707 is called a @i(string type).
708
709 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00285-01]}
710 @Leading@redundant[There are @Chg{Version=[2],New=[three],Old=[two]} predefined
711 string types, String@Chg{Version=[2],New=[,],Old=[ and]}
712 Wide_String@Chg{Version=[2],New=[, and Wide_Wide_String],Old=[]},
713 each indexed by values of the predefined subtype Positive;
714 these are declared in the visible part of package Standard:]
715 @begin(example)
716 @redundant[@key(subtype) Positive @key(is) Integer @key(range) 1 .. Integer'Last;
717
718 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00285-01]}
719 @key(type) String @key(is) @key(array)(Positive @key(range) <>) @key(of) Character;
720 @key(type) Wide_String @key(is) @key(array)(Positive @key(range) <>) @key(of) Wide_Character;
721 @Chg{Version=[2],New=[@key(type) Wide_Wide_String @key(is) @key(array)(Positive @key(range) <>) @key(of) Wide_Wide_Character;],Old=[]}
722 ]@end(example)
723 @end{StaticSem}
724
725 @begin{Notes}
726 String literals (see @RefSecNum(String Literals) and
727 @RefSecNum(Literals)) are defined for all string types.
728 The concatenation operator & is predefined
729 for string types, as for all nonlimited
730 one-dimensional array types.
731 The ordering operators <, <=, >, and >= are predefined
732 for string types, as for all one-dimensional discrete array types;
733 these ordering operators correspond to lexicographic order
734 (see @RefSecNum(Relational Operators and Membership Tests)).
735
736 @end{Notes}
737
738 @begin{Examples}
739 @Leading@keepnext@i(Examples of string objects:)
740 @begin(Example)
741 @TabClear()@TabSet(P50)
742 Stars : String(1 .. 120) := (1 .. 120 => '*' );
743 Question : @key(constant) String := "How many characters?";
744 @\--@RI[ Question'First = 1, Question'Last = 20]
745 @\--@RI[ Question'Length = 20 (the number of characters)]
746
747 Ask_Twice : String := Question & Question;@\--@RI[ constrained to (1..40)]
748 Ninety_Six : @key(constant) Roman := "XCVI";@\--@RI[ see @RefSecNum(Character Types) and @RefSecNum(Array Types)]
749 @end(Example)
750 @end{Examples}
751
752 @begin{Inconsistent83}
753 @Defn{inconsistencies with Ada 83}
754 The declaration of Wide_String in Standard hides a use-visible
755 declaration with the same @nt<defining_identifier>.
756 In rare cases, this might result in an inconsistency between
757 Ada 83 and Ada 95.
758 @end{Inconsistent83}
759
760 @begin{Incompatible83}
761 @Defn{incompatibilities with Ada 83}
762 Because both String and Wide_String are always directly visible,
763 an expression like
764 @begin(Example)
765 "a" < "bc"
766 @end(Example)
767
768 is now ambiguous, whereas in Ada 83 both string literals could
769 be resolved to type String.
770 @end{Incompatible83}
771
772 @begin{Extend83}
773 @Defn{extensions to Ada 83}
774 The type Wide_String is new (though it was approved by
775 ARG for Ada 83 compilers as well).
776 @end{Extend83}
777
778 @begin{DiffWord83}
779 We define the term @i(string type) as a natural analogy
780 to the term @i(character type).
781 @end{DiffWord83}
782
783 @begin{Inconsistent95}
784 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00285-01]}
785 @ChgAdded{Version=[2],Text=[@Defn{inconsistencies with Ada 95}
786 The declaration of Wide_Wide_String in Standard hides a use-visible
787 declaration with the same @nt<defining_identifier>.
788 In the (very) unlikely event that an Ada 95 program had
789 depended on such a use-visible declaration, and the program remains
790 legal after the substitution of Standard.Wide_Wide_String,
791 the meaning of the program will be different.]}
792 @end{Inconsistent95}
793
794 @begin{Extend95}
795 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00285-01]}
796 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
797 The type Wide_Wide_String is new.]}
798 @end{Extend95}
799
800 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
801
802 @LabeledClause{Discriminants}
803
804 @begin{Intro}
805 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01],ARef=[AI95-00326-01]}
806 @redundant[@Defn{discriminant}
807 @IndexSee{Term=[type parameter],See=(discriminant)}
808 @IndexSeeAlso{Term=[parameter],See=(discriminant)}
809 A composite type (other than an array@Chg{Version=[2],New=[ or
810 interface],Old=[]} type) can have discriminants,
811 which parameterize the type.
812 A @nt<known_discriminant_part> specifies the discriminants
813 of a composite type.
814 A discriminant of an object is a component of the object,
815 and is either of a discrete type or an access type.
816 An @nt<unknown_discriminant_part> in the declaration of
817 a@Chg{Version=[2],New=[],Old=[ partial]} view of a type
818 specifies that the discriminants of the type are unknown
819 for the given view;
820 all subtypes of such a@Chg{Version=[2],New=[],Old=[ partial]} view
821 are indefinite subtypes.]
822 @ChgRef{Version=[2],Kind=[Revised]}
823 @ChgToGlossary{Version=[2],Kind=[Revised],Term=<Discriminant>,
824 Text=<A discriminant is a parameter @Chg{Version=[2],New=[for],Old=[of]} a composite type.
825 It can control, for example, the bounds of a component
826 of the type if @Chg{Version=[2],New=[the component is],Old=[that type is]}
827 an array@Chg{Version=[2],New=[],Old=[ type]}.
828 A discriminant @Chg{Version=[2],New=[for],Old=[of]} a task type can be
829 used to pass data to a task of the type upon creation.>}
830 @begin{Discussion}
831 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
832 @PDefn{unknown discriminants}
833 @PDefn2{Term=[discriminants], Sec=(unknown)}
834 A @Chg{Version=[2],New=[view of a ],Old=[]}type, and all @Chg{Version=[2],New=[],
835 Old=[of its ]}subtypes@Chg{Version=[2],New=[ of the view],Old=[]}, have
836 @i(unknown discriminants)
837 when the number or names of the discriminants, if any, are unknown at
838 the point of the type declaration@Chg{Version=[2],New=[ for the view],Old=[]}.
839 A @nt<discriminant_part> of (<>) is used to indicate unknown discriminants.
840 @end{Discussion}
841 @end{Intro}
842
843 @begin{Metarules}
844 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00402-01]}
845 @ChgAdded{Version=[2],Text=[When an access discriminant
846 is initialized at the time of object creation with an allocator
847 of an anonymous type, the allocated object and the object
848 with the discriminant are tied together for their lifetime.
849 They should be allocated out of the same storage pool,
850 and then at the end of the lifetime of the enclosing object, finalized
851 and reclaimed together. In this case, the allocated object is called
852 a coextension (see @RefSecNum{Operations of Access Types}).]}
853 @begin{Discussion}
854 @ChgRef{Version=[2],Kind=[AddedNormal]}
855 @ChgAdded{Version=[2],Text=[The above principle when applied to a
856 nonlimited type implies that such an object may be copied only to a
857 shorter-lived object, because attempting to assign it to a longer-lived
858 object would fail because the access discriminants would not match.
859 In a copy, the lifetime connection between the enclosing object
860 and the allocated object does not exist. The allocated object is
861 tied in the above sense only to the original object. Other copies
862 have only secondary references to it.]}
863
864 @ChgRef{Version=[2],Kind=[AddedNormal]}
865 @ChgAdded{Version=[2],Text=[Note that when an @nt{allocator} appears as a
866 constraint on an access discriminant in a @nt{subtype_indication}
867 that is elaborated independently from object creation,
868 no such connection exists. For example, if a named constrained
869 subtype is declared via
870 "@key{subtype} Constr @key{is} Rec(Acc_Discrim => @key{new} T);"
871 or if such an @nt{allocator} appears in the @nt{subtype_indication} for
872 a component, the allocator is evaluated when the @nt{subtype_indication}
873 is elaborated, and hence its lifetime is typically longer than
874 the objects or components that will later be subject to the
875 constraint. In these cases, the allocated object should not
876 be reclaimed until the @nt{subtype_indication} goes out of scope.]}
877 @end{Discussion}
878 @end{Metarules}
879
880 @begin{Syntax}
881 @Syn{lhs=<discriminant_part>,rhs="@Syn2{unknown_discriminant_part} | @Syn2{known_discriminant_part}"}
882
883 @Syn{lhs=<unknown_discriminant_part>,rhs="(<>)"}
884
885 @Syn{lhs=<known_discriminant_part>,rhs="
886 (@Syn2{discriminant_specification} {; @Syn2{discriminant_specification}})"}
887
888 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
889 @Syn{lhs=<discriminant_specification>,rhs="
890 @Syn2{defining_identifier_list} : @Chg{Version=[2],New=<[@Syn2{null_exclusion}] >,Old=<>}@Syn2{subtype_mark} [:= @Syn2{default_expression}]
891 | @Syn2{defining_identifier_list} : @Syn2{access_definition} [:= @Syn2{default_expression}]"}
892
893 @Syn{lhs=<default_expression>,rhs="@Syn2{expression}"}
894 @end{Syntax}
895
896 @begin{Resolution}
897 @PDefn2{Term=[expected type], Sec=(discriminant default_expression)}
898 The expected type for the @nt{default_expression}
899 of a @nt{discriminant_specification} is that of the corresponding
900 discriminant.
901 @end{Resolution}
902
903 @begin{Legality}
904 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0007],ARef=[AI95-00098-01]}
905 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
906 A @Chg{New=[@nt{discriminant_part}],Old=[@nt{known_discriminant_part}]} is only
907 permitted in a declaration for a composite type that is not an
908 array@Chg{Version=[2],New=[ or interface],Old=[]} type
909 @Redundant[(this includes generic formal types)]@Chg{New=[. A],Old=[; a]}
910 type declared with a @nt<known_discriminant_part> is called
911 a @i(discriminated) type,@Defn{discriminated type} as is a type that inherits
912 (known) discriminants.
913 @begin{ImplNote}
914 Discriminants on array types were considered,
915 but were omitted to ease (existing) implementations.
916 @end{implnote}
917 @begin(Discussion)
918 Note that the above definition for @lquotes@;discriminated type@rquotes@; does
919 not include types declared with an @nt<unknown_discriminant_part>.
920 This seems consistent with Ada 83, where such types (in a generic
921 formal part) would not be considered discriminated types.
922 Furthermore, the full type for a type with unknown discriminants
923 need not even be composite, much less have any discriminants.
924
925 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0007],ARef=[AI95-00098-01]}
926 @Chg{New=[On the other hand, @nt<unknown_discriminant_part>s cannot be
927 applied to type declarations that cannot have a @nt<known_discriminant_part>.
928 There is no point in having unknown discriminants on a type that can never
929 have discriminants (for instance, a formal modular type), even when these
930 are allowed syntactically.],Old=[]}
931 @end(Discussion)
932
933 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01],ARef=[AI95-00254-01]}
934 The subtype of a discriminant may be defined
935 by@Chg{Version=[2],New=[ an optional @nt{null_exclusion} and],Old=[]} a
936 @nt<subtype_mark>, in which case the @nt<subtype_mark> shall denote
937 a discrete or access subtype, or it may be defined by an
938 @nt<access_definition>@Chg{Version=[2],New=[],Old=[ @Redundant[(in which case
939 the @nt<subtype_mark> of the @nt<access_definition> may denote
940 any kind of subtype)]]}.
941 @Defn{access discriminant}
942 A discriminant that is defined by an @nt<access_definition>
943 is called an @i(access discriminant)
944 and is of an anonymous @Chg{Version=[2],New=[access],
945 Old=[general access-to-variable]} type@Chg{Version=[2],New=[],Old=[ whose
946 designated subtype is
947 denoted by the @nt<subtype_mark> of the @nt<access_definition>]}.
948 @begin(Reason)
949 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00230-01]}
950 @ChgNote{It still does cause complexities. :-)}@ChgNote{We don't
951 want to delete "Reason:" here, thus we use @Chg and not @ChgDeleted}
952 @Chg{Version=[2],New=[],Old=[In an early version of Ada 9X,
953 we allowed access discriminants on nonlimited types,
954 but this created unpleasant complexities.
955 It turned out to be simpler and more uniform to allow discriminants
956 of a named access type on any discriminated type, and keep access
957 discriminants just for limited types.]}
958
959 Note that discriminants of a named access type are not
960 considered @lquotes@;access discriminants.@rquotes@;
961 Similarly, @lquotes@;access parameter@rquotes@;
962 only refers to a formal parameter defined by an @nt<access_definition>.
963 @end(Reason)
964
965 @ChgNote{This paragraph is just moved up}
966 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00402-01]}
967 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0214-1]}
968 @ChgAdded{Version=[2],Text=[@nt{Default_expression}s shall be provided either
969 for all or for none
970 of the discriminants of a @nt{known_@!discriminant_@!part}.
971 No @nt<default_@!expression>s are permitted in a
972 @nt<known_@!discriminant_@!part> in a declaration of a
973 @Chg{Version=[3],New=[nonlimited ],Old=[]}tagged
974 type @Redundant[or a generic formal type].]}
975
976 @begin(Reason)
977 @ChgRef{Version=[2],Kind=[AddedNormal]}
978 @ChgAdded{Version=[2],Text=[The all-or-none rule
979 is related to the rule that a discriminant constraint shall specify
980 values for all discriminants. One could imagine a different rule
981 that allowed a constraint to specify only some of the discriminants,
982 with the others provided by default. Having defaults for discriminants
983 has a special significance @em it allows objects of the type to
984 be unconstrained, with the discriminants alterable as part of
985 assigning to the object.]}
986
987 @ChgRef{Version=[2],Kind=[AddedNormal]}
988 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0214-1]}
989 @ChgAdded{Version=[2],Text=[Defaults for discriminants of tagged types
990 are disallowed so that every object of a
991 @Chg{Version=[3],New=[nonlimited ],Old=[]}tagged type is constrained,
992 either by an explicit constraint,
993 or by its initial discriminant values.
994 This substantially simplifies the semantic rules
995 and the implementation of inherited
996 dispatching operations. @Chg{Version=[3],New=[We don't need this rule for
997 limited tagged types, as the discriminants of such objects cannot be changed
998 after the object is created in any case @em no full-object assignment is
999 supported, and that is required to change discriminant values. ],Old=[]}For
1000 generic formal types, the restriction simplifies the type matching rules.
1001 If one simply wants a "default" value for the discriminants,
1002 a constrained subtype can be declared for future use.]}
1003 @end(Reason)
1004
1005 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01],ARef=[AI95-00402-01],ARef=[AI95-00419-01]}
1006 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0063-1]}
1007 A @nt<discriminant_specification> for
1008 an access discriminant
1009 @Chg{Version=[2],New=[may have a @nt{default_expression}],Old=[shall appear]}
1010 only in the declaration for @Chg{Version=[3],New=[an immutably limited type
1011 (see @RefSecNum{Limited Types})],Old=[a task or protected type,
1012 or for a type @Chg{Version=[2],New=[that is a descendant of an explicitly
1013 limited record type],Old=[with the reserved word @key[limited] in its
1014 @Redundant[(full)] definition
1015 or in that of one of its ancestors]}]}.
1016 In addition to the places where @LegalityTitle normally apply
1017 (see @RefSecNum{Generic Instantiation}),
1018 this rule applies also in the private part of an
1019 instance of a generic unit.@PDefn{generic contract issue}
1020 @begin{Discussion}
1021 @ChgRef{Version=[2],Kind=[Revised]}
1022 @ChgRef{Version=[3],Kind=[Revised]}
1023 This rule implies that a type can have @Chg{Version=[2],
1024 New=[a default for ],Old=[]}an access discriminant
1025 if the type is limited,
1026 but not if the only reason it's limited is because of a limited component.
1027 Compare @Chg{Version=[3],New=[],Old=[with ]}the definition of limited type
1028 @Chg{Version=[3],New=[and immutably limited type ],Old=[]}in
1029 @RefSecNum{Limited Types}.@Chg{Version=[3],New=[],Old=[@Chg{Version=[2],New=[ Also,
1030 recall that a @ldquote@;descendant@rdquote includes the type itself, so
1031 an explicitly limited record type can have defaults.],Old=[]}]}
1032 @end{Discussion}
1033 @begin{Ramification}
1034 @ChgRef{Version=[2],Kind=[Revised]}
1035 @ChgRef{Version=[3],Kind=[Revised]}
1036 @ChgDeleted{Version=[2],Text=[It is a consequence
1037 of this rule that only a return-by-reference
1038 type can have an access discriminant (see @RefSecNum{Return Statements}).
1039 This is important to avoid dangling references to local variables.]}
1040 @ChgAdded{Version=[3],Text=[A (nonformal) limited private type can always
1041 have a default for an access discriminant, because having the default itself
1042 makes the type immutably limited. Such a private type must necessarily
1043 have a full type with the same access discriminant with a default, and
1044 thus the full type will always be immutably limited (if legal).]}
1045 @end{Ramification}
1046 @begin{Reason}
1047 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
1048 @Leading@Keepnext@;We @Chg{Version=[2],New=[],Old=[also ]}considered the
1049 following rules@Chg{Version=[2],New=[ for access discriminants],Old=[]}:
1050 @begin{Itemize}
1051 If a type has an access discriminant,
1052 this automatically makes it limited,
1053 just like having a limited component automatically
1054 makes a type limited.
1055 This was rejected because it decreases program readability,
1056 and because it seemed error prone (two bugs in a previous
1057 version of the RM9X were attributable to this rule).
1058
1059 @ChgRef{Version=[2],Kind=[Revised]}
1060 A type with an access discriminant shall be limited.
1061 This is equivalent to the rule we actually chose@Chg{Version=[2],New=[ for Ada 95],Old=[]},
1062 except that it allows a type to have an access discriminant
1063 if it is limited just because of a limited component.
1064 For example, any record containing a task would be allowed to have
1065 an access discriminant, whereas the actual rule requires
1066 @lquotes@key[limited] @key[record]@rquotes@;.
1067 This rule was also rejected due to readability concerns,
1068 and because would interact badly with the rules for
1069 limited types that @lquotes@;become nonlimited@rquotes@;.
1070
1071 @ChgRef{Version=[2],Kind=[AddedNormal]}
1072 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0063-1]}
1073 @ChgAdded{Version=[2],Text=[A type may have an access discriminant if
1074 it is @Chg{Version=[3],New=[an immutably limited],Old=[a limited partial
1075 view, or a task, protected, or explicitly limited record]} type. This
1076 was the rule chosen for Ada 95.]}
1077
1078 @ChgRef{Version=[2],Kind=[AddedNormal]}
1079 @ChgAdded{Version=[2],Text=[Any type may have an access discriminant.
1080 For nonlimited type, there is no special accessibility for access
1081 discriminants; they're the same as any other anonymous access component.
1082 For a limited type, they have the special accessibility of Ada 95. However,
1083 this doesn't work because a limited partial view can have a nonlimited
1084 full view -- giving the two views different accessibility.]}
1085
1086 @ChgRef{Version=[2],Kind=[AddedNormal]}
1087 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0063-1]}
1088 @ChgAdded{Version=[2],Text=[Any type may have an access discriminant,
1089 as above. However, special accessibility rules only apply to types
1090 that are @Chg{Version=[3],New=[immutably],Old=[@lquotes@;really@rquotes@;]}
1091 limited (task, protected, and
1092 explicitly limited records). However, this breaks privacy; worse,
1093 @LegalityTitle depend on the definition of accessibility.]}
1094
1095 @ChgRef{Version=[2],Kind=[AddedNormal]}
1096 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0063-1]}
1097 @ChgAdded{Version=[2],Text=[Any type may have an access discriminant,
1098 as above. Limited types have special accessibility, while nonlimited
1099 types have normal accessibility. However, a limited partial view with an
1100 access discriminant can only be completed by @Chg{Version=[3],New=[an
1101 immutably limited],Old=[a task, protected, or
1102 explicitly limited record]} type. That prevents accessibility from changing.
1103 A runtime accessibility check is required on generic formal types with
1104 access discriminants. However, changing between limited and nonlimited
1105 types would have far-reaching consequences for access discriminants @em
1106 which is uncomfortable.]}
1107
1108 @ChgRef{Version=[2],Kind=[AddedNormal]}
1109 @ChgAdded{Version=[2],Text=[Any type may have an access discriminant.
1110 All types have special accessibility. This was considered early during
1111 the Ada 9X process, but was dropped for @lquotes@;unpleasant
1112 complexities@rquotes@;, which unfortunately aren't recorded. It does
1113 seem that an accessibility check would be needed on assignment of such
1114 a type, to avoid copying an object with a discriminant pointing to a
1115 local object into a more global object (and thus creating a dangling
1116 pointer).]}
1117
1118 @ChgRef{Version=[2],Kind=[AddedNormal]}
1119 @ChgAdded{Version=[2],Text=[Any type may have an access discriminant,
1120 but access discriminants cannot have defaults. All types have special
1121 accessibility. This gets rid of the problems on assignment (you couldn't
1122 change such a discriminant), but it would be horribly incompatible with
1123 Ada 95.]}
1124
1125 @ChgRef{Version=[2],Kind=[AddedNormal]}
1126 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0063-1]}
1127 @ChgAdded{Version=[2],Text=[Any type may have an access discriminant,
1128 but access discriminants may have defaults only if they are
1129 @Chg{Version=[3],New=[of an immutably], Old=[a
1130 @lquotes@;really@rquotes@;]} limited type. This is the rule chosen for
1131 Ada 2005, as it is not incompatible, and it doesn't require weird
1132 accessibility checks.]}
1133 @end{Itemize}
1134 @end{Reason}
1135
1136 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00402-01]}
1137 @ChgDeleted{Version=[2],Text=[@nt{Default_expression}s shall be provided either for all or for none
1138 of the discriminants of a @nt{known_@!discriminant_@!part}.
1139 No @nt<default_@!expression>s are permitted in a
1140 @nt<known_@!discriminant_@!part> in a declaration of a tagged
1141 type @Redundant[or a generic formal type].]}
1142 @begin(Reason)
1143 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
1144 @ChgDeleted{Version=[2],Text=[The all-or-none rule
1145 is related to the rule that a discriminant constraint shall specify
1146 values for all discriminants. One could imagine a different rule
1147 that allowed a constraint to specify only some of the discriminants,
1148 with the others provided by default. Having defaults for discriminants
1149 has a special significance @em it allows objects of the type to
1150 be unconstrained, with the discriminants alterable as part of
1151 assigning to the object.]}
1152
1153 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg]}
1154 @ChgDeleted{Version=[2],Text=[Defaults for discriminants of tagged types
1155 are disallowed so that every object of a
1156 tagged type is constrained,
1157 either by an explicit constraint,
1158 or by its initial discriminant values.
1159 This substantially simplifies the semantic rules
1160 and the implementation of inherited
1161 dispatching operations. For generic formal types,
1162 the restriction simplifies the type matching rules.
1163 If one simply wants a "default" value for the discriminants,
1164 a constrained subtype can be declared for future use.]}
1165 @end(Reason)
1166
1167 @Leading@;For a type defined by a @nt<derived_type_definition>,
1168 if a @nt<known_discriminant_part> is provided in its declaration, then:
1169 @begin{Itemize}
1170 The parent subtype shall be constrained;
1171
1172 If the parent type is not a tagged type, then each discriminant
1173 of the derived type shall be used in the constraint defining
1174 the parent subtype;@begin{ImplNote}
1175
1176 This ensures that the new discriminant can share storage with
1177 an existing discriminant.@end{implnote}
1178
1179 If a discriminant is used in the constraint defining the parent subtype,
1180 the subtype of the discriminant shall be statically compatible
1181 (see @RefSecNum{Statically Matching Constraints and Subtypes}) with the
1182 subtype of the corresponding parent discriminant.
1183 @begin(Reason)
1184 This ensures that on conversion
1185 (or extension via an extension aggregate) to a distantly related type,
1186 if the discriminants satisfy the target type's requirements they satisfy
1187 all the intermediate types' requirements as well.
1188 @end(Reason)
1189 @begin(Ramification)
1190 There is no requirement that the new discriminant have the
1191 same (or any) @nt<default_expression> as the parent's discriminant.
1192 @end(Ramification)
1193 @end{Itemize}
1194
1195 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0102-1]}
1196 @ChgDeleted{Version=[3],Text=[The type of the @nt<default_expression>, if any,
1197 for an access discriminant shall be convertible to the anonymous access type of
1198 the discriminant (see @RefSecNum{Type Conversions}).
1199 @PDefn2{Term=[convertible],Sec=(required)}]}
1200 @begin{Ramification}
1201 @ChgRef{Version=[3],Kind=[Deleted]}
1202 @ChgDeleted{Version=[3],Text=[This requires convertibility
1203 of the designated subtypes.]}
1204 @end{ramification}
1205 @end{Legality}
1206
1207 @begin{StaticSem}
1208 A @nt{discriminant_specification} declares a discriminant;
1209 the @nt{subtype_mark}
1210 denotes its subtype unless it is an access discriminant, in which case
1211 the discriminant's subtype is the anonymous access-to-variable subtype
1212 defined by the @nt{access_definition}.
1213
1214 @Redundant[For a type defined by a @nt<derived_type_definition>,
1215 each discriminant of the parent type
1216 is either inherited,
1217 constrained to equal some new discriminant of the derived type,
1218 or constrained to the value of an expression.]
1219 @Defn{corresponding discriminants}
1220 When inherited or constrained to equal some new discriminant,
1221 the parent discriminant and the discriminant of the derived type are said
1222 to @i(correspond).
1223 Two discriminants also correspond
1224 if there is some common discriminant to which they
1225 both correspond. A discriminant corresponds to itself
1226 as well.
1227 @Defn{specified discriminant}
1228 If a discriminant of a parent type is constrained to a specific value
1229 by a @nt<derived_type_definition>,
1230 then that discriminant is said to be @i(specified)
1231 by that @nt<derived_type_definition>.
1232 @begin{Ramification}
1233
1234 The correspondence relationship is transitive, symmetric,
1235 and reflexive. That is,
1236 if A corresponds to B, and B corresponds to C, then
1237 A, B, and C each corresponds to A, B, and C in all combinations.@end{ramification}
1238
1239 @Defn2{Term=[depend on a discriminant],
1240 Sec=(for a @nt<constraint> or @nt<component_definition>)}
1241 A @nt{constraint} that appears
1242 within the definition of a discriminated
1243 type @i(depends on a discriminant) of the type if it names the
1244 discriminant as a bound or discriminant value.
1245 A @nt{component_definition} depends on a discriminant if its
1246 @nt{constraint} depends on the discriminant, or on a discriminant
1247 that corresponds to it.
1248 @begin(Ramification)
1249 A @nt{constraint} in a @nt{task_body} is not considered to
1250 @i(depend) on a discriminant of the task type, even if it
1251 names it. It is only the @nt<constraint>s in the type definition
1252 itself that are considered dependents. Similarly for protected types.
1253 @end(Ramification)
1254
1255 @Leading@keepnext@Defn2{Term=[depend on a discriminant], Sec=(for a component)}
1256 A component @i(depends on a discriminant) if:
1257 @begin(itemize)
1258 Its @nt{component_definition} depends on the discriminant; or
1259 @begin{Ramification}
1260 A component does @i(not) depend on a discriminant just because
1261 its @nt<default_expression> refers to the discriminant.@end{ramification}
1262
1263 It is declared in a @nt{variant_part} that is governed by the
1264 discriminant; or
1265
1266 It is a component inherited as part of a @nt<derived_type_definition>,
1267 and the @nt<constraint> of the @i(parent_)@nt<subtype_indication>
1268 depends on the discriminant; or
1269 @begin{Reason}
1270 When the parent subtype depends on a discriminant, the parent
1271 part of the derived type is treated like a discriminant-dependent
1272 component.
1273 @end{Reason}
1274 @begin{Ramification}
1275 Because of this rule, we don't really need to worry about @lquotes@;corresponding@rquotes@;
1276 discriminants, since all the inherited components will be
1277 discriminant-dependent if there is a new @nt<known_discriminant_part>
1278 whose discriminants are used to constrain the old discriminants.
1279 @end{Ramification}
1280
1281 It is a subcomponent of a component that depends on the discriminant.
1282 @end(itemize)
1283 @begin{Reason}
1284 The concept of discriminant-dependent (sub)components is primarily used
1285 in various rules that disallow renaming or 'Access, or specify that
1286 certain discriminant-changing assignments are erroneous.
1287 The goal is to allow implementations to move around or change the size
1288 of discriminant-dependent subcomponents upon a discriminant-changing
1289 assignment to an enclosing object. The above definition specifies that
1290 all subcomponents of a discriminant-dependent component or parent part
1291 are themselves discriminant-dependent, even though their presence
1292 or size does not in fact depend on a discriminant. This is because
1293 it is likely that they will move in a discriminant-changing assignment
1294 if they are a component of one of several discriminant-dependent
1295 parts of the same record.
1296 @end{Reason}
1297
1298 Each value of a discriminated type includes a value for
1299 each component
1300 of the type that does not depend
1301 on a discriminant@Redundant[; this includes the discriminants
1302 themselves]. The values
1303 of discriminants determine which other component values are present
1304 in the value of the discriminated type.
1305 @begin{Honest}
1306 Which values are present might depend on discriminants of
1307 some ancestor type that are constrained in an intervening
1308 @nt<derived_type_definition>. That's why we say "values of discriminants"
1309 instead of "values of @i(the) discriminants" @em a subtle point.@end{honest}
1310
1311 @Defn{known discriminants}
1312 @Defn2{Term=[discriminants], Sec=(known)}
1313 @Defn2{Term=[constrained], Sec=(subtype)}
1314 @Defn2{Term=[unconstrained], Sec=(subtype)}
1315 A type declared with a @nt<known_discriminant_part> is said to have
1316 @i(known discriminants); its first subtype is unconstrained.
1317 @Defn{unknown discriminants}
1318 @Defn2{Term=[discriminants], Sec=(unknown)}
1319 A type declared with an
1320 @nt<unknown_discriminant_part> is said to have @i(unknown discriminants).
1321 A type declared without a @nt<discriminant_part> has
1322 no discriminants, unless it is a derived type; if derived,
1323 such a type has the same sort of discriminants (known, unknown, or none)
1324 as its parent (or ancestor) type.
1325 A tagged class-wide type also has unknown discriminants.
1326 @Defn{class-wide type}
1327 @Defn{indefinite subtype}
1328 @Redundant[Any subtype of a type with unknown discriminants
1329 is an unconstrained and indefinite
1330 subtype (see @RefSecNum{Types and Subtypes}
1331 and @RefSecNum{Objects and Named Numbers}).]
1332 @begin(Discussion)
1333 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
1334 An @nt<unknown_discriminant_part> @lquotes@;(<>)@rquotes@; is only permitted in
1335 the declaration of a (generic or nongeneric) private type,
1336 private extension, @Chg{Version=[2],New=[incomplete type, ],Old=[]}or
1337 formal derived type.@Comment{That was always intended,
1338 but 8652/0007 was needed to make it true.}
1339 Hence, only such types, descendants thereof, and class-wide
1340 types can have unknown discriminants.
1341 An @nt<unknown_discriminant_part> is used to indicate that the corresponding
1342 actual or full type might have discriminants without defaults,
1343 or be an unconstrained array subtype. Tagged class-wide types
1344 are also considered to have unknown discriminants because discriminants
1345 can be added by type extensions, so the total number of
1346 discriminants of any given value of a tagged class-wide type
1347 is not known at compile time.
1348
1349 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
1350 A subtype with unknown discriminants
1351 is indefinite, and hence an object of such a subtype needs
1352 explicit initialization.@Chg{Version=[2],New=[],Old=[ If the subtype is
1353 limited, no (stand-alone) objects
1354 can be declared since initialization is not permitted (though
1355 formal parameters are permitted, and objects of the actual/full type
1356 will generally be declarable).]} A limited private type with
1357 unknown discriminants is @lquotes@;extremely@rquotes@;
1358 limited;@Chg{Version=[2],New=[ objects of],Old=[]} such a type
1359 @Chg{Version=[2],New=[ can be initialized only by subprograms (either
1360 procedures with a parameter of the type, or a function returning the
1361 type) declared in the package. Subprograms declared elsewhere can operate on
1362 and even return the type, but they can only initialize the object by calling
1363 (ultimately) a subprogram in the package declaring the type. Such a type],Old=[]} is useful for
1364 keeping complete control over object creation within the package declaring
1365 the type.
1366
1367 A partial view of a type might have unknown discriminants, while
1368 the full view of the same type might have known, unknown, or no
1369 discriminants@Chg{New=[.],Old=[,]}
1370 @end(Discussion)
1371 @end{StaticSem}
1372
1373 @begin{RunTime}
1374 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01],ARef=[AI95-00416-01]}
1375 @Chg{Version=[2],New=[For an access discriminant, its],
1376 Old=[An]} @nt<access_definition> is
1377 elaborated when the value of @Chg{Version=[2],New=[the],Old=[a corresponding]}
1378 access discriminant is defined@Chg{Version=[2],New=[:],Old=[, either]}
1379 by evaluation of its @nt<default_expression>@Chg{Version=[2],New=[,],Old=[ or]}
1380 by elaboration of a @nt<discriminant_constraint>@Chg{Version=[2],New=[, or
1381 by an assignment that initializes the enclosing object.],Old=[.
1382 @Redundant[The elaboration of an @nt<access_definition> creates the
1383 anonymous access type. When the expression defining the
1384 access discriminant is evaluated, it is converted to this
1385 anonymous access type (see @RefSecNum{Type Conversions}).]]}
1386 @PDefn2{Term=[implicit subtype conversion],Sec=(access discriminant)}
1387 @begin(Ramification)
1388 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01],ARef=[AI95-00416-01]}
1389 @Chg{Version=[2],New=[The],Old=[This]} conversion@Chg{Version=[2],New=[
1390 of the @nt{expression} defining the access discriminant to the anonymous
1391 access type],Old=[]} raises @Chg{Version=[2],New=[Program_Error],
1392 Old=[Constraint_Error if the initial value is @key(null), or,]} for an
1393 object created by an allocator of an access type T, if the
1394 initial value is an access parameter that designates a view whose
1395 accessibility level is deeper than that of T.
1396 @end(Ramification)
1397 @end{RunTime}
1398
1399 @begin{Notes}
1400 If a discriminated type has @nt<default_expression>s for its
1401 discriminants, then unconstrained variables of the type are permitted,
1402 and the values of the discriminants can be changed by an assignment
1403 to such a variable.
1404 If defaults are not provided for the discriminants, then all
1405 variables of the type are constrained, either by explicit
1406 constraint or by their initial value; the values of the discriminants
1407 of such a variable cannot be changed after initialization.
1408 @begin(Discussion)
1409 This connection between discriminant defaults and unconstrained variables
1410 can be a source of confusion. For Ada 95, we considered various
1411 ways to break the connection between defaults and unconstrainedness,
1412 but ultimately gave up for lack of a sufficiently simple and intuitive
1413 alternative.
1414
1415
1416 @Defn{mutable}
1417 An unconstrained discriminated subtype with defaults is called
1418 a @i{mutable} subtype, and a variable of such a subtype is called
1419 a mutable variable,
1420 because the discriminants of such a variable can change.
1421 There are no mutable arrays (that is, the bounds of an array
1422 object can never change), because there is no way in the language
1423 to define default values for the bounds.
1424 Similarly, there are no mutable class-wide subtypes,
1425 because there is no way to define the default tag,
1426 and defaults for discriminants are not allowed in the
1427 tagged case.
1428 Mutable tags would also require a way for the
1429 maximum possible size of such a class-wide subtype to be known.
1430 (In some implementations, all mutable variables are allocated
1431 with the maximum possible size. This approach is appropriate
1432 for real-time applications where implicit use of the heap
1433 is inappropriate.)
1434
1435 @end(Discussion)
1436
1437 The @nt{default_expression} for a discriminant of a type is evaluated
1438 when an object of an unconstrained subtype of the type is created.
1439
1440 Assignment to a discriminant of an object (after its
1441 initialization) is not allowed,
1442 since the name of a discriminant is a constant; neither
1443 @nt{assignment_statement}s
1444 nor assignments inherent in passing as an @key(in out)
1445 or @key(out) parameter
1446 are allowed.
1447 Note however that the value of a discriminant
1448 can be changed by assigning to the enclosing object, presuming it
1449 is an unconstrained variable.
1450 @begin(Discussion)
1451 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
1452 An @nt{unknown_discriminant_part} is permitted only in the
1453 declaration of a private type (including generic formal private),
1454 private extension, @Chg{Version=[2],New=[incomplete type, ],Old=[]}or
1455 generic formal derived type.
1456 These are the things that will have a corresponding completion or
1457 generic actual, which will either define the discriminants,
1458 or say there are none.
1459 The (<>) indicates that the actual/full subtype might be an indefinite subtype.
1460 An @nt{unknown_discriminant_part} is not permitted in a normal
1461 untagged derived type declaration, because there is no separate
1462 full type declaration for such a type.
1463 Note that (<>) allows unconstrained array bounds;
1464 those are somewhat like undefaulted discriminants.
1465
1466 For a derived type, either the discriminants are inherited as is,
1467 or completely respecified in a new @nt<discriminant_part>. In this
1468 latter case, each discriminant of the parent type shall be constrained,
1469 either to a specific value, or to equal one of the new discriminants.
1470 Constraining a parent type's discriminant to equal one of the new
1471 discriminants is like a renaming of the discriminant, except that
1472 the subtype of the new discriminant can be more restrictive than
1473 that of the parent's one.
1474 In any case, the new discriminant can share storage with the
1475 parent's discriminant.
1476 @end(Discussion)
1477
1478 A discriminant that is of a named access type is not called
1479 an access discriminant; that term is
1480 used only for discriminants defined by an @nt<access_definition>.
1481 @end{Notes}
1482
1483 @begin{Examples}
1484 @Leading@keepnext@i(Examples of discriminated types:)
1485 @begin(Example)
1486 @key(type) Buffer(Size : Buffer_Size := 100) @key(is) --@RI[ see @RefSecNum(Integer Types)]
1487 @key(record)
1488 Pos : Buffer_Size := 0;
1489 Value : String(1 .. Size);
1490 @key(end) @key(record);
1491
1492 @key(type) Matrix_Rec(Rows, Columns : Integer) @key(is)
1493 @key(record)
1494 Mat : Matrix(1 .. Rows, 1 .. Columns); --@RI[ see @RefSecNum(Array Types)]
1495 @key(end) @key(record);
1496
1497 @key(type) Square(Side : Integer) @key(is) @key(new)
1498 Matrix_Rec(Rows => Side, Columns => Side);
1499
1500 @key(type) Double_Square(Number : Integer) @key(is)
1501 @key(record)
1502 Left : Square(Number);
1503 Right : Square(Number);
1504 @key(end) @key(record);
1505
1506 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00433-01]}
1507 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0229-1]}
1508 @Chg{Version=[2],New=[@key(task type) Worker(Prio : System.Priority; Buf : @key(access) Buffer)@Chg{Version=[3],New=[
1509 @key(with) Priority => Prio @key(is) --@RI[ see @RefSecNum{Task Priorities}]],Old=[@key(is)]}
1510 --@RI[ discriminants used to parameterize the task type (see @RefSecNum{Task Units and Task Objects})]@Chg{Version=[3],New=[],Old=[
1511 @key(pragma) Priority(Prio); --@RI[ see @RefSecNum{Task Priorities}]]}
1512 @key(entry) Fill;
1513 @key(entry) Drain;
1514 @key(end) Worker;],Old=[@key(type) Item(Number : Positive) @key(is)
1515 @key(record)
1516 Content : Integer;
1517 --@RI[ no component depends on the discriminant]
1518 @key(end) @key(record);]}
1519 @end(Example)
1520 @end{Examples}
1521
1522 @begin{Extend83}
1523 @Defn{extensions to Ada 83}
1524 The syntax for a @nt{discriminant_specification}
1525 is modified to allow an @i{access discriminant},
1526 with a type specified by an @nt{access_definition}
1527 (see @RefSecNum{Access Types}).
1528
1529 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
1530 Discriminants are allowed on all composite types other than array@Chg{Version=[2],New=[ and
1531 interface],Old=[]} types.
1532
1533 Discriminants may be of an access type.
1534 @end{Extend83}
1535
1536 @begin{DiffWord83}
1537 @nt{Discriminant_part}s are not elaborated,
1538 though an @nt<access_definition> is elaborated
1539 when the discriminant is initialized.
1540
1541 @end{DiffWord83}
1542
1543 @begin{Extend95}
1544 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00230-01],ARef=[AI95-00402-01],ARef=[AI95-00416-01]}
1545 @ChgAdded{Version=[2],Text=[Access discriminants (anonymous access types
1546 used as a discriminant) can be used on any type allowing discriminants.
1547 Defaults aren't allowed on discriminants of nonlimited types, however, so
1548 that accessibility problems don't happen on assignment.]}
1549
1550 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
1551 @ChgAdded{Version=[2],Text=[
1552 @nt{null_exclusion} can be used in the declaration of a discriminant.]}
1553 @end{Extend95}
1554
1555 @begin{DiffWord95}
1556 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0007],ARef=[AI95-00098-01]}
1557 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> The wording was clarified so
1558 that types that cannot have discriminants cannot have an
1559 @nt{unknown_discriminant_part}.]}
1560
1561 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
1562 @ChgAdded{Version=[2],Text=[Added wording to prevent interfaces from
1563 having discriminants. We don't want interfaces to have any components.]}
1564
1565 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00254-01]}
1566 @ChgAdded{Version=[2],Text=[Removed wording which implied or required an
1567 access discriminant to have an access-to-object type (anonymous access
1568 types can now be access-to-subprogram types as well).]}
1569
1570 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00326-01]}
1571 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
1572 @ChgAdded{Version=[2],Text=[Fixed the wording of the introduction to this
1573 @Chg{Version=[3],New=[subclause],Old=[clause]} to reflect that both incomplete
1574 and partial views can have unknown discriminants. That was always true,
1575 but for some reason this wording specified partial views.]}
1576
1577 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
1578 @ChgAdded{Version=[2],Text=[Changed the wording to use the new term
1579 @lquotes@;explicitly limited record@rquotes, which makes the intent
1580 much clearer (and eliminates confusion with derived types that happen to
1581 contain the reserved word @key[limited]).]}
1582 @end{DiffWord95}
1583
1584 @begin{Incompatible2005}
1585 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0063-1]}
1586 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}@b<Correction:>
1587 Changed the rules for when access discriminants can have defaults to depend
1588 on the new definition for immutably limited types; this will help ensure that
1589 unusual corner cases are properly handled. Note that the Ada 2005 rule was
1590 unintentionally incompatible with the Ada 95 rule (as enforced by the ACATS);
1591 this change brings it back into alignment with actual practice. So there
1592 should be no practical incompatibility.]}
1593 @end{Incompatible2005}
1594
1595 @begin{Extend2005}
1596 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0214-1]}
1597 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
1598 A limited tagged type may now have defaults for its discriminants.]}
1599 @end{Extend2005}
1600
1601 @begin{DiffWord2005}
1602 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0102-1]}
1603 @ChgAdded{Version=[3],Text=[@b<Correction:> Moved implicit conversion
1604 @LegalityName to @RefSecNum{The Context of Overload Resolution}.]}
1605 @end{DiffWord2005}
1606
1607
1608 @ISOOnlyRMNewPageVer{Version=[3]}@Comment{For ISO version of Ada 2012 Standard}
1609 @LabeledSubClause{Discriminant Constraints}
1610
1611 @begin{Intro}
1612 A @nt<discriminant_constraint> specifies the values of the discriminants
1613 for a given discriminated type.
1614 @end{Intro}
1615
1616 @begin{MetaRules}
1617 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
1618 The rules in this @Chg{Version=[3],New=[subclause],Old=[clause]} are
1619 intentionally parallel to those given in @RefSec{Record Aggregates}.
1620 @end{MetaRules}
1621
1622 @begin{Syntax}
1623 @Syn{lhs=<discriminant_constraint>,rhs="
1624 (@Syn2{discriminant_association} {, @Syn2{discriminant_association}})"}
1625
1626
1627 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0212-1]}
1628 @Syn{lhs=<discriminant_association>,rhs="
1629 [@SynI{discriminant_}@Syn2{selector_name} {@Chg{Version=[5],New=['|'],Old=[|]} @SynI{discriminant_}@Syn2{selector_name}} =>] @Syn2{expression}"}
1630
1631 @begin{SyntaxText}
1632 @Defn{named discriminant association}
1633 A @nt<discriminant_association>
1634 is said to be @i(named) if it has one or more
1635 @i(discriminant_)@nt<selector_name>s;
1636 @Defn{positional discriminant association}
1637 it is otherwise said to be @i(positional).
1638 In a @nt<discriminant_constraint>,
1639 any positional associations shall precede any named
1640 associations.
1641 @end{SyntaxText}
1642
1643 @end{Syntax}
1644
1645 @begin{Resolution}
1646 Each @nt<selector_name> of a named @nt<discriminant_@!association> shall
1647 resolve to denote a discriminant of the subtype being constrained;
1648 @Defn2{Term=[associated discriminants],
1649 Sec=(of a named @nt<discriminant_association>)}
1650 the discriminants so named are the @i(associated discriminants) of
1651 the named association.
1652 @Defn2{Term=[associated discriminants],
1653 Sec=(of a positional @nt<discriminant_association>)}
1654 For a positional association, the @i(associated discriminant)
1655 is the one whose @nt<discriminant_@!specification> occurred in
1656 the corresponding position in the @nt<known_@!discriminant_@!part>
1657 that defined the discriminants of the subtype being constrained.
1658
1659 @PDefn2{Term=[expected type], Sec=(discriminant_association expression)}
1660 The expected type for the @nt{expression} in
1661 a @nt{discriminant_association}
1662 is that of the associated discriminant(s).
1663 @end{Resolution}
1664
1665 @begin{Legality}
1666 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0008],ARef=[AI95-00168-01]}
1667 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00363-01]}
1668 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0041-1]}
1669 A @nt{discriminant_constraint} is only allowed in a
1670 @nt{subtype_indication} whose @nt{subtype_mark} denotes
1671 either an unconstrained discriminated subtype, or an
1672 unconstrained access subtype whose designated subtype is
1673 an unconstrained discriminated subtype.
1674 @Chg{New=[However, in the case of @Chg{Version=[2],New=[an],Old=[a general]}
1675 access subtype, a @nt{discriminant_@!constraint} is
1676 @Chg{Version=[3],New=[legal only if any dereference of a value of the access
1677 type is known to be constrained (see @RefSecNum{Objects and Named Numbers})],
1678 Old=[illegal if @Chg{Version=[2],New=[the
1679 designated type has a partial view that is constrained or, for a general
1680 access subtype, has @nt{default_expression}s for its discriminants],Old=[]}]}],
1681 Old=[there is a place within the
1682 immediate scope of the designated subtype where the designated subtype's view
1683 is constrained]}.
1684 @Chg{Version=[2],New=[In addition to the places where @LegalityTitle@;
1685 normally apply (see @RefSecNum{Generic Instantiation}),
1686 these rules apply also in the private part of an instance
1687 of a generic unit.@PDefn{generic contract issue}],Old=[]}@Chg{Version=[3],
1688 New=[],Old=[@Chg{Version=[2],
1689 New=[ In a generic body, this rule is checked presuming all
1690 formal access types of the generic might be general access types, and all
1691 untagged discriminated formal types of the generic might have
1692 @nt{default_expression}s for their discriminants.],Old=[]}]}
1693
1694 @begin{Reason}
1695 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0008],ARef=[AI95-00168-01]}
1696 @ChgRef{Version=[2],Kind=[DeletedAdded],ARef=[AI95-00363-01]}
1697 @ChgDeleted{Version=[2],Text=[@Chg{New=[The second rule is necessary to
1698 prevent assignments that change the discriminant of a constrained object.
1699 See the defect report for examples.],Old=[]}]}
1700 @end{Reason}
1701
1702 @begin{Reason}
1703 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
1704 @ChgAdded{Version=[2],Text=[The second rule is necessary to prevent objects
1705 from changing so that they no longer match their constraint. In Ada 95, we
1706 attempted to prevent this by banning every case where an aliased object
1707 could be unconstrained or be changed by an enclosing assignment. New ways
1708 to cause this problem were being discovered frequently, meaning that new rules
1709 had to be dreamed up to cover them. Meanwhile, aliased objects and components
1710 were getting more and more limited. In Ada 2005, we sweep away all of that
1711 cruft and replace it by a simple rule @lquotes@;thou shalt not create an
1712 access subtype that can point to an item whose discriminants can be changed by
1713 assignment@rquotes@;.]}
1714 @end{Reason}
1715
1716 @begin{Discussion}
1717 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0041-1]}
1718 @ChgAdded{Version=[3],Text=[The second rule will only use the indefinite or
1719 dereference bullets in the definition of @ldquote@;known to be constrained@rdquote.
1720 The rule is worded in terms of @ldquote@;known to be constrained@rdquote in
1721 order to capture the special rules that apply in generic bodies (rather than
1722 repeating them and getting them subtly wrong).]}
1723 @end{Discussion}
1724
1725 A named @nt<discriminant_association> with more than one
1726 @nt<selector_name> is allowed only if the named discriminants
1727 are all of the same type.
1728 A @nt<discriminant_constraint> shall provide exactly one value for each
1729 discriminant of the subtype being constrained.
1730
1731 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0102-1]}
1732 @ChgDeleted{Version=[3],Text=[The @nt<expression> associated with an access
1733 discriminant shall be of a type convertible to the anonymous access type.
1734 @PDefn2{Term=[convertible],Sec=(required)}]}
1735
1736 @begin(Ramification)
1737
1738 @ChgRef{Version=[3],Kind=[Revised]}
1739 @Chg{Version=[3],New=[In addition, @RefSecNum{The Context of Overload Resolution}
1740 requires that the @nt{expression} associated with an access discriminant
1741 is convertible (see @RefSecNum{Type Conversions}) to the anonymous
1742 access type. ],Old=[]}This implies both convertibility of designated
1743 types, and static accessibility.
1744 This implies that if an object of type T with an
1745 access discriminant is created
1746 by an allocator for an access type A, then it requires that the
1747 type of the @nt<expression> associated
1748 with the access discriminant have an accessibility level
1749 that is not statically deeper than that of A.
1750 This is to avoid dangling references.
1751
1752 @end(Ramification)
1753 @end{Legality}
1754
1755 @begin{RunTime}
1756 @PDefn2{Term=[compatibility], Sec=(discriminant constraint with a
1757 subtype)}
1758 A @nt{discriminant_constraint} is @i(compatible) with an unconstrained
1759 discriminated subtype if each discriminant value belongs to the
1760 subtype of the corresponding discriminant.
1761 @begin{Ramification}
1762 The "dependent
1763 compatibility check" has been eliminated in Ada 95. Any checking
1764 on subcomponents is performed when (and if) an object is created.@end{ramification}
1765 @begin{Discussion}
1766 There is no need to define compatibility with a constrained
1767 discriminated subtype, because one is not allowed to constrain it again.@end{discussion}
1768
1769 @PDefn2{Term=[satisfies], Sec=(a discriminant constraint)}
1770 A composite value @i(satisfies) a discriminant constraint if and only
1771 if each discriminant of the composite value has the value imposed
1772 by the discriminant constraint.
1773
1774 @PDefn2{Term=[elaboration], Sec=(discriminant_constraint)}
1775 For the elaboration of a @nt{discriminant_constraint}, the @nt{expression}s
1776 in the @nt{discriminant_association}s are evaluated in an arbitrary
1777 order and converted to the type of the associated
1778 discriminant (which might raise Constraint_Error @em
1779 see @RefSecNum{Type Conversions});
1780 the @nt{expression} of a named association is evaluated
1781 (and converted) once for each associated discriminant.
1782 @PDefn2{Term=[implicit subtype conversion],Sec=(discriminant values)}
1783 The result of each evaluation and conversion is the
1784 value imposed by the constraint for the associated discriminant.
1785 @begin{Reason}
1786
1787 We convert to the type, not the subtype, so that the definition
1788 of compatibility of discriminant constraints is not vacuous.@end{reason}
1789
1790 @end{RunTime}
1791
1792 @begin{Notes}
1793 The rules of the language ensure that
1794 a discriminant of an object always has a value, either
1795 from explicit or implicit initialization.
1796 @begin(Discussion)
1797 Although it is illegal to constrain a class-wide tagged subtype, it
1798 is possible to have a partially constrained class-wide
1799 subtype: If the subtype S is defined by T(A => B),
1800 then S'Class is partially constrained in the sense that objects of
1801 subtype S'Class have to have discriminants corresponding
1802 to A equal to B,
1803 but there can be other discriminants defined
1804 in extensions that are not constrained to any particular value.
1805 @end(Discussion)
1806 @end{Notes}
1807
1808 @begin{Examples}
1809 @Leading@keepnext@ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
1810 @i{Examples (using types declared above in
1811 @Chg{Version=[3],New=[subclause],Old=[clause]} @RefSecNum(Discriminants)):}
1812 @begin(Example)
1813 Large : Buffer(200); --@RI[ constrained, always 200 characters]
1814 --@RI[ (explicit discriminant value)]
1815 Message : Buffer; --@RI[ unconstrained, initially 100 characters]
1816 --@RI[ (default discriminant value)]
1817 Basis : Square(5); --@RI[ constrained, always 5 by 5]
1818 Illegal : Square; --@RI[ illegal, a Square has to be constrained]
1819 @end(Example)
1820 @end{Examples}
1821
1822 @begin{Inconsistent83}
1823 @Defn{inconsistencies with Ada 83}
1824 Dependent compatibility checks are no longer performed on
1825 subtype declaration. Instead they are deferred until
1826 object creation (see @RefSecNum(Object Declarations)).
1827 This is upward compatible for a program
1828 that does not raise Constraint_Error.
1829 @end{Inconsistent83}
1830
1831 @begin{DiffWord83}
1832 Everything in RM83-3.7.2(7-12), which specifies the
1833 initial values for discriminants, is now redundant
1834 with 3.3.1, 6.4.1, 8.5.1, and 12.4. Therefore, we don't
1835 repeat it here. Since the material is largely intuitive,
1836 but nevertheless complicated to state formally, it doesn't
1837 seem worth putting it in a "NOTE."
1838 @end{DiffWord83}
1839
1840 @begin{Incompatible95}
1841 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0008],ARef=[AI95-00168-01],ARef=[AI95-00363-01]}
1842 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
1843 The Corrigendum added a restriction on @nt{discriminant_constraint}s for
1844 general access subtypes. Such constraints are prohibited
1845 if the designated type can be treated as constrained somewhere in the program.
1846 Ada 2005 goes further and prohibits such @nt{discriminant_constraint}s if
1847 the designated type has (or might have, in the case of a formal type)
1848 defaults for its discriminants. The use of general access subtypes is rare,
1849 and this eliminates a boatload of problems that required many restrictions
1850 on the use of aliased objects and components (now lifted). Similarly,
1851 Ada 2005 prohibits @nt{discriminant_constraint}s on any access type whose
1852 designated type has a partial view that is constrained. Such a type will
1853 not be constrained in the heap to avoid privacy problems. Again, the use
1854 of such subtypes is rare (they can only happen within the package and its
1855 child units).]}
1856 @end{Incompatible95}
1857
1858 @begin{Diffword2005}
1859 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0041-1]}
1860 @ChgAdded{Version=[3],Text=[@b<Correction:> Revised the rules on
1861 access subtypes having discriminant constraints to depend on the
1862 @ldquote@;known to be constrained@rdquote rules. This centralizes
1863 the rules so that future fixes need to be made in only one place,
1864 as well as fixing bugs in obscure cases.]}
1865
1866 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0102-1]}
1867 @ChgAdded{Version=[3],Text=[@b<Correction:> Moved implicit conversion
1868 @LegalityName to @RefSecNum{The Context of Overload Resolution}.]}
1869 @end{Diffword2005}
1870
1871
1872 @LabeledSubClause{Operations of Discriminated Types}
1873
1874 @begin{Intro}
1875 @Redundant[If a discriminated type has @nt<default_expression>s for its
1876 discriminants, then unconstrained variables of the type are permitted,
1877 and the discriminants of such a variable can be changed by assignment to
1878 the variable. For a formal parameter of such a type, an attribute
1879 is provided to determine whether the corresponding actual parameter
1880 is constrained or unconstrained.]
1881 @end{Intro}
1882
1883 @begin{StaticSem}
1884 @Leading@Keepnext@;For @PrefixType{a @nt<prefix> A that is of a
1885 discriminated type @Redundant[(after any implicit dereference)]},
1886 the following attribute is defined:
1887 @begin(description)
1888 @ChgAttribute{Version=[5], Kind=[Revised], ChginAnnex=[T],
1889 Leading=[F], Prefix=<A>, AttrName=<Constrained>, ARef=[AI05-0214-1], ARef=[AI12-0183-1],
1890 InitialVersion=[0],
1891 Text=[Yields the value True if A denotes a constant, a value,
1892 @Chg{Version=[3],New=[a tagged object, ],Old=[]}or a constrained variable,
1893 and False otherwise.@Chg{Version=[5],New=[ The value of this attribute is
1894 of the predefined type Boolean.],Old=[]}]}
1895 @begin(ImplNote)
1896 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0214-1]}
1897 This attribute is primarily used on parameters, to determine whether
1898 the discriminants can be changed as part of an assignment.
1899 The Constrained attribute is statically True for @key(in) parameters.
1900 For @key(in out) and @key(out) parameters of a discriminated type,
1901 the value of this attribute needs to be passed as an implicit
1902 parameter, in general. However, if the type @Chg{Version=[3],New=[is
1903 tagged or ],Old=[]}does not have
1904 defaults for its discriminants, the attribute is statically True,
1905 so no implicit parameter is needed.
1906 Parameters of a limited @Chg{Version=[3],New=[untagged ],Old=[]}type
1907 with defaulted discriminants need this implicit parameter,
1908 unless there are no nonlimited views,
1909 because they might be passed to a subprogram whose body has
1910 visibility on a nonlimited view of the type, and hence might be
1911 able to assign to the object and change its discriminants.
1912 @end(ImplNote)
1913
1914 @begin{Reason}
1915 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0214-1]}
1916 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0005-1]}
1917 @ChgAdded{Version=[3],Text=[All tagged objects are known to be constrained (as
1918 nonlimited tagged types cannot have discriminant defaults, and limited tagged
1919 objects are immutably limited), and are always considered constrained by this
1920 attribute to avoid distributed overhead for parameters of limited
1921 @Chg{Version=[4],New=[class-wide],Old=[classwide]}
1922 types, as limited tagged objects may technically be unconstrained if they use
1923 defaulted discriminants. Such objects still cannot have their discriminants
1924 changed, as assignment is not supported for them, so there is no use for this
1925 attribute that would justify the overhead of passing it with all
1926 @Chg{Version=[4],New=[class-wide],Old=[classwide]} parameters.]}
1927 @end{Reason}
1928
1929 @begin{Discussion}
1930 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1],ARef=[AI05-0214-1]}
1931 @ChgAdded{Version=[3],Text=[If the type of A is a type derived from an untagged
1932 partial view of a tagged type such that it is not a tagged type, then A is not
1933 considered a tagged object, and A'Constrained can return either True or
1934 False depending on the nature of the object.]}
1935 @end{Discussion}
1936 @end(description)
1937 @EndPrefixType{}
1938 @end{StaticSem}
1939
1940 @begin{Erron}
1941 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1942 The execution of a construct is erroneous if the construct
1943 has a constituent that is a @nt<name> denoting
1944 a subcomponent that depends on discriminants, and the
1945 value of any of these discriminants is changed by this execution
1946 between evaluating the @nt<name> and the last use (within this
1947 execution) of the subcomponent denoted by the @nt<name>.
1948 @begin{Ramification}
1949 This rule applies to @nt<assignment_statement>s,
1950 calls (except when the discriminant-dependent subcomponent is
1951 an @key(in) parameter passed by copy),
1952 @nt<indexed_component>s, and @nt<slice>s.
1953 Ada 83 only covered the first two cases. AI83-00585 pointed out
1954 the situation with the last two cases.
1955 The cases of @nt<object_renaming_declaration>s and
1956 generic formal @key(in out) objects are handled differently,
1957 by disallowing the situation at compile time.
1958 @end{Ramification}
1959 @end{Erron}
1960
1961 @begin{Extend83}
1962 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{To be consistent with 8652/0006}
1963 @Defn{extensions to Ada 83}
1964 For consistency with other attributes, we are allowing
1965 the @Chg{New=[@nt{prefix}],Old=[prefix]} of Constrained to be a value as well
1966 as an object of a discriminated type, and also an implicit
1967 dereference. These extensions are
1968 not important capabilities, but there seems no
1969 reason to make this attribute different from
1970 other similar attributes. We are curious what most
1971 Ada 83 compilers do with F(1).X'Constrained.
1972
1973 We now handle in a general way
1974 the cases of erroneousness identified by AI83-00585, where
1975 the @nt<prefix> of an @nt<indexed_component> or @nt<slice>
1976 is discriminant-dependent, and the evaluation of the index or
1977 discrete range changes the value of a discriminant.
1978 @end{Extend83}
1979
1980 @begin{DiffWord83}
1981 We have moved all discussion of erroneous use of @nt<name>s
1982 that denote discriminant-dependent
1983 subcomponents to this subclause. In Ada 83, it used to
1984 appear separately under @nt{assignment_statement}s and
1985 subprogram calls.
1986 @end{DiffWord83}
1987
1988 @begin{DiffWord2005}
1989 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0214-1]}
1990 @ChgAdded{Version=[3],Text=[A'Constrained is now defined to return
1991 True for any A that is a tagged object. This doesn't change the result
1992 for any A allowed by previous versions of Ada; the change is necessary to
1993 avoid unnecessary overhead for limited tagged parameters.]}
1994 @end{DiffWord2005}
1995
1996
1997 @LabeledClause{Record Types}
1998
1999 @begin{Intro}
2000 @Defn{record}
2001 @Defn{record type}
2002 A record object is a composite object consisting of named components.
2003 The value of a record object is a composite value consisting of the
2004 values of the components.
2005 @IndexSee{Term=[structure],See=(record type)}
2006 @end{Intro}
2007
2008 @begin{Syntax}
2009 @Syn{lhs=<record_type_definition>,rhs="[[@key{abstract}] @key{tagged}] [@key{limited}] @Syn2{record_definition}"}
2010
2011 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0213-1]}
2012 @Syn{lhs=<record_definition>,rhs="
2013 @key{record}
2014 @Syn2{component_list}
2015 @key{end} @key{record}@Chg{Version=[5],New=< [@SynI{record_}@Syn2{identifier}]>,Old=<>}
2016 | @key{null record}"}
2017
2018
2019 @Syn{lhs=<component_list>,rhs="
2020 @Syn2{component_item} {@Syn2{component_item}}
2021 | {@Syn2{component_item}} @Syn2{variant_part}
2022 | @key{null};"}
2023
2024 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0009],ARef=[AI95-00137-01]}
2025 @Syn{lhs=<component_item>,rhs="@Syn2{component_declaration} | @Chg{New=[@Syn2{aspect_clause}],Old=[@Syn2{representation_clause}]}"}
2026
2027 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
2028 @Syn{lhs=<component_declaration>,rhs="
2029 @Syn2{defining_identifier_list} : @Syn2{component_definition} [:= @Syn2{default_expression}]@Chg{Version=[3],New=<
2030 [@Syn2{aspect_specification}]>,Old=[]};"}
2031
2032 @ChgRef{Version=[5],Kind=[Added],ARef=[AI12-0213-1]}
2033 @ChgAdded{Version=[5],Text=[If a @SynI<record_>@nt<identifier> appears at the
2034 end of the @nt<record_definition>, it shall repeat the @nt<defining_identifier>
2035 of the enclosing @nt<full_type_declaration>.]}
2036 @end{Syntax}
2037
2038 @begin{Resolution}
2039 @PDefn2{Term=[expected type], Sec=(component_declaration default_expression)}
2040 The expected type for the @nt<default_expression>, if any, in
2041 a @nt<component_declaration> is the type of the
2042 component.
2043 @end{Resolution}
2044
2045 @begin{Legality}
2046 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00287-01]}
2047 @ChgDeleted{Version=[2],Text=[A @nt<default_expression> is not permitted if
2048 the component is of a limited type.]}
2049
2050 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00366-01]}@ChgNote{This is not THE definition of component}
2051 @PDefn2{Term=[components], Sec=(of a record type)}
2052 Each @nt<component_declaration> declares a
2053 @Chg{Version=[2],New=[component],Old=[@i(component)]} of the record type.
2054 Besides components declared by @nt<component_declaration>s, the components of a
2055 record type include any components declared by @nt<discriminant_specification>s
2056 of the record type declaration. @Redundant[The identifiers of all components of
2057 a record type shall be distinct.]
2058 @begin{TheProof}
2059 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2060 The identifiers of all
2061 components of a record type have to
2062 be distinct because they are all declared immediately
2063 within the same declarative region. See @Chg{Version=[3],New=[Clause],Old=[Section]}
2064 @RefSecNum{Visibility Rules}.
2065 @end{theproof}
2066
2067
2068 Within a @nt{type_declaration},
2069 a @nt{name} that denotes a component, protected subprogram,
2070 or entry of the type is allowed only in the following cases:
2071
2072 @begin{Itemize}
2073 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0004-1],ARef=[AI05-0295-1]}
2074 A @nt{name} that denotes any component, protected subprogram,
2075 or entry is allowed within @Chg{Version=[3],New=[an @nt{aspect_specification},
2076 an operational item, or ],Old=[]}a
2077 representation item that occurs within the declaration of the composite type.
2078
2079 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0264-1]}
2080 A @nt{name} that denotes a noninherited discriminant
2081 is allowed within the declaration of the type,
2082 but not within the @nt{discriminant_part}.
2083 If the discriminant is used to define the constraint of
2084 a component, the bounds of an entry family, or
2085 the constraint of the parent subtype in a
2086 @nt<derived_type_definition>@Chg{Version=[3],New=[,],Old=[]}
2087 then its name shall appear alone as a @nt<direct_name> (not as
2088 part of a larger expression or expanded name).
2089 A discriminant shall not be used to define the constraint of
2090 a scalar component.@Defn2{Term=[discriminant],Sec=[use in a record definition]}@ChgNote{New index entry}
2091 @begin{Reason}
2092 The penultimate restriction simplifies implementation,
2093 and allows the outer discriminant and the inner discriminant
2094 or bound to possibly share storage.
2095 @end{reason}
2096 @begin{Ramification}
2097 Other rules prevent such a discriminant from being an inherited one.
2098 @end{ramification}
2099 @begin{Reason}
2100 The last restriction is inherited from Ada 83.
2101 The restriction is not really necessary from a language design point of
2102 view, but we did not remove it,
2103 in order to avoid unnecessary changes to existing compilers.
2104 @end{Reason}
2105 @begin{Discussion}
2106 Note that a discriminant can be used to define the constraint
2107 for a component that is of an access-to-composite type.
2108 @end{Discussion}
2109 @begin{Reason}
2110 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
2111 The above rules, and a similar one in @RefSecNum{Subprogram Declarations}
2112 for formal parameters, are intended to allow initializations of
2113 components or parameters to occur in @Chg{Version=[2],
2114 New=[a (nearly)],Old=[an]} arbitrary order @em whatever
2115 order is most efficient@Chg{Version=[2],New=[ (subject to the restrictions
2116 of @RefSecNum{Object Declarations})],Old=[]},
2117 since one @nt{default_expression} cannot depend on the value of
2118 another one.
2119 @Chg{Version=[2],New=[They],Old=[It]} also prevent circularities.
2120 @end{Reason}
2121 @begin{Ramification}
2122 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0295-1]}
2123 Inherited discriminants are not allowed to be denoted,
2124 except within @Chg{Version=[3],New=[@nt{aspect_specification}s
2125 and ],Old=[]}representation items.
2126 However, the @SynI{discriminant_}@nt{selector_name} of
2127 the parent @nt{subtype_indication} is allowed to denote
2128 a discriminant of the parent.
2129 @end{Ramification}
2130 @end{Itemize}
2131
2132 If the name of the current instance of a type
2133 (see @RefSecNum{The Context of Overload Resolution})
2134 is used to define the constraint of a component,
2135 then it shall appear as a @nt{direct_name}
2136 that is the @nt<prefix> of an @nt{attribute_reference}
2137 whose result is of an access type,
2138 and the @nt{attribute_reference} shall appear alone.
2139 @begin{Reason}
2140 This rule allows T'Access
2141 or T'Unchecked_Access, but disallows, for example,
2142 a range constraint (1..T'Size).
2143 Allowing things like (1..T'Size) would mean that a per-object
2144 constraint could affect the size of the object,
2145 which would be bad.
2146 @end{Reason}
2147
2148 @end{Legality}
2149
2150 @begin{StaticSem}
2151 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00318-02]}
2152 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0004-1]}
2153 @ChgAdded{Version=[2],Text=[@Defn{explicitly limited record}
2154 @Defn2{Term=[record],Sec=(explicitly limited)}
2155 If a @Chg{Version=[3],New=[@nt{record_type_definition}],Old=[@ntf{record_type_declaration}]}
2156 includes the reserved word @key{limited}, the type is called an
2157 @i<explicitly limited record> type.]}
2158
2159 @PDefn2{Term=[nominal subtype], Sec=(of a record component)}
2160 The @nt{component_definition} of a @nt<component_declaration>
2161 defines the (nominal) subtype of the component.
2162 If the reserved word @key(aliased) appears in the @nt{component_definition},
2163 then the component is aliased (see @RefSecNum{Access Types}).
2164 @begin(Ramification)
2165 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00363-01]}
2166 @ChgDeleted{Version=[2],Text=[In this case, the nominal subtype cannot be
2167 an unconstrained discriminated subtype.
2168 See @RefSecNum{Array Types}.]}
2169 @end(Ramification)
2170
2171 @Defn{null record}
2172 If the @nt<component_list> of a record type is defined by the reserved
2173 word @key(null) and there are no discriminants, then the record type has
2174 no components and all records of the type are @i(null records).
2175 A @nt<record_definition> of @key{null record} is equivalent to
2176 @key{record null; end record}.
2177 @begin{Ramification}
2178
2179 This short-hand is available both for declaring a record type
2180 and a record extension @em see @RefSecNum(Type Extensions).
2181 @end{ramification}
2182
2183 @end{StaticSem}
2184
2185 @begin{RunTime}
2186 @PDefn2{Term=[elaboration], Sec=(record_type_definition)}
2187 The elaboration of a @nt{record_type_definition} creates
2188 the record type and its first subtype,
2189 and consists of the elaboration of the @nt<record_definition>.
2190 @PDefn2{Term=[elaboration], Sec=(record_definition)}
2191 The elaboration of a @nt<record_definition> consists of the
2192 elaboration of its @nt{component_list}, if any.
2193
2194 @PDefn2{Term=[elaboration], Sec=(component_list)}
2195 The elaboration of a @nt{component_list} consists of the elaboration
2196 of the @nt{component_item}s and
2197 @nt{variant_part}, if any, in the order in which they appear.
2198 @PDefn2{Term=[elaboration], Sec=(component_declaration)}
2199 The elaboration of a @nt{component_declaration} consists of the
2200 elaboration of the @nt{component_definition}.
2201 @begin(Discussion)
2202 If the @nt<defining_identifier_list> has more than one
2203 @nt<defining_identifier>, we presume here that the transformation
2204 explained in @RefSecNum(Object Declarations) has already
2205 taken place. Alternatively, we could say that
2206 the @nt<component_definition> is elaborated once for
2207 each @nt<defining_identifier> in the list.
2208 @end(Discussion)
2209
2210 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0002],ARef=[AI95-00171-01]}
2211 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
2212 @Defn{per-object expression}
2213 @Defn{per-object constraint}
2214 @Defn{entry index subtype}
2215 Within the definition of a composite type,
2216 if a @nt<component_definition> or @nt<discrete_subtype_definition>
2217 (see @RefSecNum{Entries and Accept Statements})
2218 includes a @nt<name> that denotes a discriminant of the type, or
2219 that is an @nt<attribute_reference> whose @nt<prefix> denotes the current
2220 instance of the type,
2221 the expression containing the @nt<name> is called a @i(per-object expression),
2222 and the @Chg{New=[@nt{constraint} or @nt{range}],Old=[constraint]} being defined
2223 is called a @i(per-object constraint).
2224 @PDefn2{Term=[elaboration], Sec=(component_definition)}
2225 For the elaboration of a @nt{component_definition} of
2226 a @nt<component_declaration>@Chg{New=[ or the @nt{discrete_@!subtype_@!definition}
2227 of an @nt{entry_@!declaration} for an entry family (see
2228 @RefSecNum{Entries and Accept Statements})],Old=[]},@Chg{Version=[2],
2229 New=[ if the component subtype is defined by an @nt{access_definition} or],
2230 Old=[]} if the @nt{constraint}
2231 @Chg{New=[or @nt{range}],Old=[]} of the @nt{subtype_indication}
2232 @Chg{New=[or @nt{discrete_@!subtype_@!definition}],Old=[]} is not a per-object
2233 constraint, then the@Chg{Version=[2],New=[ @nt{access_definition},],Old=[]}
2234 @nt{subtype_indication}@Chg{Version=[2],New=[,],Old=[]}
2235 @Chg{New=[or @nt{discrete_@!subtype_@!definition}],Old=[]}
2236 is elaborated. On the other hand, if the @nt{constraint}
2237 @Chg{New=[or @nt{range}],Old=[]} is a per-object constraint,
2238 then the elaboration consists of the evaluation of any included
2239 expression that is not part of a per-object expression.
2240 @Chg{New=[Each such expression is evaluated once unless it is part of a named
2241 association in a discriminant constraint, in which case it is evaluated once
2242 for each associated discriminant.],Old=[]}
2243
2244 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0002],ARef=[AI95-00171-01]}
2245 @Chg{New=[@PDefn2{Term=[Elaboration],Sec=(per-object constraint)}When a
2246 per-object constraint is elaborated @Redundant[(as part of creating an
2247 object)], each per-object expression of the constraint is evaluated. For other
2248 expressions, the values determined during the elaboration of the
2249 @nt{component_@!definition} or @nt{entry_@!declaration} are used. Any checks
2250 associated with the enclosing @nt{subtype_indication} or
2251 @nt{discrete_subtype_definition} are performed@Redundant[, including the subtype
2252 compatibility check (see @RefSecNum{Subtype Declarations}),] and the associated
2253 subtype is created.],
2254 Old=[]}
2255 @begin(Discussion)
2256 The evaluation of other expressions that appear in
2257 @nt<component_definition>s and @nt<discrete_subtype_definition>s
2258 is performed when the type definition is elaborated.
2259 The evaluation of expressions that appear as
2260 @nt<default_expression>s is postponed until an object is created.
2261 Expressions in representation items
2262 that appear within a composite type definition are evaluated
2263 according to the rules of the particular representation item.
2264 @end(Discussion)
2265 @end{RunTime}
2266
2267 @begin{Notes}
2268 A @nt<component_declaration> with several identifiers is equivalent
2269 to a sequence of single @nt<component_declaration>s, as explained
2270 in @RefSecNum{Object Declarations}.
2271
2272 The @nt<default_expression> of a record component is only
2273 evaluated upon the creation of
2274 a default-initialized object of the record type (presuming
2275 the object has the component, if it is in a @nt<variant_part> @em
2276 see @RefSecNum{Object Declarations}).
2277
2278 The subtype defined by a @nt<component_definition> (see @RefSecNum(Array Types))
2279 has to be a definite subtype.
2280
2281 If a record type does not have a @nt<variant_part>, then the same components
2282 are present in all values of the type.
2283
2284 A record type is limited if it has the reserved word @key[limited]
2285 in its definition, or if any of its components are
2286 limited (see @RefSecNum{Limited Types}).
2287
2288 @PDefn2{Term=[predefined operations],Sec=(of a record type)}
2289 The predefined operations of a record type include membership
2290 tests, qualification, and explicit conversion. If the
2291 record type is nonlimited, they also include
2292 assignment and the predefined equality operators.
2293
2294 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
2295 A component of a record can be named with a @nt<selected_component>.
2296 A value of a record can be specified with a @nt<record_aggregate>@Chg{Version=[2],
2297 New=[],Old=[, unless the record type is limited]}.
2298
2299 @end{Notes}
2300
2301 @begin{Examples}
2302 @Leading@keepnext@i(Examples of record type declarations: )
2303 @begin(Example)
2304 @key(type) Date @key(is)
2305 @key(record)
2306 Day : Integer @key(range) 1 .. 31;
2307 Month : Month_Name;
2308 Year : Integer @key(range) 0 .. 4000;
2309 @key(end) @key(record);
2310
2311 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0213-1]}
2312 @key(type) Complex @key(is)
2313 @key(record)
2314 Re : Real := 0.0;
2315 Im : Real := 0.0;
2316 @key(end) @key(record)@Chg{Version=[5],New=[ Complex],Old=[]};
2317 @end(Example)
2318
2319 @begin{WideAbove}
2320 @leading@keepnext@i(Examples of record variables: )
2321 @end{WideAbove}
2322 @begin(Example)
2323 Tomorrow, Yesterday : Date;
2324 A, B, C : Complex;
2325
2326 --@RI[ both components of A, B, and C are implicitly initialized to zero ]
2327 @end(Example)
2328 @end{Examples}
2329
2330 @begin{Extend83}
2331 @Defn{extensions to Ada 83}
2332 The syntax rule for @nt<component_declaration> is modified
2333 to use @nt<component_definition> (instead of
2334 @ntf<component_subtype_definition>). The effect of this change
2335 is to allow the reserved word @key(aliased) before the
2336 @ntf<component_subtype_definition>.
2337
2338 A short-hand is provided for defining a null record type
2339 (and a null record extension), as these will be more common
2340 for abstract root types (and derived types without additional components).
2341
2342 The syntax rule for @nt{record_type_definition} is modified to allow
2343 the reserved words @key{tagged} and @key{limited}.
2344 Tagging is new.
2345 Limitedness is now orthogonal to privateness.
2346 In Ada 83 the syntax implied that limited private was sort of more
2347 private than private.
2348 However, limitedness really has nothing to do with privateness;
2349 limitedness simply indicates the lack of assignment capabilities,
2350 and makes perfect sense for nonprivate types such as record types.
2351 @end{Extend83}
2352
2353 @begin{DiffWord83}
2354 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0009],ARef=[AI95-00137-01]}
2355 The syntax rules now allow @Chg{New=[@nt{aspect_clause}s],
2356 Old=[@nt{representation_clause}s]} to appear in a @nt{record_definition}.
2357 This is not a language extension, because @LegalityName@;s prevent all
2358 language-defined representation clauses from appearing there.
2359 However, an implementation-defined @nt{attribute_definition_clause}
2360 could appear there.
2361 The reason for this change is to allow the rules for
2362 @Chg{New=[@nt{aspect_clause}s],Old=[@nt{representation_clause}s]} and
2363 representation pragmas to be as similar as possible.
2364 @end{DiffWord83}
2365
2366 @begin{Extend95}
2367 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
2368 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2369 Record components can have an anonymous access type.]}
2370
2371 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
2372 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2373 Limited components can be initialized, so long as the expression is
2374 one that allows building the object in place (such as an @nt{aggregate} or
2375 @nt{function_call}).]}
2376 @end{Extend95}
2377
2378 @begin{DiffWord95}
2379 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0002],ARef=[AI95-00171-01]}
2380 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Improved the description of the
2381 elaboration of per-object constraints.]}
2382
2383 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0009],ARef=[AI95-00137-01]}
2384 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Changed representation clauses to
2385 aspect clauses to reflect that they are used for more than just
2386 representation.]}
2387
2388 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
2389 @ChgAdded{Version=[2],Text=[Defined @i{explicitly limited record} type to
2390 use in other rules.]}
2391 @end{DiffWord95}
2392
2393 @begin{Extend2005}
2394 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
2395 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
2396 An optional @nt{aspect_specification} can be used in a @nt{component_declaration}.
2397 This is described in @RefSecNum{Aspect Specifications}.]}
2398 @end{Extend2005}
2399
2400 @begin{Extend2012}
2401 @ChgRef{Version=[5],Kind=[AddedNormal],ARef=[AI12-0213-1]}
2402 @ChgAdded{Version=[5],Text=[@Defn{extensions to Ada 2012}The
2403 @SynI{record_}@nt{identifier} following @key[end record] is new.]}
2404 @end{Extend2012}
2405
2406
2407 @LabeledSubClause{Variant Parts and Discrete Choices}
2408
2409 @begin{Intro}
2410 A record type with a @nt<variant_part> specifies alternative
2411 lists of components. Each @nt<variant> defines the components
2412 for the value or values of the discriminant
2413 covered by its @nt<discrete_choice_list>.
2414 @begin{Discussion}
2415 @RootDefn{cover a value}
2416 @nt{Discrete_choice_list}s and @nt{discrete_choice}s are
2417 said to @i(cover) values as defined below; which
2418 @nt{discrete_choice_list} covers a value determines
2419 which of various alternatives is chosen. These are used
2420 in @nt{variant_part}s, @nt{array_aggregate}s, and
2421 @nt{case_statement}s.
2422 @end{Discussion}
2423 @end{Intro}
2424
2425 @begin{MetaRules}
2426 The definition of @lquotes@;cover@rquotes@; in this subclause
2427 and the rules about discrete choices
2428 are designed so that they
2429 are also appropriate for array aggregates and case statements.
2430
2431 The rules of this subclause intentionally
2432 parallel those for case statements.
2433 @end{MetaRules}
2434
2435 @begin{Syntax}
2436 @Syn{lhs=<variant_part>,rhs="
2437 @key{case} @SynI{discriminant_}@Syn2{direct_name} @key{is}
2438 @Syn2{variant}
2439 {@Syn2{variant}}
2440 @key{end} @key{case};"}
2441
2442
2443 @Syn{lhs=<variant>,rhs="
2444 @key{when} @Syn2{discrete_choice_list} =>
2445 @Syn2{component_list}"}
2446
2447
2448 @ChgRef{Version=[5],Kind=[Revised],ARef=[AI12-0212-1]}
2449 @Syn{lhs=<discrete_choice_list>,rhs="@Syn2{discrete_choice} {@Chg{Version=[5],New=['|'],Old=[|]} @Syn2{discrete_choice}}"}
2450
2451 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3],ARef=[AI05-0158-1]}
2452 @Syn{lhs=<discrete_choice>,rhs="@Chg{Version=[3],New=[@Syn2{choice_expression}],Old=[@Syn2{expression}]} | @Chg{Version=[3],New=[@SynI{discrete_}@Syn2{subtype_indication} | @Syn2{range}],Old=[@Syn2{discrete_range}]} | @key{others}"}
2453 @end{Syntax}
2454
2455 @begin{Resolution}
2456 @Defn2{Term=[discriminant], Sec=(of a @nt<variant_part>)}
2457 The @i(discriminant_)@nt{direct_name} shall resolve to denote
2458 a discriminant (called
2459 the @i(discriminant of the @nt<variant_part>)) specified in
2460 the @nt{known_discriminant_part} of the @nt{full_type_declaration}
2461 that contains the @nt{variant_part}.
2462 @PDefn2{Term=[expected type], Sec=(variant_part discrete_choice)}
2463 The expected type for each @nt{discrete_choice} in a @nt<variant> is the type
2464 of the discriminant of the @nt{variant_part}.
2465 @begin{Ramification}
2466 A @nt<full_type_declaration> with a @nt<variant_part>
2467 has to have a (new) @nt<known_discriminant_part>;
2468 the discriminant of the @nt<variant_part> cannot be an
2469 inherited discriminant.
2470 @end{Ramification}
2471 @end{Resolution}
2472
2473 @begin{Legality}
2474 The discriminant of the @nt{variant_part} shall
2475 be of a discrete type.
2476 @begin{Ramification}
2477 It shall not be of an access type,
2478 named or anonymous.@end{ramification}
2479
2480 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3]}
2481 The @Chg{Version=[3],New=[@nt{choice_expression}s, @nt{subtype_indication}s,
2482 and @nt{range}s],Old=[@nt{expression}s and @nt{discrete_range}s]} given as
2483 @nt{discrete_choice}s in a @nt{variant_part} shall be static.
2484 The @nt{discrete_choice} @key(others) shall appear alone
2485 in a @nt{discrete_choice_list}, and such a @nt{discrete_choice_list},
2486 if it appears, shall be the last one in the enclosing construct.
2487
2488 @Leading@PDefn2{Term=[cover a value], Sec=(by a @nt{discrete_choice})}
2489 A @nt<discrete_choice> is defined to @i(cover a value) in the
2490 following cases:
2491 @begin(itemize)
2492 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0262-1]}
2493 A @nt{discrete_choice} that is
2494 @Chg{Version=[3],New=[a @nt{choice_expression}],Old=[an @nt{expression}]}
2495 covers a value if
2496 the value equals the value of
2497 the @Chg{Version=[3],New=[@nt{choice_expression}],Old=[@nt{expression}]}
2498 converted to the expected type.
2499
2500 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0153-3],ARef=[AI05-0262-1]}
2501 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0071-1]}
2502 @ChgAdded{Version=[3],Text=[A @nt{discrete_choice} that is a
2503 @nt{subtype_indication} covers all values (possibly none) that belong to the
2504 subtype and that satisfy the static @Chg{Version=[4],New=[predicates],
2505 Old=[predicate]} of the subtype (see @RefSecnum{Subtype Predicates}).]}
2506
2507 @begin{Ramification}
2508 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0262-1]}
2509 @ChgAdded{Version=[3],Text=[A dynamic predicate is never allowed in this case
2510 (for @nt{variant}s, @nt{case_statement}s, and @nt{case_expression}s,
2511 a subtype with a dynamic
2512 predicate isn't static and thus isn't allowed in a @nt{discrete_choice},
2513 and for a choice in an @nt{array_aggregate}, a dynamic predicate
2514 is explicitly disallowed @em see @RefSecnum{Subtype Predicates}).]}
2515 @end{Ramification}
2516
2517 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3]}
2518 A @nt{discrete_choice} that is a
2519 @Chg{Version=[3],New=[@nt{range}],Old=[@nt{discrete_range}]} covers all values
2520 (possibly none) that belong to the range.
2521
2522 The @nt{discrete_choice} @key{others} covers all values of its
2523 expected type that are not covered by
2524 previous @nt{discrete_choice_list}s of the same construct.
2525 @begin(Ramification)
2526 For @nt{case_statement}s,
2527 this includes values outside the range of the static subtype (if any)
2528 to be covered by the choices.
2529 It even includes values outside the base
2530 range of the case expression's type,
2531 since values of numeric types (and undefined values of any scalar type?)
2532 can be outside their base range.
2533 @end(Ramification)
2534 @end(itemize)
2535
2536 @PDefn2{Term=[cover a value], Sec=(by a @nt{discrete_choice_list})}
2537 A @nt{discrete_choice_list} covers a value if one of
2538 its @nt{discrete_choice}s covers the value.
2539
2540 @Leading@keepnext@;The possible values of the discriminant of a @nt{variant_part}
2541 shall be covered as follows:
2542 @begin{itemize}
2543 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3],ARef=[AI05-0188-1],ARef=[AI05-0262-1]}
2544 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0071-1]}
2545 If the discriminant is of a static constrained scalar
2546 subtype@Chg{Version=[3],New=[],Old=[,]}
2547 then@Chg{Version=[3],New=[, except within an instance of a generic
2548 unit,],Old=[]} each non-@key{others} @nt{discrete_@!choice} shall cover
2549 only values in that subtype@Chg{Version=[3],New=[ that satisfy its
2550 @Chg{Version=[4],New=[predicates],Old=[predicate]}],Old=[]}, and each value
2551 of that subtype @Chg{Version=[3],New=[that satisfies its
2552 @Chg{Version=[4],New=[predicates],Old=[predicate]} ],Old=[]}shall be covered
2553 by some @nt{discrete_@!choice} @Redundant[(either explicitly or
2554 by @key<others>)];
2555
2556 @begin{Reason}
2557 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0188-1]}
2558 @ChgAdded{Version=[3],Type=[Leading],Text=[The exemption for a discriminated type declared in an instance
2559 allows the following example:]}
2560 @begin{Example}
2561 @ChgRef{Version=[3],Kind=[AddedNormal]}
2562 @ChgAdded{Version=[3],Text=[@key[generic]
2563 @key[type] T @key[is new] Integer;
2564 @key[package] G @key[is]
2565 @key[type] Rec (Discrim : T) @key[is record]
2566 @key[case] Discrim @key[is]
2567 @key[when] -10 .. -1 =>
2568 Foo : Float;
2569 @key[when others] =>
2570 @key[null];
2571 @key[end case];
2572 @key[end record];
2573 @key[end] G;]}
2574
2575 @ChgRef{Version=[3],Kind=[AddedNormal]}
2576 @ChgAdded{Version=[3],Text=[@key{package} I @key{is new} G (Natural); -- @Examcom{Legal}]}
2577 @end{Example}
2578 @end{Reason}
2579 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0264-1]}
2580 If the type of the discriminant is a
2581 descendant of a generic formal scalar type@Chg{Version=[3],New=[,],Old=[]}
2582 then the @nt{variant_part} shall have an @key{others}
2583 @nt{discrete_choice};
2584 @begin{Reason}
2585 The base range is not known statically in this case.
2586 @end{Reason}
2587
2588 Otherwise,
2589 each value of the base range of the type of the discriminant shall
2590 be covered @Redundant[(either explicitly or by @key<others>)].
2591 @end{itemize}
2592
2593 Two distinct @nt{discrete_choice}s of a @nt{variant_part} shall not cover
2594 the same value.
2595
2596 @end{Legality}
2597
2598 @begin{StaticSem}
2599 If the @nt{component_list} of a @nt{variant} is specified by @key(null),
2600 the variant has no components.
2601
2602 @Defn{govern a @nt{variant_part}}
2603 @Defn{govern a @nt{variant}}
2604 The discriminant of a @nt<variant_part> is said to @i(govern) the
2605 @nt<variant_part> and its @nt<variant>s. In addition,
2606 the discriminant of a derived
2607 type governs a @nt<variant_part> and its @nt<variant>s if it
2608 corresponds (see @RefSecNum(Discriminants)) to the
2609 discriminant of the @nt<variant_part>.
2610
2611 @end{StaticSem}
2612
2613 @begin{RunTime}
2614 A record value contains the values of the components of a particular
2615 @nt{variant} only if the
2616 value of the discriminant governing the @nt<variant> is covered
2617 by the @nt{discrete_choice_list} of the @nt{variant}.
2618 This rule applies in turn to any further @nt{variant} that is, itself,
2619 included in the @nt{component_list} of the given @nt{variant}.
2620
2621 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0290-1]}
2622 @ChgAdded{Version=[3],Text=[When an object of a discriminated type @i<T> is
2623 initialized by default, Constraint_Error is raised if no
2624 @nt{discrete_choice_list} of any @nt{variant} of a @nt{variant_part} of @i<T>
2625 covers the value of the discriminant that governs the @nt{variant_part}. When a
2626 @nt{variant_part} appears in the @nt{component_list} of another @nt{variant}
2627 @i<V>, this test is only applied if the value of the discriminant governing
2628 @i<V> is covered by the @nt{discrete_choice_list}
2629 of @i<V>.@Defn2{Term=[Constraint_Error],Sec=(raised by object of discriminated type)}]}
2630 @begin{ImplNote}
2631 @ChgRef{Version=[3],Kind=[AddedNormal]}
2632 @ChgAdded{Version=[3],Text=[This is not a @ldquote@;check@rdquote; it cannot
2633 be suppressed. However, in most cases it is not necessary to generate any code
2634 to raise this exception. A test is needed (and can fail) in the case where the
2635 discriminant subtype has a Static_Predicate specified, it also has predicate
2636 checking disabled, and the discriminant governs a @nt{variant_part} which
2637 lacks a @key[when others] choice.]}
2638
2639 @ChgRef{Version=[3],Kind=[AddedNormal]}
2640 @ChgAdded{Version=[3],Text=[The test also could fail for a static discriminant
2641 subtype with range checking suppressed and the discriminant governs a
2642 @nt{variant_part} which lacks a @key[when others] choice. But execution is erroneous if a range
2643 check that would have failed is suppressed (see @RefSecNum{Suppressing Checks}),
2644 so an implementation does not have to generate code to check this case. (An
2645 unchecked failed predicate does not cause erroneous execution, so the test is
2646 required in that case.)]}
2647
2648 @ChgRef{Version=[3],Kind=[AddedNormal]}
2649 @ChgAdded{Version=[3],Text=[Like the checks associated with a per-object
2650 constraint, this test is not made during the elaboration of a
2651 @nt{subtype_indication}.]}
2652 @end{ImplNote}
2653
2654 @PDefn2{Term=[elaboration], Sec=(variant_part)}
2655 The elaboration of a @nt{variant_part} consists
2656 of the elaboration of the @nt{component_list} of each
2657 @nt{variant} in the order in which they appear.
2658 @end{RunTime}
2659
2660 @begin{Examples}
2661 @Leading@keepnext@i(Example of record type with a variant part: )
2662 @begin(Example)
2663 @key(type) Device @key(is) (Printer, Disk, Drum);
2664 @key(type) State @key(is) (Open, Closed);
2665
2666 @key(type) Peripheral(Unit : Device := Disk) @key(is)
2667 @key(record)
2668 Status : State;
2669 @key(case) Unit @key(is)
2670 @key(when) Printer =>
2671 Line_Count : Integer @key(range) 1 .. Page_Size;
2672 @key(when) @key(others) =>
2673 Cylinder : Cylinder_Index;
2674 Track : Track_Number;
2675 @key(end) @key(case);
2676 @key(end) @key(record);
2677 @end(Example)
2678
2679 @begin{WideAbove}
2680 @leading@keepnext@i(Examples of record subtypes:)
2681 @end{WideAbove}
2682 @begin(Example)
2683 @key(subtype) Drum_Unit @key(is) Peripheral(Drum);
2684 @key(subtype) Disk_Unit @key(is) Peripheral(Disk);
2685 @end(Example)
2686
2687 @begin{WideAbove}
2688 @leading@keepnext@i(Examples of constrained record variables:)
2689 @end{WideAbove}
2690 @begin(Example)
2691 Writer : Peripheral(Unit => Printer);
2692 Archive : Disk_Unit;
2693 @end(Example)
2694 @end{Examples}
2695
2696 @begin{Extend83}
2697 @Defn{extensions to Ada 83}
2698 In Ada 83, the discriminant of a @nt{variant_part} is not allowed to
2699 be of a generic formal type.
2700 This restriction is removed in Ada 95; an @key{others} @nt{discrete_choice}
2701 is required in this case.
2702 @end{Extend83}
2703
2704 @begin{DiffWord83}
2705 The syntactic category @ntf{choice} is removed.
2706 The syntax rules for @nt{variant}, @nt{array_aggregate}, and
2707 @nt{case_statement} now use @nt{discrete_choice_list}
2708 or @nt{discrete_choice} instead.
2709 The syntax rule for @nt{record_aggregate} now defines its own syntax
2710 for named associations.
2711
2712 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2713 We have added the term Discrete Choice to the title
2714 since this is where they are talked about.
2715 This is analogous to the name of the subclause
2716 "Index Constraints and Discrete Ranges" in
2717 the @Chg{Version=[3],New=[subclause],Old=[clause]} on Array Types.
2718
2719 The rule requiring that the discriminant denote
2720 a discriminant of the type being defined seems to have been
2721 left implicit in RM83.
2722 @end{DiffWord83}
2723
2724 @begin{Incompatible2005}
2725 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0158-1]}
2726 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}
2727 Membership tests are no longer allowed as a @nt{discrete_choice}, in
2728 order that those tests can be expanded to allow multiple tests in a
2729 single expression without ambiguity. Since a membership test has a
2730 boolean type, they are very unlikely to be used as a @nt{discrete_choice}.]}
2731 @end{Incompatible2005}
2732
2733 @begin{Extend2005}
2734 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0153-3]}
2735 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
2736 Subtypes with static predicates can be used in @nt{discrete_choice}s,
2737 and the coverage rules are modified to respect the predicates.]}
2738
2739 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0188-1]}
2740 @ChgAdded{Version=[3],Text=[Variants in generic specifications are no
2741 longer rejected if the subtype
2742 of the actual type does not include all of the case choices. This probably
2743 isn't useful, but it is consistent with the treatment of @nt{case_expression}s.]}
2744 @end{Extend2005}
2745
2746 @begin{DiffWord2005}
2747 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
2748 @ChgAdded{Version=[3],Text=[Added a test that some @nt{variant} covers the
2749 value of a discriminant that governs a @nt{variant_part}. This is similar
2750 to the test that some case limb covers the value of the
2751 @SynI{Selecting_}@nt{expression} of a @nt{case_statement}. This test cannot
2752 change the behavior of any nonerroneous Ada 2005 program, so it is not
2753 an inconsistency.]}
2754 @end{DiffWord2005}
2755
2756 @begin{DiffWord2012}
2757 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0071-1]}
2758 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Updated wording of the
2759 coverage rules to use the new term "satisfies the predicates"
2760 (see @RefSecNum{Subtype Predicates}).]}
2761 @end{DiffWord2012}
2762