Skip to content

Commit f766ba7

Browse files
committed
Save FieldDecl BitWidth as a ConstantExpr
1 parent 6f16a8b commit f766ba7

32 files changed

+74
-76
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,7 +3143,7 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
31433143

31443144
/// Computes the bit width of this field, if this is a bit field.
31453145
/// May not be called on non-bitfields.
3146-
unsigned getBitWidthValue(const ASTContext &Ctx) const;
3146+
unsigned getBitWidthValue() const;
31473147

31483148
/// Set the bit-field width for this member.
31493149
// Note: used by some clients (i.e., do not remove it).
@@ -3174,7 +3174,7 @@ class FieldDecl : public DeclaratorDecl, public Mergeable<FieldDecl> {
31743174
/// Is this a zero-length bit-field? Such bit-fields aren't really bit-fields
31753175
/// at all and instead act as a separator between contiguous runs of other
31763176
/// bit-fields.
3177-
bool isZeroLengthBitField(const ASTContext &Ctx) const;
3177+
bool isZeroLengthBitField() const;
31783178

31793179
/// Determine if this field is a subobject of zero size, that is, either a
31803180
/// zero-length bit-field or a field of empty class type with the

clang/include/clang/ASTMatchers/ASTMatchers.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,7 @@ AST_MATCHER(FieldDecl, isBitField) {
708708
/// fieldDecl(hasBitWidth(2))
709709
/// matches 'int a;' and 'int c;' but not 'int b;'.
710710
AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) {
711-
return Node.isBitField() &&
712-
Node.getBitWidthValue(Finder->getASTContext()) == Width;
711+
return Node.isBitField() && Node.getBitWidthValue() == Width;
713712
}
714713

715714
/// Matches non-static data members that have an in-class initializer.

clang/lib/AST/ASTContext.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2774,7 +2774,7 @@ getSubobjectSizeInBits(const FieldDecl *Field, const ASTContext &Context,
27742774
if (Field->isUnnamedBitField())
27752775
return 0;
27762776

2777-
int64_t BitfieldSize = Field->getBitWidthValue(Context);
2777+
int64_t BitfieldSize = Field->getBitWidthValue();
27782778
if (IsBitIntType) {
27792779
if ((unsigned)BitfieldSize >
27802780
cast<BitIntType>(Field->getType())->getNumBits())
@@ -7745,7 +7745,7 @@ QualType ASTContext::isPromotableBitField(Expr *E) const {
77457745

77467746
QualType FT = Field->getType();
77477747

7748-
uint64_t BitWidth = Field->getBitWidthValue(*this);
7748+
uint64_t BitWidth = Field->getBitWidthValue();
77497749
uint64_t IntSize = getTypeSize(IntTy);
77507750
// C++ [conv.prom]p5:
77517751
// A prvalue for an integral bit-field can be converted to a prvalue of type
@@ -8773,7 +8773,7 @@ static void EncodeBitField(const ASTContext *Ctx, std::string& S,
87738773
S += getObjCEncodingForPrimitiveType(Ctx, BT);
87748774
}
87758775
}
8776-
S += llvm::utostr(FD->getBitWidthValue(*Ctx));
8776+
S += llvm::utostr(FD->getBitWidthValue());
87778777
}
87788778

87798779
// Helper function for determining whether the encoded type string would include
@@ -9199,7 +9199,7 @@ void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
91999199
}
92009200

92019201
for (FieldDecl *Field : RDecl->fields()) {
9202-
if (!Field->isZeroLengthBitField(*this) && Field->isZeroSize(*this))
9202+
if (!Field->isZeroLengthBitField() && Field->isZeroSize(*this))
92039203
continue;
92049204
uint64_t offs = layout.getFieldOffset(Field->getFieldIndex());
92059205
FieldOrBaseOffsets.insert(FieldOrBaseOffsets.upper_bound(offs),
@@ -9296,7 +9296,7 @@ void ASTContext::getObjCEncodingForStructureImpl(RecordDecl *RDecl,
92969296
if (field->isBitField()) {
92979297
EncodeBitField(this, S, field->getType(), field);
92989298
#ifndef NDEBUG
9299-
CurOffs += field->getBitWidthValue(*this);
9299+
CurOffs += field->getBitWidthValue();
93009300
#endif
93019301
} else {
93029302
QualType qt = field->getType();

clang/lib/AST/ByteCode/Interp.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,8 +1489,7 @@ bool InitThisBitField(InterpState &S, CodePtr OpPC, const Record::Field *F,
14891489
return false;
14901490
const Pointer &Field = This.atField(FieldOffset);
14911491
const auto &Value = S.Stk.pop<T>();
1492-
Field.deref<T>() =
1493-
Value.truncate(F->Decl->getBitWidthValue(S.getASTContext()));
1492+
Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
14941493
Field.initialize();
14951494
return true;
14961495
}
@@ -1513,8 +1512,7 @@ bool InitBitField(InterpState &S, CodePtr OpPC, const Record::Field *F) {
15131512
assert(F->isBitField());
15141513
const T &Value = S.Stk.pop<T>();
15151514
const Pointer &Field = S.Stk.peek<Pointer>().atField(F->Offset);
1516-
Field.deref<T>() =
1517-
Value.truncate(F->Decl->getBitWidthValue(S.getASTContext()));
1515+
Field.deref<T>() = Value.truncate(F->Decl->getBitWidthValue());
15181516
Field.activate();
15191517
Field.initialize();
15201518
return true;
@@ -1821,7 +1819,7 @@ bool StoreBitField(InterpState &S, CodePtr OpPC) {
18211819
if (Ptr.canBeInitialized())
18221820
Ptr.initialize();
18231821
if (const auto *FD = Ptr.getField())
1824-
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getASTContext()));
1822+
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
18251823
else
18261824
Ptr.deref<T>() = Value;
18271825
return true;
@@ -1836,7 +1834,7 @@ bool StoreBitFieldPop(InterpState &S, CodePtr OpPC) {
18361834
if (Ptr.canBeInitialized())
18371835
Ptr.initialize();
18381836
if (const auto *FD = Ptr.getField())
1839-
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue(S.getASTContext()));
1837+
Ptr.deref<T>() = Value.truncate(FD->getBitWidthValue());
18401838
else
18411839
Ptr.deref<T>() = Value;
18421840
return true;

clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static bool readPointerToBuffer(const Context &Ctx, const Pointer &FromPtr,
333333

334334
} else {
335335
if (const FieldDecl *FD = P.getField(); FD && FD->isBitField())
336-
BitWidth = FD->getBitWidthValue(ASTCtx);
336+
BitWidth = FD->getBitWidthValue();
337337
else if (T == PT_Bool && PackedBools)
338338
BitWidth = 1;
339339

clang/lib/AST/Decl.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,18 +4599,21 @@ void FieldDecl::setLazyInClassInitializer(LazyDeclStmtPtr NewInit) {
45994599
Init = NewInit;
46004600
}
46014601

4602-
unsigned FieldDecl::getBitWidthValue(const ASTContext &Ctx) const {
4602+
unsigned FieldDecl::getBitWidthValue() const {
46034603
assert(isBitField() && "not a bitfield");
4604-
return getBitWidth()->EvaluateKnownConstInt(Ctx).getZExtValue();
4604+
return cast<ConstantExpr>(getBitWidth())
4605+
->getAPValueResult()
4606+
.getInt()
4607+
.getZExtValue();
46054608
}
46064609

4607-
bool FieldDecl::isZeroLengthBitField(const ASTContext &Ctx) const {
4610+
bool FieldDecl::isZeroLengthBitField() const {
46084611
return isUnnamedBitField() && !getBitWidth()->isValueDependent() &&
4609-
getBitWidthValue(Ctx) == 0;
4612+
getBitWidthValue() == 0;
46104613
}
46114614

46124615
bool FieldDecl::isZeroSize(const ASTContext &Ctx) const {
4613-
if (isZeroLengthBitField(Ctx))
4616+
if (isZeroLengthBitField())
46144617
return true;
46154618

46164619
// C++2a [intro.object]p7:

clang/lib/AST/DeclCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ void CXXRecordDecl::addedMember(Decl *D) {
993993
// C++ [meta.unary.prop]p4: [LWG2358]
994994
// T is a class type [...] with [...] no unnamed bit-fields of non-zero
995995
// length
996-
if (data().Empty && !Field->isZeroLengthBitField(Context) &&
996+
if (data().Empty && !Field->isZeroLengthBitField() &&
997997
Context.getLangOpts().getClangABICompat() >
998998
LangOptions::ClangABI::Ver6)
999999
data().Empty = false;

clang/lib/AST/Expr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ bool Expr::isKnownToHaveBooleanValue(bool Semantic) const {
196196

197197
if (const FieldDecl *FD = E->getSourceBitField())
198198
if (!Semantic && FD->getType()->isUnsignedIntegerType() &&
199-
!FD->getBitWidth()->isValueDependent() &&
200-
FD->getBitWidthValue(FD->getASTContext()) == 1)
199+
!FD->getBitWidth()->isValueDependent() && FD->getBitWidthValue() == 1)
201200
return true;
202201

203202
return false;

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2875,7 +2875,7 @@ static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,
28752875

28762876
APSInt &Int = Value.getInt();
28772877
unsigned OldBitWidth = Int.getBitWidth();
2878-
unsigned NewBitWidth = FD->getBitWidthValue(Info.Ctx);
2878+
unsigned NewBitWidth = FD->getBitWidthValue();
28792879
if (NewBitWidth < OldBitWidth)
28802880
Int = Int.trunc(NewBitWidth).extend(OldBitWidth);
28812881
return true;

clang/lib/AST/Randstruct.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void randomizeStructureLayoutImpl(const ASTContext &Context,
9191
auto FieldIter = FieldsOut.begin();
9292
FieldDecl *FD = *FieldIter;
9393

94-
if (FD->isBitField() && !FD->isZeroLengthBitField(Context)) {
94+
if (FD->isBitField() && !FD->isZeroLengthBitField()) {
9595
// Start a bitfield run if this is the first bitfield we have found.
9696
if (!CurrentBitfieldRun)
9797
CurrentBitfieldRun = std::make_unique<BitfieldRunBucket>();

clang/lib/AST/RecordLayoutBuilder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ static bool isAIXLayout(const ASTContext &Context) {
15421542

15431543
void ItaniumRecordLayoutBuilder::LayoutBitField(const FieldDecl *D) {
15441544
bool FieldPacked = Packed || D->hasAttr<PackedAttr>();
1545-
uint64_t FieldSize = D->getBitWidthValue(Context);
1545+
uint64_t FieldSize = D->getBitWidthValue();
15461546
TypeInfo FieldInfo = Context.getTypeInfo(D->getType());
15471547
uint64_t StorageUnitSize = FieldInfo.Width;
15481548
unsigned FieldAlign = FieldInfo.Align;
@@ -3022,7 +3022,7 @@ void MicrosoftRecordLayoutBuilder::layoutField(const FieldDecl *FD) {
30223022
}
30233023

30243024
void MicrosoftRecordLayoutBuilder::layoutBitField(const FieldDecl *FD) {
3025-
unsigned Width = FD->getBitWidthValue(Context);
3025+
unsigned Width = FD->getBitWidthValue();
30263026
if (Width == 0) {
30273027
layoutZeroWidthBitField(FD);
30283028
return;
@@ -3692,7 +3692,7 @@ static void DumpRecordLayout(raw_ostream &OS, const RecordDecl *RD,
36923692
if (Field.isBitField()) {
36933693
uint64_t LocalFieldByteOffsetInBits = C.toBits(FieldOffset - Offset);
36943694
unsigned Begin = LocalFieldOffsetInBits - LocalFieldByteOffsetInBits;
3695-
unsigned Width = Field.getBitWidthValue(C);
3695+
unsigned Width = Field.getBitWidthValue();
36963696
PrintBitFieldOffset(OS, FieldOffset, Begin, Width, IndentLevel);
36973697
} else {
36983698
PrintOffset(OS, FieldOffset, IndentLevel);

clang/lib/CodeGen/ABIInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ bool ABIInfo::isHomogeneousAggregate(QualType Ty, const Type *&Base,
106106
continue;
107107

108108
if (isZeroLengthBitfieldPermittedInHomogeneousAggregate() &&
109-
FD->isZeroLengthBitField(getContext()))
109+
FD->isZeroLengthBitField())
110110
continue;
111111

112112
uint64_t FldMembers;

clang/lib/CodeGen/ABIInfoImpl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ bool CodeGen::isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays,
303303

304304
bool CodeGen::isEmptyFieldForLayout(const ASTContext &Context,
305305
const FieldDecl *FD) {
306-
if (FD->isZeroLengthBitField(Context))
306+
if (FD->isZeroLengthBitField())
307307
return true;
308308

309309
if (FD->isUnnamedBitField())

clang/lib/CodeGen/CGCall.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ getTypeExpansion(QualType Ty, const ASTContext &Context) {
954954
CharUnits UnionSize = CharUnits::Zero();
955955

956956
for (const auto *FD : RD->fields()) {
957-
if (FD->isZeroLengthBitField(Context))
957+
if (FD->isZeroLengthBitField())
958958
continue;
959959
assert(!FD->isBitField() &&
960960
"Cannot expand structure with bit-field members.");
@@ -974,7 +974,7 @@ getTypeExpansion(QualType Ty, const ASTContext &Context) {
974974
}
975975

976976
for (const auto *FD : RD->fields()) {
977-
if (FD->isZeroLengthBitField(Context))
977+
if (FD->isZeroLengthBitField())
978978
continue;
979979
assert(!FD->isBitField() &&
980980
"Cannot expand structure with bit-field members.");
@@ -3698,7 +3698,7 @@ static void setUsedBits(CodeGenModule &CGM, const RecordType *RTy, int Offset,
36983698
for (auto I = RD->field_begin(), E = RD->field_end(); I != E; ++I, ++Idx) {
36993699
const FieldDecl *F = *I;
37003700

3701-
if (F->isUnnamedBitField() || F->isZeroLengthBitField(Context) ||
3701+
if (F->isUnnamedBitField() || F->isZeroLengthBitField() ||
37023702
F->getType()->isIncompleteArrayType())
37033703
continue;
37043704

clang/lib/CodeGen/CGClass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ namespace {
945945
ASTContext &Ctx = CGF.getContext();
946946
unsigned LastFieldSize =
947947
LastField->isBitField()
948-
? LastField->getBitWidthValue(Ctx)
948+
? LastField->getBitWidthValue()
949949
: Ctx.toBits(
950950
Ctx.getTypeInfoDataSizeInChars(LastField->getType()).Width);
951951
uint64_t MemcpySizeBits = LastFieldOffset + LastFieldSize -

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,7 @@ llvm::DIDerivedType *CGDebugInfo::createBitFieldSeparatorIfNeeded(
17221722
assert(PreviousBitfield->isBitField());
17231723

17241724
ASTContext &Context = CGM.getContext();
1725-
if (!PreviousBitfield->isZeroLengthBitField(Context))
1725+
if (!PreviousBitfield->isZeroLengthBitField())
17261726
return nullptr;
17271727

17281728
QualType Ty = PreviousBitfield->getType();
@@ -3231,9 +3231,8 @@ llvm::DIType *CGDebugInfo::CreateTypeDefinition(const ObjCInterfaceType *Ty,
32313231
if (!FType->isIncompleteArrayType()) {
32323232

32333233
// Bit size, align and offset of the type.
3234-
FieldSize = Field->isBitField()
3235-
? Field->getBitWidthValue(CGM.getContext())
3236-
: CGM.getContext().getTypeSize(FType);
3234+
FieldSize = Field->isBitField() ? Field->getBitWidthValue()
3235+
: CGM.getContext().getTypeSize(FType);
32373236
FieldAlign = getTypeAlignIfRequired(FType, CGM.getContext());
32383237
}
32393238

clang/lib/CodeGen/CGNonTrivialStruct.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ using namespace CodeGen;
2525
static uint64_t getFieldSize(const FieldDecl *FD, QualType FT,
2626
ASTContext &Ctx) {
2727
if (FD && FD->isBitField())
28-
return FD->getBitWidthValue(Ctx);
28+
return FD->getBitWidthValue();
2929
return Ctx.getTypeSize(FT);
3030
}
3131

@@ -255,7 +255,7 @@ struct GenBinaryFuncName : CopyStructVisitor<GenBinaryFuncName<IsMove>, IsMove>,
255255
void visitVolatileTrivial(QualType FT, const FieldDecl *FD,
256256
CharUnits CurStructOffset) {
257257
// Zero-length bit-fields don't need to be copied/assigned.
258-
if (FD && FD->isZeroLengthBitField(this->Ctx))
258+
if (FD && FD->isZeroLengthBitField())
259259
return;
260260

261261
// Because volatile fields can be bit-fields and are individually copied,
@@ -544,7 +544,7 @@ struct GenBinaryFunc : CopyStructVisitor<Derived, IsMove>,
544544
LValue DstLV, SrcLV;
545545
if (FD) {
546546
// No need to copy zero-length bit-fields.
547-
if (FD->isZeroLengthBitField(this->CGF->getContext()))
547+
if (FD->isZeroLengthBitField())
548548
return;
549549

550550
QualType RT = QualType(FD->getParent()->getTypeForDecl(), 0);

clang/lib/CodeGen/CGObjCMac.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,8 +2543,7 @@ void CGObjCCommonMac::BuildRCRecordLayout(const llvm::StructLayout *RecLayout,
25432543
if (LastFieldBitfieldOrUnnamed) {
25442544
if (LastFieldBitfieldOrUnnamed->isBitField()) {
25452545
// Last field was a bitfield. Must update the info.
2546-
uint64_t BitFieldSize
2547-
= LastFieldBitfieldOrUnnamed->getBitWidthValue(CGM.getContext());
2546+
uint64_t BitFieldSize = LastFieldBitfieldOrUnnamed->getBitWidthValue();
25482547
unsigned UnsSize = (BitFieldSize / ByteSizeInBits) +
25492548
((BitFieldSize % ByteSizeInBits) != 0);
25502549
CharUnits Size = CharUnits::fromQuantity(UnsSize);

clang/lib/CodeGen/CGObjCRuntime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ LValue CGObjCRuntime::EmitValueForIvarAtOffset(CodeGen::CodeGenFunction &CGF,
8989
CGF.CGM.getContext().lookupFieldBitOffset(OID, nullptr, Ivar);
9090
uint64_t BitOffset = FieldBitOffset % CGF.CGM.getContext().getCharWidth();
9191
uint64_t AlignmentBits = CGF.CGM.getTarget().getCharAlign();
92-
uint64_t BitFieldSize = Ivar->getBitWidthValue(CGF.getContext());
92+
uint64_t BitFieldSize = Ivar->getBitWidthValue();
9393
CharUnits StorageSize = CGF.CGM.getContext().toCharUnitsFromBits(
9494
llvm::alignTo(BitOffset + BitFieldSize, AlignmentBits));
9595
CharUnits Alignment = CGF.CGM.getContext().toCharUnitsFromBits(AlignmentBits);

0 commit comments

Comments
 (0)