[Home] [Prev] [Next] [Index]

8.5 Renaming Declarations

8.5 Renaming Declarations

1
[A renaming_declaration declares another name for an entity, such as an object, exception, package, subprogram, entry, or generic unit. Alternatively, a subprogram_renaming_declaration can be the completion of a previous subprogram_declaration.]

Syntax

2
renaming_declaration ::=
      object_renaming_declaration
    | exception_renaming_declaration
    | package_renaming_declaration
    | subprogram_renaming_declaration
    | generic_renaming_declaration

Dynamic Semantics

3
The elaboration of a renaming_declaration evaluates the name that follows the reserved word renames and thereby determines the view and entity denoted by this name (the renamed view and renamed entity). [A name that denotes the renaming_declaration denotes (a new view of) the renamed entity.]

NOTES

4 8
Renaming may be used to resolve name conflicts and to act as a shorthand.  Renaming with a different identifier or operator_symbol does not hide the old name; the new name and the old name need not be visible at the same places.

5 9
A task or protected object that is declared by an explicit object_declaration can be renamed as an object.  However, a single task or protected object cannot be renamed since the corresponding type is anonymous (meaning it has no nameable subtypes). For similar reasons, an object of an anonymous array or access type cannot be renamed.

6 10
A subtype defined without any additional constraint can be used to achieve the effect of renaming another subtype (including a task or protected subtype) as in

7
subtype Mode is Ada.Text_IO.File_Mode;

Wording Changes From Ada 83

7.a
The second sentence of RM83-8.5(3), "At any point where a renaming declaration is visible, the identifier, or operator symbol of this declaration denotes the renamed entity." is incorrect. It doesn't say directly visible. Also, such an identifier might resolve to something else.

7.b
The verbiage about renamings being legal "only if exactly one...", which appears in RM83-8.5(4) (for objects) and RM83-8.5(7) (for subprograms) is removed, because it follows from the normal rules about overload resolution. For language lawyers, these facts are obvious; for programmers, they are irrelevant, since failing these tests is highly unlikely.

8.5.1 Object Renaming Declarations

1
[An object_renaming_declaration is used to rename an object.]

Syntax

2
object_renaming_declaration ::= defining_identifier : subtype_mark renames object_name;

Name Resolution Rules

3
The type of the object_name shall resolve to the type determined by the subtype_mark.

3.a
Reason: A previous version of Ada 9X used the usual "expected type" wording: "The expected type for the object_name is that determined by the subtype_mark." We changed it so that this would be illegal:

3.b
X: T;
Y: T'Class renames X; --Illegal!

3.c
When the above was legal, it was unclear whether Y was of type T or T'Class. Note that we still allow this:

3.d
Z: T'Class := ...;
W: T renames F(Z);

3.e
where F is a function with a controlling parameter and result. This is admittedly a bit odd.

3.f
Note that the matching rule for generic formal parameters of mode in out was changed to keep it consistent with the rule for renaming. That makes the rule different for in vs. in out.

Legality Rules

4
The renamed entity shall be an object.

5
The renamed entity shall not be a subcomponent that depends on discriminants of a variable whose nominal subtype is unconstrained, unless this subtype is indefinite, or the variable is aliased. A slice of an array shall not be renamed if this restriction disallows renaming of the array.

5.a
Reason: This prevents renaming of subcomponents that might disappear, which might leave dangling references. Similar restrictions exist for the Access attribute.

5.b
Implementation Note: Note that if an implementation chooses to deallocate-then-reallocate on assignment_statements assigning to unconstrained definite objects, then it cannot represent renamings and access values as simple addresses, because the above rule does not apply to all components of such an object.

5.c
Ramification: If it is a generic formal object, then the assume-the-best or assume-the-worst rules are applied as appropriate.

Static Semantics

6
An object_renaming_declaration declares a new view [of the renamed object] whose properties are identical to those of the renamed view. [Thus, the properties of the renamed object are not affected by the renaming_declaration. In particular, its value and whether or not it is a constant are unaffected; similarly, the constraints that apply to an object are not affected by renaming (any constraint implied by the subtype_mark of the object_renaming_declaration is ignored).]

6.a
Discussion:  Because the constraints are ignored, it is a good idea to use the nominal subtype of the renamed object when writing an object_renaming_declaration.

Examples

7
Example of renaming an object:

8
declare
   L : Person renames Leftmost_Person; --see 3.10.1
begin
   L.Age := L.Age + 1;
end;

Wording Changes From Ada 83

