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 HAVE_DEPENDENT_EH_ABI
11#error this header may only be used with libc++abi or libcxxrt
12#endif
13
14namespace std {
15
16exception_ptr::~exception_ptr() noexcept {
17  __cxa_decrement_exception_refcount(__ptr_);
18}
19
20exception_ptr::exception_ptr(const exception_ptr& other) noexcept
21    : __ptr_(other.__ptr_)
22{
23    __cxa_increment_exception_refcount(__ptr_);
24}
25
26exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept
27{
28    if (__ptr_ != other.__ptr_)
29    {
30        __cxa_increment_exception_refcount(other.__ptr_);
31        __cxa_decrement_exception_refcount(__ptr_);
32        __ptr_ = other.__ptr_;
33    }
34    return *this;
35}
36
37nested_exception::nested_exception() noexcept
38    : __ptr_(current_exception())
39{
40}
41
42nested_exception::~nested_exception() noexcept
43{
44}
45
46_LIBCPP_NORETURN
47void
48nested_exception::rethrow_nested() const
49{
50    if (__ptr_ == nullptr)
51        terminate();
52    rethrow_exception(__ptr_);
53}
54
55exception_ptr current_exception() noexcept
56{
57    // be nicer if there was a constructor that took a ptr, then
58    // this whole function would be just:
59    //    return exception_ptr(__cxa_current_primary_exception());
60    exception_ptr ptr;
61    ptr.__ptr_ = __cxa_current_primary_exception();
62    return ptr;
63}
64
65_LIBCPP_NORETURN
66void rethrow_exception(exception_ptr p)
67{
68    __cxa_rethrow_primary_exception(p.__ptr_);
69    // if p.__ptr_ is NULL, above returns so we terminate
70    terminate();
71}
72
73} // namespace std
74