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 | 3472f5b | 2012-02-29 15:37:30 +0000 | [diff] [blame] | 102 | #elif LIBCXXRT |
103 | __cxa_eh_globals * globals = __cxa_get_globals(); | ||||
104 | return (globals->uncaughtExceptions != 0); | ||||
105 | #else // __APPLE__ | ||||
Howard Hinnant | 155c2af | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 106 | #warning uncaught_exception not yet implemented |
107 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 108 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 109 | } |
110 | |||||
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 111 | #ifndef _LIBCPPABI_VERSION |
112 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 113 | exception::~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 | |||||
David Chisnall | ba252b8 | 2012-03-14 14:11:13 +0000 | [diff] [blame^] | 122 | #endif // _LIBCPPABI_VERSION |
123 | #endif //LIBCXXRT | ||||
124 | #ifndef _LIBCPPABI_VERSION | ||||
125 | |||||
126 | bad_exception::~bad_exception() _NOEXCEPT | ||||
127 | { | ||||
128 | } | ||||
129 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 130 | const char* bad_exception::what() const _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 131 | { |
132 | return "std::bad_exception"; | ||||
133 | } | ||||
134 | |||||
David Chisnall | ba252b8 | 2012-03-14 14:11:13 +0000 | [diff] [blame^] | 135 | #endif |
136 | |||||
Howard Hinnant | 4ad4eee | 2012-02-02 20:48:35 +0000 | [diff] [blame] | 137 | |
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 138 | exception_ptr::~exception_ptr() _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 139 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 140 | #if HAVE_DEPENDENT_EH_ABI |
141 | __cxa_decrement_exception_refcount(__ptr_); | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 142 | #else |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 143 | #warning exception_ptr not yet implemented |
144 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 145 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 146 | } |
147 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 148 | exception_ptr::exception_ptr(const exception_ptr& other) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 149 | : __ptr_(other.__ptr_) |
150 | { | ||||
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 151 | #if HAVE_DEPENDENT_EH_ABI |
152 | __cxa_increment_exception_refcount(__ptr_); | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 153 | #else |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 154 | #warning exception_ptr not yet implemented |
155 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 156 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 157 | } |
158 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 159 | exception_ptr& exception_ptr::operator=(const exception_ptr& other) _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 160 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 161 | #if HAVE_DEPENDENT_EH_ABI |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 162 | if (__ptr_ != other.__ptr_) |
163 | { | ||||
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 164 | __cxa_increment_exception_refcount(other.__ptr_); |
165 | __cxa_decrement_exception_refcount(__ptr_); | ||||
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 166 | __ptr_ = other.__ptr_; |
167 | } | ||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 168 | return *this; |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 169 | #else // __APPLE__ |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 170 | #warning exception_ptr not yet implemented |
171 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 172 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 173 | } |
174 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 175 | nested_exception::nested_exception() _NOEXCEPT |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 176 | : __ptr_(current_exception()) |
177 | { | ||||
178 | } | ||||
179 | |||||
Howard Hinnant | 1bc52cf | 2011-05-26 18:23:59 +0000 | [diff] [blame] | 180 | nested_exception::~nested_exception() _NOEXCEPT |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 181 | { |
182 | } | ||||
183 | |||||
Howard Hinnant | 8df9629 | 2011-05-26 17:07:32 +0000 | [diff] [blame] | 184 | _ATTRIBUTE(noreturn) |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 185 | void |
Howard Hinnant | 8df9629 | 2011-05-26 17:07:32 +0000 | [diff] [blame] | 186 | nested_exception::rethrow_nested() const |
Howard Hinnant | e4f9272 | 2010-05-27 17:06:52 +0000 | [diff] [blame] | 187 | { |
188 | if (__ptr_ == nullptr) | ||||
189 | terminate(); | ||||
190 | rethrow_exception(__ptr_); | ||||
191 | } | ||||
192 | |||||
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 193 | |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 194 | exception_ptr current_exception() _NOEXCEPT |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 195 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 196 | #if HAVE_DEPENDENT_EH_ABI |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 197 | // be nicer if there was a constructor that took a ptr, then |
198 | // this whole function would be just: | ||||
199 | // return exception_ptr(__cxa_current_primary_exception()); | ||||
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 200 | exception_ptr ptr; |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 201 | ptr.__ptr_ = __cxa_current_primary_exception(); |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 202 | return ptr; |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 203 | #else // __APPLE__ |
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 204 | #warning exception_ptr not yet implemented |
205 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 206 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 207 | } |
208 | |||||
Howard Hinnant | 1aa8819 | 2012-02-03 18:31:43 +0000 | [diff] [blame] | 209 | _ATTRIBUTE(noreturn) |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 210 | void rethrow_exception(exception_ptr p) |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 211 | { |
David Chisnall | 1d58106 | 2011-09-21 08:39:44 +0000 | [diff] [blame] | 212 | #if HAVE_DEPENDENT_EH_ABI |
213 | __cxa_rethrow_primary_exception(p.__ptr_); | ||||
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 214 | // if p.__ptr_ is NULL, above returns so we terminate |
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 215 | terminate(); |
216 | #else // __APPLE__ | ||||
Howard Hinnant | 34468d4 | 2010-08-22 13:53:14 +0000 | [diff] [blame] | 217 | #warning exception_ptr not yet implemented |
218 | ::abort(); | ||||
Howard Hinnant | ffb308e | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 219 | #endif // __APPLE__ |
Nick Kledzik | d1a61bb | 2010-05-14 20:19:37 +0000 | [diff] [blame] | 220 | } |
David Chisnall | 3954f44 | 2012-02-29 12:59:17 +0000 | [diff] [blame] | 221 | } // std |