cor3ntin created this revision.
Herald added a project: All.
cor3ntin requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151094

Files:
  clang/docs/ReleaseNotes.rst
  clang/include/clang/AST/Decl.h
  clang/include/clang/AST/DeclBase.h
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/include/clang/Sema/ScopeInfo.h
  clang/include/clang/Sema/Sema.h
  clang/lib/AST/Decl.cpp
  clang/lib/AST/VTableBuilder.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Frontend/InitPreprocessor.cpp
  clang/lib/Parse/ParseDecl.cpp
  clang/lib/Parse/ParseDeclCXX.cpp
  clang/lib/Sema/ScopeInfo.cpp
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclCXX.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/lib/Sema/SemaLambda.cpp
  clang/lib/Sema/SemaStmt.cpp
  clang/lib/Sema/SemaTemplateDeduction.cpp
  clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
  clang/test/SemaCXX/cxx2a-consteval-default-params.cpp
  clang/test/SemaCXX/cxx2a-consteval.cpp
  clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
  clang/www/cxx_status.html

Index: clang/www/cxx_status.html
===================================================================
--- clang/www/cxx_status.html
+++ clang/www/cxx_status.html
@@ -363,7 +363,7 @@
     <tr>
       <td>consteval needs to propagate up</td>
       <td><a href="https://wg21.link/P2564R3";>P2564R3</a> (<a href="#dr">DR</a>)</td>
-      <td class="none" align="center">No</td>
+      <td class="unreleased" align="center">Clang 17</td>
     </tr>
     <tr>
       <td>Lifetime extension in range-based for loops</td>
Index: clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/cxx2b-consteval-propagate.cpp
@@ -0,0 +1,130 @@
+
+// RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value %s -verify
+// RUN: %clang_cc1 -std=c++2b -emit-llvm-only -Wno-unused-value %s -verify
+
+consteval int id(int i) { return i; }
+constexpr char id(char c) { return c; }
+
+namespace examples {
+
+template <typename T>
+constexpr int f(T t) { // expected-note {{declared here}}
+    return t + id(t);
+}
+auto a = &f<char>; // ok, f<char> is not an immediate function
+auto b = &f<int>;  // expected-error {{cannot take address of immediate function 'f<int>' outside of an immediate invocation}}
+
+static_assert(f(3) == 6); // ok
+
+template <typename T>
+constexpr int g(T t) {    // g<int> is not an immediate function
+    return t + id(42);    // because id(42) is already a constant
+}
+
+template <typename T, typename F>
+constexpr bool is_not(T t, F f) {
+    return not f(t);
+}
+
+consteval bool is_even(int i) { return i % 2 == 0; }
+
+static_assert(is_not(5, is_even));
+
+int x = 0; // expected-note {{declared here}}
+
+template <typename T>
+constexpr T h(T t = id(x)) { // expected-note {{read of non-const variable 'x' is not allowed in a constant expression}}
+    return t;
+}
+
+template <typename T>
+constexpr T hh() {           // hh<int> is an immediate function
+    return h<T>();
+}
+
+int i = hh<int>(); // expected-error {{call to consteval function 'examples::hh<int>' is not a constant expression}} \
+                   // expected-note {{in call to 'hh()'}}
+
+struct A {
+  int x;
+  int y = id(x);
+};
+
+template <typename T>
+constexpr int k(int) {
+  return A(42).y;
+}
+
+}
+
+namespace e2{
+template <typename T>
+constexpr int f(T t);
+auto a = &f<char>;
+auto b = &f<int>;
+}
+
+namespace forward_declare_constexpr{
+template <typename T>
+constexpr int f(T t);
+
+auto a = &f<char>;
+auto b = &f<int>;
+
+template <typename T>
+constexpr int f(T t) {
+    return id(0);
+}
+}
+
+namespace forward_declare_consteval{
+template <typename T>
+constexpr int f(T t);  // expected-note {{'f<int>' defined here}}
+
+auto a = &f<char>;
+auto b = &f<int>; // expected-error {{immediate function 'f<int>' used before it is defined}} \
+                  // expected-note {{in instantiation of function template specialization}}
+
+template <typename T>
+constexpr int f(T t) {
+    return id(t); // expected-note {{'f<int>' is an immediate function because its body contains a call to a consteval function 'id' and that call is not a constant expression}}
+}
+}
+
+namespace constructors {
+consteval int f(int) {
+  return 0;
+}
+struct S {
+  constexpr S(auto i) {
+    f(i);
+  }
+};
+constexpr void g(auto i) {
+  [[maybe_unused]] S s{i};
+}
+void test() {
+  g(0);
+}
+}
+
+namespace aggregate {
+consteval int f(int);
+struct S{
+    int a = 0;
+    int b = f(a);
+};
+
+constexpr bool test(auto i) {
+    S s{i};
+    return s.b == 2 *i;
+}
+consteval int f(int i) {
+    return 2 * i;
+}
+
+void test() {
+    static_assert(test(42));
+}
+
+}
Index: clang/test/SemaCXX/cxx2a-consteval.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval.cpp
+++ clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -250,14 +250,14 @@
   return f(i);
 };
 
