From a7674d8a9a69f3f6fe16e70cf2a3b2b15fb7c43d Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 13 Jul 2007 22:13:22 +0000 Subject: [PATCH] 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 --- AST/ASTContext.cpp | 53 ++++++++++++++++++++++++++++++++++ AST/Type.cpp | 46 ----------------------------- CodeGen/CGExpr.cpp | 2 +- CodeGen/CodeGenTypes.h | 1 + Sema/SemaDecl.cpp | 5 ++-- include/clang/AST/ASTContext.h | 5 ++++ include/clang/AST/Type.h | 3 -- 7 files changed, 62 insertions(+), 53 deletions(-) diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp index 17b18d52f4..5e640b61f9 100644 --- a/AST/ASTContext.cpp +++ b/AST/ASTContext.cpp @@ -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(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(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) { diff --git a/AST/Type.cpp b/AST/Type.cpp index 6e048a4f90..a2dcac22f9 100644 --- a/AST/Type.cpp +++ b/AST/Type.cpp @@ -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(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(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(CanonicalType)) diff --git a/CodeGen/CGExpr.cpp b/CodeGen/CGExpr.cpp index 78818ce75c..98fae6d2b3 100644 --- a/CodeGen/CGExpr.cpp +++ b/CodeGen/CGExpr.cpp @@ -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"); diff --git a/CodeGen/CodeGenTypes.h b/CodeGen/CodeGenTypes.h index dd322a19ec..3d8756415b 100644 --- a/CodeGen/CodeGenTypes.h +++ b/CodeGen/CodeGenTypes.h @@ -41,6 +41,7 @@ public: void DecodeArgumentTypes(const FunctionTypeProto &FTP, std::vector &ArgTys); }; + } // end namespace CodeGen } // end namespace clang diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp index eab2bec37a..4c2c5a5e06 100644 --- a/Sema/SemaDecl.cpp +++ b/Sema/SemaDecl.cpp @@ -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(canonType); - unsigned typeSize = baseType->getSize(); + unsigned typeSize = Context.getTypeSize(curType); // vecSize is specified in bytes - convert to bits. unsigned vectorSize = vecSize.getZExtValue() * 8; diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 984b6f99be..5aa6a0401f 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -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. diff --git a/include/clang/AST/Type.h b/include/clang/AST/Type.h index e23a7bd7b7..a4f596cf3b 100644 --- a/include/clang/AST/Type.h +++ b/include/clang/AST/Type.h @@ -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; }