Skip to content

Commit da94831

Browse files
committed
Remove isDecisionVariable flag
1 parent 8dd6f82 commit da94831

File tree

15 files changed

+74
-104
lines changed

15 files changed

+74
-104
lines changed

clang/docs/LanguageExtensions.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,6 +1641,7 @@ Conditional ``explicit`` __cpp_conditional_explicit C+
16411641
Attributes on Lambda-Expressions C++23 C++11
16421642
Attributes on Structured Bindings __cpp_structured_bindings C++26 C++03
16431643
Packs in Structured Bindings __cpp_structured_bindings C++26 C++03
1644+
Structured binding declaration as a condition C++26 C++11
16441645
Static assert with user-generated message __cpp_static_assert >= 202306L C++26 C++11
16451646
Pack Indexing __cpp_pack_indexing C++26 C++03
16461647
``= delete ("should have a reason");`` __cpp_deleted_function C++26 C++03

clang/include/clang/AST/DeclCXX.h

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4224,18 +4224,15 @@ class DecompositionDecl final
42244224
: public VarDecl,
42254225
private llvm::TrailingObjects<DecompositionDecl, BindingDecl *> {
42264226
/// The number of BindingDecl*s following this object.
4227-
unsigned NumBindings : 31;
4228-
4229-
LLVM_PREFERRED_TYPE(bool)
4230-
unsigned IsDecisionVariable : 1;
4227+
unsigned NumBindings;
42314228

42324229
DecompositionDecl(ASTContext &C, DeclContext *DC, SourceLocation StartLoc,
42334230
SourceLocation LSquareLoc, QualType T,
42344231
TypeSourceInfo *TInfo, StorageClass SC,
4235-
ArrayRef<BindingDecl *> Bindings, bool IsDecisionVariable)
4232+
ArrayRef<BindingDecl *> Bindings)
42364233
: VarDecl(Decomposition, C, DC, StartLoc, LSquareLoc, nullptr, T, TInfo,
42374234
SC),
4238-
NumBindings(Bindings.size()), IsDecisionVariable(IsDecisionVariable) {
4235+
NumBindings(Bindings.size()) {
42394236
std::uninitialized_copy(Bindings.begin(), Bindings.end(),
42404237
getTrailingObjects<BindingDecl *>());
42414238
for (auto *B : Bindings) {
@@ -4256,14 +4253,13 @@ class DecompositionDecl final
42564253

42574254
static DecompositionDecl *Create(ASTContext &C, DeclContext *DC,
42584255
SourceLocation StartLoc,
4259-
SourceLocation LSquareLoc, QualType T,
4260-
TypeSourceInfo *TInfo, StorageClass S,
4261-
ArrayRef<BindingDecl *> Bindings,
4262-
bool IsDecisionVariable);
4256+
SourceLocation LSquareLoc,
4257+
QualType T, TypeSourceInfo *TInfo,
4258+
StorageClass S,
4259+
ArrayRef<BindingDecl *> Bindings);
42634260

42644261
static DecompositionDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID,
4265-
unsigned NumBindings,
4266-
bool IsDecisionVariable);
4262+
unsigned NumBindings);
42674263

42684264
// Provide the range of bindings which may have a nested pack.
42694265
llvm::ArrayRef<BindingDecl *> bindings() const {
@@ -4290,12 +4286,6 @@ class DecompositionDecl final
42904286
std::move(Bindings));
42914287
}
42924288

4293-
/// The decision variable of a condition that is a structured binding
4294-
/// declaration is specified in [dcl.struct.bind]p4:
4295-
/// If a structured binding declaration appears as a condition, the decision
4296-
/// variable of the condition is e.
4297-
bool isDecisionVariable() const { return IsDecisionVariable; }
4298-
42994289
void printName(raw_ostream &OS, const PrintingPolicy &Policy) const override;
43004290

43014291
static bool classof(const Decl *D) { return classofKind(D->getKind()); }