-auto l1 = [](int i) constexpr {
-// expected-note@-1 {{declared here}}
+auto l1 = [](int i) constexpr { // expected-error{{cannot take address of immediate call operator}} \
+                                // expected-note {{declared here}}
   int t = f(i);
-// expected-error@-1 {{is not a constant expression}}
-// expected-note@-2 {{function parameter}}
-  return f(0);  
+  return f(0);
 };
 
+int(*test)(int)  = l1;
+
 }
 
 namespace std {
Index: clang/test/SemaCXX/cxx2a-consteval-default-params.cpp
===================================================================
--- clang/test/SemaCXX/cxx2a-consteval-default-params.cpp
+++ clang/test/SemaCXX/cxx2a-consteval-default-params.cpp
@@ -1,21 +1,13 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++23 %s
 
-consteval int undefined();  // expected-note 4 {{declared here}}
+consteval int undefined();  // expected-note 2 {{declared here}}
 
 void check_lambdas_unused(
-    int a = []
-    {
-        // The body of a lambda is not a subexpression of the lambda
-        // so this is immediately evaluated even if the parameter
-        // is never used.
-        return undefined();  // expected-error {{not a constant expression}} \
-                             // expected-note  {{undefined function 'undefined'}}
-    }(),
-    int b = [](int no_error = undefined()) {
+    int a = [](int no_error = undefined()) {
         return no_error;
     }(0),
-    int c = [](int defaulted = undefined()) {
+    int b = [](int defaulted = undefined()) {
         return defaulted;
     }()
 ) {}
@@ -40,8 +32,7 @@
 
 struct UnusedInitWithLambda {
     int a = [] {
-        return undefined();  // expected-error {{not a constant expression}} \
-                             // expected-note  {{undefined function 'undefined'}}
+        return undefined(); // never evaluated because immediate escalating
     }();
     // UnusedInitWithLambda is never constructed, so the initializer
     // of b and undefined() are never evaluated.
@@ -50,22 +41,19 @@
     }();
 };
 
-consteval int ub(int n) {
-    return 0/n; // expected-note  {{division}}
+consteval int ub(int n) { // expected-note {{declared here}}
+    return 0/n;
 }
 
 struct InitWithLambda {
-    int b = [](int error = undefined()) { // expected-error {{not a constant expression}} \
-                              // expected-note  {{declared here}} \
-                              // expected-note  {{undefined function 'undefined'}}
+    int b = [](int error = undefined()) { // expected-error {{cannot take address of consteval function 'undefined' outside of an immediate invocation}}
         return error;
-    }(); // expected-note {{in the default initalizer of 'error'}}
-    int c = [](int error = sizeof(undefined()) + ub(0)) { // expected-error {{'ub' is not a constant expression}} \
-                                                          // expected-note  {{declared here}} \
-                                                          // expected-note {{in call to 'ub(0)}}
+    }();
+    int c = [](int error = sizeof(undefined()) + ub(0)) { // expected-error {{cannot take address of consteval function 'ub' outside of an immediate invocation}}
+
         return error;
-    }(); // expected-note {{in the default initalizer of 'error'}}
-} i; // expected-note {{in implicit default constructor}}
+    }();
+} i;
 
 namespace ShouldNotCrash {
     template<typename T>
Index: clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
===================================================================
--- clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
+++ clang/test/CodeGenCXX/cxx20-consteval-crash.cpp
@@ -17,7 +17,7 @@
 // This code would previously cause a crash.
 struct X { int val; };
 consteval X g() { return {0}; }
-void f() { g(); }
+void f() { (void)g(); }
 
 // CHECK: define dso_local void @_ZN7PR514841fEv() #1 {
 // CHECK: entry:
Index: clang/lib/Sema/SemaTemplateDeduction.cpp
===================================================================
--- clang/lib/Sema/SemaTemplateDeduction.cpp
+++ clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -4382,6 +4382,11 @@
       DeduceReturnType(Specialization, Info.getLocation(), false))
     return TDK_MiscellaneousDeductionFailure;
 
+  if (IsAddressOfFunction && Specialization->isImmediateEscalating() &&
+      CheckIfFunctionSpecializationIsImmediate(Specialization,
+                                               Info.getLocation()))
+    return TDK_MiscellaneousDeductionFailure;
+
   // If the function has a dependent exception specification, resolve it now,
   // so we can check that the exception specification matches.
   auto *SpecializationFPT =
@@ -5002,6 +5007,35 @@
   return StillUndeduced;
 }
 
+bool Sema::CheckIfFunctionSpecializationIsImmediate(FunctionDecl *FD,
+                                                    SourceLocation Loc) {
+  assert(FD->isImmediateEscalating());
+
+  if (isLambdaConversionOperator(FD)) {
+    CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent();
+    FunctionDecl *CallOp = Lambda->getLambdaCallOperator();
+
+    // For a generic lambda, instantiate the call operator if needed.
+    if (auto *Args = FD->getTemplateSpecializationArgs()) {
+      CallOp = InstantiateFunctionDeclaration(
+          CallOp->getDescribedFunctionTemplate(), Args, Loc);
+      if (!CallOp || CallOp->isInvalidDecl())
+        return true;
+      runWithSufficientStackSpace(
+          Loc, [&] { InstantiateFunctionDefinition(Loc, CallOp); });
+    }
+    if (CallOp->isInvalidDecl())
+      return true;
+    return false;
+  }
+
+  if (FD->getTemplateInstantiationPattern()) {
+    runWithSufficientStackSpace(
+        Loc, [&] { InstantiateFunctionDefinition(Loc, FD); });
+  }
+  return false;
+}
+
 /// If this is a non-static member function,
 static void
 AddImplicitObjectParameterType(ASTContext &Context,
Index: clang/lib/Sema/SemaStmt.cpp
===================================================================
--- clang/lib/Sema/SemaStmt.cpp
+++ clang/lib/Sema/SemaStmt.cpp
@@ -932,11 +932,12 @@
   }
 
   if (ConstevalOrNegatedConsteval) {
-    bool Immediate = isImmediateFunctionContext();
+    bool Immediate = ExprEvalContexts.back().Context ==
+                     ExpressionEvaluationContext::ImmediateFunctionContext;
     if (CurContext->isFunctionOrMethod()) {
       const auto *FD =
           dyn_cast<FunctionDecl>(Decl::castFromDeclContext(CurContext));
-      if (FD && FD->isConsteval())
+      if (FD && FD->isImmediateFunction())
         Immediate = true;
     }
     if (isUnevaluatedContext() || Immediate)
@@ -4743,6 +4744,7 @@
 
   PushExpressionEvaluationContext(
       ExpressionEvaluationContext::PotentiallyEvaluated);
+  ExprEvalContexts.back().InImmediateEscalatingFunctionContext = false;
 }
 
 void Sema::ActOnCapturedRegionStart(SourceLocation Loc, Scope *CurScope,
Index: clang/lib/Sema/SemaLambda.cpp
===================================================================
--- clang/lib/Sema/SemaLambda.cpp
+++ clang/lib/Sema/SemaLambda.cpp
@@ -1422,6 +1422,10 @@
       LSI->CallOperator->isConsteval()
           ? ExpressionEvaluationContext::ImmediateFunctionContext
           : ExpressionEvaluationContext::PotentiallyEvaluated);
+  ExprEvalContexts.back().InImmediateFunctionContext =
+      LSI->CallOperator->isConsteval();
+  ExprEvalContexts.back().InImmediateEscalatingFunctionContext =
+      LSI->CallOperator->isImmediateEscalating();
 }
 
 void Sema::ActOnLambdaError(SourceLocation StartLoc, Scope *CurScope,
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -5923,8 +5923,9 @@
   //   is a function parameter scope of an immediate function.
   EnterExpressionEvaluationContext EvalContext(
       *this,
-      FD->isConsteval() ? ExpressionEvaluationContext::ImmediateFunctionContext
-                        : ExpressionEvaluationContext::PotentiallyEvaluated,
+      FD->isImmediateFunction()
+          ? ExpressionEvaluationContext::ImmediateFunctionContext
+          : ExpressionEvaluationContext::PotentiallyEvaluated,
       Param);
   ExprEvalContexts.back().IsCurrentlyCheckingDefaultArgumentOrInitializer =
       SkipImmediateInvocations;
@@ -5935,13 +5936,21 @@
 }
 
 struct ImmediateCallVisitor : public RecursiveASTVisitor<ImmediateCallVisitor> {
+  const ASTContext &Context;
+  ImmediateCallVisitor(const ASTContext &Ctx) : Context(Ctx) {}
+
   bool HasImmediateCalls = false;
+  bool IsImmediateInvocation = false;
 
   bool shouldVisitImplicitCode() const { return true; }
 
   bool VisitCallExpr(CallExpr *E) {
-    if (const FunctionDecl *FD = E->getDirectCallee())
-      HasImmediateCalls |= FD->isConsteval();
+    if (const FunctionDecl *FD = E->getDirectCallee()) {
+      HasImmediateCalls |= FD->isImmediateFunction();
+      if (FD->isConsteval() && !E->isCXX11ConstantExpr(Context))
+        IsImmediateInvocation = true;
+    }
+
     return RecursiveASTVisitor<ImmediateCallVisitor>::VisitStmt(E);
   }
 
@@ -6020,7 +6029,7 @@
     //   scope is a function parameter scope of an immediate function.
     EnterExpressionEvaluationContext EvalContext(
         *this,
-        FD->isConsteval()
+        FD->isImmediateFunction()
             ? ExpressionEvaluationContext::ImmediateFunctionContext
             : ExpressionEvaluationContext::PotentiallyEvaluated,
         Param);
@@ -6033,7 +6042,7 @@
     // An immediate invocation that is not evaluated where it appears is
     // evaluated and checked for whether it is a constant expression at the
     // point where the enclosing initializer is used in a function call.
-    ImmediateCallVisitor V;
+    ImmediateCallVisitor V(getASTContext());
     if (!NestedDefaultChecking)
       V.TraverseDecl(Param);
     if (V.HasImmediateCalls) {
@@ -6114,10 +6123,17 @@
   // evaluated and checked for whether it is a constant expression at the
   // point where the enclosing initializer is used in a [...] a constructor
   // definition, or an aggregate initialization.
-  ImmediateCallVisitor V;
+  ImmediateCallVisitor V(getASTContext());
   if (!NestedDefaultChecking)
     V.TraverseDecl(Field);
   if (V.HasImmediateCalls) {
+    // C++23 [expr.const]/p15
+    // An aggregate initialization is an immediate invocation
+    // if it evaluates a default member initializer that has a subexpression
+    // that is an immediate-escalating expression.
+    ExprEvalContexts.back().InImmediateFunctionContext |=
+        V.IsImmediateInvocation;
+
     ExprEvalContexts.back().DelayedDefaultInitializationContext = {Loc, Field,
                                                                    CurContext};
     ExprEvalContexts.back().IsCurrentlyCheckingDefaultArgumentOrInitializer =
@@ -17844,9 +17860,17 @@
   ExprEvalContexts.back().InDiscardedStatement =
       ExprEvalContexts[ExprEvalContexts.size() - 2]
           .isDiscardedStatementContext();
+
+  // C++23 [expr.const]/p15
+  // An expression or conversion is in an immediate function context if [...]
+  // it is a subexpression of a manifestly constant-evaluated expression or
+  // conversion.
+  const auto &Prev = ExprEvalContexts[ExprEvalContexts.size() - 2];
   ExprEvalContexts.back().InImmediateFunctionContext =
-      ExprEvalContexts[ExprEvalContexts.size() - 2]
-          .isImmediateFunctionContext();
+      Prev.isImmediateFunctionContext() || Prev.isConstantEvaluated();
+
+  ExprEvalContexts.back().InImmediateEscalatingFunctionContext =
+      Prev.InImmediateEscalatingFunctionContext;
 
   Cleanup.reset();
   if (!MaybeODRUseExprs.empty())
@@ -17925,9 +17949,18 @@
   }
 }
 
+bool Sema::CheckImmediateEscalatingExpression(Expr *E) {
+  if (!LangOpts.CPlusPlus20 ||
+      !ExprEvalContexts.back().InImmediateEscalatingFunctionContext ||
+      FunctionScopes.empty())
+    return false;
+  FunctionScopes.back()->ImmediateEscalatingExpression = E;
+  return true;
+}
+
 ExprResult Sema::CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl) {
   if (isUnevaluatedContext() || !E.isUsable() || !Decl ||
-      !Decl->isConsteval() || isConstantEvaluated() ||
+      !Decl->isImmediateFunction() || isConstantEvaluated() ||
       isCheckingDefaultArgumentOrInitializer() ||
       RebuildingImmediateInvocation || isImmediateFunctionContext())
     return E;
@@ -17941,6 +17974,18 @@
             dyn_cast<DeclRefExpr>(Call->getCallee()->IgnoreImplicit()))
       ExprEvalContexts.back().ReferenceToConsteval.erase(DeclRef);
 
