]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/include/llvm/Support/TypeBuilder.h
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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 #include <vector>
22
23 namespace llvm {
24
25 /// TypeBuilder - This provides a uniform API for looking up types
26 /// known at compile time.  To support cross-compilation, we define a
27 /// series of tag types in the llvm::types namespace, like i<N>,
28 /// ieee_float, ppc_fp128, etc.  TypeBuilder<T, false> allows T to be
29 /// any of these, a native C type (whose size may depend on the host
30 /// compiler), or a pointer, function, or struct type built out of
31 /// these.  TypeBuilder<T, true> removes native C types from this set
32 /// to guarantee that its result is suitable for cross-compilation.
33 /// We define the primitive types, pointer types, and functions up to
34 /// 5 arguments here, but to use this class with your own types,
35 /// you'll need to specialize it.  For example, say you want to call a
36 /// function defined externally as:
37 ///
38 ///   struct MyType {
39 ///     int32 a;
40 ///     int32 *b;
41 ///     void *array[1];  // Intended as a flexible array.
42 ///   };
43 ///   int8 AFunction(struct MyType *value);
44 ///
45 /// You'll want to use
46 ///   Function::Create(TypeBuilder<types::i<8>(MyType*), true>::get(), ...)
47 /// to declare the function, but when you first try this, your compiler will
48 /// complain that TypeBuilder<MyType, true>::get() doesn't exist. To fix this,
49 /// write:
50 ///
51 ///   namespace llvm {
52 ///   template<bool xcompile> class TypeBuilder<MyType, xcompile> {
53 ///   public:
54 ///     static StructType *get(LLVMContext &Context) {
55 ///       // If you cache this result, be sure to cache it separately
56 ///       // for each LLVMContext.
57 ///       return StructType::get(
58 ///         TypeBuilder<types::i<32>, xcompile>::get(Context),
59 ///         TypeBuilder<types::i<32>*, xcompile>::get(Context),
60 ///         TypeBuilder<types::i<8>*[], xcompile>::get(Context),
61 ///         NULL);
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 /// TypeBuilder cannot handle recursive types or types you only know at runtime.
76 /// If you try to give it a recursive type, it will deadlock, infinitely
77 /// recurse, or do something similarly undesirable.
78 template<typename T, bool cross_compilable> class TypeBuilder {};
79
80 // Types for use with cross-compilable TypeBuilders.  These correspond
81 // exactly with an LLVM-native type.
82 namespace types {
83 /// i<N> corresponds to the LLVM IntegerType with N bits.
84 template<uint32_t num_bits> class i {};
85
86 // The following classes represent the LLVM floating types.
87 class ieee_float {};
88 class ieee_double {};
89 class x86_fp80 {};
90 class fp128 {};
91 class ppc_fp128 {};
92 // X86 MMX.
93 class x86_mmx {};
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 PointerType *get(LLVMContext &Context) {
108     return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
109   }
110 };
111
112 /// There is no support for references
113 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
114
115 // Arrays
116 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
117 public:
118   static ArrayType *get(LLVMContext &Context) {
119     return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
120   }
121 };
122 /// LLVM uses an array of length 0 to represent an unknown-length array.
123 template<typename T, bool cross> class TypeBuilder<T[], cross> {
124 public:
125   static ArrayType *get(LLVMContext &Context) {
126     return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
127   }
128 };
129
130 // Define the C integral types only for TypeBuilder<T, false>.
131 //
132 // C integral types do not have a defined size. It would be nice to use the
133 // stdint.h-defined typedefs that do have defined sizes, but we'd run into the
134 // following problem:
135 //
136 // On an ILP32 machine, stdint.h might define:
137 //
138 //   typedef int int32_t;
139 //   typedef long long int64_t;
140 //   typedef long size_t;
141 //
142 // If we defined TypeBuilder<int32_t> and TypeBuilder<int64_t>, then any use of
143 // TypeBuilder<size_t> would fail.  We couldn't define TypeBuilder<size_t> in
144 // addition to the defined-size types because we'd get duplicate definitions on
145 // platforms where stdint.h instead defines:
146 //
147 //   typedef int int32_t;
148 //   typedef long long int64_t;
149 //   typedef int size_t;
150 //
151 // So we define all the primitive C types and nothing else.
152 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
153 template<> class TypeBuilder<T, false> { \
154 public: \
155   static IntegerType *get(LLVMContext &Context) { \
156     return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
157   } \
158 }; \
159 template<> class TypeBuilder<T, true> { \
160   /* We provide a definition here so users don't accidentally */ \
161   /* define these types to work. */ \
162 }
163 DEFINE_INTEGRAL_TYPEBUILDER(char);
164 DEFINE_INTEGRAL_TYPEBUILDER(signed char);
165 DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
166 DEFINE_INTEGRAL_TYPEBUILDER(short);
167 DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
168 DEFINE_INTEGRAL_TYPEBUILDER(int);
169 DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
170 DEFINE_INTEGRAL_TYPEBUILDER(long);
171 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
172 #ifdef _MSC_VER
173 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
174 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
175 #else /* _MSC_VER */
176 DEFINE_INTEGRAL_TYPEBUILDER(long long);
177 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
178 #endif /* _MSC_VER */
179 #undef DEFINE_INTEGRAL_TYPEBUILDER
180
181 template<uint32_t num_bits, bool cross>
182 class TypeBuilder<types::i<num_bits>, cross> {
183 public:
184   static IntegerType *get(LLVMContext &C) {
185     return IntegerType::get(C, num_bits);
186   }
187 };
188
189 template<> class TypeBuilder<float, false> {
190 public:
191   static Type *get(LLVMContext& C) {
192     return Type::getFloatTy(C);
193   }
194 };
195 template<> class TypeBuilder<float, true> {};
196
197 template<> class TypeBuilder<double, false> {
198 public:
199   static Type *get(LLVMContext& C) {
200     return Type::getDoubleTy(C);
201   }
202 };
203 template<> class TypeBuilder<double, true> {};
204
205 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
206 public:
207   static Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
208 };
209 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
210 public:
211   static Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
212 };
213 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
214 public:
215   static Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
216 };
217 template<bool cross> class TypeBuilder<types::fp128, cross> {
218 public:
219   static Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
220 };
221 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
222 public:
223   static Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
224 };
225 template<bool cross> class TypeBuilder<types::x86_mmx, cross> {
226 public:
227   static Type *get(LLVMContext& C) { return Type::getX86_MMXTy(C); }
228 };
229
230 template<bool cross> class TypeBuilder<void, cross> {
231 public:
232   static Type *get(LLVMContext &C) {
233     return Type::getVoidTy(C);
234   }
235 };
236
237 /// void* is disallowed in LLVM types, but it occurs often enough in C code that
238 /// we special case it.
239 template<> class TypeBuilder<void*, false>
240   : public TypeBuilder<types::i<8>*, false> {};
241 template<> class TypeBuilder<const void*, false>
242   : public TypeBuilder<types::i<8>*, false> {};
243 template<> class TypeBuilder<volatile void*, false>
244   : public TypeBuilder<types::i<8>*, false> {};
245 template<> class TypeBuilder<const volatile void*, false>
246   : public TypeBuilder<types::i<8>*, false> {};
247
248 template<typename R, bool cross> class TypeBuilder<R(), cross> {
249 public:
250   static FunctionType *get(LLVMContext &Context) {
251     return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
252   }
253 };
254 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
255 public:
256   static FunctionType *get(LLVMContext &Context) {
257     std::vector<Type*> params;
258     params.reserve(1);
259     params.push_back(TypeBuilder<A1, cross>::get(Context));
260     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
261                              params, false);
262   }
263 };
264 template<typename R, typename A1, typename A2, bool cross>
265 class TypeBuilder<R(A1, A2), cross> {
266 public:
267   static FunctionType *get(LLVMContext &Context) {
268     std::vector<Type*> params;
269     params.reserve(2);
270     params.push_back(TypeBuilder<A1, cross>::get(Context));
271     params.push_back(TypeBuilder<A2, 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, typename A3, bool cross>
277 class TypeBuilder<R(A1, A2, A3), cross> {
278 public:
279   static FunctionType *get(LLVMContext &Context) {
280     std::vector<Type*> params;
281     params.reserve(3);
282     params.push_back(TypeBuilder<A1, cross>::get(Context));
283     params.push_back(TypeBuilder<A2, cross>::get(Context));
284     params.push_back(TypeBuilder<A3, cross>::get(Context));
285     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
286                              params, false);
287   }
288 };
289
290 template<typename R, typename A1, typename A2, typename A3, typename A4,
291          bool cross>
292 class TypeBuilder<R(A1, A2, A3, A4), cross> {
293 public:
294   static FunctionType *get(LLVMContext &Context) {
295     std::vector<Type*> params;
296     params.reserve(4);
297     params.push_back(TypeBuilder<A1, cross>::get(Context));
298     params.push_back(TypeBuilder<A2, cross>::get(Context));
299     params.push_back(TypeBuilder<A3, cross>::get(Context));
300     params.push_back(TypeBuilder<A4, cross>::get(Context));
301     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
302                              params, false);
303   }
304 };
305
306 template<typename R, typename A1, typename A2, typename A3, typename A4,
307          typename A5, bool cross>
308 class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
309 public:
310   static FunctionType *get(LLVMContext &Context) {
311     std::vector<Type*> params;
312     params.reserve(5);
313     params.push_back(TypeBuilder<A1, cross>::get(Context));
314     params.push_back(TypeBuilder<A2, cross>::get(Context));
315     params.push_back(TypeBuilder<A3, cross>::get(Context));
316     params.push_back(TypeBuilder<A4, cross>::get(Context));
317     params.push_back(TypeBuilder<A5, cross>::get(Context));
318     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
319                              params, false);
320   }
321 };
322
323 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
324 public:
325   static FunctionType *get(LLVMContext &Context) {
326     return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
327   }
328 };
329 template<typename R, typename A1, bool cross>
330 class TypeBuilder<R(A1, ...), cross> {
331 public:
332   static FunctionType *get(LLVMContext &Context) {
333     std::vector<Type*> params;
334     params.reserve(1);
335     params.push_back(TypeBuilder<A1, cross>::get(Context));
336     return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
337   }
338 };
339 template<typename R, typename A1, typename A2, bool cross>
340 class TypeBuilder<R(A1, A2, ...), cross> {
341 public:
342   static FunctionType *get(LLVMContext &Context) {
343     std::vector<Type*> params;
344     params.reserve(2);
345     params.push_back(TypeBuilder<A1, cross>::get(Context));
346     params.push_back(TypeBuilder<A2, cross>::get(Context));
347     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
348                                    params, true);
349   }
350 };
351 template<typename R, typename A1, typename A2, typename A3, bool cross>
352 class TypeBuilder<R(A1, A2, A3, ...), cross> {
353 public:
354   static FunctionType *get(LLVMContext &Context) {
355     std::vector<Type*> params;
356     params.reserve(3);
357     params.push_back(TypeBuilder<A1, cross>::get(Context));
358     params.push_back(TypeBuilder<A2, cross>::get(Context));
359     params.push_back(TypeBuilder<A3, cross>::get(Context));
360     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
361                                    params, true);
362   }
363 };
364
365 template<typename R, typename A1, typename A2, typename A3, typename A4,
366          bool cross>
367 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
368 public:
369   static FunctionType *get(LLVMContext &Context) {
370     std::vector<Type*> params;
371     params.reserve(4);
372     params.push_back(TypeBuilder<A1, cross>::get(Context));
373     params.push_back(TypeBuilder<A2, cross>::get(Context));
374     params.push_back(TypeBuilder<A3, cross>::get(Context));
375     params.push_back(TypeBuilder<A4, cross>::get(Context));
376     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
377                              params, true);
378   }
379 };
380
381 template<typename R, typename A1, typename A2, typename A3, typename A4,
382          typename A5, bool cross>
383 class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
384 public:
385   static FunctionType *get(LLVMContext &Context) {
386     std::vector<Type*> params;
387     params.reserve(5);
388     params.push_back(TypeBuilder<A1, cross>::get(Context));
389     params.push_back(TypeBuilder<A2, cross>::get(Context));
390     params.push_back(TypeBuilder<A3, cross>::get(Context));
391     params.push_back(TypeBuilder<A4, cross>::get(Context));
392     params.push_back(TypeBuilder<A5, cross>::get(Context));
393     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
394                                    params, true);
395   }
396 };
397
398 }  // namespace llvm
399
400 #endif