Skip to content

Commit 04034d5

Browse files
committed
Account for different module ABI name when reconstructing a type
A type's mangled name will store the module's ABI name, not the module's regular name. When reconstructing a type from a mangled name, the demangler needs to take that into account. rdar://126953614
1 parent f0aba4d commit 04034d5

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,11 @@ class ASTContext final {
11811181

11821182
ModuleDecl *getModuleByIdentifier(Identifier ModuleID);
11831183

1184+
/// Looks up an already loaded module by its ABI name.
1185+
///
1186+
/// \returns The module if found, nullptr otherwise.
1187+
ModuleDecl *getLoadedModuleByABIName(StringRef ModuleName);
1188+
11841189
/// Returns the standard library module, or null if the library isn't present.
11851190
///
11861191
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module

lib/AST/ASTContext.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,6 +2550,14 @@ ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
25502550
return getModule(builder.get());
25512551
}
25522552

2553+
ModuleDecl *ASTContext::getLoadedModuleByABIName(StringRef ModuleName) {
2554+
for (auto &[_, module] : getLoadedModules()) {
2555+
if (ModuleName == module->getABIName().str())
2556+
return module;
2557+
}
2558+
return nullptr;
2559+
}
2560+
25532561
ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
25542562
if (TheStdlibModule)
25552563
return TheStdlibModule;

lib/AST/ASTDemangler.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,16 +1105,15 @@ ASTBuilder::createTypeDecl(NodePointer node,
11051105
return dyn_cast<GenericTypeDecl>(DC);
11061106
}
11071107

1108-
ModuleDecl *
1109-
ASTBuilder::findModule(NodePointer node) {
1108+
ModuleDecl *ASTBuilder::findModule(NodePointer node) {
11101109
assert(node->getKind() == Demangle::Node::Kind::Module);
11111110
const auto moduleName = node->getText();
1112-
// Respect the main module's ABI name when we're trying to resolve
1111+
// Respect the module's ABI name when we're trying to resolve
11131112
// mangled names. But don't touch anything under the Swift stdlib's
1114-
// umbrella.
1115-
if (Ctx.MainModule && Ctx.MainModule->getABIName().is(moduleName))
1116-
if (!Ctx.MainModule->getABIName().is(STDLIB_NAME))
1117-
return Ctx.MainModule;
1113+
// umbrella.
1114+
if (moduleName != STDLIB_NAME)
1115+
if (auto *Module = Ctx.getLoadedModuleByABIName(moduleName))
1116+
return Module;
11181117

11191118
return Ctx.getModuleByName(moduleName);
11201119
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Tests that reconstructing a type from a mangled name whose type is defined
2+
// in a separate module which has a different ABI name compared to its regular
3+
// name works.
4+
5+
// RUN: %empty-directory(%t)
6+
// RUN: split-file %s %t
7+
// RUN: cd %t
8+
9+
// RUN: %target-build-swift -emit-library -emit-module -parse-as-library -module-abi-name Other -g %t/TheModule.swift
10+
// RUN: %target-build-swift -emit-executable -I %t -L %t -lTheModule %s -g -o %t/user -emit-module
11+
12+
13+
// RUN: sed -ne '/\/\/ *DEMANGLE-TYPE: /s/\/\/ *DEMANGLE-TYPE: *//p' < %s > %t/input
14+
// RUN: %lldb-moduleimport-test-with-sdk %t/user -qualify-types=1 -type-from-mangled=%t/input | %FileCheck %s --check-prefix=CHECK-TYPE
15+
16+
//--- TheModule.swift
17+
public class Foo {
18+
let i = 42
19+
public init() {
20+
}
21+
}
22+
23+
//--- user.swift
24+
25+
import TheModule
26+
27+
let c = TheModule.Foo()
28+
29+
// DEMANGLE-TYPE: $s5Other3FooCD
30+
// CHECK-TYPE: TheModule.Foo
31+

0 commit comments

Comments
 (0)