improve the APIs for creating struct and function types with no arguments/elements
[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     return FunctionType::get(TypeBuilder<R, cross>::get(), false);
257   }
258 };
259 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
260 public:
261   static const FunctionType *get() {
262     static const FunctionType *const result = create();
263     return result;
264   }
265
266 private:
267   static const FunctionType *create() {
268     std::vector<const Type*> params;
269     params.reserve(1);
270     params.push_back(TypeBuilder<A1, cross>::get());
271     return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
272   }
273 };
274 template<typename R, typename A1, typename A2, bool cross>
275 class TypeBuilder<R(A1, A2), cross> {
276 public:
277   static const FunctionType *get() {
278     static const FunctionType *const result = create();
279     return result;
280   }
281
282 private:
283   static const FunctionType *create() {
284     std::vector<const Type*> params;
285     params.reserve(2);
286     params.push_back(TypeBuilder<A1, cross>::get());
287     params.push_back(TypeBuilder<A2, cross>::get());
288     return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
289   }
290 };
291 template<typename R, typename A1, typename A2, typename A3, bool cross>
292 class TypeBuilder<R(A1, A2, A3), cross> {
293 public:
294   static const FunctionType *get() {
295     static const FunctionType *const result = create();
296     return result;
297   }
298
299 private:
300   static const FunctionType *create() {
301     std::vector<const Type*> params;
302     params.reserve(3);
303     params.push_back(TypeBuilder<A1, cross>::get());
304     params.push_back(TypeBuilder<A2, cross>::get());
305     params.push_back(TypeBuilder<A3, cross>::get());
306     return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
307   }
308 };
309
310 template<typename R, typename A1, typename A2, typename A3, typename A4,
311          bool cross>
312 class TypeBuilder<R(A1, A2, A3, A4), cross> {
313 public:
314   static const FunctionType *get() {
315     static const FunctionType *const result = create();
316     return result;
317   }
318
319 private:
320   static const FunctionType *create() {
321     std::vector<const Type*> params;
322     params.reserve(4);
323     params.push_back(TypeBuilder<A1, cross>::get());
324     params.push_back(TypeBuilder<A2, cross>::get());
325     params.push_back(TypeBuilder<A3, cross>::get());
326     params.push_back(TypeBuilder<A4, cross>::get());
327     return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
328   }
329 };
330
331 template<typename R, typename A1, typename A2, typename A3, typename A4,
332          typename A5, bool cross>
333 class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
334 public:
335   static const FunctionType *get() {
336     static const FunctionType *const result = create();
337     return result;
338   }
339
340 private:
341   static const FunctionType *create() {
342     std::vector<const Type*> params;
343     params.reserve(5);
344     params.push_back(TypeBuilder<A1, cross>::get());
345     params.push_back(TypeBuilder<A2, cross>::get());
346     params.push_back(TypeBuilder<A3, cross>::get());
347     params.push_back(TypeBuilder<A4, cross>::get());
348     params.push_back(TypeBuilder<A5, cross>::get());
349     return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
350   }
351 };
352
353 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
354 public:
355   static const FunctionType *get() {
356     static const FunctionType *const result = create();
357     return result;
358   }
359
360 private:
361   static const FunctionType *create() {
362     return FunctionType::get(TypeBuilder<R, cross>::get(), true);
363   }
364 };
365 template<typename R, typename A1, bool cross>
366 class TypeBuilder<R(A1, ...), cross> {
367 public:
368   static const FunctionType *get() {
369     static const FunctionType *const result = create();
370     return result;
371   }
372
373 private:
374   static const FunctionType *create() {
375     std::vector<const Type*> params;
376     params.reserve(1);
377     params.push_back(TypeBuilder<A1, cross>::get());
378     return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
379   }
380 };
381 template<typename R, typename A1, typename A2, bool cross>
382 class TypeBuilder<R(A1, A2, ...), cross> {
383 public:
384   static const FunctionType *get() {
385     static const FunctionType *const result = create();
386     return result;
387   }
388
389 private:
390   static const FunctionType *create() {
391     std::vector<const Type*> params;
392     params.reserve(2);
393     params.push_back(TypeBuilder<A1, cross>::get());
394     params.push_back(TypeBuilder<A2, cross>::get());
395     return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
396   }
397 };
398 template<typename R, typename A1, typename A2, typename A3, bool cross>
399 class TypeBuilder<R(A1, A2, A3, ...), cross> {
400 public:
401   static const FunctionType *get() {
402     static const FunctionType *const result = create();
403     return result;
404   }
405
406 private:
407   static const FunctionType *create() {
408     std::vector<const Type*> params;
409     params.reserve(3);
410     params.push_back(TypeBuilder<A1, cross>::get());
411     params.push_back(TypeBuilder<A2, cross>::get());
412     params.push_back(TypeBuilder<A3, cross>::get());
413     return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
414   }
415 };
416
417 template<typename R, typename A1, typename A2, typename A3, typename A4,
418          bool cross>
419 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
420 public:
421   static const FunctionType *get() {
422     static const FunctionType *const result = create();
423     return result;
424   }
425
426 private:
427   static const FunctionType *create() {
428     std::vector<const Type*> params;
429     params.reserve(4);
430     params.push_back(TypeBuilder<A1, cross>::get());
431     params.push_back(TypeBuilder<A2, cross>::get());
432     params.push_back(TypeBuilder<A3, cross>::get());
433     params.push_back(TypeBuilder<A4, cross>::get());
434     return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
435   }
436 };
437
438 template<typename R, typename A1, typename A2, typename A3, typename A4,
439          typename A5, bool cross>
440 class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
441 public:
442   static const FunctionType *get() {
443     static const FunctionType *const result = create();
444     return result;
445   }
446
447 private:
448   static const FunctionType *create() {
449     std::vector<const Type*> params;
450     params.reserve(5);
451     params.push_back(TypeBuilder<A1, cross>::get());
452     params.push_back(TypeBuilder<A2, cross>::get());
453     params.push_back(TypeBuilder<A3, cross>::get());
454     params.push_back(TypeBuilder<A4, cross>::get());
455     params.push_back(TypeBuilder<A5, cross>::get());
456     return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
457   }
458 };
459
460 }  // namespace llvm
461
462 #endif