xref: /freebsd/contrib/llvm-project/libcxx/include/stdexcept (revision 1631382cf2820245cc72965498ff174bb548dd63)
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 <__verbose_abort>
48#include <iosfwd>  // for string forward decl
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    _VSTD::__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 _VSTD::string&); // Symbol uses versioned std::string
96    _LIBCPP_INLINE_VISIBILITY 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    _VSTD::__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 _VSTD::string&); // Symbol uses versioned std::string
119   _LIBCPP_INLINE_VISIBILITY 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_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {}
128    _LIBCPP_INLINE_VISIBILITY 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    ~domain_error() _NOEXCEPT override;
133#endif
134};
135
136class _LIBCPP_EXPORTED_FROM_ABI invalid_argument
137    : public logic_error
138{
139public:
140    _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
141    _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s)   : logic_error(__s) {}
142
143#ifndef _LIBCPP_ABI_VCRUNTIME
144    _LIBCPP_HIDE_FROM_ABI invalid_argument(const invalid_argument&) _NOEXCEPT = default;
145    ~invalid_argument() _NOEXCEPT override;
146#endif
147};
148
149class _LIBCPP_EXPORTED_FROM_ABI length_error
150    : public logic_error
151{
152public:
153    _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
154    _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s)   : logic_error(__s) {}
155#ifndef _LIBCPP_ABI_VCRUNTIME
156    _LIBCPP_HIDE_FROM_ABI length_error(const length_error&) _NOEXCEPT = default;
157    ~length_error() _NOEXCEPT override;
158#endif
159};
160
161class _LIBCPP_EXPORTED_FROM_ABI out_of_range
162    : public logic_error
163{
164public:
165    _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
166    _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s)   : logic_error(__s) {}
167
168#ifndef _LIBCPP_ABI_VCRUNTIME
169    _LIBCPP_HIDE_FROM_ABI out_of_range(const out_of_range&) _NOEXCEPT = default;
170    ~out_of_range() _NOEXCEPT override;
171#endif
172};
173
174class _LIBCPP_EXPORTED_FROM_ABI range_error
175    : public runtime_error
176{
177public:
178    _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
179    _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s)   : runtime_error(__s) {}
180
181#ifndef _LIBCPP_ABI_VCRUNTIME
182    _LIBCPP_HIDE_FROM_ABI range_error(const range_error&) _NOEXCEPT = default;
183    ~range_error() _NOEXCEPT override;
184#endif
185};
186
187class _LIBCPP_EXPORTED_FROM_ABI overflow_error
188    : public runtime_error
189{
190public:
191    _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
192    _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s)   : runtime_error(__s) {}
193
194#ifndef _LIBCPP_ABI_VCRUNTIME
195    _LIBCPP_HIDE_FROM_ABI overflow_error(const overflow_error&) _NOEXCEPT = default;
196    ~overflow_error() _NOEXCEPT override;
197#endif
198};
199
200class _LIBCPP_EXPORTED_FROM_ABI underflow_error
201    : public runtime_error
202{
203public:
204    _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
205    _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s)   : runtime_error(__s) {}
206
207#ifndef _LIBCPP_ABI_VCRUNTIME
208    _LIBCPP_HIDE_FROM_ABI underflow_error(const underflow_error&) _NOEXCEPT = default;
209    ~underflow_error() _NOEXCEPT override;
210#endif
211};
212
213} // namespace std
214
215_LIBCPP_BEGIN_NAMESPACE_STD
216
217// in the dylib
218_LIBCPP_NORETURN _LIBCPP_EXPORTED_FROM_ABI void __throw_runtime_error(const char*);
219
220_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
221void __throw_logic_error(const char*__msg)
222{
223#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
224    throw logic_error(__msg);
225#else
226    _LIBCPP_VERBOSE_ABORT("logic_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
227#endif
228}
229
230_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
231void __throw_domain_error(const char*__msg)
232{
233#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
234    throw domain_error(__msg);
235#else
236    _LIBCPP_VERBOSE_ABORT("domain_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
237#endif
238}
239
240_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
241void __throw_invalid_argument(const char*__msg)
242{
243#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
244    throw invalid_argument(__msg);
245#else
246    _LIBCPP_VERBOSE_ABORT("invalid_argument was thrown in -fno-exceptions mode with message \"%s\"", __msg);
247#endif
248}
249
250_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
251void __throw_length_error(const char*__msg)
252{
253#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
254    throw length_error(__msg);
255#else
256    _LIBCPP_VERBOSE_ABORT("length_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
257#endif
258}
259
260_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
261void __throw_out_of_range(const char*__msg)
262{
263#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
264    throw out_of_range(__msg);
265#else
266    _LIBCPP_VERBOSE_ABORT("out_of_range was thrown in -fno-exceptions mode with message \"%s\"", __msg);
267#endif
268}
269
270_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
271void __throw_range_error(const char*__msg)
272{
273#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
274    throw range_error(__msg);
275#else
276    _LIBCPP_VERBOSE_ABORT("range_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
277#endif
278}
279
280_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
281void __throw_overflow_error(const char*__msg)
282{
283#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
284    throw overflow_error(__msg);
285#else
286    _LIBCPP_VERBOSE_ABORT("overflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
287#endif
288}
289
290_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
291void __throw_underflow_error(const char*__msg)
292{
293#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
294    throw underflow_error(__msg);
295#else
296    _LIBCPP_VERBOSE_ABORT("underflow_error was thrown in -fno-exceptions mode with message \"%s\"", __msg);
297#endif
298}
299
300_LIBCPP_END_NAMESPACE_STD
301
302#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
303#  include <cstdlib>
304#  include <exception>
305#endif
306
307#endif // _LIBCPP_STDEXCEPT
308