+  // C++23 [expr.const]/p16
+  // An expression or conversion is immediate-escalating if it is not initially
+  // in an immediate function context and it is [...] an immediate invocation
+  // that is not a constant expression and is not a subexpression of an
+  // immediate invocation.
+  if (!E.get()->isValueDependent() &&
+      !E.get()->isCXX11ConstantExpr(getASTContext()) &&
+      ExprEvalContexts.back().InImmediateEscalatingFunctionContext &&
+      CheckImmediateEscalatingExpression(E.get())) {
+    return E;
+  }
+
   E = MaybeCreateExprWithCleanups(E);
 
   ConstantExpr *Res = ConstantExpr::Create(
@@ -17975,7 +18020,7 @@
       FD = Call->getConstructor();
     else
       llvm_unreachable("unhandled decl kind");
-    assert(FD && FD->isConsteval());
+    assert(FD && FD->isImmediateFunction());
     SemaRef.Diag(CE->getBeginLoc(), diag::err_invalid_consteval_call) << FD;
     if (auto Context =
             SemaRef.InnermostDeclarationWithDelayedImmediateInvocations()) {
@@ -18128,13 +18173,32 @@
     if (!CE.getInt())
       EvaluateAndDiagnoseImmediateInvocation(SemaRef, CE);
   for (auto *DR : Rec.ReferenceToConsteval) {
+    bool IsConsteval = cast<FunctionDecl>(DR->getDecl())->isConsteval();
     NamedDecl *ND = cast<FunctionDecl>(DR->getDecl());
     if (auto *MD = llvm::dyn_cast<CXXMethodDecl>(ND);
         MD && (MD->isLambdaStaticInvoker() || isLambdaCallOperator(MD)))
       ND = MD->getParent();
-    SemaRef.Diag(DR->getBeginLoc(), diag::err_invalid_consteval_take_address)
-        << ND << isa<CXXRecordDecl>(ND);
-    SemaRef.Diag(ND->getLocation(), diag::note_declared_at);
+
+    // C++23 [expr.const]/p16
+    // An expression or conversion is immediate-escalating if it is not
+    // initially in an immediate function context and it is [...] a
+    // potentially-evaluated id-expression that denotes an immediate function
+    // that is not a subexpression of an immediate invocation.
+    bool ImmediateEscalating = false;
+    bool IsPotentiallyEvaluated =
+        Rec.Context ==
+            Sema::ExpressionEvaluationContext::PotentiallyEvaluated ||
+        Rec.Context ==
+            Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed;
+    if (SemaRef.inTemplateInstantiation() && IsPotentiallyEvaluated)
+      ImmediateEscalating = SemaRef.CheckImmediateEscalatingExpression(DR);
+
+    if (!Rec.InImmediateEscalatingFunctionContext ||
+        (SemaRef.inTemplateInstantiation() && !ImmediateEscalating)) {
+      SemaRef.Diag(DR->getBeginLoc(), diag::err_invalid_consteval_take_address)
+          << ND << isa<CXXRecordDecl>(ND) << IsConsteval;
+      SemaRef.Diag(ND->getLocation(), diag::note_declared_at);
+    }
   }
 }
 
@@ -20212,12 +20276,14 @@
         !Method->getDevirtualizedMethod(Base, getLangOpts().AppleKext))
       OdrUse = false;
 
