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 <__assert> // all public C++ headers provide the assertion handler
60 #include <__availability>
62 #include <__exception/exception.h>
63 #include <__type_traits/is_constant_evaluated.h>
64 #include <__verbose_abort>
68 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
69 # pragma GCC system_header
72 #if defined(_LIBCPP_ABI_VCRUNTIME)
73 #include <vcruntime_typeinfo.h>
76 namespace std // purposefully not using versioning namespace
80 #if defined(_LIBCPP_ABI_MICROSOFT)
82 class _LIBCPP_EXPORTED_FROM_ABI type_info
84 type_info& operator=(const type_info&);
85 type_info(const type_info&);
88 const char *__undecorated_name;
89 const char __decorated_name[1];
92 int __compare(const type_info &__rhs) const _NOEXCEPT;
95 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
98 const char *name() const _NOEXCEPT;
100 _LIBCPP_INLINE_VISIBILITY
101 bool before(const type_info& __arg) const _NOEXCEPT {
102 return __compare(__arg) < 0;
105 size_t hash_code() const _NOEXCEPT;
107 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
108 bool operator==(const type_info& __arg) const _NOEXCEPT {
109 // When evaluated in a constant expression, both type infos simply can't come
110 // from different translation units, so it is sufficient to compare their addresses.
111 if (__libcpp_is_constant_evaluated()) {
112 return this == &__arg;
114 return __compare(__arg) == 0;
117 #if _LIBCPP_STD_VER <= 17
118 _LIBCPP_INLINE_VISIBILITY
119 bool operator!=(const type_info& __arg) const _NOEXCEPT
120 { return !operator==(__arg); }
124 #else // !defined(_LIBCPP_ABI_MICROSOFT)
126 // ========================================================================== //
128 // ========================================================================== //
129 // ------------------------------------------------------------------------- //
131 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
132 // ------------------------------------------------------------------------- //
133 // This implementation of type_info assumes a unique copy of the RTTI for a
134 // given type inside a program. This is a valid assumption when abiding to the
135 // Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
136 // Under this assumption, we can always compare the addresses of the type names
137 // to implement equality-comparison of type_infos instead of having to perform
138 // a deep string comparison.
139 // -------------------------------------------------------------------------- //
141 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
142 // -------------------------------------------------------------------------- //
143 // This implementation of type_info does not assume there is always a unique
144 // copy of the RTTI for a given type inside a program. For various reasons
145 // the linker may have failed to merge every copy of a types RTTI
146 // (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
147 // type_infos are equal if their addresses are equal or if a deep string
148 // comparison is equal.
149 // -------------------------------------------------------------------------- //
150 // NonUniqueARMRTTIBit
151 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
152 // -------------------------------------------------------------------------- //
153 // This implementation is specific to ARM64 on Apple platforms.
155 // This implementation of type_info does not assume always a unique copy of
156 // the RTTI for a given type inside a program. When constructing the type_info,
157 // the compiler packs the pointer to the type name into a uintptr_t and reserves
158 // the high bit of that pointer, which is assumed to be free for use under that
159 // ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
160 // to be unique within the program. If the high bit is unset, then the RTTI can
161 // be assumed to be unique within the program.
163 // When comparing type_infos, if both RTTIs can be assumed to be unique, it
164 // suffices to compare their addresses. If both the RTTIs can't be assumed to
165 // be unique, we must perform a deep string comparison of the type names.
166 // However, if one of the RTTIs is guaranteed unique and the other one isn't,
167 // then both RTTIs are necessarily not to be considered equal.
169 // The intent of this design is to remove the need for weak symbols. Specifically,
170 // if a type would normally have a default-visibility RTTI emitted as a weak
171 // symbol, it is given hidden visibility instead and the non-unique bit is set.
172 // Otherwise, types declared with hidden visibility are always considered to have
173 // a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
174 // to be deduplicated by the linker within the linked image. Across linked image
175 // boundaries, such types are thus considered different types.
177 // This value can be overriden in the __config_site. When it's not overriden,
178 // we pick a default implementation based on the platform here.
179 #ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
181 // Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation.
182 # if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)
183 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
185 // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
186 # elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
187 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
189 // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
191 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
195 struct __type_info_implementations {
196 struct __string_impl_base {
197 typedef const char* __type_name_t;
198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
199 _LIBCPP_CONSTEXPR static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
202 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
203 _LIBCPP_CONSTEXPR static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
208 struct __unique_impl : __string_impl_base {
209 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
210 static size_t __hash(__type_name_t __v) _NOEXCEPT {
211 return reinterpret_cast<size_t>(__v);
213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
214 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
215 return __lhs == __rhs;
217 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
218 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
219 return __lhs < __rhs;
223 struct __non_unique_impl : __string_impl_base {
224 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
225 static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
226 size_t __hash = 5381;
227 while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
228 __hash = (__hash * 33) ^ __c;
231 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
232 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
233 return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
235 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
236 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
237 return __builtin_strcmp(__lhs, __rhs) < 0;
241 struct __non_unique_arm_rtti_bit_impl {
242 typedef uintptr_t __type_name_t;
244 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
245 static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
246 return reinterpret_cast<const char*>(__v &
247 ~__non_unique_rtti_bit::value);
249 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
250 static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
251 return reinterpret_cast<__type_name_t>(__v);
254 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
255 static size_t __hash(__type_name_t __v) _NOEXCEPT {
256 if (__is_type_name_unique(__v))
258 return __non_unique_impl::__hash(__type_name_to_string(__v));
260 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
261 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
264 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
265 // Either both are unique and have a different address, or one of them
266 // is unique and the other one isn't. In both cases they are unequal.
268 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
270 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
271 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
272 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
273 return __lhs < __rhs;
274 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
278 // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
279 // this implementation is actually used.
280 typedef integral_constant<__type_name_t,
281 (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> __non_unique_rtti_bit;
283 _LIBCPP_INLINE_VISIBILITY
284 static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
285 return !(__lhs & __non_unique_rtti_bit::value);
290 #if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
292 #elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
294 #elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
295 __non_unique_arm_rtti_bit_impl
297 # error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
302 class _LIBCPP_EXPORTED_FROM_ABI type_info
304 type_info& operator=(const type_info&);
305 type_info(const type_info&);
308 typedef __type_info_implementations::__impl __impl;
310 __impl::__type_name_t __type_name;
312 _LIBCPP_INLINE_VISIBILITY
313 explicit type_info(const char* __n)
314 : __type_name(__impl::__string_to_type_name(__n)) {}
317 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
318 virtual ~type_info();
320 _LIBCPP_INLINE_VISIBILITY
321 const char* name() const _NOEXCEPT
323 return __impl::__type_name_to_string(__type_name);
326 _LIBCPP_INLINE_VISIBILITY
327 bool before(const type_info& __arg) const _NOEXCEPT
329 return __impl::__lt(__type_name, __arg.__type_name);
332 _LIBCPP_INLINE_VISIBILITY
333 size_t hash_code() const _NOEXCEPT
335 return __impl::__hash(__type_name);
338 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX23
339 bool operator==(const type_info& __arg) const _NOEXCEPT
341 // When evaluated in a constant expression, both type infos simply can't come
342 // from different translation units, so it is sufficient to compare their addresses.
343 if (__libcpp_is_constant_evaluated()) {
344 return this == &__arg;
346 return __impl::__eq(__type_name, __arg.__type_name);
349 #if _LIBCPP_STD_VER <= 17
350 _LIBCPP_INLINE_VISIBILITY
351 bool operator!=(const type_info& __arg) const _NOEXCEPT
352 { return !operator==(__arg); }
355 #endif // defined(_LIBCPP_ABI_MICROSOFT)
357 class _LIBCPP_EXPORTED_FROM_ABI bad_cast
361 bad_cast() _NOEXCEPT;
362 _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
363 _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
364 ~bad_cast() _NOEXCEPT override;
365 const char* what() const _NOEXCEPT override;
368 class _LIBCPP_EXPORTED_FROM_ABI bad_typeid
372 bad_typeid() _NOEXCEPT;
373 _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
374 _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
375 ~bad_typeid() _NOEXCEPT override;
376 const char* what() const _NOEXCEPT override;
381 #endif // defined(_LIBCPP_ABI_VCRUNTIME)
383 #if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
387 class bad_cast : public exception {
389 bad_cast() _NOEXCEPT : exception("bad cast") {}
392 bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {}
395 class bad_typeid : public exception {
397 bad_typeid() _NOEXCEPT : exception("bad typeid") {}
400 bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
405 #endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
407 _LIBCPP_BEGIN_NAMESPACE_STD
408 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
409 void __throw_bad_cast()
411 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
414 _LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode");
417 _LIBCPP_END_NAMESPACE_STD
419 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
421 # include <exception>
422 # include <type_traits>
425 #endif // _LIBCPP_TYPEINFO