Skip to content

Commit 6096b8b

Browse files
committed
[index] Make IndexSymbol's subkind into a bitset
This follows a similar change to clang indexing, and will allow us to add overlapping sub kinds such as "UnitTest", etc.
1 parent ef4e84b commit 6096b8b

File tree

6 files changed

+57
-45
lines changed

6 files changed

+57
-45
lines changed

include/swift/Index/IndexDataConsumer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class IndexDataConsumer {
3434
virtual bool finishDependency(SymbolKind kind) = 0;
3535
virtual bool startSourceEntity(const IndexSymbol &symbol) = 0;
3636
virtual bool recordRelatedEntity(const IndexSymbol &symbol) = 0;
37-
virtual bool finishSourceEntity(SymbolKind kind, SymbolSubKind subKind,
37+
virtual bool finishSourceEntity(SymbolKind kind, SymbolSubKindSet subKinds,
3838
SymbolRoleSet roles) = 0;
3939
};
4040

include/swift/Index/IndexSymbol.h

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,34 @@ enum class SymbolKind {
5858
Destructor,
5959
};
6060

61-
enum class SymbolSubKind {
62-
None,
63-
64-
AccessorGetter,
65-
AccessorSetter,
66-
AccessorWillSet,
67-
AccessorDidSet,
68-
AccessorAddressor,
69-
AccessorMutableAddressor,
70-
71-
ExtensionOfStruct,
72-
ExtensionOfClass,
73-
ExtensionOfEnum,
74-
ExtensionOfProtocol,
61+
enum class SymbolSubKind : uint32_t {
62+
None = 0,
63+
64+
AccessorGetter = 1 << 0,
65+
AccessorSetter = 1 << 1,
66+
AccessorWillSet = 1 << 2,
67+
AccessorDidSet = 1 << 3,
68+
AccessorAddressor = 1 << 4,
69+
AccessorMutableAddressor = 1 << 5,
70+
71+
ExtensionOfStruct = 1 << 6,
72+
ExtensionOfClass = 1 << 7,
73+
ExtensionOfEnum = 1 << 8,
74+
ExtensionOfProtocol = 1 << 9,
7575
};
7676

77+
typedef uint32_t SymbolSubKindSet;
78+
79+
inline SymbolSubKindSet operator&(SymbolSubKindSet SKSet, SymbolSubKind SK) {
80+
return SKSet & (SymbolSubKindSet)SK;
81+
}
82+
inline SymbolSubKindSet operator|(SymbolSubKindSet SKSet, SymbolSubKind SK) {
83+
return SKSet | (SymbolSubKindSet)SK;
84+
}
85+
inline SymbolSubKindSet &operator|=(SymbolSubKindSet &SKSet, SymbolSubKind SK) {
86+
return SKSet = SKSet | SK;
87+
}
88+
7789
using SymbolRole = clang::index::SymbolRole;
7890
using SymbolRoleSet = clang::index::SymbolRoleSet;
7991

@@ -82,7 +94,7 @@ struct IndexSymbol {
8294
TypeKind entityType = Base;
8395

8496
SymbolKind kind;
85-
SymbolSubKind subKind = SymbolSubKind::None;
97+
SymbolSubKindSet subKinds = SymbolSubKindSet(0);
8698
SymbolRoleSet roles = SymbolRoleSet(0);
8799
// The following strings are guaranteed to live at least as long as the
88100
// current indexing action.

lib/Index/Index.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
8585
struct Entity {
8686
Decl *D;
8787
SymbolKind Kind;
88-
SymbolSubKind SubKind;
88+
SymbolSubKindSet SubKinds;
8989
SymbolRoleSet Roles;
9090
};
9191
SmallVector<Entity, 6> EntitiesStack;
@@ -268,7 +268,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
268268
bool finishCurrentEntity() {
269269
Entity CurrEnt = EntitiesStack.pop_back_val();
270270
assert(CurrEnt.Kind != SymbolKind::Unknown);
271-
if (!IdxConsumer.finishSourceEntity(CurrEnt.Kind, CurrEnt.SubKind,
271+
if (!IdxConsumer.finishSourceEntity(CurrEnt.Kind, CurrEnt.SubKinds,
272272
CurrEnt.Roles)) {
273273
Cancelled = true;
274274
return false;
@@ -502,7 +502,7 @@ bool IndexSwiftASTWalker::startEntity(ValueDecl *D, const IndexSymbol &Info) {
502502
return false;
503503
}
504504

505-
EntitiesStack.push_back({D, Info.kind, Info.subKind, Info.roles});
505+
EntitiesStack.push_back({D, Info.kind, Info.subKinds, Info.roles});
506506
return true;
507507
}
508508

@@ -573,7 +573,7 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
573573

574574
auto handleInfo = [this, D, AccKind](IndexSymbol &Info) {
575575
Info.kind = SymbolKind::Accessor;
576-
Info.subKind = getSubKindForAccessor(AccKind);
576+
Info.subKinds |= getSubKindForAccessor(AccKind);
577577
Info.name = "";
578578
Info.USR = getAccessorUSR(D, AccKind);
579579
Info.group = "";
@@ -582,7 +582,7 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
582582
Cancelled = true;
583583
return false;
584584
}
585-
if (!IdxConsumer.finishSourceEntity(Info.kind, Info.subKind, Info.roles)) {
585+
if (!IdxConsumer.finishSourceEntity(Info.kind, Info.subKinds, Info.roles)) {
586586
Cancelled = true;
587587
return false;
588588
}
@@ -634,15 +634,15 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
634634

635635
Info.kind = getSymbolKindForDecl(D);
636636
if (isa<StructDecl>(NTD))
637-
Info.subKind = SymbolSubKind::ExtensionOfStruct;
637+
Info.subKinds |= SymbolSubKind::ExtensionOfStruct;
638638
else if (isa<ClassDecl>(NTD))
639-
Info.subKind = SymbolSubKind::ExtensionOfClass;
639+
Info.subKinds |= SymbolSubKind::ExtensionOfClass;
640640
else if (isa<EnumDecl>(NTD))
641-
Info.subKind = SymbolSubKind::ExtensionOfEnum;
641+
Info.subKinds |= SymbolSubKind::ExtensionOfEnum;
642642
else if (isa<ProtocolDecl>(NTD))
643-
Info.subKind = SymbolSubKind::ExtensionOfProtocol;
643+
Info.subKinds |= SymbolSubKind::ExtensionOfProtocol;
644644

645-
assert(Info.subKind != SymbolSubKind::None);
645+
assert(Info.subKinds != 0);
646646

647647
if (!IdxConsumer.startSourceEntity(Info)) {
648648
Cancelled = true;
@@ -653,7 +653,7 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
653653
if (Cancelled)
654654
return false;
655655

656-
EntitiesStack.push_back({D, Info.kind, Info.subKind, Info.roles});
656+
EntitiesStack.push_back({D, Info.kind, Info.subKinds, Info.roles});
657657
return true;
658658
}
659659

@@ -766,7 +766,7 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
766766
return true;
767767

768768
if (Info.kind == SymbolKind::Accessor)
769-
Info.subKind = getSubKindForAccessor(cast<FuncDecl>(D)->getAccessorKind());
769+
Info.subKinds |= getSubKindForAccessor(cast<FuncDecl>(D)->getAccessorKind());
770770
// Cannot be extension, which is not a ValueDecl.
771771

772772
if (IsRef)

tools/SourceKit/lib/SwiftLang/SwiftIndexing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,18 @@ class SKIndexDataConsumer : public IndexDataConsumer {
8383
});
8484
}
8585

86-
bool finishSourceEntity(SymbolKind kind, SymbolSubKind subKind,
86+
bool finishSourceEntity(SymbolKind kind, SymbolSubKindSet subKinds,
8787
SymbolRoleSet roles) override {
8888
bool isRef = roles & (unsigned)SymbolRole::Reference;
89-
auto UID = SwiftLangSupport::getUIDForSymbol(kind, subKind, isRef);
89+
auto UID = SwiftLangSupport::getUIDForSymbol(kind, subKinds, isRef);
9090
return impl.finishSourceEntity(UID);
9191
}
9292

9393
template <typename F>
9494
bool withEntityInfo(const IndexSymbol &symbol, F func) {
9595
auto initEntity = [](EntityInfo &info, const IndexSymbol &symbol) {
9696
bool isRef = symbol.roles & (unsigned)SymbolRole::Reference;
97-
info.Kind = SwiftLangSupport::getUIDForSymbol(symbol.kind, symbol.subKind,
97+
info.Kind = SwiftLangSupport::getUIDForSymbol(symbol.kind, symbol.subKinds,
9898
isRef);
9999
info.Name = symbol.name;
100100
info.USR = symbol.USR;

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ using namespace swift;
3939
using namespace swift::ide;
4040
using swift::index::SymbolKind;
4141
using swift::index::SymbolSubKind;
42+
using swift::index::SymbolSubKindSet;
4243
using swift::index::SymbolRole;
4344
using swift::index::SymbolRoleSet;
4445

@@ -522,7 +523,7 @@ UIdent SwiftLangSupport::getUIDForSyntaxStructureElementKind(
522523
}
523524
}
524525

525-
UIdent SwiftLangSupport::getUIDForSymbol(SymbolKind kind, SymbolSubKind subKind,
526+
UIdent SwiftLangSupport::getUIDForSymbol(SymbolKind kind, SymbolSubKindSet subKinds,
526527
bool isRef) {
527528

528529
#define UID_FOR(CLASS) isRef ? KindRef##CLASS : KindDecl##CLASS;
@@ -571,30 +572,29 @@ UIdent SwiftLangSupport::getUIDForSymbol(SymbolKind kind, SymbolSubKind subKind,
571572
assert(!isRef && "reference to extension decl?");
572573
SWIFT_FALLTHROUGH;
573574
case SymbolKind::Accessor:
574-
switch (subKind) {
575-
case SymbolSubKind::AccessorGetter:
575+
if (subKinds & SymbolSubKind::AccessorGetter) {
576576
return UID_FOR(AccessorGetter);
577-
case SymbolSubKind::AccessorSetter:
577+
} else if (subKinds & SymbolSubKind::AccessorSetter) {
578578
return UID_FOR(AccessorSetter);
579-
case SymbolSubKind::AccessorWillSet:
579+
} else if (subKinds & SymbolSubKind::AccessorWillSet) {
580580
return UID_FOR(AccessorWillSet);
581-
case SymbolSubKind::AccessorDidSet:
581+
} else if (subKinds & SymbolSubKind::AccessorDidSet) {
582582
return UID_FOR(AccessorDidSet);
583-
case SymbolSubKind::AccessorAddressor:
583+
} else if (subKinds & SymbolSubKind::AccessorAddressor) {
584584
return UID_FOR(AccessorAddress);
585-
case SymbolSubKind::AccessorMutableAddressor:
585+
} else if (subKinds & SymbolSubKind::AccessorMutableAddressor) {
586586
return UID_FOR(AccessorMutableAddress);
587587

588-
case SymbolSubKind::ExtensionOfStruct:
588+
} else if (subKinds & SymbolSubKind::ExtensionOfStruct) {
589589
return KindDeclExtensionStruct;
590-
case SymbolSubKind::ExtensionOfClass:
590+
} else if (subKinds & SymbolSubKind::ExtensionOfClass) {
591591
return KindDeclExtensionClass;
592-
case SymbolSubKind::ExtensionOfEnum:
592+
} else if (subKinds & SymbolSubKind::ExtensionOfEnum) {
593593
return KindDeclExtensionEnum;
594-
case SymbolSubKind::ExtensionOfProtocol:
594+
} else if (subKinds & SymbolSubKind::ExtensionOfProtocol) {
595595
return KindDeclExtensionProtocol;
596596

597-
case SymbolSubKind::None:
597+
} else {
598598
llvm_unreachable("missing sub kind");
599599
}
600600

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ class SwiftLangSupport : public LangSupport {
248248
swift::ide::SyntaxStructureElementKind Kind);
249249

250250
static SourceKit::UIdent getUIDForSymbol(swift::index::SymbolKind kind,
251-
swift::index::SymbolSubKind subKind,
251+
swift::index::SymbolSubKindSet subKinds,
252252
bool isRef);
253253

254254
static bool printDisplayName(const swift::ValueDecl *D, llvm::raw_ostream &OS);

0 commit comments

Comments
 (0)