@@ -1509,13 +1509,20 @@ static bool isTopLevelContext(const DeclContext *DC) {
1509
1509
static Type getReturnTypeFromContext (const DeclContext *DC) {
1510
1510
if (auto FD = dyn_cast<AbstractFunctionDecl>(DC)) {
1511
1511
if (FD->hasInterfaceType ()) {
1512
- if (auto FT = FD->getInterfaceType ()->getAs <FunctionType>()) {
1512
+ auto Ty = FD->getInterfaceType ();
1513
+ if (FD->getDeclContext ()->isTypeContext ())
1514
+ Ty = FD->getMethodInterfaceType ();
1515
+ if (auto FT = Ty->getAs <AnyFunctionType>())
1513
1516
return FT->getResult ();
1514
- }
1515
1517
}
1516
- } else if (auto CE = dyn_cast<AbstractClosureExpr>(DC)) {
1517
- if (CE->getType ()) {
1518
- return CE->getResultType ();
1518
+ } else if (auto ACE = dyn_cast<AbstractClosureExpr>(DC)) {
1519
+ if (ACE->getType ())
1520
+ return ACE->getResultType ();
1521
+ if (auto CE = dyn_cast<ClosureExpr>(ACE)) {
1522
+ if (CE->hasExplicitResultType ())
1523
+ return const_cast <ClosureExpr *>(CE)
1524
+ ->getExplicitResultTypeLoc ()
1525
+ .getType ();
1519
1526
}
1520
1527
}
1521
1528
return Type ();
@@ -3893,7 +3900,7 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3893
3900
using FunctionParams = ArrayRef<AnyFunctionType::Param>;
3894
3901
3895
3902
static bool
3896
- collectPossibleParamLists (DeclContext &DC, CallExpr *callExpr,
3903
+ collectPossibleParamLists (DeclContext &DC, ApplyExpr *callExpr,
3897
3904
SmallVectorImpl<FunctionParams> &candidates) {
3898
3905
auto *fnExpr = callExpr->getFn ();
3899
3906
@@ -3940,27 +3947,20 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3940
3947
if (!tuple)
3941
3948
return false ;
3942
3949
3943
- for (unsigned i = 0 , n = tuple->getNumElements (); i != n; ++i) {
3944
- if (isa<CodeCompletionExpr>(tuple->getElement (i))) {
3945
- HasName = !tuple->getElementName (i).empty ();
3946
- Position = i;
3947
- return true ;
3948
- }
3949
- }
3950
3950
auto &SM = DC.getASTContext ().SourceMgr ;
3951
3951
for (unsigned i = 0 , n = tuple->getNumElements (); i != n; ++i) {
3952
3952
if (SM.isBeforeInBuffer (tuple->getElement (i)->getEndLoc (),
3953
3953
CCExpr->getStartLoc ()))
3954
3954
continue ;
3955
- HasName = ! tuple->getElementName (i).empty ();
3955
+ HasName = tuple->getElementNameLoc (i).isValid ();
3956
3956
Position = i;
3957
3957
return true ;
3958
3958
}
3959
3959
return false ;
3960
3960
}
3961
3961
3962
3962
static bool
3963
- collectArgumentExpectation (DeclContext &DC, CallExpr *CallE, Expr *CCExpr,
3963
+ collectArgumentExpectation (DeclContext &DC, ApplyExpr *CallE, Expr *CCExpr,
3964
3964
std::vector<Type> &ExpectedTypes,
3965
3965
std::vector<StringRef> &ExpectedNames) {
3966
3966
// Collect parameter lists for possible func decls.
@@ -3974,15 +3974,17 @@ class CompletionLookup final : public swift::VisibleDeclConsumer {
3974
3974
if (!getPositionInArgs (DC, CallE->getArg (), CCExpr, Position, HasName))
3975
3975
return false ;
3976
3976
3977
- // Collect possible types at the position.
3977
+ // Collect possible types (or labels) at the position.
3978
3978
{
3979
+ bool MayNeedName =
3980
+ !HasName && isa<CallExpr>(CallE) && !CallE->isImplicit ();
3979
3981
SmallPtrSet<TypeBase *, 4 > seenTypes;
3980
3982
SmallPtrSet<Identifier, 4 > seenNames;
3981
3983
for (auto Params : Candidates) {
3982
3984
if (Position >= Params.size ())
3983
3985
continue ;
3984
3986
const auto &Param = Params[Position];
3985
- if (Param.hasLabel () && !HasName ) {
3987
+ if (Param.hasLabel () && MayNeedName ) {
3986
3988
if (seenNames.insert (Param.getLabel ()).second )
3987
3989
ExpectedNames.push_back (Param.getLabel ().str ());
3988
3990
} else {
@@ -4909,19 +4911,19 @@ namespace {
4909
4911
class ExprParentFinder : public ASTWalker {
4910
4912
friend class CodeCompletionTypeContextAnalyzer ;
4911
4913
Expr *ChildExpr;
4912
- llvm::function_ref<bool (ASTNode )> Predicate;
4914
+ llvm::function_ref<bool (ParentTy )> Predicate;
4913
4915
4914
4916
bool arePositionsSame (Expr *E1 , Expr *E2 ) {
4915
4917
return E1 ->getSourceRange ().Start == E2 ->getSourceRange ().Start &&
4916
4918
E1 ->getSourceRange ().End == E2 ->getSourceRange ().End ;
4917
4919
}
4918
4920
4919
4921
public:
4920
- llvm::SmallVector<ASTNode , 5 > Ancestors;
4921
- ASTNode ParentClosest;
4922
- ASTNode ParentFarthest;
4922
+ llvm::SmallVector<ParentTy , 5 > Ancestors;
4923
+ ParentTy ParentClosest;
4924
+ ParentTy ParentFarthest;
4923
4925
ExprParentFinder (Expr* ChildExpr,
4924
- llvm::function_ref<bool (ASTNode )> Predicate) :
4926
+ llvm::function_ref<bool (ParentTy )> Predicate) :
4925
4927
ChildExpr (ChildExpr), Predicate(Predicate) {}
4926
4928
4927
4929
std::pair<bool , Expr *> walkToExprPre (Expr *E) override {
@@ -4966,6 +4968,18 @@ namespace {
4966
4968
Ancestors.pop_back ();
4967
4969
return true ;
4968
4970
}
4971
+
4972
+ std::pair<bool , Pattern *> walkToPatternPre (Pattern *P) override {
4973
+ if (Predicate (P))
4974
+ Ancestors.push_back (P);
4975
+ return { true , P };
4976
+ }
4977
+
4978
+ Pattern *walkToPatternPost (Pattern *P) override {
4979
+ if (Predicate (P))
4980
+ Ancestors.pop_back ();
4981
+ return P;
4982
+ }
4969
4983
};
4970
4984
} // end anonymous namespace
4971
4985
@@ -4981,16 +4995,19 @@ class CodeCompletionTypeContextAnalyzer {
4981
4995
public:
4982
4996
CodeCompletionTypeContextAnalyzer (DeclContext *DC, Expr *ParsedExpr) : DC(DC),
4983
4997
ParsedExpr (ParsedExpr), SM(DC->getASTContext ().SourceMgr),
4984
- Context(DC->getASTContext ()), Finder(ParsedExpr, [](ASTNode Node) {
4985
- if (auto E = Node.dyn_cast <Expr *>()) {
4998
+ Context(DC->getASTContext ()),
4999
+ Finder(ParsedExpr, [](ASTWalker::ParentTy Node) {
5000
+ if (auto E = Node.getAsExpr ()) {
4986
5001
switch (E->getKind ()) {
4987
5002
case ExprKind::Call:
5003
+ case ExprKind::Binary:
5004
+ case ExprKind::PrefixUnary:
4988
5005
case ExprKind::Assign:
4989
5006
return true ;
4990
5007
default :
4991
5008
return false ;
4992
- }
4993
- } else if (auto S = Node.dyn_cast <Stmt *> ()) {
5009
+ }
5010
+ } else if (auto S = Node.getAsStmt ()) {
4994
5011
switch (S->getKind ()) {
4995
5012
case StmtKind::Return:
4996
5013
case StmtKind::ForEach:
@@ -5002,25 +5019,34 @@ class CodeCompletionTypeContextAnalyzer {
5002
5019
default :
5003
5020
return false ;
5004
5021
}
5005
- } else if (auto D = Node.dyn_cast <Decl *> ()) {
5022
+ } else if (auto D = Node.getAsDecl ()) {
5006
5023
switch (D->getKind ()) {
5007
5024
case DeclKind::PatternBinding:
5008
5025
return true ;
5009
5026
default :
5010
5027
return false ;
5011
5028
}
5029
+ } else if (auto P = Node.getAsPattern ()) {
5030
+ switch (P->getKind ()) {
5031
+ case PatternKind::Expr:
5032
+ return true ;
5033
+ default :
5034
+ return false ;
5035
+ }
5012
5036
} else
5013
5037
return false ;
5014
- }) {}
5038
+ }) {}
5015
5039
5016
5040
void analyzeExpr (Expr *Parent, llvm::function_ref<void (Type)> Callback,
5017
5041
SmallVectorImpl<StringRef> &PossibleNames) {
5018
5042
switch (Parent->getKind ()) {
5019
- case ExprKind::Call: {
5043
+ case ExprKind::Call:
5044
+ case ExprKind::Binary:
5045
+ case ExprKind::PrefixUnary: {
5020
5046
std::vector<Type> PotentialTypes;
5021
5047
std::vector<StringRef> ExpectedNames;
5022
5048
CompletionLookup::collectArgumentExpectation (
5023
- *DC, cast<CallExpr >(Parent), ParsedExpr, PotentialTypes,
5049
+ *DC, cast<ApplyExpr >(Parent), ParsedExpr, PotentialTypes,
5024
5050
ExpectedNames);
5025
5051
for (Type Ty : PotentialTypes)
5026
5052
Callback (Ty);
@@ -5047,7 +5073,7 @@ class CodeCompletionTypeContextAnalyzer {
5047
5073
break ;
5048
5074
}
5049
5075
default :
5050
- llvm_unreachable (" Unhandled expression kinds ." );
5076
+ llvm_unreachable (" Unhandled expression kind ." );
5051
5077
}
5052
5078
}
5053
5079
@@ -5072,7 +5098,7 @@ class CodeCompletionTypeContextAnalyzer {
5072
5098
}
5073
5099
break ;
5074
5100
default :
5075
- llvm_unreachable (" Unhandled statement kinds ." );
5101
+ llvm_unreachable (" Unhandled statement kind ." );
5076
5102
}
5077
5103
}
5078
5104
@@ -5114,7 +5140,22 @@ class CodeCompletionTypeContextAnalyzer {
5114
5140
break ;
5115
5141
}
5116
5142
default :
5117
- llvm_unreachable (" Unhandled decl kinds." );
5143
+ llvm_unreachable (" Unhandled decl kind." );
5144
+ }
5145
+ }
5146
+
5147
+ void analyzePattern (Pattern *P, llvm::function_ref<void (Type)> Callback) {
5148
+ switch (P->getKind ()) {
5149
+ case PatternKind::Expr: {
5150
+ auto ExprPat = cast<ExprPattern>(P);
5151
+ if (auto D = ExprPat->getMatchVar ()) {
5152
+ if (D->hasInterfaceType ())
5153
+ Callback (D->getInterfaceType ());
5154
+ }
5155
+ break ;
5156
+ }
5157
+ default :
5158
+ llvm_unreachable (" Unhandled pattern kind." );
5118
5159
}
5119
5160
}
5120
5161
@@ -5136,12 +5177,14 @@ class CodeCompletionTypeContextAnalyzer {
5136
5177
5137
5178
for (auto It = Finder.Ancestors .rbegin (); It != Finder.Ancestors .rend ();
5138
5179
++ It) {
5139
- if (auto Parent = It->dyn_cast <Expr *> ()) {
5180
+ if (auto Parent = It->getAsExpr ()) {
5140
5181
analyzeExpr (Parent, Callback, PossibleNames);
5141
- } else if (auto Parent = It->dyn_cast <Stmt *> ()) {
5182
+ } else if (auto Parent = It->getAsStmt ()) {
5142
5183
analyzeStmt (Parent, Callback);
5143
- } else if (auto Parent = It->dyn_cast <Decl *> ()) {
5184
+ } else if (auto Parent = It->getAsDecl ()) {
5144
5185
analyzeDecl (Parent, Callback);
5186
+ } else if (auto Parent = It->getAsPattern ()) {
5187
+ analyzePattern (Parent, Callback);
5145
5188
}
5146
5189
if (!PossibleTypes.empty () || !PossibleNames.empty ())
5147
5190
return true ;
@@ -5417,12 +5460,12 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5417
5460
case CompletionKind::UnresolvedMember : {
5418
5461
Lookup.setHaveDot (SourceLoc ());
5419
5462
SmallVector<Type, 1 > PossibleTypes;
5420
- ExprParentFinder Walker (UnresolvedExpr, [&](ASTNode Node) {
5421
- return Node.is <Expr *> ();
5463
+ ExprParentFinder Walker (UnresolvedExpr, [&](ASTWalker::ParentTy Node) {
5464
+ return Node.getAsExpr ();
5422
5465
});
5423
5466
CurDeclContext->walkContext (Walker);
5424
5467
bool Success = false ;
5425
- if (auto PE = Walker.ParentFarthest .get <Expr *> ()) {
5468
+ if (auto PE = Walker.ParentFarthest .getAsExpr ()) {
5426
5469
prepareForRetypechecking (PE);
5427
5470
Success = typeCheckUnresolvedExpr (*CurDeclContext, UnresolvedExpr, PE,
5428
5471
PossibleTypes);
@@ -5452,11 +5495,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5452
5495
5453
5496
case CompletionKind::ReturnStmtExpr : {
5454
5497
SourceLoc Loc = P.Context .SourceMgr .getCodeCompletionLoc ();
5455
- if (auto FD = dyn_cast<AbstractFunctionDecl>(CurDeclContext)) {
5456
- if (auto FT = FD->getInterfaceType ()->getAs <FunctionType>()) {
5457
- Lookup.setExpectedTypes (FT->getResult ());
5458
- }
5459
- }
5498
+ Lookup.setExpectedTypes (getReturnTypeFromContext (CurDeclContext));
5460
5499
Lookup.getValueCompletionsInDeclContext (Loc);
5461
5500
break ;
5462
5501
}
0 commit comments