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 22 namespace clang { 23 24 class ASTContext; 25 class ASTUnit; 26 class Attr; 27 class CXXBaseSpecifier; 28 class Decl; 29 class Expr; 30 class FieldDecl; 31 class InclusionDirective; 32 class LabelStmt; 33 class MacroDefinition; 34 class MacroExpansion; 35 class NamedDecl; 36 class ObjCInterfaceDecl; 37 class ObjCProtocolDecl; 38 class OverloadedTemplateStorage; 39 class OverloadExpr; 40 class Stmt; 41 class TemplateDecl; 42 class TemplateName; 43 class TypeDecl; 44 class VarDecl; 45 46 namespace cxcursor { 47 48 CXCursor getCursor(CXTranslationUnit, SourceLocation); 49 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, 56 CXTranslationUnit TU, 57 SourceRange RegionOfInterest = SourceRange()); 58 CXCursor MakeCXCursorInvalid(CXCursorKind K); 59 60 /// \brief Create an Objective-C superclass reference at the given location. 61 CXCursor 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. 67 std::pair<ObjCInterfaceDecl *, SourceLocation> 68 getCursorObjCSuperClassRef(CXCursor C); 69 70 /// \brief Create an Objective-C protocol reference at the given location. 71 CXCursor 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. 77 std::pair<ObjCProtocolDecl *, SourceLocation> 78 getCursorObjCProtocolRef(CXCursor C); 79 80 /// \brief Create an Objective-C class reference at the given location. 81 CXCursor 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. 87 std::pair<ObjCInterfaceDecl *, SourceLocation> 88 getCursorObjCClassRef(CXCursor C); 89 90 /// \brief Create a type reference at the given location. 91 CXCursor 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. 96 std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C); 97 98 /// \brief Create a reference to a template at the given location. 99 CXCursor 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. 104 std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C); 105 106 /// \brief Create a reference to a namespace or namespace alias at the given 107 /// location. 108 CXCursor 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. 113 std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C); 114 115 /// \brief Create a reference to a variable at the given location. 116 CXCursor 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. 121 std::pair<VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C); 122 123 /// \brief Create a reference to a field at the given location. 124 CXCursor 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. 129 std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C); 130 131 /// \brief Create a CXX base specifier cursor. 132 CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B, 133 CXTranslationUnit TU); 134 135 /// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier. 136 CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C); 137 138 /// \brief Create a preprocessing directive cursor. 139 CXCursor MakePreprocessingDirectiveCursor(SourceRange Range, 140 CXTranslationUnit TU); 141 142 /// \brief Unpack a given preprocessing directive to retrieve its source range. 143 SourceRange getCursorPreprocessingDirective(CXCursor C); 144 145 /// \brief Create a macro definition cursor. 146 CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU); 147 148 /// \brief Unpack a given macro definition cursor to retrieve its 149 /// source range. 150 MacroDefinition *getCursorMacroDefinition(CXCursor C); 151 152 /// \brief Create a macro expansion cursor. 153 CXCursor MakeMacroExpansionCursor(MacroExpansion *, 154 CXTranslationUnit TU); 155 156 /// \brief Unpack a given macro expansion cursor to retrieve its 157 /// source range. 158 MacroExpansion *getCursorMacroExpansion(CXCursor C); 159 160 /// \brief Create an inclusion directive cursor. 161 CXCursor MakeInclusionDirectiveCursor(InclusionDirective *, 162 CXTranslationUnit TU); 163 164 /// \brief Unpack a given inclusion directive cursor to retrieve its 165 /// source range. 166 InclusionDirective *getCursorInclusionDirective(CXCursor C); 167 168 /// \brief Create a label reference at the given location. 169 CXCursor 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. 174 std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C); 175 176 /// \brief Create a overloaded declaration reference cursor for an expression. 177 CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU); 178 179 /// \brief Create a overloaded declaration reference cursor for a declaration. 180 CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location, 181 CXTranslationUnit TU); 182 183 /// \brief Create a overloaded declaration reference cursor for a template name. 184 CXCursor MakeCursorOverloadedDeclRef(TemplateName Template, 185 SourceLocation Location, 186 CXTranslationUnit TU); 187 188 /// \brief Internal storage for an overloaded declaration reference cursor; 189 typedef 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. 195 std::pair<OverloadedDeclRefStorage, SourceLocation> 196 getCursorOverloadedDeclRef(CXCursor C); 197 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); 203 204 ASTContext &getCursorContext(CXCursor Cursor); 205 ASTUnit *getCursorASTUnit(CXCursor Cursor); 206 CXTranslationUnit getCursorTU(CXCursor Cursor); 207 208 void 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. 213 std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor); 214 static inline int getSelectorIdentifierIndex(CXCursor cursor) { 215 return getSelectorIdentifierIndexAndLoc(cursor).first; 216 } 217 static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) { 218 return getSelectorIdentifierIndexAndLoc(cursor).second; 219 } 220 221 CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor); 222 223 static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) { 224 CXCursor newCursor = cursor; 225 if (cursor.kind == CXCursor_CallExpr) 226 newCursor.xdata = 1; 227 return newCursor; 228 } 229 230 CXCursor 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. 235 bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf); 236 237 bool operator==(CXCursor X, CXCursor Y); 238 239 inline 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. 245 bool isFirstInDeclGroup(CXCursor C); 246 247 }} // end namespace: clang::cxcursor 248 249 #endif 250