blob: f96e17f5b321b489e5002b303af861cee741327c [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;
Alexander Musman80c22892014-07-17 08:54:58 +0000538 case Stmt::OMPMasterDirectiveClass:
539 K = CXCursor_OMPMasterDirective;
540 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000541 case Stmt::OMPParallelForDirectiveClass:
542 K = CXCursor_OMPParallelForDirective;
543 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000544 case Stmt::OMPParallelSectionsDirectiveClass:
545 K = CXCursor_OMPParallelSectionsDirective;
546 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000547 case Stmt::OMPTaskDirectiveClass:
548 K = CXCursor_OMPTaskDirective;
549 break;
Alexey Bataev68446b72014-07-18 07:47:19 +0000550 case Stmt::OMPTaskyieldDirectiveClass:
551 K = CXCursor_OMPTaskyieldDirective;
552 break;
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000553 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000554
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000555 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000556 return C;
557}
558
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000559CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000560 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000561 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000562 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000563 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000564 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000565 return C;
566}
567
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000568std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000569cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
570 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000571 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000572 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000573}
574
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000575CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000576 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000577 CXTranslationUnit TU) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000578 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000579 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000580 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregoref6eb842010-01-16 15:44:18 +0000581 return C;
582}
583
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000584std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregoref6eb842010-01-16 15:44:18 +0000585cxcursor::getCursorObjCProtocolRef(CXCursor C) {
586 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000587 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000588 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregoref6eb842010-01-16 15:44:18 +0000589}
590
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000591CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000592 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000593 CXTranslationUnit TU) {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000594 // 'Class' can be null for invalid code.
595 if (!Class)
596 return MakeCXCursorInvalid(CXCursor_InvalidCode);
597 assert(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_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor46d66142010-01-16 17:14:40 +0000600 return C;
601}
602
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000603std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor46d66142010-01-16 17:14:40 +0000604cxcursor::getCursorObjCClassRef(CXCursor C) {
605 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000606 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000607 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor46d66142010-01-16 17:14:40 +0000608}
609
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000610CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000611 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000612 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000613 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000614 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor93f89952010-01-21 16:28:34 +0000615 return C;
616}
617
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000618std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor93f89952010-01-21 16:28:34 +0000619cxcursor::getCursorTypeRef(CXCursor C) {
620 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000621 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000622 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor93f89952010-01-21 16:28:34 +0000623}
624
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000625CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremenek91554282010-11-16 08:15:36 +0000626 SourceLocation Loc,
627 CXTranslationUnit TU) {
Douglas Gregora23e8f72010-08-31 20:37:03 +0000628 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000629 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000630 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregora23e8f72010-08-31 20:37:03 +0000631 return C;
632}
633
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000634std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregora23e8f72010-08-31 20:37:03 +0000635cxcursor::getCursorTemplateRef(CXCursor C) {
636 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000637 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000638 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora23e8f72010-08-31 20:37:03 +0000639}
640
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000641CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
642 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000643 CXTranslationUnit TU) {
Douglas Gregora89314e2010-08-31 23:48:11 +0000644
645 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
646 "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000647 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000648 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregora89314e2010-08-31 23:48:11 +0000649 return C;
650}
651
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000652std::pair<const NamedDecl *, SourceLocation>
Douglas Gregora89314e2010-08-31 23:48:11 +0000653cxcursor::getCursorNamespaceRef(CXCursor C) {
654 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000655 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000656 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora89314e2010-08-31 23:48:11 +0000657}
658
Douglas Gregor30093832012-02-15 00:54:55 +0000659CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
660 CXTranslationUnit TU) {
661
662 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000663 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000664 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor30093832012-02-15 00:54:55 +0000665 return C;
666}
667
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000668std::pair<const VarDecl *, SourceLocation>
Douglas Gregor30093832012-02-15 00:54:55 +0000669cxcursor::getCursorVariableRef(CXCursor C) {
670 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000671 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000672 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor30093832012-02-15 00:54:55 +0000673}
674
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000675CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000676 CXTranslationUnit TU) {
Douglas Gregorf3af3112010-09-09 21:42:20 +0000677
678 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000679 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000680 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregorf3af3112010-09-09 21:42:20 +0000681 return C;
682}
683
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000684std::pair<const FieldDecl *, SourceLocation>
Douglas Gregorf3af3112010-09-09 21:42:20 +0000685cxcursor::getCursorMemberRef(CXCursor C) {
686 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000687 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000688 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregorf3af3112010-09-09 21:42:20 +0000689}
690
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000691CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremenek91554282010-11-16 08:15:36 +0000692 CXTranslationUnit TU){
Craig Topper69186e72014-06-08 08:38:04 +0000693 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, nullptr, TU } };
Ted Kremenekae9e2212010-08-27 21:34:58 +0000694 return C;
695}
696
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000697const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenekae9e2212010-08-27 21:34:58 +0000698 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000699 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenekae9e2212010-08-27 21:34:58 +0000700}
701
Douglas Gregor92a524f2010-03-18 00:42:48 +0000702CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremenek91554282010-11-16 08:15:36 +0000703 CXTranslationUnit TU) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000704 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000705 { Range.getBegin().getPtrEncoding(),
706 Range.getEnd().getPtrEncoding(),
Douglas Gregor92a524f2010-03-18 00:42:48 +0000707 TU }
708 };
709 return C;
710}
711
712SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
713 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000714 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
715 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +0000716 ASTUnit *TU = getCursorASTUnit(C);
717 return TU->mapRangeFromPreamble(Range);
Douglas Gregor92a524f2010-03-18 00:42:48 +0000718}
719
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000720CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremenek91554282010-11-16 08:15:36 +0000721 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000722 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, nullptr, TU } };
Douglas Gregor06d6d322010-03-18 18:04:21 +0000723 return C;
724}
725
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000726const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor06d6d322010-03-18 18:04:21 +0000727 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000728 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor06d6d322010-03-18 18:04:21 +0000729}
730
Chandler Carrutha88a22182011-07-14 08:20:46 +0000731CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
732 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000733 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, nullptr, TU } };
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000734 return C;
735}
736
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000737CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
738 SourceLocation Loc,
739 CXTranslationUnit TU) {
740 assert(Loc.isValid());
741 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
742 return C;
743}
744
745const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
746 if (isPseudo())
747 return getAsMacroDefinition()->getName();
748 return getAsMacroExpansion()->getName();
749}
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000750const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000751 if (isPseudo())
752 return getAsMacroDefinition();
753 return getAsMacroExpansion()->getDefinition();
754}
755SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
756 if (isPseudo())
757 return getPseudoLoc();
758 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000759}
760
Douglas Gregor796d76a2010-10-20 22:00:55 +0000761CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremenek91554282010-11-16 08:15:36 +0000762 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000763 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, nullptr, TU } };
Douglas Gregor796d76a2010-10-20 22:00:55 +0000764 return C;
765}
766
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000767const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000768 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000769 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000770}
771
Douglas Gregora93ab662010-09-10 00:22:18 +0000772CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000773 CXTranslationUnit TU) {
Douglas Gregora93ab662010-09-10 00:22:18 +0000774
775 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000776 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000777 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregora93ab662010-09-10 00:22:18 +0000778 return C;
779}
780
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000781std::pair<const LabelStmt *, SourceLocation>
Douglas Gregora93ab662010-09-10 00:22:18 +0000782cxcursor::getCursorLabelRef(CXCursor C) {
783 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000784 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000785 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora93ab662010-09-10 00:22:18 +0000786}
787
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000788CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremenek91554282010-11-16 08:15:36 +0000789 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000790 assert(E && TU && "Invalid arguments!");
791 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000792 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000793 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000794 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000795 { Storage.getOpaqueValue(), RawLoc, TU }
796 };
797 return C;
798}
799
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000800CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000801 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000802 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000803 assert(D && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000804 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000805 OverloadedDeclRefStorage Storage(D);
806 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000807 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000808 { Storage.getOpaqueValue(), RawLoc, TU }
809 };
810 return C;
811}
812
813CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
814 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000815 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000816 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000817 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000818 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
819 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000820 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000821 { Storage.getOpaqueValue(), RawLoc, TU }
822 };
823 return C;
824}
825
826std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
827cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
828 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000829 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
830 const_cast<void *>(C.data[0])),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000831 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000832}
833
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000834const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
835 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000836}
837
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000838const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000839 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
840}
841
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000842const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregoref6eb842010-01-16 15:44:18 +0000843 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor46d66142010-01-16 17:14:40 +0000844 Cursor.kind == CXCursor_ObjCProtocolRef ||
845 Cursor.kind == CXCursor_ObjCClassRef)
Craig Topper69186e72014-06-08 08:38:04 +0000846 return nullptr;
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000847
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000848 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000849}
850
Dmitri Gribenkoe4baea62013-01-26 18:08:08 +0000851const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
852 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremeneka5940822010-08-26 01:42:22 +0000853}
854
Dmitri Gribenkoa1691182013-01-26 18:12:08 +0000855const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
856 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8bb2ecf2011-06-29 22:20:07 +0000857}
858
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000859ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorfed36b12010-01-20 23:57:43 +0000860 return getCursorASTUnit(Cursor)->getASTContext();
861}
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000862
Douglas Gregorfed36b12010-01-20 23:57:43 +0000863ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko2c173b42013-01-11 19:28:44 +0000864 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidisfa469d02011-12-09 00:17:49 +0000865 if (!TU)
Craig Topper69186e72014-06-08 08:38:04 +0000866 return nullptr;
Dmitri Gribenkoc22ea1c2013-01-26 18:53:38 +0000867 return cxtu::getASTUnit(TU);
Ted Kremenek91554282010-11-16 08:15:36 +0000868}
869
870CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000871 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000872}
873
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000874void cxcursor::getOverriddenCursors(CXCursor cursor,
875 SmallVectorImpl<CXCursor> &overridden) {
876 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000877 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000878 if (!D)
879 return;
880
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000881 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000882 SmallVector<const NamedDecl *, 8> OverDecls;
883 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000884
Craig Topper2341c0d2013-07-04 03:08:24 +0000885 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000886 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko9c256e32013-01-14 00:46:27 +0000887 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000888 }
889}
890
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000891std::pair<int, SourceLocation>
892cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
893 if (cursor.kind == CXCursor_ObjCMessageExpr) {
894 if (cursor.xdata != -1)
895 return std::make_pair(cursor.xdata,
896 cast<ObjCMessageExpr>(getCursorExpr(cursor))
897 ->getSelectorLoc(cursor.xdata));
898 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
899 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
900 if (cursor.xdata != -1)
901 return std::make_pair(cursor.xdata,
902 cast<ObjCMethodDecl>(getCursorDecl(cursor))
903 ->getSelectorLoc(cursor.xdata));
904 }
905
906 return std::make_pair(-1, SourceLocation());
907}
908
909CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
910 CXCursor newCursor = cursor;
911
912 if (cursor.kind == CXCursor_ObjCMessageExpr) {
913 if (SelIdx == -1 ||
914 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
915 ->getNumSelectorLocs())
916 newCursor.xdata = -1;
917 else
918 newCursor.xdata = SelIdx;
919 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
920 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
921 if (SelIdx == -1 ||
922 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
923 ->getNumSelectorLocs())
924 newCursor.xdata = -1;
925 else
926 newCursor.xdata = SelIdx;
927 }
928
929 return newCursor;
930}
931
932CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
933 if (cursor.kind != CXCursor_CallExpr)
934 return cursor;
935
936 if (cursor.xdata == 0)
937 return cursor;
938
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000939 const Expr *E = getCursorExpr(cursor);
Craig Topper69186e72014-06-08 08:38:04 +0000940 TypeSourceInfo *Type = nullptr;
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000941 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000942 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
943 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000944 } else if (const CXXTemporaryObjectExpr *Tmp =
945 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000946 Type = Tmp->getTypeSourceInfo();
947 }
948
949 if (!Type)
950 return cursor;
951
952 CXTranslationUnit TU = getCursorTU(cursor);
953 QualType Ty = Type->getType();
954 TypeLoc TL = Type->getTypeLoc();
955 SourceLocation Loc = TL.getBeginLoc();
956
957 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
958 Ty = ElabT->getNamedType();
David Blaikie6adc78e2013-02-18 22:06:02 +0000959 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000960 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
961 }
962
963 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
964 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
965 if (const TagType *Tag = Ty->getAs<TagType>())
966 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
967 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
968 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
969
970 return cursor;
971}
972
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000973bool cxcursor::operator==(CXCursor X, CXCursor Y) {
974 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
975 X.data[2] == Y.data[2];
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000976}
Ted Kremenek818e5c12010-11-01 23:26:51 +0000977
978// FIXME: Remove once we can model DeclGroups and their appropriate ranges
979// properly in the ASTs.
980bool cxcursor::isFirstInDeclGroup(CXCursor C) {
981 assert(clang_isDeclaration(C.kind));
982 return ((uintptr_t) (C.data[1])) != 0;
983}
984
Ted Kremenekbbedd062010-12-08 23:43:14 +0000985//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000986// libclang CXCursor APIs
987//===----------------------------------------------------------------------===//
988
Argyrios Kyrtzidisa1bcb6a2011-09-27 04:14:36 +0000989extern "C" {
990
991int clang_Cursor_isNull(CXCursor cursor) {
992 return clang_equalCursors(cursor, clang_getNullCursor());
993}
994
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000995CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
996 return getCursorTU(cursor);
997}
998
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +0000999int clang_Cursor_getNumArguments(CXCursor C) {
1000 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001001 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001002 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1003 return MD->param_size();
1004 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1005 return FD->param_size();
1006 }
1007
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +00001008 if (clang_isExpression(C.kind)) {
1009 const Expr *E = cxcursor::getCursorExpr(C);
1010 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1011 return CE->getNumArgs();
1012 }
1013 }
1014
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001015 return -1;
1016}
1017
1018CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1019 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001020 const Decl *D = cxcursor::getCursorDecl(C);
1021 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001022 if (i < MD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001023 return cxcursor::MakeCXCursor(MD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001024 cxcursor::getCursorTU(C));
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001025 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001026 if (i < FD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001027 return cxcursor::MakeCXCursor(FD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001028 cxcursor::getCursorTU(C));
1029 }
1030 }
1031
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +00001032 if (clang_isExpression(C.kind)) {
1033 const Expr *E = cxcursor::getCursorExpr(C);
1034 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1035 if (i < CE->getNumArgs()) {
1036 return cxcursor::MakeCXCursor(CE->getArg(i),
1037 getCursorDecl(C),
1038 cxcursor::getCursorTU(C));
1039 }
1040 }
1041 }
1042
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001043 return clang_getNullCursor();
1044}
1045
Ted Kremenekc0b98662013-04-24 07:17:12 +00001046} // end: extern "C"
1047
1048//===----------------------------------------------------------------------===//
1049// CXCursorSet.
1050//===----------------------------------------------------------------------===//
1051
1052typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1053
1054static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1055 return (CXCursorSet) setImpl;
1056}
1057static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1058 return (CXCursorSet_Impl*) set;
1059}
1060namespace llvm {
1061template<> struct DenseMapInfo<CXCursor> {
1062public:
1063 static inline CXCursor getEmptyKey() {
1064 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1065 }
1066 static inline CXCursor getTombstoneKey() {
1067 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1068 }
1069 static inline unsigned getHashValue(const CXCursor &cursor) {
1070 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1071 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1072 }
1073 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1074 return x.kind == y.kind &&
1075 x.data[0] == y.data[0] &&
1076 x.data[1] == y.data[1];
1077 }
1078};
1079}
1080
1081extern "C" {
1082CXCursorSet clang_createCXCursorSet() {
1083 return packCXCursorSet(new CXCursorSet_Impl());
1084}
1085
1086void clang_disposeCXCursorSet(CXCursorSet set) {
1087 delete unpackCXCursorSet(set);
1088}
1089
1090unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1091 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1092 if (!setImpl)
1093 return 0;
Ted Kremenek29b28e82013-04-24 07:25:40 +00001094 return setImpl->find(cursor) != setImpl->end();
Ted Kremenekc0b98662013-04-24 07:17:12 +00001095}
1096
1097unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1098 // Do not insert invalid cursors into the set.
1099 if (cursor.kind >= CXCursor_FirstInvalid &&
1100 cursor.kind <= CXCursor_LastInvalid)
1101 return 1;
1102
1103 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1104 if (!setImpl)
1105 return 1;
1106 unsigned &entry = (*setImpl)[cursor];
1107 unsigned flag = entry == 0 ? 1 : 0;
1108 entry = 1;
1109 return flag;
1110}
1111
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001112CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1113 enum CXCursorKind kind = clang_getCursorKind(cursor);
1114 if (clang_isDeclaration(kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001115 const Decl *decl = getCursorDecl(cursor);
1116 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001117 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00001118 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001119 CodeCompletionString *String
1120 = Result.CreateCodeCompletionString(unit->getASTContext(),
1121 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001122 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001123 unit->getCodeCompletionTUInfo(),
1124 true);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001125 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001126 }
1127 }
1128 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001129 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001130 const IdentifierInfo *MacroInfo = definition->getName();
1131 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenko049a4ff2013-01-14 00:36:42 +00001132 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001133 CodeCompletionString *String
1134 = Result.CreateCodeCompletionString(unit->getASTContext(),
1135 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001136 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001137 unit->getCodeCompletionTUInfo(),
1138 false);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001139 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001140 }
Craig Topper69186e72014-06-08 08:38:04 +00001141 return nullptr;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001142}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001143} // end: extern C.
Ted Kremenekd77f6212012-04-30 19:06:49 +00001144
1145namespace {
1146 struct OverridenCursorsPool {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001147 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001148 std::vector<CursorVec*> AllCursors;
1149 std::vector<CursorVec*> AvailableCursors;
1150
1151 ~OverridenCursorsPool() {
1152 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1153 E = AllCursors.end(); I != E; ++I) {
1154 delete *I;
1155 }
1156 }
1157 };
1158}
1159
1160void *cxcursor::createOverridenCXCursorsPool() {
1161 return new OverridenCursorsPool();
1162}
1163
1164void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1165 delete static_cast<OverridenCursorsPool*>(pool);
1166}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001167
1168extern "C" {
Ted Kremenekd77f6212012-04-30 19:06:49 +00001169void clang_getOverriddenCursors(CXCursor cursor,
1170 CXCursor **overridden,
1171 unsigned *num_overridden) {
1172 if (overridden)
Craig Topper69186e72014-06-08 08:38:04 +00001173 *overridden = nullptr;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001174 if (num_overridden)
1175 *num_overridden = 0;
1176
1177 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1178
1179 if (!overridden || !num_overridden || !TU)
1180 return;
1181
1182 if (!clang_isDeclaration(cursor.kind))
1183 return;
1184
1185 OverridenCursorsPool &pool =
1186 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
Craig Topper69186e72014-06-08 08:38:04 +00001187
1188 OverridenCursorsPool::CursorVec *Vec = nullptr;
1189
Ted Kremenekd77f6212012-04-30 19:06:49 +00001190 if (!pool.AvailableCursors.empty()) {
1191 Vec = pool.AvailableCursors.back();
1192 pool.AvailableCursors.pop_back();
1193 }
1194 else {
1195 Vec = new OverridenCursorsPool::CursorVec();
1196 pool.AllCursors.push_back(Vec);
1197 }
1198
1199 // Clear out the vector, but don't free the memory contents. This
1200 // reduces malloc() traffic.
1201 Vec->clear();
1202
1203 // Use the first entry to contain a back reference to the vector.
1204 // This is a complete hack.
1205 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1206 backRefCursor.data[0] = Vec;
1207 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1208 Vec->push_back(backRefCursor);
1209
1210 // Get the overriden cursors.
1211 cxcursor::getOverriddenCursors(cursor, *Vec);
1212
1213 // Did we get any overriden cursors? If not, return Vec to the pool
1214 // of available cursor vectors.
1215 if (Vec->size() == 1) {
1216 pool.AvailableCursors.push_back(Vec);
1217 return;
1218 }
1219
1220 // Now tell the caller about the overriden cursors.
1221 assert(Vec->size() > 1);
1222 *overridden = &((*Vec)[1]);
1223 *num_overridden = Vec->size() - 1;
1224}
1225
1226void clang_disposeOverriddenCursors(CXCursor *overridden) {
1227 if (!overridden)
1228 return;
1229
1230 // Use pointer arithmetic to get back the first faux entry
1231 // which has a back-reference to the TU and the vector.
1232 --overridden;
1233 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001234 static_cast<OverridenCursorsPool::CursorVec *>(
1235 const_cast<void *>(overridden->data[0]));
Ted Kremenekd77f6212012-04-30 19:06:49 +00001236 CXTranslationUnit TU = getCursorTU(*overridden);
1237
1238 assert(Vec && TU);
1239
1240 OverridenCursorsPool &pool =
1241 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1242
1243 pool.AvailableCursors.push_back(Vec);
1244}
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001245
1246int clang_Cursor_isDynamicCall(CXCursor C) {
Craig Topper69186e72014-06-08 08:38:04 +00001247 const Expr *E = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001248 if (clang_isExpression(C.kind))
1249 E = getCursorExpr(C);
1250 if (!E)
1251 return 0;
1252
1253 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1254 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1255
Craig Topper69186e72014-06-08 08:38:04 +00001256 const MemberExpr *ME = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001257 if (isa<MemberExpr>(E))
1258 ME = cast<MemberExpr>(E);
1259 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1260 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1261
1262 if (ME) {
1263 if (const CXXMethodDecl *
1264 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1265 return MD->isVirtual() && !ME->hasQualifier();
1266 }
1267
1268 return 0;
1269}
1270
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001271CXType clang_Cursor_getReceiverType(CXCursor C) {
1272 CXTranslationUnit TU = cxcursor::getCursorTU(C);
Craig Topper69186e72014-06-08 08:38:04 +00001273 const Expr *E = nullptr;
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001274 if (clang_isExpression(C.kind))
1275 E = getCursorExpr(C);
1276
1277 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1278 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1279
1280 return cxtype::MakeCXType(QualType(), TU);
1281}
1282
Ted Kremenekbbedd062010-12-08 23:43:14 +00001283} // end: extern "C"