Skip to content

Commit c08a627

Browse files
committed
Move DWARFImporterDelegate into ClangImporter and remove DWARFImporter.
This refactors DWARFImporter to become a part of ClangImporter, since it needs access to many of its implementation details anyway. The DWARFImporterDelegate is just another mechanism for deserializing Clang ASTs and once we have a Clang AST, the processing is effectively the same.
1 parent 5adac04 commit c08a627

File tree

24 files changed

+277
-568
lines changed

24 files changed

+277
-568
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -71,32 +71,6 @@ class ClangModuleLoader : public ModuleLoader {
7171
virtual bool
7272
isInOverlayModuleForImportedModule(const DeclContext *overlayDC,
7373
const DeclContext *importedDC) = 0;
74-
75-
/// Look for declarations associated with the given name.
76-
///
77-
/// \param name The name we're searching for.
78-
virtual void lookupValue(DeclName name, VisibleDeclConsumer &consumer) {}
79-
80-
/// Look up a type declaration by its Clang name.
81-
///
82-
/// Note that this method does no filtering. If it finds the type in a loaded
83-
/// module, it returns it. This is intended for use in reflection / debugging
84-
/// contexts where access is not a problem.
85-
virtual void lookupTypeDecl(StringRef clangName, ClangTypeKind kind,
86-
llvm::function_ref<void(TypeDecl *)> receiver) {}
87-
88-
/// Look up type a declaration synthesized by the Clang importer itself, using
89-
/// a "related entity kind" to determine which type it should be. For example,
90-
/// this can be used to find the synthesized error struct for an
91-
/// NS_ERROR_ENUM.
92-
///
93-
/// Note that this method does no filtering. If it finds the type in a loaded
94-
/// module, it returns it. This is intended for use in reflection / debugging
95-
/// contexts where access is not a problem.
96-
virtual void
97-
lookupRelatedEntity(StringRef clangName, ClangTypeKind kind,
98-
StringRef relatedEntityKind,
99-
llvm::function_ref<void(TypeDecl *)> receiver) {}
10074
};
10175

10276
} // namespace swift

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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,28 @@ 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 {
6684
friend class ClangModuleUnit;
67-
friend class DWARFImporter;
6885

6986
public:
7087
class Implementation;
@@ -73,8 +90,11 @@ class ClangImporter final : public ClangModuleLoader {
7390
Implementation &Impl;
7491

7592
ClangImporter(ASTContext &ctx, const ClangImporterOptions &clangImporterOpts,
76-
DependencyTracker *tracker);
93+
DependencyTracker *tracker,
94+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate);
7795

96+
ModuleDecl *loadModuleClang(SourceLoc importLoc,
97+
ArrayRef<std::pair<Identifier, SourceLoc>> path);
7898
public:
7999
/// Create a new Clang importer that can import a suitable Clang
80100
/// module into the given ASTContext.
@@ -89,13 +109,15 @@ class ClangImporter final : public ClangModuleLoader {
89109
///
90110
/// \param tracker The object tracking files this compilation depends on.
91111
///
112+
/// \param dwarfImporterDelegate A helper object that can synthesize
113+
/// Clang Decls from debug info. Used by LLDB.
114+
///
92115
/// \returns a new Clang module importer, or null (with a diagnostic) if
93116
/// an error occurred.
94117
static std::unique_ptr<ClangImporter>
95-
create(ASTContext &ctx,
96-
const ClangImporterOptions &importerOpts,
97-
std::string swiftPCHHash = "",
98-
DependencyTracker *tracker = nullptr);
118+
create(ASTContext &ctx, const ClangImporterOptions &importerOpts,
119+
std::string swiftPCHHash = "", DependencyTracker *tracker = nullptr,
120+
std::unique_ptr<DWARFImporterDelegate> dwarfImporterDelegate = {});
99121

100122
ClangImporter(const ClangImporter &) = delete;
101123
ClangImporter(ClangImporter &&) = delete;
@@ -150,15 +172,15 @@ class ClangImporter final : public ClangModuleLoader {
150172
/// Look for declarations associated with the given name.
151173
///
152174
/// \param name The name we're searching for.
153-
void lookupValue(DeclName name, VisibleDeclConsumer &consumer) override;
175+
void lookupValue(DeclName name, VisibleDeclConsumer &consumer);
154176

155177
/// Look up a type declaration by its Clang name.
156178
///
157179
/// Note that this method does no filtering. If it finds the type in a loaded
158180
/// module, it returns it. This is intended for use in reflection / debugging
159181
/// contexts where access is not a problem.
160182
void lookupTypeDecl(StringRef clangName, ClangTypeKind kind,
161-
llvm::function_ref<void(TypeDecl *)> receiver) override;
183+
llvm::function_ref<void(TypeDecl *)> receiver);
162184

163185
/// Look up type a declaration synthesized by the Clang importer itself, using
164186
/// a "related entity kind" to determine which type it should be. For example,
@@ -171,7 +193,7 @@ class ClangImporter final : public ClangModuleLoader {
171193
void
172194
lookupRelatedEntity(StringRef clangName, ClangTypeKind kind,
173195
StringRef relatedEntityKind,
174-
llvm::function_ref<void(TypeDecl *)> receiver) override;
196+
llvm::function_ref<void(TypeDecl *)> receiver);
175197

176198
/// Look for textually included declarations from the bridging header.
177199
///

include/swift/DWARFImporter/DWARFImporter.h

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

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
#include "swift/AST/ASTDemangler.h"
2323

2424
#include "swift/AST/ASTContext.h"
25-
#include "swift/AST/ClangModuleLoader.h"
2625
#include "swift/AST/Decl.h"
2726
#include "swift/AST/GenericSignature.h"
2827
#include "swift/AST/GenericSignatureBuilder.h"
2928
#include "swift/AST/Module.h"
3029
#include "swift/AST/NameLookup.h"
3130
#include "swift/AST/Type.h"
3231
#include "swift/AST/Types.h"
32+
#include "swift/ClangImporter/ClangImporter.h"
3333
#include "swift/Demangling/Demangler.h"
3434
#include "swift/Demangling/ManglingMacros.h"
3535

@@ -1045,7 +1045,7 @@ GenericTypeDecl *ASTBuilder::findForeignTypeDecl(StringRef name,
10451045
ForeignModuleKind foreignKind,
10461046
Demangle::Node::Kind kind) {
10471047
// Check to see if we have an importer loaded.
1048-
auto importer = Ctx.getClangModuleLoader();
1048+
auto importer = static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
10491049
if (!importer)
10501050
return nullptr;
10511051

@@ -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

0 commit comments

Comments
 (0)