From 392e141079a198c93b19bfcd1fe2bd5df456c999 Mon Sep 17 00:00:00 2001 From: Iain Buclaw Date: Sun, 29 Aug 2021 20:26:06 +0200 Subject: [PATCH] libiberty: Add support for demangling local D template declarations The D language now allows multiple different template declarations in the same function that have the same mangled name. To make the mangled names unique, a fake parent in the form `__Sddd' is added to the symbol. This information is not important for the user, so the demangler now handles and ignores it. libiberty/ChangeLog: * d-demangle.c (dlang_identifier): Skip over fake parent manglings. * testsuite/d-demangle-expected: Add tests. --- libiberty/d-demangle.c | 19 +++++++++++++++++++ libiberty/testsuite/d-demangle-expected | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/libiberty/d-demangle.c b/libiberty/d-demangle.c index d74cf47b1a9..a2152cc6551 100644 --- a/libiberty/d-demangle.c +++ b/libiberty/d-demangle.c @@ -1044,6 +1044,25 @@ dlang_identifier (string *decl, const char *mangled, struct dlang_info *info) && (mangled[2] == 'T' || mangled[2] == 'U')) return dlang_parse_template (decl, mangled, info, len); + /* There can be multiple different declarations in the same function that have + the same mangled name. To make the mangled names unique, a fake parent in + the form `__Sddd' is added to the symbol. */ + if (len >= 4 && mangled[0] == '_' && mangled[1] == '_' && mangled[2] == 'S') + { + const char *numptr = mangled + 3; + while (numptr < (mangled + len) && ISDIGIT (*numptr)) + numptr++; + + if (mangled + len == numptr) + { + /* Skip over the fake parent. */ + mangled += len; + return dlang_identifier (decl, mangled, info); + } + + /* else demangle it as a plain identifier. */ + } + return dlang_lname (decl, mangled, len); } diff --git a/libiberty/testsuite/d-demangle-expected b/libiberty/testsuite/d-demangle-expected index 87ed8d330a8..c35185c3e1e 100644 --- a/libiberty/testsuite/d-demangle-expected +++ b/libiberty/testsuite/d-demangle-expected @@ -1420,5 +1420,29 @@ _D3std3uni__T6toCaseS_DQvQt12toLowerIndexFNaNbNiNewZtVii1043S_DQCjQCi10toLowerTa std.uni.toCase!(std.uni.toLowerIndex(dchar), 1043, std.uni.toLowerTab(ulong), std.ascii.toLower, immutable(char)[]).toCase(immutable(char)[]).__foreachbody2(ref ulong, ref dchar).__foreachbody3(ref dchar) # --format=dlang +_D8demangle4mainFZ1xi +demangle.main().x +# +--format=dlang +_D8demangle4mainFZ4__S11xi +demangle.main().x +# +--format=dlang +_D8demangle4mainFZ1fMFNaNbNiNfZv +demangle.main().f() +# +--format=dlang +_D8demangle4mainFZ4__S11fMFNaNbNiNfZv +demangle.main().f() +# +--format=dlang +_D3mod4funcFZ__T6nestedTiZQkMFNaNbNiNfZi +mod.func().nested!(int).nested() +# +--format=dlang +_D3mod4funcFZ__T6nestedTiZ4__S1QpMFNaNbNiNfZi +mod.func().nested!(int).nested() +# +--format=dlang _D6mangle__T8fun21753VSQv6S21753S1f_DQBj10__lambda71MFNaNbNiNfZvZQCbQp mangle.fun21753!(mangle.S21753(mangle.__lambda71())).fun21753