Ada Conformity Assessment Authority      Home Conformity Assessment   Test Suite ARGAda Standard
Annotated Ada Reference Manual (Ada 2022)Legal Information
Contents   Index   References   Search   Previous   Next 

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


body_stub ::= 
    subprogram_body_stub | package_body_stub
  | task_body_stub | protected_body_stub
{AI95-00218-03} {AI05-0267-1} subprogram_body_stub ::= 
    subprogram_specification is separate
Discussion: Although this syntax allows a parent_unit_name, that is disallowed by 10.1.1, “Compilation Units - Library Units”. 
{AI05-0267-1} package_body_stub ::= 
   package body defining_identifier is separate
{AI05-0267-1} task_body_stub ::= 
   task body defining_identifier is separate
{AI05-0267-1} protected_body_stub ::= 
   protected body defining_identifier is separate
subunit ::= separate (parent_unit_nameproper_body

Legality Rules

{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). 
Reason: {AI95-00243-01} We want any rule that applies to a subunit to apply to a subunit of a subunit as well. 
Term entry: subunit — body of a program unit that can be compiled separately from its enclosing program unit
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. 
Discussion: This can't be a Name Resolution Rule, because a subunit is not a complete context. 
{AI05-0004-1} 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.
{AI12-0444-1} In contrast, a subprogram_body_stub can be defined without it being 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.
Discussion: The part about subprogram_body_stubs echoes the corresponding rule for subprogram_bodies in 6.3, “Subprogram Bodies”. 
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. 
Discussion: This is a methodological restriction; that is, it is not necessary for the semantics of the language to make sense. 
The defining_identifiers of all body_stubs that appear immediately within a particular declarative_part shall be distinct. 

Post-Compilation Rules

For each body_stub, there shall be a subunit containing the corresponding proper_body.
NOTE   The rules in 10.1.4, “The Compilation Process” say that a body_stub is equivalent to the corresponding proper_body. This implies: 
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. 
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.
The effect of the elaboration of a body_stub is to elaborate the subunit. 
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. 
Ramification: A library_item that is mentioned in a with_clause of a subunit can be hidden (from direct visibility) 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.
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. 


{AI12-0429-1} Example that defines package Parent without subunits: 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
    end Inner;
end Parent;
{AI12-0429-1} {AI12-0440-1} Example showing how the body of procedure Inner can be turned into a subunit by rewriting the package body as follows (with the declaration of Parent remaining the same): 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;
procedure Inner is
end Inner;

Extensions to Ada 83

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

{AI95-00218-03} An overriding_indicator (see 8.3.1) is allowed on a subprogram stub.

Wording Changes from Ada 95

{AI95-00243-01} Clarified that a subunit of a subunit is still a subunit. 

Extensions to Ada 2005

{AI05-0267-1} An optional aspect_specification can be used in a body_stub. This is described in 13.1.1

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe