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