blob: 7a7f3679409959772d3614ae822ddce1a2f27ac4 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- stdexcept --------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
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);
Howard Hinnant1f098bc2011-05-26 19:48:01 +000035 explicit xxx_error(const char* what_arg);
Howard Hinnantc51e1022010-05-11 19:42:16 +000036
Howard Hinnant1f098bc2011-05-26 19:48:01 +000037 virtual const char* what() const noexcept // returns what_arg
Howard Hinnantc51e1022010-05-11 19:42:16 +000038};
39
40} // std
41
42*/
43
44#include <__config>
45#include <exception>
46#include <iosfwd> // for string forward decl
Eric Fiselier279c3192016-09-06 21:25:27 +000047#ifdef _LIBCPP_NO_EXCEPTIONS
48#include <cstdlib>
49#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000050
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000051#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000052#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000053#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000054
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000055_LIBCPP_BEGIN_NAMESPACE_STD
Eric Fiselier1e655162016-10-25 19:33:14 +000056
Eric Fiselierc6e689b2019-03-06 20:31:57 +000057#ifndef _LIBCPP_ABI_VCRUNTIME
Eric Fiselier1e655162016-10-25 19:33:14 +000058class _LIBCPP_HIDDEN __libcpp_refstring
59{
60 const char* __imp_;
61
62 bool __uses_refcount() const;
63public:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +000064 explicit __libcpp_refstring(const char* __msg);
65 __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
66 __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
Eric Fiselier1e655162016-10-25 19:33:14 +000067 ~__libcpp_refstring();
68
69 const char* c_str() const _NOEXCEPT {return __imp_;}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000070};
Eric Fiselierc6e689b2019-03-06 20:31:57 +000071#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselier1e655162016-10-25 19:33:14 +000072
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000073_LIBCPP_END_NAMESPACE_STD
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000074
Howard Hinnantc51e1022010-05-11 19:42:16 +000075namespace std // purposefully not using versioning namespace
76{
77
78class _LIBCPP_EXCEPTION_ABI logic_error
79 : public exception
80{
Eric Fiselierc6e689b2019-03-06 20:31:57 +000081#ifndef _LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +000082private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000083 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084public:
85 explicit logic_error(const string&);
86 explicit logic_error(const char*);
87
Howard Hinnant1f098bc2011-05-26 19:48:01 +000088 logic_error(const logic_error&) _NOEXCEPT;
89 logic_error& operator=(const logic_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
Howard Hinnant1f098bc2011-05-26 19:48:01 +000091 virtual ~logic_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000092
Howard Hinnant1f098bc2011-05-26 19:48:01 +000093 virtual const char* what() const _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +000094#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
Howard Hinnantc51e1022010-05-11 19:42:16 +000099};
100
101class _LIBCPP_EXCEPTION_ABI runtime_error
102 : public exception
103{
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000104#ifndef _LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000106 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000107public:
108 explicit runtime_error(const string&);
109 explicit runtime_error(const char*);
110
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000111 runtime_error(const runtime_error&) _NOEXCEPT;
112 runtime_error& operator=(const runtime_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000114 virtual ~runtime_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000116 virtual const char* what() const _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000117#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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000122};
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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000131#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100132 domain_error(const domain_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000133 virtual ~domain_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000134#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000135};
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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000144#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100145 invalid_argument(const invalid_argument&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000146 virtual ~invalid_argument() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000147#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148};
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) {}
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000156#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100157 length_error(const length_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000158 virtual ~length_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000159#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160};
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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000169#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100170 out_of_range(const out_of_range&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000171 virtual ~out_of_range() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000172#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173};
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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000182#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100183 range_error(const range_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000184 virtual ~range_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000185#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000186};
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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000195#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100196 overflow_error(const overflow_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000197 virtual ~overflow_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000198#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199};
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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000208#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100209 underflow_error(const underflow_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000210 virtual ~underflow_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000211#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212};
213
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214} // std
215
Marshall Clow8fea1612016-08-25 15:09:01 +0000216_LIBCPP_BEGIN_NAMESPACE_STD
217
218// in the dylib
219_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
220
Louis Dionne16fe2952018-07-11 23:14:33 +0000221_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000222void __throw_logic_error(const char*__msg)
223{
224#ifndef _LIBCPP_NO_EXCEPTIONS
225 throw logic_error(__msg);
226#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000227 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000228 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000229#endif
230}
231
Louis Dionne16fe2952018-07-11 23:14:33 +0000232_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000233void __throw_domain_error(const char*__msg)
234{
235#ifndef _LIBCPP_NO_EXCEPTIONS
236 throw domain_error(__msg);
237#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000238 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000239 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000240#endif
241}
242
Louis Dionne16fe2952018-07-11 23:14:33 +0000243_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000244void __throw_invalid_argument(const char*__msg)
245{
246#ifndef _LIBCPP_NO_EXCEPTIONS
247 throw invalid_argument(__msg);
248#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000249 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000250 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000251#endif
252}
253
Louis Dionne16fe2952018-07-11 23:14:33 +0000254_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000255void __throw_length_error(const char*__msg)
256{
257#ifndef _LIBCPP_NO_EXCEPTIONS
258 throw length_error(__msg);
259#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000260 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000261 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000262#endif
263}
264
Louis Dionne16fe2952018-07-11 23:14:33 +0000265_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000266void __throw_out_of_range(const char*__msg)
267{
268#ifndef _LIBCPP_NO_EXCEPTIONS
269 throw out_of_range(__msg);
270#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000271 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000272 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000273#endif
274}
275
Louis Dionne16fe2952018-07-11 23:14:33 +0000276_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000277void __throw_range_error(const char*__msg)
278{
279#ifndef _LIBCPP_NO_EXCEPTIONS
280 throw range_error(__msg);
281#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000282 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000283 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000284#endif
285}
286
Louis Dionne16fe2952018-07-11 23:14:33 +0000287_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000288void __throw_overflow_error(const char*__msg)
289{
290#ifndef _LIBCPP_NO_EXCEPTIONS
291 throw overflow_error(__msg);
292#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000293 ((void)__msg);
294 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000295#endif
296}
297
Louis Dionne16fe2952018-07-11 23:14:33 +0000298_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000299void __throw_underflow_error(const char*__msg)
300{
301#ifndef _LIBCPP_NO_EXCEPTIONS
302 throw underflow_error(__msg);
303#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000304 ((void)__msg);
305 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000306#endif
307}
308
309_LIBCPP_END_NAMESPACE_STD
310
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311#endif // _LIBCPP_STDEXCEPT