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