OSDN Git Service

* 41intnam.ads, 42intnam.ads, 4aintnam.ads, 4cintnam.ads,
[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 --                            $Revision$
10 --                                                                          --
11 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
12 --                                                                          --
13 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
14 -- terms of the  GNU General Public License as published  by the Free Soft- --
15 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
16 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
17 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
18 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
19 -- for  more details.  You should have  received  a copy of the GNU General --
20 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
21 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
22 -- MA 02111-1307, USA.                                                      --
23 --                                                                          --
24 -- GNAT was originally developed  by the GNAT team at  New York University. --
25 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 --                                                                          --
27 ------------------------------------------------------------------------------
28
29 with Atree;    use Atree;
30 with Einfo;    use Einfo;
31 with Exp_Dbug; use Exp_Dbug;
32 with Exp_Util; use Exp_Util;
33 with Nlists;   use Nlists;
34 with Sem;      use Sem;
35 with Sem_Ch8;  use Sem_Ch8;
36 with Sinfo;    use Sinfo;
37 with Stand;    use Stand;
38
39 package body Exp_Ch8 is
40
41    ---------------------------------------------
42    -- Expand_N_Exception_Renaming_Declaration --
43    ---------------------------------------------
44
45    procedure Expand_N_Exception_Renaming_Declaration (N : Node_Id) is
46       Decl : constant Node_Id := Debug_Renaming_Declaration (N);
47
48    begin
49       if Present (Decl) then
50          Insert_Action (N, Decl);
51       end if;
52    end Expand_N_Exception_Renaming_Declaration;
53
54    ------------------------------------------
55    -- Expand_N_Object_Renaming_Declaration --
56    ------------------------------------------
57
58    --  Most object renaming cases can be done by just capturing the address
59    --  of the renamed object. The cases in which this is not true are when
60    --  this address is not computable, since it involves extraction of a
61    --  packed array element, or of a record component to which a component
62    --  clause applies (that can specify an arbitrary bit boundary), or where
63    --  the enclosing record itself has a non-standard representation.
64
65    --  In these two cases, we pre-evaluate the renaming expression, by
66    --  extracting and freezing the values of any subscripts, and then we
67    --  set the flag Is_Renaming_Of_Object which means that any reference
68    --  to the object will be handled by macro substitution in the front
69    --  end, and the back end will know to ignore the renaming declaration.
70
71    --  An additional odd case that requires processing by expansion is
72    --  the renaming of a discriminant of a mutable record type. The object
73    --  is a constant because it renames something that cannot be assigned to,
74    --  but in fact the underlying value can change and must be reevaluated
75    --  at each reference. Gigi does have a notion of a "constant view" of
76    --  an object, and therefore the front-end must perform the expansion.
77    --  For simplicity, and to bypass some obscure code-generation problem,
78    --  we use macro substitution for all renamed discriminants, whether the
79    --  enclosing type is constrained or not.
80
81    --  The other special processing required is for the case of renaming
82    --  of an object of a class wide type, where it is necessary to build
83    --  the appropriate subtype for the renamed object.
84    --  More comments needed for this para ???
85
86    procedure Expand_N_Object_Renaming_Declaration (N : Node_Id) is
87       Nam  : Node_Id := Name (N);
88       T    : Entity_Id;
89       Decl : Node_Id;
90
91       procedure Evaluate_Name (Fname : Node_Id);
92       --  A recursive procedure used to freeze a name in the sense described
93       --  above, i.e. any variable references or function calls are removed.
94       --  Of course the outer level variable reference must not be removed.
95       --  For example in A(J,F(K)), A is left as is, but J and F(K) are
96       --  evaluated and removed.
97
98       function Evaluation_Required (Nam : Node_Id) return Boolean;
99       --  Determines whether it is necessary to do static name evaluation
100       --  for renaming of Nam. It is considered necessary if evaluating the
101       --  name involves indexing a packed array, or extracting a component
102       --  of a record to which a component clause applies. Note that we are
103       --  only interested in these operations if they occur as part of the
104       --  name itself, subscripts are just values that are computed as part
105       --  of the evaluation, so their form is unimportant.
106
107       -------------------
108       -- Evaluate_Name --
109       -------------------
110
111       procedure Evaluate_Name (Fname : Node_Id) is
112          K : constant Node_Kind := Nkind (Fname);
113          E : Node_Id;
114
115       begin
116          --  For an explicit dereference, we simply force the evaluation
117          --  of the name expression. The dereference provides a value that
118          --  is the address for the renamed object, and it is precisely
119          --  this value that we want to preserve.
120
121          if K = N_Explicit_Dereference then
122             Force_Evaluation (Prefix (Fname));
123
124          --  For a selected component, we simply evaluate the prefix
125
126          elsif K = N_Selected_Component then
127             Evaluate_Name (Prefix (Fname));
128
129          --  For an indexed component, or an attribute reference, we evaluate
130          --  the prefix, which is itself a name, recursively, and then force
131          --  the evaluation of all the subscripts (or attribute expressions).
132
133          elsif K = N_Indexed_Component
134            or else K = N_Attribute_Reference
135          then
136             Evaluate_Name (Prefix (Fname));
137
138             E := First (Expressions (Fname));
139             while Present (E) loop
140                Force_Evaluation (E);
141
142                if Original_Node (E) /= E then
143                   Set_Do_Range_Check (E, Do_Range_Check (Original_Node (E)));
144                end if;
145
146                Next (E);
147             end loop;
148
149          --  For a slice, we evaluate the prefix, as for the indexed component
150          --  case and then, if there is a range present, either directly or
151          --  as the constraint of a discrete subtype indication, we evaluate
152          --  the two bounds of this range.
153
154          elsif K = N_Slice then
155             Evaluate_Name (Prefix (Fname));
156
157             declare
158                DR     : constant Node_Id := Discrete_Range (Fname);
159                Constr : Node_Id;
160                Rexpr  : Node_Id;
161
162             begin
163                if Nkind (DR) = N_Range then
164                   Force_Evaluation (Low_Bound (DR));
165                   Force_Evaluation (High_Bound (DR));
166
167                elsif Nkind (DR) = N_Subtype_Indication then
168                   Constr := Constraint (DR);
169
170                   if Nkind (Constr) = N_Range_Constraint then
171                      Rexpr := Range_Expression (Constr);
172
173                      Force_Evaluation (Low_Bound (Rexpr));
174                      Force_Evaluation (High_Bound (Rexpr));
175                   end if;
176                end if;
177             end;
178
179          --  For a type conversion, the expression of the conversion must be
180          --  the name of an object, and we simply need to evaluate this name.
181
182          elsif K = N_Type_Conversion then
183             Evaluate_Name (Expression (Fname));
184
185          --  For a function call, we evaluate the call.
186
187          elsif K = N_Function_Call then
188             Force_Evaluation (Fname);
189
190          --  The remaining cases are direct name, operator symbol and
191          --  character literal. In all these cases, we do nothing, since
192          --  we want to reevaluate each time the renamed object is used.
193
194          else
195             return;
196          end if;
197       end Evaluate_Name;
198
199       -------------------------
200       -- Evaluation_Required --
201       -------------------------
202
203       function Evaluation_Required (Nam : Node_Id) return Boolean is
204       begin
205          if Nkind (Nam) = N_Indexed_Component
206            or else Nkind (Nam) = N_Slice
207          then
208             if Is_Packed (Etype (Prefix (Nam))) then
209                return True;
210             else
211                return Evaluation_Required (Prefix (Nam));
212             end if;
213
214          elsif Nkind (Nam) = N_Selected_Component then
215             declare
216                Rec_Type : Entity_Id := Etype (Prefix (Nam));
217
218             begin
219                if Present (Component_Clause (Entity (Selector_Name (Nam))))
220                  or else Has_Non_Standard_Rep (Rec_Type)
221                then
222                   return True;
223
224                elsif Ekind (Entity (Selector_Name (Nam))) = E_Discriminant
225                  and then Is_Record_Type (Rec_Type)
226                  and then not Is_Concurrent_Record_Type (Rec_Type)
227                then
228                   return True;
229
230                else
231                   return Evaluation_Required (Prefix (Nam));
232                end if;
233             end;
234
235          else
236             return False;
237          end if;
238       end Evaluation_Required;
239
240    --  Start of processing for Expand_N_Object_Renaming_Declaration
241
242    begin
243       --  Perform name evaluation if required
244
245       if Evaluation_Required (Nam) then
246          Evaluate_Name (Nam);
247          Set_Is_Renaming_Of_Object (Defining_Identifier (N));
248       end if;
249
250       --  Deal with construction of subtype in class-wide case
251
252       T := Etype (Defining_Identifier (N));
253
254       if Is_Class_Wide_Type (T) then
255          Expand_Subtype_From_Expr (N, T, Subtype_Mark (N), Name (N));
256          Find_Type (Subtype_Mark (N));
257          Set_Etype (Defining_Identifier (N), Entity (Subtype_Mark (N)));
258       end if;
259
260       --  Create renaming entry for debug information
261
262       Decl := Debug_Renaming_Declaration (N);
263
264       if Present (Decl) then
265          Insert_Action (N, Decl);
266       end if;
267    end Expand_N_Object_Renaming_Declaration;
268
269    -------------------------------------------
270    -- Expand_N_Package_Renaming_Declaration --
271    -------------------------------------------
272
273    procedure Expand_N_Package_Renaming_Declaration (N : Node_Id) is
274       Decl : constant Node_Id := Debug_Renaming_Declaration (N);
275
276    begin
277       if Present (Decl) then
278
279          --  If we are in a compilation unit, then this is an outer
280          --  level declaration, and must have a scope of Standard
281
282          if Nkind (Parent (N)) = N_Compilation_Unit then
283             declare
284                Aux : constant Node_Id := Aux_Decls_Node (Parent (N));
285
286             begin
287                New_Scope (Standard_Standard);
288
289                if No (Actions (Aux)) then
290                   Set_Actions (Aux, New_List (Decl));
291                else
292                   Append (Decl, Actions (Aux));
293                end if;
294
295                Analyze (Decl);
296                Pop_Scope;
297             end;
298
299          --  Otherwise, just insert after the package declaration
300
301          else
302             Insert_Action (N, Decl);
303          end if;
304       end if;
305    end Expand_N_Package_Renaming_Declaration;
306
307 end Exp_Ch8;