blob: 120b881069da99aa976ebaff41baab7992e11766 [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;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000045
Ted Kremenek16c440a2010-01-15 20:35:54 +000046namespace cxcursor {
Argyrios Kyrtzidis671436e2011-09-27 00:30:33 +000047
48CXCursor getCursor(CXTranslationUnit, SourceLocation);
Ted Kremenek16c440a2010-01-15 20:35:54 +000049
Ted Kremeneka60ed472010-11-16 08:15:36 +000050CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent,
51 CXTranslationUnit TU);
52CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000053 SourceRange RegionOfInterest = SourceRange(),
Ted Kremenek007a7c92010-11-01 23:26:51 +000054 bool FirstInDeclGroup = true);
Ted Kremeneka60ed472010-11-16 08:15:36 +000055CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000056 CXTranslationUnit TU,
57 SourceRange RegionOfInterest = SourceRange());
Ted Kremenekbbf66ca2012-04-30 19:06:49 +000058CXCursor MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU = 0);
Ted Kremenek16c440a2010-01-15 20:35:54 +000059
Douglas Gregor2e331b92010-01-16 14:00:32 +000060/// \brief Create an Objective-C superclass reference at the given location.
61CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +000062 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000063 CXTranslationUnit TU);
Douglas Gregor2e331b92010-01-16 14:00:32 +000064
65/// \brief Unpack an ObjCSuperClassRef cursor into the interface it references
66/// and optionally the location where the reference occurred.
67std::pair<ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +000068 getCursorObjCSuperClassRef(CXCursor C);
69
70/// \brief Create an Objective-C protocol reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000071CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
72 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000073 CXTranslationUnit TU);
Douglas Gregor78db0cd2010-01-16 15:44:18 +000074
75/// \brief Unpack an ObjCProtocolRef cursor into the protocol it references
76/// and optionally the location where the reference occurred.
77std::pair<ObjCProtocolDecl *, SourceLocation>
78 getCursorObjCProtocolRef(CXCursor C);
Douglas Gregor2e331b92010-01-16 14:00:32 +000079
Douglas Gregor1adb0822010-01-16 17:14:40 +000080/// \brief Create an Objective-C class reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000081CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
82 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000083 CXTranslationUnit TU);
Douglas Gregor1adb0822010-01-16 17:14:40 +000084
85/// \brief Unpack an ObjCClassRef cursor into the class it references
86/// and optionally the location where the reference occurred.
87std::pair<ObjCInterfaceDecl *, SourceLocation>
88 getCursorObjCClassRef(CXCursor C);
89
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000090/// \brief Create a type reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000091CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000092 CXTranslationUnit TU);
Douglas Gregor0b36e612010-08-31 20:37:03 +000093
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000094/// \brief Unpack a TypeRef cursor into the class it references
95/// and optionally the location where the reference occurred.
96std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
97
Douglas Gregor0b36e612010-08-31 20:37:03 +000098/// \brief Create a reference to a template at the given location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +000099CXCursor MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000100 CXTranslationUnit TU);
Douglas Gregor0b36e612010-08-31 20:37:03 +0000101
102/// \brief Unpack a TemplateRef cursor into the template it references and
103/// the location where the reference occurred.
104std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C);
Douglas Gregor69319002010-08-31 23:48:11 +0000105
106/// \brief Create a reference to a namespace or namespace alias at the given
107/// location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000108CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000109 CXTranslationUnit TU);
Douglas Gregor69319002010-08-31 23:48:11 +0000110
111/// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias
112/// it references and the location where the reference occurred.
113std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
114
Douglas Gregor011d8b92012-02-15 00:54:55 +0000115/// \brief Create a reference to a variable at the given location.
116CXCursor MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
117 CXTranslationUnit TU);
118
119/// \brief Unpack a VariableRef cursor into the variable it references and the
120/// location where the where the reference occurred.
121std::pair<VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C);
122
Douglas Gregora67e03f2010-09-09 21:42:20 +0000123/// \brief Create a reference to a field at the given location.
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000124CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000125 CXTranslationUnit TU);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000126
127/// \brief Unpack a MemberRef cursor into the field it references and the
128/// location where the reference occurred.
129std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
130
Ted Kremenek3064ef92010-08-27 21:34:58 +0000131/// \brief Create a CXX base specifier cursor.
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000132CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000133 CXTranslationUnit TU);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000134
135/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
136CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
137
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000138/// \brief Create a preprocessing directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000139CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
140 CXTranslationUnit TU);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000141
142/// \brief Unpack a given preprocessing directive to retrieve its source range.
143SourceRange getCursorPreprocessingDirective(CXCursor C);
Douglas Gregor48072312010-03-18 15:23:44 +0000144
Douglas Gregor572feb22010-03-18 18:04:21 +0000145/// \brief Create a macro definition cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000146CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU);
Douglas Gregor572feb22010-03-18 18:04:21 +0000147
148/// \brief Unpack a given macro definition cursor to retrieve its
149/// source range.
150MacroDefinition *getCursorMacroDefinition(CXCursor C);
151
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000152/// \brief Create a macro expansion cursor.
153CXCursor MakeMacroExpansionCursor(MacroExpansion *,
154 CXTranslationUnit TU);
Douglas Gregor48072312010-03-18 15:23:44 +0000155
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000156/// \brief Unpack a given macro expansion cursor to retrieve its
Douglas Gregor48072312010-03-18 15:23:44 +0000157/// source range.
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000158MacroExpansion *getCursorMacroExpansion(CXCursor C);
Douglas Gregor48072312010-03-18 15:23:44 +0000159
Douglas Gregorecdcb882010-10-20 22:00:55 +0000160/// \brief Create an inclusion directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000161CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
162 CXTranslationUnit TU);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000163
164/// \brief Unpack a given inclusion directive cursor to retrieve its
165/// source range.
166InclusionDirective *getCursorInclusionDirective(CXCursor C);
167
Douglas Gregor36897b02010-09-10 00:22:18 +0000168/// \brief Create a label reference at the given location.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000169CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
170 CXTranslationUnit TU);
Douglas Gregor36897b02010-09-10 00:22:18 +0000171
172/// \brief Unpack a label reference into the label statement it refers to and
173/// the location of the reference.
174std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000175
176/// \brief Create a overloaded declaration reference cursor for an expression.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000177CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000178
179/// \brief Create a overloaded declaration reference cursor for a declaration.
180CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000181 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000182
183/// \brief Create a overloaded declaration reference cursor for a template name.
184CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000185 SourceLocation Location,
186 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000187
188/// \brief Internal storage for an overloaded declaration reference cursor;
189typedef llvm::PointerUnion3<OverloadExpr *, Decl *,
190 OverloadedTemplateStorage *>
191 OverloadedDeclRefStorage;
192
193/// \brief Unpack an overloaded declaration reference into an expression,
194/// declaration, or template name along with the source location.
195std::pair<OverloadedDeclRefStorage, SourceLocation>
196 getCursorOverloadedDeclRef(CXCursor C);
Douglas Gregor36897b02010-09-10 00:22:18 +0000197
Douglas Gregor283cae32010-01-15 21:56:13 +0000198Decl *getCursorDecl(CXCursor Cursor);
199Expr *getCursorExpr(CXCursor Cursor);
200Stmt *getCursorStmt(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000201Attr *getCursorAttr(CXCursor Cursor);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000202Decl *getCursorParentDecl(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000203
Douglas Gregorf46034a2010-01-18 23:41:10 +0000204ASTContext &getCursorContext(CXCursor Cursor);
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000205ASTUnit *getCursorASTUnit(CXCursor Cursor);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000206CXTranslationUnit getCursorTU(CXCursor Cursor);
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000207
208void getOverriddenCursors(CXCursor cursor,
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000209 SmallVectorImpl<CXCursor> &overridden);
210
211/// \brief Create an opaque pool used for fast generation of overriden
212/// CXCursor arrays.
213void *createOverridenCXCursorsPool();
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000214
Ted Kremenekbbf66ca2012-04-30 19:06:49 +0000215/// \brief Dispose of the overriden CXCursors pool.
216void disposeOverridenCXCursorsPool(void *pool);
217
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000218/// \brief Returns a index/location pair for a selector identifier if the cursor
219/// points to one.
220std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
221static inline int getSelectorIdentifierIndex(CXCursor cursor) {
222 return getSelectorIdentifierIndexAndLoc(cursor).first;
223}
224static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
225 return getSelectorIdentifierIndexAndLoc(cursor).second;
226}
227
228CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
229
230static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
231 CXCursor newCursor = cursor;
232 if (cursor.kind == CXCursor_CallExpr)
233 newCursor.xdata = 1;
234 return newCursor;
235}
236
237CXCursor getTypeRefCursor(CXCursor cursor);
238
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000239/// \brief Generate a USR for \arg D and put it in \arg Buf.
240/// \returns true if no USR was computed or the result should be ignored,
241/// false otherwise.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000242bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000243
Douglas Gregor283cae32010-01-15 21:56:13 +0000244bool operator==(CXCursor X, CXCursor Y);
245
246inline bool operator!=(CXCursor X, CXCursor Y) {
247 return !(X == Y);
248}
249
Ted Kremenek007a7c92010-11-01 23:26:51 +0000250/// \brief Return true if the cursor represents a declaration that is the
251/// first in a declaration group.
252bool isFirstInDeclGroup(CXCursor C);
253
Ted Kremenek16c440a2010-01-15 20:35:54 +0000254}} // end namespace: clang::cxcursor
255
256#endif