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