2010-05-12 03:42:16 +08:00
|
|
|
// -*- C++ -*-
|
|
|
|
//===-------------------------- typeinfo ----------------------------------===//
|
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// 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
|
2010-05-12 03:42:16 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef __LIBCPP_TYPEINFO
|
|
|
|
#define __LIBCPP_TYPEINFO
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
typeinfo synopsis
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
class type_info
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual ~type_info();
|
2010-08-22 08:02:43 +08:00
|
|
|
|
2011-05-27 02:23:59 +08:00
|
|
|
bool operator==(const type_info& rhs) const noexcept;
|
|
|
|
bool operator!=(const type_info& rhs) const noexcept;
|
2010-08-22 08:02:43 +08:00
|
|
|
|
2011-05-27 02:23:59 +08:00
|
|
|
bool before(const type_info& rhs) const noexcept;
|
|
|
|
size_t hash_code() const noexcept;
|
|
|
|
const char* name() const noexcept;
|
2010-08-22 08:02:43 +08:00
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
type_info(const type_info& rhs) = delete;
|
|
|
|
type_info& operator=(const type_info& rhs) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
class bad_cast
|
|
|
|
: public exception
|
|
|
|
{
|
|
|
|
public:
|
2011-05-27 02:23:59 +08:00
|
|
|
bad_cast() noexcept;
|
|
|
|
bad_cast(const bad_cast&) noexcept;
|
|
|
|
bad_cast& operator=(const bad_cast&) noexcept;
|
|
|
|
virtual const char* what() const noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class bad_typeid
|
|
|
|
: public exception
|
|
|
|
{
|
|
|
|
public:
|
2011-05-27 02:23:59 +08:00
|
|
|
bad_typeid() noexcept;
|
|
|
|
bad_typeid(const bad_typeid&) noexcept;
|
|
|
|
bad_typeid& operator=(const bad_typeid&) noexcept;
|
|
|
|
virtual const char* what() const noexcept;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <__config>
|
|
|
|
#include <exception>
|
|
|
|
#include <cstddef>
|
2014-03-30 19:34:26 +08:00
|
|
|
#include <cstdint>
|
2020-02-14 23:38:16 +08:00
|
|
|
#include <type_traits>
|
2016-09-07 05:25:27 +08:00
|
|
|
#ifdef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
#include <cstdlib>
|
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2011-10-18 04:05:10 +08:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2010-05-12 03:42:16 +08:00
|
|
|
#pragma GCC system_header
|
2011-10-18 04:05:10 +08:00
|
|
|
#endif
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2019-03-05 09:57:01 +08:00
|
|
|
#if defined(_LIBCPP_ABI_VCRUNTIME)
|
2018-01-26 09:22:17 +08:00
|
|
|
#include <vcruntime_typeinfo.h>
|
|
|
|
#else
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
namespace std // purposefully not using versioning namespace
|
|
|
|
{
|
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
|
2019-02-02 04:00:13 +08:00
|
|
|
#if defined(_LIBCPP_ABI_MICROSOFT)
|
|
|
|
|
2010-08-22 08:02:43 +08:00
|
|
|
class _LIBCPP_EXCEPTION_ABI type_info
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
|
|
|
type_info& operator=(const type_info&);
|
|
|
|
type_info(const type_info&);
|
2016-08-30 04:43:38 +08:00
|
|
|
|
2017-09-15 13:42:39 +08:00
|
|
|
mutable struct {
|
|
|
|
const char *__undecorated_name;
|
|
|
|
const char __decorated_name[1];
|
|
|
|
} __data;
|
|
|
|
|
2017-09-15 14:19:31 +08:00
|
|
|
int __compare(const type_info &__rhs) const _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
|
|
|
|
public:
|
2017-05-05 01:08:54 +08:00
|
|
|
_LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
|
2010-05-12 03:42:16 +08:00
|
|
|
virtual ~type_info();
|
|
|
|
|
2017-09-15 13:42:39 +08:00
|
|
|
const char *name() const _NOEXCEPT;
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool before(const type_info& __arg) const _NOEXCEPT {
|
|
|
|
return __compare(__arg) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t hash_code() const _NOEXCEPT;
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator==(const type_info& __arg) const _NOEXCEPT {
|
|
|
|
return __compare(__arg) == 0;
|
|
|
|
}
|
2019-02-02 04:00:13 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator!=(const type_info& __arg) const _NOEXCEPT
|
|
|
|
{ return !operator==(__arg); }
|
|
|
|
};
|
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
#else // !defined(_LIBCPP_ABI_MICROSOFT)
|
2019-02-02 04:00:13 +08:00
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
// ========================================================================== //
|
|
|
|
// Implementations
|
|
|
|
// ========================================================================== //
|
|
|
|
// ------------------------------------------------------------------------- //
|
|
|
|
// Unique
|
|
|
|
// ------------------------------------------------------------------------- //
|
|
|
|
// 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
|
|
|
|
// 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
|
|
|
|
// -------------------------------------------------------------------------- //
|
|
|
|
// 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
|
|
|
|
// -------------------------------------------------------------------------- //
|
2019-02-02 04:00:13 +08:00
|
|
|
// This implementation of type_info does not assume always a unique copy of
|
|
|
|
// the RTTI for a given type inside a program. It 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 the ABI in use) to represent whether
|
|
|
|
// that specific copy of the RTTI can be assumed unique inside the program.
|
|
|
|
// To implement equality-comparison of type_infos, we check whether BOTH
|
|
|
|
// type_infos are guaranteed unique, and if so, we simply compare the addresses
|
|
|
|
// of their type names instead of doing a deep string comparison, which is
|
|
|
|
// faster. If at least one of the type_infos can't guarantee uniqueness, we
|
|
|
|
// have no choice but to fall back to a deep string comparison.
|
|
|
|
//
|
2019-05-29 10:21:37 +08:00
|
|
|
// This implementation is specific to ARM64 on Apple platforms.
|
|
|
|
//
|
2019-02-02 04:00:13 +08:00
|
|
|
// Note that the compiler is the one setting (or unsetting) the high bit of
|
|
|
|
// the pointer when it constructs the type_info, depending on whether it can
|
|
|
|
// guarantee uniqueness for that specific type_info.
|
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
struct __type_info_implementations {
|
|
|
|
struct __string_impl_base {
|
|
|
|
typedef const char* __type_name_t;
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
_LIBCPP_CONSTEXPR static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
|
|
|
|
return __v;
|
2017-01-06 05:22:22 +08:00
|
|
|
}
|
2019-05-29 10:21:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
_LIBCPP_CONSTEXPR static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
|
|
|
|
return __v;
|
2017-01-06 05:22:22 +08:00
|
|
|
}
|
2019-05-29 10:21:37 +08:00
|
|
|
};
|
2014-03-30 19:34:26 +08:00
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
struct __unique_impl : __string_impl_base {
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static size_t __hash(__type_name_t __v) _NOEXCEPT {
|
|
|
|
return reinterpret_cast<size_t>(__v);
|
|
|
|
}
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
|
|
|
|
return __lhs == __rhs;
|
|
|
|
}
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
|
|
|
|
return __lhs < __rhs;
|
|
|
|
}
|
|
|
|
};
|
2017-01-06 05:22:22 +08:00
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
struct __non_unique_impl : __string_impl_base {
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static size_t __hash(__type_name_t __ptr) _NOEXCEPT {
|
2017-01-06 05:22:22 +08:00
|
|
|
size_t __hash = 5381;
|
|
|
|
while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
|
|
|
|
__hash = (__hash * 33) ^ __c;
|
|
|
|
return __hash;
|
|
|
|
}
|
2019-05-29 10:21:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
|
|
|
|
return __lhs == __rhs || __builtin_strcmp(__lhs, __rhs) == 0;
|
|
|
|
}
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
|
|
|
|
return __builtin_strcmp(__lhs, __rhs) < 0;
|
|
|
|
}
|
|
|
|
};
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
struct __non_unique_arm_rtti_bit_impl {
|
|
|
|
typedef uintptr_t __type_name_t;
|
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static const char* __type_name_to_string(__type_name_t __v) _NOEXCEPT {
|
|
|
|
return reinterpret_cast<const char*>(__v &
|
|
|
|
~__non_unique_rtti_bit::value);
|
|
|
|
}
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static __type_name_t __string_to_type_name(const char* __v) _NOEXCEPT {
|
|
|
|
return reinterpret_cast<__type_name_t>(__v);
|
|
|
|
}
|
2017-01-06 05:22:22 +08:00
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static size_t __hash(__type_name_t __v) _NOEXCEPT {
|
|
|
|
if (__is_type_name_unique(__v))
|
|
|
|
return reinterpret_cast<size_t>(__v);
|
|
|
|
return __non_unique_impl::__hash(__type_name_to_string(__v));
|
|
|
|
}
|
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static bool __eq(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
|
|
|
|
if (__lhs == __rhs)
|
|
|
|
return true;
|
|
|
|
if (__is_type_name_unique(__lhs, __rhs))
|
2017-01-06 05:22:22 +08:00
|
|
|
return false;
|
2019-05-29 10:21:37 +08:00
|
|
|
return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) == 0;
|
2017-01-06 05:22:22 +08:00
|
|
|
}
|
2019-05-29 10:21:37 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
|
|
|
|
static bool __lt(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
|
|
|
|
if (__is_type_name_unique(__lhs, __rhs))
|
|
|
|
return __lhs < __rhs;
|
|
|
|
return __builtin_strcmp(__type_name_to_string(__lhs), __type_name_to_string(__rhs)) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-29 10:38:19 +08:00
|
|
|
// The unique bit is the top bit. It is expected that __type_name_t is 64 bits when
|
|
|
|
// this implementation is actually used.
|
|
|
|
typedef std::integral_constant<__type_name_t,
|
|
|
|
(1ULL << ((__CHAR_BIT__ * sizeof(__type_name_t)) - 1))> __non_unique_rtti_bit;
|
2019-02-02 04:00:13 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2019-05-29 10:21:37 +08:00
|
|
|
static bool __is_type_name_unique(__type_name_t __lhs) _NOEXCEPT {
|
|
|
|
return !(__lhs & __non_unique_rtti_bit::value);
|
|
|
|
}
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
static bool __is_type_name_unique(__type_name_t __lhs, __type_name_t __rhs) _NOEXCEPT {
|
|
|
|
return !((__lhs & __rhs) & __non_unique_rtti_bit::value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef
|
|
|
|
#if defined(__APPLE__) && defined(__LP64__) && !defined(__x86_64__)
|
|
|
|
__non_unique_arm_rtti_bit_impl
|
|
|
|
#elif _LIBCPP_HAS_MERGED_TYPEINFO_NAMES_DEFAULT == 0
|
|
|
|
__non_unique_impl
|
|
|
|
#elif _LIBCPP_HAS_MERGED_TYPEINFO_NAMES_DEFAULT == 1
|
|
|
|
__unique_impl
|
|
|
|
#else
|
|
|
|
# error invalid configuration for _LIBCPP_HAS_MERGED_TYPEINFO_NAMES_DEFAULT
|
|
|
|
#endif
|
|
|
|
__impl;
|
2019-02-02 04:00:13 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class _LIBCPP_EXCEPTION_ABI type_info
|
|
|
|
{
|
2019-05-29 10:21:37 +08:00
|
|
|
type_info& operator=(const type_info&);
|
|
|
|
type_info(const type_info&);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
typedef __type_info_implementations::__impl __impl;
|
2019-02-02 04:00:13 +08:00
|
|
|
|
2019-05-29 10:21:37 +08:00
|
|
|
__impl::__type_name_t __type_name;
|
2019-02-02 04:00:13 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2019-05-29 10:21:37 +08:00
|
|
|
explicit type_info(const char* __n)
|
|
|
|
: __type_name(__impl::__string_to_type_name(__n)) {}
|
2019-02-02 04:00:13 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
_LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
|
|
|
|
virtual ~type_info();
|
|
|
|
|
2010-09-24 02:58:28 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-01-06 05:22:22 +08:00
|
|
|
const char* name() const _NOEXCEPT
|
2019-05-29 10:21:37 +08:00
|
|
|
{
|
|
|
|
return __impl::__type_name_to_string(__type_name);
|
|
|
|
}
|
2010-05-12 03:42:16 +08:00
|
|
|
|
2014-03-30 19:34:26 +08:00
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
2017-01-06 05:22:22 +08:00
|
|
|
bool before(const type_info& __arg) const _NOEXCEPT
|
2019-05-29 10:21:37 +08:00
|
|
|
{
|
|
|
|
return __impl::__lt(__type_name, __arg.__type_name);
|
|
|
|
}
|
2017-01-06 05:22:22 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
size_t hash_code() const _NOEXCEPT
|
2019-05-29 10:21:37 +08:00
|
|
|
{
|
|
|
|
return __impl::__hash(__type_name);
|
|
|
|
}
|
2017-01-06 05:22:22 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator==(const type_info& __arg) const _NOEXCEPT
|
2019-05-29 10:21:37 +08:00
|
|
|
{
|
|
|
|
return __impl::__eq(__type_name, __arg.__type_name);
|
|
|
|
}
|
2017-01-06 05:22:22 +08:00
|
|
|
|
|
|
|
_LIBCPP_INLINE_VISIBILITY
|
|
|
|
bool operator!=(const type_info& __arg) const _NOEXCEPT
|
|
|
|
{ return !operator==(__arg); }
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
2019-05-29 10:21:37 +08:00
|
|
|
#endif // defined(_LIBCPP_ABI_MICROSOFT)
|
2019-02-02 04:00:13 +08:00
|
|
|
|
2010-05-15 04:19:37 +08:00
|
|
|
class _LIBCPP_EXCEPTION_ABI bad_cast
|
2010-08-22 08:02:43 +08:00
|
|
|
: public exception
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2019-05-29 10:21:37 +08:00
|
|
|
public:
|
|
|
|
bad_cast() _NOEXCEPT;
|
2020-03-14 02:36:26 +08:00
|
|
|
bad_cast(const bad_cast&) _NOEXCEPT = default;
|
2019-05-29 10:21:37 +08:00
|
|
|
virtual ~bad_cast() _NOEXCEPT;
|
|
|
|
virtual const char* what() const _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
2010-05-15 04:19:37 +08:00
|
|
|
class _LIBCPP_EXCEPTION_ABI bad_typeid
|
2010-08-22 08:02:43 +08:00
|
|
|
: public exception
|
2010-05-12 03:42:16 +08:00
|
|
|
{
|
2019-05-29 10:21:37 +08:00
|
|
|
public:
|
|
|
|
bad_typeid() _NOEXCEPT;
|
|
|
|
virtual ~bad_typeid() _NOEXCEPT;
|
|
|
|
virtual const char* what() const _NOEXCEPT;
|
2010-05-12 03:42:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // std
|
|
|
|
|
2019-03-05 09:57:01 +08:00
|
|
|
#endif // defined(_LIBCPP_ABI_VCRUNTIME)
|
2018-01-26 09:22:17 +08:00
|
|
|
|
2016-08-25 23:09:01 +08:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
2018-07-12 07:14:33 +08:00
|
|
|
_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
|
2016-08-25 23:09:01 +08:00
|
|
|
void __throw_bad_cast()
|
|
|
|
{
|
|
|
|
#ifndef _LIBCPP_NO_EXCEPTIONS
|
|
|
|
throw bad_cast();
|
|
|
|
#else
|
2018-08-04 06:36:53 +08:00
|
|
|
_VSTD::abort();
|
2016-08-25 23:09:01 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
2010-05-12 03:42:16 +08:00
|
|
|
#endif // __LIBCPP_TYPEINFO
|