blob: 6615e4a6f4267f2caa85e7a9ca88988a5f4cb583 [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
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000680MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000681 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000682 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000683}
684
Douglas Gregorecdcb882010-10-20 22:00:55 +0000685CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000686 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000687 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000688 return C;
689}
690
691InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
692 assert(C.kind == CXCursor_InclusionDirective);
693 return static_cast<InclusionDirective *>(C.data[0]);
694}
695
Douglas Gregor36897b02010-09-10 00:22:18 +0000696CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000697 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000698
699 assert(Label && TU && "Invalid arguments!");
700 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000701 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000702 return C;
703}
704
705std::pair<LabelStmt*, SourceLocation>
706cxcursor::getCursorLabelRef(CXCursor C) {
707 assert(C.kind == CXCursor_LabelRef);
708 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
709 SourceLocation::getFromRawEncoding(
710 reinterpret_cast<uintptr_t>(C.data[1])));
711}
712
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000713CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000714 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000715 assert(E && TU && "Invalid arguments!");
716 OverloadedDeclRefStorage Storage(E);
717 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
718 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000719 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000720 { Storage.getOpaqueValue(), RawLoc, TU }
721 };
722 return C;
723}
724
725CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
726 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000727 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000728 assert(D && TU && "Invalid arguments!");
729 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
730 OverloadedDeclRefStorage Storage(D);
731 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000732 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000733 { Storage.getOpaqueValue(), RawLoc, TU }
734 };
735 return C;
736}
737
738CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
739 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000740 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000741 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
742 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
743 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
744 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000745 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000746 { Storage.getOpaqueValue(), RawLoc, TU }
747 };
748 return C;
749}
750
751std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
752cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
753 assert(C.kind == CXCursor_OverloadedDeclRef);
754 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
755 SourceLocation::getFromRawEncoding(
756 reinterpret_cast<uintptr_t>(C.data[1])));
757}
758
Douglas Gregor283cae32010-01-15 21:56:13 +0000759Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
760 return (Decl *)Cursor.data[0];
761}
762
763Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
764 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
765}
766
767Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000768 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000769 Cursor.kind == CXCursor_ObjCProtocolRef ||
770 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000771 return 0;
772
Douglas Gregor283cae32010-01-15 21:56:13 +0000773 return (Stmt *)Cursor.data[1];
774}
775
Ted Kremenek95f33552010-08-26 01:42:22 +0000776Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
777 return (Attr *)Cursor.data[1];
778}
779
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000780Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
781 return (Decl *)Cursor.data[0];
782}
783
Douglas Gregorf46034a2010-01-18 23:41:10 +0000784ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000785 return getCursorASTUnit(Cursor)->getASTContext();
786}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000787
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000788ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000789 CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]);
790 if (!TU)
791 return 0;
792 return static_cast<ASTUnit *>(TU->TUData);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000793}
794
795CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
796 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000797}
798
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000799void cxcursor::getOverriddenCursors(CXCursor cursor,
800 SmallVectorImpl<CXCursor> &overridden) {
801 assert(clang_isDeclaration(cursor.kind));
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000802 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000803 if (!D)
804 return;
805
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000806 CXTranslationUnit TU = getCursorTU(cursor);
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000807 SmallVector<const NamedDecl *, 8> OverDecls;
808 D->getASTContext().getOverriddenMethods(D, OverDecls);
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000809
Argyrios Kyrtzidis21c36072012-10-09 01:23:50 +0000810 for (SmallVector<const NamedDecl *, 8>::iterator
811 I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
812 overridden.push_back(MakeCXCursor(const_cast<NamedDecl*>(*I), TU));
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +0000813 }
814}
815
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000816std::pair<int, SourceLocation>
817cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
818 if (cursor.kind == CXCursor_ObjCMessageExpr) {
819 if (cursor.xdata != -1)
820 return std::make_pair(cursor.xdata,
821 cast<ObjCMessageExpr>(getCursorExpr(cursor))
822 ->getSelectorLoc(cursor.xdata));
823 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
824 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
825 if (cursor.xdata != -1)
826 return std::make_pair(cursor.xdata,
827 cast<ObjCMethodDecl>(getCursorDecl(cursor))
828 ->getSelectorLoc(cursor.xdata));
829 }
830
831 return std::make_pair(-1, SourceLocation());
832}
833
834CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
835 CXCursor newCursor = cursor;
836
837 if (cursor.kind == CXCursor_ObjCMessageExpr) {
838 if (SelIdx == -1 ||
839 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
840 ->getNumSelectorLocs())
841 newCursor.xdata = -1;
842 else
843 newCursor.xdata = SelIdx;
844 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
845 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
846 if (SelIdx == -1 ||
847 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
848 ->getNumSelectorLocs())
849 newCursor.xdata = -1;
850 else
851 newCursor.xdata = SelIdx;
852 }
853
854 return newCursor;
855}
856
857CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
858 if (cursor.kind != CXCursor_CallExpr)
859 return cursor;
860
861 if (cursor.xdata == 0)
862 return cursor;
863
864 Expr *E = getCursorExpr(cursor);
865 TypeSourceInfo *Type = 0;
866 if (CXXUnresolvedConstructExpr *
867 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
868 Type = UnCtor->getTypeSourceInfo();
869 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
870 Type = Tmp->getTypeSourceInfo();
871 }
872
873 if (!Type)
874 return cursor;
875
876 CXTranslationUnit TU = getCursorTU(cursor);
877 QualType Ty = Type->getType();
878 TypeLoc TL = Type->getTypeLoc();
879 SourceLocation Loc = TL.getBeginLoc();
880
881 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
882 Ty = ElabT->getNamedType();
883 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
884 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
885 }
886
887 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
888 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
889 if (const TagType *Tag = Ty->getAs<TagType>())
890 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
891 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
892 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
893
894 return cursor;
895}
896
Douglas Gregor283cae32010-01-15 21:56:13 +0000897bool cxcursor::operator==(CXCursor X, CXCursor Y) {
898 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
899 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000900}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000901
902// FIXME: Remove once we can model DeclGroups and their appropriate ranges
903// properly in the ASTs.
904bool cxcursor::isFirstInDeclGroup(CXCursor C) {
905 assert(clang_isDeclaration(C.kind));
906 return ((uintptr_t) (C.data[1])) != 0;
907}
908
Ted Kremenekeca099b2010-12-08 23:43:14 +0000909//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000910// libclang CXCursor APIs
911//===----------------------------------------------------------------------===//
912
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000913extern "C" {
914
915int clang_Cursor_isNull(CXCursor cursor) {
916 return clang_equalCursors(cursor, clang_getNullCursor());
917}
918
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000919CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
920 return getCursorTU(cursor);
921}
922
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000923int clang_Cursor_getNumArguments(CXCursor C) {
924 if (clang_isDeclaration(C.kind)) {
925 Decl *D = cxcursor::getCursorDecl(C);
926 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
927 return MD->param_size();
928 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
929 return FD->param_size();
930 }
931
932 return -1;
933}
934
935CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
936 if (clang_isDeclaration(C.kind)) {
937 Decl *D = cxcursor::getCursorDecl(C);
938 if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
939 if (i < MD->param_size())
940 return cxcursor::MakeCXCursor(MD->param_begin()[i],
941 cxcursor::getCursorTU(C));
942 } else if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
943 if (i < FD->param_size())
944 return cxcursor::MakeCXCursor(FD->param_begin()[i],
945 cxcursor::getCursorTU(C));
946 }
947 }
948
949 return clang_getNullCursor();
950}
951
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000952} // end: extern "C"
953
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000954//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000955// CXCursorSet.
956//===----------------------------------------------------------------------===//
957
958typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
959
960static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
961 return (CXCursorSet) setImpl;
962}
963static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
964 return (CXCursorSet_Impl*) set;
965}
966namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000967template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000968public:
969 static inline CXCursor getEmptyKey() {
970 return MakeCXCursorInvalid(CXCursor_InvalidFile);
971 }
972 static inline CXCursor getTombstoneKey() {
973 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
974 }
975 static inline unsigned getHashValue(const CXCursor &cursor) {
976 return llvm::DenseMapInfo<std::pair<void*,void*> >
977 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
978 }
979 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
980 return x.kind == y.kind &&
981 x.data[0] == y.data[0] &&
982 x.data[1] == y.data[1];
983 }
984};
985}
986
987extern "C" {
988CXCursorSet clang_createCXCursorSet() {
989 return packCXCursorSet(new CXCursorSet_Impl());
990}
991
992void clang_disposeCXCursorSet(CXCursorSet set) {
993 delete unpackCXCursorSet(set);
994}
995
996unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
997 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
998 if (!setImpl)
999 return 0;
1000 return setImpl->find(cursor) == setImpl->end();
1001}
1002
Anders Carlssone8b3de02010-12-09 01:00:12 +00001003unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001004 // Do not insert invalid cursors into the set.
1005 if (cursor.kind >= CXCursor_FirstInvalid &&
1006 cursor.kind <= CXCursor_LastInvalid)
1007 return 1;
1008
1009 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1010 if (!setImpl)
1011 return 1;
1012 unsigned &entry = (*setImpl)[cursor];
1013 unsigned flag = entry == 0 ? 1 : 0;
1014 entry = 1;
1015 return flag;
1016}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001017
1018CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1019 enum CXCursorKind kind = clang_getCursorKind(cursor);
1020 if (clang_isDeclaration(kind)) {
1021 Decl *decl = getCursorDecl(cursor);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +00001022 if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001023 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001024 CodeCompletionResult Result(namedDecl);
1025 CodeCompletionString *String
1026 = Result.CreateCodeCompletionString(unit->getASTContext(),
1027 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001028 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001029 unit->getCodeCompletionTUInfo(),
1030 true);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001031 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001032 }
1033 }
1034 else if (kind == CXCursor_MacroDefinition) {
1035 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1036 const IdentifierInfo *MacroInfo = definition->getName();
1037 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001038 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
1039 CodeCompletionString *String
1040 = Result.CreateCodeCompletionString(unit->getASTContext(),
1041 unit->getPreprocessor(),
Argyrios Kyrtzidis28a83f52012-04-10 17:23:48 +00001042 unit->getCodeCompletionTUInfo().getAllocator(),
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001043 unit->getCodeCompletionTUInfo(),
1044 false);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001045 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001046 }
1047 return NULL;
1048}
Ted Kremenek8eece462012-04-30 19:33:45 +00001049} // end: extern C.
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001050
1051namespace {
1052 struct OverridenCursorsPool {
1053 typedef llvm::SmallVector<CXCursor, 2> CursorVec;
1054 std::vector<CursorVec*> AllCursors;
1055 std::vector<CursorVec*> AvailableCursors;
1056
1057 ~OverridenCursorsPool() {
1058 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1059 E = AllCursors.end(); I != E; ++I) {
1060 delete *I;
1061 }
1062 }
1063 };
1064}
1065
1066void *cxcursor::createOverridenCXCursorsPool() {
1067 return new OverridenCursorsPool();
1068}
1069
1070void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1071 delete static_cast<OverridenCursorsPool*>(pool);
1072}
Ted Kremenek8eece462012-04-30 19:33:45 +00001073
1074extern "C" {
Ted Kremenekbbf66ca2012-04-30 19:06:49 +00001075void clang_getOverriddenCursors(CXCursor cursor,
1076 CXCursor **overridden,
1077 unsigned *num_overridden) {
1078 if (overridden)
1079 *overridden = 0;
1080 if (num_overridden)
1081 *num_overridden = 0;
1082
1083 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1084
1085 if (!overridden || !num_overridden || !TU)
1086 return;
1087
1088 if (!clang_isDeclaration(cursor.kind))
1089 return;
1090
1091 OverridenCursorsPool &pool =
1092 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1093
1094 OverridenCursorsPool::CursorVec *Vec = 0;
1095
1096 if (!pool.AvailableCursors.empty()) {
1097 Vec = pool.AvailableCursors.back();
1098 pool.AvailableCursors.pop_back();
1099 }
1100 else {
1101 Vec = new OverridenCursorsPool::CursorVec();
1102 pool.AllCursors.push_back(Vec);
1103 }
1104
1105 // Clear out the vector, but don't free the memory contents. This
1106 // reduces malloc() traffic.
1107 Vec->clear();
1108
1109 // Use the first entry to contain a back reference to the vector.
1110 // This is a complete hack.
1111 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1112 backRefCursor.data[0] = Vec;
1113 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1114 Vec->push_back(backRefCursor);
1115
1116 // Get the overriden cursors.
1117 cxcursor::getOverriddenCursors(cursor, *Vec);
1118
1119 // Did we get any overriden cursors? If not, return Vec to the pool
1120 // of available cursor vectors.
1121 if (Vec->size() == 1) {
1122 pool.AvailableCursors.push_back(Vec);
1123 return;
1124 }
1125
1126 // Now tell the caller about the overriden cursors.
1127 assert(Vec->size() > 1);
1128 *overridden = &((*Vec)[1]);
1129 *num_overridden = Vec->size() - 1;
1130}
1131
1132void clang_disposeOverriddenCursors(CXCursor *overridden) {
1133 if (!overridden)
1134 return;
1135
1136 // Use pointer arithmetic to get back the first faux entry
1137 // which has a back-reference to the TU and the vector.
1138 --overridden;
1139 OverridenCursorsPool::CursorVec *Vec =
1140 static_cast<OverridenCursorsPool::CursorVec*>(overridden->data[0]);
1141 CXTranslationUnit TU = getCursorTU(*overridden);
1142
1143 assert(Vec && TU);
1144
1145 OverridenCursorsPool &pool =
1146 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1147
1148 pool.AvailableCursors.push_back(Vec);
1149}
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001150
1151int clang_Cursor_isDynamicCall(CXCursor C) {
1152 const Expr *E = 0;
1153 if (clang_isExpression(C.kind))
1154 E = getCursorExpr(C);
1155 if (!E)
1156 return 0;
1157
1158 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1159 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1160
1161 const MemberExpr *ME = 0;
1162 if (isa<MemberExpr>(E))
1163 ME = cast<MemberExpr>(E);
1164 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1165 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1166
1167 if (ME) {
1168 if (const CXXMethodDecl *
1169 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1170 return MD->isVirtual() && !ME->hasQualifier();
1171 }
1172
1173 return 0;
1174}
1175
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001176CXType clang_Cursor_getReceiverType(CXCursor C) {
1177 CXTranslationUnit TU = cxcursor::getCursorTU(C);
1178 const Expr *E = 0;
1179 if (clang_isExpression(C.kind))
1180 E = getCursorExpr(C);
1181
1182 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1183 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1184
1185 return cxtype::MakeCXType(QualType(), TU);
1186}
1187
Ted Kremenekeca099b2010-12-08 23:43:14 +00001188} // end: extern "C"