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 //===----------------------------------------------------------------------===//
16 #define __has_include(inc) 0
19 #if defined(__APPLE__) && !defined(LIBCXXRT)
22 using namespace __cxxabiv1
;
23 #define HAVE_DEPENDENT_EH_ABI 1
24 #ifndef _LIBCPPABI_VERSION
25 using namespace __cxxabiapple
;
26 // On Darwin, there are two STL shared libraries and a lower level ABI
27 // shared library. The globals holding the current terminate handler and
28 // current unexpected handler are in the ABI library.
29 #define __terminate_handler __cxxabiapple::__cxa_terminate_handler
30 #define __unexpected_handler __cxxabiapple::__cxa_unexpected_handler
31 #endif // _LIBCPPABI_VERSION
32 #elif defined(LIBCXXRT) || defined(LIBCXX_BUILDING_LIBCXXABI) || __has_include(<cxxabi.h>)
34 using namespace __cxxabiv1
;
35 #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION)
36 #define HAVE_DEPENDENT_EH_ABI 1
38 #elif !defined(__GLIBCXX__) // __has_include(<cxxabi.h>)
39 static std::terminate_handler __terminate_handler
;
40 static std::unexpected_handler __unexpected_handler
;
41 #endif // __has_include(<cxxabi.h>)
46 #if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
48 // libcxxrt provides implementations of these functions itself.
50 set_unexpected(unexpected_handler func
) _NOEXCEPT
52 return __sync_lock_test_and_set(&__unexpected_handler
, func
);
56 get_unexpected() _NOEXCEPT
58 return __sync_fetch_and_add(&__unexpected_handler
, (unexpected_handler
)0);
65 (*get_unexpected())();
66 // unexpected handler should not return
71 set_terminate(terminate_handler func
) _NOEXCEPT
73 return __sync_lock_test_and_set(&__terminate_handler
, func
);
77 get_terminate() _NOEXCEPT
79 return __sync_fetch_and_add(&__terminate_handler
, (terminate_handler
)0);
82 #ifndef __EMSCRIPTEN__ // We provide this in JS
87 #ifndef _LIBCPP_NO_EXCEPTIONS
90 #endif // _LIBCPP_NO_EXCEPTIONS
92 // handler should not return
93 fprintf(stderr
, "terminate_handler unexpectedly returned\n");
95 #ifndef _LIBCPP_NO_EXCEPTIONS
99 // handler should not throw exception
100 fprintf(stderr
, "terminate_handler unexpectedly threw an exception\n");
103 #endif // _LIBCPP_NO_EXCEPTIONS
105 #endif // !__EMSCRIPTEN__
106 #endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION)
108 #if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(__EMSCRIPTEN__)
109 bool uncaught_exception() _NOEXCEPT
{ return uncaught_exceptions() > 0; }
111 int uncaught_exceptions() _NOEXCEPT
113 #if defined(__APPLE__) || defined(_LIBCPPABI_VERSION)
114 // on Darwin, there is a helper function so __cxa_get_globals is private
115 # if _LIBCPPABI_VERSION > 1101
116 return __cxa_uncaught_exceptions();
118 return __cxa_uncaught_exception() ? 1 : 0;
121 # if defined(_MSC_VER) && ! defined(__clang__)
122 _LIBCPP_WARNING("uncaught_exceptions not yet implemented")
124 # warning uncaught_exception not yet implemented
126 fprintf(stderr
, "uncaught_exceptions not yet implemented\n");
132 #ifndef _LIBCPPABI_VERSION
134 exception::~exception() _NOEXCEPT
138 const char* exception::what() const _NOEXCEPT
140 return "std::exception";
143 #endif // _LIBCPPABI_VERSION
145 #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
147 bad_exception::~bad_exception() _NOEXCEPT
151 const char* bad_exception::what() const _NOEXCEPT
153 return "std::bad_exception";
158 #if defined(__GLIBCXX__)
160 // libsupc++ does not implement the dependent EH ABI and the functionality
161 // it uses to implement std::exception_ptr (which it declares as an alias of
162 // std::__exception_ptr::exception_ptr) is not directly exported to clients. So
163 // we have little choice but to hijack std::__exception_ptr::exception_ptr's
164 // (which fortunately has the same layout as our std::exception_ptr) copy
165 // constructor, assignment operator and destructor (which are part of its
166 // stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr)
169 namespace __exception_ptr
176 exception_ptr(const exception_ptr
&) _NOEXCEPT
;
177 exception_ptr
& operator=(const exception_ptr
&) _NOEXCEPT
;
178 ~exception_ptr() _NOEXCEPT
;
183 _LIBCPP_NORETURN
void rethrow_exception(__exception_ptr::exception_ptr
);
187 exception_ptr::~exception_ptr() _NOEXCEPT
189 #if HAVE_DEPENDENT_EH_ABI
190 __cxa_decrement_exception_refcount(__ptr_
);
191 #elif defined(__GLIBCXX__)
192 reinterpret_cast<__exception_ptr::exception_ptr
*>(this)->~exception_ptr();
194 # if defined(_MSC_VER) && ! defined(__clang__)
195 _LIBCPP_WARNING("exception_ptr not yet implemented")
197 # warning exception_ptr not yet implemented
199 fprintf(stderr
, "exception_ptr not yet implemented\n");
204 exception_ptr::exception_ptr(const exception_ptr
& other
) _NOEXCEPT
205 : __ptr_(other
.__ptr_
)
207 #if HAVE_DEPENDENT_EH_ABI
208 __cxa_increment_exception_refcount(__ptr_
);
209 #elif defined(__GLIBCXX__)
210 new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr(
211 reinterpret_cast<const __exception_ptr::exception_ptr
&>(other
));
213 # if defined(_MSC_VER) && ! defined(__clang__)
214 _LIBCPP_WARNING("exception_ptr not yet implemented")
216 # warning exception_ptr not yet implemented
218 fprintf(stderr
, "exception_ptr not yet implemented\n");
223 exception_ptr
& exception_ptr::operator=(const exception_ptr
& other
) _NOEXCEPT
225 #if HAVE_DEPENDENT_EH_ABI
226 if (__ptr_
!= other
.__ptr_
)
228 __cxa_increment_exception_refcount(other
.__ptr_
);
229 __cxa_decrement_exception_refcount(__ptr_
);
230 __ptr_
= other
.__ptr_
;
233 #elif defined(__GLIBCXX__)
234 *reinterpret_cast<__exception_ptr::exception_ptr
*>(this) =
235 reinterpret_cast<const __exception_ptr::exception_ptr
&>(other
);
238 # if defined(_MSC_VER) && ! defined(__clang__)
239 _LIBCPP_WARNING("exception_ptr not yet implemented")
241 # warning exception_ptr not yet implemented
243 fprintf(stderr
, "exception_ptr not yet implemented\n");
248 nested_exception::nested_exception() _NOEXCEPT
249 : __ptr_(current_exception())
253 #if !defined(__GLIBCXX__)
255 nested_exception::~nested_exception() _NOEXCEPT
263 nested_exception::rethrow_nested() const
265 if (__ptr_
== nullptr)
267 rethrow_exception(__ptr_
);
270 #if !defined(__GLIBCXX__)
272 exception_ptr
current_exception() _NOEXCEPT
274 #if HAVE_DEPENDENT_EH_ABI
275 // be nicer if there was a constructor that took a ptr, then
276 // this whole function would be just:
277 // return exception_ptr(__cxa_current_primary_exception());
279 ptr
.__ptr_
= __cxa_current_primary_exception();
282 # if defined(_MSC_VER) && ! defined(__clang__)
283 _LIBCPP_WARNING( "exception_ptr not yet implemented" )
285 # warning exception_ptr not yet implemented
287 fprintf(stderr
, "exception_ptr not yet implemented\n");
292 #endif // !__GLIBCXX__
295 void rethrow_exception(exception_ptr p
)
297 #if HAVE_DEPENDENT_EH_ABI
298 __cxa_rethrow_primary_exception(p
.__ptr_
);
299 // if p.__ptr_ is NULL, above returns so we terminate
301 #elif defined(__GLIBCXX__)
302 rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr
&>(p
));
304 # if defined(_MSC_VER) && ! defined(__clang__)
305 _LIBCPP_WARNING("exception_ptr not yet implemented")
307 # warning exception_ptr not yet implemented
309 fprintf(stderr
, "exception_ptr not yet implemented\n");