Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 1 | //===------------------------ exception.cpp -------------------------------===// |
2 | // | ||||
3 | // The LLVM Compiler Infrastructure | ||||
4 | // | ||||
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
6 | // Source Licenses. See LICENSE.TXT for details. | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 7 | // |
8 | //===----------------------------------------------------------------------===// | ||||
9 | #include <stdlib.h> | ||||
10 | |||||
11 | #include "exception" | ||||
12 | |||||
Richard Smith | 591e32d | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 13 | #ifndef __has_include |
14 | #define __has_include(inc) 0 | ||||
15 | #endif | ||||
16 | |||||
Marshall Clow | dece7fe | 2013-03-18 17:45:34 +0000 | [diff] [blame] | 17 | #ifdef __APPLE__ |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 18 | #include <cxxabi.h> |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 19 | |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 20 | using namespace __cxxabiv1; |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 21 | #define HAVE_DEPENDENT_EH_ABI 1 |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 22 | #ifndef _LIBCPPABI_VERSION |
23 | using namespace __cxxabiapple; | ||||
24 | // On Darwin, there are two STL shared libraries and a lower level ABI | ||||
25 | // shared libray. The globals holding the current terminate handler and | ||||
26 | // current unexpected handler are in the ABI library. | ||||
27 | #define __terminate_handler __cxxabiapple::__cxa_terminate_handler | ||||
28 | #define __unexpected_handler __cxxabiapple::__cxa_unexpected_handler | ||||
29 | #endif // _LIBCPPABI_VERSION | ||||
Richard Smith | 591e32d | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 30 | #elif defined(LIBCXXRT) || __has_include(<cxxabi.h>) |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 31 | #include <cxxabi.h> |
32 | using namespace __cxxabiv1; | ||||
Richard Smith | 591e32d | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 33 | #if defined(LIBCXXRT) || defined(_LIBCPPABI_VERSION) |
34 | #define HAVE_DEPENDENT_EH_ABI 1 | ||||
35 | #endif | ||||
Howard Hinnant | e0f0bfb | 2013-01-22 14:48:10 +0000 | [diff] [blame] | 36 | #elif !defined(__GLIBCXX__) // __has_include(<cxxabi.h>) |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 37 | static std::terminate_handler __terminate_handler; |
38 | static std::unexpected_handler __unexpected_handler; | ||||
Richard Smith | 591e32d | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 39 | #endif // __has_include(<cxxabi.h>) |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 40 | |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 41 | namespace std |
42 | { | ||||
43 | |||||
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 44 | #if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 45 | |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 46 | // libcxxrt provides implementations of these functions itself. |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 47 | unexpected_handler |
48 | set_unexpected(unexpected_handler func) _NOEXCEPT | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 49 | { |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 50 | return __sync_lock_test_and_set(&__unexpected_handler, func); |
51 | } | ||||
52 | |||||
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 53 | unexpected_handler |
54 | get_unexpected() _NOEXCEPT | ||||
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 55 | { |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 56 | return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 57 | } |
58 | |||||
Richard Smith | 0405cc4 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 59 | _LIBCPP_NORETURN |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 60 | void |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 61 | unexpected() |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 62 | { |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 63 | (*get_unexpected())(); |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 64 | // unexpected handler should not return |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 65 | terminate(); |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 66 | } |
67 | |||||
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 68 | terminate_handler |
69 | set_terminate(terminate_handler func) _NOEXCEPT | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 70 | { |
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 71 | return __sync_lock_test_and_set(&__terminate_handler, func); |
72 | } | ||||
73 | |||||
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 74 | terminate_handler |
75 | get_terminate() _NOEXCEPT | ||||
Howard Hinnant | a445151 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 76 | { |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 77 | return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 78 | } |
79 | |||||
Howard Hinnant | fc2f021 | 2013-03-29 18:27:28 +0000 | [diff] [blame^] | 80 | #ifndef EMSCRIPTEN // We provide this in JS |
Richard Smith | 0405cc4 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 81 | _LIBCPP_NORETURN |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 82 | void |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 83 | terminate() _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 84 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 85 | #ifndef _LIBCPP_NO_EXCEPTIONS |
86 | try | ||||
87 | { | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 88 | #endif // _LIBCPP_NO_EXCEPTIONS |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 89 | (*get_terminate())(); |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 90 | // handler should not return |
91 | ::abort (); | ||||
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 92 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 93 | } |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 94 | catch (...) |
95 | { | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 96 | // handler should not throw exception |
97 | ::abort (); | ||||
98 | } | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 99 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 100 | } |
Howard Hinnant | fc2f021 | 2013-03-29 18:27:28 +0000 | [diff] [blame^] | 101 | #endif // !EMSCRIPTEN |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 102 | #endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 103 | |
Howard Hinnant | fc2f021 | 2013-03-29 18:27:28 +0000 | [diff] [blame^] | 104 | #if !defined(LIBCXXRT) && !defined(__GLIBCXX__) && !defined(EMSCRIPTEN) |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 105 | bool uncaught_exception() _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 106 | { |
Marshall Clow | dece7fe | 2013-03-18 17:45:34 +0000 | [diff] [blame] | 107 | #if defined(__APPLE__) || defined(_LIBCPPABI_VERSION) |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 108 | // on Darwin, there is a helper function so __cxa_get_globals is private |
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 109 | return __cxa_uncaught_exception(); |
Howard Hinnant | d0ed21e | 2012-02-29 15:37:30 +0000 | [diff] [blame] | 110 | #else // __APPLE__ |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 111 | #warning uncaught_exception not yet implemented |
112 | ::abort(); | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 113 | #endif // __APPLE__ |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 114 | } |
115 | |||||
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 116 | #ifndef _LIBCPPABI_VERSION |
117 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 118 | exception::~exception() _NOEXCEPT |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 119 | { |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 120 | } |
121 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 122 | const char* exception::what() const _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 123 | { |
124 | return "std::exception"; | ||||
125 | } | ||||
126 | |||||
David Chisnall | 21a84cf | 2012-03-14 14:11:13 +0000 | [diff] [blame] | 127 | #endif // _LIBCPPABI_VERSION |
128 | #endif //LIBCXXRT | ||||
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 129 | #if !defined(_LIBCPPABI_VERSION) && !defined(__GLIBCXX__) |
David Chisnall | 21a84cf | 2012-03-14 14:11:13 +0000 | [diff] [blame] | 130 | |
131 | bad_exception::~bad_exception() _NOEXCEPT | ||||
132 | { | ||||
133 | } | ||||
134 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 135 | const char* bad_exception::what() const _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 136 | { |
137 | return "std::bad_exception"; | ||||
138 | } | ||||
139 | |||||
David Chisnall | 21a84cf | 2012-03-14 14:11:13 +0000 | [diff] [blame] | 140 | #endif |
141 | |||||
Howard Hinnant | dea7f39 | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 142 | |
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 143 | exception_ptr::~exception_ptr() _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 144 | { |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 145 | #if HAVE_DEPENDENT_EH_ABI |
146 | __cxa_decrement_exception_refcount(__ptr_); | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 147 | #else |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 148 | #warning exception_ptr not yet implemented |
149 | ::abort(); | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 150 | #endif // __APPLE__ |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 151 | } |
152 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 153 | exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 154 | : __ptr_(other.__ptr_) |
155 | { | ||||
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 156 | #if HAVE_DEPENDENT_EH_ABI |
157 | __cxa_increment_exception_refcount(__ptr_); | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 158 | #else |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 159 | #warning exception_ptr not yet implemented |
160 | ::abort(); | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 161 | #endif // __APPLE__ |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 162 | } |
163 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 164 | exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 165 | { |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 166 | #if HAVE_DEPENDENT_EH_ABI |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 167 | if (__ptr_ != other.__ptr_) |
168 | { | ||||
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 169 | __cxa_increment_exception_refcount(other.__ptr_); |
170 | __cxa_decrement_exception_refcount(__ptr_); | ||||
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 171 | __ptr_ = other.__ptr_; |
172 | } | ||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 173 | return *this; |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 174 | #else // __APPLE__ |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 175 | #warning exception_ptr not yet implemented |
176 | ::abort(); | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 177 | #endif // __APPLE__ |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 178 | } |
179 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 180 | nested_exception::nested_exception() _NOEXCEPT |
Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 181 | : __ptr_(current_exception()) |
182 | { | ||||
183 | } | ||||
184 | |||||
Howard Hinnant | ed56921 | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 185 | nested_exception::~nested_exception() _NOEXCEPT |
Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 186 | { |
187 | } | ||||
188 | |||||
Richard Smith | 0405cc4 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 189 | _LIBCPP_NORETURN |
Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 190 | void |
Howard Hinnant | 4b7a43d | 2011-05-26 17:07:32 +0000 | [diff] [blame] | 191 | nested_exception::rethrow_nested() const |
Howard Hinnant | ed2c291 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 192 | { |
193 | if (__ptr_ == nullptr) | ||||
194 | terminate(); | ||||
195 | rethrow_exception(__ptr_); | ||||
196 | } | ||||
197 | |||||
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 198 | |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 199 | exception_ptr current_exception() _NOEXCEPT |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 200 | { |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 201 | #if HAVE_DEPENDENT_EH_ABI |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 202 | // be nicer if there was a constructor that took a ptr, then |
203 | // this whole function would be just: | ||||
204 | // return exception_ptr(__cxa_current_primary_exception()); | ||||
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 205 | exception_ptr ptr; |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 206 | ptr.__ptr_ = __cxa_current_primary_exception(); |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 207 | return ptr; |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 208 | #else // __APPLE__ |
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 209 | #warning exception_ptr not yet implemented |
210 | ::abort(); | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 211 | #endif // __APPLE__ |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 212 | } |
213 | |||||
Richard Smith | 0405cc4 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 214 | _LIBCPP_NORETURN |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 215 | void rethrow_exception(exception_ptr p) |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 216 | { |
David Chisnall | c512df1 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 217 | #if HAVE_DEPENDENT_EH_ABI |
218 | __cxa_rethrow_primary_exception(p.__ptr_); | ||||
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 219 | // if p.__ptr_ is NULL, above returns so we terminate |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 220 | terminate(); |
221 | #else // __APPLE__ | ||||
Howard Hinnant | d510977 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 222 | #warning exception_ptr not yet implemented |
223 | ::abort(); | ||||
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 224 | #endif // __APPLE__ |
Nick Kledzik | 804b6e7 | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 225 | } |
David Chisnall | 1e8b3f9 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 226 | } // std |