12.3 Generic Instantiation
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[ An instance of a generic unit is declared by a generic_instantiation
.]
Language Design Principles
generic_declaration
forms a contract between the body and the instances; if each obeys the rules with respect to the generic_declaration
, then no legality problems will arise. This is really a special case of the “legality determinable via semantic dependences” Language Design Principle (see Clause 10), given that a generic_instantiation
does not depend semantically upon the generic body, nor vice versa.Syntax
2/3generic_instantiation
::=
package defining_program_unit_name
is
new generic_package_name
[generic_actual_part
]
[aspect_specification
];
| [overriding_indicator
]
procedure defining_program_unit_name
is
new generic_procedure_name
[generic_actual_part
]
[aspect_specification
];
| [overriding_indicator
]
function defining_designator
is
new generic_function_name
[generic_actual_part
]
[aspect_specification
];
3generic_actual_part
::=
(generic_association
{, generic_association
})
4generic_association
::=
[generic_formal_parameter_selector_name
=>] explicit_generic_actual_parameter
5explicit_generic_actual_parameter
::=
expression
| variable_name
| subprogram_name
| entry_name
| subtype_mark
| package_instance_name
6A generic_association
is named or positional according to whether or not the generic_formal_parameter_selector_name
is specified. Any positional associations shall precede any named associations.
The generic actual parameter is either the explicit_generic_actual_parameter
given in a generic_association
for each formal, or the corresponding default_expression
, default_subtype_mark
, or default_name
if no generic_association
is given for the formal. When the meaning is clear from context, the term “generic actual ”, or simply “actual ”, is used as a synonym for “generic actual parameter” and also for the view denoted by one, or the value of one.
default_expression
, default_subtype_mark
, or default_name
that are used as an actual. Legality Rules
8In a generic_instantiation
for a particular kind of program unit [(package, procedure, or function)], the name
shall denote a generic unit of the corresponding kind [(generic package, generic procedure, or generic function, respectively)].
The generic_formal_parameter_selector_name
of a named generic_association
shall denote a generic_formal_parameter_declaration
of the generic unit being instantiated. If two or more formal subprograms have the same defining name, then named associations are not allowed for the corresponding actuals.
The generic_formal_parameter_declaration
for a positional generic_association
is the parameter with the corresponding position in the generic_formal_part
of the generic unit being instantiated.
A generic_instantiation
shall contain at most one generic_association
for each formal. Each formal without an association shall have a default_expression
, default_subtype_mark
, or subprogram_default
.
In a generic unit, Legality Rules are enforced at compile time of the generic_declaration
and generic body, given the properties of the formals. In the visible part and formal part of an instance, Legality Rules are enforced at compile time of the generic_instantiation
, given the properties of the actuals. In other parts of an instance, Legality Rules are not enforced; this rule does not apply when a given rule explicitly specifies otherwise.
record_extension_part
. A formal tagged limited type is limited, but the actual might be nonlimited. Hence any rule that requires a tagged type to be limited runs into this problem. Such rules are rare; in most cases, the rules for matching of formals and actuals guarantee that if the rule is obeyed in the generic unit, then it has to be obeyed in the instance.- {8652/0095} A formal derived subtype is constrained if and only if the ancestor subtype is constrained. A formal array type is constrained if and only if the declarations say so. A formal private type is constrained if it does not have a discriminant part. Other formal subtypes are unconstrained, even though they might be constrained in an instance.
- A formal subtype can be indefinite, even though the copy might be definite in an instance.
- A formal object of mode in is not a static constant; in an instance, the copy is static if the actual is.
- A formal subtype is not static, even though the actual might be.
- Formal types are specific, even though the actual can be class-wide.
- The subtype of a formal object of mode in out is not static. (This covers the case of AI83-00878.)
- The subtype of a formal parameter of a formal subprogram does not provide an applicable index constraint.
- The profile of a formal subprogram is not subtype conformant with any other profile.
- A generic formal function is not static.
- Some rules are checked in the generic declaration, and then again in both the visible and private parts of the instance:
- The parent type of a record extension has to be specific (see 3.9.1). This rule is not checked in the instance body.
- The parent type of a private extension has to be specific (see 7.3). This rule is not checked in the instance body.
- A type with an access discriminant with a
default_expression
has to be immutably limited. In the generic body, the definition of immutably limited is adjusted in an assume-the-worst manner (thus the rule is checked that way). 11.q - In the declaration of a record extension, if the parent type is nonlimited, then each of the components of the
record_extension_part
have to be nonlimited (see 3.9.1). In the generic body, this rule is checked in an assume-the-worst manner. 11.r - A preelaborated library unit has to be preelaborable (see 10.2.1). In the generic body, this rule is checked in an assume-the-worst manner.
- The corrections made by the Corrigendum added a number of such rules, and the Amendment added many more. There doesn't seem to be much value in repeating all of these rules here (as of this writing, there are roughly 33 such rules). As noted below, all such rules are indexed in the AARM.
- For the accessibility rules, the formals have nothing to say about the property in question. Like the above rules, these rules are checked in the generic declaration, and then again in both the visible and private parts of the instance. In the generic body, we have explicit rules that essentially assume the worst (in the cases of type extensions and access-to-subprogram types), and we have runtime checks (in the case of access-to-object types). See 3.9.1, 3.10.2, and 4.6.
- We considered runtime checks for access-to-subprogram types as well. However, this would present difficulties for implementations that share generic bodies.
- The rules requiring “reasonable” values for static expressions are ignored when the expected type for the expression is a descendant of a generic formal type other than a generic formal derived type, and do not apply in an instance.
- The rule forbidding two explicit homographs in the same declarative region does not apply in an instance of a generic unit, except that it does apply in the declaration of a record extension that appears in the visible part of an instance.
- Some rules do not apply at all in an instance, not even in the visible part:
record_extension_part
shall be nonlimited.” generic
type Parent is tagged private;
type Comp is limited private;
package G1 is
type Extension is new Parent with
record
C : Comp; -- Illegal!
end record;
end G1;
generic
type Parent is tagged limited private; -- Parent is limited.
type Comp is limited private;
package G2 is
type Extension is new Parent with
record
C : Comp; -- OK.
end record;
end G2;
11.ggtype Limited_Tagged is tagged limited null record;
type Non_Limited_Tagged is tagged null record;
11.hhtype Limited_Untagged is limited null record;
type Non_Limited_Untagged is null record;
11.iipackage Good_1 is new G2(Parent => Limited_Tagged,
Comp => Limited_Untagged);
package Good_2 is new G2(Parent => Non_Limited_Tagged,
Comp => Non_Limited_Untagged);
package Bad is new G2(Parent => Non_Limited_Tagged,
Comp => Limited_Untagged); -- Illegal!
Static Semantics
12A generic_instantiation
declares an instance; it is equivalent to the instance declaration (a package_declaration
or subprogram_declaration
) immediately followed by the instance body, both at the place of the instantiation.
generic_instantiation
is that of the instance declaration, by the stated equivalence. The instance is a copy of the text of the template. [Each use of a formal parameter becomes (in the copy) a use of the actual, as explained below.] An instance of a generic package is a package, that of a generic procedure is a procedure, and that of a generic function is a function.
generic_formal_part
, and therefore doesn't look like one. This is strange, but it's OK, since the syntax rules are overloading rules, and therefore do not apply in an instance. name
denoting a declaration D, then in an instance, the copy of the construct will still be a name, and will still denote D (or a copy of D). From an implementation point of view, overload resolution is performed on the template, and not on each copy.generic_formal_parameter_declaration
. The copy of that name in an instance will denote the copy of that generic_formal_parameter_declaration
in the instance. Since the generic_formal_parameter_declaration
in the instance declares a view of the actual, the name will denote a view of the actual.generic_formal_part
is included in an instance, the declarations in the generic_formal_part
are only visible outside the instance in the case of a generic formal package whose formal_package_actual_part
includes one or more <> indicators — see 12.7. The interpretation of each construct within a generic declaration or body is determined using the overloading rules when that generic declaration or body is compiled. In an instance, the interpretation of each (copied) construct is the same, except in the case of a name that denotes the generic_declaration
or some declaration within the generic unit; the corresponding name in the instance then denotes the corresponding copy of the denoted declaration. The overloading rules do not apply in the instance.
generic_formal_parameter_declaration
s have corresponding declarations in the instance, which declare views of the actuals.generic_formal_part
.generic_instantiation
in order to determine the interpretation of the constituents of the generic_instantiation
.package_specification
, even though the Syntax Rules forbid bodies in package_specification
s. In an instance, a generic_formal_parameter_declaration
declares a view whose properties are identical to those of the actual, except when specified otherwise (in particular, see 6.1.1, “Preconditions and Postconditions”, 12.4, “Formal Objects”, and 12.6, “Formal Subprograms”). Similarly, for a declaration within a generic_formal_parameter_declaration
, the corresponding declaration in an instance declares a view whose properties are identical to the corresponding declaration within the declaration of the actual.
formal_type_declaration
can contain discriminant_specification
s, a formal_subprogram_declaration
can contain parameter_specification
s, and a formal_package_declaration
can contain many kinds of declarations. These are all inside the generic unit, and have corresponding declarations in the instance.generic_instantiation
, if a generic actual is a static [(scalar or string)] subtype, then each use of the corresponding formal parameter within the specification of the instance is considered to be static. (See AI83-00409.)type T1 is tagged record ... end record;
15.i/5generic
type Formal is new T1 with private;
package G is
type Derived_From_Formal is new Formal with record ... end record;
procedure Foo(X : in Derived_From_Formal); -- Does not override anything.
end G;
15.jtype T2 is new T1 with record ... end record;
procedure Foo(X : in T2);
15.kpackage Inst is new G(Formal => T2);
aspect_clause
s are determined by the actual. [Implicit declarations are also copied, and a name that denotes an implicit declaration in the generic denotes the corresponding copy in the instance. However, for a type declared within the visible part of the generic, a whole new set of primitive subprograms is implicitly declared for use outside the instance, and may differ from the copied set if the properties of the type in some way depend on the properties of some actual type specified in the instantiation. For example, if the type in the generic is derived from a formal private type, then in the instance the type will inherit subprograms from the corresponding actual type.
These new implicit declarations occur immediately after the type declaration in the instance, and override the copied ones. The copied ones can be called only from within the instance; the new ones can be called only from outside the instance, although for tagged types, the body of a new one can be executed by a call to an old one.]
[In the visible part of an instance, an explicit declaration overrides an implicit declaration if they are homographs, as described in 8.3.] On the other hand, an explicit declaration in the private part of an instance overrides an implicit declaration in the instance, only if the corresponding explicit declaration in the generic overrides a corresponding implicit declaration in the generic. Corresponding rules apply to the other kinds of overriding described in 8.3.
type Ancestor is tagged null record;
18.cgeneric
type Formal is new Ancestor with private;
package G is
type T is new Formal with null record;
procedure P(X : in T); -- (1)
private
procedure Q(X : in T); -- (2)
end G;
18.dtype Actual is new Ancestor with null record;
procedure P(X : in Actual);
procedure Q(X : in Actual);
18.epackage Instance is new G(Formal => Actual);
generic_instantiation
need not look at the private part of the generic in order to determine which subprograms will be overridden. Post-Compilation Rules
19Recursive generic instantiation is not allowed in the following sense: if a given generic unit includes an instantiation of a second generic unit, then the instance generated by this instantiation shall not include an instance of the first generic unit [(whether this instance is generated directly, or indirectly by intermediate instantiations)].
Dynamic Semantics
20For the elaboration of a generic_instantiation
, each generic_association
is first evaluated. If a default is used, an implicit generic_association
is assumed for this rule. These evaluations are done in an arbitrary order, except that the evaluation for a default actual takes place after the evaluation for another actual if the default includes a name
that denotes the other one. Finally, the instance declaration and body are elaborated.
For the evaluation of a generic_association
the generic actual parameter is evaluated. Additional actions are performed in the case of a formal object of mode in (see 12.4).
subtype_mark
s. generic
type A is (<>);
type B is private;
package G is
function Next(X : A) return A;
function Next(X : B) return B;
end G;
22.cpackage P is new G(A => Boolean, B => Boolean);
-- All calls of P.Next are ambiguous.
generic
type T1 is private;
-- A predefined "=" operator is implicitly declared here:
-- function "="(Left, Right : T1) return Boolean;
-- Call this "="1.
...
package G is
subtype S1 is T1; -- So we can get our hands on the type from
-- outside an instance.
type T2 is new T1;
-- An inherited "=" operator is implicitly declared here:
-- function "="(Left, Right : T2) return Boolean;
-- Call this "="2.
22.fT1_Obj : T1 := ...;
Bool_1 : Boolean := T1_Obj = T1_Obj;
22.gT2_Obj : T2 := ...;
Bool_2 : Boolean := T2_Obj = T2_Obj;
end G;
...
22.h/5package P is
type My_Int is new Integer;
-- A predefined "=" operator is implicitly declared here:
-- function "="(Left, Right : My_Int) return Boolean;
-- Call this "="3.
function "="(X, Y : My_Int) return Boolean;
-- Call this "="4.
-- "="3 is hidden from all visibility by "="4.
-- Nonetheless, "="3 can “reemerge” in certain circumstances.
end P;
use P;
...
package I is new G(T1 => My_Int, ...); -- "="5 is declared in I (see below).
use I;
22.iAnother_T1_Obj : S1 := 13; -- Can't denote T1, but S1 will do.
Bool_3 : Boolean := Another_T1_Obj = Another_T1_Obj;
22.jAnother_T2_Obj : T2 := 45;
Bool_4 : Boolean := Another_T2_Obj = Another_T2_Obj;
22.kDouble : T2 := T2_Obj + Another_T2_Obj;
Examples
23Examples of generic instantiations (see 12.1):
procedure Swap is new Exchange(Elem => Integer);
procedure Swap is new Exchange(Character); -- Swap is overloaded
function Square is new Squaring(Integer); -- "*" of Integer used by default
function Square1 is new Squaring(Item => Matrix, "*" => Matrix_Product);
function Square2 is new Squaring(Matrix, Matrix_Product); -- same as previous
25package Int_Vectors is new On_Vectors(Integer, Table, "+");
26Examples of uses of instantiated units:
Swap(A, B);
A := Square(A);
28T : Table(1 .. 5) := (10, 20, 30, 40, 50);
N : Integer := Int_Vectors.Sigma(T); -- 150
-- (see 12.2, “Generic Bodies” for the body of Sigma)
29use Int_Vectors;
M : Integer := Sigma(T); -- 150
Inconsistencies With Ada 83
Incompatibilities With Ada 83
Extensions to Ada 83
Wording Changes from Ada 83
explicit_generic_actual_parameter
shall not be supplied more than once for a given generic formal parameter” seems to be missing from RM83, although it was clearly the intent.generic_instantiation
(RM83-12.3(17)); we don't elaborate entities, and the instance is not “implicit”.Extensions to Ada 95
Extensions to Ada 2005
aspect_specification
can be used in a generic_instantiation
. This is described in 13.1.1.