Skip to content

Commit 8936633

Browse files
committed
[Parse] Consolidate AFD body parsing implemtations
1 parent 6640316 commit 8936633

File tree

3 files changed

+29
-32
lines changed

3 files changed

+29
-32
lines changed

include/swift/Parse/Parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,8 @@ class Parser {
11221122
ParseDeclOptions Flags,
11231123
DeclAttributes &Attributes,
11241124
bool HasFuncKeyword = true);
1125+
ParserResult<BraceStmt>
1126+
parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD);
11251127
void parseAbstractFunctionBody(AbstractFunctionDecl *AFD);
11261128
BraceStmt *parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD);
11271129
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,

lib/Parse/ParseDecl.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6328,7 +6328,30 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
63286328
return DCC.fixupParserResult(FD);
63296329
}
63306330

6331-
/// Parse function body into \p AFD.
6331+
/// Parse a function body for \p AFD and returns it without setting the body
6332+
/// to \p AFD .
6333+
ParserResult<BraceStmt>
6334+
Parser::parseAbstractFunctionBodyImpl(AbstractFunctionDecl *AFD) {
6335+
assert(Tok.is(tok::l_brace));
6336+
6337+
// Enter the arguments for the function into a new function-body scope. We
6338+
// need this even if there is no function body to detect argument name
6339+
// duplication.
6340+
if (auto *P = AFD->getImplicitSelfDecl())
6341+
addToScope(P);
6342+
addParametersToScope(AFD->getParameters());
6343+
6344+
// Establish the new context.
6345+
ParseFunctionBody CC(*this, AFD);
6346+
setLocalDiscriminatorToParamList(AFD->getParameters());
6347+
6348+
if (Context.Stats)
6349+
Context.Stats->getFrontendCounters().NumFunctionsParsed++;
6350+
6351+
return parseBraceItemList(diag::invalid_diagnostic);
6352+
}
6353+
6354+
/// Parse function body into \p AFD or skip it for delayed parsing.
63326355
void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
63336356
if (!Tok.is(tok::l_brace)) {
63346357
checkForInputIncomplete();
@@ -6348,21 +6371,7 @@ void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
63486371

63496372
Scope S(this, ScopeKind::FunctionBody);
63506373

6351-
// Enter the arguments for the function into a new function-body scope. We
6352-
// need this even if there is no function body to detect argument name
6353-
// duplication.
6354-
if (auto *P = AFD->getImplicitSelfDecl())
6355-
addToScope(P);
6356-
addParametersToScope(AFD->getParameters());
6357-
6358-
// Establish the new context.
6359-
ParseFunctionBody CC(*this, AFD);
6360-
setLocalDiscriminatorToParamList(AFD->getParameters());
6361-
6362-
if (Context.Stats)
6363-
Context.Stats->getFrontendCounters().NumFunctionsParsed++;
6364-
6365-
ParserResult<BraceStmt> Body = parseBraceItemList(diag::invalid_diagnostic);
6374+
ParserResult<BraceStmt> Body = parseAbstractFunctionBodyImpl(AFD);
63666375
if (!Body.isNull()) {
63676376
BraceStmt * BS = Body.get();
63686377
AFD->setBodyParsed(BS);
@@ -6445,13 +6454,8 @@ BraceStmt *Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
64456454
// Re-enter the lexical scope.
64466455
Scope TopLevelScope(this, ScopeKind::TopLevel);
64476456
Scope S(this, ScopeKind::FunctionBody);
6448-
if (auto *P = AFD->getImplicitSelfDecl())
6449-
addToScope(P);
6450-
addParametersToScope(AFD->getParameters());
6451-
ParseFunctionBody CC(*this, AFD);
6452-
setLocalDiscriminatorToParamList(AFD->getParameters());
64536457

6454-
return parseBraceItemList(diag::func_decl_without_brace).getPtrOrNull();
6458+
return parseAbstractFunctionBodyImpl(AFD).getPtrOrNull();
64556459
}
64566460

64576461
/// Parse a 'enum' declaration, returning true (and doing no token

lib/Parse/Parser.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,7 @@ void Parser::performCodeCompletionSecondPassImpl(
197197

198198
case CodeCompletionDelayedDeclKind::FunctionBody: {
199199
auto *AFD = cast<AbstractFunctionDecl>(DC);
200-
201-
if (auto *P = AFD->getImplicitSelfDecl())
202-
addToScope(P);
203-
addParametersToScope(AFD->getParameters());
204-
205-
ParseFunctionBody CC(*this, AFD);
206-
setLocalDiscriminatorToParamList(AFD->getParameters());
207-
208-
auto result = parseBraceItemList(diag::func_decl_without_brace);
209-
AFD->setBody(result.getPtrOrNull());
200+
AFD->setBodyParsed(parseAbstractFunctionBodyImpl(AFD).getPtrOrNull());
210201
break;
211202
}
212203
}

0 commit comments

Comments
 (0)