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