Skip to content

Commit f60039b

Browse files
committed
[SourceKit] Extend expression type request to show fully qualified types
1 parent 762e0ba commit f60039b

File tree

7 files changed

+70
-30
lines changed

7 files changed

+70
-30
lines changed

include/swift/Sema/IDETypeChecking.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,23 +213,29 @@ namespace swift {
213213
/// length in the source buffer;
214214
struct ExpressionTypeInfo {
215215

216-
/// The start of the expression;
216+
/// The start of the expression.
217217
uint32_t offset;
218218

219-
/// The length of the expression;
219+
/// The length of the expression.
220220
uint32_t length;
221221

222222
/// The start of the printed type in a separately given string buffer.
223223
uint32_t typeOffset;
224224

225-
/// The length of the printed type
225+
/// The length of the printed type.
226226
uint32_t typeLength;
227227

228-
/// The offsets and lengths of all protocols the type conforms to
228+
/// The start of the printed qualified type.
229+
uint32_t qualifiedTypeOffset;
230+
231+
/// The length of the printed qualified type.
232+
uint32_t qualifiedTypeLength;
233+
234+
/// The offsets and lengths of all protocols the type conforms to.
229235
std::vector<std::pair<uint32_t, uint32_t>> protocols;
230236
};
231237

232-
/// Collect type information for every expression in \c SF; all types will
238+
/// Collect type information for every expression in \c SF; all types will.
233239
/// be printed to \c OS.
234240
ArrayRef<ExpressionTypeInfo> collectExpressionType(SourceFile &SF,
235241
ArrayRef<const char *> ExpectedProtocols,

lib/IDE/IDETypeChecking.cpp

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -696,20 +696,21 @@ class ExpressionTypeCollector: public SourceEntityWalker {
696696
std::vector<StringRef> Conformances;
697697
if (!shouldReport(Offset, Length, E, Conformances))
698698
return true;
699-
// Print the type to a temporary buffer.
700-
SmallString<64> Buffer;
701-
{
702-
llvm::raw_svector_ostream OS(Buffer);
703-
auto Ty = E->getType()->getRValueType();
704-
if (CanonicalType) {
705-
Ty->getCanonicalType()->print(OS);
706-
} else {
707-
Ty->reconstituteSugar(true)->print(OS);
708-
}
709-
}
710-
auto Ty = getTypeOffsets(Buffer.str());
699+
700+
auto TyOffsets = addToTypeBuffer(PrintOptions(), E);
701+
702+
auto printOptions = PrintOptions();
703+
printOptions.FullyQualifiedTypes = true;
704+
auto QTyOffsets = addToTypeBuffer(printOptions, E);
705+
711706
// Add the type information to the result list.
712-
Results.push_back({Offset, Length, Ty.first, Ty.second, {}});
707+
Results.push_back({Offset,
708+
Length,
709+
TyOffsets.first,
710+
TyOffsets.second,
711+
QTyOffsets.first,
712+
QTyOffsets.second,
713+
{}});
713714

714715
// Adding all protocol names to the result.
715716
for(auto Con: Conformances) {
@@ -721,6 +722,24 @@ class ExpressionTypeCollector: public SourceEntityWalker {
721722
AllPrintedTypes[Offset].insert(Length);
722723
return true;
723724
}
725+
726+
private:
727+
// Save the type to the result buffer with desired formatting and return the
728+
// offset in the buffer.
729+
std::pair<uint32_t, uint32_t> addToTypeBuffer(const PrintOptions &Options,
730+
Expr *E) {
731+
SmallString<64> Buffer;
732+
llvm::raw_svector_ostream OS(Buffer);
733+
auto Type = E->getType()->getRValueType();
734+
735+
if (CanonicalType) {
736+
Type->getCanonicalType()->print(OS, Options);
737+
} else {
738+
Type->reconstituteSugar(true)->print(OS, Options);
739+
}
740+
741+
return getTypeOffsets(Buffer.str());
742+
}
724743
};
725744

726745
ProtocolDecl* swift::resolveProtocolName(DeclContext *dc, StringRef name) {

tools/SourceKit/docs/Protocol.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -772,10 +772,11 @@ type checking and the necessary compiler arguments to help resolve all dependenc
772772
```
773773
expr-type-info ::=
774774
{
775-
<key.expression_offset>: (int64) // Offset of an expression in the source file
776-
<key.expression_length>: (int64) // Length of an expression in the source file
777-
<key.expression_type>: (string) // Printed type of this expression
778-
<key.expectedtypes>: [string*] // A list of interested protocol USRs this expression conforms to
775+
<key.expression_offset>: (int64) // Offset of an expression in the source file
776+
<key.expression_length>: (int64) // Length of an expression in the source file
777+
<key.expression_type>: (string) // Printed type of this expression
778+
<key.expression_qualified_type>: (string) // Printed fully qualified type of this expression
779+
<key.expectedtypes>: [string*] // A list of interested protocol USRs this expression conforms to
779780
}
780781
```
781782

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ struct ExpressionType {
131131
unsigned ExprOffset;
132132
unsigned ExprLength;
133133
unsigned TypeOffset;
134+
unsigned QualifiedTypeOffset;
134135
std::vector<unsigned> ProtocolOffsets;
135136
};
136137

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2559,7 +2559,11 @@ void SwiftLangSupport::collectExpressionTypes(
25592559
ExpressionTypesInFile Result;
25602560
for (auto Item: collectExpressionType(*SF, ExpectedProtocols, Scratch,
25612561
CanonicalType, OS)) {
2562-
Result.Results.push_back({Item.offset, Item.length, Item.typeOffset, {}});
2562+
Result.Results.push_back({Item.offset,
2563+
Item.length,
2564+
Item.typeOffset,
2565+
Item.qualifiedTypeOffset,
2566+
{}});
25632567
for (auto P: Item.protocols) {
25642568
Result.Results.back().ProtocolOffsets.push_back(P.first);
25652569
}

tools/SourceKit/tools/sourcekitd/lib/API/ExpressionTypeArray.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ class ExpressionTypeReader {
2929
// - Expression offset in the source buffer
3030
// - Expression length in the source buffer
3131
// - Offset of printed expression type inside printedType
32+
// - Offset of printed expression qualified type inside printedType
3233
// - Offset of the first conforming protocol in the protocol buffer
3334
// - Number of conforming protocols
34-
CompactArrayReader<unsigned, unsigned, unsigned, unsigned, unsigned> entryReader;
35+
CompactArrayReader<unsigned, unsigned, unsigned, unsigned, unsigned, unsigned>
36+
entryReader;
3537

3638
// Offsets inside printedType where a protocol name starts.
3739
CompactArrayReader<unsigned> protoReader;
@@ -57,7 +59,8 @@ class ExpressionTypeReader {
5759
ExpressionType result;
5860
unsigned protoStart, protoCount;
5961
entryReader.readEntries(idx, result.ExprOffset, result.ExprLength,
60-
result.TypeOffset, protoStart, protoCount);
62+
result.TypeOffset, result.QualifiedTypeOffset,
63+
protoStart, protoCount);
6164
for (unsigned i = protoStart, n = protoStart + protoCount; i < n; i ++) {
6265
result.ProtocolOffsets.emplace_back();
6366
protoReader.readEntries(i, result.ProtocolOffsets.back());
@@ -94,6 +97,8 @@ class ExpressionTypeReader {
9497
APPLY(KeyExpressionOffset, Int, result.ExprOffset);
9598
APPLY(KeyExpressionLength, Int, result.ExprLength);
9699
APPLY(KeyExpressionType, String, reader.readPrintedType(result.TypeOffset));
100+
APPLY(KeyExpressionQualifiedType, String,
101+
reader.readPrintedType(result.QualifiedTypeOffset));
97102
APPLY_ARRAY(ProtocolName, KeyExpectedTypes);
98103
return true;
99104
}
@@ -154,7 +159,9 @@ VariantFunctions ProtocolListFuncs::Funcs = {
154159
struct ExpressionTypeArrayBuilder::Implementation {
155160
StringRef printedType;
156161
SmallVector<char, 256> buffer;
157-
CompactArrayBuilder<unsigned, unsigned, unsigned, unsigned, unsigned> builder;
162+
CompactArrayBuilder<unsigned, unsigned, unsigned, unsigned, unsigned,
163+
unsigned>
164+
builder;
158165
CompactArrayBuilder<StringRef> strBuilder;
159166
CompactArrayBuilder<unsigned> protoBuilder;
160167

@@ -218,10 +225,11 @@ void ExpressionTypeArrayBuilder::add(const ExpressionType &expType) {
218225
Impl.protoBuilder.addEntry(off);
219226
}
220227
auto protoCount = Impl.protoBuilder.size() - protoStart;
221-
Impl.builder.addEntry(expType.ExprOffset, expType.ExprLength,
222-
expType.TypeOffset/*Printed type is null ended*/,
223-
protoStart/*Index of first protocol in the protocol buffer*/,
224-
protoCount/*Number of conforming protocols*/);
228+
Impl.builder.addEntry(
229+
expType.ExprOffset, expType.ExprLength, expType.TypeOffset,
230+
expType.QualifiedTypeOffset /*Printed types are null ended*/,
231+
protoStart /*Index of first protocol in the protocol buffer*/,
232+
protoCount /*Number of conforming protocols*/);
225233
}
226234

227235
std::unique_ptr<llvm::MemoryBuffer>

utils/gyb_sourcekit_support/UIDs.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def __init__(self, internal_name, external_name):
177177
KEY('ExpressionOffset', 'key.expression_offset'),
178178
KEY('ExpressionLength', 'key.expression_length'),
179179
KEY('ExpressionType', 'key.expression_type'),
180+
KEY('ExpressionQualifiedType', 'key.expression_qualified_type'),
180181
KEY('VariableTypeList', 'key.variable_type_list'),
181182
KEY('VariableOffset', 'key.variable_offset'),
182183
KEY('VariableLength', 'key.variable_length'),

0 commit comments

Comments
 (0)