1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Debug
; use Debug
;
28 with Einfo
; use Einfo
;
29 with Elists
; use Elists
;
30 with Errout
; use Errout
;
31 with Exp_Ch7
; use Exp_Ch7
;
32 with Exp_Intr
; use Exp_Intr
;
33 with Exp_Util
; use Exp_Util
;
34 with Ghost
; use Ghost
;
35 with Namet
; use Namet
;
36 with Nlists
; use Nlists
;
37 with Nmake
; use Nmake
;
39 with Restrict
; use Restrict
;
40 with Rident
; use Rident
;
41 with Rtsfind
; use Rtsfind
;
43 with Sem_Ch8
; use Sem_Ch8
;
44 with Sem_Res
; use Sem_Res
;
45 with Sem_Util
; use Sem_Util
;
46 with Sinfo
; use Sinfo
;
47 with Sinput
; use Sinput
;
48 with Snames
; use Snames
;
49 with Stand
; use Stand
;
50 with Stringt
; use Stringt
;
51 with Targparm
; use Targparm
;
52 with Tbuild
; use Tbuild
;
53 with Uintp
; use Uintp
;
55 package body Exp_Ch11
is
57 -----------------------
58 -- Local Subprograms --
59 -----------------------
61 procedure Warn_No_Exception_Propagation_Active
(N
: Node_Id
);
62 -- Generates warning that pragma Restrictions (No_Exception_Propagation)
63 -- is in effect. Caller then generates appropriate continuation message.
64 -- N is the node on which the warning is placed.
66 procedure Warn_If_No_Propagation
(N
: Node_Id
);
67 -- Called for an exception raise that is not a local raise (and thus can
68 -- not be optimized to a goto. Issues warning if No_Exception_Propagation
69 -- restriction is set. N is the node for the raise or equivalent call.
71 ---------------------------
72 -- Expand_At_End_Handler --
73 ---------------------------
75 -- For a handled statement sequence that has a cleanup (At_End_Proc
76 -- field set), an exception handler of the following form is required:
83 -- Note: this exception handler is treated rather specially by
84 -- subsequent expansion in two respects:
86 -- The normal call to Undefer_Abort is omitted
87 -- The raise call does not do Defer_Abort
89 -- This is because the current tasking code seems to assume that
90 -- the call to the cleanup routine that is made from an exception
91 -- handler for the abort signal is called with aborts deferred.
93 -- This expansion is only done if we have front end exception handling.
94 -- If we have back end exception handling, then the AT END handler is
95 -- left alone, and cleanups (including the exceptional case) are handled
98 -- In the front end case, the exception handler described above handles
99 -- the exceptional case. The AT END handler is left in the generated tree
100 -- and the code generator (e.g. gigi) must still handle proper generation
101 -- of cleanup calls for the non-exceptional case.
103 procedure Expand_At_End_Handler
(HSS
: Node_Id
; Block
: Node_Id
) is
104 Clean
: constant Entity_Id
:= Entity
(At_End_Proc
(HSS
));
108 Loc
: constant Source_Ptr
:= No_Location
;
109 -- Location used for expansion. We quite deliberately do not set a
110 -- specific source location for the expanded handler. This makes
111 -- sense since really the handler is not associated with specific
112 -- source. We used to set this to Sloc (Clean), but that caused
113 -- useless and annoying bouncing around of line numbers in the
114 -- debugger in some circumstances.
117 pragma Assert
(Present
(Clean
));
118 pragma Assert
(No
(Exception_Handlers
(HSS
)));
120 -- Don't expand if back end exception handling active
122 if Exception_Mechanism
= Back_End_Exceptions
then
126 -- Don't expand an At End handler if we have already had configurable
127 -- run-time violations, since likely this will just be a matter of
128 -- generating useless cascaded messages
130 if Configurable_Run_Time_Violations
> 0 then
134 -- Don't expand an At End handler if we are not allowing exceptions
135 -- or if exceptions are transformed into local gotos, and never
136 -- propagated (No_Exception_Propagation).
138 if No_Exception_Handlers_Set
then
142 if Present
(Block
) then
147 Make_Others_Choice
(Loc
);
148 Set_All_Others
(Ohandle
);
151 Make_Procedure_Call_Statement
(Loc
,
152 Name
=> New_Occurrence_Of
(Clean
, Loc
)));
154 -- Generate reraise statement as last statement of AT-END handler,
155 -- unless we are under control of No_Exception_Propagation, in which
156 -- case no exception propagation is possible anyway, so we do not need
157 -- a reraise (the AT END handler in this case is only for normal exits
158 -- not for exceptional exits). Also, we flag the Reraise statement as
159 -- being part of an AT END handler to prevent signalling this reraise
160 -- as a violation of the restriction when it is not set.
162 if not Restriction_Active
(No_Exception_Propagation
) then
164 Rstm
: constant Node_Id
:= Make_Raise_Statement
(Loc
);
166 Set_From_At_End
(Rstm
);
167 Append_To
(Stmnts
, Rstm
);
171 Set_Exception_Handlers
(HSS
, New_List
(
172 Make_Implicit_Exception_Handler
(Loc
,
173 Exception_Choices
=> New_List
(Ohandle
),
174 Statements
=> Stmnts
)));
176 Analyze_List
(Stmnts
, Suppress
=> All_Checks
);
177 Expand_Exception_Handlers
(HSS
);
179 if Present
(Block
) then
182 end Expand_At_End_Handler
;
184 -------------------------------
185 -- Expand_Exception_Handlers --
186 -------------------------------
188 procedure Expand_Exception_Handlers
(HSS
: Node_Id
) is
189 Handlrs
: constant List_Id
:= Exception_Handlers
(HSS
);
190 Loc
: constant Source_Ptr
:= Sloc
(HSS
);
192 Others_Choice
: Boolean;
194 Next_Handler
: Node_Id
;
196 procedure Expand_Local_Exception_Handlers
;
197 -- This procedure handles the expansion of exception handlers for the
198 -- optimization of local raise statements into goto statements.
200 procedure Prepend_Call_To_Handler
202 Args
: List_Id
:= No_List
);
203 -- Routine to prepend a call to the procedure referenced by Proc at
204 -- the start of the handler code for the current Handler.
206 procedure Replace_Raise_By_Goto
(Raise_S
: Node_Id
; Goto_L1
: Node_Id
);
207 -- Raise_S is a raise statement (possibly expanded, and possibly of the
208 -- form of a Raise_xxx_Error node with a condition. This procedure is
209 -- called to replace the raise action with the (already analyzed) goto
210 -- statement passed as Goto_L1. This procedure also takes care of the
211 -- requirement of inserting a Local_Raise call where possible.
213 -------------------------------------
214 -- Expand_Local_Exception_Handlers --
215 -------------------------------------
217 -- There are two cases for this transformation. First the case of
218 -- explicit raise statements. For this case, the transformation we do
219 -- looks like this. Right now we have for example (where L1, L2 are
224 -- raise_exception (excep1'identity); -- was raise excep1
226 -- raise_exception (excep2'identity); -- was raise excep2
235 -- This gets transformed into:
238 -- L1 : label; -- marked Exception_Junk
239 -- L2 : label; -- marked Exception_Junk
240 -- L3 : label; -- marked Exception_Junk
242 -- begin -- marked Exception_Junk
244 -- local_raise (excep1'address); -- was raise excep1
247 -- local_raise (excep2'address); -- was raise excep2
257 -- goto L3; -- skip handler if no raise, marked Exception_Junk
259 -- <<L1>> -- local excep target label, marked Exception_Junk
260 -- begin -- marked Exception_Junk
263 -- goto L3; -- marked Exception_Junk
265 -- <<L2>> -- marked Exception_Junk
266 -- begin -- marked Exception_Junk
269 -- goto L3; -- marked Exception_Junk
270 -- <<L3>> -- marked Exception_Junk
273 -- Note: the reason we wrap the original statement sequence in an
274 -- inner block is that there may be raise statements within the
275 -- sequence of statements in the handlers, and we must ensure that
276 -- these are properly handled, and in particular, such raise statements
277 -- must not reenter the same exception handlers.
279 -- If the restriction No_Exception_Propagation is in effect, then we
280 -- can omit the exception handlers.
283 -- L1 : label; -- marked Exception_Junk
284 -- L2 : label; -- marked Exception_Junk
285 -- L3 : label; -- marked Exception_Junk
287 -- begin -- marked Exception_Junk
289 -- local_raise (excep1'address); -- was raise excep1
292 -- local_raise (excep2'address); -- was raise excep2
297 -- goto L3; -- skip handler if no raise, marked Exception_Junk
299 -- <<L1>> -- local excep target label, marked Exception_Junk
300 -- begin -- marked Exception_Junk
303 -- goto L3; -- marked Exception_Junk
305 -- <<L2>> -- marked Exception_Junk
306 -- begin -- marked Exception_Junk
310 -- <<L3>> -- marked Exception_Junk
313 -- The second case is for exceptions generated by the back end in one
314 -- of three situations:
316 -- 1. Front end generates N_Raise_xxx_Error node
317 -- 2. Front end sets Do_xxx_Check flag in subexpression node
318 -- 3. Back end detects a situation where an exception is appropriate
320 -- In all these cases, the current processing in gigi is to generate a
321 -- call to the appropriate Rcheck_xx routine (where xx encodes both the
322 -- exception message and the exception to be raised, Constraint_Error,
323 -- Program_Error, or Storage_Error.
325 -- We could handle some subcases of 1 using the same front end expansion
326 -- into gotos, but even for case 1, we can't handle all cases, since
327 -- generating gotos in the middle of expressions is not possible (it's
328 -- possible at the gigi/gcc level, but not at the level of the GNAT
331 -- In any case, it seems easier to have a scheme which handles all three
332 -- cases in a uniform manner. So here is how we proceed in this case.
334 -- This procedure detects all handlers for these three exceptions,
335 -- Constraint_Error, Program_Error and Storage_Error (including WHEN
336 -- OTHERS handlers that cover one or more of these cases).
338 -- If the handler meets the requirements for being the target of a local
339 -- raise, then the front end does the expansion described previously,
340 -- creating a label to be used as a goto target to raise the exception.
341 -- However, no attempt is made in the front end to convert any related
342 -- raise statements into gotos, e.g. all N_Raise_xxx_Error nodes are
343 -- left unchanged and passed to the back end.
345 -- Instead, the front end generates three nodes
347 -- N_Push_Constraint_Error_Label
348 -- N_Push_Program_Error_Label
349 -- N_Push_Storage_Error_Label
351 -- The Push node is generated at the start of the statements
352 -- covered by the handler, and has as a parameter the label to be
353 -- used as the raise target.
355 -- N_Pop_Constraint_Error_Label
356 -- N_Pop_Program_Error_Label
357 -- N_Pop_Storage_Error_Label
359 -- The Pop node is generated at the end of the covered statements
360 -- and undoes the effect of the preceding corresponding Push node.
362 -- In the case where the handler does NOT meet the requirements, the
363 -- front end will still generate the Push and Pop nodes, but the label
364 -- field in the Push node will be empty signifying that for this region
365 -- of code, no optimization is possible.
367 -- These Push/Pop nodes are inhibited if No_Exception_Handlers is set
368 -- since they are useless in this case, and in CodePeer mode, where
369 -- they serve no purpose and can intefere with the analysis.
371 -- The back end must maintain three stacks, one for each exception case,
372 -- the Push node pushes an entry onto the corresponding stack, and Pop
373 -- node pops off the entry. Then instead of calling Rcheck_nn, if the
374 -- corresponding top stack entry has an non-empty label, a goto is
375 -- generated. This goto should be preceded by a call to Local_Raise as
378 -- An example of this transformation is as follows, given:
381 -- A : Integer range 1 .. 10;
385 -- when Constraint_Error =>
389 -- gets transformed to:
392 -- A : Integer range 1 .. 10;
399 -- %push_constraint_error_label (L1)
400 -- R1b : constant long_long_integer := long_long_integer?(b) +
401 -- long_long_integer?(c);
402 -- [constraint_error when
403 -- not (R1b in -16#8000_0000# .. 16#7FFF_FFFF#)
404 -- "overflow check failed"]
405 -- a := integer?(R1b);
406 -- %pop_constraint_error_Label
410 -- when constraint_error =>
414 -- goto L2; -- skip handler when exception not raised
415 -- <<L1>> -- target label for local exception
420 -- Note: the generated labels and goto statements all have the flag
421 -- Exception_Junk set True, so that Sem_Ch6.Check_Returns will ignore
422 -- this generated exception stuff when checking for missing return
423 -- statements (see circuitry in Check_Statement_Sequence).
425 -- Note: All of the processing described above occurs only if
426 -- restriction No_Exception_Propagation applies or debug flag .g is
429 CE_Locally_Handled
: Boolean := False;
430 SE_Locally_Handled
: Boolean := False;
431 PE_Locally_Handled
: Boolean := False;
432 -- These three flags indicate whether a handler for the corresponding
433 -- exception (CE=Constraint_Error, SE=Storage_Error, PE=Program_Error)
434 -- is present. If so the switch is set to True, the Exception_Label
435 -- field of the corresponding handler is set, and appropriate Push
436 -- and Pop nodes are inserted into the code.
438 Local_Expansion_Required
: Boolean := False;
439 -- Set True if we have at least one handler requiring local raise
440 -- expansion as described above.
442 procedure Expand_Local_Exception_Handlers
is
444 procedure Add_Exception_Label
(H
: Node_Id
);
445 -- H is an exception handler. First check for an Exception_Label
446 -- already allocated for H. If none, allocate one, set the field in
447 -- the handler node, add the label declaration, and set the flag
448 -- Local_Expansion_Required. Note: if Local_Raise_Not_OK is set
449 -- the call has no effect and Exception_Label is left empty.
451 procedure Add_Label_Declaration
(L
: Entity_Id
);
452 -- Add an implicit declaration of the given label to the declaration
453 -- list in the parent of the current sequence of handled statements.
456 Exc_Locally_Handled
: in out Boolean;
457 -- Flag indicating whether a local handler for this exception
458 -- has already been generated.
460 with function Make_Push_Label
(Loc
: Source_Ptr
) return Node_Id
;
461 -- Function to create a Push_xxx_Label node
463 with function Make_Pop_Label
(Loc
: Source_Ptr
) return Node_Id
;
464 -- Function to create a Pop_xxx_Label node
466 procedure Generate_Push_Pop
(H
: Node_Id
);
467 -- Common code for Generate_Push_Pop_xxx below, used to generate an
468 -- exception label and Push/Pop nodes for Constraint_Error,
469 -- Program_Error, or Storage_Error.
471 -------------------------
472 -- Add_Exception_Label --
473 -------------------------
475 procedure Add_Exception_Label
(H
: Node_Id
) is
477 if No
(Exception_Label
(H
))
478 and then not Local_Raise_Not_OK
(H
)
479 and then not Special_Exception_Package_Used
481 Local_Expansion_Required
:= True;
484 L
: constant Entity_Id
:= Make_Temporary
(Sloc
(H
), 'L');
486 Set_Exception_Label
(H
, L
);
487 Add_Label_Declaration
(L
);
490 end Add_Exception_Label
;
492 ---------------------------
493 -- Add_Label_Declaration --
494 ---------------------------
496 procedure Add_Label_Declaration
(L
: Entity_Id
) is
497 P
: constant Node_Id
:= Parent
(HSS
);
499 Decl_L
: constant Node_Id
:=
500 Make_Implicit_Label_Declaration
(Loc
,
501 Defining_Identifier
=> L
);
504 if Declarations
(P
) = No_List
then
505 Set_Declarations
(P
, Empty_List
);
508 Append
(Decl_L
, Declarations
(P
));
510 end Add_Label_Declaration
;
512 -----------------------
513 -- Generate_Push_Pop --
514 -----------------------
516 procedure Generate_Push_Pop
(H
: Node_Id
) is
518 if Restriction_Active
(No_Exception_Handlers
)
519 or else CodePeer_Mode
524 if Exc_Locally_Handled
then
527 Exc_Locally_Handled
:= True;
530 Add_Exception_Label
(H
);
533 F
: constant Node_Id
:= First
(Statements
(HSS
));
534 L
: constant Node_Id
:= Last
(Statements
(HSS
));
536 Push
: constant Node_Id
:= Make_Push_Label
(Sloc
(F
));
537 Pop
: constant Node_Id
:= Make_Pop_Label
(Sloc
(L
));
540 -- We make sure that a call to Get_Local_Raise_Call_Entity is
541 -- made during front end processing, so that when we need it
542 -- in the back end, it will already be available and loaded.
544 Discard_Node
(Get_Local_Raise_Call_Entity
);
546 -- Prepare and insert Push and Pop nodes
548 Set_Exception_Label
(Push
, Exception_Label
(H
));
549 Insert_Before
(F
, Push
);
552 Insert_After
(L
, Pop
);
555 end Generate_Push_Pop
;
557 -- Local declarations
559 Loc
: constant Source_Ptr
:= Sloc
(HSS
);
560 Stmts
: List_Id
:= No_List
;
564 procedure Generate_Push_Pop_For_Constraint_Error
is
565 new Generate_Push_Pop
566 (Exc_Locally_Handled
=> CE_Locally_Handled
,
567 Make_Push_Label
=> Make_Push_Constraint_Error_Label
,
568 Make_Pop_Label
=> Make_Pop_Constraint_Error_Label
);
569 -- If no Push/Pop has been generated for CE yet, then set the flag
570 -- CE_Locally_Handled, allocate an Exception_Label for handler H (if
571 -- not already done), and generate Push/Pop nodes for the exception
572 -- label at the start and end of the statements of HSS.
574 procedure Generate_Push_Pop_For_Program_Error
is
575 new Generate_Push_Pop
576 (Exc_Locally_Handled
=> PE_Locally_Handled
,
577 Make_Push_Label
=> Make_Push_Program_Error_Label
,
578 Make_Pop_Label
=> Make_Pop_Program_Error_Label
);
579 -- If no Push/Pop has been generated for PE yet, then set the flag
580 -- PE_Locally_Handled, allocate an Exception_Label for handler H (if
581 -- not already done), and generate Push/Pop nodes for the exception
582 -- label at the start and end of the statements of HSS.
584 procedure Generate_Push_Pop_For_Storage_Error
is
585 new Generate_Push_Pop
586 (Exc_Locally_Handled
=> SE_Locally_Handled
,
587 Make_Push_Label
=> Make_Push_Storage_Error_Label
,
588 Make_Pop_Label
=> Make_Pop_Storage_Error_Label
);
589 -- If no Push/Pop has been generated for SE yet, then set the flag
590 -- SE_Locally_Handled, allocate an Exception_Label for handler H (if
591 -- not already done), and generate Push/Pop nodes for the exception
592 -- label at the start and end of the statements of HSS.
594 -- Start of processing for Expand_Local_Exception_Handlers
597 -- No processing if all exception handlers will get removed
599 if Debug_Flag_Dot_X
then
603 -- See for each handler if we have any local raises to expand
605 Handler
:= First_Non_Pragma
(Handlrs
);
606 while Present
(Handler
) loop
608 -- Note, we do not test Local_Raise_Not_OK here, because in the
609 -- case of Push/Pop generation we want to generate push with a
610 -- null label. The Add_Exception_Label routine has no effect if
611 -- Local_Raise_Not_OK is set, so this works as required.
613 if Present
(Local_Raise_Statements
(Handler
)) then
614 Add_Exception_Label
(Handler
);
617 -- If we are doing local raise to goto optimization (restriction
618 -- No_Exception_Propagation set or debug flag .g set), then check
619 -- to see if handler handles CE, PE, SE and if so generate the
620 -- appropriate push/pop sequence for the back end.
623 or else Restriction_Active
(No_Exception_Propagation
))
624 and then Has_Local_Raise
(Handler
)
626 Choice
:= First
(Exception_Choices
(Handler
));
627 while Present
(Choice
) loop
628 if Nkind
(Choice
) = N_Others_Choice
629 and then not All_Others
(Choice
)
631 Generate_Push_Pop_For_Constraint_Error
(Handler
);
632 Generate_Push_Pop_For_Program_Error
(Handler
);
633 Generate_Push_Pop_For_Storage_Error
(Handler
);
635 elsif Is_Entity_Name
(Choice
) then
636 Excep
:= Get_Renamed_Entity
(Entity
(Choice
));
638 if Excep
= Standard_Constraint_Error
then
639 Generate_Push_Pop_For_Constraint_Error
(Handler
);
640 elsif Excep
= Standard_Program_Error
then
641 Generate_Push_Pop_For_Program_Error
(Handler
);
642 elsif Excep
= Standard_Storage_Error
then
643 Generate_Push_Pop_For_Storage_Error
(Handler
);
651 Next_Non_Pragma
(Handler
);
654 -- Nothing to do if no handlers requiring the goto transformation
656 if not (Local_Expansion_Required
) then
660 -- Prepare to do the transformation
663 -- L3 is the label to exit the HSS
665 L3_Dent
: constant Entity_Id
:= Make_Temporary
(Loc
, 'L');
667 Labl_L3
: constant Node_Id
:=
669 Identifier
=> New_Occurrence_Of
(L3_Dent
, Loc
));
675 Set_Exception_Junk
(Labl_L3
);
676 Add_Label_Declaration
(L3_Dent
);
678 -- Wrap existing statements and handlers in an inner block
681 Make_Block_Statement
(Loc
,
682 Handled_Statement_Sequence
=> Relocate_Node
(HSS
));
683 Set_Exception_Junk
(Blk_Stm
);
686 Make_Handled_Sequence_Of_Statements
(Loc
,
687 Statements
=> New_List
(Blk_Stm
),
688 End_Label
=> Relocate_Node
(End_Label
(HSS
))));
690 -- Set block statement as analyzed, we don't want to actually call
691 -- Analyze on this block, it would cause a recursion in exception
692 -- handler processing which would mess things up.
694 Set_Analyzed
(Blk_Stm
);
696 -- Now loop through the exception handlers to deal with those that
697 -- are targets of local raise statements.
699 Handler
:= First_Non_Pragma
(Handlrs
);
700 while Present
(Handler
) loop
701 if Present
(Exception_Label
(Handler
)) then
703 -- This handler needs the goto expansion
706 Loc
: constant Source_Ptr
:= Sloc
(Handler
);
708 -- L1 is the start label for this handler
710 L1_Dent
: constant Entity_Id
:= Exception_Label
(Handler
);
712 Labl_L1
: constant Node_Id
:=
715 New_Occurrence_Of
(L1_Dent
, Loc
));
717 -- Jump to L1 to be used as replacement for the original
718 -- handler (used in the case where exception propagation
721 Name_L1
: constant Node_Id
:=
722 New_Occurrence_Of
(L1_Dent
, Loc
);
724 Goto_L1
: constant Node_Id
:=
725 Make_Goto_Statement
(Loc
,
728 -- Jump to L3 to be used at the end of handler
730 Name_L3
: constant Node_Id
:=
731 New_Occurrence_Of
(L3_Dent
, Loc
);
733 Goto_L3
: constant Node_Id
:=
734 Make_Goto_Statement
(Loc
,
737 H_Stmts
: constant List_Id
:= Statements
(Handler
);
740 Set_Exception_Junk
(Labl_L1
);
741 Set_Exception_Junk
(Goto_L3
);
743 -- Note: we do NOT set Exception_Junk in Goto_L1, since
744 -- this is a real transfer of control that we want the
745 -- Sem_Ch6.Check_Returns procedure to recognize properly.
747 -- Replace handler by a goto L1. We can mark this as
748 -- analyzed since it is fully formed, and we don't
749 -- want it going through any further checks. We save
750 -- the last statement location in the goto L1 node for
751 -- the benefit of Sem_Ch6.Check_Returns.
753 Set_Statements
(Handler
, New_List
(Goto_L1
));
754 Set_Analyzed
(Goto_L1
);
755 Set_Etype
(Name_L1
, Standard_Void_Type
);
757 -- Now replace all the raise statements by goto L1
759 if Present
(Local_Raise_Statements
(Handler
)) then
760 Relmt
:= First_Elmt
(Local_Raise_Statements
(Handler
));
761 while Present
(Relmt
) loop
763 Raise_S
: constant Node_Id
:= Node
(Relmt
);
764 RLoc
: constant Source_Ptr
:= Sloc
(Raise_S
);
765 Name_L1
: constant Node_Id
:=
766 New_Occurrence_Of
(L1_Dent
, Loc
);
767 Goto_L1
: constant Node_Id
:=
768 Make_Goto_Statement
(RLoc
,
772 -- Replace raise by goto L1
774 Set_Analyzed
(Goto_L1
);
775 Set_Etype
(Name_L1
, Standard_Void_Type
);
776 Replace_Raise_By_Goto
(Raise_S
, Goto_L1
);
783 -- Add a goto L3 at end of statement list in block. The
784 -- first time, this is what skips over the exception
785 -- handlers in the normal case. Subsequent times, it
786 -- terminates the execution of the previous handler code,
787 -- and skips subsequent handlers.
789 Stmts
:= Statements
(HSS
);
791 Insert_After
(Last
(Stmts
), Goto_L3
);
792 Set_Analyzed
(Goto_L3
);
793 Set_Etype
(Name_L3
, Standard_Void_Type
);
795 -- Now we drop the label that marks the handler start,
796 -- followed by the statements of the handler.
798 Set_Etype
(Identifier
(Labl_L1
), Standard_Void_Type
);
800 Insert_After_And_Analyze
(Last
(Stmts
), Labl_L1
);
803 Loc
: constant Source_Ptr
:= Sloc
(First
(H_Stmts
));
804 Blk
: constant Node_Id
:=
805 Make_Block_Statement
(Loc
,
806 Handled_Statement_Sequence
=>
807 Make_Handled_Sequence_Of_Statements
(Loc
,
808 Statements
=> H_Stmts
));
810 Set_Exception_Junk
(Blk
);
811 Insert_After_And_Analyze
(Last
(Stmts
), Blk
);
815 -- Here if we have local raise statements but the handler is
816 -- not suitable for processing with a local raise. In this
817 -- case we have to generate possible diagnostics.
819 elsif Has_Local_Raise
(Handler
)
820 and then Local_Raise_Statements
(Handler
) /= No_Elist
822 Relmt
:= First_Elmt
(Local_Raise_Statements
(Handler
));
823 while Present
(Relmt
) loop
824 Warn_If_No_Propagation
(Node
(Relmt
));
832 -- Only remaining step is to drop the L3 label and we are done
834 Set_Etype
(Identifier
(Labl_L3
), Standard_Void_Type
);
836 -- If we had at least one handler, then we drop the label after
837 -- the last statement of that handler.
839 if Stmts
/= No_List
then
840 Insert_After_And_Analyze
(Last
(Stmts
), Labl_L3
);
842 -- Otherwise we have removed all the handlers (this results from
843 -- use of pragma Restrictions (No_Exception_Propagation), and we
844 -- drop the label at the end of the statements of the HSS.
847 Insert_After_And_Analyze
(Last
(Statements
(HSS
)), Labl_L3
);
852 end Expand_Local_Exception_Handlers
;
854 -----------------------------
855 -- Prepend_Call_To_Handler --
856 -----------------------------
858 procedure Prepend_Call_To_Handler
860 Args
: List_Id
:= No_List
)
862 Ent
: constant Entity_Id
:= RTE
(Proc
);
865 -- If we have no Entity, then we are probably in no run time mode or
866 -- some weird error has occurred. In either case do nothing. Note use
867 -- of No_Location to hide this code from the debugger, so single
868 -- stepping doesn't jump back and forth.
870 if Present
(Ent
) then
872 Call
: constant Node_Id
:=
873 Make_Procedure_Call_Statement
(No_Location
,
874 Name
=> New_Occurrence_Of
(RTE
(Proc
), No_Location
),
875 Parameter_Associations
=> Args
);
878 Prepend_To
(Statements
(Handler
), Call
);
879 Analyze
(Call
, Suppress
=> All_Checks
);
882 end Prepend_Call_To_Handler
;
884 ---------------------------
885 -- Replace_Raise_By_Goto --
886 ---------------------------
888 procedure Replace_Raise_By_Goto
(Raise_S
: Node_Id
; Goto_L1
: Node_Id
) is
889 Loc
: constant Source_Ptr
:= Sloc
(Raise_S
);
896 -- If we have a null statement, it means that there is no replacement
897 -- needed (typically this results from a suppressed check).
899 if Nkind
(Raise_S
) = N_Null_Statement
then
902 -- Test for Raise_xxx_Error
904 elsif Nkind
(Raise_S
) = N_Raise_Constraint_Error
then
905 Excep
:= Standard_Constraint_Error
;
906 Cond
:= Condition
(Raise_S
);
908 elsif Nkind
(Raise_S
) = N_Raise_Storage_Error
then
909 Excep
:= Standard_Storage_Error
;
910 Cond
:= Condition
(Raise_S
);
912 elsif Nkind
(Raise_S
) = N_Raise_Program_Error
then
913 Excep
:= Standard_Program_Error
;
914 Cond
:= Condition
(Raise_S
);
916 -- The only other possibility is a node that is or used to be a
917 -- simple raise statement.
920 Orig
:= Original_Node
(Raise_S
);
921 pragma Assert
(Nkind
(Orig
) = N_Raise_Statement
922 and then Present
(Name
(Orig
))
923 and then No
(Expression
(Orig
)));
924 Excep
:= Entity
(Name
(Orig
));
928 -- Here Excep is the exception to raise, and Cond is the condition
929 -- First prepare the call to Local_Raise (excep'address).
931 if RTE_Available
(RE_Local_Raise
) then
933 Make_Procedure_Call_Statement
(Loc
,
934 Name
=> New_Occurrence_Of
(RTE
(RE_Local_Raise
), Loc
),
935 Parameter_Associations
=> New_List
(
936 Unchecked_Convert_To
(RTE
(RE_Address
),
937 Make_Attribute_Reference
(Loc
,
938 Prefix
=> New_Occurrence_Of
(Excep
, Loc
),
939 Attribute_Name
=> Name_Identity
))));
941 -- Use null statement if Local_Raise not available
945 Make_Null_Statement
(Loc
);
948 -- If there is no condition, we rewrite as
951 -- Local_Raise (excep'Identity);
957 Make_Block_Statement
(Loc
,
958 Handled_Statement_Sequence
=>
959 Make_Handled_Sequence_Of_Statements
(Loc
,
960 Statements
=> New_List
(LR
, Goto_L1
))));
961 Set_Exception_Junk
(Raise_S
);
963 -- If there is a condition, we rewrite as
966 -- Local_Raise (excep'Identity);
972 Make_If_Statement
(Loc
,
974 Then_Statements
=> New_List
(LR
, Goto_L1
)));
978 end Replace_Raise_By_Goto
;
980 -- Start of processing for Expand_Exception_Handlers
983 Expand_Local_Exception_Handlers
;
985 -- Loop through handlers
987 Handler
:= First_Non_Pragma
(Handlrs
);
988 Handler_Loop
: while Present
(Handler
) loop
989 Process_Statements_For_Controlled_Objects
(Handler
);
991 Next_Handler
:= Next_Non_Pragma
(Handler
);
993 -- Remove source handler if gnat debug flag .x is set
995 if Debug_Flag_Dot_X
and then Comes_From_Source
(Handler
) then
998 -- Remove handler if no exception propagation, generating a warning
999 -- if a source generated handler was not the target of a local raise.
1002 if Restriction_Active
(No_Exception_Propagation
)
1003 and then not Has_Local_Raise
(Handler
)
1004 and then Comes_From_Source
(Handler
)
1005 and then Warn_On_Non_Local_Exception
1007 Warn_No_Exception_Propagation_Active
(Handler
);
1009 ("\?X?this handler can never be entered, "
1010 & "and has been removed", Handler
);
1013 if No_Exception_Propagation_Active
then
1016 -- Exception handler is active and retained and must be processed
1019 -- If an exception occurrence is present, then we must declare
1020 -- it and initialize it from the value stored in the TSD
1023 -- name : Exception_Occurrence;
1025 -- Save_Occurrence (name, Get_Current_Excep.all)
1029 -- This expansion is not performed when using GCC ZCX. Gigi
1030 -- will insert a call to initialize the choice parameter.
1032 if Present
(Choice_Parameter
(Handler
))
1033 and then (Exception_Mechanism
/= Back_End_Exceptions
1034 or else CodePeer_Mode
)
1037 Cparm
: constant Entity_Id
:= Choice_Parameter
(Handler
);
1038 Cloc
: constant Source_Ptr
:= Sloc
(Cparm
);
1039 Hloc
: constant Source_Ptr
:= Sloc
(Handler
);
1043 -- Note: No_Location used to hide code from the debugger,
1044 -- so single stepping doesn't jump back and forth.
1047 Make_Procedure_Call_Statement
(No_Location
,
1050 (RTE
(RE_Save_Occurrence
), No_Location
),
1051 Parameter_Associations
=> New_List
(
1052 New_Occurrence_Of
(Cparm
, No_Location
),
1053 Make_Explicit_Dereference
(No_Location
,
1055 Make_Function_Call
(No_Location
,
1057 Make_Explicit_Dereference
(No_Location
,
1060 (RTE
(RE_Get_Current_Excep
),
1063 Mark_Rewrite_Insertion
(Save
);
1064 Prepend
(Save
, Statements
(Handler
));
1067 Make_Object_Declaration
(Cloc
,
1068 Defining_Identifier
=> Cparm
,
1069 Object_Definition
=>
1071 (RTE
(RE_Exception_Occurrence
), Cloc
));
1072 Set_No_Initialization
(Obj_Decl
, True);
1075 Make_Exception_Handler
(Hloc
,
1076 Choice_Parameter
=> Empty
,
1077 Exception_Choices
=> Exception_Choices
(Handler
),
1078 Statements
=> New_List
(
1079 Make_Block_Statement
(Hloc
,
1080 Declarations
=> New_List
(Obj_Decl
),
1081 Handled_Statement_Sequence
=>
1082 Make_Handled_Sequence_Of_Statements
(Hloc
,
1083 Statements
=> Statements
(Handler
))))));
1085 -- Local raise statements can't occur, since exception
1086 -- handlers with choice parameters are not allowed when
1087 -- No_Exception_Propagation applies, so set attributes
1090 Set_Local_Raise_Statements
(Handler
, No_Elist
);
1091 Set_Local_Raise_Not_OK
(Handler
);
1094 (Statements
(Handler
), Suppress
=> All_Checks
);
1098 -- The processing at this point is rather different for the JVM
1099 -- case, so we completely separate the processing.
1101 -- For the VM case, we unconditionally call Update_Exception,
1102 -- passing a call to the intrinsic Current_Target_Exception
1103 -- (see JVM/.NET versions of Ada.Exceptions for details).
1105 if VM_Target
/= No_VM
then
1107 Arg
: constant Node_Id
:=
1108 Make_Function_Call
(Loc
,
1111 (RTE
(RE_Current_Target_Exception
), Loc
));
1113 Prepend_Call_To_Handler
1114 (RE_Update_Exception
, New_List
(Arg
));
1117 -- For the normal case, we have to worry about the state of
1118 -- abort deferral. Generally, we defer abort during runtime
1119 -- handling of exceptions. When control is passed to the
1120 -- handler, then in the normal case we undefer aborts. In
1121 -- any case this entire handling is relevant only if aborts
1125 and then Exception_Mechanism
/= Back_End_Exceptions
1127 -- There are some special cases in which we do not do the
1128 -- undefer. In particular a finalization (AT END) handler
1129 -- wants to operate with aborts still deferred.
1131 -- We also suppress the call if this is the special handler
1132 -- for Abort_Signal, since if we are aborting, we want to
1133 -- keep aborts deferred (one abort is enough).
1135 -- If abort really needs to be deferred the expander must
1136 -- add this call explicitly, see
1137 -- Expand_N_Asynchronous_Select.
1140 Nkind
(First
(Exception_Choices
(Handler
))) =
1144 or else Entity
(First
(Exception_Choices
(Handler
))) /=
1149 All_Others
(First
(Exception_Choices
(Handler
))))
1151 Prepend_Call_To_Handler
(RE_Abort_Undefer
);
1157 Handler
:= Next_Handler
;
1158 end loop Handler_Loop
;
1160 -- If all handlers got removed, then remove the list. Note we cannot
1161 -- reference HSS here, since expanding local handlers may have buried
1162 -- the handlers in an inner block.
1164 if Is_Empty_List
(Handlrs
) then
1165 Set_Exception_Handlers
(Parent
(Handlrs
), No_List
);
1167 end Expand_Exception_Handlers
;
1169 ------------------------------------
1170 -- Expand_N_Exception_Declaration --
1171 ------------------------------------
1174 -- exceptE : constant String := "A.B.EXCEP"; -- static data
1175 -- except : exception_data :=
1176 -- (Handled_By_Other => False,
1178 -- Name_Length => exceptE'Length,
1179 -- Full_Name => exceptE'Address,
1180 -- HTable_Ptr => null,
1181 -- Foreign_Data => null,
1182 -- Raise_Hook => null);
1184 -- (protecting test only needed if not at library level)
1186 -- exceptF : Boolean := True -- static data
1188 -- exceptF := False;
1189 -- Register_Exception (except'Unchecked_Access);
1192 procedure Expand_N_Exception_Declaration
(N
: Node_Id
) is
1193 GM
: constant Ghost_Mode_Type
:= Ghost_Mode
;
1194 Id
: constant Entity_Id
:= Defining_Identifier
(N
);
1195 Loc
: constant Source_Ptr
:= Sloc
(N
);
1197 Flag_Id
: Entity_Id
;
1200 procedure Force_Static_Allocation_Of_Referenced_Objects
1201 (Aggregate
: Node_Id
);
1202 -- A specialized solution to one particular case of an ugly problem
1204 -- The given aggregate includes an Unchecked_Conversion as one of the
1205 -- component values. The call to Analyze_And_Resolve below ends up
1206 -- calling Exp_Ch4.Expand_N_Unchecked_Type_Conversion, which may decide
1207 -- to introduce a (constant) temporary and then obtain the component
1208 -- value by evaluating the temporary.
1210 -- In the case of an exception declared within a subprogram (or any
1211 -- other dynamic scope), this is a bad transformation. The exception
1212 -- object is marked as being Statically_Allocated but the temporary is
1213 -- not. If the initial value of a Statically_Allocated declaration
1214 -- references a dynamically allocated object, this prevents static
1215 -- initialization of the object.
1217 -- We cope with this here by marking the temporary Statically_Allocated.
1218 -- It might seem cleaner to generalize this utility and then use it to
1219 -- enforce a rule that the entities referenced in the declaration of any
1220 -- "hoisted" (i.e., Is_Statically_Allocated and not Is_Library_Level)
1221 -- entity must also be either Library_Level or hoisted. It turns out
1222 -- that this would be incompatible with the current treatment of an
1223 -- object which is local to a subprogram, subject to an Export pragma,
1224 -- not subject to an address clause, and whose declaration contains
1225 -- references to other local (non-hoisted) objects (e.g., in the initial
1226 -- value expression).
1228 ---------------------------------------------------
1229 -- Force_Static_Allocation_Of_Referenced_Objects --
1230 ---------------------------------------------------
1232 procedure Force_Static_Allocation_Of_Referenced_Objects
1233 (Aggregate
: Node_Id
)
1235 function Fixup_Node
(N
: Node_Id
) return Traverse_Result
;
1236 -- If the given node references a dynamically allocated object, then
1237 -- correct the declaration of the object.
1243 function Fixup_Node
(N
: Node_Id
) return Traverse_Result
is
1245 if Nkind
(N
) in N_Has_Entity
1246 and then Present
(Entity
(N
))
1247 and then not Is_Library_Level_Entity
(Entity
(N
))
1249 -- Note: the following test is not needed but it seems cleaner
1250 -- to do this test (this would be more important if procedure
1251 -- Force_Static_Allocation_Of_Referenced_Objects recursively
1252 -- traversed the declaration of an entity after marking it as
1253 -- statically allocated).
1255 and then not Is_Statically_Allocated
(Entity
(N
))
1257 Set_Is_Statically_Allocated
(Entity
(N
));
1263 procedure Fixup_Tree
is new Traverse_Proc
(Fixup_Node
);
1265 -- Start of processing for Force_Static_Allocation_Of_Referenced_Objects
1268 Fixup_Tree
(Aggregate
);
1269 end Force_Static_Allocation_Of_Referenced_Objects
;
1271 -- Start of processing for Expand_N_Exception_Declaration
1274 -- There is no expansion needed when compiling for the JVM since the
1275 -- JVM has a built-in exception mechanism. See cil/gnatlib/a-except.ads
1278 if VM_Target
/= No_VM
then
1282 -- The exception declaration may be subject to pragma Ghost with policy
1283 -- Ignore. Set the mode now to ensure that any nodes generated during
1284 -- expansion are properly flagged as ignored Ghost.
1288 -- Definition of the external name: nam : constant String := "A.B.NAME";
1291 Make_Defining_Identifier
(Loc
, New_External_Name
(Chars
(Id
), 'E'));
1294 Make_Object_Declaration
(Loc
,
1295 Defining_Identifier
=> Ex_Id
,
1296 Constant_Present
=> True,
1297 Object_Definition
=> New_Occurrence_Of
(Standard_String
, Loc
),
1299 Make_String_Literal
(Loc
,
1300 Strval
=> Fully_Qualified_Name_String
(Id
))));
1302 Set_Is_Statically_Allocated
(Ex_Id
);
1304 -- Create the aggregate list for type Standard.Exception_Type:
1305 -- Handled_By_Other component: False
1308 Append_To
(L
, New_Occurrence_Of
(Standard_False
, Loc
));
1310 -- Lang component: 'A'
1313 Make_Character_Literal
(Loc
,
1315 Char_Literal_Value
=> UI_From_Int
(Character'Pos ('A'))));
1317 -- Name_Length component: Nam'Length
1320 Make_Attribute_Reference
(Loc
,
1321 Prefix
=> New_Occurrence_Of
(Ex_Id
, Loc
),
1322 Attribute_Name
=> Name_Length
));
1324 -- Full_Name component: Standard.A_Char!(Nam'Address)
1326 Append_To
(L
, Unchecked_Convert_To
(Standard_A_Char
,
1327 Make_Attribute_Reference
(Loc
,
1328 Prefix
=> New_Occurrence_Of
(Ex_Id
, Loc
),
1329 Attribute_Name
=> Name_Address
)));
1331 -- HTable_Ptr component: null
1333 Append_To
(L
, Make_Null
(Loc
));
1335 -- Foreign_Data component: null
1337 Append_To
(L
, Make_Null
(Loc
));
1339 -- Raise_Hook component: null
1341 Append_To
(L
, Make_Null
(Loc
));
1343 Set_Expression
(N
, Make_Aggregate
(Loc
, Expressions
=> L
));
1344 Analyze_And_Resolve
(Expression
(N
), Etype
(Id
));
1346 Force_Static_Allocation_Of_Referenced_Objects
(Expression
(N
));
1348 -- Register_Exception (except'Unchecked_Access);
1350 if not No_Exception_Handlers_Set
1351 and then not Restriction_Active
(No_Exception_Registration
)
1354 Make_Procedure_Call_Statement
(Loc
,
1356 New_Occurrence_Of
(RTE
(RE_Register_Exception
), Loc
),
1357 Parameter_Associations
=> New_List
(
1358 Unchecked_Convert_To
(RTE
(RE_Exception_Data_Ptr
),
1359 Make_Attribute_Reference
(Loc
,
1360 Prefix
=> New_Occurrence_Of
(Id
, Loc
),
1361 Attribute_Name
=> Name_Unrestricted_Access
)))));
1363 Set_Register_Exception_Call
(Id
, First
(L
));
1365 if not Is_Library_Level_Entity
(Id
) then
1367 Make_Defining_Identifier
(Loc
,
1368 Chars
=> New_External_Name
(Chars
(Id
), 'F'));
1371 Make_Object_Declaration
(Loc
,
1372 Defining_Identifier
=> Flag_Id
,
1373 Object_Definition
=>
1374 New_Occurrence_Of
(Standard_Boolean
, Loc
),
1376 New_Occurrence_Of
(Standard_True
, Loc
)));
1378 Set_Is_Statically_Allocated
(Flag_Id
);
1381 Make_Assignment_Statement
(Loc
,
1382 Name
=> New_Occurrence_Of
(Flag_Id
, Loc
),
1383 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
)));
1385 Insert_After_And_Analyze
(N
,
1386 Make_Implicit_If_Statement
(N
,
1387 Condition
=> New_Occurrence_Of
(Flag_Id
, Loc
),
1388 Then_Statements
=> L
));
1391 Insert_List_After_And_Analyze
(N
, L
);
1395 -- Restore the original Ghost mode once analysis and expansion have
1399 end Expand_N_Exception_Declaration
;
1401 ---------------------------------------------
1402 -- Expand_N_Handled_Sequence_Of_Statements --
1403 ---------------------------------------------
1405 procedure Expand_N_Handled_Sequence_Of_Statements
(N
: Node_Id
) is
1407 -- Expand exception handlers
1409 if Present
(Exception_Handlers
(N
))
1410 and then not Restriction_Active
(No_Exception_Handlers
)
1412 Expand_Exception_Handlers
(N
);
1415 -- If local exceptions are being expanded, the previous call will
1416 -- have rewritten the construct as a block and reanalyzed it. No
1417 -- further expansion is needed.
1419 if Analyzed
(N
) then
1423 -- Add clean up actions if required
1425 if not Nkind_In
(Parent
(N
), N_Package_Body
,
1427 N_Extended_Return_Statement
)
1428 and then not Delay_Cleanups
(Current_Scope
)
1430 -- No cleanup action needed in thunks associated with interfaces
1431 -- because they only displace the pointer to the object.
1433 and then not Is_Thunk
(Current_Scope
)
1435 Expand_Cleanup_Actions
(Parent
(N
));
1437 Set_First_Real_Statement
(N
, First
(Statements
(N
)));
1439 end Expand_N_Handled_Sequence_Of_Statements
;
1441 -------------------------------------
1442 -- Expand_N_Raise_Constraint_Error --
1443 -------------------------------------
1445 procedure Expand_N_Raise_Constraint_Error
(N
: Node_Id
) is
1447 -- We adjust the condition to deal with the C/Fortran boolean case. This
1448 -- may well not be necessary, as all such conditions are generated by
1449 -- the expander and probably are all standard boolean, but who knows
1450 -- what strange optimization in future may require this adjustment.
1452 Adjust_Condition
(Condition
(N
));
1454 -- Now deal with possible local raise handling
1456 Possible_Local_Raise
(N
, Standard_Constraint_Error
);
1457 end Expand_N_Raise_Constraint_Error
;
1459 -------------------------------
1460 -- Expand_N_Raise_Expression --
1461 -------------------------------
1463 procedure Expand_N_Raise_Expression
(N
: Node_Id
) is
1464 Loc
: constant Source_Ptr
:= Sloc
(N
);
1465 Typ
: constant Entity_Id
:= Etype
(N
);
1469 Possible_Local_Raise
(N
, Entity
(Name
(N
)));
1471 -- Later we must teach the back end/gigi how to deal with this, but
1472 -- for now we will assume the type is Standard_Boolean and transform
1476 -- raise X [with string]
1478 -- raise Constraint_Error;
1480 -- unless the flag Convert_To_Return_False is set, in which case
1481 -- the transformation is to:
1486 -- raise Constraint_Error;
1488 -- The raise constraint error can never be executed. It is just a dummy
1489 -- node that can be labeled with an arbitrary type.
1491 RCE
:= Make_Raise_Constraint_Error
(Loc
, Reason
=> CE_Explicit_Raise
);
1492 Set_Etype
(RCE
, Typ
);
1494 if Convert_To_Return_False
(N
) then
1496 Make_Expression_With_Actions
(Loc
,
1497 Actions
=> New_List
(
1498 Make_Simple_Return_Statement
(Loc
,
1499 Expression
=> New_Occurrence_Of
(Standard_False
, Loc
))),
1500 Expression
=> RCE
));
1504 Make_Expression_With_Actions
(Loc
,
1505 Actions
=> New_List
(
1506 Make_Raise_Statement
(Loc
,
1508 Expression
=> Expression
(N
))),
1509 Expression
=> RCE
));
1512 Analyze_And_Resolve
(N
, Typ
);
1513 end Expand_N_Raise_Expression
;
1515 ----------------------------------
1516 -- Expand_N_Raise_Program_Error --
1517 ----------------------------------
1519 procedure Expand_N_Raise_Program_Error
(N
: Node_Id
) is
1521 -- We adjust the condition to deal with the C/Fortran boolean case. This
1522 -- may well not be necessary, as all such conditions are generated by
1523 -- the expander and probably are all standard boolean, but who knows
1524 -- what strange optimization in future may require this adjustment.
1526 Adjust_Condition
(Condition
(N
));
1528 -- Now deal with possible local raise handling
1530 Possible_Local_Raise
(N
, Standard_Program_Error
);
1531 end Expand_N_Raise_Program_Error
;
1533 ------------------------------
1534 -- Expand_N_Raise_Statement --
1535 ------------------------------
1537 procedure Expand_N_Raise_Statement
(N
: Node_Id
) is
1538 Loc
: constant Source_Ptr
:= Sloc
(N
);
1546 -- Processing for locally handled exception (exclude reraise case)
1548 if Present
(Name
(N
)) and then Nkind
(Name
(N
)) = N_Identifier
then
1550 or else Restriction_Active
(No_Exception_Propagation
)
1552 -- If we have a local handler, then note that this is potentially
1553 -- able to be transformed into a goto statement.
1555 H
:= Find_Local_Handler
(Entity
(Name
(N
)), N
);
1558 if Local_Raise_Statements
(H
) = No_Elist
then
1559 Set_Local_Raise_Statements
(H
, New_Elmt_List
);
1562 -- Append the new entry if it is not there already. Sometimes
1563 -- we have situations where due to reexpansion, the same node
1564 -- is analyzed twice and would otherwise be added twice.
1566 Append_Unique_Elmt
(N
, Local_Raise_Statements
(H
));
1567 Set_Has_Local_Raise
(H
);
1569 -- If no local handler, then generate no propagation warning
1572 Warn_If_No_Propagation
(N
);
1578 -- If a string expression is present, then the raise statement is
1579 -- converted to a call:
1580 -- Raise_Exception (exception-name'Identity, string);
1581 -- and there is nothing else to do.
1583 if Present
(Expression
(N
)) then
1585 -- Adjust message to deal with Prefix_Exception_Messages. We only
1586 -- add the prefix to string literals, if the message is being
1587 -- constructed, we assume it already deals with uniqueness.
1589 if Prefix_Exception_Messages
1590 and then Nkind
(Expression
(N
)) = N_String_Literal
1593 Add_Source_Info
(Loc
, Name_Enclosing_Entity
);
1594 Add_Str_To_Name_Buffer
(": ");
1595 Add_String_To_Name_Buffer
(Strval
(Expression
(N
)));
1596 Rewrite
(Expression
(N
),
1597 Make_String_Literal
(Loc
, Name_Buffer
(1 .. Name_Len
)));
1598 Analyze_And_Resolve
(Expression
(N
), Standard_String
);
1601 -- Avoid passing exception-name'identity in runtimes in which this
1602 -- argument is not used. This avoids generating undefined references
1603 -- to these exceptions when compiling with no optimization
1605 if Configurable_Run_Time_On_Target
1606 and then (Restriction_Active
(No_Exception_Handlers
)
1608 Restriction_Active
(No_Exception_Propagation
))
1611 Make_Procedure_Call_Statement
(Loc
,
1612 Name
=> New_Occurrence_Of
(RTE
(RE_Raise_Exception
), Loc
),
1613 Parameter_Associations
=> New_List
(
1614 New_Occurrence_Of
(RTE
(RE_Null_Id
), Loc
),
1618 Make_Procedure_Call_Statement
(Loc
,
1619 Name
=> New_Occurrence_Of
(RTE
(RE_Raise_Exception
), Loc
),
1620 Parameter_Associations
=> New_List
(
1621 Make_Attribute_Reference
(Loc
,
1623 Attribute_Name
=> Name_Identity
),
1631 -- Remaining processing is for the case where no string expression is
1634 -- Don't expand a raise statement that does not come from source if we
1635 -- have already had configurable run-time violations, since most likely
1636 -- it will be junk cascaded nonsense.
1638 if Configurable_Run_Time_Violations
> 0
1639 and then not Comes_From_Source
(N
)
1644 -- Convert explicit raise of Program_Error, Constraint_Error, and
1645 -- Storage_Error into the corresponding raise (in High_Integrity_Mode
1646 -- all other raises will get normal expansion and be disallowed,
1647 -- but this is also faster in all modes). Propagate Comes_From_Source
1648 -- flag to the new node.
1650 if Present
(Name
(N
)) and then Nkind
(Name
(N
)) = N_Identifier
then
1651 Src
:= Comes_From_Source
(N
);
1653 if Entity
(Name
(N
)) = Standard_Constraint_Error
then
1655 Make_Raise_Constraint_Error
(Loc
, Reason
=> CE_Explicit_Raise
));
1656 Set_Comes_From_Source
(N
, Src
);
1660 elsif Entity
(Name
(N
)) = Standard_Program_Error
then
1662 Make_Raise_Program_Error
(Loc
, Reason
=> PE_Explicit_Raise
));
1663 Set_Comes_From_Source
(N
, Src
);
1667 elsif Entity
(Name
(N
)) = Standard_Storage_Error
then
1669 Make_Raise_Storage_Error
(Loc
, Reason
=> SE_Explicit_Raise
));
1670 Set_Comes_From_Source
(N
, Src
);
1676 -- Case of name present, in this case we expand raise name to
1678 -- Raise_Exception (name'Identity, location_string);
1680 -- where location_string identifies the file/line of the raise
1682 if Present
(Name
(N
)) then
1684 Id
: Entity_Id
:= Entity
(Name
(N
));
1688 Build_Location_String
(Loc
);
1690 -- If the exception is a renaming, use the exception that it
1691 -- renames (which might be a predefined exception, e.g.).
1693 if Present
(Renamed_Object
(Id
)) then
1694 Id
:= Renamed_Object
(Id
);
1697 -- Build a C-compatible string in case of no exception handlers,
1698 -- since this is what the last chance handler is expecting.
1700 if No_Exception_Handlers_Set
then
1702 -- Generate an empty message if configuration pragma
1703 -- Suppress_Exception_Locations is set for this unit.
1705 if Opt
.Exception_Locations_Suppressed
then
1708 Name_Len
:= Name_Len
+ 1;
1711 Name_Buffer
(Name_Len
) := ASCII
.NUL
;
1714 if Opt
.Exception_Locations_Suppressed
then
1718 Str
:= String_From_Name_Buffer
;
1720 -- Convert raise to call to the Raise_Exception routine
1723 Make_Procedure_Call_Statement
(Loc
,
1725 New_Occurrence_Of
(RTE
(RE_Raise_Exception
), Loc
),
1726 Parameter_Associations
=> New_List
(
1727 Make_Attribute_Reference
(Loc
,
1729 Attribute_Name
=> Name_Identity
),
1730 Make_String_Literal
(Loc
, Strval
=> Str
))));
1733 -- Case of no name present (reraise). We rewrite the raise to:
1735 -- Reraise_Occurrence_Always (EO);
1737 -- where EO is the current exception occurrence. If the current handler
1738 -- does not have a choice parameter specification, then we provide one.
1741 -- Bypass expansion to a run-time call when back-end exception
1742 -- handling is active, unless the target is a VM, CodePeer or
1743 -- GNATprove. In CodePeer, raising an exception is treated as an
1744 -- error, while in GNATprove all code with exceptions falls outside
1745 -- the subset of code which can be formally analyzed.
1747 if VM_Target
= No_VM
1748 and then not CodePeer_Mode
1749 and then Exception_Mechanism
= Back_End_Exceptions
1754 -- Find innermost enclosing exception handler (there must be one,
1755 -- since the semantics has already verified that this raise statement
1756 -- is valid, and a raise with no arguments is only permitted in the
1757 -- context of an exception handler.
1759 Ehand
:= Parent
(N
);
1760 while Nkind
(Ehand
) /= N_Exception_Handler
loop
1761 Ehand
:= Parent
(Ehand
);
1764 -- Make exception choice parameter if none present. Note that we do
1765 -- not need to put the entity on the entity chain, since no one will
1766 -- be referencing this entity by normal visibility methods.
1768 if No
(Choice_Parameter
(Ehand
)) then
1769 E
:= Make_Temporary
(Loc
, 'E');
1770 Set_Choice_Parameter
(Ehand
, E
);
1771 Set_Ekind
(E
, E_Variable
);
1772 Set_Etype
(E
, RTE
(RE_Exception_Occurrence
));
1773 Set_Scope
(E
, Current_Scope
);
1776 -- Now rewrite the raise as a call to Reraise. A special case arises
1777 -- if this raise statement occurs in the context of a handler for
1778 -- all others (i.e. an at end handler). in this case we avoid
1779 -- the call to defer abort, cleanup routines are expected to be
1780 -- called in this case with aborts deferred.
1783 Ech
: constant Node_Id
:= First
(Exception_Choices
(Ehand
));
1787 if Nkind
(Ech
) = N_Others_Choice
1788 and then All_Others
(Ech
)
1790 Ent
:= RTE
(RE_Reraise_Occurrence_No_Defer
);
1792 Ent
:= RTE
(RE_Reraise_Occurrence_Always
);
1796 Make_Procedure_Call_Statement
(Loc
,
1797 Name
=> New_Occurrence_Of
(Ent
, Loc
),
1798 Parameter_Associations
=> New_List
(
1799 New_Occurrence_Of
(Choice_Parameter
(Ehand
), Loc
))));
1804 end Expand_N_Raise_Statement
;
1806 ----------------------------------
1807 -- Expand_N_Raise_Storage_Error --
1808 ----------------------------------
1810 procedure Expand_N_Raise_Storage_Error
(N
: Node_Id
) is
1812 -- We adjust the condition to deal with the C/Fortran boolean case. This
1813 -- may well not be necessary, as all such conditions are generated by
1814 -- the expander and probably are all standard boolean, but who knows
1815 -- what strange optimization in future may require this adjustment.
1817 Adjust_Condition
(Condition
(N
));
1819 -- Now deal with possible local raise handling
1821 Possible_Local_Raise
(N
, Standard_Storage_Error
);
1822 end Expand_N_Raise_Storage_Error
;
1824 --------------------------
1825 -- Possible_Local_Raise --
1826 --------------------------
1828 procedure Possible_Local_Raise
(N
: Node_Id
; E
: Entity_Id
) is
1830 -- Nothing to do if local raise optimization not active
1832 if not Debug_Flag_Dot_G
1833 and then not Restriction_Active
(No_Exception_Propagation
)
1838 -- Nothing to do if original node was an explicit raise, because in
1839 -- that case, we already generated the required warning for the raise.
1841 if Nkind
(Original_Node
(N
)) = N_Raise_Statement
then
1845 -- Otherwise see if we have a local handler for the exception
1848 H
: constant Node_Id
:= Find_Local_Handler
(E
, N
);
1851 -- If so, mark that it has a local raise
1854 Set_Has_Local_Raise
(H
, True);
1856 -- Otherwise, if the No_Exception_Propagation restriction is active
1857 -- and the warning is enabled, generate the appropriate warnings.
1859 elsif Warn_On_Non_Local_Exception
1860 and then Restriction_Active
(No_Exception_Propagation
)
1862 Warn_No_Exception_Propagation_Active
(N
);
1864 if Configurable_Run_Time_Mode
then
1866 ("\?X?& may call Last_Chance_Handler", N
, E
);
1869 ("\?X?& may result in unhandled exception", N
, E
);
1873 end Possible_Local_Raise
;
1875 ------------------------
1876 -- Find_Local_Handler --
1877 ------------------------
1879 function Find_Local_Handler
1881 Nod
: Node_Id
) return Node_Id
1888 SSE
: Scope_Stack_Entry
renames Scope_Stack
.Table
(Scope_Stack
.Last
);
1889 -- This is used to test for wrapped actions below
1892 EHandle
: Entity_Id
;
1893 -- The entity Id's for the exception we are raising and handling, using
1894 -- the renamed exception if a Renamed_Entity is present.
1897 -- Never any local handler if all handlers removed
1899 if Debug_Flag_Dot_X
then
1903 -- Get the exception we are raising, allowing for renaming
1905 ERaise
:= Get_Renamed_Entity
(Ename
);
1907 -- We need to check if the node we are looking at is contained in
1910 -- Loop to search up the tree
1916 -- If we get to the top of the tree, or to a subprogram, task, entry,
1917 -- protected body, or accept statement without having found a
1918 -- matching handler, then there is no local handler.
1921 or else Nkind
(P
) = N_Subprogram_Body
1922 or else Nkind
(P
) = N_Task_Body
1923 or else Nkind
(P
) = N_Protected_Body
1924 or else Nkind
(P
) = N_Entry_Body
1925 or else Nkind
(P
) = N_Accept_Statement
1929 -- Test for handled sequence of statements with at least one
1930 -- exception handler which might be the one we are looking for.
1932 elsif Nkind
(P
) = N_Handled_Sequence_Of_Statements
1933 and then Present
(Exception_Handlers
(P
))
1935 -- Before we proceed we need to check if the node N is covered
1936 -- by the statement part of P rather than one of its exception
1937 -- handlers (an exception handler obviously does not cover its
1940 -- This test is more delicate than might be thought. It is not
1941 -- just a matter of checking the Statements (P), because the node
1942 -- might be waiting to be wrapped in a transient scope, in which
1943 -- case it will end up in the block statements, even though it
1944 -- is not there now.
1946 if Is_List_Member
(N
) then
1948 LCN
: constant List_Id
:= List_Containing
(N
);
1951 if LCN
= Statements
(P
)
1953 LCN
= SSE
.Actions_To_Be_Wrapped
(Before
)
1955 LCN
= SSE
.Actions_To_Be_Wrapped
(After
)
1957 LCN
= SSE
.Actions_To_Be_Wrapped
(Cleanup
)
1959 -- Loop through exception handlers
1961 H
:= First
(Exception_Handlers
(P
));
1962 while Present
(H
) loop
1964 -- Guard against other constructs appearing in the
1965 -- list of exception handlers.
1967 if Nkind
(H
) = N_Exception_Handler
then
1969 -- Loop through choices in one handler
1971 C
:= First
(Exception_Choices
(H
));
1972 while Present
(C
) loop
1974 -- Deal with others case
1976 if Nkind
(C
) = N_Others_Choice
then
1978 -- Matching others handler, but we need
1979 -- to ensure there is no choice parameter.
1980 -- If there is, then we don't have a local
1981 -- handler after all (since we do not allow
1982 -- choice parameters for local handlers).
1984 if No
(Choice_Parameter
(H
)) then
1990 -- If not others must be entity name
1992 elsif Nkind
(C
) /= N_Others_Choice
then
1993 pragma Assert
(Is_Entity_Name
(C
));
1994 pragma Assert
(Present
(Entity
(C
)));
1996 -- Get exception being handled, dealing with
1999 EHandle
:= Get_Renamed_Entity
(Entity
(C
));
2001 -- If match, then check choice parameter
2003 if ERaise
= EHandle
then
2004 if No
(Choice_Parameter
(H
)) then
2025 end Find_Local_Handler
;
2027 ---------------------------------
2028 -- Get_Local_Raise_Call_Entity --
2029 ---------------------------------
2031 -- Note: this is primary provided for use by the back end in generating
2032 -- calls to Local_Raise. But it would be too late in the back end to call
2033 -- RTE if this actually caused a load/analyze of the unit. So what we do
2034 -- is to ensure there is a dummy call to this function during front end
2035 -- processing so that the unit gets loaded then, and not later.
2037 Local_Raise_Call_Entity
: Entity_Id
;
2038 Local_Raise_Call_Entity_Set
: Boolean := False;
2040 function Get_Local_Raise_Call_Entity
return Entity_Id
is
2042 if not Local_Raise_Call_Entity_Set
then
2043 Local_Raise_Call_Entity_Set
:= True;
2045 if RTE_Available
(RE_Local_Raise
) then
2046 Local_Raise_Call_Entity
:= RTE
(RE_Local_Raise
);
2048 Local_Raise_Call_Entity
:= Empty
;
2052 return Local_Raise_Call_Entity
;
2053 end Get_Local_Raise_Call_Entity
;
2055 -----------------------------
2056 -- Get_RT_Exception_Entity --
2057 -----------------------------
2059 function Get_RT_Exception_Entity
(R
: RT_Exception_Code
) return Entity_Id
is
2062 when CE_Reason
=> return Standard_Constraint_Error
;
2063 when PE_Reason
=> return Standard_Program_Error
;
2064 when SE_Reason
=> return Standard_Storage_Error
;
2066 end Get_RT_Exception_Entity
;
2068 ---------------------------
2069 -- Get_RT_Exception_Name --
2070 ---------------------------
2072 procedure Get_RT_Exception_Name
(Code
: RT_Exception_Code
) is
2075 when CE_Access_Check_Failed
=>
2076 Add_Str_To_Name_Buffer
("CE_Access_Check");
2077 when CE_Access_Parameter_Is_Null
=>
2078 Add_Str_To_Name_Buffer
("CE_Null_Access_Parameter");
2079 when CE_Discriminant_Check_Failed
=>
2080 Add_Str_To_Name_Buffer
("CE_Discriminant_Check");
2081 when CE_Divide_By_Zero
=>
2082 Add_Str_To_Name_Buffer
("CE_Divide_By_Zero");
2083 when CE_Explicit_Raise
=>
2084 Add_Str_To_Name_Buffer
("CE_Explicit_Raise");
2085 when CE_Index_Check_Failed
=>
2086 Add_Str_To_Name_Buffer
("CE_Index_Check");
2087 when CE_Invalid_Data
=>
2088 Add_Str_To_Name_Buffer
("CE_Invalid_Data");
2089 when CE_Length_Check_Failed
=>
2090 Add_Str_To_Name_Buffer
("CE_Length_Check");
2091 when CE_Null_Exception_Id
=>
2092 Add_Str_To_Name_Buffer
("CE_Null_Exception_Id");
2093 when CE_Null_Not_Allowed
=>
2094 Add_Str_To_Name_Buffer
("CE_Null_Not_Allowed");
2095 when CE_Overflow_Check_Failed
=>
2096 Add_Str_To_Name_Buffer
("CE_Overflow_Check");
2097 when CE_Partition_Check_Failed
=>
2098 Add_Str_To_Name_Buffer
("CE_Partition_Check");
2099 when CE_Range_Check_Failed
=>
2100 Add_Str_To_Name_Buffer
("CE_Range_Check");
2101 when CE_Tag_Check_Failed
=>
2102 Add_Str_To_Name_Buffer
("CE_Tag_Check");
2104 when PE_Access_Before_Elaboration
=>
2105 Add_Str_To_Name_Buffer
("PE_Access_Before_Elaboration");
2106 when PE_Accessibility_Check_Failed
=>
2107 Add_Str_To_Name_Buffer
("PE_Accessibility_Check");
2108 when PE_Address_Of_Intrinsic
=>
2109 Add_Str_To_Name_Buffer
("PE_Address_Of_Intrinsic");
2110 when PE_Aliased_Parameters
=>
2111 Add_Str_To_Name_Buffer
("PE_Aliased_Parameters");
2112 when PE_All_Guards_Closed
=>
2113 Add_Str_To_Name_Buffer
("PE_All_Guards_Closed");
2114 when PE_Bad_Predicated_Generic_Type
=>
2115 Add_Str_To_Name_Buffer
("PE_Bad_Predicated_Generic_Type");
2116 when PE_Current_Task_In_Entry_Body
=>
2117 Add_Str_To_Name_Buffer
("PE_Current_Task_In_Entry_Body");
2118 when PE_Duplicated_Entry_Address
=>
2119 Add_Str_To_Name_Buffer
("PE_Duplicated_Entry_Address");
2120 when PE_Explicit_Raise
=>
2121 Add_Str_To_Name_Buffer
("PE_Explicit_Raise");
2122 when PE_Finalize_Raised_Exception
=>
2123 Add_Str_To_Name_Buffer
("PE_Finalize_Raised_Exception");
2124 when PE_Implicit_Return
=>
2125 Add_Str_To_Name_Buffer
("PE_Implicit_Return");
2126 when PE_Misaligned_Address_Value
=>
2127 Add_Str_To_Name_Buffer
("PE_Misaligned_Address_Value");
2128 when PE_Missing_Return
=>
2129 Add_Str_To_Name_Buffer
("PE_Missing_Return");
2130 when PE_Non_Transportable_Actual
=>
2131 Add_Str_To_Name_Buffer
("PE_Non_Transportable_Actual");
2132 when PE_Overlaid_Controlled_Object
=>
2133 Add_Str_To_Name_Buffer
("PE_Overlaid_Controlled_Object");
2134 when PE_Potentially_Blocking_Operation
=>
2135 Add_Str_To_Name_Buffer
("PE_Potentially_Blocking_Operation");
2136 when PE_Stream_Operation_Not_Allowed
=>
2137 Add_Str_To_Name_Buffer
("PE_Stream_Operation_Not_Allowed");
2138 when PE_Stubbed_Subprogram_Called
=>
2139 Add_Str_To_Name_Buffer
("PE_Stubbed_Subprogram_Called");
2140 when PE_Unchecked_Union_Restriction
=>
2141 Add_Str_To_Name_Buffer
("PE_Unchecked_Union_Restriction");
2143 when SE_Empty_Storage_Pool
=>
2144 Add_Str_To_Name_Buffer
("SE_Empty_Storage_Pool");
2145 when SE_Explicit_Raise
=>
2146 Add_Str_To_Name_Buffer
("SE_Explicit_Raise");
2147 when SE_Infinite_Recursion
=>
2148 Add_Str_To_Name_Buffer
("SE_Infinite_Recursion");
2149 when SE_Object_Too_Large
=>
2150 Add_Str_To_Name_Buffer
("SE_Object_Too_Large");
2152 end Get_RT_Exception_Name
;
2154 ----------------------------
2155 -- Warn_If_No_Propagation --
2156 ----------------------------
2158 procedure Warn_If_No_Propagation
(N
: Node_Id
) is
2160 if Restriction_Check_Required
(No_Exception_Propagation
)
2161 and then Warn_On_Non_Local_Exception
2163 Warn_No_Exception_Propagation_Active
(N
);
2165 if Configurable_Run_Time_Mode
then
2167 ("\?X?Last_Chance_Handler will be called on exception", N
);
2170 ("\?X?execution may raise unhandled exception", N
);
2173 end Warn_If_No_Propagation
;
2175 ------------------------------------------
2176 -- Warn_No_Exception_Propagation_Active --
2177 ------------------------------------------
2179 procedure Warn_No_Exception_Propagation_Active
(N
: Node_Id
) is
2182 ("?X?pragma Restrictions (No_Exception_Propagation) in effect", N
);
2183 end Warn_No_Exception_Propagation_Active
;