Move getSize() out of type, into ASTContext, where it has target info, and

where ASTContext can manage caches for struct layout, etc.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@39835 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2007-07-13 22:13:22 +00:00
Родитель 7a543ad55a
Коммит a7674d8a9a
7 изменённых файлов: 62 добавлений и 53 удалений

Просмотреть файл

@ -137,6 +137,59 @@ void ASTContext::InitBuiltinTypes() {
LongDoubleComplexTy = getComplexType(LongDoubleTy);
}
/// getTypeSize - Return the size of the specified type, in bits. This method
/// does not work on incomplete types.
unsigned ASTContext::getTypeSize(QualType T) {
T = T.getCanonicalType();
switch (T->getTypeClass()) {
case Type::Builtin: {
// FIXME: need to use TargetInfo to derive the target specific sizes. This
// implementation will suffice for play with vector support.
switch (cast<BuiltinType>(T)->getKind()) {
case BuiltinType::Void: return 0;
case BuiltinType::Bool:
case BuiltinType::Char_S:
case BuiltinType::Char_U: return sizeof(char) * 8;
case BuiltinType::SChar: return sizeof(signed char) * 8;
case BuiltinType::Short: return sizeof(short) * 8;
case BuiltinType::Int: return sizeof(int) * 8;
case BuiltinType::Long: return sizeof(long) * 8;
case BuiltinType::LongLong: return sizeof(long long) * 8;
case BuiltinType::UChar: return sizeof(unsigned char) * 8;
case BuiltinType::UShort: return sizeof(unsigned short) * 8;
case BuiltinType::UInt: return sizeof(unsigned int) * 8;
case BuiltinType::ULong: return sizeof(unsigned long) * 8;
case BuiltinType::ULongLong: return sizeof(unsigned long long) * 8;
case BuiltinType::Float: return sizeof(float) * 8;
case BuiltinType::Double: return sizeof(double) * 8;
case BuiltinType::LongDouble: return sizeof(long double) * 8;
}
assert(0 && "Can't get here");
}
case Type::Pointer:
// FIXME: need to use TargetInfo again
return sizeof(void *) * 8;
case Type::Reference:
// seems that sizeof(T&) == sizeof(T) -- spec reference?
return getTypeSize(cast<ReferenceType>(T)->getReferenceeType());
case Type::Complex:
case Type::Array:
case Type::Vector:
case Type::FunctionNoProto:
case Type::FunctionProto:
case Type::TypeName:
case Type::Tagged:
assert(0 && "Type sizes are not yet known, in general");
}
assert(0 && "Can't get here");
}
//===----------------------------------------------------------------------===//
// Type creation/memoization methods
//===----------------------------------------------------------------------===//
/// getComplexType - Return the uniqued reference to the type for a complex
/// number with the specified element type.
QualType ASTContext::getComplexType(QualType T) {

Просмотреть файл

@ -22,52 +22,6 @@ using namespace clang;
Type::~Type() {}
/// getSize - the number of bits to represent the type.
unsigned Type::getSize() const
{
switch (CanonicalType->getTypeClass()) {
case Builtin: {
// FIXME: need to use TargetInfo to derive the target specific sizes. This
// implementation will suffice for play with vector support.
switch (cast<BuiltinType>(this)->getKind()) {
case BuiltinType::Void: return 0;
case BuiltinType::Bool:
case BuiltinType::Char_S:
case BuiltinType::Char_U: return sizeof(char) * 8;
case BuiltinType::SChar: return sizeof(signed char) * 8;
case BuiltinType::Short: return sizeof(short) * 8;
case BuiltinType::Int: return sizeof(int) * 8;
case BuiltinType::Long: return sizeof(long) * 8;
case BuiltinType::LongLong: return sizeof(long long) * 8;
case BuiltinType::UChar: return sizeof(unsigned char) * 8;
case BuiltinType::UShort: return sizeof(unsigned short) * 8;
case BuiltinType::UInt: return sizeof(unsigned int) * 8;
case BuiltinType::ULong: return sizeof(unsigned long) * 8;
case BuiltinType::ULongLong: return sizeof(unsigned long long) * 8;
case BuiltinType::Float: return sizeof(float) * 8;
case BuiltinType::Double: return sizeof(double) * 8;
case BuiltinType::LongDouble: return sizeof(long double) * 8;
}
assert(0 && "Can't get here");
}
case Pointer:
// FIXME: need to use TargetInfo again
return sizeof(void *) * 8;
case Reference:
// seems that sizeof(T&) == sizeof(T) -- spec reference?
return (cast<ReferenceType>(this)->getReferenceeType()->getSize());
case Complex:
case Array:
case Vector:
case FunctionNoProto:
case FunctionProto:
case TypeName:
case Tagged:
assert(0 && "Type sizes are not yet known, in general");
}
assert(0 && "Can't get here");
}
/// isVoidType - Helper method to determine if this is the 'void' type.
bool Type::isVoidType() const {
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))

Просмотреть файл

@ -1152,7 +1152,7 @@ RValue CodeGenFunction::EmitPointerSub(RValue LHS, QualType LHSTy,
QualType LHSElementType = LHSPtrType->getPointeeType();
assert(LHSElementType == RHSPtrType->getPointeeType() &&
"can't subtract pointers with differing element types");
unsigned ElementSize = LHSElementType->getSize() / 8;
unsigned ElementSize = getContext().getTypeSize(LHSElementType) / 8;
const llvm::Type *ResultType = ConvertType(ResTy);
llvm::Value *CastLHS = Builder.CreatePtrToInt(LHSValue, ResultType,
"sub.ptr.lhs.cast");

Просмотреть файл

@ -41,6 +41,7 @@ public:
void DecodeArgumentTypes(const FunctionTypeProto &FTP,
std::vector<const llvm::Type*> &ArgTys);
};
} // end namespace CodeGen
} // end namespace clang

Просмотреть файл

@ -972,7 +972,7 @@ void Sema::HandleDeclAttributes(Decl *New, AttributeList *declspec_prefix,
}
QualType Sema::HandleVectorTypeAttribute(QualType curType,
AttributeList *rawAttr) {
AttributeList *rawAttr) {
// check the attribute arugments.
if (rawAttr->getNumArgs() != 1) {
Diag(rawAttr->getAttributeLoc(), diag::err_attribute_wrong_number_arguments,
@ -1005,8 +1005,7 @@ QualType Sema::HandleVectorTypeAttribute(QualType curType,
curType.getCanonicalType().getAsString());
return QualType();
}
BuiltinType *baseType = cast<BuiltinType>(canonType);
unsigned typeSize = baseType->getSize();
unsigned typeSize = Context.getTypeSize(curType);
// vecSize is specified in bytes - convert to bits.
unsigned vectorSize = vecSize.getZExtValue() * 8;

Просмотреть файл

@ -54,6 +54,11 @@ public:
~ASTContext();
void PrintStats() const;
/// getTypeSize - Return the size of the specified type, in bits. This method
/// does not work on incomplete types.
unsigned getTypeSize(QualType T);
//TODO: unsigned getTypeAlign(QualType T);
/// getComplexType - Return the uniqued reference to the type for a complex
/// number with the specified element type.

Просмотреть файл

@ -196,9 +196,6 @@ protected:
virtual ~Type();
friend class ASTContext;
public:
/// getSize - the number of bits to represent the type.
unsigned getSize() const;
TypeClass getTypeClass() const { return TC; }
bool isCanonical() const { return CanonicalType.getTypePtr() == this; }