Revert "System: Add SwapByteOrder and update Support/MathExtras.h to use it."
[oota-llvm.git] / include / llvm / Support / Casting.h
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
20 namespace llvm {
21
22 //===----------------------------------------------------------------------===//
23 //                          isa<x> Support Templates
24 //===----------------------------------------------------------------------===//
25
26 template<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 //
32 template<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
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));
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 //
52 template <typename To, typename From>
53 struct isa_impl {
54   static inline bool doit(const From &Val) {
55     return To::classof(&Val);
56   }
57 };
58
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));
66   }
67 };
68
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);
74   }
75 };
76
77 // isa_impl_cl - Use class partial specialization to transform types to a single
78 // canonical form for isa_impl.
79 //
80 template<typename FromCl>
81 struct isa_impl_cl {
82   template<class ToCl>
83   static bool isa(const FromCl &Val) {
84     return isa_impl_wrap<ToCl,const FromCl,
85                    typename simplify_type<const FromCl>::SimpleType>::doit(Val);
86   }
87 };
88
89 // Specialization used to strip const qualifiers off of the FromCl type...
90 template<typename FromCl>
91 struct isa_impl_cl<const FromCl> {
92   template<class ToCl>
93   static bool isa(const FromCl &Val) {
94     return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
95   }
96 };
97
98 // Define pointer traits in terms of base traits...
99 template<class FromCl>
100 struct isa_impl_cl<FromCl*> {
101   template<class ToCl>
102   static bool isa(FromCl *Val) {
103     return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
104   }
105 };
106
107 // Define reference traits in terms of base traits...
108 template<class FromCl>
109 struct isa_impl_cl<FromCl&> {
110   template<class ToCl>
111   static bool isa(FromCl &Val) {
112     return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
113   }
114 };
115
116 template <class X, class Y>
117 inline bool isa(const Y &Val) {
118   return isa_impl_cl<Y>::template isa<X>(Val);
119 }
120
121 //===----------------------------------------------------------------------===//
122 //                          cast<x> Support Templates
123 //===----------------------------------------------------------------------===//
124
125 template<class To, class From> struct cast_retty;
126
127
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&
132 };
133 template<class To, class From> struct cast_retty_impl<To, const From> {
134   typedef const To &ret_type;   // Normal case, return Ty&
135 };
136
137 template<class To, class From> struct cast_retty_impl<To, From*> {
138   typedef To* ret_type;         // Pointer arg case, return Ty*
139 };
140
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*
143 };
144
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*
147 };
148
149
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
154   // resultant type.
155   typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
156 };
157
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;
162 };
163
164 template<class To, class From>
165 struct cast_retty {
166   typedef typename cast_retty_wrap<To, From,
167                    typename simplify_type<From>::SimpleType>::ret_type ret_type;
168 };
169
170 // Ensure the non-simple values are converted using the simplify_type template
171 // that may be specialized by smart pointers...
172 //
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));
179   }
180 };
181
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);
187     return Res2;
188   }
189 };
190
191
192
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.
196 // Used Like this:
197 //
198 //  cast<Instruction>(myVal)->getParent()
199 //
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);
205 }
206
207 // cast_or_null<X> - Functionally identical to cast, except that a null value is
208 // accepted.
209 //
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!");
214   return cast<X>(Val);
215 }
216
217
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:
222 //
223 //  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
224 //
225
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;
229 }
230
231 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
232 // value is accepted.
233 //
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;
237 }
238
239 } // End llvm namespace
240
241 #endif