@@ -44,72 +44,58 @@ void Symbol::serializeKind(StringRef Identifier, StringRef DisplayName,
44
44
});
45
45
}
46
46
47
- void Symbol::serializeKind (llvm::json::OStream &OS ) const {
48
- // supportsKind and serializeKind must agree .
47
+ std::pair<StringRef, StringRef> Symbol::getKind ( const ValueDecl *VD ) const {
48
+ // Make sure supportsKind stays in sync with getKind .
49
49
assert (Symbol::supportsKind (VD->getKind ()) && " unsupported decl kind" );
50
-
51
- AttributeRAII A (" kind" , OS);
52
50
switch (VD->getKind ()) {
53
51
case swift::DeclKind::Class:
54
- serializeKind (" swift.class" , " Class" , OS);
55
- break ;
52
+ return {" swift.class" , " Class" };
56
53
case swift::DeclKind::Struct:
57
- serializeKind (" swift.struct" , " Structure" , OS);
58
- break ;
54
+ return {" swift.struct" , " Structure" };
59
55
case swift::DeclKind::Enum:
60
- serializeKind (" swift.enum" , " Enumeration" , OS);
61
- break ;
56
+ return {" swift.enum" , " Enumeration" };
62
57
case swift::DeclKind::EnumElement:
63
- serializeKind (" swift.enum.case" , " Case" , OS);
64
- break ;
58
+ return {" swift.enum.case" , " Case" };
65
59
case swift::DeclKind::Protocol:
66
- serializeKind (" swift.protocol" , " Protocol" , OS);
67
- break ;
60
+ return {" swift.protocol" , " Protocol" };
68
61
case swift::DeclKind::Constructor:
69
- serializeKind (" swift.init" , " Initializer" , OS);
70
- break ;
62
+ return {" swift.init" , " Initializer" };
71
63
case swift::DeclKind::Destructor:
72
- serializeKind (" swift.deinit" , " Deinitializer" , OS);
73
- break ;
64
+ return {" swift.deinit" , " Deinitializer" };
74
65
case swift::DeclKind::Func:
75
- if (VD->isOperator ()) {
76
- serializeKind (" swift.func.op" , " Operator" , OS);
77
- } else if (VD->isStatic ()) {
78
- serializeKind (" swift.type.method" , " Type Method" , OS);
79
- } else if (VD->getDeclContext ()->getSelfNominalTypeDecl ()){
80
- serializeKind (" swift.method" , " Instance Method" , OS);
81
- } else {
82
- serializeKind (" swift.func" , " Function" , OS);
83
- }
84
- break ;
66
+ if (VD->isOperator ())
67
+ return {" swift.func.op" , " Operator" };
68
+ if (VD->isStatic ())
69
+ return {" swift.type.method" , " Type Method" };
70
+ if (VD->getDeclContext ()->getSelfNominalTypeDecl ())
71
+ return {" swift.method" , " Instance Method" };
72
+ return {" swift.func" , " Function" };
85
73
case swift::DeclKind::Var:
86
- if (VD->isStatic ()) {
87
- serializeKind (" swift.type.property" , " Type Property" , OS);
88
- } else if (VD->getDeclContext ()->getSelfNominalTypeDecl ()) {
89
- serializeKind (" swift.property" , " Instance Property" , OS);
90
- } else {
91
- serializeKind (" swift.var" , " Global Variable" , OS);
92
- }
93
- break ;
74
+ if (VD->isStatic ())
75
+ return {" swift.type.property" , " Type Property" };
76
+ if (VD->getDeclContext ()->getSelfNominalTypeDecl ())
77
+ return {" swift.property" , " Instance Property" };
78
+ return {" swift.var" , " Global Variable" };
94
79
case swift::DeclKind::Subscript:
95
- if (VD->isStatic ()) {
96
- serializeKind (" swift.type.subscript" , " Type Subscript" , OS);
97
- } else {
98
- serializeKind (" swift.subscript" , " Instance Subscript" , OS);
99
- }
100
- break ;
80
+ if (VD->isStatic ())
81
+ return {" swift.type.subscript" , " Type Subscript" };
82
+ return {" swift.subscript" , " Instance Subscript" };
101
83
case swift::DeclKind::TypeAlias:
102
- serializeKind (" swift.typealias" , " Type Alias" , OS);
103
- break ;
84
+ return {" swift.typealias" , " Type Alias" };
104
85
case swift::DeclKind::AssociatedType:
105
- serializeKind (" swift.associatedtype" , " Associated Type" , OS);
106
- break ;
86
+ return {" swift.associatedtype" , " Associated Type" };
107
87
default :
108
88
llvm::errs () << " Unsupported kind: " << VD->getKindName (VD->getKind ());
109
89
llvm_unreachable (" Unsupported declaration kind for symbol graph" );
110
90
}
111
91
}
112
92
93
+ void Symbol::serializeKind (llvm::json::OStream &OS) const {
94
+ AttributeRAII A (" kind" , OS);
95
+ std::pair<StringRef, StringRef> IDAndName = getKind (VD);
96
+ serializeKind (IDAndName.first , IDAndName.second , OS);
97
+ }
98
+
113
99
void Symbol::serializeIdentifier (llvm::json::OStream &OS) const {
114
100
OS.attributeObject (" identifier" , [&](){
115
101
SmallString<256 > USR;
@@ -121,17 +107,17 @@ void Symbol::serializeIdentifier(llvm::json::OStream &OS) const {
121
107
122
108
void Symbol::serializePathComponents (llvm::json::OStream &OS) const {
123
109
OS.attributeArray (" pathComponents" , [&](){
124
- SmallVector<SmallString< 32 > , 8 > PathComponents;
110
+ SmallVector<PathComponent , 8 > PathComponents;
125
111
getPathComponents (PathComponents);
126
112
for (auto Component : PathComponents) {
127
- OS.value (Component);
113
+ OS.value (Component. Title );
128
114
}
129
115
});
130
116
}
131
117
132
118
void Symbol::serializeNames (llvm::json::OStream &OS) const {
133
119
OS.attributeObject (" names" , [&](){
134
- SmallVector<SmallString< 32 > , 8 > PathComponents;
120
+ SmallVector<PathComponent , 8 > PathComponents;
135
121
getPathComponents (PathComponents);
136
122
137
123
if (isa<GenericTypeDecl>(VD)) {
@@ -141,12 +127,12 @@ void Symbol::serializeNames(llvm::json::OStream &OS) const {
141
127
if (It != PathComponents.begin ()) {
142
128
FullyQualifiedTitle.push_back (' .' );
143
129
}
144
- FullyQualifiedTitle.append (*It );
130
+ FullyQualifiedTitle.append (It-> Title );
145
131
}
146
132
147
133
OS.attribute (" title" , FullyQualifiedTitle.str ());
148
134
} else {
149
- OS.attribute (" title" , PathComponents.back ());
135
+ OS.attribute (" title" , PathComponents.back (). Title );
150
136
}
151
137
152
138
Graph->serializeNavigatorDeclarationFragments (" navigator" , *this , OS);
@@ -481,16 +467,27 @@ void Symbol::serialize(llvm::json::OStream &OS) const {
481
467
}
482
468
483
469
void
484
- Symbol::getPathComponents (SmallVectorImpl<SmallString<32 >> &Components) const {
470
+ Symbol::getPathComponents (SmallVectorImpl<PathComponent> &Components) const {
471
+ // Note: this is also used for sourcekit's cursor-info request, so can be
472
+ // called on local symbols too. For such symbols, the path contains all parent
473
+ // decl contexts that are currently representable in the symbol graph,
474
+ // skipping over the rest (e.g. containing closures and accessors).
485
475
486
476
auto collectPathComponents = [&](const ValueDecl *Decl,
487
- SmallVectorImpl<SmallString<32 >> &DeclComponents) {
488
- // Collect the spellings of the fully qualified identifier components.
477
+ SmallVectorImpl<PathComponent> &DeclComponents) {
478
+ // Collect the spellings, kinds, and decls of the fully qualified identifier
479
+ // components.
489
480
while (Decl && !isa<ModuleDecl>(Decl)) {
490
481
SmallString<32 > Scratch;
491
482
Decl->getName ().getString (Scratch);
492
- DeclComponents.push_back (Scratch);
493
- if (const auto *DC = Decl->getDeclContext ()) {
483
+ if (supportsKind (Decl->getKind ()))
484
+ DeclComponents.push_back ({Scratch, getKind (Decl).first , Decl});
485
+
486
+ // Find the next parent.
487
+ auto *DC = Decl->getDeclContext ();
488
+ while (DC && DC->getContextKind () == DeclContextKind::AbstractClosureExpr)
489
+ DC = DC->getParent ();
490
+ if (DC) {
494
491
if (const auto *Nominal = DC->getSelfNominalTypeDecl ()) {
495
492
Decl = Nominal;
496
493
} else {
@@ -508,7 +505,8 @@ Symbol::getPathComponents(SmallVectorImpl<SmallString<32>> &Components) const {
508
505
// a protocol. Build a path as if it were defined in the base type.
509
506
SmallString<32 > LastPathComponent;
510
507
VD->getName ().getString (LastPathComponent);
511
- Components.push_back (LastPathComponent);
508
+ if (supportsKind (VD->getKind ()))
509
+ Components.push_back ({LastPathComponent, getKind (VD).first , VD});
512
510
collectPathComponents (BaseTypeDecl, Components);
513
511
} else {
514
512
// Otherwise, this is just a normal declaration, so we can build
@@ -521,13 +519,13 @@ Symbol::getPathComponents(SmallVectorImpl<SmallString<32>> &Components) const {
521
519
}
522
520
523
521
void Symbol::printPath (llvm::raw_ostream &OS) const {
524
- SmallVector<SmallString< 32 > , 8 > Components;
522
+ SmallVector<PathComponent , 8 > Components;
525
523
getPathComponents (Components);
526
524
for (auto it = Components.begin (); it != Components.end (); ++it) {
527
525
if (it != Components.begin ()) {
528
526
OS << ' .' ;
529
527
}
530
- OS << it->str ();
528
+ OS << it->Title . str ();
531
529
}
532
530
}
533
531
0 commit comments