blob: 3772fe32bbafeb133aeade17b07ef932fc1f0b3d [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_EXCEPTION
11#define _LIBCPP_EXCEPTION
12
13/*
14 exception synopsis
15
16namespace std
17{
18
19class exception
20{
21public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000022 exception() noexcept;
23 exception(const exception&) noexcept;
24 exception& operator=(const exception&) noexcept;
25 virtual ~exception() noexcept;
26 virtual const char* what() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000027};
28
29class bad_exception
30 : public exception
31{
32public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000033 bad_exception() noexcept;
34 bad_exception(const bad_exception&) noexcept;
35 bad_exception& operator=(const bad_exception&) noexcept;
36 virtual ~bad_exception() noexcept;
37 virtual const char* what() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000038};
39
40typedef void (*unexpected_handler)();
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000041unexpected_handler set_unexpected(unexpected_handler f ) noexcept;
42unexpected_handler get_unexpected() noexcept;
43[[noreturn]] void unexpected();
Howard Hinnantc51e1022010-05-11 19:42:16 +000044
45typedef void (*terminate_handler)();
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000046terminate_handler set_terminate(terminate_handler f ) noexcept;
47terminate_handler get_terminate() noexcept;
48[[noreturn]] void terminate() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000049
Marshall Clow209dacc2015-06-02 15:33:38 +000050bool uncaught_exception() noexcept;
51int uncaught_exceptions() noexcept; // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
53typedef unspecified exception_ptr;
54
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000055exception_ptr current_exception() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000056void rethrow_exception [[noreturn]] (exception_ptr p);
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000057template<class E> exception_ptr make_exception_ptr(E e) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000058
59class nested_exception
60{
61public:
Howard Hinnant1bc52cf2011-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 Hinnantc51e1022010-05-11 19:42:16 +000065 virtual ~nested_exception() = default;
66
67 // access functions
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000068 [[noreturn]] void rethrow_nested() const;
69 exception_ptr nested_ptr() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000070};
71
Howard Hinnant1bc52cf2011-05-26 18:23:59 +000072template <class T> [[noreturn]] void throw_with_nested(T&& t);
Howard Hinnantc51e1022010-05-11 19:42:16 +000073template <class E> void rethrow_if_nested(const E& e);
74
75} // std
76
77*/
78
Louis Dionne73912b22020-11-04 15:01:25 -050079#include <__availability>
Arthur O'Dwyeref181602021-05-19 11:57:04 -040080#include <__config>
Louis Dionne735bc462021-04-14 13:59:03 -040081#include <__memory/addressof.h>
Howard Hinnantc51e1022010-05-11 19:42:16 +000082#include <cstddef>
Eric Fiselieraae1ccf2016-12-03 03:22:11 +000083#include <cstdlib>
Howard Hinnante4f92722010-05-27 17:06:52 +000084#include <type_traits>
Marshall Clow0a1e7502018-09-12 19:41:40 +000085#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +000086
Eric Fiselier85f66332019-03-05 01:57:01 +000087#if defined(_LIBCPP_ABI_VCRUNTIME)
Eric Fiselierec3a1672017-02-10 08:57:35 +000088#include <vcruntime_exception.h>
89#endif
90
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000091#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050092# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000093#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000094
95namespace std // purposefully not using versioning namespace
96{
97
Eric Fiselier85f66332019-03-05 01:57:01 +000098#if !defined(_LIBCPP_ABI_VCRUNTIME)
Howard Hinnantc51e1022010-05-11 19:42:16 +000099class _LIBCPP_EXCEPTION_ABI exception
100{
101public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000102 _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
Dimitry Andric47269ce2020-03-13 19:36:26 +0100103 _LIBCPP_INLINE_VISIBILITY exception(const exception&) _NOEXCEPT = default;
104
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000105 virtual ~exception() _NOEXCEPT;
106 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000107};
108
109class _LIBCPP_EXCEPTION_ABI bad_exception
110 : public exception
111{
112public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000113 _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
114 virtual ~bad_exception() _NOEXCEPT;
115 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116};
Eric Fiselier85f66332019-03-05 01:57:01 +0000117#endif // !_LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118
Eric Fiselierddd77792017-02-17 03:25:08 +0000119#if _LIBCPP_STD_VER <= 14 \
120 || defined(_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS) \
121 || defined(_LIBCPP_BUILDING_LIBRARY)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122typedef void (*unexpected_handler)();
Howard Hinnant8331b762013-03-06 23:30:19 +0000123_LIBCPP_FUNC_VIS unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
124_LIBCPP_FUNC_VIS unexpected_handler get_unexpected() _NOEXCEPT;
125_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void unexpected();
Eric Fiselierddd77792017-02-17 03:25:08 +0000126#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
128typedef void (*terminate_handler)();
Howard Hinnant8331b762013-03-06 23:30:19 +0000129_LIBCPP_FUNC_VIS terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
130_LIBCPP_FUNC_VIS terminate_handler get_terminate() _NOEXCEPT;
131_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void terminate() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132
Howard Hinnant8331b762013-03-06 23:30:19 +0000133_LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT;
Mehdi Amini228053d2017-05-04 17:08:54 +0000134_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_exceptions() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135
Howard Hinnant8331b762013-03-06 23:30:19 +0000136class _LIBCPP_TYPE_VIS exception_ptr;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000138_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
139_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000141#ifndef _LIBCPP_ABI_MICROSOFT
142
Howard Hinnant8331b762013-03-06 23:30:19 +0000143class _LIBCPP_TYPE_VIS exception_ptr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144{
145 void* __ptr_;
146public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000147 _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
148 _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000149
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000150 exception_ptr(const exception_ptr&) _NOEXCEPT;
151 exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
152 ~exception_ptr() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400154 _LIBCPP_INLINE_VISIBILITY explicit operator bool() const _NOEXCEPT
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000155 {return __ptr_ != nullptr;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000156
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000157 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000158 bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159 {return __x.__ptr_ == __y.__ptr_;}
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000160
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000161 friend _LIBCPP_INLINE_VISIBILITY
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000162 bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163 {return !(__x == __y);}
164
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000165 friend _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
166 friend _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167};
168
Howard Hinnantc834c512011-11-29 18:15:50 +0000169template<class _Ep>
Louis Dionnecdc41112018-11-21 17:00:52 +0000170_LIBCPP_INLINE_VISIBILITY exception_ptr
Howard Hinnantc834c512011-11-29 18:15:50 +0000171make_exception_ptr(_Ep __e) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172{
Howard Hinnant72f73582010-08-11 17:04:31 +0000173#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174 try
175 {
176 throw __e;
177 }
178 catch (...)
179 {
180 return current_exception();
181 }
Eric Fiselieraae1ccf2016-12-03 03:22:11 +0000182#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000183 ((void)__e);
Eric Fiselieraae1ccf2016-12-03 03:22:11 +0000184 _VSTD::abort();
185#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186}
187
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000188#else // _LIBCPP_ABI_MICROSOFT
189
190class _LIBCPP_TYPE_VIS exception_ptr
191{
192#if defined(__clang__)
193#pragma clang diagnostic push
194#pragma clang diagnostic ignored "-Wunused-private-field"
195#endif
196 void* __ptr1_;
197 void* __ptr2_;
198#if defined(__clang__)
199#pragma clang diagnostic pop
200#endif
201public:
202 exception_ptr() _NOEXCEPT;
203 exception_ptr(nullptr_t) _NOEXCEPT;
204 exception_ptr(const exception_ptr& __other) _NOEXCEPT;
205 exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT;
206 exception_ptr& operator=(nullptr_t) _NOEXCEPT;
207 ~exception_ptr() _NOEXCEPT;
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400208 explicit operator bool() const _NOEXCEPT;
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000209};
210
211_LIBCPP_FUNC_VIS
212bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT;
213
214inline _LIBCPP_INLINE_VISIBILITY
215bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
216 {return !(__x == __y);}
217
218_LIBCPP_FUNC_VIS void swap(exception_ptr&, exception_ptr&) _NOEXCEPT;
219
220_LIBCPP_FUNC_VIS exception_ptr __copy_exception_ptr(void *__except, const void* __ptr);
221_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
222_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr p);
223
224// This is a built-in template function which automagically extracts the required
225// information.
226template <class _E> void *__GetExceptionInfo(_E);
227
228template<class _Ep>
Louis Dionnecdc41112018-11-21 17:00:52 +0000229_LIBCPP_INLINE_VISIBILITY exception_ptr
Eric Fiselier94ef6cc2017-05-08 01:17:50 +0000230make_exception_ptr(_Ep __e) _NOEXCEPT
231{
232 return __copy_exception_ptr(_VSTD::addressof(__e), __GetExceptionInfo(__e));
233}
234
235#endif // _LIBCPP_ABI_MICROSOFT
Howard Hinnante4f92722010-05-27 17:06:52 +0000236// nested_exception
237
238class _LIBCPP_EXCEPTION_ABI nested_exception
239{
240 exception_ptr __ptr_;
241public:
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000242 nested_exception() _NOEXCEPT;
243// nested_exception(const nested_exception&) noexcept = default;
244// nested_exception& operator=(const nested_exception&) noexcept = default;
245 virtual ~nested_exception() _NOEXCEPT;
Howard Hinnante4f92722010-05-27 17:06:52 +0000246
247 // access functions
Richard Smithcabd3922012-07-26 02:04:22 +0000248 _LIBCPP_NORETURN void rethrow_nested() const;
Howard Hinnant1bc52cf2011-05-26 18:23:59 +0000249 _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
Howard Hinnante4f92722010-05-27 17:06:52 +0000250};
251
252template <class _Tp>
253struct __nested
254 : public _Tp,
255 public nested_exception
256{
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000257 _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
Howard Hinnante4f92722010-05-27 17:06:52 +0000258};
259
Howard Hinnant72f73582010-08-11 17:04:31 +0000260#ifndef _LIBCPP_NO_EXCEPTIONS
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000261template <class _Tp, class _Up, bool>
262struct __throw_with_nested;
263
264template <class _Tp, class _Up>
265struct __throw_with_nested<_Tp, _Up, true> {
Louis Dionne16fe2952018-07-11 23:14:33 +0000266 _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000267 __do_throw(_Tp&& __t)
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000268 {
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000269 throw __nested<_Up>(static_cast<_Tp&&>(__t));
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000270 }
271};
272
273template <class _Tp, class _Up>
274struct __throw_with_nested<_Tp, _Up, false> {
Louis Dionne16fe2952018-07-11 23:14:33 +0000275 _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
Eric Fiselier58e1c912017-04-19 01:35:58 +0000276#ifndef _LIBCPP_CXX03_LANG
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000277 __do_throw(_Tp&& __t)
Eric Fiselier58e1c912017-04-19 01:35:58 +0000278#else
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000279 __do_throw (_Tp& __t)
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400280#endif // _LIBCPP_CXX03_LANG
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000281 {
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000282 throw static_cast<_Tp&&>(__t);
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000283 }
284};
Howard Hinnant72f73582010-08-11 17:04:31 +0000285#endif
Howard Hinnante4f92722010-05-27 17:06:52 +0000286
287template <class _Tp>
Richard Smithcabd3922012-07-26 02:04:22 +0000288_LIBCPP_NORETURN
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000289void
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000290throw_with_nested(_Tp&& __t)
Howard Hinnante4f92722010-05-27 17:06:52 +0000291{
Howard Hinnant72f73582010-08-11 17:04:31 +0000292#ifndef _LIBCPP_NO_EXCEPTIONS
Marshall Clow94a38502017-04-13 16:57:42 +0000293 typedef typename decay<_Tp>::type _Up;
294 static_assert( is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
Marshall Clowb1cf5a92017-04-13 14:41:45 +0000295 __throw_with_nested<_Tp, _Up,
296 is_class<_Up>::value &&
297 !is_base_of<nested_exception, _Up>::value &&
298 !__libcpp_is_final<_Up>::value>::
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000299 __do_throw(static_cast<_Tp&&>(__t));
Eric Fiselier6003c772016-12-23 23:37:52 +0000300#else
301 ((void)__t);
302 // FIXME: Make this abort
Howard Hinnant72f73582010-08-11 17:04:31 +0000303#endif
Howard Hinnante4f92722010-05-27 17:06:52 +0000304}
305
Marshall Clowb75c89c2017-03-14 17:08:47 +0000306template <class _From, class _To>
307struct __can_dynamic_cast : public _LIBCPP_BOOL_CONSTANT(
308 is_polymorphic<_From>::value &&
309 (!is_base_of<_To, _From>::value ||
310 is_convertible<const _From*, const _To*>::value)) {};
311
Howard Hinnantc834c512011-11-29 18:15:50 +0000312template <class _Ep>
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000313inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante4f92722010-05-27 17:06:52 +0000314void
Marshall Clowb75c89c2017-03-14 17:08:47 +0000315rethrow_if_nested(const _Ep& __e,
316 typename enable_if< __can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
Howard Hinnante4f92722010-05-27 17:06:52 +0000317{
Marshall Clowe993abe2015-12-14 18:01:56 +0000318 const nested_exception* __nep = dynamic_cast<const nested_exception*>(_VSTD::addressof(__e));
Howard Hinnant3615a192010-05-28 13:35:41 +0000319 if (__nep)
320 __nep->rethrow_nested();
Howard Hinnante4f92722010-05-27 17:06:52 +0000321}
322
Howard Hinnantc834c512011-11-29 18:15:50 +0000323template <class _Ep>
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000324inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnante4f92722010-05-27 17:06:52 +0000325void
Marshall Clowb75c89c2017-03-14 17:08:47 +0000326rethrow_if_nested(const _Ep&,
327 typename enable_if<!__can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
Howard Hinnante4f92722010-05-27 17:06:52 +0000328{
329}
330
Nikolas Klauserd26407a2021-12-02 14:12:51 +0100331} // namespace std
Howard Hinnantc51e1022010-05-11 19:42:16 +0000332
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400333#endif // _LIBCPP_EXCEPTION