1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef _LIBCPP___NEW_EXCEPTIONS_H
10#define _LIBCPP___NEW_EXCEPTIONS_H
11
12#include <__config>
13#include <__exception/exception.h>
14#include <__verbose_abort>
15
16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17# pragma GCC system_header
18#endif
19
20// <vcruntime_exception.h> defines its own std::bad_alloc type,
21// which we use in order to be ABI-compatible with other STLs on Windows.
22#if defined(_LIBCPP_ABI_VCRUNTIME)
23# include <vcruntime_exception.h>
24#endif
25
26_LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD
27_LIBCPP_BEGIN_EXPLICIT_ABI_ANNOTATIONS
28
29#if !defined(_LIBCPP_ABI_VCRUNTIME)
30
31class _LIBCPP_EXPORTED_FROM_ABI bad_alloc : public exception {
32public:
33 bad_alloc() _NOEXCEPT;
34 _LIBCPP_HIDE_FROM_ABI bad_alloc(const bad_alloc&) _NOEXCEPT = default;
35 _LIBCPP_HIDE_FROM_ABI bad_alloc& operator=(const bad_alloc&) _NOEXCEPT = default;
36 ~bad_alloc() _NOEXCEPT override;
37 [[__nodiscard__]] const char* what() const _NOEXCEPT override;
38};
39
40class _LIBCPP_EXPORTED_FROM_ABI bad_array_new_length : public bad_alloc {
41public:
42 bad_array_new_length() _NOEXCEPT;
43 _LIBCPP_HIDE_FROM_ABI bad_array_new_length(const bad_array_new_length&) _NOEXCEPT = default;
44 _LIBCPP_HIDE_FROM_ABI bad_array_new_length& operator=(const bad_array_new_length&) _NOEXCEPT = default;
45 ~bad_array_new_length() _NOEXCEPT override;
46 [[__nodiscard__]] const char* what() const _NOEXCEPT override;
47};
48
49#elif defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0 // !_LIBCPP_ABI_VCRUNTIME
50
51// When _HAS_EXCEPTIONS == 0, these complete definitions are needed,
52// since they would normally be provided in vcruntime_exception.h
53class bad_alloc : public exception {
54public:
55 bad_alloc() noexcept : exception("bad allocation") {}
56
57private:
58 friend class bad_array_new_length;
59
60 bad_alloc(char const* const __message) noexcept : exception(__message) {}
61};
62
63class bad_array_new_length : public bad_alloc {
64public:
65 bad_array_new_length() noexcept : bad_alloc("bad array new length") {}
66};
67
68#endif // defined(_LIBCPP_ABI_VCRUNTIME) && defined(_HAS_EXCEPTIONS) && _HAS_EXCEPTIONS == 0
69
70[[__noreturn__]] _LIBCPP_EXPORTED_FROM_ABI void __throw_bad_alloc(); // not in C++ spec
71
72[[__noreturn__]] inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_array_new_length() {
73#if _LIBCPP_HAS_EXCEPTIONS
74 throw bad_array_new_length();
75#else
76 _LIBCPP_VERBOSE_ABORT("bad_array_new_length was thrown in -fno-exceptions mode");
77#endif
78}
79_LIBCPP_END_EXPLICIT_ABI_ANNOTATIONS
80_LIBCPP_END_UNVERSIONED_NAMESPACE_STD
81
82#endif // _LIBCPP___NEW_EXCEPTIONS_H
83