blob: 51a48c8286d64fff58785b6bceac82d5a5496a51 [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
Howard Hinnant08e17472011-10-17 20:05:10 +000083#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +000085#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086
87namespace std // purposefully not using versioning namespace
88{
89
90class _LIBCPP_EXCEPTION_ABI exception
91{
92public:
Howard Hinnanted569212011-05-26 18:23:59 +000093 _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
94 virtual ~exception() _NOEXCEPT;
95 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096};
97
98class _LIBCPP_EXCEPTION_ABI bad_exception
99 : public exception
100{
101public:
Howard Hinnanted569212011-05-26 18:23:59 +0000102 _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
103 virtual ~bad_exception() _NOEXCEPT;
104 virtual const char* what() const _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000105};
106
107typedef void (*unexpected_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +0000108_LIBCPP_VISIBLE unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
109_LIBCPP_VISIBLE unexpected_handler get_unexpected() _NOEXCEPT;
Richard Smith0405cc42012-07-26 02:04:22 +0000110_LIBCPP_NORETURN _LIBCPP_VISIBLE void unexpected();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000111
112typedef void (*terminate_handler)();
Howard Hinnanted569212011-05-26 18:23:59 +0000113_LIBCPP_VISIBLE terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
114_LIBCPP_VISIBLE terminate_handler get_terminate() _NOEXCEPT;
Richard Smith0405cc42012-07-26 02:04:22 +0000115_LIBCPP_NORETURN _LIBCPP_VISIBLE void terminate() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000116
Howard Hinnanted569212011-05-26 18:23:59 +0000117_LIBCPP_VISIBLE bool uncaught_exception() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000118
Howard Hinnant33be35e2012-09-14 00:39:16 +0000119class _LIBCPP_VISIBLE exception_ptr;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000120
Howard Hinnanted569212011-05-26 18:23:59 +0000121exception_ptr current_exception() _NOEXCEPT;
Richard Smith0405cc42012-07-26 02:04:22 +0000122_LIBCPP_NORETURN void rethrow_exception(exception_ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123
Howard Hinnant422a53f2010-09-21 21:28:23 +0000124class _LIBCPP_VISIBLE exception_ptr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125{
126 void* __ptr_;
127public:
Howard Hinnanted569212011-05-26 18:23:59 +0000128 _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
129 _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
130 exception_ptr(const exception_ptr&) _NOEXCEPT;
131 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
132 ~exception_ptr() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133
Howard Hinnant422a53f2010-09-21 21:28:23 +0000134 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant77861882012-02-21 21:46:43 +0000135 _LIBCPP_EXPLICIT
Howard Hinnanted569212011-05-26 18:23:59 +0000136 operator bool() const _NOEXCEPT {return __ptr_ != nullptr;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000137
Howard Hinnant422a53f2010-09-21 21:28:23 +0000138 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted569212011-05-26 18:23:59 +0000139 bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000140 {return __x.__ptr_ == __y.__ptr_;}
Howard Hinnant422a53f2010-09-21 21:28:23 +0000141 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted569212011-05-26 18:23:59 +0000142 bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000143 {return !(__x == __y);}
144
Howard Hinnanted569212011-05-26 18:23:59 +0000145 friend exception_ptr current_exception() _NOEXCEPT;
Richard Smith53008d82012-11-29 04:30:50 +0000146 friend void rethrow_exception(exception_ptr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000147};
148
Howard Hinnant99968442011-11-29 18:15:50 +0000149template<class _Ep>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000150exception_ptr
Howard Hinnant99968442011-11-29 18:15:50 +0000151make_exception_ptr(_Ep __e) _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152{
Howard Hinnantd4444702010-08-11 17:04:31 +0000153#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000154 try
155 {
156 throw __e;
157 }
158 catch (...)
159 {
160 return current_exception();
161 }
Howard Hinnant324bb032010-08-22 00:02:43 +0000162#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163}
164
Howard Hinnanted2c2912010-05-27 17:06:52 +0000165// nested_exception
166
167class _LIBCPP_EXCEPTION_ABI nested_exception
168{
169 exception_ptr __ptr_;
170public:
Howard Hinnanted569212011-05-26 18:23:59 +0000171 nested_exception() _NOEXCEPT;
172// nested_exception(const nested_exception&) noexcept = default;
173// nested_exception& operator=(const nested_exception&) noexcept = default;
174 virtual ~nested_exception() _NOEXCEPT;
Howard Hinnanted2c2912010-05-27 17:06:52 +0000175
176 // access functions
Richard Smith0405cc42012-07-26 02:04:22 +0000177 _LIBCPP_NORETURN void rethrow_nested() const;
Howard Hinnanted569212011-05-26 18:23:59 +0000178 _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
Howard Hinnanted2c2912010-05-27 17:06:52 +0000179};
180
181template <class _Tp>
182struct __nested
183 : public _Tp,
184 public nested_exception
185{
Howard Hinnant422a53f2010-09-21 21:28:23 +0000186 _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
Howard Hinnanted2c2912010-05-27 17:06:52 +0000187};
188
189template <class _Tp>
Richard Smith0405cc42012-07-26 02:04:22 +0000190_LIBCPP_NORETURN
Howard Hinnant324bb032010-08-22 00:02:43 +0000191void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000192#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000193throw_with_nested(_Tp&& __t, typename enable_if<
Howard Hinnanted2c2912010-05-27 17:06:52 +0000194 is_class<typename remove_reference<_Tp>::type>::value &&
195 !is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
196 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000197#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000198throw_with_nested (_Tp& __t, typename enable_if<
199 is_class<_Tp>::value && !is_base_of<nested_exception, _Tp>::value
200 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000201#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000202{
Howard Hinnantd4444702010-08-11 17:04:31 +0000203#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +0000204 throw __nested<typename remove_reference<_Tp>::type>(_VSTD::forward<_Tp>(__t));
Howard Hinnantd4444702010-08-11 17:04:31 +0000205#endif
Howard Hinnanted2c2912010-05-27 17:06:52 +0000206}
207
208template <class _Tp>
Richard Smith0405cc42012-07-26 02:04:22 +0000209_LIBCPP_NORETURN
Howard Hinnant324bb032010-08-22 00:02:43 +0000210void
Howard Hinnant73d21a42010-09-04 23:28:19 +0000211#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnant4b7a43d2011-05-26 17:07:32 +0000212throw_with_nested(_Tp&& __t, typename enable_if<
Howard Hinnanted2c2912010-05-27 17:06:52 +0000213 !is_class<typename remove_reference<_Tp>::type>::value ||
214 is_base_of<nested_exception, typename remove_reference<_Tp>::type>::value
215 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000216#else // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000217throw_with_nested (_Tp& __t, typename enable_if<
218 !is_class<_Tp>::value || is_base_of<nested_exception, _Tp>::value
219 >::type* = 0)
Howard Hinnant73d21a42010-09-04 23:28:19 +0000220#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnanted2c2912010-05-27 17:06:52 +0000221{
Howard Hinnantd4444702010-08-11 17:04:31 +0000222#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant0949eed2011-06-30 21:18:19 +0000223 throw _VSTD::forward<_Tp>(__t);
Howard Hinnantd4444702010-08-11 17:04:31 +0000224#endif
Howard Hinnanted2c2912010-05-27 17:06:52 +0000225}
226
Howard Hinnant99968442011-11-29 18:15:50 +0000227template <class _Ep>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000228inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted2c2912010-05-27 17:06:52 +0000229void
Howard Hinnant99968442011-11-29 18:15:50 +0000230rethrow_if_nested(const _Ep& __e, typename enable_if<
231 is_polymorphic<_Ep>::value
Howard Hinnanted2c2912010-05-27 17:06:52 +0000232 >::type* = 0)
233{
Howard Hinnant6bb9f582010-05-28 13:35:41 +0000234 const nested_exception* __nep = dynamic_cast<const nested_exception*>(&__e);
235 if (__nep)
236 __nep->rethrow_nested();
Howard Hinnanted2c2912010-05-27 17:06:52 +0000237}
238
Howard Hinnant99968442011-11-29 18:15:50 +0000239template <class _Ep>
Howard Hinnant422a53f2010-09-21 21:28:23 +0000240inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnanted2c2912010-05-27 17:06:52 +0000241void
Howard Hinnantec3773c2011-12-01 20:21:04 +0000242rethrow_if_nested(const _Ep&, typename enable_if<
Howard Hinnant99968442011-11-29 18:15:50 +0000243 !is_polymorphic<_Ep>::value
Howard Hinnanted2c2912010-05-27 17:06:52 +0000244 >::type* = 0)
245{
246}
247
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248} // std
249
250#endif // _LIBCPP_EXCEPTION