12.3 Generic Instantiation
Syntax
A
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.
Legality Rules
In 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).
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.
Static Semantics
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.
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.
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.
Post-Compilation Rules
Recursive 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
For 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).
5 If a formal type is not tagged, then
the type is treated as an untagged type within the generic body. Deriving
from such a type in a generic body is permitted; the new type does not
get a new tag value, even if the actual is tagged. Overriding operations
for such a derived type cannot be dispatched to from outside the instance.
Examples
Examples 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 Square is new Squaring(Item => Matrix, "*" => Matrix_Product);
function Square is new Squaring(Matrix, Matrix_Product); -- same as previous
package Int_Vectors is new On_Vectors(Integer, Table, "+");
Examples of uses
of instantiated units:
Swap(A, B);
A := Square(A);
T : 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)
use Int_Vectors;
M : Integer := Sigma(T); -- 150