c++: constexpr pointer indirection with negative offset [PR100209]
authorPatrick Palka <ppalka@redhat.com>
Mon, 26 Apr 2021 21:30:39 +0000 (17:30 -0400)
committerPatrick Palka <ppalka@redhat.com>
Mon, 26 Apr 2021 21:30:39 +0000 (17:30 -0400)
commit0120cd9382728fdc99d4cfdcb72cd0f55aca2ce3
treef24c2aca76b5e6a869254f9dd4633b83a3524fec
parentbd7ebe9da745a62184052dd1b15f4dd10fbdc9f4
c++: constexpr pointer indirection with negative offset [PR100209]

During constexpr evaluation, a base-to-derived conversion may yield an
expression like (Derived*)(&D.2217.D.2106 p+ -4) where D.2217 is the
derived object and D.2106 is the base.  But cxx_fold_indirect_ref
doesn't know how to resolve an INDIRECT_REF thereof to just D.2217,
because it doesn't handle POINTER_PLUS_EXPR of a COMPONENT_REF with
negative offset well: when the offset N is positive, it knows that
'&x p+ N' is equivalent to '&x.f p+ (N - bytepos(f))', but it doesn't
know about the reverse transformation, that '&x.f p+ N' is equivalent
to '&x p+ (N + bytepos(f))' when N is negative, which is important for
resolving such base-to-derived conversions and for accessing subobjects
backwards.  This patch teaches cxx_fold_indirect_ref this reverse
transformation.

gcc/cp/ChangeLog:

PR c++/100209
* constexpr.c (cxx_fold_indirect_ref): Try to canonicalize the
object/offset pair for a POINTER_PLUS_EXPR of a COMPONENT_REF
with a negative offset into one whose offset is nonnegative
before calling cxx_fold_indirect_ref_1.

gcc/testsuite/ChangeLog:

PR c++/100209
* g++.dg/cpp1y/constexpr-base1.C: New test.
* g++.dg/cpp1y/constexpr-ptrsub1.C: New test.
gcc/cp/constexpr.c
gcc/testsuite/g++.dg/cpp1y/constexpr-base1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1y/constexpr-ptrsub1.C [new file with mode: 0644]