16
16
#include " swift/AST/GenericEnvironment.h"
17
17
#include " swift/AST/NameLookup.h"
18
18
#include " swift/AST/USRGeneration.h"
19
+ #include " swift/IDE/TypeCheckCompletionCallback.h"
19
20
#include " swift/Parse/CodeCompletionCallbacks.h"
20
21
#include " swift/Sema/IDETypeChecking.h"
21
22
#include " clang/AST/Attr.h"
@@ -29,7 +30,7 @@ class ConformingMethodListCallbacks : public CodeCompletionCallbacks {
29
30
ArrayRef<const char *> ExpectedTypeNames;
30
31
ConformingMethodListConsumer &Consumer;
31
32
SourceLoc Loc;
32
- Expr *ParsedExpr = nullptr ;
33
+ CodeCompletionExpr *CCExpr = nullptr ;
33
34
DeclContext *CurDeclContext = nullptr ;
34
35
35
36
void getMatchingMethods (Type T,
@@ -55,34 +56,46 @@ class ConformingMethodListCallbacks : public CodeCompletionCallbacks {
55
56
void ConformingMethodListCallbacks::completeDotExpr (CodeCompletionExpr *E,
56
57
SourceLoc DotLoc) {
57
58
CurDeclContext = P.CurDeclContext ;
58
- ParsedExpr = E-> getBase () ;
59
+ CCExpr = E;
59
60
}
60
61
61
62
void ConformingMethodListCallbacks::completePostfixExpr (CodeCompletionExpr *E,
62
63
bool hasSpace) {
63
64
CurDeclContext = P.CurDeclContext ;
64
- ParsedExpr = E-> getBase () ;
65
+ CCExpr = E;
65
66
}
66
67
68
+ class ConformingMethodListCallback : public TypeCheckCompletionCallback {
69
+ CodeCompletionExpr *CCExpr;
70
+ SmallVector<Type, 2 > Types;
71
+
72
+ void sawSolutionImpl (const constraints::Solution &S) override {
73
+ if (Type T = getTypeForCompletion (S, CCExpr->getBase ())) {
74
+ Types.push_back (T);
75
+ }
76
+ }
77
+
78
+ public:
79
+ ConformingMethodListCallback (CodeCompletionExpr *CCExpr) : CCExpr(CCExpr) {}
80
+
81
+ ArrayRef<Type> getTypes () const { return Types; }
82
+ };
83
+
67
84
void ConformingMethodListCallbacks::doneParsing () {
68
- if (!ParsedExpr )
85
+ if (!CCExpr || !CCExpr-> getBase () )
69
86
return ;
70
87
88
+ ConformingMethodListCallback TypeCheckCallback (CCExpr);
89
+ llvm::SaveAndRestore<TypeCheckCompletionCallback *> CompletionCollector (
90
+ Context.CompletionCallback , &TypeCheckCallback);
71
91
typeCheckContextAt (TypeCheckASTNodeAtLocContext::declContext (CurDeclContext),
72
- ParsedExpr->getLoc ());
73
-
74
- Type T = ParsedExpr->getType ();
75
-
76
- // Type check the expression if needed.
77
- if (!T || T->is <ErrorType>()) {
78
- ConcreteDeclRef ReferencedDecl = nullptr ;
79
- auto optT = getTypeOfCompletionContextExpr (P.Context , CurDeclContext,
80
- CompletionTypeCheckKind::Normal,
81
- ParsedExpr, ReferencedDecl);
82
- if (!optT)
83
- return ;
84
- T = *optT;
92
+ CCExpr->getLoc ());
93
+
94
+ if (TypeCheckCallback.getTypes ().size () != 1 ) {
95
+ // Either no results or results were ambiguous, which we cannot handle.
96
+ return ;
85
97
}
98
+ Type T = TypeCheckCallback.getTypes ()[0 ];
86
99
87
100
if (!T || T->is <ErrorType>() || T->is <UnresolvedType>())
88
101
return ;
0 commit comments