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 <__availability>
64 #include <type_traits>
66 #ifdef _LIBCPP_NO_EXCEPTIONS
70 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
71 #pragma GCC system_header
74 #if defined(_LIBCPP_ABI_VCRUNTIME)
75 #include <vcruntime_typeinfo.h>
78 namespace std // purposefully not using versioning namespace
82 #if defined(_LIBCPP_ABI_MICROSOFT)
84 class _LIBCPP_EXCEPTION_ABI type_info
86 type_info& operator=(const type_info&);
87 type_info(const type_info&);
90 const char *__undecorated_name;
91 const char __decorated_name[1];
94 int __compare(const type_info &__rhs) const _NOEXCEPT;
97 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
100 const char *name() const _NOEXCEPT;
102 _LIBCPP_INLINE_VISIBILITY
103 bool before(const type_info& __arg) const _NOEXCEPT {
104 return __compare(__arg) < 0;
107 size_t hash_code() const _NOEXCEPT;
109 _LIBCPP_INLINE_VISIBILITY
110 bool operator==(const type_info& __arg) const _NOEXCEPT {
111 return __compare(__arg) == 0;
114 _LIBCPP_INLINE_VISIBILITY
115 bool operator!=(const type_info& __arg) const _NOEXCEPT
116 { return !operator==(__arg); }
119 #else // !defined(_LIBCPP_ABI_MICROSOFT)
121 // ========================================================================== //
123 // ========================================================================== //
124 // ------------------------------------------------------------------------- //
126 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
127 // ------------------------------------------------------------------------- //
128 // This implementation of type_info assumes a unique copy of the RTTI for a
129 // given type inside a program. This is a valid assumption when abiding to the
130 // Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
131 // Under this assumption, we can always compare the addresses of the type names
132 // to implement equality-comparison of type_infos instead of having to perform
133 // a deep string comparison.
134 // -------------------------------------------------------------------------- //
136 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
137 // -------------------------------------------------------------------------- //
138 // This implementation of type_info does not assume there is always a unique
139 // copy of the RTTI for a given type inside a program. For various reasons
140 // the linker may have failed to merge every copy of a types RTTI
141 // (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
142 // type_infos are equal if their addresses are equal or if a deep string
143 // comparison is equal.
144 // -------------------------------------------------------------------------- //
145 // NonUniqueARMRTTIBit
146 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
147 // -------------------------------------------------------------------------- //
148 // This implementation is specific to ARM64 on Apple platforms.
150 // This implementation of type_info does not assume always a unique copy of
151 // the RTTI for a given type inside a program. When constructing the type_info,
152 // the compiler packs the pointer to the type name into a uintptr_t and reserves
153 // the high bit of that pointer, which is assumed to be free for use under that
154 // ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
155 // to be unique within the program. If the high bit is unset, then the RTTI can
156 // be assumed to be unique within the program.
158 // When comparing type_infos, if both RTTIs can be assumed to be unique, it
159 // suffices to compare their addresses. If both the RTTIs can't be assumed to
160 // be unique, we must perform a deep string comparison of the type names.
161 // However, if one of the RTTIs is guaranteed unique and the other one isn't,
162 // then both RTTIs are necessarily not to be considered equal.
164 // The intent of this design is to remove the need for weak symbols. Specifically,
165 // if a type would normally have a default-visibility RTTI emitted as a weak
166 // symbol, it is given hidden visibility instead and the non-unique bit is set.
167 // Otherwise, types declared with hidden visibility are always considered to have
168 // a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
169 // to be deduplicated by the linker within the linked image. Across linked image
170 // boundaries, such types are thus considered different types.
172 // This value can be overriden in the __config_site. When it's not overriden,
173 // we pick a default implementation based on the platform here.
174 #ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
176 // Windows binaries can't merge typeinfos, so use the NonUnique implementation.
177 # ifdef _LIBCPP_OBJECT_FORMAT_COFF
178 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
180 // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
181 # elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
182 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
184 // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
186 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
190 struct __type_info_implementations {
191 struct __string_impl_base {
192 typedef const char* __type_name_t;
193 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
194 _LIBCPP_CONSTEXPR static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
197 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
198 _LIBCPP_CONSTEXPR static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
203 struct __unique_impl : __string_impl_base {
204 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
205 static size_t __hash(__type_name_t __v) _NOEXCEPT {
206 return reinterpret_cast<size_t>(__v);
208 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
209 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
210 return __lhs == __rhs;
212 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
213 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
214 return __lhs < __rhs;
218 struct __non_unique_impl : __string_impl_base {
219 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
220 static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
221 size_t __hash = 5381;
222 while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
223 __hash = (__hash * 33) ^ __c;
226 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
227 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
228 return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
230 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
231 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
232 return __builtin_strcmp(__lhs, __rhs) < 0;
236 struct __non_unique_arm_rtti_bit_impl {
237 typedef uintptr_t __type_name_t;
239 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
240 static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
241 return reinterpret_cast<const char*>(__v &
242 ~__non_unique_rtti_bit::value);
244 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
245 static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
246 return reinterpret_cast<__type_name_t>(__v);
249 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
250 static size_t __hash(__type_name_t __v) _NOEXCEPT {
251 if (__is_type_name_unique(__v))
253 return __non_unique_impl::__hash(__type_name_to_string(__v));
255 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
256 static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
259 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
260 // Either both are unique and have a different address, or one of them
261 // is unique and the other one isn't. In both cases they are unequal.
263 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
265 _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
266 static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
267 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
268 return __lhs < __rhs;
269 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
273 // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
274 // this implementation is actually used.
275 typedef integral_constant<__type_name_t,
276 (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> __non_unique_rtti_bit;
278 _LIBCPP_INLINE_VISIBILITY
279 static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
280 return !(__lhs & __non_unique_rtti_bit::value);
285 #if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
287 #elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
289 #elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
290 __non_unique_arm_rtti_bit_impl
292 # error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
297 class _LIBCPP_EXCEPTION_ABI type_info
299 type_info& operator=(const type_info&);
300 type_info(const type_info&);
303 typedef __type_info_implementations::__impl __impl;
305 __impl::__type_name_t __type_name;
307 _LIBCPP_INLINE_VISIBILITY
308 explicit type_info(const char* __n)
309 : __type_name(__impl::__string_to_type_name(__n)) {}
312 _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
313 virtual ~type_info();
315 _LIBCPP_INLINE_VISIBILITY
316 const char* name() const _NOEXCEPT
318 return __impl::__type_name_to_string(__type_name);
321 _LIBCPP_INLINE_VISIBILITY
322 bool before(const type_info& __arg) const _NOEXCEPT
324 return __impl::__lt(__type_name, __arg.__type_name);
327 _LIBCPP_INLINE_VISIBILITY
328 size_t hash_code() const _NOEXCEPT
330 return __impl::__hash(__type_name);
333 _LIBCPP_INLINE_VISIBILITY
334 bool operator==(const type_info& __arg) const _NOEXCEPT
336 return __impl::__eq(__type_name, __arg.__type_name);
339 _LIBCPP_INLINE_VISIBILITY
340 bool operator!=(const type_info& __arg) const _NOEXCEPT
341 { return !operator==(__arg); }
343 #endif // defined(_LIBCPP_ABI_MICROSOFT)
345 class _LIBCPP_EXCEPTION_ABI bad_cast
349 bad_cast() _NOEXCEPT;
350 bad_cast(const bad_cast&) _NOEXCEPT = default;
351 virtual ~bad_cast() _NOEXCEPT;
352 virtual const char* what() const _NOEXCEPT;
355 class _LIBCPP_EXCEPTION_ABI bad_typeid
359 bad_typeid() _NOEXCEPT;
360 virtual ~bad_typeid() _NOEXCEPT;
361 virtual const char* what() const _NOEXCEPT;
366 #endif // defined(_LIBCPP_ABI_VCRUNTIME)
368 _LIBCPP_BEGIN_NAMESPACE_STD
369 _LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
370 void __throw_bad_cast()
372 #ifndef _LIBCPP_NO_EXCEPTIONS
378 _LIBCPP_END_NAMESPACE_STD
380 #endif // __LIBCPP_TYPEINFO