blob: 4871f41394acceb25d8b8f3a6a708c73c6d67c18 [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:
23 exception() throw();
24 exception(const exception&) throw();
25 exception& operator=(const exception&) throw();
26 virtual ~exception() throw();
27 virtual const char* what() const throw();
28};
29
30class bad_exception
31 : public exception
32{
33public:
34 bad_exception() throw();
35 bad_exception(const bad_exception&) throw();
36 bad_exception& operator=(const bad_exception&) throw();
37 virtual ~bad_exception() throw();
38 virtual const char* what() const throw();
39};
40
41typedef void (*unexpected_handler)();
42unexpected_handler set_unexpected(unexpected_handler f ) throw();
Howard Hinnanta4451512010-12-02 16:45:21 +000043unexpected_handler get_unexpected() throw();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000044void unexpected [[noreturn]] ();
45
46typedef void (*terminate_handler)();
47terminate_handler set_terminate(terminate_handler f ) throw();
Howard Hinnanta4451512010-12-02 16:45:21 +000048terminate_handler get_terminate() throw();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000049void terminate [[noreturn]] ();
50
51bool uncaught_exception() throw();
52
53typedef unspecified exception_ptr;
54
55exception_ptr current_exception();
56void rethrow_exception [[noreturn]] (exception_ptr p);
57template<class E> exception_ptr make_exception_ptr(E e);
58
59class nested_exception
60{
61public:
62 nested_exception() throw();
63 nested_exception(const nested_exception&) throw() = default;
64 nested_exception& operator=(const nested_exception&) throw() = default;
65 virtual ~nested_exception() = default;
66
67 // access functions
68 void rethrow_nested [[noreturn]] () const;
69 exception_ptr nested_ptr() const;
70};
71
72template <class T> void throw_with_nested [[noreturn]] (T&& t);
73template <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:
91 _LIBCPP_INLINE_VISIBILITY exception() throw() {}
92 virtual ~exception() throw();
93 virtual const char* what() const throw();
94};
95
96class _LIBCPP_EXCEPTION_ABI bad_exception
97 : public exception
98{
99public:
100 _LIBCPP_INLINE_VISIBILITY bad_exception() throw() {}
101 virtual ~bad_exception() throw();
102 virtual const char* what() const throw();
103};
104
105typedef void (*unexpected_handler)();
106_LIBCPP_VISIBLE unexpected_handler set_unexpected(unexpected_handler) throw();
Howard Hinnanta4451512010-12-02 16:45:21 +0000107_LIBCPP_VISIBLE unexpected_handler get_unexpected() throw();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000108_LIBCPP_VISIBLE void unexpected();
109
110typedef void (*terminate_handler)();
111_LIBCPP_VISIBLE terminate_handler set_terminate(terminate_handler) throw();
Howard Hinnanta4451512010-12-02 16:45:21 +0000112_LIBCPP_VISIBLE terminate_handler get_terminate() throw();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000113_LIBCPP_VISIBLE void terminate() __attribute__((__noreturn__));
114
115_LIBCPP_VISIBLE bool uncaught_exception() throw();
116
117class exception_ptr;
118
119exception_ptr current_exception();
120void rethrow_exception(exception_ptr); // noreturn
121
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 Hinnant422a53f2010-09-21 21:28:23 +0000126 _LIBCPP_INLINE_VISIBILITY exception_ptr() : __ptr_() {}
127 _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) : __ptr_() {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000128 exception_ptr(const exception_ptr&);
129 exception_ptr& operator=(const exception_ptr&);
130 ~exception_ptr();
131
Howard Hinnant422a53f2010-09-21 21:28:23 +0000132 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 // explicit
134 operator bool() const {return __ptr_ != nullptr;}
135
Howard Hinnant422a53f2010-09-21 21:28:23 +0000136 friend _LIBCPP_INLINE_VISIBILITY
137 bool operator==(const exception_ptr& __x, const exception_ptr& __y)
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
140 bool operator!=(const exception_ptr& __x, const exception_ptr& __y)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141 {return !(__x == __y);}
142
143 friend exception_ptr current_exception();
144 friend void rethrow_exception(exception_ptr); // noreturn
145};
146
147template<class _E>
148exception_ptr
149make_exception_ptr(_E __e)
150{
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:
169 nested_exception();
170// nested_exception(const nested_exception&) throw() = default;
171// nested_exception& operator=(const nested_exception&) throw() = default;
172 virtual ~nested_exception();
173
174 // access functions
175 void rethrow_nested /*[[noreturn]]*/ () const;
Howard Hinnant422a53f2010-09-21 21:28:23 +0000176 _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const {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 Hinnant324bb032010-08-22 00:02:43 +0000188void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000189#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000190throw_with_nested /*[[noreturn]]*/ (_Tp&& __t, typename enable_if<
191 is_class<typename remove_reference<_Tp>::type>::value &&
192 !is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
193 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000194#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000195throw_with_nested (_Tp& __t, typename enable_if<
196 is_class<_Tp>::value && !is_base_of<nested_exception, _Tp>::value
197 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000198#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000199{
Howard Hinnantd4444702010-08-11 17:04:31 +0000200#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnanted2c2912010-05-27 17:06:52 +0000201 throw __nested<typename remove_reference<_Tp>::type>(_STD::forward<_Tp>(__t));
Howard Hinnantd4444702010-08-11 17:04:31 +0000202#endif
Howard Hinnanted2c2912010-05-27 17:06:52 +0000203}
204
205template <class _Tp>
Howard Hinnant324bb032010-08-22 00:02:43 +0000206void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000207#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000208throw_with_nested /*[[noreturn]]*/ (_Tp&& __t, typename enable_if<
209 !is_class<typename remove_reference<_Tp>::type>::value ||
210 is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
211 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000212#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000213throw_with_nested (_Tp& __t, typename enable_if<
214 !is_class<_Tp>::value || is_base_of<nested_exception, _Tp>::value
215 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000216#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000217{
Howard Hinnantd4444702010-08-11 17:04:31 +0000218#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnanted2c2912010-05-27 17:06:52 +0000219 throw _STD::forward<_Tp>(__t);
Howard Hinnantd4444702010-08-11 17:04:31 +0000220#endif
Howard Hinnanted2c2912010-05-27 17:06:52 +0000221}
222
223template <class _E>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000224inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted2c2912010-05-27 17:06:52 +0000225void
226rethrow_if_nested(const _E& __e, typename enable_if<
Howard Hinnant6bb9f582010-05-28 13:35:41 +0000227 is_polymorphic<_E>::value
Howard Hinnanted2c2912010-05-27 17:06:52 +0000228 >::type* = 0)
229{
Howard Hinnant6bb9f582010-05-28 13:35:41 +0000230 const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
231 if (__nep)
232 __nep->rethrow_nested();
Howard Hinnanted2c2912010-05-27 17:06:52 +0000233}
234
235template <class _E>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000236inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted2c2912010-05-27 17:06:52 +0000237void
238rethrow_if_nested(const _E& __e, typename enable_if<
Howard Hinnant6bb9f582010-05-28 13:35:41 +0000239 !is_polymorphic<_E>::value
Howard Hinnanted2c2912010-05-27 17:06:52 +0000240 >::type* = 0)
241{
242}
243
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000244} // std
245
246#endif // _LIBCPP_EXCEPTION