Skip to content

Commit 4078d67

Browse files
committed
Move DWARFImporterDelegate into ClangImporter and remove DWARFImporter.
1 parent 5adac04 commit 4078d67

File tree

17 files changed

+84
-523
lines changed

17 files changed

+84
-523
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ namespace swift {
115115
/// Only used by lldb-moduleimport-test.
116116
bool EnableMemoryBufferImporter = false;
117117

118-
/// Enable the DWARFImporter. Only used by lldb-moduleimport-test.
119-
bool EnableDWARFImporter = false;
120-
121118
/// Allows using identifiers with a leading dollar.
122119
bool EnableDollarIdentifiers = false;
123120

include/swift/ClangImporter/ClangImporter.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,24 @@ class TypeDecl;
6060
class VisibleDeclConsumer;
6161
enum class SelectorSplitKind;
6262

63+
/// This interface is implemented by LLDB to serve as a fallback when Clang
64+
/// modules can't be imported from source in the debugger.
65+
///
66+
/// During compile time, ClangImporter-imported Clang modules are compiled with
67+
/// -gmodules, which emits a DWARF rendition of all types defined in the module
68+
/// into the .pcm file. On Darwin, these types can be collected by
69+
/// dsymutil. This delegate allows DWARFImporter to ask LLDB to look up a Clang
70+
/// type by name, synthesize a Clang AST from it. DWARFImporter then hands this
71+
/// Clang AST to ClangImporter to import the type into Swift.
72+
class DWARFImporterDelegate {
73+
public:
74+
virtual ~DWARFImporterDelegate() {}
75+
/// Perform a qualified lookup of a Clang type with this name.
76+
/// \param kind Only return results with this type kind.
77+
virtual void lookupValue(StringRef name, llvm::Optional<ClangTypeKind> kind,
78+
SmallVectorImpl<clang::Decl *> &results) {}
79+
};
80+
6381
/// Class that imports Clang modules into Swift, mapping directly
6482
/// from Clang ASTs over to Swift ASTs.
6583
class ClangImporter final : public ClangModuleLoader {
@@ -73,7 +91,13 @@ class ClangImporter final : public ClangModuleLoader {
7391
Implementation &Impl;
7492

7593
ClangImporter(ASTContext &ctx, const ClangImporterOptions &clangImporterOpts,
76-
DependencyTracker *tracker);
94+
DependencyTracker *tracker,
95+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate);
96+
97+
ModuleDecl *loadModuleClang(SourceLoc importLoc,
98+
ArrayRef<std::pair<Identifier, SourceLoc>> path);
99+
ModuleDecl *loadModuleDWARF(SourceLoc importLoc,
100+
ArrayRef<std::pair<Identifier, SourceLoc>> path);
77101

78102
public:
79103
/// Create a new Clang importer that can import a suitable Clang
@@ -89,13 +113,15 @@ class ClangImporter final : public ClangModuleLoader {
89113
///
90114
/// \param tracker The object tracking files this compilation depends on.
91115
///
116+
/// \param dwarfImporterDelegate A helper object that can synthesize
117+
/// Clang Decls from debug info. Used by LLDB.
118+
///
92119
/// \returns a new Clang module importer, or null (with a diagnostic) if
93120
/// an error occurred.
94121
static std::unique_ptr<ClangImporter>
95-
create(ASTContext &ctx,
96-
const ClangImporterOptions &importerOpts,
97-
std::string swiftPCHHash = "",
98-
DependencyTracker *tracker = nullptr);
122+
create(ASTContext &ctx, const ClangImporterOptions &importerOpts,
123+
std::string swiftPCHHash = "", DependencyTracker *tracker = nullptr,
124+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate = {});
99125

100126
ClangImporter(const ClangImporter &) = delete;
101127
ClangImporter(ClangImporter &&) = delete;

include/swift/DWARFImporter/DWARFImporter.h

Lines changed: 0 additions & 135 deletions
This file was deleted.

lib/AST/ASTDemangler.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,11 +1094,6 @@ GenericTypeDecl *ASTBuilder::findForeignTypeDecl(StringRef name,
10941094
break;
10951095
case ForeignModuleKind::Imported:
10961096
importer->lookupTypeDecl(name, *lookupKind, found);
1097-
1098-
// Try the DWARFImporter if it exists.
1099-
if (!consumer.Result)
1100-
if (auto *dwarf_importer = Ctx.getDWARFModuleLoader())
1101-
dwarf_importer->lookupTypeDecl(name, *lookupKind, found);
11021097
}
11031098

11041099
return consumer.Result;

lib/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ add_subdirectory(Basic)
2020
add_subdirectory(ClangImporter)
2121
add_subdirectory(Demangling)
2222
add_subdirectory(Driver)
23-
add_subdirectory(DWARFImporter)
2423
add_subdirectory(Frontend)
2524
add_subdirectory(FrontendTool)
2625
add_subdirectory(Index)

lib/ClangImporter/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_swift_host_library(swiftClangImporter STATIC
99
ClangAdapter.cpp
1010
ClangDiagnosticConsumer.cpp
1111
ClangImporter.cpp
12+
DWARFImporter.cpp
1213
IAMInference.cpp
1314
ImportDecl.cpp
1415
ImportEnumInfo.cpp

lib/ClangImporter/ClangImporter.cpp

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,12 @@ bool ClangImporter::Implementation::shouldIgnoreBridgeHeaderTopLevelDecl(
386386
return false;
387387
}
388388

389-
ClangImporter::ClangImporter(ASTContext &ctx,
390-
const ClangImporterOptions &clangImporterOpts,
391-
DependencyTracker *tracker)
392-
: ClangModuleLoader(tracker),
393-
Impl(*new Implementation(ctx, clangImporterOpts))
394-
{
389+
ClangImporter::ClangImporter(
390+
ASTContext &ctx, const ClangImporterOptions &clangImporterOpts,
391+
DependencyTracker *tracker,
392+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate)
393+
: ClangModuleLoader(tracker),
394+
Impl(*new Implementation(ctx, clangImporterOpts, dwarfImporterDelegate)) {
395395
}
396396

397397
ClangImporter::~ClangImporter() {
@@ -909,13 +909,12 @@ ClangImporter::getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
909909
return PCHFilename.getValue();
910910
}
911911

912-
std::unique_ptr<ClangImporter>
913-
ClangImporter::create(ASTContext &ctx,
914-
const ClangImporterOptions &importerOpts,
915-
std::string swiftPCHHash,
916-
DependencyTracker *tracker) {
912+
std::unique_ptr<ClangImporter> ClangImporter::create(
913+
ASTContext &ctx, const ClangImporterOptions &importerOpts,
914+
std::string swiftPCHHash, DependencyTracker *tracker,
915+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate) {
917916
std::unique_ptr<ClangImporter> importer{
918-
new ClangImporter(ctx, importerOpts, tracker)
917+
new ClangImporter(ctx, importerOpts, tracker, dwarfImporterDelegate)
919918
};
920919

921920
std::vector<std::string> invocationArgStrs;
@@ -1575,7 +1574,7 @@ bool ClangImporter::canImportModule(std::pair<Identifier, SourceLoc> moduleID) {
15751574
return clangModule->isAvailable(ctx.getLangOpts(), getTargetInfo(), r, mh, m);
15761575
}
15771576

1578-
ModuleDecl *ClangImporter::loadModule(
1577+
ModuleDecl *ClangImporter::loadModuleClang(
15791578
SourceLoc importLoc,
15801579
ArrayRef<std::pair<Identifier, SourceLoc>> path) {
15811580
auto &clangContext = Impl.getClangASTContext();
@@ -1668,6 +1667,15 @@ ModuleDecl *ClangImporter::loadModule(
16681667
return Impl.finishLoadingClangModule(clangModule, /*preferOverlay=*/false);
16691668
}
16701669

1670+
ModuleDecl *ClangImporter::loadModule(
1671+
SourceLoc importLoc,
1672+
ArrayRef<std::pair<Identifier, SourceLoc>> path) {
1673+
ModuleDecl *MD = loadModuleClang(importLoc, path);
1674+
if (!MD)
1675+
MD = loadModuleDWARF(importLoc, path);
1676+
return MD;
1677+
}
1678+
16711679
ModuleDecl *ClangImporter::Implementation::finishLoadingClangModule(
16721680
const clang::Module *clangModule,
16731681
bool findOverlay) {
@@ -1834,8 +1842,9 @@ bool PlatformAvailability::treatDeprecatedAsUnavailable(
18341842
llvm_unreachable("Unexpected platform");
18351843
}
18361844

1837-
ClangImporter::Implementation::Implementation(ASTContext &ctx,
1838-
const ClangImporterOptions &opts)
1845+
ClangImporter::Implementation::Implementation(
1846+
ASTContext &ctx, const ClangImporterOptions &opts,
1847+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate)
18391848
: SwiftContext(ctx),
18401849
ImportForwardDeclarations(opts.ImportForwardDeclarations),
18411850
InferImportAsMember(opts.InferImportAsMember),
@@ -1845,8 +1854,8 @@ ClangImporter::Implementation::Implementation(ASTContext &ctx,
18451854
IsReadingBridgingPCH(false),
18461855
CurrentVersion(ImportNameVersion::fromOptions(ctx.LangOpts)),
18471856
BridgingHeaderLookupTable(new SwiftLookupTable(nullptr)),
1848-
platformAvailability(ctx.LangOpts),
1849-
nameImporter() {}
1857+
platformAvailability(ctx.LangOpts), nameImporter(),
1858+
DWARFImporter(dwarfImporterDelegate) {}
18501859

18511860
ClangImporter::Implementation::~Implementation() {
18521861
#ifndef NDEBUG
@@ -2489,10 +2498,25 @@ bool ClangImporter::lookupDeclsFromHeader(StringRef Filename,
24892498
}
24902499

24912500
void ClangImporter::lookupValue(DeclName name, VisibleDeclConsumer &consumer){
2501+
struct ForwardingConsumer : VisibleDeclConsumer {
2502+
VisibleDeclConsumer &consumer;
2503+
bool foundAny = false;
2504+
2505+
ForwardingConsumer(ForwardingConsumer &forwardee) : consumer(forwardee) {}
2506+
2507+
void foundDecl(ValueDecl *decl, DeclVisibilityKind reason,
2508+
DynamicLookupInfo dynamicLookupInfo = {}) override {
2509+
FoundAny = true;
2510+
consumer.foundDecl(decl, reason, dynamicLookupInfo);
2511+
}
2512+
} forwarder(consumer);
2513+
24922514
Impl.forEachLookupTable([&](SwiftLookupTable &table) -> bool {
2493-
Impl.lookupValue(table, name, consumer);
2515+
Impl.lookupValue(table, name, forwarder);
24942516
return false;
24952517
});
2518+
if (!forwarder.foundAny)
2519+
Impl.lookupValueDWARF(name, consumer);
24962520
}
24972521

24982522
void ClangImporter::lookupTypeDecl(

lib/ClangImporter/ImporterImpl.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
318318
public LazyConformanceLoader
319319
{
320320
friend class ClangImporter;
321-
friend class DWARFImporter;
322321
using Version = importer::ImportNameVersion;
323322

324323
public:
325-
Implementation(ASTContext &ctx, const ClangImporterOptions &opts);
324+
Implementation(ASTContext &ctx, const ClangImporterOptions &opts,
325+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate);
326326
~Implementation();
327327

328328
/// Swift AST context.
@@ -603,6 +603,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
603603
void addBridgeHeaderTopLevelDecls(clang::Decl *D);
604604
bool shouldIgnoreBridgeHeaderTopLevelDecl(clang::Decl *D);
605605

606+
private:
607+
/// The DWARF importer delegate, if installed.
608+
std::unique_ptr<DWARFImporterDelegate> DWARFImporter;
609+
606610
public:
607611
void recordImplicitUnwrapForDecl(Decl *decl, bool isIUO) {
608612
#if !defined(NDEBUG)
@@ -1308,6 +1312,10 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
13081312
void lookupValue(SwiftLookupTable &table, DeclName name,
13091313
VisibleDeclConsumer &consumer);
13101314

1315+
/// Look for namespace-scope values with the given name using the
1316+
/// DWARFImporterDelegate.
1317+
void lookupValueDWARF(DeclName name, VisibleDeclConsumer &consumer);
1318+
13111319
/// Look for namespace-scope values in the given Swift lookup table.
13121320
void lookupVisibleDecls(SwiftLookupTable &table,
13131321
VisibleDeclConsumer &consumer);

lib/DWARFImporter/CMakeLists.txt

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)