8.a
The phrase "subtype ... as defined in a corresponding object declaration, component declaration, or component subtype indication," from RM83-8.5(5), is incorrect in Ada 9X; therefore we removed it. It is incorrect in the case of an object with an indefinite unconstrained nominal subtype.

8.5.2 Exception Renaming Declarations

1
[An exception_renaming_declaration is used to rename an exception.]

Syntax

2
exception_renaming_declaration ::= defining_identifier : exception renames exception_name;

Legality Rules

3
The renamed entity shall be an exception.

Static Semantics

4
An exception_renaming_declaration declares a new view [of the renamed exception].

Examples

5
Example of renaming an exception:

6
EOF : exception renames Ada.IO_Exceptions.End_Error;-- see A.13

8.5.3 Package Renaming Declarations

1
[A package_renaming_declaration is used to rename a package.]

Syntax

2
package_renaming_declaration ::= package defining_program_unit_name renames package_name;

Legality Rules

3
The renamed entity shall be a package.

Static Semantics

4
A package_renaming_declaration declares a new view [of the renamed package].

Examples

5
Example of renaming a package:

6
package TM renames Table_Manager;

8.5.4 Subprogram Renaming Declarations

1
A subprogram_renaming_declaration can serve as the completion of a subprogram_declaration; such a renaming_declaration is called a renaming-as-body. A subprogram_renaming_declaration that is not a completion is called a renaming-as-declaration[, and is used to rename a subprogram (possibly an enumeration literal) or an entry].

1.a
Ramification: A renaming-as-body is a declaration, as defined in Section 3.

Syntax

2
subprogram_renaming_declaration ::= subprogram_specification renames callable_entity_name;

Name Resolution Rules

3
The expected profile for the callable_entity_name is the profile given in the subprogram_specification.

Legality Rules

4
The profile of a renaming-as-declaration shall be mode-conformant with that of the renamed callable entity.

5
The profile of a renaming-as-body shall be subtype-conformant with that of the renamed callable entity, and shall conform fully to that of the declaration it completes. If the renaming-as-body completes that declaration before the subprogram it declares is frozen, the subprogram it declares takes its convention from the renamed subprogram; otherwise the convention of the renamed subprogram shall not be Intrinsic.

5.a
Reason: The first part of the first sentence is to allow an implementation of a renaming-as-body as a single jump instruction to the target subprogram. Among other things, this prevents a subprogram from being completed with a renaming of an entry. (In most cases, the target of the jump can be filled in at link time. In some cases, such as a renaming of a name like "A(I).all", an indirect jump is needed. Note that the name is evaluated at renaming time, not at call time.)

5.b
The second part of the first sentence is the normal rule for completions of subprogram_declarations.

5.c
Ramification: An entry_declaration, unlike a subprogram_declaration, cannot be completed with a renaming_declaration. Nor can a generic_subprogram_declaration.

5.d
The syntax rules prevent a protected subprogram declaration from being completed by a renaming. This is fortunate, because it allows us to avoid worrying about whether the implicit protected object parameter of a protected operation is involved in the conformance rules.

6
A name that denotes a formal parameter of the subprogram_specification is not allowed within the callable_entity_name.

6.a
Reason: This is to prevent things like this:

6.b
function F(X : Integer) return Integer renames Table(X).all;

6.c
A similar rule in 6.1 forbids things like this:

6.d
function F(X : Integer; Y : Integer := X) return Integer;

Static Semantics

7
A renaming-as-declaration declares a new view of the renamed entity. The profile of this new view takes its subtypes, parameter modes, and calling convention from the original profile of the callable entity, while taking the formal parameter names and default_expressions from the profile given in the subprogram_renaming_declaration. The new view is a function or procedure, never an entry.

7.a
To be honest: When renaming an entry as a procedure, the compile-time rules apply as if the new view is a procedure, but the run-time semantics of a call are that of an entry call.

7.b
Ramification: For example, it is illegal for the entry_call_statement of a timed_entry_call to call the new view. But what looks like a procedure call will do things like barrier waiting.

Dynamic Semantics

8
For a call on a renaming of a dispatching subprogram that is overridden, if the overriding occurred before the renaming, then the body executed is that of the overriding declaration, even if the overriding declaration is not visible at the place of the renaming; otherwise, the inherited or predefined subprogram is called.

8.a
Discussion:  Note that whether or not the renaming is itself primitive has nothing to do with the renamed subprogram.

8.b
Note that the above rule is only for tagged types.

8.c
Consider the following example:

8.d
package P is
    type T is tagged null record;
    function Predefined_Equal(X, Y : T) return Boolean renames "=";
private
    function "="(X, Y : T) return Boolean; --Override predefined "=".
