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
3/2
3.a
4
5
6
7
Legality Rules
8/2
{
AI95-00243-01}
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).
8.a.1/2
Reason: {
AI95-00243-01}
We want any rule that applies to a subunit to apply
to a subunit of a subunit as well.
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.
9.a
Discussion: This can't be a Name Resolution
Rule, because a
subunit
is not a complete context.
10/3
11
11.a
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.
13.a
Discussion: This
is a methodological restriction; that is, it is not necessary for the
semantics of the language to make sense.
14
Post-Compilation Rules
15
16
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.
17.a
Ramification: Recursively. Note that
this transformation might make the parent illegal; hence it is not a
true equivalence, but applies only to visibility within the subunit.
18
The effect of the elaboration of
a
body_stub
is to elaborate the subunit.
18.a
Ramification: The elaboration of a subunit
is part of its parent body's elaboration, whereas the elaboration of
a child unit is not part of its parent declaration's elaboration.
18.b
Ramification: A
library_item
that is mentioned in a
with_clause
of a subunit can be hidden (from direct
visibility visiblity)
by a declaration (with the same
identifier)
given in the subunit. Moreover, such a
library_item
can even be hidden by a declaration given within the parent body since
a library unit is declared in its parent's declarative region; this however
does not affect the interpretation of the
with_clauses
themselves, since only
library_items
are visible or directly visible in
with_clauses.
18.c
The body of a protected operation cannot be
a subunit. This follows from the syntax rules. The body of a protected
unit can be a 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;
Extensions to Ada 83
24.a
Subunits of the same ancestor
library unit are no longer restricted to have distinct identifiers. Instead,
we require only that the full expanded names be distinct.
Extensions to Ada 95
24.b/2
Wording Changes from Ada 95
24.c/2
{
AI95-00243-01}
Clarified that a subunit of a subunit is still
a subunit.
Ada 2005 and 2012 Editions sponsored in part by Ada-Europe