Debian Codegraph @

source_2005/03b.mss

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