Skip to content

Commit e264d02

Browse files
authored
Merge pull request #6420 from nkcsgexi/reference-kind
2 parents 20a023b + 14f968a commit e264d02

File tree

10 files changed

+66
-33
lines changed

10 files changed

+66
-33
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ class TypeRepr;
2727
struct TypeLoc;
2828
class ParameterList;
2929

30+
enum class SemaReferenceKind : uint8_t {
31+
ModuleRef = 0,
32+
DeclRef,
33+
DeclMemberRef,
34+
DeclConstructorRef,
35+
TypeRef,
36+
EnumElementRef,
37+
SubscriptRef,
38+
};
39+
3040
/// \brief An abstract class used to traverse an AST.
3141
class ASTWalker {
3242
public:
@@ -188,18 +198,18 @@ class ASTWalker {
188198
/// the subtree is skipped.
189199
///
190200
virtual bool walkToParameterListPre(ParameterList *PL) { return true; }
191-
201+
192202
/// walkToParameterListPost - This method is called after visiting the
193203
/// children of a parameter list. If it returns false, the remaining
194204
/// traversal is terminated and returns failure.
195205
virtual bool walkToParameterListPost(ParameterList *PL) { return true; }
196-
197-
206+
207+
198208
protected:
199209
ASTWalker() = default;
200210
ASTWalker(const ASTWalker &) = default;
201211
virtual ~ASTWalker() = default;
202-
212+
203213
virtual void anchor();
204214
};
205215

include/swift/AST/SourceEntityWalker.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_SOURCE_ENTITY_WALKER_H
1414
#define SWIFT_AST_SOURCE_ENTITY_WALKER_H
1515

16+
#include "swift/AST/ASTWalker.h"
1617
#include "swift/Basic/LLVM.h"
1718
#include "swift/Basic/SourceLoc.h"
1819
#include "llvm/ADT/PointerUnion.h"
@@ -86,7 +87,8 @@ class SourceEntityWalker {
8687
/// \c ConstructorDecl, to point to the type declaration that the source
8788
/// refers to.
8889
virtual bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
89-
TypeDecl *CtorTyRef, Type T);
90+
TypeDecl *CtorTyRef, Type T,
91+
SemaReferenceKind Kind);
9092

9193
/// This method is called when a ValueDecl for a subscript is referenced in
9294
/// source. If it returns false, the remaining traversal is terminated

include/swift/IDE/Utils.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct SourceCompleteResult {
5959
// prefix will contain the leading space characters of the line that
6060
// contained the '{', '(' or '[' character that was unbalanced.
6161
std::string IndentPrefix;
62-
// Returns the indent level as an indentation count (number of indentations
62+
// Returns the indent level as an indentation count (number of indentations
6363
// to apply). Clients can translate this into the standard indentation that
6464
// is being used by the IDE (3 spaces? 1 tab?) and should use the indent
6565
// prefix string followed by the correct indentation.
@@ -181,7 +181,8 @@ class SemaLocResolver : public SourceEntityWalker {
181181
bool walkToStmtPre(Stmt *S) override;
182182
bool walkToStmtPost(Stmt *S) override;
183183
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
184-
TypeDecl *CtorTyRef, Type T) override;
184+
TypeDecl *CtorTyRef, Type T,
185+
SemaReferenceKind Kind) override;
185186
bool visitCallArgName(Identifier Name, CharSourceRange Range,
186187
ValueDecl *D) override;
187188
bool visitModuleReference(ModuleEntity Mod, CharSourceRange Range) override;
@@ -239,7 +240,8 @@ class RangeResolver : public SourceEntityWalker {
239240
bool walkToDeclPre(Decl *D, CharSourceRange Range) override;
240241
bool walkToDeclPost(Decl *D) override;
241242
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
242-
TypeDecl *CtorTyRef, Type T) override;
243+
TypeDecl *CtorTyRef, Type T,
244+
SemaReferenceKind Kind) override;
243245
public:
244246
RangeResolver(SourceFile &File, SourceLoc Start, SourceLoc End);
245247
RangeResolver(SourceFile &File, unsigned Offset, unsigned Length);

