LLVM API Documentation

Casting.h
Go to the documentation of this file.
00001 //===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
00011 // and dyn_cast_or_null<X>() templates.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_CASTING_H
00016 #define LLVM_SUPPORT_CASTING_H
00017 
00018 #include "llvm/Support/Compiler.h"
00019 #include "llvm/Support/type_traits.h"
00020 #include <cassert>
00021 
00022 namespace llvm {
00023 
00024 //===----------------------------------------------------------------------===//
00025 //                          isa<x> Support Templates
00026 //===----------------------------------------------------------------------===//
00027 
00028 // Define a template that can be specialized by smart pointers to reflect the
00029 // fact that they are automatically dereferenced, and are not involved with the
00030 // template selection process...  the default implementation is a noop.
00031 //
00032 template<typename From> struct simplify_type {
00033   typedef       From SimpleType;        // The real type this represents...
00034 
00035   // An accessor to get the real value...
00036   static SimpleType &getSimplifiedValue(From &Val) { return Val; }
00037 };
00038 
00039 template<typename From> struct simplify_type<const From> {
00040   typedef typename simplify_type<From>::SimpleType NonConstSimpleType;
00041   typedef typename add_const_past_pointer<NonConstSimpleType>::type
00042     SimpleType;
00043   typedef typename add_lvalue_reference_if_not_pointer<SimpleType>::type
00044     RetType;
00045   static RetType getSimplifiedValue(const From& Val) {
00046     return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val));
00047   }
00048 };
00049 
00050 // The core of the implementation of isa<X> is here; To and From should be
00051 // the names of classes.  This template can be specialized to customize the
00052 // implementation of isa<> without rewriting it from scratch.
00053 template <typename To, typename From, typename Enabler = void>
00054 struct isa_impl {
00055   static inline bool doit(const From &Val) {
00056     return To::classof(&Val);
00057   }
00058 };
00059 
00060 /// \brief Always allow upcasts, and perform no dynamic check for them.
00061 template <typename To, typename From>
00062 struct isa_impl<
00063     To, From, typename std::enable_if<std::is_base_of<To, From>::value>::type> {
00064   static inline bool doit(const From &) { return true; }
00065 };
00066 
00067 template <typename To, typename From> struct isa_impl_cl {
00068   static inline bool doit(const From &Val) {
00069     return isa_impl<To, From>::doit(Val);
00070   }
00071 };
00072 
00073 template <typename To, typename From> struct isa_impl_cl<To, const From> {
00074   static inline bool doit(const From &Val) {
00075     return isa_impl<To, From>::doit(Val);
00076   }
00077 };
00078 
00079 template <typename To, typename From> struct isa_impl_cl<To, From*> {
00080   static inline bool doit(const From *Val) {
00081     assert(Val && "isa<> used on a null pointer");
00082     return isa_impl<To, From>::doit(*Val);
00083   }
00084 };
00085 
00086 template <typename To, typename From> struct isa_impl_cl<To, From*const> {
00087   static inline bool doit(const From *Val) {
00088     assert(Val && "isa<> used on a null pointer");
00089     return isa_impl<To, From>::doit(*Val);
00090   }
00091 };
00092 
00093 template <typename To, typename From> struct isa_impl_cl<To, const From*> {
00094   static inline bool doit(const From *Val) {
00095     assert(Val && "isa<> used on a null pointer");
00096     return isa_impl<To, From>::doit(*Val);
00097   }
00098 };
00099 
00100 template <typename To, typename From> struct isa_impl_cl<To, const From*const> {
00101   static inline bool doit(const From *Val) {
00102     assert(Val && "isa<> used on a null pointer");
00103     return isa_impl<To, From>::doit(*Val);
00104   }
00105 };
00106 
00107 template<typename To, typename From, typename SimpleFrom>
00108 struct isa_impl_wrap {
00109   // When From != SimplifiedType, we can simplify the type some more by using
00110   // the simplify_type template.
00111   static bool doit(const From &Val) {
00112     return isa_impl_wrap<To, SimpleFrom,
00113       typename simplify_type<SimpleFrom>::SimpleType>::doit(
00114                           simplify_type<const From>::getSimplifiedValue(Val));
00115   }
00116 };
00117 
00118 template<typename To, typename FromTy>
00119 struct isa_impl_wrap<To, FromTy, FromTy> {
00120   // When From == SimpleType, we are as simple as we are going to get.
00121   static bool doit(const FromTy &Val) {
00122     return isa_impl_cl<To,FromTy>::doit(Val);
00123   }
00124 };
00125 
00126 // isa<X> - Return true if the parameter to the template is an instance of the
00127 // template type argument.  Used like this:
00128 //
00129 //  if (isa<Type>(myVal)) { ... }
00130 //
00131 template <class X, class Y>
00132 LLVM_ATTRIBUTE_UNUSED_RESULT inline bool isa(const Y &Val) {
00133   return isa_impl_wrap<X, const Y,
00134                        typename simplify_type<const Y>::SimpleType>::doit(Val);
00135 }
00136 
00137 //===----------------------------------------------------------------------===//
00138 //                          cast<x> Support Templates
00139 //===----------------------------------------------------------------------===//
00140 
00141 template<class To, class From> struct cast_retty;
00142 
00143 
00144 // Calculate what type the 'cast' function should return, based on a requested
00145 // type of To and a source type of From.
00146 template<class To, class From> struct cast_retty_impl {
00147   typedef To& ret_type;         // Normal case, return Ty&
00148 };
00149 template<class To, class From> struct cast_retty_impl<To, const From> {
00150   typedef const To &ret_type;   // Normal case, return Ty&
00151 };
00152 
00153 template<class To, class From> struct cast_retty_impl<To, From*> {
00154   typedef To* ret_type;         // Pointer arg case, return Ty*
00155 };
00156 
00157 template<class To, class From> struct cast_retty_impl<To, const From*> {
00158   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
00159 };
00160 
00161 template<class To, class From> struct cast_retty_impl<To, const From*const> {
00162   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
00163 };
00164 
00165 
00166 template<class To, class From, class SimpleFrom>
00167 struct cast_retty_wrap {
00168   // When the simplified type and the from type are not the same, use the type
00169   // simplifier to reduce the type, then reuse cast_retty_impl to get the
00170   // resultant type.
00171   typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
00172 };
00173 
00174 template<class To, class FromTy>
00175 struct cast_retty_wrap<To, FromTy, FromTy> {
00176   // When the simplified type is equal to the from type, use it directly.
00177   typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
00178 };
00179 
00180 template<class To, class From>
00181 struct cast_retty {
00182   typedef typename cast_retty_wrap<To, From,
00183                    typename simplify_type<From>::SimpleType>::ret_type ret_type;
00184 };
00185 
00186 // Ensure the non-simple values are converted using the simplify_type template
00187 // that may be specialized by smart pointers...
00188 //
00189 template<class To, class From, class SimpleFrom> struct cast_convert_val {
00190   // This is not a simple type, use the template to simplify it...
00191   static typename cast_retty<To, From>::ret_type doit(From &Val) {
00192     return cast_convert_val<To, SimpleFrom,
00193       typename simplify_type<SimpleFrom>::SimpleType>::doit(
00194                           simplify_type<From>::getSimplifiedValue(Val));
00195   }
00196 };
00197 
00198 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
00199   // This _is_ a simple type, just cast it.
00200   static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
00201     typename cast_retty<To, FromTy>::ret_type Res2
00202      = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
00203     return Res2;
00204   }
00205 };
00206 
00207 template <class X> struct is_simple_type {
00208   static const bool value =
00209       std::is_same<X, typename simplify_type<X>::SimpleType>::value;
00210 };
00211 
00212 // cast<X> - Return the argument parameter cast to the specified type.  This
00213 // casting operator asserts that the type is correct, so it does not return null
00214 // on failure.  It does not allow a null argument (use cast_or_null for that).
00215 // It is typically used like this:
00216 //
00217 //  cast<Instruction>(myVal)->getParent()
00218 //
00219 template <class X, class Y>
00220 inline typename std::enable_if<!is_simple_type<Y>::value,
00221                                typename cast_retty<X, const Y>::ret_type>::type
00222 cast(const Y &Val) {
00223   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
00224   return cast_convert_val<
00225       X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val);
00226 }
00227 
00228 template <class X, class Y>
00229 inline typename cast_retty<X, Y>::ret_type cast(Y &Val) {
00230   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
00231   return cast_convert_val<X, Y,
00232                           typename simplify_type<Y>::SimpleType>::doit(Val);
00233 }
00234 
00235 template <class X, class Y>
00236 inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) {
00237   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
00238   return cast_convert_val<X, Y*,
00239                           typename simplify_type<Y*>::SimpleType>::doit(Val);
00240 }
00241 
00242 // cast_or_null<X> - Functionally identical to cast, except that a null value is
00243 // accepted.
00244 //
00245 template <class X, class Y>
00246 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
00247     !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
00248 cast_or_null(const Y &Val) {
00249   if (!Val)
00250     return nullptr;
00251   assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
00252   return cast<X>(Val);
00253 }
00254 
00255 template <class X, class Y>
00256 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
00257     !is_simple_type<Y>::value, typename cast_retty<X, Y>::ret_type>::type
00258 cast_or_null(Y &Val) {
00259   if (!Val)
00260     return nullptr;
00261   assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
00262   return cast<X>(Val);
00263 }
00264 
00265 template <class X, class Y>
00266 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
00267 cast_or_null(Y *Val) {
00268   if (!Val) return nullptr;
00269   assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
00270   return cast<X>(Val);
00271 }
00272 
00273 
00274 // dyn_cast<X> - Return the argument parameter cast to the specified type.  This
00275 // casting operator returns null if the argument is of the wrong type, so it can
00276 // be used to test for a type as well as cast if successful.  This should be
00277 // used in the context of an if statement like this:
00278 //
00279 //  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
00280 //
00281 
00282 template <class X, class Y>
00283 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
00284     !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
00285 dyn_cast(const Y &Val) {
00286   return isa<X>(Val) ? cast<X>(Val) : nullptr;
00287 }
00288 
00289 template <class X, class Y>
00290 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y>::ret_type
00291 dyn_cast(Y &Val) {
00292   return isa<X>(Val) ? cast<X>(Val) : nullptr;
00293 }
00294 
00295 template <class X, class Y>
00296 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
00297 dyn_cast(Y *Val) {
00298   return isa<X>(Val) ? cast<X>(Val) : nullptr;
00299 }
00300 
00301 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
00302 // value is accepted.
00303 //
00304 template <class X, class Y>
00305 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
00306     !is_simple_type<Y>::value, typename cast_retty<X, const Y>::ret_type>::type
00307 dyn_cast_or_null(const Y &Val) {
00308   return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
00309 }
00310 
00311 template <class X, class Y>
00312 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename std::enable_if<
00313     !is_simple_type<Y>::value, typename cast_retty<X, Y>::ret_type>::type
00314 dyn_cast_or_null(Y &Val) {
00315   return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
00316 }
00317 
00318 template <class X, class Y>
00319 LLVM_ATTRIBUTE_UNUSED_RESULT inline typename cast_retty<X, Y *>::ret_type
00320 dyn_cast_or_null(Y *Val) {
00321   return (Val && isa<X>(Val)) ? cast<X>(Val) : nullptr;
00322 }
00323 
00324 } // End llvm namespace
00325 
00326 #endif