mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-02-07 14:33:15 +00:00
Add a new TypeBuilder helper class, which eases making LLVM IR types.
Patch by Jeffrey Yasskin! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70084 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
552672d285
commit
096fd7b5a7
463
include/llvm/Support/TypeBuilder.h
Normal file
463
include/llvm/Support/TypeBuilder.h
Normal file
@ -0,0 +1,463 @@
|
|||||||
|
//===---- llvm/Support/TypeBuilder.h - Builder for LLVM types ---*- C++ -*-===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// This file defines the TypeBuilder class, which is used as a convenient way to
|
||||||
|
// create LLVM types with a consistent and simplified interface.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#ifndef LLVM_SUPPORT_TYPEBUILDER_H
|
||||||
|
#define LLVM_SUPPORT_TYPEBUILDER_H
|
||||||
|
|
||||||
|
#include "llvm/DerivedTypes.h"
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
|
||||||
|
/// TypeBuilder - This provides a uniform API for looking up types
|
||||||
|
/// known at compile time. To support cross-compilation, we define a
|
||||||
|
/// series of tag types in the llvm::types namespace, like i<N>,
|
||||||
|
/// ieee_float, ppc_fp128, etc. TypeBuilder<T, false> allows T to be
|
||||||
|
/// any of these, a native C type (whose size may depend on the host
|
||||||
|
/// compiler), or a pointer, function, or struct type built out of
|
||||||
|
/// these. TypeBuilder<T, true> removes native C types from this set
|
||||||
|
/// to guarantee that its result is suitable for cross-compilation.
|
||||||
|
/// We define the primitive types, pointer types, and functions up to
|
||||||
|
/// 5 arguments here, but to use this class with your own types,
|
||||||
|
/// you'll need to specialize it. For example, say you want to call a
|
||||||
|
/// function defined externally as:
|
||||||
|
///
|
||||||
|
/// struct MyType {
|
||||||
|
/// int32 a;
|
||||||
|
/// int32 *b;
|
||||||
|
/// void *array[1]; // Intended as a flexible array.
|
||||||
|
/// };
|
||||||
|
/// int8 AFunction(struct MyType *value);
|
||||||
|
///
|
||||||
|
/// You'll want to use
|
||||||
|
/// Function::Create(TypeBuilder<types::i<8>(MyType*)>::get(), ...)
|
||||||
|
/// to declare the function, but when you first try this, your compiler will
|
||||||
|
/// complain that TypeBuilder<MyType>::get() doesn't exist. To fix this, write:
|
||||||
|
///
|
||||||
|
/// namespace llvm {
|
||||||
|
/// using types::i;
|
||||||
|
/// 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<i<32>, xcompile>::get(),
|
||||||
|
/// TypeBuilder<i<32>*, xcompile>::get(),
|
||||||
|
/// TypeBuilder<i<8>*[], xcompile>::get(),
|
||||||
|
/// NULL);
|
||||||
|
/// return result;
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// // You may find this a convenient place to put some constants
|
||||||
|
/// // to help with getelementptr. They don't have any effect on
|
||||||
|
/// // the operation of TypeBuilder.
|
||||||
|
/// enum Fields {
|
||||||
|
/// FIELD_A,
|
||||||
|
/// FIELD_B,
|
||||||
|
/// FIELD_ARRAY
|
||||||
|
/// };
|
||||||
|
/// }
|
||||||
|
/// } // 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.
|
||||||
|
template<typename T, bool cross_compilable> class TypeBuilder {};
|
||||||
|
|
||||||
|
// Types for use with cross-compilable TypeBuilders. These correspond
|
||||||
|
// exactly with an LLVM-native type.
|
||||||
|
namespace types {
|
||||||
|
/// i<N> corresponds to the LLVM IntegerType with N bits.
|
||||||
|
template<uint32_t num_bits> class i {};
|
||||||
|
|
||||||
|
// The following classes represent the LLVM floating types.
|
||||||
|
class ieee_float {};
|
||||||
|
class ieee_double {};
|
||||||
|
class x86_fp80 {};
|
||||||
|
class fp128 {};
|
||||||
|
class ppc_fp128 {};
|
||||||
|
} // namespace types
|
||||||
|
|
||||||
|
// LLVM doesn't have const or volatile types.
|
||||||
|
template<typename T, bool cross> class TypeBuilder<const T, cross>
|
||||||
|
: public TypeBuilder<T, cross> {};
|
||||||
|
template<typename T, bool cross> class TypeBuilder<volatile T, cross>
|
||||||
|
: public TypeBuilder<T, cross> {};
|
||||||
|
template<typename T, bool cross> class TypeBuilder<const volatile T, cross>
|
||||||
|
: public TypeBuilder<T, cross> {};
|
||||||
|
|
||||||
|
// Pointers
|
||||||
|
template<typename T, bool cross> class TypeBuilder<T*, cross> {
|
||||||
|
public:
|
||||||
|
static const PointerType *get() {
|
||||||
|
static const PointerType *const result =
|
||||||
|
PointerType::getUnqual(TypeBuilder<T,cross>::get());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// There is no support for references
|
||||||
|
template<typename T, bool cross> class TypeBuilder<T&, cross> {};
|
||||||
|
|
||||||
|
// Arrays
|
||||||
|
template<typename T, size_t N, bool cross> class TypeBuilder<T[N], cross> {
|
||||||
|
public:
|
||||||
|
static const ArrayType *get() {
|
||||||
|
static const ArrayType *const result =
|
||||||
|
ArrayType::get(TypeBuilder<T, cross>::get(), N);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
/// 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() {
|
||||||
|
static const ArrayType *const result =
|
||||||
|
ArrayType::get(TypeBuilder<T, cross>::get(), 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Define the C integral types only for TypeBuilder<T, false>.
|
||||||
|
//
|
||||||
|
// C integral types do not have a defined size. It would be nice to use the
|
||||||
|
// stdint.h-defined typedefs that do have defined sizes, but we'd run into the
|
||||||
|
// following problem:
|
||||||
|
//
|
||||||
|
// On an ILP32 machine, stdint.h might define:
|
||||||
|
//
|
||||||
|
// typedef int int32_t;
|
||||||
|
// typedef long long int64_t;
|
||||||
|
// typedef long size_t;
|
||||||
|
//
|
||||||
|
// If we defined TypeBuilder<int32_t> and TypeBuilder<int64_t>, then any use of
|
||||||
|
// TypeBuilder<size_t> would fail. We couldn't define TypeBuilder<size_t> in
|
||||||
|
// addition to the defined-size types because we'd get duplicate definitions on
|
||||||
|
// platforms where stdint.h instead defines:
|
||||||
|
//
|
||||||
|
// typedef int int32_t;
|
||||||
|
// typedef long long int64_t;
|
||||||
|
// typedef int size_t;
|
||||||
|
//
|
||||||
|
// So we define all the primitive C types and nothing else.
|
||||||
|
#define DEFINE_INTEGRAL_TYPEBUILDER(T) \
|
||||||
|
template<> class TypeBuilder<T, false> { \
|
||||||
|
public: \
|
||||||
|
static const IntegerType *get() { \
|
||||||
|
static const IntegerType *const result = \
|
||||||
|
IntegerType::get(sizeof(T) * CHAR_BIT); \
|
||||||
|
return result; \
|
||||||
|
} \
|
||||||
|
}; \
|
||||||
|
template<> class TypeBuilder<T, true> { \
|
||||||
|
/* We provide a definition here so users don't accidentally */ \
|
||||||
|
/* define these types to work. */ \
|
||||||
|
}
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(char);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(signed char);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(unsigned char);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(short);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(unsigned short);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(int);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(unsigned int);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(long);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(unsigned long);
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(__int64);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(unsigned __int64);
|
||||||
|
#else /* _MSC_VER */
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(long long);
|
||||||
|
DEFINE_INTEGRAL_TYPEBUILDER(unsigned long long);
|
||||||
|
#endif /* _MSC_VER */
|
||||||
|
#undef DEFINE_INTEGRAL_TYPEBUILDER
|
||||||
|
|
||||||
|
template<uint32_t num_bits, bool cross>
|
||||||
|
class TypeBuilder<types::i<num_bits>, cross> {
|
||||||
|
public:
|
||||||
|
static const IntegerType *get() {
|
||||||
|
static const IntegerType *const result = IntegerType::get(num_bits);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> class TypeBuilder<float, false> {
|
||||||
|
public:
|
||||||
|
static const Type *get() {
|
||||||
|
return Type::FloatTy;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<> class TypeBuilder<float, true> {};
|
||||||
|
|
||||||
|
template<> class TypeBuilder<double, false> {
|
||||||
|
public:
|
||||||
|
static const Type *get() {
|
||||||
|
return Type::DoubleTy;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<> class TypeBuilder<double, true> {};
|
||||||
|
|
||||||
|
template<bool cross> class TypeBuilder<types::ieee_float, cross> {
|
||||||
|
public:
|
||||||
|
static const Type *get() { return Type::FloatTy; }
|
||||||
|
};
|
||||||
|
template<bool cross> class TypeBuilder<types::ieee_double, cross> {
|
||||||
|
public:
|
||||||
|
static const Type *get() { return Type::DoubleTy; }
|
||||||
|
};
|
||||||
|
template<bool cross> class TypeBuilder<types::x86_fp80, cross> {
|
||||||
|
public:
|
||||||
|
static const Type *get() { return Type::X86_FP80Ty; }
|
||||||
|
};
|
||||||
|
template<bool cross> class TypeBuilder<types::fp128, cross> {
|
||||||
|
public:
|
||||||
|
static const Type *get() { return Type::FP128Ty; }
|
||||||
|
};
|
||||||
|
template<bool cross> class TypeBuilder<types::ppc_fp128, cross> {
|
||||||
|
public:
|
||||||
|
static const Type *get() { return Type::PPC_FP128Ty; }
|
||||||
|
};
|
||||||
|
|
||||||
|
template<bool cross> class TypeBuilder<void, cross> {
|
||||||
|
public:
|
||||||
|
static const Type *get() {
|
||||||
|
return Type::VoidTy;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/// void* is disallowed in LLVM types, but it occurs often enough in C code that
|
||||||
|
/// we special case it.
|
||||||
|
template<> class TypeBuilder<void*, false>
|
||||||
|
: public TypeBuilder<types::i<8>*, false> {};
|
||||||
|
|
||||||
|
template<typename R, bool cross> class TypeBuilder<R(), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<typename R, typename A1, bool cross> class TypeBuilder<R(A1), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(1);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<typename R, typename A1, typename A2, bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(2);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<typename R, typename A1, typename A2, typename A3, bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2, A3), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(3);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A3, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R, typename A1, typename A2, typename A3, typename A4,
|
||||||
|
bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2, A3, A4), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(4);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A3, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A4, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R, typename A1, typename A2, typename A3, typename A4,
|
||||||
|
typename A5, bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2, A3, A4, A5), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(5);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A3, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A4, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A5, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R, bool cross> class TypeBuilder<R(...), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<typename R, typename A1, bool cross>
|
||||||
|
class TypeBuilder<R(A1, ...), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(1);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<typename R, typename A1, typename A2, bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2, ...), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(2);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
template<typename R, typename A1, typename A2, typename A3, bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2, A3, ...), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(3);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A3, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R, typename A1, typename A2, typename A3, typename A4,
|
||||||
|
bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2, A3, A4, ...), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(4);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A3, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A4, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename R, typename A1, typename A2, typename A3, typename A4,
|
||||||
|
typename A5, bool cross>
|
||||||
|
class TypeBuilder<R(A1, A2, A3, A4, A5, ...), cross> {
|
||||||
|
public:
|
||||||
|
static const FunctionType *get() {
|
||||||
|
static const FunctionType *const result = create();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const FunctionType *create() {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
params.reserve(5);
|
||||||
|
params.push_back(TypeBuilder<A1, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A2, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A3, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A4, cross>::get());
|
||||||
|
params.push_back(TypeBuilder<A5, cross>::get());
|
||||||
|
return FunctionType::get(TypeBuilder<R, cross>::get(), params, true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace llvm
|
||||||
|
|
||||||
|
#endif
|
233
unittests/Support/TypeBuilderTest.cpp
Normal file
233
unittests/Support/TypeBuilderTest.cpp
Normal file
@ -0,0 +1,233 @@
|
|||||||
|
//===- llvm/unittest/Support/TypeBuilderTest.cpp - TypeBuilder tests -----===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is distributed under the University of Illinois Open Source
|
||||||
|
// License. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
#include "llvm/Support/TypeBuilder.h"
|
||||||
|
|
||||||
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
using namespace llvm;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
TEST(TypeBuilderTest, Void) {
|
||||||
|
EXPECT_EQ(Type::VoidTy, (TypeBuilder<void, true>::get()));
|
||||||
|
EXPECT_EQ(Type::VoidTy, (TypeBuilder<void, false>::get()));
|
||||||
|
// Special case for C compatibility:
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(Type::Int8Ty),
|
||||||
|
(TypeBuilder<void*, false>::get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TypeBuilderTest, HostIntegers) {
|
||||||
|
EXPECT_EQ(Type::Int8Ty, (TypeBuilder<int8_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int8Ty, (TypeBuilder<uint8_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int16Ty, (TypeBuilder<int16_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int16Ty, (TypeBuilder<uint16_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int32Ty, (TypeBuilder<int32_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int32Ty, (TypeBuilder<uint32_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int64Ty, (TypeBuilder<int64_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int64Ty, (TypeBuilder<uint64_t, false>::get()));
|
||||||
|
|
||||||
|
EXPECT_EQ(IntegerType::get(sizeof(size_t) * CHAR_BIT),
|
||||||
|
(TypeBuilder<size_t, false>::get()));
|
||||||
|
EXPECT_EQ(IntegerType::get(sizeof(ptrdiff_t) * CHAR_BIT),
|
||||||
|
(TypeBuilder<ptrdiff_t, false>::get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TypeBuilderTest, CrossCompilableIntegers) {
|
||||||
|
EXPECT_EQ(IntegerType::get(1), (TypeBuilder<types::i<1>, true>::get()));
|
||||||
|
EXPECT_EQ(IntegerType::get(1), (TypeBuilder<types::i<1>, false>::get()));
|
||||||
|
EXPECT_EQ(IntegerType::get(72), (TypeBuilder<types::i<72>, true>::get()));
|
||||||
|
EXPECT_EQ(IntegerType::get(72), (TypeBuilder<types::i<72>, false>::get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TypeBuilderTest, Float) {
|
||||||
|
EXPECT_EQ(Type::FloatTy, (TypeBuilder<float, false>::get()));
|
||||||
|
EXPECT_EQ(Type::DoubleTy, (TypeBuilder<double, false>::get()));
|
||||||
|
// long double isn't supported yet.
|
||||||
|
EXPECT_EQ(Type::FloatTy, (TypeBuilder<types::ieee_float, true>::get()));
|
||||||
|
EXPECT_EQ(Type::FloatTy, (TypeBuilder<types::ieee_float, false>::get()));
|
||||||
|
EXPECT_EQ(Type::DoubleTy, (TypeBuilder<types::ieee_double, true>::get()));
|
||||||
|
EXPECT_EQ(Type::DoubleTy, (TypeBuilder<types::ieee_double, false>::get()));
|
||||||
|
EXPECT_EQ(Type::X86_FP80Ty, (TypeBuilder<types::x86_fp80, true>::get()));
|
||||||
|
EXPECT_EQ(Type::X86_FP80Ty, (TypeBuilder<types::x86_fp80, false>::get()));
|
||||||
|
EXPECT_EQ(Type::FP128Ty, (TypeBuilder<types::fp128, true>::get()));
|
||||||
|
EXPECT_EQ(Type::FP128Ty, (TypeBuilder<types::fp128, false>::get()));
|
||||||
|
EXPECT_EQ(Type::PPC_FP128Ty, (TypeBuilder<types::ppc_fp128, true>::get()));
|
||||||
|
EXPECT_EQ(Type::PPC_FP128Ty, (TypeBuilder<types::ppc_fp128, false>::get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TypeBuilderTest, Derived) {
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)),
|
||||||
|
(TypeBuilder<int8_t**, false>::get()));
|
||||||
|
EXPECT_EQ(ArrayType::get(Type::Int8Ty, 7),
|
||||||
|
(TypeBuilder<int8_t[7], false>::get()));
|
||||||
|
EXPECT_EQ(ArrayType::get(Type::Int8Ty, 0),
|
||||||
|
(TypeBuilder<int8_t[], false>::get()));
|
||||||
|
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)),
|
||||||
|
(TypeBuilder<types::i<8>**, false>::get()));
|
||||||
|
EXPECT_EQ(ArrayType::get(Type::Int8Ty, 7),
|
||||||
|
(TypeBuilder<types::i<8>[7], false>::get()));
|
||||||
|
EXPECT_EQ(ArrayType::get(Type::Int8Ty, 0),
|
||||||
|
(TypeBuilder<types::i<8>[], false>::get()));
|
||||||
|
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)),
|
||||||
|
(TypeBuilder<types::i<8>**, true>::get()));
|
||||||
|
EXPECT_EQ(ArrayType::get(Type::Int8Ty, 7),
|
||||||
|
(TypeBuilder<types::i<8>[7], true>::get()));
|
||||||
|
EXPECT_EQ(ArrayType::get(Type::Int8Ty, 0),
|
||||||
|
(TypeBuilder<types::i<8>[], true>::get()));
|
||||||
|
|
||||||
|
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<const int8_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<volatile int8_t, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<const volatile int8_t, false>::get()));
|
||||||
|
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<const types::i<8>, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<volatile types::i<8>, false>::get()));
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<const volatile types::i<8>, false>::get()));
|
||||||
|
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<const types::i<8>, true>::get()));
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<volatile types::i<8>, true>::get()));
|
||||||
|
EXPECT_EQ(Type::Int8Ty,
|
||||||
|
(TypeBuilder<const volatile types::i<8>, true>::get()));
|
||||||
|
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(Type::Int8Ty),
|
||||||
|
(TypeBuilder<const volatile int8_t*const volatile, false>::get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(TypeBuilderTest, Functions) {
|
||||||
|
std::vector<const Type*> params;
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::VoidTy, params, false),
|
||||||
|
(TypeBuilder<void(), true>::get()));
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true),
|
||||||
|
(TypeBuilder<int8_t(...), false>::get()));
|
||||||
|
params.push_back(TypeBuilder<int32_t*, false>::get());
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false),
|
||||||
|
(TypeBuilder<int8_t(const int32_t*), false>::get()));
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true),
|
||||||
|
(TypeBuilder<int8_t(const int32_t*, ...), false>::get()));
|
||||||
|
params.push_back(TypeBuilder<char*, false>::get());
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, void*), false>::get()));
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, char*, ...), false>::get()));
|
||||||
|
params.push_back(TypeBuilder<char, false>::get());
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, void*, char), false>::get()));
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, char*, char, ...), false>::get()));
|
||||||
|
params.push_back(TypeBuilder<char, false>::get());
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, void*, char, char), false>::get()));
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, char*, char, char, ...),
|
||||||
|
false>::get()));
|
||||||
|
params.push_back(TypeBuilder<char, false>::get());
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, false),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, void*, char, char, char),
|
||||||
|
false>::get()));
|
||||||
|
EXPECT_EQ(FunctionType::get(Type::Int8Ty, params, true),
|
||||||
|
(TypeBuilder<int8_t(int32_t*, char*, char, char, char, ...),
|
||||||
|
false>::get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyType {
|
||||||
|
int a;
|
||||||
|
int *b;
|
||||||
|
void *array[1];
|
||||||
|
};
|
||||||
|
|
||||||
|
class MyPortableType {
|
||||||
|
int32_t a;
|
||||||
|
int32_t *b;
|
||||||
|
void *array[1];
|
||||||
|
};
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
|
||||||
|
namespace llvm {
|
||||||
|
template<bool cross> class TypeBuilder<MyType, cross> {
|
||||||
|
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<int, cross>::get(),
|
||||||
|
TypeBuilder<int*, cross>::get(),
|
||||||
|
TypeBuilder<void*[], cross>::get(),
|
||||||
|
NULL);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// You may find this a convenient place to put some constants
|
||||||
|
// to help with getelementptr. They don't have any effect on
|
||||||
|
// the operation of TypeBuilder.
|
||||||
|
enum Fields {
|
||||||
|
FIELD_A,
|
||||||
|
FIELD_B,
|
||||||
|
FIELD_ARRAY
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
template<bool cross> class TypeBuilder<MyPortableType, cross> {
|
||||||
|
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>, cross>::get(),
|
||||||
|
TypeBuilder<types::i<32>*, cross>::get(),
|
||||||
|
TypeBuilder<types::i<8>*[], cross>::get(),
|
||||||
|
NULL);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// You may find this a convenient place to put some constants
|
||||||
|
// to help with getelementptr. They don't have any effect on
|
||||||
|
// the operation of TypeBuilder.
|
||||||
|
enum Fields {
|
||||||
|
FIELD_A,
|
||||||
|
FIELD_B,
|
||||||
|
FIELD_ARRAY
|
||||||
|
};
|
||||||
|
};
|
||||||
|
} // namespace llvm
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
TEST(TypeBuilderTest, Extensions) {
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(StructType::get(
|
||||||
|
TypeBuilder<int, false>::get(),
|
||||||
|
TypeBuilder<int*, false>::get(),
|
||||||
|
TypeBuilder<void*[], false>::get(),
|
||||||
|
NULL)),
|
||||||
|
(TypeBuilder<MyType*, false>::get()));
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(StructType::get(
|
||||||
|
TypeBuilder<types::i<32>, false>::get(),
|
||||||
|
TypeBuilder<types::i<32>*, false>::get(),
|
||||||
|
TypeBuilder<types::i<8>*[], false>::get(),
|
||||||
|
NULL)),
|
||||||
|
(TypeBuilder<MyPortableType*, false>::get()));
|
||||||
|
EXPECT_EQ(PointerType::getUnqual(StructType::get(
|
||||||
|
TypeBuilder<types::i<32>, false>::get(),
|
||||||
|
TypeBuilder<types::i<32>*, false>::get(),
|
||||||
|
TypeBuilder<types::i<8>*[], false>::get(),
|
||||||
|
NULL)),
|
||||||
|
(TypeBuilder<MyPortableType*, true>::get()));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // anonymous namespace
|
Loading…
x
Reference in New Issue
Block a user