Import gcc-4.4.1
[dragonfly.git] / contrib / gcc-4.4 / libstdc++-v3 / libsupc++ / eh_call.cc
1 // -*- C++ -*- Helpers for calling unextected and terminate
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 // Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 //
12 // GCC is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU General Public License for more details.
16 //
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 #include <bits/c++config.h>
27 #include <cstdlib>
28 #include <exception_defines.h>
29 #include "unwind-cxx.h"
30
31 using namespace __cxxabiv1;
32
33 #include "unwind-pe.h"
34
35
36 // Helper routine for when the exception handling code needs to call
37 // terminate.
38
39 extern "C" void
40 __cxa_call_terminate(_Unwind_Exception* ue_header)
41 {
42
43   if (ue_header)
44     {
45       // terminate is classed as a catch handler.
46       __cxa_begin_catch(ue_header);
47
48       // Call the terminate handler that was in effect when we threw this
49       // exception.  */
50       if (__is_gxx_exception_class(ue_header->exception_class))
51         {
52           __cxa_exception* xh;
53
54           xh = __get_exception_header_from_ue(ue_header);
55           __terminate(xh->terminateHandler);
56         }
57     }
58   /* Call the global routine if we don't have anything better.  */
59   std::terminate();
60 }
61
62
63 #ifdef __ARM_EABI_UNWINDER__
64 // The ARM EABI __cxa_call_unexpected has the same semantics as the generic
65 // routine, but the exception specification has a different format.
66 extern "C" void
67 __cxa_call_unexpected(void* exc_obj_in)
68 {
69   _Unwind_Exception* exc_obj
70     = reinterpret_cast<_Unwind_Exception*>(exc_obj_in);
71
72   int rtti_count = 0;
73   _Unwind_Word rtti_stride = 0;
74   _Unwind_Word* rtti_list = NULL;
75   bool foreign_exception;
76   std::unexpected_handler unexpectedHandler = NULL;
77   std::terminate_handler terminateHandler = NULL;
78   __cxa_exception* xh;
79   if (__is_gxx_exception_class(exc_obj->exception_class))
80     {
81       // Save data from the EO, which may be clobbered by _cxa_begin_catch.
82       xh = __get_exception_header_from_ue(exc_obj);
83       unexpectedHandler = xh->unexpectedHandler;
84       terminateHandler = xh->terminateHandler;
85       rtti_count = exc_obj->barrier_cache.bitpattern[1];
86
87       rtti_stride = exc_obj->barrier_cache.bitpattern[3];
88       rtti_list = (_Unwind_Word*) exc_obj->barrier_cache.bitpattern[4];
89       foreign_exception = false;
90     }
91   else
92     foreign_exception = true;
93
94   /* This must be called after extracting data from the EO, but before
95      calling unexpected().   */
96   __cxa_begin_catch(exc_obj);
97
98   // This function is a handler for our exception argument.  If we exit
99   // by throwing a different exception, we'll need the original cleaned up.
100   struct end_catch_protect
101   {
102     end_catch_protect() { }
103     ~end_catch_protect() { __cxa_end_catch(); }
104   } end_catch_protect_obj;
105
106
107   __try 
108     { 
109       if (foreign_exception)
110         std::unexpected();
111       else
112         __unexpected(unexpectedHandler);
113     }
114   __catch(...) 
115     {
116       /* See if the new exception matches the rtti list.  */
117       if (foreign_exception)
118         std::terminate();
119
120       // Get the exception thrown from unexpected.
121
122       __cxa_eh_globals* globals = __cxa_get_globals_fast();
123       __cxa_exception* new_xh = globals->caughtExceptions;
124       void* new_ptr = __get_object_from_ambiguous_exception (new_xh);
125       const std::type_info* catch_type;
126       int n;
127       bool bad_exception_allowed = false;
128       const std::type_info& bad_exc = typeid(std::bad_exception);
129
130       // Check the new exception against the rtti list
131       for (n = 0; n < rtti_count; n++)
132         {
133           _Unwind_Word offset;
134
135           offset = (_Unwind_Word) &rtti_list[n * (rtti_stride >> 2)];
136           offset = _Unwind_decode_target2(offset);
137           catch_type = (const std::type_info*) (offset);
138
139           if (__cxa_type_match(&new_xh->unwindHeader, catch_type, false,
140                                &new_ptr) != ctm_failed)
141             __throw_exception_again;
142
143           if (catch_type->__do_catch(&bad_exc, 0, 1))
144             bad_exception_allowed = true;
145         }
146
147       // If the exception spec allows std::bad_exception, throw that.
148 #ifdef __EXCEPTIONS  
149       if (bad_exception_allowed)
150         throw std::bad_exception();
151 #endif   
152
153       // Otherwise, die.
154       __terminate(terminateHandler);
155     }
156 }
157 #endif // __ARM_EABI_UNWINDER__