llvm.org GIT mirror llvm / release_60 include / llvm / IR / TypeBuilder.h
release_60

Tree @release_60 (Download .tar.gz)

TypeBuilder.h @release_60raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//===---- llvm/TypeBuilder.h - Builder for LLVM types -----------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the TypeBuilder class, which is used as a convenient way to
// create LLVM types with a consistent and simplified interface.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_IR_TYPEBUILDER_H
#define LLVM_IR_TYPEBUILDER_H

#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/LLVMContext.h"
#include <climits>

namespace llvm {

/// TypeBuilder - This provides a uniform API for looking up types
/// known at compile time.  To support cross-compilation, we define a
/// series of tag types in the llvm::types namespace, like i<N>,
/// ieee_float, ppc_fp128, etc.  TypeBuilder<T, false> allows T to be
/// any of these, a native C type (whose size may depend on the host
/// compiler), or a pointer, function, or struct type built out of
/// these.  TypeBuilder<T, true> removes native C types from this set
/// to guarantee that its result is suitable for cross-compilation.
/// We define the primitive types, pointer types, and functions up to
/// 5 arguments here, but to use this class with your own types,
/// you'll need to specialize it.  For example, say you want to call a
/// function defined externally as:
///
/// \code{.cpp}
///
///   struct MyType {
///     int32 a;
///     int32 *b;
///     void *array[1];  // Intended as a flexible array.
///   };
///   int8 AFunction(struct MyType *value);
///
/// \endcode
///
/// You'll want to use
///   Function::Create(TypeBuilder<types::i<8>(MyType*), true>::get(), ...)
/// to declare the function, but when you first try this, your compiler will
/// complain that TypeBuilder<MyType, true>::get() doesn't exist. To fix this,
/// write:
///
/// \code{.cpp}
///
///   namespace llvm {
///   template<bool xcompile> class TypeBuilder<MyType, xcompile> {
///   public:
///     static StructType *get(LLVMContext &Context) {
///       // If you cache this result, be sure to cache it separately
///       // for each LLVMContext.
///       return StructType::get(
///         TypeBuilder<types::i<32>, xcompile>::get(Context),
///         TypeBuilder<types::i<32>*, xcompile>::get(Context),
///         TypeBuilder<types::i<8>*[], xcompile>::get(Context),
///         nullptr);
///     }
///
///     // You may find this a convenient place to put some constants
///     // to help with getelementptr.  They don't have any effect on
///     // the operation of TypeBuilder.
///     enum Fields {
///       FIELD_A,
///       FIELD_B,
///       FIELD_ARRAY
///     };
///   }
///   }  // namespace llvm
///
/// \endcode
///
/// TypeBuilder cannot handle recursive types or types you only know at runtime.
/// If you try to give it a recursive type, it will deadlock, infinitely
/// recurse, or do something similarly undesirable.
template<typename T, bool cross_compilable> class TypeBuilder {};

// Types for use with cross-compilable TypeBuilders.  These correspond
// exactly with an LLVM-native type.
namespace types {
/// i<N> corresponds to the LLVM IntegerType with N bits.
template<uint32_t num_bits> class i {};

// The following classes represent the LLVM floating types.
class ieee_float {};
class ieee_double {};
class x86_fp80 {};
class fp128 {};
class ppc_fp128 {};
// X86 MMX.
class x86_mmx {};
}  // namespace types

// LLVM doesn't have const or volatile types.
template<typename T, bool cross> class TypeBuilder<const T, cross>
  : public TypeBuilder<T, cross> {};
template<typename T, bool cross> class TypeBuilder<volatile T, cross>
  : public TypeBuilder<T, cross> {};
template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
  : public TypeBuilder<T, cross> {};

// Pointers
template<typename T, bool cross> class TypeBuilder<T*, cross> {
public:
  static PointerType *get(LLVMContext &Context) {
    return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
  }
};

/// There is no support for references
template<typename T, bool cross> class TypeBuilder<T&, cross> {};

// Arrays
template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
public:
  static ArrayType *get(LLVMContext &Context) {
    return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
  }
};
/// LLVM uses an array of length 0 to represent an unknown-length array.
template<typename T, bool cross> class TypeBuilder<T[], cross> {
public:
  static ArrayType *get(LLVMContext &Context) {
    return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
  }
};

