From: Ken Dyck Date: Fri, 11 Feb 2011 01:54:29 +0000 (+0000) Subject: Add a helper function, ASTContext::toBits(), that converts sizes in X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dd76a9ab9ea675671200f94b18ce95766841952b;p=clang Add a helper function, ASTContext::toBits(), that converts sizes in CharUnits to sizes in bits, and use it to tidy up the places where the conversion was done explicitly. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@125332 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 911a50cf67..244cf26b89 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -1015,6 +1015,9 @@ public: /// toCharUnitsFromBits - Convert a size in bits to a size in characters. CharUnits toCharUnitsFromBits(int64_t BitSize) const; + /// toBits - Convert a size in characters to a size in bits. + int64_t toBits(CharUnits CharSize) const; + /// getTypeSizeInChars - Return the size of the specified type, in characters. /// This method does not work on incomplete types. CharUnits getTypeSizeInChars(QualType T) const; diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 6b26743258..03c0abd3c6 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -847,7 +847,7 @@ ASTContext::getTypeInfo(const Type *T) const { case Type::ObjCInterface: { const ObjCInterfaceType *ObjCI = cast(T); const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl()); - Width = Layout.getSize().getQuantity() * getCharWidth(); + Width = toBits(Layout.getSize()); Align = Layout.getAlignment(); break; } @@ -866,7 +866,7 @@ ASTContext::getTypeInfo(const Type *T) const { const RecordType *RT = cast(TT); const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl()); - Width = Layout.getSize().getQuantity() * getCharWidth(); + Width = toBits(Layout.getSize()); Align = Layout.getAlignment(); break; } @@ -923,6 +923,11 @@ CharUnits ASTContext::toCharUnitsFromBits(int64_t BitSize) const { return CharUnits::fromQuantity(BitSize / getCharWidth()); } +/// toBits - Convert a size in characters to a size in characters. +int64_t ASTContext::toBits(CharUnits CharSize) const { + return CharSize.getQuantity() * getCharWidth(); +} + /// getTypeSizeInChars - Return the size of the specified type, in characters. /// This method does not work on incomplete types. CharUnits ASTContext::getTypeSizeInChars(QualType T) const { diff --git a/lib/AST/RecordLayoutBuilder.cpp b/lib/AST/RecordLayoutBuilder.cpp index 62306894ab..9e7c9e5feb 100644 --- a/lib/AST/RecordLayoutBuilder.cpp +++ b/lib/AST/RecordLayoutBuilder.cpp @@ -616,11 +616,6 @@ protected: DataSize(0), NonVirtualSize(0), NonVirtualAlignment(8), PrimaryBase(0), PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { } - // FIXME: Remove this. - uint64_t toOffset(CharUnits Offset) const { - return Offset.getQuantity() * Context.getCharWidth(); - } - void Layout(const RecordDecl *D); void Layout(const CXXRecordDecl *D); void Layout(const ObjCInterfaceDecl *D); @@ -1091,14 +1086,13 @@ CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { // If we have an empty base class, try to place it at offset 0. if (Base->Class->isEmpty() && EmptySubobjects->CanPlaceBaseAtOffset(Base, CharUnits::Zero())) { - Size = std::max(Size, - Layout.getSize().getQuantity() * Context.getCharWidth()); + uint64_t RecordSizeInBits = Context.toBits(Layout.getSize()); + Size = std::max(Size, RecordSizeInBits); return CharUnits::Zero(); } - unsigned UnpackedBaseAlign = - Layout.getNonVirtualAlign().getQuantity() * Context.getCharWidth(); + unsigned UnpackedBaseAlign = Context.toBits(Layout.getNonVirtualAlign()); unsigned BaseAlign = (Packed) ? 8 : UnpackedBaseAlign; // The maximum field alignment overrides base align. @@ -1117,13 +1111,11 @@ CharUnits RecordLayoutBuilder::LayoutBase(const BaseSubobjectInfo *Base) { if (!Base->Class->isEmpty()) { // Update the data size. - DataSize = Offset + - (Layout.getNonVirtualSize().getQuantity() * Context.getCharWidth()); + DataSize = Offset + Context.toBits(Layout.getNonVirtualSize()); Size = std::max(Size, DataSize); } else - Size = std::max(Size, - Offset + (Layout.getSize().getQuantity() * Context.getCharWidth())); + Size = std::max(Size, Offset + Context.toBits(Layout.getSize())); // Remember max struct/class alignment. UpdateAlignment(BaseAlign, UnpackedBaseAlign); @@ -1885,7 +1877,7 @@ void ASTContext::DumpRecordLayout(const RecordDecl *RD, RD->dump(); OS << "\nLayout: "; OS << "getParent(); const ASTRecordLayout &RL = Types.getContext().getASTRecordLayout(RD); - uint64_t ContainingTypeSizeInBits = - RL.getSize().getQuantity() * Types.getContext().getCharWidth(); + uint64_t ContainingTypeSizeInBits = Types.getContext().toBits(RL.getSize()); unsigned ContainingTypeAlign = RL.getAlignment(); return MakeInfo(Types, FD, FieldOffset, FieldSize, ContainingTypeSizeInBits, @@ -689,8 +688,7 @@ bool CGRecordLayoutBuilder::LayoutFields(const RecordDecl *D) { } // Append tail padding if necessary. - AppendTailPadding( - Layout.getSize().getQuantity() * Types.getContext().getCharWidth()); + AppendTailPadding(Types.getContext().toBits(Layout.getSize())); return true; } @@ -855,8 +853,7 @@ CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) { // Verify that the computed LLVM struct size matches the AST layout size. const ASTRecordLayout &Layout = getContext().getASTRecordLayout(D); - uint64_t TypeSizeInBits = - Layout.getSize().getQuantity() * getContext().getCharWidth(); + uint64_t TypeSizeInBits = getContext().toBits(Layout.getSize()); assert(TypeSizeInBits == getTargetData().getTypeAllocSizeInBits(Ty) && "Type size mismatch!"); @@ -867,7 +864,7 @@ CGRecordLayout *CodeGenTypes::ComputeRecordLayout(const RecordDecl *D) { NonVirtualSize.RoundUpToAlignment(NonVirtualAlign); uint64_t AlignedNonVirtualTypeSizeInBits = - AlignedNonVirtualTypeSize.getQuantity() * getContext().getCharWidth(); + getContext().toBits(AlignedNonVirtualTypeSize); assert(AlignedNonVirtualTypeSizeInBits == getTargetData().getTypeAllocSizeInBits(BaseTy) &&