blob: 48f3480bc2fc56942376701951491547d99e39c8 [file] [log] [blame]
Ted Kremenek16c440a2010-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 Gregor2e331b92010-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 Kremenek16c440a2010-01-15 20:35:54 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek0a90d322010-11-17 23:24:11 +000016#include "CXTranslationUnit.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000017#include "CXCursor.h"
Ted Kremeneked122732010-11-16 01:56:27 +000018#include "CXString.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000019#include "CXType.h"
20#include "clang-c/Index.h"
David Blaikie0b5ca512013-09-13 18:32:52 +000021#include "clang/AST/Attr.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000022#include "clang/AST/Decl.h"
Douglas Gregor69319002010-08-31 23:48:11 +000023#include "clang/AST/DeclCXX.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000024#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000025#include "clang/AST/DeclTemplate.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000026#include "clang/AST/Expr.h"
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000027#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000028#include "clang/AST/ExprObjC.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000029#include "clang/Frontend/ASTUnit.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000030#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000031
32using namespace clang;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000033using namespace cxcursor;
Ted Kremenek16c440a2010-01-15 20:35:54 +000034
Ted Kremenekbbf66ca2012-04-30 19:06:49 +000035CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000036 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
Stephen Hinesc568f1e2014-07-21 00:47:37 -070037 CXCursor C = { K, 0, { nullptr, nullptr, TU } };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000038 return C;
Ted Kremenek16c440a2010-01-15 20:35:54 +000039}
40
Ted Kremeneke77f4432010-02-18 03:09:07 +000041static CXCursorKind GetCursorKind(const Attr *A) {
42 assert(A && "Invalid arguments!");
43 switch (A->getKind()) {
44 default: break;
Sean Hunt387475d2010-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 Kyrtzidis6639e922011-09-13 17:39:31 +000048 case attr::Final: return CXCursor_CXXFinalAttr;
49 case attr::Override: return CXCursor_CXXOverrideAttr;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000050 case attr::Annotate: return CXCursor_AnnotateAttr;
Argyrios Kyrtzidis84b79642011-12-06 22:05:01 +000051 case attr::AsmLabel: return CXCursor_AsmLabelAttr;
Argyrios Kyrtzidis51337112013-09-25 00:14:38 +000052 case attr::Packed: return CXCursor_PackedAttr;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070053 case attr::Pure: return CXCursor_PureAttr;
54 case attr::Const: return CXCursor_ConstAttr;
55 case attr::NoDuplicate: return CXCursor_NoDuplicateAttr;
Stephen Hinesc568f1e2014-07-21 00:47:37 -070056 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 Kremeneke77f4432010-02-18 03:09:07 +000060 }
61
62 return CXCursor_UnexposedAttr;
63}
64
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000065CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
Ted Kremeneka60ed472010-11-16 08:15:36 +000066 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000067 assert(A && Parent && TU && "Invalid arguments!");
Dmitri Gribenko33156182013-01-11 21:06:06 +000068 CXCursor C = { GetCursorKind(A), 0, { Parent, A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000069 return C;
70}
71
Dmitri Gribenko67812b22013-01-11 21:01:49 +000072CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000073 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000074 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000075 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-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 Topper09d19ef2013-07-04 03:08:24 +000087 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-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 Gregor5bfb8c12010-01-20 23:34:41 +000098 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000099}
100
Dmitri Gribenko05756dc2013-01-14 00:46:27 +0000101CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000102 CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000103 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000104 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +0000105 CXCursorKind K = CXCursor_NotImplemented;
106
107 switch (S->getStmtClass()) {
108 case Stmt::NoStmtClass:
109 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000110
Douglas Gregor97b98722010-01-19 23:20:36 +0000111 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000112 K = CXCursor_CaseStmt;
113 break;
114
Douglas Gregor97b98722010-01-19 23:20:36 +0000115 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-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 Gregor97b98722010-01-19 23:20:36 +0000143 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-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 Rosierdf5faf52012-08-25 00:11:56 +0000159 case Stmt::GCCAsmStmtClass:
160 K = CXCursor_GCCAsmStmt;
Douglas Gregor42b29842011-10-05 19:00:14 +0000161 break;
Chad Rosier8cd64b42012-06-11 20:47:18 +0000162
163 case Stmt::MSAsmStmtClass:
164 K = CXCursor_MSAsmStmt;
165 break;
Douglas Gregor42b29842011-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 Gregor97b98722010-01-19 23:20:36 +0000191 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000192 K = CXCursor_ObjCForCollectionStmt;
193 break;
194
Douglas Gregor97b98722010-01-19 23:20:36 +0000195 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-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 Wiegley28bbe4b2011-04-28 01:08:34 +0000207 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000208 K = CXCursor_SEHTryStmt;
209 break;
210
John Wiegley28bbe4b2011-04-28 01:08:34 +0000211 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000212 K = CXCursor_SEHExceptStmt;
213 break;
214
John Wiegley28bbe4b2011-04-28 01:08:34 +0000215 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000216 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000217 break;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700218
219 case Stmt::SEHLeaveStmtClass:
220 K = CXCursor_SEHLeaveStmt;
221 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000222
John Wiegley21ff2e52011-04-28 00:16:57 +0000223 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000224 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000225 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000226 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000227 case Stmt::TypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000228 case Stmt::CXXBindTemporaryExprClass:
229 case Stmt::CXXDefaultArgExprClass:
Richard Smithc3bf52c2013-04-20 22:23:05 +0000230 case Stmt::CXXDefaultInitExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +0000231 case Stmt::CXXStdInitializerListExprClass:
Douglas Gregor42b29842011-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 Gregor42b29842011-10-05 19:00:14 +0000244 case Stmt::ParenListExprClass:
245 case Stmt::PredefinedExprClass:
246 case Stmt::ShuffleVectorExprClass:
Hal Finkel414a1bd2013-09-18 03:29:45 +0000247 case Stmt::ConvertVectorExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000248 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000249 case Stmt::VAArgExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000250 case Stmt::ObjCArrayLiteralClass:
251 case Stmt::ObjCDictionaryLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +0000252 case Stmt::ObjCBoxedExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000253 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000254 K = CXCursor_UnexposedExpr;
255 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000256
John McCall4b9c2d22011-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 Gregor42b29842011-10-05 19:00:14 +0000267 case Stmt::CompoundStmtClass:
268 K = CXCursor_CompoundStmt;
269 break;
Richard Smith534986f2012-04-14 00:33:13 +0000270
Douglas Gregor42b29842011-10-05 19:00:14 +0000271 case Stmt::NullStmtClass:
272 K = CXCursor_NullStmt;
273 break;
Richard Smith534986f2012-04-14 00:33:13 +0000274
Douglas Gregor42b29842011-10-05 19:00:14 +0000275 case Stmt::LabelStmtClass:
276 K = CXCursor_LabelStmt;
277 break;
Richard Smith534986f2012-04-14 00:33:13 +0000278
279 case Stmt::AttributedStmtClass:
280 K = CXCursor_UnexposedStmt;
281 break;
282
Douglas Gregor42b29842011-10-05 19:00:14 +0000283 case Stmt::DeclStmtClass:
284 K = CXCursor_DeclStmt;
285 break;
Richard Smith534986f2012-04-14 00:33:13 +0000286
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000287 case Stmt::CapturedStmtClass:
288 K = CXCursor_UnexposedStmt;
289 break;
290
Douglas Gregor42b29842011-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 Smith534986f2012-04-14 00:33:13 +0000318
Douglas Gregor42b29842011-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 Kremenekb3f75422012-03-06 20:06:06 +0000430
431 case Stmt::ObjCBoolLiteralExprClass:
432 K = CXCursor_ObjCBoolLiteralExpr;
433 break;
434
Douglas Gregor42b29842011-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 Kyrtzidisedab0472013-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 Gregor42b29842011-10-05 19:00:14 +0000466 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000467 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000468 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +0000469 case Stmt::FunctionParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000470 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000471 K = CXCursor_DeclRefExpr;
472 break;
473
Douglas Gregor42b29842011-10-05 19:00:14 +0000474 case Stmt::CXXDependentScopeMemberExprClass:
475 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000476 case Stmt::MemberExprClass:
John McCall76da55d2013-04-16 07:28:30 +0000477 case Stmt::MSPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000478 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000479 case Stmt::ObjCIvarRefExprClass:
480 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000481 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-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 Collingbournee08ce652011-02-09 21:07:24 +0000488 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000489 case Stmt::CXXConstructExprClass:
490 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000491 case Stmt::CXXUnresolvedConstructExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000492 case Stmt::UserDefinedLiteralClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000493 K = CXCursor_CallExpr;
494 break;
495
Douglas Gregor011d8b92012-02-15 00:54:55 +0000496 case Stmt::LambdaExprClass:
497 K = CXCursor_LambdaExpr;
498 break;
499
Douglas Gregorba0513d2011-10-25 01:33:02 +0000500 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000501 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-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 Topper09d19ef2013-07-04 03:08:24 +0000508 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-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 Gregor97b98722010-01-19 23:20:36 +0000515 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000516
517 case Stmt::MSDependentExistsStmtClass:
518 K = CXCursor_UnexposedStmt;
519 break;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000520 case Stmt::OMPParallelDirectiveClass:
521 K = CXCursor_OMPParallelDirective;
522 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700523 case Stmt::OMPSimdDirectiveClass:
524 K = CXCursor_OMPSimdDirective;
525 break;
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700526 case Stmt::OMPForDirectiveClass:
527 K = CXCursor_OMPForDirective;
528 break;
529 case Stmt::OMPSectionsDirectiveClass:
530 K = CXCursor_OMPSectionsDirective;
531 break;
532 case Stmt::OMPSectionDirectiveClass:
533 K = CXCursor_OMPSectionDirective;
534 break;
535 case Stmt::OMPSingleDirectiveClass:
536 K = CXCursor_OMPSingleDirective;
537 break;
538 case Stmt::OMPParallelForDirectiveClass:
539 K = CXCursor_OMPParallelForDirective;
540 break;
541 case Stmt::OMPParallelSectionsDirectiveClass:
542 K = CXCursor_OMPParallelSectionsDirective;
543 break;
Douglas Gregorba0513d2011-10-25 01:33:02 +0000544 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700545
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000546 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000547 return C;
548}
549
Douglas Gregor2e331b92010-01-16 14:00:32 +0000550CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000551 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000552 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000553 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000554 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000555 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000556 return C;
557}
558
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000559std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor2e331b92010-01-16 14:00:32 +0000560cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
561 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000562 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000563 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor2e331b92010-01-16 14:00:32 +0000564}
565
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000566CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000567 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000568 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000569 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000570 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000571 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000572 return C;
573}
574
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000575std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000576cxcursor::getCursorObjCProtocolRef(CXCursor C) {
577 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000578 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000579 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000580}
581
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000582CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000583 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000584 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000585 // 'Class' can be null for invalid code.
586 if (!Class)
587 return MakeCXCursorInvalid(CXCursor_InvalidCode);
588 assert(TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000589 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000590 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000591 return C;
592}
593
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000594std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor1adb0822010-01-16 17:14:40 +0000595cxcursor::getCursorObjCClassRef(CXCursor C) {
596 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000597 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000598 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1adb0822010-01-16 17:14:40 +0000599}
600
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000601CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000602 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000603 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000604 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000605 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000606 return C;
607}
608
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000609std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000610cxcursor::getCursorTypeRef(CXCursor C) {
611 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000612 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000613 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000614}
615
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000616CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000617 SourceLocation Loc,
618 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000619 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000620 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000621 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000622 return C;
623}
624
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000625std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregor0b36e612010-08-31 20:37:03 +0000626cxcursor::getCursorTemplateRef(CXCursor C) {
627 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000628 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000629 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor0b36e612010-08-31 20:37:03 +0000630}
631
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000632CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
633 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000634 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000635
636 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
637 "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000638 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000639 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000640 return C;
641}
642
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000643std::pair<const NamedDecl *, SourceLocation>
Douglas Gregor69319002010-08-31 23:48:11 +0000644cxcursor::getCursorNamespaceRef(CXCursor C) {
645 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000646 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000647 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor69319002010-08-31 23:48:11 +0000648}
649
Douglas Gregor011d8b92012-02-15 00:54:55 +0000650CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
651 CXTranslationUnit TU) {
652
653 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000654 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000655 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor011d8b92012-02-15 00:54:55 +0000656 return C;
657}
658
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000659std::pair<const VarDecl *, SourceLocation>
Douglas Gregor011d8b92012-02-15 00:54:55 +0000660cxcursor::getCursorVariableRef(CXCursor C) {
661 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000662 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000663 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor011d8b92012-02-15 00:54:55 +0000664}
665
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000666CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000667 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000668
669 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000670 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000671 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000672 return C;
673}
674
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000675std::pair<const FieldDecl *, SourceLocation>
Douglas Gregora67e03f2010-09-09 21:42:20 +0000676cxcursor::getCursorMemberRef(CXCursor C) {
677 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000678 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000679 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora67e03f2010-09-09 21:42:20 +0000680}
681
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000682CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000683 CXTranslationUnit TU){
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700684 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, nullptr, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000685 return C;
686}
687
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000688const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenek3064ef92010-08-27 21:34:58 +0000689 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000690 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000691}
692
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000693CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000694 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000695 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000696 { Range.getBegin().getPtrEncoding(),
697 Range.getEnd().getPtrEncoding(),
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000698 TU }
699 };
700 return C;
701}
702
703SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
704 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000705 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
706 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000707 ASTUnit *TU = getCursorASTUnit(C);
708 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000709}
710
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000711CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000712 CXTranslationUnit TU) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700713 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, nullptr, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000714 return C;
715}
716
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000717const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000718 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000719 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor572feb22010-03-18 18:04:21 +0000720}
721
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000722CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
723 CXTranslationUnit TU) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700724 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, nullptr, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000725 return C;
726}
727
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000728CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
729 SourceLocation Loc,
730 CXTranslationUnit TU) {
731 assert(Loc.isValid());
732 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
733 return C;
734}
735
736const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
737 if (isPseudo())
738 return getAsMacroDefinition()->getName();
739 return getAsMacroExpansion()->getName();
740}
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000741const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000742 if (isPseudo())
743 return getAsMacroDefinition();
744 return getAsMacroExpansion()->getDefinition();
745}
746SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
747 if (isPseudo())
748 return getPseudoLoc();
749 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +0000750}
751
Douglas Gregorecdcb882010-10-20 22:00:55 +0000752CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000753 CXTranslationUnit TU) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700754 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, nullptr, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000755 return C;
756}
757
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000758const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000759 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000760 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000761}
762
Douglas Gregor36897b02010-09-10 00:22:18 +0000763CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000764 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000765
766 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000767 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000768 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000769 return C;
770}
771
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000772std::pair<const LabelStmt *, SourceLocation>
Douglas Gregor36897b02010-09-10 00:22:18 +0000773cxcursor::getCursorLabelRef(CXCursor C) {
774 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000775 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000776 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor36897b02010-09-10 00:22:18 +0000777}
778
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000779CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000780 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000781 assert(E && TU && "Invalid arguments!");
782 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000783 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000784 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000785 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000786 { Storage.getOpaqueValue(), RawLoc, TU }
787 };
788 return C;
789}
790
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000791CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000792 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000793 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000794 assert(D && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000795 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000796 OverloadedDeclRefStorage Storage(D);
797 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000798 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000799 { Storage.getOpaqueValue(), RawLoc, TU }
800 };
801 return C;
802}
803
804CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
805 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000806 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000807 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000808 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000809 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
810 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000811 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000812 { Storage.getOpaqueValue(), RawLoc, TU }
813 };
814 return C;
815}
816
817std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
818cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
819 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000820 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
821 const_cast<void *>(C.data[0])),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000822 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000823}
824
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000825const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
826 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000827}
828
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000829const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000830 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
831}
832
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000833const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000834 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000835 Cursor.kind == CXCursor_ObjCProtocolRef ||
836 Cursor.kind == CXCursor_ObjCClassRef)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700837 return nullptr;
Douglas Gregor2e331b92010-01-16 14:00:32 +0000838
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000839 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000840}
841
Dmitri Gribenko7d914382013-01-26 18:08:08 +0000842const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
843 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremenek95f33552010-08-26 01:42:22 +0000844}
845
Dmitri Gribenko404628c2013-01-26 18:12:08 +0000846const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
847 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000848}
849
Douglas Gregorf46034a2010-01-18 23:41:10 +0000850ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000851 return getCursorASTUnit(Cursor)->getASTContext();
852}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000853
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000854ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko46f92522013-01-11 19:28:44 +0000855 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000856 if (!TU)
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700857 return nullptr;
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000858 return cxtu::getASTUnit(TU);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000859}
860
861CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000862 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregor283cae32010-01-15 21:56:13 +0000863}
864
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000865void cxcursor::getOverriddenCursors(CXCursor cursor,
866 SmallVectorImpl<CXCursor> &overridden) {
867 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000868 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000869 if (!D)
870 return;
871
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000872 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000873 SmallVector<const NamedDecl *, 8> OverDecls;
874 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000875
Craig Topper09d19ef2013-07-04 03:08:24 +0000876 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000877 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko05756dc2013-01-14 00:46:27 +0000878 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000879 }
880}
881
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000882std::pair<int, SourceLocation>
883cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
884 if (cursor.kind == CXCursor_ObjCMessageExpr) {
885 if (cursor.xdata != -1)
886 return std::make_pair(cursor.xdata,
887 cast<ObjCMessageExpr>(getCursorExpr(cursor))
888 ->getSelectorLoc(cursor.xdata));
889 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
890 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
891 if (cursor.xdata != -1)
892 return std::make_pair(cursor.xdata,
893 cast<ObjCMethodDecl>(getCursorDecl(cursor))
894 ->getSelectorLoc(cursor.xdata));
895 }
896
897 return std::make_pair(-1, SourceLocation());
898}
899
900CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
901 CXCursor newCursor = cursor;
902
903 if (cursor.kind == CXCursor_ObjCMessageExpr) {
904 if (SelIdx == -1 ||
905 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
906 ->getNumSelectorLocs())
907 newCursor.xdata = -1;
908 else
909 newCursor.xdata = SelIdx;
910 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
911 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
912 if (SelIdx == -1 ||
913 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
914 ->getNumSelectorLocs())
915 newCursor.xdata = -1;
916 else
917 newCursor.xdata = SelIdx;
918 }
919
920 return newCursor;
921}
922
923CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
924 if (cursor.kind != CXCursor_CallExpr)
925 return cursor;
926
927 if (cursor.xdata == 0)
928 return cursor;
929
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000930 const Expr *E = getCursorExpr(cursor);
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700931 TypeSourceInfo *Type = nullptr;
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000932 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000933 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
934 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000935 } else if (const CXXTemporaryObjectExpr *Tmp =
936 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000937 Type = Tmp->getTypeSourceInfo();
938 }
939
940 if (!Type)
941 return cursor;
942
943 CXTranslationUnit TU = getCursorTU(cursor);
944 QualType Ty = Type->getType();
945 TypeLoc TL = Type->getTypeLoc();
946 SourceLocation Loc = TL.getBeginLoc();
947
948 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
949 Ty = ElabT->getNamedType();
David Blaikie39e6ab42013-02-18 22:06:02 +0000950 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000951 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
952 }
953
954 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
955 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
956 if (const TagType *Tag = Ty->getAs<TagType>())
957 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
958 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
959 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
960
961 return cursor;
962}
963
Douglas Gregor283cae32010-01-15 21:56:13 +0000964bool cxcursor::operator==(CXCursor X, CXCursor Y) {
965 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
966 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000967}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000968
969// FIXME: Remove once we can model DeclGroups and their appropriate ranges
970// properly in the ASTs.
971bool cxcursor::isFirstInDeclGroup(CXCursor C) {
972 assert(clang_isDeclaration(C.kind));
973 return ((uintptr_t) (C.data[1])) != 0;
974}
975
Ted Kremenekeca099b2010-12-08 23:43:14 +0000976//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000977// libclang CXCursor APIs
978//===----------------------------------------------------------------------===//
979
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000980extern "C" {
981
982int clang_Cursor_isNull(CXCursor cursor) {
983 return clang_equalCursors(cursor, clang_getNullCursor());
984}
985
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000986CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
987 return getCursorTU(cursor);
988}
989
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000990int clang_Cursor_getNumArguments(CXCursor C) {
991 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000992 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000993 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
994 return MD->param_size();
995 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
996 return FD->param_size();
997 }
998
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000999 if (clang_isExpression(C.kind)) {
1000 const Expr *E = cxcursor::getCursorExpr(C);
1001 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1002 return CE->getNumArgs();
1003 }
1004 }
1005
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001006 return -1;
1007}
1008
1009CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1010 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00001011 const Decl *D = cxcursor::getCursorDecl(C);
1012 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001013 if (i < MD->param_size())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001014 return cxcursor::MakeCXCursor(MD->parameters()[i],
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001015 cxcursor::getCursorTU(C));
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00001016 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001017 if (i < FD->param_size())
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001018 return cxcursor::MakeCXCursor(FD->parameters()[i],
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001019 cxcursor::getCursorTU(C));
1020 }
1021 }
1022
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +00001023 if (clang_isExpression(C.kind)) {
1024 const Expr *E = cxcursor::getCursorExpr(C);
1025 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1026 if (i < CE->getNumArgs()) {
1027 return cxcursor::MakeCXCursor(CE->getArg(i),
1028 getCursorDecl(C),
1029 cxcursor::getCursorTU(C));
1030 }
1031 }
1032 }
1033
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001034 return clang_getNullCursor();
1035}
1036
Ted Kremenek017dd742013-04-24 07:17:12 +00001037} // end: extern "C"
1038
1039//===----------------------------------------------------------------------===//
1040// CXCursorSet.
1041//===----------------------------------------------------------------------===//
1042
1043typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1044
1045static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1046 return (CXCursorSet) setImpl;
1047}
1048static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1049 return (CXCursorSet_Impl*) set;
1050}
1051namespace llvm {
1052template<> struct DenseMapInfo<CXCursor> {
1053public:
1054 static inline CXCursor getEmptyKey() {
1055 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1056 }
1057 static inline CXCursor getTombstoneKey() {
1058 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1059 }
1060 static inline unsigned getHashValue(const CXCursor &cursor) {
1061 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1062 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1063 }
1064 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1065 return x.kind == y.kind &&
1066 x.data[0] == y.data[0] &&
1067 x.data[1] == y.data[1];
1068 }
1069};
1070}
1071
1072extern "C" {
1073CXCursorSet clang_createCXCursorSet() {
1074 return packCXCursorSet(new CXCursorSet_Impl());
1075}
1076
1077void clang_disposeCXCursorSet(CXCursorSet set) {
1078 delete unpackCXCursorSet(set);
1079}
1080
1081unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1082 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1083 if (!setImpl)
1084 return 0;
Ted Kremenek96bbe192013-04-24 07:25:40 +00001085 return setImpl->find(cursor) != setImpl->end();
Ted Kremenek017dd742013-04-24 07:17:12 +00001086}
1087
1088unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1089 // Do not insert invalid cursors into the set.
1090 if (cursor.kind >= CXCursor_FirstInvalid &&
1091 cursor.kind <= CXCursor_LastInvalid)
1092 return 1;
1093
1094 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1095 if (!setImpl)
1096 return 1;
1097 unsigned &entry = (*setImpl)[cursor];
1098 unsigned flag = entry == 0 ? 1 : 0;
1099 entry = 1;
1100 return flag;
1101}
1102
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001103CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1104 enum CXCursorKind kind = clang_getCursorKind(cursor);
1105 if (clang_isDeclaration(kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00001106 const Decl *decl = getCursorDecl(cursor);
1107 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001108 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregord1f09b42013-01-31 04:52:16 +00001109 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001110 CodeCompletionString *String
1111 = Result.CreateCodeCompletionString(unit->getASTContext(),
1112 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001113 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001114 unit->getCodeCompletionTUInfo(),
1115 true);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001116 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001117 }
1118 }
1119 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001120 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001121 const IdentifierInfo *MacroInfo = definition->getName();
1122 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenkob3958472013-01-14 00:36:42 +00001123 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001124 CodeCompletionString *String
1125 = Result.CreateCodeCompletionString(unit->getASTContext(),
1126 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001127 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001128 unit->getCodeCompletionTUInfo(),
1129 false);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001130 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001131 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001132 return nullptr;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001133}
Ted Kremenek8eece462012-04-30 19:33:45 +00001134} // end: extern C.
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001135
1136namespace {
1137 struct OverridenCursorsPool {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001138 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001139 std::vector<CursorVec*> AllCursors;
1140 std::vector<CursorVec*> AvailableCursors;
1141
1142 ~OverridenCursorsPool() {
1143 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1144 E = AllCursors.end(); I != E; ++I) {
1145 delete *I;
1146 }
1147 }
1148 };
1149}
1150
1151void *cxcursor::createOverridenCXCursorsPool() {
1152 return new OverridenCursorsPool();
1153}
1154
1155void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1156 delete static_cast<OverridenCursorsPool*>(pool);
1157}
Ted Kremenek8eece462012-04-30 19:33:45 +00001158
1159extern "C" {
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001160void clang_getOverriddenCursors(CXCursor cursor,
1161 CXCursor **overridden,
1162 unsigned *num_overridden) {
1163 if (overridden)
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001164 *overridden = nullptr;
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001165 if (num_overridden)
1166 *num_overridden = 0;
1167
1168 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1169
1170 if (!overridden || !num_overridden || !TU)
1171 return;
1172
1173 if (!clang_isDeclaration(cursor.kind))
1174 return;
1175
1176 OverridenCursorsPool &pool =
1177 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001178
1179 OverridenCursorsPool::CursorVec *Vec = nullptr;
1180
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001181 if (!pool.AvailableCursors.empty()) {
1182 Vec = pool.AvailableCursors.back();
1183 pool.AvailableCursors.pop_back();
1184 }
1185 else {
1186 Vec = new OverridenCursorsPool::CursorVec();
1187 pool.AllCursors.push_back(Vec);
1188 }
1189
1190 // Clear out the vector, but don't free the memory contents. This
1191 // reduces malloc() traffic.
1192 Vec->clear();
1193
1194 // Use the first entry to contain a back reference to the vector.
1195 // This is a complete hack.
1196 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1197 backRefCursor.data[0] = Vec;
1198 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1199 Vec->push_back(backRefCursor);
1200
1201 // Get the overriden cursors.
1202 cxcursor::getOverriddenCursors(cursor, *Vec);
1203
1204 // Did we get any overriden cursors? If not, return Vec to the pool
1205 // of available cursor vectors.
1206 if (Vec->size() == 1) {
1207 pool.AvailableCursors.push_back(Vec);
1208 return;
1209 }
1210
1211 // Now tell the caller about the overriden cursors.
1212 assert(Vec->size() > 1);
1213 *overridden = &((*Vec)[1]);
1214 *num_overridden = Vec->size() - 1;
1215}
1216
1217void clang_disposeOverriddenCursors(CXCursor *overridden) {
1218 if (!overridden)
1219 return;
1220
1221 // Use pointer arithmetic to get back the first faux entry
1222 // which has a back-reference to the TU and the vector.
1223 --overridden;
1224 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001225 static_cast<OverridenCursorsPool::CursorVec *>(
1226 const_cast<void *>(overridden->data[0]));
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001227 CXTranslationUnit TU = getCursorTU(*overridden);
1228
1229 assert(Vec && TU);
1230
1231 OverridenCursorsPool &pool =
1232 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1233
1234 pool.AvailableCursors.push_back(Vec);
1235}
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001236
1237int clang_Cursor_isDynamicCall(CXCursor C) {
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001238 const Expr *E = nullptr;
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001239 if (clang_isExpression(C.kind))
1240 E = getCursorExpr(C);
1241 if (!E)
1242 return 0;
1243
1244 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1245 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1246
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001247 const MemberExpr *ME = nullptr;
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001248 if (isa<MemberExpr>(E))
1249 ME = cast<MemberExpr>(E);
1250 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1251 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1252
1253 if (ME) {
1254 if (const CXXMethodDecl *
1255 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1256 return MD->isVirtual() && !ME->hasQualifier();
1257 }
1258
1259 return 0;
1260}
1261
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001262CXType clang_Cursor_getReceiverType(CXCursor C) {
1263 CXTranslationUnit TU = cxcursor::getCursorTU(C);
Stephen Hinesc568f1e2014-07-21 00:47:37 -07001264 const Expr *E = nullptr;
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001265 if (clang_isExpression(C.kind))
1266 E = getCursorExpr(C);
1267
1268 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1269 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1270
1271 return cxtype::MakeCXType(QualType(), TU);
1272}
1273
Ted Kremenekeca099b2010-12-08 23:43:14 +00001274} // end: extern "C"