exception 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // -*- C++ -*-
  2. //===-------------------------- exception ---------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===----------------------------------------------------------------------===//
  10. #ifndef _LIBCPP_EXCEPTION
  11. #define _LIBCPP_EXCEPTION
  12. /*
  13. exception synopsis
  14. namespace std
  15. {
  16. class exception
  17. {
  18. public:
  19. exception() noexcept;
  20. exception(const exception&) noexcept;
  21. exception& operator=(const exception&) noexcept;
  22. virtual ~exception() noexcept;
  23. virtual const char* what() const noexcept;
  24. };
  25. class bad_exception
  26. : public exception
  27. {
  28. public:
  29. bad_exception() noexcept;
  30. bad_exception(const bad_exception&) noexcept;
  31. bad_exception& operator=(const bad_exception&) noexcept;
  32. virtual ~bad_exception() noexcept;
  33. virtual const char* what() const noexcept;
  34. };
  35. typedef void (*unexpected_handler)();
  36. unexpected_handler set_unexpected(unexpected_handler f ) noexcept;
  37. unexpected_handler get_unexpected() noexcept;
  38. [[noreturn]] void unexpected();
  39. typedef void (*terminate_handler)();
  40. terminate_handler set_terminate(terminate_handler f ) noexcept;
  41. terminate_handler get_terminate() noexcept;
  42. [[noreturn]] void terminate() noexcept;
  43. bool uncaught_exception() noexcept;
  44. int uncaught_exceptions() noexcept; // C++17
  45. typedef unspecified exception_ptr;
  46. exception_ptr current_exception() noexcept;
  47. void rethrow_exception [[noreturn]] (exception_ptr p);
  48. template<class E> exception_ptr make_exception_ptr(E e) noexcept;
  49. class nested_exception
  50. {
  51. public:
  52. nested_exception() noexcept;
  53. nested_exception(const nested_exception&) noexcept = default;
  54. nested_exception& operator=(const nested_exception&) noexcept = default;
  55. virtual ~nested_exception() = default;
  56. // access functions
  57. [[noreturn]] void rethrow_nested() const;
  58. exception_ptr nested_ptr() const noexcept;
  59. };
  60. template <class T> [[noreturn]] void throw_with_nested(T&& t);
  61. template <class E> void rethrow_if_nested(const E& e);
  62. } // std
  63. */
  64. #include <__config>
  65. #if defined(_LIBCPP_SGX_CONFIG)
  66. #include <../stdc++/exception>
  67. #else // !defined(_LIBCPP_SGX_CONFIG)
  68. #include <cstddef>
  69. #include <type_traits>
  70. #if defined(_LIBCPP_NO_EXCEPTIONS)
  71. #include <cstdio>
  72. #include <cstdlib>
  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. class _LIBCPP_EXCEPTION_ABI exception
  80. {
  81. public:
  82. _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
  83. virtual ~exception() _NOEXCEPT;
  84. virtual const char* what() const _NOEXCEPT;
  85. };
  86. class _LIBCPP_EXCEPTION_ABI bad_exception
  87. : public exception
  88. {
  89. public:
  90. _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
  91. virtual ~bad_exception() _NOEXCEPT;
  92. virtual const char* what() const _NOEXCEPT;
  93. };
  94. typedef void (*unexpected_handler)();
  95. _LIBCPP_FUNC_VIS unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
  96. _LIBCPP_FUNC_VIS unexpected_handler get_unexpected() _NOEXCEPT;
  97. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void unexpected();
  98. typedef void (*terminate_handler)();
  99. _LIBCPP_FUNC_VIS terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
  100. _LIBCPP_FUNC_VIS terminate_handler get_terminate() _NOEXCEPT;
  101. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void terminate() _NOEXCEPT;
  102. _LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT;
  103. _LIBCPP_FUNC_VIS int uncaught_exceptions() _NOEXCEPT;
  104. class _LIBCPP_TYPE_VIS exception_ptr;
  105. _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
  106. _LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
  107. class _LIBCPP_TYPE_VIS exception_ptr
  108. {
  109. void* __ptr_;
  110. public:
  111. _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
  112. _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
  113. exception_ptr(const exception_ptr&) _NOEXCEPT;
  114. exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
  115. ~exception_ptr() _NOEXCEPT;
  116. _LIBCPP_INLINE_VISIBILITY
  117. _LIBCPP_EXPLICIT
  118. operator bool() const _NOEXCEPT {return __ptr_ != nullptr;}
  119. friend _LIBCPP_INLINE_VISIBILITY
  120. bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
  121. {return __x.__ptr_ == __y.__ptr_;}
  122. friend _LIBCPP_INLINE_VISIBILITY
  123. bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
  124. {return !(__x == __y);}
  125. friend _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
  126. friend _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
  127. };
  128. template<class _Ep>
  129. exception_ptr
  130. make_exception_ptr(_Ep __e) _NOEXCEPT
  131. {
  132. #ifndef _LIBCPP_NO_EXCEPTIONS
  133. try
  134. {
  135. throw __e;
  136. }
  137. catch (...)
  138. {
  139. return current_exception();
  140. }
  141. #endif // _LIBCPP_NO_EXCEPTIONS
  142. }
  143. // nested_exception
  144. class _LIBCPP_EXCEPTION_ABI nested_exception
  145. {
  146. exception_ptr __ptr_;
  147. public:
  148. nested_exception() _NOEXCEPT;
  149. // nested_exception(const nested_exception&) noexcept = default;
  150. // nested_exception& operator=(const nested_exception&) noexcept = default;
  151. virtual ~nested_exception() _NOEXCEPT;
  152. // access functions
  153. _LIBCPP_NORETURN void rethrow_nested() const;
  154. _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
  155. };
  156. template <class _Tp>
  157. struct __nested
  158. : public _Tp,
  159. public nested_exception
  160. {
  161. _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
  162. };
  163. template <class _Tp>
  164. _LIBCPP_NORETURN
  165. void
  166. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  167. throw_with_nested(_Tp&& __t, typename enable_if<
  168. is_class<typename remove_reference<_Tp>::type>::value &&
  169. !is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
  170. && !__libcpp_is_final<typename remove_reference<_Tp>::type>::value
  171. >::type* = 0)
  172. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  173. throw_with_nested (_Tp& __t, typename enable_if<
  174. is_class<_Tp>::value && !is_base_of<nested_exception, _Tp>::value
  175. >::type* = 0)
  176. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  177. {
  178. #ifndef _LIBCPP_NO_EXCEPTIONS
  179. throw __nested<typename remove_reference<_Tp>::type>(_VSTD::forward<_Tp>(__t));
  180. #endif
  181. }
  182. template <class _Tp>
  183. _LIBCPP_NORETURN
  184. void
  185. #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
  186. throw_with_nested(_Tp&& __t, typename enable_if<
  187. !is_class<typename remove_reference<_Tp>::type>::value ||
  188. is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
  189. || __libcpp_is_final<typename remove_reference<_Tp>::type>::value
  190. >::type* = 0)
  191. #else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  192. throw_with_nested (_Tp& __t, typename enable_if<
  193. !is_class<_Tp>::value || is_base_of<nested_exception, _Tp>::value
  194. >::type* = 0)
  195. #endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
  196. {
  197. #ifndef _LIBCPP_NO_EXCEPTIONS
  198. throw _VSTD::forward<_Tp>(__t);
  199. #endif
  200. }
  201. template <class _Ep>
  202. inline _LIBCPP_INLINE_VISIBILITY
  203. void
  204. rethrow_if_nested(const _Ep& __e, typename enable_if<
  205. is_polymorphic<_Ep>::value
  206. >::type* = 0)
  207. {
  208. const nested_exception* __nep = dynamic_cast<const nested_exception*>(_VSTD::addressof(__e));
  209. if (__nep)
  210. __nep->rethrow_nested();
  211. }
  212. template <class _Ep>
  213. inline _LIBCPP_INLINE_VISIBILITY
  214. void
  215. rethrow_if_nested(const _Ep&, typename enable_if<
  216. !is_polymorphic<_Ep>::value
  217. >::type* = 0)
  218. {
  219. }
  220. } // std
  221. _LIBCPP_BEGIN_NAMESPACE_STD
  222. template <class _Exception>
  223. _LIBCPP_INLINE_VISIBILITY
  224. inline void __libcpp_throw(_Exception const& __e) {
  225. #ifndef _LIBCPP_NO_EXCEPTIONS
  226. throw __e;
  227. #else
  228. _VSTD::fprintf(stderr, "%s\n", __e.what());
  229. _VSTD::abort();
  230. #endif
  231. }
  232. _LIBCPP_END_NAMESPACE_STD
  233. #endif // defined(_LIBCPP_SGX_CONFIG)
  234. #endif // _LIBCPP_EXCEPTION