[IDE] Mangle USRs using API decls rather than ABI decls

For semantic functionality the API decl is the more useful thing to
mangle, and redeclaration checking ensures we can't end up with
conflicts. This ensures we're able to perform a name lookup for the
decl based on its USR without having to maintain a separate lookup 
table for ABI names.
This commit is contained in:
Hamish Knight
2025-10-23 09:11:17 +01:00
parent 169096c233
commit c7e9809480
4 changed files with 20 additions and 5 deletions

View File

@@ -106,6 +106,12 @@ protected:
/// defined in.
bool RespectOriginallyDefinedIn = true;
/// Whether to always mangle using the declaration's API, ignoring e.g
/// attached `@abi` attributes. This is necessary for USR mangling since for
/// semantic functionality we're only concerned about the API entity, and need
/// to be able to do name lookups to find the original decl based on the USR.
bool UseAPIMangling = false;
public:
class SymbolicReferent {
public:
@@ -188,11 +194,14 @@ public:
};
/// lldb overrides \p DWARFMangling to 'true'.
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false) : Context(Ctx) {
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false,
bool UseAPIMangling = false)
: Context(Ctx) {
if (DWARFMangling) {
this->DWARFMangling = true;
RespectOriginallyDefinedIn = false;
}
this->UseAPIMangling = UseAPIMangling;
Flavor = Ctx.LangOpts.hasFeature(Feature::Embedded)
? ManglingFlavor::Embedded
: ManglingFlavor::Default;
@@ -201,7 +210,7 @@ public:
/// Create an ASTMangler suitable for mangling a USR for use in semantic
/// functionality.
static ASTMangler forUSR(const ASTContext &Ctx) {
return ASTMangler(Ctx, /*DWARFMangling*/ true);
return ASTMangler(Ctx, /*DWARFMangling*/ true, /*UseAPIMangling*/ true);
}
const ASTContext &getASTContext() { return Context; }
@@ -808,7 +817,7 @@ protected:
template <typename DeclType>
DeclType *getABIDecl(DeclType *D) const {
if (!D)
if (!D || UseAPIMangling)
return nullptr;
auto abiRole = ABIRoleInfo(D);

View File

@@ -0,0 +1,6 @@
// RUN: %target-swift-ide-test -print-indexed-symbols -include-locals -source-filename %s | %FileCheck %s
// Make sure we use the API decl for mangling the USR.
@abi(func bar())
public func foo() {}
// CHECK: [[@LINE-1]]:13 | function/Swift | foo() | s:14swift_ide_test3fooyyF | Def | rel: 0

View File

@@ -34,7 +34,7 @@ public func foo() {}
{
key.kind: source.lang.swift.decl.function.free,
key.name: "foo()",
key.usr: "s:3Mod3baryyF",
key.usr: "s:3Mod3fooyyF",
key.offset: 0,
key.length: 27,
key.fully_annotated_decl: "<decl.function.free><syntaxtype.attribute.builtin>@abi(<decl.function.free><syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>bar</decl.name>()</decl.function.free>)</syntaxtype.attribute.builtin> <syntaxtype.keyword>func</syntaxtype.keyword> <decl.name>foo</decl.name>()</decl.function.free>"

View File

@@ -1,4 +1,4 @@
// {"kind":"complete","original":"070481b6","signature":"swift::Mangle::ASTMangler::appendEntity(swift::ValueDecl const*)","signatureAssert":"Assertion failed: (!isa<ConstructorDecl>(decl)), function appendEntity"}
// RUN: not --crash %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
// RUN: %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
@abi( init () ) func a
#^^#()