slightly increase prettiness.
[llvm/avr.git] / unittests / Support / TypeBuilderTest.cpp
blobfae8907cda6eb1abb5b68b06177052bc81b2982d
1 //===- llvm/unittest/Support/TypeBuilderTest.cpp - TypeBuilder tests -----===//
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 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/TypeBuilder.h"
11 #include "llvm/LLVMContext.h"
13 #include "gtest/gtest.h"
15 using namespace llvm;
17 namespace {
19 TEST(TypeBuilderTest, Void) {
20 EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder<void, true>::get(getGlobalContext())));
21 EXPECT_EQ(Type::getVoidTy(getGlobalContext()), (TypeBuilder<void, false>::get(getGlobalContext())));
22 // Special case for C compatibility:
23 EXPECT_EQ(PointerType::getUnqual(Type::getInt8Ty(getGlobalContext())),
24 (TypeBuilder<void*, false>::get(getGlobalContext())));
27 TEST(TypeBuilderTest, HostIntegers) {
28 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder<int8_t, false>::get(getGlobalContext())));
29 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()), (TypeBuilder<uint8_t, false>::get(getGlobalContext())));
30 EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder<int16_t, false>::get(getGlobalContext())));
31 EXPECT_EQ(Type::getInt16Ty(getGlobalContext()), (TypeBuilder<uint16_t, false>::get(getGlobalContext())));
32 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder<int32_t, false>::get(getGlobalContext())));
33 EXPECT_EQ(Type::getInt32Ty(getGlobalContext()), (TypeBuilder<uint32_t, false>::get(getGlobalContext())));
34 EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder<int64_t, false>::get(getGlobalContext())));
35 EXPECT_EQ(Type::getInt64Ty(getGlobalContext()), (TypeBuilder<uint64_t, false>::get(getGlobalContext())));
37 EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(size_t) * CHAR_BIT),
38 (TypeBuilder<size_t, false>::get(getGlobalContext())));
39 EXPECT_EQ(IntegerType::get(getGlobalContext(), sizeof(ptrdiff_t) * CHAR_BIT),
40 (TypeBuilder<ptrdiff_t, false>::get(getGlobalContext())));
43 TEST(TypeBuilderTest, CrossCompilableIntegers) {
44 EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder<types::i<1>, true>::get(getGlobalContext())));
45 EXPECT_EQ(IntegerType::get(getGlobalContext(), 1), (TypeBuilder<types::i<1>, false>::get(getGlobalContext())));
46 EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder<types::i<72>, true>::get(getGlobalContext())));
47 EXPECT_EQ(IntegerType::get(getGlobalContext(), 72), (TypeBuilder<types::i<72>, false>::get(getGlobalContext())));
50 TEST(TypeBuilderTest, Float) {
51 EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder<float, false>::get(getGlobalContext())));
52 EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder<double, false>::get(getGlobalContext())));
53 // long double isn't supported yet.
54 EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder<types::ieee_float, true>::get(getGlobalContext())));
55 EXPECT_EQ(Type::getFloatTy(getGlobalContext()), (TypeBuilder<types::ieee_float, false>::get(getGlobalContext())));
56 EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder<types::ieee_double, true>::get(getGlobalContext())));
57 EXPECT_EQ(Type::getDoubleTy(getGlobalContext()), (TypeBuilder<types::ieee_double, false>::get(getGlobalContext())));
58 EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder<types::x86_fp80, true>::get(getGlobalContext())));
59 EXPECT_EQ(Type::getX86_FP80Ty(getGlobalContext()), (TypeBuilder<types::x86_fp80, false>::get(getGlobalContext())));
60 EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder<types::fp128, true>::get(getGlobalContext())));
61 EXPECT_EQ(Type::getFP128Ty(getGlobalContext()), (TypeBuilder<types::fp128, false>::get(getGlobalContext())));
62 EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder<types::ppc_fp128, true>::get(getGlobalContext())));
63 EXPECT_EQ(Type::getPPC_FP128Ty(getGlobalContext()), (TypeBuilder<types::ppc_fp128, false>::get(getGlobalContext())));
66 TEST(TypeBuilderTest, Derived) {
67 EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::getInt8Ty(getGlobalContext()))),
68 (TypeBuilder<int8_t**, false>::get(getGlobalContext())));
69 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7),
70 (TypeBuilder<int8_t[7], false>::get(getGlobalContext())));
71 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0),
72 (TypeBuilder<int8_t[], false>::get(getGlobalContext())));
74 EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::getInt8Ty(getGlobalContext()))),
75 (TypeBuilder<types::i<8>**, false>::get(getGlobalContext())));
76 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7),
77 (TypeBuilder<types::i<8>[7], false>::get(getGlobalContext())));
78 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0),
79 (TypeBuilder<types::i<8>[], false>::get(getGlobalContext())));
81 EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::getInt8Ty(getGlobalContext()))),
82 (TypeBuilder<types::i<8>**, true>::get(getGlobalContext())));
83 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 7),
84 (TypeBuilder<types::i<8>[7], true>::get(getGlobalContext())));
85 EXPECT_EQ(ArrayType::get(Type::getInt8Ty(getGlobalContext()), 0),
86 (TypeBuilder<types::i<8>[], true>::get(getGlobalContext())));
89 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
90 (TypeBuilder<const int8_t, false>::get(getGlobalContext())));
91 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
92 (TypeBuilder<volatile int8_t, false>::get(getGlobalContext())));
93 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
94 (TypeBuilder<const volatile int8_t, false>::get(getGlobalContext())));
96 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
97 (TypeBuilder<const types::i<8>, false>::get(getGlobalContext())));
98 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
99 (TypeBuilder<volatile types::i<8>, false>::get(getGlobalContext())));
100 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
101 (TypeBuilder<const volatile types::i<8>, false>::get(getGlobalContext())));
103 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
104 (TypeBuilder<const types::i<8>, true>::get(getGlobalContext())));
105 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
106 (TypeBuilder<volatile types::i<8>, true>::get(getGlobalContext())));
107 EXPECT_EQ(Type::getInt8Ty(getGlobalContext()),
108 (TypeBuilder<const volatile types::i<8>, true>::get(getGlobalContext())));
110 EXPECT_EQ(PointerType::getUnqual(Type::getInt8Ty(getGlobalContext())),
111 (TypeBuilder<const volatile int8_t*const volatile, false>::get(getGlobalContext())));
114 TEST(TypeBuilderTest, Functions) {
115 std::vector<const Type*> params;
116 EXPECT_EQ(FunctionType::get(Type::getVoidTy(getGlobalContext()), params, false),
117 (TypeBuilder<void(), true>::get(getGlobalContext())));
118 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
119 (TypeBuilder<int8_t(...), false>::get(getGlobalContext())));
120 params.push_back(TypeBuilder<int32_t*, false>::get(getGlobalContext()));
121 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
122 (TypeBuilder<int8_t(const int32_t*), false>::get(getGlobalContext())));
123 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
124 (TypeBuilder<int8_t(const int32_t*, ...), false>::get(getGlobalContext())));
125 params.push_back(TypeBuilder<char*, false>::get(getGlobalContext()));
126 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
127 (TypeBuilder<int8_t(int32_t*, void*), false>::get(getGlobalContext())));
128 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
129 (TypeBuilder<int8_t(int32_t*, char*, ...), false>::get(getGlobalContext())));
130 params.push_back(TypeBuilder<char, false>::get(getGlobalContext()));
131 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
132 (TypeBuilder<int8_t(int32_t*, void*, char), false>::get(getGlobalContext())));
133 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
134 (TypeBuilder<int8_t(int32_t*, char*, char, ...), false>::get(getGlobalContext())));
135 params.push_back(TypeBuilder<char, false>::get(getGlobalContext()));
136 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
137 (TypeBuilder<int8_t(int32_t*, void*, char, char), false>::get(getGlobalContext())));
138 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
139 (TypeBuilder<int8_t(int32_t*, char*, char, char, ...),
140 false>::get(getGlobalContext())));
141 params.push_back(TypeBuilder<char, false>::get(getGlobalContext()));
142 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, false),
143 (TypeBuilder<int8_t(int32_t*, void*, char, char, char),
144 false>::get(getGlobalContext())));
145 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
146 (TypeBuilder<int8_t(int32_t*, char*, char, char, char, ...),
147 false>::get(getGlobalContext())));
150 TEST(TypeBuilderTest, Context) {
151 // We used to cache TypeBuilder results in static local variables. This
152 // produced the same type for different contexts, which of course broke
153 // things.
154 LLVMContext context1;
155 EXPECT_EQ(&context1,
156 &(TypeBuilder<types::i<1>, true>::get(context1))->getContext());
157 LLVMContext context2;
158 EXPECT_EQ(&context2,
159 &(TypeBuilder<types::i<1>, true>::get(context2))->getContext());
162 class MyType {
163 int a;
164 int *b;
165 void *array[1];
168 class MyPortableType {
169 int32_t a;
170 int32_t *b;
171 void *array[1];
174 } // anonymous namespace
176 namespace llvm {
177 template<bool cross> class TypeBuilder<MyType, cross> {
178 public:
179 static const StructType *get(LLVMContext &Context) {
180 // Using the static result variable ensures that the type is
181 // only looked up once.
182 std::vector<const Type*> st;
183 st.push_back(TypeBuilder<int, cross>::get(Context));
184 st.push_back(TypeBuilder<int*, cross>::get(Context));
185 st.push_back(TypeBuilder<void*[], cross>::get(Context));
186 static const StructType *const result = StructType::get(Context, st);
187 return result;
190 // You may find this a convenient place to put some constants
191 // to help with getelementptr. They don't have any effect on
192 // the operation of TypeBuilder.
193 enum Fields {
194 FIELD_A,
195 FIELD_B,
196 FIELD_ARRAY
200 template<bool cross> class TypeBuilder<MyPortableType, cross> {
201 public:
202 static const StructType *get(LLVMContext &Context) {
203 // Using the static result variable ensures that the type is
204 // only looked up once.
205 std::vector<const Type*> st;
206 st.push_back(TypeBuilder<types::i<32>, cross>::get(Context));
207 st.push_back(TypeBuilder<types::i<32>*, cross>::get(Context));
208 st.push_back(TypeBuilder<types::i<8>*[], cross>::get(Context));
209 static const StructType *const result = StructType::get(Context, st);
210 return result;
213 // You may find this a convenient place to put some constants
214 // to help with getelementptr. They don't have any effect on
215 // the operation of TypeBuilder.
216 enum Fields {
217 FIELD_A,
218 FIELD_B,
219 FIELD_ARRAY
222 } // namespace llvm
223 namespace {
225 TEST(TypeBuilderTest, Extensions) {
226 EXPECT_EQ(PointerType::getUnqual(StructType::get(getGlobalContext(),
227 TypeBuilder<int, false>::get(getGlobalContext()),
228 TypeBuilder<int*, false>::get(getGlobalContext()),
229 TypeBuilder<void*[], false>::get(getGlobalContext()),
230 NULL)),
231 (TypeBuilder<MyType*, false>::get(getGlobalContext())));
232 EXPECT_EQ(PointerType::getUnqual(StructType::get(getGlobalContext(),
233 TypeBuilder<types::i<32>, false>::get(getGlobalContext()),
234 TypeBuilder<types::i<32>*, false>::get(getGlobalContext()),
235 TypeBuilder<types::i<8>*[], false>::get(getGlobalContext()),
236 NULL)),
237 (TypeBuilder<MyPortableType*, false>::get(getGlobalContext())));
238 EXPECT_EQ(PointerType::getUnqual(StructType::get(getGlobalContext(),
239 TypeBuilder<types::i<32>, false>::get(getGlobalContext()),
240 TypeBuilder<types::i<32>*, false>::get(getGlobalContext()),
241 TypeBuilder<types::i<8>*[], false>::get(getGlobalContext()),
242 NULL)),
243 (TypeBuilder<MyPortableType*, true>::get(getGlobalContext())));
246 } // anonymous namespace