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;

0 commit comments

Comments
 (0)