Skip to content

Commit c7e9809

Browse files
committed
[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.
1 parent 169096c commit c7e9809

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ class ASTMangler : public Mangler {
106106
/// defined in.
107107
bool RespectOriginallyDefinedIn = true;
108108

109+
/// Whether to always mangle using the declaration's API, ignoring e.g
110+
/// attached `@abi` attributes. This is necessary for USR mangling since for
111+
/// semantic functionality we're only concerned about the API entity, and need
112+
/// to be able to do name lookups to find the original decl based on the USR.
113+
bool UseAPIMangling = false;
114+
109115
public:
110116
class SymbolicReferent {
111117
public:
@@ -188,11 +194,14 @@ class ASTMangler : public Mangler {
188194
};
189195

190196
/// lldb overrides \p DWARFMangling to 'true'.
191-
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false) : Context(Ctx) {
197+
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false,
198+
bool UseAPIMangling = false)
199+
: Context(Ctx) {
192200
if (DWARFMangling) {
193201
this->DWARFMangling = true;
194202
RespectOriginallyDefinedIn = false;
195203
}
204+
this->UseAPIMangling = UseAPIMangling;
196205
Flavor = Ctx.LangOpts.hasFeature(Feature::Embedded)
197206
? ManglingFlavor::Embedded
198207
: ManglingFlavor::Default;
@@ -201,7 +210,7 @@ class ASTMangler : public Mangler {
201210
/// Create an ASTMangler suitable for mangling a USR for use in semantic
202211
/// functionality.
203212
static ASTMangler forUSR(const ASTContext &Ctx) {
204-
return ASTMangler(Ctx, /*DWARFMangling*/ true);
213+
return ASTMangler(Ctx, /*DWARFMangling*/ true, /*UseAPIMangling*/ true);
205214
}
206215

207216
const ASTContext &getASTContext() { return Context; }
@@ -808,7 +817,7 @@ class ASTMangler : public Mangler {
808817

809818
template <typename DeclType>
810819
DeclType *getABIDecl(DeclType *D) const {
811-
if (!D)
820+
if (!D || UseAPIMangling)
812821
return nullptr;
813822

814823
auto abiRole = ABIRoleInfo(D);

test/Index/index_abi_attr.swift

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

test/SourceKit/DocSupport/doc_abi.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public func foo() {}
3434
{
3535
key.kind: source.lang.swift.decl.function.free,
3636
key.name: "foo()",
37-
key.usr: "s:3Mod3baryyF",
37+
key.usr: "s:3Mod3fooyyF",
3838
key.offset: 0,
3939
key.length: 27,
4040
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>"
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
// {"kind":"complete","original":"070481b6","signature":"swift::Mangle::ASTMangler::appendEntity(swift::ValueDecl const*)","signatureAssert":"Assertion failed: (!isa<ConstructorDecl>(decl)), function appendEntity"}
2-
// RUN: not --crash %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
2+
// RUN: %target-swift-ide-test -code-completion -batch-code-completion -skip-filecheck -code-completion-diagnostics -source-filename %s
33
@abi( init () ) func a
44
#^^#()

0 commit comments

Comments
 (0)