1 //===---- llvm/Support/TypeBuilder.h - Builder for LLVM types ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_TYPEBUILDER_H
16 #define LLVM_SUPPORT_TYPEBUILDER_H
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/LLVMContext.h"
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:
40 /// void *array[1]; // Intended as a flexible array.
42 /// int8 AFunction(struct MyType *value);
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,
51 /// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
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(),
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.
73 /// } // namespace llvm
75 /// Using the static result variable ensures that the type is only looked up
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 {};
83 // Types for use with cross-compilable TypeBuilders. These correspond
84 // exactly with an LLVM-native type.
86 /// i<N> corresponds to the LLVM IntegerType with N bits.
87 template<uint32_t num_bits> class i {};
89 // The following classes represent the LLVM floating types.
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> {};
106 template<typename T, bool cross> class TypeBuilder<T*, cross> {
108 static const PointerType *get(LLVMContext &Context) {
109 static const PointerType *const result =
110 PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
115 /// There is no support for references
116 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
119 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
121 static const ArrayType *get(LLVMContext &Context) {
122 static const ArrayType *const result =
123 ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
127 /// LLVM uses an array of length 0 to represent an unknown-length array.
128 template<typename T, bool cross> class TypeBuilder<T[], cross> {
130 static const ArrayType *get(LLVMContext &Context) {
131 static const ArrayType *const result =
132 ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
137 // Define the C integral types only for TypeBuilder<T, false>.
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:
143 // On an ILP32 machine, stdint.h might define:
145 // typedef int int32_t;
146 // typedef long long int64_t;
147 // typedef long size_t;
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:
154 // typedef int int32_t;
155 // typedef long long int64_t;
156 // typedef int size_t;
158 // So we define all the primitive C types and nothing else.
159 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
160 template<> class TypeBuilder<T, false> { \
162 static const IntegerType *get(LLVMContext &Context) { \
163 static const IntegerType *const result = \
164 IntegerType::get(sizeof(T) * CHAR_BIT); \
168 template<> class TypeBuilder<T, true> { \
169 /* We provide a definition here so users don't accidentally */ \
170 /* define these types to work. */ \
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);
182 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
183 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
185 DEFINE_INTEGRAL_TYPEBUILDER(long long);
186 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
187 #endif /* _MSC_VER */
188 #undef DEFINE_INTEGRAL_TYPEBUILDER
190 template<uint32_t num_bits, bool cross>
191 class TypeBuilder<types::i<num_bits>, cross> {
193 static const IntegerType *get(LLVMContext &Context) {
194 static const IntegerType *const result = IntegerType::get(num_bits);
199 template<> class TypeBuilder<float, false> {
201 static const Type *get(LLVMContext&) {
202 return Type::FloatTy;
205 template<> class TypeBuilder<float, true> {};
207 template<> class TypeBuilder<double, false> {
209 static const Type *get(LLVMContext&) {
210 return Type::DoubleTy;
213 template<> class TypeBuilder<double, true> {};
215 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
217 static const Type *get(LLVMContext&) { return Type::FloatTy; }
219 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
221 static const Type *get(LLVMContext&) { return Type::DoubleTy; }
223 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
225 static const Type *get(LLVMContext&) { return Type::X86_FP80Ty; }
227 template<bool cross> class TypeBuilder<types::fp128, cross> {
229 static const Type *get(LLVMContext&) { return Type::FP128Ty; }
231 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
233 static const Type *get(LLVMContext&) { return Type::PPC_FP128Ty; }
236 template<bool cross> class TypeBuilder<void, cross> {
238 static const Type *get(LLVMContext&) {
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> {};
248 template<typename R, bool cross> class TypeBuilder<R(), cross> {
250 static const FunctionType *get(LLVMContext &Context) {
251 static const FunctionType *const result = create(Context);
256 static const FunctionType *create(LLVMContext &Context) {
257 return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
260 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
262 static const FunctionType *get(LLVMContext &Context) {
263 static const FunctionType *const result = create(Context);
268 static const FunctionType *create(LLVMContext &Context) {
269 std::vector<const Type*> params;
271 params.push_back(TypeBuilder<A1, cross>::get(Context));
272 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
276 template<typename R, typename A1, typename A2, bool cross>
277 class TypeBuilder<R(A1, A2), cross> {
279 static const FunctionType *get(LLVMContext &Context) {
280 static const FunctionType *const result = create(Context);
285 static const FunctionType *create(LLVMContext &Context) {
286 std::vector<const Type*> params;
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),
294 template<typename R, typename A1, typename A2, typename A3, bool cross>
295 class TypeBuilder<R(A1, A2, A3), cross> {
297 static const FunctionType *get(LLVMContext &Context) {
298 static const FunctionType *const result = create(Context);
303 static const FunctionType *create(LLVMContext &Context) {
304 std::vector<const Type*> params;
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),
314 template<typename R, typename A1, typename A2, typename A3, typename A4,
316 class TypeBuilder<R(A1, A2, A3, A4), cross> {
318 static const FunctionType *get(LLVMContext &Context) {
319 static const FunctionType *const result = create(Context);
324 static const FunctionType *create(LLVMContext &Context) {
325 std::vector<const Type*> params;
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),
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> {
340 static const FunctionType *get(LLVMContext &Context) {
341 static const FunctionType *const result = create(Context);
346 static const FunctionType *create(LLVMContext &Context) {
347 std::vector<const Type*> params;
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),
359 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
361 static const FunctionType *get(LLVMContext &Context) {
362 static const FunctionType *const result = create(Context);
367 static const FunctionType *create(LLVMContext &Context) {
368 return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
371 template<typename R, typename A1, bool cross>
372 class TypeBuilder<R(A1, ...), cross> {
374 static const FunctionType *get(LLVMContext &Context) {
375 static const FunctionType *const result = create(Context);
380 static const FunctionType *create(LLVMContext &Context) {
381 std::vector<const Type*> params;
383 params.push_back(TypeBuilder<A1, cross>::get(Context));
384 return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
387 template<typename R, typename A1, typename A2, bool cross>
388 class TypeBuilder<R(A1, A2, ...), cross> {
390 static const FunctionType *get(LLVMContext &Context) {
391 static const FunctionType *const result = create(Context);
396 static const FunctionType *create(LLVMContext &Context) {
397 std::vector<const Type*> params;
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),
405 template<typename R, typename A1, typename A2, typename A3, bool cross>
406 class TypeBuilder<R(A1, A2, A3, ...), cross> {
408 static const FunctionType *get(LLVMContext &Context) {
409 static const FunctionType *const result = create(Context);
414 static const FunctionType *create(LLVMContext &Context) {
415 std::vector<const Type*> params;
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),
425 template<typename R, typename A1, typename A2, typename A3, typename A4,
427 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
429 static const FunctionType *get(LLVMContext &Context) {
430 static const FunctionType *const result = create(Context);
435 static const FunctionType *create(LLVMContext &Context) {
436 std::vector<const Type*> params;
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),
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> {
451 static const FunctionType *get(LLVMContext &Context) {
452 static const FunctionType *const result = create(Context);
457 static const FunctionType *create(LLVMContext &Context) {
458 std::vector<const Type*> params;
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),