Skip to content

Commit e60c7fe

Browse files
committed
Add request to query primary files
Remove the `PrimarySourceFiles` vector from the frontend and replace it with a request on ModuleDecl that retrieves the primary files for the main module. This is in preparation for having `CompilerInstance::getMainModule` automatically populate the main module with files when queried.
1 parent 71b2c50 commit e60c7fe

File tree

7 files changed

+67
-16
lines changed

7 files changed

+67
-16
lines changed

include/swift/AST/Module.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,10 @@ class ModuleDecl : public DeclContext, public TypeDecl {
548548
return Bits.ModuleDecl.IsMainModule;
549549
}
550550

551+
/// For the main module, retrieves the list of primary source files being
552+
/// compiled, that is, the files we're generating code for.
553+
ArrayRef<SourceFile *> getPrimarySourceFiles() const;
554+
551555
/// Retrieve the top-level module. If this module is already top-level, this
552556
/// returns itself. If this is a submodule such as \c Foo.Bar.Baz, this
553557
/// returns the module \c Foo.

include/swift/AST/SourceFile.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@ class SourceFile final : public FileUnit {
158158
/// The parsing options for the file.
159159
ParsingOptions ParsingOpts;
160160

161+
/// Whether this is a primary source file which we'll be generating code for.
162+
bool IsPrimary;
163+
161164
/// The scope map that describes this source file.
162165
std::unique_ptr<ASTScope> Scope;
163166

@@ -232,6 +235,10 @@ class SourceFile final : public FileUnit {
232235
/// Retrieve the parsing options for the file.
233236
ParsingOptions getParsingOptions() const { return ParsingOpts; }
234237

238+
/// Whether this source file is a primary file, meaning that we're generating
239+
/// code for it. Note this method returns \c false in WMO.
240+
bool isPrimary() const { return IsPrimary; }
241+
235242
/// A cache of syntax nodes that can be reused when creating the syntax tree
236243
/// for this file.
237244
swift::SyntaxParsingCache *SyntaxParsingCache = nullptr;
@@ -307,7 +314,7 @@ class SourceFile final : public FileUnit {
307314

308315
SourceFile(ModuleDecl &M, SourceFileKind K, Optional<unsigned> bufferID,
309316
bool KeepParsedTokens = false, bool KeepSyntaxTree = false,
310-
ParsingOptions parsingOpts = {});
317+
ParsingOptions parsingOpts = {}, bool isPrimary = false);
311318

312319
~SourceFile();
313320

include/swift/AST/TypeCheckRequests.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,26 @@ class CheckInconsistentImplementationOnlyImportsRequest
24592459
bool isCached() const { return true; }
24602460
};
24612461

2462+
/// Retrieves the primary source files in the main module.
2463+
// FIXME: This isn't really a type-checking request, if we ever split off a
2464+
// zone for more basic AST requests, this should be moved there.
2465+
class PrimarySourceFilesRequest
2466+
: public SimpleRequest<PrimarySourceFilesRequest,
2467+
ArrayRef<SourceFile *>(ModuleDecl *),
2468+
RequestFlags::Cached> {
2469+
public:
2470+
using SimpleRequest::SimpleRequest;
2471+
2472+
private:
2473+
friend SimpleRequest;
2474+
2475+
ArrayRef<SourceFile *> evaluate(Evaluator &evaluator, ModuleDecl *mod) const;
2476+
2477+
public:
2478+
// Cached.
2479+
bool isCached() const { return true; }
2480+
};
2481+
24622482
// Allow AnyValue to compare two Type values, even though Type doesn't
24632483
// support ==.
24642484
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ SWIFT_REQUEST(TypeChecker, OverriddenDeclsRequest,
151151
SWIFT_REQUEST(TypeChecker, PatternBindingEntryRequest,
152152
const PatternBindingEntry *(PatternBindingDecl *, unsigned),
153153
SeparatelyCached, NoLocationInfo)
154+
SWIFT_REQUEST(TypeChecker, PrimarySourceFilesRequest,
155+
ArrayRef<SourceFile *>(ModuleDecl *), Cached, NoLocationInfo)
154156
SWIFT_REQUEST(TypeChecker, PropertyWrapperBackingPropertyInfoRequest,
155157
PropertyWrapperBackingPropertyInfo(VarDecl *), Cached,
156158
NoLocationInfo)

include/swift/Frontend/Frontend.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,6 @@ class CompilerInstance {
451451
/// considered primaries.
452452
llvm::SetVector<unsigned> PrimaryBufferIDs;
453453

454-
/// Identifies the set of SourceFiles that are considered primaries. An
455-
/// invariant is that any SourceFile in this set with an associated
456-
/// buffer will also have its buffer ID in PrimaryBufferIDs.
457-
std::vector<SourceFile *> PrimarySourceFiles;
458-
459454
/// The file that has been registered for code completion.
460455
NullablePtr<SourceFile> CodeCompletionFile;
461456

@@ -536,7 +531,7 @@ class CompilerInstance {
536531
/// Gets the set of SourceFiles which are the primary inputs for this
537532
/// CompilerInstance.
538533
ArrayRef<SourceFile *> getPrimarySourceFiles() const {
539-
return PrimarySourceFiles;
534+
return getMainModule()->getPrimarySourceFiles();
540535
}
541536

542537
/// Gets the SourceFile which is the primary input for this CompilerInstance.
@@ -546,11 +541,12 @@ class CompilerInstance {
546541
/// FIXME: This should be removed eventually, once there are no longer any
547542
/// codepaths that rely on a single primary file.
548543
SourceFile *getPrimarySourceFile() const {
549-
if (PrimarySourceFiles.empty()) {
544+
auto primaries = getPrimarySourceFiles();
545+
if (primaries.empty()) {
550546
return nullptr;
551547
} else {
552-
assert(PrimarySourceFiles.size() == 1);
553-
return *PrimarySourceFiles.begin();
548+
assert(primaries.size() == 1);
549+
return *primaries.begin();
554550
}
555551
}
556552

lib/AST/Module.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,27 @@ void ModuleDecl::addFile(FileUnit &newFile) {
511511
clearLookupCache();
512512
}
513513

514+
ArrayRef<SourceFile *>
515+
PrimarySourceFilesRequest::evaluate(Evaluator &evaluator,
516+
ModuleDecl *mod) const {
517+
assert(mod->isMainModule() && "Only the main module can have primaries");
518+
519+
SmallVector<SourceFile *, 8> primaries;
520+
for (auto *file : mod->getFiles()) {
521+
if (auto *SF = dyn_cast<SourceFile>(file)) {
522+
if (SF->isPrimary())
523+
primaries.push_back(SF);
524+
}
525+
}
526+
return mod->getASTContext().AllocateCopy(primaries);
527+
}
528+
529+
ArrayRef<SourceFile *> ModuleDecl::getPrimarySourceFiles() const {
530+
auto &eval = getASTContext().evaluator;
531+
auto *mutableThis = const_cast<ModuleDecl *>(this);
532+
return evaluateOrDefault(eval, PrimarySourceFilesRequest{mutableThis}, {});
533+
}
534+
514535
#define FORWARD(name, args) \
515536
for (const FileUnit *file : getFiles()) \
516537
file->name args;
@@ -2182,12 +2203,15 @@ ModuleDecl::computeMagicFileStringMap(bool shouldDiagnose) const {
21822203
SourceFile::SourceFile(ModuleDecl &M, SourceFileKind K,
21832204
Optional<unsigned> bufferID,
21842205
bool KeepParsedTokens, bool BuildSyntaxTree,
2185-
ParsingOptions parsingOpts)
2206+
ParsingOptions parsingOpts, bool isPrimary)
21862207
: FileUnit(FileUnitKind::Source, M), BufferID(bufferID ? *bufferID : -1),
2187-
ParsingOpts(parsingOpts), Kind(K),
2208+
ParsingOpts(parsingOpts), IsPrimary(isPrimary), Kind(K),
21882209
SyntaxInfo(new SourceFileSyntaxInfo(BuildSyntaxTree)) {
21892210
M.getASTContext().addDestructorCleanup(*this);
21902211

2212+
assert(!IsPrimary || M.isMainModule() &&
2213+
"A primary cannot appear outside the main module");
2214+
21912215
if (isScriptMode()) {
21922216
bool problem = M.registerEntryPointFile(this, SourceLoc(), None);
21932217
assert(!problem && "multiple main files?");

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ void CompilerInstance::forEachFileToTypeCheck(
870870
fn(*SF);
871871
}
872872
} else {
873-
for (auto *SF : PrimarySourceFiles) {
873+
for (auto *SF : getPrimarySourceFiles()) {
874874
fn(*SF);
875875
}
876876
}
@@ -900,11 +900,10 @@ SourceFile *CompilerInstance::createSourceFileForMainModule(
900900
SourceFile *inputFile = new (*Context)
901901
SourceFile(*mainModule, fileKind, bufferID,
902902
Invocation.getLangOptions().CollectParsedToken,
903-
Invocation.getLangOptions().BuildSyntaxTree, opts);
903+
Invocation.getLangOptions().BuildSyntaxTree, opts, isPrimary);
904904
MainModule->addFile(*inputFile);
905905

906906
if (isPrimary) {
907-
PrimarySourceFiles.push_back(inputFile);
908907
inputFile->enableInterfaceHash();
909908
}
910909

@@ -923,7 +922,6 @@ void CompilerInstance::freeASTContext() {
923922
SML = nullptr;
924923
MemoryBufferLoader = nullptr;
925924
PrimaryBufferIDs.clear();
926-
PrimarySourceFiles.clear();
927925
}
928926

929927
/// Perform "stable" optimizations that are invariant across compiler versions.

0 commit comments

Comments
 (0)