Skip to content

Commit a5e51a3

Browse files
committed
[SourceKit] In CursorInfo response, include the mangle name of the type of the underlying decl.
The mangled name of the type is identical to those for debugger. These mangled names allow us to reconstruct the type from AST and generate interface specifically for that type. Related rdar://27306890
1 parent e9688dc commit a5e51a3

File tree

14 files changed

+67
-13
lines changed

14 files changed

+67
-13
lines changed

include/swift/AST/USRGeneration.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ enum class AccessorKind;
2323

2424
namespace ide {
2525

26+
/// Prints out the USR for the Type of the given decl.
27+
/// \returns true if it failed, false on success.
28+
bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
29+
2630
/// Prints out the USR for the given Decl.
2731
/// \returns true if it failed, false on success.
2832
bool printDeclUSR(const ValueDecl *D, raw_ostream &OS);

lib/AST/USRGeneration.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ static inline StringRef getUSRSpacePrefix() {
2929
return "s:";
3030
}
3131

32+
bool ide::printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS) {
33+
using namespace Mangle;
34+
Mangler Mangler(true);
35+
Mangler.mangleDeclTypeForDebugger(D);
36+
Mangler.finalize(OS);
37+
return false;
38+
}
39+
3240
bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
3341
using namespace Mangle;
3442

test/SourceKit/CursorInfo/cursor_info.swift

Lines changed: 23 additions & 13 deletions
Large diffs are not rendered by default.

test/SourceKit/CursorInfo/cursor_stdlib.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func foo3(a: Float, b: Bool) {}
2626
// CHECK-OVERLAY-NEXT: NSUTF8StringEncoding
2727
// CHECK-OVERLAY-NEXT: s:v10Foundation20NSUTF8StringEncodingSu
2828
// CHECK-OVERLAY-NEXT: UInt
29+
// CHECK-OVERLAY-NEXT: _TtSu
2930
// CHECK-OVERLAY-NEXT: <Declaration>public let NSUTF8StringEncoding: <Type usr="s:Su">UInt</Type></Declaration>
3031

3132
// RUN: %sourcekitd-test -req=cursor -pos=5:13 %s -- %s %mcp_opt %clang-importer-sdk | FileCheck -check-prefix=CHECK-ITERATOR %s

test/SourceKit/CursorInfo/cursor_usr.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func foo(x: FooStruct1) -> S1 {}
2222
// CHECK_SANITY1-NEXT: global
2323
// CHECK_SANITY1-NEXT: s:v10cursor_usr6globalSi
2424
// CHECK_SANITY1-NEXT: Int
25+
// CHECK_SANITY1-NEXT: _TtSi
2526
// CHECK_SANITY1-NEXT: <Declaration>var global: <Type usr="s:Si">Int</Type></Declaration>
2627
// CHECK_SANITY1-NEXT: <decl.var.global><syntaxtype.keyword>var</syntaxtype.keyword> <decl.name>global</decl.name>: <decl.var.type><ref.struct usr="s:Si">Int</ref.struct></decl.var.type></decl.var.global>
2728

test/SourceKit/InterfaceGen/gen_stdlib.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var x: Int
3333
// CHECK1-NEXT: Int
3434
// CHECK1-NEXT: s:Si
3535
// CHECK1-NEXT: Int.Type
36+
// CHECK1-NEXT: _Tt
3637
// CHECK1-NEXT: Swift{{$}}
3738
// CHECK1-NEXT: <Group>Math/Integers</Group>
3839
// CHECK1-NEXT: /<interface-gen>{{$}}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ struct CursorInfo {
253253
StringRef Name;
254254
StringRef USR;
255255
StringRef TypeName;
256+
StringRef TypeUSR;
256257
StringRef DocComment;
257258
StringRef TypeInterface;
258259
StringRef GroupName;

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,10 @@ bool SwiftLangSupport::printUSR(const ValueDecl *D, llvm::raw_ostream &OS) {
697697
return ide::printDeclUSR(D, OS);
698698
}
699699

