Skip to content

Commit b665de8

Browse files
authored
Merge pull request #3472 from nkcsgexi/type-interface
[SourceKit] In CursorInfo response, include the mangle name of type.
2 parents d39ad94 + a7e4cfd commit b665de8

File tree

17 files changed

+207
-19
lines changed

17 files changed

+207
-19
lines changed

include/swift/AST/USRGeneration.h

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

2424
namespace ide {
2525

26+
/// Prints out the USR for the Type.
27+
/// \returns true if it failed, false on success.
28+
bool printTypeUSR(Type Ty, raw_ostream &OS);
29+
30+
/// Prints out the USR for the Type of the given decl.
31+
/// \returns true if it failed, false on success.
32+
bool printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS);
33+
2634
/// Prints out the USR for the given Decl.
2735
/// \returns true if it failed, false on success.
2836
bool printDeclUSR(const ValueDecl *D, raw_ostream &OS);

include/swift/IDE/Utils.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,13 @@ struct SemaToken {
151151
bool IsKeywordArgument = false;
152152
Type Ty;
153153
DeclContext *DC = nullptr;
154+
Type ContainerType;
154155

155156
SemaToken() = default;
156157
SemaToken(ValueDecl *ValueD, TypeDecl *CtorTyRef, SourceLoc Loc, bool IsRef,
157-
Type Ty) : ValueD(ValueD), CtorTyRef(CtorTyRef), Loc(Loc),
158-
IsRef(IsRef), Ty(Ty), DC(ValueD->getDeclContext()) {}
158+
Type Ty, Type ContainerType) : ValueD(ValueD), CtorTyRef(CtorTyRef), Loc(Loc),
159+
IsRef(IsRef), Ty(Ty), DC(ValueD->getDeclContext()),
160+
ContainerType(ContainerType) {}
159161
SemaToken(ModuleEntity Mod, SourceLoc Loc) : Mod(Mod), Loc(Loc) { }
160162

