Skip to content

Commit d93c6d8

Browse files
committed
Remove Cascading Computations from ASTScope
1 parent fe5dacb commit d93c6d8

File tree

6 files changed

+111
-205
lines changed

6 files changed

+111
-205
lines changed

include/swift/AST/ASTScope.h

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,6 @@ class ASTScopeImpl {
417417
static llvm::SmallVector<LabeledStmt *, 4>
418418
lookupLabeledStmts(SourceFile *sourceFile, SourceLoc loc);
419419

420-
static Optional<bool>
421-
computeIsCascadingUse(ArrayRef<const ASTScopeImpl *> history,
422-
Optional<bool> initialIsCascadingUse);
423-
424420
static std::pair<CaseStmt *, CaseStmt *>
425421
lookupFallthroughSourceAndDest(SourceFile *sourceFile, SourceLoc loc);
426422

@@ -448,7 +444,6 @@ class ASTScopeImpl {
448444
/// The main (recursive) lookup function:
449445
/// Tell DeclConsumer about all names found in this scope and if not done,
450446
/// recurse for enclosing scopes. Stop lookup if about to look in limit.
451-
/// Return final value for isCascadingUse
452447
///
453448
/// If the lookup depends on implicit self, selfDC is its context.
454449
/// (Names in extensions never depend on self.)
@@ -508,9 +503,6 @@ class ASTScopeImpl {
508503

509504
#pragma mark - - lookup- local bindings
510505
protected:
511-
virtual Optional<bool>
512-
resolveIsCascadingUseForThisScope(Optional<bool>) const;
513-
514506
// A local binding is a basically a local variable defined in that very scope
515507
// It is not an instance variable or inherited type.
516508

@@ -611,11 +603,11 @@ class Portion {
611603
virtual ASTScopeImpl *expandScope(GenericTypeOrExtensionScope *,
612604
ScopeCreator &) const = 0;
613605

606+
/// \Returns \c true if this lookup is done looking for results, else \c false.
614607
virtual SourceRange
615608
getChildlessSourceRangeOf(const GenericTypeOrExtensionScope *scope,
616609
bool omitAssertions) const = 0;
617610

618-
/// Returns isDone and isCascadingUse
619611
virtual bool lookupMembersOf(const GenericTypeOrExtensionScope *scope,
620612
ArrayRef<const ASTScopeImpl *>,
621613
ASTScopeImpl::DeclConsumer consumer) const;
@@ -780,10 +772,6 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
780772
virtual bool doesDeclHaveABody() const;
781773
const char *portionName() const { return portion->portionName; }
782774

783-
protected:
784-
Optional<bool> resolveIsCascadingUseForThisScope(
785-
Optional<bool> isCascadingUse) const override;
786-
787775
public:
788776
// Only for DeclScope, not BodyScope
789777
// Returns the where clause scope, or the parent if none
@@ -969,8 +957,7 @@ class GenericParamScope final : public ASTScopeImpl {
969957
protected:
970958
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
971959
DeclConsumer) const override;
972-
Optional<bool>
973-
resolveIsCascadingUseForThisScope(Optional<bool>) const override;
960+
bool doesContextMatchStartingContext(const DeclContext *) const override;
974961
};
975962

976963
/// Concrete class for a function/initializer/deinitializer
@@ -1014,9 +1001,6 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
10141001

10151002
protected:
10161003
NullablePtr<const GenericParamList> genericParams() const override;
1017-
1018-
Optional<bool>
1019-
resolveIsCascadingUseForThisScope(Optional<bool>) const override;
10201004
};
10211005

10221006
/// The parameters for an abstract function (init/func/deinit)., subscript, and
@@ -1087,8 +1071,6 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
10871071
protected:
10881072
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
10891073
DeclConsumer) const override;
1090-
Optional<bool>
1091-
resolveIsCascadingUseForThisScope(Optional<bool>) const override;
10921074

10931075
public:
10941076
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion() override;
@@ -1123,10 +1105,6 @@ class DefaultArgumentInitializerScope final : public ASTScopeImpl {
11231105
virtual NullablePtr<DeclContext> getDeclContext() const override;
11241106
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
11251107
Decl *getDecl() const { return decl; }
1126-
1127-
protected:
1128-
Optional<bool>
1129-
resolveIsCascadingUseForThisScope(Optional<bool>) const override;
11301108
};
11311109

11321110
/// Consider:
@@ -1274,9 +1252,6 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
12741252
protected:
12751253
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
12761254
DeclConsumer) const override;
1277-
1278-
Optional<bool>
1279-
resolveIsCascadingUseForThisScope(Optional<bool>) const override;
12801255
};
12811256

