Skip to content

ASTScope: Remove "re-expansion" mechanism #34022

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 10 commits into from
Sep 23, 2020
Merged
108 changes: 1 addition & 107 deletions include/swift/AST/ASTScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,6 @@ class ASTScopeImpl {

bool wasExpanded = false;

/// For use-before-def, ASTAncestor scopes may be added to a BraceStmt.
unsigned astAncestorScopeCount = 0;

/// Can clear storedChildren, so must remember this
bool haveAddedCleanup = false;

Expand Down Expand Up @@ -192,26 +189,10 @@ class ASTScopeImpl {

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

/// Get ride of descendants and remove them from scopedNodes so the scopes
/// can be recreated. Needed because typechecking inserts a return statment
/// into initializers.
void disownDescendants(ScopeCreator &);

public: // for addReusedBodyScopes
public:
void addChild(ASTScopeImpl *child, ASTContext &);
std::vector<ASTScopeImpl *> rescueASTAncestorScopesForReuseFromMe();

/// When reexpanding, do we always create a new body?
virtual NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued();
std::vector<ASTScopeImpl *>
rescueASTAncestorScopesForReuseFromMeOrDescendants();
void replaceASTAncestorScopes(ArrayRef<ASTScopeImpl *>);

private:
void removeChildren();

private:
void emancipate() { parent = nullptr; }
NullablePtr<ASTScopeImpl> getPriorSibling() const;

public:
Expand Down Expand Up @@ -278,12 +259,6 @@ class ASTScopeImpl {
private:
void clearCachedSourceRangesOfMeAndAncestors();

public:
/// Since source ranges are cached but depend on child ranges,
/// when descendants are added, my and my ancestor ranges must be
/// recalculated.
void ensureSourceRangesAreCorrectWhenAddingDescendants(function_ref<void()>);

public: // public for debugging
/// Returns source range of this node alone, without factoring in any
/// children.
Expand Down Expand Up @@ -349,23 +324,11 @@ class ASTScopeImpl {
/// fact, above and beyond adding Decls to the SourceFile.
ASTScopeImpl *expandAndBeCurrent(ScopeCreator &);

unsigned getASTAncestorScopeCount() const { return astAncestorScopeCount; }
bool getWasExpanded() const { return wasExpanded; }

protected:
void resetASTAncestorScopeCount() { astAncestorScopeCount = 0; }
void increaseASTAncestorScopeCount(unsigned c) { astAncestorScopeCount += c; }
void setWasExpanded() { wasExpanded = true; }
virtual ASTScopeImpl *expandSpecifically(ScopeCreator &) = 0;
virtual void beCurrent();
virtual bool doesExpansionOnlyAddNewDeclsAtEnd() const;

public:
bool isExpansionNeeded(const ScopeCreator &) const;

protected:
bool isCurrent() const;
virtual bool isCurrentIfWasExpanded() const;

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

public:
// Some nodes (VarDecls and Accessors) are created directly from
// pattern scope code and should neither be deferred nor should
// contribute to widenSourceRangeForIgnoredASTNode.
// Closures and captures are also created directly but are
// screened out because they are expressions.
static bool isHandledSpeciallyByPatterns(const ASTNode n);

virtual NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const;

Expand Down Expand Up @@ -530,12 +486,6 @@ class ASTSourceFileScope final : public ASTScopeImpl {
ScopeCreator *const scopeCreator;
ASTScopeImpl *insertionPoint;

/// The number of \c Decls in the \c SourceFile that were already seen.
/// Since parsing can be interleaved with type-checking, on every
/// lookup, look at creating scopes for any \c Decls beyond this number.
/// TODO: Unify with numberOfChildrenWhenLastExpanded
size_t numberOfDeclsAlreadySeen = 0;

ASTSourceFileScope(SourceFile *SF, ScopeCreator *scopeCreator);

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

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
bool isCurrentIfWasExpanded() const override;
void beCurrent() override;
bool doesExpansionOnlyAddNewDeclsAtEnd() const override;

ScopeCreator &getScopeCreator() override;

Expand Down Expand Up @@ -609,8 +556,6 @@ class Portion {
virtual const Decl *
getReferrentOfScope(const GenericTypeOrExtensionScope *s) const;

virtual void beCurrent(IterableTypeScope *) const = 0;
virtual bool isCurrentIfWasExpanded(const IterableTypeScope *) const = 0;
virtual NullablePtr<ASTScopeImpl>
insertionPointForDeferredExpansion(IterableTypeScope *) const = 0;
virtual SourceRange
Expand All @@ -636,19 +581,6 @@ class Portion {
const Decl *
getReferrentOfScope(const GenericTypeOrExtensionScope *s) const override;

/// Make whole portion lazy to avoid circularity in lookup of generic
/// parameters of extensions. When \c bindExtension is called, it needs to
/// unqualifed-lookup the type being extended. That causes an \c
/// ExtensionScope
/// (\c GenericTypeOrExtensionWholePortion) to be built.
/// The building process needs the generic parameters, but that results in a
/// request for the extended nominal type of the \c ExtensionDecl, which is
/// an endless recursion. Although we only need to make \c ExtensionScope
/// lazy, might as well do it for all \c IterableTypeScopes.

void beCurrent(IterableTypeScope *) const override;
bool isCurrentIfWasExpanded(const IterableTypeScope *) const override;

NullablePtr<ASTScopeImpl>
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
SourceRange
Expand Down Expand Up @@ -682,9 +614,6 @@ class GenericTypeOrExtensionWherePortion final
SourceRange getChildlessSourceRangeOf(const GenericTypeOrExtensionScope *,
bool omitAssertions) const override;

void beCurrent(IterableTypeScope *) const override;
bool isCurrentIfWasExpanded(const IterableTypeScope *) const override;

NullablePtr<ASTScopeImpl>
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
SourceRange
Expand All @@ -704,8 +633,6 @@ class IterableTypeBodyPortion final
SourceRange getChildlessSourceRangeOf(const GenericTypeOrExtensionScope *,
bool omitAssertions) const override;

void beCurrent(IterableTypeScope *) const override;
bool isCurrentIfWasExpanded(const IterableTypeScope *) const override;
NullablePtr<ASTScopeImpl>
insertionPointForDeferredExpansion(IterableTypeScope *) const override;
SourceRange
Expand Down Expand Up @@ -796,11 +723,6 @@ class GenericTypeScope : public GenericTypeOrExtensionScope {
};

class IterableTypeScope : public GenericTypeScope {
/// Because of \c parseDelayedDecl members can get added after the tree is
/// constructed, and they can be out of order. Detect this happening by
/// remembering the member count.
unsigned memberCount = 0;

public:
IterableTypeScope(const Portion *p) : GenericTypeScope(p) {}
virtual ~IterableTypeScope() {}
Expand All @@ -810,15 +732,7 @@ class IterableTypeScope : public GenericTypeScope {
bool doesDeclHaveABody() const override;
void expandBody(ScopeCreator &) override;

protected:
void beCurrent() override;
bool isCurrentIfWasExpanded() const override;

public:
void makeWholeCurrent();
bool isWholeCurrent() const;
void makeBodyCurrent();
bool isBodyCurrent() const;
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion() override;
SourceRange sourceRangeForDeferredExpansion() const override;

Expand Down Expand Up @@ -1019,18 +933,11 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
public:
AbstractFunctionDecl *const decl;

/// \c Parser::parseAbstractFunctionBodyDelayed can call \c
/// AbstractFunctionDecl::setBody after the tree has been constructed. So if
/// this changes, have to rebuild body.
NullablePtr<BraceStmt> bodyWhenLastExpanded;

AbstractFunctionBodyScope(AbstractFunctionDecl *e) : decl(e) {}
virtual ~AbstractFunctionBodyScope() {}

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
void beCurrent() override;
bool isCurrentIfWasExpanded() const override;

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

NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued() override;

protected:
bool lookupLocalsOrMembers(DeclConsumer) const override;

Expand Down Expand Up @@ -1173,9 +1078,6 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
};

class PatternEntryDeclScope final : public AbstractPatternEntryScope {
const Expr *initWhenLastExpanded;
unsigned varCountWhenLastExpanded = 0;

public:
PatternEntryDeclScope(PatternBindingDecl *pbDecl, unsigned entryIndex,
DeclVisibilityKind vis)
Expand All @@ -1184,8 +1086,6 @@ class PatternEntryDeclScope final : public AbstractPatternEntryScope {

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
void beCurrent() override;
bool isCurrentIfWasExpanded() const override;

private:
AnnotatedInsertionPoint
Expand Down Expand Up @@ -1354,20 +1254,16 @@ class ClosureParametersScope final : public ASTScopeImpl {
class TopLevelCodeScope final : public ASTScopeImpl {
public:
TopLevelCodeDecl *const decl;
BraceStmt *bodyWhenLastExpanded;

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

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
void beCurrent() override;
bool isCurrentIfWasExpanded() const override;

private:
AnnotatedInsertionPoint
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
std::vector<ASTScopeImpl *> rescueBodyScopesToReuse();

public:
std::string getClassName() const override;
Expand All @@ -1379,8 +1275,6 @@ class TopLevelCodeScope final : public ASTScopeImpl {
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
NullablePtr<const void> getReferrent() const override;

NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued() override;
};

/// The \c _@specialize attribute.
Expand Down
2 changes: 0 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1948,8 +1948,6 @@ class PatternBindingEntry {
CaptureInfo getCaptureInfo() const { return Captures; }
void setCaptureInfo(CaptureInfo captures) { Captures = captures; }

unsigned getNumBoundVariables() const;

private:
SourceLoc getLastAccessorEndLoc() const;
};
Expand Down
11 changes: 0 additions & 11 deletions include/swift/AST/DeclContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,6 @@ class IterableDeclContext {
/// member loading, as a key when doing lookup in this IDC.
serialization::DeclID SerialID;

/// Because \c parseDelayedDecl and lazy member adding can add members *after*
/// an \c ASTScope tree is created, there must be some way for the tree to
/// detect when a member has been added. A bit would suffice,
/// but would be more fragile, The scope code could count the members each
/// time, but I think it's a better trade to just keep a count here.
unsigned MemberCount : 29;

/// Whether we have already added the parsed members into the context.
unsigned AddedParsedMembers : 1;

Expand All @@ -741,7 +734,6 @@ class IterableDeclContext {
public:
IterableDeclContext(IterableDeclContextKind kind)
: LastDeclAndKind(nullptr, kind) {
MemberCount = 0;
AddedParsedMembers = 0;
HasOperatorDeclarations = 0;
HasNestedClassDeclarations = 0;
Expand Down Expand Up @@ -794,9 +786,6 @@ class IterableDeclContext {
/// is inserted immediately after the hint.
void addMember(Decl *member, Decl *hint = nullptr);

/// See \c MemberCount
unsigned getMemberCount() const;

/// Check whether there are lazily-loaded members.
bool hasLazyMembers() const {
return FirstDeclAndLazyMembers.getInt();
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ namespace swift {
/// Someday, ASTScopeLookup will supplant lookup in the parser
bool DisableParserLookup = false;

/// Should we stress ASTScope-based resolution for debugging?
bool StressASTScopeLookup = false;

/// Whether to enable the new operator decl and precedencegroup lookup
/// behavior. This is a staging flag, and will be removed in the future.
bool EnableNewOperatorLookup = false;
Expand Down
3 changes: 0 additions & 3 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,6 @@ def disable_target_os_checking :
def crosscheck_unqualified_lookup : Flag<["-"], "crosscheck-unqualified-lookup">,
HelpText<"Compare legacy DeclContext- to ASTScope-based unqualified name lookup (for debugging)">;

def stress_astscope_lookup : Flag<["-"], "stress-astscope-lookup">,
HelpText<"Stress ASTScope-based unqualified name lookup (for testing)">;

def use_clang_function_types : Flag<["-"], "use-clang-function-types">,
HelpText<"Use stored Clang function types for computing canonical types.">;

Expand Down
Loading