1 //===---- llvm/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_IR_TYPEBUILDER_H
16 #define LLVM_IR_TYPEBUILDER_H
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/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:
42 /// void *array[1]; // Intended as a flexible array.
44 /// int8 AFunction(struct MyType *value);
48 /// You'll want to use
49 /// Function::Create(TypeBuilder<types::i<8>(MyType*), true>::get(), ...)
50 /// to declare the function, but when you first try this, your compiler will
51 /// complain that TypeBuilder<MyType, true>::get() doesn't exist. To fix this,
57 /// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
59 /// static StructType *get(LLVMContext &Context) {
60 /// // If you cache this result, be sure to cache it separately
61 /// // for each LLVMContext.
62 /// return StructType::get(
63 /// TypeBuilder<types::i<32>, xcompile>::get(Context),
64 /// TypeBuilder<types::i<32>*, xcompile>::get(Context),
65 /// TypeBuilder<types::i<8>*[], xcompile>::get(Context),
69 /// // You may find this a convenient place to put some constants
70 /// // to help with getelementptr. They don't have any effect on
71 /// // the operation of TypeBuilder.
78 /// } // namespace llvm
82 /// TypeBuilder cannot handle recursive types or types you only know at runtime.
83 /// If you try to give it a recursive type, it will deadlock, infinitely
84 /// recurse, or do something similarly undesirable.
85 template<typename T, bool cross_compilable> class TypeBuilder {};
87 // Types for use with cross-compilable TypeBuilders. These correspond
88 // exactly with an LLVM-native type.
90 /// i<N> corresponds to the LLVM IntegerType with N bits.
91 template<uint32_t num_bits> class i {};
93 // The following classes represent the LLVM floating types.
103 // LLVM doesn't have const or volatile types.
104 template<typename T, bool cross> class TypeBuilder<const T, cross>
105 : public TypeBuilder<T, cross> {};
106 template<typename T, bool cross> class TypeBuilder<volatile T, cross>
107 : public TypeBuilder<T, cross> {};
108 template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
109 : public TypeBuilder<T, cross> {};
112 template<typename T, bool cross> class TypeBuilder<T*, cross> {
114 static PointerType *get(LLVMContext &Context) {
115 return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
119 /// There is no support for references
120 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
123 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
125 static ArrayType *get(LLVMContext &Context) {
126 return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
129 /// LLVM uses an array of length 0 to represent an unknown-length array.
130 template<typename T, bool cross> class TypeBuilder<T[], cross> {
132 static ArrayType *get(LLVMContext &Context) {
133 return 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 IntegerType *get(LLVMContext &Context) { \
163 return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
166 template<> class TypeBuilder<T, true> { \
167 /* We provide a definition here so users don't accidentally */ \
168 /* define these types to work. */ \
170 DEFINE_INTEGRAL_TYPEBUILDER(char);
171 DEFINE_INTEGRAL_TYPEBUILDER(signed char);
172 DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
173 DEFINE_INTEGRAL_TYPEBUILDER(short);
174 DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
175 DEFINE_INTEGRAL_TYPEBUILDER(int);
176 DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
177 DEFINE_INTEGRAL_TYPEBUILDER(long);
178 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
180 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
181 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
183 DEFINE_INTEGRAL_TYPEBUILDER(long long);
184 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
185 #endif /* _MSC_VER */
186 #undef DEFINE_INTEGRAL_TYPEBUILDER
188 template<uint32_t num_bits, bool cross>
189 class TypeBuilder<types::i<num_bits>, cross> {
191 static IntegerType *get(LLVMContext &C) {
192 return IntegerType::get(C, num_bits);
196 template<> class TypeBuilder<float, false> {
198 static Type *get(LLVMContext& C) {
199 return Type::getFloatTy(C);
202 template<> class TypeBuilder<float, true> {};
204 template<> class TypeBuilder<double, false> {
206 static Type *get(LLVMContext& C) {
207 return Type::getDoubleTy(C);
210 template<> class TypeBuilder<double, true> {};
212 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
214 static Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
216 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
218 static Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
220 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
222 static Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
224 template<bool cross> class TypeBuilder<types::fp128, cross> {
226 static Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
228 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
230 static Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
232 template<bool cross> class TypeBuilder<types::x86_mmx, cross> {
234 static Type *get(LLVMContext& C) { return Type::getX86_MMXTy(C); }
237 template<bool cross> class TypeBuilder<void, cross> {
239 static Type *get(LLVMContext &C) {
240 return Type::getVoidTy(C);
244 /// void* is disallowed in LLVM types, but it occurs often enough in C code that
245 /// we special case it.
246 template<> class TypeBuilder<void*, false>
247 : public TypeBuilder<types::i<8>*, false> {};
248 template<> class TypeBuilder<const void*, false>
249 : public TypeBuilder<types::i<8>*, false> {};
250 template<> class TypeBuilder<volatile void*, false>
251 : public TypeBuilder<types::i<8>*, false> {};
252 template<> class TypeBuilder<const volatile void*, false>
253 : public TypeBuilder<types::i<8>*, false> {};
255 template<typename R, bool cross> class TypeBuilder<R(), cross> {
257 static FunctionType *get(LLVMContext &Context) {
258 return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
261 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
263 static FunctionType *get(LLVMContext &Context) {
265 TypeBuilder<A1, cross>::get(Context),
267 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
271 template<typename R, typename A1, typename A2, bool cross>
272 class TypeBuilder<R(A1, A2), cross> {
274 static FunctionType *get(LLVMContext &Context) {
276 TypeBuilder<A1, cross>::get(Context),
277 TypeBuilder<A2, cross>::get(Context),
279 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
283 template<typename R, typename A1, typename A2, typename A3, bool cross>
284 class TypeBuilder<R(A1, A2, A3), cross> {
286 static FunctionType *get(LLVMContext &Context) {
288 TypeBuilder<A1, cross>::get(Context),
289 TypeBuilder<A2, cross>::get(Context),
290 TypeBuilder<A3, cross>::get(Context),
292 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
297 template<typename R, typename A1, typename A2, typename A3, typename A4,
299 class TypeBuilder<R(A1, A2, A3, A4), cross> {
301 static FunctionType *get(LLVMContext &Context) {
303 TypeBuilder<A1, cross>::get(Context),
304 TypeBuilder<A2, cross>::get(Context),
305 TypeBuilder<A3, cross>::get(Context),
306 TypeBuilder<A4, cross>::get(Context),
308 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
313 template<typename R, typename A1, typename A2, typename A3, typename A4,
314 typename A5, bool cross>
315 class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
317 static FunctionType *get(LLVMContext &Context) {
319 TypeBuilder<A1, cross>::get(Context),
320 TypeBuilder<A2, cross>::get(Context),
321 TypeBuilder<A3, cross>::get(Context),
322 TypeBuilder<A4, cross>::get(Context),
323 TypeBuilder<A5, cross>::get(Context),
325 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
330 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
332 static FunctionType *get(LLVMContext &Context) {
333 return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
336 template<typename R, typename A1, bool cross>
337 class TypeBuilder<R(A1, ...), cross> {
339 static FunctionType *get(LLVMContext &Context) {
341 TypeBuilder<A1, cross>::get(Context),
343 return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
346 template<typename R, typename A1, typename A2, bool cross>
347 class TypeBuilder<R(A1, A2, ...), cross> {
349 static FunctionType *get(LLVMContext &Context) {
351 TypeBuilder<A1, cross>::get(Context),
352 TypeBuilder<A2, cross>::get(Context),
354 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
358 template<typename R, typename A1, typename A2, typename A3, bool cross>
359 class TypeBuilder<R(A1, A2, A3, ...), cross> {
361 static FunctionType *get(LLVMContext &Context) {
363 TypeBuilder<A1, cross>::get(Context),
364 TypeBuilder<A2, cross>::get(Context),
365 TypeBuilder<A3, cross>::get(Context),
367 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
372 template<typename R, typename A1, typename A2, typename A3, typename A4,
374 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
376 static FunctionType *get(LLVMContext &Context) {
378 TypeBuilder<A1, cross>::get(Context),
379 TypeBuilder<A2, cross>::get(Context),
380 TypeBuilder<A3, cross>::get(Context),
381 TypeBuilder<A4, cross>::get(Context),
383 return FunctionType::get(TypeBuilder<R, cross>::get(Context),
388 template<typename R, typename A1, typename A2, typename A3, typename A4,
389 typename A5, bool cross>
390 class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
392 static FunctionType *get(LLVMContext &Context) {
394 TypeBuilder<A1, cross>::get(Context),
395 TypeBuilder<A2, cross>::get(Context),
396 TypeBuilder<A3, cross>::get(Context),
397 TypeBuilder<A4, cross>::get(Context),
398 TypeBuilder<A5, cross>::get(Context),
400 return FunctionType::get(TypeBuilder<R, cross>::get(Context),