From: Ken Dyck Date: Sun, 20 Feb 2011 02:06:09 +0000 (+0000) Subject: Expand use of CharUnits in LayoutField(). No change in functionality X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9ed9a250180f19b2c44df83a196fc3a2ac82f817;p=clang Expand use of CharUnits in LayoutField(). No change in functionality intended. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126066 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/RecordLayoutBuilder.cpp b/lib/AST/RecordLayoutBuilder.cpp index 18ab442bec..cf14eba140 100644 --- a/lib/AST/RecordLayoutBuilder.cpp +++ b/lib/AST/RecordLayoutBuilder.cpp @@ -1383,24 +1383,28 @@ void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { UnfilledBitsInLastByte = 0; bool FieldPacked = Packed || D->hasAttr(); - uint64_t FieldOffset = IsUnion ? 0 : DataSize; - uint64_t FieldSize; - unsigned FieldAlign; + CharUnits FieldOffset = + IsUnion ? CharUnits::Zero() : Context.toCharUnitsFromBits(DataSize); + CharUnits FieldSize; + CharUnits FieldAlign; if (D->getType()->isIncompleteArrayType()) { // This is a flexible array member; we can't directly // query getTypeInfo about these, so we figure it out here. // Flexible array members don't have any size, but they // have to be aligned appropriately for their element type. - FieldSize = 0; + FieldSize = CharUnits::Zero(); const ArrayType* ATy = Context.getAsArrayType(D->getType()); - FieldAlign = Context.getTypeAlign(ATy->getElementType()); + FieldAlign = Context.getTypeAlignInChars(ATy->getElementType()); } else if (const ReferenceType *RT = D->getType()->getAs()) { unsigned AS = RT->getPointeeType().getAddressSpace(); - FieldSize = Context.Target.getPointerWidth(AS); - FieldAlign = Context.Target.getPointerAlign(AS); + FieldSize = + Context.toCharUnitsFromBits(Context.Target.getPointerWidth(AS)); + FieldAlign = + Context.toCharUnitsFromBits(Context.Target.getPointerAlign(AS)); } else { - std::pair FieldInfo = Context.getTypeInfo(D->getType()); + std::pair FieldInfo = + Context.getTypeInfoInChars(D->getType()); FieldSize = FieldInfo.first; FieldAlign = FieldInfo.second; @@ -1412,7 +1416,7 @@ void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { // alignment if necessary. QualType T = Context.getBaseElementType(D->getType()); if (const BuiltinType *BTy = T->getAs()) { - uint64_t TypeSize = Context.getTypeSize(BTy); + CharUnits TypeSize = Context.getTypeSizeInChars(BTy); if (TypeSize > FieldAlign) FieldAlign = TypeSize; } @@ -1421,53 +1425,54 @@ void RecordLayoutBuilder::LayoutField(const FieldDecl *D) { // The align if the field is not packed. This is to check if the attribute // was unnecessary (-Wpacked). - unsigned UnpackedFieldAlign = FieldAlign; - uint64_t UnpackedFieldOffset = FieldOffset; + CharUnits UnpackedFieldAlign = FieldAlign; + CharUnits UnpackedFieldOffset = FieldOffset; if (FieldPacked) - FieldAlign = 8; - FieldAlign = std::max(FieldAlign, D->getMaxAlignment()); - UnpackedFieldAlign = std::max(UnpackedFieldAlign, D->getMaxAlignment()); + FieldAlign = CharUnits::One(); + CharUnits MaxAlignmentInChars = + Context.toCharUnitsFromBits(D->getMaxAlignment()); + FieldAlign = std::max(FieldAlign, MaxAlignmentInChars); + UnpackedFieldAlign = std::max(UnpackedFieldAlign, MaxAlignmentInChars); // The maximum field alignment overrides the aligned attribute. if (!MaxFieldAlignment.isZero()) { - unsigned MaxFieldAlignmentInBits = Context.toBits(MaxFieldAlignment); - FieldAlign = std::min(FieldAlign, MaxFieldAlignmentInBits); - UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignmentInBits); + FieldAlign = std::min(FieldAlign, MaxFieldAlignment); + UnpackedFieldAlign = std::min(UnpackedFieldAlign, MaxFieldAlignment); } // Round up the current record size to the field's alignment boundary. - FieldOffset = llvm::RoundUpToAlignment(FieldOffset, FieldAlign); - UnpackedFieldOffset = llvm::RoundUpToAlignment(UnpackedFieldOffset, - UnpackedFieldAlign); + FieldOffset = FieldOffset.RoundUpToAlignment(FieldAlign); + UnpackedFieldOffset = + UnpackedFieldOffset.RoundUpToAlignment(UnpackedFieldAlign); if (!IsUnion && EmptySubobjects) { // Check if we can place the field at this offset. - while (!EmptySubobjects->CanPlaceFieldAtOffset(D, - Context.toCharUnitsFromBits(FieldOffset))) { + while (!EmptySubobjects->CanPlaceFieldAtOffset(D, FieldOffset)) { // We couldn't place the field at the offset. Try again at a new offset. FieldOffset += FieldAlign; } } // Place this field at the current location. - FieldOffsets.push_back(FieldOffset); + FieldOffsets.push_back(Context.toBits(FieldOffset)); - CheckFieldPadding(FieldOffset, UnpaddedFieldOffset, UnpackedFieldOffset, - UnpackedFieldAlign, FieldPacked, D); + CheckFieldPadding(Context.toBits(FieldOffset), UnpaddedFieldOffset, + Context.toBits(UnpackedFieldOffset), + Context.toBits(UnpackedFieldAlign), FieldPacked, D); // Reserve space for this field. + uint64_t FieldSizeInBits = Context.toBits(FieldSize); if (IsUnion) - Size = std::max(Size, FieldSize); + Size = std::max(Size, FieldSizeInBits); else - Size = FieldOffset + FieldSize; + Size = Context.toBits(FieldOffset) + FieldSizeInBits; // Update the data size. DataSize = Size; // Remember max struct/class alignment. - UpdateAlignment(Context.toCharUnitsFromBits(FieldAlign), - Context.toCharUnitsFromBits(UnpackedFieldAlign)); + UpdateAlignment(FieldAlign, UnpackedFieldAlign); } void RecordLayoutBuilder::FinishLayout(const NamedDecl *D) {