@@ -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 {
21822203SourceFile::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?" );
0 commit comments