Debian Codegraph @

gcc/ada/libgnat/g-cppexc.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- G N A T . C P P _ E X C E P T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2013-2025, AdaCore --
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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 with System; use System;
33 with Interfaces.C; use Interfaces.C;
34 with Ada.Unchecked_Conversion;
35 with System.Standard_Library; use System.Standard_Library;
36
37 package body GNAT.CPP_Exceptions is
38
39 -- Note: all functions prefixed by __cxa are part of the c++ ABI for
40 -- exception handling. As they are provided by the c++ library, there
41 -- must be no dependencies on it in the compiled code of this unit, but
42 -- there can be dependencies in instances. This is required to be able
43 -- to build the shared library without the c++ library.
44
45 function To_Exception_Data_Ptr is new
46 Ada.Unchecked_Conversion
47 (Exception_Id, Exception_Data_Ptr);
48 -- Convert an Exception_Id to its non-private type. This is used to get
49 -- the RTTI of a C++ exception
50
51 function Get_Exception_Machine_Occurrence
52 (X : Exception_Occurrence) return System.Address;
53 pragma Import (Ada, Get_Exception_Machine_Occurrence,
54 "__gnat_get_exception_machine_occurrence");
55 -- Imported function (from Ada.Exceptions) that returns the machine
56 -- occurrence from an exception occurrence.
57
58 -------------------------
59 -- Raise_Cpp_Exception --
60 -------------------------
61
62 procedure Raise_Cpp_Exception (Id : Exception_Id; Value : T)
63 is
64 Id_Data : constant Exception_Data_Ptr := To_Exception_Data_Ptr (Id);
65 -- Get a non-private view on the exception
66
67 type T_Acc is access all T;
68 pragma Convention (C, T_Acc);
69 -- Access type to the object compatible with C
70
71 Occ : T_Acc;
72 -- The occurrence to propagate
73
74 function cxa_allocate_exception (Size : size_t) return T_Acc;
75 pragma Import (C, cxa_allocate_exception, "__cxa_allocate_exception");
76 -- The C++ function to allocate an occurrence
77
78 procedure cxa_throw (Obj : T_Acc; Tinfo : System.Address;
79 Dest : System.Address);
80 pragma Import (C, cxa_throw, "__cxa_throw");
81 pragma No_Return (cxa_throw);
82 -- The C++ function to raise an exception
83 begin
84 -- Check the exception was imported from C++
85
86 if Id_Data.Lang not in 'B' | 'C' then
87 raise Constraint_Error;
88 end if;
89
90 -- Allocate the C++ occurrence
91
92 Occ := cxa_allocate_exception (T'Size / System.Storage_Unit);
93
94 -- Set the object
95
96 Occ.all := Value;
97
98 -- Throw the exception
99
100 cxa_throw (Occ, Id_Data.Foreign_Data, System.Null_Address);
101 end Raise_Cpp_Exception;
102
103 ------------------------
104 -- Get_Object_Address --
105 ------------------------
106
107 function Get_Object_Address
108 (X : Exception_Occurrence) return System.Address
109 is
110 Exception_Addr : constant Address :=
111 Get_Exception_Machine_Occurrence (X);
112 -- Machine occurrence of X
113
114 Object_Addr : Address;
115 -- Address of the raised object, after calling Convert
116
117 Id : constant Exception_Id := Exception_Identity (X);
118
119 Id_Data : constant Exception_Data_Ptr := To_Exception_Data_Ptr (Id);
120 -- Get a non-private view on the exception
121
122 Success : Integer;
123
124 procedure Convert (Success : out Integer;
125 Object_Addr : out Address;
126 Catch_Type : Address;
127 Convention : Character;
128 Unw_Except : Address);
129 pragma Import (C, Convert, "__gnat_obtain_caught_object");
130
131 begin
132 -- Check the machine occurrence exists
133
134 if Exception_Addr = Null_Address then
135 raise Constraint_Error;
136 end if;
137
138 if Id_Data.Lang not in 'A' .. 'C' then
139 raise Constraint_Error;
140 end if;
141
142 Convert (Success, Object_Addr,
143 Id_Data.Foreign_Data, Id_Data.Lang,
144 Exception_Addr);
145 if Success = 0 then
146 raise Constraint_Error;
147 end if;
148
149 return Object_Addr;
150 end Get_Object_Address;
151
152 ----------------
153 -- Get_Object --
154 ----------------
155
156 function Get_Object (X : Exception_Occurrence) return T
157 is
158 Object_Addr : constant System.Address := Get_Object_Address (X);
159 -- Address of the raised object
160
161 -- Import the object from the occurrence
162 Result : constant T;
163 pragma Import (Ada, Result);
164 for Result'Address use Object_Addr;
165 begin
166 return Result;
167 end Get_Object;
168
169 --------------------------
170 -- Get_Access_To_Object --
171 --------------------------
172
173 function Get_Access_To_Object (X : Exception_Occurrence)
174 return access T
175 is
176 Object_Addr : constant System.Address := Get_Object_Address (X);
177 -- Address of the raised object
178
179 type T_Acc is access T;
180
181 function To_T_Acc is
182 new Ada.Unchecked_Conversion (System.Address, T_Acc);
183
184 -- Import the object from the occurrence
185 Result : constant T_Acc := To_T_Acc (Object_Addr);
186 begin
187 return Result;
188 end Get_Access_To_Object;
189
190 ---------------------------------
191 -- Get_Access_To_Tagged_Object --
192 ---------------------------------
193
194 function Get_Access_To_Tagged_Object (X : Exception_Occurrence)
195 return access T'Class
196 is
197 Object_Addr : constant System.Address := Get_Object_Address (X);
198 -- Address of the raised object
199
200 type T_Acc is access T'Class;
201
202 function To_T_Acc is
203 new Ada.Unchecked_Conversion (System.Address, T_Acc);
204
205 -- Import the object from the occurrence
206 Result : constant T_Acc := To_T_Acc (Object_Addr);
207 begin
208 return Result;
209 end Get_Access_To_Tagged_Object;
210
211 -------------------
212 -- Get_Type_Info --
213 -------------------
214
215 function Get_Type_Info (Id : Exception_Id) return Type_Info_Ptr is
216 Id_Data : constant Exception_Data_Ptr := To_Exception_Data_Ptr (Id);
217 -- Get a non-private view on the exception
218
219 Foreign_Exception : aliased Exception_Data;
220 pragma Import
221 (Ada, Foreign_Exception, "system__exceptions__foreign_exception");
222 begin
223 if Id_Data.Lang not in 'B' | 'C' then
224 if Id_Data.Lang = 'A'
225 and then
226 Id_Data = Foreign_Exception'Unchecked_Access
227 then
228 return No_Type_Info;
229 end if;
230 raise Constraint_Error;
231 end if;
232
233 return To_Type_Info_Ptr (Id_Data.Foreign_Data);
234 end Get_Type_Info;
235
236 -------------------
237 -- Get_Type_Info --
238 -------------------
239
240 function Get_Type_Info (X : Exception_Occurrence) return Type_Info_Ptr is
241 Exception_Addr : constant Address :=
242 Get_Exception_Machine_Occurrence (X);
243 -- Machine occurrence of X
244
245 function Get_MO_RTTI (MO : Address) return Type_Info_Ptr;
246 pragma Import (Cpp, Get_MO_RTTI, "__gnat_get_cxx_exception_type_info");
247
248 TI : Type_Info_Ptr;
249 begin
250 if Exception_Addr = Null_Address then
251 raise Constraint_Error;
252 end if;
253
254 if To_Exception_Data_Ptr (Exception_Identity (X)).Lang
255 not in 'A' .. 'C'
256 then
257 raise Constraint_Error;
258 end if;
259
260 TI := Get_MO_RTTI (Exception_Addr);
261
262 if TI = No_Type_Info then
263 raise Constraint_Error;
264 end if;
265
266 return TI;
267
268 end Get_Type_Info;
269
270 end GNAT.CPP_Exceptions;