Skip to content

Commit 7f582c4

Browse files
author
Nathan Hawes
committed
[indexer] When visiting a NominalTypeDecl, cover the type references in the where clause and generic param inheritance too.
Also rename ASTWalker::shouldWalkIntoFunctionGenericParams() to shouldWalkIntoGenericParams() since it's now used when walking NominalTypeDecl (not just AbstractFunctionDecl).
1 parent 62e825a commit 7f582c4

File tree

10 files changed

+264
-10
lines changed

10 files changed

+264
-10
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/IndexSymbol.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ struct IndexSymbol {
111111

112112
SymbolKind getSymbolKindForDecl(const Decl *D);
113113

114+
StringRef getSymbolKindString(SymbolKind K);
115+
116+
void applyForEachSymbolSubKind(SymbolSubKindSet SubKinds,
117+
llvm::function_ref<void(SymbolSubKind)> Fn);
118+
void printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS);
119+
114120
} // end namespace index
115121
} // end namespace swift
116122

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,

lib/Index/Index.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
316316
bool initFuncDeclIndexSymbol(ValueDecl *D, IndexSymbol &Info);
317317
bool initCallRefIndexSymbol(Expr *CurrentE, Expr *ParentE, ValueDecl *D,
318318
SourceLoc Loc, IndexSymbol &Info);
319+
bool initVarRefIndexSymbols(Expr *CurrentE, ValueDecl *D, SourceLoc Loc,
320+
IndexSymbol &Info);
319321

