| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef _LIBCPP___EXCEPTION_EXCEPTION_H |
| 10 | #define _LIBCPP___EXCEPTION_EXCEPTION_H |
| 11 | |
| 12 | #include <__config> |
| 13 | |
| 14 | // <vcruntime_exception.h> defines its own std::exception and std::bad_exception types, |
| 15 | // which we use in order to be ABI-compatible with other STLs on Windows. |
| 16 | #if defined(_LIBCPP_ABI_VCRUNTIME) |
| 17 | # include <vcruntime_exception.h> |
| 18 | #endif |
| 19 | |
| 20 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 21 | # pragma GCC system_header |
| 22 | #endif |
| 23 | |
| 24 | _LIBCPP_BEGIN_UNVERSIONED_NAMESPACE_STD |
| 25 | |
| 26 | #if defined(_LIBCPP_ABI_VCRUNTIME) && (!defined(_HAS_EXCEPTIONS) || _HAS_EXCEPTIONS != 0) |
| 27 | // The std::exception class was already included above, but we're explicit about this condition here for clarity. |
| 28 | |
| 29 | #elif defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0 |
| 30 | // However, <vcruntime_exception.h> does not define std::exception and std::bad_exception |
| 31 | // when _HAS_EXCEPTIONS == 0. |
| 32 | // |
| 33 | // Since libc++ still wants to provide the std::exception hierarchy even when _HAS_EXCEPTIONS == 0 |
| 34 | // (after all those are simply types like any other), we define an ABI-compatible version |
| 35 | // of the VCRuntime std::exception and std::bad_exception types in that mode. |
| 36 | |
| 37 | struct __std_exception_data { |
| 38 | char const* _What; |
| 39 | bool _DoFree; |
| 40 | }; |
| 41 | |
| 42 | class exception { // base of all library exceptions |
| 43 | public: |
| 44 | exception() _NOEXCEPT : __data_() {} |
| 45 | |
| 46 | explicit exception(char const* __message) _NOEXCEPT : __data_() { |
| 47 | __data_._What = __message; |
| 48 | __data_._DoFree = true; |
| 49 | } |
| 50 | |
| 51 | exception(exception const&) _NOEXCEPT {} |
| 52 | |
| 53 | exception& operator=(exception const&) _NOEXCEPT { return *this; } |
| 54 | |
| 55 | virtual ~exception() _NOEXCEPT {} |
| 56 | |
| 57 | virtual char const* what() const _NOEXCEPT { return __data_._What ? __data_._What : "Unknown exception" ; } |
| 58 | |
| 59 | private: |
| 60 | __std_exception_data __data_; |
| 61 | }; |
| 62 | |
| 63 | class bad_exception : public exception { |
| 64 | public: |
| 65 | bad_exception() _NOEXCEPT : exception("bad exception" ) {} |
| 66 | }; |
| 67 | |
| 68 | #else // !defined(_LIBCPP_ABI_VCRUNTIME) |
| 69 | // On all other platforms, we define our own std::exception and std::bad_exception types |
| 70 | // regardless of whether exceptions are turned on as a language feature. |
| 71 | |
| 72 | class _LIBCPP_EXPORTED_FROM_ABI exception { |
| 73 | public: |
| 74 | _LIBCPP_HIDE_FROM_ABI exception() _NOEXCEPT {} |
| 75 | _LIBCPP_HIDE_FROM_ABI exception(const exception&) _NOEXCEPT = default; |
| 76 | _LIBCPP_HIDE_FROM_ABI exception& operator=(const exception&) _NOEXCEPT = default; |
| 77 | |
| 78 | virtual ~exception() _NOEXCEPT; |
| 79 | virtual const char* what() const _NOEXCEPT; |
| 80 | }; |
| 81 | |
| 82 | class _LIBCPP_EXPORTED_FROM_ABI bad_exception : public exception { |
| 83 | public: |
| 84 | _LIBCPP_HIDE_FROM_ABI bad_exception() _NOEXCEPT {} |
| 85 | _LIBCPP_HIDE_FROM_ABI bad_exception(const bad_exception&) _NOEXCEPT = default; |
| 86 | _LIBCPP_HIDE_FROM_ABI bad_exception& operator=(const bad_exception&) _NOEXCEPT = default; |
| 87 | ~bad_exception() _NOEXCEPT override; |
| 88 | const char* what() const _NOEXCEPT override; |
| 89 | }; |
| 90 | #endif // !_LIBCPP_ABI_VCRUNTIME |
| 91 | |
| 92 | _LIBCPP_END_UNVERSIONED_NAMESPACE_STD |
| 93 | |
| 94 | #endif // _LIBCPP___EXCEPTION_EXCEPTION_H |
| 95 | |