Skip to content

Commit 3221734

Browse files
authored
Merge pull request #34244 from artemcm/RefactorScannerBinaryDependencies
[Dependency Scanner] Refactor ModuleDependencies to represent binary-only Swift modules explicitly
2 parents 54fc26f + f9d6c6a commit 3221734

File tree

11 files changed

+358
-191
lines changed

11 files changed

+358
-191
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 97 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class Identifier;
3535

3636
/// Which kind of module dependencies we are looking for.
3737
enum class ModuleDependenciesKind : int8_t {
38-
Swift,
38+
SwiftTextual,
39+
SwiftBinary,
3940
// Placeholder dependencies are a kind of dependencies used only by the
4041
// dependency scanner. They are swift modules that the scanner will not be
4142
// able to locate in its search paths and which are the responsibility of the
@@ -69,26 +70,22 @@ class ModuleDependenciesStorageBase {
6970
public:
7071
const ModuleDependenciesKind dependencyKind;
7172

72-
ModuleDependenciesStorageBase(ModuleDependenciesKind dependencyKind,
73-
const std::string &compiledModulePath)
74-
: dependencyKind(dependencyKind),
75-
compiledModulePath(compiledModulePath) { }
73+
ModuleDependenciesStorageBase(ModuleDependenciesKind dependencyKind)
74+
: dependencyKind(dependencyKind) { }
7675

7776
virtual ModuleDependenciesStorageBase *clone() const = 0;
7877

7978
virtual ~ModuleDependenciesStorageBase();
8079

81-
/// The path to the compiled module file.
82-
const std::string compiledModulePath;
83-
8480
/// The set of modules on which this module depends.
8581
std::vector<std::string> moduleDependencies;
8682
};
8783

8884
/// Describes the dependencies of a Swift module.
8985
///
9086
/// This class is mostly an implementation detail for \c ModuleDependencies.
91-
class SwiftModuleDependenciesStorage : public ModuleDependenciesStorageBase {
87+
class SwiftTextualModuleDependenciesStorage :
88+
public ModuleDependenciesStorageBase {
9289
public:
9390
/// The Swift interface file, if it can be used to generate the module file.
9491
const Optional<std::string> swiftInterfaceFile;
@@ -123,16 +120,14 @@ class SwiftModuleDependenciesStorage : public ModuleDependenciesStorageBase {
123120
/// (Clang) modules on which the bridging header depends.
124121
std::vector<std::string> bridgingModuleDependencies;
125122

126-
SwiftModuleDependenciesStorage(
127-
const std::string &compiledModulePath,
123+
SwiftTextualModuleDependenciesStorage(
128124
const Optional<std::string> &swiftInterfaceFile,
129125
ArrayRef<std::string> compiledModuleCandidates,
130126
ArrayRef<StringRef> buildCommandLine,
131127
ArrayRef<StringRef> extraPCMArgs,
132128
StringRef contextHash,
133129
bool isFramework
134-
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::Swift,
135-
compiledModulePath),
130+
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftTextual),
136131
swiftInterfaceFile(swiftInterfaceFile),
137132
compiledModuleCandidates(compiledModuleCandidates.begin(),
138133
compiledModuleCandidates.end()),
@@ -141,11 +136,47 @@ class SwiftModuleDependenciesStorage : public ModuleDependenciesStorageBase {
141136
contextHash(contextHash), isFramework(isFramework) { }
142137

143138
ModuleDependenciesStorageBase *clone() const override {
144-
return new SwiftModuleDependenciesStorage(*this);
139+
return new SwiftTextualModuleDependenciesStorage(*this);
140+
}
141+
142+
static bool classof(const ModuleDependenciesStorageBase *base) {
143+
return base->dependencyKind == ModuleDependenciesKind::SwiftTextual;
145144
}
145+
};
146+
147+
/// Describes the dependencies of a pre-built Swift module (with no .swiftinterface).
148+
///
149+
/// This class is mostly an implementation detail for \c ModuleDependencies.
150+
class SwiftBinaryModuleDependencyStorage : public ModuleDependenciesStorageBase {
151+
public:
152+
SwiftBinaryModuleDependencyStorage(const std::string &compiledModulePath,
153+
const std::string &moduleDocPath,
154+
const std::string &sourceInfoPath,
155+
const bool isFramework)
156+
: ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftBinary),
157+
compiledModulePath(compiledModulePath),
158+
moduleDocPath(moduleDocPath),
159+
sourceInfoPath(sourceInfoPath),
160+
isFramework(isFramework) {}
161+
162+
ModuleDependenciesStorageBase *clone() const override {
163+
return new SwiftBinaryModuleDependencyStorage(*this);
164+
}
165+
166+
/// The path to the .swiftmodule file.
167+
const std::string compiledModulePath;
168+
169+
/// The path to the .swiftModuleDoc file.
170+
const std::string moduleDocPath;
171+
172+
/// The path to the .swiftSourceInfo file.
173+
const std::string sourceInfoPath;
174+
175+
/// A flag that indicates this dependency is a framework
176+
const bool isFramework;
146177

147178
static bool classof(const ModuleDependenciesStorageBase *base) {
148-
return base->dependencyKind == ModuleDependenciesKind::Swift;
179+
return base->dependencyKind == ModuleDependenciesKind::SwiftBinary;
149180
}
150181
};
151182

@@ -167,13 +198,11 @@ class ClangModuleDependenciesStorage : public ModuleDependenciesStorageBase {
167198
const std::vector<std::string> fileDependencies;
168199

169200
ClangModuleDependenciesStorage(
170-
const std::string &compiledModulePath,
171201
const std::string &moduleMapFile,
172202
const std::string &contextHash,
173203
const std::vector<std::string> &nonPathCommandLine,
174204
const std::vector<std::string> &fileDependencies
175-
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::Clang,
176-
compiledModulePath),
205+
) : ModuleDependenciesStorageBase(ModuleDependenciesKind::Clang),
177206
moduleMapFile(moduleMapFile),
178207
contextHash(contextHash),
179208
nonPathCommandLine(nonPathCommandLine),
@@ -191,20 +220,24 @@ class ClangModuleDependenciesStorage : public ModuleDependenciesStorageBase {
191220
/// Describes an placeholder Swift module dependency module stub.
192221
///
193222
/// This class is mostly an implementation detail for \c ModuleDependencies.
194-
class PlaceholderSwiftModuleDependencyStorage : public ModuleDependenciesStorageBase {
223+
224+
class SwiftPlaceholderModuleDependencyStorage : public ModuleDependenciesStorageBase {
195225
public:
196-
PlaceholderSwiftModuleDependencyStorage(const std::string &compiledModulePath,
197-
const std::string &moduleDocPath,
198-
const std::string &sourceInfoPath)
199-
: ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftPlaceholder,
200-
compiledModulePath),
226+
SwiftPlaceholderModuleDependencyStorage(const std::string &compiledModulePath,
227+
const std::string &moduleDocPath,
228+
const std::string &sourceInfoPath)
229+
: ModuleDependenciesStorageBase(ModuleDependenciesKind::SwiftPlaceholder),
230+
compiledModulePath(compiledModulePath),
201231
moduleDocPath(moduleDocPath),
202232
sourceInfoPath(sourceInfoPath) {}
203233

204234
ModuleDependenciesStorageBase *clone() const override {
205-
return new PlaceholderSwiftModuleDependencyStorage(*this);
235+
return new SwiftPlaceholderModuleDependencyStorage(*this);
206236
}
207237

238+
/// The path to the .swiftmodule file.
239+
const std::string compiledModulePath;
240+
208241
/// The path to the .swiftModuleDoc file.
209242
const std::string moduleDocPath;
210243

@@ -249,43 +282,41 @@ class ModuleDependencies {
249282
ArrayRef<StringRef> extraPCMArgs,
250283
StringRef contextHash,
251284
bool isFramework) {
252-
std::string compiledModulePath;
253285
return ModuleDependencies(
254-
std::make_unique<SwiftModuleDependenciesStorage>(
255-
compiledModulePath, swiftInterfaceFile, compiledCandidates, buildCommands,
286+
std::make_unique<SwiftTextualModuleDependenciesStorage>(
287+
swiftInterfaceFile, compiledCandidates, buildCommands,
256288
extraPCMArgs, contextHash, isFramework));
257289
}
258290

259291
/// Describe the module dependencies for a serialized or parsed Swift module.
260-
static ModuleDependencies forSwiftModule(
261-
const std::string &compiledModulePath, bool isFramework) {
292+
static ModuleDependencies forSwiftBinaryModule(
293+
const std::string &compiledModulePath,
294+
const std::string &moduleDocPath,
295+
const std::string &sourceInfoPath,
296+
bool isFramework) {
262297
return ModuleDependencies(
263-
std::make_unique<SwiftModuleDependenciesStorage>(
264-
compiledModulePath, None, ArrayRef<std::string>(), ArrayRef<StringRef>(),
265-
ArrayRef<StringRef>(), StringRef(), isFramework));
298+
std::make_unique<SwiftBinaryModuleDependencyStorage>(
299+
compiledModulePath, moduleDocPath, sourceInfoPath, isFramework));
266300
}
267301

268302
/// Describe the main Swift module.
269303
static ModuleDependencies forMainSwiftModule(ArrayRef<StringRef> extraPCMArgs) {
270-
std::string compiledModulePath;
271304
return ModuleDependencies(
272-
std::make_unique<SwiftModuleDependenciesStorage>(
273-
compiledModulePath, None, ArrayRef<std::string>(),
274-
ArrayRef<StringRef>(), extraPCMArgs, StringRef(), false));
305+
std::make_unique<SwiftTextualModuleDependenciesStorage>(
306+
None, ArrayRef<std::string>(), ArrayRef<StringRef>(),
307+
extraPCMArgs, StringRef(), false));
275308
}
276309

277310
/// Describe the module dependencies for a Clang module that can be
278311
/// built from a module map and headers.
279312
static ModuleDependencies forClangModule(
280-
const std::string &compiledModulePath,
281313
const std::string &moduleMapFile,
282314
const std::string &contextHash,
283315
const std::vector<std::string> &nonPathCommandLine,
284316
const std::vector<std::string> &fileDependencies) {
285317
return ModuleDependencies(
286318
std::make_unique<ClangModuleDependenciesStorage>(
287-
compiledModulePath, moduleMapFile, contextHash, nonPathCommandLine,
288-
fileDependencies));
319+
moduleMapFile, contextHash, nonPathCommandLine, fileDependencies));
289320
}
290321

291322
/// Describe a placeholder dependency swift module.
@@ -294,38 +325,45 @@ class ModuleDependencies {
294325
const std::string &moduleDocPath,
295326
const std::string &sourceInfoPath) {
296327
return ModuleDependencies(
297-
std::make_unique<PlaceholderSwiftModuleDependencyStorage>(
328+
std::make_unique<SwiftPlaceholderModuleDependencyStorage>(
298329
compiledModulePath, moduleDocPath, sourceInfoPath));
299330
}
300331

301-
/// Retrieve the path to the compiled module.
302-
const std::string getCompiledModulePath() const {
303-
return storage->compiledModulePath;
304-
}
305-
306332
/// Retrieve the module-level dependencies.
307333
ArrayRef<std::string> getModuleDependencies() const {
308334
return storage->moduleDependencies;
309335
}
310336

311-
/// Whether the dependencies are for a Swift module.
337+
/// Whether the dependencies are for a Swift module: either Textual, Binary, or Placeholder.
312338
bool isSwiftModule() const;
313339

340+
/// Whether the dependencies are for a textual Swift module.
341+
bool isSwiftTextualModule() const;
342+
343+
/// Whether the dependencies are for a binary Swift module.
344+
bool isSwiftBinaryModule() const;
345+
314346
/// Whether this represents a placeholder module stub
315-
bool isPlaceholderSwiftModule() const;
347+
bool isSwiftPlaceholderModule() const;
348+
349+
/// Whether the dependencies are for a Clang module.
350+
bool isClangModule() const;
316351

317352
ModuleDependenciesKind getKind() const {
318353
return storage->dependencyKind;
319354
}
320355
/// Retrieve the dependencies for a Swift module.
321-
const SwiftModuleDependenciesStorage *getAsSwiftModule() const;
356+
const SwiftTextualModuleDependenciesStorage *getAsSwiftTextualModule() const;
357+
358+
/// Retrieve the dependencies for a binary Swift module.
359+
const SwiftBinaryModuleDependencyStorage *getAsSwiftBinaryModule() const;
322360

323361
/// Retrieve the dependencies for a Clang module.
324362
const ClangModuleDependenciesStorage *getAsClangModule() const;
325363

326364
/// Retrieve the dependencies for a placeholder dependency module stub.
327-
const PlaceholderSwiftModuleDependencyStorage *
328-
getAsPlaceholderDependencyModule() const;
365+
const SwiftPlaceholderModuleDependencyStorage *
366+
getAsPlaceholderDependencyModule() const;
329367

330368
/// Add a dependency on the given module, if it was not already in the set.
331369
void addModuleDependency(StringRef module,
@@ -370,11 +408,14 @@ class ModuleDependenciesCache {
370408
/// encountered.
371409
std::vector<ModuleDependencyID> AllModules;
372410

373-
/// Dependencies for Swift modules that have already been computed.
374-
llvm::StringMap<ModuleDependencies> SwiftModuleDependencies;
411+
/// Dependencies for Textual Swift modules that have already been computed.
412+
llvm::StringMap<ModuleDependencies> SwiftTextualModuleDependencies;
375413

376-
/// Dependencies for Swift placeholder dependency modules.
377-
llvm::StringMap<ModuleDependencies> PlaceholderSwiftModuleDependencies;
414+
/// Dependencies for Binary Swift modules that have already been computed.
415+
llvm::StringMap<ModuleDependencies> SwiftBinaryModuleDependencies;
416+
417+
/// Dependencies for Swift placeholder dependency modules that have already been computed.
418+
llvm::StringMap<ModuleDependencies> SwiftPlaceholderModuleDependencies;
378419

379420
/// Dependencies for Clang modules that have already been computed.
380421
llvm::StringMap<ModuleDependencies> ClangModuleDependencies;
@@ -436,8 +477,7 @@ class ModuleDependenciesCache {
436477

437478
/// Record dependencies for the given module.
438479
void recordDependencies(StringRef moduleName,
439-
ModuleDependencies dependencies,
440-
ModuleDependenciesKind kind);
480+
ModuleDependencies dependencies);
441481

442482
/// Update stored dependencies for the given module.
443483
void updateDependencies(ModuleDependencyID moduleID,

include/swift/Serialization/ModuleDependencyScanner.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,13 @@ namespace swift {
4040
public:
4141
Optional<ModuleDependencies> dependencies;
4242

43-
/// Describes the kind of dependencies this scanner is able to identify
44-
ModuleDependenciesKind dependencyKind;
45-
4643
ModuleDependencyScanner(
4744
ASTContext &ctx, ModuleLoadingMode LoadMode, Identifier moduleName,
4845
InterfaceSubContextDelegate &astDelegate,
49-
ModuleDependenciesKind dependencyKind = ModuleDependenciesKind::Swift,
5046
ScannerKind kind = MDS_plain)
5147
: SerializedModuleLoaderBase(ctx, nullptr, LoadMode,
5248
/*IgnoreSwiftSourceInfoFile=*/true),
53-
kind(kind), moduleName(moduleName), astDelegate(astDelegate),
54-
dependencyKind(dependencyKind) {}
49+
kind(kind), moduleName(moduleName), astDelegate(astDelegate) {}
5550

5651
std::error_code findModuleFilesInDirectory(
5752
ImportPath::Element ModuleID,
@@ -60,7 +55,7 @@ namespace swift {
6055
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
6156
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
6257
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
63-
bool IsFramework) override;
58+
bool IsFramework) override;
6459

6560
virtual void collectVisibleTopLevelModuleNames(
6661
SmallVectorImpl<Identifier> &names) const override {
@@ -106,7 +101,6 @@ namespace swift {
106101
StringRef PlaceholderDependencyModuleMap,
107102
InterfaceSubContextDelegate &astDelegate)
108103
: ModuleDependencyScanner(ctx, LoadMode, moduleName, astDelegate,
109-
ModuleDependenciesKind::SwiftPlaceholder,
110104
MDS_placeholder) {
111105

112106
// FIXME: Find a better place for this map to live, to avoid

0 commit comments

Comments
 (0)