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/trunk@81316
91177308-0d34-0410-b5e6-
96231b3b80d8
/// namespace llvm {
/// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
/// public:
/// namespace llvm {
/// template<bool xcompile> class TypeBuilder<MyType, xcompile> {
/// public:
-/// static const StructType *get() {
-/// // Using the static result variable ensures that the type is
-/// // only looked up once.
-/// static const StructType *const result = StructType::get(
-/// TypeBuilder<types::i<32>, xcompile>::get(),
-/// TypeBuilder<types::i<32>*, xcompile>::get(),
-/// TypeBuilder<types::i<8>*[], xcompile>::get(),
+/// static const StructType *get(LLVMContext &Context) {
+/// // If you cache this result, be sure to cache it separately
+/// // for each LLVMContext.
+/// return StructType::get(
+/// TypeBuilder<types::i<32>, xcompile>::get(Context),
+/// TypeBuilder<types::i<32>*, xcompile>::get(Context),
+/// TypeBuilder<types::i<8>*[], xcompile>::get(Context),
/// }
///
/// // You may find this a convenient place to put some constants
/// }
///
/// // You may find this a convenient place to put some constants
/// }
/// } // namespace llvm
///
/// }
/// } // namespace llvm
///
-/// Using the static result variable ensures that the type is only looked up
-/// once.
-///
/// TypeBuilder cannot handle recursive types or types you only know at runtime.
/// If you try to give it a recursive type, it will deadlock, infinitely
/// recurse, or throw a recursive_init exception.
/// TypeBuilder cannot handle recursive types or types you only know at runtime.
/// If you try to give it a recursive type, it will deadlock, infinitely
/// recurse, or throw a recursive_init exception.
template<typename T, bool cross> class TypeBuilder<T*, cross> {
public:
static const PointerType *get(LLVMContext &Context) {
template<typename T, bool cross> class TypeBuilder<T*, cross> {
public:
static const PointerType *get(LLVMContext &Context) {
- static const PointerType *const result =
- PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
- return result;
+ return PointerType::getUnqual(TypeBuilder<T,cross>::get(Context));
template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
public:
static const ArrayType *get(LLVMContext &Context) {
template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
public:
static const ArrayType *get(LLVMContext &Context) {
- static const ArrayType *const result =
- ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
- return result;
+ return ArrayType::get(TypeBuilder<T, cross>::get(Context), N);
}
};
/// LLVM uses an array of length 0 to represent an unknown-length array.
template<typename T, bool cross> class TypeBuilder<T[], cross> {
public:
static const ArrayType *get(LLVMContext &Context) {
}
};
/// LLVM uses an array of length 0 to represent an unknown-length array.
template<typename T, bool cross> class TypeBuilder<T[], cross> {
public:
static const ArrayType *get(LLVMContext &Context) {
- static const ArrayType *const result =
- ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
- return result;
+ return ArrayType::get(TypeBuilder<T, cross>::get(Context), 0);
template<> class TypeBuilder<T, false> { \
public: \
static const IntegerType *get(LLVMContext &Context) { \
template<> class TypeBuilder<T, false> { \
public: \
static const IntegerType *get(LLVMContext &Context) { \
- static const IntegerType *const result = \
- IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
- return result; \
+ return IntegerType::get(Context, sizeof(T) * CHAR_BIT); \
} \
}; \
template<> class TypeBuilder<T, true> { \
} \
}; \
template<> class TypeBuilder<T, true> { \
class TypeBuilder<types::i<num_bits>, cross> {
public:
static const IntegerType *get(LLVMContext &C) {
class TypeBuilder<types::i<num_bits>, cross> {
public:
static const IntegerType *get(LLVMContext &C) {
- static const IntegerType *const result = IntegerType::get(C, num_bits);
- return result;
+ return IntegerType::get(C, num_bits);
template<typename R, bool cross> class TypeBuilder<R(), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
template<typename R, bool cross> class TypeBuilder<R(), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
}
};
template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
return FunctionType::get(TypeBuilder<R, cross>::get(Context), false);
}
};
template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(1);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(1);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(2);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(2);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2, A3), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2, A3), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(3);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(3);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2, A3, A4), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2, A3, A4), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(4);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(4);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(5);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(5);
params.push_back(TypeBuilder<A1, cross>::get(Context));
template<typename R, bool cross> class TypeBuilder<R(...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
template<typename R, bool cross> class TypeBuilder<R(...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
}
};
return FunctionType::get(TypeBuilder<R, cross>::get(Context), true);
}
};
class TypeBuilder<R(A1, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(1);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(1);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(2);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(2);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2, A3, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2, A3, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(3);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(3);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(4);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(4);
params.push_back(TypeBuilder<A1, cross>::get(Context));
class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
public:
static const FunctionType *get(LLVMContext &Context) {
- static const FunctionType *const result = create(Context);
- return result;
- }
-
-private:
- static const FunctionType *create(LLVMContext &Context) {
std::vector<const Type*> params;
params.reserve(5);
params.push_back(TypeBuilder<A1, cross>::get(Context));
std::vector<const Type*> params;
params.reserve(5);
params.push_back(TypeBuilder<A1, cross>::get(Context));
false>::get(getGlobalContext())));
}
false>::get(getGlobalContext())));
}
+TEST(TypeBuilderTest, Context) {
+ // We used to cache TypeBuilder results in static local variables. This
+ // produced the same type for different contexts, which of course broke
+ // things.
+ LLVMContext context1;
+ EXPECT_EQ(&context1,
+ &(TypeBuilder<types::i<1>, true>::get(context1))->getContext());
+ LLVMContext context2;
+ EXPECT_EQ(&context2,
+ &(TypeBuilder<types::i<1>, true>::get(context2))->getContext());
+}
+
class MyType {
int a;
int *b;
class MyType {
int a;
int *b;