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