1 //===------------------------ exception.cpp -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
15 #if defined(__APPLE__) && !defined(LIBCXXRT)
18 using namespace __cxxabiv1
;
19 #define HAVE_DEPENDENT_EH_ABI 1
20 #ifndef _LIBCPPABI_VERSION
21 using namespace __cxxabiapple
;
22 // On Darwin, there are two STL shared libraries and a lower level ABI
23 // shared library. The globals holding the current terminate handler and
24 // current unexpected handler are in the ABI library.
25 #define __terminate_handler __cxxabiapple::__cxa_terminate_handler
26 #define __unexpected_handler __cxxabiapple::__cxa_unexpected_handler
27 #endif // _LIBCPPABI_VERSION
28 #elif defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI)
30 using namespace __cxxabiv1
;
31 #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION)
32 #define HAVE_DEPENDENT_EH_ABI 1
34 #elif !defined(__GLIBCXX__) // defined(LIBCXX_BUILDING_LIBCXXABI)
35 static std::terminate_handler __terminate_handler
;
36 static std::unexpected_handler __unexpected_handler
;
37 #endif // defined(LIBCXX_BUILDING_LIBCXXABI)
42 #if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
44 // libcxxrt provides implementations of these functions itself.
46 set_unexpected(unexpected_handler func
) _NOEXCEPT
48 return __sync_lock_test_and_set(&__unexpected_handler
, func
);
52 get_unexpected() _NOEXCEPT
54 return __sync_fetch_and_add(&__unexpected_handler
, (unexpected_handler
)0);
61 (*get_unexpected())();
62 // unexpected handler should not return
67 set_terminate(terminate_handler func
) _NOEXCEPT
69 return __sync_lock_test_and_set(&__terminate_handler
, func
);
73 get_terminate() _NOEXCEPT
75 return __sync_fetch_and_add(&__terminate_handler
, (terminate_handler
)0);
78 #ifndef __EMSCRIPTEN__ // We provide this in JS
83 #ifndef _LIBCPP_NO_EXCEPTIONS
86 #endif // _LIBCPP_NO_EXCEPTIONS
88 // handler should not return
89 fprintf(stderr
, "terminate_handler unexpectedly returned\n");
91 #ifndef _LIBCPP_NO_EXCEPTIONS
95 // handler should not throw exception
96 fprintf(stderr
, "terminate_handler unexpectedly threw an exception\n");
99 #endif // _LIBCPP_NO_EXCEPTIONS
101 #endif // !__EMSCRIPTEN__
102 #endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION)
104 #if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(__EMSCRIPTEN__)
105 bool uncaught_exception() _NOEXCEPT
{ return uncaught_exceptions() > 0; }
107 int uncaught_exceptions() _NOEXCEPT
109 #if defined(__APPLE__) || defined(_LIBCPPABI_VERSION)
110 // on Darwin, there is a helper function so __cxa_get_globals is private
111 # if _LIBCPPABI_VERSION > 1101
112 return __cxa_uncaught_exceptions();
114 return __cxa_uncaught_exception() ? 1 : 0;
117 # if defined(_MSC_VER) && ! defined(__clang__)
118 _LIBCPP_WARNING("uncaught_exceptions not yet implemented")
120 # warning uncaught_exception not yet implemented
122 fprintf(stderr
, "uncaught_exceptions not yet implemented\n");
128 #ifndef _LIBCPPABI_VERSION
130 exception::~exception() _NOEXCEPT
134 const char* exception::what() const _NOEXCEPT
136 return "std::exception";
139 #endif // _LIBCPPABI_VERSION
141 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
143 bad_exception::~bad_exception() _NOEXCEPT
147 const char* bad_exception::what() const _NOEXCEPT
149 return "std::bad_exception";
154 #if defined(__GLIBCXX__)
156 // libsupc++ does not implement the dependent EH ABI and the functionality
157 // it uses to implement std::exception_ptr (which it declares as an alias of
158 // std::__exception_ptr::exception_ptr) is not directly exported to clients. So
159 // we have little choice but to hijack std::__exception_ptr::exception_ptr's
160 // (which fortunately has the same layout as our std::exception_ptr) copy
161 // constructor, assignment operator and destructor (which are part of its
162 // stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr)
165 namespace __exception_ptr
172 exception_ptr(const exception_ptr
&) _NOEXCEPT
;
173 exception_ptr
& operator=(const exception_ptr
&) _NOEXCEPT
;
174 ~exception_ptr() _NOEXCEPT
;
179 _LIBCPP_NORETURN
void rethrow_exception(__exception_ptr::exception_ptr
);
183 exception_ptr::~exception_ptr() _NOEXCEPT
185 #if HAVE_DEPENDENT_EH_ABI
186 __cxa_decrement_exception_refcount(__ptr_
);
187 #elif defined(__GLIBCXX__)
188 reinterpret_cast<__exception_ptr::exception_ptr
*>(this)->~exception_ptr();
190 # if defined(_MSC_VER) && ! defined(__clang__)
191 _LIBCPP_WARNING("exception_ptr not yet implemented")
193 # warning exception_ptr not yet implemented
195 fprintf(stderr
, "exception_ptr not yet implemented\n");
200 exception_ptr::exception_ptr(const exception_ptr
& other
) _NOEXCEPT
201 : __ptr_(other
.__ptr_
)
203 #if HAVE_DEPENDENT_EH_ABI
204 __cxa_increment_exception_refcount(__ptr_
);
205 #elif defined(__GLIBCXX__)
206 new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr(
207 reinterpret_cast<const __exception_ptr::exception_ptr
&>(other
));
209 # if defined(_MSC_VER) && ! defined(__clang__)
210 _LIBCPP_WARNING("exception_ptr not yet implemented")
212 # warning exception_ptr not yet implemented
214 fprintf(stderr
, "exception_ptr not yet implemented\n");
219 exception_ptr
& exception_ptr::operator=(const exception_ptr
& other
) _NOEXCEPT
221 #if HAVE_DEPENDENT_EH_ABI
222 if (__ptr_
!= other
.__ptr_
)
224 __cxa_increment_exception_refcount(other
.__ptr_
);
225 __cxa_decrement_exception_refcount(__ptr_
);
226 __ptr_
= other
.__ptr_
;
229 #elif defined(__GLIBCXX__)
230 *reinterpret_cast<__exception_ptr::exception_ptr
*>(this) =
231 reinterpret_cast<const __exception_ptr::exception_ptr
&>(other
);
234 # if defined(_MSC_VER) && ! defined(__clang__)
235 _LIBCPP_WARNING("exception_ptr not yet implemented")
237 # warning exception_ptr not yet implemented
239 fprintf(stderr
, "exception_ptr not yet implemented\n");
244 nested_exception::nested_exception() _NOEXCEPT
245 : __ptr_(current_exception())
249 #if !defined(__GLIBCXX__)
251 nested_exception::~nested_exception() _NOEXCEPT
259 nested_exception::rethrow_nested() const
261 if (__ptr_
== nullptr)
263 rethrow_exception(__ptr_
);
266 #if !defined(__GLIBCXX__)
268 exception_ptr
current_exception() _NOEXCEPT
270 #if HAVE_DEPENDENT_EH_ABI
271 // be nicer if there was a constructor that took a ptr, then
272 // this whole function would be just:
273 // return exception_ptr(__cxa_current_primary_exception());
275 ptr
.__ptr_
= __cxa_current_primary_exception();
278 # if defined(_MSC_VER) && ! defined(__clang__)
279 _LIBCPP_WARNING( "exception_ptr not yet implemented" )
281 # warning exception_ptr not yet implemented
283 fprintf(stderr
, "exception_ptr not yet implemented\n");
288 #endif // !__GLIBCXX__
291 void rethrow_exception(exception_ptr p
)
293 #if HAVE_DEPENDENT_EH_ABI
294 __cxa_rethrow_primary_exception(p
.__ptr_
);
295 // if p.__ptr_ is NULL, above returns so we terminate
297 #elif defined(__GLIBCXX__)
298 rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr
&>(p
));
300 # if defined(_MSC_VER) && ! defined(__clang__)
301 _LIBCPP_WARNING("exception_ptr not yet implemented")
303 # warning exception_ptr not yet implemented
305 fprintf(stderr
, "exception_ptr not yet implemented\n");