146035553Spatrick// -*- C++ -*-
246035553Spatrick//===----------------------------------------------------------------------===//
346035553Spatrick//
446035553Spatrick// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
546035553Spatrick// See https://llvm.org/LICENSE.txt for license information.
646035553Spatrick// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
746035553Spatrick//
846035553Spatrick//===----------------------------------------------------------------------===//
946035553Spatrick
1046035553Spatrick#include <stdio.h>
1146035553Spatrick#include <stdlib.h>
1246035553Spatrick
1346035553Spatrick_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCreate(void*);
1446035553Spatrick_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrDestroy(void*);
1546035553Spatrick_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCopy(void*, const void*);
1646035553Spatrick_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrAssign(void*, const void*);
1746035553Spatrick_LIBCPP_CRT_FUNC bool __cdecl __ExceptionPtrCompare(const void*, const void*);
1846035553Spatrick_LIBCPP_CRT_FUNC bool __cdecl __ExceptionPtrToBool(const void*);
1946035553Spatrick_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrSwap(void*, void*);
2046035553Spatrick_LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrCurrentException(void*);
2146035553Spatrick[[noreturn]] _LIBCPP_CRT_FUNC void __cdecl __ExceptionPtrRethrow(const void*);
2246035553Spatrick_LIBCPP_CRT_FUNC void __cdecl
2346035553Spatrick__ExceptionPtrCopyException(void*, const void*, const void*);
2446035553Spatrick
2546035553Spatricknamespace std {
2646035553Spatrick
27*76d0caaeSpatrickexception_ptr::exception_ptr() noexcept { __ExceptionPtrCreate(this); }
28*76d0caaeSpatrickexception_ptr::exception_ptr(nullptr_t) noexcept { __ExceptionPtrCreate(this); }
2946035553Spatrick
30*76d0caaeSpatrickexception_ptr::exception_ptr(const exception_ptr& __other) noexcept {
3146035553Spatrick  __ExceptionPtrCopy(this, &__other);
3246035553Spatrick}
33*76d0caaeSpatrickexception_ptr& exception_ptr::operator=(const exception_ptr& __other) noexcept {
3446035553Spatrick  __ExceptionPtrAssign(this, &__other);
3546035553Spatrick  return *this;
3646035553Spatrick}
3746035553Spatrick
38*76d0caaeSpatrickexception_ptr& exception_ptr::operator=(nullptr_t) noexcept {
3946035553Spatrick  exception_ptr dummy;
4046035553Spatrick  __ExceptionPtrAssign(this, &dummy);
4146035553Spatrick  return *this;
4246035553Spatrick}
4346035553Spatrick
44*76d0caaeSpatrickexception_ptr::~exception_ptr() noexcept { __ExceptionPtrDestroy(this); }
4546035553Spatrick
46*76d0caaeSpatrickexception_ptr::operator bool() const noexcept {
4746035553Spatrick  return __ExceptionPtrToBool(this);
4846035553Spatrick}
4946035553Spatrick
50*76d0caaeSpatrickbool operator==(const exception_ptr& __x, const exception_ptr& __y) noexcept {
5146035553Spatrick  return __ExceptionPtrCompare(&__x, &__y);
5246035553Spatrick}
5346035553Spatrick
5446035553Spatrick
55*76d0caaeSpatrickvoid swap(exception_ptr& lhs, exception_ptr& rhs) noexcept {
5646035553Spatrick  __ExceptionPtrSwap(&rhs, &lhs);
5746035553Spatrick}
5846035553Spatrick
5946035553Spatrickexception_ptr __copy_exception_ptr(void* __except, const void* __ptr) {
6046035553Spatrick  exception_ptr __ret = nullptr;
6146035553Spatrick  if (__ptr)
6246035553Spatrick    __ExceptionPtrCopyException(&__ret, __except, __ptr);
6346035553Spatrick  return __ret;
6446035553Spatrick}
6546035553Spatrick
66*76d0caaeSpatrickexception_ptr current_exception() noexcept {
6746035553Spatrick  exception_ptr __ret;
6846035553Spatrick  __ExceptionPtrCurrentException(&__ret);
6946035553Spatrick  return __ret;
7046035553Spatrick}
7146035553Spatrick
7246035553Spatrick_LIBCPP_NORETURN
7346035553Spatrickvoid rethrow_exception(exception_ptr p) { __ExceptionPtrRethrow(&p); }
7446035553Spatrick
75*76d0caaeSpatricknested_exception::nested_exception() noexcept : __ptr_(current_exception()) {}
7646035553Spatrick
77*76d0caaeSpatricknested_exception::~nested_exception() noexcept {}
7846035553Spatrick
7946035553Spatrick_LIBCPP_NORETURN
8046035553Spatrickvoid nested_exception::rethrow_nested() const {
8146035553Spatrick  if (__ptr_ == nullptr)
8246035553Spatrick    terminate();
8346035553Spatrick  rethrow_exception(__ptr_);
8446035553Spatrick}
8546035553Spatrick
8646035553Spatrick} // namespace std
87