1 //===-- Support/Casting.h - Allow flexible, checked, casts ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 SUPPORT_CASTING_H
16 #define SUPPORT_CASTING_H
20 //===----------------------------------------------------------------------===//
21 // isa<x> Support Templates
22 //===----------------------------------------------------------------------===//
24 template<typename FromCl> struct isa_impl_cl;
26 // Define a template that can be specialized by smart pointers to reflect the
27 // fact that they are automatically dereferenced, and are not involved with the
28 // template selection process... the default implementation is a noop.
30 template<typename From> struct simplify_type {
31 typedef From SimpleType; // The real type this represents...
33 // An accessor to get the real value...
34 static SimpleType &getSimplifiedValue(From &Val) { return Val; }
37 template<typename From> struct simplify_type<const From> {
38 typedef const From SimpleType;
39 static SimpleType &getSimplifiedValue(const From &Val) {
40 return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val));
45 // isa<X> - Return true if the parameter to the template is an instance of the
46 // template type argument. Used like this:
48 // if (isa<Type*>(myVal)) { ... }
50 template <typename To, typename From>
51 inline bool isa_impl(const From &Val) {
52 return To::classof(&Val);
55 template<typename To, typename From, typename SimpleType>
56 struct isa_impl_wrap {
57 // When From != SimplifiedType, we can simplify the type some more by using
58 // the simplify_type template.
59 static bool doit(const From &Val) {
60 return isa_impl_cl<const SimpleType>::template
61 isa<To>(simplify_type<const From>::getSimplifiedValue(Val));
65 template<typename To, typename FromTy>
66 struct isa_impl_wrap<To, const FromTy, const FromTy> {
67 // When From == SimpleType, we are as simple as we are going to get.
68 static bool doit(const FromTy &Val) {
69 return isa_impl<To,FromTy>(Val);
73 // isa_impl_cl - Use class partial specialization to transform types to a single
74 // canonical form for isa_impl.
76 template<typename FromCl>
79 static bool isa(const FromCl &Val) {
80 return isa_impl_wrap<ToCl,const FromCl,
81 typename simplify_type<const FromCl>::SimpleType>::doit(Val);
85 // Specialization used to strip const qualifiers off of the FromCl type...
86 template<typename FromCl>
87 struct isa_impl_cl<const FromCl> {
89 static bool isa(const FromCl &Val) {
90 return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
94 // Define pointer 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 // Define reference traits in terms of base traits...
104 template<class FromCl>
105 struct isa_impl_cl<FromCl&> {
107 static bool isa(FromCl &Val) {
108 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
112 template <class X, class Y>
113 inline bool isa(const Y &Val) {
114 return isa_impl_cl<Y>::template isa<X>(Val);
117 //===----------------------------------------------------------------------===//
118 // cast<x> Support Templates
119 //===----------------------------------------------------------------------===//
121 template<class To, class From> struct cast_retty;
124 // Calculate what type the 'cast' function should return, based on a requested
125 // type of To and a source type of From.
126 template<class To, class From> struct cast_retty_impl {
127 typedef To& ret_type; // Normal case, return Ty&
129 template<class To, class From> struct cast_retty_impl<To, const From> {
130 typedef const To &ret_type; // Normal case, return Ty&
133 template<class To, class From> struct cast_retty_impl<To, From*> {
134 typedef To* ret_type; // Pointer arg case, return Ty*
137 template<class To, class From> struct cast_retty_impl<To, const From*> {
138 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
141 template<class To, class From> struct cast_retty_impl<To, const From*const> {
142 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
146 template<class To, class From, class SimpleFrom>
147 struct cast_retty_wrap {
148 // When the simplified type and the from type are not the same, use the type
149 // simplifier to reduce the type, then reuse cast_retty_impl to get the
151 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
154 template<class To, class FromTy>
155 struct cast_retty_wrap<To, FromTy, FromTy> {
156 // When the simplified type is equal to the from type, use it directly.
157 typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
160 template<class To, class From>
162 typedef typename cast_retty_wrap<To, From,
163 typename simplify_type<From>::SimpleType>::ret_type ret_type;
166 // Ensure the non-simple values are converted using the simplify_type template
167 // that may be specialized by smart pointers...
169 template<class To, class From, class SimpleFrom> struct cast_convert_val {
170 // This is not a simple type, use the template to simplify it...
171 static typename cast_retty<To, From>::ret_type doit(const From &Val) {
172 return cast_convert_val<To, SimpleFrom,
173 typename simplify_type<SimpleFrom>::SimpleType>::doit(
174 simplify_type<From>::getSimplifiedValue(Val));
178 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
179 // This _is_ a simple type, just cast it.
180 static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
181 return reinterpret_cast<typename cast_retty<To, FromTy>::ret_type>(
182 const_cast<FromTy&>(Val));
188 // cast<X> - Return the argument parameter cast to the specified type. This
189 // casting operator asserts that the type is correct, so it does not return null
190 // on failure. But it will correctly return NULL when the input is NULL.
193 // cast<Instruction>(myVal)->getParent()
195 template <class X, class Y>
196 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
197 assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
198 return cast_convert_val<X, Y,
199 typename simplify_type<Y>::SimpleType>::doit(Val);
202 // cast_or_null<X> - Functionally identical to cast, except that a null value is
205 template <class X, class Y>
206 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
207 if (Val == 0) return 0;
208 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
213 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
214 // casting operator returns null if the argument is of the wrong type, so it can
215 // be used to test for a type as well as cast if successful. This should be
216 // used in the context of an if statement like this:
218 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
221 template <class X, class Y>
222 inline typename cast_retty<X, Y>::ret_type dyn_cast(Y Val) {
223 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
226 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
227 // value is accepted.
229 template <class X, class Y>
230 inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(Y Val) {
231 return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
235 #ifdef DEBUG_CAST_OPERATORS
245 /* static bool classof(const bar *X) {
246 cerr << "Classof: " << X << "\n";
251 template <> inline bool isa_impl<foo,bar>(const bar &Val) {
252 cerr << "Classof: " << &Val << "\n";
258 void test(bar &B1, const bar *B2) {
259 // test various configurations of const
261 const bar *const B4 = B2;
264 if (!isa<foo>(B1)) return;
265 if (!isa<foo>(B2)) return;
266 if (!isa<foo>(B3)) return;
267 if (!isa<foo>(B4)) return;
270 foo &F1 = cast<foo>(B1);
271 const foo *F3 = cast<foo>(B2);
272 const foo *F4 = cast<foo>(B2);
273 const foo &F8 = cast<foo>(B3);
274 const foo *F9 = cast<foo>(B4);
275 foo *F10 = cast<foo>(fub());
278 const foo *F11 = cast_or_null<foo>(B2);
279 const foo *F12 = cast_or_null<foo>(B2);
280 const foo *F13 = cast_or_null<foo>(B4);
281 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
283 // These lines are errors...
284 //foo *F20 = cast<foo>(B2); // Yields const foo*
285 //foo &F21 = cast<foo>(B3); // Yields const foo&
286 //foo *F22 = cast<foo>(B4); // Yields const foo*
287 //foo &F23 = cast_or_null<foo>(B1);
288 //const foo &F24 = cast_or_null<foo>(B3);
291 bar *fub() { return 0; }
299 } // End llvm namespace