Skip to content

Commit 7aa472c

Browse files
committed
[CodeComplete] Make SourceKit::CodeCompletion::Completion store a reference to the underlying swift result instead of extending that type
Previously, when creating a `SourceKit::CodeCompletion::Completion`, we needed to copy all fields from the underlying `SwiftResult` (aka `swift::ide::CodeCompletionResult`). The arena in which the `SwiftResult` was allocated still needed to be kept alive for the references stored in the `SwiftResult`. To avoid this unnecessary copy, make `SourceKit::CodeCompletion::Completion` store a reference to the underlying `SwiftResult`.
1 parent ee99666 commit 7aa472c

File tree

3 files changed

+154
-93
lines changed

3 files changed

+154
-93
lines changed

tools/SourceKit/lib/SwiftLang/CodeCompletion.h

Lines changed: 74 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ namespace SourceKit {
2323
namespace CodeCompletion {
2424

2525
using swift::ide::CodeCompletionDeclKind;
26+
using swift::ide::CodeCompletionFlair;
2627
using swift::ide::CodeCompletionKeywordKind;
2728
using swift::ide::CodeCompletionLiteralKind;
28-
using swift::ide::SemanticContextKind;
29-
using swift::ide::CodeCompletionFlair;
29+
using swift::ide::CodeCompletionOperatorKind;
3030
using swift::ide::CodeCompletionString;
31+
using swift::ide::SemanticContextKind;
3132
using SwiftResult = swift::ide::CodeCompletionResult;
3233
using swift::ide::CompletionKind;
3334

@@ -75,7 +76,8 @@ struct NameStyle {
7576
///
7677
/// Extends a \c swift::ide::CodeCompletionResult with extra fields that are
7778
/// filled in by SourceKit. Generally stored in an \c CompletionSink.
78-
class Completion : public SwiftResult {
79+
class Completion {
80+
const SwiftResult &base;
7981
void *opaqueCustomKind = nullptr;
8082
Optional<uint8_t> moduleImportDepth;
8183
PopularityFactor popularityFactor;
@@ -89,9 +91,12 @@ class Completion : public SwiftResult {
8991

9092
/// Wraps \p base with an \c Completion. The \p name and \p description
9193
/// should outlive the result, generally by being stored in the same
92-
/// \c CompletionSink.
93-
Completion(SwiftResult base, StringRef name, StringRef description)
94-
: SwiftResult(base), name(name), description(description) {}
94+
/// \c CompletionSink or in a sink that was adopted by the sink that this
95+
/// \c Compleiton is being stored in.
96+
Completion(const SwiftResult &base, StringRef name, StringRef description)
97+
: base(base), name(name), description(description) {}
98+
99+
const SwiftResult &getSwiftResult() const { return base; }
95100

96101
bool hasCustomKind() const { return opaqueCustomKind; }
97102
void *getCustomKind() const { return opaqueCustomKind; }
@@ -102,6 +107,63 @@ class Completion : public SwiftResult {
102107
/// A popularity factory in the range [-1, 1]. The higher the value, the more
103108
/// 'popular' this result is. 0 indicates unknown.
104109
PopularityFactor getPopularityFactor() const { return popularityFactor; }
110+
111+
// MARK: Methods that forward to the SwiftResult
112+
113+
SwiftResult::ResultKind getKind() const { return getSwiftResult().getKind(); }
114+
115+
CodeCompletionDeclKind getAssociatedDeclKind() const {
116+
return getSwiftResult().getAssociatedDeclKind();
117+
}
118+
119+
CodeCompletionLiteralKind getLiteralKind() const {
120+
return getSwiftResult().getLiteralKind();
121+
}
122+
123+
CodeCompletionKeywordKind getKeywordKind() const {
124+
return getSwiftResult().getKeywordKind();
125+
}
126+
127+
bool isOperator() const { return getSwiftResult().isOperator(); }
128+
129+
CodeCompletionOperatorKind getOperatorKind() const {
130+
return getSwiftResult().getOperatorKind();
131+
}
132+
133+
bool isSystem() const { return getSwiftResult().isSystem(); }
134+
135+
SwiftResult::ExpectedTypeRelation getExpectedTypeRelation() const {
136+
return getSwiftResult().getExpectedTypeRelation();
137+
}
138+
139+
SemanticContextKind getSemanticContext() const {
140+
return getSwiftResult().getSemanticContext();
141+
}
142+
143+
CodeCompletionFlair getFlair() const { return getSwiftResult().getFlair(); }
144+
145+
bool isNotRecommended() const { return getSwiftResult().isNotRecommended(); }
146+
147+
unsigned getNumBytesToErase() const {
148+
return getSwiftResult().getNumBytesToErase();
149+
}
150+
151+
CodeCompletionString *getCompletionString() const {
152+
return getSwiftResult().getCompletionString();
153+
}
154+
155+
StringRef getModuleName() const { return getSwiftResult().getModuleName(); }
156+
157+
StringRef getBriefDocComment() const {
158+
return getSwiftResult().getBriefDocComment();
159+
}
160+
161+
ArrayRef<StringRef> getAssociatedUSRs() const {
162+
return getSwiftResult().getAssociatedUSRs();
163+
}
164+
165+
/// Allow "upcasting" the completion result to a SwiftResult.
166+
operator const SwiftResult &() const { return getSwiftResult(); }
105167
};
106168

107169
/// Storage sink for \c Completion objects.
@@ -123,9 +185,9 @@ struct CompletionSink {
123185

124186
class CompletionBuilder {
125187
CompletionSink &sink;
126-
SwiftResult &current;
188+
const SwiftResult &base;
127189
bool modified = false;
128-
Completion::ExpectedTypeRelation typeRelation;
190+
SwiftResult::ExpectedTypeRelation typeRelation;
129191
SemanticContextKind semanticContext;
130192
CodeCompletionFlair flair;
131193
CodeCompletionString *completionString;
@@ -135,7 +197,7 @@ class CompletionBuilder {
135197
PopularityFactor popularityFactor;
136198

137199
public:
138-
CompletionBuilder(CompletionSink &sink, SwiftResult &base);
200+
CompletionBuilder(CompletionSink &sink, const SwiftResult &base);
139201

140202
void setCustomKind(void *opaqueCustomKind) { customKind = opaqueCustomKind; }
141203

@@ -144,7 +206,7 @@ class CompletionBuilder {
144206
moduleImportDepth = value;
145207
}
146208

147-
void setExpectedTypeRelation(Completion::ExpectedTypeRelation Relation) {
209+
void setExpectedTypeRelation(SwiftResult::ExpectedTypeRelation Relation) {
148210
modified = true;
149211
typeRelation = Relation;
150212
}
@@ -233,8 +295,8 @@ struct FilterRules {
233295
llvm::StringMap<bool> hideByFilterName;
234296
llvm::StringMap<bool> hideByDescription;
235297

236-
bool hideCompletion(Completion *completion) const;
237-
bool hideCompletion(SwiftResult *completion, StringRef name,
298+
bool hideCompletion(const Completion &completion) const;
299+
bool hideCompletion(const SwiftResult &completion, StringRef name,
238300
StringRef description, void *customKind = nullptr) const;
239301
bool hideFilterName(StringRef name) const;
240302
};

0 commit comments

Comments
 (0)