Skip to content

Commit 7c52dee

Browse files
committed
[Parse] Consolidate body parsing for Func/Constructor/Destructor decls
1 parent 2e5b398 commit 7c52dee

File tree

2 files changed

+59
-104
lines changed

2 files changed

+59
-104
lines changed

include/swift/Parse/Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,7 @@ class Parser {
930930
StaticSpellingKind StaticSpelling,
931931
ParseDeclOptions Flags,
932932
DeclAttributes &Attributes);
933+
void parseAbstractFunctionBody(AbstractFunctionDecl *AFD);
933934
bool parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD);
934935
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
935936
DeclAttributes &Attributes);

lib/Parse/ParseDecl.cpp

Lines changed: 58 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -5305,63 +5305,26 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
53055305
if (rethrows) {
53065306
Attributes.add(new (Context) RethrowsAttr(throwsLoc));
53075307
}
5308-
5309-
// Enter the arguments for the function into a new function-body scope. We
5310-
// need this even if there is no function body to detect argument name
5311-
// duplication.
5312-
{
5313-
Scope S(this, ScopeKind::FunctionBody);
53145308

5315-
diagnoseOperatorFixityAttributes(*this, Attributes, FD);
5316-
5317-
// Add the attributes here so if we need them while parsing the body
5318-
// they are available.
5319-
FD->getAttrs() = Attributes;
5309+
diagnoseOperatorFixityAttributes(*this, Attributes, FD);
5310+
// Add the attributes here so if we need them while parsing the body
5311+
// they are available.
5312+
FD->getAttrs() = Attributes;
53205313

5321-
// Pass the function signature to code completion.
5322-
if (SignatureHasCodeCompletion)
5323-
CodeCompletion->setParsedDecl(FD);
5314+
// Pass the function signature to code completion.
5315+
if (SignatureHasCodeCompletion)
5316+
CodeCompletion->setParsedDecl(FD);
53245317

5325-
DefaultArgs.setFunctionContext(FD, FD->getParameters());
5326-
if (auto *P = FD->getImplicitSelfDecl())
5327-
addToScope(P);
5328-
addParametersToScope(FD->getParameters());
5329-
setLocalDiscriminator(FD);
5330-
5331-
// Establish the new context.
5332-
ParseFunctionBody CC(*this, FD);
5333-
setLocalDiscriminatorToParamList(FD->getParameters());
5318+
DefaultArgs.setFunctionContext(FD, FD->getParameters());
5319+
setLocalDiscriminator(FD);
53345320

5335-
// Check to see if we have a "{" to start a brace statement.
5321+
if (Flags.contains(PD_InProtocol)) {
53365322
if (Tok.is(tok::l_brace)) {
5337-
// Record the curly braces but nothing inside.
5338-
SF.recordInterfaceToken("{");
5339-
SF.recordInterfaceToken("}");
5340-
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);
5341-
5342-
if (Flags.contains(PD_InProtocol)) {
5343-
diagnose(Tok, diag::protocol_method_with_body);
5344-
skipUntilDeclRBrace();
5345-
} else if (!isDelayedParsingEnabled()) {
5346-
if (Context.Stats)
5347-
Context.Stats->getFrontendCounters().NumFunctionsParsed++;
5348-
5349-
ParserResult<BraceStmt> Body =
5350-
parseBraceItemList(diag::func_decl_without_brace);
5351-
if (Body.isNull()) {
5352-
// FIXME: Should do some sort of error recovery here?
5353-
} else if (SignatureStatus.hasCodeCompletion()) {
5354-
// Code completion was inside the signature, don't attach the body.
5355-
FD->setBodySkipped(Body.get()->getSourceRange());
5356-
} else {
5357-
FD->setBody(Body.get());
5358-
}
5359-
} else {
5360-
consumeAbstractFunctionBody(FD, Attributes);
5361-
}
5362-
} else {
5363-
checkForInputIncomplete();
5323+
diagnose(Tok, diag::protocol_method_with_body);
5324+
skipSingle();
53645325
}
5326+
} else {
5327+
parseAbstractFunctionBody(FD);
53655328
}
53665329

53675330
// Exit the scope introduced for the generic parameters.
@@ -5371,6 +5334,44 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
53715334
return DCC.fixupParserResult(FD);
53725335
}
53735336

