Skip to content

Commit 52615ea

Browse files
committed
[NFC] PreCheckExpr: Simplify RebindSelfInConstructorExpr insertion point computation
1 parent 6d66211 commit 52615ea

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed

lib/Sema/PreCheckExpr.cpp

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,10 +1184,6 @@ namespace {
11841184
assert(ExprStack.back() == expr);
11851185
ExprStack.pop_back();
11861186

1187-
// Mark the direct callee as being a callee.
1188-
if (auto *call = dyn_cast<ApplyExpr>(expr))
1189-
markDirectCallee(call->getFn());
1190-
11911187
// Fold sequence expressions.
11921188
if (auto *seqExpr = dyn_cast<SequenceExpr>(expr)) {
11931189
auto result = TypeChecker::foldSequence(seqExpr, DC);
@@ -1215,66 +1211,55 @@ namespace {
12151211
if (isa<SingleValueStmtExpr>(expr))
12161212
SingleValueStmtExprDepth -= 1;
12171213

1218-
// A 'self.init' or 'super.init' application inside a constructor will
1219-
// evaluate to void, with the initializer's result implicitly rebound
1220-
// to 'self'. Recognize the unresolved constructor expression and
1221-
// determine where to place the RebindSelfInConstructorExpr node.
1222-
// When updating this logic, also update
1223-
// RebindSelfInConstructorExpr::getCalledConstructor.
1224-
auto &ctx = getASTContext();
1225-
if (auto unresolvedDot = dyn_cast<UnresolvedDotExpr>(expr)) {
1226-
if (auto self = TypeChecker::getSelfForInitDelegationInConstructor(
1227-
DC, unresolvedDot)) {
1214+
if (auto *apply = dyn_cast<ApplyExpr>(expr)) {
1215+
// Mark the direct callee as being a callee.
1216+
markDirectCallee(apply->getFn());
1217+
1218+
// A 'self.init' or 'super.init' application inside a constructor will
1219+
// evaluate to void, with the initializer's result implicitly rebound
1220+
// to 'self'. Recognize the unresolved constructor expression and
1221+
// determine where to place the RebindSelfInConstructorExpr node.
1222+
//
1223+
// When updating this logic, also may need to also update
1224+
// RebindSelfInConstructorExpr::getCalledConstructor.
1225+
VarDecl *self = nullptr;
1226+
if (auto *unresolvedDot =
1227+
dyn_cast<UnresolvedDotExpr>(apply->getSemanticFn())) {
1228+
self = TypeChecker::getSelfForInitDelegationInConstructor(
1229+
DC, unresolvedDot);
1230+
}
1231+
1232+
if (self) {
12281233
// Walk our ancestor expressions looking for the appropriate place
12291234
// to insert the RebindSelfInConstructorExpr.
1230-
Expr *target = nullptr;
1231-
bool foundApply = false;
1232-
bool foundRebind = false;
1235+
Expr *target = apply;
12331236
for (auto ancestor : llvm::reverse(ExprStack)) {
1234-
if (isa<RebindSelfInConstructorExpr>(ancestor)) {
1235-
// If we already have a rebind, then we're re-typechecking an
1236-
// expression and are done.
1237-
foundRebind = true;
1238-
break;
1239-
}
1240-
1241-
// Recognize applications.
1242-
if (auto apply = dyn_cast<ApplyExpr>(ancestor)) {
1243-
// If we already saw an application, we're done.
1244-
if (foundApply)
1245-
break;
1246-
1247-
// If the function being called is not our unresolved initializer
1248-
// reference, we're done.
1249-
if (apply->getSemanticFn() != unresolvedDot)
1250-
break;
1251-
1252-
foundApply = true;
1237+
if (isa<IdentityExpr>(ancestor) || isa<ForceValueExpr>(ancestor) ||
1238+
isa<AnyTryExpr>(ancestor)) {
12531239
target = ancestor;
12541240
continue;
12551241
}
12561242

1257-
// Look through identity, force-value, and 'try' expressions.
1258-
if (isa<IdentityExpr>(ancestor) ||
1259-
isa<ForceValueExpr>(ancestor) ||
1260-
isa<AnyTryExpr>(ancestor)) {
1261-
if (target)
1262-
target = ancestor;
1263-
continue;
1243+
if (isa<RebindSelfInConstructorExpr>(ancestor)) {
1244+
// If we already have a rebind, then we're re-typechecking an
1245+
// expression and are done.
1246+
target = nullptr;
12641247
}
12651248

12661249
// No other expression kinds are permitted.
12671250
break;
12681251
}
12691252

12701253
// If we found a rebind target, note the insertion point.
1271-
if (target && !foundRebind) {
1254+
if (target) {
12721255
UnresolvedCtorRebindTarget = target;
12731256
UnresolvedCtorSelf = self;
12741257
}
12751258
}
12761259
}
12771260

1261+
auto &ctx = getASTContext();
1262+
12781263
// If the expression we've found is the intended target of an
12791264
// RebindSelfInConstructorExpr, wrap it in the
12801265
// RebindSelfInConstructorExpr.

0 commit comments

Comments
 (0)