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;
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;
59 #include <__assert> // all public C++ headers provide the assertion handler
60 #include <__availability>
65 #include <type_traits>
67 #ifdef _LIBCPP_NO_EXCEPTIONS
71 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
72 # pragma GCC system_header
75 #if defined(_LIBCPP_ABI_VCRUNTIME)
76 #include <vcruntime_typeinfo.h>
79 namespace std // purposefully not using versioning namespace
83 #if defined(_LIBCPP_ABI_MICROSOFT)
85 class _LIBCPP_EXCEPTION_ABI type_info
87 type_info& operator=(const type_info&);
88 type_info(const type_info&);
91 const char *__undecorated_name;
92 const char __decorated_name[1];
95 int __compare(const type_info &__rhs) const _NOEXCEPT;
98 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
101 const char *name() const _NOEXCEPT;
103 _LIBCPP_INLINE_VISIBILITY
104 bool before(const type_info& __arg) const _NOEXCEPT {
105 return __compare(__arg) < 0;
108 size_t hash_code() const _NOEXCEPT;
110 _LIBCPP_INLINE_VISIBILITY
111 bool operator==(const type_info& __arg) const _NOEXCEPT {
112 return __compare(__arg) == 0;
115 _LIBCPP_INLINE_VISIBILITY
116 bool operator!=(const type_info& __arg) const _NOEXCEPT
117 { return !operator==(__arg); }
120 #else // !defined(_LIBCPP_ABI_MICROSOFT)
122 // ========================================================================== //
124 // ========================================================================== //
125 // ------------------------------------------------------------------------- //
127 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
128 // ------------------------------------------------------------------------- //
129 // This implementation of type_info assumes a unique copy of the RTTI for a
130 // given type inside a program. This is a valid assumption when abiding to the
131 // Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
132 // Under this assumption, we can always compare the addresses of the type names
133 // to implement equality-comparison of type_infos instead of having to perform
134 // a deep string comparison.
135 // -------------------------------------------------------------------------- //
137 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
138 // -------------------------------------------------------------------------- //
139 // This implementation of type_info does not assume there is always a unique
140 // copy of the RTTI for a given type inside a program. For various reasons
141 // the linker may have failed to merge every copy of a types RTTI
142 // (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
143 // type_infos are equal if their addresses are equal or if a deep string
144 // comparison is equal.
145 // -------------------------------------------------------------------------- //
146 // NonUniqueARMRTTIBit
147 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
148 // -------------------------------------------------------------------------- //
149 // This implementation is specific to ARM64 on Apple platforms.
151 // This implementation of type_info does not assume always a unique copy of
152 // the RTTI for a given type inside a program. When constructing the type_info,
153 // the compiler packs the pointer to the type name into a uintptr_t and reserves
154 // the high bit of that pointer, which is assumed to be free for use under that
155 // ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
156 // to be unique within the program. If the high bit is unset, then the RTTI can
157 // be assumed to be unique within the program.
159 // When comparing type_infos, if both RTTIs can be assumed to be unique, it
160 // suffices to compare their addresses. If both the RTTIs can't be assumed to
161 // be unique, we must perform a deep string comparison of the type names.
162 // However, if one of the RTTIs is guaranteed unique and the other one isn't,
163 // then both RTTIs are necessarily not to be considered equal.
165 // The intent of this design is to remove the need for weak symbols. Specifically,
166 // if a type would normally have a default-visibility RTTI emitted as a weak
167 // symbol, it is given hidden visibility instead and the non-unique bit is set.
168 // Otherwise, types declared with hidden visibility are always considered to have
169 // a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
170 // to be deduplicated by the linker within the linked image. Across linked image
171 // boundaries, such types are thus considered different types.
173 // This value can be overriden in the __config_site. When it's not overriden,
174 // we pick a default implementation based on the platform here.
175 #ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
177 // Windows binaries can't merge typeinfos, so use the NonUnique implementation.
178 # ifdef _LIBCPP_OBJECT_FORMAT_COFF
179 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
181 // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
182 # elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
183 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
185 // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
187 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
191 struct __type_info_implementations {
192 struct __string_impl_base {
193 typedef const char* __type_name_t;
194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
195 _LIBCPP_CONSTEXPR static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
198 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
199 _LIBCPP_CONSTEXPR static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
204 struct __unique_impl : __string_impl_base {
205 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
206 static size_t __hash(__type_name_t __v) _NOEXCEPT {
207 return reinterpret_cast<size_t>(__v);
209 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
210 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
211 return __lhs == __rhs;
213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
214 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
215 return __lhs < __rhs;
219 struct __non_unique_impl : __string_impl_base {
220 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
221 static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
222 size_t __hash = 5381;
223 while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
224 __hash = (__hash * 33) ^ __c;
227 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
228 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
229 return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
231 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
232 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
233 return __builtin_strcmp(__lhs, __rhs) < 0;
237 struct __non_unique_arm_rtti_bit_impl {
238 typedef uintptr_t __type_name_t;
240 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
241 static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
242 return reinterpret_cast<const char*>(__v &
243 ~__non_unique_rtti_bit::value);
245 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
246 static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
247 return reinterpret_cast<__type_name_t>(__v);
250 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
251 static size_t __hash(__type_name_t __v) _NOEXCEPT {
252 if (__is_type_name_unique(__v))
254 return __non_unique_impl::__hash(__type_name_to_string(__v));
256 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
257 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
260 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
261 // Either both are unique and have a different address, or one of them
262 // is unique and the other one isn't. In both cases they are unequal.
264 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
266 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
267 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
268 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
269 return __lhs < __rhs;
270 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
274 // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
275 // this implementation is actually used.
276 typedef integral_constant<__type_name_t,
277 (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> __non_unique_rtti_bit;
279 _LIBCPP_INLINE_VISIBILITY
280 static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
281 return !(__lhs & __non_unique_rtti_bit::value);
286 #if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
288 #elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
290 #elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
291 __non_unique_arm_rtti_bit_impl
293 # error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
298 class _LIBCPP_EXCEPTION_ABI type_info
300 type_info& operator=(const type_info&);
301 type_info(const type_info&);
304 typedef __type_info_implementations::__impl __impl;
306 __impl::__type_name_t __type_name;
308 _LIBCPP_INLINE_VISIBILITY
309 explicit type_info(const char* __n)
310 : __type_name(__impl::__string_to_type_name(__n)) {}
313 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
314 virtual ~type_info();
316 _LIBCPP_INLINE_VISIBILITY
317 const char* name() const _NOEXCEPT
319 return __impl::__type_name_to_string(__type_name);
322 _LIBCPP_INLINE_VISIBILITY
323 bool before(const type_info& __arg) const _NOEXCEPT
325 return __impl::__lt(__type_name, __arg.__type_name);
328 _LIBCPP_INLINE_VISIBILITY
329 size_t hash_code() const _NOEXCEPT
331 return __impl::__hash(__type_name);
334 _LIBCPP_INLINE_VISIBILITY
335 bool operator==(const type_info& __arg) const _NOEXCEPT
337 return __impl::__eq(__type_name, __arg.__type_name);
340 _LIBCPP_INLINE_VISIBILITY
341 bool operator!=(const type_info& __arg) const _NOEXCEPT
342 { return !operator==(__arg); }
344 #endif // defined(_LIBCPP_ABI_MICROSOFT)
346 class _LIBCPP_EXCEPTION_ABI bad_cast
350 bad_cast() _NOEXCEPT;
351 bad_cast(const bad_cast&) _NOEXCEPT = default;
352 virtual ~bad_cast() _NOEXCEPT;
353 virtual const char* what() const _NOEXCEPT;
356 class _LIBCPP_EXCEPTION_ABI bad_typeid
360 bad_typeid() _NOEXCEPT;
361 virtual ~bad_typeid() _NOEXCEPT;
362 virtual const char* what() const _NOEXCEPT;
367 #endif // defined(_LIBCPP_ABI_VCRUNTIME)
369 _LIBCPP_BEGIN_NAMESPACE_STD
370 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
371 void __throw_bad_cast()
373 #ifndef _LIBCPP_NO_EXCEPTIONS
379 _LIBCPP_END_NAMESPACE_STD
381 #endif // __LIBCPP_TYPEINFO