12821257
/// The scope introduced by a conditional clause in an if/guard/while
@@ -1392,6 +1367,42 @@ class ClosureParametersScope final : public ASTScopeImpl {
13921367
NullablePtr<Expr> getExprIfAny() const override { return closureExpr; }
13931368
Expr *getExpr() const { return closureExpr; }
13941369
NullablePtr<const void> getReferrent() const override;
1370+
};
1371+
1372+
/// For a closure with named parameters, this scope does the local bindings.
1373+
/// Absent if no "in".
1374+
class ClosureParametersScope final : public AbstractClosureScope {
1375+
public:
1376+
ClosureParametersScope(ClosureExpr *closureExpr,
1377+
NullablePtr<CaptureListExpr> captureList)
1378+
: AbstractClosureScope(closureExpr, captureList) {}
1379+
virtual ~ClosureParametersScope() {}
1380+
1381+
std::string getClassName() const override;
1382+
SourceRange
1383+
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
1384+
1385+
/// Since explicit captures of \c self by closures enable the use of implicit
1386+
/// \c self, we need to make sure that the appropriate \c self is used as the
1387+
/// base decl for these uses (otherwise, the capture would be marked as
1388+
/// unused. \c ClosureParametersScope::capturedSelfDC() checks if we have such
1389+
/// a capture of self.
1390+
NullablePtr<DeclContext> capturedSelfDC() const override;
1391+
1392+
protected:
1393+
ASTScopeImpl *expandSpecifically(ScopeCreator &) override;
1394+
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
1395+
DeclConsumer) const override;
1396+
};
1397+
1398+
// The body encompasses the code in the closure; the part after the "in" if
1399+
// there is an "in"
1400+
class ClosureBodyScope final : public AbstractClosureScope {
1401+
public:
1402+
ClosureBodyScope(ClosureExpr *closureExpr,
1403+
NullablePtr<CaptureListExpr> captureList)
1404+
: AbstractClosureScope(closureExpr, captureList) {}
1405+
virtual ~ClosureBodyScope() {}
13951406

13961407
protected:
13971408
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
@@ -1402,8 +1413,10 @@ class ClosureParametersScope final : public ASTScopeImpl {
14021413
protected:
14031414
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
14041415
DeclConsumer) const override;
1405-
Optional<bool> resolveIsCascadingUseForThisScope(
1406-
Optional<bool> isCascadingUse) const override;
1416+
public:
1417+
std::string getClassName() const override;
1418+
SourceRange
1419+
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
14071420
};
14081421

14091422
class TopLevelCodeScope final : public ASTScopeImpl {

include/swift/AST/NameLookup.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -505,9 +505,6 @@ template <typename Result>
505505
void filterForDiscriminator(SmallVectorImpl<Result> &results,
506506
DebuggerClient *debugClient);
507507

508-
void recordLookupOfTopLevelName(DeclContext *topLevelContext, DeclName name,
509-
bool isCascading);
510-
511508
} // end namespace namelookup
512509

513510
/// Retrieve the set of nominal type declarations that are directly
@@ -632,8 +629,7 @@ class AbstractASTScopeDeclConsumer {
632629
/// Eventually this functionality should move into ASTScopeLookup
633630
virtual bool
634631
lookInMembers(DeclContext *const scopeDC,
635-
NominalTypeDecl *const nominal,
636-
function_ref<bool(Optional<bool>)> calculateIsCascadingUse) = 0;
632+
NominalTypeDecl *const nominal) = 0;
637633

