OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ada / expander.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             E X P A N D E R                              --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                                                                          --
10 --          Copyright (C) 1992-2001 Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- GNAT was originally developed  by the GNAT team at  New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
25 --                                                                          --
26 ------------------------------------------------------------------------------
27
28 with Atree;     use Atree;
29 with Debug_A;   use Debug_A;
30 with Errout;    use Errout;
31 with Exp_Aggr;  use Exp_Aggr;
32 with Exp_Attr;  use Exp_Attr;
33 with Exp_Ch2;   use Exp_Ch2;
34 with Exp_Ch3;   use Exp_Ch3;
35 with Exp_Ch4;   use Exp_Ch4;
36 with Exp_Ch5;   use Exp_Ch5;
37 with Exp_Ch6;   use Exp_Ch6;
38 with Exp_Ch7;   use Exp_Ch7;
39 with Exp_Ch8;   use Exp_Ch8;
40 with Exp_Ch9;   use Exp_Ch9;
41 with Exp_Ch11;  use Exp_Ch11;
42 with Exp_Ch12;  use Exp_Ch12;
43 with Exp_Ch13;  use Exp_Ch13;
44 with Exp_Prag;  use Exp_Prag;
45 with Opt;       use Opt;
46 with Sem;       use Sem;
47 with Sem_Ch8;   use Sem_Ch8;
48 with Sem_Util;  use Sem_Util;
49 with Sinfo;     use Sinfo;
50 with Table;
51
52 package body Expander is
53
54    ----------------
55    -- Local Data --
56    ----------------
57
58    --  The following table is used to save values of the Expander_Active
59    --  flag when they are saved by Expander_Mode_Save_And_Set. We use an
60    --  extendible table (which is a bit of overkill) because it is easier
61    --  than figuring out a maximum value or bothering with range checks!
62
63    package Expander_Flags is new Table.Table (
64      Table_Component_Type => Boolean,
65      Table_Index_Type     => Int,
66      Table_Low_Bound      => 0,
67      Table_Initial        => 32,
68      Table_Increment      => 200,
69      Table_Name           => "Expander_Flags");
70
71    ------------
72    -- Expand --
73    ------------
74
75    procedure Expand (N : Node_Id) is
76    begin
77       --  If we were analyzing a default expression the Full_Analysis flag
78       --  must have be off. If we are in expansion mode then we must be
79       --  performing a full analysis. If we are analyzing a generic then
80       --  Expansion must be off.
81
82       pragma Assert
83         (not (Full_Analysis and then In_Default_Expression)
84          and then (Full_Analysis or else not Expander_Active)
85          and then not (Inside_A_Generic and then Expander_Active));
86
87       --  There are three reasons for the Expander_Active flag to be false.
88       --
89       --  The first is when are not generating code. In this mode the
90       --  Full_Analysis flag indicates whether we are performing a complete
91       --  analysis, in which case Full_Analysis = True or a pre-analysis in
92       --  which case Full_Analysis = False. See the spec of Sem for more
93       --  info on this.
94       --
95       --  The second reason for the Expander_Active flag to be False is that
96       --  we are performing a pre-analysis. During pre-analysis all
97       --  expansion activity is turned off to make sure nodes are
98       --  semantically decorated but no extra nodes are generated.  This is
99       --  for instance needed for the first pass of aggregate semantic
100       --  processing. Note that in this case the Full_Analysis flag is set
101       --  to False because the node will subsequently be re-analyzed with
102       --  expansion on (see the spec of sem).
103
104       --  Finally, expansion is turned off in a regular compilation if there
105       --  are serious errors. In that case there will be no further expansion,
106       --  but one cleanup action may be required: if a transient scope was
107       --  created (e.g. for a function that returns an unconstrained type)
108       --  the scope may still be on the stack, and must be removed explicitly,
109       --  given that the expansion actions that would normally process it will
110       --  not take place. This prevents cascaded errors due to stack mismatch.
111
112       if not Expander_Active then
113          Set_Analyzed (N, Full_Analysis);
114
115          if Serious_Errors_Detected > 0
116            and then Scope_Is_Transient
117          then
118             Scope_Stack.Table
119              (Scope_Stack.Last).Actions_To_Be_Wrapped_Before := No_List;
120             Scope_Stack.Table
121              (Scope_Stack.Last).Actions_To_Be_Wrapped_After  := No_List;
122
123             Pop_Scope;
124          end if;
125
126          return;
127
128       else
129          Debug_A_Entry ("expanding  ", N);
130
131          --  Processing depends on node kind. For full details on the expansion
132          --  activity required in each case, see bodies of corresponding
133          --  expand routines
134
135          case Nkind (N) is
136
137             when N_Abort_Statement =>
138                Expand_N_Abort_Statement (N);
139
140             when N_Accept_Statement =>
141                Expand_N_Accept_Statement (N);
142
143             when N_Aggregate =>
144                Expand_N_Aggregate (N);
145
146             when N_Allocator =>
147                Expand_N_Allocator (N);
148
149             when N_And_Then =>
150                Expand_N_And_Then (N);
151
152             when N_Assignment_Statement =>
153                Expand_N_Assignment_Statement (N);
154
155             when N_Asynchronous_Select =>
156                Expand_N_Asynchronous_Select (N);
157
158             when N_Attribute_Definition_Clause =>
159                Expand_N_Attribute_Definition_Clause (N);
160
161             when N_Attribute_Reference =>
162                Expand_N_Attribute_Reference (N);
163
164             when N_Block_Statement =>
165                Expand_N_Block_Statement (N);
166
167             when N_Case_Statement =>
168                Expand_N_Case_Statement (N);
169
170             when N_Conditional_Entry_Call =>
171                Expand_N_Conditional_Entry_Call (N);
172
173             when N_Conditional_Expression =>
174                Expand_N_Conditional_Expression (N);
175
176             when N_Delay_Relative_Statement =>
177                Expand_N_Delay_Relative_Statement (N);
178
179             when N_Delay_Until_Statement =>
180                Expand_N_Delay_Until_Statement (N);
181
182             when N_Entry_Body =>
183                Expand_N_Entry_Body (N);
184
185             when N_Entry_Call_Statement =>
186                Expand_N_Entry_Call_Statement (N);
187
188             when N_Entry_Declaration =>
189                Expand_N_Entry_Declaration (N);
190
191             when N_Exception_Declaration =>
192                Expand_N_Exception_Declaration (N);
193
194             when N_Exception_Renaming_Declaration =>
195                Expand_N_Exception_Renaming_Declaration (N);
196
197             when N_Exit_Statement =>
198                Expand_N_Exit_Statement (N);
199
200             when N_Expanded_Name =>
201                Expand_N_Expanded_Name (N);
202
203             when N_Explicit_Dereference =>
204                Expand_N_Explicit_Dereference (N);
205
206             when N_Extension_Aggregate =>
207                Expand_N_Extension_Aggregate (N);
208
209             when N_Freeze_Entity =>
210                Expand_N_Freeze_Entity (N);
211
212             when N_Full_Type_Declaration =>
213                Expand_N_Full_Type_Declaration (N);
214
215             when N_Function_Call =>
216                Expand_N_Function_Call (N);
217
218             when N_Generic_Instantiation =>
219                Expand_N_Generic_Instantiation (N);
220
221             when N_Goto_Statement =>
222                Expand_N_Goto_Statement (N);
223
224             when N_Handled_Sequence_Of_Statements =>
225                Expand_N_Handled_Sequence_Of_Statements (N);
226
227             when N_Identifier =>
228                Expand_N_Identifier (N);
229
230             when N_Indexed_Component =>
231                Expand_N_Indexed_Component (N);
232
233             when N_If_Statement =>
234                Expand_N_If_Statement (N);
235
236             when N_In =>
237                Expand_N_In (N);
238
239             when N_Loop_Statement =>
240                Expand_N_Loop_Statement (N);
241
242             when N_Not_In =>
243                Expand_N_Not_In (N);
244
245             when N_Null =>
246                Expand_N_Null (N);
247
248             when N_Object_Declaration =>
249                Expand_N_Object_Declaration (N);
250
251             when N_Object_Renaming_Declaration =>
252                Expand_N_Object_Renaming_Declaration (N);
253
254             when N_Op_Add =>
255                Expand_N_Op_Add (N);
256
257             when N_Op_Abs =>
258                Expand_N_Op_Abs (N);
259
260             when N_Op_And =>
261                Expand_N_Op_And (N);
262
263             when N_Op_Concat =>
264                Expand_N_Op_Concat (N);
265
266             when N_Op_Divide =>
267                Expand_N_Op_Divide (N);
268
269             when N_Op_Eq =>
270                Expand_N_Op_Eq (N);
271
272             when N_Op_Expon =>
273                Expand_N_Op_Expon (N);
274
275             when N_Op_Ge =>
276                Expand_N_Op_Ge (N);
277
278             when N_Op_Gt =>
279                Expand_N_Op_Gt (N);
280
281             when N_Op_Le =>
282                Expand_N_Op_Le (N);
283
284             when N_Op_Lt =>
285                Expand_N_Op_Lt (N);
286
287             when N_Op_Minus =>
288                Expand_N_Op_Minus (N);
289
290             when N_Op_Mod =>
291                Expand_N_Op_Mod (N);
292
293             when N_Op_Multiply =>
294                Expand_N_Op_Multiply (N);
295
296             when N_Op_Ne =>
297                Expand_N_Op_Ne (N);
298
299             when N_Op_Not =>
300                Expand_N_Op_Not (N);
301
302             when N_Op_Or =>
303                Expand_N_Op_Or (N);
304
305             when N_Op_Plus =>
306                Expand_N_Op_Plus (N);
307
308             when N_Op_Rem =>
309                Expand_N_Op_Rem (N);
310
311             when N_Op_Rotate_Left =>
312                Expand_N_Op_Rotate_Left (N);
313
314             when N_Op_Rotate_Right =>
315                Expand_N_Op_Rotate_Right (N);
316
317             when N_Op_Shift_Left =>
318                Expand_N_Op_Shift_Left (N);
319
320             when N_Op_Shift_Right =>
321                Expand_N_Op_Shift_Right (N);
322
323             when N_Op_Shift_Right_Arithmetic =>
324                Expand_N_Op_Shift_Right_Arithmetic (N);
325
326             when N_Op_Subtract =>
327                Expand_N_Op_Subtract (N);
328
329             when N_Op_Xor =>
330                Expand_N_Op_Xor (N);
331
332             when N_Or_Else =>
333                Expand_N_Or_Else (N);
334
335             when N_Package_Body =>
336                Expand_N_Package_Body (N);
337
338             when N_Package_Declaration =>
339                Expand_N_Package_Declaration (N);
340
341             when N_Package_Renaming_Declaration =>
342                Expand_N_Package_Renaming_Declaration (N);
343
344             when N_Pragma =>
345                Expand_N_Pragma (N);
346
347             when N_Procedure_Call_Statement =>
348                Expand_N_Procedure_Call_Statement (N);
349
350             when N_Protected_Type_Declaration =>
351                Expand_N_Protected_Type_Declaration (N);
352
353             when N_Protected_Body =>
354                Expand_N_Protected_Body (N);
355
356             when N_Qualified_Expression =>
357                Expand_N_Qualified_Expression (N);
358
359             when N_Raise_Statement =>
360                Expand_N_Raise_Statement (N);
361
362             when N_Raise_Constraint_Error =>
363                Expand_N_Raise_Constraint_Error (N);
364
365             when N_Raise_Program_Error =>
366                Expand_N_Raise_Program_Error (N);
367
368             when N_Raise_Storage_Error =>
369                Expand_N_Raise_Storage_Error (N);
370
371             when N_Real_Literal =>
372                Expand_N_Real_Literal (N);
373
374             when N_Record_Representation_Clause =>
375                Expand_N_Record_Representation_Clause (N);
376
377             when N_Requeue_Statement =>
378                Expand_N_Requeue_Statement (N);
379
380             when N_Return_Statement =>
381                Expand_N_Return_Statement (N);
382
383             when N_Selected_Component =>
384                Expand_N_Selected_Component (N);
385
386             when N_Selective_Accept =>
387                Expand_N_Selective_Accept (N);
388
389             when N_Single_Task_Declaration =>
390                Expand_N_Single_Task_Declaration (N);
391
392             when N_Slice =>
393                Expand_N_Slice (N);
394
395             when N_Subtype_Indication =>
396                Expand_N_Subtype_Indication (N);
397
398             when N_Subprogram_Body =>
399                Expand_N_Subprogram_Body (N);
400
401             when N_Subprogram_Body_Stub =>
402                Expand_N_Subprogram_Body_Stub (N);
403
404             when N_Subprogram_Declaration =>
405                Expand_N_Subprogram_Declaration (N);
406
407             when N_Subprogram_Info =>
408                Expand_N_Subprogram_Info (N);
409
410             when N_Task_Body =>
411                Expand_N_Task_Body (N);
412
413             when N_Task_Type_Declaration =>
414                Expand_N_Task_Type_Declaration (N);
415
416             when N_Timed_Entry_Call =>
417                Expand_N_Timed_Entry_Call (N);
418
419             when N_Type_Conversion =>
420                Expand_N_Type_Conversion (N);
421
422             when N_Unchecked_Expression =>
423                Expand_N_Unchecked_Expression (N);
424
425             when N_Unchecked_Type_Conversion =>
426                Expand_N_Unchecked_Type_Conversion (N);
427
428             when N_Variant_Part =>
429                Expand_N_Variant_Part (N);
430
431             --  For all other node kinds, no expansion activity is required
432
433             when others => null;
434
435          end case;
436
437          --  Set result as analyzed and then do a possible transient wrap. The
438          --  transient wrap must be done after the Analyzed flag is set on, so
439          --  that we do not get a recursive attempt to expand the node N.
440
441          Set_Analyzed (N);
442
443          --  Deal with transient scopes
444
445          if Scope_Is_Transient and then N = Node_To_Be_Wrapped then
446
447             case Nkind (N) is
448                when N_Statement_Other_Than_Procedure_Call |
449                     N_Procedure_Call_Statement            =>
450                   Wrap_Transient_Statement (N);
451
452                when N_Object_Declaration          |
453                     N_Object_Renaming_Declaration |
454                     N_Subtype_Declaration         =>
455                   Wrap_Transient_Declaration (N);
456
457                when others => Wrap_Transient_Expression (N);
458             end case;
459          end if;
460
461          Debug_A_Exit ("expanding  ", N, "  (done)");
462       end if;
463    end Expand;
464
465    ---------------------------
466    -- Expander_Mode_Restore --
467    ---------------------------
468
469    procedure Expander_Mode_Restore is
470    begin
471       Expander_Active := Expander_Flags.Table (Expander_Flags.Last);
472       Expander_Flags.Decrement_Last;
473
474       if Serious_Errors_Detected /= 0 then
475          Expander_Active := False;
476       end if;
477    end Expander_Mode_Restore;
478
479    --------------------------------
480    -- Expander_Mode_Save_And_Set --
481    --------------------------------
482
483    procedure Expander_Mode_Save_And_Set (Status : Boolean) is
484    begin
485       Expander_Flags.Increment_Last;
486       Expander_Flags.Table (Expander_Flags.Last) := Expander_Active;
487       Expander_Active := Status;
488    end Expander_Mode_Save_And_Set;
489
490 end Expander;