CXCursor.h revision b6a4ac41353476ed771d02ae6a96c2663625ffcd
1//===- 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 15#define LLVM_CLANG_CXCURSOR_H 16 17#include "clang-c/Index.h" 18#include "clang/Basic/SourceLocation.h" 19#include "llvm/ADT/PointerUnion.h" 20#include <utility> 21 22namespace clang { 23 24class ASTContext; 25class ASTUnit; 26class Attr; 27class CXXBaseSpecifier; 28class Decl; 29class Expr; 30class FieldDecl; 31class InclusionDirective; 32class LabelStmt; 33class MacroDefinition; 34class MacroExpansion; 35class NamedDecl; 36class ObjCInterfaceDecl; 37class ObjCProtocolDecl; 38class OverloadedTemplateStorage; 39class OverloadExpr; 40class Stmt; 41class TemplateDecl; 42class TemplateName; 43class TypeDecl; 44 45namespace cxcursor { 46 47CXCursor getCursor(CXTranslationUnit, SourceLocation); 48 49CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent, 50 CXTranslationUnit TU); 51CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU, 52 SourceRange RegionOfInterest = SourceRange(), 53 bool FirstInDeclGroup = true); 54CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent, 55 CXTranslationUnit TU, 56 SourceRange RegionOfInterest = SourceRange()); 57CXCursor MakeCXCursorInvalid(CXCursorKind K); 58 59/// \brief Create an Objective-C superclass reference at the given location. 60CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, 61 SourceLocation Loc, 62 CXTranslationUnit TU); 63 64/// \brief Unpack an ObjCSuperClassRef cursor into the interface it references 65/// and optionally the location where the reference occurred. 66std::pair<ObjCInterfaceDecl *, SourceLocation> 67 getCursorObjCSuperClassRef(CXCursor C); 68 69/// \brief Create an Objective-C protocol reference at the given location. 70CXCursor MakeCursorObjCProtocolRef(ObjCProtocolDecl *Proto, SourceLocation Loc, 71 CXTranslationUnit TU); 72 73/// \brief Unpack an ObjCProtocolRef cursor into the protocol it references 74/// and optionally the location where the reference occurred. 75std::pair<ObjCProtocolDecl *, SourceLocation> 76 getCursorObjCProtocolRef(CXCursor C); 77 78/// \brief Create an Objective-C class reference at the given location. 79CXCursor MakeCursorObjCClassRef(ObjCInterfaceDecl *Class, SourceLocation Loc, 80 CXTranslationUnit TU); 81 82/// \brief Unpack an ObjCClassRef cursor into the class it references 83/// and optionally the location where the reference occurred. 84std::pair<ObjCInterfaceDecl *, SourceLocation> 85 getCursorObjCClassRef(CXCursor C); 86 87/// \brief Create a type reference at the given location. 88CXCursor MakeCursorTypeRef(TypeDecl *Type, SourceLocation Loc, 89 CXTranslationUnit TU); 90 91/// \brief Unpack a TypeRef cursor into the class it references 92/// and optionally the location where the reference occurred. 93std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C); 94 95/// \brief Create a reference to a template at the given location. 96CXCursor MakeCursorTemplateRef(TemplateDecl *Template, SourceLocation Loc, 97 CXTranslationUnit TU); 98 99/// \brief Unpack a TemplateRef cursor into the template it references and 100/// the location where the reference occurred. 101std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C); 102 103/// \brief Create a reference to a namespace or namespace alias at the given 104/// location. 105CXCursor MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, 106 CXTranslationUnit TU); 107 108/// \brief Unpack a NamespaceRef cursor into the namespace or namespace alias 109/// it references and the location where the reference occurred. 110std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C); 111 112/// \brief Create a reference to a field at the given location. 113CXCursor MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc, 114 CXTranslationUnit TU); 115 116/// \brief Unpack a MemberRef cursor into the field it references and the 117/// location where the reference occurred. 118std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C); 119 120/// \brief Create a CXX base specifier cursor. 121CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, 122 CXTranslationUnit TU); 123 124/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier. 125CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C); 126 127/// \brief Create a preprocessing directive cursor. 128CXCursor MakePreprocessingDirectiveCursor(SourceRange Range, 129 CXTranslationUnit TU); 130 131/// \brief Unpack a given preprocessing directive to retrieve its source range. 132SourceRange getCursorPreprocessingDirective(CXCursor C); 133 134/// \brief Create a macro definition cursor. 135CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU); 136 137/// \brief Unpack a given macro definition cursor to retrieve its 138/// source range. 139MacroDefinition *getCursorMacroDefinition(CXCursor C); 140 141/// \brief Create a macro expansion cursor. 142CXCursor MakeMacroExpansionCursor(MacroExpansion *, 143 CXTranslationUnit TU); 144 145/// \brief Unpack a given macro expansion cursor to retrieve its 146/// source range. 147MacroExpansion *getCursorMacroExpansion(CXCursor C); 148 149/// \brief Create an inclusion directive cursor. 150CXCursor MakeInclusionDirectiveCursor(InclusionDirective *, 151 CXTranslationUnit TU); 152 153/// \brief Unpack a given inclusion directive cursor to retrieve its 154/// source range. 155InclusionDirective *getCursorInclusionDirective(CXCursor C); 156 157/// \brief Create a label reference at the given location. 158CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, 159 CXTranslationUnit TU); 160 161/// \brief Unpack a label reference into the label statement it refers to and 162/// the location of the reference. 163std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C); 164 165/// \brief Create a overloaded declaration reference cursor for an expression. 166CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU); 167 168/// \brief Create a overloaded declaration reference cursor for a declaration. 169CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location, 170 CXTranslationUnit TU); 171 172/// \brief Create a overloaded declaration reference cursor for a template name. 173CXCursor MakeCursorOverloadedDeclRef(TemplateName Template, 174 SourceLocation Location, 175 CXTranslationUnit TU); 176 177/// \brief Internal storage for an overloaded declaration reference cursor; 178typedef llvm::PointerUnion3<OverloadExpr *, Decl *, 179 OverloadedTemplateStorage *> 180 OverloadedDeclRefStorage; 181 182/// \brief Unpack an overloaded declaration reference into an expression, 183/// declaration, or template name along with the source location. 184std::pair<OverloadedDeclRefStorage, SourceLocation> 185 getCursorOverloadedDeclRef(CXCursor C); 186 187Decl *getCursorDecl(CXCursor Cursor); 188Expr *getCursorExpr(CXCursor Cursor); 189Stmt *getCursorStmt(CXCursor Cursor); 190Attr *getCursorAttr(CXCursor Cursor); 191Decl *getCursorParentDecl(CXCursor Cursor); 192 193ASTContext &getCursorContext(CXCursor Cursor); 194ASTUnit *getCursorASTUnit(CXCursor Cursor); 195CXTranslationUnit getCursorTU(CXCursor Cursor); 196 197void getOverriddenCursors(CXCursor cursor, 198 SmallVectorImpl<CXCursor> &overridden); 199 200/// \brief Returns a index/location pair for a selector identifier if the cursor 201/// points to one. 202std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor); 203static inline int getSelectorIdentifierIndex(CXCursor cursor) { 204 return getSelectorIdentifierIndexAndLoc(cursor).first; 205} 206static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) { 207 return getSelectorIdentifierIndexAndLoc(cursor).second; 208} 209 210CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor); 211 212static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) { 213 CXCursor newCursor = cursor; 214 if (cursor.kind == CXCursor_CallExpr) 215 newCursor.xdata = 1; 216 return newCursor; 217} 218 219CXCursor getTypeRefCursor(CXCursor cursor); 220 221/// \brief Generate a USR for \arg D and put it in \arg Buf. 222/// \returns true if no USR was computed or the result should be ignored, 223/// false otherwise. 224bool getDeclCursorUSR(Decl *D, SmallVectorImpl<char> &Buf); 225 226bool operator==(CXCursor X, CXCursor Y); 227 228inline bool operator!=(CXCursor X, CXCursor Y) { 229 return !(X == Y); 230} 231 232/// \brief Return true if the cursor represents a declaration that is the 233/// first in a declaration group. 234bool isFirstInDeclGroup(CXCursor C); 235 236}} // end namespace: clang::cxcursor 237 238#endif 239