Skip to content

Commit c292b11

Browse files
authored
[Frontend] Remove parseAndTypeCheckMainFileUpTo (#31168)
[Frontend] Remove parseAndTypeCheckMainFileUpTo
2 parents b7df804 + 057ca60 commit c292b11

File tree

3 files changed

+61
-112
lines changed

3 files changed

+61
-112
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,10 +466,6 @@ class CompilerInstance {
466466
/// If \p BufID is already in the set, do nothing.
467467
void recordPrimaryInputBuffer(unsigned BufID);
468468

469-
/// Record in PrimarySourceFiles the fact that \p SF is a primary, and
470-
/// call recordPrimaryInputBuffer on \p SF's buffer (if it exists).
471-
void recordPrimarySourceFile(SourceFile *SF);
472-
473469
bool isWholeModuleCompilation() { return PrimaryBufferIDs.empty(); }
474470

475471
public:
@@ -674,15 +670,11 @@ class CompilerInstance {
674670
void performSemaUpTo(SourceFile::ASTStage_t LimitStage);
675671
void parseAndCheckTypesUpTo(SourceFile::ASTStage_t LimitStage);
676672

677-
void parseLibraryFile(unsigned BufferID);
678-
679673
/// Return true if had load error
680-
bool parsePartialModulesAndLibraryFiles();
674+
bool parsePartialModulesAndInputFiles();
681675

682676
void forEachFileToTypeCheck(llvm::function_ref<void(SourceFile &)> fn);
683677

684-
void parseAndTypeCheckMainFileUpTo(SourceFile::ASTStage_t LimitStage);
685-
686678
void finishTypeChecking();
687679

688680
public:

lib/Frontend/Frontend.cpp

Lines changed: 49 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -198,15 +198,6 @@ void CompilerInstance::recordPrimaryInputBuffer(unsigned BufID) {
198198
PrimaryBufferIDs.insert(BufID);
199199
}
200200

201-
void CompilerInstance::recordPrimarySourceFile(SourceFile *SF) {
202-
assert(MainModule && "main module not created yet");
203-
PrimarySourceFiles.push_back(SF);
204-
SF->enableInterfaceHash();
205-
SF->createReferencedNameTracker();
206-
if (SF->getBufferID().hasValue())
207-
recordPrimaryInputBuffer(SF->getBufferID().getValue());
208-
}
209-
210201
bool CompilerInstance::setUpASTContextIfNeeded() {
211202
if (Invocation.getFrontendOptions().RequestedAction ==
212203
FrontendOptions::ActionType::CompileModuleFromInterface) {
@@ -774,6 +765,7 @@ void CompilerInstance::performSemaUpTo(SourceFile::ASTStage_t LimitStage) {
774765
assert(!InputSourceCodeBufferIDs.empty());
775766
assert(InputSourceCodeBufferIDs.size() == 1);
776767
assert(MainBufferID != NO_SUCH_BUFFER);
768+
assert(isPrimaryInput(MainBufferID) || isWholeModuleCompilation());
777769
createSILModule();
778770
}
779771

@@ -829,7 +821,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
829821
SourceFile::ASTStage_t limitStage) {
830822
FrontendStatsTracer tracer(getStatsReporter(), "parse-and-check-types");
831823

832-
bool hadLoadError = parsePartialModulesAndLibraryFiles();
824+
bool hadLoadError = parsePartialModulesAndInputFiles();
833825
if (Invocation.isCodeCompletion()) {
834826
// When we are doing code completion, make sure to emit at least one
835827
// diagnostic, so that ASTContext is marked as erroneous. In this case
@@ -840,14 +832,6 @@ void CompilerInstance::parseAndCheckTypesUpTo(
840832
if (hadLoadError)
841833
return;
842834

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-
851835
assert(llvm::all_of(MainModule->getFiles(), [](const FileUnit *File) -> bool {
852836
auto *SF = dyn_cast<SourceFile>(File);
853837
if (!SF)
@@ -864,17 +848,24 @@ void CompilerInstance::parseAndCheckTypesUpTo(
864848

865849
performTypeChecking(SF);
866850

867-
if (!Context->hadError() && Invocation.getFrontendOptions().PCMacro) {
868-
performPCMacro(SF);
851+
// Parse the SIL decls if needed.
852+
// TODO: Requestify SIL parsing.
853+
if (TheSILModule) {
854+
SILParserState SILContext(TheSILModule.get());
855+
parseSourceFileSIL(SF, &SILContext);
869856
}
870857

858+
auto &opts = Invocation.getFrontendOptions();
859+
if (!Context->hadError() && opts.DebuggerTestingTransform)
860+
performDebuggerTestingTransform(SF);
861+
862+
if (!Context->hadError() && opts.PCMacro)
863+
performPCMacro(SF);
864+
871865
// Playground transform knows to look out for PCMacro's changes and not
872866
// to playground log them.
873-
if (!Context->hadError() &&
874-
Invocation.getFrontendOptions().PlaygroundTransform) {
875-
performPlaygroundTransform(
876-
SF, Invocation.getFrontendOptions().PlaygroundHighPerformance);
877-
}
867+
if (!Context->hadError() && opts.PlaygroundTransform)
868+
performPlaygroundTransform(SF, opts.PlaygroundHighPerformance);
878869
});
879870

880871
// If the limiting AST stage is import resolution, we're done.
@@ -885,19 +876,9 @@ void CompilerInstance::parseAndCheckTypesUpTo(
885876
finishTypeChecking();
886877
}
887878

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() {
879+
bool CompilerInstance::parsePartialModulesAndInputFiles() {
899880
FrontendStatsTracer tracer(getStatsReporter(),
900-
"parse-partial-modules-and-library-files");
881+
"parse-partial-modules-and-input-files");
901882
bool hadLoadError = false;
902883
// Parse all the partial modules first.
903884
for (auto &PM : PartialModules) {
@@ -909,53 +890,22 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles() {
909890
hadLoadError = true;
910891
}
911892

912-
// Then parse all the library files.
893+
// Then parse all the input files.
913894
for (auto BufferID : InputSourceCodeBufferIDs) {
914-
if (BufferID != MainBufferID) {
915-
parseLibraryFile(BufferID);
895+
SourceFile *SF;
896+
if (BufferID == MainBufferID) {
897+
// If this is the main file, we've already created it.
898+
SF = &getMainModule()->getMainSourceFile(Invocation.getSourceFileKind());
899+
} else {
900+
// Otherwise create a library file.
901+
SF = createSourceFileForMainModule(SourceFileKind::Library, BufferID);
916902
}
903+
// Import resolution will lazily trigger parsing of the file.
904+
performImportResolution(*SF);
917905
}
918906
return hadLoadError;
919907
}
920908

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-
auto &Diags = MainFile.getASTContext().Diags;
933-
auto DidSuppressWarnings = Diags.getSuppressWarnings();
934-
Diags.setSuppressWarnings(DidSuppressWarnings || !mainIsPrimary);
935-
936-
// For a primary, perform type checking if needed. Otherwise, just do import
937-
// resolution.
938-
if (mainIsPrimary && LimitStage >= SourceFile::TypeChecked) {
939-
performTypeChecking(MainFile);
940-
} else {
941-
assert(!TheSILModule && "Should perform type checking for SIL");
942-
performImportResolution(MainFile);
943-
}
944-
945-
// Parse the SIL decls if needed.
946-
if (TheSILModule) {
947-
SILParserState SILContext(TheSILModule.get());
948-
parseSourceFileSIL(MainFile, &SILContext);
949-
}
950-
951-
Diags.setSuppressWarnings(DidSuppressWarnings);
952-
953-
if (mainIsPrimary && !Context->hadError() &&
954-
Invocation.getFrontendOptions().DebuggerTestingTransform) {
955-
performDebuggerTestingTransform(MainFile);
956-
}
957-
}
958-
959909
static void
960910
forEachSourceFileIn(ModuleDecl *module,
961911
llvm::function_ref<void(SourceFile &)> fn) {
@@ -1007,8 +957,11 @@ SourceFile *CompilerInstance::createSourceFileForMainModule(
1007957
Invocation.getLangOptions().BuildSyntaxTree, opts);
1008958
MainModule->addFile(*inputFile);
1009959

1010-
if (isPrimary)
1011-
recordPrimarySourceFile(inputFile);
960+
if (isPrimary) {
961+
PrimarySourceFiles.push_back(inputFile);
962+
inputFile->enableInterfaceHash();
963+
inputFile->createReferencedNameTracker();
964+
}
1012965

1013966
if (bufferID == SourceMgr.getCodeCompletionBufferID()) {
1014967
assert(!CodeCompletionFile && "Multiple code completion files?");
@@ -1036,35 +989,28 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
1036989
if (!CanDelayBodies)
1037990
parsingOpts |= SourceFile::ParsingFlags::DisableDelayedBodies;
1038991

1039-
// Make sure the main file is the first file in the module but parse it last,
1040-
// to match the parsing logic used when performing Sema.
992+
// Make sure the main file is the first file in the module.
1041993
if (MainBufferID != NO_SUCH_BUFFER) {
1042994
assert(Kind == InputFileKind::Swift ||
1043995
Kind == InputFileKind::SwiftModuleInterface);
1044-
createSourceFileForMainModule(Invocation.getSourceFileKind(),
1045-
MainBufferID, parsingOpts);
996+
auto *mainFile = createSourceFileForMainModule(
997+
Invocation.getSourceFileKind(), MainBufferID, parsingOpts);
998+
mainFile->SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache();
1046999
}
10471000

1048-
// Parse all the library files.
1049-
for (auto BufferID : InputSourceCodeBufferIDs) {
1050-
if (BufferID == MainBufferID)
1051-
continue;
1052-
1053-
SourceFile *NextInput = createSourceFileForMainModule(
1054-
SourceFileKind::Library, BufferID, parsingOpts);
1055-
1056-
// Force the parsing of the top level decls.
1057-
(void)NextInput->getTopLevelDecls();
1058-
}
1059-
1060-
// Now parse the main file.
1061-
if (MainBufferID != NO_SUCH_BUFFER) {
1062-
SourceFile &MainFile =
1063-
MainModule->getMainSourceFile(Invocation.getSourceFileKind());
1064-
MainFile.SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache();
1065-
1001+
// Parse all of the input files.
1002+
for (auto bufferID : InputSourceCodeBufferIDs) {
1003+
SourceFile *SF;
1004+
if (bufferID == MainBufferID) {
1005+
// If this is the main file, we've already created it.
1006+
SF = &MainModule->getMainSourceFile(Invocation.getSourceFileKind());
1007+
} else {
1008+
// Otherwise create a library file.
1009+
SF = createSourceFileForMainModule(SourceFileKind::Library, bufferID,
1010+
parsingOpts);
1011+
}
10661012
// Force the parsing of the top level decls.
1067-
(void)MainFile.getTopLevelDecls();
1013+
(void)SF->getTopLevelDecls();
10681014
}
10691015

10701016
assert(Context->LoadedModules.size() == 1 &&

lib/Sema/ImportResolution.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,17 @@ void swift::performImportResolution(SourceFile &SF) {
282282

283283
FrontendStatsTracer tracer(SF.getASTContext().Stats,
284284
"Import resolution");
285+
286+
// If we're silencing parsing warnings, then also silence import warnings.
287+
// This is necessary for secondary files as they can be parsed and have their
288+
// imports resolved multiple times.
289+
auto &diags = SF.getASTContext().Diags;
290+
auto didSuppressWarnings = diags.getSuppressWarnings();
291+
auto shouldSuppress = SF.getParsingOptions().contains(
292+
SourceFile::ParsingFlags::SuppressWarnings);
293+
diags.setSuppressWarnings(didSuppressWarnings || shouldSuppress);
294+
SWIFT_DEFER { diags.setSuppressWarnings(didSuppressWarnings); };
295+
285296
ImportResolver resolver(SF);
286297

287298
// Resolve each import declaration.

0 commit comments

Comments
 (0)