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