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(LLVMContext &Context) {
54 /// // If you cache this result, be sure to cache it separately
55 /// // for each LLVMContext.
56 /// return StructType::get(
57 /// TypeBuilder<types::i<32>, xcompile>::get(Context),
58 /// TypeBuilder<types::i<32>*, xcompile>::get(Context),
59 /// TypeBuilder<types::i<8>*[], xcompile>::get(Context),
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 /// TypeBuilder cannot handle recursive types or types you only know at runtime.
75 /// If you try to give it a recursive type, it will deadlock, infinitely
76 /// recurse, or do something similarly undesirable.
77 template<typename T, bool cross_compilable> class TypeBuilder {};
79 // Types for use with cross-compilable TypeBuilders. These correspond
80 // exactly with an LLVM-native type.
82 /// i<N> corresponds to the LLVM IntegerType with N bits.
83 template<uint32_t num_bits> class i {};
85 // The following classes represent the LLVM floating types.
95 // LLVM doesn't have const or volatile types.
96 template<typename T, bool cross> class TypeBuilder<const T, cross>
97 : public TypeBuilder<T, cross> {};
98 template<typename T, bool cross> class TypeBuilder<volatile T, cross>
99 : public TypeBuilder<T, cross> {};
100 template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
101 : public TypeBuilder<T, cross> {};
104 template<typename T, bool cross> class TypeBuilder<T*, cross> {
106 static const PointerType *get(LLVMContext &Context) {
107 return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
111 /// There is no support for references
112 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
115 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
117 static const ArrayType *get(LLVMContext &Context) {
118 return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
121 /// LLVM uses an array of length 0 to represent an unknown-length array.
122 template<typename T, bool cross> class TypeBuilder<T[], cross> {
124 static const ArrayType *get(LLVMContext &Context) {
125 return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
129 // Define the C integral types only for TypeBuilder<T, false>.
131 // C integral types do not have a defined size. It would be nice to use the
132 // stdint.h-defined typedefs that do have defined sizes, but we'd run into the
133 // following problem:
135 // On an ILP32 machine, stdint.h might define:
137 // typedef int int32_t;
138 // typedef long long int64_t;
139 // typedef long size_t;
141 // If we defined TypeBuilder<int32_t> and TypeBuilder<int64_t>, then any use of
142 // TypeBuilder<size_t> would fail. We couldn't define TypeBuilder<size_t> in
143 // addition to the defined-size types because we'd get duplicate definitions on
144 // platforms where stdint.h instead defines:
146 // typedef int int32_t;
147 // typedef long long int64_t;
148 // typedef int size_t;
150 // So we define all the primitive C types and nothing else.
151 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
152 template<> class TypeBuilder<T, false> { \
154 static const IntegerType *get(LLVMContext &Context) { \
155 return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
158 template<> class TypeBuilder<T, true> { \
159 /* We provide a definition here so users don't accidentally */ \
160 /* define these types to work. */ \
162 DEFINE_INTEGRAL_TYPEBUILDER(char);
163 DEFINE_INTEGRAL_TYPEBUILDER(signed char);
164 DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
165 DEFINE_INTEGRAL_TYPEBUILDER(short);
166 DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
167 DEFINE_INTEGRAL_TYPEBUILDER(int);
168 DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
169 DEFINE_INTEGRAL_TYPEBUILDER(long);
170 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
172 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
173 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
175 DEFINE_INTEGRAL_TYPEBUILDER(long long);
176 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
177 #endif /* _MSC_VER */
178 #undef DEFINE_INTEGRAL_TYPEBUILDER
180 template<uint32_t num_bits, bool cross>
181 class TypeBuilder<types::i<num_bits>, cross> {
183 static const IntegerType *get(LLVMContext &C) {
184 return IntegerType::get(C, num_bits);
188 template<> class TypeBuilder<float, false> {
190 static const Type *get(LLVMContext& C) {
191 return Type::getFloatTy(C);
194 template<> class TypeBuilder<float, true> {};
196 template<> class TypeBuilder<double, false> {
198 static const Type *get(LLVMContext& C) {
199 return Type::getDoubleTy(C);
202 template<> class TypeBuilder<double, true> {};
204 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
206 static const Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
208 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
210 static const Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
212 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
214 static const Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
216 template<bool cross> class TypeBuilder<types::fp128, cross> {
218 static const Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
220 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
222 static const Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
224 template<bool cross> class TypeBuilder<types::x86_mmx, cross> {
226 static const Type *get(LLVMContext& C) { return Type::getX86_MMXTy(C); }
229 template<bool cross> class TypeBuilder<void, cross> {
231 static const Type *get(LLVMContext &C) {
232 return Type::getVoidTy(C);
236 /// void* is disallowed in LLVM types, but it occurs often enough in C code that
237 /// we special case it.
238 template<> class TypeBuilder<void*, false>
239 : public TypeBuilder<types::i<8>*, false> {};
240 template<> class TypeBuilder<const void*, false>
241 : public TypeBuilder<types::i<8>*, false> {};
242 template<> class TypeBuilder<volatile void*, false>
243 : public TypeBuilder<types::i<8>*, false> {};
244 template<> class TypeBuilder<const volatile void*, false>
245 : public TypeBuilder<types::i<8>*, false> {};
247 template<typename R, bool cross> class TypeBuilder<R(), cross> {
249 static const FunctionType *get(LLVMContext &Context) {
250 return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
253 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
255 static const FunctionType *get(LLVMContext &Context) {
256 std::vector<const Type*> params;
258 params.push_back(TypeBuilder<A1, cross>::get(Context));
259 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
263 template<typename R, typename A1, typename A2, bool cross>
264 class TypeBuilder<R(A1, A2), cross> {
266 static const FunctionType *get(LLVMContext &Context) {
267 std::vector<const Type*> params;
269 params.push_back(TypeBuilder<A1, cross>::get(Context));
270 params.push_back(TypeBuilder<A2, cross>::get(Context));
271 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
275 template<typename R, typename A1, typename A2, typename A3, bool cross>
276 class TypeBuilder<R(A1, A2, A3), cross> {
278 static const FunctionType *get(LLVMContext &Context) {
279 std::vector<const Type*> params;
281 params.push_back(TypeBuilder<A1, cross>::get(Context));
282 params.push_back(TypeBuilder<A2, cross>::get(Context));
283 params.push_back(TypeBuilder<A3, cross>::get(Context));
284 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
289 template<typename R, typename A1, typename A2, typename A3, typename A4,
291 class TypeBuilder<R(A1, A2, A3, A4), cross> {
293 static const FunctionType *get(LLVMContext &Context) {
294 std::vector<const Type*> params;
296 params.push_back(TypeBuilder<A1, cross>::get(Context));
297 params.push_back(TypeBuilder<A2, cross>::get(Context));
298 params.push_back(TypeBuilder<A3, cross>::get(Context));
299 params.push_back(TypeBuilder<A4, cross>::get(Context));
300 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
305 template<typename R, typename A1, typename A2, typename A3, typename A4,
306 typename A5, bool cross>
307 class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
309 static const FunctionType *get(LLVMContext &Context) {
310 std::vector<const Type*> params;
312 params.push_back(TypeBuilder<A1, cross>::get(Context));
313 params.push_back(TypeBuilder<A2, cross>::get(Context));
314 params.push_back(TypeBuilder<A3, cross>::get(Context));
315 params.push_back(TypeBuilder<A4, cross>::get(Context));
316 params.push_back(TypeBuilder<A5, cross>::get(Context));
317 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
322 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
324 static const FunctionType *get(LLVMContext &Context) {
325 return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
328 template<typename R, typename A1, bool cross>
329 class TypeBuilder<R(A1, ...), cross> {
331 static const FunctionType *get(LLVMContext &Context) {
332 std::vector<const Type*> params;
334 params.push_back(TypeBuilder<A1, cross>::get(Context));
335 return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
338 template<typename R, typename A1, typename A2, bool cross>
339 class TypeBuilder<R(A1, A2, ...), cross> {
341 static const FunctionType *get(LLVMContext &Context) {
342 std::vector<const Type*> params;
344 params.push_back(TypeBuilder<A1, cross>::get(Context));
345 params.push_back(TypeBuilder<A2, cross>::get(Context));
346 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
350 template<typename R, typename A1, typename A2, typename A3, bool cross>
351 class TypeBuilder<R(A1, A2, A3, ...), cross> {
353 static const FunctionType *get(LLVMContext &Context) {
354 std::vector<const Type*> params;
356 params.push_back(TypeBuilder<A1, cross>::get(Context));
357 params.push_back(TypeBuilder<A2, cross>::get(Context));
358 params.push_back(TypeBuilder<A3, cross>::get(Context));
359 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
364 template<typename R, typename A1, typename A2, typename A3, typename A4,
366 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
368 static const FunctionType *get(LLVMContext &Context) {
369 std::vector<const Type*> params;
371 params.push_back(TypeBuilder<A1, cross>::get(Context));
372 params.push_back(TypeBuilder<A2, cross>::get(Context));
373 params.push_back(TypeBuilder<A3, cross>::get(Context));
374 params.push_back(TypeBuilder<A4, cross>::get(Context));
375 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
380 template<typename R, typename A1, typename A2, typename A3, typename A4,
381 typename A5, bool cross>
382 class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
384 static const FunctionType *get(LLVMContext &Context) {
385 std::vector<const Type*> params;
387 params.push_back(TypeBuilder<A1, cross>::get(Context));
388 params.push_back(TypeBuilder<A2, cross>::get(Context));
389 params.push_back(TypeBuilder<A3, cross>::get(Context));
390 params.push_back(TypeBuilder<A4, cross>::get(Context));
391 params.push_back(TypeBuilder<A5, cross>::get(Context));
392 return FunctionType::get(TypeBuilder<R, cross>::get(Context),