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 // 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.
30 template<typename From> struct simplify_type {
31 typedef From SimpleType; // The real type this represents...
33 // An accessor to get the real value...
34 static SimpleType &getSimplifiedValue(From &Val) { return Val; }
37 template<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(static_cast<From&>(Val));
44 // The core of the implementation of isa<X> is here; To and From should be
45 // the names of classes. This template can be specialized to customize the
46 // implementation of isa<> without rewriting it from scratch.
47 template <typename To, typename From>
49 static inline bool doit(const From &Val) {
50 return To::classof(&Val);
54 template <typename To, typename From> struct isa_impl_cl {
55 static inline bool doit(const From &Val) {
56 return isa_impl<To, From>::doit(Val);
60 template <typename To, typename From> struct isa_impl_cl<To, const From> {
61 static inline bool doit(const From &Val) {
62 return isa_impl<To, From>::doit(Val);
66 template <typename To, typename From> struct isa_impl_cl<To, From*> {
67 static inline bool doit(const From *Val) {
68 assert(Val && "isa<> used on a null pointer");
69 return isa_impl<To, From>::doit(*Val);
73 template <typename To, typename From> struct isa_impl_cl<To, const From*> {
74 static inline bool doit(const From *Val) {
75 assert(Val && "isa<> used on a null pointer");
76 return isa_impl<To, From>::doit(*Val);
80 template <typename To, typename From> struct isa_impl_cl<To, const From*const> {
81 static inline bool doit(const From *Val) {
82 assert(Val && "isa<> used on a null pointer");
83 return isa_impl<To, From>::doit(*Val);
87 template<typename To, typename From, typename SimpleFrom>
88 struct isa_impl_wrap {
89 // When From != SimplifiedType, we can simplify the type some more by using
90 // the simplify_type template.
91 static bool doit(const From &Val) {
92 return isa_impl_wrap<To, SimpleFrom,
93 typename simplify_type<SimpleFrom>::SimpleType>::doit(
94 simplify_type<From>::getSimplifiedValue(Val));
98 template<typename To, typename FromTy>
99 struct isa_impl_wrap<To, FromTy, FromTy> {
100 // When From == SimpleType, we are as simple as we are going to get.
101 static bool doit(const FromTy &Val) {
102 return isa_impl_cl<To,FromTy>::doit(Val);
106 // isa<X> - Return true if the parameter to the template is an instance of the
107 // template type argument. Used like this:
109 // if (isa<Type>(myVal)) { ... }
111 template <class X, class Y>
112 inline bool isa(const Y &Val) {
113 return isa_impl_wrap<X, Y, typename simplify_type<Y>::SimpleType>::doit(Val);
116 //===----------------------------------------------------------------------===//
117 // cast<x> Support Templates
118 //===----------------------------------------------------------------------===//
120 template<class To, class From> struct cast_retty;
123 // Calculate what type the 'cast' function should return, based on a requested
124 // type of To and a source type of From.
125 template<class To, class From> struct cast_retty_impl {
126 typedef To& ret_type; // Normal case, return Ty&
128 template<class To, class From> struct cast_retty_impl<To, const From> {
129 typedef const To &ret_type; // Normal case, return Ty&
132 template<class To, class From> struct cast_retty_impl<To, From*> {
133 typedef To* ret_type; // Pointer arg case, return Ty*
136 template<class To, class From> struct cast_retty_impl<To, const From*> {
137 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
140 template<class To, class From> struct cast_retty_impl<To, const From*const> {
141 typedef const To* ret_type; // Constant pointer arg case, return const Ty*
145 template<class To, class From, class SimpleFrom>
146 struct cast_retty_wrap {
147 // When the simplified type and the from type are not the same, use the type
148 // simplifier to reduce the type, then reuse cast_retty_impl to get the
150 typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
153 template<class To, class FromTy>
154 struct cast_retty_wrap<To, FromTy, FromTy> {
155 // When the simplified type is equal to the from type, use it directly.
156 typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
159 template<class To, class From>
161 typedef typename cast_retty_wrap<To, From,
162 typename simplify_type<From>::SimpleType>::ret_type ret_type;
165 // Ensure the non-simple values are converted using the simplify_type template
166 // that may be specialized by smart pointers...
168 template<class To, class From, class SimpleFrom> struct cast_convert_val {
169 // This is not a simple type, use the template to simplify it...
170 static typename cast_retty<To, From>::ret_type doit(const From &Val) {
171 return cast_convert_val<To, SimpleFrom,
172 typename simplify_type<SimpleFrom>::SimpleType>::doit(
173 simplify_type<From>::getSimplifiedValue(Val));
177 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
178 // This _is_ a simple type, just cast it.
179 static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
180 typename cast_retty<To, FromTy>::ret_type Res2
181 = (typename cast_retty<To, FromTy>::ret_type)const_cast<FromTy&>(Val);
188 // cast<X> - Return the argument parameter cast to the specified type. This
189 // casting operator asserts that the type is correct, so it does not return null
190 // on failure. It does not allow a null argument (use cast_or_null for that).
191 // It is typically used like this:
193 // cast<Instruction>(myVal)->getParent()
195 template <class X, class Y>
196 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
197 assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
198 return cast_convert_val<X, Y,
199 typename simplify_type<Y>::SimpleType>::doit(Val);
202 // cast_or_null<X> - Functionally identical to cast, except that a null value is
205 template <class X, class Y>
206 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
207 if (Val == 0) return 0;
208 assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
213 // dyn_cast<X> - Return the argument parameter cast to the specified type. This
214 // casting operator returns null if the argument is of the wrong type, so it can
215 // be used to test for a type as well as cast if successful. This should be
216 // used in the context of an if statement like this:
218 // if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
221 template <class X, class Y>
222 inline typename cast_retty<X, Y>::ret_type dyn_cast(const Y &Val) {
223 return isa<X>(Val) ? cast<X, Y>(Val) : 0;
226 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
227 // value is accepted.
229 template <class X, class Y>
230 inline typename cast_retty<X, Y*>::ret_type dyn_cast_or_null(Y *Val) {
231 return (Val && isa<X>(Val)) ? cast<X>(Val) : 0;
234 } // End llvm namespace