Skip to content

Commit 2697e96

Browse files
authored
Merge pull request #41885 from ahoppen/pr/refactor-solver-completion
[CodeCompletion] Some refactorings surrounding solver-based completion
2 parents 406aa86 + 3597652 commit 2697e96

24 files changed

+408
-311
lines changed

include/swift/AST/ASTContext.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ namespace swift {
130130
class IndexSubset;
131131
struct SILAutoDiffDerivativeFunctionKey;
132132
struct InterfaceSubContextDelegate;
133-
class TypeCheckCompletionCallback;
134133

135134
enum class KnownProtocolKind : uint8_t;
136135

@@ -146,6 +145,10 @@ namespace syntax {
146145
class SyntaxArena;
147146
}
148147

148+
namespace ide {
149+
class TypeCheckCompletionCallback;
150+
}
151+
149152
/// Lists the set of "known" Foundation entities that are used in the
150153
/// compiler.
151154
///
@@ -281,7 +284,7 @@ class ASTContext final {
281284
/// the cancellation might return with any result.
282285
std::shared_ptr<std::atomic<bool>> CancellationFlag = nullptr;
283286

284-
TypeCheckCompletionCallback *CompletionCallback = nullptr;
287+
ide::TypeCheckCompletionCallback *CompletionCallback = nullptr;
285288

286289
/// The request-evaluator that is used to process various requests.
287290
Evaluator evaluator;

include/swift/IDE/ArgumentCompletion.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "swift/IDE/CodeCompletionConsumer.h"
1717
#include "swift/IDE/CodeCompletionContext.h"
1818
#include "swift/IDE/PossibleParamInfo.h"
19-
#include "swift/Sema/CodeCompletionTypeChecking.h"
19+
#include "swift/IDE/TypeCheckCompletionCallback.h"
2020

2121
namespace swift {
2222
namespace ide {
@@ -45,9 +45,14 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
4545
Type BaseType;
4646
/// True if an argument label precedes the completion location.
4747
bool HasLabel;
48+
/// Whether the surrounding context is async and thus calling async
49+
/// functions is supported.
50+
bool IsInAsyncContext;
4851
};
4952

5053
CodeCompletionExpr *CompletionExpr;
54+
DeclContext *DC;
55+
5156
SmallVector<Result, 4> Results;
5257

5358
/// Populates a vector of parameters to suggest along with a vector of types
@@ -58,11 +63,12 @@ class ArgumentTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
5863
SmallVectorImpl<PossibleParamInfo> &Params,
5964
SmallVectorImpl<Type> &Types);
6065

61-
public:
62-
ArgumentTypeCheckCompletionCallback(CodeCompletionExpr *CompletionExpr)
63-
: CompletionExpr(CompletionExpr) {}
66+
void sawSolutionImpl(const constraints::Solution &solution) override;
6467

65-
void sawSolution(const constraints::Solution &solution) override;
68+
public:
69+
ArgumentTypeCheckCompletionCallback(CodeCompletionExpr *CompletionExpr,
70+
DeclContext *DC)
71+
: CompletionExpr(CompletionExpr), DC(DC) {}
6672

6773
/// \param IncludeSignature Whether to include a suggestion for the entire
6874
/// function signature instead of suggesting individual labels. Used when

include/swift/IDE/CompletionLookup.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ bool isCodeCompletionAtTopLevel(const DeclContext *DC);
7676
/// }
7777
bool isCompletionDeclContextLocalContext(DeclContext *DC);
7878

79+
/// Returns \c true if \p DC can handles async call.
80+
bool canDeclContextHandleAsync(const DeclContext *DC);
81+
7982
/// Return \c true if the completion happens at top-level of a library file.
8083
bool isCodeCompletionAtTopLevelOfLibraryFile(const DeclContext *DC);
8184

include/swift/IDE/DotExprCompletion.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "swift/IDE/CodeCompletionConsumer.h"
1717
#include "swift/IDE/CodeCompletionContext.h"
18-
#include "swift/Sema/CodeCompletionTypeChecking.h"
18+
#include "swift/IDE/TypeCheckCompletionCallback.h"
1919

2020
namespace swift {
2121
namespace ide {
@@ -31,22 +31,29 @@ class DotExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
3131
bool ExpectsNonVoid;
3232
bool BaseIsStaticMetaType;
3333
bool IsImplicitSingleExpressionReturn;
34+
35+
/// Whether the surrounding context is async and thus calling async
36+
/// functions is supported.
37+
bool IsInAsyncContext;
3438
};
3539

3640
CodeCompletionExpr *CompletionExpr;
41+
DeclContext *DC;
42+
3743
SmallVector<Result, 4> Results;
3844
llvm::DenseMap<std::pair<Type, Decl *>, size_t> BaseToSolutionIdx;
3945

46+
void sawSolutionImpl(const constraints::Solution &solution) override;
47+
4048
public:
41-
DotExprTypeCheckCompletionCallback(CodeCompletionExpr *CompletionExpr)
42-
: CompletionExpr(CompletionExpr) {}
49+
DotExprTypeCheckCompletionCallback(CodeCompletionExpr *CompletionExpr,
50+
DeclContext *DC)
51+
: CompletionExpr(CompletionExpr), DC(DC) {}
4352

4453
/// Typecheck the code completion expression in isolation, calling
4554
/// \c sawSolution for each solution formed.
4655
void fallbackTypeCheck(DeclContext *DC) override;
4756

48-
void sawSolution(const constraints::Solution &solution) override;
49-
5057
void deliverResults(Expr *BaseExpr, DeclContext *DC, SourceLoc DotLoc,
5158
bool IsInSelector, CodeCompletionContext &CompletionCtx,
5259
CodeCompletionConsumer &Consumer);

include/swift/IDE/ExprCompletion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "swift/IDE/CodeCompletionConsumer.h"
1717
#include "swift/IDE/CodeCompletionContext.h"
18-
#include "swift/Sema/CodeCompletionTypeChecking.h"
18+
#include "swift/IDE/TypeCheckCompletionCallback.h"
1919

2020
namespace swift {
2121
namespace ide {
@@ -46,14 +46,14 @@ class ExprTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
4646

4747
SmallVector<Result, 4> Results;
4848

49+
void sawSolutionImpl(const constraints::Solution &solution) override;
50+
4951
public:
5052
/// \param DC The decl context in which the \p CompletionExpr occurs.
5153
ExprTypeCheckCompletionCallback(CodeCompletionExpr *CompletionExpr,
5254
DeclContext *DC)
5355
: CompletionExpr(CompletionExpr), DC(DC) {}
5456

55-
void sawSolution(const constraints::Solution &solution) override;
56-
5757
/// \param CCLoc The location of the code completion token.
5858
void deliverResults(SourceLoc CCLoc,
5959
ide::CodeCompletionContext &CompletionCtx,

include/swift/IDE/KeyPathCompletion.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "swift/IDE/CodeCompletionConsumer.h"
1717
#include "swift/IDE/CodeCompletionContext.h"
18-
#include "swift/Sema/CodeCompletionTypeChecking.h"
18+
#include "swift/IDE/TypeCheckCompletionCallback.h"
1919

2020
namespace swift {
2121
namespace ide {
@@ -32,11 +32,11 @@ class KeyPathTypeCheckCompletionCallback : public TypeCheckCompletionCallback {
3232
KeyPathExpr *KeyPath;
3333
SmallVector<Result, 4> Results;
3434

35+
void sawSolutionImpl(const constraints::Solution &solution) override;
36+
3537
public:
3638
KeyPathTypeCheckCompletionCallback(KeyPathExpr *KeyPath) : KeyPath(KeyPath) {}
3739

38-
void sawSolution(const constraints::Solution &solution) override;
39-
4040
void deliverResults(DeclContext *DC, SourceLoc DotLoc,
4141
ide::CodeCompletionContext &CompletionCtx,
4242
CodeCompletionConsumer &Consumer);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===--- TypeCheckCompletionCallback.h -----------------------------------===//
2+
//
3+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See https://swift.org/LICENSE.txt for license information
7+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
//===----------------------------------------------------------------------===//
10+
//
11+
/// \file
12+
/// Provides TypeCheckCompletionCallback implementations for the various kinds
13+
/// of code completion. These extract and persist information needed to compute
14+
/// completion results from the solutions formed during expression typechecking.
15+
//
16+
//===----------------------------------------------------------------------===//
17+
18+
#ifndef SWIFT_IDE_TYPECHECKCOMPLETIONCALLBACK_H
19+
#define SWIFT_IDE_TYPECHECKCOMPLETIONCALLBACK_H
20+
21+
#include "swift/AST/Expr.h"
22+
#include "swift/AST/Type.h"
23+
#include "swift/Basic/LLVM.h"
24+
#include "llvm/ADT/DenseMap.h"
25+
#include "llvm/ADT/SmallVector.h"
26+
27+
namespace swift {
28+
class Decl;
29+
class DeclContext;
30+
class Type;
31+
class ValueDecl;
32+
class CodeCompletionExpr;
33+
34+
namespace constraints {
35+
class ConstraintSystem;
36+
class Solution;
37+
} // namespace constraints
38+
39+
namespace ide {
40+
41+
class TypeCheckCompletionCallback {
42+
bool GotCallback = false;
43+
44+
protected:
45+
/// Subclasses of \c TypeCheckCompletionCallback handle solutions discovered
46+
/// by the constraint system in this function
47+
virtual void sawSolutionImpl(const constraints::Solution &solution) = 0;
48+
49+
public:
50+
virtual ~TypeCheckCompletionCallback() {}
51+
52+
/// Called for each solution produced while type-checking an expression
53+
/// that the code completion expression participates in.
54+
void sawSolution(const constraints::Solution &solution) {
55+
GotCallback = true;
56+
sawSolutionImpl(solution);
57+
};
58+
59+
/// True if at least one solution was passed via the \c sawSolution
60+
/// callback.
61+
bool gotCallback() const { return GotCallback; }
62+
63+
/// Typecheck the code completion expression in its outermost expression
64+
/// context, calling \c sawSolution for each solution formed.
65+
virtual void fallbackTypeCheck(DeclContext *DC);
66+
};
67+
68+
// MARK: - Utility functions for subclasses of TypeCheckCompletionCallback
69+
70+
Type getTypeForCompletion(const constraints::Solution &S, Expr *E);
71+
72+
/// Whether the given completion expression is the only expression in its
73+
/// containing closure or function body and its value is implicitly returned.
74+
///
75+
/// If these conditions are met, code completion needs to avoid penalizing
76+
/// completion results that don't match the expected return type when
77+
/// computing type relations, as since no return statement was explicitly
78+
/// written by the user, it's possible they intend the single expression not
79+
/// as the return value but merely the first entry in a multi-statement body
80+
/// they just haven't finished writing yet.
81+
bool isImplicitSingleExpressionReturn(constraints::ConstraintSystem &CS,
82+
Expr *CompletionExpr);
83+
84+
/// Returns \c true iff the decl context \p DC allows calling async functions.
85+
bool isContextAsync(const constraints::Solution &S, DeclContext *DC);
86+
87+
} // namespace ide
88+
} // namespace swift
89+
90+
#endif // SWIFT_IDE_TYPECHECKCOMPLETIONCALLBACK_H

include/swift/IDE/UnresolvedMemberCompletion.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "swift/IDE/CodeCompletionConsumer.h"
1717
#include "swift/IDE/CodeCompletionContext.h"
18-
#include "swift/Sema/CodeCompletionTypeChecking.h"
18+
#include "swift/IDE/TypeCheckCompletionCallback.h"
1919

2020
namespace swift {
2121
namespace ide {
@@ -25,21 +25,27 @@ namespace ide {
2525
/// formed during expression type-checking.
2626
class UnresolvedMemberTypeCheckCompletionCallback
2727
: public TypeCheckCompletionCallback {
28-
struct ExprResult {
28+
struct Result {
2929
Type ExpectedTy;
3030
bool IsImplicitSingleExpressionReturn;
31+
32+
/// Whether the surrounding context is async and thus calling async
33+
/// functions is supported.
34+
bool IsInAsyncContext;
3135
};
3236

3337
CodeCompletionExpr *CompletionExpr;
34-
SmallVector<ExprResult, 4> ExprResults;
35-
SmallVector<Type, 1> EnumPatternTypes;
38+
DeclContext *DC;
39+
40+
SmallVector<Result, 4> ExprResults;
41+
SmallVector<Result, 1> EnumPatternTypes;
42+
43+
void sawSolutionImpl(const constraints::Solution &solution) override;
3644

3745
public:
3846
UnresolvedMemberTypeCheckCompletionCallback(
39-
CodeCompletionExpr *CompletionExpr)
40-
: CompletionExpr(CompletionExpr) {}
41-
42-
void sawSolution(const constraints::Solution &solution) override;
47+
CodeCompletionExpr *CompletionExpr, DeclContext *DC)
48+
: CompletionExpr(CompletionExpr), DC(DC) {}
4349

4450
void deliverResults(DeclContext *DC, SourceLoc DotLoc,
4551
ide::CodeCompletionContext &CompletionCtx,

include/swift/Sema/CodeCompletionTypeChecking.h

Lines changed: 0 additions & 60 deletions
This file was deleted.

include/swift/Sema/CompletionContextFinder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "swift/AST/ASTNode.h"
1717
#include "swift/AST/ASTWalker.h"
1818
#include "swift/AST/Expr.h"
19-
#include "swift/Sema/CodeCompletionTypeChecking.h"
19+
#include "swift/IDE/TypeCheckCompletionCallback.h"
2020

2121
namespace swift {
2222

include/swift/Sema/IDETypeChecking.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -159,20 +159,6 @@ namespace swift {
159159
constraints::SolutionApplicationTarget &target, bool needsPrecheck,
160160
llvm::function_ref<void(const constraints::Solution &)> callback);
161161

162-
Type getTypeForCompletion(const constraints::Solution &S, Expr *E);
163-
164-
/// Whether the given completion expression is the only expression in its
165-
/// containing closure or function body and its value is implicitly returned.
166-
///
167-
/// If these conditions are met, code completion needs to avoid penalizing
168-
/// completion results that don't match the expected return type when
169-
/// computing type relations, as since no return statement was explicitly
170-
/// written by the user, it's possible they intend the single expression not
171-
/// as the return value but merely the first entry in a multi-statement body
172-
/// they just haven't finished writing yet.
173-
bool isImplicitSingleExpressionReturn(constraints::ConstraintSystem &CS,
174-
Expr *CompletionExpr);
175-
176162
LookupResult
177163
lookupSemanticMember(DeclContext *DC, Type ty, DeclName name);
178164

0 commit comments

Comments
 (0)