Debian Codegraph @

source_2005/12.mss

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