Skip to content

Commit 5c877c5

Browse files
authored
Merge pull request #34039 from slavapestov/astscope-clean-up-pattern-bindings
ASTScope cleanup in preparation for re-doing local pattern bindings
2 parents 769034b + 7c43e10 commit 5c877c5

File tree

6 files changed

+87
-285
lines changed

6 files changed

+87
-285
lines changed

include/swift/AST/ASTScope.h

Lines changed: 17 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -345,18 +345,11 @@ class ASTScopeImpl {
345345
virtual SourceRange sourceRangeForDeferredExpansion() const;
346346

347347
public:
348-
virtual NullablePtr<AbstractStorageDecl>
349-
getEnclosingAbstractStorageDecl() const;
350-
351348
bool isATypeDeclScope() const;
352349

353350
private:
354351
virtual ScopeCreator &getScopeCreator();
355352

356-
#pragma mark - - creation queries
357-
public:
358-
virtual bool isThisAnAbstractStorageDecl() const { return false; }
359-
360353
#pragma mark - lookup
361354

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

851844
public:
852-
NullablePtr<AbstractStorageDecl>
853-
getEnclosingAbstractStorageDecl() const override;
854-
855845
NullablePtr<const void> addressForPrinting() const override {
856846
return paramList;
857847
}
@@ -887,9 +877,6 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
887877
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
888878
Decl *getDecl() const { return decl; }
889879

890-
NullablePtr<AbstractStorageDecl>
891-
getEnclosingAbstractStorageDecl() const override;
892-
893880
NullablePtr<const void> getReferrent() const override;
894881

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

916903
private:
917-
AnnotatedInsertionPoint
918-
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
904+
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
919905
SourceLoc fixupEndForBadInput(SourceRange) const;
920906

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

927-
NullablePtr<AbstractStorageDecl>
928-
getEnclosingAbstractStorageDecl() const override;
929913
NullablePtr<const void> addressForPrinting() const override { return params; }
930914
};
931915

