| 1 |
// -*- C++ -*- |
| 2 |
//===----------------------------------------------------------------------===// |
| 3 |
// |
| 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 |
| 7 |
// |
| 8 |
//===----------------------------------------------------------------------===// |
| 9 |
|
| 10 |
#ifndef _LIBCPP___CXX03_TYPEINFO |
| 11 |
#define _LIBCPP___CXX03_TYPEINFO |
| 12 |
|
| 13 |
/* |
| 14 |
|
| 15 |
typeinfo synopsis |
| 16 |
|
| 17 |
namespace std { |
| 18 |
|
| 19 |
class type_info |
| 20 |
{ |
| 21 |
public: |
| 22 |
virtual ~type_info(); |
| 23 |
|
| 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 |
| 26 |
|
| 27 |
bool before(const type_info& rhs) const noexcept; |
| 28 |
size_t hash_code() const noexcept; |
| 29 |
const char* name() const noexcept; |
| 30 |
|
| 31 |
type_info(const type_info& rhs) = delete; |
| 32 |
type_info& operator=(const type_info& rhs) = delete; |
| 33 |
}; |
| 34 |
|
| 35 |
class bad_cast |
| 36 |
: public exception |
| 37 |
{ |
| 38 |
public: |
| 39 |
bad_cast() noexcept; |
| 40 |
bad_cast(const bad_cast&) noexcept; |
| 41 |
bad_cast& operator=(const bad_cast&) noexcept; |
| 42 |
virtual const char* what() const noexcept; |
| 43 |
}; |
| 44 |
|
| 45 |
class bad_typeid |
| 46 |
: public exception |
| 47 |
{ |
| 48 |
public: |
| 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; |
| 53 |
}; |
| 54 |
|
| 55 |
} // std |
| 56 |
|
| 57 |
*/ |
| 58 |
|
| 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> |
| 65 |
|
| 66 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 67 |
# pragma GCC system_header |
| 68 |
#endif |
| 69 |
|
| 70 |
#if defined(_LIBCPP_ABI_VCRUNTIME) |
| 71 |
# include <__cxx03/vcruntime_typeinfo.h> |
| 72 |
#else |
| 73 |
|
| 74 |
namespace std // purposefully not using versioning namespace |
| 75 |
{ |
| 76 |
|
| 77 |
# if defined(_LIBCPP_ABI_MICROSOFT) |
| 78 |
|
| 79 |
class _LIBCPP_EXPORTED_FROM_ABI type_info { |
| 80 |
type_info& operator=(const type_info&); |
| 81 |
type_info(const type_info&); |
| 82 |
|
| 83 |
mutable struct { |
| 84 |
const char* __undecorated_name; |
| 85 |
const char __decorated_name[1]; |
| 86 |
} __data; |
| 87 |
|
| 88 |
int __compare(const type_info& __rhs) const _NOEXCEPT; |
| 89 |
|
| 90 |
public: |
| 91 |
virtual ~type_info(); |
| 92 |
|
| 93 |
const char* name() const _NOEXCEPT; |
| 94 |
|
| 95 |
_LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT { return __compare(__arg) < 0; } |
| 96 |
|
| 97 |
size_t hash_code() const _NOEXCEPT; |
| 98 |
|
| 99 |
_LIBCPP_HIDE_FROM_ABI 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; |
| 104 |
} |
| 105 |
return __compare(__arg) == 0; |
| 106 |
} |
| 107 |
|
| 108 |
_LIBCPP_HIDE_FROM_ABI bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); } |
| 109 |
}; |
| 110 |
|
| 111 |
# else // !defined(_LIBCPP_ABI_MICROSOFT) |
| 112 |
|
| 113 |
// ========================================================================== // |
| 114 |
// Implementations |
| 115 |
// ========================================================================== // |
| 116 |
// ------------------------------------------------------------------------- // |
| 117 |
// Unique |
| 118 |
// (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1) |
| 119 |
// ------------------------------------------------------------------------- // |
| 120 |
// This implementation of type_info assumes a unique copy of the RTTI for a |
| 121 |
// given type inside a program. This is a valid assumption when abiding to the |
| 122 |
// Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components). |
| 123 |
// Under this assumption, we can always compare the addresses of the type names |
| 124 |
// to implement equality-comparison of type_infos instead of having to perform |
| 125 |
// a deep string comparison. |
| 126 |
// -------------------------------------------------------------------------- // |
| 127 |
// NonUnique |
| 128 |
// (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2) |
| 129 |
// -------------------------------------------------------------------------- // |
| 130 |
// This implementation of type_info does not assume there is always a unique |
| 131 |
// copy of the RTTI for a given type inside a program. For various reasons |
| 132 |
// the linker may have failed to merge every copy of a types RTTI |
| 133 |
// (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two |
| 134 |
// type_infos are equal if their addresses are equal or if a deep string |
| 135 |
// comparison is equal. |
| 136 |
// -------------------------------------------------------------------------- // |
| 137 |
// NonUniqueARMRTTIBit |
| 138 |
// (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3) |
| 139 |
// -------------------------------------------------------------------------- // |
| 140 |
// This implementation is specific to ARM64 on Apple platforms. |
| 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. When constructing the type_info, |
| 144 |
// the compiler packs the pointer to the type name into a uintptr_t and reserves |
| 145 |
// the high bit of that pointer, which is assumed to be free for use under that |
| 146 |
// ABI. If that high bit is set, that specific copy of the RTTI can't be assumed |
| 147 |
// to be unique within the program. If the high bit is unset, then the RTTI can |
| 148 |
// be assumed to be unique within the program. |
| 149 |
// |
| 150 |
// When comparing type_infos, if both RTTIs can be assumed to be unique, it |
| 151 |
// suffices to compare their addresses. If both the RTTIs can't be assumed to |
| 152 |
// be unique, we must perform a deep string comparison of the type names. |
| 153 |
// However, if one of the RTTIs is guaranteed unique and the other one isn't, |
| 154 |
// then both RTTIs are necessarily not to be considered equal. |
| 155 |
// |
| 156 |
// The intent of this design is to remove the need for weak symbols. Specifically, |
| 157 |
// if a type would normally have a default-visibility RTTI emitted as a weak |
| 158 |
// symbol, it is given hidden visibility instead and the non-unique bit is set. |
| 159 |
// Otherwise, types declared with hidden visibility are always considered to have |
| 160 |
// a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed |
| 161 |
// to be deduplicated by the linker within the linked image. Across linked image |
| 162 |
// boundaries, such types are thus considered different types. |
| 163 |
|
| 164 |
// This value can be overriden in the __config_site. When it's not overriden, |
| 165 |
// we pick a default implementation based on the platform here. |
| 166 |
# ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION |
| 167 |
|
| 168 |
// Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation. |
| 169 |
# if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF) |
| 170 |
# define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2 |
| 171 |
|
| 172 |
// On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation. |
| 173 |
# elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__) |
| 174 |
# define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3 |
| 175 |
|
| 176 |
// On all other platforms, assume the Itanium C++ ABI and use the Unique implementation. |
| 177 |
# else |
| 178 |
# define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1 |
| 179 |
# endif |
| 180 |
# endif |
| 181 |
|
| 182 |
struct __type_info_implementations { |
| 183 |
struct __string_impl_base { |
| 184 |
typedef const char* __type_name_t; |
| 185 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT { |
| 186 |
return __v; |
| 187 |
} |
| 188 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT { |
| 189 |
return __v; |
| 190 |
} |
| 191 |
}; |
| 192 |
|
| 193 |
struct __unique_impl : __string_impl_base { |
| 194 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT { |
| 195 |
return reinterpret_cast<size_t>(__v); |
| 196 |
} |
| 197 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT { |
| 198 |
return __lhs == __rhs; |
| 199 |
} |
| 200 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT { |
| 201 |
return __lhs < __rhs; |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
struct __non_unique_impl : __string_impl_base { |
| 206 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __ptr) _NOEXCEPT { |
| 207 |
size_t __hash = 5381; |
| 208 |
while (unsigned char __c = static_cast<unsigned char>(*__ptr++)) |
| 209 |
__hash = (__hash * 33) ^ __c; |
| 210 |
return __hash; |
| 211 |
} |
| 212 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT { |
| 213 |
return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0; |
| 214 |
} |
| 215 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT { |
| 216 |
return __builtin_strcmp(__lhs, __rhs) < 0; |
| 217 |
} |
| 218 |
}; |
| 219 |
|
| 220 |
struct __non_unique_arm_rtti_bit_impl { |
| 221 |
typedef uintptr_t __type_name_t; |
| 222 |
|
| 223 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT { |
| 224 |
return reinterpret_cast<const char*>(__v & ~__non_unique_rtti_bit::value); |
| 225 |
} |
| 226 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT { |
| 227 |
return reinterpret_cast<__type_name_t>(__v); |
| 228 |
} |
| 229 |
|
| 230 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT { |
| 231 |
if (__is_type_name_unique(__v)) |
| 232 |
return __v; |
| 233 |
return __non_unique_impl::__hash(__type_name_to_string(__v)); |
| 234 |
} |
| 235 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT { |
| 236 |
if (__lhs == __rhs) |
| 237 |
return true; |
| 238 |
if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs)) |
| 239 |
// Either both are unique and have a different address, or one of them |
| 240 |
// is unique and the other one isn't. In both cases they are unequal. |
| 241 |
return false; |
| 242 |
return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0; |
| 243 |
} |
| 244 |
_LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT { |
| 245 |
if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs)) |
| 246 |
return __lhs < __rhs; |
| 247 |
return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0; |
| 248 |
} |
| 249 |
|
| 250 |
private: |
| 251 |
// The unique bit is the top bit. It is expected that __type_name_t is 64 bits when |
| 252 |
// this implementation is actually used. |
| 253 |
typedef integral_constant<__type_name_t, (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> |
| 254 |
__non_unique_rtti_bit; |
| 255 |
|
| 256 |
_LIBCPP_HIDE_FROM_ABI static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT { |
| 257 |
return !(__lhs & __non_unique_rtti_bit::value); |
| 258 |
} |
| 259 |
}; |
| 260 |
|
| 261 |
typedef |
| 262 |
# if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1 |
| 263 |
__unique_impl |
| 264 |
# elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2 |
| 265 |
__non_unique_impl |
| 266 |
# elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3 |
| 267 |
__non_unique_arm_rtti_bit_impl |
| 268 |
# else |
| 269 |
# error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION |
| 270 |
# endif |
| 271 |
__impl; |
| 272 |
}; |
| 273 |
|
| 274 |
# if __has_cpp_attribute(_Clang::__ptrauth_vtable_pointer__) |
| 275 |
# if __has_feature(ptrauth_type_info_vtable_pointer_discrimination) |
| 276 |
# define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \ |
| 277 |
[[_Clang::__ptrauth_vtable_pointer__(process_independent, address_discrimination, type_discrimination)]] |
| 278 |
# else |
| 279 |
# define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \ |
| 280 |
[[_Clang::__ptrauth_vtable_pointer__( \ |
| 281 |
process_independent, no_address_discrimination, no_extra_discrimination)]] |
| 282 |
# endif |
| 283 |
# else |
| 284 |
# define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH |
| 285 |
# endif |
| 286 |
|
| 287 |
class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH type_info { |
| 288 |
type_info& operator=(const type_info&); |
| 289 |
type_info(const type_info&); |
| 290 |
|
| 291 |
protected: |
| 292 |
typedef __type_info_implementations::__impl __impl; |
| 293 |
|
| 294 |
__impl::__type_name_t __type_name; |
| 295 |
|
| 296 |
_LIBCPP_HIDE_FROM_ABI explicit type_info(const char* __n) : __type_name(__impl::__string_to_type_name(__n)) {} |
| 297 |
|
| 298 |
public: |
| 299 |
virtual ~type_info(); |
| 300 |
|
| 301 |
_LIBCPP_HIDE_FROM_ABI const char* name() const _NOEXCEPT { return __impl::__type_name_to_string(__type_name); } |
| 302 |
|
| 303 |
_LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT { |
| 304 |
return __impl::__lt(__type_name, __arg.__type_name); |
| 305 |
} |
| 306 |
|
| 307 |
_LIBCPP_HIDE_FROM_ABI size_t hash_code() const _NOEXCEPT { return __impl::__hash(__type_name); } |
| 308 |
|
| 309 |
_LIBCPP_HIDE_FROM_ABI bool operator==(const type_info& __arg) const _NOEXCEPT { |
| 310 |
// When evaluated in a constant expression, both type infos simply can't come |
| 311 |
// from different translation units, so it is sufficient to compare their addresses. |
| 312 |
if (__libcpp_is_constant_evaluated()) { |
| 313 |
return this == &__arg; |
| 314 |
} |
| 315 |
return __impl::__eq(__type_name, __arg.__type_name); |
| 316 |
} |
| 317 |
|
| 318 |
_LIBCPP_HIDE_FROM_ABI bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); } |
| 319 |
}; |
| 320 |
# endif // defined(_LIBCPP_ABI_MICROSOFT) |
| 321 |
|
| 322 |
class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception { |
| 323 |
public: |
| 324 |
bad_cast() _NOEXCEPT; |
| 325 |
_LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default; |
| 326 |
_LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default; |
| 327 |
~bad_cast() _NOEXCEPT override; |
| 328 |
const char* what() const _NOEXCEPT override; |
| 329 |
}; |
| 330 |
|
| 331 |
class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception { |
| 332 |
public: |
| 333 |
bad_typeid() _NOEXCEPT; |
| 334 |
_LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default; |
| 335 |
_LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default; |
| 336 |
~bad_typeid() _NOEXCEPT override; |
| 337 |
const char* what() const _NOEXCEPT override; |
| 338 |
}; |
| 339 |
|
| 340 |
} // namespace std |
| 341 |
|
| 342 |
#endif // defined(_LIBCPP_ABI_VCRUNTIME) |
| 343 |
|
| 344 |
#if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0 |
| 345 |
|
| 346 |
namespace std { |
| 347 |
|
| 348 |
class bad_cast : public exception { |
| 349 |
public: |
| 350 |
bad_cast() _NOEXCEPT : exception("bad cast") {} |
| 351 |
|
| 352 |
private: |
| 353 |
bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {} |
| 354 |
}; |
| 355 |
|
| 356 |
class bad_typeid : public exception { |
| 357 |
public: |
| 358 |
bad_typeid() _NOEXCEPT : exception("bad typeid") {} |
| 359 |
|
| 360 |
private: |
| 361 |
bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {} |
| 362 |
}; |
| 363 |
|
| 364 |
} // namespace std |
| 365 |
|
| 366 |
#endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0 |
| 367 |
|
| 368 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 369 |
_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_cast() { |
| 370 |
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS |
| 371 |
throw bad_cast(); |
| 372 |
#else |
| 373 |
_LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode"); |
| 374 |
#endif |
| 375 |
} |
| 376 |
_LIBCPP_END_NAMESPACE_STD |
| 377 |
|
| 378 |
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) |
| 379 |
# include <__cxx03/cstdlib> |
| 380 |
# include <__cxx03/type_traits> |
| 381 |
#endif |
| 382 |
|
| 383 |
#endif // _LIBCPP___CXX03_TYPEINFO |