Casting.h revision 8b6d86df062f9402c0cde66eb6c7013afb414285
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===// 2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// The LLVM Compiler Infrastructure 4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// This file is distributed under the University of Illinois Open Source 6a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// License. See LICENSE.TXT for details. 7a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//===----------------------------------------------------------------------===// 9a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(), 11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// and dyn_cast_or_null<X>() templates. 12a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 13a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//===----------------------------------------------------------------------===// 14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#ifndef LLVM_SUPPORT_CASTING_H 16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define LLVM_SUPPORT_CASTING_H 17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <cassert> 19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace llvm { 21a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//===----------------------------------------------------------------------===// 23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// isa<x> Support Templates 24a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//===----------------------------------------------------------------------===// 25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 26a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)template<typename FromCl> struct isa_impl_cl; 27a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 28a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Define a template that can be specialized by smart pointers to reflect the 29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// fact that they are automatically dereferenced, and are not involved with the 30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// template selection process... the default implementation is a noop. 31a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// 32a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)template<typename From> struct simplify_type { 33a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) typedef From SimpleType; // The real type this represents... 34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) // An accessor to get the real value... 36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) static SimpleType &getSimplifiedValue(From &Val) { return Val; } 37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}; 38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) 39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)template<typename From> struct simplify_type<const From> { 40 typedef const From SimpleType; 41 static SimpleType &getSimplifiedValue(const From &Val) { 42 return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val)); 43 } 44}; 45 46 47// isa<X> - Return true if the parameter to the template is an instance of the 48// template type argument. Used like this: 49// 50// if (isa<Type*>(myVal)) { ... } 51// 52template <typename To, typename From> 53inline bool isa_impl(const From &Val) { 54 return To::classof(&Val); 55} 56 57template<typename To, typename From, typename SimpleType> 58struct isa_impl_wrap { 59 // When From != SimplifiedType, we can simplify the type some more by using 60 // the simplify_type template. 61 static bool doit(const From &Val) { 62 return isa_impl_cl<const SimpleType>::template 63 isa<To>(simplify_type<const From>::getSimplifiedValue(Val)); 64 } 65}; 66 67template<typename To, typename FromTy> 68struct isa_impl_wrap<To, const FromTy, const FromTy> { 69 // When From == SimpleType, we are as simple as we are going to get. 70 static bool doit(const FromTy &Val) { 71 return isa_impl<To,FromTy>(Val); 72 } 73}; 74 75// isa_impl_cl - Use class partial specialization to transform types to a single 76// canonical form for isa_impl. 77// 78template<typename FromCl> 79struct isa_impl_cl { 80 template<class ToCl> 81 static bool isa(const FromCl &Val) { 82 return isa_impl_wrap<ToCl,const FromCl, 83 typename simplify_type<const FromCl>::SimpleType>::doit(Val); 84 } 85}; 86 87// Specialization used to strip const qualifiers off of the FromCl type... 88template<typename FromCl> 89struct isa_impl_cl<const FromCl> { 90 template<class ToCl> 91 static bool isa(const FromCl &Val) { 92 return isa_impl_cl<FromCl>::template isa<ToCl>(Val); 93 } 94}; 95 96// Define pointer traits in terms of base traits... 97template<class FromCl> 98struct isa_impl_cl<FromCl*> { 99 template<class ToCl> 100 static bool isa(FromCl *Val) { 101 return isa_impl_cl<FromCl>::template isa<ToCl>(*Val); 102 } 103}; 104 105// Define reference traits in terms of base traits... 106template<class FromCl> 107struct isa_impl_cl<FromCl&> { 108 template<class ToCl> 109 static bool isa(FromCl &Val) { 110 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val); 111 } 112}; 113 114template <class X, class Y> 115inline bool isa(const Y &Val) { 116 return isa_impl_cl<Y>::template isa<X>(Val); 117} 118 119//===----------------------------------------------------------------------===// 120// cast<x> Support Templates 121//===----------------------------------------------------------------------===// 122 123template<class To, class From> struct cast_retty; 124 125 126// Calculate what type the 'cast' function should return, based on a requested 127// type of To and a source type of From. 128template<class To, class From> struct cast_retty_impl { 129 typedef To& ret_type; // Normal case, return Ty& 130}; 131template<class To, class From> struct cast_retty_impl<To, const From> { 132 typedef const To &ret_type; // Normal case, return Ty& 133}; 134 135template<class To, class From> struct cast_retty_impl<To, From*> { 136 typedef To* ret_type; // Pointer arg case, return Ty* 137}; 138 139template<class To, class From> struct cast_retty_impl<To, const From*> { 140 typedef const To* ret_type; // Constant pointer arg case, return const Ty* 141}; 142 143template<class To, class From> struct cast_retty_impl<To, const From*const> { 144 typedef const To* ret_type; // Constant pointer arg case, return const Ty* 145}; 146 147 148template<class To, class From, class SimpleFrom> 149struct cast_retty_wrap { 150 // When the simplified type and the from type are not the same, use the type 151 // simplifier to reduce the type, then reuse cast_retty_impl to get the 152 // resultant type. 153 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type; 154}; 155 156template<class To, class FromTy> 157struct cast_retty_wrap<To, FromTy, FromTy> { 158 // When the simplified type is equal to the from type, use it directly. 159 typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type; 160}; 161 162template<class To, class From> 163struct cast_retty { 164 typedef typename cast_retty_wrap<To, From, 165 typename simplify_type<From>::SimpleType>::ret_type ret_type; 166}; 167 168// Ensure the non-simple values are converted using the simplify_type template 169// that may be specialized by smart pointers... 170// 171template<class To, class From, class SimpleFrom> struct cast_convert_val { 172 // This is not a simple type, use the template to simplify it... 173 static typename cast_retty<To, From>::ret_type doit(const From &Val) { 174 return cast_convert_val<To, SimpleFrom, 175 typename simplify_type<SimpleFrom>::SimpleType>::doit( 176 simplify_type<From>::getSimplifiedValue(Val)); 177 } 178}; 179 180template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> { 181 // This _is_ a simple type, just cast it. 182 static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) { 183 return reinterpret_cast<typename cast_retty<To, FromTy>::ret_type>( 184 const_cast<FromTy&>(Val)); 185 } 186}; 187 188 189 190// cast<X> - Return the argument parameter cast to the specified type. This 191// casting operator asserts that the type is correct, so it does not return null 192// on failure. But it will correctly return NULL when the input is NULL. 193// Used Like this: 194// 195// cast<Instruction>(myVal)->getParent() 196// 197template <class X, class Y> 198inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) { 199 assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!"); 200 return cast_convert_val<X, Y, 201 typename simplify_type<Y>::SimpleType>::doit(Val); 202} 203 204// cast_or_null<X> - Functionally identical to cast, except that a null value is 205// accepted. 206// 207template <class X, class Y> 208inline typename cast_retty<X, Y>::ret_type cast_or_null(const Y &Val) { 209 typedef typename cast_retty<X, Y>::ret_type ret_type; 210 if (!Val) return ret_type(); 211 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!"); 212 return cast<X>(Val); 213} 214 215 216// dyn_cast<X> - Return the argument parameter cast to the specified type. This 217// casting operator returns null if the argument is of the wrong type, so it can 218// be used to test for a type as well as cast if successful. This should be 219// used in the context of an if statement like this: 220// 221// if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... } 222// 223 224template <class X, class Y> 225inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) { 226 typedef typename cast_retty<X, Y>::ret_type ret_type; 227 return isa<X>(Val) ? cast<X, Y>(Val) : (ret_type)ret_type(); 228} 229 230// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null 231// value is accepted. 232// 233template <class X, class Y> 234inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(const Y &Val) { 235 typedef typename cast_retty<X, Y>::ret_type ret_type; 236 return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : (ret_type)ret_type(); 237} 238 239 240#ifdef DEBUG_CAST_OPERATORS 241#include "llvm/Support/Streams.h" 242 243struct bar { 244 bar() {} 245private: 246 bar(const bar &); 247}; 248struct foo { 249 void ext() const; 250 /* static bool classof(const bar *X) { 251 cerr << "Classof: " << X << "\n"; 252 return true; 253 }*/ 254}; 255 256template <> inline bool isa_impl<foo,bar>(const bar &Val) { 257 cerr << "Classof: " << &Val << "\n"; 258 return true; 259} 260 261 262bar *fub(); 263void test(bar &B1, const bar *B2) { 264 // test various configurations of const 265 const bar &B3 = B1; 266 const bar *const B4 = B2; 267 268 // test isa 269 if (!isa<foo>(B1)) return; 270 if (!isa<foo>(B2)) return; 271 if (!isa<foo>(B3)) return; 272 if (!isa<foo>(B4)) return; 273 274 // test cast 275 foo &F1 = cast<foo>(B1); 276 const foo *F3 = cast<foo>(B2); 277 const foo *F4 = cast<foo>(B2); 278 const foo &F8 = cast<foo>(B3); 279 const foo *F9 = cast<foo>(B4); 280 foo *F10 = cast<foo>(fub()); 281 282 // test cast_or_null 283 const foo *F11 = cast_or_null<foo>(B2); 284 const foo *F12 = cast_or_null<foo>(B2); 285 const foo *F13 = cast_or_null<foo>(B4); 286 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print. 287 288 // These lines are errors... 289 //foo *F20 = cast<foo>(B2); // Yields const foo* 290 //foo &F21 = cast<foo>(B3); // Yields const foo& 291 //foo *F22 = cast<foo>(B4); // Yields const foo* 292 //foo &F23 = cast_or_null<foo>(B1); 293 //const foo &F24 = cast_or_null<foo>(B3); 294} 295 296bar *fub() { return 0; } 297void main() { 298 bar B; 299 test(B, &B); 300} 301 302#endif 303 304} // End llvm namespace 305 306#endif 307