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