blob: af640bd7ab4dc5867de9024ae76ecb7521c77144 [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:
209 case Stmt::CXXBindTemporaryExprClass:
210 case Stmt::CXXDefaultArgExprClass:
211 case Stmt::CXXScalarValueInitExprClass:
212 case Stmt::CXXUuidofExprClass:
213 case Stmt::ChooseExprClass:
214 case Stmt::DesignatedInitExprClass:
215 case Stmt::ExprWithCleanupsClass:
216 case Stmt::ExpressionTraitExprClass:
217 case Stmt::ExtVectorElementExprClass:
218 case Stmt::ImplicitCastExprClass:
219 case Stmt::ImplicitValueInitExprClass:
220 case Stmt::MaterializeTemporaryExprClass:
221 case Stmt::ObjCIndirectCopyRestoreExprClass:
222 case Stmt::OffsetOfExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000223 case Stmt::ParenListExprClass:
224 case Stmt::PredefinedExprClass:
225 case Stmt::ShuffleVectorExprClass:
226 case Stmt::UnaryExprOrTypeTraitExprClass:
227 case Stmt::UnaryTypeTraitExprClass:
228 case Stmt::VAArgExprClass:
Douglas Gregor01d08012012-02-07 10:09:13 +0000229 case Stmt::LambdaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000230 K = CXCursor_UnexposedExpr;
231 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000232
John McCall4b9c2d22011-11-06 09:01:30 +0000233 case Stmt::OpaqueValueExprClass:
234 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
235 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
236 K = CXCursor_UnexposedExpr;
237 break;
238
239 case Stmt::PseudoObjectExprClass:
240 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
241 Parent, TU, RegionOfInterest);
242
Douglas Gregor42b29842011-10-05 19:00:14 +0000243 case Stmt::CompoundStmtClass:
244 K = CXCursor_CompoundStmt;
245 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000246
Douglas Gregor42b29842011-10-05 19:00:14 +0000247 case Stmt::NullStmtClass:
248 K = CXCursor_NullStmt;
249 break;
250
251 case Stmt::LabelStmtClass:
252 K = CXCursor_LabelStmt;
253 break;
254
255 case Stmt::DeclStmtClass:
256 K = CXCursor_DeclStmt;
257 break;
258
259 case Stmt::IntegerLiteralClass:
260 K = CXCursor_IntegerLiteral;
261 break;
262
263 case Stmt::FloatingLiteralClass:
264 K = CXCursor_FloatingLiteral;
265 break;
266
267 case Stmt::ImaginaryLiteralClass:
268 K = CXCursor_ImaginaryLiteral;
269 break;
270
271 case Stmt::StringLiteralClass:
272 K = CXCursor_StringLiteral;
273 break;
274
275 case Stmt::CharacterLiteralClass:
276 K = CXCursor_CharacterLiteral;
277 break;
278
279 case Stmt::ParenExprClass:
280 K = CXCursor_ParenExpr;
281 break;
282
283 case Stmt::UnaryOperatorClass:
284 K = CXCursor_UnaryOperator;
285 break;
286
287 case Stmt::CXXNoexceptExprClass:
288 K = CXCursor_UnaryExpr;
289 break;
290
291 case Stmt::ArraySubscriptExprClass:
292 K = CXCursor_ArraySubscriptExpr;
293 break;
294
295 case Stmt::BinaryOperatorClass:
296 K = CXCursor_BinaryOperator;
297 break;
298
299 case Stmt::CompoundAssignOperatorClass:
300 K = CXCursor_CompoundAssignOperator;
301 break;
302
303 case Stmt::ConditionalOperatorClass:
304 K = CXCursor_ConditionalOperator;
305 break;
306
307 case Stmt::CStyleCastExprClass:
308 K = CXCursor_CStyleCastExpr;
309 break;
310
311 case Stmt::CompoundLiteralExprClass:
312 K = CXCursor_CompoundLiteralExpr;
313 break;
314
315 case Stmt::InitListExprClass:
316 K = CXCursor_InitListExpr;
317 break;
318
319 case Stmt::AddrLabelExprClass:
320 K = CXCursor_AddrLabelExpr;
321 break;
322
323 case Stmt::StmtExprClass:
324 K = CXCursor_StmtExpr;
325 break;
326
327 case Stmt::GenericSelectionExprClass:
328 K = CXCursor_GenericSelectionExpr;
329 break;
330
331 case Stmt::GNUNullExprClass:
332 K = CXCursor_GNUNullExpr;
333 break;
334
335 case Stmt::CXXStaticCastExprClass:
336 K = CXCursor_CXXStaticCastExpr;
337 break;
338
339 case Stmt::CXXDynamicCastExprClass:
340 K = CXCursor_CXXDynamicCastExpr;
341 break;
342
343 case Stmt::CXXReinterpretCastExprClass:
344 K = CXCursor_CXXReinterpretCastExpr;
345 break;
346
347 case Stmt::CXXConstCastExprClass:
348 K = CXCursor_CXXConstCastExpr;
349 break;
350
351 case Stmt::CXXFunctionalCastExprClass:
352 K = CXCursor_CXXFunctionalCastExpr;
353 break;
354
355 case Stmt::CXXTypeidExprClass:
356 K = CXCursor_CXXTypeidExpr;
357 break;
358
359 case Stmt::CXXBoolLiteralExprClass:
360 K = CXCursor_CXXBoolLiteralExpr;
361 break;
362
363 case Stmt::CXXNullPtrLiteralExprClass:
364 K = CXCursor_CXXNullPtrLiteralExpr;
365 break;
366
367 case Stmt::CXXThisExprClass:
368 K = CXCursor_CXXThisExpr;
369 break;
370
371 case Stmt::CXXThrowExprClass:
372 K = CXCursor_CXXThrowExpr;
373 break;
374
375 case Stmt::CXXNewExprClass:
376 K = CXCursor_CXXNewExpr;
377 break;
378
379 case Stmt::CXXDeleteExprClass:
380 K = CXCursor_CXXDeleteExpr;
381 break;
382
383 case Stmt::ObjCStringLiteralClass:
384 K = CXCursor_ObjCStringLiteral;
385 break;
386
387 case Stmt::ObjCEncodeExprClass:
388 K = CXCursor_ObjCEncodeExpr;
389 break;
390
391 case Stmt::ObjCSelectorExprClass:
392 K = CXCursor_ObjCSelectorExpr;
393 break;
394
395 case Stmt::ObjCProtocolExprClass:
396 K = CXCursor_ObjCProtocolExpr;
397 break;
398
399 case Stmt::ObjCBridgedCastExprClass:
400 K = CXCursor_ObjCBridgedCastExpr;
401 break;
402
403 case Stmt::BlockExprClass:
404 K = CXCursor_BlockExpr;
405 break;
406
407 case Stmt::PackExpansionExprClass:
408 K = CXCursor_PackExpansionExpr;
409 break;
410
411 case Stmt::SizeOfPackExprClass:
412 K = CXCursor_SizeOfPackExpr;
413 break;
414
Douglas Gregor97b98722010-01-19 23:20:36 +0000415 case Stmt::BlockDeclRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000416 case Stmt::DeclRefExprClass:
417 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000418 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000419 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000420 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000421 K = CXCursor_DeclRefExpr;
422 break;
423
Douglas Gregor42b29842011-10-05 19:00:14 +0000424 case Stmt::CXXDependentScopeMemberExprClass:
425 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000426 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000427 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000428 case Stmt::ObjCIvarRefExprClass:
429 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000430 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000431 K = CXCursor_MemberRefExpr;
432 break;
433
434 case Stmt::CallExprClass:
435 case Stmt::CXXOperatorCallExprClass:
436 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000437 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000438 case Stmt::CXXConstructExprClass:
439 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000440 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000441 K = CXCursor_CallExpr;
442 break;
443
Douglas Gregorba0513d2011-10-25 01:33:02 +0000444 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000445 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000446 int SelectorIdIndex = -1;
447 // Check if cursor points to a selector id.
448 if (RegionOfInterest.isValid() &&
449 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
450 SmallVector<SourceLocation, 16> SelLocs;
451 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
452 SmallVector<SourceLocation, 16>::iterator
453 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
454 if (I != SelLocs.end())
455 SelectorIdIndex = I - SelLocs.begin();
456 }
457 CXCursor C = { K, 0, { Parent, S, TU } };
458 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000459 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000460
461 case Stmt::MSDependentExistsStmtClass:
462 K = CXCursor_UnexposedStmt;
463 break;
464 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000465
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000466 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000467 return C;
468}
469
Douglas Gregor2e331b92010-01-16 14:00:32 +0000470CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000471 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000472 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000473 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000474 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000475 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000476 return C;
477}
478
479std::pair<ObjCInterfaceDecl *, SourceLocation>
480cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
481 assert(C.kind == CXCursor_ObjCSuperClassRef);
482 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
483 SourceLocation::getFromRawEncoding(
484 reinterpret_cast<uintptr_t>(C.data[1])));
485}
486
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000487CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000488 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000489 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000490 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000491 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000492 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000493 return C;
494}
495
496std::pair<ObjCProtocolDecl *, SourceLocation>
497cxcursor::getCursorObjCProtocolRef(CXCursor C) {
498 assert(C.kind == CXCursor_ObjCProtocolRef);
499 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
500 SourceLocation::getFromRawEncoding(
501 reinterpret_cast<uintptr_t>(C.data[1])));
502}
503
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000504CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000505 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000506 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000507 // 'Class' can be null for invalid code.
508 if (!Class)
509 return MakeCXCursorInvalid(CXCursor_InvalidCode);
510 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000511 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000512 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000513 return C;
514}
515
516std::pair<ObjCInterfaceDecl *, SourceLocation>
517cxcursor::getCursorObjCClassRef(CXCursor C) {
518 assert(C.kind == CXCursor_ObjCClassRef);
519 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
520 SourceLocation::getFromRawEncoding(
521 reinterpret_cast<uintptr_t>(C.data[1])));
522}
523
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000524CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000525 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000526 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000527 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000528 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000529 return C;
530}
531
532std::pair<TypeDecl *, SourceLocation>
533cxcursor::getCursorTypeRef(CXCursor C) {
534 assert(C.kind == CXCursor_TypeRef);
535 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
536 SourceLocation::getFromRawEncoding(
537 reinterpret_cast<uintptr_t>(C.data[1])));
538}
539
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000540CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000541 SourceLocation Loc,
542 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000543 assert(Template && TU && "Invalid arguments!");
544 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000545 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000546 return C;
547}
548
549std::pair<TemplateDecl *, SourceLocation>
550cxcursor::getCursorTemplateRef(CXCursor C) {
551 assert(C.kind == CXCursor_TemplateRef);
552 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
553 SourceLocation::getFromRawEncoding(
554 reinterpret_cast<uintptr_t>(C.data[1])));
555}
556
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000557CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
558 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000559 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000560
561 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
562 "Invalid arguments!");
563 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000564 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000565 return C;
566}
567
568std::pair<NamedDecl *, SourceLocation>
569cxcursor::getCursorNamespaceRef(CXCursor C) {
570 assert(C.kind == CXCursor_NamespaceRef);
571 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
572 SourceLocation::getFromRawEncoding(
573 reinterpret_cast<uintptr_t>(C.data[1])));
574}
575
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000576CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000577 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000578
579 assert(Field && TU && "Invalid arguments!");
580 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000581 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000582 return C;
583}
584
585std::pair<FieldDecl *, SourceLocation>
586cxcursor::getCursorMemberRef(CXCursor C) {
587 assert(C.kind == CXCursor_MemberRef);
588 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
589 SourceLocation::getFromRawEncoding(
590 reinterpret_cast<uintptr_t>(C.data[1])));
591}
592
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000593CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000594 CXTranslationUnit TU){
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000595 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000596 return C;
597}
598
599CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
600 assert(C.kind == CXCursor_CXXBaseSpecifier);
601 return static_cast<CXXBaseSpecifier*>(C.data[0]);
602}
603
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000604CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000605 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000606 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000607 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
608 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
609 TU }
610 };
611 return C;
612}
613
614SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
615 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000616 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000617 reinterpret_cast<uintptr_t> (C.data[0])),
618 SourceLocation::getFromRawEncoding(
619 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000620 ASTUnit *TU = getCursorASTUnit(C);
621 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000622}
623
Ted Kremeneka60ed472010-11-16 08:15:36 +0000624CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
625 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000626 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000627 return C;
628}
629
630MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
631 assert(C.kind == CXCursor_MacroDefinition);
632 return static_cast<MacroDefinition *>(C.data[0]);
633}
634
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000635CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
636 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000637 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000638 return C;
639}
640
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000641MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000642 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000643 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000644}
645
Douglas Gregorecdcb882010-10-20 22:00:55 +0000646CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000647 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000648 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000649 return C;
650}
651
652InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
653 assert(C.kind == CXCursor_InclusionDirective);
654 return static_cast<InclusionDirective *>(C.data[0]);
655}
656
Douglas Gregor36897b02010-09-10 00:22:18 +0000657CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000658 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000659
660 assert(Label && TU && "Invalid arguments!");
661 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000662 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000663 return C;
664}
665
666std::pair<LabelStmt*, SourceLocation>
667cxcursor::getCursorLabelRef(CXCursor C) {
668 assert(C.kind == CXCursor_LabelRef);
669 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
670 SourceLocation::getFromRawEncoding(
671 reinterpret_cast<uintptr_t>(C.data[1])));
672}
673
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000674CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000675 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000676 assert(E && TU && "Invalid arguments!");
677 OverloadedDeclRefStorage Storage(E);
678 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
679 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000680 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000681 { Storage.getOpaqueValue(), RawLoc, TU }
682 };
683 return C;
684}
685
686CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
687 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000688 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000689 assert(D && TU && "Invalid arguments!");
690 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
691 OverloadedDeclRefStorage Storage(D);
692 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000693 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000694 { Storage.getOpaqueValue(), RawLoc, TU }
695 };
696 return C;
697}
698
699CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
700 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000701 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000702 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
703 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
704 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
705 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000706 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000707 { Storage.getOpaqueValue(), RawLoc, TU }
708 };
709 return C;
710}
711
712std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
713cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
714 assert(C.kind == CXCursor_OverloadedDeclRef);
715 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
716 SourceLocation::getFromRawEncoding(
717 reinterpret_cast<uintptr_t>(C.data[1])));
718}
719
Douglas Gregor283cae32010-01-15 21:56:13 +0000720Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
721 return (Decl *)Cursor.data[0];
722}
723
724Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
725 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
726}
727
728Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000729 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000730 Cursor.kind == CXCursor_ObjCProtocolRef ||
731 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000732 return 0;
733
Douglas Gregor283cae32010-01-15 21:56:13 +0000734 return (Stmt *)Cursor.data[1];
735}
736
Ted Kremenek95f33552010-08-26 01:42:22 +0000737Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
738 return (Attr *)Cursor.data[1];
739}
740
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000741Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
742 return (Decl *)Cursor.data[0];
743}
744
Douglas Gregorf46034a2010-01-18 23:41:10 +0000745ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000746 return getCursorASTUnit(Cursor)->getASTContext();
747}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000748
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000749ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000750 CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]);
751 if (!TU)
752 return 0;
753 return static_cast<ASTUnit *>(TU->TUData);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000754}
755
756CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
757 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000758}
759
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000760static void CollectOverriddenMethods(CXTranslationUnit TU,
761 DeclContext *Ctx,
762 ObjCMethodDecl *Method,
763 SmallVectorImpl<CXCursor> &Methods) {
764 if (!Ctx)
765 return;
766
767 // If we have a class or category implementation, jump straight to the
768 // interface.
769 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
770 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
771 Method, Methods);
772
773 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
774 if (!Container)
775 return;
776
777 // Check whether we have a matching method at this level.
778 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
779 Method->isInstanceMethod()))
780 if (Method != Overridden) {
781 // We found an override at this level; there is no need to look
782 // into other protocols or categories.
783 Methods.push_back(MakeCXCursor(Overridden, TU));
784 return;
785 }
786
787 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
788 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
789 PEnd = Protocol->protocol_end();
790 P != PEnd; ++P)
791 CollectOverriddenMethods(TU, *P, Method, Methods);
792 }
793
794 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
795 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
796 PEnd = Category->protocol_end();
797 P != PEnd; ++P)
798 CollectOverriddenMethods(TU, *P, Method, Methods);
799 }
800
801 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
802 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
803 PEnd = Interface->protocol_end();
804 P != PEnd; ++P)
805 CollectOverriddenMethods(TU, *P, Method, Methods);
806
807 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
808 Category; Category = Category->getNextClassCategory())
809 CollectOverriddenMethods(TU, Category, Method, Methods);
810
811 // We only look into the superclass if we haven't found anything yet.
812 if (Methods.empty())
813 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
814 return CollectOverriddenMethods(TU, Super, Method, Methods);
815 }
816}
817
818void cxcursor::getOverriddenCursors(CXCursor cursor,
819 SmallVectorImpl<CXCursor> &overridden) {
820 if (!clang_isDeclaration(cursor.kind))
821 return;
822
823 Decl *D = getCursorDecl(cursor);
824 if (!D)
825 return;
826
827 // Handle C++ member functions.
828 CXTranslationUnit TU = getCursorTU(cursor);
829 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
830 for (CXXMethodDecl::method_iterator
831 M = CXXMethod->begin_overridden_methods(),
832 MEnd = CXXMethod->end_overridden_methods();
833 M != MEnd; ++M)
834 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
835 return;
836 }
837
838 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
839 if (!Method)
840 return;
841
842 // Handle Objective-C methods.
843 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
844}
845
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000846std::pair<int, SourceLocation>
847cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
848 if (cursor.kind == CXCursor_ObjCMessageExpr) {
849 if (cursor.xdata != -1)
850 return std::make_pair(cursor.xdata,
851 cast<ObjCMessageExpr>(getCursorExpr(cursor))
852 ->getSelectorLoc(cursor.xdata));
853 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
854 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
855 if (cursor.xdata != -1)
856 return std::make_pair(cursor.xdata,
857 cast<ObjCMethodDecl>(getCursorDecl(cursor))
858 ->getSelectorLoc(cursor.xdata));
859 }
860
861 return std::make_pair(-1, SourceLocation());
862}
863
864CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
865 CXCursor newCursor = cursor;
866
867 if (cursor.kind == CXCursor_ObjCMessageExpr) {
868 if (SelIdx == -1 ||
869 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
870 ->getNumSelectorLocs())
871 newCursor.xdata = -1;
872 else
873 newCursor.xdata = SelIdx;
874 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
875 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
876 if (SelIdx == -1 ||
877 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
878 ->getNumSelectorLocs())
879 newCursor.xdata = -1;
880 else
881 newCursor.xdata = SelIdx;
882 }
883
884 return newCursor;
885}
886
887CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
888 if (cursor.kind != CXCursor_CallExpr)
889 return cursor;
890
891 if (cursor.xdata == 0)
892 return cursor;
893
894 Expr *E = getCursorExpr(cursor);
895 TypeSourceInfo *Type = 0;
896 if (CXXUnresolvedConstructExpr *
897 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
898 Type = UnCtor->getTypeSourceInfo();
899 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
900 Type = Tmp->getTypeSourceInfo();
901 }
902
903 if (!Type)
904 return cursor;
905
906 CXTranslationUnit TU = getCursorTU(cursor);
907 QualType Ty = Type->getType();
908 TypeLoc TL = Type->getTypeLoc();
909 SourceLocation Loc = TL.getBeginLoc();
910
911 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
912 Ty = ElabT->getNamedType();
913 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
914 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
915 }
916
917 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
918 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
919 if (const TagType *Tag = Ty->getAs<TagType>())
920 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
921 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
922 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
923
924 return cursor;
925}
926
Douglas Gregor283cae32010-01-15 21:56:13 +0000927bool cxcursor::operator==(CXCursor X, CXCursor Y) {
928 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
929 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000930}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000931
932// FIXME: Remove once we can model DeclGroups and their appropriate ranges
933// properly in the ASTs.
934bool cxcursor::isFirstInDeclGroup(CXCursor C) {
935 assert(clang_isDeclaration(C.kind));
936 return ((uintptr_t) (C.data[1])) != 0;
937}
938
Ted Kremenekeca099b2010-12-08 23:43:14 +0000939//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000940// libclang CXCursor APIs
941//===----------------------------------------------------------------------===//
942
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000943extern "C" {
944
945int clang_Cursor_isNull(CXCursor cursor) {
946 return clang_equalCursors(cursor, clang_getNullCursor());
947}
948
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000949CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
950 return getCursorTU(cursor);
951}
952
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000953} // end: extern "C"
954
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000955//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000956// CXCursorSet.
957//===----------------------------------------------------------------------===//
958
959typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
960
961static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
962 return (CXCursorSet) setImpl;
963}
964static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
965 return (CXCursorSet_Impl*) set;
966}
967namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000968template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000969public:
970 static inline CXCursor getEmptyKey() {
971 return MakeCXCursorInvalid(CXCursor_InvalidFile);
972 }
973 static inline CXCursor getTombstoneKey() {
974 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
975 }
976 static inline unsigned getHashValue(const CXCursor &cursor) {
977 return llvm::DenseMapInfo<std::pair<void*,void*> >
978 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
979 }
980 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
981 return x.kind == y.kind &&
982 x.data[0] == y.data[0] &&
983 x.data[1] == y.data[1];
984 }
985};
986}
987
988extern "C" {
989CXCursorSet clang_createCXCursorSet() {
990 return packCXCursorSet(new CXCursorSet_Impl());
991}
992
993void clang_disposeCXCursorSet(CXCursorSet set) {
994 delete unpackCXCursorSet(set);
995}
996
997unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
998 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
999 if (!setImpl)
1000 return 0;
1001 return setImpl->find(cursor) == setImpl->end();
1002}
1003
Anders Carlssone8b3de02010-12-09 01:00:12 +00001004unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001005 // Do not insert invalid cursors into the set.
1006 if (cursor.kind >= CXCursor_FirstInvalid &&
1007 cursor.kind <= CXCursor_LastInvalid)
1008 return 1;
1009
1010 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1011 if (!setImpl)
1012 return 1;
1013 unsigned &entry = (*setImpl)[cursor];
1014 unsigned flag = entry == 0 ? 1 : 0;
1015 entry = 1;
1016 return flag;
1017}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001018
1019CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1020 enum CXCursorKind kind = clang_getCursorKind(cursor);
1021 if (clang_isDeclaration(kind)) {
1022 Decl *decl = getCursorDecl(cursor);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +00001023 if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001024 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001025 CodeCompletionAllocator *Allocator
1026 = unit->getCursorCompletionAllocator().getPtr();
1027 CodeCompletionResult Result(namedDecl);
1028 CodeCompletionString *String
1029 = Result.CreateCodeCompletionString(unit->getASTContext(),
1030 unit->getPreprocessor(),
1031 *Allocator);
1032 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001033 }
1034 }
1035 else if (kind == CXCursor_MacroDefinition) {
1036 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1037 const IdentifierInfo *MacroInfo = definition->getName();
1038 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001039 CodeCompletionAllocator *Allocator
1040 = unit->getCursorCompletionAllocator().getPtr();
1041 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
1042 CodeCompletionString *String
1043 = Result.CreateCodeCompletionString(unit->getASTContext(),
1044 unit->getPreprocessor(),
1045 *Allocator);
1046 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001047 }
1048 return NULL;
1049}
1050
Ted Kremenekeca099b2010-12-08 23:43:14 +00001051} // end: extern "C"