Skip to content

Commit a840af3

Browse files
committed
Move getSelfForInitDelegationInConstructor to ASTContext
1 parent 36987e0 commit a840af3

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

include/swift/AST/ASTContext.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,13 @@ class ASTContext final {
917917
/// array literals exist.
918918
bool requireArrayLiteralIntrinsics(SourceLoc loc);
919919

920+
public:
921+
/// If an expression references 'self.init' or 'super.init' in an
922+
/// initializer context, returns the implicit 'self' decl of the constructor.
923+
/// Otherwise, return nil.
924+
VarDecl *getSelfForInitDelegationInConstructor(DeclContext *DC,
925+
UnresolvedDotExpr *ctorRef);
926+
920927
private:
921928
friend Decl;
922929
Optional<RawComment> getRawComment(const Decl *D);

lib/AST/ASTContext.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4646,6 +4646,30 @@ bool ASTContext::requireArrayLiteralIntrinsics(SourceLoc loc) {
46464646
return true;
46474647
}
46484648

4649+
/// If an expression references 'self.init' or 'super.init' in an
4650+
/// initializer context, returns the implicit 'self' decl of the constructor.
4651+
/// Otherwise, return nil.
4652+
VarDecl *
4653+
ASTContext::getSelfForInitDelegationInConstructor(DeclContext *DC,
4654+
UnresolvedDotExpr *ctorRef) {
4655+
// If the reference isn't to a constructor, we're done.
4656+
if (ctorRef->getName().getBaseName() != DeclBaseName::createConstructor())
4657+
return nullptr;
4658+
4659+
if (auto ctorContext
4660+
= dyn_cast_or_null<ConstructorDecl>(DC->getInnermostMethodContext())) {
4661+
auto nestedArg = ctorRef->getBase();
4662+
if (auto inout = dyn_cast<InOutExpr>(nestedArg))
4663+
nestedArg = inout->getSubExpr();
4664+
if (nestedArg->isSuperExpr())
4665+
return ctorContext->getImplicitSelfDecl();
4666+
if (auto declRef = dyn_cast<DeclRefExpr>(nestedArg))
4667+
if (declRef->getDecl()->getFullName() == Id_self)
4668+
return ctorContext->getImplicitSelfDecl();
4669+
}
4670+
return nullptr;
4671+
}
4672+
46494673
VarDecl *VarDecl::getOriginalWrappedProperty(
46504674
Optional<PropertyWrapperSynthesizedPropertyKind> kind) const {
46514675
if (!Bits.VarDecl.IsPropertyWrapperBackingProperty)

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ ConstraintSystem::getCalleeLocator(ConstraintLocator *locator,
482482

483483
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(anchor)) {
484484
return getConstraintLocator(
485-
anchor, TC.getSelfForInitDelegationInConstructor(DC, UDE)
485+
anchor, getASTContext().getSelfForInitDelegationInConstructor(DC, UDE)
486486
? ConstraintLocator::ConstructorMember
487487
: ConstraintLocator::Member);
488488
}

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -802,30 +802,6 @@ Expr *TypeChecker::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE,
802802
return new (Context) ErrorExpr(UDRE->getSourceRange());
803803
}
804804

805-
/// If an expression references 'self.init' or 'super.init' in an
806-
/// initializer context, returns the implicit 'self' decl of the constructor.
807-
/// Otherwise, return nil.
808-
VarDecl *
809-
TypeChecker::getSelfForInitDelegationInConstructor(DeclContext *DC,
810-
UnresolvedDotExpr *ctorRef) {
811-
// If the reference isn't to a constructor, we're done.
812-
if (ctorRef->getName().getBaseName() != DeclBaseName::createConstructor())
813-
return nullptr;
814-
815-
if (auto ctorContext
816-
= dyn_cast_or_null<ConstructorDecl>(DC->getInnermostMethodContext())) {
817-
auto nestedArg = ctorRef->getBase();
818-
if (auto inout = dyn_cast<InOutExpr>(nestedArg))
819-
nestedArg = inout->getSubExpr();
820-
if (nestedArg->isSuperExpr())
821-
return ctorContext->getImplicitSelfDecl();
822-
if (auto declRef = dyn_cast<DeclRefExpr>(nestedArg))
823-
if (declRef->getDecl()->getFullName() == Context.Id_self)
824-
return ctorContext->getImplicitSelfDecl();
825-
}
826-
return nullptr;
827-
}
828-
829805
namespace {
830806
/// Update the function reference kind based on adding a direct call to a
831807
/// callee with this kind.
@@ -1236,9 +1212,10 @@ namespace {
12361212
// determine where to place the RebindSelfInConstructorExpr node.
12371213
// When updating this logic, also update
12381214
// RebindSelfInConstructorExpr::getCalledConstructor.
1215+
auto &ctx = getASTContext();
12391216
if (auto unresolvedDot = dyn_cast<UnresolvedDotExpr>(expr)) {
12401217
if (auto self
1241-
= TC.getSelfForInitDelegationInConstructor(DC, unresolvedDot)) {
1218+
= ctx.getSelfForInitDelegationInConstructor(DC, unresolvedDot)) {
12421219
// Walk our ancestor expressions looking for the appropriate place
12431220
// to insert the RebindSelfInConstructorExpr.
12441221
Expr *target = nullptr;
@@ -1295,7 +1272,7 @@ namespace {
12951272
// RebindSelfInConstructorExpr, wrap it in the
12961273
// RebindSelfInConstructorExpr.
12971274
if (expr == UnresolvedCtorRebindTarget) {
1298-
expr = new (getASTContext())
1275+
expr = new (ctx)
12991276
RebindSelfInConstructorExpr(expr, UnresolvedCtorSelf);
13001277
UnresolvedCtorRebindTarget = nullptr;
13011278
return expr;

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,12 +1840,6 @@ class TypeChecker final {
18401840
return DiagnosedExprs[E];
18411841
}
18421842

1843-
/// If an expression references 'self.init' or 'super.init' in an
1844-
/// initializer context, returns the implicit 'self' decl of the constructor.
1845-
/// Otherwise, return nil.
1846-
VarDecl *getSelfForInitDelegationInConstructor(DeclContext *DC,
1847-
UnresolvedDotExpr *ctorRef);
1848-
18491843
/// Diagnose assigning variable to itself.
18501844
bool diagnoseSelfAssignment(const Expr *E);
18511845

0 commit comments

Comments
 (0)