xref: /openbsd-src/gnu/gcc/libstdc++-v3/libsupc++/eh_catch.cc (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1*404b540aSrobert // -*- C++ -*- Exception handling routines for catching.
2*404b540aSrobert // Copyright (C) 2001, 2003, 2004 Free Software Foundation, Inc.
3*404b540aSrobert //
4*404b540aSrobert // This file is part of GCC.
5*404b540aSrobert //
6*404b540aSrobert // GCC is free software; you can redistribute it and/or modify
7*404b540aSrobert // it under the terms of the GNU General Public License as published by
8*404b540aSrobert // the Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert //
11*404b540aSrobert // GCC is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert //
16*404b540aSrobert // You should have received a copy of the GNU General Public License
17*404b540aSrobert // along with GCC; see the file COPYING.  If not, write to
18*404b540aSrobert // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19*404b540aSrobert // Boston, MA 02110-1301, USA.
20*404b540aSrobert 
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction.  Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License.  This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert 
30*404b540aSrobert #include <cstdlib>
31*404b540aSrobert #include "unwind-cxx.h"
32*404b540aSrobert 
33*404b540aSrobert using namespace __cxxabiv1;
34*404b540aSrobert 
35*404b540aSrobert extern "C" void *
__cxa_get_exception_ptr(void * exc_obj_in)36*404b540aSrobert __cxxabiv1::__cxa_get_exception_ptr(void *exc_obj_in) throw()
37*404b540aSrobert {
38*404b540aSrobert   _Unwind_Exception *exceptionObject
39*404b540aSrobert     = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
40*404b540aSrobert 
41*404b540aSrobert   return __gxx_caught_object(exceptionObject);
42*404b540aSrobert }
43*404b540aSrobert 
44*404b540aSrobert extern "C" void *
__cxa_begin_catch(void * exc_obj_in)45*404b540aSrobert __cxxabiv1::__cxa_begin_catch (void *exc_obj_in) throw()
46*404b540aSrobert {
47*404b540aSrobert   _Unwind_Exception *exceptionObject
48*404b540aSrobert     = reinterpret_cast <_Unwind_Exception *>(exc_obj_in);
49*404b540aSrobert   __cxa_eh_globals *globals = __cxa_get_globals ();
50*404b540aSrobert   __cxa_exception *prev = globals->caughtExceptions;
51*404b540aSrobert   __cxa_exception *header = __get_exception_header_from_ue (exceptionObject);
52*404b540aSrobert   void* objectp;
53*404b540aSrobert 
54*404b540aSrobert   // Foreign exceptions can't be stacked here.  If the exception stack is
55*404b540aSrobert   // empty, then fine.  Otherwise we really have no choice but to terminate.
56*404b540aSrobert   // Note that this use of "header" is a lie.  It's fine so long as we only
57*404b540aSrobert   // examine header->unwindHeader though.
58*404b540aSrobert   if (!__is_gxx_exception_class(header->unwindHeader.exception_class))
59*404b540aSrobert     {
60*404b540aSrobert       if (prev != 0)
61*404b540aSrobert 	std::terminate ();
62*404b540aSrobert 
63*404b540aSrobert       // Remember for end_catch and rethrow.
64*404b540aSrobert       globals->caughtExceptions = header;
65*404b540aSrobert 
66*404b540aSrobert       // ??? No sensible value to return; we don't know what the
67*404b540aSrobert       // object is, much less where it is in relation to the header.
68*404b540aSrobert       return 0;
69*404b540aSrobert     }
70*404b540aSrobert 
71*404b540aSrobert   int count = header->handlerCount;
72*404b540aSrobert   // Count is less than zero if this exception was rethrown from an
73*404b540aSrobert   // immediately enclosing region.
74*404b540aSrobert   if (count < 0)
75*404b540aSrobert     count = -count + 1;
76*404b540aSrobert   else
77*404b540aSrobert     count += 1;
78*404b540aSrobert   header->handlerCount = count;
79*404b540aSrobert   globals->uncaughtExceptions -= 1;
80*404b540aSrobert 
81*404b540aSrobert   if (header != prev)
82*404b540aSrobert     {
83*404b540aSrobert       header->nextException = prev;
84*404b540aSrobert       globals->caughtExceptions = header;
85*404b540aSrobert     }
86*404b540aSrobert 
87*404b540aSrobert   objectp = __gxx_caught_object(exceptionObject);
88*404b540aSrobert #ifdef __ARM_EABI_UNWINDER__
89*404b540aSrobert   _Unwind_Complete(exceptionObject);
90*404b540aSrobert #endif
91*404b540aSrobert   return objectp;
92*404b540aSrobert }
93*404b540aSrobert 
94*404b540aSrobert 
95*404b540aSrobert extern "C" void
__cxa_end_catch()96*404b540aSrobert __cxxabiv1::__cxa_end_catch ()
97*404b540aSrobert {
98*404b540aSrobert   __cxa_eh_globals *globals = __cxa_get_globals_fast ();
99*404b540aSrobert   __cxa_exception *header = globals->caughtExceptions;
100*404b540aSrobert 
101*404b540aSrobert   // A rethrow of a foreign exception will be removed from the
102*404b540aSrobert   // the exception stack immediately by __cxa_rethrow.
103*404b540aSrobert   if (!header)
104*404b540aSrobert     return;
105*404b540aSrobert 
106*404b540aSrobert   // A foreign exception couldn't have been stacked (see above),
107*404b540aSrobert   // so by definition processing must be complete.
108*404b540aSrobert   if (!__is_gxx_exception_class(header->unwindHeader.exception_class))
109*404b540aSrobert     {
110*404b540aSrobert       globals->caughtExceptions = 0;
111*404b540aSrobert       _Unwind_DeleteException (&header->unwindHeader);
112*404b540aSrobert       return;
113*404b540aSrobert     }
114*404b540aSrobert 
115*404b540aSrobert   int count = header->handlerCount;
116*404b540aSrobert   if (count < 0)
117*404b540aSrobert     {
118*404b540aSrobert       // This exception was rethrown.  Decrement the (inverted) catch
119*404b540aSrobert       // count and remove it from the chain when it reaches zero.
120*404b540aSrobert       if (++count == 0)
121*404b540aSrobert 	globals->caughtExceptions = header->nextException;
122*404b540aSrobert     }
123*404b540aSrobert   else if (--count == 0)
124*404b540aSrobert     {
125*404b540aSrobert       // Handling for this exception is complete.  Destroy the object.
126*404b540aSrobert       globals->caughtExceptions = header->nextException;
127*404b540aSrobert       _Unwind_DeleteException (&header->unwindHeader);
128*404b540aSrobert       return;
129*404b540aSrobert     }
130*404b540aSrobert   else if (count < 0)
131*404b540aSrobert     // A bug in the exception handling library or compiler.
132*404b540aSrobert     std::terminate ();
133*404b540aSrobert 
134*404b540aSrobert   header->handlerCount = count;
135*404b540aSrobert }
136*404b540aSrobert 
137*404b540aSrobert 
138*404b540aSrobert bool
uncaught_exception()139*404b540aSrobert std::uncaught_exception() throw()
140*404b540aSrobert {
141*404b540aSrobert   __cxa_eh_globals *globals = __cxa_get_globals ();
142*404b540aSrobert   return globals->uncaughtExceptions != 0;
143*404b540aSrobert }
144