blob: 00fc6b89aab7d505810233df182e7a844ab530e5 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- exception ---------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_EXCEPTION
12#define _LIBCPP_EXCEPTION
13
14/*
15 exception synopsis
16
17namespace std
18{
19
20class exception
21{
22public:
Howard Hinnanted569212011-05-26 18:23:59 +000023 exception() noexcept;
24 exception(const exception&) noexcept;
25 exception& operator=(const exception&) noexcept;
26 virtual ~exception() noexcept;
27 virtual const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000028};
29
30class bad_exception
31 : public exception
32{
33public:
Howard Hinnanted569212011-05-26 18:23:59 +000034 bad_exception() noexcept;
35 bad_exception(const bad_exception&) noexcept;
36 bad_exception& operator=(const bad_exception&) noexcept;
37 virtual ~bad_exception() noexcept;
38 virtual const char* what() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000039};
40
41typedef void (*unexpected_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +000042unexpected_handler set_unexpected(unexpected_handler f ) noexcept;
43unexpected_handler get_unexpected() noexcept;
44[[noreturn]] void unexpected();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045
46typedef void (*terminate_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +000047terminate_handler set_terminate(terminate_handler f ) noexcept;
48terminate_handler get_terminate() noexcept;
49[[noreturn]] void terminate() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000050
Howard Hinnanted569212011-05-26 18:23:59 +000051bool uncaught_exception() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052
53typedef unspecified exception_ptr;
54
Howard Hinnanted569212011-05-26 18:23:59 +000055exception_ptr current_exception() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056void rethrow_exception [[noreturn]] (exception_ptr p);
Howard Hinnanted569212011-05-26 18:23:59 +000057template<class E> exception_ptr make_exception_ptr(E e) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000058
59class nested_exception
60{
61public:
Howard Hinnanted569212011-05-26 18:23:59 +000062 nested_exception() noexcept;
63 nested_exception(const nested_exception&) noexcept = default;
64 nested_exception& operator=(const nested_exception&) noexcept = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000065 virtual ~nested_exception() = default;
66
67 // access functions
Howard Hinnanted569212011-05-26 18:23:59 +000068 [[noreturn]] void rethrow_nested() const;
69 exception_ptr nested_ptr() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000070};
71
Howard Hinnanted569212011-05-26 18:23:59 +000072template <class T> [[noreturn]] void throw_with_nested(T&& t);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000073template <class E> void rethrow_if_nested(const E& e);
74
75} // std
76
77*/
78
79#include <__config>
80#include <cstddef>
Howard Hinnanted2c2912010-05-27 17:06:52 +000081#include <type_traits>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000082
83#pragma GCC system_header
84
85namespace std // purposefully not using versioning namespace
86{
87
88class _LIBCPP_EXCEPTION_ABI exception
89{
90public:
Howard Hinnanted569212011-05-26 18:23:59 +000091 _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
92 virtual ~exception() _NOEXCEPT;
93 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000094};
95
96class _LIBCPP_EXCEPTION_ABI bad_exception
97 : public exception
98{
99public:
Howard Hinnanted569212011-05-26 18:23:59 +0000100 _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
101 virtual ~bad_exception() _NOEXCEPT;
102 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000103};
104
105typedef void (*unexpected_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +0000106_LIBCPP_VISIBLE unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
107_LIBCPP_VISIBLE unexpected_handler get_unexpected() _NOEXCEPT;
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000108_ATTRIBUTE(noreturn) _LIBCPP_VISIBLE void unexpected();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109
110typedef void (*terminate_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +0000111_LIBCPP_VISIBLE terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
112_LIBCPP_VISIBLE terminate_handler get_terminate() _NOEXCEPT;
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000113_ATTRIBUTE(noreturn) _LIBCPP_VISIBLE void terminate() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000114
Howard Hinnanted569212011-05-26 18:23:59 +0000115_LIBCPP_VISIBLE bool uncaught_exception() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116
117class exception_ptr;
118
Howard Hinnanted569212011-05-26 18:23:59 +0000119exception_ptr current_exception() _NOEXCEPT;
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000120_ATTRIBUTE(noreturn) void rethrow_exception(exception_ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000121
Howard Hinnant422a53f2010-09-21 21:28:23 +0000122class _LIBCPP_VISIBLE exception_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123{
124 void* __ptr_;
125public:
Howard Hinnanted569212011-05-26 18:23:59 +0000126 _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
127 _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
128 exception_ptr(const exception_ptr&) _NOEXCEPT;
129 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
130 ~exception_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131
Howard Hinnant422a53f2010-09-21 21:28:23 +0000132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 // explicit
Howard Hinnanted569212011-05-26 18:23:59 +0000134 operator bool() const _NOEXCEPT {return __ptr_ != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135
Howard Hinnant422a53f2010-09-21 21:28:23 +0000136 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted569212011-05-26 18:23:59 +0000137 bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000138 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant422a53f2010-09-21 21:28:23 +0000139 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted569212011-05-26 18:23:59 +0000140 bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141 {return !(__x == __y);}
142
Howard Hinnanted569212011-05-26 18:23:59 +0000143 friend exception_ptr current_exception() _NOEXCEPT;
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000144 _ATTRIBUTE(noreturn) friend void rethrow_exception(exception_ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000145};
146
147template<class _E>
148exception_ptr
Howard Hinnanted569212011-05-26 18:23:59 +0000149make_exception_ptr(_E __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150{
Howard Hinnantd4444702010-08-11 17:04:31 +0000151#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152 try
153 {
154 throw __e;
155 }
156 catch (...)
157 {
158 return current_exception();
159 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000160#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161}
162
Howard Hinnanted2c2912010-05-27 17:06:52 +0000163// nested_exception
164
165class _LIBCPP_EXCEPTION_ABI nested_exception
166{
167 exception_ptr __ptr_;
168public:
Howard Hinnanted569212011-05-26 18:23:59 +0000169 nested_exception() _NOEXCEPT;
170// nested_exception(const nested_exception&) noexcept = default;
171// nested_exception& operator=(const nested_exception&) noexcept = default;
172 virtual ~nested_exception() _NOEXCEPT;
Howard Hinnanted2c2912010-05-27 17:06:52 +0000173
174 // access functions
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000175 _ATTRIBUTE(noreturn) void rethrow_nested() const;
Howard Hinnanted569212011-05-26 18:23:59 +0000176 _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
Howard Hinnanted2c2912010-05-27 17:06:52 +0000177};
178
179template <class _Tp>
180struct __nested
181 : public _Tp,
182 public nested_exception
183{
Howard Hinnant422a53f2010-09-21 21:28:23 +0000184 _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
Howard Hinnanted2c2912010-05-27 17:06:52 +0000185};
186
187template <class _Tp>
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000188_ATTRIBUTE(noreturn)
Howard Hinnant324bb032010-08-22 00:02:43 +0000189void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000190#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000191throw_with_nested(_Tp&& __t, typename enable_if<
Howard Hinnanted2c2912010-05-27 17:06:52 +0000192 is_class<typename remove_reference<_Tp>::type>::value &&
193 !is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
194 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000195#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000196throw_with_nested (_Tp& __t, typename enable_if<
197 is_class<_Tp>::value && !is_base_of<nested_exception, _Tp>::value
198 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000199#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000200{
Howard Hinnantd4444702010-08-11 17:04:31 +0000201#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnanted2c2912010-05-27 17:06:52 +0000202 throw __nested<typename remove_reference<_Tp>::type>(_STD::forward<_Tp>(__t));
Howard Hinnantd4444702010-08-11 17:04:31 +0000203#endif
Howard Hinnanted2c2912010-05-27 17:06:52 +0000204}
205
206template <class _Tp>
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000207_ATTRIBUTE(noreturn)
Howard Hinnant324bb032010-08-22 00:02:43 +0000208void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000209#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000210throw_with_nested(_Tp&& __t, typename enable_if<
Howard Hinnanted2c2912010-05-27 17:06:52 +0000211 !is_class<typename remove_reference<_Tp>::type>::value ||
212 is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
213 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000214#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000215throw_with_nested (_Tp& __t, typename enable_if<
216 !is_class<_Tp>::value || is_base_of<nested_exception, _Tp>::value
217 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000218#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000219{
Howard Hinnantd4444702010-08-11 17:04:31 +0000220#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnanted2c2912010-05-27 17:06:52 +0000221 throw _STD::forward<_Tp>(__t);
Howard Hinnantd4444702010-08-11 17:04:31 +0000222#endif
Howard Hinnanted2c2912010-05-27 17:06:52 +0000223}
224
225template <class _E>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000226inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted2c2912010-05-27 17:06:52 +0000227void
228rethrow_if_nested(const _E& __e, typename enable_if<
Howard Hinnant6bb9f582010-05-28 13:35:41 +0000229 is_polymorphic<_E>::value
Howard Hinnanted2c2912010-05-27 17:06:52 +0000230 >::type* = 0)
231{
Howard Hinnant6bb9f582010-05-28 13:35:41 +0000232 const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
233 if (__nep)
234 __nep->rethrow_nested();
Howard Hinnanted2c2912010-05-27 17:06:52 +0000235}
236
237template <class _E>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000238inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted2c2912010-05-27 17:06:52 +0000239void
240rethrow_if_nested(const _E& __e, typename enable_if<
Howard Hinnant6bb9f582010-05-28 13:35:41 +0000241 !is_polymorphic<_E>::value
Howard Hinnanted2c2912010-05-27 17:06:52 +0000242 >::type* = 0)
243{
244}
245
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246} // std
247
248#endif // _LIBCPP_EXCEPTION