Push LLVMContexts through the IntegerType APIs.
[oota-llvm.git] / include / llvm / Support / TypeBuilder.h
1 //===---- llvm/Support/TypeBuilder.h - Builder for LLVM types ---*- 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 TypeBuilder class, which is used as a convenient way to
11 // create LLVM types with a consistent and simplified interface.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_TYPEBUILDER_H
16 #define LLVM_SUPPORT_TYPEBUILDER_H
17
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/LLVMContext.h"
20 #include <limits.h>
21
22 namespace llvm {
23
24 /// TypeBuilder - This provides a uniform API for looking up types
25 /// known at compile time.  To support cross-compilation, we define a
26 /// series of tag types in the llvm::types namespace, like i<N>,
27 /// ieee_float, ppc_fp128, etc.  TypeBuilder<T, false> allows T to be
28 /// any of these, a native C type (whose size may depend on the host
29 /// compiler), or a pointer, function, or struct type built out of
30 /// these.  TypeBuilder<T, true> removes native C types from this set
31 /// to guarantee that its result is suitable for cross-compilation.
32 /// We define the primitive types, pointer types, and functions up to
33 /// 5 arguments here, but to use this class with your own types,
34 /// you'll need to specialize it.  For example, say you want to call a
35 /// function defined externally as:
36 ///
37 ///   struct MyType {
38 ///     int32 a;
39 ///     int32 *b;
40 ///     void *array[1];  // Intended as a flexible array.
41 ///   };
42 ///   int8 AFunction(struct MyType *value);
43 ///
44 /// You'll want to use
45 ///   Function::Create(TypeBuilder<types::i<8>(MyType*), true>::get(), ...)
46 /// to declare the function, but when you first try this, your compiler will
47 /// complain that TypeBuilder<MyType, true>::get() doesn't exist. To fix this,
48 /// write:
49 ///
50 ///   namespace llvm {
51 ///   template<bool xcompile> class TypeBuilder<MyType, xcompile> {
52 ///   public:
53 ///     static const StructType *get() {
54 ///       // Using the static result variable ensures that the type is
55 ///       // only looked up once.
56 ///       static const StructType *const result = StructType::get(
57 ///         TypeBuilder<types::i<32>, xcompile>::get(),
58 ///         TypeBuilder<types::i<32>*, xcompile>::get(),
59 ///         TypeBuilder<types::i<8>*[], xcompile>::get(),
60 ///         NULL);
61 ///       return result;
62 ///     }
63 ///
64 ///     // You may find this a convenient place to put some constants
65 ///     // to help with getelementptr.  They don't have any effect on
66 ///     // the operation of TypeBuilder.
67 ///     enum Fields {
68 ///       FIELD_A,
69 ///       FIELD_B,
70 ///       FIELD_ARRAY
71 ///     };
72 ///   }
73 ///   }  // namespace llvm
74 ///
75 /// Using the static result variable ensures that the type is only looked up
76 /// once.
77 ///
78 /// TypeBuilder cannot handle recursive types or types you only know at runtime.
79 /// If you try to give it a recursive type, it will deadlock, infinitely
80 /// recurse, or throw a recursive_init exception.
81 template<typename T, bool cross_compilable> class TypeBuilder {};
82
83 // Types for use with cross-compilable TypeBuilders.  These correspond
84 // exactly with an LLVM-native type.
85 namespace types {
86 /// i<N> corresponds to the LLVM IntegerType with N bits.
87 template<uint32_t num_bits> class i {};
88
89 // The following classes represent the LLVM floating types.
90 class ieee_float {};
91 class ieee_double {};
92 class x86_fp80 {};
93 class fp128 {};
94 class ppc_fp128 {};
95 }  // namespace types
96
97 // LLVM doesn't have const or volatile types.
98 template<typename T, bool cross> class TypeBuilder<const T, cross>
99   : public TypeBuilder<T, cross> {};
100 template<typename T, bool cross> class TypeBuilder<volatile T, cross>
101   : public TypeBuilder<T, cross> {};
102 template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
103   : public TypeBuilder<T, cross> {};
104
105 // Pointers
106 template<typename T, bool cross> class TypeBuilder<T*, cross> {
107 public:
108   static const PointerType *get(LLVMContext &Context) {
109     static const PointerType *const result =
110       PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
111     return result;
112   }
113 };
114
115 /// There is no support for references
116 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
117
118 // Arrays
119 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
120 public:
121   static const ArrayType *get(LLVMContext &Context) {
122     static const ArrayType *const result =
123     ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
124     return result;
125   }
126 };
127 /// LLVM uses an array of length 0 to represent an unknown-length array.
128 template<typename T, bool cross> class TypeBuilder<T[], cross> {
129 public:
130   static const ArrayType *get(LLVMContext &Context) {
131     static const ArrayType *const result =
132       ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
133     return result;
134   }
135 };
136
137 // Define the C integral types only for TypeBuilder<T, false>.
138 //
139 // C integral types do not have a defined size. It would be nice to use the
140 // stdint.h-defined typedefs that do have defined sizes, but we'd run into the
141 // following problem:
142 //
143 // On an ILP32 machine, stdint.h might define:
144 //
145 //   typedef int int32_t;
146 //   typedef long long int64_t;
147 //   typedef long size_t;
148 //
149 // If we defined TypeBuilder<int32_t> and TypeBuilder<int64_t>, then any use of
150 // TypeBuilder<size_t> would fail.  We couldn't define TypeBuilder<size_t> in
151 // addition to the defined-size types because we'd get duplicate definitions on
152 // platforms where stdint.h instead defines:
153 //
154 //   typedef int int32_t;
155 //   typedef long long int64_t;
156 //   typedef int size_t;
157 //
158 // So we define all the primitive C types and nothing else.
159 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
160 template<> class TypeBuilder<T, false> { \
161 public: \
162   static const IntegerType *get(LLVMContext &Context) { \
163     static const IntegerType *const result = \
164       IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
165     return result; \
166   } \
167 }; \
168 template<> class TypeBuilder<T, true> { \
169   /* We provide a definition here so users don't accidentally */ \
170   /* define these types to work. */ \
171 }
172 DEFINE_INTEGRAL_TYPEBUILDER(char);
173 DEFINE_INTEGRAL_TYPEBUILDER(signed char);
174 DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
175 DEFINE_INTEGRAL_TYPEBUILDER(short);
176 DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
177 DEFINE_INTEGRAL_TYPEBUILDER(int);
178 DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
179 DEFINE_INTEGRAL_TYPEBUILDER(long);
180 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
181 #ifdef _MSC_VER
182 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
183 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
184 #else /* _MSC_VER */
185 DEFINE_INTEGRAL_TYPEBUILDER(long long);
186 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
187 #endif /* _MSC_VER */
188 #undef DEFINE_INTEGRAL_TYPEBUILDER
189
190 template<uint32_t num_bits, bool cross>
191 class TypeBuilder<types::i<num_bits>, cross> {
192 public:
193   static const IntegerType *get(LLVMContext &C) {
194     static const IntegerType *const result = IntegerType::get(C, num_bits);
195     return result;
196   }
197 };
198
199 template<> class TypeBuilder<float, false> {
200 public:
201   static const Type *get(LLVMContext& C) {
202     return Type::getFloatTy(C);
203   }
204 };
205 template<> class TypeBuilder<float, true> {};
206
207 template<> class TypeBuilder<double, false> {
208 public:
209   static const Type *get(LLVMContext& C) {
210     return Type::getDoubleTy(C);
211   }
212 };
213 template<> class TypeBuilder<double, true> {};
214
215 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
216 public:
217   static const Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
218 };
219 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
220 public:
221   static const Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
222 };
223 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
224 public:
225   static const Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
226 };
227 template<bool cross> class TypeBuilder<types::fp128, cross> {
228 public:
229   static const Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
230 };
231 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
232 public:
233   static const Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
234 };
235
236 template<bool cross> class TypeBuilder<void, cross> {
237 public:
238   static const Type *get(LLVMContext &C) {
239     return Type::getVoidTy(C);
240   }
241 };
242
243 /// void* is disallowed in LLVM types, but it occurs often enough in C code that
244 /// we special case it.
245 template<> class TypeBuilder<void*, false>
246   : public TypeBuilder<types::i<8>*, false> {};
247
248 template<typename R, bool cross> class TypeBuilder<R(), cross> {
249 public:
250   static const FunctionType *get(LLVMContext &Context) {
251     static const FunctionType *const result = create(Context);
252     return result;
253   }
254
255 private:
256   static const FunctionType *create(LLVMContext &Context) {
257     return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
258   }
259 };
260 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
261 public:
262   static const FunctionType *get(LLVMContext &Context) {
263     static const FunctionType *const result = create(Context);
264     return result;
265   }
266
267 private:
268   static const FunctionType *create(LLVMContext &Context) {
269     std::vector<const Type*> params;
270     params.reserve(1);
271     params.push_back(TypeBuilder<A1, cross>::get(Context));
272     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
273                              params, false);
274   }
275 };
276 template<typename R, typename A1, typename A2, bool cross>
277 class TypeBuilder<R(A1, A2), cross> {
278 public:
279   static const FunctionType *get(LLVMContext &Context) {
280     static const FunctionType *const result = create(Context);
281     return result;
282   }
283
284 private:
285   static const FunctionType *create(LLVMContext &Context) {
286     std::vector<const Type*> params;
287     params.reserve(2);
288     params.push_back(TypeBuilder<A1, cross>::get(Context));
289     params.push_back(TypeBuilder<A2, cross>::get(Context));
290     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
291                              params, false);
292   }
293 };
294 template<typename R, typename A1, typename A2, typename A3, bool cross>
295 class TypeBuilder<R(A1, A2, A3), cross> {
296 public:
297   static const FunctionType *get(LLVMContext &Context) {
298     static const FunctionType *const result = create(Context);
299     return result;
300   }
301
302 private:
303   static const FunctionType *create(LLVMContext &Context) {
304     std::vector<const Type*> params;
305     params.reserve(3);
306     params.push_back(TypeBuilder<A1, cross>::get(Context));
307     params.push_back(TypeBuilder<A2, cross>::get(Context));
308     params.push_back(TypeBuilder<A3, cross>::get(Context));
309     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
310                              params, false);
311   }
312 };
313
314 template<typename R, typename A1, typename A2, typename A3, typename A4,
315          bool cross>
316 class TypeBuilder<R(A1, A2, A3, A4), cross> {
317 public:
318   static const FunctionType *get(LLVMContext &Context) {
319     static const FunctionType *const result = create(Context);
320     return result;
321   }
322
323 private:
324   static const FunctionType *create(LLVMContext &Context) {
325     std::vector<const Type*> params;
326     params.reserve(4);
327     params.push_back(TypeBuilder<A1, cross>::get(Context));
328     params.push_back(TypeBuilder<A2, cross>::get(Context));
329     params.push_back(TypeBuilder<A3, cross>::get(Context));
330     params.push_back(TypeBuilder<A4, cross>::get(Context));
331     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
332                              params, false);
333   }
334 };
335
336 template<typename R, typename A1, typename A2, typename A3, typename A4,
337          typename A5, bool cross>
338 class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
339 public:
340   static const FunctionType *get(LLVMContext &Context) {
341     static const FunctionType *const result = create(Context);
342     return result;
343   }
344
345 private:
346   static const FunctionType *create(LLVMContext &Context) {
347     std::vector<const Type*> params;
348     params.reserve(5);
349     params.push_back(TypeBuilder<A1, cross>::get(Context));
350     params.push_back(TypeBuilder<A2, cross>::get(Context));
351     params.push_back(TypeBuilder<A3, cross>::get(Context));
352     params.push_back(TypeBuilder<A4, cross>::get(Context));
353     params.push_back(TypeBuilder<A5, cross>::get(Context));
354     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
355                              params, false);
356   }
357 };
358
359 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
360 public:
361   static const FunctionType *get(LLVMContext &Context) {
362     static const FunctionType *const result = create(Context);
363     return result;
364   }
365
366 private:
367   static const FunctionType *create(LLVMContext &Context) {
368     return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
369   }
370 };
371 template<typename R, typename A1, bool cross>
372 class TypeBuilder<R(A1, ...), cross> {
373 public:
374   static const FunctionType *get(LLVMContext &Context) {
375     static const FunctionType *const result = create(Context);
376     return result;
377   }
378
379 private:
380   static const FunctionType *create(LLVMContext &Context) {
381     std::vector<const Type*> params;
382     params.reserve(1);
383     params.push_back(TypeBuilder<A1, cross>::get(Context));
384     return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
385   }
386 };
387 template<typename R, typename A1, typename A2, bool cross>
388 class TypeBuilder<R(A1, A2, ...), cross> {
389 public:
390   static const FunctionType *get(LLVMContext &Context) {
391     static const FunctionType *const result = create(Context);
392     return result;
393   }
394
395 private:
396   static const FunctionType *create(LLVMContext &Context) {
397     std::vector<const Type*> params;
398     params.reserve(2);
399     params.push_back(TypeBuilder<A1, cross>::get(Context));
400     params.push_back(TypeBuilder<A2, cross>::get(Context));
401     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
402                                    params, true);
403   }
404 };
405 template<typename R, typename A1, typename A2, typename A3, bool cross>
406 class TypeBuilder<R(A1, A2, A3, ...), cross> {
407 public:
408   static const FunctionType *get(LLVMContext &Context) {
409     static const FunctionType *const result = create(Context);
410     return result;
411   }
412
413 private:
414   static const FunctionType *create(LLVMContext &Context) {
415     std::vector<const Type*> params;
416     params.reserve(3);
417     params.push_back(TypeBuilder<A1, cross>::get(Context));
418     params.push_back(TypeBuilder<A2, cross>::get(Context));
419     params.push_back(TypeBuilder<A3, cross>::get(Context));
420     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
421                                    params, true);
422   }
423 };
424
425 template<typename R, typename A1, typename A2, typename A3, typename A4,
426          bool cross>
427 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
428 public:
429   static const FunctionType *get(LLVMContext &Context) {
430     static const FunctionType *const result = create(Context);
431     return result;
432   }
433
434 private:
435   static const FunctionType *create(LLVMContext &Context) {
436     std::vector<const Type*> params;
437     params.reserve(4);
438     params.push_back(TypeBuilder<A1, cross>::get(Context));
439     params.push_back(TypeBuilder<A2, cross>::get(Context));
440     params.push_back(TypeBuilder<A3, cross>::get(Context));
441     params.push_back(TypeBuilder<A4, cross>::get(Context));
442     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
443                              params, true);
444   }
445 };
446
447 template<typename R, typename A1, typename A2, typename A3, typename A4,
448          typename A5, bool cross>
449 class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
450 public:
451   static const FunctionType *get(LLVMContext &Context) {
452     static const FunctionType *const result = create(Context);
453     return result;
454   }
455
456 private:
457   static const FunctionType *create(LLVMContext &Context) {
458     std::vector<const Type*> params;
459     params.reserve(5);
460     params.push_back(TypeBuilder<A1, cross>::get(Context));
461     params.push_back(TypeBuilder<A2, cross>::get(Context));
462     params.push_back(TypeBuilder<A3, cross>::get(Context));
463     params.push_back(TypeBuilder<A4, cross>::get(Context));
464     params.push_back(TypeBuilder<A5, cross>::get(Context));
465     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
466                                    params, true);
467   }
468 };
469
470 }  // namespace llvm
471
472 #endif