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