blob: ae3aeea06c744de249b00c354e9115fc1192af49 [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
Douglas Gregorba0513d2011-10-25 01:33:02 +0000433 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000434 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 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000449
450 case Stmt::MSDependentExistsStmtClass:
451 K = CXCursor_UnexposedStmt;
452 break;
453 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000454
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000455 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000456 return C;
457}
458
Douglas Gregor2e331b92010-01-16 14:00:32 +0000459CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000460 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000461 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000462 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000463 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000464 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000465 return C;
466}
467
468std::pair<ObjCInterfaceDecl *, SourceLocation>
469cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
470 assert(C.kind == CXCursor_ObjCSuperClassRef);
471 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
472 SourceLocation::getFromRawEncoding(
473 reinterpret_cast<uintptr_t>(C.data[1])));
474}
475
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000476CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000477 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000478 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000479 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000480 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000481 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000482 return C;
483}
484
485std::pair<ObjCProtocolDecl *, SourceLocation>
486cxcursor::getCursorObjCProtocolRef(CXCursor C) {
487 assert(C.kind == CXCursor_ObjCProtocolRef);
488 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
489 SourceLocation::getFromRawEncoding(
490 reinterpret_cast<uintptr_t>(C.data[1])));
491}
492
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000493CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000494 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000495 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000496 // 'Class' can be null for invalid code.
497 if (!Class)
498 return MakeCXCursorInvalid(CXCursor_InvalidCode);
499 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000500 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000501 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000502 return C;
503}
504
505std::pair<ObjCInterfaceDecl *, SourceLocation>
506cxcursor::getCursorObjCClassRef(CXCursor C) {
507 assert(C.kind == CXCursor_ObjCClassRef);
508 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
509 SourceLocation::getFromRawEncoding(
510 reinterpret_cast<uintptr_t>(C.data[1])));
511}
512
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000513CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000514 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000515 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000516 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000517 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000518 return C;
519}
520
521std::pair<TypeDecl *, SourceLocation>
522cxcursor::getCursorTypeRef(CXCursor C) {
523 assert(C.kind == CXCursor_TypeRef);
524 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
525 SourceLocation::getFromRawEncoding(
526 reinterpret_cast<uintptr_t>(C.data[1])));
527}
528
Douglas Gregor0b36e612010-08-31 20:37:03 +0000529CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000530 SourceLocation Loc,
531 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000532 assert(Template && TU && "Invalid arguments!");
533 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000534 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000535 return C;
536}
537
538std::pair<TemplateDecl *, SourceLocation>
539cxcursor::getCursorTemplateRef(CXCursor C) {
540 assert(C.kind == CXCursor_TemplateRef);
541 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
542 SourceLocation::getFromRawEncoding(
543 reinterpret_cast<uintptr_t>(C.data[1])));
544}
545
Douglas Gregor69319002010-08-31 23:48:11 +0000546CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000547 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000548
549 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
550 "Invalid arguments!");
551 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000552 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000553 return C;
554}
555
556std::pair<NamedDecl *, SourceLocation>
557cxcursor::getCursorNamespaceRef(CXCursor C) {
558 assert(C.kind == CXCursor_NamespaceRef);
559 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
560 SourceLocation::getFromRawEncoding(
561 reinterpret_cast<uintptr_t>(C.data[1])));
562}
563
Douglas Gregora67e03f2010-09-09 21:42:20 +0000564CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000565 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000566
567 assert(Field && TU && "Invalid arguments!");
568 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000569 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000570 return C;
571}
572
573std::pair<FieldDecl *, SourceLocation>
574cxcursor::getCursorMemberRef(CXCursor C) {
575 assert(C.kind == CXCursor_MemberRef);
576 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
577 SourceLocation::getFromRawEncoding(
578 reinterpret_cast<uintptr_t>(C.data[1])));
579}
580
Ted Kremeneka60ed472010-11-16 08:15:36 +0000581CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
582 CXTranslationUnit TU){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000583 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000584 return C;
585}
586
587CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
588 assert(C.kind == CXCursor_CXXBaseSpecifier);
589 return static_cast<CXXBaseSpecifier*>(C.data[0]);
590}
591
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000592CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000593 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000594 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000595 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
596 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
597 TU }
598 };
599 return C;
600}
601
602SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
603 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000604 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000605 reinterpret_cast<uintptr_t> (C.data[0])),
606 SourceLocation::getFromRawEncoding(
607 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000608 ASTUnit *TU = getCursorASTUnit(C);
609 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000610}
611
Ted Kremeneka60ed472010-11-16 08:15:36 +0000612CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
613 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000614 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000615 return C;
616}
617
618MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
619 assert(C.kind == CXCursor_MacroDefinition);
620 return static_cast<MacroDefinition *>(C.data[0]);
621}
622
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000623CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
624 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000625 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000626 return C;
627}
628
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000629MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000630 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000631 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000632}
633
Douglas Gregorecdcb882010-10-20 22:00:55 +0000634CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000635 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000636 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000637 return C;
638}
639
640InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
641 assert(C.kind == CXCursor_InclusionDirective);
642 return static_cast<InclusionDirective *>(C.data[0]);
643}
644
Douglas Gregor36897b02010-09-10 00:22:18 +0000645CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000646 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000647
648 assert(Label && TU && "Invalid arguments!");
649 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000650 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000651 return C;
652}
653
654std::pair<LabelStmt*, SourceLocation>
655cxcursor::getCursorLabelRef(CXCursor C) {
656 assert(C.kind == CXCursor_LabelRef);
657 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
658 SourceLocation::getFromRawEncoding(
659 reinterpret_cast<uintptr_t>(C.data[1])));
660}
661
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000662CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000663 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000664 assert(E && TU && "Invalid arguments!");
665 OverloadedDeclRefStorage Storage(E);
666 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
667 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000668 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000669 { Storage.getOpaqueValue(), RawLoc, TU }
670 };
671 return C;
672}
673
674CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
675 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000676 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000677 assert(D && TU && "Invalid arguments!");
678 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
679 OverloadedDeclRefStorage Storage(D);
680 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000681 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000682 { Storage.getOpaqueValue(), RawLoc, TU }
683 };
684 return C;
685}
686
687CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
688 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000689 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000690 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
691 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
692 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
693 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000694 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000695 { Storage.getOpaqueValue(), RawLoc, TU }
696 };
697 return C;
698}
699
700std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
701cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
702 assert(C.kind == CXCursor_OverloadedDeclRef);
703 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
704 SourceLocation::getFromRawEncoding(
705 reinterpret_cast<uintptr_t>(C.data[1])));
706}
707
Douglas Gregor283cae32010-01-15 21:56:13 +0000708Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
709 return (Decl *)Cursor.data[0];
710}
711
712Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
713 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
714}
715
716Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000717 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000718 Cursor.kind == CXCursor_ObjCProtocolRef ||
719 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000720 return 0;
721
Douglas Gregor283cae32010-01-15 21:56:13 +0000722 return (Stmt *)Cursor.data[1];
723}
724
Ted Kremenek95f33552010-08-26 01:42:22 +0000725Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
726 return (Attr *)Cursor.data[1];
727}
728
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000729Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
730 return (Decl *)Cursor.data[0];
731}
732
Douglas Gregorf46034a2010-01-18 23:41:10 +0000733ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000734 return getCursorASTUnit(Cursor)->getASTContext();
735}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000736
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000737ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000738 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
739 ->TUData);
740}
741
742CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
743 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000744}
745
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000746static void CollectOverriddenMethods(CXTranslationUnit TU,
747 DeclContext *Ctx,
748 ObjCMethodDecl *Method,
749 SmallVectorImpl<CXCursor> &Methods) {
750 if (!Ctx)
751 return;
752
753 // If we have a class or category implementation, jump straight to the
754 // interface.
755 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
756 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
757 Method, Methods);
758
759 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
760 if (!Container)
761 return;
762
763 // Check whether we have a matching method at this level.
764 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
765 Method->isInstanceMethod()))
766 if (Method != Overridden) {
767 // We found an override at this level; there is no need to look
768 // into other protocols or categories.
769 Methods.push_back(MakeCXCursor(Overridden, TU));
770 return;
771 }
772
773 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
774 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
775 PEnd = Protocol->protocol_end();
776 P != PEnd; ++P)
777 CollectOverriddenMethods(TU, *P, Method, Methods);
778 }
779
780 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
781 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
782 PEnd = Category->protocol_end();
783 P != PEnd; ++P)
784 CollectOverriddenMethods(TU, *P, Method, Methods);
785 }
786
787 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
788 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
789 PEnd = Interface->protocol_end();
790 P != PEnd; ++P)
791 CollectOverriddenMethods(TU, *P, Method, Methods);
792
793 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
794 Category; Category = Category->getNextClassCategory())
795 CollectOverriddenMethods(TU, Category, Method, Methods);
796
797 // We only look into the superclass if we haven't found anything yet.
798 if (Methods.empty())
799 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
800 return CollectOverriddenMethods(TU, Super, Method, Methods);
801 }
802}
803
804void cxcursor::getOverriddenCursors(CXCursor cursor,
805 SmallVectorImpl<CXCursor> &overridden) {
806 if (!clang_isDeclaration(cursor.kind))
807 return;
808
809 Decl *D = getCursorDecl(cursor);
810 if (!D)
811 return;
812
813 // Handle C++ member functions.
814 CXTranslationUnit TU = getCursorTU(cursor);
815 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
816 for (CXXMethodDecl::method_iterator
817 M = CXXMethod->begin_overridden_methods(),
818 MEnd = CXXMethod->end_overridden_methods();
819 M != MEnd; ++M)
820 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
821 return;
822 }
823
824 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
825 if (!Method)
826 return;
827
828 // Handle Objective-C methods.
829 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
830}
831
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000832std::pair<int, SourceLocation>
833cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
834 if (cursor.kind == CXCursor_ObjCMessageExpr) {
835 if (cursor.xdata != -1)
836 return std::make_pair(cursor.xdata,
837 cast<ObjCMessageExpr>(getCursorExpr(cursor))
838 ->getSelectorLoc(cursor.xdata));
839 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
840 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
841 if (cursor.xdata != -1)
842 return std::make_pair(cursor.xdata,
843 cast<ObjCMethodDecl>(getCursorDecl(cursor))
844 ->getSelectorLoc(cursor.xdata));
845 }
846
847 return std::make_pair(-1, SourceLocation());
848}
849
850CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
851 CXCursor newCursor = cursor;
852
853 if (cursor.kind == CXCursor_ObjCMessageExpr) {
854 if (SelIdx == -1 ||
855 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
856 ->getNumSelectorLocs())
857 newCursor.xdata = -1;
858 else
859 newCursor.xdata = SelIdx;
860 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
861 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
862 if (SelIdx == -1 ||
863 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
864 ->getNumSelectorLocs())
865 newCursor.xdata = -1;
866 else
867 newCursor.xdata = SelIdx;
868 }
869
870 return newCursor;
871}
872
873CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
874 if (cursor.kind != CXCursor_CallExpr)
875 return cursor;
876
877 if (cursor.xdata == 0)
878 return cursor;
879
880 Expr *E = getCursorExpr(cursor);
881 TypeSourceInfo *Type = 0;
882 if (CXXUnresolvedConstructExpr *
883 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
884 Type = UnCtor->getTypeSourceInfo();
885 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
886 Type = Tmp->getTypeSourceInfo();
887 }
888
889 if (!Type)
890 return cursor;
891
892 CXTranslationUnit TU = getCursorTU(cursor);
893 QualType Ty = Type->getType();
894 TypeLoc TL = Type->getTypeLoc();
895 SourceLocation Loc = TL.getBeginLoc();
896
897 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
898 Ty = ElabT->getNamedType();
899 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
900 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
901 }
902
903 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
904 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
905 if (const TagType *Tag = Ty->getAs<TagType>())
906 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
907 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
908 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
909
910 return cursor;
911}
912
Douglas Gregor283cae32010-01-15 21:56:13 +0000913bool cxcursor::operator==(CXCursor X, CXCursor Y) {
914 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
915 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000916}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000917
918// FIXME: Remove once we can model DeclGroups and their appropriate ranges
919// properly in the ASTs.
920bool cxcursor::isFirstInDeclGroup(CXCursor C) {
921 assert(clang_isDeclaration(C.kind));
922 return ((uintptr_t) (C.data[1])) != 0;
923}
924
Ted Kremenekeca099b2010-12-08 23:43:14 +0000925//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000926// libclang CXCursor APIs
927//===----------------------------------------------------------------------===//
928
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000929extern "C" {
930
931int clang_Cursor_isNull(CXCursor cursor) {
932 return clang_equalCursors(cursor, clang_getNullCursor());
933}
934
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000935CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
936 return getCursorTU(cursor);
937}
938
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000939} // end: extern "C"
940
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000941//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000942// CXCursorSet.
943//===----------------------------------------------------------------------===//
944
945typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
946
947static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
948 return (CXCursorSet) setImpl;
949}
950static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
951 return (CXCursorSet_Impl*) set;
952}
953namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000954template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000955public:
956 static inline CXCursor getEmptyKey() {
957 return MakeCXCursorInvalid(CXCursor_InvalidFile);
958 }
959 static inline CXCursor getTombstoneKey() {
960 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
961 }
962 static inline unsigned getHashValue(const CXCursor &cursor) {
963 return llvm::DenseMapInfo<std::pair<void*,void*> >
964 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
965 }
966 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
967 return x.kind == y.kind &&
968 x.data[0] == y.data[0] &&
969 x.data[1] == y.data[1];
970 }
971};
972}
973
974extern "C" {
975CXCursorSet clang_createCXCursorSet() {
976 return packCXCursorSet(new CXCursorSet_Impl());
977}
978
979void clang_disposeCXCursorSet(CXCursorSet set) {
980 delete unpackCXCursorSet(set);
981}
982
983unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
984 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
985 if (!setImpl)
986 return 0;
987 return setImpl->find(cursor) == setImpl->end();
988}
989
Anders Carlssone8b3de02010-12-09 01:00:12 +0000990unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000991 // Do not insert invalid cursors into the set.
992 if (cursor.kind >= CXCursor_FirstInvalid &&
993 cursor.kind <= CXCursor_LastInvalid)
994 return 1;
995
996 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
997 if (!setImpl)
998 return 1;
999 unsigned &entry = (*setImpl)[cursor];
1000 unsigned flag = entry == 0 ? 1 : 0;
1001 entry = 1;
1002 return flag;
1003}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001004
1005CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1006 enum CXCursorKind kind = clang_getCursorKind(cursor);
1007 if (clang_isDeclaration(kind)) {
1008 Decl *decl = getCursorDecl(cursor);
1009 if (isa<NamedDecl>(decl)) {
1010 NamedDecl *namedDecl = (NamedDecl *)decl;
1011 ASTUnit *unit = getCursorASTUnit(cursor);
1012 if (unit->hasSema()) {
1013 Sema &S = unit->getSema();
1014 CodeCompletionAllocator *Allocator
1015 = unit->getCursorCompletionAllocator().getPtr();
1016 CodeCompletionResult Result(namedDecl);
1017 CodeCompletionString *String
1018 = Result.CreateCodeCompletionString(S, *Allocator);
1019 return String;
1020 }
1021 }
1022 }
1023 else if (kind == CXCursor_MacroDefinition) {
1024 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1025 const IdentifierInfo *MacroInfo = definition->getName();
1026 ASTUnit *unit = getCursorASTUnit(cursor);
1027 if (unit->hasSema()) {
1028 Sema &S = unit->getSema();
1029 CodeCompletionAllocator *Allocator
1030 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +00001031 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001032 CodeCompletionString *String
1033 = Result.CreateCodeCompletionString(S, *Allocator);
1034 return String;
1035 }
1036 }
1037 return NULL;
1038}
1039
Ted Kremenekeca099b2010-12-08 23:43:14 +00001040} // end: extern "C"