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"
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:
39 /// void *array[1]; // Intended as a flexible array.
41 /// int8 AFunction(struct MyType *value);
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,
50 /// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
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(),
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.
72 /// } // namespace llvm
74 /// Using the static result variable ensures that the type is only looked up
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 {};
82 // Types for use with cross-compilable TypeBuilders. These correspond
83 // exactly with an LLVM-native type.
85 /// i<N> corresponds to the LLVM IntegerType with N bits.
86 template<uint32_t num_bits> class i {};
88 // The following classes represent the LLVM floating types.
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> {};
105 template<typename T, bool cross> class TypeBuilder<T*, cross> {
107 static const PointerType *get() {
108 static const PointerType *const result =
109 PointerType::getUnqual(TypeBuilder<T,cross>::get());
114 /// There is no support for references
115 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
118 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
120 static const ArrayType *get() {
121 static const ArrayType *const result =
122 ArrayType::get(TypeBuilder<T, cross>::get(), N);
126 /// LLVM uses an array of length 0 to represent an unknown-length array.
127 template<typename T, bool cross> class TypeBuilder<T[], cross> {
129 static const ArrayType *get() {
130 static const ArrayType *const result =
131 ArrayType::get(TypeBuilder<T, cross>::get(), 0);
136 // Define the C integral types only for TypeBuilder<T, false>.
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:
142 // On an ILP32 machine, stdint.h might define:
144 // typedef int int32_t;
145 // typedef long long int64_t;
146 // typedef long size_t;
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:
153 // typedef int int32_t;
154 // typedef long long int64_t;
155 // typedef int size_t;
157 // So we define all the primitive C types and nothing else.
158 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
159 template<> class TypeBuilder<T, false> { \
161 static const IntegerType *get() { \
162 static const IntegerType *const result = \
163 IntegerType::get(sizeof(T) * CHAR_BIT); \
167 template<> class TypeBuilder<T, true> { \
168 /* We provide a definition here so users don't accidentally */ \
169 /* define these types to work. */ \
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);
181 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
182 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
184 DEFINE_INTEGRAL_TYPEBUILDER(long long);
185 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
186 #endif /* _MSC_VER */
187 #undef DEFINE_INTEGRAL_TYPEBUILDER
189 template<uint32_t num_bits, bool cross>
190 class TypeBuilder<types::i<num_bits>, cross> {
192 static const IntegerType *get() {
193 static const IntegerType *const result = IntegerType::get(num_bits);
198 template<> class TypeBuilder<float, false> {
200 static const Type *get() {
201 return Type::FloatTy;
204 template<> class TypeBuilder<float, true> {};
206 template<> class TypeBuilder<double, false> {
208 static const Type *get() {
209 return Type::DoubleTy;
212 template<> class TypeBuilder<double, true> {};
214 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
216 static const Type *get() { return Type::FloatTy; }
218 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
220 static const Type *get() { return Type::DoubleTy; }
222 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
224 static const Type *get() { return Type::X86_FP80Ty; }
226 template<bool cross> class TypeBuilder<types::fp128, cross> {
228 static const Type *get() { return Type::FP128Ty; }
230 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
232 static const Type *get() { return Type::PPC_FP128Ty; }
235 template<bool cross> class TypeBuilder<void, cross> {
237 static const Type *get() {
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> {};
247 template<typename R, bool cross> class TypeBuilder<R(), cross> {
249 static const FunctionType *get() {
250 static const FunctionType *const result = create();
255 static const FunctionType *create() {
256 return FunctionType::get(TypeBuilder<R, cross>::get(), false);
259 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
261 static const FunctionType *get() {
262 static const FunctionType *const result = create();
267 static const FunctionType *create() {
268 std::vector<const Type*> params;
270 params.push_back(TypeBuilder<A1, cross>::get());
271 return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
274 template<typename R, typename A1, typename A2, bool cross>
275 class TypeBuilder<R(A1, A2), cross> {
277 static const FunctionType *get() {
278 static const FunctionType *const result = create();
283 static const FunctionType *create() {
284 std::vector<const Type*> params;
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);
291 template<typename R, typename A1, typename A2, typename A3, bool cross>
292 class TypeBuilder<R(A1, A2, A3), cross> {
294 static const FunctionType *get() {
295 static const FunctionType *const result = create();
300 static const FunctionType *create() {
301 std::vector<const Type*> params;
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);
310 template<typename R, typename A1, typename A2, typename A3, typename A4,
312 class TypeBuilder<R(A1, A2, A3, A4), cross> {
314 static const FunctionType *get() {
315 static const FunctionType *const result = create();
320 static const FunctionType *create() {
321 std::vector<const Type*> params;
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);
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> {
335 static const FunctionType *get() {
336 static const FunctionType *const result = create();
341 static const FunctionType *create() {
342 std::vector<const Type*> params;
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);
353 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
355 static const FunctionType *get() {
356 static const FunctionType *const result = create();
361 static const FunctionType *create() {
362 return FunctionType::get(TypeBuilder<R, cross>::get(), true);
365 template<typename R, typename A1, bool cross>
366 class TypeBuilder<R(A1, ...), cross> {
368 static const FunctionType *get() {
369 static const FunctionType *const result = create();
374 static const FunctionType *create() {
375 std::vector<const Type*> params;
377 params.push_back(TypeBuilder<A1, cross>::get());
378 return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
381 template<typename R, typename A1, typename A2, bool cross>
382 class TypeBuilder<R(A1, A2, ...), cross> {
384 static const FunctionType *get() {
385 static const FunctionType *const result = create();
390 static const FunctionType *create() {
391 std::vector<const Type*> params;
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);
398 template<typename R, typename A1, typename A2, typename A3, bool cross>
399 class TypeBuilder<R(A1, A2, A3, ...), cross> {
401 static const FunctionType *get() {
402 static const FunctionType *const result = create();
407 static const FunctionType *create() {
408 std::vector<const Type*> params;
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);
417 template<typename R, typename A1, typename A2, typename A3, typename A4,
419 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
421 static const FunctionType *get() {
422 static const FunctionType *const result = create();
427 static const FunctionType *create() {
428 std::vector<const Type*> params;
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);
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> {
442 static const FunctionType *get() {
443 static const FunctionType *const result = create();
448 static const FunctionType *create() {
449 std::vector<const Type*> params;
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);