Skip to content

Commit 1af8078

Browse files
committed
[CodeCompletion][NFC] Store context results in analyzer member fields
To simplify call-site code.
1 parent 5ade8dd commit 1af8078

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
@@ -5150,9 +5150,21 @@ class CodeCompletionTypeContextAnalyzer {
51505150
ASTContext &Context;
51515151
ExprParentFinder Finder;
51525152

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) {
51565168
// Collect parameter lists for possible func decls.
51575169
SmallVector<FunctionParams, 4> Candidates;
51585170
Expr *Arg = nullptr;
@@ -5186,14 +5198,14 @@ class CodeCompletionTypeContextAnalyzer {
51865198
const auto &Param = Params[Position];
51875199
if (Param.hasLabel() && MayNeedName) {
51885200
if (seenNames.insert(Param.getLabel()).second)
5189-
ExpectedNames.push_back(Param.getLabel().str());
5201+
recordPossibleName(Param.getLabel().str());
51905202
} else {
51915203
if (seenTypes.insert(Param.getOldType().getPointer()).second)
5192-
ExpectedTypes.push_back(Param.getOldType());
5204+
recordPossibleType(Param.getOldType());
51935205
}
51945206
}
51955207
}
5196-
return !ExpectedTypes.empty() || !ExpectedNames.empty();
5208+
return !PossibleTypes.empty() || !PossibleNames.empty();
51975209
}
51985210

51995211
public:
@@ -5249,21 +5261,13 @@ class CodeCompletionTypeContextAnalyzer {
52495261
return false;
52505262
}) {}
52515263

