Skip to content

Commit a02c7e7

Browse files
authored
Merge pull request #41601 from ahoppen/pr/prepare-for-solver-based-arg-completion
[CodeCompletion] Some more code cleanup
2 parents f3119b8 + d9f3324 commit a02c7e7

8 files changed

+74
-75
lines changed

include/swift/IDE/DotExprCompletion.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,17 @@ class DotExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
3333
bool IsImplicitSingleExpressionReturn;
3434
};
3535

36-
DeclContext *DC;
3736
CodeCompletionExpr *CompletionExpr;
3837
SmallVector<Result, 4> Results;
3938
llvm::DenseMap<std::pair<Type, Decl *>, size_t> BaseToSolutionIdx;
40-
bool GotCallback = false;
4139

4240
public:
43-
DotExprTypeCheckCompletionCallback(DeclContext *DC,
44-
CodeCompletionExpr *CompletionExpr)
45-
: DC(DC), CompletionExpr(CompletionExpr) {}
46-
47-
/// True if at least one solution was passed via the \c sawSolution
48-
/// callback.
49-
bool gotCallback() const { return GotCallback; }
41+
DotExprTypeCheckCompletionCallback(CodeCompletionExpr *CompletionExpr)
42+
: CompletionExpr(CompletionExpr) {}
5043

5144
/// Typecheck the code completion expression in isolation, calling
5245
/// \c sawSolution for each solution formed.
53-
void fallbackTypeCheck();
46+
void fallbackTypeCheck(DeclContext *DC) override;
5447

5548
void sawSolution(const constraints::Solution &solution) override;
5649

include/swift/IDE/UnresolvedMemberCompletion.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,12 @@ class UnresolvedMemberTypeCheckCompletionCallback
3333
CodeCompletionExpr *CompletionExpr;
3434
SmallVector<ExprResult, 4> ExprResults;
3535
SmallVector<Type, 1> EnumPatternTypes;
36-
bool GotCallback = false;
3736

3837
public:
3938
UnresolvedMemberTypeCheckCompletionCallback(
4039
CodeCompletionExpr *CompletionExpr)
4140
: CompletionExpr(CompletionExpr) {}
4241

43-
/// True if at least one solution was passed via the \c sawSolution
44-
/// callback.
45-
bool gotCallback() const { return GotCallback; }
46-
47-
/// Typecheck the code completion expression in its outermost expression
48-
/// context, calling \c sawSolution for each solution formed.
49-
void fallbackTypeCheck(DeclContext *DC);
50-
5142
void sawSolution(const constraints::Solution &solution) override;
5243

5344
void deliverResults(DeclContext *DC, SourceLoc DotLoc,

include/swift/Sema/CodeCompletionTypeChecking.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,24 @@ namespace swift {
3636
}
3737

3838
class TypeCheckCompletionCallback {
39+
bool GotCallback = false;
40+
3941
public:
42+
virtual ~TypeCheckCompletionCallback() {}
43+
4044
/// Called for each solution produced while type-checking an expression
4145
/// that the code completion expression participates in.
42-
virtual void sawSolution(const constraints::Solution &solution) = 0;
43-
virtual ~TypeCheckCompletionCallback() {}
46+
virtual void sawSolution(const constraints::Solution &solution) {
47+
GotCallback = true;
48+
};
49+
50+
/// True if at least one solution was passed via the \c sawSolution
51+
/// callback.
52+
bool gotCallback() const { return GotCallback; }
53+
54+
/// Typecheck the code completion expression in its outermost expression
55+
/// context, calling \c sawSolution for each solution formed.
56+
virtual void fallbackTypeCheck(DeclContext *DC);
4457
};
4558
}
4659

