[Clang] Instantiate functions from constant evaluation. (#205557)
This PR is a rework of #173537, which was a rework of #115168; the sole
goal remains to address #73232. I used #173537 as a starting point (for
both code and tests), but attempted to follow the alternative design
requested in the comments therein.
A quick design overview:
- A `SemaProxy` interface is introduced to `AST`, which defines a set of
(possibly mutable) semantic operations that can be triggered by the
evaluation of certain expressions; this class finds its implementation
in `Sema/SemaEval.cpp`. `Sema` creates a unique `SemaProxy` to itself at
construction time, which is made available via
`Sema::getproxyForEval()`.
- Ordinary evaluation (e.g., via `Expr::EvaluateAsConstantExpr`) has no
access to `SemaProxy`, and therefore cannot act on the AST: instead,
variants of these functions (e.g.,
`Expr::EvaluateAsMandatedConstantExpr`) are introduced, which accept a
`SemaProxy &`. This makes clear from the call-side whether an operation
can or cannot act on the AST (according to language semantic rules).
- Within the constant evaluator(s), a pointer to `SemaProxy` is stored
in `interp::State`; this is `nullptr` if evaluation began through a
non-`Mandated` evaluation function (again, e.g.,
`EvaluateAsConstantExpr`). The proxy, if present, is leveraged during
evaluation to call back through to `Sema` (e.g., to instantiate a
specialization of a templated function).
- Various callsites are updated to leverage the new
`Expr::EvaluateAsMandatedConstant*` forms of evaluation. I'd appreciate
help from reviewers in auditing whether I've made the correct choices
here.
The tests are mostly from #173537, although I've added some to more
explicitly cover certain kinds of manifestly constant-evaluated
expressions (e.g., immediate invocations). I commented one test from
#173537 that fails an assertion due to an unrelated issue (i.e.,
#199347). #17537 included some other changes to existing tests due to
changes to diagnostic messages; I was able to avoid these changes by
checking whether the template of a specialization has a definition,
prior to trying to instantiate it (so the diagnostic continues to refer
to "undefined function `f<int>`", rather than to an explicit
specialization).
As with #173537, this PR neither addresses #59966 nor provides
scaffolding for Reflection, though the scaffolding introduced here will
help to address both use-cases. In particular, the availability of
`Sema` through the constant evaluator will unblock the implementation of
many of C++26 Reflection's metafunctions, which can explicitly act on
the AST during evaluation.
It was brought to my attention that the Clang 23 branch is scheduled for
a few weeks from now; seeing as this is a fairly significant
architectural change, I have no intention to target that release.
---------
Co-authored-by: Corentin Jabot <corentinjabot@gmail.com>