Skip to content

Commit da20780

Browse files
authored
Merge pull request #16695 from davezarzycki/nfc_move_isSelfExpr
[AST] NFC: Make 'isSelfExpr()' available for general use
2 parents a617b14 + 43a457b commit da20780

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
@@ -549,6 +549,10 @@ class alignas(8) Expr {
549549
/// a builtin operator.
550550
bool isInfixOperator() const;
551551

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

lib/AST/Decl.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5468,21 +5468,6 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
54685468
DiagnosticEngine *diags)
54695469
: Decl(decl), Diags(diags) { }
54705470

5471-
bool isSelfExpr(Expr *E) {
5472-
E = E->getSemanticsProvidingExpr();
5473-
5474-
if (auto ATSE = dyn_cast<ArchetypeToSuperExpr>(E))
5475-
E = ATSE->getSubExpr();
5476-
if (auto IOE = dyn_cast<InOutExpr>(E))
5477-
E = IOE->getSubExpr();
5478-
if (auto LE = dyn_cast<LoadExpr>(E))
5479-
E = LE->getSubExpr();
5480-
if (auto DRE = dyn_cast<DeclRefExpr>(E))
5481-
return DRE->getDecl() == Decl->getImplicitSelfDecl();
5482-
5483-
return false;
5484-
}
5485-
54865471
bool walkToDeclPre(class Decl *D) override {
54875472
// Don't walk into further nominal decls.
54885473
return !isa<NominalTypeDecl>(D);
@@ -5520,7 +5505,7 @@ ConstructorDecl::getDelegatingOrChainedInitKind(DiagnosticEngine *diags,
55205505
BodyInitKind myKind;
55215506
if (arg->isSuperExpr())
55225507
myKind = BodyInitKind::Chained;
5523-
else if (isSelfExpr(arg))
5508+
else if (arg->isSelfExprOf(Decl, /*sameBase*/true))
55245509
myKind = BodyInitKind::Delegating;
55255510
else {
55265511
// We're constructing something else.

lib/AST/Expr.cpp

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

2177+
bool Expr::isSelfExprOf(const AbstractFunctionDecl *AFD, bool sameBase) const {
2178+
auto *E = getSemanticsProvidingExpr();
2179+
2180+
if (auto IOE = dyn_cast<InOutExpr>(E))
2181+
E = IOE->getSubExpr();
2182+
2183+
while (auto ICE = dyn_cast<ImplicitConversionExpr>(E)) {
2184+
if (sameBase && isa<DerivedToBaseExpr>(ICE))
2185+
return false;
2186+
E = ICE->getSubExpr();
2187+
}
2188+
2189+
if (auto DRE = dyn_cast<DeclRefExpr>(E))
2190+
return DRE->getDecl() == AFD->getImplicitSelfDecl();
2191+
2192+
return false;
2193+
}
21772194

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

0 commit comments

Comments
 (0)