blob: 531e70e17ada8e9d11ef16aa1736f90a2b5902e3 [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 Gregor97b98722010-01-19 23:20:36 +0000229 K = CXCursor_UnexposedExpr;
230 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000231
John McCall4b9c2d22011-11-06 09:01:30 +0000232 case Stmt::OpaqueValueExprClass:
233 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
234 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
235 K = CXCursor_UnexposedExpr;
236 break;
237
238 case Stmt::PseudoObjectExprClass:
239 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
240 Parent, TU, RegionOfInterest);
241
Douglas Gregor42b29842011-10-05 19:00:14 +0000242 case Stmt::CompoundStmtClass:
243 K = CXCursor_CompoundStmt;
244 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000245
Douglas Gregor42b29842011-10-05 19:00:14 +0000246 case Stmt::NullStmtClass:
247 K = CXCursor_NullStmt;
248 break;
249
250 case Stmt::LabelStmtClass:
251 K = CXCursor_LabelStmt;
252 break;
253
254 case Stmt::DeclStmtClass:
255 K = CXCursor_DeclStmt;
256 break;
257
258 case Stmt::IntegerLiteralClass:
259 K = CXCursor_IntegerLiteral;
260 break;
261
262 case Stmt::FloatingLiteralClass:
263 K = CXCursor_FloatingLiteral;
264 break;
265
266 case Stmt::ImaginaryLiteralClass:
267 K = CXCursor_ImaginaryLiteral;
268 break;
269
270 case Stmt::StringLiteralClass:
271 K = CXCursor_StringLiteral;
272 break;
273
274 case Stmt::CharacterLiteralClass:
275 K = CXCursor_CharacterLiteral;
276 break;
277
278 case Stmt::ParenExprClass:
279 K = CXCursor_ParenExpr;
280 break;
281
282 case Stmt::UnaryOperatorClass:
283 K = CXCursor_UnaryOperator;
284 break;
285
286 case Stmt::CXXNoexceptExprClass:
287 K = CXCursor_UnaryExpr;
288 break;
289
290 case Stmt::ArraySubscriptExprClass:
291 K = CXCursor_ArraySubscriptExpr;
292 break;
293
294 case Stmt::BinaryOperatorClass:
295 K = CXCursor_BinaryOperator;
296 break;
297
298 case Stmt::CompoundAssignOperatorClass:
299 K = CXCursor_CompoundAssignOperator;
300 break;
301
302 case Stmt::ConditionalOperatorClass:
303 K = CXCursor_ConditionalOperator;
304 break;
305
306 case Stmt::CStyleCastExprClass:
307 K = CXCursor_CStyleCastExpr;
308 break;
309
310 case Stmt::CompoundLiteralExprClass:
311 K = CXCursor_CompoundLiteralExpr;
312 break;
313
314 case Stmt::InitListExprClass:
315 K = CXCursor_InitListExpr;
316 break;
317
318 case Stmt::AddrLabelExprClass:
319 K = CXCursor_AddrLabelExpr;
320 break;
321
322 case Stmt::StmtExprClass:
323 K = CXCursor_StmtExpr;
324 break;
325
326 case Stmt::GenericSelectionExprClass:
327 K = CXCursor_GenericSelectionExpr;
328 break;
329
330 case Stmt::GNUNullExprClass:
331 K = CXCursor_GNUNullExpr;
332 break;
333
334 case Stmt::CXXStaticCastExprClass:
335 K = CXCursor_CXXStaticCastExpr;
336 break;
337
338 case Stmt::CXXDynamicCastExprClass:
339 K = CXCursor_CXXDynamicCastExpr;
340 break;
341
342 case Stmt::CXXReinterpretCastExprClass:
343 K = CXCursor_CXXReinterpretCastExpr;
344 break;
345
346 case Stmt::CXXConstCastExprClass:
347 K = CXCursor_CXXConstCastExpr;
348 break;
349
350 case Stmt::CXXFunctionalCastExprClass:
351 K = CXCursor_CXXFunctionalCastExpr;
352 break;
353
354 case Stmt::CXXTypeidExprClass:
355 K = CXCursor_CXXTypeidExpr;
356 break;
357
358 case Stmt::CXXBoolLiteralExprClass:
359 K = CXCursor_CXXBoolLiteralExpr;
360 break;
361
362 case Stmt::CXXNullPtrLiteralExprClass:
363 K = CXCursor_CXXNullPtrLiteralExpr;
364 break;
365
366 case Stmt::CXXThisExprClass:
367 K = CXCursor_CXXThisExpr;
368 break;
369
370 case Stmt::CXXThrowExprClass:
371 K = CXCursor_CXXThrowExpr;
372 break;
373
374 case Stmt::CXXNewExprClass:
375 K = CXCursor_CXXNewExpr;
376 break;
377
378 case Stmt::CXXDeleteExprClass:
379 K = CXCursor_CXXDeleteExpr;
380 break;
381
382 case Stmt::ObjCStringLiteralClass:
383 K = CXCursor_ObjCStringLiteral;
384 break;
385
386 case Stmt::ObjCEncodeExprClass:
387 K = CXCursor_ObjCEncodeExpr;
388 break;
389
390 case Stmt::ObjCSelectorExprClass:
391 K = CXCursor_ObjCSelectorExpr;
392 break;
393
394 case Stmt::ObjCProtocolExprClass:
395 K = CXCursor_ObjCProtocolExpr;
396 break;
397
398 case Stmt::ObjCBridgedCastExprClass:
399 K = CXCursor_ObjCBridgedCastExpr;
400 break;
401
402 case Stmt::BlockExprClass:
403 K = CXCursor_BlockExpr;
404 break;
405
406 case Stmt::PackExpansionExprClass:
407 K = CXCursor_PackExpansionExpr;
408 break;
409
410 case Stmt::SizeOfPackExprClass:
411 K = CXCursor_SizeOfPackExpr;
412 break;
413
Douglas Gregor97b98722010-01-19 23:20:36 +0000414 case Stmt::BlockDeclRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000415 case Stmt::DeclRefExprClass:
416 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000417 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000418 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000419 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000420 K = CXCursor_DeclRefExpr;
421 break;
422
Douglas Gregor42b29842011-10-05 19:00:14 +0000423 case Stmt::CXXDependentScopeMemberExprClass:
424 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000425 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000426 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000427 case Stmt::ObjCIvarRefExprClass:
428 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000429 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000430 K = CXCursor_MemberRefExpr;
431 break;
432
433 case Stmt::CallExprClass:
434 case Stmt::CXXOperatorCallExprClass:
435 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000436 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000437 case Stmt::CXXConstructExprClass:
438 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000439 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000440 K = CXCursor_CallExpr;
441 break;
442
Douglas Gregor011d8b92012-02-15 00:54:55 +0000443 case Stmt::LambdaExprClass:
444 K = CXCursor_LambdaExpr;
445 break;
446
Douglas Gregorba0513d2011-10-25 01:33:02 +0000447 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000448 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000449 int SelectorIdIndex = -1;
450 // Check if cursor points to a selector id.
451 if (RegionOfInterest.isValid() &&
452 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
453 SmallVector<SourceLocation, 16> SelLocs;
454 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
455 SmallVector<SourceLocation, 16>::iterator
456 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
457 if (I != SelLocs.end())
458 SelectorIdIndex = I - SelLocs.begin();
459 }
460 CXCursor C = { K, 0, { Parent, S, TU } };
461 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000462 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000463
464 case Stmt::MSDependentExistsStmtClass:
465 K = CXCursor_UnexposedStmt;
466 break;
467 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000468
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000469 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000470 return C;
471}
472
Douglas Gregor2e331b92010-01-16 14:00:32 +0000473CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000474 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000475 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000476 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000477 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000478 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000479 return C;
480}
481
482std::pair<ObjCInterfaceDecl *, SourceLocation>
483cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
484 assert(C.kind == CXCursor_ObjCSuperClassRef);
485 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
486 SourceLocation::getFromRawEncoding(
487 reinterpret_cast<uintptr_t>(C.data[1])));
488}
489
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000490CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000491 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000492 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000493 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000494 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000495 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000496 return C;
497}
498
499std::pair<ObjCProtocolDecl *, SourceLocation>
500cxcursor::getCursorObjCProtocolRef(CXCursor C) {
501 assert(C.kind == CXCursor_ObjCProtocolRef);
502 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
503 SourceLocation::getFromRawEncoding(
504 reinterpret_cast<uintptr_t>(C.data[1])));
505}
506
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000507CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000508 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000509 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000510 // 'Class' can be null for invalid code.
511 if (!Class)
512 return MakeCXCursorInvalid(CXCursor_InvalidCode);
513 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000514 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000515 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000516 return C;
517}
518
519std::pair<ObjCInterfaceDecl *, SourceLocation>
520cxcursor::getCursorObjCClassRef(CXCursor C) {
521 assert(C.kind == CXCursor_ObjCClassRef);
522 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
523 SourceLocation::getFromRawEncoding(
524 reinterpret_cast<uintptr_t>(C.data[1])));
525}
526
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000527CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000528 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000529 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000530 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000531 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000532 return C;
533}
534
535std::pair<TypeDecl *, SourceLocation>
536cxcursor::getCursorTypeRef(CXCursor C) {
537 assert(C.kind == CXCursor_TypeRef);
538 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
539 SourceLocation::getFromRawEncoding(
540 reinterpret_cast<uintptr_t>(C.data[1])));
541}
542
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000543CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000544 SourceLocation Loc,
545 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000546 assert(Template && TU && "Invalid arguments!");
547 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000548 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000549 return C;
550}
551
552std::pair<TemplateDecl *, SourceLocation>
553cxcursor::getCursorTemplateRef(CXCursor C) {
554 assert(C.kind == CXCursor_TemplateRef);
555 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
556 SourceLocation::getFromRawEncoding(
557 reinterpret_cast<uintptr_t>(C.data[1])));
558}
559
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000560CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
561 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000562 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000563
564 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
565 "Invalid arguments!");
566 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000567 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000568 return C;
569}
570
571std::pair<NamedDecl *, SourceLocation>
572cxcursor::getCursorNamespaceRef(CXCursor C) {
573 assert(C.kind == CXCursor_NamespaceRef);
574 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
575 SourceLocation::getFromRawEncoding(
576 reinterpret_cast<uintptr_t>(C.data[1])));
577}
578
Douglas Gregor011d8b92012-02-15 00:54:55 +0000579CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
580 CXTranslationUnit TU) {
581
582 assert(Var && TU && "Invalid arguments!");
583 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
584 CXCursor C = { CXCursor_VariableRef, 0, { (void*)Var, RawLoc, TU } };
585 return C;
586}
587
588std::pair<VarDecl *, SourceLocation>
589cxcursor::getCursorVariableRef(CXCursor C) {
590 assert(C.kind == CXCursor_VariableRef);
591 return std::make_pair(static_cast<VarDecl *>(C.data[0]),
592 SourceLocation::getFromRawEncoding(
593 reinterpret_cast<uintptr_t>(C.data[1])));
594}
595
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000596CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000597 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000598
599 assert(Field && TU && "Invalid arguments!");
600 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000601 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000602 return C;
603}
604
605std::pair<FieldDecl *, SourceLocation>
606cxcursor::getCursorMemberRef(CXCursor C) {
607 assert(C.kind == CXCursor_MemberRef);
608 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
609 SourceLocation::getFromRawEncoding(
610 reinterpret_cast<uintptr_t>(C.data[1])));
611}
612
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000613CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000614 CXTranslationUnit TU){
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000615 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000616 return C;
617}
618
619CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
620 assert(C.kind == CXCursor_CXXBaseSpecifier);
621 return static_cast<CXXBaseSpecifier*>(C.data[0]);
622}
623
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000624CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000625 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000626 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000627 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
628 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
629 TU }
630 };
631 return C;
632}
633
634SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
635 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000636 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000637 reinterpret_cast<uintptr_t> (C.data[0])),
638 SourceLocation::getFromRawEncoding(
639 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000640 ASTUnit *TU = getCursorASTUnit(C);
641 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000642}
643
Ted Kremeneka60ed472010-11-16 08:15:36 +0000644CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
645 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000646 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000647 return C;
648}
649
650MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
651 assert(C.kind == CXCursor_MacroDefinition);
652 return static_cast<MacroDefinition *>(C.data[0]);
653}
654
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000655CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
656 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000657 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000658 return C;
659}
660
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000661MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000662 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000663 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000664}
665
Douglas Gregorecdcb882010-10-20 22:00:55 +0000666CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000667 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000668 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000669 return C;
670}
671
672InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
673 assert(C.kind == CXCursor_InclusionDirective);
674 return static_cast<InclusionDirective *>(C.data[0]);
675}
676
Douglas Gregor36897b02010-09-10 00:22:18 +0000677CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000678 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000679
680 assert(Label && TU && "Invalid arguments!");
681 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000682 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000683 return C;
684}
685
686std::pair<LabelStmt*, SourceLocation>
687cxcursor::getCursorLabelRef(CXCursor C) {
688 assert(C.kind == CXCursor_LabelRef);
689 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
690 SourceLocation::getFromRawEncoding(
691 reinterpret_cast<uintptr_t>(C.data[1])));
692}
693
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000694CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000695 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000696 assert(E && TU && "Invalid arguments!");
697 OverloadedDeclRefStorage Storage(E);
698 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
699 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000700 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000701 { Storage.getOpaqueValue(), RawLoc, TU }
702 };
703 return C;
704}
705
706CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
707 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000708 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000709 assert(D && TU && "Invalid arguments!");
710 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
711 OverloadedDeclRefStorage Storage(D);
712 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000713 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000714 { Storage.getOpaqueValue(), RawLoc, TU }
715 };
716 return C;
717}
718
719CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
720 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000721 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000722 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
723 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
724 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
725 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000726 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000727 { Storage.getOpaqueValue(), RawLoc, TU }
728 };
729 return C;
730}
731
732std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
733cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
734 assert(C.kind == CXCursor_OverloadedDeclRef);
735 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
736 SourceLocation::getFromRawEncoding(
737 reinterpret_cast<uintptr_t>(C.data[1])));
738}
739
Douglas Gregor283cae32010-01-15 21:56:13 +0000740Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
741 return (Decl *)Cursor.data[0];
742}
743
744Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
745 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
746}
747
748Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000749 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000750 Cursor.kind == CXCursor_ObjCProtocolRef ||
751 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000752 return 0;
753
Douglas Gregor283cae32010-01-15 21:56:13 +0000754 return (Stmt *)Cursor.data[1];
755}
756
Ted Kremenek95f33552010-08-26 01:42:22 +0000757Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
758 return (Attr *)Cursor.data[1];
759}
760
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000761Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
762 return (Decl *)Cursor.data[0];
763}
764
Douglas Gregorf46034a2010-01-18 23:41:10 +0000765ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000766 return getCursorASTUnit(Cursor)->getASTContext();
767}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000768
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000769ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000770 CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]);
771 if (!TU)
772 return 0;
773 return static_cast<ASTUnit *>(TU->TUData);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000774}
775
776CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
777 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000778}
779
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000780static void CollectOverriddenMethods(CXTranslationUnit TU,
781 DeclContext *Ctx,
782 ObjCMethodDecl *Method,
783 SmallVectorImpl<CXCursor> &Methods) {
784 if (!Ctx)
785 return;
786
787 // If we have a class or category implementation, jump straight to the
788 // interface.
789 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
790 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
791 Method, Methods);
792
793 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
794 if (!Container)
795 return;
796
797 // Check whether we have a matching method at this level.
798 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
799 Method->isInstanceMethod()))
800 if (Method != Overridden) {
801 // We found an override at this level; there is no need to look
802 // into other protocols or categories.
803 Methods.push_back(MakeCXCursor(Overridden, TU));
804 return;
805 }
806
807 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
808 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
809 PEnd = Protocol->protocol_end();
810 P != PEnd; ++P)
811 CollectOverriddenMethods(TU, *P, Method, Methods);
812 }
813
814 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
815 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
816 PEnd = Category->protocol_end();
817 P != PEnd; ++P)
818 CollectOverriddenMethods(TU, *P, Method, Methods);
819 }
820
821 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
822 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
823 PEnd = Interface->protocol_end();
824 P != PEnd; ++P)
825 CollectOverriddenMethods(TU, *P, Method, Methods);
826
827 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
828 Category; Category = Category->getNextClassCategory())
829 CollectOverriddenMethods(TU, Category, Method, Methods);
830
831 // We only look into the superclass if we haven't found anything yet.
832 if (Methods.empty())
833 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
834 return CollectOverriddenMethods(TU, Super, Method, Methods);
835 }
836}
837
838void cxcursor::getOverriddenCursors(CXCursor cursor,
839 SmallVectorImpl<CXCursor> &overridden) {
840 if (!clang_isDeclaration(cursor.kind))
841 return;
842
843 Decl *D = getCursorDecl(cursor);
844 if (!D)
845 return;
846
847 // Handle C++ member functions.
848 CXTranslationUnit TU = getCursorTU(cursor);
849 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
850 for (CXXMethodDecl::method_iterator
851 M = CXXMethod->begin_overridden_methods(),
852 MEnd = CXXMethod->end_overridden_methods();
853 M != MEnd; ++M)
854 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
855 return;
856 }
857
858 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
859 if (!Method)
860 return;
861
862 // Handle Objective-C methods.
863 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
864}
865
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000866std::pair<int, SourceLocation>
867cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
868 if (cursor.kind == CXCursor_ObjCMessageExpr) {
869 if (cursor.xdata != -1)
870 return std::make_pair(cursor.xdata,
871 cast<ObjCMessageExpr>(getCursorExpr(cursor))
872 ->getSelectorLoc(cursor.xdata));
873 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
874 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
875 if (cursor.xdata != -1)
876 return std::make_pair(cursor.xdata,
877 cast<ObjCMethodDecl>(getCursorDecl(cursor))
878 ->getSelectorLoc(cursor.xdata));
879 }
880
881 return std::make_pair(-1, SourceLocation());
882}
883
884CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
885 CXCursor newCursor = cursor;
886
887 if (cursor.kind == CXCursor_ObjCMessageExpr) {
888 if (SelIdx == -1 ||
889 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
890 ->getNumSelectorLocs())
891 newCursor.xdata = -1;
892 else
893 newCursor.xdata = SelIdx;
894 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
895 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
896 if (SelIdx == -1 ||
897 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
898 ->getNumSelectorLocs())
899 newCursor.xdata = -1;
900 else
901 newCursor.xdata = SelIdx;
902 }
903
904 return newCursor;
905}
906
907CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
908 if (cursor.kind != CXCursor_CallExpr)
909 return cursor;
910
911 if (cursor.xdata == 0)
912 return cursor;
913
914 Expr *E = getCursorExpr(cursor);
915 TypeSourceInfo *Type = 0;
916 if (CXXUnresolvedConstructExpr *
917 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
918 Type = UnCtor->getTypeSourceInfo();
919 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
920 Type = Tmp->getTypeSourceInfo();
921 }
922
923 if (!Type)
924 return cursor;
925
926 CXTranslationUnit TU = getCursorTU(cursor);
927 QualType Ty = Type->getType();
928 TypeLoc TL = Type->getTypeLoc();
929 SourceLocation Loc = TL.getBeginLoc();
930
931 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
932 Ty = ElabT->getNamedType();
933 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
934 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
935 }
936
937 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
938 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
939 if (const TagType *Tag = Ty->getAs<TagType>())
940 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
941 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
942 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
943
944 return cursor;
945}
946
Douglas Gregor283cae32010-01-15 21:56:13 +0000947bool cxcursor::operator==(CXCursor X, CXCursor Y) {
948 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
949 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000950}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000951
952// FIXME: Remove once we can model DeclGroups and their appropriate ranges
953// properly in the ASTs.
954bool cxcursor::isFirstInDeclGroup(CXCursor C) {
955 assert(clang_isDeclaration(C.kind));
956 return ((uintptr_t) (C.data[1])) != 0;
957}
958
Ted Kremenekeca099b2010-12-08 23:43:14 +0000959//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000960// libclang CXCursor APIs
961//===----------------------------------------------------------------------===//
962
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000963extern "C" {
964
965int clang_Cursor_isNull(CXCursor cursor) {
966 return clang_equalCursors(cursor, clang_getNullCursor());
967}
968
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000969CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
970 return getCursorTU(cursor);
971}
972
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000973} // end: extern "C"
974
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000975//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000976// CXCursorSet.
977//===----------------------------------------------------------------------===//
978
979typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
980
981static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
982 return (CXCursorSet) setImpl;
983}
984static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
985 return (CXCursorSet_Impl*) set;
986}
987namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000988template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000989public:
990 static inline CXCursor getEmptyKey() {
991 return MakeCXCursorInvalid(CXCursor_InvalidFile);
992 }
993 static inline CXCursor getTombstoneKey() {
994 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
995 }
996 static inline unsigned getHashValue(const CXCursor &cursor) {
997 return llvm::DenseMapInfo<std::pair<void*,void*> >
998 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
999 }
1000 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1001 return x.kind == y.kind &&
1002 x.data[0] == y.data[0] &&
1003 x.data[1] == y.data[1];
1004 }
1005};
1006}
1007
1008extern "C" {
1009CXCursorSet clang_createCXCursorSet() {
1010 return packCXCursorSet(new CXCursorSet_Impl());
1011}
1012
1013void clang_disposeCXCursorSet(CXCursorSet set) {
1014 delete unpackCXCursorSet(set);
1015}
1016
1017unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1018 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1019 if (!setImpl)
1020 return 0;
1021 return setImpl->find(cursor) == setImpl->end();
1022}
1023
Anders Carlssone8b3de02010-12-09 01:00:12 +00001024unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001025 // Do not insert invalid cursors into the set.
1026 if (cursor.kind >= CXCursor_FirstInvalid &&
1027 cursor.kind <= CXCursor_LastInvalid)
1028 return 1;
1029
1030 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1031 if (!setImpl)
1032 return 1;
1033 unsigned &entry = (*setImpl)[cursor];
1034 unsigned flag = entry == 0 ? 1 : 0;
1035 entry = 1;
1036 return flag;
1037}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001038
1039CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1040 enum CXCursorKind kind = clang_getCursorKind(cursor);
1041 if (clang_isDeclaration(kind)) {
1042 Decl *decl = getCursorDecl(cursor);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +00001043 if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001044 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001045 CodeCompletionAllocator *Allocator
1046 = unit->getCursorCompletionAllocator().getPtr();
1047 CodeCompletionResult Result(namedDecl);
1048 CodeCompletionString *String
1049 = Result.CreateCodeCompletionString(unit->getASTContext(),
1050 unit->getPreprocessor(),
1051 *Allocator);
1052 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001053 }
1054 }
1055 else if (kind == CXCursor_MacroDefinition) {
1056 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1057 const IdentifierInfo *MacroInfo = definition->getName();
1058 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001059 CodeCompletionAllocator *Allocator
1060 = unit->getCursorCompletionAllocator().getPtr();
1061 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
1062 CodeCompletionString *String
1063 = Result.CreateCodeCompletionString(unit->getASTContext(),
1064 unit->getPreprocessor(),
1065 *Allocator);
1066 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001067 }
1068 return NULL;
1069}
1070
Ted Kremenekeca099b2010-12-08 23:43:14 +00001071} // end: extern "C"