exception 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #ifndef _LIBCPP_EXCEPTION
  10. #define _LIBCPP_EXCEPTION
  11. /*
  12. exception synopsis
  13. namespace std
  14. {
  15. class exception
  16. {
  17. public:
  18. exception() noexcept;
  19. exception(const exception&) noexcept;
  20. exception& operator=(const exception&) noexcept;
  21. virtual ~exception() noexcept;
  22. virtual const char* what() const noexcept;
  23. };
  24. class bad_exception
  25. : public exception
  26. {
  27. public:
  28. bad_exception() noexcept;
  29. bad_exception(const bad_exception&) noexcept;
  30. bad_exception& operator=(const bad_exception&) noexcept;
  31. virtual ~bad_exception() noexcept;
  32. virtual const char* what() const noexcept;
  33. };
  34. typedef void (*unexpected_handler)();
  35. unexpected_handler set_unexpected(unexpected_handler f ) noexcept;
  36. unexpected_handler get_unexpected() noexcept;
  37. [[noreturn]] void unexpected();
  38. typedef void (*terminate_handler)();
  39. terminate_handler set_terminate(terminate_handler f ) noexcept;
  40. terminate_handler get_terminate() noexcept;
  41. [[noreturn]] void terminate() noexcept;
  42. bool uncaught_exception() noexcept;
  43. int uncaught_exceptions() noexcept; // C++17
  44. typedef unspecified exception_ptr;
  45. exception_ptr current_exception() noexcept;
  46. void rethrow_exception [[noreturn]] (exception_ptr p);
  47. template<class E> exception_ptr make_exception_ptr(E e) noexcept;
  48. class nested_exception
  49. {
  50. public:
  51. nested_exception() noexcept;
  52. nested_exception(const nested_exception&) noexcept = default;
  53. nested_exception& operator=(const nested_exception&) noexcept = default;
  54. virtual ~nested_exception() = default;
  55. // access functions
  56. [[noreturn]] void rethrow_nested() const;
  57. exception_ptr nested_ptr() const noexcept;
  58. };
  59. template <class T> [[noreturn]] void throw_with_nested(T&& t);
  60. template <class E> void rethrow_if_nested(const E& e);
  61. } // std
  62. */
  63. #include <__availability>
  64. #include <__config>
  65. #include <__memory/addressof.h>
  66. #include <cstddef>
  67. #include <cstdlib>
  68. #include <type_traits>
  69. #include <version>
  70. #if defined(_LIBCPP_ABI_VCRUNTIME)
  71. #include <vcruntime_exception.h>
  72. #endif
  73. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  74. # pragma GCC system_header
  75. #endif
  76. namespace std // purposefully not using versioning namespace
  77. {
  78. #if !defined(_LIBCPP_ABI_VCRUNTIME)
  79. class _LIBCPP_EXCEPTION_ABI exception
  80. {
  81. public:
  82. _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
  83. _LIBCPP_INLINE_VISIBILITY exception(const exception&) _NOEXCEPT = default;
  84. virtual ~exception() _NOEXCEPT;
  85. virtual const char* what() const _NOEXCEPT;
  86. };
  87. class _LIBCPP_EXCEPTION_ABI bad_exception
  88. : public exception
  89. {
  90. public:
  91. _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
  92. virtual ~bad_exception() _NOEXCEPT;
  93. virtual const char* what() const _NOEXCEPT;
  94. };
  95. #endif // !_LIBCPP_ABI_VCRUNTIME
  96. #if _LIBCPP_STD_VER <= 14 \
  97. || defined(_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS) \
  98. || defined(_LIBCPP_BUILDING_LIBRARY)
  99. typedef void (*unexpected_handler)();
  100. _LIBCPP_FUNC_VIS unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
  101. _LIBCPP_FUNC_VIS unexpected_handler get_unexpected() _NOEXCEPT;
  102. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void unexpected();
  103. #endif
  104. typedef void (*terminate_handler)();
  105. _LIBCPP_FUNC_VIS terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
  106. _LIBCPP_FUNC_VIS terminate_handler get_terminate() _NOEXCEPT;
  107. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void terminate() _NOEXCEPT;
  108. _LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT;
  109. _LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_exceptions() _NOEXCEPT;
  110. class _LIBCPP_TYPE_VIS exception_ptr;
  111. _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
  112. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
  113. class _LIBCPP_TYPE_VIS exception_ptr
  114. {
  115. void* __ptr_;
  116. public:
  117. _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
  118. _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
  119. exception_ptr(const exception_ptr&) _NOEXCEPT;
  120. exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
  121. ~exception_ptr() _NOEXCEPT;
  122. _LIBCPP_INLINE_VISIBILITY explicit operator bool() const _NOEXCEPT
  123. {return __ptr_ != nullptr;}
  124. friend _LIBCPP_INLINE_VISIBILITY
  125. bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
  126. {return __x.__ptr_ == __y.__ptr_;}
  127. friend _LIBCPP_INLINE_VISIBILITY
  128. bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
  129. {return !(__x == __y);}
  130. friend _LIBCPP_FUNC_VIS void swap(exception_ptr& __x, exception_ptr& __y) _NOEXCEPT
  131. {
  132. void* __tmp = __x.__ptr_;
  133. __x.__ptr_ = __y.__ptr_;
  134. __y.__ptr_ = __tmp;
  135. }
  136. friend _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
  137. friend _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
  138. };
  139. #ifndef _LIBCPP_ABI_MICROSOFT
  140. template<class _Ep>
  141. _LIBCPP_INLINE_VISIBILITY exception_ptr
  142. make_exception_ptr(_Ep __e) _NOEXCEPT
  143. {
  144. #ifndef _LIBCPP_NO_EXCEPTIONS
  145. try
  146. {
  147. throw __e;
  148. }
  149. catch (...)
  150. {
  151. return current_exception();
  152. }
  153. #else
  154. ((void)__e);
  155. _VSTD::abort();
  156. #endif
  157. }
  158. #else // _LIBCPP_ABI_MICROSOFT
  159. _LIBCPP_FUNC_VIS exception_ptr __copy_exception_ptr(void *__exception, const void* __ptr);
  160. // This is a built-in template function which automagically extracts the required
  161. // information.
  162. template <class _E> void *__GetExceptionInfo(_E);
  163. template<class _Ep>
  164. exception_ptr
  165. make_exception_ptr(_Ep __e) _NOEXCEPT
  166. {
  167. return __copy_exception_ptr(_VSTD::addressof(__e), __GetExceptionInfo(__e));
  168. }
  169. #endif // _LIBCPP_ABI_MICROSOFT
  170. // nested_exception
  171. class _LIBCPP_EXCEPTION_ABI nested_exception
  172. {
  173. exception_ptr __ptr_;
  174. public:
  175. nested_exception() _NOEXCEPT;
  176. // nested_exception(const nested_exception&) noexcept = default;
  177. // nested_exception& operator=(const nested_exception&) noexcept = default;
  178. virtual ~nested_exception() _NOEXCEPT;
  179. // access functions
  180. _LIBCPP_NORETURN void rethrow_nested() const;
  181. _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
  182. };
  183. template <class _Tp>
  184. struct __nested
  185. : public _Tp,
  186. public nested_exception
  187. {
  188. _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
  189. };
  190. #ifndef _LIBCPP_NO_EXCEPTIONS
  191. template <class _Tp, class _Up, bool>
  192. struct __throw_with_nested;
  193. template <class _Tp, class _Up>
  194. struct __throw_with_nested<_Tp, _Up, true> {
  195. _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
  196. __do_throw(_Tp&& __t)
  197. {
  198. throw __nested<_Up>(static_cast<_Tp&&>(__t));
  199. }
  200. };
  201. template <class _Tp, class _Up>
  202. struct __throw_with_nested<_Tp, _Up, false> {
  203. _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
  204. #ifndef _LIBCPP_CXX03_LANG
  205. __do_throw(_Tp&& __t)
  206. #else
  207. __do_throw (_Tp& __t)
  208. #endif // _LIBCPP_CXX03_LANG
  209. {
  210. throw static_cast<_Tp&&>(__t);
  211. }
  212. };
  213. #endif
  214. template <class _Tp>
  215. _LIBCPP_NORETURN
  216. void
  217. throw_with_nested(_Tp&& __t)
  218. {
  219. #ifndef _LIBCPP_NO_EXCEPTIONS
  220. typedef typename decay<_Tp>::type _Up;
  221. static_assert( is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
  222. __throw_with_nested<_Tp, _Up,
  223. is_class<_Up>::value &&
  224. !is_base_of<nested_exception, _Up>::value &&
  225. !__libcpp_is_final<_Up>::value>::
  226. __do_throw(static_cast<_Tp&&>(__t));
  227. #else
  228. ((void)__t);
  229. // FIXME: Make this abort
  230. #endif
  231. }
  232. template <class _From, class _To>
  233. struct __can_dynamic_cast : public _LIBCPP_BOOL_CONSTANT(
  234. is_polymorphic<_From>::value &&
  235. (!is_base_of<_To, _From>::value ||
  236. is_convertible<const _From*, const _To*>::value)) {};
  237. template <class _Ep>
  238. inline _LIBCPP_INLINE_VISIBILITY
  239. void
  240. rethrow_if_nested(const _Ep& __e,
  241. typename enable_if< __can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
  242. {
  243. const nested_exception* __nep = dynamic_cast<const nested_exception*>(_VSTD::addressof(__e));
  244. if (__nep)
  245. __nep->rethrow_nested();
  246. }
  247. template <class _Ep>
  248. inline _LIBCPP_INLINE_VISIBILITY
  249. void
  250. rethrow_if_nested(const _Ep&,
  251. typename enable_if<!__can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
  252. {
  253. }
  254. } // namespace std
  255. #endif // _LIBCPP_EXCEPTION