LLVM  6.0.0svn
TypeBuilder.h
Go to the documentation of this file.
1 //===---- llvm/TypeBuilder.h - Builder for LLVM types -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_TYPEBUILDER_H
16 #define LLVM_IR_TYPEBUILDER_H
17 
18 #include "llvm/IR/DerivedTypes.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include <climits>
21 
22 namespace llvm {
23 
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:
36 ///
37 /// \code{.cpp}
38 ///
39 /// struct MyType {
40 /// int32 a;
41 /// int32 *b;
42 /// void *array[1]; // Intended as a flexible array.
43 /// };
44 /// int8 AFunction(struct MyType *value);
45 ///
46 /// \endcode
47 ///
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,
52 /// write:
53 ///
54 /// \code{.cpp}
55 ///
56 /// namespace llvm {
57 /// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
58 /// public:
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),
66 /// nullptr);
67 /// }
68 ///
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.
72 /// enum Fields {
73 /// FIELD_A,
74 /// FIELD_B,
75 /// FIELD_ARRAY
76 /// };
77 /// }
78 /// } // namespace llvm
79 ///
80 /// \endcode
81 ///
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 {};
86 
87 // Types for use with cross-compilable TypeBuilders. These correspond
88 // exactly with an LLVM-native type.
89 namespace types {
90 /// i<N> corresponds to the LLVM IntegerType with N bits.
91 template<uint32_t num_bits> class i {};
92 
93 // The following classes represent the LLVM floating types.
94 class ieee_float {};
95 class ieee_double {};
96 class x86_fp80 {};
97 class fp128 {};
98 class ppc_fp128 {};
99 // X86 MMX.
100 class x86_mmx {};
101 } // namespace types
102 
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> {};
110 
111 // Pointers
112 template<typename T, bool cross> class TypeBuilder<T*, cross> {
113 public:
116  }
117 };
118 
119 /// There is no support for references
120 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
121 
122 // Arrays
123 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
124 public:
125  static ArrayType *get(LLVMContext &Context) {
127  }
128 };
129 /// LLVM uses an array of length 0 to represent an unknown-length array.
130 template<typename T, bool cross> class TypeBuilder<T[], cross> {
131 public:
132  static ArrayType *get(LLVMContext &Context) {
134  }
135 };
136 
137 // Define the C integral types only for TypeBuilder<T, false>.
138 //
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:
142 //
143 // On an ILP32 machine, stdint.h might define:
144 //
145 // typedef int int32_t;
146 // typedef long long int64_t;
147 // typedef long size_t;
148 //
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:
153 //
154 // typedef int int32_t;
155 // typedef long long int64_t;
156 // typedef int size_t;
157 //
158 // So we define all the primitive C types and nothing else.
159 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
160 template<> class TypeBuilder<T, false> { \
161 public: \
162  static IntegerType *get(LLVMContext &Context) { \
163  return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
164  } \
165 }; \
166 template<> class TypeBuilder<T, true> { \
167  /* We provide a definition here so users don't accidentally */ \
168  /* define these types to work. */ \
169 }
171 DEFINE_INTEGRAL_TYPEBUILDER(signed char);
172 DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
174 DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
176 DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
178 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
179 #ifdef _MSC_VER
181 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
182 #else /* _MSC_VER */
183 DEFINE_INTEGRAL_TYPEBUILDER(long long);
184 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
185 #endif /* _MSC_VER */
186 #undef DEFINE_INTEGRAL_TYPEBUILDER
187 
188 template<uint32_t num_bits, bool cross>
189 class TypeBuilder<types::i<num_bits>, cross> {
190 public:
191  static IntegerType *get(LLVMContext &C) {
192  return IntegerType::get(C, num_bits);
193  }
194 };
195 
196 template<> class TypeBuilder<float, false> {
197 public:
198  static Type *get(LLVMContext& C) {
199  return Type::getFloatTy(C);
200  }
201 };
202 template<> class TypeBuilder<float, true> {};
203 
204 template<> class TypeBuilder<double, false> {
205 public:
206  static Type *get(LLVMContext& C) {
207  return Type::getDoubleTy(C);
208  }
209 };
210 template<> class TypeBuilder<double, true> {};
211 
212 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
213 public:
214  static Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
215 };
216 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
217 public:
218  static Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
219 };
220 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
221 public:
222  static Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
223 };
224 template<bool cross> class TypeBuilder<types::fp128, cross> {
225 public:
226  static Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
227 };
228 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
229 public:
230  static Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
231 };
232 template<bool cross> class TypeBuilder<types::x86_mmx, cross> {
233 public:
234  static Type *get(LLVMContext& C) { return Type::getX86_MMXTy(C); }
235 };
236 
237 template<bool cross> class TypeBuilder<void, cross> {
238 public:
239  static Type *get(LLVMContext &C) {
240  return Type::getVoidTy(C);
241  }
242 };
243 
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> {};
254 
255 template<typename R, bool cross> class TypeBuilder<R(), cross> {
256 public:
259  }
260 };
261 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
262 public:
264  Type *params[] = {
266  };
268  params, false);
269  }
270 };
271 template<typename R, typename A1, typename A2, bool cross>
272 class TypeBuilder<R(A1, A2), cross> {
273 public:
275  Type *params[] = {
278  };
280  params, false);
281  }
282 };
283 template<typename R, typename A1, typename A2, typename A3, bool cross>
284 class TypeBuilder<R(A1, A2, A3), cross> {
285 public:
287  Type *params[] = {
291  };
293  params, false);
294  }
295 };
296 
297 template<typename R, typename A1, typename A2, typename A3, typename A4,
298  bool cross>
299 class TypeBuilder<R(A1, A2, A3, A4), cross> {
300 public:
302  Type *params[] = {
307  };
309  params, false);
310  }
311 };
312 
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> {
316 public:
318  Type *params[] = {
324  };
326  params, false);
327  }
328 };
329 
330 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
331 public:
334  }
335 };
336 template<typename R, typename A1, bool cross>
337 class TypeBuilder<R(A1, ...), cross> {
338 public:
340  Type *params[] = {
342  };
344  }
345 };
346 template<typename R, typename A1, typename A2, bool cross>
347 class TypeBuilder<R(A1, A2, ...), cross> {
348 public:
350  Type *params[] = {
353  };
355  params, true);
356  }
357 };
358 template<typename R, typename A1, typename A2, typename A3, bool cross>
359 class TypeBuilder<R(A1, A2, A3, ...), cross> {
360 public:
362  Type *params[] = {
366  };
368  params, true);
369  }
370 };
371 
372 template<typename R, typename A1, typename A2, typename A3, typename A4,
373  bool cross>
374 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
375 public:
377  Type *params[] = {
382  };
384  params, true);
385  }
386 };
387 
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> {
391 public:
393  Type *params[] = {
399  };
401  params, true);
402  }
403 };
404 
405 } // namespace llvm
406 
407 #endif
uint64_t CallInst * C
static Type * getDoubleTy(LLVMContext &C)
Definition: Type.cpp:165
LLVMContext & Context
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
i<N> corresponds to the LLVM IntegerType with N bits.
Definition: TypeBuilder.h:91
static Type * getX86_MMXTy(LLVMContext &C)
Definition: Type.cpp:171
static Type * getX86_FP80Ty(LLVMContext &C)
Definition: Type.cpp:168
static Type * getFloatTy(LLVMContext &C)
Definition: Type.cpp:164
static Type * getPPC_FP128Ty(LLVMContext &C)
Definition: Type.cpp:170
Class to represent function types.
Definition: DerivedTypes.h:103
Class to represent array types.
Definition: DerivedTypes.h:369
Class to represent pointers.
Definition: DerivedTypes.h:467
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:69
static Type * getVoidTy(LLVMContext &C)
Definition: Type.cpp:161
TypeBuilder - This provides a uniform API for looking up types known at compile time.
Definition: TypeBuilder.h:85
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Definition: Type.cpp:297
Class to represent integer types.
Definition: DerivedTypes.h:40
static Type * getFP128Ty(LLVMContext &C)
Definition: Type.cpp:169
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
Definition: DerivedTypes.h:482
DEFINE_INTEGRAL_TYPEBUILDER(char)
Basic Alias true
#define N
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:568
aarch64 promote const