1// -*- C++ -*-
2//===--------------------------- stdexcept --------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
8//
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);
36    explicit xxx_error(const char*   what_arg);
37
38    virtual const char* what() const noexcept // returns what_arg
39};
40
41}  // std
42
43*/
44
45#include <__config>
46#include <exception>
47#include <iosfwd>  // for string forward decl
48#ifdef _LIBCPP_NO_EXCEPTIONS
49#include <cstdlib>
50#endif
51
52#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
53#pragma GCC system_header
54#endif
55
56#ifndef _LIBCPP___REFSTRING
57_LIBCPP_BEGIN_NAMESPACE_STD
58class _LIBCPP_HIDDEN __libcpp_refstring {
59#ifdef __clang__
60    const char *__imp_ __attribute__((__unused__)); // only clang emits a warning
61#else
62    const char *__imp_;
63#endif
64};
65_LIBCPP_END_NAMESPACE_STD
66#endif
67
68namespace std  // purposefully not using versioning namespace
69{
70
71class _LIBCPP_EXCEPTION_ABI logic_error
72    : public exception
73{
74private:
75    _VSTD::__libcpp_refstring __imp_;
76public:
77    explicit logic_error(const string&);
78    explicit logic_error(const char*);
79
80    logic_error(const logic_error&) _NOEXCEPT;
81    logic_error& operator=(const logic_error&) _NOEXCEPT;
82
83    virtual ~logic_error() _NOEXCEPT;
84
85    virtual const char* what() const _NOEXCEPT;
86};
87
88class _LIBCPP_EXCEPTION_ABI runtime_error
89    : public exception
90{
91private:
92    _VSTD::__libcpp_refstring __imp_;
93public:
94    explicit runtime_error(const string&);
95    explicit runtime_error(const char*);
96
97    runtime_error(const runtime_error&) _NOEXCEPT;
98    runtime_error& operator=(const runtime_error&) _NOEXCEPT;
99
100    virtual ~runtime_error() _NOEXCEPT;
101
102    virtual const char* what() const _NOEXCEPT;
103};
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
112    virtual ~domain_error() _NOEXCEPT;
113};
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
122    virtual ~invalid_argument() _NOEXCEPT;
123};
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
132    virtual ~length_error() _NOEXCEPT;
133};
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
142    virtual ~out_of_range() _NOEXCEPT;
143};
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
152    virtual ~range_error() _NOEXCEPT;
153};
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
162    virtual ~overflow_error() _NOEXCEPT;
163};
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
172    virtual ~underflow_error() _NOEXCEPT;
173};
174
175}  // std
176
177_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
264#endif  // _LIBCPP_STDEXCEPT
265