1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree
; use Atree
;
28 with Einfo
; use Einfo
;
29 with Exp_Dbug
; use Exp_Dbug
;
30 with Exp_Util
; use Exp_Util
;
31 with Nlists
; use Nlists
;
33 with Sem_Ch8
; use Sem_Ch8
;
34 with Sinfo
; use Sinfo
;
35 with Stand
; use Stand
;
37 package body Exp_Ch8
is
39 ---------------------------------------------
40 -- Expand_N_Exception_Renaming_Declaration --
41 ---------------------------------------------
43 procedure Expand_N_Exception_Renaming_Declaration
(N
: Node_Id
) is
44 Decl
: constant Node_Id
:= Debug_Renaming_Declaration
(N
);
47 if Present
(Decl
) then
48 Insert_Action
(N
, Decl
);
50 end Expand_N_Exception_Renaming_Declaration
;
52 ------------------------------------------
53 -- Expand_N_Object_Renaming_Declaration --
54 ------------------------------------------
56 -- Most object renaming cases can be done by just capturing the address
57 -- of the renamed object. The cases in which this is not true are when
58 -- this address is not computable, since it involves extraction of a
59 -- packed array element, or of a record component to which a component
60 -- clause applies (that can specify an arbitrary bit boundary), or where
61 -- the enclosing record itself has a non-standard representation.
63 -- In these two cases, we pre-evaluate the renaming expression, by
64 -- extracting and freezing the values of any subscripts, and then we
65 -- set the flag Is_Renaming_Of_Object which means that any reference
66 -- to the object will be handled by macro substitution in the front
67 -- end, and the back end will know to ignore the renaming declaration.
69 -- An additional odd case that requires processing by expansion is
70 -- the renaming of a discriminant of a mutable record type. The object
71 -- is a constant because it renames something that cannot be assigned to,
72 -- but in fact the underlying value can change and must be reevaluated
73 -- at each reference. Gigi does have a notion of a "constant view" of
74 -- an object, and therefore the front-end must perform the expansion.
75 -- For simplicity, and to bypass some obscure code-generation problem,
76 -- we use macro substitution for all renamed discriminants, whether the
77 -- enclosing type is constrained or not.
79 -- The other special processing required is for the case of renaming
80 -- of an object of a class wide type, where it is necessary to build
81 -- the appropriate subtype for the renamed object.
82 -- More comments needed for this para ???
84 procedure Expand_N_Object_Renaming_Declaration
(N
: Node_Id
) is
85 Nam
: Node_Id
:= Name
(N
);
89 procedure Evaluate_Name
(Fname
: Node_Id
);
90 -- A recursive procedure used to freeze a name in the sense described
91 -- above, i.e. any variable references or function calls are removed.
92 -- Of course the outer level variable reference must not be removed.
93 -- For example in A(J,F(K)), A is left as is, but J and F(K) are
94 -- evaluated and removed.
96 function Evaluation_Required
(Nam
: Node_Id
) return Boolean;
97 -- Determines whether it is necessary to do static name evaluation
98 -- for renaming of Nam. It is considered necessary if evaluating the
99 -- name involves indexing a packed array, or extracting a component
100 -- of a record to which a component clause applies. Note that we are
101 -- only interested in these operations if they occur as part of the
102 -- name itself, subscripts are just values that are computed as part
103 -- of the evaluation, so their form is unimportant.
109 procedure Evaluate_Name
(Fname
: Node_Id
) is
110 K
: constant Node_Kind
:= Nkind
(Fname
);
114 -- For an explicit dereference, we simply force the evaluation
115 -- of the name expression. The dereference provides a value that
116 -- is the address for the renamed object, and it is precisely
117 -- this value that we want to preserve.
119 if K
= N_Explicit_Dereference
then
120 Force_Evaluation
(Prefix
(Fname
));
122 -- For a selected component, we simply evaluate the prefix
124 elsif K
= N_Selected_Component
then
125 Evaluate_Name
(Prefix
(Fname
));
127 -- For an indexed component, or an attribute reference, we evaluate
128 -- the prefix, which is itself a name, recursively, and then force
129 -- the evaluation of all the subscripts (or attribute expressions).
131 elsif K
= N_Indexed_Component
132 or else K
= N_Attribute_Reference
134 Evaluate_Name
(Prefix
(Fname
));
136 E
:= First
(Expressions
(Fname
));
137 while Present
(E
) loop
138 Force_Evaluation
(E
);
140 if Original_Node
(E
) /= E
then
141 Set_Do_Range_Check
(E
, Do_Range_Check
(Original_Node
(E
)));
147 -- For a slice, we evaluate the prefix, as for the indexed component
148 -- case and then, if there is a range present, either directly or
149 -- as the constraint of a discrete subtype indication, we evaluate
150 -- the two bounds of this range.
152 elsif K
= N_Slice
then
153 Evaluate_Name
(Prefix
(Fname
));
156 DR
: constant Node_Id
:= Discrete_Range
(Fname
);
161 if Nkind
(DR
) = N_Range
then
162 Force_Evaluation
(Low_Bound
(DR
));
163 Force_Evaluation
(High_Bound
(DR
));
165 elsif Nkind
(DR
) = N_Subtype_Indication
then
166 Constr
:= Constraint
(DR
);
168 if Nkind
(Constr
) = N_Range_Constraint
then
169 Rexpr
:= Range_Expression
(Constr
);
171 Force_Evaluation
(Low_Bound
(Rexpr
));
172 Force_Evaluation
(High_Bound
(Rexpr
));
177 -- For a type conversion, the expression of the conversion must be
178 -- the name of an object, and we simply need to evaluate this name.
180 elsif K
= N_Type_Conversion
then
181 Evaluate_Name
(Expression
(Fname
));
183 -- For a function call, we evaluate the call.
185 elsif K
= N_Function_Call
then
186 Force_Evaluation
(Fname
);
188 -- The remaining cases are direct name, operator symbol and
189 -- character literal. In all these cases, we do nothing, since
190 -- we want to reevaluate each time the renamed object is used.
197 -------------------------
198 -- Evaluation_Required --
199 -------------------------
201 function Evaluation_Required
(Nam
: Node_Id
) return Boolean is
203 if Nkind
(Nam
) = N_Indexed_Component
204 or else Nkind
(Nam
) = N_Slice
206 if Is_Packed
(Etype
(Prefix
(Nam
))) then
209 return Evaluation_Required
(Prefix
(Nam
));
212 elsif Nkind
(Nam
) = N_Selected_Component
then
214 Rec_Type
: Entity_Id
:= Etype
(Prefix
(Nam
));
217 if Present
(Component_Clause
(Entity
(Selector_Name
(Nam
))))
218 or else Has_Non_Standard_Rep
(Rec_Type
)
222 elsif Ekind
(Entity
(Selector_Name
(Nam
))) = E_Discriminant
223 and then Is_Record_Type
(Rec_Type
)
224 and then not Is_Concurrent_Record_Type
(Rec_Type
)
229 return Evaluation_Required
(Prefix
(Nam
));
236 end Evaluation_Required
;
238 -- Start of processing for Expand_N_Object_Renaming_Declaration
241 -- Perform name evaluation if required
243 if Evaluation_Required
(Nam
) then
245 Set_Is_Renaming_Of_Object
(Defining_Identifier
(N
));
248 -- Deal with construction of subtype in class-wide case
250 T
:= Etype
(Defining_Identifier
(N
));
252 if Is_Class_Wide_Type
(T
) then
253 Expand_Subtype_From_Expr
(N
, T
, Subtype_Mark
(N
), Name
(N
));
254 Find_Type
(Subtype_Mark
(N
));
255 Set_Etype
(Defining_Identifier
(N
), Entity
(Subtype_Mark
(N
)));
258 -- Create renaming entry for debug information
260 Decl
:= Debug_Renaming_Declaration
(N
);
262 if Present
(Decl
) then
263 Insert_Action
(N
, Decl
);
265 end Expand_N_Object_Renaming_Declaration
;
267 -------------------------------------------
268 -- Expand_N_Package_Renaming_Declaration --
269 -------------------------------------------
271 procedure Expand_N_Package_Renaming_Declaration
(N
: Node_Id
) is
272 Decl
: constant Node_Id
:= Debug_Renaming_Declaration
(N
);
275 if Present
(Decl
) then
277 -- If we are in a compilation unit, then this is an outer
278 -- level declaration, and must have a scope of Standard
280 if Nkind
(Parent
(N
)) = N_Compilation_Unit
then
282 Aux
: constant Node_Id
:= Aux_Decls_Node
(Parent
(N
));
285 New_Scope
(Standard_Standard
);
287 if No
(Actions
(Aux
)) then
288 Set_Actions
(Aux
, New_List
(Decl
));
290 Append
(Decl
, Actions
(Aux
));
297 -- Otherwise, just insert after the package declaration
300 Insert_Action
(N
, Decl
);
303 end Expand_N_Package_Renaming_Declaration
;