master
  1//===----------------------------------------------------------------------===//
  2//
  3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4// See https://llvm.org/LICENSE.txt for license information.
  5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6//
  7//===----------------------------------------------------------------------===//
  8
  9#ifndef _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H
 10#define _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H
 11
 12#include <__config>
 13#include <__exception/exception_ptr.h>
 14#include <__memory/addressof.h>
 15#include <__type_traits/decay.h>
 16#include <__type_traits/enable_if.h>
 17#include <__type_traits/integral_constant.h>
 18#include <__type_traits/is_base_of.h>
 19#include <__type_traits/is_class.h>
 20#include <__type_traits/is_constructible.h>
 21#include <__type_traits/is_convertible.h>
 22#include <__type_traits/is_final.h>
 23#include <__type_traits/is_polymorphic.h>
 24#include <__utility/forward.h>
 25
 26#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 27#  pragma GCC system_header
 28#endif
 29
 30_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
 31
 32class _LIBCPP_EXPORTED_FROM_ABI nested_exception {
 33  exception_ptr __ptr_;
 34
 35public:
 36  nested_exception() _NOEXCEPT;
 37  _LIBCPP_HIDE_FROM_ABI nested_exception(const nested_exception&) _NOEXCEPT            = default;
 38  _LIBCPP_HIDE_FROM_ABI nested_exception& operator=(const nested_exception&) _NOEXCEPT = default;
 39  virtual ~nested_exception() _NOEXCEPT;
 40
 41  // access functions
 42  [[__noreturn__]] void rethrow_nested() const;
 43  _LIBCPP_HIDE_FROM_ABI exception_ptr nested_ptr() const _NOEXCEPT { return __ptr_; }
 44};
 45
 46template <class _Tp>
 47struct __nested : public _Tp, public nested_exception {
 48  _LIBCPP_HIDE_FROM_ABI explicit __nested(const _Tp& __t) : _Tp(__t) {}
 49};
 50
 51#if _LIBCPP_HAS_EXCEPTIONS
 52template <class _Tp, class _Up, bool>
 53struct __throw_with_nested;
 54
 55template <class _Tp, class _Up>
 56struct __throw_with_nested<_Tp, _Up, true> {
 57  [[__noreturn__]] static inline _LIBCPP_HIDE_FROM_ABI void __do_throw(_Tp&& __t) {
 58    throw __nested<_Up>(std::forward<_Tp>(__t));
 59  }
 60};
 61
 62template <class _Tp, class _Up>
 63struct __throw_with_nested<_Tp, _Up, false> {
 64  [[__noreturn__]] static inline _LIBCPP_HIDE_FROM_ABI void __do_throw(_Tp&& __t) { throw std::forward<_Tp>(__t); }
 65};
 66#endif
 67
 68template <class _Tp>
 69[[__noreturn__]] _LIBCPP_HIDE_FROM_ABI void throw_with_nested(_Tp&& __t) {
 70#if _LIBCPP_HAS_EXCEPTIONS
 71  using _Up = __decay_t<_Tp>;
 72  static_assert(is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
 73  __throw_with_nested<_Tp,
 74                      _Up,
 75                      is_class<_Up>::value && !is_base_of<nested_exception, _Up>::value &&
 76                          !__libcpp_is_final<_Up>::value>::__do_throw(std::forward<_Tp>(__t));
 77#else
 78  ((void)__t);
 79  // FIXME: Make this abort
 80#endif
 81}
 82
 83template <class _From, class _To>
 84struct __can_dynamic_cast
 85    : _BoolConstant< is_polymorphic<_From>::value &&
 86                     (!is_base_of<_To, _From>::value || is_convertible<const _From*, const _To*>::value)> {};
 87
 88template <class _Ep, __enable_if_t< __can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
 89inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep& __e) {
 90  const nested_exception* __nep = dynamic_cast<const nested_exception*>(std::addressof(__e));
 91  if (__nep)
 92    __nep->rethrow_nested();
 93}
 94
 95template <class _Ep, __enable_if_t<!__can_dynamic_cast<_Ep, nested_exception>::value, int> = 0>
 96inline _LIBCPP_HIDE_FROM_ABI void rethrow_if_nested(const _Ep&) {}
 97
 98_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
 99
100#endif // _LIBCPP___EXCEPTION_NESTED_EXCEPTION_H