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