LLVM API Documentation

TypeBuilder.h

Go to the documentation of this file.
00001 //===---- llvm/Support/TypeBuilder.h - Builder for LLVM types ---*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the TypeBuilder class, which is used as a convenient way to
00011 // create LLVM types with a consistent and simplified interface.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_TYPEBUILDER_H
00016 #define LLVM_SUPPORT_TYPEBUILDER_H
00017 
00018 #include "llvm/DerivedTypes.h"
00019 #include "llvm/LLVMContext.h"
00020 #include <limits.h>
00021 
00022 namespace llvm {
00023 
00024 /// TypeBuilder - This provides a uniform API for looking up types
00025 /// known at compile time.  To support cross-compilation, we define a
00026 /// series of tag types in the llvm::types namespace, like i<N>,
00027 /// ieee_float, ppc_fp128, etc.  TypeBuilder<T, false> allows T to be
00028 /// any of these, a native C type (whose size may depend on the host
00029 /// compiler), or a pointer, function, or struct type built out of
00030 /// these.  TypeBuilder<T, true> removes native C types from this set
00031 /// to guarantee that its result is suitable for cross-compilation.
00032 /// We define the primitive types, pointer types, and functions up to
00033 /// 5 arguments here, but to use this class with your own types,
00034 /// you'll need to specialize it.  For example, say you want to call a
00035 /// function defined externally as:
00036 ///
00037 ///   struct MyType {
00038 ///     int32 a;
00039 ///     int32 *b;
00040 ///     void *array[1];  // Intended as a flexible array.
00041 ///   };
00042 ///   int8 AFunction(struct MyType *value);
00043 ///
00044 /// You'll want to use
00045 ///   Function::Create(TypeBuilder<types::i<8>(MyType*), true>::get(), ...)
00046 /// to declare the function, but when you first try this, your compiler will
00047 /// complain that TypeBuilder<MyType, true>::get() doesn't exist. To fix this,
00048 /// write:
00049 ///
00050 ///   namespace llvm {
00051 ///   template<bool xcompile> class TypeBuilder<MyType, xcompile> {
00052 ///   public:
00053 ///     static const StructType *get(LLVMContext &Context) {
00054 ///       // If you cache this result, be sure to cache it separately
00055 ///       // for each LLVMContext.
00056 ///       return StructType::get(
00057 ///         TypeBuilder<types::i<32>, xcompile>::get(Context),
00058 ///         TypeBuilder<types::i<32>*, xcompile>::get(Context),
00059 ///         TypeBuilder<types::i<8>*[], xcompile>::get(Context),
00060 ///         NULL);
00061 ///     }
00062 ///
00063 ///     // You may find this a convenient place to put some constants
00064 ///     // to help with getelementptr.  They don't have any effect on
00065 ///     // the operation of TypeBuilder.
00066 ///     enum Fields {
00067 ///       FIELD_A,
00068 ///       FIELD_B,
00069 ///       FIELD_ARRAY
00070 ///     };
00071 ///   }
00072 ///   }  // namespace llvm
00073 ///
00074 /// TypeBuilder cannot handle recursive types or types you only know at runtime.
00075 /// If you try to give it a recursive type, it will deadlock, infinitely
00076 /// recurse, or throw a recursive_init exception.
00077 template<typename T, bool cross_compilable> class TypeBuilder {};
00078 
00079 // Types for use with cross-compilable TypeBuilders.  These correspond
00080 // exactly with an LLVM-native type.
00081 namespace types {
00082 /// i<N> corresponds to the LLVM IntegerType with N bits.
00083 template<uint32_t num_bits> class i {};
00084 
00085 // The following classes represent the LLVM floating types.
00086 class ieee_float {};
00087 class ieee_double {};
00088 class x86_fp80 {};
00089 class fp128 {};
00090 class ppc_fp128 {};
00091 }  // namespace types
00092 
00093 // LLVM doesn't have const or volatile types.
00094 template<typename T, bool cross> class TypeBuilder<const T, cross>
00095   : public TypeBuilder<T, cross> {};
00096 template<typename T, bool cross> class TypeBuilder<volatile T, cross>
00097   : public TypeBuilder<T, cross> {};
00098 template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
00099   : public TypeBuilder<T, cross> {};
00100 
00101 // Pointers
00102 template<typename T, bool cross> class TypeBuilder<T*, cross> {
00103 public:
00104   static const PointerType *get(LLVMContext &Context) {
00105     return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
00106   }
00107 };
00108 
00109 /// There is no support for references
00110 template<typename T, bool cross> class TypeBuilder<T&, cross> {};
00111 
00112 // Arrays
00113 template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
00114 public:
00115   static const ArrayType *get(LLVMContext &Context) {
00116     return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
00117   }
00118 };
00119 /// LLVM uses an array of length 0 to represent an unknown-length array.
00120 template<typename T, bool cross> class TypeBuilder<T[], cross> {
00121 public:
00122   static const ArrayType *get(LLVMContext &Context) {
00123     return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
00124   }
00125 };
00126 
00127 // Define the C integral types only for TypeBuilder<T, false>.
00128 //
00129 // C integral types do not have a defined size. It would be nice to use the
00130 // stdint.h-defined typedefs that do have defined sizes, but we'd run into the
00131 // following problem:
00132 //
00133 // On an ILP32 machine, stdint.h might define:
00134 //
00135 //   typedef int int32_t;
00136 //   typedef long long int64_t;
00137 //   typedef long size_t;
00138 //
00139 // If we defined TypeBuilder<int32_t> and TypeBuilder<int64_t>, then any use of
00140 // TypeBuilder<size_t> would fail.  We couldn't define TypeBuilder<size_t> in
00141 // addition to the defined-size types because we'd get duplicate definitions on
00142 // platforms where stdint.h instead defines:
00143 //
00144 //   typedef int int32_t;
00145 //   typedef long long int64_t;
00146 //   typedef int size_t;
00147 //
00148 // So we define all the primitive C types and nothing else.
00149 #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
00150 template<> class TypeBuilder<T, false> { \
00151 public: \
00152   static const IntegerType *get(LLVMContext &Context) { \
00153     return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
00154   } \
00155 }; \
00156 template<> class TypeBuilder<T, true> { \
00157   /* We provide a definition here so users don't accidentally */ \
00158   /* define these types to work. */ \
00159 }
00160 DEFINE_INTEGRAL_TYPEBUILDER(char);
00161 DEFINE_INTEGRAL_TYPEBUILDER(signed char);
00162 DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
00163 DEFINE_INTEGRAL_TYPEBUILDER(short);
00164 DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
00165 DEFINE_INTEGRAL_TYPEBUILDER(int);
00166 DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
00167 DEFINE_INTEGRAL_TYPEBUILDER(long);
00168 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
00169 #ifdef _MSC_VER
00170 DEFINE_INTEGRAL_TYPEBUILDER(__int64);
00171 DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
00172 #else /* _MSC_VER */
00173 DEFINE_INTEGRAL_TYPEBUILDER(long long);
00174 DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
00175 #endif /* _MSC_VER */
00176 #undef DEFINE_INTEGRAL_TYPEBUILDER
00177 
00178 template<uint32_t num_bits, bool cross>
00179 class TypeBuilder<types::i<num_bits>, cross> {
00180 public:
00181   static const IntegerType *get(LLVMContext &C) {
00182     return IntegerType::get(C, num_bits);
00183   }
00184 };
00185 
00186 template<> class TypeBuilder<float, false> {
00187 public:
00188   static const Type *get(LLVMContext& C) {
00189     return Type::getFloatTy(C);
00190   }
00191 };
00192 template<> class TypeBuilder<float, true> {};
00193 
00194 template<> class TypeBuilder<double, false> {
00195 public:
00196   static const Type *get(LLVMContext& C) {
00197     return Type::getDoubleTy(C);
00198   }
00199 };
00200 template<> class TypeBuilder<double, true> {};
00201 
00202 template<bool cross> class TypeBuilder<types::ieee_float, cross> {
00203 public:
00204   static const Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
00205 };
00206 template<bool cross> class TypeBuilder<types::ieee_double, cross> {
00207 public:
00208   static const Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
00209 };
00210 template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
00211 public:
00212   static const Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
00213 };
00214 template<bool cross> class TypeBuilder<types::fp128, cross> {
00215 public:
00216   static const Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
00217 };
00218 template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
00219 public:
00220   static const Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
00221 };
00222 
00223 template<bool cross> class TypeBuilder<void, cross> {
00224 public:
00225   static const Type *get(LLVMContext &C) {
00226     return Type::getVoidTy(C);
00227   }
00228 };
00229 
00230 /// void* is disallowed in LLVM types, but it occurs often enough in C code that
00231 /// we special case it.
00232 template<> class TypeBuilder<void*, false>
00233   : public TypeBuilder<types::i<8>*, false> {};
00234 
00235 template<typename R, bool cross> class TypeBuilder<R(), cross> {
00236 public:
00237   static const FunctionType *get(LLVMContext &Context) {
00238     return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
00239   }
00240 };
00241 template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
00242 public:
00243   static const FunctionType *get(LLVMContext &Context) {
00244     std::vector<const Type*> params;
00245     params.reserve(1);
00246     params.push_back(TypeBuilder<A1, cross>::get(Context));
00247     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00248                              params, false);
00249   }
00250 };
00251 template<typename R, typename A1, typename A2, bool cross>
00252 class TypeBuilder<R(A1, A2), cross> {
00253 public:
00254   static const FunctionType *get(LLVMContext &Context) {
00255     std::vector<const Type*> params;
00256     params.reserve(2);
00257     params.push_back(TypeBuilder<A1, cross>::get(Context));
00258     params.push_back(TypeBuilder<A2, cross>::get(Context));
00259     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00260                              params, false);
00261   }
00262 };
00263 template<typename R, typename A1, typename A2, typename A3, bool cross>
00264 class TypeBuilder<R(A1, A2, A3), cross> {
00265 public:
00266   static const FunctionType *get(LLVMContext &Context) {
00267     std::vector<const Type*> params;
00268     params.reserve(3);
00269     params.push_back(TypeBuilder<A1, cross>::get(Context));
00270     params.push_back(TypeBuilder<A2, cross>::get(Context));
00271     params.push_back(TypeBuilder<A3, cross>::get(Context));
00272     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00273                              params, false);
00274   }
00275 };
00276 
00277 template<typename R, typename A1, typename A2, typename A3, typename A4,
00278          bool cross>
00279 class TypeBuilder<R(A1, A2, A3, A4), cross> {
00280 public:
00281   static const FunctionType *get(LLVMContext &Context) {
00282     std::vector<const Type*> params;
00283     params.reserve(4);
00284     params.push_back(TypeBuilder<A1, cross>::get(Context));
00285     params.push_back(TypeBuilder<A2, cross>::get(Context));
00286     params.push_back(TypeBuilder<A3, cross>::get(Context));
00287     params.push_back(TypeBuilder<A4, cross>::get(Context));
00288     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00289                              params, false);
00290   }
00291 };
00292 
00293 template<typename R, typename A1, typename A2, typename A3, typename A4,
00294          typename A5, bool cross>
00295 class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
00296 public:
00297   static const FunctionType *get(LLVMContext &Context) {
00298     std::vector<const Type*> params;
00299     params.reserve(5);
00300     params.push_back(TypeBuilder<A1, cross>::get(Context));
00301     params.push_back(TypeBuilder<A2, cross>::get(Context));
00302     params.push_back(TypeBuilder<A3, cross>::get(Context));
00303     params.push_back(TypeBuilder<A4, cross>::get(Context));
00304     params.push_back(TypeBuilder<A5, cross>::get(Context));
00305     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00306                              params, false);
00307   }
00308 };
00309 
00310 template<typename R, bool cross> class TypeBuilder<R(...), cross> {
00311 public:
00312   static const FunctionType *get(LLVMContext &Context) {
00313     return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
00314   }
00315 };
00316 template<typename R, typename A1, bool cross>
00317 class TypeBuilder<R(A1, ...), cross> {
00318 public:
00319   static const FunctionType *get(LLVMContext &Context) {
00320     std::vector<const Type*> params;
00321     params.reserve(1);
00322     params.push_back(TypeBuilder<A1, cross>::get(Context));
00323     return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
00324   }
00325 };
00326 template<typename R, typename A1, typename A2, bool cross>
00327 class TypeBuilder<R(A1, A2, ...), cross> {
00328 public:
00329   static const FunctionType *get(LLVMContext &Context) {
00330     std::vector<const Type*> params;
00331     params.reserve(2);
00332     params.push_back(TypeBuilder<A1, cross>::get(Context));
00333     params.push_back(TypeBuilder<A2, cross>::get(Context));
00334     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00335                                    params, true);
00336   }
00337 };
00338 template<typename R, typename A1, typename A2, typename A3, bool cross>
00339 class TypeBuilder<R(A1, A2, A3, ...), cross> {
00340 public:
00341   static const FunctionType *get(LLVMContext &Context) {
00342     std::vector<const Type*> params;
00343     params.reserve(3);
00344     params.push_back(TypeBuilder<A1, cross>::get(Context));
00345     params.push_back(TypeBuilder<A2, cross>::get(Context));
00346     params.push_back(TypeBuilder<A3, cross>::get(Context));
00347     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00348                                    params, true);
00349   }
00350 };
00351 
00352 template<typename R, typename A1, typename A2, typename A3, typename A4,
00353          bool cross>
00354 class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
00355 public:
00356   static const FunctionType *get(LLVMContext &Context) {
00357     std::vector<const Type*> params;
00358     params.reserve(4);
00359     params.push_back(TypeBuilder<A1, cross>::get(Context));
00360     params.push_back(TypeBuilder<A2, cross>::get(Context));
00361     params.push_back(TypeBuilder<A3, cross>::get(Context));
00362     params.push_back(TypeBuilder<A4, cross>::get(Context));
00363     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00364                              params, true);
00365   }
00366 };
00367 
00368 template<typename R, typename A1, typename A2, typename A3, typename A4,
00369          typename A5, bool cross>
00370 class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
00371 public:
00372   static const FunctionType *get(LLVMContext &Context) {
00373     std::vector<const Type*> params;
00374     params.reserve(5);
00375     params.push_back(TypeBuilder<A1, cross>::get(Context));
00376     params.push_back(TypeBuilder<A2, cross>::get(Context));
00377     params.push_back(TypeBuilder<A3, cross>::get(Context));
00378     params.push_back(TypeBuilder<A4, cross>::get(Context));
00379     params.push_back(TypeBuilder<A5, cross>::get(Context));
00380     return FunctionType::get(TypeBuilder<R, cross>::get(Context),
00381                                    params, true);
00382   }
00383 };
00384 
00385 }  // namespace llvm
00386 
00387 #endif