Skip to main content

12.1 Generic Declarations

danger

This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue

1

[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

2

generic_declaration ::=
generic_subprogram_declaration | generic_package_declaration

3/3

generic_subprogram_declaration ::=
generic_formal_part subprogram_specification
[aspect_specification];

4

generic_package_declaration ::=
generic_formal_part package_specification;

4.a/3
ramification

No syntax change is needed here to allow an aspect_specification; a generic package can have an aspect_specification because a package_specification allows an aspect_specification.

5

generic_formal_part ::=
generic {generic_formal_parameter_declaration | use_clause}

6

generic_formal_parameter_declaration ::=
formal_object_declaration
| formal_type_declaration
| formal_subprogram_declaration
| formal_package_declaration

7

The only form of subtype_indication allowed within a generic_formal_part is a subtype_mark [(that is, the subtype_indication shall not include an explicit constraint)]. The defining name of a generic subprogram shall be an identifier [(not an operator_symbol)].

7.a
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

8/2

A generic_declaration declares a generic unit — a generic package, generic procedure, or generic function, as appropriate.

9

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”.]

9.1/5

The list of generic_formal_parameter_declarations of a generic_formal_part form a declaration list of the generic unit.

9.a/5
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

10

The elaboration of a generic_declaration has no effect.

11

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.

11.a
proof

This is stated officially as part of the “current instance” rule in 8.6, “The Context of Overload Resolution”. See also 12.3, “Generic Instantiation”.

12

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.

13/5

NOTE 3 A default_expression or default_name appearing in a generic_formal_part is not evaluated during elaboration of the generic_formal_part; instead, it is evaluated when used. However, the usual visibility rules apply to any name used in a default, with name resolution performed based on the location of the name within the generic_formal_part.

Examples

14

Examples of generic formal parts:

15

generic -- parameterless 16 generic Size : Natural; -- formal object 17 generic Length : Integer := 200; -- formal object with a default expression 18 Area : Integer := Length*Length; -- formal object with a default expression 19 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

20

Examples of generic declarations declaring generic subprograms Exchange and Squaring:

21

generic type Elem is private; procedure Exchange(U, V : in out Elem); 22/5

generic type Item (<>) is private; with function "*"(U, V : Item) return Item is <>; function Squaring(X : Item) return Item;

23

Example of a generic declaration declaring a generic package:

24

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

24.a

The syntax rule for generic_formal_parameter_declaration is modified to allow the reserved words tagged and abstract, to allow formal derived types, and to allow formal packages.

24.b

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

24.c/3

The syntax for generic_formal_parameter_declaration and formal_type_definition is split up into more named categories. The rules for these categories are moved to the appropriate subclauses. The names of the categories are changed to be more intuitive and uniform. For example, we changed generic_parameter_declaration to generic_formal_parameter_declaration, because the thing it declares is a generic formal, not a generic. In the others, we abbreviate “generic_formal” to just “formal”. We can't do that for generic_formal_parameter_declaration, because of confusion with normal formal parameters of subprograms.

Extensions to Ada 2005

24.d/3

An optional aspect_specification can be used in a generic_subprogram_declaration (as well as a generic_package_declaration). This is described in 13.1.1.

Wording Changes from Ada 2012

24.e/5

Defined a formal part as a declaration list, so that the visibility of entities in aspect specifications is properly defined.