blob: 77b6ba276071ccf893b44f83dda51d49db054ec4 [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"
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +000019#include "clang/Frontend/ASTUnit.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000020#include "clang/AST/Decl.h"
Douglas Gregor69319002010-08-31 23:48:11 +000021#include "clang/AST/DeclCXX.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000022#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000024#include "clang/AST/Expr.h"
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000025#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000026#include "clang/AST/ExprObjC.h"
Ted Kremenek007a7c92010-11-01 23:26:51 +000027#include "clang-c/Index.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000029
30using namespace clang;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000031using namespace cxcursor;
Ted Kremenek16c440a2010-01-15 20:35:54 +000032
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000033CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
34 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000035 CXCursor C = { K, 0, { 0, 0, 0 } };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000036 return C;
Ted Kremenek16c440a2010-01-15 20:35:54 +000037}
38
Ted Kremeneke77f4432010-02-18 03:09:07 +000039static CXCursorKind GetCursorKind(const Attr *A) {
40 assert(A && "Invalid arguments!");
41 switch (A->getKind()) {
42 default: break;
Sean Hunt387475d2010-06-16 23:43:53 +000043 case attr::IBAction: return CXCursor_IBActionAttr;
44 case attr::IBOutlet: return CXCursor_IBOutletAttr;
45 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Argyrios Kyrtzidis6639e922011-09-13 17:39:31 +000046 case attr::Final: return CXCursor_CXXFinalAttr;
47 case attr::Override: return CXCursor_CXXOverrideAttr;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000048 case attr::Annotate: return CXCursor_AnnotateAttr;
Argyrios Kyrtzidis84b79642011-12-06 22:05:01 +000049 case attr::AsmLabel: return CXCursor_AsmLabelAttr;
Ted Kremeneke77f4432010-02-18 03:09:07 +000050 }
51
52 return CXCursor_UnexposedAttr;
53}
54
Ted Kremeneka60ed472010-11-16 08:15:36 +000055CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
56 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000057 assert(A && Parent && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000058 CXCursor C = { GetCursorKind(A), 0, { Parent, (void*)A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000059 return C;
60}
61
Ted Kremeneka60ed472010-11-16 08:15:36 +000062CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000063 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000064 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000065 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000066
67 CXCursorKind K = getCursorKindForDecl(D);
68
69 if (K == CXCursor_ObjCClassMethodDecl ||
70 K == CXCursor_ObjCInstanceMethodDecl) {
71 int SelectorIdIndex = -1;
72 // Check if cursor points to a selector id.
73 if (RegionOfInterest.isValid() &&
74 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
75 SmallVector<SourceLocation, 16> SelLocs;
76 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
77 SmallVector<SourceLocation, 16>::iterator
78 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
79 if (I != SelLocs.end())
80 SelectorIdIndex = I - SelLocs.begin();
81 }
82 CXCursor C = { K, SelectorIdIndex,
83 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
84 return C;
85 }
86
87 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000088 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000089}
90
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000091CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU,
92 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000093 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000094 CXCursorKind K = CXCursor_NotImplemented;
95
96 switch (S->getStmtClass()) {
97 case Stmt::NoStmtClass:
98 break;
Douglas Gregor42b29842011-10-05 19:00:14 +000099
Douglas Gregor97b98722010-01-19 23:20:36 +0000100 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000101 K = CXCursor_CaseStmt;
102 break;
103
Douglas Gregor97b98722010-01-19 23:20:36 +0000104 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000105 K = CXCursor_DefaultStmt;
106 break;
107
108 case Stmt::IfStmtClass:
109 K = CXCursor_IfStmt;
110 break;
111
112 case Stmt::SwitchStmtClass:
113 K = CXCursor_SwitchStmt;
114 break;
115
116 case Stmt::WhileStmtClass:
117 K = CXCursor_WhileStmt;
118 break;
119
120 case Stmt::DoStmtClass:
121 K = CXCursor_DoStmt;
122 break;
123
124 case Stmt::ForStmtClass:
125 K = CXCursor_ForStmt;
126 break;
127
128 case Stmt::GotoStmtClass:
129 K = CXCursor_GotoStmt;
130 break;
131
Douglas Gregor97b98722010-01-19 23:20:36 +0000132 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000133 K = CXCursor_IndirectGotoStmt;
134 break;
135
136 case Stmt::ContinueStmtClass:
137 K = CXCursor_ContinueStmt;
138 break;
139
140 case Stmt::BreakStmtClass:
141 K = CXCursor_BreakStmt;
142 break;
143
144 case Stmt::ReturnStmtClass:
145 K = CXCursor_ReturnStmt;
146 break;
147
148 case Stmt::AsmStmtClass:
149 K = CXCursor_AsmStmt;
150 break;
151
152 case Stmt::ObjCAtTryStmtClass:
153 K = CXCursor_ObjCAtTryStmt;
154 break;
155
156 case Stmt::ObjCAtCatchStmtClass:
157 K = CXCursor_ObjCAtCatchStmt;
158 break;
159
160 case Stmt::ObjCAtFinallyStmtClass:
161 K = CXCursor_ObjCAtFinallyStmt;
162 break;
163
164 case Stmt::ObjCAtThrowStmtClass:
165 K = CXCursor_ObjCAtThrowStmt;
166 break;
167
168 case Stmt::ObjCAtSynchronizedStmtClass:
169 K = CXCursor_ObjCAtSynchronizedStmt;
170 break;
171
172 case Stmt::ObjCAutoreleasePoolStmtClass:
173 K = CXCursor_ObjCAutoreleasePoolStmt;
174 break;
175
Douglas Gregor97b98722010-01-19 23:20:36 +0000176 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000177 K = CXCursor_ObjCForCollectionStmt;
178 break;
179
Douglas Gregor97b98722010-01-19 23:20:36 +0000180 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000181 K = CXCursor_CXXCatchStmt;
182 break;
183
184 case Stmt::CXXTryStmtClass:
185 K = CXCursor_CXXTryStmt;
186 break;
187
188 case Stmt::CXXForRangeStmtClass:
189 K = CXCursor_CXXForRangeStmt;
190 break;
191
John Wiegley28bbe4b2011-04-28 01:08:34 +0000192 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000193 K = CXCursor_SEHTryStmt;
194 break;
195
John Wiegley28bbe4b2011-04-28 01:08:34 +0000196 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000197 K = CXCursor_SEHExceptStmt;
198 break;
199
John Wiegley28bbe4b2011-04-28 01:08:34 +0000200 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000201 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000202 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000203
John Wiegley21ff2e52011-04-28 00:16:57 +0000204 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000205 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000206 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000207 case Stmt::BinaryConditionalOperatorClass:
208 case Stmt::BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +0000209 case Stmt::TypeTraitExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000210 case Stmt::CXXBindTemporaryExprClass:
211 case Stmt::CXXDefaultArgExprClass:
212 case Stmt::CXXScalarValueInitExprClass:
213 case Stmt::CXXUuidofExprClass:
214 case Stmt::ChooseExprClass:
215 case Stmt::DesignatedInitExprClass:
216 case Stmt::ExprWithCleanupsClass:
217 case Stmt::ExpressionTraitExprClass:
218 case Stmt::ExtVectorElementExprClass:
219 case Stmt::ImplicitCastExprClass:
220 case Stmt::ImplicitValueInitExprClass:
221 case Stmt::MaterializeTemporaryExprClass:
222 case Stmt::ObjCIndirectCopyRestoreExprClass:
223 case Stmt::OffsetOfExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000224 case Stmt::ParenListExprClass:
225 case Stmt::PredefinedExprClass:
226 case Stmt::ShuffleVectorExprClass:
227 case Stmt::UnaryExprOrTypeTraitExprClass:
228 case Stmt::UnaryTypeTraitExprClass:
229 case Stmt::VAArgExprClass:
Ted Kremenekb3f75422012-03-06 20:06:06 +0000230 case Stmt::ObjCArrayLiteralClass:
231 case Stmt::ObjCDictionaryLiteralClass:
232 case Stmt::ObjCNumericLiteralClass:
233 case Stmt::ObjCSubscriptRefExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000234 K = CXCursor_UnexposedExpr;
235 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000236
John McCall4b9c2d22011-11-06 09:01:30 +0000237 case Stmt::OpaqueValueExprClass:
238 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
239 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
240 K = CXCursor_UnexposedExpr;
241 break;
242
243 case Stmt::PseudoObjectExprClass:
244 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
245 Parent, TU, RegionOfInterest);
246
Douglas Gregor42b29842011-10-05 19:00:14 +0000247 case Stmt::CompoundStmtClass:
248 K = CXCursor_CompoundStmt;
249 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000250
Douglas Gregor42b29842011-10-05 19:00:14 +0000251 case Stmt::NullStmtClass:
252 K = CXCursor_NullStmt;
253 break;
254
255 case Stmt::LabelStmtClass:
256 K = CXCursor_LabelStmt;
257 break;
258
259 case Stmt::DeclStmtClass:
260 K = CXCursor_DeclStmt;
261 break;
262
263 case Stmt::IntegerLiteralClass:
264 K = CXCursor_IntegerLiteral;
265 break;
266
267 case Stmt::FloatingLiteralClass:
268 K = CXCursor_FloatingLiteral;
269 break;
270
271 case Stmt::ImaginaryLiteralClass:
272 K = CXCursor_ImaginaryLiteral;
273 break;
274
275 case Stmt::StringLiteralClass:
276 K = CXCursor_StringLiteral;
277 break;
278
279 case Stmt::CharacterLiteralClass:
280 K = CXCursor_CharacterLiteral;
281 break;
282
283 case Stmt::ParenExprClass:
284 K = CXCursor_ParenExpr;
285 break;
286
287 case Stmt::UnaryOperatorClass:
288 K = CXCursor_UnaryOperator;
289 break;
290
291 case Stmt::CXXNoexceptExprClass:
292 K = CXCursor_UnaryExpr;
293 break;
294
295 case Stmt::ArraySubscriptExprClass:
296 K = CXCursor_ArraySubscriptExpr;
297 break;
298
299 case Stmt::BinaryOperatorClass:
300 K = CXCursor_BinaryOperator;
301 break;
302
303 case Stmt::CompoundAssignOperatorClass:
304 K = CXCursor_CompoundAssignOperator;
305 break;
306
307 case Stmt::ConditionalOperatorClass:
308 K = CXCursor_ConditionalOperator;
309 break;
310
311 case Stmt::CStyleCastExprClass:
312 K = CXCursor_CStyleCastExpr;
313 break;
314
315 case Stmt::CompoundLiteralExprClass:
316 K = CXCursor_CompoundLiteralExpr;
317 break;
318
319 case Stmt::InitListExprClass:
320 K = CXCursor_InitListExpr;
321 break;
322
323 case Stmt::AddrLabelExprClass:
324 K = CXCursor_AddrLabelExpr;
325 break;
326
327 case Stmt::StmtExprClass:
328 K = CXCursor_StmtExpr;
329 break;
330
331 case Stmt::GenericSelectionExprClass:
332 K = CXCursor_GenericSelectionExpr;
333 break;
334
335 case Stmt::GNUNullExprClass:
336 K = CXCursor_GNUNullExpr;
337 break;
338
339 case Stmt::CXXStaticCastExprClass:
340 K = CXCursor_CXXStaticCastExpr;
341 break;
342
343 case Stmt::CXXDynamicCastExprClass:
344 K = CXCursor_CXXDynamicCastExpr;
345 break;
346
347 case Stmt::CXXReinterpretCastExprClass:
348 K = CXCursor_CXXReinterpretCastExpr;
349 break;
350
351 case Stmt::CXXConstCastExprClass:
352 K = CXCursor_CXXConstCastExpr;
353 break;
354
355 case Stmt::CXXFunctionalCastExprClass:
356 K = CXCursor_CXXFunctionalCastExpr;
357 break;
358
359 case Stmt::CXXTypeidExprClass:
360 K = CXCursor_CXXTypeidExpr;
361 break;
362
363 case Stmt::CXXBoolLiteralExprClass:
364 K = CXCursor_CXXBoolLiteralExpr;
365 break;
366
367 case Stmt::CXXNullPtrLiteralExprClass:
368 K = CXCursor_CXXNullPtrLiteralExpr;
369 break;
370
371 case Stmt::CXXThisExprClass:
372 K = CXCursor_CXXThisExpr;
373 break;
374
375 case Stmt::CXXThrowExprClass:
376 K = CXCursor_CXXThrowExpr;
377 break;
378
379 case Stmt::CXXNewExprClass:
380 K = CXCursor_CXXNewExpr;
381 break;
382
383 case Stmt::CXXDeleteExprClass:
384 K = CXCursor_CXXDeleteExpr;
385 break;
386
387 case Stmt::ObjCStringLiteralClass:
388 K = CXCursor_ObjCStringLiteral;
389 break;
390
391 case Stmt::ObjCEncodeExprClass:
392 K = CXCursor_ObjCEncodeExpr;
393 break;
394
395 case Stmt::ObjCSelectorExprClass:
396 K = CXCursor_ObjCSelectorExpr;
397 break;
398
399 case Stmt::ObjCProtocolExprClass:
400 K = CXCursor_ObjCProtocolExpr;
401 break;
Ted Kremenekb3f75422012-03-06 20:06:06 +0000402
403 case Stmt::ObjCBoolLiteralExprClass:
404 K = CXCursor_ObjCBoolLiteralExpr;
405 break;
406
Douglas Gregor42b29842011-10-05 19:00:14 +0000407 case Stmt::ObjCBridgedCastExprClass:
408 K = CXCursor_ObjCBridgedCastExpr;
409 break;
410
411 case Stmt::BlockExprClass:
412 K = CXCursor_BlockExpr;
413 break;
414
415 case Stmt::PackExpansionExprClass:
416 K = CXCursor_PackExpansionExpr;
417 break;
418
419 case Stmt::SizeOfPackExprClass:
420 K = CXCursor_SizeOfPackExpr;
421 break;
422
Douglas Gregor97b98722010-01-19 23:20:36 +0000423 case Stmt::BlockDeclRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000424 case Stmt::DeclRefExprClass:
425 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000426 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000427 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000428 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000429 K = CXCursor_DeclRefExpr;
430 break;
431
Douglas Gregor42b29842011-10-05 19:00:14 +0000432 case Stmt::CXXDependentScopeMemberExprClass:
433 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000434 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000435 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000436 case Stmt::ObjCIvarRefExprClass:
437 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000438 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000439 K = CXCursor_MemberRefExpr;
440 break;
441
442 case Stmt::CallExprClass:
443 case Stmt::CXXOperatorCallExprClass:
444 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000445 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000446 case Stmt::CXXConstructExprClass:
447 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000448 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000449 K = CXCursor_CallExpr;
450 break;
451
Douglas Gregor011d8b92012-02-15 00:54:55 +0000452 case Stmt::LambdaExprClass:
453 K = CXCursor_LambdaExpr;
454 break;
455
Douglas Gregorba0513d2011-10-25 01:33:02 +0000456 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000457 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000458 int SelectorIdIndex = -1;
459 // Check if cursor points to a selector id.
460 if (RegionOfInterest.isValid() &&
461 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
462 SmallVector<SourceLocation, 16> SelLocs;
463 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
464 SmallVector<SourceLocation, 16>::iterator
465 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
466 if (I != SelLocs.end())
467 SelectorIdIndex = I - SelLocs.begin();
468 }
469 CXCursor C = { K, 0, { Parent, S, TU } };
470 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000471 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000472
473 case Stmt::MSDependentExistsStmtClass:
474 K = CXCursor_UnexposedStmt;
475 break;
476 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000477
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000478 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000479 return C;
480}
481
Douglas Gregor2e331b92010-01-16 14:00:32 +0000482CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000483 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000484 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000485 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000486 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000487 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000488 return C;
489}
490
491std::pair<ObjCInterfaceDecl *, SourceLocation>
492cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
493 assert(C.kind == CXCursor_ObjCSuperClassRef);
494 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
495 SourceLocation::getFromRawEncoding(
496 reinterpret_cast<uintptr_t>(C.data[1])));
497}
498
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000499CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000500 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000501 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000502 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000503 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000504 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000505 return C;
506}
507
508std::pair<ObjCProtocolDecl *, SourceLocation>
509cxcursor::getCursorObjCProtocolRef(CXCursor C) {
510 assert(C.kind == CXCursor_ObjCProtocolRef);
511 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
512 SourceLocation::getFromRawEncoding(
513 reinterpret_cast<uintptr_t>(C.data[1])));
514}
515
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000516CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000517 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000518 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000519 // 'Class' can be null for invalid code.
520 if (!Class)
521 return MakeCXCursorInvalid(CXCursor_InvalidCode);
522 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000523 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000524 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000525 return C;
526}
527
528std::pair<ObjCInterfaceDecl *, SourceLocation>
529cxcursor::getCursorObjCClassRef(CXCursor C) {
530 assert(C.kind == CXCursor_ObjCClassRef);
531 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
532 SourceLocation::getFromRawEncoding(
533 reinterpret_cast<uintptr_t>(C.data[1])));
534}
535
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000536CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000537 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000538 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000539 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000540 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000541 return C;
542}
543
544std::pair<TypeDecl *, SourceLocation>
545cxcursor::getCursorTypeRef(CXCursor C) {
546 assert(C.kind == CXCursor_TypeRef);
547 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
548 SourceLocation::getFromRawEncoding(
549 reinterpret_cast<uintptr_t>(C.data[1])));
550}
551
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000552CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000553 SourceLocation Loc,
554 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000555 assert(Template && TU && "Invalid arguments!");
556 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000557 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000558 return C;
559}
560
561std::pair<TemplateDecl *, SourceLocation>
562cxcursor::getCursorTemplateRef(CXCursor C) {
563 assert(C.kind == CXCursor_TemplateRef);
564 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
565 SourceLocation::getFromRawEncoding(
566 reinterpret_cast<uintptr_t>(C.data[1])));
567}
568
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000569CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
570 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000571 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000572
573 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
574 "Invalid arguments!");
575 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000576 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000577 return C;
578}
579
580std::pair<NamedDecl *, SourceLocation>
581cxcursor::getCursorNamespaceRef(CXCursor C) {
582 assert(C.kind == CXCursor_NamespaceRef);
583 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
584 SourceLocation::getFromRawEncoding(
585 reinterpret_cast<uintptr_t>(C.data[1])));
586}
587
Douglas Gregor011d8b92012-02-15 00:54:55 +0000588CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
589 CXTranslationUnit TU) {
590
591 assert(Var && TU && "Invalid arguments!");
592 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
593 CXCursor C = { CXCursor_VariableRef, 0, { (void*)Var, RawLoc, TU } };
594 return C;
595}
596
597std::pair<VarDecl *, SourceLocation>
598cxcursor::getCursorVariableRef(CXCursor C) {
599 assert(C.kind == CXCursor_VariableRef);
600 return std::make_pair(static_cast<VarDecl *>(C.data[0]),
601 SourceLocation::getFromRawEncoding(
602 reinterpret_cast<uintptr_t>(C.data[1])));
603}
604
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000605CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000606 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000607
608 assert(Field && TU && "Invalid arguments!");
609 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000610 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000611 return C;
612}
613
614std::pair<FieldDecl *, SourceLocation>
615cxcursor::getCursorMemberRef(CXCursor C) {
616 assert(C.kind == CXCursor_MemberRef);
617 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
618 SourceLocation::getFromRawEncoding(
619 reinterpret_cast<uintptr_t>(C.data[1])));
620}
621
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000622CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000623 CXTranslationUnit TU){
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000624 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000625 return C;
626}
627
628CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
629 assert(C.kind == CXCursor_CXXBaseSpecifier);
630 return static_cast<CXXBaseSpecifier*>(C.data[0]);
631}
632
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000633CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000634 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000635 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000636 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
637 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
638 TU }
639 };
640 return C;
641}
642
643SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
644 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000645 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000646 reinterpret_cast<uintptr_t> (C.data[0])),
647 SourceLocation::getFromRawEncoding(
648 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000649 ASTUnit *TU = getCursorASTUnit(C);
650 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000651}
652
Ted Kremeneka60ed472010-11-16 08:15:36 +0000653CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
654 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000655 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000656 return C;
657}
658
659MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
660 assert(C.kind == CXCursor_MacroDefinition);
661 return static_cast<MacroDefinition *>(C.data[0]);
662}
663
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000664CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
665 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000666 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000667 return C;
668}
669
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000670MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000671 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000672 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000673}
674
Douglas Gregorecdcb882010-10-20 22:00:55 +0000675CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000676 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000677 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000678 return C;
679}
680
681InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
682 assert(C.kind == CXCursor_InclusionDirective);
683 return static_cast<InclusionDirective *>(C.data[0]);
684}
685
Douglas Gregor36897b02010-09-10 00:22:18 +0000686CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000687 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000688
689 assert(Label && TU && "Invalid arguments!");
690 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000691 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000692 return C;
693}
694
695std::pair<LabelStmt*, SourceLocation>
696cxcursor::getCursorLabelRef(CXCursor C) {
697 assert(C.kind == CXCursor_LabelRef);
698 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
699 SourceLocation::getFromRawEncoding(
700 reinterpret_cast<uintptr_t>(C.data[1])));
701}
702
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000703CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000704 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000705 assert(E && TU && "Invalid arguments!");
706 OverloadedDeclRefStorage Storage(E);
707 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
708 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000709 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000710 { Storage.getOpaqueValue(), RawLoc, TU }
711 };
712 return C;
713}
714
715CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
716 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000717 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000718 assert(D && TU && "Invalid arguments!");
719 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
720 OverloadedDeclRefStorage Storage(D);
721 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000722 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000723 { Storage.getOpaqueValue(), RawLoc, TU }
724 };
725 return C;
726}
727
728CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
729 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000730 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000731 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
732 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
733 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
734 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000735 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000736 { Storage.getOpaqueValue(), RawLoc, TU }
737 };
738 return C;
739}
740
741std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
742cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
743 assert(C.kind == CXCursor_OverloadedDeclRef);
744 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
745 SourceLocation::getFromRawEncoding(
746 reinterpret_cast<uintptr_t>(C.data[1])));
747}
748
Douglas Gregor283cae32010-01-15 21:56:13 +0000749Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
750 return (Decl *)Cursor.data[0];
751}
752
753Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
754 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
755}
756
757Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000758 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000759 Cursor.kind == CXCursor_ObjCProtocolRef ||
760 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000761 return 0;
762
Douglas Gregor283cae32010-01-15 21:56:13 +0000763 return (Stmt *)Cursor.data[1];
764}
765
Ted Kremenek95f33552010-08-26 01:42:22 +0000766Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
767 return (Attr *)Cursor.data[1];
768}
769
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000770Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
771 return (Decl *)Cursor.data[0];
772}
773
Douglas Gregorf46034a2010-01-18 23:41:10 +0000774ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000775 return getCursorASTUnit(Cursor)->getASTContext();
776}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000777
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000778ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000779 CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]);
780 if (!TU)
781 return 0;
782 return static_cast<ASTUnit *>(TU->TUData);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000783}
784
785CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
786 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000787}
788
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000789static void CollectOverriddenMethods(CXTranslationUnit TU,
790 DeclContext *Ctx,
791 ObjCMethodDecl *Method,
792 SmallVectorImpl<CXCursor> &Methods) {
793 if (!Ctx)
794 return;
795
796 // If we have a class or category implementation, jump straight to the
797 // interface.
798 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
799 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
800 Method, Methods);
801
802 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
803 if (!Container)
804 return;
805
806 // Check whether we have a matching method at this level.
807 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
808 Method->isInstanceMethod()))
809 if (Method != Overridden) {
810 // We found an override at this level; there is no need to look
811 // into other protocols or categories.
812 Methods.push_back(MakeCXCursor(Overridden, TU));
813 return;
814 }
815
816 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
817 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
818 PEnd = Protocol->protocol_end();
819 P != PEnd; ++P)
820 CollectOverriddenMethods(TU, *P, Method, Methods);
821 }
822
823 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
824 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
825 PEnd = Category->protocol_end();
826 P != PEnd; ++P)
827 CollectOverriddenMethods(TU, *P, Method, Methods);
828 }
829
830 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
831 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
832 PEnd = Interface->protocol_end();
833 P != PEnd; ++P)
834 CollectOverriddenMethods(TU, *P, Method, Methods);
835
836 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
837 Category; Category = Category->getNextClassCategory())
838 CollectOverriddenMethods(TU, Category, Method, Methods);
839
840 // We only look into the superclass if we haven't found anything yet.
841 if (Methods.empty())
842 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
843 return CollectOverriddenMethods(TU, Super, Method, Methods);
844 }
845}
846
847void cxcursor::getOverriddenCursors(CXCursor cursor,
848 SmallVectorImpl<CXCursor> &overridden) {
849 if (!clang_isDeclaration(cursor.kind))
850 return;
851
852 Decl *D = getCursorDecl(cursor);
853 if (!D)
854 return;
855
856 // Handle C++ member functions.
857 CXTranslationUnit TU = getCursorTU(cursor);
858 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
859 for (CXXMethodDecl::method_iterator
860 M = CXXMethod->begin_overridden_methods(),
861 MEnd = CXXMethod->end_overridden_methods();
862 M != MEnd; ++M)
863 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
864 return;
865 }
866
867 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
868 if (!Method)
869 return;
870
871 // Handle Objective-C methods.
872 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
873}
874
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000875std::pair<int, SourceLocation>
876cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
877 if (cursor.kind == CXCursor_ObjCMessageExpr) {
878 if (cursor.xdata != -1)
879 return std::make_pair(cursor.xdata,
880 cast<ObjCMessageExpr>(getCursorExpr(cursor))
881 ->getSelectorLoc(cursor.xdata));
882 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
883 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
884 if (cursor.xdata != -1)
885 return std::make_pair(cursor.xdata,
886 cast<ObjCMethodDecl>(getCursorDecl(cursor))
887 ->getSelectorLoc(cursor.xdata));
888 }
889
890 return std::make_pair(-1, SourceLocation());
891}
892
893CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
894 CXCursor newCursor = cursor;
895
896 if (cursor.kind == CXCursor_ObjCMessageExpr) {
897 if (SelIdx == -1 ||
898 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
899 ->getNumSelectorLocs())
900 newCursor.xdata = -1;
901 else
902 newCursor.xdata = SelIdx;
903 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
904 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
905 if (SelIdx == -1 ||
906 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
907 ->getNumSelectorLocs())
908 newCursor.xdata = -1;
909 else
910 newCursor.xdata = SelIdx;
911 }
912
913 return newCursor;
914}
915
916CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
917 if (cursor.kind != CXCursor_CallExpr)
918 return cursor;
919
920 if (cursor.xdata == 0)
921 return cursor;
922
923 Expr *E = getCursorExpr(cursor);
924 TypeSourceInfo *Type = 0;
925 if (CXXUnresolvedConstructExpr *
926 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
927 Type = UnCtor->getTypeSourceInfo();
928 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
929 Type = Tmp->getTypeSourceInfo();
930 }
931
932 if (!Type)
933 return cursor;
934
935 CXTranslationUnit TU = getCursorTU(cursor);
936 QualType Ty = Type->getType();
937 TypeLoc TL = Type->getTypeLoc();
938 SourceLocation Loc = TL.getBeginLoc();
939
940 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
941 Ty = ElabT->getNamedType();
942 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
943 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
944 }
945
946 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
947 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
948 if (const TagType *Tag = Ty->getAs<TagType>())
949 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
950 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
951 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
952
953 return cursor;
954}
955
Douglas Gregor283cae32010-01-15 21:56:13 +0000956bool cxcursor::operator==(CXCursor X, CXCursor Y) {
957 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
958 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000959}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000960
961// FIXME: Remove once we can model DeclGroups and their appropriate ranges
962// properly in the ASTs.
963bool cxcursor::isFirstInDeclGroup(CXCursor C) {
964 assert(clang_isDeclaration(C.kind));
965 return ((uintptr_t) (C.data[1])) != 0;
966}
967
Ted Kremenekeca099b2010-12-08 23:43:14 +0000968//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000969// libclang CXCursor APIs
970//===----------------------------------------------------------------------===//
971
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000972extern "C" {
973
974int clang_Cursor_isNull(CXCursor cursor) {
975 return clang_equalCursors(cursor, clang_getNullCursor());
976}
977
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000978CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
979 return getCursorTU(cursor);
980}
981
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000982} // end: extern "C"
983
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000984//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000985// CXCursorSet.
986//===----------------------------------------------------------------------===//
987
988typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
989
990static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
991 return (CXCursorSet) setImpl;
992}
993static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
994 return (CXCursorSet_Impl*) set;
995}
996namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000997template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000998public:
999 static inline CXCursor getEmptyKey() {
1000 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1001 }
1002 static inline CXCursor getTombstoneKey() {
1003 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1004 }
1005 static inline unsigned getHashValue(const CXCursor &cursor) {
1006 return llvm::DenseMapInfo<std::pair<void*,void*> >
1007 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1008 }
1009 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1010 return x.kind == y.kind &&
1011 x.data[0] == y.data[0] &&
1012 x.data[1] == y.data[1];
1013 }
1014};
1015}
1016
1017extern "C" {
1018CXCursorSet clang_createCXCursorSet() {
1019 return packCXCursorSet(new CXCursorSet_Impl());
1020}
1021
1022void clang_disposeCXCursorSet(CXCursorSet set) {
1023 delete unpackCXCursorSet(set);
1024}
1025
1026unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1027 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1028 if (!setImpl)
1029 return 0;
1030 return setImpl->find(cursor) == setImpl->end();
1031}
1032
Anders Carlssone8b3de02010-12-09 01:00:12 +00001033unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001034 // Do not insert invalid cursors into the set.
1035 if (cursor.kind >= CXCursor_FirstInvalid &&
1036 cursor.kind <= CXCursor_LastInvalid)
1037 return 1;
1038
1039 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1040 if (!setImpl)
1041 return 1;
1042 unsigned &entry = (*setImpl)[cursor];
1043 unsigned flag = entry == 0 ? 1 : 0;
1044 entry = 1;
1045 return flag;
1046}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001047
1048CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1049 enum CXCursorKind kind = clang_getCursorKind(cursor);
1050 if (clang_isDeclaration(kind)) {
1051 Decl *decl = getCursorDecl(cursor);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +00001052 if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001053 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001054 CodeCompletionAllocator *Allocator
1055 = unit->getCursorCompletionAllocator().getPtr();
1056 CodeCompletionResult Result(namedDecl);
1057 CodeCompletionString *String
1058 = Result.CreateCodeCompletionString(unit->getASTContext(),
1059 unit->getPreprocessor(),
1060 *Allocator);
1061 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001062 }
1063 }
1064 else if (kind == CXCursor_MacroDefinition) {
1065 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1066 const IdentifierInfo *MacroInfo = definition->getName();
1067 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001068 CodeCompletionAllocator *Allocator
1069 = unit->getCursorCompletionAllocator().getPtr();
1070 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
1071 CodeCompletionString *String
1072 = Result.CreateCodeCompletionString(unit->getASTContext(),
1073 unit->getPreprocessor(),
1074 *Allocator);
1075 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001076 }
1077 return NULL;
1078}
1079
Ted Kremenekeca099b2010-12-08 23:43:14 +00001080} // end: extern "C"