xref: /aosp_15_r20/external/clang/tools/libclang/CXCursor.h (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===- CXCursor.h - Routines for manipulating CXCursors -------------------===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li // This file defines routines for manipulating CXCursors.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li 
14*67e74705SXin Li #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
15*67e74705SXin Li #define LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
16*67e74705SXin Li 
17*67e74705SXin Li #include "clang-c/Index.h"
18*67e74705SXin Li #include "clang/Basic/SourceLocation.h"
19*67e74705SXin Li #include "llvm/ADT/PointerUnion.h"
20*67e74705SXin Li #include <utility>
21*67e74705SXin Li 
22*67e74705SXin Li namespace clang {
23*67e74705SXin Li 
24*67e74705SXin Li class ASTContext;
25*67e74705SXin Li class ASTUnit;
26*67e74705SXin Li class Attr;
27*67e74705SXin Li class CXXBaseSpecifier;
28*67e74705SXin Li class Decl;
29*67e74705SXin Li class Expr;
30*67e74705SXin Li class FieldDecl;
31*67e74705SXin Li class InclusionDirective;
32*67e74705SXin Li class LabelStmt;
33*67e74705SXin Li class MacroDefinitionRecord;
34*67e74705SXin Li class MacroExpansion;
35*67e74705SXin Li class NamedDecl;
36*67e74705SXin Li class ObjCInterfaceDecl;
37*67e74705SXin Li class ObjCProtocolDecl;
38*67e74705SXin Li class OverloadedTemplateStorage;
39*67e74705SXin Li class OverloadExpr;
40*67e74705SXin Li class Stmt;
41*67e74705SXin Li class TemplateDecl;
42*67e74705SXin Li class TemplateName;
43*67e74705SXin Li class TypeDecl;
44*67e74705SXin Li class VarDecl;
45*67e74705SXin Li class IdentifierInfo;
46*67e74705SXin Li 
47*67e74705SXin Li namespace cxcursor {
48*67e74705SXin Li 
49*67e74705SXin Li CXCursor getCursor(CXTranslationUnit, SourceLocation);
50*67e74705SXin Li 
51*67e74705SXin Li CXCursor MakeCXCursor(const clang::Attr *A, const clang::Decl *Parent,
52*67e74705SXin Li                       CXTranslationUnit TU);
53*67e74705SXin Li CXCursor MakeCXCursor(const clang::Decl *D, CXTranslationUnit TU,
54*67e74705SXin Li                       SourceRange RegionOfInterest = SourceRange(),
55*67e74705SXin Li                       bool FirstInDeclGroup = true);
56*67e74705SXin Li CXCursor MakeCXCursor(const clang::Stmt *S, const clang::Decl *Parent,
57*67e74705SXin Li                       CXTranslationUnit TU,
58*67e74705SXin Li                       SourceRange RegionOfInterest = SourceRange());
59*67e74705SXin Li CXCursor MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU = nullptr);
60*67e74705SXin Li 
61*67e74705SXin Li /// \brief Create an Objective-C superclass reference at the given location.
62*67e74705SXin Li CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
63*67e74705SXin Li                                      SourceLocation Loc,
64*67e74705SXin Li                                      CXTranslationUnit TU);
65*67e74705SXin Li 
66*67e74705SXin Li /// \brief Unpack an ObjCSuperClassRef cursor into the interface it references
67*67e74705SXin Li /// and optionally the location where the reference occurred.
68*67e74705SXin Li std::pair<const ObjCInterfaceDecl *, SourceLocation>
69*67e74705SXin Li   getCursorObjCSuperClassRef(CXCursor C);
70*67e74705SXin Li 
71*67e74705SXin Li /// \brief Create an Objective-C protocol reference at the given location.
72*67e74705SXin Li CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
73*67e74705SXin Li                                    SourceLocation Loc,
74*67e74705SXin Li                                    CXTranslationUnit TU);
75*67e74705SXin Li 
76*67e74705SXin Li /// \brief Unpack an ObjCProtocolRef cursor into the protocol it references
77*67e74705SXin Li /// and optionally the location where the reference occurred.
78*67e74705SXin Li std::pair<const ObjCProtocolDecl *, SourceLocation>
79*67e74705SXin Li   getCursorObjCProtocolRef(CXCursor C);
80*67e74705SXin Li 
81*67e74705SXin Li /// \brief Create an Objective-C class reference at the given location.
82*67e74705SXin Li CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
83*67e74705SXin Li                                 SourceLocation Loc,
84*67e74705SXin Li                                 CXTranslationUnit TU);
85*67e74705SXin Li 
86*67e74705SXin Li /// \brief Unpack an ObjCClassRef cursor into the class it references
87*67e74705SXin Li /// and optionally the location where the reference occurred.
88*67e74705SXin Li std::pair<const ObjCInterfaceDecl *, SourceLocation>
89*67e74705SXin Li   getCursorObjCClassRef(CXCursor C);
90*67e74705SXin Li 
91*67e74705SXin Li /// \brief Create a type reference at the given location.
92*67e74705SXin Li CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
93*67e74705SXin Li                            CXTranslationUnit TU);
94*67e74705SXin Li 
95*67e74705SXin Li /// \brief Unpack a TypeRef cursor into the class it references
96*67e74705SXin Li /// and optionally the location where the reference occurred.
97*67e74705SXin Li std::pair<const TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
98*67e74705SXin Li 
99*67e74705SXin Li /// \brief Create a reference to a template at the given location.
100*67e74705SXin Li CXCursor MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc,
101*67e74705SXin Li                                CXTranslationUnit TU);
102*67e74705SXin Li 
103*67e74705SXin Li /// \brief Unpack a TemplateRef cursor into the template it references and
104*67e74705SXin Li /// the location where the reference occurred.
105*67e74705SXin Li std::pair<const TemplateDecl *, SourceLocation>
106*67e74705SXin Li   getCursorTemplateRef(CXCursor C);
107*67e74705SXin Li 
108*67e74705SXin Li /// \brief Create a reference to a namespace or namespace alias at the given
109*67e74705SXin Li /// location.
110*67e74705SXin Li CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc,
111*67e74705SXin Li                                 CXTranslationUnit TU);
112*67e74705SXin Li 
113*67e74705SXin Li /// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias
114*67e74705SXin Li /// it references and the location where the reference occurred.
115*67e74705SXin Li std::pair<const NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
116*67e74705SXin Li 
117*67e74705SXin Li /// \brief Create a reference to a variable at the given location.
118*67e74705SXin Li CXCursor MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
119*67e74705SXin Li                                CXTranslationUnit TU);
120*67e74705SXin Li 
121*67e74705SXin Li /// \brief Unpack a VariableRef cursor into the variable it references and the
122*67e74705SXin Li /// location where the where the reference occurred.
123*67e74705SXin Li std::pair<const VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C);
124*67e74705SXin Li 
125*67e74705SXin Li /// \brief Create a reference to a field at the given location.
126*67e74705SXin Li CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
127*67e74705SXin Li                              CXTranslationUnit TU);
128*67e74705SXin Li 
129*67e74705SXin Li /// \brief Unpack a MemberRef cursor into the field it references and the
130*67e74705SXin Li /// location where the reference occurred.
131*67e74705SXin Li std::pair<const FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
132*67e74705SXin Li 
133*67e74705SXin Li /// \brief Create a CXX base specifier cursor.
134*67e74705SXin Li CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
135*67e74705SXin Li                                     CXTranslationUnit TU);
136*67e74705SXin Li 
137*67e74705SXin Li /// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
138*67e74705SXin Li const CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
139*67e74705SXin Li 
140*67e74705SXin Li /// \brief Create a preprocessing directive cursor.
141*67e74705SXin Li CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
142*67e74705SXin Li                                           CXTranslationUnit TU);
143*67e74705SXin Li 
144*67e74705SXin Li /// \brief Unpack a given preprocessing directive to retrieve its source range.
145*67e74705SXin Li SourceRange getCursorPreprocessingDirective(CXCursor C);
146*67e74705SXin Li 
147*67e74705SXin Li /// \brief Create a macro definition cursor.
148*67e74705SXin Li CXCursor MakeMacroDefinitionCursor(const MacroDefinitionRecord *,
149*67e74705SXin Li                                    CXTranslationUnit TU);
150*67e74705SXin Li 
151*67e74705SXin Li /// \brief Unpack a given macro definition cursor to retrieve its
152*67e74705SXin Li /// source range.
153*67e74705SXin Li const MacroDefinitionRecord *getCursorMacroDefinition(CXCursor C);
154*67e74705SXin Li 
155*67e74705SXin Li /// \brief Create a macro expansion cursor.
156*67e74705SXin Li CXCursor MakeMacroExpansionCursor(MacroExpansion *, CXTranslationUnit TU);
157*67e74705SXin Li 
158*67e74705SXin Li /// \brief Create a "pseudo" macro expansion cursor, using a macro definition
159*67e74705SXin Li /// and a source location.
160*67e74705SXin Li CXCursor MakeMacroExpansionCursor(MacroDefinitionRecord *, SourceLocation Loc,
161*67e74705SXin Li                                   CXTranslationUnit TU);
162*67e74705SXin Li 
163*67e74705SXin Li /// \brief Wraps a macro expansion cursor and provides a common interface
164*67e74705SXin Li /// for a normal macro expansion cursor or a "pseudo" one.
165*67e74705SXin Li ///
166*67e74705SXin Li /// "Pseudo" macro expansion cursors (essentially a macro definition along with
167*67e74705SXin Li /// a source location) are created in special cases, for example they can be
168*67e74705SXin Li /// created for identifiers inside macro definitions, if these identifiers are
169*67e74705SXin Li /// macro names.
170*67e74705SXin Li class MacroExpansionCursor {
171*67e74705SXin Li   CXCursor C;
172*67e74705SXin Li 
isPseudo()173*67e74705SXin Li   bool isPseudo() const { return C.data[1] != nullptr; }
getAsMacroDefinition()174*67e74705SXin Li   const MacroDefinitionRecord *getAsMacroDefinition() const {
175*67e74705SXin Li     assert(isPseudo());
176*67e74705SXin Li     return static_cast<const MacroDefinitionRecord *>(C.data[0]);
177*67e74705SXin Li   }
getAsMacroExpansion()178*67e74705SXin Li   const MacroExpansion *getAsMacroExpansion() const {
179*67e74705SXin Li     assert(!isPseudo());
180*67e74705SXin Li     return static_cast<const MacroExpansion *>(C.data[0]);
181*67e74705SXin Li   }
getPseudoLoc()182*67e74705SXin Li   SourceLocation getPseudoLoc() const {
183*67e74705SXin Li     assert(isPseudo());
184*67e74705SXin Li     return SourceLocation::getFromPtrEncoding(C.data[1]);
185*67e74705SXin Li   }
186*67e74705SXin Li 
187*67e74705SXin Li public:
MacroExpansionCursor(CXCursor C)188*67e74705SXin Li   MacroExpansionCursor(CXCursor C) : C(C) {
189*67e74705SXin Li     assert(C.kind == CXCursor_MacroExpansion);
190*67e74705SXin Li   }
191*67e74705SXin Li 
192*67e74705SXin Li   const IdentifierInfo *getName() const;
193*67e74705SXin Li   const MacroDefinitionRecord *getDefinition() const;
194*67e74705SXin Li   SourceRange getSourceRange() const;
195*67e74705SXin Li };
196*67e74705SXin Li 
197*67e74705SXin Li /// \brief Unpack a given macro expansion cursor to retrieve its info.
getCursorMacroExpansion(CXCursor C)198*67e74705SXin Li static inline MacroExpansionCursor getCursorMacroExpansion(CXCursor C) {
199*67e74705SXin Li   return C;
200*67e74705SXin Li }
201*67e74705SXin Li 
202*67e74705SXin Li /// \brief Create an inclusion directive cursor.
203*67e74705SXin Li CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
204*67e74705SXin Li                                       CXTranslationUnit TU);
205*67e74705SXin Li 
206*67e74705SXin Li /// \brief Unpack a given inclusion directive cursor to retrieve its
207*67e74705SXin Li /// source range.
208*67e74705SXin Li const InclusionDirective *getCursorInclusionDirective(CXCursor C);
209*67e74705SXin Li 
210*67e74705SXin Li /// \brief Create a label reference at the given location.
211*67e74705SXin Li CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
212*67e74705SXin Li                             CXTranslationUnit TU);
213*67e74705SXin Li 
214*67e74705SXin Li /// \brief Unpack a label reference into the label statement it refers to and
215*67e74705SXin Li /// the location of the reference.
216*67e74705SXin Li std::pair<const LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
217*67e74705SXin Li 
218*67e74705SXin Li /// \brief Create a overloaded declaration reference cursor for an expression.
219*67e74705SXin Li CXCursor MakeCursorOverloadedDeclRef(const OverloadExpr *E,
220*67e74705SXin Li                                      CXTranslationUnit TU);
221*67e74705SXin Li 
222*67e74705SXin Li /// \brief Create a overloaded declaration reference cursor for a declaration.
223*67e74705SXin Li CXCursor MakeCursorOverloadedDeclRef(const Decl *D, SourceLocation Location,
224*67e74705SXin Li                                      CXTranslationUnit TU);
225*67e74705SXin Li 
226*67e74705SXin Li /// \brief Create a overloaded declaration reference cursor for a template name.
227*67e74705SXin Li CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
228*67e74705SXin Li                                      SourceLocation Location,
229*67e74705SXin Li                                      CXTranslationUnit TU);
230*67e74705SXin Li 
231*67e74705SXin Li /// \brief Internal storage for an overloaded declaration reference cursor;
232*67e74705SXin Li typedef llvm::PointerUnion3<const OverloadExpr *, const Decl *,
233*67e74705SXin Li                             OverloadedTemplateStorage *>
234*67e74705SXin Li   OverloadedDeclRefStorage;
235*67e74705SXin Li 
236*67e74705SXin Li /// \brief Unpack an overloaded declaration reference into an expression,
237*67e74705SXin Li /// declaration, or template name along with the source location.
238*67e74705SXin Li std::pair<OverloadedDeclRefStorage, SourceLocation>
239*67e74705SXin Li   getCursorOverloadedDeclRef(CXCursor C);
240*67e74705SXin Li 
241*67e74705SXin Li const Decl *getCursorDecl(CXCursor Cursor);
242*67e74705SXin Li const Expr *getCursorExpr(CXCursor Cursor);
243*67e74705SXin Li const Stmt *getCursorStmt(CXCursor Cursor);
244*67e74705SXin Li const Attr *getCursorAttr(CXCursor Cursor);
245*67e74705SXin Li const Decl *getCursorParentDecl(CXCursor Cursor);
246*67e74705SXin Li 
247*67e74705SXin Li ASTContext &getCursorContext(CXCursor Cursor);
248*67e74705SXin Li ASTUnit *getCursorASTUnit(CXCursor Cursor);
249*67e74705SXin Li CXTranslationUnit getCursorTU(CXCursor Cursor);
250*67e74705SXin Li 
251*67e74705SXin Li void getOverriddenCursors(CXCursor cursor,
252*67e74705SXin Li                           SmallVectorImpl<CXCursor> &overridden);
253*67e74705SXin Li 
254*67e74705SXin Li /// \brief Create an opaque  pool used for fast generation of overriden
255*67e74705SXin Li /// CXCursor arrays.
256*67e74705SXin Li void *createOverridenCXCursorsPool();
257*67e74705SXin Li 
258*67e74705SXin Li /// \brief Dispose of the overriden CXCursors pool.
259*67e74705SXin Li void disposeOverridenCXCursorsPool(void *pool);
260*67e74705SXin Li 
261*67e74705SXin Li /// \brief Returns a index/location pair for a selector identifier if the cursor
262*67e74705SXin Li /// points to one.
263*67e74705SXin Li std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
getSelectorIdentifierIndex(CXCursor cursor)264*67e74705SXin Li static inline int getSelectorIdentifierIndex(CXCursor cursor) {
265*67e74705SXin Li   return getSelectorIdentifierIndexAndLoc(cursor).first;
266*67e74705SXin Li }
getSelectorIdentifierLoc(CXCursor cursor)267*67e74705SXin Li static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
268*67e74705SXin Li   return getSelectorIdentifierIndexAndLoc(cursor).second;
269*67e74705SXin Li }
270*67e74705SXin Li 
271*67e74705SXin Li CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
272*67e74705SXin Li 
getTypeRefedCallExprCursor(CXCursor cursor)273*67e74705SXin Li static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
274*67e74705SXin Li   CXCursor newCursor = cursor;
275*67e74705SXin Li   if (cursor.kind == CXCursor_CallExpr)
276*67e74705SXin Li     newCursor.xdata = 1;
277*67e74705SXin Li   return newCursor;
278*67e74705SXin Li }
279*67e74705SXin Li 
280*67e74705SXin Li CXCursor getTypeRefCursor(CXCursor cursor);
281*67e74705SXin Li 
282*67e74705SXin Li /// \brief Generate a USR for \arg D and put it in \arg Buf.
283*67e74705SXin Li /// \returns true if no USR was computed or the result should be ignored,
284*67e74705SXin Li /// false otherwise.
285*67e74705SXin Li bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
286*67e74705SXin Li 
287*67e74705SXin Li bool operator==(CXCursor X, CXCursor Y);
288*67e74705SXin Li 
289*67e74705SXin Li inline bool operator!=(CXCursor X, CXCursor Y) {
290*67e74705SXin Li   return !(X == Y);
291*67e74705SXin Li }
292*67e74705SXin Li 
293*67e74705SXin Li /// \brief Return true if the cursor represents a declaration that is the
294*67e74705SXin Li /// first in a declaration group.
295*67e74705SXin Li bool isFirstInDeclGroup(CXCursor C);
296*67e74705SXin Li 
297*67e74705SXin Li }} // end namespace: clang::cxcursor
298*67e74705SXin Li 
299*67e74705SXin Li #endif
300