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