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_STDEXCEPT
11#define _LIBCPP_STDEXCEPT
12
13/*
14    stdexcept synopsis
15
16namespace std
17{
18
19class logic_error;
20    class domain_error;
21    class invalid_argument;
22    class length_error;
23    class out_of_range;
24class runtime_error;
25    class range_error;
26    class overflow_error;
27    class underflow_error;
28
29for each class xxx_error:
30
31class xxx_error : public exception // at least indirectly
32{
33public:
34    explicit xxx_error(const string& what_arg);
35    explicit xxx_error(const char*   what_arg);
36
37    virtual const char* what() const noexcept // returns what_arg
38};
39
40}  // std
41
42*/
43
44#include <__assert> // all public C++ headers provide the assertion handler
45#include <__config>
46#include <__exception/exception.h>
47#include <__fwd/string.h>
48#include <__verbose_abort>
49
50#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
51#  pragma GCC system_header
52#endif
53
54_LIBCPP_BEGIN_NAMESPACE_STD
55
56#ifndef _LIBCPP_ABI_VCRUNTIME
57class _LIBCPP_HIDDEN __libcpp_refstring {
58  const char* __imp_;
59
60  bool __uses_refcount() const;
61
62public:
63  explicit __libcpp_refstring(const char* __msg);
64  __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
65  __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
66  ~__libcpp_refstring();
67
68  _LIBCPP_HIDE_FROM_ABI const char* c_str() const _NOEXCEPT { return __imp_; }
69};
70#endif // !_LIBCPP_ABI_VCRUNTIME
71
72_LIBCPP_END_NAMESPACE_STD
73
74namespace std // purposefully not using versioning namespace
75{
76
77class _LIBCPP_EXPORTED_FROM_ABI logic_error : public exception {
78#ifndef _LIBCPP_ABI_VCRUNTIME
79
80private:
81  std::__libcpp_refstring __imp_;
82
83public:
84  explicit logic_error(const string&);
85  explicit logic_error(const char*);
86
87  logic_error(const logic_error&) _NOEXCEPT;
88  logic_error& operator=(const logic_error&) _NOEXCEPT;
89
90  ~logic_error() _NOEXCEPT override;
91
92  const char* what() const _NOEXCEPT override;
93#else
94
95public:
96  explicit logic_error(const std::string&); // Symbol uses versioned std::string
97  _LIBCPP_HIDE_FROM_ABI explicit logic_error(const char* __s) : exception(__s) {}
98#endif
99};
100
101class _LIBCPP_EXPORTED_FROM_ABI runtime_error : public exception {
102#ifndef _LIBCPP_ABI_VCRUNTIME
103
104private:
105  std::__libcpp_refstring __imp_;
106
107public:
108  explicit runtime_error(const string&);
109  explicit runtime_error(const char*);
110
111  runtime_error(const runtime_error&) _NOEXCEPT;
112  runtime_error& operator=(const runtime_error&) _NOEXCEPT;
113
114  ~runtime_error() _NOEXCEPT override;
115
116  const char* what() const _NOEXCEPT override;
117#else
118
119public:
120  explicit runtime_error(const std::string&); // Symbol uses versioned std::string
121  _LIBCPP_HIDE_FROM_ABI explicit runtime_error(const char* __s) : exception(__s) {}
122#endif // _LIBCPP_ABI_VCRUNTIME
123};
124
125class _LIBCPP_EXPORTED_FROM_ABI domain_error : public logic_error {
126public:
127  _LIBCPP_HIDE_FROM_ABI explicit domain_error(const string& __s) : logic_error(__s) {}
128  _LIBCPP_HIDE_FROM_ABI explicit domain_error(const char* __s) : logic_error(__s) {}
129
130#ifndef _LIBCPP_ABI_VCRUNTIME
131  _LIBCPP_HIDE_FROM_ABI domain_error(const domain_error&) _NOEXCEPT            = default;
132  _LIBCPP_HIDE_FROM_ABI domain_error& operator=(const domain_error&) _NOEXCEPT = default;
133  ~domain_error() _NOEXCEPT override;
134#endif
135};
136
137class _LIBCPP_EXPORTED_FROM_ABI invalid_argument : public logic_error {
138public:
139  _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const string& __s) : logic_error(__s) {}
140  _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const char* __s) : logic_error(__s) {}
141
142#ifndef _LIBCPP_ABI_VCRUNTIME
143  _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT            = default;
144  _LIBCPP_HIDE_FROM_ABI invalid_argument& operator=(const invalid_argument&) _NOEXCEPT = default;
145  ~invalid_argument() _NOEXCEPT override;
146#endif
147};
148
149class _LIBCPP_EXPORTED_FROM_ABI length_error : public logic_error {
150public:
151  _LIBCPP_HIDE_FROM_ABI explicit length_error(const string& __s) : logic_error(__s) {}
152  _LIBCPP_HIDE_FROM_ABI explicit length_error(const char* __s) : logic_error(__s) {}
153#ifndef _LIBCPP_ABI_VCRUNTIME
154  _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT            = default;
155  _LIBCPP_HIDE_FROM_ABI length_error& operator=(const length_error&) _NOEXCEPT = default;
156  ~length_error() _NOEXCEPT override;
157#endif
158};
159
160class _LIBCPP_EXPORTED_FROM_ABI out_of_range : public logic_error {
161public:
162  _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const string& __s) : logic_error(__s) {}
163  _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const char* __s) : logic_error(__s) {}
164
165#ifndef _LIBCPP_ABI_VCRUNTIME
166  _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT            = default;
167  _LIBCPP_HIDE_FROM_ABI out_of_range& operator=(const out_of_range&) _NOEXCEPT = default;
168  ~out_of_range() _NOEXCEPT override;
169#endif
170};
171
172class _LIBCPP_EXPORTED_FROM_ABI range_error : public runtime_error {
173public:
174  _LIBCPP_HIDE_FROM_ABI explicit range_error(const string& __s) : runtime_error(__s) {}
175  _LIBCPP_HIDE_FROM_ABI explicit range_error(const char* __s) : runtime_error(__s) {}
176
177#ifndef _LIBCPP_ABI_VCRUNTIME
178  _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT            = default;
179  _LIBCPP_HIDE_FROM_ABI range_error& operator=(const range_error&) _NOEXCEPT = default;
180  ~range_error() _NOEXCEPT override;
181#endif
182};
183
184class _LIBCPP_EXPORTED_FROM_ABI overflow_error : public runtime_error {
185public:
186  _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const string& __s) : runtime_error(__s) {}
187  _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const char* __s) : runtime_error(__s) {}
188
189#ifndef _LIBCPP_ABI_VCRUNTIME
190  _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT            = default;
191  _LIBCPP_HIDE_FROM_ABI overflow_error& operator=(const overflow_error&) _NOEXCEPT = default;
192  ~overflow_error() _NOEXCEPT override;
193#endif
194};
195
196class _LIBCPP_EXPORTED_FROM_ABI underflow_error : public runtime_error {
197public:
198  _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const string& __s) : runtime_error(__s) {}
199  _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const char* __s) : runtime_error(__s) {}
200
201#ifndef _LIBCPP_ABI_VCRUNTIME
202  _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT            = default;
203  _LIBCPP_HIDE_FROM_ABI underflow_error& operator=(const underflow_error&) _NOEXCEPT = default;
204  ~underflow_error() _NOEXCEPT override;
205#endif
206};
207
208} // namespace std
209
210_LIBCPP_BEGIN_NAMESPACE_STD
211
212// in the dylib
213_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
214
215_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_logic_error(const char* __msg) {
216#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
217  throw logic_error(__msg);
218#else
219  _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
220#endif
221}
222
223_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_domain_error(const char* __msg) {
224#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
225  throw domain_error(__msg);
226#else
227  _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
228#endif
229}
230
231_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_invalid_argument(const char* __msg) {
232#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
233  throw invalid_argument(__msg);
234#else
235  _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
236#endif
237}
238
239_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_length_error(const char* __msg) {
240#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
241  throw length_error(__msg);
242#else
243  _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
244#endif
245}
246
247_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_out_of_range(const char* __msg) {
248#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
249  throw out_of_range(__msg);
250#else
251  _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
252#endif
253}
254
255_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_range_error(const char* __msg) {
256#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
257  throw range_error(__msg);
258#else
259  _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
260#endif
261}
262
263_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_overflow_error(const char* __msg) {
264#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
265  throw overflow_error(__msg);
266#else
267  _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
268#endif
269}
270
271_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_underflow_error(const char* __msg) {
272#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
273  throw underflow_error(__msg);
274#else
275  _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
276#endif
277}
278
279_LIBCPP_END_NAMESPACE_STD
280
281#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
282#  include <cstdlib>
283#  include <exception>
284#  include <iosfwd>
285#endif
286
287#endif // _LIBCPP_STDEXCEPT
288