1// -*- C++ -*-
2//===-------------------------- exception ---------------------------------===//
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 <__config>
80#include <__availability>
81#include <cstddef>
82#include <cstdlib>
83#include <type_traits>
84#include <version>
85
86#if defined(_LIBCPP_ABI_VCRUNTIME)
87#include <vcruntime_exception.h>
88#endif
89
90#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
91#pragma GCC system_header
92#endif
93
94namespace std  // purposefully not using versioning namespace
95{
96
97#if !defined(_LIBCPP_ABI_VCRUNTIME)
98class _LIBCPP_EXCEPTION_ABI exception
99{
100public:
101    _LIBCPP_INLINE_VISIBILITY exception() _NOEXCEPT {}
102    _LIBCPP_INLINE_VISIBILITY exception(const exception&) _NOEXCEPT = default;
103
104    virtual ~exception() _NOEXCEPT;
105    virtual const char* what() const _NOEXCEPT;
106};
107
108class _LIBCPP_EXCEPTION_ABI bad_exception
109    : public exception
110{
111public:
112    _LIBCPP_INLINE_VISIBILITY bad_exception() _NOEXCEPT {}
113    virtual ~bad_exception() _NOEXCEPT;
114    virtual const char* what() const _NOEXCEPT;
115};
116#endif // !_LIBCPP_ABI_VCRUNTIME
117
118#if _LIBCPP_STD_VER <= 14 \
119    || defined(_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS) \
120    || defined(_LIBCPP_BUILDING_LIBRARY)
121typedef void (*unexpected_handler)();
122_LIBCPP_FUNC_VIS unexpected_handler set_unexpected(unexpected_handler) _NOEXCEPT;
123_LIBCPP_FUNC_VIS unexpected_handler get_unexpected() _NOEXCEPT;
124_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void unexpected();
125#endif
126
127typedef void (*terminate_handler)();
128_LIBCPP_FUNC_VIS terminate_handler set_terminate(terminate_handler) _NOEXCEPT;
129_LIBCPP_FUNC_VIS terminate_handler get_terminate() _NOEXCEPT;
130_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void terminate() _NOEXCEPT;
131
132_LIBCPP_FUNC_VIS bool uncaught_exception() _NOEXCEPT;
133_LIBCPP_FUNC_VIS _LIBCPP_AVAILABILITY_UNCAUGHT_EXCEPTIONS int uncaught_exceptions() _NOEXCEPT;
134
135class _LIBCPP_TYPE_VIS exception_ptr;
136
137_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
138_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
139
140#ifndef _LIBCPP_ABI_MICROSOFT
141
142class _LIBCPP_TYPE_VIS exception_ptr
143{
144    void* __ptr_;
145public:
146    _LIBCPP_INLINE_VISIBILITY exception_ptr() _NOEXCEPT : __ptr_() {}
147    _LIBCPP_INLINE_VISIBILITY exception_ptr(nullptr_t) _NOEXCEPT : __ptr_() {}
148
149    exception_ptr(const exception_ptr&) _NOEXCEPT;
150    exception_ptr& operator=(const exception_ptr&) _NOEXCEPT;
151    ~exception_ptr() _NOEXCEPT;
152
153    _LIBCPP_INLINE_VISIBILITY _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT
154    {return __ptr_ != nullptr;}
155
156    friend _LIBCPP_INLINE_VISIBILITY
157    bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
158        {return __x.__ptr_ == __y.__ptr_;}
159
160    friend _LIBCPP_INLINE_VISIBILITY
161    bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
162        {return !(__x == __y);}
163
164    friend _LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
165    friend _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr);
166};
167
168template<class _Ep>
169_LIBCPP_INLINE_VISIBILITY exception_ptr
170make_exception_ptr(_Ep __e) _NOEXCEPT
171{
172#ifndef _LIBCPP_NO_EXCEPTIONS
173    try
174    {
175        throw __e;
176    }
177    catch (...)
178    {
179        return current_exception();
180    }
181#else
182    ((void)__e);
183    _VSTD::abort();
184#endif
185}
186
187#else // _LIBCPP_ABI_MICROSOFT
188
189class _LIBCPP_TYPE_VIS exception_ptr
190{
191#if defined(__clang__)
192#pragma clang diagnostic push
193#pragma clang diagnostic ignored "-Wunused-private-field"
194#endif
195    void* __ptr1_;
196    void* __ptr2_;
197#if defined(__clang__)
198#pragma clang diagnostic pop
199#endif
200public:
201    exception_ptr() _NOEXCEPT;
202    exception_ptr(nullptr_t) _NOEXCEPT;
203    exception_ptr(const exception_ptr& __other) _NOEXCEPT;
204    exception_ptr& operator=(const exception_ptr& __other) _NOEXCEPT;
205    exception_ptr& operator=(nullptr_t) _NOEXCEPT;
206    ~exception_ptr() _NOEXCEPT;
207    _LIBCPP_EXPLICIT operator bool() const _NOEXCEPT;
208};
209
210_LIBCPP_FUNC_VIS
211bool operator==(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT;
212
213inline _LIBCPP_INLINE_VISIBILITY
214bool operator!=(const exception_ptr& __x, const exception_ptr& __y) _NOEXCEPT
215    {return !(__x == __y);}
216
217_LIBCPP_FUNC_VIS void swap(exception_ptr&, exception_ptr&) _NOEXCEPT;
218
219_LIBCPP_FUNC_VIS exception_ptr __copy_exception_ptr(void *__except, const void* __ptr);
220_LIBCPP_FUNC_VIS exception_ptr current_exception() _NOEXCEPT;
221_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void rethrow_exception(exception_ptr p);
222
223// This is a built-in template function which automagically extracts the required
224// information.
225template <class _E> void *__GetExceptionInfo(_E);
226
227template<class _Ep>
228_LIBCPP_INLINE_VISIBILITY exception_ptr
229make_exception_ptr(_Ep __e) _NOEXCEPT
230{
231  return __copy_exception_ptr(_VSTD::addressof(__e), __GetExceptionInfo(__e));
232}
233
234#endif // _LIBCPP_ABI_MICROSOFT
235// nested_exception
236
237class _LIBCPP_EXCEPTION_ABI nested_exception
238{
239    exception_ptr __ptr_;
240public:
241    nested_exception() _NOEXCEPT;
242//     nested_exception(const nested_exception&) noexcept = default;
243//     nested_exception& operator=(const nested_exception&) noexcept = default;
244    virtual ~nested_exception() _NOEXCEPT;
245
246    // access functions
247    _LIBCPP_NORETURN void rethrow_nested() const;
248    _LIBCPP_INLINE_VISIBILITY exception_ptr nested_ptr() const _NOEXCEPT {return __ptr_;}
249};
250
251template <class _Tp>
252struct __nested
253    : public _Tp,
254      public nested_exception
255{
256    _LIBCPP_INLINE_VISIBILITY explicit __nested(const _Tp& __t) : _Tp(__t) {}
257};
258
259#ifndef _LIBCPP_NO_EXCEPTIONS
260template <class _Tp, class _Up, bool>
261struct __throw_with_nested;
262
263template <class _Tp, class _Up>
264struct __throw_with_nested<_Tp, _Up, true> {
265    _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
266    __do_throw(_Tp&& __t)
267    {
268        throw __nested<_Up>(_VSTD::forward<_Tp>(__t));
269    }
270};
271
272template <class _Tp, class _Up>
273struct __throw_with_nested<_Tp, _Up, false> {
274    _LIBCPP_NORETURN static inline _LIBCPP_INLINE_VISIBILITY void
275#ifndef _LIBCPP_CXX03_LANG
276    __do_throw(_Tp&& __t)
277#else
278    __do_throw (_Tp& __t)
279#endif  // _LIBCPP_CXX03_LANG
280    {
281        throw _VSTD::forward<_Tp>(__t);
282    }
283};
284#endif
285
286template <class _Tp>
287_LIBCPP_NORETURN
288void
289throw_with_nested(_Tp&& __t)
290{
291#ifndef _LIBCPP_NO_EXCEPTIONS
292    typedef typename decay<_Tp>::type _Up;
293    static_assert( is_copy_constructible<_Up>::value, "type thrown must be CopyConstructible");
294    __throw_with_nested<_Tp, _Up,
295        is_class<_Up>::value &&
296        !is_base_of<nested_exception, _Up>::value &&
297        !__libcpp_is_final<_Up>::value>::
298            __do_throw(_VSTD::forward<_Tp>(__t));
299#else
300    ((void)__t);
301    // FIXME: Make this abort
302#endif
303}
304
305template <class _From, class _To>
306struct __can_dynamic_cast : public _LIBCPP_BOOL_CONSTANT(
307              is_polymorphic<_From>::value &&
308                 (!is_base_of<_To, _From>::value ||
309                   is_convertible<const _From*, const _To*>::value)) {};
310
311template <class _Ep>
312inline _LIBCPP_INLINE_VISIBILITY
313void
314rethrow_if_nested(const _Ep& __e,
315                  typename enable_if< __can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
316{
317    const nested_exception* __nep = dynamic_cast<const nested_exception*>(_VSTD::addressof(__e));
318    if (__nep)
319        __nep->rethrow_nested();
320}
321
322template <class _Ep>
323inline _LIBCPP_INLINE_VISIBILITY
324void
325rethrow_if_nested(const _Ep&,
326                  typename enable_if<!__can_dynamic_cast<_Ep, nested_exception>::value>::type* = 0)
327{
328}
329
330}  // std
331
332#endif  // _LIBCPP_EXCEPTION
333