Skip to content

Commit 43a457b

Browse files
committed
[AST] NFC: Make 'isSelfExpr()' available for general use
Also, generalize the stripping of implicit conversions.
1 parent 8db3368 commit 43a457b

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

include/swift/AST/Expr.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,10 @@ class alignas(8) Expr {
547547
/// a builtin operator.
548548
bool isInfixOperator() const;
549549

550+
/// Returns true if this is a reference to the implicit self of function.
551+
bool isSelfExprOf(const AbstractFunctionDecl *AFD,
552+
bool sameBase = false) const;
553+
550554
/// Produce a mapping from each subexpression to its parent
551555
/// expression, with the provided expression serving as the root of
552556
/// the parent map.

lib/AST/Decl.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5465,21 +5465,6 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
54655465
DiagnosticEngine *diags)
54665466
: Decl(decl), Diags(diags) { }
54675467

5468-
bool isSelfExpr(Expr *E) {
5469-
E = E->getSemanticsProvidingExpr();
5470-
5471-
if (auto ATSE = dyn_cast<ArchetypeToSuperExpr>(E))
5472-
E = ATSE->getSubExpr();
5473-
if (auto IOE = dyn_cast<InOutExpr>(E))
5474-
E = IOE->getSubExpr();
5475-
if (auto LE = dyn_cast<LoadExpr>(E))
5476-
E = LE->getSubExpr();
5477-
if (auto DRE = dyn_cast<DeclRefExpr>(E))
5478-
return DRE->getDecl() == Decl->getImplicitSelfDecl();
5479-
5480-
return false;
5481-
}
5482-
54835468
bool walkToDeclPre(class Decl *D) override {
54845469
// Don't walk into further nominal decls.
54855470
return !isa<NominalTypeDecl>(D);
@@ -5517,7 +5502,7 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
55175502
BodyInitKind myKind;
55185503
if (arg->isSuperExpr())
55195504
myKind = BodyInitKind::Chained;
5520-
else if (isSelfExpr(arg))
5505+
else if (arg->isSelfExprOf(Decl, /*sameBase*/true))
55215506
myKind = BodyInitKind::Delegating;
55225507
else {
55235508
// We're constructing something else.

lib/AST/Expr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,23 @@ TypeExpr *TypeExpr::createImplicitHack(SourceLoc Loc, Type Ty, ASTContext &C) {
21642164
return Res;
21652165
}
21662166

2167+
bool Expr::isSelfExprOf(const AbstractFunctionDecl *AFD, bool sameBase) const {
2168+
auto *E = getSemanticsProvidingExpr();
2169+
2170+
if (auto IOE = dyn_cast<InOutExpr>(E))
2171+
E = IOE->getSubExpr();
2172+
2173+
while (auto ICE = dyn_cast<ImplicitConversionExpr>(E)) {
2174+
if (sameBase && isa<DerivedToBaseExpr>(ICE))
2175+
return false;
2176+
E = ICE->getSubExpr();
2177+
}
2178+
2179+
if (auto DRE = dyn_cast<DeclRefExpr>(E))
2180+
return DRE->getDecl() == AFD->getImplicitSelfDecl();
2181+
2182+
return false;
2183+
}
21672184

21682185
ArchetypeType *OpenExistentialExpr::getOpenedArchetype() const {
21692186
auto type = getOpaqueValue()->getType()->getRValueType();

0 commit comments

Comments
 (0)