blob: 931d112766ed8ee1c23a3b48ca0469027126d484 [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
Stephen Hines176edba2014-12-01 14:53:08 -080014#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
15#define LLVM_CLANG_TOOLS_LIBCLANG_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
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000051CXCursor MakeCXCursor(const clang::Attr *A, const clang::Decl *Parent,
Ted Kremeneka60ed472010-11-16 08:15:36 +000052 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 Gribenko05756dc2013-01-14 00:46:27 +000056CXCursor MakeCXCursor(const clang::Stmt *S, const clang::Decl *Parent,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000057 CXTranslationUnit TU,
58 SourceRange RegionOfInterest = SourceRange());
Stephen Hinesc568f1e2014-07-21 00:47:37 -070059CXCursor MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU = nullptr);
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 {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700175 return C.data[1] != nullptr;
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000176 }
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.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000222CXCursor MakeCursorOverloadedDeclRef(const OverloadExpr *E,
223 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000224
225/// \brief Create a overloaded declaration reference cursor for a declaration.
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000226CXCursor MakeCursorOverloadedDeclRef(const Decl *D, SourceLocation Location,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000227 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000228
229/// \brief Create a overloaded declaration reference cursor for a template name.
230CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000231 SourceLocation Location,
232 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000233
234/// \brief Internal storage for an overloaded declaration reference cursor;
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000235typedef llvm::PointerUnion3<const OverloadExpr *, const Decl *,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000236 OverloadedTemplateStorage *>
237 OverloadedDeclRefStorage;
238
239/// \brief Unpack an overloaded declaration reference into an expression,
240/// declaration, or template name along with the source location.
241std::pair<OverloadedDeclRefStorage, SourceLocation>
242 getCursorOverloadedDeclRef(CXCursor C);
Douglas Gregor36897b02010-09-10 00:22:18 +0000243
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000244const Decl *getCursorDecl(CXCursor Cursor);
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000245const Expr *getCursorExpr(CXCursor Cursor);
246const Stmt *getCursorStmt(CXCursor Cursor);
Dmitri Gribenko7d914382013-01-26 18:08:08 +0000247const Attr *getCursorAttr(CXCursor Cursor);
Dmitri Gribenko404628c2013-01-26 18:12:08 +0000248const Decl *getCursorParentDecl(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000249
Douglas Gregorf46034a2010-01-18 23:41:10 +0000250ASTContext &getCursorContext(CXCursor Cursor);
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000251ASTUnit *getCursorASTUnit(CXCursor Cursor);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000252CXTranslationUnit getCursorTU(CXCursor Cursor);
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000253
254void getOverriddenCursors(CXCursor cursor,
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000255 SmallVectorImpl<CXCursor> &overridden);
256
257/// \brief Create an opaque pool used for fast generation of overriden
258/// CXCursor arrays.
259void *createOverridenCXCursorsPool();
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000260
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000261/// \brief Dispose of the overriden CXCursors pool.
262void disposeOverridenCXCursorsPool(void *pool);
263
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000264/// \brief Returns a index/location pair for a selector identifier if the cursor
265/// points to one.
266std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
267static inline int getSelectorIdentifierIndex(CXCursor cursor) {
268 return getSelectorIdentifierIndexAndLoc(cursor).first;
269}
270static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
271 return getSelectorIdentifierIndexAndLoc(cursor).second;
272}
273
274CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
275
276static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
277 CXCursor newCursor = cursor;
278 if (cursor.kind == CXCursor_CallExpr)
279 newCursor.xdata = 1;
280 return newCursor;
281}
282
283CXCursor getTypeRefCursor(CXCursor cursor);
284
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000285/// \brief Generate a USR for \arg D and put it in \arg Buf.
286/// \returns true if no USR was computed or the result should be ignored,
287/// false otherwise.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000288bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000289
Douglas Gregor283cae32010-01-15 21:56:13 +0000290bool operator==(CXCursor X, CXCursor Y);
291
292inline bool operator!=(CXCursor X, CXCursor Y) {
293 return !(X == Y);
294}
295
Ted Kremenek007a7c92010-11-01 23:26:51 +0000296/// \brief Return true if the cursor represents a declaration that is the
297/// first in a declaration group.
298bool isFirstInDeclGroup(CXCursor C);
299
Ted Kremenek16c440a2010-01-15 20:35:54 +0000300}} // end namespace: clang::cxcursor
301
302#endif