Skip to content

Commit 7cb809b

Browse files
committed
ASTScope: Remove old implementation of 'selfDC' computation
1 parent ed17c33 commit 7cb809b

File tree

6 files changed

+5
-190
lines changed

6 files changed

+5
-190
lines changed

include/swift/AST/ASTScope.h

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -474,16 +474,6 @@ class ASTScopeImpl {
474474
NullablePtr<const GenericParamList> lastListSearched,
475475
DeclConsumer consumer) const;
476476

477-
public:
478-
/// Returns the SelfDC for parent (and possibly ancestor) scopes.
479-
/// A return of None indicates that the previous child (in history) should be
480-
/// asked.
481-
virtual Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const;
482-
483-
/// Returns the context that should be used when a nested scope (e.g. a
484-
/// closure) captures self explicitly.
485-
virtual NullablePtr<DeclContext> capturedSelfDC() const;
486-
487477
protected:
488478
/// Find either locals or members (no scope has both)
489479
/// \param history The scopes visited since the start of lookup (including
@@ -691,27 +681,6 @@ class Portion {
691681
bool lookupMembersOf(const GenericTypeOrExtensionScope *scope,
692682
ArrayRef<const ASTScopeImpl *>,
693683
ASTScopeImpl::DeclConsumer consumer) const override;
694-
695-
private:
696-
/// A client needs to know if a lookup result required the dynamic implicit
697-
/// self value. It is required if the lookup originates from a method body
698-
/// or a lazy pattern initializer. So, one approach would be to call the
699-
/// consumer to find members right from those scopes. However, because
700-
/// members aren't the first things searched, generics are, that approache
701-
/// ends up duplicating code from the \c GenericTypeOrExtensionScope. So we
702-
/// take the approach of doing those lookups there, and using this function
703-
/// to compute the selfDC from the history.
704-
static NullablePtr<DeclContext>
705-
computeSelfDC(ArrayRef<const ASTScopeImpl *> history);
706-
707-
/// If we find a lookup result that requires the dynamic implict self value,
708-
/// we need to check the nested scopes to see if any closures explicitly
709-
/// captured \c self. In that case, the appropriate selfDC is that of the
710-
/// innermost closure which captures a \c self value from one of this type's
711-
/// methods.
712-
static NullablePtr<DeclContext>
713-
checkNestedScopesForSelfCapture(ArrayRef<const ASTScopeImpl *> history,
714-
size_t start);
715684
};
716685

717686
/// Behavior specific to representing the trailing where clause of a
@@ -810,7 +779,6 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
810779
virtual std::string declKindName() const = 0;
811780
virtual bool doesDeclHaveABody() const;
812781
const char *portionName() const { return portion->portionName; }
813-
Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const override;
814782

815783
protected:
816784
Optional<bool> resolveIsCascadingUseForThisScope(
@@ -1135,7 +1103,6 @@ class FunctionBodyScope final : public AbstractFunctionBodyScope {
11351103
std::string getClassName() const override;
11361104
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
11371105
DeclConsumer consumer) const override;
1138-
Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const override;
11391106
};
11401107

