@@ -5272,9 +5272,21 @@ class CodeCompletionTypeContextAnalyzer {
5272
5272
ASTContext &Context;
5273
5273
ExprParentFinder Finder;
5274
5274
5275
- bool collectArgumentExpectation (DeclContext &DC, Expr *E, Expr *CCExpr,
5276
- std::vector<Type> &ExpectedTypes,
5277
- std::vector<StringRef> &ExpectedNames) {
5275
+ // Results populated by Analyze()
5276
+ SmallVector<Type, 2 > PossibleTypes;
5277
+ SmallVector<StringRef, 2 > PossibleNames;
5278
+
5279
+ void recordPossibleType (Type ty) {
5280
+ if (!ty || ty->is <ErrorType>())
5281
+ return ;
5282
+ PossibleTypes.push_back (ty->getRValueType ());
5283
+ }
5284
+
5285
+ void recordPossibleName (StringRef name) {
5286
+ PossibleNames.push_back (name);
5287
+ }
5288
+
5289
+ bool collectArgumentExpectation (DeclContext &DC, Expr *E, Expr *CCExpr) {
5278
5290
// Collect parameter lists for possible func decls.
5279
5291
SmallVector<FunctionParams, 4 > Candidates;
5280
5292
Expr *Arg = nullptr ;
@@ -5308,14 +5320,14 @@ class CodeCompletionTypeContextAnalyzer {
5308
5320
const auto &Param = Params[Position];
5309
5321
if (Param.hasLabel () && MayNeedName) {
5310
5322
if (seenNames.insert (Param.getLabel ()).second )
5311
- ExpectedNames. push_back (Param.getLabel ().str ());
5323
+ recordPossibleName (Param.getLabel ().str ());
5312
5324
} else {
5313
5325
if (seenTypes.insert (Param.getOldType ().getPointer ()).second )
5314
- ExpectedTypes. push_back (Param.getOldType ());
5326
+ recordPossibleType (Param.getOldType ());
5315
5327
}
5316
5328
}
5317
5329
}
5318
- return !ExpectedTypes .empty () || !ExpectedNames .empty ();
5330
+ return !PossibleTypes .empty () || !PossibleNames .empty ();
5319
5331
}
5320
5332
5321
5333
public:
@@ -5371,21 +5383,13 @@ class CodeCompletionTypeContextAnalyzer {
5371
5383
return false ;
5372
5384
}) {}
5373
5385
5374
- void analyzeExpr (Expr *Parent, llvm::function_ref<void (Type)> Callback,
5375
- SmallVectorImpl<StringRef> &PossibleNames) {
5386
+ void analyzeExpr (Expr *Parent) {
5376
5387
switch (Parent->getKind ()) {
5377
5388
case ExprKind::Call:
5378
5389
case ExprKind::Subscript:
5379
5390
case ExprKind::Binary:
5380
5391
case ExprKind::PrefixUnary: {
5381
- std::vector<Type> PotentialTypes;
5382
- std::vector<StringRef> ExpectedNames;
5383
- collectArgumentExpectation (
5384
- *DC, Parent, ParsedExpr, PotentialTypes, ExpectedNames);
5385
- for (Type Ty : PotentialTypes)
5386
- Callback (Ty);
5387
- for (auto name : ExpectedNames)
5388
- PossibleNames.push_back (name);
5392
+ collectArgumentExpectation (*DC, Parent, ParsedExpr);
5389
5393
break ;
5390
5394
}
5391
5395
case ExprKind::Assign: {
@@ -5398,10 +5402,10 @@ class CodeCompletionTypeContextAnalyzer {
5398
5402
// The destination is of the expected type.
5399
5403
auto *destExpr = AE->getDest ();
5400
5404
if (auto type = destExpr->getType ()) {
5401
- Callback (type);
5405
+ recordPossibleType (type);
5402
5406
} else if (auto *DRE = dyn_cast<DeclRefExpr>(destExpr)) {
5403
5407
if (auto *decl = DRE->getDecl ())
5404
- Callback (decl->getInterfaceType ());
5408
+ recordPossibleType (decl->getInterfaceType ());
5405
5409
}
5406
5410
}
5407
5411
break ;
@@ -5412,7 +5416,7 @@ class CodeCompletionTypeContextAnalyzer {
5412
5416
unsigned Position = 0 ;
5413
5417
bool HasName;
5414
5418
if (getPositionInArgs (*DC, Parent, ParsedExpr, Position, HasName)) {
5415
- Callback (
5419
+ recordPossibleType (
5416
5420
Parent->getType ()->castTo <TupleType>()->getElementType (Position));
5417
5421
}
5418
5422
break ;
@@ -5422,15 +5426,16 @@ class CodeCompletionTypeContextAnalyzer {
5422
5426
}
5423
5427
}
5424
5428
5425
- void analyzeStmt (Stmt *Parent, llvm::function_ref< void (Type)> Callback ) {
5429
+ void analyzeStmt (Stmt *Parent) {
5426
5430
switch (Parent->getKind ()) {
5427
5431
case StmtKind::Return:
5428
- Callback (getReturnTypeFromContext (DC));
5432
+ recordPossibleType (getReturnTypeFromContext (DC));
5429
5433
break ;
5430
5434
case StmtKind::ForEach:
5431
5435
if (auto SEQ = cast<ForEachStmt>(Parent)->getSequence ()) {
5432
5436
if (containsTarget (SEQ)) {
5433
- Callback (Context.getSequenceDecl ()->getDeclaredInterfaceType ());
5437
+ recordPossibleType (
5438
+ Context.getSequenceDecl ()->getDeclaredInterfaceType ());
5434
5439
}
5435
5440
}
5436
5441
break ;
@@ -5439,7 +5444,7 @@ class CodeCompletionTypeContextAnalyzer {
5439
5444
case StmtKind::While:
5440
5445
case StmtKind::Guard:
5441
5446
if (isBoolConditionOf (Parent)) {
5442
- Callback (Context.getBoolDecl ()->getDeclaredInterfaceType ());
5447
+ recordPossibleType (Context.getBoolDecl ()->getDeclaredInterfaceType ());
5443
5448
}
5444
5449
break ;
5445
5450
default :
@@ -5468,15 +5473,15 @@ class CodeCompletionTypeContextAnalyzer {
5468
5473
return SM.rangeContains (E->getSourceRange (), ParsedExpr->getSourceRange ());
5469
5474
}
5470
5475
5471
- void analyzeDecl (Decl *D, llvm::function_ref< void (Type)> Callback ) {
5476
+ void analyzeDecl (Decl *D) {
5472
5477
switch (D->getKind ()) {
5473
5478
case DeclKind::PatternBinding: {
5474
5479
auto PBD = cast<PatternBindingDecl>(D);
5475
5480
for (unsigned I = 0 ; I < PBD->getNumPatternEntries (); ++ I) {
5476
5481
if (auto Init = PBD->getInit (I)) {
5477
5482
if (containsTarget (Init)) {
5478
5483
if (PBD->getPattern (I)->hasType ()) {
5479
- Callback (PBD->getPattern (I)->getType ());
5484
+ recordPossibleType (PBD->getPattern (I)->getType ());
5480
5485
break ;
5481
5486
}
5482
5487
}
@@ -5489,13 +5494,13 @@ class CodeCompletionTypeContextAnalyzer {
5489
5494
}
5490
5495
}
5491
5496
5492
- void analyzePattern (Pattern *P, llvm::function_ref< void (Type)> Callback ) {
5497
+ void analyzePattern (Pattern *P) {
5493
5498
switch (P->getKind ()) {
5494
5499
case PatternKind::Expr: {
5495
5500
auto ExprPat = cast<ExprPattern>(P);
5496
5501
if (auto D = ExprPat->getMatchVar ()) {
5497
5502
if (D->hasInterfaceType ())
5498
- Callback (D->getInterfaceType ());
5503
+ recordPossibleType (D->getInterfaceType ());
5499
5504
}
5500
5505
break ;
5501
5506
}
@@ -5504,38 +5509,31 @@ class CodeCompletionTypeContextAnalyzer {
5504
5509
}
5505
5510
}
5506
5511
5507
- bool Analyze (llvm::SmallVectorImpl<Type> &PossibleTypes) {
5508
- SmallVector<StringRef, 1 > PossibleNames;
5509
- return Analyze (PossibleTypes, PossibleNames) && !PossibleTypes.empty ();
5510
- }
5511
- bool Analyze (SmallVectorImpl<Type> &PossibleTypes,
5512
- SmallVectorImpl<StringRef> &PossibleNames) {
5512
+ bool Analyze () {
5513
5513
// We cannot analyze without target.
5514
5514
if (!ParsedExpr)
5515
5515
return false ;
5516
5516
DC->walkContext (Finder);
5517
- auto Callback = [&] (Type Result) {
5518
- if (Result &&
5519
- Result->getKind () != TypeKind::Error)
5520
- PossibleTypes.push_back (Result->getRValueType ());
5521
- };
5522
5517
5523
5518
for (auto It = Finder.Ancestors .rbegin (); It != Finder.Ancestors .rend ();
5524
5519
++ It) {
5525
5520
if (auto Parent = It->getAsExpr ()) {
5526
- analyzeExpr (Parent, Callback, PossibleNames );
5521
+ analyzeExpr (Parent);
5527
5522
} else if (auto Parent = It->getAsStmt ()) {
5528
- analyzeStmt (Parent, Callback );
5523
+ analyzeStmt (Parent);
5529
5524
} else if (auto Parent = It->getAsDecl ()) {
5530
- analyzeDecl (Parent, Callback );
5525
+ analyzeDecl (Parent);
5531
5526
} else if (auto Parent = It->getAsPattern ()) {
5532
- analyzePattern (Parent, Callback );
5527
+ analyzePattern (Parent);
5533
5528
}
5534
5529
if (!PossibleTypes.empty () || !PossibleNames.empty ())
5535
5530
return true ;
5536
5531
}
5537
5532
return false ;
5538
5533
}
5534
+
5535
+ ArrayRef<Type> getPossibleTypes () const { return PossibleTypes; }
5536
+ ArrayRef<StringRef> getPossibleNames () const { return PossibleNames; }
5539
5537
};
5540
5538
5541
5539
} // end anonymous namespace
@@ -5632,9 +5630,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5632
5630
5633
5631
::CodeCompletionTypeContextAnalyzer TypeAnalyzer (CurDeclContext,
5634
5632
ParsedExpr);
5635
- llvm::SmallVector<Type, 2 > PossibleTypes;
5636
- if (TypeAnalyzer.Analyze (PossibleTypes)) {
5637
- Lookup.setExpectedTypes (PossibleTypes);
5633
+ if (TypeAnalyzer.Analyze ()) {
5634
+ Lookup.setExpectedTypes (TypeAnalyzer.getPossibleTypes ());
5638
5635
}
5639
5636
Lookup.getValueExprCompletions (*ExprType, ReferencedDecl.getDecl ());
5640
5637
break ;
@@ -5677,9 +5674,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5677
5674
case CompletionKind::PostfixExprBeginning: {
5678
5675
::CodeCompletionTypeContextAnalyzer Analyzer (CurDeclContext,
5679
5676
CodeCompleteTokenExpr);
5680
- llvm::SmallVector<Type, 1 > Types;
5681
- if (Analyzer.Analyze (Types)) {
5682
- Lookup.setExpectedTypes (Types);
5677
+ if (Analyzer.Analyze ()) {
5678
+ Lookup.setExpectedTypes (Analyzer.getPossibleTypes ());
5683
5679
}
5684
5680
DoPostfixExprBeginning ();
5685
5681
break ;
@@ -5705,18 +5701,16 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5705
5701
5706
5702
::CodeCompletionTypeContextAnalyzer TypeAnalyzer (CurDeclContext,
5707
5703
CodeCompleteTokenExpr);
5708
- SmallVector<Type, 2 > PossibleTypes;
5709
- SmallVector<StringRef, 2 > PossibleNames;
5710
- if (TypeAnalyzer.Analyze (PossibleTypes, PossibleNames)) {
5711
- Lookup.setExpectedTypes (PossibleTypes);
5704
+ if (TypeAnalyzer.Analyze ()) {
5705
+ Lookup.setExpectedTypes (TypeAnalyzer.getPossibleTypes ());
5712
5706
}
5713
5707
5714
5708
if (ExprType) {
5715
5709
if (ShouldCompleteCallPatternAfterParen) {
5716
5710
Lookup.getValueExprCompletions (*ExprType, ReferencedDecl.getDecl ());
5717
5711
} else {
5718
5712
// Add argument labels, then fallthrough to get values.
5719
- Lookup.addArgNameCompletionResults (PossibleNames );
5713
+ Lookup.addArgNameCompletionResults (TypeAnalyzer. getPossibleNames () );
5720
5714
}
5721
5715
}
5722
5716
@@ -5827,12 +5821,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5827
5821
}
5828
5822
case CompletionKind::UnresolvedMember: {
5829
5823
Lookup.setHaveDot (DotLoc);
5830
- SmallVector<Type, 2 > PossibleTypes;
5831
5824
::CodeCompletionTypeContextAnalyzer TypeAnalyzer (CurDeclContext,
5832
5825
CodeCompleteTokenExpr);
5833
- if (TypeAnalyzer.Analyze (PossibleTypes ))
5834
- Lookup.setExpectedTypes (PossibleTypes );
5835
- Lookup.getUnresolvedMemberCompletions (PossibleTypes );
5826
+ if (TypeAnalyzer.Analyze ())
5827
+ Lookup.setExpectedTypes (TypeAnalyzer. getPossibleTypes () );
5828
+ Lookup.getUnresolvedMemberCompletions (TypeAnalyzer. getPossibleTypes () );
5836
5829
break ;
5837
5830
}
5838
5831
case CompletionKind::AssignmentRHS : {
@@ -5845,14 +5838,12 @@ void CodeCompletionCallbacksImpl::doneParsing() {
5845
5838
case CompletionKind::CallArg : {
5846
5839
::CodeCompletionTypeContextAnalyzer Analyzer (CurDeclContext,
5847
5840
CodeCompleteTokenExpr);
5848
- SmallVector<Type, 2 > PossibleTypes;
5849
- SmallVector<StringRef, 2 > PossibleNames;
5850
- Analyzer.Analyze (PossibleTypes, PossibleNames);
5851
- if (!PossibleNames.empty ()) {
5852
- Lookup.addArgNameCompletionResults (PossibleNames);
5841
+ Analyzer.Analyze ();
5842
+ if (!Analyzer.getPossibleNames ().empty ()) {
5843
+ Lookup.addArgNameCompletionResults (Analyzer.getPossibleNames ());
5853
5844
break ;
5854
5845
}
5855
- Lookup.setExpectedTypes (PossibleTypes );
5846
+ Lookup.setExpectedTypes (Analyzer. getPossibleTypes () );
5856
5847
DoPostfixExprBeginning ();
5857
5848
break ;
5858
5849
}
0 commit comments