5252-
void analyzeExpr(Expr *Parent, llvm::function_ref<void(Type)> Callback,
5253-
SmallVectorImpl<StringRef> &PossibleNames) {
5264+
void analyzeExpr(Expr *Parent) {
52545265
switch (Parent->getKind()) {
52555266
case ExprKind::Call:
52565267
case ExprKind::Subscript:
52575268
case ExprKind::Binary:
52585269
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);
52675271
break;
52685272
}
52695273
case ExprKind::Assign: {
@@ -5276,10 +5280,10 @@ class CodeCompletionTypeContextAnalyzer {
52765280
// The destination is of the expected type.
52775281
auto *destExpr = AE->getDest();
52785282
if (auto type = destExpr->getType()) {
5279-
Callback(type);
5283+
recordPossibleType(type);
52805284
} else if (auto *DRE = dyn_cast<DeclRefExpr>(destExpr)) {
52815285
if (auto *decl = DRE->getDecl())
5282-
Callback(decl->getInterfaceType());
5286+
recordPossibleType(decl->getInterfaceType());
52835287
}
52845288
}
52855289
break;
@@ -5290,7 +5294,7 @@ class CodeCompletionTypeContextAnalyzer {
52905294
unsigned Position = 0;
52915295
bool HasName;
52925296
if (getPositionInArgs(*DC, Parent, ParsedExpr, Position, HasName)) {
5293-
Callback(
5297+
recordPossibleType(
52945298
Parent->getType()->castTo<TupleType>()->getElementType(Position));
52955299
}
52965300
break;
@@ -5300,15 +5304,16 @@ class CodeCompletionTypeContextAnalyzer {
53005304
}
53015305
}
53025306

5303-
void analyzeStmt(Stmt *Parent, llvm::function_ref<void(Type)> Callback) {
5307+
void analyzeStmt(Stmt *Parent) {
53045308
switch (Parent->getKind()) {
53055309
case StmtKind::Return:
5306-
Callback(getReturnTypeFromContext(DC));
5310+
recordPossibleType(getReturnTypeFromContext(DC));
53075311
break;
53085312
case StmtKind::ForEach:
53095313
if (auto SEQ = cast<ForEachStmt>(Parent)->getSequence()) {
53105314
if (containsTarget(SEQ)) {
5311-
Callback(Context.getSequenceDecl()->getDeclaredInterfaceType());
5315+
recordPossibleType(
5316+
Context.getSequenceDecl()->getDeclaredInterfaceType());
53125317
}
53135318
}
53145319
break;
@@ -5317,7 +5322,7 @@ class CodeCompletionTypeContextAnalyzer {
53175322
case StmtKind::While:
53185323
case StmtKind::Guard:
53195324
if (isBoolConditionOf(Parent)) {
5320-
Callback(Context.getBoolDecl()->getDeclaredInterfaceType());
5325+
recordPossibleType(Context.getBoolDecl()->getDeclaredInterfaceType());
53215326
}
53225327
break;
53235328
default:
@@ -5346,15 +5351,15 @@ class CodeCompletionTypeContextAnalyzer {
53465351
return SM.rangeContains(E->getSourceRange(), ParsedExpr->getSourceRange());
53475352
}
53485353

5349-
void analyzeDecl(Decl *D, llvm::function_ref<void(Type)> Callback) {
5354+
void analyzeDecl(Decl *D) {
53505355
switch (D->getKind()) {
53515356
case DeclKind::PatternBinding: {
53525357
auto PBD = cast<PatternBindingDecl>(D);
53535358
for (unsigned I = 0; I < PBD->getNumPatternEntries(); ++ I) {
53545359
if (auto Init = PBD->getInit(I)) {
53555360
if (containsTarget(Init)) {
53565361
if (PBD->getPattern(I)->hasType()) {
5357-
Callback(PBD->getPattern(I)->getType());
5362+
recordPossibleType(PBD->getPattern(I)->getType());
53585363
break;
53595364
}
53605365
}
@@ -5367,13 +5372,13 @@ class CodeCompletionTypeContextAnalyzer {
53675372
}
53685373
}
53695374

5370-
void analyzePattern(Pattern *P, llvm::function_ref<void(Type)> Callback) {
5375+
void analyzePattern(Pattern *P) {
53715376
switch (P->getKind()) {
53725377
case PatternKind::Expr: {
53735378
auto ExprPat = cast<ExprPattern>(P);
53745379
if (auto D = ExprPat->getMatchVar()) {
53755380
if (D->hasInterfaceType())
5376-
Callback(D->getInterfaceType());
5381+
recordPossibleType(D->getInterfaceType());
53775382
}
53785383
break;
53795384
}
@@ -5382,38 +5387,31 @@ class CodeCompletionTypeContextAnalyzer {
53825387
}
53835388
}
53845389

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() {
53915391
// We cannot analyze without target.
53925392
if (!ParsedExpr)
53935393
return false;
53945394
DC->walkContext(Finder);
5395-
auto Callback = [&] (Type Result) {
5396-
if (Result &&
5397-
Result->getKind() != TypeKind::Error)
5398-
PossibleTypes.push_back(Result->getRValueType());
5399-
};
54005395

54015396
for (auto It = Finder.Ancestors.rbegin(); It != Finder.Ancestors.rend();
54025397
++ It) {
54035398
if (auto Parent = It->getAsExpr()) {
5404-
analyzeExpr(Parent, Callback, PossibleNames);
5399+
analyzeExpr(Parent);
54055400
} else if (auto Parent = It->getAsStmt()) {
5406-
analyzeStmt(Parent, Callback);
5401+
analyzeStmt(Parent);
54075402
} else if (auto Parent = It->getAsDecl()) {
5408-
analyzeDecl(Parent, Callback);
5403+
analyzeDecl(Parent);
54095404
} else if (auto Parent = It->getAsPattern()) {
5410-
analyzePattern(Parent, Callback);
5405+
analyzePattern(Parent);
54115406
}
54125407
if (!PossibleTypes.empty() || !PossibleNames.empty())
54135408
return true;
54145409
}
54155410
return false;
54165411
}
5412+
5413+
ArrayRef<Type> getPossibleTypes() const { return PossibleTypes; }
5414+
ArrayRef<StringRef> getPossibleNames() const { return PossibleNames; }
54175415
};
54185416

54195417
} // end anonymous namespace
@@ -5510,9 +5508,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
55105508

55115509
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
55125510
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());
55165513
}
55175514
Lookup.getValueExprCompletions(*ExprType, ReferencedDecl.getDecl());
55185515
break;
@@ -5555,9 +5552,8 @@ void CodeCompletionCallbacksImpl::doneParsing() {
55555552
case CompletionKind::PostfixExprBeginning: {
55565553
::CodeCompletionTypeContextAnalyzer Analyzer(CurDeclContext,
55575554
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());
55615557
}
55625558
DoPostfixExprBeginning();
55635559
break;
@@ -5583,18 +5579,16 @@ void CodeCompletionCallbacksImpl::doneParsing() {
55835579

55845580
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
55855581
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());
55905584
}
55915585

55925586
if (ExprType) {
55935587
if (ShouldCompleteCallPatternAfterParen) {
55945588
Lookup.getValueExprCompletions(*ExprType, ReferencedDecl.getDecl());
55955589
} else {
55965590
// Add argument labels, then fallthrough to get values.
5597-
Lookup.addArgNameCompletionResults(PossibleNames);
5591+
Lookup.addArgNameCompletionResults(TypeAnalyzer.getPossibleNames());
55985592
}
55995593
}
56005594

@@ -5705,12 +5699,11 @@ void CodeCompletionCallbacksImpl::doneParsing() {
57055699
}
57065700
case CompletionKind::UnresolvedMember: {
57075701
Lookup.setHaveDot(DotLoc);
5708-
SmallVector<Type, 2> PossibleTypes;
57095702
::CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext,
57105703
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());
57145707
break;
57155708
}
57165709
case CompletionKind::AssignmentRHS : {
@@ -5723,14 +5716,12 @@ void CodeCompletionCallbacksImpl::doneParsing() {
57235716
case CompletionKind::CallArg : {
57245717
::CodeCompletionTypeContextAnalyzer Analyzer(CurDeclContext,
57255718
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());
57315722
break;
57325723
}
5733-
Lookup.setExpectedTypes(PossibleTypes);
5724+
Lookup.setExpectedTypes(Analyzer.getPossibleTypes());
57345725
DoPostfixExprBeginning();
57355726
break;
57365727
}

0 commit comments

Comments
 (0)