clang/lib/AST/ASTImporter.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4614,10 +4614,9 @@ ExpectedDecl ASTNodeImporter::VisitVarDecl(VarDecl *D) {
46144614
ImportArrayChecked(FromDecomp->bindings(), Bindings.begin()))
46154615
return std::move(Err);
46164616
DecompositionDecl *ToDecomp;
4617-
if (GetImportedOrCreateDecl(ToDecomp, FromDecomp, Importer.getToContext(),
4618-
DC, ToInnerLocStart, Loc, ToType,
4619-
ToTypeSourceInfo, D->getStorageClass(),
4620-
Bindings, FromDecomp->isDecisionVariable()))
4617+
if (GetImportedOrCreateDecl(
4618+
ToDecomp, FromDecomp, Importer.getToContext(), DC, ToInnerLocStart,
4619+
Loc, ToType, ToTypeSourceInfo, D->getStorageClass(), Bindings))
46214620
return ToDecomp;
46224621
ToVar = ToDecomp;
46234622
} else {

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5051,7 +5051,7 @@ template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
50515051
case Stmt::CompoundStmtClass:
50525052
return visitCompoundStmt(cast<CompoundStmt>(S));
50535053
case Stmt::DeclStmtClass:
5054-
return visitDeclStmt(cast<DeclStmt>(S));
5054+
return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
50555055
case Stmt::ReturnStmtClass:
50565056
return visitReturnStmt(cast<ReturnStmt>(S));
50575057
case Stmt::IfStmtClass:
@@ -5115,7 +5115,8 @@ bool Compiler<Emitter>::emitDecompositionVarInit(const DecompositionDecl *DD) {
51155115
}
51165116

51175117
template <class Emitter>
5118-
bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS) {
5118+
bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS,
5119+
bool EvaluateConditionDecl) {
51195120
for (const auto *D : DS->decls()) {
51205121
if (isa<StaticAssertDecl, TagDecl, TypedefNameDecl, BaseUsingDecl,
51215122
FunctionDecl, NamespaceAliasDecl, UsingDirectiveDecl>(D))
@@ -5129,7 +5130,7 @@ bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS) {
51295130

51305131
// Register decomposition decl holding vars.
51315132
if (const auto *DD = dyn_cast<DecompositionDecl>(VD);
5132-
DD && !DD->isDecisionVariable()) {
5133+
EvaluateConditionDecl && DD) {
51335134
if (!this->emitDecompositionVarInit(DD))
51345135
return false;
51355136
}
@@ -5180,7 +5181,7 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
51805181
return false;
51815182

51825183
if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt())
5183-
if (!visitDeclStmt(CondDecl))
5184+
if (!visitDeclStmt(CondDecl, /*EvaluateConditionDecl=*/false))
51845185
return false;
51855186

51865187
// Compile condition.
@@ -5198,8 +5199,7 @@ template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
51985199
}
51995200

52005201
if (auto *DD =
5201-
dyn_cast_if_present<DecompositionDecl>(IS->getConditionVariable());
5202-
DD && DD->isDecisionVariable())
5202+
dyn_cast_if_present<DecompositionDecl>(IS->getConditionVariable()))
52035203
if (!this->emitDecompositionVarInit(DD))
52045204
return false;
52055205

@@ -5258,15 +5258,14 @@ bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
52585258
{
52595259
LocalScope<Emitter> CondScope(this);
52605260
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5261-
if (!visitDeclStmt(CondDecl))
5261+
if (!visitDeclStmt(CondDecl, /*EvaluateConditionDecl=*/false))
52625262
return false;
52635263

52645264
if (!this->visitBool(Cond))
52655265
return false;
52665266

52675267
if (auto *DD =
5268-
dyn_cast_if_present<DecompositionDecl>(S->getConditionVariable());
5269-
DD && DD->isDecisionVariable())
5268+
dyn_cast_if_present<DecompositionDecl>(S->getConditionVariable()))
52705269
if (!this->emitDecompositionVarInit(DD))
52715270
return false;
52725271

@@ -5341,7 +5340,7 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
53415340
{
53425341
LocalScope<Emitter> CondScope(this);
53435342
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5344-
if (!visitDeclStmt(CondDecl))
5343+
if (!visitDeclStmt(CondDecl, /*EvaluateConditionDecl=*/false))
53455344
return false;
53465345

53475346
if (Cond) {
@@ -5352,8 +5351,7 @@ bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
53525351
}
53535352

53545353
if (auto *DD =
5355-
dyn_cast_if_present<DecompositionDecl>(S->getConditionVariable());
5356-
DD && DD->isDecisionVariable())
5354+
dyn_cast_if_present<DecompositionDecl>(S->getConditionVariable()))
53575355
if (!this->emitDecompositionVarInit(DD))
53585356
return false;
53595357

