Skip to content

Commit bfa5ea2

Browse files
committed
[CodeCompletion][NFC] Store context results in analyzer member fields
To simplify call-site code. (cherry picked from commit 1af8078)
1 parent ed6ea5f commit bfa5ea2

File tree

1 file changed

+54
-63
lines changed

1 file changed

+54
-63
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5272,9 +5272,21 @@ class CodeCompletionTypeContextAnalyzer {
52725272
ASTContext &Context;
52735273
ExprParentFinder Finder;
52745274

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) {
52785290
// Collect parameter lists for possible func decls.
52795291
SmallVector<FunctionParams, 4> Candidates;
52805292
Expr *Arg = nullptr;
@@ -5308,14 +5320,14 @@ class CodeCompletionTypeContextAnalyzer {
53085320
const auto &Param = Params[Position];
53095321
if (Param.hasLabel() && MayNeedName) {
53105322
if (seenNames.insert(Param.getLabel()).second)
5311-
ExpectedNames.push_back(Param.getLabel().str());
5323+
recordPossibleName(Param.getLabel().str());
53125324
} else {
53135325
if (seenTypes.insert(Param.getOldType().getPointer()).second)
5314-
ExpectedTypes.push_back(Param.getOldType());
5326+
recordPossibleType(Param.getOldType());
53155327
}
53165328
}
53175329
}
5318-
return !ExpectedTypes.empty() || !ExpectedNames.empty();
5330+
return !PossibleTypes.empty() || !PossibleNames.empty();
53195331
}
53205332

53215333
public:
@@ -5371,21 +5383,13 @@ class CodeCompletionTypeContextAnalyzer {
53715383
return false;
53725384
}) {}
53735385