5337+
/// Parse function body into \p AFD.
5338+
void Parser::parseAbstractFunctionBody(AbstractFunctionDecl *AFD) {
5339+
Scope S(this, ScopeKind::FunctionBody);
5340+
5341+
// Enter the arguments for the function into a new function-body scope. We
5342+
// need this even if there is no function body to detect argument name
5343+
// duplication.
5344+
if (auto *P = AFD->getImplicitSelfDecl())
5345+
addToScope(P);
5346+
addParametersToScope(AFD->getParameters());
5347+
5348+
// Establish the new context.
5349+
ParseFunctionBody CC(*this, AFD);
5350+
setLocalDiscriminatorToParamList(AFD->getParameters());
5351+
5352+
if (!Tok.is(tok::l_brace)) {
5353+
checkForInputIncomplete();
5354+
return;
5355+
}
5356+
5357+
// Record the curly braces but nothing inside.
5358+
SF.recordInterfaceToken("{");
5359+
SF.recordInterfaceToken("}");
5360+
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);
5361+
5362+
if (isDelayedParsingEnabled()) {
5363+
consumeAbstractFunctionBody(AFD, AFD->getAttrs());
5364+
return;
5365+
}
5366+
5367+
if (Context.Stats)
5368+
Context.Stats->getFrontendCounters().NumFunctionsParsed++;
5369+
5370+
ParserResult<BraceStmt> Body = parseBraceItemList(diag::invalid_diagnostic);
5371+
if (!Body.isNull())
5372+
AFD->setBody(Body.get());
5373+
}
5374+
53745375
bool Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
53755376
assert(!AFD->getBody() && "function should not have a parsed body");
53765377
assert(AFD->getBodyKind() == AbstractFunctionDecl::BodyKind::Unparsed &&
@@ -6225,7 +6226,6 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
62256226

62266227
CD->setGenericParams(GenericParams);
62276228

6228-
Scope S2(this, ScopeKind::ConstructorBody);
62296229
CtorInitializerKind initKind = CtorInitializerKind::Designated;
62306230
if (Attributes.hasAttribute<ConvenienceAttr>())
62316231
initKind = CtorInitializerKind::Convenience;
@@ -6244,37 +6244,13 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
62446244
CD->setInvalid();
62456245
}
62466246

6247-
addToScope(CD->getImplicitSelfDecl());
6248-
addParametersToScope(Params.get());
6249-
6250-
// '{'
6251-
if (Tok.is(tok::l_brace)) {
6252-
// Record the curly braces but nothing inside.
6253-
SF.recordInterfaceToken("{");
6254-
SF.recordInterfaceToken("}");
6255-
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);
6256-
6257-
if (Flags.contains(PD_InProtocol)) {
6247+
if (Flags.contains(PD_InProtocol)) {
6248+
if (Tok.is(tok::l_brace)) {
62586249
diagnose(Tok, diag::protocol_init_with_body);
6259-
skipUntilDeclRBrace();
6260-
} else {
6261-
// Parse the body.
6262-
ParseFunctionBody CC(*this, CD);
6263-
setLocalDiscriminatorToParamList(CD->getParameters());
6264-
6265-
if (!isDelayedParsingEnabled()) {
6266-
if (Context.Stats)
6267-
Context.Stats->getFrontendCounters().NumFunctionsParsed++;
6268-
6269-
ParserResult<BraceStmt> Body =
6270-
parseBraceItemList(diag::invalid_diagnostic);
6271-
6272-
if (!Body.isNull())
6273-
CD->setBody(Body.get());
6274-
} else {
6275-
consumeAbstractFunctionBody(CD, Attributes);
6276-
}
6250+
skipSingle();
62776251
}
6252+
} else {
6253+
parseAbstractFunctionBody(CD);
62786254
}
62796255

62806256
CD->getAttrs() = Attributes;
@@ -6327,30 +6303,8 @@ parseDeclDeinit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
63276303
}
63286304
}
63296305

6330-
Scope S(this, ScopeKind::DestructorBody);
63316306
auto *DD = new (Context) DestructorDecl(DestructorLoc, CurDeclContext);
6332-
6333-
// Parse the body.
6334-
if (Tok.is(tok::l_brace)) {
6335-
// Record the curly braces but nothing inside.
6336-
SF.recordInterfaceToken("{");
6337-
SF.recordInterfaceToken("}");
6338-
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);
6339-
6340-
ParseFunctionBody CC(*this, DD);
6341-
if (!isDelayedParsingEnabled()) {
6342-
if (Context.Stats)
6343-
Context.Stats->getFrontendCounters().NumFunctionsParsed++;
6344-
6345-
ParserResult<BraceStmt> Body =
6346-
parseBraceItemList(diag::invalid_diagnostic);
6347-
6348-
if (!Body.isNull())
6349-
DD->setBody(Body.get());
6350-
} else {
6351-
consumeAbstractFunctionBody(DD, Attributes);
6352-
}
6353-
}
6307+
parseAbstractFunctionBody(DD);
63546308

63556309
DD->getAttrs() = Attributes;
63566310

0 commit comments

Comments
 (0)