PR c++/88337 - Implement P1327R1: Allow dynamic_cast in constexpr.
authorMarek Polacek <polacek@redhat.com>
Sun, 29 Dec 2019 16:44:41 +0000 (16:44 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Sun, 29 Dec 2019 16:44:41 +0000 (16:44 +0000)
commit22edf9431e4519c409ebf41c7589cccfb8c4b625
treec4cb09adb7774b0c2c01d580d49c46ea9d4a394e
parent6ec067548fa994158819db0a62a8b5356d452c2c
PR c++/88337 - Implement P1327R1: Allow dynamic_cast in constexpr.

This patch implements
<http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1327r1.html>.

When build_dynamic_cast realizes that a dynamic_cast needs a run-time check, it
generates a call to __dynamic_cast -- see dyncast.cc in libsupc++ for its
definition.  The gist of my approach is to evaluate such a call at compile time.

* constexpr.c (cxx_dynamic_cast_fn_p): New function.
(extract_obj_from_addr_offset): New function.
(get_component_with_type): New function.
(cxx_eval_dynamic_cast_fn): New function.
(cxx_eval_call_expression): Call cxx_eval_dynamic_cast_fn for a call
to __dynamic_cast.
(potential_constant_expression_1): Don't give up on
cxx_dynamic_cast_fn_p.
* rtti.c (build_dynamic_cast_1): When creating a call to
__dynamic_cast, use the location of the original expression.

* g++.dg/cpp2a/constexpr-dynamic1.C: New test.
* g++.dg/cpp2a/constexpr-dynamic10.C: New test.
* g++.dg/cpp2a/constexpr-dynamic11.C: New test.
* g++.dg/cpp2a/constexpr-dynamic12.C: New test.
* g++.dg/cpp2a/constexpr-dynamic13.C: New test.
* g++.dg/cpp2a/constexpr-dynamic14.C: New test.
* g++.dg/cpp2a/constexpr-dynamic15.C: New test.
* g++.dg/cpp2a/constexpr-dynamic16.C: New test.
* g++.dg/cpp2a/constexpr-dynamic17.C: New test.
* g++.dg/cpp2a/constexpr-dynamic2.C: New test.
* g++.dg/cpp2a/constexpr-dynamic3.C: New test.
* g++.dg/cpp2a/constexpr-dynamic4.C: New test.
* g++.dg/cpp2a/constexpr-dynamic5.C: New test.
* g++.dg/cpp2a/constexpr-dynamic6.C: New test.
* g++.dg/cpp2a/constexpr-dynamic7.C: New test.
* g++.dg/cpp2a/constexpr-dynamic8.C: New test.
* g++.dg/cpp2a/constexpr-dynamic9.C: New test.

From-SVN: r279755
21 files changed:
gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/cp/rtti.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic10.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic11.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic12.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic13.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic14.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic15.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic16.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic17.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic5.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic6.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic7.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic8.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/constexpr-dynamic9.C [new file with mode: 0644]