161163
bool isValid() const { return ValueD != nullptr || Mod; }
@@ -166,12 +168,14 @@ class SemaLocResolver : public SourceEntityWalker {
166168
SourceFile &SrcFile;
167169
SourceLoc LocToResolve;
168170
SemaToken SemaTok;
171+
Type ContainerType;
169172

170173
public:
171174
explicit SemaLocResolver(SourceFile &SrcFile) : SrcFile(SrcFile) { }
172175
SemaToken resolve(SourceLoc Loc);
173176
SourceManager &getSourceMgr() const;
174177
private:
178+
bool walkToExprPre(Expr *E) override;
175179
bool walkToDeclPre(Decl *D, CharSourceRange Range) override;
176180
bool walkToDeclPost(Decl *D) override;
177181
bool walkToStmtPre(Stmt *S) override;

lib/AST/USRGeneration.cpp

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

32+
bool ide::printTypeUSR(Type Ty, raw_ostream &OS) {
33+
using namespace Mangle;
34+
Mangler Mangler(true);
35+
Mangler.mangleType(Ty->getRValueType(), 0);
36+
Mangler.finalize(OS);
37+
return false;
38+
}
39+
40+
bool ide::printDeclTypeUSR(const ValueDecl *D, raw_ostream &OS) {
41+
using namespace Mangle;
42+
Mangler Mangler(true);
43+
Mangler.mangleDeclTypeForDebugger(D);
44+
Mangler.finalize(OS);
45+
return false;
46+
}
47+
3248
bool ide::printDeclUSR(const ValueDecl *D, raw_ostream &OS) {
3349
using namespace Mangle;
3450

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ SourceManager &SemaLocResolver::getSourceMgr() const
5757
}
5858

5959
bool SemaLocResolver::tryResolve(ValueDecl *D, TypeDecl *CtorTyRef,
60-
SourceLoc Loc, bool IsRef, Type Ty) {
60+
SourceLoc Loc, bool IsRef, Type Ty) {
6161
if (!D->hasName())
6262
return false;
6363

6464
if (Loc == LocToResolve) {
65-
SemaTok = { D, CtorTyRef, Loc, IsRef, Ty };
65+
SemaTok = { D, CtorTyRef, Loc, IsRef, Ty, ContainerType };
6666
return true;
6767
}
6868
return false;
@@ -138,6 +138,22 @@ bool SemaLocResolver::visitDeclReference(ValueDecl *D, CharSourceRange Range,
138138
return !tryResolve(D, CtorTyRef, Range.getStart(), /*IsRef=*/true, T);
139139
}
140140

141+
bool SemaLocResolver::walkToExprPre(Expr *E) {
142+
if (!isDone()) {
143+
if (auto SAE = dyn_cast<SelfApplyExpr>(E)) {
144+
if (SAE->getFn()->getStartLoc() == LocToResolve) {
145+
ContainerType = SAE->getBase()->getType();
146+
}
147+
} else if (auto ME = dyn_cast<MemberRefExpr>(E)) {
148+
SourceLoc DotLoc = ME->getDotLoc();
149+
if (DotLoc.isValid() && DotLoc.getAdvancedLoc(1) == LocToResolve) {
150+
ContainerType = ME->getBase()->getType();
151+
}
152+
}
153+
}
154+
return true;
155+
}
156+
141157
bool SemaLocResolver::visitCallArgName(Identifier Name, CharSourceRange Range,
142158
ValueDecl *D) {
143159
if (isDone())

test/SourceKit/CursorInfo/cursor_info.swift

Lines changed: 24 additions & 13 deletions
Large diffs are not rendered by default.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
struct S {
2+
func getArray() -> [S] { return [] }
3+
func getInstance() -> S { return self }
4+
static var Instance : S = S()
5+
}
6+
7+
class C {
8+
func getArray() -> [C] { return [] }
9+
func getInstance() -> C { return self }
10+
static var Instance : C = C()
11+
}
12+
13+
enum E {
14+
case CASE1
15+
func getArray() -> [E] { return [] }
16+
func getInstance() -> E { return self }
17+
static var Instance : E = E.CASE1
18+
}
19+
func SGen() -> S { return S() }
20+
func CGen() -> C { return C() }
21+
func EGen() -> E { return .CASE1}
22+
23+
func foo(s : S, c : C, e: E) {
24+
_ = s.getArray()
25+
_ = s.getInstance()
26+
_ = S.Instance
27+
_ = c.getArray()
28+
_ = c.getInstance()
29+
_ = C.Instance
30+
_ = e.getInstance()
31+
_ = e.getArray()
32+
_ = E.CASE1
33+
_ = E.Instance
34+
_ = SGen().getArray()
35+
_ = CGen().getInstance()
36+
_ = EGen().getArray()
37+
_ = SArrayGen().count
38+
}
39+
40+
func SArrayGen() -> [S] { return [] }
41+
42+
// RUN: %sourcekitd-test -req=cursor -pos=24:12 %s -- %s | FileCheck -check-prefix=CHECK1 %s
43+
// RUN: %sourcekitd-test -req=cursor -pos=25:12 %s -- %s | FileCheck -check-prefix=CHECK1 %s
44+
// RUN: %sourcekitd-test -req=cursor -pos=34:19 %s -- %s | FileCheck -check-prefix=CHECK1 %s
45+
// CHECK1: <Container>V21cursor_info_container1S</Container>
46+
47+
// RUN: %sourcekitd-test -req=cursor -pos=26:12 %s -- %s | FileCheck -check-prefix=CHECK2 %s
48+
// CHECK2: <Container>MV21cursor_info_container1S</Container>
49+
50+
// RUN: %sourcekitd-test -req=cursor -pos=27:12 %s -- %s | FileCheck -check-prefix=CHECK3 %s
51+
// RUN: %sourcekitd-test -req=cursor -pos=28:12 %s -- %s | FileCheck -check-prefix=CHECK3 %s
52+
// RUN: %sourcekitd-test -req=cursor -pos=35:19 %s -- %s | FileCheck -check-prefix=CHECK3 %s
53+
// CHECK3: <Container>C21cursor_info_container1C</Container>
54+
55+
// RUN: %sourcekitd-test -req=cursor -pos=29:12 %s -- %s | FileCheck -check-prefix=CHECK4 %s
56+
// CHECK4: <Container>MC21cursor_info_container1C</Container>
57+
58+
// RUN: %sourcekitd-test -req=cursor -pos=30:12 %s -- %s | FileCheck -check-prefix=CHECK5 %s
59+
// RUN: %sourcekitd-test -req=cursor -pos=31:12 %s -- %s | FileCheck -check-prefix=CHECK5 %s
60+
// RUN: %sourcekitd-test -req=cursor -pos=36:19 %s -- %s | FileCheck -check-prefix=CHECK5 %s
61+
// CHECK5: <Container>O21cursor_info_container1E</Container>
62+
63+
// RUN: %sourcekitd-test -req=cursor -pos=32:12 %s -- %s | FileCheck -check-prefix=CHECK6 %s
64+
// RUN: %sourcekitd-test -req=cursor -pos=33:12 %s -- %s | FileCheck -check-prefix=CHECK6 %s
65+
// CHECK6: <Container>MO21cursor_info_container1E</Container>
66+
67+
// RUN: %sourcekitd-test -req=cursor -pos=37:22 %s -- %s | FileCheck -check-prefix=CHECK7 %s
68+
// CHECK7: <Container>GSaV21cursor_info_container1S_</Container>

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ struct CursorInfo {
253253
StringRef Name;
254254
StringRef USR;
255255
StringRef TypeName;
256+
StringRef TypeUSR;
257+
StringRef ContainerTypeUSR;
256258
StringRef DocComment;
257259
StringRef TypeInterface;
258260
StringRef GroupName;

0 commit comments

Comments
 (0)