Skip to content

Commit

Permalink
Merge pull request #73291 from augusto2112/6.0-mangle-abi-mod
Browse files Browse the repository at this point in the history
Account for different module ABI name when reconstructing a type
  • Loading branch information
augusto2112 committed Apr 29, 2024
2 parents b42e726 + f6eba5a commit 763421c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
5 changes: 5 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,11 @@ class ASTContext final {

ModuleDecl *getModuleByIdentifier(Identifier ModuleID);

/// Looks up an already loaded module by its ABI name.
///
/// \returns The module if found, nullptr otherwise.
ModuleDecl *getLoadedModuleByABIName(StringRef ModuleName);

/// Returns the standard library module, or null if the library isn't present.
///
/// If \p loadIfAbsent is true, the ASTContext will attempt to load the module
Expand Down
8 changes: 8 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2550,6 +2550,14 @@ ModuleDecl *ASTContext::getModuleByIdentifier(Identifier ModuleID) {
return getModule(builder.get());
}

ModuleDecl *ASTContext::getLoadedModuleByABIName(StringRef ModuleName) {
for (auto &[_, module] : getLoadedModules()) {
if (ModuleName == module->getABIName().str())
return module;
}
return nullptr;
}

ModuleDecl *ASTContext::getStdlibModule(bool loadIfAbsent) {
if (TheStdlibModule)
return TheStdlibModule;
Expand Down
13 changes: 6 additions & 7 deletions lib/AST/ASTDemangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1082,16 +1082,15 @@ ASTBuilder::createTypeDecl(NodePointer node,
return dyn_cast<GenericTypeDecl>(DC);
}

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

return Ctx.getModuleByName(moduleName);
}
Expand Down
31 changes: 31 additions & 0 deletions test/TypeDecoder/different_abi_name.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Tests that reconstructing a type from a mangled name whose type is defined
// in a separate module which has a different ABI name compared to its regular
// name works.

// RUN: %empty-directory(%t)
// RUN: split-file %s %t
// RUN: cd %t

// RUN: %target-build-swift -emit-library -emit-module -parse-as-library -module-abi-name Other -g %t/TheModule.swift
// RUN: %target-build-swift -emit-executable -I %t -L %t -lTheModule %s -g -o %t/user -emit-module


// RUN: sed -ne '/\/\/ *DEMANGLE-TYPE: /s/\/\/ *DEMANGLE-TYPE: *//p' < %s > %t/input
// RUN: %lldb-moduleimport-test-with-sdk %t/user -qualify-types=1 -type-from-mangled=%t/input | %FileCheck %s --check-prefix=CHECK-TYPE

//--- TheModule.swift
public class Foo {
let i = 42
public init() {
}
}

//--- user.swift

import TheModule

let c = TheModule.Foo()

// DEMANGLE-TYPE: $s5Other3FooCD
// CHECK-TYPE: TheModule.Foo

0 comments on commit 763421c

Please sign in to comment.