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(const ObjCProtocolDecl *Proto, 71 SourceLocation Loc, 72 CXTranslationUnit TU); 73 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); 78 79/// \brief Create an Objective-C class reference at the given location. 80CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class, 81 SourceLocation Loc, 82 CXTranslationUnit TU); 83 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 89/// \brief Create a type reference at the given location. 90CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc, 91 CXTranslationUnit TU); 92 93/// \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 97/// \brief Create a reference to a template at the given location. 98CXCursor MakeCursorTemplateRef(TemplateDecl *Template, SourceLocation Loc, 99 CXTranslationUnit TU); 100 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); 104 105/// \brief Create a reference to a namespace or namespace alias at the given 106/// location. 107CXCursor MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, 108 CXTranslationUnit TU); 109 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 114/// \brief Create a reference to a field at the given location. 115CXCursor MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc, 116 CXTranslationUnit TU); 117 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 122/// \brief Create a CXX base specifier cursor. 123CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, 124 CXTranslationUnit TU); 125 126/// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier. 127CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C); 128 129/// \brief Create a preprocessing directive cursor. 130CXCursor MakePreprocessingDirectiveCursor(SourceRange Range, 131 CXTranslationUnit TU); 132 133/// \brief Unpack a given preprocessing directive to retrieve its source range. 134SourceRange getCursorPreprocessingDirective(CXCursor C); 135 136/// \brief Create a macro definition cursor. 137CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU); 138 139/// \brief Unpack a given macro definition cursor to retrieve its 140/// source range. 141MacroDefinition *getCursorMacroDefinition(CXCursor C); 142 143/// \brief Create a macro expansion cursor. 144CXCursor MakeMacroExpansionCursor(MacroExpansion *, 145 CXTranslationUnit TU); 146 147/// \brief Unpack a given macro expansion cursor to retrieve its 148/// source range. 149MacroExpansion *getCursorMacroExpansion(CXCursor C); 150 151/// \brief Create an inclusion directive cursor. 152CXCursor MakeInclusionDirectiveCursor(InclusionDirective *, 153 CXTranslationUnit TU); 154 155/// \brief Unpack a given inclusion directive cursor to retrieve its 156/// source range. 157InclusionDirective *getCursorInclusionDirective(CXCursor C); 158 159/// \brief Create a label reference at the given location. 160CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, 161 CXTranslationUnit TU); 162 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); 166 167/// \brief Create a overloaded declaration reference cursor for an expression. 168CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU); 169 170/// \brief Create a overloaded declaration reference cursor for a declaration. 171CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location, 172 CXTranslationUnit TU); 173 174/// \brief Create a overloaded declaration reference cursor for a template name. 175CXCursor MakeCursorOverloadedDeclRef(TemplateName Template, 176 SourceLocation Location, 177 CXTranslationUnit TU); 178 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); 188 189Decl *getCursorDecl(CXCursor Cursor); 190Expr *getCursorExpr(CXCursor Cursor); 191Stmt *getCursorStmt(CXCursor Cursor); 192Attr *getCursorAttr(CXCursor Cursor); 193Decl *getCursorParentDecl(CXCursor Cursor); 194 195ASTContext &getCursorContext(CXCursor Cursor); 196ASTUnit *getCursorASTUnit(CXCursor Cursor); 197CXTranslationUnit getCursorTU(CXCursor Cursor); 198 199void getOverriddenCursors(CXCursor cursor, 200 SmallVectorImpl<CXCursor> &overridden); 201 202/// \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 223/// \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. 226bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf); 227 228bool operator==(CXCursor X, CXCursor Y); 229 230inline bool operator!=(CXCursor X, CXCursor Y) { 231 return !(X == Y); 232} 233 234/// \brief Return true if the cursor represents a declaration that is the 235/// first in a declaration group. 236bool isFirstInDeclGroup(CXCursor C); 237 238}} // end namespace: clang::cxcursor 239 240#endif 241