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