Skip several gcc.dg/builtin-dynamic-object-size tests on hppa*-*-hpux*
[official-gcc.git] / gcc / ada / exp_ch11.adb
blob53f0753cdce90de72db12153cd0c32cecdf41066
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 1 1 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, 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 Einfo; use Einfo;
29 with Einfo.Entities; use Einfo.Entities;
30 with Einfo.Utils; use Einfo.Utils;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Exp_Ch7; use Exp_Ch7;
34 with Exp_Intr; use Exp_Intr;
35 with Exp_Util; use Exp_Util;
36 with Namet; use Namet;
37 with Nlists; use Nlists;
38 with Nmake; use Nmake;
39 with Opt; use Opt;
40 with Restrict; use Restrict;
41 with Rident; use Rident;
42 with Rtsfind; use Rtsfind;
43 with Sem; use Sem;
44 with Sem_Res; use Sem_Res;
45 with Sem_Util; use Sem_Util;
46 with Sinfo; use Sinfo;
47 with Sinfo.Nodes; use Sinfo.Nodes;
48 with Sinfo.Utils; use Sinfo.Utils;
49 with Sinput; use Sinput;
50 with Snames; use Snames;
51 with Stand; use Stand;
52 with Stringt; use Stringt;
53 with Targparm; use Targparm;
54 with Tbuild; use Tbuild;
55 with Uintp; use Uintp;
56 with Warnsw; use Warnsw;
58 package body Exp_Ch11 is
60 -----------------------
61 -- Local Subprograms --
62 -----------------------
64 procedure Warn_No_Exception_Propagation_Active (N : Node_Id);
65 -- Generates warning that pragma Restrictions (No_Exception_Propagation)
66 -- is in effect. Caller then generates appropriate continuation message.
67 -- N is the node on which the warning is placed.
69 procedure Warn_If_No_Propagation (N : Node_Id);
70 -- Called for an exception raise that is not a local raise (and thus cannot
71 -- be optimized to a goto). Issues warning if No_Exception_Propagation
72 -- restriction is set. N is the node for the raise or equivalent call.
74 ---------------------------
75 -- Expand_At_End_Handler --
76 ---------------------------
78 -- For a handled statement sequence that has a cleanup (At_End_Proc
79 -- field set), perform any needed expansion.
81 -- Do nothing by default. We used to perform a special expansion for
82 -- front-end SJLJ, and we may want to customize this processing in
83 -- the future for new back-ends.
85 procedure Expand_At_End_Handler (HSS : Node_Id; Blk_Id : Entity_Id) is
86 pragma Unreferenced (Blk_Id);
87 begin
88 pragma Assert (Present (Entity (At_End_Proc (HSS))));
89 end Expand_At_End_Handler;
91 -------------------------------
92 -- Expand_Exception_Handlers --
93 -------------------------------
95 procedure Expand_Exception_Handlers (HSS : Node_Id) is
96 Handlrs : constant List_Id := Exception_Handlers (HSS);
97 Loc : constant Source_Ptr := Sloc (HSS);
98 Handler : Node_Id;
99 Obj_Decl : Node_Id;
100 Next_Handler : Node_Id;
102 procedure Expand_Local_Exception_Handlers;
103 -- This procedure handles the expansion of exception handlers for the
104 -- optimization of local raise statements into goto statements.
106 procedure Replace_Raise_By_Goto (Raise_S : Node_Id; Goto_L1 : Node_Id);
107 -- Raise_S is a raise statement (possibly expanded, and possibly of the
108 -- form of a Raise_xxx_Error node with a condition. This procedure is
109 -- called to replace the raise action with the (already analyzed) goto
110 -- statement passed as Goto_L1. This procedure also takes care of the
111 -- requirement of inserting a Local_Raise call where possible.
113 -------------------------------------
114 -- Expand_Local_Exception_Handlers --
115 -------------------------------------
117 -- There are two cases for this transformation. First the case of
118 -- explicit raise statements. For this case, the transformation we do
119 -- looks like this. Right now we have for example (where L1, L2 are
120 -- exception labels)
122 -- begin
123 -- ...
124 -- raise_exception (excep1'identity); -- was raise excep1
125 -- ...
126 -- raise_exception (excep2'identity); -- was raise excep2
127 -- ...
128 -- exception
129 -- when excep1 =>
130 -- estmts1
131 -- when excep2 =>
132 -- estmts2
133 -- end;
135 -- This gets transformed into:
137 -- begin
138 -- L1 : label; -- marked Exception_Junk
139 -- L2 : label; -- marked Exception_Junk
140 -- L3 : label; -- marked Exception_Junk
142 -- begin -- marked Exception_Junk
143 -- ...
144 -- local_raise (excep1'address); -- was raise excep1
145 -- goto L1;
146 -- ...
147 -- local_raise (excep2'address); -- was raise excep2
148 -- goto L2;
149 -- ...
150 -- exception
151 -- when excep1 =>
152 -- goto L1;
153 -- when excep2 =>
154 -- goto L2;
155 -- end;
157 -- goto L3; -- skip handler if no raise, marked Exception_Junk
159 -- <<L1>> -- local excep target label, marked Exception_Junk
160 -- begin -- marked Exception_Junk
161 -- estmts1
162 -- end;
163 -- goto L3; -- marked Exception_Junk
165 -- <<L2>> -- marked Exception_Junk
166 -- begin -- marked Exception_Junk
167 -- estmts2
168 -- end;
169 -- goto L3; -- marked Exception_Junk
170 -- <<L3>> -- marked Exception_Junk
171 -- end;
173 -- Note: the reason we wrap the original statement sequence in an
174 -- inner block is that there may be raise statements within the
175 -- sequence of statements in the handlers, and we must ensure that
176 -- these are properly handled, and in particular, such raise statements
177 -- must not reenter the same exception handlers.
179 -- If the restriction No_Exception_Propagation is in effect, then we
180 -- can omit the exception handlers.
182 -- begin
183 -- L1 : label; -- marked Exception_Junk
184 -- L2 : label; -- marked Exception_Junk
185 -- L3 : label; -- marked Exception_Junk
187 -- begin -- marked Exception_Junk
188 -- ...
189 -- local_raise (excep1'address); -- was raise excep1
190 -- goto L1;
191 -- ...
192 -- local_raise (excep2'address); -- was raise excep2
193 -- goto L2;
194 -- ...
195 -- end;
197 -- goto L3; -- skip handler if no raise, marked Exception_Junk
199 -- <<L1>> -- local excep target label, marked Exception_Junk
200 -- begin -- marked Exception_Junk
201 -- estmts1
202 -- end;
203 -- goto L3; -- marked Exception_Junk
205 -- <<L2>> -- marked Exception_Junk
206 -- begin -- marked Exception_Junk
207 -- estmts2
208 -- end;
210 -- <<L3>> -- marked Exception_Junk
211 -- end;
213 -- The second case is for exceptions generated by the back end in one
214 -- of three situations:
216 -- 1. Front end generates N_Raise_xxx_Error node
217 -- 2. Front end sets Do_xxx_Check flag in subexpression node
218 -- 3. Back end detects a situation where an exception is appropriate
220 -- In all these cases, the current processing in gigi is to generate a
221 -- call to the appropriate Rcheck_xx routine (where xx encodes both the
222 -- exception message and the exception to be raised, Constraint_Error,
223 -- Program_Error, or Storage_Error.
225 -- We could handle some subcases of 1 using the same front end expansion
226 -- into gotos, but even for case 1, we can't handle all cases, since
227 -- generating gotos in the middle of expressions is not possible (it's
228 -- possible at the gigi/gcc level, but not at the level of the GNAT
229 -- tree).
231 -- In any case, it seems easier to have a scheme which handles all three
232 -- cases in a uniform manner. So here is how we proceed in this case.
234 -- This procedure detects all handlers for these three exceptions,
235 -- Constraint_Error, Program_Error and Storage_Error (including WHEN
236 -- OTHERS handlers that cover one or more of these cases).
238 -- If the handler meets the requirements for being the target of a local
239 -- raise, then the front end does the expansion described previously,
240 -- creating a label to be used as a goto target to raise the exception.
241 -- However, no attempt is made in the front end to convert any related
242 -- raise statements into gotos, e.g. all N_Raise_xxx_Error nodes are
243 -- left unchanged and passed to the back end.
245 -- Instead, the front end generates three nodes
247 -- N_Push_Constraint_Error_Label
248 -- N_Push_Program_Error_Label
249 -- N_Push_Storage_Error_Label
251 -- The Push node is generated at the start of the statements
252 -- covered by the handler, and has as a parameter the label to be
253 -- used as the raise target.
255 -- N_Pop_Constraint_Error_Label
256 -- N_Pop_Program_Error_Label
257 -- N_Pop_Storage_Error_Label
259 -- The Pop node is generated at the end of the covered statements
260 -- and undoes the effect of the preceding corresponding Push node.
262 -- In the case where the handler does NOT meet the requirements, the
263 -- front end will still generate the Push and Pop nodes, but the label
264 -- field in the Push node will be empty signifying that for this region
265 -- of code, no optimization is possible.
267 -- These Push/Pop nodes are inhibited if No_Exception_Handlers is set
268 -- since they are useless in this case, and in CodePeer mode, where
269 -- they serve no purpose and can intefere with the analysis.
271 -- The back end must maintain three stacks, one for each exception case,
272 -- the Push node pushes an entry onto the corresponding stack, and Pop
273 -- node pops off the entry. Then instead of calling Rcheck_nn, if the
274 -- corresponding top stack entry has an non-empty label, a goto is
275 -- generated. This goto should be preceded by a call to Local_Raise as
276 -- described above.
278 -- An example of this transformation is as follows, given:
280 -- declare
281 -- A : Integer range 1 .. 10;
282 -- begin
283 -- A := B + C;
284 -- exception
285 -- when Constraint_Error =>
286 -- estmts
287 -- end;
289 -- gets transformed to:
291 -- declare
292 -- A : Integer range 1 .. 10;
294 -- begin
295 -- L1 : label;
296 -- L2 : label;
298 -- begin
299 -- %push_constraint_error_label (L1)
300 -- R1b : constant long_long_integer := long_long_integer?(b) +
301 -- long_long_integer?(c);
302 -- [constraint_error when
303 -- not (R1b in -16#8000_0000# .. 16#7FFF_FFFF#)
304 -- "overflow check failed"]
305 -- a := integer?(R1b);
306 -- %pop_constraint_error_Label
308 -- exception
309 -- ...
310 -- when constraint_error =>
311 -- goto L1;
312 -- end;
314 -- goto L2; -- skip handler when exception not raised
315 -- <<L1>> -- target label for local exception
316 -- estmts
317 -- <<L2>>
318 -- end;
320 -- Note: the generated labels and goto statements all have the flag
321 -- Exception_Junk set True, so that Sem_Ch6.Check_Returns will ignore
322 -- this generated exception stuff when checking for missing return
323 -- statements (see circuitry in Check_Statement_Sequence).
325 -- Note: All of the processing described above occurs only if
326 -- restriction No_Exception_Propagation applies or debug flag .g is
327 -- enabled.
329 CE_Locally_Handled : Boolean := False;
330 SE_Locally_Handled : Boolean := False;
331 PE_Locally_Handled : Boolean := False;
332 -- These three flags indicate whether a handler for the corresponding
333 -- exception (CE=Constraint_Error, SE=Storage_Error, PE=Program_Error)
334 -- is present. If so the switch is set to True, the Exception_Label
335 -- field of the corresponding handler is set, and appropriate Push
336 -- and Pop nodes are inserted into the code.
338 Local_Expansion_Required : Boolean := False;
339 -- Set True if we have at least one handler requiring local raise
340 -- expansion as described above.
342 procedure Expand_Local_Exception_Handlers is
343 procedure Add_Exception_Label (H : Node_Id);
344 -- H is an exception handler. First check for an Exception_Label
345 -- already allocated for H. If none, allocate one, set the field in
346 -- the handler node, add the label declaration, and set the flag
347 -- Local_Expansion_Required. Note: if Local_Raise_Not_OK is set
348 -- the call has no effect and Exception_Label is left empty.
350 procedure Add_Label_Declaration (L : Entity_Id);
351 -- Add an implicit declaration of the given label to the declaration
352 -- list in the parent of the current sequence of handled statements.
354 generic
355 Exc_Locally_Handled : in out Boolean;
356 -- Flag indicating whether a local handler for this exception
357 -- has already been generated.
359 with function Make_Push_Label (Loc : Source_Ptr) return Node_Id;
360 -- Function to create a Push_xxx_Label node
362 with function Make_Pop_Label (Loc : Source_Ptr) return Node_Id;
363 -- Function to create a Pop_xxx_Label node
365 procedure Generate_Push_Pop (H : Node_Id);
366 -- Common code for Generate_Push_Pop_xxx below, used to generate an
367 -- exception label and Push/Pop nodes for Constraint_Error,
368 -- Program_Error, or Storage_Error.
370 -------------------------
371 -- Add_Exception_Label --
372 -------------------------
374 procedure Add_Exception_Label (H : Node_Id) is
375 begin
376 if No (Exception_Label (H))
377 and then not Local_Raise_Not_OK (H)
378 and then not Special_Exception_Package_Used
379 then
380 Local_Expansion_Required := True;
382 declare
383 L : constant Entity_Id := Make_Temporary (Sloc (H), 'L');
384 begin
385 Set_Exception_Label (H, L);
386 Add_Label_Declaration (L);
387 end;
388 end if;
389 end Add_Exception_Label;
391 ---------------------------
392 -- Add_Label_Declaration --
393 ---------------------------
395 procedure Add_Label_Declaration (L : Entity_Id) is
396 P : constant Node_Id := Parent (HSS);
398 Decl_L : constant Node_Id :=
399 Make_Implicit_Label_Declaration (Loc,
400 Defining_Identifier => L);
402 begin
403 if Declarations (P) = No_List then
404 Set_Declarations (P, Empty_List);
405 end if;
407 Append (Decl_L, Declarations (P));
408 Analyze (Decl_L);
409 end Add_Label_Declaration;
411 -----------------------
412 -- Generate_Push_Pop --
413 -----------------------
415 procedure Generate_Push_Pop (H : Node_Id) is
416 begin
417 if Restriction_Active (No_Exception_Handlers)
418 or else CodePeer_Mode
419 then
420 return;
421 end if;
423 if Exc_Locally_Handled then
424 return;
425 else
426 Exc_Locally_Handled := True;
427 end if;
429 Add_Exception_Label (H);
431 declare
432 F : constant Node_Id := First (Statements (HSS));
433 L : constant Node_Id := Last (Statements (HSS));
435 Push : constant Node_Id := Make_Push_Label (Sloc (F));
436 Pop : constant Node_Id := Make_Pop_Label (Sloc (L));
438 begin
439 -- We make sure that a call to Get_Local_Raise_Call_Entity is
440 -- made during front end processing, so that when we need it
441 -- in the back end, it will already be available and loaded.
443 Discard_Node (Get_Local_Raise_Call_Entity);
445 -- Prepare and insert Push and Pop nodes
447 Set_Exception_Label (Push, Exception_Label (H));
448 Insert_Before (F, Push);
449 Set_Analyzed (Push);
451 Insert_After (L, Pop);
452 Set_Analyzed (Pop);
453 end;
454 end Generate_Push_Pop;
456 -- Local declarations
458 Loc : constant Source_Ptr := Sloc (HSS);
459 Stmts : List_Id := No_List;
460 Choice : Node_Id;
461 Excep : Entity_Id;
463 procedure Generate_Push_Pop_For_Constraint_Error is
464 new Generate_Push_Pop
465 (Exc_Locally_Handled => CE_Locally_Handled,
466 Make_Push_Label => Make_Push_Constraint_Error_Label,
467 Make_Pop_Label => Make_Pop_Constraint_Error_Label);
468 -- If no Push/Pop has been generated for CE yet, then set the flag
469 -- CE_Locally_Handled, allocate an Exception_Label for handler H (if
470 -- not already done), and generate Push/Pop nodes for the exception
471 -- label at the start and end of the statements of HSS.
473 procedure Generate_Push_Pop_For_Program_Error is
474 new Generate_Push_Pop
475 (Exc_Locally_Handled => PE_Locally_Handled,
476 Make_Push_Label => Make_Push_Program_Error_Label,
477 Make_Pop_Label => Make_Pop_Program_Error_Label);
478 -- If no Push/Pop has been generated for PE yet, then set the flag
479 -- PE_Locally_Handled, allocate an Exception_Label for handler H (if
480 -- not already done), and generate Push/Pop nodes for the exception
481 -- label at the start and end of the statements of HSS.
483 procedure Generate_Push_Pop_For_Storage_Error is
484 new Generate_Push_Pop
485 (Exc_Locally_Handled => SE_Locally_Handled,
486 Make_Push_Label => Make_Push_Storage_Error_Label,
487 Make_Pop_Label => Make_Pop_Storage_Error_Label);
488 -- If no Push/Pop has been generated for SE yet, then set the flag
489 -- SE_Locally_Handled, allocate an Exception_Label for handler H (if
490 -- not already done), and generate Push/Pop nodes for the exception
491 -- label at the start and end of the statements of HSS.
493 -- Start of processing for Expand_Local_Exception_Handlers
495 begin
496 -- No processing if all exception handlers will get removed
498 if Debug_Flag_Dot_X then
499 return;
500 end if;
502 -- See for each handler if we have any local raises to expand
504 Handler := First_Non_Pragma (Handlrs);
505 while Present (Handler) loop
507 -- Note, we do not test Local_Raise_Not_OK here, because in the
508 -- case of Push/Pop generation we want to generate push with a
509 -- null label. The Add_Exception_Label routine has no effect if
510 -- Local_Raise_Not_OK is set, so this works as required.
512 if Present (Local_Raise_Statements (Handler)) then
513 Add_Exception_Label (Handler);
514 end if;
516 -- If we are doing local raise to goto optimization (restriction
517 -- No_Exception_Propagation set or debug flag .g set), then check
518 -- to see if handler handles CE, PE, SE and if so generate the
519 -- appropriate push/pop sequence for the back end.
521 if (Debug_Flag_Dot_G
522 or else Restriction_Active (No_Exception_Propagation))
523 and then Has_Local_Raise (Handler)
524 then
525 Choice := First (Exception_Choices (Handler));
526 while Present (Choice) loop
527 if Nkind (Choice) = N_Others_Choice
528 and then not All_Others (Choice)
529 then
530 Generate_Push_Pop_For_Constraint_Error (Handler);
531 Generate_Push_Pop_For_Program_Error (Handler);
532 Generate_Push_Pop_For_Storage_Error (Handler);
534 elsif Is_Entity_Name (Choice) then
535 Excep := Get_Renamed_Entity (Entity (Choice));
537 if Excep = Standard_Constraint_Error then
538 Generate_Push_Pop_For_Constraint_Error (Handler);
539 elsif Excep = Standard_Program_Error then
540 Generate_Push_Pop_For_Program_Error (Handler);
541 elsif Excep = Standard_Storage_Error then
542 Generate_Push_Pop_For_Storage_Error (Handler);
543 end if;
544 end if;
546 Next (Choice);
547 end loop;
548 end if;
550 Next_Non_Pragma (Handler);
551 end loop;
553 -- Nothing to do if no handlers requiring the goto transformation
555 if not (Local_Expansion_Required) then
556 return;
557 end if;
559 -- Prepare to do the transformation
561 declare
562 -- L3 is the label to exit the HSS
564 L3_Dent : constant Entity_Id := Make_Temporary (Loc, 'L');
566 Labl_L3 : constant Node_Id :=
567 Make_Label (Loc,
568 Identifier => New_Occurrence_Of (L3_Dent, Loc));
570 Blk_Stm : Node_Id;
571 Relmt : Elmt_Id;
573 begin
574 Set_Exception_Junk (Labl_L3);
575 Add_Label_Declaration (L3_Dent);
577 -- Wrap existing statements and handlers in an inner block
579 Blk_Stm :=
580 Make_Block_Statement (Loc,
581 Handled_Statement_Sequence => Relocate_Node (HSS));
582 Set_Exception_Junk (Blk_Stm);
584 Rewrite (HSS,
585 Make_Handled_Sequence_Of_Statements (Loc,
586 Statements => New_List (Blk_Stm),
587 End_Label => Relocate_Node (End_Label (HSS))));
589 -- Set block statement as analyzed, we don't want to actually call
590 -- Analyze on this block, it would cause a recursion in exception
591 -- handler processing which would mess things up.
593 Set_Analyzed (Blk_Stm);
595 -- Now loop through the exception handlers to deal with those that
596 -- are targets of local raise statements.
598 Handler := First_Non_Pragma (Handlrs);
599 while Present (Handler) loop
600 if Present (Exception_Label (Handler)) then
602 -- This handler needs the goto expansion
604 declare
605 Loc : constant Source_Ptr := Sloc (Handler);
607 -- L1 is the start label for this handler
609 L1_Dent : constant Entity_Id := Exception_Label (Handler);
611 Labl_L1 : constant Node_Id :=
612 Make_Label (Loc,
613 Identifier =>
614 New_Occurrence_Of (L1_Dent, Loc));
616 -- Jump to L1 to be used as replacement for the original
617 -- handler (used in the case where exception propagation
618 -- may still occur).
620 Name_L1 : constant Node_Id :=
621 New_Occurrence_Of (L1_Dent, Loc);
623 Goto_L1 : constant Node_Id :=
624 Make_Goto_Statement (Loc,
625 Name => Name_L1);
627 -- Jump to L3 to be used at the end of handler
629 Name_L3 : constant Node_Id :=
630 New_Occurrence_Of (L3_Dent, Loc);
632 Goto_L3 : constant Node_Id :=
633 Make_Goto_Statement (Loc,
634 Name => Name_L3);
636 H_Stmts : constant List_Id := Statements (Handler);
638 begin
639 Set_Exception_Junk (Labl_L1);
640 Set_Exception_Junk (Goto_L3);
642 -- Note: we do NOT set Exception_Junk in Goto_L1, since
643 -- this is a real transfer of control that we want the
644 -- Sem_Ch6.Check_Returns procedure to recognize properly.
646 -- Replace handler by a goto L1. We can mark this as
647 -- analyzed since it is fully formed, and we don't
648 -- want it going through any further checks. We save
649 -- the last statement location in the goto L1 node for
650 -- the benefit of Sem_Ch6.Check_Returns.
652 Set_Statements (Handler, New_List (Goto_L1));
653 Set_Analyzed (Goto_L1);
654 Set_Etype (Name_L1, Standard_Void_Type);
656 -- Now replace all the raise statements by goto L1
658 if Present (Local_Raise_Statements (Handler)) then
659 Relmt := First_Elmt (Local_Raise_Statements (Handler));
660 while Present (Relmt) loop
661 declare
662 Raise_S : constant Node_Id := Node (Relmt);
663 RLoc : constant Source_Ptr := Sloc (Raise_S);
664 Name_L1 : constant Node_Id :=
665 New_Occurrence_Of (L1_Dent, Loc);
666 Goto_L1 : constant Node_Id :=
667 Make_Goto_Statement (RLoc,
668 Name => Name_L1);
670 begin
671 -- Replace raise by goto L1
673 Set_Analyzed (Goto_L1);
674 Set_Etype (Name_L1, Standard_Void_Type);
675 Replace_Raise_By_Goto (Raise_S, Goto_L1);
676 end;
678 Next_Elmt (Relmt);
679 end loop;
680 end if;
682 -- Add a goto L3 at end of statement list in block. The
683 -- first time, this is what skips over the exception
684 -- handlers in the normal case. Subsequent times, it
685 -- terminates the execution of the previous handler code,
686 -- and skips subsequent handlers.
688 Stmts := Statements (HSS);
690 Insert_After (Last (Stmts), Goto_L3);
691 Set_Analyzed (Goto_L3);
692 Set_Etype (Name_L3, Standard_Void_Type);
694 -- Now we drop the label that marks the handler start,
695 -- followed by the statements of the handler.
697 Set_Etype (Identifier (Labl_L1), Standard_Void_Type);
699 Insert_After_And_Analyze (Last (Stmts), Labl_L1);
701 declare
702 Loc : constant Source_Ptr := Sloc (First (H_Stmts));
703 Blk : constant Node_Id :=
704 Make_Block_Statement (Loc,
705 Handled_Statement_Sequence =>
706 Make_Handled_Sequence_Of_Statements (Loc,
707 Statements => H_Stmts));
708 begin
709 Set_Exception_Junk (Blk);
710 Insert_After_And_Analyze (Last (Stmts), Blk);
711 end;
712 end;
714 -- Here if we have local raise statements but the handler is
715 -- not suitable for processing with a local raise. In this
716 -- case we have to generate possible diagnostics.
718 elsif Has_Local_Raise (Handler)
719 and then Present (Local_Raise_Statements (Handler))
720 then
721 Relmt := First_Elmt (Local_Raise_Statements (Handler));
722 while Present (Relmt) loop
723 Warn_If_No_Propagation (Node (Relmt));
724 Next_Elmt (Relmt);
725 end loop;
726 end if;
728 Next (Handler);
729 end loop;
731 -- Only remaining step is to drop the L3 label and we are done
733 Set_Etype (Identifier (Labl_L3), Standard_Void_Type);
735 -- If we had at least one handler, then we drop the label after
736 -- the last statement of that handler.
738 if Stmts /= No_List then
739 Insert_After_And_Analyze (Last (Stmts), Labl_L3);
741 -- Otherwise we have removed all the handlers (this results from
742 -- use of pragma Restrictions (No_Exception_Propagation), and we
743 -- drop the label at the end of the statements of the HSS.
745 else
746 Insert_After_And_Analyze (Last (Statements (HSS)), Labl_L3);
747 end if;
749 return;
750 end;
751 end Expand_Local_Exception_Handlers;
753 ---------------------------
754 -- Replace_Raise_By_Goto --
755 ---------------------------
757 procedure Replace_Raise_By_Goto (Raise_S : Node_Id; Goto_L1 : Node_Id) is
758 Loc : constant Source_Ptr := Sloc (Raise_S);
759 Excep : Entity_Id;
760 LR : Node_Id;
761 Cond : Node_Id;
762 Orig : Node_Id;
764 begin
765 -- If we have a null statement, it means that there is no replacement
766 -- needed (typically this results from a suppressed check).
768 if Nkind (Raise_S) = N_Null_Statement then
769 return;
771 -- Test for Raise_xxx_Error
773 elsif Nkind (Raise_S) = N_Raise_Constraint_Error then
774 Excep := Standard_Constraint_Error;
775 Cond := Condition (Raise_S);
777 elsif Nkind (Raise_S) = N_Raise_Storage_Error then
778 Excep := Standard_Storage_Error;
779 Cond := Condition (Raise_S);
781 elsif Nkind (Raise_S) = N_Raise_Program_Error then
782 Excep := Standard_Program_Error;
783 Cond := Condition (Raise_S);
785 -- The only other possibility is a node that is or used to be a
786 -- simple raise statement. Note that the string expression in the
787 -- original Raise statement is ignored.
789 else
790 Orig := Original_Node (Raise_S);
791 pragma Assert (Nkind (Orig) = N_Raise_Statement
792 and then Present (Name (Orig)));
793 Excep := Entity (Name (Orig));
794 Cond := Empty;
795 end if;
797 -- Here Excep is the exception to raise, and Cond is the condition
798 -- First prepare the call to Local_Raise (excep'address).
800 if RTE_Available (RE_Local_Raise) then
801 LR :=
802 Make_Procedure_Call_Statement (Loc,
803 Name => New_Occurrence_Of (RTE (RE_Local_Raise), Loc),
804 Parameter_Associations => New_List (
805 Unchecked_Convert_To (RTE (RE_Address),
806 Make_Attribute_Reference (Loc,
807 Prefix => New_Occurrence_Of (Excep, Loc),
808 Attribute_Name => Name_Identity))));
810 -- Use null statement if Local_Raise not available
812 else
813 LR :=
814 Make_Null_Statement (Loc);
815 end if;
817 -- If there is no condition, we rewrite as
819 -- begin
820 -- Local_Raise (excep'Identity);
821 -- goto L1;
822 -- end;
824 if No (Cond) then
825 Rewrite (Raise_S,
826 Make_Block_Statement (Loc,
827 Handled_Statement_Sequence =>
828 Make_Handled_Sequence_Of_Statements (Loc,
829 Statements => New_List (LR, Goto_L1))));
830 Set_Exception_Junk (Raise_S);
832 -- If there is a condition, we rewrite as
834 -- if condition then
835 -- Local_Raise (excep'Identity);
836 -- goto L1;
837 -- end if;
839 else
840 Rewrite (Raise_S,
841 Make_If_Statement (Loc,
842 Condition => Cond,
843 Then_Statements => New_List (LR, Goto_L1)));
844 end if;
846 Analyze (Raise_S);
847 end Replace_Raise_By_Goto;
849 -- Start of processing for Expand_Exception_Handlers
851 begin
852 Expand_Local_Exception_Handlers;
854 -- Loop through handlers
856 Handler := First_Non_Pragma (Handlrs);
857 Handler_Loop : while Present (Handler) loop
858 Process_Statements_For_Controlled_Objects (Handler);
860 Next_Handler := Next_Non_Pragma (Handler);
862 -- Remove source handler if gnat debug flag .x is set
864 if Debug_Flag_Dot_X and then Comes_From_Source (Handler) then
865 Remove (Handler);
867 -- Remove handler if no exception propagation, generating a warning
868 -- if a source generated handler was not the target of a local raise.
870 else
871 if not Has_Local_Raise (Handler)
872 and then Comes_From_Source (Handler)
873 then
874 Warn_If_No_Local_Raise (Handler);
875 end if;
877 if No_Exception_Propagation_Active then
878 Remove (Handler);
880 -- Exception handler is active and retained and must be processed
882 else
883 -- If an exception occurrence is present, then we must declare
884 -- it and initialize it from the value stored in the TSD
886 -- declare
887 -- name : Exception_Occurrence;
888 -- begin
889 -- Save_Occurrence (name, Get_Current_Excep.all)
890 -- ...
891 -- end;
893 -- This expansion is only performed when using CodePeer.
894 -- Gigi will insert a call to initialize the choice parameter.
896 if Present (Choice_Parameter (Handler))
897 and then CodePeer_Mode
898 then
899 declare
900 Cparm : constant Entity_Id := Choice_Parameter (Handler);
901 Cloc : constant Source_Ptr := Sloc (Cparm);
902 Hloc : constant Source_Ptr := Sloc (Handler);
903 Save : Node_Id;
905 begin
906 -- Note: No_Location used to hide code from the debugger,
907 -- so single stepping doesn't jump back and forth.
909 Save :=
910 Make_Procedure_Call_Statement (No_Location,
911 Name =>
912 New_Occurrence_Of
913 (RTE (RE_Save_Occurrence), No_Location),
914 Parameter_Associations => New_List (
915 New_Occurrence_Of (Cparm, No_Location),
916 Make_Explicit_Dereference (No_Location,
917 Prefix =>
918 Make_Function_Call (No_Location,
919 Name =>
920 Make_Explicit_Dereference (No_Location,
921 Prefix =>
922 New_Occurrence_Of
923 (RTE (RE_Get_Current_Excep),
924 No_Location))))));
926 Mark_Rewrite_Insertion (Save);
927 Prepend (Save, Statements (Handler));
929 Obj_Decl :=
930 Make_Object_Declaration (Cloc,
931 Defining_Identifier => Cparm,
932 Object_Definition =>
933 New_Occurrence_Of
934 (RTE (RE_Exception_Occurrence), Cloc));
935 Set_No_Initialization (Obj_Decl, True);
937 Rewrite (Handler,
938 Make_Exception_Handler (Hloc,
939 Choice_Parameter => Empty,
940 Exception_Choices => Exception_Choices (Handler),
941 Statements => New_List (
942 Make_Block_Statement (Hloc,
943 Declarations => New_List (Obj_Decl),
944 Handled_Statement_Sequence =>
945 Make_Handled_Sequence_Of_Statements (Hloc,
946 Statements => Statements (Handler))))));
948 -- Local raise statements can't occur, since exception
949 -- handlers with choice parameters are not allowed when
950 -- No_Exception_Propagation applies, so set attributes
951 -- accordingly.
953 Set_Local_Raise_Statements (Handler, No_Elist);
954 Set_Local_Raise_Not_OK (Handler);
956 Analyze_List
957 (Statements (Handler), Suppress => All_Checks);
958 end;
959 end if;
960 end if;
961 end if;
963 Handler := Next_Handler;
964 end loop Handler_Loop;
966 -- If all handlers got removed, then remove the list. Note we cannot
967 -- reference HSS here, since expanding local handlers may have buried
968 -- the handlers in an inner block.
970 if Is_Empty_List (Handlrs) then
971 Set_Exception_Handlers (Parent (Handlrs), No_List);
972 end if;
973 end Expand_Exception_Handlers;
975 ------------------------------------
976 -- Expand_N_Exception_Declaration --
977 ------------------------------------
979 -- Generates:
980 -- exceptE : constant String := "A.B.EXCEP"; -- static data
981 -- except : exception_data :=
982 -- (Handled_By_Other => False,
983 -- Lang => 'A',
984 -- Name_Length => exceptE'Length,
985 -- Full_Name => exceptE'Address,
986 -- HTable_Ptr => null,
987 -- Foreign_Data => null,
988 -- Raise_Hook => null);
990 -- (protecting test only needed if not at library level)
992 -- exceptF : aliased System.Atomic_Operations.Test_And_Set.
993 -- .Test_And_Set_Flag; -- static data
994 -- if not Atomic_Test_And_Set (exceptF) then
995 -- Register_Exception (except'Unrestricted_Access);
996 -- end if;
998 -- If a No_Tasking restriction is in effect, or if Test_And_Set_Flag
999 -- is unavailable, then use Boolean instead. In that case, we generate:
1001 -- exceptF : Boolean := True; -- static data
1002 -- if exceptF then
1003 -- ExceptF := False;
1004 -- Register_Exception (except'Unrestricted_Access);
1005 -- end if;
1007 procedure Expand_N_Exception_Declaration (N : Node_Id) is
1008 Id : constant Entity_Id := Defining_Identifier (N);
1009 Loc : constant Source_Ptr := Sloc (N);
1011 procedure Force_Static_Allocation_Of_Referenced_Objects
1012 (Aggregate : Node_Id);
1013 -- A specialized solution to one particular case of an ugly problem
1015 -- The given aggregate includes an Unchecked_Conversion as one of the
1016 -- component values. The call to Analyze_And_Resolve below ends up
1017 -- calling Exp_Ch4.Expand_N_Unchecked_Type_Conversion, which may decide
1018 -- to introduce a (constant) temporary and then obtain the component
1019 -- value by evaluating the temporary.
1021 -- In the case of an exception declared within a subprogram (or any
1022 -- other dynamic scope), this is a bad transformation. The exception
1023 -- object is marked as being Statically_Allocated but the temporary is
1024 -- not. If the initial value of a Statically_Allocated declaration
1025 -- references a dynamically allocated object, this prevents static
1026 -- initialization of the object.
1028 -- We cope with this here by marking the temporary Statically_Allocated.
1029 -- It might seem cleaner to generalize this utility and then use it to
1030 -- enforce a rule that the entities referenced in the declaration of any
1031 -- "hoisted" (i.e., Is_Statically_Allocated and not Is_Library_Level)
1032 -- entity must also be either Library_Level or hoisted. It turns out
1033 -- that this would be incompatible with the current treatment of an
1034 -- object which is local to a subprogram, subject to an Export pragma,
1035 -- not subject to an address clause, and whose declaration contains
1036 -- references to other local (non-hoisted) objects (e.g., in the initial
1037 -- value expression).
1039 function Null_String return String_Id;
1040 -- Build a null-terminated empty string
1042 ---------------------------------------------------
1043 -- Force_Static_Allocation_Of_Referenced_Objects --
1044 ---------------------------------------------------
1046 procedure Force_Static_Allocation_Of_Referenced_Objects
1047 (Aggregate : Node_Id)
1049 function Fixup_Node (N : Node_Id) return Traverse_Result;
1050 -- If the given node references a dynamically allocated object, then
1051 -- correct the declaration of the object.
1053 ----------------
1054 -- Fixup_Node --
1055 ----------------
1057 function Fixup_Node (N : Node_Id) return Traverse_Result is
1058 begin
1059 if Nkind (N) in N_Has_Entity
1060 and then Present (Entity (N))
1061 and then not Is_Library_Level_Entity (Entity (N))
1063 -- Note: the following test is not needed but it seems cleaner
1064 -- to do this test (this would be more important if procedure
1065 -- Force_Static_Allocation_Of_Referenced_Objects recursively
1066 -- traversed the declaration of an entity after marking it as
1067 -- statically allocated).
1069 and then not Is_Statically_Allocated (Entity (N))
1070 then
1071 Set_Is_Statically_Allocated (Entity (N));
1072 end if;
1074 return OK;
1075 end Fixup_Node;
1077 procedure Fixup_Tree is new Traverse_Proc (Fixup_Node);
1079 -- Start of processing for Force_Static_Allocation_Of_Referenced_Objects
1081 begin
1082 Fixup_Tree (Aggregate);
1083 end Force_Static_Allocation_Of_Referenced_Objects;
1085 -----------------
1086 -- Null_String --
1087 -----------------
1089 function Null_String return String_Id is
1090 begin
1091 Start_String;
1092 Store_String_Char (Get_Char_Code (ASCII.NUL));
1093 return End_String;
1094 end Null_String;
1096 -- Local variables
1098 Ex_Id : Entity_Id;
1099 Ex_Val : String_Id;
1100 Flag_Id : Entity_Id;
1101 L : List_Id;
1103 -- Start of processing for Expand_N_Exception_Declaration
1105 begin
1106 -- Nothing to do when generating C code
1108 if Modify_Tree_For_C then
1109 return;
1110 end if;
1112 -- Definition of the external name: nam : constant String := "A.B.NAME";
1114 Ex_Id :=
1115 Make_Defining_Identifier (Loc, New_External_Name (Chars (Id), 'E'));
1117 -- Do not generate an external name if the exception declaration is
1118 -- subject to pragma Discard_Names. Use a null-terminated empty name
1119 -- to ensure that Ada.Exceptions.Exception_Name functions properly.
1121 if Global_Discard_Names or else Discard_Names (Ex_Id) then
1122 Ex_Val := Null_String;
1124 -- Otherwise generate the fully qualified name of the exception
1126 else
1127 Ex_Val := Fully_Qualified_Name_String (Id);
1128 end if;
1130 Insert_Action (N,
1131 Make_Object_Declaration (Loc,
1132 Defining_Identifier => Ex_Id,
1133 Constant_Present => True,
1134 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
1135 Expression => Make_String_Literal (Loc, Ex_Val)));
1137 Set_Is_Statically_Allocated (Ex_Id);
1139 -- Create the aggregate list for type Standard.Exception_Type:
1140 -- Not_Handled_By_Others component: False
1142 L := Empty_List;
1143 Append_To (L, New_Occurrence_Of (Standard_False, Loc));
1145 -- Lang component: 'A'
1147 Append_To (L,
1148 Make_Character_Literal (Loc,
1149 Chars => Name_uA,
1150 Char_Literal_Value => UI_From_CC (Get_Char_Code ('A'))));
1152 -- Name_Length component: Nam'Length
1154 Append_To (L,
1155 Make_Attribute_Reference (Loc,
1156 Prefix => New_Occurrence_Of (Ex_Id, Loc),
1157 Attribute_Name => Name_Length));
1159 -- Full_Name component: Standard_Address?(Nam'Address)
1160 -- or 0 if CodePeer_Mode
1162 if CodePeer_Mode then
1163 Append_To (L, Make_Integer_Literal (Loc, Uint_0));
1164 else
1165 Append_To (L, OK_Convert_To (Standard_Address,
1166 Make_Attribute_Reference (Loc,
1167 Prefix => New_Occurrence_Of (Ex_Id, Loc),
1168 Attribute_Name => Name_Address)));
1169 end if;
1171 -- HTable_Ptr component: null
1173 Append_To (L, Make_Null (Loc));
1175 -- Foreign_Data component: null address
1177 Append_To (L, Make_Integer_Literal (Loc, Uint_0));
1179 -- Raise_Hook component: null
1181 Append_To (L, Make_Null (Loc));
1183 Set_Expression (N, Make_Aggregate (Loc, Expressions => L));
1184 Analyze_And_Resolve (Expression (N), Etype (Id));
1186 Force_Static_Allocation_Of_Referenced_Objects (Expression (N));
1188 -- Register_Exception (except'Unrestricted_Access);
1190 if not No_Exception_Handlers_Set
1191 and then not Restriction_Active (No_Exception_Registration)
1192 then
1193 L := New_List (
1194 Make_Procedure_Call_Statement (Loc,
1195 Name =>
1196 New_Occurrence_Of (RTE (RE_Register_Exception), Loc),
1197 Parameter_Associations => New_List (
1198 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
1199 Make_Attribute_Reference (Loc,
1200 Prefix => New_Occurrence_Of (Id, Loc),
1201 Attribute_Name => Name_Unrestricted_Access)))));
1203 Set_Register_Exception_Call (Id, First (L));
1205 if not Is_Library_Level_Entity (Id) then
1206 Flag_Id :=
1207 Make_Defining_Identifier (Loc,
1208 Chars => New_External_Name (Chars (Id), 'F'));
1209 Set_Is_Statically_Allocated (Flag_Id);
1211 declare
1212 Use_Test_And_Set_Flag : constant Boolean :=
1213 not Global_No_Tasking
1214 and then RTE_Available (RE_Test_And_Set_Flag);
1216 Flag_Decl : Node_Id;
1217 Condition : Node_Id;
1218 begin
1219 if Use_Test_And_Set_Flag then
1220 Flag_Decl :=
1221 Make_Object_Declaration (Loc,
1222 Defining_Identifier => Flag_Id,
1223 Aliased_Present => True,
1224 Object_Definition =>
1225 New_Occurrence_Of (RTE (RE_Test_And_Set_Flag), Loc));
1226 else
1227 Flag_Decl :=
1228 Make_Object_Declaration (Loc,
1229 Defining_Identifier => Flag_Id,
1230 Object_Definition =>
1231 New_Occurrence_Of (Standard_Boolean, Loc),
1232 Expression =>
1233 New_Occurrence_Of (Standard_True, Loc));
1234 end if;
1236 Insert_Action (N, Flag_Decl);
1238 if Use_Test_And_Set_Flag then
1239 Condition :=
1240 Make_Op_Not (Loc,
1241 Make_Function_Call (Loc,
1242 Name => New_Occurrence_Of
1243 (RTE (RE_Atomic_Test_And_Set), Loc),
1244 Parameter_Associations =>
1245 New_List (New_Occurrence_Of (Flag_Id, Loc))));
1246 else
1247 Condition := New_Occurrence_Of (Flag_Id, Loc);
1249 Append_To (L,
1250 Make_Assignment_Statement (Loc,
1251 Name => New_Occurrence_Of (Flag_Id, Loc),
1252 Expression => New_Occurrence_Of (Standard_False, Loc)));
1253 end if;
1255 Insert_After_And_Analyze (N,
1256 Make_Implicit_If_Statement (N,
1257 Condition => Condition,
1258 Then_Statements => L));
1259 end;
1260 else
1261 Insert_List_After_And_Analyze (N, L);
1262 end if;
1263 end if;
1264 end Expand_N_Exception_Declaration;
1266 ---------------------------------------------
1267 -- Expand_N_Handled_Sequence_Of_Statements --
1268 ---------------------------------------------
1270 procedure Expand_N_Handled_Sequence_Of_Statements (N : Node_Id) is
1271 begin
1272 -- Expand exception handlers
1274 if Present (Exception_Handlers (N))
1275 and then not Restriction_Active (No_Exception_Handlers)
1276 then
1277 Expand_Exception_Handlers (N);
1278 end if;
1280 -- If local exceptions are being expanded, the previous call will
1281 -- have rewritten the construct as a block and reanalyzed it. No
1282 -- further expansion is needed.
1284 if Analyzed (N) then
1285 return;
1286 end if;
1288 -- Add cleanup actions if required. No cleanup actions are needed in
1289 -- thunks associated with interfaces, because they only displace the
1290 -- pointer to the object. For extended return statements, we need
1291 -- cleanup actions if the Handled_Statement_Sequence contains generated
1292 -- objects of controlled types, for example. We do not want to clean up
1293 -- the return object.
1295 if Nkind (Parent (N)) not in N_Accept_Statement
1296 | N_Extended_Return_Statement
1297 | N_Package_Body
1298 and then not Delay_Cleanups (Current_Scope)
1299 and then not Is_Thunk (Current_Scope)
1300 then
1301 Expand_Cleanup_Actions (Parent (N));
1303 elsif Nkind (Parent (N)) = N_Extended_Return_Statement
1304 and then Handled_Statement_Sequence (Parent (N)) = N
1305 and then not Delay_Cleanups (Current_Scope)
1306 then
1307 pragma Assert (not Is_Thunk (Current_Scope));
1308 Expand_Cleanup_Actions (Parent (N));
1309 end if;
1310 end Expand_N_Handled_Sequence_Of_Statements;
1312 -------------------------------------
1313 -- Expand_N_Raise_Constraint_Error --
1314 -------------------------------------
1316 procedure Expand_N_Raise_Constraint_Error (N : Node_Id) is
1317 begin
1318 -- We adjust the condition to deal with the C/Fortran boolean case. This
1319 -- may well not be necessary, as all such conditions are generated by
1320 -- the expander and probably are all standard boolean, but who knows
1321 -- what strange optimization in future may require this adjustment.
1323 Adjust_Condition (Condition (N));
1325 -- Now deal with possible local raise handling
1327 Possible_Local_Raise (N, Standard_Constraint_Error);
1328 end Expand_N_Raise_Constraint_Error;
1330 -------------------------------
1331 -- Expand_N_Raise_Expression --
1332 -------------------------------
1334 procedure Expand_N_Raise_Expression (N : Node_Id) is
1335 Loc : constant Source_Ptr := Sloc (N);
1336 Typ : constant Entity_Id := Etype (N);
1337 RCE : Node_Id;
1339 begin
1340 Possible_Local_Raise (N, Entity (Name (N)));
1342 -- Later we must teach the back end/gigi how to deal with this, but
1343 -- for now we will assume the type is Standard_Boolean and transform
1344 -- the node to:
1346 -- do
1347 -- raise X [with string]
1348 -- in
1349 -- raise Constraint_Error;
1351 -- The raise constraint error can never be executed. It is just a dummy
1352 -- node that can be labeled with an arbitrary type.
1354 RCE := Make_Raise_Constraint_Error (Loc, Reason => CE_Explicit_Raise);
1355 Set_Etype (RCE, Typ);
1357 Rewrite (N,
1358 Make_Expression_With_Actions (Loc,
1359 Actions => New_List (
1360 Make_Raise_Statement (Loc,
1361 Name => Name (N),
1362 Expression => Expression (N))),
1363 Expression => RCE));
1365 Analyze_And_Resolve (N, Typ);
1366 end Expand_N_Raise_Expression;
1368 ----------------------------------
1369 -- Expand_N_Raise_Program_Error --
1370 ----------------------------------
1372 procedure Expand_N_Raise_Program_Error (N : Node_Id) is
1373 begin
1374 -- We adjust the condition to deal with the C/Fortran boolean case. This
1375 -- may well not be necessary, as all such conditions are generated by
1376 -- the expander and probably are all standard boolean, but who knows
1377 -- what strange optimization in future may require this adjustment.
1379 Adjust_Condition (Condition (N));
1381 -- Now deal with possible local raise handling
1383 Possible_Local_Raise (N, Standard_Program_Error);
1384 end Expand_N_Raise_Program_Error;
1386 ------------------------------
1387 -- Expand_N_Raise_Statement --
1388 ------------------------------
1390 procedure Expand_N_Raise_Statement (N : Node_Id) is
1391 Loc : constant Source_Ptr := Sloc (N);
1392 Ehand : Node_Id;
1393 E : Entity_Id;
1394 Str : String_Id;
1395 H : Node_Id;
1396 Src : Boolean;
1398 begin
1399 -- Processing for locally handled exception (exclude reraise case)
1401 if Present (Name (N)) and then Is_Entity_Name (Name (N)) then
1402 if Debug_Flag_Dot_G
1403 or else Restriction_Active (No_Exception_Propagation)
1404 then
1405 -- If we have a local handler, then note that this is potentially
1406 -- able to be transformed into a goto statement.
1408 H := Find_Local_Handler (Entity (Name (N)), N);
1410 if Present (H) then
1411 if No (Local_Raise_Statements (H)) then
1412 Set_Local_Raise_Statements (H, New_Elmt_List);
1413 end if;
1415 -- Append the new entry if it is not there already. Sometimes
1416 -- we have situations where due to reexpansion, the same node
1417 -- is analyzed twice and would otherwise be added twice.
1419 Append_Unique_Elmt (N, Local_Raise_Statements (H));
1420 Set_Has_Local_Raise (H);
1422 -- If no local handler, then generate no propagation warning
1424 else
1425 Warn_If_No_Propagation (N);
1426 end if;
1428 end if;
1429 end if;
1431 -- If a string expression is present, then the raise statement is
1432 -- converted to a call:
1433 -- Raise_Exception (exception-name'Identity, string);
1434 -- and there is nothing else to do.
1436 if Present (Expression (N)) then
1438 -- Adjust message to deal with Prefix_Exception_Messages. We only
1439 -- add the prefix to string literals, if the message is being
1440 -- constructed, we assume it already deals with uniqueness.
1442 if Prefix_Exception_Messages
1443 and then Nkind (Expression (N)) = N_String_Literal
1444 then
1445 declare
1446 Buf : Bounded_String;
1447 begin
1448 Add_Source_Info (Buf, Loc, Name_Enclosing_Entity);
1449 Append (Buf, ": ");
1450 Append (Buf, Strval (Expression (N)));
1451 Rewrite (Expression (N), Make_String_Literal (Loc, +Buf));
1452 Analyze_And_Resolve (Expression (N), Standard_String);
1453 end;
1454 end if;
1456 -- Avoid passing exception-name'identity in runtimes in which this
1457 -- argument is not used. This avoids generating undefined references
1458 -- to these exceptions when compiling with no optimization
1460 if Configurable_Run_Time_On_Target
1461 and then (Restriction_Active (No_Exception_Handlers)
1462 or else
1463 Restriction_Active (No_Exception_Propagation))
1464 then
1465 Rewrite (N,
1466 Make_Procedure_Call_Statement (Loc,
1467 Name => New_Occurrence_Of (RTE (RE_Raise_Exception), Loc),
1468 Parameter_Associations => New_List (
1469 New_Occurrence_Of (RTE (RE_Null_Id), Loc),
1470 Expression (N))));
1471 else
1472 Rewrite (N,
1473 Make_Procedure_Call_Statement (Loc,
1474 Name => New_Occurrence_Of (RTE (RE_Raise_Exception), Loc),
1475 Parameter_Associations => New_List (
1476 Make_Attribute_Reference (Loc,
1477 Prefix => Name (N),
1478 Attribute_Name => Name_Identity),
1479 Expression (N))));
1480 end if;
1482 Analyze (N);
1483 return;
1484 end if;
1486 -- Remaining processing is for the case where no string expression is
1487 -- present.
1489 -- Don't expand a raise statement that does not come from source if we
1490 -- have already had configurable run-time violations, since most likely
1491 -- it will be junk cascaded nonsense.
1493 if Configurable_Run_Time_Violations > 0
1494 and then not Comes_From_Source (N)
1495 then
1496 return;
1497 end if;
1499 -- Convert explicit raise of Program_Error, Constraint_Error, and
1500 -- Storage_Error into the corresponding raise (in High_Integrity_Mode
1501 -- all other raises will get normal expansion and be disallowed,
1502 -- but this is also faster in all modes). Propagate Comes_From_Source
1503 -- flag to the new node.
1505 if Present (Name (N)) and then Is_Entity_Name (Name (N)) then
1506 Src := Comes_From_Source (N);
1508 if Entity (Name (N)) = Standard_Constraint_Error then
1509 Rewrite (N,
1510 Make_Raise_Constraint_Error (Loc, Reason => CE_Explicit_Raise));
1511 Set_Comes_From_Source (N, Src);
1512 Analyze (N);
1513 return;
1515 elsif Entity (Name (N)) = Standard_Program_Error then
1516 Rewrite (N,
1517 Make_Raise_Program_Error (Loc, Reason => PE_Explicit_Raise));
1518 Set_Comes_From_Source (N, Src);
1519 Analyze (N);
1520 return;
1522 elsif Entity (Name (N)) = Standard_Storage_Error then
1523 Rewrite (N,
1524 Make_Raise_Storage_Error (Loc, Reason => SE_Explicit_Raise));
1525 Set_Comes_From_Source (N, Src);
1526 Analyze (N);
1527 return;
1528 end if;
1529 end if;
1531 -- Case of name present, in this case we expand raise name to
1533 -- Raise_Exception (name'Identity, location_string);
1535 -- where location_string identifies the file/line of the raise
1537 if Present (Name (N)) and then Is_Entity_Name (Name (N)) then
1538 declare
1539 Id : Entity_Id := Entity (Name (N));
1540 Buf : Bounded_String;
1542 begin
1543 Build_Location_String (Buf, Loc);
1545 -- If the exception is a renaming, use the exception that it
1546 -- renames (which might be a predefined exception, e.g.).
1548 if Present (Renamed_Entity (Id)) then
1549 Id := Renamed_Entity (Id);
1550 end if;
1552 -- Build a C-compatible string in case of no exception handlers,
1553 -- since this is what the last chance handler is expecting.
1555 if No_Exception_Handlers_Set then
1557 -- Generate an empty message if configuration pragma
1558 -- Suppress_Exception_Locations is set for this unit.
1560 if Opt.Exception_Locations_Suppressed then
1561 Buf.Length := 0;
1562 end if;
1564 Append (Buf, ASCII.NUL);
1565 end if;
1567 if Opt.Exception_Locations_Suppressed then
1568 Buf.Length := 0;
1569 end if;
1571 Str := String_From_Name_Buffer (Buf);
1573 -- Convert raise to call to the Raise_Exception routine
1575 Rewrite (N,
1576 Make_Procedure_Call_Statement (Loc,
1577 Name =>
1578 New_Occurrence_Of (RTE (RE_Raise_Exception), Loc),
1579 Parameter_Associations => New_List (
1580 Make_Attribute_Reference (Loc,
1581 Prefix => Name (N),
1582 Attribute_Name => Name_Identity),
1583 Make_String_Literal (Loc, Strval => Str))));
1584 end;
1586 -- Case of no name present (reraise). We rewrite the raise to:
1588 -- Reraise_Occurrence_Always (EO);
1590 -- where EO is the current exception occurrence. If the current handler
1591 -- does not have a choice parameter specification, then we provide one.
1593 else
1594 -- Bypass expansion to a run-time call when back-end exception
1595 -- handling is active, unless the target is CodePeer, where
1596 -- raising an exception is treated as an error.
1598 if not CodePeer_Mode then
1599 return;
1600 end if;
1602 -- Find innermost enclosing exception handler (there must be one,
1603 -- since the semantics has already verified that this raise statement
1604 -- is valid, and a raise with no arguments is only permitted in the
1605 -- context of an exception handler).
1607 Ehand := Parent (N);
1608 while Nkind (Ehand) /= N_Exception_Handler loop
1609 Ehand := Parent (Ehand);
1610 end loop;
1612 -- Make exception choice parameter if none present. Note that we do
1613 -- not need to put the entity on the entity chain, since no one will
1614 -- be referencing this entity by normal visibility methods.
1616 if No (Choice_Parameter (Ehand)) then
1617 E := Make_Temporary (Loc, 'E');
1618 Set_Choice_Parameter (Ehand, E);
1619 Mutate_Ekind (E, E_Variable);
1620 Set_Etype (E, RTE (RE_Exception_Occurrence));
1621 Set_Scope (E, Current_Scope);
1622 end if;
1624 -- Now rewrite the raise as a call to Reraise. A special case arises
1625 -- if this raise statement occurs in the context of a handler for
1626 -- all others (i.e. an at end handler). in this case we avoid
1627 -- the call to defer abort, cleanup routines are expected to be
1628 -- called in this case with aborts deferred.
1630 declare
1631 Ech : constant Node_Id := First (Exception_Choices (Ehand));
1632 Ent : Entity_Id;
1634 begin
1635 if Nkind (Ech) = N_Others_Choice
1636 and then All_Others (Ech)
1637 then
1638 Ent := RTE (RE_Reraise_Occurrence_No_Defer);
1639 else
1640 Ent := RTE (RE_Reraise_Occurrence_Always);
1641 end if;
1643 Rewrite (N,
1644 Make_Procedure_Call_Statement (Loc,
1645 Name => New_Occurrence_Of (Ent, Loc),
1646 Parameter_Associations => New_List (
1647 New_Occurrence_Of (Choice_Parameter (Ehand), Loc))));
1648 end;
1649 end if;
1651 Analyze (N);
1652 end Expand_N_Raise_Statement;
1654 -----------------------------------
1655 -- Expand_N_Raise_When_Statement --
1656 -----------------------------------
1658 procedure Expand_N_Raise_When_Statement (N : Node_Id) is
1659 Loc : constant Source_Ptr := Sloc (N);
1660 begin
1661 Rewrite (N,
1662 Make_If_Statement (Loc,
1663 Condition => Condition (N),
1664 Then_Statements => New_List (
1665 Make_Raise_Statement (Loc,
1666 Name => Name (N),
1667 Expression => Expression (N)))));
1669 Analyze (N);
1670 end Expand_N_Raise_When_Statement;
1672 ----------------------------------
1673 -- Expand_N_Raise_Storage_Error --
1674 ----------------------------------
1676 procedure Expand_N_Raise_Storage_Error (N : Node_Id) is
1677 begin
1678 -- We adjust the condition to deal with the C/Fortran boolean case. This
1679 -- may well not be necessary, as all such conditions are generated by
1680 -- the expander and probably are all standard boolean, but who knows
1681 -- what strange optimization in future may require this adjustment.
1683 Adjust_Condition (Condition (N));
1685 -- Now deal with possible local raise handling
1687 Possible_Local_Raise (N, Standard_Storage_Error);
1688 end Expand_N_Raise_Storage_Error;
1690 --------------------------
1691 -- Possible_Local_Raise --
1692 --------------------------
1694 procedure Possible_Local_Raise (N : Node_Id; E : Entity_Id) is
1695 begin
1696 -- Nothing to do if local raise optimization not active
1698 if not Debug_Flag_Dot_G
1699 and then not Restriction_Active (No_Exception_Propagation)
1700 then
1701 return;
1702 end if;
1704 -- Nothing to do if original node was an explicit raise, because in
1705 -- that case, we already generated the required warning for the raise.
1707 if Nkind (Original_Node (N)) = N_Raise_Statement then
1708 return;
1709 end if;
1711 -- Otherwise see if we have a local handler for the exception
1713 declare
1714 H : constant Node_Id := Find_Local_Handler (E, N);
1716 begin
1717 -- If so, mark that it has a local raise
1719 if Present (H) then
1720 Set_Has_Local_Raise (H, True);
1722 -- Otherwise, if the No_Exception_Propagation restriction is active
1723 -- and the warning is enabled, generate the appropriate warnings.
1725 -- ??? Do not do it for the Call_Marker nodes inserted by the ABE
1726 -- mechanism because this generates too many false positives, or
1727 -- for generic instantiations for the same reason.
1729 elsif Warn_On_Non_Local_Exception
1730 and then Restriction_Active (No_Exception_Propagation)
1731 and then Nkind (N) /= N_Call_Marker
1732 and then Nkind (N) not in N_Generic_Instantiation
1733 then
1734 Warn_No_Exception_Propagation_Active (N);
1736 if Configurable_Run_Time_Mode then
1737 Error_Msg_NE
1738 ("\?.x?& may call Last_Chance_Handler", N, E);
1739 else
1740 Error_Msg_NE
1741 ("\?.x?& may result in unhandled exception", N, E);
1742 end if;
1743 end if;
1744 end;
1745 end Possible_Local_Raise;
1747 ------------------------
1748 -- Find_Local_Handler --
1749 ------------------------
1751 function Find_Local_Handler
1752 (Ename : Entity_Id;
1753 Nod : Node_Id) return Node_Id
1755 N : Node_Id;
1756 P : Node_Id;
1757 H : Node_Id;
1758 C : Node_Id;
1760 SSE : Scope_Stack_Entry renames Scope_Stack.Table (Scope_Stack.Last);
1761 -- This is used to test for wrapped actions below
1763 ERaise : Entity_Id;
1764 EHandle : Entity_Id;
1765 -- The entity Id's for the exception we are raising and handling, using
1766 -- the renamed exception if a Renamed_Entity is present.
1768 begin
1769 -- Never any local handler if all handlers removed
1771 if Debug_Flag_Dot_X then
1772 return Empty;
1773 end if;
1775 -- Get the exception we are raising, allowing for renaming
1777 ERaise := Get_Renamed_Entity (Ename);
1779 -- We need to check if the node we are looking at is contained in
1782 -- Loop to search up the tree
1784 N := Nod;
1785 loop
1786 P := Parent (N);
1788 -- If we get to the top of the tree, or to a subprogram, task, entry,
1789 -- protected body, or accept statement without having found a
1790 -- matching handler, then there is no local handler.
1792 if No (P)
1793 or else Nkind (P) = N_Subprogram_Body
1794 or else Nkind (P) = N_Task_Body
1795 or else Nkind (P) = N_Protected_Body
1796 or else Nkind (P) = N_Entry_Body
1797 or else Nkind (P) = N_Accept_Statement
1798 then
1799 return Empty;
1801 -- Test for handled sequence of statements with at least one
1802 -- exception handler which might be the one we are looking for.
1804 -- We need to check if the node N is covered by the statement part of
1805 -- P rather than one of its exception handlers (an exception handler
1806 -- obviously does not cover its own statements).
1808 -- This test is more delicate than might be thought. It is not just
1809 -- a matter of checking the Statements (P), because the node might be
1810 -- waiting to be wrapped in a transient scope, in which case it will
1811 -- end up in the block statements, even though it is not there now.
1813 elsif Nkind (P) = N_Handled_Sequence_Of_Statements
1814 and then Is_List_Member (N)
1815 and then List_Containing (N) in Statements (P)
1816 | SSE.Actions_To_Be_Wrapped (Before)
1817 | SSE.Actions_To_Be_Wrapped (After)
1818 | SSE.Actions_To_Be_Wrapped (Cleanup)
1819 then
1820 -- Loop through exception handlers and guard against pragmas
1821 -- appearing among them.
1823 H := First_Non_Pragma (Exception_Handlers (P));
1824 while Present (H) loop
1826 -- Guard against other constructs appearing in the list of
1827 -- exception handlers.
1829 -- Loop through choices in one handler
1831 C := First (Exception_Choices (H));
1832 while Present (C) loop
1834 -- Deal with others case
1836 if Nkind (C) = N_Others_Choice then
1838 -- Matching others handler, but we need to ensure there
1839 -- is no choice parameter. If there is, then we don't
1840 -- have a local handler after all (since we do not allow
1841 -- choice parameters for local handlers).
1843 if No (Choice_Parameter (H)) then
1844 return H;
1845 else
1846 return Empty;
1847 end if;
1849 -- If not others must be entity name
1851 else
1852 pragma Assert (Is_Entity_Name (C));
1853 pragma Assert (Present (Entity (C)));
1855 -- Get exception being handled, dealing with renaming
1857 EHandle := Get_Renamed_Entity (Entity (C));
1859 -- If match, then check choice parameter
1861 if ERaise = EHandle then
1862 if No (Choice_Parameter (H)) then
1863 return H;
1864 else
1865 return Empty;
1866 end if;
1867 end if;
1868 end if;
1870 Next (C);
1871 end loop;
1873 Next_Non_Pragma (H);
1874 end loop;
1875 end if;
1877 N := P;
1878 end loop;
1879 end Find_Local_Handler;
1881 ---------------------------------
1882 -- Get_Local_Raise_Call_Entity --
1883 ---------------------------------
1885 -- Note: this is primarily provided for use by the back end in generating
1886 -- calls to Local_Raise. But it would be too late in the back end to call
1887 -- RTE if this actually caused a load/analyze of the unit. So what we do
1888 -- is to ensure there is a dummy call to this function during front end
1889 -- processing so that the unit gets loaded then, and not later.
1891 Local_Raise_Call_Entity : Entity_Id;
1892 Local_Raise_Call_Entity_Set : Boolean := False;
1894 function Get_Local_Raise_Call_Entity return Entity_Id is
1895 begin
1896 if not Local_Raise_Call_Entity_Set then
1897 Local_Raise_Call_Entity_Set := True;
1899 if RTE_Available (RE_Local_Raise) then
1900 Local_Raise_Call_Entity := RTE (RE_Local_Raise);
1901 else
1902 Local_Raise_Call_Entity := Empty;
1903 end if;
1904 end if;
1906 return Local_Raise_Call_Entity;
1907 end Get_Local_Raise_Call_Entity;
1909 -----------------------------
1910 -- Get_RT_Exception_Entity --
1911 -----------------------------
1913 function Get_RT_Exception_Entity (R : RT_Exception_Code) return Entity_Id is
1914 begin
1915 case Rkind (R) is
1916 when CE_Reason => return Standard_Constraint_Error;
1917 when PE_Reason => return Standard_Program_Error;
1918 when SE_Reason => return Standard_Storage_Error;
1919 end case;
1920 end Get_RT_Exception_Entity;
1922 ---------------------------
1923 -- Get_RT_Exception_Name --
1924 ---------------------------
1926 procedure Get_RT_Exception_Name (Code : RT_Exception_Code) is
1927 begin
1928 case Code is
1929 when CE_Access_Check_Failed =>
1930 Add_Str_To_Name_Buffer ("CE_Access_Check");
1931 when CE_Access_Parameter_Is_Null =>
1932 Add_Str_To_Name_Buffer ("CE_Null_Access_Parameter");
1933 when CE_Discriminant_Check_Failed =>
1934 Add_Str_To_Name_Buffer ("CE_Discriminant_Check");
1935 when CE_Divide_By_Zero =>
1936 Add_Str_To_Name_Buffer ("CE_Divide_By_Zero");
1937 when CE_Explicit_Raise =>
1938 Add_Str_To_Name_Buffer ("CE_Explicit_Raise");
1939 when CE_Index_Check_Failed =>
1940 Add_Str_To_Name_Buffer ("CE_Index_Check");
1941 when CE_Invalid_Data =>
1942 Add_Str_To_Name_Buffer ("CE_Invalid_Data");
1943 when CE_Length_Check_Failed =>
1944 Add_Str_To_Name_Buffer ("CE_Length_Check");
1945 when CE_Null_Exception_Id =>
1946 Add_Str_To_Name_Buffer ("CE_Null_Exception_Id");
1947 when CE_Null_Not_Allowed =>
1948 Add_Str_To_Name_Buffer ("CE_Null_Not_Allowed");
1949 when CE_Overflow_Check_Failed =>
1950 Add_Str_To_Name_Buffer ("CE_Overflow_Check");
1951 when CE_Partition_Check_Failed =>
1952 Add_Str_To_Name_Buffer ("CE_Partition_Check");
1953 when CE_Range_Check_Failed =>
1954 Add_Str_To_Name_Buffer ("CE_Range_Check");
1955 when CE_Tag_Check_Failed =>
1956 Add_Str_To_Name_Buffer ("CE_Tag_Check");
1958 when PE_Access_Before_Elaboration =>
1959 Add_Str_To_Name_Buffer ("PE_Access_Before_Elaboration");
1960 when PE_Accessibility_Check_Failed =>
1961 Add_Str_To_Name_Buffer ("PE_Accessibility_Check");
1962 when PE_Address_Of_Intrinsic =>
1963 Add_Str_To_Name_Buffer ("PE_Address_Of_Intrinsic");
1964 when PE_Aliased_Parameters =>
1965 Add_Str_To_Name_Buffer ("PE_Aliased_Parameters");
1966 when PE_All_Guards_Closed =>
1967 Add_Str_To_Name_Buffer ("PE_All_Guards_Closed");
1968 when PE_Bad_Predicated_Generic_Type =>
1969 Add_Str_To_Name_Buffer ("PE_Bad_Predicated_Generic_Type");
1970 when PE_Build_In_Place_Mismatch =>
1971 Add_Str_To_Name_Buffer ("PE_Build_In_Place_Mismatch");
1972 when PE_Current_Task_In_Entry_Body =>
1973 Add_Str_To_Name_Buffer ("PE_Current_Task_In_Entry_Body");
1974 when PE_Duplicated_Entry_Address =>
1975 Add_Str_To_Name_Buffer ("PE_Duplicated_Entry_Address");
1976 when PE_Explicit_Raise =>
1977 Add_Str_To_Name_Buffer ("PE_Explicit_Raise");
1978 when PE_Finalize_Raised_Exception =>
1979 Add_Str_To_Name_Buffer ("PE_Finalize_Raised_Exception");
1980 when PE_Implicit_Return =>
1981 Add_Str_To_Name_Buffer ("PE_Implicit_Return");
1982 when PE_Misaligned_Address_Value =>
1983 Add_Str_To_Name_Buffer ("PE_Misaligned_Address_Value");
1984 when PE_Missing_Return =>
1985 Add_Str_To_Name_Buffer ("PE_Missing_Return");
1986 when PE_Non_Transportable_Actual =>
1987 Add_Str_To_Name_Buffer ("PE_Non_Transportable_Actual");
1988 when PE_Overlaid_Controlled_Object =>
1989 Add_Str_To_Name_Buffer ("PE_Overlaid_Controlled_Object");
1990 when PE_Potentially_Blocking_Operation =>
1991 Add_Str_To_Name_Buffer ("PE_Potentially_Blocking_Operation");
1992 when PE_Stream_Operation_Not_Allowed =>
1993 Add_Str_To_Name_Buffer ("PE_Stream_Operation_Not_Allowed");
1994 when PE_Stubbed_Subprogram_Called =>
1995 Add_Str_To_Name_Buffer ("PE_Stubbed_Subprogram_Called");
1996 when PE_Unchecked_Union_Restriction =>
1997 Add_Str_To_Name_Buffer ("PE_Unchecked_Union_Restriction");
1999 when SE_Empty_Storage_Pool =>
2000 Add_Str_To_Name_Buffer ("SE_Empty_Storage_Pool");
2001 when SE_Explicit_Raise =>
2002 Add_Str_To_Name_Buffer ("SE_Explicit_Raise");
2003 when SE_Infinite_Recursion =>
2004 Add_Str_To_Name_Buffer ("SE_Infinite_Recursion");
2005 when SE_Object_Too_Large =>
2006 Add_Str_To_Name_Buffer ("SE_Object_Too_Large");
2007 end case;
2008 end Get_RT_Exception_Name;
2010 ----------------------------
2011 -- Warn_If_No_Local_Raise --
2012 ----------------------------
2014 procedure Warn_If_No_Local_Raise (N : Node_Id) is
2015 begin
2016 if Restriction_Active (No_Exception_Propagation)
2017 and then Warn_On_Non_Local_Exception
2018 then
2019 Warn_No_Exception_Propagation_Active (N);
2021 Error_Msg_N
2022 ("\?.x?this handler can never be entered, and has been removed", N);
2023 end if;
2024 end Warn_If_No_Local_Raise;
2026 ----------------------------
2027 -- Warn_If_No_Propagation --
2028 ----------------------------
2030 procedure Warn_If_No_Propagation (N : Node_Id) is
2031 begin
2032 if Restriction_Check_Required (No_Exception_Propagation)
2033 and then Warn_On_Non_Local_Exception
2034 then
2035 Warn_No_Exception_Propagation_Active (N);
2037 if Configurable_Run_Time_Mode then
2038 Error_Msg_N
2039 ("\?.x?Last_Chance_Handler will be called on exception", N);
2040 else
2041 Error_Msg_N
2042 ("\?.x?execution may raise unhandled exception", N);
2043 end if;
2044 end if;
2045 end Warn_If_No_Propagation;
2047 ------------------------------------------
2048 -- Warn_No_Exception_Propagation_Active --
2049 ------------------------------------------
2051 procedure Warn_No_Exception_Propagation_Active (N : Node_Id) is
2052 begin
2053 Error_Msg_N
2054 ("?.x?pragma Restrictions (No_Exception_Propagation) in effect", N);
2055 end Warn_No_Exception_Propagation_Active;
2057 end Exp_Ch11;