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