From a663ce5a831198b5ef99cbc0b7205a2d9a37a574 Mon Sep 17 00:00:00 2001
From: Jason Merrill <jason@redhat.com>
Date: Thu, 20 Nov 2014 19:47:50 -0500
Subject: [PATCH] re PR c++/63658 (Using class reference as template parameter
 causes compilation to fail)

	PR c++/63658
	* pt.c (convert_nontype_argument): Call convert_from_reference.
	(check_instantiated_arg): Don't be confused by reference refs.
	(unify): Look through reference refs on the arg, too.
	* mangle.c (write_template_arg): Look through reference refs.

From-SVN: r217900
---
 gcc/cp/ChangeLog                     |  6 ++++++
 gcc/cp/mangle.c                      |  2 ++
 gcc/cp/pt.c                          | 11 ++++++++---
 gcc/testsuite/g++.dg/template/ref9.C | 15 +++++++++++++++
 4 files changed, 31 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/template/ref9.C

diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 305565fa3502..ee431a1091d0 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,5 +1,11 @@
 2014-11-20  Jason Merrill  <jason@redhat.com>
 
+	PR c++/63658
+	* pt.c (convert_nontype_argument): Call convert_from_reference.
+	(check_instantiated_arg): Don't be confused by reference refs.
+	(unify): Look through reference refs on the arg, too.
+	* mangle.c (write_template_arg): Look through reference refs.
+
 	* error.c (dump_expr): Avoid printing (*&i) for references.
 
 2014-11-20  Ville Voutilainen  <ville.voutilainen@gmail.com>
diff --git a/gcc/cp/mangle.c b/gcc/cp/mangle.c
index 576ad1d19780..4c63df042faf 100644
--- a/gcc/cp/mangle.c
+++ b/gcc/cp/mangle.c
@@ -3086,6 +3086,8 @@ write_template_arg (tree node)
 	}
     }
 
+  if (REFERENCE_REF_P (node))
+    node = TREE_OPERAND (node, 0);
   if (TREE_CODE (node) == NOP_EXPR
       && TREE_CODE (TREE_TYPE (node)) == REFERENCE_TYPE)
     {
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 05ca70628658..71b7af1c206f 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -6174,7 +6174,7 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
      right type?  */
   gcc_assert (same_type_ignoring_top_level_qualifiers_p
 	      (type, TREE_TYPE (expr)));
-  return expr;
+  return convert_from_reference (expr);
 }
 
 /* Subroutine of coerce_template_template_parms, which returns 1 if
@@ -15740,6 +15740,7 @@ check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
      constant.  */
   else if (TREE_TYPE (t)
 	   && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
+	   && !REFERENCE_REF_P (t)
 	   && !TREE_CONSTANT (t))
     {
       if (complain & tf_error)
@@ -18473,8 +18474,12 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 
     case INDIRECT_REF:
       if (REFERENCE_REF_P (parm))
-	return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
-		      strict, explain_p);
+	{
+	  if (REFERENCE_REF_P (arg))
+	    arg = TREE_OPERAND (arg, 0);
+	  return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
+			strict, explain_p);
+	}
       /* FALLTHRU */
 
     default:
diff --git a/gcc/testsuite/g++.dg/template/ref9.C b/gcc/testsuite/g++.dg/template/ref9.C
new file mode 100644
index 000000000000..983f627887fb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/ref9.C
@@ -0,0 +1,15 @@
+// PR c++/63658
+
+struct Descriptor {};
+
+template <Descriptor & D>
+struct foo
+{
+  void size ();
+};
+
+Descriptor g_descriptor = {};
+
+template<> void foo<g_descriptor>::size()
+{
+}
-- 
GitLab