public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: sizeof... mangling with alias template [PR95298]
@ 2023-12-22  0:20 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2023-12-22  0:20 UTC (permalink / raw)
  To: gcc-patches

Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

We were getting sizeof... mangling wrong when the argument after
substitution was a pack expansion that is not a simple T..., such as
list<T>... in variadic-mangle4.C or (A+1)... in variadic-mangle5.C.  In the
former case we ICEd; in the latter case we wrongly mangled it as sZ
<expression>.

	PR c++/95298

gcc/cp/ChangeLog:

	* mangle.cc (write_expression): Handle v18 sizeof... bug.
	* pt.cc (tsubst_pack_expansion): Keep TREE_VEC for sizeof...
	(tsubst_expr): Don't strip TREE_VEC here.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/variadic-mangle2.C: Add non-member.
	* g++.dg/cpp0x/variadic-mangle4.C: New test.
	* g++.dg/cpp0x/variadic-mangle5.C: New test.
	* g++.dg/cpp0x/variadic-mangle5a.C: New test.
---
 gcc/cp/mangle.cc                              | 14 +++++++++
 gcc/cp/pt.cc                                  | 12 ++++++--
 gcc/testsuite/g++.dg/cpp0x/variadic-mangle2.C |  8 +++++
 gcc/testsuite/g++.dg/cpp0x/variadic-mangle4.C | 29 +++++++++++++++++++
 gcc/testsuite/g++.dg/cpp0x/variadic-mangle5.C | 13 +++++++++
 .../g++.dg/cpp0x/variadic-mangle5a.C          | 13 +++++++++
 6 files changed, 86 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/variadic-mangle4.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/variadic-mangle5.C
 create mode 100644 gcc/testsuite/g++.dg/cpp0x/variadic-mangle5a.C

diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc
index 365d470f46e..36c5ac5c4da 100644
--- a/gcc/cp/mangle.cc
+++ b/gcc/cp/mangle.cc
@@ -3444,6 +3444,7 @@ write_expression (tree expr)
 
       if (PACK_EXPANSION_P (op))
 	{
+    sizeof_pack:
 	  if (abi_check (11))
 	    {
 	      /* sZ rather than szDp.  */
@@ -3464,6 +3465,19 @@ write_expression (tree expr)
 	  int length = TREE_VEC_LENGTH (args);
 	  if (abi_check (10))
 	    {
+	      /* Before v19 we wrongly mangled all single pack expansions with
+		 sZ, but now only for expressions, as types ICEd (95298).  */
+	      if (length == 1)
+		{
+		  tree arg = TREE_VEC_ELT (args, 0);
+		  if (TREE_CODE (arg) == EXPR_PACK_EXPANSION
+		      && !abi_check (19))
+		    {
+		      op = arg;
+		      goto sizeof_pack;
+		    }
+		}
+
 	      /* sP <template-arg>* E # sizeof...(T), size of a captured
 		 template parameter pack from an alias template */
 	      write_string ("sP");
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2817657a8bb..5278ef6e981 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -13572,7 +13572,15 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
       /* If the argument pack is a single pack expansion, pull it out.  */
       if (TREE_VEC_LENGTH (args) == 1
 	  && pack_expansion_args_count (args))
-	return TREE_VEC_ELT (args, 0);
+	{
+	  tree arg = TREE_VEC_ELT (args, 0);
+	  if (PACK_EXPANSION_SIZEOF_P (t)
+	      && !TEMPLATE_PARM_P (PACK_EXPANSION_PATTERN (arg)))
+	    /* Except if this isn't a simple sizeof...(T) which gets sZ
+	       mangling, keep the TREE_VEC to get sP mangling.  */;
+	  else
+	    return TREE_VEC_ELT (args, 0);
+	}
 
       /* Types need no adjustment, nor does sizeof..., and if we still have
 	 some pack expansion args we won't do anything yet.  */
@@ -20261,8 +20269,6 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	    {
 	      if (PACK_EXPANSION_P (expanded))
 		/* OK.  */;
-	      else if (TREE_VEC_LENGTH (expanded) == 1)
-		expanded = TREE_VEC_ELT (expanded, 0);
 	      else
 		expanded = make_argument_pack (expanded);
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/variadic-mangle2.C b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle2.C
index ea96ef87308..596242ab8b7 100644
--- a/gcc/testsuite/g++.dg/cpp0x/variadic-mangle2.C
+++ b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle2.C
@@ -8,6 +8,11 @@ struct A {
   template<typename...T> using M = int[sizeof...(T)];
   template<typename...A> void g(M<A...> &);
 };
+
+template<typename ...T> using N = int[sizeof...(T)];
+template<typename ...T> void f(N<T...> &);
+// equivalent to template<typename ...T> void f(int(&)[sizeof...(T)])
+
 void g(A a)
 {
   int arr[3];
@@ -15,4 +20,7 @@ void g(A a)
   a.f<1,2,3>(arr);
   // { dg-final { scan-assembler "_ZN1A1gIJiiiEEEvRAsZT__i" } }
   a.g<int,int,int>(arr);
+  // { dg-final { scan-assembler "_Z1fIJiiiEEvRAsZT__i" } }
+  f<int,int,int>(arr);
 }
+
diff --git a/gcc/testsuite/g++.dg/cpp0x/variadic-mangle4.C b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle4.C
new file mode 100644
index 00000000000..6930180d777
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle4.C
@@ -0,0 +1,29 @@
+// PR c++/95298
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -fabi-compat-version=0 }
+
+template<class...>
+struct list{};
+
+template<int n>
+struct _func_select
+{
+  using f = void;
+};
+
+struct func
+{
+  template<class... seqs>
+  using f = typename _func_select<sizeof...(seqs)>::f;
+};
+
+template<class... T>
+func::f<list<T>...> foo(T&&...)
+{}
+
+// { dg-final { scan-assembler "_Z3fooIJEEN12_func_selectIXsPDp4listIJT_EEEEE1fEDpOS2_" } }
+
+int main()
+{
+  foo();
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/variadic-mangle5.C b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle5.C
new file mode 100644
index 00000000000..e22a33dad61
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle5.C
@@ -0,0 +1,13 @@
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-fabi-version=0 -fabi-compat-version=0" }
+
+template<int...T> using N = int[sizeof...(T)];
+template<int...A> void f(N<(A+1)...> &);
+
+void g()
+{
+  int arr[3];
+
+  // { dg-final { scan-assembler "_Z1fIJLi1ELi2ELi3EEEvRAsPXspplT_Li1EEE_i" } }
+  f<1,2,3>(arr);
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/variadic-mangle5a.C b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle5a.C
new file mode 100644
index 00000000000..eb1b0847a99
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/variadic-mangle5a.C
@@ -0,0 +1,13 @@
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-fabi-version=18 -fabi-compat-version=18" }
+
+template<int...T> using N = int[sizeof...(T)];
+template<int...A> void f(N<(A+1)...> &);
+
+void g()
+{
+  int arr[3];
+
+  // { dg-final { scan-assembler "_Z1fIJLi1ELi2ELi3EEEvRAsZplT_Li1E_i" } }
+  f<1,2,3>(arr);
+}

base-commit: cdfaa4aa52752e55d27bb068b6de933d17b176d3
prerequisite-patch-id: 6b409ce898fde643a415f24e698c3cca69ff34d6
-- 
2.39.3


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-12-22  0:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-22  0:20 [pushed] c++: sizeof... mangling with alias template [PR95298] Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).