diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c index 0a002db14e709882ae12cc341a1a24183ef19d99..e3ede02a48ebb0815c42b1b4758c0499b4aca0e6 100644 --- a/gcc/cp/cp-gimplify.c +++ b/gcc/cp/cp-gimplify.c @@ -2421,7 +2421,8 @@ cp_fold (tree x) if (REF_PARENTHESIZED_P (x)) { tree p = maybe_undo_parenthesized_ref (x); - return cp_fold (p); + if (p != x) + return cp_fold (p); } goto unary; diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 6b560952639f5a2defcec64bc808dac756dabd62..d3efc6ea23805251dafc25aca1842e2003a5fdb4 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -29889,13 +29889,18 @@ do_auto_deduction (tree type, tree init, tree auto_node, } else if (AUTO_IS_DECLTYPE (auto_node)) { + /* Figure out if INIT is an unparenthesized id-expression or an + unparenthesized class member access. */ tree stripped_init = tree_strip_any_location_wrapper (init); - if (REFERENCE_REF_P (stripped_init)) + /* We need to be able to tell '(r)' and 'r' apart (when it's of + reference type). Only the latter is an id-expression. */ + if (REFERENCE_REF_P (stripped_init) + && !REF_PARENTHESIZED_P (stripped_init)) stripped_init = TREE_OPERAND (stripped_init, 0); - bool id = (DECL_P (stripped_init) - || ((TREE_CODE (init) == COMPONENT_REF - || TREE_CODE (init) == SCOPE_REF) - && !REF_PARENTHESIZED_P (init))); + const bool id = (DECL_P (stripped_init) + || ((TREE_CODE (stripped_init) == COMPONENT_REF + || TREE_CODE (stripped_init) == SCOPE_REF) + && !REF_PARENTHESIZED_P (stripped_init))); tree deduced = finish_decltype_type (init, id, complain); deduced = canonicalize_type_argument (deduced, complain); if (deduced == error_mark_node) diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index cd1956497f8cfe0e60034d7b802ee6a7fb1e81c8..edba4b60e1090c0c737be6eb3183c639df60a446 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -2049,7 +2049,8 @@ force_paren_expr (tree expr, bool even_uneval) return expr; if (TREE_CODE (expr) == COMPONENT_REF - || TREE_CODE (expr) == SCOPE_REF) + || TREE_CODE (expr) == SCOPE_REF + || REFERENCE_REF_P (expr)) REF_PARENTHESIZED_P (expr) = true; else if (DECL_P (tree_strip_any_location_wrapper (expr))) { @@ -2072,19 +2073,8 @@ maybe_undo_parenthesized_ref (tree t) if (cxx_dialect < cxx14) return t; - if (INDIRECT_REF_P (t) && REF_PARENTHESIZED_P (t)) - { - t = TREE_OPERAND (t, 0); - while (TREE_CODE (t) == NON_LVALUE_EXPR - || TREE_CODE (t) == NOP_EXPR) - t = TREE_OPERAND (t, 0); - - gcc_assert (TREE_CODE (t) == ADDR_EXPR - || TREE_CODE (t) == STATIC_CAST_EXPR); - t = TREE_OPERAND (t, 0); - } - else if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR) - && REF_PARENTHESIZED_P (t)) + if ((TREE_CODE (t) == PAREN_EXPR || TREE_CODE (t) == VIEW_CONVERT_EXPR) + && REF_PARENTHESIZED_P (t)) t = TREE_OPERAND (t, 0); return t; diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C new file mode 100644 index 0000000000000000000000000000000000000000..56e011e36f4622f1b47371c69f8cb687751a2c0e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto2.C @@ -0,0 +1,12 @@ +// PR c++/103403 +// { dg-do compile { target c++14 } } + +template<typename T> +auto constexpr RtoL1(T&& r) -> decltype(auto) { + return (r); +}; +int main() { + int t; + int x{3}; + decltype (RtoL1(x+0)) y = t; +} diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C new file mode 100644 index 0000000000000000000000000000000000000000..914e87f5b79c34c706ecb2aab1547ed7291d9fe4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto3.C @@ -0,0 +1,12 @@ +// PR c++/103403 +// { dg-do compile { target c++14 } } + +int main() +{ + int i = 1; + int&& r = 1; + + decltype(auto) ri = (i); + decltype(auto) rr = (r); + decltype((r)) rr2 = (r); +} diff --git a/gcc/testsuite/g++.dg/cpp1y/decltype-auto4.C b/gcc/testsuite/g++.dg/cpp1y/decltype-auto4.C new file mode 100644 index 0000000000000000000000000000000000000000..9765857efb199958223f8db6bcb9f256fc06d67c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/decltype-auto4.C @@ -0,0 +1,65 @@ +// PR c++/103403 +// { dg-do compile { target c++14 } } + +struct false_type { static constexpr bool value = false; }; +struct true_type { static constexpr bool value = true; }; +template<class T, class U> +struct is_same : false_type {}; +template<class T> +struct is_same<T, T> : true_type {}; + +int fn (); +int &ref (); +int &&rref (); + +struct S { + int i; + int &r = i; +}; + +void +ids () +{ + const S *s = new S(); + int i; + int &ir = i; + decltype(auto) r1 = s->i; + static_assert (is_same<decltype(r1), int>::value, ""); + decltype(auto) r2 = s->r; + static_assert (is_same<decltype(r2), int&>::value, ""); + decltype(auto) r3 = i; + static_assert (is_same<decltype(r3), int>::value, ""); + decltype(auto) r4 = ir; + static_assert (is_same<decltype(r4), int&>::value, ""); +} + +void +nonids () +{ + const S *s = new S(); + int i; + int &ir = i; + int &&irr = 42; + decltype(auto) r1 = fn (); + static_assert (is_same<decltype(r1), int>::value, ""); + decltype(auto) r2 = (fn ()); + static_assert (is_same<decltype(r2), int>::value, ""); + decltype(auto) r3 = ref (); + static_assert (is_same<decltype(r3), int&>::value, ""); + decltype(auto) r4 = (ref ()); + static_assert (is_same<decltype(r4), int&>::value, ""); + decltype(auto) r5 = rref (); + static_assert (is_same<decltype(r5), int&&>::value, ""); + decltype(auto) r6 = (rref ()); + static_assert (is_same<decltype(r6), int&&>::value, ""); + decltype(auto) r8 = (s->i); + static_assert (is_same<decltype(r8), const int&>::value, ""); + decltype(auto) r9 = (s->r); + static_assert (is_same<decltype(r9), int&>::value, ""); + decltype(auto) r10 = (i); + static_assert (is_same<decltype(r10), int&>::value, ""); + decltype(auto) r11 = (ir); + static_assert (is_same<decltype(r11), int&>::value, ""); + decltype(auto) r12 = (irr); + static_assert (is_same<decltype(r12), int&>::value, ""); +} diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp-decltype1.C b/gcc/testsuite/g++.dg/cpp1z/decomp-decltype1.C new file mode 100644 index 0000000000000000000000000000000000000000..e8ca0fc0beeb91ff770bf7d1081a6d0df29b241d --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/decomp-decltype1.C @@ -0,0 +1,28 @@ +// PR c++/81176 +// { dg-do compile { target c++17 } } + +namespace std { + template<typename T> struct tuple_size; + template<int, typename> struct tuple_element; +} + +struct A { + int i; + template <int I> int& get() { return i; } +}; + +template<> struct std::tuple_size<A> { static const int value = 2; }; +template<int I> struct std::tuple_element<I,A> { using type = int; }; + +template <class,class> struct same_type; +template <class T> struct same_type<T,T> {}; + +void +foo (A x) +{ + auto [ a, b ] = x; + decltype(auto) c = a; + same_type<decltype(a), int>{}; + same_type<decltype(c), int>{}; +} +