Skip to content

Swift indexing #6677

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/ASTWalker.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ class ASTWalker {
virtual bool walkToTypeReprPost(TypeRepr *T) { return true; }

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

/// walkToParameterListPre - This method is called when first visiting a
/// ParameterList, before walking into its parameters. If it returns false,
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/SourceEntityWalker.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class SourceEntityWalker {
/// Whether walk into the inactive region in a #if config statement.
virtual bool shouldWalkInactiveConfigRegion() { return false; }

virtual bool shouldWalkIntoFunctionGenericParams() { return true; }
virtual bool shouldWalkIntoGenericParams() { return true; }

protected:
SourceEntityWalker() = default;
Expand Down
5 changes: 3 additions & 2 deletions include/swift/Index/IndexDataConsumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class IndexDataConsumer {
virtual void anchor();

public:
enum Action {Skip, Abort, Continue};

virtual ~IndexDataConsumer() {}

virtual void failed(StringRef error) = 0;
Expand All @@ -32,8 +34,7 @@ class IndexDataConsumer {
virtual bool startDependency(SymbolKind kind, StringRef name, StringRef path,
bool isSystem, StringRef hash) = 0;
virtual bool finishDependency(SymbolKind kind) = 0;
virtual bool startSourceEntity(const IndexSymbol &symbol) = 0;
virtual bool recordRelatedEntity(const IndexSymbol &symbol) = 0;
virtual Action startSourceEntity(const IndexSymbol &symbol) = 0;
virtual bool finishSourceEntity(SymbolKind kind, SymbolSubKindSet subKinds,
SymbolRoleSet roles) = 0;

Expand Down
27 changes: 25 additions & 2 deletions include/swift/Index/IndexSymbol.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,48 @@ inline SymbolSubKindSet &operator|=(SymbolSubKindSet &SKSet, SymbolSubKind SK) {
using SymbolRole = clang::index::SymbolRole;
using SymbolRoleSet = clang::index::SymbolRoleSet;

struct IndexSymbol {
struct IndexRelation {
const ValueDecl *decl;
SymbolKind kind;
SymbolSubKindSet subKinds = SymbolSubKindSet(0);
SymbolRoleSet roles = SymbolRoleSet(0);

// The following strings are guaranteed to live at least as long as the
// current indexing action.
StringRef name;
StringRef USR; // USR may be safely compared by pointer.
StringRef group;
StringRef receiverUSR;

IndexRelation(SymbolRoleSet Roles, const ValueDecl *Sym, SymbolKind Kind, SymbolSubKindSet SubKinds, StringRef Name, StringRef USR)
: decl(Sym), kind(Kind), subKinds(SubKinds), roles(Roles), name(Name), USR(USR) {}

IndexRelation() = default;
};

struct IndexSymbol : IndexRelation {
SmallVector<IndexRelation, 3> Relations;
unsigned line = 0;
unsigned column = 0;

IndexSymbol() = default;

StringRef getReceiverUSR() const {
for(auto Relation: Relations) {
if (Relation.roles & (SymbolRoleSet) SymbolRole::RelationReceivedBy)
return Relation.USR;
}
return StringRef();
}
};

SymbolKind getSymbolKindForDecl(const Decl *D);

StringRef getSymbolKindString(SymbolKind K);

void applyForEachSymbolSubKind(SymbolSubKindSet SubKinds,
llvm::function_ref<void(SymbolSubKind)> Fn);
void printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS);

} // end namespace index
} // end namespace swift

Expand Down
25 changes: 22 additions & 3 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,29 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
}

bool visitNominalTypeDecl(NominalTypeDecl *NTD) {
if (auto GPS = NTD->getGenericParams()) {
for (auto GP : GPS->getParams()) {
if (NTD->getGenericParams() &&
Walker.shouldWalkIntoGenericParams()) {
// Visit generic params
for (auto GP : NTD->getGenericParams()->getParams()) {
if (doIt(GP))
return true;
for(auto Inherit: GP->getInherited()) {
if (doIt(Inherit))
return true;
}
}
// Visit param conformance
for (auto &Req : NTD->getGenericParams()->getRequirements()) {
switch (Req.getKind()) {
case RequirementReprKind::SameType:
if (doIt(Req.getFirstTypeLoc()) || doIt(Req.getSecondTypeLoc()))
return true;
break;
case RequirementReprKind::TypeConstraint:
if (doIt(Req.getSubjectLoc()) || doIt(Req.getConstraintLoc()))
return true;
break;
}
}
}

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

// Visit generic params
for (auto &P : AFD->getGenericParams()->getParams()) {
Expand Down
4 changes: 2 additions & 2 deletions lib/AST/SourceEntityWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class SemaAnnotator : public ASTWalker {
bool isDone() const { return Cancelled; }

private:
bool shouldWalkIntoFunctionGenericParams() override {
return SEWalker.shouldWalkIntoFunctionGenericParams();
bool shouldWalkIntoGenericParams() override {
return SEWalker.shouldWalkIntoGenericParams();
}
bool walkToDeclPre(Decl *D) override;
std::pair<bool, Expr *> walkToExprPre(Expr *E) override;
Expand Down
2 changes: 1 addition & 1 deletion lib/IDE/SyntaxModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class ModelASTWalker : public ASTWalker {
bool walkToDeclPost(Decl *D) override;
bool walkToTypeReprPre(TypeRepr *T) override;
std::pair<bool, Pattern*> walkToPatternPre(Pattern *P) override;
bool shouldWalkIntoFunctionGenericParams() override { return true; }
bool shouldWalkIntoGenericParams() override { return true; }

private:
static bool findUrlStartingLoc(StringRef Text, unsigned &Start,
Expand Down
Loading