106c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 206c3fb27SDimitry Andric // 306c3fb27SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 406c3fb27SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 506c3fb27SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 606c3fb27SDimitry Andric // 706c3fb27SDimitry Andric //===----------------------------------------------------------------------===// 806c3fb27SDimitry Andric 906c3fb27SDimitry Andric #ifndef _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H 1006c3fb27SDimitry Andric #define _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H 1106c3fb27SDimitry Andric 1206c3fb27SDimitry Andric #include <__config> 1306c3fb27SDimitry Andric #include <__exception/exception_ptr.h> 1406c3fb27SDimitry Andric #include <__memory/addressof.h> 1506c3fb27SDimitry Andric #include <__type_traits/decay.h> 1606c3fb27SDimitry Andric #include <__type_traits/is_base_of.h> 1706c3fb27SDimitry Andric #include <__type_traits/is_class.h> 18*0fca6ea1SDimitry Andric #include <__type_traits/is_constructible.h> 1906c3fb27SDimitry Andric #include <__type_traits/is_convertible.h> 2006c3fb27SDimitry Andric #include <__type_traits/is_final.h> 2106c3fb27SDimitry Andric #include <__type_traits/is_polymorphic.h> 2206c3fb27SDimitry Andric #include <__utility/forward.h> 2306c3fb27SDimitry Andric #include <cstddef> 2406c3fb27SDimitry Andric 2506c3fb27SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 2606c3fb27SDimitry Andric # pragma GCC system_header 2706c3fb27SDimitry Andric #endif 2806c3fb27SDimitry Andric 2906c3fb27SDimitry Andric namespace std { // purposefully not using versioning namespace 3006c3fb27SDimitry Andric 3106c3fb27SDimitry Andric class _LIBCPP_EXPORTED_FROM_ABI nested_exception { 3206c3fb27SDimitry Andric exception_ptr __ptr_; 3306c3fb27SDimitry Andric 3406c3fb27SDimitry Andric public: 3506c3fb27SDimitry Andric nested_exception() _NOEXCEPT; 365f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI nested_exception(const nested_exception&) _NOEXCEPT = default; 375f757f3fSDimitry Andric _LIBCPP_HIDE_FROM_ABI nested_exception& operator=(const nested_exception&) _NOEXCEPT = default; 3806c3fb27SDimitry Andric virtual ~nested_exception() _NOEXCEPT; 3906c3fb27SDimitry Andric 4006c3fb27SDimitry Andric // access functions 4106c3fb27SDimitry Andric _LIBCPP_NORETURN void rethrow_nested() const; 4206c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI exception_ptr nested_ptr() const _NOEXCEPT { return __ptr_; } 4306c3fb27SDimitry Andric }; 4406c3fb27SDimitry Andric 4506c3fb27SDimitry Andric template <class _Tp> 4606c3fb27SDimitry Andric struct __nested : public _Tp, public nested_exception { 4706c3fb27SDimitry Andric _LIBCPP_HIDE_FROM_ABI explicit __nested(const _Tp& __t) : _Tp(__t) {} 4806c3fb27SDimitry Andric }; 4906c3fb27SDimitry Andric 5006c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 5106c3fb27SDimitry Andric template <class _Tp, class _Up, bool> 5206c3fb27SDimitry Andric struct __throw_with_nested; 5306c3fb27SDimitry Andric 5406c3fb27SDimitry Andric template <class _Tp, class _Up> 5506c3fb27SDimitry Andric struct __throw_with_nested<_Tp, _Up, true> { 565f757f3fSDimitry Andric _LIBCPP_NORETURN static inline _LIBCPP_HIDE_FROM_ABI void __do_throw(_Tp&& __t) { 5706c3fb27SDimitry Andric throw __nested<_Up>(std::forward<_Tp>(__t)); 5806c3fb27SDimitry Andric } 5906c3fb27SDimitry Andric }; 6006c3fb27SDimitry Andric 6106c3fb27SDimitry Andric template <class _Tp, class _Up> 6206c3fb27SDimitry Andric struct __throw_with_nested<_Tp, _Up, false> { 635f757f3fSDimitry Andric _LIBCPP_NORETURN static inline _LIBCPP_HIDE_FROM_ABI void __do_throw(_Tp&& __t) { throw std::forward<_Tp>(__t); } 6406c3fb27SDimitry Andric }; 6506c3fb27SDimitry Andric #endif 6606c3fb27SDimitry Andric 6706c3fb27SDimitry Andric template <class _Tp> 6806c3fb27SDimitry Andric _LIBCPP_NORETURN _LIBCPP_HIDE_FROM_ABI void throw_with_nested(_Tp&& __t) { 6906c3fb27SDimitry Andric #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 7006c3fb27SDimitry Andric using _Up = __decay_t<_Tp>; 7106c3fb27SDimitry Andric static_assert(is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible"); 7206c3fb27SDimitry Andric __throw_with_nested<_Tp, 7306c3fb27SDimitry Andric _Up, 7406c3fb27SDimitry Andric is_class<_Up>::value && !is_base_of<nested_exception, _Up>::value && 7506c3fb27SDimitry Andric !__libcpp_is_final<_Up>::value>::__do_throw(std::forward<_Tp>(__t)); 7606c3fb27SDimitry Andric #else 7706c3fb27SDimitry Andric ((void)__t); 7806c3fb27SDimitry Andric // FIXME: Make this abort 7906c3fb27SDimitry Andric #endif 8006c3fb27SDimitry Andric } 8106c3fb27SDimitry Andric 8206c3fb27SDimitry Andric template <class _From, class _To> 8306c3fb27SDimitry Andric struct __can_dynamic_cast 8406c3fb27SDimitry Andric : _BoolConstant< is_polymorphic<_From>::value && 8506c3fb27SDimitry Andric (!is_base_of<_To, _From>::value || is_convertible<const _From*, const _To*>::value)> {}; 8606c3fb27SDimitry Andric 87*0fca6ea1SDimitry Andric template <class _Ep, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value, int> = 0> 88*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep& __e) { 8906c3fb27SDimitry Andric const nested_exception* __nep = dynamic_cast<const nested_exception*>(std::addressof(__e)); 9006c3fb27SDimitry Andric if (__nep) 9106c3fb27SDimitry Andric __nep->rethrow_nested(); 9206c3fb27SDimitry Andric } 9306c3fb27SDimitry Andric 94*0fca6ea1SDimitry Andric template <class _Ep, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value, int> = 0> 95*0fca6ea1SDimitry Andric inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep&) {} 9606c3fb27SDimitry Andric 9706c3fb27SDimitry Andric } // namespace std 9806c3fb27SDimitry Andric 9906c3fb27SDimitry Andric #endif // _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H 100