Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 1 | //===------------------------ exception.cpp -------------------------------===// |
2 | // | ||||
3 | // The LLVM Compiler Infrastructure | ||||
4 | // | ||||
Howard Hinnant | ee11c31 | 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 | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 7 | // |
8 | //===----------------------------------------------------------------------===// | ||||
9 | #include <stdlib.h> | ||||
10 | |||||
11 | #include "exception" | ||||
12 | |||||
Richard Smith | ffd7467 | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 13 | #ifndef __has_include |
14 | #define __has_include(inc) 0 | ||||
15 | #endif | ||||
16 | |||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 17 | #if __APPLE__ |
18 | #include <cxxabi.h> | ||||
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 19 | |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 20 | using namespace __cxxabiv1; |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 21 | #define HAVE_DEPENDENT_EH_ABI 1 |
Howard Hinnant | 4ad4eee | 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 | ffd7467 | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 30 | #elif defined(LIBCXXRT) || __has_include(<cxxabi.h>) |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 31 | #include <cxxabi.h> |
32 | using namespace __cxxabiv1; | ||||
Richard Smith | ffd7467 | 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 | ||||
36 | #else // __has_include(<cxxabi.h>) | ||||
Nick Kledzik | d1a61bb | 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 | ffd7467 | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 39 | #endif // __has_include(<cxxabi.h>) |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 40 | |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 41 | namespace std |
42 | { | ||||
43 | |||||
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 44 | #if !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) |
45 | |||||
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 46 | // libcxxrt provides implementations of these functions itself. |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 47 | unexpected_handler |
48 | set_unexpected(unexpected_handler func) _NOEXCEPT | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 49 | { |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 50 | return __sync_lock_test_and_set(&__unexpected_handler, func); |
51 | } | ||||
52 | |||||
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 53 | unexpected_handler |
54 | get_unexpected() _NOEXCEPT | ||||
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 55 | { |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 56 | return __sync_fetch_and_add(&__unexpected_handler, (unexpected_handler)0); |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 57 | } |
58 | |||||
Richard Smith | cabd392 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 59 | _LIBCPP_NORETURN |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 60 | void |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 61 | unexpected() |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 62 | { |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 63 | (*get_unexpected())(); |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 64 | // unexpected handler should not return |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 65 | terminate(); |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 66 | } |
67 | |||||
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 68 | terminate_handler |
69 | set_terminate(terminate_handler func) _NOEXCEPT | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 70 | { |
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 71 | return __sync_lock_test_and_set(&__terminate_handler, func); |
72 | } | ||||
73 | |||||
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 74 | terminate_handler |
75 | get_terminate() _NOEXCEPT | ||||
Howard Hinnant | e65e8e3 | 2010-12-02 16:45:21 +0000 | [diff] [blame] | 76 | { |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 77 | return __sync_fetch_and_add(&__terminate_handler, (terminate_handler)0); |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 78 | } |
79 | |||||
Richard Smith | cabd392 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 80 | _LIBCPP_NORETURN |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 81 | void |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 82 | terminate() _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 83 | { |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 84 | #ifndef _LIBCPP_NO_EXCEPTIONS |
85 | try | ||||
86 | { | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 87 | #endif // _LIBCPP_NO_EXCEPTIONS |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 88 | (*get_terminate())(); |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 89 | // handler should not return |
90 | ::abort (); | ||||
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 91 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 92 | } |
Howard Hinnant | 72f7358 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 93 | catch (...) |
94 | { | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 95 | // handler should not throw exception |
96 | ::abort (); | ||||
97 | } | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 98 | #endif // _LIBCPP_NO_EXCEPTIONS |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 99 | } |
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 100 | #endif // !defined(LIBCXXRT) && !defined(_LIBCPPABI_VERSION) |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 101 | |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 102 | #ifndef LIBCXXRT |
103 | bool uncaught_exception() _NOEXCEPT | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 104 | { |
Richard Smith | ffd7467 | 2012-07-11 09:35:47 +0000 | [diff] [blame] | 105 | #if __APPLE__ || defined(_LIBCPPABI_VERSION) |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 106 | // on Darwin, there is a helper function so __cxa_get_globals is private |
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 107 | return __cxa_uncaught_exception(); |
Howard Hinnant | 3472f5b | 2012-02-29 15:37:30 +0000 | [diff] [blame] | 108 | #else // __APPLE__ |
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 109 | #warning uncaught_exception not yet implemented |
110 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 111 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 112 | } |
113 | |||||
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 114 | #ifndef _LIBCPPABI_VERSION |
115 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 116 | exception::~exception() _NOEXCEPT |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 117 | { |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 118 | } |
119 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 120 | const char* exception::what() const _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 121 | { |
122 | return "std::exception"; | ||||
123 | } | ||||
124 | |||||
David Chisnall | ba252b8 | 2012-03-14 14:11:13 +0000 | [diff] [blame] | 125 | #endif // _LIBCPPABI_VERSION |
126 | #endif //LIBCXXRT | ||||
127 | #ifndef _LIBCPPABI_VERSION | ||||
128 | |||||
129 | bad_exception::~bad_exception() _NOEXCEPT | ||||
130 | { | ||||
131 | } | ||||
132 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 133 | const char* bad_exception::what() const _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 134 | { |
135 | return "std::bad_exception"; | ||||
136 | } | ||||
137 | |||||
David Chisnall | ba252b8 | 2012-03-14 14:11:13 +0000 | [diff] [blame] | 138 | #endif |
139 | |||||
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 140 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 141 | exception_ptr::~exception_ptr() _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 142 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 143 | #if HAVE_DEPENDENT_EH_ABI |
144 | __cxa_decrement_exception_refcount(__ptr_); | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 145 | #else |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 146 | #warning exception_ptr not yet implemented |
147 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 148 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 149 | } |
150 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 151 | exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 152 | : __ptr_(other.__ptr_) |
153 | { | ||||
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 154 | #if HAVE_DEPENDENT_EH_ABI |
155 | __cxa_increment_exception_refcount(__ptr_); | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 156 | #else |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 157 | #warning exception_ptr not yet implemented |
158 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 159 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 160 | } |
161 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 162 | exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 163 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 164 | #if HAVE_DEPENDENT_EH_ABI |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 165 | if (__ptr_ != other.__ptr_) |
166 | { | ||||
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 167 | __cxa_increment_exception_refcount(other.__ptr_); |
168 | __cxa_decrement_exception_refcount(__ptr_); | ||||
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 169 | __ptr_ = other.__ptr_; |
170 | } | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 171 | return *this; |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 172 | #else // __APPLE__ |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 173 | #warning exception_ptr not yet implemented |
174 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 175 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 176 | } |
177 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 178 | nested_exception::nested_exception() _NOEXCEPT |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 179 | : __ptr_(current_exception()) |
180 | { | ||||
181 | } | ||||
182 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 183 | nested_exception::~nested_exception() _NOEXCEPT |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 184 | { |
185 | } | ||||
186 | |||||
Richard Smith | cabd392 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 187 | _LIBCPP_NORETURN |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 188 | void |
Howard Hinnant | 8df9629 | 2011-05-26 17:07:32 +0000 | [diff] [blame] | 189 | nested_exception::rethrow_nested() const |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 190 | { |
191 | if (__ptr_ == nullptr) | ||||
192 | terminate(); | ||||
193 | rethrow_exception(__ptr_); | ||||
194 | } | ||||
195 | |||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 196 | |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 197 | exception_ptr current_exception() _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 198 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 199 | #if HAVE_DEPENDENT_EH_ABI |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 200 | // be nicer if there was a constructor that took a ptr, then |
201 | // this whole function would be just: | ||||
202 | // return exception_ptr(__cxa_current_primary_exception()); | ||||
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 203 | exception_ptr ptr; |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 204 | ptr.__ptr_ = __cxa_current_primary_exception(); |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 205 | return ptr; |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 206 | #else // __APPLE__ |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 207 | #warning exception_ptr not yet implemented |
208 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 209 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 210 | } |
211 | |||||
Richard Smith | cabd392 | 2012-07-26 02:04:22 +0000 | [diff] [blame] | 212 | _LIBCPP_NORETURN |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 213 | void rethrow_exception(exception_ptr p) |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 214 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 215 | #if HAVE_DEPENDENT_EH_ABI |
216 | __cxa_rethrow_primary_exception(p.__ptr_); | ||||
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 217 | // if p.__ptr_ is NULL, above returns so we terminate |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 218 | terminate(); |
219 | #else // __APPLE__ | ||||
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 220 | #warning exception_ptr not yet implemented |
221 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 222 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 223 | } |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 224 | } // std |