exception 9.2 KB

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