10.1.3 Subunits of Compilation Units
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
Legality Rules
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).
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.
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.
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.
Post-Compilation Rules
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.
The effect of the elaboration of
a
body_stub
is to elaborate the subunit.
Examples
The package Parent
is first written without subunits:
package Parent is
procedure Inner;
end Parent;
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;
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):
package body Parent is
Variable : String := "Hello, there.";
procedure Inner is separate;
end Parent;
with Ada.Text_IO;
separate(Parent)
procedure Inner is
begin
Ada.Text_IO.Put_Line(Variable);
end Inner;