Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 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 |
Ted Kremenek | 2cd10b0 | 2010-01-25 21:09:34 +0000 | [diff] [blame] | 15 | #define LLVM_CLANG_CXCURSOR_H |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 16 | |
| 17 | #include "clang-c/Index.h" |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceLocation.h" |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/PointerUnion.h" |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 20 | #include <utility> |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 21 | |
| 22 | namespace clang { |
| 23 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 24 | class ASTContext; |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 25 | class ASTUnit; |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 26 | class Attr; |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 27 | class CXXBaseSpecifier; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 28 | class Decl; |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 29 | class Expr; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 30 | class FieldDecl; |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 31 | class InclusionDirective; |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 32 | class LabelStmt; |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 33 | class MacroDefinition; |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 34 | class MacroExpansion; |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 35 | class NamedDecl; |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 36 | class ObjCInterfaceDecl; |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 37 | class ObjCProtocolDecl; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 38 | class OverloadedTemplateStorage; |
| 39 | class OverloadExpr; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 40 | class Stmt; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 41 | class TemplateDecl; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 42 | class TemplateName; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 43 | class TypeDecl; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 44 | |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 45 | namespace cxcursor { |
Argyrios Kyrtzidis | 671436e | 2011-09-27 00:30:33 +0000 | [diff] [blame] | 46 | |
| 47 | CXCursor getCursor(CXTranslationUnit, SourceLocation); |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 49 | CXCursor MakeCXCursor(const clang::Attr *A, clang::Decl *Parent, |
| 50 | CXTranslationUnit TU); |
| 51 | CXCursor MakeCXCursor(clang::Decl *D, CXTranslationUnit TU, |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 52 | SourceRange RegionOfInterest = SourceRange(), |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 53 | bool FirstInDeclGroup = true); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 54 | CXCursor MakeCXCursor(clang::Stmt *S, clang::Decl *Parent, |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 55 | CXTranslationUnit TU, |
| 56 | SourceRange RegionOfInterest = SourceRange()); |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 57 | CXCursor MakeCXCursorInvalid(CXCursorKind K); |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 58 | |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 59 | /// \brief Create an Objective-C superclass reference at the given location. |
| 60 | CXCursor MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 61 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 62 | CXTranslationUnit TU); |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 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> |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 67 | getCursorObjCSuperClassRef(CXCursor C); |
| 68 | |
| 69 | /// \brief Create an Objective-C protocol reference at the given location. |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame^] | 70 | CXCursor MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto, |
| 71 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 72 | CXTranslationUnit TU); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 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); |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 78 | |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 79 | /// \brief Create an Objective-C class reference at the given location. |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame^] | 80 | CXCursor MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class, |
| 81 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 82 | CXTranslationUnit TU); |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 89 | /// \brief Create a type reference at the given location. |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame^] | 90 | CXCursor MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 91 | CXTranslationUnit TU); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 92 | |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 97 | /// \brief Create a reference to a template at the given location. |
| 98 | CXCursor MakeCursorTemplateRef(TemplateDecl *Template, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 99 | CXTranslationUnit TU); |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 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); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 104 | |
| 105 | /// \brief Create a reference to a namespace or namespace alias at the given |
| 106 | /// location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 107 | CXCursor MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc, |
| 108 | CXTranslationUnit TU); |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 114 | /// \brief Create a reference to a field at the given location. |
| 115 | CXCursor MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 116 | CXTranslationUnit TU); |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 122 | /// \brief Create a CXX base specifier cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 123 | CXCursor MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B, |
| 124 | CXTranslationUnit TU); |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 125 | |
| 126 | /// \brief Unpack a CXXBaseSpecifier cursor into a CXXBaseSpecifier. |
| 127 | CXXBaseSpecifier *getCursorCXXBaseSpecifier(CXCursor C); |
| 128 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 129 | /// \brief Create a preprocessing directive cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 130 | CXCursor MakePreprocessingDirectiveCursor(SourceRange Range, |
| 131 | CXTranslationUnit TU); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 132 | |
| 133 | /// \brief Unpack a given preprocessing directive to retrieve its source range. |
| 134 | SourceRange getCursorPreprocessingDirective(CXCursor C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 135 | |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 136 | /// \brief Create a macro definition cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 137 | CXCursor MakeMacroDefinitionCursor(MacroDefinition *, CXTranslationUnit TU); |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 138 | |
| 139 | /// \brief Unpack a given macro definition cursor to retrieve its |
| 140 | /// source range. |
| 141 | MacroDefinition *getCursorMacroDefinition(CXCursor C); |
| 142 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 143 | /// \brief Create a macro expansion cursor. |
| 144 | CXCursor MakeMacroExpansionCursor(MacroExpansion *, |
| 145 | CXTranslationUnit TU); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 146 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 147 | /// \brief Unpack a given macro expansion cursor to retrieve its |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 148 | /// source range. |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 149 | MacroExpansion *getCursorMacroExpansion(CXCursor C); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 150 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 151 | /// \brief Create an inclusion directive cursor. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 152 | CXCursor MakeInclusionDirectiveCursor(InclusionDirective *, |
| 153 | CXTranslationUnit TU); |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 154 | |
| 155 | /// \brief Unpack a given inclusion directive cursor to retrieve its |
| 156 | /// source range. |
| 157 | InclusionDirective *getCursorInclusionDirective(CXCursor C); |
| 158 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 159 | /// \brief Create a label reference at the given location. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 160 | CXCursor MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, |
| 161 | CXTranslationUnit TU); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 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); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 166 | |
| 167 | /// \brief Create a overloaded declaration reference cursor for an expression. |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 168 | CXCursor MakeCursorOverloadedDeclRef(OverloadExpr *E, CXTranslationUnit TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 169 | |
| 170 | /// \brief Create a overloaded declaration reference cursor for a declaration. |
| 171 | CXCursor MakeCursorOverloadedDeclRef(Decl *D, SourceLocation Location, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 172 | CXTranslationUnit TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 173 | |
| 174 | /// \brief Create a overloaded declaration reference cursor for a template name. |
| 175 | CXCursor MakeCursorOverloadedDeclRef(TemplateName Template, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 176 | SourceLocation Location, |
| 177 | CXTranslationUnit TU); |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 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); |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 188 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 189 | Decl *getCursorDecl(CXCursor Cursor); |
| 190 | Expr *getCursorExpr(CXCursor Cursor); |
| 191 | Stmt *getCursorStmt(CXCursor Cursor); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 192 | Attr *getCursorAttr(CXCursor Cursor); |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 193 | Decl *getCursorParentDecl(CXCursor Cursor); |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 194 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 195 | ASTContext &getCursorContext(CXCursor Cursor); |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 196 | ASTUnit *getCursorASTUnit(CXCursor Cursor); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 197 | CXTranslationUnit getCursorTU(CXCursor Cursor); |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 198 | |
| 199 | void getOverriddenCursors(CXCursor cursor, |
| 200 | SmallVectorImpl<CXCursor> &overridden); |
| 201 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | b6a4ac4 | 2011-10-12 07:07:36 +0000 | [diff] [blame] | 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. |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame^] | 226 | bool getDeclCursorUSR(const Decl *D, SmallVectorImpl<char> &Buf); |
Argyrios Kyrtzidis | b6a4ac4 | 2011-10-12 07:07:36 +0000 | [diff] [blame] | 227 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 228 | bool operator==(CXCursor X, CXCursor Y); |
| 229 | |
| 230 | inline bool operator!=(CXCursor X, CXCursor Y) { |
| 231 | return !(X == Y); |
| 232 | } |
| 233 | |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 238 | }} // end namespace: clang::cxcursor |
| 239 | |
| 240 | #endif |