exception 8.5 KB

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