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
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 #include <exception>
00035 
00036 #pragma GCC visibility push(default)
00037 
00038 extern "C++" {
00039 
00040 namespace __cxxabiv1
00041 {
00042   class __class_type_info;
00043 } // namespace __cxxabiv1
00044 
00045 // Determine whether typeinfo names for the same type are merged (in which
00046 // case comparison can just compare pointers) or not (in which case strings
00047 // must be compared), and whether comparison is to be implemented inline or
00048 // not.  We used to do inline pointer comparison by default if weak symbols
00049 // are available, but even with weak symbols sometimes names are not merged
00050 // when objects are loaded with RTLD_LOCAL, so now we always use strcmp by
00051 // default.  For ABI compatibility, we do the strcmp inline if weak symbols
00052 // are available, and out-of-line if not.  Out-of-line pointer comparison
00053 // is used where the object files are to be portable to multiple systems,
00054 // some of which may not be able to use pointer comparison, but the
00055 // particular system for which libstdc++ is being built can use pointer
00056 // comparison; in particular for most ARM EABI systems, where the ABI
00057 // specifies out-of-line comparison.  The compiler's target configuration
00058 // can override the defaults by defining __GXX_TYPEINFO_EQUALITY_INLINE to
00059 // 1 or 0 to indicate whether or not comparison is inline, and
00060 // __GXX_MERGED_TYPEINFO_NAMES to 1 or 0 to indicate whether or not pointer
00061 // comparison can be used.
00062 
00063 #ifndef __GXX_MERGED_TYPEINFO_NAMES
00064 // By default, typeinfo names are not merged.
00065 #define __GXX_MERGED_TYPEINFO_NAMES 0
00066 #endif
00067 
00068 // By default follow the old inline rules to avoid ABI changes.
00069 #ifndef __GXX_TYPEINFO_EQUALITY_INLINE
00070   #if !__GXX_WEAK__
00071     #define __GXX_TYPEINFO_EQUALITY_INLINE 0
00072   #else
00073     #define __GXX_TYPEINFO_EQUALITY_INLINE 1
00074   #endif
00075 #endif
00076 
00077 namespace std 
00078 {
00079   /**
00080    *  @brief  Part of RTTI.
00081    *
00082    *  The @c type_info class describes type information generated by
00083    *  an implementation.
00084   */
00085   class type_info 
00086   {
00087   public:
00088     /** Destructor first. Being the first non-inline virtual function, this
00089      *  controls in which translation unit the vtable is emitted. The
00090      *  compiler makes use of that information to know where to emit
00091      *  the runtime-mandated type_info structures in the new-abi.  */
00092     virtual ~type_info();
00093 
00094     /** Returns an @e implementation-defined byte string; this is not
00095      *  portable between compilers!  */
00096     const char* name() const
00097     { return __name; }
00098 
00099 #if !__GXX_TYPEINFO_EQUALITY_INLINE
00100     // In old abi, or when weak symbols are not supported, there can
00101     // be multiple instances of a type_info object for one
00102     // type. Uniqueness must use the _name value, not object address.
00103     bool before(const type_info& __arg) const;
00104     bool operator==(const type_info& __arg) const;
00105 #else
00106   #if !__GXX_MERGED_TYPEINFO_NAMES
00107     /** Returns true if @c *this precedes @c __arg in the implementation's
00108      *  collation order.  */
00109     // Even with the new abi, on systems that support dlopen
00110     // we can run into cases where type_info names aren't merged,
00111     // so we still need to do string comparison.
00112     bool before(const type_info& __arg) const
00113     { return __builtin_strcmp (__name, __arg.__name) < 0; }
00114 
00115     bool operator==(const type_info& __arg) const
00116     {
00117       return ((__name == __arg.__name)
00118           || __builtin_strcmp (__name, __arg.__name) == 0);
00119     }
00120   #else
00121     // On some targets we can rely on type_info's NTBS being unique,
00122     // and therefore address comparisons are sufficient.
00123     bool before(const type_info& __arg) const
00124     { return __name < __arg.__name; }
00125 
00126     bool operator==(const type_info& __arg) const
00127     { return __name == __arg.__name; }
00128   #endif
00129 #endif
00130     bool operator!=(const type_info& __arg) const
00131     { return !operator==(__arg); }
00132 
00133     // Return true if this is a pointer type of some kind
00134     virtual bool __is_pointer_p() const;
00135 
00136     // Return true if this is a function type
00137     virtual bool __is_function_p() const;
00138 
00139     // Try and catch a thrown type. Store an adjusted pointer to the
00140     // caught type in THR_OBJ. If THR_TYPE is not a pointer type, then
00141     // THR_OBJ points to the thrown object. If THR_TYPE is a pointer
00142     // type, then THR_OBJ is the pointer itself. OUTER indicates the
00143     // number of outer pointers, and whether they were const
00144     // qualified.
00145     virtual bool __do_catch(const type_info *__thr_type, void **__thr_obj,
00146                 unsigned __outer) const;
00147 
00148     // Internally used during catch matching
00149     virtual bool __do_upcast(const __cxxabiv1::__class_type_info *__target,
00150                  void **__obj_ptr) const;
00151 
00152   protected:
00153     const char *__name;
00154     
00155     explicit type_info(const char *__n): __name(__n) { }
00156     
00157   private:
00158     /// Assigning type_info is not supported.
00159     type_info& operator=(const type_info&);
00160     type_info(const type_info&);
00161   };
00162 
00163   /**
00164    *  @brief  Thrown during incorrect typecasting.
00165    *  @ingroup exceptions
00166    *
00167    *  If you attempt an invalid @c dynamic_cast expression, an instance of
00168    *  this class (or something derived from this class) is thrown.  */
00169   class bad_cast : public exception 
00170   {
00171   public:
00172     bad_cast() throw() { }
00173 
00174     // This declaration is not useless:
00175     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00176     virtual ~bad_cast() throw();
00177 
00178     // See comment in eh_exception.cc.
00179     virtual const char* what() const throw();
00180   };
00181   
00182   /** 
00183    *  @brief Thrown when a NULL pointer in a @c typeid expression is used.
00184    *  @ingroup exceptions
00185    */
00186   class bad_typeid : public exception 
00187   {
00188   public:
00189     bad_typeid () throw() { }
00190 
00191     // This declaration is not useless:
00192     // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00193     virtual ~bad_typeid() throw();
00194 
00195     // See comment in eh_exception.cc.
00196     virtual const char* what() const throw();
00197   };
00198 } // namespace std
00199 
00200 #pragma GCC visibility pop
00201 
00202 } // extern "C++"
00203 #endif

Generated on 23 Sep 2009 for libstdc++ by  doxygen 1.6.1