CXCursor.h revision 011d8b93b7cfa8492b8a9c909a850d6577e08dca
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; 44class VarDecl; 45 46namespace cxcursor { 47 48CXCursor getCursor(CXTranslationUnit, SourceLocation); 49 50CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent, 51 CXTranslationUnit TU); 52CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU, 53 SourceRange RegionOfInterest = SourceRange(), 54 bool FirstInDeclGroup = true); 55CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent, 56 CXTranslationUnit TU, 57 SourceRange RegionOfInterest = SourceRange()); 58CXCursor MakeCXCursorInvalid(CXCursorKind K); 59 60/// \brief Create an Objective-C superclass reference at the given location. 61CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, 62 SourceLocation Loc, 63 CXTranslationUnit TU); 64 65/// \brief Unpack an ObjCSuperClassRef cursor into the interface it references 66/// and optionally the location where the reference occurred. 67std::pair<ObjCInterfaceDecl *, SourceLocation> 68 getCursorObjCSuperClassRef(CXCursor C); 69 70/// \brief Create an Objective-C protocol reference at the given location. 71CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto, 72 SourceLocation Loc, 73 CXTranslationUnit TU); 74 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); 79 80/// \brief Create an Objective-C class reference at the given location. 81CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class, 82 SourceLocation Loc, 83 CXTranslationUnit TU); 84 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 90/// \brief Create a type reference at the given location. 91CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc, 92 CXTranslationUnit TU); 93 94/// \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 98/// \brief Create a reference to a template at the given location. 99CXCursor MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc, 100 CXTranslationUnit TU); 101 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); 105 106/// \brief Create a reference to a namespace or namespace alias at the given 107/// location. 108CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc, 109 CXTranslationUnit TU); 110 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 115/// \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 123/// \brief Create a reference to a field at the given location. 124CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc, 125 CXTranslationUnit TU); 126 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 131/// \brief Create a CXX base specifier cursor. 132CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B, 133 CXTranslationUnit TU); 134 135/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier. 136CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C); 137 138/// \brief Create a preprocessing directive cursor. 139CXCursor MakePreprocessingDirectiveCursor(SourceRange Range, 140 CXTranslationUnit TU); 141 142/// \brief Unpack a given preprocessing directive to retrieve its source range. 143SourceRange getCursorPreprocessingDirective(CXCursor C); 144 145/// \brief Create a macro definition cursor. 146CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU); 147 148/// \brief Unpack a given macro definition cursor to retrieve its 149/// source range. 150MacroDefinition *getCursorMacroDefinition(CXCursor C); 151 152/// \brief Create a macro expansion cursor. 153CXCursor MakeMacroExpansionCursor(MacroExpansion *, 154 CXTranslationUnit TU); 155 156/// \brief Unpack a given macro expansion cursor to retrieve its 157/// source range. 158MacroExpansion *getCursorMacroExpansion(CXCursor C); 159 160/// \brief Create an inclusion directive cursor. 161CXCursor MakeInclusionDirectiveCursor(InclusionDirective *, 162 CXTranslationUnit TU); 163 164/// \brief Unpack a given inclusion directive cursor to retrieve its 165/// source range. 166InclusionDirective *getCursorInclusionDirective(CXCursor C); 167 168/// \brief Create a label reference at the given location. 169CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, 170 CXTranslationUnit TU); 171 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); 175 176/// \brief Create a overloaded declaration reference cursor for an expression. 177CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU); 178 179/// \brief Create a overloaded declaration reference cursor for a declaration. 180CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location, 181 CXTranslationUnit TU); 182 183/// \brief Create a overloaded declaration reference cursor for a template name. 184CXCursor MakeCursorOverloadedDeclRef(TemplateName Template, 185 SourceLocation Location, 186 CXTranslationUnit TU); 187 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); 197 198Decl *getCursorDecl(CXCursor Cursor); 199Expr *getCursorExpr(CXCursor Cursor); 200Stmt *getCursorStmt(CXCursor Cursor); 201Attr *getCursorAttr(CXCursor Cursor); 202Decl *getCursorParentDecl(CXCursor Cursor); 203 204ASTContext &getCursorContext(CXCursor Cursor); 205ASTUnit *getCursorASTUnit(CXCursor Cursor); 206CXTranslationUnit getCursorTU(CXCursor Cursor); 207 208void getOverriddenCursors(CXCursor cursor, 209 SmallVectorImpl<CXCursor> &overridden); 210 211/// \brief Returns a index/location pair for a selector identifier if the cursor 212/// points to one. 213std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor); 214static inline int getSelectorIdentifierIndex(CXCursor cursor) { 215 return getSelectorIdentifierIndexAndLoc(cursor).first; 216} 217static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) { 218 return getSelectorIdentifierIndexAndLoc(cursor).second; 219} 220 221CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor); 222 223static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) { 224 CXCursor newCursor = cursor; 225 if (cursor.kind == CXCursor_CallExpr) 226 newCursor.xdata = 1; 227 return newCursor; 228} 229 230CXCursor getTypeRefCursor(CXCursor cursor); 231 232/// \brief Generate a USR for \arg D and put it in \arg Buf. 233/// \returns true if no USR was computed or the result should be ignored, 234/// false otherwise. 235bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf); 236 237bool operator==(CXCursor X, CXCursor Y); 238 239inline bool operator!=(CXCursor X, CXCursor Y) { 240 return !(X == Y); 241} 242 243/// \brief Return true if the cursor represents a declaration that is the 244/// first in a declaration group. 245bool isFirstInDeclGroup(CXCursor C); 246 247}} // end namespace: clang::cxcursor 248 249#endif 250