1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
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. --
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). --
27 ------------------------------------------------------------------------------
29 with Atree
; use Atree
;
30 with Debug_A
; use Debug_A
;
31 with Errout
; use Errout
;
32 with Exp_Aggr
; use Exp_Aggr
;
33 with Exp_Attr
; use Exp_Attr
;
34 with Exp_Ch2
; use Exp_Ch2
;
35 with Exp_Ch3
; use Exp_Ch3
;
36 with Exp_Ch4
; use Exp_Ch4
;
37 with Exp_Ch5
; use Exp_Ch5
;
38 with Exp_Ch6
; use Exp_Ch6
;
39 with Exp_Ch7
; use Exp_Ch7
;
40 with Exp_Ch8
; use Exp_Ch8
;
41 with Exp_Ch9
; use Exp_Ch9
;
42 with Exp_Ch11
; use Exp_Ch11
;
43 with Exp_Ch12
; use Exp_Ch12
;
44 with Exp_Ch13
; use Exp_Ch13
;
45 with Exp_Prag
; use Exp_Prag
;
48 with Sem_Ch8
; use Sem_Ch8
;
49 with Sem_Util
; use Sem_Util
;
50 with Sinfo
; use Sinfo
;
53 package body Expander
is
59 -- The following table is used to save values of the Expander_Active
60 -- flag when they are saved by Expander_Mode_Save_And_Set. We use an
61 -- extendible table (which is a bit of overkill) because it is easier
62 -- than figuring out a maximum value or bothering with range checks!
64 package Expander_Flags
is new Table
.Table
(
65 Table_Component_Type
=> Boolean,
66 Table_Index_Type
=> Int
,
69 Table_Increment
=> 200,
70 Table_Name
=> "Expander_Flags");
76 procedure Expand
(N
: Node_Id
) is
78 -- If we were analyzing a default expression the Full_Analysis flag
79 -- must have be off. If we are in expansion mode then we must be
80 -- performing a full analysis. If we are analyzing a generic then
81 -- Expansion must be off.
84 (not (Full_Analysis
and then In_Default_Expression
)
85 and then (Full_Analysis
or else not Expander_Active
)
86 and then not (Inside_A_Generic
and then Expander_Active
));
88 -- There are three reasons for the Expander_Active flag to be false.
90 -- The first is when are not generating code. In this mode the
91 -- Full_Analysis flag indicates whether we are performing a complete
92 -- analysis, in which case Full_Analysis = True or a pre-analysis in
93 -- which case Full_Analysis = False. See the spec of Sem for more
96 -- The second reason for the Expander_Active flag to be False is that
97 -- we are performing a pre-analysis. During pre-analysis all
98 -- expansion activity is turned off to make sure nodes are
99 -- semantically decorated but no extra nodes are generated. This is
100 -- for instance needed for the first pass of aggregate semantic
101 -- processing. Note that in this case the Full_Analysis flag is set
102 -- to False because the node will subsequently be re-analyzed with
103 -- expansion on (see the spec of sem).
105 -- Finally, expansion is turned off in a regular compilation if there
106 -- are semantic errors. In that case there will be no further expansion,
107 -- but one cleanup action may be required: if a transient scope was
108 -- created (e.g. for a function that returns an unconstrained type)
109 -- the scope may still be on the stack, and must be removed explicitly,
110 -- given that the expansion actions that would normally process it will
111 -- not take place. This prevents cascaded errors due to stack mismatch.
113 if not Expander_Active
then
114 Set_Analyzed
(N
, Full_Analysis
);
116 if Errors_Detected
> 0
117 and then Scope_Is_Transient
120 (Scope_Stack
.Last
).Actions_To_Be_Wrapped_Before
:= No_List
;
122 (Scope_Stack
.Last
).Actions_To_Be_Wrapped_After
:= No_List
;
131 Debug_A_Entry
("expanding ", N
);
133 -- Processing depends on node kind. For full details on the expansion
134 -- activity required in each case, see bodies of corresponding
139 when N_Abort_Statement
=>
140 Expand_N_Abort_Statement
(N
);
142 when N_Accept_Statement
=>
143 Expand_N_Accept_Statement
(N
);
146 Expand_N_Aggregate
(N
);
149 Expand_N_Allocator
(N
);
152 Expand_N_And_Then
(N
);
154 when N_Assignment_Statement
=>
155 Expand_N_Assignment_Statement
(N
);
157 when N_Asynchronous_Select
=>
158 Expand_N_Asynchronous_Select
(N
);
160 when N_Attribute_Definition_Clause
=>
161 Expand_N_Attribute_Definition_Clause
(N
);
163 when N_Attribute_Reference
=>
164 Expand_N_Attribute_Reference
(N
);
166 when N_Block_Statement
=>
167 Expand_N_Block_Statement
(N
);
169 when N_Case_Statement
=>
170 Expand_N_Case_Statement
(N
);
172 when N_Conditional_Entry_Call
=>
173 Expand_N_Conditional_Entry_Call
(N
);
175 when N_Conditional_Expression
=>
176 Expand_N_Conditional_Expression
(N
);
178 when N_Delay_Relative_Statement
=>
179 Expand_N_Delay_Relative_Statement
(N
);
181 when N_Delay_Until_Statement
=>
182 Expand_N_Delay_Until_Statement
(N
);
185 Expand_N_Entry_Body
(N
);
187 when N_Entry_Call_Statement
=>
188 Expand_N_Entry_Call_Statement
(N
);
190 when N_Entry_Declaration
=>
191 Expand_N_Entry_Declaration
(N
);
193 when N_Exception_Declaration
=>
194 Expand_N_Exception_Declaration
(N
);
196 when N_Exception_Renaming_Declaration
=>
197 Expand_N_Exception_Renaming_Declaration
(N
);
199 when N_Exit_Statement
=>
200 Expand_N_Exit_Statement
(N
);
202 when N_Expanded_Name
=>
203 Expand_N_Expanded_Name
(N
);
205 when N_Explicit_Dereference
=>
206 Expand_N_Explicit_Dereference
(N
);
208 when N_Extension_Aggregate
=>
209 Expand_N_Extension_Aggregate
(N
);
211 when N_Freeze_Entity
=>
212 Expand_N_Freeze_Entity
(N
);
214 when N_Full_Type_Declaration
=>
215 Expand_N_Full_Type_Declaration
(N
);
217 when N_Function_Call
=>
218 Expand_N_Function_Call
(N
);
220 when N_Generic_Instantiation
=>
221 Expand_N_Generic_Instantiation
(N
);
223 when N_Goto_Statement
=>
224 Expand_N_Goto_Statement
(N
);
226 when N_Handled_Sequence_Of_Statements
=>
227 Expand_N_Handled_Sequence_Of_Statements
(N
);
230 Expand_N_Identifier
(N
);
232 when N_Indexed_Component
=>
233 Expand_N_Indexed_Component
(N
);
235 when N_If_Statement
=>
236 Expand_N_If_Statement
(N
);
241 when N_Loop_Statement
=>
242 Expand_N_Loop_Statement
(N
);
250 when N_Object_Declaration
=>
251 Expand_N_Object_Declaration
(N
);
253 when N_Object_Renaming_Declaration
=>
254 Expand_N_Object_Renaming_Declaration
(N
);
266 Expand_N_Op_Concat
(N
);
269 Expand_N_Op_Divide
(N
);
275 Expand_N_Op_Expon
(N
);
290 Expand_N_Op_Minus
(N
);
295 when N_Op_Multiply
=>
296 Expand_N_Op_Multiply
(N
);
308 Expand_N_Op_Plus
(N
);
313 when N_Op_Rotate_Left
=>
314 Expand_N_Op_Rotate_Left
(N
);
316 when N_Op_Rotate_Right
=>
317 Expand_N_Op_Rotate_Right
(N
);
319 when N_Op_Shift_Left
=>
320 Expand_N_Op_Shift_Left
(N
);
322 when N_Op_Shift_Right
=>
323 Expand_N_Op_Shift_Right
(N
);
325 when N_Op_Shift_Right_Arithmetic
=>
326 Expand_N_Op_Shift_Right_Arithmetic
(N
);
328 when N_Op_Subtract
=>
329 Expand_N_Op_Subtract
(N
);
335 Expand_N_Or_Else
(N
);
337 when N_Package_Body
=>
338 Expand_N_Package_Body
(N
);
340 when N_Package_Declaration
=>
341 Expand_N_Package_Declaration
(N
);
343 when N_Package_Renaming_Declaration
=>
344 Expand_N_Package_Renaming_Declaration
(N
);
349 when N_Procedure_Call_Statement
=>
350 Expand_N_Procedure_Call_Statement
(N
);
352 when N_Protected_Type_Declaration
=>
353 Expand_N_Protected_Type_Declaration
(N
);
355 when N_Protected_Body
=>
356 Expand_N_Protected_Body
(N
);
358 when N_Qualified_Expression
=>
359 Expand_N_Qualified_Expression
(N
);
361 when N_Raise_Statement
=>
362 Expand_N_Raise_Statement
(N
);
364 when N_Raise_Constraint_Error
=>
365 Expand_N_Raise_Constraint_Error
(N
);
367 when N_Raise_Program_Error
=>
368 Expand_N_Raise_Program_Error
(N
);
370 when N_Raise_Storage_Error
=>
371 Expand_N_Raise_Storage_Error
(N
);
373 when N_Real_Literal
=>
374 Expand_N_Real_Literal
(N
);
376 when N_Record_Representation_Clause
=>
377 Expand_N_Record_Representation_Clause
(N
);
379 when N_Requeue_Statement
=>
380 Expand_N_Requeue_Statement
(N
);
382 when N_Return_Statement
=>
383 Expand_N_Return_Statement
(N
);
385 when N_Selected_Component
=>
386 Expand_N_Selected_Component
(N
);
388 when N_Selective_Accept
=>
389 Expand_N_Selective_Accept
(N
);
391 when N_Single_Task_Declaration
=>
392 Expand_N_Single_Task_Declaration
(N
);
397 when N_Subtype_Indication
=>
398 Expand_N_Subtype_Indication
(N
);
400 when N_Subprogram_Body
=>
401 Expand_N_Subprogram_Body
(N
);
403 when N_Subprogram_Body_Stub
=>
404 Expand_N_Subprogram_Body_Stub
(N
);
406 when N_Subprogram_Declaration
=>
407 Expand_N_Subprogram_Declaration
(N
);
409 when N_Subprogram_Info
=>
410 Expand_N_Subprogram_Info
(N
);
413 Expand_N_Task_Body
(N
);
415 when N_Task_Type_Declaration
=>
416 Expand_N_Task_Type_Declaration
(N
);
418 when N_Timed_Entry_Call
=>
419 Expand_N_Timed_Entry_Call
(N
);
421 when N_Type_Conversion
=>
422 Expand_N_Type_Conversion
(N
);
424 when N_Unchecked_Expression
=>
425 Expand_N_Unchecked_Expression
(N
);
427 when N_Unchecked_Type_Conversion
=>
428 Expand_N_Unchecked_Type_Conversion
(N
);
430 when N_Variant_Part
=>
431 Expand_N_Variant_Part
(N
);
433 -- For all other node kinds, no expansion activity is required
439 -- Set result as analyzed and then do a possible transient wrap. The
440 -- transient wrap must be done after the Analyzed flag is set on, so
441 -- that we do not get a recursive attempt to expand the node N.
445 -- Deal with transient scopes
447 if Scope_Is_Transient
and then N
= Node_To_Be_Wrapped
then
450 when N_Statement_Other_Than_Procedure_Call |
451 N_Procedure_Call_Statement
=>
452 Wrap_Transient_Statement
(N
);
454 when N_Object_Declaration |
455 N_Object_Renaming_Declaration |
456 N_Subtype_Declaration
=>
457 Wrap_Transient_Declaration
(N
);
459 when others => Wrap_Transient_Expression
(N
);
463 Debug_A_Exit
("expanding ", N
, " (done)");
467 ---------------------------
468 -- Expander_Mode_Restore --
469 ---------------------------
471 procedure Expander_Mode_Restore
is
473 Expander_Active
:= Expander_Flags
.Table
(Expander_Flags
.Last
);
474 Expander_Flags
.Decrement_Last
;
476 if Errors_Detected
/= 0 then
477 Expander_Active
:= False;
479 end Expander_Mode_Restore
;
481 --------------------------------
482 -- Expander_Mode_Save_And_Set --
483 --------------------------------
485 procedure Expander_Mode_Save_And_Set
(Status
: Boolean) is
487 Expander_Flags
.Increment_Last
;
488 Expander_Flags
.Table
(Expander_Flags
.Last
) := Expander_Active
;
489 Expander_Active
:= Status
;
490 end Expander_Mode_Save_And_Set
;