1//===--- IndexSymbol.cpp - Types and functions for indexing symbols -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Index/IndexSymbol.h"
10#include "clang/AST/Attr.h"
11#include "clang/AST/Decl.h"
12#include "clang/AST/DeclCXX.h"
13#include "clang/AST/DeclObjC.h"
14#include "clang/AST/DeclTemplate.h"
15#include "clang/AST/PrettyPrinter.h"
16#include "clang/AST/TypeBase.h"
17#include "clang/Lex/MacroInfo.h"
18
19using namespace clang;
20using namespace clang::index;
21
22/// \returns true if \c D is a subclass of 'XCTestCase'.
23static bool isUnitTestCase(const ObjCInterfaceDecl *D) {
24 if (!D)
25 return false;
26 while (const ObjCInterfaceDecl *SuperD = D->getSuperClass()) {
27 if (SuperD->getName() == "XCTestCase")
28 return true;
29 D = SuperD;
30 }
31 return false;
32}
33
34/// \returns true if \c D is in a subclass of 'XCTestCase', returns void, has
35/// no parameters, and its name starts with 'test'.
36static bool isUnitTest(const ObjCMethodDecl *D) {
37 if (!D->parameters().empty())
38 return false;
39 if (!D->getReturnType()->isVoidType())
40 return false;
41 if (!D->getSelector().getNameForSlot(argIndex: 0).starts_with(Prefix: "test"))
42 return false;
43 return isUnitTestCase(D: D->getClassInterface());
44}
45
46static void checkForIBOutlets(const Decl *D, SymbolPropertySet &PropSet) {
47 if (D->hasAttr<IBOutletAttr>()) {
48 PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
49 } else if (D->hasAttr<IBOutletCollectionAttr>()) {
50 PropSet |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
51 PropSet |= (SymbolPropertySet)SymbolProperty::IBOutletCollection;
52 }
53}
54
55bool index::isFunctionLocalSymbol(const Decl *D) {
56 assert(D);
57
58 if (isa<ParmVarDecl>(Val: D))
59 return true;
60
61 if (isa<ObjCTypeParamDecl>(Val: D))
62 return true;
63
64 if (isa<UsingDirectiveDecl>(Val: D))
65 return false;
66 if (!D->getParentFunctionOrMethod())
67 return false;
68
69 if (const NamedDecl *ND = dyn_cast<NamedDecl>(Val: D)) {
70 switch (ND->getFormalLinkage()) {
71 case Linkage::Invalid:
72 llvm_unreachable("Linkage hasn't been computed!");
73 case Linkage::None:
74 case Linkage::Internal:
75 return true;
76 case Linkage::VisibleNone:
77 case Linkage::UniqueExternal:
78 llvm_unreachable("Not a sema linkage");
79 case Linkage::Module:
80 case Linkage::External:
81 return false;
82 }
83 }
84
85 return true;
86}
87
88static SymbolSubKind getSubKindForTypedef(const TypedefNameDecl *TND) {
89 if (const TagDecl *TD = TND->getUnderlyingType()->getAsTagDecl()) {
90 switch (TD->getTagKind()) {
91 case TagTypeKind::Class:
92 return SymbolSubKind::UsingClass;
93 case TagTypeKind::Struct:
94 return SymbolSubKind::UsingStruct;
95 default:
96 // Leave SymbolSubKind blank.
97 // New subkinds like UsingUnion can be added if/when needed.
98 return SymbolSubKind::None;
99 }
100 }
101 // Not a tag type, e.g. typedef for a builtin type.
102 return SymbolSubKind::None;
103}
104
105SymbolInfo index::getSymbolInfo(const Decl *D) {
106 assert(D);
107 SymbolInfo Info;
108 Info.Kind = SymbolKind::Unknown;
109 Info.SubKind = SymbolSubKind::None;
110 Info.Properties = SymbolPropertySet();
111 Info.Lang = SymbolLanguage::C;
112
113 if (isFunctionLocalSymbol(D)) {
114 Info.Properties |= (SymbolPropertySet)SymbolProperty::Local;
115 }
116 if (isa<ObjCProtocolDecl>(Val: D->getDeclContext())) {
117 Info.Properties |= (SymbolPropertySet)SymbolProperty::ProtocolInterface;
118 }
119
120 if (auto *VT = dyn_cast<VarTemplateDecl>(Val: D)) {
121 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
122 Info.Lang = SymbolLanguage::CXX;
123 // All other fields are filled from the templated decl.
124 D = VT->getTemplatedDecl();
125 }
126
127 if (const TagDecl *TD = dyn_cast<TagDecl>(Val: D)) {
128 switch (TD->getTagKind()) {
129 case TagTypeKind::Struct:
130 Info.Kind = SymbolKind::Struct; break;
131 case TagTypeKind::Union:
132 Info.Kind = SymbolKind::Union; break;
133 case TagTypeKind::Class:
134 Info.Kind = SymbolKind::Class;
135 Info.Lang = SymbolLanguage::CXX;
136 break;
137 case TagTypeKind::Interface:
138 Info.Kind = SymbolKind::Protocol;
139 Info.Lang = SymbolLanguage::CXX;
140 break;
141 case TagTypeKind::Enum:
142 Info.Kind = SymbolKind::Enum; break;
143 }
144
145 if (const CXXRecordDecl *CXXRec = dyn_cast<CXXRecordDecl>(Val: D)) {
146 if (!CXXRec->isCLike()) {
147 Info.Lang = SymbolLanguage::CXX;
148 if (CXXRec->getDescribedClassTemplate()) {
149 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
150 }
151 }
152 }
153
154 if (isa<ClassTemplatePartialSpecializationDecl>(Val: D)) {
155 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
156 Info.Properties |=
157 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
158 } else if (isa<ClassTemplateSpecializationDecl>(Val: D)) {
159 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
160 Info.Properties |=
161 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
162 }
163
164 } else if (auto *VD = dyn_cast<VarDecl>(Val: D)) {
165 Info.Kind = SymbolKind::Variable;
166 if (isa<ParmVarDecl>(Val: D)) {
167 Info.Kind = SymbolKind::Parameter;
168 } else if (isa<CXXRecordDecl>(Val: D->getDeclContext())) {
169 Info.Kind = SymbolKind::StaticProperty;
170 Info.Lang = SymbolLanguage::CXX;
171 }
172
173 if (isa<VarTemplatePartialSpecializationDecl>(Val: D)) {
174 Info.Lang = SymbolLanguage::CXX;
175 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
176 Info.Properties |=
177 (SymbolPropertySet)SymbolProperty::TemplatePartialSpecialization;
178 } else if (isa<VarTemplateSpecializationDecl>(Val: D)) {
179 Info.Lang = SymbolLanguage::CXX;
180 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
181 Info.Properties |=
182 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
183 } else if (VD->getDescribedVarTemplate()) {
184 Info.Lang = SymbolLanguage::CXX;
185 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
186 }
187
188 } else {
189 switch (D->getKind()) {
190 case Decl::Import:
191 Info.Kind = SymbolKind::Module;
192 break;
193 case Decl::Typedef: {
194 Info.Kind = SymbolKind::TypeAlias; // Lang = C
195 Info.SubKind = getSubKindForTypedef(TND: cast<TypedefDecl>(Val: D));
196 break;
197 }
198 case Decl::Function:
199 Info.Kind = SymbolKind::Function;
200 break;
201 case Decl::Field:
202 case Decl::IndirectField:
203 Info.Kind = SymbolKind::Field;
204 if (const CXXRecordDecl *
205 CXXRec = dyn_cast<CXXRecordDecl>(Val: D->getDeclContext())) {
206 if (!CXXRec->isCLike())
207 Info.Lang = SymbolLanguage::CXX;
208 }
209 break;
210 case Decl::EnumConstant:
211 Info.Kind = SymbolKind::EnumConstant; break;
212 case Decl::ObjCInterface:
213 case Decl::ObjCImplementation: {
214 Info.Kind = SymbolKind::Class;
215 Info.Lang = SymbolLanguage::ObjC;
216 const ObjCInterfaceDecl *ClsD = dyn_cast<ObjCInterfaceDecl>(Val: D);
217 if (!ClsD)
218 ClsD = cast<ObjCImplementationDecl>(Val: D)->getClassInterface();
219 if (isUnitTestCase(D: ClsD))
220 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
221 break;
222 }
223 case Decl::ObjCProtocol:
224 Info.Kind = SymbolKind::Protocol;
225 Info.Lang = SymbolLanguage::ObjC;
226 break;
227 case Decl::ObjCCategory:
228 case Decl::ObjCCategoryImpl: {
229 Info.Kind = SymbolKind::Extension;
230 Info.Lang = SymbolLanguage::ObjC;
231 const ObjCInterfaceDecl *ClsD = nullptr;
232 if (auto *CatD = dyn_cast<ObjCCategoryDecl>(Val: D))
233 ClsD = CatD->getClassInterface();
234 else
235 ClsD = cast<ObjCCategoryImplDecl>(Val: D)->getClassInterface();
236 if (isUnitTestCase(D: ClsD))
237 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
238 break;
239 }
240 case Decl::ObjCMethod: {
241 const ObjCMethodDecl *MD = cast<ObjCMethodDecl>(Val: D);
242 Info.Kind = MD->isInstanceMethod() ? SymbolKind::InstanceMethod : SymbolKind::ClassMethod;
243 if (MD->isPropertyAccessor()) {
244 if (MD->param_size())
245 Info.SubKind = SymbolSubKind::AccessorSetter;
246 else
247 Info.SubKind = SymbolSubKind::AccessorGetter;
248 }
249 Info.Lang = SymbolLanguage::ObjC;
250 if (isUnitTest(D: MD))
251 Info.Properties |= (SymbolPropertySet)SymbolProperty::UnitTest;
252 if (D->hasAttr<IBActionAttr>())
253 Info.Properties |= (SymbolPropertySet)SymbolProperty::IBAnnotated;
254 break;
255 }
256 case Decl::ObjCProperty:
257 Info.Kind = SymbolKind::InstanceProperty;
258 Info.Lang = SymbolLanguage::ObjC;
259 checkForIBOutlets(D, PropSet&: Info.Properties);
260 if (auto *Annot = D->getAttr<AnnotateAttr>()) {
261 if (Annot->getAnnotation() == "gk_inspectable")
262 Info.Properties |= (SymbolPropertySet)SymbolProperty::GKInspectable;
263 }
264 break;
265 case Decl::ObjCIvar:
266 Info.Kind = SymbolKind::Field;
267 Info.Lang = SymbolLanguage::ObjC;
268 checkForIBOutlets(D, PropSet&: Info.Properties);
269 break;
270 case Decl::Namespace:
271 Info.Kind = SymbolKind::Namespace;
272 Info.Lang = SymbolLanguage::CXX;
273 break;
274 case Decl::NamespaceAlias:
275 Info.Kind = SymbolKind::NamespaceAlias;
276 Info.Lang = SymbolLanguage::CXX;
277 break;
278 case Decl::CXXConstructor: {
279 Info.Kind = SymbolKind::Constructor;
280 Info.Lang = SymbolLanguage::CXX;
281 auto *CD = cast<CXXConstructorDecl>(Val: D);
282 if (CD->isCopyConstructor())
283 Info.SubKind = SymbolSubKind::CXXCopyConstructor;
284 else if (CD->isMoveConstructor())
285 Info.SubKind = SymbolSubKind::CXXMoveConstructor;
286 break;
287 }
288 case Decl::CXXDestructor:
289 Info.Kind = SymbolKind::Destructor;
290 Info.Lang = SymbolLanguage::CXX;
291 break;
292 case Decl::CXXConversion:
293 Info.Kind = SymbolKind::ConversionFunction;
294 Info.Lang = SymbolLanguage::CXX;
295 break;
296 case Decl::CXXMethod: {
297 const CXXMethodDecl *MD = cast<CXXMethodDecl>(Val: D);
298 if (MD->isStatic())
299 Info.Kind = SymbolKind::StaticMethod;
300 else
301 Info.Kind = SymbolKind::InstanceMethod;
302 Info.Lang = SymbolLanguage::CXX;
303 break;
304 }
305 case Decl::ClassTemplate:
306 Info.Kind = SymbolKind::Class;
307 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
308 Info.Lang = SymbolLanguage::CXX;
309 break;
310 case Decl::FunctionTemplate:
311 Info.Kind = SymbolKind::Function;
312 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
313 Info.Lang = SymbolLanguage::CXX;
314 if (const CXXMethodDecl *MD = dyn_cast_or_null<CXXMethodDecl>(
315 Val: cast<FunctionTemplateDecl>(Val: D)->getTemplatedDecl())) {
316 if (isa<CXXConstructorDecl>(Val: MD))
317 Info.Kind = SymbolKind::Constructor;
318 else if (isa<CXXDestructorDecl>(Val: MD))
319 Info.Kind = SymbolKind::Destructor;
320 else if (isa<CXXConversionDecl>(Val: MD))
321 Info.Kind = SymbolKind::ConversionFunction;
322 else {
323 if (MD->isStatic())
324 Info.Kind = SymbolKind::StaticMethod;
325 else
326 Info.Kind = SymbolKind::InstanceMethod;
327 }
328 }
329 break;
330 case Decl::TypeAliasTemplate:
331 Info.Kind = SymbolKind::TypeAlias;
332 Info.Lang = SymbolLanguage::CXX;
333 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
334 break;
335 case Decl::TypeAlias: {
336 Info.Kind = SymbolKind::TypeAlias;
337 Info.SubKind = getSubKindForTypedef(TND: cast<TypeAliasDecl>(Val: D));
338 Info.Lang = SymbolLanguage::CXX;
339 break;
340 }
341 case Decl::UnresolvedUsingTypename:
342 Info.Kind = SymbolKind::Using;
343 Info.SubKind = SymbolSubKind::UsingTypename;
344 Info.Lang = SymbolLanguage::CXX;
345 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
346 break;
347 case Decl::UnresolvedUsingValue:
348 Info.Kind = SymbolKind::Using;
349 Info.SubKind = SymbolSubKind::UsingValue;
350 Info.Lang = SymbolLanguage::CXX;
351 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
352 break;
353 case Decl::Using:
354 Info.Kind = SymbolKind::Using;
355 Info.Lang = SymbolLanguage::CXX;
356 break;
357 case Decl::UsingEnum:
358 Info.Kind = SymbolKind::Using;
359 Info.Lang = SymbolLanguage::CXX;
360 Info.SubKind = SymbolSubKind::UsingEnum;
361 break;
362 case Decl::Binding:
363 Info.Kind = SymbolKind::Variable;
364 Info.Lang = SymbolLanguage::CXX;
365 break;
366 case Decl::MSProperty:
367 Info.Kind = SymbolKind::InstanceProperty;
368 if (const CXXRecordDecl *CXXRec =
369 dyn_cast<CXXRecordDecl>(Val: D->getDeclContext())) {
370 if (!CXXRec->isCLike())
371 Info.Lang = SymbolLanguage::CXX;
372 }
373 break;
374 case Decl::ClassTemplatePartialSpecialization:
375 case Decl::ClassTemplateSpecialization:
376 case Decl::CXXRecord:
377 case Decl::Enum:
378 case Decl::Record:
379 llvm_unreachable("records handled before");
380 break;
381 case Decl::VarTemplateSpecialization:
382 case Decl::VarTemplatePartialSpecialization:
383 case Decl::ImplicitParam:
384 case Decl::ParmVar:
385 case Decl::Var:
386 case Decl::VarTemplate:
387 llvm_unreachable("variables handled before");
388 break;
389 case Decl::TemplateTypeParm:
390 Info.Kind = SymbolKind::TemplateTypeParm;
391 break;
392 case Decl::TemplateTemplateParm:
393 Info.Kind = SymbolKind::TemplateTemplateParm;
394 break;
395 case Decl::NonTypeTemplateParm:
396 Info.Kind = SymbolKind::NonTypeTemplateParm;
397 break;
398 case Decl::Concept:
399 Info.Kind = SymbolKind::Concept;
400 break;
401 // Other decls get the 'unknown' kind.
402 default:
403 break;
404 }
405 }
406
407 if (Info.Kind == SymbolKind::Unknown)
408 return Info;
409
410 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: D)) {
411 if (FD->getTemplatedKind() ==
412 FunctionDecl::TK_FunctionTemplateSpecialization) {
413 Info.Properties |= (SymbolPropertySet)SymbolProperty::Generic;
414 Info.Properties |=
415 (SymbolPropertySet)SymbolProperty::TemplateSpecialization;
416 }
417 }
418
419 if (Info.Properties & (SymbolPropertySet)SymbolProperty::Generic)
420 Info.Lang = SymbolLanguage::CXX;
421
422 if (auto *attr = D->getExternalSourceSymbolAttr()) {
423 if (attr->getLanguage() == "Swift")
424 Info.Lang = SymbolLanguage::Swift;
425 }
426
427 return Info;
428}
429
430SymbolInfo index::getSymbolInfoForMacro(const MacroInfo &) {
431 SymbolInfo Info;
432 Info.Kind = SymbolKind::Macro;
433 Info.SubKind = SymbolSubKind::None;
434 Info.Properties = SymbolPropertySet();
435 Info.Lang = SymbolLanguage::C;
436 return Info;
437}
438
439bool index::applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
440 llvm::function_ref<bool(SymbolRole)> Fn) {
441#define APPLY_FOR_ROLE(Role) \
442 if (Roles & (unsigned)SymbolRole::Role) \
443 if (!Fn(SymbolRole::Role)) \
444 return false;
445
446 APPLY_FOR_ROLE(Declaration);
447 APPLY_FOR_ROLE(Definition);
448 APPLY_FOR_ROLE(Reference);
449 APPLY_FOR_ROLE(Read);
450 APPLY_FOR_ROLE(Write);
451 APPLY_FOR_ROLE(Call);
452 APPLY_FOR_ROLE(Dynamic);
453 APPLY_FOR_ROLE(AddressOf);
454 APPLY_FOR_ROLE(Implicit);
455 APPLY_FOR_ROLE(Undefinition);
456 APPLY_FOR_ROLE(RelationChildOf);
457 APPLY_FOR_ROLE(RelationBaseOf);
458 APPLY_FOR_ROLE(RelationOverrideOf);
459 APPLY_FOR_ROLE(RelationReceivedBy);
460 APPLY_FOR_ROLE(RelationCalledBy);
461 APPLY_FOR_ROLE(RelationExtendedBy);
462 APPLY_FOR_ROLE(RelationAccessorOf);
463 APPLY_FOR_ROLE(RelationContainedBy);
464 APPLY_FOR_ROLE(RelationIBTypeOf);
465 APPLY_FOR_ROLE(RelationSpecializationOf);
466 APPLY_FOR_ROLE(NameReference);
467
468#undef APPLY_FOR_ROLE
469
470 return true;
471}
472
473void index::applyForEachSymbolRole(SymbolRoleSet Roles,
474 llvm::function_ref<void(SymbolRole)> Fn) {
475 applyForEachSymbolRoleInterruptible(Roles, Fn: [&](SymbolRole r) -> bool {
476 Fn(r);
477 return true;
478 });
479}
480
481void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) {
482 bool VisitedOnce = false;
483 applyForEachSymbolRole(Roles, Fn: [&](SymbolRole Role) {
484 if (VisitedOnce)
485 OS << ',';
486 else
487 VisitedOnce = true;
488 switch (Role) {
489 case SymbolRole::Declaration: OS << "Decl"; break;
490 case SymbolRole::Definition: OS << "Def"; break;
491 case SymbolRole::Reference: OS << "Ref"; break;
492 case SymbolRole::Read: OS << "Read"; break;
493 case SymbolRole::Write: OS << "Writ"; break;
494 case SymbolRole::Call: OS << "Call"; break;
495 case SymbolRole::Dynamic: OS << "Dyn"; break;
496 case SymbolRole::AddressOf: OS << "Addr"; break;
497 case SymbolRole::Implicit: OS << "Impl"; break;
498 case SymbolRole::Undefinition: OS << "Undef"; break;
499 case SymbolRole::RelationChildOf: OS << "RelChild"; break;
500 case SymbolRole::RelationBaseOf: OS << "RelBase"; break;
501 case SymbolRole::RelationOverrideOf: OS << "RelOver"; break;
502 case SymbolRole::RelationReceivedBy: OS << "RelRec"; break;
503 case SymbolRole::RelationCalledBy: OS << "RelCall"; break;
504 case SymbolRole::RelationExtendedBy: OS << "RelExt"; break;
505 case SymbolRole::RelationAccessorOf: OS << "RelAcc"; break;
506 case SymbolRole::RelationContainedBy: OS << "RelCont"; break;
507 case SymbolRole::RelationIBTypeOf: OS << "RelIBType"; break;
508 case SymbolRole::RelationSpecializationOf: OS << "RelSpecialization"; break;
509 case SymbolRole::NameReference: OS << "NameReference"; break;
510 }
511 });
512}
513
514bool index::printSymbolName(const Decl *D, const LangOptions &LO,
515 raw_ostream &OS) {
516 if (auto *ND = dyn_cast<NamedDecl>(Val: D)) {
517 PrintingPolicy Policy(LO);
518 // Forward references can have different template argument names. Suppress
519 // the template argument names in constructors to make their name more
520 // stable.
521 Policy.SuppressTemplateArgsInCXXConstructors = true;
522 DeclarationName DeclName = ND->getDeclName();
523 if (DeclName.isEmpty())
524 return true;
525 DeclName.print(OS, Policy);
526 return false;
527 } else {
528 return true;
529 }
530}
531
532StringRef index::getSymbolKindString(SymbolKind K) {
533 switch (K) {
534 // FIXME: for backwards compatibility, the include directive kind is treated
535 // the same as Unknown
536 case SymbolKind::IncludeDirective:
537 case SymbolKind::Unknown: return "<unknown>";
538 case SymbolKind::Module: return "module";
539 case SymbolKind::Namespace: return "namespace";
540 case SymbolKind::NamespaceAlias: return "namespace-alias";
541 case SymbolKind::Macro: return "macro";
542 case SymbolKind::Enum: return "enum";
543 case SymbolKind::Struct: return "struct";
544 case SymbolKind::Class: return "class";
545 case SymbolKind::Protocol: return "protocol";
546 case SymbolKind::Extension: return "extension";
547 case SymbolKind::Union: return "union";
548 case SymbolKind::TypeAlias: return "type-alias";
549 case SymbolKind::Function: return "function";
550 case SymbolKind::Variable: return "variable";
551 case SymbolKind::Field: return "field";
552 case SymbolKind::EnumConstant: return "enumerator";
553 case SymbolKind::InstanceMethod: return "instance-method";
554 case SymbolKind::ClassMethod: return "class-method";
555 case SymbolKind::StaticMethod: return "static-method";
556 case SymbolKind::InstanceProperty: return "instance-property";
557 case SymbolKind::ClassProperty: return "class-property";
558 case SymbolKind::StaticProperty: return "static-property";
559 case SymbolKind::Constructor: return "constructor";
560 case SymbolKind::Destructor: return "destructor";
561 case SymbolKind::ConversionFunction: return "conversion-func";
562 case SymbolKind::Parameter: return "param";
563 case SymbolKind::Using: return "using";
564 case SymbolKind::TemplateTypeParm: return "template-type-param";
565 case SymbolKind::TemplateTemplateParm: return "template-template-param";
566 case SymbolKind::NonTypeTemplateParm: return "non-type-template-param";
567 case SymbolKind::Concept:
568 return "concept";
569 }
570 llvm_unreachable("invalid symbol kind");
571}
572
573StringRef index::getSymbolSubKindString(SymbolSubKind K) {
574 switch (K) {
575 case SymbolSubKind::None: return "<none>";
576 case SymbolSubKind::CXXCopyConstructor: return "cxx-copy-ctor";
577 case SymbolSubKind::CXXMoveConstructor: return "cxx-move-ctor";
578 case SymbolSubKind::AccessorGetter: return "acc-get";
579 case SymbolSubKind::AccessorSetter: return "acc-set";
580 case SymbolSubKind::UsingTypename:
581 return "using-typename";
582 case SymbolSubKind::UsingValue:
583 return "using-value";
584 case SymbolSubKind::UsingEnum:
585 return "using-enum";
586 case SymbolSubKind::UsingClass:
587 return "using-class";
588 case SymbolSubKind::UsingStruct:
589 return "using-struct";
590 }
591 llvm_unreachable("invalid symbol subkind");
592}
593
594StringRef index::getSymbolLanguageString(SymbolLanguage K) {
595 switch (K) {
596 case SymbolLanguage::C: return "C";
597 case SymbolLanguage::ObjC: return "ObjC";
598 case SymbolLanguage::CXX: return "C++";
599 case SymbolLanguage::Swift: return "Swift";
600 }
601 llvm_unreachable("invalid symbol language kind");
602}
603
604void index::applyForEachSymbolProperty(SymbolPropertySet Props,
605 llvm::function_ref<void(SymbolProperty)> Fn) {
606#define APPLY_FOR_PROPERTY(K) \
607 if (Props & (SymbolPropertySet)SymbolProperty::K) \
608 Fn(SymbolProperty::K)
609
610 APPLY_FOR_PROPERTY(Generic);
611 APPLY_FOR_PROPERTY(TemplatePartialSpecialization);
612 APPLY_FOR_PROPERTY(TemplateSpecialization);
613 APPLY_FOR_PROPERTY(UnitTest);
614 APPLY_FOR_PROPERTY(IBAnnotated);
615 APPLY_FOR_PROPERTY(IBOutletCollection);
616 APPLY_FOR_PROPERTY(GKInspectable);
617 APPLY_FOR_PROPERTY(Local);
618 APPLY_FOR_PROPERTY(ProtocolInterface);
619
620#undef APPLY_FOR_PROPERTY
621}
622
623void index::printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS) {
624 bool VisitedOnce = false;
625 applyForEachSymbolProperty(Props, Fn: [&](SymbolProperty Prop) {
626 if (VisitedOnce)
627 OS << ',';
628 else
629 VisitedOnce = true;
630 switch (Prop) {
631 case SymbolProperty::Generic: OS << "Gen"; break;
632 case SymbolProperty::TemplatePartialSpecialization: OS << "TPS"; break;
633 case SymbolProperty::TemplateSpecialization: OS << "TS"; break;
634 case SymbolProperty::UnitTest: OS << "test"; break;
635 case SymbolProperty::IBAnnotated: OS << "IB"; break;
636 case SymbolProperty::IBOutletCollection: OS << "IBColl"; break;
637 case SymbolProperty::GKInspectable: OS << "GKI"; break;
638 case SymbolProperty::Local: OS << "local"; break;
639 case SymbolProperty::ProtocolInterface: OS << "protocol"; break;
640 }
641 });
642}
643