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((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 (typename cast_retty<To, FromTy>::ret_type)Val;
187 // cast<X> - Return the argument parameter cast to the specified type. This
188 // casting operator asserts that the type is correct, so it does not return null
189 // on failure. But it will correctly return NULL when the input is NULL.
192 // cast<Instruction>(myVal)->getParent()
194 template <class X, class Y>
195 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
196 assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
197 return cast_convert_val<X, Y,
198 typename simplify_type<Y>::SimpleType>::doit(Val);
201 // cast_or_null<X> - Functionally identical to cast, except that a null value is
204 template <class X, class Y>
205 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
206 if (Val == 0) return 0;
207 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
212 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
213 // casting operator returns null if the argument is of the wrong type, so it can
214 // be used to test for a type as well as cast if successful. This should be
215 // used in the context of an if statement like this:
217 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
220 template <class X, class Y>
221 inline typename cast_retty<X, Y>::ret_type dyn_cast(Y Val) {
222 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
225 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
226 // value is accepted.
228 template <class X, class Y>
229 inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(Y Val) {
230 return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
234 #ifdef DEBUG_CAST_OPERATORS
244 /* static bool classof(const bar *X) {
245 cerr << "Classof: " << X << "\n";
250 template <> inline bool isa_impl<foo,bar>(const bar &Val) {
251 cerr << "Classof: " << &Val << "\n";
257 void test(bar &B1, const bar *B2) {
258 // test various configurations of const
260 const bar *const B4 = B2;
263 if (!isa<foo>(B1)) return;
264 if (!isa<foo>(B2)) return;
265 if (!isa<foo>(B3)) return;
266 if (!isa<foo>(B4)) return;
269 foo &F1 = cast<foo>(B1);
270 const foo *F3 = cast<foo>(B2);
271 const foo *F4 = cast<foo>(B2);
272 const foo &F8 = cast<foo>(B3);
273 const foo *F9 = cast<foo>(B4);
274 foo *F10 = cast<foo>(fub());
277 const foo *F11 = cast_or_null<foo>(B2);
278 const foo *F12 = cast_or_null<foo>(B2);
279 const foo *F13 = cast_or_null<foo>(B4);
280 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
282 // These lines are errors...
283 //foo *F20 = cast<foo>(B2); // Yields const foo*
284 //foo &F21 = cast<foo>(B3); // Yields const foo&
285 //foo *F22 = cast<foo>(B4); // Yields const foo*
286 //foo &F23 = cast_or_null<foo>(B1);
287 //const foo &F24 = cast_or_null<foo>(B3);
290 bar *fub() { return 0; }
298 } // End llvm namespace