typeinfo

Go to the documentation of this file.
00001 // RTTI support for -*- C++ -*-
00002 // Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
00003 // 2003, 2004, 2005, 2006, 2007, 2009, 2010
00004 // Free Software Foundation
00005 //
00006 // This file is part of GCC.
00007 //
00008 // GCC is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 // 
00013 // GCC is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 // 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file typeinfo
00028  *  This is a Standard C++ Library header.
00029  */
00030 
00031 #ifndef _TYPEINFO
00032 #define _TYPEINFO
00033 
00034 #pragma GCC system_header
00035 
00036 #include <exception>
00037 
00038 #pragma GCC visibility push(default)
00039 
00040 extern "C++" {
00041 
00042 namespace __cxxabiv1
00043 {
00044   class __class_type_info;
00045 } // namespace __cxxabiv1
00046 
00047 // Determine whether typeinfo names for the same type are merged (in which
00048 // case comparison can just compare pointers) or not (in which case strings
00049 // must be compared), and whether comparison is to be implemented inline or
00050 // not.  We used to do inline pointer comparison by default if weak symbols
00051 // are available, but even with weak symbols sometimes names are not merged
00052 // when objects are loaded with RTLD_LOCAL, so now we always use strcmp by
00053 // default.  For ABI compatibility, we do the strcmp inline if weak symbols
00054 // are available, and out-of-line if not.  Out-of-line pointer comparison
00055 // is used where the object files are to be portable to multiple systems,
00056 // some of which may not be able to use pointer comparison, but the
00057 // particular system for which libstdc++ is being built can use pointer
00058 // comparison; in particular for most ARM EABI systems, where the ABI
00059 // specifies out-of-line comparison.  The compiler's target configuration
00060 // can override the defaults by defining __GXX_TYPEINFO_EQUALITY_INLINE to
00061 // 1 or 0 to indicate whether or not comparison is inline, and
00062 // __GXX_MERGED_TYPEINFO_NAMES to 1 or 0 to indicate whether or not pointer
00063 // comparison can be used.
00064 
00065 #ifndef __GXX_MERGED_TYPEINFO_NAMES
00066 // By default, typeinfo names are not merged.
00067 #define __GXX_MERGED_TYPEINFO_NAMES 0
00068 #endif
00069 
00070 // By default follow the old inline rules to avoid ABI changes.
00071 #ifndef __GXX_TYPEINFO_EQUALITY_INLINE
00072   #if !__GXX_WEAK__
00073     #define __GXX_TYPEINFO_EQUALITY_INLINE 0
00074   #else
00075     #define __GXX_TYPEINFO_EQUALITY_INLINE 1
00076   #endif
00077 #endif
00078 
00079 namespace std 
00080 {
00081 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00082   size_t
00083   _Hash_bytes(const void* __ptr, size_t __len, size_t __seed);
00084 #endif
00085 
00086   /**
00087    *  @brief  Part of RTTI.
00088    *
00089    *  The @c type_info class describes type information generated by
00090    *  an implementation.
00091   */
00092   class type_info 
00093   {
00094   public:
00095     /** Destructor first. Being the first non-inline virtual function, this
00096      *  controls in which translation unit the vtable is emitted. The
00097      *  compiler makes use of that information to know where to emit
00098      *  the runtime-mandated type_info structures in the new-abi.  */
00099     virtual ~type_info();
00100 
00101     /** Returns an @e implementation-defined byte string; this is not
00102      *  portable between compilers!  */
00103     const char* name() const
00104     { return __name[0] == '*' ? __name + 1 : __name; }
00105 
00106 #if !__GXX_TYPEINFO_EQUALITY_INLINE
00107     // In old abi, or when weak symbols are not supported, there can
00108     // be multiple instances of a type_info object for one
00109     // type. Uniqueness must use the _name value, not object address.
00110     bool before(const type_info& __arg) const;
00111     bool operator==(const type_info& __arg) const;
00112 #else
00113   #if !__GXX_MERGED_TYPEINFO_NAMES
00114     /** Returns true if @c *this precedes @c __arg in the implementation's
00115      *  collation order.  */
00116     // Even with the new abi, on systems that support dlopen
00117     // we can run into cases where type_info names aren't merged,
00118     // so we still need to do string comparison.
00119     bool before(const type_info& __arg) const
00120     { return (__name[0] == '*' && __arg.__name[0] == '*')
00121     ? __name < __arg.__name
00122     : __builtin_strcmp (__name, __arg.__name) < 0; }
00123 
00124     bool operator==(const type_info& __arg) const
00125     {
00126       return ((__name == __arg.__name)
00127           || (__name[0] != '*' &&
00128           __builtin_strcmp (__name, __arg.__name) == 0));
00129     }
00130   #else
00131     // On some targets we can rely on type_info's NTBS being unique,
00132     // and therefore address comparisons are sufficient.
00133     bool before(const type_info& __arg) const
00134     { return __name < __arg.__name; }
00135 
00136     bool operator==(const type_info& __arg) const
00137     { return __name == __arg.__name; }
00138   #endif
00139 #endif
00140     bool operator!=(const type_info& __arg) const
00141     { return !operator==(__arg); }
00142 
00143 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00144     size_t hash_code() const throw()
00145     {
00146 #  if !__GXX_MERGED_TYPEINFO_NAMES
00147       return _Hash_bytes(name(), __builtin_strlen(name()),
00148              static_cast<size_t>(0xc70f6907UL));
00149 #  else
00150       return reinterpret_cast<size_t>(__name);
00151 #  endif
00152     }
00153 #endif // __GXX_EXPERIMENTAL_CXX0X__
00154 
00155     // Return true if this is a pointer type of some kind
00156     virtual bool __is_pointer_p() const;
00157 
00158     // Return true if this is a function type
00159     virtual bool __is_function_p() const;
00160 
00161     // Try and catch a thrown type. Store an adjusted pointer to the
00162     // caught type in THR_OBJ. If THR_TYPE is not a pointer type, then
00163     // THR_OBJ points to the thrown object. If THR_TYPE is a pointer
00164     // type, then THR_OBJ is the pointer itself. OUTER indicates the
00165     // number of outer pointers, and whether they were const
00166     // qualified.
00167     virtual bool __do_catch(const type_info *__thr_type, void **__thr_obj,
00168                 unsigned __outer) const;
00169 
00170     // Internally used during catch matching
00171     virtual bool __do_upcast(const __cxxabiv1::__class_type_info *__target,
00172                  void **__obj_ptr) const;
00173 
00174   protected:
00175     const char *__name;
00176     
00177     explicit type_info(const char *__n): __name(__n) { }
00178     
00179   private:
00180     /// Assigning type_info is not supported.
00181     type_info& operator=(const type_info&);
00182     type_info(const type_info&);
00183   };
00184 
00185   /**
00186    *  @brief  Thrown during incorrect typecasting.
00187    *  @ingroup exceptions
00188    *
00189    *  If you attempt an invalid @c dynamic_cast expression, an instance of
00190    *  this class (or something derived from this class) is thrown.  */
00191   class bad_cast : public exception 
00192   {
00193   public:
00194     bad_cast() throw() { }
00195 
00196     // This declaration is not useless:
00197     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00198     virtual ~bad_cast() throw();
00199 
00200     // See comment in eh_exception.cc.
00201     virtual const char* what() const throw();
00202   };
00203   
00204   /** 
00205    *  @brief Thrown when a NULL pointer in a @c typeid expression is used.
00206    *  @ingroup exceptions
00207    */
00208   class bad_typeid : public exception 
00209   {
00210   public:
00211     bad_typeid () throw() { }
00212 
00213     // This declaration is not useless:
00214     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00215     virtual ~bad_typeid() throw();
00216 
00217     // See comment in eh_exception.cc.
00218     virtual const char* what() const throw();
00219   };
00220 } // namespace std
00221 
00222 #pragma GCC visibility pop
00223 
00224 } // extern "C++"
00225 #endif