llvm.org GIT mirror llvm / bb16ea9
Merge 81316 from mainline. Make TypeBuilder's result depend on the LLVMContext it's passed. TypeBuilder was using a local static variable to cache its result. This made it ignore changes in its LLVMContext argument and always return a type constructed from the argument to the first call. git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_26@81694 91177308-0d34-0410-b5e6-96231b3b80d8 Tanya Lattner 10 years ago
2 changed file(s) with 24 addition(s) and 97 deletion(s). Raw diff Collapse all Expand all
4949 /// namespace llvm {
5050 /// template class TypeBuilder {
5151 /// public:
52 /// static const StructType *get() {
53 /// // Using the static result variable ensures that the type is
54 /// // only looked up once.
55 /// static const StructType *const result = StructType::get(
56 /// TypeBuilder, xcompile>::get(),
57 /// TypeBuilder*, xcompile>::get(),
58 /// TypeBuilder*[], xcompile>::get(),
52 /// static const StructType *get(LLVMContext &Context) {
53 /// // If you cache this result, be sure to cache it separately
54 /// // for each LLVMContext.
55 /// return StructType::get(
56 /// TypeBuilder, xcompile>::get(Context),
57 /// TypeBuilder*, xcompile>::get(Context),
58 /// TypeBuilder*[], xcompile>::get(Context),
5959 /// NULL);
60 /// return result;
6160 /// }
6261 ///
6362 /// // You may find this a convenient place to put some constants
7170 /// }
7271 /// } // namespace llvm
7372 ///
74 /// Using the static result variable ensures that the type is only looked up
75 /// once.
76 ///
7773 /// TypeBuilder cannot handle recursive types or types you only know at runtime.
7874 /// If you try to give it a recursive type, it will deadlock, infinitely
7975 /// recurse, or throw a recursive_init exception.
105101 template class TypeBuilder {
106102 public:
107103 static const PointerType *get(LLVMContext &Context) {
108 static const PointerType *const result =
109 PointerType::getUnqual(TypeBuilder::get(Context));
110 return result;
104 return PointerType::getUnqual(TypeBuilder::get(Context));
111105 }
112106 };
113107
118112 template class TypeBuilder {
119113 public:
120114 static const ArrayType *get(LLVMContext &Context) {
121 static const ArrayType *const result =
122 ArrayType::get(TypeBuilder::get(Context), N);
123 return result;
115 return ArrayType::get(TypeBuilder::get(Context), N);
124116 }
125117 };
126118 /// LLVM uses an array of length 0 to represent an unknown-length array.
127119 template class TypeBuilder {
128120 public:
129121 static const ArrayType *get(LLVMContext &Context) {
130 static const ArrayType *const result =
131 ArrayType::get(TypeBuilder::get(Context), 0);
132 return result;
122 return ArrayType::get(TypeBuilder::get(Context), 0);
133123 }
134124 };
135125
159149 template<> class TypeBuilder { \
160150 public: \
161151 static const IntegerType *get(LLVMContext &Context) { \
162 static const IntegerType *const result = \
163 IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
164 return result; \
152 return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
165153 } \
166154 }; \
167155 template<> class TypeBuilder { \
190178 class TypeBuilder, cross> {
191179 public:
192180 static const IntegerType *get(LLVMContext &C) {
193 static const IntegerType *const result = IntegerType::get(C, num_bits);
194 return result;
181 return IntegerType::get(C, num_bits);
195182 }
196183 };
197184
247234 template class TypeBuilder {
248235 public:
249236 static const FunctionType *get(LLVMContext &Context) {
250 static const FunctionType *const result = create(Context);
251 return result;
252 }
253
254 private:
255 static const FunctionType *create(LLVMContext &Context) {
256237 return FunctionType::get(TypeBuilder::get(Context), false);
257238 }
258239 };
259240 template class TypeBuilder {
260241 public:
261242 static const FunctionType *get(LLVMContext &Context) {
262 static const FunctionType *const result = create(Context);
263 return result;
264 }
265
266 private:
267 static const FunctionType *create(LLVMContext &Context) {
268243 std::vector params;
269244 params.reserve(1);
270245 params.push_back(TypeBuilder::get(Context));
276251 class TypeBuilder {
277252 public:
278253 static const FunctionType *get(LLVMContext &Context) {
279 static const FunctionType *const result = create(Context);
280 return result;
281 }
282
283 private:
284 static const FunctionType *create(LLVMContext &Context) {
285254 std::vector params;
286255 params.reserve(2);
287256 params.push_back(TypeBuilder::get(Context));
294263 class TypeBuilder {
295264 public:
296265 static const FunctionType *get(LLVMContext &Context) {
297 static const FunctionType *const result = create(Context);
298 return result;
299 }
300
301 private:
302 static const FunctionType *create(LLVMContext &Context) {
303266 std::vector params;
304267 params.reserve(3);
305268 params.push_back(TypeBuilder::get(Context));
315278 class TypeBuilder {
316279 public:
317280 static const FunctionType *get(LLVMContext &Context) {
318 static const FunctionType *const result = create(Context);
319 return result;
320 }
321
322 private:
323 static const FunctionType *create(LLVMContext &Context) {
324281 std::vector params;
325282 params.reserve(4);
326283 params.push_back(TypeBuilder::get(Context));
337294 class TypeBuilder {
338295 public:
339296 static const FunctionType *get(LLVMContext &Context) {
340 static const FunctionType *const result = create(Context);
341 return result;
342 }
343
344 private:
345 static const FunctionType *create(LLVMContext &Context) {
346297 std::vector params;
347298 params.reserve(5);
348299 params.push_back(TypeBuilder::get(Context));
358309 template class TypeBuilder {
359310 public:
360311 static const FunctionType *get(LLVMContext &Context) {
361 static const FunctionType *const result = create(Context);
362 return result;
363 }
364
365 private:
366 static const FunctionType *create(LLVMContext &Context) {
367312 return FunctionType::get(TypeBuilder::get(Context), true);
368313 }
369314 };
371316 class TypeBuilder {
372317 public:
373318 static const FunctionType *get(LLVMContext &Context) {
374 static const FunctionType *const result = create(Context);
375 return result;
376 }
377
378 private:
379 static const FunctionType *create(LLVMContext &Context) {
380319 std::vector params;
381320 params.reserve(1);
382321 params.push_back(TypeBuilder::get(Context));
387326 class TypeBuilder {
388327 public:
389328 static const FunctionType *get(LLVMContext &Context) {
390 static const FunctionType *const result = create(Context);
391 return result;
392 }
393
394 private:
395 static const FunctionType *create(LLVMContext &Context) {
396329 std::vector params;
397330 params.reserve(2);
398331 params.push_back(TypeBuilder::get(Context));
405338 class TypeBuilder {
406339 public:
407340 static const FunctionType *get(LLVMContext &Context) {
408 static const FunctionType *const result = create(Context);
409 return result;
410 }
411
412 private:
413 static const FunctionType *create(LLVMContext &Context) {
414341 std::vector params;
415342 params.reserve(3);
416343 params.push_back(TypeBuilder::get(Context));
426353 class TypeBuilder {
427354 public:
428355 static const FunctionType *get(LLVMContext &Context) {
429 static const FunctionType *const result = create(Context);
430 return result;
431 }
432
433 private:
434 static const FunctionType *create(LLVMContext &Context) {
435356 std::vector params;
436357 params.reserve(4);
437358 params.push_back(TypeBuilder::get(Context));
448369 class TypeBuilder {
449370 public:
450371 static const FunctionType *get(LLVMContext &Context) {
451 static const FunctionType *const result = create(Context);
452 return result;
453 }
454
455 private:
456 static const FunctionType *create(LLVMContext &Context) {
457372 std::vector params;
458373 params.reserve(5);
459374 params.push_back(TypeBuilder::get(Context));
144144 EXPECT_EQ(FunctionType::get(Type::getInt8Ty(getGlobalContext()), params, true),
145145 (TypeBuilder
146146 false>::get(getGlobalContext())));
147 }
148
149 TEST(TypeBuilderTest, Context) {
150 // We used to cache TypeBuilder results in static local variables. This
151 // produced the same type for different contexts, which of course broke
152 // things.
153 LLVMContext context1;
154 EXPECT_EQ(&context1,
155 &(TypeBuilder, true>::get(context1))->getContext());
156 LLVMContext context2;
157 EXPECT_EQ(&context2,
158 &(TypeBuilder, true>::get(context2))->getContext());
147159 }
148160
149161 class MyType {