xref: /freebsd/contrib/llvm-project/libcxx/include/stdexcept (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
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{
59    const char* __imp_;
60
61    bool __uses_refcount() const;
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
78    : public exception
79{
80#ifndef _LIBCPP_ABI_VCRUNTIME
81private:
82    std::__libcpp_refstring __imp_;
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
94public:
95    explicit logic_error(const std::string&); // Symbol uses versioned std::string
96    _LIBCPP_HIDE_FROM_ABI explicit logic_error(const char* __s) : exception(__s) {}
97#endif
98};
99
100class _LIBCPP_EXPORTED_FROM_ABI runtime_error
101    : public exception
102{
103#ifndef _LIBCPP_ABI_VCRUNTIME
104private:
105    std::__libcpp_refstring __imp_;
106public:
107    explicit runtime_error(const string&);
108    explicit runtime_error(const char*);
109
110    runtime_error(const runtime_error&) _NOEXCEPT;
111    runtime_error& operator=(const runtime_error&) _NOEXCEPT;
112
113    ~runtime_error() _NOEXCEPT override;
114
115    const char* what() const _NOEXCEPT override;
116#else
117public:
118   explicit runtime_error(const std::string&); // Symbol uses versioned std::string
119   _LIBCPP_HIDE_FROM_ABI explicit runtime_error(const char* __s) : exception(__s) {}
120#endif // _LIBCPP_ABI_VCRUNTIME
121};
122
123class _LIBCPP_EXPORTED_FROM_ABI domain_error
124    : public logic_error
125{
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
138    : public logic_error
139{
140public:
141    _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const string& __s) : logic_error(__s) {}
142    _LIBCPP_HIDE_FROM_ABI explicit invalid_argument(const char* __s)   : logic_error(__s) {}
143
144#ifndef _LIBCPP_ABI_VCRUNTIME
145    _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT = default;
146    _LIBCPP_HIDE_FROM_ABI invalid_argument& operator=(const invalid_argument&) _NOEXCEPT = default;
147    ~invalid_argument() _NOEXCEPT override;
148#endif
149};
150
151class _LIBCPP_EXPORTED_FROM_ABI length_error
152    : public logic_error
153{
154public:
155    _LIBCPP_HIDE_FROM_ABI explicit length_error(const string& __s) : logic_error(__s) {}
156    _LIBCPP_HIDE_FROM_ABI explicit length_error(const char* __s)   : logic_error(__s) {}
157#ifndef _LIBCPP_ABI_VCRUNTIME
158    _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT = default;
159    _LIBCPP_HIDE_FROM_ABI length_error& operator=(const length_error&) _NOEXCEPT = default;
160    ~length_error() _NOEXCEPT override;
161#endif
162};
163
164class _LIBCPP_EXPORTED_FROM_ABI out_of_range
165    : public logic_error
166{
167public:
168    _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const string& __s) : logic_error(__s) {}
169    _LIBCPP_HIDE_FROM_ABI explicit out_of_range(const char* __s)   : logic_error(__s) {}
170
171#ifndef _LIBCPP_ABI_VCRUNTIME
172    _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT = default;
173    _LIBCPP_HIDE_FROM_ABI out_of_range& operator=(const out_of_range&) _NOEXCEPT = default;
174    ~out_of_range() _NOEXCEPT override;
175#endif
176};
177
178class _LIBCPP_EXPORTED_FROM_ABI range_error
179    : public runtime_error
180{
181public:
182    _LIBCPP_HIDE_FROM_ABI explicit range_error(const string& __s) : runtime_error(__s) {}
183    _LIBCPP_HIDE_FROM_ABI explicit range_error(const char* __s)   : runtime_error(__s) {}
184
185#ifndef _LIBCPP_ABI_VCRUNTIME
186    _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT = default;
187    _LIBCPP_HIDE_FROM_ABI range_error& operator=(const range_error&) _NOEXCEPT = default;
188    ~range_error() _NOEXCEPT override;
189#endif
190};
191
192class _LIBCPP_EXPORTED_FROM_ABI overflow_error
193    : public runtime_error
194{
195public:
196    _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const string& __s) : runtime_error(__s) {}
197    _LIBCPP_HIDE_FROM_ABI explicit overflow_error(const char* __s)   : runtime_error(__s) {}
198
199#ifndef _LIBCPP_ABI_VCRUNTIME
200    _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT = default;
201    _LIBCPP_HIDE_FROM_ABI overflow_error& operator=(const overflow_error&) _NOEXCEPT = default;
202    ~overflow_error() _NOEXCEPT override;
203#endif
204};
205
206class _LIBCPP_EXPORTED_FROM_ABI underflow_error
207    : public runtime_error
208{
209public:
210    _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const string& __s) : runtime_error(__s) {}
211    _LIBCPP_HIDE_FROM_ABI explicit underflow_error(const char* __s)   : runtime_error(__s) {}
212
213#ifndef _LIBCPP_ABI_VCRUNTIME
214    _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT = default;
215    _LIBCPP_HIDE_FROM_ABI underflow_error& operator=(const underflow_error&) _NOEXCEPT = default;
216    ~underflow_error() _NOEXCEPT override;
217#endif
218};
219
220} // namespace std
221
222_LIBCPP_BEGIN_NAMESPACE_STD
223
224// in the dylib
225_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
226
227_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
228void __throw_logic_error(const char*__msg)
229{
230#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
231    throw logic_error(__msg);
232#else
233    _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
234#endif
235}
236
237_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
238void __throw_domain_error(const char*__msg)
239{
240#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
241    throw domain_error(__msg);
242#else
243    _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
244#endif
245}
246
247_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
248void __throw_invalid_argument(const char*__msg)
249{
250#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
251    throw invalid_argument(__msg);
252#else
253    _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
254#endif
255}
256
257_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
258void __throw_length_error(const char*__msg)
259{
260#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
261    throw length_error(__msg);
262#else
263    _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
264#endif
265}
266
267_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
268void __throw_out_of_range(const char*__msg)
269{
270#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
271    throw out_of_range(__msg);
272#else
273    _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
274#endif
275}
276
277_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
278void __throw_range_error(const char*__msg)
279{
280#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
281    throw range_error(__msg);
282#else
283    _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
284#endif
285}
286
287_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
288void __throw_overflow_error(const char*__msg)
289{
290#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
291    throw overflow_error(__msg);
292#else
293    _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
294#endif
295}
296
297_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI
298void __throw_underflow_error(const char*__msg)
299{
300#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
301    throw underflow_error(__msg);
302#else
303    _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
304#endif
305}
306
307_LIBCPP_END_NAMESPACE_STD
308
309#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
310#  include <cstdlib>
311#  include <exception>
312#  include <iosfwd>
313#endif
314
315#endif // _LIBCPP_STDEXCEPT
316