Casting.h revision 103289e9383ad1eb66caf28c9b166aebce963a35
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    return reinterpret_cast<typename cast_retty<To, FromTy>::ret_type>(
184                         const_cast<FromTy&>(Val));
185  }
186};
187
188
189
190// cast<X> - Return the argument parameter cast to the specified type.  This
191// casting operator asserts that the type is correct, so it does not return null
192// on failure.  But it will correctly return NULL when the input is NULL.
193// Used Like this:
194//
195//  cast<Instruction>(myVal)->getParent()
196//
197template <class X, class Y>
198inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
199  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
200  return cast_convert_val<X, Y,
201                          typename simplify_type<Y>::SimpleType>::doit(Val);
202}
203
204// cast_or_null<X> - Functionally identical to cast, except that a null value is
205// accepted.
206//
207template <class X, class Y>
208inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
209  if (Val == 0) return 0;
210  assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
211  return cast<X>(Val);
212}
213
214
215// dyn_cast<X> - Return the argument parameter cast to the specified type.  This
216// casting operator returns null if the argument is of the wrong type, so it can
217// be used to test for a type as well as cast if successful.  This should be
218// used in the context of an if statement like this:
219//
220//  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
221//
222
223template <class X, class Y>
224inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
225  return isa<X>(Val) ? cast<X, Y>(Val) : 0;
226}
227
228// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
229// value is accepted.
230//
231template <class X, class Y>
232inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(const Y &Val) {
233  return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
234}
235
236
237#ifdef DEBUG_CAST_OPERATORS
238#include "llvm/Support/raw_ostream.h"
239
240struct bar {
241  bar() {}
242private:
243  bar(const bar &);
244};
245struct foo {
246  void ext() const;
247  /*  static bool classof(const bar *X) {
248    cerr << "Classof: " << X << "\n";
249    return true;
250    }*/
251};
252
253template <> inline bool isa_impl<foo,bar>(const bar &Val) {
254  errs() << "Classof: " << &Val << "\n";
255  return true;
256}
257
258
259bar *fub();
260void test(bar &B1, const bar *B2) {
261  // test various configurations of const
262  const bar &B3 = B1;
263  const bar *const B4 = B2;
264
265  // test isa
266  if (!isa<foo>(B1)) return;
267  if (!isa<foo>(B2)) return;
268  if (!isa<foo>(B3)) return;
269  if (!isa<foo>(B4)) return;
270
271  // test cast
272  foo &F1 = cast<foo>(B1);
273  const foo *F3 = cast<foo>(B2);
274  const foo *F4 = cast<foo>(B2);
275  const foo &F8 = cast<foo>(B3);
276  const foo *F9 = cast<foo>(B4);
277  foo *F10 = cast<foo>(fub());
278
279  // test cast_or_null
280  const foo *F11 = cast_or_null<foo>(B2);
281  const foo *F12 = cast_or_null<foo>(B2);
282  const foo *F13 = cast_or_null<foo>(B4);
283  const foo *F14 = cast_or_null<foo>(fub());  // Shouldn't print.
284
285  // These lines are errors...
286  //foo *F20 = cast<foo>(B2);  // Yields const foo*
287  //foo &F21 = cast<foo>(B3);  // Yields const foo&
288  //foo *F22 = cast<foo>(B4);  // Yields const foo*
289  //foo &F23 = cast_or_null<foo>(B1);
290  //const foo &F24 = cast_or_null<foo>(B3);
291}
292
293bar *fub() { return 0; }
294void main() {
295  bar B;
296  test(B, &B);
297}
298
299#endif
300
301} // End llvm namespace
302
303#endif
304