Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 1 | //===- CXCursor.cpp - 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 | // |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 10 | // This file defines routines for manipulating CXCursors. It should be the |
| 11 | // only file that has internal knowledge of the encoding of the data in |
| 12 | // CXCursor. |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Ted Kremenek | 0a90d32 | 2010-11-17 23:24:11 +0000 | [diff] [blame] | 16 | #include "CXTranslationUnit.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 17 | #include "CXCursor.h" |
Ted Kremenek | ed12273 | 2010-11-16 01:56:27 +0000 | [diff] [blame] | 18 | #include "CXString.h" |
Douglas Gregor | 7eaa8ae | 2010-01-20 00:23:15 +0000 | [diff] [blame] | 19 | #include "clang/Frontend/ASTUnit.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/Decl.h" |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclObjC.h" |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 23 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 27 | #include "clang-c/Index.h" |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace clang; |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 31 | using namespace cxcursor; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 32 | |
Ted Kremenek | bbf66ca | 2012-04-30 19:06:49 +0000 | [diff] [blame] | 33 | CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) { |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 34 | assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid); |
Ted Kremenek | bbf66ca | 2012-04-30 19:06:49 +0000 | [diff] [blame] | 35 | CXCursor C = { K, 0, { 0, 0, TU } }; |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 36 | return C; |
Ted Kremenek | 16c440a | 2010-01-15 20:35:54 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 39 | static CXCursorKind GetCursorKind(const Attr *A) { |
| 40 | assert(A && "Invalid arguments!"); |
| 41 | switch (A->getKind()) { |
| 42 | default: break; |
Sean Hunt | 387475d | 2010-06-16 23:43:53 +0000 | [diff] [blame] | 43 | case attr::IBAction: return CXCursor_IBActionAttr; |
| 44 | case attr::IBOutlet: return CXCursor_IBOutletAttr; |
| 45 | case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr; |
Argyrios Kyrtzidis | 6639e92 | 2011-09-13 17:39:31 +0000 | [diff] [blame] | 46 | case attr::Final: return CXCursor_CXXFinalAttr; |
| 47 | case attr::Override: return CXCursor_CXXOverrideAttr; |
Erik Verbruggen | 5f1c822 | 2011-10-13 09:41:32 +0000 | [diff] [blame] | 48 | case attr::Annotate: return CXCursor_AnnotateAttr; |
Argyrios Kyrtzidis | 84b7964 | 2011-12-06 22:05:01 +0000 | [diff] [blame] | 49 | case attr::AsmLabel: return CXCursor_AsmLabelAttr; |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return CXCursor_UnexposedAttr; |
| 53 | } |
| 54 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 55 | CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent, |
| 56 | CXTranslationUnit TU) { |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 57 | assert(A && Parent && TU && "Invalid arguments!"); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 58 | CXCursor C = { GetCursorKind(A), 0, { Parent, (void*)A, TU } }; |
Ted Kremenek | e77f443 | 2010-02-18 03:09:07 +0000 | [diff] [blame] | 59 | return C; |
| 60 | } |
| 61 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 62 | CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU, |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 63 | SourceRange RegionOfInterest, |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 64 | bool FirstInDeclGroup) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 65 | assert(D && TU && "Invalid arguments!"); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 66 | |
| 67 | CXCursorKind K = getCursorKindForDecl(D); |
| 68 | |
| 69 | if (K == CXCursor_ObjCClassMethodDecl || |
| 70 | K == CXCursor_ObjCInstanceMethodDecl) { |
| 71 | int SelectorIdIndex = -1; |
| 72 | // Check if cursor points to a selector id. |
| 73 | if (RegionOfInterest.isValid() && |
| 74 | RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) { |
| 75 | SmallVector<SourceLocation, 16> SelLocs; |
| 76 | cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs); |
| 77 | SmallVector<SourceLocation, 16>::iterator |
| 78 | I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin()); |
| 79 | if (I != SelLocs.end()) |
| 80 | SelectorIdIndex = I - SelLocs.begin(); |
| 81 | } |
| 82 | CXCursor C = { K, SelectorIdIndex, |
| 83 | { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }}; |
| 84 | return C; |
| 85 | } |
| 86 | |
| 87 | CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }}; |
Douglas Gregor | 5bfb8c1 | 2010-01-20 23:34:41 +0000 | [diff] [blame] | 88 | return C; |
Ted Kremenek | edc8aa6 | 2010-01-16 00:36:30 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 91 | CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU, |
| 92 | SourceRange RegionOfInterest) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 93 | assert(S && TU && "Invalid arguments!"); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 94 | CXCursorKind K = CXCursor_NotImplemented; |
| 95 | |
| 96 | switch (S->getStmtClass()) { |
| 97 | case Stmt::NoStmtClass: |
| 98 | break; |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 99 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 100 | case Stmt::CaseStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 101 | K = CXCursor_CaseStmt; |
| 102 | break; |
| 103 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 104 | case Stmt::DefaultStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 105 | K = CXCursor_DefaultStmt; |
| 106 | break; |
| 107 | |
| 108 | case Stmt::IfStmtClass: |
| 109 | K = CXCursor_IfStmt; |
| 110 | break; |
| 111 | |
| 112 | case Stmt::SwitchStmtClass: |
| 113 | K = CXCursor_SwitchStmt; |
| 114 | break; |
| 115 | |
| 116 | case Stmt::WhileStmtClass: |
| 117 | K = CXCursor_WhileStmt; |
| 118 | break; |
| 119 | |
| 120 | case Stmt::DoStmtClass: |
| 121 | K = CXCursor_DoStmt; |
| 122 | break; |
| 123 | |
| 124 | case Stmt::ForStmtClass: |
| 125 | K = CXCursor_ForStmt; |
| 126 | break; |
| 127 | |
| 128 | case Stmt::GotoStmtClass: |
| 129 | K = CXCursor_GotoStmt; |
| 130 | break; |
| 131 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 132 | case Stmt::IndirectGotoStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 133 | K = CXCursor_IndirectGotoStmt; |
| 134 | break; |
| 135 | |
| 136 | case Stmt::ContinueStmtClass: |
| 137 | K = CXCursor_ContinueStmt; |
| 138 | break; |
| 139 | |
| 140 | case Stmt::BreakStmtClass: |
| 141 | K = CXCursor_BreakStmt; |
| 142 | break; |
| 143 | |
| 144 | case Stmt::ReturnStmtClass: |
| 145 | K = CXCursor_ReturnStmt; |
| 146 | break; |
| 147 | |
| 148 | case Stmt::AsmStmtClass: |
| 149 | K = CXCursor_AsmStmt; |
| 150 | break; |
| 151 | |
| 152 | case Stmt::ObjCAtTryStmtClass: |
| 153 | K = CXCursor_ObjCAtTryStmt; |
| 154 | break; |
| 155 | |
| 156 | case Stmt::ObjCAtCatchStmtClass: |
| 157 | K = CXCursor_ObjCAtCatchStmt; |
| 158 | break; |
| 159 | |
| 160 | case Stmt::ObjCAtFinallyStmtClass: |
| 161 | K = CXCursor_ObjCAtFinallyStmt; |
| 162 | break; |
| 163 | |
| 164 | case Stmt::ObjCAtThrowStmtClass: |
| 165 | K = CXCursor_ObjCAtThrowStmt; |
| 166 | break; |
| 167 | |
| 168 | case Stmt::ObjCAtSynchronizedStmtClass: |
| 169 | K = CXCursor_ObjCAtSynchronizedStmt; |
| 170 | break; |
| 171 | |
| 172 | case Stmt::ObjCAutoreleasePoolStmtClass: |
| 173 | K = CXCursor_ObjCAutoreleasePoolStmt; |
| 174 | break; |
| 175 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 176 | case Stmt::ObjCForCollectionStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 177 | K = CXCursor_ObjCForCollectionStmt; |
| 178 | break; |
| 179 | |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 180 | case Stmt::CXXCatchStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 181 | K = CXCursor_CXXCatchStmt; |
| 182 | break; |
| 183 | |
| 184 | case Stmt::CXXTryStmtClass: |
| 185 | K = CXCursor_CXXTryStmt; |
| 186 | break; |
| 187 | |
| 188 | case Stmt::CXXForRangeStmtClass: |
| 189 | K = CXCursor_CXXForRangeStmt; |
| 190 | break; |
| 191 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 192 | case Stmt::SEHTryStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 193 | K = CXCursor_SEHTryStmt; |
| 194 | break; |
| 195 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 196 | case Stmt::SEHExceptStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 197 | K = CXCursor_SEHExceptStmt; |
| 198 | break; |
| 199 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 200 | case Stmt::SEHFinallyStmtClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 201 | K = CXCursor_SEHFinallyStmt; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 202 | break; |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 203 | |
John Wiegley | 21ff2e5 | 2011-04-28 00:16:57 +0000 | [diff] [blame] | 204 | case Stmt::ArrayTypeTraitExprClass: |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 205 | case Stmt::AsTypeExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 206 | case Stmt::AtomicExprClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 207 | case Stmt::BinaryConditionalOperatorClass: |
| 208 | case Stmt::BinaryTypeTraitExprClass: |
Douglas Gregor | 4ca8ac2 | 2012-02-24 07:38:34 +0000 | [diff] [blame] | 209 | case Stmt::TypeTraitExprClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 210 | case Stmt::CXXBindTemporaryExprClass: |
| 211 | case Stmt::CXXDefaultArgExprClass: |
| 212 | case Stmt::CXXScalarValueInitExprClass: |
| 213 | case Stmt::CXXUuidofExprClass: |
| 214 | case Stmt::ChooseExprClass: |
| 215 | case Stmt::DesignatedInitExprClass: |
| 216 | case Stmt::ExprWithCleanupsClass: |
| 217 | case Stmt::ExpressionTraitExprClass: |
| 218 | case Stmt::ExtVectorElementExprClass: |
| 219 | case Stmt::ImplicitCastExprClass: |
| 220 | case Stmt::ImplicitValueInitExprClass: |
| 221 | case Stmt::MaterializeTemporaryExprClass: |
| 222 | case Stmt::ObjCIndirectCopyRestoreExprClass: |
| 223 | case Stmt::OffsetOfExprClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 224 | case Stmt::ParenListExprClass: |
| 225 | case Stmt::PredefinedExprClass: |
| 226 | case Stmt::ShuffleVectorExprClass: |
| 227 | case Stmt::UnaryExprOrTypeTraitExprClass: |
| 228 | case Stmt::UnaryTypeTraitExprClass: |
| 229 | case Stmt::VAArgExprClass: |
Ted Kremenek | b3f7542 | 2012-03-06 20:06:06 +0000 | [diff] [blame] | 230 | case Stmt::ObjCArrayLiteralClass: |
| 231 | case Stmt::ObjCDictionaryLiteralClass: |
Patrick Beard | eb382ec | 2012-04-19 00:25:12 +0000 | [diff] [blame] | 232 | case Stmt::ObjCBoxedExprClass: |
Ted Kremenek | b3f7542 | 2012-03-06 20:06:06 +0000 | [diff] [blame] | 233 | case Stmt::ObjCSubscriptRefExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 234 | K = CXCursor_UnexposedExpr; |
| 235 | break; |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 236 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 237 | case Stmt::OpaqueValueExprClass: |
| 238 | if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr()) |
| 239 | return MakeCXCursor(Src, Parent, TU, RegionOfInterest); |
| 240 | K = CXCursor_UnexposedExpr; |
| 241 | break; |
| 242 | |
| 243 | case Stmt::PseudoObjectExprClass: |
| 244 | return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), |
| 245 | Parent, TU, RegionOfInterest); |
| 246 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 247 | case Stmt::CompoundStmtClass: |
| 248 | K = CXCursor_CompoundStmt; |
| 249 | break; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 251 | case Stmt::NullStmtClass: |
| 252 | K = CXCursor_NullStmt; |
| 253 | break; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 254 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 255 | case Stmt::LabelStmtClass: |
| 256 | K = CXCursor_LabelStmt; |
| 257 | break; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 258 | |
| 259 | case Stmt::AttributedStmtClass: |
| 260 | K = CXCursor_UnexposedStmt; |
| 261 | break; |
| 262 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 263 | case Stmt::DeclStmtClass: |
| 264 | K = CXCursor_DeclStmt; |
| 265 | break; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 267 | case Stmt::IntegerLiteralClass: |
| 268 | K = CXCursor_IntegerLiteral; |
| 269 | break; |
| 270 | |
| 271 | case Stmt::FloatingLiteralClass: |
| 272 | K = CXCursor_FloatingLiteral; |
| 273 | break; |
| 274 | |
| 275 | case Stmt::ImaginaryLiteralClass: |
| 276 | K = CXCursor_ImaginaryLiteral; |
| 277 | break; |
| 278 | |
| 279 | case Stmt::StringLiteralClass: |
| 280 | K = CXCursor_StringLiteral; |
| 281 | break; |
| 282 | |
| 283 | case Stmt::CharacterLiteralClass: |
| 284 | K = CXCursor_CharacterLiteral; |
| 285 | break; |
| 286 | |
| 287 | case Stmt::ParenExprClass: |
| 288 | K = CXCursor_ParenExpr; |
| 289 | break; |
| 290 | |
| 291 | case Stmt::UnaryOperatorClass: |
| 292 | K = CXCursor_UnaryOperator; |
| 293 | break; |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 294 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 295 | case Stmt::CXXNoexceptExprClass: |
| 296 | K = CXCursor_UnaryExpr; |
| 297 | break; |
| 298 | |
| 299 | case Stmt::ArraySubscriptExprClass: |
| 300 | K = CXCursor_ArraySubscriptExpr; |
| 301 | break; |
| 302 | |
| 303 | case Stmt::BinaryOperatorClass: |
| 304 | K = CXCursor_BinaryOperator; |
| 305 | break; |
| 306 | |
| 307 | case Stmt::CompoundAssignOperatorClass: |
| 308 | K = CXCursor_CompoundAssignOperator; |
| 309 | break; |
| 310 | |
| 311 | case Stmt::ConditionalOperatorClass: |
| 312 | K = CXCursor_ConditionalOperator; |
| 313 | break; |
| 314 | |
| 315 | case Stmt::CStyleCastExprClass: |
| 316 | K = CXCursor_CStyleCastExpr; |
| 317 | break; |
| 318 | |
| 319 | case Stmt::CompoundLiteralExprClass: |
| 320 | K = CXCursor_CompoundLiteralExpr; |
| 321 | break; |
| 322 | |
| 323 | case Stmt::InitListExprClass: |
| 324 | K = CXCursor_InitListExpr; |
| 325 | break; |
| 326 | |
| 327 | case Stmt::AddrLabelExprClass: |
| 328 | K = CXCursor_AddrLabelExpr; |
| 329 | break; |
| 330 | |
| 331 | case Stmt::StmtExprClass: |
| 332 | K = CXCursor_StmtExpr; |
| 333 | break; |
| 334 | |
| 335 | case Stmt::GenericSelectionExprClass: |
| 336 | K = CXCursor_GenericSelectionExpr; |
| 337 | break; |
| 338 | |
| 339 | case Stmt::GNUNullExprClass: |
| 340 | K = CXCursor_GNUNullExpr; |
| 341 | break; |
| 342 | |
| 343 | case Stmt::CXXStaticCastExprClass: |
| 344 | K = CXCursor_CXXStaticCastExpr; |
| 345 | break; |
| 346 | |
| 347 | case Stmt::CXXDynamicCastExprClass: |
| 348 | K = CXCursor_CXXDynamicCastExpr; |
| 349 | break; |
| 350 | |
| 351 | case Stmt::CXXReinterpretCastExprClass: |
| 352 | K = CXCursor_CXXReinterpretCastExpr; |
| 353 | break; |
| 354 | |
| 355 | case Stmt::CXXConstCastExprClass: |
| 356 | K = CXCursor_CXXConstCastExpr; |
| 357 | break; |
| 358 | |
| 359 | case Stmt::CXXFunctionalCastExprClass: |
| 360 | K = CXCursor_CXXFunctionalCastExpr; |
| 361 | break; |
| 362 | |
| 363 | case Stmt::CXXTypeidExprClass: |
| 364 | K = CXCursor_CXXTypeidExpr; |
| 365 | break; |
| 366 | |
| 367 | case Stmt::CXXBoolLiteralExprClass: |
| 368 | K = CXCursor_CXXBoolLiteralExpr; |
| 369 | break; |
| 370 | |
| 371 | case Stmt::CXXNullPtrLiteralExprClass: |
| 372 | K = CXCursor_CXXNullPtrLiteralExpr; |
| 373 | break; |
| 374 | |
| 375 | case Stmt::CXXThisExprClass: |
| 376 | K = CXCursor_CXXThisExpr; |
| 377 | break; |
| 378 | |
| 379 | case Stmt::CXXThrowExprClass: |
| 380 | K = CXCursor_CXXThrowExpr; |
| 381 | break; |
| 382 | |
| 383 | case Stmt::CXXNewExprClass: |
| 384 | K = CXCursor_CXXNewExpr; |
| 385 | break; |
| 386 | |
| 387 | case Stmt::CXXDeleteExprClass: |
| 388 | K = CXCursor_CXXDeleteExpr; |
| 389 | break; |
| 390 | |
| 391 | case Stmt::ObjCStringLiteralClass: |
| 392 | K = CXCursor_ObjCStringLiteral; |
| 393 | break; |
| 394 | |
| 395 | case Stmt::ObjCEncodeExprClass: |
| 396 | K = CXCursor_ObjCEncodeExpr; |
| 397 | break; |
| 398 | |
| 399 | case Stmt::ObjCSelectorExprClass: |
| 400 | K = CXCursor_ObjCSelectorExpr; |
| 401 | break; |
| 402 | |
| 403 | case Stmt::ObjCProtocolExprClass: |
| 404 | K = CXCursor_ObjCProtocolExpr; |
| 405 | break; |
Ted Kremenek | b3f7542 | 2012-03-06 20:06:06 +0000 | [diff] [blame] | 406 | |
| 407 | case Stmt::ObjCBoolLiteralExprClass: |
| 408 | K = CXCursor_ObjCBoolLiteralExpr; |
| 409 | break; |
| 410 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 411 | case Stmt::ObjCBridgedCastExprClass: |
| 412 | K = CXCursor_ObjCBridgedCastExpr; |
| 413 | break; |
| 414 | |
| 415 | case Stmt::BlockExprClass: |
| 416 | K = CXCursor_BlockExpr; |
| 417 | break; |
| 418 | |
| 419 | case Stmt::PackExpansionExprClass: |
| 420 | K = CXCursor_PackExpansionExpr; |
| 421 | break; |
| 422 | |
| 423 | case Stmt::SizeOfPackExprClass: |
| 424 | K = CXCursor_SizeOfPackExpr; |
| 425 | break; |
| 426 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 427 | case Stmt::DeclRefExprClass: |
| 428 | case Stmt::DependentScopeDeclRefExprClass: |
John McCall | 91a5755 | 2011-07-15 05:09:51 +0000 | [diff] [blame] | 429 | case Stmt::SubstNonTypeTemplateParmExprClass: |
Douglas Gregor | c7793c7 | 2011-01-15 01:15:58 +0000 | [diff] [blame] | 430 | case Stmt::SubstNonTypeTemplateParmPackExprClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 431 | case Stmt::UnresolvedLookupExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 432 | K = CXCursor_DeclRefExpr; |
| 433 | break; |
| 434 | |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 435 | case Stmt::CXXDependentScopeMemberExprClass: |
| 436 | case Stmt::CXXPseudoDestructorExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 437 | case Stmt::MemberExprClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 438 | case Stmt::ObjCIsaExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 439 | case Stmt::ObjCIvarRefExprClass: |
| 440 | case Stmt::ObjCPropertyRefExprClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 441 | case Stmt::UnresolvedMemberExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 442 | K = CXCursor_MemberRefExpr; |
| 443 | break; |
| 444 | |
| 445 | case Stmt::CallExprClass: |
| 446 | case Stmt::CXXOperatorCallExprClass: |
| 447 | case Stmt::CXXMemberCallExprClass: |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 448 | case Stmt::CUDAKernelCallExprClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 449 | case Stmt::CXXConstructExprClass: |
| 450 | case Stmt::CXXTemporaryObjectExprClass: |
Douglas Gregor | 42b2984 | 2011-10-05 19:00:14 +0000 | [diff] [blame] | 451 | case Stmt::CXXUnresolvedConstructExprClass: |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 452 | case Stmt::UserDefinedLiteralClass: |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 453 | K = CXCursor_CallExpr; |
| 454 | break; |
| 455 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 456 | case Stmt::LambdaExprClass: |
| 457 | K = CXCursor_LambdaExpr; |
| 458 | break; |
| 459 | |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 460 | case Stmt::ObjCMessageExprClass: { |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 461 | K = CXCursor_ObjCMessageExpr; |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 462 | int SelectorIdIndex = -1; |
| 463 | // Check if cursor points to a selector id. |
| 464 | if (RegionOfInterest.isValid() && |
| 465 | RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) { |
| 466 | SmallVector<SourceLocation, 16> SelLocs; |
| 467 | cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs); |
| 468 | SmallVector<SourceLocation, 16>::iterator |
| 469 | I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin()); |
| 470 | if (I != SelLocs.end()) |
| 471 | SelectorIdIndex = I - SelLocs.begin(); |
| 472 | } |
| 473 | CXCursor C = { K, 0, { Parent, S, TU } }; |
| 474 | return getSelectorIdentifierCursor(SelectorIdIndex, C); |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 475 | } |
Douglas Gregor | ba0513d | 2011-10-25 01:33:02 +0000 | [diff] [blame] | 476 | |
| 477 | case Stmt::MSDependentExistsStmtClass: |
| 478 | K = CXCursor_UnexposedStmt; |
| 479 | break; |
| 480 | } |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 481 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 482 | CXCursor C = { K, 0, { Parent, S, TU } }; |
Douglas Gregor | 97b9872 | 2010-01-19 23:20:36 +0000 | [diff] [blame] | 483 | return C; |
| 484 | } |
| 485 | |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 486 | CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super, |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 487 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 488 | CXTranslationUnit TU) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 489 | assert(Super && TU && "Invalid arguments!"); |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 490 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 491 | CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } }; |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 492 | return C; |
| 493 | } |
| 494 | |
| 495 | std::pair<ObjCInterfaceDecl *, SourceLocation> |
| 496 | cxcursor::getCursorObjCSuperClassRef(CXCursor C) { |
| 497 | assert(C.kind == CXCursor_ObjCSuperClassRef); |
| 498 | return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), |
| 499 | SourceLocation::getFromRawEncoding( |
| 500 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 501 | } |
| 502 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 503 | CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto, |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 504 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 505 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 506 | assert(Proto && TU && "Invalid arguments!"); |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 507 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 508 | CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } }; |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 509 | return C; |
| 510 | } |
| 511 | |
| 512 | std::pair<ObjCProtocolDecl *, SourceLocation> |
| 513 | cxcursor::getCursorObjCProtocolRef(CXCursor C) { |
| 514 | assert(C.kind == CXCursor_ObjCProtocolRef); |
| 515 | return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]), |
| 516 | SourceLocation::getFromRawEncoding( |
| 517 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 518 | } |
| 519 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 520 | CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class, |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 521 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 522 | CXTranslationUnit TU) { |
Ted Kremenek | ebfa339 | 2010-03-19 20:39:03 +0000 | [diff] [blame] | 523 | // 'Class' can be null for invalid code. |
| 524 | if (!Class) |
| 525 | return MakeCXCursorInvalid(CXCursor_InvalidCode); |
| 526 | assert(TU && "Invalid arguments!"); |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 527 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 528 | CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } }; |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 529 | return C; |
| 530 | } |
| 531 | |
| 532 | std::pair<ObjCInterfaceDecl *, SourceLocation> |
| 533 | cxcursor::getCursorObjCClassRef(CXCursor C) { |
| 534 | assert(C.kind == CXCursor_ObjCClassRef); |
| 535 | return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]), |
| 536 | SourceLocation::getFromRawEncoding( |
| 537 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 538 | } |
| 539 | |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 540 | CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 541 | CXTranslationUnit TU) { |
Daniel Dunbar | 54d67ca | 2010-01-25 00:40:30 +0000 | [diff] [blame] | 542 | assert(Type && TU && "Invalid arguments!"); |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 543 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | 4e7064f | 2011-10-17 19:48:19 +0000 | [diff] [blame] | 544 | CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } }; |
Douglas Gregor | 7d0d40e | 2010-01-21 16:28:34 +0000 | [diff] [blame] | 545 | return C; |
| 546 | } |
| 547 | |
| 548 | std::pair<TypeDecl *, SourceLocation> |
| 549 | cxcursor::getCursorTypeRef(CXCursor C) { |
| 550 | assert(C.kind == CXCursor_TypeRef); |
| 551 | return std::make_pair(static_cast<TypeDecl *>(C.data[0]), |
| 552 | SourceLocation::getFromRawEncoding( |
| 553 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 554 | } |
| 555 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 556 | CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 557 | SourceLocation Loc, |
| 558 | CXTranslationUnit TU) { |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 559 | assert(Template && TU && "Invalid arguments!"); |
| 560 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 561 | CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } }; |
Douglas Gregor | 0b36e61 | 2010-08-31 20:37:03 +0000 | [diff] [blame] | 562 | return C; |
| 563 | } |
| 564 | |
| 565 | std::pair<TemplateDecl *, SourceLocation> |
| 566 | cxcursor::getCursorTemplateRef(CXCursor C) { |
| 567 | assert(C.kind == CXCursor_TemplateRef); |
| 568 | return std::make_pair(static_cast<TemplateDecl *>(C.data[0]), |
| 569 | SourceLocation::getFromRawEncoding( |
| 570 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 571 | } |
| 572 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 573 | CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS, |
| 574 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 575 | CXTranslationUnit TU) { |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 576 | |
| 577 | assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU && |
| 578 | "Invalid arguments!"); |
| 579 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 580 | CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } }; |
Douglas Gregor | 6931900 | 2010-08-31 23:48:11 +0000 | [diff] [blame] | 581 | return C; |
| 582 | } |
| 583 | |
| 584 | std::pair<NamedDecl *, SourceLocation> |
| 585 | cxcursor::getCursorNamespaceRef(CXCursor C) { |
| 586 | assert(C.kind == CXCursor_NamespaceRef); |
| 587 | return std::make_pair(static_cast<NamedDecl *>(C.data[0]), |
| 588 | SourceLocation::getFromRawEncoding( |
| 589 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 590 | } |
| 591 | |
Douglas Gregor | 011d8b9 | 2012-02-15 00:54:55 +0000 | [diff] [blame] | 592 | CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc, |
| 593 | CXTranslationUnit TU) { |
| 594 | |
| 595 | assert(Var && TU && "Invalid arguments!"); |
| 596 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 597 | CXCursor C = { CXCursor_VariableRef, 0, { (void*)Var, RawLoc, TU } }; |
| 598 | return C; |
| 599 | } |
| 600 | |
| 601 | std::pair<VarDecl *, SourceLocation> |
| 602 | cxcursor::getCursorVariableRef(CXCursor C) { |
| 603 | assert(C.kind == CXCursor_VariableRef); |
| 604 | return std::make_pair(static_cast<VarDecl *>(C.data[0]), |
| 605 | SourceLocation::getFromRawEncoding( |
| 606 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 607 | } |
| 608 | |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 609 | CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 610 | CXTranslationUnit TU) { |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 611 | |
| 612 | assert(Field && TU && "Invalid arguments!"); |
| 613 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | b395c63 | 2011-11-18 00:26:51 +0000 | [diff] [blame] | 614 | CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } }; |
Douglas Gregor | a67e03f | 2010-09-09 21:42:20 +0000 | [diff] [blame] | 615 | return C; |
| 616 | } |
| 617 | |
| 618 | std::pair<FieldDecl *, SourceLocation> |
| 619 | cxcursor::getCursorMemberRef(CXCursor C) { |
| 620 | assert(C.kind == CXCursor_MemberRef); |
| 621 | return std::make_pair(static_cast<FieldDecl *>(C.data[0]), |
| 622 | SourceLocation::getFromRawEncoding( |
| 623 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 624 | } |
| 625 | |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 626 | CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 627 | CXTranslationUnit TU){ |
Argyrios Kyrtzidis | 2957e6f | 2011-11-22 07:24:51 +0000 | [diff] [blame] | 628 | CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } }; |
Ted Kremenek | 3064ef9 | 2010-08-27 21:34:58 +0000 | [diff] [blame] | 629 | return C; |
| 630 | } |
| 631 | |
| 632 | CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) { |
| 633 | assert(C.kind == CXCursor_CXXBaseSpecifier); |
| 634 | return static_cast<CXXBaseSpecifier*>(C.data[0]); |
| 635 | } |
| 636 | |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 637 | CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 638 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 639 | CXCursor C = { CXCursor_PreprocessingDirective, 0, |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 640 | { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()), |
| 641 | reinterpret_cast<void *>(Range.getEnd().getRawEncoding()), |
| 642 | TU } |
| 643 | }; |
| 644 | return C; |
| 645 | } |
| 646 | |
| 647 | SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) { |
| 648 | assert(C.kind == CXCursor_PreprocessingDirective); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 649 | SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding( |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 650 | reinterpret_cast<uintptr_t> (C.data[0])), |
| 651 | SourceLocation::getFromRawEncoding( |
| 652 | reinterpret_cast<uintptr_t> (C.data[1]))); |
Argyrios Kyrtzidis | ee0f84f | 2011-09-26 08:01:41 +0000 | [diff] [blame] | 653 | ASTUnit *TU = getCursorASTUnit(C); |
| 654 | return TU->mapRangeFromPreamble(Range); |
Douglas Gregor | 9f1e3ff | 2010-03-18 00:42:48 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 657 | CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI, |
| 658 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 659 | CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } }; |
Douglas Gregor | 572feb2 | 2010-03-18 18:04:21 +0000 | [diff] [blame] | 660 | return C; |
| 661 | } |
| 662 | |
| 663 | MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) { |
| 664 | assert(C.kind == CXCursor_MacroDefinition); |
| 665 | return static_cast<MacroDefinition *>(C.data[0]); |
| 666 | } |
| 667 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 668 | CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI, |
| 669 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 670 | CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } }; |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 671 | return C; |
| 672 | } |
| 673 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 674 | MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) { |
Chandler Carruth | 9b2a0ac | 2011-07-14 08:41:15 +0000 | [diff] [blame] | 675 | assert(C.kind == CXCursor_MacroExpansion); |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 676 | return static_cast<MacroExpansion *>(C.data[0]); |
Douglas Gregor | 4807231 | 2010-03-18 15:23:44 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 679 | CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 680 | CXTranslationUnit TU) { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 681 | CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } }; |
Douglas Gregor | ecdcb88 | 2010-10-20 22:00:55 +0000 | [diff] [blame] | 682 | return C; |
| 683 | } |
| 684 | |
| 685 | InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) { |
| 686 | assert(C.kind == CXCursor_InclusionDirective); |
| 687 | return static_cast<InclusionDirective *>(C.data[0]); |
| 688 | } |
| 689 | |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 690 | CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 691 | CXTranslationUnit TU) { |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 692 | |
| 693 | assert(Label && TU && "Invalid arguments!"); |
| 694 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 695 | CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } }; |
Douglas Gregor | 36897b0 | 2010-09-10 00:22:18 +0000 | [diff] [blame] | 696 | return C; |
| 697 | } |
| 698 | |
| 699 | std::pair<LabelStmt*, SourceLocation> |
| 700 | cxcursor::getCursorLabelRef(CXCursor C) { |
| 701 | assert(C.kind == CXCursor_LabelRef); |
| 702 | return std::make_pair(static_cast<LabelStmt *>(C.data[0]), |
| 703 | SourceLocation::getFromRawEncoding( |
| 704 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 705 | } |
| 706 | |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 707 | CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 708 | CXTranslationUnit TU) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 709 | assert(E && TU && "Invalid arguments!"); |
| 710 | OverloadedDeclRefStorage Storage(E); |
| 711 | void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding()); |
| 712 | CXCursor C = { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 713 | CXCursor_OverloadedDeclRef, 0, |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 714 | { Storage.getOpaqueValue(), RawLoc, TU } |
| 715 | }; |
| 716 | return C; |
| 717 | } |
| 718 | |
| 719 | CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D, |
| 720 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 721 | CXTranslationUnit TU) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 722 | assert(D && TU && "Invalid arguments!"); |
| 723 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 724 | OverloadedDeclRefStorage Storage(D); |
| 725 | CXCursor C = { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 726 | CXCursor_OverloadedDeclRef, 0, |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 727 | { Storage.getOpaqueValue(), RawLoc, TU } |
| 728 | }; |
| 729 | return C; |
| 730 | } |
| 731 | |
| 732 | CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name, |
| 733 | SourceLocation Loc, |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 734 | CXTranslationUnit TU) { |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 735 | assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!"); |
| 736 | void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding()); |
| 737 | OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate()); |
| 738 | CXCursor C = { |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 739 | CXCursor_OverloadedDeclRef, 0, |
Douglas Gregor | 1f60d9e | 2010-09-13 22:52:57 +0000 | [diff] [blame] | 740 | { Storage.getOpaqueValue(), RawLoc, TU } |
| 741 | }; |
| 742 | return C; |
| 743 | } |
| 744 | |
| 745 | std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation> |
| 746 | cxcursor::getCursorOverloadedDeclRef(CXCursor C) { |
| 747 | assert(C.kind == CXCursor_OverloadedDeclRef); |
| 748 | return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]), |
| 749 | SourceLocation::getFromRawEncoding( |
| 750 | reinterpret_cast<uintptr_t>(C.data[1]))); |
| 751 | } |
| 752 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 753 | Decl *cxcursor::getCursorDecl(CXCursor Cursor) { |
| 754 | return (Decl *)Cursor.data[0]; |
| 755 | } |
| 756 | |
| 757 | Expr *cxcursor::getCursorExpr(CXCursor Cursor) { |
| 758 | return dyn_cast_or_null<Expr>(getCursorStmt(Cursor)); |
| 759 | } |
| 760 | |
| 761 | Stmt *cxcursor::getCursorStmt(CXCursor Cursor) { |
Douglas Gregor | 78db0cd | 2010-01-16 15:44:18 +0000 | [diff] [blame] | 762 | if (Cursor.kind == CXCursor_ObjCSuperClassRef || |
Douglas Gregor | 1adb082 | 2010-01-16 17:14:40 +0000 | [diff] [blame] | 763 | Cursor.kind == CXCursor_ObjCProtocolRef || |
| 764 | Cursor.kind == CXCursor_ObjCClassRef) |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 765 | return 0; |
| 766 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 767 | return (Stmt *)Cursor.data[1]; |
| 768 | } |
| 769 | |
Ted Kremenek | 95f3355 | 2010-08-26 01:42:22 +0000 | [diff] [blame] | 770 | Attr *cxcursor::getCursorAttr(CXCursor Cursor) { |
| 771 | return (Attr *)Cursor.data[1]; |
| 772 | } |
| 773 | |
Argyrios Kyrtzidis | 8ccac3d | 2011-06-29 22:20:07 +0000 | [diff] [blame] | 774 | Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) { |
| 775 | return (Decl *)Cursor.data[0]; |
| 776 | } |
| 777 | |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 778 | ASTContext &cxcursor::getCursorContext(CXCursor Cursor) { |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 779 | return getCursorASTUnit(Cursor)->getASTContext(); |
| 780 | } |
Douglas Gregor | f46034a | 2010-01-18 23:41:10 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | b2cd487 | 2010-01-20 23:57:43 +0000 | [diff] [blame] | 782 | ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) { |
Argyrios Kyrtzidis | 4451746 | 2011-12-09 00:17:49 +0000 | [diff] [blame] | 783 | CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]); |
| 784 | if (!TU) |
| 785 | return 0; |
| 786 | return static_cast<ASTUnit *>(TU->TUData); |
Ted Kremenek | a60ed47 | 2010-11-16 08:15:36 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) { |
| 790 | return static_cast<CXTranslationUnit>(Cursor.data[2]); |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 791 | } |
| 792 | |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 793 | static void CollectOverriddenMethodsRecurse(CXTranslationUnit TU, |
| 794 | ObjCContainerDecl *Container, |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 795 | ObjCMethodDecl *Method, |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 796 | SmallVectorImpl<CXCursor> &Methods, |
| 797 | bool MovedToSuper) { |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 798 | if (!Container) |
| 799 | return; |
| 800 | |
Argyrios Kyrtzidis | 044e645 | 2012-03-08 00:20:03 +0000 | [diff] [blame] | 801 | // In categories look for overriden methods from protocols. A method from |
| 802 | // category is not "overriden" since it is considered as the "same" method |
| 803 | // (same USR) as the one from the interface. |
| 804 | if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) { |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 805 | // Check whether we have a matching method at this category but only if we |
| 806 | // are at the super class level. |
| 807 | if (MovedToSuper) |
| 808 | if (ObjCMethodDecl * |
| 809 | Overridden = Container->getMethod(Method->getSelector(), |
| 810 | Method->isInstanceMethod())) |
| 811 | if (Method != Overridden) { |
| 812 | // We found an override at this category; there is no need to look |
| 813 | // into its protocols. |
| 814 | Methods.push_back(MakeCXCursor(Overridden, TU)); |
| 815 | return; |
| 816 | } |
| 817 | |
Argyrios Kyrtzidis | 044e645 | 2012-03-08 00:20:03 +0000 | [diff] [blame] | 818 | for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(), |
| 819 | PEnd = Category->protocol_end(); |
| 820 | P != PEnd; ++P) |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 821 | CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper); |
Argyrios Kyrtzidis | 044e645 | 2012-03-08 00:20:03 +0000 | [diff] [blame] | 822 | return; |
| 823 | } |
| 824 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 825 | // Check whether we have a matching method at this level. |
| 826 | if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(), |
| 827 | Method->isInstanceMethod())) |
| 828 | if (Method != Overridden) { |
| 829 | // We found an override at this level; there is no need to look |
| 830 | // into other protocols or categories. |
| 831 | Methods.push_back(MakeCXCursor(Overridden, TU)); |
| 832 | return; |
| 833 | } |
| 834 | |
| 835 | if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) { |
| 836 | for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(), |
| 837 | PEnd = Protocol->protocol_end(); |
| 838 | P != PEnd; ++P) |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 839 | CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper); |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 842 | if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) { |
| 843 | for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(), |
| 844 | PEnd = Interface->protocol_end(); |
| 845 | P != PEnd; ++P) |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 846 | CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper); |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 847 | |
| 848 | for (ObjCCategoryDecl *Category = Interface->getCategoryList(); |
| 849 | Category; Category = Category->getNextClassCategory()) |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 850 | CollectOverriddenMethodsRecurse(TU, Category, Method, Methods, |
| 851 | MovedToSuper); |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 852 | |
Argyrios Kyrtzidis | 044e645 | 2012-03-08 00:20:03 +0000 | [diff] [blame] | 853 | if (ObjCInterfaceDecl *Super = Interface->getSuperClass()) |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 854 | return CollectOverriddenMethodsRecurse(TU, Super, Method, Methods, |
| 855 | /*MovedToSuper=*/true); |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 856 | } |
| 857 | } |
| 858 | |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 859 | static inline void CollectOverriddenMethods(CXTranslationUnit TU, |
| 860 | ObjCContainerDecl *Container, |
| 861 | ObjCMethodDecl *Method, |
| 862 | SmallVectorImpl<CXCursor> &Methods) { |
| 863 | CollectOverriddenMethodsRecurse(TU, Container, Method, Methods, |
| 864 | /*MovedToSuper=*/false); |
| 865 | } |
| 866 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 867 | static void collectOverriddenMethodsSlow(CXTranslationUnit TU, |
| 868 | ObjCMethodDecl *Method, |
| 869 | SmallVectorImpl<CXCursor> &overridden) { |
| 870 | assert(Method->isOverriding()); |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 871 | |
Argyrios Kyrtzidis | 044e645 | 2012-03-08 00:20:03 +0000 | [diff] [blame] | 872 | if (ObjCProtocolDecl * |
| 873 | ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) { |
| 874 | CollectOverriddenMethods(TU, ProtD, Method, overridden); |
| 875 | |
| 876 | } else if (ObjCImplDecl * |
| 877 | IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) { |
| 878 | ObjCInterfaceDecl *ID = IMD->getClassInterface(); |
| 879 | if (!ID) |
| 880 | return; |
| 881 | // Start searching for overridden methods using the method from the |
| 882 | // interface as starting point. |
| 883 | if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), |
| 884 | Method->isInstanceMethod())) |
| 885 | Method = IFaceMeth; |
| 886 | CollectOverriddenMethods(TU, ID, Method, overridden); |
| 887 | |
| 888 | } else if (ObjCCategoryDecl * |
| 889 | CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) { |
| 890 | ObjCInterfaceDecl *ID = CatD->getClassInterface(); |
| 891 | if (!ID) |
| 892 | return; |
| 893 | // Start searching for overridden methods using the method from the |
| 894 | // interface as starting point. |
| 895 | if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(), |
| 896 | Method->isInstanceMethod())) |
| 897 | Method = IFaceMeth; |
| 898 | CollectOverriddenMethods(TU, ID, Method, overridden); |
| 899 | |
| 900 | } else { |
Argyrios Kyrtzidis | 15f4c98 | 2012-04-10 21:01:03 +0000 | [diff] [blame] | 901 | CollectOverriddenMethods(TU, |
| 902 | dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()), |
| 903 | Method, overridden); |
Argyrios Kyrtzidis | 044e645 | 2012-03-08 00:20:03 +0000 | [diff] [blame] | 904 | } |
Argyrios Kyrtzidis | b11be04 | 2011-10-06 07:00:46 +0000 | [diff] [blame] | 905 | } |
| 906 | |
Argyrios Kyrtzidis | e15db6f | 2012-05-09 16:12:57 +0000 | [diff] [blame] | 907 | static void collectOnCategoriesAfterLocation(SourceLocation Loc, |
| 908 | ObjCInterfaceDecl *Class, |
| 909 | CXTranslationUnit TU, |
| 910 | ObjCMethodDecl *Method, |
| 911 | SmallVectorImpl<CXCursor> &Methods) { |
| 912 | if (!Class) |
| 913 | return; |
| 914 | |
| 915 | SourceManager &SM = static_cast<ASTUnit *>(TU->TUData)->getSourceManager(); |
| 916 | for (ObjCCategoryDecl *Category = Class->getCategoryList(); |
| 917 | Category; Category = Category->getNextClassCategory()) |
| 918 | if (SM.isBeforeInTranslationUnit(Loc, Category->getLocation())) |
| 919 | CollectOverriddenMethodsRecurse(TU, Category, Method, Methods, true); |
| 920 | |
| 921 | collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), TU, |
| 922 | Method, Methods); |
| 923 | } |
| 924 | |
| 925 | /// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding() |
| 926 | /// returns false. |
| 927 | /// You'd think that in that case there are no overrides but categories can |
| 928 | /// "introduce" new overridden methods that are missed by Sema because the |
| 929 | /// overrides lookup that it does for methods, inside implementations, will |
| 930 | /// stop at the interface level (if there is a method there) and not look |
| 931 | /// further in super classes. |
| 932 | static void collectOverriddenMethodsFast(CXTranslationUnit TU, |
| 933 | ObjCMethodDecl *Method, |
| 934 | SmallVectorImpl<CXCursor> &Methods) { |
| 935 | assert(!Method->isOverriding()); |
| 936 | |
| 937 | ObjCContainerDecl *ContD = cast<ObjCContainerDecl>(Method->getDeclContext()); |
| 938 | if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD)) |
| 939 | return; |
| 940 | ObjCInterfaceDecl *Class = Method->getClassInterface(); |
| 941 | if (!Class) |
| 942 | return; |
| 943 | |
| 944 | collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(), |
| 945 | TU, Method, Methods); |
| 946 | } |
| 947 | |
| 948 | void cxcursor::getOverriddenCursors(CXCursor cursor, |
| 949 | SmallVectorImpl<CXCursor> &overridden) { |
| 950 | assert(clang_isDeclaration(cursor.kind)); |
| 951 | Decl *D = getCursorDecl(cursor); |
| 952 | if (!D) |
| 953 | return; |
| 954 | |
| 955 | // Handle C++ member functions. |
| 956 | CXTranslationUnit TU = getCursorTU(cursor); |
| 957 | if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) { |
| 958 | for (CXXMethodDecl::method_iterator |
| 959 | M = CXXMethod->begin_overridden_methods(), |
| 960 | MEnd = CXXMethod->end_overridden_methods(); |
| 961 | M != MEnd; ++M) |
| 962 | overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU)); |
| 963 | return; |
| 964 | } |
| 965 | |
| 966 | ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D); |
| 967 | if (!Method) |
| 968 | return; |
| 969 | |
| 970 | if (Method->isRedeclaration()) { |
| 971 | Method = cast<ObjCContainerDecl>(Method->getDeclContext())-> |
| 972 | getMethod(Method->getSelector(), Method->isInstanceMethod()); |
| 973 | } |
| 974 | |
| 975 | if (!Method->isOverriding()) { |
| 976 | collectOverriddenMethodsFast(TU, Method, overridden); |
| 977 | } else { |
| 978 | collectOverriddenMethodsSlow(TU, Method, overridden); |
| 979 | assert(!overridden.empty() && |
| 980 | "ObjCMethodDecl's overriding bit is not as expected"); |
| 981 | } |
| 982 | } |
| 983 | |
Argyrios Kyrtzidis | aed123e | 2011-10-06 07:00:54 +0000 | [diff] [blame] | 984 | std::pair<int, SourceLocation> |
| 985 | cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) { |
| 986 | if (cursor.kind == CXCursor_ObjCMessageExpr) { |
| 987 | if (cursor.xdata != -1) |
| 988 | return std::make_pair(cursor.xdata, |
| 989 | cast<ObjCMessageExpr>(getCursorExpr(cursor)) |
| 990 | ->getSelectorLoc(cursor.xdata)); |
| 991 | } else if (cursor.kind == CXCursor_ObjCClassMethodDecl || |
| 992 | cursor.kind == CXCursor_ObjCInstanceMethodDecl) { |
| 993 | if (cursor.xdata != -1) |
| 994 | return std::make_pair(cursor.xdata, |
| 995 | cast<ObjCMethodDecl>(getCursorDecl(cursor)) |
| 996 | ->getSelectorLoc(cursor.xdata)); |
| 997 | } |
| 998 | |
| 999 | return std::make_pair(-1, SourceLocation()); |
| 1000 | } |
| 1001 | |
| 1002 | CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) { |
| 1003 | CXCursor newCursor = cursor; |
| 1004 | |
| 1005 | if (cursor.kind == CXCursor_ObjCMessageExpr) { |
| 1006 | if (SelIdx == -1 || |
| 1007 | unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor)) |
| 1008 | ->getNumSelectorLocs()) |
| 1009 | newCursor.xdata = -1; |
| 1010 | else |
| 1011 | newCursor.xdata = SelIdx; |
| 1012 | } else if (cursor.kind == CXCursor_ObjCClassMethodDecl || |
| 1013 | cursor.kind == CXCursor_ObjCInstanceMethodDecl) { |
| 1014 | if (SelIdx == -1 || |
| 1015 | unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor)) |
| 1016 | ->getNumSelectorLocs()) |
| 1017 | newCursor.xdata = -1; |
| 1018 | else |
| 1019 | newCursor.xdata = SelIdx; |
| 1020 | } |
| 1021 | |
| 1022 | return newCursor; |
| 1023 | } |
| 1024 | |
| 1025 | CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) { |
| 1026 | if (cursor.kind != CXCursor_CallExpr) |
| 1027 | return cursor; |
| 1028 | |
| 1029 | if (cursor.xdata == 0) |
| 1030 | return cursor; |
| 1031 | |
| 1032 | Expr *E = getCursorExpr(cursor); |
| 1033 | TypeSourceInfo *Type = 0; |
| 1034 | if (CXXUnresolvedConstructExpr * |
| 1035 | UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) { |
| 1036 | Type = UnCtor->getTypeSourceInfo(); |
| 1037 | } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){ |
| 1038 | Type = Tmp->getTypeSourceInfo(); |
| 1039 | } |
| 1040 | |
| 1041 | if (!Type) |
| 1042 | return cursor; |
| 1043 | |
| 1044 | CXTranslationUnit TU = getCursorTU(cursor); |
| 1045 | QualType Ty = Type->getType(); |
| 1046 | TypeLoc TL = Type->getTypeLoc(); |
| 1047 | SourceLocation Loc = TL.getBeginLoc(); |
| 1048 | |
| 1049 | if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) { |
| 1050 | Ty = ElabT->getNamedType(); |
| 1051 | ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL); |
| 1052 | Loc = ElabTL.getNamedTypeLoc().getBeginLoc(); |
| 1053 | } |
| 1054 | |
| 1055 | if (const TypedefType *Typedef = Ty->getAs<TypedefType>()) |
| 1056 | return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU); |
| 1057 | if (const TagType *Tag = Ty->getAs<TagType>()) |
| 1058 | return MakeCursorTypeRef(Tag->getDecl(), Loc, TU); |
| 1059 | if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>()) |
| 1060 | return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU); |
| 1061 | |
| 1062 | return cursor; |
| 1063 | } |
| 1064 | |
Douglas Gregor | 283cae3 | 2010-01-15 21:56:13 +0000 | [diff] [blame] | 1065 | bool cxcursor::operator==(CXCursor X, CXCursor Y) { |
| 1066 | return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] && |
| 1067 | X.data[2] == Y.data[2]; |
Douglas Gregor | 2e331b9 | 2010-01-16 14:00:32 +0000 | [diff] [blame] | 1068 | } |
Ted Kremenek | 007a7c9 | 2010-11-01 23:26:51 +0000 | [diff] [blame] | 1069 | |
| 1070 | // FIXME: Remove once we can model DeclGroups and their appropriate ranges |
| 1071 | // properly in the ASTs. |
| 1072 | bool cxcursor::isFirstInDeclGroup(CXCursor C) { |
| 1073 | assert(clang_isDeclaration(C.kind)); |
| 1074 | return ((uintptr_t) (C.data[1])) != 0; |
| 1075 | } |
| 1076 | |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 1077 | //===----------------------------------------------------------------------===// |
Argyrios Kyrtzidis | b0d6eaa | 2011-09-27 00:30:30 +0000 | [diff] [blame] | 1078 | // libclang CXCursor APIs |
| 1079 | //===----------------------------------------------------------------------===// |
| 1080 | |
Argyrios Kyrtzidis | fa865df | 2011-09-27 04:14:36 +0000 | [diff] [blame] | 1081 | extern "C" { |
| 1082 | |
| 1083 | int clang_Cursor_isNull(CXCursor cursor) { |
| 1084 | return clang_equalCursors(cursor, clang_getNullCursor()); |
| 1085 | } |
| 1086 | |
Argyrios Kyrtzidis | b0d6eaa | 2011-09-27 00:30:30 +0000 | [diff] [blame] | 1087 | CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) { |
| 1088 | return getCursorTU(cursor); |
| 1089 | } |
| 1090 | |
Argyrios Kyrtzidis | d98ef9a | 2012-04-11 19:32:19 +0000 | [diff] [blame] | 1091 | int clang_Cursor_getNumArguments(CXCursor C) { |
| 1092 | if (clang_isDeclaration(C.kind)) { |
| 1093 | Decl *D = cxcursor::getCursorDecl(C); |
| 1094 | if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) |
| 1095 | return MD->param_size(); |
| 1096 | if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) |
| 1097 | return FD->param_size(); |
| 1098 | } |
| 1099 | |
| 1100 | return -1; |
| 1101 | } |
| 1102 | |
| 1103 | CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) { |
| 1104 | if (clang_isDeclaration(C.kind)) { |
| 1105 | Decl *D = cxcursor::getCursorDecl(C); |
| 1106 | if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) { |
| 1107 | if (i < MD->param_size()) |
| 1108 | return cxcursor::MakeCXCursor(MD->param_begin()[i], |
| 1109 | cxcursor::getCursorTU(C)); |
| 1110 | } else if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) { |
| 1111 | if (i < FD->param_size()) |
| 1112 | return cxcursor::MakeCXCursor(FD->param_begin()[i], |
| 1113 | cxcursor::getCursorTU(C)); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | return clang_getNullCursor(); |
| 1118 | } |
| 1119 | |
Argyrios Kyrtzidis | fa865df | 2011-09-27 04:14:36 +0000 | [diff] [blame] | 1120 | } // end: extern "C" |
| 1121 | |
Argyrios Kyrtzidis | b0d6eaa | 2011-09-27 00:30:30 +0000 | [diff] [blame] | 1122 | //===----------------------------------------------------------------------===// |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 1123 | // CXCursorSet. |
| 1124 | //===----------------------------------------------------------------------===// |
| 1125 | |
| 1126 | typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl; |
| 1127 | |
| 1128 | static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) { |
| 1129 | return (CXCursorSet) setImpl; |
| 1130 | } |
| 1131 | static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) { |
| 1132 | return (CXCursorSet_Impl*) set; |
| 1133 | } |
| 1134 | namespace llvm { |
Ted Kremenek | da6fb69 | 2010-12-09 00:33:41 +0000 | [diff] [blame] | 1135 | template<> struct DenseMapInfo<CXCursor> { |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 1136 | public: |
| 1137 | static inline CXCursor getEmptyKey() { |
| 1138 | return MakeCXCursorInvalid(CXCursor_InvalidFile); |
| 1139 | } |
| 1140 | static inline CXCursor getTombstoneKey() { |
| 1141 | return MakeCXCursorInvalid(CXCursor_NoDeclFound); |
| 1142 | } |
| 1143 | static inline unsigned getHashValue(const CXCursor &cursor) { |
| 1144 | return llvm::DenseMapInfo<std::pair<void*,void*> > |
| 1145 | ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1])); |
| 1146 | } |
| 1147 | static inline bool isEqual(const CXCursor &x, const CXCursor &y) { |
| 1148 | return x.kind == y.kind && |
| 1149 | x.data[0] == y.data[0] && |
| 1150 | x.data[1] == y.data[1]; |
| 1151 | } |
| 1152 | }; |
| 1153 | } |
| 1154 | |
| 1155 | extern "C" { |
| 1156 | CXCursorSet clang_createCXCursorSet() { |
| 1157 | return packCXCursorSet(new CXCursorSet_Impl()); |
| 1158 | } |
| 1159 | |
| 1160 | void clang_disposeCXCursorSet(CXCursorSet set) { |
| 1161 | delete unpackCXCursorSet(set); |
| 1162 | } |
| 1163 | |
| 1164 | unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) { |
| 1165 | CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); |
| 1166 | if (!setImpl) |
| 1167 | return 0; |
| 1168 | return setImpl->find(cursor) == setImpl->end(); |
| 1169 | } |
| 1170 | |
Anders Carlsson | e8b3de0 | 2010-12-09 01:00:12 +0000 | [diff] [blame] | 1171 | unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) { |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 1172 | // Do not insert invalid cursors into the set. |
| 1173 | if (cursor.kind >= CXCursor_FirstInvalid && |
| 1174 | cursor.kind <= CXCursor_LastInvalid) |
| 1175 | return 1; |
| 1176 | |
| 1177 | CXCursorSet_Impl *setImpl = unpackCXCursorSet(set); |
| 1178 | if (!setImpl) |
| 1179 | return 1; |
| 1180 | unsigned &entry = (*setImpl)[cursor]; |
| 1181 | unsigned flag = entry == 0 ? 1 : 0; |
| 1182 | entry = 1; |
| 1183 | return flag; |
| 1184 | } |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1185 | |
| 1186 | CXCompletionString clang_getCursorCompletionString(CXCursor cursor) { |
| 1187 | enum CXCursorKind kind = clang_getCursorKind(cursor); |
| 1188 | if (clang_isDeclaration(kind)) { |
| 1189 | Decl *decl = getCursorDecl(cursor); |
Argyrios Kyrtzidis | 16ed0e6 | 2011-12-10 02:36:25 +0000 | [diff] [blame] | 1190 | if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) { |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1191 | ASTUnit *unit = getCursorASTUnit(cursor); |
Argyrios Kyrtzidis | 5e192a7 | 2012-01-17 02:15:54 +0000 | [diff] [blame] | 1192 | CodeCompletionResult Result(namedDecl); |
| 1193 | CodeCompletionString *String |
| 1194 | = Result.CreateCodeCompletionString(unit->getASTContext(), |
| 1195 | unit->getPreprocessor(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1196 | unit->getCodeCompletionTUInfo().getAllocator(), |
| 1197 | unit->getCodeCompletionTUInfo()); |
Argyrios Kyrtzidis | 5e192a7 | 2012-01-17 02:15:54 +0000 | [diff] [blame] | 1198 | return String; |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1199 | } |
| 1200 | } |
| 1201 | else if (kind == CXCursor_MacroDefinition) { |
| 1202 | MacroDefinition *definition = getCursorMacroDefinition(cursor); |
| 1203 | const IdentifierInfo *MacroInfo = definition->getName(); |
| 1204 | ASTUnit *unit = getCursorASTUnit(cursor); |
Argyrios Kyrtzidis | 5e192a7 | 2012-01-17 02:15:54 +0000 | [diff] [blame] | 1205 | CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo)); |
| 1206 | CodeCompletionString *String |
| 1207 | = Result.CreateCodeCompletionString(unit->getASTContext(), |
| 1208 | unit->getPreprocessor(), |
Argyrios Kyrtzidis | 28a83f5 | 2012-04-10 17:23:48 +0000 | [diff] [blame] | 1209 | unit->getCodeCompletionTUInfo().getAllocator(), |
| 1210 | unit->getCodeCompletionTUInfo()); |
Argyrios Kyrtzidis | 5e192a7 | 2012-01-17 02:15:54 +0000 | [diff] [blame] | 1211 | return String; |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1212 | } |
| 1213 | return NULL; |
| 1214 | } |
Ted Kremenek | 8eece46 | 2012-04-30 19:33:45 +0000 | [diff] [blame] | 1215 | } // end: extern C. |
Ted Kremenek | bbf66ca | 2012-04-30 19:06:49 +0000 | [diff] [blame] | 1216 | |
| 1217 | namespace { |
| 1218 | struct OverridenCursorsPool { |
| 1219 | typedef llvm::SmallVector<CXCursor, 2> CursorVec; |
| 1220 | std::vector<CursorVec*> AllCursors; |
| 1221 | std::vector<CursorVec*> AvailableCursors; |
| 1222 | |
| 1223 | ~OverridenCursorsPool() { |
| 1224 | for (std::vector<CursorVec*>::iterator I = AllCursors.begin(), |
| 1225 | E = AllCursors.end(); I != E; ++I) { |
| 1226 | delete *I; |
| 1227 | } |
| 1228 | } |
| 1229 | }; |
| 1230 | } |
| 1231 | |
| 1232 | void *cxcursor::createOverridenCXCursorsPool() { |
| 1233 | return new OverridenCursorsPool(); |
| 1234 | } |
| 1235 | |
| 1236 | void cxcursor::disposeOverridenCXCursorsPool(void *pool) { |
| 1237 | delete static_cast<OverridenCursorsPool*>(pool); |
| 1238 | } |
Ted Kremenek | 8eece46 | 2012-04-30 19:33:45 +0000 | [diff] [blame] | 1239 | |
| 1240 | extern "C" { |
Ted Kremenek | bbf66ca | 2012-04-30 19:06:49 +0000 | [diff] [blame] | 1241 | void clang_getOverriddenCursors(CXCursor cursor, |
| 1242 | CXCursor **overridden, |
| 1243 | unsigned *num_overridden) { |
| 1244 | if (overridden) |
| 1245 | *overridden = 0; |
| 1246 | if (num_overridden) |
| 1247 | *num_overridden = 0; |
| 1248 | |
| 1249 | CXTranslationUnit TU = cxcursor::getCursorTU(cursor); |
| 1250 | |
| 1251 | if (!overridden || !num_overridden || !TU) |
| 1252 | return; |
| 1253 | |
| 1254 | if (!clang_isDeclaration(cursor.kind)) |
| 1255 | return; |
| 1256 | |
| 1257 | OverridenCursorsPool &pool = |
| 1258 | *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool); |
| 1259 | |
| 1260 | OverridenCursorsPool::CursorVec *Vec = 0; |
| 1261 | |
| 1262 | if (!pool.AvailableCursors.empty()) { |
| 1263 | Vec = pool.AvailableCursors.back(); |
| 1264 | pool.AvailableCursors.pop_back(); |
| 1265 | } |
| 1266 | else { |
| 1267 | Vec = new OverridenCursorsPool::CursorVec(); |
| 1268 | pool.AllCursors.push_back(Vec); |
| 1269 | } |
| 1270 | |
| 1271 | // Clear out the vector, but don't free the memory contents. This |
| 1272 | // reduces malloc() traffic. |
| 1273 | Vec->clear(); |
| 1274 | |
| 1275 | // Use the first entry to contain a back reference to the vector. |
| 1276 | // This is a complete hack. |
| 1277 | CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU); |
| 1278 | backRefCursor.data[0] = Vec; |
| 1279 | assert(cxcursor::getCursorTU(backRefCursor) == TU); |
| 1280 | Vec->push_back(backRefCursor); |
| 1281 | |
| 1282 | // Get the overriden cursors. |
| 1283 | cxcursor::getOverriddenCursors(cursor, *Vec); |
| 1284 | |
| 1285 | // Did we get any overriden cursors? If not, return Vec to the pool |
| 1286 | // of available cursor vectors. |
| 1287 | if (Vec->size() == 1) { |
| 1288 | pool.AvailableCursors.push_back(Vec); |
| 1289 | return; |
| 1290 | } |
| 1291 | |
| 1292 | // Now tell the caller about the overriden cursors. |
| 1293 | assert(Vec->size() > 1); |
| 1294 | *overridden = &((*Vec)[1]); |
| 1295 | *num_overridden = Vec->size() - 1; |
| 1296 | } |
| 1297 | |
| 1298 | void clang_disposeOverriddenCursors(CXCursor *overridden) { |
| 1299 | if (!overridden) |
| 1300 | return; |
| 1301 | |
| 1302 | // Use pointer arithmetic to get back the first faux entry |
| 1303 | // which has a back-reference to the TU and the vector. |
| 1304 | --overridden; |
| 1305 | OverridenCursorsPool::CursorVec *Vec = |
| 1306 | static_cast<OverridenCursorsPool::CursorVec*>(overridden->data[0]); |
| 1307 | CXTranslationUnit TU = getCursorTU(*overridden); |
| 1308 | |
| 1309 | assert(Vec && TU); |
| 1310 | |
| 1311 | OverridenCursorsPool &pool = |
| 1312 | *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool); |
| 1313 | |
| 1314 | pool.AvailableCursors.push_back(Vec); |
| 1315 | } |
Douglas Gregor | 8fa0a80 | 2011-08-04 20:04:59 +0000 | [diff] [blame] | 1316 | |
Ted Kremenek | eca099b | 2010-12-08 23:43:14 +0000 | [diff] [blame] | 1317 | } // end: extern "C" |