PR middle-end/77674
[official-gcc.git] / gcc / ada / expander.adb
blob84d64226772d0b6cb0178eef011f1ba14f55186e
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-2016, 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; use Debug;
28 with Debug_A; use Debug_A;
29 with Exp_Aggr; use Exp_Aggr;
30 with Exp_SPARK; use Exp_SPARK;
31 with Exp_Attr; use Exp_Attr;
32 with Exp_Ch2; use Exp_Ch2;
33 with Exp_Ch3; use Exp_Ch3;
34 with Exp_Ch4; use Exp_Ch4;
35 with Exp_Ch5; use Exp_Ch5;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch8; use Exp_Ch8;
39 with Exp_Ch9; use Exp_Ch9;
40 with Exp_Ch11; use Exp_Ch11;
41 with Exp_Ch12; use Exp_Ch12;
42 with Exp_Ch13; use Exp_Ch13;
43 with Exp_Prag; use Exp_Prag;
44 with Opt; use Opt;
45 with Rtsfind; use Rtsfind;
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;
52 package body Expander is
54 ----------------
55 -- Local Data --
56 ----------------
58 -- The following table is used to save values of the Expander_Active flag
59 -- when they are saved by Expander_Mode_Save_And_Set. We use an extendible
60 -- table (which is a bit of overkill) because it is easier than figuring
61 -- out a maximum value or bothering with range checks.
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");
71 Abort_Bug_Box_Error : exception;
72 -- Arbitrary exception to raise for implementation of -gnatd.B. See "when
73 -- N_Abort_Statement" below. See also debug.adb.
75 ------------
76 -- Expand --
77 ------------
79 procedure Expand (N : Node_Id) is
80 begin
81 -- If we were analyzing a default expression (or other spec expression)
82 -- the Full_Analysis flag must be off. If we are in expansion mode then
83 -- we must be performing a full analysis. If we are analyzing a generic
84 -- then Expansion must be off.
86 pragma Assert
87 (not (Full_Analysis and then In_Spec_Expression)
88 and then (Full_Analysis or else not Expander_Active)
89 and then not (Inside_A_Generic and then Expander_Active));
91 -- The GNATprove_Mode flag indicates that a light expansion for formal
92 -- verification should be used. This expansion is never done inside
93 -- generics, because otherwise, this breaks the name resolution
94 -- mechanism for generic instances.
96 if GNATprove_Mode then
97 if not Inside_A_Generic then
98 Expand_SPARK (N);
99 end if;
101 Set_Analyzed (N, Full_Analysis);
103 -- Regular expansion is normally followed by special handling for
104 -- transient scopes for unconstrained results, etc. but this is not
105 -- needed, and in general cannot be done correctly, in this mode, so
106 -- we are all done.
108 return;
110 -- There are three reasons for the Expander_Active flag to be false
112 -- The first is when are not generating code. In this mode the
113 -- Full_Analysis flag indicates whether we are performing a complete
114 -- analysis, in which case Full_Analysis = True or a pre-analysis in
115 -- which case Full_Analysis = False. See the spec of Sem for more info
116 -- on this.
118 -- The second reason for the Expander_Active flag to be False is that
119 -- we are performing a pre-analysis. During pre-analysis all expansion
120 -- activity is turned off to make sure nodes are semantically decorated
121 -- but no extra nodes are generated. This is for instance needed for
122 -- the first pass of aggregate semantic processing. Note that in this
123 -- case the Full_Analysis flag is set to False because the node will
124 -- subsequently be re-analyzed with expansion on (see the spec of sem).
126 -- Finally, expansion is turned off in a regular compilation if there
127 -- are serious errors. In that case there will be no further expansion,
128 -- but one cleanup action may be required: if a transient scope was
129 -- created (e.g. for a function that returns an unconstrained type) the
130 -- scope may still be on the stack, and must be removed explicitly,
131 -- given that the expansion actions that would normally process it will
132 -- not take place. This prevents cascaded errors due to stack mismatch.
134 elsif not Expander_Active then
135 Set_Analyzed (N, Full_Analysis);
137 if Serious_Errors_Detected > 0 and then Scope_Is_Transient then
138 Scope_Stack.Table
139 (Scope_Stack.Last).Actions_To_Be_Wrapped := (others => No_List);
140 Pop_Scope;
141 end if;
143 return;
145 else
146 begin
147 Debug_A_Entry ("expanding ", N);
149 -- Processing depends on node kind. For full details on the
150 -- expansion activity required in each case, see bodies of
151 -- corresponding expand routines.
153 case Nkind (N) is
154 when N_Abort_Statement =>
155 Expand_N_Abort_Statement (N);
157 -- If -gnatd.B switch was given, crash the compiler. See
158 -- debug.adb for explanation.
160 if Debug_Flag_Dot_BB then
161 raise Abort_Bug_Box_Error;
162 end if;
164 when N_Accept_Statement =>
165 Expand_N_Accept_Statement (N);
167 when N_Aggregate =>
168 Expand_N_Aggregate (N);
170 when N_Allocator =>
171 Expand_N_Allocator (N);
173 when N_And_Then =>
174 Expand_N_And_Then (N);
176 when N_Assignment_Statement =>
177 Expand_N_Assignment_Statement (N);
179 when N_Asynchronous_Select =>
180 Expand_N_Asynchronous_Select (N);
182 when N_Attribute_Definition_Clause =>
183 Expand_N_Attribute_Definition_Clause (N);
185 when N_Attribute_Reference =>
186 Expand_N_Attribute_Reference (N);
188 when N_Block_Statement =>
189 Expand_N_Block_Statement (N);
191 when N_Case_Expression =>
192 Expand_N_Case_Expression (N);
194 when N_Case_Statement =>
195 Expand_N_Case_Statement (N);
197 when N_Conditional_Entry_Call =>
198 Expand_N_Conditional_Entry_Call (N);
200 when N_Delay_Relative_Statement =>
201 Expand_N_Delay_Relative_Statement (N);
203 when N_Delay_Until_Statement =>
204 Expand_N_Delay_Until_Statement (N);
206 when N_Entry_Body =>
207 Expand_N_Entry_Body (N);
209 when N_Entry_Call_Statement =>
210 Expand_N_Entry_Call_Statement (N);
212 when N_Entry_Declaration =>
213 Expand_N_Entry_Declaration (N);
215 when N_Exception_Declaration =>
216 Expand_N_Exception_Declaration (N);
218 when N_Exception_Renaming_Declaration =>
219 Expand_N_Exception_Renaming_Declaration (N);
221 when N_Exit_Statement =>
222 Expand_N_Exit_Statement (N);
224 when N_Expanded_Name =>
225 Expand_N_Expanded_Name (N);
227 when N_Explicit_Dereference =>
228 Expand_N_Explicit_Dereference (N);
230 when N_Expression_With_Actions =>
231 Expand_N_Expression_With_Actions (N);
233 when N_Extended_Return_Statement =>
234 Expand_N_Extended_Return_Statement (N);
236 when N_Extension_Aggregate =>
237 Expand_N_Extension_Aggregate (N);
239 when N_Free_Statement =>
240 Expand_N_Free_Statement (N);
242 when N_Freeze_Entity =>
243 Expand_N_Freeze_Entity (N);
245 when N_Full_Type_Declaration =>
246 Expand_N_Full_Type_Declaration (N);
248 when N_Function_Call =>
249 Expand_N_Function_Call (N);
251 when N_Generic_Instantiation =>
252 Expand_N_Generic_Instantiation (N);
254 when N_Goto_Statement =>
255 Expand_N_Goto_Statement (N);
257 when N_Handled_Sequence_Of_Statements =>
258 Expand_N_Handled_Sequence_Of_Statements (N);
260 when N_Identifier =>
261 Expand_N_Identifier (N);
263 when N_If_Expression =>
264 Expand_N_If_Expression (N);
266 when N_Indexed_Component =>
267 Expand_N_Indexed_Component (N);
269 when N_If_Statement =>
270 Expand_N_If_Statement (N);
272 when N_In =>
273 Expand_N_In (N);
275 when N_Loop_Statement =>
276 Expand_N_Loop_Statement (N);
278 when N_Not_In =>
279 Expand_N_Not_In (N);
281 when N_Null =>
282 Expand_N_Null (N);
284 when N_Object_Declaration =>
285 Expand_N_Object_Declaration (N);
287 when N_Object_Renaming_Declaration =>
288 Expand_N_Object_Renaming_Declaration (N);
290 when N_Op_Add =>
291 Expand_N_Op_Add (N);
293 when N_Op_Abs =>
294 Expand_N_Op_Abs (N);
296 when N_Op_And =>
297 Expand_N_Op_And (N);
299 when N_Op_Concat =>
300 Expand_N_Op_Concat (N);
302 when N_Op_Divide =>
303 Expand_N_Op_Divide (N);
305 when N_Op_Eq =>
306 Expand_N_Op_Eq (N);
308 when N_Op_Expon =>
309 Expand_N_Op_Expon (N);
311 when N_Op_Ge =>
312 Expand_N_Op_Ge (N);
314 when N_Op_Gt =>
315 Expand_N_Op_Gt (N);
317 when N_Op_Le =>
318 Expand_N_Op_Le (N);
320 when N_Op_Lt =>
321 Expand_N_Op_Lt (N);
323 when N_Op_Minus =>
324 Expand_N_Op_Minus (N);
326 when N_Op_Mod =>
327 Expand_N_Op_Mod (N);
329 when N_Op_Multiply =>
330 Expand_N_Op_Multiply (N);
332 when N_Op_Ne =>
333 Expand_N_Op_Ne (N);
335 when N_Op_Not =>
336 Expand_N_Op_Not (N);
338 when N_Op_Or =>
339 Expand_N_Op_Or (N);
341 when N_Op_Plus =>
342 Expand_N_Op_Plus (N);
344 when N_Op_Rem =>
345 Expand_N_Op_Rem (N);
347 when N_Op_Rotate_Left =>
348 Expand_N_Op_Rotate_Left (N);
350 when N_Op_Rotate_Right =>
351 Expand_N_Op_Rotate_Right (N);
353 when N_Op_Shift_Left =>
354 Expand_N_Op_Shift_Left (N);
356 when N_Op_Shift_Right =>
357 Expand_N_Op_Shift_Right (N);
359 when N_Op_Shift_Right_Arithmetic =>
360 Expand_N_Op_Shift_Right_Arithmetic (N);
362 when N_Op_Subtract =>
363 Expand_N_Op_Subtract (N);
365 when N_Op_Xor =>
366 Expand_N_Op_Xor (N);
368 when N_Or_Else =>
369 Expand_N_Or_Else (N);
371 when N_Package_Body =>
372 Expand_N_Package_Body (N);
374 when N_Package_Declaration =>
375 Expand_N_Package_Declaration (N);
377 when N_Package_Renaming_Declaration =>
378 Expand_N_Package_Renaming_Declaration (N);
380 when N_Subprogram_Renaming_Declaration =>
381 Expand_N_Subprogram_Renaming_Declaration (N);
383 when N_Pragma =>
384 Expand_N_Pragma (N);
386 when N_Procedure_Call_Statement =>
387 Expand_N_Procedure_Call_Statement (N);
389 when N_Protected_Type_Declaration =>
390 Expand_N_Protected_Type_Declaration (N);
392 when N_Protected_Body =>
393 Expand_N_Protected_Body (N);
395 when N_Qualified_Expression =>
396 Expand_N_Qualified_Expression (N);
398 when N_Quantified_Expression =>
399 Expand_N_Quantified_Expression (N);
401 when N_Raise_Statement =>
402 Expand_N_Raise_Statement (N);
404 when N_Raise_Constraint_Error =>
405 Expand_N_Raise_Constraint_Error (N);
407 when N_Raise_Expression =>
408 Expand_N_Raise_Expression (N);
410 when N_Raise_Program_Error =>
411 Expand_N_Raise_Program_Error (N);
413 when N_Raise_Storage_Error =>
414 Expand_N_Raise_Storage_Error (N);
416 when N_Real_Literal =>
417 Expand_N_Real_Literal (N);
419 when N_Record_Representation_Clause =>
420 Expand_N_Record_Representation_Clause (N);
422 when N_Requeue_Statement =>
423 Expand_N_Requeue_Statement (N);
425 when N_Simple_Return_Statement =>
426 Expand_N_Simple_Return_Statement (N);
428 when N_Selected_Component =>
429 Expand_N_Selected_Component (N);
431 when N_Selective_Accept =>
432 Expand_N_Selective_Accept (N);
434 when N_Single_Protected_Declaration =>
435 Expand_N_Single_Protected_Declaration (N);
437 when N_Single_Task_Declaration =>
438 Expand_N_Single_Task_Declaration (N);
440 when N_Slice =>
441 Expand_N_Slice (N);
443 when N_Subtype_Indication =>
444 Expand_N_Subtype_Indication (N);
446 when N_Subprogram_Body =>
447 Expand_N_Subprogram_Body (N);
449 when N_Subprogram_Body_Stub =>
450 Expand_N_Subprogram_Body_Stub (N);
452 when N_Subprogram_Declaration =>
453 Expand_N_Subprogram_Declaration (N);
455 when N_Task_Body =>
456 Expand_N_Task_Body (N);
458 when N_Task_Type_Declaration =>
459 Expand_N_Task_Type_Declaration (N);
461 when N_Timed_Entry_Call =>
462 Expand_N_Timed_Entry_Call (N);
464 when N_Type_Conversion =>
465 Expand_N_Type_Conversion (N);
467 when N_Unchecked_Expression =>
468 Expand_N_Unchecked_Expression (N);
470 when N_Unchecked_Type_Conversion =>
471 Expand_N_Unchecked_Type_Conversion (N);
473 when N_Variant_Part =>
474 Expand_N_Variant_Part (N);
476 -- For all other node kinds, no expansion activity required
478 when others =>
479 null;
481 end case;
483 exception
484 when RE_Not_Available =>
485 return;
486 end;
488 -- Set result as analyzed and then do a possible transient wrap. The
489 -- transient wrap must be done after the Analyzed flag is set on, so
490 -- that we do not get a recursive attempt to expand the node N.
492 Set_Analyzed (N);
494 -- Deal with transient scopes
496 if Scope_Is_Transient and then N = Node_To_Be_Wrapped then
497 case Nkind (N) is
498 when N_Statement_Other_Than_Procedure_Call |
499 N_Procedure_Call_Statement =>
500 Wrap_Transient_Statement (N);
502 when N_Object_Declaration |
503 N_Object_Renaming_Declaration |
504 N_Subtype_Declaration =>
505 Wrap_Transient_Declaration (N);
507 when others => Wrap_Transient_Expression (N);
508 end case;
509 end if;
511 Debug_A_Exit ("expanding ", N, " (done)");
512 end if;
513 end Expand;
515 ---------------------------
516 -- Expander_Mode_Restore --
517 ---------------------------
519 procedure Expander_Mode_Restore is
520 begin
521 -- Not active (has no effect) in ASIS and GNATprove modes (see comments
522 -- in spec of Expander_Mode_Save_And_Set).
524 if ASIS_Mode or GNATprove_Mode then
525 return;
526 end if;
528 -- Otherwise restore the flag
530 Expander_Active := Expander_Flags.Table (Expander_Flags.Last);
531 Expander_Flags.Decrement_Last;
533 -- Keep expander off if serious errors detected. In this case we do not
534 -- need expansion, and continued expansion may cause cascaded errors or
535 -- compiler bombs.
537 if Serious_Errors_Detected /= 0 then
538 Expander_Active := False;
539 end if;
540 end Expander_Mode_Restore;
542 --------------------------------
543 -- Expander_Mode_Save_And_Set --
544 --------------------------------
546 procedure Expander_Mode_Save_And_Set (Status : Boolean) is
547 begin
548 -- Not active (has no effect) in ASIS and GNATprove modes (see comments
549 -- in spec of Expander_Mode_Save_And_Set).
551 if ASIS_Mode or GNATprove_Mode then
552 return;
553 end if;
555 -- Otherwise save and set the flag
557 Expander_Flags.Increment_Last;
558 Expander_Flags.Table (Expander_Flags.Last) := Expander_Active;
559 Expander_Active := Status;
560 end Expander_Mode_Save_And_Set;
562 end Expander;