lib/IDE/CodeCompletion.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -132,31 +132,6 @@ class CodeCompletionCallbacksImpl : public CodeCompletionCallbacks {
132132

133133
std::vector<std::pair<std::string, bool>> SubModuleNameVisibilityPairs;
134134

135-
void addSuperKeyword(CodeCompletionResultSink &Sink) {
136-
auto *DC = CurDeclContext->getInnermostTypeContext();
137-
if (!DC)
138-
return;
139-
auto *CD = DC->getSelfClassDecl();
140-
if (!CD)
141-
return;
142-
Type ST = CD->getSuperclass();
143-
if (ST.isNull() || ST->is<ErrorType>())
144-
return;
145-
146-
CodeCompletionResultBuilder Builder(Sink, CodeCompletionResultKind::Keyword,
147-
SemanticContextKind::CurrentNominal,
148-
{});
149-
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(CurDeclContext)) {
150-
if (AFD->getOverriddenDecl() != nullptr) {
151-
Builder.addFlair(CodeCompletionFlairBit::CommonKeywordAtCurrentPosition);
152-
}
153-
}
154-
155-
Builder.setKeywordKind(CodeCompletionKeywordKind::kw_super);
156-
Builder.addKeyword("super");
157-
Builder.addTypeAnnotation(ST, PrintOptions());
158-
}
159-
160135
Optional<std::pair<Type, ConcreteDeclRef>> typeCheckParsedExpr() {
161136
assert(ParsedExpr && "should have an expression");
162137

@@ -879,6 +854,32 @@ static void addExprKeywords(CodeCompletionResultSink &Sink, DeclContext *DC) {
879854
addKeyword(Sink, "await", CodeCompletionKeywordKind::None, "", flair);
880855
}
881856

857+
static void addSuperKeyword(CodeCompletionResultSink &Sink, DeclContext *DC) {
858+
if (!DC)
859+
return;
860+
auto *TC = DC->getInnermostTypeContext();
861+
if (!TC)
862+
return;
863+
auto *CD = TC->getSelfClassDecl();
864+
if (!CD)
865+
return;
866+
Type ST = CD->getSuperclass();
867+
if (ST.isNull() || ST->is<ErrorType>())
868+
return;
869+
870+
CodeCompletionResultBuilder Builder(Sink, CodeCompletionResultKind::Keyword,
871+
SemanticContextKind::CurrentNominal, {});
872+
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(DC)) {
873+
if (AFD->getOverriddenDecl() != nullptr) {
874+
Builder.addFlair(CodeCompletionFlairBit::CommonKeywordAtCurrentPosition);
875+
}
876+
}
877+
878+
Builder.setKeywordKind(CodeCompletionKeywordKind::kw_super);
879+
Builder.addKeyword("super");
880+
Builder.addTypeAnnotation(ST, PrintOptions());
881+
}
882+
882883
static void addOpaqueTypeKeyword(CodeCompletionResultSink &Sink) {
883884
addKeyword(Sink, "some", CodeCompletionKeywordKind::None, "some");
884885
}
@@ -950,7 +951,7 @@ void CodeCompletionCallbacksImpl::addKeywords(CodeCompletionResultSink &Sink,
950951
case CompletionKind::YieldStmtExpr:
951952
case CompletionKind::PostfixExprBeginning:
952953
case CompletionKind::ForEachSequence:
953-
addSuperKeyword(Sink);
954+
addSuperKeyword(Sink, CurDeclContext);
954955
addLetVarKeywords(Sink);
955956
addExprKeywords(Sink, CurDeclContext);
956957
addAnyTypeKeyword(Sink, CurDeclContext->getASTContext().TheAnyType);
@@ -1334,8 +1335,7 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
13341335
assert(CodeCompleteTokenExpr);
13351336
assert(CurDeclContext);
13361337

1337-
DotExprTypeCheckCompletionCallback Lookup(CurDeclContext,
1338-
CodeCompleteTokenExpr);
1338+
DotExprTypeCheckCompletionCallback Lookup(CodeCompleteTokenExpr);
13391339
llvm::SaveAndRestore<TypeCheckCompletionCallback*>
13401340
CompletionCollector(Context.CompletionCallback, &Lookup);
13411341
typeCheckContextAt(CurDeclContext, CompletionLoc);
@@ -1346,7 +1346,7 @@ bool CodeCompletionCallbacksImpl::trySolverCompletion(bool MaybeFuncBody) {
13461346
// typechecking still resolve even these cases would be beneficial for
13471347
// tooling in general though.
13481348
if (!Lookup.gotCallback())
1349-
Lookup.fallbackTypeCheck();
1349+
Lookup.fallbackTypeCheck(CurDeclContext);
13501350

13511351
addKeywords(CompletionContext.getResultSink(), MaybeFuncBody);
13521352

@@ -1567,7 +1567,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
15671567
Lookup.setHaveLParen(false);
15681568

15691569
// Add any keywords that can be used in an argument expr position.
1570-
addSuperKeyword(CompletionContext.getResultSink());
1570+
addSuperKeyword(CompletionContext.getResultSink(), CurDeclContext);
15711571
addExprKeywords(CompletionContext.getResultSink(), CurDeclContext);
15721572

15731573
DoPostfixExprBeginning();
@@ -1713,7 +1713,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
17131713
ContextInfo.isImplicitSingleExpressionReturn());
17141714

17151715
// Add any keywords that can be used in an argument expr position.
1716-
addSuperKeyword(CompletionContext.getResultSink());
1716+
addSuperKeyword(CompletionContext.getResultSink(), CurDeclContext);
17171717
addExprKeywords(CompletionContext.getResultSink(), CurDeclContext);
17181718

17191719
DoPostfixExprBeginning();
@@ -1771,7 +1771,7 @@ void CodeCompletionCallbacksImpl::doneParsing() {
17711771
Context.LangOpts.EnableExperimentalConcurrency,
17721772
Context.LangOpts.EnableExperimentalDistributed);
17731773
addStmtKeywords(Sink, CurDeclContext, MaybeFuncBody);
1774-
addSuperKeyword(Sink);
1774+
addSuperKeyword(Sink, CurDeclContext);
17751775
addLetVarKeywords(Sink);
17761776
addExprKeywords(Sink, CurDeclContext);
17771777
addAnyTypeKeyword(Sink, Context.TheAnyType);

lib/IDE/DotExprCompletion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ using namespace swift;
2121
using namespace swift::constraints;
2222
using namespace swift::ide;
2323

24-
void DotExprTypeCheckCompletionCallback::fallbackTypeCheck() {
24+
void DotExprTypeCheckCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
2525
assert(!gotCallback());
2626

2727
// Default to checking the completion expression in isolation.
@@ -46,7 +46,7 @@ void DotExprTypeCheckCompletionCallback::fallbackTypeCheck() {
4646

4747
void DotExprTypeCheckCompletionCallback::sawSolution(
4848
const constraints::Solution &S) {
49-
GotCallback = true;
49+
TypeCheckCompletionCallback::sawSolution(S);
5050
auto &CS = S.getConstraintSystem();
5151
auto *ParsedExpr = CompletionExpr->getBase();
5252
auto *SemanticExpr = ParsedExpr->getSemanticsProvidingExpr();

lib/IDE/KeyPathCompletion.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ using namespace swift::ide;
2121

2222
void KeyPathTypeCheckCompletionCallback::sawSolution(
2323
const constraints::Solution &S) {
24+
TypeCheckCompletionCallback::sawSolution(S);
25+
2426
// Determine the code completion.
2527
size_t ComponentIndex = 0;
2628
for (auto &Component : KeyPath->getComponents()) {

lib/IDE/UnresolvedMemberCompletion.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static VarDecl *getMatchVarIfInPatternMatch(CodeCompletionExpr *CompletionExpr,
8282

8383
void UnresolvedMemberTypeCheckCompletionCallback::sawSolution(
8484
const constraints::Solution &S) {
85-
GotCallback = true;
85+
TypeCheckCompletionCallback::sawSolution(S);
8686

8787
auto &CS = S.getConstraintSystem();
8888
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);
@@ -119,25 +119,6 @@ void UnresolvedMemberTypeCheckCompletionCallback::sawSolution(
119119
}
120120
}
121121

122-
void UnresolvedMemberTypeCheckCompletionCallback::fallbackTypeCheck(
123-
DeclContext *DC) {
124-
assert(!gotCallback());
125-
126-
CompletionContextFinder finder(DC);
127-
if (!finder.hasCompletionExpr())
128-
return;
129-
130-
auto fallback = finder.getFallbackCompletionExpr();
131-
if (!fallback)
132-
return;
133-
134-
SolutionApplicationTarget completionTarget(fallback->E, fallback->DC,
135-
CTP_Unused, Type(),
136-
/*isDiscared=*/true);
137-
typeCheckForCodeCompletion(completionTarget, /*needsPrecheck*/ true,
138-
[&](const Solution &S) { sawSolution(S); });
139-
}
140-
141122
void UnresolvedMemberTypeCheckCompletionCallback::deliverResults(
142123
DeclContext *DC, SourceLoc DotLoc,
143124
ide::CodeCompletionContext &CompletionCtx,

lib/Sema/TypeCheckCodeCompletion.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,3 +848,22 @@ bool swift::isImplicitSingleExpressionReturn(ConstraintSystem &CS,
848848
}
849849
return false;
850850
}
851+
852+
void TypeCheckCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
853+
assert(!GotCallback);
854+
855+
CompletionContextFinder finder(DC);
856+
if (!finder.hasCompletionExpr())
857+
return;
858+
859+
auto fallback = finder.getFallbackCompletionExpr();
860+
if (!fallback)
861+
return;
862+
863+
SolutionApplicationTarget completionTarget(fallback->E, fallback->DC,
864+
CTP_Unused, Type(),
865+
/*isDiscared=*/true);
866+
TypeChecker::typeCheckForCodeCompletion(
867+
completionTarget, /*needsPrecheck*/ true,
868+
[&](const Solution &S) { sawSolution(S); });
869+
}

0 commit comments

Comments
 (0)