gcc/
[official-gcc.git] / gcc / ada / expander.adb
blobf6e65e7a40b75cea8f2cb5580f7058581280ceee
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P A N D E R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2013, 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 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Debug_A; use Debug_A;
28 with Exp_Aggr; use Exp_Aggr;
29 with Exp_SPARK; use Exp_SPARK;
30 with Exp_Attr; use Exp_Attr;
31 with Exp_Ch2; use Exp_Ch2;
32 with Exp_Ch3; use Exp_Ch3;
33 with Exp_Ch4; use Exp_Ch4;
34 with Exp_Ch5; use Exp_Ch5;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch8; use Exp_Ch8;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Ch11; use Exp_Ch11;
40 with Exp_Ch12; use Exp_Ch12;
41 with Exp_Ch13; use Exp_Ch13;
42 with Exp_Prag; use Exp_Prag;
43 with Opt; use Opt;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Ch8; use Sem_Ch8;
47 with Sem_Util; use Sem_Util;
48 with Sinfo; use Sinfo;
49 with Table;
51 package body Expander is
53 ----------------
54 -- Local Data --
55 ----------------
57 -- The following table is used to save values of the Expander_Active flag
58 -- when they are saved by Expander_Mode_Save_And_Set. We use an extendible
59 -- table (which is a bit of overkill) because it is easier than figuring
60 -- out a maximum value or bothering with range checks.
62 package Expander_Flags is new Table.Table (
63 Table_Component_Type => Boolean,
64 Table_Index_Type => Int,
65 Table_Low_Bound => 0,
66 Table_Initial => 32,
67 Table_Increment => 200,
68 Table_Name => "Expander_Flags");
70 ------------
71 -- Expand --
72 ------------
74 procedure Expand (N : Node_Id) is
75 begin
76 -- If we were analyzing a default expression (or other spec expression)
77 -- the Full_Analysis flag must be off. If we are in expansion mode then
78 -- we must be performing a full analysis. If we are analyzing a generic
79 -- then Expansion must be off.
81 pragma Assert
82 (not (Full_Analysis and then In_Spec_Expression)
83 and then (Full_Analysis or else not Expander_Active)
84 and then not (Inside_A_Generic and then Expander_Active));
86 -- There are three reasons for the Expander_Active flag to be false
88 -- The first is when are not generating code. In this mode the
89 -- Full_Analysis flag indicates whether we are performing a complete
90 -- analysis, in which case Full_Analysis = True or a pre-analysis in
91 -- which case Full_Analysis = False. See the spec of Sem for more info
92 -- on this.
94 -- Additionally, the GNATprove_Mode flag indicates that a light
95 -- expansion for formal verification should be used. This expansion is
96 -- never done inside generics, because otherwise, this breaks the name
97 -- resolution mechanism for generic instances
99 -- The second reason for the Expander_Active flag to be False is that
100 -- we are performing a pre-analysis. During pre-analysis all expansion
101 -- activity is turned off to make sure nodes are semantically decorated
102 -- but no extra nodes are generated. This is for instance needed for
103 -- the first pass of aggregate semantic processing. Note that in this
104 -- case the Full_Analysis flag is set to False because the node will
105 -- subsequently be re-analyzed with expansion on (see the spec of sem).
107 -- Finally, expansion is turned off in a regular compilation if there
108 -- are serious errors. In that case there will be no further expansion,
109 -- but one cleanup action may be required: if a transient scope was
110 -- created (e.g. for a function that returns an unconstrained type) the
111 -- scope may still be on the stack, and must be removed explicitly,
112 -- given that the expansion actions that would normally process it will
113 -- not take place. This prevents cascaded errors due to stack mismatch.
115 if not Expander_Active
116 and (Inside_A_Generic or not Full_Analysis or not GNATprove_Mode)
117 then
118 Set_Analyzed (N, Full_Analysis);
120 if Serious_Errors_Detected > 0 and then Scope_Is_Transient then
121 Scope_Stack.Table
122 (Scope_Stack.Last).Actions_To_Be_Wrapped_Before := No_List;
123 Scope_Stack.Table
124 (Scope_Stack.Last).Actions_To_Be_Wrapped_After := No_List;
126 Pop_Scope;
127 end if;
129 return;
131 else
132 Debug_A_Entry ("expanding ", N);
134 begin
135 -- In GNATprove mode we only need a very limited subset of
136 -- the usual expansions. This limited subset is implemented
137 -- in Expand_SPARK.
139 if GNATprove_Mode then
140 Expand_SPARK (N);
141 Set_Analyzed (N);
143 -- Regular expansion is normally followed by special handling
144 -- for transient scopes for unconstrained results, etc. but
145 -- this is not needed, and in general cannot be done correctly,
146 -- in this mode, so we are all done.
148 return;
150 -- Here for normal non-SPARK mode
152 else
153 -- Processing depends on node kind. For full details on the
154 -- expansion activity required in each case, see bodies of
155 -- corresponding expand routines.
157 case Nkind (N) is
159 when N_Abort_Statement =>
160 Expand_N_Abort_Statement (N);
162 when N_Accept_Statement =>
163 Expand_N_Accept_Statement (N);
165 when N_Aggregate =>
166 Expand_N_Aggregate (N);
168 when N_Allocator =>
169 Expand_N_Allocator (N);
171 when N_And_Then =>
172 Expand_N_And_Then (N);
174 when N_Assignment_Statement =>
175 Expand_N_Assignment_Statement (N);
177 when N_Asynchronous_Select =>
178 Expand_N_Asynchronous_Select (N);
180 when N_Attribute_Definition_Clause =>
181 Expand_N_Attribute_Definition_Clause (N);
183 when N_Attribute_Reference =>
184 Expand_N_Attribute_Reference (N);
186 when N_Block_Statement =>
187 Expand_N_Block_Statement (N);
189 when N_Case_Expression =>
190 Expand_N_Case_Expression (N);
192 when N_Case_Statement =>
193 Expand_N_Case_Statement (N);
195 when N_Conditional_Entry_Call =>
196 Expand_N_Conditional_Entry_Call (N);
198 when N_Delay_Relative_Statement =>
199 Expand_N_Delay_Relative_Statement (N);
201 when N_Delay_Until_Statement =>
202 Expand_N_Delay_Until_Statement (N);
204 when N_Entry_Body =>
205 Expand_N_Entry_Body (N);
207 when N_Entry_Call_Statement =>
208 Expand_N_Entry_Call_Statement (N);
210 when N_Entry_Declaration =>
211 Expand_N_Entry_Declaration (N);
213 when N_Exception_Declaration =>
214 Expand_N_Exception_Declaration (N);
216 when N_Exception_Renaming_Declaration =>
217 Expand_N_Exception_Renaming_Declaration (N);
219 when N_Exit_Statement =>
220 Expand_N_Exit_Statement (N);
222 when N_Expanded_Name =>
223 Expand_N_Expanded_Name (N);
225 when N_Explicit_Dereference =>
226 Expand_N_Explicit_Dereference (N);
228 when N_Expression_With_Actions =>
229 Expand_N_Expression_With_Actions (N);
231 when N_Extended_Return_Statement =>
232 Expand_N_Extended_Return_Statement (N);
234 when N_Extension_Aggregate =>
235 Expand_N_Extension_Aggregate (N);
237 when N_Free_Statement =>
238 Expand_N_Free_Statement (N);
240 when N_Freeze_Entity =>
241 Expand_N_Freeze_Entity (N);
243 when N_Full_Type_Declaration =>
244 Expand_N_Full_Type_Declaration (N);
246 when N_Function_Call =>
247 Expand_N_Function_Call (N);
249 when N_Generic_Instantiation =>
250 Expand_N_Generic_Instantiation (N);
252 when N_Goto_Statement =>
253 Expand_N_Goto_Statement (N);
255 when N_Handled_Sequence_Of_Statements =>
256 Expand_N_Handled_Sequence_Of_Statements (N);
258 when N_Identifier =>
259 Expand_N_Identifier (N);
261 when N_If_Expression =>
262 Expand_N_If_Expression (N);
264 when N_Indexed_Component =>
265 Expand_N_Indexed_Component (N);
267 when N_If_Statement =>
268 Expand_N_If_Statement (N);
270 when N_In =>
271 Expand_N_In (N);
273 when N_Loop_Statement =>
274 Expand_N_Loop_Statement (N);
276 when N_Not_In =>
277 Expand_N_Not_In (N);
279 when N_Null =>
280 Expand_N_Null (N);
282 when N_Object_Declaration =>
283 Expand_N_Object_Declaration (N);
285 when N_Object_Renaming_Declaration =>
286 Expand_N_Object_Renaming_Declaration (N);
288 when N_Op_Add =>
289 Expand_N_Op_Add (N);
291 when N_Op_Abs =>
292 Expand_N_Op_Abs (N);
294 when N_Op_And =>
295 Expand_N_Op_And (N);
297 when N_Op_Concat =>
298 Expand_N_Op_Concat (N);
300 when N_Op_Divide =>
301 Expand_N_Op_Divide (N);
303 when N_Op_Eq =>
304 Expand_N_Op_Eq (N);
306 when N_Op_Expon =>
307 Expand_N_Op_Expon (N);
309 when N_Op_Ge =>
310 Expand_N_Op_Ge (N);
312 when N_Op_Gt =>
313 Expand_N_Op_Gt (N);
315 when N_Op_Le =>
316 Expand_N_Op_Le (N);
318 when N_Op_Lt =>
319 Expand_N_Op_Lt (N);
321 when N_Op_Minus =>
322 Expand_N_Op_Minus (N);
324 when N_Op_Mod =>
325 Expand_N_Op_Mod (N);
327 when N_Op_Multiply =>
328 Expand_N_Op_Multiply (N);
330 when N_Op_Ne =>
331 Expand_N_Op_Ne (N);
333 when N_Op_Not =>
334 Expand_N_Op_Not (N);
336 when N_Op_Or =>
337 Expand_N_Op_Or (N);
339 when N_Op_Plus =>
340 Expand_N_Op_Plus (N);
342 when N_Op_Rem =>
343 Expand_N_Op_Rem (N);
345 when N_Op_Rotate_Left =>
346 Expand_N_Op_Rotate_Left (N);
348 when N_Op_Rotate_Right =>
349 Expand_N_Op_Rotate_Right (N);
351 when N_Op_Shift_Left =>
352 Expand_N_Op_Shift_Left (N);
354 when N_Op_Shift_Right =>
355 Expand_N_Op_Shift_Right (N);
357 when N_Op_Shift_Right_Arithmetic =>
358 Expand_N_Op_Shift_Right_Arithmetic (N);
360 when N_Op_Subtract =>
361 Expand_N_Op_Subtract (N);
363 when N_Op_Xor =>
364 Expand_N_Op_Xor (N);
366 when N_Or_Else =>
367 Expand_N_Or_Else (N);
369 when N_Package_Body =>
370 Expand_N_Package_Body (N);
372 when N_Package_Declaration =>
373 Expand_N_Package_Declaration (N);
375 when N_Package_Renaming_Declaration =>
376 Expand_N_Package_Renaming_Declaration (N);
378 when N_Subprogram_Renaming_Declaration =>
379 Expand_N_Subprogram_Renaming_Declaration (N);
381 when N_Pragma =>
382 Expand_N_Pragma (N);
384 when N_Procedure_Call_Statement =>
385 Expand_N_Procedure_Call_Statement (N);
387 when N_Protected_Type_Declaration =>
388 Expand_N_Protected_Type_Declaration (N);
390 when N_Protected_Body =>
391 Expand_N_Protected_Body (N);
393 when N_Qualified_Expression =>
394 Expand_N_Qualified_Expression (N);
396 when N_Quantified_Expression =>
397 Expand_N_Quantified_Expression (N);
399 when N_Raise_Statement =>
400 Expand_N_Raise_Statement (N);
402 when N_Raise_Constraint_Error =>
403 Expand_N_Raise_Constraint_Error (N);
405 when N_Raise_Expression =>
406 Expand_N_Raise_Expression (N);
408 when N_Raise_Program_Error =>
409 Expand_N_Raise_Program_Error (N);
411 when N_Raise_Storage_Error =>
412 Expand_N_Raise_Storage_Error (N);
414 when N_Real_Literal =>
415 Expand_N_Real_Literal (N);
417 when N_Record_Representation_Clause =>
418 Expand_N_Record_Representation_Clause (N);
420 when N_Requeue_Statement =>
421 Expand_N_Requeue_Statement (N);
423 when N_Simple_Return_Statement =>
424 Expand_N_Simple_Return_Statement (N);
426 when N_Selected_Component =>
427 Expand_N_Selected_Component (N);
429 when N_Selective_Accept =>
430 Expand_N_Selective_Accept (N);
432 when N_Single_Task_Declaration =>
433 Expand_N_Single_Task_Declaration (N);
435 when N_Slice =>
436 Expand_N_Slice (N);
438 when N_Subtype_Indication =>
439 Expand_N_Subtype_Indication (N);
441 when N_Subprogram_Body =>
442 Expand_N_Subprogram_Body (N);
444 when N_Subprogram_Body_Stub =>
445 Expand_N_Subprogram_Body_Stub (N);
447 when N_Subprogram_Declaration =>
448 Expand_N_Subprogram_Declaration (N);
450 when N_Task_Body =>
451 Expand_N_Task_Body (N);
453 when N_Task_Type_Declaration =>
454 Expand_N_Task_Type_Declaration (N);
456 when N_Timed_Entry_Call =>
457 Expand_N_Timed_Entry_Call (N);
459 when N_Type_Conversion =>
460 Expand_N_Type_Conversion (N);
462 when N_Unchecked_Expression =>
463 Expand_N_Unchecked_Expression (N);
465 when N_Unchecked_Type_Conversion =>
466 Expand_N_Unchecked_Type_Conversion (N);
468 when N_Variant_Part =>
469 Expand_N_Variant_Part (N);
471 -- For all other node kinds, no expansion activity required
473 when others =>
474 null;
476 end case;
477 end if;
479 exception
480 when RE_Not_Available =>
481 return;
482 end;
484 -- Set result as analyzed and then do a possible transient wrap. The
485 -- transient wrap must be done after the Analyzed flag is set on, so
486 -- that we do not get a recursive attempt to expand the node N.
488 Set_Analyzed (N);
490 -- Deal with transient scopes
492 if Scope_Is_Transient and then N = Node_To_Be_Wrapped then
493 case Nkind (N) is
494 when N_Statement_Other_Than_Procedure_Call |
495 N_Procedure_Call_Statement =>
496 Wrap_Transient_Statement (N);
498 when N_Object_Declaration |
499 N_Object_Renaming_Declaration |
500 N_Subtype_Declaration =>
501 Wrap_Transient_Declaration (N);
503 when others => Wrap_Transient_Expression (N);
504 end case;
505 end if;
507 Debug_A_Exit ("expanding ", N, " (done)");
508 end if;
509 end Expand;
511 ---------------------------
512 -- Expander_Mode_Restore --
513 ---------------------------
515 procedure Expander_Mode_Restore is
516 begin
517 -- Not active (has no effect) in ASIS and GNATprove modes (see comments
518 -- in spec of Expander_Mode_Save_And_Set).
520 if ASIS_Mode or GNATprove_Mode then
521 return;
522 end if;
524 -- Otherwise restore the flag
526 Expander_Active := Expander_Flags.Table (Expander_Flags.Last);
527 Expander_Flags.Decrement_Last;
529 -- Keep expander off if serious errors detected. In this case we do not
530 -- need expansion, and continued expansion may cause cascaded errors or
531 -- compiler bombs.
533 if Serious_Errors_Detected /= 0 then
534 Expander_Active := False;
535 end if;
536 end Expander_Mode_Restore;
538 --------------------------------
539 -- Expander_Mode_Save_And_Set --
540 --------------------------------
542 procedure Expander_Mode_Save_And_Set (Status : Boolean) is
543 begin
544 -- Not active (has no effect) in ASIS and GNATprove modes (see comments
545 -- in spec of Expander_Mode_Save_And_Set).
547 if ASIS_Mode or GNATprove_Mode then
548 return;
549 end if;
551 -- Otherwise save and set the flag
553 Expander_Flags.Increment_Last;
554 Expander_Flags.Table (Expander_Flags.Last) := Expander_Active;
555 Expander_Active := Status;
556 end Expander_Mode_Save_And_Set;
558 end Expander;