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 _LIBCPP_ABI_MICROSOFT
11#error this header can only be used when targeting the MSVC ABI
12#endif
13
14#include <stdio.h>
15#include <stdlib.h>
16
17extern "C" {
18typedef void (__cdecl* terminate_handler)();
19_LIBCPP_CRT_FUNC terminate_handler __cdecl set_terminate(
20    terminate_handler _NewTerminateHandler) throw();
21_LIBCPP_CRT_FUNC terminate_handler __cdecl _get_terminate();
22
23typedef void (__cdecl* unexpected_handler)();
24unexpected_handler __cdecl set_unexpected(
25    unexpected_handler _NewUnexpectedHandler) throw();
26unexpected_handler __cdecl _get_unexpected();
27
28int __cdecl __uncaught_exceptions();
29}
30
31namespace std {
32
33unexpected_handler
34set_unexpected(unexpected_handler func) _NOEXCEPT {
35  return ::set_unexpected(func);
36}
37
38unexpected_handler get_unexpected() _NOEXCEPT {
39  return ::_get_unexpected();
40}
41
42_LIBCPP_NORETURN
43void unexpected() {
44    (*get_unexpected())();
45    // unexpected handler should not return
46    terminate();
47}
48
49terminate_handler set_terminate(terminate_handler func) _NOEXCEPT {
50  return ::set_terminate(func);
51}
52
53terminate_handler get_terminate() _NOEXCEPT {
54  return ::_get_terminate();
55}
56
57_LIBCPP_NORETURN
58void terminate() _NOEXCEPT
59{
60#ifndef _LIBCPP_NO_EXCEPTIONS
61    try
62    {
63#endif  // _LIBCPP_NO_EXCEPTIONS
64        (*get_terminate())();
65        // handler should not return
66        fprintf(stderr, "terminate_handler unexpectedly returned\n");
67        ::abort();
68#ifndef _LIBCPP_NO_EXCEPTIONS
69    }
70    catch (...)
71    {
72        // handler should not throw exception
73        fprintf(stderr, "terminate_handler unexpectedly threw an exception\n");
74        ::abort();
75    }
76#endif  // _LIBCPP_NO_EXCEPTIONS
77}
78
79bool uncaught_exception() _NOEXCEPT { return uncaught_exceptions() > 0; }
80
81int uncaught_exceptions() _NOEXCEPT {
82    return __uncaught_exceptions();
83}
84
85#if !defined(_LIBCPP_ABI_VCRUNTIME)
86bad_cast::bad_cast() _NOEXCEPT
87{
88}
89
90bad_cast::~bad_cast() _NOEXCEPT
91{
92}
93
94const char *
95bad_cast::what() const _NOEXCEPT
96{
97  return "std::bad_cast";
98}
99
100bad_typeid::bad_typeid() _NOEXCEPT
101{
102}
103
104bad_typeid::~bad_typeid() _NOEXCEPT
105{
106}
107
108const char *
109bad_typeid::what() const _NOEXCEPT
110{
111  return "std::bad_typeid";
112}
113
114exception::~exception() _NOEXCEPT
115{
116}
117
118const char* exception::what() const _NOEXCEPT
119{
120  return "std::exception";
121}
122
123
124bad_exception::~bad_exception() _NOEXCEPT
125{
126}
127
128const char* bad_exception::what() const _NOEXCEPT
129{
130  return "std::bad_exception";
131}
132
133
134bad_alloc::bad_alloc() _NOEXCEPT
135{
136}
137
138bad_alloc::~bad_alloc() _NOEXCEPT
139{
140}
141
142const char*
143bad_alloc::what() const _NOEXCEPT
144{
145    return "std::bad_alloc";
146}
147
148bad_array_new_length::bad_array_new_length() _NOEXCEPT
149{
150}
151
152bad_array_new_length::~bad_array_new_length() _NOEXCEPT
153{
154}
155
156const char*
157bad_array_new_length::what() const _NOEXCEPT
158{
159    return "bad_array_new_length";
160}
161#endif // !_LIBCPP_ABI_VCRUNTIME
162
163} // namespace std
164