lib/AST/SourceEntityWalker.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SemaAnnotator : public ASTWalker {
6060
bool passModulePathElements(ArrayRef<ImportDecl::AccessPathElement> Path,
6161
const clang::Module *ClangMod);
6262

63-
bool passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc);
63+
bool passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc, SemaReferenceKind Kind);
6464
bool passReference(ModuleEntity Mod, std::pair<Identifier, SourceLoc> IdLoc);
6565

6666
bool passSubscriptReference(ValueDecl *D, SourceLoc Loc, bool IsOpenBracket);
@@ -211,15 +211,16 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
211211
std::make_pair(module->getName(), E->getLoc())))
212212
return { false, nullptr };
213213
} else if (!passReference(DRE->getDecl(), DRE->getType(),
214-
DRE->getNameLoc())) {
214+
DRE->getNameLoc(),
215+
SemaReferenceKind::DeclRef)) {
215216
return { false, nullptr };
216217
}
217218
} else if (MemberRefExpr *MRE = dyn_cast<MemberRefExpr>(E)) {
218219
// Visit in source order.
219220
if (!MRE->getBase()->walk(*this))
220221
return { false, nullptr };
221222
if (!passReference(MRE->getMember().getDecl(), MRE->getType(),
222-
MRE->getNameLoc()))
223+
MRE->getNameLoc(), SemaReferenceKind::DeclMemberRef))
223224
return { false, nullptr };
224225

225226
// We already visited the children.
@@ -229,7 +230,8 @@ std::pair<bool, Expr *> SemaAnnotator::walkToExprPre(Expr *E) {
229230

230231
} else if (auto OtherCtorE = dyn_cast<OtherConstructorDeclRefExpr>(E)) {
231232
if (!passReference(OtherCtorE->getDecl(), OtherCtorE->getType(),
232-
OtherCtorE->getConstructorLoc()))
233+
OtherCtorE->getConstructorLoc(),
234+
SemaReferenceKind::DeclConstructorRef))
233235
return { false, nullptr };
234236

235237
} else if (SubscriptExpr *SE = dyn_cast<SubscriptExpr>(E)) {
@@ -293,7 +295,8 @@ bool SemaAnnotator::walkToTypeReprPre(TypeRepr *T) {
293295
return passReference(ModD, std::make_pair(IdT->getIdentifier(),
294296
IdT->getIdLoc()));
295297

296-
return passReference(VD, Type(), DeclNameLoc(IdT->getIdLoc()));
298+
return passReference(VD, Type(), DeclNameLoc(IdT->getIdLoc()),
299+
SemaReferenceKind::TypeRef);
297300
}
298301
}
299302
return true;
@@ -323,7 +326,8 @@ std::pair<bool, Pattern *> SemaAnnotator::walkToPatternPre(Pattern *P) {
323326
if (!Element)
324327
return { true, P };
325328
Type T = EP->hasType() ? EP->getType() : Type();
326-
return { passReference(Element, T, DeclNameLoc(EP->getLoc())), P };
329+
return { passReference(Element, T, DeclNameLoc(EP->getLoc()),
330+
SemaReferenceKind::EnumElementRef), P };
327331
}
328332

