1 // Functions for Exception Support for -*- C++ -*-
2 // Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999 Free Software Foundation
4 // This file is part of GNU CC.
6 // GNU CC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
11 // GNU CC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with GNU CC; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
21 // As a special exception, if you link this library with other files,
22 // some of which are compiled with GCC, to produce an executable,
23 // this library does not by itself cause the resulting executable
24 // to be covered by the GNU General Public License.
25 // This exception does not however invalidate any other reasons why
26 // the executable file might be covered by the GNU General Public License.
28 #pragma implementation "exception"
33 #include "gansidecl.h" /* Needed to support macros used in eh-common.h. */
34 #include "eh-common.h"
36 /* Define terminate, unexpected, set_terminate, set_unexpected as
37 well as the default terminate func and default unexpected func. */
39 extern std::terminate_handler __terminate_func
__attribute__((__noreturn__
));
49 __default_unexpected ()
54 static std::unexpected_handler __unexpected_func
__attribute__((__noreturn__
))
55 = __default_unexpected
;
57 std::terminate_handler
58 std::set_terminate (std::terminate_handler func
)
60 std::terminate_handler old
= __terminate_func
;
62 __terminate_func
= func
;
66 std::unexpected_handler
67 std::set_unexpected (std::unexpected_handler func
)
69 std::unexpected_handler old
= __unexpected_func
;
71 __unexpected_func
= func
;
81 /* C++-specific state about the current exception.
82 This must match init_exception_processing().
84 Note that handlers and caught are not redundant; when rethrown, an
85 exception can have multiple active handlers and still be considered
93 void (*cleanup
)(void *, int);
100 /* Language-specific EH info pointer, defined in libgcc2. */
102 extern "C" cp_eh_info
**__get_eh_info (); // actually void **
104 /* Is P the type_info node for a pointer of some kind? */
106 extern bool __is_pointer (void *);
109 /* OLD Compiler hook to return a pointer to the info for the current exception.
110 Used by get_eh_info (). This fudges the actualy returned value to
111 point to the beginning of what USE to be the cp_eh_info structure.
112 THis is so that old code that dereferences this pointer will find
113 things where it expects it to be.*/
115 __cp_exception_info (void)
117 return &((*__get_eh_info ())->value
);
120 #define CP_EH_INFO ((cp_eh_info *) *__get_eh_info ())
122 /* Old Compiler hook to return a pointer to the info for the current exception.
123 Used by get_eh_info (). */
125 extern "C" cp_eh_info
*
128 cp_eh_info
*p
= CP_EH_INFO
;
132 /* Compiler hook to return a pointer to the info for the current exception,
133 Set the caught bit, and increment the number of handlers that are
134 looking at this exception. This makes handlers smaller. */
136 extern "C" cp_eh_info
*
137 __start_cp_handler (void)
139 cp_eh_info
*p
= CP_EH_INFO
;
145 /* Allocate a buffer for a cp_eh_info and an exception object of size SIZE,
146 and return a pointer to the beginning of the object's space. */
148 extern "C" void * malloc (size_t);
150 __eh_alloc (size_t size
)
152 void *p
= malloc (size
);
158 /* Free the memory for an cp_eh_info and associated exception, given
159 a pointer to the cp_eh_info. */
161 extern "C" void free (void *);
168 extern "C" int __throw_type_match_rtti_2 (const void *, const void *,
172 __cplus_type_matcher (__eh_info
*info_
, void *match_info
,
173 exception_descriptor
*exception_table
)
175 cp_eh_info
*info
= (cp_eh_info
*)info_
;
177 /* No exception table implies the old style mechanism, so don't check. */
178 if (exception_table
!= NULL
179 && exception_table
->lang
.language
!= EH_LANG_C_plus_plus
)
182 if (match_info
== CATCH_ALL_TYPE
)
185 /* we don't worry about version info yet, there is only one version! */
187 void *match_type
= match_info
;
189 #if !defined (__GXX_ABI_VERSION) || __GXX_ABI_VERSION < 100
190 match_type
= ((void *(*)())match_type
) ();
193 if (__throw_type_match_rtti_2 (match_type
, info
->type
,
194 info
->original_value
, &info
->value
))
195 // Arbitrary non-null pointer.
201 /* Compiler hook to push a new exception onto the stack.
202 Used by expand_throw(). */
205 __cp_push_exception (void *value
, void *type
, void (*cleanup
)(void *, int))
207 cp_eh_info
*p
= (cp_eh_info
*) __eh_alloc (sizeof (cp_eh_info
));
211 p
->cleanup
= cleanup
;
214 p
->original_value
= value
;
216 p
->eh_info
.match_function
= __cplus_type_matcher
;
217 p
->eh_info
.language
= EH_LANG_C_plus_plus
;
218 p
->eh_info
.version
= 1;
220 cp_eh_info
**q
= __get_eh_info ();
226 /* Compiler hook to pop an exception that has been finalized. Used by
227 push_eh_cleanup(). P is the info for the exception caught by the
228 current catch block. */
231 __cp_pop_exception (cp_eh_info
*p
)
233 cp_eh_info
**q
= __get_eh_info ();
237 /* Don't really pop if there are still active handlers for our exception,
238 or if our exception is being rethrown (i.e. if the active exception is
239 our exception and it is uncaught). */
241 || (p
== *q
&& !p
->caught
))
244 for (; *q
; q
= &((*q
)->next
))
254 /* 2 is a magic value for destructors; see build_delete(). */
255 p
->cleanup (p
->value
, 2);
257 if (! __is_pointer (p
->type
))
258 __eh_free (p
->original_value
); // value may have been co-erced.
264 __uncatch_exception (void)
266 cp_eh_info
*p
= CP_EH_INFO
;
272 /* As per [except.unexpected]:
273 If an exception is thrown, we check it against the spec. If it doesn't
274 match, we call unexpected (). If unexpected () throws, we check that
275 exception against the spec. If it doesn't match, if the spec allows
276 bad_exception we throw that; otherwise we call terminate ().
278 The compiler treats an exception spec as a try block with a generic
279 handler that just calls this function with a list of the allowed
280 exception types, so we have an active exception that can be rethrown.
282 This function does not return. */
285 __check_eh_spec (int n
, const void **spec
)
287 cp_eh_info
*p
= CP_EH_INFO
;
290 for (int i
= 0; i
< n
; ++i
)
292 if (__throw_type_match_rtti_2 (spec
[i
], p
->type
, p
->value
, &d
))
302 // __exception_info is an artificial var pushed into each catch block.
303 if (p
!= __exception_info
)
305 p
= __exception_info
;
306 for (int i
= 0; i
< n
; ++i
)
308 if (__throw_type_match_rtti_2 (spec
[i
], p
->type
, p
->value
, &d
))
313 const std::type_info
&bad_exc
= typeid (std::bad_exception
);
314 for (int i
= 0; i
< n
; ++i
)
316 if (__throw_type_match_rtti_2 (spec
[i
], &bad_exc
, p
->value
, &d
))
317 throw std::bad_exception ();
324 // Helpers for rtti. Although these don't return, we give them return types so
325 // that the type system is not broken.
330 throw std::bad_cast ();
334 extern "C" std::type_info
const &
335 __throw_bad_typeid ()
337 throw std::bad_typeid ();
338 return typeid (void);
341 /* Has the current exception been caught? */
344 std::uncaught_exception ()
346 cp_eh_info
*p
= CP_EH_INFO
;
347 return p
&& ! p
->caught
;
350 const char * std::exception::
353 return typeid (*this).name ();