@@ -5470,7 +5468,7 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
54705468
return false;
54715469

54725470
if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5473-
if (!visitDeclStmt(CondDecl))
5471+
if (!visitDeclStmt(CondDecl, /*EvaluateConditionDecl=*/false))
54745472
return false;
54755473

54765474
// Initialize condition variable.
@@ -5480,8 +5478,7 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
54805478
return false;
54815479

54825480
if (auto *DD =
5483-
dyn_cast_if_present<DecompositionDecl>(S->getConditionVariable());
5484-
DD && DD->isDecisionVariable())
5481+
dyn_cast_if_present<DecompositionDecl>(S->getConditionVariable()))
54855482
if (!this->emitDecompositionVarInit(DD))
54865483
return false;
54875484

clang/lib/AST/ByteCode/Compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
213213

214214
// Statements.
215215
bool visitCompoundStmt(const CompoundStmt *S);
216-
bool visitDeclStmt(const DeclStmt *DS);
216+
bool visitDeclStmt(const DeclStmt *DS, bool EvaluateConditionDecl);
217217
bool visitReturnStmt(const ReturnStmt *RS);
218218
bool visitIfStmt(const IfStmt *IS);
219219
bool visitWhileStmt(const WhileStmt *S);

clang/lib/AST/DeclCXX.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,24 +3520,21 @@ DecompositionDecl *DecompositionDecl::Create(ASTContext &C, DeclContext *DC,
35203520
SourceLocation LSquareLoc,
35213521
QualType T, TypeSourceInfo *TInfo,
35223522
StorageClass SC,
3523-
ArrayRef<BindingDecl *> Bindings,
3524-
bool IsDecisionVariable) {
3523+
ArrayRef<BindingDecl *> Bindings) {
35253524
size_t Extra = additionalSizeToAlloc<BindingDecl *>(Bindings.size());
3526-
return new (C, DC, Extra) DecompositionDecl(
3527-
C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings, IsDecisionVariable);
3525+
return new (C, DC, Extra)
3526+
DecompositionDecl(C, DC, StartLoc, LSquareLoc, T, TInfo, SC, Bindings);
35283527
}
35293528

3530-
DecompositionDecl *
3531-
DecompositionDecl::CreateDeserialized(ASTContext &C, GlobalDeclID ID,
3532-
unsigned NumBindings,
3533-
bool IsDecisionVariable) {
3529+
DecompositionDecl *DecompositionDecl::CreateDeserialized(ASTContext &C,
3530+
GlobalDeclID ID,
3531+
unsigned NumBindings) {
35343532
size_t Extra = additionalSizeToAlloc<BindingDecl *>(NumBindings);
3535-
auto *Result = new (C, ID, Extra) DecompositionDecl(
3536-
C, nullptr, SourceLocation(), SourceLocation(), QualType(), nullptr,
3537-
StorageClass(), {}, IsDecisionVariable);
3533+
auto *Result = new (C, ID, Extra)
3534+
DecompositionDecl(C, nullptr, SourceLocation(), SourceLocation(),
3535+
QualType(), nullptr, StorageClass(), {});
35383536
// Set up and clean out the bindings array.
35393537
Result->NumBindings = NumBindings;
3540-
Result->IsDecisionVariable = IsDecisionVariable;
35413538
auto *Trail = Result->getTrailingObjects<BindingDecl *>();
35423539
for (unsigned I = 0; I != NumBindings; ++I)
35433540
new (Trail + I) BindingDecl*(nullptr);

clang/lib/AST/ExprConstant.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5221,27 +5221,28 @@ static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {
52215221
static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
52225222
const DecompositionDecl *DD);
52235223

5224-
static bool EvaluateDecl(EvalInfo &Info, const Decl *D) {
5224+
static bool EvaluateDecl(EvalInfo &Info, const Decl *D,
5225+
bool EvaluateConditionDecl) {
52255226
bool OK = true;
52265227

52275228
if (const VarDecl *VD = dyn_cast<VarDecl>(D))
52285229
OK &= EvaluateVarDecl(Info, VD);
52295230

52305231
if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(D);
5231-
DD && !DD->isDecisionVariable())
5232+
EvaluateConditionDecl && DD)
52325233
OK &= EvaluateDecompositionDeclInit(Info, DD);
52335234

52345235
return OK;
52355236
}
52365237

52375238
static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
52385239
const DecompositionDecl *DD) {
5239-
bool OK = true;
52405240
for (auto *BD : DD->flat_bindings())
52415241
if (auto *VD = BD->getHoldingVar())
5242-
OK &= EvaluateDecl(Info, VD);
5242+
if (!EvaluateDecl(Info, VD, /*EvaluateConditionDecl=*/true))
5243+
return false;
52435244

5244-
return OK;
5245+
return true;
52455246
}
52465247

