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