-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathPostfixCompletion.cpp
464 lines (398 loc) · 16 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//===--- 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/Basic/Assertions.h"
#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;
bool PostfixCompletionCallback::Result::tryMerge(const Result &Other,
DeclContext *DC) {
if (BaseDecl != Other.BaseDecl)
return false;
// These properties should match if we are talking about the same BaseDecl.
assert(IsBaseDeclUnapplied == Other.IsBaseDeclUnapplied);
assert(BaseIsStaticMetaType == Other.BaseIsStaticMetaType);
auto baseTy = tryMergeBaseTypeForCompletionLookup(BaseTy, Other.BaseTy, DC);
if (!baseTy)
return false;
BaseTy = baseTy;
// There could be multiple results that have different actor isolations if the
// closure is an argument to a function that has multiple overloads with
// different isolations for the closure. Producing multiple results for these
// is usually not very enlightning. For now, we just pick the first actor
// isolation that we find. This is good enough in practice.
// What we should really do is probably merge these two actor isolations and
// pick the weakest isolation for each closure.
for (auto &OtherExpectedTy : Other.ExpectedTypes) {
auto IsEqual = [&](Type Ty) { return Ty->isEqual(OtherExpectedTy); };
if (llvm::any_of(ExpectedTypes, IsEqual)) {
// We already know if this expected type
continue;
}
ExpectedTypes.push_back(OtherExpectedTy);
}
ExpectsNonVoid &= Other.ExpectsNonVoid;
IsImpliedResult |= Other.IsImpliedResult;
IsInAsyncContext |= Other.IsInAsyncContext;
return true;
}
void PostfixCompletionCallback::addResult(const Result &Res) {
for (auto idx : indices(Results)) {
if (Results[idx].tryMerge(Res, DC))
return;
}
Results.push_back(Res);
}
void PostfixCompletionCallback::fallbackTypeCheck(DeclContext *DC) {
assert(!gotCallback());
// Default to checking the completion expression in isolation.
Expr *fallbackExpr = CompletionExpr;
DeclContext *fallbackDC = DC;
auto finder = CompletionContextFinder::forFallback(DC);
if (finder.hasCompletionExpr()) {
if (auto fallback = finder.getFallbackCompletionExpr()) {
fallbackExpr = fallback->E;
fallbackDC = fallback->DC;
}
}
if (isa<AbstractClosureExpr>(fallbackDC)) {
// 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(fallbackExpr, fallbackDC, CTP_Unused,
Type(),
/*isDiscared=*/true);
typeCheckForCodeCompletion(completionTarget, /*needsPrecheck*/ true,
[&](const Solution &S) { sawSolution(S); });
}
static ActorIsolation
getClosureActorIsolation(const Solution &S, AbstractClosureExpr *ACE) {
auto getType = [&S](Expr *E) -> Type {
// Prefer the contextual type of the closure because it might be 'weaker'
// than the type determined for the closure by the constraints system. E.g.
// the contextual type might have a global actor attribute but because no
// methods from that global actor are called in the closure, the closure has
// a non-actor type.
if (auto target = S.getTargetFor(dyn_cast<ClosureExpr>(E))) {
if (auto Ty = target->getClosureContextualType())
return Ty;
}
if (!S.hasType(E)) {
return Type();
}
return getTypeForCompletion(S, E);
};
auto getClosureActorIsolationThunk = [&S](AbstractClosureExpr *ACE) {
return getClosureActorIsolation(S, ACE);
};
return determineClosureActorIsolation(ACE, getType,
getClosureActorIsolationThunk);
}
/// Returns \c true if \p Choice refers to a function that hasn't been called
/// yet.
static bool isUnappliedFunctionRef(const OverloadChoice &Choice) {
if (!Choice.isDecl()) {
return false;
}
auto fnRefKind = Choice.getFunctionRefInfo();
if (fnRefKind.isUnapplied())
return true;
// We consider curried member calls as unapplied. E.g.
// MyStruct.someInstanceFunc(theInstance)#^COMPLETE^#
// is unapplied.
if (fnRefKind.isSingleApply()) {
if (auto BaseTy = Choice.getBaseType())
return BaseTy->is<MetatypeType>() && !Choice.getDeclOrNull()->isStatic();
}
return false;
}
void PostfixCompletionCallback::sawSolutionImpl(
const constraints::Solution &S) {
auto &CS = S.getConstraintSystem();
auto *ParsedExpr = CompletionExpr->getBase();
auto *SemanticExpr = ParsedExpr->getSemanticsProvidingExpr();
if (!S.hasType(ParsedExpr)) {
return;
}
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);
auto *CalleeLocator = S.getCalleeLocator(Locator);
ValueDecl *ReferencedDecl = nullptr;
bool IsBaseDeclUnapplied = false;
if (auto SelectedOverload = S.getOverloadChoiceIfAvailable(CalleeLocator)) {
ReferencedDecl = SelectedOverload->choice.getDeclOrNull();
IsBaseDeclUnapplied = isUnappliedFunctionRef(SelectedOverload->choice);
}
bool BaseIsStaticMetaType = S.isStaticallyDerivedMetatype(ParsedExpr);
bool ExpectsNonVoid = false;
SmallVector<Type, 4> ExpectedTypes;
if (ExpectedTy) {
ExpectedTypes.push_back(ExpectedTy);
ExpectsNonVoid = !ExpectedTy->isVoid();
} else {
// If we don't know what the expected type is, assume it must be non-Void
// if we have a contextual type that is not unused. This prevents us from
// suggesting Void values for e.g bindings without explicit types.
ExpectsNonVoid |= !ParentExpr &&
CS.getContextualTypePurpose(CompletionExpr) != CTP_Unused;
for (auto SAT : S.targets) {
if (ExpectsNonVoid) {
// ExpectsNonVoid is already set. No need to iterate further.
break;
}
if (SAT.second.getAsExpr() == CompletionExpr) {
ExpectsNonVoid |=
SAT.second.getExprContextualTypePurpose() != CTP_Unused;
}
}
}
bool IsImpliedResult = isImpliedResult(S, CompletionExpr);
bool IsInAsyncContext = isContextAsync(S, DC);
llvm::DenseMap<AbstractClosureExpr *, ActorIsolation>
ClosureActorIsolations;
for (auto SAT : S.targets) {
if (auto ACE = getAsExpr<AbstractClosureExpr>(SAT.second.getAsASTNode())) {
ClosureActorIsolations[ACE] = getClosureActorIsolation(S, ACE);
}
}
Result Res = {
BaseTy,
ReferencedDecl,
IsBaseDeclUnapplied,
BaseIsStaticMetaType,
ExpectedTypes,
ExpectsNonVoid,
IsImpliedResult,
IsInAsyncContext,
ClosureActorIsolations
};
addResult(Res);
}
/// Returns \c true if \p T is '_OptionalNilComparisonType'.
static bool isOptionalNilComparisonType(Type T) {
if (!T) {
return false;
}
auto *nominal = T->getAnyNominal();
if (!nominal) {
return false;
}
return (nominal->isStdlibDecl() &&
nominal->getName() ==
nominal->getASTContext().Id_OptionalNilComparisonType);
}
static DeclRefKind getDeclRefKindOfOperator(OperatorDecl *op) {
switch (op->getKind()) {
case DeclKind::PrefixOperator:
return DeclRefKind::PrefixOperator;
case DeclKind::PostfixOperator:
return DeclRefKind::PostfixOperator;
case DeclKind::InfixOperator:
return DeclRefKind::BinaryOperator;
default:
llvm_unreachable("unexpected operator kind");
}
}
/// Return type of \c getOperatorCompletionTypes.
struct OperatorResultTypes {
/// If we are trying to complete a binary operator, the type the operator
/// expects for the RHS. Null for postfix operators.
Type RHSType;
/// The type the operator returns when called.
Type ResultType;
bool operator==(const OperatorResultTypes &Other) const {
return nullableTypesEqual(RHSType, Other.RHSType) &&
nullableTypesEqual(ResultType, Other.ResultType);
}
};
/// Builds a constriant system that tries applying the operator \p op on a LHS
/// of type \p LHSType. If that succeeds, returns the result type of the
/// operator call and (in case of binary operators) the expected type for the
/// RHS.
static SmallVector<OperatorResultTypes>
getOperatorCompletionTypes(DeclContext *DC, Type LHSType, OperatorDecl *Op) {
ConstraintSystemOptions options;
options |= ConstraintSystemFlags::SuppressDiagnostics;
ConstraintSystem CS(DC, options);
// The source loc of the generated expression doesn't matter.
SourceLoc Loc;
// We represent the LHS and RHS by CodeCompletionExprs because there's no
// other better choice. rhs will have its type set in the constraint system
// below and, in case of binary operators, rhs will be inspected for its type
// when the constraint system has been solved.
CodeCompletionExpr LHS(Loc);
CodeCompletionExpr RHS(Loc);
UnresolvedDeclRefExpr UDRE(DeclNameRef(Op->getName()),
getDeclRefKindOfOperator(Op), DeclNameLoc(Loc));
DiagnosticTransaction IgnoreDiags(DC->getASTContext().Diags);
Expr *OpExpr = resolveDeclRefExpr(&UDRE, DC);
IgnoreDiags.abort();
if (isa<ErrorExpr>(OpExpr)) {
// If we couldn't resolve the operator (e.g. because there is only an
// operator definition but no decls that implement it), we can't call the
// operator.
return {};
}
Expr *OpCallExpr;
switch (Op->getKind()) {
case DeclKind::PrefixOperator:
// Don't insert prefix operators in postfix position.
return {};
case DeclKind::PostfixOperator:
OpCallExpr = PostfixUnaryExpr::create(DC->getASTContext(), OpExpr, &LHS);
break;
case DeclKind::InfixOperator:
OpCallExpr = BinaryExpr::create(DC->getASTContext(), &LHS, OpExpr, &RHS,
/*implicit*/ true);
break;
default:
llvm_unreachable("unexpected operator kind");
}
auto target = SyntacticElementTarget(OpCallExpr, DC, CTP_Unused, Type(),
/*isDiscarded*/ true);
if (CS.preCheckTarget(target))
return {};
if (CS.generateConstraints(target))
return {};
OpCallExpr = target.getAsExpr();
CS.assignFixedType(CS.getType(&LHS)->getAs<TypeVariableType>(), LHSType);
SmallVector<Solution, 1> Solutions;
CS.solve(Solutions);
SmallVector<OperatorResultTypes> Results;
for (auto &S : Solutions) {
Type RHSType;
if (Op->getKind() == DeclKind::InfixOperator) {
RHSType = getTypeForCompletion(S, &RHS);
}
Type ResultType = getTypeForCompletion(S, OpCallExpr);
OperatorResultTypes ResultTypes = {RHSType, ResultType};
if (llvm::is_contained(Results, ResultTypes)) {
continue;
}
if (S.getFixedScore().Data[SK_ValueToOptional] > 0) {
if (Op->getName().str() == "??" || isOptionalNilComparisonType(RHSType)) {
// Don't suggest optional operators that need to demote the LHS to an
// Optional to become applicable.
continue;
}
}
Results.push_back(ResultTypes);
}
return Results;
}
/// Adds applicable operator suggestions to \p Lookup.
static void addOperatorResults(Type LHSType, ArrayRef<OperatorDecl *> Operators,
DeclContext *DC, CompletionLookup &Lookup) {
for (auto Op : Operators) {
switch (Op->getKind()) {
case DeclKind::PrefixOperator:
break;
case DeclKind::PostfixOperator:
for (auto operatorType : getOperatorCompletionTypes(DC, LHSType, Op)) {
Lookup.addPostfixOperatorCompletion(Op, operatorType.ResultType);
}
break;
case DeclKind::InfixOperator:
for (auto operatorType : getOperatorCompletionTypes(DC, LHSType, Op)) {
Lookup.addInfixOperatorCompletion(Op, operatorType.ResultType,
operatorType.RHSType);
}
break;
default:
llvm_unreachable("unexpected operator kind");
}
}
if (LHSType->hasLValueType()) {
Lookup.addAssignmentOperator(LHSType->getRValueType());
}
if (auto ValueT = LHSType->getRValueType()->getOptionalObjectType()) {
Lookup.addPostfixBang(ValueT);
}
}
void PostfixCompletionCallback::collectResults(
SourceLoc DotLoc, bool IsInSelector, bool IncludeOperators,
bool HasLeadingSpace, CodeCompletionContext &CompletionCtx) {
ASTContext &Ctx = DC->getASTContext();
CompletionLookup Lookup(CompletionCtx.getResultSink(), Ctx, DC,
&CompletionCtx);
if (DotLoc.isValid()) {
assert(!IncludeOperators && "We shouldn't be suggesting operators if we "
"are completing after a dot");
Lookup.setHaveDot(DotLoc);
}
Lookup.setHaveLeadingSpace(HasLeadingSpace);
Expr *BaseExpr = CompletionExpr->getBase();
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();
}
SmallVector<OperatorDecl *> Operators;
if (IncludeOperators) {
Lookup.collectOperators(Operators);
}
// The type context that is being used for global results.
ExpectedTypeContext UnifiedTypeContext;
UnifiedTypeContext.setPreferNonVoid(true);
bool UnifiedCanHandleAsync = false;
// The base types of the result for which we already returned results.
// Used so we only return keyword and operator completions once for each base
// type.
llvm::SmallPtrSet<Type, 2> ProcessedBaseTypes;
Lookup.shouldCheckForDuplicates(Results.size() > 1);
for (auto &Result : Results) {
Lookup.setCanCurrDeclContextHandleAsync(Result.IsInAsyncContext);
Lookup.setClosureActorIsolations(Result.ClosureActorIsolations);
Lookup.setIsStaticMetatype(Result.BaseIsStaticMetaType);
if (!ProcessedBaseTypes.contains(Result.BaseTy)) {
Lookup.getPostfixKeywordCompletions(Result.BaseTy, BaseExpr);
}
Lookup.setExpectedTypes(Result.ExpectedTypes, Result.IsImpliedResult,
Result.ExpectsNonVoid);
if (isDynamicLookup(Result.BaseTy))
Lookup.setIsDynamicLookup();
Lookup.getValueExprCompletions(Result.BaseTy, Result.BaseDecl,
Result.IsBaseDeclUnapplied);
// `==`, `<=` etc can be used on `Void` because `Void` is just an empty
// tuple. But that doesn’t really make sense so we shouldn't be suggesting
// any operators based on `Void`.
if (IncludeOperators && !Result.BaseIsStaticMetaType &&
!Result.BaseTy->isVoid() &&
!ProcessedBaseTypes.contains(Result.BaseTy)) {
addOperatorResults(Result.BaseTy, Operators, DC, Lookup);
}
UnifiedTypeContext.merge(*Lookup.getExpectedTypeContext());
UnifiedCanHandleAsync |= Result.IsInAsyncContext;
ProcessedBaseTypes.insert(Result.BaseTy);
}
collectCompletionResults(CompletionCtx, Lookup, DC, UnifiedTypeContext,
UnifiedCanHandleAsync);
}