11411108
class DefaultArgumentInitializerScope final : public ASTScopeImpl {
@@ -1304,8 +1271,6 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
13041271
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
13051272
virtual NullablePtr<DeclContext> getDeclContext() const override;
13061273

1307-
Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const override;
1308-
13091274
protected:
13101275
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
13111276
DeclConsumer) const override;
@@ -1417,13 +1382,6 @@ class ClosureParametersScope final : public ASTScopeImpl {
14171382
std::string getClassName() const override;
14181383
SourceRange
14191384
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
1420-
1421-
/// Since explicit captures of \c self by closures enable the use of implicit
1422-
/// \c self, we need to make sure that the appropriate \c self is used as the
1423-
/// base decl for these uses (otherwise, the capture would be marked as
1424-
/// unused. \c ClosureParametersScope::capturedSelfDC() checks if we have such
1425-
/// a capture of self.
1426-
NullablePtr<DeclContext> capturedSelfDC() const override;
14271385

14281386
NullablePtr<ClosureExpr> getClosureIfClosureScope() const override {
14291387
return closureExpr;

include/swift/AST/Expr.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3941,12 +3941,6 @@ class ClosureExpr : public AbstractClosureExpr {
39413941
VarDecl *getCapturedSelfDecl() const {
39423942
return CapturedSelfDecl;
39433943
}
3944-
3945-
/// Whether this closure captures the \c self param in its body in such a
3946-
/// way that implicit \c self is enabled within its body (i.e. \c self is
3947-
/// captured non-weakly).
3948-
bool capturesSelfEnablingImplictSelf() const;
3949-
39503944

39513945
/// Get the type checking state of this closure's body.
39523946
BodyState getBodyState() const {

include/swift/AST/NameLookup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ class AbstractASTScopeDeclConsumer {
633633

634634
/// Eventually this functionality should move into ASTScopeLookup
635635
virtual bool
636-
lookInMembers(NullablePtr<DeclContext> selfDC, DeclContext *const scopeDC,
636+
lookInMembers(DeclContext *const scopeDC,
637637
NominalTypeDecl *const nominal,
638638
function_ref<bool(Optional<bool>)> calculateIsCascadingUse) = 0;
639639

@@ -656,7 +656,7 @@ class ASTScopeDeclGatherer : public AbstractASTScopeDeclConsumer {
656656
NullablePtr<DeclContext> baseDC = nullptr) override;
657657

658658
/// Eventually this functionality should move into ASTScopeLookup
659-
bool lookInMembers(NullablePtr<DeclContext>, DeclContext *const,
659+
bool lookInMembers(DeclContext *const,
660660
NominalTypeDecl *const,
661661
function_ref<bool(Optional<bool>)>) override {
662662
return false;

lib/AST/ASTScopeLookup.cpp

Lines changed: 1 addition & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ bool GenericTypeOrExtensionWhereOrBodyPortion::lookupMembersOf(
264264
auto nt = scope->getCorrespondingNominalTypeDecl().getPtrOrNull();
265265
if (!nt)
266266
return false;
267-
auto selfDC = computeSelfDC(history);
268-
return consumer.lookInMembers(selfDC, scope->getDeclContext().get(), nt,
267+
return consumer.lookInMembers(scope->getDeclContext().get(), nt,
269268
[&](Optional<bool> initialIsCascadingUse) {
270269
return ASTScopeImpl::computeIsCascadingUse(
271270
history, initialIsCascadingUse)
@@ -456,69 +455,6 @@ bool ASTScopeImpl::lookupLocalBindingsInPattern(const Pattern *p,
456455
return isDone;
457456
}
458457

459-
#pragma mark computeSelfDC
460-
461-
NullablePtr<DeclContext>
462-
GenericTypeOrExtensionWhereOrBodyPortion::computeSelfDC(
463-
ArrayRef<const ASTScopeImpl *> history) {
464-
ASTScopeAssert(history.size() != 0, "includes current scope");
465-
size_t i = history.size() - 1; // skip last entry (this scope)
466-
while (i != 0) {
467-
Optional<NullablePtr<DeclContext>> maybeSelfDC =
468-
history[--i]->computeSelfDCForParent();
469-
if (maybeSelfDC) {
470-
// If we've found a selfDC, we'll definitely be returning something.
471-
// However, we may have captured 'self' somewhere down the tree, so we
472-
// can't return outright without checking the nested scopes.
473-
NullablePtr<DeclContext> nestedCapturedSelfDC =
474-
checkNestedScopesForSelfCapture(history, i);
475-
return nestedCapturedSelfDC ? nestedCapturedSelfDC : *maybeSelfDC;
476-
}
477-
}
478-
return nullptr;
479-
}
480-
481-
#pragma mark checkNestedScopesForSelfCapture
482-
483-
NullablePtr<DeclContext>
484-
GenericTypeOrExtensionWhereOrBodyPortion::checkNestedScopesForSelfCapture(
485-
ArrayRef<const ASTScopeImpl *> history, size_t start) {
486-
NullablePtr<DeclContext> innerCapturedSelfDC;
487-
// Start with the next scope down the tree.
488-
size_t j = start;
489-
490-
// Note: even though having this loop nested inside the while loop from
491-
// GenericTypeOrExtensionWhereOrBodyPortion::computeSelfDC may appear to
492-
// result in quadratic blowup, complexity actually remains linear with respect
493-
// to the size of history. This relies on the fact that
494-
// GenericTypeOrExtensionScope::computeSelfDCForParent returns a null pointer,
495-
// which will cause this method to bail out of the search early. Thus, this
496-
// method is called once per type body in the lookup history, and will not
497-
// end up re-checking the bodies of nested types that have already been
498-
// covered by earlier calls, so the total impact of this method across all
499-
// calls in a single lookup is O(n).
500-
while (j != 0) {
501-
auto *entry = history[--j];
502-
Optional<NullablePtr<DeclContext>> selfDCForParent =
503-
entry->computeSelfDCForParent();
504-
505-
// If we encounter a scope that should cause us to forget the self
506-
// context (such as a nested type), bail out and use whatever the
507-
// the last inner captured context was.
508-
if (selfDCForParent && (*selfDCForParent).isNull())
509-
break;
510-
511-
// Otherwise, if we have a captured self context for this scope, then
512-
// remember it since it is now the innermost scope we have encountered.
513-
NullablePtr<DeclContext> capturedSelfDC = entry->capturedSelfDC();
514-
if (!capturedSelfDC.isNull())
515-
innerCapturedSelfDC = entry->capturedSelfDC();
516-
517-
// Continue searching in the next scope down.
518-
}
519-
return innerCapturedSelfDC;
520-
}
521-
522458
#pragma mark compute isCascadingUse
523459

524460
Optional<bool> ASTScopeImpl::computeIsCascadingUse(
@@ -587,65 +523,6 @@ NullablePtr<const ASTScopeImpl> ASTScopeImpl::ancestorWithDeclSatisfying(
587523
return nullptr;
588524
}
589525

590-
#pragma mark computeSelfDCForParent
591-
592-
// If the lookup depends on implicit self, selfDC is its context.
593-
// (Names in extensions never depend on self.)
594-
// Lookup can propagate it up from, say a method to the enclosing type body.
595-
596-
// By default, propagate the selfDC up to a NomExt decl, body,
597-
// or where clause
598-
Optional<NullablePtr<DeclContext>>
599-
ASTScopeImpl::computeSelfDCForParent() const {
600-
return None;
601-
}
602-
603-
// Forget the "self" declaration:
604-
Optional<NullablePtr<DeclContext>>
605-
GenericTypeOrExtensionScope::computeSelfDCForParent() const {
606-
return NullablePtr<DeclContext>();
607-
}
608-
609-
Optional<NullablePtr<DeclContext>>
610-
PatternEntryInitializerScope::computeSelfDCForParent() const {
611-
// Pattern binding initializers are only interesting insofar as they
612-
// affect lookup in an enclosing nominal type or extension thereof.
613-
if (auto *ic = getPatternEntry().getInitContext()) {
614-
if (auto *bindingInit = dyn_cast<PatternBindingInitializer>(ic)) {
615-
// Lazy variable initializer contexts have a 'self' parameter for
616-
// instance member lookup.
617-
if (bindingInit->getImplicitSelfDecl()) {
618-
return NullablePtr<DeclContext>(bindingInit);
619-
}
620-
}
621-
}
622-
return None;
623-
}
624-
625-
Optional<NullablePtr<DeclContext>>
626-
FunctionBodyScope::computeSelfDCForParent() const {
627-
if (decl->getDeclContext()->isTypeContext())
628-
return NullablePtr<DeclContext>(decl);
629-
return None;
630-
}
631-
632-
#pragma mark capturedSelfDC
633-
634-
// Closures may explicitly capture the self param, in which case the lookup
635-
// should use the closure as the context for implicit self lookups.
636-
637-
// By default, there is no such context to return.
638-
NullablePtr<DeclContext> ASTScopeImpl::capturedSelfDC() const {
639-
return NullablePtr<DeclContext>();
640-
}
641-
642-
// Closures track this information explicitly.
643-
NullablePtr<DeclContext> ClosureParametersScope::capturedSelfDC() const {
644-
if (closureExpr->capturesSelfEnablingImplictSelf())
645-
return NullablePtr<DeclContext>(closureExpr);
646-
return NullablePtr<DeclContext>();
647-
}
648-
649526
#pragma mark ifUnknownIsCascadingUseAccordingTo
650527

651528
static bool isCascadingUseAccordingTo(const DeclContext *const dc) {

lib/AST/Expr.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,19 +1955,6 @@ bool ClosureExpr::hasEmptyBody() const {
19551955
return getBody()->empty();
19561956
}
19571957

1958-
bool ClosureExpr::capturesSelfEnablingImplictSelf() const {
1959-
if (auto *VD = getCapturedSelfDecl()) {
1960-
if (!VD->isSelfParamCapture())
1961-
return false;
1962-
1963-
if (auto *attr = VD->getAttrs().getAttribute<ReferenceOwnershipAttr>())
1964-
return attr->get() != ReferenceOwnership::Weak;
1965-
1966-
return true;
1967-
}
1968-
return false;
1969-
}
1970-
19711958
void ClosureExpr::setExplicitResultType(Type ty) {
19721959
assert(ty && !ty->hasTypeVariable() && !ty->hasHole());
19731960
ExplicitResultTypeAndBodyState.getPointer()

lib/AST/UnqualifiedLookup.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ class ASTScopeDeclConsumerForUnqualifiedLookup
248248
NullablePtr<DeclContext> baseDC = nullptr) override;
249249

250250
/// returns true if finished and new value for isCascadingUse
251-
bool lookInMembers(NullablePtr<DeclContext> selfDC,
252-
DeclContext *const scopeDC, NominalTypeDecl *const nominal,
251+
bool lookInMembers(DeclContext *const scopeDC, NominalTypeDecl *const nominal,
253252
function_ref<bool(Optional<bool>)>) override;
254253

255254
#ifndef NDEBUG
@@ -642,7 +641,7 @@ bool ASTScopeDeclGatherer::consume(ArrayRef<ValueDecl *> valuesArg,
642641

643642
// TODO: in future, migrate this functionality into ASTScopes
644643
bool ASTScopeDeclConsumerForUnqualifiedLookup::lookInMembers(
645-
NullablePtr<DeclContext> selfDC, DeclContext *const scopeDC,
644+
DeclContext *const scopeDC,
646645
NominalTypeDecl *const nominal,
647646
function_ref<bool(Optional<bool>)> calculateIsCascadingUse) {
648647
if (candidateSelfDC) {

0 commit comments

Comments
 (0)