blob: 79892f4d29f56cabcb83380c0319697fbe7bb0bd [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);
Dmitri Gribenko67812b22013-01-11 21:01:49 +000053CXCursor MakeCXCursor(const 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);
Dmitri Gribenko67812b22013-01-11 21:01:49 +000056CXCursor MakeCXCursor(const 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.
Dmitri Gribenko67812b22013-01-11 21:01:49 +000068std::pair<const 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.
Dmitri Gribenko67812b22013-01-11 21:01:49 +000078std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +000079 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.
Dmitri Gribenko67812b22013-01-11 21:01:49 +000088std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor1adb0822010-01-16 17:14:40 +000089 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.
Dmitri Gribenko67812b22013-01-11 21:01:49 +000097std::pair<const TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000098
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.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000105std::pair<const TemplateDecl *, SourceLocation>
106 getCursorTemplateRef(CXCursor C);
Douglas Gregor69319002010-08-31 23:48:11 +0000107
108/// \brief Create a reference to a namespace or namespace alias at the given
109/// location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000110CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000111 CXTranslationUnit TU);
Douglas Gregor69319002010-08-31 23:48:11 +0000112
113/// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias
114/// it references and the location where the reference occurred.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000115std::pair<const NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
Douglas Gregor69319002010-08-31 23:48:11 +0000116
Douglas Gregor011d8b92012-02-15 00:54:55 +0000117/// \brief Create a reference to a variable at the given location.
118CXCursor MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
119 CXTranslationUnit TU);
120
121/// \brief Unpack a VariableRef cursor into the variable it references and the
122/// location where the where the reference occurred.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000123std::pair<const VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C);
Douglas Gregor011d8b92012-02-15 00:54:55 +0000124
Douglas Gregora67e03f2010-09-09 21:42:20 +0000125/// \brief Create a reference to a field at the given location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000126CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000127 CXTranslationUnit TU);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000128
129/// \brief Unpack a MemberRef cursor into the field it references and the
130/// location where the reference occurred.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000131std::pair<const FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000132
Ted Kremenek3064ef92010-08-27 21:34:58 +0000133/// \brief Create a CXX base specifier cursor.
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000134CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000135 CXTranslationUnit TU);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000136
137/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000138const CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000139
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000140/// \brief Create a preprocessing directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000141CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
142 CXTranslationUnit TU);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000143
144/// \brief Unpack a given preprocessing directive to retrieve its source range.
145SourceRange getCursorPreprocessingDirective(CXCursor C);
Douglas Gregor48072312010-03-18 15:23:44 +0000146
Douglas Gregor572feb22010-03-18 18:04:21 +0000147/// \brief Create a macro definition cursor.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000148CXCursor MakeMacroDefinitionCursor(const MacroDefinition *,
149 CXTranslationUnit TU);
Douglas Gregor572feb22010-03-18 18:04:21 +0000150
151/// \brief Unpack a given macro definition cursor to retrieve its
152/// source range.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000153const MacroDefinition *getCursorMacroDefinition(CXCursor C);
Douglas Gregor572feb22010-03-18 18:04:21 +0000154
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000155/// \brief Create a macro expansion cursor.
156CXCursor MakeMacroExpansionCursor(MacroExpansion *,
157 CXTranslationUnit TU);
Douglas Gregor48072312010-03-18 15:23:44 +0000158
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000159/// \brief Create a "pseudo" macro expansion cursor, using a macro definition
160/// and a source location.
161CXCursor MakeMacroExpansionCursor(MacroDefinition *, SourceLocation Loc,
162 CXTranslationUnit TU);
163
164/// \brief Wraps a macro expansion cursor and provides a common interface
165/// for a normal macro expansion cursor or a "pseudo" one.
166///
167/// "Pseudo" macro expansion cursors (essentially a macro definition along with
168/// a source location) are created in special cases, for example they can be
169/// created for identifiers inside macro definitions, if these identifiers are
170/// macro names.
171class MacroExpansionCursor {
172 CXCursor C;
173
174 bool isPseudo() const {
175 return C.data[1] != 0;
176 }
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000177 const MacroDefinition *getAsMacroDefinition() const {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000178 assert(isPseudo());
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000179 return static_cast<const MacroDefinition *>(C.data[0]);
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000180 }
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000181 const MacroExpansion *getAsMacroExpansion() const {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000182 assert(!isPseudo());
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000183 return static_cast<const MacroExpansion *>(C.data[0]);
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000184 }
185 SourceLocation getPseudoLoc() const {
186 assert(isPseudo());
187 return SourceLocation::getFromPtrEncoding(C.data[1]);
188 }
189
190public:
191 MacroExpansionCursor(CXCursor C) : C(C) {
192 assert(C.kind == CXCursor_MacroExpansion);
193 }
194
195 const IdentifierInfo *getName() const;
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000196 const MacroDefinition *getDefinition() const;
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000197 SourceRange getSourceRange() const;
198};
199
200/// \brief Unpack a given macro expansion cursor to retrieve its info.
201static inline MacroExpansionCursor getCursorMacroExpansion(CXCursor C) {
202 return C;
203}
Douglas Gregor48072312010-03-18 15:23:44 +0000204
Douglas Gregorecdcb882010-10-20 22:00:55 +0000205/// \brief Create an inclusion directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000206CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
207 CXTranslationUnit TU);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000208
209/// \brief Unpack a given inclusion directive cursor to retrieve its
210/// source range.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000211const InclusionDirective *getCursorInclusionDirective(CXCursor C);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000212
Douglas Gregor36897b02010-09-10 00:22:18 +0000213/// \brief Create a label reference at the given location.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000214CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
215 CXTranslationUnit TU);
Douglas Gregor36897b02010-09-10 00:22:18 +0000216
217/// \brief Unpack a label reference into the label statement it refers to and
218/// the location of the reference.
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000219std::pair<const LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000220
221/// \brief Create a overloaded declaration reference cursor for an expression.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000222CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000223
224/// \brief Create a overloaded declaration reference cursor for a declaration.
225CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000226 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000227
228/// \brief Create a overloaded declaration reference cursor for a template name.
229CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000230 SourceLocation Location,
231 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000232
233/// \brief Internal storage for an overloaded declaration reference cursor;
234typedef llvm::PointerUnion3<OverloadExpr *, Decl *,
235 OverloadedTemplateStorage *>
236 OverloadedDeclRefStorage;
237
238/// \brief Unpack an overloaded declaration reference into an expression,
239/// declaration, or template name along with the source location.
240std::pair<OverloadedDeclRefStorage, SourceLocation>
241 getCursorOverloadedDeclRef(CXCursor C);
Douglas Gregor36897b02010-09-10 00:22:18 +0000242
Douglas Gregor283cae32010-01-15 21:56:13 +0000243Decl *getCursorDecl(CXCursor Cursor);
244Expr *getCursorExpr(CXCursor Cursor);
245Stmt *getCursorStmt(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000246Attr *getCursorAttr(CXCursor Cursor);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000247Decl *getCursorParentDecl(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000248
Douglas Gregorf46034a2010-01-18 23:41:10 +0000249ASTContext &getCursorContext(CXCursor Cursor);
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000250ASTUnit *getCursorASTUnit(CXCursor Cursor);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000251CXTranslationUnit getCursorTU(CXCursor Cursor);
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000252
253void getOverriddenCursors(CXCursor cursor,
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000254 SmallVectorImpl<CXCursor> &overridden);
255
256/// \brief Create an opaque pool used for fast generation of overriden
257/// CXCursor arrays.
258void *createOverridenCXCursorsPool();
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000259
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000260/// \brief Dispose of the overriden CXCursors pool.
261void disposeOverridenCXCursorsPool(void *pool);
262
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000263/// \brief Returns a index/location pair for a selector identifier if the cursor
264/// points to one.
265std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
266static inline int getSelectorIdentifierIndex(CXCursor cursor) {
267 return getSelectorIdentifierIndexAndLoc(cursor).first;
268}
269static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
270 return getSelectorIdentifierIndexAndLoc(cursor).second;
271}
272
273CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
274
275static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
276 CXCursor newCursor = cursor;
277 if (cursor.kind == CXCursor_CallExpr)
278 newCursor.xdata = 1;
279 return newCursor;
280}
281
282CXCursor getTypeRefCursor(CXCursor cursor);
283
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000284/// \brief Generate a USR for \arg D and put it in \arg Buf.
285/// \returns true if no USR was computed or the result should be ignored,
286/// false otherwise.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000287bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000288
Douglas Gregor283cae32010-01-15 21:56:13 +0000289bool operator==(CXCursor X, CXCursor Y);
290
291inline bool operator!=(CXCursor X, CXCursor Y) {
292 return !(X == Y);
293}
294
Ted Kremenek007a7c92010-11-01 23:26:51 +0000295/// \brief Return true if the cursor represents a declaration that is the
296/// first in a declaration group.
297bool isFirstInDeclGroup(CXCursor C);
298
Ted Kremenek16c440a2010-01-15 20:35:54 +0000299}} // end namespace: clang::cxcursor
300
301#endif