1 //===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
11 // and dyn_cast_or_null<X>() templates.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_CASTING_H
16 #define LLVM_SUPPORT_CASTING_H
22 //===----------------------------------------------------------------------===//
23 // isa<x> Support Templates
24 //===----------------------------------------------------------------------===//
26 template<typename FromCl> struct isa_impl_cl;
28 // Define a template that can be specialized by smart pointers to reflect the
29 // fact that they are automatically dereferenced, and are not involved with the
30 // template selection process... the default implementation is a noop.
32 template<typename From> struct simplify_type {
33 typedef From SimpleType; // The real type this represents...
35 // An accessor to get the real value...
36 static SimpleType &getSimplifiedValue(From &Val) { return Val; }
39 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));
47 // isa<X> - Return true if the parameter to the template is an instance of the
48 // template type argument. Used like this:
50 // if (isa<Type*>(myVal)) { ... }
52 template <typename To, typename From>
53 inline bool isa_impl(const From &Val) {
54 return To::classof(&Val);
57 template<typename To, typename From, typename SimpleType>
58 struct 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));
67 template<typename To, typename FromTy>
68 struct 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);
75 // isa_impl_cl - Use class partial specialization to transform types to a single
76 // canonical form for isa_impl.
78 template<typename FromCl>
81 static bool isa(const FromCl &Val) {
82 return isa_impl_wrap<ToCl,const FromCl,
83 typename simplify_type<const FromCl>::SimpleType>::doit(Val);
87 // Specialization used to strip const qualifiers off of the FromCl type...
88 template<typename FromCl>
89 struct isa_impl_cl<const FromCl> {
91 static bool isa(const FromCl &Val) {
92 return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
96 // Define pointer traits in terms of base traits...
97 template<class FromCl>
98 struct isa_impl_cl<FromCl*> {
100 static bool isa(FromCl *Val) {
101 return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
105 // Define reference traits in terms of base traits...
106 template<class FromCl>
107 struct isa_impl_cl<FromCl&> {
109 static bool isa(FromCl &Val) {
110 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
114 template <class X, class Y>
115 inline bool isa(const Y &Val) {
116 return isa_impl_cl<Y>::template isa<X>(Val);
119 //===----------------------------------------------------------------------===//
120 // cast<x> Support Templates
121 //===----------------------------------------------------------------------===//
123 template<class To, class From> struct cast_retty;
126 // Calculate what type the 'cast' function should return, based on a requested
127 // type of To and a source type of From.
128 template<class To, class From> struct cast_retty_impl {
129 typedef To& ret_type; // Normal case, return Ty&
131 template<class To, class From> struct cast_retty_impl<To, const From> {
132 typedef const To &ret_type; // Normal case, return Ty&
135 template<class To, class From> struct cast_retty_impl<To, From*> {
136 typedef To* ret_type; // Pointer arg case, return Ty*
139 template<class To, class From> struct cast_retty_impl<To, const From*> {
140 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
143 template<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*
148 template<class To, class From, class SimpleFrom>
149 struct 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
153 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
156 template<class To, class FromTy>
157 struct 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;
162 template<class To, class From>
164 typedef typename cast_retty_wrap<To, From,
165 typename simplify_type<From>::SimpleType>::ret_type ret_type;
168 // Ensure the non-simple values are converted using the simplify_type template
169 // that may be specialized by smart pointers...
171 template<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));
180 template<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));
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.
195 // cast<Instruction>(myVal)->getParent()
197 template <class X, class Y>
198 inline 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);
204 // cast_or_null<X> - Functionally identical to cast, except that a null value is
207 template <class X, class Y>
208 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
209 if (Val == 0) return 0;
210 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
215 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
216 // casting operator returns null if the argument is of the wrong type, so it can
217 // be used to test for a type as well as cast if successful. This should be
218 // used in the context of an if statement like this:
220 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
223 template <class X, class Y>
224 inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
225 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
228 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
229 // value is accepted.
231 template <class X, class Y>
232 inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(const Y &Val) {
233 return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
237 #ifdef DEBUG_CAST_OPERATORS
238 #include "llvm/Support/raw_ostream.h"
247 /* static bool classof(const bar *X) {
248 cerr << "Classof: " << X << "\n";
253 template <> inline bool isa_impl<foo,bar>(const bar &Val) {
254 errs() << "Classof: " << &Val << "\n";
260 void test(bar &B1, const bar *B2) {
261 // test various configurations of const
263 const bar *const B4 = B2;
266 if (!isa<foo>(B1)) return;
267 if (!isa<foo>(B2)) return;
268 if (!isa<foo>(B3)) return;
269 if (!isa<foo>(B4)) return;
272 foo &F1 = cast<foo>(B1);
273 const foo *F3 = cast<foo>(B2);
274 const foo *F4 = cast<foo>(B2);
275 const foo &F8 = cast<foo>(B3);
276 const foo *F9 = cast<foo>(B4);
277 foo *F10 = cast<foo>(fub());
280 const foo *F11 = cast_or_null<foo>(B2);
281 const foo *F12 = cast_or_null<foo>(B2);
282 const foo *F13 = cast_or_null<foo>(B4);
283 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
285 // These lines are errors...
286 //foo *F20 = cast<foo>(B2); // Yields const foo*
287 //foo &F21 = cast<foo>(B3); // Yields const foo&
288 //foo *F22 = cast<foo>(B4); // Yields const foo*
289 //foo &F23 = cast_or_null<foo>(B1);
290 //const foo &F24 = cast_or_null<foo>(B3);
293 bar *fub() { return 0; }
301 } // End llvm namespace