2 //===----------------------------------------------------------------------===//
4 // 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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_TYPEINFO
11 #define _LIBCPP_TYPEINFO
24 bool operator==(const type_info& rhs) const noexcept; // constexpr since C++23
25 bool operator!=(const type_info& rhs) const noexcept; // removed in C++20
27 bool before(const type_info& rhs) const noexcept;
28 size_t hash_code() const noexcept;
29 const char* name() const noexcept;
31 type_info(const type_info& rhs) = delete;
32 type_info& operator=(const type_info& rhs) = delete;
40 bad_cast(const bad_cast&) noexcept;
41 bad_cast& operator=(const bad_cast&) noexcept;
42 virtual const char* what() const noexcept;
49 bad_typeid() noexcept;
50 bad_typeid(const bad_typeid&) noexcept;
51 bad_typeid& operator=(const bad_typeid&) noexcept;
52 virtual const char* what() const noexcept;
59 #include <__cxx03/__config>
60 #include <__cxx03/__exception/exception.h>
61 #include <__cxx03/__type_traits/is_constant_evaluated.h>
62 #include <__cxx03/__verbose_abort>
63 #include <__cxx03/cstddef>
64 #include <__cxx03/cstdint>
66 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
67 # pragma GCC system_header
70 #if defined(_LIBCPP_ABI_VCRUNTIME)
71 # include <__cxx03/vcruntime_typeinfo.h>
74 namespace std // purposefully not using versioning namespace
77 # if defined(_LIBCPP_ABI_MICROSOFT)
79 class _LIBCPP_EXPORTED_FROM_ABI type_info {
80 type_info& operator=(const type_info&);
81 type_info(const type_info&);
84 const char* __undecorated_name;
85 const char __decorated_name[1];
88 int __compare(const type_info& __rhs) const _NOEXCEPT;
93 const char* name() const _NOEXCEPT;
95 _LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT { return __compare(__arg) < 0; }
97 size_t hash_code() const _NOEXCEPT;
99 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
100 // When evaluated in a constant expression, both type infos simply can't come
101 // from different translation units, so it is sufficient to compare their addresses.
102 if (__libcpp_is_constant_evaluated()) {
103 return this == &__arg;
105 return __compare(__arg) == 0;
108 # if _LIBCPP_STD_VER <= 17
109 _LIBCPP_HIDE_FROM_ABI bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
113 # else // !defined(_LIBCPP_ABI_MICROSOFT)
115 // ========================================================================== //
117 // ========================================================================== //
118 // ------------------------------------------------------------------------- //
120 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
121 // ------------------------------------------------------------------------- //
122 // This implementation of type_info assumes a unique copy of the RTTI for a
123 // given type inside a program. This is a valid assumption when abiding to the
124 // Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
125 // Under this assumption, we can always compare the addresses of the type names
126 // to implement equality-comparison of type_infos instead of having to perform
127 // a deep string comparison.
128 // -------------------------------------------------------------------------- //
130 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
131 // -------------------------------------------------------------------------- //
132 // This implementation of type_info does not assume there is always a unique
133 // copy of the RTTI for a given type inside a program. For various reasons
134 // the linker may have failed to merge every copy of a types RTTI
135 // (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
136 // type_infos are equal if their addresses are equal or if a deep string
137 // comparison is equal.
138 // -------------------------------------------------------------------------- //
139 // NonUniqueARMRTTIBit
140 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
141 // -------------------------------------------------------------------------- //
142 // This implementation is specific to ARM64 on Apple platforms.
144 // This implementation of type_info does not assume always a unique copy of
145 // the RTTI for a given type inside a program. When constructing the type_info,
146 // the compiler packs the pointer to the type name into a uintptr_t and reserves
147 // the high bit of that pointer, which is assumed to be free for use under that
148 // ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
149 // to be unique within the program. If the high bit is unset, then the RTTI can
150 // be assumed to be unique within the program.
152 // When comparing type_infos, if both RTTIs can be assumed to be unique, it
153 // suffices to compare their addresses. If both the RTTIs can't be assumed to
154 // be unique, we must perform a deep string comparison of the type names.
155 // However, if one of the RTTIs is guaranteed unique and the other one isn't,
156 // then both RTTIs are necessarily not to be considered equal.
158 // The intent of this design is to remove the need for weak symbols. Specifically,
159 // if a type would normally have a default-visibility RTTI emitted as a weak
160 // symbol, it is given hidden visibility instead and the non-unique bit is set.
161 // Otherwise, types declared with hidden visibility are always considered to have
162 // a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
163 // to be deduplicated by the linker within the linked image. Across linked image
164 // boundaries, such types are thus considered different types.
166 // This value can be overriden in the __config_site. When it's not overriden,
167 // we pick a default implementation based on the platform here.
168 # ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
170 // Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation.
171 # if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)
172 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
174 // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
175 # elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
176 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
178 // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
180 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
184 struct __type_info_implementations {
185 struct __string_impl_base {
186 typedef const char* __type_name_t;
187 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static const char*
188 __type_name_to_string(__type_name_t __v) _NOEXCEPT {
191 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static __type_name_t
192 __string_to_type_name(const char* __v) _NOEXCEPT {
197 struct __unique_impl : __string_impl_base {
198 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
199 return reinterpret_cast<size_t>(__v);
201 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
202 return __lhs == __rhs;
204 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
205 return __lhs < __rhs;
209 struct __non_unique_impl : __string_impl_base {
210 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
211 size_t __hash = 5381;
212 while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
213 __hash = (__hash * 33) ^ __c;
216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
217 return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
219 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
220 return __builtin_strcmp(__lhs, __rhs) < 0;
224 struct __non_unique_arm_rtti_bit_impl {
225 typedef uintptr_t __type_name_t;
227 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
228 return reinterpret_cast<const char*>(__v & ~__non_unique_rtti_bit::value);
230 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
231 return reinterpret_cast<__type_name_t>(__v);
234 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
235 if (__is_type_name_unique(__v))
237 return __non_unique_impl::__hash(__type_name_to_string(__v));
239 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
242 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
243 // Either both are unique and have a different address, or one of them
244 // is unique and the other one isn't. In both cases they are unequal.
246 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
248 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
249 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
250 return __lhs < __rhs;
251 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
255 // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
256 // this implementation is actually used.
257 typedef integral_constant<__type_name_t, (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))>
258 __non_unique_rtti_bit;
260 _LIBCPP_HIDE_FROM_ABI static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
261 return !(__lhs & __non_unique_rtti_bit::value);
266 # if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
268 # elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
270 # elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
271 __non_unique_arm_rtti_bit_impl
273 # error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
278 # if __has_cpp_attribute(_Clang::__ptrauth_vtable_pointer__)
279 # if __has_feature(ptrauth_type_info_vtable_pointer_discrimination)
280 # define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \
281 [[_Clang::__ptrauth_vtable_pointer__(process_independent, address_discrimination, type_discrimination)]]
283 # define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \
284 [[_Clang::__ptrauth_vtable_pointer__( \
285 process_independent, no_address_discrimination, no_extra_discrimination)]]
288 # define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH
291 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH type_info {
292 type_info& operator=(const type_info&);
293 type_info(const type_info&);
296 typedef __type_info_implementations::__impl __impl;
298 __impl::__type_name_t __type_name;
300 _LIBCPP_HIDE_FROM_ABI explicit type_info(const char* __n) : __type_name(__impl::__string_to_type_name(__n)) {}
303 virtual ~type_info();
305 _LIBCPP_HIDE_FROM_ABI const char* name() const _NOEXCEPT { return __impl::__type_name_to_string(__type_name); }
307 _LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT {
308 return __impl::__lt(__type_name, __arg.__type_name);
311 _LIBCPP_HIDE_FROM_ABI size_t hash_code() const _NOEXCEPT { return __impl::__hash(__type_name); }
313 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
314 // When evaluated in a constant expression, both type infos simply can't come
315 // from different translation units, so it is sufficient to compare their addresses.
316 if (__libcpp_is_constant_evaluated()) {
317 return this == &__arg;
319 return __impl::__eq(__type_name, __arg.__type_name);
322 # if _LIBCPP_STD_VER <= 17
323 _LIBCPP_HIDE_FROM_ABI bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
326 # endif // defined(_LIBCPP_ABI_MICROSOFT)
328 class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
330 bad_cast() _NOEXCEPT;
331 _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
332 _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
333 ~bad_cast() _NOEXCEPT override;
334 const char* what() const _NOEXCEPT override;
337 class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
339 bad_typeid() _NOEXCEPT;
340 _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
341 _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
342 ~bad_typeid() _NOEXCEPT override;
343 const char* what() const _NOEXCEPT override;
348 #endif // defined(_LIBCPP_ABI_VCRUNTIME)
350 #if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
354 class bad_cast : public exception {
356 bad_cast() _NOEXCEPT : exception("bad cast") {}
359 bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {}
362 class bad_typeid : public exception {
364 bad_typeid() _NOEXCEPT : exception("bad typeid") {}
367 bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
372 #endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
374 _LIBCPP_BEGIN_NAMESPACE_STD
375 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_cast() {
376 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
379 _LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode");
382 _LIBCPP_END_NAMESPACE_STD
384 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
385 # include <__cxx03/cstdlib>
386 # include <__cxx03/type_traits>
389 #endif // _LIBCPP_TYPEINFO