blob: 8a9cf233c07af479fb26b0d04054447870b10bc9 [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
Ted Kremeneka60ed472010-11-16 08:15:36 +000056CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
57 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000058 assert(A && Parent && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000059 CXCursor C = { GetCursorKind(A), 0, { Parent, (void*)A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000060 return C;
61}
62
Ted Kremeneka60ed472010-11-16 08:15:36 +000063CXCursor cxcursor::MakeCXCursor(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
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000092CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU,
93 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000094 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000095 CXCursorKind K = CXCursor_NotImplemented;
96
97 switch (S->getStmtClass()) {
98 case Stmt::NoStmtClass:
99 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000100
Douglas Gregor97b98722010-01-19 23:20:36 +0000101 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000102 K = CXCursor_CaseStmt;
103 break;
104
Douglas Gregor97b98722010-01-19 23:20:36 +0000105 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000106 K = CXCursor_DefaultStmt;
107 break;
108
109 case Stmt::IfStmtClass:
110 K = CXCursor_IfStmt;
111 break;
112
113 case Stmt::SwitchStmtClass:
114 K = CXCursor_SwitchStmt;
115 break;
116
117 case Stmt::WhileStmtClass:
118 K = CXCursor_WhileStmt;
119 break;
120
121 case Stmt::DoStmtClass:
122 K = CXCursor_DoStmt;
123 break;
124
125 case Stmt::ForStmtClass:
126 K = CXCursor_ForStmt;
127 break;
128
129 case Stmt::GotoStmtClass:
130 K = CXCursor_GotoStmt;
131 break;
132
Douglas Gregor97b98722010-01-19 23:20:36 +0000133 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000134 K = CXCursor_IndirectGotoStmt;
135 break;
136
137 case Stmt::ContinueStmtClass:
138 K = CXCursor_ContinueStmt;
139 break;
140
141 case Stmt::BreakStmtClass:
142 K = CXCursor_BreakStmt;
143 break;
144
145 case Stmt::ReturnStmtClass:
146 K = CXCursor_ReturnStmt;
147 break;
148
Chad Rosierdf5faf52012-08-25 00:11:56 +0000149 case Stmt::GCCAsmStmtClass:
150 K = CXCursor_GCCAsmStmt;
Douglas Gregor42b29842011-10-05 19:00:14 +0000151 break;
Chad Rosier8cd64b42012-06-11 20:47:18 +0000152
153 case Stmt::MSAsmStmtClass:
154 K = CXCursor_MSAsmStmt;
155 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000156
157 case Stmt::ObjCAtTryStmtClass:
158 K = CXCursor_ObjCAtTryStmt;
159 break;
160
161 case Stmt::ObjCAtCatchStmtClass:
162 K = CXCursor_ObjCAtCatchStmt;
163 break;
164
165 case Stmt::ObjCAtFinallyStmtClass:
166 K = CXCursor_ObjCAtFinallyStmt;
167 break;
168
169 case Stmt::ObjCAtThrowStmtClass:
170 K = CXCursor_ObjCAtThrowStmt;
171 break;
172
173 case Stmt::ObjCAtSynchronizedStmtClass:
174 K = CXCursor_ObjCAtSynchronizedStmt;
175 break;
176
177 case Stmt::ObjCAutoreleasePoolStmtClass:
178 K = CXCursor_ObjCAutoreleasePoolStmt;
179 break;
180
Douglas Gregor97b98722010-01-19 23:20:36 +0000181 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000182 K = CXCursor_ObjCForCollectionStmt;
183 break;
184
Douglas Gregor97b98722010-01-19 23:20:36 +0000185 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000186 K = CXCursor_CXXCatchStmt;
187 break;
188
189 case Stmt::CXXTryStmtClass:
190 K = CXCursor_CXXTryStmt;
191 break;
192
193 case Stmt::CXXForRangeStmtClass:
194 K = CXCursor_CXXForRangeStmt;
195 break;
196
John Wiegley28bbe4b2011-04-28 01:08:34 +0000197 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000198 K = CXCursor_SEHTryStmt;
199 break;
200
John Wiegley28bbe4b2011-04-28 01:08:34 +0000201 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000202 K = CXCursor_SEHExceptStmt;
203 break;
204
John Wiegley28bbe4b2011-04-28 01:08:34 +0000205 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000206 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000207 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000208
John Wiegley21ff2e52011-04-28 00:16:57 +0000209 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000210 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000211 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000212 case Stmt::BinaryConditionalOperatorClass:
213 case Stmt::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000214 case Stmt::TypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000215 case Stmt::CXXBindTemporaryExprClass:
216 case Stmt::CXXDefaultArgExprClass:
217 case Stmt::CXXScalarValueInitExprClass:
218 case Stmt::CXXUuidofExprClass:
219 case Stmt::ChooseExprClass:
220 case Stmt::DesignatedInitExprClass:
221 case Stmt::ExprWithCleanupsClass:
222 case Stmt::ExpressionTraitExprClass:
223 case Stmt::ExtVectorElementExprClass:
224 case Stmt::ImplicitCastExprClass:
225 case Stmt::ImplicitValueInitExprClass:
226 case Stmt::MaterializeTemporaryExprClass:
227 case Stmt::ObjCIndirectCopyRestoreExprClass:
228 case Stmt::OffsetOfExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000229 case Stmt::ParenListExprClass:
230 case Stmt::PredefinedExprClass:
231 case Stmt::ShuffleVectorExprClass:
232 case Stmt::UnaryExprOrTypeTraitExprClass:
233 case Stmt::UnaryTypeTraitExprClass:
234 case Stmt::VAArgExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000235 case Stmt::ObjCArrayLiteralClass:
236 case Stmt::ObjCDictionaryLiteralClass:
Patrick Beardeb382ec2012-04-19 00:25:12 +0000237 case Stmt::ObjCBoxedExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000238 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000239 K = CXCursor_UnexposedExpr;
240 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000241
John McCall4b9c2d22011-11-06 09:01:30 +0000242 case Stmt::OpaqueValueExprClass:
243 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
244 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
245 K = CXCursor_UnexposedExpr;
246 break;
247
248 case Stmt::PseudoObjectExprClass:
249 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
250 Parent, TU, RegionOfInterest);
251
Douglas Gregor42b29842011-10-05 19:00:14 +0000252 case Stmt::CompoundStmtClass:
253 K = CXCursor_CompoundStmt;
254 break;
Richard Smith534986f2012-04-14 00:33:13 +0000255
Douglas Gregor42b29842011-10-05 19:00:14 +0000256 case Stmt::NullStmtClass:
257 K = CXCursor_NullStmt;
258 break;
Richard Smith534986f2012-04-14 00:33:13 +0000259
Douglas Gregor42b29842011-10-05 19:00:14 +0000260 case Stmt::LabelStmtClass:
261 K = CXCursor_LabelStmt;
262 break;
Richard Smith534986f2012-04-14 00:33:13 +0000263
264 case Stmt::AttributedStmtClass:
265 K = CXCursor_UnexposedStmt;
266 break;
267
Douglas Gregor42b29842011-10-05 19:00:14 +0000268 case Stmt::DeclStmtClass:
269 K = CXCursor_DeclStmt;
270 break;
Richard Smith534986f2012-04-14 00:33:13 +0000271
Douglas Gregor42b29842011-10-05 19:00:14 +0000272 case Stmt::IntegerLiteralClass:
273 K = CXCursor_IntegerLiteral;
274 break;
275
276 case Stmt::FloatingLiteralClass:
277 K = CXCursor_FloatingLiteral;
278 break;
279
280 case Stmt::ImaginaryLiteralClass:
281 K = CXCursor_ImaginaryLiteral;
282 break;
283
284 case Stmt::StringLiteralClass:
285 K = CXCursor_StringLiteral;
286 break;
287
288 case Stmt::CharacterLiteralClass:
289 K = CXCursor_CharacterLiteral;
290 break;
291
292 case Stmt::ParenExprClass:
293 K = CXCursor_ParenExpr;
294 break;
295
296 case Stmt::UnaryOperatorClass:
297 K = CXCursor_UnaryOperator;
298 break;
Richard Smith534986f2012-04-14 00:33:13 +0000299
Douglas Gregor42b29842011-10-05 19:00:14 +0000300 case Stmt::CXXNoexceptExprClass:
301 K = CXCursor_UnaryExpr;
302 break;
303
304 case Stmt::ArraySubscriptExprClass:
305 K = CXCursor_ArraySubscriptExpr;
306 break;
307
308 case Stmt::BinaryOperatorClass:
309 K = CXCursor_BinaryOperator;
310 break;
311
312 case Stmt::CompoundAssignOperatorClass:
313 K = CXCursor_CompoundAssignOperator;
314 break;
315
316 case Stmt::ConditionalOperatorClass:
317 K = CXCursor_ConditionalOperator;
318 break;
319
320 case Stmt::CStyleCastExprClass:
321 K = CXCursor_CStyleCastExpr;
322 break;
323
324 case Stmt::CompoundLiteralExprClass:
325 K = CXCursor_CompoundLiteralExpr;
326 break;
327
328 case Stmt::InitListExprClass:
329 K = CXCursor_InitListExpr;
330 break;
331
332 case Stmt::AddrLabelExprClass:
333 K = CXCursor_AddrLabelExpr;
334 break;
335
336 case Stmt::StmtExprClass:
337 K = CXCursor_StmtExpr;
338 break;
339
340 case Stmt::GenericSelectionExprClass:
341 K = CXCursor_GenericSelectionExpr;
342 break;
343
344 case Stmt::GNUNullExprClass:
345 K = CXCursor_GNUNullExpr;
346 break;
347
348 case Stmt::CXXStaticCastExprClass:
349 K = CXCursor_CXXStaticCastExpr;
350 break;
351
352 case Stmt::CXXDynamicCastExprClass:
353 K = CXCursor_CXXDynamicCastExpr;
354 break;
355
356 case Stmt::CXXReinterpretCastExprClass:
357 K = CXCursor_CXXReinterpretCastExpr;
358 break;
359
360 case Stmt::CXXConstCastExprClass:
361 K = CXCursor_CXXConstCastExpr;
362 break;
363
364 case Stmt::CXXFunctionalCastExprClass:
365 K = CXCursor_CXXFunctionalCastExpr;
366 break;
367
368 case Stmt::CXXTypeidExprClass:
369 K = CXCursor_CXXTypeidExpr;
370 break;
371
372 case Stmt::CXXBoolLiteralExprClass:
373 K = CXCursor_CXXBoolLiteralExpr;
374 break;
375
376 case Stmt::CXXNullPtrLiteralExprClass:
377 K = CXCursor_CXXNullPtrLiteralExpr;
378 break;
379
380 case Stmt::CXXThisExprClass:
381 K = CXCursor_CXXThisExpr;
382 break;
383
384 case Stmt::CXXThrowExprClass:
385 K = CXCursor_CXXThrowExpr;
386 break;
387
388 case Stmt::CXXNewExprClass:
389 K = CXCursor_CXXNewExpr;
390 break;
391
392 case Stmt::CXXDeleteExprClass:
393 K = CXCursor_CXXDeleteExpr;
394 break;
395
396 case Stmt::ObjCStringLiteralClass:
397 K = CXCursor_ObjCStringLiteral;
398 break;
399
400 case Stmt::ObjCEncodeExprClass:
401 K = CXCursor_ObjCEncodeExpr;
402 break;
403
404 case Stmt::ObjCSelectorExprClass:
405 K = CXCursor_ObjCSelectorExpr;
406 break;
407
408 case Stmt::ObjCProtocolExprClass:
409 K = CXCursor_ObjCProtocolExpr;
410 break;
Ted Kremenekb3f75422012-03-06 20:06:06 +0000411
412 case Stmt::ObjCBoolLiteralExprClass:
413 K = CXCursor_ObjCBoolLiteralExpr;
414 break;
415
Douglas Gregor42b29842011-10-05 19:00:14 +0000416 case Stmt::ObjCBridgedCastExprClass:
417 K = CXCursor_ObjCBridgedCastExpr;
418 break;
419
420 case Stmt::BlockExprClass:
421 K = CXCursor_BlockExpr;
422 break;
423
424 case Stmt::PackExpansionExprClass:
425 K = CXCursor_PackExpansionExpr;
426 break;
427
428 case Stmt::SizeOfPackExprClass:
429 K = CXCursor_SizeOfPackExpr;
430 break;
431
Douglas Gregor42b29842011-10-05 19:00:14 +0000432 case Stmt::DeclRefExprClass:
433 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000434 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000435 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Richard Smith9a4db032012-09-12 00:56:43 +0000436 case Stmt::FunctionParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000437 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000438 K = CXCursor_DeclRefExpr;
439 break;
440
Douglas Gregor42b29842011-10-05 19:00:14 +0000441 case Stmt::CXXDependentScopeMemberExprClass:
442 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000443 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000444 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000445 case Stmt::ObjCIvarRefExprClass:
446 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000447 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000448 K = CXCursor_MemberRefExpr;
449 break;
450
451 case Stmt::CallExprClass:
452 case Stmt::CXXOperatorCallExprClass:
453 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000454 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000455 case Stmt::CXXConstructExprClass:
456 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000457 case Stmt::CXXUnresolvedConstructExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +0000458 case Stmt::UserDefinedLiteralClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000459 K = CXCursor_CallExpr;
460 break;
461
Douglas Gregor011d8b92012-02-15 00:54:55 +0000462 case Stmt::LambdaExprClass:
463 K = CXCursor_LambdaExpr;
464 break;
465
Douglas Gregorba0513d2011-10-25 01:33:02 +0000466 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000467 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000468 int SelectorIdIndex = -1;
469 // Check if cursor points to a selector id.
470 if (RegionOfInterest.isValid() &&
471 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
472 SmallVector<SourceLocation, 16> SelLocs;
473 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
474 SmallVector<SourceLocation, 16>::iterator
475 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
476 if (I != SelLocs.end())
477 SelectorIdIndex = I - SelLocs.begin();
478 }
479 CXCursor C = { K, 0, { Parent, S, TU } };
480 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000481 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000482
483 case Stmt::MSDependentExistsStmtClass:
484 K = CXCursor_UnexposedStmt;
485 break;
486 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000487
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000488 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000489 return C;
490}
491
Douglas Gregor2e331b92010-01-16 14:00:32 +0000492CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000493 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000494 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000495 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000496 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000497 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000498 return C;
499}
500
501std::pair<ObjCInterfaceDecl *, SourceLocation>
502cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
503 assert(C.kind == CXCursor_ObjCSuperClassRef);
504 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
505 SourceLocation::getFromRawEncoding(
506 reinterpret_cast<uintptr_t>(C.data[1])));
507}
508
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000509CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000510 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000511 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000512 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000513 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000514 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000515 return C;
516}
517
518std::pair<ObjCProtocolDecl *, SourceLocation>
519cxcursor::getCursorObjCProtocolRef(CXCursor C) {
520 assert(C.kind == CXCursor_ObjCProtocolRef);
521 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
522 SourceLocation::getFromRawEncoding(
523 reinterpret_cast<uintptr_t>(C.data[1])));
524}
525
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000526CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000527 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000528 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000529 // 'Class' can be null for invalid code.
530 if (!Class)
531 return MakeCXCursorInvalid(CXCursor_InvalidCode);
532 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000533 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000534 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000535 return C;
536}
537
538std::pair<ObjCInterfaceDecl *, SourceLocation>
539cxcursor::getCursorObjCClassRef(CXCursor C) {
540 assert(C.kind == CXCursor_ObjCClassRef);
541 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
542 SourceLocation::getFromRawEncoding(
543 reinterpret_cast<uintptr_t>(C.data[1])));
544}
545
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000546CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000547 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000548 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000549 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000550 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000551 return C;
552}
553
554std::pair<TypeDecl *, SourceLocation>
555cxcursor::getCursorTypeRef(CXCursor C) {
556 assert(C.kind == CXCursor_TypeRef);
557 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
558 SourceLocation::getFromRawEncoding(
559 reinterpret_cast<uintptr_t>(C.data[1])));
560}
561
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000562CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000563 SourceLocation Loc,
564 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000565 assert(Template && TU && "Invalid arguments!");
566 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000567 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000568 return C;
569}
570
571std::pair<TemplateDecl *, SourceLocation>
572cxcursor::getCursorTemplateRef(CXCursor C) {
573 assert(C.kind == CXCursor_TemplateRef);
574 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
575 SourceLocation::getFromRawEncoding(
576 reinterpret_cast<uintptr_t>(C.data[1])));
577}
578
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000579CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
580 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000581 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000582
583 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
584 "Invalid arguments!");
585 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000586 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000587 return C;
588}
589
590std::pair<NamedDecl *, SourceLocation>
591cxcursor::getCursorNamespaceRef(CXCursor C) {
592 assert(C.kind == CXCursor_NamespaceRef);
593 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
594 SourceLocation::getFromRawEncoding(
595 reinterpret_cast<uintptr_t>(C.data[1])));
596}
597
Douglas Gregor011d8b92012-02-15 00:54:55 +0000598CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
599 CXTranslationUnit TU) {
600
601 assert(Var && TU && "Invalid arguments!");
602 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
603 CXCursor C = { CXCursor_VariableRef, 0, { (void*)Var, RawLoc, TU } };
604 return C;
605}
606
607std::pair<VarDecl *, SourceLocation>
608cxcursor::getCursorVariableRef(CXCursor C) {
609 assert(C.kind == CXCursor_VariableRef);
610 return std::make_pair(static_cast<VarDecl *>(C.data[0]),
611 SourceLocation::getFromRawEncoding(
612 reinterpret_cast<uintptr_t>(C.data[1])));
613}
614
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000615CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000616 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000617
618 assert(Field && TU && "Invalid arguments!");
619 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000620 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000621 return C;
622}
623
624std::pair<FieldDecl *, SourceLocation>
625cxcursor::getCursorMemberRef(CXCursor C) {
626 assert(C.kind == CXCursor_MemberRef);
627 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
628 SourceLocation::getFromRawEncoding(
629 reinterpret_cast<uintptr_t>(C.data[1])));
630}
631
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000632CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000633 CXTranslationUnit TU){
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000634 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000635 return C;
636}
637
638CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
639 assert(C.kind == CXCursor_CXXBaseSpecifier);
640 return static_cast<CXXBaseSpecifier*>(C.data[0]);
641}
642
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000643CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000644 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000645 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000646 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
647 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
648 TU }
649 };
650 return C;
651}
652
653SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
654 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000655 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000656 reinterpret_cast<uintptr_t> (C.data[0])),
657 SourceLocation::getFromRawEncoding(
658 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000659 ASTUnit *TU = getCursorASTUnit(C);
660 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000661}
662
Ted Kremeneka60ed472010-11-16 08:15:36 +0000663CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
664 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000665 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000666 return C;
667}
668
669MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
670 assert(C.kind == CXCursor_MacroDefinition);
671 return static_cast<MacroDefinition *>(C.data[0]);
672}
673
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000674CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
675 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000676 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000677 return C;
678}
679
Argyrios Kyrtzidis664b06f2013-01-07 19:16:25 +0000680CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
681 SourceLocation Loc,
682 CXTranslationUnit TU) {
683 assert(Loc.isValid());
684 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
685 return C;
686}
687
688const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
689 if (isPseudo())
690 return getAsMacroDefinition()->getName();
691 return getAsMacroExpansion()->getName();
692}
693MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
694 if (isPseudo())
695 return getAsMacroDefinition();
696 return getAsMacroExpansion()->getDefinition();
697}
698SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
699 if (isPseudo())
700 return getPseudoLoc();
701 return getAsMacroExpansion()->getSourceRange();
Douglas Gregor48072312010-03-18 15:23:44 +0000702}
703
Douglas Gregorecdcb882010-10-20 22:00:55 +0000704CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000705 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000706 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000707 return C;
708}
709
710InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
711 assert(C.kind == CXCursor_InclusionDirective);
712 return static_cast<InclusionDirective *>(C.data[0]);
713}
714
Douglas Gregor36897b02010-09-10 00:22:18 +0000715CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000716 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000717
718 assert(Label && TU && "Invalid arguments!");
719 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000720 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000721 return C;
722}
723
724std::pair<LabelStmt*, SourceLocation>
725cxcursor::getCursorLabelRef(CXCursor C) {
726 assert(C.kind == CXCursor_LabelRef);
727 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
728 SourceLocation::getFromRawEncoding(
729 reinterpret_cast<uintptr_t>(C.data[1])));
730}
731
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000732CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000733 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000734 assert(E && TU && "Invalid arguments!");
735 OverloadedDeclRefStorage Storage(E);
736 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
737 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000738 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000739 { Storage.getOpaqueValue(), RawLoc, TU }
740 };
741 return C;
742}
743
744CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
745 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000746 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000747 assert(D && TU && "Invalid arguments!");
748 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
749 OverloadedDeclRefStorage Storage(D);
750 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000751 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000752 { Storage.getOpaqueValue(), RawLoc, TU }
753 };
754 return C;
755}
756
757CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
758 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000759 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000760 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
761 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
762 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
763 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000764 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000765 { Storage.getOpaqueValue(), RawLoc, TU }
766 };
767 return C;
768}
769
770std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
771cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
772 assert(C.kind == CXCursor_OverloadedDeclRef);
773 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
774 SourceLocation::getFromRawEncoding(
775 reinterpret_cast<uintptr_t>(C.data[1])));
776}
777
Douglas Gregor283cae32010-01-15 21:56:13 +0000778Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
779 return (Decl *)Cursor.data[0];
780}
781
782Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
783 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
784}
785
786Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000787 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000788 Cursor.kind == CXCursor_ObjCProtocolRef ||
789 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000790 return 0;
791
Douglas Gregor283cae32010-01-15 21:56:13 +0000792 return (Stmt *)Cursor.data[1];
793}
794
Ted Kremenek95f33552010-08-26 01:42:22 +0000795Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
796 return (Attr *)Cursor.data[1];
797}
798
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000799Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
800 return (Decl *)Cursor.data[0];
801}
802
Douglas Gregorf46034a2010-01-18 23:41:10 +0000803ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000804 return getCursorASTUnit(Cursor)->getASTContext();
805}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000806
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000807ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Dmitri Gribenko46f92522013-01-11 19:28:44 +0000808 CXTranslationUnit TU = getCursorTU(Cursor);
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000809 if (!TU)
810 return 0;
811 return static_cast<ASTUnit *>(TU->TUData);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000812}
813
814CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
815 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000816}
817
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000818void cxcursor::getOverriddenCursors(CXCursor cursor,
819 SmallVectorImpl<CXCursor> &overridden) {
820 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000821 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000822 if (!D)
823 return;
824
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000825 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000826 SmallVector<const NamedDecl *, 8> OverDecls;
827 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000828
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000829 for (SmallVector<const NamedDecl *, 8>::iterator
830 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
831 overridden.push_back(MakeCXCursor(const_cast<NamedDecl*>(*I), TU));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000832 }
833}
834
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000835std::pair<int, SourceLocation>
836cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
837 if (cursor.kind == CXCursor_ObjCMessageExpr) {
838 if (cursor.xdata != -1)
839 return std::make_pair(cursor.xdata,
840 cast<ObjCMessageExpr>(getCursorExpr(cursor))
841 ->getSelectorLoc(cursor.xdata));
842 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
843 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
844 if (cursor.xdata != -1)
845 return std::make_pair(cursor.xdata,
846 cast<ObjCMethodDecl>(getCursorDecl(cursor))
847 ->getSelectorLoc(cursor.xdata));
848 }
849
850 return std::make_pair(-1, SourceLocation());
851}
852
853CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
854 CXCursor newCursor = cursor;
855
856 if (cursor.kind == CXCursor_ObjCMessageExpr) {
857 if (SelIdx == -1 ||
858 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
859 ->getNumSelectorLocs())
860 newCursor.xdata = -1;
861 else
862 newCursor.xdata = SelIdx;
863 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
864 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
865 if (SelIdx == -1 ||
866 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
867 ->getNumSelectorLocs())
868 newCursor.xdata = -1;
869 else
870 newCursor.xdata = SelIdx;
871 }
872
873 return newCursor;
874}
875
876CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
877 if (cursor.kind != CXCursor_CallExpr)
878 return cursor;
879
880 if (cursor.xdata == 0)
881 return cursor;
882
883 Expr *E = getCursorExpr(cursor);
884 TypeSourceInfo *Type = 0;
885 if (CXXUnresolvedConstructExpr *
886 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
887 Type = UnCtor->getTypeSourceInfo();
888 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
889 Type = Tmp->getTypeSourceInfo();
890 }
891
892 if (!Type)
893 return cursor;
894
895 CXTranslationUnit TU = getCursorTU(cursor);
896 QualType Ty = Type->getType();
897 TypeLoc TL = Type->getTypeLoc();
898 SourceLocation Loc = TL.getBeginLoc();
899
900 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
901 Ty = ElabT->getNamedType();
902 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
903 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
904 }
905
906 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
907 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
908 if (const TagType *Tag = Ty->getAs<TagType>())
909 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
910 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
911 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
912
913 return cursor;
914}
915
Douglas Gregor283cae32010-01-15 21:56:13 +0000916bool cxcursor::operator==(CXCursor X, CXCursor Y) {
917 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
918 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000919}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000920
921// FIXME: Remove once we can model DeclGroups and their appropriate ranges
922// properly in the ASTs.
923bool cxcursor::isFirstInDeclGroup(CXCursor C) {
924 assert(clang_isDeclaration(C.kind));
925 return ((uintptr_t) (C.data[1])) != 0;
926}
927
Ted Kremenekeca099b2010-12-08 23:43:14 +0000928//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000929// libclang CXCursor APIs
930//===----------------------------------------------------------------------===//
931
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000932extern "C" {
933
934int clang_Cursor_isNull(CXCursor cursor) {
935 return clang_equalCursors(cursor, clang_getNullCursor());
936}
937
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000938CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
939 return getCursorTU(cursor);
940}
941
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000942int clang_Cursor_getNumArguments(CXCursor C) {
943 if (clang_isDeclaration(C.kind)) {
944 Decl *D = cxcursor::getCursorDecl(C);
945 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
946 return MD->param_size();
947 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
948 return FD->param_size();
949 }
950
951 return -1;
952}
953
954CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
955 if (clang_isDeclaration(C.kind)) {
956 Decl *D = cxcursor::getCursorDecl(C);
957 if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
958 if (i < MD->param_size())
959 return cxcursor::MakeCXCursor(MD->param_begin()[i],
960 cxcursor::getCursorTU(C));
961 } else if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
962 if (i < FD->param_size())
963 return cxcursor::MakeCXCursor(FD->param_begin()[i],
964 cxcursor::getCursorTU(C));
965 }
966 }
967
968 return clang_getNullCursor();
969}
970
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000971} // end: extern "C"
972
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000973//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000974// CXCursorSet.
975//===----------------------------------------------------------------------===//
976
977typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
978
979static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
980 return (CXCursorSet) setImpl;
981}
982static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
983 return (CXCursorSet_Impl*) set;
984}
985namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000986template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000987public:
988 static inline CXCursor getEmptyKey() {
989 return MakeCXCursorInvalid(CXCursor_InvalidFile);
990 }
991 static inline CXCursor getTombstoneKey() {
992 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
993 }
994 static inline unsigned getHashValue(const CXCursor &cursor) {
995 return llvm::DenseMapInfo<std::pair<void*,void*> >
996 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
997 }
998 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
999 return x.kind == y.kind &&
1000 x.data[0] == y.data[0] &&
1001 x.data[1] == y.data[1];
1002 }
1003};
1004}
1005
1006extern "C" {
1007CXCursorSet clang_createCXCursorSet() {
1008 return packCXCursorSet(new CXCursorSet_Impl());
1009}
1010
1011void clang_disposeCXCursorSet(CXCursorSet set) {
1012 delete unpackCXCursorSet(set);
1013}
1014
1015unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1016 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1017 if (!setImpl)
1018 return 0;
1019 return setImpl->find(cursor) == setImpl->end();
1020}
1021
Anders Carlssone8b3de02010-12-09 01:00:12 +00001022unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001023 // Do not insert invalid cursors into the set.
1024 if (cursor.kind >= CXCursor_FirstInvalid &&
1025 cursor.kind <= CXCursor_LastInvalid)
1026 return 1;
1027
1028 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1029 if (!setImpl)
1030 return 1;
1031 unsigned &entry = (*setImpl)[cursor];
1032 unsigned flag = entry == 0 ? 1 : 0;
1033 entry = 1;
1034 return flag;
1035}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001036
1037CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1038 enum CXCursorKind kind = clang_getCursorKind(cursor);
1039 if (clang_isDeclaration(kind)) {
1040 Decl *decl = getCursorDecl(cursor);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +00001041 if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001042 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001043 CodeCompletionResult Result(namedDecl);
1044 CodeCompletionString *String
1045 = Result.CreateCodeCompletionString(unit->getASTContext(),
1046 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001047 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001048 unit->getCodeCompletionTUInfo(),
1049 true);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001050 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001051 }
1052 }
1053 else if (kind == CXCursor_MacroDefinition) {
1054 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1055 const IdentifierInfo *MacroInfo = definition->getName();
1056 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001057 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
1058 CodeCompletionString *String
1059 = Result.CreateCodeCompletionString(unit->getASTContext(),
1060 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001061 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001062 unit->getCodeCompletionTUInfo(),
1063 false);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001064 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001065 }
1066 return NULL;
1067}
Ted Kremenek8eece462012-04-30 19:33:45 +00001068} // end: extern C.
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001069
1070namespace {
1071 struct OverridenCursorsPool {
1072 typedef llvm::SmallVector<CXCursor, 2> CursorVec;
1073 std::vector<CursorVec*> AllCursors;
1074 std::vector<CursorVec*> AvailableCursors;
1075
1076 ~OverridenCursorsPool() {
1077 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1078 E = AllCursors.end(); I != E; ++I) {
1079 delete *I;
1080 }
1081 }
1082 };
1083}
1084
1085void *cxcursor::createOverridenCXCursorsPool() {
1086 return new OverridenCursorsPool();
1087}
1088
1089void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1090 delete static_cast<OverridenCursorsPool*>(pool);
1091}
Ted Kremenek8eece462012-04-30 19:33:45 +00001092
1093extern "C" {
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001094void clang_getOverriddenCursors(CXCursor cursor,
1095 CXCursor **overridden,
1096 unsigned *num_overridden) {
1097 if (overridden)
1098 *overridden = 0;
1099 if (num_overridden)
1100 *num_overridden = 0;
1101
1102 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1103
1104 if (!overridden || !num_overridden || !TU)
1105 return;
1106
1107 if (!clang_isDeclaration(cursor.kind))
1108 return;
1109
1110 OverridenCursorsPool &pool =
1111 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1112
1113 OverridenCursorsPool::CursorVec *Vec = 0;
1114
1115 if (!pool.AvailableCursors.empty()) {
1116 Vec = pool.AvailableCursors.back();
1117 pool.AvailableCursors.pop_back();
1118 }
1119 else {
1120 Vec = new OverridenCursorsPool::CursorVec();
1121 pool.AllCursors.push_back(Vec);
1122 }
1123
1124 // Clear out the vector, but don't free the memory contents. This
1125 // reduces malloc() traffic.
1126 Vec->clear();
1127
1128 // Use the first entry to contain a back reference to the vector.
1129 // This is a complete hack.
1130 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1131 backRefCursor.data[0] = Vec;
1132 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1133 Vec->push_back(backRefCursor);
1134
1135 // Get the overriden cursors.
1136 cxcursor::getOverriddenCursors(cursor, *Vec);
1137
1138 // Did we get any overriden cursors? If not, return Vec to the pool
1139 // of available cursor vectors.
1140 if (Vec->size() == 1) {
1141 pool.AvailableCursors.push_back(Vec);
1142 return;
1143 }
1144
1145 // Now tell the caller about the overriden cursors.
1146 assert(Vec->size() > 1);
1147 *overridden = &((*Vec)[1]);
1148 *num_overridden = Vec->size() - 1;
1149}
1150
1151void clang_disposeOverriddenCursors(CXCursor *overridden) {
1152 if (!overridden)
1153 return;
1154
1155 // Use pointer arithmetic to get back the first faux entry
1156 // which has a back-reference to the TU and the vector.
1157 --overridden;
1158 OverridenCursorsPool::CursorVec *Vec =
1159 static_cast<OverridenCursorsPool::CursorVec*>(overridden->data[0]);
1160 CXTranslationUnit TU = getCursorTU(*overridden);
1161
1162 assert(Vec && TU);
1163
1164 OverridenCursorsPool &pool =
1165 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1166
1167 pool.AvailableCursors.push_back(Vec);
1168}
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001169
1170int clang_Cursor_isDynamicCall(CXCursor C) {
1171 const Expr *E = 0;
1172 if (clang_isExpression(C.kind))
1173 E = getCursorExpr(C);
1174 if (!E)
1175 return 0;
1176
1177 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1178 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1179
1180 const MemberExpr *ME = 0;
1181 if (isa<MemberExpr>(E))
1182 ME = cast<MemberExpr>(E);
1183 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1184 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1185
1186 if (ME) {
1187 if (const CXXMethodDecl *
1188 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1189 return MD->isVirtual() && !ME->hasQualifier();
1190 }
1191
1192 return 0;
1193}
1194
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001195CXType clang_Cursor_getReceiverType(CXCursor C) {
1196 CXTranslationUnit TU = cxcursor::getCursorTU(C);
1197 const Expr *E = 0;
1198 if (clang_isExpression(C.kind))
1199 E = getCursorExpr(C);
1200
1201 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1202 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1203
1204 return cxtype::MakeCXType(QualType(), TU);
1205}
1206
Ted Kremenekeca099b2010-12-08 23:43:14 +00001207} // end: extern "C"