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 "llvm/Support/type_traits.h"
19#include <cassert>
20
21namespace llvm {
22
23//===----------------------------------------------------------------------===//
24//                          isa<x> Support Templates
25//===----------------------------------------------------------------------===//
26
27// Define a template that can be specialized by smart pointers to reflect the
28// fact that they are automatically dereferenced, and are not involved with the
29// template selection process...  the default implementation is a noop.
30//
31template<typename From> struct simplify_type {
32  typedef       From SimpleType;        // The real type this represents...
33
34  // An accessor to get the real value...
35  static SimpleType &getSimplifiedValue(From &Val) { return Val; }
36};
37
38template<typename From> struct simplify_type<const From> {
39  typedef const From SimpleType;
40  static SimpleType &getSimplifiedValue(const From &Val) {
41    return simplify_type<From>::getSimplifiedValue(static_cast<From&>(Val));
42  }
43};
44
45// The core of the implementation of isa<X> is here; To and From should be
46// the names of classes.  This template can be specialized to customize the
47// implementation of isa<> without rewriting it from scratch.
48template <typename To, typename From, typename Enabler = void>
49struct isa_impl {
50  static inline bool doit(const From &Val) {
51    return To::classof(&Val);
52  }
53};
54
55/// \brief Always allow upcasts, and perform no dynamic check for them.
56template <typename To, typename From>
57struct isa_impl<To, From,
58                typename enable_if<
59                  llvm::is_base_of<To, From>
60                >::type
61               > {
62  static inline bool doit(const From &) { return true; }
63};
64
65template <typename To, typename From> struct isa_impl_cl {
66  static inline bool doit(const From &Val) {
67    return isa_impl<To, From>::doit(Val);
68  }
69};
70
71template <typename To, typename From> struct isa_impl_cl<To, const From> {
72  static inline bool doit(const From &Val) {
73    return isa_impl<To, From>::doit(Val);
74  }
75};
76
77template <typename To, typename From> struct isa_impl_cl<To, From*> {
78  static inline bool doit(const From *Val) {
79    assert(Val && "isa<> used on a null pointer");
80    return isa_impl<To, From>::doit(*Val);
81  }
82};
83
84template <typename To, typename From> struct isa_impl_cl<To, const From*> {
85  static inline bool doit(const From *Val) {
86    assert(Val && "isa<> used on a null pointer");
87    return isa_impl<To, From>::doit(*Val);
88  }
89};
90
91template <typename To, typename From> struct isa_impl_cl<To, const From*const> {
92  static inline bool doit(const From *Val) {
93    assert(Val && "isa<> used on a null pointer");
94    return isa_impl<To, From>::doit(*Val);
95  }
96};
97
98template<typename To, typename From, typename SimpleFrom>
99struct isa_impl_wrap {
100  // When From != SimplifiedType, we can simplify the type some more by using
101  // the simplify_type template.
102  static bool doit(const From &Val) {
103    return isa_impl_wrap<To, SimpleFrom,
104      typename simplify_type<SimpleFrom>::SimpleType>::doit(
105                          simplify_type<From>::getSimplifiedValue(Val));
106  }
107};
108
109template<typename To, typename FromTy>
110struct isa_impl_wrap<To, FromTy, FromTy> {
111  // When From == SimpleType, we are as simple as we are going to get.
112  static bool doit(const FromTy &Val) {
113    return isa_impl_cl<To,FromTy>::doit(Val);
114  }
115};
116
117// isa<X> - Return true if the parameter to the template is an instance of the
118// template type argument.  Used like this:
119//
120//  if (isa<Type>(myVal)) { ... }
121//
122template <class X, class Y>
123inline bool isa(const Y &Val) {
124  return isa_impl_wrap<X, Y, typename simplify_type<Y>::SimpleType>::doit(Val);
125}
126
127//===----------------------------------------------------------------------===//
128//                          cast<x> Support Templates
129//===----------------------------------------------------------------------===//
130
131template<class To, class From> struct cast_retty;
132
133
134// Calculate what type the 'cast' function should return, based on a requested
135// type of To and a source type of From.
136template<class To, class From> struct cast_retty_impl {
137  typedef To& ret_type;         // Normal case, return Ty&
138};
139template<class To, class From> struct cast_retty_impl<To, const From> {
140  typedef const To &ret_type;   // Normal case, return Ty&
141};
142
143template<class To, class From> struct cast_retty_impl<To, From*> {
144  typedef To* ret_type;         // Pointer arg case, return Ty*
145};
146
147template<class To, class From> struct cast_retty_impl<To, const From*> {
148  typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
149};
150
151template<class To, class From> struct cast_retty_impl<To, const From*const> {
152  typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
153};
154
155
156template<class To, class From, class SimpleFrom>
157struct cast_retty_wrap {
158  // When the simplified type and the from type are not the same, use the type
159  // simplifier to reduce the type, then reuse cast_retty_impl to get the
160  // resultant type.
161  typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
162};
163
164template<class To, class FromTy>
165struct cast_retty_wrap<To, FromTy, FromTy> {
166  // When the simplified type is equal to the from type, use it directly.
167  typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
168};
169
170template<class To, class From>
171struct cast_retty {
172  typedef typename cast_retty_wrap<To, From,
173                   typename simplify_type<From>::SimpleType>::ret_type ret_type;
174};
175
176// Ensure the non-simple values are converted using the simplify_type template
177// that may be specialized by smart pointers...
178//
179template<class To, class From, class SimpleFrom> struct cast_convert_val {
180  // This is not a simple type, use the template to simplify it...
181  static typename cast_retty<To, From>::ret_type doit(const From &Val) {
182    return cast_convert_val<To, SimpleFrom,
183      typename simplify_type<SimpleFrom>::SimpleType>::doit(
184                          simplify_type<From>::getSimplifiedValue(Val));
185  }
186};
187
188template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
189  // This _is_ a simple type, just cast it.
190  static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
191    typename cast_retty<To, FromTy>::ret_type Res2
192     = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
193    return Res2;
194  }
195};
196
197
198
199// cast<X> - Return the argument parameter cast to the specified type.  This
200// casting operator asserts that the type is correct, so it does not return null
201// on failure.  It does not allow a null argument (use cast_or_null for that).
202// It is typically used like this:
203//
204//  cast<Instruction>(myVal)->getParent()
205//
206template <class X, class Y>
207inline typename enable_if_c<
208  !is_same<Y, typename simplify_type<Y>::SimpleType>::value,
209  typename cast_retty<X, Y>::ret_type
210>::type cast(const Y &Val) {
211  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
212  return cast_convert_val<X, Y,
213                          typename simplify_type<Y>::SimpleType>::doit(Val);
214}
215
216template <class X, class Y>
217inline typename enable_if<
218  is_same<Y, typename simplify_type<Y>::SimpleType>,
219  typename cast_retty<X, Y>::ret_type
220>::type cast(Y &Val) {
221  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
222  return cast_convert_val<X, Y,
223                          typename simplify_type<Y>::SimpleType>::doit(Val);
224}
225
226template <class X, class Y>
227inline typename enable_if<
228  is_same<Y, typename simplify_type<Y>::SimpleType>,
229  typename cast_retty<X, Y*>::ret_type
230>::type cast(Y *Val) {
231  assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
232  return cast_convert_val<X, Y*,
233                          typename simplify_type<Y*>::SimpleType>::doit(Val);
234}
235
236// cast_or_null<X> - Functionally identical to cast, except that a null value is
237// accepted.
238//
239template <class X, class Y>
240inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
241  if (Val == 0) return 0;
242  assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
243  return cast<X>(Val);
244}
245
246
247// dyn_cast<X> - Return the argument parameter cast to the specified type.  This
248// casting operator returns null if the argument is of the wrong type, so it can
249// be used to test for a type as well as cast if successful.  This should be
250// used in the context of an if statement like this:
251//
252//  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
253//
254
255template <class X, class Y>
256inline typename enable_if_c<
257  !is_same<Y, typename simplify_type<Y>::SimpleType>::value,
258  typename cast_retty<X, Y>::ret_type
259>::type dyn_cast(const Y &Val) {
260  return isa<X>(Val) ? cast<X>(Val) : 0;
261}
262
263template <class X, class Y>
264inline typename enable_if<
265  is_same<Y, typename simplify_type<Y>::SimpleType>,
266  typename cast_retty<X, Y>::ret_type
267>::type dyn_cast(Y &Val) {
268  return isa<X>(Val) ? cast<X>(Val) : 0;
269}
270
271template <class X, class Y>
272inline typename enable_if<
273  is_same<Y, typename simplify_type<Y>::SimpleType>,
274  typename cast_retty<X, Y*>::ret_type
275>::type dyn_cast(Y *Val) {
276  return isa<X>(Val) ? cast<X>(Val) : 0;
277}
278
279// dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
280// value is accepted.
281//
282template <class X, class Y>
283inline typename cast_retty<X, Y*>::ret_type dyn_cast_or_null(Y *Val) {
284  return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
285}
286
287} // End llvm namespace
288
289#endif
290