CXCursor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //===- CXCursor.h - Routines for manipulating CXCursors -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines routines for manipulating CXCursors.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
  13. #define LLVM_CLANG_TOOLS_LIBCLANG_CXCURSOR_H
  14. #include "clang-c/Index.h"
  15. #include "clang/Basic/SourceLocation.h"
  16. #include "llvm/ADT/PointerUnion.h"
  17. #include <utility>
  18. namespace clang {
  19. class ASTContext;
  20. class ASTUnit;
  21. class Attr;
  22. class CXXBaseSpecifier;
  23. class Decl;
  24. class Expr;
  25. class FieldDecl;
  26. class InclusionDirective;
  27. class LabelStmt;
  28. class MacroDefinitionRecord;
  29. class MacroExpansion;
  30. class NamedDecl;
  31. class ObjCInterfaceDecl;
  32. class ObjCProtocolDecl;
  33. class OverloadedTemplateStorage;
  34. class OverloadExpr;
  35. class Stmt;
  36. class TemplateDecl;
  37. class TemplateName;
  38. class TypeDecl;
  39. class VarDecl;
  40. class IdentifierInfo;
  41. namespace cxcursor {
  42. CXCursor getCursor(CXTranslationUnit, SourceLocation);
  43. CXCursor MakeCXCursor(const clang::Attr *A, const clang::Decl *Parent,
  44. CXTranslationUnit TU);
  45. CXCursor MakeCXCursor(const clang::Decl *D, CXTranslationUnit TU,
  46. SourceRange RegionOfInterest = SourceRange(),
  47. bool FirstInDeclGroup = true);
  48. CXCursor MakeCXCursor(const clang::Stmt *S, const clang::Decl *Parent,
  49. CXTranslationUnit TU,
  50. SourceRange RegionOfInterest = SourceRange());
  51. CXCursor MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU = nullptr);
  52. /// Create an Objective-C superclass reference at the given location.
  53. CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
  54. SourceLocation Loc, CXTranslationUnit TU);
  55. /// Unpack an ObjCSuperClassRef cursor into the interface it references
  56. /// and optionally the location where the reference occurred.
  57. std::pair<const ObjCInterfaceDecl *, SourceLocation>
  58. getCursorObjCSuperClassRef(CXCursor C);
  59. /// Create an Objective-C protocol reference at the given location.
  60. CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
  61. SourceLocation Loc, CXTranslationUnit TU);
  62. /// Unpack an ObjCProtocolRef cursor into the protocol it references
  63. /// and optionally the location where the reference occurred.
  64. std::pair<const ObjCProtocolDecl *, SourceLocation>
  65. getCursorObjCProtocolRef(CXCursor C);
  66. /// Create an Objective-C class reference at the given location.
  67. CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
  68. SourceLocation Loc, CXTranslationUnit TU);
  69. /// Unpack an ObjCClassRef cursor into the class it references
  70. /// and optionally the location where the reference occurred.
  71. std::pair<const ObjCInterfaceDecl *, SourceLocation>
  72. getCursorObjCClassRef(CXCursor C);
  73. /// Create a type reference at the given location.
  74. CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
  75. CXTranslationUnit TU);
  76. /// Unpack a TypeRef cursor into the class it references
  77. /// and optionally the location where the reference occurred.
  78. std::pair<const TypeDecl *, SourceLocation> getCursorTypeRef(CXCursor C);
  79. /// Create a reference to a template at the given location.
  80. CXCursor MakeCursorTemplateRef(const TemplateDecl *Template, SourceLocation Loc,
  81. CXTranslationUnit TU);
  82. /// Unpack a TemplateRef cursor into the template it references and
  83. /// the location where the reference occurred.
  84. std::pair<const TemplateDecl *, SourceLocation>
  85. getCursorTemplateRef(CXCursor C);
  86. /// Create a reference to a namespace or namespace alias at the given
  87. /// location.
  88. CXCursor MakeCursorNamespaceRef(const NamedDecl *NS, SourceLocation Loc,
  89. CXTranslationUnit TU);
  90. /// Unpack a NamespaceRef cursor into the namespace or namespace alias
  91. /// it references and the location where the reference occurred.
  92. std::pair<const NamedDecl *, SourceLocation> getCursorNamespaceRef(CXCursor C);
  93. /// Create a reference to a variable at the given location.
  94. CXCursor MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
  95. CXTranslationUnit TU);
  96. /// Unpack a VariableRef cursor into the variable it references and the
  97. /// location where the where the reference occurred.
  98. std::pair<const VarDecl *, SourceLocation> getCursorVariableRef(CXCursor C);
  99. /// Create a reference to a field at the given location.
  100. CXCursor MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
  101. CXTranslationUnit TU);
  102. /// Unpack a MemberRef cursor into the field it references and the
  103. /// location where the reference occurred.
  104. std::pair<const FieldDecl *, SourceLocation> getCursorMemberRef(CXCursor C);
  105. /// Create a CXX base specifier cursor.
  106. CXCursor MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
  107. CXTranslationUnit TU);
  108. /// Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier.
  109. const CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C);
  110. /// Create a preprocessing directive cursor.
  111. CXCursor MakePreprocessingDirectiveCursor(SourceRange Range,
  112. CXTranslationUnit TU);
  113. /// Unpack a given preprocessing directive to retrieve its source range.
  114. SourceRange getCursorPreprocessingDirective(CXCursor C);
  115. /// Create a macro definition cursor.
  116. CXCursor MakeMacroDefinitionCursor(const MacroDefinitionRecord *,
  117. CXTranslationUnit TU);
  118. /// Unpack a given macro definition cursor to retrieve its
  119. /// source range.
  120. const MacroDefinitionRecord *getCursorMacroDefinition(CXCursor C);
  121. /// Create a macro expansion cursor.
  122. CXCursor MakeMacroExpansionCursor(MacroExpansion *, CXTranslationUnit TU);
  123. /// Create a "pseudo" macro expansion cursor, using a macro definition
  124. /// and a source location.
  125. CXCursor MakeMacroExpansionCursor(MacroDefinitionRecord *, SourceLocation Loc,
  126. CXTranslationUnit TU);
  127. /// Wraps a macro expansion cursor and provides a common interface
  128. /// for a normal macro expansion cursor or a "pseudo" one.
  129. ///
  130. /// "Pseudo" macro expansion cursors (essentially a macro definition along with
  131. /// a source location) are created in special cases, for example they can be
  132. /// created for identifiers inside macro definitions, if these identifiers are
  133. /// macro names.
  134. class MacroExpansionCursor {
  135. CXCursor C;
  136. bool isPseudo() const { return C.data[1] != nullptr; }
  137. const MacroDefinitionRecord *getAsMacroDefinition() const {
  138. assert(isPseudo());
  139. return static_cast<const MacroDefinitionRecord *>(C.data[0]);
  140. }
  141. const MacroExpansion *getAsMacroExpansion() const {
  142. assert(!isPseudo());
  143. return static_cast<const MacroExpansion *>(C.data[0]);
  144. }
  145. SourceLocation getPseudoLoc() const {
  146. assert(isPseudo());
  147. return SourceLocation::getFromPtrEncoding(C.data[1]);
  148. }
  149. public:
  150. MacroExpansionCursor(CXCursor C) : C(C) {
  151. assert(C.kind == CXCursor_MacroExpansion);
  152. }
  153. const IdentifierInfo *getName() const;
  154. const MacroDefinitionRecord *getDefinition() const;
  155. SourceRange getSourceRange() const;
  156. };
  157. /// Unpack a given macro expansion cursor to retrieve its info.
  158. static inline MacroExpansionCursor getCursorMacroExpansion(CXCursor C) {
  159. return C;
  160. }
  161. /// Create an inclusion directive cursor.
  162. CXCursor MakeInclusionDirectiveCursor(InclusionDirective *,
  163. CXTranslationUnit TU);
  164. /// Unpack a given inclusion directive cursor to retrieve its
  165. /// source range.
  166. const InclusionDirective *getCursorInclusionDirective(CXCursor C);
  167. /// Create a label reference at the given location.
  168. CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
  169. CXTranslationUnit TU);
  170. /// Unpack a label reference into the label statement it refers to and
  171. /// the location of the reference.
  172. std::pair<const LabelStmt *, SourceLocation> getCursorLabelRef(CXCursor C);
  173. /// Create a overloaded declaration reference cursor for an expression.
  174. CXCursor MakeCursorOverloadedDeclRef(const OverloadExpr *E,
  175. CXTranslationUnit TU);
  176. /// Create a overloaded declaration reference cursor for a declaration.
  177. CXCursor MakeCursorOverloadedDeclRef(const Decl *D, SourceLocation Location,
  178. CXTranslationUnit TU);
  179. /// Create a overloaded declaration reference cursor for a template name.
  180. CXCursor MakeCursorOverloadedDeclRef(TemplateName Template,
  181. SourceLocation Location,
  182. CXTranslationUnit TU);
  183. /// Internal storage for an overloaded declaration reference cursor;
  184. typedef llvm::PointerUnion<const OverloadExpr *, const Decl *,
  185. OverloadedTemplateStorage *>
  186. OverloadedDeclRefStorage;
  187. /// Unpack an overloaded declaration reference into an expression,
  188. /// declaration, or template name along with the source location.
  189. std::pair<OverloadedDeclRefStorage, SourceLocation>
  190. getCursorOverloadedDeclRef(CXCursor C);
  191. const Decl *getCursorDecl(CXCursor Cursor);
  192. const Expr *getCursorExpr(CXCursor Cursor);
  193. const Stmt *getCursorStmt(CXCursor Cursor);
  194. const Attr *getCursorAttr(CXCursor Cursor);
  195. ASTContext &getCursorContext(CXCursor Cursor);
  196. ASTUnit *getCursorASTUnit(CXCursor Cursor);
  197. CXTranslationUnit getCursorTU(CXCursor Cursor);
  198. void getOverriddenCursors(CXCursor cursor,
  199. SmallVectorImpl<CXCursor> &overridden);
  200. /// Create an opaque pool used for fast generation of overridden
  201. /// CXCursor arrays.
  202. void *createOverridenCXCursorsPool();
  203. /// Dispose of the overridden CXCursors pool.
  204. void disposeOverridenCXCursorsPool(void *pool);
  205. /// Returns a index/location pair for a selector identifier if the cursor
  206. /// points to one.
  207. std::pair<int, SourceLocation> getSelectorIdentifierIndexAndLoc(CXCursor);
  208. static inline int getSelectorIdentifierIndex(CXCursor cursor) {
  209. return getSelectorIdentifierIndexAndLoc(cursor).first;
  210. }
  211. static inline SourceLocation getSelectorIdentifierLoc(CXCursor cursor) {
  212. return getSelectorIdentifierIndexAndLoc(cursor).second;
  213. }
  214. CXCursor getSelectorIdentifierCursor(int SelIdx, CXCursor cursor);
  215. static inline CXCursor getTypeRefedCallExprCursor(CXCursor cursor) {
  216. CXCursor newCursor = cursor;
  217. if (cursor.kind == CXCursor_CallExpr)
  218. newCursor.xdata = 1;
  219. return newCursor;
  220. }
  221. CXCursor getTypeRefCursor(CXCursor cursor);
  222. /// Generate a USR for \arg D and put it in \arg Buf.
  223. /// \returns true if no USR was computed or the result should be ignored,
  224. /// false otherwise.
  225. bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf);
  226. bool operator==(CXCursor X, CXCursor Y);
  227. inline bool operator!=(CXCursor X, CXCursor Y) { return !(X == Y); }
  228. /// Return true if the cursor represents a declaration that is the
  229. /// first in a declaration group.
  230. bool isFirstInDeclGroup(CXCursor C);
  231. } // namespace cxcursor
  232. } // namespace clang
  233. #endif