Contents Index Previous Next
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/1
{
8652/0017}
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.
In addition to the places where Legality Rules normally apply, these
rules apply also in the private part of an instance of a generic unit. These
rules also apply for a renaming that appears in the body of a generic unit,
with the additional requirement that even if the nominal subtype of the variable
is indefinite, its type shall not be a descendant of an untagged generic formal
derived type.
5.a
Reason: This prevents renaming
of subcomponents that might disappear, which might leave dangling references.
Similar restrictions exist for the Access attribute.
5.a.1/1
{8652/0017}
The "recheck on instantiation" and "assume-the-worst in the
body" restrictions on generics are necessary to avoid renaming of components
which could disappear even when the nominal subtype would prevent the problem:
5.a.2/1
type T1 (D1 : Boolean) is
record
case D1 is
when False =>
C1 : Integer;
when True =>
null;
end case;
end record;
5.a.3/1
generic
type F is new T1;
X : in out F;
package G is
C1_Ren : Integer renames X.C1;
end G;
5.a.4/1
type T2 (D2 : Boolean := True) is new T1 (D1 => D2);
Y : T2;
package I is new G (T2, Y);
Y := (D1 => True); -- Oops! What happened to I.C1_Ren?
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 95; therefore
we removed it. It is incorrect in the case of an object with an indefinite
unconstrained nominal subtype.
Contents Index Previous Next Legal