Warning, /include/c++/v1/__cxx03/typeinfo is written in an unsupported language. File is not indexed.
0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009
0010 #ifndef _LIBCPP___CXX03_TYPEINFO
0011 #define _LIBCPP___CXX03_TYPEINFO
0012
0013 /*
0014
0015 typeinfo synopsis
0016
0017 namespace std {
0018
0019 class type_info
0020 {
0021 public:
0022 virtual ~type_info();
0023
0024 bool operator==(const type_info& rhs) const noexcept; // constexpr since C++23
0025 bool operator!=(const type_info& rhs) const noexcept; // removed in C++20
0026
0027 bool before(const type_info& rhs) const noexcept;
0028 size_t hash_code() const noexcept;
0029 const char* name() const noexcept;
0030
0031 type_info(const type_info& rhs) = delete;
0032 type_info& operator=(const type_info& rhs) = delete;
0033 };
0034
0035 class bad_cast
0036 : public exception
0037 {
0038 public:
0039 bad_cast() noexcept;
0040 bad_cast(const bad_cast&) noexcept;
0041 bad_cast& operator=(const bad_cast&) noexcept;
0042 virtual const char* what() const noexcept;
0043 };
0044
0045 class bad_typeid
0046 : public exception
0047 {
0048 public:
0049 bad_typeid() noexcept;
0050 bad_typeid(const bad_typeid&) noexcept;
0051 bad_typeid& operator=(const bad_typeid&) noexcept;
0052 virtual const char* what() const noexcept;
0053 };
0054
0055 } // std
0056
0057 */
0058
0059 #include <__cxx03/__config>
0060 #include <__cxx03/__exception/exception.h>
0061 #include <__cxx03/__type_traits/is_constant_evaluated.h>
0062 #include <__cxx03/__verbose_abort>
0063 #include <__cxx03/cstddef>
0064 #include <__cxx03/cstdint>
0065
0066 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0067 # pragma GCC system_header
0068 #endif
0069
0070 #if defined(_LIBCPP_ABI_VCRUNTIME)
0071 # include <__cxx03/vcruntime_typeinfo.h>
0072 #else
0073
0074 namespace std // purposefully not using versioning namespace
0075 {
0076
0077 # if defined(_LIBCPP_ABI_MICROSOFT)
0078
0079 class _LIBCPP_EXPORTED_FROM_ABI type_info {
0080 type_info& operator=(const type_info&);
0081 type_info(const type_info&);
0082
0083 mutable struct {
0084 const char* __undecorated_name;
0085 const char __decorated_name[1];
0086 } __data;
0087
0088 int __compare(const type_info& __rhs) const _NOEXCEPT;
0089
0090 public:
0091 virtual ~type_info();
0092
0093 const char* name() const _NOEXCEPT;
0094
0095 _LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT { return __compare(__arg) < 0; }
0096
0097 size_t hash_code() const _NOEXCEPT;
0098
0099 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
0100 // When evaluated in a constant expression, both type infos simply can't come
0101 // from different translation units, so it is sufficient to compare their addresses.
0102 if (__libcpp_is_constant_evaluated()) {
0103 return this == &__arg;
0104 }
0105 return __compare(__arg) == 0;
0106 }
0107
0108 # if _LIBCPP_STD_VER <= 17
0109 _LIBCPP_HIDE_FROM_ABI bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
0110 # endif
0111 };
0112
0113 # else // !defined(_LIBCPP_ABI_MICROSOFT)
0114
0115 // ========================================================================== //
0116 // Implementations
0117 // ========================================================================== //
0118 // ------------------------------------------------------------------------- //
0119 // Unique
0120 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1)
0121 // ------------------------------------------------------------------------- //
0122 // This implementation of type_info assumes a unique copy of the RTTI for a
0123 // given type inside a program. This is a valid assumption when abiding to the
0124 // Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components).
0125 // Under this assumption, we can always compare the addresses of the type names
0126 // to implement equality-comparison of type_infos instead of having to perform
0127 // a deep string comparison.
0128 // -------------------------------------------------------------------------- //
0129 // NonUnique
0130 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2)
0131 // -------------------------------------------------------------------------- //
0132 // This implementation of type_info does not assume there is always a unique
0133 // copy of the RTTI for a given type inside a program. For various reasons
0134 // the linker may have failed to merge every copy of a types RTTI
0135 // (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two
0136 // type_infos are equal if their addresses are equal or if a deep string
0137 // comparison is equal.
0138 // -------------------------------------------------------------------------- //
0139 // NonUniqueARMRTTIBit
0140 // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3)
0141 // -------------------------------------------------------------------------- //
0142 // This implementation is specific to ARM64 on Apple platforms.
0143 //
0144 // This implementation of type_info does not assume always a unique copy of
0145 // the RTTI for a given type inside a program. When constructing the type_info,
0146 // the compiler packs the pointer to the type name into a uintptr_t and reserves
0147 // the high bit of that pointer, which is assumed to be free for use under that
0148 // ABI. If that high bit is set, that specific copy of the RTTI can't be assumed
0149 // to be unique within the program. If the high bit is unset, then the RTTI can
0150 // be assumed to be unique within the program.
0151 //
0152 // When comparing type_infos, if both RTTIs can be assumed to be unique, it
0153 // suffices to compare their addresses. If both the RTTIs can't be assumed to
0154 // be unique, we must perform a deep string comparison of the type names.
0155 // However, if one of the RTTIs is guaranteed unique and the other one isn't,
0156 // then both RTTIs are necessarily not to be considered equal.
0157 //
0158 // The intent of this design is to remove the need for weak symbols. Specifically,
0159 // if a type would normally have a default-visibility RTTI emitted as a weak
0160 // symbol, it is given hidden visibility instead and the non-unique bit is set.
0161 // Otherwise, types declared with hidden visibility are always considered to have
0162 // a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed
0163 // to be deduplicated by the linker within the linked image. Across linked image
0164 // boundaries, such types are thus considered different types.
0165
0166 // This value can be overriden in the __config_site. When it's not overriden,
0167 // we pick a default implementation based on the platform here.
0168 # ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
0169
0170 // Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation.
0171 # if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF)
0172 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 2
0173
0174 // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation.
0175 # elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
0176 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 3
0177
0178 // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation.
0179 # else
0180 # define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION 1
0181 # endif
0182 # endif
0183
0184 struct __type_info_implementations {
0185 struct __string_impl_base {
0186 typedef const char* __type_name_t;
0187 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static const char*
0188 __type_name_to_string(__type_name_t __v) _NOEXCEPT {
0189 return __v;
0190 }
0191 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE _LIBCPP_CONSTEXPR static __type_name_t
0192 __string_to_type_name(const char* __v) _NOEXCEPT {
0193 return __v;
0194 }
0195 };
0196
0197 struct __unique_impl : __string_impl_base {
0198 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
0199 return reinterpret_cast<size_t>(__v);
0200 }
0201 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
0202 return __lhs == __rhs;
0203 }
0204 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
0205 return __lhs < __rhs;
0206 }
0207 };
0208
0209 struct __non_unique_impl : __string_impl_base {
0210 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
0211 size_t __hash = 5381;
0212 while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
0213 __hash = (__hash * 33) ^ __c;
0214 return __hash;
0215 }
0216 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
0217 return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
0218 }
0219 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
0220 return __builtin_strcmp(__lhs, __rhs) < 0;
0221 }
0222 };
0223
0224 struct __non_unique_arm_rtti_bit_impl {
0225 typedef uintptr_t __type_name_t;
0226
0227 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
0228 return reinterpret_cast<const char*>(__v & ~__non_unique_rtti_bit::value);
0229 }
0230 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
0231 return reinterpret_cast<__type_name_t>(__v);
0232 }
0233
0234 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static size_t __hash(__type_name_t __v) _NOEXCEPT {
0235 if (__is_type_name_unique(__v))
0236 return __v;
0237 return __non_unique_impl::__hash(__type_name_to_string(__v));
0238 }
0239 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
0240 if (__lhs == __rhs)
0241 return true;
0242 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
0243 // Either both are unique and have a different address, or one of them
0244 // is unique and the other one isn't. In both cases they are unequal.
0245 return false;
0246 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
0247 }
0248 _LIBCPP_HIDE_FROM_ABI _LIBCPP_ALWAYS_INLINE static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
0249 if (__is_type_name_unique(__lhs) || __is_type_name_unique(__rhs))
0250 return __lhs < __rhs;
0251 return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
0252 }
0253
0254 private:
0255 // The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
0256 // this implementation is actually used.
0257 typedef integral_constant<__type_name_t, (1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))>
0258 __non_unique_rtti_bit;
0259
0260 _LIBCPP_HIDE_FROM_ABI static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
0261 return !(__lhs & __non_unique_rtti_bit::value);
0262 }
0263 };
0264
0265 typedef
0266 # if _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 1
0267 __unique_impl
0268 # elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 2
0269 __non_unique_impl
0270 # elif _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION == 3
0271 __non_unique_arm_rtti_bit_impl
0272 # else
0273 # error invalid configuration for _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION
0274 # endif
0275 __impl;
0276 };
0277
0278 # if __has_cpp_attribute(_Clang::__ptrauth_vtable_pointer__)
0279 # if __has_feature(ptrauth_type_info_vtable_pointer_discrimination)
0280 # define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \
0281 [[_Clang::__ptrauth_vtable_pointer__(process_independent, address_discrimination, type_discrimination)]]
0282 # else
0283 # define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH \
0284 [[_Clang::__ptrauth_vtable_pointer__( \
0285 process_independent, no_address_discrimination, no_extra_discrimination)]]
0286 # endif
0287 # else
0288 # define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH
0289 # endif
0290
0291 class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH type_info {
0292 type_info& operator=(const type_info&);
0293 type_info(const type_info&);
0294
0295 protected:
0296 typedef __type_info_implementations::__impl __impl;
0297
0298 __impl::__type_name_t __type_name;
0299
0300 _LIBCPP_HIDE_FROM_ABI explicit type_info(const char* __n) : __type_name(__impl::__string_to_type_name(__n)) {}
0301
0302 public:
0303 virtual ~type_info();
0304
0305 _LIBCPP_HIDE_FROM_ABI const char* name() const _NOEXCEPT { return __impl::__type_name_to_string(__type_name); }
0306
0307 _LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT {
0308 return __impl::__lt(__type_name, __arg.__type_name);
0309 }
0310
0311 _LIBCPP_HIDE_FROM_ABI size_t hash_code() const _NOEXCEPT { return __impl::__hash(__type_name); }
0312
0313 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT {
0314 // When evaluated in a constant expression, both type infos simply can't come
0315 // from different translation units, so it is sufficient to compare their addresses.
0316 if (__libcpp_is_constant_evaluated()) {
0317 return this == &__arg;
0318 }
0319 return __impl::__eq(__type_name, __arg.__type_name);
0320 }
0321
0322 # if _LIBCPP_STD_VER <= 17
0323 _LIBCPP_HIDE_FROM_ABI bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); }
0324 # endif
0325 };
0326 # endif // defined(_LIBCPP_ABI_MICROSOFT)
0327
0328 class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception {
0329 public:
0330 bad_cast() _NOEXCEPT;
0331 _LIBCPP_HIDE_FROM_ABI bad_cast(const bad_cast&) _NOEXCEPT = default;
0332 _LIBCPP_HIDE_FROM_ABI bad_cast& operator=(const bad_cast&) _NOEXCEPT = default;
0333 ~bad_cast() _NOEXCEPT override;
0334 const char* what() const _NOEXCEPT override;
0335 };
0336
0337 class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception {
0338 public:
0339 bad_typeid() _NOEXCEPT;
0340 _LIBCPP_HIDE_FROM_ABI bad_typeid(const bad_typeid&) _NOEXCEPT = default;
0341 _LIBCPP_HIDE_FROM_ABI bad_typeid& operator=(const bad_typeid&) _NOEXCEPT = default;
0342 ~bad_typeid() _NOEXCEPT override;
0343 const char* what() const _NOEXCEPT override;
0344 };
0345
0346 } // namespace std
0347
0348 #endif // defined(_LIBCPP_ABI_VCRUNTIME)
0349
0350 #if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
0351
0352 namespace std {
0353
0354 class bad_cast : public exception {
0355 public:
0356 bad_cast() _NOEXCEPT : exception("bad cast") {}
0357
0358 private:
0359 bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {}
0360 };
0361
0362 class bad_typeid : public exception {
0363 public:
0364 bad_typeid() _NOEXCEPT : exception("bad typeid") {}
0365
0366 private:
0367 bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {}
0368 };
0369
0370 } // namespace std
0371
0372 #endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0
0373
0374 _LIBCPP_BEGIN_NAMESPACE_STD
0375 _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_cast() {
0376 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0377 throw bad_cast();
0378 #else
0379 _LIBCPP_VERBOSE_ABORT("bad_cast was thrown in -fno-exceptions mode");
0380 #endif
0381 }
0382 _LIBCPP_END_NAMESPACE_STD
0383
0384 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0385 # include <__cxx03/cstdlib>
0386 # include <__cxx03/type_traits>
0387 #endif
0388
0389 #endif // _LIBCPP___CXX03_TYPEINFO