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