]>
Commit | Line | Data |
---|---|---|
6305f20a | 1 | // RTTI support for -*- C++ -*- |
83ffe9cd | 2 | // Copyright (C) 1994-2023 Free Software Foundation, Inc. |
e2c09482 | 3 | // |
cbecceb9 | 4 | // This file is part of GCC. |
6305f20a | 5 | // |
cbecceb9 | 6 | // GCC is free software; you can redistribute it and/or modify |
6305f20a | 7 | // it under the terms of the GNU General Public License as published by |
748086b7 | 8 | // the Free Software Foundation; either version 3, or (at your option) |
6305f20a | 9 | // any later version. |
b939d4f6 | 10 | // |
cbecceb9 | 11 | // GCC is distributed in the hope that it will be useful, |
6305f20a BK |
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. | |
b939d4f6 | 15 | // |
748086b7 JJ |
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/>. | |
6305f20a | 24 | |
669f7a03 | 25 | /** @file typeinfo |
78a53887 | 26 | * This is a Standard C++ Library header. |
669f7a03 PE |
27 | */ |
28 | ||
3d7c150e BK |
29 | #ifndef _TYPEINFO |
30 | #define _TYPEINFO | |
6305f20a | 31 | |
584fd146 PC |
32 | #pragma GCC system_header |
33 | ||
27abac26 | 34 | #include <bits/exception.h> |
734f5023 | 35 | #if __cplusplus >= 201103L |
7c3e9502 BK |
36 | #include <bits/hash_bytes.h> |
37 | #endif | |
38 | ||
723acbd5 MM |
39 | #pragma GCC visibility push(default) |
40 | ||
3633cc54 JW |
41 | #if __cplusplus >= 202100L |
42 | # define __cpp_lib_constexpr_typeinfo 202106L | |
43 | #endif | |
44 | ||
6305f20a BK |
45 | extern "C++" { |
46 | ||
6305f20a BK |
47 | namespace __cxxabiv1 |
48 | { | |
49 | class __class_type_info; | |
50 | } // namespace __cxxabiv1 | |
6305f20a | 51 | |
b54c93b7 | 52 | // Determine whether typeinfo names for the same type are merged (in which |
61e6d522 JM |
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. | |
b54c93b7 | 69 | |
40a1c5cb | 70 | #ifndef __GXX_MERGED_TYPEINFO_NAMES |
61e6d522 JM |
71 | // By default, typeinfo names are not merged. |
72 | #define __GXX_MERGED_TYPEINFO_NAMES 0 | |
0f0b2faf MM |
73 | #endif |
74 | ||
61e6d522 | 75 | // By default follow the old inline rules to avoid ABI changes. |
b54c93b7 | 76 | #ifndef __GXX_TYPEINFO_EQUALITY_INLINE |
a0080f02 JW |
77 | # if !__GXX_WEAK__ |
78 | # define __GXX_TYPEINFO_EQUALITY_INLINE 0 | |
79 | # else | |
80 | # define __GXX_TYPEINFO_EQUALITY_INLINE 1 | |
81 | # endif | |
b54c93b7 JM |
82 | #endif |
83 | ||
b939d4f6 | 84 | namespace std |
d34786e3 | 85 | { |
aa2d5ba2 PE |
86 | /** |
87 | * @brief Part of RTTI. | |
88 | * | |
89 | * The @c type_info class describes type information generated by | |
669f7a03 | 90 | * an implementation. |
aa2d5ba2 | 91 | */ |
b939d4f6 | 92 | class type_info |
d34786e3 BK |
93 | { |
94 | public: | |
949d9ae1 | 95 | /** Destructor first. Being the first non-inline virtual function, this |
669f7a03 PE |
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. */ | |
d34786e3 BK |
99 | virtual ~type_info(); |
100 | ||
77cd227e | 101 | /** Returns an @e implementation-defined byte string; this is not |
669f7a03 | 102 | * portable between compilers! */ |
370d8a3d | 103 | const char* name() const _GLIBCXX_NOEXCEPT |
52669d59 | 104 | { return __name[0] == '*' ? __name + 1 : __name; } |
6305f20a | 105 | |
3633cc54 | 106 | /** Returns true if `*this` precedes `__arg` in the implementation's |
669f7a03 | 107 | * collation order. */ |
3633cc54 | 108 | bool before(const type_info& __arg) const _GLIBCXX_NOEXCEPT; |
949d9ae1 | 109 | |
3633cc54 JW |
110 | _GLIBCXX23_CONSTEXPR |
111 | bool operator==(const type_info& __arg) const _GLIBCXX_NOEXCEPT; | |
20fa41e6 JW |
112 | |
113 | #if __cpp_impl_three_way_comparison < 201907L | |
370d8a3d | 114 | bool operator!=(const type_info& __arg) const _GLIBCXX_NOEXCEPT |
d34786e3 | 115 | { return !operator==(__arg); } |
20fa41e6 | 116 | #endif |
049d2422 | 117 | |
734f5023 | 118 | #if __cplusplus >= 201103L |
72f1c34b | 119 | size_t hash_code() const noexcept |
33da99cb PC |
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 | } | |
734f5023 | 128 | #endif // C++11 |
33da99cb | 129 | |
049d2422 BK |
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 | ||
d34786e3 BK |
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 | ||
949d9ae1 | 145 | // Internally used during catch matching |
d34786e3 BK |
146 | virtual bool __do_upcast(const __cxxabiv1::__class_type_info *__target, |
147 | void **__obj_ptr) const; | |
949d9ae1 | 148 | |
949d9ae1 BK |
149 | protected: |
150 | const char *__name; | |
b939d4f6 | 151 | |
949d9ae1 | 152 | explicit type_info(const char *__n): __name(__n) { } |
b939d4f6 | 153 | |
949d9ae1 | 154 | private: |
3633cc54 JW |
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 | |
949d9ae1 BK |
160 | type_info& operator=(const type_info&); |
161 | type_info(const type_info&); | |
3633cc54 JW |
162 | #endif |
163 | ||
164 | #if ! __GXX_TYPEINFO_EQUALITY_INLINE | |
165 | bool __equal(const type_info&) const _GLIBCXX_NOEXCEPT; | |
166 | #endif | |
d34786e3 BK |
167 | }; |
168 | ||
3633cc54 JW |
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 | ||
aa2d5ba2 PE |
214 | /** |
215 | * @brief Thrown during incorrect typecasting. | |
5b9daa7e | 216 | * @ingroup exceptions |
aa2d5ba2 PE |
217 | * |
218 | * If you attempt an invalid @c dynamic_cast expression, an instance of | |
669f7a03 | 219 | * this class (or something derived from this class) is thrown. */ |
b939d4f6 | 220 | class bad_cast : public exception |
d34786e3 BK |
221 | { |
222 | public: | |
8535715d | 223 | bad_cast() _GLIBCXX_USE_NOEXCEPT { } |
949d9ae1 | 224 | |
e05cd3dd PC |
225 | // This declaration is not useless: |
226 | // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 | |
8535715d | 227 | virtual ~bad_cast() _GLIBCXX_USE_NOEXCEPT; |
949d9ae1 | 228 | |
c3f0f556 | 229 | // See comment in eh_exception.cc. |
8535715d | 230 | virtual const char* what() const _GLIBCXX_USE_NOEXCEPT; |
d34786e3 | 231 | }; |
b939d4f6 RÁE |
232 | |
233 | /** | |
5b9daa7e BK |
234 | * @brief Thrown when a NULL pointer in a @c typeid expression is used. |
235 | * @ingroup exceptions | |
236 | */ | |
b939d4f6 | 237 | class bad_typeid : public exception |
d34786e3 BK |
238 | { |
239 | public: | |
8535715d | 240 | bad_typeid () _GLIBCXX_USE_NOEXCEPT { } |
949d9ae1 | 241 | |
e05cd3dd PC |
242 | // This declaration is not useless: |
243 | // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 | |
8535715d | 244 | virtual ~bad_typeid() _GLIBCXX_USE_NOEXCEPT; |
949d9ae1 | 245 | |
c3f0f556 | 246 | // See comment in eh_exception.cc. |
8535715d | 247 | virtual const char* what() const _GLIBCXX_USE_NOEXCEPT; |
d34786e3 | 248 | }; |
6305f20a BK |
249 | } // namespace std |
250 | ||
b939d4f6 RÁE |
251 | } // extern "C++" |
252 | ||
723acbd5 MM |
253 | #pragma GCC visibility pop |
254 | ||
6305f20a | 255 | #endif |