blob: 083b86934d1657a429df81e4095480807f16468d [file] [log] [blame]
Ted Kremenek87553c42010-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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
15#define LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
Ted Kremenek87553c42010-01-15 20:35:54 +000016
17#include "clang-c/Index.h"
Douglas Gregor6c8959b2010-01-16 14:00:32 +000018#include "clang/Basic/SourceLocation.h"
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000019#include "llvm/ADT/PointerUnion.h"
Douglas Gregor6c8959b2010-01-16 14:00:32 +000020#include <utility>
Ted Kremenek87553c42010-01-15 20:35:54 +000021
22namespace clang {
23
Douglas Gregor7ecd0202010-01-18 23:41:10 +000024class ASTContext;
Douglas Gregorfed36b12010-01-20 23:57:43 +000025class ASTUnit;
Ted Kremenekbff31432010-02-18 03:09:07 +000026class Attr;
Ted Kremenekae9e2212010-08-27 21:34:58 +000027class CXXBaseSpecifier;
Ted Kremenek87553c42010-01-15 20:35:54 +000028class Decl;
Douglas Gregorc58d05b2010-01-15 21:56:13 +000029class Expr;
Douglas Gregorf3af3112010-09-09 21:42:20 +000030class FieldDecl;
Douglas Gregor796d76a2010-10-20 22:00:55 +000031class InclusionDirective;
Douglas Gregora93ab662010-09-10 00:22:18 +000032class LabelStmt;
Richard Smith66a81862015-05-04 02:25:31 +000033class MacroDefinitionRecord;
Chandler Carrutha88a22182011-07-14 08:20:46 +000034class MacroExpansion;
Douglas Gregorc58d05b2010-01-15 21:56:13 +000035class NamedDecl;
Douglas Gregor6c8959b2010-01-16 14:00:32 +000036class ObjCInterfaceDecl;
Douglas Gregoref6eb842010-01-16 15:44:18 +000037class ObjCProtocolDecl;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000038class OverloadedTemplateStorage;
39class OverloadExpr;
Ted Kremenek87553c42010-01-15 20:35:54 +000040class Stmt;
Douglas Gregora23e8f72010-08-31 20:37:03 +000041class TemplateDecl;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000042class TemplateName;
Douglas Gregor93f89952010-01-21 16:28:34 +000043class TypeDecl;
Douglas Gregor30093832012-02-15 00:54:55 +000044class VarDecl;
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +000045class IdentifierInfo;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000046
Ted Kremenek87553c42010-01-15 20:35:54 +000047namespace cxcursor {
Argyrios Kyrtzidisfd515202011-09-27 00:30:33 +000048
49CXCursor getCursor(CXTranslationUnit, SourceLocation);
Ted Kremenek87553c42010-01-15 20:35:54 +000050
Dmitri Gribenko9c256e32013-01-14 00:46:27 +000051CXCursor MakeCXCursor(const clang::Attr *A, const clang::Decl *Parent,
Ted Kremenek91554282010-11-16 08:15:36 +000052 CXTranslationUnit TU);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +000053CXCursor MakeCXCursor(const clang::Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +000054 SourceRange RegionOfInterest = SourceRange(),
Ted Kremenek818e5c12010-11-01 23:26:51 +000055 bool FirstInDeclGroup = true);
Dmitri Gribenko9c256e32013-01-14 00:46:27 +000056CXCursor MakeCXCursor(const clang::Stmt *S, const clang::Decl *Parent,
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +000057 CXTranslationUnit TU,
58 SourceRange RegionOfInterest = SourceRange());
Craig Topper69186e72014-06-08 08:38:04 +000059CXCursor MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU = nullptr);
Ted Kremenek87553c42010-01-15 20:35:54 +000060
Douglas Gregor6c8959b2010-01-16 14:00:32 +000061/// \brief Create an Objective-C superclass reference at the given location.
62CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorfed36b12010-01-20 23:57:43 +000063 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +000064 CXTranslationUnit TU);
Douglas Gregor6c8959b2010-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 Gribenkoba2f7462013-01-11 21:01:49 +000068std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregoref6eb842010-01-16 15:44:18 +000069 getCursorObjCSuperClassRef(CXCursor C);
70
71/// \brief Create an Objective-C protocol reference at the given location.
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000072CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
73 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +000074 CXTranslationUnit TU);
Douglas Gregoref6eb842010-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 Gribenkoba2f7462013-01-11 21:01:49 +000078std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregoref6eb842010-01-16 15:44:18 +000079 getCursorObjCProtocolRef(CXCursor C);
Douglas Gregor6c8959b2010-01-16 14:00:32 +000080
Douglas Gregor46d66142010-01-16 17:14:40 +000081/// \brief Create an Objective-C class reference at the given location.
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000082CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
83 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +000084 CXTranslationUnit TU);
Douglas Gregor46d66142010-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 Gribenkoba2f7462013-01-11 21:01:49 +000088std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor46d66142010-01-16 17:14:40 +000089 getCursorObjCClassRef(CXCursor C);
90
Douglas Gregor93f89952010-01-21 16:28:34 +000091/// \brief Create a type reference at the given location.
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +000092CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +000093 CXTranslationUnit TU);
Douglas Gregora23e8f72010-08-31 20:37:03 +000094
Douglas Gregor93f89952010-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 Gribenkoba2f7462013-01-11 21:01:49 +000097std::pair<const TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
Douglas Gregor93f89952010-01-21 16:28:34 +000098
Douglas Gregora23e8f72010-08-31 20:37:03 +000099/// \brief Create a reference to a template at the given location.
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000100CXCursor MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000101 CXTranslationUnit TU);
Douglas Gregora23e8f72010-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 Gribenkoba2f7462013-01-11 21:01:49 +0000105std::pair<const TemplateDecl *, SourceLocation>
106 getCursorTemplateRef(CXCursor C);
Douglas Gregora89314e2010-08-31 23:48:11 +0000107
108/// \brief Create a reference to a namespace or namespace alias at the given
109/// location.
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000110CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000111 CXTranslationUnit TU);
Douglas Gregora89314e2010-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 Gribenkoba2f7462013-01-11 21:01:49 +0000115std::pair<const NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
Douglas Gregora89314e2010-08-31 23:48:11 +0000116
Douglas Gregor30093832012-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 Gribenkoba2f7462013-01-11 21:01:49 +0000123std::pair<const VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C);
Douglas Gregor30093832012-02-15 00:54:55 +0000124
Douglas Gregorf3af3112010-09-09 21:42:20 +0000125/// \brief Create a reference to a field at the given location.
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000126CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000127 CXTranslationUnit TU);
Douglas Gregorf3af3112010-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 Gribenkoba2f7462013-01-11 21:01:49 +0000131std::pair<const FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
Douglas Gregorf3af3112010-09-09 21:42:20 +0000132
Ted Kremenekae9e2212010-08-27 21:34:58 +0000133/// \brief Create a CXX base specifier cursor.
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000134CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremenek91554282010-11-16 08:15:36 +0000135 CXTranslationUnit TU);
Ted Kremenekae9e2212010-08-27 21:34:58 +0000136
137/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000138const CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
Ted Kremenekae9e2212010-08-27 21:34:58 +0000139
Douglas Gregor92a524f2010-03-18 00:42:48 +0000140/// \brief Create a preprocessing directive cursor.
Ted Kremenek91554282010-11-16 08:15:36 +0000141CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
142 CXTranslationUnit TU);
Douglas Gregor92a524f2010-03-18 00:42:48 +0000143
144/// \brief Unpack a given preprocessing directive to retrieve its source range.
145SourceRange getCursorPreprocessingDirective(CXCursor C);
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000146
Douglas Gregor06d6d322010-03-18 18:04:21 +0000147/// \brief Create a macro definition cursor.
Richard Smith66a81862015-05-04 02:25:31 +0000148CXCursor MakeMacroDefinitionCursor(const MacroDefinitionRecord *,
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000149 CXTranslationUnit TU);
Douglas Gregor06d6d322010-03-18 18:04:21 +0000150
151/// \brief Unpack a given macro definition cursor to retrieve its
152/// source range.
Richard Smith66a81862015-05-04 02:25:31 +0000153const MacroDefinitionRecord *getCursorMacroDefinition(CXCursor C);
Douglas Gregor06d6d322010-03-18 18:04:21 +0000154
Chandler Carrutha88a22182011-07-14 08:20:46 +0000155/// \brief Create a macro expansion cursor.
Richard Smith66a81862015-05-04 02:25:31 +0000156CXCursor MakeMacroExpansionCursor(MacroExpansion *, CXTranslationUnit TU);
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000157
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000158/// \brief Create a "pseudo" macro expansion cursor, using a macro definition
159/// and a source location.
Richard Smith66a81862015-05-04 02:25:31 +0000160CXCursor MakeMacroExpansionCursor(MacroDefinitionRecord *, SourceLocation Loc,
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000161 CXTranslationUnit TU);
162
163/// \brief Wraps a macro expansion cursor and provides a common interface
164/// for a normal macro expansion cursor or a "pseudo" one.
165///
166/// "Pseudo" macro expansion cursors (essentially a macro definition along with
167/// a source location) are created in special cases, for example they can be
168/// created for identifiers inside macro definitions, if these identifiers are
169/// macro names.
170class MacroExpansionCursor {
171 CXCursor C;
172
Richard Smith66a81862015-05-04 02:25:31 +0000173 bool isPseudo() const { return C.data[1] != nullptr; }
174 const MacroDefinitionRecord *getAsMacroDefinition() const {
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000175 assert(isPseudo());
Richard Smith66a81862015-05-04 02:25:31 +0000176 return static_cast<const MacroDefinitionRecord *>(C.data[0]);
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000177 }
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000178 const MacroExpansion *getAsMacroExpansion() const {
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000179 assert(!isPseudo());
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000180 return static_cast<const MacroExpansion *>(C.data[0]);
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000181 }
182 SourceLocation getPseudoLoc() const {
183 assert(isPseudo());
184 return SourceLocation::getFromPtrEncoding(C.data[1]);
185 }
186
187public:
188 MacroExpansionCursor(CXCursor C) : C(C) {
189 assert(C.kind == CXCursor_MacroExpansion);
190 }
191
192 const IdentifierInfo *getName() const;
Richard Smith66a81862015-05-04 02:25:31 +0000193 const MacroDefinitionRecord *getDefinition() const;
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000194 SourceRange getSourceRange() const;
195};
196
197/// \brief Unpack a given macro expansion cursor to retrieve its info.
198static inline MacroExpansionCursor getCursorMacroExpansion(CXCursor C) {
199 return C;
200}
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000201
Douglas Gregor796d76a2010-10-20 22:00:55 +0000202/// \brief Create an inclusion directive cursor.
Ted Kremenek91554282010-11-16 08:15:36 +0000203CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
204 CXTranslationUnit TU);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000205
206/// \brief Unpack a given inclusion directive cursor to retrieve its
207/// source range.
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000208const InclusionDirective *getCursorInclusionDirective(CXCursor C);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000209
Douglas Gregora93ab662010-09-10 00:22:18 +0000210/// \brief Create a label reference at the given location.
Ted Kremenek91554282010-11-16 08:15:36 +0000211CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
212 CXTranslationUnit TU);
Douglas Gregora93ab662010-09-10 00:22:18 +0000213
214/// \brief Unpack a label reference into the label statement it refers to and
215/// the location of the reference.
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000216std::pair<const LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000217
218/// \brief Create a overloaded declaration reference cursor for an expression.
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000219CXCursor MakeCursorOverloadedDeclRef(const OverloadExpr *E,
220 CXTranslationUnit TU);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000221
222/// \brief Create a overloaded declaration reference cursor for a declaration.
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000223CXCursor MakeCursorOverloadedDeclRef(const Decl *D, SourceLocation Location,
Ted Kremenek91554282010-11-16 08:15:36 +0000224 CXTranslationUnit TU);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000225
226/// \brief Create a overloaded declaration reference cursor for a template name.
227CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
Ted Kremenek91554282010-11-16 08:15:36 +0000228 SourceLocation Location,
229 CXTranslationUnit TU);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000230
231/// \brief Internal storage for an overloaded declaration reference cursor;
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000232typedef llvm::PointerUnion3<const OverloadExpr *, const Decl *,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000233 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 Gregora93ab662010-09-10 00:22:18 +0000240
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000241const Decl *getCursorDecl(CXCursor Cursor);
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000242const Expr *getCursorExpr(CXCursor Cursor);
243const Stmt *getCursorStmt(CXCursor Cursor);
Dmitri Gribenkoe4baea62013-01-26 18:08:08 +0000244const Attr *getCursorAttr(CXCursor Cursor);
Dmitri Gribenkoa1691182013-01-26 18:12:08 +0000245const Decl *getCursorParentDecl(CXCursor Cursor);
Ted Kremeneka5940822010-08-26 01:42:22 +0000246
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000247ASTContext &getCursorContext(CXCursor Cursor);
Douglas Gregorfed36b12010-01-20 23:57:43 +0000248ASTUnit *getCursorASTUnit(CXCursor Cursor);
Ted Kremenek91554282010-11-16 08:15:36 +0000249CXTranslationUnit getCursorTU(CXCursor Cursor);
Argyrios Kyrtzidis3b947f72011-10-06 07:00:46 +0000250
251void getOverriddenCursors(CXCursor cursor,
Ted Kremenekd77f6212012-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 Kyrtzidis3b947f72011-10-06 07:00:46 +0000257
Ted Kremenekd77f6212012-04-30 19:06:49 +0000258/// \brief Dispose of the overriden CXCursors pool.
259void disposeOverridenCXCursorsPool(void *pool);
260
Argyrios Kyrtzidiscddafd32011-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 Kyrtzidise4549862011-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 Kyrtzidisdc199a32011-10-17 19:48:19 +0000285bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
Argyrios Kyrtzidise4549862011-10-12 07:07:36 +0000286
Douglas Gregorc58d05b2010-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 Kremenek818e5c12010-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
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000297}} // end namespace: clang::cxcursor
Ted Kremenek87553c42010-01-15 20:35:54 +0000298
299#endif