700+
bool SwiftLangSupport::printDeclTypeUSR(const ValueDecl *D, llvm::raw_ostream &OS) {
701+
return ide::printDeclTypeUSR(D, OS);
702+
}
703+
700704
bool SwiftLangSupport::printAccessorUSR(const AbstractStorageDecl *D,
701705
AccessorKind AccKind,
702706
llvm::raw_ostream &OS) {

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ class SwiftLangSupport : public LangSupport {
261261
/// \returns true if the results should be ignored, false otherwise.
262262
static bool printUSR(const swift::ValueDecl *D, llvm::raw_ostream &OS);
263263

264+
/// Generate a USR for the Type of a given decl.
265+
/// \returns true if the results should be ignored, false otherwise.
266+
static bool printDeclTypeUSR(const swift::ValueDecl *D, llvm::raw_ostream &OS);
267+
264268
/// Generate a USR for an accessor, including the prefix.
265269
/// \returns true if the results should be ignored, false otherwise.
266270
static bool printAccessorUSR(const swift::AbstractStorageDecl *D,

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,13 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
657657
}
658658
unsigned TypenameEnd = SS.size();
659659

660+
unsigned MangledTypeStart = SS.size();
661+
{
662+
llvm::raw_svector_ostream OS(SS);
663+
SwiftLangSupport::printDeclTypeUSR(VD, OS);
664+
}
665+
unsigned MangledTypeEnd = SS.size();
666+
660667
unsigned DocCommentBegin = SS.size();
661668
{
662669
llvm::raw_svector_ostream OS(SS);
@@ -772,6 +779,8 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
772779
StringRef USR = StringRef(SS.begin()+USRBegin, USREnd-USRBegin);
773780
StringRef TypeName = StringRef(SS.begin()+TypenameBegin,
774781
TypenameEnd-TypenameBegin);
782+
StringRef TypeUsr = StringRef(SS.begin()+MangledTypeStart,
783+
MangledTypeEnd - MangledTypeStart);
775784
StringRef DocComment = StringRef(SS.begin()+DocCommentBegin,
776785
DocCommentEnd-DocCommentBegin);
777786
StringRef AnnotatedDecl = StringRef(SS.begin()+DeclBegin,
@@ -812,6 +821,7 @@ static bool passCursorInfoForDecl(const ValueDecl *VD,
812821
Info.Name = Name;
813822
Info.USR = USR;
814823
Info.TypeName = TypeName;
824+
Info.TypeUSR = TypeUsr;
815825
Info.DocComment = DocComment;
816826
Info.AnnotatedDeclaration = AnnotatedDecl;
817827
Info.FullyAnnotatedDeclaration = FullyAnnotatedDecl;

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ static sourcekitd_uid_t KeyNotification;
118118
static sourcekitd_uid_t KeyPopular;
119119
static sourcekitd_uid_t KeyUnpopular;
120120
static sourcekitd_uid_t KeyTypeInterface;
121+
static sourcekitd_uid_t KeyTypeUsr;
121122
static sourcekitd_uid_t KeyModuleGroups;
122123
static sourcekitd_uid_t KeySimplified;
123124

@@ -216,6 +217,7 @@ static int skt_main(int argc, const char **argv) {
216217
KeyPopular = sourcekitd_uid_get_from_cstr("key.popular");
217218
KeyUnpopular = sourcekitd_uid_get_from_cstr("key.unpopular");
218219
KeyTypeInterface = sourcekitd_uid_get_from_cstr("key.typeinterface");
220+
KeyTypeUsr = sourcekitd_uid_get_from_cstr("key.typeusr");
219221
KeyModuleGroups = sourcekitd_uid_get_from_cstr("key.modulegroups");
220222
KeySimplified = sourcekitd_uid_get_from_cstr("key.simplified");
221223

@@ -935,6 +937,8 @@ static void printCursorInfo(sourcekitd_variant_t Info, StringRef FilenameIn,
935937
const char *Name = sourcekitd_variant_dictionary_get_string(Info, KeyName);
936938
const char *Typename = sourcekitd_variant_dictionary_get_string(Info,
937939
KeyTypename);
940+
const char *TypeUsr = sourcekitd_variant_dictionary_get_string(Info,
941+
KeyTypeUsr);
938942
const char *ModuleName = sourcekitd_variant_dictionary_get_string(Info,
939943
KeyModuleName);
940944
const char *GroupName = sourcekitd_variant_dictionary_get_string(Info,
@@ -1006,6 +1010,8 @@ static void printCursorInfo(sourcekitd_variant_t Info, StringRef FilenameIn,
10061010
OS << USR << '\n';
10071011
if (Typename)
10081012
OS << Typename << '\n';
1013+
if (TypeUsr)
1014+
OS << TypeUsr << '\n';
10091015
if (ModuleName)
10101016
OS << ModuleName << '\n';
10111017
if (GroupName)

tools/SourceKit/tools/sourcekitd/lib/API/DictionaryKeys.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ extern SourceKit::UIdent KeyDeprecated;
117117
extern SourceKit::UIdent KeyObsoleted;
118118
extern SourceKit::UIdent KeyRemoveCache;
119119
extern SourceKit::UIdent KeyTypeInterface;
120+
extern SourceKit::UIdent KeyTypeUsr;
120121
extern SourceKit::UIdent KeyModuleGroups;
121122

122123
/// \brief Used for determining the printing order of dictionary keys.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,8 @@ static void reportCursorInfo(const CursorInfo &Info, ResponseReceiver Rec) {
13281328
Elem.setBool(KeyIsSystem, true);
13291329
if (!Info.TypeInterface.empty())
13301330
Elem.set(KeyTypeInterface, Info.TypeInterface);
1331+
if (!Info.TypeUSR.empty())
1332+
Elem.set(KeyTypeUsr, Info.TypeUSR);
13311333

13321334
return Rec(RespBuilder.createResponse());
13331335
}

tools/SourceKit/tools/sourcekitd/lib/API/sourcekitdAPI-Common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ UIdent sourcekitd::KeyDeprecated("key.deprecated");
131131
UIdent sourcekitd::KeyObsoleted("key.obsoleted");
132132
UIdent sourcekitd::KeyRemoveCache("key.removecache");
133133
UIdent sourcekitd::KeyTypeInterface("key.typeinterface");
134+
UIdent sourcekitd::KeyTypeUsr("key.typeusr");
134135
UIdent sourcekitd::KeyModuleGroups("key.modulegroups");
135136

136137
/// \brief Order for the keys to use when emitting the debug description of

0 commit comments

Comments
 (0)