Skip to content

Commit 1e0a9ca

Browse files
authored
Merge pull request #33989 from slavapestov/astscope-self-dc-cleanup
ASTScope: Redo 'selfDC' computation
2 parents 1c5be70 + d4cc35a commit 1e0a9ca

40 files changed

+207
-405
lines changed

include/swift/AST/ASTScope.h

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -474,16 +474,6 @@ class ASTScopeImpl {
474474
NullablePtr<const GenericParamList> lastListSearched,
475475
DeclConsumer consumer) const;
476476

477-
public:
478-
/// Returns the SelfDC for parent (and possibly ancestor) scopes.
479-
/// A return of None indicates that the previous child (in history) should be
480-
/// asked.
481-
virtual Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const;
482-
483-
/// Returns the context that should be used when a nested scope (e.g. a
484-
/// closure) captures self explicitly.
485-
virtual NullablePtr<DeclContext> capturedSelfDC() const;
486-
487477
protected:
488478
/// Find either locals or members (no scope has both)
489479
/// \param history The scopes visited since the start of lookup (including
@@ -691,27 +681,6 @@ class Portion {
691681
bool lookupMembersOf(const GenericTypeOrExtensionScope *scope,
692682
ArrayRef<const ASTScopeImpl *>,
693683
ASTScopeImpl::DeclConsumer consumer) const override;
694-
695-
private:
696-
/// A client needs to know if a lookup result required the dynamic implicit
697-
/// self value. It is required if the lookup originates from a method body
698-
/// or a lazy pattern initializer. So, one approach would be to call the
699-
/// consumer to find members right from those scopes. However, because
700-
/// members aren't the first things searched, generics are, that approache
701-
/// ends up duplicating code from the \c GenericTypeOrExtensionScope. So we
702-
/// take the approach of doing those lookups there, and using this function
703-
/// to compute the selfDC from the history.
704-
static NullablePtr<DeclContext>
705-
computeSelfDC(ArrayRef<const ASTScopeImpl *> history);
706-
707-
/// If we find a lookup result that requires the dynamic implict self value,
708-
/// we need to check the nested scopes to see if any closures explicitly
709-
/// captured \c self. In that case, the appropriate selfDC is that of the
710-
/// innermost closure which captures a \c self value from one of this type's
711-
/// methods.
712-
static NullablePtr<DeclContext>
713-
checkNestedScopesForSelfCapture(ArrayRef<const ASTScopeImpl *> history,
714-
size_t start);
715684
};
716685

