@@ -731,6 +731,24 @@ ModuleDecl *CompilerInstance::getMainModule() const {
731
731
return MainModule;
732
732
}
733
733
734
+ void CompilerInstance::performParseOnly (bool EvaluateConditionals,
735
+ bool CanDelayBodies) {
736
+ const InputFileKind Kind = Invocation.getInputKind ();
737
+ assert ((Kind == InputFileKind::Swift || Kind == InputFileKind::SwiftLibrary ||
738
+ Kind == InputFileKind::SwiftModuleInterface) &&
739
+ " only supports parsing .swift files" );
740
+ (void )Kind;
741
+
742
+ SourceFile::ParsingOptions parsingOpts;
743
+ if (!EvaluateConditionals)
744
+ parsingOpts |= SourceFile::ParsingFlags::DisablePoundIfEvaluation;
745
+ if (!CanDelayBodies)
746
+ parsingOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
747
+ performSemaUpTo (SourceFile::Unprocessed, parsingOpts);
748
+ assert (Context->LoadedModules .size () == 1 &&
749
+ " Loaded a module during parse-only" );
750
+ }
751
+
734
752
void CompilerInstance::performParseAndResolveImportsOnly () {
735
753
performSemaUpTo (SourceFile::ImportsResolved);
736
754
}
@@ -739,33 +757,55 @@ void CompilerInstance::performSema() {
739
757
performSemaUpTo (SourceFile::TypeChecked);
740
758
}
741
759
742
- void CompilerInstance::performSemaUpTo (SourceFile::ASTStage_t LimitStage) {
743
- assert (LimitStage > SourceFile::Unprocessed);
744
-
760
+ void CompilerInstance::performSemaUpTo (SourceFile::ASTStage_t LimitStage,
761
+ SourceFile::ParsingOptions POpts) {
745
762
FrontendStatsTracer tracer (getStatsReporter (), " perform-sema" );
746
763
747
764
ModuleDecl *mainModule = getMainModule ();
748
765
Context->LoadedModules [mainModule->getName ()] = mainModule;
749
766
750
- if (Invocation.getImplicitStdlibKind () == ImplicitStdlibKind::Stdlib) {
751
- if (!loadStdlib ())
752
- return ;
767
+ // If we aren't in a parse-only context, load the standard library.
768
+ if (LimitStage > SourceFile::Unprocessed &&
769
+ Invocation.getImplicitStdlibKind () == ImplicitStdlibKind::Stdlib
770
+ && !loadStdlib ()) {
771
+ return ;
753
772
}
754
773
755
- // Force loading implicit imports. This is currently needed to allow
756
- // deserialization to resolve cross references into bridging headers.
757
- // FIXME: Once deserialization loads all the modules it needs for cross
758
- // references, this can be removed.
759
- (void )MainModule->getImplicitImports ();
760
-
761
774
// Make sure the main file is the first file in the module, so do this now.
762
775
if (MainBufferID != NO_SUCH_BUFFER) {
763
- (void )createSourceFileForMainModule (Invocation.getSourceFileKind (),
764
- MainBufferID);
776
+ auto *mainFile = createSourceFileForMainModule (
777
+ Invocation.getSourceFileKind (), MainBufferID, POpts);
778
+ mainFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
779
+ }
780
+
781
+ // If we aren't in a parse-only context, load the remaining serialized inputs
782
+ // and resolve implicit imports.
783
+ if (LimitStage > SourceFile::Unprocessed &&
784
+ loadPartialModulesAndImplicitImports ())
785
+ return ;
786
+
787
+ // Then parse all the input files.
788
+ // FIXME: This is the only demand point for InputSourceCodeBufferIDs. We
789
+ // should compute this list of source files lazily.
790
+ for (auto BufferID : InputSourceCodeBufferIDs) {
791
+ SourceFile *SF;
792
+ if (BufferID == MainBufferID) {
793
+ // If this is the main file, we've already created it.
794
+ SF = &getMainModule ()->getMainSourceFile (Invocation.getSourceFileKind ());
795
+ } else {
796
+ // Otherwise create a library file.
797
+ SF = createSourceFileForMainModule (SourceFileKind::Library,
798
+ BufferID, POpts);
799
+ }
800
+ // Trigger parsing of the file.
801
+ if (LimitStage == SourceFile::Unprocessed) {
802
+ (void )SF->getTopLevelDecls ();
803
+ } else {
804
+ performImportResolution (*SF);
805
+ }
765
806
}
766
807
767
- bool hadLoadError = parsePartialModulesAndInputFiles ();
768
- if (hadLoadError)
808
+ if (LimitStage == SourceFile::Unprocessed)
769
809
return ;
770
810
771
811
assert (llvm::all_of (MainModule->getFiles (), [](const FileUnit *File) -> bool {
@@ -808,9 +848,15 @@ bool CompilerInstance::loadStdlib() {
808
848
return true ;
809
849
}
810
850
811
- bool CompilerInstance::parsePartialModulesAndInputFiles () {
851
+ bool CompilerInstance::loadPartialModulesAndImplicitImports () {
812
852
FrontendStatsTracer tracer (getStatsReporter (),
813
- " parse-partial-modules-and-input-files" );
853
+ " load-partial-modules-and-implicit-imports" );
854
+ // Force loading implicit imports. This is currently needed to allow
855
+ // deserialization to resolve cross references into bridging headers.
856
+ // FIXME: Once deserialization loads all the modules it needs for cross
857
+ // references, this can be removed.
858
+ (void )MainModule->getImplicitImports ();
859
+
814
860
bool hadLoadError = false ;
815
861
// Parse all the partial modules first.
816
862
for (auto &PM : PartialModules) {
@@ -821,20 +867,6 @@ bool CompilerInstance::parsePartialModulesAndInputFiles() {
821
867
/* treatAsPartialModule*/ true ))
822
868
hadLoadError = true ;
823
869
}
824
-
825
- // Then parse all the input files.
826
- for (auto BufferID : InputSourceCodeBufferIDs) {
827
- SourceFile *SF;
828
- if (BufferID == MainBufferID) {
829
- // If this is the main file, we've already created it.
830
- SF = &getMainModule ()->getMainSourceFile (Invocation.getSourceFileKind ());
831
- } else {
832
- // Otherwise create a library file.
833
- SF = createSourceFileForMainModule (SourceFileKind::Library, BufferID);
834
- }
835
- // Import resolution will lazily trigger parsing of the file.
836
- performImportResolution (*SF);
837
- }
838
870
return hadLoadError;
839
871
}
840
872
@@ -903,52 +935,6 @@ SourceFile *CompilerInstance::createSourceFileForMainModule(
903
935
return inputFile;
904
936
}
905
937
906
- void CompilerInstance::performParseOnly (bool EvaluateConditionals,
907
- bool CanDelayBodies) {
908
- const InputFileKind Kind = Invocation.getInputKind ();
909
- ModuleDecl *const MainModule = getMainModule ();
910
- Context->LoadedModules [MainModule->getName ()] = MainModule;
911
-
912
- assert ((Kind == InputFileKind::Swift ||
913
- Kind == InputFileKind::SwiftLibrary ||
914
- Kind == InputFileKind::SwiftModuleInterface) &&
915
- " only supports parsing .swift files" );
916
- (void )Kind;
917
-
918
- SourceFile::ParsingOptions parsingOpts;
919
- if (!EvaluateConditionals)
920
- parsingOpts |= SourceFile::ParsingFlags::DisablePoundIfEvaluation;
921
- if (!CanDelayBodies)
922
- parsingOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
923
-
924
- // Make sure the main file is the first file in the module.
925
- if (MainBufferID != NO_SUCH_BUFFER) {
926
- assert (Kind == InputFileKind::Swift ||
927
- Kind == InputFileKind::SwiftModuleInterface);
928
- auto *mainFile = createSourceFileForMainModule (
929
- Invocation.getSourceFileKind (), MainBufferID, parsingOpts);
930
- mainFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
931
- }
932
-
933
- // Parse all of the input files.
934
- for (auto bufferID : InputSourceCodeBufferIDs) {
935
- SourceFile *SF;
936
- if (bufferID == MainBufferID) {
937
- // If this is the main file, we've already created it.
938
- SF = &MainModule->getMainSourceFile (Invocation.getSourceFileKind ());
939
- } else {
940
- // Otherwise create a library file.
941
- SF = createSourceFileForMainModule (SourceFileKind::Library, bufferID,
942
- parsingOpts);
943
- }
944
- // Force the parsing of the top level decls.
945
- (void )SF->getTopLevelDecls ();
946
- }
947
-
948
- assert (Context->LoadedModules .size () == 1 &&
949
- " Loaded a module during parse-only" );
950
- }
951
-
952
938
void CompilerInstance::freeASTContext () {
953
939
TheSILTypes.reset ();
954
940
Context.reset ();
0 commit comments