blob: d682986b3f3c57a3685bb9b7f5f4e3833efa0837 [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;
Ted Kremeneke77f4432010-02-18 03:09:07 +000053 }
54
55 return CXCursor_UnexposedAttr;
56}
57
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000058CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
Ted Kremeneka60ed472010-11-16 08:15:36 +000059 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000060 assert(A && Parent && TU && "Invalid arguments!");
Dmitri Gribenko33156182013-01-11 21:06:06 +000061 CXCursor C = { GetCursorKind(A), 0, { Parent, A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000062 return C;
63}
64
Dmitri Gribenko67812b22013-01-11 21:01:49 +000065CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000066 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000067 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000068 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000069
70 CXCursorKind K = getCursorKindForDecl(D);
71
72 if (K == CXCursor_ObjCClassMethodDecl ||
73 K == CXCursor_ObjCInstanceMethodDecl) {
74 int SelectorIdIndex = -1;
75 // Check if cursor points to a selector id.
76 if (RegionOfInterest.isValid() &&
77 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
78 SmallVector<SourceLocation, 16> SelLocs;
79 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
Craig Topper09d19ef2013-07-04 03:08:24 +000080 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000081 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
82 if (I != SelLocs.end())
83 SelectorIdIndex = I - SelLocs.begin();
84 }
85 CXCursor C = { K, SelectorIdIndex,
86 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
87 return C;
88 }
89
90 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000091 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000092}
93
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000094CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
Dmitri Gribenko67812b22013-01-11 21:01:49 +000095 CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000096 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000097 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000098 CXCursorKind K = CXCursor_NotImplemented;
99
100 switch (S->getStmtClass()) {
101 case Stmt::NoStmtClass:
102 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000103
Douglas Gregor97b98722010-01-19 23:20:36 +0000104 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000105 K = CXCursor_CaseStmt;
106 break;
107
Douglas Gregor97b98722010-01-19 23:20:36 +0000108 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000109 K = CXCursor_DefaultStmt;
110 break;
111
112 case Stmt::IfStmtClass:
113 K = CXCursor_IfStmt;
114 break;
115
116 case Stmt::SwitchStmtClass:
117 K = CXCursor_SwitchStmt;
118 break;
119
120 case Stmt::WhileStmtClass:
121 K = CXCursor_WhileStmt;
122 break;
123
124 case Stmt::DoStmtClass:
125 K = CXCursor_DoStmt;
126 break;
127
128 case Stmt::ForStmtClass:
129 K = CXCursor_ForStmt;
130 break;
131
132 case Stmt::GotoStmtClass:
133 K = CXCursor_GotoStmt;
134 break;
135
Douglas Gregor97b98722010-01-19 23:20:36 +0000136 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000137 K = CXCursor_IndirectGotoStmt;
138 break;
139
140 case Stmt::ContinueStmtClass:
141 K = CXCursor_ContinueStmt;
142 break;
143
144 case Stmt::BreakStmtClass:
145 K = CXCursor_BreakStmt;
146 break;
147
148 case Stmt::ReturnStmtClass:
149 K = CXCursor_ReturnStmt;
150 break;
151
Chad Rosierdf5faf52012-08-25 00:11:56 +0000152 case Stmt::GCCAsmStmtClass:
153 K = CXCursor_GCCAsmStmt;
Douglas Gregor42b29842011-10-05 19:00:14 +0000154 break;
Chad Rosier8cd64b42012-06-11 20:47:18 +0000155
156 case Stmt::MSAsmStmtClass:
157 K = CXCursor_MSAsmStmt;
158 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000159
160 case Stmt::ObjCAtTryStmtClass:
161 K = CXCursor_ObjCAtTryStmt;
162 break;
163
164 case Stmt::ObjCAtCatchStmtClass:
165 K = CXCursor_ObjCAtCatchStmt;
166 break;
167
168 case Stmt::ObjCAtFinallyStmtClass:
169 K = CXCursor_ObjCAtFinallyStmt;
170 break;
171
172 case Stmt::ObjCAtThrowStmtClass:
173 K = CXCursor_ObjCAtThrowStmt;
174 break;
175
176 case Stmt::ObjCAtSynchronizedStmtClass:
177 K = CXCursor_ObjCAtSynchronizedStmt;
178 break;
179
180 case Stmt::ObjCAutoreleasePoolStmtClass:
181 K = CXCursor_ObjCAutoreleasePoolStmt;
182 break;
183
Douglas Gregor97b98722010-01-19 23:20:36 +0000184 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000185 K = CXCursor_ObjCForCollectionStmt;
186 break;
187
Douglas Gregor97b98722010-01-19 23:20:36 +0000188 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000189 K = CXCursor_CXXCatchStmt;
190 break;
191
192 case Stmt::CXXTryStmtClass:
193 K = CXCursor_CXXTryStmt;
194 break;
195
196 case Stmt::CXXForRangeStmtClass:
197 K = CXCursor_CXXForRangeStmt;
198 break;
199
John Wiegley28bbe4b2011-04-28 01:08:34 +0000200 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000201 K = CXCursor_SEHTryStmt;
202 break;
203
John Wiegley28bbe4b2011-04-28 01:08:34 +0000204 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000205 K = CXCursor_SEHExceptStmt;
206 break;
207
John Wiegley28bbe4b2011-04-28 01:08:34 +0000208 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000209 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000210 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000211
John Wiegley21ff2e52011-04-28 00:16:57 +0000212 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000213 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000214 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000215 case Stmt::BinaryConditionalOperatorClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000216 case Stmt::TypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000217 case Stmt::CXXBindTemporaryExprClass:
218 case Stmt::CXXDefaultArgExprClass:
Richard Smithc3bf52c2013-04-20 22:23:05 +0000219 case Stmt::CXXDefaultInitExprClass:
Richard Smith7c3e6152013-06-12 22:31:48 +0000220 case Stmt::CXXStdInitializerListExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000221 case Stmt::CXXScalarValueInitExprClass:
222 case Stmt::CXXUuidofExprClass:
223 case Stmt::ChooseExprClass:
224 case Stmt::DesignatedInitExprClass:
225 case Stmt::ExprWithCleanupsClass:
226 case Stmt::ExpressionTraitExprClass:
227 case Stmt::ExtVectorElementExprClass:
228 case Stmt::ImplicitCastExprClass:
229 case Stmt::ImplicitValueInitExprClass:
230 case Stmt::MaterializeTemporaryExprClass:
231 case Stmt::ObjCIndirectCopyRestoreExprClass:
232 case Stmt::OffsetOfExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000233 case Stmt::ParenListExprClass:
234 case Stmt::PredefinedExprClass:
235 case Stmt::ShuffleVectorExprClass:
Hal Finkel414a1bd2013-09-18 03:29:45 +0000236 case Stmt::ConvertVectorExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000237 case Stmt::UnaryExprOrTypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000238 case Stmt::VAArgExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000239 case Stmt::ObjCArrayLiteralClass:
240 case Stmt::ObjCDictionaryLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +0000241 case Stmt::ObjCBoxedExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000242 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000243 K = CXCursor_UnexposedExpr;
244 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000245
John McCall4b9c2d22011-11-06 09:01:30 +0000246 case Stmt::OpaqueValueExprClass:
247 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
248 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
249 K = CXCursor_UnexposedExpr;
250 break;
251
252 case Stmt::PseudoObjectExprClass:
253 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
254 Parent, TU, RegionOfInterest);
255
Douglas Gregor42b29842011-10-05 19:00:14 +0000256 case Stmt::CompoundStmtClass:
257 K = CXCursor_CompoundStmt;
258 break;
Richard Smith534986f2012-04-14 00:33:13 +0000259
Douglas Gregor42b29842011-10-05 19:00:14 +0000260 case Stmt::NullStmtClass:
261 K = CXCursor_NullStmt;
262 break;
Richard Smith534986f2012-04-14 00:33:13 +0000263
Douglas Gregor42b29842011-10-05 19:00:14 +0000264 case Stmt::LabelStmtClass:
265 K = CXCursor_LabelStmt;
266 break;
Richard Smith534986f2012-04-14 00:33:13 +0000267
268 case Stmt::AttributedStmtClass:
269 K = CXCursor_UnexposedStmt;
270 break;
271
Douglas Gregor42b29842011-10-05 19:00:14 +0000272 case Stmt::DeclStmtClass:
273 K = CXCursor_DeclStmt;
274 break;
Richard Smith534986f2012-04-14 00:33:13 +0000275
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000276 case Stmt::CapturedStmtClass:
277 K = CXCursor_UnexposedStmt;
278 break;
279
Douglas Gregor42b29842011-10-05 19:00:14 +0000280 case Stmt::IntegerLiteralClass:
281 K = CXCursor_IntegerLiteral;
282 break;
283
284 case Stmt::FloatingLiteralClass:
285 K = CXCursor_FloatingLiteral;
286 break;
287
288 case Stmt::ImaginaryLiteralClass:
289 K = CXCursor_ImaginaryLiteral;
290 break;
291
292 case Stmt::StringLiteralClass:
293 K = CXCursor_StringLiteral;
294 break;
295
296 case Stmt::CharacterLiteralClass:
297 K = CXCursor_CharacterLiteral;
298 break;
299
300 case Stmt::ParenExprClass:
301 K = CXCursor_ParenExpr;
302 break;
303
304 case Stmt::UnaryOperatorClass:
305 K = CXCursor_UnaryOperator;
306 break;
Richard Smith534986f2012-04-14 00:33:13 +0000307
Douglas Gregor42b29842011-10-05 19:00:14 +0000308 case Stmt::CXXNoexceptExprClass:
309 K = CXCursor_UnaryExpr;
310 break;
311
312 case Stmt::ArraySubscriptExprClass:
313 K = CXCursor_ArraySubscriptExpr;
314 break;
315
316 case Stmt::BinaryOperatorClass:
317 K = CXCursor_BinaryOperator;
318 break;
319
320 case Stmt::CompoundAssignOperatorClass:
321 K = CXCursor_CompoundAssignOperator;
322 break;
323
324 case Stmt::ConditionalOperatorClass:
325 K = CXCursor_ConditionalOperator;
326 break;
327
328 case Stmt::CStyleCastExprClass:
329 K = CXCursor_CStyleCastExpr;
330 break;
331
332 case Stmt::CompoundLiteralExprClass:
333 K = CXCursor_CompoundLiteralExpr;
334 break;
335
336 case Stmt::InitListExprClass:
337 K = CXCursor_InitListExpr;
338 break;
339
340 case Stmt::AddrLabelExprClass:
341 K = CXCursor_AddrLabelExpr;
342 break;
343
344 case Stmt::StmtExprClass:
345 K = CXCursor_StmtExpr;
346 break;
347
348 case Stmt::GenericSelectionExprClass:
349 K = CXCursor_GenericSelectionExpr;
350 break;
351
352 case Stmt::GNUNullExprClass:
353 K = CXCursor_GNUNullExpr;
354 break;
355
356 case Stmt::CXXStaticCastExprClass:
357 K = CXCursor_CXXStaticCastExpr;
358 break;
359
360 case Stmt::CXXDynamicCastExprClass:
361 K = CXCursor_CXXDynamicCastExpr;
362 break;
363
364 case Stmt::CXXReinterpretCastExprClass:
365 K = CXCursor_CXXReinterpretCastExpr;
366 break;
367
368 case Stmt::CXXConstCastExprClass:
369 K = CXCursor_CXXConstCastExpr;
370 break;
371
372 case Stmt::CXXFunctionalCastExprClass:
373 K = CXCursor_CXXFunctionalCastExpr;
374 break;
375
376 case Stmt::CXXTypeidExprClass:
377 K = CXCursor_CXXTypeidExpr;
378 break;
379
380 case Stmt::CXXBoolLiteralExprClass:
381 K = CXCursor_CXXBoolLiteralExpr;
382 break;
383
384 case Stmt::CXXNullPtrLiteralExprClass:
385 K = CXCursor_CXXNullPtrLiteralExpr;
386 break;
387
388 case Stmt::CXXThisExprClass:
389 K = CXCursor_CXXThisExpr;
390 break;
391
392 case Stmt::CXXThrowExprClass:
393 K = CXCursor_CXXThrowExpr;
394 break;
395
396 case Stmt::CXXNewExprClass:
397 K = CXCursor_CXXNewExpr;
398 break;
399
400 case Stmt::CXXDeleteExprClass:
401 K = CXCursor_CXXDeleteExpr;
402 break;
403
404 case Stmt::ObjCStringLiteralClass:
405 K = CXCursor_ObjCStringLiteral;
406 break;
407
408 case Stmt::ObjCEncodeExprClass:
409 K = CXCursor_ObjCEncodeExpr;
410 break;
411
412 case Stmt::ObjCSelectorExprClass:
413 K = CXCursor_ObjCSelectorExpr;
414 break;
415
416 case Stmt::ObjCProtocolExprClass:
417 K = CXCursor_ObjCProtocolExpr;
418 break;
Ted Kremenekb3f75422012-03-06 20:06:06 +0000419
420 case Stmt::ObjCBoolLiteralExprClass:
421 K = CXCursor_ObjCBoolLiteralExpr;
422 break;
423
Douglas Gregor42b29842011-10-05 19:00:14 +0000424 case Stmt::ObjCBridgedCastExprClass:
425 K = CXCursor_ObjCBridgedCastExpr;
426 break;
427
428 case Stmt::BlockExprClass:
429 K = CXCursor_BlockExpr;
430 break;
431
432 case Stmt::PackExpansionExprClass:
433 K = CXCursor_PackExpansionExpr;
434 break;
435
436 case Stmt::SizeOfPackExprClass:
437 K = CXCursor_SizeOfPackExpr;
438 break;
439
Argyrios Kyrtzidisedab0472013-04-23 17:57:17 +0000440 case Stmt::DeclRefExprClass:
441 if (const ImplicitParamDecl *IPD =
442 dyn_cast_or_null<ImplicitParamDecl>(cast<DeclRefExpr>(S)->getDecl())) {
443 if (const ObjCMethodDecl *MD =
444 dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
445 if (MD->getSelfDecl() == IPD) {
446 K = CXCursor_ObjCSelfExpr;
447 break;
448 }
449 }
450 }
451
452 K = CXCursor_DeclRefExpr;
453 break;
454
Douglas Gregor42b29842011-10-05 19:00:14 +0000455 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000456 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000457 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +0000458 case Stmt::FunctionParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000459 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000460 K = CXCursor_DeclRefExpr;
461 break;
462
Douglas Gregor42b29842011-10-05 19:00:14 +0000463 case Stmt::CXXDependentScopeMemberExprClass:
464 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000465 case Stmt::MemberExprClass:
John McCall76da55d2013-04-16 07:28:30 +0000466 case Stmt::MSPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000467 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000468 case Stmt::ObjCIvarRefExprClass:
469 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000470 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000471 K = CXCursor_MemberRefExpr;
472 break;
473
474 case Stmt::CallExprClass:
475 case Stmt::CXXOperatorCallExprClass:
476 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000477 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000478 case Stmt::CXXConstructExprClass:
479 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000480 case Stmt::CXXUnresolvedConstructExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000481 case Stmt::UserDefinedLiteralClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000482 K = CXCursor_CallExpr;
483 break;
484
Douglas Gregor011d8b92012-02-15 00:54:55 +0000485 case Stmt::LambdaExprClass:
486 K = CXCursor_LambdaExpr;
487 break;
488
Douglas Gregorba0513d2011-10-25 01:33:02 +0000489 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000490 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000491 int SelectorIdIndex = -1;
492 // Check if cursor points to a selector id.
493 if (RegionOfInterest.isValid() &&
494 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
495 SmallVector<SourceLocation, 16> SelLocs;
496 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
Craig Topper09d19ef2013-07-04 03:08:24 +0000497 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000498 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
499 if (I != SelLocs.end())
500 SelectorIdIndex = I - SelLocs.begin();
501 }
502 CXCursor C = { K, 0, { Parent, S, TU } };
503 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000504 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000505
506 case Stmt::MSDependentExistsStmtClass:
507 K = CXCursor_UnexposedStmt;
508 break;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000509 case Stmt::OMPParallelDirectiveClass:
510 K = CXCursor_OMPParallelDirective;
511 break;
Stephen Hines651f13c2014-04-23 16:59:28 -0700512 case Stmt::OMPSimdDirectiveClass:
513 K = CXCursor_OMPSimdDirective;
514 break;
Douglas Gregorba0513d2011-10-25 01:33:02 +0000515 }
Stephen Hines651f13c2014-04-23 16:59:28 -0700516
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000517 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000518 return C;
519}
520
Douglas Gregor2e331b92010-01-16 14:00:32 +0000521CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000522 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000523 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000524 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000525 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000526 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000527 return C;
528}
529
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000530std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor2e331b92010-01-16 14:00:32 +0000531cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
532 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000533 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000534 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor2e331b92010-01-16 14:00:32 +0000535}
536
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000537CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000538 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000539 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000540 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000541 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000542 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000543 return C;
544}
545
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000546std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000547cxcursor::getCursorObjCProtocolRef(CXCursor C) {
548 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000549 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000550 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000551}
552
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000553CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000554 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000555 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000556 // 'Class' can be null for invalid code.
557 if (!Class)
558 return MakeCXCursorInvalid(CXCursor_InvalidCode);
559 assert(TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000560 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000561 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000562 return C;
563}
564
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000565std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor1adb0822010-01-16 17:14:40 +0000566cxcursor::getCursorObjCClassRef(CXCursor C) {
567 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000568 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000569 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1adb0822010-01-16 17:14:40 +0000570}
571
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000572CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000573 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000574 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000575 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000576 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000577 return C;
578}
579
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000580std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000581cxcursor::getCursorTypeRef(CXCursor C) {
582 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000583 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000584 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000585}
586
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000587CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000588 SourceLocation Loc,
589 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000590 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000591 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000592 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000593 return C;
594}
595
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000596std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregor0b36e612010-08-31 20:37:03 +0000597cxcursor::getCursorTemplateRef(CXCursor C) {
598 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000599 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000600 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor0b36e612010-08-31 20:37:03 +0000601}
602
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000603CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
604 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000605 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000606
607 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
608 "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000609 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000610 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000611 return C;
612}
613
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000614std::pair<const NamedDecl *, SourceLocation>
Douglas Gregor69319002010-08-31 23:48:11 +0000615cxcursor::getCursorNamespaceRef(CXCursor C) {
616 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000617 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000618 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor69319002010-08-31 23:48:11 +0000619}
620
Douglas Gregor011d8b92012-02-15 00:54:55 +0000621CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
622 CXTranslationUnit TU) {
623
624 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000625 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000626 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor011d8b92012-02-15 00:54:55 +0000627 return C;
628}
629
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000630std::pair<const VarDecl *, SourceLocation>
Douglas Gregor011d8b92012-02-15 00:54:55 +0000631cxcursor::getCursorVariableRef(CXCursor C) {
632 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000633 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000634 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor011d8b92012-02-15 00:54:55 +0000635}
636
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000637CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000638 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000639
640 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000641 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000642 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000643 return C;
644}
645
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000646std::pair<const FieldDecl *, SourceLocation>
Douglas Gregora67e03f2010-09-09 21:42:20 +0000647cxcursor::getCursorMemberRef(CXCursor C) {
648 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000649 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000650 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora67e03f2010-09-09 21:42:20 +0000651}
652
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000653CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000654 CXTranslationUnit TU){
Dmitri Gribenko33156182013-01-11 21:06:06 +0000655 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000656 return C;
657}
658
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000659const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenek3064ef92010-08-27 21:34:58 +0000660 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000661 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000662}
663
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000664CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000665 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000666 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000667 { Range.getBegin().getPtrEncoding(),
668 Range.getEnd().getPtrEncoding(),
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000669 TU }
670 };
671 return C;
672}
673
674SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
675 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000676 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
677 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000678 ASTUnit *TU = getCursorASTUnit(C);
679 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000680}
681
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000682CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000683 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000684 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000685 return C;
686}
687
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000688const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000689 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000690 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor572feb22010-03-18 18:04:21 +0000691}
692
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000693CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
694 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000695 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000696 return C;
697}
698
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000699CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
700 SourceLocation Loc,
701 CXTranslationUnit TU) {
702 assert(Loc.isValid());
703 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
704 return C;
705}
706
707const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
708 if (isPseudo())
709 return getAsMacroDefinition()->getName();
710 return getAsMacroExpansion()->getName();
711}
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000712const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000713 if (isPseudo())
714 return getAsMacroDefinition();
715 return getAsMacroExpansion()->getDefinition();
716}
717SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
718 if (isPseudo())
719 return getPseudoLoc();
720 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +0000721}
722
Douglas Gregorecdcb882010-10-20 22:00:55 +0000723CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000724 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000725 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000726 return C;
727}
728
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000729const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000730 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000731 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000732}
733
Douglas Gregor36897b02010-09-10 00:22:18 +0000734CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000735 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000736
737 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000738 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000739 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000740 return C;
741}
742
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000743std::pair<const LabelStmt *, SourceLocation>
Douglas Gregor36897b02010-09-10 00:22:18 +0000744cxcursor::getCursorLabelRef(CXCursor C) {
745 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000746 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000747 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor36897b02010-09-10 00:22:18 +0000748}
749
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000750CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000751 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000752 assert(E && TU && "Invalid arguments!");
753 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000754 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000755 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000756 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000757 { Storage.getOpaqueValue(), RawLoc, TU }
758 };
759 return C;
760}
761
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000762CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000763 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000764 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000765 assert(D && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000766 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000767 OverloadedDeclRefStorage Storage(D);
768 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000769 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000770 { Storage.getOpaqueValue(), RawLoc, TU }
771 };
772 return C;
773}
774
775CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
776 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000777 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000778 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000779 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000780 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
781 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000782 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000783 { Storage.getOpaqueValue(), RawLoc, TU }
784 };
785 return C;
786}
787
788std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
789cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
790 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000791 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
792 const_cast<void *>(C.data[0])),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000793 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000794}
795
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000796const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
797 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000798}
799
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000800const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000801 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
802}
803
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000804const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000805 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000806 Cursor.kind == CXCursor_ObjCProtocolRef ||
807 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000808 return 0;
809
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000810 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000811}
812
Dmitri Gribenko7d914382013-01-26 18:08:08 +0000813const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
814 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremenek95f33552010-08-26 01:42:22 +0000815}
816
Dmitri Gribenko404628c2013-01-26 18:12:08 +0000817const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
818 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000819}
820
Douglas Gregorf46034a2010-01-18 23:41:10 +0000821ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000822 return getCursorASTUnit(Cursor)->getASTContext();
823}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000824
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000825ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko46f92522013-01-11 19:28:44 +0000826 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000827 if (!TU)
828 return 0;
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000829 return cxtu::getASTUnit(TU);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000830}
831
832CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000833 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregor283cae32010-01-15 21:56:13 +0000834}
835
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000836void cxcursor::getOverriddenCursors(CXCursor cursor,
837 SmallVectorImpl<CXCursor> &overridden) {
838 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000839 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000840 if (!D)
841 return;
842
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000843 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000844 SmallVector<const NamedDecl *, 8> OverDecls;
845 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000846
Craig Topper09d19ef2013-07-04 03:08:24 +0000847 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000848 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko05756dc2013-01-14 00:46:27 +0000849 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000850 }
851}
852
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000853std::pair<int, SourceLocation>
854cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
855 if (cursor.kind == CXCursor_ObjCMessageExpr) {
856 if (cursor.xdata != -1)
857 return std::make_pair(cursor.xdata,
858 cast<ObjCMessageExpr>(getCursorExpr(cursor))
859 ->getSelectorLoc(cursor.xdata));
860 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
861 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
862 if (cursor.xdata != -1)
863 return std::make_pair(cursor.xdata,
864 cast<ObjCMethodDecl>(getCursorDecl(cursor))
865 ->getSelectorLoc(cursor.xdata));
866 }
867
868 return std::make_pair(-1, SourceLocation());
869}
870
871CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
872 CXCursor newCursor = cursor;
873
874 if (cursor.kind == CXCursor_ObjCMessageExpr) {
875 if (SelIdx == -1 ||
876 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
877 ->getNumSelectorLocs())
878 newCursor.xdata = -1;
879 else
880 newCursor.xdata = SelIdx;
881 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
882 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
883 if (SelIdx == -1 ||
884 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
885 ->getNumSelectorLocs())
886 newCursor.xdata = -1;
887 else
888 newCursor.xdata = SelIdx;
889 }
890
891 return newCursor;
892}
893
894CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
895 if (cursor.kind != CXCursor_CallExpr)
896 return cursor;
897
898 if (cursor.xdata == 0)
899 return cursor;
900
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000901 const Expr *E = getCursorExpr(cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000902 TypeSourceInfo *Type = 0;
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000903 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000904 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
905 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000906 } else if (const CXXTemporaryObjectExpr *Tmp =
907 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000908 Type = Tmp->getTypeSourceInfo();
909 }
910
911 if (!Type)
912 return cursor;
913
914 CXTranslationUnit TU = getCursorTU(cursor);
915 QualType Ty = Type->getType();
916 TypeLoc TL = Type->getTypeLoc();
917 SourceLocation Loc = TL.getBeginLoc();
918
919 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
920 Ty = ElabT->getNamedType();
David Blaikie39e6ab42013-02-18 22:06:02 +0000921 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000922 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
923 }
924
925 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
926 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
927 if (const TagType *Tag = Ty->getAs<TagType>())
928 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
929 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
930 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
931
932 return cursor;
933}
934
Douglas Gregor283cae32010-01-15 21:56:13 +0000935bool cxcursor::operator==(CXCursor X, CXCursor Y) {
936 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
937 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000938}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000939
940// FIXME: Remove once we can model DeclGroups and their appropriate ranges
941// properly in the ASTs.
942bool cxcursor::isFirstInDeclGroup(CXCursor C) {
943 assert(clang_isDeclaration(C.kind));
944 return ((uintptr_t) (C.data[1])) != 0;
945}
946
Ted Kremenekeca099b2010-12-08 23:43:14 +0000947//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000948// libclang CXCursor APIs
949//===----------------------------------------------------------------------===//
950
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000951extern "C" {
952
953int clang_Cursor_isNull(CXCursor cursor) {
954 return clang_equalCursors(cursor, clang_getNullCursor());
955}
956
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000957CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
958 return getCursorTU(cursor);
959}
960
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000961int clang_Cursor_getNumArguments(CXCursor C) {
962 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000963 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000964 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
965 return MD->param_size();
966 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
967 return FD->param_size();
968 }
969
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000970 if (clang_isExpression(C.kind)) {
971 const Expr *E = cxcursor::getCursorExpr(C);
972 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
973 return CE->getNumArgs();
974 }
975 }
976
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000977 return -1;
978}
979
980CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
981 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000982 const Decl *D = cxcursor::getCursorDecl(C);
983 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000984 if (i < MD->param_size())
985 return cxcursor::MakeCXCursor(MD->param_begin()[i],
986 cxcursor::getCursorTU(C));
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000987 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000988 if (i < FD->param_size())
989 return cxcursor::MakeCXCursor(FD->param_begin()[i],
990 cxcursor::getCursorTU(C));
991 }
992 }
993
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000994 if (clang_isExpression(C.kind)) {
995 const Expr *E = cxcursor::getCursorExpr(C);
996 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
997 if (i < CE->getNumArgs()) {
998 return cxcursor::MakeCXCursor(CE->getArg(i),
999 getCursorDecl(C),
1000 cxcursor::getCursorTU(C));
1001 }
1002 }
1003 }
1004
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001005 return clang_getNullCursor();
1006}
1007
Ted Kremenek017dd742013-04-24 07:17:12 +00001008} // end: extern "C"
1009
1010//===----------------------------------------------------------------------===//
1011// CXCursorSet.
1012//===----------------------------------------------------------------------===//
1013
1014typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1015
1016static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1017 return (CXCursorSet) setImpl;
1018}
1019static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1020 return (CXCursorSet_Impl*) set;
1021}
1022namespace llvm {
1023template<> struct DenseMapInfo<CXCursor> {
1024public:
1025 static inline CXCursor getEmptyKey() {
1026 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1027 }
1028 static inline CXCursor getTombstoneKey() {
1029 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1030 }
1031 static inline unsigned getHashValue(const CXCursor &cursor) {
1032 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1033 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1034 }
1035 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1036 return x.kind == y.kind &&
1037 x.data[0] == y.data[0] &&
1038 x.data[1] == y.data[1];
1039 }
1040};
1041}
1042
1043extern "C" {
1044CXCursorSet clang_createCXCursorSet() {
1045 return packCXCursorSet(new CXCursorSet_Impl());
1046}
1047
1048void clang_disposeCXCursorSet(CXCursorSet set) {
1049 delete unpackCXCursorSet(set);
1050}
1051
1052unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1053 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1054 if (!setImpl)
1055 return 0;
Ted Kremenek96bbe192013-04-24 07:25:40 +00001056 return setImpl->find(cursor) != setImpl->end();
Ted Kremenek017dd742013-04-24 07:17:12 +00001057}
1058
1059unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1060 // Do not insert invalid cursors into the set.
1061 if (cursor.kind >= CXCursor_FirstInvalid &&
1062 cursor.kind <= CXCursor_LastInvalid)
1063 return 1;
1064
1065 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1066 if (!setImpl)
1067 return 1;
1068 unsigned &entry = (*setImpl)[cursor];
1069 unsigned flag = entry == 0 ? 1 : 0;
1070 entry = 1;
1071 return flag;
1072}
1073
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001074CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1075 enum CXCursorKind kind = clang_getCursorKind(cursor);
1076 if (clang_isDeclaration(kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00001077 const Decl *decl = getCursorDecl(cursor);
1078 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001079 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregord1f09b42013-01-31 04:52:16 +00001080 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001081 CodeCompletionString *String
1082 = Result.CreateCodeCompletionString(unit->getASTContext(),
1083 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001084 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001085 unit->getCodeCompletionTUInfo(),
1086 true);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001087 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001088 }
1089 }
1090 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001091 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001092 const IdentifierInfo *MacroInfo = definition->getName();
1093 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenkob3958472013-01-14 00:36:42 +00001094 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001095 CodeCompletionString *String
1096 = Result.CreateCodeCompletionString(unit->getASTContext(),
1097 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001098 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001099 unit->getCodeCompletionTUInfo(),
1100 false);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001101 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001102 }
1103 return NULL;
1104}
Ted Kremenek8eece462012-04-30 19:33:45 +00001105} // end: extern C.
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001106
1107namespace {
1108 struct OverridenCursorsPool {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001109 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001110 std::vector<CursorVec*> AllCursors;
1111 std::vector<CursorVec*> AvailableCursors;
1112
1113 ~OverridenCursorsPool() {
1114 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1115 E = AllCursors.end(); I != E; ++I) {
1116 delete *I;
1117 }
1118 }
1119 };
1120}
1121
1122void *cxcursor::createOverridenCXCursorsPool() {
1123 return new OverridenCursorsPool();
1124}
1125
1126void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1127 delete static_cast<OverridenCursorsPool*>(pool);
1128}
Ted Kremenek8eece462012-04-30 19:33:45 +00001129
1130extern "C" {
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001131void clang_getOverriddenCursors(CXCursor cursor,
1132 CXCursor **overridden,
1133 unsigned *num_overridden) {
1134 if (overridden)
1135 *overridden = 0;
1136 if (num_overridden)
1137 *num_overridden = 0;
1138
1139 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1140
1141 if (!overridden || !num_overridden || !TU)
1142 return;
1143
1144 if (!clang_isDeclaration(cursor.kind))
1145 return;
1146
1147 OverridenCursorsPool &pool =
1148 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1149
1150 OverridenCursorsPool::CursorVec *Vec = 0;
1151
1152 if (!pool.AvailableCursors.empty()) {
1153 Vec = pool.AvailableCursors.back();
1154 pool.AvailableCursors.pop_back();
1155 }
1156 else {
1157 Vec = new OverridenCursorsPool::CursorVec();
1158 pool.AllCursors.push_back(Vec);
1159 }
1160
1161 // Clear out the vector, but don't free the memory contents. This
1162 // reduces malloc() traffic.
1163 Vec->clear();
1164
1165 // Use the first entry to contain a back reference to the vector.
1166 // This is a complete hack.
1167 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1168 backRefCursor.data[0] = Vec;
1169 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1170 Vec->push_back(backRefCursor);
1171
1172 // Get the overriden cursors.
1173 cxcursor::getOverriddenCursors(cursor, *Vec);
1174
1175 // Did we get any overriden cursors? If not, return Vec to the pool
1176 // of available cursor vectors.
1177 if (Vec->size() == 1) {
1178 pool.AvailableCursors.push_back(Vec);
1179 return;
1180 }
1181
1182 // Now tell the caller about the overriden cursors.
1183 assert(Vec->size() > 1);
1184 *overridden = &((*Vec)[1]);
1185 *num_overridden = Vec->size() - 1;
1186}
1187
1188void clang_disposeOverriddenCursors(CXCursor *overridden) {
1189 if (!overridden)
1190 return;
1191
1192 // Use pointer arithmetic to get back the first faux entry
1193 // which has a back-reference to the TU and the vector.
1194 --overridden;
1195 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001196 static_cast<OverridenCursorsPool::CursorVec *>(
1197 const_cast<void *>(overridden->data[0]));
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001198 CXTranslationUnit TU = getCursorTU(*overridden);
1199
1200 assert(Vec && TU);
1201
1202 OverridenCursorsPool &pool =
1203 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1204
1205 pool.AvailableCursors.push_back(Vec);
1206}
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001207
1208int clang_Cursor_isDynamicCall(CXCursor C) {
1209 const Expr *E = 0;
1210 if (clang_isExpression(C.kind))
1211 E = getCursorExpr(C);
1212 if (!E)
1213 return 0;
1214
1215 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1216 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1217
1218 const MemberExpr *ME = 0;
1219 if (isa<MemberExpr>(E))
1220 ME = cast<MemberExpr>(E);
1221 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1222 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1223
1224 if (ME) {
1225 if (const CXXMethodDecl *
1226 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1227 return MD->isVirtual() && !ME->hasQualifier();
1228 }
1229
1230 return 0;
1231}
1232
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001233CXType clang_Cursor_getReceiverType(CXCursor C) {
1234 CXTranslationUnit TU = cxcursor::getCursorTU(C);
1235 const Expr *E = 0;
1236 if (clang_isExpression(C.kind))
1237 E = getCursorExpr(C);
1238
1239 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1240 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1241
1242 return cxtype::MakeCXType(QualType(), TU);
1243}
1244
Ted Kremenekeca099b2010-12-08 23:43:14 +00001245} // end: extern "C"