1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
5 -- S Y S T E M . E X C E P T I O N _ T A B L E --
9 -- Copyright (C) 1996-2017, Free Software Foundation, Inc. --
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. --
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. --
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/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 pragma Compiler_Unit_Warning
;
34 with System
.Soft_Links
; use System
.Soft_Links
;
36 package body System
.Exception_Table
is
38 use System
.Standard_Library
;
40 type Hash_Val
is mod 2 ** 8;
41 subtype Hash_Idx
is Hash_Val
range 1 .. 37;
43 HTable
: array (Hash_Idx
) of aliased Exception_Data_Ptr
;
44 -- Actual hash table containing all registered exceptions
46 -- The table is very small and the hash function weak, as looking up
47 -- registered exceptions is rare and minimizing space and time overhead
48 -- of registration is more important. In addition, it is expected that the
49 -- exceptions that need to be looked up are registered dynamically, and
50 -- therefore will be at the begin of the hash chains.
52 -- The table differs from System.HTable.Static_HTable in that the final
53 -- element of each chain is not marked by null, but by a pointer to self.
54 -- This way it is possible to defend against the same entry being inserted
55 -- twice, without having to do a lookup which is relatively expensive for
56 -- programs with large number
58 -- All non-local subprograms use the global Task_Lock to protect against
59 -- concurrent use of the exception table. This is needed as local
60 -- exceptions may be declared concurrently with those declared at the
66 with procedure Process
(T
: Exception_Data_Ptr
; More
: out Boolean);
70 function Lookup
(Name
: String) return Exception_Data_Ptr
;
71 -- Find and return the Exception_Data of the exception with the given Name
72 -- (which must be in all uppercase), or null if none was registered.
74 procedure Register
(Item
: Exception_Data_Ptr
);
75 -- Register an exception with the given Exception_Data in the table.
77 function Has_Name
(Item
: Exception_Data_Ptr
; Name
: String) return Boolean;
78 -- Return True iff Item.Full_Name and Name are equal. Both names are
79 -- assumed to be in all uppercase and end with ASCII.NUL.
81 function Hash
(S
: String) return Hash_Idx
;
82 -- Return the index in the hash table for S, which is assumed to be all
83 -- uppercase and end with ASCII.NUL.
89 function Has_Name
(Item
: Exception_Data_Ptr
; Name
: String) return Boolean
91 S
: constant Big_String_Ptr
:= To_Ptr
(Item
.Full_Name
);
92 J
: Integer := S
'First;
95 for K
in Name
'Range loop
97 -- Note that as both items are terminated with ASCII.NUL, the
98 -- comparison below must fail for strings of different lengths.
100 if S
(J
) /= Name
(K
) then
114 function Lookup
(Name
: String) return Exception_Data_Ptr
is
115 Prev
: Exception_Data_Ptr
;
116 Curr
: Exception_Data_Ptr
;
119 Curr
:= HTable
(Hash
(Name
));
121 while Curr
/= Prev
loop
122 if Has_Name
(Curr
, Name
) then
127 Curr
:= Curr
.HTable_Ptr
;
137 function Hash
(S
: String) return Hash_Idx
is
138 Hash
: Hash_Val
:= 0;
141 for J
in S
'Range loop
142 exit when S
(J
) = ASCII
.NUL
;
143 Hash
:= Hash
xor Character'Pos (S
(J
));
146 return Hash_Idx
'First + Hash
mod (Hash_Idx
'Last - Hash_Idx
'First + 1);
155 Prev
, Curr
: Exception_Data_Ptr
;
158 Outer
: for Idx
in HTable
'Range loop
160 Curr
:= HTable
(Idx
);
162 while Curr
/= Prev
loop
163 Process
(Curr
, More
);
165 exit Outer
when not More
;
168 Curr
:= Curr
.HTable_Ptr
;
177 procedure Register
(Item
: Exception_Data_Ptr
) is
179 if Item
.HTable_Ptr
= null then
180 Prepend_To_Chain
: declare
181 Chain
: Exception_Data_Ptr
182 renames HTable
(Hash
(To_Ptr
(Item
.Full_Name
).all));
186 Item
.HTable_Ptr
:= Item
;
188 Item
.HTable_Ptr
:= Chain
;
192 end Prepend_To_Chain
;
196 -------------------------------
197 -- Get_Registered_Exceptions --
198 -------------------------------
200 procedure Get_Registered_Exceptions
201 (List
: out Exception_Data_Array
;
204 procedure Get_One
(Item
: Exception_Data_Ptr
; More
: out Boolean);
205 -- Add Item to List (List'First .. Last) by first incrementing Last
206 -- and storing Item in List (Last). Last should be in List'First - 1
209 procedure Get_All
is new Iterate
(Get_One
);
210 -- Store all registered exceptions in List, updating Last
216 procedure Get_One
(Item
: Exception_Data_Ptr
; More
: out Boolean) is
218 if Last
< List
'Last then
229 -- In this routine the invariant is that List (List'First .. Last)
230 -- contains the registered exceptions retrieved so far.
232 Last
:= List
'First - 1;
237 end Get_Registered_Exceptions
;
239 ------------------------
240 -- Internal_Exception --
241 ------------------------
243 function Internal_Exception
245 Create_If_Not_Exist
: Boolean := True) return Exception_Data_Ptr
247 -- If X was not yet registered and Create_if_Not_Exist is True,
248 -- dynamically allocate and register a new exception.
250 type String_Ptr
is access all String;
252 Dyn_Copy
: String_Ptr
;
253 Copy
: aliased String (X
'First .. X
'Last + 1);
254 Result
: Exception_Data_Ptr
;
260 Copy
(Copy
'Last) := ASCII
.NUL
;
261 Result
:= Lookup
(Copy
);
263 -- If unknown exception, create it on the heap. This is a legitimate
264 -- situation in the distributed case when an exception is defined
265 -- only in a partition
267 if Result
= null and then Create_If_Not_Exist
then
268 Dyn_Copy
:= new String'(Copy);
272 (Not_Handled_By_Others
=> False,
274 Name_Length
=> Copy
'Length,
275 Full_Name
=> Dyn_Copy
.all'Address,
277 Foreign_Data
=> Null_Address
,
286 end Internal_Exception
;
288 ------------------------
289 -- Register_Exception --
290 ------------------------
292 procedure Register_Exception
(X
: Exception_Data_Ptr
) is
297 end Register_Exception
;
299 ---------------------------------
300 -- Registered_Exceptions_Count --
301 ---------------------------------
303 function Registered_Exceptions_Count
return Natural is
304 Count
: Natural := 0;
306 procedure Count_Item
(Item
: Exception_Data_Ptr
; More
: out Boolean);
307 -- Update Count for given Item
309 procedure Count_Item
(Item
: Exception_Data_Ptr
; More
: out Boolean) is
310 pragma Unreferenced
(Item
);
313 More
:= Count
< Natural'Last;
316 procedure Count_All
is new Iterate
(Count_Item
);
324 end Registered_Exceptions_Count
;
327 -- Register the standard exceptions at elaboration time
329 -- We don't need to use the locking version here as the elaboration
330 -- will not be concurrent and no tasks can call any subprograms of this
331 -- unit before it has been elaborated.
333 Register
(Abort_Signal_Def
'Access);
334 Register
(Tasking_Error_Def
'Access);
335 Register
(Storage_Error_Def
'Access);
336 Register
(Program_Error_Def
'Access);
337 Register
(Numeric_Error_Def
'Access);
338 Register
(Constraint_Error_Def
'Access);
339 end System
.Exception_Table
;