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
18 //===----------------------------------------------------------------------===//
19 // isa<x> Support Templates
20 //===----------------------------------------------------------------------===//
22 template<typename FromCl> struct isa_impl_cl;
24 // Define a template that can be specialized by smart pointers to reflect the
25 // fact that they are automatically dereferenced, and are not involved with the
26 // template selection process... the default implementation is a noop.
28 template<typename From> struct simplify_type {
29 typedef From SimpleType; // The real type this represents...
31 // An accessor to get the real value...
32 static SimpleType &getSimplifiedValue(From &Val) { return Val; }
35 template<typename From> struct simplify_type<const From> {
36 typedef const From SimpleType;
37 static SimpleType &getSimplifiedValue(const From &Val) {
38 return simplify_type<From>::getSimplifiedValue((From&)Val);
43 // isa<X> - Return true if the parameter to the template is an instance of the
44 // template type argument. Used like this:
46 // if (isa<Type*>(myVal)) { ... }
48 template <typename To, typename From>
49 inline bool isa_impl(const From &Val) {
50 return To::classof(&Val);
53 template<typename To, typename From, typename SimpleType>
54 struct isa_impl_wrap {
55 // When From != SimplifiedType, we can simplify the type some more by using
56 // the simplify_type template.
57 static bool doit(const From &Val) {
58 return isa_impl_cl<const SimpleType>::template
59 isa<To>(simplify_type<const From>::getSimplifiedValue(Val));
63 template<typename To, typename FromTy>
64 struct isa_impl_wrap<To, const FromTy, const FromTy> {
65 // When From == SimpleType, we are as simple as we are going to get.
66 static bool doit(const FromTy &Val) {
67 return isa_impl<To,FromTy>(Val);
71 // isa_impl_cl - Use class partial specialization to transform types to a single
72 // canonical form for isa_impl.
74 template<typename FromCl>
77 static bool isa(const FromCl &Val) {
78 return isa_impl_wrap<ToCl,const FromCl,
79 typename simplify_type<const FromCl>::SimpleType>::doit(Val);
83 // Specialization used to strip const qualifiers off of the FromCl type...
84 template<typename FromCl>
85 struct isa_impl_cl<const FromCl> {
87 static bool isa(const FromCl &Val) {
88 return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
92 // Define pointer traits in terms of base traits...
93 template<class FromCl>
94 struct isa_impl_cl<FromCl*> {
96 static bool isa(FromCl *Val) {
97 return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
101 // Define reference traits in terms of base traits...
102 template<class FromCl>
103 struct isa_impl_cl<FromCl&> {
105 static bool isa(FromCl &Val) {
106 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
110 template <class X, class Y>
111 inline bool isa(const Y &Val) {
112 return isa_impl_cl<Y>::template isa<X>(Val);
115 //===----------------------------------------------------------------------===//
116 // cast<x> Support Templates
117 //===----------------------------------------------------------------------===//
119 template<class To, class From> struct cast_retty;
122 // Calculate what type the 'cast' function should return, based on a requested
123 // type of To and a source type of From.
124 template<class To, class From> struct cast_retty_impl {
125 typedef To& ret_type; // Normal case, return Ty&
127 template<class To, class From> struct cast_retty_impl<To, const From> {
128 typedef const To &ret_type; // Normal case, return Ty&
131 template<class To, class From> struct cast_retty_impl<To, From*> {
132 typedef To* ret_type; // Pointer arg case, return Ty*
135 template<class To, class From> struct cast_retty_impl<To, const From*> {
136 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
139 template<class To, class From> struct cast_retty_impl<To, const From*const> {
140 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
144 template<class To, class From, class SimpleFrom>
145 struct cast_retty_wrap {
146 // When the simplified type and the from type are not the same, use the type
147 // simplifier to reduce the type, then reuse cast_retty_impl to get the
149 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
152 template<class To, class FromTy>
153 struct cast_retty_wrap<To, FromTy, FromTy> {
154 // When the simplified type is equal to the from type, use it directly.
155 typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
158 template<class To, class From>
160 typedef typename cast_retty_wrap<To, From,
161 typename simplify_type<From>::SimpleType>::ret_type ret_type;
164 // Ensure the non-simple values are converted using the simplify_type template
165 // that may be specialized by smart pointers...
167 template<class To, class From, class SimpleFrom> struct cast_convert_val {
168 // This is not a simple type, use the template to simplify it...
169 static typename cast_retty<To, From>::ret_type doit(const From &Val) {
170 return cast_convert_val<To, SimpleFrom,
171 typename simplify_type<SimpleFrom>::SimpleType>::doit(
172 simplify_type<From>::getSimplifiedValue(Val));
176 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
177 // This _is_ a simple type, just cast it.
178 static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
179 return (typename cast_retty<To, FromTy>::ret_type)Val;
185 // cast<X> - Return the argument parameter cast to the specified type. This
186 // casting operator asserts that the type is correct, so it does not return null
187 // on failure. But it will correctly return NULL when the input is NULL.
190 // cast<Instruction>(myVal)->getParent()
192 template <class X, class Y>
193 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
194 assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
195 return cast_convert_val<X, Y,
196 typename simplify_type<Y>::SimpleType>::doit(Val);
199 // cast_or_null<X> - Functionally identical to cast, except that a null value is
202 template <class X, class Y>
203 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
204 if (Val == 0) return 0;
205 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
210 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
211 // casting operator returns null if the argument is of the wrong type, so it can
212 // be used to test for a type as well as cast if successful. This should be
213 // used in the context of an if statement like this:
215 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
218 template <class X, class Y>
219 inline typename cast_retty<X, Y>::ret_type dyn_cast(Y Val) {
220 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
223 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
224 // value is accepted.
226 template <class X, class Y>
227 inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(Y Val) {
228 return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
232 #ifdef DEBUG_CAST_OPERATORS
242 /* static bool classof(const bar *X) {
243 cerr << "Classof: " << X << "\n";
248 template <> inline bool isa_impl<foo,bar>(const bar &Val) {
249 cerr << "Classof: " << &Val << "\n";
255 void test(bar &B1, const bar *B2) {
256 // test various configurations of const
258 const bar *const B4 = B2;
261 if (!isa<foo>(B1)) return;
262 if (!isa<foo>(B2)) return;
263 if (!isa<foo>(B3)) return;
264 if (!isa<foo>(B4)) return;
267 foo &F1 = cast<foo>(B1);
268 const foo *F3 = cast<foo>(B2);
269 const foo *F4 = cast<foo>(B2);
270 const foo &F8 = cast<foo>(B3);
271 const foo *F9 = cast<foo>(B4);
272 foo *F10 = cast<foo>(fub());
275 const foo *F11 = cast_or_null<foo>(B2);
276 const foo *F12 = cast_or_null<foo>(B2);
277 const foo *F13 = cast_or_null<foo>(B4);
278 const foo *F14 = cast_or_null<foo>(fub()); // Shouldn't print.
280 // These lines are errors...
281 //foo *F20 = cast<foo>(B2); // Yields const foo*
282 //foo &F21 = cast<foo>(B3); // Yields const foo&
283 //foo *F22 = cast<foo>(B4); // Yields const foo*
284 //foo &F23 = cast_or_null<foo>(B1);
285 //const foo &F24 = cast_or_null<foo>(B3);
288 bar *fub() { return 0; }