Skip to content

Commit 42fdacc

Browse files
authored
Merge pull request #26991 from DougGregor/delay-parsing-more-often
2 parents 157613a + 28383d4 commit 42fdacc

File tree

12 files changed

+36
-128
lines changed

12 files changed

+36
-128
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,6 @@ class CompilerInstance {
616616

617617
private:
618618
void createREPLFile(const ImplicitImports &implicitImports);
619-
std::unique_ptr<DelayedParsingCallbacks> computeDelayedParsingCallback();
620619

621620
void addMainFileToModule(const ImplicitImports &implicitImports);
622621

@@ -625,20 +624,17 @@ class CompilerInstance {
625624
SourceFile::ASTStage_t LimitStage);
626625

627626
void parseLibraryFile(unsigned BufferID,
628-
const ImplicitImports &implicitImports,
629-
DelayedParsingCallbacks *DelayedCB);
627+
const ImplicitImports &implicitImports);
630628

631629
/// Return true if had load error
632630
bool
633-
parsePartialModulesAndLibraryFiles(const ImplicitImports &implicitImports,
634-
DelayedParsingCallbacks *DelayedCB);
631+
parsePartialModulesAndLibraryFiles(const ImplicitImports &implicitImports);
635632

636633
OptionSet<TypeCheckingFlags> computeTypeCheckingOptions();
637634

638635
void forEachFileToTypeCheck(llvm::function_ref<void(SourceFile &)> fn);
639636

640637
void parseAndTypeCheckMainFileUpTo(SourceFile::ASTStage_t LimitStage,
641-
DelayedParsingCallbacks *DelayedParseCB,
642638
OptionSet<TypeCheckingFlags> TypeCheckOptions);
643639

644640
void finishTypeChecking(OptionSet<TypeCheckingFlags> TypeCheckOptions);

include/swift/Parse/DelayedParsingCallbacks.h

Lines changed: 0 additions & 59 deletions
This file was deleted.

