blob: b5682055b9e3851c7af918e2eae6e62c081575ad [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"
Ted Kremenek16c440a2010-01-15 20:35:54 +000021#include "clang/AST/Decl.h"
Douglas Gregor69319002010-08-31 23:48:11 +000022#include "clang/AST/DeclCXX.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000023#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000024#include "clang/AST/DeclTemplate.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000025#include "clang/AST/Expr.h"
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000026#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000027#include "clang/AST/ExprObjC.h"
Chandler Carruthf59edb92012-12-04 09:25:21 +000028#include "clang/Frontend/ASTUnit.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000029#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000030
31using namespace clang;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000032using namespace cxcursor;
Ted Kremenek16c440a2010-01-15 20:35:54 +000033
Ted Kremenekbbf66ca2012-04-30 19:06:49 +000034CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000035 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
Ted Kremenekbbf66ca2012-04-30 19:06:49 +000036 CXCursor C = { K, 0, { 0, 0, TU } };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000037 return C;
Ted Kremenek16c440a2010-01-15 20:35:54 +000038}
39
Ted Kremeneke77f4432010-02-18 03:09:07 +000040static CXCursorKind GetCursorKind(const Attr *A) {
41 assert(A && "Invalid arguments!");
42 switch (A->getKind()) {
43 default: break;
Sean Hunt387475d2010-06-16 23:43:53 +000044 case attr::IBAction: return CXCursor_IBActionAttr;
45 case attr::IBOutlet: return CXCursor_IBOutletAttr;
46 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Argyrios Kyrtzidis6639e922011-09-13 17:39:31 +000047 case attr::Final: return CXCursor_CXXFinalAttr;
48 case attr::Override: return CXCursor_CXXOverrideAttr;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000049 case attr::Annotate: return CXCursor_AnnotateAttr;
Argyrios Kyrtzidis84b79642011-12-06 22:05:01 +000050 case attr::AsmLabel: return CXCursor_AsmLabelAttr;
Ted Kremeneke77f4432010-02-18 03:09:07 +000051 }
52
53 return CXCursor_UnexposedAttr;
54}
55
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000056CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
Ted Kremeneka60ed472010-11-16 08:15:36 +000057 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000058 assert(A && Parent && TU && "Invalid arguments!");
Dmitri Gribenko33156182013-01-11 21:06:06 +000059 CXCursor C = { GetCursorKind(A), 0, { Parent, A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000060 return C;
61}
62
Dmitri Gribenko67812b22013-01-11 21:01:49 +000063CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000064 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000065 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000066 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000067
68 CXCursorKind K = getCursorKindForDecl(D);
69
70 if (K == CXCursor_ObjCClassMethodDecl ||
71 K == CXCursor_ObjCInstanceMethodDecl) {
72 int SelectorIdIndex = -1;
73 // Check if cursor points to a selector id.
74 if (RegionOfInterest.isValid() &&
75 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
76 SmallVector<SourceLocation, 16> SelLocs;
77 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
78 SmallVector<SourceLocation, 16>::iterator
79 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
80 if (I != SelLocs.end())
81 SelectorIdIndex = I - SelLocs.begin();
82 }
83 CXCursor C = { K, SelectorIdIndex,
84 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
85 return C;
86 }
87
88 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000089 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000090}
91
Dmitri Gribenko05756dc2013-01-14 00:46:27 +000092CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
Dmitri Gribenko67812b22013-01-11 21:01:49 +000093 CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000094 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000095 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000096 CXCursorKind K = CXCursor_NotImplemented;
97
98 switch (S->getStmtClass()) {
99 case Stmt::NoStmtClass:
100 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000101
Douglas Gregor97b98722010-01-19 23:20:36 +0000102 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000103 K = CXCursor_CaseStmt;
104 break;
105
Douglas Gregor97b98722010-01-19 23:20:36 +0000106 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000107 K = CXCursor_DefaultStmt;
108 break;
109
110 case Stmt::IfStmtClass:
111 K = CXCursor_IfStmt;
112 break;
113
114 case Stmt::SwitchStmtClass:
115 K = CXCursor_SwitchStmt;
116 break;
117
118 case Stmt::WhileStmtClass:
119 K = CXCursor_WhileStmt;
120 break;
121
122 case Stmt::DoStmtClass:
123 K = CXCursor_DoStmt;
124 break;
125
126 case Stmt::ForStmtClass:
127 K = CXCursor_ForStmt;
128 break;
129
130 case Stmt::GotoStmtClass:
131 K = CXCursor_GotoStmt;
132 break;
133
Douglas Gregor97b98722010-01-19 23:20:36 +0000134 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000135 K = CXCursor_IndirectGotoStmt;
136 break;
137
138 case Stmt::ContinueStmtClass:
139 K = CXCursor_ContinueStmt;
140 break;
141
142 case Stmt::BreakStmtClass:
143 K = CXCursor_BreakStmt;
144 break;
145
146 case Stmt::ReturnStmtClass:
147 K = CXCursor_ReturnStmt;
148 break;
149
Chad Rosierdf5faf52012-08-25 00:11:56 +0000150 case Stmt::GCCAsmStmtClass:
151 K = CXCursor_GCCAsmStmt;
Douglas Gregor42b29842011-10-05 19:00:14 +0000152 break;
Chad Rosier8cd64b42012-06-11 20:47:18 +0000153
154 case Stmt::MSAsmStmtClass:
155 K = CXCursor_MSAsmStmt;
156 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000157
158 case Stmt::ObjCAtTryStmtClass:
159 K = CXCursor_ObjCAtTryStmt;
160 break;
161
162 case Stmt::ObjCAtCatchStmtClass:
163 K = CXCursor_ObjCAtCatchStmt;
164 break;
165
166 case Stmt::ObjCAtFinallyStmtClass:
167 K = CXCursor_ObjCAtFinallyStmt;
168 break;
169
170 case Stmt::ObjCAtThrowStmtClass:
171 K = CXCursor_ObjCAtThrowStmt;
172 break;
173
174 case Stmt::ObjCAtSynchronizedStmtClass:
175 K = CXCursor_ObjCAtSynchronizedStmt;
176 break;
177
178 case Stmt::ObjCAutoreleasePoolStmtClass:
179 K = CXCursor_ObjCAutoreleasePoolStmt;
180 break;
181
Douglas Gregor97b98722010-01-19 23:20:36 +0000182 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000183 K = CXCursor_ObjCForCollectionStmt;
184 break;
185
Douglas Gregor97b98722010-01-19 23:20:36 +0000186 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000187 K = CXCursor_CXXCatchStmt;
188 break;
189
190 case Stmt::CXXTryStmtClass:
191 K = CXCursor_CXXTryStmt;
192 break;
193
194 case Stmt::CXXForRangeStmtClass:
195 K = CXCursor_CXXForRangeStmt;
196 break;
197
John Wiegley28bbe4b2011-04-28 01:08:34 +0000198 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000199 K = CXCursor_SEHTryStmt;
200 break;
201
John Wiegley28bbe4b2011-04-28 01:08:34 +0000202 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000203 K = CXCursor_SEHExceptStmt;
204 break;
205
John Wiegley28bbe4b2011-04-28 01:08:34 +0000206 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000207 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000208 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000209
John Wiegley21ff2e52011-04-28 00:16:57 +0000210 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000211 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000212 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000213 case Stmt::BinaryConditionalOperatorClass:
214 case Stmt::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000215 case Stmt::TypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000216 case Stmt::CXXBindTemporaryExprClass:
217 case Stmt::CXXDefaultArgExprClass:
Richard Smithc3bf52c2013-04-20 22:23:05 +0000218 case Stmt::CXXDefaultInitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000219 case Stmt::CXXScalarValueInitExprClass:
220 case Stmt::CXXUuidofExprClass:
221 case Stmt::ChooseExprClass:
222 case Stmt::DesignatedInitExprClass:
223 case Stmt::ExprWithCleanupsClass:
224 case Stmt::ExpressionTraitExprClass:
225 case Stmt::ExtVectorElementExprClass:
226 case Stmt::ImplicitCastExprClass:
227 case Stmt::ImplicitValueInitExprClass:
228 case Stmt::MaterializeTemporaryExprClass:
229 case Stmt::ObjCIndirectCopyRestoreExprClass:
230 case Stmt::OffsetOfExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000231 case Stmt::ParenListExprClass:
232 case Stmt::PredefinedExprClass:
233 case Stmt::ShuffleVectorExprClass:
234 case Stmt::UnaryExprOrTypeTraitExprClass:
235 case Stmt::UnaryTypeTraitExprClass:
236 case Stmt::VAArgExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000237 case Stmt::ObjCArrayLiteralClass:
238 case Stmt::ObjCDictionaryLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +0000239 case Stmt::ObjCBoxedExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000240 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000241 K = CXCursor_UnexposedExpr;
242 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000243
John McCall4b9c2d22011-11-06 09:01:30 +0000244 case Stmt::OpaqueValueExprClass:
245 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
246 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
247 K = CXCursor_UnexposedExpr;
248 break;
249
250 case Stmt::PseudoObjectExprClass:
251 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
252 Parent, TU, RegionOfInterest);
253
Douglas Gregor42b29842011-10-05 19:00:14 +0000254 case Stmt::CompoundStmtClass:
255 K = CXCursor_CompoundStmt;
256 break;
Richard Smith534986f2012-04-14 00:33:13 +0000257
Douglas Gregor42b29842011-10-05 19:00:14 +0000258 case Stmt::NullStmtClass:
259 K = CXCursor_NullStmt;
260 break;
Richard Smith534986f2012-04-14 00:33:13 +0000261
Douglas Gregor42b29842011-10-05 19:00:14 +0000262 case Stmt::LabelStmtClass:
263 K = CXCursor_LabelStmt;
264 break;
Richard Smith534986f2012-04-14 00:33:13 +0000265
266 case Stmt::AttributedStmtClass:
267 K = CXCursor_UnexposedStmt;
268 break;
269
Douglas Gregor42b29842011-10-05 19:00:14 +0000270 case Stmt::DeclStmtClass:
271 K = CXCursor_DeclStmt;
272 break;
Richard Smith534986f2012-04-14 00:33:13 +0000273
Tareq A. Siraj051303c2013-04-16 18:53:08 +0000274 case Stmt::CapturedStmtClass:
275 K = CXCursor_UnexposedStmt;
276 break;
277
Douglas Gregor42b29842011-10-05 19:00:14 +0000278 case Stmt::IntegerLiteralClass:
279 K = CXCursor_IntegerLiteral;
280 break;
281
282 case Stmt::FloatingLiteralClass:
283 K = CXCursor_FloatingLiteral;
284 break;
285
286 case Stmt::ImaginaryLiteralClass:
287 K = CXCursor_ImaginaryLiteral;
288 break;
289
290 case Stmt::StringLiteralClass:
291 K = CXCursor_StringLiteral;
292 break;
293
294 case Stmt::CharacterLiteralClass:
295 K = CXCursor_CharacterLiteral;
296 break;
297
298 case Stmt::ParenExprClass:
299 K = CXCursor_ParenExpr;
300 break;
301
302 case Stmt::UnaryOperatorClass:
303 K = CXCursor_UnaryOperator;
304 break;
Richard Smith534986f2012-04-14 00:33:13 +0000305
Douglas Gregor42b29842011-10-05 19:00:14 +0000306 case Stmt::CXXNoexceptExprClass:
307 K = CXCursor_UnaryExpr;
308 break;
309
310 case Stmt::ArraySubscriptExprClass:
311 K = CXCursor_ArraySubscriptExpr;
312 break;
313
314 case Stmt::BinaryOperatorClass:
315 K = CXCursor_BinaryOperator;
316 break;
317
318 case Stmt::CompoundAssignOperatorClass:
319 K = CXCursor_CompoundAssignOperator;
320 break;
321
322 case Stmt::ConditionalOperatorClass:
323 K = CXCursor_ConditionalOperator;
324 break;
325
326 case Stmt::CStyleCastExprClass:
327 K = CXCursor_CStyleCastExpr;
328 break;
329
330 case Stmt::CompoundLiteralExprClass:
331 K = CXCursor_CompoundLiteralExpr;
332 break;
333
334 case Stmt::InitListExprClass:
335 K = CXCursor_InitListExpr;
336 break;
337
338 case Stmt::AddrLabelExprClass:
339 K = CXCursor_AddrLabelExpr;
340 break;
341
342 case Stmt::StmtExprClass:
343 K = CXCursor_StmtExpr;
344 break;
345
346 case Stmt::GenericSelectionExprClass:
347 K = CXCursor_GenericSelectionExpr;
348 break;
349
350 case Stmt::GNUNullExprClass:
351 K = CXCursor_GNUNullExpr;
352 break;
353
354 case Stmt::CXXStaticCastExprClass:
355 K = CXCursor_CXXStaticCastExpr;
356 break;
357
358 case Stmt::CXXDynamicCastExprClass:
359 K = CXCursor_CXXDynamicCastExpr;
360 break;
361
362 case Stmt::CXXReinterpretCastExprClass:
363 K = CXCursor_CXXReinterpretCastExpr;
364 break;
365
366 case Stmt::CXXConstCastExprClass:
367 K = CXCursor_CXXConstCastExpr;
368 break;
369
370 case Stmt::CXXFunctionalCastExprClass:
371 K = CXCursor_CXXFunctionalCastExpr;
372 break;
373
374 case Stmt::CXXTypeidExprClass:
375 K = CXCursor_CXXTypeidExpr;
376 break;
377
378 case Stmt::CXXBoolLiteralExprClass:
379 K = CXCursor_CXXBoolLiteralExpr;
380 break;
381
382 case Stmt::CXXNullPtrLiteralExprClass:
383 K = CXCursor_CXXNullPtrLiteralExpr;
384 break;
385
386 case Stmt::CXXThisExprClass:
387 K = CXCursor_CXXThisExpr;
388 break;
389
390 case Stmt::CXXThrowExprClass:
391 K = CXCursor_CXXThrowExpr;
392 break;
393
394 case Stmt::CXXNewExprClass:
395 K = CXCursor_CXXNewExpr;
396 break;
397
398 case Stmt::CXXDeleteExprClass:
399 K = CXCursor_CXXDeleteExpr;
400 break;
401
402 case Stmt::ObjCStringLiteralClass:
403 K = CXCursor_ObjCStringLiteral;
404 break;
405
406 case Stmt::ObjCEncodeExprClass:
407 K = CXCursor_ObjCEncodeExpr;
408 break;
409
410 case Stmt::ObjCSelectorExprClass:
411 K = CXCursor_ObjCSelectorExpr;
412 break;
413
414 case Stmt::ObjCProtocolExprClass:
415 K = CXCursor_ObjCProtocolExpr;
416 break;
Ted Kremenekb3f75422012-03-06 20:06:06 +0000417
418 case Stmt::ObjCBoolLiteralExprClass:
419 K = CXCursor_ObjCBoolLiteralExpr;
420 break;
421
Douglas Gregor42b29842011-10-05 19:00:14 +0000422 case Stmt::ObjCBridgedCastExprClass:
423 K = CXCursor_ObjCBridgedCastExpr;
424 break;
425
426 case Stmt::BlockExprClass:
427 K = CXCursor_BlockExpr;
428 break;
429
430 case Stmt::PackExpansionExprClass:
431 K = CXCursor_PackExpansionExpr;
432 break;
433
434 case Stmt::SizeOfPackExprClass:
435 K = CXCursor_SizeOfPackExpr;
436 break;
437
Argyrios Kyrtzidisedab0472013-04-23 17:57:17 +0000438 case Stmt::DeclRefExprClass:
439 if (const ImplicitParamDecl *IPD =
440 dyn_cast_or_null<ImplicitParamDecl>(cast<DeclRefExpr>(S)->getDecl())) {
441 if (const ObjCMethodDecl *MD =
442 dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
443 if (MD->getSelfDecl() == IPD) {
444 K = CXCursor_ObjCSelfExpr;
445 break;
446 }
447 }
448 }
449
450 K = CXCursor_DeclRefExpr;
451 break;
452
Douglas Gregor42b29842011-10-05 19:00:14 +0000453 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000454 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000455 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +0000456 case Stmt::FunctionParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000457 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000458 K = CXCursor_DeclRefExpr;
459 break;
460
Douglas Gregor42b29842011-10-05 19:00:14 +0000461 case Stmt::CXXDependentScopeMemberExprClass:
462 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000463 case Stmt::MemberExprClass:
John McCall76da55d2013-04-16 07:28:30 +0000464 case Stmt::MSPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000465 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000466 case Stmt::ObjCIvarRefExprClass:
467 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000468 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000469 K = CXCursor_MemberRefExpr;
470 break;
471
472 case Stmt::CallExprClass:
473 case Stmt::CXXOperatorCallExprClass:
474 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000475 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000476 case Stmt::CXXConstructExprClass:
477 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000478 case Stmt::CXXUnresolvedConstructExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000479 case Stmt::UserDefinedLiteralClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000480 K = CXCursor_CallExpr;
481 break;
482
Douglas Gregor011d8b92012-02-15 00:54:55 +0000483 case Stmt::LambdaExprClass:
484 K = CXCursor_LambdaExpr;
485 break;
486
Douglas Gregorba0513d2011-10-25 01:33:02 +0000487 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000488 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000489 int SelectorIdIndex = -1;
490 // Check if cursor points to a selector id.
491 if (RegionOfInterest.isValid() &&
492 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
493 SmallVector<SourceLocation, 16> SelLocs;
494 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
495 SmallVector<SourceLocation, 16>::iterator
496 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
497 if (I != SelLocs.end())
498 SelectorIdIndex = I - SelLocs.begin();
499 }
500 CXCursor C = { K, 0, { Parent, S, TU } };
501 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000502 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000503
504 case Stmt::MSDependentExistsStmtClass:
505 K = CXCursor_UnexposedStmt;
506 break;
507 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000508
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000509 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000510 return C;
511}
512
Douglas Gregor2e331b92010-01-16 14:00:32 +0000513CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000514 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000515 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000516 assert(Super && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000517 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000518 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000519 return C;
520}
521
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000522std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor2e331b92010-01-16 14:00:32 +0000523cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
524 assert(C.kind == CXCursor_ObjCSuperClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000525 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000526 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor2e331b92010-01-16 14:00:32 +0000527}
528
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000529CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000530 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000531 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000532 assert(Proto && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000533 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000534 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000535 return C;
536}
537
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000538std::pair<const ObjCProtocolDecl *, SourceLocation>
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000539cxcursor::getCursorObjCProtocolRef(CXCursor C) {
540 assert(C.kind == CXCursor_ObjCProtocolRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000541 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000542 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000543}
544
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000545CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000546 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000547 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000548 // 'Class' can be null for invalid code.
549 if (!Class)
550 return MakeCXCursorInvalid(CXCursor_InvalidCode);
551 assert(TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000552 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000553 CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000554 return C;
555}
556
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000557std::pair<const ObjCInterfaceDecl *, SourceLocation>
Douglas Gregor1adb0822010-01-16 17:14:40 +0000558cxcursor::getCursorObjCClassRef(CXCursor C) {
559 assert(C.kind == CXCursor_ObjCClassRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000560 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000561 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1adb0822010-01-16 17:14:40 +0000562}
563
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000564CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000565 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000566 assert(Type && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000567 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000568 CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000569 return C;
570}
571
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000572std::pair<const TypeDecl *, SourceLocation>
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000573cxcursor::getCursorTypeRef(CXCursor C) {
574 assert(C.kind == CXCursor_TypeRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000575 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000576 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000577}
578
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000579CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000580 SourceLocation Loc,
581 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000582 assert(Template && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000583 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000584 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000585 return C;
586}
587
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000588std::pair<const TemplateDecl *, SourceLocation>
Douglas Gregor0b36e612010-08-31 20:37:03 +0000589cxcursor::getCursorTemplateRef(CXCursor C) {
590 assert(C.kind == CXCursor_TemplateRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000591 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000592 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor0b36e612010-08-31 20:37:03 +0000593}
594
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000595CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
596 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000597 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000598
599 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
600 "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000601 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000602 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000603 return C;
604}
605
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000606std::pair<const NamedDecl *, SourceLocation>
Douglas Gregor69319002010-08-31 23:48:11 +0000607cxcursor::getCursorNamespaceRef(CXCursor C) {
608 assert(C.kind == CXCursor_NamespaceRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000609 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000610 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor69319002010-08-31 23:48:11 +0000611}
612
Douglas Gregor011d8b92012-02-15 00:54:55 +0000613CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
614 CXTranslationUnit TU) {
615
616 assert(Var && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000617 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000618 CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
Douglas Gregor011d8b92012-02-15 00:54:55 +0000619 return C;
620}
621
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000622std::pair<const VarDecl *, SourceLocation>
Douglas Gregor011d8b92012-02-15 00:54:55 +0000623cxcursor::getCursorVariableRef(CXCursor C) {
624 assert(C.kind == CXCursor_VariableRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000625 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000626 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor011d8b92012-02-15 00:54:55 +0000627}
628
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000629CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000630 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000631
632 assert(Field && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000633 void *RawLoc = Loc.getPtrEncoding();
Dmitri Gribenko33156182013-01-11 21:06:06 +0000634 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000635 return C;
636}
637
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000638std::pair<const FieldDecl *, SourceLocation>
Douglas Gregora67e03f2010-09-09 21:42:20 +0000639cxcursor::getCursorMemberRef(CXCursor C) {
640 assert(C.kind == CXCursor_MemberRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000641 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000642 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregora67e03f2010-09-09 21:42:20 +0000643}
644
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000645CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000646 CXTranslationUnit TU){
Dmitri Gribenko33156182013-01-11 21:06:06 +0000647 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000648 return C;
649}
650
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000651const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
Ted Kremenek3064ef92010-08-27 21:34:58 +0000652 assert(C.kind == CXCursor_CXXBaseSpecifier);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000653 return static_cast<const CXXBaseSpecifier*>(C.data[0]);
Ted Kremenek3064ef92010-08-27 21:34:58 +0000654}
655
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000656CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000657 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000658 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000659 { Range.getBegin().getPtrEncoding(),
660 Range.getEnd().getPtrEncoding(),
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000661 TU }
662 };
663 return C;
664}
665
666SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
667 assert(C.kind == CXCursor_PreprocessingDirective);
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000668 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
669 SourceLocation::getFromPtrEncoding(C.data[1]));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000670 ASTUnit *TU = getCursorASTUnit(C);
671 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000672}
673
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000674CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000675 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000676 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000677 return C;
678}
679
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000680const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
Douglas Gregor572feb22010-03-18 18:04:21 +0000681 assert(C.kind == CXCursor_MacroDefinition);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000682 return static_cast<const MacroDefinition *>(C.data[0]);
Douglas Gregor572feb22010-03-18 18:04:21 +0000683}
684
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000685CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
686 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000687 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000688 return C;
689}
690
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000691CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
692 SourceLocation Loc,
693 CXTranslationUnit TU) {
694 assert(Loc.isValid());
695 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
696 return C;
697}
698
699const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
700 if (isPseudo())
701 return getAsMacroDefinition()->getName();
702 return getAsMacroExpansion()->getName();
703}
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000704const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000705 if (isPseudo())
706 return getAsMacroDefinition();
707 return getAsMacroExpansion()->getDefinition();
708}
709SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
710 if (isPseudo())
711 return getPseudoLoc();
712 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +0000713}
714
Douglas Gregorecdcb882010-10-20 22:00:55 +0000715CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000716 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000717 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000718 return C;
719}
720
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000721const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
Douglas Gregorecdcb882010-10-20 22:00:55 +0000722 assert(C.kind == CXCursor_InclusionDirective);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000723 return static_cast<const InclusionDirective *>(C.data[0]);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000724}
725
Douglas Gregor36897b02010-09-10 00:22:18 +0000726CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000727 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000728
729 assert(Label && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000730 void *RawLoc = Loc.getPtrEncoding();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000731 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000732 return C;
733}
734
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000735std::pair<const LabelStmt *, SourceLocation>
Douglas Gregor36897b02010-09-10 00:22:18 +0000736cxcursor::getCursorLabelRef(CXCursor C) {
737 assert(C.kind == CXCursor_LabelRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000738 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000739 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor36897b02010-09-10 00:22:18 +0000740}
741
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000742CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000743 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000744 assert(E && TU && "Invalid arguments!");
745 OverloadedDeclRefStorage Storage(E);
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000746 void *RawLoc = E->getNameLoc().getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000747 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000748 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000749 { Storage.getOpaqueValue(), RawLoc, TU }
750 };
751 return C;
752}
753
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000754CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000755 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000756 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000757 assert(D && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000758 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000759 OverloadedDeclRefStorage Storage(D);
760 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000761 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000762 { Storage.getOpaqueValue(), RawLoc, TU }
763 };
764 return C;
765}
766
767CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
768 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000769 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000770 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
Dmitri Gribenkocb6bcf12013-02-16 01:07:48 +0000771 void *RawLoc = Loc.getPtrEncoding();
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000772 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
773 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000774 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000775 { Storage.getOpaqueValue(), RawLoc, TU }
776 };
777 return C;
778}
779
780std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
781cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
782 assert(C.kind == CXCursor_OverloadedDeclRef);
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000783 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
784 const_cast<void *>(C.data[0])),
Dmitri Gribenko62d0f562013-02-14 20:07:36 +0000785 SourceLocation::getFromPtrEncoding(C.data[1]));
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000786}
787
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000788const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
789 return static_cast<const Decl *>(Cursor.data[0]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000790}
791
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000792const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
Douglas Gregor283cae32010-01-15 21:56:13 +0000793 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
794}
795
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000796const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000797 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000798 Cursor.kind == CXCursor_ObjCProtocolRef ||
799 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000800 return 0;
801
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000802 return static_cast<const Stmt *>(Cursor.data[1]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000803}
804
Dmitri Gribenko7d914382013-01-26 18:08:08 +0000805const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
806 return static_cast<const Attr *>(Cursor.data[1]);
Ted Kremenek95f33552010-08-26 01:42:22 +0000807}
808
Dmitri Gribenko404628c2013-01-26 18:12:08 +0000809const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
810 return static_cast<const Decl *>(Cursor.data[0]);
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000811}
812
Douglas Gregorf46034a2010-01-18 23:41:10 +0000813ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000814 return getCursorASTUnit(Cursor)->getASTContext();
815}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000816
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000817ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko46f92522013-01-11 19:28:44 +0000818 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000819 if (!TU)
820 return 0;
Dmitri Gribenko5694feb2013-01-26 18:53:38 +0000821 return cxtu::getASTUnit(TU);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000822}
823
824CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +0000825 return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
Douglas Gregor283cae32010-01-15 21:56:13 +0000826}
827
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000828void cxcursor::getOverriddenCursors(CXCursor cursor,
829 SmallVectorImpl<CXCursor> &overridden) {
830 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000831 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000832 if (!D)
833 return;
834
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000835 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000836 SmallVector<const NamedDecl *, 8> OverDecls;
837 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000838
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000839 for (SmallVector<const NamedDecl *, 8>::iterator
840 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
Dmitri Gribenko05756dc2013-01-14 00:46:27 +0000841 overridden.push_back(MakeCXCursor(*I, TU));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000842 }
843}
844
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000845std::pair<int, SourceLocation>
846cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
847 if (cursor.kind == CXCursor_ObjCMessageExpr) {
848 if (cursor.xdata != -1)
849 return std::make_pair(cursor.xdata,
850 cast<ObjCMessageExpr>(getCursorExpr(cursor))
851 ->getSelectorLoc(cursor.xdata));
852 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
853 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
854 if (cursor.xdata != -1)
855 return std::make_pair(cursor.xdata,
856 cast<ObjCMethodDecl>(getCursorDecl(cursor))
857 ->getSelectorLoc(cursor.xdata));
858 }
859
860 return std::make_pair(-1, SourceLocation());
861}
862
863CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
864 CXCursor newCursor = cursor;
865
866 if (cursor.kind == CXCursor_ObjCMessageExpr) {
867 if (SelIdx == -1 ||
868 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
869 ->getNumSelectorLocs())
870 newCursor.xdata = -1;
871 else
872 newCursor.xdata = SelIdx;
873 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
874 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
875 if (SelIdx == -1 ||
876 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
877 ->getNumSelectorLocs())
878 newCursor.xdata = -1;
879 else
880 newCursor.xdata = SelIdx;
881 }
882
883 return newCursor;
884}
885
886CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
887 if (cursor.kind != CXCursor_CallExpr)
888 return cursor;
889
890 if (cursor.xdata == 0)
891 return cursor;
892
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000893 const Expr *E = getCursorExpr(cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000894 TypeSourceInfo *Type = 0;
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000895 if (const CXXUnresolvedConstructExpr *
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000896 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
897 Type = UnCtor->getTypeSourceInfo();
Dmitri Gribenkoff74f962013-01-26 15:29:08 +0000898 } else if (const CXXTemporaryObjectExpr *Tmp =
899 dyn_cast<CXXTemporaryObjectExpr>(E)){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000900 Type = Tmp->getTypeSourceInfo();
901 }
902
903 if (!Type)
904 return cursor;
905
906 CXTranslationUnit TU = getCursorTU(cursor);
907 QualType Ty = Type->getType();
908 TypeLoc TL = Type->getTypeLoc();
909 SourceLocation Loc = TL.getBeginLoc();
910
911 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
912 Ty = ElabT->getNamedType();
David Blaikie39e6ab42013-02-18 22:06:02 +0000913 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000914 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
915 }
916
917 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
918 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
919 if (const TagType *Tag = Ty->getAs<TagType>())
920 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
921 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
922 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
923
924 return cursor;
925}
926
Douglas Gregor283cae32010-01-15 21:56:13 +0000927bool cxcursor::operator==(CXCursor X, CXCursor Y) {
928 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
929 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000930}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000931
932// FIXME: Remove once we can model DeclGroups and their appropriate ranges
933// properly in the ASTs.
934bool cxcursor::isFirstInDeclGroup(CXCursor C) {
935 assert(clang_isDeclaration(C.kind));
936 return ((uintptr_t) (C.data[1])) != 0;
937}
938
Ted Kremenekeca099b2010-12-08 23:43:14 +0000939//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000940// libclang CXCursor APIs
941//===----------------------------------------------------------------------===//
942
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000943extern "C" {
944
945int clang_Cursor_isNull(CXCursor cursor) {
946 return clang_equalCursors(cursor, clang_getNullCursor());
947}
948
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000949CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
950 return getCursorTU(cursor);
951}
952
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000953int clang_Cursor_getNumArguments(CXCursor C) {
954 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000955 const Decl *D = cxcursor::getCursorDecl(C);
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000956 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
957 return MD->param_size();
958 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
959 return FD->param_size();
960 }
961
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000962 if (clang_isExpression(C.kind)) {
963 const Expr *E = cxcursor::getCursorExpr(C);
964 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
965 return CE->getNumArgs();
966 }
967 }
968
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000969 return -1;
970}
971
972CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
973 if (clang_isDeclaration(C.kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000974 const Decl *D = cxcursor::getCursorDecl(C);
975 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000976 if (i < MD->param_size())
977 return cxcursor::MakeCXCursor(MD->param_begin()[i],
978 cxcursor::getCursorTU(C));
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +0000979 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000980 if (i < FD->param_size())
981 return cxcursor::MakeCXCursor(FD->param_begin()[i],
982 cxcursor::getCursorTU(C));
983 }
984 }
985
Argyrios Kyrtzidise9ebd852013-04-01 17:38:59 +0000986 if (clang_isExpression(C.kind)) {
987 const Expr *E = cxcursor::getCursorExpr(C);
988 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
989 if (i < CE->getNumArgs()) {
990 return cxcursor::MakeCXCursor(CE->getArg(i),
991 getCursorDecl(C),
992 cxcursor::getCursorTU(C));
993 }
994 }
995 }
996
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000997 return clang_getNullCursor();
998}
999
Ted Kremenek017dd742013-04-24 07:17:12 +00001000} // end: extern "C"
1001
1002//===----------------------------------------------------------------------===//
1003// CXCursorSet.
1004//===----------------------------------------------------------------------===//
1005
1006typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1007
1008static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1009 return (CXCursorSet) setImpl;
1010}
1011static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1012 return (CXCursorSet_Impl*) set;
1013}
1014namespace llvm {
1015template<> struct DenseMapInfo<CXCursor> {
1016public:
1017 static inline CXCursor getEmptyKey() {
1018 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1019 }
1020 static inline CXCursor getTombstoneKey() {
1021 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1022 }
1023 static inline unsigned getHashValue(const CXCursor &cursor) {
1024 return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1025 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1026 }
1027 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1028 return x.kind == y.kind &&
1029 x.data[0] == y.data[0] &&
1030 x.data[1] == y.data[1];
1031 }
1032};
1033}
1034
1035extern "C" {
1036CXCursorSet clang_createCXCursorSet() {
1037 return packCXCursorSet(new CXCursorSet_Impl());
1038}
1039
1040void clang_disposeCXCursorSet(CXCursorSet set) {
1041 delete unpackCXCursorSet(set);
1042}
1043
1044unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1045 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1046 if (!setImpl)
1047 return 0;
1048 return setImpl->find(cursor) == setImpl->end();
1049}
1050
1051unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1052 // Do not insert invalid cursors into the set.
1053 if (cursor.kind >= CXCursor_FirstInvalid &&
1054 cursor.kind <= CXCursor_LastInvalid)
1055 return 1;
1056
1057 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1058 if (!setImpl)
1059 return 1;
1060 unsigned &entry = (*setImpl)[cursor];
1061 unsigned flag = entry == 0 ? 1 : 0;
1062 entry = 1;
1063 return flag;
1064}
1065
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001066CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1067 enum CXCursorKind kind = clang_getCursorKind(cursor);
1068 if (clang_isDeclaration(kind)) {
Dmitri Gribenkoe22339c2013-01-23 17:25:27 +00001069 const Decl *decl = getCursorDecl(cursor);
1070 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001071 ASTUnit *unit = getCursorASTUnit(cursor);
Douglas Gregord1f09b42013-01-31 04:52:16 +00001072 CodeCompletionResult Result(namedDecl, CCP_Declaration);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001073 CodeCompletionString *String
1074 = Result.CreateCodeCompletionString(unit->getASTContext(),
1075 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001076 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001077 unit->getCodeCompletionTUInfo(),
1078 true);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001079 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001080 }
1081 }
1082 else if (kind == CXCursor_MacroDefinition) {
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001083 const MacroDefinition *definition = getCursorMacroDefinition(cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001084 const IdentifierInfo *MacroInfo = definition->getName();
1085 ASTUnit *unit = getCursorASTUnit(cursor);
Dmitri Gribenkob3958472013-01-14 00:36:42 +00001086 CodeCompletionResult Result(MacroInfo);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001087 CodeCompletionString *String
1088 = Result.CreateCodeCompletionString(unit->getASTContext(),
1089 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001090 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001091 unit->getCodeCompletionTUInfo(),
1092 false);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001093 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001094 }
1095 return NULL;
1096}
Ted Kremenek8eece462012-04-30 19:33:45 +00001097} // end: extern C.
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001098
1099namespace {
1100 struct OverridenCursorsPool {
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +00001101 typedef SmallVector<CXCursor, 2> CursorVec;
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001102 std::vector<CursorVec*> AllCursors;
1103 std::vector<CursorVec*> AvailableCursors;
1104
1105 ~OverridenCursorsPool() {
1106 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1107 E = AllCursors.end(); I != E; ++I) {
1108 delete *I;
1109 }
1110 }
1111 };
1112}
1113
1114void *cxcursor::createOverridenCXCursorsPool() {
1115 return new OverridenCursorsPool();
1116}
1117
1118void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1119 delete static_cast<OverridenCursorsPool*>(pool);
1120}
Ted Kremenek8eece462012-04-30 19:33:45 +00001121
1122extern "C" {
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001123void clang_getOverriddenCursors(CXCursor cursor,
1124 CXCursor **overridden,
1125 unsigned *num_overridden) {
1126 if (overridden)
1127 *overridden = 0;
1128 if (num_overridden)
1129 *num_overridden = 0;
1130
1131 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1132
1133 if (!overridden || !num_overridden || !TU)
1134 return;
1135
1136 if (!clang_isDeclaration(cursor.kind))
1137 return;
1138
1139 OverridenCursorsPool &pool =
1140 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1141
1142 OverridenCursorsPool::CursorVec *Vec = 0;
1143
1144 if (!pool.AvailableCursors.empty()) {
1145 Vec = pool.AvailableCursors.back();
1146 pool.AvailableCursors.pop_back();
1147 }
1148 else {
1149 Vec = new OverridenCursorsPool::CursorVec();
1150 pool.AllCursors.push_back(Vec);
1151 }
1152
1153 // Clear out the vector, but don't free the memory contents. This
1154 // reduces malloc() traffic.
1155 Vec->clear();
1156
1157 // Use the first entry to contain a back reference to the vector.
1158 // This is a complete hack.
1159 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1160 backRefCursor.data[0] = Vec;
1161 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1162 Vec->push_back(backRefCursor);
1163
1164 // Get the overriden cursors.
1165 cxcursor::getOverriddenCursors(cursor, *Vec);
1166
1167 // Did we get any overriden cursors? If not, return Vec to the pool
1168 // of available cursor vectors.
1169 if (Vec->size() == 1) {
1170 pool.AvailableCursors.push_back(Vec);
1171 return;
1172 }
1173
1174 // Now tell the caller about the overriden cursors.
1175 assert(Vec->size() > 1);
1176 *overridden = &((*Vec)[1]);
1177 *num_overridden = Vec->size() - 1;
1178}
1179
1180void clang_disposeOverriddenCursors(CXCursor *overridden) {
1181 if (!overridden)
1182 return;
1183
1184 // Use pointer arithmetic to get back the first faux entry
1185 // which has a back-reference to the TU and the vector.
1186 --overridden;
1187 OverridenCursorsPool::CursorVec *Vec =
Dmitri Gribenko67812b22013-01-11 21:01:49 +00001188 static_cast<OverridenCursorsPool::CursorVec *>(
1189 const_cast<void *>(overridden->data[0]));
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001190 CXTranslationUnit TU = getCursorTU(*overridden);
1191
1192 assert(Vec && TU);
1193
1194 OverridenCursorsPool &pool =
1195 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1196
1197 pool.AvailableCursors.push_back(Vec);
1198}
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001199
1200int clang_Cursor_isDynamicCall(CXCursor C) {
1201 const Expr *E = 0;
1202 if (clang_isExpression(C.kind))
1203 E = getCursorExpr(C);
1204 if (!E)
1205 return 0;
1206
1207 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1208 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1209
1210 const MemberExpr *ME = 0;
1211 if (isa<MemberExpr>(E))
1212 ME = cast<MemberExpr>(E);
1213 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1214 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1215
1216 if (ME) {
1217 if (const CXXMethodDecl *
1218 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1219 return MD->isVirtual() && !ME->hasQualifier();
1220 }
1221
1222 return 0;
1223}
1224
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001225CXType clang_Cursor_getReceiverType(CXCursor C) {
1226 CXTranslationUnit TU = cxcursor::getCursorTU(C);
1227 const Expr *E = 0;
1228 if (clang_isExpression(C.kind))
1229 E = getCursorExpr(C);
1230
1231 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1232 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1233
1234 return cxtype::MakeCXType(QualType(), TU);
1235}
1236
Ted Kremenekeca099b2010-12-08 23:43:14 +00001237} // end: extern "C"