Skip to content

Commit

Permalink
[Parse] Consolidate AFD body parsing implemtations
Browse files Browse the repository at this point in the history
  • Loading branch information
rintaro committed Feb 12, 2020
1 parent 6640316 commit 8936633
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
2 changes: 2 additions & 0 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,8 @@ class Parser {
ParseDeclOptions Flags,
DeclAttributes &Attributes,
bool HasFuncKeyword = true);
ParserResult<BraceStmt>
parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD);
void parseAbstractFunctionBody(AbstractFunctionDecl *AFD);
BraceStmt *parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD);
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
Expand Down
48 changes: 26 additions & 22 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6328,7 +6328,30 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
return DCC.fixupParserResult(FD);
}

/// Parse function body into \p AFD.
/// Parse a function body for \p AFD and returns it without setting the body
/// to \p AFD .
ParserResult<BraceStmt>
Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
assert(Tok.is(tok::l_brace));

// Enter the arguments for the function into a new function-body scope. We
// need this even if there is no function body to detect argument name
// duplication.
if (auto *P = AFD->getImplicitSelfDecl())
addToScope(P);
addParametersToScope(AFD->getParameters());

// Establish the new context.
ParseFunctionBody CC(*this, AFD);
setLocalDiscriminatorToParamList(AFD->getParameters());

if (Context.Stats)
Context.Stats->getFrontendCounters().NumFunctionsParsed++;

return parseBraceItemList(diag::invalid_diagnostic);
}

/// Parse function body into \p AFD or skip it for delayed parsing.
void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
if (!Tok.is(tok::l_brace)) {
checkForInputIncomplete();
Expand All @@ -6348,21 +6371,7 @@ void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {

Scope S(this, ScopeKind::FunctionBody);

// Enter the arguments for the function into a new function-body scope. We
// need this even if there is no function body to detect argument name
// duplication.
if (auto *P = AFD->getImplicitSelfDecl())
addToScope(P);
addParametersToScope(AFD->getParameters());

// Establish the new context.
ParseFunctionBody CC(*this, AFD);
setLocalDiscriminatorToParamList(AFD->getParameters());

if (Context.Stats)
Context.Stats->getFrontendCounters().NumFunctionsParsed++;

ParserResult<BraceStmt> Body = parseBraceItemList(diag::invalid_diagnostic);
ParserResult<BraceStmt> Body = parseAbstractFunctionBodyImpl(AFD);
if (!Body.isNull()) {
BraceStmt * BS = Body.get();
AFD->setBodyParsed(BS);
Expand Down Expand Up @@ -6445,13 +6454,8 @@ BraceStmt *Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
// Re-enter the lexical scope.
Scope TopLevelScope(this, ScopeKind::TopLevel);
Scope S(this, ScopeKind::FunctionBody);
if (auto *P = AFD->getImplicitSelfDecl())
addToScope(P);
addParametersToScope(AFD->getParameters());
ParseFunctionBody CC(*this, AFD);
setLocalDiscriminatorToParamList(AFD->getParameters());

return parseBraceItemList(diag::func_decl_without_brace).getPtrOrNull();
return parseAbstractFunctionBodyImpl(AFD).getPtrOrNull();
}

/// Parse a 'enum' declaration, returning true (and doing no token
Expand Down
11 changes: 1 addition & 10 deletions lib/Parse/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,7 @@ void Parser::performCodeCompletionSecondPassImpl(

case CodeCompletionDelayedDeclKind::FunctionBody: {
auto *AFD = cast<AbstractFunctionDecl>(DC);

if (auto *P = AFD->getImplicitSelfDecl())
addToScope(P);
addParametersToScope(AFD->getParameters());

ParseFunctionBody CC(*this, AFD);
setLocalDiscriminatorToParamList(AFD->getParameters());

auto result = parseBraceItemList(diag::func_decl_without_brace);
AFD->setBody(result.getPtrOrNull());
AFD->setBodyParsed(parseAbstractFunctionBodyImpl(AFD).getPtrOrNull());
break;
}
}
Expand Down

0 comments on commit 8936633

Please sign in to comment.