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
13 //===----------------------------------------------------------------------===//
14 // isa<x> Support Templates
15 //===----------------------------------------------------------------------===//
17 template<typename FromCl> struct isa_impl_cl;
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.
23 template<typename From> struct simplify_type {
24 typedef From SimpleType; // The real type this represents...
26 // An accessor to get the real value...
27 static SimpleType &getSimplifiedValue(From &Val) { return Val; }
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);
38 // isa<X> - Return true if the parameter to the template is an instance of the
39 // template type argument. Used like this:
41 // if (isa<Type*>(myVal)) { ... }
43 template <typename To, typename From>
44 inline bool isa_impl(const From &Val) {
45 return To::classof(&Val);
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));
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);
66 // isa_impl_cl - Use class partial specialization to transform types to a single
67 // cannonical form for isa_impl.
69 template<typename FromCl>
72 static bool isa(const FromCl &Val) {
73 return isa_impl_wrap<ToCl,const FromCl,
74 typename simplify_type<const FromCl>::SimpleType>::doit(Val);
78 // Specialization used to strip const qualifiers off of the FromCl type...
79 template<typename FromCl>
80 struct isa_impl_cl<const FromCl> {
82 static bool isa(const FromCl &Val) {
83 return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
87 // Define pointer traits in terms of base traits...
88 template<class FromCl>
89 struct isa_impl_cl<FromCl*> {
91 static bool isa(FromCl *Val) {
92 return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
96 // Define reference 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 template <class X, class Y>
106 inline bool isa(const Y &Val) {
107 return isa_impl_cl<Y>::template isa<X>(Val);
110 //===----------------------------------------------------------------------===//
111 // cast<x> Support Templates
112 //===----------------------------------------------------------------------===//
114 template<class To, class From> struct cast_retty;
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&
122 template<class To, class From> struct cast_retty_impl<To, const From> {
123 typedef const To &ret_type; // Normal case, return Ty&
126 template<class To, class From> struct cast_retty_impl<To, From*> {
127 typedef To* ret_type; // Pointer arg case, return Ty*
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*
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*
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
144 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
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;
153 template<class To, class From>
155 typedef typename cast_retty_wrap<To, From,
156 typename simplify_type<From>::SimpleType>::ret_type ret_type;
159 // Ensure the non-simple values are converted using the simplify_type template
160 // that may be specialized by smart pointers...
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...
164 static typename cast_retty<To, From>::ret_type doit(const From &Val) {
165 return cast_convert_val<To, SimpleFrom,
166 typename simplify_type<SimpleFrom>::SimpleType>::doit(
167 simplify_type<From>::getSimplifiedValue(Val));
171 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
172 // This _is_ a simple type, just cast it.
173 static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
174 return (typename cast_retty<To, FromTy>::ret_type)Val;
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.
185 // cast<Instruction>(myVal)->getParent()
187 template <class X, class Y>
188 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
189 assert(isa<X>(Val) && "cast<Ty>() argument of uncompatible type!");
190 return cast_convert_val<X, Y,
191 typename simplify_type<Y>::SimpleType>::doit(Val);
194 // cast_or_null<X> - Functionally identical to cast, except that a null value is
197 template <class X, class Y>
198 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
199 if (Val == 0) return 0;
200 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of uncompatible type!");
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:
210 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
213 template <class X, class Y>
214 inline typename cast_retty<X, Y>::ret_type dyn_cast(Y Val) {
215 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
218 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
219 // value is accepted.
221 template <class X, class Y>
222 inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(Y Val) {
223 return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
227 #ifdef DEBUG_CAST_OPERATORS
237 /* static bool classof(const bar *X) {
238 cerr << "Classof: " << X << "\n";
243 template <> inline bool isa_impl<foo,bar>(const bar &Val) {
244 cerr << "Classof: " << &Val << "\n";
250 void test(bar &B1, const bar *B2) {
251 // test various configurations of const
253 const bar *const B4 = B2;
256 if (!isa<foo>(B1)) return;
257 if (!isa<foo>(B2)) return;
258 if (!isa<foo>(B3)) return;
259 if (!isa<foo>(B4)) return;
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());
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.
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);
283 bar *fub() { return 0; }