@@ -7600,157 +7600,3 @@ Expr *Solution::coerceToType(Expr *expr, Type toType,
7600
7600
rewriter.finalize (result);
7601
7601
return result;
7602
7602
}
7603
-
7604
- // Determine whether this is a variadic witness.
7605
- static bool isVariadicWitness (AbstractFunctionDecl *afd) {
7606
- for (auto param : *afd->getParameters ())
7607
- if (param->isVariadic ())
7608
- return true ;
7609
-
7610
- return false ;
7611
- }
7612
-
7613
- static bool argumentNamesMatch (Type argTy, ArrayRef<Identifier> names) {
7614
- auto tupleType = argTy->getAs <TupleType>();
7615
- if (!tupleType)
7616
- return names.size () == 1 && names[0 ].empty ();
7617
-
7618
- if (tupleType->getNumElements () != names.size ())
7619
- return false ;
7620
-
7621
- for (unsigned i = 0 , n = tupleType->getNumElements (); i != n; ++i) {
7622
- if (tupleType->getElement (i).getName () != names[i])
7623
- return false ;
7624
- }
7625
-
7626
- return true ;
7627
- }
7628
-
7629
- Expr *TypeChecker::callWitness (Expr *base, DeclContext *dc,
7630
- ProtocolDecl *protocol,
7631
- ProtocolConformanceRef conformance,
7632
- DeclName name,
7633
- ArrayRef<Expr *> arguments,
7634
- Diag<> brokenProtocolDiag) {
7635
- // Construct an empty constraint system and solution.
7636
- ConstraintSystem cs (*this , dc, ConstraintSystemOptions ());
7637
-
7638
- for (auto *arg : arguments)
7639
- cs.cacheType (arg);
7640
-
7641
- // Find the witness we need to use.
7642
- auto type = base->getType ();
7643
- assert (!type->hasTypeVariable () &&
7644
- " Building call to witness with unresolved base type!" );
7645
-
7646
- if (auto metaType = type->getAs <AnyMetatypeType>())
7647
- type = metaType->getInstanceType ();
7648
-
7649
- // Find the member used to satisfy the named requirement.
7650
- auto witness = conformance.getWitnessByName (type, name);
7651
- if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl ()))
7652
- return nullptr ;
7653
-
7654
- auto *witnessFn = cast<AbstractFunctionDecl>(witness.getDecl ());
7655
-
7656
- // Form a syntactic expression that describes the reference to the
7657
- // witness.
7658
- // FIXME: Egregious hack.
7659
- auto unresolvedDot = new (Context) UnresolvedDotExpr (
7660
- base, SourceLoc (),
7661
- witness.getDecl ()->getFullName (),
7662
- DeclNameLoc (base->getEndLoc ()),
7663
- /* Implicit=*/ true );
7664
- unresolvedDot->setFunctionRefKind (FunctionRefKind::SingleApply);
7665
- auto dotLocator = cs.getConstraintLocator (unresolvedDot);
7666
-
7667
- // Form a reference to the witness itself.
7668
- Type openedFullType, openedType;
7669
- std::tie (openedFullType, openedType)
7670
- = cs.getTypeOfMemberReference (base->getType (), witness.getDecl (), dc,
7671
- /* isDynamicResult=*/ false ,
7672
- FunctionRefKind::DoubleApply,
7673
- dotLocator);
7674
-
7675
- auto getType = [&](const Expr *E) -> Type {
7676
- return cs.getType (E);
7677
- };
7678
-
7679
- // Form the call argument.
7680
- // FIXME: Standardize all callers to always provide all argument names,
7681
- // rather than hack around this.
7682
- CallExpr *call;
7683
- auto argLabels = witness.getDecl ()->getFullName ().getArgumentNames ();
7684
- if (arguments.size () == 1 &&
7685
- (isVariadicWitness (witnessFn) ||
7686
- argumentNamesMatch (cs.getType (arguments[0 ]), argLabels))) {
7687
- call = CallExpr::create (Context, unresolvedDot, arguments[0 ], {}, {},
7688
- /* hasTrailingClosure=*/ false ,
7689
- /* implicit=*/ true , Type (), getType);
7690
- } else {
7691
- // The tuple should have the source range enclosing its arguments unless
7692
- // they are invalid or there are no arguments.
7693
- SourceLoc TupleStartLoc = base->getStartLoc ();
7694
- SourceLoc TupleEndLoc = base->getEndLoc ();
7695
- if (!arguments.empty ()) {
7696
- SourceLoc AltStartLoc = arguments.front ()->getStartLoc ();
7697
- SourceLoc AltEndLoc = arguments.back ()->getEndLoc ();
7698
- if (AltStartLoc.isValid () && AltEndLoc.isValid ()) {
7699
- TupleStartLoc = AltStartLoc;
7700
- TupleEndLoc = AltEndLoc;
7701
- }
7702
- }
7703
-
7704
- call = CallExpr::create (Context, unresolvedDot, TupleStartLoc, arguments,
7705
- argLabels, {}, TupleEndLoc,
7706
- /* trailingClosure=*/ nullptr ,
7707
- /* implicit=*/ true , getType);
7708
- }
7709
-
7710
- cs.cacheSubExprTypes (call);
7711
-
7712
- // Extract the arguments.
7713
- SmallVector<AnyFunctionType::Param, 8 > args;
7714
- AnyFunctionType::decomposeInput (cs.getType (call->getArg ()), args);
7715
-
7716
- // Add the conversion from the argument to the function parameter type.
7717
- auto openedFuncType = openedType->castTo <FunctionType>();
7718
- ::matchCallArguments (
7719
- cs, args, openedFuncType->getParams (),
7720
- ConstraintKind::ArgumentConversion,
7721
- cs.getConstraintLocator(call, ConstraintLocator::ApplyArgument));
7722
-
7723
- // Solve the system.
7724
- SmallVector<Solution, 1 > solutions;
7725
-
7726
- cs.cacheExprTypes(call);
7727
-
7728
- // If the system failed to produce a solution, post any available diagnostics.
7729
- if (cs.solve (call, solutions) || solutions.size () != 1 ) {
7730
- cs.salvage (solutions, base);
7731
- return nullptr ;
7732
- }
7733
-
7734
- Solution &solution = solutions.front ();
7735
- ExprRewriter rewriter (cs, solution,
7736
- /* suppressDiagnostics=*/ false );
7737
-
7738
- auto choice =
7739
- OverloadChoice (openedFullType, witnessFn, FunctionRefKind::SingleApply);
7740
- auto memberRef = rewriter.buildMemberRef (
7741
- base, openedFullType, base->getStartLoc (), choice,
7742
- DeclNameLoc (base->getEndLoc ()), openedType, dotLocator, dotLocator,
7743
- /* Implicit=*/ true , FunctionRefKind::SingleApply,
7744
- AccessSemantics::Ordinary,
7745
- /* isDynamic=*/ false );
7746
- call->setFn (memberRef);
7747
-
7748
- // Call the witness.
7749
- Expr *result = rewriter.finishApply (call, openedType,
7750
- cs.getConstraintLocator (call));
7751
- if (!result)
7752
- return nullptr ;
7753
-
7754
- rewriter.finalize (result);
7755
- return result;
7756
- }
0 commit comments