Skip to content

More lazy body synthesis #26105

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 13 commits into from
Jul 12, 2019
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
24 changes: 9 additions & 15 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5615,7 +5615,7 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
}

struct BodySynthesizer {
void (* Fn)(AbstractFunctionDecl *, void *);
std::pair<BraceStmt *, bool> (* Fn)(AbstractFunctionDecl *, void *);
void *Context;
};

Expand Down Expand Up @@ -5748,18 +5748,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
/// have a body for this function.
///
/// \sa hasBody()
BraceStmt *getBody(bool canSynthesize = true) const {
if (canSynthesize && getBodyKind() == BodyKind::Synthesize) {
const_cast<AbstractFunctionDecl *>(this)->setBodyKind(BodyKind::None);
(Synthesizer.Fn)(const_cast<AbstractFunctionDecl *>(this),
Synthesizer.Context);
}
if (getBodyKind() == BodyKind::Parsed ||
getBodyKind() == BodyKind::TypeChecked) {
return Body;
}
return nullptr;
}
BraceStmt *getBody(bool canSynthesize = true) const;

void setBody(BraceStmt *S, BodyKind NewBodyKind = BodyKind::Parsed) {
assert(getBodyKind() != BodyKind::Skipped &&
"cannot set a body if it was skipped");
Expand All @@ -5784,8 +5774,12 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
}

/// Note that parsing for the body was delayed.
void setBodySynthesizer(void (* fn)(AbstractFunctionDecl *, void *),
void *context = nullptr) {
///
/// The function should return the body statement and a flag indicating
/// whether that body is already type-checked.
void setBodySynthesizer(
std::pair<BraceStmt *, bool> (* fn)(AbstractFunctionDecl *, void *),
void *context = nullptr) {
assert(getBodyKind() == BodyKind::None);
Synthesizer = {fn, context};
setBodyKind(BodyKind::Synthesize);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTScopeSourceRange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ SourceRange AbstractFunctionDeclScope::getChildlessSourceRange() const {
assert(r.End.isValid());
return r;
}
return decl->getBody()->getSourceRange();
return decl->getBodySourceRange();
}

SourceRange AbstractFunctionParamsScope::getChildlessSourceRange() const {
Expand Down
64 changes: 45 additions & 19 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6236,17 +6236,51 @@ bool AbstractFunctionDecl::argumentNameIsAPIByDefault() const {
return false;
}

BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
switch (getBodyKind()) {
case BodyKind::Deserialized:
case BodyKind::MemberwiseInitializer:
case BodyKind::None:
case BodyKind::Skipped:
return nullptr;

case BodyKind::Parsed:
case BodyKind::TypeChecked:
return Body;

case BodyKind::Unparsed:
// FIXME: Go parse now!
return nullptr;

case BodyKind::Synthesize: {
if (!canSynthesize)
return nullptr;

const_cast<AbstractFunctionDecl *>(this)->setBodyKind(BodyKind::None);
BraceStmt *body;
bool isTypeChecked;

auto mutableThis = const_cast<AbstractFunctionDecl *>(this);
std::tie(body, isTypeChecked) = (Synthesizer.Fn)(
mutableThis, Synthesizer.Context);
mutableThis->setBody(
body, isTypeChecked ? BodyKind::TypeChecked : BodyKind::Parsed);
return body;
}
}
}

SourceRange AbstractFunctionDecl::getBodySourceRange() const {
switch (getBodyKind()) {
case BodyKind::None:
case BodyKind::MemberwiseInitializer:
case BodyKind::Deserialized:
case BodyKind::Synthesize:
return SourceRange();

case BodyKind::Parsed:
case BodyKind::Synthesize:
case BodyKind::TypeChecked:
if (auto body = getBody())
if (auto body = getBody(/*canSynthesize=*/false))
return body->getSourceRange();

return SourceRange();
Expand Down Expand Up @@ -6968,9 +7002,9 @@ SourceRange FuncDecl::getSourceRange() const {
getBodyKind() == BodyKind::Skipped)
return { StartLoc, BodyRange.End };

if (auto *B = getBody(/*canSynthesize=*/false)) {
if (!B->isImplicit())
return { StartLoc, B->getEndLoc() };
SourceLoc RBraceLoc = getBodySourceRange().End;
if (RBraceLoc.isValid()) {
return { StartLoc, RBraceLoc };
}

if (isa<AccessorDecl>(this))
Expand Down Expand Up @@ -7069,13 +7103,7 @@ SourceRange ConstructorDecl::getSourceRange() const {
if (isImplicit())
return getConstructorLoc();

if (getBodyKind() == BodyKind::Unparsed ||
getBodyKind() == BodyKind::Skipped)
return { getConstructorLoc(), BodyRange.End };

SourceLoc End;
if (auto body = getBody())
End = body->getEndLoc();
SourceLoc End = getBodySourceRange().End;
if (End.isInvalid())
End = getGenericTrailingWhereClauseSourceRange().End;
if (End.isInvalid())
Expand Down Expand Up @@ -7283,14 +7311,12 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
}

SourceRange DestructorDecl::getSourceRange() const {
if (getBodyKind() == BodyKind::Unparsed ||
getBodyKind() == BodyKind::Skipped)
return { getDestructorLoc(), BodyRange.End };

if (getBodyKind() == BodyKind::None)
return getDestructorLoc();
SourceLoc End = getBodySourceRange().End;
if (End.isInvalid()) {
End = getDestructorLoc();
}

return { getDestructorLoc(), getBody()->getEndLoc() };
return { getDestructorLoc(), End };
}

StringRef swift::getAssociativitySpelling(Associativity value) {
Expand Down
Loading