932-
class AbstractFunctionBodyScope : public ASTScopeImpl {
916+
/// Body of functions, methods, constructors, destructors and accessors.
917+
class FunctionBodyScope : public ASTScopeImpl {
933918
public:
934919
AbstractFunctionDecl *const decl;
935920

936-
AbstractFunctionBodyScope(AbstractFunctionDecl *e) : decl(e) {}
937-
virtual ~AbstractFunctionBodyScope() {}
921+
FunctionBodyScope(AbstractFunctionDecl *e) : decl(e) {}
922+
virtual ~FunctionBodyScope() {}
938923

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

958943
public:
944+
std::string getClassName() const override;
959945
NullablePtr<ASTScopeImpl> insertionPointForDeferredExpansion() override;
960946
SourceRange sourceRangeForDeferredExpansion() const override;
961947
};
962948

963-
/// Body of functions and methods.
964-
class FunctionBodyScope final : public AbstractFunctionBodyScope {
965-
public:
966-
FunctionBodyScope(AbstractFunctionDecl *e)
967-
: AbstractFunctionBodyScope(e) {}
968-
std::string getClassName() const override;
969-
bool lookupLocalsOrMembers(DeclConsumer consumer) const override;
970-
};
971-
972949
class DefaultArgumentInitializerScope final : public ASTScopeImpl {
973950
public:
974951
ParamDecl *const decl;
@@ -1001,17 +978,19 @@ class DefaultArgumentInitializerScope final : public ASTScopeImpl {
1001978

1002979
class AttachedPropertyWrapperScope final : public ASTScopeImpl {
1003980
public:
1004-
VarDecl *const decl;
981+
CustomAttr *attr;
982+
VarDecl *decl;
983+
1005984
/// Because we have to avoid request cycles, we approximate the test for an
1006985
/// AttachedPropertyWrapper with one based on source location. We might get
1007986
/// false positives, that that doesn't hurt anything. However, the result of
1008987
/// the conservative source range computation doesn't seem to be stable. So
1009988
/// keep the original here, and use it for source range queries.
1010-
1011989
const SourceRange sourceRangeWhenCreated;
1012990

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

1030-
static SourceRange getSourceRangeOfVarDecl(const VarDecl *);
1009+
NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
1010+
return attr;
1011+
}
1012+
NullablePtr<const void> getReferrent() const override;
10311013

10321014
private:
10331015
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
@@ -1068,11 +1050,8 @@ class AbstractPatternEntryScope : public ASTScopeImpl {
10681050

10691051
protected:
10701052
void printSpecifics(llvm::raw_ostream &out) const override;
1071-
void forEachVarDeclWithLocalizableAccessors(
1072-
ScopeCreator &scopeCreator, function_ref<void(VarDecl *)> foundOne) const;
10731053

10741054
public:
1075-
bool isLastEntry() const;
10761055
NullablePtr<Decl> getDeclIfAny() const override { return decl; }
10771056
Decl *getDecl() const { return decl; }
10781057
};
@@ -1116,8 +1095,7 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
11161095
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
11171096

11181097
private:
1119-
AnnotatedInsertionPoint
1120-
expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &);
1098+
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
11211099

11221100
public:
11231101
std::string getClassName() const override;
@@ -1296,9 +1274,6 @@ class SpecializeAttributeScope final : public ASTScopeImpl {
12961274
return specializeAttr;
12971275
}
12981276

1299-
NullablePtr<AbstractStorageDecl>
1300-
getEnclosingAbstractStorageDecl() const override;
1301-
13021277
NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
13031278
return specializeAttr;
13041279
}
@@ -1329,9 +1304,6 @@ class DifferentiableAttributeScope final : public ASTScopeImpl {
13291304
return differentiableAttr;
13301305
}
13311306

1332-
NullablePtr<AbstractStorageDecl>
1333-
getEnclosingAbstractStorageDecl() const override;
1334-
13351307
NullablePtr<DeclAttribute> getDeclAttributeIfAny() const override {
13361308
return differentiableAttr;
13371309
}
@@ -1373,44 +1345,6 @@ class SubscriptDeclScope final : public ASTScopeImpl {
13731345

13741346
protected:
13751347
NullablePtr<const GenericParamList> genericParams() const override;
1376-
NullablePtr<AbstractStorageDecl>
1377-
getEnclosingAbstractStorageDecl() const override {
1378-
return decl;
1379-
}
1380-
public:
1381-
bool isThisAnAbstractStorageDecl() const override { return true; }
1382-
};
1383-
1384-
class VarDeclScope final : public ASTScopeImpl {
1385-
1386-
public:
1387-
VarDecl *const decl;
1388-
VarDeclScope(VarDecl *e) : decl(e) {}
1389-
virtual ~VarDeclScope() {}
1390-
1391-
protected:
1392-
ASTScopeImpl *expandSpecifically(ScopeCreator &scopeCreator) override;
1393-
1394-
private:
1395-
void expandAScopeThatDoesNotCreateANewInsertionPoint(ScopeCreator &);
1396-
1397-
public:
1398-
std::string getClassName() const override;
1399-
SourceRange
1400-
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
1401-
1402-
protected:
1403-
void printSpecifics(llvm::raw_ostream &out) const override;
1404-
1405-
public:
1406-
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
1407-
Decl *getDecl() const { return decl; }
1408-
NullablePtr<const void> getReferrent() const override;
1409-
NullablePtr<AbstractStorageDecl>
1410-
getEnclosingAbstractStorageDecl() const override {
1411-
return decl;
1412-
}
1413-
bool isThisAnAbstractStorageDecl() const override { return true; }
14141348
};
14151349

14161350
class EnumElementScope : public ASTScopeImpl {

lib/AST/ASTScope.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,6 @@ NullablePtr<ClosureExpr> ASTScopeImpl::getClosureIfClosureScope() const {
9191
return nullptr;
9292
}
9393

94-
// Conservative, because using precise info would be circular
95-
SourceRange
96-
AttachedPropertyWrapperScope::getSourceRangeOfVarDecl(const VarDecl *const vd) {
97-
SourceRange sr;
98-
for (auto *attr : vd->getAttrs().getAttributes<CustomAttr>()) {
99-
if (sr.isInvalid())
100-
sr = attr->getTypeRepr()->getSourceRange();
101-
else
102-
sr.widen(attr->getTypeRepr()->getSourceRange());
103-
}
104-
return sr;
105-
}
106-
10794
SourceManager &ASTScopeImpl::getSourceManager() const {
10895
return getASTContext().SourceMgr;
10996
}
@@ -211,7 +198,6 @@ DEFINE_GET_CLASS_NAME(TopLevelCodeScope)
211198
DEFINE_GET_CLASS_NAME(SpecializeAttributeScope)
212199
DEFINE_GET_CLASS_NAME(DifferentiableAttributeScope)
213200
DEFINE_GET_CLASS_NAME(SubscriptDeclScope)
214-
DEFINE_GET_CLASS_NAME(VarDeclScope)
215201
DEFINE_GET_CLASS_NAME(EnumElementScope)
216202
DEFINE_GET_CLASS_NAME(IfStmtScope)
217203
DEFINE_GET_CLASS_NAME(WhileStmtScope)

0 commit comments

Comments
 (0)