blob: fb4ce66d3ac4ed633bd07f9bbfc87a8b6aa7a750 [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);
Ted Kremenekbbf66ca2012-04-30 19:06:49 +000037 CXCursor C = { K, 0, { 0, 0, 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;
Ted Kremeneke77f4432010-02-18 03:09:07 +000056 }
57
58 return CXCursor_UnexposedAttr;
59}
60
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000061CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
Ted Kremeneka60ed472010-11-16 08:15:36 +000062 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000063 assert(A && Parent && TU && "Invalid arguments!");
Dmitri Gribenko33156182013-01-11 21:06:06 +000064 CXCursor C = { GetCursorKind(A), 0, { Parent, A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000065 return C;
66}
67
Dmitri Gribenko67812b22013-01-11 21:01:49 +000068CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000069 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000070 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000071 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000072
73 CXCursorKind K = getCursorKindForDecl(D);
74
75 if (K == CXCursor_ObjCClassMethodDecl ||
76 K == CXCursor_ObjCInstanceMethodDecl) {
77 int SelectorIdIndex = -1;
78 // Check if cursor points to a selector id.
79 if (RegionOfInterest.isValid() &&
80 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
81 SmallVector<SourceLocation, 16> SelLocs;
82 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
Craig Topper09d19ef2013-07-04 03:08:24 +000083 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000084 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
85 if (I != SelLocs.end())
86 SelectorIdIndex = I - SelLocs.begin();
87 }
88 CXCursor C = { K, SelectorIdIndex,
89 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
90 return C;
91 }
92
93 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000094 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000095}
96
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000097CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
Dmitri Gribenko67812b22013-01-11 21:01:49 +000098 CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000099 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000100 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +0000101 CXCursorKind K = CXCursor_NotImplemented;
102
103 switch (S->getStmtClass()) {
104 case Stmt::NoStmtClass:
105 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000106
Douglas Gregor97b98722010-01-19 23:20:36 +0000107 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000108 K = CXCursor_CaseStmt;
109 break;
110
Douglas Gregor97b98722010-01-19 23:20:36 +0000111 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000112 K = CXCursor_DefaultStmt;
113 break;
114
115 case Stmt::IfStmtClass:
116 K = CXCursor_IfStmt;
117 break;
118
119 case Stmt::SwitchStmtClass:
120 K = CXCursor_SwitchStmt;
121 break;
122
123 case Stmt::WhileStmtClass:
124 K = CXCursor_WhileStmt;
125 break;
126
127 case Stmt::DoStmtClass:
128 K = CXCursor_DoStmt;
129 break;
130
131 case Stmt::ForStmtClass:
132 K = CXCursor_ForStmt;
133 break;
134
135 case Stmt::GotoStmtClass:
136 K = CXCursor_GotoStmt;
137 break;
138
Douglas Gregor97b98722010-01-19 23:20:36 +0000139 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000140 K = CXCursor_IndirectGotoStmt;
141 break;
142
143 case Stmt::ContinueStmtClass:
144 K = CXCursor_ContinueStmt;
145 break;
146
147 case Stmt::BreakStmtClass:
148 K = CXCursor_BreakStmt;
149 break;
150
151 case Stmt::ReturnStmtClass:
152 K = CXCursor_ReturnStmt;
153 break;
154
Chad Rosierdf5faf52012-08-25 00:11:56 +0000155 case Stmt::GCCAsmStmtClass:
156 K = CXCursor_GCCAsmStmt;
Douglas Gregor42b29842011-10-05 19:00:14 +0000157 break;
Chad Rosier8cd64b42012-06-11 20:47:18 +0000158
159 case Stmt::MSAsmStmtClass:
160 K = CXCursor_MSAsmStmt;
161 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000162
163 case Stmt::ObjCAtTryStmtClass:
164 K = CXCursor_ObjCAtTryStmt;
165 break;
166
167 case Stmt::ObjCAtCatchStmtClass:
168 K = CXCursor_ObjCAtCatchStmt;
169 break;
170
171 case Stmt::ObjCAtFinallyStmtClass:
172 K = CXCursor_ObjCAtFinallyStmt;
173 break;
174
175 case Stmt::ObjCAtThrowStmtClass:
176 K = CXCursor_ObjCAtThrowStmt;
177 break;
178
179 case Stmt::ObjCAtSynchronizedStmtClass:
180 K = CXCursor_ObjCAtSynchronizedStmt;
181 break;
182
183 case Stmt::ObjCAutoreleasePoolStmtClass:
184 K = CXCursor_ObjCAutoreleasePoolStmt;
185 break;
186
Douglas Gregor97b98722010-01-19 23:20:36 +0000187 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000188 K = CXCursor_ObjCForCollectionStmt;
189 break;
190
Douglas Gregor97b98722010-01-19 23:20:36 +0000191 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000192 K = CXCursor_CXXCatchStmt;
193 break;
194
195 case Stmt::CXXTryStmtClass:
196 K = CXCursor_CXXTryStmt;
197 break;
198
199 case Stmt::CXXForRangeStmtClass:
200 K = CXCursor_CXXForRangeStmt;
201 break;
202
John Wiegley28bbe4b2011-04-28 01:08:34 +0000203 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000204 K = CXCursor_SEHTryStmt;
205 break;
206
John Wiegley28bbe4b2011-04-28 01:08:34 +0000207 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000208 K = CXCursor_SEHExceptStmt;
209 break;
210
John Wiegley28bbe4b2011-04-28 01:08:34 +0000211 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000212 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000213 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000214
John Wiegley21ff2e52011-04-28 00:16:57 +0000215 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000216 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000217 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000218 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000219 case Stmt::TypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000220 case Stmt::CXXBindTemporaryExprClass:
221 case Stmt::CXXDefaultArgExprClass:
Richard Smithc3bf52c2013-04-20 22:23:05 +0000222 case Stmt::CXXDefaultInitExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +0000223 case Stmt::CXXStdInitializerListExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000224 case Stmt::CXXScalarValueInitExprClass:
225 case Stmt::CXXUuidofExprClass:
226 case Stmt::ChooseExprClass:
227 case Stmt::DesignatedInitExprClass:
228 case Stmt::ExprWithCleanupsClass:
229 case Stmt::ExpressionTraitExprClass:
230 case Stmt::ExtVectorElementExprClass:
231 case Stmt::ImplicitCastExprClass:
232 case Stmt::ImplicitValueInitExprClass:
233 case Stmt::MaterializeTemporaryExprClass:
234 case Stmt::ObjCIndirectCopyRestoreExprClass:
235 case Stmt::OffsetOfExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000236 case Stmt::ParenListExprClass:
237 case Stmt::PredefinedExprClass:
238 case Stmt::ShuffleVectorExprClass:
Hal Finkel414a1bd2013-09-18 03:29:45 +0000239 case Stmt::ConvertVectorExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000240 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000241 case Stmt::VAArgExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000242 case Stmt::ObjCArrayLiteralClass:
243 case Stmt::ObjCDictionaryLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +0000244 case Stmt::ObjCBoxedExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000245 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000246 K = CXCursor_UnexposedExpr;
247 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000248
John McCall4b9c2d22011-11-06 09:01:30 +0000249 case Stmt::OpaqueValueExprClass:
250 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
251 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
252 K = CXCursor_UnexposedExpr;
253 break;
254
255 case Stmt::PseudoObjectExprClass:
256 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
257 Parent, TU, RegionOfInterest);
258
Douglas Gregor42b29842011-10-05 19:00:14 +0000259 case Stmt::CompoundStmtClass:
260 K = CXCursor_CompoundStmt;
261 break;
Richard Smith534986f2012-04-14 00:33:13 +0000262
Douglas Gregor42b29842011-10-05 19:00:14 +0000263 case Stmt::NullStmtClass:
264 K = CXCursor_NullStmt;
265 break;
Richard Smith534986f2012-04-14 00:33:13 +0000266
Douglas Gregor42b29842011-10-05 19:00:14 +0000267 case Stmt::LabelStmtClass:
268 K = CXCursor_LabelStmt;
269 break;
Richard Smith534986f2012-04-14 00:33:13 +0000270
271 case Stmt::AttributedStmtClass:
272 K = CXCursor_UnexposedStmt;
273 break;
274
Douglas Gregor42b29842011-10-05 19:00:14 +0000275 case Stmt::DeclStmtClass:
276 K = CXCursor_DeclStmt;
277 break;
Richard Smith534986f2012-04-14 00:33:13 +0000278
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000279 case Stmt::CapturedStmtClass:
280 K = CXCursor_UnexposedStmt;
281 break;
282
Douglas Gregor42b29842011-10-05 19:00:14 +0000283 case Stmt::IntegerLiteralClass:
284 K = CXCursor_IntegerLiteral;
285 break;
286
287 case Stmt::FloatingLiteralClass:
288 K = CXCursor_FloatingLiteral;
289 break;
290
291 case Stmt::ImaginaryLiteralClass:
292 K = CXCursor_ImaginaryLiteral;
293 break;
294
295 case Stmt::StringLiteralClass:
296 K = CXCursor_StringLiteral;
297 break;
298
299 case Stmt::CharacterLiteralClass:
300 K = CXCursor_CharacterLiteral;
301 break;
302
303 case Stmt::ParenExprClass:
304 K = CXCursor_ParenExpr;
305 break;
306
307 case Stmt::UnaryOperatorClass:
308 K = CXCursor_UnaryOperator;
309 break;
Richard Smith534986f2012-04-14 00:33:13 +0000310
Douglas Gregor42b29842011-10-05 19:00:14 +0000311 case Stmt::CXXNoexceptExprClass:
312 K = CXCursor_UnaryExpr;
313 break;
314
315 case Stmt::ArraySubscriptExprClass:
316 K = CXCursor_ArraySubscriptExpr;
317 break;
318
319 case Stmt::BinaryOperatorClass:
320 K = CXCursor_BinaryOperator;
321 break;
322
323 case Stmt::CompoundAssignOperatorClass:
324 K = CXCursor_CompoundAssignOperator;
325 break;
326
327 case Stmt::ConditionalOperatorClass:
328 K = CXCursor_ConditionalOperator;
329 break;
330
331 case Stmt::CStyleCastExprClass:
332 K = CXCursor_CStyleCastExpr;
333 break;
334
335 case Stmt::CompoundLiteralExprClass:
336 K = CXCursor_CompoundLiteralExpr;
337 break;
338
339 case Stmt::InitListExprClass:
340 K = CXCursor_InitListExpr;
341 break;
342
343 case Stmt::AddrLabelExprClass:
344 K = CXCursor_AddrLabelExpr;
345 break;
346
347 case Stmt::StmtExprClass:
348 K = CXCursor_StmtExpr;
349 break;
350
351 case Stmt::GenericSelectionExprClass:
352 K = CXCursor_GenericSelectionExpr;
353 break;
354
355 case Stmt::GNUNullExprClass:
356 K = CXCursor_GNUNullExpr;
357 break;
358
359 case Stmt::CXXStaticCastExprClass:
360 K = CXCursor_CXXStaticCastExpr;
361 break;
362
363 case Stmt::CXXDynamicCastExprClass:
364 K = CXCursor_CXXDynamicCastExpr;
365 break;
366
367 case Stmt::CXXReinterpretCastExprClass:
368 K = CXCursor_CXXReinterpretCastExpr;
369 break;
370
371 case Stmt::CXXConstCastExprClass:
372 K = CXCursor_CXXConstCastExpr;
373 break;
374
375 case Stmt::CXXFunctionalCastExprClass:
376 K = CXCursor_CXXFunctionalCastExpr;
377 break;
378
379 case Stmt::CXXTypeidExprClass:
380 K = CXCursor_CXXTypeidExpr;
381 break;
382
383 case Stmt::CXXBoolLiteralExprClass:
384 K = CXCursor_CXXBoolLiteralExpr;
385 break;
386
387 case Stmt::CXXNullPtrLiteralExprClass:
388 K = CXCursor_CXXNullPtrLiteralExpr;
389 break;
390
391 case Stmt::CXXThisExprClass:
392 K = CXCursor_CXXThisExpr;
393 break;
394
395 case Stmt::CXXThrowExprClass:
396 K = CXCursor_CXXThrowExpr;
397 break;
398
399 case Stmt::CXXNewExprClass:
400 K = CXCursor_CXXNewExpr;
401 break;
402
403 case Stmt::CXXDeleteExprClass:
404 K = CXCursor_CXXDeleteExpr;
405 break;
406
407 case Stmt::ObjCStringLiteralClass:
408 K = CXCursor_ObjCStringLiteral;
409 break;
410
411 case Stmt::ObjCEncodeExprClass:
412 K = CXCursor_ObjCEncodeExpr;
413 break;
414
415 case Stmt::ObjCSelectorExprClass:
416 K = CXCursor_ObjCSelectorExpr;
417 break;
418
419 case Stmt::ObjCProtocolExprClass:
420 K = CXCursor_ObjCProtocolExpr;
421 break;
Ted Kremenekb3f75422012-03-06 20:06:06 +0000422
423 case Stmt::ObjCBoolLiteralExprClass:
424 K = CXCursor_ObjCBoolLiteralExpr;
425 break;
426
Douglas Gregor42b29842011-10-05 19:00:14 +0000427 case Stmt::ObjCBridgedCastExprClass:
428 K = CXCursor_ObjCBridgedCastExpr;
429 break;
430
431 case Stmt::BlockExprClass:
432 K = CXCursor_BlockExpr;
433 break;
434
435 case Stmt::PackExpansionExprClass:
436 K = CXCursor_PackExpansionExpr;
437 break;
438
439 case Stmt::SizeOfPackExprClass:
440 K = CXCursor_SizeOfPackExpr;
441 break;
442
Argyrios Kyrtzidisedab0472013-04-23 17:57:17 +0000443 case Stmt::DeclRefExprClass:
444 if (const ImplicitParamDecl *IPD =
445 dyn_cast_or_null<ImplicitParamDecl>(cast<DeclRefExpr>(S)->getDecl())) {
446 if (const ObjCMethodDecl *MD =
447 dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
448 if (MD->getSelfDecl() == IPD) {
449 K = CXCursor_ObjCSelfExpr;
450 break;
451 }
452 }
453 }
454
455 K = CXCursor_DeclRefExpr;
456 break;
457
Douglas Gregor42b29842011-10-05 19:00:14 +0000458 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000459 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000460 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +0000461 case Stmt::FunctionParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000462 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000463 K = CXCursor_DeclRefExpr;
464 break;
465
Douglas Gregor42b29842011-10-05 19:00:14 +0000466 case Stmt::CXXDependentScopeMemberExprClass:
467 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000468 case Stmt::MemberExprClass:
John McCall76da55d2013-04-16 07:28:30 +0000469 case Stmt::MSPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000470 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000471 case Stmt::ObjCIvarRefExprClass:
472 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000473 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000474 K = CXCursor_MemberRefExpr;
475 break;
476
477 case Stmt::CallExprClass:
478 case Stmt::CXXOperatorCallExprClass:
479 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000480 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000481 case Stmt::CXXConstructExprClass:
482 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000483 case Stmt::CXXUnresolvedConstructExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000484 case Stmt::UserDefinedLiteralClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000485 K = CXCursor_CallExpr;
486 break;
487
Douglas Gregor011d8b92012-02-15 00:54:55 +0000488 case Stmt::LambdaExprClass:
489 K = CXCursor_LambdaExpr;
490 break;
491
Douglas Gregorba0513d2011-10-25 01:33:02 +0000492 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000493 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000494 int SelectorIdIndex = -1;
495 // Check if cursor points to a selector id.
496 if (RegionOfInterest.isValid() &&
497 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
498 SmallVector<SourceLocation, 16> SelLocs;
499 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
Craig Topper09d19ef2013-07-04 03:08:24 +0000500 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000501 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
502 if (I != SelLocs.end())
503 SelectorIdIndex = I - SelLocs.begin();
504 }
505 CXCursor C = { K, 0, { Parent, S, TU } };
506 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000507 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000508
509 case Stmt::MSDependentExistsStmtClass:
510 K = CXCursor_UnexposedStmt;
511 break;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000512 case Stmt::OMPParallelDirectiveClass:
513 K = CXCursor_OMPParallelDirective;
514 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700515 case Stmt::OMPSimdDirectiveClass:
516 K = CXCursor_OMPSimdDirective;
517 break;
Douglas Gregorba0513d2011-10-25 01:33:02 +0000518 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700519
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000520 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000521 return C;
522}
523
Douglas Gregor2e331b92010-01-16 14:00:32 +0000524CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000525 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000526 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000527 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000528 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000529 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000530 return C;
531}
532
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000533std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor2e331b92010-01-16 14:00:32 +0000534cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
535 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000536 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000537 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor2e331b92010-01-16 14:00:32 +0000538}
539
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000540CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000541 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000542 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000543 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000544 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000545 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000546 return C;
547}
548
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000549std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000550cxcursor::getCursorObjCProtocolRef(CXCursor C) {
551 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000552 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000553 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000554}
555
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000556CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000557 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000558 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000559 // 'Class' can be null for invalid code.
560 if (!Class)
561 return MakeCXCursorInvalid(CXCursor_InvalidCode);
562 assert(TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000563 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000564 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000565 return C;
566}
567
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000568std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor1adb0822010-01-16 17:14:40 +0000569cxcursor::getCursorObjCClassRef(CXCursor C) {
570 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000571 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000572 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1adb0822010-01-16 17:14:40 +0000573}
574
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000575CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000576 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000577 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000578 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000579 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000580 return C;
581}
582
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000583std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000584cxcursor::getCursorTypeRef(CXCursor C) {
585 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000586 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000587 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000588}
589
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000590CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000591 SourceLocation Loc,
592 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000593 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000594 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000595 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000596 return C;
597}
598
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000599std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregor0b36e612010-08-31 20:37:03 +0000600cxcursor::getCursorTemplateRef(CXCursor C) {
601 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000602 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000603 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor0b36e612010-08-31 20:37:03 +0000604}
605
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000606CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
607 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000608 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000609
610 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
611 "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000612 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000613 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000614 return C;
615}
616
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000617std::pair<const NamedDecl *, SourceLocation>
Douglas Gregor69319002010-08-31 23:48:11 +0000618cxcursor::getCursorNamespaceRef(CXCursor C) {
619 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000620 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000621 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor69319002010-08-31 23:48:11 +0000622}
623
Douglas Gregor011d8b92012-02-15 00:54:55 +0000624CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
625 CXTranslationUnit TU) {
626
627 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000628 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000629 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor011d8b92012-02-15 00:54:55 +0000630 return C;
631}
632
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000633std::pair<const VarDecl *, SourceLocation>
Douglas Gregor011d8b92012-02-15 00:54:55 +0000634cxcursor::getCursorVariableRef(CXCursor C) {
635 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000636 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000637 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor011d8b92012-02-15 00:54:55 +0000638}
639
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000640CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000641 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000642
643 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000644 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000645 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000646 return C;
647}
648
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000649std::pair<const FieldDecl *, SourceLocation>
Douglas Gregora67e03f2010-09-09 21:42:20 +0000650cxcursor::getCursorMemberRef(CXCursor C) {
651 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000652 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000653 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora67e03f2010-09-09 21:42:20 +0000654}
655
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000656CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000657 CXTranslationUnit TU){
Dmitri Gribenko33156182013-01-11 21:06:06 +0000658 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000659 return C;
660}
661
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000662const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenek3064ef92010-08-27 21:34:58 +0000663 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000664 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000665}
666
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000667CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000668 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000669 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000670 { Range.getBegin().getPtrEncoding(),
671 Range.getEnd().getPtrEncoding(),
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000672 TU }
673 };
674 return C;
675}
676
677SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
678 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000679 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
680 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000681 ASTUnit *TU = getCursorASTUnit(C);
682 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000683}
684
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000685CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000686 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000687 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000688 return C;
689}
690
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000691const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000692 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000693 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor572feb22010-03-18 18:04:21 +0000694}
695
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000696CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
697 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000698 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000699 return C;
700}
701
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000702CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
703 SourceLocation Loc,
704 CXTranslationUnit TU) {
705 assert(Loc.isValid());
706 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
707 return C;
708}
709
710const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
711 if (isPseudo())
712 return getAsMacroDefinition()->getName();
713 return getAsMacroExpansion()->getName();
714}
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000715const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000716 if (isPseudo())
717 return getAsMacroDefinition();
718 return getAsMacroExpansion()->getDefinition();
719}
720SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
721 if (isPseudo())
722 return getPseudoLoc();
723 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +0000724}
725
Douglas Gregorecdcb882010-10-20 22:00:55 +0000726CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000727 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000728 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000729 return C;
730}
731
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000732const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000733 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000734 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000735}
736
Douglas Gregor36897b02010-09-10 00:22:18 +0000737CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000738 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000739
740 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000741 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000742 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000743 return C;
744}
745
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000746std::pair<const LabelStmt *, SourceLocation>
Douglas Gregor36897b02010-09-10 00:22:18 +0000747cxcursor::getCursorLabelRef(CXCursor C) {
748 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000749 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000750 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor36897b02010-09-10 00:22:18 +0000751}
752
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000753CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000754 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000755 assert(E && TU && "Invalid arguments!");
756 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000757 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000758 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000759 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000760 { Storage.getOpaqueValue(), RawLoc, TU }
761 };
762 return C;
763}
764
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000765CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000766 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000767 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000768 assert(D && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000769 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000770 OverloadedDeclRefStorage Storage(D);
771 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000772 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000773 { Storage.getOpaqueValue(), RawLoc, TU }
774 };
775 return C;
776}
777
778CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
779 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000780 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000781 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000782 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000783 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
784 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
791std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
792cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
793 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000794 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
795 const_cast<void *>(C.data[0])),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000796 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000797}
798
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000799const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
800 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000801}
802
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000803const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000804 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
805}
806
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000807const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000808 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000809 Cursor.kind == CXCursor_ObjCProtocolRef ||
810 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000811 return 0;
812
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000813 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000814}
815
Dmitri Gribenko7d914382013-01-26 18:08:08 +0000816const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
817 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremenek95f33552010-08-26 01:42:22 +0000818}
819
Dmitri Gribenko404628c2013-01-26 18:12:08 +0000820const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
821 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000822}
823
Douglas Gregorf46034a2010-01-18 23:41:10 +0000824ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000825 return getCursorASTUnit(Cursor)->getASTContext();
826}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000827
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000828ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko46f92522013-01-11 19:28:44 +0000829 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000830 if (!TU)
831 return 0;
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000832 return cxtu::getASTUnit(TU);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000833}
834
835CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000836 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregor283cae32010-01-15 21:56:13 +0000837}
838
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000839void cxcursor::getOverriddenCursors(CXCursor cursor,
840 SmallVectorImpl<CXCursor> &overridden) {
841 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000842 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000843 if (!D)
844 return;
845
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000846 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000847 SmallVector<const NamedDecl *, 8> OverDecls;
848 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000849
Craig Topper09d19ef2013-07-04 03:08:24 +0000850 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000851 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko05756dc2013-01-14 00:46:27 +0000852 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000853 }
854}
855
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000856std::pair<int, SourceLocation>
857cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
858 if (cursor.kind == CXCursor_ObjCMessageExpr) {
859 if (cursor.xdata != -1)
860 return std::make_pair(cursor.xdata,
861 cast<ObjCMessageExpr>(getCursorExpr(cursor))
862 ->getSelectorLoc(cursor.xdata));
863 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
864 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
865 if (cursor.xdata != -1)
866 return std::make_pair(cursor.xdata,
867 cast<ObjCMethodDecl>(getCursorDecl(cursor))
868 ->getSelectorLoc(cursor.xdata));
869 }
870
871 return std::make_pair(-1, SourceLocation());
872}
873
874CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
875 CXCursor newCursor = cursor;
876
877 if (cursor.kind == CXCursor_ObjCMessageExpr) {
878 if (SelIdx == -1 ||
879 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
880 ->getNumSelectorLocs())
881 newCursor.xdata = -1;
882 else
883 newCursor.xdata = SelIdx;
884 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
885 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
886 if (SelIdx == -1 ||
887 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
888 ->getNumSelectorLocs())
889 newCursor.xdata = -1;
890 else
891 newCursor.xdata = SelIdx;
892 }
893
894 return newCursor;
895}
896
897CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
898 if (cursor.kind != CXCursor_CallExpr)
899 return cursor;
900
901 if (cursor.xdata == 0)
902 return cursor;
903
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000904 const Expr *E = getCursorExpr(cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000905 TypeSourceInfo *Type = 0;
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000906 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000907 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
908 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000909 } else if (const CXXTemporaryObjectExpr *Tmp =
910 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000911 Type = Tmp->getTypeSourceInfo();
912 }
913
914 if (!Type)
915 return cursor;
916
917 CXTranslationUnit TU = getCursorTU(cursor);
918 QualType Ty = Type->getType();
919 TypeLoc TL = Type->getTypeLoc();
920 SourceLocation Loc = TL.getBeginLoc();
921
922 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
923 Ty = ElabT->getNamedType();
David Blaikie39e6ab42013-02-18 22:06:02 +0000924 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000925 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
926 }
927
928 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
929 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
930 if (const TagType *Tag = Ty->getAs<TagType>())
931 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
932 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
933 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
934
935 return cursor;
936}
937
Douglas Gregor283cae32010-01-15 21:56:13 +0000938bool cxcursor::operator==(CXCursor X, CXCursor Y) {
939 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
940 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000941}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000942
943// FIXME: Remove once we can model DeclGroups and their appropriate ranges
944// properly in the ASTs.
945bool cxcursor::isFirstInDeclGroup(CXCursor C) {
946 assert(clang_isDeclaration(C.kind));
947 return ((uintptr_t) (C.data[1])) != 0;
948}
949
Ted Kremenekeca099b2010-12-08 23:43:14 +0000950//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000951// libclang CXCursor APIs
952//===----------------------------------------------------------------------===//
953
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000954extern "C" {
955
956int clang_Cursor_isNull(CXCursor cursor) {
957 return clang_equalCursors(cursor, clang_getNullCursor());
958}
959
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000960CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
961 return getCursorTU(cursor);
962}
963
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000964int clang_Cursor_getNumArguments(CXCursor C) {
965 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000966 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000967 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
968 return MD->param_size();
969 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
970 return FD->param_size();
971 }
972
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000973 if (clang_isExpression(C.kind)) {
974 const Expr *E = cxcursor::getCursorExpr(C);
975 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
976 return CE->getNumArgs();
977 }
978 }
979
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000980 return -1;
981}
982
983CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
984 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000985 const Decl *D = cxcursor::getCursorDecl(C);
986 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000987 if (i < MD->param_size())
988 return cxcursor::MakeCXCursor(MD->param_begin()[i],
989 cxcursor::getCursorTU(C));
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000990 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000991 if (i < FD->param_size())
992 return cxcursor::MakeCXCursor(FD->param_begin()[i],
993 cxcursor::getCursorTU(C));
994 }
995 }
996
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000997 if (clang_isExpression(C.kind)) {
998 const Expr *E = cxcursor::getCursorExpr(C);
999 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1000 if (i < CE->getNumArgs()) {
1001 return cxcursor::MakeCXCursor(CE->getArg(i),
1002 getCursorDecl(C),
1003 cxcursor::getCursorTU(C));
1004 }
1005 }
1006 }
1007
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001008 return clang_getNullCursor();
1009}
1010
Ted Kremenek017dd742013-04-24 07:17:12 +00001011} // end: extern "C"
1012
1013//===----------------------------------------------------------------------===//
1014// CXCursorSet.
1015//===----------------------------------------------------------------------===//
1016
1017typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1018
1019static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1020 return (CXCursorSet) setImpl;
1021}
1022static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1023 return (CXCursorSet_Impl*) set;
1024}
1025namespace llvm {
1026template<> struct DenseMapInfo<CXCursor> {
1027public:
1028 static inline CXCursor getEmptyKey() {
1029 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1030 }
1031 static inline CXCursor getTombstoneKey() {
1032 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1033 }
1034 static inline unsigned getHashValue(const CXCursor &cursor) {
1035 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1036 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1037 }
1038 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1039 return x.kind == y.kind &&
1040 x.data[0] == y.data[0] &&
1041 x.data[1] == y.data[1];
1042 }
1043};
1044}
1045
1046extern "C" {
1047CXCursorSet clang_createCXCursorSet() {
1048 return packCXCursorSet(new CXCursorSet_Impl());
1049}
1050
1051void clang_disposeCXCursorSet(CXCursorSet set) {
1052 delete unpackCXCursorSet(set);
1053}
1054
1055unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1056 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1057 if (!setImpl)
1058 return 0;
Ted Kremenek96bbe192013-04-24 07:25:40 +00001059 return setImpl->find(cursor) != setImpl->end();
Ted Kremenek017dd742013-04-24 07:17:12 +00001060}
1061
1062unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1063 // Do not insert invalid cursors into the set.
1064 if (cursor.kind >= CXCursor_FirstInvalid &&
1065 cursor.kind <= CXCursor_LastInvalid)
1066 return 1;
1067
1068 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1069 if (!setImpl)
1070 return 1;
1071 unsigned &entry = (*setImpl)[cursor];
1072 unsigned flag = entry == 0 ? 1 : 0;
1073 entry = 1;
1074 return flag;
1075}
1076
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001077CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1078 enum CXCursorKind kind = clang_getCursorKind(cursor);
1079 if (clang_isDeclaration(kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00001080 const Decl *decl = getCursorDecl(cursor);
1081 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001082 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregord1f09b42013-01-31 04:52:16 +00001083 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001084 CodeCompletionString *String
1085 = Result.CreateCodeCompletionString(unit->getASTContext(),
1086 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001087 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001088 unit->getCodeCompletionTUInfo(),
1089 true);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001090 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001091 }
1092 }
1093 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001094 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001095 const IdentifierInfo *MacroInfo = definition->getName();
1096 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenkob3958472013-01-14 00:36:42 +00001097 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001098 CodeCompletionString *String
1099 = Result.CreateCodeCompletionString(unit->getASTContext(),
1100 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001101 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001102 unit->getCodeCompletionTUInfo(),
1103 false);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001104 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001105 }
1106 return NULL;
1107}
Ted Kremenek8eece462012-04-30 19:33:45 +00001108} // end: extern C.
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001109
1110namespace {
1111 struct OverridenCursorsPool {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001112 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001113 std::vector<CursorVec*> AllCursors;
1114 std::vector<CursorVec*> AvailableCursors;
1115
1116 ~OverridenCursorsPool() {
1117 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1118 E = AllCursors.end(); I != E; ++I) {
1119 delete *I;
1120 }
1121 }
1122 };
1123}
1124
1125void *cxcursor::createOverridenCXCursorsPool() {
1126 return new OverridenCursorsPool();
1127}
1128
1129void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1130 delete static_cast<OverridenCursorsPool*>(pool);
1131}
Ted Kremenek8eece462012-04-30 19:33:45 +00001132
1133extern "C" {
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001134void clang_getOverriddenCursors(CXCursor cursor,
1135 CXCursor **overridden,
1136 unsigned *num_overridden) {
1137 if (overridden)
1138 *overridden = 0;
1139 if (num_overridden)
1140 *num_overridden = 0;
1141
1142 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1143
1144 if (!overridden || !num_overridden || !TU)
1145 return;
1146
1147 if (!clang_isDeclaration(cursor.kind))
1148 return;
1149
1150 OverridenCursorsPool &pool =
1151 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1152
1153 OverridenCursorsPool::CursorVec *Vec = 0;
1154
1155 if (!pool.AvailableCursors.empty()) {
1156 Vec = pool.AvailableCursors.back();
1157 pool.AvailableCursors.pop_back();
1158 }
1159 else {
1160 Vec = new OverridenCursorsPool::CursorVec();
1161 pool.AllCursors.push_back(Vec);
1162 }
1163
1164 // Clear out the vector, but don't free the memory contents. This
1165 // reduces malloc() traffic.
1166 Vec->clear();
1167
1168 // Use the first entry to contain a back reference to the vector.
1169 // This is a complete hack.
1170 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1171 backRefCursor.data[0] = Vec;
1172 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1173 Vec->push_back(backRefCursor);
1174
1175 // Get the overriden cursors.
1176 cxcursor::getOverriddenCursors(cursor, *Vec);
1177
1178 // Did we get any overriden cursors? If not, return Vec to the pool
1179 // of available cursor vectors.
1180 if (Vec->size() == 1) {
1181 pool.AvailableCursors.push_back(Vec);
1182 return;
1183 }
1184
1185 // Now tell the caller about the overriden cursors.
1186 assert(Vec->size() > 1);
1187 *overridden = &((*Vec)[1]);
1188 *num_overridden = Vec->size() - 1;
1189}
1190
1191void clang_disposeOverriddenCursors(CXCursor *overridden) {
1192 if (!overridden)
1193 return;
1194
1195 // Use pointer arithmetic to get back the first faux entry
1196 // which has a back-reference to the TU and the vector.
1197 --overridden;
1198 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001199 static_cast<OverridenCursorsPool::CursorVec *>(
1200 const_cast<void *>(overridden->data[0]));
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001201 CXTranslationUnit TU = getCursorTU(*overridden);
1202
1203 assert(Vec && TU);
1204
1205 OverridenCursorsPool &pool =
1206 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1207
1208 pool.AvailableCursors.push_back(Vec);
1209}
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001210
1211int clang_Cursor_isDynamicCall(CXCursor C) {
1212 const Expr *E = 0;
1213 if (clang_isExpression(C.kind))
1214 E = getCursorExpr(C);
1215 if (!E)
1216 return 0;
1217
1218 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1219 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1220
1221 const MemberExpr *ME = 0;
1222 if (isa<MemberExpr>(E))
1223 ME = cast<MemberExpr>(E);
1224 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1225 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1226
1227 if (ME) {
1228 if (const CXXMethodDecl *
1229 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1230 return MD->isVirtual() && !ME->hasQualifier();
1231 }
1232
1233 return 0;
1234}
1235
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001236CXType clang_Cursor_getReceiverType(CXCursor C) {
1237 CXTranslationUnit TU = cxcursor::getCursorTU(C);
1238 const Expr *E = 0;
1239 if (clang_isExpression(C.kind))
1240 E = getCursorExpr(C);
1241
1242 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1243 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1244
1245 return cxtype::MakeCXType(QualType(), TU);
1246}
1247
Ted Kremenekeca099b2010-12-08 23:43:14 +00001248} // end: extern "C"