SourceEntityWalker: Add a parameter to visitDeclReference() describing the kind of the reference under visit. NFC

This commit is contained in:
Xi Ge
2016-12-20 14:29:12 -08:00
parent dbefa60605
commit 14f968a5ed
10 changed files with 66 additions and 33 deletions

View File

@@ -27,6 +27,16 @@ class TypeRepr;
struct TypeLoc;
class ParameterList;
enum class SemaReferenceKind : uint8_t {
ModuleRef = 0,
DeclRef,
DeclMemberRef,
DeclConstructorRef,
TypeRef,
EnumElementRef,
SubscriptRef,
};
/// \brief An abstract class used to traverse an AST.
class ASTWalker {
public:

View File

@@ -13,6 +13,7 @@
#ifndef SWIFT_AST_SOURCE_ENTITY_WALKER_H
#define SWIFT_AST_SOURCE_ENTITY_WALKER_H
#include "swift/AST/ASTWalker.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/SourceLoc.h"
#include "llvm/ADT/PointerUnion.h"
@@ -86,7 +87,8 @@ public:
/// \c ConstructorDecl, to point to the type declaration that the source
/// refers to.
virtual bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T);
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind);
/// This method is called when a ValueDecl for a subscript is referenced in
/// source. If it returns false, the remaining traversal is terminated

View File

@@ -181,7 +181,8 @@ private:
bool walkToStmtPre(Stmt *S) override;
bool walkToStmtPost(Stmt *S) override;
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) override;
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) override;
bool visitCallArgName(Identifier Name, CharSourceRange Range,
ValueDecl *D) override;
bool visitModuleReference(ModuleEntity Mod, CharSourceRange Range) override;
@@ -239,7 +240,8 @@ class RangeResolver : public SourceEntityWalker {
bool walkToDeclPre(Decl *D, CharSourceRange Range) override;
bool walkToDeclPost(Decl *D) override;
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) override;
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) override;
public:
RangeResolver(SourceFile &File, SourceLoc Start, SourceLoc End);
RangeResolver(SourceFile &File, unsigned Offset, unsigned Length);

View File

