c++: Implement LWG3396 Clarify point of reference for source_location::current()...
authorJakub Jelinek <jakub@redhat.com>
Wed, 2 Dec 2020 23:25:51 +0000 (00:25 +0100)
committerJakub Jelinek <jakub@redhat.com>
Wed, 2 Dec 2020 23:40:33 +0000 (00:40 +0100)
commitba3d8dffcc1c23b30370ab24fc20d09cff005d7b
treee4ea6a9a8e575d5f90fdc95c71e3e56bb94816cc
parent2cace1cd683318e83e8a613f1aa2f2b16e37a342
c++: Implement LWG3396 Clarify point of reference for source_location::current() [PR80780, PR93093]

While std::source_location::current () is static consteval source_location
current() noexcept; in the standard, it also says with LWG3396:
"Any call to current that appears as a default member initializer
([class.mem]), or as a subexpression thereof, should correspond to the
location of the constructor definition or aggregate initialization that uses
the default member initializer.  Any call to current that appears as a
default argument ([dcl.fct.default]), or as a subexpression thereof, should
correspond to the location of the invocation of the function that uses the
default argument ([expr.call])."
so it must work as compiler magic rather than normal immediate functions,
in particular we need to defer its evaluation when parsing default arguments
or nsdmis.

This patch actually defers evaluation of all the calls to
std::source_location::current () until genericization (or constant expression
evaluation when called from constant expression contexts).
I had to change constexpr.c too so that it temporarily adjusts
current_function_decl from the constexpr evaluation context, but we do the
same already from __builtin_FUNCTION ().

2020-12-03  Jakub Jelinek  <jakub@redhat.com>

PR c++/80780
PR c++/93093
* cp-tree.h (source_location_current_p): Declare.
* tree.c (source_location_current_p): New function.
* call.c (immediate_invocation_p): New function.
(build_over_call): Use it to resolve LWG3396.
* constexpr.c (cxx_eval_builtin_function_call): Temporarily set
current_function_decl from ctx->call->fundef->decl if any.
* cp-gimplify.c (cp_genericize_r) <case CALL_EXPR>: Fold calls
to immediate function std::source_location::current ().

* g++.dg/cpp2a/srcloc15.C: New test.
* g++.dg/cpp2a/srcloc16.C: New test.
* g++.dg/cpp2a/srcloc17.C: New test.
* g++.dg/cpp2a/srcloc18.C: New test.
gcc/cp/call.c
gcc/cp/constexpr.c
gcc/cp/cp-gimplify.c
gcc/cp/cp-tree.h
gcc/cp/tree.c
gcc/testsuite/g++.dg/cpp2a/srcloc15.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/srcloc16.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/srcloc17.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/srcloc18.C [new file with mode: 0644]