1 // RTTI support for -*- C++ -*-
2 // Copyright (C) 1994-2022 Free Software Foundation, Inc.
4 // This file is part of GCC.
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)
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.
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.
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/>.
26 * This is a Standard C++ Library header.
32 #pragma GCC system_header
34 #include <bits/exception.h>
35 #if __cplusplus >= 201103L
36 #include <bits/hash_bytes.h>
39 #pragma GCC visibility push(default)
41 #if __cplusplus >= 202100L
42 # define __cpp_lib_constexpr_typeinfo 202106L
49 class __class_type_info;
50 } // namespace __cxxabiv1
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.
70 #ifndef __GXX_MERGED_TYPEINFO_NAMES
71 // By default, typeinfo names are not merged.
72 #define __GXX_MERGED_TYPEINFO_NAMES 0
75 // By default follow the old inline rules to avoid ABI changes.
76 #ifndef __GXX_TYPEINFO_EQUALITY_INLINE
78 # define __GXX_TYPEINFO_EQUALITY_INLINE 0
80 # define __GXX_TYPEINFO_EQUALITY_INLINE 1
87 * @brief Part of RTTI.
89 * The @c type_info class describes type information generated by
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. */
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; }
106 /** Returns true if `*this` precedes `__arg` in the implementation's
107 * collation order. */
108 bool before(const type_info& __arg) const _GLIBCXX_NOEXCEPT;
111 bool operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT;
113 #if __cpp_impl_three_way_comparison < 201907L
114 bool operator!=(const type_info& __arg) const _GLIBCXX_NOEXCEPT
115 { return !operator==(__arg); }
118 #if __cplusplus >= 201103L
119 size_t hash_code() const noexcept
121 # if !__GXX_MERGED_TYPEINFO_NAMES
122 return _Hash_bytes(name(), __builtin_strlen(name()),
123 static_cast<size_t>(0xc70f6907UL));
125 return reinterpret_cast<size_t>(__name);
130 // Return true if this is a pointer type of some kind
131 virtual bool __is_pointer_p() const;
133 // Return true if this is a function type
134 virtual bool __is_function_p() const;
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
142 virtual bool __do_catch(const type_info *__thr_type, void **__thr_obj,
143 unsigned __outer) const;
145 // Internally used during catch matching
146 virtual bool __do_upcast(const __cxxabiv1::__class_type_info *__target,
147 void **__obj_ptr) const;
152 explicit type_info(const char *__n): __name(__n) { }
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;
160 type_info& operator=(const type_info&);
161 type_info(const type_info&);
164 #if ! __GXX_TYPEINFO_EQUALITY_INLINE
165 bool __equal(const type_info&) const _GLIBCXX_NOEXCEPT;
169 #if __GXX_TYPEINFO_EQUALITY_INLINE
171 type_info::before(const type_info& __arg) const _GLIBCXX_NOEXCEPT
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;
180 // On some targets we can rely on type_info's NTBS being unique,
181 // and therefore address comparisons are sufficient.
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;
191 #if __GXX_TYPEINFO_EQUALITY_INLINE || __cplusplus > 202002L
192 _GLIBCXX23_CONSTEXPR inline bool
193 type_info::operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT
195 if (std::__is_constant_evaluated())
196 return this == &__arg;
198 if (__name == __arg.__name)
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;
215 * @brief Thrown during incorrect typecasting.
216 * @ingroup exceptions
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
223 bad_cast() _GLIBCXX_USE_NOEXCEPT { }
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;
229 // See comment in eh_exception.cc.
230 virtual const char* what() const _GLIBCXX_USE_NOEXCEPT;
234 * @brief Thrown when a NULL pointer in a @c typeid expression is used.
235 * @ingroup exceptions
237 class bad_typeid : public exception
240 bad_typeid () _GLIBCXX_USE_NOEXCEPT { }
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;
246 // See comment in eh_exception.cc.
247 virtual const char* what() const _GLIBCXX_USE_NOEXCEPT;
253 #pragma GCC visibility pop