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 { __cxa_decrement_exception_refcount(__ptr_); }
17
18exception_ptr::exception_ptr(const exception_ptr& other) noexcept : __ptr_(other.__ptr_) {
19  __cxa_increment_exception_refcount(__ptr_);
20}
21
22exception_ptr& exception_ptr::operator=(const exception_ptr& other) noexcept {
23  if (__ptr_ != other.__ptr_) {
24    __cxa_increment_exception_refcount(other.__ptr_);
25    __cxa_decrement_exception_refcount(__ptr_);
26    __ptr_ = other.__ptr_;
27  }
28  return *this;
29}
30
31nested_exception::nested_exception() noexcept : __ptr_(current_exception()) {}
32
33nested_exception::~nested_exception() noexcept {}
34
35_LIBCPP_NORETURN void nested_exception::rethrow_nested() const {
36  if (__ptr_ == nullptr)
37    terminate();
38  rethrow_exception(__ptr_);
39}
40
41exception_ptr current_exception() noexcept {
42  // be nicer if there was a constructor that took a ptr, then
43  // this whole function would be just:
44  //    return exception_ptr(__cxa_current_primary_exception());
45  exception_ptr ptr;
46  ptr.__ptr_ = __cxa_current_primary_exception();
47  return ptr;
48}
49
50_LIBCPP_NORETURN void rethrow_exception(exception_ptr p) {
51  __cxa_rethrow_primary_exception(p.__ptr_);
52  // if p.__ptr_ is NULL, above returns so we terminate
53  terminate();
54}
55
56} // namespace std
57