blob: 5aab94403d4fa5d40f777a5d5528c222b9e0533f [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;
Ted Kremeneke77f4432010-02-18 03:09:07 +000052 }
53
54 return CXCursor_UnexposedAttr;
55}
56
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000057CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
Ted Kremeneka60ed472010-11-16 08:15:36 +000058 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000059 assert(A && Parent && TU && "Invalid arguments!");
Dmitri Gribenko33156182013-01-11 21:06:06 +000060 CXCursor C = { GetCursorKind(A), 0, { Parent, A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000061 return C;
62}
63
Dmitri Gribenko67812b22013-01-11 21:01:49 +000064CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000065 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000066 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000067 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000068
69 CXCursorKind K = getCursorKindForDecl(D);
70
71 if (K == CXCursor_ObjCClassMethodDecl ||
72 K == CXCursor_ObjCInstanceMethodDecl) {
73 int SelectorIdIndex = -1;
74 // Check if cursor points to a selector id.
75 if (RegionOfInterest.isValid() &&
76 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
77 SmallVector<SourceLocation, 16> SelLocs;
78 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
Craig Topper09d19ef2013-07-04 03:08:24 +000079 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000080 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
81 if (I != SelLocs.end())
82 SelectorIdIndex = I - SelLocs.begin();
83 }
84 CXCursor C = { K, SelectorIdIndex,
85 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
86 return C;
87 }
88
89 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000090 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000091}
92
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000093CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
Dmitri Gribenko67812b22013-01-11 21:01:49 +000094 CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000095 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000096 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000097 CXCursorKind K = CXCursor_NotImplemented;
98
99 switch (S->getStmtClass()) {
100 case Stmt::NoStmtClass:
101 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000102
Douglas Gregor97b98722010-01-19 23:20:36 +0000103 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000104 K = CXCursor_CaseStmt;
105 break;
106
Douglas Gregor97b98722010-01-19 23:20:36 +0000107 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000108 K = CXCursor_DefaultStmt;
109 break;
110
111 case Stmt::IfStmtClass:
112 K = CXCursor_IfStmt;
113 break;
114
115 case Stmt::SwitchStmtClass:
116 K = CXCursor_SwitchStmt;
117 break;
118
119 case Stmt::WhileStmtClass:
120 K = CXCursor_WhileStmt;
121 break;
122
123 case Stmt::DoStmtClass:
124 K = CXCursor_DoStmt;
125 break;
126
127 case Stmt::ForStmtClass:
128 K = CXCursor_ForStmt;
129 break;
130
131 case Stmt::GotoStmtClass:
132 K = CXCursor_GotoStmt;
133 break;
134
Douglas Gregor97b98722010-01-19 23:20:36 +0000135 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000136 K = CXCursor_IndirectGotoStmt;
137 break;
138
139 case Stmt::ContinueStmtClass:
140 K = CXCursor_ContinueStmt;
141 break;
142
143 case Stmt::BreakStmtClass:
144 K = CXCursor_BreakStmt;
145 break;
146
147 case Stmt::ReturnStmtClass:
148 K = CXCursor_ReturnStmt;
149 break;
150
Chad Rosierdf5faf52012-08-25 00:11:56 +0000151 case Stmt::GCCAsmStmtClass:
152 K = CXCursor_GCCAsmStmt;
Douglas Gregor42b29842011-10-05 19:00:14 +0000153 break;
Chad Rosier8cd64b42012-06-11 20:47:18 +0000154
155 case Stmt::MSAsmStmtClass:
156 K = CXCursor_MSAsmStmt;
157 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000158
159 case Stmt::ObjCAtTryStmtClass:
160 K = CXCursor_ObjCAtTryStmt;
161 break;
162
163 case Stmt::ObjCAtCatchStmtClass:
164 K = CXCursor_ObjCAtCatchStmt;
165 break;
166
167 case Stmt::ObjCAtFinallyStmtClass:
168 K = CXCursor_ObjCAtFinallyStmt;
169 break;
170
171 case Stmt::ObjCAtThrowStmtClass:
172 K = CXCursor_ObjCAtThrowStmt;
173 break;
174
175 case Stmt::ObjCAtSynchronizedStmtClass:
176 K = CXCursor_ObjCAtSynchronizedStmt;
177 break;
178
179 case Stmt::ObjCAutoreleasePoolStmtClass:
180 K = CXCursor_ObjCAutoreleasePoolStmt;
181 break;
182
Douglas Gregor97b98722010-01-19 23:20:36 +0000183 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000184 K = CXCursor_ObjCForCollectionStmt;
185 break;
186
Douglas Gregor97b98722010-01-19 23:20:36 +0000187 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000188 K = CXCursor_CXXCatchStmt;
189 break;
190
191 case Stmt::CXXTryStmtClass:
192 K = CXCursor_CXXTryStmt;
193 break;
194
195 case Stmt::CXXForRangeStmtClass:
196 K = CXCursor_CXXForRangeStmt;
197 break;
198
John Wiegley28bbe4b2011-04-28 01:08:34 +0000199 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000200 K = CXCursor_SEHTryStmt;
201 break;
202
John Wiegley28bbe4b2011-04-28 01:08:34 +0000203 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000204 K = CXCursor_SEHExceptStmt;
205 break;
206
John Wiegley28bbe4b2011-04-28 01:08:34 +0000207 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000208 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000209 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000210
John Wiegley21ff2e52011-04-28 00:16:57 +0000211 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000212 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000213 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000214 case Stmt::BinaryConditionalOperatorClass:
215 case Stmt::BinaryTypeTraitExprClass:
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:
238 case Stmt::UnaryTypeTraitExprClass:
239 case Stmt::VAArgExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000240 case Stmt::ObjCArrayLiteralClass:
241 case Stmt::ObjCDictionaryLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +0000242 case Stmt::ObjCBoxedExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000243 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000244 K = CXCursor_UnexposedExpr;
245 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000246
John McCall4b9c2d22011-11-06 09:01:30 +0000247 case Stmt::OpaqueValueExprClass:
248 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
249 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
250 K = CXCursor_UnexposedExpr;
251 break;
252
253 case Stmt::PseudoObjectExprClass:
254 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
255 Parent, TU, RegionOfInterest);
256
Douglas Gregor42b29842011-10-05 19:00:14 +0000257 case Stmt::CompoundStmtClass:
258 K = CXCursor_CompoundStmt;
259 break;
Richard Smith534986f2012-04-14 00:33:13 +0000260
Douglas Gregor42b29842011-10-05 19:00:14 +0000261 case Stmt::NullStmtClass:
262 K = CXCursor_NullStmt;
263 break;
Richard Smith534986f2012-04-14 00:33:13 +0000264
Douglas Gregor42b29842011-10-05 19:00:14 +0000265 case Stmt::LabelStmtClass:
266 K = CXCursor_LabelStmt;
267 break;
Richard Smith534986f2012-04-14 00:33:13 +0000268
269 case Stmt::AttributedStmtClass:
270 K = CXCursor_UnexposedStmt;
271 break;
272
Douglas Gregor42b29842011-10-05 19:00:14 +0000273 case Stmt::DeclStmtClass:
274 K = CXCursor_DeclStmt;
275 break;
Richard Smith534986f2012-04-14 00:33:13 +0000276
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000277 case Stmt::CapturedStmtClass:
278 K = CXCursor_UnexposedStmt;
279 break;
280
Douglas Gregor42b29842011-10-05 19:00:14 +0000281 case Stmt::IntegerLiteralClass:
282 K = CXCursor_IntegerLiteral;
283 break;
284
285 case Stmt::FloatingLiteralClass:
286 K = CXCursor_FloatingLiteral;
287 break;
288
289 case Stmt::ImaginaryLiteralClass:
290 K = CXCursor_ImaginaryLiteral;
291 break;
292
293 case Stmt::StringLiteralClass:
294 K = CXCursor_StringLiteral;
295 break;
296
297 case Stmt::CharacterLiteralClass:
298 K = CXCursor_CharacterLiteral;
299 break;
300
301 case Stmt::ParenExprClass:
302 K = CXCursor_ParenExpr;
303 break;
304
305 case Stmt::UnaryOperatorClass:
306 K = CXCursor_UnaryOperator;
307 break;
Richard Smith534986f2012-04-14 00:33:13 +0000308
Douglas Gregor42b29842011-10-05 19:00:14 +0000309 case Stmt::CXXNoexceptExprClass:
310 K = CXCursor_UnaryExpr;
311 break;
312
313 case Stmt::ArraySubscriptExprClass:
314 K = CXCursor_ArraySubscriptExpr;
315 break;
316
317 case Stmt::BinaryOperatorClass:
318 K = CXCursor_BinaryOperator;
319 break;
320
321 case Stmt::CompoundAssignOperatorClass:
322 K = CXCursor_CompoundAssignOperator;
323 break;
324
325 case Stmt::ConditionalOperatorClass:
326 K = CXCursor_ConditionalOperator;
327 break;
328
329 case Stmt::CStyleCastExprClass:
330 K = CXCursor_CStyleCastExpr;
331 break;
332
333 case Stmt::CompoundLiteralExprClass:
334 K = CXCursor_CompoundLiteralExpr;
335 break;
336
337 case Stmt::InitListExprClass:
338 K = CXCursor_InitListExpr;
339 break;
340
341 case Stmt::AddrLabelExprClass:
342 K = CXCursor_AddrLabelExpr;
343 break;
344
345 case Stmt::StmtExprClass:
346 K = CXCursor_StmtExpr;
347 break;
348
349 case Stmt::GenericSelectionExprClass:
350 K = CXCursor_GenericSelectionExpr;
351 break;
352
353 case Stmt::GNUNullExprClass:
354 K = CXCursor_GNUNullExpr;
355 break;
356
357 case Stmt::CXXStaticCastExprClass:
358 K = CXCursor_CXXStaticCastExpr;
359 break;
360
361 case Stmt::CXXDynamicCastExprClass:
362 K = CXCursor_CXXDynamicCastExpr;
363 break;
364
365 case Stmt::CXXReinterpretCastExprClass:
366 K = CXCursor_CXXReinterpretCastExpr;
367 break;
368
369 case Stmt::CXXConstCastExprClass:
370 K = CXCursor_CXXConstCastExpr;
371 break;
372
373 case Stmt::CXXFunctionalCastExprClass:
374 K = CXCursor_CXXFunctionalCastExpr;
375 break;
376
377 case Stmt::CXXTypeidExprClass:
378 K = CXCursor_CXXTypeidExpr;
379 break;
380
381 case Stmt::CXXBoolLiteralExprClass:
382 K = CXCursor_CXXBoolLiteralExpr;
383 break;
384
385 case Stmt::CXXNullPtrLiteralExprClass:
386 K = CXCursor_CXXNullPtrLiteralExpr;
387 break;
388
389 case Stmt::CXXThisExprClass:
390 K = CXCursor_CXXThisExpr;
391 break;
392
393 case Stmt::CXXThrowExprClass:
394 K = CXCursor_CXXThrowExpr;
395 break;
396
397 case Stmt::CXXNewExprClass:
398 K = CXCursor_CXXNewExpr;
399 break;
400
401 case Stmt::CXXDeleteExprClass:
402 K = CXCursor_CXXDeleteExpr;
403 break;
404
405 case Stmt::ObjCStringLiteralClass:
406 K = CXCursor_ObjCStringLiteral;
407 break;
408
409 case Stmt::ObjCEncodeExprClass:
410 K = CXCursor_ObjCEncodeExpr;
411 break;
412
413 case Stmt::ObjCSelectorExprClass:
414 K = CXCursor_ObjCSelectorExpr;
415 break;
416
417 case Stmt::ObjCProtocolExprClass:
418 K = CXCursor_ObjCProtocolExpr;
419 break;
Ted Kremenekb3f75422012-03-06 20:06:06 +0000420
421 case Stmt::ObjCBoolLiteralExprClass:
422 K = CXCursor_ObjCBoolLiteralExpr;
423 break;
424
Douglas Gregor42b29842011-10-05 19:00:14 +0000425 case Stmt::ObjCBridgedCastExprClass:
426 K = CXCursor_ObjCBridgedCastExpr;
427 break;
428
429 case Stmt::BlockExprClass:
430 K = CXCursor_BlockExpr;
431 break;
432
433 case Stmt::PackExpansionExprClass:
434 K = CXCursor_PackExpansionExpr;
435 break;
436
437 case Stmt::SizeOfPackExprClass:
438 K = CXCursor_SizeOfPackExpr;
439 break;
440
Argyrios Kyrtzidisedab0472013-04-23 17:57:17 +0000441 case Stmt::DeclRefExprClass:
442 if (const ImplicitParamDecl *IPD =
443 dyn_cast_or_null<ImplicitParamDecl>(cast<DeclRefExpr>(S)->getDecl())) {
444 if (const ObjCMethodDecl *MD =
445 dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
446 if (MD->getSelfDecl() == IPD) {
447 K = CXCursor_ObjCSelfExpr;
448 break;
449 }
450 }
451 }
452
453 K = CXCursor_DeclRefExpr;
454 break;
455
Douglas Gregor42b29842011-10-05 19:00:14 +0000456 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000457 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000458 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +0000459 case Stmt::FunctionParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000460 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000461 K = CXCursor_DeclRefExpr;
462 break;
463
Douglas Gregor42b29842011-10-05 19:00:14 +0000464 case Stmt::CXXDependentScopeMemberExprClass:
465 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000466 case Stmt::MemberExprClass:
John McCall76da55d2013-04-16 07:28:30 +0000467 case Stmt::MSPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000468 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000469 case Stmt::ObjCIvarRefExprClass:
470 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000471 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000472 K = CXCursor_MemberRefExpr;
473 break;
474
475 case Stmt::CallExprClass:
476 case Stmt::CXXOperatorCallExprClass:
477 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000478 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000479 case Stmt::CXXConstructExprClass:
480 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000481 case Stmt::CXXUnresolvedConstructExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000482 case Stmt::UserDefinedLiteralClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000483 K = CXCursor_CallExpr;
484 break;
485
Douglas Gregor011d8b92012-02-15 00:54:55 +0000486 case Stmt::LambdaExprClass:
487 K = CXCursor_LambdaExpr;
488 break;
489
Douglas Gregorba0513d2011-10-25 01:33:02 +0000490 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000491 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000492 int SelectorIdIndex = -1;
493 // Check if cursor points to a selector id.
494 if (RegionOfInterest.isValid() &&
495 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
496 SmallVector<SourceLocation, 16> SelLocs;
497 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
Craig Topper09d19ef2013-07-04 03:08:24 +0000498 SmallVectorImpl<SourceLocation>::iterator
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000499 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
500 if (I != SelLocs.end())
501 SelectorIdIndex = I - SelLocs.begin();
502 }
503 CXCursor C = { K, 0, { Parent, S, TU } };
504 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000505 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000506
507 case Stmt::MSDependentExistsStmtClass:
508 K = CXCursor_UnexposedStmt;
509 break;
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000510 case Stmt::OMPParallelDirectiveClass:
511 K = CXCursor_OMPParallelDirective;
512 break;
513
Douglas Gregorba0513d2011-10-25 01:33:02 +0000514 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000515
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000516 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000517 return C;
518}
519
Douglas Gregor2e331b92010-01-16 14:00:32 +0000520CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000521 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000522 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000523 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000524 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000525 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000526 return C;
527}
528
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000529std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor2e331b92010-01-16 14:00:32 +0000530cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
531 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000532 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000533 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor2e331b92010-01-16 14:00:32 +0000534}
535
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000536CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000537 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000538 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000539 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000540 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000541 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000542 return C;
543}
544
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000545std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000546cxcursor::getCursorObjCProtocolRef(CXCursor C) {
547 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000548 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000549 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000550}
551
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000552CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000553 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000554 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000555 // 'Class' can be null for invalid code.
556 if (!Class)
557 return MakeCXCursorInvalid(CXCursor_InvalidCode);
558 assert(TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000559 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000560 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000561 return C;
562}
563
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000564std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor1adb0822010-01-16 17:14:40 +0000565cxcursor::getCursorObjCClassRef(CXCursor C) {
566 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000567 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000568 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1adb0822010-01-16 17:14:40 +0000569}
570
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000571CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000572 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000573 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000574 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000575 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000576 return C;
577}
578
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000579std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000580cxcursor::getCursorTypeRef(CXCursor C) {
581 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000582 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000583 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000584}
585
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000586CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000587 SourceLocation Loc,
588 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000589 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000590 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000591 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000592 return C;
593}
594
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000595std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregor0b36e612010-08-31 20:37:03 +0000596cxcursor::getCursorTemplateRef(CXCursor C) {
597 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000598 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000599 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor0b36e612010-08-31 20:37:03 +0000600}
601
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000602CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
603 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000604 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000605
606 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
607 "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000608 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000609 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000610 return C;
611}
612
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000613std::pair<const NamedDecl *, SourceLocation>
Douglas Gregor69319002010-08-31 23:48:11 +0000614cxcursor::getCursorNamespaceRef(CXCursor C) {
615 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000616 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000617 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor69319002010-08-31 23:48:11 +0000618}
619
Douglas Gregor011d8b92012-02-15 00:54:55 +0000620CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
621 CXTranslationUnit TU) {
622
623 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000624 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000625 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor011d8b92012-02-15 00:54:55 +0000626 return C;
627}
628
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000629std::pair<const VarDecl *, SourceLocation>
Douglas Gregor011d8b92012-02-15 00:54:55 +0000630cxcursor::getCursorVariableRef(CXCursor C) {
631 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000632 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000633 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor011d8b92012-02-15 00:54:55 +0000634}
635
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000636CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000637 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000638
639 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000640 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000641 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000642 return C;
643}
644
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000645std::pair<const FieldDecl *, SourceLocation>
Douglas Gregora67e03f2010-09-09 21:42:20 +0000646cxcursor::getCursorMemberRef(CXCursor C) {
647 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000648 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000649 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora67e03f2010-09-09 21:42:20 +0000650}
651
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000652CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000653 CXTranslationUnit TU){
Dmitri Gribenko33156182013-01-11 21:06:06 +0000654 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000655 return C;
656}
657
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000658const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenek3064ef92010-08-27 21:34:58 +0000659 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000660 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000661}
662
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000663CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000664 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000665 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000666 { Range.getBegin().getPtrEncoding(),
667 Range.getEnd().getPtrEncoding(),
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000668 TU }
669 };
670 return C;
671}
672
673SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
674 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000675 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
676 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000677 ASTUnit *TU = getCursorASTUnit(C);
678 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000679}
680
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000681CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000682 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000683 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000684 return C;
685}
686
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000687const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000688 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000689 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor572feb22010-03-18 18:04:21 +0000690}
691
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000692CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
693 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000694 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000695 return C;
696}
697
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000698CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
699 SourceLocation Loc,
700 CXTranslationUnit TU) {
701 assert(Loc.isValid());
702 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
703 return C;
704}
705
706const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
707 if (isPseudo())
708 return getAsMacroDefinition()->getName();
709 return getAsMacroExpansion()->getName();
710}
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000711const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000712 if (isPseudo())
713 return getAsMacroDefinition();
714 return getAsMacroExpansion()->getDefinition();
715}
716SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
717 if (isPseudo())
718 return getPseudoLoc();
719 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +0000720}
721
Douglas Gregorecdcb882010-10-20 22:00:55 +0000722CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000723 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000724 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000725 return C;
726}
727
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000728const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000729 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000730 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000731}
732
Douglas Gregor36897b02010-09-10 00:22:18 +0000733CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000734 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000735
736 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000737 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000738 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000739 return C;
740}
741
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000742std::pair<const LabelStmt *, SourceLocation>
Douglas Gregor36897b02010-09-10 00:22:18 +0000743cxcursor::getCursorLabelRef(CXCursor C) {
744 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000745 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000746 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor36897b02010-09-10 00:22:18 +0000747}
748
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000749CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000750 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000751 assert(E && TU && "Invalid arguments!");
752 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000753 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000754 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000755 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000756 { Storage.getOpaqueValue(), RawLoc, TU }
757 };
758 return C;
759}
760
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000761CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000762 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000763 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000764 assert(D && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000765 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000766 OverloadedDeclRefStorage Storage(D);
767 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000768 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000769 { Storage.getOpaqueValue(), RawLoc, TU }
770 };
771 return C;
772}
773
774CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
775 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000776 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000777 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000778 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000779 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
780 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000781 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000782 { Storage.getOpaqueValue(), RawLoc, TU }
783 };
784 return C;
785}
786
787std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
788cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
789 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000790 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
791 const_cast<void *>(C.data[0])),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000792 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000793}
794
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000795const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
796 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000797}
798
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000799const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000800 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
801}
802
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000803const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000804 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000805 Cursor.kind == CXCursor_ObjCProtocolRef ||
806 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000807 return 0;
808
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000809 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000810}
811
Dmitri Gribenko7d914382013-01-26 18:08:08 +0000812const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
813 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremenek95f33552010-08-26 01:42:22 +0000814}
815
Dmitri Gribenko404628c2013-01-26 18:12:08 +0000816const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
817 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000818}
819
Douglas Gregorf46034a2010-01-18 23:41:10 +0000820ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000821 return getCursorASTUnit(Cursor)->getASTContext();
822}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000823
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000824ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko46f92522013-01-11 19:28:44 +0000825 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000826 if (!TU)
827 return 0;
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000828 return cxtu::getASTUnit(TU);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000829}
830
831CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000832 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregor283cae32010-01-15 21:56:13 +0000833}
834
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000835void cxcursor::getOverriddenCursors(CXCursor cursor,
836 SmallVectorImpl<CXCursor> &overridden) {
837 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000838 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000839 if (!D)
840 return;
841
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000842 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000843 SmallVector<const NamedDecl *, 8> OverDecls;
844 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000845
Craig Topper09d19ef2013-07-04 03:08:24 +0000846 for (SmallVectorImpl<const NamedDecl *>::iterator
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000847 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko05756dc2013-01-14 00:46:27 +0000848 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000849 }
850}
851
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000852std::pair<int, SourceLocation>
853cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
854 if (cursor.kind == CXCursor_ObjCMessageExpr) {
855 if (cursor.xdata != -1)
856 return std::make_pair(cursor.xdata,
857 cast<ObjCMessageExpr>(getCursorExpr(cursor))
858 ->getSelectorLoc(cursor.xdata));
859 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
860 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
861 if (cursor.xdata != -1)
862 return std::make_pair(cursor.xdata,
863 cast<ObjCMethodDecl>(getCursorDecl(cursor))
864 ->getSelectorLoc(cursor.xdata));
865 }
866
867 return std::make_pair(-1, SourceLocation());
868}
869
870CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
871 CXCursor newCursor = cursor;
872
873 if (cursor.kind == CXCursor_ObjCMessageExpr) {
874 if (SelIdx == -1 ||
875 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
876 ->getNumSelectorLocs())
877 newCursor.xdata = -1;
878 else
879 newCursor.xdata = SelIdx;
880 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
881 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
882 if (SelIdx == -1 ||
883 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
884 ->getNumSelectorLocs())
885 newCursor.xdata = -1;
886 else
887 newCursor.xdata = SelIdx;
888 }
889
890 return newCursor;
891}
892
893CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
894 if (cursor.kind != CXCursor_CallExpr)
895 return cursor;
896
897 if (cursor.xdata == 0)
898 return cursor;
899
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000900 const Expr *E = getCursorExpr(cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000901 TypeSourceInfo *Type = 0;
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000902 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000903 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
904 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000905 } else if (const CXXTemporaryObjectExpr *Tmp =
906 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000907 Type = Tmp->getTypeSourceInfo();
908 }
909
910 if (!Type)
911 return cursor;
912
913 CXTranslationUnit TU = getCursorTU(cursor);
914 QualType Ty = Type->getType();
915 TypeLoc TL = Type->getTypeLoc();
916 SourceLocation Loc = TL.getBeginLoc();
917
918 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
919 Ty = ElabT->getNamedType();
David Blaikie39e6ab42013-02-18 22:06:02 +0000920 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000921 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
922 }
923
924 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
925 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
926 if (const TagType *Tag = Ty->getAs<TagType>())
927 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
928 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
929 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
930
931 return cursor;
932}
933
Douglas Gregor283cae32010-01-15 21:56:13 +0000934bool cxcursor::operator==(CXCursor X, CXCursor Y) {
935 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
936 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000937}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000938
939// FIXME: Remove once we can model DeclGroups and their appropriate ranges
940// properly in the ASTs.
941bool cxcursor::isFirstInDeclGroup(CXCursor C) {
942 assert(clang_isDeclaration(C.kind));
943 return ((uintptr_t) (C.data[1])) != 0;
944}
945
Ted Kremenekeca099b2010-12-08 23:43:14 +0000946//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000947// libclang CXCursor APIs
948//===----------------------------------------------------------------------===//
949
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000950extern "C" {
951
952int clang_Cursor_isNull(CXCursor cursor) {
953 return clang_equalCursors(cursor, clang_getNullCursor());
954}
955
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000956CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
957 return getCursorTU(cursor);
958}
959
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000960int clang_Cursor_getNumArguments(CXCursor C) {
961 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000962 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000963 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
964 return MD->param_size();
965 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
966 return FD->param_size();
967 }
968
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000969 if (clang_isExpression(C.kind)) {
970 const Expr *E = cxcursor::getCursorExpr(C);
971 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
972 return CE->getNumArgs();
973 }
974 }
975
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000976 return -1;
977}
978
979CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
980 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000981 const Decl *D = cxcursor::getCursorDecl(C);
982 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000983 if (i < MD->param_size())
984 return cxcursor::MakeCXCursor(MD->param_begin()[i],
985 cxcursor::getCursorTU(C));
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000986 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000987 if (i < FD->param_size())
988 return cxcursor::MakeCXCursor(FD->param_begin()[i],
989 cxcursor::getCursorTU(C));
990 }
991 }
992
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000993 if (clang_isExpression(C.kind)) {
994 const Expr *E = cxcursor::getCursorExpr(C);
995 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
996 if (i < CE->getNumArgs()) {
997 return cxcursor::MakeCXCursor(CE->getArg(i),
998 getCursorDecl(C),
999 cxcursor::getCursorTU(C));
1000 }
1001 }
1002 }
1003
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001004 return clang_getNullCursor();
1005}
1006
Ted Kremenek017dd742013-04-24 07:17:12 +00001007} // end: extern "C"
1008
1009//===----------------------------------------------------------------------===//
1010// CXCursorSet.
1011//===----------------------------------------------------------------------===//
1012
1013typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1014
1015static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1016 return (CXCursorSet) setImpl;
1017}
1018static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1019 return (CXCursorSet_Impl*) set;
1020}
1021namespace llvm {
1022template<> struct DenseMapInfo<CXCursor> {
1023public:
1024 static inline CXCursor getEmptyKey() {
1025 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1026 }
1027 static inline CXCursor getTombstoneKey() {
1028 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1029 }
1030 static inline unsigned getHashValue(const CXCursor &cursor) {
1031 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1032 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1033 }
1034 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1035 return x.kind == y.kind &&
1036 x.data[0] == y.data[0] &&
1037 x.data[1] == y.data[1];
1038 }
1039};
1040}
1041
1042extern "C" {
1043CXCursorSet clang_createCXCursorSet() {
1044 return packCXCursorSet(new CXCursorSet_Impl());
1045}
1046
1047void clang_disposeCXCursorSet(CXCursorSet set) {
1048 delete unpackCXCursorSet(set);
1049}
1050
1051unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1052 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1053 if (!setImpl)
1054 return 0;
Ted Kremenek96bbe192013-04-24 07:25:40 +00001055 return setImpl->find(cursor) != setImpl->end();
Ted Kremenek017dd742013-04-24 07:17:12 +00001056}
1057
1058unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1059 // Do not insert invalid cursors into the set.
1060 if (cursor.kind >= CXCursor_FirstInvalid &&
1061 cursor.kind <= CXCursor_LastInvalid)
1062 return 1;
1063
1064 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1065 if (!setImpl)
1066 return 1;
1067 unsigned &entry = (*setImpl)[cursor];
1068 unsigned flag = entry == 0 ? 1 : 0;
1069 entry = 1;
1070 return flag;
1071}
1072
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001073CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1074 enum CXCursorKind kind = clang_getCursorKind(cursor);
1075 if (clang_isDeclaration(kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00001076 const Decl *decl = getCursorDecl(cursor);
1077 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001078 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregord1f09b42013-01-31 04:52:16 +00001079 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001080 CodeCompletionString *String
1081 = Result.CreateCodeCompletionString(unit->getASTContext(),
1082 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001083 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001084 unit->getCodeCompletionTUInfo(),
1085 true);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001086 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001087 }
1088 }
1089 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001090 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001091 const IdentifierInfo *MacroInfo = definition->getName();
1092 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenkob3958472013-01-14 00:36:42 +00001093 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001094 CodeCompletionString *String
1095 = Result.CreateCodeCompletionString(unit->getASTContext(),
1096 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001097 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001098 unit->getCodeCompletionTUInfo(),
1099 false);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001100 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001101 }
1102 return NULL;
1103}
Ted Kremenek8eece462012-04-30 19:33:45 +00001104} // end: extern C.
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001105
1106namespace {
1107 struct OverridenCursorsPool {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001108 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001109 std::vector<CursorVec*> AllCursors;
1110 std::vector<CursorVec*> AvailableCursors;
1111
1112 ~OverridenCursorsPool() {
1113 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1114 E = AllCursors.end(); I != E; ++I) {
1115 delete *I;
1116 }
1117 }
1118 };
1119}
1120
1121void *cxcursor::createOverridenCXCursorsPool() {
1122 return new OverridenCursorsPool();
1123}
1124
1125void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1126 delete static_cast<OverridenCursorsPool*>(pool);
1127}
Ted Kremenek8eece462012-04-30 19:33:45 +00001128
1129extern "C" {
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001130void clang_getOverriddenCursors(CXCursor cursor,
1131 CXCursor **overridden,
1132 unsigned *num_overridden) {
1133 if (overridden)
1134 *overridden = 0;
1135 if (num_overridden)
1136 *num_overridden = 0;
1137
1138 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1139
1140 if (!overridden || !num_overridden || !TU)
1141 return;
1142
1143 if (!clang_isDeclaration(cursor.kind))
1144 return;
1145
1146 OverridenCursorsPool &pool =
1147 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1148
1149 OverridenCursorsPool::CursorVec *Vec = 0;
1150
1151 if (!pool.AvailableCursors.empty()) {
1152 Vec = pool.AvailableCursors.back();
1153 pool.AvailableCursors.pop_back();
1154 }
1155 else {
1156 Vec = new OverridenCursorsPool::CursorVec();
1157 pool.AllCursors.push_back(Vec);
1158 }
1159
1160 // Clear out the vector, but don't free the memory contents. This
1161 // reduces malloc() traffic.
1162 Vec->clear();
1163
1164 // Use the first entry to contain a back reference to the vector.
1165 // This is a complete hack.
1166 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1167 backRefCursor.data[0] = Vec;
1168 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1169 Vec->push_back(backRefCursor);
1170
1171 // Get the overriden cursors.
1172 cxcursor::getOverriddenCursors(cursor, *Vec);
1173
1174 // Did we get any overriden cursors? If not, return Vec to the pool
1175 // of available cursor vectors.
1176 if (Vec->size() == 1) {
1177 pool.AvailableCursors.push_back(Vec);
1178 return;
1179 }
1180
1181 // Now tell the caller about the overriden cursors.
1182 assert(Vec->size() > 1);
1183 *overridden = &((*Vec)[1]);
1184 *num_overridden = Vec->size() - 1;
1185}
1186
1187void clang_disposeOverriddenCursors(CXCursor *overridden) {
1188 if (!overridden)
1189 return;
1190
1191 // Use pointer arithmetic to get back the first faux entry
1192 // which has a back-reference to the TU and the vector.
1193 --overridden;
1194 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001195 static_cast<OverridenCursorsPool::CursorVec *>(
1196 const_cast<void *>(overridden->data[0]));
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001197 CXTranslationUnit TU = getCursorTU(*overridden);
1198
1199 assert(Vec && TU);
1200
1201 OverridenCursorsPool &pool =
1202 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1203
1204 pool.AvailableCursors.push_back(Vec);
1205}
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001206
1207int clang_Cursor_isDynamicCall(CXCursor C) {
1208 const Expr *E = 0;
1209 if (clang_isExpression(C.kind))
1210 E = getCursorExpr(C);
1211 if (!E)
1212 return 0;
1213
1214 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1215 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1216
1217 const MemberExpr *ME = 0;
1218 if (isa<MemberExpr>(E))
1219 ME = cast<MemberExpr>(E);
1220 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1221 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1222
1223 if (ME) {
1224 if (const CXXMethodDecl *
1225 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1226 return MD->isVirtual() && !ME->hasQualifier();
1227 }
1228
1229 return 0;
1230}
1231
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001232CXType clang_Cursor_getReceiverType(CXCursor C) {
1233 CXTranslationUnit TU = cxcursor::getCursorTU(C);
1234 const Expr *E = 0;
1235 if (clang_isExpression(C.kind))
1236 E = getCursorExpr(C);
1237
1238 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1239 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1240
1241 return cxtype::MakeCXType(QualType(), TU);
1242}
1243
Ted Kremenekeca099b2010-12-08 23:43:14 +00001244} // end: extern "C"