-  if (auto *FD = dyn_cast<FunctionDecl>(E->getDecl()))
+  if (auto *FD = dyn_cast<FunctionDecl>(E->getDecl())) {
     if (!isUnevaluatedContext() && !isConstantEvaluated() &&
         !isImmediateFunctionContext() &&
-        !isCheckingDefaultArgumentOrInitializer() && FD->isConsteval() &&
+        !isCheckingDefaultArgumentOrInitializer() &&
+        FD->isImmediateFunction() && //
         !RebuildingImmediateInvocation && !FD->isDependentContext())
       ExprEvalContexts.back().ReferenceToConsteval.insert(E);
+  }
   MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E, OdrUse,
                      RefsMinusAssignments);
 }
Index: clang/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- clang/lib/Sema/SemaDeclCXX.cpp
+++ clang/lib/Sema/SemaDeclCXX.cpp
@@ -2437,6 +2437,32 @@
   return true;
 }
 
+bool Sema::CheckImmediateEscalatingFunctionDefinition(
+    FunctionDecl *FD, Expr *ImmediateEscalatingExpression) {
+  if (!FD->hasBody() || !FD->isImmediateEscalating() ||
+      !getLangOpts().CPlusPlus20)
+    return true;
+  FD->setBodyContainsImmediateEscalatingExpressions(
+      ImmediateEscalatingExpression != nullptr);
+  if (ImmediateEscalatingExpression) {
+    auto it = UndefinedButUsed.find(FD->getCanonicalDecl());
+    if (it != UndefinedButUsed.end()) {
+      Diag(it->second, diag::err_immediate_function_used_before_definition)
+          << it->first;
+      Diag(FD->getLocation(), diag::note_defined_here) << FD;
+      if (auto *CE = llvm::dyn_cast<CallExpr>(
+              ImmediateEscalatingExpression->IgnoreImplicit())) {
+        Diag(CE->getBeginLoc(), diag::note_immediate_function_reason)
+            << FD << CE->getDirectCallee()
+            << CE->getDirectCallee()->isConsteval() << 1
+            << CE->getSourceRange();
+      }
+      return false;
+    }
+  }
+  return true;
+}
+
 /// Get the class that is directly named by the current context. This is the
 /// class for which an unqualified-id in this scope could name a constructor
 /// or destructor.
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -15183,6 +15183,8 @@
   // function is entered, we need to reset this tracking, since the entered
   // function might be not an immediate function.
   ExprEvalContexts.back().InImmediateFunctionContext = FD->isConsteval();
