[PATCH] D76447: Apply ConstantEvaluated evaluation contexts to more manifestly constant evaluated scopes

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 19 14:15:23 PDT 2020


rsmith added inline comments.


================
Comment at: clang/lib/Sema/SemaDeclCXX.cpp:16793-16797
+  if (isManifestlyEvaluatedVar(*this, D)) {
+    using ExpressionKind = ExpressionEvaluationContextRecord::ExpressionKind;
+
+    PushExpressionEvaluationContext(
+        ExpressionEvaluationContext::ConstantEvaluated, D, ExpressionKind::EK_ConstexprVarInit);
----------------
We can't implement the checks for manifestly constant-evaluated initializers this way in general. Per [expr.const]/14, we need to treat the initializer as manifestly constant-evaluated if it's "the initializer of a variable that is usable in constant expressions or has constant initialization". We can't test either of those conditions in general until we know what the initializer is, because they can both depend on whether evaluation of the initializer succeeds. (This approach works for the case of `constexpr` variables, which are always usable in constant expressions, but not any of the other cases, and the approach we'll need for the other cases will also handle `constexpr` variables. There is a very modest benefit to special-casing `constexpr` variable initializers regardless -- we can avoid forming and then pruning out nested `ConstantExpr` nodes for immediate invocations inside the initializer -- but I think it's probably not worth the added complexity.)

To address the general problem, we should handle this in `CheckCompleteVariableDeclaration`, which is where we evaluate the initializer and generally determine whether the variable has constant initialization and / or is usable in constant expressions. Probably the cleanest approach -- and certainly the one I'd been intending to pursue -- would be to wrap the initializer with a `ConstantExpr` there in the relevant cases, and allow the usual handling of nested immediate invocations to prune out any `ConstantExpr`s nested within the initializer representing inner calls to `consteval` functions. (I think I've mentioned elsewhere that I would like to remove the "evaluated value" storage on `VarDecl` in favor of using `ConstantExpr` for this purpose.)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D76447/new/

https://reviews.llvm.org/D76447





More information about the cfe-commits mailing list