@@ -561,47 +561,6 @@ static bool getPositionInArgs(DeclContext &DC, Expr *Args, Expr *CCExpr,
561
561
return false ;
562
562
}
563
563
564
- enum class ArgPositionKind {
565
- // / The argument is in normal calling parenthesis.
566
- // / i.e. foo(args..., <HERE>) { ... }
567
- NormalArgument,
568
- // / The argument is inside multiple trailing closure block.
569
- // / i.e. foo(args...) { arg2: { ... } <HERE> }
570
- InClosureBlock,
571
- // / The argument is inside multiple trailing closure block, also it is the
572
- // / sole element.
573
- // / foo(args...) { <HERE> }
574
- InEmptyClosureBlock,
575
- };
576
-
577
- static ArgPositionKind getArgPositionKind (DeclContext &DC, Expr *Args,
578
- unsigned Position) {
579
- SourceManager &SM = DC.getASTContext ().SourceMgr ;
580
-
581
- if (auto tuple = dyn_cast<TupleExpr>(Args)) {
582
- SourceLoc argPos = tuple->getElement (Position)->getStartLoc ();
583
- SourceLoc rParenLoc = tuple->getRParenLoc ();
584
- if (rParenLoc.isInvalid () || SM.isBeforeInBuffer (rParenLoc, argPos)) {
585
- // Invariant: If the token is at the label position, the label location is
586
- // invalid, and the value is a CodeCompletionExpr.
587
- if (Position == tuple->getNumElements () - 1 &&
588
- tuple->getNumTrailingElements () == 1 &&
589
- tuple->getElementNameLoc (Position).isInvalid ()) {
590
- return ArgPositionKind::InEmptyClosureBlock;
591
- }
592
- return ArgPositionKind::InClosureBlock;
593
- }
594
- } else if (auto paren = dyn_cast<ParenExpr>(Args)) {
595
- SourceLoc argLoc = paren->getSubExpr ()->getStartLoc ();
596
- SourceLoc rParenLoc = paren->getRParenLoc ();
597
- // We don't have a way to distingish between 'foo { _: <here> }' and
598
- // 'foo { <here> }'. For now, consider it latter one.
599
- if (rParenLoc.isInvalid () || SM.isBeforeInBuffer (rParenLoc, argLoc))
600
- return ArgPositionKind::InEmptyClosureBlock;
601
- }
602
- return ArgPositionKind::NormalArgument;
603
- }
604
-
605
564
// / Given an expression and its context, the analyzer tries to figure out the
606
565
// / expected type of the expression by analyzing its context.
607
566
class ExprContextAnalyzer {
@@ -612,7 +571,7 @@ class ExprContextAnalyzer {
612
571
613
572
// Results populated by Analyze()
614
573
SmallVectorImpl<Type> &PossibleTypes;
615
- SmallVectorImpl<const AnyFunctionType::Param * > &PossibleParams;
574
+ SmallVectorImpl<PossibleParamInfo > &PossibleParams;
616
575
SmallVectorImpl<FunctionTypeAndDecl> &PossibleCallees;
617
576
bool &singleExpressionBody;
618
577
@@ -623,8 +582,8 @@ class ExprContextAnalyzer {
623
582
PossibleTypes.push_back (ty->getRValueType ());
624
583
}
625
584
626
- void recordPossibleParam (const AnyFunctionType::Param & arg) {
627
- PossibleParams.push_back (& arg);
585
+ void recordPossibleParam (const AnyFunctionType::Param * arg, bool isRequired ) {
586
+ PossibleParams.emplace_back ( arg, isRequired );
628
587
}
629
588
630
589
// / Collect context information at call argument position.
@@ -657,9 +616,9 @@ class ExprContextAnalyzer {
657
616
if (!getPositionInArgs (*DC, Arg, ParsedExpr, Position, HasName))
658
617
return false ;
659
618
660
- ArgPositionKind positionKind = getArgPositionKind (*DC, Arg, Position);
661
-
662
619
// Collect possible types (or labels) at the position.
620
+ // FIXME: Take variadic and optional parameters into account. We need to do
621
+ // something equivalent to 'constraints::matchCallArguments'
663
622
{
664
623
bool MayNeedName = !HasName && !E->isImplicit () &&
665
624
(isa<CallExpr>(E) | isa<SubscriptExpr>(E) ||
@@ -681,48 +640,18 @@ class ExprContextAnalyzer {
681
640
if (paramList && paramList->size () != Params.size ())
682
641
paramList = nullptr ;
683
642
}
684
-
685
- // Determine the index of the parameter that can be a single trailing
686
- // closure.
687
- unsigned singleTrailingClosureIdx = Params.size ();
688
- for (int idx = Params.size () - 1 ; idx >= 0 ; --idx) {
689
- if (Params[idx].getPlainType ()->is <AnyFunctionType>()) {
690
- singleTrailingClosureIdx = idx;
691
- break ;
692
- }
693
- if (!paramList || !paramList->get (idx)->isDefaultArgument ())
694
- break ;
695
- }
696
-
697
643
for (auto Pos = Position; Pos < Params.size (); ++Pos) {
698
644
const auto ¶mType = Params[Pos];
699
645
Type ty = paramType.getPlainType ();
700
646
if (memberDC && ty->hasTypeParameter ())
701
647
ty = memberDC->mapTypeIntoContext (ty);
702
648
703
649
if (paramType.hasLabel () && MayNeedName) {
704
- // In trailing closure block, don't suggest non-closure arguments.
705
- if (positionKind >= ArgPositionKind::InClosureBlock) {
706
- Type argTy = ty;
707
- if (paramType.isAutoClosure () && ty->is <AnyFunctionType>())
708
- argTy = ty->castTo <AnyFunctionType>()->getResult ();
709
- if (!argTy->is <AnyFunctionType>())
710
- continue ;
711
-
712
- // If the token is the only element in the closure block. It might
713
- // be a single trailing closure. We should perform global
714
- // completion as well.
715
- if (positionKind == ArgPositionKind::InEmptyClosureBlock &&
716
- Pos == singleTrailingClosureIdx) {
717
- auto resultTy = argTy->castTo <AnyFunctionType>()->getResult ();
718
- if (seenTypes.insert (resultTy.getPointer ()).second )
719
- recordPossibleType (resultTy);
720
- }
721
- }
722
-
650
+ bool isDefaulted = paramList &&
651
+ paramList->get (Pos)->isDefaultArgument ();
723
652
if (seenArgs.insert ({paramType.getLabel (), ty.getPointer ()}).second )
724
- recordPossibleParam (paramType);
725
- if (paramList && paramList-> get (Position)-> isDefaultArgument () )
653
+ recordPossibleParam (& paramType, !isDefaulted );
654
+ if (isDefaulted )
726
655
continue ;
727
656
} else {
728
657
auto argTy = ty;
@@ -733,6 +662,12 @@ class ExprContextAnalyzer {
733
662
}
734
663
break ;
735
664
}
665
+ // If the argument position is out of expeceted number, indicate that
666
+ // with optional nullptr param.
667
+ if (Position >= Params.size ()) {
668
+ if (seenArgs.insert ({Identifier (), nullptr }).second )
669
+ recordPossibleParam (nullptr , /* isRequired=*/ false );
670
+ }
736
671
}
737
672
}
738
673
return !PossibleTypes.empty () || !PossibleParams.empty ();
@@ -999,7 +934,7 @@ class ExprContextAnalyzer {
999
934
public:
1000
935
ExprContextAnalyzer (
1001
936
DeclContext *DC, Expr *ParsedExpr, SmallVectorImpl<Type> &PossibleTypes,
1002
- SmallVectorImpl<const AnyFunctionType::Param * > &PossibleArgs,
937
+ SmallVectorImpl<PossibleParamInfo > &PossibleArgs,
1003
938
SmallVectorImpl<FunctionTypeAndDecl> &PossibleCallees,
1004
939
bool &singleExpressionBody)
1005
940
: DC(DC), ParsedExpr(ParsedExpr), SM(DC->getASTContext ().SourceMgr),
0 commit comments