Skip to content

Commit db8ec19

Browse files
committed
[index] Use the index symbol types and APIs from the clang header.
This avoids duplication and centralizes handling of symbols.
1 parent e2c9e24 commit db8ec19

File tree

10 files changed

+245
-359
lines changed

10 files changed

+245
-359
lines changed

include/swift/Index/IndexDataConsumer.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ class IndexDataConsumer {
3131
virtual bool enableWarnings() { return false; }
3232

3333
virtual bool recordHash(StringRef hash, bool isKnown) = 0;
34-
virtual bool startDependency(SymbolKind kind, StringRef name, StringRef path,
34+
virtual bool startDependency(StringRef name, StringRef path, bool isClangModule,
3535
bool isSystem, StringRef hash) = 0;
36-
virtual bool finishDependency(SymbolKind kind) = 0;
36+
virtual bool finishDependency(bool isClangModule) = 0;
3737
virtual Action startSourceEntity(const IndexSymbol &symbol) = 0;
38-
virtual bool finishSourceEntity(SymbolKind kind, SymbolSubKindSet subKinds,
39-
SymbolRoleSet roles) = 0;
38+
virtual bool finishSourceEntity(SymbolInfo symInfo, SymbolRoleSet roles) = 0;
4039

4140
virtual void finish() {}
4241
};

include/swift/Index/IndexSymbol.h

Lines changed: 22 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,33 @@
2020
namespace swift {
2121
class Decl;
2222
class ValueDecl;
23+
enum class AccessorKind;
2324

2425
namespace index {
2526

26-
enum class SymbolKind {
27-
Unknown,
28-
29-
Module,
30-
ClangModule, // FIXME: collapse into Module and use a separate Language field.
31-
32-
Enum,
33-
Struct,
34-
Class,
35-
Protocol,
36-
Extension,
37-
38-
TypeAlias,
39-
AssociatedType,
40-
GenericTypeParam,
41-
42-
Function,
43-
Variable,
44-
PrefixOperator,
45-
PostfixOperator,
46-
InfixOperator,
47-
Accessor,
48-
Subscript,
49-
EnumElement,
50-
51-
InstanceMethod,
52-
ClassMethod,
53-
StaticMethod,
54-
InstanceProperty,
55-
ClassProperty,
56-
StaticProperty,
57-
58-
Constructor,
59-
Destructor,
60-
};
61-
62-
enum class SymbolSubKind : uint32_t {
63-
None = 0,
64-
65-
AccessorGetter = 1 << 0,
66-
AccessorSetter = 1 << 1,
67-
AccessorWillSet = 1 << 2,
68-
AccessorDidSet = 1 << 3,
69-
AccessorAddressor = 1 << 4,
70-
AccessorMutableAddressor = 1 << 5,
71-
72-
ExtensionOfStruct = 1 << 6,
73-
ExtensionOfClass = 1 << 7,
74-
ExtensionOfEnum = 1 << 8,
75-
ExtensionOfProtocol = 1 << 9,
76-
77-
UnitTest = 1 << 10,
78-
};
79-
80-
typedef uint32_t SymbolSubKindSet;
81-
82-
inline SymbolSubKindSet operator&(SymbolSubKindSet SKSet, SymbolSubKind SK) {
83-
return SKSet & (SymbolSubKindSet)SK;
27+
using clang::index::SymbolKind;
28+
using clang::index::SymbolLanguage;
29+
using clang::index::SymbolSubKind;
30+
using clang::index::SymbolProperty;
31+
using clang::index::SymbolPropertySet;
32+
using clang::index::SymbolRole;
33+
using clang::index::SymbolRoleSet;
34+
using clang::index::SymbolRelation;
35+
using clang::index::SymbolInfo;
36+
37+
inline SymbolPropertySet operator&(SymbolPropertySet SKSet, SymbolProperty SK) {
38+
return SKSet & (SymbolPropertySet)SK;
8439
}
85-
inline SymbolSubKindSet operator|(SymbolSubKindSet SKSet, SymbolSubKind SK) {
86-
return SKSet | (SymbolSubKindSet)SK;
40+
inline SymbolPropertySet operator|(SymbolPropertySet SKSet, SymbolProperty SK) {
41+
return SKSet | (SymbolPropertySet)SK;
8742
}
88-
inline SymbolSubKindSet &operator|=(SymbolSubKindSet &SKSet, SymbolSubKind SK) {
43+
inline SymbolPropertySet &operator|=(SymbolPropertySet &SKSet, SymbolProperty SK) {
8944
return SKSet = SKSet | SK;
9045
}
9146

92-
using SymbolRole = clang::index::SymbolRole;
93-
using SymbolRoleSet = clang::index::SymbolRoleSet;
94-
9547
struct IndexRelation {
9648
const ValueDecl *decl;
97-
SymbolKind kind;
98-
SymbolSubKindSet subKinds = SymbolSubKindSet(0);
49+
SymbolInfo symInfo;
9950
SymbolRoleSet roles = SymbolRoleSet(0);
10051

10152
// The following strings are guaranteed to live at least as long as the
@@ -104,8 +55,8 @@ struct IndexRelation {
10455
StringRef USR; // USR may be safely compared by pointer.
10556
StringRef group;
10657

107-
IndexRelation(SymbolRoleSet Roles, const ValueDecl *Sym, SymbolKind Kind, SymbolSubKindSet SubKinds, StringRef Name, StringRef USR)
108-
: decl(Sym), kind(Kind), subKinds(SubKinds), roles(Roles), name(Name), USR(USR) {}
58+
IndexRelation(SymbolRoleSet Roles, const ValueDecl *Sym, SymbolInfo SymInfo, StringRef Name, StringRef USR)
59+
: decl(Sym), symInfo(SymInfo), roles(Roles), name(Name), USR(USR) {}
10960

11061
IndexRelation() = default;
11162
};
@@ -126,13 +77,10 @@ struct IndexSymbol : IndexRelation {
12677
}
12778
};
12879

129-
SymbolKind getSymbolKindForDecl(const Decl *D);
130-
131-
StringRef getSymbolKindString(SymbolKind K);
80+
SymbolInfo getSymbolInfoForDecl(const Decl *D);
81+
SymbolSubKind getSubKindForAccessor(AccessorKind AK);
13282

133-
void applyForEachSymbolSubKind(SymbolSubKindSet SubKinds,
134-
llvm::function_ref<void(SymbolSubKind)> Fn);
135-
void printSymbolSubKinds(SymbolSubKindSet SubKinds, raw_ostream &OS);
83+
using clang::index::printSymbolProperties;
13684

13785
} // end namespace index
13886
} // end namespace swift

lib/Index/Index.cpp

Lines changed: 28 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@
2424
using namespace swift;
2525
using namespace swift::index;
2626

27-
static SymbolSubKind getSubKindForAccessor(AccessorKind AK) {
28-
switch (AK) {
29-
case AccessorKind::NotAccessor: return SymbolSubKind::None;
30-
case AccessorKind::IsGetter: return SymbolSubKind::AccessorGetter;
31-
case AccessorKind::IsSetter: return SymbolSubKind::AccessorSetter;
32-
case AccessorKind::IsWillSet: return SymbolSubKind::AccessorWillSet;
33-
case AccessorKind::IsDidSet: return SymbolSubKind::AccessorDidSet;
34-
case AccessorKind::IsAddressor: return SymbolSubKind::AccessorAddressor;
35-
case AccessorKind::IsMutableAddressor:
36-
return SymbolSubKind::AccessorMutableAddressor;
37-
case AccessorKind::IsMaterializeForSet:
38-
llvm_unreachable("unexpected MaterializeForSet");
39-
}
40-
41-
llvm_unreachable("Unhandled AccessorKind in switch.");
42-
}
43-
4427
static bool
4528
printArtificialName(const swift::AbstractStorageDecl *ASD, AccessorKind AK, llvm::raw_ostream &OS) {
4629
switch (AK) {
@@ -132,8 +115,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
132115
bool isSystemModule = false;
133116
struct Entity {
134117
Decl *D;
135-
SymbolKind Kind;
136-
SymbolSubKindSet SubKinds;
118+
SymbolInfo SymInfo;
137119
SymbolRoleSet Roles;
138120
SmallVector<SourceLoc, 6> RefsToSuppress;
139121
};
@@ -204,17 +186,14 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
204186
bool addRelation(IndexSymbol &Info, SymbolRoleSet RelationRoles, ValueDecl *D) {
205187
assert(D);
206188
StringRef Name, USR;
207-
SymbolKind Kind = getSymbolKindForDecl(D);
208-
SymbolSubKindSet SubKinds = 0;
189+
SymbolInfo SymInfo = getSymbolInfoForDecl(D);
209190

210-
if (Kind == SymbolKind::Unknown)
191+
if (SymInfo.Kind == SymbolKind::Unknown)
211192
return true;
212-
if (Kind == SymbolKind::Accessor)
213-
SubKinds |= getSubKindForAccessor(cast<FuncDecl>(D)->getAccessorKind());
214193
if (getNameAndUSR(D, Name, USR))
215194
return true;
216195

217-
Info.Relations.push_back(IndexRelation(RelationRoles, D, Kind, SubKinds, Name, USR));
196+
Info.Relations.push_back(IndexRelation(RelationRoles, D, SymInfo, Name, USR));
218197
Info.roles |= RelationRoles;
219198
return false;
220199
}
@@ -371,9 +350,8 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
371350

372351
bool finishCurrentEntity() {
373352
Entity CurrEnt = EntitiesStack.pop_back_val();
374-
assert(CurrEnt.Kind != SymbolKind::Unknown);
375-
if (!IdxConsumer.finishSourceEntity(CurrEnt.Kind, CurrEnt.SubKinds,
376-
CurrEnt.Roles)) {
353+
assert(CurrEnt.SymInfo.Kind != SymbolKind::Unknown);
354+
if (!IdxConsumer.finishSourceEntity(CurrEnt.SymInfo, CurrEnt.Roles)) {
377355
Cancelled = true;
378356
return false;
379357
}
@@ -512,43 +490,44 @@ bool IndexSwiftASTWalker::visitImports(
512490
if (Path.empty() || Path == TopMod.getFilename())
513491
continue; // this is a submodule.
514492

515-
SymbolKind ImportKind = SymbolKind::Unknown;
493+
Optional<bool> IsClangModuleOpt;
516494
for (auto File : Mod->getFiles()) {
517495
switch (File->getKind()) {
518496
case FileUnitKind::Source:
519497
case FileUnitKind::Builtin:
520498
case FileUnitKind::Derived:
521499
break;
522500
case FileUnitKind::SerializedAST:
523-
assert(ImportKind == SymbolKind::Unknown &&
501+
assert(!IsClangModuleOpt.hasValue() &&
524502
"cannot handle multi-file modules");
525-
ImportKind = SymbolKind::Module;
503+
IsClangModuleOpt = false;
526504
break;
527505
case FileUnitKind::ClangModule:
528-
assert(ImportKind == SymbolKind::Unknown &&
506+
assert(!IsClangModuleOpt.hasValue() &&
529507
"cannot handle multi-file modules");
530-
ImportKind = SymbolKind::ClangModule;
508+
IsClangModuleOpt = true;
531509
break;
532510
}
533511
}
534-
if (ImportKind == SymbolKind::Unknown)
512+
if (!IsClangModuleOpt.hasValue())
535513
continue;
514+
bool IsClangModule = *IsClangModuleOpt;
536515

537516
StringRef Hash;
538517
SmallString<32> HashBuf;
539-
if (ImportKind != SymbolKind::ClangModule) {
518+
if (!IsClangModule) {
540519
llvm::raw_svector_ostream HashOS(HashBuf);
541520
getModuleHash(*Mod, HashOS);
542521
Hash = HashOS.str();
543522
}
544523

545-
if (!IdxConsumer.startDependency(ImportKind, Mod->getName().str(), Path,
524+
if (!IdxConsumer.startDependency(Mod->getName().str(), Path, IsClangModule,
546525
Mod->isSystemModule(), Hash))
547526
return false;
548-
if (ImportKind != SymbolKind::ClangModule)
527+
if (!IsClangModule)
549528
if (!visitImports(*Mod, Visited))
550529
return false;
551-
if (!IdxConsumer.finishDependency(ImportKind))
530+
if (!IdxConsumer.finishDependency(IsClangModule))
552531
return false;
553532
}
554533

@@ -563,7 +542,7 @@ bool IndexSwiftASTWalker::startEntity(Decl *D, IndexSymbol &Info) {
563542
case swift::index::IndexDataConsumer::Skip:
564543
return false;
565544
case swift::index::IndexDataConsumer::Continue:
566-
EntitiesStack.push_back({D, Info.kind, Info.subKinds, Info.roles, {}});
545+
EntitiesStack.push_back({D, Info.symInfo, Info.roles, {}});
567546
return true;
568547
}
569548
}
@@ -598,7 +577,8 @@ bool IndexSwiftASTWalker::startEntityDecl(ValueDecl *D) {
598577
// FIXME handle extensions properly
599578
if (auto ParentVD = dyn_cast<ValueDecl>(Parent)) {
600579
SymbolRoleSet RelationsToParent = (SymbolRoleSet)SymbolRole::RelationChildOf;
601-
if (Info.kind == SymbolKind::Accessor)
580+
if (Info.symInfo.SubKind >= SymbolSubKind::SwiftAccessorGetter &&
581+
Info.symInfo.SubKind <= SymbolSubKind::SwiftAccessorMutableAddressor)
602582
RelationsToParent |= (SymbolRoleSet)SymbolRole::RelationAccessorOf;
603583
if (addRelation(Info, RelationsToParent, ParentVD))
604584
return false;
@@ -670,8 +650,8 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
670650
auto updateInfo = [this, D, AccKind](IndexSymbol &Info) {
671651
if (getPseudoAccessorNameAndUSR(D, AccKind, Info.name, Info.USR))
672652
return true;
673-
Info.kind = SymbolKind::Accessor;
674-
Info.subKinds |= getSubKindForAccessor(AccKind);
653+
Info.symInfo.Kind = SymbolKind::Function;
654+
Info.symInfo.SubKind = getSubKindForAccessor(AccKind);
675655
Info.roles |= (SymbolRoleSet)SymbolRole::Implicit;
676656
Info.group = "";
677657
return false;
@@ -684,7 +664,7 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
684664
if (updateInfo(Info))
685665
return true;
686666

687-
if (!IdxConsumer.startSourceEntity(Info) || !IdxConsumer.finishSourceEntity(Info.kind, Info.subKinds, Info.roles))
667+
if (!IdxConsumer.startSourceEntity(Info) || !IdxConsumer.finishSourceEntity(Info.symInfo, Info.roles))
688668
Cancelled = true;
689669
} else {
690670
IndexSymbol Info;
@@ -695,7 +675,7 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
695675
if (addRelation(Info, (SymbolRoleSet) SymbolRole::RelationAccessorOf, D))
696676
return true;
697677

698-
if (!IdxConsumer.startSourceEntity(Info) || !IdxConsumer.finishSourceEntity(Info.kind, Info.subKinds, Info.roles))
678+
if (!IdxConsumer.startSourceEntity(Info) || !IdxConsumer.finishSourceEntity(Info.symInfo, Info.roles))
699679
Cancelled = true;
700680
}
701681
return !Cancelled;
@@ -727,17 +707,7 @@ bool IndexSwiftASTWalker::reportExtension(ExtensionDecl *D) {
727707
if (initIndexSymbol(NTD, Loc, /*IsRef=*/false, Info))
728708
return true;
729709

730-
Info.kind = getSymbolKindForDecl(D);
731-
if (isa<StructDecl>(NTD))
732-
Info.subKinds |= SymbolSubKind::ExtensionOfStruct;
733-
else if (isa<ClassDecl>(NTD))
734-
Info.subKinds |= SymbolSubKind::ExtensionOfClass;
735-
else if (isa<EnumDecl>(NTD))
736-
Info.subKinds |= SymbolSubKind::ExtensionOfEnum;
737-
else if (isa<ProtocolDecl>(NTD))
738-
Info.subKinds |= SymbolSubKind::ExtensionOfProtocol;
739-
740-
assert(Info.subKinds != 0);
710+
Info.symInfo = getSymbolInfoForDecl(D);
741711

742712
if (!startEntity(D, Info))
743713
return false;
@@ -850,12 +820,10 @@ bool IndexSwiftASTWalker::initIndexSymbol(ValueDecl *D, SourceLoc Loc,
850820
bool IsRef, IndexSymbol &Info) {
851821
assert(D);
852822
Info.decl = D;
853-
Info.kind = getSymbolKindForDecl(D);
854-
if (Info.kind == SymbolKind::Unknown)
823+
Info.symInfo = getSymbolInfoForDecl(D);
824+
if (Info.symInfo.Kind == SymbolKind::Unknown)
855825
return true;
856826

857-
if (Info.kind == SymbolKind::Accessor)
858-
Info.subKinds |= getSubKindForAccessor(cast<FuncDecl>(D)->getAccessorKind());
859827
// Cannot be extension, which is not a ValueDecl.
860828

861829
if (IsRef)
@@ -931,7 +899,7 @@ bool IndexSwiftASTWalker::initFuncDeclIndexSymbol(ValueDecl *D,
931899
return true;
932900

933901
if (isTestCandidate(D))
934-
Info.subKinds |= SymbolSubKind::UnitTest;
902+
Info.symInfo.Properties |= SymbolProperty::UnitTest;
935903

936904
if (auto Group = D->getGroupName())
937905
Info.group = Group.getValue();

0 commit comments

Comments
 (0)