blob: ee5a296cd9305b4d1bd345cc6981b8393a78ee7a [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
Louis Dionneb4fce352022-03-25 12:55:36 -040044#include <__assert> // all public C++ headers provide the assertion handler
Howard Hinnantc51e1022010-05-11 19:42:16 +000045#include <__config>
Arthur O'Dwyer65077c02022-01-07 09:45:05 -050046#include <cstdlib>
Howard Hinnantc51e1022010-05-11 19:42:16 +000047#include <exception>
48#include <iosfwd> // for string forward decl
49
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000050#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Arthur O'Dwyer6eeaa002022-02-01 20:16:40 -050051# pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000052#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000053
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000054_LIBCPP_BEGIN_NAMESPACE_STD
Eric Fiselier1e655162016-10-25 19:33:14 +000055
Eric Fiselierc6e689b2019-03-06 20:31:57 +000056#ifndef _LIBCPP_ABI_VCRUNTIME
Eric Fiselier1e655162016-10-25 19:33:14 +000057class _LIBCPP_HIDDEN __libcpp_refstring
58{
59 const char* __imp_;
60
61 bool __uses_refcount() const;
62public:
Eric Fiselierc5ea1ae2017-06-01 02:29:37 +000063 explicit __libcpp_refstring(const char* __msg);
64 __libcpp_refstring(const __libcpp_refstring& __s) _NOEXCEPT;
65 __libcpp_refstring& operator=(const __libcpp_refstring& __s) _NOEXCEPT;
Eric Fiselier1e655162016-10-25 19:33:14 +000066 ~__libcpp_refstring();
67
68 const char* c_str() const _NOEXCEPT {return __imp_;}
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000069};
Eric Fiselierc6e689b2019-03-06 20:31:57 +000070#endif // !_LIBCPP_ABI_VCRUNTIME
Eric Fiselier1e655162016-10-25 19:33:14 +000071
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000072_LIBCPP_END_NAMESPACE_STD
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000073
Howard Hinnantc51e1022010-05-11 19:42:16 +000074namespace std // purposefully not using versioning namespace
75{
76
77class _LIBCPP_EXCEPTION_ABI logic_error
78 : public exception
79{
Eric Fiselierc6e689b2019-03-06 20:31:57 +000080#ifndef _LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +000081private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000082 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +000083public:
84 explicit logic_error(const string&);
85 explicit logic_error(const char*);
86
Howard Hinnant1f098bc2011-05-26 19:48:01 +000087 logic_error(const logic_error&) _NOEXCEPT;
88 logic_error& operator=(const logic_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000089
Howard Hinnant1f098bc2011-05-26 19:48:01 +000090 virtual ~logic_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000091
Howard Hinnant1f098bc2011-05-26 19:48:01 +000092 virtual const char* what() const _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +000093#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
Howard Hinnantc51e1022010-05-11 19:42:16 +000098};
99
100class _LIBCPP_EXCEPTION_ABI runtime_error
101 : public exception
102{
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000103#ifndef _LIBCPP_ABI_VCRUNTIME
Howard Hinnantc51e1022010-05-11 19:42:16 +0000104private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +0000105 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000106public:
107 explicit runtime_error(const string&);
108 explicit runtime_error(const char*);
109
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000110 runtime_error(const runtime_error&) _NOEXCEPT;
111 runtime_error& operator=(const runtime_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000112
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000113 virtual ~runtime_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000114
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000115 virtual const char* what() const _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000116#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
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121};
122
123class _LIBCPP_EXCEPTION_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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000130#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100131 domain_error(const domain_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000132 virtual ~domain_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000133#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134};
135
136class _LIBCPP_EXCEPTION_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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000143#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100144 invalid_argument(const invalid_argument&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000145 virtual ~invalid_argument() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000146#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147};
148
149class _LIBCPP_EXCEPTION_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) {}
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000155#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100156 length_error(const length_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000157 virtual ~length_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000158#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000159};
160
161class _LIBCPP_EXCEPTION_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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000168#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100169 out_of_range(const out_of_range&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000170 virtual ~out_of_range() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000171#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172};
173
174class _LIBCPP_EXCEPTION_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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000181#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100182 range_error(const range_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000183 virtual ~range_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000184#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185};
186
187class _LIBCPP_EXCEPTION_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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000194#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100195 overflow_error(const overflow_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000196 virtual ~overflow_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000197#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198};
199
200class _LIBCPP_EXCEPTION_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
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000207#ifndef _LIBCPP_ABI_VCRUNTIME
Dimitry Andric47269ce2020-03-13 19:36:26 +0100208 underflow_error(const underflow_error&) _NOEXCEPT = default;
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000209 virtual ~underflow_error() _NOEXCEPT;
Eric Fiselierc6e689b2019-03-06 20:31:57 +0000210#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211};
212
Nikolas Klauserd26407a2021-12-02 14:12:51 +0100213} // namespace std
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214
Marshall Clow8fea1612016-08-25 15:09:01 +0000215_LIBCPP_BEGIN_NAMESPACE_STD
216
217// in the dylib
218_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
219
Louis Dionne16fe2952018-07-11 23:14:33 +0000220_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000221void __throw_logic_error(const char*__msg)
222{
223#ifndef _LIBCPP_NO_EXCEPTIONS
224 throw logic_error(__msg);
225#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000226 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000227 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000228#endif
229}
230
Louis Dionne16fe2952018-07-11 23:14:33 +0000231_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000232void __throw_domain_error(const char*__msg)
233{
234#ifndef _LIBCPP_NO_EXCEPTIONS
235 throw domain_error(__msg);
236#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000237 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000238 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000239#endif
240}
241
Louis Dionne16fe2952018-07-11 23:14:33 +0000242_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000243void __throw_invalid_argument(const char*__msg)
244{
245#ifndef _LIBCPP_NO_EXCEPTIONS
246 throw invalid_argument(__msg);
247#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000248 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000249 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000250#endif
251}
252
Louis Dionne16fe2952018-07-11 23:14:33 +0000253_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000254void __throw_length_error(const char*__msg)
255{
256#ifndef _LIBCPP_NO_EXCEPTIONS
257 throw length_error(__msg);
258#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000259 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000260 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000261#endif
262}
263
Louis Dionne16fe2952018-07-11 23:14:33 +0000264_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000265void __throw_out_of_range(const char*__msg)
266{
267#ifndef _LIBCPP_NO_EXCEPTIONS
268 throw out_of_range(__msg);
269#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000270 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000271 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000272#endif
273}
274
Louis Dionne16fe2952018-07-11 23:14:33 +0000275_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000276void __throw_range_error(const char*__msg)
277{
278#ifndef _LIBCPP_NO_EXCEPTIONS
279 throw range_error(__msg);
280#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000281 ((void)__msg);
Louis Dionne44bcff92018-08-03 22:36:53 +0000282 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000283#endif
284}
285
Louis Dionne16fe2952018-07-11 23:14:33 +0000286_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000287void __throw_overflow_error(const char*__msg)
288{
289#ifndef _LIBCPP_NO_EXCEPTIONS
290 throw overflow_error(__msg);
291#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000292 ((void)__msg);
293 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000294#endif
295}
296
Louis Dionne16fe2952018-07-11 23:14:33 +0000297_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow8fea1612016-08-25 15:09:01 +0000298void __throw_underflow_error(const char*__msg)
299{
300#ifndef _LIBCPP_NO_EXCEPTIONS
301 throw underflow_error(__msg);
302#else
Eric Fiselier6003c772016-12-23 23:37:52 +0000303 ((void)__msg);
304 _VSTD::abort();
Marshall Clow8fea1612016-08-25 15:09:01 +0000305#endif
306}
307
308_LIBCPP_END_NAMESPACE_STD
309
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400310#endif // _LIBCPP_STDEXCEPT