Skip to content

[CodeCompleiton] Avoid walking the whole module to find the delayed func #28014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,14 @@ class Parser {
LocalContext *CurLocalContext = nullptr;

bool isDelayedParsingEnabled() const {
return DelayBodyParsing || SourceMgr.getCodeCompletionLoc().isValid();
return DelayBodyParsing || isCodeCompletionFirstPass();
}

void setCodeCompletionCallbacks(CodeCompletionCallbacks *Callbacks) {
CodeCompletion = Callbacks;
}

bool isCodeCompletionFirstPass() {
bool isCodeCompletionFirstPass() const {
return L->isCodeCompletion() && !CodeCompletion;
}

Expand Down Expand Up @@ -1057,6 +1057,7 @@ class Parser {
bool HasFuncKeyword = true);
void parseAbstractFunctionBody(AbstractFunctionDecl *AFD);
BraceStmt *parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD);
void parseAbstractFunctionBodyDelayed();
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
DeclAttributes &Attributes);

Expand Down
1 change: 1 addition & 0 deletions include/swift/Parse/PersistentParserState.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class PersistentParserState {
enum class DelayedDeclKind {
TopLevelCodeDecl,
Decl,
FunctionBody,
};

class DelayedDeclState {
Expand Down
8 changes: 3 additions & 5 deletions include/swift/Subsystems.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,9 @@ namespace swift {
PersistentParserState *PersistentState = nullptr,
bool DelayBodyParsing = true);

/// Finish the parsing by going over the nodes that were delayed
/// during the first parsing pass.
void performDelayedParsing(DeclContext *DC,
PersistentParserState &PersistentState,
CodeCompletionCallbacksFactory *Factory);
/// Finish the code completion.
void performCodeCompletionSecondPass(PersistentParserState &PersistentState,
CodeCompletionCallbacksFactory &Factory);

/// Lex and return a vector of tokens for the given buffer.
std::vector<Token> tokenize(const LangOptions &LangOpts,
Expand Down
4 changes: 2 additions & 2 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -869,8 +869,8 @@ void CompilerInstance::parseAndCheckTypesUpTo(
});

if (Invocation.isCodeCompletion()) {
performDelayedParsing(MainModule, *PersistentState.get(),
Invocation.getCodeCompletionFactory());
performCodeCompletionSecondPass(*PersistentState.get(),
*Invocation.getCodeCompletionFactory());
}
finishTypeChecking(TypeCheckOptions);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/REPLCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ doCodeCompletion(SourceFile &SF, StringRef EnteredCode, unsigned *BufferID,
performTypeChecking(SF, PersistentState.getTopLevelContext(), None,
OriginalDeclCount);

performDelayedParsing(&SF, PersistentState, CompletionCallbacksFactory);
performCodeCompletionSecondPass(PersistentState, *CompletionCallbacksFactory);

// Now we are done with code completion. Remove the declarations we
// temporarily inserted.
Expand Down
29 changes: 24 additions & 5 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5340,11 +5340,16 @@ void Parser::consumeAbstractFunctionBody(AbstractFunctionDecl *AFD,

BodyRange.End = PreviousLoc;

if (SourceMgr.getCodeCompletionLoc().isInvalid() ||
SourceMgr.rangeContainsCodeCompletionLoc(BodyRange)) {
AFD->setBodyDelayed(BodyRange);
} else {
AFD->setBodySkipped(BodyRange);
AFD->setBodyDelayed(BodyRange);

if (isCodeCompletionFirstPass()) {
if (SourceMgr.rangeContainsCodeCompletionLoc(BodyRange)) {
State->delayDecl(PersistentParserState::DelayedDeclKind::FunctionBody,
PD_Default, AFD, BodyRange,
BeginParserPosition.PreviousLoc);
} else {
AFD->setBodySkipped(BodyRange);
}
}
}

Expand Down Expand Up @@ -5656,6 +5661,20 @@ BraceStmt *Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
return parseBraceItemList(diag::func_decl_without_brace).getPtrOrNull();
}

/// Parse a delayed function body from the 'PersistentParserState'.
void Parser::parseAbstractFunctionBodyDelayed() {
auto DelayedState = State->takeDelayedDeclState();
assert(DelayedState.get() && "should have delayed state");
auto CD = DelayedState->ParentContext->getAsDecl();
auto AFD = cast<AbstractFunctionDecl>(CD);

// Eagarly parse local decls or nested function bodies inside the body.
llvm::SaveAndRestore<bool> DisableDelayedBody(DelayBodyParsing, false);

auto body = parseAbstractFunctionBodyDelayed(AFD);
AFD->setBodyParsed(body);
}

/// Parse a 'enum' declaration, returning true (and doing no token
/// skipping) on error.
///
Expand Down
94 changes: 17 additions & 77 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,75 +110,24 @@ using namespace swift::syntax;

void SILParserTUStateBase::anchor() { }

namespace {
/// A visitor that does delayed parsing of function bodies.
class ParseDelayedFunctionBodies : public ASTWalker {
PersistentParserState &ParserState;
CodeCompletionCallbacksFactory *CodeCompletionFactory;

public:
ParseDelayedFunctionBodies(PersistentParserState &ParserState,
CodeCompletionCallbacksFactory *Factory)
: ParserState(ParserState), CodeCompletionFactory(Factory) {}

bool walkToDeclPre(Decl *D) override {
if (auto AFD = dyn_cast<AbstractFunctionDecl>(D)) {
if (AFD->getBodyKind() != FuncDecl::BodyKind::Unparsed)
return false;
parseFunctionBody(AFD);
return true;
}
return true;
}

private:
void parseFunctionBody(AbstractFunctionDecl *AFD) {
// FIXME: This duplicates the evaluation of
// ParseAbstractFunctionBodyRequest, but installs a code completion
// factory.
assert(AFD->getBodyKind() == FuncDecl::BodyKind::Unparsed);

SourceFile &SF = *AFD->getDeclContext()->getParentSourceFile();
SourceManager &SourceMgr = SF.getASTContext().SourceMgr;
unsigned BufferID = SourceMgr.findBufferContainingLoc(AFD->getLoc());
Parser TheParser(BufferID, SF, nullptr, &ParserState, nullptr,
/*DelayBodyParsing=*/false);
TheParser.SyntaxContext->disable();
std::unique_ptr<CodeCompletionCallbacks> CodeCompletion;
if (CodeCompletionFactory) {
CodeCompletion.reset(
CodeCompletionFactory->createCodeCompletionCallbacks(TheParser));
TheParser.setCodeCompletionCallbacks(CodeCompletion.get());
}
auto body = TheParser.parseAbstractFunctionBodyDelayed(AFD);
AFD->setBodyParsed(body);

if (CodeCompletion)
CodeCompletion->doneParsing();
}
};

static void parseDelayedDecl(
PersistentParserState &ParserState,
CodeCompletionCallbacksFactory *CodeCompletionFactory) {
void swift::performCodeCompletionSecondPass(
PersistentParserState &ParserState,
CodeCompletionCallbacksFactory &Factory) {
SharedTimer timer("CodeCompletionSecondPass");
if (!ParserState.hasDelayedDecl())
return;

SourceFile &SF = *ParserState.getDelayedDeclContext()->getParentSourceFile();
SourceManager &SourceMgr = SF.getASTContext().SourceMgr;
unsigned BufferID =
SourceMgr.findBufferContainingLoc(ParserState.getDelayedDeclLoc());
auto &SF = *ParserState.getDelayedDeclContext()->getParentSourceFile();
auto &SM = SF.getASTContext().SourceMgr;
auto BufferID = SM.findBufferContainingLoc(ParserState.getDelayedDeclLoc());
Parser TheParser(BufferID, SF, nullptr, &ParserState, nullptr);

// Disable libSyntax creation in the delayed parsing.
TheParser.SyntaxContext->disable();

std::unique_ptr<CodeCompletionCallbacks> CodeCompletion;
if (CodeCompletionFactory) {
CodeCompletion.reset(
CodeCompletionFactory->createCodeCompletionCallbacks(TheParser));
TheParser.setCodeCompletionCallbacks(CodeCompletion.get());
}
std::unique_ptr<CodeCompletionCallbacks> CodeCompletion(
Factory.createCodeCompletionCallbacks(TheParser));
TheParser.setCodeCompletionCallbacks(CodeCompletion.get());

switch (ParserState.getDelayedDeclKind()) {
case PersistentParserState::DelayedDeclKind::TopLevelCodeDecl:
Expand All @@ -188,13 +137,16 @@ static void parseDelayedDecl(
case PersistentParserState::DelayedDeclKind::Decl:
TheParser.parseDeclDelayed();
break;

case PersistentParserState::DelayedDeclKind::FunctionBody: {
TheParser.parseAbstractFunctionBodyDelayed();
break;
}
}
assert(!ParserState.hasDelayedDecl());

if (CodeCompletion)
CodeCompletion->doneParsing();
CodeCompletion->doneParsing();
}
} // unnamed namespace


swift::Parser::BacktrackingScope::~BacktrackingScope() {
if (Backtrack) {
Expand All @@ -203,18 +155,6 @@ swift::Parser::BacktrackingScope::~BacktrackingScope() {
}
}

void swift::performDelayedParsing(
DeclContext *DC, PersistentParserState &PersistentState,
CodeCompletionCallbacksFactory *CodeCompletionFactory) {
SharedTimer timer("Parsing");
ParseDelayedFunctionBodies Walker(PersistentState,
CodeCompletionFactory);
DC->walkContext(Walker);

if (CodeCompletionFactory)
parseDelayedDecl(PersistentState, CodeCompletionFactory);
}

/// Tokenizes a string literal, taking into account string interpolation.
static void getStringPartTokens(const Token &Tok, const LangOptions &LangOpts,
const SourceManager &SM,
Expand Down