@@ -384,11 +384,13 @@ namespace {
384
384
385
385
public:
386
386
// / 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 ;
390
391
assert (choice.getKind () != OverloadChoiceKind::DeclViaDynamic);
391
392
auto *decl = choice.getDecl ();
393
+ auto fullType = simplifyType (overload.openedFullType );
392
394
393
395
// Determine the declaration selected for this overloaded reference.
394
396
auto &ctx = cs.getASTContext ();
@@ -398,10 +400,7 @@ namespace {
398
400
if (decl->getDeclContext ()->isTypeContext () && isa<FuncDecl>(decl)) {
399
401
assert (cast<FuncDecl>(decl)->isOperator () && " Must be an operator" );
400
402
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>());
405
404
406
405
// Handle operator requirements found in protocols.
407
406
if (auto proto = dyn_cast<ProtocolDecl>(decl->getDeclContext ())) {
@@ -423,7 +422,10 @@ namespace {
423
422
// FIXME: the hop through 'getDecl()' is because
424
423
// SpecializedProtocolConformance doesn't substitute into
425
424
// 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" );
427
429
Expr *refExpr;
428
430
if (witness->getDeclContext ()->isTypeContext ()) {
429
431
Expr *base =
@@ -463,24 +465,20 @@ namespace {
463
465
TypeExpr::createImplicitHack (loc.getBaseNameLoc (), baseTy, ctx);
464
466
cs.cacheExprTypes (base);
465
467
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);
469
470
}
470
471
471
- auto type = solution.simplifyType (openedFullType);
472
-
473
472
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
474
473
auto typeExpr = TypeExpr::createImplicitHack (
475
- loc.getBaseNameLoc (), type->getMetatypeInstanceType (),
476
- ctx);
474
+ loc.getBaseNameLoc (), fullType->getMetatypeInstanceType (), ctx);
477
475
cs.cacheType (typeExpr);
478
476
return typeExpr;
479
477
}
480
478
481
479
auto ref = resolveConcreteDeclRef (decl, locator);
482
480
auto declRefExpr =
483
- new (ctx) DeclRefExpr (ref, loc, implicit, semantics, type );
481
+ new (ctx) DeclRefExpr (ref, loc, implicit, semantics, fullType );
484
482
cs.cacheType (declRefExpr);
485
483
declRefExpr->setFunctionRefKind (choice.getFunctionRefKind ());
486
484
return forceUnwrapIfExpected (declRefExpr, choice, locator);
@@ -733,11 +731,15 @@ namespace {
733
731
}
734
732
735
733
// / 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,
739
737
ConstraintLocatorBuilder memberLocator, bool Implicit,
740
738
AccessSemantics semantics) {
739
+ auto choice = overload.choice ;
740
+ auto openedType = overload.openedType ;
741
+ auto openedFullType = overload.openedFullType ;
742
+
741
743
ValueDecl *member = choice.getDecl ();
742
744
743
745
auto &context = cs.getASTContext ();
@@ -2271,9 +2273,8 @@ namespace {
2271
2273
return expr;
2272
2274
}
2273
2275
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 ());
2277
2278
}
2278
2279
2279
2280
Expr *visitSuperRefExpr (SuperRefExpr *expr) {
@@ -2301,11 +2302,9 @@ namespace {
2301
2302
// Determine the declaration selected for this overloaded reference.
2302
2303
auto locator = cs.getConstraintLocator (expr);
2303
2304
auto selected = solution.getOverloadChoice (locator);
2304
- auto choice = selected.choice ;
2305
2305
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);
2309
2308
}
2310
2309
2311
2310
Expr *visitUnresolvedDeclRefExpr (UnresolvedDeclRefExpr *expr) {
@@ -2324,8 +2323,7 @@ namespace {
2324
2323
ConstraintLocator::Member);
2325
2324
auto selected = solution.getOverloadChoice (memberLocator);
2326
2325
return buildMemberRef (
2327
- expr->getBase (), selected.openedFullType , expr->getDotLoc (),
2328
- selected.choice , expr->getNameLoc (), selected.openedType ,
2326
+ expr->getBase (), expr->getDotLoc (), selected, expr->getNameLoc (),
2329
2327
cs.getConstraintLocator (expr), memberLocator, expr->isImplicit (),
2330
2328
expr->getAccessSemantics ());
2331
2329
}
@@ -2369,10 +2367,8 @@ namespace {
2369
2367
// Build the member reference.
2370
2368
auto *exprLoc = cs.getConstraintLocator (expr);
2371
2369
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);
2376
2372
if (!result)
2377
2373
return nullptr ;
2378
2374
@@ -2511,16 +2507,16 @@ namespace {
2511
2507
Expr *applyCtorRefExpr (Expr *expr, Expr *base, SourceLoc dotLoc,
2512
2508
DeclNameLoc nameLoc, bool implicit,
2513
2509
ConstraintLocator *ctorLocator,
2514
- OverloadChoice choice, Type openedFullType) {
2510
+ SelectedOverload overload) {
2511
+ auto choice = overload.choice ;
2515
2512
assert (choice.getKind () != OverloadChoiceKind::DeclViaDynamic);
2516
2513
auto *ctor = cast<ConstructorDecl>(choice.getDecl ());
2517
2514
2518
2515
// If the subexpression is a metatype, build a direct reference to the
2519
2516
// constructor.
2520
2517
if (cs.getType (base)->is <AnyMetatypeType>()) {
2521
2518
return buildMemberRef (
2522
- base, openedFullType, dotLoc, choice, nameLoc, cs.getType (expr),
2523
- ConstraintLocatorBuilder (cs.getConstraintLocator (expr)),
2519
+ base, dotLoc, overload, nameLoc, cs.getConstraintLocator (expr),
2524
2520
ctorLocator, implicit, AccessSemantics::Ordinary);
2525
2521
}
2526
2522
@@ -2565,8 +2561,9 @@ namespace {
2565
2561
2566
2562
// Build a partial application of the delegated initializer.
2567
2563
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);
2570
2567
auto *call = new (cs.getASTContext ()) DotSyntaxCallExpr (ctorRef, dotLoc,
2571
2568
base);
2572
2569
@@ -2582,10 +2579,8 @@ namespace {
2582
2579
expr,
2583
2580
ConstraintLocator::ConstructorMember);
2584
2581
if (auto selected = solution.getOverloadChoiceIfAvailable (ctorLocator)) {
2585
- auto choice = selected->choice ;
2586
2582
return applyCtorRefExpr (
2587
- expr, base, dotLoc, nameLoc, implicit, ctorLocator, choice,
2588
- selected->openedFullType );
2583
+ expr, base, dotLoc, nameLoc, implicit, ctorLocator, *selected);
2589
2584
}
2590
2585
2591
2586
// Determine the declaration selected for this overloaded reference.
@@ -2613,8 +2608,7 @@ namespace {
2613
2608
cs.diagnoseDeprecatedConditionalConformanceOuterAccess (
2614
2609
UDE, selected.choice .getDecl ());
2615
2610
2616
- return buildDeclRef (selected.choice , nameLoc, selected.openedFullType ,
2617
- memberLocator, implicit,
2611
+ return buildDeclRef (selected, nameLoc, memberLocator, implicit,
2618
2612
AccessSemantics::Ordinary);
2619
2613
}
2620
2614
@@ -2645,8 +2639,7 @@ namespace {
2645
2639
case OverloadChoiceKind::Decl:
2646
2640
case OverloadChoiceKind::DeclViaUnwrappedOptional:
2647
2641
case OverloadChoiceKind::DeclViaDynamic:
2648
- return buildMemberRef (base, selected.openedFullType , dotLoc,
2649
- selected.choice , nameLoc, selected.openedType ,
2642
+ return buildMemberRef (base, dotLoc, selected, nameLoc,
2650
2643
cs.getConstraintLocator (expr), memberLocator,
2651
2644
implicit, AccessSemantics::Ordinary);
2652
2645
@@ -6478,12 +6471,11 @@ static Expr *buildCallAsFunctionMethodRef(
6478
6471
ExprRewriter &rewriter, ApplyExpr *apply, SelectedOverload selected,
6479
6472
ConstraintLocatorBuilder applyFunctionLoc) {
6480
6473
auto *fn = apply->getFn ();
6481
- auto choice = selected.choice ;
6482
6474
// Create direct reference to `callAsFunction` method.
6483
6475
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);
6487
6479
if (!declRef)
6488
6480
return nullptr ;
6489
6481
declRef->setImplicit (apply->isImplicit ());
@@ -6515,11 +6507,9 @@ ExprRewriter::finishApplyDynamicCallable(ApplyExpr *apply,
6515
6507
bool useKwargsMethod = argumentLabel == ctx.Id_withKeywordArguments ;
6516
6508
6517
6509
// 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);
6523
6513
6524
6514
// Construct argument to the method (either an array or dictionary
6525
6515
// expression).
@@ -6874,11 +6864,9 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
6874
6864
// Consider the constructor decl reference expr 'implicit', but the
6875
6865
// constructor call expr itself has the apply's 'implicitness'.
6876
6866
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 ,
6882
6870
AccessSemantics::Ordinary);
6883
6871
if (!declRef)
6884
6872
return nullptr ;
0 commit comments