IndexSymbol.h revision 360660
1//===- IndexSymbol.h - Types and functions for indexing symbols -*- C++ -*-===//
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#ifndef LLVM_CLANG_INDEX_INDEXSYMBOL_H
10#define LLVM_CLANG_INDEX_INDEXSYMBOL_H
11
12#include "clang/Basic/LLVM.h"
13#include "clang/Lex/MacroInfo.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/Support/DataTypes.h"
16
17namespace clang {
18  class Decl;
19  class LangOptions;
20
21namespace index {
22
23enum class SymbolKind : uint8_t {
24  Unknown,
25
26  Module,
27  Namespace,
28  NamespaceAlias,
29  Macro,
30
31  Enum,
32  Struct,
33  Class,
34  Protocol,
35  Extension,
36  Union,
37  TypeAlias,
38
39  Function,
40  Variable,
41  Field,
42  EnumConstant,
43
44  InstanceMethod,
45  ClassMethod,
46  StaticMethod,
47  InstanceProperty,
48  ClassProperty,
49  StaticProperty,
50
51  Constructor,
52  Destructor,
53  ConversionFunction,
54
55  Parameter,
56  Using,
57};
58
59enum class SymbolLanguage : uint8_t {
60  C,
61  ObjC,
62  CXX,
63  Swift,
64};
65
66/// Language specific sub-kinds.
67enum class SymbolSubKind : uint8_t {
68  None,
69  CXXCopyConstructor,
70  CXXMoveConstructor,
71  AccessorGetter,
72  AccessorSetter,
73  UsingTypename,
74  UsingValue,
75};
76
77typedef uint16_t SymbolPropertySet;
78/// Set of properties that provide additional info about a symbol.
79enum class SymbolProperty : SymbolPropertySet {
80  Generic                       = 1 << 0,
81  TemplatePartialSpecialization = 1 << 1,
82  TemplateSpecialization        = 1 << 2,
83  UnitTest                      = 1 << 3,
84  IBAnnotated                   = 1 << 4,
85  IBOutletCollection            = 1 << 5,
86  GKInspectable                 = 1 << 6,
87  Local                         = 1 << 7,
88  /// Symbol is part of a protocol interface.
89  ProtocolInterface             = 1 << 8,
90};
91static const unsigned SymbolPropertyBitNum = 9;
92
93/// Set of roles that are attributed to symbol occurrences.
94///
95/// Low 9 bits of clang-c/include/Index.h CXSymbolRole mirrors this enum.
96enum class SymbolRole : uint32_t {
97  Declaration = 1 << 0,
98  Definition = 1 << 1,
99  Reference = 1 << 2,
100  Read = 1 << 3,
101  Write = 1 << 4,
102  Call = 1 << 5,
103  Dynamic = 1 << 6,
104  AddressOf = 1 << 7,
105  Implicit = 1 << 8,
106  // FIXME: this is not mirrored in CXSymbolRole.
107  // Note that macro occurrences aren't currently supported in libclang.
108  Undefinition = 1 << 9, // macro #undef
109
110  // Relation roles.
111  RelationChildOf = 1 << 10,
112  RelationBaseOf = 1 << 11,
113  RelationOverrideOf = 1 << 12,
114  RelationReceivedBy = 1 << 13,
115  RelationCalledBy = 1 << 14,
116  RelationExtendedBy = 1 << 15,
117  RelationAccessorOf = 1 << 16,
118  RelationContainedBy = 1 << 17,
119  RelationIBTypeOf = 1 << 18,
120  RelationSpecializationOf = 1 << 19,
121
122  // Symbol only references the name of the object as written. For example, a
123  // constructor references the class declaration using that role.
124  NameReference = 1 << 20,
125};
126static const unsigned SymbolRoleBitNum = 21;
127typedef unsigned SymbolRoleSet;
128
129/// Represents a relation to another symbol for a symbol occurrence.
130struct SymbolRelation {
131  SymbolRoleSet Roles;
132  const Decl *RelatedSymbol;
133
134  SymbolRelation(SymbolRoleSet Roles, const Decl *Sym)
135    : Roles(Roles), RelatedSymbol(Sym) {}
136};
137
138struct SymbolInfo {
139  SymbolKind Kind;
140  SymbolSubKind SubKind;
141  SymbolLanguage Lang;
142  SymbolPropertySet Properties;
143};
144
145SymbolInfo getSymbolInfo(const Decl *D);
146
147SymbolInfo getSymbolInfoForMacro(const MacroInfo &MI);
148
149bool isFunctionLocalSymbol(const Decl *D);
150
151void applyForEachSymbolRole(SymbolRoleSet Roles,
152                            llvm::function_ref<void(SymbolRole)> Fn);
153bool applyForEachSymbolRoleInterruptible(SymbolRoleSet Roles,
154                            llvm::function_ref<bool(SymbolRole)> Fn);
155void printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS);
156
157/// \returns true if no name was printed, false otherwise.
158bool printSymbolName(const Decl *D, const LangOptions &LO, raw_ostream &OS);
159
160StringRef getSymbolKindString(SymbolKind K);
161StringRef getSymbolSubKindString(SymbolSubKind K);
162StringRef getSymbolLanguageString(SymbolLanguage K);
163
164void applyForEachSymbolProperty(SymbolPropertySet Props,
165                            llvm::function_ref<void(SymbolProperty)> Fn);
166void printSymbolProperties(SymbolPropertySet Props, raw_ostream &OS);
167
168} // namespace index
169} // namespace clang
170
171#endif
172