|
33 | 33 | #include "swift/AST/NameLookup.h"
|
34 | 34 | #include "swift/AST/NameLookupRequests.h"
|
35 | 35 | #include "swift/AST/ParameterList.h"
|
| 36 | +#include "swift/AST/ParseRequests.h" |
36 | 37 | #include "swift/AST/Pattern.h"
|
37 | 38 | #include "swift/AST/PropertyWrappers.h"
|
38 | 39 | #include "swift/AST/ProtocolConformance.h"
|
@@ -6307,37 +6308,22 @@ bool AbstractFunctionDecl::argumentNameIsAPIByDefault() const {
|
6307 | 6308 | }
|
6308 | 6309 |
|
6309 | 6310 | BraceStmt *AbstractFunctionDecl::getBody(bool canSynthesize) const {
|
6310 |
| - switch (getBodyKind()) { |
6311 |
| - case BodyKind::Deserialized: |
6312 |
| - case BodyKind::MemberwiseInitializer: |
6313 |
| - case BodyKind::None: |
6314 |
| - case BodyKind::Skipped: |
| 6311 | + if ((getBodyKind() == BodyKind::Synthesize || |
| 6312 | + getBodyKind() == BodyKind::Unparsed) && |
| 6313 | + !canSynthesize) |
6315 | 6314 | return nullptr;
|
6316 | 6315 |
|
6317 |
| - case BodyKind::Parsed: |
6318 |
| - case BodyKind::TypeChecked: |
6319 |
| - return Body; |
| 6316 | + ASTContext &ctx = getASTContext(); |
6320 | 6317 |
|
6321 |
| - case BodyKind::Unparsed: |
6322 |
| - // FIXME: Go parse now! |
| 6318 | + // Don't allow getBody() to trigger parsing of an unparsed body containing the |
| 6319 | + // code completion location. |
| 6320 | + if (getBodyKind() == BodyKind::Unparsed && |
| 6321 | + ctx.SourceMgr.rangeContainsCodeCompletionLoc(getBodySourceRange())) { |
6323 | 6322 | return nullptr;
|
6324 |
| - |
6325 |
| - case BodyKind::Synthesize: { |
6326 |
| - if (!canSynthesize) |
6327 |
| - return nullptr; |
6328 |
| - |
6329 |
| - const_cast<AbstractFunctionDecl *>(this)->setBodyKind(BodyKind::None); |
6330 |
| - BraceStmt *body; |
6331 |
| - bool isTypeChecked; |
6332 |
| - |
6333 |
| - auto mutableThis = const_cast<AbstractFunctionDecl *>(this); |
6334 |
| - std::tie(body, isTypeChecked) = (Synthesizer.Fn)( |
6335 |
| - mutableThis, Synthesizer.Context); |
6336 |
| - mutableThis->setBody( |
6337 |
| - body, isTypeChecked ? BodyKind::TypeChecked : BodyKind::Parsed); |
6338 |
| - return body; |
6339 |
| - } |
6340 | 6323 | }
|
| 6324 | + |
| 6325 | + auto mutableThis = const_cast<AbstractFunctionDecl *>(this); |
| 6326 | + return evaluateOrDefault(ctx.evaluator, ParseAbstractFunctionBodyRequest{mutableThis}, nullptr); |
6341 | 6327 | }
|
6342 | 6328 |
|
6343 | 6329 | SourceRange AbstractFunctionDecl::getBodySourceRange() const {
|
@@ -6804,11 +6790,15 @@ bool AbstractFunctionDecl::hasInlinableBodyText() const {
|
6804 | 6790 | switch (getBodyKind()) {
|
6805 | 6791 | case BodyKind::Deserialized:
|
6806 | 6792 | return true;
|
| 6793 | + |
| 6794 | + case BodyKind::Unparsed: |
6807 | 6795 | case BodyKind::Parsed:
|
6808 | 6796 | case BodyKind::TypeChecked:
|
6809 |
| - return getBody() && !getBody()->isImplicit(); |
| 6797 | + if (auto body = getBody()) |
| 6798 | + return !body->isImplicit(); |
| 6799 | + return false; |
| 6800 | + |
6810 | 6801 | case BodyKind::None:
|
6811 |
| - case BodyKind::Unparsed: |
6812 | 6802 | case BodyKind::Synthesize:
|
6813 | 6803 | case BodyKind::Skipped:
|
6814 | 6804 | case BodyKind::MemberwiseInitializer:
|
@@ -7676,3 +7666,49 @@ SourceLoc swift::extractNearestSourceLoc(const Decl *decl) {
|
7676 | 7666 |
|
7677 | 7667 | return extractNearestSourceLoc(decl->getDeclContext());
|
7678 | 7668 | }
|
| 7669 | + |
| 7670 | +Optional<BraceStmt *> |
| 7671 | +ParseAbstractFunctionBodyRequest::getCachedResult() const { |
| 7672 | + using BodyKind = AbstractFunctionDecl::BodyKind; |
| 7673 | + auto afd = std::get<0>(getStorage()); |
| 7674 | + switch (afd->getBodyKind()) { |
| 7675 | + case BodyKind::Deserialized: |
| 7676 | + case BodyKind::MemberwiseInitializer: |
| 7677 | + case BodyKind::None: |
| 7678 | + case BodyKind::Skipped: |
| 7679 | + return nullptr; |
| 7680 | + |
| 7681 | + case BodyKind::TypeChecked: |
| 7682 | + case BodyKind::Parsed: |
| 7683 | + return afd->Body; |
| 7684 | + |
| 7685 | + case BodyKind::Synthesize: |
| 7686 | + case BodyKind::Unparsed: |
| 7687 | + return None; |
| 7688 | + } |
| 7689 | +} |
| 7690 | + |
| 7691 | +void ParseAbstractFunctionBodyRequest::cacheResult(BraceStmt *value) const { |
| 7692 | + using BodyKind = AbstractFunctionDecl::BodyKind; |
| 7693 | + auto afd = std::get<0>(getStorage()); |
| 7694 | + switch (afd->getBodyKind()) { |
| 7695 | + case BodyKind::Deserialized: |
| 7696 | + case BodyKind::MemberwiseInitializer: |
| 7697 | + case BodyKind::None: |
| 7698 | + case BodyKind::Skipped: |
| 7699 | + // The body is always empty, so don't cache anything. |
| 7700 | + assert(value == nullptr); |
| 7701 | + return; |
| 7702 | + |
| 7703 | + case BodyKind::Parsed: |
| 7704 | + case BodyKind::TypeChecked: |
| 7705 | + afd->Body = value; |
| 7706 | + return; |
| 7707 | + |
| 7708 | + case BodyKind::Synthesize: |
| 7709 | + case BodyKind::Unparsed: |
| 7710 | + llvm_unreachable("evaluate() did not set the body kind"); |
| 7711 | + return; |
| 7712 | + } |
| 7713 | + |
| 7714 | +} |
0 commit comments