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 "llvm/Support/type_traits.h"
     19 #include <cassert>
     20 
     21 namespace llvm {
     22 
     23 //===----------------------------------------------------------------------===//
     24 //                          isa<x> Support Templates
     25 //===----------------------------------------------------------------------===//
     26 
     27 // Define a template that can be specialized by smart pointers to reflect the
     28 // fact that they are automatically dereferenced, and are not involved with the
     29 // template selection process...  the default implementation is a noop.
     30 //
     31 template<typename From> struct simplify_type {
     32   typedef       From SimpleType;        // The real type this represents...
     33 
     34   // An accessor to get the real value...
     35   static SimpleType &getSimplifiedValue(From &Val) { return Val; }
     36 };
     37 
     38 template<typename From> struct simplify_type<const From> {
     39   typedef typename simplify_type<From>::SimpleType NonConstSimpleType;
     40   typedef typename add_const_past_pointer<NonConstSimpleType>::type
     41     SimpleType;
     42   typedef typename add_lvalue_reference_if_not_pointer<SimpleType>::type
     43     RetType;
     44   static RetType getSimplifiedValue(const From& Val) {
     45     return simplify_type<From>::getSimplifiedValue(const_cast<From&>(Val));
     46   }
     47 };
     48 
     49 // The core of the implementation of isa<X> is here; To and From should be
     50 // the names of classes.  This template can be specialized to customize the
     51 // implementation of isa<> without rewriting it from scratch.
     52 template <typename To, typename From, typename Enabler = void>
     53 struct isa_impl {
     54   static inline bool doit(const From &Val) {
     55     return To::classof(&Val);
     56   }
     57 };
     58 
     59 /// \brief Always allow upcasts, and perform no dynamic check for them.
     60 template <typename To, typename From>
     61 struct isa_impl<To, From,
     62                 typename enable_if<
     63                   llvm::is_base_of<To, From>
     64                 >::type
     65                > {
     66   static inline bool doit(const From &) { return true; }
     67 };
     68 
     69 template <typename To, typename From> struct isa_impl_cl {
     70   static inline bool doit(const From &Val) {
     71     return isa_impl<To, From>::doit(Val);
     72   }
     73 };
     74 
     75 template <typename To, typename From> struct isa_impl_cl<To, const From> {
     76   static inline bool doit(const From &Val) {
     77     return isa_impl<To, From>::doit(Val);
     78   }
     79 };
     80 
     81 template <typename To, typename From> struct isa_impl_cl<To, From*> {
     82   static inline bool doit(const From *Val) {
     83     assert(Val && "isa<> used on a null pointer");
     84     return isa_impl<To, From>::doit(*Val);
     85   }
     86 };
     87 
     88 template <typename To, typename From> struct isa_impl_cl<To, From*const> {
     89   static inline bool doit(const From *Val) {
     90     assert(Val && "isa<> used on a null pointer");
     91     return isa_impl<To, From>::doit(*Val);
     92   }
     93 };
     94 
     95 template <typename To, typename From> struct isa_impl_cl<To, const From*> {
     96   static inline bool doit(const From *Val) {
     97     assert(Val && "isa<> used on a null pointer");
     98     return isa_impl<To, From>::doit(*Val);
     99   }
    100 };
    101 
    102 template <typename To, typename From> struct isa_impl_cl<To, const From*const> {
    103   static inline bool doit(const From *Val) {
    104     assert(Val && "isa<> used on a null pointer");
    105     return isa_impl<To, From>::doit(*Val);
    106   }
    107 };
    108 
    109 template<typename To, typename From, typename SimpleFrom>
    110 struct isa_impl_wrap {
    111   // When From != SimplifiedType, we can simplify the type some more by using
    112   // the simplify_type template.
    113   static bool doit(const From &Val) {
    114     return isa_impl_wrap<To, SimpleFrom,
    115       typename simplify_type<SimpleFrom>::SimpleType>::doit(
    116                           simplify_type<const From>::getSimplifiedValue(Val));
    117   }
    118 };
    119 
    120 template<typename To, typename FromTy>
    121 struct isa_impl_wrap<To, FromTy, FromTy> {
    122   // When From == SimpleType, we are as simple as we are going to get.
    123   static bool doit(const FromTy &Val) {
    124     return isa_impl_cl<To,FromTy>::doit(Val);
    125   }
    126 };
    127 
    128 // isa<X> - Return true if the parameter to the template is an instance of the
    129 // template type argument.  Used like this:
    130 //
    131 //  if (isa<Type>(myVal)) { ... }
    132 //
    133 template <class X, class Y>
    134 inline bool isa(const Y &Val) {
    135   return isa_impl_wrap<X, const Y,
    136                        typename simplify_type<const Y>::SimpleType>::doit(Val);
    137 }
    138 
    139 //===----------------------------------------------------------------------===//
    140 //                          cast<x> Support Templates
    141 //===----------------------------------------------------------------------===//
    142 
    143 template<class To, class From> struct cast_retty;
    144 
    145 
    146 // Calculate what type the 'cast' function should return, based on a requested
    147 // type of To and a source type of From.
    148 template<class To, class From> struct cast_retty_impl {
    149   typedef To& ret_type;         // Normal case, return Ty&
    150 };
    151 template<class To, class From> struct cast_retty_impl<To, const From> {
    152   typedef const To &ret_type;   // Normal case, return Ty&
    153 };
    154 
    155 template<class To, class From> struct cast_retty_impl<To, From*> {
    156   typedef To* ret_type;         // Pointer arg case, return Ty*
    157 };
    158 
    159 template<class To, class From> struct cast_retty_impl<To, const From*> {
    160   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
    161 };
    162 
    163 template<class To, class From> struct cast_retty_impl<To, const From*const> {
    164   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
    165 };
    166 
    167 
    168 template<class To, class From, class SimpleFrom>
    169 struct cast_retty_wrap {
    170   // When the simplified type and the from type are not the same, use the type
    171   // simplifier to reduce the type, then reuse cast_retty_impl to get the
    172   // resultant type.
    173   typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
    174 };
    175 
    176 template<class To, class FromTy>
    177 struct cast_retty_wrap<To, FromTy, FromTy> {
    178   // When the simplified type is equal to the from type, use it directly.
    179   typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
    180 };
    181 
    182 template<class To, class From>
    183 struct cast_retty {
    184   typedef typename cast_retty_wrap<To, From,
    185                    typename simplify_type<From>::SimpleType>::ret_type ret_type;
    186 };
    187 
    188 // Ensure the non-simple values are converted using the simplify_type template
    189 // that may be specialized by smart pointers...
    190 //
    191 template<class To, class From, class SimpleFrom> struct cast_convert_val {
    192   // This is not a simple type, use the template to simplify it...
    193   static typename cast_retty<To, From>::ret_type doit(From &Val) {
    194     return cast_convert_val<To, SimpleFrom,
    195       typename simplify_type<SimpleFrom>::SimpleType>::doit(
    196                           simplify_type<From>::getSimplifiedValue(Val));
    197   }
    198 };
    199 
    200 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
    201   // This _is_ a simple type, just cast it.
    202   static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
    203     typename cast_retty<To, FromTy>::ret_type Res2
    204      = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
    205     return Res2;
    206   }
    207 };
    208 
    209 template <class X> struct is_simple_type {
    210   static const bool value =
    211       is_same<X, typename simplify_type<X>::SimpleType>::value;
    212 };
    213 
    214 // cast<X> - Return the argument parameter cast to the specified type.  This
    215 // casting operator asserts that the type is correct, so it does not return null
    216 // on failure.  It does not allow a null argument (use cast_or_null for that).
    217 // It is typically used like this:
    218 //
    219 //  cast<Instruction>(myVal)->getParent()
    220 //
    221 template <class X, class Y>
    222 inline typename enable_if_c<!is_simple_type<Y>::value,
    223                             typename cast_retty<X, const Y>::ret_type>::type
    224 cast(const Y &Val) {
    225   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
    226   return cast_convert_val<
    227       X, const Y, typename simplify_type<const Y>::SimpleType>::doit(Val);
    228 }
    229 
    230 template <class X, class Y>
    231 inline typename cast_retty<X, Y>::ret_type cast(Y &Val) {
    232   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
    233   return cast_convert_val<X, Y,
    234                           typename simplify_type<Y>::SimpleType>::doit(Val);
    235 }
    236 
    237 template <class X, class Y>
    238 inline typename cast_retty<X, Y *>::ret_type cast(Y *Val) {
    239   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
    240   return cast_convert_val<X, Y*,
    241                           typename simplify_type<Y*>::SimpleType>::doit(Val);
    242 }
    243 
    244 // cast_or_null<X> - Functionally identical to cast, except that a null value is
    245 // accepted.
    246 //
    247 template <class X, class Y>
    248 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
    249   if (Val == 0) return 0;
    250   assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
    251   return cast<X>(Val);
    252 }
    253 
    254 
    255 // dyn_cast<X> - Return the argument parameter cast to the specified type.  This
    256 // casting operator returns null if the argument is of the wrong type, so it can
    257 // be used to test for a type as well as cast if successful.  This should be
    258 // used in the context of an if statement like this:
    259 //
    260 //  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
    261 //
    262 
    263 template <class X, class Y>
    264 inline typename enable_if_c<!is_simple_type<Y>::value,
    265                             typename cast_retty<X, const Y>::ret_type>::type
    266 dyn_cast(const Y &Val) {
    267   return isa<X>(Val) ? cast<X>(Val) : 0;
    268 }
    269 
    270 template <class X, class Y>
    271 inline typename cast_retty<X, Y>::ret_type dyn_cast(Y &Val) {
    272   return isa<X>(Val) ? cast<X>(Val) : 0;
    273 }
    274 
    275 template <class X, class Y>
    276 inline typename cast_retty<X, Y *>::ret_type dyn_cast(Y *Val) {
    277   return isa<X>(Val) ? cast<X>(Val) : 0;
    278 }
    279 
    280 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
    281 // value is accepted.
    282 //
    283 template <class X, class Y>
    284 inline typename cast_retty<X, Y*>::ret_type dyn_cast_or_null(Y *Val) {
    285   return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
    286 }
    287 
    288 } // End llvm namespace
    289 
    290 #endif
    291