blob: 3ce6f2e11188d056d39c7db254fca51b2ebb6f0a [file] [log] [blame]
Nick Kledzik804b6e72010-05-14 20:19:37 +00001//===------------------------ exception.cpp -------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Nick Kledzik804b6e72010-05-14 20:19:37 +00007//
8//===----------------------------------------------------------------------===//
9#include <stdlib.h>
Howard Hinnanted14a762013-07-23 16:05:56 +000010#include <stdio.h>
Nick Kledzik804b6e72010-05-14 20:19:37 +000011
12#include "exception"
Peter Collingbourneece95912013-10-06 22:13:21 +000013#include "new"
Nick Kledzik804b6e72010-05-14 20:19:37 +000014
Richard Smith591e32d2012-07-11 09:35:47 +000015#ifndef __has_include
16#define __has_include(inc) 0
17#endif
18
Marshall Clowdece7fe2013-03-18 17:45:34 +000019#ifdef __APPLE__
Nick Kledzik804b6e72010-05-14 20:19:37 +000020 #include <cxxabi.h>
Howard Hinnantdea7f392012-02-02 20:48:35 +000021
Nick Kledzik804b6e72010-05-14 20:19:37 +000022 using namespace __cxxabiv1;
David Chisnallc512df12011-09-21 08:39:44 +000023 #define HAVE_DEPENDENT_EH_ABI 1
Howard Hinnantdea7f392012-02-02 20:48:35 +000024 #ifndef _LIBCPPABI_VERSION
25 using namespace __cxxabiapple;
26 // On Darwin, there are two STL shared libraries and a lower level ABI
Marshall Clowa46a0ad2013-11-11 23:27:19 +000027 // shared library. The globals holding the current terminate handler and
Howard Hinnantdea7f392012-02-02 20:48:35 +000028 // 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
Richard Smith591e32d2012-07-11 09:35:47 +000032#elif defined(LIBCXXRT) || __has_include(<cxxabi.h>)
David Chisnallc512df12011-09-21 08:39:44 +000033 #include <cxxabi.h>
34 using namespace __cxxabiv1;
Richard Smith591e32d2012-07-11 09:35:47 +000035 #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION)
36 #define HAVE_DEPENDENT_EH_ABI 1
37 #endif
Howard Hinnante0f0bfb2013-01-22 14:48:10 +000038#elif !defined(__GLIBCXX__) // __has_include(<cxxabi.h>)
Nick Kledzik804b6e72010-05-14 20:19:37 +000039 static std::terminate_handler __terminate_handler;
40 static std::unexpected_handler __unexpected_handler;
Richard Smith591e32d2012-07-11 09:35:47 +000041#endif // __has_include(<cxxabi.h>)
Nick Kledzik804b6e72010-05-14 20:19:37 +000042
David Chisnall1e8b3f92012-02-29 12:59:17 +000043namespace std
44{
45
Michael J. Spencera358fbe2012-11-30 21:02:29 +000046#if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
Howard Hinnantdea7f392012-02-02 20:48:35 +000047
David Chisnallc512df12011-09-21 08:39:44 +000048// libcxxrt provides implementations of these functions itself.
David Chisnall1e8b3f92012-02-29 12:59:17 +000049unexpected_handler
50set_unexpected(unexpected_handler func) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +000051{
Howard Hinnanta4451512010-12-02 16:45:21 +000052 return __sync_lock_test_and_set(&__unexpected_handler, func);
53}
54
David Chisnall1e8b3f92012-02-29 12:59:17 +000055unexpected_handler
56get_unexpected() _NOEXCEPT
Howard Hinnanta4451512010-12-02 16:45:21 +000057{
David Chisnall1e8b3f92012-02-29 12:59:17 +000058 return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0);
Nick Kledzik804b6e72010-05-14 20:19:37 +000059}
60
Richard Smith0405cc42012-07-26 02:04:22 +000061_LIBCPP_NORETURN
Nick Kledzik804b6e72010-05-14 20:19:37 +000062void
David Chisnall1e8b3f92012-02-29 12:59:17 +000063unexpected()
Nick Kledzik804b6e72010-05-14 20:19:37 +000064{
David Chisnall1e8b3f92012-02-29 12:59:17 +000065 (*get_unexpected())();
Howard Hinnantd5109772010-08-22 13:53:14 +000066 // unexpected handler should not return
David Chisnall1e8b3f92012-02-29 12:59:17 +000067 terminate();
Nick Kledzik804b6e72010-05-14 20:19:37 +000068}
69
David Chisnall1e8b3f92012-02-29 12:59:17 +000070terminate_handler
71set_terminate(terminate_handler func) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +000072{
Howard Hinnanta4451512010-12-02 16:45:21 +000073 return __sync_lock_test_and_set(&__terminate_handler, func);
74}
75
David Chisnall1e8b3f92012-02-29 12:59:17 +000076terminate_handler
77get_terminate() _NOEXCEPT
Howard Hinnanta4451512010-12-02 16:45:21 +000078{
David Chisnall1e8b3f92012-02-29 12:59:17 +000079 return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0);
Nick Kledzik804b6e72010-05-14 20:19:37 +000080}
81
Marshall Clow2ccffef2013-11-19 18:05:03 +000082#ifndef __EMSCRIPTEN__ // We provide this in JS
Richard Smith0405cc42012-07-26 02:04:22 +000083_LIBCPP_NORETURN
Nick Kledzik804b6e72010-05-14 20:19:37 +000084void
David Chisnall1e8b3f92012-02-29 12:59:17 +000085terminate() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +000086{
Howard Hinnantd4444702010-08-11 17:04:31 +000087#ifndef _LIBCPP_NO_EXCEPTIONS
88 try
89 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000090#endif // _LIBCPP_NO_EXCEPTIONS
David Chisnall1e8b3f92012-02-29 12:59:17 +000091 (*get_terminate())();
Nick Kledzik804b6e72010-05-14 20:19:37 +000092 // handler should not return
Peter Collingbourne4a0555a2013-10-06 22:13:24 +000093 printf("terminate_handler unexpectedly returned\n");
94 ::abort();
Howard Hinnantd4444702010-08-11 17:04:31 +000095#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant16e6e1d2010-08-22 00:03:27 +000096 }
Howard Hinnantd4444702010-08-11 17:04:31 +000097 catch (...)
98 {
Nick Kledzik804b6e72010-05-14 20:19:37 +000099 // handler should not throw exception
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000100 printf("terminate_handler unexpectedly threw an exception\n");
101 ::abort();
Nick Kledzik804b6e72010-05-14 20:19:37 +0000102 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000103#endif // _LIBCPP_NO_EXCEPTIONS
Nick Kledzik804b6e72010-05-14 20:19:37 +0000104}
Marshall Clow2ccffef2013-11-19 18:05:03 +0000105#endif // !__EMSCRIPTEN__
Howard Hinnantdea7f392012-02-02 20:48:35 +0000106#endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION)
Nick Kledzik804b6e72010-05-14 20:19:37 +0000107
Marshall Clow2ccffef2013-11-19 18:05:03 +0000108#if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(__EMSCRIPTEN__)
David Chisnall1e8b3f92012-02-29 12:59:17 +0000109bool uncaught_exception() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000110{
Marshall Clowdece7fe2013-03-18 17:45:34 +0000111#if defined(__APPLE__) || defined(_LIBCPPABI_VERSION)
Howard Hinnantd5109772010-08-22 13:53:14 +0000112 // on Darwin, there is a helper function so __cxa_get_globals is private
Howard Hinnantdea7f392012-02-02 20:48:35 +0000113 return __cxa_uncaught_exception();
Howard Hinnantd0ed21e2012-02-29 15:37:30 +0000114#else // __APPLE__
Howard Hinnantf7555062013-10-04 21:14:44 +0000115# if defined(_MSC_VER) && ! defined(__clang__)
116 _LIBCPP_WARNING("uncaught_exception not yet implemented")
117# else
118# warning uncaught_exception not yet implemented
119# endif
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000120 printf("uncaught_exception not yet implemented\n");
121 ::abort();
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000122#endif // __APPLE__
Nick Kledzik804b6e72010-05-14 20:19:37 +0000123}
124
Howard Hinnantf7555062013-10-04 21:14:44 +0000125
Howard Hinnantdea7f392012-02-02 20:48:35 +0000126#ifndef _LIBCPPABI_VERSION
127
Howard Hinnanted569212011-05-26 18:23:59 +0000128exception::~exception() _NOEXCEPT
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000129{
Nick Kledzik804b6e72010-05-14 20:19:37 +0000130}
131
Howard Hinnanted569212011-05-26 18:23:59 +0000132const char* exception::what() const _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000133{
134 return "std::exception";
135}
136
David Chisnall21a84cf2012-03-14 14:11:13 +0000137#endif // _LIBCPPABI_VERSION
138#endif //LIBCXXRT
Michael J. Spencera358fbe2012-11-30 21:02:29 +0000139#if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__)
David Chisnall21a84cf2012-03-14 14:11:13 +0000140
141bad_exception::~bad_exception() _NOEXCEPT
142{
143}
144
Howard Hinnanted569212011-05-26 18:23:59 +0000145const char* bad_exception::what() const _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000146{
147 return "std::bad_exception";
148}
149
David Chisnall21a84cf2012-03-14 14:11:13 +0000150#endif
151
Peter Collingbourneece95912013-10-06 22:13:21 +0000152#if defined(__GLIBCXX__)
153
154// libsupc++ does not implement the dependent EH ABI and the functionality
155// it uses to implement std::exception_ptr (which it declares as an alias of
156// std::__exception_ptr::exception_ptr) is not directly exported to clients. So
157// we have little choice but to hijack std::__exception_ptr::exception_ptr's
158// (which fortunately has the same layout as our std::exception_ptr) copy
159// constructor, assignment operator and destructor (which are part of its
160// stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr)
161// function.
162
163namespace __exception_ptr
164{
165
166struct exception_ptr
167{
168 void* __ptr_;
169
170 exception_ptr(const exception_ptr&) _NOEXCEPT;
171 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
172 ~exception_ptr() _NOEXCEPT;
173};
174
175}
176
177_LIBCPP_NORETURN void rethrow_exception(__exception_ptr::exception_ptr);
178
179#endif
Howard Hinnantdea7f392012-02-02 20:48:35 +0000180
Howard Hinnanted569212011-05-26 18:23:59 +0000181exception_ptr::~exception_ptr() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000182{
David Chisnallc512df12011-09-21 08:39:44 +0000183#if HAVE_DEPENDENT_EH_ABI
184 __cxa_decrement_exception_refcount(__ptr_);
Peter Collingbourneece95912013-10-06 22:13:21 +0000185#elif defined(__GLIBCXX__)
186 reinterpret_cast<__exception_ptr::exception_ptr*>(this)->~exception_ptr();
Nick Kledzik804b6e72010-05-14 20:19:37 +0000187#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000188# if defined(_MSC_VER) && ! defined(__clang__)
189 _LIBCPP_WARNING("exception_ptr not yet implemented")
190# else
191# warning exception_ptr not yet implemented
192# endif
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000193 printf("exception_ptr not yet implemented\n");
194 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000195#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000196}
197
Howard Hinnanted569212011-05-26 18:23:59 +0000198exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000199 : __ptr_(other.__ptr_)
200{
David Chisnallc512df12011-09-21 08:39:44 +0000201#if HAVE_DEPENDENT_EH_ABI
202 __cxa_increment_exception_refcount(__ptr_);
Peter Collingbourneece95912013-10-06 22:13:21 +0000203#elif defined(__GLIBCXX__)
204 new (reinterpret_cast<void*>(this)) __exception_ptr::exception_ptr(
205 reinterpret_cast<const __exception_ptr::exception_ptr&>(other));
Nick Kledzik804b6e72010-05-14 20:19:37 +0000206#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000207# if defined(_MSC_VER) && ! defined(__clang__)
208 _LIBCPP_WARNING("exception_ptr not yet implemented")
209# else
210# warning exception_ptr not yet implemented
211# endif
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000212 printf("exception_ptr not yet implemented\n");
213 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000214#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000215}
216
Howard Hinnanted569212011-05-26 18:23:59 +0000217exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000218{
David Chisnallc512df12011-09-21 08:39:44 +0000219#if HAVE_DEPENDENT_EH_ABI
Nick Kledzik804b6e72010-05-14 20:19:37 +0000220 if (__ptr_ != other.__ptr_)
221 {
David Chisnallc512df12011-09-21 08:39:44 +0000222 __cxa_increment_exception_refcount(other.__ptr_);
223 __cxa_decrement_exception_refcount(__ptr_);
Howard Hinnantd5109772010-08-22 13:53:14 +0000224 __ptr_ = other.__ptr_;
225 }
Nick Kledzik804b6e72010-05-14 20:19:37 +0000226 return *this;
Peter Collingbourneece95912013-10-06 22:13:21 +0000227#elif defined(__GLIBCXX__)
228 *reinterpret_cast<__exception_ptr::exception_ptr*>(this) =
229 reinterpret_cast<const __exception_ptr::exception_ptr&>(other);
230 return *this;
231#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000232# if defined(_MSC_VER) && ! defined(__clang__)
233 _LIBCPP_WARNING("exception_ptr not yet implemented")
234# else
235# warning exception_ptr not yet implemented
236# endif
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000237 printf("exception_ptr not yet implemented\n");
238 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000239#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000240}
241
Howard Hinnanted569212011-05-26 18:23:59 +0000242nested_exception::nested_exception() _NOEXCEPT
Howard Hinnanted2c2912010-05-27 17:06:52 +0000243 : __ptr_(current_exception())
244{
245}
246
Peter Collingbourne40455c62013-10-06 22:13:16 +0000247#if !defined(__GLIBCXX__)
248
Howard Hinnanted569212011-05-26 18:23:59 +0000249nested_exception::~nested_exception() _NOEXCEPT
Howard Hinnanted2c2912010-05-27 17:06:52 +0000250{
251}
252
Peter Collingbourne40455c62013-10-06 22:13:16 +0000253#endif
254
Richard Smith0405cc42012-07-26 02:04:22 +0000255_LIBCPP_NORETURN
Howard Hinnanted2c2912010-05-27 17:06:52 +0000256void
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000257nested_exception::rethrow_nested() const
Howard Hinnanted2c2912010-05-27 17:06:52 +0000258{
259 if (__ptr_ == nullptr)
260 terminate();
261 rethrow_exception(__ptr_);
262}
263
Peter Collingbourneece95912013-10-06 22:13:21 +0000264#if !defined(__GLIBCXX__)
Nick Kledzik804b6e72010-05-14 20:19:37 +0000265
David Chisnall1e8b3f92012-02-29 12:59:17 +0000266exception_ptr current_exception() _NOEXCEPT
Nick Kledzik804b6e72010-05-14 20:19:37 +0000267{
David Chisnallc512df12011-09-21 08:39:44 +0000268#if HAVE_DEPENDENT_EH_ABI
Howard Hinnantd5109772010-08-22 13:53:14 +0000269 // be nicer if there was a constructor that took a ptr, then
270 // this whole function would be just:
271 // return exception_ptr(__cxa_current_primary_exception());
David Chisnall1e8b3f92012-02-29 12:59:17 +0000272 exception_ptr ptr;
David Chisnallc512df12011-09-21 08:39:44 +0000273 ptr.__ptr_ = __cxa_current_primary_exception();
Howard Hinnantd5109772010-08-22 13:53:14 +0000274 return ptr;
Peter Collingbourneece95912013-10-06 22:13:21 +0000275#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000276# if defined(_MSC_VER) && ! defined(__clang__)
277 _LIBCPP_WARNING( "exception_ptr not yet implemented" )
278# else
279# warning exception_ptr not yet implemented
280# endif
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000281 printf("exception_ptr not yet implemented\n");
282 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000283#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000284}
285
Peter Collingbourneece95912013-10-06 22:13:21 +0000286#endif // !__GLIBCXX__
287
Richard Smith0405cc42012-07-26 02:04:22 +0000288_LIBCPP_NORETURN
David Chisnall1e8b3f92012-02-29 12:59:17 +0000289void rethrow_exception(exception_ptr p)
Nick Kledzik804b6e72010-05-14 20:19:37 +0000290{
David Chisnallc512df12011-09-21 08:39:44 +0000291#if HAVE_DEPENDENT_EH_ABI
292 __cxa_rethrow_primary_exception(p.__ptr_);
Howard Hinnantd5109772010-08-22 13:53:14 +0000293 // if p.__ptr_ is NULL, above returns so we terminate
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000294 terminate();
Peter Collingbourneece95912013-10-06 22:13:21 +0000295#elif defined(__GLIBCXX__)
296 rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr&>(p));
297#else
Howard Hinnantf7555062013-10-04 21:14:44 +0000298# if defined(_MSC_VER) && ! defined(__clang__)
299 _LIBCPP_WARNING("exception_ptr not yet implemented")
300# else
301# warning exception_ptr not yet implemented
302# endif
Peter Collingbourne4a0555a2013-10-06 22:13:24 +0000303 printf("exception_ptr not yet implemented\n");
304 ::abort();
Peter Collingbourneece95912013-10-06 22:13:21 +0000305#endif
Nick Kledzik804b6e72010-05-14 20:19:37 +0000306}
David Chisnall1e8b3f92012-02-29 12:59:17 +0000307} // std