OSDN Git Service

f42d094ae0536ec21a9b57b6497cf72338c37b6f
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-except-2005.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-2006, 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 is used for all Ada 2005 builds. It differs from a-except.ads
39 --  only with respect to the addition of Wide_[Wide]Exception_Name functions.
40 --  The additional entities are marked with pragma Ada_05, so this extended
41 --  unit is also perfectly suitable for use in Ada 95 or Ada 83 mode.
42
43 --  The reason for this splitting off of a separate version is that bootstrap
44 --  compilers often will be used that do not support Ada 2005 features, and
45 --  Ada.Exceptions is part of the compiler sources.
46
47 --  The base version of this unit Ada.Exceptions omits the Wide version of
48 --  Exception_Name and is used to build the compiler and other basic tools.
49
50 pragma Polling (Off);
51 --  We must turn polling off for this unit, because otherwise we get
52 --  elaboration circularities with ourself.
53
54 with System;
55 with System.Parameters;
56 with System.Standard_Library;
57 with System.Traceback_Entries;
58
59 package Ada.Exceptions is
60    pragma Warnings (Off);
61    pragma Preelaborate_05;
62    pragma Warnings (On);
63    --  In accordance with Ada 2005 AI-362. The warnings pragmas are so that we
64    --  can compile this using older compiler versions, which will ignore the
65    --  pragma, which is fine for the bootstrap.
66
67    type Exception_Id is private;
68    pragma Preelaborable_Initialization (Exception_Id);
69
70    Null_Id : constant Exception_Id;
71
72    type Exception_Occurrence is limited private;
73    pragma Preelaborable_Initialization (Exception_Occurrence);
74
75    type Exception_Occurrence_Access is access all Exception_Occurrence;
76
77    Null_Occurrence : constant Exception_Occurrence;
78
79    function Exception_Name (Id : Exception_Id) return String;
80
81    function Exception_Name (X : Exception_Occurrence) return String;
82
83    function Wide_Exception_Name
84      (Id : Exception_Id) return Wide_String;
85    pragma Ada_05 (Wide_Exception_Name);
86
87    function Wide_Exception_Name
88      (X : Exception_Occurrence) return Wide_String;
89    pragma Ada_05 (Wide_Exception_Name);
90
91    function Wide_Wide_Exception_Name
92      (Id : Exception_Id) return Wide_Wide_String;
93    pragma Ada_05 (Wide_Wide_Exception_Name);
94
95    function Wide_Wide_Exception_Name
96      (X : Exception_Occurrence) return Wide_Wide_String;
97    pragma Ada_05 (Wide_Wide_Exception_Name);
98
99    procedure Raise_Exception (E : Exception_Id; Message : String := "");
100    --  Note: it would be really nice to give a pragma No_Return for this
101    --  procedure, but it would be wrong, since Raise_Exception does return
102    --  if given the null exception. However we do special case the name in
103    --  the test in the compiler for issuing a warning for a missing return
104    --  after this call. Program_Error seems reasonable enough in such a case.
105    --  See also the routine Raise_Exception_Always in the private part.
106
107    function Exception_Message (X : Exception_Occurrence) return String;
108
109    procedure Reraise_Occurrence (X : Exception_Occurrence);
110    --  Note: it would be really nice to give a pragma No_Return for this
111    --  procedure, but it would be wrong, since Reraise_Occurrence does return
112    --  if the argument is the null exception occurrence. See also procedure
113    --  Reraise_Occurrence_Always in the private part of this package.
114
115    function Exception_Identity (X : Exception_Occurrence) return Exception_Id;
116
117    function Exception_Information (X : Exception_Occurrence) return String;
118    --  The format of the exception information is as follows:
119    --
120    --    exception name (as in Exception_Name)
121    --    message (or a null line if no message)
122    --    PID=nnnn
123    --    0xyyyyyyyy 0xyyyyyyyy ...
124    --
125    --  The lines are separated by a ASCII.LF character
126    --  The nnnn is the partition Id given as decimal digits.
127    --  The 0x... line represents traceback program counter locations,
128    --  in order with the first one being the exception location.
129
130    --  Note on ordering: the compiler uses the Save_Occurrence procedure, but
131    --  not the function from Rtsfind, so it is important that the procedure
132    --  come first, since Rtsfind finds the first matching entity.
133
134    procedure Save_Occurrence
135      (Target : out Exception_Occurrence;
136       Source : Exception_Occurrence);
137
138    function Save_Occurrence
139      (Source : Exception_Occurrence)
140       return   Exception_Occurrence_Access;
141
142    --  Ada 2005 (AI-438): The language revision introduces the
143    --  following subprograms and attribute definitions. We do not
144    --  provide them explicitly; instead, the corresponding stream
145    --  attributes are made available through a pragma Stream_Convert
146    --  in the private part of this package.
147
148    --  procedure Read_Exception_Occurrence
149    --    (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
150    --     Item   : out Exception_Occurrence);
151
152    --  procedure Write_Exception_Occurrence
153    --    (Stream : not null access Ada.Streams.Root_Stream_Type'Class;
154    --     Item   : Exception_Occurrence);
155
156    --  for Exception_Occurrence'Read use Read_Exception_Occurrence;
157    --  for Exception_Occurrence'Write use Write_Exception_Occurrence;
158
159 private
160    package SSL renames System.Standard_Library;
161    package SP renames System.Parameters;
162
163    subtype EOA is Exception_Occurrence_Access;
164
165    Exception_Msg_Max_Length : constant := SP.Default_Exception_Msg_Max_Length;
166
167    ------------------
168    -- Exception_Id --
169    ------------------
170
171    subtype Code_Loc is System.Address;
172    --  Code location used in building exception tables and for call addresses
173    --  when propagating an exception. Values of this type are created by using
174    --  Label'Address or extracted from machine states using Get_Code_Loc.
175
176    Null_Loc : constant Code_Loc := System.Null_Address;
177    --  Null code location, used to flag outer level frame
178
179    type Exception_Id is new SSL.Exception_Data_Ptr;
180
181    function EId_To_String (X : Exception_Id) return String;
182    function String_To_EId (S : String) return Exception_Id;
183    pragma Stream_Convert (Exception_Id, String_To_EId, EId_To_String);
184    --  Functions for implementing Exception_Id stream attributes
185
186    Null_Id : constant Exception_Id := null;
187
188    -------------------------
189    -- Private Subprograms --
190    -------------------------
191
192    function Current_Target_Exception return Exception_Occurrence;
193    pragma Export
194      (Ada, Current_Target_Exception,
195       "__gnat_current_target_exception");
196    --  This routine should return the current raised exception on targets
197    --  which have built-in exception handling such as the Java Virtual
198    --  Machine. For other targets this routine is simply ignored. Currently,
199    --  only JGNAT uses this. See 4jexcept.ads for details. The pragma Export
200    --  allows this routine to be accessed elsewhere in the run-time, even
201    --  though it is in the private part of this package (it is not allowed
202    --  to be in the visible part, since this is set by the reference manual).
203
204    function Exception_Name_Simple (X : Exception_Occurrence) return String;
205    --  Like Exception_Name, but returns the simple non-qualified name of the
206    --  exception. This is used to implement the Exception_Name function in
207    --  Current_Exceptions (the DEC compatible unit). It is called from the
208    --  compiler generated code (using Rtsfind, which does not respect the
209    --  private barrier, so we can place this function in the private part
210    --  where the compiler can find it, but the spec is unchanged.)
211
212    procedure Local_Raise (Excep : Exception_Id);
213    pragma Export (Ada, Local_Raise);
214    --  This is a dummy routine, used only by the debugger for the purpose of
215    --  logging local raise statements that were transformed into a direct goto
216    --  to the handler code. The compiler in this case generates:
217    --
218    --    Local_Raise (exception_id);
219    --    goto Handler
220
221    procedure Raise_Exception_Always (E : Exception_Id; Message : String := "");
222    pragma No_Return (Raise_Exception_Always);
223    pragma Export (Ada, Raise_Exception_Always, "__gnat_raise_exception");
224    --  This differs from Raise_Exception only in that the caller has determined
225    --  that for sure the parameter E is not null, and that therefore the call
226    --  to this procedure cannot return. The expander converts Raise_Exception
227    --  calls to Raise_Exception_Always if it can determine this is the case.
228    --  The Export allows this routine to be accessed from Pure units.
229
230    procedure Raise_From_Signal_Handler
231      (E : Exception_Id;
232       M : System.Address);
233    pragma Export
234      (Ada, Raise_From_Signal_Handler,
235            "ada__exceptions__raise_from_signal_handler");
236    pragma No_Return (Raise_From_Signal_Handler);
237    --  This routine is used to raise an exception from a signal handler. The
238    --  signal handler has already stored the machine state (i.e. the state that
239    --  corresponds to the location at which the signal was raised). E is the
240    --  Exception_Id specifying what exception is being raised, and M is a
241    --  pointer to a null-terminated string which is the message to be raised.
242    --  Note that this routine never returns, so it is permissible to simply
243    --  jump to this routine, rather than call it. This may be appropriate for
244    --  systems where the right way to get out of signal handler is to alter the
245    --  PC value in the machine state or in some other way ask the operating
246    --  system to return here rather than to the original location.
247
248    procedure Reraise_Occurrence_Always (X : Exception_Occurrence);
249    pragma No_Return (Reraise_Occurrence_Always);
250    --  This differs from Raise_Occurrence only in that the caller guarantees
251    --  that for sure the parameter X is not the null occurrence, and that
252    --  therefore this procedure cannot return. The expander uses this routine
253    --  in the translation of a raise statement with no parameter (reraise).
254
255    procedure Reraise_Occurrence_No_Defer (X : Exception_Occurrence);
256    pragma No_Return (Reraise_Occurrence_No_Defer);
257    --  Exactly like Reraise_Occurrence, except that abort is not deferred
258    --  before the call and the parameter X is known not to be the null
259    --  occurrence. This is used in generated code when it is known that
260    --  abort is already deferred.
261
262    -----------------------
263    -- Polling Interface --
264    -----------------------
265
266    --  The GNAT compiler has an option to generate polling calls to the Poll
267    --  routine in this package. Specifying the -gnatP option for a compilation
268    --  causes a call to Ada.Exceptions.Poll to be generated on every subprogram
269    --  entry and on every iteration of a loop, thus avoiding the possibility of
270    --  a case of unbounded time between calls.
271
272    --  This polling interface may be used for instrumentation or debugging
273    --  purposes (e.g. implementing watchpoints in software or in the debugger).
274
275    --  In the GNAT technology itself, this interface is used to implement
276    --  immediate aynschronous transfer of control and immediate abort on
277    --  targets which do not provide for one thread interrupting another.
278
279    --  Note: this used to be in a separate unit called System.Poll, but that
280    --  caused horrible circular elaboration problems between System.Poll and
281    --  Ada.Exceptions. One way of solving such circularities is unification!
282
283    procedure Poll;
284    --  Check for asynchronous abort. Note that we do not inline the body.
285    --  This makes the interface more useful for debugging purposes.
286
287    --------------------------
288    -- Exception_Occurrence --
289    --------------------------
290
291    package TBE renames System.Traceback_Entries;
292
293    Max_Tracebacks : constant := 50;
294    --  Maximum number of trace backs stored in exception occurrence
295
296    type Tracebacks_Array is array (1 .. Max_Tracebacks) of TBE.Traceback_Entry;
297    --  Traceback array stored in exception occurrence
298
299    type Exception_Occurrence is record
300       Id : Exception_Id;
301       --  Exception_Identity for this exception occurrence
302       --  WARNING System.System.Finalization_Implementation.Finalize_List
303       --  relies on the fact that this field is always first in the exception
304       --  occurrence
305
306       Msg_Length : Natural := 0;
307       --  Length of message (zero = no message)
308
309       Msg : String (1 .. Exception_Msg_Max_Length);
310       --  Characters of message
311
312       Cleanup_Flag : Boolean := False;
313       --  The cleanup flag is normally False, it is set True for an exception
314       --  occurrence passed to a cleanup routine, and will still be set True
315       --  when the cleanup routine does a Reraise_Occurrence call using this
316       --  exception occurrence. This is used to avoid recording a bogus trace
317       --  back entry from this reraise call.
318
319       Exception_Raised : Boolean := False;
320       --  Set to true to indicate that this exception occurrence has actually
321       --  been raised. When an exception occurrence is first created, this is
322       --  set to False, then when it is processed by Raise_Current_Exception,
323       --  it is set to True. If Raise_Current_Exception is used to raise an
324       --  exception for which this flag is already True, then it knows that
325       --  it is dealing with the reraise case (which is useful to distinguish
326       --  for exception tracing purposes).
327
328       Pid : Natural := 0;
329       --  Partition_Id for partition raising exception
330
331       Num_Tracebacks : Natural range 0 .. Max_Tracebacks := 0;
332       --  Number of traceback entries stored
333
334       Tracebacks : Tracebacks_Array;
335       --  Stored tracebacks (in Tracebacks (1 .. Num_Tracebacks))
336
337       Private_Data : System.Address := System.Null_Address;
338       --  Field used by low level exception mechanism to store specific data.
339       --  Currently used by the GCC exception mechanism to store a pointer to
340       --  a GNAT_GCC_Exception.
341    end record;
342
343    function "=" (Left, Right : Exception_Occurrence) return Boolean
344      is abstract;
345    --  Don't allow comparison on exception occurrences, we should not need
346    --  this, and it would not work right, because of the Msg and Tracebacks
347    --  fields which have unused entries not copied by Save_Occurrence.
348
349    function EO_To_String (X : Exception_Occurrence) return String;
350    function String_To_EO (S : String) return Exception_Occurrence;
351    pragma Stream_Convert (Exception_Occurrence, String_To_EO, EO_To_String);
352    --  Functions for implementing Exception_Occurrence stream attributes
353
354    Null_Occurrence : constant Exception_Occurrence := (
355      Id               => null,
356      Msg_Length       => 0,
357      Msg              => (others => ' '),
358      Cleanup_Flag     => False,
359      Exception_Raised => False,
360      Pid              => 0,
361      Num_Tracebacks   => 0,
362      Tracebacks       => (others => TBE.Null_TB_Entry),
363      Private_Data     => System.Null_Address);
364
365 end Ada.Exceptions;