Subunits of Compilation Units
Author: JTC1/SC22/WG9/ARG, by Randall Brukardt, ARG Editor

Ada Reference Manual — Legal Information

# 10.1.3 Subunits of Compilation Units

1
Subunits are like child units, with these (important) differences: subunits support the separate compilation of bodies only (not declarations); the parent contains a body_stub to indicate the existence and place of each of its subunits; declarations appearing in the parent's body can be visible within the subunits.

#### Syntax

2
body_stub ::= subprogram_body_stub | package_body_stub | task_body_stub | protected_body_stub
3/3
subprogram_body_stub ::=
[overriding_indicator]
subprogram_specification is separate
[aspect_specification];
4
package_body_stub ::=
package body defining_identifier is separate
[aspect_specification];
5
task_body_stub ::=
task body defining_identifier is separate
[aspect_specification];
6
protected_body_stub ::=
protected body defining_identifier is separate
[aspect_specification];
7
subunit ::= separate (parent_unit_name) proper_body

#### Legality Rules

8/2
The parent body of a subunit is the body of the program unit denoted by its parent_unit_name. The term subunit is used to refer to a subunit and also to the proper_body of a subunit. The subunits of a program unit include any subunit that names that program unit as its parent, as well as any subunit that names such a subunit as its parent (recursively).
9
The parent body of a subunit shall be present in the current environment, and shall contain a corresponding body_stub with the same defining_identifier as the subunit.
10/3
A package_body_stub shall be the completion of a package_declaration or generic_package_declaration; a task_body_stub shall be the completion of a task declaration; a protected_body_stub shall be the completion of a protected declaration.
11
In contrast, a subprogram_body_stub need not be the completion of a previous declaration, in which case the _stub declares the subprogram. If the _stub is a completion, it shall be the completion of a subprogram_declaration or generic_subprogram_declaration. The profile of a subprogram_body_stub that completes a declaration shall conform fully to that of the declaration.
12
A subunit that corresponds to a body_stub shall be of the same kind (package_, subprogram_, task_, or protected_) as the body_stub. The profile of a subprogram_body subunit shall be fully conformant to that of the corresponding body_stub.
13
A body_stub shall appear immediately within the declarative_part of a compilation unit body. This rule does not apply within an instance of a generic unit.
14
The defining_identifiers of all body_stubs that appear immediately within a particular declarative_part shall be distinct.

#### Post-Compilation Rules

15
For each body_stub, there shall be a subunit containing the corresponding proper_body.
NOTES
16
4 The rules in 10.1.4, “The Compilation Process” say that a body_stub is equivalent to the corresponding proper_body. This implies:
17
Visibility within a subunit is the visibility that would be obtained at the place of the corresponding body_stub (within the parent body) if the context_clause of the subunit were appended to that of the parent body.
18
The effect of the elaboration of a body_stub is to elaborate the subunit.

#### Examples

19
The package Parent is first written without subunits:
20
package Parent is
procedure Inner;
end Parent;
21
with Ada.Text_IO;
package body Parent is
Variable : String := "Hello, there.";
procedure Inner is
begin
Ada.Text_IO.Put_Line(Variable);
end Inner;
end Parent;
22
The body of procedure Inner may be turned into a subunit by rewriting the package body as follows (with the declaration of Parent remaining the same):
23
package body Parent is
Variable : String := "Hello, there.";
procedure Inner is separate;
end Parent;
24
with Ada.Text_IO;
separate(Parent)
procedure Inner is
begin
Ada.Text_IO.Put_Line(Variable);
end Inner;

Ada 2005 and 2012 Editions sponsored in part by Ada-Europe