Home | History | Annotate | Download | only in Support
      1 //===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
     11 // and dyn_cast_or_null<X>() templates.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_SUPPORT_CASTING_H
     16 #define LLVM_SUPPORT_CASTING_H
     17 
     18 #include <cassert>
     19 
     20 namespace llvm {
     21 
     22 //===----------------------------------------------------------------------===//
     23 //                          isa<x> Support Templates
     24 //===----------------------------------------------------------------------===//
     25 
     26 // Define a template that can be specialized by smart pointers to reflect the
     27 // fact that they are automatically dereferenced, and are not involved with the
     28 // template selection process...  the default implementation is a noop.
     29 //
     30 template<typename From> struct simplify_type {
     31   typedef       From SimpleType;        // The real type this represents...
     32 
     33   // An accessor to get the real value...
     34   static SimpleType &getSimplifiedValue(From &Val) { return Val; }
     35 };
     36 
     37 template<typename From> struct simplify_type<const From> {
     38   typedef const From SimpleType;
     39   static SimpleType &getSimplifiedValue(const From &Val) {
     40     return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val));
     41   }
     42 };
     43 
     44 // The core of the implementation of isa<X> is here; To and From should be
     45 // the names of classes.  This template can be specialized to customize the
     46 // implementation of isa<> without rewriting it from scratch.
     47 template <typename To, typename From>
     48 struct isa_impl {
     49   static inline bool doit(const From &Val) {
     50     return To::classof(&Val);
     51   }
     52 };
     53 
     54 template <typename To, typename From> struct isa_impl_cl {
     55   static inline bool doit(const From &Val) {
     56     return isa_impl<To, From>::doit(Val);
     57   }
     58 };
     59 
     60 template <typename To, typename From> struct isa_impl_cl<To, const From> {
     61   static inline bool doit(const From &Val) {
     62     return isa_impl<To, From>::doit(Val);
     63   }
     64 };
     65 
     66 template <typename To, typename From> struct isa_impl_cl<To, From*> {
     67   static inline bool doit(const From *Val) {
     68     return isa_impl<To, From>::doit(*Val);
     69   }
     70 };
     71 
     72 template <typename To, typename From> struct isa_impl_cl<To, const From*> {
     73   static inline bool doit(const From *Val) {
     74     return isa_impl<To, From>::doit(*Val);
     75   }
     76 };
     77 
     78 template <typename To, typename From> struct isa_impl_cl<To, const From*const> {
     79   static inline bool doit(const From *Val) {
     80     return isa_impl<To, From>::doit(*Val);
     81   }
     82 };
     83 
     84 template<typename To, typename From, typename SimpleFrom>
     85 struct isa_impl_wrap {
     86   // When From != SimplifiedType, we can simplify the type some more by using
     87   // the simplify_type template.
     88   static bool doit(const From &Val) {
     89     return isa_impl_wrap<To, SimpleFrom,
     90       typename simplify_type<SimpleFrom>::SimpleType>::doit(
     91                           simplify_type<From>::getSimplifiedValue(Val));
     92   }
     93 };
     94 
     95 template<typename To, typename FromTy>
     96 struct isa_impl_wrap<To, FromTy, FromTy> {
     97   // When From == SimpleType, we are as simple as we are going to get.
     98   static bool doit(const FromTy &Val) {
     99     return isa_impl_cl<To,FromTy>::doit(Val);
    100   }
    101 };
    102 
    103 // isa<X> - Return true if the parameter to the template is an instance of the
    104 // template type argument.  Used like this:
    105 //
    106 //  if (isa<Type>(myVal)) { ... }
    107 //
    108 template <class X, class Y>
    109 inline bool isa(const Y &Val) {
    110   return isa_impl_wrap<X, Y, typename simplify_type<Y>::SimpleType>::doit(Val);
    111 }
    112 
    113 //===----------------------------------------------------------------------===//
    114 //                          cast<x> Support Templates
    115 //===----------------------------------------------------------------------===//
    116 
    117 template<class To, class From> struct cast_retty;
    118 
    119 
    120 // Calculate what type the 'cast' function should return, based on a requested
    121 // type of To and a source type of From.
    122 template<class To, class From> struct cast_retty_impl {
    123   typedef To& ret_type;         // Normal case, return Ty&
    124 };
    125 template<class To, class From> struct cast_retty_impl<To, const From> {
    126   typedef const To &ret_type;   // Normal case, return Ty&
    127 };
    128 
    129 template<class To, class From> struct cast_retty_impl<To, From*> {
    130   typedef To* ret_type;         // Pointer arg case, return Ty*
    131 };
    132 
    133 template<class To, class From> struct cast_retty_impl<To, const From*> {
    134   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
    135 };
    136 
    137 template<class To, class From> struct cast_retty_impl<To, const From*const> {
    138   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
    139 };
    140 
    141 
    142 template<class To, class From, class SimpleFrom>
    143 struct cast_retty_wrap {
    144   // When the simplified type and the from type are not the same, use the type
    145   // simplifier to reduce the type, then reuse cast_retty_impl to get the
    146   // resultant type.
    147   typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
    148 };
    149 
    150 template<class To, class FromTy>
    151 struct cast_retty_wrap<To, FromTy, FromTy> {
    152   // When the simplified type is equal to the from type, use it directly.
    153   typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
    154 };
    155 
    156 template<class To, class From>
    157 struct cast_retty {
    158   typedef typename cast_retty_wrap<To, From,
    159                    typename simplify_type<From>::SimpleType>::ret_type ret_type;
    160 };
    161 
    162 // Ensure the non-simple values are converted using the simplify_type template
    163 // that may be specialized by smart pointers...
    164 //
    165 template<class To, class From, class SimpleFrom> struct cast_convert_val {
    166   // This is not a simple type, use the template to simplify it...
    167   static typename cast_retty<To, From>::ret_type doit(const From &Val) {
    168     return cast_convert_val<To, SimpleFrom,
    169       typename simplify_type<SimpleFrom>::SimpleType>::doit(
    170                           simplify_type<From>::getSimplifiedValue(Val));
    171   }
    172 };
    173 
    174 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
    175   // This _is_ a simple type, just cast it.
    176   static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
    177     typename cast_retty<To, FromTy>::ret_type Res2
    178      = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
    179     return Res2;
    180   }
    181 };
    182 
    183 
    184 
    185 // cast<X> - Return the argument parameter cast to the specified type.  This
    186 // casting operator asserts that the type is correct, so it does not return null
    187 // on failure.  It does not allow a null argument (use cast_or_null for that).
    188 // It is typically used like this:
    189 //
    190 //  cast<Instruction>(myVal)->getParent()
    191 //
    192 template <class X, class Y>
    193 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
    194   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
    195   return cast_convert_val<X, Y,
    196                           typename simplify_type<Y>::SimpleType>::doit(Val);
    197 }
    198 
    199 // cast_or_null<X> - Functionally identical to cast, except that a null value is
    200 // accepted.
    201 //
    202 template <class X, class Y>
    203 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
    204   if (Val == 0) return 0;
    205   assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
    206   return cast<X>(Val);
    207 }
    208 
    209 
    210 // dyn_cast<X> - Return the argument parameter cast to the specified type.  This
    211 // casting operator returns null if the argument is of the wrong type, so it can
    212 // be used to test for a type as well as cast if successful.  This should be
    213 // used in the context of an if statement like this:
    214 //
    215 //  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
    216 //
    217 
    218 template <class X, class Y>
    219 inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
    220   return isa<X>(Val) ? cast<X, Y>(Val) : 0;
    221 }
    222 
    223 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
    224 // value is accepted.
    225 //
    226 template <class X, class Y>
    227 inline typename cast_retty<X, Y*>::ret_type dyn_cast_or_null(Y *Val) {
    228   return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
    229 }
    230 
    231 } // End llvm namespace
    232 
    233 #endif
    234