1 //===- llvm/unittest/Support/TypeBuilderTest.cpp - TypeBuilder tests -----===//
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 #include "llvm/Support/TypeBuilder.h"
12 #include "gtest/gtest.h"
18 TEST(TypeBuilderTest
, Void
) {
19 EXPECT_EQ(Type::VoidTy
, (TypeBuilder
<void, true>::get()));
20 EXPECT_EQ(Type::VoidTy
, (TypeBuilder
<void, false>::get()));
21 // Special case for C compatibility:
22 EXPECT_EQ(PointerType::getUnqual(Type::Int8Ty
),
23 (TypeBuilder
<void*, false>::get()));
26 TEST(TypeBuilderTest
, HostIntegers
) {
27 EXPECT_EQ(Type::Int8Ty
, (TypeBuilder
<int8_t, false>::get()));
28 EXPECT_EQ(Type::Int8Ty
, (TypeBuilder
<uint8_t, false>::get()));
29 EXPECT_EQ(Type::Int16Ty
, (TypeBuilder
<int16_t, false>::get()));
30 EXPECT_EQ(Type::Int16Ty
, (TypeBuilder
<uint16_t, false>::get()));
31 EXPECT_EQ(Type::Int32Ty
, (TypeBuilder
<int32_t, false>::get()));
32 EXPECT_EQ(Type::Int32Ty
, (TypeBuilder
<uint32_t, false>::get()));
33 EXPECT_EQ(Type::Int64Ty
, (TypeBuilder
<int64_t, false>::get()));
34 EXPECT_EQ(Type::Int64Ty
, (TypeBuilder
<uint64_t, false>::get()));
36 EXPECT_EQ(IntegerType::get(sizeof(size_t) * CHAR_BIT
),
37 (TypeBuilder
<size_t, false>::get()));
38 EXPECT_EQ(IntegerType::get(sizeof(ptrdiff_t) * CHAR_BIT
),
39 (TypeBuilder
<ptrdiff_t, false>::get()));
42 TEST(TypeBuilderTest
, CrossCompilableIntegers
) {
43 EXPECT_EQ(IntegerType::get(1), (TypeBuilder
<types::i
<1>, true>::get()));
44 EXPECT_EQ(IntegerType::get(1), (TypeBuilder
<types::i
<1>, false>::get()));
45 EXPECT_EQ(IntegerType::get(72), (TypeBuilder
<types::i
<72>, true>::get()));
46 EXPECT_EQ(IntegerType::get(72), (TypeBuilder
<types::i
<72>, false>::get()));
49 TEST(TypeBuilderTest
, Float
) {
50 EXPECT_EQ(Type::FloatTy
, (TypeBuilder
<float, false>::get()));
51 EXPECT_EQ(Type::DoubleTy
, (TypeBuilder
<double, false>::get()));
52 // long double isn't supported yet.
53 EXPECT_EQ(Type::FloatTy
, (TypeBuilder
<types::ieee_float
, true>::get()));
54 EXPECT_EQ(Type::FloatTy
, (TypeBuilder
<types::ieee_float
, false>::get()));
55 EXPECT_EQ(Type::DoubleTy
, (TypeBuilder
<types::ieee_double
, true>::get()));
56 EXPECT_EQ(Type::DoubleTy
, (TypeBuilder
<types::ieee_double
, false>::get()));
57 EXPECT_EQ(Type::X86_FP80Ty
, (TypeBuilder
<types::x86_fp80
, true>::get()));
58 EXPECT_EQ(Type::X86_FP80Ty
, (TypeBuilder
<types::x86_fp80
, false>::get()));
59 EXPECT_EQ(Type::FP128Ty
, (TypeBuilder
<types::fp128
, true>::get()));
60 EXPECT_EQ(Type::FP128Ty
, (TypeBuilder
<types::fp128
, false>::get()));
61 EXPECT_EQ(Type::PPC_FP128Ty
, (TypeBuilder
<types::ppc_fp128
, true>::get()));
62 EXPECT_EQ(Type::PPC_FP128Ty
, (TypeBuilder
<types::ppc_fp128
, false>::get()));
65 TEST(TypeBuilderTest
, Derived
) {
66 EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty
)),
67 (TypeBuilder
<int8_t**, false>::get()));
68 EXPECT_EQ(ArrayType::get(Type::Int8Ty
, 7),
69 (TypeBuilder
<int8_t[7], false>::get()));
70 EXPECT_EQ(ArrayType::get(Type::Int8Ty
, 0),
71 (TypeBuilder
<int8_t[], false>::get()));
73 EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty
)),
74 (TypeBuilder
<types::i
<8>**, false>::get()));
75 EXPECT_EQ(ArrayType::get(Type::Int8Ty
, 7),
76 (TypeBuilder
<types::i
<8>[7], false>::get()));
77 EXPECT_EQ(ArrayType::get(Type::Int8Ty
, 0),
78 (TypeBuilder
<types::i
<8>[], false>::get()));
80 EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty
)),
81 (TypeBuilder
<types::i
<8>**, true>::get()));
82 EXPECT_EQ(ArrayType::get(Type::Int8Ty
, 7),
83 (TypeBuilder
<types::i
<8>[7], true>::get()));
84 EXPECT_EQ(ArrayType::get(Type::Int8Ty
, 0),
85 (TypeBuilder
<types::i
<8>[], true>::get()));
88 EXPECT_EQ(Type::Int8Ty
,
89 (TypeBuilder
<const int8_t, false>::get()));
90 EXPECT_EQ(Type::Int8Ty
,
91 (TypeBuilder
<volatile int8_t, false>::get()));
92 EXPECT_EQ(Type::Int8Ty
,
93 (TypeBuilder
<const volatile int8_t, false>::get()));
95 EXPECT_EQ(Type::Int8Ty
,
96 (TypeBuilder
<const types::i
<8>, false>::get()));
97 EXPECT_EQ(Type::Int8Ty
,
98 (TypeBuilder
<volatile types::i
<8>, false>::get()));
99 EXPECT_EQ(Type::Int8Ty
,
100 (TypeBuilder
<const volatile types::i
<8>, false>::get()));
102 EXPECT_EQ(Type::Int8Ty
,
103 (TypeBuilder
<const types::i
<8>, true>::get()));
104 EXPECT_EQ(Type::Int8Ty
,
105 (TypeBuilder
<volatile types::i
<8>, true>::get()));
106 EXPECT_EQ(Type::Int8Ty
,
107 (TypeBuilder
<const volatile types::i
<8>, true>::get()));
109 EXPECT_EQ(PointerType::getUnqual(Type::Int8Ty
),
110 (TypeBuilder
<const volatile int8_t*const volatile, false>::get()));
113 TEST(TypeBuilderTest
, Functions
) {
114 std::vector
<const Type
*> params
;
115 EXPECT_EQ(FunctionType::get(Type::VoidTy
, params
, false),
116 (TypeBuilder
<void(), true>::get()));
117 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, true),
118 (TypeBuilder
<int8_t(...), false>::get()));
119 params
.push_back(TypeBuilder
<int32_t*, false>::get());
120 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, false),
121 (TypeBuilder
<int8_t(const int32_t*), false>::get()));
122 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, true),
123 (TypeBuilder
<int8_t(const int32_t*, ...), false>::get()));
124 params
.push_back(TypeBuilder
<char*, false>::get());
125 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, false),
126 (TypeBuilder
<int8_t(int32_t*, void*), false>::get()));
127 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, true),
128 (TypeBuilder
<int8_t(int32_t*, char*, ...), false>::get()));
129 params
.push_back(TypeBuilder
<char, false>::get());
130 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, false),
131 (TypeBuilder
<int8_t(int32_t*, void*, char), false>::get()));
132 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, true),
133 (TypeBuilder
<int8_t(int32_t*, char*, char, ...), false>::get()));
134 params
.push_back(TypeBuilder
<char, false>::get());
135 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, false),
136 (TypeBuilder
<int8_t(int32_t*, void*, char, char), false>::get()));
137 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, true),
138 (TypeBuilder
<int8_t(int32_t*, char*, char, char, ...),
140 params
.push_back(TypeBuilder
<char, false>::get());
141 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, false),
142 (TypeBuilder
<int8_t(int32_t*, void*, char, char, char),
144 EXPECT_EQ(FunctionType::get(Type::Int8Ty
, params
, true),
145 (TypeBuilder
<int8_t(int32_t*, char*, char, char, char, ...),
155 class MyPortableType
{
161 } // anonymous namespace
164 template<bool cross
> class TypeBuilder
<MyType
, cross
> {
166 static const StructType
*get() {
167 // Using the static result variable ensures that the type is
168 // only looked up once.
169 static const StructType
*const result
= StructType::get(
170 TypeBuilder
<int, cross
>::get(),
171 TypeBuilder
<int*, cross
>::get(),
172 TypeBuilder
<void*[], cross
>::get(),
177 // You may find this a convenient place to put some constants
178 // to help with getelementptr. They don't have any effect on
179 // the operation of TypeBuilder.
187 template<bool cross
> class TypeBuilder
<MyPortableType
, cross
> {
189 static const StructType
*get() {
190 // Using the static result variable ensures that the type is
191 // only looked up once.
192 static const StructType
*const result
= StructType::get(
193 TypeBuilder
<types::i
<32>, cross
>::get(),
194 TypeBuilder
<types::i
<32>*, cross
>::get(),
195 TypeBuilder
<types::i
<8>*[], cross
>::get(),
200 // You may find this a convenient place to put some constants
201 // to help with getelementptr. They don't have any effect on
202 // the operation of TypeBuilder.
212 TEST(TypeBuilderTest
, Extensions
) {
213 EXPECT_EQ(PointerType::getUnqual(StructType::get(
214 TypeBuilder
<int, false>::get(),
215 TypeBuilder
<int*, false>::get(),
216 TypeBuilder
<void*[], false>::get(),
218 (TypeBuilder
<MyType
*, false>::get()));
219 EXPECT_EQ(PointerType::getUnqual(StructType::get(
220 TypeBuilder
<types::i
<32>, false>::get(),
221 TypeBuilder
<types::i
<32>*, false>::get(),
222 TypeBuilder
<types::i
<8>*[], false>::get(),
224 (TypeBuilder
<MyPortableType
*, false>::get()));
225 EXPECT_EQ(PointerType::getUnqual(StructType::get(
226 TypeBuilder
<types::i
<32>, false>::get(),
227 TypeBuilder
<types::i
<32>*, false>::get(),
228 TypeBuilder
<types::i
<8>*[], false>::get(),
230 (TypeBuilder
<MyPortableType
*, true>::get()));
233 } // anonymous namespace