2 //===-------------------------- typeinfo ----------------------------------===//
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;
25 bool operator!=(const type_info& rhs) const noexcept;
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;
63 #ifdef _LIBCPP_NO_EXCEPTIONS
67 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
68 #pragma GCC system_header
71 #if defined(_LIBCPP_ABI_VCRUNTIME)
72 #include <vcruntime_typeinfo.h>
75 namespace std // purposefully not using versioning namespace
79 #if defined(_LIBCPP_ABI_MICROSOFT)
81 class _LIBCPP_EXCEPTION_ABI type_info
83 type_info& operator=(const type_info&);
84 type_info(const type_info&);
87 const char *__undecorated_name;
88 const char __decorated_name[1];
91 int __compare(const type_info &__rhs) const _NOEXCEPT;
94 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
97 const char *name() const _NOEXCEPT;
99 _LIBCPP_INLINE_VISIBILITY
100 bool before(const type_info& __arg) const _NOEXCEPT {
101 return __compare(__arg) < 0;
104 size_t hash_code() const _NOEXCEPT;
106 _LIBCPP_INLINE_VISIBILITY
107 bool operator==(const type_info& __arg) const _NOEXCEPT {
108 return __compare(__arg) == 0;
111 _LIBCPP_INLINE_VISIBILITY
112 bool operator!=(const type_info& __arg) const _NOEXCEPT
113 { return !operator==(__arg); }
116 #else // !defined(_LIBCPP_ABI_MICROSOFT)
118 // ========================================================================== //
120 // ========================================================================== //
121 // ------------------------------------------------------------------------- //
123 // ------------------------------------------------------------------------- //
124 // This implementation of type_info assumes a unique copy of the RTTI for a
125 // given type inside a program. This is a valid assumption when abiding to
126 // Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
127 // Under this assumption, we can always compare the addresses of the type names
128 // to implement equality-comparison of type_infos instead of having to perform
129 // a deep string comparison.
130 // -------------------------------------------------------------------------- //
132 // -------------------------------------------------------------------------- //
133 // This implementation of type_info does not assume there is always a unique
134 // copy of the RTTI for a given type inside a program. For various reasons
135 // the linker may have failed to merge every copy of a types RTTI
136 // (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
137 // type_infos are equal if their addresses are equal or if a deep string
138 // comparison is equal.
139 // -------------------------------------------------------------------------- //
140 // NonUniqueARMRTTIBit
141 // -------------------------------------------------------------------------- //
142 // This implementation of type_info does not assume always a unique copy of
143 // the RTTI for a given type inside a program. It packs the pointer to the
144 // type name into a uintptr_t and reserves the high bit of that pointer (which
145 // is assumed to be free for use under the ABI in use) to represent whether
146 // that specific copy of the RTTI can be assumed unique inside the program.
147 // To implement equality-comparison of type_infos, we check whether BOTH
148 // type_infos are guaranteed unique, and if so, we simply compare the addresses
149 // of their type names instead of doing a deep string comparison, which is
150 // faster. If at least one of the type_infos can't guarantee uniqueness, we
151 // have no choice but to fall back to a deep string comparison.
153 // This implementation is specific to ARM64 on Apple platforms.
155 // Note that the compiler is the one setting (or unsetting) the high bit of
156 // the pointer when it constructs the type_info, depending on whether it can
157 // guarantee uniqueness for that specific type_info.
159 struct __type_info_implementations {
160 struct __string_impl_base {
161 typedef const char* __type_name_t;
162 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
163 _LIBCPP_CONSTEXPR static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
166 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
167 _LIBCPP_CONSTEXPR static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
172 struct __unique_impl : __string_impl_base {
173 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
174 static size_t __hash(__type_name_t __v) _NOEXCEPT {
175 return reinterpret_cast<size_t>(__v);
177 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
178 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
179 return __lhs == __rhs;
181 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
182 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
183 return __lhs < __rhs;
187 struct __non_unique_impl : __string_impl_base {
188 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
189 static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
190 size_t __hash = 5381;
191 while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
192 __hash = (__hash * 33) ^ __c;
195 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
196 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
197 return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
199 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
200 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
201 return __builtin_strcmp(__lhs, __rhs) < 0;
205 struct __non_unique_arm_rtti_bit_impl {
206 typedef uintptr_t __type_name_t;
208 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
209 static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
210 return reinterpret_cast<const char*>(__v &
211 ~__non_unique_rtti_bit::value);
213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
214 static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
215 return reinterpret_cast<__type_name_t>(__v);
218 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
219 static size_t __hash(__type_name_t __v) _NOEXCEPT {
220 if (__is_type_name_unique(__v))
221 return reinterpret_cast<size_t>(__v);
222 return __non_unique_impl::__hash(__type_name_to_string(__v));
224 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
225 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
228 if (__is_type_name_unique(__lhs, __rhs))
230 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
232 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
233 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
234 if (__is_type_name_unique(__lhs, __rhs))
235 return __lhs < __rhs;
236 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
240 // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
241 // this implementation is actually used.
242 typedef std::integral_constant<__type_name_t,
243 (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> __non_unique_rtti_bit;
245 _LIBCPP_INLINE_VISIBILITY
246 static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
247 return !(__lhs & __non_unique_rtti_bit::value);
249 _LIBCPP_INLINE_VISIBILITY
250 static bool __is_type_name_unique(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
251 return !((__lhs & __rhs) & __non_unique_rtti_bit::value);
256 #if defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
257 __non_unique_arm_rtti_bit_impl
258 #elif _LIBCPP_HAS_MERGED_TYPEINFO_NAMES_DEFAULT == 0
260 #elif _LIBCPP_HAS_MERGED_TYPEINFO_NAMES_DEFAULT == 1
263 # error invalid configuration for _LIBCPP_HAS_MERGED_TYPEINFO_NAMES_DEFAULT
268 class _LIBCPP_EXCEPTION_ABI type_info
270 type_info& operator=(const type_info&);
271 type_info(const type_info&);
274 typedef __type_info_implementations::__impl __impl;
276 __impl::__type_name_t __type_name;
278 _LIBCPP_INLINE_VISIBILITY
279 explicit type_info(const char* __n)
280 : __type_name(__impl::__string_to_type_name(__n)) {}
283 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
284 virtual ~type_info();
286 _LIBCPP_INLINE_VISIBILITY
287 const char* name() const _NOEXCEPT
289 return __impl::__type_name_to_string(__type_name);
292 _LIBCPP_INLINE_VISIBILITY
293 bool before(const type_info& __arg) const _NOEXCEPT
295 return __impl::__lt(__type_name, __arg.__type_name);
298 _LIBCPP_INLINE_VISIBILITY
299 size_t hash_code() const _NOEXCEPT
301 return __impl::__hash(__type_name);
304 _LIBCPP_INLINE_VISIBILITY
305 bool operator==(const type_info& __arg) const _NOEXCEPT
307 return __impl::__eq(__type_name, __arg.__type_name);
310 _LIBCPP_INLINE_VISIBILITY
311 bool operator!=(const type_info& __arg) const _NOEXCEPT
312 { return !operator==(__arg); }
314 #endif // defined(_LIBCPP_ABI_MICROSOFT)
316 class _LIBCPP_EXCEPTION_ABI bad_cast
320 bad_cast() _NOEXCEPT;
321 virtual ~bad_cast() _NOEXCEPT;
322 virtual const char* what() const _NOEXCEPT;
325 class _LIBCPP_EXCEPTION_ABI bad_typeid
329 bad_typeid() _NOEXCEPT;
330 virtual ~bad_typeid() _NOEXCEPT;
331 virtual const char* what() const _NOEXCEPT;
336 #endif // defined(_LIBCPP_ABI_VCRUNTIME)
338 _LIBCPP_BEGIN_NAMESPACE_STD
339 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
340 void __throw_bad_cast()
342 #ifndef _LIBCPP_NO_EXCEPTIONS
348 _LIBCPP_END_NAMESPACE_STD
350 #endif // __LIBCPP_TYPEINFO