blob: 6b0e2eb25588b75d45fae5e563b96ee355adbb90 [file] [log] [blame]
Ted Kremenek87553c42010-01-15 20:35:54 +00001//===- 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 Gregor6c8959b2010-01-16 14:00:32 +000010// 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 Kremenek87553c42010-01-15 20:35:54 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek7df92ae2010-11-17 23:24:11 +000016#include "CXTranslationUnit.h"
Ted Kremenek87553c42010-01-15 20:35:54 +000017#include "CXCursor.h"
Ted Kremenek4b4f3692010-11-16 01:56:27 +000018#include "CXString.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000019#include "CXType.h"
20#include "clang-c/Index.h"
David Blaikie0a4e61f2013-09-13 18:32:52 +000021#include "clang/AST/Attr.h"
Ted Kremenek87553c42010-01-15 20:35:54 +000022#include "clang/AST/Decl.h"
Douglas Gregora89314e2010-08-31 23:48:11 +000023#include "clang/AST/DeclCXX.h"
Douglas Gregorc58d05b2010-01-15 21:56:13 +000024#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +000025#include "clang/AST/DeclTemplate.h"
Douglas Gregorc58d05b2010-01-15 21:56:13 +000026#include "clang/AST/Expr.h"
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000027#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +000028#include "clang/AST/ExprObjC.h"
Chandler Carruthcc0694c2012-12-04 09:25:21 +000029#include "clang/Frontend/ASTUnit.h"
Ted Kremenekc2aa0f12010-01-16 00:36:30 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenek87553c42010-01-15 20:35:54 +000031
32using namespace clang;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +000033using namespace cxcursor;
Ted Kremenek87553c42010-01-15 20:35:54 +000034
Ted Kremenekd77f6212012-04-30 19:06:49 +000035CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
Douglas Gregor58552bc2010-01-20 23:34:41 +000036 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
Craig Topper69186e72014-06-08 08:38:04 +000037 CXCursor C = { K, 0, { nullptr, nullptr, TU } };
Douglas Gregor58552bc2010-01-20 23:34:41 +000038 return C;
Ted Kremenek87553c42010-01-15 20:35:54 +000039}
40
Ted Kremenekbff31432010-02-18 03:09:07 +000041static CXCursorKind GetCursorKind(const Attr *A) {
42 assert(A && "Invalid arguments!");
43 switch (A->getKind()) {
44 default: break;
Alexis Hunt344393e2010-06-16 23:43:53 +000045 case attr::IBAction: return CXCursor_IBActionAttr;
46 case attr::IBOutlet: return CXCursor_IBOutletAttr;
47 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Argyrios Kyrtzidis2cb4e3c2011-09-13 17:39:31 +000048 case attr::Final: return CXCursor_CXXFinalAttr;
49 case attr::Override: return CXCursor_CXXOverrideAttr;
Erik Verbruggenca98f2a2011-10-13 09:41:32 +000050 case attr::Annotate: return CXCursor_AnnotateAttr;
Argyrios Kyrtzidis66f433a2011-12-06 22:05:01 +000051 case attr::AsmLabel: return CXCursor_AsmLabelAttr;
Argyrios Kyrtzidis16834f12013-09-25 00:14:38 +000052 case attr::Packed: return CXCursor_PackedAttr;
Joey Gouly81228382014-05-01 15:41:58 +000053 case attr::Pure: return CXCursor_PureAttr;
54 case attr::Const: return CXCursor_ConstAttr;
55 case attr::NoDuplicate: return CXCursor_NoDuplicateAttr;
Eli Bendersky2581e662014-05-28 19:29:58 +000056 case attr::CUDAConstant: return CXCursor_CUDAConstantAttr;
57 case attr::CUDADevice: return CXCursor_CUDADeviceAttr;
58 case attr::CUDAGlobal: return CXCursor_CUDAGlobalAttr;
59 case attr::CUDAHost: return CXCursor_CUDAHostAttr;
Ted Kremenekbff31432010-02-18 03:09:07 +000060 }
61
62 return CXCursor_UnexposedAttr;
63}
64
Dmitri Gribenko9c256e32013-01-14 00:46:27 +000065CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
Ted Kremenek91554282010-11-16 08:15:36 +000066 CXTranslationUnit TU) {
Ted Kremenekbff31432010-02-18 03:09:07 +000067 assert(A && Parent && TU && "Invalid arguments!");
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +000068 CXCursor C = { GetCursorKind(A), 0, { Parent, A, TU } };
Ted Kremenekbff31432010-02-18 03:09:07 +000069 return C;
70}
71
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +000072CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +000073 SourceRange RegionOfInterest,
Ted Kremenek818e5c12010-11-01 23:26:51 +000074 bool FirstInDeclGroup) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +000075 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +000076
77 CXCursorKind K = getCursorKindForDecl(D);
78
79 if (K == CXCursor_ObjCClassMethodDecl ||
80 K == CXCursor_ObjCInstanceMethodDecl) {
81 int SelectorIdIndex = -1;
82 // Check if cursor points to a selector id.
83 if (RegionOfInterest.isValid() &&
84 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
85 SmallVector<SourceLocation, 16> SelLocs;
86 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
Craig Topper2341c0d2013-07-04 03:08:24 +000087 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +000088 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
89 if (I != SelLocs.end())
90 SelectorIdIndex = I - SelLocs.begin();
91 }
92 CXCursor C = { K, SelectorIdIndex,
93 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
94 return C;
95 }
96
97 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor58552bc2010-01-20 23:34:41 +000098 return C;
Ted Kremenekc2aa0f12010-01-16 00:36:30 +000099}
100
Dmitri Gribenko9c256e32013-01-14 00:46:27 +0000101CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000102 CXTranslationUnit TU,
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000103 SourceRange RegionOfInterest) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000104 assert(S && TU && "Invalid arguments!");
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000105 CXCursorKind K = CXCursor_NotImplemented;
106
107 switch (S->getStmtClass()) {
108 case Stmt::NoStmtClass:
109 break;
Douglas Gregor4c362d52011-10-05 19:00:14 +0000110
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000111 case Stmt::CaseStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000112 K = CXCursor_CaseStmt;
113 break;
114
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000115 case Stmt::DefaultStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000116 K = CXCursor_DefaultStmt;
117 break;
118
119 case Stmt::IfStmtClass:
120 K = CXCursor_IfStmt;
121 break;
122
123 case Stmt::SwitchStmtClass:
124 K = CXCursor_SwitchStmt;
125 break;
126
127 case Stmt::WhileStmtClass:
128 K = CXCursor_WhileStmt;
129 break;
130
131 case Stmt::DoStmtClass:
132 K = CXCursor_DoStmt;
133 break;
134
135 case Stmt::ForStmtClass:
136 K = CXCursor_ForStmt;
137 break;
138
139 case Stmt::GotoStmtClass:
140 K = CXCursor_GotoStmt;
141 break;
142
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000143 case Stmt::IndirectGotoStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000144 K = CXCursor_IndirectGotoStmt;
145 break;
146
147 case Stmt::ContinueStmtClass:
148 K = CXCursor_ContinueStmt;
149 break;
150
151 case Stmt::BreakStmtClass:
152 K = CXCursor_BreakStmt;
153 break;
154
155 case Stmt::ReturnStmtClass:
156 K = CXCursor_ReturnStmt;
157 break;
158
Chad Rosierde70e0e2012-08-25 00:11:56 +0000159 case Stmt::GCCAsmStmtClass:
160 K = CXCursor_GCCAsmStmt;
Douglas Gregor4c362d52011-10-05 19:00:14 +0000161 break;
Chad Rosier32503022012-06-11 20:47:18 +0000162
163 case Stmt::MSAsmStmtClass:
164 K = CXCursor_MSAsmStmt;
165 break;
Douglas Gregor4c362d52011-10-05 19:00:14 +0000166
167 case Stmt::ObjCAtTryStmtClass:
168 K = CXCursor_ObjCAtTryStmt;
169 break;
170
171 case Stmt::ObjCAtCatchStmtClass:
172 K = CXCursor_ObjCAtCatchStmt;
173 break;
174
175 case Stmt::ObjCAtFinallyStmtClass:
176 K = CXCursor_ObjCAtFinallyStmt;
177 break;
178
179 case Stmt::ObjCAtThrowStmtClass:
180 K = CXCursor_ObjCAtThrowStmt;
181 break;
182
183 case Stmt::ObjCAtSynchronizedStmtClass:
184 K = CXCursor_ObjCAtSynchronizedStmt;
185 break;
186
187 case Stmt::ObjCAutoreleasePoolStmtClass:
188 K = CXCursor_ObjCAutoreleasePoolStmt;
189 break;
190
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000191 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000192 K = CXCursor_ObjCForCollectionStmt;
193 break;
194
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000195 case Stmt::CXXCatchStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000196 K = CXCursor_CXXCatchStmt;
197 break;
198
199 case Stmt::CXXTryStmtClass:
200 K = CXCursor_CXXTryStmt;
201 break;
202
203 case Stmt::CXXForRangeStmtClass:
204 K = CXCursor_CXXForRangeStmt;
205 break;
206
John Wiegley1c0675e2011-04-28 01:08:34 +0000207 case Stmt::SEHTryStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000208 K = CXCursor_SEHTryStmt;
209 break;
210
John Wiegley1c0675e2011-04-28 01:08:34 +0000211 case Stmt::SEHExceptStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000212 K = CXCursor_SEHExceptStmt;
213 break;
214
John Wiegley1c0675e2011-04-28 01:08:34 +0000215 case Stmt::SEHFinallyStmtClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000216 K = CXCursor_SEHFinallyStmt;
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000217 break;
Nico Weber9b982072014-07-07 00:12:30 +0000218
219 case Stmt::SEHLeaveStmtClass:
220 K = CXCursor_SEHLeaveStmt;
221 break;
Douglas Gregor4c362d52011-10-05 19:00:14 +0000222
John Wiegley6242b6a2011-04-28 00:16:57 +0000223 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner55808c12011-06-04 00:47:47 +0000224 case Stmt::AsTypeExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +0000225 case Stmt::AtomicExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000226 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor29c42f22012-02-24 07:38:34 +0000227 case Stmt::TypeTraitExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000228 case Stmt::CXXBindTemporaryExprClass:
229 case Stmt::CXXDefaultArgExprClass:
Richard Smith852c9db2013-04-20 22:23:05 +0000230 case Stmt::CXXDefaultInitExprClass:
Richard Smithcc1b96d2013-06-12 22:31:48 +0000231 case Stmt::CXXStdInitializerListExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000232 case Stmt::CXXScalarValueInitExprClass:
233 case Stmt::CXXUuidofExprClass:
234 case Stmt::ChooseExprClass:
235 case Stmt::DesignatedInitExprClass:
236 case Stmt::ExprWithCleanupsClass:
237 case Stmt::ExpressionTraitExprClass:
238 case Stmt::ExtVectorElementExprClass:
239 case Stmt::ImplicitCastExprClass:
240 case Stmt::ImplicitValueInitExprClass:
241 case Stmt::MaterializeTemporaryExprClass:
242 case Stmt::ObjCIndirectCopyRestoreExprClass:
243 case Stmt::OffsetOfExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000244 case Stmt::ParenListExprClass:
245 case Stmt::PredefinedExprClass:
246 case Stmt::ShuffleVectorExprClass:
Hal Finkelc4d7c822013-09-18 03:29:45 +0000247 case Stmt::ConvertVectorExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000248 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000249 case Stmt::VAArgExprClass:
Ted Kremenek77006f62012-03-06 20:06:06 +0000250 case Stmt::ObjCArrayLiteralClass:
251 case Stmt::ObjCDictionaryLiteralClass:
Patrick Beard0caa3942012-04-19 00:25:12 +0000252 case Stmt::ObjCBoxedExprClass:
Ted Kremenek77006f62012-03-06 20:06:06 +0000253 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000254 K = CXCursor_UnexposedExpr;
255 break;
Douglas Gregor4c362d52011-10-05 19:00:14 +0000256
John McCallfe96e0b2011-11-06 09:01:30 +0000257 case Stmt::OpaqueValueExprClass:
258 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
259 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
260 K = CXCursor_UnexposedExpr;
261 break;
262
263 case Stmt::PseudoObjectExprClass:
264 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
265 Parent, TU, RegionOfInterest);
266
Douglas Gregor4c362d52011-10-05 19:00:14 +0000267 case Stmt::CompoundStmtClass:
268 K = CXCursor_CompoundStmt;
269 break;
Richard Smithc202b282012-04-14 00:33:13 +0000270
Douglas Gregor4c362d52011-10-05 19:00:14 +0000271 case Stmt::NullStmtClass:
272 K = CXCursor_NullStmt;
273 break;
Richard Smithc202b282012-04-14 00:33:13 +0000274
Douglas Gregor4c362d52011-10-05 19:00:14 +0000275 case Stmt::LabelStmtClass:
276 K = CXCursor_LabelStmt;
277 break;
Richard Smithc202b282012-04-14 00:33:13 +0000278
279 case Stmt::AttributedStmtClass:
280 K = CXCursor_UnexposedStmt;
281 break;
282
Douglas Gregor4c362d52011-10-05 19:00:14 +0000283 case Stmt::DeclStmtClass:
284 K = CXCursor_DeclStmt;
285 break;
Richard Smithc202b282012-04-14 00:33:13 +0000286
Tareq A. Siraj24110cc2013-04-16 18:53:08 +0000287 case Stmt::CapturedStmtClass:
288 K = CXCursor_UnexposedStmt;
289 break;
290
Douglas Gregor4c362d52011-10-05 19:00:14 +0000291 case Stmt::IntegerLiteralClass:
292 K = CXCursor_IntegerLiteral;
293 break;
294
295 case Stmt::FloatingLiteralClass:
296 K = CXCursor_FloatingLiteral;
297 break;
298
299 case Stmt::ImaginaryLiteralClass:
300 K = CXCursor_ImaginaryLiteral;
301 break;
302
303 case Stmt::StringLiteralClass:
304 K = CXCursor_StringLiteral;
305 break;
306
307 case Stmt::CharacterLiteralClass:
308 K = CXCursor_CharacterLiteral;
309 break;
310
311 case Stmt::ParenExprClass:
312 K = CXCursor_ParenExpr;
313 break;
314
315 case Stmt::UnaryOperatorClass:
316 K = CXCursor_UnaryOperator;
317 break;
Richard Smithc202b282012-04-14 00:33:13 +0000318
Douglas Gregor4c362d52011-10-05 19:00:14 +0000319 case Stmt::CXXNoexceptExprClass:
320 K = CXCursor_UnaryExpr;
321 break;
322
323 case Stmt::ArraySubscriptExprClass:
324 K = CXCursor_ArraySubscriptExpr;
325 break;
326
327 case Stmt::BinaryOperatorClass:
328 K = CXCursor_BinaryOperator;
329 break;
330
331 case Stmt::CompoundAssignOperatorClass:
332 K = CXCursor_CompoundAssignOperator;
333 break;
334
335 case Stmt::ConditionalOperatorClass:
336 K = CXCursor_ConditionalOperator;
337 break;
338
339 case Stmt::CStyleCastExprClass:
340 K = CXCursor_CStyleCastExpr;
341 break;
342
343 case Stmt::CompoundLiteralExprClass:
344 K = CXCursor_CompoundLiteralExpr;
345 break;
346
347 case Stmt::InitListExprClass:
348 K = CXCursor_InitListExpr;
349 break;
350
351 case Stmt::AddrLabelExprClass:
352 K = CXCursor_AddrLabelExpr;
353 break;
354
355 case Stmt::StmtExprClass:
356 K = CXCursor_StmtExpr;
357 break;
358
359 case Stmt::GenericSelectionExprClass:
360 K = CXCursor_GenericSelectionExpr;
361 break;
362
363 case Stmt::GNUNullExprClass:
364 K = CXCursor_GNUNullExpr;
365 break;
366
367 case Stmt::CXXStaticCastExprClass:
368 K = CXCursor_CXXStaticCastExpr;
369 break;
370
371 case Stmt::CXXDynamicCastExprClass:
372 K = CXCursor_CXXDynamicCastExpr;
373 break;
374
375 case Stmt::CXXReinterpretCastExprClass:
376 K = CXCursor_CXXReinterpretCastExpr;
377 break;
378
379 case Stmt::CXXConstCastExprClass:
380 K = CXCursor_CXXConstCastExpr;
381 break;
382
383 case Stmt::CXXFunctionalCastExprClass:
384 K = CXCursor_CXXFunctionalCastExpr;
385 break;
386
387 case Stmt::CXXTypeidExprClass:
388 K = CXCursor_CXXTypeidExpr;
389 break;
390
391 case Stmt::CXXBoolLiteralExprClass:
392 K = CXCursor_CXXBoolLiteralExpr;
393 break;
394
395 case Stmt::CXXNullPtrLiteralExprClass:
396 K = CXCursor_CXXNullPtrLiteralExpr;
397 break;
398
399 case Stmt::CXXThisExprClass:
400 K = CXCursor_CXXThisExpr;
401 break;
402
403 case Stmt::CXXThrowExprClass:
404 K = CXCursor_CXXThrowExpr;
405 break;
406
407 case Stmt::CXXNewExprClass:
408 K = CXCursor_CXXNewExpr;
409 break;
410
411 case Stmt::CXXDeleteExprClass:
412 K = CXCursor_CXXDeleteExpr;
413 break;
414
415 case Stmt::ObjCStringLiteralClass:
416 K = CXCursor_ObjCStringLiteral;
417 break;
418
419 case Stmt::ObjCEncodeExprClass:
420 K = CXCursor_ObjCEncodeExpr;
421 break;
422
423 case Stmt::ObjCSelectorExprClass:
424 K = CXCursor_ObjCSelectorExpr;
425 break;
426
427 case Stmt::ObjCProtocolExprClass:
428 K = CXCursor_ObjCProtocolExpr;
429 break;
Ted Kremenek77006f62012-03-06 20:06:06 +0000430
431 case Stmt::ObjCBoolLiteralExprClass:
432 K = CXCursor_ObjCBoolLiteralExpr;
433 break;
434
Douglas Gregor4c362d52011-10-05 19:00:14 +0000435 case Stmt::ObjCBridgedCastExprClass:
436 K = CXCursor_ObjCBridgedCastExpr;
437 break;
438
439 case Stmt::BlockExprClass:
440 K = CXCursor_BlockExpr;
441 break;
442
443 case Stmt::PackExpansionExprClass:
444 K = CXCursor_PackExpansionExpr;
445 break;
446
447 case Stmt::SizeOfPackExprClass:
448 K = CXCursor_SizeOfPackExpr;
449 break;
450
Argyrios Kyrtzidisc2233be2013-04-23 17:57:17 +0000451 case Stmt::DeclRefExprClass:
452 if (const ImplicitParamDecl *IPD =
453 dyn_cast_or_null<ImplicitParamDecl>(cast<DeclRefExpr>(S)->getDecl())) {
454 if (const ObjCMethodDecl *MD =
455 dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
456 if (MD->getSelfDecl() == IPD) {
457 K = CXCursor_ObjCSelfExpr;
458 break;
459 }
460 }
461 }
462
463 K = CXCursor_DeclRefExpr;
464 break;
465
Douglas Gregor4c362d52011-10-05 19:00:14 +0000466 case Stmt::DependentScopeDeclRefExprClass:
John McCall7c454bb2011-07-15 05:09:51 +0000467 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorcdbc5392011-01-15 01:15:58 +0000468 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Richard Smithb15fe3a2012-09-12 00:56:43 +0000469 case Stmt::FunctionParmPackExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000470 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000471 K = CXCursor_DeclRefExpr;
472 break;
473
Douglas Gregor4c362d52011-10-05 19:00:14 +0000474 case Stmt::CXXDependentScopeMemberExprClass:
475 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000476 case Stmt::MemberExprClass:
John McCall5e77d762013-04-16 07:28:30 +0000477 case Stmt::MSPropertyRefExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000478 case Stmt::ObjCIsaExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000479 case Stmt::ObjCIvarRefExprClass:
480 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000481 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000482 K = CXCursor_MemberRefExpr;
483 break;
484
485 case Stmt::CallExprClass:
486 case Stmt::CXXOperatorCallExprClass:
487 case Stmt::CXXMemberCallExprClass:
Peter Collingbourne41f85462011-02-09 21:07:24 +0000488 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000489 case Stmt::CXXConstructExprClass:
490 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor4c362d52011-10-05 19:00:14 +0000491 case Stmt::CXXUnresolvedConstructExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +0000492 case Stmt::UserDefinedLiteralClass:
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000493 K = CXCursor_CallExpr;
494 break;
495
Douglas Gregor30093832012-02-15 00:54:55 +0000496 case Stmt::LambdaExprClass:
497 K = CXCursor_LambdaExpr;
498 break;
499
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000500 case Stmt::ObjCMessageExprClass: {
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000501 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000502 int SelectorIdIndex = -1;
503 // Check if cursor points to a selector id.
504 if (RegionOfInterest.isValid() &&
505 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
506 SmallVector<SourceLocation, 16> SelLocs;
507 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
Craig Topper2341c0d2013-07-04 03:08:24 +0000508 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000509 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
510 if (I != SelLocs.end())
511 SelectorIdIndex = I - SelLocs.begin();
512 }
513 CXCursor C = { K, 0, { Parent, S, TU } };
514 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000515 }
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000516
517 case Stmt::MSDependentExistsStmtClass:
518 K = CXCursor_UnexposedStmt;
519 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +0000520 case Stmt::OMPParallelDirectiveClass:
521 K = CXCursor_OMPParallelDirective;
522 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000523 case Stmt::OMPSimdDirectiveClass:
524 K = CXCursor_OMPSimdDirective;
525 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +0000526 case Stmt::OMPForDirectiveClass:
527 K = CXCursor_OMPForDirective;
528 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +0000529 case Stmt::OMPSectionsDirectiveClass:
530 K = CXCursor_OMPSectionsDirective;
531 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +0000532 case Stmt::OMPSectionDirectiveClass:
533 K = CXCursor_OMPSectionDirective;
534 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +0000535 case Stmt::OMPSingleDirectiveClass:
536 K = CXCursor_OMPSingleDirective;
537 break;
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000538 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000539
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000540 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000541 return C;
542}
543
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000544CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000545 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000546 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000547 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000548 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000549 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000550 return C;
551}
552
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000553std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000554cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
555 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000556 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000557 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000558}
559
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000560CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000561 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000562 CXTranslationUnit TU) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000563 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000564 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000565 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregoref6eb842010-01-16 15:44:18 +0000566 return C;
567}
568
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000569std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregoref6eb842010-01-16 15:44:18 +0000570cxcursor::getCursorObjCProtocolRef(CXCursor C) {
571 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000572 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000573 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregoref6eb842010-01-16 15:44:18 +0000574}
575
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000576CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000577 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000578 CXTranslationUnit TU) {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000579 // 'Class' can be null for invalid code.
580 if (!Class)
581 return MakeCXCursorInvalid(CXCursor_InvalidCode);
582 assert(TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000583 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000584 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor46d66142010-01-16 17:14:40 +0000585 return C;
586}
587
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000588std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor46d66142010-01-16 17:14:40 +0000589cxcursor::getCursorObjCClassRef(CXCursor C) {
590 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000591 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000592 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor46d66142010-01-16 17:14:40 +0000593}
594
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000595CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000596 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000597 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000598 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000599 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor93f89952010-01-21 16:28:34 +0000600 return C;
601}
602
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000603std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor93f89952010-01-21 16:28:34 +0000604cxcursor::getCursorTypeRef(CXCursor C) {
605 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000606 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000607 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor93f89952010-01-21 16:28:34 +0000608}
609
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000610CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremenek91554282010-11-16 08:15:36 +0000611 SourceLocation Loc,
612 CXTranslationUnit TU) {
Douglas Gregora23e8f72010-08-31 20:37:03 +0000613 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000614 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000615 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregora23e8f72010-08-31 20:37:03 +0000616 return C;
617}
618
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000619std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregora23e8f72010-08-31 20:37:03 +0000620cxcursor::getCursorTemplateRef(CXCursor C) {
621 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000622 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000623 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora23e8f72010-08-31 20:37:03 +0000624}
625
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000626CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
627 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000628 CXTranslationUnit TU) {
Douglas Gregora89314e2010-08-31 23:48:11 +0000629
630 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
631 "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000632 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000633 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregora89314e2010-08-31 23:48:11 +0000634 return C;
635}
636
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000637std::pair<const NamedDecl *, SourceLocation>
Douglas Gregora89314e2010-08-31 23:48:11 +0000638cxcursor::getCursorNamespaceRef(CXCursor C) {
639 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000640 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000641 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora89314e2010-08-31 23:48:11 +0000642}
643
Douglas Gregor30093832012-02-15 00:54:55 +0000644CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
645 CXTranslationUnit TU) {
646
647 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000648 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000649 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor30093832012-02-15 00:54:55 +0000650 return C;
651}
652
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000653std::pair<const VarDecl *, SourceLocation>
Douglas Gregor30093832012-02-15 00:54:55 +0000654cxcursor::getCursorVariableRef(CXCursor C) {
655 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000656 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000657 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor30093832012-02-15 00:54:55 +0000658}
659
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000660CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000661 CXTranslationUnit TU) {
Douglas Gregorf3af3112010-09-09 21:42:20 +0000662
663 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000664 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000665 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregorf3af3112010-09-09 21:42:20 +0000666 return C;
667}
668
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000669std::pair<const FieldDecl *, SourceLocation>
Douglas Gregorf3af3112010-09-09 21:42:20 +0000670cxcursor::getCursorMemberRef(CXCursor C) {
671 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000672 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000673 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregorf3af3112010-09-09 21:42:20 +0000674}
675
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000676CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremenek91554282010-11-16 08:15:36 +0000677 CXTranslationUnit TU){
Craig Topper69186e72014-06-08 08:38:04 +0000678 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, nullptr, TU } };
Ted Kremenekae9e2212010-08-27 21:34:58 +0000679 return C;
680}
681
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000682const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenekae9e2212010-08-27 21:34:58 +0000683 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000684 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenekae9e2212010-08-27 21:34:58 +0000685}
686
Douglas Gregor92a524f2010-03-18 00:42:48 +0000687CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremenek91554282010-11-16 08:15:36 +0000688 CXTranslationUnit TU) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000689 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000690 { Range.getBegin().getPtrEncoding(),
691 Range.getEnd().getPtrEncoding(),
Douglas Gregor92a524f2010-03-18 00:42:48 +0000692 TU }
693 };
694 return C;
695}
696
697SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
698 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000699 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
700 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +0000701 ASTUnit *TU = getCursorASTUnit(C);
702 return TU->mapRangeFromPreamble(Range);
Douglas Gregor92a524f2010-03-18 00:42:48 +0000703}
704
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000705CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremenek91554282010-11-16 08:15:36 +0000706 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000707 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, nullptr, TU } };
Douglas Gregor06d6d322010-03-18 18:04:21 +0000708 return C;
709}
710
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000711const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor06d6d322010-03-18 18:04:21 +0000712 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000713 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor06d6d322010-03-18 18:04:21 +0000714}
715
Chandler Carrutha88a22182011-07-14 08:20:46 +0000716CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
717 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000718 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, nullptr, TU } };
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000719 return C;
720}
721
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000722CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
723 SourceLocation Loc,
724 CXTranslationUnit TU) {
725 assert(Loc.isValid());
726 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
727 return C;
728}
729
730const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
731 if (isPseudo())
732 return getAsMacroDefinition()->getName();
733 return getAsMacroExpansion()->getName();
734}
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000735const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000736 if (isPseudo())
737 return getAsMacroDefinition();
738 return getAsMacroExpansion()->getDefinition();
739}
740SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
741 if (isPseudo())
742 return getPseudoLoc();
743 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000744}
745
Douglas Gregor796d76a2010-10-20 22:00:55 +0000746CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremenek91554282010-11-16 08:15:36 +0000747 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000748 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, nullptr, TU } };
Douglas Gregor796d76a2010-10-20 22:00:55 +0000749 return C;
750}
751
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000752const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000753 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000754 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000755}
756
Douglas Gregora93ab662010-09-10 00:22:18 +0000757CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000758 CXTranslationUnit TU) {
Douglas Gregora93ab662010-09-10 00:22:18 +0000759
760 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000761 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000762 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregora93ab662010-09-10 00:22:18 +0000763 return C;
764}
765
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000766std::pair<const LabelStmt *, SourceLocation>
Douglas Gregora93ab662010-09-10 00:22:18 +0000767cxcursor::getCursorLabelRef(CXCursor C) {
768 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000769 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000770 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora93ab662010-09-10 00:22:18 +0000771}
772
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000773CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremenek91554282010-11-16 08:15:36 +0000774 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000775 assert(E && TU && "Invalid arguments!");
776 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000777 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000778 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000779 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000780 { Storage.getOpaqueValue(), RawLoc, TU }
781 };
782 return C;
783}
784
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000785CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000786 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000787 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000788 assert(D && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000789 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000790 OverloadedDeclRefStorage Storage(D);
791 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000792 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000793 { Storage.getOpaqueValue(), RawLoc, TU }
794 };
795 return C;
796}
797
798CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
799 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000800 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000801 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000802 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000803 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
804 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000805 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000806 { Storage.getOpaqueValue(), RawLoc, TU }
807 };
808 return C;
809}
810
811std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
812cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
813 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000814 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
815 const_cast<void *>(C.data[0])),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000816 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000817}
818
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000819const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
820 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000821}
822
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000823const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000824 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
825}
826
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000827const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregoref6eb842010-01-16 15:44:18 +0000828 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor46d66142010-01-16 17:14:40 +0000829 Cursor.kind == CXCursor_ObjCProtocolRef ||
830 Cursor.kind == CXCursor_ObjCClassRef)
Craig Topper69186e72014-06-08 08:38:04 +0000831 return nullptr;
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000832
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000833 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000834}
835
Dmitri Gribenkoe4baea62013-01-26 18:08:08 +0000836const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
837 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremeneka5940822010-08-26 01:42:22 +0000838}
839
Dmitri Gribenkoa1691182013-01-26 18:12:08 +0000840const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
841 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8bb2ecf2011-06-29 22:20:07 +0000842}
843
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000844ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorfed36b12010-01-20 23:57:43 +0000845 return getCursorASTUnit(Cursor)->getASTContext();
846}
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000847
Douglas Gregorfed36b12010-01-20 23:57:43 +0000848ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko2c173b42013-01-11 19:28:44 +0000849 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidisfa469d02011-12-09 00:17:49 +0000850 if (!TU)
Craig Topper69186e72014-06-08 08:38:04 +0000851 return nullptr;
Dmitri Gribenkoc22ea1c2013-01-26 18:53:38 +0000852 return cxtu::getASTUnit(TU);
Ted Kremenek91554282010-11-16 08:15:36 +0000853}
854
855CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000856 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000857}
858
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000859void cxcursor::getOverriddenCursors(CXCursor cursor,
860 SmallVectorImpl<CXCursor> &overridden) {
861 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000862 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000863 if (!D)
864 return;
865
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000866 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000867 SmallVector<const NamedDecl *, 8> OverDecls;
868 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000869
Craig Topper2341c0d2013-07-04 03:08:24 +0000870 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000871 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko9c256e32013-01-14 00:46:27 +0000872 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000873 }
874}
875
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000876std::pair<int, SourceLocation>
877cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
878 if (cursor.kind == CXCursor_ObjCMessageExpr) {
879 if (cursor.xdata != -1)
880 return std::make_pair(cursor.xdata,
881 cast<ObjCMessageExpr>(getCursorExpr(cursor))
882 ->getSelectorLoc(cursor.xdata));
883 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
884 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
885 if (cursor.xdata != -1)
886 return std::make_pair(cursor.xdata,
887 cast<ObjCMethodDecl>(getCursorDecl(cursor))
888 ->getSelectorLoc(cursor.xdata));
889 }
890
891 return std::make_pair(-1, SourceLocation());
892}
893
894CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
895 CXCursor newCursor = cursor;
896
897 if (cursor.kind == CXCursor_ObjCMessageExpr) {
898 if (SelIdx == -1 ||
899 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
900 ->getNumSelectorLocs())
901 newCursor.xdata = -1;
902 else
903 newCursor.xdata = SelIdx;
904 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
905 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
906 if (SelIdx == -1 ||
907 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
908 ->getNumSelectorLocs())
909 newCursor.xdata = -1;
910 else
911 newCursor.xdata = SelIdx;
912 }
913
914 return newCursor;
915}
916
917CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
918 if (cursor.kind != CXCursor_CallExpr)
919 return cursor;
920
921 if (cursor.xdata == 0)
922 return cursor;
923
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000924 const Expr *E = getCursorExpr(cursor);
Craig Topper69186e72014-06-08 08:38:04 +0000925 TypeSourceInfo *Type = nullptr;
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000926 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000927 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
928 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000929 } else if (const CXXTemporaryObjectExpr *Tmp =
930 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000931 Type = Tmp->getTypeSourceInfo();
932 }
933
934 if (!Type)
935 return cursor;
936
937 CXTranslationUnit TU = getCursorTU(cursor);
938 QualType Ty = Type->getType();
939 TypeLoc TL = Type->getTypeLoc();
940 SourceLocation Loc = TL.getBeginLoc();
941
942 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
943 Ty = ElabT->getNamedType();
David Blaikie6adc78e2013-02-18 22:06:02 +0000944 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000945 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
946 }
947
948 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
949 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
950 if (const TagType *Tag = Ty->getAs<TagType>())
951 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
952 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
953 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
954
955 return cursor;
956}
957
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000958bool cxcursor::operator==(CXCursor X, CXCursor Y) {
959 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
960 X.data[2] == Y.data[2];
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000961}
Ted Kremenek818e5c12010-11-01 23:26:51 +0000962
963// FIXME: Remove once we can model DeclGroups and their appropriate ranges
964// properly in the ASTs.
965bool cxcursor::isFirstInDeclGroup(CXCursor C) {
966 assert(clang_isDeclaration(C.kind));
967 return ((uintptr_t) (C.data[1])) != 0;
968}
969
Ted Kremenekbbedd062010-12-08 23:43:14 +0000970//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000971// libclang CXCursor APIs
972//===----------------------------------------------------------------------===//
973
Argyrios Kyrtzidisa1bcb6a2011-09-27 04:14:36 +0000974extern "C" {
975
976int clang_Cursor_isNull(CXCursor cursor) {
977 return clang_equalCursors(cursor, clang_getNullCursor());
978}
979
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000980CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
981 return getCursorTU(cursor);
982}
983
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +0000984int clang_Cursor_getNumArguments(CXCursor C) {
985 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000986 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +0000987 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
988 return MD->param_size();
989 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
990 return FD->param_size();
991 }
992
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +0000993 if (clang_isExpression(C.kind)) {
994 const Expr *E = cxcursor::getCursorExpr(C);
995 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
996 return CE->getNumArgs();
997 }
998 }
999
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001000 return -1;
1001}
1002
1003CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1004 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001005 const Decl *D = cxcursor::getCursorDecl(C);
1006 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001007 if (i < MD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001008 return cxcursor::MakeCXCursor(MD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001009 cxcursor::getCursorTU(C));
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001010 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001011 if (i < FD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001012 return cxcursor::MakeCXCursor(FD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001013 cxcursor::getCursorTU(C));
1014 }
1015 }
1016
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +00001017 if (clang_isExpression(C.kind)) {
1018 const Expr *E = cxcursor::getCursorExpr(C);
1019 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1020 if (i < CE->getNumArgs()) {
1021 return cxcursor::MakeCXCursor(CE->getArg(i),
1022 getCursorDecl(C),
1023 cxcursor::getCursorTU(C));
1024 }
1025 }
1026 }
1027
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001028 return clang_getNullCursor();
1029}
1030
Ted Kremenekc0b98662013-04-24 07:17:12 +00001031} // end: extern "C"
1032
1033//===----------------------------------------------------------------------===//
1034// CXCursorSet.
1035//===----------------------------------------------------------------------===//
1036
1037typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1038
1039static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1040 return (CXCursorSet) setImpl;
1041}
1042static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1043 return (CXCursorSet_Impl*) set;
1044}
1045namespace llvm {
1046template<> struct DenseMapInfo<CXCursor> {
1047public:
1048 static inline CXCursor getEmptyKey() {
1049 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1050 }
1051 static inline CXCursor getTombstoneKey() {
1052 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1053 }
1054 static inline unsigned getHashValue(const CXCursor &cursor) {
1055 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1056 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1057 }
1058 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1059 return x.kind == y.kind &&
1060 x.data[0] == y.data[0] &&
1061 x.data[1] == y.data[1];
1062 }
1063};
1064}
1065
1066extern "C" {
1067CXCursorSet clang_createCXCursorSet() {
1068 return packCXCursorSet(new CXCursorSet_Impl());
1069}
1070
1071void clang_disposeCXCursorSet(CXCursorSet set) {
1072 delete unpackCXCursorSet(set);
1073}
1074
1075unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1076 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1077 if (!setImpl)
1078 return 0;
Ted Kremenek29b28e82013-04-24 07:25:40 +00001079 return setImpl->find(cursor) != setImpl->end();
Ted Kremenekc0b98662013-04-24 07:17:12 +00001080}
1081
1082unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1083 // Do not insert invalid cursors into the set.
1084 if (cursor.kind >= CXCursor_FirstInvalid &&
1085 cursor.kind <= CXCursor_LastInvalid)
1086 return 1;
1087
1088 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1089 if (!setImpl)
1090 return 1;
1091 unsigned &entry = (*setImpl)[cursor];
1092 unsigned flag = entry == 0 ? 1 : 0;
1093 entry = 1;
1094 return flag;
1095}
1096
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001097CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1098 enum CXCursorKind kind = clang_getCursorKind(cursor);
1099 if (clang_isDeclaration(kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001100 const Decl *decl = getCursorDecl(cursor);
1101 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001102 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00001103 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001104 CodeCompletionString *String
1105 = Result.CreateCodeCompletionString(unit->getASTContext(),
1106 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001107 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001108 unit->getCodeCompletionTUInfo(),
1109 true);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001110 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001111 }
1112 }
1113 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001114 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001115 const IdentifierInfo *MacroInfo = definition->getName();
1116 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenko049a4ff2013-01-14 00:36:42 +00001117 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001118 CodeCompletionString *String
1119 = Result.CreateCodeCompletionString(unit->getASTContext(),
1120 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001121 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001122 unit->getCodeCompletionTUInfo(),
1123 false);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001124 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001125 }
Craig Topper69186e72014-06-08 08:38:04 +00001126 return nullptr;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001127}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001128} // end: extern C.
Ted Kremenekd77f6212012-04-30 19:06:49 +00001129
1130namespace {
1131 struct OverridenCursorsPool {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001132 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001133 std::vector<CursorVec*> AllCursors;
1134 std::vector<CursorVec*> AvailableCursors;
1135
1136 ~OverridenCursorsPool() {
1137 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1138 E = AllCursors.end(); I != E; ++I) {
1139 delete *I;
1140 }
1141 }
1142 };
1143}
1144
1145void *cxcursor::createOverridenCXCursorsPool() {
1146 return new OverridenCursorsPool();
1147}
1148
1149void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1150 delete static_cast<OverridenCursorsPool*>(pool);
1151}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001152
1153extern "C" {
Ted Kremenekd77f6212012-04-30 19:06:49 +00001154void clang_getOverriddenCursors(CXCursor cursor,
1155 CXCursor **overridden,
1156 unsigned *num_overridden) {
1157 if (overridden)
Craig Topper69186e72014-06-08 08:38:04 +00001158 *overridden = nullptr;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001159 if (num_overridden)
1160 *num_overridden = 0;
1161
1162 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1163
1164 if (!overridden || !num_overridden || !TU)
1165 return;
1166
1167 if (!clang_isDeclaration(cursor.kind))
1168 return;
1169
1170 OverridenCursorsPool &pool =
1171 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
Craig Topper69186e72014-06-08 08:38:04 +00001172
1173 OverridenCursorsPool::CursorVec *Vec = nullptr;
1174
Ted Kremenekd77f6212012-04-30 19:06:49 +00001175 if (!pool.AvailableCursors.empty()) {
1176 Vec = pool.AvailableCursors.back();
1177 pool.AvailableCursors.pop_back();
1178 }
1179 else {
1180 Vec = new OverridenCursorsPool::CursorVec();
1181 pool.AllCursors.push_back(Vec);
1182 }
1183
1184 // Clear out the vector, but don't free the memory contents. This
1185 // reduces malloc() traffic.
1186 Vec->clear();
1187
1188 // Use the first entry to contain a back reference to the vector.
1189 // This is a complete hack.
1190 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1191 backRefCursor.data[0] = Vec;
1192 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1193 Vec->push_back(backRefCursor);
1194
1195 // Get the overriden cursors.
1196 cxcursor::getOverriddenCursors(cursor, *Vec);
1197
1198 // Did we get any overriden cursors? If not, return Vec to the pool
1199 // of available cursor vectors.
1200 if (Vec->size() == 1) {
1201 pool.AvailableCursors.push_back(Vec);
1202 return;
1203 }
1204
1205 // Now tell the caller about the overriden cursors.
1206 assert(Vec->size() > 1);
1207 *overridden = &((*Vec)[1]);
1208 *num_overridden = Vec->size() - 1;
1209}
1210
1211void clang_disposeOverriddenCursors(CXCursor *overridden) {
1212 if (!overridden)
1213 return;
1214
1215 // Use pointer arithmetic to get back the first faux entry
1216 // which has a back-reference to the TU and the vector.
1217 --overridden;
1218 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001219 static_cast<OverridenCursorsPool::CursorVec *>(
1220 const_cast<void *>(overridden->data[0]));
Ted Kremenekd77f6212012-04-30 19:06:49 +00001221 CXTranslationUnit TU = getCursorTU(*overridden);
1222
1223 assert(Vec && TU);
1224
1225 OverridenCursorsPool &pool =
1226 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1227
1228 pool.AvailableCursors.push_back(Vec);
1229}
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001230
1231int clang_Cursor_isDynamicCall(CXCursor C) {
Craig Topper69186e72014-06-08 08:38:04 +00001232 const Expr *E = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001233 if (clang_isExpression(C.kind))
1234 E = getCursorExpr(C);
1235 if (!E)
1236 return 0;
1237
1238 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1239 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1240
Craig Topper69186e72014-06-08 08:38:04 +00001241 const MemberExpr *ME = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001242 if (isa<MemberExpr>(E))
1243 ME = cast<MemberExpr>(E);
1244 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1245 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1246
1247 if (ME) {
1248 if (const CXXMethodDecl *
1249 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1250 return MD->isVirtual() && !ME->hasQualifier();
1251 }
1252
1253 return 0;
1254}
1255
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001256CXType clang_Cursor_getReceiverType(CXCursor C) {
1257 CXTranslationUnit TU = cxcursor::getCursorTU(C);
Craig Topper69186e72014-06-08 08:38:04 +00001258 const Expr *E = nullptr;
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001259 if (clang_isExpression(C.kind))
1260 E = getCursorExpr(C);
1261
1262 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1263 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1264
1265 return cxtype::MakeCXType(QualType(), TU);
1266}
1267
Ted Kremenekbbedd062010-12-08 23:43:14 +00001268} // end: extern "C"