| 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-2025, 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 3, 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. -- |
| 21 |
-- -- |
| 22 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 23 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 24 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 25 |
-- -- |
| 26 |
-- You should have received a copy of the GNU General Public License and -- |
| 27 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 28 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 29 |
-- <http://www.gnu.org/licenses/>. -- |
| 30 |
-- -- |
| 31 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 32 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 33 |
-- -- |
| 34 |
------------------------------------------------------------------------------ |
| 35 |
|
| 36 |
-- This is the default version of this package. We also have cert and zfp |
| 37 |
-- versions. |
| 38 |
|
| 39 |
with System; |
| 40 |
with System.Parameters; |
| 41 |
with System.Standard_Library; |
| 42 |
with System.Traceback_Entries; |
| 43 |
|
| 44 |
package Ada.Exceptions is |
| 45 |
pragma Preelaborate; |
| 46 |
-- In accordance with Ada 2005 AI-362. |
| 47 |
|
| 48 |
type Exception_Id is private; |
| 49 |
pragma Preelaborable_Initialization (Exception_Id); |
| 50 |
|
| 51 |
Null_Id : constant Exception_Id; |
| 52 |
|
| 53 |
type Exception_Occurrence is limited private; |
| 54 |
pragma Preelaborable_Initialization (Exception_Occurrence); |
| 55 |
|
| 56 |
type Exception_Occurrence_Access is access all Exception_Occurrence; |
| 57 |
|
| 58 |
Null_Occurrence : constant Exception_Occurrence; |
| 59 |
|
| 60 |
function Exception_Name (Id : Exception_Id) return String; |
| 61 |
|
| 62 |
function Exception_Name (X : Exception_Occurrence) return String; |
| 63 |
|
| 64 |
function Wide_Exception_Name |
| 65 |
(Id : Exception_Id) return Wide_String; |
| 66 |
pragma Ada_05 (Wide_Exception_Name); |
| 67 |
|
| 68 |
function Wide_Exception_Name |
| 69 |
(X : Exception_Occurrence) return Wide_String; |
| 70 |
pragma Ada_05 (Wide_Exception_Name); |
| 71 |
|
| 72 |
function Wide_Wide_Exception_Name |
| 73 |
(Id : Exception_Id) return Wide_Wide_String; |
| 74 |
pragma Ada_05 (Wide_Wide_Exception_Name); |
| 75 |
|
| 76 |
function Wide_Wide_Exception_Name |
| 77 |
(X : Exception_Occurrence) return Wide_Wide_String; |
| 78 |
pragma Ada_05 (Wide_Wide_Exception_Name); |
| 79 |
|
| 80 |
procedure Raise_Exception (E : Exception_Id; Message : String := ""); |
| 81 |
pragma No_Return (Raise_Exception); |
| 82 |
-- Note: In accordance with AI-466, CE is raised if E = Null_Id |
| 83 |
|
| 84 |
function Exception_Message (X : Exception_Occurrence) return String; |
| 85 |
|
| 86 |
procedure Reraise_Occurrence (X : Exception_Occurrence); |
| 87 |
-- Note: it would be really nice to give a pragma No_Return for this |
| 88 |
-- procedure, but it would be wrong, since Reraise_Occurrence does return |
| 89 |
-- if the argument is the null exception occurrence. See also procedure |
| 90 |
-- Reraise_Occurrence_Always in the private part of this package. |
| 91 |
|
| 92 |
function Exception_Identity (X : Exception_Occurrence) return Exception_Id; |
| 93 |
|
| 94 |
function Exception_Information (X : Exception_Occurrence) return String; |
| 95 |
-- The format of the exception information is as follows: |
| 96 |
-- |
| 97 |
-- exception name (as in Exception_Name) |
| 98 |
-- message (or a null line if no message) |
| 99 |
-- PID=nnnn |
| 100 |
-- 0xyyyyyyyy 0xyyyyyyyy ... |
| 101 |
-- |
| 102 |
-- The lines are separated by a ASCII.LF character |
| 103 |
-- |
| 104 |
-- The nnnn is the partition Id given as decimal digits |
| 105 |
-- |
| 106 |
-- The 0x... line represents traceback program counter locations, |
| 107 |
-- in order with the first one being the exception location. |
| 108 |
|
| 109 |
-- Note on ordering: the compiler uses the Save_Occurrence procedure, but |
| 110 |
-- not the function from Rtsfind, so it is important that the procedure |
| 111 |
-- come first, since Rtsfind finds the first matching entity. |
| 112 |
|
| 113 |
procedure Save_Occurrence |
| 114 |
(Target : out Exception_Occurrence; |
| 115 |
Source : Exception_Occurrence); |
| 116 |
|
| 117 |
function Save_Occurrence |
| 118 |
(Source : Exception_Occurrence) |
| 119 |
return Exception_Occurrence_Access; |
| 120 |
|
| 121 |
-- Ada 2005 (AI-438): The language revision introduces the following |
| 122 |
-- subprograms and attribute definitions. We do not provide them |
| 123 |
-- explicitly. Instead, the corresponding stream attributes are made |
| 124 |
-- available through a pragma Stream_Convert in the private part. |
| 125 |
|
| 126 |
-- procedure Read_Exception_Occurrence |
| 127 |
-- (Stream : not null access Ada.Streams.Root_Stream_Type'Class; |
| 128 |
-- Item : out Exception_Occurrence); |
| 129 |
|
| 130 |
-- procedure Write_Exception_Occurrence |
| 131 |
-- (Stream : not null access Ada.Streams.Root_Stream_Type'Class; |
| 132 |
-- Item : Exception_Occurrence); |
| 133 |
|
| 134 |
-- for Exception_Occurrence'Read use Read_Exception_Occurrence; |
| 135 |
-- for Exception_Occurrence'Write use Write_Exception_Occurrence; |
| 136 |
|
| 137 |
private |
| 138 |
package SSL renames System.Standard_Library; |
| 139 |
package SP renames System.Parameters; |
| 140 |
|
| 141 |
subtype EOA is Exception_Occurrence_Access; |
| 142 |
|
| 143 |
Exception_Msg_Max_Length : constant := SP.Default_Exception_Msg_Max_Length; |
| 144 |
|
| 145 |
------------------ |
| 146 |
-- Exception_Id -- |
| 147 |
------------------ |
| 148 |
|
| 149 |
subtype Code_Loc is System.Address; |
| 150 |
-- Code location used in building exception tables and for call addresses |
| 151 |
-- when propagating an exception. Values of this type are created by using |
| 152 |
-- Label'Address or extracted from machine states using Get_Code_Loc. |
| 153 |
|
| 154 |
Null_Loc : constant Code_Loc := System.Null_Address; |
| 155 |
-- Null code location, used to flag outer level frame |
| 156 |
|
| 157 |
type Exception_Id is new SSL.Exception_Data_Ptr; |
| 158 |
|
| 159 |
function EId_To_String (X : Exception_Id) return String; |
| 160 |
function String_To_EId (S : String) return Exception_Id; |
| 161 |
pragma Stream_Convert (Exception_Id, String_To_EId, EId_To_String); |
| 162 |
-- Functions for implementing Exception_Id stream attributes |
| 163 |
|
| 164 |
Null_Id : constant Exception_Id := null; |
| 165 |
|
| 166 |
pragma Machine_Attribute (Raise_Exception, "expected_throw"); |
| 167 |
pragma Machine_Attribute (Reraise_Occurrence, "expected_throw"); |
| 168 |
-- Tell the compiler that an exception is likely after calling |
| 169 |
-- these subprograms. This could eventually be used for hot/cold |
| 170 |
-- partitioning. For now, this only enables the control flow |
| 171 |
-- redundancy to avoid duplicating a check before the No_Return |
| 172 |
-- call and in the exception handler for the call. |
| 173 |
|
| 174 |
------------------------- |
| 175 |
-- Private Subprograms -- |
| 176 |
------------------------- |
| 177 |
|
| 178 |
function Exception_Name_Simple (X : Exception_Occurrence) return String; |
| 179 |
-- Like Exception_Name, but returns the simple non-qualified name of the |
| 180 |
-- exception. This is used to implement the Exception_Name function in |
| 181 |
-- Current_Exceptions (the DEC compatible unit). It is called from the |
| 182 |
-- compiler generated code (using Rtsfind, which does not respect the |
| 183 |
-- private barrier, so we can place this function in the private part |
| 184 |
-- where the compiler can find it, but the spec is unchanged.) |
| 185 |
|
| 186 |
procedure Raise_Exception_Always (E : Exception_Id; Message : String := ""); |
| 187 |
pragma No_Return (Raise_Exception_Always); |
| 188 |
pragma Machine_Attribute (Raise_Exception_Always, "expected_throw"); |
| 189 |
pragma Export (Ada, Raise_Exception_Always, "__gnat_raise_exception"); |
| 190 |
-- This differs from Raise_Exception only in that the caller has determined |
| 191 |
-- that for sure the parameter E is not null, and that therefore no check |
| 192 |
-- for Null_Id is required. The expander converts Raise_Exception calls to |
| 193 |
-- Raise_Exception_Always if it can determine this is the case. The Export |
| 194 |
-- allows this routine to be accessed from Pure units. |
| 195 |
|
| 196 |
pragma Machine_Attribute (Raise_Exception_Always, |
| 197 |
"strub", "callable"); |
| 198 |
-- Make it callable from strub contexts |
| 199 |
|
| 200 |
procedure Raise_From_Controlled_Operation (X : Exception_Occurrence); |
| 201 |
pragma No_Return (Raise_From_Controlled_Operation); |
| 202 |
pragma Export |
| 203 |
(Ada, Raise_From_Controlled_Operation, |
| 204 |
"__gnat_raise_from_controlled_operation"); |
| 205 |
-- Raise Program_Error, providing information about X (an exception raised |
| 206 |
-- during a controlled operation) in the exception message. |
| 207 |
pragma Machine_Attribute (Raise_From_Controlled_Operation, |
| 208 |
"expected_throw"); |
| 209 |
-- Mark it like internal exception-raising subprograms |
| 210 |
|
| 211 |
procedure Reraise_Library_Exception_If_Any; |
| 212 |
pragma Export |
| 213 |
(Ada, Reraise_Library_Exception_If_Any, |
| 214 |
"__gnat_reraise_library_exception_if_any"); |
| 215 |
-- If there was an exception raised during library-level finalization, |
| 216 |
-- reraise the exception. |
| 217 |
|
| 218 |
procedure Reraise_Occurrence_Always (X : Exception_Occurrence); |
| 219 |
pragma No_Return (Reraise_Occurrence_Always); |
| 220 |
pragma Machine_Attribute (Reraise_Occurrence_Always, "expected_throw"); |
| 221 |
-- This differs from Raise_Occurrence only in that the caller guarantees |
| 222 |
-- that for sure the parameter X is not the null occurrence, and that |
| 223 |
-- therefore this procedure cannot return. The expander uses this routine |
| 224 |
-- in the translation of a raise statement with no parameter (reraise). |
| 225 |
|
| 226 |
procedure Reraise_Occurrence_No_Defer (X : Exception_Occurrence); |
| 227 |
pragma No_Return (Reraise_Occurrence_No_Defer); |
| 228 |
pragma Machine_Attribute (Reraise_Occurrence_No_Defer, "expected_throw"); |
| 229 |
-- Exactly like Reraise_Occurrence, except that abort is not deferred |
| 230 |
-- before the call and the parameter X is known not to be the null |
| 231 |
-- occurrence. This is used in generated code when it is known that abort |
| 232 |
-- is already deferred. |
| 233 |
|
| 234 |
function Triggered_By_Abort return Boolean; |
| 235 |
-- Determine whether the current exception (if it exists) is an instance of |
| 236 |
-- Standard'Abort_Signal. |
| 237 |
|
| 238 |
-------------------------- |
| 239 |
-- Exception_Occurrence -- |
| 240 |
-------------------------- |
| 241 |
|
| 242 |
package TBE renames System.Traceback_Entries; |
| 243 |
|
| 244 |
Max_Tracebacks : constant := 50; |
| 245 |
-- Maximum number of trace backs stored in exception occurrence |
| 246 |
|
| 247 |
subtype Tracebacks_Array is TBE.Tracebacks_Array (1 .. Max_Tracebacks); |
| 248 |
-- Traceback array stored in exception occurrence |
| 249 |
|
| 250 |
type Exception_Occurrence is record |
| 251 |
Id : Exception_Id := Null_Id; |
| 252 |
-- Exception_Identity for this exception occurrence |
| 253 |
|
| 254 |
Machine_Occurrence : System.Address; |
| 255 |
-- The underlying machine occurrence. For GCC, this corresponds to the |
| 256 |
-- _Unwind_Exception structure address. |
| 257 |
|
| 258 |
Msg_Length : Natural := 0; |
| 259 |
-- Length of message (zero = no message) |
| 260 |
|
| 261 |
Msg : String (1 .. Exception_Msg_Max_Length); |
| 262 |
-- Characters of message |
| 263 |
|
| 264 |
Exception_Raised : Boolean := False; |
| 265 |
-- Set to true to indicate that this exception occurrence has actually |
| 266 |
-- been raised. When an exception occurrence is first created, this is |
| 267 |
-- set to False, then when it is processed by Raise_Current_Exception, |
| 268 |
-- it is set to True. If Raise_Current_Exception is used to raise an |
| 269 |
-- exception for which this flag is already True, then it knows that |
| 270 |
-- it is dealing with the reraise case (which is useful to distinguish |
| 271 |
-- for exception tracing purposes). |
| 272 |
|
| 273 |
Pid : Natural := 0; |
| 274 |
-- Partition_Id for partition raising exception |
| 275 |
|
| 276 |
Num_Tracebacks : Natural range 0 .. Max_Tracebacks := 0; |
| 277 |
-- Number of traceback entries stored |
| 278 |
|
| 279 |
Tracebacks : Tracebacks_Array; |
| 280 |
-- Stored tracebacks (in Tracebacks (1 .. Num_Tracebacks)) |
| 281 |
end record; |
| 282 |
|
| 283 |
function "=" (Left, Right : Exception_Occurrence) return Boolean |
| 284 |
is abstract; |
| 285 |
-- Don't allow comparison on exception occurrences, we should not need |
| 286 |
-- this, and it would not work right, because of the Msg and Tracebacks |
| 287 |
-- fields which have unused entries not copied by Save_Occurrence. |
| 288 |
|
| 289 |
function Get_Exception_Machine_Occurrence |
| 290 |
(X : Exception_Occurrence) return System.Address; |
| 291 |
pragma Export (Ada, Get_Exception_Machine_Occurrence, |
| 292 |
"__gnat_get_exception_machine_occurrence"); |
| 293 |
-- Get the machine occurrence corresponding to an exception occurrence. |
| 294 |
-- It is Null_Address if there is no machine occurrence (in runtimes that |
| 295 |
-- doesn't use GCC mechanism) or if it has been lost (Save_Occurrence |
| 296 |
-- doesn't save the machine occurrence). |
| 297 |
|
| 298 |
function EO_To_String (X : Exception_Occurrence) return String; |
| 299 |
function String_To_EO (S : String) return Exception_Occurrence; |
| 300 |
pragma Stream_Convert (Exception_Occurrence, String_To_EO, EO_To_String); |
| 301 |
-- Functions for implementing Exception_Occurrence stream attributes |
| 302 |
|
| 303 |
Null_Occurrence : constant Exception_Occurrence := |
| 304 |
(Machine_Occurrence => System.Null_Address, |
| 305 |
Msg => (others => '*'), |
| 306 |
Tracebacks => (others => System.Traceback_Entries.Null_TB_Entry), |
| 307 |
others => <>); |
| 308 |
|
| 309 |
end Ada.Exceptions; |