libstdc++
typeinfo
Go to the documentation of this file.
1// RTTI support for -*- C++ -*-
2// Copyright (C) 1994-2022 Free Software Foundation, Inc.
3//
4// This file is part of GCC.
5//
6// GCC is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation; either version 3, or (at your option)
9// any later version.
10//
11// GCC is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
19
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
24
25/** @file typeinfo
26 * This is a Standard C++ Library header.
27 */
28
29#ifndef _TYPEINFO
30#define _TYPEINFO
31
32#pragma GCC system_header
33
34#include <bits/exception.h>
35#if __cplusplus >= 201103L
36#include <bits/hash_bytes.h>
37#endif
38
39#pragma GCC visibility push(default)
40
41#if __cplusplus >= 202100L
42# define __cpp_lib_constexpr_typeinfo 202106L
43#endif
44
45extern "C++" {
46
47namespace __cxxabiv1
48{
49 class __class_type_info;
50} // namespace __cxxabiv1
51
52// Determine whether typeinfo names for the same type are merged (in which
53// case comparison can just compare pointers) or not (in which case strings
54// must be compared), and whether comparison is to be implemented inline or
55// not. We used to do inline pointer comparison by default if weak symbols
56// are available, but even with weak symbols sometimes names are not merged
57// when objects are loaded with RTLD_LOCAL, so now we always use strcmp by
58// default. For ABI compatibility, we do the strcmp inline if weak symbols
59// are available, and out-of-line if not. Out-of-line pointer comparison
60// is used where the object files are to be portable to multiple systems,
61// some of which may not be able to use pointer comparison, but the
62// particular system for which libstdc++ is being built can use pointer
63// comparison; in particular for most ARM EABI systems, where the ABI
64// specifies out-of-line comparison. The compiler's target configuration
65// can override the defaults by defining __GXX_TYPEINFO_EQUALITY_INLINE to
66// 1 or 0 to indicate whether or not comparison is inline, and
67// __GXX_MERGED_TYPEINFO_NAMES to 1 or 0 to indicate whether or not pointer
68// comparison can be used.
69
70#ifndef __GXX_MERGED_TYPEINFO_NAMES
71// By default, typeinfo names are not merged.
72#define __GXX_MERGED_TYPEINFO_NAMES 0
73#endif
74
75// By default follow the old inline rules to avoid ABI changes.
76#ifndef __GXX_TYPEINFO_EQUALITY_INLINE
77 #if !__GXX_WEAK__
78 #define __GXX_TYPEINFO_EQUALITY_INLINE 0
79 #else
80 #define __GXX_TYPEINFO_EQUALITY_INLINE 1
81 #endif
82#endif
83
84namespace std
85{
86 /**
87 * @brief Part of RTTI.
88 *
89 * The @c type_info class describes type information generated by
90 * an implementation.
91 */
93 {
94 public:
95 /** Destructor first. Being the first non-inline virtual function, this
96 * controls in which translation unit the vtable is emitted. The
97 * compiler makes use of that information to know where to emit
98 * the runtime-mandated type_info structures in the new-abi. */
99 virtual ~type_info();
100
101 /** Returns an @e implementation-defined byte string; this is not
102 * portable between compilers! */
103 const char* name() const _GLIBCXX_NOEXCEPT
104 { return __name[0] == '*' ? __name + 1 : __name; }
105
106 /** Returns true if `*this` precedes `__arg` in the implementation's
107 * collation order. */
108 bool before(const type_info& __arg) const _GLIBCXX_NOEXCEPT;
109
110 _GLIBCXX23_CONSTEXPR
111 bool operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT;
112
113#if __cpp_impl_three_way_comparison < 201907L
114 bool operator!=(const type_info& __arg) const _GLIBCXX_NOEXCEPT
115 { return !operator==(__arg); }
116#endif
117
118#if __cplusplus >= 201103L
119 size_t hash_code() const noexcept
120 {
121# if !__GXX_MERGED_TYPEINFO_NAMES
122 return _Hash_bytes(name(), __builtin_strlen(name()),
123 static_cast<size_t>(0xc70f6907UL));
124# else
125 return reinterpret_cast<size_t>(__name);
126# endif
127 }
128#endif // C++11
129
130 // Return true if this is a pointer type of some kind
131 virtual bool __is_pointer_p() const;
132
133 // Return true if this is a function type
134 virtual bool __is_function_p() const;
135
136 // Try and catch a thrown type. Store an adjusted pointer to the
137 // caught type in THR_OBJ. If THR_TYPE is not a pointer type, then
138 // THR_OBJ points to the thrown object. If THR_TYPE is a pointer
139 // type, then THR_OBJ is the pointer itself. OUTER indicates the
140 // number of outer pointers, and whether they were const
141 // qualified.
142 virtual bool __do_catch(const type_info *__thr_type, void **__thr_obj,
143 unsigned __outer) const;
144
145 // Internally used during catch matching
146 virtual bool __do_upcast(const __cxxabiv1::__class_type_info *__target,
147 void **__obj_ptr) const;
148
149 protected:
150 const char *__name;
151
152 explicit type_info(const char *__n): __name(__n) { }
153
154 private:
155 // type_info objects cannot be copied.
156#if __cplusplus >= 201103L
157 type_info& operator=(const type_info&) = delete;
158 type_info(const type_info&) = delete;
159#else
160 type_info& operator=(const type_info&);
161 type_info(const type_info&);
162#endif
163
164#if ! __GXX_TYPEINFO_EQUALITY_INLINE
165 bool __equal(const type_info&) const _GLIBCXX_NOEXCEPT;
166#endif
167 };
168
169#if __GXX_TYPEINFO_EQUALITY_INLINE
170 inline bool
171 type_info::before(const type_info& __arg) const _GLIBCXX_NOEXCEPT
172 {
173#if !__GXX_MERGED_TYPEINFO_NAMES
174 // Even with the new abi, on systems that support dlopen
175 // we can run into cases where type_info names aren't merged,
176 // so we still need to do string comparison.
177 if (__name[0] != '*' || __arg.__name[0] != '*')
178 return __builtin_strcmp (__name, __arg.__name) < 0;
179#else
180 // On some targets we can rely on type_info's NTBS being unique,
181 // and therefore address comparisons are sufficient.
182#endif
183
184 // In old abi, or when weak symbols are not supported, there can
185 // be multiple instances of a type_info object for one
186 // type. Uniqueness must use the __name value, not object address.
187 return __name < __arg.__name;
188 }
189#endif
190
191#if __GXX_TYPEINFO_EQUALITY_INLINE || __cplusplus > 202002L
192 _GLIBCXX23_CONSTEXPR inline bool
193 type_info::operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT
194 {
195 if (std::__is_constant_evaluated())
196 return this == &__arg;
197
198 if (__name == __arg.__name)
199 return true;
200
201#if !__GXX_TYPEINFO_EQUALITY_INLINE
202 // ABI requires comparisons to be non-inline.
203 return __equal(__arg);
204#elif !__GXX_MERGED_TYPEINFO_NAMES
205 // Need to do string comparison.
206 return __name[0] != '*' && __builtin_strcmp (__name, __arg.name()) == 0;
207#else
208 return false;
209#endif
210 }
211# endif
212
213
214 /**
215 * @brief Thrown during incorrect typecasting.
216 * @ingroup exceptions
217 *
218 * If you attempt an invalid @c dynamic_cast expression, an instance of
219 * this class (or something derived from this class) is thrown. */
220 class bad_cast : public exception
221 {
222 public:
223 bad_cast() _GLIBCXX_USE_NOEXCEPT { }
224
225 // This declaration is not useless:
226 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
227 virtual ~bad_cast() _GLIBCXX_USE_NOEXCEPT;
228
229 // See comment in eh_exception.cc.
230 virtual const char* what() const _GLIBCXX_USE_NOEXCEPT;
231 };
232
233 /**
234 * @brief Thrown when a NULL pointer in a @c typeid expression is used.
235 * @ingroup exceptions
236 */
237 class bad_typeid : public exception
238 {
239 public:
240 bad_typeid () _GLIBCXX_USE_NOEXCEPT { }
241
242 // This declaration is not useless:
243 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
244 virtual ~bad_typeid() _GLIBCXX_USE_NOEXCEPT;
245
246 // See comment in eh_exception.cc.
247 virtual const char* what() const _GLIBCXX_USE_NOEXCEPT;
248 };
249} // namespace std
250
251} // extern "C++"
252
253#pragma GCC visibility pop
254
255#endif
ISO C++ entities toplevel namespace is std.
Part of RTTI.
Definition: typeinfo:93
const char * name() const noexcept
Definition: typeinfo:103
virtual ~type_info()
bool before(const type_info &__arg) const noexcept
Thrown during incorrect typecasting.
Definition: typeinfo:221
virtual const char * what() const noexcept
Thrown when a NULL pointer in a typeid expression is used.
Definition: typeinfo:238
virtual const char * what() const noexcept
Base class for all library exceptions.
Definition: exception.h:62