Put all LLVM code into the llvm namespace, as per bug 109.
[oota-llvm.git] / include / llvm / Support / Casting.h
1 //===-- Support/Casting.h - Allow flexible, checked, casts ------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 SUPPORT_CASTING_H
16 #define SUPPORT_CASTING_H
17
18 namespace llvm {
19
20 //===----------------------------------------------------------------------===//
21 //                          isa<x> Support Templates
22 //===----------------------------------------------------------------------===//
23
24 template<typename FromCl> struct isa_impl_cl;
25
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.
29 //
30 template<typename From> struct simplify_type {
31   typedef       From SimpleType;        // The real type this represents...
32
33   // An accessor to get the real value...
34   static SimpleType &getSimplifiedValue(From &Val) { return Val; }
35 };
36
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((From&)Val);
41   }
42 };
43
44
45 // isa<X> - Return true if the parameter to the template is an instance of the
46 // template type argument.  Used like this:
47 //
48 //  if (isa<Type*>(myVal)) { ... }
49 //
50 template <typename To, typename From>
51 inline bool isa_impl(const From &Val) { 
52   return To::classof(&Val);
53 }
54
55 template<typename To, typename From, typename SimpleType>
56 struct isa_impl_wrap {
57   // When From != SimplifiedType, we can simplify the type some more by using
58   // the simplify_type template.
59   static bool doit(const From &Val) {
60     return isa_impl_cl<const SimpleType>::template 
61                     isa<To>(simplify_type<const From>::getSimplifiedValue(Val));
62   }
63 };
64
65 template<typename To, typename FromTy>
66 struct isa_impl_wrap<To, const FromTy, const FromTy> {
67   // When From == SimpleType, we are as simple as we are going to get.
68   static bool doit(const FromTy &Val) {
69     return isa_impl<To,FromTy>(Val);
70   }
71 };
72
73 // isa_impl_cl - Use class partial specialization to transform types to a single
74 // canonical form for isa_impl.
75 //
76 template<typename FromCl>
77 struct isa_impl_cl {
78   template<class ToCl>
79   static bool isa(const FromCl &Val) {
80     return isa_impl_wrap<ToCl,const FromCl,
81                    typename simplify_type<const FromCl>::SimpleType>::doit(Val);
82   }
83 };
84
85 // Specialization used to strip const qualifiers off of the FromCl type...
86 template<typename FromCl>
87 struct isa_impl_cl<const FromCl> {
88   template<class ToCl>
89   static bool isa(const FromCl &Val) {
90     return isa_impl_cl<FromCl>::template isa<ToCl>(Val);
91   }
92 };
93
94 // Define pointer traits in terms of base traits...
95 template<class FromCl>
96 struct isa_impl_cl<FromCl*> {
97   template<class ToCl>
98   static bool isa(FromCl *Val) {
99     return isa_impl_cl<FromCl>::template isa<ToCl>(*Val);
100   }
101 };
102
103 // Define reference traits in terms of base traits...
104 template<class FromCl>
105 struct isa_impl_cl<FromCl&> {
106   template<class ToCl>
107   static bool isa(FromCl &Val) {
108     return isa_impl_cl<FromCl>::template isa<ToCl>(&Val);
109   }
110 };
111
112 template <class X, class Y>
113 inline bool isa(const Y &Val) {
114   return isa_impl_cl<Y>::template isa<X>(Val);
115 }
116
117 //===----------------------------------------------------------------------===//
118 //                          cast<x> Support Templates
119 //===----------------------------------------------------------------------===//
120
121 template<class To, class From> struct cast_retty;
122
123
124 // Calculate what type the 'cast' function should return, based on a requested
125 // type of To and a source type of From.
126 template<class To, class From> struct cast_retty_impl {
127   typedef To& ret_type;         // Normal case, return Ty&
128 };
129 template<class To, class From> struct cast_retty_impl<To, const From> {
130   typedef const To &ret_type;   // Normal case, return Ty&
131 };
132
133 template<class To, class From> struct cast_retty_impl<To, From*> {
134   typedef To* ret_type;         // Pointer arg case, return Ty*
135 };
136
137 template<class To, class From> struct cast_retty_impl<To, const From*> {
138   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
139 };
140
141 template<class To, class From> struct cast_retty_impl<To, const From*const> {
142   typedef const To* ret_type;   // Constant pointer arg case, return const Ty*
143 };
144
145
146 template<class To, class From, class SimpleFrom>
147 struct cast_retty_wrap {
148   // When the simplified type and the from type are not the same, use the type
149   // simplifier to reduce the type, then reuse cast_retty_impl to get the
150   // resultant type.
151   typedef typename cast_retty<To, SimpleFrom>::ret_type ret_type;
152 };
153
154 template<class To, class FromTy>
155 struct cast_retty_wrap<To, FromTy, FromTy> {
156   // When the simplified type is equal to the from type, use it directly.
157   typedef typename cast_retty_impl<To,FromTy>::ret_type ret_type;
158 };
159
160 template<class To, class From>
161 struct cast_retty {
162   typedef typename cast_retty_wrap<To, From, 
163                    typename simplify_type<From>::SimpleType>::ret_type ret_type;
164 };
165
166 // Ensure the non-simple values are converted using the simplify_type template
167 // that may be specialized by smart pointers...
168 //
169 template<class To, class From, class SimpleFrom> struct cast_convert_val {
170   // This is not a simple type, use the template to simplify it...
171   static typename cast_retty<To, From>::ret_type doit(const From &Val) {
172     return cast_convert_val<To, SimpleFrom,
173       typename simplify_type<SimpleFrom>::SimpleType>::doit(
174                           simplify_type<From>::getSimplifiedValue(Val));
175   }
176 };
177
178 template<class To, class FromTy> struct cast_convert_val<To,FromTy,FromTy> {
179   // This _is_ a simple type, just cast it.
180   static typename cast_retty<To, FromTy>::ret_type doit(const FromTy &Val) {
181     return (typename cast_retty<To, FromTy>::ret_type)Val;
182   }
183 };
184
185
186
187 // cast<X> - Return the argument parameter cast to the specified type.  This
188 // casting operator asserts that the type is correct, so it does not return null
189 // on failure.  But it will correctly return NULL when the input is NULL.
190 // Used Like this:
191 //
192 //  cast<Instruction>(myVal)->getParent()
193 //
194 template <class X, class Y>
195 inline typename cast_retty<X, Y>::ret_type cast(const Y &Val) {
196   assert(isa<X>(Val) && "cast<Ty>() argument of incompatible type!");
197   return cast_convert_val<X, Y,
198                           typename simplify_type<Y>::SimpleType>::doit(Val);
199 }
200
201 // cast_or_null<X> - Functionally identical to cast, except that a null value is
202 // accepted.
203 //
204 template <class X, class Y>
205 inline typename cast_retty<X, Y*>::ret_type cast_or_null(Y *Val) {
206   if (Val == 0) return 0;
207   assert(isa<X>(Val) && "cast_or_null<Ty>() argument of incompatible type!");
208   return cast<X>(Val);
209 }
210
211
212 // dyn_cast<X> - Return the argument parameter cast to the specified type.  This
213 // casting operator returns null if the argument is of the wrong type, so it can
214 // be used to test for a type as well as cast if successful.  This should be
215 // used in the context of an if statement like this:
216 //
217 //  if (const Instruction *I = dyn_cast<Instruction>(myVal)) { ... }
218 //
219
220 template <class X, class Y>
221 inline typename cast_retty<X, Y>::ret_type dyn_cast(Y Val) {
222   return isa<X>(Val) ? cast<X, Y>(Val) : 0;
223 }
224
225 // dyn_cast_or_null<X> - Functionally identical to dyn_cast, except that a null
226 // value is accepted.
227 //
228 template <class X, class Y>
229 inline typename cast_retty<X, Y>::ret_type dyn_cast_or_null(Y Val) {
230   return (Val && isa<X>(Val)) ? cast<X, Y>(Val) : 0;
231 }
232
233
234 #ifdef DEBUG_CAST_OPERATORS
235 #include <iostream>
236
237 struct bar {
238   bar() {}
239 private:
240   bar(const bar &);
241 };
242 struct foo {
243   void ext() const;
244   /*  static bool classof(const bar *X) {
245     cerr << "Classof: " << X << "\n";
246     return true;
247     }*/
248 };
249
250 template <> inline bool isa_impl<foo,bar>(const bar &Val) { 
251   cerr << "Classof: " << &Val << "\n";
252   return true;
253 }
254
255
256 bar *fub();
257 void test(bar &B1, const bar *B2) {
258   // test various configurations of const
259   const bar &B3 = B1;
260   const bar *const B4 = B2;
261
262   // test isa
263   if (!isa<foo>(B1)) return;
264   if (!isa<foo>(B2)) return;
265   if (!isa<foo>(B3)) return;
266   if (!isa<foo>(B4)) return;
267
268   // test cast
269   foo &F1 = cast<foo>(B1);
270   const foo *F3 = cast<foo>(B2);
271   const foo *F4 = cast<foo>(B2);
272   const foo &F8 = cast<foo>(B3);
273   const foo *F9 = cast<foo>(B4);
274   foo *F10 = cast<foo>(fub());
275
276   // test cast_or_null
277   const foo *F11 = cast_or_null<foo>(B2);
278   const foo *F12 = cast_or_null<foo>(B2);
279   const foo *F13 = cast_or_null<foo>(B4);
280   const foo *F14 = cast_or_null<foo>(fub());  // Shouldn't print.
281   
282   // These lines are errors...
283   //foo *F20 = cast<foo>(B2);  // Yields const foo*
284   //foo &F21 = cast<foo>(B3);  // Yields const foo&
285   //foo *F22 = cast<foo>(B4);  // Yields const foo*
286   //foo &F23 = cast_or_null<foo>(B1);
287   //const foo &F24 = cast_or_null<foo>(B3);
288 }
289
290 bar *fub() { return 0; }
291 void main() {
292   bar B;
293   test(B, &B);
294 }
295
296 #endif
297
298 } // End llvm namespace
299
300 #endif