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