320322
std::pair<unsigned, unsigned> getLineCol(SourceLoc Loc) {
321323
if (Loc.isInvalid())
@@ -522,6 +524,12 @@ bool IndexSwiftASTWalker::startEntityRef(ValueDecl *D, SourceLoc Loc) {
522524

523525
return startEntity(D, Info);
524526

527+
} else if (isa<AbstractStorageDecl>(D)) {
528+
IndexSymbol Info;
529+
if (initVarRefIndexSymbols(getCurrentExpr(), D, Loc, Info))
530+
return false;
531+
532+
return startEntity(D, Info);
525533
} else {
526534
IndexSymbol Info;
527535
if (initIndexSymbol(D, Loc, /*IsRef=*/true, Info))
@@ -955,6 +963,29 @@ bool IndexSwiftASTWalker::initCallRefIndexSymbol(Expr *CurrentE, Expr *ParentE,
955963
return false;
956964
}
957965

966+
bool IndexSwiftASTWalker::initVarRefIndexSymbols(Expr *CurrentE, ValueDecl *D, SourceLoc Loc, IndexSymbol &Info) {
967+
968+
if (!(CurrentE->getReferencedDecl() == D))
969+
return true;
970+
971+
if (initIndexSymbol(D, Loc, /*IsRef=*/true, Info))
972+
return true;
973+
974+
AccessKind Kind = CurrentE->hasLValueAccessKind() ? CurrentE->getLValueAccessKind() : AccessKind::Read;
975+
switch (Kind) {
976+
case swift::AccessKind::Read:
977+
Info.roles |= (unsigned)SymbolRole::Read;
978+
break;
979+
case swift::AccessKind::ReadWrite:
980+
Info.roles |= (unsigned)SymbolRole::Read;
981+
LLVM_FALLTHROUGH;
982+
case swift::AccessKind::Write:
983+
Info.roles |= (unsigned)SymbolRole::Write;
984+
}
985+
986+
return false;
987+
}
988+
958989
llvm::hash_code
959990
IndexSwiftASTWalker::hashFileReference(llvm::hash_code code,
960991
SourceFileOrModule SFOrMod) {

lib/Index/IndexSymbol.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,81 @@ SymbolKind index::getSymbolKindForDecl(const Decl *D) {
8383
return SymbolKind::Unknown;
8484
}
8585
}
86+
87+
StringRef index::getSymbolKindString(SymbolKind K) {
88+
switch (K) {
89+
case SymbolKind::Unknown: return "<unknown>";
90+
case SymbolKind::Module: return "module";
91+
case SymbolKind::ClangModule: return "clang-module";
92+
case SymbolKind::Enum: return "enum";
93+
case SymbolKind::EnumElement: return "enum-element";
94+
case SymbolKind::Struct: return "struct";
95+
case SymbolKind::Class: return "class";
96+
case SymbolKind::Protocol: return "protocol";
97+
case SymbolKind::Extension: return "extension";
98+
case SymbolKind::TypeAlias: return "type-alias";
99+
case SymbolKind::Function: return "function";
100+
case SymbolKind::Variable: return "variable";
101+
case SymbolKind::InstanceMethod: return "instance-method";
102+
case SymbolKind::ClassMethod: return "class-method";
103+
case SymbolKind::StaticMethod: return "static-method";
104+
case SymbolKind::InstanceProperty: return "instance-property";
105+
case SymbolKind::ClassProperty: return "class-property";
106+
case SymbolKind::StaticProperty: return "static-property";
107+
case SymbolKind::Constructor: return "constructor";
108+
case SymbolKind::Destructor: return "destructor";
109+
case SymbolKind::PrefixOperator: return "prefix-operator";
110+
case SymbolKind::PostfixOperator: return "postfix-operator";
111+
case SymbolKind::InfixOperator: return "infix-operator";
112+
case SymbolKind::Accessor: return "accessor";
113+
case SymbolKind::Subscript: return "subscript";
114+
case SymbolKind::AssociatedType: return "associated-type";
115+
case SymbolKind::GenericTypeParam: return "generic-type-param";
116+
}
117+
llvm_unreachable("invalid symbol kind");
118+
}
119+
120+
void index::applyForEachSymbolSubKind(SymbolSubKindSet SubKinds,
121+
llvm::function_ref<void(SymbolSubKind)> Fn) {
122+
#define APPLY_FOR_SUBKIND(K) \
123+
if (SubKinds & (unsigned)SymbolSubKind::K) \
124+
Fn(SymbolSubKind::K)
125+
126+
APPLY_FOR_SUBKIND(AccessorGetter);
127+
APPLY_FOR_SUBKIND(AccessorSetter);
128+
APPLY_FOR_SUBKIND(AccessorWillSet);
129+
APPLY_FOR_SUBKIND(AccessorDidSet);
130+
APPLY_FOR_SUBKIND(AccessorAddressor);
131+
APPLY_FOR_SUBKIND(AccessorMutableAddressor);
132+
APPLY_FOR_SUBKIND(ExtensionOfStruct);
133+
APPLY_FOR_SUBKIND(ExtensionOfClass);
134+
APPLY_FOR_SUBKIND(ExtensionOfEnum);
135+
APPLY_FOR_SUBKIND(ExtensionOfProtocol);
136+
APPLY_FOR_SUBKIND(UnitTest);
137+
138+
#undef APPLY_FOR_SUBKIND
139+
}
140+
141+
void index::printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS) {
142+
bool VisitedOnce = false;
143+
applyForEachSymbolSubKind(SubKinds, [&](SymbolSubKind SubKind) {
144+
if (VisitedOnce)
145+
OS << ',';
146+
else
147+
VisitedOnce = true;
148+
switch (SubKind) {
149+
case SymbolSubKind::None: OS << "none"; break;
150+
case SymbolSubKind::UnitTest: OS << "test"; break;
151+
case SymbolSubKind::AccessorGetter: OS << "get"; break;
152+
case SymbolSubKind::AccessorSetter: OS << "set"; break;
153+
case SymbolSubKind::AccessorWillSet: OS << "willSet"; break;
154+
case SymbolSubKind::AccessorDidSet: OS << "didSet"; break;
155+
case SymbolSubKind::AccessorAddressor: OS << "addr"; break;
156+
case SymbolSubKind::AccessorMutableAddressor: OS << "mutAddr"; break;
157+
case SymbolSubKind::ExtensionOfStruct: OS << "extStruct"; break;
158+
case SymbolSubKind::ExtensionOfClass: OS << "extClass"; break;
159+
case SymbolSubKind::ExtensionOfEnum: OS << "extEnum"; break;
160+
case SymbolSubKind::ExtensionOfProtocol: OS << "extProt"; break;
161+
}
162+
});
163+
}

tools/swift-ide-test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_swift_host_tool(swift-ide-test
55
LINK_LIBRARIES
66
swiftDriver
77
swiftFrontend
8+
swiftIndex
89
swiftIDE
910
LLVM_COMPONENT_DEPENDS
1011
DebugInfoCodeView

0 commit comments

Comments
 (0)