end P;

8.e
with P; use P;
package Q is
    function User_Defined_Equal(X, Y : T) return Boolean renames P."=";
end Q;

8.f
A call on Predefined_Equal will execute the predefined equality operator of T, whereas a call on User_Defined_Equal will execute the body of the overriding declaration in the private part of P.

8.g
Thus a renaming allows one to squirrel away a copy of an inherited or predefined subprogram before later overriding it.

NOTES

9 11
A procedure can only be renamed as a procedure. A function whose defining_designator is either an identifier or an operator_symbol can be renamed with either an identifier or an operator_symbol; for renaming as an operator, the subprogram specification given in the renaming_declaration is subject to the rules given in 6.6 for operator declarations.  Enumeration literals can be renamed as functions; similarly, attribute_references that denote functions (such as references to Succ and Pred) can be renamed as functions.  An entry can only be renamed as a procedure; the new name is only allowed to appear in contexts that allow a procedure name.  An entry of a family can be renamed, but an entry family cannot be renamed as a whole.

10 12
The operators of the root numeric types cannot be renamed because the types in the profile are anonymous, so the corresponding specifications cannot be written; the same holds for certain attributes, such as Pos.

11 13
Calls with the new name of a renamed entry are procedure_call_statements and are not allowed at places where the syntax requires an entry_call_statement in conditional_ and timed_entry_calls, nor in an asynchronous_select; similarly, the Count attribute is not available for the new name.

12 14
The primitiveness of a renaming-as-declaration is determined by its profile, and by where it occurs, as for any declaration of (a view of) a subprogram; primitiveness is not determined by the renamed view. In order to perform a dispatching call, the subprogram name has to denote a primitive subprogram, not a non-primitive renaming of a primitive subprogram.

12.a
Reason: A subprogram_renaming_declaration could more properly be called renaming_as_subprogram_declaration, since you're renaming something as a subprogram, but you're not necessarily renaming a subprogram. But that's too much of a mouthful. Or, alternatively, we could call it a callable_entity_renaming_declaration, but that's even worse. Not only is it a mouthful, it emphasizes the entity being renamed, rather than the new view, which we think is a bad idea. We'll live with the oddity.

Examples

13
Examples of subprogram renaming declarations:

14
procedure My_Write(C : in Character) renames Pool(K).Write; --see 4.1.3

15
function Real_Plus(Left, Right : Real   ) return Real    renames "+";
function Int_Plus (Left, Right : Integer) return Integer renames "+";

16
function Rouge return Color renames Red;  --see 3.5.1
function Rot   return Color renames Red;
function Rosso return Color renames Rouge;

17
function Next(X : Color) return Color renames Color'Succ; --see 3.5.1

18
Example of a subprogram renaming declaration with new parameter names:

19
function "*" (X,Y : Vector) return Real renames Dot_Product; --see 6.1

20
Example of a subprogram renaming declaration with a new default expression:

21
function Minimum(L : Link := Head) return Cell renames Min_Cell; --see 6.1

8.5.5 Generic Renaming Declarations

1
[A generic_renaming_declaration is used to rename a generic unit.]

Syntax

2
generic_renaming_declaration ::=
    generic package       defining_program_unit_name renames generic_package_name;
  | generic procedure defining_program_unit_name renames generic_procedure_name;
  | generic function defining_program_unit_name renames generic_function_name;

Legality Rules

3
The renamed entity shall be a generic unit of the corresponding kind.

Static Semantics

4
A generic_renaming_declaration declares a new view [of the renamed generic unit].

NOTES

5 15
Although the properties of the new view are the same as those of the renamed view, the place where the generic_renaming_declaration occurs may affect the legality of subsequent renamings and instantiations that denote the generic_renaming_declaration, in particular if the renamed generic unit is a library unit (see 10.1.1).

Examples

6
Example of renaming a generic unit:

7
generic package Enum_IO renames Ada.Text_IO.Enumeration_IO;  -- see A.10.10

Extensions to Ada 83

7.a
Renaming of generic units is new to Ada 9X. It is particularly important for renaming child library units that are generic units.  For example, it might be used to rename Numerics.Generic_Elementary_Functions as simply Generic_Elementary_Functions, to match the name for the corresponding Ada-83-based package.

Wording Changes From Ada 83

7.b
The information in RM83-8.6, "The Package Standard," has been updated for the child unit feature, and moved to Annex A, except for the definition of "predefined type," which has been moved to 3.2.1.



[Home] [Prev] [Next] [Index]

documentation@rational.com
Copyright © 1993-1998, Rational Software Corporation.   All rights reserved.