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>();

0 commit comments

Comments
 (0)