Debian Codegraph @

gcc/ada/expander.adb

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