1// -*- C++ -*-
2//===-------------------------- typeinfo ----------------------------------===//
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_TYPEINFO
11#define __LIBCPP_TYPEINFO
12
13/*
14
15    typeinfo synopsis
16
17namespace std {
18
19class type_info
20{
21public:
22    virtual ~type_info();
23
24    bool operator==(const type_info& rhs) const noexcept;
25    bool operator!=(const type_info& rhs) const noexcept;
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
35class bad_cast
36    : public exception
37{
38public:
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
45class bad_typeid
46    : public exception
47{
48public:
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 <__config>
60#include <exception>
61#include <cstddef>
62#include <cstdint>
63#ifdef _LIBCPP_NO_EXCEPTIONS
64#include <cstdlib>
65#endif
66
67#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
68#pragma GCC system_header
69#endif
70
71#if defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
72#include <vcruntime_typeinfo.h>
73#else
74
75#if defined(_LIBCPP_NONUNIQUE_RTTI_BIT) && !defined(_LIBCPP_ABI_MICROSOFT)
76#   define _LIBCPP_HAS_NONUNIQUE_TYPEINFO
77#endif
78
79namespace std  // purposefully not using versioning namespace
80{
81
82class _LIBCPP_EXCEPTION_ABI type_info
83{
84    type_info& operator=(const type_info&);
85    type_info(const type_info&);
86
87#if defined(_LIBCPP_HAS_NONUNIQUE_TYPEINFO)
88    _LIBCPP_INLINE_VISIBILITY
89    int __compare_nonunique_names(const type_info &__arg) const _NOEXCEPT
90    { return __builtin_strcmp(name(), __arg.name()); }
91#endif
92
93#if defined(_LIBCPP_ABI_MICROSOFT)
94    mutable struct {
95      const char *__undecorated_name;
96      const char __decorated_name[1];
97    } __data;
98
99    int __compare(const type_info &__rhs) const _NOEXCEPT;
100#endif // _LIBCPP_ABI_MICROSOFT
101
102protected:
103#if !defined(_LIBCPP_ABI_MICROSOFT)
104#if defined(_LIBCPP_HAS_NONUNIQUE_TYPEINFO)
105    // A const char* with the non-unique RTTI bit possibly set.
106    uintptr_t __type_name;
107
108    _LIBCPP_INLINE_VISIBILITY
109    explicit type_info(const char* __n)
110      : __type_name(reinterpret_cast<uintptr_t>(__n)) {}
111#else
112    const char *__type_name;
113
114    _LIBCPP_INLINE_VISIBILITY
115    explicit type_info(const char* __n) : __type_name(__n) {}
116#endif
117#endif // ! _LIBCPP_ABI_MICROSOFT
118
119public:
120    _LIBCPP_AVAILABILITY_TYPEINFO_VTABLE
121    virtual ~type_info();
122
123#if defined(_LIBCPP_ABI_MICROSOFT)
124    const char *name() const _NOEXCEPT;
125
126    _LIBCPP_INLINE_VISIBILITY
127    bool before(const type_info& __arg) const _NOEXCEPT {
128      return __compare(__arg) < 0;
129    }
130
131    size_t hash_code() const _NOEXCEPT;
132
133    _LIBCPP_INLINE_VISIBILITY
134    bool operator==(const type_info& __arg) const _NOEXCEPT {
135      return __compare(__arg) == 0;
136    }
137#else
138#if defined(_LIBCPP_HAS_NONUNIQUE_TYPEINFO)
139    _LIBCPP_INLINE_VISIBILITY
140    const char* name() const _NOEXCEPT
141    {
142      return reinterpret_cast<const char*>(__type_name &
143                                           ~_LIBCPP_NONUNIQUE_RTTI_BIT);
144    }
145
146    _LIBCPP_INLINE_VISIBILITY
147    bool before(const type_info& __arg) const _NOEXCEPT
148    {
149      if (!((__type_name & __arg.__type_name) & _LIBCPP_NONUNIQUE_RTTI_BIT))
150        return __type_name < __arg.__type_name;
151      return __compare_nonunique_names(__arg) < 0;
152    }
153
154    _LIBCPP_INLINE_VISIBILITY
155    size_t hash_code() const _NOEXCEPT
156    {
157      if (!(__type_name & _LIBCPP_NONUNIQUE_RTTI_BIT))
158        return __type_name;
159
160      const char* __ptr = name();
161      size_t __hash = 5381;
162      while (unsigned char __c = static_cast<unsigned char>(*__ptr++))
163        __hash = (__hash * 33) ^ __c;
164      return __hash;
165    }
166
167    _LIBCPP_INLINE_VISIBILITY
168    bool operator==(const type_info& __arg) const _NOEXCEPT
169    {
170      if (__type_name == __arg.__type_name)
171        return true;
172
173      if (!((__type_name & __arg.__type_name) & _LIBCPP_NONUNIQUE_RTTI_BIT))
174        return false;
175      return __compare_nonunique_names(__arg) == 0;
176    }
177#else
178    _LIBCPP_INLINE_VISIBILITY
179    const char* name() const _NOEXCEPT
180    { return __type_name; }
181
182    _LIBCPP_INLINE_VISIBILITY
183    bool before(const type_info& __arg) const _NOEXCEPT
184    { return __type_name < __arg.__type_name; }
185
186    _LIBCPP_INLINE_VISIBILITY
187    size_t hash_code() const _NOEXCEPT
188    { return reinterpret_cast<size_t>(__type_name); }
189
190    _LIBCPP_INLINE_VISIBILITY
191    bool operator==(const type_info& __arg) const _NOEXCEPT
192    { return __type_name == __arg.__type_name; }
193#endif
194#endif // _LIBCPP_ABI_MICROSOFT
195
196    _LIBCPP_INLINE_VISIBILITY
197    bool operator!=(const type_info& __arg) const _NOEXCEPT
198    { return !operator==(__arg); }
199};
200
201class _LIBCPP_EXCEPTION_ABI bad_cast
202    : public exception
203{
204public:
205    bad_cast() _NOEXCEPT;
206    virtual ~bad_cast() _NOEXCEPT;
207    virtual const char* what() const _NOEXCEPT;
208};
209
210class _LIBCPP_EXCEPTION_ABI bad_typeid
211    : public exception
212{
213public:
214    bad_typeid() _NOEXCEPT;
215    virtual ~bad_typeid() _NOEXCEPT;
216    virtual const char* what() const _NOEXCEPT;
217};
218
219}  // std
220
221#endif // defined(_LIBCPP_ABI_MICROSOFT) && !defined(_LIBCPP_NO_VCRUNTIME)
222
223_LIBCPP_BEGIN_NAMESPACE_STD
224_LIBCPP_NORETURN inline _LIBCPP_INLINE_VISIBILITY
225void __throw_bad_cast()
226{
227#ifndef _LIBCPP_NO_EXCEPTIONS
228    throw bad_cast();
229#else
230    _VSTD::abort();
231#endif
232}
233_LIBCPP_END_NAMESPACE_STD
234
235#endif  // __LIBCPP_TYPEINFO
236