@@ -60,7 +60,7 @@ private:
bool passModulePathElements(ArrayRef<ImportDecl::AccessPathElement> Path,
const clang::Module *ClangMod);
bool passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc);
bool passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc, SemaReferenceKind Kind);
bool passReference(ModuleEntity Mod, std::pair<Identifier, SourceLoc> IdLoc);
bool passSubscriptReference(ValueDecl *D, SourceLoc Loc, bool IsOpenBracket);
@@ -211,7 +211,8 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
std::make_pair(module->getName(), E->getLoc())))
return { false, nullptr };
} else if (!passReference(DRE->getDecl(), DRE->getType(),
DRE->getNameLoc())) {
DRE->getNameLoc(),
SemaReferenceKind::DeclRef)) {
return { false, nullptr };
}
} else if (MemberRefExpr *MRE = dyn_cast<MemberRefExpr>(E)) {
@@ -219,7 +220,7 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
if (!MRE->getBase()->walk(*this))
return { false, nullptr };
if (!passReference(MRE->getMember().getDecl(), MRE->getType(),
MRE->getNameLoc()))
MRE->getNameLoc(), SemaReferenceKind::DeclMemberRef))
return { false, nullptr };
// We already visited the children.
@@ -229,7 +230,8 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
} else if (auto OtherCtorE = dyn_cast<OtherConstructorDeclRefExpr>(E)) {
if (!passReference(OtherCtorE->getDecl(), OtherCtorE->getType(),
OtherCtorE->getConstructorLoc()))
OtherCtorE->getConstructorLoc(),
SemaReferenceKind::DeclConstructorRef))
return { false, nullptr };
} else if (SubscriptExpr *SE = dyn_cast<SubscriptExpr>(E)) {
@@ -293,7 +295,8 @@ bool SemaAnnotator::walkToTypeReprPre(TypeRepr *T) {
return passReference(ModD, std::make_pair(IdT->getIdentifier(),
IdT->getIdLoc()));
return passReference(VD, Type(), DeclNameLoc(IdT->getIdLoc()));
return passReference(VD, Type(), DeclNameLoc(IdT->getIdLoc()),
SemaReferenceKind::TypeRef);
}
}
return true;
@@ -323,7 +326,8 @@ std::pair<bool, Pattern *> SemaAnnotator::walkToPatternPre(Pattern *P) {
if (!Element)
return { true, P };
Type T = EP->hasType() ? EP->getType() : Type();
return { passReference(Element, T, DeclNameLoc(EP->getLoc())), P };
return { passReference(Element, T, DeclNameLoc(EP->getLoc()),
SemaReferenceKind::EnumElementRef), P };
}
auto *TP = dyn_cast<TypedPattern>(P);
@@ -353,7 +357,8 @@ bool SemaAnnotator::handleImports(ImportDecl *Import) {
auto Decls = Import->getDecls();
if (Decls.size() == 1) {
// FIXME: ImportDecl should store a DeclNameLoc.
if (!passReference(Decls.front(), Type(), DeclNameLoc(Import->getEndLoc())))
if (!passReference(Decls.front(), Type(), DeclNameLoc(Import->getEndLoc()),
SemaReferenceKind::DeclRef))
return false;
}
@@ -385,7 +390,8 @@ bool SemaAnnotator::passSubscriptReference(ValueDecl *D, SourceLoc Loc,
return Continue;
}
bool SemaAnnotator::passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc) {
bool SemaAnnotator::
passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc, SemaReferenceKind Kind) {
TypeDecl *CtorTyRef = nullptr;
if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
@@ -401,7 +407,7 @@ bool SemaAnnotator::passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc) {
CharSourceRange Range =
Lexer::getCharSourceRangeFromSourceRange(D->getASTContext().SourceMgr,
Loc.getSourceRange());
bool Continue = SEWalker.visitDeclReference(D, Range, CtorTyRef, Ty);
bool Continue = SEWalker.visitDeclReference(D, Range, CtorTyRef, Ty, Kind);
if (!Continue)
Cancelled = true;
return Continue;
@@ -475,7 +481,8 @@ bool SourceEntityWalker::walk(DeclContext *DC) {
}
bool SourceEntityWalker::visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) {
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) {
return true;
}
@@ -485,7 +492,8 @@ bool SourceEntityWalker::visitSubscriptReference(ValueDecl *D,
// Most of the clients treat subscript reference the same way as a
// regular reference when called on the open bracket and
// ignore the closing one.
return IsOpenBracket ? visitDeclReference(D, Range, nullptr, Type()) : true;
return IsOpenBracket ? visitDeclReference(D, Range, nullptr, Type(),
SemaReferenceKind::SubscriptRef) : true;
}
bool SourceEntityWalker::visitCallArgName(Identifier Name,

View File

@@ -93,7 +93,8 @@ bool SemaLocResolver::tryResolve(ModuleEntity Mod, SourceLoc Loc) {
bool SemaLocResolver::visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
bool IsOpenBracket) {
// We should treat both open and close brackets equally
return visitDeclReference(D, Range, nullptr, Type());
return visitDeclReference(D, Range, nullptr, Type(),
SemaReferenceKind::SubscriptRef);
}
SemaToken SemaLocResolver::resolve(SourceLoc Loc) {
@@ -146,7 +147,8 @@ bool SemaLocResolver::walkToStmtPost(Stmt *S) {
}
bool SemaLocResolver::visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) {
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) {
if (isDone())
return false;
return !tryResolve(D, CtorTyRef, Range.getStart(), /*IsRef=*/true, T);
@@ -506,7 +508,7 @@ bool RangeResolver::walkToDeclPost(Decl *D) {
bool RangeResolver::
visitDeclReference(ValueDecl *D, CharSourceRange Range, TypeDecl *CtorTyRef,
Type T) {
Type T, SemaReferenceKind Kind) {
Impl->analyzeDeclRef(D, Range, T);
return true;
}

View File

@@ -247,7 +247,8 @@ private:
}
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) override {
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) override {
SourceLoc Loc = Range.getStart();
if (CtorTyRef)
if (!reportRef(CtorTyRef, Loc))

View File

@@ -977,7 +977,8 @@ public:
}
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type Ty) override {
TypeDecl *CtorTyRef, Type Ty,
SemaReferenceKind Kind) override {
unsigned StartOffset = getOffset(Range.getStart());
References.emplace_back(D, StartOffset, Range.getByteLength(), Ty);
return true;
@@ -986,7 +987,8 @@ public:
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
bool IsOpenBracket) override {
// Treat both open and close brackets equally
return visitDeclReference(D, Range, nullptr, Type());
return visitDeclReference(D, Range, nullptr, Type(),
SemaReferenceKind::SubscriptRef);
}
bool isLocal(Decl *D) const {

View File

@@ -771,7 +771,8 @@ public:
: SM(SM), BufferID(BufferID) {}
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) override {
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) override {
if (isa<VarDecl>(D) && D->hasName() && D->getName().str() == "self")
return true;
@@ -788,7 +789,8 @@ public:
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
bool IsOpenBracket) override {
// We should treat both open and close brackets equally
return visitDeclReference(D, Range, nullptr, Type());
return visitDeclReference(D, Range, nullptr, Type(),
SemaReferenceKind::SubscriptRef);
}
void annotate(const Decl *D, bool IsRef, CharSourceRange Range) {

View File

@@ -1367,7 +1367,8 @@ private:
return true;
}
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) override {
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) override {
if (Cancelled)
return false;
if (CtorTyRef)

View File

@@ -1147,14 +1147,16 @@ private:
}
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type Ty) override {
TypeDecl *CtorTyRef, Type Ty,
SemaReferenceKind Kind) override {
annotateSourceEntity({ Range, D, CtorTyRef, /*IsRef=*/true });
return true;
}
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
bool IsOpenBracket) override {
return visitDeclReference(D, Range, nullptr, Type());
return visitDeclReference(D, Range, nullptr, Type(),
SemaReferenceKind::SubscriptRef);
}
bool visitCallArgName(Identifier Name, CharSourceRange Range,
@@ -2545,7 +2547,8 @@ public:
}
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
TypeDecl *CtorTyRef, Type T) override {
TypeDecl *CtorTyRef, Type T,
SemaReferenceKind Kind) override {
if (SeenDecls.insert(D).second)
tryDemangleDecl(D, Range, /*isRef=*/true);