blob: 202a19254e9dff0deaaeaeb7a93a78b074adfabb [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:
Douglas Gregor42b29842011-10-05 19:00:14 +0000222 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
John McCall4b9c2d22011-11-06 09:01:30 +0000231 case Stmt::OpaqueValueExprClass:
232 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
233 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
234 K = CXCursor_UnexposedExpr;
235 break;
236
237 case Stmt::PseudoObjectExprClass:
238 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
239 Parent, TU, RegionOfInterest);
240
Douglas Gregor42b29842011-10-05 19:00:14 +0000241 case Stmt::CompoundStmtClass:
242 K = CXCursor_CompoundStmt;
243 break;
Douglas Gregor36897b02010-09-10 00:22:18 +0000244
Douglas Gregor42b29842011-10-05 19:00:14 +0000245 case Stmt::NullStmtClass:
246 K = CXCursor_NullStmt;
247 break;
248
249 case Stmt::LabelStmtClass:
250 K = CXCursor_LabelStmt;
251 break;
252
253 case Stmt::DeclStmtClass:
254 K = CXCursor_DeclStmt;
255 break;
256
257 case Stmt::IntegerLiteralClass:
258 K = CXCursor_IntegerLiteral;
259 break;
260
261 case Stmt::FloatingLiteralClass:
262 K = CXCursor_FloatingLiteral;
263 break;
264
265 case Stmt::ImaginaryLiteralClass:
266 K = CXCursor_ImaginaryLiteral;
267 break;
268
269 case Stmt::StringLiteralClass:
270 K = CXCursor_StringLiteral;
271 break;
272
273 case Stmt::CharacterLiteralClass:
274 K = CXCursor_CharacterLiteral;
275 break;
276
277 case Stmt::ParenExprClass:
278 K = CXCursor_ParenExpr;
279 break;
280
281 case Stmt::UnaryOperatorClass:
282 K = CXCursor_UnaryOperator;
283 break;
284
285 case Stmt::CXXNoexceptExprClass:
286 K = CXCursor_UnaryExpr;
287 break;
288
289 case Stmt::ArraySubscriptExprClass:
290 K = CXCursor_ArraySubscriptExpr;
291 break;
292
293 case Stmt::BinaryOperatorClass:
294 K = CXCursor_BinaryOperator;
295 break;
296
297 case Stmt::CompoundAssignOperatorClass:
298 K = CXCursor_CompoundAssignOperator;
299 break;
300
301 case Stmt::ConditionalOperatorClass:
302 K = CXCursor_ConditionalOperator;
303 break;
304
305 case Stmt::CStyleCastExprClass:
306 K = CXCursor_CStyleCastExpr;
307 break;
308
309 case Stmt::CompoundLiteralExprClass:
310 K = CXCursor_CompoundLiteralExpr;
311 break;
312
313 case Stmt::InitListExprClass:
314 K = CXCursor_InitListExpr;
315 break;
316
317 case Stmt::AddrLabelExprClass:
318 K = CXCursor_AddrLabelExpr;
319 break;
320
321 case Stmt::StmtExprClass:
322 K = CXCursor_StmtExpr;
323 break;
324
325 case Stmt::GenericSelectionExprClass:
326 K = CXCursor_GenericSelectionExpr;
327 break;
328
329 case Stmt::GNUNullExprClass:
330 K = CXCursor_GNUNullExpr;
331 break;
332
333 case Stmt::CXXStaticCastExprClass:
334 K = CXCursor_CXXStaticCastExpr;
335 break;
336
337 case Stmt::CXXDynamicCastExprClass:
338 K = CXCursor_CXXDynamicCastExpr;
339 break;
340
341 case Stmt::CXXReinterpretCastExprClass:
342 K = CXCursor_CXXReinterpretCastExpr;
343 break;
344
345 case Stmt::CXXConstCastExprClass:
346 K = CXCursor_CXXConstCastExpr;
347 break;
348
349 case Stmt::CXXFunctionalCastExprClass:
350 K = CXCursor_CXXFunctionalCastExpr;
351 break;
352
353 case Stmt::CXXTypeidExprClass:
354 K = CXCursor_CXXTypeidExpr;
355 break;
356
357 case Stmt::CXXBoolLiteralExprClass:
358 K = CXCursor_CXXBoolLiteralExpr;
359 break;
360
361 case Stmt::CXXNullPtrLiteralExprClass:
362 K = CXCursor_CXXNullPtrLiteralExpr;
363 break;
364
365 case Stmt::CXXThisExprClass:
366 K = CXCursor_CXXThisExpr;
367 break;
368
369 case Stmt::CXXThrowExprClass:
370 K = CXCursor_CXXThrowExpr;
371 break;
372
373 case Stmt::CXXNewExprClass:
374 K = CXCursor_CXXNewExpr;
375 break;
376
377 case Stmt::CXXDeleteExprClass:
378 K = CXCursor_CXXDeleteExpr;
379 break;
380
381 case Stmt::ObjCStringLiteralClass:
382 K = CXCursor_ObjCStringLiteral;
383 break;
384
385 case Stmt::ObjCEncodeExprClass:
386 K = CXCursor_ObjCEncodeExpr;
387 break;
388
389 case Stmt::ObjCSelectorExprClass:
390 K = CXCursor_ObjCSelectorExpr;
391 break;
392
393 case Stmt::ObjCProtocolExprClass:
394 K = CXCursor_ObjCProtocolExpr;
395 break;
396
397 case Stmt::ObjCBridgedCastExprClass:
398 K = CXCursor_ObjCBridgedCastExpr;
399 break;
400
401 case Stmt::BlockExprClass:
402 K = CXCursor_BlockExpr;
403 break;
404
405 case Stmt::PackExpansionExprClass:
406 K = CXCursor_PackExpansionExpr;
407 break;
408
409 case Stmt::SizeOfPackExprClass:
410 K = CXCursor_SizeOfPackExpr;
411 break;
412
Douglas Gregor97b98722010-01-19 23:20:36 +0000413 case Stmt::BlockDeclRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000414 case Stmt::DeclRefExprClass:
415 case Stmt::DependentScopeDeclRefExprClass:
John McCall91a57552011-07-15 05:09:51 +0000416 case Stmt::SubstNonTypeTemplateParmExprClass:
Douglas Gregorc7793c72011-01-15 01:15:58 +0000417 case Stmt::SubstNonTypeTemplateParmPackExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000418 case Stmt::UnresolvedLookupExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000419 K = CXCursor_DeclRefExpr;
420 break;
421
Douglas Gregor42b29842011-10-05 19:00:14 +0000422 case Stmt::CXXDependentScopeMemberExprClass:
423 case Stmt::CXXPseudoDestructorExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000424 case Stmt::MemberExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000425 case Stmt::ObjCIsaExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000426 case Stmt::ObjCIvarRefExprClass:
427 case Stmt::ObjCPropertyRefExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000428 case Stmt::UnresolvedMemberExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000429 K = CXCursor_MemberRefExpr;
430 break;
431
432 case Stmt::CallExprClass:
433 case Stmt::CXXOperatorCallExprClass:
434 case Stmt::CXXMemberCallExprClass:
Peter Collingbournee08ce652011-02-09 21:07:24 +0000435 case Stmt::CUDAKernelCallExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000436 case Stmt::CXXConstructExprClass:
437 case Stmt::CXXTemporaryObjectExprClass:
Douglas Gregor42b29842011-10-05 19:00:14 +0000438 case Stmt::CXXUnresolvedConstructExprClass:
Douglas Gregor97b98722010-01-19 23:20:36 +0000439 K = CXCursor_CallExpr;
440 break;
441
Douglas Gregorba0513d2011-10-25 01:33:02 +0000442 case Stmt::ObjCMessageExprClass: {
Douglas Gregor97b98722010-01-19 23:20:36 +0000443 K = CXCursor_ObjCMessageExpr;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000444 int SelectorIdIndex = -1;
445 // Check if cursor points to a selector id.
446 if (RegionOfInterest.isValid() &&
447 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
448 SmallVector<SourceLocation, 16> SelLocs;
449 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
450 SmallVector<SourceLocation, 16>::iterator
451 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
452 if (I != SelLocs.end())
453 SelectorIdIndex = I - SelLocs.begin();
454 }
455 CXCursor C = { K, 0, { Parent, S, TU } };
456 return getSelectorIdentifierCursor(SelectorIdIndex, C);
Douglas Gregor97b98722010-01-19 23:20:36 +0000457 }
Douglas Gregorba0513d2011-10-25 01:33:02 +0000458
459 case Stmt::MSDependentExistsStmtClass:
460 K = CXCursor_UnexposedStmt;
461 break;
462 }
Douglas Gregor97b98722010-01-19 23:20:36 +0000463
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000464 CXCursor C = { K, 0, { Parent, S, TU } };
Douglas Gregor97b98722010-01-19 23:20:36 +0000465 return C;
466}
467
Douglas Gregor2e331b92010-01-16 14:00:32 +0000468CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000469 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000470 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000471 assert(Super && TU && "Invalid arguments!");
Douglas Gregor2e331b92010-01-16 14:00:32 +0000472 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000473 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
Douglas Gregor2e331b92010-01-16 14:00:32 +0000474 return C;
475}
476
477std::pair<ObjCInterfaceDecl *, SourceLocation>
478cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
479 assert(C.kind == CXCursor_ObjCSuperClassRef);
480 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
481 SourceLocation::getFromRawEncoding(
482 reinterpret_cast<uintptr_t>(C.data[1])));
483}
484
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000485CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000486 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000487 CXTranslationUnit TU) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000488 assert(Proto && TU && "Invalid arguments!");
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000489 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000490 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000491 return C;
492}
493
494std::pair<ObjCProtocolDecl *, SourceLocation>
495cxcursor::getCursorObjCProtocolRef(CXCursor C) {
496 assert(C.kind == CXCursor_ObjCProtocolRef);
497 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
498 SourceLocation::getFromRawEncoding(
499 reinterpret_cast<uintptr_t>(C.data[1])));
500}
501
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000502CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000503 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000504 CXTranslationUnit TU) {
Ted Kremenekebfa3392010-03-19 20:39:03 +0000505 // 'Class' can be null for invalid code.
506 if (!Class)
507 return MakeCXCursorInvalid(CXCursor_InvalidCode);
508 assert(TU && "Invalid arguments!");
Douglas Gregor1adb0822010-01-16 17:14:40 +0000509 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000510 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
Douglas Gregor1adb0822010-01-16 17:14:40 +0000511 return C;
512}
513
514std::pair<ObjCInterfaceDecl *, SourceLocation>
515cxcursor::getCursorObjCClassRef(CXCursor C) {
516 assert(C.kind == CXCursor_ObjCClassRef);
517 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
518 SourceLocation::getFromRawEncoding(
519 reinterpret_cast<uintptr_t>(C.data[1])));
520}
521
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000522CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000523 CXTranslationUnit TU) {
Daniel Dunbar54d67ca2010-01-25 00:40:30 +0000524 assert(Type && TU && "Invalid arguments!");
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000525 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +0000526 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
Douglas Gregor7d0d40e2010-01-21 16:28:34 +0000527 return C;
528}
529
530std::pair<TypeDecl *, SourceLocation>
531cxcursor::getCursorTypeRef(CXCursor C) {
532 assert(C.kind == CXCursor_TypeRef);
533 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
534 SourceLocation::getFromRawEncoding(
535 reinterpret_cast<uintptr_t>(C.data[1])));
536}
537
Douglas Gregor0b36e612010-08-31 20:37:03 +0000538CXCursor cxcursor::MakeCursorTemplateRef(TemplateDecl *Template,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000539 SourceLocation Loc,
540 CXTranslationUnit TU) {
Douglas Gregor0b36e612010-08-31 20:37:03 +0000541 assert(Template && TU && "Invalid arguments!");
542 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000543 CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
Douglas Gregor0b36e612010-08-31 20:37:03 +0000544 return C;
545}
546
547std::pair<TemplateDecl *, SourceLocation>
548cxcursor::getCursorTemplateRef(CXCursor C) {
549 assert(C.kind == CXCursor_TemplateRef);
550 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
551 SourceLocation::getFromRawEncoding(
552 reinterpret_cast<uintptr_t>(C.data[1])));
553}
554
Douglas Gregor69319002010-08-31 23:48:11 +0000555CXCursor cxcursor::MakeCursorNamespaceRef(NamedDecl *NS, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000556 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000557
558 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
559 "Invalid arguments!");
560 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000561 CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000562 return C;
563}
564
565std::pair<NamedDecl *, SourceLocation>
566cxcursor::getCursorNamespaceRef(CXCursor C) {
567 assert(C.kind == CXCursor_NamespaceRef);
568 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
569 SourceLocation::getFromRawEncoding(
570 reinterpret_cast<uintptr_t>(C.data[1])));
571}
572
Douglas Gregora67e03f2010-09-09 21:42:20 +0000573CXCursor cxcursor::MakeCursorMemberRef(FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000574 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000575
576 assert(Field && TU && "Invalid arguments!");
577 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000578 CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000579 return C;
580}
581
582std::pair<FieldDecl *, SourceLocation>
583cxcursor::getCursorMemberRef(CXCursor C) {
584 assert(C.kind == CXCursor_MemberRef);
585 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
586 SourceLocation::getFromRawEncoding(
587 reinterpret_cast<uintptr_t>(C.data[1])));
588}
589
Ted Kremeneka60ed472010-11-16 08:15:36 +0000590CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
591 CXTranslationUnit TU){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000592 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000593 return C;
594}
595
596CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
597 assert(C.kind == CXCursor_CXXBaseSpecifier);
598 return static_cast<CXXBaseSpecifier*>(C.data[0]);
599}
600
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000601CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000602 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000603 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000604 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
605 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
606 TU }
607 };
608 return C;
609}
610
611SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
612 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000613 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000614 reinterpret_cast<uintptr_t> (C.data[0])),
615 SourceLocation::getFromRawEncoding(
616 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000617 ASTUnit *TU = getCursorASTUnit(C);
618 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000619}
620
Ted Kremeneka60ed472010-11-16 08:15:36 +0000621CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
622 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000623 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000624 return C;
625}
626
627MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
628 assert(C.kind == CXCursor_MacroDefinition);
629 return static_cast<MacroDefinition *>(C.data[0]);
630}
631
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000632CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
633 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000634 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000635 return C;
636}
637
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000638MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000639 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000640 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000641}
642
Douglas Gregorecdcb882010-10-20 22:00:55 +0000643CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000644 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000645 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000646 return C;
647}
648
649InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
650 assert(C.kind == CXCursor_InclusionDirective);
651 return static_cast<InclusionDirective *>(C.data[0]);
652}
653
Douglas Gregor36897b02010-09-10 00:22:18 +0000654CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000655 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000656
657 assert(Label && TU && "Invalid arguments!");
658 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000659 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000660 return C;
661}
662
663std::pair<LabelStmt*, SourceLocation>
664cxcursor::getCursorLabelRef(CXCursor C) {
665 assert(C.kind == CXCursor_LabelRef);
666 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
667 SourceLocation::getFromRawEncoding(
668 reinterpret_cast<uintptr_t>(C.data[1])));
669}
670
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000671CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000672 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000673 assert(E && TU && "Invalid arguments!");
674 OverloadedDeclRefStorage Storage(E);
675 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
676 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000677 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000678 { Storage.getOpaqueValue(), RawLoc, TU }
679 };
680 return C;
681}
682
683CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
684 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000685 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000686 assert(D && TU && "Invalid arguments!");
687 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
688 OverloadedDeclRefStorage Storage(D);
689 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000690 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000691 { Storage.getOpaqueValue(), RawLoc, TU }
692 };
693 return C;
694}
695
696CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
697 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000698 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000699 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
700 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
701 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
702 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000703 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000704 { Storage.getOpaqueValue(), RawLoc, TU }
705 };
706 return C;
707}
708
709std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
710cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
711 assert(C.kind == CXCursor_OverloadedDeclRef);
712 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
713 SourceLocation::getFromRawEncoding(
714 reinterpret_cast<uintptr_t>(C.data[1])));
715}
716
Douglas Gregor283cae32010-01-15 21:56:13 +0000717Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
718 return (Decl *)Cursor.data[0];
719}
720
721Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
722 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
723}
724
725Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000726 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000727 Cursor.kind == CXCursor_ObjCProtocolRef ||
728 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000729 return 0;
730
Douglas Gregor283cae32010-01-15 21:56:13 +0000731 return (Stmt *)Cursor.data[1];
732}
733
Ted Kremenek95f33552010-08-26 01:42:22 +0000734Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
735 return (Attr *)Cursor.data[1];
736}
737
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000738Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
739 return (Decl *)Cursor.data[0];
740}
741
Douglas Gregorf46034a2010-01-18 23:41:10 +0000742ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000743 return getCursorASTUnit(Cursor)->getASTContext();
744}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000745
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000746ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000747 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
748 ->TUData);
749}
750
751CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
752 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000753}
754
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000755static void CollectOverriddenMethods(CXTranslationUnit TU,
756 DeclContext *Ctx,
757 ObjCMethodDecl *Method,
758 SmallVectorImpl<CXCursor> &Methods) {
759 if (!Ctx)
760 return;
761
762 // If we have a class or category implementation, jump straight to the
763 // interface.
764 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
765 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
766 Method, Methods);
767
768 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
769 if (!Container)
770 return;
771
772 // Check whether we have a matching method at this level.
773 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
774 Method->isInstanceMethod()))
775 if (Method != Overridden) {
776 // We found an override at this level; there is no need to look
777 // into other protocols or categories.
778 Methods.push_back(MakeCXCursor(Overridden, TU));
779 return;
780 }
781
782 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
783 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
784 PEnd = Protocol->protocol_end();
785 P != PEnd; ++P)
786 CollectOverriddenMethods(TU, *P, Method, Methods);
787 }
788
789 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
790 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
791 PEnd = Category->protocol_end();
792 P != PEnd; ++P)
793 CollectOverriddenMethods(TU, *P, Method, Methods);
794 }
795
796 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
797 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
798 PEnd = Interface->protocol_end();
799 P != PEnd; ++P)
800 CollectOverriddenMethods(TU, *P, Method, Methods);
801
802 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
803 Category; Category = Category->getNextClassCategory())
804 CollectOverriddenMethods(TU, Category, Method, Methods);
805
806 // We only look into the superclass if we haven't found anything yet.
807 if (Methods.empty())
808 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
809 return CollectOverriddenMethods(TU, Super, Method, Methods);
810 }
811}
812
813void cxcursor::getOverriddenCursors(CXCursor cursor,
814 SmallVectorImpl<CXCursor> &overridden) {
815 if (!clang_isDeclaration(cursor.kind))
816 return;
817
818 Decl *D = getCursorDecl(cursor);
819 if (!D)
820 return;
821
822 // Handle C++ member functions.
823 CXTranslationUnit TU = getCursorTU(cursor);
824 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
825 for (CXXMethodDecl::method_iterator
826 M = CXXMethod->begin_overridden_methods(),
827 MEnd = CXXMethod->end_overridden_methods();
828 M != MEnd; ++M)
829 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
830 return;
831 }
832
833 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
834 if (!Method)
835 return;
836
837 // Handle Objective-C methods.
838 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
839}
840
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000841std::pair<int, SourceLocation>
842cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
843 if (cursor.kind == CXCursor_ObjCMessageExpr) {
844 if (cursor.xdata != -1)
845 return std::make_pair(cursor.xdata,
846 cast<ObjCMessageExpr>(getCursorExpr(cursor))
847 ->getSelectorLoc(cursor.xdata));
848 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
849 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
850 if (cursor.xdata != -1)
851 return std::make_pair(cursor.xdata,
852 cast<ObjCMethodDecl>(getCursorDecl(cursor))
853 ->getSelectorLoc(cursor.xdata));
854 }
855
856 return std::make_pair(-1, SourceLocation());
857}
858
859CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
860 CXCursor newCursor = cursor;
861
862 if (cursor.kind == CXCursor_ObjCMessageExpr) {
863 if (SelIdx == -1 ||
864 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
865 ->getNumSelectorLocs())
866 newCursor.xdata = -1;
867 else
868 newCursor.xdata = SelIdx;
869 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
870 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
871 if (SelIdx == -1 ||
872 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
873 ->getNumSelectorLocs())
874 newCursor.xdata = -1;
875 else
876 newCursor.xdata = SelIdx;
877 }
878
879 return newCursor;
880}
881
882CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
883 if (cursor.kind != CXCursor_CallExpr)
884 return cursor;
885
886 if (cursor.xdata == 0)
887 return cursor;
888
889 Expr *E = getCursorExpr(cursor);
890 TypeSourceInfo *Type = 0;
891 if (CXXUnresolvedConstructExpr *
892 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
893 Type = UnCtor->getTypeSourceInfo();
894 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
895 Type = Tmp->getTypeSourceInfo();
896 }
897
898 if (!Type)
899 return cursor;
900
901 CXTranslationUnit TU = getCursorTU(cursor);
902 QualType Ty = Type->getType();
903 TypeLoc TL = Type->getTypeLoc();
904 SourceLocation Loc = TL.getBeginLoc();
905
906 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
907 Ty = ElabT->getNamedType();
908 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
909 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
910 }
911
912 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
913 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
914 if (const TagType *Tag = Ty->getAs<TagType>())
915 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
916 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
917 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
918
919 return cursor;
920}
921
Douglas Gregor283cae32010-01-15 21:56:13 +0000922bool cxcursor::operator==(CXCursor X, CXCursor Y) {
923 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
924 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000925}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000926
927// FIXME: Remove once we can model DeclGroups and their appropriate ranges
928// properly in the ASTs.
929bool cxcursor::isFirstInDeclGroup(CXCursor C) {
930 assert(clang_isDeclaration(C.kind));
931 return ((uintptr_t) (C.data[1])) != 0;
932}
933
Ted Kremenekeca099b2010-12-08 23:43:14 +0000934//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000935// libclang CXCursor APIs
936//===----------------------------------------------------------------------===//
937
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000938extern "C" {
939
940int clang_Cursor_isNull(CXCursor cursor) {
941 return clang_equalCursors(cursor, clang_getNullCursor());
942}
943
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000944CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
945 return getCursorTU(cursor);
946}
947
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000948} // end: extern "C"
949
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000950//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000951// CXCursorSet.
952//===----------------------------------------------------------------------===//
953
954typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
955
956static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
957 return (CXCursorSet) setImpl;
958}
959static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
960 return (CXCursorSet_Impl*) set;
961}
962namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000963template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000964public:
965 static inline CXCursor getEmptyKey() {
966 return MakeCXCursorInvalid(CXCursor_InvalidFile);
967 }
968 static inline CXCursor getTombstoneKey() {
969 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
970 }
971 static inline unsigned getHashValue(const CXCursor &cursor) {
972 return llvm::DenseMapInfo<std::pair<void*,void*> >
973 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
974 }
975 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
976 return x.kind == y.kind &&
977 x.data[0] == y.data[0] &&
978 x.data[1] == y.data[1];
979 }
980};
981}
982
983extern "C" {
984CXCursorSet clang_createCXCursorSet() {
985 return packCXCursorSet(new CXCursorSet_Impl());
986}
987
988void clang_disposeCXCursorSet(CXCursorSet set) {
989 delete unpackCXCursorSet(set);
990}
991
992unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
993 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
994 if (!setImpl)
995 return 0;
996 return setImpl->find(cursor) == setImpl->end();
997}
998
Anders Carlssone8b3de02010-12-09 01:00:12 +0000999unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001000 // Do not insert invalid cursors into the set.
1001 if (cursor.kind >= CXCursor_FirstInvalid &&
1002 cursor.kind <= CXCursor_LastInvalid)
1003 return 1;
1004
1005 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1006 if (!setImpl)
1007 return 1;
1008 unsigned &entry = (*setImpl)[cursor];
1009 unsigned flag = entry == 0 ? 1 : 0;
1010 entry = 1;
1011 return flag;
1012}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001013
1014CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1015 enum CXCursorKind kind = clang_getCursorKind(cursor);
1016 if (clang_isDeclaration(kind)) {
1017 Decl *decl = getCursorDecl(cursor);
1018 if (isa<NamedDecl>(decl)) {
1019 NamedDecl *namedDecl = (NamedDecl *)decl;
1020 ASTUnit *unit = getCursorASTUnit(cursor);
1021 if (unit->hasSema()) {
1022 Sema &S = unit->getSema();
1023 CodeCompletionAllocator *Allocator
1024 = unit->getCursorCompletionAllocator().getPtr();
1025 CodeCompletionResult Result(namedDecl);
1026 CodeCompletionString *String
1027 = Result.CreateCodeCompletionString(S, *Allocator);
1028 return String;
1029 }
1030 }
1031 }
1032 else if (kind == CXCursor_MacroDefinition) {
1033 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1034 const IdentifierInfo *MacroInfo = definition->getName();
1035 ASTUnit *unit = getCursorASTUnit(cursor);
1036 if (unit->hasSema()) {
1037 Sema &S = unit->getSema();
1038 CodeCompletionAllocator *Allocator
1039 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +00001040 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001041 CodeCompletionString *String
1042 = Result.CreateCodeCompletionString(S, *Allocator);
1043 return String;
1044 }
1045 }
1046 return NULL;
1047}
1048
Ted Kremenekeca099b2010-12-08 23:43:14 +00001049} // end: extern "C"