Skip to content

Commit 944691f

Browse files
committed
[NFC][FE] Replace TypeSize with StorageUnitSize
On some targets like AIX, last bitfield size is not always equal to last bitfield type size. Some bitfield like bool will have the same alignment as [unsigned]. So we'd like to use a more general term `StorageUnit` to replace type in this field. Differential Revision: https://reviews.llvm.org/D88260
1 parent dc261d2 commit 944691f

File tree

1 file changed

+31
-29
lines changed

1 file changed

+31
-29
lines changed

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,10 @@ class ItaniumRecordLayoutBuilder {
622622
/// an adjacent bitfield if necessary. The unit in question is usually
623623
/// a byte, but larger units are used if IsMsStruct.
624624
unsigned char UnfilledBitsInLastUnit;
625-
/// LastBitfieldTypeSize - If IsMsStruct, represents the size of the type
626-
/// of the previous field if it was a bitfield.
627-
unsigned char LastBitfieldTypeSize;
625+
626+
/// LastBitfieldStorageUnitSize - If IsMsStruct, represents the size of the
627+
/// storage unit of the previous field if it was a bitfield.
628+
unsigned char LastBitfieldStorageUnitSize;
628629

629630
/// MaxFieldAlignment - The maximum allowed field alignment. This is set by
630631
/// #pragma pack.
@@ -693,7 +694,7 @@ class ItaniumRecordLayoutBuilder {
693694
UnadjustedAlignment(CharUnits::One()), UseExternalLayout(false),
694695
InferAlignment(false), Packed(false), IsUnion(false),
695696
IsMac68kAlign(false), IsMsStruct(false), UnfilledBitsInLastUnit(0),
696-
LastBitfieldTypeSize(0), MaxFieldAlignment(CharUnits::Zero()),
697+
LastBitfieldStorageUnitSize(0), MaxFieldAlignment(CharUnits::Zero()),
697698
DataSize(0), NonVirtualSize(CharUnits::Zero()),
698699
NonVirtualAlignment(CharUnits::One()),
699700
PreferredNVAlignment(CharUnits::One()),
@@ -708,7 +709,7 @@ class ItaniumRecordLayoutBuilder {
708709

709710
void LayoutFields(const RecordDecl *D);
710711
void LayoutField(const FieldDecl *D, bool InsertExtraPadding);
711-
void LayoutWideBitField(uint64_t FieldSize, uint64_t TypeSize,
712+
void LayoutWideBitField(uint64_t FieldSize, uint64_t StorageUnitSize,
712713
bool FieldPacked, const FieldDecl *D);
713714
void LayoutBitField(const FieldDecl *D);
714715

@@ -1451,7 +1452,7 @@ roundUpSizeToCharAlignment(uint64_t Size,
14511452
}
14521453

14531454
void ItaniumRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
1454-
uint64_t TypeSize,
1455+
uint64_t StorageUnitSize,
14551456
bool FieldPacked,
14561457
const FieldDecl *D) {
14571458
assert(Context.getLangOpts().CPlusPlus &&
@@ -1481,7 +1482,7 @@ void ItaniumRecordLayoutBuilder::LayoutWideBitField(uint64_t FieldSize,
14811482

14821483
// We're not going to use any of the unfilled bits in the last byte.
14831484
UnfilledBitsInLastUnit = 0;
1484-
LastBitfieldTypeSize = 0;
1485+
LastBitfieldStorageUnitSize = 0;
14851486

14861487
uint64_t FieldOffset;
14871488
uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit;
@@ -1520,7 +1521,7 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
15201521
bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
15211522
uint64_t FieldSize = D->getBitWidthValue(Context);
15221523
TypeInfo FieldInfo = Context.getTypeInfo(D->getType());
1523-
uint64_t TypeSize = FieldInfo.Width;
1524+
uint64_t StorageUnitSize = FieldInfo.Width;
15241525
unsigned FieldAlign = FieldInfo.Align;
15251526

15261527
// UnfilledBitsInLastUnit is the difference between the end of the
@@ -1529,7 +1530,7 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
15291530
// first bit offset available for non-bitfields). The current data
15301531
// size in bits is always a multiple of the char size; additionally,
15311532
// for ms_struct records it's also a multiple of the
1532-
// LastBitfieldTypeSize (if set).
1533+
// LastBitfieldStorageUnitSize (if set).
15331534

15341535
// The struct-layout algorithm is dictated by the platform ABI,
15351536
// which in principle could use almost any rules it likes. In
@@ -1583,26 +1584,26 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
15831584
// First, some simple bookkeeping to perform for ms_struct structs.
15841585
if (IsMsStruct) {
15851586
// The field alignment for integer types is always the size.
1586-
FieldAlign = TypeSize;
1587+
FieldAlign = StorageUnitSize;
15871588

15881589
// If the previous field was not a bitfield, or was a bitfield
15891590
// with a different storage unit size, or if this field doesn't fit into
15901591
// the current storage unit, we're done with that storage unit.
1591-
if (LastBitfieldTypeSize != TypeSize ||
1592+
if (LastBitfieldStorageUnitSize != StorageUnitSize ||
15921593
UnfilledBitsInLastUnit < FieldSize) {
15931594
// Also, ignore zero-length bitfields after non-bitfields.
1594-
if (!LastBitfieldTypeSize && !FieldSize)
1595+
if (!LastBitfieldStorageUnitSize && !FieldSize)
15951596
FieldAlign = 1;
15961597

15971598
UnfilledBitsInLastUnit = 0;
1598-
LastBitfieldTypeSize = 0;
1599+
LastBitfieldStorageUnitSize = 0;
15991600
}
16001601
}
16011602

16021603
// If the field is wider than its declared type, it follows
16031604
// different rules in all cases.
1604-
if (FieldSize > TypeSize) {
1605-
LayoutWideBitField(FieldSize, TypeSize, FieldPacked, D);
1605+
if (FieldSize > StorageUnitSize) {
1606+
LayoutWideBitField(FieldSize, StorageUnitSize, FieldPacked, D);
16061607
return;
16071608
}
16081609

@@ -1686,7 +1687,7 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
16861687
// Compute the real offset.
16871688
if (FieldSize == 0 ||
16881689
(AllowPadding &&
1689-
(FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)) {
1690+
(FieldOffset & (FieldAlign - 1)) + FieldSize > StorageUnitSize)) {
16901691
FieldOffset = llvm::alignTo(FieldOffset, FieldAlign);
16911692
} else if (ExplicitFieldAlign &&
16921693
(MaxFieldAlignmentInBits == 0 ||
@@ -1700,7 +1701,8 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
17001701
// Repeat the computation for diagnostic purposes.
17011702
if (FieldSize == 0 ||
17021703
(AllowPadding &&
1703-
(UnpackedFieldOffset & (UnpackedFieldAlign-1)) + FieldSize > TypeSize))
1704+
(UnpackedFieldOffset & (UnpackedFieldAlign - 1)) + FieldSize >
1705+
StorageUnitSize))
17041706
UnpackedFieldOffset =
17051707
llvm::alignTo(UnpackedFieldOffset, UnpackedFieldAlign);
17061708
else if (ExplicitFieldAlign &&
@@ -1741,11 +1743,11 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
17411743
// is a zero-width bitfield, in which case just use a size of 1.
17421744
uint64_t RoundedFieldSize;
17431745
if (IsMsStruct) {
1744-
RoundedFieldSize =
1745-
(FieldSize ? TypeSize : Context.getTargetInfo().getCharWidth());
1746+
RoundedFieldSize = (FieldSize ? StorageUnitSize
1747+
: Context.getTargetInfo().getCharWidth());
17461748

1747-
// Otherwise, allocate just the number of bytes required to store
1748-
// the bitfield.
1749+
// Otherwise, allocate just the number of bytes required to store
1750+
// the bitfield.
17491751
} else {
17501752
RoundedFieldSize = roundUpSizeToCharAlignment(FieldSize, Context);
17511753
}
@@ -1757,15 +1759,15 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
17571759
// We should have cleared UnfilledBitsInLastUnit in every case
17581760
// where we changed storage units.
17591761
if (!UnfilledBitsInLastUnit) {
1760-
setDataSize(FieldOffset + TypeSize);
1761-
UnfilledBitsInLastUnit = TypeSize;
1762+
setDataSize(FieldOffset + StorageUnitSize);
1763+
UnfilledBitsInLastUnit = StorageUnitSize;
17621764
}
17631765
UnfilledBitsInLastUnit -= FieldSize;
1764-
LastBitfieldTypeSize = TypeSize;
1766+
LastBitfieldStorageUnitSize = StorageUnitSize;
17651767

1766-
// Otherwise, bump the data size up to include the bitfield,
1767-
// including padding up to char alignment, and then remember how
1768-
// bits we didn't use.
1768+
// Otherwise, bump the data size up to include the bitfield,
1769+
// including padding up to char alignment, and then remember how
1770+
// bits we didn't use.
17691771
} else {
17701772
uint64_t NewSizeInBits = FieldOffset + FieldSize;
17711773
uint64_t CharAlignment = Context.getTargetInfo().getCharAlign();
@@ -1775,7 +1777,7 @@ void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
17751777
// The only time we can get here for an ms_struct is if this is a
17761778
// zero-width bitfield, which doesn't count as anything for the
17771779
// purposes of unfilled bits.
1778-
LastBitfieldTypeSize = 0;
1780+
LastBitfieldStorageUnitSize = 0;
17791781
}
17801782

17811783
// Update the size.
@@ -1825,7 +1827,7 @@ void ItaniumRecordLayoutBuilder::LayoutField(const FieldDecl *D,
18251827
uint64_t UnpaddedFieldOffset = getDataSizeInBits() - UnfilledBitsInLastUnit;
18261828
// Reset the unfilled bits.
18271829
UnfilledBitsInLastUnit = 0;
1828-
LastBitfieldTypeSize = 0;
1830+
LastBitfieldStorageUnitSize = 0;
18291831

18301832
bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
18311833

0 commit comments

Comments
 (0)