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