@@ -5150,9 +5150,21 @@ class CodeCompletionTypeContextAnalyzer {
5150
5150
ASTContext &Context;
5151
5151
ExprParentFinder Finder;
5152
5152
5153
- bool collectArgumentExpectation (DeclContext &DC, Expr *E, Expr *CCExpr,
5154
- std::vector<Type> &ExpectedTypes,
5155
- std::vector<StringRef> &ExpectedNames) {
5153
+ // Results populated by Analyze()
5154
+ SmallVector<Type, 2 > PossibleTypes;
5155
+ SmallVector<StringRef, 2 > PossibleNames;
5156
+
5157
+ void recordPossibleType (Type ty) {
5158
+ if (!ty || ty->is <ErrorType>())
5159
+ return ;
5160
+ PossibleTypes.push_back (ty->getRValueType ());
5161
+ }
5162
+
5163
+ void recordPossibleName (StringRef name) {
5164
+ PossibleNames.push_back (name);
5165
+ }
5166
+
5167
+ bool collectArgumentExpectation (DeclContext &DC, Expr *E, Expr *CCExpr) {
5156
5168
// Collect parameter lists for possible func decls.
5157
5169
SmallVector<FunctionParams, 4 > Candidates;
5158
5170
Expr *Arg = nullptr ;
@@ -5186,14 +5198,14 @@ class CodeCompletionTypeContextAnalyzer {
5186
5198
const auto &Param = Params[Position];
5187
5199
if (Param.hasLabel () && MayNeedName) {
5188
5200
if (seenNames.insert (Param.getLabel ()).second )
5189
- ExpectedNames. push_back (Param.getLabel ().str ());
5201
+ recordPossibleName (Param.getLabel ().str ());
5190
5202
} else {
5191
5203
if (seenTypes.insert (Param.getOldType ().getPointer ()).second )
5192
- ExpectedTypes. push_back (Param.getOldType ());
5204
+ recordPossibleType (Param.getOldType ());
5193
5205
}
5194
5206
}
5195
5207
}
5196
- return !ExpectedTypes .empty () || !ExpectedNames .empty ();
5208
+ return !PossibleTypes .empty () || !PossibleNames .empty ();
5197
5209
}
5198
5210
5199
5211
public:
@@ -5249,21 +5261,13 @@ class CodeCompletionTypeContextAnalyzer {
5249
5261
return false ;
5250
5262
}) {}
5251
5263
5252
- void analyzeExpr (Expr *Parent, llvm::function_ref<void (Type)> Callback,
5253
- SmallVectorImpl<StringRef> &PossibleNames) {
5264
+ void analyzeExpr (Expr *Parent) {
5254
5265
switch (Parent->getKind ()) {
5255
5266
case ExprKind::Call:
5256
5267
case ExprKind::Subscript:
5257
5268
case ExprKind::Binary:
5258
5269
case ExprKind::PrefixUnary: {
5259
- std::vector<Type> PotentialTypes;
5260
- std::vector<StringRef> ExpectedNames;
5261
- collectArgumentExpectation (
5262
- *DC, Parent, ParsedExpr, PotentialTypes, ExpectedNames);
5263
- for (Type Ty : PotentialTypes)
5264
- Callback (Ty);
5265
- for (auto name : ExpectedNames)
5266
- PossibleNames.push_back (name);
5270
+ collectArgumentExpectation (*DC, Parent, ParsedExpr);
5267
5271
break ;
5268
5272
}
5269
5273
case ExprKind::Assign: {
@@ -5276,10 +5280,10 @@ class CodeCompletionTypeContextAnalyzer {
5276
5280
// The destination is of the expected type.
5277
5281
auto *destExpr = AE->getDest ();
5278
5282
if (auto type = destExpr->getType ()) {
5279
- Callback (type);
5283
+ recordPossibleType (type);
5280
5284
} else if (auto *DRE = dyn_cast<DeclRefExpr>(destExpr)) {
5281
5285
if (auto *decl = DRE->getDecl ())
5282
- Callback (decl->getInterfaceType ());
5286
+ recordPossibleType (decl->getInterfaceType ());
5283
5287
}
5284
5288
}
5285
5289
break ;
@@ -5290,7 +5294,7 @@ class CodeCompletionTypeContextAnalyzer {
5290
5294
unsigned Position = 0 ;
5291
5295
bool HasName;
5292
5296
if (getPositionInArgs (*DC, Parent, ParsedExpr, Position, HasName)) {
5293
- Callback (
5297
+ recordPossibleType (
5294
5298
Parent->getType ()->castTo <TupleType>()->getElementType (Position));
5295
5299
}
5296
5300
break ;
@@ -5300,15 +5304,16 @@ class CodeCompletionTypeContextAnalyzer {
5300
5304
}
5301
5305
}
5302
5306
5303
- void analyzeStmt (Stmt *Parent, llvm::function_ref< void (Type)> Callback ) {
5307
+ void analyzeStmt (Stmt *Parent) {
5304
5308
switch (Parent->getKind ()) {
5305
5309
case StmtKind::Return:
5306
- Callback (getReturnTypeFromContext (DC));
5310
+ recordPossibleType (getReturnTypeFromContext (DC));
5307
5311
break ;
5308
5312
case StmtKind::ForEach:
5309
5313
if (auto SEQ = cast<ForEachStmt>(Parent)->getSequence ()) {
5310
5314
if (containsTarget (SEQ)) {
5311
- Callback (Context.getSequenceDecl ()->getDeclaredInterfaceType ());
5315
+ recordPossibleType (
5316
+ Context.getSequenceDecl ()->getDeclaredInterfaceType ());
5312
5317
}
5313
5318
}
5314
5319
break ;
@@ -5317,7 +5322,7 @@ class CodeCompletionTypeContextAnalyzer {
5317
5322
case StmtKind::While:
5318
5323
case StmtKind::Guard:
5319
5324
if (isBoolConditionOf (Parent)) {
5320
- Callback (Context.getBoolDecl ()->getDeclaredInterfaceType ());
5325
+ recordPossibleType (Context.getBoolDecl ()->getDeclaredInterfaceType ());
5321
5326
}
5322
5327
break ;
5323
5328
default :
@@ -5346,15 +5351,15 @@ class CodeCompletionTypeContextAnalyzer {
5346
5351
return SM.rangeContains (E->getSourceRange (), ParsedExpr->getSourceRange ());
5347
5352
}
5348
5353
5349
- void analyzeDecl (Decl *D, llvm::function_ref< void (Type)> Callback ) {
5354
+ void analyzeDecl (Decl *D) {
5350
5355
switch (D->getKind ()) {
5351
5356
case DeclKind::PatternBinding: {
5352
5357
auto PBD = cast<PatternBindingDecl>(D);
5353
5358
for (unsigned I = 0 ; I < PBD->getNumPatternEntries (); ++ I) {
5354
5359
if (auto Init = PBD->getInit (I)) {
5355
5360
if (containsTarget (Init)) {
5356
5361
if (PBD->getPattern (I)->hasType ()) {
5357
- Callback (PBD->getPattern (I)->getType ());
5362
+ recordPossibleType (PBD->getPattern (I)->getType ());
5358
5363
break ;
5359
5364
}
5360
5365
}
@@ -5367,13 +5372,13 @@ class CodeCompletionTypeContextAnalyzer {
5367
5372
}
5368
5373
}
5369
5374
5370
- void analyzePattern (Pattern *P, llvm::function_ref< void (Type)> Callback ) {
5375
+ void analyzePattern (Pattern *P) {
5371
5376
switch (P->getKind ()) {
5372
5377
case PatternKind::Expr: {
5373
5378
auto ExprPat = cast<ExprPattern>(P);
5374
5379
if (auto D = ExprPat->getMatchVar ()) {
5375
5380
if (D->hasInterfaceType ())
5376
- Callback (D->getInterfaceType ());
5381
+ recordPossibleType (D->getInterfaceType ());
5377
5382
}
5378
5383
break ;
5379
5384
}
@@ -5382,38 +5387,31 @@ class CodeCompletionTypeContextAnalyzer {
5382
5387
}
5383
5388
}
5384
5389
5385
- bool Analyze (llvm::SmallVectorImpl<Type> &PossibleTypes) {
5386
- SmallVector<StringRef, 1 > PossibleNames;
5387
- return Analyze (PossibleTypes, PossibleNames) && !PossibleTypes.empty ();
5388
- }
5389
- bool Analyze (SmallVectorImpl<Type> &PossibleTypes,
5390
- SmallVectorImpl<StringRef> &PossibleNames) {
5390
+ bool Analyze () {
5391
5391
// We cannot analyze without target.
5392
5392
if (!ParsedExpr)
5393
5393
return false ;
5394
5394
DC->walkContext (Finder);
5395
- auto Callback = [&] (Type Result) {
5396
- if (Result &&
5397
- Result->getKind () != TypeKind::Error)
5398
- PossibleTypes.push_back (Result->getRValueType ());
5399
- };
5400
5395
5401
5396
for (auto It = Finder.Ancestors .rbegin (); It != Finder.Ancestors .rend ();
5402
5397
++ It) {
5403
5398
if (auto Parent = It->getAsExpr ()) {
5404
- analyzeExpr (Parent, Callback, PossibleNames );
5399
+ analyzeExpr (Parent);
5405
5400
} else if (auto Parent = It->getAsStmt ()) {
5406
- analyzeStmt (Parent, Callback );
5401
+ analyzeStmt (Parent);
5407
5402
} else if (auto Parent = It->getAsDecl ()) {
5408
- analyzeDecl (Parent, Callback );
5403
+ analyzeDecl (Parent);
5409
5404
} else if (auto Parent = It->getAsPattern ()) {
5410
- analyzePattern (Parent, Callback );
5405
+ analyzePattern (Parent);
5411
5406
}
5412
5407
if (!PossibleTypes.empty () || !PossibleNames.empty ())
5413
5408
return true ;
5414
5409
}
5415
5410
return false ;
5416
5411
}
5412
+
5413
+ ArrayRef<Type> getPossibleTypes () const { return PossibleTypes; }
5414
+ ArrayRef<StringRef> getPossibleNames () const { return PossibleNames; }
5417
5415
};
5418
5416
5419
5417
} // end anonymous namespace
@@ -5510,9 +5508,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5510
5508
5511
5509
::CodeCompletionTypeContextAnalyzer TypeAnalyzer (CurDeclContext,
5512
5510
ParsedExpr);
5513
- llvm::SmallVector<Type, 2 > PossibleTypes;
5514
- if (TypeAnalyzer.Analyze (PossibleTypes)) {
5515
- Lookup.setExpectedTypes (PossibleTypes);
5511
+ if (TypeAnalyzer.Analyze ()) {
5512
+ Lookup.setExpectedTypes (TypeAnalyzer.getPossibleTypes ());
5516
5513
}
5517
5514
Lookup.getValueExprCompletions (*ExprType, ReferencedDecl.getDecl ());
5518
5515
break ;
@@ -5555,9 +5552,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5555
5552
case CompletionKind::PostfixExprBeginning: {
5556
5553
::CodeCompletionTypeContextAnalyzer Analyzer (CurDeclContext,
5557
5554
CodeCompleteTokenExpr);
5558
- llvm::SmallVector<Type, 1 > Types;
5559
- if (Analyzer.Analyze (Types)) {
5560
- Lookup.setExpectedTypes (Types);
5555
+ if (Analyzer.Analyze ()) {
5556
+ Lookup.setExpectedTypes (Analyzer.getPossibleTypes ());
5561
5557
}
5562
5558
DoPostfixExprBeginning ();
5563
5559
break ;
@@ -5583,18 +5579,16 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5583
5579
5584
5580
::CodeCompletionTypeContextAnalyzer TypeAnalyzer (CurDeclContext,
5585
5581
CodeCompleteTokenExpr);
5586
- SmallVector<Type, 2 > PossibleTypes;
5587
- SmallVector<StringRef, 2 > PossibleNames;
5588
- if (TypeAnalyzer.Analyze (PossibleTypes, PossibleNames)) {
5589
- Lookup.setExpectedTypes (PossibleTypes);
5582
+ if (TypeAnalyzer.Analyze ()) {
5583
+ Lookup.setExpectedTypes (TypeAnalyzer.getPossibleTypes ());
5590
5584
}
5591
5585
5592
5586
if (ExprType) {
5593
5587
if (ShouldCompleteCallPatternAfterParen) {
5594
5588
Lookup.getValueExprCompletions (*ExprType, ReferencedDecl.getDecl ());
5595
5589
} else {
5596
5590
// Add argument labels, then fallthrough to get values.
5597
- Lookup.addArgNameCompletionResults (PossibleNames );
5591
+ Lookup.addArgNameCompletionResults (TypeAnalyzer. getPossibleNames () );
5598
5592
}
5599
5593
}
5600
5594
@@ -5705,12 +5699,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5705
5699
}
5706
5700
case CompletionKind::UnresolvedMember: {
5707
5701
Lookup.setHaveDot (DotLoc);
5708
- SmallVector<Type, 2 > PossibleTypes;
5709
5702
::CodeCompletionTypeContextAnalyzer TypeAnalyzer (CurDeclContext,
5710
5703
CodeCompleteTokenExpr);
5711
- if (TypeAnalyzer.Analyze (PossibleTypes ))
5712
- Lookup.setExpectedTypes (PossibleTypes );
5713
- Lookup.getUnresolvedMemberCompletions (PossibleTypes );
5704
+ if (TypeAnalyzer.Analyze ())
5705
+ Lookup.setExpectedTypes (TypeAnalyzer. getPossibleTypes () );
5706
+ Lookup.getUnresolvedMemberCompletions (TypeAnalyzer. getPossibleTypes () );
5714
5707
break ;
5715
5708
}
5716
5709
case CompletionKind::AssignmentRHS : {
@@ -5723,14 +5716,12 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5723
5716
case CompletionKind::CallArg : {
5724
5717
::CodeCompletionTypeContextAnalyzer Analyzer (CurDeclContext,
5725
5718
CodeCompleteTokenExpr);
5726
- SmallVector<Type, 2 > PossibleTypes;
5727
- SmallVector<StringRef, 2 > PossibleNames;
5728
- Analyzer.Analyze (PossibleTypes, PossibleNames);
5729
- if (!PossibleNames.empty ()) {
5730
- Lookup.addArgNameCompletionResults (PossibleNames);
5719
+ Analyzer.Analyze ();
5720
+ if (!Analyzer.getPossibleNames ().empty ()) {
5721
+ Lookup.addArgNameCompletionResults (Analyzer.getPossibleNames ());
5731
5722
break ;
5732
5723
}
5733
- Lookup.setExpectedTypes (PossibleTypes );
5724
+ Lookup.setExpectedTypes (Analyzer. getPossibleTypes () );
5734
5725
DoPostfixExprBeginning ();
5735
5726
break ;
5736
5727
}
0 commit comments