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