include/swift/Parse/Parser.h

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ namespace llvm {
4646
namespace swift {
4747
class CodeCompletionCallbacks;
4848
class DefaultArgumentInitializer;
49-
class DelayedParsingCallbacks;
5049
class DiagnosticEngine;
5150
class Expr;
5251
class Lexer;
@@ -165,14 +164,8 @@ class Parser {
165164

166165
LocalContext *CurLocalContext = nullptr;
167166

168-
DelayedParsingCallbacks *DelayedParseCB = nullptr;
169-
170167
bool isDelayedParsingEnabled() const {
171-
return DelayBodyParsing || DelayedParseCB != nullptr;
172-
}
173-
174-
void setDelayedParsingCallbacks(DelayedParsingCallbacks *DelayedParseCB) {
175-
this->DelayedParseCB = DelayedParseCB;
168+
return DelayBodyParsing || SourceMgr.getCodeCompletionLoc().isValid();
176169
}
177170

178171
void setCodeCompletionCallbacks(CodeCompletionCallbacks *Callbacks) {

include/swift/Subsystems.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ namespace swift {
4343
class CodeCompletionCallbacksFactory;
4444
class Decl;
4545
class DeclContext;
46-
class DelayedParsingCallbacks;
4746
class DiagnosticConsumer;
4847
class DiagnosticEngine;
4948
class Evaluator;
@@ -114,23 +113,29 @@ namespace swift {
114113
/// \param PersistentState if non-null the same PersistentState object can
115114
/// be used to resume parsing or parse delayed function bodies.
116115
///
117-
/// \param DelayedParseCB if non-null enables delayed parsing for function
118-
/// bodies.
119-
///
120116
/// \return true if the parser found code with side effects.
121117
bool parseIntoSourceFile(SourceFile &SF, unsigned BufferID, bool *Done,
122118
SILParserState *SIL = nullptr,
123119
PersistentParserState *PersistentState = nullptr,
124-
DelayedParsingCallbacks *DelayedParseCB = nullptr,
125120
bool DelayBodyParsing = true);
126121

122+
/// DEPRECATED: Only used to break LLDB/Swift dependency.
123+
inline
124+
bool parseIntoSourceFile(SourceFile &SF, unsigned BufferID, bool *Done,
125+
SILParserState *SIL,
126+
PersistentParserState *PersistentState,
127+
std::nullptr_t,
128+
bool DelayBodyParsing) {
129+
return parseIntoSourceFile(SF, BufferID, Done, SIL, PersistentState,
130+
DelayBodyParsing);
131+
}
132+
127133
/// Parse a single buffer into the given source file, until the full source
128134
/// contents are parsed.
129135
///
130136
/// \return true if the parser found code with side effects.
131137
bool parseIntoSourceFileFull(SourceFile &SF, unsigned BufferID,
132-
PersistentParserState *PersistentState = nullptr,
133-
DelayedParsingCallbacks *DelayedParseCB = nullptr,
138+
PersistentParserState *PersistentState = nullptr,
134139
bool DelayBodyParsing = true);
135140

136141
/// Finish the parsing by going over the nodes that were delayed

lib/Basic/SourceLoc.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ void SourceManager::verifyAllBuffers() const {
3535
}
3636

3737
SourceLoc SourceManager::getCodeCompletionLoc() const {
38+
if (CodeCompletionBufferID == 0U)
39+
return SourceLoc();
40+
3841
return getLocForBufferStart(CodeCompletionBufferID)
3942
.getAdvancedLoc(CodeCompletionOffset);
4043
}

lib/Frontend/Frontend.cpp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "swift/Basic/SourceManager.h"
2525
#include "swift/Basic/Statistic.h"
2626
#include "swift/Frontend/ParseableInterfaceModuleLoader.h"
27-
#include "swift/Parse/DelayedParsingCallbacks.h"
2827
#include "swift/Parse/Lexer.h"
2928
#include "swift/SIL/SILModule.h"
3029
#include "swift/SILOptimizer/PassManager/Passes.h"
@@ -758,14 +757,6 @@ void CompilerInstance::createREPLFile(const ImplicitImports &implicitImports) {
758757
addAdditionalInitialImportsTo(SingleInputFile, implicitImports);
759758
}
760759

761-
std::unique_ptr<DelayedParsingCallbacks>
762-
CompilerInstance::computeDelayedParsingCallback() {
763-
if (Invocation.isCodeCompletion())
764-
return llvm::make_unique<CodeCompleteDelayedCallbacks>(
765-
SourceMgr.getCodeCompletionLoc());
766-
return nullptr;
767-
}
768-
769760
void CompilerInstance::addMainFileToModule(
770761
const ImplicitImports &implicitImports) {
771762
auto *MainFile = createSourceFileForMainModule(
@@ -776,13 +767,10 @@ void CompilerInstance::addMainFileToModule(
776767
void CompilerInstance::parseAndCheckTypesUpTo(
777768
const ImplicitImports &implicitImports, SourceFile::ASTStage_t limitStage) {
778769
FrontendStatsTracer tracer(Context->Stats, "parse-and-check-types");
779-
std::unique_ptr<DelayedParsingCallbacks> DelayedCB{
780-
computeDelayedParsingCallback()};
781770

782771
PersistentState = llvm::make_unique<PersistentParserState>();
783772

784-
bool hadLoadError = parsePartialModulesAndLibraryFiles(
785-
implicitImports, DelayedCB.get());
773+
bool hadLoadError = parsePartialModulesAndLibraryFiles(implicitImports);
786774
if (Invocation.isCodeCompletion()) {
787775
// When we are doing code completion, make sure to emit at least one
788776
// diagnostic, so that ASTContext is marked as erroneous. In this case
@@ -800,7 +788,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
800788
// In addition, the main file has parsing and type-checking
801789
// interwined.
802790
if (MainBufferID != NO_SUCH_BUFFER) {
803-
parseAndTypeCheckMainFileUpTo(limitStage, DelayedCB.get(), TypeCheckOptions);
791+
parseAndTypeCheckMainFileUpTo(limitStage, TypeCheckOptions);
804792
}
805793

806794
assert(llvm::all_of(MainModule->getFiles(), [](const FileUnit *File) -> bool {
@@ -846,8 +834,7 @@ void CompilerInstance::parseAndCheckTypesUpTo(
846834
}
847835

848836
void CompilerInstance::parseLibraryFile(
849-
unsigned BufferID, const ImplicitImports &implicitImports,
850-
DelayedParsingCallbacks *DelayedCB) {
837+
unsigned BufferID, const ImplicitImports &implicitImports) {
851838
FrontendStatsTracer tracer(Context->Stats, "parse-library-file");
852839

853840
auto *NextInput = createSourceFileForMainModule(
@@ -865,7 +852,7 @@ void CompilerInstance::parseLibraryFile(
865852
// Parser may stop at some erroneous constructions like #else, #endif
866853
// or '}' in some cases, continue parsing until we are done
867854
parseIntoSourceFile(*NextInput, BufferID, &Done, nullptr,
868-
PersistentState.get(), DelayedCB,
855+
PersistentState.get(),
869856
/*DelayedBodyParsing=*/!IsPrimary);
870857
} while (!Done);
871858

@@ -893,8 +880,7 @@ OptionSet<TypeCheckingFlags> CompilerInstance::computeTypeCheckingOptions() {
893880
}
894881

895882
bool CompilerInstance::parsePartialModulesAndLibraryFiles(
896-
const ImplicitImports &implicitImports,
897-
DelayedParsingCallbacks *DelayedCB) {
883+
const ImplicitImports &implicitImports) {
898884
FrontendStatsTracer tracer(Context->Stats,
899885
"parse-partial-modules-and-library-files");
900886
bool hadLoadError = false;
@@ -910,15 +896,14 @@ bool CompilerInstance::parsePartialModulesAndLibraryFiles(
910896
// Then parse all the library files.
911897
for (auto BufferID : InputSourceCodeBufferIDs) {
912898
if (BufferID != MainBufferID) {
913-
parseLibraryFile(BufferID, implicitImports, DelayedCB);
899+
parseLibraryFile(BufferID, implicitImports);
914900
}
915901
}
916902
return hadLoadError;
917903
}
918904

919905
void CompilerInstance::parseAndTypeCheckMainFileUpTo(
920906
SourceFile::ASTStage_t LimitStage,
921-
DelayedParsingCallbacks *DelayedParseCB,
922907
OptionSet<TypeCheckingFlags> TypeCheckOptions) {
923908
FrontendStatsTracer tracer(Context->Stats,
924909
"parse-and-typecheck-main-file");
@@ -942,7 +927,7 @@ void CompilerInstance::parseAndTypeCheckMainFileUpTo(
942927
// with 'sil' definitions.
943928
parseIntoSourceFile(MainFile, MainFile.getBufferID().getValue(), &Done,
944929
TheSILModule ? &SILContext : nullptr,
945-
PersistentState.get(), DelayedParseCB,
930+
PersistentState.get(),
946931
/*DelayedBodyParsing=*/false);
947932

948933
if (mainIsPrimary && (Done || CurTUElem < MainFile.Decls.size())) {
@@ -1069,7 +1054,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
10691054
BufferID);
10701055

10711056
parseIntoSourceFileFull(*NextInput, BufferID, PersistentState.get(),
1072-
nullptr, /*DelayBodyParsing=*/!IsPrimary);
1057+
/*DelayBodyParsing=*/!IsPrimary);
10731058
}
10741059

10751060
// Now parse the main file.
@@ -1079,7 +1064,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
10791064
MainFile.SyntaxParsingCache = Invocation.getMainFileSyntaxParsingCache();
10801065

10811066
parseIntoSourceFileFull(MainFile, MainFile.getBufferID().getValue(),
1082-
PersistentState.get(), nullptr,
1067+
PersistentState.get(),
10831068
/*DelayBodyParsing=*/false);
10841069
}
10851070

lib/IDE/REPLCodeCompletion.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "swift/AST/Module.h"
2121
#include "swift/Basic/LLVM.h"
2222
#include "swift/Basic/SourceManager.h"
23-
#include "swift/Parse/DelayedParsingCallbacks.h"
2423
#include "swift/Parse/Parser.h"
2524
#include "swift/IDE/CodeCompletion.h"
2625
#include "swift/Subsystems.h"
@@ -208,12 +207,9 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
208207
const unsigned OriginalDeclCount = SF.Decls.size();
209208

210209
PersistentParserState PersistentState;
211-
std::unique_ptr<DelayedParsingCallbacks> DelayedCB(
212-
new CodeCompleteDelayedCallbacks(Ctx.SourceMgr.getCodeCompletionLoc()));
213210
bool Done;
214211
do {
215-
parseIntoSourceFile(SF, *BufferID, &Done, nullptr, &PersistentState,
216-
DelayedCB.get());
212+
parseIntoSourceFile(SF, *BufferID, &Done, nullptr, &PersistentState);
217213
} while (!Done);
218214
performTypeChecking(SF, PersistentState.getTopLevelContext(), None,
219215
OriginalDeclCount);

lib/Parse/ParseDecl.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "swift/Parse/Parser.h"
1818
#include "swift/Parse/CodeCompletionCallbacks.h"
19-
#include "swift/Parse/DelayedParsingCallbacks.h"
2019
#include "swift/Parse/ParsedSyntaxRecorder.h"
2120
#include "swift/Parse/ParseSILSupport.h"
2221
#include "swift/Parse/SyntaxParsingContext.h"
@@ -5387,9 +5386,8 @@ void Parser::consumeAbstractFunctionBody(AbstractFunctionDecl *AFD,
53875386

53885387
BodyRange.End = PreviousLoc;
53895388

5390-
if (DelayedParseCB &&
5391-
DelayedParseCB->shouldDelayFunctionBodyParsing(*this, AFD, Attrs,
5392-
BodyRange)) {
5389+
if (SourceMgr.getCodeCompletionLoc().isInvalid() ||
5390+
SourceMgr.rangeContainsCodeCompletionLoc(BodyRange)) {
53935391
AFD->setBodyDelayed(BodyRange);
53945392
} else {
53955393
AFD->setBodySkipped(BodyRange);

lib/Parse/ParseRequests.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,12 @@ BraceStmt *ParseAbstractFunctionBodyRequest::evaluate(
7474
}
7575

7676
case BodyKind::Unparsed: {
77-
// FIXME: It should be fine to delay body parsing of local functions, so
78-
// the DelayBodyParsing should go away entirely
7977
// FIXME: How do we configure code completion?
8078
SourceFile &sf = *afd->getDeclContext()->getParentSourceFile();
8179
SourceManager &sourceMgr = sf.getASTContext().SourceMgr;
8280
unsigned bufferID = sourceMgr.findBufferContainingLoc(afd->getLoc());
8381
Parser parser(bufferID, sf, static_cast<SILParserTUStateBase *>(nullptr),
84-
nullptr, nullptr, /*DelayBodyParsing=*/false);
82+
nullptr, nullptr);
8583
parser.SyntaxContext->setDiscard();
8684
auto body = parser.parseAbstractFunctionBodyDelayed(afd);
8785
afd->setBodyKind(BodyKind::Parsed);

lib/Parse/Parser.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include "swift/Basic/Timer.h"
2626
#include "swift/Parse/Lexer.h"
2727
#include "swift/Parse/CodeCompletionCallbacks.h"
28-
#include "swift/Parse/DelayedParsingCallbacks.h"
2928
#include "swift/Parse/ParsedSyntaxNodes.h"
3029
#include "swift/Parse/ParsedSyntaxRecorder.h"
3130
#include "swift/Parse/ParseSILSupport.h"
@@ -112,7 +111,6 @@ void tokenize(const LangOptions &LangOpts, const SourceManager &SM,
112111
using namespace swift;
113112
using namespace swift::syntax;
114113

115-
void DelayedParsingCallbacks::anchor() { }
116114
void SILParserTUStateBase::anchor() { }
117115

118116
namespace {

0 commit comments

Comments
 (0)