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"
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:
41 /// void *array[1]; // Intended as a flexible array.
43 /// int8 AFunction(struct MyType *value);
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,
52 /// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
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),
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 /// 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 {};
80 // Types for use with cross-compilable TypeBuilders. These correspond
81 // exactly with an LLVM-native type.
83 /// i<N> corresponds to the LLVM IntegerType with N bits.
84 template<uint32_t num_bits> class i {};
86 // 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 PointerType *get(LLVMContext &Context) {
108 return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
112 /// There is no support for references
113 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
116 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
118 static ArrayType *get(LLVMContext &Context) {
119 return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
122 /// LLVM uses an array of length 0 to represent an unknown-length array.
123 template<typename T, bool cross> class TypeBuilder<T[], cross> {
125 static ArrayType *get(LLVMContext &Context) {
126 return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
130 // Define the C integral types only for TypeBuilder<T, false>.
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:
136 // On an ILP32 machine, stdint.h might define:
138 // typedef int int32_t;
139 // typedef long long int64_t;
140 // typedef long size_t;
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:
147 // typedef int int32_t;
148 // typedef long long int64_t;
149 // typedef int size_t;
151 // So we define all the primitive C types and nothing else.
152 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
153 template<> class TypeBuilder<T, false> { \
155 static IntegerType *get(LLVMContext &Context) { \
156 return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
159 template<> class TypeBuilder<T, true> { \
160 /* We provide a definition here so users don't accidentally */ \
161 /* define these types to work. */ \
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);
173 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
174 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
176 DEFINE_INTEGRAL_TYPEBUILDER(long long);
177 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
178 #endif /* _MSC_VER */
179 #undef DEFINE_INTEGRAL_TYPEBUILDER
181 template<uint32_t num_bits, bool cross>
182 class TypeBuilder<types::i<num_bits>, cross> {
184 static IntegerType *get(LLVMContext &C) {
185 return IntegerType::get(C, num_bits);
189 template<> class TypeBuilder<float, false> {
191 static Type *get(LLVMContext& C) {
192 return Type::getFloatTy(C);
195 template<> class TypeBuilder<float, true> {};
197 template<> class TypeBuilder<double, false> {
199 static Type *get(LLVMContext& C) {
200 return Type::getDoubleTy(C);
203 template<> class TypeBuilder<double, true> {};
205 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
207 static Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
209 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
211 static Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
213 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
215 static Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
217 template<bool cross> class TypeBuilder<types::fp128, cross> {
219 static Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
221 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
223 static Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
225 template<bool cross> class TypeBuilder<types::x86_mmx, cross> {
227 static Type *get(LLVMContext& C) { return Type::getX86_MMXTy(C); }
230 template<bool cross> class TypeBuilder<void, cross> {
232 static Type *get(LLVMContext &C) {
233 return Type::getVoidTy(C);
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> {};
248 template<typename R, bool cross> class TypeBuilder<R(), cross> {
250 static FunctionType *get(LLVMContext &Context) {
251 return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
254 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
256 static FunctionType *get(LLVMContext &Context) {
257 std::vector<Type*> params;
259 params.push_back(TypeBuilder<A1, cross>::get(Context));
260 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
264 template<typename R, typename A1, typename A2, bool cross>
265 class TypeBuilder<R(A1, A2), cross> {
267 static FunctionType *get(LLVMContext &Context) {
268 std::vector<Type*> params;
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),
276 template<typename R, typename A1, typename A2, typename A3, bool cross>
277 class TypeBuilder<R(A1, A2, A3), cross> {
279 static FunctionType *get(LLVMContext &Context) {
280 std::vector<Type*> params;
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),
290 template<typename R, typename A1, typename A2, typename A3, typename A4,
292 class TypeBuilder<R(A1, A2, A3, A4), cross> {
294 static FunctionType *get(LLVMContext &Context) {
295 std::vector<Type*> params;
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),
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> {
310 static FunctionType *get(LLVMContext &Context) {
311 std::vector<Type*> params;
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),
323 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
325 static FunctionType *get(LLVMContext &Context) {
326 return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
329 template<typename R, typename A1, bool cross>
330 class TypeBuilder<R(A1, ...), cross> {
332 static FunctionType *get(LLVMContext &Context) {
333 std::vector<Type*> params;
335 params.push_back(TypeBuilder<A1, cross>::get(Context));
336 return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
339 template<typename R, typename A1, typename A2, bool cross>
340 class TypeBuilder<R(A1, A2, ...), cross> {
342 static FunctionType *get(LLVMContext &Context) {
343 std::vector<Type*> params;
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),
351 template<typename R, typename A1, typename A2, typename A3, bool cross>
352 class TypeBuilder<R(A1, A2, A3, ...), cross> {
354 static FunctionType *get(LLVMContext &Context) {
355 std::vector<Type*> params;
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),
365 template<typename R, typename A1, typename A2, typename A3, typename A4,
367 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
369 static FunctionType *get(LLVMContext &Context) {
370 std::vector<Type*> params;
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),
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> {
385 static FunctionType *get(LLVMContext &Context) {
386 std::vector<Type*> params;
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),