Fix build on sparc64-linux-gnu.
[official-gcc.git] / gcc / ada / expander.adb
blobf6d91dc0bb3df8a512f65bae6e086b33d5c7efaf
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-2018, 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 Ghost; use Ghost;
45 with Opt; use Opt;
46 with Rtsfind; use Rtsfind;
47 with Sem; use Sem;
48 with Sem_Ch8; use Sem_Ch8;
49 with Sem_Util; use Sem_Util;
50 with Sinfo; use Sinfo;
51 with Table;
53 package body Expander is
55 ----------------
56 -- Local Data --
57 ----------------
59 -- The following table is used to save values of the Expander_Active flag
60 -- when they are saved by Expander_Mode_Save_And_Set. We use an extendible
61 -- table (which is a bit of overkill) because it is easier than figuring
62 -- 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,
67 Table_Low_Bound => 0,
68 Table_Initial => 32,
69 Table_Increment => 200,
70 Table_Name => "Expander_Flags");
72 Abort_Bug_Box_Error : exception;
73 -- Arbitrary exception to raise for implementation of -gnatd.B. See "when
74 -- N_Abort_Statement" below. See also debug.adb.
76 ------------
77 -- Expand --
78 ------------
80 -- WARNING: This routine manages Ghost regions. Return statements must be
81 -- replaced by gotos which jump to the end of the routine and restore the
82 -- Ghost mode.
84 procedure Expand (N : Node_Id) is
85 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
86 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
87 -- Save the Ghost-related attributes to restore on exit
89 begin
90 -- If we were analyzing a default expression (or other spec expression)
91 -- the Full_Analysis flag must be off. If we are in expansion mode then
92 -- we must be performing a full analysis. If we are analyzing a generic
93 -- then Expansion must be off.
95 pragma Assert
96 (not (Full_Analysis and then In_Spec_Expression)
97 and then (Full_Analysis or else not Expander_Active)
98 and then not (Inside_A_Generic and then Expander_Active));
100 -- Establish the Ghost mode of the context to ensure that any generated
101 -- nodes during expansion are marked as Ghost.
103 Set_Ghost_Mode (N);
105 -- The GNATprove_Mode flag indicates that a light expansion for formal
106 -- verification should be used. This expansion is never done inside
107 -- generics, because otherwise, this breaks the name resolution
108 -- mechanism for generic instances.
110 if GNATprove_Mode then
111 if not Inside_A_Generic then
112 Expand_SPARK (N);
113 end if;
115 Set_Analyzed (N, Full_Analysis);
117 -- Regular expansion is normally followed by special handling for
118 -- transient scopes for unconstrained results, etc. but this is not
119 -- needed, and in general cannot be done correctly, in this mode, so
120 -- we are all done.
122 goto Leave;
124 -- There are three reasons for the Expander_Active flag to be false
126 -- The first is when are not generating code. In this mode the
127 -- Full_Analysis flag indicates whether we are performing a complete
128 -- analysis, in which case Full_Analysis = True or a preanalysis in
129 -- which case Full_Analysis = False. See the spec of Sem for more info
130 -- on this.
132 -- The second reason for the Expander_Active flag to be False is that
133 -- we are performing a preanalysis. During preanalysis all expansion
134 -- activity is turned off to make sure nodes are semantically decorated
135 -- but no extra nodes are generated. This is for instance needed for
136 -- the first pass of aggregate semantic processing. Note that in this
137 -- case the Full_Analysis flag is set to False because the node will
138 -- subsequently be re-analyzed with expansion on (see the spec of sem).
140 -- Finally, expansion is turned off in a regular compilation if there
141 -- are serious errors. In that case there will be no further expansion,
142 -- but one cleanup action may be required: if a transient scope was
143 -- created (e.g. for a function that returns an unconstrained type) the
144 -- scope may still be on the stack, and must be removed explicitly,
145 -- given that the expansion actions that would normally process it will
146 -- not take place. This prevents cascaded errors due to stack mismatch.
148 elsif not Expander_Active then
149 Set_Analyzed (N, Full_Analysis);
151 if Serious_Errors_Detected > 0 and then Scope_Is_Transient then
152 Scope_Stack.Table
153 (Scope_Stack.Last).Actions_To_Be_Wrapped := (others => No_List);
154 Pop_Scope;
155 end if;
157 goto Leave;
159 else
160 begin
161 Debug_A_Entry ("expanding ", N);
163 -- Processing depends on node kind. For full details on the
164 -- expansion activity required in each case, see bodies of
165 -- corresponding expand routines.
167 case Nkind (N) is
168 when N_Abort_Statement =>
169 Expand_N_Abort_Statement (N);
171 -- If -gnatd.B switch was given, crash the compiler. See
172 -- debug.adb for explanation.
174 if Debug_Flag_Dot_BB then
175 raise Abort_Bug_Box_Error;
176 end if;
178 when N_Accept_Statement =>
179 Expand_N_Accept_Statement (N);
181 when N_Aggregate =>
182 Expand_N_Aggregate (N);
184 when N_Allocator =>
185 Expand_N_Allocator (N);
187 when N_And_Then =>
188 Expand_N_And_Then (N);
190 when N_Assignment_Statement =>
191 Expand_N_Assignment_Statement (N);
193 when N_Asynchronous_Select =>
194 Expand_N_Asynchronous_Select (N);
196 when N_Attribute_Definition_Clause =>
197 Expand_N_Attribute_Definition_Clause (N);
199 when N_Attribute_Reference =>
200 Expand_N_Attribute_Reference (N);
202 when N_Block_Statement =>
203 Expand_N_Block_Statement (N);
205 when N_Case_Expression =>
206 Expand_N_Case_Expression (N);
208 when N_Case_Statement =>
209 Expand_N_Case_Statement (N);
211 when N_Conditional_Entry_Call =>
212 Expand_N_Conditional_Entry_Call (N);
214 when N_Delay_Relative_Statement =>
215 Expand_N_Delay_Relative_Statement (N);
217 when N_Delay_Until_Statement =>
218 Expand_N_Delay_Until_Statement (N);
220 when N_Delta_Aggregate =>
221 Expand_N_Delta_Aggregate (N);
223 when N_Entry_Body =>
224 Expand_N_Entry_Body (N);
226 when N_Entry_Call_Statement =>
227 Expand_N_Entry_Call_Statement (N);
229 when N_Entry_Declaration =>
230 Expand_N_Entry_Declaration (N);
232 when N_Exception_Declaration =>
233 Expand_N_Exception_Declaration (N);
235 when N_Exception_Renaming_Declaration =>
236 Expand_N_Exception_Renaming_Declaration (N);
238 when N_Exit_Statement =>
239 Expand_N_Exit_Statement (N);
241 when N_Expanded_Name =>
242 Expand_N_Expanded_Name (N);
244 when N_Explicit_Dereference =>
245 Expand_N_Explicit_Dereference (N);
247 when N_Expression_With_Actions =>
248 Expand_N_Expression_With_Actions (N);
250 when N_Extended_Return_Statement =>
251 Expand_N_Extended_Return_Statement (N);
253 when N_Extension_Aggregate =>
254 Expand_N_Extension_Aggregate (N);
256 when N_Free_Statement =>
257 Expand_N_Free_Statement (N);
259 when N_Freeze_Entity =>
260 Expand_N_Freeze_Entity (N);
262 when N_Full_Type_Declaration =>
263 Expand_N_Full_Type_Declaration (N);
265 when N_Function_Call =>
266 Expand_N_Function_Call (N);
268 when N_Generic_Instantiation =>
269 Expand_N_Generic_Instantiation (N);
271 when N_Goto_Statement =>
272 Expand_N_Goto_Statement (N);
274 when N_Handled_Sequence_Of_Statements =>
275 Expand_N_Handled_Sequence_Of_Statements (N);
277 when N_Identifier =>
278 Expand_N_Identifier (N);
280 when N_If_Expression =>
281 Expand_N_If_Expression (N);
283 when N_Indexed_Component =>
284 Expand_N_Indexed_Component (N);
286 when N_If_Statement =>
287 Expand_N_If_Statement (N);
289 when N_In =>
290 Expand_N_In (N);
292 when N_Loop_Statement =>
293 Expand_N_Loop_Statement (N);
295 when N_Not_In =>
296 Expand_N_Not_In (N);
298 when N_Null =>
299 Expand_N_Null (N);
301 when N_Object_Declaration =>
302 Expand_N_Object_Declaration (N);
304 when N_Object_Renaming_Declaration =>
305 Expand_N_Object_Renaming_Declaration (N);
307 when N_Op_Add =>
308 Expand_N_Op_Add (N);
310 when N_Op_Abs =>
311 Expand_N_Op_Abs (N);
313 when N_Op_And =>
314 Expand_N_Op_And (N);
316 when N_Op_Concat =>
317 Expand_N_Op_Concat (N);
319 when N_Op_Divide =>
320 Expand_N_Op_Divide (N);
322 when N_Op_Eq =>
323 Expand_N_Op_Eq (N);
325 when N_Op_Expon =>
326 Expand_N_Op_Expon (N);
328 when N_Op_Ge =>
329 Expand_N_Op_Ge (N);
331 when N_Op_Gt =>
332 Expand_N_Op_Gt (N);
334 when N_Op_Le =>
335 Expand_N_Op_Le (N);
337 when N_Op_Lt =>
338 Expand_N_Op_Lt (N);
340 when N_Op_Minus =>
341 Expand_N_Op_Minus (N);
343 when N_Op_Mod =>
344 Expand_N_Op_Mod (N);
346 when N_Op_Multiply =>
347 Expand_N_Op_Multiply (N);
349 when N_Op_Ne =>
350 Expand_N_Op_Ne (N);
352 when N_Op_Not =>
353 Expand_N_Op_Not (N);
355 when N_Op_Or =>
356 Expand_N_Op_Or (N);
358 when N_Op_Plus =>
359 Expand_N_Op_Plus (N);
361 when N_Op_Rem =>
362 Expand_N_Op_Rem (N);
364 when N_Op_Rotate_Left =>
365 Expand_N_Op_Rotate_Left (N);
367 when N_Op_Rotate_Right =>
368 Expand_N_Op_Rotate_Right (N);
370 when N_Op_Shift_Left =>
371 Expand_N_Op_Shift_Left (N);
373 when N_Op_Shift_Right =>
374 Expand_N_Op_Shift_Right (N);
376 when N_Op_Shift_Right_Arithmetic =>
377 Expand_N_Op_Shift_Right_Arithmetic (N);
379 when N_Op_Subtract =>
380 Expand_N_Op_Subtract (N);
382 when N_Op_Xor =>
383 Expand_N_Op_Xor (N);
385 when N_Or_Else =>
386 Expand_N_Or_Else (N);
388 when N_Package_Body =>
389 Expand_N_Package_Body (N);
391 when N_Package_Declaration =>
392 Expand_N_Package_Declaration (N);
394 when N_Package_Renaming_Declaration =>
395 Expand_N_Package_Renaming_Declaration (N);
397 when N_Subprogram_Renaming_Declaration =>
398 Expand_N_Subprogram_Renaming_Declaration (N);
400 when N_Pragma =>
401 Expand_N_Pragma (N);
403 when N_Procedure_Call_Statement =>
404 Expand_N_Procedure_Call_Statement (N);
406 when N_Protected_Type_Declaration =>
407 Expand_N_Protected_Type_Declaration (N);
409 when N_Protected_Body =>
410 Expand_N_Protected_Body (N);
412 when N_Qualified_Expression =>
413 Expand_N_Qualified_Expression (N);
415 when N_Quantified_Expression =>
416 Expand_N_Quantified_Expression (N);
418 when N_Raise_Statement =>
419 Expand_N_Raise_Statement (N);
421 when N_Raise_Constraint_Error =>
422 Expand_N_Raise_Constraint_Error (N);
424 when N_Raise_Expression =>
425 Expand_N_Raise_Expression (N);
427 when N_Raise_Program_Error =>
428 Expand_N_Raise_Program_Error (N);
430 when N_Raise_Storage_Error =>
431 Expand_N_Raise_Storage_Error (N);
433 when N_Real_Literal =>
434 Expand_N_Real_Literal (N);
436 when N_Record_Representation_Clause =>
437 Expand_N_Record_Representation_Clause (N);
439 when N_Requeue_Statement =>
440 Expand_N_Requeue_Statement (N);
442 when N_Simple_Return_Statement =>
443 Expand_N_Simple_Return_Statement (N);
445 when N_Selected_Component =>
446 Expand_N_Selected_Component (N);
448 when N_Selective_Accept =>
449 Expand_N_Selective_Accept (N);
451 when N_Single_Protected_Declaration =>
452 Expand_N_Single_Protected_Declaration (N);
454 when N_Single_Task_Declaration =>
455 Expand_N_Single_Task_Declaration (N);
457 when N_Slice =>
458 Expand_N_Slice (N);
460 when N_Subtype_Indication =>
461 Expand_N_Subtype_Indication (N);
463 when N_Subprogram_Body =>
464 Expand_N_Subprogram_Body (N);
466 when N_Subprogram_Body_Stub =>
467 Expand_N_Subprogram_Body_Stub (N);
469 when N_Subprogram_Declaration =>
470 Expand_N_Subprogram_Declaration (N);
472 when N_Task_Body =>
473 Expand_N_Task_Body (N);
475 when N_Task_Type_Declaration =>
476 Expand_N_Task_Type_Declaration (N);
478 when N_Timed_Entry_Call =>
479 Expand_N_Timed_Entry_Call (N);
481 when N_Type_Conversion =>
482 Expand_N_Type_Conversion (N);
484 when N_Unchecked_Expression =>
485 Expand_N_Unchecked_Expression (N);
487 when N_Unchecked_Type_Conversion =>
488 Expand_N_Unchecked_Type_Conversion (N);
490 when N_Variant_Part =>
491 Expand_N_Variant_Part (N);
493 -- For all other node kinds, no expansion activity required
495 when others =>
496 null;
497 end case;
499 exception
500 when RE_Not_Available =>
501 goto Leave;
502 end;
504 -- Set result as analyzed and then do a possible transient wrap. The
505 -- transient wrap must be done after the Analyzed flag is set on, so
506 -- that we do not get a recursive attempt to expand the node N.
508 Set_Analyzed (N);
510 -- Deal with transient scopes
512 if Scope_Is_Transient and then N = Node_To_Be_Wrapped then
513 case Nkind (N) is
514 when N_Procedure_Call_Statement
515 | N_Statement_Other_Than_Procedure_Call
517 Wrap_Transient_Statement (N);
519 when N_Object_Declaration
520 | N_Object_Renaming_Declaration
521 | N_Subtype_Declaration
523 Wrap_Transient_Declaration (N);
525 when others =>
526 Wrap_Transient_Expression (N);
527 end case;
528 end if;
530 Debug_A_Exit ("expanding ", N, " (done)");
531 end if;
533 <<Leave>>
534 Restore_Ghost_Region (Saved_GM, Saved_IGR);
535 end Expand;
537 ---------------------------
538 -- Expander_Mode_Restore --
539 ---------------------------
541 procedure Expander_Mode_Restore is
542 begin
543 -- Not active (has no effect) in ASIS and GNATprove modes (see comments
544 -- in spec of Expander_Mode_Save_And_Set).
546 if ASIS_Mode or GNATprove_Mode then
547 return;
548 end if;
550 -- Otherwise restore the flag
552 Expander_Active := Expander_Flags.Table (Expander_Flags.Last);
553 Expander_Flags.Decrement_Last;
555 -- Keep expander off if serious errors detected. In this case we do not
556 -- need expansion, and continued expansion may cause cascaded errors or
557 -- compiler bombs.
559 if Serious_Errors_Detected /= 0 then
560 Expander_Active := False;
561 end if;
562 end Expander_Mode_Restore;
564 --------------------------------
565 -- Expander_Mode_Save_And_Set --
566 --------------------------------
568 procedure Expander_Mode_Save_And_Set (Status : Boolean) is
569 begin
570 -- Not active (has no effect) in ASIS and GNATprove modes (see comments
571 -- in spec of Expander_Mode_Save_And_Set).
573 if ASIS_Mode or GNATprove_Mode then
574 return;
575 end if;
577 -- Otherwise save and set the flag
579 Expander_Flags.Increment_Last;
580 Expander_Flags.Table (Expander_Flags.Last) := Expander_Active;
581 Expander_Active := Status;
582 end Expander_Mode_Save_And_Set;
584 end Expander;