12.1 Generic Declarations
[A
generic_declaration
declares a generic unit, which is either a generic subprogram or a generic
package. A
generic_declaration
includes a
generic_formal_part
declaring any generic formal parameters. A generic formal parameter can
be an object; alternatively (unlike a parameter of a subprogram), it
can be a type, a subprogram, or a package.]
Syntax
Reason: The reason for forbidding
constraints
in
subtype_indications
is that it simplifies the elaboration of
generic_declarations
(since there is nothing to evaluate), and that it simplifies the matching
rules, and makes them more checkable at compile time.
Static Semantics
An entity is a
generic formal
entity if it is declared by a
generic_formal_parameter_declaration.
“Generic formal”, or simply “formal”, is used
as a prefix in referring to objects, subtypes (and types), functions,
procedures and packages, that are generic formal entities, as well as
to their respective declarations. [Examples: “generic formal procedure”
or a “formal integer type declaration”.]
Ramification: Aspect specifications (see
13.1.1) given in a generic formal part can
only use declarations given in the formal part, and not those in the
visible part of the generic unit.
Dynamic Semantics
NOTE 1 Outside a generic unit a
name
that denotes the
generic_declaration
denotes the generic unit. In contrast, within the declarative region
of the generic unit, a
name
that denotes the
generic_declaration
denotes the current instance.
NOTE 2 Within a generic
subprogram_body,
the name of this program unit acts as the name of a subprogram. Hence
this name can be overloaded, and it can appear in a recursive call of
the current instance. For the same reason, this name cannot appear after
the reserved word
new in a (recursive)
generic_instantiation.
Examples
Examples of generic
formal parts:
generic -- parameterless
generic
Size : Natural; -- formal object
generic
Length : Integer := 200; -- formal object with a default expression
Area : Integer := Length*Length; -- formal object with a default expression
generic
type Item is private; -- formal type
type Index is (<>); -- formal type
type Row is array(Index range <>) of Item; -- formal type
with function "<"(X, Y : Item) return Boolean; -- formal subprogram
Examples of generic
declarations declaring generic subprograms Exchange and Squaring:
generic
type Elem is private;
procedure Exchange(U, V : in out Elem);
{
AI12-0178-1}
generic
type Item (<>)
is private;
with function "*"(U, V : Item)
return Item
is <>;
function Squaring(X : Item)
return Item;
Example of a generic
declaration declaring a generic package:
generic
type Item is private;
type Vector is array (Positive range <>) of Item;
with function Sum(X, Y : Item) return Item;
package On_Vectors is
function Sum (A, B : Vector) return Vector;
function Sigma(A : Vector) return Item;
Length_Error : exception;
end On_Vectors;
Extensions to Ada 83
Use_clauses
are allowed in
generic_formal_parts.
This is necessary in order to allow a
use_clause
within a formal part to provide direct visibility of declarations within
a generic formal package.
Wording Changes from Ada 83
Extensions to Ada 2005
Wording Changes from Ada 2012
{
AI12-0371-1}
Defined a formal part as a declaration list, so that the visibility of
entities in aspect specifications is properly defined.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe