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