Casting.h revision 1ff1da7ac9bff43ba24445ff30ba2fd79bd95cd7
1//===-- Support/Casting.h - Allow flexible, checked, casts -------*- C++ -*--=//
2//
3// This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
4// and dyn_cast_or_null<X>() templates.
5//
6//===----------------------------------------------------------------------===//
7
8#ifndef SUPPORT_CASTING_H
9#define SUPPORT_CASTING_H
10
11#include <assert.h>
12
13//===----------------------------------------------------------------------===//
14//                          isa<x> Support Templates
15//===----------------------------------------------------------------------===//
16
17template<typename FromCl> struct isa_impl_cl;
18
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.
22//
23template<typename From> struct simplify_type {
24  typedef       From SimpleType;        // The real type this represents...
25
26  // An accessor to get the real value...
27  static SimpleType &getSimplifiedValue(From &Val) { return Val; }
28};
29
30template<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);
34  }
35};
36
37
38// isa<X> - Return true if the parameter to the template is an instance of the
39// template type argument.  Used like this:
40//
41//  if (isa<Type*>(myVal)) { ... }
42//
43template <typename To, typename From>
44inline bool isa_impl(const From &Val) {
45  return To::classof(&Val);
46}
47
48template<typename To, typename From, typename SimpleType>
49struct 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));
55  }
56};
57
58template<typename To, typename FromTy>
59struct 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);
63  }
64};
65
66// isa_impl_cl - Use class partial specialization to transform types to a single
67// cannonical form for isa_impl.
68//
69template<typename FromCl>
70struct isa_impl_cl {
71  template<class ToCl>
72  static bool isa(const FromCl &Val) {
73    return isa_impl_wrap<ToCl,const FromCl,
74                   typename simplify_type<const FromCl>::SimpleType>::doit(Val);
75  }
76};
77
78// Specialization used to strip const qualifiers off of the FromCl type...
79template<typename FromCl>
80struct isa_impl_cl<const FromCl> {
81  template<class ToCl>
82  static bool isa(const FromCl &Val) {
83    return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
84  }
85};
86
87// Define pointer traits in terms of base traits...
88template<class FromCl>
89struct isa_impl_cl<FromCl*> {
90  template<class ToCl>
91  static bool isa(FromCl *Val) {
92    return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
93  }
94};
95
96// Define reference traits in terms of base traits...
97template<class FromCl>
98struct isa_impl_cl<FromCl&> {
99  template<class ToCl>
100  static bool isa(FromCl &Val) {
101    return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
102  }
103};
104
105template <class X, class Y>
106inline bool isa(const Y &Val) {
107  return isa_impl_cl<Y>::template isa<X>(Val);
108}
109
110//===----------------------------------------------------------------------===//
111//                          cast<x> Support Templates
112//===----------------------------------------------------------------------===//
113
114template<class To, class From> struct cast_retty;
115
116
117// Calculate what type the 'cast' function should return, based on a requested
118// type of To and a source type of From.
119template<class To, class From> struct cast_retty_impl {
120  typedef To& ret_type;         // Normal case, return Ty&
121};
122template<class To, class From> struct cast_retty_impl<To, const From> {
123  typedef const To &ret_type;   // Normal case, return Ty&
124};
125
126template<class To, class From> struct cast_retty_impl<To, From*> {
127  typedef To* ret_type;         // Pointer arg case, return Ty*
128};
129
130template<class To, class From> struct cast_retty_impl<To, const From*> {
131  typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
132};
133
134template<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*
136};
137
138
139template<class To, class From, class SimpleFrom>
140struct 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
143  // resultant type.
144  typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
145};
146
147template<class To, class FromTy>
148struct 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;
151};
152
153template<class To, class From>
154struct cast_retty {
155  typedef typename cast_retty_wrap<To, From,
156                   typename simplify_type<From>::SimpleType>::ret_type ret_type;
157};
158
159// Ensure the non-simple values are converted using the simplify_type template
160// that may be specialized by smart pointers...
161//
162template<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));
168  }
169};
170
171template<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;
175  }
176};
177
178
179
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.
183// Used Like this:
184//
185//  cast<Instruction>(myVal)->getParent()
186//
187template <class X, class Y>
188inline 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);
192}
193
194// cast_or_null<X> - Functionally identical to cast, except that a null value is
195// accepted.
196//
197template <class X, class Y>
198inline 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!");
201  return cast<X>(Val);
202}
203
204
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:
209//
210//  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
211//
212
213template <class X, class Y>
214inline typename cast_retty<X, Y>::ret_type dyn_cast(Y Val) {
215  return isa<X>(Val) ? cast<X, Y>(Val) : 0;
216}
217
218// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
219// value is accepted.
220//
221template <class X, class Y>
222inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(Y Val) {
223  return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
224}
225
226
227#ifdef DEBUG_CAST_OPERATORS
228#include <iostream>
229
230struct bar {
231  bar() {}
232private:
233  bar(const bar &);
234};
235struct foo {
236  void ext() const;
237  /*  static bool classof(const bar *X) {
238    cerr << "Classof: " << X << "\n";
239    return true;
240    }*/
241};
242
243template <> inline bool isa_impl<foo,bar>(const bar &Val) {
244  cerr << "Classof: " << &Val << "\n";
245  return true;
246}
247
248
249bar *fub();
250void test(bar &B1, const bar *B2) {
251  // test various configurations of const
252  const bar &B3 = B1;
253  const bar *const B4 = B2;
254
255  // test isa
256  if (!isa<foo>(B1)) return;
257  if (!isa<foo>(B2)) return;
258  if (!isa<foo>(B3)) return;
259  if (!isa<foo>(B4)) return;
260
261  // test cast
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());
268
269  // test cast_or_null
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.
274
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);
281}
282
283bar *fub() { return 0; }
284void main() {
285  bar B;
286  test(B, &B);
287}
288
289#endif
290
291#endif
292