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// libsupc++ does not implement the dependent EH ABI and the functionality
11// it uses to implement std::exception_ptr (which it declares as an alias of
12// std::__exception_ptr::exception_ptr) is not directly exported to clients. So
13// we have little choice but to hijack std::__exception_ptr::exception_ptr's
14// (which fortunately has the same layout as our std::exception_ptr) copy
15// constructor, assignment operator and destructor (which are part of its
16// stable ABI), and its rethrow_exception(std::__exception_ptr::exception_ptr)
17// function.
18
19namespace std {
20
21namespace __exception_ptr {
22
23struct exception_ptr {
24 void* __ptr_;
25
26 explicit exception_ptr(void*) noexcept;
27 exception_ptr(const exception_ptr&) noexcept;
28 exception_ptr& operator=(const exception_ptr&) noexcept;
29 ~exception_ptr() noexcept;
30};
31
32} // namespace __exception_ptr
33
34_LIBCPP_NORETURN void rethrow_exception(__exception_ptr::exception_ptr);
35
36exception_ptr::~exception_ptr() noexcept { reinterpret_cast<__exception_ptr::exception_ptr*>(this)->~exception_ptr(); }
37
38exception_ptr::exception_ptr(const exception_ptr& other) noexcept : __ptr_(other.__ptr_) {
39 new (reinterpret_cast<void*>(this))
40 __exception_ptr::exception_ptr(reinterpret_cast<const __exception_ptr::exception_ptr&>(other));
41}
42
43exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept {
44 *reinterpret_cast<__exception_ptr::exception_ptr*>(this) =
45 reinterpret_cast<const __exception_ptr::exception_ptr&>(other);
46 return *this;
47}
48
49exception_ptr exception_ptr::__from_native_exception_pointer(void* __e) noexcept {
50 exception_ptr ptr{};
51 new (reinterpret_cast<void*>(&ptr)) __exception_ptr::exception_ptr(__e);
52
53 return ptr;
54}
55
56nested_exception::nested_exception() noexcept : __ptr_(current_exception()) {}
57
58_LIBCPP_NORETURN void nested_exception::rethrow_nested() const {
59 if (__ptr_ == nullptr)
60 terminate();
61 rethrow_exception(__ptr_);
62}
63
64_LIBCPP_NORETURN void rethrow_exception(exception_ptr p) {
65 rethrow_exception(reinterpret_cast<__exception_ptr::exception_ptr&>(p));
66}
67
68} // namespace std
69

source code of libcxx/src/support/runtime/exception_pointer_glibcxx.ipp