Skip to content

Commit e2fbe3a

Browse files
committed
[CS] Pass SelectedOverload in a couple of places
Rather than passing separate parameters for the choice, openedType, and openedFullType, just pass the selected overload.
1 parent bfdc2c7 commit e2fbe3a

File tree

1 file changed

+46
-58
lines changed

1 file changed

+46
-58
lines changed

lib/Sema/CSApply.cpp

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,13 @@ namespace {
384384

385385
public:
386386
/// Build a reference to the given declaration.
387-
Expr *buildDeclRef(OverloadChoice choice, DeclNameLoc loc,
388-
Type openedFullType, ConstraintLocatorBuilder locator,
389-
bool implicit, AccessSemantics semantics) {
387+
Expr *buildDeclRef(SelectedOverload overload, DeclNameLoc loc,
388+
ConstraintLocatorBuilder locator, bool implicit,
389+
AccessSemantics semantics) {
390+
auto choice = overload.choice;
390391
assert(choice.getKind() != OverloadChoiceKind::DeclViaDynamic);
391392
auto *decl = choice.getDecl();
393+
auto fullType = simplifyType(overload.openedFullType);
392394

393395
// Determine the declaration selected for this overloaded reference.
394396
auto &ctx = cs.getASTContext();
@@ -398,10 +400,7 @@ namespace {
398400
if (decl->getDeclContext()->isTypeContext() && isa<FuncDecl>(decl)) {
399401
assert(cast<FuncDecl>(decl)->isOperator() && "Must be an operator");
400402

401-
auto openedFnType = openedFullType->castTo<FunctionType>();
402-
auto simplifiedFnType
403-
= simplifyType(openedFnType)->castTo<FunctionType>();
404-
auto baseTy = getBaseType(simplifiedFnType);
403+
auto baseTy = getBaseType(fullType->castTo<FunctionType>());
405404

406405
// Handle operator requirements found in protocols.
407406
if (auto proto = dyn_cast<ProtocolDecl>(decl->getDeclContext())) {
@@ -423,7 +422,10 @@ namespace {
423422
// FIXME: the hop through 'getDecl()' is because
424423
// SpecializedProtocolConformance doesn't substitute into
425424
// witnesses' ConcreteDeclRefs.
426-
Type expectedFnType = simplifiedFnType->getResult();
425+
Type expectedFnType = simplifyType(overload.openedType);
426+
assert(expectedFnType->isEqual(
427+
fullType->castTo<AnyFunctionType>()->getResult()) &&
428+
"Cannot handle adjustments made to the opened type");
427429
Expr *refExpr;
428430
if (witness->getDeclContext()->isTypeContext()) {
429431
Expr *base =
@@ -463,24 +465,20 @@ namespace {
463465
TypeExpr::createImplicitHack(loc.getBaseNameLoc(), baseTy, ctx);
464466
cs.cacheExprTypes(base);
465467

466-
return buildMemberRef(base, openedFullType, SourceLoc(), choice, loc,
467-
openedFnType->getResult(), locator, locator,
468-
implicit, semantics);
468+
return buildMemberRef(base, SourceLoc(), overload, loc, locator,
469+
locator, implicit, semantics);
469470
}
470471

471-
auto type = solution.simplifyType(openedFullType);
472-
473472
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
474473
auto typeExpr = TypeExpr::createImplicitHack(
475-
loc.getBaseNameLoc(), type->getMetatypeInstanceType(),
476-
ctx);
474+
loc.getBaseNameLoc(), fullType->getMetatypeInstanceType(), ctx);
477475
cs.cacheType(typeExpr);
478476
return typeExpr;
479477
}
480478

481479
auto ref = resolveConcreteDeclRef(decl, locator);
482480
auto declRefExpr =
483-
new (ctx) DeclRefExpr(ref, loc, implicit, semantics, type);
481+
new (ctx) DeclRefExpr(ref, loc, implicit, semantics, fullType);
484482
cs.cacheType(declRefExpr);
485483
declRefExpr->setFunctionRefKind(choice.getFunctionRefKind());
486484
return forceUnwrapIfExpected(declRefExpr, choice, locator);
@@ -733,11 +731,15 @@ namespace {
733731
}
734732

735733
/// Build a new member reference with the given base and member.
736-
Expr *buildMemberRef(Expr *base, Type openedFullType, SourceLoc dotLoc,
737-
OverloadChoice choice, DeclNameLoc memberLoc,
738-
Type openedType, ConstraintLocatorBuilder locator,
734+
Expr *buildMemberRef(Expr *base, SourceLoc dotLoc,
735+
SelectedOverload overload, DeclNameLoc memberLoc,
736+
ConstraintLocatorBuilder locator,
739737
ConstraintLocatorBuilder memberLocator, bool Implicit,
740738
AccessSemantics semantics) {
739+
auto choice = overload.choice;
740+
auto openedType = overload.openedType;
741+
auto openedFullType = overload.openedFullType;
742+
741743
ValueDecl *member = choice.getDecl();
742744

743745
auto &context = cs.getASTContext();
@@ -2271,9 +2273,8 @@ namespace {
22712273
return expr;
22722274
}
22732275

2274-
return buildDeclRef(selected->choice, expr->getNameLoc(),
2275-
selected->openedFullType, locator, expr->isImplicit(),
2276-
expr->getAccessSemantics());
2276+
return buildDeclRef(*selected, expr->getNameLoc(), locator,
2277+
expr->isImplicit(), expr->getAccessSemantics());
22772278
}
22782279

22792280
Expr *visitSuperRefExpr(SuperRefExpr *expr) {
@@ -2301,11 +2302,9 @@ namespace {
23012302
// Determine the declaration selected for this overloaded reference.
23022303
auto locator = cs.getConstraintLocator(expr);
23032304
auto selected = solution.getOverloadChoice(locator);
2304-
auto choice = selected.choice;
23052305

2306-
return buildDeclRef(choice, expr->getNameLoc(), selected.openedFullType,
2307-
locator, expr->isImplicit(),
2308-
AccessSemantics::Ordinary);
2306+
return buildDeclRef(selected, expr->getNameLoc(), locator,
2307+
expr->isImplicit(), AccessSemantics::Ordinary);
23092308
}
23102309

23112310
Expr *visitUnresolvedDeclRefExpr(UnresolvedDeclRefExpr *expr) {
@@ -2324,8 +2323,7 @@ namespace {
23242323
ConstraintLocator::Member);
23252324
auto selected = solution.getOverloadChoice(memberLocator);
23262325
return buildMemberRef(
2327-
expr->getBase(), selected.openedFullType, expr->getDotLoc(),
2328-
selected.choice, expr->getNameLoc(), selected.openedType,
2326+
expr->getBase(), expr->getDotLoc(), selected, expr->getNameLoc(),
23292327
cs.getConstraintLocator(expr), memberLocator, expr->isImplicit(),
23302328
expr->getAccessSemantics());
23312329
}
@@ -2369,10 +2367,8 @@ namespace {
23692367
// Build the member reference.
23702368
auto *exprLoc = cs.getConstraintLocator(expr);
23712369
auto result = buildMemberRef(
2372-
base, selected.openedFullType, expr->getDotLoc(), selected.choice,
2373-
expr->getNameLoc(), selected.openedType,
2374-
exprLoc, memberLocator, expr->isImplicit(),
2375-
AccessSemantics::Ordinary);
2370+
base, expr->getDotLoc(), selected, expr->getNameLoc(), exprLoc,
2371+
memberLocator, expr->isImplicit(), AccessSemantics::Ordinary);
23762372
if (!result)
23772373
return nullptr;
23782374

@@ -2511,16 +2507,16 @@ namespace {
25112507
Expr *applyCtorRefExpr(Expr *expr, Expr *base, SourceLoc dotLoc,
25122508
DeclNameLoc nameLoc, bool implicit,
25132509
ConstraintLocator *ctorLocator,
2514-
OverloadChoice choice, Type openedFullType) {
2510+
SelectedOverload overload) {
2511+
auto choice = overload.choice;
25152512
assert(choice.getKind() != OverloadChoiceKind::DeclViaDynamic);
25162513
auto *ctor = cast<ConstructorDecl>(choice.getDecl());
25172514

25182515
// If the subexpression is a metatype, build a direct reference to the
25192516
// constructor.
25202517
if (cs.getType(base)->is<AnyMetatypeType>()) {
25212518
return buildMemberRef(
2522-
base, openedFullType, dotLoc, choice, nameLoc, cs.getType(expr),
2523-
ConstraintLocatorBuilder(cs.getConstraintLocator(expr)),
2519+
base, dotLoc, overload, nameLoc, cs.getConstraintLocator(expr),
25242520
ctorLocator, implicit, AccessSemantics::Ordinary);
25252521
}
25262522

@@ -2565,8 +2561,9 @@ namespace {
25652561

25662562
// Build a partial application of the delegated initializer.
25672563
auto callee = resolveConcreteDeclRef(ctor, ctorLocator);
2568-
Expr *ctorRef = buildOtherConstructorRef(openedFullType, callee, base,
2569-
nameLoc, ctorLocator, implicit);
2564+
Expr *ctorRef = buildOtherConstructorRef(overload.openedFullType, callee,
2565+
base, nameLoc, ctorLocator,
2566+
implicit);
25702567
auto *call = new (cs.getASTContext()) DotSyntaxCallExpr(ctorRef, dotLoc,
25712568
base);
25722569

@@ -2582,10 +2579,8 @@ namespace {
25822579
expr,
25832580
ConstraintLocator::ConstructorMember);
25842581
if (auto selected = solution.getOverloadChoiceIfAvailable(ctorLocator)) {
2585-
auto choice = selected->choice;
25862582
return applyCtorRefExpr(
2587-
expr, base, dotLoc, nameLoc, implicit, ctorLocator, choice,
2588-
selected->openedFullType);
2583+
expr, base, dotLoc, nameLoc, implicit, ctorLocator, *selected);
25892584
}
25902585

25912586
// Determine the declaration selected for this overloaded reference.
@@ -2613,8 +2608,7 @@ namespace {
26132608
cs.diagnoseDeprecatedConditionalConformanceOuterAccess(
26142609
UDE, selected.choice.getDecl());
26152610

2616-
return buildDeclRef(selected.choice, nameLoc, selected.openedFullType,
2617-
memberLocator, implicit,
2611+
return buildDeclRef(selected, nameLoc, memberLocator, implicit,
26182612
AccessSemantics::Ordinary);
26192613
}
26202614

@@ -2645,8 +2639,7 @@ namespace {
26452639
case OverloadChoiceKind::Decl:
26462640
case OverloadChoiceKind::DeclViaUnwrappedOptional:
26472641
case OverloadChoiceKind::DeclViaDynamic:
2648-
return buildMemberRef(base, selected.openedFullType, dotLoc,
2649-
selected.choice, nameLoc, selected.openedType,
2642+
return buildMemberRef(base, dotLoc, selected, nameLoc,
26502643
cs.getConstraintLocator(expr), memberLocator,
26512644
implicit, AccessSemantics::Ordinary);
26522645

@@ -6478,12 +6471,11 @@ static Expr *buildCallAsFunctionMethodRef(
64786471
ExprRewriter &rewriter, ApplyExpr *apply, SelectedOverload selected,
64796472
ConstraintLocatorBuilder applyFunctionLoc) {
64806473
auto *fn = apply->getFn();
6481-
auto choice = selected.choice;
64826474
// Create direct reference to `callAsFunction` method.
64836475
auto *declRef = rewriter.buildMemberRef(
6484-
fn, selected.openedFullType, /*dotLoc*/ SourceLoc(), choice,
6485-
DeclNameLoc(fn->getEndLoc()), selected.openedType, applyFunctionLoc,
6486-
applyFunctionLoc, /*implicit*/ true, AccessSemantics::Ordinary);
6476+
fn, /*dotLoc*/ SourceLoc(), selected, DeclNameLoc(fn->getEndLoc()),
6477+
applyFunctionLoc, applyFunctionLoc, /*implicit*/ true,
6478+
AccessSemantics::Ordinary);
64876479
if (!declRef)
64886480
return nullptr;
64896481
declRef->setImplicit(apply->isImplicit());
@@ -6515,11 +6507,9 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
65156507
bool useKwargsMethod = argumentLabel == ctx.Id_withKeywordArguments;
65166508

65176509
// Construct expression referencing the `dynamicallyCall` method.
6518-
auto member = buildMemberRef(fn, selected.openedFullType,
6519-
SourceLoc(), selected.choice,
6520-
DeclNameLoc(method->getNameLoc()),
6521-
selected.openedType, loc, loc, /*implicit*/ true,
6522-
AccessSemantics::Ordinary);
6510+
auto member = buildMemberRef(fn, SourceLoc(), selected,
6511+
DeclNameLoc(method->getNameLoc()), loc, loc,
6512+
/*implicit*/ true, AccessSemantics::Ordinary);
65236513

65246514
// Construct argument to the method (either an array or dictionary
65256515
// expression).
@@ -6874,11 +6864,9 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
68746864
// Consider the constructor decl reference expr 'implicit', but the
68756865
// constructor call expr itself has the apply's 'implicitness'.
68766866
auto ctorRef = resolveConcreteDeclRef(choice.getDecl(), ctorLocator);
6877-
Expr *declRef = buildMemberRef(fn, selected->openedFullType,
6878-
/*dotLoc=*/SourceLoc(), choice,
6879-
DeclNameLoc(fn->getEndLoc()),
6880-
selected->openedType, locator, ctorLocator,
6881-
/*Implicit=*/true,
6867+
Expr *declRef = buildMemberRef(fn, /*dotLoc=*/SourceLoc(), *selected,
6868+
DeclNameLoc(fn->getEndLoc()), locator,
6869+
ctorLocator, /*Implicit=*/true,
68826870
AccessSemantics::Ordinary);
68836871
if (!declRef)
68846872
return nullptr;

0 commit comments

Comments
 (0)