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