1 //===- CXCursor.h - Routines for manipulating CXCursors -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines routines for manipulating CXCursors.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_CXCURSOR_H
15 #define LLVM_CLANG_CXCURSOR_H
17 #include "clang-c/Index.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "llvm/ADT/PointerUnion.h"
27 class CXXBaseSpecifier;
31 class InclusionDirective;
33 class MacroDefinition;
36 class ObjCInterfaceDecl;
37 class ObjCProtocolDecl;
38 class OverloadedTemplateStorage;
48 CXCursor getCursor(CXTranslationUnit, SourceLocation);
50 CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent,
51 CXTranslationUnit TU);
52 CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU,
53 SourceRange RegionOfInterest = SourceRange(),
54 bool FirstInDeclGroup = true);
55 CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent,
57 SourceRange RegionOfInterest = SourceRange());
58 CXCursor MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU = 0);
60 /// \brief Create an Objective-C superclass reference at the given location.
61 CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
63 CXTranslationUnit TU);
65 /// \brief Unpack an ObjCSuperClassRef cursor into the interface it references
66 /// and optionally the location where the reference occurred.
67 std::pair<ObjCInterfaceDecl *, SourceLocation>
68 getCursorObjCSuperClassRef(CXCursor C);
70 /// \brief Create an Objective-C protocol reference at the given location.
71 CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
73 CXTranslationUnit TU);
75 /// \brief Unpack an ObjCProtocolRef cursor into the protocol it references
76 /// and optionally the location where the reference occurred.
77 std::pair<ObjCProtocolDecl *, SourceLocation>
78 getCursorObjCProtocolRef(CXCursor C);
80 /// \brief Create an Objective-C class reference at the given location.
81 CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
83 CXTranslationUnit TU);
85 /// \brief Unpack an ObjCClassRef cursor into the class it references
86 /// and optionally the location where the reference occurred.
87 std::pair<ObjCInterfaceDecl *, SourceLocation>
88 getCursorObjCClassRef(CXCursor C);
90 /// \brief Create a type reference at the given location.
91 CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
92 CXTranslationUnit TU);
94 /// \brief Unpack a TypeRef cursor into the class it references
95 /// and optionally the location where the reference occurred.
96 std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
98 /// \brief Create a reference to a template at the given location.
99 CXCursor MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc,
100 CXTranslationUnit TU);
102 /// \brief Unpack a TemplateRef cursor into the template it references and
103 /// the location where the reference occurred.
104 std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C);
106 /// \brief Create a reference to a namespace or namespace alias at the given
108 CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc,
109 CXTranslationUnit TU);
111 /// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias
112 /// it references and the location where the reference occurred.
113 std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
115 /// \brief Create a reference to a variable at the given location.
116 CXCursor MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
117 CXTranslationUnit TU);
119 /// \brief Unpack a VariableRef cursor into the variable it references and the
120 /// location where the where the reference occurred.
121 std::pair<VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C);
123 /// \brief Create a reference to a field at the given location.
124 CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
125 CXTranslationUnit TU);
127 /// \brief Unpack a MemberRef cursor into the field it references and the
128 /// location where the reference occurred.
129 std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
131 /// \brief Create a CXX base specifier cursor.
132 CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
133 CXTranslationUnit TU);
135 /// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
136 CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
138 /// \brief Create a preprocessing directive cursor.
139 CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
140 CXTranslationUnit TU);
142 /// \brief Unpack a given preprocessing directive to retrieve its source range.
143 SourceRange getCursorPreprocessingDirective(CXCursor C);
145 /// \brief Create a macro definition cursor.
146 CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU);
148 /// \brief Unpack a given macro definition cursor to retrieve its
150 MacroDefinition *getCursorMacroDefinition(CXCursor C);
152 /// \brief Create a macro expansion cursor.
153 CXCursor MakeMacroExpansionCursor(MacroExpansion *,
154 CXTranslationUnit TU);
156 /// \brief Unpack a given macro expansion cursor to retrieve its
158 MacroExpansion *getCursorMacroExpansion(CXCursor C);
160 /// \brief Create an inclusion directive cursor.
161 CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
162 CXTranslationUnit TU);
164 /// \brief Unpack a given inclusion directive cursor to retrieve its
166 InclusionDirective *getCursorInclusionDirective(CXCursor C);
168 /// \brief Create a label reference at the given location.
169 CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
170 CXTranslationUnit TU);
172 /// \brief Unpack a label reference into the label statement it refers to and
173 /// the location of the reference.
174 std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
176 /// \brief Create a overloaded declaration reference cursor for an expression.
177 CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU);
179 /// \brief Create a overloaded declaration reference cursor for a declaration.
180 CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location,
181 CXTranslationUnit TU);
183 /// \brief Create a overloaded declaration reference cursor for a template name.
184 CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
185 SourceLocation Location,
186 CXTranslationUnit TU);
188 /// \brief Internal storage for an overloaded declaration reference cursor;
189 typedef llvm::PointerUnion3<OverloadExpr *, Decl *,
190 OverloadedTemplateStorage *>
191 OverloadedDeclRefStorage;
193 /// \brief Unpack an overloaded declaration reference into an expression,
194 /// declaration, or template name along with the source location.
195 std::pair<OverloadedDeclRefStorage, SourceLocation>
196 getCursorOverloadedDeclRef(CXCursor C);
198 Decl *getCursorDecl(CXCursor Cursor);
199 Expr *getCursorExpr(CXCursor Cursor);
200 Stmt *getCursorStmt(CXCursor Cursor);
201 Attr *getCursorAttr(CXCursor Cursor);
202 Decl *getCursorParentDecl(CXCursor Cursor);
204 ASTContext &getCursorContext(CXCursor Cursor);
205 ASTUnit *getCursorASTUnit(CXCursor Cursor);
206 CXTranslationUnit getCursorTU(CXCursor Cursor);
208 void getOverriddenCursors(CXCursor cursor,
209 SmallVectorImpl<CXCursor> &overridden);
211 /// \brief Create an opaque pool used for fast generation of overriden
213 void *createOverridenCXCursorsPool();
215 /// \brief Dispose of the overriden CXCursors pool.
216 void disposeOverridenCXCursorsPool(void *pool);
218 /// \brief Returns a index/location pair for a selector identifier if the cursor
220 std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
221 static inline int getSelectorIdentifierIndex(CXCursor cursor) {
222 return getSelectorIdentifierIndexAndLoc(cursor).first;
224 static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
225 return getSelectorIdentifierIndexAndLoc(cursor).second;
228 CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
230 static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
231 CXCursor newCursor = cursor;
232 if (cursor.kind == CXCursor_CallExpr)
237 CXCursor getTypeRefCursor(CXCursor cursor);
239 /// \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,
242 bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
244 bool operator==(CXCursor X, CXCursor Y);
246 inline bool operator!=(CXCursor X, CXCursor Y) {
250 /// \brief Return true if the cursor represents a declaration that is the
251 /// first in a declaration group.
252 bool isFirstInDeclGroup(CXCursor C);
254 }} // end namespace: clang::cxcursor