Skip to content

ASTScope cleanup in preparation for re-doing local pattern bindings #34039

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
100 changes: 17 additions & 83 deletions include/swift/AST/ASTScope.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,11 @@ class ASTScopeImpl {
virtual SourceRange sourceRangeForDeferredExpansion() const;

public:
virtual NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const;

bool isATypeDeclScope() const;

private:
virtual ScopeCreator &getScopeCreator();

#pragma mark - - creation queries
public:
virtual bool isThisAnAbstractStorageDecl() const { return false; }

#pragma mark - lookup

public:
Expand Down Expand Up @@ -849,9 +842,6 @@ class GenericParamScope final : public ASTScopeImpl {
void printSpecifics(llvm::raw_ostream &out) const override;

public:
NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<const void> addressForPrinting() const override {
return paramList;
}
Expand Down Expand Up @@ -887,9 +877,6 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<const void> getReferrent() const override;

protected:
Expand All @@ -914,8 +901,7 @@ class ParameterListScope final : public ASTScopeImpl {
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;

private:
AnnotatedInsertionPoint
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
SourceLoc fixupEndForBadInput(SourceRange) const;

public:
Expand All @@ -924,17 +910,16 @@ class ParameterListScope final : public ASTScopeImpl {
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
virtual NullablePtr<DeclContext> getDeclContext() const override;

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;
NullablePtr<const void> addressForPrinting() const override { return params; }
};

class AbstractFunctionBodyScope : public ASTScopeImpl {
/// Body of functions, methods, constructors, destructors and accessors.
class FunctionBodyScope : public ASTScopeImpl {
public:
AbstractFunctionDecl *const decl;

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

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
Expand All @@ -956,19 +941,11 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
bool lookupLocalsOrMembers(DeclConsumer) const override;

public:
std::string getClassName() const override;
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion() override;
SourceRange sourceRangeForDeferredExpansion() const override;
};

/// Body of functions and methods.
class FunctionBodyScope final : public AbstractFunctionBodyScope {
public:
FunctionBodyScope(AbstractFunctionDecl *e)
: AbstractFunctionBodyScope(e) {}
std::string getClassName() const override;
bool lookupLocalsOrMembers(DeclConsumer consumer) const override;
};

class DefaultArgumentInitializerScope final : public ASTScopeImpl {
public:
ParamDecl *const decl;
Expand Down Expand Up @@ -1001,17 +978,19 @@ class DefaultArgumentInitializerScope final : public ASTScopeImpl {

class AttachedPropertyWrapperScope final : public ASTScopeImpl {
public:
VarDecl *const decl;
CustomAttr *attr;
VarDecl *decl;

/// Because we have to avoid request cycles, we approximate the test for an
/// AttachedPropertyWrapper with one based on source location. We might get
/// false positives, that that doesn't hurt anything. However, the result of
/// the conservative source range computation doesn't seem to be stable. So
/// keep the original here, and use it for source range queries.

const SourceRange sourceRangeWhenCreated;

AttachedPropertyWrapperScope(VarDecl *e)
: decl(e), sourceRangeWhenCreated(getSourceRangeOfVarDecl(e)) {
AttachedPropertyWrapperScope(CustomAttr *attr, VarDecl *decl)
: attr(attr), decl(decl),
sourceRangeWhenCreated(attr->getTypeRepr()->getSourceRange()) {
ASTScopeAssert(sourceRangeWhenCreated.isValid(),
"VarDecls must have ranges to be looked-up");
}
Expand All @@ -1027,7 +1006,10 @@ class AttachedPropertyWrapperScope final : public ASTScopeImpl {
NullablePtr<const void> addressForPrinting() const override { return decl; }
virtual NullablePtr<DeclContext> getDeclContext() const override;

static SourceRange getSourceRangeOfVarDecl(const VarDecl *);
NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
return attr;
}
NullablePtr<const void> getReferrent() const override;

private:
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
Expand Down Expand Up @@ -1068,11 +1050,8 @@ class AbstractPatternEntryScope : public ASTScopeImpl {

protected:
void printSpecifics(llvm::raw_ostream &out) const override;
void forEachVarDeclWithLocalizableAccessors(
ScopeCreator &scopeCreator, function_ref<void(VarDecl *)> foundOne) const;

public:
bool isLastEntry() const;
NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
};
Expand Down Expand Up @@ -1116,8 +1095,7 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;

private:
AnnotatedInsertionPoint
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);

public:
std::string getClassName() const override;
Expand Down Expand Up @@ -1296,9 +1274,6 @@ class SpecializeAttributeScope final : public ASTScopeImpl {
return specializeAttr;
}

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
return specializeAttr;
}
Expand Down Expand Up @@ -1329,9 +1304,6 @@ class DifferentiableAttributeScope final : public ASTScopeImpl {
return differentiableAttr;
}

NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override;

NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
return differentiableAttr;
}
Expand Down Expand Up @@ -1373,44 +1345,6 @@ class SubscriptDeclScope final : public ASTScopeImpl {

protected:
NullablePtr<const GenericParamList> genericParams() const override;
NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override {
return decl;
}
public:
bool isThisAnAbstractStorageDecl() const override { return true; }
};

class VarDeclScope final : public ASTScopeImpl {

public:
VarDecl *const decl;
VarDeclScope(VarDecl *e) : decl(e) {}
virtual ~VarDeclScope() {}

protected:
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;

private:
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);

public:
std::string getClassName() const override;
SourceRange
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;

protected:
void printSpecifics(llvm::raw_ostream &out) const override;

public:
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
Decl *getDecl() const { return decl; }
NullablePtr<const void> getReferrent() const override;
NullablePtr<AbstractStorageDecl>
getEnclosingAbstractStorageDecl() const override {
return decl;
}
bool isThisAnAbstractStorageDecl() const override { return true; }
};

class EnumElementScope : public ASTScopeImpl {
Expand Down
14 changes: 0 additions & 14 deletions lib/AST/ASTScope.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,6 @@ NullablePtr<ClosureExpr> ASTScopeImpl::getClosureIfClosureScope() const {
return nullptr;
}

// Conservative, because using precise info would be circular
SourceRange
AttachedPropertyWrapperScope::getSourceRangeOfVarDecl(const VarDecl *const vd) {
SourceRange sr;
for (auto *attr : vd->getAttrs().getAttributes<CustomAttr>()) {
if (sr.isInvalid())
sr = attr->getTypeRepr()->getSourceRange();
else
sr.widen(attr->getTypeRepr()->getSourceRange());
}
return sr;
}

SourceManager &ASTScopeImpl::getSourceManager() const {
return getASTContext().SourceMgr;
}
Expand Down Expand Up @@ -211,7 +198,6 @@ DEFINE_GET_CLASS_NAME(TopLevelCodeScope)
DEFINE_GET_CLASS_NAME(SpecializeAttributeScope)
DEFINE_GET_CLASS_NAME(DifferentiableAttributeScope)
DEFINE_GET_CLASS_NAME(SubscriptDeclScope)
DEFINE_GET_CLASS_NAME(VarDeclScope)
DEFINE_GET_CLASS_NAME(EnumElementScope)
DEFINE_GET_CLASS_NAME(IfStmtScope)
DEFINE_GET_CLASS_NAME(WhileStmtScope)
Expand Down
Loading