2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ada / exp_ch11.adb
blob16e6544d281b6ebff2047a908c8061957d8c156c
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-2003 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
21 -- --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 -- --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Casing; use Casing;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Errout; use Errout;
32 with Exp_Ch7; use Exp_Ch7;
33 with Exp_Util; use Exp_Util;
34 with Hostparm; use Hostparm;
35 with Inline; use Inline;
36 with Lib; use Lib;
37 with Namet; use Namet;
38 with Nlists; use Nlists;
39 with Nmake; use Nmake;
40 with Opt; use Opt;
41 with Rtsfind; use Rtsfind;
42 with Restrict; use Restrict;
43 with Sem; use Sem;
44 with Sem_Ch5; use Sem_Ch5;
45 with Sem_Ch8; use Sem_Ch8;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sinfo; use Sinfo;
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 Uname; use Uname;
58 package body Exp_Ch11 is
60 SD_List : List_Id;
61 -- This list gathers the values SDn'Unrestricted_Access used to
62 -- construct the unit exception table. It is set to Empty_List if
63 -- there are no subprogram descriptors.
65 -----------------------
66 -- Local Subprograms --
67 -----------------------
69 procedure Expand_Exception_Handler_Tables (HSS : Node_Id);
70 -- Subsidiary procedure called by Expand_Exception_Handlers if zero
71 -- cost exception handling is installed for this target. Replaces the
72 -- exception handler structure with appropriate labeled code and tables
73 -- that allow the zero cost exception handling circuits to find the
74 -- correct handler (see unit Ada.Exceptions for details).
76 procedure Generate_Subprogram_Descriptor
77 (N : Node_Id;
78 Loc : Source_Ptr;
79 Spec : Entity_Id;
80 Slist : List_Id);
81 -- Procedure called to generate a subprogram descriptor. N is the
82 -- subprogram body node or, in the case of an imported subprogram, is
83 -- Empty, and Spec is the entity of the sunprogram. For details of the
84 -- required structure, see package System.Exceptions. The generated
85 -- subprogram descriptor is appended to Slist. Loc provides the
86 -- source location to be used for the generated descriptor.
88 ---------------------------
89 -- Expand_At_End_Handler --
90 ---------------------------
92 -- For a handled statement sequence that has a cleanup (At_End_Proc
93 -- field set), an exception handler of the following form is required:
95 -- exception
96 -- when all others =>
97 -- cleanup call
98 -- raise;
100 -- Note: this exception handler is treated rather specially by
101 -- subsequent expansion in two respects:
103 -- The normal call to Undefer_Abort is omitted
104 -- The raise call does not do Defer_Abort
106 -- This is because the current tasking code seems to assume that
107 -- the call to the cleanup routine that is made from an exception
108 -- handler for the abort signal is called with aborts deferred.
110 -- This expansion is only done if we have front end exception handling.
111 -- If we have back end exception handling, then the AT END handler is
112 -- left alone, and cleanups (including the exceptional case) are handled
113 -- by the back end.
115 -- In the front end case, the exception handler described above handles
116 -- the exceptional case. The AT END handler is left in the generated tree
117 -- and the code generator (e.g. gigi) must still handle proper generation
118 -- of cleanup calls for the non-exceptional case.
120 procedure Expand_At_End_Handler (HSS : Node_Id; Block : Node_Id) is
121 Clean : constant Entity_Id := Entity (At_End_Proc (HSS));
122 Loc : constant Source_Ptr := Sloc (Clean);
123 Ohandle : Node_Id;
124 Stmnts : List_Id;
126 begin
127 pragma Assert (Present (Clean));
128 pragma Assert (No (Exception_Handlers (HSS)));
130 -- Don't expand if back end exception handling active
132 if Exception_Mechanism = Back_End_ZCX_Exceptions then
133 return;
134 end if;
136 -- Don't expand an At End handler if we have already had configurable
137 -- run-time violations, since likely this will just be a matter of
138 -- generating useless cascaded messages
140 if Configurable_Run_Time_Violations > 0 then
141 return;
142 end if;
144 if Restrictions (No_Exception_Handlers) then
145 return;
146 end if;
148 if Present (Block) then
149 New_Scope (Block);
150 end if;
152 Ohandle :=
153 Make_Others_Choice (Loc);
154 Set_All_Others (Ohandle);
156 Stmnts := New_List (
157 Make_Procedure_Call_Statement (Loc,
158 Name => New_Occurrence_Of (Clean, Loc)),
159 Make_Raise_Statement (Loc));
161 Set_Exception_Handlers (HSS, New_List (
162 Make_Exception_Handler (Loc,
163 Exception_Choices => New_List (Ohandle),
164 Statements => Stmnts)));
166 Analyze_List (Stmnts, Suppress => All_Checks);
167 Expand_Exception_Handlers (HSS);
169 if Present (Block) then
170 Pop_Scope;
171 end if;
172 end Expand_At_End_Handler;
174 -------------------------------------
175 -- Expand_Exception_Handler_Tables --
176 -------------------------------------
178 -- See Ada.Exceptions specification for full details of the data
179 -- structures that we need to construct here. As an example of the
180 -- transformation that is required, given the structure:
182 -- declare
183 -- {declarations}
184 -- ..
185 -- begin
186 -- {statements-1}
187 -- ...
188 -- exception
189 -- when a | b =>
190 -- {statements-2}
191 -- ...
192 -- when others =>
193 -- {statements-3}
194 -- ...
195 -- end;
197 -- We transform this into:
199 -- declare
200 -- {declarations}
201 -- ...
202 -- L1 : label;
203 -- L2 : label;
204 -- L3 : label;
205 -- L4 : Label;
206 -- L5 : label;
208 -- begin
209 -- <<L1>>
210 -- {statements-1}
211 -- <<L2>>
213 -- exception
215 -- when a | b =>
216 -- <<L3>>
217 -- {statements-2}
219 -- HR2 : constant Handler_Record := (
220 -- Lo => L1'Address,
221 -- Hi => L2'Address,
222 -- Id => a'Identity,
223 -- Handler => L5'Address);
225 -- HR3 : constant Handler_Record := (
226 -- Lo => L1'Address,
227 -- Hi => L2'Address,
228 -- Id => b'Identity,
229 -- Handler => L4'Address);
231 -- when others =>
232 -- <<L4>>
233 -- {statements-3}
235 -- HR1 : constant Handler_Record := (
236 -- Lo => L1'Address,
237 -- Hi => L2'Address,
238 -- Id => Others_Id,
239 -- Handler => L4'Address);
240 -- end;
242 -- The exception handlers in the transformed version are marked with the
243 -- Zero_Cost_Handling flag set, and all gigi does in this case is simply
244 -- to put the handler code somewhere. It can optionally be put inline
245 -- between the goto L3 and the label <<L3>> (which is why we generate
246 -- that goto in the first place).
248 procedure Expand_Exception_Handler_Tables (HSS : Node_Id) is
249 Loc : constant Source_Ptr := Sloc (HSS);
250 Handlrs : constant List_Id := Exception_Handlers (HSS);
251 Stms : constant List_Id := Statements (HSS);
252 Handler : Node_Id;
254 Hlist : List_Id;
255 -- This is the list to which handlers are to be appended. It is
256 -- either the list for the enclosing subprogram, or the enclosing
257 -- selective accept statement (which will turn into a subprogram
258 -- during expansion later on).
260 L1 : constant Entity_Id :=
261 Make_Defining_Identifier (Loc,
262 Chars => New_Internal_Name ('L'));
264 L2 : constant Entity_Id :=
265 Make_Defining_Identifier (Loc,
266 Chars => New_Internal_Name ('L'));
268 Lnn : Entity_Id;
269 Choice : Node_Id;
270 E_Id : Node_Id;
271 HR_Ent : Node_Id;
272 HL_Ref : Node_Id;
273 Item : Node_Id;
275 Subp_Entity : Entity_Id;
276 -- This is the entity for the subprogram (or library level package)
277 -- to which the handler record is to be attached for later reference
278 -- in a subprogram descriptor for this entity.
280 procedure Append_To_Stms (N : Node_Id);
281 -- Append given statement to the end of the statements of the
282 -- handled sequence of statements and analyze it in place.
284 function Inside_Selective_Accept return Boolean;
285 -- This function is called if we are inside the scope of an entry
286 -- or task. It checks if the handler is appearing in the context
287 -- of a selective accept statement. If so, Hlist is set to
288 -- temporarily park the handlers in the N_Accept_Alternative.
289 -- node. They will subsequently be moved to the procedure entity
290 -- for the procedure built for this alternative. The statements that
291 -- follow the Accept within the alternative are not inside the Accept
292 -- for purposes of this test, and handlers that may appear within
293 -- them belong in the enclosing task procedure.
295 procedure Set_Hlist;
296 -- Sets the handler list corresponding to Subp_Entity
298 --------------------
299 -- Append_To_Stms --
300 --------------------
302 procedure Append_To_Stms (N : Node_Id) is
303 begin
304 Insert_After_And_Analyze (Last (Stms), N);
305 Set_Exception_Junk (N);
306 end Append_To_Stms;
308 -----------------------------
309 -- Inside_Selective_Accept --
310 -----------------------------
312 function Inside_Selective_Accept return Boolean is
313 Parnt : Node_Id;
314 Curr : Node_Id := HSS;
316 begin
317 Parnt := Parent (HSS);
318 while Nkind (Parnt) /= N_Compilation_Unit loop
319 if Nkind (Parnt) = N_Accept_Alternative
320 and then Curr = Accept_Statement (Parnt)
321 then
322 if Present (Accept_Handler_Records (Parnt)) then
323 Hlist := Accept_Handler_Records (Parnt);
324 else
325 Hlist := New_List;
326 Set_Accept_Handler_Records (Parnt, Hlist);
327 end if;
329 return True;
330 else
331 Curr := Parnt;
332 Parnt := Parent (Parnt);
333 end if;
334 end loop;
336 return False;
337 end Inside_Selective_Accept;
339 ---------------
340 -- Set_Hlist --
341 ---------------
343 procedure Set_Hlist is
344 begin
345 -- Never try to inline a subprogram with exception handlers
347 Set_Is_Inlined (Subp_Entity, False);
349 if Present (Subp_Entity)
350 and then Present (Handler_Records (Subp_Entity))
351 then
352 Hlist := Handler_Records (Subp_Entity);
353 else
354 Hlist := New_List;
355 Set_Handler_Records (Subp_Entity, Hlist);
356 end if;
357 end Set_Hlist;
359 -- Start of processing for Expand_Exception_Handler_Tables
361 begin
362 -- Nothing to do if this handler has already been processed
364 if Zero_Cost_Handling (HSS) then
365 return;
366 end if;
368 Set_Zero_Cost_Handling (HSS);
370 -- Find the parent subprogram or package scope containing this
371 -- exception frame. This should always find a real package or
372 -- subprogram. If it does not it will stop at Standard, but
373 -- this cannot legitimately occur.
375 -- We only stop at library level packages, for inner packages
376 -- we always attach handlers to the containing procedure.
378 Subp_Entity := Current_Scope;
379 Scope_Loop : loop
381 -- Never need tables expanded inside a generic template
383 if Is_Generic_Unit (Subp_Entity) then
384 return;
386 -- Stop if we reached containing subprogram. Go to protected
387 -- subprogram if there is one defined.
389 elsif Ekind (Subp_Entity) = E_Function
390 or else Ekind (Subp_Entity) = E_Procedure
391 then
392 if Present (Protected_Body_Subprogram (Subp_Entity)) then
393 Subp_Entity := Protected_Body_Subprogram (Subp_Entity);
394 end if;
396 Set_Hlist;
397 exit Scope_Loop;
399 -- Case of within an entry
401 elsif Is_Entry (Subp_Entity) then
403 -- Protected entry, use corresponding body subprogram
405 if Present (Protected_Body_Subprogram (Subp_Entity)) then
406 Subp_Entity := Protected_Body_Subprogram (Subp_Entity);
407 Set_Hlist;
408 exit Scope_Loop;
410 -- Check if we are within a selective accept alternative
412 elsif Inside_Selective_Accept then
414 -- As a side effect, Inside_Selective_Accept set Hlist,
415 -- in much the same manner as Set_Hlist, except that
416 -- the list involved was the one for the selective accept.
418 exit Scope_Loop;
419 end if;
421 -- Case of within library level package
423 elsif Ekind (Subp_Entity) = E_Package
424 and then Is_Compilation_Unit (Subp_Entity)
425 then
426 if Is_Body_Name (Unit_Name (Get_Code_Unit (HSS))) then
427 Subp_Entity := Body_Entity (Subp_Entity);
428 end if;
430 Set_Hlist;
431 exit Scope_Loop;
433 -- Task type case
435 elsif Ekind (Subp_Entity) = E_Task_Type then
437 -- Check if we are within a selective accept alternative
439 if Inside_Selective_Accept then
441 -- As a side effect, Inside_Selective_Accept set Hlist,
442 -- in much the same manner as Set_Hlist, except that the
443 -- list involved was the one for the selective accept.
445 exit Scope_Loop;
447 -- Stop if we reached task type with task body procedure,
448 -- use the task body procedure.
450 elsif Present (Get_Task_Body_Procedure (Subp_Entity)) then
451 Subp_Entity := Get_Task_Body_Procedure (Subp_Entity);
452 Set_Hlist;
453 exit Scope_Loop;
454 end if;
455 end if;
457 -- If we fall through, keep looking
459 Subp_Entity := Scope (Subp_Entity);
460 end loop Scope_Loop;
462 pragma Assert (Subp_Entity /= Standard_Standard);
464 -- Analyze standard labels
466 Analyze_Label_Entity (L1);
467 Analyze_Label_Entity (L2);
469 Insert_Before_And_Analyze (First (Stms),
470 Make_Label (Loc,
471 Identifier => New_Occurrence_Of (L1, Loc)));
472 Set_Exception_Junk (First (Stms));
474 Append_To_Stms (
475 Make_Label (Loc,
476 Identifier => New_Occurrence_Of (L2, Loc)));
478 -- Loop through exception handlers
480 Handler := First_Non_Pragma (Handlrs);
481 while Present (Handler) loop
482 Set_Zero_Cost_Handling (Handler);
484 -- Add label at start of handler, and goto at the end
486 Lnn :=
487 Make_Defining_Identifier (Loc,
488 Chars => New_Internal_Name ('L'));
490 Analyze_Label_Entity (Lnn);
492 Item :=
493 Make_Label (Loc,
494 Identifier => New_Occurrence_Of (Lnn, Loc));
495 Set_Exception_Junk (Item);
496 Insert_Before_And_Analyze (First (Statements (Handler)), Item);
498 -- Loop through choices
500 Choice := First (Exception_Choices (Handler));
501 while Present (Choice) loop
503 -- Others (or all others) choice
505 if Nkind (Choice) = N_Others_Choice then
506 if All_Others (Choice) then
507 E_Id := New_Occurrence_Of (RTE (RE_All_Others_Id), Loc);
508 else
509 E_Id := New_Occurrence_Of (RTE (RE_Others_Id), Loc);
510 end if;
512 -- Special case of VMS_Exception. Not clear what we will do
513 -- eventually here if and when we implement zero cost exceptions
514 -- on VMS. But at least for now, don't blow up trying to take
515 -- a garbage code address for such an exception.
517 elsif Is_VMS_Exception (Entity (Choice)) then
518 E_Id := New_Occurrence_Of (RTE (RE_Null_Id), Loc);
520 -- Normal case of specific exception choice
522 else
523 E_Id :=
524 Make_Attribute_Reference (Loc,
525 Prefix => New_Occurrence_Of (Entity (Choice), Loc),
526 Attribute_Name => Name_Identity);
527 end if;
529 HR_Ent :=
530 Make_Defining_Identifier (Loc,
531 Chars => New_Internal_Name ('H'));
533 HL_Ref :=
534 Make_Attribute_Reference (Loc,
535 Prefix => New_Occurrence_Of (HR_Ent, Loc),
536 Attribute_Name => Name_Unrestricted_Access);
538 -- Now we need to add the entry for the new handler record to
539 -- the list of handler records for the current subprogram.
541 -- Normally we end up generating the handler records in exactly
542 -- the right order. Here right order means innermost first,
543 -- since the table will be searched sequentially. Since we
544 -- generally expand from outside to inside, the order is just
545 -- what we want, and we need to append the new entry to the
546 -- end of the list.
548 -- However, there are exceptions, notably in the case where
549 -- a generic body is inserted later on. See for example the
550 -- case of ACVC test C37213J, which has the following form:
552 -- generic package x ... end x;
553 -- package body x is
554 -- begin
555 -- ...
556 -- exception (1)
557 -- ...
558 -- end x;
560 -- ...
562 -- declare
563 -- package q is new x;
564 -- begin
565 -- ...
566 -- exception (2)
567 -- ...
568 -- end;
570 -- In this case, we will expand exception handler (2) first,
571 -- since the expansion of (1) is delayed till later when the
572 -- generic body is inserted. But (1) belongs before (2) in
573 -- the chain.
575 -- Note that scopes are not totally ordered, because two
576 -- scopes can be in parallel blocks, so that it does not
577 -- matter what order these entries appear in. An ordering
578 -- relation exists if one scope is inside another, and what
579 -- we really want is some partial ordering.
581 -- A simple, not very efficient, but adequate algorithm to
582 -- achieve this partial ordering is to search the list for
583 -- the first entry containing the given scope, and put the
584 -- new entry just before it.
586 declare
587 New_Scop : constant Entity_Id := Current_Scope;
588 Ent : Node_Id;
590 begin
591 Ent := First (Hlist);
592 loop
593 -- If all searched, then we can just put the new
594 -- entry at the end of the list (it actually does
595 -- not matter where we put it in this case).
597 if No (Ent) then
598 Append_To (Hlist, HL_Ref);
599 exit;
601 -- If the current scope is within the scope of the
602 -- entry then insert the entry before to retain the
603 -- proper order as per above discussion.
605 -- Note that for equal entries, we just keep going,
606 -- which is fine, the entry will end up at the end
607 -- of the list where it belongs.
609 elsif Scope_Within
610 (New_Scop, Scope (Entity (Prefix (Ent))))
611 then
612 Insert_Before (Ent, HL_Ref);
613 exit;
615 -- Otherwise keep looking
617 else
618 Next (Ent);
619 end if;
620 end loop;
621 end;
623 Item :=
624 Make_Object_Declaration (Loc,
625 Defining_Identifier => HR_Ent,
626 Constant_Present => True,
627 Aliased_Present => True,
628 Object_Definition =>
629 New_Occurrence_Of (RTE (RE_Handler_Record), Loc),
631 Expression =>
632 Make_Aggregate (Loc,
633 Expressions => New_List (
634 Make_Attribute_Reference (Loc, -- Lo
635 Prefix => New_Occurrence_Of (L1, Loc),
636 Attribute_Name => Name_Address),
638 Make_Attribute_Reference (Loc, -- Hi
639 Prefix => New_Occurrence_Of (L2, Loc),
640 Attribute_Name => Name_Address),
642 E_Id, -- Id
644 Make_Attribute_Reference (Loc,
645 Prefix => New_Occurrence_Of (Lnn, Loc), -- Handler
646 Attribute_Name => Name_Address))));
648 Set_Handler_List_Entry (Item, HL_Ref);
649 Set_Exception_Junk (Item);
650 Insert_After_And_Analyze (Last (Statements (Handler)), Item);
651 Set_Is_Statically_Allocated (HR_Ent);
653 -- If this is a late insertion (from body instance) it is being
654 -- inserted in the component list of an already analyzed aggre-
655 -- gate, and must be analyzed explicitly.
657 Analyze_And_Resolve (HL_Ref, RTE (RE_Handler_Record_Ptr));
659 Next (Choice);
660 end loop;
662 Next_Non_Pragma (Handler);
663 end loop;
664 end Expand_Exception_Handler_Tables;
666 -------------------------------
667 -- Expand_Exception_Handlers --
668 -------------------------------
670 procedure Expand_Exception_Handlers (HSS : Node_Id) is
671 Handlrs : constant List_Id := Exception_Handlers (HSS);
672 Loc : Source_Ptr;
673 Handler : Node_Id;
674 Others_Choice : Boolean;
675 Obj_Decl : Node_Id;
677 procedure Prepend_Call_To_Handler
678 (Proc : RE_Id;
679 Args : List_Id := No_List);
680 -- Routine to prepend a call to the procedure referenced by Proc at
681 -- the start of the handler code for the current Handler.
683 -----------------------------
684 -- Prepend_Call_To_Handler --
685 -----------------------------
687 procedure Prepend_Call_To_Handler
688 (Proc : RE_Id;
689 Args : List_Id := No_List)
691 Ent : constant Entity_Id := RTE (Proc);
693 begin
694 -- If we have no Entity, then we are probably in no run time mode
695 -- or some weird error has occured. In either case do do nothing!
697 if Present (Ent) then
698 declare
699 Call : constant Node_Id :=
700 Make_Procedure_Call_Statement (Loc,
701 Name => New_Occurrence_Of (RTE (Proc), Loc),
702 Parameter_Associations => Args);
704 begin
705 Prepend_To (Statements (Handler), Call);
706 Analyze (Call, Suppress => All_Checks);
707 end;
708 end if;
709 end Prepend_Call_To_Handler;
711 -- Start of processing for Expand_Exception_Handlers
713 begin
714 -- Loop through handlers
716 Handler := First_Non_Pragma (Handlrs);
717 Handler_Loop : while Present (Handler) loop
718 Loc := Sloc (Handler);
720 -- Remove source handler if gnat debug flag N is set
722 if Debug_Flag_Dot_X and then Comes_From_Source (Handler) then
723 declare
724 H : Node_Id := Handler;
725 begin
726 Next_Non_Pragma (Handler);
727 Remove (H);
728 goto Continue_Handler_Loop;
729 end;
730 end if;
733 -- If an exception occurrence is present, then we must declare it
734 -- and initialize it from the value stored in the TSD
736 -- declare
737 -- name : Exception_Occurrence;
739 -- begin
740 -- Save_Occurrence (name, Get_Current_Excep.all)
741 -- ...
742 -- end;
744 if Present (Choice_Parameter (Handler)) then
745 declare
746 Cparm : constant Entity_Id := Choice_Parameter (Handler);
747 Clc : constant Source_Ptr := Sloc (Cparm);
748 Save : Node_Id;
750 begin
751 Save :=
752 Make_Procedure_Call_Statement (Loc,
753 Name =>
754 New_Occurrence_Of (RTE (RE_Save_Occurrence), Loc),
755 Parameter_Associations => New_List (
756 New_Occurrence_Of (Cparm, Clc),
757 Make_Explicit_Dereference (Loc,
758 Make_Function_Call (Loc,
759 Name => Make_Explicit_Dereference (Loc,
760 New_Occurrence_Of
761 (RTE (RE_Get_Current_Excep), Loc))))));
763 Mark_Rewrite_Insertion (Save);
764 Prepend (Save, Statements (Handler));
766 Obj_Decl :=
767 Make_Object_Declaration (Clc,
768 Defining_Identifier => Cparm,
769 Object_Definition =>
770 New_Occurrence_Of
771 (RTE (RE_Exception_Occurrence), Clc));
772 Set_No_Initialization (Obj_Decl, True);
774 Rewrite (Handler,
775 Make_Exception_Handler (Loc,
776 Exception_Choices => Exception_Choices (Handler),
778 Statements => New_List (
779 Make_Block_Statement (Loc,
780 Declarations => New_List (Obj_Decl),
781 Handled_Statement_Sequence =>
782 Make_Handled_Sequence_Of_Statements (Loc,
783 Statements => Statements (Handler))))));
785 Analyze_List (Statements (Handler), Suppress => All_Checks);
786 end;
787 end if;
789 -- The processing at this point is rather different for the
790 -- JVM case, so we completely separate the processing.
792 -- For the JVM case, we unconditionally call Update_Exception,
793 -- passing a call to the intrinsic function Current_Target_Exception
794 -- (see JVM version of Ada.Exceptions in 4jexcept.adb for details).
796 if Hostparm.Java_VM then
797 declare
798 Arg : constant Node_Id :=
799 Make_Function_Call (Loc,
800 Name => New_Occurrence_Of
801 (RTE (RE_Current_Target_Exception), Loc));
802 begin
803 Prepend_Call_To_Handler (RE_Update_Exception, New_List (Arg));
804 end;
806 -- For the normal case, we have to worry about the state of abort
807 -- deferral. Generally, we defer abort during runtime handling of
808 -- exceptions. When control is passed to the handler, then in the
809 -- normal case we undefer aborts. In any case this entire handling
810 -- is relevant only if aborts are allowed!
812 elsif Abort_Allowed then
814 -- There are some special cases in which we do not do the
815 -- undefer. In particular a finalization (AT END) handler
816 -- wants to operate with aborts still deferred.
818 -- We also suppress the call if this is the special handler
819 -- for Abort_Signal, since if we are aborting, we want to keep
820 -- aborts deferred (one abort is enough thank you very much :-)
822 -- If abort really needs to be deferred the expander must add
823 -- this call explicitly, see Exp_Ch9.Expand_N_Asynchronous_Select.
825 Others_Choice :=
826 Nkind (First (Exception_Choices (Handler))) = N_Others_Choice;
828 if (Others_Choice
829 or else Entity (First (Exception_Choices (Handler))) /=
830 Stand.Abort_Signal)
831 and then not
832 (Others_Choice
833 and then All_Others (First (Exception_Choices (Handler))))
834 and then Abort_Allowed
835 then
836 Prepend_Call_To_Handler (RE_Abort_Undefer);
837 end if;
838 end if;
840 Next_Non_Pragma (Handler);
842 <<Continue_Handler_Loop>>
843 null;
844 end loop Handler_Loop;
846 -- If all handlers got removed by gnatdN, then remove the list
848 if Debug_Flag_Dot_X
849 and then Is_Empty_List (Exception_Handlers (HSS))
850 then
851 Set_Exception_Handlers (HSS, No_List);
852 end if;
854 -- The last step for expanding exception handlers is to expand the
855 -- exception tables if zero cost exception handling is active.
857 if Exception_Mechanism = Front_End_ZCX_Exceptions then
858 Expand_Exception_Handler_Tables (HSS);
859 end if;
860 end Expand_Exception_Handlers;
862 ------------------------------------
863 -- Expand_N_Exception_Declaration --
864 ------------------------------------
866 -- Generates:
867 -- exceptE : constant String := "A.B.EXCEP"; -- static data
868 -- except : exception_data := (
869 -- Handled_By_Other => False,
870 -- Lang => 'A',
871 -- Name_Length => exceptE'Length,
872 -- Full_Name => exceptE'Address,
873 -- HTable_Ptr => null,
874 -- Import_Code => 0,
875 -- Raise_Hook => null,
876 -- );
878 -- (protecting test only needed if not at library level)
880 -- exceptF : Boolean := True -- static data
881 -- if exceptF then
882 -- exceptF := False;
883 -- Register_Exception (except'Unchecked_Access);
884 -- end if;
886 procedure Expand_N_Exception_Declaration (N : Node_Id) is
887 Loc : constant Source_Ptr := Sloc (N);
888 Id : constant Entity_Id := Defining_Identifier (N);
889 L : List_Id := New_List;
890 Flag_Id : Entity_Id;
892 Name_Exname : constant Name_Id := New_External_Name (Chars (Id), 'E');
893 Exname : constant Node_Id :=
894 Make_Defining_Identifier (Loc, Name_Exname);
896 begin
897 -- There is no expansion needed when compiling for the JVM since the
898 -- JVM has a built-in exception mechanism. See 4jexcept.ads for details.
900 if Hostparm.Java_VM then
901 return;
902 end if;
904 -- Definition of the external name: nam : constant String := "A.B.NAME";
906 Insert_Action (N,
907 Make_Object_Declaration (Loc,
908 Defining_Identifier => Exname,
909 Constant_Present => True,
910 Object_Definition => New_Occurrence_Of (Standard_String, Loc),
911 Expression => Make_String_Literal (Loc, Full_Qualified_Name (Id))));
913 Set_Is_Statically_Allocated (Exname);
915 -- Create the aggregate list for type Standard.Exception_Type:
916 -- Handled_By_Other component: False
918 Append_To (L, New_Occurrence_Of (Standard_False, Loc));
920 -- Lang component: 'A'
922 Append_To (L,
923 Make_Character_Literal (Loc, Name_uA, Get_Char_Code ('A')));
925 -- Name_Length component: Nam'Length
927 Append_To (L,
928 Make_Attribute_Reference (Loc,
929 Prefix => New_Occurrence_Of (Exname, Loc),
930 Attribute_Name => Name_Length));
932 -- Full_Name component: Standard.A_Char!(Nam'Address)
934 Append_To (L, Unchecked_Convert_To (Standard_A_Char,
935 Make_Attribute_Reference (Loc,
936 Prefix => New_Occurrence_Of (Exname, Loc),
937 Attribute_Name => Name_Address)));
939 -- HTable_Ptr component: null
941 Append_To (L, Make_Null (Loc));
943 -- Import_Code component: 0
945 Append_To (L, Make_Integer_Literal (Loc, 0));
947 -- Raise_Hook component: null
949 Append_To (L, Make_Null (Loc));
951 Set_Expression (N, Make_Aggregate (Loc, Expressions => L));
952 Analyze_And_Resolve (Expression (N), Etype (Id));
954 -- Register_Exception (except'Unchecked_Access);
956 if not Restrictions (No_Exception_Handlers)
957 and then not Restrictions (No_Exception_Registration)
958 then
959 L := New_List (
960 Make_Procedure_Call_Statement (Loc,
961 Name => New_Occurrence_Of (RTE (RE_Register_Exception), Loc),
962 Parameter_Associations => New_List (
963 Unchecked_Convert_To (RTE (RE_Exception_Data_Ptr),
964 Make_Attribute_Reference (Loc,
965 Prefix => New_Occurrence_Of (Id, Loc),
966 Attribute_Name => Name_Unrestricted_Access)))));
968 Set_Register_Exception_Call (Id, First (L));
970 if not Is_Library_Level_Entity (Id) then
971 Flag_Id := Make_Defining_Identifier (Loc,
972 New_External_Name (Chars (Id), 'F'));
974 Insert_Action (N,
975 Make_Object_Declaration (Loc,
976 Defining_Identifier => Flag_Id,
977 Object_Definition =>
978 New_Occurrence_Of (Standard_Boolean, Loc),
979 Expression =>
980 New_Occurrence_Of (Standard_True, Loc)));
982 Set_Is_Statically_Allocated (Flag_Id);
984 Append_To (L,
985 Make_Assignment_Statement (Loc,
986 Name => New_Occurrence_Of (Flag_Id, Loc),
987 Expression => New_Occurrence_Of (Standard_False, Loc)));
989 Insert_After_And_Analyze (N,
990 Make_Implicit_If_Statement (N,
991 Condition => New_Occurrence_Of (Flag_Id, Loc),
992 Then_Statements => L));
994 else
995 Insert_List_After_And_Analyze (N, L);
996 end if;
997 end if;
999 end Expand_N_Exception_Declaration;
1001 ---------------------------------------------
1002 -- Expand_N_Handled_Sequence_Of_Statements --
1003 ---------------------------------------------
1005 procedure Expand_N_Handled_Sequence_Of_Statements (N : Node_Id) is
1006 begin
1007 if Present (Exception_Handlers (N))
1008 and then not Restrictions (No_Exception_Handlers)
1009 then
1010 Expand_Exception_Handlers (N);
1011 end if;
1013 -- The following code needs comments ???
1015 if Nkind (Parent (N)) /= N_Package_Body
1016 and then Nkind (Parent (N)) /= N_Accept_Statement
1017 and then not Delay_Cleanups (Current_Scope)
1018 then
1019 Expand_Cleanup_Actions (Parent (N));
1020 else
1021 Set_First_Real_Statement (N, First (Statements (N)));
1022 end if;
1024 end Expand_N_Handled_Sequence_Of_Statements;
1026 -------------------------------------
1027 -- Expand_N_Raise_Constraint_Error --
1028 -------------------------------------
1030 -- The only processing required is to adjust the condition to deal
1031 -- with the C/Fortran boolean case. This may well not be necessary,
1032 -- as all such conditions are generated by the expander and probably
1033 -- are all standard boolean, but who knows what strange optimization
1034 -- in future may require this adjustment!
1036 procedure Expand_N_Raise_Constraint_Error (N : Node_Id) is
1037 begin
1038 Adjust_Condition (Condition (N));
1039 end Expand_N_Raise_Constraint_Error;
1041 ----------------------------------
1042 -- Expand_N_Raise_Program_Error --
1043 ----------------------------------
1045 -- The only processing required is to adjust the condition to deal
1046 -- with the C/Fortran boolean case. This may well not be necessary,
1047 -- as all such conditions are generated by the expander and probably
1048 -- are all standard boolean, but who knows what strange optimization
1049 -- in future may require this adjustment!
1051 procedure Expand_N_Raise_Program_Error (N : Node_Id) is
1052 begin
1053 Adjust_Condition (Condition (N));
1054 end Expand_N_Raise_Program_Error;
1056 ------------------------------
1057 -- Expand_N_Raise_Statement --
1058 ------------------------------
1060 procedure Expand_N_Raise_Statement (N : Node_Id) is
1061 Loc : constant Source_Ptr := Sloc (N);
1062 Ehand : Node_Id;
1063 E : Entity_Id;
1064 Str : String_Id;
1066 begin
1067 -- There is no expansion needed for statement "raise <exception>;" when
1068 -- compiling for the JVM since the JVM has a built-in exception
1069 -- mechanism. However we need the keep the expansion for "raise;"
1070 -- statements. See 4jexcept.ads for details.
1072 if Present (Name (N)) and then Hostparm.Java_VM then
1073 return;
1074 end if;
1076 -- Don't expand a raise statement that does not come from source
1077 -- if we have already had configurable run-time violations, since
1078 -- most likely it will be junk cascaded nonsense.
1080 if Configurable_Run_Time_Violations > 0
1081 and then not Comes_From_Source (N)
1082 then
1083 return;
1084 end if;
1086 -- Convert explicit raise of Program_Error, Constraint_Error, and
1087 -- Storage_Error into the corresponding raise (in High_Integrity_Mode
1088 -- all other raises will get normal expansion and be disallowed,
1089 -- but this is also faster in all modes).
1091 if Present (Name (N)) and then Nkind (Name (N)) = N_Identifier then
1092 if Entity (Name (N)) = Standard_Constraint_Error then
1093 Rewrite (N,
1094 Make_Raise_Constraint_Error (Loc,
1095 Reason => CE_Explicit_Raise));
1096 Analyze (N);
1097 return;
1099 elsif Entity (Name (N)) = Standard_Program_Error then
1100 Rewrite (N,
1101 Make_Raise_Program_Error (Loc,
1102 Reason => PE_Explicit_Raise));
1103 Analyze (N);
1104 return;
1106 elsif Entity (Name (N)) = Standard_Storage_Error then
1107 Rewrite (N,
1108 Make_Raise_Storage_Error (Loc,
1109 Reason => SE_Explicit_Raise));
1110 Analyze (N);
1111 return;
1112 end if;
1113 end if;
1115 -- Case of name present, in this case we expand raise name to
1117 -- Raise_Exception (name'Identity, location_string);
1119 -- where location_string identifies the file/line of the raise
1121 if Present (Name (N)) then
1122 declare
1123 Id : Entity_Id := Entity (Name (N));
1125 begin
1126 Build_Location_String (Loc);
1128 -- If the exception is a renaming, use the exception that it
1129 -- renames (which might be a predefined exception, e.g.).
1131 if Present (Renamed_Object (Id)) then
1132 Id := Renamed_Object (Id);
1133 end if;
1135 -- Build a C-compatible string in case of no exception handlers,
1136 -- since this is what the last chance handler is expecting.
1138 if Restrictions (No_Exception_Handlers) then
1140 -- Generate an empty message if configuration pragma
1141 -- Suppress_Exception_Locations is set for this unit.
1143 if Opt.Exception_Locations_Suppressed then
1144 Name_Len := 1;
1145 else
1146 Name_Len := Name_Len + 1;
1147 end if;
1149 Name_Buffer (Name_Len) := ASCII.NUL;
1150 end if;
1153 if Opt.Exception_Locations_Suppressed then
1154 Name_Len := 0;
1155 end if;
1157 Str := String_From_Name_Buffer;
1159 -- For VMS exceptions, convert the raise into a call to
1160 -- lib$stop so it will be handled by __gnat_error_handler.
1162 if Is_VMS_Exception (Id) then
1163 declare
1164 Excep_Image : String_Id;
1165 Cond : Node_Id;
1167 begin
1168 if Present (Interface_Name (Id)) then
1169 Excep_Image := Strval (Interface_Name (Id));
1170 else
1171 Get_Name_String (Chars (Id));
1172 Set_All_Upper_Case;
1173 Excep_Image := String_From_Name_Buffer;
1174 end if;
1176 if Exception_Code (Id) /= No_Uint then
1177 Cond :=
1178 Make_Integer_Literal (Loc, Exception_Code (Id));
1179 else
1180 Cond :=
1181 Unchecked_Convert_To (Standard_Integer,
1182 Make_Function_Call (Loc,
1183 Name => New_Occurrence_Of
1184 (RTE (RE_Import_Value), Loc),
1185 Parameter_Associations => New_List
1186 (Make_String_Literal (Loc,
1187 Strval => Excep_Image))));
1188 end if;
1190 Rewrite (N,
1191 Make_Procedure_Call_Statement (Loc,
1192 Name =>
1193 New_Occurrence_Of (RTE (RE_Lib_Stop), Loc),
1194 Parameter_Associations => New_List (Cond)));
1195 Analyze_And_Resolve (Cond, Standard_Integer);
1196 end;
1198 -- Not VMS exception case, convert raise to call to the
1199 -- Raise_Exception routine.
1201 else
1202 Rewrite (N,
1203 Make_Procedure_Call_Statement (Loc,
1204 Name => New_Occurrence_Of (RTE (RE_Raise_Exception), Loc),
1205 Parameter_Associations => New_List (
1206 Make_Attribute_Reference (Loc,
1207 Prefix => Name (N),
1208 Attribute_Name => Name_Identity),
1209 Make_String_Literal (Loc,
1210 Strval => Str))));
1211 end if;
1212 end;
1214 -- Case of no name present (reraise). We rewrite the raise to:
1216 -- Reraise_Occurrence_Always (EO);
1218 -- where EO is the current exception occurrence. If the current handler
1219 -- does not have a choice parameter specification, then we provide one.
1221 else
1222 -- Find innermost enclosing exception handler (there must be one,
1223 -- since the semantics has already verified that this raise statement
1224 -- is valid, and a raise with no arguments is only permitted in the
1225 -- context of an exception handler.
1227 Ehand := Parent (N);
1228 while Nkind (Ehand) /= N_Exception_Handler loop
1229 Ehand := Parent (Ehand);
1230 end loop;
1232 -- Make exception choice parameter if none present. Note that we do
1233 -- not need to put the entity on the entity chain, since no one will
1234 -- be referencing this entity by normal visibility methods.
1236 if No (Choice_Parameter (Ehand)) then
1237 E := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1238 Set_Choice_Parameter (Ehand, E);
1239 Set_Ekind (E, E_Variable);
1240 Set_Etype (E, RTE (RE_Exception_Occurrence));
1241 Set_Scope (E, Current_Scope);
1242 end if;
1244 -- Now rewrite the raise as a call to Reraise. A special case arises
1245 -- if this raise statement occurs in the context of a handler for
1246 -- all others (i.e. an at end handler). in this case we avoid
1247 -- the call to defer abort, cleanup routines are expected to be
1248 -- called in this case with aborts deferred.
1250 declare
1251 Ech : constant Node_Id := First (Exception_Choices (Ehand));
1252 Ent : Entity_Id;
1254 begin
1255 if Nkind (Ech) = N_Others_Choice
1256 and then All_Others (Ech)
1257 then
1258 Ent := RTE (RE_Reraise_Occurrence_No_Defer);
1259 else
1260 Ent := RTE (RE_Reraise_Occurrence_Always);
1261 end if;
1263 Rewrite (N,
1264 Make_Procedure_Call_Statement (Loc,
1265 Name => New_Occurrence_Of (Ent, Loc),
1266 Parameter_Associations => New_List (
1267 New_Occurrence_Of (Choice_Parameter (Ehand), Loc))));
1268 end;
1269 end if;
1271 Analyze (N);
1272 end Expand_N_Raise_Statement;
1274 ----------------------------------
1275 -- Expand_N_Raise_Storage_Error --
1276 ----------------------------------
1278 -- The only processing required is to adjust the condition to deal
1279 -- with the C/Fortran boolean case. This may well not be necessary,
1280 -- as all such conditions are generated by the expander and probably
1281 -- are all standard boolean, but who knows what strange optimization
1282 -- in future may require this adjustment!
1284 procedure Expand_N_Raise_Storage_Error (N : Node_Id) is
1285 begin
1286 Adjust_Condition (Condition (N));
1287 end Expand_N_Raise_Storage_Error;
1289 ------------------------------
1290 -- Expand_N_Subprogram_Info --
1291 ------------------------------
1293 procedure Expand_N_Subprogram_Info (N : Node_Id) is
1294 Loc : constant Source_Ptr := Sloc (N);
1296 begin
1297 -- For now, we replace an Expand_N_Subprogram_Info node with an
1298 -- attribute reference that gives the address of the procedure.
1299 -- This is because gigi does not yet recognize this node, and
1300 -- for the initial targets, this is the right value anyway.
1302 Rewrite (N,
1303 Make_Attribute_Reference (Loc,
1304 Prefix => Identifier (N),
1305 Attribute_Name => Name_Code_Address));
1307 Analyze_And_Resolve (N, RTE (RE_Code_Loc));
1308 end Expand_N_Subprogram_Info;
1310 ------------------------------------
1311 -- Generate_Subprogram_Descriptor --
1312 ------------------------------------
1314 procedure Generate_Subprogram_Descriptor
1315 (N : Node_Id;
1316 Loc : Source_Ptr;
1317 Spec : Entity_Id;
1318 Slist : List_Id)
1320 Code : Node_Id;
1321 Ent : Entity_Id;
1322 Decl : Node_Id;
1323 Dtyp : Entity_Id;
1324 Numh : Nat;
1325 Sdes : Node_Id;
1326 Hrc : List_Id;
1328 begin
1329 if Exception_Mechanism /= Front_End_ZCX_Exceptions then
1330 return;
1331 end if;
1333 if Restrictions (No_Exception_Handlers) then
1334 return;
1335 end if;
1337 -- Suppress descriptor if we are not generating code. This happens
1338 -- in the case of a -gnatc -gnatt compilation where we force generics
1339 -- to be generated, but we still don't want exception tables.
1341 if Operating_Mode /= Generate_Code then
1342 return;
1343 end if;
1345 -- Suppress descriptor if we are in No_Exceptions restrictions mode,
1346 -- since we can never propagate exceptions in any case in this mode.
1347 -- The same consideration applies for No_Exception_Handlers (which
1348 -- is also set in High_Integrity_Mode).
1350 if Restrictions (No_Exceptions)
1351 or Restrictions (No_Exception_Handlers)
1352 then
1353 return;
1354 end if;
1356 -- Suppress descriptor if we are inside a generic. There are two
1357 -- ways that we can tell that, depending on what is going on. If
1358 -- we are actually inside the processing for a generic right now,
1359 -- then Expander_Active will be reset. If we are outside the
1360 -- generic, then we will see the generic entity.
1362 if not Expander_Active then
1363 return;
1364 end if;
1366 -- Suppress descriptor is subprogram is marked as eliminated, for
1367 -- example if this is a subprogram created to analyze a default
1368 -- expression with potential side effects. Ditto if it is nested
1369 -- within an eliminated subprogram, for example a cleanup action.
1371 declare
1372 Scop : Entity_Id;
1374 begin
1375 Scop := Spec;
1376 while Scop /= Standard_Standard loop
1377 if Is_Generic_Unit (Scop) or else Is_Eliminated (Scop) then
1378 return;
1379 end if;
1381 Scop := Scope (Scop);
1382 end loop;
1383 end;
1385 -- Suppress descriptor for original protected subprogram (we will
1386 -- be called again later to generate the descriptor for the actual
1387 -- protected body subprogram.) This does not apply to barrier
1388 -- functions which are there own protected subprogram.
1390 if Is_Subprogram (Spec)
1391 and then Present (Protected_Body_Subprogram (Spec))
1392 and then Protected_Body_Subprogram (Spec) /= Spec
1393 then
1394 return;
1395 end if;
1397 -- Suppress descriptors for packages unless they have at least one
1398 -- handler. The binder will generate the dummy (no handler) descriptors
1399 -- for elaboration procedures. We can't do it here, because we don't
1400 -- know if an elaboration routine does in fact exist.
1402 -- If there is at least one handler for the package spec or body
1403 -- then most certainly an elaboration routine must exist, so we
1404 -- can safely reference it.
1406 if (Nkind (N) = N_Package_Declaration
1407 or else
1408 Nkind (N) = N_Package_Body)
1409 and then No (Handler_Records (Spec))
1410 then
1411 return;
1412 end if;
1414 -- Suppress all subprogram descriptors for the file System.Exceptions.
1415 -- We similarly suppress subprogram descriptors for Ada.Exceptions.
1416 -- These are all init procs for types which cannot raise exceptions.
1417 -- The reason this is done is that otherwise we get embarassing
1418 -- elaboration dependencies.
1420 Get_Name_String (Unit_File_Name (Current_Sem_Unit));
1422 if Name_Buffer (1 .. 12) = "s-except.ads"
1423 or else
1424 Name_Buffer (1 .. 12) = "a-except.ads"
1425 then
1426 return;
1427 end if;
1429 -- Similarly, we need to suppress entries for System.Standard_Library,
1430 -- since otherwise we get elaboration circularities. Again, this would
1431 -- better be done with a Suppress_Initialization pragma :-)
1433 if Name_Buffer (1 .. 11) = "s-stalib.ad" then
1434 return;
1435 end if;
1437 -- For now, also suppress entries for s-stoele because we have
1438 -- some kind of unexplained error there ???
1440 if Name_Buffer (1 .. 11) = "s-stoele.ad" then
1441 return;
1442 end if;
1444 -- And also for g-htable, because it cannot raise exceptions,
1445 -- and generates some kind of elaboration order problem.
1447 if Name_Buffer (1 .. 11) = "g-htable.ad" then
1448 return;
1449 end if;
1451 -- Suppress subprogram descriptor if already generated. This happens
1452 -- in the case of late generation from Delay_Subprogram_Descriptors
1453 -- beging set (where there is more than one instantiation in the list)
1455 if Has_Subprogram_Descriptor (Spec) then
1456 return;
1457 else
1458 Set_Has_Subprogram_Descriptor (Spec);
1459 end if;
1461 -- Never generate descriptors for inlined bodies
1463 if Analyzing_Inlined_Bodies then
1464 return;
1465 end if;
1467 -- Here we definitely are going to generate a subprogram descriptor
1469 declare
1470 Hnum : Nat := Homonym_Number (Spec);
1472 begin
1473 if Hnum = 1 then
1474 Hnum := 0;
1475 end if;
1477 Ent :=
1478 Make_Defining_Identifier (Loc,
1479 Chars => New_External_Name (Chars (Spec), "SD", Hnum));
1480 end;
1482 if No (Handler_Records (Spec)) then
1483 Hrc := Empty_List;
1484 Numh := 0;
1485 else
1486 Hrc := Handler_Records (Spec);
1487 Numh := List_Length (Hrc);
1488 end if;
1490 New_Scope (Spec);
1492 -- We need a static subtype for the declaration of the subprogram
1493 -- descriptor. For the case of 0-3 handlers we can use one of the
1494 -- predefined subtypes in System.Exceptions. For more handlers,
1495 -- we build our own subtype here.
1497 case Numh is
1498 when 0 =>
1499 Dtyp := RTE (RE_Subprogram_Descriptor_0);
1501 when 1 =>
1502 Dtyp := RTE (RE_Subprogram_Descriptor_1);
1504 when 2 =>
1505 Dtyp := RTE (RE_Subprogram_Descriptor_2);
1507 when 3 =>
1508 Dtyp := RTE (RE_Subprogram_Descriptor_3);
1510 when others =>
1511 Dtyp :=
1512 Make_Defining_Identifier (Loc,
1513 Chars => New_Internal_Name ('T'));
1515 -- Set the constructed type as global, since we will be
1516 -- referencing the object that is of this type globally
1518 Set_Is_Statically_Allocated (Dtyp);
1520 Decl :=
1521 Make_Subtype_Declaration (Loc,
1522 Defining_Identifier => Dtyp,
1523 Subtype_Indication =>
1524 Make_Subtype_Indication (Loc,
1525 Subtype_Mark =>
1526 New_Occurrence_Of (RTE (RE_Subprogram_Descriptor), Loc),
1527 Constraint =>
1528 Make_Index_Or_Discriminant_Constraint (Loc,
1529 Constraints => New_List (
1530 Make_Integer_Literal (Loc, Numh)))));
1532 Append (Decl, Slist);
1534 -- We analyze the descriptor for the subprogram and package
1535 -- case, but not for the imported subprogram case (it will
1536 -- be analyzed when the freeze entity actions are analyzed.
1538 if Present (N) then
1539 Analyze (Decl);
1540 end if;
1542 Set_Exception_Junk (Decl);
1543 end case;
1545 -- Prepare the code address entry for the table entry. For the normal
1546 -- case of being within a procedure, this is simply:
1548 -- P'Code_Address
1550 -- where P is the procedure, but for the package case, it is
1552 -- P'Elab_Body'Code_Address
1553 -- P'Elab_Spec'Code_Address
1555 -- for the body and spec respectively. Note that we do our own
1556 -- analysis of these attribute references, because we know in this
1557 -- case that the prefix of ELab_Body/Spec is a visible package,
1558 -- which can be referenced directly instead of using the general
1559 -- case expansion for these attributes.
1561 if Ekind (Spec) = E_Package then
1562 Code :=
1563 Make_Attribute_Reference (Loc,
1564 Prefix => New_Occurrence_Of (Spec, Loc),
1565 Attribute_Name => Name_Elab_Spec);
1566 Set_Etype (Code, Standard_Void_Type);
1567 Set_Analyzed (Code);
1569 elsif Ekind (Spec) = E_Package_Body then
1570 Code :=
1571 Make_Attribute_Reference (Loc,
1572 Prefix => New_Occurrence_Of (Spec_Entity (Spec), Loc),
1573 Attribute_Name => Name_Elab_Body);
1574 Set_Etype (Code, Standard_Void_Type);
1575 Set_Analyzed (Code);
1577 else
1578 Code := New_Occurrence_Of (Spec, Loc);
1579 end if;
1581 Code :=
1582 Make_Attribute_Reference (Loc,
1583 Prefix => Code,
1584 Attribute_Name => Name_Code_Address);
1586 Set_Etype (Code, RTE (RE_Address));
1587 Set_Analyzed (Code);
1589 -- Now we can build the subprogram descriptor
1591 Sdes :=
1592 Make_Object_Declaration (Loc,
1593 Defining_Identifier => Ent,
1594 Constant_Present => True,
1595 Aliased_Present => True,
1596 Object_Definition => New_Occurrence_Of (Dtyp, Loc),
1598 Expression =>
1599 Make_Aggregate (Loc,
1600 Expressions => New_List (
1601 Make_Integer_Literal (Loc, Numh), -- Num_Handlers
1603 Code, -- Code
1605 -- temp code ???
1607 -- Make_Subprogram_Info (Loc, -- Subprogram_Info
1608 -- Identifier =>
1609 -- New_Occurrence_Of (Spec, Loc)),
1611 New_Copy_Tree (Code),
1613 Make_Aggregate (Loc, -- Handler_Records
1614 Expressions => Hrc))));
1616 Set_Exception_Junk (Sdes);
1617 Set_Is_Subprogram_Descriptor (Sdes);
1619 Append (Sdes, Slist);
1621 -- We analyze the descriptor for the subprogram and package case,
1622 -- but not for the imported subprogram case (it will be analyzed
1623 -- when the freeze entity actions are analyzed.
1625 if Present (N) then
1626 Analyze (Sdes);
1627 end if;
1629 -- We can now pop the scope used for analyzing the descriptor
1631 Pop_Scope;
1633 -- We need to set the descriptor as statically allocated, since
1634 -- it will be referenced from the unit exception table.
1636 Set_Is_Statically_Allocated (Ent);
1638 -- Append the resulting descriptor to the list. We do this only
1639 -- if we are in the main unit. You might think that we could
1640 -- simply skip generating the descriptors completely if we are
1641 -- not in the main unit, but in fact this is not the case, since
1642 -- we have problems with inconsistent serial numbers for internal
1643 -- names if we do this.
1645 if In_Extended_Main_Code_Unit (Spec) then
1646 Append_To (SD_List,
1647 Make_Attribute_Reference (Loc,
1648 Prefix => New_Occurrence_Of (Ent, Loc),
1649 Attribute_Name => Name_Unrestricted_Access));
1651 Unit_Exception_Table_Present := True;
1652 end if;
1654 end Generate_Subprogram_Descriptor;
1656 ------------------------------------------------------------
1657 -- Generate_Subprogram_Descriptor_For_Imported_Subprogram --
1658 ------------------------------------------------------------
1660 procedure Generate_Subprogram_Descriptor_For_Imported_Subprogram
1661 (Spec : Entity_Id;
1662 Slist : List_Id)
1664 begin
1665 Generate_Subprogram_Descriptor (Empty, Sloc (Spec), Spec, Slist);
1666 end Generate_Subprogram_Descriptor_For_Imported_Subprogram;
1668 ------------------------------------------------
1669 -- Generate_Subprogram_Descriptor_For_Package --
1670 ------------------------------------------------
1672 procedure Generate_Subprogram_Descriptor_For_Package
1673 (N : Node_Id;
1674 Spec : Entity_Id)
1676 Adecl : Node_Id;
1678 begin
1679 -- If N is empty with prior errors, ignore
1681 if Total_Errors_Detected /= 0 and then No (N) then
1682 return;
1683 end if;
1685 -- Do not generate if no exceptions
1687 if Restrictions (No_Exception_Handlers) then
1688 return;
1689 end if;
1691 -- Otherwise generate descriptor
1693 Adecl := Aux_Decls_Node (Parent (N));
1695 if No (Actions (Adecl)) then
1696 Set_Actions (Adecl, New_List);
1697 end if;
1699 Generate_Subprogram_Descriptor (N, Sloc (N), Spec, Actions (Adecl));
1700 end Generate_Subprogram_Descriptor_For_Package;
1702 ---------------------------------------------------
1703 -- Generate_Subprogram_Descriptor_For_Subprogram --
1704 ---------------------------------------------------
1706 procedure Generate_Subprogram_Descriptor_For_Subprogram
1707 (N : Node_Id;
1708 Spec : Entity_Id)
1710 begin
1711 -- If we have no subprogram body and prior errors, ignore
1713 if Total_Errors_Detected /= 0 and then No (N) then
1714 return;
1715 end if;
1717 -- Do not generate if no exceptions
1719 if Restrictions (No_Exception_Handlers) then
1720 return;
1721 end if;
1723 -- Else generate descriptor
1725 declare
1726 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1728 begin
1729 if No (Exception_Handlers (HSS)) then
1730 Generate_Subprogram_Descriptor
1731 (N, Sloc (N), Spec, Statements (HSS));
1732 else
1733 Generate_Subprogram_Descriptor
1734 (N, Sloc (N),
1735 Spec, Statements (Last (Exception_Handlers (HSS))));
1736 end if;
1737 end;
1738 end Generate_Subprogram_Descriptor_For_Subprogram;
1740 -----------------------------------
1741 -- Generate_Unit_Exception_Table --
1742 -----------------------------------
1744 -- The only remaining thing to generate here is to generate the
1745 -- reference to the subprogram descriptor chain. See Ada.Exceptions
1746 -- for details of required data structures.
1748 procedure Generate_Unit_Exception_Table is
1749 Loc : constant Source_Ptr := No_Location;
1750 Num : Nat;
1751 Decl : Node_Id;
1752 Ent : Entity_Id;
1753 Next_Ent : Entity_Id;
1754 Stent : Entity_Id;
1756 begin
1757 -- Nothing to be done if zero length exceptions not active
1759 if Exception_Mechanism /= Front_End_ZCX_Exceptions then
1760 return;
1761 end if;
1763 -- Nothing to do if no exceptions
1765 if Restrictions (No_Exception_Handlers) then
1766 return;
1767 end if;
1769 -- Remove any entries from SD_List that correspond to eliminated
1770 -- subprograms.
1772 Ent := First (SD_List);
1773 while Present (Ent) loop
1774 Next_Ent := Next (Ent);
1775 if Is_Eliminated (Scope (Entity (Prefix (Ent)))) then
1776 Remove (Ent); -- After this, there is no Next (Ent) anymore
1777 end if;
1779 Ent := Next_Ent;
1780 end loop;
1782 -- Nothing to do if no unit exception table present.
1783 -- An empty table can result from subprogram elimination,
1784 -- in such a case, eliminate the exception table itself.
1786 if Is_Empty_List (SD_List) then
1787 Unit_Exception_Table_Present := False;
1788 return;
1789 end if;
1791 -- Do not generate table in a generic
1793 if Inside_A_Generic then
1794 return;
1795 end if;
1797 -- Generate the unit exception table
1799 -- subtype Tnn is Subprogram_Descriptors_Record (Num);
1800 -- __gnat_unitname__SDP : aliased constant Tnn :=
1801 -- Num,
1802 -- (sub1'unrestricted_access,
1803 -- sub2'unrestricted_access,
1804 -- ...
1805 -- subNum'unrestricted_access));
1807 Num := List_Length (SD_List);
1809 Stent :=
1810 Make_Defining_Identifier (Loc,
1811 Chars => New_Internal_Name ('T'));
1813 Insert_Library_Level_Action (
1814 Make_Subtype_Declaration (Loc,
1815 Defining_Identifier => Stent,
1816 Subtype_Indication =>
1817 Make_Subtype_Indication (Loc,
1818 Subtype_Mark =>
1819 New_Occurrence_Of
1820 (RTE (RE_Subprogram_Descriptors_Record), Loc),
1821 Constraint =>
1822 Make_Index_Or_Discriminant_Constraint (Loc,
1823 Constraints => New_List (
1824 Make_Integer_Literal (Loc, Num))))));
1826 Set_Is_Statically_Allocated (Stent);
1828 Get_External_Unit_Name_String (Unit_Name (Main_Unit));
1829 Name_Buffer (1 + 7 .. Name_Len + 7) := Name_Buffer (1 .. Name_Len);
1830 Name_Buffer (1 .. 7) := "__gnat_";
1831 Name_Len := Name_Len + 7;
1832 Add_Str_To_Name_Buffer ("__SDP");
1834 Ent :=
1835 Make_Defining_Identifier (Loc,
1836 Chars => Name_Find);
1838 Get_Name_String (Chars (Ent));
1839 Set_Interface_Name (Ent,
1840 Make_String_Literal (Loc, Strval => String_From_Name_Buffer));
1842 Decl :=
1843 Make_Object_Declaration (Loc,
1844 Defining_Identifier => Ent,
1845 Object_Definition => New_Occurrence_Of (Stent, Loc),
1846 Constant_Present => True,
1847 Aliased_Present => True,
1848 Expression =>
1849 Make_Aggregate (Loc,
1850 New_List (
1851 Make_Integer_Literal (Loc, List_Length (SD_List)),
1853 Make_Aggregate (Loc,
1854 Expressions => SD_List))));
1856 Insert_Library_Level_Action (Decl);
1858 Set_Is_Exported (Ent, True);
1859 Set_Is_Public (Ent, True);
1860 Set_Is_Statically_Allocated (Ent, True);
1862 Get_Name_String (Chars (Ent));
1863 Set_Interface_Name (Ent,
1864 Make_String_Literal (Loc,
1865 Strval => String_From_Name_Buffer));
1867 end Generate_Unit_Exception_Table;
1869 ----------------
1870 -- Initialize --
1871 ----------------
1873 procedure Initialize is
1874 begin
1875 SD_List := Empty_List;
1876 end Initialize;
1878 ----------------------
1879 -- Is_Non_Ada_Error --
1880 ----------------------
1882 function Is_Non_Ada_Error (E : Entity_Id) return Boolean is
1883 begin
1884 if not OpenVMS_On_Target then
1885 return False;
1886 end if;
1888 Get_Name_String (Chars (E));
1890 -- Note: it is a little irregular for the body of exp_ch11 to know
1891 -- the details of the encoding scheme for names, but on the other
1892 -- hand, gigi knows them, and this is for gigi's benefit anyway!
1894 if Name_Buffer (1 .. 30) /= "system__aux_dec__non_ada_error" then
1895 return False;
1896 end if;
1898 return True;
1899 end Is_Non_Ada_Error;
1901 ----------------------------
1902 -- Remove_Handler_Entries --
1903 ----------------------------
1905 procedure Remove_Handler_Entries (N : Node_Id) is
1906 function Check_Handler_Entry (N : Node_Id) return Traverse_Result;
1907 -- This function checks one node for a possible reference to a
1908 -- handler entry that must be deleted. it always returns OK.
1910 function Remove_All_Handler_Entries is new
1911 Traverse_Func (Check_Handler_Entry);
1912 -- This defines the traversal operation
1914 Discard : Traverse_Result;
1915 pragma Warnings (Off, Discard);
1917 function Check_Handler_Entry (N : Node_Id) return Traverse_Result is
1918 begin
1919 if Nkind (N) = N_Object_Declaration then
1921 if Present (Handler_List_Entry (N)) then
1922 Remove (Handler_List_Entry (N));
1923 Delete_Tree (Handler_List_Entry (N));
1924 Set_Handler_List_Entry (N, Empty);
1926 elsif Is_Subprogram_Descriptor (N) then
1927 declare
1928 SDN : Node_Id;
1930 begin
1931 SDN := First (SD_List);
1932 while Present (SDN) loop
1933 if Defining_Identifier (N) = Entity (Prefix (SDN)) then
1934 Remove (SDN);
1935 Delete_Tree (SDN);
1936 exit;
1937 end if;
1939 Next (SDN);
1940 end loop;
1941 end;
1942 end if;
1943 end if;
1945 return OK;
1946 end Check_Handler_Entry;
1948 -- Start of processing for Remove_Handler_Entries
1950 begin
1951 if Exception_Mechanism = Front_End_ZCX_Exceptions then
1952 Discard := Remove_All_Handler_Entries (N);
1953 end if;
1954 end Remove_Handler_Entries;
1956 end Exp_Ch11;