TypeBuilder.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. #ifndef LLVM_IR_TYPEBUILDER_H
  15. #define LLVM_IR_TYPEBUILDER_H
  16. #include "llvm/IR/DerivedTypes.h"
  17. #include "llvm/IR/LLVMContext.h"
  18. #include <climits>
  19. namespace llvm {
  20. /// TypeBuilder - This provides a uniform API for looking up types
  21. /// known at compile time. To support cross-compilation, we define a
  22. /// series of tag types in the llvm::types namespace, like i<N>,
  23. /// ieee_float, ppc_fp128, etc. TypeBuilder<T, false> allows T to be
  24. /// any of these, a native C type (whose size may depend on the host
  25. /// compiler), or a pointer, function, or struct type built out of
  26. /// these. TypeBuilder<T, true> removes native C types from this set
  27. /// to guarantee that its result is suitable for cross-compilation.
  28. /// We define the primitive types, pointer types, and functions up to
  29. /// 5 arguments here, but to use this class with your own types,
  30. /// you'll need to specialize it. For example, say you want to call a
  31. /// function defined externally as:
  32. ///
  33. /// \code{.cpp}
  34. ///
  35. /// struct MyType {
  36. /// int32 a;
  37. /// int32 *b;
  38. /// void *array[1]; // Intended as a flexible array.
  39. /// };
  40. /// int8 AFunction(struct MyType *value);
  41. ///
  42. /// \endcode
  43. ///
  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,
  48. /// write:
  49. ///
  50. /// \code{.cpp}
  51. ///
  52. /// namespace llvm {
  53. /// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
  54. /// public:
  55. /// static StructType *get(LLVMContext &Context) {
  56. /// // If you cache this result, be sure to cache it separately
  57. /// // for each LLVMContext.
  58. /// return StructType::get(
  59. /// TypeBuilder<types::i<32>, xcompile>::get(Context),
  60. /// TypeBuilder<types::i<32>*, xcompile>::get(Context),
  61. /// TypeBuilder<types::i<8>*[], xcompile>::get(Context),
  62. /// nullptr);
  63. /// }
  64. ///
  65. /// // You may find this a convenient place to put some constants
  66. /// // to help with getelementptr. They don't have any effect on
  67. /// // the operation of TypeBuilder.
  68. /// enum Fields {
  69. /// FIELD_A,
  70. /// FIELD_B,
  71. /// FIELD_ARRAY
  72. /// };
  73. /// }
  74. /// } // namespace llvm
  75. ///
  76. /// \endcode
  77. ///
  78. /// TypeBuilder cannot handle recursive types or types you only know at runtime.
  79. /// If you try to give it a recursive type, it will deadlock, infinitely
  80. /// recurse, or do something similarly undesirable.
  81. template<typename T, bool cross_compilable> class TypeBuilder {};
  82. // Types for use with cross-compilable TypeBuilders. These correspond
  83. // exactly with an LLVM-native type.
  84. namespace types {
  85. /// i<N> corresponds to the LLVM IntegerType with N bits.
  86. template<uint32_t num_bits> class i {};
  87. // The following classes represent the LLVM floating types.
  88. class ieee_float {};
  89. class ieee_double {};
  90. class x86_fp80 {};
  91. class fp128 {};
  92. class ppc_fp128 {};
  93. // X86 MMX.
  94. class x86_mmx {};
  95. } // namespace types
  96. // LLVM doesn't have const or volatile types.
  97. template<typename T, bool cross> class TypeBuilder<const T, cross>
  98. : public TypeBuilder<T, cross> {};
  99. template<typename T, bool cross> class TypeBuilder<volatile T, cross>
  100. : public TypeBuilder<T, cross> {};
  101. template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
  102. : public TypeBuilder<T, cross> {};
  103. // Pointers
  104. template<typename T, bool cross> class TypeBuilder<T*, cross> {
  105. public:
  106. static PointerType *get(LLVMContext &Context) {
  107. return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
  108. }
  109. };
  110. /// There is no support for references
  111. template<typename T, bool cross> class TypeBuilder<T&, cross> {};
  112. // Arrays
  113. template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
  114. public:
  115. static ArrayType *get(LLVMContext &Context) {
  116. return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
  117. }
  118. };
  119. /// LLVM uses an array of length 0 to represent an unknown-length array.
  120. template<typename T, bool cross> class TypeBuilder<T[], cross> {
  121. public:
  122. static ArrayType *get(LLVMContext &Context) {
  123. return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
  124. }
  125. };
  126. // Define the C integral types only for TypeBuilder<T, false>.
  127. //
  128. // C integral types do not have a defined size. It would be nice to use the
  129. // stdint.h-defined typedefs that do have defined sizes, but we'd run into the
  130. // following problem:
  131. //
  132. // On an ILP32 machine, stdint.h might define:
  133. //
  134. // typedef int int32_t;
  135. // typedef long long int64_t;
  136. // typedef long size_t;
  137. //
  138. // If we defined TypeBuilder<int32_t> and TypeBuilder<int64_t>, then any use of
  139. // TypeBuilder<size_t> would fail. We couldn't define TypeBuilder<size_t> in
  140. // addition to the defined-size types because we'd get duplicate definitions on
  141. // platforms where stdint.h instead defines:
  142. //
  143. // typedef int int32_t;
  144. // typedef long long int64_t;
  145. // typedef int size_t;
  146. //
  147. // So we define all the primitive C types and nothing else.
  148. #define DEFINE_INTEGRAL_TYPEBUILDER(T) \
  149. template<> class TypeBuilder<T, false> { \
  150. public: \
  151. static IntegerType *get(LLVMContext &Context) { \
  152. return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
  153. } \
  154. }; \
  155. template<> class TypeBuilder<T, true> { \
  156. /* We provide a definition here so users don't accidentally */ \
  157. /* define these types to work. */ \
  158. }
  159. DEFINE_INTEGRAL_TYPEBUILDER(char);
  160. DEFINE_INTEGRAL_TYPEBUILDER(signed char);
  161. DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
  162. DEFINE_INTEGRAL_TYPEBUILDER(short);
  163. DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
  164. DEFINE_INTEGRAL_TYPEBUILDER(int);
  165. DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
  166. DEFINE_INTEGRAL_TYPEBUILDER(long);
  167. DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
  168. #ifdef _MSC_VER
  169. DEFINE_INTEGRAL_TYPEBUILDER(__int64);
  170. DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
  171. #else /* _MSC_VER */
  172. DEFINE_INTEGRAL_TYPEBUILDER(long long);
  173. DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
  174. #endif /* _MSC_VER */
  175. #undef DEFINE_INTEGRAL_TYPEBUILDER
  176. template<uint32_t num_bits, bool cross>
  177. class TypeBuilder<types::i<num_bits>, cross> {
  178. public:
  179. static IntegerType *get(LLVMContext &C) {
  180. return IntegerType::get(C, num_bits);
  181. }
  182. };
  183. template<> class TypeBuilder<float, false> {
  184. public:
  185. static Type *get(LLVMContext& C) {
  186. return Type::getFloatTy(C);
  187. }
  188. };
  189. template<> class TypeBuilder<float, true> {};
  190. template<> class TypeBuilder<double, false> {
  191. public:
  192. static Type *get(LLVMContext& C) {
  193. return Type::getDoubleTy(C);
  194. }
  195. };
  196. template<> class TypeBuilder<double, true> {};
  197. template<bool cross> class TypeBuilder<types::ieee_float, cross> {
  198. public:
  199. static Type *get(LLVMContext& C) { return Type::getFloatTy(C); }
  200. };
  201. template<bool cross> class TypeBuilder<types::ieee_double, cross> {
  202. public:
  203. static Type *get(LLVMContext& C) { return Type::getDoubleTy(C); }
  204. };
  205. template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
  206. public:
  207. static Type *get(LLVMContext& C) { return Type::getX86_FP80Ty(C); }
  208. };
  209. template<bool cross> class TypeBuilder<types::fp128, cross> {
  210. public:
  211. static Type *get(LLVMContext& C) { return Type::getFP128Ty(C); }
  212. };
  213. template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
  214. public:
  215. static Type *get(LLVMContext& C) { return Type::getPPC_FP128Ty(C); }
  216. };
  217. template<bool cross> class TypeBuilder<types::x86_mmx, cross> {
  218. public:
  219. static Type *get(LLVMContext& C) { return Type::getX86_MMXTy(C); }
  220. };
  221. template<bool cross> class TypeBuilder<void, cross> {
  222. public:
  223. static Type *get(LLVMContext &C) {
  224. return Type::getVoidTy(C);
  225. }
  226. };
  227. /// void* is disallowed in LLVM types, but it occurs often enough in C code that
  228. /// we special case it.
  229. template<> class TypeBuilder<void*, false>
  230. : public TypeBuilder<types::i<8>*, false> {};
  231. template<> class TypeBuilder<const void*, false>
  232. : public TypeBuilder<types::i<8>*, false> {};
  233. template<> class TypeBuilder<volatile void*, false>
  234. : public TypeBuilder<types::i<8>*, false> {};
  235. template<> class TypeBuilder<const volatile void*, false>
  236. : public TypeBuilder<types::i<8>*, false> {};
  237. template<typename R, bool cross> class TypeBuilder<R(), cross> {
  238. public:
  239. static FunctionType *get(LLVMContext &Context) {
  240. return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
  241. }
  242. };
  243. template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
  244. public:
  245. static FunctionType *get(LLVMContext &Context) {
  246. Type *params[] = {
  247. TypeBuilder<A1, cross>::get(Context),
  248. };
  249. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  250. params, false);
  251. }
  252. };
  253. template<typename R, typename A1, typename A2, bool cross>
  254. class TypeBuilder<R(A1, A2), cross> {
  255. public:
  256. static FunctionType *get(LLVMContext &Context) {
  257. Type *params[] = {
  258. TypeBuilder<A1, cross>::get(Context),
  259. TypeBuilder<A2, cross>::get(Context),
  260. };
  261. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  262. params, false);
  263. }
  264. };
  265. template<typename R, typename A1, typename A2, typename A3, bool cross>
  266. class TypeBuilder<R(A1, A2, A3), cross> {
  267. public:
  268. static FunctionType *get(LLVMContext &Context) {
  269. Type *params[] = {
  270. TypeBuilder<A1, cross>::get(Context),
  271. TypeBuilder<A2, cross>::get(Context),
  272. TypeBuilder<A3, cross>::get(Context),
  273. };
  274. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  275. params, false);
  276. }
  277. };
  278. template<typename R, typename A1, typename A2, typename A3, typename A4,
  279. bool cross>
  280. class TypeBuilder<R(A1, A2, A3, A4), cross> {
  281. public:
  282. static FunctionType *get(LLVMContext &Context) {
  283. Type *params[] = {
  284. TypeBuilder<A1, cross>::get(Context),
  285. TypeBuilder<A2, cross>::get(Context),
  286. TypeBuilder<A3, cross>::get(Context),
  287. TypeBuilder<A4, cross>::get(Context),
  288. };
  289. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  290. params, false);
  291. }
  292. };
  293. template<typename R, typename A1, typename A2, typename A3, typename A4,
  294. typename A5, bool cross>
  295. class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
  296. public:
  297. static FunctionType *get(LLVMContext &Context) {
  298. Type *params[] = {
  299. TypeBuilder<A1, cross>::get(Context),
  300. TypeBuilder<A2, cross>::get(Context),
  301. TypeBuilder<A3, cross>::get(Context),
  302. TypeBuilder<A4, cross>::get(Context),
  303. TypeBuilder<A5, cross>::get(Context),
  304. };
  305. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  306. params, false);
  307. }
  308. };
  309. template<typename R, bool cross> class TypeBuilder<R(...), cross> {
  310. public:
  311. static FunctionType *get(LLVMContext &Context) {
  312. return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
  313. }
  314. };
  315. template<typename R, typename A1, bool cross>
  316. class TypeBuilder<R(A1, ...), cross> {
  317. public:
  318. static FunctionType *get(LLVMContext &Context) {
  319. Type *params[] = {
  320. TypeBuilder<A1, cross>::get(Context),
  321. };
  322. return FunctionType::get(TypeBuilder<R, cross>::get(Context), params, true);
  323. }
  324. };
  325. template<typename R, typename A1, typename A2, bool cross>
  326. class TypeBuilder<R(A1, A2, ...), cross> {
  327. public:
  328. static FunctionType *get(LLVMContext &Context) {
  329. Type *params[] = {
  330. TypeBuilder<A1, cross>::get(Context),
  331. TypeBuilder<A2, cross>::get(Context),
  332. };
  333. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  334. params, true);
  335. }
  336. };
  337. template<typename R, typename A1, typename A2, typename A3, bool cross>
  338. class TypeBuilder<R(A1, A2, A3, ...), cross> {
  339. public:
  340. static FunctionType *get(LLVMContext &Context) {
  341. Type *params[] = {
  342. TypeBuilder<A1, cross>::get(Context),
  343. TypeBuilder<A2, cross>::get(Context),
  344. TypeBuilder<A3, cross>::get(Context),
  345. };
  346. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  347. params, true);
  348. }
  349. };
  350. template<typename R, typename A1, typename A2, typename A3, typename A4,
  351. bool cross>
  352. class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
  353. public:
  354. static FunctionType *get(LLVMContext &Context) {
  355. Type *params[] = {
  356. TypeBuilder<A1, cross>::get(Context),
  357. TypeBuilder<A2, cross>::get(Context),
  358. TypeBuilder<A3, cross>::get(Context),
  359. TypeBuilder<A4, cross>::get(Context),
  360. };
  361. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  362. params, true);
  363. }
  364. };
  365. template<typename R, typename A1, typename A2, typename A3, typename A4,
  366. typename A5, bool cross>
  367. class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
  368. public:
  369. static FunctionType *get(LLVMContext &Context) {
  370. Type *params[] = {
  371. TypeBuilder<A1, cross>::get(Context),
  372. TypeBuilder<A2, cross>::get(Context),
  373. TypeBuilder<A3, cross>::get(Context),
  374. TypeBuilder<A4, cross>::get(Context),
  375. TypeBuilder<A5, cross>::get(Context),
  376. };
  377. return FunctionType::get(TypeBuilder<R, cross>::get(Context),
  378. params, true);
  379. }
  380. };
  381. } // namespace llvm
  382. #endif