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 45 namespace cxcursor { 46 47 CXCursor getCursor(CXTranslationUnit, SourceLocation); 48 49 CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent, 50 CXTranslationUnit TU); 51 CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU, 52 SourceRange RegionOfInterest = SourceRange(), 53 bool FirstInDeclGroup = true); 54 CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent, 55 CXTranslationUnit TU, 56 SourceRange RegionOfInterest = SourceRange()); 57 CXCursor MakeCXCursorInvalid(CXCursorKind K); 58 59 /// \brief Create an Objective-C superclass reference at the given location. 60 CXCursor 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. 66 std::pair<ObjCInterfaceDecl *, SourceLocation> 67 getCursorObjCSuperClassRef(CXCursor C); 68 69 /// \brief Create an Objective-C protocol reference at the given location. 70 CXCursor 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. 76 std::pair<ObjCProtocolDecl *, SourceLocation> 77 getCursorObjCProtocolRef(CXCursor C); 78 79 /// \brief Create an Objective-C class reference at the given location. 80 CXCursor 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. 86 std::pair<ObjCInterfaceDecl *, SourceLocation> 87 getCursorObjCClassRef(CXCursor C); 88 89 /// \brief Create a type reference at the given location. 90 CXCursor 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. 95 std::pair<TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C); 96 97 /// \brief Create a reference to a template at the given location. 98 CXCursor 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. 103 std::pair<TemplateDecl *, SourceLocation> getCursorTemplateRef(CXCursor C); 104 105 /// \brief Create a reference to a namespace or namespace alias at the given 106 /// location. 107 CXCursor 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. 112 std::pair<NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C); 113 114 /// \brief Create a reference to a field at the given location. 115 CXCursor 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. 120 std::pair<FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C); 121 122 /// \brief Create a CXX base specifier cursor. 123 CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, 124 CXTranslationUnit TU); 125 126 /// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier. 127 CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C); 128 129 /// \brief Create a preprocessing directive cursor. 130 CXCursor MakePreprocessingDirectiveCursor(SourceRange Range, 131 CXTranslationUnit TU); 132 133 /// \brief Unpack a given preprocessing directive to retrieve its source range. 134 SourceRange getCursorPreprocessingDirective(CXCursor C); 135 136 /// \brief Create a macro definition cursor. 137 CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU); 138 139 /// \brief Unpack a given macro definition cursor to retrieve its 140 /// source range. 141 MacroDefinition *getCursorMacroDefinition(CXCursor C); 142 143 /// \brief Create a macro expansion cursor. 144 CXCursor MakeMacroExpansionCursor(MacroExpansion *, 145 CXTranslationUnit TU); 146 147 /// \brief Unpack a given macro expansion cursor to retrieve its 148 /// source range. 149 MacroExpansion *getCursorMacroExpansion(CXCursor C); 150 151 /// \brief Create an inclusion directive cursor. 152 CXCursor MakeInclusionDirectiveCursor(InclusionDirective *, 153 CXTranslationUnit TU); 154 155 /// \brief Unpack a given inclusion directive cursor to retrieve its 156 /// source range. 157 InclusionDirective *getCursorInclusionDirective(CXCursor C); 158 159 /// \brief Create a label reference at the given location. 160 CXCursor 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. 165 std::pair<LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C); 166 167 /// \brief Create a overloaded declaration reference cursor for an expression. 168 CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU); 169 170 /// \brief Create a overloaded declaration reference cursor for a declaration. 171 CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location, 172 CXTranslationUnit TU); 173 174 /// \brief Create a overloaded declaration reference cursor for a template name. 175 CXCursor MakeCursorOverloadedDeclRef(TemplateName Template, 176 SourceLocation Location, 177 CXTranslationUnit TU); 178 179 /// \brief Internal storage for an overloaded declaration reference cursor; 180 typedef 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. 186 std::pair<OverloadedDeclRefStorage, SourceLocation> 187 getCursorOverloadedDeclRef(CXCursor C); 188 189 Decl *getCursorDecl(CXCursor Cursor); 190 Expr *getCursorExpr(CXCursor Cursor); 191 Stmt *getCursorStmt(CXCursor Cursor); 192 Attr *getCursorAttr(CXCursor Cursor); 193 Decl *getCursorParentDecl(CXCursor Cursor); 194 195 ASTContext &getCursorContext(CXCursor Cursor); 196 ASTUnit *getCursorASTUnit(CXCursor Cursor); 197 CXTranslationUnit getCursorTU(CXCursor Cursor); 198 199 void 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. 204 std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor); 205 static inline int getSelectorIdentifierIndex(CXCursor cursor) { 206 return getSelectorIdentifierIndexAndLoc(cursor).first; 207 } 208 static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) { 209 return getSelectorIdentifierIndexAndLoc(cursor).second; 210 } 211 212 CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor); 213 214 static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) { 215 CXCursor newCursor = cursor; 216 if (cursor.kind == CXCursor_CallExpr) 217 newCursor.xdata = 1; 218 return newCursor; 219 } 220 221 CXCursor 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. 226 bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf); 227 228 bool operator==(CXCursor X, CXCursor Y); 229 230 inline 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. 236 bool isFirstInDeclGroup(CXCursor C); 237 238 }} // end namespace: clang::cxcursor 239 240 #endif 241