blob: b78d5d60273bde207d2ac492ecf1f3ba947c5497 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- stdexcept --------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantc51e1022010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_STDEXCEPT
12#define _LIBCPP_STDEXCEPT
13
14/*
15 stdexcept synopsis
16
17namespace std
18{
19
20class logic_error;
21 class domain_error;
22 class invalid_argument;
23 class length_error;
24 class out_of_range;
25class runtime_error;
26 class range_error;
27 class overflow_error;
28 class underflow_error;
29
30for each class xxx_error:
31
32class xxx_error : public exception // at least indirectly
33{
34public:
35 explicit xxx_error(const string& what_arg);
Howard Hinnant1f098bc2011-05-26 19:48:01 +000036 explicit xxx_error(const char* what_arg);
Howard Hinnantc51e1022010-05-11 19:42:16 +000037
Howard Hinnant1f098bc2011-05-26 19:48:01 +000038 virtual const char* what() const noexcept // returns what_arg
Howard Hinnantc51e1022010-05-11 19:42:16 +000039};
40
41} // std
42
43*/
44
45#include <__config>
46#include <exception>
47#include <iosfwd> // for string forward decl
Eric Fiselier279c3192016-09-06 21:25:27 +000048#ifdef _LIBCPP_NO_EXCEPTIONS
49#include <cstdlib>
50#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000051
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000052#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +000053#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +000054#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000056#ifndef _LIBCPP___REFSTRING
57_LIBCPP_BEGIN_NAMESPACE_STD
58class _LIBCPP_HIDDEN __libcpp_refstring {
Eric Fiselier6585c752016-04-21 22:54:21 +000059#ifdef __clang__
60 const char *__imp_ __attribute__((__unused__)); // only clang emits a warning
61#else
62 const char *__imp_;
63#endif
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000064};
65_LIBCPP_END_NAMESPACE_STD
66#endif
67
Howard Hinnantc51e1022010-05-11 19:42:16 +000068namespace std // purposefully not using versioning namespace
69{
70
71class _LIBCPP_EXCEPTION_ABI logic_error
72 : public exception
73{
74private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000075 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +000076public:
77 explicit logic_error(const string&);
78 explicit logic_error(const char*);
79
Howard Hinnant1f098bc2011-05-26 19:48:01 +000080 logic_error(const logic_error&) _NOEXCEPT;
81 logic_error& operator=(const logic_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082
Howard Hinnant1f098bc2011-05-26 19:48:01 +000083 virtual ~logic_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
Howard Hinnant1f098bc2011-05-26 19:48:01 +000085 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000086};
87
88class _LIBCPP_EXCEPTION_ABI runtime_error
89 : public exception
90{
91private:
Joerg Sonnenbergera47c2e72014-04-30 19:54:11 +000092 _VSTD::__libcpp_refstring __imp_;
Howard Hinnantc51e1022010-05-11 19:42:16 +000093public:
94 explicit runtime_error(const string&);
95 explicit runtime_error(const char*);
96
Howard Hinnant1f098bc2011-05-26 19:48:01 +000097 runtime_error(const runtime_error&) _NOEXCEPT;
98 runtime_error& operator=(const runtime_error&) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +000099
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000100 virtual ~runtime_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000102 virtual const char* what() const _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000103};
104
105class _LIBCPP_EXCEPTION_ABI domain_error
106 : public logic_error
107{
108public:
109 _LIBCPP_INLINE_VISIBILITY explicit domain_error(const string& __s) : logic_error(__s) {}
110 _LIBCPP_INLINE_VISIBILITY explicit domain_error(const char* __s) : logic_error(__s) {}
111
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000112 virtual ~domain_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113};
114
115class _LIBCPP_EXCEPTION_ABI invalid_argument
116 : public logic_error
117{
118public:
119 _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const string& __s) : logic_error(__s) {}
120 _LIBCPP_INLINE_VISIBILITY explicit invalid_argument(const char* __s) : logic_error(__s) {}
121
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000122 virtual ~invalid_argument() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123};
124
125class _LIBCPP_EXCEPTION_ABI length_error
126 : public logic_error
127{
128public:
129 _LIBCPP_INLINE_VISIBILITY explicit length_error(const string& __s) : logic_error(__s) {}
130 _LIBCPP_INLINE_VISIBILITY explicit length_error(const char* __s) : logic_error(__s) {}
131
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000132 virtual ~length_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133};
134
135class _LIBCPP_EXCEPTION_ABI out_of_range
136 : public logic_error
137{
138public:
139 _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const string& __s) : logic_error(__s) {}
140 _LIBCPP_INLINE_VISIBILITY explicit out_of_range(const char* __s) : logic_error(__s) {}
141
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000142 virtual ~out_of_range() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143};
144
145class _LIBCPP_EXCEPTION_ABI range_error
146 : public runtime_error
147{
148public:
149 _LIBCPP_INLINE_VISIBILITY explicit range_error(const string& __s) : runtime_error(__s) {}
150 _LIBCPP_INLINE_VISIBILITY explicit range_error(const char* __s) : runtime_error(__s) {}
151
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000152 virtual ~range_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153};
154
155class _LIBCPP_EXCEPTION_ABI overflow_error
156 : public runtime_error
157{
158public:
159 _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const string& __s) : runtime_error(__s) {}
160 _LIBCPP_INLINE_VISIBILITY explicit overflow_error(const char* __s) : runtime_error(__s) {}
161
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000162 virtual ~overflow_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163};
164
165class _LIBCPP_EXCEPTION_ABI underflow_error
166 : public runtime_error
167{
168public:
169 _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const string& __s) : runtime_error(__s) {}
170 _LIBCPP_INLINE_VISIBILITY explicit underflow_error(const char* __s) : runtime_error(__s) {}
171
Howard Hinnant1f098bc2011-05-26 19:48:01 +0000172 virtual ~underflow_error() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173};
174
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175} // std
176
Marshall Clow8fea1612016-08-25 15:09:01 +0000177_LIBCPP_BEGIN_NAMESPACE_STD
178
179// in the dylib
180_LIBCPP_NORETURN _LIBCPP_FUNC_VIS void __throw_runtime_error(const char*);
181
182_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
183void __throw_logic_error(const char*__msg)
184{
185#ifndef _LIBCPP_NO_EXCEPTIONS
186 throw logic_error(__msg);
187#else
188 _VSTD::abort();
189#endif
190}
191
192_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
193void __throw_domain_error(const char*__msg)
194{
195#ifndef _LIBCPP_NO_EXCEPTIONS
196 throw domain_error(__msg);
197#else
198 _VSTD::abort();
199#endif
200}
201
202_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
203void __throw_invalid_argument(const char*__msg)
204{
205#ifndef _LIBCPP_NO_EXCEPTIONS
206 throw invalid_argument(__msg);
207#else
208 _VSTD::abort();
209#endif
210}
211
212_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
213void __throw_length_error(const char*__msg)
214{
215#ifndef _LIBCPP_NO_EXCEPTIONS
216 throw length_error(__msg);
217#else
218 _VSTD::abort();
219#endif
220}
221
222_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
223void __throw_out_of_range(const char*__msg)
224{
225#ifndef _LIBCPP_NO_EXCEPTIONS
226 throw out_of_range(__msg);
227#else
228 _VSTD::abort();
229#endif
230}
231
232_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
233void __throw_range_error(const char*__msg)
234{
235#ifndef _LIBCPP_NO_EXCEPTIONS
236 throw range_error(__msg);
237#else
238 _VSTD::abort();
239#endif
240}
241
242_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
243void __throw_overflow_error(const char*__msg)
244{
245#ifndef _LIBCPP_NO_EXCEPTIONS
246 throw overflow_error(__msg);
247#else
248 _VSTD::abort();
249#endif
250}
251
252_LIBCPP_NORETURN inline _LIBCPP_ALWAYS_INLINE
253void __throw_underflow_error(const char*__msg)
254{
255#ifndef _LIBCPP_NO_EXCEPTIONS
256 throw underflow_error(__msg);
257#else
258 _VSTD::abort();
259#endif
260}
261
262_LIBCPP_END_NAMESPACE_STD
263
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264#endif // _LIBCPP_STDEXCEPT