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