Casting.h revision 0179e977234fef45b1877eb93a3c7565cdd1862d
1//===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// 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 LLVM_SUPPORT_CASTING_H
16#define LLVM_SUPPORT_CASTING_H
17
18#include <cassert>
19
20namespace llvm {
21
22//===----------------------------------------------------------------------===//
23//                          isa<x> Support Templates
24//===----------------------------------------------------------------------===//
25
26template<typename FromCl> struct isa_impl_cl;
27
28// Define a template that can be specialized by smart pointers to reflect the
29// fact that they are automatically dereferenced, and are not involved with the
30// template selection process...  the default implementation is a noop.
31//
32template<typename From> struct simplify_type {
33  typedef       From SimpleType;        // The real type this represents...
34
35  // An accessor to get the real value...
36  static SimpleType &getSimplifiedValue(From &Val) { return Val; }
37};
38
39template<typename From> struct simplify_type<const From> {
40  typedef const From SimpleType;
41  static SimpleType &getSimplifiedValue(const From &Val) {
42    return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val));
43  }
44};
45
46
47// isa<X> - Return true if the parameter to the template is an instance of the
48// template type argument.  Used like this:
49//
50//  if (isa<Type*>(myVal)) { ... }
51//
52template <typename To, typename From>
53struct isa_impl {
54  static inline bool doit(const From &Val) {
55    return To::classof(&Val);
56  }
57};
58
59template<typename To, typename From, typename SimpleType>
60struct isa_impl_wrap {
61  // When From != SimplifiedType, we can simplify the type some more by using
62  // the simplify_type template.
63  static bool doit(const From &Val) {
64    return isa_impl_cl<const SimpleType>::template
65                    isa<To>(simplify_type<const From>::getSimplifiedValue(Val));
66  }
67};
68
69template<typename To, typename FromTy>
70struct isa_impl_wrap<To, const FromTy, const FromTy> {
71  // When From == SimpleType, we are as simple as we are going to get.
72  static bool doit(const FromTy &Val) {
73    return isa_impl<To,FromTy>::doit(Val);
74  }
75};
76
77// isa_impl_cl - Use class partial specialization to transform types to a single
78// canonical form for isa_impl.
79//
80template<typename FromCl>
81struct isa_impl_cl {
82  template<class ToCl>
83  static bool isa(const FromCl &Val) {
84    return isa_impl_wrap<ToCl,const FromCl,
85                   typename simplify_type<const FromCl>::SimpleType>::doit(Val);
86  }
87};
88
89// Specialization used to strip const qualifiers off of the FromCl type...
90template<typename FromCl>
91struct isa_impl_cl<const FromCl> {
92  template<class ToCl>
93  static bool isa(const FromCl &Val) {
94    return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
95  }
96};
97
98// Define pointer traits in terms of base traits...
99template<class FromCl>
100struct isa_impl_cl<FromCl*> {
101  template<class ToCl>
102  static bool isa(FromCl *Val) {
103    return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
104  }
105};
106
107// Define reference traits in terms of base traits...
108template<class FromCl>
109struct isa_impl_cl<FromCl&> {
110  template<class ToCl>
111  static bool isa(FromCl &Val) {
112    return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
113  }
114};
115
116template <class X, class Y>
117inline bool isa(const Y &Val) {
118  return isa_impl_cl<Y>::template isa<X>(Val);
119}
120
121//===----------------------------------------------------------------------===//
122//                          cast<x> Support Templates
123//===----------------------------------------------------------------------===//
124
125template<class To, class From> struct cast_retty;
126
127
128// Calculate what type the 'cast' function should return, based on a requested
129// type of To and a source type of From.
130template<class To, class From> struct cast_retty_impl {
131  typedef To& ret_type;         // Normal case, return Ty&
132};
133template<class To, class From> struct cast_retty_impl<To, const From> {
134  typedef const To &ret_type;   // Normal case, return Ty&
135};
136
137template<class To, class From> struct cast_retty_impl<To, From*> {
138  typedef To* ret_type;         // Pointer arg case, return Ty*
139};
140
141template<class To, class From> struct cast_retty_impl<To, const From*> {
142  typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
143};
144
145template<class To, class From> struct cast_retty_impl<To, const From*const> {
146  typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
147};
148
149
150template<class To, class From, class SimpleFrom>
151struct cast_retty_wrap {
152  // When the simplified type and the from type are not the same, use the type
153  // simplifier to reduce the type, then reuse cast_retty_impl to get the
154  // resultant type.
155  typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
156};
157
158template<class To, class FromTy>
159struct cast_retty_wrap<To, FromTy, FromTy> {
160  // When the simplified type is equal to the from type, use it directly.
161  typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
162};
163
164template<class To, class From>
165struct cast_retty {
166  typedef typename cast_retty_wrap<To, From,
167                   typename simplify_type<From>::SimpleType>::ret_type ret_type;
168};
169
170// Ensure the non-simple values are converted using the simplify_type template
171// that may be specialized by smart pointers...
172//
173template<class To, class From, class SimpleFrom> struct cast_convert_val {
174  // This is not a simple type, use the template to simplify it...
175  static typename cast_retty<To, From>::ret_type doit(const From &Val) {
176    return cast_convert_val<To, SimpleFrom,
177      typename simplify_type<SimpleFrom>::SimpleType>::doit(
178                          simplify_type<From>::getSimplifiedValue(Val));
179  }
180};
181
182template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
183  // This _is_ a simple type, just cast it.
184  static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
185    typename cast_retty<To, FromTy>::ret_type Res2
186     = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
187    return Res2;
188  }
189};
190
191
192
193// cast<X> - Return the argument parameter cast to the specified type.  This
194// casting operator asserts that the type is correct, so it does not return null
195// on failure.  But it will correctly return NULL when the input is NULL.
196// Used Like this:
197//
198//  cast<Instruction>(myVal)->getParent()
199//
200template <class X, class Y>
201inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
202  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
203  return cast_convert_val<X, Y,
204                          typename simplify_type<Y>::SimpleType>::doit(Val);
205}
206
207// cast_or_null<X> - Functionally identical to cast, except that a null value is
208// accepted.
209//
210template <class X, class Y>
211inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
212  if (Val == 0) return 0;
213  assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
214  return cast<X>(Val);
215}
216
217
218// dyn_cast<X> - Return the argument parameter cast to the specified type.  This
219// casting operator returns null if the argument is of the wrong type, so it can
220// be used to test for a type as well as cast if successful.  This should be
221// used in the context of an if statement like this:
222//
223//  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
224//
225
226template <class X, class Y>
227inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
228  return isa<X>(Val) ? cast<X, Y>(Val) : 0;
229}
230
231// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
232// value is accepted.
233//
234template <class X, class Y>
235inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(const Y &Val) {
236  return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
237}
238
239
240#ifdef DEBUG_CAST_OPERATORS
241#include "llvm/Support/raw_ostream.h"
242
243struct bar {
244  bar() {}
245private:
246  bar(const bar &);
247};
248struct foo {
249  void ext() const;
250  /*  static bool classof(const bar *X) {
251    cerr << "Classof: " << X << "\n";
252    return true;
253    }*/
254};
255
256template <> struct isa_impl<foo,bar> {
257  static inline bool doit(const bar &Val) {
258    dbgs() << "Classof: " << &Val << "\n";
259    return true;
260  }
261};
262
263
264bar *fub();
265void test(bar &B1, const bar *B2) {
266  // test various configurations of const
267  const bar &B3 = B1;
268  const bar *const B4 = B2;
269
270  // test isa
271  if (!isa<foo>(B1)) return;
272  if (!isa<foo>(B2)) return;
273  if (!isa<foo>(B3)) return;
274  if (!isa<foo>(B4)) return;
275
276  // test cast
277  foo &F1 = cast<foo>(B1);
278  const foo *F3 = cast<foo>(B2);
279  const foo *F4 = cast<foo>(B2);
280  const foo &F8 = cast<foo>(B3);
281  const foo *F9 = cast<foo>(B4);
282  foo *F10 = cast<foo>(fub());
283
284  // test cast_or_null
285  const foo *F11 = cast_or_null<foo>(B2);
286  const foo *F12 = cast_or_null<foo>(B2);
287  const foo *F13 = cast_or_null<foo>(B4);
288  const foo *F14 = cast_or_null<foo>(fub());  // Shouldn't print.
289
290  // These lines are errors...
291  //foo *F20 = cast<foo>(B2);  // Yields const foo*
292  //foo &F21 = cast<foo>(B3);  // Yields const foo&
293  //foo *F22 = cast<foo>(B4);  // Yields const foo*
294  //foo &F23 = cast_or_null<foo>(B1);
295  //const foo &F24 = cast_or_null<foo>(B3);
296}
297
298bar *fub() { return 0; }
299void main() {
300  bar B;
301  test(B, &B);
302}
303
304#endif
305
306} // End llvm namespace
307
308#endif
309