1 //===- CXCursor.h - Routines for manipulating CXCursors -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines routines for manipulating CXCursors.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
14 #define LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
16 #include "clang-c/Index.h"
17 #include "clang/Basic/SourceLocation.h"
18 #include "llvm/ADT/PointerUnion.h"
26 class CXXBaseSpecifier
;
30 class InclusionDirective
;
32 class MacroDefinitionRecord
;
35 class ObjCInterfaceDecl
;
36 class ObjCProtocolDecl
;
37 class OverloadedTemplateStorage
;
48 CXCursor
getCursor(CXTranslationUnit
, SourceLocation
);
50 CXCursor
MakeCXCursor(const clang::Attr
*A
, const clang::Decl
*Parent
,
51 CXTranslationUnit TU
);
52 CXCursor
MakeCXCursor(const clang::Decl
*D
, CXTranslationUnit TU
,
53 SourceRange RegionOfInterest
= SourceRange(),
54 bool FirstInDeclGroup
= true);
55 CXCursor
MakeCXCursor(const clang::Stmt
*S
, const clang::Decl
*Parent
,
57 SourceRange RegionOfInterest
= SourceRange());
58 CXCursor
MakeCXCursorInvalid(CXCursorKind K
, CXTranslationUnit TU
= nullptr);
60 /// Create an Objective-C superclass reference at the given location.
61 CXCursor
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl
*Super
,
62 SourceLocation Loc
, CXTranslationUnit TU
);
64 /// Unpack an ObjCSuperClassRef cursor into the interface it references
65 /// and optionally the location where the reference occurred.
66 std::pair
<const ObjCInterfaceDecl
*, SourceLocation
>
67 getCursorObjCSuperClassRef(CXCursor C
);
69 /// Create an Objective-C protocol reference at the given location.
70 CXCursor
MakeCursorObjCProtocolRef(const ObjCProtocolDecl
*Proto
,
71 SourceLocation Loc
, CXTranslationUnit TU
);
73 /// Unpack an ObjCProtocolRef cursor into the protocol it references
74 /// and optionally the location where the reference occurred.
75 std::pair
<const ObjCProtocolDecl
*, SourceLocation
>
76 getCursorObjCProtocolRef(CXCursor C
);
78 /// Create an Objective-C class reference at the given location.
79 CXCursor
MakeCursorObjCClassRef(const ObjCInterfaceDecl
*Class
,
80 SourceLocation Loc
, CXTranslationUnit TU
);
82 /// Unpack an ObjCClassRef cursor into the class it references
83 /// and optionally the location where the reference occurred.
84 std::pair
<const ObjCInterfaceDecl
*, SourceLocation
>
85 getCursorObjCClassRef(CXCursor C
);
87 /// Create a type reference at the given location.
88 CXCursor
MakeCursorTypeRef(const TypeDecl
*Type
, SourceLocation Loc
,
89 CXTranslationUnit TU
);
91 /// Unpack a TypeRef cursor into the class it references
92 /// and optionally the location where the reference occurred.
93 std::pair
<const TypeDecl
*, SourceLocation
> getCursorTypeRef(CXCursor C
);
95 /// Create a reference to a template at the given location.
96 CXCursor
MakeCursorTemplateRef(const TemplateDecl
*Template
, SourceLocation Loc
,
97 CXTranslationUnit TU
);
99 /// Unpack a TemplateRef cursor into the template it references and
100 /// the location where the reference occurred.
101 std::pair
<const TemplateDecl
*, SourceLocation
>
102 getCursorTemplateRef(CXCursor C
);
104 /// Create a reference to a namespace or namespace alias at the given
106 CXCursor
MakeCursorNamespaceRef(const NamedDecl
*NS
, SourceLocation Loc
,
107 CXTranslationUnit TU
);
109 /// Unpack a NamespaceRef cursor into the namespace or namespace alias
110 /// it references and the location where the reference occurred.
111 std::pair
<const NamedDecl
*, SourceLocation
> getCursorNamespaceRef(CXCursor C
);
113 /// Create a reference to a variable at the given location.
114 CXCursor
MakeCursorVariableRef(const VarDecl
*Var
, SourceLocation Loc
,
115 CXTranslationUnit TU
);
117 /// Unpack a VariableRef cursor into the variable it references and the
118 /// location where the where the reference occurred.
119 std::pair
<const VarDecl
*, SourceLocation
> getCursorVariableRef(CXCursor C
);
121 /// Create a reference to a field at the given location.
122 CXCursor
MakeCursorMemberRef(const FieldDecl
*Field
, SourceLocation Loc
,
123 CXTranslationUnit TU
);
125 /// Unpack a MemberRef cursor into the field it references and the
126 /// location where the reference occurred.
127 std::pair
<const FieldDecl
*, SourceLocation
> getCursorMemberRef(CXCursor C
);
129 /// Create a CXX base specifier cursor.
130 CXCursor
MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier
*B
,
131 CXTranslationUnit TU
);
133 /// Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
134 const CXXBaseSpecifier
*getCursorCXXBaseSpecifier(CXCursor C
);
136 /// Create a preprocessing directive cursor.
137 CXCursor
MakePreprocessingDirectiveCursor(SourceRange Range
,
138 CXTranslationUnit TU
);
140 /// Unpack a given preprocessing directive to retrieve its source range.
141 SourceRange
getCursorPreprocessingDirective(CXCursor C
);
143 /// Create a macro definition cursor.
144 CXCursor
MakeMacroDefinitionCursor(const MacroDefinitionRecord
*,
145 CXTranslationUnit TU
);
147 /// Unpack a given macro definition cursor to retrieve its
149 const MacroDefinitionRecord
*getCursorMacroDefinition(CXCursor C
);
151 /// Create a macro expansion cursor.
152 CXCursor
MakeMacroExpansionCursor(MacroExpansion
*, CXTranslationUnit TU
);
154 /// Create a "pseudo" macro expansion cursor, using a macro definition
155 /// and a source location.
156 CXCursor
MakeMacroExpansionCursor(MacroDefinitionRecord
*, SourceLocation Loc
,
157 CXTranslationUnit TU
);
159 /// Wraps a macro expansion cursor and provides a common interface
160 /// for a normal macro expansion cursor or a "pseudo" one.
162 /// "Pseudo" macro expansion cursors (essentially a macro definition along with
163 /// a source location) are created in special cases, for example they can be
164 /// created for identifiers inside macro definitions, if these identifiers are
166 class MacroExpansionCursor
{
169 bool isPseudo() const { return C
.data
[1] != nullptr; }
170 const MacroDefinitionRecord
*getAsMacroDefinition() const {
172 return static_cast<const MacroDefinitionRecord
*>(C
.data
[0]);
174 const MacroExpansion
*getAsMacroExpansion() const {
176 return static_cast<const MacroExpansion
*>(C
.data
[0]);
178 SourceLocation
getPseudoLoc() const {
180 return SourceLocation::getFromPtrEncoding(C
.data
[1]);
184 MacroExpansionCursor(CXCursor C
) : C(C
) {
185 assert(C
.kind
== CXCursor_MacroExpansion
);
188 const IdentifierInfo
*getName() const;
189 const MacroDefinitionRecord
*getDefinition() const;
190 SourceRange
getSourceRange() const;
193 /// Unpack a given macro expansion cursor to retrieve its info.
194 static inline MacroExpansionCursor
getCursorMacroExpansion(CXCursor C
) {
198 /// Create an inclusion directive cursor.
199 CXCursor
MakeInclusionDirectiveCursor(InclusionDirective
*,
200 CXTranslationUnit TU
);
202 /// Unpack a given inclusion directive cursor to retrieve its
204 const InclusionDirective
*getCursorInclusionDirective(CXCursor C
);
206 /// Create a label reference at the given location.
207 CXCursor
MakeCursorLabelRef(LabelStmt
*Label
, SourceLocation Loc
,
208 CXTranslationUnit TU
);
210 /// Unpack a label reference into the label statement it refers to and
211 /// the location of the reference.
212 std::pair
<const LabelStmt
*, SourceLocation
> getCursorLabelRef(CXCursor C
);
214 /// Create a overloaded declaration reference cursor for an expression.
215 CXCursor
MakeCursorOverloadedDeclRef(const OverloadExpr
*E
,
216 CXTranslationUnit TU
);
218 /// Create a overloaded declaration reference cursor for a declaration.
219 CXCursor
MakeCursorOverloadedDeclRef(const Decl
*D
, SourceLocation Location
,
220 CXTranslationUnit TU
);
222 /// Create a overloaded declaration reference cursor for a template name.
223 CXCursor
MakeCursorOverloadedDeclRef(TemplateName Template
,
224 SourceLocation Location
,
225 CXTranslationUnit TU
);
227 /// Internal storage for an overloaded declaration reference cursor;
228 typedef llvm::PointerUnion
<const OverloadExpr
*, const Decl
*,
229 OverloadedTemplateStorage
*>
230 OverloadedDeclRefStorage
;
232 /// Unpack an overloaded declaration reference into an expression,
233 /// declaration, or template name along with the source location.
234 std::pair
<OverloadedDeclRefStorage
, SourceLocation
>
235 getCursorOverloadedDeclRef(CXCursor C
);
237 const Decl
*getCursorDecl(CXCursor Cursor
);
238 const Expr
*getCursorExpr(CXCursor Cursor
);
239 const Stmt
*getCursorStmt(CXCursor Cursor
);
240 const Attr
*getCursorAttr(CXCursor Cursor
);
242 ASTContext
&getCursorContext(CXCursor Cursor
);
243 ASTUnit
*getCursorASTUnit(CXCursor Cursor
);
244 CXTranslationUnit
getCursorTU(CXCursor Cursor
);
246 void getOverriddenCursors(CXCursor cursor
,
247 SmallVectorImpl
<CXCursor
> &overridden
);
249 /// Create an opaque pool used for fast generation of overridden
251 void *createOverridenCXCursorsPool();
253 /// Dispose of the overridden CXCursors pool.
254 void disposeOverridenCXCursorsPool(void *pool
);
256 /// Returns a index/location pair for a selector identifier if the cursor
258 std::pair
<int, SourceLocation
> getSelectorIdentifierIndexAndLoc(CXCursor
);
259 static inline int getSelectorIdentifierIndex(CXCursor cursor
) {
260 return getSelectorIdentifierIndexAndLoc(cursor
).first
;
262 static inline SourceLocation
getSelectorIdentifierLoc(CXCursor cursor
) {
263 return getSelectorIdentifierIndexAndLoc(cursor
).second
;
266 CXCursor
getSelectorIdentifierCursor(int SelIdx
, CXCursor cursor
);
268 static inline CXCursor
getTypeRefedCallExprCursor(CXCursor cursor
) {
269 CXCursor newCursor
= cursor
;
270 if (cursor
.kind
== CXCursor_CallExpr
)
275 CXCursor
getTypeRefCursor(CXCursor cursor
);
277 /// Generate a USR for \arg D and put it in \arg Buf.
278 /// \returns true if no USR was computed or the result should be ignored,
280 bool getDeclCursorUSR(const Decl
*D
, SmallVectorImpl
<char> &Buf
);
282 bool operator==(CXCursor X
, CXCursor Y
);
284 inline bool operator!=(CXCursor X
, CXCursor Y
) { return !(X
== Y
); }
286 /// Return true if the cursor represents a declaration that is the
287 /// first in a declaration group.
288 bool isFirstInDeclGroup(CXCursor C
);
290 } // namespace cxcursor