638634
#ifndef NDEBUG
639635
virtual void startingNextLookupStep() = 0;
@@ -655,8 +651,7 @@ class ASTScopeDeclGatherer : public AbstractASTScopeDeclConsumer {
655651

656652
/// Eventually this functionality should move into ASTScopeLookup
657653
bool lookInMembers(DeclContext *const,
658-
NominalTypeDecl *const,
659-
function_ref<bool(Optional<bool>)>) override {
654+
NominalTypeDecl *const) override {
660655
return false;
661656
}
662657

@@ -691,10 +686,6 @@ class ASTScope {
691686
unqualifiedLookup(SourceFile *, DeclNameRef, SourceLoc,
692687
namelookup::AbstractASTScopeDeclConsumer &);
693688

694-
static Optional<bool>
695-
computeIsCascadingUse(ArrayRef<const ast_scope::ASTScopeImpl *> history,
696-
Optional<bool> initialIsCascadingUse);
697-
698689
/// Entry point to record the visible statement labels from the given
699690
/// point.
700691
///

lib/AST/ASTScope.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ llvm::SmallVector<const ASTScopeImpl *, 0> ASTScope::unqualifiedLookup(
4646
return ASTScopeImpl::unqualifiedLookup(SF, name, loc, consumer);
4747
}
4848

49-
Optional<bool> ASTScope::computeIsCascadingUse(
50-
ArrayRef<const ast_scope::ASTScopeImpl *> history,
51-
Optional<bool> initialIsCascadingUse) {
52-
return ASTScopeImpl::computeIsCascadingUse(history, initialIsCascadingUse);
53-
}
54-
5549
llvm::SmallVector<LabeledStmt *, 4> ASTScope::lookupLabeledStmts(
5650
SourceFile *sourceFile, SourceLoc loc) {
5751
return ASTScopeImpl::lookupLabeledStmts(sourceFile, loc);

lib/AST/ASTScopeLookup.cpp

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ bool ASTScopeImpl::lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
246246
bool GenericTypeOrExtensionScope::lookupLocalsOrMembers(
247247
ArrayRef<const ASTScopeImpl *> history,
248248
ASTScopeImpl::DeclConsumer consumer) const {
249-
// isCascadingUseArg must have already been resolved, for a real lookup
250-
// but may be \c None for dumping.
251249
return portion->lookupMembersOf(this, history, consumer);
252250
}
253251

@@ -264,12 +262,7 @@ bool GenericTypeOrExtensionWhereOrBodyPortion::lookupMembersOf(
264262
auto nt = scope->getCorrespondingNominalTypeDecl().getPtrOrNull();
265263
if (!nt)
266264
return false;
267-
return consumer.lookInMembers(scope->getDeclContext().get(), nt,
268-
[&](Optional<bool> initialIsCascadingUse) {
269-
return ASTScopeImpl::computeIsCascadingUse(
270-
history, initialIsCascadingUse)
271-
.getValueOr(true);
272-
});
265+
return consumer.lookInMembers(scope->getDeclContext().get(), nt);
273266
}
274267

275268
bool GenericTypeOrExtensionWherePortion::lookupMembersOf(
@@ -455,17 +448,6 @@ bool ASTScopeImpl::lookupLocalBindingsInPattern(const Pattern *p,
455448
return isDone;
456449
}
457450

458-
#pragma mark compute isCascadingUse
459-
460-
Optional<bool> ASTScopeImpl::computeIsCascadingUse(
461-
ArrayRef<const ASTScopeImpl *> history,
462-
const Optional<bool> initialIsCascadingUse) {
463-
Optional<bool> isCascadingUse = initialIsCascadingUse;
464-
for (const auto *scope : history)
465-
isCascadingUse = scope->resolveIsCascadingUseForThisScope(isCascadingUse);
466-
return isCascadingUse;
467-
}
468-
469451
#pragma mark getLookupLimit
470452

471453
NullablePtr<const ASTScopeImpl> ASTScopeImpl::getLookupLimit() const {
@@ -523,77 +505,6 @@ NullablePtr<const ASTScopeImpl> ASTScopeImpl::ancestorWithDeclSatisfying(
523505
return nullptr;
524506
}
525507

526-
#pragma mark ifUnknownIsCascadingUseAccordingTo
527-
528-
static bool ifUnknownIsCascadingUseAccordingTo(Optional<bool> isCascadingUse,
529-
const DeclContext *const dc) {
530-
return isCascadingUse.getValueOr(false);
531-
}
532-
533-
#pragma mark resolveIsCascadingUseForThisScope
534-
535-
Optional<bool> ASTScopeImpl::resolveIsCascadingUseForThisScope(
536-
Optional<bool> isCascadingUse) const {
537-
return isCascadingUse;
538-
}
539-
540-
Optional<bool> GenericParamScope::resolveIsCascadingUseForThisScope(
541-
Optional<bool> isCascadingUse) const {
542-
if (auto *dc = getDeclContext().getPtrOrNull())
543-
return ifUnknownIsCascadingUseAccordingTo(isCascadingUse, dc);
544-
ASTScope_unreachable("generic what?");
545-
}
546-
547-
Optional<bool> AbstractFunctionDeclScope::resolveIsCascadingUseForThisScope(
548-
Optional<bool> isCascadingUse) const {
549-
return false;
550-
}
551-
552-
Optional<bool> AbstractFunctionBodyScope::resolveIsCascadingUseForThisScope(
553-
Optional<bool> isCascadingUse) const {
554-
return false;
555-
}
556-
557-
Optional<bool> GenericTypeOrExtensionScope::resolveIsCascadingUseForThisScope(
558-
Optional<bool> isCascadingUse) const {
559-
// Could override for ExtensionScope and just return true
560-
return ifUnknownIsCascadingUseAccordingTo(isCascadingUse,
561-
getDeclContext().get());
562-
}
563-
564-
Optional<bool>
565-
DefaultArgumentInitializerScope::resolveIsCascadingUseForThisScope(
566-
Optional<bool>) const {
567-
return false;
568-
}
569-
570-
Optional<bool> ClosureParametersScope::resolveIsCascadingUseForThisScope(
571-
Optional<bool> isCascadingUse) const {
572-
return ifUnknownIsCascadingUseAccordingTo(isCascadingUse, closureExpr);
573-
}
574-
575-
Optional<bool> PatternEntryInitializerScope::resolveIsCascadingUseForThisScope(
576-
Optional<bool> isCascadingUse) const {
577-
auto *const initContext = getPatternEntry().getInitContext();
578-
auto *PBI = dyn_cast_or_null<PatternBindingInitializer>(initContext);
579-
auto *isd = PBI ? PBI->getImplicitSelfDecl() : nullptr;
580-
581-
// 'self' is available within the pattern initializer of a 'lazy' variable.
582-
if (isd)
583-
return ifUnknownIsCascadingUseAccordingTo(isCascadingUse, PBI);
584-
585-
// initializing stored property of a type
586-
auto *const patternDeclContext = decl->getDeclContext();
587-
if (patternDeclContext->isTypeContext())
588-
return false;
589-
590-
// initializing global or local
591-
if (PBI)
592-
return ifUnknownIsCascadingUseAccordingTo(isCascadingUse, PBI);
593-
594-
return isCascadingUse;
595-
}
596-
597508
#pragma mark isLabeledStmtLookupTerminator implementations
598509
bool ASTScopeImpl::isLabeledStmtLookupTerminator() const {
599510
return true;

lib/AST/NameLookup.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,8 @@ namespace {
814814
/// Retrieve the set of type declarations that are directly referenced from
815815
/// the given parsed type representation.
816816
static DirectlyReferencedTypeDecls
817-
directReferencesForTypeRepr(Evaluator &evaluator,
818-
ASTContext &ctx, TypeRepr *typeRepr,
819-
DeclContext *dc);
817+
directReferencesForTypeRepr(Evaluator &evaluator, ASTContext &ctx,
818+
TypeRepr *typeRepr, DeclContext *dc);
820819

821820
/// Retrieve the set of type declarations that are directly referenced from
822821
/// the given type.
@@ -838,7 +837,8 @@ SelfBounds SelfBoundsFromWhereClauseRequest::evaluate(
838837
auto *protoDecl = dyn_cast_or_null<const ProtocolDecl>(typeDecl);
839838
auto *extDecl = decl.dyn_cast<const ExtensionDecl *>();
840839

841-
DeclContext *dc = protoDecl ? (DeclContext *)protoDecl : (DeclContext *)extDecl;
840+
const DeclContext *dc =
841+
protoDecl ? (const DeclContext *)protoDecl : (const DeclContext *)extDecl;
842842

843843
// A protocol or extension 'where' clause can reference associated types of
844844
// the protocol itself, so we have to start unqualified lookup from 'dc'.
@@ -2192,7 +2192,7 @@ DirectlyReferencedTypeDecls InheritedDeclsReferencedRequest::evaluate(
21922192
dc = (DeclContext *)decl.get<const ExtensionDecl *>();
21932193

21942194
return directReferencesForTypeRepr(evaluator, dc->getASTContext(), typeRepr,
2195-
dc);
2195+
const_cast<DeclContext *>(dc));
21962196
}
21972197

21982198
// Fall back to semantic types.

0 commit comments

Comments
 (0)