Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5533,6 +5533,9 @@ template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
return this->emitInvalid(S);
case Stmt::LabelStmtClass:
return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
case Stmt::CXXExpansionStmtInstantiationClass:
return this->visitCXXExpansionStmtInstantiation(
cast<CXXExpansionStmtInstantiation>(S));
default: {
if (const auto *E = dyn_cast<Expr>(S))
return this->discard(E);
Expand Down Expand Up @@ -5616,6 +5619,13 @@ bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS,
FunctionDecl, NamespaceAliasDecl, UsingDirectiveDecl>(D))
continue;

if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(D)) {
assert(ESD->getInstantiations() && "not expanded?");
if (!this->visitStmt(ESD->getInstantiations()))
return false;
continue;
}

const auto *VD = dyn_cast<VarDecl>(D);
if (!VD)
return false;
Expand Down Expand Up @@ -6180,6 +6190,38 @@ bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) {
return this->visitStmt(S->getTryBlock());
}

/// template for (auto x : {1, 2}) {}
///
/// This is not a loop from an AST perspective at all since it has already
/// been instantiated to a list of compound statements.
///
/// Since we can have control flow in those compound statements, we need to
/// handle it mostly like a loop though.
template <class Emitter>
bool Compiler<Emitter>::visitCXXExpansionStmtInstantiation(
const CXXExpansionStmtInstantiation *S) {
LocalScope<Emitter> WholeLoopScope(this, ScopeKind::Block);

for (const Stmt *Shared : S->getSharedStmts()) {
if (!this->visitDeclStmt(cast<DeclStmt>(Shared), true))
return false;
}

LabelTy EndLabel = this->getLabel();
for (const Stmt *Instantiation : S->getInstantiations()) {
LabelTy ContinueLabel = this->getLabel();
LoopScope<Emitter> LS(this, S, EndLabel, ContinueLabel);

if (!this->visitStmt(Instantiation))
return false;
this->emitLabel(ContinueLabel);
}

this->emitLabel(EndLabel);

return WholeLoopScope.destroyLocals();
}

template <class Emitter>
bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
assert(MD->isLambdaStaticInvoker());
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/ByteCode/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
bool visitDefaultStmt(const DefaultStmt *S);
bool visitAttributedStmt(const AttributedStmt *S);
bool visitCXXTryStmt(const CXXTryStmt *S);

bool
visitCXXExpansionStmtInstantiation(const CXXExpansionStmtInstantiation *S);
protected:
bool visitStmt(const Stmt *S);
bool visitExpr(const Expr *E, bool DestroyToplevelScope) override;
Expand Down
40 changes: 40 additions & 0 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6036,6 +6036,12 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
const VarDecl *VD = dyn_cast_or_null<VarDecl>(D);
if (VD && !CheckLocalVariableDeclaration(Info, VD))
return ESR_Failed;

if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(D)) {
assert(ESD->getInstantiations() && "not expanded?");
return EvaluateStmt(Result, Info, ESD->getInstantiations(), Case);
}

// Each declaration initialization is its own full-expression.
FullExpressionRAII Scope(Info);
if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
Expand Down Expand Up @@ -6308,6 +6314,40 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
}

case Stmt::CXXExpansionStmtInstantiationClass: {
BlockScopeRAII Scope(Info);
const auto *Expansion = cast<CXXExpansionStmtInstantiation>(S);
for (const Stmt *Shared : Expansion->getSharedStmts()) {
EvalStmtResult ESR = EvaluateStmt(Result, Info, Shared);
if (ESR != ESR_Succeeded) {
if (ESR != ESR_Failed && !Scope.destroy())
return ESR_Failed;
return ESR;
}
}

// No need to push an extra scope for these since they're already
// CompoundStmts.
EvalStmtResult ESR = ESR_Succeeded;
for (const Stmt *Instantiation : Expansion->getInstantiations()) {
ESR = EvaluateStmt(Result, Info, Instantiation);
if (ESR == ESR_Failed ||
ShouldPropagateBreakContinue(Info, Expansion, &Scope, ESR))
return ESR;
if (ESR != ESR_Continue) {
// Succeeded here actually means we encountered a 'break'.
assert(ESR == ESR_Succeeded || ESR == ESR_Returned);
break;
}
}

// Map Continue back to Succeeded if we fell off the end of the loop.
if (ESR == ESR_Continue)
ESR = ESR_Succeeded;

return Scope.destroy() ? ESR : ESR_Failed;
}

case Stmt::SwitchStmtClass:
return EvaluateSwitch(Result, Info, cast<SwitchStmt>(S));

Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaDeclCXX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,9 @@ static bool CheckConstexprDeclStmt(Sema &SemaRef, const FunctionDecl *Dcl,
// - using-enum-declaration
continue;

case Decl::CXXExpansionStmt:
continue;

case Decl::Typedef:
case Decl::TypeAlias: {
// - typedef declarations and alias-declarations that do not define
Expand Down
Loading
Loading