xref: /freebsd/contrib/llvm-project/libcxx/include/exception (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// 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
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_EXCEPTION
11#define _LIBCPP_EXCEPTION
12
13/*
14    exception synopsis
15
16namespace std
17{
18
19class exception
20{
21public:
22    exception() noexcept;
23    exception(const exception&) noexcept;
24    exception& operator=(const exception&) noexcept;
25    virtual ~exception() noexcept;
26    virtual const char* what() const noexcept;
27};
28
29class bad_exception
30    : public exception
31{
32public:
33    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;
38};
39
40typedef void (*unexpected_handler)();
41unexpected_handler set_unexpected(unexpected_handler  f ) noexcept;
42unexpected_handler get_unexpected() noexcept;
43[[noreturn]] void unexpected();
44
45typedef void (*terminate_handler)();
46terminate_handler set_terminate(terminate_handler  f ) noexcept;
47terminate_handler get_terminate() noexcept;
48[[noreturn]] void terminate() noexcept;
49
50bool uncaught_exception()  noexcept;
51int  uncaught_exceptions() noexcept;  // C++17
52
53typedef unspecified exception_ptr;
54
55exception_ptr current_exception() noexcept;
56void rethrow_exception [[noreturn]] (exception_ptr p);
57template<class E> exception_ptr make_exception_ptr(E e) noexcept;
58
59class nested_exception
60{
61public:
62    nested_exception() noexcept;
63    nested_exception(const nested_exception&) noexcept = default;
64    nested_exception& operator=(const nested_exception&) noexcept = default;
65    virtual ~nested_exception() = default;
66
67    // access functions
68    [[noreturn]] void rethrow_nested() const;
69    exception_ptr nested_ptr() const noexcept;
70};
71
72template <class T> [[noreturn]] void throw_with_nested(T&& t);
73template <class E> void rethrow_if_nested(const E& e);
74
75}  // std
76
77*/
78
79#include <__availability>
80#include <__config>
81#include <__memory/addressof.h>
82#include <cstddef>
83#include <cstdlib>
84#include <type_traits>
85#include <version>
86
87#if defined(_LIBCPP_ABI_VCRUNTIME)
88#include <vcruntime_exception.h>
89#endif
90
91#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
92#pragma GCC system_header
93#endif
94
95namespace std  // purposefully not using versioning namespace
96{
97
98#if !defined(_LIBCPP_ABI_VCRUNTIME)
99class _LIBCPP_EXCEPTION_ABI exception
100{
101public:
102    _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
103    _LIBCPP_INLINE_VISIBILITY exception(const exception&) _NOEXCEPT = default;
104
105    virtual ~exception() _NOEXCEPT;
106    virtual const char* what() const _NOEXCEPT;
107};
108
109class _LIBCPP_EXCEPTION_ABI bad_exception
110    : public exception
111{
112public:
113    _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
114    virtual ~bad_exception() _NOEXCEPT;
115    virtual const char* what() const _NOEXCEPT;
116};
117#endif // !_LIBCPP_ABI_VCRUNTIME
118
119#if _LIBCPP_STD_VER <= 14 \
120    || defined(_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS) \
121    || defined(_LIBCPP_BUILDING_LIBRARY)
122typedef void (*unexpected_handler)();
123_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();
126#endif
127
128typedef void (*terminate_handler)();
129_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;
132
133_LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT;
134_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_exceptions() _NOEXCEPT;
135
136class _LIBCPP_TYPE_VIS exception_ptr;
137
138_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
139_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
140
141#ifndef _LIBCPP_ABI_MICROSOFT
142
143class _LIBCPP_TYPE_VIS exception_ptr
144{
145    void* __ptr_;
146public:
147    _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
148    _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
149
150    exception_ptr(const exception_ptr&) _NOEXCEPT;
151    exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
152    ~exception_ptr() _NOEXCEPT;
153
154    _LIBCPP_INLINE_VISIBILITY explicit operator bool() const _NOEXCEPT
155    {return __ptr_ != nullptr;}
156
157    friend _LIBCPP_INLINE_VISIBILITY
158    bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
159        {return __x.__ptr_ == __y.__ptr_;}
160
161    friend _LIBCPP_INLINE_VISIBILITY
162    bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
163        {return !(__x == __y);}
164
165    friend _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
166    friend _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
167};
168
169template<class _Ep>
170_LIBCPP_INLINE_VISIBILITY exception_ptr
171make_exception_ptr(_Ep __e) _NOEXCEPT
172{
173#ifndef _LIBCPP_NO_EXCEPTIONS
174    try
175    {
176        throw __e;
177    }
178    catch (...)
179    {
180        return current_exception();
181    }
182#else
183    ((void)__e);
184    _VSTD::abort();
185#endif
186}
187
188#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;
208    explicit operator bool() const _NOEXCEPT;
209};
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>
229_LIBCPP_INLINE_VISIBILITY exception_ptr
230make_exception_ptr(_Ep __e) _NOEXCEPT
231{
232  return __copy_exception_ptr(_VSTD::addressof(__e), __GetExceptionInfo(__e));
233}
234
235#endif // _LIBCPP_ABI_MICROSOFT
236// nested_exception
237
238class _LIBCPP_EXCEPTION_ABI nested_exception
239{
240    exception_ptr __ptr_;
241public:
242    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;
246
247    // access functions
248    _LIBCPP_NORETURN void rethrow_nested() const;
249    _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
250};
251
252template <class _Tp>
253struct __nested
254    : public _Tp,
255      public nested_exception
256{
257    _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
258};
259
260#ifndef _LIBCPP_NO_EXCEPTIONS
261template <class _Tp, class _Up, bool>
262struct __throw_with_nested;
263
264template <class _Tp, class _Up>
265struct __throw_with_nested<_Tp, _Up, true> {
266    _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
267    __do_throw(_Tp&& __t)
268    {
269        throw __nested<_Up>(static_cast<_Tp&&>(__t));
270    }
271};
272
273template <class _Tp, class _Up>
274struct __throw_with_nested<_Tp, _Up, false> {
275    _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
276#ifndef _LIBCPP_CXX03_LANG
277    __do_throw(_Tp&& __t)
278#else
279    __do_throw (_Tp& __t)
280#endif // _LIBCPP_CXX03_LANG
281    {
282        throw static_cast<_Tp&&>(__t);
283    }
284};
285#endif
286
287template <class _Tp>
288_LIBCPP_NORETURN
289void
290throw_with_nested(_Tp&& __t)
291{
292#ifndef _LIBCPP_NO_EXCEPTIONS
293    typedef typename decay<_Tp>::type _Up;
294    static_assert( is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
295    __throw_with_nested<_Tp, _Up,
296        is_class<_Up>::value &&
297        !is_base_of<nested_exception, _Up>::value &&
298        !__libcpp_is_final<_Up>::value>::
299            __do_throw(static_cast<_Tp&&>(__t));
300#else
301    ((void)__t);
302    // FIXME: Make this abort
303#endif
304}
305
306template <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
312template <class _Ep>
313inline _LIBCPP_INLINE_VISIBILITY
314void
315rethrow_if_nested(const _Ep& __e,
316                  typename enable_if< __can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
317{
318    const nested_exception* __nep = dynamic_cast<const nested_exception*>(_VSTD::addressof(__e));
319    if (__nep)
320        __nep->rethrow_nested();
321}
322
323template <class _Ep>
324inline _LIBCPP_INLINE_VISIBILITY
325void
326rethrow_if_nested(const _Ep&,
327                  typename enable_if<!__can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
328{
329}
330
331} // namespace std
332
333#endif // _LIBCPP_EXCEPTION
334