13
13
#include " swift/IDE/ArgumentCompletion.h"
14
14
#include " swift/IDE/CodeCompletion.h"
15
15
#include " swift/IDE/CompletionLookup.h"
16
+ #include " swift/IDE/SelectedOverloadInfo.h"
16
17
#include " swift/Sema/ConstraintSystem.h"
17
18
#include " swift/Sema/IDETypeChecking.h"
18
19
@@ -78,98 +79,6 @@ bool ArgumentTypeCheckCompletionCallback::addPossibleParams(
78
79
return ShowGlobalCompletions;
79
80
}
80
81
81
- // / Information that \c getSelectedOverloadInfo gathered about a
82
- // / \c SelectedOverload.
83
- struct SelectedOverloadInfo {
84
- // / The function that is being called.
85
- ValueDecl *FuncD = nullptr ;
86
- // / The type of the called function itself (not its result type)
87
- Type FuncTy;
88
- // / The type on which the function is being called. \c null if the function is
89
- // / a free function.
90
- Type CallBaseTy;
91
- };
92
-
93
- // / Extract additional information about the overload that is being called by
94
- // / \p CalleeLocator.
95
- SelectedOverloadInfo getSelectedOverloadInfo (const Solution &S,
96
- ConstraintLocator *CalleeLocator) {
97
- auto &CS = S.getConstraintSystem ();
98
-
99
- SelectedOverloadInfo Result;
100
-
101
- auto SelectedOverload = S.getOverloadChoiceIfAvailable (CalleeLocator);
102
- if (!SelectedOverload) {
103
- return Result;
104
- }
105
-
106
- switch (SelectedOverload->choice .getKind ()) {
107
- case OverloadChoiceKind::KeyPathApplication:
108
- case OverloadChoiceKind::Decl:
109
- case OverloadChoiceKind::DeclViaDynamic:
110
- case OverloadChoiceKind::DeclViaBridge:
111
- case OverloadChoiceKind::DeclViaUnwrappedOptional: {
112
- Result.CallBaseTy = SelectedOverload->choice .getBaseType ();
113
- if (Result.CallBaseTy ) {
114
- Result.CallBaseTy = S.simplifyType (Result.CallBaseTy )->getRValueType ();
115
- }
116
-
117
- Result.FuncD = SelectedOverload->choice .getDeclOrNull ();
118
- Result.FuncTy =
119
- S.simplifyTypeForCodeCompletion (SelectedOverload->adjustedOpenedType );
120
-
121
- // For completion as the arg in a call to the implicit [keypath: _]
122
- // subscript the solver can't know what kind of keypath is expected without
123
- // an actual argument (e.g. a KeyPath vs WritableKeyPath) so it ends up as a
124
- // hole. Just assume KeyPath so we show the expected keypath's root type to
125
- // users rather than '_'.
126
- if (SelectedOverload->choice .getKind () ==
127
- OverloadChoiceKind::KeyPathApplication) {
128
- auto Params = Result.FuncTy ->getAs <AnyFunctionType>()->getParams ();
129
- if (Params.size () == 1 &&
130
- Params[0 ].getPlainType ()->is <UnresolvedType>()) {
131
- auto *KPDecl = CS.getASTContext ().getKeyPathDecl ();
132
- Type KPTy =
133
- KPDecl->mapTypeIntoContext (KPDecl->getDeclaredInterfaceType ());
134
- Type KPValueTy = KPTy->castTo <BoundGenericType>()->getGenericArgs ()[1 ];
135
- KPTy = BoundGenericType::get (KPDecl, Type (),
136
- {Result.CallBaseTy , KPValueTy});
137
- Result.FuncTy =
138
- FunctionType::get ({Params[0 ].withType (KPTy)}, KPValueTy);
139
- }
140
- }
141
- break ;
142
- }
143
- case OverloadChoiceKind::KeyPathDynamicMemberLookup: {
144
- auto *fnType = SelectedOverload->adjustedOpenedType ->castTo <FunctionType>();
145
- assert (fnType->getParams ().size () == 1 &&
146
- " subscript always has one argument" );
147
- // Parameter type is KeyPath<T, U> where `T` is a root type
148
- // and U is a leaf type (aka member type).
149
- auto keyPathTy =
150
- fnType->getParams ()[0 ].getPlainType ()->castTo <BoundGenericType>();
151
-
152
- auto *keyPathDecl = keyPathTy->getAnyNominal ();
153
- assert (isKnownKeyPathType (keyPathTy) &&
154
- " parameter is supposed to be a keypath" );
155
-
156
- auto KeyPathDynamicLocator = CS.getConstraintLocator (
157
- CalleeLocator, LocatorPathElt::KeyPathDynamicMember (keyPathDecl));
158
- Result = getSelectedOverloadInfo (S, KeyPathDynamicLocator);
159
- break ;
160
- }
161
- case OverloadChoiceKind::DynamicMemberLookup:
162
- case OverloadChoiceKind::TupleIndex:
163
- // If it's DynamicMemberLookup, we don't know which function is being
164
- // called, so we can't extract any information from it.
165
- // TupleIndex isn't a function call and is not relevant for argument
166
- // completion because it doesn't take arguments.
167
- break ;
168
- }
169
-
170
- return Result;
171
- }
172
-
173
82
void ArgumentTypeCheckCompletionCallback::sawSolutionImpl (const Solution &S) {
174
83
Type ExpectedTy = getTypeForCompletion (S, CompletionExpr);
175
84
@@ -246,9 +155,9 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
246
155
247
156
// If this is a duplicate of any other result, ignore this solution.
248
157
if (llvm::any_of (Results, [&](const Result &R) {
249
- return R.FuncD == Info.FuncD &&
250
- nullableTypesEqual (R.FuncTy , Info.FuncTy ) &&
251
- nullableTypesEqual (R.BaseType , Info.CallBaseTy ) &&
158
+ return R.FuncD == Info.Value &&
159
+ nullableTypesEqual (R.FuncTy , Info.ValueTy ) &&
160
+ nullableTypesEqual (R.BaseType , Info.BaseTy ) &&
252
161
R.ParamIdx == ParamIdx &&
253
162
R.IsNoninitialVariadic == IsNoninitialVariadic;
254
163
})) {
@@ -258,9 +167,9 @@ void ArgumentTypeCheckCompletionCallback::sawSolutionImpl(const Solution &S) {
258
167
llvm::SmallDenseMap<const VarDecl *, Type> SolutionSpecificVarTypes;
259
168
getSolutionSpecificVarTypes (S, SolutionSpecificVarTypes);
260
169
261
- Results.push_back ({ExpectedTy, isa<SubscriptExpr>(ParentCall), Info.FuncD ,
262
- Info.FuncTy , ArgIdx, ParamIdx, std::move (ClaimedParams),
263
- IsNoninitialVariadic, Info.CallBaseTy , HasLabel, IsAsync,
170
+ Results.push_back ({ExpectedTy, isa<SubscriptExpr>(ParentCall), Info.Value ,
171
+ Info.ValueTy , ArgIdx, ParamIdx, std::move (ClaimedParams),
172
+ IsNoninitialVariadic, Info.BaseTy , HasLabel, IsAsync,
264
173
SolutionSpecificVarTypes});
265
174
}
266
175
0 commit comments