Debian Codegraph @

source_2012/12.mss

raw
1 @Part(12, Root="ada.mss")
2
3 @Comment{$Date: 2015/04/03 04:12:42 $}
4 @LabeledSection{Generic Units}
5
6 @Comment{$Source: e:\\cvsroot/ARM/Source/12.mss,v $}
7 @Comment{$Revision: 1.97 $}
8
9 @begin{Intro}
10 @Defn{generic unit}
11 A @i{generic unit} is a program unit that is either a generic subprogram
12 or a generic package.
13 @Defn{template}
14 A generic unit is a @i{template}@Redundant[, which can be parameterized,
15 and from which corresponding (nongeneric) subprograms or packages can be
16 obtained].
17 The resulting program units are said to be @i{instances} of the original
18 generic unit.
19 @IndexSee{Term=[template],See=(generic unit)}
20 @IndexSee{Term=[macro],See=(generic unit)}
21 @IndexSee{Term=[parameter],See=[generic formal parameter]}
22 @ToGlossary{Term=<Generic unit>,
23 Text=<A generic unit is a template for a (nongeneric) program unit;
24 the template can be parameterized by objects, types, subprograms, and
25 packages. An instance of a generic unit is created by a
26 @nt(generic_instantiation).
27 The rules of the language are enforced when a generic unit is compiled,
28 using a generic contract model; additional checks are
29 performed upon instantiation to verify the contract is met.
30 That is, the declaration of a generic unit represents a contract
31 between the body of the generic and instances of the generic.
32 Generic units can be used to perform the role that macros
33 sometimes play in other languages.>}@ChgNote{Correction for AI-00024, no mechism to correct glossary entries.}
34
35 @redundant[A generic unit is declared by a @nt{generic_declaration}. This form
36 of declaration has a @nt{generic_@!formal_@!part} declaring any generic
37 formal parameters. An instance of a generic unit is obtained as the
38 result of a @nt{generic_instantiation} with appropriate
39 generic actual parameters for the generic formal parameters. An
40 instance of a generic subprogram is a subprogram. An instance of a
41 generic package is a package.
42
43 Generic units are templates. As templates they do not have the
44 properties that are specific to their nongeneric counterparts. For
45 example, a generic subprogram can be instantiated but it cannot be
46 called. In contrast, an instance of a generic subprogram is a
47 (nongeneric) subprogram; hence, this instance can be called but it
48 cannot be used to produce further instances.]
49 @end{Intro}
50
51 @LabeledClause{Generic Declarations}
52
53 @begin{Intro}
54 @redundant[A @nt{generic_declaration} declares a generic unit, which is either a
55 generic subprogram or a generic package. A @nt{generic_declaration}
56 includes a @nt{generic_formal_part} declaring any generic formal
57 parameters. A generic formal parameter can be an object;
58 alternatively (unlike a parameter of a subprogram), it can be a type,
59 a subprogram, or a package.]
60 @end{Intro}
61
62 @begin{Syntax}
63 @Syn{lhs=<generic_declaration>,rhs="@Syn2{generic_subprogram_declaration} | @Syn2{generic_package_declaration}"}
64
65
66 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
67 @Syn{lhs=<generic_subprogram_declaration>,rhs="
68 @Syn2{generic_formal_part} @Syn2{subprogram_specification}@Chg{Version=[3],New=<
69 [@Syn2{aspect_specification}]>,Old=[]};"}
70
71 @Syn{lhs=<generic_package_declaration>,rhs="
72 @Syn2{generic_formal_part} @Syn2{package_specification};"}
73
74 @begin{Ramification}
75 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
76 @ChgAdded{Version=[3],Text=[No syntax change is needed here to allow an
77 @nt{aspect_specification}; a generic package can have an @nt{aspect_specification}
78 because a @nt{package_specification} allows an @nt{aspect_specification}.]}
79 @end{Ramification}
80
81 @Syn{lhs=<generic_formal_part>,rhs="@key{generic} {@Syn2{generic_formal_parameter_declaration} | @Syn2{use_clause}}"}
82
83 @Syn{lhs=<generic_formal_parameter_declaration>,rhs="
84 @Syn2{formal_object_declaration}
85 | @Syn2{formal_type_declaration}
86 | @Syn2{formal_subprogram_declaration}
87 | @Syn2{formal_package_declaration}"}
88 @begin{SyntaxText}
89 The only form of @nt{subtype_indication} allowed within a
90 @nt{generic_formal_part} is a @nt{subtype_mark}
91 @Redundant[(that is, the @nt{subtype_indication} shall not include an
92 explicit @nt{constraint})].
93 The defining name of a generic subprogram shall be an @nt{identifier}
94 @Redundant[(not an @nt{operator_symbol})].
95 @begin{Reason}
96 The reason for forbidding @nt{constraint}s in
97 @nt{subtype_indication}s is that it simplifies the elaboration of
98 @nt{generic_declaration}s (since there is nothing to evaluate),
99 and that it simplifies the matching rules,
100 and makes them more checkable at compile time.
101 @end{Reason}
102 @end{SyntaxText}
103 @end{Syntax}
104
105 @begin{StaticSem}
106 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00434-01]}
107 @Defn{generic package}
108 @Defn{generic subprogram}
109 @Defn{generic procedure}
110 @Defn{generic function}
111 A @nt{generic_declaration} declares a generic unit @em a
112 generic package, generic procedure@Chg{Version=[2],New=[,],Old=[]}
113 or generic function, as appropriate.
114
115 @Defn{generic formal}
116 An entity is a @i{generic formal} entity if it is declared
117 by a @nt<generic_formal_parameter_declaration>. @lquotes@;Generic formal,@rquotes@;
118 or simply @lquotes@;formal,@rquotes@; is used as a prefix in referring
119 to objects, subtypes (and types), functions, procedures and packages,
120 that are generic formal entities, as well as to their respective
121 declarations.
122 @Redundant[Examples: @lquotes@;generic formal procedure@rquotes@;
123 or a @lquotes@;formal integer type declaration.@rquotes@;]
124 @end{StaticSem}
125
126 @begin{RunTime}
127 @PDefn2{Term=[elaboration], Sec=(generic_declaration)}
128 The elaboration of a @nt{generic_declaration} has no effect.
129 @end{RunTime}
130
131 @begin{Notes}
132 Outside a generic unit
133 a @nt{name} that denotes the @nt{generic_declaration} denotes the
134 generic unit.
135 In contrast, within the declarative region of the generic unit,
136 a @nt{name} that denotes the @nt{generic_declaration} denotes the
137 current instance.
138 @begin{TheProof}
139 This is stated officially as part of the @lquotes@;current instance@rquotes@;
140 rule in @RefSec{The Context of Overload Resolution}.
141 See also @RefSec{Generic Instantiation}.
142 @end{TheProof}
143
144 Within a generic @nt{subprogram_body}, the name of this program unit
145 acts as the name of a subprogram. Hence this name can be overloaded,
146 and it can appear in a recursive call of the current instance. For the
147 same reason, this name cannot appear after the reserved word @key{new}
148 in a (recursive) @nt{generic_instantiation}.
149
150 A @nt{default_expression} or @nt{default_name} appearing in a
151 @nt{generic_formal_part} is not evaluated during elaboration of the
152 @nt{generic_formal_part}; instead, it is evaluated when used.
153 (The usual visibility rules apply to any @nt{name} used in a default:
154 the denoted declaration therefore has to be visible at the place of the
155 expression.)
156 @end{Notes}
157
158 @begin{Examples}
159 @leading@keepnext@i{Examples of generic formal parts:}
160 @begin{Example}
161 @key[generic] --@RI{ parameterless }
162
163 @key[generic]
164 Size : Natural; --@RI{ formal object }
165
166 @key[generic]
167 Length : Integer := 200; --@RI{ formal object with a default expression}
168
169 Area : Integer := Length*Length; --@RI{ formal object with a default expression}
170
171 @key[generic]
172 @key[type] Item @key[is] @key[private]; --@RI{ formal type}
173 @key[type] Index @key[is] (<>); --@RI{ formal type}
174 @key[type] Row @key[is] @key[array](Index @key[range] <>) @key[of] Item; --@RI{ formal type}
175 @key[with] @key[function] "<"(X, Y : Item) @key[return] Boolean; --@RI{ formal subprogram }
176 @end{Example}
177
178 @begin{WideAbove}
179 @leading@keepnext@i{Examples of generic declarations declaring generic subprograms
180 Exchange and Squaring:}
181 @end{WideAbove}
182 @begin{Example}
183 @key[generic]
184 @key[type] Elem @key[is] @key[private];
185 @key[procedure] Exchange(U, V : @key[in] @key[out] Elem);
186
187 @key[generic]
188 @key[type] Item @key[is] @key[private];
189 @key[with] @key[function] "*"(U, V : Item) @key[return] Item @key[is] <>;
190 @key[function] Squaring(X : Item) @key[return] Item;
191 @end{Example}
192
193 @begin{WideAbove}
194 @leading@keepnext@i{Example of a generic declaration declaring a generic package:}
195 @end{WideAbove}
196 @begin{Example}
197 @key[generic]
198 @key[type] Item @key[is] @key[private];
199 @key[type] Vector @key[is] @key[array] (Positive @key[range] <>) @key[of] Item;
200 @key[with] @key[function] Sum(X, Y : Item) @key[return] Item;
201 @key[package] On_Vectors @key[is]
202 @key[function] Sum (A, B : Vector) @key[return] Vector;
203 @key[function] Sigma(A : Vector) @key[return] Item;
204 Length_Error : @key[exception];
205 @key[end] On_Vectors;
206 @end{Example}
207 @end{Examples}
208
209 @begin{Extend83}
210 @Defn{extensions to Ada 83}
211 The syntax rule for @nt{generic_formal_parameter_declaration} is modified
212 to allow the reserved words @key{tagged} and @key{abstract}, to allow
213 formal derived types, and to allow formal packages.
214
215 @nt{Use_clause}s are allowed in @nt{generic_formal_part}s.
216 This is necessary in order to allow a @nt{use_clause} within a formal
217 part to provide direct visibility of declarations within a generic
218 formal package.
219 @end{Extend83}
220
221 @begin{DiffWord83}
222 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0299-1]}
223 The syntax for @nt{generic_formal_parameter_declaration} and
224 @nt{formal_type_definition} is split up into more named categories.
225 The rules for these categories are moved to the appropriate
226 @Chg{Version=[3],New=[],Old=[clauses and ]}subclauses.
227 The names of the categories are changed to be more intuitive and uniform.
228 For example, we changed @ntf{generic_parameter_declaration} to
229 @nt{generic_formal_parameter_declaration}, because the thing it declares
230 is a generic formal, not a generic.
231 In the others, we abbreviate @lquotes@;generic_formal@rquotes@; to just @lquotes@;formal@rquotes@;.
232 We can't do that for @nt{generic_formal_parameter_declaration},
233 because of confusion with normal formal parameters of subprograms.
234 @end{DiffWord83}
235
236 @begin{Extend2005}
237 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
238 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
239 An optional @nt{aspect_specification} can be used in a
240 @nt{generic_subprogram_declaration} (as well as a @nt{generic_package_declaration}).
241 This is described in @RefSecNum{Aspect Specifications}.]}
242 @end{Extend2005}
243
244
245 @RmNewPage@Comment{Insert page break so printed RM's look better.}
246 @LabeledClause{Generic Bodies}
247
248 @begin{Intro}
249 @Defn{generic body}
250 The body of a generic unit (a @i{generic body})
251 @Redundant[is a template for the instance bodies.
252 The syntax of a generic body is identical to that of a nongeneric body].
253 @begin{Ramification}
254 We also use terms like @lquotes@;generic function body@rquotes@; and
255 @lquotes@;nongeneric package body.@rquotes@;
256 @end{Ramification}
257 @end{Intro}
258
259 @begin{RunTime}
260 @PDefn2{Term=[elaboration], Sec=(generic body)}
261 The elaboration of a generic body has no other effect than to
262 establish that the generic unit can from then on be instantiated without
263 failing the Elaboration_Check.
264 If the generic body is a child of a generic package,
265 then its elaboration establishes that each corresponding
266 declaration nested in an instance of the parent
267 (see @RefSecNum{Compilation Units - Library Units})
268 can from then on be instantiated without failing the Elaboration_Check.
269 @end{RunTime}
270
271 @begin{Notes}
272 The syntax of generic subprograms implies that a generic subprogram body
273 is always the completion of a declaration.
274 @end{Notes}
275
276 @begin{Examples}
277 @leading@keepnext@i{Example of a generic procedure body:}
278 @begin{Example}
279 @key[procedure] Exchange(U, V : @key[in] @key[out] Elem) @key[is] --@RI{ see @RefSecNum{Generic Declarations}}
280 T : Elem; --@RI{ the generic formal type}
281 @key[begin]
282 T := U;
283 U := V;
284 V := T;
285 @key[end] Exchange;
286 @end{Example}
287
288 @begin{WideAbove}
289 @leading@keepnext@i{Example of a generic function body:}
290 @end{WideAbove}
291 @begin{Example}
292 @key[function] Squaring(X : Item) @key[return] Item @key[is] --@RI{ see @RefSecNum{Generic Declarations}}
293 @key[begin]
294 @key[return] X*X; --@RI{ the formal operator "*"}
295 @key[end] Squaring;
296 @end{Example}
297
298 @begin{WideAbove}
299 @leading@keepnext@i{Example of a generic package body:}
300 @end{WideAbove}
301 @begin{Example}
302 @key[package] @key[body] On_Vectors @key[is] --@RI{ see @RefSecNum{Generic Declarations}}
303
304 @key[function] Sum(A, B : Vector) @key[return] Vector @key[is]
305 Result : Vector(A'Range); --@RI{ the formal type Vector}
306 Bias : @key[constant] Integer := B'First - A'First;
307 @key[begin]
308 @key[if] A'Length /= B'Length @key[then]
309 @key[raise] Length_Error;
310 @key[end] @key[if];
311
312 @key[for] N @key[in] A'Range @key[loop]
313 Result(N) := Sum(A(N), B(N + Bias)); --@RI{ the formal function Sum}
314 @key[end] @key[loop];
315 @key[return] Result;
316 @key[end] Sum;
317
318 @key[function] Sigma(A : Vector) @key[return] Item @key[is]
319 Total : Item := A(A'First); --@RI{ the formal type Item}
320 @key[begin]
321 @key[for] N @key[in] A'First + 1 .. A'Last @key[loop]
322 Total := Sum(Total, A(N)); --@RI{ the formal function Sum}
323 @key[end] @key[loop];
324 @key[return] Total;
325 @key[end] Sigma;
326 @key[end] On_Vectors;
327 @end{Example}
328 @end{Examples}
329
330
331 @ISOOnlyRMNewPageVer{Version=[3]}@Comment{For ISO version of Ada 2012 Standard}
332 @LabeledClause{Generic Instantiation}
333
334 @begin{Intro}
335 @redundant[@Defn2{Term=[instance], Sec=(of a generic unit)}
336 An instance of a generic unit is declared by a
337 @nt{generic_instantiation}.]
338 @end{Intro}
339
340 @begin{MetaRules}
341 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
342 @Defn{generic contract model}
343 @Defn{contract model of generics}
344 The legality of an instance should be determinable without looking at
345 the generic body.
346 Likewise, the legality of a generic body should be determinable without
347 looking at any instances.
348 Thus, the @nt{generic_declaration} forms a contract between the body and
349 the instances; if each obeys the rules with respect to the
350 @nt{generic_declaration}, then no legality problems will arise.
351 This is really a special case of the
352 @lquotes@;legality determinable via semantic dependences@rquotes@;
353 @MetaRulesName (see @Chg{Version=[3],New=[Clause],Old=[Section]}
354 @RefSecNum{Program Structure and Compilation Issues}),
355 given that a @nt{generic_instantiation} does not depend semantically
356 upon the generic body, nor vice-versa.
357
358 Run-time issues are another story.
359 For example, whether parameter passing is by copy or by reference is
360 determined in part by the properties of the generic actuals,
361 and thus cannot be determined at compile time of the generic body.
362 Similarly, the contract model does not apply to @LinkTimeTitle.
363 @end{MetaRules}
364
365 @begin{Syntax}
366 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00218-03]}
367 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
368 @Syn{lhs=<generic_instantiation>,rhs="
369 @key{package} @Syn2{defining_program_unit_name} @key{is}
370 @key{new} @SynI{generic_package_}@Syn2{name} [@Syn2{generic_actual_part}]@Chg{Version=[3],New=<
371 [@Syn2{aspect_specification}]>,Old=[]};
372 | @Chg{Version=[2],New=<[@Syn2{overriding_indicator}]
373 >,Old=<>}@key{procedure} @Syn2{defining_program_unit_name} @key{is}
374 @key{new} @SynI{generic_procedure_}@Syn2{name} [@Syn2{generic_actual_part}]@Chg{Version=[3],New=<
375 [@Syn2{aspect_specification}]>,Old=[]};
376 | @Chg{Version=[2],New=<[@Syn2{overriding_indicator}]
377 >,Old=<>}@key{function} @Syn2{defining_designator} @key{is}
378 @key{new} @SynI{generic_function_}@Syn2{name} [@Syn2{generic_actual_part}]@Chg{Version=[3],New=<
379 [@Syn2{aspect_specification}]>,Old=[]};"}
380
381
382 @Syn{lhs=<generic_actual_part>,rhs="
383 (@Syn2{generic_association} {, @Syn2{generic_association}})"}
384
385 @Syn{lhs=<generic_association>,rhs="
386 [@SynI{generic_formal_parameter_}@Syn2{selector_name} =>] @Syn2{explicit_generic_actual_parameter}"}
387
388 @Syn{lhs=<explicit_generic_actual_parameter>,rhs="@Syn2{expression} | @SynI{variable_}@Syn2{name}
389 | @SynI{subprogram_}@Syn2{name} | @SynI{entry_}@Syn2{name} | @Syn2{subtype_mark}
390 | @SynI{package_instance_}@Syn2{name}"}
391
392 @begin{SyntaxText}
393 @Defn{named association}
394 @Defn{positional association}
395 A @nt{generic_association} is @i{named} or @i{positional}
396 according to whether or not the @i{generic_@!formal_@!parameter_}@!@nt<selector_@!name>
397 is specified. Any positional associations shall precede any
398 named associations.
399 @end{SyntaxText}
400 @end{Syntax}
401
402 @begin{Intro}
403 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0004-1]}
404 @Defn{generic actual parameter}
405 @Defn{generic actual}
406 @Defn{actual}
407 The @i{generic actual parameter} is either the
408 @nt{explicit_generic_actual_parameter} given in a
409 @Chg{Version=[3],New=[@nt{generic_@!association}],Old=[@ntf{generic_@!parameter_@!association}]}
410 for each formal,
411 or the corresponding @nt{default_@!expression} or @nt{default_@!name} if no
412 @Chg{Version=[3],New=[@nt{generic_@!association}],Old=[@ntf{generic_@!parameter_@!association}]}
413 is given for the formal.
414 When the meaning is clear from context,
415 the term @lquotes@;generic actual,@rquotes@; or simply @lquotes@;actual,@rquotes@; is used as a synonym for
416 @lquotes@;generic actual parameter@rquotes@;
417 and also for the view denoted by one, or the value of one.
418 @end{Intro}
419
420 @begin{Legality}
421 In a @nt<generic_instantiation> for a particular kind of program
422 unit @Redundant[(package, procedure, or function)],
423 the @nt<name> shall denote a generic
424 unit of the corresponding kind @Redundant[(generic package,
425 generic procedure, or generic
426 function, respectively)].
427
428 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0118-1]}
429 The @SynI{generic_formal_parameter_}@nt{selector_name} of
430 a@Chg{Version=[3],New=[ named],Old=[]}
431 @nt{generic_association} shall denote a
432 @nt{generic_formal_parameter_declaration}
433 of the generic unit being instantiated.
434 If two or more formal subprograms have the same defining name, then
435 named associations are not allowed for the corresponding
436 actuals.
437
438 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0118-1]}
439 @ChgAdded{Version=[3],Text=[The @nt{generic_formal_parameter_declaration}
440 for a positional @nt{generic_association} is the parameter with the
441 corresponding position in the @nt{generic_formal_part} of the
442 generic unit being instantiated.]}
443
444 A @nt{generic_instantiation} shall contain at
445 most one @nt<generic_association> for each formal.
446 Each formal without an association shall have a
447 @nt{default_expression} or @nt{subprogram_default}.
448
449 In a generic unit @LegalityName@;s
450 are enforced at compile time of the
451 @nt{generic_declaration} and generic body,
452 given the properties of the formals.
453 In the visible part and formal part of an instance,
454 @LegalityName@;s are enforced at
455 compile time of the @nt{generic_instantiation},
456 given the properties of the actuals.
457 In other parts of an instance, @LegalityName@;s
458 are not enforced;
459 this rule does not apply when a given rule
460 explicitly specifies otherwise.
461 @begin{Reason}
462 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
463 Since rules are checked using the properties of the formals,
464 and since these properties do not always carry over to the actuals,
465 we need to check the rules again in the visible part of the instance.
466 For example, only if a tagged type is limited may
467 an extension of it have limited components in
468 the @Chg{Version=[2],New=[@nt{record_extension_part}],Old=[@ntf<extension_part>]}.
469 A formal tagged limited type
470 is limited, but the actual might be nonlimited. Hence
471 any rule that requires a tagged type to be limited
472 runs into this problem.
473 Such rules are rare; in most cases, the rules for matching of formals
474 and actuals guarantee that if the rule is obeyed in the generic unit,
475 then it has to be obeyed in the instance.
476
477 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0005-1]}
478 @ChgAdded{Version=[3],Text=[Ada 2012 addendum: Such @LegalityTitle are not
479 as rare as the authors of Ada 95 hoped; there are more than 30 of them known
480 at this point. They are indexed under "generic contract issue" and
481 are associated with the boilerplate "In addition to the places where
482 @LegalityTitle normally apply...". Indeed, there is only one known rule
483 where rechecking in the specification is needed and where rechecking in the
484 private part is @i<not> wanted (it is in @RefSecNum{Derived Types and Classes},
485 but even it needs rechecking when tagged types are involved).]}
486 @end{Reason}
487 @begin{Ramification}
488 @leading@;The @lquotes@;properties@rquotes@; of the formals are determined
489 without knowing anything about the actuals:
490 @begin{Itemize}
491 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0095],ARef=[AI95-00034-01]}
492 A formal derived subtype is constrained if and only if the ancestor
493 subtype is constrained.
494 A formal array type is constrained if and only if the declarations
495 @Chg{New=[say],Old=[says]}
496 so.@Chg{New=[ A formal private type is constrained if it does not have a
497 discriminant part.],Old=[]}
498 Other formal subtypes are unconstrained,
499 even though they might be constrained in an instance.
500
501 A formal subtype can be indefinite,
502 even though the copy might be definite in an instance.
503
504 A formal object of mode @key[in] is not a static constant;
505 in an instance, the copy is static if the actual is.
506
507 A formal subtype is not static,
508 even though the actual might be.
509
510 Formal types are specific,
511 even though the actual can be class-wide.
512
513 The subtype of a formal object of mode @key[in out]
514 is not static.
515 (This covers the case of AI83-00878.)
516
517 The subtype of a formal parameter of
518 a formal subprogram does not
519 provide an applicable index constraint.
520
521 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0239-1]}
522 The profile of a formal subprogram is not @Chg{Version=[3],New=[subtype conformant],Old=[subtype-conformant]}
523 with any other profile.
524 @Defn{subtype conformance}
525
526 A generic formal function is not static.
527 @end{Itemize}
528 @end{Ramification}
529 @begin{Ramification}
530 @leading@;The exceptions to the above rule about when legality rules are
531 enforced fall into these categories:
532 @begin{Itemize}
533 @leading@;Some rules are checked in the generic declaration, and then again
534 in both the visible and private parts of the instance:
535 @begin{InnerItemize}
536 The parent type of a record extension has to be specific
537 (see @RefSecNum{Type Extensions}).
538 This rule is not checked in the instance body.
539
540 The parent type of a private extension has to be specific
541 (see @RefSecNum{Private Types and Private Extensions}).
542 This rule is not checked in the instance body.
543
544 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00402-01]}
545 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0093-1]}
546 A type with an access discriminant @Chg{Version=[2],New=[with a
547 @nt{default_expression} ],Old=[]}has to be @Chg{Version=[3],New=[immutably
548 limited. In the generic body, the definition of immutably limited is adjusted
549 in an assume-the-worst manner (thus the rule is checked that way)],Old=[a
550 descendant of @Chg{Version=[2],New=[an explicitly limited record type],Old=[a
551 type declared with @key[limited]]}, or be a task or protected
552 type. This rule is irrelevant in the instance body]}.
553
554 In the declaration of a record extension,
555 if the parent type is nonlimited, then each of the
556 components of the @nt{record_extension_part} have to be nonlimited
557 (see @RefSecNum{Type Extensions}).
558 In the generic body, this rule is checked in an assume-the-worst
559 manner.
560
561 A preelaborated library unit has to be preelaborable
562 (see @RefSecNum{Elaboration Control}).
563 In the generic body, this rule is checked in an assume-the-worst
564 manner.
565 @end{InnerItemize}
566
567 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00402-01]}
568 @ChgAdded{Version=[2],NoPrefix=[T],Text=[The corrections made by the
569 Corrigendum added a number of such rules, and the Amendment added many more.
570 There doesn't seem to be much value in repeating all of these rules here (as
571 of this writing, there are roughly 33 such rules).
572 As noted below, all such rules are indexed in the AARM.]}
573
574 @PDefn2{Term=[accessibility rule],Sec=(checking in generic units)}
575 For the accessibility rules,
576 the formals have nothing to say about the property in question.
577 Like the above rules, these rules are checked in the generic declaration,
578 and then again in both the visible and private parts of
579 the instance.
580 In the generic body, we have explicit rules that essentially
581 assume the worst (in the cases of type extensions and
582 access-to-subprogram types),
583 and we have run-time checks
584 (in the case of access-to-object types).
585 See @RefSecNum{Type Extensions},
586 @RefSecNum{Operations of Access Types},
587 and @RefSecNum{Type Conversions}.
588
589 @NoPrefix@;We considered run-time checks for access-to-subprogram types as well.
590 However, this would present difficulties for
591 implementations that share generic bodies.
592
593 The rules requiring @lquotes@;reasonable@rquotes@; values for static expressions are
594 ignored when the expected type for the expression is a descendant of a
595 generic formal type other than a generic formal derived type,
596 and do not apply in an instance.
597
598 The rule forbidding two explicit homographs in the same declarative
599 region does not apply in an instance of a generic unit,
600 except that it @i{does} apply in the declaration of a record extension
601 that appears in the visible part of an instance.
602
603 @leading@;Some rules do not apply at all in an instance,
604 not even in the visible part:
605 @begin{InnerItemize}
606 @nt{Body_stub}s are not normally allowed to be multiply nested,
607 but they can be in instances.
608 @end{InnerItemize}
609 @end{Itemize}
610
611 @RootDefn{generic contract issue}
612 Each rule that is an exception is marked with
613 @lquotes@;generic contract issue;@rquotes@; look that up in the index to find them all.
614 @end{Ramification}
615 @begin{Ramification}
616 The @LegalityName@;s are the ones labeled @LegalityTitle.
617 We are talking about all @LegalityName@;s in the entire language here.
618 Note that, with some exceptions,
619 the legality of a generic unit is checked even if there are no
620 instantiations of the generic unit.
621 @end{Ramification}
622 @begin{Ramification}
623 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
624 The @LegalityName@;s are described here, and
625 the overloading rules were described earlier in this @Chg{Version=[3],New=[subclause],Old=[clause]}.
626 Presumably, every @StaticSemName is sucked in by one of those.
627 Thus, we have covered all the compile-time rules of the language.
628 There is no need to say anything special about the @LinkTimeName@;s
629 or the @RunTimeName@;s.
630 @end{Ramification}
631 @begin{Discussion}
632 Here is an example illustrating how this rule is checked:
633 @lquotes@;In the declaration of a record extension,
634 if the parent type is nonlimited, then each of the
635 components of the @nt{record_extension_part} shall be nonlimited.@rquotes@;
636 @begin{Example}
637 @key[generic]
638 @key[type] Parent @key[is] @key[tagged] @key[private];
639 @key[type] Comp @key[is] @key[limited] @key[private];
640 @key[package] G1 @key[is]
641 @key[type] Extension @key[is] @key[new] Parent @key[with]
642 @key[record]
643 C : Comp; --@RI{ Illegal!}
644 @key[end] @key[record];
645 @key[end] G1;
646 @end{Example}
647
648 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{Presentation AI-00114}
649 The parent type is nonlimited, and the component type is limited,
650 which is illegal.
651 It doesn't matter that @Chg{New=[],Old=[an ]}one could imagine writing an
652 instantiation with the actual for Comp being nonlimited @em we never get to
653 the instance, because the generic itself is illegal.
654
655 @leading@;On the other hand:
656 @begin{Example}
657 @key[generic]
658 @key[type] Parent @key[is] @key[tagged] @key[limited] @key[private]; --@RI{ Parent is limited.}
659 @key[type] Comp @key[is] @key[limited] @key[private];
660 @key[package] G2 @key[is]
661 @key[type] Extension @key[is] @key[new] Parent @key[with]
662 @key[record]
663 C : Comp; --@RI{ OK.}
664 @key[end] @key[record];
665 @key[end] G2;
666
667 @key[type] Limited_Tagged @key[is] @key[tagged] @key[limited] @key[null] @key[record];
668 @key[type] Non_Limited_Tagged @key[is] @key[tagged] @key[null] @key[record];
669
670 @key[type] Limited_Untagged @key[is] @key[limited] @key[null] @key[record];
671 @key[type] Non_Limited_Untagged @key[is] @key[null] @key[record];
672
673 @key[package] Good_1 @key[is] @key[new] G2(Parent => Limited_Tagged,
674 Comp => Limited_Untagged);
675 @key[package] Good_2 @key[is] @key[new] G2(Parent => Non_Limited_Tagged,
676 Comp => Non_Limited_Untagged);
677 @key[package] Bad @key[is] @key[new] G2(Parent => Non_Limited_Tagged,
678 Comp => Limited_Untagged); --@RI{ Illegal!}
679 @end{Example}
680
681 The first instantiation is legal,
682 because in the instance the parent is limited,
683 so the rule is not violated.
684 Likewise, in the second instantiation,
685 the rule is not violated in the instance.
686 However, in the Bad instance,
687 the parent type is nonlimited,
688 and the component type is limited,
689 so this instantiation is illegal.
690 @end{Discussion}
691 @end{Legality}
692
693 @begin{StaticSem}
694 A @nt{generic_instantiation} declares an instance;
695 it is equivalent to the instance declaration (a @nt{package_@!declaration}
696 or @nt{subprogram_@!declaration}) immediately followed by the instance body,
697 both at the place of the instantiation.
698 @begin{Ramification}
699 The declaration and the body of the instance are not @lquotes@;implicit@rquotes@;
700 in the technical sense, even though you can't see them in the program text.
701 Nor are declarations within an instance @lquotes@;implicit@rquotes@;
702 (unless they are implicit by other rules).
703 This is necessary because implicit declarations have special semantics
704 that should not be attached to instances.
705 For a generic subprogram,
706 the profile of a @nt{generic_instantiation} is that of the instance
707 declaration, by the stated equivalence.
708 @end{Ramification}
709 @begin{Ramification}
710 @PDefn2{Term=[visible part], Sec=(of an instance)}
711 @PDefn2{Term=[private part], Sec=(of a package)}
712 The visible and private parts of a package instance are defined in
713 @RefSec{Package Specifications and Declarations}
714 and @RefSec{Formal Packages}.
715 The visible and private parts of a subprogram instance are defined in
716 @RefSec{Scope of Declarations}.
717 @end{Ramification}
718
719 The instance is a copy of the text of the template.
720 @Redundant[Each use of a formal parameter
721 becomes (in the copy) a use of the actual, as explained below.]
722 @Defn{package instance}
723 @Defn{subprogram instance}
724 @Defn{procedure instance}
725 @Defn{function instance}
726 @Defn2{Term=[instance], Sec=(of a generic package)}
727 @Defn2{Term=[instance], Sec=(of a generic subprogram)}
728 @Defn2{Term=[instance], Sec=(of a generic procedure)}
729 @Defn2{Term=[instance], Sec=(of a generic function)}
730 An instance of a generic package is a package,
731 that of a generic procedure is a procedure, and that of a generic
732 function is a function.
733 @begin{Ramification}
734 An instance is a package or subprogram
735 (because we say so),
736 even though it contains a copy of the @nt{generic_formal_part},
737 and therefore doesn't look like one.
738 This is strange, but it's OK, since the syntax rules are overloading
739 rules, and therefore do not apply in an instance.
740 @end{Ramification}
741 @begin{Discussion}
742 We use a macro-expansion model, with some explicitly-stated
743 exceptions (see below).
744 The main exception is that the interpretation of each construct in a
745 generic unit
746 (especially including the denotation of each name)
747 is determined when the declaration and body of the generic unit
748 (as opposed to the instance) are compiled,
749 and in each instance this interpretation is (a copy of) the template
750 interpretation.
751 In other words, if a construct is interpreted as a @nt{name} denoting
752 a declaration D, then in an instance, the copy of the construct will
753 still be a name, and will still denote D (or a copy of D).
754 From an implementation point of view,
755 overload resolution is performed on the template,
756 and not on each copy.
757
758 We describe the substitution of generic actual parameters by saying
759 (in most cases) that the copy of each generic formal parameter
760 declares a view of the actual.
761 Suppose a name in a generic unit denotes a
762 @nt{generic_formal_parameter_declaration}.
763 The copy of that name in an instance will denote the copy of that
764 @nt{generic_formal_parameter_declaration} in the instance.
765 Since the @nt{generic_formal_parameter_declaration} in the instance
766 declares a view of the actual,
767 the name will denote a view of the actual.
768
769 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
770 Other properties of the copy (for example, staticness,
771 @Chg{Version=[2],New=[categories],Old=[classes]} to
772 which types belong) are recalculated for each instance;
773 this is implied by the fact that it's a copy.
774
775 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00317-01]}
776 Although the @nt{generic_formal_part} is included in an instance,
777 the declarations in the @nt{generic_formal_part} are only visible
778 outside the instance in the case of a generic formal package whose
779 @nt{formal_package_actual_part} @Chg{Version=[2],New=[includes one or more
780 <> indicators],Old=[is (<>)]}
781 @em see @RefSecNum{Formal Packages}.
782 @end{Discussion}
783
784 The interpretation
785 of each construct within a generic declaration or body is determined
786 using the overloading rules
787 when that generic declaration or body is compiled.
788 In an instance, the interpretation of each (copied) construct is the
789 same,
790 except in the case of a name that denotes the
791 @nt{generic_declaration}
792 or some declaration within the generic unit;
793 the corresponding name in the instance then denotes the corresponding copy
794 of the denoted declaration.
795 The overloading rules do not apply in the instance.
796 @begin{Ramification}
797 See @RefSec{The Context of Overload Resolution} for definitions of
798 @lquotes@;interpretation@rquotes@; and @lquotes@;overloading rule.@rquotes@;
799
800 Even the @nt{generic_formal_parameter_declaration}s have corresponding
801 declarations in the instance,
802 which declare views of the actuals.
803
804 Although the declarations in the instance are copies of those in the
805 generic unit, they often have quite different properties,
806 as explained below.
807 For example a constant declaration in the generic unit might declare
808 a nonstatic constant, whereas the copy of that declaration might
809 declare a static constant.
810 This can happen when the staticness depends on some generic formal.
811
812 This rule is partly a ramification of the @lquotes@;current instance@rquotes@;
813 rule in @RefSec{The Context of Overload Resolution}.
814 Note that that rule doesn't cover the @nt{generic_formal_part}.
815
816 Although the overloading rules are not observed in the instance,
817 they are, of course, observed in the @ntf{_instantiation} in order to
818 determine the interpretation of the constituents of the
819 @ntf{_instantiation}.
820
821 Since children are considered to occur within their parent's
822 declarative region, the above rule applies to a name that denotes a
823 child of a generic unit, or a declaration inside such a child.
824
825 Since the @SyntaxName@;s are overloading rules,
826 it is possible (legal) to violate them in an instance.
827 For example, it is possible for an instance body to occur in a
828 @nt{package_specification}, even though the @SyntaxName@;s forbid bodies
829 in @nt{package_specification}s.
830 @end{Ramification}
831
832 In an instance,
833 a @nt{generic_formal_parameter_declaration} declares a view
834 whose properties are identical to those of the actual,
835 except as specified in
836 @RefSec{Formal Objects} and @RefSec{Formal Subprograms}.
837 Similarly, for a declaration within
838 a @nt{generic_formal_parameter_declaration},
839 the corresponding declaration in an instance declares a view whose
840 properties are identical to the corresponding declaration within the
841 declaration of the actual.
842 @begin{Ramification}
843 In an instance,
844 there are no @lquotes@;properties@rquotes@; of types and subtypes
845 that come from the formal.
846 The primitive operations of the type come from the
847 formal, but these are declarations in their own right,
848 and are therefore handled separately.
849
850 Note that certain properties that come from the actuals are
851 irrelevant in the instance.
852 For example, if an actual type is of a class deeper in the
853 derived-type hierarchy than the formal,
854 it is impossible to call the additional operations of the deeper class
855 in the instance,
856 because any such call would have to be a copy of some corresponding call
857 in the generic unit,
858 which would have been illegal.
859 However, it is sometimes possible to reach into the specification of
860 the instance from outside, and notice such properties.
861 For example, one could pass an object declared in the instance
862 specification to one of the additional operations of the deeper type.
863
864 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
865 A @nt{formal_type_declaration} can contain
866 @nt{discriminant_specification}s,
867 a @nt{formal_subprogram_declaration} can contain
868 @Chg{Version=[2],New=[@nt{parameter_specification}s],Old=[@ntf{formal_parameter_specification}s]}, and
869 a @nt{formal_package_declaration} can contain many kinds of
870 declarations.
871 These are all inside the generic unit, and have corresponding
872 declarations in the instance.
873
874 This rule implies, for example, that if a subtype in a generic unit
875 is a subtype of a generic formal subtype,
876 then the corresponding subtype in the instance is a subtype of the
877 corresponding actual subtype.
878
879 For a @nt{generic_instantiation},
880 if a generic actual is a static @Redundant[(scalar or string)] subtype,
881 then each use of the corresponding formal parameter within the
882 specification of the instance is considered to be static.
883 (See AI83-00409.)
884
885 Similarly, if a generic actual is a static expression and
886 the corresponding formal parameter has a
887 static @Redundant[(scalar or string)] subtype,
888 then each use of the formal parameter in the specification of the
889 instance is considered to be static.
890 (See AI83-00505.)
891
892 @leading@;If a primitive subprogram of a type derived from a generic formal
893 derived tagged type is not overriding (that is, it is a new
894 subprogram), it is possible for the copy of that subprogram in
895 an instance to override a subprogram inherited from the actual.
896 For example:
897 @begin{Example}
898 @key[type] T1 @key[is] @key[tagged] @key[record] ... @key[end] @key[record];
899
900 @key[generic]
901 @key[type] Formal @key[is] @key[new] T1;
902 @key[package] G @key[is]
903 @key[type] Derived_From_Formal @key[is] @key[new] Formal @key[with] @key[record] ... @key[end] @key[record];
904 @key[procedure] Foo(X : @key[in] Derived_From_Formal); --@RI{ Does not override anything.}
905 @key[end] G;
906
907 @key[type] T2 @key[is] @key[new] T1 @key[with] @key[record] ... @key[end] @key[record];
908 @key[procedure] Foo(X : @key[in] T2);
909
910 @key[package] Inst @key[is] @key[new] G(Formal => T2);
911 @end{Example}
912
913 In the instance Inst,
914 the declaration of Foo for Derived_From_Formal
915 overrides the Foo inherited from T2.
916 @end{Ramification}
917 @begin{ImplNote}
918 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0009],ARef=[AI95-00137-01]}
919 For formal types,
920 an implementation that shares the code among multiple instances of the
921 same generic unit needs to beware that things like parameter passing
922 mechanisms (by-copy vs. by-reference) and
923 @Chg{New=[@nt{aspect_clause}s],Old=[@nt{representation_clause}s]} are
924 determined by the actual.
925 @end{ImplNote}
926
927 @redundant[Implicit declarations are also copied,
928 and a name that denotes an implicit declaration in the generic
929 denotes the corresponding copy in the instance.
930 However, for a type declared within the visible part of the
931 generic, a whole new set of primitive subprograms
932 is implicitly declared for use outside the instance,
933 and may differ from the copied set if the properties of the
934 type in some way depend on the properties of some actual type
935 specified in the instantiation.
936 For example, if the type in the generic is derived
937 from a formal private type, then in the instance the type
938 will inherit subprograms from the corresponding actual type.
939
940 @Defn{override}
941 These new implicit declarations occur immediately after the type
942 declaration in the instance, and override
943 the copied ones. The copied ones
944 can be called only from within the instance; the new
945 ones can be called only from outside the instance, although
946 for tagged types, the
947 body of a new one can be executed by a call to an old one.]
948 @begin{TheProof}
949 This rule is stated officially in @RefSec{Visibility}.
950 @end{TheProof}
951 @begin{Ramification}
952 The new ones follow from the class(es) of the formal types.
953 For example, for a type T derived from a generic formal private type,
954 if the actual is Integer, then the copy of T in the instance has a
955 "+" primitive operator,
956 which can be called from outside the instance
957 (assuming T is declared in the visible part of the instance).
958
959 AI83-00398.
960
961 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
962 Since an actual type is always in the
963 @Chg{Version=[2],New=[category],Old=[class]} determined for
964 the formal, the new subprograms hide all of the copied ones,
965 except for a declaration of "/=" that corresponds to an explicit
966 declaration of "=".
967 Such "/=" operators are special, because unlike other implicit
968 declarations of primitive subprograms, they do not appear by virtue
969 of the class, but because of an explicit declaration of "=".
970 If the declaration of "=" is implicit (and therefore overridden
971 in the instance), then a corresponding implicitly declared "/="
972 is also overridden.
973 But if the declaration of "=" is explicit (and therefore not overridden
974 in the instance), then a corresponding implicitly declared "/="
975 is not overridden either,
976 even though it's implicit.
977
978 Note that the copied ones can be called from inside the instance, even
979 though they are hidden from all visibility,
980 because the names are resolved in the generic unit @em visibility
981 is irrelevant for calls in the instance.
982 @end{Ramification}
983
984 @Redundant[In the visible part of an instance, an explicit declaration
985 overrides an implicit declaration if they are homographs,
986 as described in @RefSecNum{Visibility}.]
987 On the other hand, an explicit declaration in
988 the private part of an instance overrides an implicit
989 declaration in the instance, only if the corresponding explicit
990 declaration in the generic overrides a corresponding
991 implicit declaration in the generic.
992 Corresponding rules apply to the other kinds of overriding
993 described in @RefSecNum{Visibility}.
994 @begin{Ramification}
995 @leading@;For example:
996 @begin{Example}
997 @key[type] Ancestor @key[is] @key[tagged] @key[null] @key[record];
998
999 @key[generic]
1000 @key[type] Formal @key[is] @key[new] Ancestor @key[with] @key[private];
1001 @key[package] G @key[is]
1002 @key[type] T @key[is] @key[new] Formal @key[with] @key[null] @key[record];
1003 @key[procedure] P(X : @key[in] T); --@RI{ (1)}
1004 @key[private]
1005 @key[procedure] Q(X : @key[in] T); --@RI{ (2)}
1006 @key[end] G;
1007
1008 @key[type] Actual @key[is] @key[new] Ancestor @key[with] @key[null] @key[record];
1009 @key[procedure] P(X : @key[in] Actual);
1010 @key[procedure] Q(X : @key[in] Actual);
1011
1012 @key[package] Instance @key[is] @key[new] G(Formal => Actual);
1013 @end{Example}
1014
1015 In the instance, the copy of P at (1) overrides Actual's P,
1016 whereas the copy of Q at (2) does not override anything;
1017 in implementation terms, it occupies a separate slot in
1018 the type descriptor.
1019 @end{Ramification}
1020 @begin{Reason}
1021 The reason for this rule is so a programmer writing an
1022 @ntf{_instantiation} need not look at the private part of the generic in
1023 order to determine which subprograms will be overridden.
1024 @end{Reason}
1025 @end{StaticSem}
1026
1027 @begin{LinkTime}
1028 Recursive generic instantiation is not allowed in the following
1029 sense: if a given generic unit includes an instantiation of a second
1030 generic unit, then the instance generated by this instantiation shall
1031 not include an instance of the first generic unit
1032 @Redundant[(whether this instance is generated directly, or indirectly
1033 by intermediate instantiations)].
1034 @begin{Discussion}
1035 Note that this rule is not a violation of the generic contract model,
1036 because it is not a @LegalityName.
1037 Some implementations may be able to check this rule at compile time,
1038 but that requires access to all the bodies,
1039 so we allow implementations to check the rule at link time.
1040 @end{Discussion}
1041 @end{LinkTime}
1042
1043 @begin{RunTime}
1044 @PDefn2{Term=[elaboration], Sec=(generic_instantiation)}
1045 For the elaboration of a @nt{generic_instantiation},
1046 each @nt{generic_association} is first evaluated.
1047 If a default is used,
1048 an implicit @nt{generic_association} is assumed for this rule.
1049 These evaluations are done in an arbitrary order, except that the
1050 evaluation for a default actual takes place after the evaluation
1051 for another actual if the default includes a @nt{name}
1052 that denotes the other one.@PDefn2{Term=[arbitrary order],Sec=[allowed]}
1053 Finally, the instance declaration and body are elaborated.
1054 @begin{Ramification}
1055 Note that if the evaluation of a default depends on some side effect
1056 of some other evaluation,
1057 the order is still arbitrary.
1058 @end{Ramification}
1059
1060 @PDefn2{Term=[evaluation], Sec=(generic_association)}
1061 For the evaluation of a @nt{generic_association}
1062 the generic actual parameter is evaluated.
1063 Additional actions are performed in the case of a formal object of
1064 mode @key{in} (see @RefSecNum{Formal Objects}).
1065 @begin{Honest}
1066 Actually, the actual is evaluated only if evaluation is defined for that
1067 kind of construct @em we don't actually @lquotes@;evaluate@rquotes@; @nt{subtype_mark}s.
1068 @end{Honest}
1069 @end{RunTime}
1070
1071 @begin{Notes}
1072 If a formal type is not tagged, then the type is treated as
1073 an untagged type within the generic body.
1074 Deriving from such a type in a generic body is permitted;
1075 the new type does not get a new tag value,
1076 even if the actual is tagged.
1077 Overriding operations for such a derived type cannot be dispatched to
1078 from outside the instance.
1079 @begin{Ramification}
1080 If two overloaded subprograms declared in a generic package
1081 specification differ only by the (formal) type of their parameters and
1082 results, then there exist legal instantiations for which all calls of
1083 these subprograms from outside the instance are ambiguous. For example:
1084 @begin{Example}
1085 @key[generic]
1086 @key[type] A @key[is] (<>);
1087 @key[type] B @key[is] @key[private];
1088 @key[package] G @key[is]
1089 @key[function] Next(X : A) @key[return] A;
1090 @key[function] Next(X : B) @key[return] B;
1091 @key[end] G;
1092
1093 @key[package] P @key[is] @key[new] G(A => Boolean, B => Boolean);
1094 --@RI{ All calls of P.Next are ambiguous.}
1095 @end{Example}
1096 @end{Ramification}
1097 @begin{Ramification}
1098 @leading@;The following example illustrates some of the subtleties of the
1099 substitution of formals and actuals:
1100 @begin{Example}
1101 @key[generic]
1102 @key[type] T1 @key[is] @key[private];
1103 --@RI{ A predefined "=" operator is implicitly declared here:}
1104 --@RI{ function "="(Left, Right : T1) return Boolean;}
1105 --@RI{ Call this "="@-{1}.}
1106 @key[package] G @key[is]
1107 @key[subtype] S1 @key[is] T1; --@RI{ So we can get our hands on the type from}
1108 --@RI{ outside an instance.}
1109 @key[type] T2 @key[is] @key[new] T1;
1110 --@RI{ An inherited "=" operator is implicitly declared here:}
1111 --@RI{ function "="(Left, Right : T2) return Boolean;}
1112 --@RI{ Call this "="@-{2}.}
1113
1114 T1_Obj : T1 := ...;
1115 Bool_1 : Boolean := T1_Obj = T1_Obj;
1116
1117 T2_Obj : T2 := ...;
1118 Bool_2 : Boolean := T2_Obj = T2_Obj;
1119 @key[end] G;
1120 ...
1121
1122 @key[package] P @key[is]
1123 @key[type] My_Int @key[is] @key[new] Integer;
1124 --@RI{ A predefined "=" operator is implicitly declared here:}
1125 --@RI{ function "="(Left, Right : My_Int) return Boolean;}
1126 --@RI{ Call this "="@-{3}.}
1127 @key[function] "="(X, Y : My_Int) @key[return] Boolean;
1128 --@RI{ Call this "="@-{4}.}
1129 --@RI{ "="@-{3} is hidden from all visibility by "="@-{4}.}
1130 --@RI{ Nonetheless, "="@-{3} can @lquotes@;reemerge@rquotes@; in certain circumstances.}
1131 @key[end] P;
1132 @key[use] P;
1133 ...
1134 @key[package] I @key[is] @key[new] G(T1 => My_Int); --@RI{ "="@-{5} is declared in I (see below).}
1135 @key[use] I;
1136
1137 Another_T1_Obj : S1 := 13; --@RI{ Can't denote T1, but S1 will do.}
1138 Bool_3 : Boolean := Another_T1_Obj = Another_T1_Obj;
1139
1140 Another_T2_Obj : T2 := 45;
1141 Bool_4 : Boolean := Another_T2_Obj = Another_T2_Obj;
1142
1143 Double : T2 := T2_Obj + Another_T2_Obj;
1144 @end{Example}
1145
1146 In the instance I, there is a copy of "="@-{1} (call it "="@-{1i}) and
1147 "="@-{2} (call it "="@-{2i}).
1148 The "="@-{1i} and "="@-{2i} declare views of the predefined "=" of My_Int
1149 (that is, "="@-{3}).
1150 In the initialization of Bool_1 and Bool_2 in the generic unit G,
1151 the names "=" denote "="@-{1} and "="@-{2}, respectively.
1152 Therefore, the copies of these names in the instances
1153 denote "="@-{1i} and "="@-{2i}, respectively.
1154 Thus, the initialization of I.Bool_1 and I.Bool_2 call the predefined
1155 equality operator of My_Int;
1156 they will not call "="@-{4}.
1157
1158 The declarations "="@-{1i} and "="@-{2i} are hidden from all
1159 visibility.
1160 This prevents them from being called from outside the instance.
1161
1162 The declaration of Bool_3
1163 calls "="@-{4}.
1164
1165 The instance I also contains implicit declarations of the primitive
1166 operators of T2, such as "=" (call it "="@-{5}) and "+".
1167 These operations cannot be called from within the instance,
1168 but the declaration of Bool_4 calls "="@-{5}.
1169 @end{Ramification}
1170 @end{Notes}
1171
1172 @begin{Examples}
1173 @leading@keepnext@i{Examples of generic instantiations (see
1174 @RefSecNum{Generic Declarations}):}
1175 @begin{Example}
1176 @tabclear()@tabset(P49)
1177 @key[procedure] Swap @key[is] @key[new] Exchange(Elem => Integer);
1178 @key[procedure] Swap @key[is] @key[new] Exchange(Character); @\--@RI{ Swap is overloaded }
1179 @key[function] Square @key[is] @key[new] Squaring(Integer); @\--@RI{ "*" of Integer used by default}
1180 @key[function] Square @key[is] @key[new] Squaring(Item => Matrix, "*" => Matrix_Product);
1181 @key[function] Square @key[is] @key[new] Squaring(Matrix, Matrix_Product); --@RI{ same as previous }
1182
1183 @key[package] Int_Vectors @key[is] @key[new] On_Vectors(Integer, Table, "+");
1184 @end{Example}
1185
1186 @begin{WideAbove}
1187 @leading@keepnext@i{Examples of uses of instantiated units:}
1188 @end{WideAbove}
1189 @begin{Example}
1190 Swap(A, B);
1191 A := Square(A);
1192
1193 T : Table(1 .. 5) := (10, 20, 30, 40, 50);
1194 N : Integer := Int_Vectors.Sigma(T); --@RI{ 150 (see @RefSec{Generic Bodies} for the body of Sigma)}
1195
1196 @key[use] Int_Vectors;
1197 M : Integer := Sigma(T); --@RI{ 150}
1198 @end{Example}
1199 @end{Examples}
1200
1201 @begin{Inconsistent83}
1202 @Defn{inconsistencies with Ada 83}
1203 In Ada 83, all explicit actuals are evaluated before all defaults,
1204 and the defaults are evaluated in the order of the formal
1205 declarations.
1206 This ordering requirement is relaxed in Ada 95.
1207 @end{Inconsistent83}
1208
1209 @begin{Incompatible83}
1210 @Defn{incompatibilities with Ada 83}
1211 We have attempted to remove every violation of the contract model.
1212 Any remaining contract model violations should be considered bugs in
1213 the RM95.
1214 The unfortunate property of reverting to the predefined operators of
1215 the actual types is retained for upward compatibility.
1216 (Note that fixing this would require subtype conformance rules.)
1217 However, tagged types do not revert in this sense.
1218 @end{Incompatible83}
1219
1220 @begin{Extend83}
1221 @Defn{extensions to Ada 83}
1222 The syntax rule for @nt{explicit_generic_actual_parameter} is modified to allow a
1223 @SynI{package_instance_}@nt{name}.
1224 @end{Extend83}
1225
1226 @begin{DiffWord83}
1227 The fact that named associations cannot be used for two formal
1228 subprograms with the same defining name is moved to AARM-only material,
1229 because it is a ramification of other rules, and because it is not of
1230 interest to the average user.
1231
1232 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
1233 The rule that
1234 @lquotes@;An explicit @nt{explicit_generic_actual_parameter} shall not be supplied more
1235 than once for a given @Chg{Version=[2],New=[generic formal parameter],
1236 Old=[@ntf{generic_formal_parameter}]}@rquotes@;
1237 seems to be missing from RM83, although it was clearly the intent.
1238
1239 In the explanation that the instance is a copy of the template,
1240 we have left out RM83-12.3(5)'s @lquotes@;apart from the generic formal
1241 part@rquotes@;, because it seems that things in the formal part still need to
1242 exist in instances.
1243 This is particularly true for generic formal packages,
1244 where you're sometimes allowed to reach in and denote the formals of
1245 the formal package from outside it.
1246 This simplifies the explanation of what each name in an instance
1247 denotes: there are just two cases: the declaration can be inside or
1248 outside (where inside needs to include the generic unit itself).
1249 Note that the RM83 approach of listing many cases (see RM83-12.5(5-14))
1250 would have become even more unwieldy with the addition of generic formal
1251 packages, and the declarations that occur therein.
1252
1253 We have corrected the definition of the elaboration of a
1254 @nt{generic_instantiation} (RM83-12.3(17)); we don't elaborate
1255 entities, and the instance is not @lquotes@;implicit.@rquotes@;
1256
1257 In RM83, there is a rule saying the formal and actual shall match, and
1258 then there is much text defining what it means to match.
1259 Here, we simply state all the latter text as rules.
1260 For example, @lquotes@;A formal foo is matched by an actual greenish bar@rquotes@;
1261 becomes @lquotes@;For a formal foo, the actual shall be a greenish bar.@rquotes@;
1262 This is necessary to split the @ResolutionName@;s
1263 from the @LegalityName@;s.
1264 Besides, there's really no need to define the concept of matching for
1265 generic parameters.
1266 @end{DiffWord83}
1267
1268 @begin{Extend95}
1269 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00218-03]}
1270 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1271 An @nt{overriding_indicator} (see
1272 @RefSecNum{Overriding Indicators}) is allowed on a subprogram instantiation.]}
1273 @end{Extend95}
1274
1275 @begin{Extend2005}
1276 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
1277 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
1278 An optional @nt{aspect_specification} can be used in a @nt{generic_instantiation}.
1279 This is described in @RefSecNum{Aspect Specifications}.]}
1280 @end{Extend2005}
1281
1282 @begin{Diffword2005}
1283 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0118-1]}
1284 @ChgAdded{Version=[3],Text=[@b<Correction:> Added a definition for
1285 positional parameters, as this is missing from Ada 95 and Ada 2005.]}
1286 @end{Diffword2005}
1287
1288
1289 @LabeledClause{Formal Objects}
1290
1291 @begin{Intro}
1292 @redundant[@Defn{generic formal object}
1293 @Defn{formal object, generic}
1294 A generic formal object can be used to pass a value or variable
1295 to a generic unit.]
1296 @end{Intro}
1297
1298 @begin{MetaRules}
1299 A generic formal object of mode @key{in} is like a constant
1300 initialized to the value of the @nt{explicit_generic_actual_parameter}.
1301
1302 A generic formal object of mode @key{in out} is like a renaming
1303 of the @nt{explicit_generic_actual_parameter}.
1304 @end{MetaRules}
1305
1306 @begin{Syntax}
1307 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00423-01]}
1308 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1],ARef=[AI05-0183-1]}
1309 @Syn{lhs=<formal_object_declaration>,rhs="
1310 @Syn2{defining_identifier_list} : @Syn2{mode} @Chg{Version=[2],New=<[@Syn2{null_exclusion}] >,Old=<>}@Syn2{subtype_mark} [:= @Syn2{default_expression}]@Chg{Version=[3],New=<
1311 [@Syn2{aspect_specification}]>,Old=[]};@Chg{Version=[2],New=<
1312 @Chg{Version=[3],New=[|],Old=[ ]} @Syn2{defining_identifier_list} : @Syn2{mode} @Syn2{access_definition} [:= @Syn2{default_expression}]@Chg{Version=[3],New=<
1313 [@Syn2{aspect_specification}]>,Old=[]};>,Old=<>}"}
1314 @end{Syntax}
1315
1316 @begin{Resolution}
1317 @PDefn2{Term=[expected type],
1318 Sec=(generic formal object default_expression)}
1319 The expected type for the @nt{default_expression}, if any, of a formal
1320 object is the type of the formal object.
1321
1322
1323 @PDefn2{Term=[expected type], Sec=(generic formal in object actual)}
1324 For a generic formal object of mode @key[in],
1325 the expected type for the actual is the type of the formal.
1326
1327 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00423-01]}
1328 For a generic formal object of mode @key[in out],
1329 the type of the actual shall resolve to the type @Chg{Version=[2],
1330 New=[determined by the @nt{subtype_mark}, or for a
1331 @nt{formal_object_declaration} with an @nt{access_definition}, to a specific
1332 anonymous access type. If the anonymous access type is an access-to-object type,
1333 the type of the actual shall
1334 have the same designated type as that of the @nt{access_definition}.
1335 If the anonymous access type is an access-to-subprogram type, the type
1336 of the actual shall have a designated profile which
1337 is type conformant with that of the @nt{access_definition}.
1338 @Defn2{Term=[type conformance],Sec=(required)}],Old=[of the formal.]}
1339 @begin{Reason}
1340 See the corresponding rule for @nt{object_renaming_declaration}s for a
1341 discussion of the reason for this rule.
1342 @end{Reason}
1343
1344 @end{Resolution}
1345
1346 @begin{Legality}
1347 If a generic formal object has a @nt{default_expression},
1348 then the mode shall be @key{in}
1349 @Redundant[(either explicitly or by default)];
1350 otherwise, its mode shall be either @key{in} or @key{in out}.
1351 @begin{Ramification}
1352 Mode @key{out} is not allowed for generic formal objects.
1353 @end{Ramification}
1354
1355 For a generic formal object of mode @key{in}, the actual shall be an
1356 @nt{expression}.
1357 For a generic formal object of mode @key{in out}, the actual shall be
1358 a @nt{name} that denotes a variable for which renaming is allowed
1359 (see @RefSecNum{Object Renaming Declarations}).
1360 @begin{Honest}
1361 The part of this that requires an @nt{expression} or @nt{name} is a
1362 @ResolutionName,
1363 but that's too pedantic to worry about.
1364 (The part about denoting a variable, and renaming being allowed,
1365 is most certainly @i{not} a @ResolutionName.)
1366 @end{Honest}
1367
1368
1369 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00423-01]}
1370 @ChgAdded{Version=[2],Type=[Leading],Text=[]}@ChgNote{Conditional leading}
1371 @Chg{Version=[2],New=[In the case where the type of the formal is defined by an
1372 @nt{access_definition}, the type of the actual and the type of the formal:],
1373 Old=[The type of a generic formal object of mode
1374 @key{in} shall be nonlimited.]}
1375
1376 @begin{Itemize}
1377 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00423-01]}
1378 @ChgAdded{Version=[2],Text=[shall both be access-to-object types with
1379 statically matching designated subtypes and with both or neither being
1380 access-to-constant types; or
1381 @PDefn2{Term=[statically matching],Sec=(required)}]}
1382
1383 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00423-01]}
1384 @ChgAdded{Version=[2],Text=[shall both be access-to-subprogram types with
1385 subtype conformant designated profiles.
1386 @Defn2{Term=[subtype conformance],Sec=(required)}]}
1387 @end{Itemize}
1388
1389 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00423-01]}
1390 @ChgAdded{Version=[2],Type=[Leading],Text=[For a
1391 @nt{formal_object_declaration} with a @nt{null_exclusion} or an
1392 @nt{access_definition} that has a @nt{null_exclusion}:]}
1393
1394 @begin{Itemize}
1395 @ChgRef{Version=[2],Kind=[Added]}
1396 @ChgAdded{Version=[2],Text=[if the actual matching the
1397 @nt{formal_object_declaration} denotes the generic formal object
1398 of another generic unit @i{G}, and the instantiation containing the actual
1399 occurs within the body
1400 of @i{G} or within the body of a generic unit declared within the declarative
1401 region of @i{G}, then the declaration of the formal object of @i{G}
1402 shall have a @nt{null_exclusion};]}
1403
1404 @ChgRef{Version=[2],Kind=[Added]}
1405 @ChgAdded{Version=[2],Text=[otherwise, the subtype of the actual
1406 matching the @nt{formal_object_declaration} shall exclude null.
1407 @PDefn{generic contract issue}
1408 In addition to the places where @LegalityTitle normally apply
1409 (see @RefSecNum{Generic Instantiation}),
1410 this rule applies also in the private part of an
1411 instance of a generic unit.]}
1412
1413 @begin{Reason}
1414 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01],ARef=[AI95-00423-01]}
1415 @Chg{Version=[2],New=[This rule prevents @lquotes@;lying@rquotes.
1416 @b<Null> must never be the value of an object with an explicit
1417 @nt{null_exclusion}. The first bullet is an assume-the-worst rule
1418 which prevents trouble in generic bodies (including bodies of child
1419 units) when the subtype of the formal object excludes null implicitly.],
1420 Old=[Since a generic formal object is like a
1421 constant of mode @key{in} initialized to the value of the actual,
1422 a limited type would not make sense, since initializing a constant is
1423 not allowed for a limited type.
1424 That is, generic formal objects of mode @key{in} are passed by copy,
1425 and limited types are not supposed to be copied.]}
1426 @end{Reason}
1427 @end{Itemize}
1428
1429 @end{Legality}
1430
1431 @begin{StaticSem}
1432 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00255-01],ARef=[AI95-00423-01]}
1433 A @nt{formal_object_declaration} declares a generic formal object.
1434 The default mode is @key{in}.
1435 @PDefn2{Term=[nominal subtype], Sec=(of a generic formal object)}
1436 For a formal object of mode @key{in},
1437 the nominal subtype is the one denoted by the
1438 @nt{subtype_mark} @Chg{Version=[2],New=[or @nt{access_definition} ],Old=[]}in
1439 the declaration of the formal.
1440 @PDefn2{Term=[static], Sec=(subtype)}
1441 For a formal object of mode @key{in out}, its type
1442 is determined by the @nt<subtype_mark> @Chg{Version=[2],
1443 New=[or @nt{access_definition} ],Old=[]}in the declaration;
1444 its nominal subtype is nonstatic, even if the
1445 @nt<subtype_mark> denotes a static subtype@Chg{Version=[2],
1446 New=[; for a composite type, its nominal subtype is unconstrained if the first
1447 subtype of the type is unconstrained@Redundant[, even if the @nt{subtype_mark}
1448 denotes a constrained subtype]],Old=[]}.
1449 @begin{Reason}
1450 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00255-01]}
1451 @ChgAdded{Version=[2],Text=[We require that the subtype is
1452 unconstrained because a formal @key{in out} acts like a renaming, and
1453 thus the given subtype is ignored for purposes of matching; any value of
1454 the type can be passed. Thus we can assume only that the object is
1455 constrained if the first subtype is constrained (and thus there can be
1456 no unconstrained subtypes for the type). If we didn't do this, it
1457 would be possible to
1458 rename or take 'Access of components that could disappear due to an
1459 assignment to the whole object.]}
1460 @end{Reason}
1461 @begin{Discussion}
1462 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00423-01]}
1463 @ChgAdded{Version=[2],Text=[The two @lquotes@;even if@rquotes clauses are
1464 OK even though they don't mention @nt{access_definition}s; an access subtype
1465 can neither be a static subtype nor be a composite type.]}
1466 @end{Discussion}
1467
1468 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00269-01]}
1469 @Chg{Version=[2],New=[@Defn2{Term=[full constant declaration],
1470 Sec=(corresponding to a formal object of mode @key[in])}],Old=[]}
1471 @Defn2{Term=[stand-alone constant],
1472 Sec=(corresponding to a formal object of mode @key[in])}
1473 @PDefn{stand-alone object}
1474 In an instance,
1475 a @nt{formal_object_declaration} of mode @key{in}
1476 @Chg{Version=[2],New=[is a @i<full constant declaration> and ],
1477 Old=[]}declares a new stand-alone constant
1478 object whose initialization expression is the actual,
1479 whereas a @nt{formal_object_declaration} of mode @key{in out}
1480 declares a view whose properties are identical to those of the actual.
1481 @begin{Ramification}
1482 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00287-01]}
1483 These rules imply that generic formal objects of mode @key{in} are
1484 passed by copy@Chg{Version=[2],New=[ (or are built-in-place for
1485 a limited type)],Old=[]},
1486 whereas generic formal objects of mode @key{in out} are passed by
1487 reference.
1488
1489 Initialization and finalization happen for the constant declared by
1490 a @nt{formal_object_declaration} of mode @key{in} as for any constant;
1491 see @RefSec{Object Declarations}
1492 and @RefSec{Assignment and Finalization}.
1493
1494 @PDefn2{Term=[subtype], Sec=(of a generic formal object)}
1495 In an instance,
1496 the subtype of a generic formal object of mode
1497 @key{in} is as for the equivalent constant.
1498 In an instance,
1499 the subtype of a generic formal object of mode @key{in out}
1500 is the subtype of the corresponding generic actual.
1501 @end{Ramification}
1502 @end{StaticSem}
1503
1504 @begin{RunTime}
1505 @PDefn2{Term=[evaluation], Sec=(generic_association for a formal
1506 object of mode @key{in})}
1507 @Defn2{Term=[assignment operation], Sec=(during evaluation of a
1508 @nt{generic_association} for a formal object of mode @key{in})}
1509 For the evaluation of a @nt{generic_association}
1510 for a formal object of mode @key{in},
1511 a constant object is created, the value of the actual parameter
1512 is converted to the nominal subtype of the formal object,
1513 and assigned to the object@Redundant[, including any value adjustment @em
1514 see @RefSecNum{Assignment and Finalization}].
1515 @PDefn2{Term=[implicit subtype conversion],Sec=(generic formal object of mode @key[in])}
1516 @begin{Ramification}
1517 This includes evaluating the actual
1518 and doing a subtype conversion,
1519 which might raise an exception.
1520 @end{Ramification}
1521 @begin{Discussion}
1522 The rule for evaluating a @nt<generic_association> for a formal
1523 object of mode @key{in out} is covered by
1524 the general Dynamic Semantics rule in @RefSecNum{Generic Instantiation}.
1525 @end{Discussion}
1526 @end{RunTime}
1527
1528 @begin{Notes}
1529 The constraints that apply to a generic formal object of mode @key{in
1530 out} are those of the corresponding generic actual parameter (not
1531 those implied by the @nt{subtype_mark} that appears in the
1532 @nt{formal_object_declaration}).
1533 Therefore, to avoid confusion, it is recommended that the name of a
1534 first subtype
1535 be used for the declaration of such a formal object.
1536 @begin{Ramification}
1537 Constraint checks are done at instantiation time for formal objects of
1538 mode @key{in},
1539 but not for formal objects of mode @key{in out}.
1540 @end{Ramification}
1541 @end{Notes}
1542
1543 @begin{Extend83}
1544 @Defn{extensions to Ada 83}
1545 In Ada 83, it is forbidden to pass a (nongeneric) formal parameter
1546 of mode @key{out}, or a subcomponent thereof, to a generic formal
1547 object of mode @key{in out}.
1548 This restriction is removed in Ada 95.
1549 @end{Extend83}
1550
1551 @begin{DiffWord83}
1552 We make @lquotes@;@nt{mode}@rquotes@; explicit in the syntax.
1553 RM83 refers to the mode without saying what it is.
1554 This is also more uniform with the way (nongeneric) formal parameters
1555 are defined.
1556
1557 We considered allowing mode @key{out} in Ada 95,
1558 for uniformity with (nongeneric) formal parameters.
1559 The semantics would be identical for modes @key{in out} and
1560 @key{out}.
1561 (Note that generic formal objects of mode @key{in out} are passed by
1562 reference. Note that for (nongeneric) formal parameters that are
1563 allowed to be passed by reference, the semantics of @key{in out} and
1564 @key{out} is the same. The difference might serve as documentation.
1565 The same would be true for generic formal objects, if @key{out} were
1566 allowed, so it would be consistent.)
1567 We decided not to make this change, because it does not produce any
1568 important benefit, and any change has some cost.
1569 @end{DiffWord83}
1570
1571 @begin{Extend95}
1572 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00287-01]}
1573 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
1574 A generic formal @key{in} object can have
1575 a limited type. The actual for such an object must be built-in-place
1576 via a @nt{function_call} or @nt{aggregate}, see @RefSecNum{Limited Types}.]}
1577
1578 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00423-01]}
1579 @ChgAdded{Version=[2],Text=[A generic formal object can have
1580 a @nt{null_exclusion} or an anonymous access type.]}
1581 @end{Extend95}
1582
1583 @begin{DiffWord95}
1584 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00255-01]}
1585 @ChgAdded{Version=[2],Text=[Clarified that the nominal subtype of a
1586 composite formal @key{in out} object is unconstrained if the first subtype
1587 of the type is unconstrained.]}
1588
1589 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00269-01]}
1590 @ChgAdded{Version=[2],Text=[Clarified that a formal @key{in} object can
1591 be static when referenced from outside of the instance (by declaring
1592 such an object to be a full constant declaration).]}
1593 @end{DiffWord95}
1594
1595 @begin{Extend2005}
1596 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
1597 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
1598 An optional @nt{aspect_specification} can be used in a @nt{formal_object_declaration}.
1599 This is described in @RefSecNum{Aspect Specifications}.]}
1600 @end{Extend2005}
1601
1602
1603 @RMNewPageVer{Version=[2]}@Comment{For printed version of Ada 2005 RM}
1604 @LabeledClause{Formal Types}
1605
1606 @begin{Intro}
1607 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1608 @redundant[A generic formal subtype can be used to pass to a generic unit
1609 a subtype whose type is in a certain @Chg{Version=[2],New=[category],
1610 Old=[class]} of types.]
1611 @begin{Reason}
1612 We considered having intermediate syntactic categories
1613 @ntf{formal_integer_type_definition},
1614 @ntf{formal_real_type_definition}, and
1615 @ntf{formal_fixed_point_definition},
1616 to be more uniform with the syntax rules for non-generic-formal
1617 types.
1618 However, that would make the rules for formal types slightly more
1619 complicated, and it would cause confusion,
1620 since @nt{formal_discrete_type_definition} would not fit into the
1621 scheme very well.
1622 @end{Reason}
1623 @end{Intro}
1624
1625 @begin{Syntax}
1626 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0213-1]}
1627 @Syn{lhs=<formal_type_declaration>,rhs="
1628 @Chg{Version=[3],New=[ @Syn2{formal_complete_type_declaration}
1629 | @Syn2{formal_incomplete_type_declaration}],Old=<@key{type} @Syn2{defining_identifier}[@Syn2{discriminant_part}] @key{is} @Syn2{formal_type_definition};>}"}
1630
1631 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0183-1],ARef=[AI05-0213-1]}
1632 @AddedSyn{Version=[3],lhs=<@Chg{Version=[3],New=<formal_complete_type_declaration>,Old=<>}>,rhs="@Chg{Version=[3],New=<
1633 @key{type} @Syn2{defining_identifier}[@Syn2{discriminant_part}] @key{is} @Syn2{formal_type_definition}
1634 [@Syn2{aspect_specification}];>,Old=<>}"}
1635
1636 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0213-1]}
1637 @AddedSyn{Version=[3],lhs=<@Chg{Version=[3],New=<formal_incomplete_type_declaration>,Old=<>}>,rhs="@Chg{Version=[3],New=<
1638 @key{type} @Syn2{defining_identifier}[@Syn2{discriminant_part}] [@key{is tagged}];>,Old=<>}"}
1639
1640 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01]}
1641 @Syn{lhs=<formal_type_definition>,rhs="
1642 @Syn2{formal_private_type_definition}
1643 | @Syn2{formal_derived_type_definition}
1644 | @Syn2{formal_discrete_type_definition}
1645 | @Syn2{formal_signed_integer_type_definition}
1646 | @Syn2{formal_modular_type_definition}
1647 | @Syn2{formal_floating_point_definition}
1648 | @Syn2{formal_ordinary_fixed_point_definition}
1649 | @Syn2{formal_decimal_fixed_point_definition}
1650 | @Syn2{formal_array_type_definition}
1651 | @Syn2{formal_access_type_definition}@Chg{Version=[2],New=[
1652 | @Syn2{formal_interface_type_definition}],Old=[]}"}
1653 @end{Syntax}
1654
1655 @begin{Legality}
1656 @Defn{generic actual subtype}
1657 @Defn{actual subtype}
1658 @Defn{generic actual type}
1659 @Defn{actual type}
1660 For a generic formal subtype, the actual shall be
1661 a @nt{subtype_mark};
1662 it denotes the @i{(generic) actual subtype}.
1663 @begin{Ramification}
1664 When we say simply @lquotes@;formal@rquotes@; or @lquotes@;actual@rquotes@; (for a generic
1665 formal that denotes a subtype) we're talking about the subtype, not
1666 the type, since a name that denotes a @nt{formal_type_declaration}
1667 denotes a subtype, and the corresponding actual also denotes a
1668 subtype.
1669 @end{Ramification}
1670 @end{Legality}
1671
1672 @begin{StaticSem}
1673 @Defn{generic formal type}
1674 @Defn{formal type}
1675 @Defn{generic formal subtype}
1676 @Defn{formal subtype}
1677 A @nt{formal_type_declaration} declares a @i{(generic) formal type},
1678 and its first subtype, the @i{(generic) formal subtype}.
1679 @begin{Ramification}
1680 A subtype (other than the first subtype)
1681 of a generic formal type is not a generic formal subtype.
1682 @end{Ramification}
1683
1684 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1685 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0213-1]}
1686 @Chg{Version=[2],New=[@Defn{determined category for a formal type}
1687 @Defn{category determined for a formal type}],
1688 Old=[@Defn{determined class for a formal type}
1689 @Defn{class determined for a formal type}]}
1690 The form of a @nt{formal_type_definition} @i{determines a
1691 @Chg{Version=[2],New=[category (of types)],Old=[class]}} to which the
1692 formal type belongs.
1693 For a @nt{formal_private_type_definition} the reserved words
1694 @key{tagged} and @key{limited} indicate the @Chg{Version=[2],New=[category of types],Old=[class]}
1695 (see @RefSecNum{Formal Private and Derived Types}).@Chg{Version=[3],New=[
1696 The reserved word @key{tagged} also plays this role in the case of a
1697 @nt{formal_incomplete_type_declaration}.],Old=[]}
1698 For a @nt{formal_derived_type_definition} the
1699 @Chg{Version=[2],New=[category of types],Old=[class]} is
1700 the derivation class rooted at the ancestor type.
1701 For other formal types,
1702 the name of the syntactic category indicates the
1703 @Chg{Version=[2],New=[category of types],Old=[class]};
1704 a @nt{formal_discrete_type_definition} defines a discrete type,
1705 and so on.
1706 @begin{Reason}
1707 This rule is clearer with the flat syntax rule for
1708 @nt{formal_type_definition} given above.
1709 Adding @ntf{formal_integer_type_definition} and others would make this
1710 rule harder to state clearly.
1711
1712 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
1713 @ChgAdded{Version=[2],Text=[We use @lquotes@;category@rquote rather than
1714 @lquotes@;class@rquotes above, because the requirement that classes are
1715 closed under derivation is not important here. Moreover, there are
1716 interesting categories that are not closed under derivation. For instance,
1717 limited and interface are categories that do not form classes.]}
1718 @end{Reason}
1719 @end{StaticSem}
1720
1721 @begin{Legality}
1722 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1723 The actual type shall be in the @Chg{Version=[2],New=[category],Old=[class]}
1724 determined for the formal.
1725 @begin{Ramification}
1726 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1727 For example, if the @Chg{Version=[2],New=[category],Old=[class]} determined
1728 for the formal is the @Chg{Version=[2],New=[category],Old=[class]} of all
1729 discrete types, then the actual has to be discrete.
1730
1731 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1732 Note that this rule does not require the actual to belong to every
1733 @Chg{Version=[2],New=[category],Old=[class]} to which the formal belongs.
1734 For example, formal private types are in the
1735 @Chg{Version=[2],New=[category],Old=[class]} of composite types,
1736 but the actual need not be composite.
1737 Furthermore, one can imagine an infinite number of @Chg{Version=[2],
1738 New=[categories],Old=[classes]} that are just
1739 arbitrary sets of types @Chg{Version=[2],New=[],Old=[that obey the
1740 closed-under-derivation rule,
1741 and are therefore technically classes]}
1742 (even though we don't give them names,
1743 since they are uninteresting).
1744 We don't want this rule to apply to @i{those}
1745 @Chg{Version=[2],New=[categories],Old=[classes]}.
1746
1747 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01],ARef=[AI95-00442-01]}
1748 @lquotes@;Limited@rquotes@; is not @Chg{Version=[2],New=[an],Old=[a]}
1749 @lquotes@;interesting@rquotes@; @Chg{Version=[2],New=[category],Old=[class]},
1750 but @lquotes@;nonlimited@rquotes@; is;
1751 it is legal to pass a nonlimited type to a limited formal type,
1752 but not the other way around.
1753 The reserved word @Chg{Version=[2],New=[@key[limited]],Old=[@ntf{limited}]} really represents a
1754 @Chg{Version=[2],New=[category],Old=[class]} containing
1755 both limited and nonlimited types.
1756 @lquotes@;Private@rquotes@; is not a @Chg{Version=[2],New=[category for this purpose],
1757 Old=[class]}; a generic formal private type accepts
1758 both private and nonprivate actual types.
1759
1760 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1761 It is legal to pass a class-wide subtype as the actual
1762 if it is in the right @Chg{Version=[2],New=[category],Old=[class]},
1763 so long as the formal has unknown discriminants.
1764 @end{Ramification}
1765 @end{Legality}
1766
1767 @begin{StaticSem}
1768 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0037],ARef=[AI95-00043-01]}
1769 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00233-01],ARef=[AI95-00442-01]}
1770 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0029-1]}
1771 @Redundant[The formal type also belongs to each
1772 @Chg{Version=[2],New=[category],Old=[class]} that contains
1773 the determined @Chg{Version=[2],New=[category],Old=[class]}.]
1774 The primitive subprograms of the type are as for any
1775 type in the determined @Chg{Version=[2],New=[category],Old=[class]}. For a
1776 formal type other than a formal
1777 derived type, these are the predefined operators of the type@Chg{New=[.
1778 For an elementary formal type, the predefined operators are implicitly declared
1779 immediately after the declaration of the formal type. For a composite formal
1780 type, the predefined operators are implicitly declared either immediately after
1781 the declaration of the formal type, or later
1782 @Chg{Version=[2],New=[immediately within the declarative region in which the
1783 type is declared],Old=[in its immediate scope]} according
1784 to the rules of @RefSecNum(Private Operations).],
1785 Old=[; they are implicitly declared immediately after the declaration
1786 of the formal type.]} In an instance, the copy of such an
1787 implicit declaration declares a view of the predefined operator
1788 of the actual type, even if this operator has been overridden for
1789 the actual type@Chg{Version=[3],New=[ and even if it is never declared
1790 for the actual type],Old=[]}.
1791 @Redundant[The rules specific to formal derived types are given
1792 in @RefSecNum{Formal Private and Derived Types}.]
1793 @begin{Ramification}
1794 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1795 All properties of the type are as for any type in the @Chg{Version=[2],New=[category],Old=[class]}.
1796 Some examples:
1797 The primitive operations available are as defined by the language for each
1798 @Chg{Version=[2],New=[category],Old=[class]}.
1799 The form of @nt{constraint} applicable to a formal type in a
1800 @nt{subtype_indication} depends on the @Chg{Version=[2],New=[category],Old=[class]} of the type as for a
1801 nonformal type.
1802 The formal type is tagged if and only if it is declared as a tagged
1803 private type, or as a type derived from a (visibly) tagged type.
1804 (Note that the actual type might be tagged even if the formal type is
1805 not.)
1806 @end{Ramification}
1807 @begin{Reason}
1808 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1809 @ChgAdded{Version=[3],Type=[Leading],Keepnext=[T],Text=[The somewhat
1810 cryptic phrase @ldquote@;even if it is never declared@rdquote@; is
1811 intended to deal with the following oddity:]}
1812 @begin{Example}
1813 @ChgRef{Version=[3],Kind=[AddedNormal]}
1814 @ChgAdded{Version=[3],Text=[@key[package] Q @key[is]
1815 @key[type] T @key[is limited private];
1816 @key[private]
1817 @key[type] T @key[is range] 1 .. 10;
1818 @key[end] Q;]}
1819
1820 @ChgRef{Version=[3],Kind=[AddedNormal]}
1821 @ChgAdded{Version=[3],Text=[@key[generic]
1822 @key[type] A @key[is array] (Positive @key[range] <>) @key[of] T;
1823 @key[package] Q.G @key[is]
1824 A1, A2 : A (1 .. 1);
1825 @key[private]
1826 B : Boolean := A1 = A2;
1827 @key[end] Q.G;]}
1828
1829 @ChgRef{Version=[3],Kind=[AddedNormal]}
1830 @ChgAdded{Version=[3],Text=[@key[with] Q.G;
1831 @key[package] R @key[is]
1832 @key[type] C @key[is array] (Positive @key[range] <>) @key[of] Q.T;]}
1833
1834 @ChgRef{Version=[3],Kind=[AddedNormal]}
1835 @ChgAdded{Version=[3],Text=[ @key[package] I @key[is new] Q.G (C); --@RI{ Where is the predefined "=" for C?}
1836 @key[end] R;]}
1837 @end{Example}
1838
1839 @ChgRef{Version=[3],Kind=[AddedNormal]}
1840 @ChgAdded{Version=[3],Text=[An "=" is available for the formal type A in the
1841 private part of Q.G. However, no "=" operator is ever declared for type C,
1842 because its component type Q.T is limited. Still, in the instance I the name
1843 "=" declares a view of the "=" for C which exists-but-is-never-declared.]}
1844 @end{Reason}
1845 @end{StaticSem}
1846
1847
1848 @begin{Notes}
1849 Generic formal types, like all types, are not named.
1850 Instead, a @nt{name} can denote a generic formal subtype.
1851 Within a generic unit, a generic formal type is considered as being
1852 distinct from all other (formal or nonformal) types.
1853 @begin{TheProof}
1854 This follows from the fact that each @nt{formal_type_declaration}
1855 declares a type.
1856 @end{TheProof}
1857
1858 A @nt{discriminant_part} is allowed only for certain kinds of types,
1859 and therefore only for certain kinds of generic formal types.
1860 See @RefSecNum{Discriminants}.
1861 @begin{Ramification}
1862 The term @lquotes@;formal floating point type@rquotes@; refers to a type defined by a
1863 @nt{formal_floating_point_definition}.
1864 It does not include
1865 a formal derived type whose ancestor is floating point.
1866 Similar terminology applies to the other kinds of
1867 @nt{formal_type_definition}.
1868 @end{Ramification}
1869 @end{Notes}
1870
1871 @begin{Examples}
1872 @leading@keepnext@i{Examples of generic formal types:}
1873 @begin{Example}
1874 @key[type] Item @key[is] @key[private];
1875 @key[type] Buffer(Length : Natural) @key[is] @key[limited] @key[private];
1876
1877 @key[type] Enum @key[is] (<>);
1878 @key[type] Int @key[is] @key[range] <>;
1879 @key[type] Angle @key[is] @key[delta] <>;
1880 @key[type] Mass @key[is] @key[digits] <>;
1881
1882 @key[type] Table @key[is] @key[array] (Enum) @key[of] Item;
1883 @end{Example}
1884
1885 @begin{WideAbove}
1886 @leading@keepnext@i{Example of a generic formal part declaring a
1887 formal integer type:}
1888 @end{WideAbove}
1889 @begin{Example}
1890 @key[generic]
1891 @key[type] Rank @key[is] @key[range] <>;
1892 First : Rank := Rank'First;
1893 Second : Rank := First + 1; --@RI{ the operator "+" of the type Rank }
1894 @end{Example}
1895 @end{Examples}
1896
1897 @begin{DiffWord83}
1898 RM83 has separate sections @lquotes@;Generic Formal Xs@rquotes@; and @lquotes@;Matching Rules for
1899 Formal Xs@rquotes@; (for various X's) with most of the text
1900 redundant between the two.
1901 We have combined the two in order to reduce the redundancy.
1902 In RM83, there is no @lquotes@;Matching Rules for Formal Types@rquotes@; section; nor is
1903 there a @lquotes@;Generic Formal Y Types@rquotes@; section (for Y = Private, Scalar, Array,
1904 and Access).
1905 This causes, for example, the duplication across all the @lquotes@;Matching
1906 Rules for Y Types@rquotes@; sections of the rule that the actual passed to a
1907 formal type shall be a subtype;
1908 the new organization avoids that problem.
1909
1910 The matching rules are stated more concisely.
1911
1912 We no longer consider the multiplying
1913 operators that deliver a result of type @i{universal_fixed} to be
1914 predefined for the various types; there is only one of each in
1915 package Standard. Therefore, we need not mention them here as RM83
1916 had to.
1917 @end{DiffWord83}
1918
1919 @begin{DiffWord95}
1920 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0037],ARef=[AI95-00043-01],ARef=[AI95-00233-01]}
1921 @ChgAdded{Version=[2],Text=[Corrigendum 1 corrected the wording to properly
1922 define the location where operators are defined for formal array types.
1923 The wording here was inconsistent with that in @RefSec{Private Operations}.
1924 For the Amendment, this wording was corrected again, because it didn't
1925 reflect the Corrigendum 1 revisions in @RefSecNum{Private Operations}.]}
1926
1927 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
1928 @ChgAdded{Version=[2],Text=[Formal interface types are defined; see
1929 @RefSec{Formal Interface Types}.]}
1930
1931 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
1932 @ChgAdded{Version=[2],Text=[We use @lquotes@;determines a category@rquotes
1933 rather than class, since not all interesting properties form a class.]}
1934 @end{DiffWord95}
1935
1936 @begin{Extend2005}
1937 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
1938 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
1939 An optional @nt{aspect_specification} can be used in a @nt{formal_type_declaration}.
1940 This is described in @RefSecNum{Aspect Specifications}.]}
1941 @end{Extend2005}
1942
1943 @begin{DiffWord2005}
1944 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
1945 @ChgAdded{Version=[3],Text=[@b<Correction>: Updated the wording to
1946 acknowledge the possibility of operations that are never declared for an
1947 actual type but still can be used inside of a generic unit.]}
1948
1949 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0213-1],ARef=[AI05-0299-1]}
1950 @ChgAdded{Version=[3],Text=[Formal incomplete types are added; these
1951 are documented as an extension in the next subclause.]}
1952 @end{DiffWord2005}
1953
1954
1955 @LabeledSubClause{Formal Private and Derived Types}
1956
1957 @begin{Intro}
1958 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
1959 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0213-1]}
1960 @Redundant[@Chg{Version=[2],New=[In its most general form, the category],
1961 Old=[The class]}
1962 determined for a formal private type @Chg{Version=[2],New=[is all types,
1963 but @Chg{Version=[3],New=[the category],Old=[it]} can be restricted to
1964 only nonlimited types or to only tagged types],
1965 Old=[can be either limited or nonlimited, and either tagged or untagged;
1966 no more specific class is known for such a type]}.@Chg{Version=[3],New=[
1967 Similarly, the category for a formal incomplete type is all types but the
1968 category can be restricted to only tagged types; unlike other formal types,
1969 the actual type does not need to be able to be frozen (see @RefSecNum{Freezing Rules}).],Old=[]}
1970 The @Chg{Version=[2],New=[category],Old=[class]} determined for a formal
1971 derived type is the derivation class rooted at the ancestor type.]
1972 @begin{TheProof}
1973 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
1974 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0213-1]}
1975 @ChgAdded{Version=[2],Text=[The first @Chg{Version=[3],New=[two rules
1976 are],Old=[rule is]} given normatively below,
1977 and the @Chg{Version=[3],New=[third],Old=[second]} rule is given normatively
1978 in @RefSecNum{Formal Types}; they are repeated here to give a capsule
1979 summary of what this subclause is about.]}
1980 @end{TheProof}
1981
1982 @begin{Ramification}
1983 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0213-1]}
1984 @ChgAdded{Version=[3],Text=[Since the actual of a formal incomplete type
1985 does not need to be able to be frozen, the actual can be an incomplete
1986 type or a partial view before its completion.]}
1987 @end{Ramification}
1988 @end{Intro}
1989
1990 @begin{Syntax}
1991 @Syn{lhs=<formal_private_type_definition>,
1992 rhs="[[@key{abstract}] @key{tagged}] [@key{limited}] @key{private}"}
1993
1994 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00251-01],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
1995 @Syn{lhs=<formal_derived_type_definition>,
1996 rhs="@Chg{Version=[2],New=[
1997 ],Old=[]}[@key{abstract}] @Chg{Version=[2],New=<[@key{limited} | @key{synchronized}] >,Old=[]}@key{new} @Syn2{subtype_mark} [@Chg{Version=[2],New=<[@key{and} @Syn2{interface_list}]>,Old=<>}@key{with} @key{private}]"}
1998 @end{Syntax}
1999
2000 @begin{Legality}
2001 If a generic formal type declaration has a @nt{known_discriminant_part},
2002 then it shall not include a
2003 @nt{default_expression} for a discriminant.
2004 @begin{Ramification}
2005 Consequently,
2006 a generic formal subtype
2007 with a @nt{known_discriminant_part} is an indefinite subtype, so
2008 the declaration of a stand-alone variable has to provide a constraint
2009 on such a subtype, either explicitly, or by its initial value.
2010 @end{Ramification}
2011
2012 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401-01],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
2013 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0237-1]}
2014 @Defn2{Term=[ancestor subtype], Sec=(of a formal derived type)}
2015 @PDefn{private extension}
2016 The @i(ancestor subtype) of a formal derived type is the
2017 subtype denoted by the @nt<subtype_mark> of
2018 the @nt<formal_derived_type_definition>.
2019 For a formal derived type declaration,
2020 the reserved words @key{with private} shall appear if and only
2021 if the ancestor type is a tagged type; in this case
2022 the formal derived type is a private extension of the
2023 ancestor type and the ancestor shall not be a class-wide
2024 type.
2025 @Redundant[Similarly, @Chg{Version=[2],New=[an @nt{interface_list} or ],Old=[]}
2026 the optional reserved @Chg{Version=[2],New=[words],Old=[word]} @key{abstract}
2027 @Chg{Version=[2],New=[or @key{synchronized} ],Old=[]}shall
2028 appear only if the ancestor type is a tagged type].@Chg{Version=[2],
2029 New=[ The reserved word @key{limited} or @key{synchronized} shall appear
2030 only if the ancestor type @Redundant[and any progenitor types] are limited types.
2031 The reserved word @key{synchronized} shall appear (rather than @key{limited}) if
2032 the ancestor type or any of the progenitor types are
2033 synchronized interfaces.],Old=[]}@Chg{Version=[3],New=[ The ancestor type shall be a limited interface if the reserved
2034 word @key{synchronized} appears.],Old=[]}
2035
2036 @begin{Reason}
2037 We use the term @lquotes@;ancestor@rquotes@; here instead of @lquotes@;parent@rquotes@;
2038 because the actual can be any descendant of the ancestor,
2039 not necessarily a direct descendant.
2040
2041 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00419-01]}
2042 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
2043 @ChgAdded{Version=[2],Text=[We require the ancestor type to be limited when
2044 @key{limited} appears so that we avoid
2045 @Chg{Version=[3],New=[oddities],Old=[oddies]} like limited integer types.
2046 Normally, @key{limited} means @lquotes@;match anything@rquotes for a generic
2047 formal, but it was felt that allowing limited elementary types to be declared
2048 was just too weird. Integer still matches a formal limited private type;
2049 it is only a problem when the type is known to be elementary.
2050 Note that the progenitors are required to be limited by rules in
2051 @RefSecNum{Interface Types}, thus that part of the rule is redundant.]}
2052
2053 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00443-01]}
2054 @ChgAdded{Version=[2],Text=[We require that @key{synchronized} appear if the
2055 ancestor or any
2056 of the progenitors are synchronized, so that property is explicitly given
2057 in the program text @en it is not automatically inherited from the ancestors.
2058 However, it can be given even if neither the ancestor nor the progenitors
2059 are synchronized.]}
2060 @end{Reason}
2061
2062 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00251-01],ARef=[AI95-00401-01],ARef=[AI95-00443-01]}
2063 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0087-1]}
2064 @ChgRef{Version=[4],Kind=[RevisedAdded],ARef=[AI12-0036-1]}
2065 @ChgAdded{Version=[2],Text=[The actual type for a formal derived type
2066 shall be a descendant of @Redundant[the ancestor type and] every progenitor of
2067 the formal type. @Chg{Version=[3],New=[If the formal type is nonlimited,
2068 the actual type shall be nonlimited. @Chg{Version=[4],New=[The actual type for
2069 a formal derived type shall be tagged if and only if the formal derived type
2070 is a private extension. ],Old=[]}],Old=[]}If the reserved word
2071 @key[synchronized] appears
2072 in the declaration of the formal derived type, the actual
2073 type shall be a synchronized tagged type.]}
2074 @begin{TheProof}
2075 @ChgRef{Version=[2],Kind=[AddedNormal]}
2076 @ChgAdded{Version=[2],Text=[The actual type has to be a descendant of the
2077 ancestor type, in order that it be in the correct class. Thus, that part
2078 of the rule is redundant.]}
2079 @end{TheProof}
2080
2081 @begin{Discussion}
2082 @ChgRef{Version=[2],Kind=[AddedNormal]}
2083 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
2084 @ChgAdded{Version=[2],Text=[For a nonformal private extension, we
2085 require the partial view to be synchronized if the full view is synchronized
2086 tagged. This does not apply to a formal private extension @em it is OK if
2087 the formal is not synchronized. Any attempt to extend the formal
2088 type will be rechecked in the instance, where the rule disallowing
2089 extending a @Chg{Version=[3],New=[synchronized],Old=[sychronized]}
2090 noninterface type will be enforced. This is
2091 consistent with the @lquotes@;no hidden interfaces@rquotes rule also
2092 applying only to nonformal private extensions, as well as the rule that
2093 a limited nonformal private extension implies a limited full type.
2094 Formal private extensions are exempted from all these rules to
2095 enable the construction of generics that can be used with the widest
2096 possible range of types. In particular, an indefinite tagged
2097 limited formal private type can match any @lquotes@;concrete@rquotes
2098 actual tagged type.]}
2099
2100 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0087-1]}
2101 @ChgAdded{Version=[3],Text=[A type (including formal types) derived from a
2102 limited interface could be nonlimited; we do not want a limited type derived
2103 from such an interface to match a nonlimited formal derived type.
2104 Otherwise, we could assign limited objects. Thus, we have to explicitly
2105 ban this case.]}
2106
2107 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0036-1]}
2108 @ChgAdded{Version=[4],Text=[If we allowed actual types that differ from
2109 the kind of the formal derived type, we could allow type conversions that
2110 would not be allowed outside of the generic. That would be particularly
2111 problematical if the actual is a tagged type with extension components;
2112 we could have created an object of the type that is missing those components
2113 by converting from the ancestor type to a formal derived type that
2114 is not an extension.]}
2115 @end{Discussion}
2116
2117 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0213-1]}
2118 If @Chg{Version=[3],New=[a],Old=[the]} formal@Chg{Version=[3],New=[ private or
2119 derived],Old=[]} subtype is definite, then the actual subtype shall
2120 also be definite.
2121 @begin{Ramification}
2122 On the other hand, for an indefinite formal subtype,
2123 the actual can be either definite or indefinite.
2124 @end{Ramification}
2125
2126 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0213-1]}
2127 @ChgAdded{Version=[3],Text=[A @nt{formal_incomplete_type_declaration} declares
2128 a formal incomplete type. The only view of a formal incomplete type is an
2129 incomplete view. @Redundant[Thus, a formal incomplete type is subject to the same
2130 usage restrictions as any other incomplete type @em see @RefSecNum{Incomplete Type Declarations}.]]}
2131
2132 @leading@;For a generic formal derived type with no @nt<discriminant_part>:
2133 @begin(Itemize)
2134 If the ancestor subtype is constrained,
2135 the actual subtype shall be constrained,
2136 and shall be statically compatible with the ancestor;
2137 @begin{Ramification}
2138 In other words, any constraint on the ancestor subtype is considered
2139 part of the @lquotes@;contract.@rquotes@;
2140 @end{Ramification}
2141
2142 If the ancestor subtype is an unconstrained access
2143 or composite subtype,
2144 the actual subtype shall be unconstrained.
2145 @begin{Reason}
2146 This rule ensures that if a composite constraint is allowed on the
2147 formal, one is also allowed on the actual.
2148 If the ancestor subtype is an unconstrained scalar subtype,
2149 the actual is allowed to be constrained, since a scalar constraint
2150 does not cause further constraints to be illegal.
2151 @end{Reason}
2152
2153 If the ancestor subtype is an unconstrained discriminated subtype, then
2154 the actual shall have the same number of discriminants, and each
2155 discriminant of the actual shall correspond to a discriminant of the
2156 ancestor, in the sense of @RefSecNum{Discriminants}.
2157
2158 @begin{Reason}
2159 This ensures that if a discriminant constraint is given on
2160 the formal subtype, the corresponding constraint in the instance
2161 will make sense, without additional run-time checks.
2162 This is not necessary for arrays, since the bounds cannot be overridden
2163 in a type extension. An @nt<unknown_discriminant_part> may be used
2164 to relax these matching requirements.
2165 @end{Reason}
2166
2167 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00231-01]}
2168 @ChgAdded{Version=[2],Text=[If the ancestor subtype is an access subtype, the
2169 actual subtype shall exclude null if and only if the ancestor subtype
2170 excludes null.]}
2171 @begin{Reason}
2172 @ChgRef{Version=[2],Kind=[AddedNormal]}
2173 @ChgAdded{Version=[2],Text=[We require that the
2174 @lquotes@;excludes null@rquotes property match, because it would be difficult
2175 to write a correct generic for a formal access type without knowing this
2176 property. Many typical algorithms and techniques will not work for a
2177 subtype that excludes null (setting an unused component to @key{null},
2178 default-initialized objects, and so on). We want this sort of requirement to
2179 be reflected in the contract of the generic.]}
2180
2181 @end{Reason}
2182
2183 @end(Itemize)
2184
2185 @Leading@ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0213-1]}
2186 The declaration of a formal derived type shall not have a
2187 @nt{known_discriminant_part}.
2188 For a generic formal private @Chg{Version=[3],New=[or incomplete ],Old=[]}type
2189 with a @nt{known_discriminant_part}:
2190 @begin{Itemize}
2191 The actual type shall be a type with the same number of discriminants.
2192
2193 The actual subtype shall be unconstrained.
2194
2195 The subtype of each discriminant of the
2196 actual type shall statically match the subtype of the corresponding
2197 discriminant of the formal type.
2198 @PDefn2{Term=[statically matching],Sec=(required)}
2199 @begin{Reason}
2200 We considered defining the first and third rule to be called
2201 @lquotes@;subtype conformance@rquotes@; for @nt{discriminant_part}s.
2202 We rejected that idea, because it would require implicit (inherited)
2203 @nt{discriminant_part}s, which seemed like too much mechanism.
2204 @end{Reason}
2205 @end{Itemize}
2206
2207 @Redundant[For a generic formal type with an
2208 @nt{unknown_discriminant_part},
2209 the actual may, but need not, have discriminants,
2210 and may be definite or indefinite.]
2211
2212 @ChgRef{Version=[4],Kind=[Added],ARef=[AI12-0095-1]}
2213 @ChgAdded{Version=[4],Text=[When enforcing @LegalityTitle@;, for the purposes of
2214 determining within a generic body whether a type is unconstrained in any partial
2215 view, a discriminated subtype is considered to have a constrained partial view
2216 if it is a descendant of an untagged generic formal private or derived type.]}
2217
2218 @end{Legality}
2219
2220 @begin{StaticSem}
2221 @leading@;@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
2222 The @Chg{Version=[2],New=[category],Old=[class]} determined for a formal
2223 private type is as follows:
2224 @ChgRef{Version=[2],Kind=[Revised]}
2225 @TabClear{}@Tabset(P32)
2226 @begin{Display}
2227 @i(Type Definition) @\@i(Determined @Chg{Version=[2],New=[Category],Old=[Class]})@*
2228 @key{limited private} @\the @Chg{Version=[2],New=[category],Old=[class]} of all types
2229 @key{private} @\the @Chg{Version=[2],New=[category],Old=[class]} of all nonlimited types
2230 @key{tagged limited private} @\the @Chg{Version=[2],New=[category],Old=[class]} of all tagged types
2231 @key{tagged private} @\the @Chg{Version=[2],New=[category],Old=[class]} of all nonlimited tagged types
2232 @end{Display}
2233
2234 @Redundant[The presence of the reserved word @key{abstract} determines
2235 whether the actual type may be abstract.]
2236
2237 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0213-1]}
2238 @ChgAdded{Version=[3],Text=[The category determined for a formal incomplete type is the
2239 category of all types, unless the @nt{formal_type_declaration}
2240 includes the reserved word @key[tagged]; in this case, it is the
2241 category of all tagged types.]}
2242
2243 A formal private or derived type is a private or derived type,
2244 respectively.
2245 A formal derived tagged type is a private extension.
2246 @Redundant[A formal private or derived type is abstract if the reserved
2247 word @key(abstract) appears in its declaration.]
2248
2249 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00233-01]}
2250 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0110-1]}
2251 @Chg{Version=[3],New=[For a formal derived type, the characteristics],Old=[If
2252 the ancestor type is a composite type that is not an array type, the formal type
2253 inherits components from the ancestor type]} (including
2254 @Chg{Version=[3],New=[components, but excluding ],Old=[]}discriminants if
2255 @Chg{Version=[3],New=[there is ],Old=[]}a new
2256 @nt<discriminant_part>@Chg{Version=[3],New=[],Old=[ is not specified]}),
2257 @Chg{Version=[3],New=[predefined operators,
2258 and inherited user-defined primitive subprograms are determined
2259 by its ancestor type and its progenitor types (if any), in the
2260 same way that those of],Old=[as for]}
2261 a derived type
2262 @Chg{Version=[3],New=[are determined by those of its parent type and its
2263 progenitor types],Old=[defined by a @nt<derived_type_definition>]}
2264 (see @RefSecNum(Derived Types and Classes)@Chg{Version=[2],New=[ and
2265 @RefSecNum{Private Operations}],Old=[]}).
2266
2267
2268 @ChgRef{Version=[1],Kind=[Revised],Ref=[8652/0038],ARef=[AI95-00202]}
2269 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00233-01],ARef=[AI95-00401-01]}
2270 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0029-1],ARef=[AI05-0110-1]}
2271 @Chg{Version=[3],New=[],Old=[For a formal derived type,
2272 the predefined operators and inherited user-defined subprograms are determined
2273 by the ancestor type@Chg{Version=[2],New=[ and any progenitor types],Old=[]}, and
2274 are implicitly declared at the earliest place, if any,
2275 @Chg{Version=[2],New=[immediately within the declarative region in which],
2276 Old=[within the immediate scope of]} the formal
2277 type@Chg{Version=[2],New=[ is declared],Old=[]}, where the corresponding
2278 primitive subprogram of the ancestor @Chg{Version=[2],New=[or progenitor
2279 ],Old=[]}is visible (see @RefSecNum{Private Operations}). ]}In an instance,
2280 the
2281 copy of @Chg{Version=[3],New=[],Old=[such ]}an implicit declaration
2282 @Chg{Version=[3],New=[of a primitive subprogram of a formal derived type ],
2283 Old=[]}declares a view of the corresponding
2284 primitive subprogram of the ancestor@Chg{New=[@Chg{Version=[2], New=[ or
2285 progenitor],Old=[]} of the formal derived type],Old=[]}, even if this primitive
2286 has been overridden for the actual type@Chg{Version=[3],New=[ and even if it is
2287 never declared for the actual type],Old=[]}. @Chg{New=[When the
2288 ancestor@Chg{Version=[2], New=[ or progenitor],Old=[]} of the formal derived
2289 type is itself a formal type, the copy of the implicit declaration declares a
2290 view of the corresponding copied operation of the ancestor@Chg{Version=[2],
2291 New=[ or progenitor],Old=[]}.],Old=[]} @Redundant[In the case of a formal
2292 private extension, however, the tag of the formal type is that of the actual
2293 type, so if the tag in a call is statically determined to be that of the formal
2294 type, the body executed will be that corresponding to the actual type.]
2295 @begin{Ramification}
2296 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00401-01]}
2297 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0239-1]}
2298 The above rule defining the properties of primitive subprograms in an
2299 instance applies even if the subprogram has been overridden or
2300 hidden for the actual type.
2301 This rule is necessary for untagged types,
2302 because their primitive subprograms might have been overridden by
2303 operations that are not @Chg{Version=[3],New=[subtype conformant],Old=[subtype-conformant]} with the operations
2304 defined for the class.
2305 For tagged types, the rule still applies, but the primitive
2306 subprograms will dispatch to the appropriate implementation based on
2307 the type and tag of the operands.
2308 Even for tagged types, the formal parameter names and
2309 @nt{default_expression}s are determined
2310 by those of the primitive subprograms of the specified
2311 ancestor type@Chg{Version=[2],New=[ (or progenitor type, for subprograms
2312 inherited from an interface type)],Old=[]}.
2313 @end{Ramification}
2314 @begin{Honest}
2315 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0030-1]}
2316 @ChgAdded{Version=[4],Text=[The availability of stream attributes is not
2317 formally a characteristic of a type, but it is still determined by the
2318 ancestor type for a formal derived type in the same way as the characteristics
2319 are. Availability is rechecked in the instance specification.]}
2320 @end{Honest}
2321 @ChgRef{Version=[1],Kind=[Revised]}@ChgNote{To be consistent with 8652/0006}
2322 @Leading@;For @ChgPrefixType{Version=[1],Kind=[Revised],Text=[a
2323 @Chg{New=[@nt{prefix}],Old=[prefix]} S that denotes a formal indefinite subtype]},
2324 the following attribute is defined:
2325 @begin{Description}
2326 @ChgAttribute{Version=[3],Kind=(Revised),ChginAnnex=[T],Leading=[F],
2327 Prefix=<S>, AttrName=<Definite>,ARef=[AI05-0264-1],InitialVersion=[0],
2328 Text=[S'Definite yields True if the actual subtype corresponding
2329 to S is definite; otherwise@Chg{Version=[3],New=[,],Old=[]} it yields False. The value of this
2330 attribute is of the predefined type Boolean.]}
2331 @begin{Discussion}
2332 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00114-01]}
2333 Whether an actual subtype is definite or indefinite may
2334 have a major effect on the algorithm used in a generic.
2335 For example, in a generic I/O package, whether to use fixed-length or
2336 variable-length records could depend on whether the actual is
2337 definite or indefinite.
2338 This attribute is essentially a replacement for the Constrained
2339 attribute@Chg{Version=[2],New=[,],Old=[]}
2340 which is now considered obsolete.
2341 @end{Discussion}
2342 @end{Description}
2343 @EndPrefixType{}
2344 @end{StaticSem}
2345
2346 @begin{RunTime}
2347 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00158-01]}
2348 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0071-1]}
2349 @ChgAdded{Version=[2],Type=[Leading],Text=[In the case where a formal type
2350 @Chg{Version=[3],New=[has],Old=[is tagged with]} unknown discriminants,
2351 and the actual type is a class-wide type @i<T>'Class:]}
2352
2353 @begin{Itemize}
2354
2355 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00158-01]}
2356 @ChgAdded{Version=[2],Text=[For the purposes of defining the primitive
2357 operations of the formal type, each of the primitive operations of the actual
2358 type is considered to be a subprogram (with an intrinsic calling convention @em
2359 see @RefSecNum{Conformance Rules}) whose body consists of a dispatching call
2360 upon the corresponding operation of @i<T>, with its formal parameters as the
2361 actual parameters. If it is a function, the result of the dispatching call is
2362 returned.]}
2363
2364 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00158-01]}
2365 @ChgAdded{Version=[2],Text=[If the corresponding operation of @i<T>
2366 has no controlling formal
2367 parameters, then the controlling tag value is determined by the
2368 context of the call, according to the rules for tag-indeterminate
2369 calls (see @RefSecNum{Dispatching Operations of Tagged Types} and
2370 @RefSecNum{Assignment Statements}). In the case where the tag would be
2371 statically determined to be that of the formal type, the call raises
2372 Program_Error. If such a function is renamed, any call on the
2373 renaming raises Program_Error.
2374 @Defn2{Term=[Program_Error],Sec=(raised by failure of run-time check)}]}
2375
2376 @end{Itemize}
2377
2378
2379 @begin{Discussion}
2380 @ChgRef{Version=[2],Kind=[AddedNormal]}
2381 @ChgAdded{Version=[2],Text=[As it states in @RefSecNum{Conformance Rules},
2382 the convention of an inherited subprogram
2383 of a generic formal tagged type with unknown discriminants is intrinsic.]}
2384
2385 @ChgRef{Version=[2],Kind=[AddedNormal]}
2386 @ChgAdded{Version=[2],Type=[Leading],Text=[In the case of a corresponding
2387 primitive of T with no controlling
2388 formal parameters, the context of the call provides the controlling
2389 tag value for the dispatch. If no tag is provided by context,
2390 Program_Error is raised rather than resorting to a nondispatching
2391 call. For example:]}
2392
2393 @begin{Example}
2394 @ChgRef{Version=[2],Kind=[AddedNormal]}
2395 @ChgAdded{Version=[2],Text=[@key{generic}
2396 @key{type} NT(<>) @key{is new} T @key{with private};
2397 -- @RI[Assume T has operation "]@key{function} Empty @key{return} T;@RI["]
2398 @key{package} G @key{is}
2399 @key{procedure} Test(X : @key{in out} NT);
2400 @key{end} G;]}
2401
2402 @ChgRef{Version=[2],Kind=[AddedNormal]}
2403 @ChgAdded{Version=[2],Text=[@key{package body} G @key{is}
2404 @key{procedure} Test(X : @key{in out} NT) @key{is}
2405 @key{begin}
2406 X := Empty; -- @RI[Dispatching based on X'Tag takes]
2407 -- @RI[place if actual is class-wide.]
2408 @key{declare}
2409 Y : NT := Empty;
2410 -- @RI[If actual is class-wide, this raises Program_Error]
2411 -- @RI[as there is no tag provided by context.]
2412 @key{begin}
2413 X := Y; -- @RI[We never get this far.]
2414 @key{end};
2415 @key{end} Test;
2416 @key{end} G;]}
2417
2418 @ChgRef{Version=[2],Kind=[AddedNormal]}
2419 @ChgAdded{Version=[2],Text=[@key{type} T1 @key{is new} T @key{with null record};
2420 @key{package} I @key{is new} G(T1'Class);]}
2421 @end{Example}
2422 @end{Discussion}
2423 @end{RunTime}
2424
2425 @begin{Notes}
2426 @Leading@;@ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
2427 In accordance with the general rule that the actual type shall
2428 belong to the @Chg{Version=[2],New=[category],Old=[class]} determined for the formal
2429 (see @RefSec(Formal Types)):
2430 @begin(itemize)
2431 If the formal type is nonlimited, then so shall be the actual;
2432
2433 For a formal derived type, the actual shall be in the class rooted
2434 at the ancestor subtype.
2435 @end(itemize)
2436
2437 The actual type can be abstract only if the formal type is abstract
2438 (see @RefSecNum{Abstract Types and Subprograms}).
2439 @begin{Reason}
2440 This is necessary to avoid contract model problems,
2441 since one or more of its primitive subprograms are abstract;
2442 it is forbidden to create objects of the type,
2443 or to declare functions returning the type.
2444 @end{Reason}
2445 @begin{Ramification}
2446 On the other hand, it is OK to pass a nonabstract actual to an abstract
2447 formal @em @key[abstract] on the formal indicates that the actual might
2448 be abstract.
2449 @end{Ramification}
2450
2451 If the formal has a @nt{discriminant_part},
2452 the actual can be either definite or indefinite.
2453 Otherwise, the actual has to be definite.
2454 @end{Notes}
2455
2456 @begin{Incompatible83}
2457 @Defn{incompatibilities with Ada 83}
2458 Ada 83 does not have
2459 @nt{unknown_discriminant_part}s, so it allows indefinite
2460 subtypes to be passed to definite formals,
2461 and applies a legality rule to the instance body.
2462 This is a contract model violation.
2463 Ada 95 disallows such cases at the point of the instantiation.
2464 The workaround is to add (<>)
2465 as the @nt{discriminant_part} of
2466 any formal subtype if it is intended
2467 to be used with indefinite actuals.
2468 If that's the intent, then there can't be anything
2469 in the generic body that would require a definite subtype.
2470
2471 The check for discriminant subtype matching is changed from a
2472 run-time check to a compile-time check.
2473 @end{Incompatible83}
2474
2475
2476 @begin{Extend95}
2477 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01],ARef=[AI95-00401-01],ARef=[AI95-00419-01],ARef=[AI95-00443-01]}
2478 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2479 A generic formal derived type can include progenitors (interfaces) as well
2480 as a primary ancestor. It also may include @key{limited} to indicate that
2481 it is a limited type, and @key{synchronized} to indicate that it is a
2482 synchronized type.]}
2483 @end{Extend95}
2484
2485 @begin{DiffWord95}
2486 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0038],ARef=[AI95-00202-01]}
2487 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Corrected wording to define the
2488 operations that are inherited when the ancestor of a formal type is itself
2489 a formal type to avoid anomalies.]}
2490
2491 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00158-01]}
2492 @ChgAdded{Version=[2],Text=[Added a semantic description of the meaning
2493 of operations of an actual class-wide type, as such a type does not have
2494 primitive operations of its own.]}
2495
2496 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
2497 @ChgAdded{Version=[2],Text=[Added a matching rule for access subtypes that
2498 exclude null.]}
2499
2500 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00233-01]}
2501 @ChgAdded{Version=[2],Text=[The wording for the declaration of implicit
2502 operations is corrected to be consistent with @RefSecNum{Private Operations}
2503 as modified by Corrigendum 1.]}
2504
2505 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2506 @ChgAdded{Version=[2],Text=[We change to
2507 @lquotes@;determines a category@rquotes as that is the new terminology
2508 (it avoids confusion, since not all interesting properties form a class).]}
2509 @end{DiffWord95}
2510
2511 @begin{Incompatible2005}
2512 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0087-1]}
2513 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}@b<Correction:>
2514 Added wording to
2515 prevent a limited type from being passed to a nonlimited formal
2516 derived type. While this was allowed, it would break the contract
2517 for the limited type, so hopefully no programs actually depend on that.]}
2518 @end{Incompatible2005}
2519
2520 @begin{Extend2005}
2521 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0213-1]}
2522 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
2523 Formal incomplete types are a new kind of generic formal; these can
2524 be instantiated with incomplete types and unfrozen private types.]}
2525 @end{Extend2005}
2526
2527 @begin{DiffWord2005}
2528 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0029-1]}
2529 @ChgAdded{Version=[3],Text=[@b<Correction:> Updated the wording to
2530 acknowledge the possibility of operations that are never declared for an
2531 actual type but still can be used inside of a generic unit.]}
2532
2533 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0071-1]}
2534 @ChgAdded{Version=[3],Text=[@b<Correction:> Fixed hole that failed
2535 to define what happened for "=" for an untagged private type whose
2536 actual is class-wide.]}
2537
2538 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0110-1]}
2539 @ChgAdded{Version=[3],Text=[@b<Correction:> Revised the wording for
2540 inheritance of characteristics and operations of formal derived types
2541 to be reuse the rules as defined for derived types; this should eliminate
2542 holes in the wording which have plagued us since Ada 95 was defined
2543 (it has been "corrected" four previous times).]}
2544
2545 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0237-1]}
2546 @ChgAdded{Version=[3],Text=[@b<Correction:> Added missing rule for the
2547 ancestors of formal derived types. The added rule would formally be
2548 incompatible, but since it would be impossible to instantiate any such
2549 generic, this cannot happen outside of test suites and thus is not
2550 documented as an incompatibility.]}
2551 @end{DiffWord2005}
2552
2553 @begin{Incompatible2012}
2554 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0036-1]}
2555 @ChgAdded{Version=[4],Text=[@Defn{incompatibilities with Ada 2012}@b<Corrigendum:>
2556 Added a requirement that a tagged type only match a formal derived type
2557 that is a private extension. This is necessary to prevent type conversions
2558 that would not be allowed outside of the generic. We expect that this will
2559 be rare, as it only can happen if the formal derived type does not
2560 accurately describe the actual type; in most such cases, extension will be
2561 desired and a private extension used so that is allowed.]}
2562 @end{Incompatible2012}
2563
2564 @begin{DiffWord2012}
2565 @ChgRef{Version=[4],Kind=[AddedNormal],ARef=[AI12-0095-1]}
2566 @ChgAdded{Version=[4],Text=[@b<Corrigendum:> The assume the worst rule
2567 for determining within a generic body whether a type is unconstrained in any
2568 partial view was moved here. While AI05-0041-1 added it to
2569 @RefSecNum{Operations of Access Types}, it's also needed (at least) in
2570 @RefSecNum{Type Conversions} and @RefSecNum{Parameter Associations}. Thus,
2571 it was moved here so that it applies generally.]}
2572 @end{DiffWord2012}
2573
2574 @NotISORMNewPageVer{Version=[3]}@Comment{For printed version of Ada 2012 RM}
2575 @LabeledSubClause{Formal Scalar Types}
2576
2577 @begin{Intro}
2578 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
2579 A @i{formal scalar type} is one defined by any of the
2580 @nt{formal_type_definition}s in this subclause.
2581 @Redundant[The @Chg{Version=[2],New=[category],Old=[class]} determined for a
2582 formal scalar type is @Chg{Version=[2],New=[the category of all ],Old=[]}discrete,
2583 signed integer, modular, floating point, ordinary fixed point, or
2584 decimal@Chg{Version=[2],New=[ types],Old=[]}.]
2585 @begin{TheProof}
2586 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2587 @ChgAdded{Version=[2],Text=[The second rule follows from the rule in
2588 @RefSecNum{Formal Types} that says that the category is determined by the
2589 one given in the name of the syntax production. The effect of the rule
2590 is repeated here to give a capsule
2591 summary of what this subclause is about.]}
2592 @end{TheProof}
2593 @begin{Ramification}
2594 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2595 @ChgAdded{Version=[2],Text=[The @lquotes@;category of a type@rquotes
2596 includes any classes that the type belongs to.]}
2597 @end{Ramification}
2598 @end{Intro}
2599
2600
2601 @begin{Syntax}
2602 @Syn{lhs=<formal_discrete_type_definition>,rhs="(<>)"}
2603
2604
2605 @Syn{lhs=<formal_signed_integer_type_definition>,rhs="@key{range} <>"}
2606 @Syn{lhs=<formal_modular_type_definition>,rhs="@key{mod} <>"}
2607
2608
2609 @Syn{lhs=<formal_floating_point_definition>,rhs="@key{digits} <>"}
2610
2611
2612 @Syn{lhs=<formal_ordinary_fixed_point_definition>,rhs="@key{delta} <>"}
2613 @Syn{lhs=<formal_decimal_fixed_point_definition>,rhs="@key{delta} <> @key{digits} <>"}
2614 @end{Syntax}
2615
2616 @begin{Legality}
2617 The actual type for a formal scalar type
2618 shall not be a nonstandard numeric type.
2619 @begin{Reason}
2620 This restriction is necessary because nonstandard numeric
2621 types have some number of restrictions on their use, which could cause
2622 contract model problems in a generic body. Note that nonstandard
2623 numeric types can be passed to formal derived and formal private
2624 subtypes, assuming they obey all the other rules, and assuming the
2625 implementation allows it (being nonstandard means the implementation
2626 might disallow anything).
2627 @end{Reason}
2628 @end{Legality}
2629
2630 @begin{Notes}
2631 The actual type shall be in the class of types implied
2632 by the syntactic category of the formal type definition
2633 (see @RefSec(Formal Types)). For example, the actual for a
2634 @nt<formal_modular_type_definition> shall be a modular type.
2635 @end{Notes}
2636
2637 @begin{DiffWord95}
2638 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2639 @ChgAdded{Version=[2],Text=[We change to
2640 @lquotes@;determines a category@rquotes as that is the new terminology
2641 (it avoids confusion, since not all interesting properties form a class).]}
2642 @end{DiffWord95}
2643
2644
2645 @LabeledSubClause{Formal Array Types}
2646
2647 @begin{Intro}
2648 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
2649 @Redundant[The @Chg{Version=[2],New=[category],Old=[class]} determined for a
2650 formal array type is the @Chg{Version=[2],New=[category],Old=[class]} of all array types.]
2651 @begin{TheProof}
2652 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2653 @ChgAdded{Version=[2],Text=[This rule follows from the rule in
2654 @RefSecNum{Formal Types} that says that the category is determined by the
2655 one given in the name of the syntax production. The effect of the rule
2656 is repeated here to give a capsule
2657 summary of what this subclause is about.]}
2658 @end{TheProof}
2659 @end{Intro}
2660
2661 @begin{Syntax}
2662 @Syn{lhs=<formal_array_type_definition>,rhs="@Syn2{array_type_definition}"}
2663 @end{Syntax}
2664
2665 @begin{Legality}
2666 The only form of @nt{discrete_subtype_definition} that is allowed within the
2667 declaration of a
2668 generic formal (constrained) array subtype is a @nt{subtype_mark}.
2669 @begin{Reason}
2670 The reason is the same as for forbidding @nt{constraint}s in
2671 @nt{subtype_indication}s (see @RefSecNum{Generic Declarations}).
2672 @end{Reason}
2673
2674 @Leading@;For a formal array subtype, the actual subtype shall satisfy the
2675 following conditions:
2676 @begin{Itemize}
2677 The formal array type and the actual array type shall have the same
2678 dimensionality; the formal subtype and the actual subtype shall be
2679 either both constrained or both unconstrained.
2680
2681 For each index position, the index types shall be the same,
2682 and the index subtypes (if unconstrained),
2683 or the index ranges (if constrained), shall statically match
2684 (see @RefSecNum{Statically Matching Constraints and Subtypes}).
2685 @PDefn2{Term=[statically matching],Sec=(required)}
2686
2687 The component subtypes of the formal and
2688 actual array types shall statically match.
2689 @PDefn2{Term=[statically matching],Sec=(required)}
2690
2691 If the formal type has aliased components,
2692 then so shall the actual.
2693 @begin{Ramification}
2694 On the other hand, if the formal's components are not aliased,
2695 then the actual's components can be either aliased or not.
2696 @end{Ramification}
2697 @end{Itemize}
2698 @end{Legality}
2699
2700 @begin{Examples}
2701 @Leading@Keepnext@i{Example of formal array types:}
2702 @begin{Example}
2703 --@RI{ given the generic package }
2704
2705 @key[generic]
2706 @key[type] Item @key[is] @key[private];
2707 @key[type] Index @key[is] (<>);
2708 @key[type] Vector @key[is] @key[array] (Index @key[range] <>) @key[of] Item;
2709 @key[type] Table @key[is] @key[array] (Index) @key[of] Item;
2710 @key[package] P @key[is]
2711 ...
2712 @key[end] P;
2713
2714 --@RI{ and the types }
2715
2716 @key[type] Mix @key[is] @key[array] (Color @key[range] <>) @key[of] Boolean;
2717 @key[type] Option @key[is] @key[array] (Color) @key[of] Boolean;
2718
2719 --@RI{ then Mix can match Vector and Option can match Table }
2720
2721 @key[package] R @key[is] @key[new] P(Item => Boolean, Index => Color,
2722 Vector => Mix, Table => Option);
2723
2724 --@RI{ Note that Mix cannot match Table and Option cannot match Vector}
2725 @end{Example}
2726 @end{Examples}
2727
2728 @begin{Incompatible83}
2729 @Defn{incompatibilities with Ada 83}
2730 The check for matching of component subtypes and index subtypes or
2731 index ranges is changed from a
2732 run-time check to a compile-time check.
2733 The Ada 83 rule that @lquotes@;If the component type is not a scalar type,
2734 then the component subtypes shall be either both constrained or both
2735 unconstrained@rquotes@; is removed, since it is subsumed by static matching.
2736 Likewise, the rules requiring that component types be
2737 the same is subsumed.
2738 @end{Incompatible83}
2739
2740 @begin{DiffWord95}
2741 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2742 @ChgAdded{Version=[2],Text=[We change to
2743 @lquotes@;determines a category@rquotes as that is the new terminology
2744 (it avoids confusion, since not all interesting properties form a class).]}
2745 @end{DiffWord95}
2746
2747
2748 @RMNewPageVer{Version=[2]}@Comment{For printed version of Ada 2005 RM}
2749 @LabeledSubClause{Formal Access Types}
2750
2751 @begin{Intro}
2752 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00442-01]}
2753 @Redundant[The @Chg{Version=[2],New=[category],Old=[class]} determined for a
2754 formal access type is the @Chg{Version=[2],New=[category],Old=[class]} of all access types.]
2755 @begin{TheProof}
2756 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2757 @ChgAdded{Version=[2],Text=[This rule follows from the rule in
2758 @RefSecNum{Formal Types} that says that the category is determined by the
2759 one given in the name of the syntax production. The effect of the rule
2760 is repeated here to give a capsule
2761 summary of what this subclause is about.]}
2762 @end{TheProof}
2763 @end{Intro}
2764
2765 @begin{Syntax}
2766 @Syn{lhs=<formal_access_type_definition>,rhs="@Syn2{access_type_definition}"}
2767 @end{Syntax}
2768
2769 @begin{Legality}
2770 For a formal access-to-object type,
2771 the designated subtypes of the formal and actual types shall
2772 statically match.
2773 @PDefn2{Term=[statically matching],Sec=(required)}
2774
2775 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00231-01]}
2776 If and only if the @nt{general_access_modifier} @key{constant} applies
2777 to the formal,
2778 the actual shall be an access-to-constant type.
2779 If the @nt{general_access_modifier} @key{all} applies to the
2780 formal, then the actual shall be a general access-to-variable type
2781 (see @RefSecNum{Access Types}).@Chg{Version=[2],New=[ If and only
2782 if the formal subtype excludes null, the actual subtype shall exclude null.],Old=[]}
2783 @begin{Ramification}
2784 If no @ntf{_modifier} applies to the formal, then
2785 the actual type may be either a pool-specific or a general
2786 access-to-variable type.
2787 @end{Ramification}
2788 @begin{Reason}
2789 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0109],ARef=[AI95-00025-01]}
2790 @ChgAdded{Version=[1],Text=[Matching an access-to-variable to a formal
2791 access-to-constant type cannot be allowed. If it were allowed, it would
2792 be possible to create an access-to-variable value designating a constant.]}
2793
2794 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
2795 @ChgAdded{Version=[2],Text=[We require that the @lquotes@;excludes null@rquotes
2796 property match, because it would be difficult to write a correct generic for a
2797 formal access type without knowing this property. Many typical algorithms and
2798 techniques will not work for a subtype that excludes null (setting an unused
2799 component to @key{null}, default-initialized objects, and so on). Even
2800 Ada.Unchecked_Deallocation would fail for a subtype that excludes null. Most
2801 generics would end up with comments saying that they are not intended to work
2802 for subtypes that exclude null. We would rather that this sort of requirement
2803 be reflected in the contract of the generic.]}
2804 @end{Reason}
2805
2806 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0239-1],ARef=[AI05-0288-1]}
2807 For a formal access-to-subprogram subtype,
2808 the designated profiles of the formal and the actual
2809 shall be @Chg{Version=[3],New=[subtype conformant],Old=[mode-conformant,
2810 and the calling convention of the actual shall be @i{protected}
2811 if and only if that of the formal is @i{protected}]}.
2812 @Chg{Version=[3],New=[@Defn2{Term=[subtype conformance],Sec=(required)}],
2813 Old=[@Defn2{Term=[mode conformance],Sec=(required)}]}
2814 @begin{Reason}
2815 @ChgRef{Version=[3],Kind=[DeletedNoDelMsg],ARef=[AI05-0288-1]}
2816 @ChgDeleted{Version=[3],Text=[We considered requiring subtype conformance
2817 here, but mode conformance is more flexible, given that there is no way in
2818 general to specify the convention of the formal.]}
2819 @end{Reason}
2820 @end{Legality}
2821
2822 @begin{Examples}
2823 @Leading@keepnext@i{Example of formal access types:}
2824 @begin{Example}
2825 --@RI{ the formal types of the generic package }
2826
2827 @key[generic]
2828 @key[type] Node @key[is] @key[private];
2829 @key[type] Link @key[is] @key[access] Node;
2830 @key[package] P @key[is]
2831 ...
2832 @key[end] P;
2833
2834 --@RI{ can be matched by the actual types }
2835
2836 @key[type] Car;
2837 @key[type] Car_Name @key[is] @key[access] Car;
2838
2839 @key[type] Car @key[is]
2840 @key[record]
2841 Pred, Succ : Car_Name;
2842 Number : License_Number;
2843 Owner : Person;
2844 @key[end] @key[record];
2845
2846 --@RI{ in the following generic instantiation }
2847
2848 @key[package] R @key[is] @key[new] P(Node => Car, Link => Car_Name);
2849 @end{Example}
2850 @end{Examples}
2851
2852 @begin{Incompatible83}
2853 @Defn{incompatibilities with Ada 83}
2854 The check for matching of designated subtypes is changed from a
2855 run-time check to a compile-time check.
2856 The Ada 83 rule that @lquotes@;If the
2857 designated type is other than a scalar type, then the designated
2858 subtypes shall be either both constrained or both unconstrained@rquotes@; is
2859 removed, since it is subsumed by static matching.
2860 @end{Incompatible83}
2861
2862 @begin{Extend83}
2863 @Defn{extensions to Ada 83}
2864 Formal access-to-subprogram subtypes and formal general access
2865 types are new concepts.
2866 @end{Extend83}
2867
2868 @begin{DiffWord95}
2869 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00231-01]}
2870 @ChgAdded{Version=[2],Text=[Added a matching rule for subtypes that exclude
2871 null.]}
2872
2873 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2874 @ChgAdded{Version=[2],Text=[We change to
2875 @lquotes@;determines a category@rquotes as that is the new terminology
2876 (it avoids confusion, since not all interesting properties form a class).]}
2877 @end{DiffWord95}
2878
2879 @begin{Incompatible2005}
2880 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0288-1]}
2881 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}
2882 @b<Correction:> Matching of formal access-to-subprogram types now uses
2883 subtype conformance rather than mode conformance, which is needed to
2884 plug a hole. This could cause some instantiations legal in Ada 95 and
2885 Ada 2005 to be rejected in Ada 2012. We believe that formal
2886 access-to-subprogram types occur rarely, and actuals that are not
2887 subtype conformant are rarer still, so this should not happen often.
2888 (In addition, one popular compiler has a bug that causes such instances
2889 to be rejected, so no code compiled with that compiler could have an
2890 incompatibility.)]}
2891 @end{Incompatible2005}
2892
2893
2894 @LabeledAddedSubClause{Version=[2],Name=[Formal Interface Types]}
2895
2896 @begin{Intro}
2897 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01],ARef=[AI95-00442-01]}
2898 @ChgAdded{Version=[2],Text=[@Redundant[The category determined for a formal
2899 interface type is the category of all interface types.]]}
2900 @begin{TheProof}
2901 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00442-01]}
2902 @ChgAdded{Version=[2],Text=[This rule follows from the rule in
2903 @RefSecNum{Formal Types} that says that the category is determined by the
2904 one given in the name of the syntax production. The effect of the rule
2905 is repeated here to give a capsule
2906 summary of what this subclause is about.]}
2907 @end{TheProof}
2908 @begin{Ramification}
2909 @ChgRef{Version=[2],Kind=[AddedNormal]}
2910 @ChgAdded{Version=[2],Text=[Here we're taking advantage
2911 of our switch in terminology from @lquotes@;determined class@rquotes to
2912 @lquotes@;determined category@rquotes; by saying @lquotes@;category@rquotes
2913 rather than @lquotes@;class@rquotes, we
2914 require that any actual type be an interface type, not just some type
2915 derived from an interface type.]}
2916 @end{Ramification}
2917
2918
2919 @end{Intro}
2920
2921 @begin{Syntax}
2922 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01]}
2923 @AddedSyn{Version=[2],lhs=<@Chg{Version=[2],New=<formal_interface_type_definition>,Old=<>}>,
2924 rhs="@Chg{Version=[2],New=<@Syn2{interface_type_definition}>,Old=<>}"}
2925 @end{Syntax}
2926
2927 @begin{Legality}
2928
2929 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251],ARef=[AI95-00401]}
2930 @ChgAdded{Version=[2],Text=[The actual type shall be a descendant of every
2931 progenitor of the formal type.]}
2932
2933 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00345]}
2934 @ChgAdded{Version=[2],Text=[The actual type shall be a limited, task,
2935 protected, or synchronized interface
2936 if and only if the formal type is also, respectively, a limited, task,
2937 protected, or synchronized interface.]}
2938 @begin{Discussion}
2939 @ChgRef{Version=[2],Kind=[AddedNormal]}
2940 @ChgAdded{Version=[2],Text=[We require the kind of interface type to match
2941 exactly because without that it is almost impossible to properly implement
2942 the interface.]}
2943 @end{Discussion}
2944 @end{Legality}
2945
2946 @begin{Examples}
2947 @begin{Example}
2948 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00433-01]}
2949 @ChgAdded{Version=[2],Text=[@key{type} Root_Work_Item @key{is tagged private};]}
2950
2951 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00433-01]}
2952 @ChgAdded{Version=[2],Text=[@key{generic}
2953 @key{type} Managed_Task @key{is task interface};
2954 @key{type} Work_Item(<>) @key{is new} Root_Work_Item @key{with private};
2955 @key{package} Server_Manager @key{is}
2956 @key{task type} Server @key{is new} Managed_Task @key{with}
2957 @key{entry} Start(Data : @key{in out} Work_Item);
2958 @key{end} Server;
2959 @key{end} Server_Manager;]}
2960 @end{Example}
2961
2962 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00433-01]}
2963 @ChgAdded{Version=[2],Text=[This generic allows an application to establish a
2964 standard interface that all tasks need to implement so they can be managed
2965 appropriately by an application-specific scheduler.]}
2966
2967 @end{Examples}
2968
2969 @begin{Extend95}
2970 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00251-01],ARef=[AI95-00345-01],ARef=[AI95-00401-01],ARef=[AI95-00442-01]}
2971 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
2972 The formal interface type is new.]}
2973 @end{Extend95}
2974
2975
2976 @LabeledClause{Formal Subprograms}
2977
2978 @begin{Intro}
2979 @redundant[@Defn{generic formal subprogram}
2980 @Defn{formal subprogram, generic}
2981 Formal subprograms can be used to pass callable entities to a generic
2982 unit.]
2983 @end{Intro}
2984
2985 @begin{MetaRules}
2986 Generic formal subprograms are like renames of the @nt{explicit_generic_actual_parameter}.
2987 @end{MetaRules}
2988
2989 @begin{Syntax}
2990 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00260-02]}
2991 @Syn{lhs=<formal_subprogram_declaration>,rhs="@Chg{Version=[2],
2992 New=<@Syn2{formal_concrete_subprogram_declaration}
2993 | @Syn2{formal_abstract_subprogram_declaration}>,
2994 Old=<@key{with} @Syn2{subprogram_specification} [@key{is} @Syn2{subprogram_default}];>}"}
2995
2996 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00260-02]}
2997 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0183-1]}
2998 @AddedSyn{Version=[2],lhs=<@Chg{Version=[2],New=<formal_concrete_subprogram_declaration>,Old=<>}>,
2999 rhs="@Chg{Version=[2],New=<
3000 @key{with} @Syn2{subprogram_specification} [@key{is} @Syn2{subprogram_default}]@Chg{Version=[3],New=<
3001 [@Syn2{aspect_specification}]>,Old=[]};>,Old=<>}"}
3002
3003 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00260-02]}
3004 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0183-1]}
3005 @AddedSyn{Version=[2],lhs=<@Chg{Version=[2],New=<formal_abstract_subprogram_declaration>,Old=<>}>,
3006 rhs="@Chg{Version=[2],New=<
3007 @key{with} @Syn2{subprogram_specification} @key{is abstract} [@Syn2{subprogram_default}]@Chg{Version=[3],New=<
3008 [@Syn2{aspect_specification}]>,Old=[]};>,Old=<>}"}
3009
3010 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00348-01]}
3011 @Syn{lhs=<subprogram_default>,rhs="@Syn2{default_name} | <>@Chg{Version=[2],New=< | @key{null}>,Old=<>}"}
3012
3013 @Syn{lhs=<default_name>,rhs="@Syn2{name}"}
3014
3015 @begin{SyntaxText}
3016 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00260-02],ARef=[AI95-00348-01]}
3017 @ChgAdded{Version=[2],Text=[A @nt{subprogram_default} of @key{null} shall not
3018 be specified for a formal function or for a
3019 @nt{formal_abstract_subprogram_declaration}.]}
3020 @begin{Reason}
3021 @ChgRef{Version=[2],Kind=[AddedNormal]}
3022 @ChgAdded{Version=[2],Text=[There are no null functions because the return
3023 value has to be constructed somehow. We don't allow null for abstract formal
3024 procedures, as the operation is dispatching. It doesn't seem appropriate (or
3025 useful) to say that the implementation of something is null in the formal
3026 type and all possible descendants of that type. This also would define a
3027 dispatching operation that doesn't correspond to a slot in the tag of the
3028 controlling type, which would be a new concept. Finally, additional rules
3029 would be needed to define the meaning of a dispatching null procedure (for
3030 instance, the convention of such a subprogram should be intrinsic, but that's
3031 not what the language says). It doesn't seem worth the effort.]}
3032 @end{Reason}
3033 @end{SyntaxText}
3034
3035 @end{Syntax}
3036
3037 @begin{Resolution}
3038 @PDefn2{Term=[expected profile], Sec=(formal subprogram default_name)}
3039 The expected profile for the @nt<default_name>, if any, is that of the
3040 formal subprogram.
3041 @begin{Ramification}
3042 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
3043 This rule, unlike others in this
3044 @Chg{Version=[3],New=[subclause],Old=[clause]}, is observed at compile
3045 time of the @nt{generic_declaration}.
3046
3047 The evaluation of the @nt{default_name} takes place during the
3048 elaboration of each instantiation that uses the default, as defined
3049 in @RefSec{Generic Instantiation}.
3050 @end{Ramification}
3051
3052 @PDefn2{Term=[expected profile], Sec=(formal subprogram actual)}
3053 For a generic formal subprogram,
3054 the expected profile for the actual is that of the formal subprogram.
3055 @end{Resolution}
3056
3057 @begin{Legality}
3058 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0239-1]}
3059 The profiles of the formal and any named default shall be
3060 @Chg{Version=[3],New=[mode conformant],Old=[mode-conformant]}.
3061 @Defn2{Term=[mode conformance],Sec=(required)}
3062 @begin{Ramification}
3063 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0299-1]}
3064 This rule, unlike others in this @Chg{Version=[3],New=[subclause],Old=[clause]},
3065 is checked at compile time of the @nt{generic_declaration}.
3066 @end{Ramification}
3067
3068 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0239-1]}
3069 The profiles of the formal and actual shall be
3070 @Chg{Version=[3],New=[mode conformant],Old=[mode-conformant]}.
3071 @Defn2{Term=[mode conformance],Sec=(required)}
3072
3073 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00423-01]}
3074 @ChgAdded{Version=[2],Type=[Leading],Text=[For a parameter or result subtype of
3075 a @nt{formal_subprogram_declaration} that has an explicit @nt{null_exclusion}:]}
3076
3077 @begin{Itemize}
3078 @ChgRef{Version=[2],Kind=[Added]}
3079 @ChgAdded{Version=[2],Text=[if the actual matching the
3080 @nt{formal_subprogram_declaration} denotes a generic formal object of
3081 another generic unit @i{G}, and the instantiation containing the actual
3082 that occurs within the body of a generic unit @i{G} or within the body of a
3083 generic unit declared within the declarative region of
3084 the generic unit @i{G}, then the corresponding parameter or result type of
3085 the formal subprogram of @i{G} shall have a @nt{null_exclusion};]}
3086
3087 @ChgRef{Version=[2],Kind=[Added]}
3088 @ChgAdded{Version=[2],Text=[otherwise, the subtype of the corresponding
3089 parameter or result type of the actual matching the
3090 @nt{formal_subprogram_declaration} shall exclude null.
3091 @PDefn{generic contract issue}
3092 In addition to the places where @LegalityTitle normally apply
3093 (see @RefSecNum{Generic Instantiation}),
3094 this rule applies also in the private part of an
3095 instance of a generic unit.]}
3096 @end{Itemize}
3097
3098 @begin{Reason}
3099 @ChgRef{Version=[2],Kind=[AddedNormal]}
3100 @ChgAdded{Version=[2],Type=[Leading],Text=[This rule prevents
3101 @lquotes@;lying@rquotes.
3102 @b<Null> must never be the value of a parameter or result with an explicit
3103 @nt{null_exclusion}. The first bullet is an assume-the-worst rule
3104 which prevents trouble in generic bodies (including bodies of child generics)
3105 when the formal subtype excludes null implicitly.]}
3106 @end{Reason}
3107
3108
3109 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00260-02]}
3110 @ChgRef{Version=[3],Kind=[RevisedAdded],ARef=[AI05-0296-1]}
3111 @ChgAdded{Version=[2],Text=[If a formal parameter of a
3112 @nt{formal_@!abstract_@!subprogram_@!declaration} is of a
3113 specific tagged type @i<T> or of an anonymous access type designating a
3114 specific tagged type @i<T>, @i<T> is called a @i<controlling type> of the
3115 @nt{formal_@!abstract_@!subprogram_@!declaration}. Similarly, if the result
3116 of a @nt{formal_@!abstract_@!subprogram_@!declaration} for a function is of
3117 a specific tagged type @i<T> or of an anonymous access type designating a
3118 specific tagged type @i<T>, @i<T> is called a controlling type of
3119 the @nt{formal_@!abstract_@!subprogram_@!declaration}. A
3120 @nt{formal_@!abstract_@!subprogram_@!declaration} shall have exactly
3121 one controlling type@Chg{Version=[3],New=[, and that type shall not be
3122 incomplete],Old=[]}.
3123 @Defn2{Term=[controlling type],Sec=[of a @nt{formal_abstract_subprogram_declaration}]}]}
3124
3125 @begin{Ramification}
3126 @ChgRef{Version=[2],Kind=[AddedNormal]}
3127 @ChgAdded{Version=[2],Text=[The specific tagged type could be any of
3128 a formal tagged private type,
3129 a formal derived type, a formal interface type,
3130 or a normal tagged type. While the last case doesn't
3131 seem to be very useful, there isn't any good reason for disallowing it.
3132 This rule ensures that the operation is a dispatching operation of some
3133 type, and that we unambiguously know what that type is.]}
3134
3135 @ChgRef{Version=[2],Kind=[AddedNormal]}
3136 @ChgAdded{Version=[2],Text=[We informally call a subprogram declared by
3137 a @nt{formal_@!abstract_@!subprogram_@!declaration} an
3138 @i{abstract formal subprogram},
3139 but we do not use this term in normative wording.
3140 @Defn{abstract formal subprogram}
3141 (We do use it often in these notes.)]}
3142 @end{Ramification}
3143
3144 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00260-02]}
3145 @ChgAdded{Version=[2],Text=[The actual subprogram for a
3146 @nt{formal_@!abstract_@!subprogram_@!declaration} shall be a
3147 dispatching operation of the controlling type or of the actual type
3148 corresponding to the controlling type.]}
3149
3150 @begin{Honest}
3151 @ChgRef{Version=[2],Kind=[AddedNormal]}
3152 @ChgAdded{Version=[2],Text=[We mean the controlling type of the
3153 @nt{formal_@!abstract_@!subprogram_@!declaration}, of course.
3154 Saying that gets unwieldy and redundant (so says at least one reviewer,
3155 anyway).]}
3156 @end{Honest}
3157
3158 @begin{Ramification}
3159 @ChgRef{Version=[2],Kind=[AddedNormal]}
3160 @ChgAdded{Version=[2],Type=[Leading],Text=[This means that the actual is
3161 either a primitive operation of the
3162 controlling type, or an abstract formal subprogram. Also note that this
3163 prevents the controlling type from being class-wide (with one exception
3164 explained below), as only specific types have primitive operations (and a
3165 formal subprogram eventually has to have an actual that is a primitive of
3166 some type). This could happen in a case like:]}
3167 @begin{Example}
3168 @ChgRef{Version=[2],Kind=[AddedNormal]}
3169 @ChgAdded{Version=[2],Text=[@key{generic}
3170 @key{type} T(<>) @key{is tagged private};
3171 @key{with procedure} Foo (Obj : @key{in} T) @key{is abstract};
3172 @key{package} P ...]}
3173
3174 @ChgRef{Version=[2],Kind=[AddedNormal]}
3175 @ChgAdded{Version=[2],Text=[@key{package} New_P @key{is new} P (Something'Class, Some_Proc);]}
3176 @end{Example}
3177
3178 @ChgRef{Version=[2],Kind=[AddedNormal]}
3179 @ChgAdded{Version=[2],Text=[The instantiation here is always illegal,
3180 because Some_Proc could never be a primitive operation of Something'Class
3181 (there are no such operations). That's good, because we want calls to Foo
3182 always to be dispatching calls.]}
3183
3184 @ChgRef{Version=[2],Kind=[AddedNormal]}
3185 @ChgAdded{Version=[2],Type=[Leading],Text=[Since it is possible for a formal
3186 tagged type to be instantiated with a class-wide type, it is possible for the
3187 (real) controlling type to be class-wide in one unusual case:]}
3188
3189 @begin{Example}
3190 @ChgRef{Version=[2],Kind=[AddedNormal]}
3191 @ChgAdded{Version=[2],Text=[@key{generic}
3192 @key{type} NT(<>) @key{is new} T @key{with private};
3193 -- @RI[Presume that T has the following primitive operation:]
3194 -- @key{with procedure} Bar (Obj : @key{in} T);
3195 @key{package} Gr ...]}
3196
3197 @ChgRef{Version=[2],Kind=[AddedNormal]}
3198 @ChgAdded{Version=[2],Text=[@key{package body} Gr @key{is}
3199 @key{package} New_P2 @key{is new} P (NT, Foo => Bar);
3200 @key{end} Gr;]}
3201
3202 @ChgRef{Version=[2],Kind=[AddedNormal]}
3203 @ChgAdded{Version=[2],Text=[@key{package} New_Gr @key{is new} Gr (Something'Class);]}
3204 @end{Example}
3205
3206 @ChgRef{Version=[2],Kind=[AddedNormal]}
3207 @ChgAdded{Version=[2],Text=[The instantiation of New_P2 is legal, since
3208 Bar is a dispatching operation of the actual type of the controlling type
3209 of the abstract formal subprogram Foo. This is not a problem, since the
3210 rules given in @RefSecNum{Formal Private and Derived Types} explain how
3211 this routine dispatches even though its parameter is class-wide.]}
3212
3213 @ChgRef{Version=[2],Kind=[AddedNormal]}
3214 @ChgAdded{Version=[2],Text=[Note that this legality rule never needs to be
3215 rechecked in an instance (that contains a nested instantiation). The rule
3216 only talks about the actual type of the instantiation; it does not require
3217 looking further; if the actual type is in fact a formal type, we do not
3218 intend looking at the actual for that formal.]}
3219 @end{Ramification}
3220 @end{Legality}
3221
3222 @begin{StaticSem}
3223 A @nt{formal_subprogram_declaration} declares a generic formal subprogram.
3224 The types of the formal parameters and result, if any, of
3225 the formal subprogram are
3226 those determined by the @nt<subtype_mark>s given in
3227 the @nt{formal_subprogram_declaration}; however, independent of
3228 the particular subtypes that are denoted by the @nt<subtype_mark>s,
3229 the nominal subtypes of the formal parameters and result, if any,
3230 are defined to be nonstatic, and unconstrained if
3231 of an array type @Redundant[(no applicable index constraint
3232 is provided in a call on a formal subprogram)].
3233 In an instance,
3234 a @nt{formal_subprogram_declaration} declares a view
3235 of the actual.
3236 The profile of this view takes its subtypes
3237 and calling convention
3238 from the original profile of the actual entity,
3239 while taking the formal parameter
3240 @nt{name}s and @nt{default_@!expression}s from the profile given in the
3241 @nt{formal_@!subprogram_@!declaration}. The
3242 view is a function or procedure, never an entry.
3243 @begin{Discussion}
3244 This rule is intended to be the same as the one for
3245 renamings-as-declarations, where the @nt{formal_subprogram_declaration}
3246 is analogous to a renaming-as-declaration,
3247 and the actual is analogous to the renamed view.
3248 @end{Discussion}
3249
3250 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0071-1],ARef=[AI05-0131-1]}
3251 @ChgAdded{Version=[3],Type=[Leading],Text=[If a @nt{subtype_mark} in the profile
3252 of the @nt{formal_subprogram_declaration} denotes a formal private or formal
3253 derived type and the actual type for this formal type is a class-wide type
3254 @i{T}'Class, then for the purposes of resolving the corresponding actual
3255 subprogram at the point of the instantiation, certain implicit declarations may
3256 be available as possible resolutions as follows:]}
3257
3258 @begin{DescribeCode}
3259 @ChgRef{Version=[3],Kind=[Added]}
3260 @ChgAdded{Version=[3],Text=[For each primitive subprogram of @i{T} that is
3261 directly visible at the point of the instantiation, and that has at least one
3262 controlling formal parameter, a corresponding implicitly declared subprogram
3263 with the same defining name, and having the same profile as the primitive
3264 subprogram except that @i{T} is systematically replaced by @i{T}'Class in the
3265 types of its profile, is potentially use-visible. The body of such a
3266 subprogram is as defined in @RefSecNum{Formal Private and Derived Types} for
3267 primitive subprograms of a formal type when the actual type is
3268 class-wide.@PDefn{potentially use-visible}]}
3269 @end{DescribeCode}
3270
3271 @begin{Reason}
3272 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0071-1],ARef=[AI05-0131-1]}
3273 @ChgAdded{Version=[3],Text=[This gives the same capabilities to formal
3274 subprograms as those that primitive operations of the formal type have
3275 when the actual type is class-wide. We do not want to discourage the use of
3276 explicit declarations for (formal) subprograms!]}
3277 @end{Reason}
3278
3279 @begin{ImplNote}
3280 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0071-1],ARef=[AI05-0131-1]}
3281 @ChgAdded{Version=[3],Text=[Although the above wording seems to require
3282 constructing implicit versions of all of the primitive subprograms of type
3283 @i{T}, it should be clear that a compiler only needs to consider those that
3284 could possibly resolve to the corresponding actual subprogram. For instance,
3285 if the formal subprogram is a procedure with two parameters, and the actual
3286 subprogram name is Bar (either given explicitly or by default), the compiler
3287 need not consider primitives that are functions, that have the wrong number of
3288 parameters, that have defining names other than Bar, and so on; thus it does
3289 not need to construct implicit declarations for those primitives.]}
3290 @end{ImplNote}
3291
3292 @begin{Ramification}
3293 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0071-1],ARef=[AI05-0131-1]}
3294 @ChgAdded{Version=[3],Text=[Functions that only have a controlling result
3295 and do not have a controlling parameter of @i<T> are not covered by this
3296 rule, as any call would be required to raise Program_Error by
3297 @RefSecNum{Formal Private and Derived Types}. It is better to detect the
3298 error earlier than at run time.]}
3299 @end{Ramification}
3300
3301 If a generic unit has a @nt<subprogram_default> specified by a box, and
3302 the corresponding actual parameter is omitted, then it is equivalent to
3303 an explicit actual parameter that is a usage name identical to the
3304 defining name of the formal.
3305
3306 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00348-01]}
3307 @ChgAdded{Version=[2],Text=[If a generic unit has a @nt{subprogram_default}
3308 specified by the reserved word @key{null}, and the corresponding actual
3309 parameter is omitted, then it is equivalent to an explicit actual parameter
3310 that is a null procedure having the profile given in the
3311 @nt{formal_@!subprogram_@!declaration}.]}
3312
3313 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00260-02]}
3314 @ChgAdded{Version=[2],Text=[The subprogram declared by a
3315 @nt{formal_@!abstract_@!subprogram_@!declaration} with a controlling type @i<T>
3316 is a dispatching operation of type @i<T>.]}
3317 @begin{Reason}
3318 @ChgRef{Version=[2],Kind=[AddedNormal]}
3319 @ChgAdded{Version=[2],Text=[This is necessary to trigger all of the
3320 dispatching operation
3321 rules. It otherwise would not be considered a dispatching operation, as
3322 formal subprograms are never primitive operations.]}
3323 @end{Reason}
3324
3325 @end{StaticSem}
3326
3327 @begin{Notes}
3328 The matching rules for formal subprograms state requirements that are
3329 similar to those applying to @nt{subprogram_renaming_declaration}s
3330 (see @RefSecNum{Subprogram Renaming Declarations}).
3331 In particular, the name of a parameter of the formal subprogram need not
3332 be the same as that of the corresponding parameter of the actual
3333 subprogram;
3334 similarly, for these parameters, @nt{default_expression}s need not
3335 correspond.
3336
3337 The constraints that apply to a parameter of a formal subprogram are
3338 those of the corresponding formal parameter of the matching actual
3339 subprogram (not those implied by the corresponding @nt{subtype_mark} in
3340 the @ntf{_specification} of the formal subprogram). A similar remark
3341 applies to the result of a function. Therefore, to avoid confusion, it
3342 is recommended that the @nt{name} of a first subtype
3343 be used in any declaration of a formal subprogram.
3344
3345 The subtype specified for a formal parameter of a generic formal
3346 subprogram can be any visible subtype, including a generic formal
3347 subtype of the same @nt{generic_formal_part}.
3348
3349 A formal subprogram is matched by an attribute of a type if the
3350 attribute is a function with a matching specification.
3351 An enumeration literal of a given type matches a parameterless formal
3352 function whose result type is the given type.
3353
3354 A @nt{default_name} denotes an entity that is visible or directly
3355 visible at the place of the @nt{generic_declaration};
3356 a box used as a default is equivalent to a name that denotes an
3357 entity that is directly visible at the place of the
3358 @ntf{_instantiation}.
3359 @begin{TheProof}
3360 Visibility and name resolution are applied to the equivalent explicit
3361 actual parameter.
3362 @end{TheProof}
3363
3364 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00260-02]}
3365 The actual subprogram cannot be abstract@Chg{Version=[2],New=[ unless
3366 the formal subprogram is a @nt{formal_@!abstract_@!subprogram_@!declaration}],
3367 Old=[]} (see @RefSecNum{Abstract Types and Subprograms}).
3368
3369 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00260-02]}
3370 @ChgAdded{Version=[2],Text=[The subprogram declared by a
3371 @nt{formal_@!abstract_@!subprogram_@!declaration} is an abstract subprogram.
3372 All calls on a subprogram declared by a
3373 @nt{formal_@!abstract_@!subprogram_@!declaration} must be dispatching calls.
3374 See @RefSecNum{Abstract Types and Subprograms}.]}
3375
3376 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00348-01]}
3377 @ChgAdded{Version=[2],Text=[A null procedure as a subprogram default has
3378 convention Intrinsic (see @RefSecNum{Conformance Rules}).]}
3379 @begin{TheProof}
3380 @ChgRef{Version=[2],Kind=[Added]}
3381 @ChgAdded{Version=[2],Text=[This is an implicitly declared subprogram,
3382 so it has convention Intrinsic as defined in @RefSecNum{Conformance Rules}.]}
3383 @end{TheProof}
3384
3385 @end{Notes}
3386
3387 @begin{Examples}
3388 @Leading@Keepnext@i{Examples of generic formal subprograms:}
3389 @begin{Example}
3390 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00433-01]}
3391 @key[with] @key[function] "+"(X, Y : Item) @key[return] Item @key[is] <>;
3392 @key[with] @key[function] Image(X : Enum) @key[return] String @key[is] Enum'Image;
3393 @key[with] @key[procedure] Update @key[is] Default_Update;@Chg{Version=[2],New=[
3394 @key[with] @key[procedure] Pre_Action(X : @key[in] Item) @key[is null]; --@RI[ defaults to no action]
3395 @key[with] @key[procedure] Write(S : @key[not null access] Root_Stream_Type'Class;
3396 Desc : Descriptor)
3397 @b<is abstract> Descriptor'Write; --@RI[ see @RefSecNum{Stream-Oriented Attributes}]
3398 --@RI[ Dispatching operation on Descriptor with default]],Old=[]}
3399
3400 --@RI{ given the generic procedure declaration }
3401
3402 @key[generic]
3403 @key[with] @key[procedure] Action (X : @key[in] Item);
3404 @key[procedure] Iterate(Seq : @key[in] Item_Sequence);
3405
3406 --@RI{ and the procedure }
3407
3408 @key[procedure] Put_Item(X : @key[in] Item);
3409
3410 --@RI{ the following instantiation is possible }
3411
3412 @key[procedure] Put_List @key[is] @key[new] Iterate(Action => Put_Item);
3413 @end{Example}
3414 @end{Examples}
3415
3416
3417 @begin{Extend95}
3418 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00260-02]}
3419 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
3420 The @nt{formal_abstract_subprogram_declaration} is new. It allows
3421 the passing of dispatching operations to generic units.]}
3422
3423 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00348-01]}
3424 @ChgAdded{Version=[2],Text=[
3425 The formal subprogram default of @key{null} is new. It allows the default
3426 of a generic procedure to do nothing, such as for passing a debugging
3427 routine.]}
3428 @end{Extend95}
3429
3430 @begin{Diffword95}
3431 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00423-01]}
3432 @ChgAdded{Version=[2],Text=[Added matching rules for @nt{null_exclusion}s.]}
3433 @end{Diffword95}
3434
3435 @begin{Incompatible2005}
3436 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0296-1]}
3437 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}It
3438 is now illegal to declare a formal abstract subprogram whose controlling
3439 type is incomplete. It was never intended to allow that, and such a type
3440 would have to come from outside of the generic unit in Ada 2005, so it is
3441 unlikely to be useful. Moreover, a dispatching call on the subprogram
3442 is likely to fail in many implementations. So it is very unlikely that any
3443 code will need to be changed because of this new rule.]}
3444 @end{Incompatible2005}
3445
3446 @begin{Extend2005}
3447 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0071-1],ARef=[AI05-0131-1]}
3448 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}@b<Correction:>
3449 Added construction of implicit subprograms for primitives of class-wide actual
3450 types, to make it possible to import subprograms via formal subprograms as
3451 well as by implicit primitive operations of a formal type. (This is a
3452 @b<Correction> as it is very important for the usability of indefinite
3453 containers when instantiated with class-wide types; thus we want Ada 2005
3454 implementations to support it.)]}
3455
3456 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
3457 @ChgAdded{Version=[3],Text=[An optional @nt{aspect_specification} can be
3458 used in a @nt{formal_concrete_subprogram_declaration} and a
3459 @nt{formal_abstract_subprogram_declaration}.
3460 This is described in @RefSecNum{Aspect Specifications}.]}
3461 @end{Extend2005}
3462
3463
3464
3465 @LabeledClause{Formal Packages}
3466
3467 @begin{Intro}
3468 @redundant[@Defn{generic formal package}
3469 @Defn{formal package, generic}
3470 Formal packages can be used to pass packages to a generic unit.
3471 The @nt{formal_package_declaration} declares that the formal package
3472 is an instance of a given generic package.
3473 Upon instantiation, the actual package has to be an instance
3474 of that generic package.]
3475 @end{Intro}
3476
3477 @begin{Syntax}
3478 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0183-1]}
3479 @Syn{lhs=<formal_package_declaration>,rhs="
3480 @key{with} @key{package} @Syn2{defining_identifier} @key{is} @key{new} @SynI{generic_package_}@Syn2{name} @Syn2{formal_package_actual_part}@Chg{Version=[3],New=<
3481 [@Syn2{aspect_specification}]>,Old=[]};"}
3482
3483 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00317-01]}
3484 @Syn{lhs=<formal_package_actual_part>,rhs="
3485 @Chg{Version=[2],New=`([@key{others} =>] <>)
3486 | [@Syn2{generic_actual_part}]
3487 | (@Syn2{formal_package_association} {, @Syn2{formal_package_association}} [, @key{others} => <>])',
3488 Old=[(<>) | [@Syn2{generic_actual_part}]]}"}
3489
3490 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00317-01]}
3491 @AddedSyn{Version=[2],lhs=<@Chg{Version=[2],New=<formal_package_association>,Old=<>}>,
3492 rhs="@Chg{Version=[2],New={
3493 @Syn2{generic_association}
3494 | @SynI{generic_formal_parameter_}@Syn2{selector_name} => <>},Old={}}"}
3495
3496 @begin{SyntaxText}
3497 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00317-01]}
3498 @ChgAdded{Version=[2],Text=[Any positional @nt{formal_package_association}s
3499 shall precede any named @nt{formal_package_association}s.]}
3500 @end{SyntaxText}
3501 @end{Syntax}
3502
3503 @begin{Legality}
3504 @Defn2{Term=[template], Sec=(for a formal package)}
3505 The @i(generic_package_)@nt<name> shall denote a generic package
3506 (the @i(template) for the formal package);
3507 the formal package is an instance of the template.
3508
3509 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0025-1]}
3510 @ChgAdded{Version=[3],Text=[The @i(generic_formal_parameter_)@nt{selector_name}
3511 of a @nt{formal_package_association} shall
3512 denote a @nt{generic_formal_parameter_declaration} of the template. If two or
3513 more formal subprograms of the template have the same defining name, then named
3514 associations are not allowed for the corresponding actuals.]}
3515
3516 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00398-01]}
3517 @ChgRef{Version=[3],Kind=[RevisedAdded]}@Comment{Because this is renumbered by the above}
3518 @ChgAdded{Version=[2],Text=[A @nt<formal_package_actual_part> shall contain
3519 at most one @nt<formal_package_association> for each formal parameter. If the
3520 @nt<formal_package_actual_part> does not include
3521 @lquotes@key[others] => <>@rquotes, each
3522 formal parameter without an association shall have a @nt<default_expression>
3523 or @nt<subprogram_default>.]}
3524
3525 @ChgRef{Version=[3],Kind=[Added],ARef=[AI05-0200-1]}
3526 @ChgAdded{Version=[3],Text=[The rules for matching between
3527 @nt{formal_package_association}s and the generic formals of the template are as
3528 follows:]}
3529 @begin{Itemize}
3530
3531 @ChgRef{Version=[3],Kind=[Added]}
3532 @ChgAdded{Version=[3],Text=[If all of the @nt{formal_package_association}s are
3533 given by generic associations, the @nt{explicit_generic_actual_parameter}s of
3534 the @nt{formal_package_association}s shall be legal for an instantiation of the
3535 template.]}
3536
3537 @ChgRef{Version=[3],Kind=[Added]}
3538 @ChgAdded{Version=[3],Text=[If a @nt{formal_package_association} for a formal
3539 type @i<T> of the template is given by <>, then the
3540 @nt{formal_package_association} for any other
3541 @nt{generic_formal_parameter_declaration} of
3542 the template that mentions @i<T> directly or indirectly must be
3543 given by <> as well.]}
3544
3545 @end{Itemize}
3546 @begin{Discussion}
3547 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0200-1]}
3548 @ChgAdded{Version=[3],Text=[The above rule is simple to state, though it does
3549 not reflect the fact that the formal package functions like an instantiation
3550 of a special kind, where each box association for a
3551 @nt{generic_formal_parameter_declaration} @i<F> is replaced with a new entity
3552 @i<F>' that has the same characteristics as @i<F>: if @i<F> is a formal
3553 discrete type then @i<F>' is a discrete type, if @i<F> is a formal subprogram
3554 then @i<F>' is a subprogram with a similar signature, etc. In practice this is
3555 achieved by making the association into a copy of the declaration of the
3556 generic formal.]}
3557
3558 @end{Discussion}
3559
3560 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00317-01]}
3561 @Leading@;The actual shall be an instance of the template.
3562 If the @nt<formal_package_actual_part> is (<>)@Chg{Version=[2],
3563 New=[ or (@key{others} => <>)],Old=[]},
3564 @Redundant[then the actual may be any instance of the template]; otherwise,
3565 @Chg{Version=[2],New=[certain of the actual parameters],
3566 Old=[each actual parameter]}
3567 of the actual instance shall match the corresponding
3568 actual @Chg{Version=[2],New=[parameters],Old=[parameter]}
3569 of the formal package@Chg{Version=[2],New=[, determined],
3570 Old=[ @Redundant[(whether the
3571 actual parameter is given explicitly or by default)],]} as follows:
3572
3573 @begin{Itemize}
3574 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00317-01]}
3575 @ChgAdded{Version=[2],Text=[If the @nt{formal_@!package_@!actual_@!part}
3576 includes @nt{generic_association}s as well as associations with <>,
3577 then only the actual parameters specified explicitly with
3578 @nt{generic_association}s are required to match;]}
3579
3580 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00317-01]}
3581 @ChgAdded{Version=[2],Text=[Otherwise, all actual parameters shall
3582 match@Redundant[, whether any actual parameter is given explicitly
3583 or by default].]}
3584 @end{Itemize}
3585
3586 @ChgRef{Version=[2],Kind=[Added],ARef=[AI95-00317-01]}
3587 @ChgAdded{Version=[2],Type=[Leading],Text=[The rules for matching of
3588 actual parameters between the actual instance and the formal package
3589 are as follows:]}
3590
3591 @begin{Itemize}
3592 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00317-01]}
3593 For a formal object of mode @key[in]@Chg{Version=[2],New=[,],Old=[]} the
3594 actuals match if they are
3595 static expressions with the same value, or if they statically denote
3596 the same constant,
3597 or if they are both the literal @key[null].
3598 @begin{Reason}
3599 We can't simply require full conformance between the two
3600 actual parameter expressions, because the two
3601 expressions are being evaluated at different times.
3602 @end{Reason}
3603
3604 For a formal subtype, the actuals match if they denote
3605 statically matching subtypes.
3606 @PDefn2{Term=[statically matching],Sec=(required)}
3607
3608 For other kinds of formals, the actuals match if they statically
3609 denote the same entity.
3610 @end{Itemize}
3611 @ChgRef{Version=[1],Kind=[Added],Ref=[8652/0039],ARef=[AI95-00213-01]}
3612 @ChgAdded{Version=[1],Text=[For the purposes of matching, any actual parameter
3613 that is the name
3614 of a formal object of mode @key{in} is replaced by the formal object's actual
3615 expression (recursively).]}
3616 @end{Legality}
3617
3618 @begin{StaticSem}
3619 A @nt{formal_package_declaration} declares a generic formal package.
3620
3621 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00317-01]}
3622 @PDefn2{Term=[visible part], Sec=(of a formal package)}
3623 The visible part of a formal package includes
3624 the first list of @nt{basic_declarative_item}s of the
3625 @nt{package_@!specification}.
3626 In addition, @Chg{Version=[2],New=[for each actual parameter that is
3627 not required to match, a copy of the
3628 declaration of the corresponding formal parameter of the template is
3629 included in the visible part of the formal package. If the copied
3630 declaration is for a formal type, copies of the implicit declarations
3631 of the primitive subprograms of the formal type are also included in
3632 the visible part of],
3633 Old=[if the @nt{formal_@!package_@!actual_@!part} is (<>),
3634 it also includes the @nt{generic_@!formal_@!part} of the template
3635 for]} the formal package.
3636
3637 @begin{Ramification}
3638 @ChgRef{Version=[2],Kind=[Revised],ARef=[AI95-00317-01]}
3639 If the @nt<formal_package_actual_part> is (<>),
3640 then the declarations that occur immediately within the
3641 @nt<generic_formal_part> of the template for the formal package
3642 are visible outside the formal package,
3643 and can be denoted by expanded names outside the formal
3644 package.@Chg{Version=[2],New=[If only some of the actual parameters are
3645 given by <>, then the declaration corresponding to those parameters (but
3646 not the others) are made visible.],Old=[]}
3647 @end{Ramification}
3648 @begin{Reason}
3649 @ChgRef{Version=[3],Kind=[Revised],ARef=[AI05-0005-1]}
3650 We always want either the actuals
3651 or the formals of an instance to be
3652 @Chg{Version=[3],New=[nameable],Old=[namable]} from outside, but never both.
3653 If both were @Chg{Version=[3],New=[nameable],Old=[namable]}, one would get
3654 some funny anomalies since
3655 they denote the same entity, but, in the case of types at least,
3656 they might have different and inconsistent sets of primitive operators
3657 due to predefined operator @lquotes@;reemergence.@rquotes@; Formal derived types
3658 exacerbate the difference. We want the implicit declarations
3659 of the @nt<generic_formal_part> as well as the explicit
3660 declarations, so we get operations on the formal types.
3661 @end{Reason}
3662 @begin{Ramification}
3663 A generic formal package is a package, and is an instance.
3664 Hence, it is possible to pass a generic formal package
3665 as an actual to another generic formal package.
3666 @end{Ramification}
3667
3668 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00317-01]}
3669 @ChgAdded{Version=[2],Text=[
3670 For the purposes of matching, if the actual instance @i<A> is itself a
3671 formal package, then the actual parameters of @i<A> are those specified
3672 explicitly or implicitly in the @nt{formal_package_actual_part} for @i<A>, plus,
3673 for those not specified, the copies of the formal parameters of the
3674 template included in the visible part of @i<A>.]}
3675
3676 @end{StaticSem}
3677
3678 @begin{Examples}
3679 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00433-01]}
3680 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[@i{Example of a generic
3681 package with formal package parameters:}]}
3682
3683 @begin{Example}
3684 @ChgRef{Version=[2],Kind=[AddedNormal]}
3685 @ChgAdded{Version=[2],Text=[@key[with] Ada.Containers.Ordered_Maps; --@RI[ see @RefSecNum{The Generic Package Containers.Ordered_Maps}]
3686 @key[generic]
3687 @key[with package] Mapping_1 @key[is new] Ada.Containers.Ordered_Maps(<>);
3688 @key[with package] Mapping_2 @key[is new] Ada.Containers.Ordered_Maps
3689 (Key_Type => Mapping_1.Element_Type,
3690 @key[others] => <>);
3691 @key[package] Ordered_Join @key[is]
3692 --@RI[ Provide a "join" between two mappings]]}
3693
3694 @ChgRef{Version=[2],Kind=[AddedNormal]}
3695 @ChgAdded{Version=[2],Text=[ @key[subtype] Key_Type @key[is] Mapping_1.Key_Type;
3696 @key[subtype] Element_Type @key[is] Mapping_2.Element_Type;]}
3697
3698 @ChgRef{Version=[2],Kind=[AddedNormal]}
3699 @ChgAdded{Version=[2],Text=[ @key[function] Lookup(Key : Key_Type) @key[return] Element_Type;]}
3700
3701 @ChgRef{Version=[2],Kind=[AddedNormal]}
3702 @ChgAdded{Version=[2],Text=[ ...
3703 @key[end] Ordered_Join;]}
3704 @end{Example}
3705
3706 @RMNewPageVer{Version=[2]}@Comment{For printed version of Ada 2005 RM}
3707 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00433-01]}
3708 @ChgAdded{Version=[2],Type=[Leading],Keepnext=[T],Text=[@i{Example of
3709 an instantiation of a package with formal packages:}]}
3710
3711 @begin{Example}
3712 @ChgRef{Version=[2],Kind=[AddedNormal]}
3713 @ChgAdded{Version=[2],Text=[@key[with] Ada.Containers.Ordered_Maps;
3714 @key[package] Symbol_Package @key[is]]}
3715
3716 @ChgRef{Version=[2],Kind=[AddedNormal]}
3717 @ChgAdded{Version=[2],Text=[ @key[type] String_Id @key[is] ...]}
3718
3719 @ChgRef{Version=[2],Kind=[AddedNormal]}
3720 @ChgAdded{Version=[2],Text=[ @key[type] Symbol_Info @key[is] ...]}
3721
3722 @ChgRef{Version=[2],Kind=[AddedNormal]}
3723 @ChgAdded{Version=[2],Text=[ @key[package] String_Table @key[is new] Ada.Containers.Ordered_Maps
3724 (Key_Type => String,
3725 Element_Type => String_Id);]}
3726
3727 @ChgRef{Version=[2],Kind=[AddedNormal]}
3728 @ChgAdded{Version=[2],Text=[ @key[package] Symbol_Table @key[is new] Ada.Containers.Ordered_Maps
3729 (Key_Type => String_Id,
3730 Element_Type => Symbol_Info);]}
3731
3732 @ChgRef{Version=[2],Kind=[AddedNormal]}
3733 @ChgAdded{Version=[2],Text=[ @key[package] String_Info @key[is new] Ordered_Join(Mapping_1 => String_Table,
3734 Mapping_2 => Symbol_Table);]}
3735
3736 @ChgRef{Version=[2],Kind=[AddedNormal]}
3737 @ChgAdded{Version=[2],Text=[ Apple_Info : @key[constant] Symbol_Info := String_Info.Lookup("Apple");]}
3738
3739 @ChgRef{Version=[2],Kind=[AddedNormal]}
3740 @ChgAdded{Version=[2],Text=[@key[end] Symbol_Package;]}
3741
3742 @end{Example}
3743 @end{Examples}
3744
3745
3746
3747 @begin{Extend83}
3748 @Defn{extensions to Ada 83}
3749 Formal packages are new to Ada 95.
3750 @end{Extend83}
3751
3752 @begin{Extend95}
3753 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00317-01],ARef=[AI95-00398-01]}
3754 @ChgAdded{Version=[2],Text=[@Defn{extensions to Ada 95}
3755 It's now allowed to mix actuals of a formal package that are specified
3756 with those that are not specified.]}
3757 @end{Extend95}
3758
3759 @begin{DiffWord95}
3760 @ChgRef{Version=[2],Kind=[AddedNormal],Ref=[8652/0039],ARef=[AI95-00213-01]}
3761 @ChgAdded{Version=[2],Text=[@b<Corrigendum:> Corrected the description of
3762 formal package matching to say that formal parameters are always replaced by
3763 their actual parameters (recursively). This matches the actual practice of
3764 compilers, as the ACATS has always required this behavior.]}
3765
3766 @ChgRef{Version=[2],Kind=[AddedNormal],ARef=[AI95-00317-01]}
3767 @ChgAdded{Version=[2],Text=[The description of which operations are
3768 visible in a formal package has been clarified. We also specify how matching
3769 is done when the actual is a formal package.]}
3770 @end{DiffWord95}
3771
3772 @begin{Incompatible2005}
3773 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0025-1],ARef=[AI05-0200-1]}
3774 @ChgAdded{Version=[3],Text=[@Defn{incompatibilities with Ada 2005}@b<Correction:>
3775 Added missing rules for parameters of generic formal package that
3776 parallel those in @RefSecNum{Generic Instantiation}, as well as some
3777 specific to <> parameters. These are technically incompatibilities because
3778 generic formal package parameters that Ada 95
3779 and Ada 2005 would have considered legal now have to be rejected. But this
3780 should not be an issue in practice as such formal parameters could not have
3781 matched any actual generics. And it is quite likely that implementations
3782 already enforce some of these rules.]}
3783 @end{Incompatible2005}
3784
3785 @begin{Extend2005}
3786 @ChgRef{Version=[3],Kind=[AddedNormal],ARef=[AI05-0183-1]}
3787 @ChgAdded{Version=[3],Text=[@Defn{extensions to Ada 2005}
3788 An optional @nt{aspect_specification} can be used in
3789 a @nt{formal_package_declaration}.
3790 This is described in @RefSecNum{Aspect Specifications}.]}
3791 @end{Extend2005}
3792
3793
3794 @LabeledClause{Example of a Generic Package}
3795
3796 @begin{Intro}
3797 The following example provides a possible formulation of stacks by means
3798 of a generic package.
3799 The size of each stack and the type of the stack elements are provided
3800 as generic formal parameters.
3801 @end{Intro}
3802
3803 @begin{Examples}
3804 @Leading
3805 @ChgRef{Version=[1], Kind=[Deleted]}
3806 @ChgDeleted[Version=[1],Text=<@ @;@comment{Empty paragraph to hang junk paragraph number from original RM}>]
3807 @begin{Example}
3808 @key[generic]
3809 Size : Positive;
3810 @key[type] Item @key[is] @key[private];
3811 @key[package] Stack @key[is]
3812 @key[procedure] Push(E : @key[in] Item);
3813 @key[procedure] Pop (E : @key[out] Item);
3814 Overflow, Underflow : @key[exception];
3815 @key[end] Stack;
3816
3817 @key[package] @key[body] Stack @key[is]
3818
3819 @key[type] Table @key[is] @key[array] (Positive @key[range] <>) @key[of] Item;
3820 Space : Table(1 .. Size);
3821 Index : Natural := 0;
3822
3823 @key[procedure] Push(E : @key[in] Item) @key[is]
3824 @key[begin]
3825 @key[if] Index >= Size @key[then]
3826 @key[raise] Overflow;
3827 @key[end] @key[if];
3828 Index := Index + 1;
3829 Space(Index) := E;
3830 @key[end] Push;
3831
3832 @key[procedure] Pop(E : @key[out] Item) @key[is]
3833 @key[begin]
3834 @key[if] Index = 0 @key[then]
3835 @key[raise] Underflow;
3836 @key[end] @key[if];
3837 E := Space(Index);
3838 Index := Index - 1;
3839 @key[end] Pop;
3840
3841 @key[end] Stack;
3842 @end{Example}
3843
3844 @begin{WideAbove}
3845 @Leading@Keepnext@;Instances of this generic package can be obtained as follows:
3846 @end{WideAbove}
3847 @begin{Example}
3848 @key[package] Stack_Int @key[is] @key[new] Stack(Size => 200, Item => Integer);
3849 @key[package] Stack_Bool @key[is] @key[new] Stack(100, Boolean);
3850 @end{Example}
3851
3852 @begin{WideAbove}
3853 @Leading@Keepnext@;Thereafter, the procedures of the instantiated packages can be called as
3854 follows:
3855 @end{WideAbove}
3856 @begin{Example}
3857 Stack_Int.Push(N);
3858 Stack_Bool.Push(True);
3859 @end{Example}
3860
3861 @begin{WideAbove}
3862 @Leading@Keepnext@;Alternatively, a generic formulation of the type Stack can be given as
3863 follows (package body omitted):
3864 @end{WideAbove}
3865 @begin{Example}
3866 @key[generic]
3867 @key[type] Item @key[is] @key[private];
3868 @key[package] On_Stacks @key[is]
3869 @key[type] Stack(Size : Positive) @key[is] @key[limited] @key[private];
3870 @key[procedure] Push(S : @key[in] @key[out] Stack; E : @key[in] Item);
3871 @key[procedure] Pop (S : @key[in] @key[out] Stack; E : @key[out] Item);
3872 Overflow, Underflow : @key[exception];
3873 @key[private]
3874 @key[type] Table @key[is] @key[array] (Positive @key[range] <>) @key[of] Item;
3875 @key[type] Stack(Size : Positive) @key[is]
3876 @key[record]
3877 Space : Table(1 .. Size);
3878 Index : Natural := 0;
3879 @key[end] @key[record];
3880 @key[end] On_Stacks;
3881 @end{Example}
3882
3883 @begin{WideAbove}
3884 @Leading@Keepnext@;In order to use such a package, an instance has to be created and
3885 thereafter stacks of the corresponding type can be declared:
3886 @end{WideAbove}
3887 @begin{Example}
3888 @key[declare]
3889 @key[package] Stack_Real @key[is] @key[new] On_Stacks(Real); @key[use] Stack_Real;
3890 S : Stack(100);
3891 @key[begin]
3892 ...
3893 Push(S, 2.54);
3894 ...
3895 @key[end];
3896 @end{Example}
3897 @end{Examples}