blob: 586d9beba3b9b1b706874748157bae26b388460d [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;
Ted Kremeneke77f4432010-02-18 03:09:07 +000049 }
50
51 return CXCursor_UnexposedAttr;
52}
53
Ted Kremeneka60ed472010-11-16 08:15:36 +000054CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
55 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000056 assert(A && Parent && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000057 CXCursor C = { GetCursorKind(A), 0, { Parent, (void*)A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000058 return C;
59}
60
Ted Kremeneka60ed472010-11-16 08:15:36 +000061CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000062 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000063 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000064 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000065
66 CXCursorKind K = getCursorKindForDecl(D);
67
68 if (K == CXCursor_ObjCClassMethodDecl ||
69 K == CXCursor_ObjCInstanceMethodDecl) {
70 int SelectorIdIndex = -1;
71 // Check if cursor points to a selector id.
72 if (RegionOfInterest.isValid() &&
73 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
74 SmallVector<SourceLocation, 16> SelLocs;
75 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
76 SmallVector<SourceLocation, 16>::iterator
77 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
78 if (I != SelLocs.end())
79 SelectorIdIndex = I - SelLocs.begin();
80 }
81 CXCursor C = { K, SelectorIdIndex,
82 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
83 return C;
84 }
85
86 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000087 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000088}
89
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000090CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU,
91 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000092 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000093 CXCursorKind K = CXCursor_NotImplemented;
94
95 switch (S->getStmtClass()) {
96 case Stmt::NoStmtClass:
97 break;
Douglas Gregor42b29842011-10-05 19:00:14 +000098
Douglas Gregor97b98722010-01-19 23:20:36 +000099 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000100 K = CXCursor_CaseStmt;
101 break;
102
Douglas Gregor97b98722010-01-19 23:20:36 +0000103 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000104 K = CXCursor_DefaultStmt;
105 break;
106
107 case Stmt::IfStmtClass:
108 K = CXCursor_IfStmt;
109 break;
110
111 case Stmt::SwitchStmtClass:
112 K = CXCursor_SwitchStmt;
113 break;
114
115 case Stmt::WhileStmtClass:
116 K = CXCursor_WhileStmt;
117 break;
118
119 case Stmt::DoStmtClass:
120 K = CXCursor_DoStmt;
121 break;
122
123 case Stmt::ForStmtClass:
124 K = CXCursor_ForStmt;
125 break;
126
127 case Stmt::GotoStmtClass:
128 K = CXCursor_GotoStmt;
129 break;
130
Douglas Gregor97b98722010-01-19 23:20:36 +0000131 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000132 K = CXCursor_IndirectGotoStmt;
133 break;
134
135 case Stmt::ContinueStmtClass:
136 K = CXCursor_ContinueStmt;
137 break;
138
139 case Stmt::BreakStmtClass:
140 K = CXCursor_BreakStmt;
141 break;
142
143 case Stmt::ReturnStmtClass:
144 K = CXCursor_ReturnStmt;
145 break;
146
147 case Stmt::AsmStmtClass:
148 K = CXCursor_AsmStmt;
149 break;
150
151 case Stmt::ObjCAtTryStmtClass:
152 K = CXCursor_ObjCAtTryStmt;
153 break;
154
155 case Stmt::ObjCAtCatchStmtClass:
156 K = CXCursor_ObjCAtCatchStmt;
157 break;
158
159 case Stmt::ObjCAtFinallyStmtClass:
160 K = CXCursor_ObjCAtFinallyStmt;
161 break;
162
163 case Stmt::ObjCAtThrowStmtClass:
164 K = CXCursor_ObjCAtThrowStmt;
165 break;
166
167 case Stmt::ObjCAtSynchronizedStmtClass:
168 K = CXCursor_ObjCAtSynchronizedStmt;
169 break;
170
171 case Stmt::ObjCAutoreleasePoolStmtClass:
172 K = CXCursor_ObjCAutoreleasePoolStmt;
173 break;
174
Douglas Gregor97b98722010-01-19 23:20:36 +0000175 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000176 K = CXCursor_ObjCForCollectionStmt;
177 break;
178
Douglas Gregor97b98722010-01-19 23:20:36 +0000179 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000180 K = CXCursor_CXXCatchStmt;
181 break;
182
183 case Stmt::CXXTryStmtClass:
184 K = CXCursor_CXXTryStmt;
185 break;
186
187 case Stmt::CXXForRangeStmtClass:
188 K = CXCursor_CXXForRangeStmt;
189 break;
190
John Wiegley28bbe4b2011-04-28 01:08:34 +0000191 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000192 K = CXCursor_SEHTryStmt;
193 break;
194
John Wiegley28bbe4b2011-04-28 01:08:34 +0000195 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000196 K = CXCursor_SEHExceptStmt;
197 break;
198
John Wiegley28bbe4b2011-04-28 01:08:34 +0000199 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000200 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000201 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000202
John Wiegley21ff2e52011-04-28 00:16:57 +0000203 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000204 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000205 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000206 case Stmt::BinaryConditionalOperatorClass:
207 case Stmt::BinaryTypeTraitExprClass:
208 case Stmt::CXXBindTemporaryExprClass:
209 case Stmt::CXXDefaultArgExprClass:
210 case Stmt::CXXScalarValueInitExprClass:
211 case Stmt::CXXUuidofExprClass:
212 case Stmt::ChooseExprClass:
213 case Stmt::DesignatedInitExprClass:
214 case Stmt::ExprWithCleanupsClass:
215 case Stmt::ExpressionTraitExprClass:
216 case Stmt::ExtVectorElementExprClass:
217 case Stmt::ImplicitCastExprClass:
218 case Stmt::ImplicitValueInitExprClass:
219 case Stmt::MaterializeTemporaryExprClass:
220 case Stmt::ObjCIndirectCopyRestoreExprClass:
221 case Stmt::OffsetOfExprClass:
222 case Stmt::OpaqueValueExprClass:
223 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
232 case Stmt::CompoundStmtClass:
233 K = CXCursor_CompoundStmt;
234 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000235
Douglas Gregor42b29842011-10-05 19:00:14 +0000236 case Stmt::NullStmtClass:
237 K = CXCursor_NullStmt;
238 break;
239
240 case Stmt::LabelStmtClass:
241 K = CXCursor_LabelStmt;
242 break;
243
244 case Stmt::DeclStmtClass:
245 K = CXCursor_DeclStmt;
246 break;
247
248 case Stmt::IntegerLiteralClass:
249 K = CXCursor_IntegerLiteral;
250 break;
251
252 case Stmt::FloatingLiteralClass:
253 K = CXCursor_FloatingLiteral;
254 break;
255
256 case Stmt::ImaginaryLiteralClass:
257 K = CXCursor_ImaginaryLiteral;
258 break;
259
260 case Stmt::StringLiteralClass:
261 K = CXCursor_StringLiteral;
262 break;
263
264 case Stmt::CharacterLiteralClass:
265 K = CXCursor_CharacterLiteral;
266 break;
267
268 case Stmt::ParenExprClass:
269 K = CXCursor_ParenExpr;
270 break;
271
272 case Stmt::UnaryOperatorClass:
273 K = CXCursor_UnaryOperator;
274 break;
275
276 case Stmt::CXXNoexceptExprClass:
277 K = CXCursor_UnaryExpr;
278 break;
279
280 case Stmt::ArraySubscriptExprClass:
281 K = CXCursor_ArraySubscriptExpr;
282 break;
283
284 case Stmt::BinaryOperatorClass:
285 K = CXCursor_BinaryOperator;
286 break;
287
288 case Stmt::CompoundAssignOperatorClass:
289 K = CXCursor_CompoundAssignOperator;
290 break;
291
292 case Stmt::ConditionalOperatorClass:
293 K = CXCursor_ConditionalOperator;
294 break;
295
296 case Stmt::CStyleCastExprClass:
297 K = CXCursor_CStyleCastExpr;
298 break;
299
300 case Stmt::CompoundLiteralExprClass:
301 K = CXCursor_CompoundLiteralExpr;
302 break;
303
304 case Stmt::InitListExprClass:
305 K = CXCursor_InitListExpr;
306 break;
307
308 case Stmt::AddrLabelExprClass:
309 K = CXCursor_AddrLabelExpr;
310 break;
311
312 case Stmt::StmtExprClass:
313 K = CXCursor_StmtExpr;
314 break;
315
316 case Stmt::GenericSelectionExprClass:
317 K = CXCursor_GenericSelectionExpr;
318 break;
319
320 case Stmt::GNUNullExprClass:
321 K = CXCursor_GNUNullExpr;
322 break;
323
324 case Stmt::CXXStaticCastExprClass:
325 K = CXCursor_CXXStaticCastExpr;
326 break;
327
328 case Stmt::CXXDynamicCastExprClass:
329 K = CXCursor_CXXDynamicCastExpr;
330 break;
331
332 case Stmt::CXXReinterpretCastExprClass:
333 K = CXCursor_CXXReinterpretCastExpr;
334 break;
335
336 case Stmt::CXXConstCastExprClass:
337 K = CXCursor_CXXConstCastExpr;
338 break;
339
340 case Stmt::CXXFunctionalCastExprClass:
341 K = CXCursor_CXXFunctionalCastExpr;
342 break;
343
344 case Stmt::CXXTypeidExprClass:
345 K = CXCursor_CXXTypeidExpr;
346 break;
347
348 case Stmt::CXXBoolLiteralExprClass:
349 K = CXCursor_CXXBoolLiteralExpr;
350 break;
351
352 case Stmt::CXXNullPtrLiteralExprClass:
353 K = CXCursor_CXXNullPtrLiteralExpr;
354 break;
355
356 case Stmt::CXXThisExprClass:
357 K = CXCursor_CXXThisExpr;
358 break;
359
360 case Stmt::CXXThrowExprClass:
361 K = CXCursor_CXXThrowExpr;
362 break;
363
364 case Stmt::CXXNewExprClass:
365 K = CXCursor_CXXNewExpr;
366 break;
367
368 case Stmt::CXXDeleteExprClass:
369 K = CXCursor_CXXDeleteExpr;
370 break;
371
372 case Stmt::ObjCStringLiteralClass:
373 K = CXCursor_ObjCStringLiteral;
374 break;
375
376 case Stmt::ObjCEncodeExprClass:
377 K = CXCursor_ObjCEncodeExpr;
378 break;
379
380 case Stmt::ObjCSelectorExprClass:
381 K = CXCursor_ObjCSelectorExpr;
382 break;
383
384 case Stmt::ObjCProtocolExprClass:
385 K = CXCursor_ObjCProtocolExpr;
386 break;
387
388 case Stmt::ObjCBridgedCastExprClass:
389 K = CXCursor_ObjCBridgedCastExpr;
390 break;
391
392 case Stmt::BlockExprClass:
393 K = CXCursor_BlockExpr;
394 break;
395
396 case Stmt::PackExpansionExprClass:
397 K = CXCursor_PackExpansionExpr;
398 break;
399
400 case Stmt::SizeOfPackExprClass:
401 K = CXCursor_SizeOfPackExpr;
402 break;
403
Douglas Gregor97b98722010-01-19 23:20:36 +0000404 case Stmt::BlockDeclRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000405 case Stmt::DeclRefExprClass:
406 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000407 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000408 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000409 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000410 K = CXCursor_DeclRefExpr;
411 break;
412
Douglas Gregor42b29842011-10-05 19:00:14 +0000413 case Stmt::CXXDependentScopeMemberExprClass:
414 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000415 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000416 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000417 case Stmt::ObjCIvarRefExprClass:
418 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000419 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000420 K = CXCursor_MemberRefExpr;
421 break;
422
423 case Stmt::CallExprClass:
424 case Stmt::CXXOperatorCallExprClass:
425 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000426 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000427 case Stmt::CXXConstructExprClass:
428 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000429 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000430 K = CXCursor_CallExpr;
431 break;
432
433 case Stmt::ObjCMessageExprClass:
434 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000435 int SelectorIdIndex = -1;
436 // Check if cursor points to a selector id.
437 if (RegionOfInterest.isValid() &&
438 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
439 SmallVector<SourceLocation, 16> SelLocs;
440 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
441 SmallVector<SourceLocation, 16>::iterator
442 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
443 if (I != SelLocs.end())
444 SelectorIdIndex = I - SelLocs.begin();
445 }
446 CXCursor C = { K, 0, { Parent, S, TU } };
447 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000448 }
449
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000450 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000451 return C;
452}
453
Douglas Gregor2e331b92010-01-16 14:00:32 +0000454CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000455 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000456 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000457 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000458 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000459 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000460 return C;
461}
462
463std::pair<ObjCInterfaceDecl *, SourceLocation>
464cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
465 assert(C.kind == CXCursor_ObjCSuperClassRef);
466 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
467 SourceLocation::getFromRawEncoding(
468 reinterpret_cast<uintptr_t>(C.data[1])));
469}
470
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000471CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000472 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000473 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000474 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000475 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000476 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000477 return C;
478}
479
480std::pair<ObjCProtocolDecl *, SourceLocation>
481cxcursor::getCursorObjCProtocolRef(CXCursor C) {
482 assert(C.kind == CXCursor_ObjCProtocolRef);
483 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
484 SourceLocation::getFromRawEncoding(
485 reinterpret_cast<uintptr_t>(C.data[1])));
486}
487
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000488CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000489 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000490 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000491 // 'Class' can be null for invalid code.
492 if (!Class)
493 return MakeCXCursorInvalid(CXCursor_InvalidCode);
494 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000495 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000496 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000497 return C;
498}
499
500std::pair<ObjCInterfaceDecl *, SourceLocation>
501cxcursor::getCursorObjCClassRef(CXCursor C) {
502 assert(C.kind == CXCursor_ObjCClassRef);
503 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
504 SourceLocation::getFromRawEncoding(
505 reinterpret_cast<uintptr_t>(C.data[1])));
506}
507
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000508CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000509 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000510 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000511 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000512 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000513 return C;
514}
515
516std::pair<TypeDecl *, SourceLocation>
517cxcursor::getCursorTypeRef(CXCursor C) {
518 assert(C.kind == CXCursor_TypeRef);
519 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
520 SourceLocation::getFromRawEncoding(
521 reinterpret_cast<uintptr_t>(C.data[1])));
522}
523
Douglas Gregor0b36e612010-08-31 20:37:03 +0000524CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000525 SourceLocation Loc,
526 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000527 assert(Template && TU && "Invalid arguments!");
528 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000529 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000530 return C;
531}
532
533std::pair<TemplateDecl *, SourceLocation>
534cxcursor::getCursorTemplateRef(CXCursor C) {
535 assert(C.kind == CXCursor_TemplateRef);
536 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
537 SourceLocation::getFromRawEncoding(
538 reinterpret_cast<uintptr_t>(C.data[1])));
539}
540
Douglas Gregor69319002010-08-31 23:48:11 +0000541CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000542 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000543
544 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
545 "Invalid arguments!");
546 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000547 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000548 return C;
549}
550
551std::pair<NamedDecl *, SourceLocation>
552cxcursor::getCursorNamespaceRef(CXCursor C) {
553 assert(C.kind == CXCursor_NamespaceRef);
554 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
555 SourceLocation::getFromRawEncoding(
556 reinterpret_cast<uintptr_t>(C.data[1])));
557}
558
Douglas Gregora67e03f2010-09-09 21:42:20 +0000559CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000560 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000561
562 assert(Field && TU && "Invalid arguments!");
563 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000564 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000565 return C;
566}
567
568std::pair<FieldDecl *, SourceLocation>
569cxcursor::getCursorMemberRef(CXCursor C) {
570 assert(C.kind == CXCursor_MemberRef);
571 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
572 SourceLocation::getFromRawEncoding(
573 reinterpret_cast<uintptr_t>(C.data[1])));
574}
575
Ted Kremeneka60ed472010-11-16 08:15:36 +0000576CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
577 CXTranslationUnit TU){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000578 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000579 return C;
580}
581
582CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
583 assert(C.kind == CXCursor_CXXBaseSpecifier);
584 return static_cast<CXXBaseSpecifier*>(C.data[0]);
585}
586
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000587CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000588 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000589 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000590 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
591 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
592 TU }
593 };
594 return C;
595}
596
597SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
598 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000599 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000600 reinterpret_cast<uintptr_t> (C.data[0])),
601 SourceLocation::getFromRawEncoding(
602 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000603 ASTUnit *TU = getCursorASTUnit(C);
604 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000605}
606
Ted Kremeneka60ed472010-11-16 08:15:36 +0000607CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
608 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000609 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000610 return C;
611}
612
613MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
614 assert(C.kind == CXCursor_MacroDefinition);
615 return static_cast<MacroDefinition *>(C.data[0]);
616}
617
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000618CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
619 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000620 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000621 return C;
622}
623
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000624MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000625 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000626 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000627}
628
Douglas Gregorecdcb882010-10-20 22:00:55 +0000629CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000630 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000631 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000632 return C;
633}
634
635InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
636 assert(C.kind == CXCursor_InclusionDirective);
637 return static_cast<InclusionDirective *>(C.data[0]);
638}
639
Douglas Gregor36897b02010-09-10 00:22:18 +0000640CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000641 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000642
643 assert(Label && TU && "Invalid arguments!");
644 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000645 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000646 return C;
647}
648
649std::pair<LabelStmt*, SourceLocation>
650cxcursor::getCursorLabelRef(CXCursor C) {
651 assert(C.kind == CXCursor_LabelRef);
652 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
653 SourceLocation::getFromRawEncoding(
654 reinterpret_cast<uintptr_t>(C.data[1])));
655}
656
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000657CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000658 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000659 assert(E && TU && "Invalid arguments!");
660 OverloadedDeclRefStorage Storage(E);
661 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
662 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000663 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000664 { Storage.getOpaqueValue(), RawLoc, TU }
665 };
666 return C;
667}
668
669CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
670 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000671 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000672 assert(D && TU && "Invalid arguments!");
673 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
674 OverloadedDeclRefStorage Storage(D);
675 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000676 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000677 { Storage.getOpaqueValue(), RawLoc, TU }
678 };
679 return C;
680}
681
682CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
683 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000684 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000685 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
686 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
687 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
688 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000689 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000690 { Storage.getOpaqueValue(), RawLoc, TU }
691 };
692 return C;
693}
694
695std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
696cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
697 assert(C.kind == CXCursor_OverloadedDeclRef);
698 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
699 SourceLocation::getFromRawEncoding(
700 reinterpret_cast<uintptr_t>(C.data[1])));
701}
702
Douglas Gregor283cae32010-01-15 21:56:13 +0000703Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
704 return (Decl *)Cursor.data[0];
705}
706
707Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
708 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
709}
710
711Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000712 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000713 Cursor.kind == CXCursor_ObjCProtocolRef ||
714 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000715 return 0;
716
Douglas Gregor283cae32010-01-15 21:56:13 +0000717 return (Stmt *)Cursor.data[1];
718}
719
Ted Kremenek95f33552010-08-26 01:42:22 +0000720Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
721 return (Attr *)Cursor.data[1];
722}
723
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000724Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
725 return (Decl *)Cursor.data[0];
726}
727
Douglas Gregorf46034a2010-01-18 23:41:10 +0000728ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000729 return getCursorASTUnit(Cursor)->getASTContext();
730}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000731
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000732ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000733 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
734 ->TUData);
735}
736
737CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
738 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000739}
740
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000741static void CollectOverriddenMethods(CXTranslationUnit TU,
742 DeclContext *Ctx,
743 ObjCMethodDecl *Method,
744 SmallVectorImpl<CXCursor> &Methods) {
745 if (!Ctx)
746 return;
747
748 // If we have a class or category implementation, jump straight to the
749 // interface.
750 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
751 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
752 Method, Methods);
753
754 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
755 if (!Container)
756 return;
757
758 // Check whether we have a matching method at this level.
759 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
760 Method->isInstanceMethod()))
761 if (Method != Overridden) {
762 // We found an override at this level; there is no need to look
763 // into other protocols or categories.
764 Methods.push_back(MakeCXCursor(Overridden, TU));
765 return;
766 }
767
768 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
769 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
770 PEnd = Protocol->protocol_end();
771 P != PEnd; ++P)
772 CollectOverriddenMethods(TU, *P, Method, Methods);
773 }
774
775 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
776 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
777 PEnd = Category->protocol_end();
778 P != PEnd; ++P)
779 CollectOverriddenMethods(TU, *P, Method, Methods);
780 }
781
782 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
783 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
784 PEnd = Interface->protocol_end();
785 P != PEnd; ++P)
786 CollectOverriddenMethods(TU, *P, Method, Methods);
787
788 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
789 Category; Category = Category->getNextClassCategory())
790 CollectOverriddenMethods(TU, Category, Method, Methods);
791
792 // We only look into the superclass if we haven't found anything yet.
793 if (Methods.empty())
794 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
795 return CollectOverriddenMethods(TU, Super, Method, Methods);
796 }
797}
798
799void cxcursor::getOverriddenCursors(CXCursor cursor,
800 SmallVectorImpl<CXCursor> &overridden) {
801 if (!clang_isDeclaration(cursor.kind))
802 return;
803
804 Decl *D = getCursorDecl(cursor);
805 if (!D)
806 return;
807
808 // Handle C++ member functions.
809 CXTranslationUnit TU = getCursorTU(cursor);
810 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
811 for (CXXMethodDecl::method_iterator
812 M = CXXMethod->begin_overridden_methods(),
813 MEnd = CXXMethod->end_overridden_methods();
814 M != MEnd; ++M)
815 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
816 return;
817 }
818
819 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
820 if (!Method)
821 return;
822
823 // Handle Objective-C methods.
824 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
825}
826
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000827std::pair<int, SourceLocation>
828cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
829 if (cursor.kind == CXCursor_ObjCMessageExpr) {
830 if (cursor.xdata != -1)
831 return std::make_pair(cursor.xdata,
832 cast<ObjCMessageExpr>(getCursorExpr(cursor))
833 ->getSelectorLoc(cursor.xdata));
834 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
835 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
836 if (cursor.xdata != -1)
837 return std::make_pair(cursor.xdata,
838 cast<ObjCMethodDecl>(getCursorDecl(cursor))
839 ->getSelectorLoc(cursor.xdata));
840 }
841
842 return std::make_pair(-1, SourceLocation());
843}
844
845CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
846 CXCursor newCursor = cursor;
847
848 if (cursor.kind == CXCursor_ObjCMessageExpr) {
849 if (SelIdx == -1 ||
850 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
851 ->getNumSelectorLocs())
852 newCursor.xdata = -1;
853 else
854 newCursor.xdata = SelIdx;
855 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
856 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
857 if (SelIdx == -1 ||
858 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
859 ->getNumSelectorLocs())
860 newCursor.xdata = -1;
861 else
862 newCursor.xdata = SelIdx;
863 }
864
865 return newCursor;
866}
867
868CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
869 if (cursor.kind != CXCursor_CallExpr)
870 return cursor;
871
872 if (cursor.xdata == 0)
873 return cursor;
874
875 Expr *E = getCursorExpr(cursor);
876 TypeSourceInfo *Type = 0;
877 if (CXXUnresolvedConstructExpr *
878 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
879 Type = UnCtor->getTypeSourceInfo();
880 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
881 Type = Tmp->getTypeSourceInfo();
882 }
883
884 if (!Type)
885 return cursor;
886
887 CXTranslationUnit TU = getCursorTU(cursor);
888 QualType Ty = Type->getType();
889 TypeLoc TL = Type->getTypeLoc();
890 SourceLocation Loc = TL.getBeginLoc();
891
892 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
893 Ty = ElabT->getNamedType();
894 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
895 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
896 }
897
898 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
899 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
900 if (const TagType *Tag = Ty->getAs<TagType>())
901 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
902 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
903 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
904
905 return cursor;
906}
907
Douglas Gregor283cae32010-01-15 21:56:13 +0000908bool cxcursor::operator==(CXCursor X, CXCursor Y) {
909 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
910 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000911}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000912
913// FIXME: Remove once we can model DeclGroups and their appropriate ranges
914// properly in the ASTs.
915bool cxcursor::isFirstInDeclGroup(CXCursor C) {
916 assert(clang_isDeclaration(C.kind));
917 return ((uintptr_t) (C.data[1])) != 0;
918}
919
Ted Kremenekeca099b2010-12-08 23:43:14 +0000920//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000921// libclang CXCursor APIs
922//===----------------------------------------------------------------------===//
923
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000924extern "C" {
925
926int clang_Cursor_isNull(CXCursor cursor) {
927 return clang_equalCursors(cursor, clang_getNullCursor());
928}
929
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000930CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
931 return getCursorTU(cursor);
932}
933
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000934} // end: extern "C"
935
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000936//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000937// CXCursorSet.
938//===----------------------------------------------------------------------===//
939
940typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
941
942static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
943 return (CXCursorSet) setImpl;
944}
945static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
946 return (CXCursorSet_Impl*) set;
947}
948namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000949template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000950public:
951 static inline CXCursor getEmptyKey() {
952 return MakeCXCursorInvalid(CXCursor_InvalidFile);
953 }
954 static inline CXCursor getTombstoneKey() {
955 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
956 }
957 static inline unsigned getHashValue(const CXCursor &cursor) {
958 return llvm::DenseMapInfo<std::pair<void*,void*> >
959 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
960 }
961 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
962 return x.kind == y.kind &&
963 x.data[0] == y.data[0] &&
964 x.data[1] == y.data[1];
965 }
966};
967}
968
969extern "C" {
970CXCursorSet clang_createCXCursorSet() {
971 return packCXCursorSet(new CXCursorSet_Impl());
972}
973
974void clang_disposeCXCursorSet(CXCursorSet set) {
975 delete unpackCXCursorSet(set);
976}
977
978unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
979 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
980 if (!setImpl)
981 return 0;
982 return setImpl->find(cursor) == setImpl->end();
983}
984
Anders Carlssone8b3de02010-12-09 01:00:12 +0000985unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000986 // Do not insert invalid cursors into the set.
987 if (cursor.kind >= CXCursor_FirstInvalid &&
988 cursor.kind <= CXCursor_LastInvalid)
989 return 1;
990
991 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
992 if (!setImpl)
993 return 1;
994 unsigned &entry = (*setImpl)[cursor];
995 unsigned flag = entry == 0 ? 1 : 0;
996 entry = 1;
997 return flag;
998}
Douglas Gregor8fa0a802011-08-04 20:04:59 +0000999
1000CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1001 enum CXCursorKind kind = clang_getCursorKind(cursor);
1002 if (clang_isDeclaration(kind)) {
1003 Decl *decl = getCursorDecl(cursor);
1004 if (isa<NamedDecl>(decl)) {
1005 NamedDecl *namedDecl = (NamedDecl *)decl;
1006 ASTUnit *unit = getCursorASTUnit(cursor);
1007 if (unit->hasSema()) {
1008 Sema &S = unit->getSema();
1009 CodeCompletionAllocator *Allocator
1010 = unit->getCursorCompletionAllocator().getPtr();
1011 CodeCompletionResult Result(namedDecl);
1012 CodeCompletionString *String
1013 = Result.CreateCodeCompletionString(S, *Allocator);
1014 return String;
1015 }
1016 }
1017 }
1018 else if (kind == CXCursor_MacroDefinition) {
1019 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1020 const IdentifierInfo *MacroInfo = definition->getName();
1021 ASTUnit *unit = getCursorASTUnit(cursor);
1022 if (unit->hasSema()) {
1023 Sema &S = unit->getSema();
1024 CodeCompletionAllocator *Allocator
1025 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +00001026 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001027 CodeCompletionString *String
1028 = Result.CreateCodeCompletionString(S, *Allocator);
1029 return String;
1030 }
1031 }
1032 return NULL;
1033}
1034
Ted Kremenekeca099b2010-12-08 23:43:14 +00001035} // end: extern "C"