blob: b4b9ed9d300387bb5c7d34e29d4fd0deb5998f14 [file] [log] [blame]
Ted Kremenek16c440a2010-01-15 20:35:54 +00001//===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Douglas Gregor2e331b92010-01-16 14:00:32 +000010// This file defines routines for manipulating CXCursors. It should be the
11// only file that has internal knowledge of the encoding of the data in
12// CXCursor.
Ted Kremenek16c440a2010-01-15 20:35:54 +000013//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek0a90d322010-11-17 23:24:11 +000016#include "CXTranslationUnit.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000017#include "CXCursor.h"
Ted Kremeneked122732010-11-16 01:56:27 +000018#include "CXString.h"
Douglas Gregor7eaa8ae2010-01-20 00:23:15 +000019#include "clang/Frontend/ASTUnit.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000020#include "clang/AST/Decl.h"
Douglas Gregor69319002010-08-31 23:48:11 +000021#include "clang/AST/DeclCXX.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000022#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000023#include "clang/AST/DeclTemplate.h"
Douglas Gregor283cae32010-01-15 21:56:13 +000024#include "clang/AST/Expr.h"
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000025#include "clang/AST/ExprCXX.h"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000026#include "clang/AST/ExprObjC.h"
Ted Kremenek007a7c92010-11-01 23:26:51 +000027#include "clang-c/Index.h"
Ted Kremenekedc8aa62010-01-16 00:36:30 +000028#include "llvm/Support/ErrorHandling.h"
Ted Kremenek16c440a2010-01-15 20:35:54 +000029
30using namespace clang;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +000031using namespace cxcursor;
Ted Kremenek16c440a2010-01-15 20:35:54 +000032
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000033CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K) {
34 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000035 CXCursor C = { K, 0, { 0, 0, 0 } };
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000036 return C;
Ted Kremenek16c440a2010-01-15 20:35:54 +000037}
38
Ted Kremeneke77f4432010-02-18 03:09:07 +000039static CXCursorKind GetCursorKind(const Attr *A) {
40 assert(A && "Invalid arguments!");
41 switch (A->getKind()) {
42 default: break;
Sean Hunt387475d2010-06-16 23:43:53 +000043 case attr::IBAction: return CXCursor_IBActionAttr;
44 case attr::IBOutlet: return CXCursor_IBOutletAttr;
45 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
Argyrios Kyrtzidis6639e922011-09-13 17:39:31 +000046 case attr::Final: return CXCursor_CXXFinalAttr;
47 case attr::Override: return CXCursor_CXXOverrideAttr;
Erik Verbruggen5f1c8222011-10-13 09:41:32 +000048 case attr::Annotate: return CXCursor_AnnotateAttr;
Argyrios Kyrtzidis84b79642011-12-06 22:05:01 +000049 case attr::AsmLabel: return CXCursor_AsmLabelAttr;
Ted Kremeneke77f4432010-02-18 03:09:07 +000050 }
51
52 return CXCursor_UnexposedAttr;
53}
54
Ted Kremeneka60ed472010-11-16 08:15:36 +000055CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
56 CXTranslationUnit TU) {
Ted Kremeneke77f4432010-02-18 03:09:07 +000057 assert(A && Parent && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000058 CXCursor C = { GetCursorKind(A), 0, { Parent, (void*)A, TU } };
Ted Kremeneke77f4432010-02-18 03:09:07 +000059 return C;
60}
61
Ted Kremeneka60ed472010-11-16 08:15:36 +000062CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000063 SourceRange RegionOfInterest,
Ted Kremenek007a7c92010-11-01 23:26:51 +000064 bool FirstInDeclGroup) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000065 assert(D && TU && "Invalid arguments!");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000066
67 CXCursorKind K = getCursorKindForDecl(D);
68
69 if (K == CXCursor_ObjCClassMethodDecl ||
70 K == CXCursor_ObjCInstanceMethodDecl) {
71 int SelectorIdIndex = -1;
72 // Check if cursor points to a selector id.
73 if (RegionOfInterest.isValid() &&
74 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
75 SmallVector<SourceLocation, 16> SelLocs;
76 cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
77 SmallVector<SourceLocation, 16>::iterator
78 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
79 if (I != SelLocs.end())
80 SelectorIdIndex = I - SelLocs.begin();
81 }
82 CXCursor C = { K, SelectorIdIndex,
83 { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
84 return C;
85 }
86
87 CXCursor C = { K, 0, { D, (void*)(intptr_t) (FirstInDeclGroup ? 1 : 0), TU }};
Douglas Gregor5bfb8c12010-01-20 23:34:41 +000088 return C;
Ted Kremenekedc8aa62010-01-16 00:36:30 +000089}
90
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +000091CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU,
92 SourceRange RegionOfInterest) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +000093 assert(S && TU && "Invalid arguments!");
Douglas Gregor97b98722010-01-19 23:20:36 +000094 CXCursorKind K = CXCursor_NotImplemented;
95
96 switch (S->getStmtClass()) {
97 case Stmt::NoStmtClass:
98 break;
Douglas Gregor42b29842011-10-05 19:00:14 +000099
Douglas Gregor97b98722010-01-19 23:20:36 +0000100 case Stmt::CaseStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000101 K = CXCursor_CaseStmt;
102 break;
103
Douglas Gregor97b98722010-01-19 23:20:36 +0000104 case Stmt::DefaultStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000105 K = CXCursor_DefaultStmt;
106 break;
107
108 case Stmt::IfStmtClass:
109 K = CXCursor_IfStmt;
110 break;
111
112 case Stmt::SwitchStmtClass:
113 K = CXCursor_SwitchStmt;
114 break;
115
116 case Stmt::WhileStmtClass:
117 K = CXCursor_WhileStmt;
118 break;
119
120 case Stmt::DoStmtClass:
121 K = CXCursor_DoStmt;
122 break;
123
124 case Stmt::ForStmtClass:
125 K = CXCursor_ForStmt;
126 break;
127
128 case Stmt::GotoStmtClass:
129 K = CXCursor_GotoStmt;
130 break;
131
Douglas Gregor97b98722010-01-19 23:20:36 +0000132 case Stmt::IndirectGotoStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000133 K = CXCursor_IndirectGotoStmt;
134 break;
135
136 case Stmt::ContinueStmtClass:
137 K = CXCursor_ContinueStmt;
138 break;
139
140 case Stmt::BreakStmtClass:
141 K = CXCursor_BreakStmt;
142 break;
143
144 case Stmt::ReturnStmtClass:
145 K = CXCursor_ReturnStmt;
146 break;
147
148 case Stmt::AsmStmtClass:
149 K = CXCursor_AsmStmt;
150 break;
151
152 case Stmt::ObjCAtTryStmtClass:
153 K = CXCursor_ObjCAtTryStmt;
154 break;
155
156 case Stmt::ObjCAtCatchStmtClass:
157 K = CXCursor_ObjCAtCatchStmt;
158 break;
159
160 case Stmt::ObjCAtFinallyStmtClass:
161 K = CXCursor_ObjCAtFinallyStmt;
162 break;
163
164 case Stmt::ObjCAtThrowStmtClass:
165 K = CXCursor_ObjCAtThrowStmt;
166 break;
167
168 case Stmt::ObjCAtSynchronizedStmtClass:
169 K = CXCursor_ObjCAtSynchronizedStmt;
170 break;
171
172 case Stmt::ObjCAutoreleasePoolStmtClass:
173 K = CXCursor_ObjCAutoreleasePoolStmt;
174 break;
175
Douglas Gregor97b98722010-01-19 23:20:36 +0000176 case Stmt::ObjCForCollectionStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000177 K = CXCursor_ObjCForCollectionStmt;
178 break;
179
Douglas Gregor97b98722010-01-19 23:20:36 +0000180 case Stmt::CXXCatchStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000181 K = CXCursor_CXXCatchStmt;
182 break;
183
184 case Stmt::CXXTryStmtClass:
185 K = CXCursor_CXXTryStmt;
186 break;
187
188 case Stmt::CXXForRangeStmtClass:
189 K = CXCursor_CXXForRangeStmt;
190 break;
191
John Wiegley28bbe4b2011-04-28 01:08:34 +0000192 case Stmt::SEHTryStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000193 K = CXCursor_SEHTryStmt;
194 break;
195
John Wiegley28bbe4b2011-04-28 01:08:34 +0000196 case Stmt::SEHExceptStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000197 K = CXCursor_SEHExceptStmt;
198 break;
199
John Wiegley28bbe4b2011-04-28 01:08:34 +0000200 case Stmt::SEHFinallyStmtClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000201 K = CXCursor_SEHFinallyStmt;
Douglas Gregor97b98722010-01-19 23:20:36 +0000202 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000203
John Wiegley21ff2e52011-04-28 00:16:57 +0000204 case Stmt::ArrayTypeTraitExprClass:
Tanya Lattner61eee0c2011-06-04 00:47:47 +0000205 case Stmt::AsTypeExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +0000206 case Stmt::AtomicExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000207 case Stmt::BinaryConditionalOperatorClass:
208 case Stmt::BinaryTypeTraitExprClass:
209 case Stmt::CXXBindTemporaryExprClass:
210 case Stmt::CXXDefaultArgExprClass:
211 case Stmt::CXXScalarValueInitExprClass:
212 case Stmt::CXXUuidofExprClass:
213 case Stmt::ChooseExprClass:
214 case Stmt::DesignatedInitExprClass:
215 case Stmt::ExprWithCleanupsClass:
216 case Stmt::ExpressionTraitExprClass:
217 case Stmt::ExtVectorElementExprClass:
218 case Stmt::ImplicitCastExprClass:
219 case Stmt::ImplicitValueInitExprClass:
220 case Stmt::MaterializeTemporaryExprClass:
221 case Stmt::ObjCIndirectCopyRestoreExprClass:
222 case Stmt::OffsetOfExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000223 case Stmt::ParenListExprClass:
224 case Stmt::PredefinedExprClass:
225 case Stmt::ShuffleVectorExprClass:
226 case Stmt::UnaryExprOrTypeTraitExprClass:
227 case Stmt::UnaryTypeTraitExprClass:
228 case Stmt::VAArgExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000229 K = CXCursor_UnexposedExpr;
230 break;
Douglas Gregor42b29842011-10-05 19:00:14 +0000231
John McCall4b9c2d22011-11-06 09:01:30 +0000232 case Stmt::OpaqueValueExprClass:
233 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
234 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
235 K = CXCursor_UnexposedExpr;
236 break;
237
238 case Stmt::PseudoObjectExprClass:
239 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
240 Parent, TU, RegionOfInterest);
241
Douglas Gregor42b29842011-10-05 19:00:14 +0000242 case Stmt::CompoundStmtClass:
243 K = CXCursor_CompoundStmt;
244 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000245
Douglas Gregor42b29842011-10-05 19:00:14 +0000246 case Stmt::NullStmtClass:
247 K = CXCursor_NullStmt;
248 break;
249
250 case Stmt::LabelStmtClass:
251 K = CXCursor_LabelStmt;
252 break;
253
254 case Stmt::DeclStmtClass:
255 K = CXCursor_DeclStmt;
256 break;
257
258 case Stmt::IntegerLiteralClass:
259 K = CXCursor_IntegerLiteral;
260 break;
261
262 case Stmt::FloatingLiteralClass:
263 K = CXCursor_FloatingLiteral;
264 break;
265
266 case Stmt::ImaginaryLiteralClass:
267 K = CXCursor_ImaginaryLiteral;
268 break;
269
270 case Stmt::StringLiteralClass:
271 K = CXCursor_StringLiteral;
272 break;
273
274 case Stmt::CharacterLiteralClass:
275 K = CXCursor_CharacterLiteral;
276 break;
277
278 case Stmt::ParenExprClass:
279 K = CXCursor_ParenExpr;
280 break;
281
282 case Stmt::UnaryOperatorClass:
283 K = CXCursor_UnaryOperator;
284 break;
285
286 case Stmt::CXXNoexceptExprClass:
287 K = CXCursor_UnaryExpr;
288 break;
289
290 case Stmt::ArraySubscriptExprClass:
291 K = CXCursor_ArraySubscriptExpr;
292 break;
293
294 case Stmt::BinaryOperatorClass:
295 K = CXCursor_BinaryOperator;
296 break;
297
298 case Stmt::CompoundAssignOperatorClass:
299 K = CXCursor_CompoundAssignOperator;
300 break;
301
302 case Stmt::ConditionalOperatorClass:
303 K = CXCursor_ConditionalOperator;
304 break;
305
306 case Stmt::CStyleCastExprClass:
307 K = CXCursor_CStyleCastExpr;
308 break;
309
310 case Stmt::CompoundLiteralExprClass:
311 K = CXCursor_CompoundLiteralExpr;
312 break;
313
314 case Stmt::InitListExprClass:
315 K = CXCursor_InitListExpr;
316 break;
317
318 case Stmt::AddrLabelExprClass:
319 K = CXCursor_AddrLabelExpr;
320 break;
321
322 case Stmt::StmtExprClass:
323 K = CXCursor_StmtExpr;
324 break;
325
326 case Stmt::GenericSelectionExprClass:
327 K = CXCursor_GenericSelectionExpr;
328 break;
329
330 case Stmt::GNUNullExprClass:
331 K = CXCursor_GNUNullExpr;
332 break;
333
334 case Stmt::CXXStaticCastExprClass:
335 K = CXCursor_CXXStaticCastExpr;
336 break;
337
338 case Stmt::CXXDynamicCastExprClass:
339 K = CXCursor_CXXDynamicCastExpr;
340 break;
341
342 case Stmt::CXXReinterpretCastExprClass:
343 K = CXCursor_CXXReinterpretCastExpr;
344 break;
345
346 case Stmt::CXXConstCastExprClass:
347 K = CXCursor_CXXConstCastExpr;
348 break;
349
350 case Stmt::CXXFunctionalCastExprClass:
351 K = CXCursor_CXXFunctionalCastExpr;
352 break;
353
354 case Stmt::CXXTypeidExprClass:
355 K = CXCursor_CXXTypeidExpr;
356 break;
357
358 case Stmt::CXXBoolLiteralExprClass:
359 K = CXCursor_CXXBoolLiteralExpr;
360 break;
361
362 case Stmt::CXXNullPtrLiteralExprClass:
363 K = CXCursor_CXXNullPtrLiteralExpr;
364 break;
365
366 case Stmt::CXXThisExprClass:
367 K = CXCursor_CXXThisExpr;
368 break;
369
370 case Stmt::CXXThrowExprClass:
371 K = CXCursor_CXXThrowExpr;
372 break;
373
374 case Stmt::CXXNewExprClass:
375 K = CXCursor_CXXNewExpr;
376 break;
377
378 case Stmt::CXXDeleteExprClass:
379 K = CXCursor_CXXDeleteExpr;
380 break;
381
382 case Stmt::ObjCStringLiteralClass:
383 K = CXCursor_ObjCStringLiteral;
384 break;
385
386 case Stmt::ObjCEncodeExprClass:
387 K = CXCursor_ObjCEncodeExpr;
388 break;
389
390 case Stmt::ObjCSelectorExprClass:
391 K = CXCursor_ObjCSelectorExpr;
392 break;
393
394 case Stmt::ObjCProtocolExprClass:
395 K = CXCursor_ObjCProtocolExpr;
396 break;
397
398 case Stmt::ObjCBridgedCastExprClass:
399 K = CXCursor_ObjCBridgedCastExpr;
400 break;
401
402 case Stmt::BlockExprClass:
403 K = CXCursor_BlockExpr;
404 break;
405
406 case Stmt::PackExpansionExprClass:
407 K = CXCursor_PackExpansionExpr;
408 break;
409
410 case Stmt::SizeOfPackExprClass:
411 K = CXCursor_SizeOfPackExpr;
412 break;
413
Douglas Gregor97b98722010-01-19 23:20:36 +0000414 case Stmt::BlockDeclRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000415 case Stmt::DeclRefExprClass:
416 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000417 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000418 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000419 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000420 K = CXCursor_DeclRefExpr;
421 break;
422
Douglas Gregor42b29842011-10-05 19:00:14 +0000423 case Stmt::CXXDependentScopeMemberExprClass:
424 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000425 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000426 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000427 case Stmt::ObjCIvarRefExprClass:
428 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000429 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000430 K = CXCursor_MemberRefExpr;
431 break;
432
433 case Stmt::CallExprClass:
434 case Stmt::CXXOperatorCallExprClass:
435 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000436 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000437 case Stmt::CXXConstructExprClass:
438 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000439 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000440 K = CXCursor_CallExpr;
441 break;
442
Douglas Gregorba0513d2011-10-25 01:33:02 +0000443 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000444 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000445 int SelectorIdIndex = -1;
446 // Check if cursor points to a selector id.
447 if (RegionOfInterest.isValid() &&
448 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
449 SmallVector<SourceLocation, 16> SelLocs;
450 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
451 SmallVector<SourceLocation, 16>::iterator
452 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
453 if (I != SelLocs.end())
454 SelectorIdIndex = I - SelLocs.begin();
455 }
456 CXCursor C = { K, 0, { Parent, S, TU } };
457 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000458 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000459
460 case Stmt::MSDependentExistsStmtClass:
461 K = CXCursor_UnexposedStmt;
462 break;
463 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000464
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000465 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000466 return C;
467}
468
Douglas Gregor2e331b92010-01-16 14:00:32 +0000469CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000470 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000471 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000472 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000473 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000474 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000475 return C;
476}
477
478std::pair<ObjCInterfaceDecl *, SourceLocation>
479cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
480 assert(C.kind == CXCursor_ObjCSuperClassRef);
481 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
482 SourceLocation::getFromRawEncoding(
483 reinterpret_cast<uintptr_t>(C.data[1])));
484}
485
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000486CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000487 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000488 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000489 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000490 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000491 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000492 return C;
493}
494
495std::pair<ObjCProtocolDecl *, SourceLocation>
496cxcursor::getCursorObjCProtocolRef(CXCursor C) {
497 assert(C.kind == CXCursor_ObjCProtocolRef);
498 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
499 SourceLocation::getFromRawEncoding(
500 reinterpret_cast<uintptr_t>(C.data[1])));
501}
502
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000503CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000504 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000505 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000506 // 'Class' can be null for invalid code.
507 if (!Class)
508 return MakeCXCursorInvalid(CXCursor_InvalidCode);
509 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000510 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000511 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000512 return C;
513}
514
515std::pair<ObjCInterfaceDecl *, SourceLocation>
516cxcursor::getCursorObjCClassRef(CXCursor C) {
517 assert(C.kind == CXCursor_ObjCClassRef);
518 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
519 SourceLocation::getFromRawEncoding(
520 reinterpret_cast<uintptr_t>(C.data[1])));
521}
522
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000523CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000524 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000525 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000526 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000527 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000528 return C;
529}
530
531std::pair<TypeDecl *, SourceLocation>
532cxcursor::getCursorTypeRef(CXCursor C) {
533 assert(C.kind == CXCursor_TypeRef);
534 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
535 SourceLocation::getFromRawEncoding(
536 reinterpret_cast<uintptr_t>(C.data[1])));
537}
538
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000539CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000540 SourceLocation Loc,
541 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000542 assert(Template && TU && "Invalid arguments!");
543 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000544 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000545 return C;
546}
547
548std::pair<TemplateDecl *, SourceLocation>
549cxcursor::getCursorTemplateRef(CXCursor C) {
550 assert(C.kind == CXCursor_TemplateRef);
551 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
552 SourceLocation::getFromRawEncoding(
553 reinterpret_cast<uintptr_t>(C.data[1])));
554}
555
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000556CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
557 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000558 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000559
560 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
561 "Invalid arguments!");
562 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000563 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000564 return C;
565}
566
567std::pair<NamedDecl *, SourceLocation>
568cxcursor::getCursorNamespaceRef(CXCursor C) {
569 assert(C.kind == CXCursor_NamespaceRef);
570 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
571 SourceLocation::getFromRawEncoding(
572 reinterpret_cast<uintptr_t>(C.data[1])));
573}
574
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000575CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000576 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000577
578 assert(Field && TU && "Invalid arguments!");
579 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000580 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000581 return C;
582}
583
584std::pair<FieldDecl *, SourceLocation>
585cxcursor::getCursorMemberRef(CXCursor C) {
586 assert(C.kind == CXCursor_MemberRef);
587 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
588 SourceLocation::getFromRawEncoding(
589 reinterpret_cast<uintptr_t>(C.data[1])));
590}
591
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000592CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000593 CXTranslationUnit TU){
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +0000594 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000595 return C;
596}
597
598CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
599 assert(C.kind == CXCursor_CXXBaseSpecifier);
600 return static_cast<CXXBaseSpecifier*>(C.data[0]);
601}
602
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000603CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000604 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000605 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000606 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
607 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
608 TU }
609 };
610 return C;
611}
612
613SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
614 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000615 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000616 reinterpret_cast<uintptr_t> (C.data[0])),
617 SourceLocation::getFromRawEncoding(
618 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000619 ASTUnit *TU = getCursorASTUnit(C);
620 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000621}
622
Ted Kremeneka60ed472010-11-16 08:15:36 +0000623CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
624 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000625 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000626 return C;
627}
628
629MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
630 assert(C.kind == CXCursor_MacroDefinition);
631 return static_cast<MacroDefinition *>(C.data[0]);
632}
633
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000634CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
635 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000636 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000637 return C;
638}
639
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000640MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000641 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000642 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000643}
644
Douglas Gregorecdcb882010-10-20 22:00:55 +0000645CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000646 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000647 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000648 return C;
649}
650
651InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
652 assert(C.kind == CXCursor_InclusionDirective);
653 return static_cast<InclusionDirective *>(C.data[0]);
654}
655
Douglas Gregor36897b02010-09-10 00:22:18 +0000656CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000657 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000658
659 assert(Label && TU && "Invalid arguments!");
660 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000661 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000662 return C;
663}
664
665std::pair<LabelStmt*, SourceLocation>
666cxcursor::getCursorLabelRef(CXCursor C) {
667 assert(C.kind == CXCursor_LabelRef);
668 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
669 SourceLocation::getFromRawEncoding(
670 reinterpret_cast<uintptr_t>(C.data[1])));
671}
672
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000673CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000674 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000675 assert(E && TU && "Invalid arguments!");
676 OverloadedDeclRefStorage Storage(E);
677 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
678 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000679 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000680 { Storage.getOpaqueValue(), RawLoc, TU }
681 };
682 return C;
683}
684
685CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
686 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000687 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000688 assert(D && TU && "Invalid arguments!");
689 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
690 OverloadedDeclRefStorage Storage(D);
691 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000692 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000693 { Storage.getOpaqueValue(), RawLoc, TU }
694 };
695 return C;
696}
697
698CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
699 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000700 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000701 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
702 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
703 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
704 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000705 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000706 { Storage.getOpaqueValue(), RawLoc, TU }
707 };
708 return C;
709}
710
711std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
712cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
713 assert(C.kind == CXCursor_OverloadedDeclRef);
714 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
715 SourceLocation::getFromRawEncoding(
716 reinterpret_cast<uintptr_t>(C.data[1])));
717}
718
Douglas Gregor283cae32010-01-15 21:56:13 +0000719Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
720 return (Decl *)Cursor.data[0];
721}
722
723Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
724 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
725}
726
727Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000728 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000729 Cursor.kind == CXCursor_ObjCProtocolRef ||
730 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000731 return 0;
732
Douglas Gregor283cae32010-01-15 21:56:13 +0000733 return (Stmt *)Cursor.data[1];
734}
735
Ted Kremenek95f33552010-08-26 01:42:22 +0000736Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
737 return (Attr *)Cursor.data[1];
738}
739
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000740Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
741 return (Decl *)Cursor.data[0];
742}
743
Douglas Gregorf46034a2010-01-18 23:41:10 +0000744ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000745 return getCursorASTUnit(Cursor)->getASTContext();
746}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000747
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000748ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000749 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
750 ->TUData);
751}
752
753CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
754 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000755}
756
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000757static void CollectOverriddenMethods(CXTranslationUnit TU,
758 DeclContext *Ctx,
759 ObjCMethodDecl *Method,
760 SmallVectorImpl<CXCursor> &Methods) {
761 if (!Ctx)
762 return;
763
764 // If we have a class or category implementation, jump straight to the
765 // interface.
766 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
767 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
768 Method, Methods);
769
770 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
771 if (!Container)
772 return;
773
774 // Check whether we have a matching method at this level.
775 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
776 Method->isInstanceMethod()))
777 if (Method != Overridden) {
778 // We found an override at this level; there is no need to look
779 // into other protocols or categories.
780 Methods.push_back(MakeCXCursor(Overridden, TU));
781 return;
782 }
783
784 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
785 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
786 PEnd = Protocol->protocol_end();
787 P != PEnd; ++P)
788 CollectOverriddenMethods(TU, *P, Method, Methods);
789 }
790
791 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
792 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
793 PEnd = Category->protocol_end();
794 P != PEnd; ++P)
795 CollectOverriddenMethods(TU, *P, Method, Methods);
796 }
797
798 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
799 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
800 PEnd = Interface->protocol_end();
801 P != PEnd; ++P)
802 CollectOverriddenMethods(TU, *P, Method, Methods);
803
804 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
805 Category; Category = Category->getNextClassCategory())
806 CollectOverriddenMethods(TU, Category, Method, Methods);
807
808 // We only look into the superclass if we haven't found anything yet.
809 if (Methods.empty())
810 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
811 return CollectOverriddenMethods(TU, Super, Method, Methods);
812 }
813}
814
815void cxcursor::getOverriddenCursors(CXCursor cursor,
816 SmallVectorImpl<CXCursor> &overridden) {
817 if (!clang_isDeclaration(cursor.kind))
818 return;
819
820 Decl *D = getCursorDecl(cursor);
821 if (!D)
822 return;
823
824 // Handle C++ member functions.
825 CXTranslationUnit TU = getCursorTU(cursor);
826 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
827 for (CXXMethodDecl::method_iterator
828 M = CXXMethod->begin_overridden_methods(),
829 MEnd = CXXMethod->end_overridden_methods();
830 M != MEnd; ++M)
831 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
832 return;
833 }
834
835 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
836 if (!Method)
837 return;
838
839 // Handle Objective-C methods.
840 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
841}
842
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000843std::pair<int, SourceLocation>
844cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
845 if (cursor.kind == CXCursor_ObjCMessageExpr) {
846 if (cursor.xdata != -1)
847 return std::make_pair(cursor.xdata,
848 cast<ObjCMessageExpr>(getCursorExpr(cursor))
849 ->getSelectorLoc(cursor.xdata));
850 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
851 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
852 if (cursor.xdata != -1)
853 return std::make_pair(cursor.xdata,
854 cast<ObjCMethodDecl>(getCursorDecl(cursor))
855 ->getSelectorLoc(cursor.xdata));
856 }
857
858 return std::make_pair(-1, SourceLocation());
859}
860
861CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
862 CXCursor newCursor = cursor;
863
864 if (cursor.kind == CXCursor_ObjCMessageExpr) {
865 if (SelIdx == -1 ||
866 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
867 ->getNumSelectorLocs())
868 newCursor.xdata = -1;
869 else
870 newCursor.xdata = SelIdx;
871 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
872 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
873 if (SelIdx == -1 ||
874 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
875 ->getNumSelectorLocs())
876 newCursor.xdata = -1;
877 else
878 newCursor.xdata = SelIdx;
879 }
880
881 return newCursor;
882}
883
884CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
885 if (cursor.kind != CXCursor_CallExpr)
886 return cursor;
887
888 if (cursor.xdata == 0)
889 return cursor;
890
891 Expr *E = getCursorExpr(cursor);
892 TypeSourceInfo *Type = 0;
893 if (CXXUnresolvedConstructExpr *
894 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
895 Type = UnCtor->getTypeSourceInfo();
896 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
897 Type = Tmp->getTypeSourceInfo();
898 }
899
900 if (!Type)
901 return cursor;
902
903 CXTranslationUnit TU = getCursorTU(cursor);
904 QualType Ty = Type->getType();
905 TypeLoc TL = Type->getTypeLoc();
906 SourceLocation Loc = TL.getBeginLoc();
907
908 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
909 Ty = ElabT->getNamedType();
910 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
911 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
912 }
913
914 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
915 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
916 if (const TagType *Tag = Ty->getAs<TagType>())
917 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
918 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
919 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
920
921 return cursor;
922}
923
Douglas Gregor283cae32010-01-15 21:56:13 +0000924bool cxcursor::operator==(CXCursor X, CXCursor Y) {
925 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
926 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000927}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000928
929// FIXME: Remove once we can model DeclGroups and their appropriate ranges
930// properly in the ASTs.
931bool cxcursor::isFirstInDeclGroup(CXCursor C) {
932 assert(clang_isDeclaration(C.kind));
933 return ((uintptr_t) (C.data[1])) != 0;
934}
935
Ted Kremenekeca099b2010-12-08 23:43:14 +0000936//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000937// libclang CXCursor APIs
938//===----------------------------------------------------------------------===//
939
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000940extern "C" {
941
942int clang_Cursor_isNull(CXCursor cursor) {
943 return clang_equalCursors(cursor, clang_getNullCursor());
944}
945
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000946CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
947 return getCursorTU(cursor);
948}
949
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000950} // end: extern "C"
951
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000952//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000953// CXCursorSet.
954//===----------------------------------------------------------------------===//
955
956typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
957
958static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
959 return (CXCursorSet) setImpl;
960}
961static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
962 return (CXCursorSet_Impl*) set;
963}
964namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000965template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000966public:
967 static inline CXCursor getEmptyKey() {
968 return MakeCXCursorInvalid(CXCursor_InvalidFile);
969 }
970 static inline CXCursor getTombstoneKey() {
971 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
972 }
973 static inline unsigned getHashValue(const CXCursor &cursor) {
974 return llvm::DenseMapInfo<std::pair<void*,void*> >
975 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
976 }
977 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
978 return x.kind == y.kind &&
979 x.data[0] == y.data[0] &&
980 x.data[1] == y.data[1];
981 }
982};
983}
984
985extern "C" {
986CXCursorSet clang_createCXCursorSet() {
987 return packCXCursorSet(new CXCursorSet_Impl());
988}
989
990void clang_disposeCXCursorSet(CXCursorSet set) {
991 delete unpackCXCursorSet(set);
992}
993
994unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
995 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
996 if (!setImpl)
997 return 0;
998 return setImpl->find(cursor) == setImpl->end();
999}
1000
Anders Carlssone8b3de02010-12-09 01:00:12 +00001001unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001002 // Do not insert invalid cursors into the set.
1003 if (cursor.kind >= CXCursor_FirstInvalid &&
1004 cursor.kind <= CXCursor_LastInvalid)
1005 return 1;
1006
1007 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1008 if (!setImpl)
1009 return 1;
1010 unsigned &entry = (*setImpl)[cursor];
1011 unsigned flag = entry == 0 ? 1 : 0;
1012 entry = 1;
1013 return flag;
1014}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001015
1016CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1017 enum CXCursorKind kind = clang_getCursorKind(cursor);
1018 if (clang_isDeclaration(kind)) {
1019 Decl *decl = getCursorDecl(cursor);
1020 if (isa<NamedDecl>(decl)) {
1021 NamedDecl *namedDecl = (NamedDecl *)decl;
1022 ASTUnit *unit = getCursorASTUnit(cursor);
1023 if (unit->hasSema()) {
1024 Sema &S = unit->getSema();
1025 CodeCompletionAllocator *Allocator
1026 = unit->getCursorCompletionAllocator().getPtr();
1027 CodeCompletionResult Result(namedDecl);
1028 CodeCompletionString *String
1029 = Result.CreateCodeCompletionString(S, *Allocator);
1030 return String;
1031 }
1032 }
1033 }
1034 else if (kind == CXCursor_MacroDefinition) {
1035 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1036 const IdentifierInfo *MacroInfo = definition->getName();
1037 ASTUnit *unit = getCursorASTUnit(cursor);
1038 if (unit->hasSema()) {
1039 Sema &S = unit->getSema();
1040 CodeCompletionAllocator *Allocator
1041 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +00001042 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001043 CodeCompletionString *String
1044 = Result.CreateCodeCompletionString(S, *Allocator);
1045 return String;
1046 }
1047 }
1048 return NULL;
1049}
1050
Ted Kremenekeca099b2010-12-08 23:43:14 +00001051} // end: extern "C"