blob: e50a156a4011b205872267092a3931c08fc24ae5 [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;
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000550 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000551
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000552 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000553 return C;
554}
555
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000556CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000557 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000558 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000559 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000560 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000561 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000562 return C;
563}
564
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000565std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000566cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
567 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000568 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000569 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000570}
571
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000572CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000573 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000574 CXTranslationUnit TU) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000575 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000576 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000577 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregoref6eb842010-01-16 15:44:18 +0000578 return C;
579}
580
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000581std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregoref6eb842010-01-16 15:44:18 +0000582cxcursor::getCursorObjCProtocolRef(CXCursor C) {
583 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000584 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000585 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregoref6eb842010-01-16 15:44:18 +0000586}
587
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000588CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000589 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000590 CXTranslationUnit TU) {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000591 // 'Class' can be null for invalid code.
592 if (!Class)
593 return MakeCXCursorInvalid(CXCursor_InvalidCode);
594 assert(TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000595 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000596 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor46d66142010-01-16 17:14:40 +0000597 return C;
598}
599
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000600std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor46d66142010-01-16 17:14:40 +0000601cxcursor::getCursorObjCClassRef(CXCursor C) {
602 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000603 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000604 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor46d66142010-01-16 17:14:40 +0000605}
606
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000607CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000608 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000609 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000610 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000611 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor93f89952010-01-21 16:28:34 +0000612 return C;
613}
614
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000615std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor93f89952010-01-21 16:28:34 +0000616cxcursor::getCursorTypeRef(CXCursor C) {
617 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000618 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000619 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor93f89952010-01-21 16:28:34 +0000620}
621
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000622CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremenek91554282010-11-16 08:15:36 +0000623 SourceLocation Loc,
624 CXTranslationUnit TU) {
Douglas Gregora23e8f72010-08-31 20:37:03 +0000625 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000626 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000627 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregora23e8f72010-08-31 20:37:03 +0000628 return C;
629}
630
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000631std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregora23e8f72010-08-31 20:37:03 +0000632cxcursor::getCursorTemplateRef(CXCursor C) {
633 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000634 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000635 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora23e8f72010-08-31 20:37:03 +0000636}
637
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000638CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
639 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000640 CXTranslationUnit TU) {
Douglas Gregora89314e2010-08-31 23:48:11 +0000641
642 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
643 "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000644 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000645 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregora89314e2010-08-31 23:48:11 +0000646 return C;
647}
648
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000649std::pair<const NamedDecl *, SourceLocation>
Douglas Gregora89314e2010-08-31 23:48:11 +0000650cxcursor::getCursorNamespaceRef(CXCursor C) {
651 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000652 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000653 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora89314e2010-08-31 23:48:11 +0000654}
655
Douglas Gregor30093832012-02-15 00:54:55 +0000656CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
657 CXTranslationUnit TU) {
658
659 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000660 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000661 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor30093832012-02-15 00:54:55 +0000662 return C;
663}
664
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000665std::pair<const VarDecl *, SourceLocation>
Douglas Gregor30093832012-02-15 00:54:55 +0000666cxcursor::getCursorVariableRef(CXCursor C) {
667 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000668 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000669 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor30093832012-02-15 00:54:55 +0000670}
671
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000672CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000673 CXTranslationUnit TU) {
Douglas Gregorf3af3112010-09-09 21:42:20 +0000674
675 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000676 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000677 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregorf3af3112010-09-09 21:42:20 +0000678 return C;
679}
680
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000681std::pair<const FieldDecl *, SourceLocation>
Douglas Gregorf3af3112010-09-09 21:42:20 +0000682cxcursor::getCursorMemberRef(CXCursor C) {
683 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000684 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000685 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregorf3af3112010-09-09 21:42:20 +0000686}
687
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000688CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremenek91554282010-11-16 08:15:36 +0000689 CXTranslationUnit TU){
Craig Topper69186e72014-06-08 08:38:04 +0000690 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, nullptr, TU } };
Ted Kremenekae9e2212010-08-27 21:34:58 +0000691 return C;
692}
693
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000694const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenekae9e2212010-08-27 21:34:58 +0000695 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000696 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenekae9e2212010-08-27 21:34:58 +0000697}
698
Douglas Gregor92a524f2010-03-18 00:42:48 +0000699CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremenek91554282010-11-16 08:15:36 +0000700 CXTranslationUnit TU) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000701 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000702 { Range.getBegin().getPtrEncoding(),
703 Range.getEnd().getPtrEncoding(),
Douglas Gregor92a524f2010-03-18 00:42:48 +0000704 TU }
705 };
706 return C;
707}
708
709SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
710 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000711 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
712 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +0000713 ASTUnit *TU = getCursorASTUnit(C);
714 return TU->mapRangeFromPreamble(Range);
Douglas Gregor92a524f2010-03-18 00:42:48 +0000715}
716
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000717CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremenek91554282010-11-16 08:15:36 +0000718 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000719 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, nullptr, TU } };
Douglas Gregor06d6d322010-03-18 18:04:21 +0000720 return C;
721}
722
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000723const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor06d6d322010-03-18 18:04:21 +0000724 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000725 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor06d6d322010-03-18 18:04:21 +0000726}
727
Chandler Carrutha88a22182011-07-14 08:20:46 +0000728CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
729 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000730 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, nullptr, TU } };
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000731 return C;
732}
733
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000734CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
735 SourceLocation Loc,
736 CXTranslationUnit TU) {
737 assert(Loc.isValid());
738 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
739 return C;
740}
741
742const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
743 if (isPseudo())
744 return getAsMacroDefinition()->getName();
745 return getAsMacroExpansion()->getName();
746}
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000747const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000748 if (isPseudo())
749 return getAsMacroDefinition();
750 return getAsMacroExpansion()->getDefinition();
751}
752SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
753 if (isPseudo())
754 return getPseudoLoc();
755 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000756}
757
Douglas Gregor796d76a2010-10-20 22:00:55 +0000758CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremenek91554282010-11-16 08:15:36 +0000759 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000760 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, nullptr, TU } };
Douglas Gregor796d76a2010-10-20 22:00:55 +0000761 return C;
762}
763
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000764const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000765 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000766 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000767}
768
Douglas Gregora93ab662010-09-10 00:22:18 +0000769CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000770 CXTranslationUnit TU) {
Douglas Gregora93ab662010-09-10 00:22:18 +0000771
772 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000773 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000774 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregora93ab662010-09-10 00:22:18 +0000775 return C;
776}
777
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000778std::pair<const LabelStmt *, SourceLocation>
Douglas Gregora93ab662010-09-10 00:22:18 +0000779cxcursor::getCursorLabelRef(CXCursor C) {
780 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000781 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000782 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora93ab662010-09-10 00:22:18 +0000783}
784
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000785CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremenek91554282010-11-16 08:15:36 +0000786 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000787 assert(E && TU && "Invalid arguments!");
788 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000789 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000790 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000791 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000792 { Storage.getOpaqueValue(), RawLoc, TU }
793 };
794 return C;
795}
796
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000797CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000798 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000799 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000800 assert(D && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000801 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000802 OverloadedDeclRefStorage Storage(D);
803 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000804 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000805 { Storage.getOpaqueValue(), RawLoc, TU }
806 };
807 return C;
808}
809
810CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
811 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000812 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000813 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000814 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000815 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
816 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000817 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000818 { Storage.getOpaqueValue(), RawLoc, TU }
819 };
820 return C;
821}
822
823std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
824cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
825 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000826 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
827 const_cast<void *>(C.data[0])),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000828 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000829}
830
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000831const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
832 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000833}
834
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000835const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000836 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
837}
838
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000839const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregoref6eb842010-01-16 15:44:18 +0000840 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor46d66142010-01-16 17:14:40 +0000841 Cursor.kind == CXCursor_ObjCProtocolRef ||
842 Cursor.kind == CXCursor_ObjCClassRef)
Craig Topper69186e72014-06-08 08:38:04 +0000843 return nullptr;
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000844
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000845 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000846}
847
Dmitri Gribenkoe4baea62013-01-26 18:08:08 +0000848const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
849 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremeneka5940822010-08-26 01:42:22 +0000850}
851
Dmitri Gribenkoa1691182013-01-26 18:12:08 +0000852const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
853 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8bb2ecf2011-06-29 22:20:07 +0000854}
855
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000856ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorfed36b12010-01-20 23:57:43 +0000857 return getCursorASTUnit(Cursor)->getASTContext();
858}
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000859
Douglas Gregorfed36b12010-01-20 23:57:43 +0000860ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko2c173b42013-01-11 19:28:44 +0000861 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidisfa469d02011-12-09 00:17:49 +0000862 if (!TU)
Craig Topper69186e72014-06-08 08:38:04 +0000863 return nullptr;
Dmitri Gribenkoc22ea1c2013-01-26 18:53:38 +0000864 return cxtu::getASTUnit(TU);
Ted Kremenek91554282010-11-16 08:15:36 +0000865}
866
867CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000868 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000869}
870
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000871void cxcursor::getOverriddenCursors(CXCursor cursor,
872 SmallVectorImpl<CXCursor> &overridden) {
873 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000874 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000875 if (!D)
876 return;
877
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000878 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000879 SmallVector<const NamedDecl *, 8> OverDecls;
880 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000881
Craig Topper2341c0d2013-07-04 03:08:24 +0000882 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000883 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko9c256e32013-01-14 00:46:27 +0000884 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000885 }
886}
887
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000888std::pair<int, SourceLocation>
889cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
890 if (cursor.kind == CXCursor_ObjCMessageExpr) {
891 if (cursor.xdata != -1)
892 return std::make_pair(cursor.xdata,
893 cast<ObjCMessageExpr>(getCursorExpr(cursor))
894 ->getSelectorLoc(cursor.xdata));
895 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
896 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
897 if (cursor.xdata != -1)
898 return std::make_pair(cursor.xdata,
899 cast<ObjCMethodDecl>(getCursorDecl(cursor))
900 ->getSelectorLoc(cursor.xdata));
901 }
902
903 return std::make_pair(-1, SourceLocation());
904}
905
906CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
907 CXCursor newCursor = cursor;
908
909 if (cursor.kind == CXCursor_ObjCMessageExpr) {
910 if (SelIdx == -1 ||
911 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
912 ->getNumSelectorLocs())
913 newCursor.xdata = -1;
914 else
915 newCursor.xdata = SelIdx;
916 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
917 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
918 if (SelIdx == -1 ||
919 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
920 ->getNumSelectorLocs())
921 newCursor.xdata = -1;
922 else
923 newCursor.xdata = SelIdx;
924 }
925
926 return newCursor;
927}
928
929CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
930 if (cursor.kind != CXCursor_CallExpr)
931 return cursor;
932
933 if (cursor.xdata == 0)
934 return cursor;
935
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000936 const Expr *E = getCursorExpr(cursor);
Craig Topper69186e72014-06-08 08:38:04 +0000937 TypeSourceInfo *Type = nullptr;
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000938 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000939 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
940 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000941 } else if (const CXXTemporaryObjectExpr *Tmp =
942 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000943 Type = Tmp->getTypeSourceInfo();
944 }
945
946 if (!Type)
947 return cursor;
948
949 CXTranslationUnit TU = getCursorTU(cursor);
950 QualType Ty = Type->getType();
951 TypeLoc TL = Type->getTypeLoc();
952 SourceLocation Loc = TL.getBeginLoc();
953
954 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
955 Ty = ElabT->getNamedType();
David Blaikie6adc78e2013-02-18 22:06:02 +0000956 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000957 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
958 }
959
960 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
961 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
962 if (const TagType *Tag = Ty->getAs<TagType>())
963 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
964 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
965 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
966
967 return cursor;
968}
969
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000970bool cxcursor::operator==(CXCursor X, CXCursor Y) {
971 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
972 X.data[2] == Y.data[2];
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000973}
Ted Kremenek818e5c12010-11-01 23:26:51 +0000974
975// FIXME: Remove once we can model DeclGroups and their appropriate ranges
976// properly in the ASTs.
977bool cxcursor::isFirstInDeclGroup(CXCursor C) {
978 assert(clang_isDeclaration(C.kind));
979 return ((uintptr_t) (C.data[1])) != 0;
980}
981
Ted Kremenekbbedd062010-12-08 23:43:14 +0000982//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000983// libclang CXCursor APIs
984//===----------------------------------------------------------------------===//
985
Argyrios Kyrtzidisa1bcb6a2011-09-27 04:14:36 +0000986extern "C" {
987
988int clang_Cursor_isNull(CXCursor cursor) {
989 return clang_equalCursors(cursor, clang_getNullCursor());
990}
991
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000992CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
993 return getCursorTU(cursor);
994}
995
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +0000996int clang_Cursor_getNumArguments(CXCursor C) {
997 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000998 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +0000999 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1000 return MD->param_size();
1001 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1002 return FD->param_size();
1003 }
1004
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +00001005 if (clang_isExpression(C.kind)) {
1006 const Expr *E = cxcursor::getCursorExpr(C);
1007 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1008 return CE->getNumArgs();
1009 }
1010 }
1011
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001012 return -1;
1013}
1014
1015CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1016 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001017 const Decl *D = cxcursor::getCursorDecl(C);
1018 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001019 if (i < MD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001020 return cxcursor::MakeCXCursor(MD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001021 cxcursor::getCursorTU(C));
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001022 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001023 if (i < FD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001024 return cxcursor::MakeCXCursor(FD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001025 cxcursor::getCursorTU(C));
1026 }
1027 }
1028
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +00001029 if (clang_isExpression(C.kind)) {
1030 const Expr *E = cxcursor::getCursorExpr(C);
1031 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1032 if (i < CE->getNumArgs()) {
1033 return cxcursor::MakeCXCursor(CE->getArg(i),
1034 getCursorDecl(C),
1035 cxcursor::getCursorTU(C));
1036 }
1037 }
1038 }
1039
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001040 return clang_getNullCursor();
1041}
1042
Ted Kremenekc0b98662013-04-24 07:17:12 +00001043} // end: extern "C"
1044
1045//===----------------------------------------------------------------------===//
1046// CXCursorSet.
1047//===----------------------------------------------------------------------===//
1048
1049typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1050
1051static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1052 return (CXCursorSet) setImpl;
1053}
1054static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1055 return (CXCursorSet_Impl*) set;
1056}
1057namespace llvm {
1058template<> struct DenseMapInfo<CXCursor> {
1059public:
1060 static inline CXCursor getEmptyKey() {
1061 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1062 }
1063 static inline CXCursor getTombstoneKey() {
1064 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1065 }
1066 static inline unsigned getHashValue(const CXCursor &cursor) {
1067 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1068 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1069 }
1070 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1071 return x.kind == y.kind &&
1072 x.data[0] == y.data[0] &&
1073 x.data[1] == y.data[1];
1074 }
1075};
1076}
1077
1078extern "C" {
1079CXCursorSet clang_createCXCursorSet() {
1080 return packCXCursorSet(new CXCursorSet_Impl());
1081}
1082
1083void clang_disposeCXCursorSet(CXCursorSet set) {
1084 delete unpackCXCursorSet(set);
1085}
1086
1087unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1088 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1089 if (!setImpl)
1090 return 0;
Ted Kremenek29b28e82013-04-24 07:25:40 +00001091 return setImpl->find(cursor) != setImpl->end();
Ted Kremenekc0b98662013-04-24 07:17:12 +00001092}
1093
1094unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1095 // Do not insert invalid cursors into the set.
1096 if (cursor.kind >= CXCursor_FirstInvalid &&
1097 cursor.kind <= CXCursor_LastInvalid)
1098 return 1;
1099
1100 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1101 if (!setImpl)
1102 return 1;
1103 unsigned &entry = (*setImpl)[cursor];
1104 unsigned flag = entry == 0 ? 1 : 0;
1105 entry = 1;
1106 return flag;
1107}
1108
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001109CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1110 enum CXCursorKind kind = clang_getCursorKind(cursor);
1111 if (clang_isDeclaration(kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001112 const Decl *decl = getCursorDecl(cursor);
1113 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001114 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00001115 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001116 CodeCompletionString *String
1117 = Result.CreateCodeCompletionString(unit->getASTContext(),
1118 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001119 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001120 unit->getCodeCompletionTUInfo(),
1121 true);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001122 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001123 }
1124 }
1125 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001126 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001127 const IdentifierInfo *MacroInfo = definition->getName();
1128 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenko049a4ff2013-01-14 00:36:42 +00001129 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001130 CodeCompletionString *String
1131 = Result.CreateCodeCompletionString(unit->getASTContext(),
1132 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001133 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001134 unit->getCodeCompletionTUInfo(),
1135 false);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001136 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001137 }
Craig Topper69186e72014-06-08 08:38:04 +00001138 return nullptr;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001139}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001140} // end: extern C.
Ted Kremenekd77f6212012-04-30 19:06:49 +00001141
1142namespace {
1143 struct OverridenCursorsPool {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001144 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001145 std::vector<CursorVec*> AllCursors;
1146 std::vector<CursorVec*> AvailableCursors;
1147
1148 ~OverridenCursorsPool() {
1149 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1150 E = AllCursors.end(); I != E; ++I) {
1151 delete *I;
1152 }
1153 }
1154 };
1155}
1156
1157void *cxcursor::createOverridenCXCursorsPool() {
1158 return new OverridenCursorsPool();
1159}
1160
1161void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1162 delete static_cast<OverridenCursorsPool*>(pool);
1163}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001164
1165extern "C" {
Ted Kremenekd77f6212012-04-30 19:06:49 +00001166void clang_getOverriddenCursors(CXCursor cursor,
1167 CXCursor **overridden,
1168 unsigned *num_overridden) {
1169 if (overridden)
Craig Topper69186e72014-06-08 08:38:04 +00001170 *overridden = nullptr;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001171 if (num_overridden)
1172 *num_overridden = 0;
1173
1174 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1175
1176 if (!overridden || !num_overridden || !TU)
1177 return;
1178
1179 if (!clang_isDeclaration(cursor.kind))
1180 return;
1181
1182 OverridenCursorsPool &pool =
1183 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
Craig Topper69186e72014-06-08 08:38:04 +00001184
1185 OverridenCursorsPool::CursorVec *Vec = nullptr;
1186
Ted Kremenekd77f6212012-04-30 19:06:49 +00001187 if (!pool.AvailableCursors.empty()) {
1188 Vec = pool.AvailableCursors.back();
1189 pool.AvailableCursors.pop_back();
1190 }
1191 else {
1192 Vec = new OverridenCursorsPool::CursorVec();
1193 pool.AllCursors.push_back(Vec);
1194 }
1195
1196 // Clear out the vector, but don't free the memory contents. This
1197 // reduces malloc() traffic.
1198 Vec->clear();
1199
1200 // Use the first entry to contain a back reference to the vector.
1201 // This is a complete hack.
1202 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1203 backRefCursor.data[0] = Vec;
1204 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1205 Vec->push_back(backRefCursor);
1206
1207 // Get the overriden cursors.
1208 cxcursor::getOverriddenCursors(cursor, *Vec);
1209
1210 // Did we get any overriden cursors? If not, return Vec to the pool
1211 // of available cursor vectors.
1212 if (Vec->size() == 1) {
1213 pool.AvailableCursors.push_back(Vec);
1214 return;
1215 }
1216
1217 // Now tell the caller about the overriden cursors.
1218 assert(Vec->size() > 1);
1219 *overridden = &((*Vec)[1]);
1220 *num_overridden = Vec->size() - 1;
1221}
1222
1223void clang_disposeOverriddenCursors(CXCursor *overridden) {
1224 if (!overridden)
1225 return;
1226
1227 // Use pointer arithmetic to get back the first faux entry
1228 // which has a back-reference to the TU and the vector.
1229 --overridden;
1230 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001231 static_cast<OverridenCursorsPool::CursorVec *>(
1232 const_cast<void *>(overridden->data[0]));
Ted Kremenekd77f6212012-04-30 19:06:49 +00001233 CXTranslationUnit TU = getCursorTU(*overridden);
1234
1235 assert(Vec && TU);
1236
1237 OverridenCursorsPool &pool =
1238 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1239
1240 pool.AvailableCursors.push_back(Vec);
1241}
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001242
1243int clang_Cursor_isDynamicCall(CXCursor C) {
Craig Topper69186e72014-06-08 08:38:04 +00001244 const Expr *E = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001245 if (clang_isExpression(C.kind))
1246 E = getCursorExpr(C);
1247 if (!E)
1248 return 0;
1249
1250 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1251 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1252
Craig Topper69186e72014-06-08 08:38:04 +00001253 const MemberExpr *ME = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001254 if (isa<MemberExpr>(E))
1255 ME = cast<MemberExpr>(E);
1256 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1257 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1258
1259 if (ME) {
1260 if (const CXXMethodDecl *
1261 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1262 return MD->isVirtual() && !ME->hasQualifier();
1263 }
1264
1265 return 0;
1266}
1267
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001268CXType clang_Cursor_getReceiverType(CXCursor C) {
1269 CXTranslationUnit TU = cxcursor::getCursorTU(C);
Craig Topper69186e72014-06-08 08:38:04 +00001270 const Expr *E = nullptr;
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001271 if (clang_isExpression(C.kind))
1272 E = getCursorExpr(C);
1273
1274 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1275 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1276
1277 return cxtype::MakeCXType(QualType(), TU);
1278}
1279
Ted Kremenekbbedd062010-12-08 23:43:14 +00001280} // end: extern "C"