blob: 6875f5abdc40a249590866783ec49ad26431ac56 [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;
Alexey Bataev4acb8592014-07-07 13:01:15 +0000538 case Stmt::OMPParallelForDirectiveClass:
539 K = CXCursor_OMPParallelForDirective;
540 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +0000541 case Stmt::OMPParallelSectionsDirectiveClass:
542 K = CXCursor_OMPParallelSectionsDirective;
543 break;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000544 case Stmt::OMPTaskDirectiveClass:
545 K = CXCursor_OMPTaskDirective;
546 break;
Douglas Gregordeb4a2be2011-10-25 01:33:02 +0000547 }
Alexey Bataev1b59ab52014-02-27 08:29:12 +0000548
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000549 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor8f40bbee2010-01-19 23:20:36 +0000550 return C;
551}
552
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000553CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000554 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000555 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000556 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000557 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000558 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000559 return C;
560}
561
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000562std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000563cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
564 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000565 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000566 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000567}
568
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000569CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000570 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000571 CXTranslationUnit TU) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000572 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000573 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000574 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregoref6eb842010-01-16 15:44:18 +0000575 return C;
576}
577
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000578std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregoref6eb842010-01-16 15:44:18 +0000579cxcursor::getCursorObjCProtocolRef(CXCursor C) {
580 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000581 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000582 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregoref6eb842010-01-16 15:44:18 +0000583}
584
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000585CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorfed36b12010-01-20 23:57:43 +0000586 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000587 CXTranslationUnit TU) {
Ted Kremeneke184ac52010-03-19 20:39:03 +0000588 // 'Class' can be null for invalid code.
589 if (!Class)
590 return MakeCXCursorInvalid(CXCursor_InvalidCode);
591 assert(TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000592 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000593 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor46d66142010-01-16 17:14:40 +0000594 return C;
595}
596
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000597std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor46d66142010-01-16 17:14:40 +0000598cxcursor::getCursorObjCClassRef(CXCursor C) {
599 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000600 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000601 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor46d66142010-01-16 17:14:40 +0000602}
603
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +0000604CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000605 CXTranslationUnit TU) {
Daniel Dunbar2def7eb2010-01-25 00:40:30 +0000606 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000607 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000608 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor93f89952010-01-21 16:28:34 +0000609 return C;
610}
611
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000612std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor93f89952010-01-21 16:28:34 +0000613cxcursor::getCursorTypeRef(CXCursor C) {
614 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000615 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000616 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor93f89952010-01-21 16:28:34 +0000617}
618
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000619CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremenek91554282010-11-16 08:15:36 +0000620 SourceLocation Loc,
621 CXTranslationUnit TU) {
Douglas Gregora23e8f72010-08-31 20:37:03 +0000622 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000623 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000624 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregora23e8f72010-08-31 20:37:03 +0000625 return C;
626}
627
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000628std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregora23e8f72010-08-31 20:37:03 +0000629cxcursor::getCursorTemplateRef(CXCursor C) {
630 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000631 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000632 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora23e8f72010-08-31 20:37:03 +0000633}
634
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000635CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
636 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000637 CXTranslationUnit TU) {
Douglas Gregora89314e2010-08-31 23:48:11 +0000638
639 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
640 "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000641 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000642 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregora89314e2010-08-31 23:48:11 +0000643 return C;
644}
645
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000646std::pair<const NamedDecl *, SourceLocation>
Douglas Gregora89314e2010-08-31 23:48:11 +0000647cxcursor::getCursorNamespaceRef(CXCursor C) {
648 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000649 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000650 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora89314e2010-08-31 23:48:11 +0000651}
652
Douglas Gregor30093832012-02-15 00:54:55 +0000653CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
654 CXTranslationUnit TU) {
655
656 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000657 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000658 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor30093832012-02-15 00:54:55 +0000659 return C;
660}
661
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000662std::pair<const VarDecl *, SourceLocation>
Douglas Gregor30093832012-02-15 00:54:55 +0000663cxcursor::getCursorVariableRef(CXCursor C) {
664 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000665 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000666 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor30093832012-02-15 00:54:55 +0000667}
668
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +0000669CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000670 CXTranslationUnit TU) {
Douglas Gregorf3af3112010-09-09 21:42:20 +0000671
672 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000673 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko7a7284d2013-01-11 21:06:06 +0000674 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregorf3af3112010-09-09 21:42:20 +0000675 return C;
676}
677
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000678std::pair<const FieldDecl *, SourceLocation>
Douglas Gregorf3af3112010-09-09 21:42:20 +0000679cxcursor::getCursorMemberRef(CXCursor C) {
680 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000681 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000682 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregorf3af3112010-09-09 21:42:20 +0000683}
684
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +0000685CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremenek91554282010-11-16 08:15:36 +0000686 CXTranslationUnit TU){
Craig Topper69186e72014-06-08 08:38:04 +0000687 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, nullptr, TU } };
Ted Kremenekae9e2212010-08-27 21:34:58 +0000688 return C;
689}
690
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000691const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenekae9e2212010-08-27 21:34:58 +0000692 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000693 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenekae9e2212010-08-27 21:34:58 +0000694}
695
Douglas Gregor92a524f2010-03-18 00:42:48 +0000696CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremenek91554282010-11-16 08:15:36 +0000697 CXTranslationUnit TU) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000698 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000699 { Range.getBegin().getPtrEncoding(),
700 Range.getEnd().getPtrEncoding(),
Douglas Gregor92a524f2010-03-18 00:42:48 +0000701 TU }
702 };
703 return C;
704}
705
706SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
707 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000708 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
709 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +0000710 ASTUnit *TU = getCursorASTUnit(C);
711 return TU->mapRangeFromPreamble(Range);
Douglas Gregor92a524f2010-03-18 00:42:48 +0000712}
713
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000714CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremenek91554282010-11-16 08:15:36 +0000715 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000716 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, nullptr, TU } };
Douglas Gregor06d6d322010-03-18 18:04:21 +0000717 return C;
718}
719
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000720const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor06d6d322010-03-18 18:04:21 +0000721 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000722 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor06d6d322010-03-18 18:04:21 +0000723}
724
Chandler Carrutha88a22182011-07-14 08:20:46 +0000725CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
726 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000727 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, nullptr, TU } };
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000728 return C;
729}
730
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000731CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
732 SourceLocation Loc,
733 CXTranslationUnit TU) {
734 assert(Loc.isValid());
735 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
736 return C;
737}
738
739const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
740 if (isPseudo())
741 return getAsMacroDefinition()->getName();
742 return getAsMacroExpansion()->getName();
743}
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000744const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis579825a2013-01-07 19:16:25 +0000745 if (isPseudo())
746 return getAsMacroDefinition();
747 return getAsMacroExpansion()->getDefinition();
748}
749SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
750 if (isPseudo())
751 return getPseudoLoc();
752 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor02ded2a2010-03-18 15:23:44 +0000753}
754
Douglas Gregor796d76a2010-10-20 22:00:55 +0000755CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremenek91554282010-11-16 08:15:36 +0000756 CXTranslationUnit TU) {
Craig Topper69186e72014-06-08 08:38:04 +0000757 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, nullptr, TU } };
Douglas Gregor796d76a2010-10-20 22:00:55 +0000758 return C;
759}
760
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000761const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregor796d76a2010-10-20 22:00:55 +0000762 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000763 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000764}
765
Douglas Gregora93ab662010-09-10 00:22:18 +0000766CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000767 CXTranslationUnit TU) {
Douglas Gregora93ab662010-09-10 00:22:18 +0000768
769 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000770 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000771 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregora93ab662010-09-10 00:22:18 +0000772 return C;
773}
774
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000775std::pair<const LabelStmt *, SourceLocation>
Douglas Gregora93ab662010-09-10 00:22:18 +0000776cxcursor::getCursorLabelRef(CXCursor C) {
777 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000778 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000779 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora93ab662010-09-10 00:22:18 +0000780}
781
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000782CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremenek91554282010-11-16 08:15:36 +0000783 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000784 assert(E && TU && "Invalid arguments!");
785 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000786 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000787 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000788 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000789 { Storage.getOpaqueValue(), RawLoc, TU }
790 };
791 return C;
792}
793
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000794CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000795 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000796 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000797 assert(D && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000798 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000799 OverloadedDeclRefStorage Storage(D);
800 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000801 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000802 { Storage.getOpaqueValue(), RawLoc, TU }
803 };
804 return C;
805}
806
807CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
808 SourceLocation Loc,
Ted Kremenek91554282010-11-16 08:15:36 +0000809 CXTranslationUnit TU) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000810 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenko19b79c82013-02-16 01:07:48 +0000811 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000812 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
813 CXCursor C = {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000814 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000815 { Storage.getOpaqueValue(), RawLoc, TU }
816 };
817 return C;
818}
819
820std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
821cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
822 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000823 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
824 const_cast<void *>(C.data[0])),
Dmitri Gribenko6b8fca12013-02-14 20:07:36 +0000825 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000826}
827
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000828const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
829 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000830}
831
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000832const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000833 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
834}
835
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000836const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregoref6eb842010-01-16 15:44:18 +0000837 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor46d66142010-01-16 17:14:40 +0000838 Cursor.kind == CXCursor_ObjCProtocolRef ||
839 Cursor.kind == CXCursor_ObjCClassRef)
Craig Topper69186e72014-06-08 08:38:04 +0000840 return nullptr;
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000841
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000842 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000843}
844
Dmitri Gribenkoe4baea62013-01-26 18:08:08 +0000845const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
846 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremeneka5940822010-08-26 01:42:22 +0000847}
848
Dmitri Gribenkoa1691182013-01-26 18:12:08 +0000849const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
850 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8bb2ecf2011-06-29 22:20:07 +0000851}
852
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000853ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorfed36b12010-01-20 23:57:43 +0000854 return getCursorASTUnit(Cursor)->getASTContext();
855}
Douglas Gregor7ecd0202010-01-18 23:41:10 +0000856
Douglas Gregorfed36b12010-01-20 23:57:43 +0000857ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko2c173b42013-01-11 19:28:44 +0000858 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidisfa469d02011-12-09 00:17:49 +0000859 if (!TU)
Craig Topper69186e72014-06-08 08:38:04 +0000860 return nullptr;
Dmitri Gribenkoc22ea1c2013-01-26 18:53:38 +0000861 return cxtu::getASTUnit(TU);
Ted Kremenek91554282010-11-16 08:15:36 +0000862}
863
864CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +0000865 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000866}
867
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000868void cxcursor::getOverriddenCursors(CXCursor cursor,
869 SmallVectorImpl<CXCursor> &overridden) {
870 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000871 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000872 if (!D)
873 return;
874
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000875 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000876 SmallVector<const NamedDecl *, 8> OverDecls;
877 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000878
Craig Topper2341c0d2013-07-04 03:08:24 +0000879 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidisb9556e62012-10-09 01:23:50 +0000880 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko9c256e32013-01-14 00:46:27 +0000881 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +0000882 }
883}
884
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000885std::pair<int, SourceLocation>
886cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
887 if (cursor.kind == CXCursor_ObjCMessageExpr) {
888 if (cursor.xdata != -1)
889 return std::make_pair(cursor.xdata,
890 cast<ObjCMessageExpr>(getCursorExpr(cursor))
891 ->getSelectorLoc(cursor.xdata));
892 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
893 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
894 if (cursor.xdata != -1)
895 return std::make_pair(cursor.xdata,
896 cast<ObjCMethodDecl>(getCursorDecl(cursor))
897 ->getSelectorLoc(cursor.xdata));
898 }
899
900 return std::make_pair(-1, SourceLocation());
901}
902
903CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
904 CXCursor newCursor = cursor;
905
906 if (cursor.kind == CXCursor_ObjCMessageExpr) {
907 if (SelIdx == -1 ||
908 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
909 ->getNumSelectorLocs())
910 newCursor.xdata = -1;
911 else
912 newCursor.xdata = SelIdx;
913 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
914 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
915 if (SelIdx == -1 ||
916 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
917 ->getNumSelectorLocs())
918 newCursor.xdata = -1;
919 else
920 newCursor.xdata = SelIdx;
921 }
922
923 return newCursor;
924}
925
926CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
927 if (cursor.kind != CXCursor_CallExpr)
928 return cursor;
929
930 if (cursor.xdata == 0)
931 return cursor;
932
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000933 const Expr *E = getCursorExpr(cursor);
Craig Topper69186e72014-06-08 08:38:04 +0000934 TypeSourceInfo *Type = nullptr;
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000935 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000936 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
937 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoe8354062013-01-26 15:29:08 +0000938 } else if (const CXXTemporaryObjectExpr *Tmp =
939 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000940 Type = Tmp->getTypeSourceInfo();
941 }
942
943 if (!Type)
944 return cursor;
945
946 CXTranslationUnit TU = getCursorTU(cursor);
947 QualType Ty = Type->getType();
948 TypeLoc TL = Type->getTypeLoc();
949 SourceLocation Loc = TL.getBeginLoc();
950
951 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
952 Ty = ElabT->getNamedType();
David Blaikie6adc78e2013-02-18 22:06:02 +0000953 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000954 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
955 }
956
957 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
958 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
959 if (const TagType *Tag = Ty->getAs<TagType>())
960 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
961 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
962 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
963
964 return cursor;
965}
966
Douglas Gregorc58d05b2010-01-15 21:56:13 +0000967bool cxcursor::operator==(CXCursor X, CXCursor Y) {
968 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
969 X.data[2] == Y.data[2];
Douglas Gregor6c8959b2010-01-16 14:00:32 +0000970}
Ted Kremenek818e5c12010-11-01 23:26:51 +0000971
972// FIXME: Remove once we can model DeclGroups and their appropriate ranges
973// properly in the ASTs.
974bool cxcursor::isFirstInDeclGroup(CXCursor C) {
975 assert(clang_isDeclaration(C.kind));
976 return ((uintptr_t) (C.data[1])) != 0;
977}
978
Ted Kremenekbbedd062010-12-08 23:43:14 +0000979//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000980// libclang CXCursor APIs
981//===----------------------------------------------------------------------===//
982
Argyrios Kyrtzidisa1bcb6a2011-09-27 04:14:36 +0000983extern "C" {
984
985int clang_Cursor_isNull(CXCursor cursor) {
986 return clang_equalCursors(cursor, clang_getNullCursor());
987}
988
Argyrios Kyrtzidisd6e9fa52011-09-27 00:30:30 +0000989CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
990 return getCursorTU(cursor);
991}
992
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +0000993int clang_Cursor_getNumArguments(CXCursor C) {
994 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +0000995 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +0000996 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
997 return MD->param_size();
998 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
999 return FD->param_size();
1000 }
1001
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +00001002 if (clang_isExpression(C.kind)) {
1003 const Expr *E = cxcursor::getCursorExpr(C);
1004 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1005 return CE->getNumArgs();
1006 }
1007 }
1008
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001009 return -1;
1010}
1011
1012CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1013 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001014 const Decl *D = cxcursor::getCursorDecl(C);
1015 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001016 if (i < MD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001017 return cxcursor::MakeCXCursor(MD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001018 cxcursor::getCursorTU(C));
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001019 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001020 if (i < FD->param_size())
Alp Toker03376dc2014-07-07 09:02:20 +00001021 return cxcursor::MakeCXCursor(FD->parameters()[i],
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001022 cxcursor::getCursorTU(C));
1023 }
1024 }
1025
Argyrios Kyrtzidisb2792972013-04-01 17:38:59 +00001026 if (clang_isExpression(C.kind)) {
1027 const Expr *E = cxcursor::getCursorExpr(C);
1028 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1029 if (i < CE->getNumArgs()) {
1030 return cxcursor::MakeCXCursor(CE->getArg(i),
1031 getCursorDecl(C),
1032 cxcursor::getCursorTU(C));
1033 }
1034 }
1035 }
1036
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001037 return clang_getNullCursor();
1038}
1039
Ted Kremenekc0b98662013-04-24 07:17:12 +00001040} // end: extern "C"
1041
1042//===----------------------------------------------------------------------===//
1043// CXCursorSet.
1044//===----------------------------------------------------------------------===//
1045
1046typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1047
1048static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1049 return (CXCursorSet) setImpl;
1050}
1051static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1052 return (CXCursorSet_Impl*) set;
1053}
1054namespace llvm {
1055template<> struct DenseMapInfo<CXCursor> {
1056public:
1057 static inline CXCursor getEmptyKey() {
1058 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1059 }
1060 static inline CXCursor getTombstoneKey() {
1061 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1062 }
1063 static inline unsigned getHashValue(const CXCursor &cursor) {
1064 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1065 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1066 }
1067 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1068 return x.kind == y.kind &&
1069 x.data[0] == y.data[0] &&
1070 x.data[1] == y.data[1];
1071 }
1072};
1073}
1074
1075extern "C" {
1076CXCursorSet clang_createCXCursorSet() {
1077 return packCXCursorSet(new CXCursorSet_Impl());
1078}
1079
1080void clang_disposeCXCursorSet(CXCursorSet set) {
1081 delete unpackCXCursorSet(set);
1082}
1083
1084unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1085 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1086 if (!setImpl)
1087 return 0;
Ted Kremenek29b28e82013-04-24 07:25:40 +00001088 return setImpl->find(cursor) != setImpl->end();
Ted Kremenekc0b98662013-04-24 07:17:12 +00001089}
1090
1091unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1092 // Do not insert invalid cursors into the set.
1093 if (cursor.kind >= CXCursor_FirstInvalid &&
1094 cursor.kind <= CXCursor_LastInvalid)
1095 return 1;
1096
1097 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1098 if (!setImpl)
1099 return 1;
1100 unsigned &entry = (*setImpl)[cursor];
1101 unsigned flag = entry == 0 ? 1 : 0;
1102 entry = 1;
1103 return flag;
1104}
1105
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001106CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1107 enum CXCursorKind kind = clang_getCursorKind(cursor);
1108 if (clang_isDeclaration(kind)) {
Dmitri Gribenkod15bb302013-01-23 17:25:27 +00001109 const Decl *decl = getCursorDecl(cursor);
1110 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001111 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregor0a0e2b32013-01-31 04:52:16 +00001112 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001113 CodeCompletionString *String
1114 = Result.CreateCodeCompletionString(unit->getASTContext(),
1115 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001116 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001117 unit->getCodeCompletionTUInfo(),
1118 true);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001119 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001120 }
1121 }
1122 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001123 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001124 const IdentifierInfo *MacroInfo = definition->getName();
1125 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenko049a4ff2013-01-14 00:36:42 +00001126 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001127 CodeCompletionString *String
1128 = Result.CreateCodeCompletionString(unit->getASTContext(),
1129 unit->getPreprocessor(),
Argyrios Kyrtzidis9d7c0fe2012-04-10 17:23:48 +00001130 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001131 unit->getCodeCompletionTUInfo(),
1132 false);
Argyrios Kyrtzidis78908212012-01-17 02:15:54 +00001133 return String;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001134 }
Craig Topper69186e72014-06-08 08:38:04 +00001135 return nullptr;
Douglas Gregor3f35bb22011-08-04 20:04:59 +00001136}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001137} // end: extern C.
Ted Kremenekd77f6212012-04-30 19:06:49 +00001138
1139namespace {
1140 struct OverridenCursorsPool {
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001141 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001142 std::vector<CursorVec*> AllCursors;
1143 std::vector<CursorVec*> AvailableCursors;
1144
1145 ~OverridenCursorsPool() {
1146 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1147 E = AllCursors.end(); I != E; ++I) {
1148 delete *I;
1149 }
1150 }
1151 };
1152}
1153
1154void *cxcursor::createOverridenCXCursorsPool() {
1155 return new OverridenCursorsPool();
1156}
1157
1158void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1159 delete static_cast<OverridenCursorsPool*>(pool);
1160}
Ted Kremenekb47610a2012-04-30 19:33:45 +00001161
1162extern "C" {
Ted Kremenekd77f6212012-04-30 19:06:49 +00001163void clang_getOverriddenCursors(CXCursor cursor,
1164 CXCursor **overridden,
1165 unsigned *num_overridden) {
1166 if (overridden)
Craig Topper69186e72014-06-08 08:38:04 +00001167 *overridden = nullptr;
Ted Kremenekd77f6212012-04-30 19:06:49 +00001168 if (num_overridden)
1169 *num_overridden = 0;
1170
1171 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1172
1173 if (!overridden || !num_overridden || !TU)
1174 return;
1175
1176 if (!clang_isDeclaration(cursor.kind))
1177 return;
1178
1179 OverridenCursorsPool &pool =
1180 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
Craig Topper69186e72014-06-08 08:38:04 +00001181
1182 OverridenCursorsPool::CursorVec *Vec = nullptr;
1183
Ted Kremenekd77f6212012-04-30 19:06:49 +00001184 if (!pool.AvailableCursors.empty()) {
1185 Vec = pool.AvailableCursors.back();
1186 pool.AvailableCursors.pop_back();
1187 }
1188 else {
1189 Vec = new OverridenCursorsPool::CursorVec();
1190 pool.AllCursors.push_back(Vec);
1191 }
1192
1193 // Clear out the vector, but don't free the memory contents. This
1194 // reduces malloc() traffic.
1195 Vec->clear();
1196
1197 // Use the first entry to contain a back reference to the vector.
1198 // This is a complete hack.
1199 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1200 backRefCursor.data[0] = Vec;
1201 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1202 Vec->push_back(backRefCursor);
1203
1204 // Get the overriden cursors.
1205 cxcursor::getOverriddenCursors(cursor, *Vec);
1206
1207 // Did we get any overriden cursors? If not, return Vec to the pool
1208 // of available cursor vectors.
1209 if (Vec->size() == 1) {
1210 pool.AvailableCursors.push_back(Vec);
1211 return;
1212 }
1213
1214 // Now tell the caller about the overriden cursors.
1215 assert(Vec->size() > 1);
1216 *overridden = &((*Vec)[1]);
1217 *num_overridden = Vec->size() - 1;
1218}
1219
1220void clang_disposeOverriddenCursors(CXCursor *overridden) {
1221 if (!overridden)
1222 return;
1223
1224 // Use pointer arithmetic to get back the first faux entry
1225 // which has a back-reference to the TU and the vector.
1226 --overridden;
1227 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenkoba2f7462013-01-11 21:01:49 +00001228 static_cast<OverridenCursorsPool::CursorVec *>(
1229 const_cast<void *>(overridden->data[0]));
Ted Kremenekd77f6212012-04-30 19:06:49 +00001230 CXTranslationUnit TU = getCursorTU(*overridden);
1231
1232 assert(Vec && TU);
1233
1234 OverridenCursorsPool &pool =
1235 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1236
1237 pool.AvailableCursors.push_back(Vec);
1238}
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001239
1240int clang_Cursor_isDynamicCall(CXCursor C) {
Craig Topper69186e72014-06-08 08:38:04 +00001241 const Expr *E = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001242 if (clang_isExpression(C.kind))
1243 E = getCursorExpr(C);
1244 if (!E)
1245 return 0;
1246
1247 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1248 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1249
Craig Topper69186e72014-06-08 08:38:04 +00001250 const MemberExpr *ME = nullptr;
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00001251 if (isa<MemberExpr>(E))
1252 ME = cast<MemberExpr>(E);
1253 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1254 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1255
1256 if (ME) {
1257 if (const CXXMethodDecl *
1258 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1259 return MD->isVirtual() && !ME->hasQualifier();
1260 }
1261
1262 return 0;
1263}
1264
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001265CXType clang_Cursor_getReceiverType(CXCursor C) {
1266 CXTranslationUnit TU = cxcursor::getCursorTU(C);
Craig Topper69186e72014-06-08 08:38:04 +00001267 const Expr *E = nullptr;
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00001268 if (clang_isExpression(C.kind))
1269 E = getCursorExpr(C);
1270
1271 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1272 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1273
1274 return cxtype::MakeCXType(QualType(), TU);
1275}
1276
Ted Kremenekbbedd062010-12-08 23:43:14 +00001277} // end: extern "C"