// Define the C integral types only for TypeBuilder<T, false>.
//
// C integral types do not have a defined size. It would be nice to use the
// stdint.h-defined typedefs that do have defined sizes, but we'd run into the
// following problem:
//
// On an ILP32 machine, stdint.h might define:
//
//   typedef int int32_t;
//   typedef long long int64_t;
//   typedef long size_t;
//
// If we defined TypeBuilder<int32_t> and TypeBuilder<int64_t>, then any use of
// TypeBuilder<size_t> would fail.  We couldn't define TypeBuilder<size_t> in
// addition to the defined-size types because we'd get duplicate definitions on
// platforms where stdint.h instead defines:
//
//   typedef int int32_t;
//   typedef long long int64_t;
//   typedef int size_t;
//
// So we define all the primitive C types and nothing else.
#define DEFINE_INTEGRAL_TYPEBUILDER(T) \
template<> class TypeBuilder<T, false> { \
public: \
  static IntegerType *get(LLVMContext &Context) { \
    return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
  } \
}; \
template<> class TypeBuilder<T, true> { \
  /* We provide a definition here so users don't accidentally */ \
  /* define these types to work. */ \
}
DEFINE_INTEGRAL_TYPEBUILDER(char);
DEFINE_INTEGRAL_TYPEBUILDER(signed char);
DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
DEFINE_INTEGRAL_TYPEBUILDER(short);
DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
DEFINE_INTEGRAL_TYPEBUILDER(int);
DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
DEFINE_INTEGRAL_TYPEBUILDER(long);
DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
#ifdef _MSC_VER
DEFINE_INTEGRAL_TYPEBUILDER(__int64);
DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
#else /* _MSC_VER */
DEFINE_INTEGRAL_TYPEBUILDER(long long);
DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
#endif /* _MSC_VER */
#undef DEFINE_INTEGRAL_TYPEBUILDER

template<uint32_t num_bits, bool cross>
class TypeBuilder<types::i<num_bits>, cross> {
public:
  static IntegerType *get(LLVMContext &C) {
    return IntegerType::get(C, num_bits);
  }
};

template<> class TypeBuilder<float, false> {
public:
  static Type *get(LLVMContext& C) {
    return Type::getFloatTy(C);
  }
};
template<> class TypeBuilder<float, true> {};

template<> class TypeBuilder<double, false> {
public:
  static Type *get(LLVMContext& C) {
    return Type::getDoubleTy(C);
  }
};
template<> class TypeBuilder<double, true> {};

template<bool cross> class TypeBuilder<types::ieee_float, cross> {
public:
  static Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
};
template<bool cross> class TypeBuilder<types::ieee_double, cross> {
public:
  static Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
};
template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
public:
  static Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
};
template<bool cross> class TypeBuilder<types::fp128, cross> {
public:
  static Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
};
template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
public:
  static Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
};
template<bool cross> class TypeBuilder<types::x86_mmx, cross> {
public:
  static Type *get(LLVMContext& C) { return Type::getX86_MMXTy(C); }
};

template<bool cross> class TypeBuilder<void, cross> {
public:
  static Type *get(LLVMContext &C) {
    return Type::getVoidTy(C);
  }
};

/// void* is disallowed in LLVM types, but it occurs often enough in C code that
/// we special case it.
template<> class TypeBuilder<void*, false>
  : public TypeBuilder<types::i<8>*, false> {};
template<> class TypeBuilder<const void*, false>
  : public TypeBuilder<types::i<8>*, false> {};
template<> class TypeBuilder<volatile void*, false>
  : public TypeBuilder<types::i<8>*, false> {};
template<> class TypeBuilder<const volatile void*, false>
  : public TypeBuilder<types::i<8>*, false> {};

template<typename R, bool cross> class TypeBuilder<R(), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
  }
};
template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                             params, false);
  }
};
template<typename R, typename A1, typename A2, bool cross>
class TypeBuilder<R(A1, A2), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                             params, false);
  }
};
template<typename R, typename A1, typename A2, typename A3, bool cross>
class TypeBuilder<R(A1, A2, A3), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
      TypeBuilder<A3, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                             params, false);
  }
};

template<typename R, typename A1, typename A2, typename A3, typename A4,
         bool cross>
class TypeBuilder<R(A1, A2, A3, A4), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
      TypeBuilder<A3, cross>::get(Context),
      TypeBuilder<A4, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                             params, false);
  }
};

template<typename R, typename A1, typename A2, typename A3, typename A4,
         typename A5, bool cross>
class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
      TypeBuilder<A3, cross>::get(Context),
      TypeBuilder<A4, cross>::get(Context),
      TypeBuilder<A5, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                             params, false);
  }
};

template<typename R, bool cross> class TypeBuilder<R(...), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
  }
};
template<typename R, typename A1, bool cross>
class TypeBuilder<R(A1, ...), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
  }
};
template<typename R, typename A1, typename A2, bool cross>
class TypeBuilder<R(A1, A2, ...), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                                   params, true);
  }
};
template<typename R, typename A1, typename A2, typename A3, bool cross>
class TypeBuilder<R(A1, A2, A3, ...), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
      TypeBuilder<A3, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                                   params, true);
  }
};

template<typename R, typename A1, typename A2, typename A3, typename A4,
         bool cross>
class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
      TypeBuilder<A3, cross>::get(Context),
      TypeBuilder<A4, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                             params, true);
  }
};

template<typename R, typename A1, typename A2, typename A3, typename A4,
         typename A5, bool cross>
class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
public:
  static FunctionType *get(LLVMContext &Context) {
    Type *params[] = {
      TypeBuilder<A1, cross>::get(Context),
      TypeBuilder<A2, cross>::get(Context),
      TypeBuilder<A3, cross>::get(Context),
      TypeBuilder<A4, cross>::get(Context),
      TypeBuilder<A5, cross>::get(Context),
    };
    return FunctionType::get(TypeBuilder<R, cross>::get(Context),
                                   params, true);
  }
};

}  // namespace llvm

#endif