OSDN Git Service

2009-04-08 Thomas Quinot <quinot@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-except.ads
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT RUN-TIME COMPONENTS                         --
4 --                                                                          --
5 --                       A D A . E X C E P T I O N S                        --
6 --                                                                          --
7 --                                 S p e c                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- This specification is derived from the Ada Reference Manual for use with --
12 -- GNAT. The copyright notice above, and the license provisions that follow --
13 -- apply solely to the  contents of the part following the private keyword. --
14 --                                                                          --
15 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
16 -- terms of the  GNU General Public License as published  by the Free Soft- --
17 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
18 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
19 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
20 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
21 -- for  more details.  You should have  received  a copy of the GNU General --
22 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
23 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
24 -- Boston, MA 02110-1301, USA.                                              --
25 --                                                                          --
26 -- As a special exception,  if other files  instantiate  generics from this --
27 -- unit, or you link  this unit with other files  to produce an executable, --
28 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
29 -- covered  by the  GNU  General  Public  License.  This exception does not --
30 -- however invalidate  any other reasons why  the executable file  might be --
31 -- covered by the  GNU Public License.                                      --
32 --                                                                          --
33 -- GNAT was originally developed  by the GNAT team at  New York University. --
34 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
35 --                                                                          --
36 ------------------------------------------------------------------------------
37
38 --  This version of Ada.Exceptions is a full Ada 95 version. It omits Ada 2005
39 --  features such as the additional definitions of Exception_Name returning
40 --  Wide_[Wide_]String.
41
42 --  It is used for building the compiler and the basic tools, since these
43 --  builds may be done with bootstrap compilers that cannot handle these
44 --  additions. The full version of Ada.Exceptions can be found in the files
45 --  a-except-2005.ads/adb, and is used for all other builds where full Ada
46 --  2005 functionality is required. In particular, it is used for building
47 --  run times on all targets.
48
49 pragma Polling (Off);
50 --  We must turn polling off for this unit, because otherwise we get
51 --  elaboration circularities with ourself.
52
53 pragma Warnings (Off);
54 pragma Compiler_Unit;
55 pragma Warnings (On);
56
57 with System;
58 with System.Parameters;
59 with System.Standard_Library;
60 with System.Traceback_Entries;
61
62 package Ada.Exceptions is
63    pragma Warnings (Off);
64    pragma Preelaborate_05;
65    pragma Warnings (On);
66    --  We make this preelaborable in Ada 2005 mode. If we did not do this, then
67    --  run time units used by the compiler (e.g. s-soflin.ads) would run
68    --  into trouble. Conformance is not an issue, since this version is used
69    --  only by the compiler.
70
71    type Exception_Id is private;
72
73    Null_Id : constant Exception_Id;
74
75    type Exception_Occurrence is limited private;
76
77    type Exception_Occurrence_Access is access all Exception_Occurrence;
78
79    Null_Occurrence : constant Exception_Occurrence;
80
81    function Exception_Name (X : Exception_Occurrence) return String;
82    --  Same as Exception_Name (Exception_Identity (X))
83
84    function Exception_Name (Id : Exception_Id) return String;
85
86    procedure Raise_Exception (E : Exception_Id; Message : String := "");
87    pragma No_Return (Raise_Exception);
88    --  Note: In accordance with AI-466, CE is raised if E = Null_Id
89
90    function Exception_Message (X : Exception_Occurrence) return String;
91
92    procedure Reraise_Occurrence (X : Exception_Occurrence);
93    --  Note: it would be really nice to give a pragma No_Return for this
94    --  procedure, but it would be wrong, since Reraise_Occurrence does return
95    --  if the argument is the null exception occurrence. See also procedure
96    --  Reraise_Occurrence_Always in the private part of this package.
97
98    function Exception_Identity (X : Exception_Occurrence) return Exception_Id;
99
100    function Exception_Information (X : Exception_Occurrence) return String;
101    --  The format of the exception information is as follows:
102    --
103    --    exception name (as in Exception_Name)
104    --    message (or a null line if no message)
105    --    PID=nnnn
106    --    0xyyyyyyyy 0xyyyyyyyy ...
107    --
108    --  The lines are separated by a ASCII.LF character
109    --  The nnnn is the partition Id given as decimal digits.
110    --  The 0x... line represents traceback program counter locations,
111    --  in order with the first one being the exception location.
112
113    --  Note on ordering: the compiler uses the Save_Occurrence procedure, but
114    --  not the function from Rtsfind, so it is important that the procedure
115    --  come first, since Rtsfind finds the first matching entity.
116
117    procedure Save_Occurrence
118      (Target : out Exception_Occurrence;
119       Source : Exception_Occurrence);
120
121    function Save_Occurrence
122      (Source : Exception_Occurrence)
123       return   Exception_Occurrence_Access;
124
125 private
126    package SSL renames System.Standard_Library;
127    package SP renames System.Parameters;
128
129    subtype EOA is Exception_Occurrence_Access;
130
131    Exception_Msg_Max_Length : constant := SP.Default_Exception_Msg_Max_Length;
132
133    ------------------
134    -- Exception_Id --
135    ------------------
136
137    subtype Code_Loc is System.Address;
138    --  Code location used in building exception tables and for call addresses
139    --  when propagating an exception. Values of this type are created by using
140    --  Label'Address or extracted from machine states using Get_Code_Loc.
141
142    Null_Loc : constant Code_Loc := System.Null_Address;
143    --  Null code location, used to flag outer level frame
144
145    type Exception_Id is new SSL.Exception_Data_Ptr;
146
147    function EId_To_String (X : Exception_Id) return String;
148    function String_To_EId (S : String) return Exception_Id;
149    pragma Stream_Convert (Exception_Id, String_To_EId, EId_To_String);
150    --  Functions for implementing Exception_Id stream attributes
151
152    Null_Id : constant Exception_Id := null;
153
154    -------------------------
155    -- Private Subprograms --
156    -------------------------
157
158    function Current_Target_Exception return Exception_Occurrence;
159    pragma Export
160      (Ada, Current_Target_Exception,
161       "__gnat_current_target_exception");
162    --  This routine should return the current raised exception on targets
163    --  which have built-in exception handling such as the Java Virtual
164    --  Machine. For other targets this routine is simply ignored. Currently,
165    --  only JGNAT uses this. See 4jexcept.ads for details. The pragma Export
166    --  allows this routine to be accessed elsewhere in the run-time, even
167    --  though it is in the private part of this package (it is not allowed
168    --  to be in the visible part, since this is set by the reference manual).
169
170    function Exception_Name_Simple (X : Exception_Occurrence) return String;
171    --  Like Exception_Name, but returns the simple non-qualified name of the
172    --  exception. This is used to implement the Exception_Name function in
173    --  Current_Exceptions (the DEC compatible unit). It is called from the
174    --  compiler generated code (using Rtsfind, which does not respect the
175    --  private barrier, so we can place this function in the private part
176    --  where the compiler can find it, but the spec is unchanged.)
177
178    procedure Raise_Exception_Always (E : Exception_Id; Message : String := "");
179    pragma No_Return (Raise_Exception_Always);
180    pragma Export (Ada, Raise_Exception_Always, "__gnat_raise_exception");
181    --  This differs from Raise_Exception only in that the caller has determined
182    --  that for sure the parameter E is not null, and that therefore no check
183    --  for Null_Id is required. The expander converts Raise_Exception calls to
184    --  Raise_Exception_Always if it can determine this is the case. The Export
185    --  allows this routine to be accessed from Pure units.
186
187    procedure Raise_From_Signal_Handler
188      (E : Exception_Id;
189       M : System.Address);
190    pragma Export
191      (Ada, Raise_From_Signal_Handler,
192            "ada__exceptions__raise_from_signal_handler");
193    pragma No_Return (Raise_From_Signal_Handler);
194    --  This routine is used to raise an exception from a signal handler. The
195    --  signal handler has already stored the machine state (i.e. the state that
196    --  corresponds to the location at which the signal was raised). E is the
197    --  Exception_Id specifying what exception is being raised, and M is a
198    --  pointer to a null-terminated string which is the message to be raised.
199    --  Note that this routine never returns, so it is permissible to simply
200    --  jump to this routine, rather than call it. This may be appropriate for
201    --  systems where the right way to get out of signal handler is to alter the
202    --  PC value in the machine state or in some other way ask the operating
203    --  system to return here rather than to the original location.
204
205    procedure Raise_From_Controlled_Operation
206      (X : Ada.Exceptions.Exception_Occurrence);
207    pragma No_Return (Raise_From_Controlled_Operation);
208    --  Raise Program_Error, providing information about X (an exception
209    --  raised during a controlled operation) in the exception message.
210
211    procedure Reraise_Occurrence_Always (X : Exception_Occurrence);
212    pragma No_Return (Reraise_Occurrence_Always);
213    --  This differs from Raise_Occurrence only in that the caller guarantees
214    --  that for sure the parameter X is not the null occurrence, and that
215    --  therefore this procedure cannot return. The expander uses this routine
216    --  in the translation of a raise statement with no parameter (reraise).
217
218    procedure Reraise_Occurrence_No_Defer (X : Exception_Occurrence);
219    pragma No_Return (Reraise_Occurrence_No_Defer);
220    --  Exactly like Reraise_Occurrence, except that abort is not deferred
221    --  before the call and the parameter X is known not to be the null
222    --  occurrence. This is used in generated code when it is known that
223    --  abort is already deferred.
224
225    -----------------------
226    -- Polling Interface --
227    -----------------------
228
229    --  The GNAT compiler has an option to generate polling calls to the Poll
230    --  routine in this package. Specifying the -gnatP option for a compilation
231    --  causes a call to Ada.Exceptions.Poll to be generated on every subprogram
232    --  entry and on every iteration of a loop, thus avoiding the possibility of
233    --  a case of unbounded time between calls.
234
235    --  This polling interface may be used for instrumentation or debugging
236    --  purposes (e.g. implementing watchpoints in software or in the debugger).
237
238    --  In the GNAT technology itself, this interface is used to implement
239    --  immediate asynchronous transfer of control and immediate abort on
240    --  targets which do not provide for one thread interrupting another.
241
242    --  Note: this used to be in a separate unit called System.Poll, but that
243    --  caused horrible circular elaboration problems between System.Poll and
244    --  Ada.Exceptions. One way of solving such circularities is unification!
245
246    procedure Poll;
247    --  Check for asynchronous abort. Note that we do not inline the body.
248    --  This makes the interface more useful for debugging purposes.
249
250    --------------------------
251    -- Exception_Occurrence --
252    --------------------------
253
254    package TBE renames System.Traceback_Entries;
255
256    Max_Tracebacks : constant := 50;
257    --  Maximum number of trace backs stored in exception occurrence
258
259    type Tracebacks_Array is array (1 .. Max_Tracebacks) of TBE.Traceback_Entry;
260    --  Traceback array stored in exception occurrence
261
262    type Exception_Occurrence is record
263       Id : Exception_Id;
264       --  Exception_Identity for this exception occurrence
265       --  WARNING System.System.Finalization_Implementation.Finalize_List
266       --  relies on the fact that this field is always first in the exception
267       --  occurrence
268
269       Msg_Length : Natural := 0;
270       --  Length of message (zero = no message)
271
272       Msg : String (1 .. Exception_Msg_Max_Length);
273       --  Characters of message
274
275       Cleanup_Flag : Boolean := False;
276       --  The cleanup flag is normally False, it is set True for an exception
277       --  occurrence passed to a cleanup routine, and will still be set True
278       --  when the cleanup routine does a Reraise_Occurrence call using this
279       --  exception occurrence. This is used to avoid recording a bogus trace
280       --  back entry from this reraise call.
281
282       Exception_Raised : Boolean := False;
283       --  Set to true to indicate that this exception occurrence has actually
284       --  been raised. When an exception occurrence is first created, this is
285       --  set to False, then when it is processed by Raise_Current_Exception,
286       --  it is set to True. If Raise_Current_Exception is used to raise an
287       --  exception for which this flag is already True, then it knows that
288       --  it is dealing with the reraise case (which is useful to distinguish
289       --  for exception tracing purposes).
290
291       Pid : Natural := 0;
292       --  Partition_Id for partition raising exception
293
294       Num_Tracebacks : Natural range 0 .. Max_Tracebacks := 0;
295       --  Number of traceback entries stored
296
297       Tracebacks : Tracebacks_Array;
298       --  Stored tracebacks (in Tracebacks (1 .. Num_Tracebacks))
299
300       Private_Data : System.Address := System.Null_Address;
301       --  Field used by low level exception mechanism to store specific data.
302       --  Currently used by the GCC exception mechanism to store a pointer to
303       --  a GNAT_GCC_Exception.
304    end record;
305
306    function "=" (Left, Right : Exception_Occurrence) return Boolean
307      is abstract;
308    --  Don't allow comparison on exception occurrences, we should not need
309    --  this, and it would not work right, because of the Msg and Tracebacks
310    --  fields which have unused entries not copied by Save_Occurrence.
311
312    function EO_To_String (X : Exception_Occurrence) return String;
313    function String_To_EO (S : String) return Exception_Occurrence;
314    pragma Stream_Convert (Exception_Occurrence, String_To_EO, EO_To_String);
315    --  Functions for implementing Exception_Occurrence stream attributes
316
317    Null_Occurrence : constant Exception_Occurrence := (
318      Id               => null,
319      Msg_Length       => 0,
320      Msg              => (others => ' '),
321      Cleanup_Flag     => False,
322      Exception_Raised => False,
323      Pid              => 0,
324      Num_Tracebacks   => 0,
325      Tracebacks       => (others => TBE.Null_TB_Entry),
326      Private_Data     => System.Null_Address);
327
328 end Ada.Exceptions;