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------------------------------------------------------------------------------
25
26with Atree;     use Atree;
27with Debug;     use Debug;
28with Debug_A;   use Debug_A;
29with Exp_Aggr;  use Exp_Aggr;
30with Exp_SPARK; use Exp_SPARK;
31with Exp_Attr;  use Exp_Attr;
32with Exp_Ch2;   use Exp_Ch2;
33with Exp_Ch3;   use Exp_Ch3;
34with Exp_Ch4;   use Exp_Ch4;
35with Exp_Ch5;   use Exp_Ch5;
36with Exp_Ch6;   use Exp_Ch6;
37with Exp_Ch7;   use Exp_Ch7;
38with Exp_Ch8;   use Exp_Ch8;
39with Exp_Ch9;   use Exp_Ch9;
40with Exp_Ch11;  use Exp_Ch11;
41with Exp_Ch12;  use Exp_Ch12;
42with Exp_Ch13;  use Exp_Ch13;
43with Exp_Prag;  use Exp_Prag;
44with Ghost;     use Ghost;
45with Opt;       use Opt;
46with Rtsfind;   use Rtsfind;
47with Sem;       use Sem;
48with Sem_Ch8;   use Sem_Ch8;
49with Sem_Util;  use Sem_Util;
50with Sinfo;     use Sinfo;
51with Table;
52
53package body Expander is
54
55   ----------------
56   -- Local Data --
57   ----------------
58
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.
63
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");
71
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.
75
76   ------------
77   -- Expand --
78   ------------
79
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.
83
84   procedure Expand (N : Node_Id) is
85      Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
86      --  Save the Ghost mode to restore on exit
87
88   begin
89      --  If we were analyzing a default expression (or other spec expression)
90      --  the Full_Analysis flag must be off. If we are in expansion mode then
91      --  we must be performing a full analysis. If we are analyzing a generic
92      --  then Expansion must be off.
93
94      pragma Assert
95        (not (Full_Analysis and then In_Spec_Expression)
96          and then (Full_Analysis or else not Expander_Active)
97          and then not (Inside_A_Generic and then Expander_Active));
98
99      --  Establish the Ghost mode of the context to ensure that any generated
100      --  nodes during expansion are marked as Ghost.
101
102      Set_Ghost_Mode (N);
103
104      --  The GNATprove_Mode flag indicates that a light expansion for formal
105      --  verification should be used. This expansion is never done inside
106      --  generics, because otherwise, this breaks the name resolution
107      --  mechanism for generic instances.
108
109      if GNATprove_Mode then
110         if not Inside_A_Generic then
111            Expand_SPARK (N);
112         end if;
113
114         Set_Analyzed (N, Full_Analysis);
115
116         --  Regular expansion is normally followed by special handling for
117         --  transient scopes for unconstrained results, etc. but this is not
118         --  needed, and in general cannot be done correctly, in this mode, so
119         --  we are all done.
120
121         goto Leave;
122
123      --  There are three reasons for the Expander_Active flag to be false
124
125      --  The first is when are not generating code. In this mode the
126      --  Full_Analysis flag indicates whether we are performing a complete
127      --  analysis, in which case Full_Analysis = True or a pre-analysis in
128      --  which case Full_Analysis = False. See the spec of Sem for more info
129      --  on this.
130
131      --  The second reason for the Expander_Active flag to be False is that
132      --  we are performing a pre-analysis. During pre-analysis all expansion
133      --  activity is turned off to make sure nodes are semantically decorated
134      --  but no extra nodes are generated. This is for instance needed for
135      --  the first pass of aggregate semantic processing. Note that in this
136      --  case the Full_Analysis flag is set to False because the node will
137      --  subsequently be re-analyzed with expansion on (see the spec of sem).
138
139      --  Finally, expansion is turned off in a regular compilation if there
140      --  are serious errors. In that case there will be no further expansion,
141      --  but one cleanup action may be required: if a transient scope was
142      --  created (e.g. for a function that returns an unconstrained type) the
143      --  scope may still be on the stack, and must be removed explicitly,
144      --  given that the expansion actions that would normally process it will
145      --  not take place. This prevents cascaded errors due to stack mismatch.
146
147      elsif not Expander_Active then
148         Set_Analyzed (N, Full_Analysis);
149
150         if Serious_Errors_Detected > 0 and then Scope_Is_Transient then
151            Scope_Stack.Table
152             (Scope_Stack.Last).Actions_To_Be_Wrapped := (others => No_List);
153            Pop_Scope;
154         end if;
155
156         goto Leave;
157
158      else
159         begin
160            Debug_A_Entry ("expanding  ", N);
161
162            --  Processing depends on node kind. For full details on the
163            --  expansion activity required in each case, see bodies of
164            --  corresponding expand routines.
165
166            case Nkind (N) is
167               when N_Abort_Statement =>
168                  Expand_N_Abort_Statement (N);
169
170                  --  If -gnatd.B switch was given, crash the compiler. See
171                  --  debug.adb for explanation.
172
173                  if Debug_Flag_Dot_BB then
174                     raise Abort_Bug_Box_Error;
175                  end if;
176
177               when N_Accept_Statement =>
178                  Expand_N_Accept_Statement (N);
179
180               when N_Aggregate =>
181                  Expand_N_Aggregate (N);
182
183               when N_Allocator =>
184                  Expand_N_Allocator (N);
185
186               when N_And_Then =>
187                  Expand_N_And_Then (N);
188
189               when N_Assignment_Statement =>
190                  Expand_N_Assignment_Statement (N);
191
192               when N_Asynchronous_Select =>
193                  Expand_N_Asynchronous_Select (N);
194
195               when N_Attribute_Definition_Clause =>
196                  Expand_N_Attribute_Definition_Clause (N);
197
198               when N_Attribute_Reference =>
199                  Expand_N_Attribute_Reference (N);
200
201               when N_Block_Statement =>
202                  Expand_N_Block_Statement (N);
203
204               when N_Case_Expression =>
205                  Expand_N_Case_Expression (N);
206
207               when N_Case_Statement =>
208                  Expand_N_Case_Statement (N);
209
210               when N_Conditional_Entry_Call =>
211                  Expand_N_Conditional_Entry_Call (N);
212
213               when N_Delay_Relative_Statement =>
214                  Expand_N_Delay_Relative_Statement (N);
215
216               when N_Delay_Until_Statement =>
217                  Expand_N_Delay_Until_Statement (N);
218
219               when N_Delta_Aggregate =>
220                  Expand_N_Delta_Aggregate (N);
221
222               when N_Entry_Body =>
223                  Expand_N_Entry_Body (N);
224
225               when N_Entry_Call_Statement =>
226                  Expand_N_Entry_Call_Statement (N);
227
228               when N_Entry_Declaration =>
229                  Expand_N_Entry_Declaration (N);
230
231               when N_Exception_Declaration =>
232                  Expand_N_Exception_Declaration (N);
233
234               when N_Exception_Renaming_Declaration =>
235                  Expand_N_Exception_Renaming_Declaration (N);
236
237               when N_Exit_Statement =>
238                  Expand_N_Exit_Statement (N);
239
240               when N_Expanded_Name =>
241                  Expand_N_Expanded_Name (N);
242
243               when N_Explicit_Dereference =>
244                  Expand_N_Explicit_Dereference (N);
245
246               when N_Expression_With_Actions =>
247                  Expand_N_Expression_With_Actions (N);
248
249               when N_Extended_Return_Statement =>
250                  Expand_N_Extended_Return_Statement (N);
251
252               when N_Extension_Aggregate =>
253                  Expand_N_Extension_Aggregate (N);
254
255               when N_Free_Statement =>
256                  Expand_N_Free_Statement (N);
257
258               when N_Freeze_Entity =>
259                  Expand_N_Freeze_Entity (N);
260
261               when N_Full_Type_Declaration =>
262                  Expand_N_Full_Type_Declaration (N);
263
264               when N_Function_Call =>
265                  Expand_N_Function_Call (N);
266
267               when N_Generic_Instantiation =>
268                  Expand_N_Generic_Instantiation (N);
269
270               when N_Goto_Statement =>
271                  Expand_N_Goto_Statement (N);
272
273               when N_Handled_Sequence_Of_Statements =>
274                  Expand_N_Handled_Sequence_Of_Statements (N);
275
276               when N_Identifier =>
277                  Expand_N_Identifier (N);
278
279               when N_If_Expression =>
280                  Expand_N_If_Expression (N);
281
282               when N_Indexed_Component =>
283                  Expand_N_Indexed_Component (N);
284
285               when N_If_Statement =>
286                  Expand_N_If_Statement (N);
287
288               when N_In =>
289                  Expand_N_In (N);
290
291               when N_Loop_Statement =>
292                  Expand_N_Loop_Statement (N);
293
294               when N_Not_In =>
295                  Expand_N_Not_In (N);
296
297               when N_Null =>
298                  Expand_N_Null (N);
299
300               when N_Object_Declaration =>
301                  Expand_N_Object_Declaration (N);
302
303               when N_Object_Renaming_Declaration =>
304                  Expand_N_Object_Renaming_Declaration (N);
305
306               when N_Op_Add =>
307                  Expand_N_Op_Add (N);
308
309               when N_Op_Abs =>
310                  Expand_N_Op_Abs (N);
311
312               when N_Op_And =>
313                  Expand_N_Op_And (N);
314
315               when N_Op_Concat =>
316                  Expand_N_Op_Concat (N);
317
318               when N_Op_Divide =>
319                  Expand_N_Op_Divide (N);
320
321               when N_Op_Eq =>
322                  Expand_N_Op_Eq (N);
323
324               when N_Op_Expon =>
325                  Expand_N_Op_Expon (N);
326
327               when N_Op_Ge =>
328                  Expand_N_Op_Ge (N);
329
330               when N_Op_Gt =>
331                  Expand_N_Op_Gt (N);
332
333               when N_Op_Le =>
334                  Expand_N_Op_Le (N);
335
336               when N_Op_Lt =>
337                  Expand_N_Op_Lt (N);
338
339               when N_Op_Minus =>
340                  Expand_N_Op_Minus (N);
341
342               when N_Op_Mod =>
343                  Expand_N_Op_Mod (N);
344
345               when N_Op_Multiply =>
346                  Expand_N_Op_Multiply (N);
347
348               when N_Op_Ne =>
349                  Expand_N_Op_Ne (N);
350
351               when N_Op_Not =>
352                  Expand_N_Op_Not (N);
353
354               when N_Op_Or =>
355                  Expand_N_Op_Or (N);
356
357               when N_Op_Plus =>
358                  Expand_N_Op_Plus (N);
359
360               when N_Op_Rem =>
361                  Expand_N_Op_Rem (N);
362
363               when N_Op_Rotate_Left =>
364                  Expand_N_Op_Rotate_Left (N);
365
366               when N_Op_Rotate_Right =>
367                  Expand_N_Op_Rotate_Right (N);
368
369               when N_Op_Shift_Left =>
370                  Expand_N_Op_Shift_Left (N);
371
372               when N_Op_Shift_Right =>
373                  Expand_N_Op_Shift_Right (N);
374
375               when N_Op_Shift_Right_Arithmetic =>
376                  Expand_N_Op_Shift_Right_Arithmetic (N);
377
378               when N_Op_Subtract =>
379                  Expand_N_Op_Subtract (N);
380
381               when N_Op_Xor =>
382                  Expand_N_Op_Xor (N);
383
384               when N_Or_Else =>
385                  Expand_N_Or_Else (N);
386
387               when N_Package_Body =>
388                  Expand_N_Package_Body (N);
389
390               when N_Package_Declaration =>
391                  Expand_N_Package_Declaration (N);
392
393               when N_Package_Renaming_Declaration =>
394                  Expand_N_Package_Renaming_Declaration (N);
395
396               when N_Subprogram_Renaming_Declaration =>
397                  Expand_N_Subprogram_Renaming_Declaration (N);
398
399               when N_Pragma =>
400                  Expand_N_Pragma (N);
401
402               when N_Procedure_Call_Statement =>
403                  Expand_N_Procedure_Call_Statement (N);
404
405               when N_Protected_Type_Declaration =>
406                  Expand_N_Protected_Type_Declaration (N);
407
408               when N_Protected_Body =>
409                  Expand_N_Protected_Body (N);
410
411               when N_Qualified_Expression =>
412                  Expand_N_Qualified_Expression (N);
413
414               when N_Quantified_Expression  =>
415                  Expand_N_Quantified_Expression (N);
416
417               when N_Raise_Statement =>
418                  Expand_N_Raise_Statement (N);
419
420               when N_Raise_Constraint_Error =>
421                  Expand_N_Raise_Constraint_Error (N);
422
423               when N_Raise_Expression =>
424                  Expand_N_Raise_Expression (N);
425
426               when N_Raise_Program_Error =>
427                  Expand_N_Raise_Program_Error (N);
428
429               when N_Raise_Storage_Error =>
430                  Expand_N_Raise_Storage_Error (N);
431
432               when N_Real_Literal =>
433                  Expand_N_Real_Literal (N);
434
435               when N_Record_Representation_Clause =>
436                  Expand_N_Record_Representation_Clause (N);
437
438               when N_Reduction_Expression =>
439                  Expand_N_Reduction_Expression (N);
440
441               when N_Requeue_Statement =>
442                  Expand_N_Requeue_Statement (N);
443
444               when N_Simple_Return_Statement =>
445                  Expand_N_Simple_Return_Statement (N);
446
447               when N_Selected_Component =>
448                  Expand_N_Selected_Component (N);
449
450               when N_Selective_Accept =>
451                  Expand_N_Selective_Accept (N);
452
453               when N_Single_Protected_Declaration =>
454                  Expand_N_Single_Protected_Declaration (N);
455
456               when N_Single_Task_Declaration =>
457                  Expand_N_Single_Task_Declaration (N);
458
459               when N_Slice =>
460                  Expand_N_Slice (N);
461
462               when N_Subtype_Indication =>
463                  Expand_N_Subtype_Indication (N);
464
465               when N_Subprogram_Body =>
466                  Expand_N_Subprogram_Body (N);
467
468               when N_Subprogram_Body_Stub =>
469                  Expand_N_Subprogram_Body_Stub (N);
470
471               when N_Subprogram_Declaration =>
472                  Expand_N_Subprogram_Declaration (N);
473
474               when N_Task_Body =>
475                  Expand_N_Task_Body (N);
476
477               when N_Task_Type_Declaration =>
478                  Expand_N_Task_Type_Declaration (N);
479
480               when N_Timed_Entry_Call =>
481                  Expand_N_Timed_Entry_Call (N);
482
483               when N_Type_Conversion =>
484                  Expand_N_Type_Conversion (N);
485
486               when N_Unchecked_Expression =>
487                  Expand_N_Unchecked_Expression (N);
488
489               when N_Unchecked_Type_Conversion =>
490                  Expand_N_Unchecked_Type_Conversion (N);
491
492               when N_Variant_Part =>
493                  Expand_N_Variant_Part (N);
494
495               --  For all other node kinds, no expansion activity required
496
497               when others =>
498                  null;
499            end case;
500
501         exception
502            when RE_Not_Available =>
503               goto Leave;
504         end;
505
506         --  Set result as analyzed and then do a possible transient wrap. The
507         --  transient wrap must be done after the Analyzed flag is set on, so
508         --  that we do not get a recursive attempt to expand the node N.
509
510         Set_Analyzed (N);
511
512         --  Deal with transient scopes
513
514         if Scope_Is_Transient and then N = Node_To_Be_Wrapped then
515            case Nkind (N) is
516               when N_Procedure_Call_Statement
517                  | N_Statement_Other_Than_Procedure_Call
518               =>
519                  Wrap_Transient_Statement (N);
520
521               when N_Object_Declaration
522                  | N_Object_Renaming_Declaration
523                  | N_Subtype_Declaration
524               =>
525                  Wrap_Transient_Declaration (N);
526
527               when others =>
528                  Wrap_Transient_Expression (N);
529            end case;
530         end if;
531
532         Debug_A_Exit ("expanding  ", N, "  (done)");
533      end if;
534
535   <<Leave>>
536      Restore_Ghost_Mode (Saved_GM);
537   end Expand;
538
539   ---------------------------
540   -- Expander_Mode_Restore --
541   ---------------------------
542
543   procedure Expander_Mode_Restore is
544   begin
545      --  Not active (has no effect) in ASIS and GNATprove modes (see comments
546      --  in spec of Expander_Mode_Save_And_Set).
547
548      if ASIS_Mode or GNATprove_Mode then
549         return;
550      end if;
551
552      --  Otherwise restore the flag
553
554      Expander_Active := Expander_Flags.Table (Expander_Flags.Last);
555      Expander_Flags.Decrement_Last;
556
557      --  Keep expander off if serious errors detected. In this case we do not
558      --  need expansion, and continued expansion may cause cascaded errors or
559      --  compiler bombs.
560
561      if Serious_Errors_Detected /= 0 then
562         Expander_Active := False;
563      end if;
564   end Expander_Mode_Restore;
565
566   --------------------------------
567   -- Expander_Mode_Save_And_Set --
568   --------------------------------
569
570   procedure Expander_Mode_Save_And_Set (Status : Boolean) is
571   begin
572      --  Not active (has no effect) in ASIS and GNATprove modes (see comments
573      --  in spec of Expander_Mode_Save_And_Set).
574
575      if ASIS_Mode or GNATprove_Mode then
576         return;
577      end if;
578
579      --  Otherwise save and set the flag
580
581      Expander_Flags.Increment_Last;
582      Expander_Flags.Table (Expander_Flags.Last) := Expander_Active;
583      Expander_Active := Status;
584   end Expander_Mode_Save_And_Set;
585
586end Expander;
587