-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathConformingMethodList.cpp
251 lines (207 loc) · 8.63 KB
/
ConformingMethodList.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
//===--- ConformingMethodList.cpp -----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2019 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/ConformingMethodList.h"
#include "ExprContextAnalysis.h"
#include "swift/AST/ASTDemangler.h"
#include "swift/AST/ConformanceLookup.h"
#include "swift/AST/GenericEnvironment.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/USRGeneration.h"
#include "swift/Basic/Assertions.h"
#include "swift/IDE/TypeCheckCompletionCallback.h"
#include "swift/Parse/IDEInspectionCallbacks.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Sema/ConstraintSystem.h"
#include "clang/AST/Attr.h"
#include "clang/AST/Decl.h"
using namespace swift;
using namespace ide;
namespace {
class ConformingMethodListCallbacks : public CodeCompletionCallbacks,
public DoneParsingCallback {
ArrayRef<const char *> ExpectedTypeNames;
ConformingMethodListConsumer &Consumer;
SourceLoc Loc;
CodeCompletionExpr *CCExpr = nullptr;
DeclContext *CurDeclContext = nullptr;
void getMatchingMethods(Type T,
llvm::SmallPtrSetImpl<ProtocolDecl*> &expectedTypes,
SmallVectorImpl<ValueDecl *> &result);
public:
ConformingMethodListCallbacks(Parser &P,
ArrayRef<const char *> ExpectedTypeNames,
ConformingMethodListConsumer &Consumer)
: CodeCompletionCallbacks(P), DoneParsingCallback(),
ExpectedTypeNames(ExpectedTypeNames), Consumer(Consumer) {}
// Only handle callbacks for suffix completions.
// {
void completeDotExpr(CodeCompletionExpr *E, SourceLoc DotLoc) override;
void completePostfixExpr(CodeCompletionExpr *E, bool hasSpace) override;
// }
void doneParsing(SourceFile *SrcFile) override;
};
void ConformingMethodListCallbacks::completeDotExpr(CodeCompletionExpr *E,
SourceLoc DotLoc) {
CurDeclContext = P.CurDeclContext;
CCExpr = E;
}
void ConformingMethodListCallbacks::completePostfixExpr(CodeCompletionExpr *E,
bool hasSpace) {
CurDeclContext = P.CurDeclContext;
CCExpr = E;
}
class ConformingMethodListCallback : public TypeCheckCompletionCallback {
public:
struct Result {
Type Ty;
/// Types of variables that were determined in the solution that produced
/// this result. This in particular includes parameters of closures that
/// were type-checked with the code completion expression.
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
};
private:
CodeCompletionExpr *CCExpr;
SmallVector<Result> Results;
void sawSolutionImpl(const constraints::Solution &S) override {
if (!S.hasType(CCExpr->getBase())) {
return;
}
if (Type T = getTypeForCompletion(S, CCExpr->getBase())) {
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
getSolutionSpecificVarTypes(S, SolutionSpecificVarTypes);
Results.push_back({T, SolutionSpecificVarTypes});
}
}
public:
ConformingMethodListCallback(CodeCompletionExpr *CCExpr) : CCExpr(CCExpr) {}
ArrayRef<Result> getResults() const { return Results; }
};
void ConformingMethodListCallbacks::doneParsing(SourceFile *SrcFile) {
if (!CCExpr || !CCExpr->getBase())
return;
ConformingMethodListCallback TypeCheckCallback(CCExpr);
{
llvm::SaveAndRestore<TypeCheckCompletionCallback *> CompletionCollector(
Context.CompletionCallback, &TypeCheckCallback);
typeCheckContextAt(
TypeCheckASTNodeAtLocContext::declContext(CurDeclContext),
CCExpr->getLoc());
}
if (TypeCheckCallback.getResults().size() != 1) {
// Either no results or results were ambiguous, which we cannot handle.
return;
}
auto Res = TypeCheckCallback.getResults()[0];
Type T = Res.Ty;
WithSolutionSpecificVarTypesRAII VarType(Res.SolutionSpecificVarTypes);
if (!T || T->is<ErrorType>() || T->is<UnresolvedType>())
return;
T = T->getRValueType();
// If there are no (instance) members for this type, bail.
if (!T->mayHaveMembers() || T->is<ModuleType>()) {
return;
}
auto interfaceTy = T;
if (T->hasArchetype())
interfaceTy = interfaceTy->mapTypeOutOfContext();
llvm::SmallPtrSet<ProtocolDecl*, 8> expectedProtocols;
for (auto Name: ExpectedTypeNames) {
if (auto *PD = resolveProtocolName(CurDeclContext, Name)) {
expectedProtocols.insert(PD);
}
}
// Collect the matching methods.
ConformingMethodListResult result(CurDeclContext, interfaceTy);
getMatchingMethods(T, expectedProtocols, result.Members);
Consumer.handleResult(result);
}
void ConformingMethodListCallbacks::getMatchingMethods(
Type T, llvm::SmallPtrSetImpl<ProtocolDecl*> &expectedTypes,
SmallVectorImpl<ValueDecl *> &result) {
assert(T->mayHaveMembers() && !T->is<ModuleType>());
class LocalConsumer : public VisibleDeclConsumer {
/// The type of the parsed expression.
Type T;
/// The list of expected types.
llvm::SmallPtrSetImpl<ProtocolDecl*> &ExpectedTypes;
/// Result sink to populate.
SmallVectorImpl<ValueDecl *> &Result;
/// Returns true if \p VD is a instance method whose return type conforms
/// to the requested protocols.
bool isMatchingMethod(ValueDecl *VD) {
auto *FD = dyn_cast<FuncDecl>(VD);
if (!FD)
return false;
if (FD->isStatic() || FD->isOperator())
return false;
assert(!T->hasTypeParameter());
// T may contain primary archetypes from some fixed generic signature G.
// This might be unrelated to the generic signature of FD. However if
// FD has a generic parameter of its own and it returns a type containing
// that parameter, we want to map it to the corresponding archetype
// from the generic environment of FD, because all we do with the
// resulting type is check conformance. If the conformance is conditional,
// we might run into trouble with really complicated cases but the fake
// archetype setup will mostly work.
auto substitutions = T->getMemberSubstitutionMap(
FD, FD->getGenericEnvironment());
auto resultTy = FD->getResultInterfaceType().subst(substitutions);
if (resultTy->is<ErrorType>())
return false;
// The return type conforms to any of the requested protocols.
for (auto Proto : ExpectedTypes) {
if (checkConformance(resultTy, Proto))
return true;
}
return false;
}
public:
LocalConsumer(DeclContext *DC, Type T,
llvm::SmallPtrSetImpl<ProtocolDecl*> &expectedTypes,
SmallVectorImpl<ValueDecl *> &result)
: T(T), ExpectedTypes(expectedTypes), Result(result) {}
void foundDecl(ValueDecl *VD, DeclVisibilityKind reason,
DynamicLookupInfo) override {
if (isMatchingMethod(VD) && !VD->shouldHideFromEditor())
Result.push_back(VD);
}
} LocalConsumer(CurDeclContext, T, expectedTypes, result);
lookupVisibleMemberDecls(LocalConsumer, MetatypeType::get(T),
Loc, CurDeclContext,
/*includeInstanceMembers=*/false,
/*includeDerivedRequirements*/false,
/*includeProtocolExtensionMembers*/true);
}
} // anonymous namespace.
IDEInspectionCallbacksFactory *
swift::ide::makeConformingMethodListCallbacksFactory(
ArrayRef<const char *> expectedTypeNames,
ConformingMethodListConsumer &Consumer) {
// CC callback factory which produces 'ContextInfoCallbacks'.
class ConformingMethodListCallbacksFactoryImpl
: public IDEInspectionCallbacksFactory {
ArrayRef<const char *> ExpectedTypeNames;
ConformingMethodListConsumer &Consumer;
public:
ConformingMethodListCallbacksFactoryImpl(
ArrayRef<const char *> ExpectedTypeNames,
ConformingMethodListConsumer &Consumer)
: ExpectedTypeNames(ExpectedTypeNames), Consumer(Consumer) {}
Callbacks createCallbacks(Parser &P) override {
auto Callback = std::make_shared<ConformingMethodListCallbacks>(
P, ExpectedTypeNames, Consumer);
return {Callback, Callback};
}
};
return new ConformingMethodListCallbacksFactoryImpl(expectedTypeNames,
Consumer);
}