Debian Codegraph @

source_2020/12.mss

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