1 //===-- Support/Casting.h - Allow flexible, checked, casts ------*- C++ -*-===//
3 // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
4 // and dyn_cast_or_null<X>() templates.
6 //===----------------------------------------------------------------------===//
8 #ifndef SUPPORT_CASTING_H
9 #define SUPPORT_CASTING_H
11 //===----------------------------------------------------------------------===//
12 // isa<x> Support Templates
13 //===----------------------------------------------------------------------===//
15 template<typename FromCl> struct isa_impl_cl;
17 // Define a template that can be specialized by smart pointers to reflect the
18 // fact that they are automatically dereferenced, and are not involved with the
19 // template selection process... the default implementation is a noop.
21 template<typename From> struct simplify_type {
22 typedef From SimpleType; // The real type this represents...
24 // An accessor to get the real value...
25 static SimpleType &getSimplifiedValue(From &Val) { return Val; }
28 template<typename From> struct simplify_type<const From> {
29 typedef const From SimpleType;
30 static SimpleType &getSimplifiedValue(const From &Val) {
31 return simplify_type<From>::getSimplifiedValue((From&)Val);
36 // isa<X> - Return true if the parameter to the template is an instance of the
37 // template type argument. Used like this:
39 // if (isa<Type*>(myVal)) { ... }
41 template <typename To, typename From>
42 inline bool isa_impl(const From &Val) {
43 return To::classof(&Val);
46 template<typename To, typename From, typename SimpleType>
47 struct isa_impl_wrap {
48 // When From != SimplifiedType, we can simplify the type some more by using
49 // the simplify_type template.
50 static bool doit(const From &Val) {
51 return isa_impl_cl<const SimpleType>::template
52 isa<To>(simplify_type<const From>::getSimplifiedValue(Val));
56 template<typename To, typename FromTy>
57 struct isa_impl_wrap<To, const FromTy, const FromTy> {
58 // When From == SimpleType, we are as simple as we are going to get.
59 static bool doit(const FromTy &Val) {
60 return isa_impl<To,FromTy>(Val);
64 // isa_impl_cl - Use class partial specialization to transform types to a single
65 // canonical form for isa_impl.
67 template<typename FromCl>
70 static bool isa(const FromCl &Val) {
71 return isa_impl_wrap<ToCl,const FromCl,
72 typename simplify_type<const FromCl>::SimpleType>::doit(Val);
76 // Specialization used to strip const qualifiers off of the FromCl type...
77 template<typename FromCl>
78 struct isa_impl_cl<const FromCl> {
80 static bool isa(const FromCl &Val) {
81 return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
85 // Define pointer traits in terms of base traits...
86 template<class FromCl>
87 struct isa_impl_cl<FromCl*> {
89 static bool isa(FromCl *Val) {
90 return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
94 // Define reference traits in terms of base traits...
95 template<class FromCl>
96 struct isa_impl_cl<FromCl&> {
98 static bool isa(FromCl &Val) {
99 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
103 template <class X, class Y>
104 inline bool isa(const Y &Val) {
105 return isa_impl_cl<Y>::template isa<X>(Val);
108 //===----------------------------------------------------------------------===//
109 // cast<x> Support Templates
110 //===----------------------------------------------------------------------===//
112 template<class To, class From> struct cast_retty;
115 // Calculate what type the 'cast' function should return, based on a requested
116 // type of To and a source type of From.
117 template<class To, class From> struct cast_retty_impl {
118 typedef To& ret_type; // Normal case, return Ty&
120 template<class To, class From> struct cast_retty_impl<To, const From> {
121 typedef const To &ret_type; // Normal case, return Ty&
124 template<class To, class From> struct cast_retty_impl<To, From*> {
125 typedef To* ret_type; // Pointer arg case, return Ty*
128 template<class To, class From> struct cast_retty_impl<To, const From*> {
129 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
132 template<class To, class From> struct cast_retty_impl<To, const From*const> {
133 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
137 template<class To, class From, class SimpleFrom>
138 struct cast_retty_wrap {
139 // When the simplified type and the from type are not the same, use the type
140 // simplifier to reduce the type, then reuse cast_retty_impl to get the
142 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
145 template<class To, class FromTy>
146 struct cast_retty_wrap<To, FromTy, FromTy> {
147 // When the simplified type is equal to the from type, use it directly.
148 typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
151 template<class To, class From>
153 typedef typename cast_retty_wrap<To, From,
154 typename simplify_type<From>::SimpleType>::ret_type ret_type;
157 // Ensure the non-simple values are converted using the simplify_type template
158 // that may be specialized by smart pointers...
160 template<class To, class From, class SimpleFrom> struct cast_convert_val {
161 // This is not a simple type, use the template to simplify it...
162 static typename cast_retty<To, From>::ret_type doit(const From &Val) {
163 return cast_convert_val<To, SimpleFrom,
164 typename simplify_type<SimpleFrom>::SimpleType>::doit(
165 simplify_type<From>::getSimplifiedValue(Val));
169 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
170 // This _is_ a simple type, just cast it.
171 static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
172 return (typename cast_retty<To, FromTy>::ret_type)Val;
178 // cast<X> - Return the argument parameter cast to the specified type. This
179 // casting operator asserts that the type is correct, so it does not return null
180 // on failure. But it will correctly return NULL when the input is NULL.
183 // cast<Instruction>(myVal)->getParent()
185 template <class X, class Y>
186 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
187 assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
188 return cast_convert_val<X, Y,
189 typename simplify_type<Y>::SimpleType>::doit(Val);
192 // cast_or_null<X> - Functionally identical to cast, except that a null value is
195 template <class X, class Y>
196 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
197 if (Val == 0) return 0;
198 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
203 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
204 // casting operator returns null if the argument is of the wrong type, so it can
205 // be used to test for a type as well as cast if successful. This should be
206 // used in the context of an if statement like this:
208 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
211 template <class X, class Y>
212 inline typename cast_retty<X, Y>::ret_type dyn_cast(Y Val) {
213 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
216 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
217 // value is accepted.
219 template <class X, class Y>
220 inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(Y Val) {
221 return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
225 #ifdef DEBUG_CAST_OPERATORS
235 /* static bool classof(const bar *X) {
236 cerr << "Classof: " << X << "\n";
241 template <> inline bool isa_impl<foo,bar>(const bar &Val) {
242 cerr << "Classof: " << &Val << "\n";
248 void test(bar &B1, const bar *B2) {
249 // test various configurations of const
251 const bar *const B4 = B2;
254 if (!isa<foo>(B1)) return;
255 if (!isa<foo>(B2)) return;
256 if (!isa<foo>(B3)) return;
257 if (!isa<foo>(B4)) return;
260 foo &F1 = cast<foo>(B1);
261 const foo *F3 = cast<foo>(B2);
262 const foo *F4 = cast<foo>(B2);
263 const foo &F8 = cast<foo>(B3);
264 const foo *F9 = cast<foo>(B4);
265 foo *F10 = cast<foo>(fub());
268 const foo *F11 = cast_or_null<foo>(B2);
269 const foo *F12 = cast_or_null<foo>(B2);
270 const foo *F13 = cast_or_null<foo>(B4);
271 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
273 // These lines are errors...
274 //foo *F20 = cast<foo>(B2); // Yields const foo*
275 //foo &F21 = cast<foo>(B3); // Yields const foo&
276 //foo *F22 = cast<foo>(B4); // Yields const foo*
277 //foo &F23 = cast_or_null<foo>(B1);
278 //const foo &F24 = cast_or_null<foo>(B3);
281 bar *fub() { return 0; }