xref: /freebsd/contrib/llvm-project/libcxx/include/stdexcept (revision 7877fdebeeb35fad1cbbafce22598b1bdf97c786)
1// -*- C++ -*-
2//===--------------------------- stdexcept --------------------------------===//
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 <__config>
45#include <exception>
46#include <iosfwd>  // for string forward decl
47#ifdef _LIBCPP_NO_EXCEPTIONS
48#include <cstdlib>
49#endif
50
51#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
52#pragma GCC system_header
53#endif
54
55_LIBCPP_BEGIN_NAMESPACE_STD
56
57#ifndef _LIBCPP_ABI_VCRUNTIME
58class _LIBCPP_HIDDEN __libcpp_refstring
59{
60    const char* __imp_;
61
62    bool __uses_refcount() const;
63public:
64    explicit __libcpp_refstring(const char* __msg);
65    __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
66    __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
67    ~__libcpp_refstring();
68
69    const char* c_str() const _NOEXCEPT {return __imp_;}
70};
71#endif // !_LIBCPP_ABI_VCRUNTIME
72
73_LIBCPP_END_NAMESPACE_STD
74
75namespace std  // purposefully not using versioning namespace
76{
77
78class _LIBCPP_EXCEPTION_ABI logic_error
79    : public exception
80{
81#ifndef _LIBCPP_ABI_VCRUNTIME
82private:
83    _VSTD::__libcpp_refstring __imp_;
84public:
85    explicit logic_error(const string&);
86    explicit logic_error(const char*);
87
88    logic_error(const logic_error&) _NOEXCEPT;
89    logic_error& operator=(const logic_error&) _NOEXCEPT;
90
91    virtual ~logic_error() _NOEXCEPT;
92
93    virtual const char* what() const _NOEXCEPT;
94#else
95public:
96    explicit logic_error(const _VSTD::string&); // Symbol uses versioned std::string
97    _LIBCPP_INLINE_VISIBILITY explicit logic_error(const char* __s) : exception(__s) {}
98#endif
99};
100
101class _LIBCPP_EXCEPTION_ABI runtime_error
102    : public exception
103{
104#ifndef _LIBCPP_ABI_VCRUNTIME
105private:
106    _VSTD::__libcpp_refstring __imp_;
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    virtual ~runtime_error() _NOEXCEPT;
115
116    virtual const char* what() const _NOEXCEPT;
117#else
118public:
119   explicit runtime_error(const _VSTD::string&); // Symbol uses versioned std::string
120   _LIBCPP_INLINE_VISIBILITY explicit runtime_error(const char* __s) : exception(__s) {}
121#endif // _LIBCPP_ABI_VCRUNTIME
122};
123
124class _LIBCPP_EXCEPTION_ABI domain_error
125    : public logic_error
126{
127public:
128    _LIBCPP_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {}
129    _LIBCPP_INLINE_VISIBILITY explicit domain_error(const char* __s)   : logic_error(__s) {}
130
131#ifndef _LIBCPP_ABI_VCRUNTIME
132    domain_error(const domain_error&) _NOEXCEPT = default;
133    virtual ~domain_error() _NOEXCEPT;
134#endif
135};
136
137class _LIBCPP_EXCEPTION_ABI invalid_argument
138    : public logic_error
139{
140public:
141    _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
142    _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s)   : logic_error(__s) {}
143
144#ifndef _LIBCPP_ABI_VCRUNTIME
145    invalid_argument(const invalid_argument&) _NOEXCEPT = default;
146    virtual ~invalid_argument() _NOEXCEPT;
147#endif
148};
149
150class _LIBCPP_EXCEPTION_ABI length_error
151    : public logic_error
152{
153public:
154    _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
155    _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s)   : logic_error(__s) {}
156#ifndef _LIBCPP_ABI_VCRUNTIME
157    length_error(const length_error&) _NOEXCEPT = default;
158    virtual ~length_error() _NOEXCEPT;
159#endif
160};
161
162class _LIBCPP_EXCEPTION_ABI out_of_range
163    : public logic_error
164{
165public:
166    _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
167    _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s)   : logic_error(__s) {}
168
169#ifndef _LIBCPP_ABI_VCRUNTIME
170    out_of_range(const out_of_range&) _NOEXCEPT = default;
171    virtual ~out_of_range() _NOEXCEPT;
172#endif
173};
174
175class _LIBCPP_EXCEPTION_ABI range_error
176    : public runtime_error
177{
178public:
179    _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
180    _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s)   : runtime_error(__s) {}
181
182#ifndef _LIBCPP_ABI_VCRUNTIME
183    range_error(const range_error&) _NOEXCEPT = default;
184    virtual ~range_error() _NOEXCEPT;
185#endif
186};
187
188class _LIBCPP_EXCEPTION_ABI overflow_error
189    : public runtime_error
190{
191public:
192    _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
193    _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s)   : runtime_error(__s) {}
194
195#ifndef _LIBCPP_ABI_VCRUNTIME
196    overflow_error(const overflow_error&) _NOEXCEPT = default;
197    virtual ~overflow_error() _NOEXCEPT;
198#endif
199};
200
201class _LIBCPP_EXCEPTION_ABI underflow_error
202    : public runtime_error
203{
204public:
205    _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
206    _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s)   : runtime_error(__s) {}
207
208#ifndef _LIBCPP_ABI_VCRUNTIME
209    underflow_error(const underflow_error&) _NOEXCEPT = default;
210    virtual ~underflow_error() _NOEXCEPT;
211#endif
212};
213
214}  // std
215
216_LIBCPP_BEGIN_NAMESPACE_STD
217
218// in the dylib
219_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
220
221_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
222void __throw_logic_error(const char*__msg)
223{
224#ifndef _LIBCPP_NO_EXCEPTIONS
225    throw logic_error(__msg);
226#else
227    ((void)__msg);
228    _VSTD::abort();
229#endif
230}
231
232_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
233void __throw_domain_error(const char*__msg)
234{
235#ifndef _LIBCPP_NO_EXCEPTIONS
236    throw domain_error(__msg);
237#else
238    ((void)__msg);
239    _VSTD::abort();
240#endif
241}
242
243_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
244void __throw_invalid_argument(const char*__msg)
245{
246#ifndef _LIBCPP_NO_EXCEPTIONS
247    throw invalid_argument(__msg);
248#else
249    ((void)__msg);
250    _VSTD::abort();
251#endif
252}
253
254_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
255void __throw_length_error(const char*__msg)
256{
257#ifndef _LIBCPP_NO_EXCEPTIONS
258    throw length_error(__msg);
259#else
260    ((void)__msg);
261    _VSTD::abort();
262#endif
263}
264
265_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
266void __throw_out_of_range(const char*__msg)
267{
268#ifndef _LIBCPP_NO_EXCEPTIONS
269    throw out_of_range(__msg);
270#else
271    ((void)__msg);
272    _VSTD::abort();
273#endif
274}
275
276_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
277void __throw_range_error(const char*__msg)
278{
279#ifndef _LIBCPP_NO_EXCEPTIONS
280    throw range_error(__msg);
281#else
282    ((void)__msg);
283    _VSTD::abort();
284#endif
285}
286
287_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
288void __throw_overflow_error(const char*__msg)
289{
290#ifndef _LIBCPP_NO_EXCEPTIONS
291    throw overflow_error(__msg);
292#else
293    ((void)__msg);
294    _VSTD::abort();
295#endif
296}
297
298_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
299void __throw_underflow_error(const char*__msg)
300{
301#ifndef _LIBCPP_NO_EXCEPTIONS
302    throw underflow_error(__msg);
303#else
304    ((void)__msg);
305    _VSTD::abort();
306#endif
307}
308
309_LIBCPP_END_NAMESPACE_STD
310
311#endif  // _LIBCPP_STDEXCEPT
312