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