@@ -774,6 +774,7 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
774774 assert (!InputSourceCodeBufferIDs.empty ());
775775 assert (InputSourceCodeBufferIDs.size () == 1 );
776776 assert (MainBufferID != NO_SUCH_BUFFER);
777+ assert (isPrimaryInput (MainBufferID) || isWholeModuleCompilation ());
777778 createSILModule ();
778779 }
779780
@@ -829,7 +830,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
829830 SourceFile::ASTStage_t limitStage) {
830831 FrontendStatsTracer tracer (getStatsReporter (), " parse-and-check-types" );
831832
832- bool hadLoadError = parsePartialModulesAndLibraryFiles ();
833+ bool hadLoadError = parsePartialModulesAndInputFiles ();
833834 if (Invocation.isCodeCompletion ()) {
834835 // When we are doing code completion, make sure to emit at least one
835836 // diagnostic, so that ASTContext is marked as erroneous. In this case
@@ -840,14 +841,6 @@ void CompilerInstance::parseAndCheckTypesUpTo(
840841 if (hadLoadError)
841842 return ;
842843
843- // Type-check main file after parsing all other files so that
844- // it can use declarations from other files.
845- // In addition, in SIL mode the main file has parsing and
846- // type-checking interwined.
847- if (MainBufferID != NO_SUCH_BUFFER) {
848- parseAndTypeCheckMainFileUpTo (limitStage);
849- }
850-
851844 assert (llvm::all_of (MainModule->getFiles (), [](const FileUnit *File) -> bool {
852845 auto *SF = dyn_cast<SourceFile>(File);
853846 if (!SF)
@@ -864,17 +857,24 @@ void CompilerInstance::parseAndCheckTypesUpTo(
864857
865858 performTypeChecking (SF);
866859
867- if (!Context->hadError () && Invocation.getFrontendOptions ().PCMacro ) {
868- performPCMacro (SF);
860+ // Parse the SIL decls if needed.
861+ // TODO: Requestify SIL parsing.
862+ if (TheSILModule) {
863+ SILParserState SILContext (TheSILModule.get ());
864+ parseSourceFileSIL (SF, &SILContext);
869865 }
870866
867+ auto &opts = Invocation.getFrontendOptions ();
868+ if (!Context->hadError () && opts.DebuggerTestingTransform )
869+ performDebuggerTestingTransform (SF);
870+
871+ if (!Context->hadError () && opts.PCMacro )
872+ performPCMacro (SF);
873+
871874 // Playground transform knows to look out for PCMacro's changes and not
872875 // to playground log them.
873- if (!Context->hadError () &&
874- Invocation.getFrontendOptions ().PlaygroundTransform ) {
875- performPlaygroundTransform (
876- SF, Invocation.getFrontendOptions ().PlaygroundHighPerformance );
877- }
876+ if (!Context->hadError () && opts.PlaygroundTransform )
877+ performPlaygroundTransform (SF, opts.PlaygroundHighPerformance );
878878 });
879879
880880 // If the limiting AST stage is import resolution, we're done.
@@ -885,19 +885,9 @@ void CompilerInstance::parseAndCheckTypesUpTo(
885885 finishTypeChecking ();
886886}
887887
888- void CompilerInstance::parseLibraryFile (unsigned BufferID) {
889- FrontendStatsTracer tracer (getStatsReporter (), " parse-library-file" );
890-
891- auto *NextInput =
892- createSourceFileForMainModule (SourceFileKind::Library, BufferID);
893-
894- // Import resolution will lazily trigger parsing of the file.
895- performImportResolution (*NextInput);
896- }
897-
898- bool CompilerInstance::parsePartialModulesAndLibraryFiles () {
888+ bool CompilerInstance::parsePartialModulesAndInputFiles () {
899889 FrontendStatsTracer tracer (getStatsReporter (),
900- " parse-partial-modules-and-library -files" );
890+ " parse-partial-modules-and-input -files" );
901891 bool hadLoadError = false ;
902892 // Parse all the partial modules first.
903893 for (auto &PM : PartialModules) {
@@ -909,47 +899,22 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles() {
909899 hadLoadError = true ;
910900 }
911901
912- // Then parse all the library files.
902+ // Then parse all the input files.
913903 for (auto BufferID : InputSourceCodeBufferIDs) {
914- if (BufferID != MainBufferID) {
915- parseLibraryFile (BufferID);
904+ SourceFile *SF;
905+ if (BufferID == MainBufferID) {
906+ // If this is the main file, we've already created it.
907+ SF = &getMainModule ()->getMainSourceFile (Invocation.getSourceFileKind ());
908+ } else {
909+ // Otherwise create a library file.
910+ SF = createSourceFileForMainModule (SourceFileKind::Library, BufferID);
916911 }
912+ // Import resolution will lazily trigger parsing of the file.
913+ performImportResolution (*SF);
917914 }
918915 return hadLoadError;
919916}
920917
921- void CompilerInstance::parseAndTypeCheckMainFileUpTo (
922- SourceFile::ASTStage_t LimitStage) {
923- assert (LimitStage >= SourceFile::ImportsResolved);
924- FrontendStatsTracer tracer (getStatsReporter (),
925- " parse-and-typecheck-main-file" );
926- bool mainIsPrimary =
927- (isWholeModuleCompilation () || isPrimaryInput (MainBufferID));
928-
929- SourceFile &MainFile =
930- MainModule->getMainSourceFile (Invocation.getSourceFileKind ());
931-
932- // For a primary, perform type checking if needed. Otherwise, just do import
933- // resolution.
934- if (mainIsPrimary && LimitStage >= SourceFile::TypeChecked) {
935- performTypeChecking (MainFile);
936- } else {
937- assert (!TheSILModule && " Should perform type checking for SIL" );
938- performImportResolution (MainFile);
939- }
940-
941- // Parse the SIL decls if needed.
942- if (TheSILModule) {
943- SILParserState SILContext (TheSILModule.get ());
944- parseSourceFileSIL (MainFile, &SILContext);
945- }
946-
947- if (mainIsPrimary && !Context->hadError () &&
948- Invocation.getFrontendOptions ().DebuggerTestingTransform ) {
949- performDebuggerTestingTransform (MainFile);
950- }
951- }
952-
953918static void
954919forEachSourceFileIn (ModuleDecl *module ,
955920 llvm::function_ref<void (SourceFile &)> fn) {
@@ -1030,35 +995,28 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
1030995 if (!CanDelayBodies)
1031996 parsingOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
1032997
1033- // Make sure the main file is the first file in the module but parse it last,
1034- // to match the parsing logic used when performing Sema.
998+ // Make sure the main file is the first file in the module.
1035999 if (MainBufferID != NO_SUCH_BUFFER) {
10361000 assert (Kind == InputFileKind::Swift ||
10371001 Kind == InputFileKind::SwiftModuleInterface);
1038- createSourceFileForMainModule (Invocation.getSourceFileKind (),
1039- MainBufferID, parsingOpts);
1040- }
1041-
1042- // Parse all the library files.
1043- for (auto BufferID : InputSourceCodeBufferIDs) {
1044- if (BufferID == MainBufferID)
1045- continue ;
1046-
1047- SourceFile *NextInput = createSourceFileForMainModule (
1048- SourceFileKind::Library, BufferID, parsingOpts);
1049-
1050- // Force the parsing of the top level decls.
1051- (void )NextInput->getTopLevelDecls ();
1002+ auto *mainFile = createSourceFileForMainModule (
1003+ Invocation.getSourceFileKind (), MainBufferID, parsingOpts);
1004+ mainFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
10521005 }
10531006
1054- // Now parse the main file.
1055- if (MainBufferID != NO_SUCH_BUFFER) {
1056- SourceFile &MainFile =
1057- MainModule->getMainSourceFile (Invocation.getSourceFileKind ());
1058- MainFile.SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache ();
1059-
1007+ // Parse all of the input files.
1008+ for (auto bufferID : InputSourceCodeBufferIDs) {
1009+ SourceFile *SF;
1010+ if (bufferID == MainBufferID) {
1011+ // If this is the main file, we've already created it.
1012+ SF = &MainModule->getMainSourceFile (Invocation.getSourceFileKind ());
1013+ } else {
1014+ // Otherwise create a library file.
1015+ SF = createSourceFileForMainModule (SourceFileKind::Library, bufferID,
1016+ parsingOpts);
1017+ }
10601018 // Force the parsing of the top level decls.
1061- (void )MainFile. getTopLevelDecls ();
1019+ (void )SF-> getTopLevelDecls ();
10621020 }
10631021
10641022 assert (Context->LoadedModules .size () == 1 &&
0 commit comments