blob: c1dc05669bb9f9d3ce24f795d6c170f55167f53e [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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
Arthur O'Dwyeref181602021-05-19 11:57:04 -040047#include <cstdlib>
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000049#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000050#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000051#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000053_LIBCPP_BEGIN_NAMESPACE_STD
Eric Fiselier1e655162016-10-25 19:33:14 +000054
Eric Fiselierc6e689b2019-03-06 20:31:57 +000055#ifndef _LIBCPP_ABI_VCRUNTIME
Eric Fiselier1e655162016-10-25 19:33:14 +000056class _LIBCPP_HIDDEN __libcpp_refstring
57{
58 const char* __imp_;
59
60 bool __uses_refcount() const;
61public:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +000062 explicit __libcpp_refstring(const char* __msg);
63 __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
64 __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
Eric Fiselier1e655162016-10-25 19:33:14 +000065 ~__libcpp_refstring();
66
67 const char* c_str() const _NOEXCEPT {return __imp_;}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000068};
Eric Fiselierc6e689b2019-03-06 20:31:57 +000069#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselier1e655162016-10-25 19:33:14 +000070
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000071_LIBCPP_END_NAMESPACE_STD
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000072
Howard Hinnantc51e1022010-05-11 19:42:16 +000073namespace std // purposefully not using versioning namespace
74{
75
76class _LIBCPP_EXCEPTION_ABI logic_error
77 : public exception
78{
Eric Fiselierc6e689b2019-03-06 20:31:57 +000079#ifndef _LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +000080private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000081 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082public:
83 explicit logic_error(const string&);
84 explicit logic_error(const char*);
85
Howard Hinnant1f098bc2011-05-26 19:48:01 +000086 logic_error(const logic_error&) _NOEXCEPT;
87 logic_error& operator=(const logic_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Howard Hinnant1f098bc2011-05-26 19:48:01 +000089 virtual ~logic_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000090
Howard Hinnant1f098bc2011-05-26 19:48:01 +000091 virtual const char* what() const _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +000092#else
93public:
94 explicit logic_error(const _VSTD::string&); // Symbol uses versioned std::string
95 _LIBCPP_INLINE_VISIBILITY explicit logic_error(const char* __s) : exception(__s) {}
96#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000097};
98
99class _LIBCPP_EXCEPTION_ABI runtime_error
100 : public exception
101{
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000102#ifndef _LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000104 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000105public:
106 explicit runtime_error(const string&);
107 explicit runtime_error(const char*);
108
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000109 runtime_error(const runtime_error&) _NOEXCEPT;
110 runtime_error& operator=(const runtime_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000111
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000112 virtual ~runtime_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000114 virtual const char* what() const _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000115#else
116public:
117 explicit runtime_error(const _VSTD::string&); // Symbol uses versioned std::string
118 _LIBCPP_INLINE_VISIBILITY explicit runtime_error(const char* __s) : exception(__s) {}
119#endif // _LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120};
121
122class _LIBCPP_EXCEPTION_ABI domain_error
123 : public logic_error
124{
125public:
126 _LIBCPP_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {}
127 _LIBCPP_INLINE_VISIBILITY explicit domain_error(const char* __s) : logic_error(__s) {}
128
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000129#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100130 domain_error(const domain_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000131 virtual ~domain_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000132#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133};
134
135class _LIBCPP_EXCEPTION_ABI invalid_argument
136 : public logic_error
137{
138public:
139 _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
140 _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s) : logic_error(__s) {}
141
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000142#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100143 invalid_argument(const invalid_argument&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000144 virtual ~invalid_argument() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000145#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146};
147
148class _LIBCPP_EXCEPTION_ABI length_error
149 : public logic_error
150{
151public:
152 _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
153 _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s) : logic_error(__s) {}
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000154#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100155 length_error(const length_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000156 virtual ~length_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000157#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158};
159
160class _LIBCPP_EXCEPTION_ABI out_of_range
161 : public logic_error
162{
163public:
164 _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
165 _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s) : logic_error(__s) {}
166
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000167#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100168 out_of_range(const out_of_range&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000169 virtual ~out_of_range() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000170#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171};
172
173class _LIBCPP_EXCEPTION_ABI range_error
174 : public runtime_error
175{
176public:
177 _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
178 _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s) : runtime_error(__s) {}
179
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000180#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100181 range_error(const range_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000182 virtual ~range_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000183#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184};
185
186class _LIBCPP_EXCEPTION_ABI overflow_error
187 : public runtime_error
188{
189public:
190 _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
191 _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s) : runtime_error(__s) {}
192
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000193#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100194 overflow_error(const overflow_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000195 virtual ~overflow_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000196#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197};
198
199class _LIBCPP_EXCEPTION_ABI underflow_error
200 : public runtime_error
201{
202public:
203 _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
204 _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s) : runtime_error(__s) {}
205
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000206#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100207 underflow_error(const underflow_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000208 virtual ~underflow_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000209#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210};
211
Nikolas Klauserd26407a2021-12-02 14:12:51 +0100212} // namespace std
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213
Marshall Clow8fea1612016-08-25 15:09:01 +0000214_LIBCPP_BEGIN_NAMESPACE_STD
215
216// in the dylib
217_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
218
Louis Dionne16fe2952018-07-11 23:14:33 +0000219_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000220void __throw_logic_error(const char*__msg)
221{
222#ifndef _LIBCPP_NO_EXCEPTIONS
223 throw logic_error(__msg);
224#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000225 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000226 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000227#endif
228}
229
Louis Dionne16fe2952018-07-11 23:14:33 +0000230_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000231void __throw_domain_error(const char*__msg)
232{
233#ifndef _LIBCPP_NO_EXCEPTIONS
234 throw domain_error(__msg);
235#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000236 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000237 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000238#endif
239}
240
Louis Dionne16fe2952018-07-11 23:14:33 +0000241_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000242void __throw_invalid_argument(const char*__msg)
243{
244#ifndef _LIBCPP_NO_EXCEPTIONS
245 throw invalid_argument(__msg);
246#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000247 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000248 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000249#endif
250}
251
Louis Dionne16fe2952018-07-11 23:14:33 +0000252_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000253void __throw_length_error(const char*__msg)
254{
255#ifndef _LIBCPP_NO_EXCEPTIONS
256 throw length_error(__msg);
257#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000258 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000259 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000260#endif
261}
262
Louis Dionne16fe2952018-07-11 23:14:33 +0000263_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000264void __throw_out_of_range(const char*__msg)
265{
266#ifndef _LIBCPP_NO_EXCEPTIONS
267 throw out_of_range(__msg);
268#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000269 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000270 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000271#endif
272}
273
Louis Dionne16fe2952018-07-11 23:14:33 +0000274_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000275void __throw_range_error(const char*__msg)
276{
277#ifndef _LIBCPP_NO_EXCEPTIONS
278 throw range_error(__msg);
279#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000280 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000281 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000282#endif
283}
284
Louis Dionne16fe2952018-07-11 23:14:33 +0000285_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000286void __throw_overflow_error(const char*__msg)
287{
288#ifndef _LIBCPP_NO_EXCEPTIONS
289 throw overflow_error(__msg);
290#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000291 ((void)__msg);
292 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000293#endif
294}
295
Louis Dionne16fe2952018-07-11 23:14:33 +0000296_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000297void __throw_underflow_error(const char*__msg)
298{
299#ifndef _LIBCPP_NO_EXCEPTIONS
300 throw underflow_error(__msg);
301#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000302 ((void)__msg);
303 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000304#endif
305}
306
307_LIBCPP_END_NAMESPACE_STD
308
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400309#endif // _LIBCPP_STDEXCEPT