Skip to content

Commit 563bc04

Browse files
author
Nathan Hawes
authored
Merge pull request #6677 from nathawes/swift-indexing
Add roles and relations for Swift indexing
2 parents 1115c25 + 32ba5f2 commit 563bc04

File tree

14 files changed

+924
-185
lines changed

14 files changed

+924
-185
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ class ASTWalker {
190190
virtual bool walkToTypeReprPost(TypeRepr *T) { return true; }
191191

192192
/// This method configures whether the walker should explore into the generic
193-
/// params in an AbstractFunctionDecl.
194-
virtual bool shouldWalkIntoFunctionGenericParams() { return false; }
193+
/// params in AbstractFunctionDecl and NominalTypeDecl.
194+
virtual bool shouldWalkIntoGenericParams() { return false; }
195195

196196
/// walkToParameterListPre - This method is called when first visiting a
197197
/// ParameterList, before walking into its parameters. If it returns false,

include/swift/AST/SourceEntityWalker.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class SourceEntityWalker {
126126
/// Whether walk into the inactive region in a #if config statement.
127127
virtual bool shouldWalkInactiveConfigRegion() { return false; }
128128

129-
virtual bool shouldWalkIntoFunctionGenericParams() { return true; }
129+
virtual bool shouldWalkIntoGenericParams() { return true; }
130130

131131
protected:
132132
SourceEntityWalker() = default;

include/swift/Index/IndexDataConsumer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class IndexDataConsumer {
2222
virtual void anchor();
2323

2424
public:
25+
enum Action {Skip, Abort, Continue};
26+
2527
virtual ~IndexDataConsumer() {}
2628

2729
virtual void failed(StringRef error) = 0;
@@ -32,8 +34,7 @@ class IndexDataConsumer {
3234
virtual bool startDependency(SymbolKind kind, StringRef name, StringRef path,
3335
bool isSystem, StringRef hash) = 0;
3436
virtual bool finishDependency(SymbolKind kind) = 0;
35-
virtual bool startSourceEntity(const IndexSymbol &symbol) = 0;
36-
virtual bool recordRelatedEntity(const IndexSymbol &symbol) = 0;
37+
virtual Action startSourceEntity(const IndexSymbol &symbol) = 0;
3738
virtual bool finishSourceEntity(SymbolKind kind, SymbolSubKindSet subKinds,
3839
SymbolRoleSet roles) = 0;
3940

include/swift/Index/IndexSymbol.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,48 @@ inline SymbolSubKindSet &operator|=(SymbolSubKindSet &SKSet, SymbolSubKind SK) {
9292
using SymbolRole = clang::index::SymbolRole;
9393
using SymbolRoleSet = clang::index::SymbolRoleSet;
9494

95-
struct IndexSymbol {
95+
struct IndexRelation {
9696
const ValueDecl *decl;
9797
SymbolKind kind;
9898
SymbolSubKindSet subKinds = SymbolSubKindSet(0);
9999
SymbolRoleSet roles = SymbolRoleSet(0);
100+
100101
// The following strings are guaranteed to live at least as long as the
101102
// current indexing action.
102103
StringRef name;
103104
StringRef USR; // USR may be safely compared by pointer.
104105
StringRef group;
105-
StringRef receiverUSR;
106+
107+
IndexRelation(SymbolRoleSet Roles, const ValueDecl *Sym, SymbolKind Kind, SymbolSubKindSet SubKinds, StringRef Name, StringRef USR)
108+
: decl(Sym), kind(Kind), subKinds(SubKinds), roles(Roles), name(Name), USR(USR) {}
109+
110+
IndexRelation() = default;
111+
};
112+
113+
struct IndexSymbol : IndexRelation {
114+
SmallVector<IndexRelation, 3> Relations;
106115
unsigned line = 0;
107116
unsigned column = 0;
108117

109118
IndexSymbol() = default;
119+
120+
StringRef getReceiverUSR() const {
121+
for(auto Relation: Relations) {
122+
if (Relation.roles & (SymbolRoleSet) SymbolRole::RelationReceivedBy)
123+
return Relation.USR;
124+
}
125+
return StringRef();
126+
}
110127
};
111128

112129
SymbolKind getSymbolKindForDecl(const Decl *D);
113130

131+
StringRef getSymbolKindString(SymbolKind K);
132+
133+
void applyForEachSymbolSubKind(SymbolSubKindSet SubKinds,
134+
llvm::function_ref<void(SymbolSubKind)> Fn);
135+
void printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS);
136+
114137
} // end namespace index
115138
} // end namespace swift
116139

lib/AST/ASTWalker.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,29 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
201201
}
202202

203203
bool visitNominalTypeDecl(NominalTypeDecl *NTD) {
204-
if (auto GPS = NTD->getGenericParams()) {
205-
for (auto GP : GPS->getParams()) {
204+
if (NTD->getGenericParams() &&
205+
Walker.shouldWalkIntoGenericParams()) {
206+
// Visit generic params
207+
for (auto GP : NTD->getGenericParams()->getParams()) {
206208
if (doIt(GP))
207209
return true;
210+
for(auto Inherit: GP->getInherited()) {
211+
if (doIt(Inherit))
212+
return true;
213+
}
214+
}
215+
// Visit param conformance
216+
for (auto &Req : NTD->getGenericParams()->getRequirements()) {
217+
switch (Req.getKind()) {
218+
case RequirementReprKind::SameType:
219+
if (doIt(Req.getFirstTypeLoc()) || doIt(Req.getSecondTypeLoc()))
220+
return true;
221+
break;
222+
case RequirementReprKind::TypeConstraint:
223+
if (doIt(Req.getSubjectLoc()) || doIt(Req.getConstraintLoc()))
224+
return true;
225+
break;
226+
}
208227
}
209228
}
210229

@@ -237,7 +256,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
237256
PrettyStackTraceDecl debugStack("walking into body of", AFD);
238257
#endif
239258
if (AFD->getGenericParams() &&
240-
Walker.shouldWalkIntoFunctionGenericParams()) {
259+
Walker.shouldWalkIntoGenericParams()) {
241260

242261
// Visit generic params
243262
for (auto &P : AFD->getGenericParams()->getParams()) {

lib/AST/SourceEntityWalker.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class SemaAnnotator : public ASTWalker {
4040
bool isDone() const { return Cancelled; }
4141

4242
private:
43-
bool shouldWalkIntoFunctionGenericParams() override {
44-
return SEWalker.shouldWalkIntoFunctionGenericParams();
43+
bool shouldWalkIntoGenericParams() override {
44+
return SEWalker.shouldWalkIntoGenericParams();
4545
}
4646
bool walkToDeclPre(Decl *D) override;
4747
std::pair<bool, Expr *> walkToExprPre(Expr *E) override;

lib/IDE/SyntaxModel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ class ModelASTWalker : public ASTWalker {
297297
bool walkToDeclPost(Decl *D) override;
298298
bool walkToTypeReprPre(TypeRepr *T) override;
299299
std::pair<bool, Pattern*> walkToPatternPre(Pattern *P) override;
300-
bool shouldWalkIntoFunctionGenericParams() override { return true; }
300+
bool shouldWalkIntoGenericParams() override { return true; }
301301

302302
private:
303303
static bool findUrlStartingLoc(StringRef Text, unsigned &Start,

0 commit comments

Comments
 (0)