blob: 86fb97524394263770554534c9e3481e282cf49a [file] [log] [blame]
Ted Kremenek16c440a2010-01-15 20:35:54 +00001//===- CXCursor.h - Routines for manipulating CXCursors -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines routines for manipulating CXCursors.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_CXCURSOR_H
Ted Kremenek2cd10b02010-01-25 21:09:34 +000015#define LLVM_CLANG_CXCURSOR_H
Ted Kremenek16c440a2010-01-15 20:35:54 +000016
17#include "clang-c/Index.h"
Douglas Gregor2e331b92010-01-16 14:00:32 +000018#include "clang/Basic/SourceLocation.h"
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000019#include "llvm/ADT/PointerUnion.h"
Douglas Gregor2e331b92010-01-16 14:00:32 +000020#include <utility>
Ted Kremenek16c440a2010-01-15 20:35:54 +000021
22namespace clang {
23
Douglas Gregorf46034a2010-01-18 23:41:10 +000024class ASTContext;
Douglas Gregorb2cd4872010-01-20 23:57:43 +000025class ASTUnit;
Ted Kremeneke77f4432010-02-18 03:09:07 +000026class Attr;
Ted Kremenek3064ef92010-08-27 21:34:58 +000027class CXXBaseSpecifier;
Ted Kremenek16c440a2010-01-15 20:35:54 +000028class Decl;
Douglas Gregor283cae32010-01-15 21:56:13 +000029class Expr;
Douglas Gregora67e03f2010-09-09 21:42:20 +000030class FieldDecl;
Douglas Gregorecdcb882010-10-20 22:00:55 +000031class InclusionDirective;
Douglas Gregor36897b02010-09-10 00:22:18 +000032class LabelStmt;
Douglas Gregor572feb22010-03-18 18:04:21 +000033class MacroDefinition;
Chandler Carruth9e5bb852011-07-14 08:20:46 +000034class MacroExpansion;
Douglas Gregor283cae32010-01-15 21:56:13 +000035class NamedDecl;
Douglas Gregor2e331b92010-01-16 14:00:32 +000036class ObjCInterfaceDecl;
Douglas Gregor78db0cd2010-01-16 15:44:18 +000037class ObjCProtocolDecl;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000038class OverloadedTemplateStorage;
39class OverloadExpr;
Ted Kremenek16c440a2010-01-15 20:35:54 +000040class Stmt;
Douglas Gregor0b36e612010-08-31 20:37:03 +000041class TemplateDecl;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000042class TemplateName;
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000043class TypeDecl;
Douglas Gregor011d8b92012-02-15 00:54:55 +000044class VarDecl;
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +000045class IdentifierInfo;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000046
Ted Kremenek16c440a2010-01-15 20:35:54 +000047namespace cxcursor {
Argyrios Kyrtzidis671436e2011-09-27 00:30:33 +000048
49CXCursor getCursor(CXTranslationUnit, SourceLocation);
Ted Kremenek16c440a2010-01-15 20:35:54 +000050
Ted Kremeneka60ed472010-11-16 08:15:36 +000051CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent,
52 CXTranslationUnit TU);
53CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000054 SourceRange RegionOfInterest = SourceRange(),
Ted Kremenek007a7c92010-11-01 23:26:51 +000055 bool FirstInDeclGroup = true);
Ted Kremeneka60ed472010-11-16 08:15:36 +000056CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000057 CXTranslationUnit TU,
58 SourceRange RegionOfInterest = SourceRange());
Ted Kremenekbbf66ca2012-04-30 19:06:49 +000059CXCursor MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU = 0);
Ted Kremenek16c440a2010-01-15 20:35:54 +000060
Douglas Gregor2e331b92010-01-16 14:00:32 +000061/// \brief Create an Objective-C superclass reference at the given location.
62CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +000063 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000064 CXTranslationUnit TU);
Douglas Gregor2e331b92010-01-16 14:00:32 +000065
66/// \brief Unpack an ObjCSuperClassRef cursor into the interface it references
67/// and optionally the location where the reference occurred.
68std::pair<ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +000069 getCursorObjCSuperClassRef(CXCursor C);
70
71/// \brief Create an Objective-C protocol reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000072CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
73 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000074 CXTranslationUnit TU);
Douglas Gregor78db0cd2010-01-16 15:44:18 +000075
76/// \brief Unpack an ObjCProtocolRef cursor into the protocol it references
77/// and optionally the location where the reference occurred.
78std::pair<ObjCProtocolDecl *, SourceLocation>
79 getCursorObjCProtocolRef(CXCursor C);
Douglas Gregor2e331b92010-01-16 14:00:32 +000080
Douglas Gregor1adb0822010-01-16 17:14:40 +000081/// \brief Create an Objective-C class reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000082CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
83 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000084 CXTranslationUnit TU);
Douglas Gregor1adb0822010-01-16 17:14:40 +000085
86/// \brief Unpack an ObjCClassRef cursor into the class it references
87/// and optionally the location where the reference occurred.
88std::pair<ObjCInterfaceDecl *, SourceLocation>
89 getCursorObjCClassRef(CXCursor C);
90
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000091/// \brief Create a type reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000092CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000093 CXTranslationUnit TU);
Douglas Gregor0b36e612010-08-31 20:37:03 +000094
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000095/// \brief Unpack a TypeRef cursor into the class it references
96/// and optionally the location where the reference occurred.
97std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
98
Douglas Gregor0b36e612010-08-31 20:37:03 +000099/// \brief Create a reference to a template at the given location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000100CXCursor MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000101 CXTranslationUnit TU);
Douglas Gregor0b36e612010-08-31 20:37:03 +0000102
103/// \brief Unpack a TemplateRef cursor into the template it references and
104/// the location where the reference occurred.
105std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C);
Douglas Gregor69319002010-08-31 23:48:11 +0000106
107/// \brief Create a reference to a namespace or namespace alias at the given
108/// location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000109CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000110 CXTranslationUnit TU);
Douglas Gregor69319002010-08-31 23:48:11 +0000111
112/// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias
113/// it references and the location where the reference occurred.
114std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
115
Douglas Gregor011d8b92012-02-15 00:54:55 +0000116/// \brief Create a reference to a variable at the given location.
117CXCursor MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
118 CXTranslationUnit TU);
119
120/// \brief Unpack a VariableRef cursor into the variable it references and the
121/// location where the where the reference occurred.
122std::pair<VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C);
123
Douglas Gregora67e03f2010-09-09 21:42:20 +0000124/// \brief Create a reference to a field at the given location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000125CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000126 CXTranslationUnit TU);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000127
128/// \brief Unpack a MemberRef cursor into the field it references and the
129/// location where the reference occurred.
130std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
131
Ted Kremenek3064ef92010-08-27 21:34:58 +0000132/// \brief Create a CXX base specifier cursor.
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000133CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000134 CXTranslationUnit TU);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000135
136/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
137CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
138
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000139/// \brief Create a preprocessing directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000140CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
141 CXTranslationUnit TU);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000142
143/// \brief Unpack a given preprocessing directive to retrieve its source range.
144SourceRange getCursorPreprocessingDirective(CXCursor C);
Douglas Gregor48072312010-03-18 15:23:44 +0000145
Douglas Gregor572feb22010-03-18 18:04:21 +0000146/// \brief Create a macro definition cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000147CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU);
Douglas Gregor572feb22010-03-18 18:04:21 +0000148
149/// \brief Unpack a given macro definition cursor to retrieve its
150/// source range.
151MacroDefinition *getCursorMacroDefinition(CXCursor C);
152
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000153/// \brief Create a macro expansion cursor.
154CXCursor MakeMacroExpansionCursor(MacroExpansion *,
155 CXTranslationUnit TU);
Douglas Gregor48072312010-03-18 15:23:44 +0000156
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000157/// \brief Create a "pseudo" macro expansion cursor, using a macro definition
158/// and a source location.
159CXCursor MakeMacroExpansionCursor(MacroDefinition *, SourceLocation Loc,
160 CXTranslationUnit TU);
161
162/// \brief Wraps a macro expansion cursor and provides a common interface
163/// for a normal macro expansion cursor or a "pseudo" one.
164///
165/// "Pseudo" macro expansion cursors (essentially a macro definition along with
166/// a source location) are created in special cases, for example they can be
167/// created for identifiers inside macro definitions, if these identifiers are
168/// macro names.
169class MacroExpansionCursor {
170 CXCursor C;
171
172 bool isPseudo() const {
173 return C.data[1] != 0;
174 }
175 MacroDefinition *getAsMacroDefinition() const {
176 assert(isPseudo());
177 return static_cast<MacroDefinition *>(C.data[0]);
178 }
179 MacroExpansion *getAsMacroExpansion() const {
180 assert(!isPseudo());
181 return static_cast<MacroExpansion *>(C.data[0]);
182 }
183 SourceLocation getPseudoLoc() const {
184 assert(isPseudo());
185 return SourceLocation::getFromPtrEncoding(C.data[1]);
186 }
187
188public:
189 MacroExpansionCursor(CXCursor C) : C(C) {
190 assert(C.kind == CXCursor_MacroExpansion);
191 }
192
193 const IdentifierInfo *getName() const;
194 MacroDefinition *getDefinition() const;
195 SourceRange getSourceRange() const;
196};
197
198/// \brief Unpack a given macro expansion cursor to retrieve its info.
199static inline MacroExpansionCursor getCursorMacroExpansion(CXCursor C) {
200 return C;
201}
Douglas Gregor48072312010-03-18 15:23:44 +0000202
Douglas Gregorecdcb882010-10-20 22:00:55 +0000203/// \brief Create an inclusion directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000204CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
205 CXTranslationUnit TU);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000206
207/// \brief Unpack a given inclusion directive cursor to retrieve its
208/// source range.
209InclusionDirective *getCursorInclusionDirective(CXCursor C);
210
Douglas Gregor36897b02010-09-10 00:22:18 +0000211/// \brief Create a label reference at the given location.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000212CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
213 CXTranslationUnit TU);
Douglas Gregor36897b02010-09-10 00:22:18 +0000214
215/// \brief Unpack a label reference into the label statement it refers to and
216/// the location of the reference.
217std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000218
219/// \brief Create a overloaded declaration reference cursor for an expression.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000220CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000221
222/// \brief Create a overloaded declaration reference cursor for a declaration.
223CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000224 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000225
226/// \brief Create a overloaded declaration reference cursor for a template name.
227CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000228 SourceLocation Location,
229 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000230
231/// \brief Internal storage for an overloaded declaration reference cursor;
232typedef llvm::PointerUnion3<OverloadExpr *, Decl *,
233 OverloadedTemplateStorage *>
234 OverloadedDeclRefStorage;
235
236/// \brief Unpack an overloaded declaration reference into an expression,
237/// declaration, or template name along with the source location.
238std::pair<OverloadedDeclRefStorage, SourceLocation>
239 getCursorOverloadedDeclRef(CXCursor C);
Douglas Gregor36897b02010-09-10 00:22:18 +0000240
Douglas Gregor283cae32010-01-15 21:56:13 +0000241Decl *getCursorDecl(CXCursor Cursor);
242Expr *getCursorExpr(CXCursor Cursor);
243Stmt *getCursorStmt(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000244Attr *getCursorAttr(CXCursor Cursor);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000245Decl *getCursorParentDecl(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000246
Douglas Gregorf46034a2010-01-18 23:41:10 +0000247ASTContext &getCursorContext(CXCursor Cursor);
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000248ASTUnit *getCursorASTUnit(CXCursor Cursor);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000249CXTranslationUnit getCursorTU(CXCursor Cursor);
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000250
251void getOverriddenCursors(CXCursor cursor,
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000252 SmallVectorImpl<CXCursor> &overridden);
253
254/// \brief Create an opaque pool used for fast generation of overriden
255/// CXCursor arrays.
256void *createOverridenCXCursorsPool();
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000257
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000258/// \brief Dispose of the overriden CXCursors pool.
259void disposeOverridenCXCursorsPool(void *pool);
260
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000261/// \brief Returns a index/location pair for a selector identifier if the cursor
262/// points to one.
263std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
264static inline int getSelectorIdentifierIndex(CXCursor cursor) {
265 return getSelectorIdentifierIndexAndLoc(cursor).first;
266}
267static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
268 return getSelectorIdentifierIndexAndLoc(cursor).second;
269}
270
271CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
272
273static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
274 CXCursor newCursor = cursor;
275 if (cursor.kind == CXCursor_CallExpr)
276 newCursor.xdata = 1;
277 return newCursor;
278}
279
280CXCursor getTypeRefCursor(CXCursor cursor);
281
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000282/// \brief Generate a USR for \arg D and put it in \arg Buf.
283/// \returns true if no USR was computed or the result should be ignored,
284/// false otherwise.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000285bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000286
Douglas Gregor283cae32010-01-15 21:56:13 +0000287bool operator==(CXCursor X, CXCursor Y);
288
289inline bool operator!=(CXCursor X, CXCursor Y) {
290 return !(X == Y);
291}
292
Ted Kremenek007a7c92010-11-01 23:26:51 +0000293/// \brief Return true if the cursor represents a declaration that is the
294/// first in a declaration group.
295bool isFirstInDeclGroup(CXCursor C);
296
Ted Kremenek16c440a2010-01-15 20:35:54 +0000297}} // end namespace: clang::cxcursor
298
299#endif