// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef _LIBCPP_TYPEINFO #define _LIBCPP_TYPEINFO /* typeinfo synopsis namespace std { class type_info { public: virtual ~type_info(); bool operator==(const type_info& rhs) const noexcept; // constexpr since C++23 bool operator!=(const type_info& rhs) const noexcept; // removed in C++20 bool before(const type_info& rhs) const noexcept; size_t hash_code() const noexcept; const char* name() const noexcept; type_info(const type_info& rhs) = delete; type_info& operator=(const type_info& rhs) = delete; }; class bad_cast : public exception { public: bad_cast() noexcept; bad_cast(const bad_cast&) noexcept; bad_cast& operator=(const bad_cast&) noexcept; virtual const char* what() const noexcept; }; class bad_typeid : public exception { public: bad_typeid() noexcept; bad_typeid(const bad_typeid&) noexcept; bad_typeid& operator=(const bad_typeid&) noexcept; virtual const char* what() const noexcept; }; } // std */ #include <__config> #include <__exception/exception.h> #include <__type_traits/is_constant_evaluated.h> #include <__verbose_abort> #include <cstddef> #include <cstdint> #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header #endif #if defined(_LIBCPP_ABI_VCRUNTIME) # include <vcruntime_typeinfo.h> #else namespace std // purposefully not using versioning namespace { # if defined(_LIBCPP_ABI_MICROSOFT) class _LIBCPP_EXPORTED_FROM_ABI type_info { type_info& operator=(const type_info&); type_info(const type_info&); mutable struct { const char* __undecorated_name; const char __decorated_name[1]; } __data; int __compare(const type_info& __rhs) const _NOEXCEPT; public: virtual ~type_info(); const char* name() const _NOEXCEPT; _LIBCPP_HIDE_FROM_ABI bool before(const type_info& __arg) const _NOEXCEPT { return __compare(__arg) < 0; } size_t hash_code() const _NOEXCEPT; _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 bool operator==(const type_info& __arg) const _NOEXCEPT { // When evaluated in a constant expression, both type infos simply can't come // from different translation units, so it is sufficient to compare their addresses. if (__libcpp_is_constant_evaluated()) { return this == &__arg; } return __compare(__arg) == 0; } # if _LIBCPP_STD_VER <= 17 _LIBCPP_HIDE_FROM_ABI bool operator!=(const type_info& __arg) const _NOEXCEPT { return !operator==(__arg); } # endif }; # else // !defined(_LIBCPP_ABI_MICROSOFT) // ========================================================================== // // Implementations // ========================================================================== // // ------------------------------------------------------------------------- // // Unique // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 1) // ------------------------------------------------------------------------- // // This implementation of type_info assumes a unique copy of the RTTI for a // given type inside a program. This is a valid assumption when abiding to the // Itanium ABI (http://itanium-cxx-abi.github.io/cxx-abi/abi.html#vtable-components). // Under this assumption, we can always compare the addresses of the type names // to implement equality-comparison of type_infos instead of having to perform // a deep string comparison. // -------------------------------------------------------------------------- // // NonUnique // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 2) // -------------------------------------------------------------------------- // // This implementation of type_info does not assume there is always a unique // copy of the RTTI for a given type inside a program. For various reasons // the linker may have failed to merge every copy of a types RTTI // (For example: -Bsymbolic or llvm.org/PR37398). Under this assumption, two // type_infos are equal if their addresses are equal or if a deep string // comparison is equal. // -------------------------------------------------------------------------- // // NonUniqueARMRTTIBit // (_LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION = 3) // -------------------------------------------------------------------------- // // This implementation is specific to ARM64 on Apple platforms. // // This implementation of type_info does not assume always a unique copy of // the RTTI for a given type inside a program. When constructing the type_info, // the compiler packs the pointer to the type name into a uintptr_t and reserves // the high bit of that pointer, which is assumed to be free for use under that // ABI. If that high bit is set, that specific copy of the RTTI can't be assumed // to be unique within the program. If the high bit is unset, then the RTTI can // be assumed to be unique within the program. // // When comparing type_infos, if both RTTIs can be assumed to be unique, it // suffices to compare their addresses. If both the RTTIs can't be assumed to // be unique, we must perform a deep string comparison of the type names. // However, if one of the RTTIs is guaranteed unique and the other one isn't, // then both RTTIs are necessarily not to be considered equal. // // The intent of this design is to remove the need for weak symbols. Specifically, // if a type would normally have a default-visibility RTTI emitted as a weak // symbol, it is given hidden visibility instead and the non-unique bit is set. // Otherwise, types declared with hidden visibility are always considered to have // a unique RTTI: the RTTI is emitted with linkonce_odr linkage and is assumed // to be deduplicated by the linker within the linked image. Across linked image // boundaries, such types are thus considered different types. // This value can be overriden in the __config_site. When it's not overriden, // we pick a default implementation based on the platform here. # ifndef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION // Windows and AIX binaries can't merge typeinfos, so use the NonUnique implementation. # if defined(_LIBCPP_OBJECT_FORMAT_COFF) || defined(_LIBCPP_OBJECT_FORMAT_XCOFF) #define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION … // On arm64 on Apple platforms, use the special NonUniqueARMRTTIBit implementation. # elif defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__) #define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION … // On all other platforms, assume the Itanium C++ ABI and use the Unique implementation. # else #define _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION … # endif # endif struct __type_info_implementations { … }; # if __has_cpp_attribute(_Clang::__ptrauth_vtable_pointer__) # if __has_feature(ptrauth_type_info_vtable_pointer_discrimination) #define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH … # else #define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH … # endif # else #define _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH # endif class _LIBCPP_EXPORTED_FROM_ABI _LIBCPP_TYPE_INFO_VTABLE_POINTER_AUTH type_info { … }; # endif // defined(_LIBCPP_ABI_MICROSOFT) class _LIBCPP_EXPORTED_FROM_ABI bad_cast : public exception { … }; class _LIBCPP_EXPORTED_FROM_ABI bad_typeid : public exception { … }; } // namespace std #endif // defined(_LIBCPP_ABI_VCRUNTIME) #if defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0 namespace std { class bad_cast : public exception { public: bad_cast() _NOEXCEPT : exception("bad cast") {} private: bad_cast(const char* const __message) _NOEXCEPT : exception(__message) {} }; class bad_typeid : public exception { public: bad_typeid() _NOEXCEPT : exception("bad typeid") {} private: bad_typeid(const char* const __message) _NOEXCEPT : exception(__message) {} }; } // namespace std #endif // defined(_LIBCPP_ABI_VCRUNTIME) && _HAS_EXCEPTIONS == 0 _LIBCPP_BEGIN_NAMESPACE_STD _LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_bad_cast() { … } _LIBCPP_END_NAMESPACE_STD #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 # include <cstdlib> # include <type_traits> #endif #endif // _LIBCPP_TYPEINFO