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