Skip to content

Commit d37318b

Browse files
authored
Merge pull request #34022 from slavapestov/astscope-remove-re-expansion
ASTScope: Remove "re-expansion" mechanism
2 parents 13f8641 + 4bbdb95 commit d37318b

File tree

9 files changed

+6
-344
lines changed

9 files changed

+6
-344
lines changed

include/swift/AST/ASTScope.h

Lines changed: 1 addition & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ class ASTScopeImpl {
146146

147147
bool wasExpanded = false;
148148

149-
/// For use-before-def, ASTAncestor scopes may be added to a BraceStmt.
150-
unsigned astAncestorScopeCount = 0;
151-
152149
/// Can clear storedChildren, so must remember this
153150
bool haveAddedCleanup = false;
154151

@@ -192,26 +189,10 @@ class ASTScopeImpl {
192189

193190
const Children &getChildren() const { return storedChildren; }
194191

195-
/// Get ride of descendants and remove them from scopedNodes so the scopes
196-
/// can be recreated. Needed because typechecking inserts a return statment
197-
/// into initializers.
198-
void disownDescendants(ScopeCreator &);
199-
200-
public: // for addReusedBodyScopes
192+
public:
201193
void addChild(ASTScopeImpl *child, ASTContext &);
202-
std::vector<ASTScopeImpl *> rescueASTAncestorScopesForReuseFromMe();
203-
204-
/// When reexpanding, do we always create a new body?
205-
virtual NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued();
206-
std::vector<ASTScopeImpl *>
207-
rescueASTAncestorScopesForReuseFromMeOrDescendants();
208-
void replaceASTAncestorScopes(ArrayRef<ASTScopeImpl *>);
209-
210-
private:
211-
void removeChildren();
212194

213195
private:
214-
void emancipate() { parent = nullptr; }
215196
NullablePtr<ASTScopeImpl> getPriorSibling() const;
216197

217198
public:
@@ -278,12 +259,6 @@ class ASTScopeImpl {
278259
private:
279260
void clearCachedSourceRangesOfMeAndAncestors();
280261

281-
public:
282-
/// Since source ranges are cached but depend on child ranges,
283-
/// when descendants are added, my and my ancestor ranges must be
284-
/// recalculated.
285-
void ensureSourceRangesAreCorrectWhenAddingDescendants(function_ref<void()>);
286-
287262
public: // public for debugging
288263
/// Returns source range of this node alone, without factoring in any
289264
/// children.
@@ -349,23 +324,11 @@ class ASTScopeImpl {
349324
/// fact, above and beyond adding Decls to the SourceFile.
350325
ASTScopeImpl *expandAndBeCurrent(ScopeCreator &);
351326

352-
unsigned getASTAncestorScopeCount() const { return astAncestorScopeCount; }
353327
bool getWasExpanded() const { return wasExpanded; }
354328

355329
protected:
356-
void resetASTAncestorScopeCount() { astAncestorScopeCount = 0; }
357-
void increaseASTAncestorScopeCount(unsigned c) { astAncestorScopeCount += c; }
358330
void setWasExpanded() { wasExpanded = true; }
359331
virtual ASTScopeImpl *expandSpecifically(ScopeCreator &) = 0;
360-
virtual void beCurrent();
361-
virtual bool doesExpansionOnlyAddNewDeclsAtEnd() const;
362-
363-
public:
364-
bool isExpansionNeeded(const ScopeCreator &) const;
365-
366-
protected:
367-
bool isCurrent() const;
368-
virtual bool isCurrentIfWasExpanded() const;
369332

370333
private:
371334
/// Compare the pre-expasion range with the post-expansion range and return
@@ -382,13 +345,6 @@ class ASTScopeImpl {
382345
virtual SourceRange sourceRangeForDeferredExpansion() const;
383346

384347
public:
385-
// Some nodes (VarDecls and Accessors) are created directly from
386-
// pattern scope code and should neither be deferred nor should
387-
// contribute to widenSourceRangeForIgnoredASTNode.
388-
// Closures and captures are also created directly but are
389-
// screened out because they are expressions.
390-
static bool isHandledSpeciallyByPatterns(const ASTNode n);
391-
392348
virtual NullablePtr<AbstractStorageDecl>
393349
getEnclosingAbstractStorageDecl() const;
394350

@@ -530,12 +486,6 @@ class ASTSourceFileScope final : public ASTScopeImpl {
530486
ScopeCreator *const scopeCreator;
531487
ASTScopeImpl *insertionPoint;
532488

533-
/// The number of \c Decls in the \c SourceFile that were already seen.
534-
/// Since parsing can be interleaved with type-checking, on every
535-
/// lookup, look at creating scopes for any \c Decls beyond this number.
536-
/// TODO: Unify with numberOfChildrenWhenLastExpanded
537-
size_t numberOfDeclsAlreadySeen = 0;
538-
539489
ASTSourceFileScope(SourceFile *SF, ScopeCreator *scopeCreator);
540490

541491
std::string getClassName() const override;
@@ -560,9 +510,6 @@ class ASTSourceFileScope final : public ASTScopeImpl {
560510

561511
protected:
562512
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
563-
bool isCurrentIfWasExpanded() const override;
564-
void beCurrent() override;
565-
bool doesExpansionOnlyAddNewDeclsAtEnd() const override;
566513

567514
ScopeCreator &getScopeCreator() override;
568515

@@ -609,8 +556,6 @@ class Portion {
609556
virtual const Decl *
610557
getReferrentOfScope(const GenericTypeOrExtensionScope *s) const;
611558

612-
virtual void beCurrent(IterableTypeScope *) const = 0;
613-
virtual bool isCurrentIfWasExpanded(const IterableTypeScope *) const = 0;
614559
virtual NullablePtr<ASTScopeImpl>
615560
insertionPointForDeferredExpansion(IterableTypeScope *) const = 0;
616561
virtual SourceRange
@@ -636,19 +581,6 @@ class Portion {
636581
const Decl *
637582
getReferrentOfScope(const GenericTypeOrExtensionScope *s) const override;
638583

639-
/// Make whole portion lazy to avoid circularity in lookup of generic
640-
/// parameters of extensions. When \c bindExtension is called, it needs to
641-
/// unqualifed-lookup the type being extended. That causes an \c
642-
/// ExtensionScope
643-
/// (\c GenericTypeOrExtensionWholePortion) to be built.
644-
/// The building process needs the generic parameters, but that results in a
645-
/// request for the extended nominal type of the \c ExtensionDecl, which is
646-
/// an endless recursion. Although we only need to make \c ExtensionScope
647-
/// lazy, might as well do it for all \c IterableTypeScopes.
648-
649-
void beCurrent(IterableTypeScope *) const override;
650-
bool isCurrentIfWasExpanded(const IterableTypeScope *) const override;
651-
652584
NullablePtr<ASTScopeImpl>
653585
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
654586
SourceRange
@@ -682,9 +614,6 @@ class GenericTypeOrExtensionWherePortion final
682614
SourceRange getChildlessSourceRangeOf(const GenericTypeOrExtensionScope *,
683615
bool omitAssertions) const override;
684616

685-
void beCurrent(IterableTypeScope *) const override;
686-
bool isCurrentIfWasExpanded(const IterableTypeScope *) const override;
687-
688617
NullablePtr<ASTScopeImpl>
689618
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
690619
SourceRange
@@ -704,8 +633,6 @@ class IterableTypeBodyPortion final
704633
SourceRange getChildlessSourceRangeOf(const GenericTypeOrExtensionScope *,
705634
bool omitAssertions) const override;
706635

707-
void beCurrent(IterableTypeScope *) const override;
708-
bool isCurrentIfWasExpanded(const IterableTypeScope *) const override;
709636
NullablePtr<ASTScopeImpl>
710637
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
711638
SourceRange
@@ -796,11 +723,6 @@ class GenericTypeScope : public GenericTypeOrExtensionScope {
796723
};
797724

798725
class IterableTypeScope : public GenericTypeScope {
799-
/// Because of \c parseDelayedDecl members can get added after the tree is
800-
/// constructed, and they can be out of order. Detect this happening by
801-
/// remembering the member count.
802-
unsigned memberCount = 0;
803-
804726
public:
805727
IterableTypeScope(const Portion *p) : GenericTypeScope(p) {}
806728
virtual ~IterableTypeScope() {}
@@ -810,15 +732,7 @@ class IterableTypeScope : public GenericTypeScope {
810732
bool doesDeclHaveABody() const override;
811733
void expandBody(ScopeCreator &) override;
812734

813-
protected:
814-
void beCurrent() override;
815-
bool isCurrentIfWasExpanded() const override;
816-
817735
public:
818-
void makeWholeCurrent();
819-
bool isWholeCurrent() const;
820-
void makeBodyCurrent();
821-
bool isBodyCurrent() const;
822736
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion() override;
823737
SourceRange sourceRangeForDeferredExpansion() const override;
824738

@@ -1019,18 +933,11 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
1019933
public:
1020934
AbstractFunctionDecl *const decl;
1021935

1022-
/// \c Parser::parseAbstractFunctionBodyDelayed can call \c
1023-
/// AbstractFunctionDecl::setBody after the tree has been constructed. So if
1024-
/// this changes, have to rebuild body.
1025-
NullablePtr<BraceStmt> bodyWhenLastExpanded;
1026-
1027936
AbstractFunctionBodyScope(AbstractFunctionDecl *e) : decl(e) {}
1028937
virtual ~AbstractFunctionBodyScope() {}
1029938

1030939
protected:
1031940
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
1032-
void beCurrent() override;
1033-
bool isCurrentIfWasExpanded() const override;
1034941

1035942
private:
1036943
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
@@ -1045,8 +952,6 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
1045952
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
1046953
Decl *getDecl() const { return decl; }
1047954

1048-
NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued() override;
1049-
1050955
protected:
1051956
bool lookupLocalsOrMembers(DeclConsumer) const override;
1052957

@@ -1173,9 +1078,6 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
11731078
};
11741079

11751080
class PatternEntryDeclScope final : public AbstractPatternEntryScope {
1176-
const Expr *initWhenLastExpanded;
1177-
unsigned varCountWhenLastExpanded = 0;
1178-
11791081
public:
11801082
PatternEntryDeclScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
11811083
DeclVisibilityKind vis)
@@ -1184,8 +1086,6 @@ class PatternEntryDeclScope final : public AbstractPatternEntryScope {
11841086

11851087
protected:
11861088
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
1187-
void beCurrent() override;
1188-
bool isCurrentIfWasExpanded() const override;
11891089

11901090
private:
11911091
AnnotatedInsertionPoint
@@ -1354,20 +1254,16 @@ class ClosureParametersScope final : public ASTScopeImpl {
13541254
class TopLevelCodeScope final : public ASTScopeImpl {
13551255
public:
13561256
TopLevelCodeDecl *const decl;
1357-
BraceStmt *bodyWhenLastExpanded;
13581257

13591258
TopLevelCodeScope(TopLevelCodeDecl *e) : decl(e) {}
13601259
virtual ~TopLevelCodeScope() {}
13611260

13621261
protected:
13631262
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
1364-
void beCurrent() override;
1365-
bool isCurrentIfWasExpanded() const override;
13661263

13671264
private:
13681265
AnnotatedInsertionPoint
13691266
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
1370-
std::vector<ASTScopeImpl *> rescueBodyScopesToReuse();
13711267

13721268
public:
13731269
std::string getClassName() const override;
@@ -1379,8 +1275,6 @@ class TopLevelCodeScope final : public ASTScopeImpl {
13791275
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
13801276
Decl *getDecl() const { return decl; }
13811277
NullablePtr<const void> getReferrent() const override;
1382-
1383-
NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued() override;
13841278
};
13851279

13861280
/// The \c _@specialize attribute.

include/swift/AST/Decl.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,8 +1948,6 @@ class PatternBindingEntry {
19481948
CaptureInfo getCaptureInfo() const { return Captures; }
19491949
void setCaptureInfo(CaptureInfo captures) { Captures = captures; }
19501950

1951-
unsigned getNumBoundVariables() const;
1952-
19531951
private:
19541952
SourceLoc getLastAccessorEndLoc() const;
19551953
};

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -712,13 +712,6 @@ class IterableDeclContext {
712712
/// member loading, as a key when doing lookup in this IDC.
713713
serialization::DeclID SerialID;
714714

715-
/// Because \c parseDelayedDecl and lazy member adding can add members *after*
716-
/// an \c ASTScope tree is created, there must be some way for the tree to
717-
/// detect when a member has been added. A bit would suffice,
718-
/// but would be more fragile, The scope code could count the members each
719-
/// time, but I think it's a better trade to just keep a count here.
720-
unsigned MemberCount : 29;
721-
722715
/// Whether we have already added the parsed members into the context.
723716
unsigned AddedParsedMembers : 1;
724717

@@ -741,7 +734,6 @@ class IterableDeclContext {
741734
public:
742735
IterableDeclContext(IterableDeclContextKind kind)
743736
: LastDeclAndKind(nullptr, kind) {
744-
MemberCount = 0;
745737
AddedParsedMembers = 0;
746738
HasOperatorDeclarations = 0;
747739
HasNestedClassDeclarations = 0;
@@ -794,9 +786,6 @@ class IterableDeclContext {
794786
/// is inserted immediately after the hint.
795787
void addMember(Decl *member, Decl *hint = nullptr);
796788

797-
/// See \c MemberCount
798-
unsigned getMemberCount() const;
799-
800789
/// Check whether there are lazily-loaded members.
801790
bool hasLazyMembers() const {
802791
return FirstDeclAndLazyMembers.getInt();

include/swift/Basic/LangOptions.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,6 @@ namespace swift {
251251
/// Someday, ASTScopeLookup will supplant lookup in the parser
252252
bool DisableParserLookup = false;
253253

254-
/// Should we stress ASTScope-based resolution for debugging?
255-
bool StressASTScopeLookup = false;
256-
257254
/// Whether to enable the new operator decl and precedencegroup lookup
258255
/// behavior. This is a staging flag, and will be removed in the future.
259256
bool EnableNewOperatorLookup = false;

include/swift/Option/FrontendOptions.td

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,6 @@ def disable_target_os_checking :
157157
def crosscheck_unqualified_lookup : Flag<["-"], "crosscheck-unqualified-lookup">,
158158
HelpText<"Compare legacy DeclContext- to ASTScope-based unqualified name lookup (for debugging)">;
159159

160-
def stress_astscope_lookup : Flag<["-"], "stress-astscope-lookup">,
161-
HelpText<"Stress ASTScope-based unqualified name lookup (for testing)">;
162-
163160
def use_clang_function_types : Flag<["-"], "use-clang-function-types">,
164161
HelpText<"Use stored Clang function types for computing canonical types.">;
165162

0 commit comments

Comments
 (0)