-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathPostfixCompletion.cpp
136 lines (114 loc) · 5.18 KB
/
PostfixCompletion.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//===--- DotExprCodeCompletion.cpp ----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/IDE/PostfixCompletion.h"
#include "swift/IDE/CodeCompletion.h"
#include "swift/IDE/CompletionLookup.h"
#include "swift/Sema/CompletionContextFinder.h"
#include "swift/Sema/ConstraintSystem.h"
#include "swift/Sema/IDETypeChecking.h"
using namespace swift;
using namespace swift::constraints;
using namespace swift::ide;
void PostfixCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
assert(!gotCallback());
// Default to checking the completion expression in isolation.
Expr *fallbackExpr = CompletionExpr;
DeclContext *fallbackDC = DC;
CompletionContextFinder finder(DC);
if (finder.hasCompletionExpr()) {
if (auto fallback = finder.getFallbackCompletionExpr()) {
fallbackExpr = fallback->E;
fallbackDC = fallback->DC;
}
}
SolutionApplicationTarget completionTarget(fallbackExpr, fallbackDC,
CTP_Unused, Type(),
/*isDiscared=*/true);
typeCheckForCodeCompletion(completionTarget, /*needsPrecheck*/ true,
[&](const Solution &S) { sawSolution(S); });
}
void PostfixCompletionCallback::sawSolutionImpl(
const constraints::Solution &S) {
auto &CS = S.getConstraintSystem();
auto *ParsedExpr = CompletionExpr->getBase();
auto *SemanticExpr = ParsedExpr->getSemanticsProvidingExpr();
auto BaseTy = getTypeForCompletion(S, ParsedExpr);
// If base type couldn't be determined (e.g. because base expression
// is an invalid reference), let's not attempt to do a lookup since
// it wouldn't produce any useful results anyway.
if (!BaseTy)
return;
auto *Locator = CS.getConstraintLocator(SemanticExpr);
Type ExpectedTy = getTypeForCompletion(S, CompletionExpr);
Expr *ParentExpr = CS.getParentExpr(CompletionExpr);
if (!ParentExpr)
ExpectedTy = CS.getContextualType(CompletionExpr, /*forConstraint=*/false);
auto *CalleeLocator = S.getCalleeLocator(Locator);
ValueDecl *ReferencedDecl = nullptr;
if (auto SelectedOverload = S.getOverloadChoiceIfAvailable(CalleeLocator))
ReferencedDecl = SelectedOverload->choice.getDeclOrNull();
bool IsAsync = isContextAsync(S, DC);
auto Key = std::make_pair(BaseTy, ReferencedDecl);
auto Ret = BaseToSolutionIdx.insert({Key, Results.size()});
if (Ret.second) {
bool ISDMT = S.isStaticallyDerivedMetatype(ParsedExpr);
bool ImplicitReturn = isImplicitSingleExpressionReturn(CS, CompletionExpr);
bool DisallowVoid = ExpectedTy
? !ExpectedTy->isVoid()
: !ParentExpr && CS.getContextualTypePurpose(
CompletionExpr) != CTP_Unused;
Results.push_back({BaseTy, ReferencedDecl,
/*ExpectedTypes=*/{}, DisallowVoid, ISDMT,
ImplicitReturn, IsAsync});
if (ExpectedTy) {
Results.back().ExpectedTypes.push_back(ExpectedTy);
}
} else if (ExpectedTy) {
auto &ExistingResult = Results[Ret.first->getSecond()];
ExistingResult.IsInAsyncContext |= IsAsync;
auto IsEqual = [&](Type Ty) { return ExpectedTy->isEqual(Ty); };
if (!llvm::any_of(ExistingResult.ExpectedTypes, IsEqual)) {
ExistingResult.ExpectedTypes.push_back(ExpectedTy);
}
}
}
void PostfixCompletionCallback::deliverResults(
Expr *BaseExpr, DeclContext *DC, SourceLoc DotLoc, bool IsInSelector,
CodeCompletionContext &CompletionCtx, CodeCompletionConsumer &Consumer) {
ASTContext &Ctx = DC->getASTContext();
CompletionLookup Lookup(CompletionCtx.getResultSink(), Ctx, DC,
&CompletionCtx);
if (DotLoc.isValid())
Lookup.setHaveDot(DotLoc);
Lookup.setIsSuperRefExpr(isa<SuperRefExpr>(BaseExpr));
if (auto *DRE = dyn_cast<DeclRefExpr>(BaseExpr))
Lookup.setIsSelfRefExpr(DRE->getDecl()->getName() == Ctx.Id_self);
if (isa<BindOptionalExpr>(BaseExpr) || isa<ForceValueExpr>(BaseExpr))
Lookup.setIsUnwrappedOptional(true);
if (IsInSelector) {
Lookup.includeInstanceMembers();
Lookup.setPreferFunctionReferencesToCalls();
}
Lookup.shouldCheckForDuplicates(Results.size() > 1);
for (auto &Result : Results) {
Lookup.setCanCurrDeclContextHandleAsync(Result.IsInAsyncContext);
Lookup.setIsStaticMetatype(Result.BaseIsStaticMetaType);
Lookup.getPostfixKeywordCompletions(Result.BaseTy, BaseExpr);
Lookup.setExpectedTypes(Result.ExpectedTypes,
Result.IsImplicitSingleExpressionReturn,
Result.ExpectsNonVoid);
if (isDynamicLookup(Result.BaseTy))
Lookup.setIsDynamicLookup();
Lookup.getValueExprCompletions(Result.BaseTy, Result.BaseDecl);
}
deliverCompletionResults(CompletionCtx, Lookup, DC, Consumer);
}