Skip to content

Commit 1ebc78b

Browse files
committed
[RemoteAST] Using module names specified by @_originallyDefinedIn for top-level decls marked as so
This should allow runtime de-mangling of those moved symbols.
1 parent 03fab30 commit 1ebc78b

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

lib/AST/Decl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,12 @@ Result->X = SM.getLocFromExternalSource(Locs->SourceFilePath, Locs->X.Line, \
577577
}
578578

579579
StringRef Decl::getAlternateModuleName() const {
580-
if (auto *OD = Attrs.getAttribute(DeclAttrKind::DAK_OriginallyDefinedIn)) {
581-
return static_cast<const OriginallyDefinedInAttr*>(OD)->OriginalModuleName;
580+
for (auto *Att: Attrs) {
581+
if (auto *OD = dyn_cast<OriginallyDefinedInAttr>(Att)) {
582+
if (OD->isActivePlatform(getASTContext())) {
583+
return OD->OriginalModuleName;
584+
}
585+
}
582586
}
583587
for (auto *DC = getDeclContext(); DC; DC = DC->getParent()) {
584588
if (auto decl = DC->getAsDecl()) {

lib/IRGen/GenDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,17 @@ IRGenModule::getAddrOfContextDescriptorForParent(DeclContext *parent,
788788
LLVM_FALLTHROUGH;
789789

790790
case DeclContextKind::Module:
791+
if (auto *D = ofChild->getAsDecl()) {
792+
// If the top-level decl has been marked as moved from another module,
793+
// using @_originallyDefinedIn, we should emit the original module as
794+
// the context because all run-time names of this decl are based on the
795+
// original module name.
796+
auto OriginalModule = D->getAlternateModuleName();
797+
if (!OriginalModule.empty()) {
798+
return {getAddrOfOriginalModuleContextDescriptor(OriginalModule),
799+
ConstantReference::Direct};
800+
}
801+
}
791802
return {getAddrOfModuleContextDescriptor(cast<ModuleDecl>(parent)),
792803
ConstantReference::Direct};
793804
}

lib/IRGen/GenMeta.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,6 +1908,13 @@ IRGenModule::getAddrOfAnonymousContextDescriptor(
19081908
[&]{ AnonymousContextDescriptorBuilder(*this, DC).emit(); });
19091909
}
19101910

1911+
llvm::Constant *
1912+
IRGenModule::getAddrOfOriginalModuleContextDescriptor(StringRef Name) {
1913+
return getAddrOfModuleContextDescriptor(OriginalModules.insert({Name,
1914+
ModuleDecl::create(Context.getIdentifier(Name), Context)})
1915+
.first->getValue());
1916+
}
1917+
19111918
static void emitInitializeFieldOffsetVector(IRGenFunction &IGF,
19121919
SILType T,
19131920
llvm::Value *metadata,

lib/IRGen/IRGenModule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ class IRGenModule {
510510
ModuleDecl *ClangImporterModule = nullptr;
511511
SourceFile *CurSourceFile = nullptr;
512512

513+
llvm::StringMap<ModuleDecl*> OriginalModules;
513514
llvm::SmallString<128> OutputFilename;
514515
llvm::SmallString<128> MainInputFilenameForDebugInfo;
515516

@@ -1340,6 +1341,7 @@ private: \
13401341
ConstantInit definition = ConstantInit());
13411342
llvm::Constant *getAddrOfObjCModuleContextDescriptor();
13421343
llvm::Constant *getAddrOfClangImporterModuleContextDescriptor();
1344+
llvm::Constant *getAddrOfOriginalModuleContextDescriptor(StringRef Name);
13431345
ConstantReference getAddrOfParentContextDescriptor(DeclContext *from,
13441346
bool fromAnonymousContext);
13451347
ConstantReference getAddrOfContextDescriptorForParent(DeclContext *parent,

0 commit comments

Comments
 (0)