1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_EXCEPTION 11#define _LIBCPP_EXCEPTION 12 13/* 14 exception synopsis 15 16namespace std 17{ 18 19class exception 20{ 21public: 22 exception() noexcept; 23 exception(const exception&) noexcept; 24 exception& operator=(const exception&) noexcept; 25 virtual ~exception() noexcept; 26 virtual const char* what() const noexcept; 27}; 28 29class bad_exception 30 : public exception 31{ 32public: 33 bad_exception() noexcept; 34 bad_exception(const bad_exception&) noexcept; 35 bad_exception& operator=(const bad_exception&) noexcept; 36 virtual ~bad_exception() noexcept; 37 virtual const char* what() const noexcept; 38}; 39 40typedef void (*unexpected_handler)(); 41unexpected_handler set_unexpected(unexpected_handler f ) noexcept; 42unexpected_handler get_unexpected() noexcept; 43[[noreturn]] void unexpected(); 44 45typedef void (*terminate_handler)(); 46terminate_handler set_terminate(terminate_handler f ) noexcept; 47terminate_handler get_terminate() noexcept; 48[[noreturn]] void terminate() noexcept; 49 50bool uncaught_exception() noexcept; 51int uncaught_exceptions() noexcept; // C++17 52 53typedef unspecified exception_ptr; 54 55exception_ptr current_exception() noexcept; 56void rethrow_exception [[noreturn]] (exception_ptr p); 57template<class E> exception_ptr make_exception_ptr(E e) noexcept; 58 59class nested_exception 60{ 61public: 62 nested_exception() noexcept; 63 nested_exception(const nested_exception&) noexcept = default; 64 nested_exception& operator=(const nested_exception&) noexcept = default; 65 virtual ~nested_exception() = default; 66 67 // access functions 68 [[noreturn]] void rethrow_nested() const; 69 exception_ptr nested_ptr() const noexcept; 70}; 71 72template <class T> [[noreturn]] void throw_with_nested(T&& t); 73template <class E> void rethrow_if_nested(const E& e); 74 75} // std 76 77*/ 78 79#include <__assert> // all public C++ headers provide the assertion handler 80#include <__availability> 81#include <__config> 82#include <__memory/addressof.h> 83#include <cstddef> 84#include <cstdlib> 85#include <type_traits> 86#include <version> 87 88#if defined(_LIBCPP_ABI_VCRUNTIME) 89#include <vcruntime_exception.h> 90#endif 91 92#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 93# pragma GCC system_header 94#endif 95 96namespace std // purposefully not using versioning namespace 97{ 98 99#if !defined(_LIBCPP_ABI_VCRUNTIME) 100class _LIBCPP_EXCEPTION_ABI exception 101{ 102public: 103 _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {} 104 _LIBCPP_INLINE_VISIBILITY exception(const exception&) _NOEXCEPT = default; 105 106 virtual ~exception() _NOEXCEPT; 107 virtual const char* what() const _NOEXCEPT; 108}; 109 110class _LIBCPP_EXCEPTION_ABI bad_exception 111 : public exception 112{ 113public: 114 _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {} 115 virtual ~bad_exception() _NOEXCEPT; 116 virtual const char* what() const _NOEXCEPT; 117}; 118#endif // !_LIBCPP_ABI_VCRUNTIME 119 120#if _LIBCPP_STD_VER <= 14 \ 121 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS) \ 122 || defined(_LIBCPP_BUILDING_LIBRARY) 123typedef void (*unexpected_handler)(); 124_LIBCPP_FUNC_VIS unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT; 125_LIBCPP_FUNC_VIS unexpected_handler get_unexpected() _NOEXCEPT; 126_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void unexpected(); 127#endif 128 129typedef void (*terminate_handler)(); 130_LIBCPP_FUNC_VIS terminate_handler set_terminate(terminate_handler) _NOEXCEPT; 131_LIBCPP_FUNC_VIS terminate_handler get_terminate() _NOEXCEPT; 132_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void terminate() _NOEXCEPT; 133 134_LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT; 135_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_exceptions() _NOEXCEPT; 136 137class _LIBCPP_TYPE_VIS exception_ptr; 138 139_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT; 140_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr); 141 142#ifndef _LIBCPP_ABI_MICROSOFT 143 144class _LIBCPP_TYPE_VIS exception_ptr 145{ 146 void* __ptr_; 147public: 148 _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {} 149 _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {} 150 151 exception_ptr(const exception_ptr&) _NOEXCEPT; 152 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT; 153 ~exception_ptr() _NOEXCEPT; 154 155 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const _NOEXCEPT 156 {return __ptr_ != nullptr;} 157 158 friend _LIBCPP_INLINE_VISIBILITY 159 bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT 160 {return __x.__ptr_ == __y.__ptr_;} 161 162 friend _LIBCPP_INLINE_VISIBILITY 163 bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT 164 {return !(__x == __y);} 165 166 friend _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT; 167 friend _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr); 168}; 169 170template<class _Ep> 171_LIBCPP_INLINE_VISIBILITY exception_ptr 172make_exception_ptr(_Ep __e) _NOEXCEPT 173{ 174#ifndef _LIBCPP_NO_EXCEPTIONS 175 try 176 { 177 throw __e; 178 } 179 catch (...) 180 { 181 return current_exception(); 182 } 183#else 184 ((void)__e); 185 _VSTD::abort(); 186#endif 187} 188 189#else // _LIBCPP_ABI_MICROSOFT 190 191class _LIBCPP_TYPE_VIS exception_ptr 192{ 193_LIBCPP_DIAGNOSTIC_PUSH 194_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Wunused-private-field") 195 void* __ptr1_; 196 void* __ptr2_; 197_LIBCPP_DIAGNOSTIC_POP 198public: 199 exception_ptr() _NOEXCEPT; 200 exception_ptr(nullptr_t) _NOEXCEPT; 201 exception_ptr(const exception_ptr& __other) _NOEXCEPT; 202 exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT; 203 exception_ptr& operator=(nullptr_t) _NOEXCEPT; 204 ~exception_ptr() _NOEXCEPT; 205 explicit operator bool() const _NOEXCEPT; 206}; 207 208_LIBCPP_FUNC_VIS 209bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT; 210 211inline _LIBCPP_INLINE_VISIBILITY 212bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT 213 {return !(__x == __y);} 214 215_LIBCPP_FUNC_VIS void swap(exception_ptr&, exception_ptr&) _NOEXCEPT; 216 217_LIBCPP_FUNC_VIS exception_ptr __copy_exception_ptr(void *__except, const void* __ptr); 218_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT; 219_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr); 220 221// This is a built-in template function which automagically extracts the required 222// information. 223template <class _E> void *__GetExceptionInfo(_E); 224 225template<class _Ep> 226_LIBCPP_INLINE_VISIBILITY exception_ptr 227make_exception_ptr(_Ep __e) _NOEXCEPT 228{ 229 return __copy_exception_ptr(_VSTD::addressof(__e), __GetExceptionInfo(__e)); 230} 231 232#endif // _LIBCPP_ABI_MICROSOFT 233// nested_exception 234 235class _LIBCPP_EXCEPTION_ABI nested_exception 236{ 237 exception_ptr __ptr_; 238public: 239 nested_exception() _NOEXCEPT; 240// nested_exception(const nested_exception&) noexcept = default; 241// nested_exception& operator=(const nested_exception&) noexcept = default; 242 virtual ~nested_exception() _NOEXCEPT; 243 244 // access functions 245 _LIBCPP_NORETURN void rethrow_nested() const; 246 _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;} 247}; 248 249template <class _Tp> 250struct __nested 251 : public _Tp, 252 public nested_exception 253{ 254 _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {} 255}; 256 257#ifndef _LIBCPP_NO_EXCEPTIONS 258template <class _Tp, class _Up, bool> 259struct __throw_with_nested; 260 261template <class _Tp, class _Up> 262struct __throw_with_nested<_Tp, _Up, true> { 263 _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void 264 __do_throw(_Tp&& __t) 265 { 266 throw __nested<_Up>(static_cast<_Tp&&>(__t)); 267 } 268}; 269 270template <class _Tp, class _Up> 271struct __throw_with_nested<_Tp, _Up, false> { 272 _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void 273#ifndef _LIBCPP_CXX03_LANG 274 __do_throw(_Tp&& __t) 275#else 276 __do_throw (_Tp& __t) 277#endif // _LIBCPP_CXX03_LANG 278 { 279 throw static_cast<_Tp&&>(__t); 280 } 281}; 282#endif 283 284template <class _Tp> 285_LIBCPP_NORETURN 286void 287throw_with_nested(_Tp&& __t) 288{ 289#ifndef _LIBCPP_NO_EXCEPTIONS 290 typedef typename decay<_Tp>::type _Up; 291 static_assert( is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible"); 292 __throw_with_nested<_Tp, _Up, 293 is_class<_Up>::value && 294 !is_base_of<nested_exception, _Up>::value && 295 !__libcpp_is_final<_Up>::value>:: 296 __do_throw(static_cast<_Tp&&>(__t)); 297#else 298 ((void)__t); 299 // FIXME: Make this abort 300#endif 301} 302 303template <class _From, class _To> 304struct __can_dynamic_cast : _BoolConstant< 305 is_polymorphic<_From>::value && 306 (!is_base_of<_To, _From>::value || 307 is_convertible<const _From*, const _To*>::value)> {}; 308 309template <class _Ep> 310inline _LIBCPP_INLINE_VISIBILITY 311void 312rethrow_if_nested(const _Ep& __e, 313 __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value>* = 0) 314{ 315 const nested_exception* __nep = dynamic_cast<const nested_exception*>(_VSTD::addressof(__e)); 316 if (__nep) 317 __nep->rethrow_nested(); 318} 319 320template <class _Ep> 321inline _LIBCPP_INLINE_VISIBILITY 322void 323rethrow_if_nested(const _Ep&, 324 __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value>* = 0) 325{ 326} 327 328} // namespace std 329 330#endif // _LIBCPP_EXCEPTION 331