717686
/// Behavior specific to representing the trailing where clause of a
@@ -810,7 +779,6 @@ class GenericTypeOrExtensionScope : public ASTScopeImpl {
810779
virtual std::string declKindName() const = 0;
811780
virtual bool doesDeclHaveABody() const;
812781
const char *portionName() const { return portion->portionName; }
813-
Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const override;
814782

815783
protected:
816784
Optional<bool> resolveIsCascadingUseForThisScope(
@@ -1037,8 +1005,6 @@ class AbstractFunctionDeclScope final : public ASTScopeImpl {
10371005

10381006
NullablePtr<const void> getReferrent() const override;
10391007

1040-
static bool shouldCreateAccessorScope(const AccessorDecl *);
1041-
10421008
protected:
10431009
SourceRange
10441010
getSourceRangeOfEnclosedParamsOfASTNode(bool omitAssertions) const override;
@@ -1115,7 +1081,6 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
11151081
}
11161082
virtual NullablePtr<Decl> getDeclIfAny() const override { return decl; }
11171083
Decl *getDecl() const { return decl; }
1118-
static bool isAMethod(const AbstractFunctionDecl *);
11191084

11201085
NullablePtr<ASTScopeImpl> getParentOfASTAncestorScopesToBeRescued() override;
11211086

@@ -1130,21 +1095,10 @@ class AbstractFunctionBodyScope : public ASTScopeImpl {
11301095
SourceRange sourceRangeForDeferredExpansion() const override;
11311096
};
11321097

1133-
/// Body of methods, functions in types.
1134-
class MethodBodyScope final : public AbstractFunctionBodyScope {
1098+
/// Body of functions and methods.
1099+
class FunctionBodyScope final : public AbstractFunctionBodyScope {
11351100
public:
1136-
MethodBodyScope(AbstractFunctionDecl *e) : AbstractFunctionBodyScope(e) {}
1137-
std::string getClassName() const override;
1138-
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
1139-
DeclConsumer consumer) const override;
1140-
1141-
Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const override;
1142-
};
1143-
1144-
/// Body of "pure" functions, functions without an implicit "self".
1145-
class PureFunctionBodyScope final : public AbstractFunctionBodyScope {
1146-
public:
1147-
PureFunctionBodyScope(AbstractFunctionDecl *e)
1101+
FunctionBodyScope(AbstractFunctionDecl *e)
11481102
: AbstractFunctionBodyScope(e) {}
11491103
std::string getClassName() const override;
11501104
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
@@ -1317,8 +1271,6 @@ class PatternEntryInitializerScope final : public AbstractPatternEntryScope {
13171271
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
13181272
virtual NullablePtr<DeclContext> getDeclContext() const override;
13191273

1320-
Optional<NullablePtr<DeclContext>> computeSelfDCForParent() const override;
1321-
13221274
protected:
13231275
bool lookupLocalsOrMembers(ArrayRef<const ASTScopeImpl *>,
13241276
DeclConsumer) const override;
@@ -1430,13 +1382,6 @@ class ClosureParametersScope final : public ASTScopeImpl {
14301382
std::string getClassName() const override;
14311383
SourceRange
14321384
getSourceRangeOfThisASTNode(bool omitAssertions = false) const override;
1433-
1434-
/// Since explicit captures of \c self by closures enable the use of implicit
1435-
/// \c self, we need to make sure that the appropriate \c self is used as the
1436-
/// base decl for these uses (otherwise, the capture would be marked as
1437-
/// unused. \c ClosureParametersScope::capturedSelfDC() checks if we have such
1438-
/// a capture of self.
1439-
NullablePtr<DeclContext> capturedSelfDC() const override;
14401385

14411386
NullablePtr<ClosureExpr> getClosureIfClosureScope() const override {
14421387
return closureExpr;

include/swift/AST/Decl.h

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace swift {
6262
class DynamicSelfType;
6363
class Type;
6464
class Expr;
65+
class CaptureListExpr;
6566
class DeclRefExpr;
6667
class ForeignAsyncConvention;
6768
class ForeignErrorConvention;
@@ -352,21 +353,13 @@ class alignas(1 << DeclAlignInBits) Decl {
352353
IsStatic : 1
353354
);
354355

355-
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 1+1+1+1+1+1+1+1,
356+
SWIFT_INLINE_BITFIELD(VarDecl, AbstractStorageDecl, 1+1+1+1+1+1,
356357
/// Encodes whether this is a 'let' binding.
357358
Introducer : 1,
358359

359-
/// Whether this declaration was an element of a capture list.
360-
IsCaptureList : 1,
361-
362360
/// Whether this declaration captures the 'self' param under the same name.
363361
IsSelfParamCapture : 1,
364362

365-
/// Whether this vardecl has an initial value bound to it in a way
366-
/// that isn't represented in the AST with an initializer in the pattern
367-
/// binding. This happens in cases like "for i in ...", switch cases, etc.
368-
HasNonPatternBindingInit : 1,
369-
370363
/// Whether this is a property used in expressions in the debugger.
371364
/// It is up to the debugger to instruct SIL how to access this variable.
372365
IsDebuggerVar : 1,
@@ -4892,16 +4885,19 @@ class VarDecl : public AbstractStorageDecl {
48924885
};
48934886

48944887
protected:
4895-
PointerUnion<PatternBindingDecl *, Stmt *, VarDecl *> Parent;
4888+
PointerUnion<PatternBindingDecl *,
4889+
Stmt *,
4890+
VarDecl *,
4891+
CaptureListExpr *> Parent;
48964892

48974893
VarDecl(DeclKind kind, bool isStatic, Introducer introducer,
4898-
bool isCaptureList, SourceLoc nameLoc, Identifier name,
4899-
DeclContext *dc, StorageIsMutable_t supportsMutation);
4894+
SourceLoc nameLoc, Identifier name, DeclContext *dc,
4895+
StorageIsMutable_t supportsMutation);
49004896

49014897
public:
4902-
VarDecl(bool isStatic, Introducer introducer, bool isCaptureList,
4898+
VarDecl(bool isStatic, Introducer introducer,
49034899
SourceLoc nameLoc, Identifier name, DeclContext *dc)
4904-
: VarDecl(DeclKind::Var, isStatic, introducer, isCaptureList, nameLoc,
4900+
: VarDecl(DeclKind::Var, isStatic, introducer, nameLoc,
49054901
name, dc, StorageIsMutable_t(introducer == Introducer::Var)) {}
49064902

49074903
SourceRange getSourceRange() const;
@@ -5091,25 +5087,29 @@ class VarDecl : public AbstractStorageDecl {
50915087
Bits.VarDecl.Introducer = uint8_t(value);
50925088
}
50935089

5090+
CaptureListExpr *getParentCaptureList() const {
5091+
if (!Parent)
5092+
return nullptr;
5093+
return Parent.dyn_cast<CaptureListExpr *>();
5094+
}
5095+
5096+
/// Set \p v to be the pattern produced VarDecl that is the parent of this
5097+
/// var decl.
5098+
void setParentCaptureList(CaptureListExpr *expr) {
5099+
assert(expr != nullptr);
5100+
Parent = expr;
5101+
}
50945102
/// Is this an element in a capture list?
5095-
bool isCaptureList() const { return Bits.VarDecl.IsCaptureList; }
5103+
bool isCaptureList() const {
5104+
return getParentCaptureList() != nullptr;
5105+
}
50965106

50975107
/// Is this a capture of the self param?
50985108
bool isSelfParamCapture() const { return Bits.VarDecl.IsSelfParamCapture; }
50995109
void setIsSelfParamCapture(bool IsSelfParamCapture = true) {
51005110
Bits.VarDecl.IsSelfParamCapture = IsSelfParamCapture;
51015111
}
51025112

5103-
/// Return true if this vardecl has an initial value bound to it in a way
5104-
/// that isn't represented in the AST with an initializer in the pattern
5105-
/// binding. This happens in cases like "for i in ...", switch cases, etc.
5106-
bool hasNonPatternBindingInit() const {
5107-
return Bits.VarDecl.HasNonPatternBindingInit;
5108-
}
5109-
void setHasNonPatternBindingInit(bool V = true) {
5110-
Bits.VarDecl.HasNonPatternBindingInit = V;
5111-
}
5112-
51135113
/// Determines if this var has an initializer expression that should be
51145114
/// exposed to clients.
51155115
/// There's a very narrow case when we would: if the decl is an instance

include/swift/AST/Expr.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3941,12 +3941,6 @@ class ClosureExpr : public AbstractClosureExpr {
39413941
VarDecl *getCapturedSelfDecl() const {
39423942
return CapturedSelfDecl;
39433943
}
3944-
3945-
/// Whether this closure captures the \c self param in its body in such a
3946-
/// way that implicit \c self is enabled within its body (i.e. \c self is
3947-
/// captured non-weakly).
3948-
bool capturesSelfEnablingImplictSelf() const;
3949-
39503944

39513945
/// Get the type checking state of this closure's body.
39523946
BodyState getBodyState() const {

include/swift/AST/NameLookup.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ class AbstractASTScopeDeclConsumer {
633633

634634
/// Eventually this functionality should move into ASTScopeLookup
635635
virtual bool
636-
lookInMembers(NullablePtr<DeclContext> selfDC, DeclContext *const scopeDC,
636+
lookInMembers(DeclContext *const scopeDC,
637637
NominalTypeDecl *const nominal,
638638
function_ref<bool(Optional<bool>)> calculateIsCascadingUse) = 0;
639639

@@ -656,7 +656,7 @@ class ASTScopeDeclGatherer : public AbstractASTScopeDeclConsumer {
656656
NullablePtr<DeclContext> baseDC = nullptr) override;
657657

658658
/// Eventually this functionality should move into ASTScopeLookup
659-
bool lookInMembers(NullablePtr<DeclContext>, DeclContext *const,
659+
bool lookInMembers(DeclContext *const,
660660
NominalTypeDecl *const,
661661
function_ref<bool(Optional<bool>)>) override {
662662
return false;

include/swift/AST/Pattern.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@ class alignas(8) Pattern {
190190

191191
bool isNeverDefaultInitializable() const;
192192

193-
/// Mark all vardecls in this pattern as having non-pattern initial
194-
/// values bound into them.
195-
void markHasNonPatternBindingInit() {
196-
forEachVariable([&](VarDecl *VD) {
197-
VD->setHasNonPatternBindingInit();
198-
});
199-
}
200-
201193
/// Mark all vardecls in this pattern as having an owning statement for
202194
/// the pattern.
203195
void markOwnedByStatement(Stmt *S) {

include/swift/AST/TypeAlignments.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace swift {
3232
class ASTContext;
3333
class AttributeBase;
3434
class BraceStmt;
35+
class CaptureListExpr;
3536
class Decl;
3637
class DeclContext;
3738
class Expr;
@@ -112,6 +113,7 @@ LLVM_DECLARE_TYPE_ALIGNMENT(swift::BraceStmt, swift::StmtAlignInBits)
112113
LLVM_DECLARE_TYPE_ALIGNMENT(swift::ASTContext, 2);
113114
LLVM_DECLARE_TYPE_ALIGNMENT(swift::DeclContext, swift::DeclContextAlignInBits)
114115
LLVM_DECLARE_TYPE_ALIGNMENT(swift::Expr, swift::ExprAlignInBits)
116+
LLVM_DECLARE_TYPE_ALIGNMENT(swift::CaptureListExpr, swift::ExprAlignInBits)
115117
LLVM_DECLARE_TYPE_ALIGNMENT(swift::AbstractClosureExpr, swift::ExprAlignInBits)
116118
LLVM_DECLARE_TYPE_ALIGNMENT(swift::OpaqueValueExpr, swift::ExprAlignInBits)
117119
LLVM_DECLARE_TYPE_ALIGNMENT(swift::ProtocolConformance, swift::DeclAlignInBits)

lib/AST/ASTDumper.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,6 @@ namespace {
842842
printCommon(VD, "var_decl");
843843
if (VD->isLet())
844844
PrintWithColorRAII(OS, DeclModifierColor) << " let";
845-
if (VD->hasNonPatternBindingInit())
846-
PrintWithColorRAII(OS, DeclModifierColor) << " non_pattern_init";
847845
if (VD->getAttrs().hasAttribute<LazyAttr>())
848846
PrintWithColorRAII(OS, DeclModifierColor) << " lazy";
849847
printStorageImpl(VD);

lib/AST/ASTScope.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,6 @@ Stmt *LabeledConditionalStmtScope::getStmt() const {
118118
return getLabeledConditionalStmt();
119119
}
120120

121-
bool AbstractFunctionBodyScope::isAMethod(
122-
const AbstractFunctionDecl *const afd) {
123-
// What makes this interesting is that a method named "init" which is not
124-
// in a nominal type or extension decl body still gets an implicit self
125-
// parameter (even though the program is illegal).
126-
// So when choosing between creating a MethodBodyScope and a
127-
// PureFunctionBodyScope do we go by the enclosing Decl (i.e.
128-
// "afd->getDeclContext()->isTypeContext()") or by
129-
// "bool(afd->getImplicitSelfDecl())"?
130-
//
131-
// Since the code uses \c getImplicitSelfDecl, use that.
132-
return afd->getImplicitSelfDecl();
133-
}
134-
135121
#pragma mark getLabeledConditionalStmt
136122
LabeledConditionalStmt *IfStmtScope::getLabeledConditionalStmt() const {
137123
return stmt;
@@ -218,8 +204,7 @@ DEFINE_GET_CLASS_NAME(ASTSourceFileScope)
218204
DEFINE_GET_CLASS_NAME(GenericParamScope)
219205
DEFINE_GET_CLASS_NAME(AbstractFunctionDeclScope)
220206
DEFINE_GET_CLASS_NAME(ParameterListScope)
221-
DEFINE_GET_CLASS_NAME(MethodBodyScope)
222-
DEFINE_GET_CLASS_NAME(PureFunctionBodyScope)
207+
DEFINE_GET_CLASS_NAME(FunctionBodyScope)
223208
DEFINE_GET_CLASS_NAME(DefaultArgumentInitializerScope)
224209
DEFINE_GET_CLASS_NAME(AttachedPropertyWrapperScope)
225210
DEFINE_GET_CLASS_NAME(PatternEntryDeclScope)

lib/AST/ASTScopeCreation.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1416,12 +1416,8 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
14161416
// We create body scopes when there is no body for source kit to complete
14171417
// erroneous code in bodies.
14181418
if (decl->getBodySourceRange().isValid()) {
1419-
if (AbstractFunctionBodyScope::isAMethod(decl))
1420-
scopeCreator.constructExpandAndInsertUncheckable<MethodBodyScope>(leaf,
1419+
scopeCreator.constructExpandAndInsertUncheckable<FunctionBodyScope>(leaf,
14211420
decl);
1422-
else
1423-
scopeCreator.constructExpandAndInsertUncheckable<PureFunctionBodyScope>(
1424-
leaf, decl);
14251421
}
14261422
}
14271423

@@ -2004,11 +2000,6 @@ ASTScopeImpl::rescueASTAncestorScopesForReuseFromMe() {
20042000
return astAncestorScopes;
20052001
}
20062002

2007-
bool AbstractFunctionDeclScope::shouldCreateAccessorScope(
2008-
const AccessorDecl *const ad) {
2009-
return isLocalizable(ad);
2010-
}
2011-
20122003
#pragma mark verification
20132004

20142005
namespace {

0 commit comments

Comments
 (0)