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
diff --git a/AST/ASTContext.cpp b/AST/ASTContext.cpp
index 17b18d5..5e640b6 100644
--- a/AST/ASTContext.cpp
+++ b/AST/ASTContext.cpp
@@ -137,6 +137,59 @@
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) {
diff --git a/AST/Type.cpp b/AST/Type.cpp
index 6e048a4..a2dcac2 100644
--- a/AST/Type.cpp
+++ b/AST/Type.cpp
@@ -22,52 +22,6 @@
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))
diff --git a/CodeGen/CGExpr.cpp b/CodeGen/CGExpr.cpp
index 78818ce..98fae6d 100644
--- a/CodeGen/CGExpr.cpp
+++ b/CodeGen/CGExpr.cpp
@@ -1152,7 +1152,7 @@
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 dd322a1..3d87564 100644
--- a/CodeGen/CodeGenTypes.h
+++ b/CodeGen/CodeGenTypes.h
@@ -41,6 +41,7 @@
void DecodeArgumentTypes(const FunctionTypeProto &FTP,
std::vector<const llvm::Type*> &ArgTys);
};
+
} // end namespace CodeGen
} // end namespace clang
diff --git a/Sema/SemaDecl.cpp b/Sema/SemaDecl.cpp
index eab2bec..4c2c5a5 100644
--- a/Sema/SemaDecl.cpp
+++ b/Sema/SemaDecl.cpp
@@ -972,7 +972,7 @@
}
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 @@
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;
diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h
index 984b6f9..5aa6a04 100644
--- a/include/clang/AST/ASTContext.h
+++ b/include/clang/AST/ASTContext.h
@@ -54,6 +54,11 @@
~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 e23a7bd..a4f596c 100644
--- a/include/clang/AST/Type.h
+++ b/include/clang/AST/Type.h
@@ -196,9 +196,6 @@
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; }