329333
auto *TP = dyn_cast<TypedPattern>(P);
@@ -353,7 +357,8 @@ bool SemaAnnotator::handleImports(ImportDecl *Import) {
353357
auto Decls = Import->getDecls();
354358
if (Decls.size() == 1) {
355359
// FIXME: ImportDecl should store a DeclNameLoc.
356-
if (!passReference(Decls.front(), Type(), DeclNameLoc(Import->getEndLoc())))
360+
if (!passReference(Decls.front(), Type(), DeclNameLoc(Import->getEndLoc()),
361+
SemaReferenceKind::DeclRef))
357362
return false;
358363
}
359364

@@ -385,7 +390,8 @@ bool SemaAnnotator::passSubscriptReference(ValueDecl *D, SourceLoc Loc,
385390
return Continue;
386391
}
387392

388-
bool SemaAnnotator::passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc) {
393+
bool SemaAnnotator::
394+
passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc, SemaReferenceKind Kind) {
389395
TypeDecl *CtorTyRef = nullptr;
390396

391397
if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
@@ -401,7 +407,7 @@ bool SemaAnnotator::passReference(ValueDecl *D, Type Ty, DeclNameLoc Loc) {
401407
CharSourceRange Range =
402408
Lexer::getCharSourceRangeFromSourceRange(D->getASTContext().SourceMgr,
403409
Loc.getSourceRange());
404-
bool Continue = SEWalker.visitDeclReference(D, Range, CtorTyRef, Ty);
410+
bool Continue = SEWalker.visitDeclReference(D, Range, CtorTyRef, Ty, Kind);
405411
if (!Continue)
406412
Cancelled = true;
407413
return Continue;
@@ -475,7 +481,8 @@ bool SourceEntityWalker::walk(DeclContext *DC) {
475481
}
476482

477483
bool SourceEntityWalker::visitDeclReference(ValueDecl *D, CharSourceRange Range,
478-
TypeDecl *CtorTyRef, Type T) {
484+
TypeDecl *CtorTyRef, Type T,
485+
SemaReferenceKind Kind) {
479486
return true;
480487
}
481488

@@ -485,7 +492,8 @@ bool SourceEntityWalker::visitSubscriptReference(ValueDecl *D,
485492
// Most of the clients treat subscript reference the same way as a
486493
// regular reference when called on the open bracket and
487494
// ignore the closing one.
488-
return IsOpenBracket ? visitDeclReference(D, Range, nullptr, Type()) : true;
495+
return IsOpenBracket ? visitDeclReference(D, Range, nullptr, Type(),
496+
SemaReferenceKind::SubscriptRef) : true;
489497
}
490498

491499
bool SourceEntityWalker::visitCallArgName(Identifier Name,

lib/IDE/SwiftSourceDocInfo.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ bool SemaLocResolver::tryResolve(ModuleEntity Mod, SourceLoc Loc) {
9393
bool SemaLocResolver::visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
9494
bool IsOpenBracket) {
9595
// We should treat both open and close brackets equally
96-
return visitDeclReference(D, Range, nullptr, Type());
96+
return visitDeclReference(D, Range, nullptr, Type(),
97+
SemaReferenceKind::SubscriptRef);
9798
}
9899

99100
SemaToken SemaLocResolver::resolve(SourceLoc Loc) {
@@ -146,7 +147,8 @@ bool SemaLocResolver::walkToStmtPost(Stmt *S) {
146147
}
147148

148149
bool SemaLocResolver::visitDeclReference(ValueDecl *D, CharSourceRange Range,
149-
TypeDecl *CtorTyRef, Type T) {
150+
TypeDecl *CtorTyRef, Type T,
151+
SemaReferenceKind Kind) {
150152
if (isDone())
151153
return false;
152154
return !tryResolve(D, CtorTyRef, Range.getStart(), /*IsRef=*/true, T);
@@ -506,7 +508,7 @@ bool RangeResolver::walkToDeclPost(Decl *D) {
506508

507509
bool RangeResolver::
508510
visitDeclReference(ValueDecl *D, CharSourceRange Range, TypeDecl *CtorTyRef,
509-
Type T) {
511+
Type T, SemaReferenceKind Kind) {
510512
Impl->analyzeDeclRef(D, Range, T);
511513
return true;
512514
}

lib/Index/Index.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
247247
}
248248

249249
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
250-
TypeDecl *CtorTyRef, Type T) override {
250+
TypeDecl *CtorTyRef, Type T,
251+
SemaReferenceKind Kind) override {
251252
SourceLoc Loc = Range.getStart();
252253
if (CtorTyRef)
253254
if (!reportRef(CtorTyRef, Loc))

tools/SourceKit/lib/SwiftLang/SwiftDocSupport.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,7 @@ static void addParameters(ArrayRef<Identifier> &ArgNames,
759759
TypeRange = InOutTyR->getBase()->getSourceRange();
760760
if (TypeRange.isInvalid())
761761
continue;
762-
762+
763763
unsigned StartOffs = SM.getLocOffsetInBuffer(TypeRange.Start, BufferID);
764764
unsigned EndOffs =
765765
SM.getLocOffsetInBuffer(Lexer::getLocForEndOfToken(SM, TypeRange.End),
@@ -927,7 +927,7 @@ static bool reportModuleDocInfo(CompilerInvocation Invocation,
927927
if (makeParserAST(ParseCI, IFaceInfo.Text))
928928
return true;
929929
addParameterEntities(ParseCI, IFaceInfo);
930-
930+
931931
Consumer.handleSourceText(IFaceInfo.Text);
932932
reportDocEntities(Ctx, IFaceInfo.TopEntities, Consumer);
933933
reportSourceAnnotations(IFaceInfo, ParseCI, Consumer);
@@ -977,7 +977,8 @@ class SourceDocASTWalker : public SourceEntityWalker {
977977
}
978978

979979
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
980-
TypeDecl *CtorTyRef, Type Ty) override {
980+
TypeDecl *CtorTyRef, Type Ty,
981+
SemaReferenceKind Kind) override {
981982
unsigned StartOffset = getOffset(Range.getStart());
982983
References.emplace_back(D, StartOffset, Range.getByteLength(), Ty);
983984
return true;
@@ -986,7 +987,8 @@ class SourceDocASTWalker : public SourceEntityWalker {
986987
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
987988
bool IsOpenBracket) override {
988989
// Treat both open and close brackets equally
989-
return visitDeclReference(D, Range, nullptr, Type());
990+
return visitDeclReference(D, Range, nullptr, Type(),
991+
SemaReferenceKind::SubscriptRef);
990992
}
991993

992994
bool isLocal(Decl *D) const {

tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,8 @@ class SemanticAnnotator : public SourceEntityWalker {
771771
: SM(SM), BufferID(BufferID) {}
772772

773773
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
774-
TypeDecl *CtorTyRef, Type T) override {
774+
TypeDecl *CtorTyRef, Type T,
775+
SemaReferenceKind Kind) override {
775776
if (isa<VarDecl>(D) && D->hasName() && D->getName().str() == "self")
776777
return true;
777778

@@ -788,7 +789,8 @@ class SemanticAnnotator : public SourceEntityWalker {
788789
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
789790
bool IsOpenBracket) override {
790791
// We should treat both open and close brackets equally
791-
return visitDeclReference(D, Range, nullptr, Type());
792+
return visitDeclReference(D, Range, nullptr, Type(),
793+
SemaReferenceKind::SubscriptRef);
792794
}
793795

794796
void annotate(const Decl *D, bool IsRef, CharSourceRange Range) {

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,8 @@ class RelatedIdScanner : public SourceEntityWalker {
13671367
return true;
13681368
}
13691369
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
1370-
TypeDecl *CtorTyRef, Type T) override {
1370+
TypeDecl *CtorTyRef, Type T,
1371+
SemaReferenceKind Kind) override {
13711372
if (Cancelled)
13721373
return false;
13731374
if (CtorTyRef)

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,14 +1147,16 @@ class AnnotationPrinter : public SourceEntityWalker {
11471147
}
11481148

11491149
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
1150-
TypeDecl *CtorTyRef, Type Ty) override {
1150+
TypeDecl *CtorTyRef, Type Ty,
1151+
SemaReferenceKind Kind) override {
11511152
annotateSourceEntity({ Range, D, CtorTyRef, /*IsRef=*/true });
11521153
return true;
11531154
}
11541155

11551156
bool visitSubscriptReference(ValueDecl *D, CharSourceRange Range,
11561157
bool IsOpenBracket) override {
1157-
return visitDeclReference(D, Range, nullptr, Type());
1158+
return visitDeclReference(D, Range, nullptr, Type(),
1159+
SemaReferenceKind::SubscriptRef);
11581160
}
11591161

11601162
bool visitCallArgName(Identifier Name, CharSourceRange Range,
@@ -2545,7 +2547,8 @@ class TypeReconstructWalker : public SourceEntityWalker {
25452547
}
25462548

25472549
bool visitDeclReference(ValueDecl *D, CharSourceRange Range,
2548-
TypeDecl *CtorTyRef, Type T) override {
2550+
TypeDecl *CtorTyRef, Type T,
2551+
SemaReferenceKind Kind) override {
25492552
if (SeenDecls.insert(D).second)
25502553
tryDemangleDecl(D, Range, /*isRef=*/true);
25512554

0 commit comments

Comments
 (0)