blob: 8209cf4948d17c024a8e1f94ca80837d10946486 [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 Gregor1f60d9e2010-09-13 22:52:57 +000044
Ted Kremenek16c440a2010-01-15 20:35:54 +000045namespace cxcursor {
Argyrios Kyrtzidis671436e2011-09-27 00:30:33 +000046
47CXCursor getCursor(CXTranslationUnit, SourceLocation);
Ted Kremenek16c440a2010-01-15 20:35:54 +000048
Ted Kremeneka60ed472010-11-16 08:15:36 +000049CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent,
50 CXTranslationUnit TU);
51CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000052 SourceRange RegionOfInterest = SourceRange(),
Ted Kremenek007a7c92010-11-01 23:26:51 +000053 bool FirstInDeclGroup = true);
Ted Kremeneka60ed472010-11-16 08:15:36 +000054CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000055 CXTranslationUnit TU,
56 SourceRange RegionOfInterest = SourceRange());
Ted Kremeneke77f4432010-02-18 03:09:07 +000057CXCursor MakeCXCursorInvalid(CXCursorKind K);
Ted Kremenek16c440a2010-01-15 20:35:54 +000058
Douglas Gregor2e331b92010-01-16 14:00:32 +000059/// \brief Create an Objective-C superclass reference at the given location.
60CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +000061 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000062 CXTranslationUnit TU);
Douglas Gregor2e331b92010-01-16 14:00:32 +000063
64/// \brief Unpack an ObjCSuperClassRef cursor into the interface it references
65/// and optionally the location where the reference occurred.
66std::pair<ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +000067 getCursorObjCSuperClassRef(CXCursor C);
68
69/// \brief Create an Objective-C protocol reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000070CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
71 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000072 CXTranslationUnit TU);
Douglas Gregor78db0cd2010-01-16 15:44:18 +000073
74/// \brief Unpack an ObjCProtocolRef cursor into the protocol it references
75/// and optionally the location where the reference occurred.
76std::pair<ObjCProtocolDecl *, SourceLocation>
77 getCursorObjCProtocolRef(CXCursor C);
Douglas Gregor2e331b92010-01-16 14:00:32 +000078
Douglas Gregor1adb0822010-01-16 17:14:40 +000079/// \brief Create an Objective-C class reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000080CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
81 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000082 CXTranslationUnit TU);
Douglas Gregor1adb0822010-01-16 17:14:40 +000083
84/// \brief Unpack an ObjCClassRef cursor into the class it references
85/// and optionally the location where the reference occurred.
86std::pair<ObjCInterfaceDecl *, SourceLocation>
87 getCursorObjCClassRef(CXCursor C);
88
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000089/// \brief Create a type reference at the given location.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +000090CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000091 CXTranslationUnit TU);
Douglas Gregor0b36e612010-08-31 20:37:03 +000092
Douglas Gregor7d0d40e2010-01-21 16:28:34 +000093/// \brief Unpack a TypeRef cursor into the class it references
94/// and optionally the location where the reference occurred.
95std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
96
Douglas Gregor0b36e612010-08-31 20:37:03 +000097/// \brief Create a reference to a template at the given location.
98CXCursor MakeCursorTemplateRef(TemplateDecl *Template, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +000099 CXTranslationUnit TU);
Douglas Gregor0b36e612010-08-31 20:37:03 +0000100
101/// \brief Unpack a TemplateRef cursor into the template it references and
102/// the location where the reference occurred.
103std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C);
Douglas Gregor69319002010-08-31 23:48:11 +0000104
105/// \brief Create a reference to a namespace or namespace alias at the given
106/// location.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000107CXCursor MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
108 CXTranslationUnit TU);
Douglas Gregor69319002010-08-31 23:48:11 +0000109
110/// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias
111/// it references and the location where the reference occurred.
112std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
113
Douglas Gregora67e03f2010-09-09 21:42:20 +0000114/// \brief Create a reference to a field at the given location.
115CXCursor MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000116 CXTranslationUnit TU);
Douglas Gregora67e03f2010-09-09 21:42:20 +0000117
118/// \brief Unpack a MemberRef cursor into the field it references and the
119/// location where the reference occurred.
120std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
121
Ted Kremenek3064ef92010-08-27 21:34:58 +0000122/// \brief Create a CXX base specifier cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000123CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
124 CXTranslationUnit TU);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000125
126/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
127CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
128
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000129/// \brief Create a preprocessing directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000130CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
131 CXTranslationUnit TU);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000132
133/// \brief Unpack a given preprocessing directive to retrieve its source range.
134SourceRange getCursorPreprocessingDirective(CXCursor C);
Douglas Gregor48072312010-03-18 15:23:44 +0000135
Douglas Gregor572feb22010-03-18 18:04:21 +0000136/// \brief Create a macro definition cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000137CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU);
Douglas Gregor572feb22010-03-18 18:04:21 +0000138
139/// \brief Unpack a given macro definition cursor to retrieve its
140/// source range.
141MacroDefinition *getCursorMacroDefinition(CXCursor C);
142
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000143/// \brief Create a macro expansion cursor.
144CXCursor MakeMacroExpansionCursor(MacroExpansion *,
145 CXTranslationUnit TU);
Douglas Gregor48072312010-03-18 15:23:44 +0000146
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000147/// \brief Unpack a given macro expansion cursor to retrieve its
Douglas Gregor48072312010-03-18 15:23:44 +0000148/// source range.
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000149MacroExpansion *getCursorMacroExpansion(CXCursor C);
Douglas Gregor48072312010-03-18 15:23:44 +0000150
Douglas Gregorecdcb882010-10-20 22:00:55 +0000151/// \brief Create an inclusion directive cursor.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000152CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
153 CXTranslationUnit TU);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000154
155/// \brief Unpack a given inclusion directive cursor to retrieve its
156/// source range.
157InclusionDirective *getCursorInclusionDirective(CXCursor C);
158
Douglas Gregor36897b02010-09-10 00:22:18 +0000159/// \brief Create a label reference at the given location.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000160CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
161 CXTranslationUnit TU);
Douglas Gregor36897b02010-09-10 00:22:18 +0000162
163/// \brief Unpack a label reference into the label statement it refers to and
164/// the location of the reference.
165std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000166
167/// \brief Create a overloaded declaration reference cursor for an expression.
Ted Kremeneka60ed472010-11-16 08:15:36 +0000168CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000169
170/// \brief Create a overloaded declaration reference cursor for a declaration.
171CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000172 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000173
174/// \brief Create a overloaded declaration reference cursor for a template name.
175CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000176 SourceLocation Location,
177 CXTranslationUnit TU);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000178
179/// \brief Internal storage for an overloaded declaration reference cursor;
180typedef llvm::PointerUnion3<OverloadExpr *, Decl *,
181 OverloadedTemplateStorage *>
182 OverloadedDeclRefStorage;
183
184/// \brief Unpack an overloaded declaration reference into an expression,
185/// declaration, or template name along with the source location.
186std::pair<OverloadedDeclRefStorage, SourceLocation>
187 getCursorOverloadedDeclRef(CXCursor C);
Douglas Gregor36897b02010-09-10 00:22:18 +0000188
Douglas Gregor283cae32010-01-15 21:56:13 +0000189Decl *getCursorDecl(CXCursor Cursor);
190Expr *getCursorExpr(CXCursor Cursor);
191Stmt *getCursorStmt(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000192Attr *getCursorAttr(CXCursor Cursor);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000193Decl *getCursorParentDecl(CXCursor Cursor);
Ted Kremenek95f33552010-08-26 01:42:22 +0000194
Douglas Gregorf46034a2010-01-18 23:41:10 +0000195ASTContext &getCursorContext(CXCursor Cursor);
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000196ASTUnit *getCursorASTUnit(CXCursor Cursor);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000197CXTranslationUnit getCursorTU(CXCursor Cursor);
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000198
199void getOverriddenCursors(CXCursor cursor,
200 SmallVectorImpl<CXCursor> &overridden);
201
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000202/// \brief Returns a index/location pair for a selector identifier if the cursor
203/// points to one.
204std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
205static inline int getSelectorIdentifierIndex(CXCursor cursor) {
206 return getSelectorIdentifierIndexAndLoc(cursor).first;
207}
208static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
209 return getSelectorIdentifierIndexAndLoc(cursor).second;
210}
211
212CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
213
214static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
215 CXCursor newCursor = cursor;
216 if (cursor.kind == CXCursor_CallExpr)
217 newCursor.xdata = 1;
218 return newCursor;
219}
220
221CXCursor getTypeRefCursor(CXCursor cursor);
222
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000223/// \brief Generate a USR for \arg D and put it in \arg Buf.
224/// \returns true if no USR was computed or the result should be ignored,
225/// false otherwise.
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000226bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
Argyrios Kyrtzidisb6a4ac42011-10-12 07:07:36 +0000227
Douglas Gregor283cae32010-01-15 21:56:13 +0000228bool operator==(CXCursor X, CXCursor Y);
229
230inline bool operator!=(CXCursor X, CXCursor Y) {
231 return !(X == Y);
232}
233
Ted Kremenek007a7c92010-11-01 23:26:51 +0000234/// \brief Return true if the cursor represents a declaration that is the
235/// first in a declaration group.
236bool isFirstInDeclGroup(CXCursor C);
237
Ted Kremenek16c440a2010-01-15 20:35:54 +0000238}} // end namespace: clang::cxcursor
239
240#endif