-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathTypeCheckCompletionCallback.cpp
202 lines (174 loc) · 6.8 KB
/
TypeCheckCompletionCallback.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//===--- TypeCheckCompletionCallback.cpp ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 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/Basic/Assertions.h"
#include "swift/IDE/TypeCheckCompletionCallback.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::ide;
using namespace swift::constraints;
void TypeCheckCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
assert(!GotCallback);
auto finder = CompletionContextFinder::forFallback(DC);
if (!finder.hasCompletionExpr())
return;
auto fallback = finder.getFallbackCompletionExpr();
if (!fallback || isa<AbstractClosureExpr>(fallback->DC)) {
// If the expression is embedded in a closure, the constraint system tries
// to retrieve that closure's type, which will fail since we won't have
// generated any type variables for it. Thus, fallback type checking isn't
// available in this case.
return;
}
SyntacticElementTarget completionTarget(fallback->E, fallback->DC, CTP_Unused,
Type(),
/*isDiscared=*/true);
typeCheckForCodeCompletion(completionTarget, /*needsPrecheck=*/true,
[&](const Solution &S) { sawSolution(S); });
}
// MARK: - Utility functions for subclasses of TypeCheckCompletionCallback
Type swift::ide::getTypeForCompletion(const constraints::Solution &S,
ASTNode Node) {
// Use the contextual type, unless it is still unresolved, in which case fall
// back to getting the type from the expression.
if (auto ContextualType = S.getContextualType(Node)) {
if (!ContextualType->hasUnresolvedType() &&
!ContextualType->hasUnboundGenericType()) {
return ContextualType;
}
}
if (!S.hasType(Node)) {
assert(false && "Expression wasn't type checked?");
return nullptr;
}
Type Result;
if (isExpr<CodeCompletionExpr>(Node)) {
Result = S.simplifyTypeForCodeCompletion(S.getType(Node));
} else {
Result = S.getResolvedType(Node);
}
if (Result && Result->is<UnresolvedType>()) {
Result = Type();
}
return Result;
}
/// If the code completion expression \p E occurs in a pattern matching
/// position, we have an AST that looks like this.
/// \code
/// (binary_expr implicit type='$T3'
/// (overloaded_decl_ref_expr function_ref=compound decls=[
/// Swift.(file).~=,
/// Swift.(file).Optional extension.~=])
/// (argument_list implicit
/// (argument
/// (code_completion_expr implicit type='$T1'))
/// (argument
/// (declref_expr implicit decl=swift_ide_test.(file).foo(x:).$match))))
/// \endcode
/// If the code completion expression occurs in such an AST, return the
/// declaration of the \c $match variable, otherwise return \c nullptr.
static VarDecl *getMatchVarIfInPatternMatch(Expr *E, const Solution &S) {
if (auto EP = S.getExprPatternFor(E))
return EP.get()->getMatchVar();
// TODO: Once ExprPattern type-checking is fully moved into the solver,
// the below can be deleted.
auto &CS = S.getConstraintSystem();
auto &Context = CS.getASTContext();
auto *Binary = dyn_cast_or_null<BinaryExpr>(CS.getParentExpr(E));
if (!Binary || !Binary->isImplicit() || Binary->getLHS() != E) {
return nullptr;
}
auto CalledOperator = Binary->getFn();
if (!isPatternMatchingOperator(CalledOperator)) {
return nullptr;
}
auto MatchArg = dyn_cast_or_null<DeclRefExpr>(Binary->getRHS());
if (!MatchArg || !MatchArg->isImplicit()) {
return nullptr;
}
auto MatchVar = MatchArg->getDecl();
if (MatchVar && MatchVar->isImplicit() &&
MatchVar->getBaseName() == Context.Id_PatternMatchVar) {
return dyn_cast<VarDecl>(MatchVar);
} else {
return nullptr;
}
}
Type swift::ide::getPatternMatchType(const constraints::Solution &S, Expr *E) {
auto MatchVar = getMatchVarIfInPatternMatch(E, S);
if (!MatchVar)
return nullptr;
if (S.hasType(MatchVar))
return S.getResolvedType(MatchVar);
// If the ExprPattern wasn't solved as part of the constraint system, it's
// not part of the solution.
// TODO: This can be removed once ExprPattern type-checking is fully part
// of the constraint system.
auto Ty = MatchVar->getTypeInContext();
if (Ty->hasError())
return Type();
return Ty;
}
void swift::ide::getSolutionSpecificVarTypes(
const constraints::Solution &S,
llvm::SmallDenseMap<const VarDecl *, Type> &Result) {
assert(Result.empty());
for (auto NT : S.nodeTypes) {
if (auto VD = dyn_cast_or_null<VarDecl>(NT.first.dyn_cast<Decl *>())) {
Result[VD] = S.simplifyType(NT.second);
}
}
}
void WithSolutionSpecificVarTypesRAII::setInterfaceType(VarDecl *VD, Type Ty) {
VD->getASTContext().evaluator.cacheOutput(InterfaceTypeRequest{VD},
std::move(Ty));
}
bool swift::ide::isImpliedResult(const Solution &S, Expr *CompletionExpr) {
return S.isImpliedResult(CompletionExpr).has_value();
}
bool swift::ide::isContextAsync(const constraints::Solution &S,
DeclContext *DC) {
// We are in an async context if
// - the decl context is async
if (S.getConstraintSystem().isAsynchronousContext(DC)) {
return true;
}
// - the decl context is sync but it's used in a context that expectes an
// async function. This happens if the code completion token is in a
// closure that doesn't contain any async calles. Thus the closure is
// type-checked as non-async, but it might get converted to an async
// closure based on its contextual type
if (auto target = S.getTargetFor(dyn_cast<ClosureExpr>(DC))) {
if (auto ContextTy = target->getClosureContextualType()) {
if (auto ContextFuncTy =
S.simplifyType(ContextTy)->getAs<AnyFunctionType>()) {
return ContextFuncTy->isAsync();
}
}
}
// - we did not record any information about async-ness of the context in the
// solution, but the type information recorded AST declares the context as
// async.
return canDeclContextHandleAsync(DC);
}
bool swift::ide::nullableTypesEqual(Type LHS, Type RHS) {
if (LHS.isNull() && RHS.isNull()) {
return true;
} else if (LHS.isNull() || RHS.isNull()) {
// One type is null but the other is not.
return false;
} else {
return LHS->isEqual(RHS);
}
}