From: Jakub Jelinek Date: Wed, 25 May 2011 07:00:01 +0000 (+0200) Subject: re PR c++/49136 ([C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=98933689804f4e8c36f5958a26b34ea7842b10a7;p=gcc.git re PR c++/49136 ([C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields) PR c++/49136 * semantics.c (cxx_eval_bit_field_ref): Handle the case when BIT_FIELD_REF doesn't cover only a single field. * g++.dg/cpp0x/constexpr-bitfield2.C: New test. * g++.dg/cpp0x/constexpr-bitfield3.C: New test. From-SVN: r174168 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index d9d8a549e62..2f2348ff3b6 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2011-05-25 Jakub Jelinek + + PR c++/49136 + * semantics.c (cxx_eval_bit_field_ref): Handle the + case when BIT_FIELD_REF doesn't cover only a single field. + 2011-05-24 Jason Merrill PR c++/49042 diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index a7ca50d15e2..50f25f0f72d 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -6449,6 +6449,9 @@ cxx_eval_bit_field_ref (const constexpr_call *call, tree t, bool *non_constant_p) { tree orig_whole = TREE_OPERAND (t, 0); + tree retval, fldval, utype, mask; + bool fld_seen = false; + HOST_WIDE_INT istart, isize; tree whole = cxx_eval_constant_expression (call, orig_whole, allow_non_constant, addr, non_constant_p); @@ -6469,12 +6472,47 @@ cxx_eval_bit_field_ref (const constexpr_call *call, tree t, return t; start = TREE_OPERAND (t, 2); + istart = tree_low_cst (start, 0); + isize = tree_low_cst (TREE_OPERAND (t, 1), 0); + utype = TREE_TYPE (t); + if (!TYPE_UNSIGNED (utype)) + utype = build_nonstandard_integer_type (TYPE_PRECISION (utype), 1); + retval = build_int_cst (utype, 0); FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value) { - if (bit_position (field) == start) + tree bitpos = bit_position (field); + if (bitpos == start && DECL_SIZE (field) == TREE_OPERAND (t, 1)) return value; + if (TREE_CODE (TREE_TYPE (field)) == INTEGER_TYPE + && TREE_CODE (value) == INTEGER_CST + && host_integerp (bitpos, 0) + && host_integerp (DECL_SIZE (field), 0)) + { + HOST_WIDE_INT bit = tree_low_cst (bitpos, 0); + HOST_WIDE_INT sz = tree_low_cst (DECL_SIZE (field), 0); + HOST_WIDE_INT shift; + if (bit >= istart && bit + sz <= istart + isize) + { + fldval = fold_convert (utype, value); + mask = build_int_cst_type (utype, -1); + mask = fold_build2 (LSHIFT_EXPR, utype, mask, + size_int (TYPE_PRECISION (utype) - sz)); + mask = fold_build2 (RSHIFT_EXPR, utype, mask, + size_int (TYPE_PRECISION (utype) - sz)); + fldval = fold_build2 (BIT_AND_EXPR, utype, fldval, mask); + shift = bit - istart; + if (BYTES_BIG_ENDIAN) + shift = TYPE_PRECISION (utype) - shift - sz; + fldval = fold_build2 (LSHIFT_EXPR, utype, fldval, + size_int (shift)); + retval = fold_build2 (BIT_IOR_EXPR, utype, retval, fldval); + fld_seen = true; + } + } } - gcc_unreachable(); + if (fld_seen) + return fold_convert (TREE_TYPE (t), retval); + gcc_unreachable (); return error_mark_node; } diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index b39b4c726c6..af20a3ec650 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2011-05-25 Jakub Jelinek + + PR c++/49136 + * g++.dg/cpp0x/constexpr-bitfield2.C: New test. + * g++.dg/cpp0x/constexpr-bitfield3.C: New test. + 2011-05-24 Vladimir Makarov PR rtl-optimization/48757 diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield2.C new file mode 100644 index 00000000000..531bf31fc01 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield2.C @@ -0,0 +1,19 @@ +// PR c++/49136 +// { dg-do compile } +// { dg-options "-std=c++0x" } + +struct day +{ + unsigned d : 5; + unsigned n : 3; + constexpr explicit day (int dd) : d(dd), n(7) {} +}; + +struct date { + int d; + constexpr date (day dd) : d(dd.n != 7 ? 7 : dd.d) {} +}; + +constexpr day d(0); +constexpr date dt(d); +static_assert (dt.d == 0, "Error"); diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield3.C new file mode 100644 index 00000000000..b0ecbfb9abb --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield3.C @@ -0,0 +1,33 @@ +// PR c++/49136 +// { dg-do compile } +// { dg-options "-std=c++0x" } + +struct S +{ + unsigned : 1; unsigned s : 27; unsigned : 4; + constexpr S (unsigned int x) : s(x) {} +}; + +template +struct T +{ + unsigned int t; + constexpr T (S s) : t(s.s != 7 ? 0 : s.s) {} + constexpr T (S s, S s2) : t(s.s != s2.s ? 0 : s.s) {} +}; + +constexpr S s (7), s2 (7); +constexpr T t (s), t2 (s, s2); +static_assert (t.t == 7, "Error"); +static_assert (t2.t == 7, "Error"); + +struct U +{ + int a : 1; int s : 1; + constexpr U (int x, int y) : a (x), s (y) {} +}; + +constexpr U u (0, -1), u2 (-1, -1); +constexpr T t3 (u), t4 (u, u2); +static_assert (t3.t == 0, "Error"); +static_assert (t4.t == -1, "Error");