OSDN Git Service

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