Skip to content

[Parse] Consolidate body parsing for Func/Constructor/Destructor decls #19018

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 3 commits into from
Aug 29, 2018
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
1 change: 1 addition & 0 deletions include/swift/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,7 @@ class Parser {
StaticSpellingKind StaticSpelling,
ParseDeclOptions Flags,
DeclAttributes &Attributes);
void parseAbstractFunctionBody(AbstractFunctionDecl *AFD);
bool parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD);
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
DeclAttributes &Attributes);
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Parse/Scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ enum class ScopeKind {
StructBody,
ClassBody,
ProtocolBody,
ConstructorBody,
DestructorBody,
InheritanceClause,

Brace,
TopLevel,
ForVars,
ForeachVars,
CaseVars,
CatchVars,
Expand Down
164 changes: 60 additions & 104 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5305,63 +5305,26 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
if (rethrows) {
Attributes.add(new (Context) RethrowsAttr(throwsLoc));
}

// 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.
{
Scope S(this, ScopeKind::FunctionBody);

diagnoseOperatorFixityAttributes(*this, Attributes, FD);

// Add the attributes here so if we need them while parsing the body
// they are available.
FD->getAttrs() = Attributes;
diagnoseOperatorFixityAttributes(*this, Attributes, FD);
// Add the attributes here so if we need them while parsing the body
// they are available.
FD->getAttrs() = Attributes;

// Pass the function signature to code completion.
if (SignatureHasCodeCompletion)
CodeCompletion->setParsedDecl(FD);
// Pass the function signature to code completion.
if (SignatureHasCodeCompletion)
CodeCompletion->setParsedDecl(FD);

DefaultArgs.setFunctionContext(FD, FD->getParameters());
if (auto *P = FD->getImplicitSelfDecl())
addToScope(P);
addParametersToScope(FD->getParameters());
setLocalDiscriminator(FD);

// Establish the new context.
ParseFunctionBody CC(*this, FD);
setLocalDiscriminatorToParamList(FD->getParameters());
DefaultArgs.setFunctionContext(FD, FD->getParameters());
setLocalDiscriminator(FD);

// Check to see if we have a "{" to start a brace statement.
if (Flags.contains(PD_InProtocol)) {
if (Tok.is(tok::l_brace)) {
// Record the curly braces but nothing inside.
SF.recordInterfaceToken("{");
SF.recordInterfaceToken("}");
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);

if (Flags.contains(PD_InProtocol)) {
diagnose(Tok, diag::protocol_method_with_body);
skipUntilDeclRBrace();
} else if (!isDelayedParsingEnabled()) {
if (Context.Stats)
Context.Stats->getFrontendCounters().NumFunctionsParsed++;

ParserResult<BraceStmt> Body =
parseBraceItemList(diag::func_decl_without_brace);
if (Body.isNull()) {
// FIXME: Should do some sort of error recovery here?
} else if (SignatureStatus.hasCodeCompletion()) {
// Code completion was inside the signature, don't attach the body.
FD->setBodySkipped(Body.get()->getSourceRange());
} else {
FD->setBody(Body.get());
}
} else {
consumeAbstractFunctionBody(FD, Attributes);
}
} else {
checkForInputIncomplete();
diagnose(Tok, diag::protocol_method_with_body);
skipSingle();
}
} else {
parseAbstractFunctionBody(FD);
}

// Exit the scope introduced for the generic parameters.
Expand All @@ -5371,6 +5334,46 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
return DCC.fixupParserResult(FD);
}

/// Parse function body into \p AFD.
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 (!Tok.is(tok::l_brace)) {
checkForInputIncomplete();
return;
}

if (IsParsingInterfaceTokens) {
// Record the curly braces but nothing inside.
SF.recordInterfaceToken("{");
SF.recordInterfaceToken("}");
}
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);

if (isDelayedParsingEnabled()) {
consumeAbstractFunctionBody(AFD, AFD->getAttrs());
return;
}

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

