[llvm-branch-commits] [clang] [Clang] [C++26] Expansion Statements (Part 5: Iterating Expansion Statements) (PR #169684)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Mar 24 08:29:50 PDT 2026


https://github.com/Sirraide updated https://github.com/llvm/llvm-project/pull/169684

>From c60a4b5e8813b42255e599cf2ead4801c849e833 Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Wed, 26 Nov 2025 16:18:02 +0100
Subject: [PATCH] [Clang] [C++26] Expansion Statements (Part 5)

---
 .../clang/Basic/DiagnosticSemaKinds.td        |  12 +
 clang/include/clang/Sema/Sema.h               |  12 +
 clang/lib/Sema/SemaExpand.cpp                 | 513 +++++++++++++++++-
 clang/lib/Sema/SemaStmt.cpp                   |  36 +-
 clang/lib/Sema/TreeTransform.h                |  71 +++
 5 files changed, 615 insertions(+), 29 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 0e52becd21779..9142afb95b89d 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -165,6 +165,11 @@ def err_ice_too_large : Error<
 def err_expr_not_string_literal : Error<"expression is not a string literal">;
 def note_constexpr_assert_failed : Note<
   "assertion failed during evaluation of constant expression">;
+def err_expansion_size_expr_not_ice : Error<
+  "expansion size is not a constant expression">;
+
+def note_iterating_expansion_stmt_iterator_requires_minus : Note<
+  "while attempting to construct 'begin - begin' with iterator type %0">;
 
 // Semantic analysis of constant literals.
 def ext_predef_outside_function : Warning<
@@ -3725,6 +3730,13 @@ def err_conflicting_codeseg_attribute : Error<
 def warn_duplicate_codeseg_attribute : Warning<
   "duplicate code segment specifiers">, InGroup<Section>;
 
+def err_expansion_stmt_vla : Error<
+  "cannot expand variable length array type %0">;
+def err_expansion_stmt_incomplete : Error<
+  "cannot expand expression of incomplete type %0">;
+def err_expansion_stmt_lambda : Error<
+  "cannot expand lambda closure type">;
+
 def err_attribute_patchable_function_entry_invalid_section
     : Error<"section argument to 'patchable_function_entry' attribute is not "
             "valid for this target: %0">;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index e745a1d4eafe6..89f8f4deb091a 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -11193,6 +11193,12 @@ class Sema final : public SemaBase {
       StmtResult *RebuildResult = nullptr,
       llvm::function_ref<StmtResult()> RebuildWithDereference = {});
 
+  /// Helper used by the expansion statements and for-range code to build
+  /// a variable declaration for e.g. 'begin' and 'end'. Prefer to use
+  /// BuildCXXForRangeBeginEndVars() instead if applicable.
+  VarDecl *BuildForRangeVarDecl(SourceLocation Loc, QualType Type,
+                                StringRef Name, bool Constexpr);
+
   /// Build the range variable of a range-based for loop or iterating
   /// expansion statement and return its DeclStmt.
   StmtResult BuildCXXForRangeRangeVar(Scope *S, Expr *Range, QualType Type,
@@ -15864,6 +15870,12 @@ class Sema final : public SemaBase {
                                                      SourceLocation ColonLoc,
                                                      SourceLocation RParenLoc);
 
+  StmtResult BuildNonEnumeratingCXXExpansionStmtPattern(
+      CXXExpansionStmtDecl *ESD, Stmt *Init, DeclStmt *ExpansionVarStmt,
+      Expr *ExpansionInitializer, SourceLocation LParenLoc,
+      SourceLocation ColonLoc, SourceLocation RParenLoc,
+      ArrayRef<MaterializeTemporaryExpr *> LifetimeExtendTemps = {});
+
   ExprResult BuildCXXExpansionSelectExpr(InitListExpr *Range, Expr *Idx);
 
   std::optional<uint64_t>
diff --git a/clang/lib/Sema/SemaExpand.cpp b/clang/lib/Sema/SemaExpand.cpp
index 106cc7b608212..f821f73cb1e55 100644
--- a/clang/lib/Sema/SemaExpand.cpp
+++ b/clang/lib/Sema/SemaExpand.cpp
@@ -20,10 +20,29 @@
 #include "clang/Sema/Overload.h"
 #include "clang/Sema/Sema.h"
 #include "clang/Sema/Template.h"
+#include "llvm/ADT/ScopeExit.h"
 
 using namespace clang;
 using namespace sema;
 
+namespace {
+struct IterableExpansionStmtData {
+  enum class State {
+    NotIterable,
+    Error,
+    Ok,
+  };
+
+  DeclStmt *RangeDecl = nullptr;
+  DeclStmt *BeginDecl = nullptr;
+  DeclStmt *IterDecl = nullptr;
+  State TheState = State::NotIterable;
+
+  bool isIterable() const { return TheState == State::Ok; }
+  bool hasError() { return TheState == State::Error; }
+};
+} // namespace
+
 // Build a 'DeclRefExpr' designating the template parameter '__N'.
 static DeclRefExpr *BuildIndexDRE(Sema &S, CXXExpansionStmtDecl *ESD) {
   return S.BuildDeclRefExpr(ESD->getIndexTemplateParm(),
@@ -47,7 +66,8 @@ static auto InitListContainsPack(const InitListExpr *ILE) {
                       [](const Expr *E) { return isa<PackExpansionExpr>(E); });
 }
 
-static bool HasDependentSize(const CXXExpansionStmtPattern *Pattern) {
+static bool HasDependentSize(const DeclContext *CurContext,
+                             const CXXExpansionStmtPattern *Pattern) {
   switch (Pattern->getKind()) {
   case CXXExpansionStmtPattern::ExpansionStmtKind::Enumerating: {
     auto *SelectExpr = cast<CXXExpansionSelectExpr>(
@@ -56,14 +76,204 @@ static bool HasDependentSize(const CXXExpansionStmtPattern *Pattern) {
   }
 
   case CXXExpansionStmtPattern::ExpansionStmtKind::Iterating:
-  case CXXExpansionStmtPattern::ExpansionStmtKind::Destructuring:
+    // Even if the size isn't technically dependent, delay expansion until
+    // we're no longer in a template since evaluating a lambda declared in
+    // a template doesn't work too well.
+    assert(CurContext->isExpansionStmt());
+    return CurContext->getParent()->isDependentContext();
+
   case CXXExpansionStmtPattern::ExpansionStmtKind::Dependent:
+    return true;
+
+  case CXXExpansionStmtPattern::ExpansionStmtKind::Destructuring:
     llvm_unreachable("TODO");
   }
 
   llvm_unreachable("invalid pattern kind");
 }
 
+static IterableExpansionStmtData TryBuildIterableExpansionStmtInitializer(
+    Sema &S, Expr *ExpansionInitializer, Expr *Index, SourceLocation ColonLoc,
+    bool VarIsConstexpr,
+    ArrayRef<MaterializeTemporaryExpr *> LifetimeExtendTemps) {
+  IterableExpansionStmtData Data;
+
+  // C++26 [stmt.expand]p3: An expression is expansion-iterable if it does not
+  // have array type [...]
+  QualType Ty = ExpansionInitializer->getType().getNonReferenceType();
+  if (Ty->isArrayType())
+    return Data;
+
+  // Lookup member and ADL 'begin()'/'end()'. Only check if they exist; even if
+  // they're deleted, inaccessible, etc., this is still an iterating expansion
+  // statement, albeit an ill-formed one.
+  DeclarationNameInfo BeginName(&S.PP.getIdentifierTable().get("begin"),
+                                ColonLoc);
+  DeclarationNameInfo EndName(&S.PP.getIdentifierTable().get("end"), ColonLoc);
+
+  // Try member lookup first.
+  bool FoundBeginEnd = false;
+  if (auto *Record = Ty->getAsCXXRecordDecl()) {
+    LookupResult BeginLR(S, BeginName, Sema::LookupMemberName);
+    LookupResult EndLR(S, EndName, Sema::LookupMemberName);
+    FoundBeginEnd = S.LookupQualifiedName(BeginLR, Record) &&
+                    S.LookupQualifiedName(EndLR, Record);
+  }
+
+  // Try ADL.
+  //
+  // If overload resolution for 'begin()' *and* 'end()' succeeds (irrespective
+  // of whether it results in a usable candidate), then assume this is an
+  // iterating expansion statement.
+  auto HasADLCandidate = [&](DeclarationName Name) {
+    OverloadCandidateSet Candidates(ColonLoc, OverloadCandidateSet::CSK_Normal);
+    OverloadCandidateSet::iterator Best;
+
+    S.AddArgumentDependentLookupCandidates(Name, ColonLoc, ExpansionInitializer,
+                                           /*ExplicitTemplateArgs=*/nullptr,
+                                           Candidates);
+
+    return Candidates.BestViableFunction(S, ColonLoc, Best) !=
+           OR_No_Viable_Function;
+  };
+
+  if (!FoundBeginEnd && (!HasADLCandidate(BeginName.getName()) ||
+                         !HasADLCandidate(EndName.getName())))
+    return Data;
+
+  auto Ctx = Sema::ExpressionEvaluationContext::PotentiallyEvaluated;
+  if (VarIsConstexpr)
+    Ctx = Sema::ExpressionEvaluationContext::ImmediateFunctionContext;
+  EnterExpressionEvaluationContext ExprEvalCtx(S, Ctx);
+
+  // The declarations should be attached to the parent decl context.
+  Sema::ContextRAII CtxGuard(S, S.CurContext->getParent(),
+                             /*NewThis=*/false);
+
+  // Ok, we know that this is supposed to be an iterable expansion statement;
+  // delegate to the for-range code to build the range/begin/end variables.
+  //
+  // Any failure at this point is a hard error.
+  Data.TheState = IterableExpansionStmtData::State::Error;
+  Scope *Scope = S.getCurScope();
+
+  // CWG 3131: The declaration of 'range' is of the form
+  //
+  //     constexpr[opt] decltype(auto) range = (expansion-initializer);
+  //
+  // where 'constexpr' is present iff the for-range-declaration is 'constexpr'.
+  StmtResult Var = S.BuildCXXForRangeRangeVar(
+      Scope, S.ActOnParenExpr(ColonLoc, ColonLoc, ExpansionInitializer).get(),
+      S.Context.getAutoType(DeducedKind::Undeduced, QualType(),
+                            AutoTypeKeyword::DecltypeAuto),
+      VarIsConstexpr);
+  if (Var.isInvalid())
+    return Data;
+
+  // CWG 3140: 'range', 'begin', and 'iter' are 'constexpr' iff the
+  // for-range-declaration is declared 'constexpr'.
+  //
+  // FIXME: As of CWG 3140, we should only create 'begin' here, and not 'end',
+  // but that requires another substantial refactor of the for-range code.
+  auto *RangeVar = cast<DeclStmt>(Var.get());
+  Sema::ForRangeBeginEndInfo Info = S.BuildCXXForRangeBeginEndVars(
+      Scope, cast<VarDecl>(RangeVar->getSingleDecl()), ColonLoc,
+      /*CoawaitLoc=*/{},
+      /*LifetimeExtendTemps=*/{}, Sema::BFRK_Build, VarIsConstexpr);
+
+  if (!Info.isValid())
+    return Data;
+
+  // CWG 3140: At runtime, we only need to evaluate 'begin', whereas 'end' is
+  // only used at compile-time; we'll rebuild it when we compute the expansion
+  // size, so only build 'begin' here.
+  StmtResult BeginStmt = S.ActOnDeclStmt(
+      S.ConvertDeclToDeclGroup(Info.BeginVar), ColonLoc, ColonLoc);
+  if (BeginStmt.isInvalid())
+    return Data;
+
+  auto BeginDRE = [&] {
+    return S.BuildDeclRefExpr(Info.BeginVar,
+                              Info.BeginVar->getType().getNonReferenceType(),
+                              VK_LValue, ColonLoc);
+  };
+
+  // Build 'constexpr auto iter = begin + decltype(begin - begin){i};'.
+  AttributeFactory AF;
+  DeclSpec DS{AF};
+  QualType BeginMinusBeginTy;
+  {
+    EnterExpressionEvaluationContext Unevaluated(
+        S, Sema::ExpressionEvaluationContext::Unevaluated, nullptr,
+        Sema::ExpressionEvaluationContextRecord::EK_Decltype);
+
+    // Build 'begin - begin'.
+    Expr *LHS = BeginDRE();
+    ExprResult BeginMinusBegin =
+        S.ActOnBinOp(Scope, ColonLoc, tok::minus, LHS, BeginDRE());
+    if (BeginMinusBegin.isInvalid()) {
+      // It is *not* obvious to the user what went wrong if we just print
+      // 'invalid operands to binary expression', so add a note that clarifies
+      // what operator we actually want here.
+      S.Diag(ColonLoc,
+             diag::note_iterating_expansion_stmt_iterator_requires_minus)
+          << LHS->getType();
+      return Data;
+    }
+
+    BeginMinusBegin = S.ActOnDecltypeExpression(BeginMinusBegin.get());
+    if (BeginMinusBegin.isInvalid())
+      return Data;
+
+    // Build 'decltype(begin - begin)'.
+    //
+    // It doesn't suffice to simply call 'getType()' on 'begin - begin' since
+    // it might be dependent, and we need the type to be resolved properly at
+    // instantiation time.
+    BeginMinusBeginTy = S.BuildDecltypeType(BeginMinusBegin.get());
+    if (BeginMinusBeginTy.isNull())
+      return Data;
+  }
+
+  // Build 'decltype(begin - begin){i}'.
+  ExprResult AddRHS = S.ActOnCXXTypeConstructExpr(
+      ParsedType::make(BeginMinusBeginTy), Index->getBeginLoc(),
+      MultiExprArg(&Index, 1), Index->getEndLoc(), /*ListInitialization=*/true);
+  if (AddRHS.isInvalid())
+    return Data;
+
+  // Build 'begin + decltype(begin - begin){i}'.
+  ExprResult BeginPlusI =
+      S.ActOnBinOp(Scope, ColonLoc, tok::plus, BeginDRE(), AddRHS.get());
+  if (BeginPlusI.isInvalid())
+    return Data;
+
+  // Store it in a variable.
+  // See also Sema::BuildCXXForRangeBeginEndVars().
+  const auto DepthStr = std::to_string(Scope->getDepth() / 2);
+  VarDecl *IterVar =
+      S.BuildForRangeVarDecl(ColonLoc, S.Context.getAutoDeductType(),
+                             std::string("__iter") + DepthStr, VarIsConstexpr);
+  S.AddInitializerToDecl(IterVar, BeginPlusI.get(), /*DirectInit=*/false);
+  if (IterVar->isInvalidDecl())
+    return Data;
+
+  StmtResult IterVarStmt =
+      S.ActOnDeclStmt(S.ConvertDeclToDeclGroup(IterVar), ColonLoc, ColonLoc);
+  if (IterVarStmt.isInvalid())
+    return Data;
+
+  // CWG 3149: Apply lifetime extension to iterating expansion statements.
+  S.ApplyForRangeOrExpansionStatementLifetimeExtension(
+      cast<VarDecl>(RangeVar->getSingleDecl()), LifetimeExtendTemps);
+
+  Data.BeginDecl = BeginStmt.getAs<DeclStmt>();
+  Data.RangeDecl = RangeVar;
+  Data.IterDecl = IterVarStmt.getAs<DeclStmt>();
+  Data.TheState = IterableExpansionStmtData::State::Ok;
+  return Data;
+}
+
 CXXExpansionStmtDecl *
 Sema::ActOnCXXExpansionStmtDecl(unsigned TemplateDepth,
                                 SourceLocation TemplateKWLoc) {
@@ -130,12 +340,27 @@ StmtResult Sema::ActOnCXXExpansionStmtPattern(
     // Note that lifetime extension only applies to destructuring expansion
     // statements, so we just ignore 'LifetimeExtendedTemps' entirely for other
     // types of expansion statements (this is CWG 3043).
+    //
+    // TODO: CWG 3131 makes it so the 'range' variable of an iterating
+    // expansion statement need no longer be 'constexpr'... so do we want
+    // lifetime extension for iterating expansion statements after all?
     return BuildCXXEnumeratingExpansionStmtPattern(ESD, Init, DS, LParenLoc,
                                                    ColonLoc, RParenLoc);
   }
 
-  Diag(ESD->getLocation(), diag::err_expansion_statements_todo);
-  return StmtError();
+  if (ExpansionInitializer->hasPlaceholderType()) {
+    ExprResult R = CheckPlaceholderExpr(ExpansionInitializer);
+    if (R.isInvalid())
+      return StmtError();
+    ExpansionInitializer = R.get();
+  }
+
+  if (DiagnoseUnexpandedParameterPack(ExpansionInitializer))
+    return StmtError();
+
+  return BuildNonEnumeratingCXXExpansionStmtPattern(
+      ESD, Init, DS, ExpansionInitializer, LParenLoc, ColonLoc, RParenLoc,
+      LifetimeExtendTemps);
 }
 
 StmtResult Sema::BuildCXXEnumeratingExpansionStmtPattern(
@@ -146,6 +371,74 @@ StmtResult Sema::BuildCXXEnumeratingExpansionStmtPattern(
       cast<DeclStmt>(ExpansionVar), LParenLoc, ColonLoc, RParenLoc);
 }
 
+StmtResult Sema::BuildNonEnumeratingCXXExpansionStmtPattern(
+    CXXExpansionStmtDecl *ESD, Stmt *Init, DeclStmt *ExpansionVarStmt,
+    Expr *ExpansionInitializer, SourceLocation LParenLoc,
+    SourceLocation ColonLoc, SourceLocation RParenLoc,
+    ArrayRef<MaterializeTemporaryExpr *> LifetimeExtendTemps) {
+  VarDecl *ExpansionVar = cast<VarDecl>(ExpansionVarStmt->getSingleDecl());
+
+  // Reject lambdas early.
+  if (auto *RD = ExpansionInitializer->getType()->getAsCXXRecordDecl();
+      RD && RD->isLambda()) {
+    Diag(ExpansionInitializer->getBeginLoc(), diag::err_expansion_stmt_lambda);
+    return StmtError();
+  }
+
+  if (ExpansionInitializer->isTypeDependent()) {
+    ActOnDependentForRangeInitializer(ExpansionVar, BFRK_Build);
+    return CXXExpansionStmtPattern::CreateDependent(
+        Context, ESD, Init, ExpansionVarStmt, ExpansionInitializer, LParenLoc,
+        ColonLoc, RParenLoc);
+  }
+
+  if (RequireCompleteType(ExpansionInitializer->getExprLoc(),
+                          ExpansionInitializer->getType(),
+                          diag::err_expansion_stmt_incomplete))
+    return StmtError();
+
+  if (ExpansionInitializer->getType()->isVariableArrayType()) {
+    Diag(ExpansionInitializer->getExprLoc(), diag::err_expansion_stmt_vla)
+        << ExpansionInitializer->getType();
+    return StmtError();
+  }
+
+  // Otherwise, if it can be an iterating expansion statement, it is one.
+  DeclRefExpr *Index = BuildIndexDRE(*this, ESD);
+  IterableExpansionStmtData Data = TryBuildIterableExpansionStmtInitializer(
+      *this, ExpansionInitializer, Index, ColonLoc, ExpansionVar->isConstexpr(),
+      LifetimeExtendTemps);
+  if (Data.hasError()) {
+    ActOnInitializerError(ExpansionVar);
+    return StmtError();
+  }
+
+  if (Data.isIterable()) {
+    // Build '*iter'.
+    auto *IterVar = cast<VarDecl>(Data.IterDecl->getSingleDecl());
+    DeclRefExpr *IterDRE = BuildDeclRefExpr(
+        IterVar, IterVar->getType().getNonReferenceType(), VK_LValue, ColonLoc);
+    ExprResult Deref =
+        ActOnUnaryOp(getCurScope(), ColonLoc, tok::star, IterDRE);
+    if (Deref.isInvalid()) {
+      ActOnInitializerError(ExpansionVar);
+      return StmtError();
+    }
+
+    Deref = MaybeCreateExprWithCleanups(Deref.get());
+
+    if (FinalizeExpansionVar(*this, ExpansionVar, Deref.get()))
+      return StmtError();
+
+    return CXXExpansionStmtPattern::CreateIterating(
+        Context, ESD, Init, ExpansionVarStmt, Data.RangeDecl, Data.BeginDecl,
+        Data.IterDecl, LParenLoc, ColonLoc, RParenLoc);
+  }
+
+  Diag(ESD->getLocation(), diag::err_expansion_statements_todo);
+  return StmtError();
+}
+
 StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
   if (!Exp || !Body)
     return StmtError();
@@ -155,9 +448,14 @@ StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
          "should not rebuild expansion statement after instantiation");
 
   Expansion->setBody(Body);
-  if (HasDependentSize(Expansion))
+  if (HasDependentSize(CurContext, Expansion))
     return Expansion;
 
+  // Now that we're expanding this, exit the context of the expansion stmt
+  // so that we no longer treat this as dependent.
+  ContextRAII CtxGuard(*this, CurContext->getParent(),
+                       /*NewThis=*/false);
+
   // This can fail if this is an iterating expansion statement.
   std::optional<uint64_t> NumInstantiations = ComputeExpansionSize(Expansion);
   if (!NumInstantiations)
@@ -168,7 +466,12 @@ StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
   if (Expansion->getInit())
     Shared.push_back(Expansion->getInit());
 
-  assert(Expansion->isEnumerating() && "TODO");
+  if (Expansion->isIterating()) {
+    Shared.push_back(Expansion->getRangeVarStmt());
+    Shared.push_back(Expansion->getBeginVarStmt());
+  } else {
+    assert(Expansion->isEnumerating() && "TODO");
+  }
 
   // Return an empty statement if the range is empty.
   if (*NumInstantiations == 0) {
@@ -179,21 +482,21 @@ StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
     return Expansion;
   }
 
-  // Create a compound statement binding the expansion variable and body.
-  Stmt *VarAndBody[] = {Expansion->getExpansionVarStmt(), Body};
+  // Create a compound statement binding the expansion variable and body,
+  // as well as the 'iter' variable if this is an iterating expansion statement.
+  SmallVector<Stmt *, 3> StmtsToInstantiate;
+  if (Expansion->isIterating())
+    StmtsToInstantiate.push_back(Expansion->getIterVarStmt());
+  StmtsToInstantiate.push_back(Expansion->getExpansionVarStmt());
+  StmtsToInstantiate.push_back(Body);
   Stmt *CombinedBody =
-      CompoundStmt::Create(Context, VarAndBody, FPOptionsOverride(),
+      CompoundStmt::Create(Context, StmtsToInstantiate, FPOptionsOverride(),
                            Body->getBeginLoc(), Body->getEndLoc());
 
   // Expand the body for each instantiation.
   SmallVector<Stmt *, 4> Instantiations;
   CXXExpansionStmtDecl *ESD = Expansion->getDecl();
   for (uint64_t I = 0; I < *NumInstantiations; ++I) {
-    // Now that we're expanding this, exit the context of the expansion stmt
-    // so that we no longer treat this as dependent.
-    ContextRAII CtxGuard(*this, CurContext->getParent(),
-                         /*NewThis=*/false);
-
     TemplateArgument Arg{Context, llvm::APSInt::get(I),
                          Context.getPointerDiffType()};
     MultiLevelTemplateArgumentList MTArgList(ESD, Arg, true);
@@ -237,5 +540,187 @@ Sema::ComputeExpansionSize(CXXExpansionStmtPattern *Expansion) {
         ->getRangeExpr()
         ->getNumInits();
 
+  // CWG 3131: N is the result of evaluating the expression
+  //
+  // [&] consteval {
+  //    std::ptrdiff_t result = 0;
+  //    auto b = begin-expr;
+  //    auto e = end-expr;
+  //    for (; b != e; ++b) ++result;
+  //    return result;
+  // }()
+  if (Expansion->isIterating()) {
+    SourceLocation Loc = Expansion->getColonLoc();
+    EnterExpressionEvaluationContext ExprEvalCtx(
+        *this, ExpressionEvaluationContext::ConstantEvaluated);
+
+    // This is mostly copied from ParseLambdaExpressionAfterIntroducer().
+    ParseScope LambdaScope(*this, Scope::LambdaScope | Scope::DeclScope |
+                                      Scope::FunctionDeclarationScope |
+                                      Scope::FunctionPrototypeScope);
+    AttributeFactory AttrFactory;
+    LambdaIntroducer Intro;
+    Intro.Range = SourceRange(Loc, Loc);
+    Intro.Default = LCD_ByRef; // CWG 3131
+    Intro.DefaultLoc = Loc;
+    DeclSpec DS(AttrFactory);
+    Declarator D(DS, ParsedAttributesView::none(),
+                 DeclaratorContext::LambdaExpr);
+    PushLambdaScope();
+    ActOnLambdaExpressionAfterIntroducer(Intro, getCurScope());
+
+    // Make the lambda 'consteval'.
+    {
+      ParseScope Prototype(*this, Scope::FunctionPrototypeScope |
+                                      Scope::FunctionDeclarationScope |
+                                      Scope::DeclScope);
+      const char *PrevSpec = nullptr;
+      unsigned DiagId = 0;
+      DS.SetConstexprSpec(ConstexprSpecKind::Consteval, Loc, PrevSpec, DiagId);
+      assert(DiagId == 0 && PrevSpec == nullptr);
+      ActOnLambdaClosureParameters(getCurScope(), /*ParamInfo=*/{});
+      ActOnLambdaClosureQualifiers(Intro, /*MutableLoc=*/SourceLocation());
+    }
+
+    ParseScope BodyScope(*this, Scope::BlockScope | Scope::FnScope |
+                                    Scope::DeclScope |
+                                    Scope::CompoundStmtScope);
+
+    ActOnStartOfLambdaDefinition(Intro, D, DS);
+
+    // Enter the compound statement that is the lambda body.
+    ActOnStartOfCompoundStmt(/*IsStmtExpr=*/false);
+    ActOnAfterCompoundStatementLeadingPragmas();
+    llvm::scope_exit PopScopesOnReturn{[&] {
+      ActOnFinishOfCompoundStmt();
+      ActOnLambdaError(Loc, getCurScope());
+    }};
+
+    // std::ptrdiff_t result = 0;
+    QualType PtrDiffT = Context.getPointerDiffType();
+    VarDecl *ResultVar = VarDecl::Create(
+        Context, CurContext, Loc, Loc, &PP.getIdentifierTable().get("__result"),
+        PtrDiffT, Context.getTrivialTypeSourceInfo(PtrDiffT, Loc), SC_None);
+    Expr *Zero = ActOnIntegerConstant(Loc, 0).get();
+    AddInitializerToDecl(ResultVar, Zero, false);
+    StmtResult ResultVarStmt =
+        ActOnDeclStmt(ConvertDeclToDeclGroup(ResultVar), Loc, Loc);
+    if (ResultVarStmt.isInvalid() || ResultVar->isInvalidDecl())
+      return std::nullopt;
+
+    // Start the for loop.
+    ParseScope ForScope(*this, Scope::DeclScope | Scope::ControlScope);
+
+    // auto b = begin-expr;
+    // auto e = end-expr;
+    ForRangeBeginEndInfo Info = BuildCXXForRangeBeginEndVars(
+        getCurScope(), Expansion->getRangeVar(), Loc,
+        /*CoawaitLoc=*/{},
+        /*LifetimeExtendTemps=*/{}, BFRK_Build, /*Constexpr=*/false);
+    if (!Info.isValid())
+      return std::nullopt;
+
+    StmtResult BeginStmt =
+        ActOnDeclStmt(ConvertDeclToDeclGroup(Info.BeginVar), Loc, Loc);
+    StmtResult EndStmt =
+        ActOnDeclStmt(ConvertDeclToDeclGroup(Info.EndVar), Loc, Loc);
+    if (BeginStmt.isInvalid() || EndStmt.isInvalid())
+      return std::nullopt;
+
+    // b != e
+    auto GetDeclRef = [&](VarDecl *VD) -> DeclRefExpr * {
+      return BuildDeclRefExpr(VD, VD->getType().getNonReferenceType(),
+                              VK_LValue, Loc);
+    };
+
+    DeclRefExpr *Begin = GetDeclRef(Info.BeginVar);
+    DeclRefExpr *End = GetDeclRef(Info.EndVar);
+    ExprResult NotEqual =
+        ActOnBinOp(getCurScope(), Loc, tok::exclaimequal, Begin, End);
+    if (NotEqual.isInvalid())
+      return std::nullopt;
+    ConditionResult Condition = ActOnCondition(
+        getCurScope(), Loc, NotEqual.get(), ConditionKind::Boolean,
+        /*MissingOk=*/false);
+    if (Condition.isInvalid())
+      return std::nullopt;
+
+    // ++b
+    Begin = GetDeclRef(Info.BeginVar);
+    ExprResult Increment =
+        ActOnUnaryOp(getCurScope(), Loc, tok::plusplus, Begin);
+    if (Increment.isInvalid())
+      return std::nullopt;
+    FullExprArg ThirdPart = MakeFullDiscardedValueExpr(Increment.get());
+
+    // Enter the body of the for loop.
+    ParseScope InnerScope(*this, Scope::DeclScope);
+    getCurScope()->decrementMSManglingNumber();
+
+    // ++result;
+    DeclRefExpr *ResultDeclRef = BuildDeclRefExpr(
+        ResultVar, ResultVar->getType().getNonReferenceType(), VK_LValue, Loc);
+    ExprResult IncrementResult =
+        ActOnUnaryOp(getCurScope(), Loc, tok::plusplus, ResultDeclRef);
+    if (IncrementResult.isInvalid())
+      return std::nullopt;
+    StmtResult IncrementStmt = ActOnExprStmt(IncrementResult.get());
+    if (IncrementStmt.isInvalid())
+      return std::nullopt;
+
+    // Exit the for loop.
+    InnerScope.Exit();
+    ForScope.Exit();
+    StmtResult ForLoop = ActOnForStmt(Loc, Loc, /*First=*/nullptr, Condition,
+                                      ThirdPart, Loc, IncrementStmt.get());
+    if (ForLoop.isInvalid())
+      return std::nullopt;
+
+    // return result;
+    ResultDeclRef = BuildDeclRefExpr(
+        ResultVar, ResultVar->getType().getNonReferenceType(), VK_LValue, Loc);
+    StmtResult Return = ActOnReturnStmt(Loc, ResultDeclRef, getCurScope());
+    if (Return.isInvalid())
+      return std::nullopt;
+
+    // Finally, we can build the compound statement that is the lambda body.
+    StmtResult LambdaBody =
+        ActOnCompoundStmt(Loc, Loc,
+                          {ResultVarStmt.get(), BeginStmt.get(), EndStmt.get(),
+                           ForLoop.get(), Return.get()},
+                          /*isStmtExpr=*/false);
+    if (LambdaBody.isInvalid())
+      return std::nullopt;
+
+    ActOnFinishOfCompoundStmt();
+    BodyScope.Exit();
+    LambdaScope.Exit();
+    PopScopesOnReturn.release();
+    ExprResult Lambda = ActOnLambdaExpr(Loc, LambdaBody.get());
+    if (Lambda.isInvalid())
+      return std::nullopt;
+
+    // Invoke the lambda.
+    ExprResult Call =
+        ActOnCallExpr(getCurScope(), Lambda.get(), Loc, /*ArgExprs=*/{}, Loc);
+    if (Call.isInvalid())
+      return std::nullopt;
+
+    Expr::EvalResult ER;
+    SmallVector<PartialDiagnosticAt, 4> Notes;
+    ER.Diag = &Notes;
+    if (!Call.get()->EvaluateAsInt(ER, Context)) {
+      Diag(Loc, diag::err_expansion_size_expr_not_ice);
+      for (const auto &[Location, PDiag] : Notes)
+        Diag(Location, PDiag);
+      return std::nullopt;
+    }
+
+    // It shouldn't be possible for this to be negative since we compute this
+    // via the built-in '++' on a ptrdiff_t.
+    assert(ER.Val.getInt().isNonNegative());
+    return ER.Val.getInt().getZExtValue();
+  }
+
   llvm_unreachable("TODO");
 }
diff --git a/clang/lib/Sema/SemaStmt.cpp b/clang/lib/Sema/SemaStmt.cpp
index 431d5ce39f28d..58010a695f8b9 100644
--- a/clang/lib/Sema/SemaStmt.cpp
+++ b/clang/lib/Sema/SemaStmt.cpp
@@ -2431,20 +2431,28 @@ void NoteForRangeBeginEndFunction(Sema &SemaRef, Expr *E,
   SemaRef.Diag(Loc, diag::note_for_range_begin_end)
     << BEF << IsTemplate << Description << E->getType();
 }
+}
 
 /// Build a variable declaration for a for-range statement.
-VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
-                              StringRef Name, bool ForExpansionStmt) {
-  DeclContext *DC = SemaRef.CurContext;
-  IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
-  TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
-  VarDecl *Decl = VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type,
-                                  TInfo, SC_None);
+VarDecl *Sema::BuildForRangeVarDecl(SourceLocation Loc, QualType Type,
+                                    StringRef Name, bool Constexpr) {
+  // Making the variable constexpr doesn't automatically add 'const' to the
+  // type, so do that now.
+  if (Constexpr && !Type->isReferenceType())
+    Type = Type.withConst();
+
+  DeclContext *DC = CurContext;
+  IdentifierInfo *II = &PP.getIdentifierTable().get(Name);
+  TypeSourceInfo *TInfo = Context.getTrivialTypeSourceInfo(Type, Loc);
+  VarDecl *Decl =
+      VarDecl::Create(Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
   Decl->setImplicit();
   Decl->setCXXForRangeImplicitVar(true);
+  if (Constexpr)
+    // CWG 3044: Do not make the variable 'static'.
+    Decl->setConstexpr(true);
   return Decl;
 }
-}
 
 static bool ObjCEnumerationCollection(Expr *Collection) {
   return !Collection->isTypeDependent()
@@ -2458,7 +2466,7 @@ StmtResult Sema::BuildCXXForRangeRangeVar(Scope *S, Expr *Range, QualType Type,
   const auto DepthStr = std::to_string(S->getDepth() / 2);
   SourceLocation RangeLoc = Range->getBeginLoc();
   VarDecl *RangeVar = BuildForRangeVarDecl(
-      *this, RangeLoc, Type, std::string("__range") + DepthStr, Constexpr);
+      RangeLoc, Type, std::string("__range") + DepthStr, Constexpr);
   if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc,
                             diag::err_for_range_deduction_failure))
 
@@ -2757,12 +2765,10 @@ Sema::ForRangeBeginEndInfo Sema::BuildCXXForRangeBeginEndVars(
   // Build auto __begin = begin-expr, __end = end-expr.
   // Divide by 2, since the variables are in the inner scope (loop body).
   const auto DepthStr = std::to_string(S->getDepth() / 2);
-  VarDecl *BeginVar =
-      BuildForRangeVarDecl(*this, ColonLoc, AutoType,
-                           std::string("__begin") + DepthStr, Constexpr);
-  VarDecl *EndVar =
-      BuildForRangeVarDecl(*this, ColonLoc, AutoType,
-                           std::string("__end") + DepthStr, Constexpr);
+  VarDecl *BeginVar = BuildForRangeVarDecl(
+      ColonLoc, AutoType, std::string("__begin") + DepthStr, Constexpr);
+  VarDecl *EndVar = BuildForRangeVarDecl(
+      ColonLoc, AutoType, std::string("__end") + DepthStr, Constexpr);
 
   // Build begin-expr and end-expr and attach to __begin and __end variables.
   ExprResult BeginExpr, EndExpr;
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 42c094fc2e94d..864667d784e6d 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -9397,6 +9397,40 @@ StmtResult TreeTransform<Derived>::TransformCXXExpansionStmtPattern(
     Init = SR.get();
   }
 
+  // Collect lifetime-extended temporaries in case this ends up being a
+  // destructuring or iterating expansion statement.
+  //
+  // CWG 3140: Additionally, for iterating expansions statements, we need to
+  // apply lifetime extension to the initializer of the range.
+  ExprResult ExpansionInitializer;
+  StmtResult Range;
+  SmallVector<MaterializeTemporaryExpr *, 8> LifetimeExtendTemps;
+  if (S->isDependent() || S->isIterating()) {
+    EnterExpressionEvaluationContext ExprEvalCtx(
+        SemaRef, SemaRef.currentEvaluationContext().Context);
+    SemaRef.currentEvaluationContext().InLifetimeExtendingContext = true;
+    SemaRef.currentEvaluationContext().RebuildDefaultArgOrDefaultInit = true;
+
+    if (S->isDependent()) {
+      // The expansion initializer should not be in the context of the expansion
+      // statement because it isn't instantiated when the expansion statement is
+      // expanded.
+      Sema::ContextRAII CtxGuard(SemaRef, SemaRef.CurContext->getParent(),
+                                 /*NewThis=*/false);
+      ExpansionInitializer =
+          getDerived().TransformExpr(S->getExpansionInitializer());
+      if (ExpansionInitializer.isInvalid())
+        return StmtError();
+    } else if (S->isIterating()) {
+      Range = TransformStmtInParentContext(S->getRangeVarStmt());
+      if (Range.isInvalid())
+        return StmtError();
+    }
+
+    LifetimeExtendTemps =
+        SemaRef.currentEvaluationContext().ForRangeLifetimeExtendTemps;
+  }
+
   CXXExpansionStmtPattern *NewPattern = nullptr;
   if (S->isEnumerating()) {
     StmtResult ExpansionVar =
@@ -9407,6 +9441,43 @@ StmtResult TreeTransform<Derived>::TransformCXXExpansionStmtPattern(
     NewPattern = CXXExpansionStmtPattern::CreateEnumerating(
         SemaRef.Context, NewESD, Init, ExpansionVar.getAs<DeclStmt>(),
         S->getLParenLoc(), S->getColonLoc(), S->getRParenLoc());
+  } else if (S->isIterating()) {
+    StmtResult Begin = TransformStmtInParentContext(S->getBeginVarStmt());
+    StmtResult Iter = TransformStmtInParentContext(S->getIterVarStmt());
+    if (Begin.isInvalid() || Iter.isInvalid())
+      return StmtError();
+
+    // The expansion variable is part of the pattern only and never ends
+    // up in the instantiations, so keep it in the expansion statement's
+    // DeclContext.
+    StmtResult ExpansionVar =
+        getDerived().TransformStmt(S->getExpansionVarStmt());
+    if (ExpansionVar.isInvalid())
+      return StmtError();
+
+    NewPattern = CXXExpansionStmtPattern::CreateIterating(
+        SemaRef.Context, NewESD, Init, ExpansionVar.getAs<DeclStmt>(),
+        Range.getAs<DeclStmt>(), Begin.getAs<DeclStmt>(),
+        Iter.getAs<DeclStmt>(), S->getLParenLoc(), S->getColonLoc(),
+        S->getRParenLoc());
+
+    SemaRef.ApplyForRangeOrExpansionStatementLifetimeExtension(
+        NewPattern->getRangeVar(), LifetimeExtendTemps);
+  } else if (S->isDependent()) {
+    StmtResult ExpansionVar =
+        getDerived().TransformStmt(S->getExpansionVarStmt());
+    if (ExpansionVar.isInvalid())
+      return StmtError();
+
+    StmtResult Res = SemaRef.BuildNonEnumeratingCXXExpansionStmtPattern(
+        NewESD, Init, ExpansionVar.getAs<DeclStmt>(),
+        ExpansionInitializer.get(), S->getLParenLoc(), S->getColonLoc(),
+        S->getRParenLoc(), LifetimeExtendTemps);
+
+    if (Res.isInvalid())
+      return StmtError();
+
+    NewPattern = cast<CXXExpansionStmtPattern>(Res.get());
   } else {
     llvm_unreachable("TODO");
   }



More information about the llvm-branch-commits mailing list