52475248
static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {
@@ -5259,13 +5260,12 @@ static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,
52595260
if (Cond->isValueDependent())
52605261
return false;
52615262
FullExpressionRAII Scope(Info);
5262-
if (CondDecl && !EvaluateDecl(Info, CondDecl))
5263+
if (CondDecl && !EvaluateDecl(Info, CondDecl, /*EvaluateConditionDecl=*/false))
52635264
return false;
52645265
if (!EvaluateAsBooleanCondition(Cond, Result, Info))
52655266
return false;
52665267
if (auto *DD = dyn_cast_if_present<DecompositionDecl>(CondDecl);
5267-
DD && DD->isDecisionVariable() &&
5268-
!EvaluateDecompositionDeclInit(Info, DD))
5268+
DD && !EvaluateDecompositionDeclInit(Info, DD))
52695269
return false;
52705270
return Scope.destroy();
52715271
}
@@ -5341,7 +5341,8 @@ static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
53415341

53425342
FullExpressionRAII CondScope(Info);
53435343
if (SS->getConditionVariable() &&
5344-
!EvaluateDecl(Info, SS->getConditionVariable()))
5344+
!EvaluateDecl(Info, SS->getConditionVariable(),
5345+
/*EvaluateConditionDecl=*/false))
53455346
return ESR_Failed;
53465347
if (SS->getCond()->isValueDependent()) {
53475348
// We don't know what the value is, and which branch should jump to.
@@ -5353,8 +5354,7 @@ static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
53535354

53545355
if (auto *DD =
53555356
dyn_cast_if_present<DecompositionDecl>(SS->getConditionVariable());
5356-
DD && DD->isDecisionVariable() &&
5357-
!EvaluateDecompositionDeclInit(Info, DD))
5357+
DD && !EvaluateDecompositionDeclInit(Info, DD))
53585358
return ESR_Failed;
53595359

53605360
if (!CondScope.destroy())
@@ -5581,7 +5581,8 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
55815581
return ESR_Failed;
55825582
// Each declaration initialization is its own full-expression.
55835583
FullExpressionRAII Scope(Info);
5584-
if (!EvaluateDecl(Info, D) && !Info.noteFailure())
5584+
if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
5585+
!Info.noteFailure())
55855586
return ESR_Failed;
55865587
if (!Scope.destroy())
55875588
return ESR_Failed;

clang/lib/CodeGen/CGDecl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ using namespace CodeGen;
4848
static_assert(clang::Sema::MaximumAlignment <= llvm::Value::MaximumAlignment,
4949
"Clang max alignment greater than what LLVM supports?");
5050

51-
void CodeGenFunction::EmitDecl(const Decl &D) {
51+
void CodeGenFunction::EmitDecl(const Decl &D, bool EvaluateConditionDecl) {
5252
switch (D.getKind()) {
5353
case Decl::BuiltinTemplate:
5454
case Decl::TranslationUnit:
@@ -152,7 +152,7 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
152152
return;
153153
case Decl::UsingPack:
154154
for (auto *Using : cast<UsingPackDecl>(D).expansions())
155-
EmitDecl(*Using);
155+
EmitDecl(*Using, /*EvaluateConditionDecl=*/EvaluateConditionDecl);
156156
return;
157157
case Decl::UsingDirective: // using namespace X; [C++]
158158
if (CGDebugInfo *DI = getDebugInfo())
@@ -165,7 +165,7 @@ void CodeGenFunction::EmitDecl(const Decl &D) {
165165
"Should not see file-scope variables inside a function!");
166166
EmitVarDecl(VD);
167167
if (auto *DD = dyn_cast<DecompositionDecl>(&VD);
168-
DD && !DD->isDecisionVariable())
168+
EvaluateConditionDecl && DD)
169169
EmitDecompositionVarInit(*DD);
170170

171171
return;

0 commit comments

Comments
 (0)