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