@@ -705,6 +705,37 @@ ImplicitImportInfo CompilerInstance::getImplicitImportInfo() const {
705
705
return imports;
706
706
}
707
707
708
+ bool CompilerInstance::createFilesForMainModule (
709
+ ModuleDecl *mod, SmallVectorImpl<FileUnit *> &files) const {
710
+ // Make sure the main file is the first file in the module.
711
+ if (MainBufferID != NO_SUCH_BUFFER) {
712
+ auto *mainFile = createSourceFileForMainModule (
713
+ mod, Invocation.getSourceFileKind (), MainBufferID);
714
+ files.push_back (mainFile);
715
+ }
716
+
717
+ // If we have partial modules to load, do so now, bailing if any failed to
718
+ // load.
719
+ if (!PartialModules.empty ()) {
720
+ if (loadPartialModulesAndImplicitImports (mod, files))
721
+ return true ;
722
+ }
723
+
724
+ // Finally add the library files.
725
+ // FIXME: This is the only demand point for InputSourceCodeBufferIDs. We
726
+ // should compute this list of source files lazily.
727
+ for (auto BufferID : InputSourceCodeBufferIDs) {
728
+ // Skip the main buffer, we've already handled it.
729
+ if (BufferID == MainBufferID)
730
+ continue ;
731
+
732
+ auto *libraryFile =
733
+ createSourceFileForMainModule (mod, SourceFileKind::Library, BufferID);
734
+ files.push_back (libraryFile);
735
+ }
736
+ return false ;
737
+ }
738
+
708
739
ModuleDecl *CompilerInstance::getMainModule () const {
709
740
if (!MainModule) {
710
741
Identifier ID = Context->getIdentifier (Invocation.getModuleName ());
@@ -719,6 +750,22 @@ ModuleDecl *CompilerInstance::getMainModule() const {
719
750
720
751
if (Invocation.getFrontendOptions ().EnableLibraryEvolution )
721
752
MainModule->setResilienceStrategy (ResilienceStrategy::Resilient);
753
+
754
+ Context->LoadedModules [MainModule->getName ()] = MainModule;
755
+
756
+ // Create and add the module's files.
757
+ SmallVector<FileUnit *, 16 > files;
758
+ if (!createFilesForMainModule (MainModule, files)) {
759
+ for (auto *file : files)
760
+ MainModule->addFile (*file);
761
+ } else {
762
+ // If we failed to load a partial module, mark the main module as having
763
+ // "failed to load", as it will contain no files. Note that we don't try
764
+ // to add any of the successfully loaded partial modules. This ensures
765
+ // that we don't encounter cases where we try to resolve a cross-reference
766
+ // into a partial module that failed to load.
767
+ MainModule->setFailedToLoad ();
768
+ }
722
769
}
723
770
return MainModule;
724
771
}
@@ -753,37 +800,13 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
753
800
FrontendStatsTracer tracer (getStatsReporter (), " perform-sema" );
754
801
755
802
ModuleDecl *mainModule = getMainModule ();
756
- Context->LoadedModules [mainModule->getName ()] = mainModule;
757
-
758
- // Make sure the main file is the first file in the module, so do this now.
759
- if (MainBufferID != NO_SUCH_BUFFER) {
760
- auto *mainFile = createSourceFileForMainModule (
761
- Invocation.getSourceFileKind (), MainBufferID);
762
- mainFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
763
- }
764
-
765
- // If we aren't in a parse-only context, load the remaining serialized inputs
766
- // and resolve implicit imports.
767
- if (LimitStage > SourceFile::Unprocessed &&
768
- loadPartialModulesAndImplicitImports ()) {
769
- // If we failed to load a partial module, mark the main module as having
770
- // "failed to load", as it may contain no files.
771
- mainModule->setFailedToLoad ();
772
- return ;
773
- }
774
803
775
804
// Then parse all the input files.
776
- // FIXME: This is the only demand point for InputSourceCodeBufferIDs. We
777
- // should compute this list of source files lazily.
778
- for (auto BufferID : InputSourceCodeBufferIDs) {
779
- SourceFile *SF;
780
- if (BufferID == MainBufferID) {
781
- // If this is the main file, we've already created it.
782
- SF = &getMainModule ()->getMainSourceFile (Invocation.getSourceFileKind ());
783
- } else {
784
- // Otherwise create a library file.
785
- SF = createSourceFileForMainModule (SourceFileKind::Library, BufferID);
786
- }
805
+ for (auto *file : mainModule->getFiles ()) {
806
+ auto *SF = dyn_cast<SourceFile>(file);
807
+ if (!SF)
808
+ continue ;
809
+
787
810
// Trigger parsing of the file.
788
811
if (LimitStage == SourceFile::Unprocessed) {
789
812
(void )SF->getTopLevelDecls ();
@@ -795,15 +818,15 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
795
818
if (LimitStage == SourceFile::Unprocessed)
796
819
return ;
797
820
798
- assert (llvm::all_of (MainModule ->getFiles (), [](const FileUnit *File) -> bool {
821
+ assert (llvm::all_of (mainModule ->getFiles (), [](const FileUnit *File) -> bool {
799
822
auto *SF = dyn_cast<SourceFile>(File);
800
823
if (!SF)
801
824
return true ;
802
825
return SF->ASTStage >= SourceFile::ImportsResolved;
803
826
}) && " some files have not yet had their imports resolved" );
804
- MainModule ->setHasResolvedImports ();
827
+ mainModule ->setHasResolvedImports ();
805
828
806
- bindExtensions (*MainModule );
829
+ bindExtensions (*mainModule );
807
830
808
831
// If the limiting AST stage is import resolution, we're done.
809
832
if (LimitStage == SourceFile::ImportsResolved)
@@ -839,26 +862,27 @@ bool CompilerInstance::loadStdlibIfNeeded() {
839
862
return false ;
840
863
}
841
864
842
- bool CompilerInstance::loadPartialModulesAndImplicitImports () {
865
+ bool CompilerInstance::loadPartialModulesAndImplicitImports (
866
+ ModuleDecl *mod, SmallVectorImpl<FileUnit *> &partialModules) const {
843
867
FrontendStatsTracer tracer (getStatsReporter (),
844
868
" load-partial-modules-and-implicit-imports" );
845
869
// Force loading implicit imports. This is currently needed to allow
846
870
// deserialization to resolve cross references into bridging headers.
847
871
// FIXME: Once deserialization loads all the modules it needs for cross
848
872
// references, this can be removed.
849
- (void )MainModule ->getImplicitImports ();
873
+ (void )mod ->getImplicitImports ();
850
874
875
+ // Load in the partial modules.
851
876
bool hadLoadError = false ;
852
- // Parse all the partial modules first.
853
877
for (auto &PM : PartialModules) {
854
878
assert (PM.ModuleBuffer );
855
879
auto *file =
856
- SML->loadAST (*MainModule, SourceLoc (), /* moduleInterfacePath*/ " " ,
880
+ SML->loadAST (*mod, /* diagLoc */ SourceLoc (), /* moduleInterfacePath*/ " " ,
857
881
std::move (PM.ModuleBuffer ), std::move (PM.ModuleDocBuffer ),
858
882
std::move (PM.ModuleSourceInfoBuffer ),
859
883
/* isFramework*/ false );
860
884
if (file) {
861
- MainModule-> addFile (* file);
885
+ partialModules. push_back ( file);
862
886
} else {
863
887
hadLoadError = true ;
864
888
}
@@ -869,7 +893,7 @@ bool CompilerInstance::loadPartialModulesAndImplicitImports() {
869
893
void CompilerInstance::forEachFileToTypeCheck (
870
894
llvm::function_ref<void (SourceFile &)> fn) {
871
895
if (isWholeModuleCompilation ()) {
872
- for (auto fileName : MainModule ->getFiles ()) {
896
+ for (auto fileName : getMainModule () ->getFiles ()) {
873
897
auto *SF = dyn_cast<SourceFile>(fileName);
874
898
if (!SF) {
875
899
continue ;
@@ -928,15 +952,16 @@ CompilerInstance::getSourceFileParsingOptions(bool forPrimary) const {
928
952
}
929
953
930
954
SourceFile *CompilerInstance::createSourceFileForMainModule (
931
- SourceFileKind fileKind, Optional<unsigned > bufferID) {
932
- ModuleDecl *mainModule = getMainModule ();
933
-
955
+ ModuleDecl *mod, SourceFileKind fileKind,
956
+ Optional<unsigned > bufferID) const {
934
957
auto isPrimary = bufferID && isPrimaryInput (*bufferID);
935
958
auto opts = getSourceFileParsingOptions (isPrimary);
936
959
937
960
auto *inputFile = new (*Context)
938
- SourceFile (*mainModule, fileKind, bufferID, opts, isPrimary);
939
- MainModule->addFile (*inputFile);
961
+ SourceFile (*mod, fileKind, bufferID, opts, isPrimary);
962
+
963
+ if (bufferID == MainBufferID)
964
+ inputFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
940
965
941
966
return inputFile;
942
967
}
0 commit comments