ParserResult<BraceStmt> Body = parseBraceItemList(diag::invalid_diagnostic);
if (!Body.isNull())
AFD->setBody(Body.get());
}

bool Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
assert(!AFD->getBody() && "function should not have a parsed body");
assert(AFD->getBodyKind() == AbstractFunctionDecl::BodyKind::Unparsed &&
Expand Down Expand Up @@ -6225,7 +6228,6 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {

CD->setGenericParams(GenericParams);

Scope S2(this, ScopeKind::ConstructorBody);
CtorInitializerKind initKind = CtorInitializerKind::Designated;
if (Attributes.hasAttribute<ConvenienceAttr>())
initKind = CtorInitializerKind::Convenience;
Expand All @@ -6244,37 +6246,13 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
CD->setInvalid();
}

addToScope(CD->getImplicitSelfDecl());
addParametersToScope(Params.get());

// '{'
if (Tok.is(tok::l_brace)) {
// Record the curly braces but nothing inside.
SF.recordInterfaceToken("{");
SF.recordInterfaceToken("}");
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);

if (Flags.contains(PD_InProtocol)) {
if (Flags.contains(PD_InProtocol)) {
if (Tok.is(tok::l_brace)) {
diagnose(Tok, diag::protocol_init_with_body);
skipUntilDeclRBrace();
} else {
// Parse the body.
ParseFunctionBody CC(*this, CD);
setLocalDiscriminatorToParamList(CD->getParameters());

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

ParserResult<BraceStmt> Body =
parseBraceItemList(diag::invalid_diagnostic);

if (!Body.isNull())
CD->setBody(Body.get());
} else {
consumeAbstractFunctionBody(CD, Attributes);
}
skipSingle();
}
} else {
parseAbstractFunctionBody(CD);
}

CD->getAttrs() = Attributes;
Expand Down Expand Up @@ -6327,30 +6305,8 @@ parseDeclDeinit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
}
}

Scope S(this, ScopeKind::DestructorBody);
auto *DD = new (Context) DestructorDecl(DestructorLoc, CurDeclContext);

// Parse the body.
if (Tok.is(tok::l_brace)) {
// Record the curly braces but nothing inside.
SF.recordInterfaceToken("{");
SF.recordInterfaceToken("}");
llvm::SaveAndRestore<bool> T(IsParsingInterfaceTokens, false);

ParseFunctionBody CC(*this, DD);
if (!isDelayedParsingEnabled()) {
if (Context.Stats)
Context.Stats->getFrontendCounters().NumFunctionsParsed++;

ParserResult<BraceStmt> Body =
parseBraceItemList(diag::invalid_diagnostic);

if (!Body.isNull())
DD->setBody(Body.get());
} else {
consumeAbstractFunctionBody(DD, Attributes);
}
}
parseAbstractFunctionBody(DD);

DD->getAttrs() = Attributes;

Expand Down
3 changes: 0 additions & 3 deletions lib/Parse/Scope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ static bool isResolvableScope(ScopeKind SK) {
return false;
case ScopeKind::FunctionBody:
case ScopeKind::Generics:
case ScopeKind::ConstructorBody:
case ScopeKind::DestructorBody:
case ScopeKind::Brace:
case ScopeKind::ForVars:
case ScopeKind::ForeachVars:
case ScopeKind::ClosureParams:
case ScopeKind::CaseVars:
Expand Down
18 changes: 18 additions & 0 deletions test/InterfaceHash/added_localfunc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s
// RUN: %target-swift-frontend -dump-interface-hash %t/a.swift 2> %t/a.hash
// RUN: %target-swift-frontend -dump-interface-hash %t/b.swift 2> %t/b.hash
// RUN: cmp %t/a.hash %t/b.hash

// BEGIN a.swift
func test() -> Int {
return 0
}

// BEGIN b.swift
func test() -> Int {
func inner() -> Int{
return 0
}
return inner()
}