OSDN Git Service

2007-10-15 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / exp_ch8.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              E X P _ C H 8                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
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 3,  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 COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Einfo;    use Einfo;
28 with Exp_Ch6;  use Exp_Ch6;
29 with Exp_Dbug; use Exp_Dbug;
30 with Exp_Util; use Exp_Util;
31 with Freeze;   use Freeze;
32 with Nlists;   use Nlists;
33 with Opt;      use Opt;
34 with Sem;      use Sem;
35 with Sem_Ch8;  use Sem_Ch8;
36 with Sinfo;    use Sinfo;
37 with Stand;    use Stand;
38 with Targparm; use Targparm;
39
40 package body Exp_Ch8 is
41
42    ---------------------------------------------
43    -- Expand_N_Exception_Renaming_Declaration --
44    ---------------------------------------------
45
46    procedure Expand_N_Exception_Renaming_Declaration (N : Node_Id) is
47       Decl : constant Node_Id := Debug_Renaming_Declaration (N);
48
49    begin
50       if Present (Decl) then
51          Insert_Action (N, Decl);
52       end if;
53    end Expand_N_Exception_Renaming_Declaration;
54
55    ------------------------------------------
56    -- Expand_N_Object_Renaming_Declaration --
57    ------------------------------------------
58
59    --  Most object renaming cases can be done by just capturing the address
60    --  of the renamed object. The cases in which this is not true are when
61    --  this address is not computable, since it involves extraction of a
62    --  packed array element, or of a record component to which a component
63    --  clause applies (that can specify an arbitrary bit boundary), or where
64    --  the enclosing record itself has a non-standard representation.
65
66    --  In these two cases, we pre-evaluate the renaming expression, by
67    --  extracting and freezing the values of any subscripts, and then we
68    --  set the flag Is_Renaming_Of_Object which means that any reference
69    --  to the object will be handled by macro substitution in the front
70    --  end, and the back end will know to ignore the renaming declaration.
71
72    --  An additional odd case that requires processing by expansion is
73    --  the renaming of a discriminant of a mutable record type. The object
74    --  is a constant because it renames something that cannot be assigned to,
75    --  but in fact the underlying value can change and must be reevaluated
76    --  at each reference. Gigi does have a notion of a "constant view" of
77    --  an object, and therefore the front-end must perform the expansion.
78    --  For simplicity, and to bypass some obscure code-generation problem,
79    --  we use macro substitution for all renamed discriminants, whether the
80    --  enclosing type is constrained or not.
81
82    --  The other special processing required is for the case of renaming
83    --  of an object of a class wide type, where it is necessary to build
84    --  the appropriate subtype for the renamed object.
85    --  More comments needed for this para ???
86
87    procedure Expand_N_Object_Renaming_Declaration (N : Node_Id) is
88       Nam  : constant Node_Id := Name (N);
89       T    : Entity_Id;
90       Decl : Node_Id;
91
92       procedure Evaluate_Name (Fname : Node_Id);
93       --  A recursive procedure used to freeze a name in the sense described
94       --  above, i.e. any variable references or function calls are removed.
95       --  Of course the outer level variable reference must not be removed.
96       --  For example in A(J,F(K)), A is left as is, but J and F(K) are
97       --  evaluated and removed.
98
99       function Evaluation_Required (Nam : Node_Id) return Boolean;
100       --  Determines whether it is necessary to do static name evaluation
101       --  for renaming of Nam. It is considered necessary if evaluating the
102       --  name involves indexing a packed array, or extracting a component
103       --  of a record to which a component clause applies. Note that we are
104       --  only interested in these operations if they occur as part of the
105       --  name itself, subscripts are just values that are computed as part
106       --  of the evaluation, so their form is unimportant.
107
108       -------------------
109       -- Evaluate_Name --
110       -------------------
111
112       procedure Evaluate_Name (Fname : Node_Id) is
113          K : constant Node_Kind := Nkind (Fname);
114          E : Node_Id;
115
116       begin
117          --  For an explicit dereference, we simply force the evaluation
118          --  of the name expression. The dereference provides a value that
119          --  is the address for the renamed object, and it is precisely
120          --  this value that we want to preserve.
121
122          if K = N_Explicit_Dereference then
123             Force_Evaluation (Prefix (Fname));
124
125          --  For a selected component, we simply evaluate the prefix
126
127          elsif K = N_Selected_Component then
128             Evaluate_Name (Prefix (Fname));
129
130          --  For an indexed component, or an attribute reference, we evaluate
131          --  the prefix, which is itself a name, recursively, and then force
132          --  the evaluation of all the subscripts (or attribute expressions).
133
134          elsif K = N_Indexed_Component
135            or else K = N_Attribute_Reference
136          then
137             Evaluate_Name (Prefix (Fname));
138
139             E := First (Expressions (Fname));
140             while Present (E) loop
141                Force_Evaluation (E);
142
143                if Original_Node (E) /= E then
144                   Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
145                end if;
146
147                Next (E);
148             end loop;
149
150          --  For a slice, we evaluate the prefix, as for the indexed component
151          --  case and then, if there is a range present, either directly or
152          --  as the constraint of a discrete subtype indication, we evaluate
153          --  the two bounds of this range.
154
155          elsif K = N_Slice then
156             Evaluate_Name (Prefix (Fname));
157
158             declare
159                DR     : constant Node_Id := Discrete_Range (Fname);
160                Constr : Node_Id;
161                Rexpr  : Node_Id;
162
163             begin
164                if Nkind (DR) = N_Range then
165                   Force_Evaluation (Low_Bound (DR));
166                   Force_Evaluation (High_Bound (DR));
167
168                elsif Nkind (DR) = N_Subtype_Indication then
169                   Constr := Constraint (DR);
170
171                   if Nkind (Constr) = N_Range_Constraint then
172                      Rexpr := Range_Expression (Constr);
173
174                      Force_Evaluation (Low_Bound (Rexpr));
175                      Force_Evaluation (High_Bound (Rexpr));
176                   end if;
177                end if;
178             end;
179
180          --  For a type conversion, the expression of the conversion must be
181          --  the name of an object, and we simply need to evaluate this name.
182
183          elsif K = N_Type_Conversion then
184             Evaluate_Name (Expression (Fname));
185
186          --  For a function call, we evaluate the call
187
188          elsif K = N_Function_Call then
189             Force_Evaluation (Fname);
190
191          --  The remaining cases are direct name, operator symbol and
192          --  character literal. In all these cases, we do nothing, since
193          --  we want to reevaluate each time the renamed object is used.
194
195          else
196             return;
197          end if;
198       end Evaluate_Name;
199
200       -------------------------
201       -- Evaluation_Required --
202       -------------------------
203
204       function Evaluation_Required (Nam : Node_Id) return Boolean is
205       begin
206          if Nkind (Nam) = N_Indexed_Component
207            or else Nkind (Nam) = N_Slice
208          then
209             if Is_Packed (Etype (Prefix (Nam))) then
210                return True;
211             else
212                return Evaluation_Required (Prefix (Nam));
213             end if;
214
215          elsif Nkind (Nam) = N_Selected_Component then
216             declare
217                Rec_Type : constant Entity_Id := Etype (Prefix (Nam));
218
219             begin
220                if Present (Component_Clause (Entity (Selector_Name (Nam))))
221                  or else Has_Non_Standard_Rep (Rec_Type)
222                then
223                   return True;
224
225                elsif Ekind (Entity (Selector_Name (Nam))) = E_Discriminant
226                  and then Is_Record_Type (Rec_Type)
227                  and then not Is_Concurrent_Record_Type (Rec_Type)
228                then
229                   return True;
230
231                else
232                   return Evaluation_Required (Prefix (Nam));
233                end if;
234             end;
235
236          else
237             return False;
238          end if;
239       end Evaluation_Required;
240
241    --  Start of processing for Expand_N_Object_Renaming_Declaration
242
243    begin
244       --  Perform name evaluation if required
245
246       if Evaluation_Required (Nam) then
247          Evaluate_Name (Nam);
248          Set_Is_Renaming_Of_Object (Defining_Identifier (N));
249       end if;
250
251       --  Deal with construction of subtype in class-wide case
252
253       T := Etype (Defining_Identifier (N));
254
255       if Is_Class_Wide_Type (T) then
256          Expand_Subtype_From_Expr (N, T, Subtype_Mark (N), Name (N));
257          Find_Type (Subtype_Mark (N));
258          Set_Etype (Defining_Identifier (N), Entity (Subtype_Mark (N)));
259
260          --  Freeze the class-wide subtype here to ensure that the subtype
261          --  and equivalent type are frozen before the renaming. This is
262          --  required for targets where Frontend_Layout_On_Target is true.
263          --  For targets where Gigi is used, class-wide subtype should not
264          --  be frozen (in that case the subtype is marked as already frozen
265          --  when it's created).
266
267          if Frontend_Layout_On_Target then
268             Freeze_Before (N, Entity (Subtype_Mark (N)));
269          end if;
270       end if;
271
272       --  Ada 2005 (AI-318-02): If the renamed object is a call to a build-in-
273       --  place function, then a temporary return object needs to be created
274       --  and access to it must be passed to the function. Currently we limit
275       --  such functions to those with inherently limited result subtypes, but
276       --  eventually we plan to expand the functions that are treated as
277       --  build-in-place to include other composite result types.
278
279       if Ada_Version >= Ada_05
280         and then Is_Build_In_Place_Function_Call (Nam)
281       then
282          Make_Build_In_Place_Call_In_Anonymous_Context (Nam);
283       end if;
284
285       --  Create renaming entry for debug information
286
287       Decl := Debug_Renaming_Declaration (N);
288
289       if Present (Decl) then
290          Insert_Action (N, Decl);
291       end if;
292    end Expand_N_Object_Renaming_Declaration;
293
294    -------------------------------------------
295    -- Expand_N_Package_Renaming_Declaration --
296    -------------------------------------------
297
298    procedure Expand_N_Package_Renaming_Declaration (N : Node_Id) is
299       Decl : constant Node_Id := Debug_Renaming_Declaration (N);
300
301    begin
302       if Present (Decl) then
303
304          --  If we are in a compilation unit, then this is an outer
305          --  level declaration, and must have a scope of Standard
306
307          if Nkind (Parent (N)) = N_Compilation_Unit then
308             declare
309                Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
310
311             begin
312                Push_Scope (Standard_Standard);
313
314                if No (Actions (Aux)) then
315                   Set_Actions (Aux, New_List (Decl));
316                else
317                   Append (Decl, Actions (Aux));
318                end if;
319
320                Analyze (Decl);
321
322                --  Enter the debug variable in the qualification list, which
323                --  must be done at this point because auxiliary declarations
324                --  occur at the library level and aren't associated with a
325                --  normal scope.
326
327                Qualify_Entity_Names (Decl);
328
329                Pop_Scope;
330             end;
331
332          --  Otherwise, just insert after the package declaration
333
334          else
335             Insert_Action (N, Decl);
336          end if;
337       end if;
338    end Expand_N_Package_Renaming_Declaration;
339
340 end Exp_Ch8;