a54273
From 55f40d968b0bd3be4478a9481e829a99ee0fa04f Mon Sep 17 00:00:00 2001
a54273
From: Jason Merrill <jason@redhat.com>
a54273
Date: Mon, 5 Apr 2021 22:50:44 -0400
a54273
Subject: [PATCH] c++: mangling of lambdas in default args [PR91241]
a54273
a54273
In this testcase, the parms remembered in LAMBDA_EXPR_EXTRA_SCOPE are no
a54273
longer the parms of the FUNCTION_DECL they have as their DECL_CONTEXT, so we
a54273
were mangling both lambdas as parm #0.  But since the parms are numbered
a54273
from right to left we don't need to need to find them in the FUNCTION_DECL,
a54273
we can measure their own DECL_CHAIN.
a54273
a54273
gcc/cp/ChangeLog:
a54273
a54273
	PR c++/91241
a54273
	* mangle.c (write_compact_number): Add sanity check.
a54273
	(write_local_name): Use list_length for parm number.
a54273
a54273
gcc/testsuite/ChangeLog:
a54273
a54273
	PR c++/91241
a54273
	* g++.dg/abi/lambda-defarg1.C: New test.
a54273
---
a54273
 gcc/cp/mangle.c                           | 11 ++---------
a54273
 gcc/testsuite/g++.dg/abi/lambda-defarg1.C | 11 +++++++++++
a54273
 2 files changed, 13 insertions(+), 9 deletions(-)
a54273
 create mode 100644 gcc/testsuite/g++.dg/abi/lambda-defarg1.C
a54273
a54273
--- gcc/cp/mangle.c
a54273
+++ gcc/cp/mangle.c
a54273
@@ -1628,6 +1628,7 @@ write_literal_operator_name (tree identifier)
a54273
 static void
a54273
 write_compact_number (int num)
a54273
 {
a54273
+  gcc_checking_assert (num >= 0);
a54273
   if (num > 0)
a54273
     write_unsigned_number (num - 1);
a54273
   write_char ('_');
a54273
@@ -2027,15 +2028,7 @@ write_local_name (tree function, const tree local_entity,
a54273
   /* For this purpose, parameters are numbered from right-to-left.  */
a54273
   if (parm)
a54273
     {
a54273
-      tree t;
a54273
-      int i = 0;
a54273
-      for (t = DECL_ARGUMENTS (function); t; t = DECL_CHAIN (t))
a54273
-	{
a54273
-	  if (t == parm)
a54273
-	    i = 1;
a54273
-	  else if (i)
a54273
-	    ++i;
a54273
-	}
a54273
+      int i = list_length (parm);
a54273
       write_char ('d');
a54273
       write_compact_number (i - 1);
a54273
     }
a54273
--- /dev/null
a54273
+++ gcc/testsuite/g++.dg/abi/lambda-defarg1.C
a54273
@@ -0,0 +1,11 @@
a54273
+// PR c++/91241
a54273
+// { dg-do compile { target c++11 } }
a54273
+
a54273
+struct A {
a54273
+  int *b(const int & = []() -> int { return 0; }(),
a54273
+	 const int & = []() -> int { return 0; }());
a54273
+};
a54273
+int *A::b(const int &, const int &) { b(); return 0; }
a54273
+// { dg-final { scan-assembler "_ZN1A1bERKiS1_" } }
a54273
+// { dg-final { scan-assembler "_ZZN1A1bERKiS1_Ed_NKUlvE_clEv" } }
a54273
+// { dg-final { scan-assembler "_ZZN1A1bERKiS1_Ed0_NKUlvE_clEv" } }
a54273
-- 
a54273
2.27.0