23
23
#include " swift/Basic/StringExtras.h"
24
24
#include " llvm/ADT/APFloat.h"
25
25
#include " llvm/ADT/APInt.h"
26
+ #include " llvm/ADT/Fixnum.h"
26
27
#include " llvm/ADT/SmallString.h"
27
28
#include " llvm/Support/SaveAndRestore.h"
28
29
@@ -1111,10 +1112,15 @@ namespace {
1111
1112
// /
1112
1113
// / \param arg The argument expression.
1113
1114
// / \param paramType The parameter type.
1115
+ // / \param applyOrLevel For function applications, the ApplyExpr that forms
1116
+ // / the call. Otherwise, a specific level describing which parameter level
1117
+ // / we're applying.
1114
1118
// / \param locator Locator used to describe where in this expression we are.
1115
1119
// /
1116
1120
// / \returns the coerced expression, which will have type \c ToType.
1117
1121
Expr *coerceCallArguments (Expr *arg, Type paramType,
1122
+ llvm::PointerUnion<ApplyExpr *, llvm::Fixnum<2 >>
1123
+ applyOrLevel,
1118
1124
ConstraintLocatorBuilder locator);
1119
1125
1120
1126
// / \brief Coerce the given object argument (e.g., for the base of a
@@ -1184,7 +1190,7 @@ namespace {
1184
1190
}
1185
1191
1186
1192
// Coerce the index argument.
1187
- index = coerceCallArguments (index, indexTy,
1193
+ index = coerceCallArguments (index, indexTy, /* level= */ llvm::Fixnum< 2 >( 1 ),
1188
1194
locator.withPathElement (
1189
1195
ConstraintLocator::SubscriptIndex));
1190
1196
if (!index)
@@ -3778,11 +3784,11 @@ resolveLocatorToDecl(ConstraintSystem &cs, ConstraintLocator *locator,
3778
3784
}
3779
3785
3780
3786
3781
- // / \brief Given a constraint locator, find the owner of default arguments for
3782
- // / that tuple, i.e., a FuncDecl .
3787
+ // / \brief Given a constraint locator, find the declaration reference
3788
+ // / to the callee, it is a call to a declaration .
3783
3789
static ConcreteDeclRef
3784
- findDefaultArgsOwner (ConstraintSystem &cs, const Solution &solution,
3785
- ConstraintLocator *locator) {
3790
+ findCalleeDeclRef (ConstraintSystem &cs, const Solution &solution,
3791
+ ConstraintLocator *locator) {
3786
3792
if (locator->getPath ().empty () || !locator->getAnchor ())
3787
3793
return nullptr ;
3788
3794
@@ -4043,8 +4049,8 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
4043
4049
SmallVector<TupleTypeElt, 4 > fromTupleExprFields (
4044
4050
fromTuple->getElements ().size ());
4045
4051
SmallVector<Expr *, 2 > callerDefaultArgs;
4046
- ConcreteDeclRef defaultArgsOwner =
4047
- findDefaultArgsOwner (cs, solution, cs.getConstraintLocator (locator));
4052
+ ConcreteDeclRef callee =
4053
+ findCalleeDeclRef (cs, solution, cs.getConstraintLocator (locator));
4048
4054
4049
4055
for (unsigned i = 0 , n = toTuple->getNumElements (); i != n; ++i) {
4050
4056
const auto &toElt = toTuple->getElement (i);
@@ -4058,7 +4064,7 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
4058
4064
4059
4065
// Create a caller-side default argument, if we need one.
4060
4066
if (auto defArg = getCallerDefaultArg (tc, dc, expr->getLoc (),
4061
- defaultArgsOwner , i).first ) {
4067
+ callee , i).first ) {
4062
4068
callerDefaultArgs.push_back (defArg);
4063
4069
sources[i] = TupleShuffleExpr::CallerDefaultInitialize;
4064
4070
}
@@ -4218,7 +4224,7 @@ Expr *ExprRewriter::coerceTupleToTuple(Expr *expr, TupleType *fromTuple,
4218
4224
auto shuffle = new (tc.Context ) TupleShuffleExpr (
4219
4225
expr, mapping,
4220
4226
TupleShuffleExpr::SourceIsTuple,
4221
- defaultArgsOwner ,
4227
+ callee ,
4222
4228
tc.Context .AllocateCopy (variadicArgs),
4223
4229
callerDefaultArgsCopy,
4224
4230
toSugarType);
@@ -4299,8 +4305,8 @@ Expr *ExprRewriter::coerceScalarToTuple(Expr *expr, TupleType *toTuple,
4299
4305
SmallVector<int , 4 > elements;
4300
4306
SmallVector<unsigned , 1 > variadicArgs;
4301
4307
SmallVector<Expr*, 4 > callerDefaultArgs;
4302
- ConcreteDeclRef defaultArgsOwner =
4303
- findDefaultArgsOwner (cs, solution, cs.getConstraintLocator (locator));
4308
+ ConcreteDeclRef callee =
4309
+ findCalleeDeclRef (cs, solution, cs.getConstraintLocator (locator));
4304
4310
4305
4311
i = 0 ;
4306
4312
for (auto &field : toTuple->getElements ()) {
@@ -4324,7 +4330,7 @@ Expr *ExprRewriter::coerceScalarToTuple(Expr *expr, TupleType *toTuple,
4324
4330
4325
4331
// Create a caller-side default argument, if we need one.
4326
4332
if (auto defArg = getCallerDefaultArg (tc, dc, expr->getLoc (),
4327
- defaultArgsOwner , i).first ) {
4333
+ callee , i).first ) {
4328
4334
// Record the caller-side default argument expression.
4329
4335
// FIXME: Do we need to record what this was synthesized from?
4330
4336
elements.push_back (TupleShuffleExpr::CallerDefaultInitialize);
@@ -4342,7 +4348,7 @@ Expr *ExprRewriter::coerceScalarToTuple(Expr *expr, TupleType *toTuple,
4342
4348
return new (tc.Context ) TupleShuffleExpr (expr,
4343
4349
tc.Context .AllocateCopy (elements),
4344
4350
TupleShuffleExpr::SourceIsScalar,
4345
- defaultArgsOwner ,
4351
+ callee ,
4346
4352
tc.Context .AllocateCopy (variadicArgs),
4347
4353
tc.Context .AllocateCopy (callerDefaultArgs),
4348
4354
destSugarTy);
@@ -4525,14 +4531,56 @@ Expr *ExprRewriter::coerceImplicitlyUnwrappedOptionalToValue(Expr *expr, Type ob
4525
4531
return expr;
4526
4532
}
4527
4533
4528
- Expr *ExprRewriter::coerceCallArguments (Expr *arg, Type paramType,
4529
- ConstraintLocatorBuilder locator) {
4534
+ // / Determine whether the given expression is a reference to an
4535
+ // / unbound instance member of a type.
4536
+ static bool isReferenceToMetatypeMember (Expr *expr) {
4537
+ expr = expr->getSemanticsProvidingExpr ();
4538
+ if (auto dotIgnored = dyn_cast<DotSyntaxBaseIgnoredExpr>(expr))
4539
+ return dotIgnored->getLHS ()->getType ()->is <AnyMetatypeType>();
4540
+ if (auto dotSyntax = dyn_cast<DotSyntaxCallExpr>(expr))
4541
+ return dotSyntax->getBase ()->getType ()->is <AnyMetatypeType>();
4542
+ return false ;
4543
+ }
4544
+
4545
+ Expr *ExprRewriter::coerceCallArguments (
4546
+ Expr *arg, Type paramType,
4547
+ llvm::PointerUnion<ApplyExpr *, llvm::Fixnum<2 >> applyOrLevel,
4548
+ ConstraintLocatorBuilder locator) {
4530
4549
4531
4550
bool allParamsMatch = arg->getType ()->isEqual (paramType);
4532
-
4551
+
4552
+ // Find the callee declaration.
4553
+ ConcreteDeclRef callee =
4554
+ findCalleeDeclRef (cs, solution, cs.getConstraintLocator (locator));
4555
+
4556
+ // Determine the level,
4557
+ unsigned level = 0 ;
4558
+ if (applyOrLevel.is <llvm::Fixnum<2 >>()) {
4559
+ // Level specified by caller.
4560
+ level = applyOrLevel.get <llvm::Fixnum<2 >>();
4561
+ } else if (callee) {
4562
+ // Determine the level based on the application itself.
4563
+ auto apply = applyOrLevel.get <ApplyExpr *>();
4564
+
4565
+ // Only calls to members of types can have level > 0.
4566
+ auto calleeDecl = callee.getDecl ();
4567
+ if (calleeDecl->getDeclContext ()->isTypeContext ()) {
4568
+ // Level 1 if we're not applying "self".
4569
+ if (auto call = dyn_cast<CallExpr>(apply)) {
4570
+ if (!calleeDecl->isInstanceMember () ||
4571
+ !isReferenceToMetatypeMember (call->getDirectCallee ()))
4572
+ level = 1 ;
4573
+ } else if (isa<PrefixUnaryExpr>(apply) ||
4574
+ isa<PostfixUnaryExpr>(apply) ||
4575
+ isa<BinaryExpr>(apply)) {
4576
+ level = 1 ;
4577
+ }
4578
+ }
4579
+ }
4580
+
4533
4581
// Determine the parameter bindings.
4534
- auto params = decomposeArgParamType (paramType);
4535
- auto args = decomposeArgParamType (arg->getType ());
4582
+ auto params = decomposeParamType (paramType, callee. getDecl (), level );
4583
+ auto args = decomposeArgType (arg->getType ());
4536
4584
4537
4585
// Quickly test if any further fix-ups for the argument types are necessary.
4538
4586
// FIXME: This hack is only necessary to work around some problems we have
@@ -4614,8 +4662,6 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, Type paramType,
4614
4662
SmallVector<Expr*, 4 > fromTupleExpr (argTuple? argTuple->getNumElements () : 1 );
4615
4663
SmallVector<unsigned , 4 > variadicArgs;
4616
4664
SmallVector<Expr *, 2 > callerDefaultArgs;
4617
- ConcreteDeclRef defaultArgsOwner =
4618
- findDefaultArgsOwner (cs, solution, cs.getConstraintLocator (locator));
4619
4665
Type sliceType = nullptr ;
4620
4666
SmallVector<int , 4 > sources;
4621
4667
for (unsigned paramIdx = 0 , numParams = parameterBindings.size ();
@@ -4673,8 +4719,7 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, Type paramType,
4673
4719
Expr *defArg;
4674
4720
DefaultArgumentKind defArgKind;
4675
4721
std::tie (defArg, defArgKind) = getCallerDefaultArg (tc, dc, arg->getLoc (),
4676
- defaultArgsOwner,
4677
- paramIdx);
4722
+ callee, paramIdx);
4678
4723
4679
4724
// Note that we'll be doing a shuffle involving default arguments.
4680
4725
anythingShuffled = true ;
@@ -4793,7 +4838,7 @@ Expr *ExprRewriter::coerceCallArguments(Expr *arg, Type paramType,
4793
4838
auto shuffle = new (tc.Context ) TupleShuffleExpr (
4794
4839
arg, mapping,
4795
4840
isSourceScalar,
4796
- defaultArgsOwner ,
4841
+ callee ,
4797
4842
tc.Context .AllocateCopy (variadicArgs),
4798
4843
callerDefaultArgsCopy,
4799
4844
paramType);
@@ -5768,8 +5813,8 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, Type openedType,
5768
5813
// the function.
5769
5814
if (auto fnType = fn->getType ()->getAs <FunctionType>()) {
5770
5815
auto origArg = apply->getArg ();
5771
-
5772
5816
Expr *arg = coerceCallArguments (origArg, fnType->getInput (),
5817
+ apply,
5773
5818
locator.withPathElement (
5774
5819
ConstraintLocator::ApplyArgument));
5775
5820
if (!arg) {
@@ -6547,14 +6592,23 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
6547
6592
if (!witness)
6548
6593
return nullptr ;
6549
6594
6595
+ // Form a syntactic expression that describes the reference to the
6596
+ // witness.
6597
+ // FIXME: Egregious hack.
6598
+ Expr *unresolvedDot = new (Context) UnresolvedDotExpr (
6599
+ base, SourceLoc (),
6600
+ witness->getFullName (),
6601
+ DeclNameLoc (base->getEndLoc ()),
6602
+ /* Implicit=*/ true );
6603
+ auto dotLocator = cs.getConstraintLocator (unresolvedDot);
6604
+
6550
6605
// Form a reference to the witness itself.
6551
- auto locator = cs.getConstraintLocator (base);
6552
6606
Type openedFullType, openedType;
6553
6607
std::tie (openedFullType, openedType)
6554
6608
= cs.getTypeOfMemberReference (base->getType (), witness,
6555
6609
/* isTypeReference=*/ false ,
6556
6610
/* isDynamicResult=*/ false ,
6557
- locator );
6611
+ dotLocator );
6558
6612
6559
6613
// Form the call argument.
6560
6614
// FIXME: Standardize all callers to always provide all argument names,
@@ -6626,7 +6680,7 @@ Expr *TypeChecker::callWitness(Expr *base, DeclContext *dc,
6626
6680
base->getStartLoc (),
6627
6681
witness,
6628
6682
DeclNameLoc (base->getEndLoc ()),
6629
- openedType, locator, locator ,
6683
+ openedType, dotLocator, dotLocator ,
6630
6684
/* Implicit=*/ true ,
6631
6685
AccessSemantics::Ordinary,
6632
6686
/* isDynamic=*/ false );
0 commit comments