Debian Codegraph @

source_2012/03b.mss

raw
1 @Part(03, Root="ada.mss")
2
3 @Comment{$Date: 2014/07/24 04:20:38 $}
4
5 @Comment{$Source: e:\\cvsroot/ARM/Source/03b.mss,v $}
6 @Comment{$Revision: 1.103 $}
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 @Syn{lhs=<discriminant_association>,rhs="
1628 [@SynI{discriminant_}@Syn2{selector_name} {| @SynI{discriminant_}@Syn2{selector_name}} =>] @Syn2{expression}"}
1629
1630 @begin{SyntaxText}
1631 @Defn{named discriminant association}
1632 A @nt<discriminant_association>
1633 is said to be @i(named) if it has one or more
1634 @i(discriminant_)@nt<selector_name>s;
1635 @Defn{positional discriminant association}
1636 it is otherwise said to be @i(positional).
1637 In a @nt<discriminant_constraint>,
1638 any positional associations shall precede any named
1639 associations.
1640 @end{SyntaxText}
1641
1642 @end{Syntax}
1643
1644 @begin{Resolution}
1645 Each @nt<selector_name> of a named @nt<discriminant_@!association> shall
1646 resolve to denote a discriminant of the subtype being constrained;
1647 @Defn2{Term=[associated discriminants],
1648 Sec=(of a named @nt<discriminant_association>)}
1649 the discriminants so named are the @i(associated discriminants) of
1650 the named association.
1651 @Defn2{Term=[associated discriminants],
1652 Sec=(of a positional @nt<discriminant_association>)}
1653 For a positional association, the @i(associated discriminant)
1654 is the one whose @nt<discriminant_@!specification> occurred in
1655 the corresponding position in the @nt<known_@!discriminant_@!part>
1656 that defined the discriminants of the subtype being constrained.
1657
1658 @PDefn2{Term=[expected type], Sec=(discriminant_association expression)}
1659 The expected type for the @nt{expression} in
1660 a @nt{discriminant_association}
1661 is that of the associated discriminant(s).
1662 @end{Resolution}
1663
1664 @begin{Legality}
1665 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0008],ARef=[AI95-00168-01]}
1666 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00363-01]}
1667 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0041-1]}
1668 A @nt{discriminant_constraint} is only allowed in a
1669 @nt{subtype_indication} whose @nt{subtype_mark} denotes
1670 either an unconstrained discriminated subtype, or an
1671 unconstrained access subtype whose designated subtype is
1672 an unconstrained discriminated subtype.
1673 @Chg{New=[However, in the case of @Chg{Version=[2],New=[an],Old=[a general]}
1674 access subtype, a @nt{discriminant_@!constraint} is
1675 @Chg{Version=[3],New=[legal only if any dereference of a value of the access
1676 type is known to be constrained (see @RefSecNum{Objects and Named Numbers})],
1677 Old=[illegal if @Chg{Version=[2],New=[the
1678 designated type has a partial view that is constrained or, for a general
1679 access subtype, has @nt{default_expression}s for its discriminants],Old=[]}]}],
1680 Old=[there is a place within the
1681 immediate scope of the designated subtype where the designated subtype's view
1682 is constrained]}.
1683 @Chg{Version=[2],New=[In addition to the places where @LegalityTitle@;
1684 normally apply (see @RefSecNum{Generic Instantiation}),
1685 these rules apply also in the private part of an instance
1686 of a generic unit.@PDefn{generic contract issue}],Old=[]}@Chg{Version=[3],
1687 New=[],Old=[@Chg{Version=[2],
1688 New=[ In a generic body, this rule is checked presuming all
1689 formal access types of the generic might be general access types, and all
1690 untagged discriminated formal types of the generic might have
1691 @nt{default_expression}s for their discriminants.],Old=[]}]}
1692
1693 @begin{Reason}
1694 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0008],ARef=[AI95-00168-01]}
1695 @ChgRef{Version=[2],Kind=[DeletedAdded],ARef=[AI95-00363-01]}
1696 @ChgDeleted{Version=[2],Text=[@Chg{New=[The second rule is necessary to
1697 prevent assignments that change the discriminant of a constrained object.
1698 See the defect report for examples.],Old=[]}]}
1699 @end{Reason}
1700
1701 @begin{Reason}
1702 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00363-01]}
1703 @ChgAdded{Version=[2],Text=[The second rule is necessary to prevent objects
1704 from changing so that they no longer match their constraint. In Ada 95, we
1705 attempted to prevent this by banning every case where an aliased object
1706 could be unconstrained or be changed by an enclosing assignment. New ways
1707 to cause this problem were being discovered frequently, meaning that new rules
1708 had to be dreamed up to cover them. Meanwhile, aliased objects and components
1709 were getting more and more limited. In Ada 2005, we sweep away all of that
1710 cruft and replace it by a simple rule @lquotes@;thou shalt not create an
1711 access subtype that can point to an item whose discriminants can be changed by
1712 assignment@rquotes@;.]}
1713 @end{Reason}
1714
1715 @begin{Discussion}
1716 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0041-1]}
1717 @ChgAdded{Version=[3],Text=[The second rule will only use the indefinite or
1718 dereference bullets in the definition of @ldquote@;known to be constrained@rdquote.
1719 The rule is worded in terms of @ldquote@;known to be constrained@rdquote in
1720 order to capture the special rules that apply in generic bodies (rather than
1721 repeating them and getting them subtly wrong).]}
1722 @end{Discussion}
1723
1724 A named @nt<discriminant_association> with more than one
1725 @nt<selector_name> is allowed only if the named discriminants
1726 are all of the same type.
1727 A @nt<discriminant_constraint> shall provide exactly one value for each
1728 discriminant of the subtype being constrained.
1729
1730 @ChgRef{Version=[3],Kind=[Deleted],ARef=[AI05-0102-1]}
1731 @ChgDeleted{Version=[3],Text=[The @nt<expression> associated with an access
1732 discriminant shall be of a type convertible to the anonymous access type.
1733 @PDefn2{Term=[convertible],Sec=(required)}]}
1734
1735 @begin(Ramification)
1736
1737 @ChgRef{Version=[3],Kind=[Revised]}
1738 @Chg{Version=[3],New=[In addition, @RefSecNum{The Context of Overload Resolution}
1739 requires that the @nt{expression} associated with an access discriminant
1740 is convertible (see @RefSecNum{Type Conversions}) to the anonymous
1741 access type. ],Old=[]}This implies both convertibility of designated
1742 types, and static accessibility.
1743 This implies that if an object of type T with an
1744 access discriminant is created
1745 by an allocator for an access type A, then it requires that the
1746 type of the @nt<expression> associated
1747 with the access discriminant have an accessibility level
1748 that is not statically deeper than that of A.
1749 This is to avoid dangling references.
1750
1751 @end(Ramification)
1752 @end{Legality}
1753
1754 @begin{RunTime}
1755 @PDefn2{Term=[compatibility], Sec=(discriminant constraint with a
1756 subtype)}
1757 A @nt{discriminant_constraint} is @i(compatible) with an unconstrained
1758 discriminated subtype if each discriminant value belongs to the
1759 subtype of the corresponding discriminant.
1760 @begin{Ramification}
1761 The "dependent
1762 compatibility check" has been eliminated in Ada 95. Any checking
1763 on subcomponents is performed when (and if) an object is created.@end{ramification}
1764 @begin{Discussion}
1765 There is no need to define compatibility with a constrained
1766 discriminated subtype, because one is not allowed to constrain it again.@end{discussion}
1767
1768 @PDefn2{Term=[satisfies], Sec=(a discriminant constraint)}
1769 A composite value @i(satisfies) a discriminant constraint if and only
1770 if each discriminant of the composite value has the value imposed
1771 by the discriminant constraint.
1772
1773 @PDefn2{Term=[elaboration], Sec=(discriminant_constraint)}
1774 For the elaboration of a @nt{discriminant_constraint}, the @nt{expression}s
1775 in the @nt{discriminant_association}s are evaluated in an arbitrary
1776 order and converted to the type of the associated
1777 discriminant (which might raise Constraint_Error @em
1778 see @RefSecNum{Type Conversions});
1779 the @nt{expression} of a named association is evaluated
1780 (and converted) once for each associated discriminant.
1781 @PDefn2{Term=[implicit subtype conversion],Sec=(discriminant values)}
1782 The result of each evaluation and conversion is the
1783 value imposed by the constraint for the associated discriminant.
1784 @begin{Reason}
1785
1786 We convert to the type, not the subtype, so that the definition
1787 of compatibility of discriminant constraints is not vacuous.@end{reason}
1788
1789 @end{RunTime}
1790
1791 @begin{Notes}
1792 The rules of the language ensure that
1793 a discriminant of an object always has a value, either
1794 from explicit or implicit initialization.
1795 @begin(Discussion)
1796 Although it is illegal to constrain a class-wide tagged subtype, it
1797 is possible to have a partially constrained class-wide
1798 subtype: If the subtype S is defined by T(A => B),
1799 then S'Class is partially constrained in the sense that objects of
1800 subtype S'Class have to have discriminants corresponding
1801 to A equal to B,
1802 but there can be other discriminants defined
1803 in extensions that are not constrained to any particular value.
1804 @end(Discussion)
1805 @end{Notes}
1806
1807 @begin{Examples}
1808 @Leading@keepnext@ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
1809 @i{Examples (using types declared above in
1810 @Chg{Version=[3],New=[subclause],Old=[clause]} @RefSecNum(Discriminants)):}
1811 @begin(Example)
1812 Large : Buffer(200); --@RI[ constrained, always 200 characters]
1813 --@RI[ (explicit discriminant value)]
1814 Message : Buffer; --@RI[ unconstrained, initially 100 characters]
1815 --@RI[ (default discriminant value)]
1816 Basis : Square(5); --@RI[ constrained, always 5 by 5]
1817 Illegal : Square; --@RI[ illegal, a Square has to be constrained]
1818 @end(Example)
1819 @end{Examples}
1820
1821 @begin{Inconsistent83}
1822 @Defn{inconsistencies with Ada 83}
1823 Dependent compatibility checks are no longer performed on
1824 subtype declaration. Instead they are deferred until
1825 object creation (see @RefSecNum(Object Declarations)).
1826 This is upward compatible for a program
1827 that does not raise Constraint_Error.
1828 @end{Inconsistent83}
1829
1830 @begin{DiffWord83}
1831 Everything in RM83-3.7.2(7-12), which specifies the
1832 initial values for discriminants, is now redundant
1833 with 3.3.1, 6.4.1, 8.5.1, and 12.4. Therefore, we don't
1834 repeat it here. Since the material is largely intuitive,
1835 but nevertheless complicated to state formally, it doesn't
1836 seem worth putting it in a "NOTE."
1837 @end{DiffWord83}
1838
1839 @begin{Incompatible95}
1840 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0008],ARef=[AI95-00168-01],ARef=[AI95-00363-01]}
1841 @ChgAdded{Version=[2],Text=[@Defn{incompatibilities with Ada 95}
1842 The Corrigendum added a restriction on @nt{discriminant_constraint}s for
1843 general access subtypes. Such constraints are prohibited
1844 if the designated type can be treated as constrained somewhere in the program.
1845 Ada 2005 goes further and prohibits such @nt{discriminant_constraint}s if
1846 the designated type has (or might have, in the case of a formal type)
1847 defaults for its discriminants. The use of general access subtypes is rare,
1848 and this eliminates a boatload of problems that required many restrictions
1849 on the use of aliased objects and components (now lifted). Similarly,
1850 Ada 2005 prohibits @nt{discriminant_constraint}s on any access type whose
1851 designated type has a partial view that is constrained. Such a type will
1852 not be constrained in the heap to avoid privacy problems. Again, the use
1853 of such subtypes is rare (they can only happen within the package and its
1854 child units).]}
1855 @end{Incompatible95}
1856
1857 @begin{Diffword2005}
1858 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0041-1]}
1859 @ChgAdded{Version=[3],Text=[@b<Correction:> Revised the rules on
1860 access subtypes having discriminant constraints to depend on the
1861 @ldquote@;known to be constrained@rdquote rules. This centralizes
1862 the rules so that future fixes need to be made in only one place,
1863 as well as fixing bugs in obscure cases.]}
1864
1865 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0102-1]}
1866 @ChgAdded{Version=[3],Text=[@b<Correction:> Moved implicit conversion
1867 @LegalityName to @RefSecNum{The Context of Overload Resolution}.]}
1868 @end{Diffword2005}
1869
1870
1871 @LabeledSubClause{Operations of Discriminated Types}
1872
1873 @begin{Intro}
1874 @Redundant[If a discriminated type has @nt<default_expression>s for its
1875 discriminants, then unconstrained variables of the type are permitted,
1876 and the discriminants of such a variable can be changed by assignment to
1877 the variable. For a formal parameter of such a type, an attribute
1878 is provided to determine whether the corresponding actual parameter
1879 is constrained or unconstrained.]
1880 @end{Intro}
1881
1882 @begin{StaticSem}
1883 @Leading@Keepnext@;For @PrefixType{a @nt<prefix> A that is of a
1884 discriminated type @Redundant[(after any implicit dereference)]},
1885 the following attribute is defined:
1886 @begin(description)
1887 @ChgAttribute{Version=[3], Kind=[Revised], ChginAnnex=[T],
1888 Leading=[F], Prefix=<A>, AttrName=<Constrained>, ARef=[AI05-0214-1],
1889 InitialVersion=[0],
1890 Text=[Yields the value True if A denotes a constant, a value,
1891 @Chg{Version=[3],New=[a tagged object, ],Old=[]}or a constrained variable,
1892 and False otherwise.]}
1893 @begin(ImplNote)
1894 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0214-1]}
1895 This attribute is primarily used on parameters, to determine whether
1896 the discriminants can be changed as part of an assignment.
1897 The Constrained attribute is statically True for @key(in) parameters.
1898 For @key(in out) and @key(out) parameters of a discriminated type,
1899 the value of this attribute needs to be passed as an implicit
1900 parameter, in general. However, if the type @Chg{Version=[3],New=[is
1901 tagged or ],Old=[]}does not have
1902 defaults for its discriminants, the attribute is statically True,
1903 so no implicit parameter is needed.
1904 Parameters of a limited @Chg{Version=[3],New=[untagged ],Old=[]}type
1905 with defaulted discriminants need this implicit parameter,
1906 unless there are no nonlimited views,
1907 because they might be passed to a subprogram whose body has
1908 visibility on a nonlimited view of the type, and hence might be
1909 able to assign to the object and change its discriminants.
1910 @end(ImplNote)
1911
1912 @begin{Reason}
1913 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0214-1]}
1914 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0005-1]}
1915 @ChgAdded{Version=[3],Text=[All tagged objects are known to be constrained (as
1916 nonlimited tagged types cannot have discriminant defaults, and limited tagged
1917 objects are immutably limited), and are always considered constrained by this
1918 attribute to avoid distributed overhead for parameters of limited
1919 @Chg{Version=[4],New=[class-wide],Old=[classwide]}
1920 types, as limited tagged objects may technically be unconstrained if they use
1921 defaulted discriminants. Such objects still cannot have their discriminants
1922 changed, as assignment is not supported for them, so there is no use for this
1923 attribute that would justify the overhead of passing it with all
1924 @Chg{Version=[4],New=[class-wide],Old=[classwide]} parameters.]}
1925 @end{Reason}
1926
1927 @begin{Discussion}
1928 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0005-1],ARef=[AI05-0214-1]}
1929 @ChgAdded{Version=[3],Text=[If the type of A is a type derived from an untagged
1930 partial view of a tagged type such that it is not a tagged type, then A is not
1931 considered a tagged object, and A'Constrained can return either True or
1932 False depending on the nature of the object.]}
1933 @end{Discussion}
1934 @end(description)
1935 @EndPrefixType{}
1936 @end{StaticSem}
1937
1938 @begin{Erron}
1939 @PDefn2{Term=(erroneous execution),Sec=(cause)}
1940 The execution of a construct is erroneous if the construct
1941 has a constituent that is a @nt<name> denoting
1942 a subcomponent that depends on discriminants, and the
1943 value of any of these discriminants is changed by this execution
1944 between evaluating the @nt<name> and the last use (within this
1945 execution) of the subcomponent denoted by the @nt<name>.
1946 @begin{Ramification}
1947 This rule applies to @nt<assignment_statement>s,
1948 calls (except when the discriminant-dependent subcomponent is
1949 an @key(in) parameter passed by copy),
1950 @nt<indexed_component>s, and @nt<slice>s.
1951 Ada 83 only covered the first two cases. AI83-00585 pointed out
1952 the situation with the last two cases.
1953 The cases of @nt<object_renaming_declaration>s and
1954 generic formal @key(in out) objects are handled differently,
1955 by disallowing the situation at compile time.
1956 @end{Ramification}
1957 @end{Erron}
1958
1959 @begin{Extend83}
1960 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{To be consistent with 8652/0006}
1961 @Defn{extensions to Ada 83}
1962 For consistency with other attributes, we are allowing
1963 the @Chg{New=[@nt{prefix}],Old=[prefix]} of Constrained to be a value as well
1964 as an object of a discriminated type, and also an implicit
1965 dereference. These extensions are
1966 not important capabilities, but there seems no
1967 reason to make this attribute different from
1968 other similar attributes. We are curious what most
1969 Ada 83 compilers do with F(1).X'Constrained.
1970
1971 We now handle in a general way
1972 the cases of erroneousness identified by AI83-00585, where
1973 the @nt<prefix> of an @nt<indexed_component> or @nt<slice>
1974 is discriminant-dependent, and the evaluation of the index or
1975 discrete range changes the value of a discriminant.
1976 @end{Extend83}
1977
1978 @begin{DiffWord83}
1979 We have moved all discussion of erroneous use of @nt<name>s
1980 that denote discriminant-dependent
1981 subcomponents to this subclause. In Ada 83, it used to
1982 appear separately under @nt{assignment_statement}s and
1983 subprogram calls.
1984 @end{DiffWord83}
1985
1986 @begin{DiffWord2005}
1987 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0214-1]}
1988 @ChgAdded{Version=[3],Text=[A'Constrained is now defined to return
1989 True for any A that is a tagged object. This doesn't change the result
1990 for any A allowed by previous versions of Ada; the change is necessary to
1991 avoid unnecessary overhead for limited tagged parameters.]}
1992 @end{DiffWord2005}
1993
1994
1995 @LabeledClause{Record Types}
1996
1997 @begin{Intro}
1998 @Defn{record}
1999 @Defn{record type}
2000 A record object is a composite object consisting of named components.
2001 The value of a record object is a composite value consisting of the
2002 values of the components.
2003 @IndexSee{Term=[structure],See=(record type)}
2004 @end{Intro}
2005
2006 @begin{Syntax}
2007 @Syn{lhs=<record_type_definition>,rhs="[[@key{abstract}] @key{tagged}] [@key{limited}] @Syn2{record_definition}"}
2008 @Syn{lhs=<record_definition>,rhs="
2009 @key{record}
2010 @Syn2{component_list}
2011 @key{end} @key{record}
2012 | @key{null record}"}
2013
2014
2015 @Syn{lhs=<component_list>,rhs="
2016 @Syn2{component_item} {@Syn2{component_item}}
2017 | {@Syn2{component_item}} @Syn2{variant_part}
2018 | @key{null};"}
2019
2020 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0009],ARef=[AI95-00137-01]}
2021 @Syn{lhs=<component_item>,rhs="@Syn2{component_declaration} | @Chg{New=[@Syn2{aspect_clause}],Old=[@Syn2{representation_clause}]}"}
2022
2023 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
2024 @Syn{lhs=<component_declaration>,rhs="
2025 @Syn2{defining_identifier_list} : @Syn2{component_definition} [:= @Syn2{default_expression}]@Chg{Version=[3],New=<
2026 [@Syn2{aspect_specification}]>,Old=[]};"}
2027 @end{Syntax}
2028
2029 @begin{Resolution}
2030 @PDefn2{Term=[expected type], Sec=(component_declaration default_expression)}
2031 The expected type for the @nt<default_expression>, if any, in
2032 a @nt<component_declaration> is the type of the
2033 component.
2034 @end{Resolution}
2035
2036 @begin{Legality}
2037 @ChgRef{Version=[2],Kind=[Deleted],ARef=[AI95-00287-01]}
2038 @ChgDeleted{Version=[2],Text=[A @nt<default_expression> is not permitted if
2039 the component is of a limited type.]}
2040
2041 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00366-01]}@ChgNote{This is not THE definition of component}
2042 @PDefn2{Term=[components], Sec=(of a record type)}
2043 Each @nt<component_declaration> declares a
2044 @Chg{Version=[2],New=[component],Old=[@i(component)]} of the record type.
2045 Besides components declared by @nt<component_declaration>s, the components of a
2046 record type include any components declared by @nt<discriminant_specification>s
2047 of the record type declaration. @Redundant[The identifiers of all components of
2048 a record type shall be distinct.]
2049 @begin{TheProof}
2050 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2051 The identifiers of all
2052 components of a record type have to
2053 be distinct because they are all declared immediately
2054 within the same declarative region. See @Chg{Version=[3],New=[Clause],Old=[Section]}
2055 @RefSecNum{Visibility Rules}.
2056 @end{theproof}
2057
2058
2059 Within a @nt{type_declaration},
2060 a @nt{name} that denotes a component, protected subprogram,
2061 or entry of the type is allowed only in the following cases:
2062
2063 @begin{Itemize}
2064 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0004-1],ARef=[AI05-0295-1]}
2065 A @nt{name} that denotes any component, protected subprogram,
2066 or entry is allowed within @Chg{Version=[3],New=[an @nt{aspect_specification},
2067 an operational item, or ],Old=[]}a
2068 representation item that occurs within the declaration of the composite type.
2069
2070 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0264-1]}
2071 A @nt{name} that denotes a noninherited discriminant
2072 is allowed within the declaration of the type,
2073 but not within the @nt{discriminant_part}.
2074 If the discriminant is used to define the constraint of
2075 a component, the bounds of an entry family, or
2076 the constraint of the parent subtype in a
2077 @nt<derived_type_definition>@Chg{Version=[3],New=[,],Old=[]}
2078 then its name shall appear alone as a @nt<direct_name> (not as
2079 part of a larger expression or expanded name).
2080 A discriminant shall not be used to define the constraint of
2081 a scalar component.@Defn2{Term=[discriminant],Sec=[use in a record definition]}@ChgNote{New index entry}
2082 @begin{Reason}
2083 The penultimate restriction simplifies implementation,
2084 and allows the outer discriminant and the inner discriminant
2085 or bound to possibly share storage.
2086 @end{reason}
2087 @begin{Ramification}
2088 Other rules prevent such a discriminant from being an inherited one.
2089 @end{ramification}
2090 @begin{Reason}
2091 The last restriction is inherited from Ada 83.
2092 The restriction is not really necessary from a language design point of
2093 view, but we did not remove it,
2094 in order to avoid unnecessary changes to existing compilers.
2095 @end{Reason}
2096 @begin{Discussion}
2097 Note that a discriminant can be used to define the constraint
2098 for a component that is of an access-to-composite type.
2099 @end{Discussion}
2100 @begin{Reason}
2101 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00373-01]}
2102 The above rules, and a similar one in @RefSecNum{Subprogram Declarations}
2103 for formal parameters, are intended to allow initializations of
2104 components or parameters to occur in @Chg{Version=[2],
2105 New=[a (nearly)],Old=[an]} arbitrary order @em whatever
2106 order is most efficient@Chg{Version=[2],New=[ (subject to the restrictions
2107 of @RefSecNum{Object Declarations})],Old=[]},
2108 since one @nt{default_expression} cannot depend on the value of
2109 another one.
2110 @Chg{Version=[2],New=[They],Old=[It]} also prevent circularities.
2111 @end{Reason}
2112 @begin{Ramification}
2113 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0295-1]}
2114 Inherited discriminants are not allowed to be denoted,
2115 except within @Chg{Version=[3],New=[@nt{aspect_specification}s
2116 and ],Old=[]}representation items.
2117 However, the @SynI{discriminant_}@nt{selector_name} of
2118 the parent @nt{subtype_indication} is allowed to denote
2119 a discriminant of the parent.
2120 @end{Ramification}
2121 @end{Itemize}
2122
2123 If the name of the current instance of a type
2124 (see @RefSecNum{The Context of Overload Resolution})
2125 is used to define the constraint of a component,
2126 then it shall appear as a @nt{direct_name}
2127 that is the @nt<prefix> of an @nt{attribute_reference}
2128 whose result is of an access type,
2129 and the @nt{attribute_reference} shall appear alone.
2130 @begin{Reason}
2131 This rule allows T'Access
2132 or T'Unchecked_Access, but disallows, for example,
2133 a range constraint (1..T'Size).
2134 Allowing things like (1..T'Size) would mean that a per-object
2135 constraint could affect the size of the object,
2136 which would be bad.
2137 @end{Reason}
2138
2139 @end{Legality}
2140
2141 @begin{StaticSem}
2142 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00318-02]}
2143 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0004-1]}
2144 @ChgAdded{Version=[2],Text=[@Defn{explicitly limited record}
2145 @Defn2{Term=[record],Sec=(explicitly limited)}
2146 If a @Chg{Version=[3],New=[@nt{record_type_definition}],Old=[@ntf{record_type_declaration}]}
2147 includes the reserved word @key{limited}, the type is called an
2148 @i<explicitly limited record> type.]}
2149
2150 @PDefn2{Term=[nominal subtype], Sec=(of a record component)}
2151 The @nt{component_definition} of a @nt<component_declaration>
2152 defines the (nominal) subtype of the component.
2153 If the reserved word @key(aliased) appears in the @nt{component_definition},
2154 then the component is aliased (see @RefSecNum{Access Types}).
2155 @begin(Ramification)
2156 @ChgRef{Version=[2],Kind=[DeletedNoDelMsg],ARef=[AI95-00363-01]}
2157 @ChgDeleted{Version=[2],Text=[In this case, the nominal subtype cannot be
2158 an unconstrained discriminated subtype.
2159 See @RefSecNum{Array Types}.]}
2160 @end(Ramification)
2161
2162 @Defn{null record}
2163 If the @nt<component_list> of a record type is defined by the reserved
2164 word @key(null) and there are no discriminants, then the record type has
2165 no components and all records of the type are @i(null records).
2166 A @nt<record_definition> of @key{null record} is equivalent to
2167 @key{record null; end record}.
2168 @begin{Ramification}
2169
2170 This short-hand is available both for declaring a record type
2171 and a record extension @em see @RefSecNum(Type Extensions).
2172 @end{ramification}
2173
2174 @end{StaticSem}
2175
2176 @begin{RunTime}
2177 @PDefn2{Term=[elaboration], Sec=(record_type_definition)}
2178 The elaboration of a @nt{record_type_definition} creates
2179 the record type and its first subtype,
2180 and consists of the elaboration of the @nt<record_definition>.
2181 @PDefn2{Term=[elaboration], Sec=(record_definition)}
2182 The elaboration of a @nt<record_definition> consists of the
2183 elaboration of its @nt{component_list}, if any.
2184
2185 @PDefn2{Term=[elaboration], Sec=(component_list)}
2186 The elaboration of a @nt{component_list} consists of the elaboration
2187 of the @nt{component_item}s and
2188 @nt{variant_part}, if any, in the order in which they appear.
2189 @PDefn2{Term=[elaboration], Sec=(component_declaration)}
2190 The elaboration of a @nt{component_declaration} consists of the
2191 elaboration of the @nt{component_definition}.
2192 @begin(Discussion)
2193 If the @nt<defining_identifier_list> has more than one
2194 @nt<defining_identifier>, we presume here that the transformation
2195 explained in @RefSecNum(Object Declarations) has already
2196 taken place. Alternatively, we could say that
2197 the @nt<component_definition> is elaborated once for
2198 each @nt<defining_identifier> in the list.
2199 @end(Discussion)
2200
2201 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0002],ARef=[AI95-00171-01]}
2202 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00230-01]}
2203 @Defn{per-object expression}
2204 @Defn{per-object constraint}
2205 @Defn{entry index subtype}
2206 Within the definition of a composite type,
2207 if a @nt<component_definition> or @nt<discrete_subtype_definition>
2208 (see @RefSecNum{Entries and Accept Statements})
2209 includes a @nt<name> that denotes a discriminant of the type, or
2210 that is an @nt<attribute_reference> whose @nt<prefix> denotes the current
2211 instance of the type,
2212 the expression containing the @nt<name> is called a @i(per-object expression),
2213 and the @Chg{New=[@nt{constraint} or @nt{range}],Old=[constraint]} being defined
2214 is called a @i(per-object constraint).
2215 @PDefn2{Term=[elaboration], Sec=(component_definition)}
2216 For the elaboration of a @nt{component_definition} of
2217 a @nt<component_declaration>@Chg{New=[ or the @nt{discrete_@!subtype_@!definition}
2218 of an @nt{entry_@!declaration} for an entry family (see
2219 @RefSecNum{Entries and Accept Statements})],Old=[]},@Chg{Version=[2],
2220 New=[ if the component subtype is defined by an @nt{access_definition} or],
2221 Old=[]} if the @nt{constraint}
2222 @Chg{New=[or @nt{range}],Old=[]} of the @nt{subtype_indication}
2223 @Chg{New=[or @nt{discrete_@!subtype_@!definition}],Old=[]} is not a per-object
2224 constraint, then the@Chg{Version=[2],New=[ @nt{access_definition},],Old=[]}
2225 @nt{subtype_indication}@Chg{Version=[2],New=[,],Old=[]}
2226 @Chg{New=[or @nt{discrete_@!subtype_@!definition}],Old=[]}
2227 is elaborated. On the other hand, if the @nt{constraint}
2228 @Chg{New=[or @nt{range}],Old=[]} is a per-object constraint,
2229 then the elaboration consists of the evaluation of any included
2230 expression that is not part of a per-object expression.
2231 @Chg{New=[Each such expression is evaluated once unless it is part of a named
2232 association in a discriminant constraint, in which case it is evaluated once
2233 for each associated discriminant.],Old=[]}
2234
2235 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0002],ARef=[AI95-00171-01]}
2236 @Chg{New=[@PDefn2{Term=[Elaboration],Sec=(per-object constraint)}When a
2237 per-object constraint is elaborated @Redundant[(as part of creating an
2238 object)], each per-object expression of the constraint is evaluated. For other
2239 expressions, the values determined during the elaboration of the
2240 @nt{component_@!definition} or @nt{entry_@!declaration} are used. Any checks
2241 associated with the enclosing @nt{subtype_indication} or
2242 @nt{discrete_subtype_definition} are performed@Redundant[, including the subtype
2243 compatibility check (see @RefSecNum{Subtype Declarations}),] and the associated
2244 subtype is created.],
2245 Old=[]}
2246 @begin(Discussion)
2247 The evaluation of other expressions that appear in
2248 @nt<component_definition>s and @nt<discrete_subtype_definition>s
2249 is performed when the type definition is elaborated.
2250 The evaluation of expressions that appear as
2251 @nt<default_expression>s is postponed until an object is created.
2252 Expressions in representation items
2253 that appear within a composite type definition are evaluated
2254 according to the rules of the particular representation item.
2255 @end(Discussion)
2256 @end{RunTime}
2257
2258 @begin{Notes}
2259 A @nt<component_declaration> with several identifiers is equivalent
2260 to a sequence of single @nt<component_declaration>s, as explained
2261 in @RefSecNum{Object Declarations}.
2262
2263 The @nt<default_expression> of a record component is only
2264 evaluated upon the creation of
2265 a default-initialized object of the record type (presuming
2266 the object has the component, if it is in a @nt<variant_part> @em
2267 see @RefSecNum{Object Declarations}).
2268
2269 The subtype defined by a @nt<component_definition> (see @RefSecNum(Array Types))
2270 has to be a definite subtype.
2271
2272 If a record type does not have a @nt<variant_part>, then the same components
2273 are present in all values of the type.
2274
2275 A record type is limited if it has the reserved word @key[limited]
2276 in its definition, or if any of its components are
2277 limited (see @RefSecNum{Limited Types}).
2278
2279 @PDefn2{Term=[predefined operations],Sec=(of a record type)}
2280 The predefined operations of a record type include membership
2281 tests, qualification, and explicit conversion. If the
2282 record type is nonlimited, they also include
2283 assignment and the predefined equality operators.
2284
2285 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
2286 A component of a record can be named with a @nt<selected_component>.
2287 A value of a record can be specified with a @nt<record_aggregate>@Chg{Version=[2],
2288 New=[],Old=[, unless the record type is limited]}.
2289
2290 @end{Notes}
2291
2292 @begin{Examples}
2293 @Leading@keepnext@i(Examples of record type declarations: )
2294 @begin(Example)
2295 @key(type) Date @key(is)
2296 @key(record)
2297 Day : Integer @key(range) 1 .. 31;
2298 Month : Month_Name;
2299 Year : Integer @key(range) 0 .. 4000;
2300 @key(end) @key(record);
2301
2302 @key(type) Complex @key(is)
2303 @key(record)
2304 Re : Real := 0.0;
2305 Im : Real := 0.0;
2306 @key(end) @key(record);
2307 @end(Example)
2308
2309 @begin{WideAbove}
2310 @leading@keepnext@i(Examples of record variables: )
2311 @end{WideAbove}
2312 @begin(Example)
2313 Tomorrow, Yesterday : Date;
2314 A, B, C : Complex;
2315
2316 --@RI[ both components of A, B, and C are implicitly initialized to zero ]
2317 @end(Example)
2318 @end{Examples}
2319
2320 @begin{Extend83}
2321 @Defn{extensions to Ada 83}
2322 The syntax rule for @nt<component_declaration> is modified
2323 to use @nt<component_definition> (instead of
2324 @ntf<component_subtype_definition>). The effect of this change
2325 is to allow the reserved word @key(aliased) before the
2326 @ntf<component_subtype_definition>.
2327
2328 A short-hand is provided for defining a null record type
2329 (and a null record extension), as these will be more common
2330 for abstract root types (and derived types without additional components).
2331
2332 The syntax rule for @nt{record_type_definition} is modified to allow
2333 the reserved words @key{tagged} and @key{limited}.
2334 Tagging is new.
2335 Limitedness is now orthogonal to privateness.
2336 In Ada 83 the syntax implied that limited private was sort of more
2337 private than private.
2338 However, limitedness really has nothing to do with privateness;
2339 limitedness simply indicates the lack of assignment capabilities,
2340 and makes perfect sense for nonprivate types such as record types.
2341 @end{Extend83}
2342
2343 @begin{DiffWord83}
2344 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0009],ARef=[AI95-00137-01]}
2345 The syntax rules now allow @Chg{New=[@nt{aspect_clause}s],
2346 Old=[@nt{representation_clause}s]} to appear in a @nt{record_definition}.
2347 This is not a language extension, because @LegalityName@;s prevent all
2348 language-defined representation clauses from appearing there.
2349 However, an implementation-defined @nt{attribute_definition_clause}
2350 could appear there.
2351 The reason for this change is to allow the rules for
2352 @Chg{New=[@nt{aspect_clause}s],Old=[@nt{representation_clause}s]} and
2353 representation pragmas to be as similar as possible.
2354 @end{DiffWord83}
2355
2356 @begin{Extend95}
2357 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
2358 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2359 Record components can have an anonymous access type.]}
2360
2361 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
2362 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2363 Limited components can be initialized, so long as the expression is
2364 one that allows building the object in place (such as an @nt{aggregate} or
2365 @nt{function_call}).]}
2366 @end{Extend95}
2367
2368 @begin{DiffWord95}
2369 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0002],ARef=[AI95-00171-01]}
2370 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Improved the description of the
2371 elaboration of per-object constraints.]}
2372
2373 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0009],ARef=[AI95-00137-01]}
2374 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Changed representation clauses to
2375 aspect clauses to reflect that they are used for more than just
2376 representation.]}
2377
2378 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00318-02]}
2379 @ChgAdded{Version=[2],Text=[Defined @i{explicitly limited record} type to
2380 use in other rules.]}
2381 @end{DiffWord95}
2382
2383 @begin{Extend2005}
2384 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
2385 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
2386 An optional @nt{aspect_specification} can be used in a @nt{component_declaration}.
2387 This is described in @RefSecNum{Aspect Specifications}.]}
2388 @end{Extend2005}
2389
2390
2391
2392 @LabeledSubClause{Variant Parts and Discrete Choices}
2393
2394 @begin{Intro}
2395 A record type with a @nt<variant_part> specifies alternative
2396 lists of components. Each @nt<variant> defines the components
2397 for the value or values of the discriminant
2398 covered by its @nt<discrete_choice_list>.
2399 @begin{Discussion}
2400 @RootDefn{cover a value}
2401 @nt{Discrete_choice_list}s and @nt{discrete_choice}s are
2402 said to @i(cover) values as defined below; which
2403 @nt{discrete_choice_list} covers a value determines
2404 which of various alternatives is chosen. These are used
2405 in @nt{variant_part}s, @nt{array_aggregate}s, and
2406 @nt{case_statement}s.
2407 @end{Discussion}
2408 @end{Intro}
2409
2410 @begin{MetaRules}
2411 The definition of @lquotes@;cover@rquotes@; in this subclause
2412 and the rules about discrete choices
2413 are designed so that they
2414 are also appropriate for array aggregates and case statements.
2415
2416 The rules of this subclause intentionally
2417 parallel those for case statements.
2418 @end{MetaRules}
2419
2420 @begin{Syntax}
2421 @Syn{lhs=<variant_part>,rhs="
2422 @key{case} @SynI{discriminant_}@Syn2{direct_name} @key{is}
2423 @Syn2{variant}
2424 {@Syn2{variant}}
2425 @key{end} @key{case};"}
2426
2427
2428 @Syn{lhs=<variant>,rhs="
2429 @key{when} @Syn2{discrete_choice_list} =>
2430 @Syn2{component_list}"}
2431
2432
2433 @Syn{lhs=<discrete_choice_list>,rhs="@Syn2{discrete_choice} {| @Syn2{discrete_choice}}"}
2434
2435 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3],ARef=[AI05-0158-1]}
2436 @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}"}
2437 @end{Syntax}
2438
2439 @begin{Resolution}
2440 @Defn2{Term=[discriminant], Sec=(of a @nt<variant_part>)}
2441 The @i(discriminant_)@nt{direct_name} shall resolve to denote
2442 a discriminant (called
2443 the @i(discriminant of the @nt<variant_part>)) specified in
2444 the @nt{known_discriminant_part} of the @nt{full_type_declaration}
2445 that contains the @nt{variant_part}.
2446 @PDefn2{Term=[expected type], Sec=(variant_part discrete_choice)}
2447 The expected type for each @nt{discrete_choice} in a @nt<variant> is the type
2448 of the discriminant of the @nt{variant_part}.
2449 @begin{Ramification}
2450 A @nt<full_type_declaration> with a @nt<variant_part>
2451 has to have a (new) @nt<known_discriminant_part>;
2452 the discriminant of the @nt<variant_part> cannot be an
2453 inherited discriminant.
2454 @end{Ramification}
2455 @end{Resolution}
2456
2457 @begin{Legality}
2458 The discriminant of the @nt{variant_part} shall
2459 be of a discrete type.
2460 @begin{Ramification}
2461 It shall not be of an access type,
2462 named or anonymous.@end{ramification}
2463
2464 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3]}
2465 The @Chg{Version=[3],New=[@nt{choice_expression}s, @nt{subtype_indication}s,
2466 and @nt{range}s],Old=[@nt{expression}s and @nt{discrete_range}s]} given as
2467 @nt{discrete_choice}s in a @nt{variant_part} shall be static.
2468 The @nt{discrete_choice} @key(others) shall appear alone
2469 in a @nt{discrete_choice_list}, and such a @nt{discrete_choice_list},
2470 if it appears, shall be the last one in the enclosing construct.
2471
2472 @Leading@PDefn2{Term=[cover a value], Sec=(by a @nt{discrete_choice})}
2473 A @nt<discrete_choice> is defined to @i(cover a value) in the
2474 following cases:
2475 @begin(itemize)
2476 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0262-1]}
2477 A @nt{discrete_choice} that is
2478 @Chg{Version=[3],New=[a @nt{choice_expression}],Old=[an @nt{expression}]}
2479 covers a value if
2480 the value equals the value of
2481 the @Chg{Version=[3],New=[@nt{choice_expression}],Old=[@nt{expression}]}
2482 converted to the expected type.
2483
2484 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0153-3],ARef=[AI05-0262-1]}
2485 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0071-1]}
2486 @ChgAdded{Version=[3],Text=[A @nt{discrete_choice} that is a
2487 @nt{subtype_indication} covers all values (possibly none) that belong to the
2488 subtype and that satisfy the static @Chg{Version=[4],New=[predicates],
2489 Old=[predicate]} of the subtype (see @RefSecnum{Subtype Predicates}).]}
2490
2491 @begin{Ramification}
2492 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0262-1]}
2493 @ChgAdded{Version=[3],Text=[A dynamic predicate is never allowed in this case
2494 (for @nt{variant}s, @nt{case_statement}s, and @nt{case_expression}s,
2495 a subtype with a dynamic
2496 predicate isn't static and thus isn't allowed in a @nt{discrete_choice},
2497 and for a choice in an @nt{array_aggregate}, a dynamic predicate
2498 is explicitly disallowed @em see @RefSecnum{Subtype Predicates}).]}
2499 @end{Ramification}
2500
2501 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3]}
2502 A @nt{discrete_choice} that is a
2503 @Chg{Version=[3],New=[@nt{range}],Old=[@nt{discrete_range}]} covers all values
2504 (possibly none) that belong to the range.
2505
2506 The @nt{discrete_choice} @key{others} covers all values of its
2507 expected type that are not covered by
2508 previous @nt{discrete_choice_list}s of the same construct.
2509 @begin(Ramification)
2510 For @nt{case_statement}s,
2511 this includes values outside the range of the static subtype (if any)
2512 to be covered by the choices.
2513 It even includes values outside the base
2514 range of the case expression's type,
2515 since values of numeric types (and undefined values of any scalar type?)
2516 can be outside their base range.
2517 @end(Ramification)
2518 @end(itemize)
2519
2520 @PDefn2{Term=[cover a value], Sec=(by a @nt{discrete_choice_list})}
2521 A @nt{discrete_choice_list} covers a value if one of
2522 its @nt{discrete_choice}s covers the value.
2523
2524 @Leading@keepnext@;The possible values of the discriminant of a @nt{variant_part}
2525 shall be covered as follows:
2526 @begin{itemize}
2527 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0153-3],ARef=[AI05-0188-1],ARef=[AI05-0262-1]}
2528 @ChgRef{Version=[4],Kind=[Revised],ARef=[AI12-0071-1]}
2529 If the discriminant is of a static constrained scalar
2530 subtype@Chg{Version=[3],New=[],Old=[,]}
2531 then@Chg{Version=[3],New=[, except within an instance of a generic
2532 unit,],Old=[]} each non-@key{others} @nt{discrete_@!choice} shall cover
2533 only values in that subtype@Chg{Version=[3],New=[ that satisfy its
2534 @Chg{Version=[4],New=[predicates],Old=[predicate]}],Old=[]}, and each value
2535 of that subtype @Chg{Version=[3],New=[that satisfies its
2536 @Chg{Version=[4],New=[predicates],Old=[predicate]} ],Old=[]}shall be covered
2537 by some @nt{discrete_@!choice} @Redundant[(either explicitly or
2538 by @key<others>)];
2539
2540 @begin{Reason}
2541 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0188-1]}
2542 @ChgAdded{Version=[3],Type=[Leading],Text=[The exemption for a discriminated type declared in an instance
2543 allows the following example:]}
2544 @begin{Example}
2545 @ChgRef{Version=[3],Kind=[AddedNormal]}
2546 @ChgAdded{Version=[3],Text=[@key[generic]
2547 @key[type] T @key[is new] Integer;
2548 @key[package] G @key[is]
2549 @key[type] Rec (Discrim : T) @key[is record]
2550 @key[case] Discrim @key[is]
2551 @key[when] -10 .. -1 =>
2552 Foo : Float;
2553 @key[when others] =>
2554 @key[null];
2555 @key[end case];
2556 @key[end record];
2557 @key[end] G;]}
2558
2559 @ChgRef{Version=[3],Kind=[AddedNormal]}
2560 @ChgAdded{Version=[3],Text=[@key{package} I @key{is new} G (Natural); -- @Examcom{Legal}]}
2561 @end{Example}
2562 @end{Reason}
2563 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0264-1]}
2564 If the type of the discriminant is a
2565 descendant of a generic formal scalar type@Chg{Version=[3],New=[,],Old=[]}
2566 then the @nt{variant_part} shall have an @key{others}
2567 @nt{discrete_choice};
2568 @begin{Reason}
2569 The base range is not known statically in this case.
2570 @end{Reason}
2571
2572 Otherwise,
2573 each value of the base range of the type of the discriminant shall
2574 be covered @Redundant[(either explicitly or by @key<others>)].
2575 @end{itemize}
2576
2577 Two distinct @nt{discrete_choice}s of a @nt{variant_part} shall not cover
2578 the same value.
2579
2580 @end{Legality}
2581
2582 @begin{StaticSem}
2583 If the @nt{component_list} of a @nt{variant} is specified by @key(null),
2584 the variant has no components.
2585
2586 @Defn{govern a @nt{variant_part}}
2587 @Defn{govern a @nt{variant}}
2588 The discriminant of a @nt<variant_part> is said to @i(govern) the
2589 @nt<variant_part> and its @nt<variant>s. In addition,
2590 the discriminant of a derived
2591 type governs a @nt<variant_part> and its @nt<variant>s if it
2592 corresponds (see @RefSecNum(Discriminants)) to the
2593 discriminant of the @nt<variant_part>.
2594
2595 @end{StaticSem}
2596
2597 @begin{RunTime}
2598 A record value contains the values of the components of a particular
2599 @nt{variant} only if the
2600 value of the discriminant governing the @nt<variant> is covered
2601 by the @nt{discrete_choice_list} of the @nt{variant}.
2602 This rule applies in turn to any further @nt{variant} that is, itself,
2603 included in the @nt{component_list} of the given @nt{variant}.
2604
2605 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0290-1]}
2606 @ChgAdded{Version=[3],Text=[When an object of a discriminated type @i<T> is
2607 initialized by default, Constraint_Error is raised if no
2608 @nt{discrete_choice_list} of any @nt{variant} of a @nt{variant_part} of @i<T>
2609 covers the value of the discriminant that governs the @nt{variant_part}. When a
2610 @nt{variant_part} appears in the @nt{component_list} of another @nt{variant}
2611 @i<V>, this test is only applied if the value of the discriminant governing
2612 @i<V> is covered by the @nt{discrete_choice_list} of @i<V>.]}
2613 @begin{ImplNote}
2614 @ChgRef{Version=[3],Kind=[AddedNormal]}
2615 @ChgAdded{Version=[3],Text=[This is not a @ldquote@;check@rdquote; it cannot
2616 be suppressed. However, in most cases it is not necessary to generate any code
2617 to raise this exception. A test is needed (and can fail) in the case where the
2618 discriminant subtype has a Static_Predicate specified, it also has predicate
2619 checking disabled, and the discriminant governs a @nt{variant_part} which
2620 lacks a @key[when others] choice.]}
2621
2622 @ChgRef{Version=[3],Kind=[AddedNormal]}
2623 @ChgAdded{Version=[3],Text=[The test also could fail for a static discriminant
2624 subtype with range checking suppressed and the discriminant governs a
2625 @nt{variant_part} which lacks a @key[when others] choice. But execution is erroneous if a range
2626 check that would have failed is suppressed (see @RefSecNum{Suppressing Checks}),
2627 so an implementation does not have to generate code to check this case. (An
2628 unchecked failed predicate does not cause erroneous execution, so the test is
2629 required in that case.)]}
2630
2631 @ChgRef{Version=[3],Kind=[AddedNormal]}
2632 @ChgAdded{Version=[3],Text=[Like the checks associated with a per-object
2633 constraint, this test is not made during the elaboration of a
2634 @nt{subtype_indication}.]}
2635 @end{ImplNote}
2636
2637 @PDefn2{Term=[elaboration], Sec=(variant_part)}
2638 The elaboration of a @nt{variant_part} consists
2639 of the elaboration of the @nt{component_list} of each
2640 @nt{variant} in the order in which they appear.
2641 @end{RunTime}
2642
2643 @begin{Examples}
2644 @Leading@keepnext@i(Example of record type with a variant part: )
2645 @begin(Example)
2646 @key(type) Device @key(is) (Printer, Disk, Drum);
2647 @key(type) State @key(is) (Open, Closed);
2648
2649 @key(type) Peripheral(Unit : Device := Disk) @key(is)
2650 @key(record)
2651 Status : State;
2652 @key(case) Unit @key(is)
2653 @key(when) Printer =>
2654 Line_Count : Integer @key(range) 1 .. Page_Size;
2655 @key(when) @key(others) =>
2656 Cylinder : Cylinder_Index;
2657 Track : Track_Number;
2658 @key(end) @key(case);
2659 @key(end) @key(record);
2660 @end(Example)
2661
2662 @begin{WideAbove}
2663 @leading@keepnext@i(Examples of record subtypes:)
2664 @end{WideAbove}
2665 @begin(Example)
2666 @key(subtype) Drum_Unit @key(is) Peripheral(Drum);
2667 @key(subtype) Disk_Unit @key(is) Peripheral(Disk);
2668 @end(Example)
2669
2670 @begin{WideAbove}
2671 @leading@keepnext@i(Examples of constrained record variables:)
2672 @end{WideAbove}
2673 @begin(Example)
2674 Writer : Peripheral(Unit => Printer);
2675 Archive : Disk_Unit;
2676 @end(Example)
2677 @end{Examples}
2678
2679 @begin{Extend83}
2680 @Defn{extensions to Ada 83}
2681 In Ada 83, the discriminant of a @nt{variant_part} is not allowed to
2682 be of a generic formal type.
2683 This restriction is removed in Ada 95; an @key{others} @nt{discrete_choice}
2684 is required in this case.
2685 @end{Extend83}
2686
2687 @begin{DiffWord83}
2688 The syntactic category @ntf{choice} is removed.
2689 The syntax rules for @nt{variant}, @nt{array_aggregate}, and
2690 @nt{case_statement} now use @nt{discrete_choice_list}
2691 or @nt{discrete_choice} instead.
2692 The syntax rule for @nt{record_aggregate} now defines its own syntax
2693 for named associations.
2694
2695 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
2696 We have added the term Discrete Choice to the title
2697 since this is where they are talked about.
2698 This is analogous to the name of the subclause
2699 "Index Constraints and Discrete Ranges" in
2700 the @Chg{Version=[3],New=[subclause],Old=[clause]} on Array Types.
2701
2702 The rule requiring that the discriminant denote
2703 a discriminant of the type being defined seems to have been
2704 left implicit in RM83.
2705 @end{DiffWord83}
2706
2707 @begin{Incompatible2005}
2708 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0158-1]}
2709 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}
2710 Membership tests are no longer allowed as a @nt{discrete_choice}, in
2711 order that those tests can be expanded to allow multiple tests in a
2712 single expression without ambiguity. Since a membership test has a
2713 boolean type, they are very unlikely to be used as a @nt{discrete_choice}.]}
2714 @end{Incompatible2005}
2715
2716 @begin{Extend2005}
2717 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0153-3]}
2718 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
2719 Subtypes with static predicates can be used in @nt{discrete_choice}s,
2720 and the coverage rules are modified to respect the predicates.]}
2721
2722 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0188-1]}
2723 @ChgAdded{Version=[3],Text=[Variants in generic specifications are no
2724 longer rejected if the subtype
2725 of the actual type does not include all of the case choices. This probably
2726 isn't useful, but it is consistent with the treatment of @nt{case_expression}s.]}
2727 @end{Extend2005}
2728
2729 @begin{DiffWord2005}
2730 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0290-1]}
2731 @ChgAdded{Version=[3],Text=[Added a test that some @nt{variant} covers the
2732 value of a discriminant that governs a @nt{variant_part}. This is similar
2733 to the test that some case limb covers the value of the
2734 @SynI{Selecting_}@nt{expression} of a @nt{case_statement}. This test cannot
2735 change the behavior of any nonerroneous Ada 2005 program, so it is not
2736 an inconsistency.]}
2737 @end{DiffWord2005}
2738
2739 @begin{DiffWord2012}
2740 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0071-1]}
2741 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> Updated wording of the
2742 coverage rules to use the new term "satisfies the predicates"
2743 (see @RefSecNum{Subtype Predicates}).]}
2744 @end{DiffWord2012}
2745