blob: fb70d9a0f6f5627e496050bfbf02a9c113adb500 [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 Gregorba0513d2011-10-25 01:33:02 +0000443 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000444 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000445 int SelectorIdIndex = -1;
446 // Check if cursor points to a selector id.
447 if (RegionOfInterest.isValid() &&
448 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
449 SmallVector<SourceLocation, 16> SelLocs;
450 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
451 SmallVector<SourceLocation, 16>::iterator
452 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
453 if (I != SelLocs.end())
454 SelectorIdIndex = I - SelLocs.begin();
455 }
456 CXCursor C = { K, 0, { Parent, S, TU } };
457 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000458 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000459
460 case Stmt::MSDependentExistsStmtClass:
461 K = CXCursor_UnexposedStmt;
462 break;
463 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000464
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000465 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000466 return C;
467}
468
Douglas Gregor2e331b92010-01-16 14:00:32 +0000469CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000470 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000471 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000472 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000473 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000474 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000475 return C;
476}
477
478std::pair<ObjCInterfaceDecl *, SourceLocation>
479cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
480 assert(C.kind == CXCursor_ObjCSuperClassRef);
481 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
482 SourceLocation::getFromRawEncoding(
483 reinterpret_cast<uintptr_t>(C.data[1])));
484}
485
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000486CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000487 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000488 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000489 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000490 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000491 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000492 return C;
493}
494
495std::pair<ObjCProtocolDecl *, SourceLocation>
496cxcursor::getCursorObjCProtocolRef(CXCursor C) {
497 assert(C.kind == CXCursor_ObjCProtocolRef);
498 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
499 SourceLocation::getFromRawEncoding(
500 reinterpret_cast<uintptr_t>(C.data[1])));
501}
502
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000503CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000504 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000505 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000506 // 'Class' can be null for invalid code.
507 if (!Class)
508 return MakeCXCursorInvalid(CXCursor_InvalidCode);
509 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000510 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000511 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000512 return C;
513}
514
515std::pair<ObjCInterfaceDecl *, SourceLocation>
516cxcursor::getCursorObjCClassRef(CXCursor C) {
517 assert(C.kind == CXCursor_ObjCClassRef);
518 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
519 SourceLocation::getFromRawEncoding(
520 reinterpret_cast<uintptr_t>(C.data[1])));
521}
522
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000523CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000524 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000525 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000526 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000527 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000528 return C;
529}
530
531std::pair<TypeDecl *, SourceLocation>
532cxcursor::getCursorTypeRef(CXCursor C) {
533 assert(C.kind == CXCursor_TypeRef);
534 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
535 SourceLocation::getFromRawEncoding(
536 reinterpret_cast<uintptr_t>(C.data[1])));
537}
538
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000539CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000540 SourceLocation Loc,
541 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000542 assert(Template && TU && "Invalid arguments!");
543 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000544 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000545 return C;
546}
547
548std::pair<TemplateDecl *, SourceLocation>
549cxcursor::getCursorTemplateRef(CXCursor C) {
550 assert(C.kind == CXCursor_TemplateRef);
551 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
552 SourceLocation::getFromRawEncoding(
553 reinterpret_cast<uintptr_t>(C.data[1])));
554}
555
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000556CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
557 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000558 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000559
560 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
561 "Invalid arguments!");
562 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000563 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000564 return C;
565}
566
567std::pair<NamedDecl *, SourceLocation>
568cxcursor::getCursorNamespaceRef(CXCursor C) {
569 assert(C.kind == CXCursor_NamespaceRef);
570 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
571 SourceLocation::getFromRawEncoding(
572 reinterpret_cast<uintptr_t>(C.data[1])));
573}
574
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000575CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000576 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000577
578 assert(Field && TU && "Invalid arguments!");
579 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000580 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000581 return C;
582}
583
584std::pair<FieldDecl *, SourceLocation>
585cxcursor::getCursorMemberRef(CXCursor C) {
586 assert(C.kind == CXCursor_MemberRef);
587 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
588 SourceLocation::getFromRawEncoding(
589 reinterpret_cast<uintptr_t>(C.data[1])));
590}
591
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000592CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000593 CXTranslationUnit TU){
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000594 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000595 return C;
596}
597
598CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
599 assert(C.kind == CXCursor_CXXBaseSpecifier);
600 return static_cast<CXXBaseSpecifier*>(C.data[0]);
601}
602
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000603CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000604 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000605 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000606 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
607 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
608 TU }
609 };
610 return C;
611}
612
613SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
614 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000615 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000616 reinterpret_cast<uintptr_t> (C.data[0])),
617 SourceLocation::getFromRawEncoding(
618 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000619 ASTUnit *TU = getCursorASTUnit(C);
620 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000621}
622
Ted Kremeneka60ed472010-11-16 08:15:36 +0000623CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
624 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000625 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000626 return C;
627}
628
629MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
630 assert(C.kind == CXCursor_MacroDefinition);
631 return static_cast<MacroDefinition *>(C.data[0]);
632}
633
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000634CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
635 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000636 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000637 return C;
638}
639
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000640MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000641 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000642 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000643}
644
Douglas Gregorecdcb882010-10-20 22:00:55 +0000645CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000646 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000647 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000648 return C;
649}
650
651InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
652 assert(C.kind == CXCursor_InclusionDirective);
653 return static_cast<InclusionDirective *>(C.data[0]);
654}
655
Douglas Gregor36897b02010-09-10 00:22:18 +0000656CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000657 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000658
659 assert(Label && TU && "Invalid arguments!");
660 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000661 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000662 return C;
663}
664
665std::pair<LabelStmt*, SourceLocation>
666cxcursor::getCursorLabelRef(CXCursor C) {
667 assert(C.kind == CXCursor_LabelRef);
668 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
669 SourceLocation::getFromRawEncoding(
670 reinterpret_cast<uintptr_t>(C.data[1])));
671}
672
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000673CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000674 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000675 assert(E && TU && "Invalid arguments!");
676 OverloadedDeclRefStorage Storage(E);
677 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
678 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000679 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000680 { Storage.getOpaqueValue(), RawLoc, TU }
681 };
682 return C;
683}
684
685CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
686 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000687 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000688 assert(D && TU && "Invalid arguments!");
689 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
690 OverloadedDeclRefStorage Storage(D);
691 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000692 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000693 { Storage.getOpaqueValue(), RawLoc, TU }
694 };
695 return C;
696}
697
698CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
699 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000700 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000701 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
702 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
703 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
704 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000705 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000706 { Storage.getOpaqueValue(), RawLoc, TU }
707 };
708 return C;
709}
710
711std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
712cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
713 assert(C.kind == CXCursor_OverloadedDeclRef);
714 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
715 SourceLocation::getFromRawEncoding(
716 reinterpret_cast<uintptr_t>(C.data[1])));
717}
718
Douglas Gregor283cae32010-01-15 21:56:13 +0000719Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
720 return (Decl *)Cursor.data[0];
721}
722
723Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
724 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
725}
726
727Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000728 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000729 Cursor.kind == CXCursor_ObjCProtocolRef ||
730 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000731 return 0;
732
Douglas Gregor283cae32010-01-15 21:56:13 +0000733 return (Stmt *)Cursor.data[1];
734}
735
Ted Kremenek95f33552010-08-26 01:42:22 +0000736Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
737 return (Attr *)Cursor.data[1];
738}
739
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000740Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
741 return (Decl *)Cursor.data[0];
742}
743
Douglas Gregorf46034a2010-01-18 23:41:10 +0000744ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000745 return getCursorASTUnit(Cursor)->getASTContext();
746}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000747
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000748ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Argyrios Kyrtzidis44517462011-12-09 00:17:49 +0000749 CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]);
750 if (!TU)
751 return 0;
752 return static_cast<ASTUnit *>(TU->TUData);
Ted Kremeneka60ed472010-11-16 08:15:36 +0000753}
754
755CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
756 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000757}
758
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000759static void CollectOverriddenMethods(CXTranslationUnit TU,
760 DeclContext *Ctx,
761 ObjCMethodDecl *Method,
762 SmallVectorImpl<CXCursor> &Methods) {
763 if (!Ctx)
764 return;
765
766 // If we have a class or category implementation, jump straight to the
767 // interface.
768 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
769 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
770 Method, Methods);
771
772 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
773 if (!Container)
774 return;
775
776 // Check whether we have a matching method at this level.
777 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
778 Method->isInstanceMethod()))
779 if (Method != Overridden) {
780 // We found an override at this level; there is no need to look
781 // into other protocols or categories.
782 Methods.push_back(MakeCXCursor(Overridden, TU));
783 return;
784 }
785
786 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
787 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
788 PEnd = Protocol->protocol_end();
789 P != PEnd; ++P)
790 CollectOverriddenMethods(TU, *P, Method, Methods);
791 }
792
793 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
794 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
795 PEnd = Category->protocol_end();
796 P != PEnd; ++P)
797 CollectOverriddenMethods(TU, *P, Method, Methods);
798 }
799
800 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
801 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
802 PEnd = Interface->protocol_end();
803 P != PEnd; ++P)
804 CollectOverriddenMethods(TU, *P, Method, Methods);
805
806 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
807 Category; Category = Category->getNextClassCategory())
808 CollectOverriddenMethods(TU, Category, Method, Methods);
809
810 // We only look into the superclass if we haven't found anything yet.
811 if (Methods.empty())
812 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
813 return CollectOverriddenMethods(TU, Super, Method, Methods);
814 }
815}
816
817void cxcursor::getOverriddenCursors(CXCursor cursor,
818 SmallVectorImpl<CXCursor> &overridden) {
819 if (!clang_isDeclaration(cursor.kind))
820 return;
821
822 Decl *D = getCursorDecl(cursor);
823 if (!D)
824 return;
825
826 // Handle C++ member functions.
827 CXTranslationUnit TU = getCursorTU(cursor);
828 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
829 for (CXXMethodDecl::method_iterator
830 M = CXXMethod->begin_overridden_methods(),
831 MEnd = CXXMethod->end_overridden_methods();
832 M != MEnd; ++M)
833 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
834 return;
835 }
836
837 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
838 if (!Method)
839 return;
840
841 // Handle Objective-C methods.
842 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
843}
844
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000845std::pair<int, SourceLocation>
846cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
847 if (cursor.kind == CXCursor_ObjCMessageExpr) {
848 if (cursor.xdata != -1)
849 return std::make_pair(cursor.xdata,
850 cast<ObjCMessageExpr>(getCursorExpr(cursor))
851 ->getSelectorLoc(cursor.xdata));
852 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
853 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
854 if (cursor.xdata != -1)
855 return std::make_pair(cursor.xdata,
856 cast<ObjCMethodDecl>(getCursorDecl(cursor))
857 ->getSelectorLoc(cursor.xdata));
858 }
859
860 return std::make_pair(-1, SourceLocation());
861}
862
863CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
864 CXCursor newCursor = cursor;
865
866 if (cursor.kind == CXCursor_ObjCMessageExpr) {
867 if (SelIdx == -1 ||
868 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
869 ->getNumSelectorLocs())
870 newCursor.xdata = -1;
871 else
872 newCursor.xdata = SelIdx;
873 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
874 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
875 if (SelIdx == -1 ||
876 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
877 ->getNumSelectorLocs())
878 newCursor.xdata = -1;
879 else
880 newCursor.xdata = SelIdx;
881 }
882
883 return newCursor;
884}
885
886CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
887 if (cursor.kind != CXCursor_CallExpr)
888 return cursor;
889
890 if (cursor.xdata == 0)
891 return cursor;
892
893 Expr *E = getCursorExpr(cursor);
894 TypeSourceInfo *Type = 0;
895 if (CXXUnresolvedConstructExpr *
896 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
897 Type = UnCtor->getTypeSourceInfo();
898 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
899 Type = Tmp->getTypeSourceInfo();
900 }
901
902 if (!Type)
903 return cursor;
904
905 CXTranslationUnit TU = getCursorTU(cursor);
906 QualType Ty = Type->getType();
907 TypeLoc TL = Type->getTypeLoc();
908 SourceLocation Loc = TL.getBeginLoc();
909
910 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
911 Ty = ElabT->getNamedType();
912 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
913 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
914 }
915
916 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
917 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
918 if (const TagType *Tag = Ty->getAs<TagType>())
919 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
920 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
921 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
922
923 return cursor;
924}
925
Douglas Gregor283cae32010-01-15 21:56:13 +0000926bool cxcursor::operator==(CXCursor X, CXCursor Y) {
927 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
928 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000929}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000930
931// FIXME: Remove once we can model DeclGroups and their appropriate ranges
932// properly in the ASTs.
933bool cxcursor::isFirstInDeclGroup(CXCursor C) {
934 assert(clang_isDeclaration(C.kind));
935 return ((uintptr_t) (C.data[1])) != 0;
936}
937
Ted Kremenekeca099b2010-12-08 23:43:14 +0000938//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000939// libclang CXCursor APIs
940//===----------------------------------------------------------------------===//
941
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000942extern "C" {
943
944int clang_Cursor_isNull(CXCursor cursor) {
945 return clang_equalCursors(cursor, clang_getNullCursor());
946}
947
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000948CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
949 return getCursorTU(cursor);
950}
951
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000952} // end: extern "C"
953
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000954//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000955// CXCursorSet.
956//===----------------------------------------------------------------------===//
957
958typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
959
960static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
961 return (CXCursorSet) setImpl;
962}
963static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
964 return (CXCursorSet_Impl*) set;
965}
966namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000967template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000968public:
969 static inline CXCursor getEmptyKey() {
970 return MakeCXCursorInvalid(CXCursor_InvalidFile);
971 }
972 static inline CXCursor getTombstoneKey() {
973 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
974 }
975 static inline unsigned getHashValue(const CXCursor &cursor) {
976 return llvm::DenseMapInfo<std::pair<void*,void*> >
977 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
978 }
979 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
980 return x.kind == y.kind &&
981 x.data[0] == y.data[0] &&
982 x.data[1] == y.data[1];
983 }
984};
985}
986
987extern "C" {
988CXCursorSet clang_createCXCursorSet() {
989 return packCXCursorSet(new CXCursorSet_Impl());
990}
991
992void clang_disposeCXCursorSet(CXCursorSet set) {
993 delete unpackCXCursorSet(set);
994}
995
996unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
997 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
998 if (!setImpl)
999 return 0;
1000 return setImpl->find(cursor) == setImpl->end();
1001}
1002
Anders Carlssone8b3de02010-12-09 01:00:12 +00001003unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001004 // Do not insert invalid cursors into the set.
1005 if (cursor.kind >= CXCursor_FirstInvalid &&
1006 cursor.kind <= CXCursor_LastInvalid)
1007 return 1;
1008
1009 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1010 if (!setImpl)
1011 return 1;
1012 unsigned &entry = (*setImpl)[cursor];
1013 unsigned flag = entry == 0 ? 1 : 0;
1014 entry = 1;
1015 return flag;
1016}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001017
1018CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1019 enum CXCursorKind kind = clang_getCursorKind(cursor);
1020 if (clang_isDeclaration(kind)) {
1021 Decl *decl = getCursorDecl(cursor);
Argyrios Kyrtzidis16ed0e62011-12-10 02:36:25 +00001022 if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001023 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001024 CodeCompletionAllocator *Allocator
1025 = unit->getCursorCompletionAllocator().getPtr();
1026 CodeCompletionResult Result(namedDecl);
1027 CodeCompletionString *String
1028 = Result.CreateCodeCompletionString(unit->getASTContext(),
1029 unit->getPreprocessor(),
1030 *Allocator);
1031 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001032 }
1033 }
1034 else if (kind == CXCursor_MacroDefinition) {
1035 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1036 const IdentifierInfo *MacroInfo = definition->getName();
1037 ASTUnit *unit = getCursorASTUnit(cursor);
Argyrios Kyrtzidis5e192a72012-01-17 02:15:54 +00001038 CodeCompletionAllocator *Allocator
1039 = unit->getCursorCompletionAllocator().getPtr();
1040 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
1041 CodeCompletionString *String
1042 = Result.CreateCodeCompletionString(unit->getASTContext(),
1043 unit->getPreprocessor(),
1044 *Allocator);
1045 return String;
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001046 }
1047 return NULL;
1048}
1049
Ted Kremenekeca099b2010-12-08 23:43:14 +00001050} // end: extern "C"