5374-
void analyzeExpr(Expr *Parent, llvm::function_ref<void(Type)> Callback,
5375-
SmallVectorImpl<StringRef> &PossibleNames) {
5386+
void analyzeExpr(Expr *Parent) {
53765387
switch (Parent->getKind()) {
53775388
case ExprKind::Call:
53785389
case ExprKind::Subscript:
53795390
case ExprKind::Binary:
53805391
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);
53895393
break;
53905394
}
53915395
case ExprKind::Assign: {
@@ -5398,10 +5402,10 @@ class CodeCompletionTypeContextAnalyzer {
53985402
// The destination is of the expected type.
53995403
auto *destExpr = AE->getDest();
54005404
if (auto type = destExpr->getType()) {
5401-
Callback(type);
5405+
recordPossibleType(type);
54025406
} else if (auto *DRE = dyn_cast<DeclRefExpr>(destExpr)) {
54035407
if (auto *decl = DRE->getDecl())
5404-
Callback(decl->getInterfaceType());
5408+
recordPossibleType(decl->getInterfaceType());
54055409
}
54065410
}
54075411
break;
@@ -5412,7 +5416,7 @@ class CodeCompletionTypeContextAnalyzer {
54125416
unsigned Position = 0;
54135417
bool HasName;
54145418
if (getPositionInArgs(*DC, Parent, ParsedExpr, Position, HasName)) {
5415-
Callback(
5419+
recordPossibleType(
54165420
Parent->getType()->castTo<TupleType>()->getElementType(Position));
54175421
}
54185422
break;
@@ -5422,15 +5426,16 @@ class CodeCompletionTypeContextAnalyzer {
54225426
}
54235427
}
54245428

5425-
void analyzeStmt(Stmt *Parent, llvm::function_ref<void(Type)> Callback) {
5429+
void analyzeStmt(Stmt *Parent) {
54265430
switch (Parent->getKind()) {
54275431
case StmtKind::Return:
5428-
Callback(getReturnTypeFromContext(DC));
5432+
recordPossibleType(getReturnTypeFromContext(DC));
54295433
break;
54305434
case StmtKind::ForEach:
54315435
if (auto SEQ = cast<ForEachStmt>(Parent)->getSequence()) {
54325436
if (containsTarget(SEQ)) {
5433-
Callback(Context.getSequenceDecl()->getDeclaredInterfaceType());
5437+
recordPossibleType(
5438+
Context.getSequenceDecl()->getDeclaredInterfaceType());
54345439
}
54355440
}
54365441
break;
@@ -5439,7 +5444,7 @@ class CodeCompletionTypeContextAnalyzer {
54395444
case StmtKind::While:
54405445
case StmtKind::Guard:
54415446
if (isBoolConditionOf(Parent)) {
5442-
Callback(Context.getBoolDecl()->getDeclaredInterfaceType());
5447+
recordPossibleType(Context.getBoolDecl()->getDeclaredInterfaceType());
54435448
}
54445449
break;
54455450
default:
@@ -5468,15 +5473,15 @@ class CodeCompletionTypeContextAnalyzer {
54685473
return SM.rangeContains(E->getSourceRange(), ParsedExpr->getSourceRange());
54695474
}
54705475

5471-
void analyzeDecl(Decl *D, llvm::function_ref<void(Type)> Callback) {
5476+
void analyzeDecl(Decl *D) {
54725477
switch (D->getKind()) {
54735478
case DeclKind::PatternBinding: {
54745479
auto PBD = cast<PatternBindingDecl>(D);
54755480
for (unsigned I = 0; I < PBD->getNumPatternEntries(); ++ I) {
54765481
if (auto Init = PBD->getInit(I)) {
54775482
if (containsTarget(Init)) {
54785483
if (PBD->getPattern(I)->hasType()) {
5479-
Callback(PBD->getPattern(I)->getType());
5484+
recordPossibleType(PBD->getPattern(I)->getType());
54805485
break;
54815486
}
54825487
}
@@ -5489,13 +5494,13 @@ class CodeCompletionTypeContextAnalyzer {
54895494
}
54905495
}
54915496

5492-
void analyzePattern(Pattern *P, llvm::function_ref<void(Type)> Callback) {
5497+
void analyzePattern(Pattern *P) {
54935498
switch (P->getKind()) {
54945499
case PatternKind::Expr: {
54955500
auto ExprPat = cast<ExprPattern>(P);
54965501
if (auto D = ExprPat->getMatchVar()) {
54975502
if (D->hasInterfaceType())
5498-
Callback(D->getInterfaceType());
5503+
recordPossibleType(D->getInterfaceType());
54995504
}
55005505
break;
55015506
}
@@ -5504,38 +5509,31 @@ class CodeCompletionTypeContextAnalyzer {
55045509
}
55055510
}
55065511

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() {
55135513
// We cannot analyze without target.
55145514
if (!ParsedExpr)
55155515
return false;
55165516
DC->walkContext(Finder);
5517-
auto Callback = [&] (Type Result) {
5518-
if (Result &&
5519-
Result->getKind() != TypeKind::Error)
5520-
PossibleTypes.push_back(Result->getRValueType());
5521-
};
55225517

55235518
for (auto It = Finder.Ancestors.rbegin(); It != Finder.Ancestors.rend();
55245519
++ It) {
55255520
if (auto Parent = It->getAsExpr()) {
5526-
analyzeExpr(Parent, Callback, PossibleNames);
5521+
analyzeExpr(Parent);
55275522
} else if (auto Parent = It->getAsStmt()) {
5528-
analyzeStmt(Parent, Callback);
5523+
analyzeStmt(Parent);
55295524
} else if (auto Parent = It->getAsDecl()) {
5530-
analyzeDecl(Parent, Callback);
5525+
analyzeDecl(Parent);
55315526
} else if (auto Parent = It->getAsPattern()) {
5532-
analyzePattern(Parent, Callback);
5527+
analyzePattern(Parent);
55335528
}
55345529
if (!PossibleTypes.empty() || !PossibleNames.empty())
55355530
return true;
55365531
}
55375532
return false;
55385533
}
5534+
5535+
ArrayRef<Type> getPossibleTypes() const { return PossibleTypes; }
5536+
ArrayRef<StringRef> getPossibleNames() const { return PossibleNames; }
55395537
};
55405538

55415539
} // end anonymous namespace
@@ -5632,9 +5630,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
56325630

56335631
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
56345632
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());
56385635
}
56395636
Lookup.getValueExprCompletions(*ExprType, ReferencedDecl.getDecl());
56405637
break;
@@ -5677,9 +5674,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
56775674
case CompletionKind::PostfixExprBeginning: {
56785675
::CodeCompletionTypeContextAnalyzer Analyzer(CurDeclContext,
56795676
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());
56835679
}
56845680
DoPostfixExprBeginning();
56855681
break;
@@ -5705,18 +5701,16 @@ void CodeCompletionCallbacksImpl::doneParsing() {
57055701

57065702
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
57075703
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());
57125706
}
57135707

57145708
if (ExprType) {
57155709
if (ShouldCompleteCallPatternAfterParen) {
57165710
Lookup.getValueExprCompletions(*ExprType, ReferencedDecl.getDecl());
57175711
} else {
57185712
// Add argument labels, then fallthrough to get values.
5719-
Lookup.addArgNameCompletionResults(PossibleNames);
5713+
Lookup.addArgNameCompletionResults(TypeAnalyzer.getPossibleNames());
57205714
}
57215715
}
57225716

@@ -5827,12 +5821,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
58275821
}
58285822
case CompletionKind::UnresolvedMember: {
58295823
Lookup.setHaveDot(DotLoc);
5830-
SmallVector<Type, 2> PossibleTypes;
58315824
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
58325825
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());
58365829
break;
58375830
}
58385831
case CompletionKind::AssignmentRHS : {
@@ -5845,14 +5838,12 @@ void CodeCompletionCallbacksImpl::doneParsing() {
58455838
case CompletionKind::CallArg : {
58465839
::CodeCompletionTypeContextAnalyzer Analyzer(CurDeclContext,
58475840
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());
58535844
break;
58545845
}
5855-
Lookup.setExpectedTypes(PossibleTypes);
5846+
Lookup.setExpectedTypes(Analyzer.getPossibleTypes());
58565847
DoPostfixExprBeginning();
58575848
break;
58585849
}

0 commit comments

Comments
 (0)