1 //===-- llvm/Support/Casting.h - Allow flexible, checked, casts -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the isa<X>(), cast<X>(), dyn_cast<X>(), cast_or_null<X>(),
11 // and dyn_cast_or_null<X>() templates.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_CASTING_H
16 #define LLVM_SUPPORT_CASTING_H
22 //===----------------------------------------------------------------------===//
23 // isa<x> Support Templates
24 //===----------------------------------------------------------------------===//
26 template<typename FromCl> struct isa_impl_cl;
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.
32 template<typename From> struct simplify_type {
33 typedef From SimpleType; // The real type this represents...
35 // An accessor to get the real value...
36 static SimpleType &getSimplifiedValue(From &Val) { return Val; }
39 template<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));
47 // isa<X> - Return true if the parameter to the template is an instance of the
48 // template type argument. Used like this:
50 // if (isa<Type*>(myVal)) { ... }
52 template <typename To, typename From>
54 static inline bool doit(const From &Val) {
55 return To::classof(&Val);
59 template<typename To, typename From, typename SimpleType>
60 struct 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));
69 template<typename To, typename FromTy>
70 struct 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);
77 // isa_impl_cl - Use class partial specialization to transform types to a single
78 // canonical form for isa_impl.
80 template<typename FromCl>
83 static bool isa(const FromCl &Val) {
84 return isa_impl_wrap<ToCl,const FromCl,
85 typename simplify_type<const FromCl>::SimpleType>::doit(Val);
89 // Specialization used to strip const qualifiers off of the FromCl type...
90 template<typename FromCl>
91 struct isa_impl_cl<const FromCl> {
93 static bool isa(const FromCl &Val) {
94 return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
98 // Define pointer traits in terms of base traits...
99 template<class FromCl>
100 struct isa_impl_cl<FromCl*> {
102 static bool isa(FromCl *Val) {
103 return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
107 // Define reference traits in terms of base traits...
108 template<class FromCl>
109 struct isa_impl_cl<FromCl&> {
111 static bool isa(FromCl &Val) {
112 return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
116 template <class X, class Y>
117 inline bool isa(const Y &Val) {
118 return isa_impl_cl<Y>::template isa<X>(Val);
121 //===----------------------------------------------------------------------===//
122 // cast<x> Support Templates
123 //===----------------------------------------------------------------------===//
125 template<class To, class From> struct cast_retty;
128 // Calculate what type the 'cast' function should return, based on a requested
129 // type of To and a source type of From.
130 template<class To, class From> struct cast_retty_impl {
131 typedef To& ret_type; // Normal case, return Ty&
133 template<class To, class From> struct cast_retty_impl<To, const From> {
134 typedef const To &ret_type; // Normal case, return Ty&
137 template<class To, class From> struct cast_retty_impl<To, From*> {
138 typedef To* ret_type; // Pointer arg case, return Ty*
141 template<class To, class From> struct cast_retty_impl<To, const From*> {
142 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
145 template<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*
150 template<class To, class From, class SimpleFrom>
151 struct 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
155 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
158 template<class To, class FromTy>
159 struct 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;
164 template<class To, class From>
166 typedef typename cast_retty_wrap<To, From,
167 typename simplify_type<From>::SimpleType>::ret_type ret_type;
170 // Ensure the non-simple values are converted using the simplify_type template
171 // that may be specialized by smart pointers...
173 template<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));
182 template<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);
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.
198 // cast<Instruction>(myVal)->getParent()
200 template <class X, class Y>
201 inline 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);
207 // cast_or_null<X> - Functionally identical to cast, except that a null value is
210 template <class X, class Y>
211 inline 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!");
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:
223 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
226 template <class X, class Y>
227 inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
228 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
231 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
232 // value is accepted.
234 template <class X, class Y>
235 inline typename cast_retty<X, Y*>::ret_type dyn_cast_or_null(Y *Val) {
236 return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
239 } // End llvm namespace