Skip to content

Commit ee20453

Browse files
authored
Merge pull request swiftlang#31725 from CodaFi/right-fold
[NFC] Fold performParseOnly into performSemaUpTo
2 parents 4b9fca5 + 4ff5b90 commit ee20453

File tree

2 files changed

+67
-80
lines changed

2 files changed

+67
-80
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,11 @@ class CompilerInstance {
653653
/// Retrieve a description of which modules should be implicitly imported.
654654
ImplicitImportInfo getImplicitImportInfo() const;
655655

656-
void performSemaUpTo(SourceFile::ASTStage_t LimitStage);
656+
void performSemaUpTo(SourceFile::ASTStage_t LimitStage,
657+
SourceFile::ParsingOptions POpts = {});
657658

658659
/// Return true if had load error
659-
bool parsePartialModulesAndInputFiles();
660+
bool loadPartialModulesAndImplicitImports();
660661

661662
void forEachFileToTypeCheck(llvm::function_ref<void(SourceFile &)> fn);
662663

lib/Frontend/Frontend.cpp

Lines changed: 64 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,24 @@ ModuleDecl *CompilerInstance::getMainModule() const {
731731
return MainModule;
732732
}
733733

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+
734752
void CompilerInstance::performParseAndResolveImportsOnly() {
735753
performSemaUpTo(SourceFile::ImportsResolved);
736754
}
@@ -739,33 +757,55 @@ void CompilerInstance::performSema() {
739757
performSemaUpTo(SourceFile::TypeChecked);
740758
}
741759

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) {
745762
FrontendStatsTracer tracer(getStatsReporter(), "perform-sema");
746763

747764
ModuleDecl *mainModule = getMainModule();
748765
Context->LoadedModules[mainModule->getName()] = mainModule;
749766

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;
753772
}
754773

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-
761774
// Make sure the main file is the first file in the module, so do this now.
762775
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+
}
765806
}
766807

767-
bool hadLoadError = parsePartialModulesAndInputFiles();
768-
if (hadLoadError)
808+
if (LimitStage == SourceFile::Unprocessed)
769809
return;
770810

771811
assert(llvm::all_of(MainModule->getFiles(), [](const FileUnit *File) -> bool {
@@ -808,9 +848,15 @@ bool CompilerInstance::loadStdlib() {
808848
return true;
809849
}
810850

811-
bool CompilerInstance::parsePartialModulesAndInputFiles() {
851+
bool CompilerInstance::loadPartialModulesAndImplicitImports() {
812852
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+
814860
bool hadLoadError = false;
815861
// Parse all the partial modules first.
816862
for (auto &PM : PartialModules) {
@@ -821,20 +867,6 @@ bool CompilerInstance::parsePartialModulesAndInputFiles() {
821867
/*treatAsPartialModule*/true))
822868
hadLoadError = true;
823869
}
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-
}
838870
return hadLoadError;
839871
}
840872

@@ -903,52 +935,6 @@ SourceFile *CompilerInstance::createSourceFileForMainModule(
903935
return inputFile;
904936
}
905937

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-
952938
void CompilerInstance::freeASTContext() {
953939
TheSILTypes.reset();
954940
Context.reset();

0 commit comments

Comments
 (0)