c++: Move consteval folding to cp_fold_r
commit6851e3423c2b5ec6516efa0677745d25c905e079
authorMarek Polacek <polacek@redhat.com>
Fri, 1 Sep 2023 00:11:50 +0000 (31 20:11 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 19 Sep 2023 13:24:44 +0000 (19 09:24 -0400)
tree72c6c8bc7a321e6f9f6090b9db82fea35931e707
parentf25960b03834712f312d46fed9a021df36f89f22
c++: Move consteval folding to cp_fold_r

In the review of P2564:
<https://gcc.gnu.org/pipermail/gcc-patches/2023-August/628747.html>
it turned out that in order to correctly handle an example in the paper,
we should stop doing immediate evaluation in build_over_call and
bot_replace, and instead do it in cp_fold_r.  This patch does that.

Another benefit is that this is a pretty significant simplification, at
least in my opinion.  Also, this fixes the c++/110997 ICE (but the test
doesn't compile yet).

The main drawback seems to be that cp_fold_r doesn't process
uninstantiated templates.  We still have to handle things like
"false ? foo () : 1".  To that end, I've added cp_fold_immediate, called
on dead branches in cxx_eval_conditional_expression.

You'll see that I've reintroduced ADDR_EXPR_DENOTES_CALL_P here.  This
is to detect

  *(&foo)) ()
  (s.*&S::foo) ()

which were deemed ill-formed.

gcc/cp/ChangeLog:

* call.cc (build_over_call): Set ADDR_EXPR_DENOTES_CALL_P.  Don't handle
immediate_invocation_p here.
* constexpr.cc (cxx_eval_call_expression): Use mce_true for
DECL_IMMEDIATE_FUNCTION_P.
(cxx_eval_conditional_expression): Call cp_fold_immediate.
* cp-gimplify.cc (enum fold_flags): Add ff_fold_immediate.
(maybe_replace_decl): Make static.
(cp_fold_r): Expand immediate invocations.
(cp_fold_immediate_r): New.
(cp_fold_immediate): New.
* cp-tree.h (ADDR_EXPR_DENOTES_CALL_P): Define.
(cp_fold_immediate): Declare.
* tree.cc (bot_replace): Don't handle immediate invocations here.

libstdc++-v3/ChangeLog:

* testsuite/20_util/allocator/105975.cc: Add dg-error.

gcc/testsuite/ChangeLog:

* g++.dg/cpp23/consteval-if2.C: Add xfail.
* g++.dg/cpp2a/consteval-memfn1.C: Adjust.
* g++.dg/cpp2a/consteval11.C: Remove dg-message.
* g++.dg/cpp2a/consteval3.C: Remove dg-message and dg-error.
* g++.dg/cpp2a/consteval9.C: Remove dg-message.
* g++.dg/cpp2a/consteval32.C: New test.
* g++.dg/cpp2a/consteval33.C: New test.
* g++.dg/cpp2a/consteval34.C: New test.
* g++.dg/cpp2a/consteval35.C: New test.
15 files changed:
gcc/cp/call.cc
gcc/cp/constexpr.cc
gcc/cp/cp-gimplify.cc
gcc/cp/cp-tree.h
gcc/cp/tree.cc
gcc/testsuite/g++.dg/cpp23/consteval-if2.C
gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C
gcc/testsuite/g++.dg/cpp2a/consteval11.C
gcc/testsuite/g++.dg/cpp2a/consteval3.C
gcc/testsuite/g++.dg/cpp2a/consteval32.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/consteval33.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/consteval34.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/consteval35.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/consteval9.C
libstdc++-v3/testsuite/20_util/allocator/105975.cc