+  ExprEvalContexts.back().InImmediateEscalatingFunctionContext =
+      FD->isImmediateEscalating();
 
   // Check for defining attributes before the check for redefinition.
   if (const auto *Attr = FD->getAttr<AliasAttr>()) {
@@ -15492,10 +15494,11 @@
     // one is already popped when finishing the lambda in BuildLambdaExpr().
     // This is meant to pop the context added in ActOnStartOfFunctionDef().
     ExitFunctionBodyRAII ExitRAII(*this, isLambdaCallOperator(FD));
-
     if (FD) {
       FD->setBody(Body);
       FD->setWillHaveBody(false);
+      CheckImmediateEscalatingFunctionDefinition(
+          FD, FSI->ImmediateEscalatingExpression);
 
       if (getLangOpts().CPlusPlus14) {
         if (!FD->isInvalidDecl() && Body && !FD->isDependentContext() &&
Index: clang/lib/Sema/ScopeInfo.cpp
===================================================================
--- clang/lib/Sema/ScopeInfo.cpp
+++ clang/lib/Sema/ScopeInfo.cpp
@@ -57,6 +57,7 @@
   Blocks.clear();
   ByrefBlockVars.clear();
   AddrLabels.clear();
+  ImmediateEscalatingExpression = nullptr;
 }
 
 static const NamedDecl *getBestPropertyDecl(const ObjCPropertyRefExpr *PropE) {
Index: clang/lib/Parse/ParseDeclCXX.cpp
===================================================================
--- clang/lib/Parse/ParseDeclCXX.cpp
+++ clang/lib/Parse/ParseDeclCXX.cpp
@@ -3195,6 +3195,7 @@
           ? Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed
           : Sema::ExpressionEvaluationContext::PotentiallyEvaluated,
       D);
+  Actions.ExprEvalContexts.back().InImmediateEscalatingFunctionContext = true;
   if (TryConsumeToken(tok::equal, EqualLoc)) {
     if (Tok.is(tok::kw_delete)) {
       // In principle, an initializer of '= delete p;' is legal, but it will
Index: clang/lib/Parse/ParseDecl.cpp
===================================================================
--- clang/lib/Parse/ParseDecl.cpp
+++ clang/lib/Parse/ParseDecl.cpp
@@ -24,7 +24,6 @@
 #include "clang/Sema/EnterExpressionEvaluationContext.h"
 #include "clang/Sema/Lookup.h"
 #include "clang/Sema/ParsedTemplate.h"
-#include "clang/Sema/Scope.h"
 #include "clang/Sema/SemaDiagnostic.h"
 #include "llvm/ADT/SmallSet.h"
 #include "llvm/ADT/SmallString.h"
@@ -2493,6 +2492,8 @@
         Diag(ConsumeToken(), diag::err_default_special_members)
             << getLangOpts().CPlusPlus20;
     } else {
+      EnterExpressionEvaluationContext Ctx(
+          Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
       InitializerScopeRAII InitScope(*this, D, ThisDecl);
 
       if (Tok.is(tok::code_completion)) {
Index: clang/lib/Frontend/InitPreprocessor.cpp
===================================================================
--- clang/lib/Frontend/InitPreprocessor.cpp
+++ clang/lib/Frontend/InitPreprocessor.cpp
@@ -683,7 +683,7 @@
     // Refer to the discussion of this at https://reviews.llvm.org/D128619.
     Builder.defineMacro("__cpp_concepts", "201907L");
     Builder.defineMacro("__cpp_conditional_explicit", "201806L");
-    //Builder.defineMacro("__cpp_consteval", "201811L");
+    // Builder.defineMacro("__cpp_consteval", "202211L");
     Builder.defineMacro("__cpp_constexpr_dynamic_alloc", "201907L");
     Builder.defineMacro("__cpp_constinit", "201907L");
     Builder.defineMacro("__cpp_impl_coroutine", "201902L");
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -4211,7 +4211,7 @@
                                                  bool ForVTable,
                                                  bool DontDefer,
                                               ForDefinition_t IsForDefinition) {
-  assert(!cast<FunctionDecl>(GD.getDecl())->isConsteval() &&
+  assert(!cast<FunctionDecl>(GD.getDecl())->isImmediateFunction() &&
          "consteval function should never be emitted");
   // If there was no specific requested type, just convert it now.
   if (!Ty) {
@@ -6309,7 +6309,7 @@
 
   // Consteval function shouldn't be emitted.
   if (auto *FD = dyn_cast<FunctionDecl>(D))
-    if (FD->isConsteval())
+    if (FD->isImmediateFunction())
       return;
 
   switch (D->getKind()) {
Index: clang/lib/AST/VTableBuilder.cpp
===================================================================
--- clang/lib/AST/VTableBuilder.cpp
+++ clang/lib/AST/VTableBuilder.cpp
@@ -2259,7 +2259,7 @@
 VTableLayout::~VTableLayout() { }
 
 bool VTableContextBase::hasVtableSlot(const CXXMethodDecl *MD) {
-  return MD->isVirtual() && !MD->isConsteval();
+  return MD->isVirtual() && !MD->isImmediateFunction();
 }
 
 ItaniumVTableContext::ItaniumVTableContext(
Index: clang/lib/AST/Decl.cpp
===================================================================
--- clang/lib/AST/Decl.cpp
+++ clang/lib/AST/Decl.cpp
@@ -3002,6 +3002,7 @@
   FunctionDeclBits.HasImplicitReturnZero = false;
   FunctionDeclBits.IsLateTemplateParsed = false;
   FunctionDeclBits.ConstexprKind = static_cast<uint64_t>(ConstexprKind);
+  FunctionDeclBits.BodyContainsImmediateEscalatingExpression = false;
   FunctionDeclBits.InstantiationIsPending = false;
   FunctionDeclBits.UsesSEHTry = false;
   FunctionDeclBits.UsesFPIntrin = UsesFPIntrin;
@@ -3167,6 +3168,43 @@
   return II && II->isStr(Str);
 }
 
+bool FunctionDecl::isImmediateEscalating() const {
+  // C++23 [expr.const]/p17
+  // An immediate-escalating function is
+  //  - the call operator of a lambda that is not declared with the consteval
+  //  specifier,
+  if (isLambdaCallOperator(this) && !isConsteval())
+    return true;
+  // - a defaulted special member function that is not declared with the
+  // consteval specifier,
+  if (isDefaulted() && !isConsteval())
+    return true;
+  // - a function that results from the instantiation of a templated entity
+  // defined with the constexpr specifier.
+  auto TK = getTemplatedKind();
+  if ((TK != TK_NonTemplate && TK != TK_DependentNonTemplate) &&
+      isConstexprSpecified())
+    return true;
+  return false;
+}
+
+bool FunctionDecl::isImmediateFunction() const {
+  // C++23 [expr.const]/p18
+  // An immediate function is a function or constructor that is
+  // - declared with the consteval specifier
+  if (isConsteval())
+    return true;
+  // - an immediate-escalating function F whose function body contains an
+  // immediate-escalating expression
+  if (auto *MD = dyn_cast<CXXMethodDecl>(this);
+      MD && MD->isLambdaStaticInvoker())
+    return MD->getParent()->getLambdaCallOperator()->isImmediateFunction();
+
+  if (isImmediateEscalating() && BodyContainsImmediateEscalatingExpressions())
+    return true;
+  return false;
+}
+
 bool FunctionDecl::isMain() const {
   const TranslationUnitDecl *tunit =
     dyn_cast<TranslationUnitDecl>(getDeclContext()->getRedeclContext());
Index: clang/include/clang/Sema/Sema.h
===================================================================
--- clang/include/clang/Sema/Sema.h
+++ clang/include/clang/Sema/Sema.h
@@ -1066,9 +1066,13 @@
       S.PushFunctionScope();
       S.PushExpressionEvaluationContext(
           Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
-      if (auto *FD = dyn_cast<FunctionDecl>(DC))
+      if (auto *FD = dyn_cast<FunctionDecl>(DC)) {
         FD->setWillHaveBody(true);
-      else
+        S.ExprEvalContexts.back().InImmediateFunctionContext =
+            FD->isConsteval();
+        S.ExprEvalContexts.back().InImmediateEscalatingFunctionContext =
+            FD->isImmediateEscalating();
+      } else
         assert(isa<ObjCMethodDecl>(DC));
     }
 
@@ -1247,7 +1251,7 @@
 
     /// In addition of being constant evaluated, the current expression
     /// occurs in an immediate function context - either a consteval function
-    /// or a consteval if function.
+    /// or a consteval if statement.
     ImmediateFunctionContext,
 
     /// The current expression is potentially evaluated at run time,
@@ -1328,6 +1332,7 @@
     // an immediate function context, so they need to be tracked independently.
     bool InDiscardedStatement;
     bool InImmediateFunctionContext;
+    bool InImmediateEscalatingFunctionContext;
 
     bool IsCurrentlyCheckingDefaultArgumentOrInitializer = false;
 
@@ -1356,7 +1361,8 @@
         : Context(Context), ParentCleanup(ParentCleanup),
           NumCleanupObjects(NumCleanupObjects), NumTypos(0),
           ManglingContextDecl(ManglingContextDecl), ExprContext(ExprContext),
-          InDiscardedStatement(false), InImmediateFunctionContext(false) {}
+          InDiscardedStatement(false), InImmediateFunctionContext(false),
+          InImmediateEscalatingFunctionContext(false) {}
 
     bool isUnevaluated() const {
       return Context == ExpressionEvaluationContext::Unevaluated ||
@@ -6527,6 +6533,11 @@
   /// invocation.
   ExprResult CheckForImmediateInvocation(ExprResult E, FunctionDecl *Decl);
 
+  bool CheckImmediateEscalatingExpression(Expr *E);
+
+  bool CheckImmediateEscalatingFunctionDefinition(
+      FunctionDecl *FD, Expr *ImmediateEscalatingExpression);
+
   bool CompleteConstructorCall(CXXConstructorDecl *Constructor,
                                QualType DeclInitType, MultiExprArg ArgsPtr,
                                SourceLocation Loc,
@@ -9148,6 +9159,9 @@
   bool DeduceReturnType(FunctionDecl *FD, SourceLocation Loc,
                         bool Diagnose = true);
 
+  bool CheckIfFunctionSpecializationIsImmediate(FunctionDecl *FD,
+                                                SourceLocation Loc);
+
   /// Declare implicit deduction guides for a class template if we've
   /// not already done so.
   void DeclareImplicitDeductionGuides(TemplateDecl *Template,
@@ -9732,6 +9746,12 @@
     return ExprEvalContexts.back().isImmediateFunctionContext();
   }
 
+  bool isImmediateEscalatingFunctionContext() const {
+    assert(!ExprEvalContexts.empty() &&
+           "Must be in an expression evaluation context");
+    return ExprEvalContexts.back().InImmediateEscalatingFunctionContext;
+  }
+
   bool isCheckingDefaultArgumentOrInitializer() const {
     assert(!ExprEvalContexts.empty() &&
            "Must be in an expression evaluation context");
Index: clang/include/clang/Sema/ScopeInfo.h
===================================================================
--- clang/include/clang/Sema/ScopeInfo.h
+++ clang/include/clang/Sema/ScopeInfo.h
@@ -236,6 +236,9 @@
   /// The set of GNU address of label extension "&&label".
   llvm::SmallVector<AddrLabelExpr *, 4> AddrLabels;
 
+  /// Whether we found an immediate-escalating expression
+  Expr *ImmediateEscalatingExpression = nullptr;
+
 public:
   /// Represents a simple identification of a weak object.
   ///
Index: clang/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2656,10 +2656,21 @@
   "in C++14; add 'const' to avoid a change in behavior">,
   InGroup<DiagGroup<"constexpr-not-const">>;
 def err_invalid_consteval_take_address : Error<
-  "cannot take address of consteval %select{function|call operator of}1 %0 outside"
+  "cannot take address of %select{immediate|consteval}2 "
+  "%select{function|call operator of}1 %0 outside"
   " of an immediate invocation">;
 def err_invalid_consteval_call : Error<
   "call to consteval function %q0 is not a constant expression">;
+
+def err_immediate_function_used_before_definition : Error<
+  "immediate function %0 used before it is defined">;
+
+def note_immediate_function_reason : Note<
+  "%0 is an immediate function because its body "
+  "%select{evaluates the address of %select{an immediate|a consteval}2 function %1|"
+  "contains a call to %select{an immediate|a consteval}2 "
+  "function %1 and that call is not a constant expression}3">;
+
 def note_invalid_consteval_initializer : Note<
   "in the default initalizer of %0">;
 def note_invalid_consteval_initializer_here : Note<
Index: clang/include/clang/AST/DeclBase.h
===================================================================
--- clang/include/clang/AST/DeclBase.h
+++ clang/include/clang/AST/DeclBase.h
@@ -1656,6 +1656,8 @@
 
     /// Kind of contexpr specifier as defined by ConstexprSpecKind.
     uint64_t ConstexprKind : 2;
+    uint64_t BodyContainsImmediateEscalatingExpression : 1;
+
     uint64_t InstantiationIsPending : 1;
 
     /// Indicates if the function uses __try.
Index: clang/include/clang/AST/Decl.h
===================================================================
--- clang/include/clang/AST/Decl.h
+++ clang/include/clang/AST/Decl.h
@@ -2378,6 +2378,18 @@
     return getConstexprKind() == ConstexprSpecKind::Consteval;
   }
 
+  void setBodyContainsImmediateEscalatingExpressions(bool Set) {
+    FunctionDeclBits.BodyContainsImmediateEscalatingExpression = Set;
+  }
+
+  bool BodyContainsImmediateEscalatingExpressions() const {
+    return FunctionDeclBits.BodyContainsImmediateEscalatingExpression;
+  }
+
+  bool isImmediateEscalating() const;
+
+  bool isImmediateFunction() const;
+
   /// Whether the instantiation of this function is pending.
   /// This bit is set when the decision to instantiate this function is made
   /// and unset if and when the function body is created. That leaves out
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -108,6 +108,7 @@
   longer have to be constexpr compatible but rather support a less restricted requirements for constexpr
   functions. Which include allowing non-literal types as return values and parameters, allow calling of
   non-constexpr functions and constructors.
+- Implemented `P2564R3: consteval needs to propagate up <https://wg21.link/P2564R3>`_.
 
 C++2c Feature Support
 ^^^^^^^^^^^^^^^^^^^^^
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to