8.2 Scope of Declarations
This Reference Manual output has not been verified, and may contain omissions or errors. Report any problems on the tracking issue
[For each declaration, the language rules define a certain portion of the program text called the scope of the declaration. The scope of a declaration is also called the scope of any view or entity declared by the declaration. Within the scope of an entity, and only there, there are places where it is legal to refer to the declared entity. These places are defined by the rules of visibility and overloading.]
The immediate scope of a declaration is a portion of the declarative region immediately enclosing the declaration. The immediate scope starts at the beginning of the declaration, except in the case of an overloadable declaration, in which case the immediate scope starts just after the place where the profile of the callable entity is determined (which is at the end of the
_specification for the callable entity, or at the end of the
generic_instantiation if an instance). The immediate scope extends to the end of the declarative region, with the following exceptions:
generic_instantiationwould be required to resolve names that denote things with the same name as this one.
- The immediate scope of a
library_itemincludes only its semantic dependents.
limited_with_clausefor the limited view.
- The immediate scope of a declaration in the private part of a library unit does not include the visible part of any public descendant of that library unit.
subprogram_bodiesthat are not completions. For a public child generic unit, it means that the parent's private part is not visible in the
generic_formal_part, as well as in the first list of
basic_declarative_items (for a generic package), or the (syntactic) profile (for a generic subprogram).
[The visible part of (a view of) an entity is a portion of the text of its declaration containing declarations that are visible from outside.] The private part of (a view of) an entity that has a visible part contains all declarations within the declaration of (the view of) the entity, except those in the visible part; [these are not visible from outside. Visible and private parts are defined only for these kinds of entities: callable entities, other program units, and composite types.]
- The visible part of a view of a callable entity is its profile.
- The visible part of a composite type other than a task or protected type consists of the declarations of all components declared [(explicitly or implicitly)] within the
- The visible part of a generic unit includes the
generic_formal_part. For a generic package, it also includes the first list of
basic_declarative_items of the
package_specification. For a generic subprogram, it also includes the profile.
- [The visible part of a package, task unit, or protected unit consists of declarations in the program unit's declaration other than those following the reserved word private, if any; see 7.1 and 12.7 for packages, 9.1 for task units, and 9.4 for protected units.]
The scope of a declaration always contains the immediate scope of the declaration. In addition, for a given declaration that occurs immediately within the visible part of an outer declaration, or is a public child of an outer declaration, the scope of the given declaration extends to the end of the scope of the outer declaration, except that the scope of a
library_item includes only its semantic dependents.
with_clausementioning P.Q is necessary to be able to refer to X.Q, even if P.Q is visible at the place where X is declared.
The scope of an
attribute_definition_clause is identical to the scope of a declaration that would occur at the point of the
attribute_definition_clause. The scope of an
aspect_specification is identical to the scope of the associated declaration.
The immediate scope of a declaration is also the immediate scope of the entity or view declared by the declaration. Similarly, the scope of a declaration is also the scope of the entity or view declared by the declaration.
- If the declaration is that of a library unit, then the immediate scope includes the declarative region of the declaration itself, but not other places, unless they are within the scope of a
with_clausethat mentions the library unit. 11.c
- It is necessary to attach the semantics of
with_clauses to [immediate] scopes (as opposed to visibility), in order for various rules to work properly. A library unit should hide a homographic implicit declaration that appears in its parent, but only within the scope of a
with_clausethat mentions the library unit. Otherwise, we would violate the "legality determinable via semantic dependences" rule of 10, “Program Structure and Compilation Issues”. The declaration of a library unit should be allowed to be a homograph of an explicit declaration in its parent's body, so long as that body does not mention the library unit in a
- This means that one cannot denote the declaration of the library unit, but one might still be able to denote the library unit via another view.
with_clausedoes not make the declaration of a library unit visible; the lack of a
with_clauseprevents it from being visible. Even if a library unit is mentioned in a
with_clause, its declaration can still be hidden. 11.f
- The completion of the declaration of a library unit (assuming that's also a declaration) is not visible, neither directly nor by selection, outside that completion.
- The immediate scope of a declaration immediately within the body of a library unit does not include any child of that library unit.
- This is needed to prevent children from looking inside their parent's body. The children are in the declarative region of the parent, and they might be after the parent's body. Therefore, the scope of a declaration that occurs immediately within the body might include some children.
The immediate scope of a pragma that is not used as a configuration pragma is defined to be the region extending from immediately after the pragma to the end of the declarative region immediately enclosing the pragma.
parameter_specifications are in the visible part of a
subprogram_declarationso that they can be used in named-notation calls appearing outside the called subprogram. For another example, declarations of the visible part of a package can be denoted by expanded names appearing outside the package, and can be made directly visible by a
Extensions to Ada 83
Wording Changes from Ada 83
with_clauses was described in terms of visibility. It is now described in terms of [immediate] scope.
package Q is I : Integer := 0; end Q; 13.hpackage R is package X renames Standard; X.Q.I := 17; -- Illegal! end R;