* gcc.dg/compat/struct-layout-1_generate.c (dg_options): New. Moved
[official-gcc.git] / gcc / ada / a-exexpr-gcc.adb
blob80db0337135ff774d7bb4f43d6c6c7ee0391afb7
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- A D A . E X C E P T I O N S . E X C E P T I O N _ P R O P A G A T I O N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2008, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
21 -- --
22 -- As a special exception, if other files instantiate generics from this --
23 -- unit, or you link this unit with other files to produce an executable, --
24 -- this unit does not by itself cause the resulting executable to be --
25 -- covered by the GNU General Public License. This exception does not --
26 -- however invalidate any other reasons why the executable file might be --
27 -- covered by the GNU Public License. --
28 -- --
29 -- GNAT was originally developed by the GNAT team at New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 -- This is the version using the GCC EH mechanism
36 with Ada.Unchecked_Conversion;
37 with Ada.Unchecked_Deallocation;
39 with System.Storage_Elements; use System.Storage_Elements;
41 separate (Ada.Exceptions)
42 package body Exception_Propagation is
44 ------------------------------------------------
45 -- Entities to interface with the GCC runtime --
46 ------------------------------------------------
48 -- These come from "C++ ABI for Itanium: Exception handling", which is
49 -- the reference for GCC. They are used only when we are relying on
50 -- back-end tables for exception propagation, which in turn is currently
51 -- only the case for Zero_Cost_Exceptions in GNAT5.
53 -- Return codes from the GCC runtime functions used to propagate
54 -- an exception.
56 type Unwind_Reason_Code is
57 (URC_NO_REASON,
58 URC_FOREIGN_EXCEPTION_CAUGHT,
59 URC_PHASE2_ERROR,
60 URC_PHASE1_ERROR,
61 URC_NORMAL_STOP,
62 URC_END_OF_STACK,
63 URC_HANDLER_FOUND,
64 URC_INSTALL_CONTEXT,
65 URC_CONTINUE_UNWIND);
67 pragma Unreferenced
68 (URC_FOREIGN_EXCEPTION_CAUGHT,
69 URC_PHASE2_ERROR,
70 URC_PHASE1_ERROR,
71 URC_NORMAL_STOP,
72 URC_END_OF_STACK,
73 URC_HANDLER_FOUND,
74 URC_INSTALL_CONTEXT,
75 URC_CONTINUE_UNWIND);
77 pragma Convention (C, Unwind_Reason_Code);
79 -- Phase identifiers
81 type Unwind_Action is
82 (UA_SEARCH_PHASE,
83 UA_CLEANUP_PHASE,
84 UA_HANDLER_FRAME,
85 UA_FORCE_UNWIND);
87 for Unwind_Action use
88 (UA_SEARCH_PHASE => 1,
89 UA_CLEANUP_PHASE => 2,
90 UA_HANDLER_FRAME => 4,
91 UA_FORCE_UNWIND => 8);
93 pragma Convention (C, Unwind_Action);
95 -- Mandatory common header for any exception object handled by the
96 -- GCC unwinding runtime.
98 type Exception_Class is mod 2 ** 64;
100 GNAT_Exception_Class : constant Exception_Class := 16#474e552d41646100#;
101 -- "GNU-Ada\0"
103 type Unwind_Word is mod 2 ** System.Word_Size;
104 for Unwind_Word'Size use System.Word_Size;
105 -- Map the corresponding C type used in Unwind_Exception below
107 type Unwind_Exception is record
108 Class : Exception_Class := GNAT_Exception_Class;
109 Cleanup : System.Address := System.Null_Address;
110 Private1 : Unwind_Word;
111 Private2 : Unwind_Word;
112 end record;
113 -- Map the GCC struct used for exception handling
115 for Unwind_Exception'Alignment use Standard'Maximum_Alignment;
116 -- The C++ ABI mandates the common exception header to be at least
117 -- doubleword aligned, and the libGCC implementation actually makes it
118 -- maximally aligned (see unwind.h). See additional comments on the
119 -- alignment below.
121 --------------------------------------------------------------
122 -- GNAT Specific Entities To Deal With The GCC EH Circuitry --
123 --------------------------------------------------------------
125 -- A GNAT exception object to be dealt with by the personality routine
126 -- called by the GCC unwinding runtime.
128 type GNAT_GCC_Exception is record
129 Header : Unwind_Exception;
130 -- ABI Exception header first
132 Id : Exception_Id;
133 -- GNAT Exception identifier. This is filled by Propagate_Exception
134 -- and then used by the personality routine to determine if the context
135 -- it examines contains a handler for the exception being propagated.
137 N_Cleanups_To_Trigger : Integer;
138 -- Number of cleanup only frames encountered in SEARCH phase. This is
139 -- initialized to 0 by Propagate_Exception and maintained by the
140 -- personality routine to control a forced unwinding phase triggering
141 -- all the cleanups before calling Unhandled_Exception_Terminate when
142 -- an exception is not handled.
144 Next_Exception : EOA;
145 -- Used to create a linked list of exception occurrences
146 end record;
148 pragma Convention (C, GNAT_GCC_Exception);
150 -- There is a subtle issue with the common header alignment, since the C
151 -- version is aligned on BIGGEST_ALIGNMENT, the Ada version is aligned on
152 -- Standard'Maximum_Alignment, and those two values don't quite represent
153 -- the same concepts and so may be decoupled someday. One typical reason
154 -- is that BIGGEST_ALIGNMENT may be larger than what the underlying system
155 -- allocator guarantees, and there are extra costs involved in allocating
156 -- objects aligned to such factors.
158 -- To deal with the potential alignment differences between the C and Ada
159 -- representations, the Ada part of the whole structure is only accessed
160 -- by the personality routine through the accessors declared below. Ada
161 -- specific fields are thus always accessed through consistent layout, and
162 -- we expect the actual alignment to always be large enough to avoid traps
163 -- from the C accesses to the common header. Besides, accessors alleviate
164 -- the need for a C struct whole counterpart, both painful and error-prone
165 -- to maintain anyway.
167 type GNAT_GCC_Exception_Access is access all GNAT_GCC_Exception;
169 function To_GNAT_GCC_Exception is new
170 Unchecked_Conversion (System.Address, GNAT_GCC_Exception_Access);
172 procedure Free is new Unchecked_Deallocation
173 (GNAT_GCC_Exception, GNAT_GCC_Exception_Access);
175 procedure Free is new Unchecked_Deallocation
176 (Exception_Occurrence, EOA);
178 function CleanupUnwind_Handler
179 (UW_Version : Integer;
180 UW_Phases : Unwind_Action;
181 UW_Eclass : Exception_Class;
182 UW_Exception : not null access GNAT_GCC_Exception;
183 UW_Context : System.Address;
184 UW_Argument : System.Address) return Unwind_Reason_Code;
185 -- Hook called at each step of the forced unwinding we perform to
186 -- trigger cleanups found during the propagation of an unhandled
187 -- exception.
189 -- GCC runtime functions used. These are C non-void functions, actually,
190 -- but we ignore the return values. See raise.c as to why we are using
191 -- __gnat stubs for these.
193 procedure Unwind_RaiseException
194 (UW_Exception : not null access GNAT_GCC_Exception);
195 pragma Import (C, Unwind_RaiseException, "__gnat_Unwind_RaiseException");
197 procedure Unwind_ForcedUnwind
198 (UW_Exception : not null access GNAT_GCC_Exception;
199 UW_Handler : System.Address;
200 UW_Argument : System.Address);
201 pragma Import (C, Unwind_ForcedUnwind, "__gnat_Unwind_ForcedUnwind");
203 ------------------------------------------------------------------
204 -- Occurrence Stack Management Facilities for the GCC-EH Scheme --
205 ------------------------------------------------------------------
207 function Remove
208 (Top : EOA;
209 Excep : GNAT_GCC_Exception_Access) return Boolean;
210 -- Remove Excep from the stack starting at Top.
211 -- Return True if Excep was found and removed, false otherwise.
213 -- Hooks called when entering/leaving an exception handler for a given
214 -- occurrence, aimed at handling the stack of active occurrences. The
215 -- calls are generated by gigi in tree_transform/N_Exception_Handler.
217 procedure Begin_Handler (GCC_Exception : GNAT_GCC_Exception_Access);
218 pragma Export (C, Begin_Handler, "__gnat_begin_handler");
220 procedure End_Handler (GCC_Exception : GNAT_GCC_Exception_Access);
221 pragma Export (C, End_Handler, "__gnat_end_handler");
223 Setup_Key : constant := 16#DEAD#;
224 -- To handle the case of a task "transferring" an exception occurrence to
225 -- another task, for instance via Exceptional_Complete_Rendezvous, we need
226 -- to be able to identify occurrences which have been Setup and not yet
227 -- Propagated. We hijack one of the common header fields for that purpose,
228 -- setting it to a special key value during the setup process, clearing it
229 -- at the very beginning of the propagation phase, and expecting it never
230 -- to be reset to the special value later on. A 16-bit value is used rather
231 -- than a 32-bit value for static compatibility with 16-bit targets such as
232 -- AAMP (where type Unwind_Word will be 16 bits).
234 function Is_Setup_And_Not_Propagated (E : EOA) return Boolean;
236 procedure Set_Setup_And_Not_Propagated (E : EOA);
237 procedure Clear_Setup_And_Not_Propagated (E : EOA);
239 procedure Save_Occurrence_And_Private
240 (Target : out Exception_Occurrence;
241 Source : Exception_Occurrence);
242 -- Copy all the components of Source to Target as well as the
243 -- Private_Data pointer.
245 --------------------------------------------------------------------
246 -- Accessors to Basic Components of a GNAT Exception Data Pointer --
247 --------------------------------------------------------------------
249 -- As of today, these are only used by the C implementation of the GCC
250 -- propagation personality routine to avoid having to rely on a C
251 -- counterpart of the whole exception_data structure, which is both
252 -- painful and error prone. These subprograms could be moved to a more
253 -- widely visible location if need be.
255 function Is_Handled_By_Others (E : Exception_Data_Ptr) return Boolean;
256 pragma Export (C, Is_Handled_By_Others, "__gnat_is_handled_by_others");
257 pragma Warnings (Off, Is_Handled_By_Others);
259 function Language_For (E : Exception_Data_Ptr) return Character;
260 pragma Export (C, Language_For, "__gnat_language_for");
262 function Import_Code_For (E : Exception_Data_Ptr) return Exception_Code;
263 pragma Export (C, Import_Code_For, "__gnat_import_code_for");
265 function EID_For (GNAT_Exception : GNAT_GCC_Exception_Access)
266 return Exception_Id;
267 pragma Export (C, EID_For, "__gnat_eid_for");
269 procedure Adjust_N_Cleanups_For
270 (GNAT_Exception : GNAT_GCC_Exception_Access;
271 Adjustment : Integer);
272 pragma Export (C, Adjust_N_Cleanups_For, "__gnat_adjust_n_cleanups_for");
274 ---------------------------------------------------------------------------
275 -- Objects to materialize "others" and "all others" in the GCC EH tables --
276 ---------------------------------------------------------------------------
278 -- Currently, these only have their address taken and compared so there is
279 -- no real point having whole exception data blocks allocated. In any case
280 -- the types should match what gigi and the personality routine expect.
281 -- The initial value is an arbitrary value that will not exceed the range
282 -- of Integer on 16-bit targets (such as AAMP).
284 Others_Value : constant Integer := 16#7FFF#;
285 pragma Export (C, Others_Value, "__gnat_others_value");
287 All_Others_Value : constant Integer := 16#7FFF#;
288 pragma Export (C, All_Others_Value, "__gnat_all_others_value");
290 ------------
291 -- Remove --
292 ------------
294 function Remove
295 (Top : EOA;
296 Excep : GNAT_GCC_Exception_Access) return Boolean
298 Prev : GNAT_GCC_Exception_Access := null;
299 Iter : EOA := Top;
300 GCC_Exception : GNAT_GCC_Exception_Access;
302 begin
303 -- Pop stack
305 loop
306 pragma Assert (Iter.Private_Data /= System.Null_Address);
308 GCC_Exception := To_GNAT_GCC_Exception (Iter.Private_Data);
310 if GCC_Exception = Excep then
311 if Prev = null then
313 -- Special case for the top of the stack: shift the contents
314 -- of the next item to the top, since top is at a fixed
315 -- location and can't be changed.
317 Iter := GCC_Exception.Next_Exception;
319 if Iter = null then
321 -- Stack is now empty
323 Top.Private_Data := System.Null_Address;
325 else
326 Save_Occurrence_And_Private (Top.all, Iter.all);
327 Free (Iter);
328 end if;
330 else
331 Prev.Next_Exception := GCC_Exception.Next_Exception;
332 Free (Iter);
333 end if;
335 Free (GCC_Exception);
337 return True;
338 end if;
340 exit when GCC_Exception.Next_Exception = null;
342 Prev := GCC_Exception;
343 Iter := GCC_Exception.Next_Exception;
344 end loop;
346 return False;
347 end Remove;
349 ---------------------------
350 -- CleanupUnwind_Handler --
351 ---------------------------
353 function CleanupUnwind_Handler
354 (UW_Version : Integer;
355 UW_Phases : Unwind_Action;
356 UW_Eclass : Exception_Class;
357 UW_Exception : not null access GNAT_GCC_Exception;
358 UW_Context : System.Address;
359 UW_Argument : System.Address) return Unwind_Reason_Code
361 pragma Unreferenced
362 (UW_Version, UW_Phases, UW_Eclass, UW_Context, UW_Argument);
364 begin
365 -- Terminate as soon as we know there is nothing more to run. The
366 -- count is maintained by the personality routine.
368 if UW_Exception.N_Cleanups_To_Trigger = 0 then
369 Unhandled_Exception_Terminate;
370 end if;
372 -- We know there is at least one cleanup further up. Return so that it
373 -- is searched and entered, after which Unwind_Resume will be called
374 -- and this hook will gain control (with an updated count) again.
376 return URC_NO_REASON;
377 end CleanupUnwind_Handler;
379 ---------------------------------
380 -- Is_Setup_And_Not_Propagated --
381 ---------------------------------
383 function Is_Setup_And_Not_Propagated (E : EOA) return Boolean is
384 GCC_E : constant GNAT_GCC_Exception_Access :=
385 To_GNAT_GCC_Exception (E.Private_Data);
386 begin
387 return GCC_E /= null and then GCC_E.Header.Private1 = Setup_Key;
388 end Is_Setup_And_Not_Propagated;
390 ------------------------------------
391 -- Clear_Setup_And_Not_Propagated --
392 ------------------------------------
394 procedure Clear_Setup_And_Not_Propagated (E : EOA) is
395 GCC_E : constant GNAT_GCC_Exception_Access :=
396 To_GNAT_GCC_Exception (E.Private_Data);
397 begin
398 pragma Assert (GCC_E /= null);
399 GCC_E.Header.Private1 := 0;
400 end Clear_Setup_And_Not_Propagated;
402 ----------------------------------
403 -- Set_Setup_And_Not_Propagated --
404 ----------------------------------
406 procedure Set_Setup_And_Not_Propagated (E : EOA) is
407 GCC_E : constant GNAT_GCC_Exception_Access :=
408 To_GNAT_GCC_Exception (E.Private_Data);
409 begin
410 pragma Assert (GCC_E /= null);
411 GCC_E.Header.Private1 := Setup_Key;
412 end Set_Setup_And_Not_Propagated;
414 --------------------------------
415 -- Save_Occurrence_And_Private --
416 --------------------------------
418 procedure Save_Occurrence_And_Private
419 (Target : out Exception_Occurrence;
420 Source : Exception_Occurrence)
422 begin
423 Save_Occurrence_No_Private (Target, Source);
424 Target.Private_Data := Source.Private_Data;
425 end Save_Occurrence_And_Private;
427 ---------------------
428 -- Setup_Exception --
429 ---------------------
431 -- In the GCC-EH implementation of the propagation scheme, this
432 -- subprogram should be understood as: Setup the exception occurrence
433 -- stack headed at Current for a forthcoming raise of Excep.
435 procedure Setup_Exception
436 (Excep : EOA;
437 Current : EOA;
438 Reraised : Boolean := False)
440 Top : constant EOA := Current;
441 Next : EOA;
442 GCC_Exception : GNAT_GCC_Exception_Access;
444 begin
445 -- The exception Excep is soon to be propagated, and the
446 -- storage used for that will be the occurrence statically allocated
447 -- for the current thread. This storage might currently be used for a
448 -- still active occurrence, so we need to push it on the thread's
449 -- occurrence stack (headed at that static occurrence) before it gets
450 -- clobbered.
452 -- What we do here is to trigger this push when need be, and allocate a
453 -- Private_Data block for the forthcoming Propagation.
455 -- Some tasking rendez-vous attempts lead to an occurrence transfer
456 -- from the server to the client (see Exceptional_Complete_Rendezvous).
457 -- In those cases Setup is called twice for the very same occurrence
458 -- before it gets propagated: once from the server, because this is
459 -- where the occurrence contents is elaborated and known, and then
460 -- once from the client when it detects the case and actually raises
461 -- the exception in its own context.
463 -- The Is_Setup_And_Not_Propagated predicate tells us when we are in
464 -- the second call to Setup for a Transferred occurrence, and there is
465 -- nothing to be done here in this situation. This predicate cannot be
466 -- True if we are dealing with a Reraise, and we may even be called
467 -- with a raw uninitialized Excep occurrence in this case so we should
468 -- not check anyway. Observe the front-end expansion for a "raise;" to
469 -- see that happening. We get a local occurrence and a direct call to
470 -- Save_Occurrence without the intermediate init-proc call.
472 if not Reraised and then Is_Setup_And_Not_Propagated (Excep) then
473 return;
474 end if;
476 -- Allocate what will be the Private_Data block for the exception
477 -- to be propagated.
479 GCC_Exception := new GNAT_GCC_Exception;
481 -- If the Top of the occurrence stack is not currently used for an
482 -- active exception (the stack is empty) we just need to setup the
483 -- Private_Data pointer.
485 -- Otherwise, we also need to shift the contents of the Top of the
486 -- stack in a freshly allocated entry and link everything together.
488 if Top.Private_Data /= System.Null_Address then
489 Next := new Exception_Occurrence;
490 Save_Occurrence_And_Private (Next.all, Top.all);
492 GCC_Exception.Next_Exception := Next;
493 Top.Private_Data := GCC_Exception.all'Address;
494 end if;
496 Top.Private_Data := GCC_Exception.all'Address;
498 Set_Setup_And_Not_Propagated (Top);
499 end Setup_Exception;
501 -------------------
502 -- Begin_Handler --
503 -------------------
505 procedure Begin_Handler (GCC_Exception : GNAT_GCC_Exception_Access) is
506 pragma Unreferenced (GCC_Exception);
508 begin
509 -- Every necessary operation related to the occurrence stack has
510 -- already been performed by Propagate_Exception. This hook remains for
511 -- potential future necessity in optimizing the overall scheme, as well
512 -- a useful debugging tool.
514 null;
515 end Begin_Handler;
517 -----------------
518 -- End_Handler --
519 -----------------
521 procedure End_Handler (GCC_Exception : GNAT_GCC_Exception_Access) is
522 Removed : Boolean;
523 begin
524 Removed := Remove (Get_Current_Excep.all, GCC_Exception);
525 pragma Assert (Removed);
526 end End_Handler;
528 -------------------------
529 -- Propagate_Exception --
530 -------------------------
532 -- Build an object suitable for the libgcc processing and call
533 -- Unwind_RaiseException to actually throw, taking care of handling
534 -- the two phase scheme it implements.
536 procedure Propagate_Exception
537 (E : Exception_Id;
538 From_Signal_Handler : Boolean)
540 pragma Inspection_Point (E);
541 pragma Unreferenced (From_Signal_Handler);
543 Excep : constant EOA := Get_Current_Excep.all;
544 GCC_Exception : GNAT_GCC_Exception_Access;
546 begin
547 pragma Assert (Excep.Private_Data /= System.Null_Address);
549 -- Retrieve the Private_Data for this occurrence and set the useful
550 -- flags for the personality routine, which will be called for each
551 -- frame via Unwind_RaiseException below.
553 GCC_Exception := To_GNAT_GCC_Exception (Excep.Private_Data);
555 Clear_Setup_And_Not_Propagated (Excep);
557 GCC_Exception.Id := Excep.Id;
558 GCC_Exception.N_Cleanups_To_Trigger := 0;
560 -- Compute the backtrace for this occurrence if the corresponding
561 -- binder option has been set. Call_Chain takes care of the reraise
562 -- case.
564 -- ??? Using Call_Chain here means we are going to walk up the stack
565 -- once only for backtracing purposes before doing it again for the
566 -- propagation per se.
568 -- The first inspection is much lighter, though, as it only requires
569 -- partial unwinding of each frame. Additionally, although we could use
570 -- the personality routine to record the addresses while propagating,
571 -- this method has two drawbacks:
573 -- 1) the trace is incomplete if the exception is handled since we
574 -- don't walk past the frame with the handler,
576 -- and
578 -- 2) we would miss the frames for which our personality routine is not
579 -- called, e.g. if C or C++ calls are on the way.
581 Call_Chain (Excep);
583 -- Perform a standard raise first. If a regular handler is found, it
584 -- will be entered after all the intermediate cleanups have run. If
585 -- there is no regular handler, control will get back to after the
586 -- call, with N_Cleanups_To_Trigger set to the number of frames with
587 -- cleanups found on the way up, and none of these already run.
589 Unwind_RaiseException (GCC_Exception);
591 -- If we get here we know the exception is not handled, as otherwise
592 -- Unwind_RaiseException arranges for the handler to be entered. Take
593 -- the necessary steps to enable the debugger to gain control while the
594 -- stack is still intact.
596 Notify_Unhandled_Exception;
598 -- Now, if cleanups have been found, run a forced unwind to trigger
599 -- them. Control should not resume there, as the unwinding hook calls
600 -- Unhandled_Exception_Terminate as soon as the last cleanup has been
601 -- triggered.
603 if GCC_Exception.N_Cleanups_To_Trigger /= 0 then
604 Unwind_ForcedUnwind (GCC_Exception,
605 CleanupUnwind_Handler'Address,
606 System.Null_Address);
607 end if;
609 -- We get here when there is no handler or cleanup to be run at all.
610 -- The debugger has been notified before the second step above.
612 Unhandled_Exception_Terminate;
613 end Propagate_Exception;
615 ---------------------------
616 -- Adjust_N_Cleanups_For --
617 ---------------------------
619 procedure Adjust_N_Cleanups_For
620 (GNAT_Exception : GNAT_GCC_Exception_Access;
621 Adjustment : Integer)
623 begin
624 GNAT_Exception.N_Cleanups_To_Trigger :=
625 GNAT_Exception.N_Cleanups_To_Trigger + Adjustment;
626 end Adjust_N_Cleanups_For;
628 -------------
629 -- EID_For --
630 -------------
632 function EID_For
633 (GNAT_Exception : GNAT_GCC_Exception_Access) return Exception_Id
635 begin
636 return GNAT_Exception.Id;
637 end EID_For;
639 ---------------------
640 -- Import_Code_For --
641 ---------------------
643 function Import_Code_For
644 (E : SSL.Exception_Data_Ptr) return Exception_Code
646 begin
647 return E.all.Import_Code;
648 end Import_Code_For;
650 --------------------------
651 -- Is_Handled_By_Others --
652 --------------------------
654 function Is_Handled_By_Others (E : SSL.Exception_Data_Ptr) return Boolean is
655 begin
656 return not E.all.Not_Handled_By_Others;
657 end Is_Handled_By_Others;
659 ------------------
660 -- Language_For --
661 ------------------
663 function Language_For (E : SSL.Exception_Data_Ptr) return Character is
664 begin
665 return E.all.Lang;
666 end Language_For;
668 -----------
669 -- Notes --
670 -----------
672 -- The current model implemented for the stack of occurrences is a
673 -- simplification of previous attempts, which all proved to be flawed or
674 -- would have needed significant additional circuitry to be made to work
675 -- correctly.
677 -- We now represent every propagation by a new entry on the stack, which
678 -- means that an exception occurrence may appear more than once (e.g. when
679 -- it is reraised during the course of its own handler).
681 -- This may seem overcostly compared to the C++ model as implemented in
682 -- the g++ v3 libstd. This is actually understandable when one considers
683 -- the extra variations of possible run-time configurations induced by the
684 -- freedom offered by the Save_Occurrence/Reraise_Occurrence public
685 -- interface.
687 -- The basic point is that arranging for an occurrence to always appear at
688 -- most once on the stack requires a way to determine if a given occurrence
689 -- is already there, which is not as easy as it might seem.
691 -- An attempt was made to use the Private_Data pointer for this purpose.
692 -- It did not work because:
694 -- 1) The Private_Data has to be saved by Save_Occurrence to be usable
695 -- as a key in case of a later reraise,
697 -- 2) There is no easy way to synchronize End_Handler for an occurrence
698 -- and the data attached to potential copies, so these copies may end
699 -- up pointing to stale data. Moreover ...
701 -- 3) The same address may be reused for different occurrences, which
702 -- defeats the idea of using it as a key.
704 -- The example below illustrates:
706 -- Saved_CE : Exception_Occurrence;
708 -- begin
709 -- raise Constraint_Error;
710 -- exception
711 -- when CE: others =>
712 -- Save_Occurrence (Saved_CE, CE); <= Saved_CE.PDA = CE.PDA
713 -- end;
715 -- <= Saved_CE.PDA is stale (!)
717 -- begin
718 -- raise Program_Error; <= Saved_CE.PDA = PE.PDA (!!)
719 -- exception
720 -- when others =>
721 -- Reraise_Occurrence (Saved_CE);
722 -- end;
724 -- Not releasing the Private_Data via End_Handler could be an option,
725 -- but making this to work while still avoiding memory leaks is far
726 -- from trivial.
728 -- The current scheme has the advantage of being simple, and induces
729 -- extra costs only in reraise cases which is acceptable.
731 end Exception_Propagation;