blob: bba0928ceac1485be3c2aa5196a59582a0a7d3bf [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
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000538CXCursor cxcursor::MakeCursorTemplateRef(const 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 Kyrtzidisb395c632011-11-18 00:26:51 +0000543 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)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
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000555CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
556 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000557 CXTranslationUnit TU) {
Douglas Gregor69319002010-08-31 23:48:11 +0000558
559 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
560 "Invalid arguments!");
561 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000562 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
Douglas Gregor69319002010-08-31 23:48:11 +0000563 return C;
564}
565
566std::pair<NamedDecl *, SourceLocation>
567cxcursor::getCursorNamespaceRef(CXCursor C) {
568 assert(C.kind == CXCursor_NamespaceRef);
569 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
570 SourceLocation::getFromRawEncoding(
571 reinterpret_cast<uintptr_t>(C.data[1])));
572}
573
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000574CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000575 CXTranslationUnit TU) {
Douglas Gregora67e03f2010-09-09 21:42:20 +0000576
577 assert(Field && TU && "Invalid arguments!");
578 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +0000579 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
Douglas Gregora67e03f2010-09-09 21:42:20 +0000580 return C;
581}
582
583std::pair<FieldDecl *, SourceLocation>
584cxcursor::getCursorMemberRef(CXCursor C) {
585 assert(C.kind == CXCursor_MemberRef);
586 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
587 SourceLocation::getFromRawEncoding(
588 reinterpret_cast<uintptr_t>(C.data[1])));
589}
590
Ted Kremeneka60ed472010-11-16 08:15:36 +0000591CXCursor cxcursor::MakeCursorCXXBaseSpecifier(CXXBaseSpecifier *B,
592 CXTranslationUnit TU){
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000593 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
Ted Kremenek3064ef92010-08-27 21:34:58 +0000594 return C;
595}
596
597CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
598 assert(C.kind == CXCursor_CXXBaseSpecifier);
599 return static_cast<CXXBaseSpecifier*>(C.data[0]);
600}
601
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000602CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000603 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000604 CXCursor C = { CXCursor_PreprocessingDirective, 0,
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000605 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
606 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
607 TU }
608 };
609 return C;
610}
611
612SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
613 assert(C.kind == CXCursor_PreprocessingDirective);
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000614 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000615 reinterpret_cast<uintptr_t> (C.data[0])),
616 SourceLocation::getFromRawEncoding(
617 reinterpret_cast<uintptr_t> (C.data[1])));
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +0000618 ASTUnit *TU = getCursorASTUnit(C);
619 return TU->mapRangeFromPreamble(Range);
Douglas Gregor9f1e3ff2010-03-18 00:42:48 +0000620}
621
Ted Kremeneka60ed472010-11-16 08:15:36 +0000622CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
623 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000624 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
Douglas Gregor572feb22010-03-18 18:04:21 +0000625 return C;
626}
627
628MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
629 assert(C.kind == CXCursor_MacroDefinition);
630 return static_cast<MacroDefinition *>(C.data[0]);
631}
632
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000633CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
634 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000635 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
Douglas Gregor48072312010-03-18 15:23:44 +0000636 return C;
637}
638
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000639MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
Chandler Carruth9b2a0ac2011-07-14 08:41:15 +0000640 assert(C.kind == CXCursor_MacroExpansion);
Chandler Carruth9e5bb852011-07-14 08:20:46 +0000641 return static_cast<MacroExpansion *>(C.data[0]);
Douglas Gregor48072312010-03-18 15:23:44 +0000642}
643
Douglas Gregorecdcb882010-10-20 22:00:55 +0000644CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000645 CXTranslationUnit TU) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000646 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
Douglas Gregorecdcb882010-10-20 22:00:55 +0000647 return C;
648}
649
650InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
651 assert(C.kind == CXCursor_InclusionDirective);
652 return static_cast<InclusionDirective *>(C.data[0]);
653}
654
Douglas Gregor36897b02010-09-10 00:22:18 +0000655CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000656 CXTranslationUnit TU) {
Douglas Gregor36897b02010-09-10 00:22:18 +0000657
658 assert(Label && TU && "Invalid arguments!");
659 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000660 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
Douglas Gregor36897b02010-09-10 00:22:18 +0000661 return C;
662}
663
664std::pair<LabelStmt*, SourceLocation>
665cxcursor::getCursorLabelRef(CXCursor C) {
666 assert(C.kind == CXCursor_LabelRef);
667 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
668 SourceLocation::getFromRawEncoding(
669 reinterpret_cast<uintptr_t>(C.data[1])));
670}
671
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000672CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000673 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000674 assert(E && TU && "Invalid arguments!");
675 OverloadedDeclRefStorage Storage(E);
676 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
677 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000678 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000679 { Storage.getOpaqueValue(), RawLoc, TU }
680 };
681 return C;
682}
683
684CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
685 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000686 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000687 assert(D && TU && "Invalid arguments!");
688 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
689 OverloadedDeclRefStorage Storage(D);
690 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000691 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000692 { Storage.getOpaqueValue(), RawLoc, TU }
693 };
694 return C;
695}
696
697CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
698 SourceLocation Loc,
Ted Kremeneka60ed472010-11-16 08:15:36 +0000699 CXTranslationUnit TU) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000700 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
701 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
702 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
703 CXCursor C = {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000704 CXCursor_OverloadedDeclRef, 0,
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000705 { Storage.getOpaqueValue(), RawLoc, TU }
706 };
707 return C;
708}
709
710std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
711cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
712 assert(C.kind == CXCursor_OverloadedDeclRef);
713 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
714 SourceLocation::getFromRawEncoding(
715 reinterpret_cast<uintptr_t>(C.data[1])));
716}
717
Douglas Gregor283cae32010-01-15 21:56:13 +0000718Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
719 return (Decl *)Cursor.data[0];
720}
721
722Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
723 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
724}
725
726Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
Douglas Gregor78db0cd2010-01-16 15:44:18 +0000727 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
Douglas Gregor1adb0822010-01-16 17:14:40 +0000728 Cursor.kind == CXCursor_ObjCProtocolRef ||
729 Cursor.kind == CXCursor_ObjCClassRef)
Douglas Gregor2e331b92010-01-16 14:00:32 +0000730 return 0;
731
Douglas Gregor283cae32010-01-15 21:56:13 +0000732 return (Stmt *)Cursor.data[1];
733}
734
Ted Kremenek95f33552010-08-26 01:42:22 +0000735Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
736 return (Attr *)Cursor.data[1];
737}
738
Argyrios Kyrtzidis8ccac3d2011-06-29 22:20:07 +0000739Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
740 return (Decl *)Cursor.data[0];
741}
742
Douglas Gregorf46034a2010-01-18 23:41:10 +0000743ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000744 return getCursorASTUnit(Cursor)->getASTContext();
745}
Douglas Gregorf46034a2010-01-18 23:41:10 +0000746
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000747ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
Ted Kremeneka60ed472010-11-16 08:15:36 +0000748 return static_cast<ASTUnit *>(static_cast<CXTranslationUnit>(Cursor.data[2])
749 ->TUData);
750}
751
752CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
753 return static_cast<CXTranslationUnit>(Cursor.data[2]);
Douglas Gregor283cae32010-01-15 21:56:13 +0000754}
755
Argyrios Kyrtzidisb11be042011-10-06 07:00:46 +0000756static void CollectOverriddenMethods(CXTranslationUnit TU,
757 DeclContext *Ctx,
758 ObjCMethodDecl *Method,
759 SmallVectorImpl<CXCursor> &Methods) {
760 if (!Ctx)
761 return;
762
763 // If we have a class or category implementation, jump straight to the
764 // interface.
765 if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(Ctx))
766 return CollectOverriddenMethods(TU, Impl->getClassInterface(),
767 Method, Methods);
768
769 ObjCContainerDecl *Container = dyn_cast<ObjCContainerDecl>(Ctx);
770 if (!Container)
771 return;
772
773 // Check whether we have a matching method at this level.
774 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
775 Method->isInstanceMethod()))
776 if (Method != Overridden) {
777 // We found an override at this level; there is no need to look
778 // into other protocols or categories.
779 Methods.push_back(MakeCXCursor(Overridden, TU));
780 return;
781 }
782
783 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
784 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
785 PEnd = Protocol->protocol_end();
786 P != PEnd; ++P)
787 CollectOverriddenMethods(TU, *P, Method, Methods);
788 }
789
790 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
791 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
792 PEnd = Category->protocol_end();
793 P != PEnd; ++P)
794 CollectOverriddenMethods(TU, *P, Method, Methods);
795 }
796
797 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
798 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
799 PEnd = Interface->protocol_end();
800 P != PEnd; ++P)
801 CollectOverriddenMethods(TU, *P, Method, Methods);
802
803 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
804 Category; Category = Category->getNextClassCategory())
805 CollectOverriddenMethods(TU, Category, Method, Methods);
806
807 // We only look into the superclass if we haven't found anything yet.
808 if (Methods.empty())
809 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
810 return CollectOverriddenMethods(TU, Super, Method, Methods);
811 }
812}
813
814void cxcursor::getOverriddenCursors(CXCursor cursor,
815 SmallVectorImpl<CXCursor> &overridden) {
816 if (!clang_isDeclaration(cursor.kind))
817 return;
818
819 Decl *D = getCursorDecl(cursor);
820 if (!D)
821 return;
822
823 // Handle C++ member functions.
824 CXTranslationUnit TU = getCursorTU(cursor);
825 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
826 for (CXXMethodDecl::method_iterator
827 M = CXXMethod->begin_overridden_methods(),
828 MEnd = CXXMethod->end_overridden_methods();
829 M != MEnd; ++M)
830 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
831 return;
832 }
833
834 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
835 if (!Method)
836 return;
837
838 // Handle Objective-C methods.
839 CollectOverriddenMethods(TU, Method->getDeclContext(), Method, overridden);
840}
841
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000842std::pair<int, SourceLocation>
843cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
844 if (cursor.kind == CXCursor_ObjCMessageExpr) {
845 if (cursor.xdata != -1)
846 return std::make_pair(cursor.xdata,
847 cast<ObjCMessageExpr>(getCursorExpr(cursor))
848 ->getSelectorLoc(cursor.xdata));
849 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
850 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
851 if (cursor.xdata != -1)
852 return std::make_pair(cursor.xdata,
853 cast<ObjCMethodDecl>(getCursorDecl(cursor))
854 ->getSelectorLoc(cursor.xdata));
855 }
856
857 return std::make_pair(-1, SourceLocation());
858}
859
860CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
861 CXCursor newCursor = cursor;
862
863 if (cursor.kind == CXCursor_ObjCMessageExpr) {
864 if (SelIdx == -1 ||
865 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
866 ->getNumSelectorLocs())
867 newCursor.xdata = -1;
868 else
869 newCursor.xdata = SelIdx;
870 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
871 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
872 if (SelIdx == -1 ||
873 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
874 ->getNumSelectorLocs())
875 newCursor.xdata = -1;
876 else
877 newCursor.xdata = SelIdx;
878 }
879
880 return newCursor;
881}
882
883CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
884 if (cursor.kind != CXCursor_CallExpr)
885 return cursor;
886
887 if (cursor.xdata == 0)
888 return cursor;
889
890 Expr *E = getCursorExpr(cursor);
891 TypeSourceInfo *Type = 0;
892 if (CXXUnresolvedConstructExpr *
893 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
894 Type = UnCtor->getTypeSourceInfo();
895 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
896 Type = Tmp->getTypeSourceInfo();
897 }
898
899 if (!Type)
900 return cursor;
901
902 CXTranslationUnit TU = getCursorTU(cursor);
903 QualType Ty = Type->getType();
904 TypeLoc TL = Type->getTypeLoc();
905 SourceLocation Loc = TL.getBeginLoc();
906
907 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
908 Ty = ElabT->getNamedType();
909 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
910 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
911 }
912
913 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
914 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
915 if (const TagType *Tag = Ty->getAs<TagType>())
916 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
917 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
918 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
919
920 return cursor;
921}
922
Douglas Gregor283cae32010-01-15 21:56:13 +0000923bool cxcursor::operator==(CXCursor X, CXCursor Y) {
924 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
925 X.data[2] == Y.data[2];
Douglas Gregor2e331b92010-01-16 14:00:32 +0000926}
Ted Kremenek007a7c92010-11-01 23:26:51 +0000927
928// FIXME: Remove once we can model DeclGroups and their appropriate ranges
929// properly in the ASTs.
930bool cxcursor::isFirstInDeclGroup(CXCursor C) {
931 assert(clang_isDeclaration(C.kind));
932 return ((uintptr_t) (C.data[1])) != 0;
933}
934
Ted Kremenekeca099b2010-12-08 23:43:14 +0000935//===----------------------------------------------------------------------===//
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000936// libclang CXCursor APIs
937//===----------------------------------------------------------------------===//
938
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000939extern "C" {
940
941int clang_Cursor_isNull(CXCursor cursor) {
942 return clang_equalCursors(cursor, clang_getNullCursor());
943}
944
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000945CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
946 return getCursorTU(cursor);
947}
948
Argyrios Kyrtzidisfa865df2011-09-27 04:14:36 +0000949} // end: extern "C"
950
Argyrios Kyrtzidisb0d6eaa2011-09-27 00:30:30 +0000951//===----------------------------------------------------------------------===//
Ted Kremenekeca099b2010-12-08 23:43:14 +0000952// CXCursorSet.
953//===----------------------------------------------------------------------===//
954
955typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
956
957static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
958 return (CXCursorSet) setImpl;
959}
960static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
961 return (CXCursorSet_Impl*) set;
962}
963namespace llvm {
Ted Kremenekda6fb692010-12-09 00:33:41 +0000964template<> struct DenseMapInfo<CXCursor> {
Ted Kremenekeca099b2010-12-08 23:43:14 +0000965public:
966 static inline CXCursor getEmptyKey() {
967 return MakeCXCursorInvalid(CXCursor_InvalidFile);
968 }
969 static inline CXCursor getTombstoneKey() {
970 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
971 }
972 static inline unsigned getHashValue(const CXCursor &cursor) {
973 return llvm::DenseMapInfo<std::pair<void*,void*> >
974 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
975 }
976 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
977 return x.kind == y.kind &&
978 x.data[0] == y.data[0] &&
979 x.data[1] == y.data[1];
980 }
981};
982}
983
984extern "C" {
985CXCursorSet clang_createCXCursorSet() {
986 return packCXCursorSet(new CXCursorSet_Impl());
987}
988
989void clang_disposeCXCursorSet(CXCursorSet set) {
990 delete unpackCXCursorSet(set);
991}
992
993unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
994 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
995 if (!setImpl)
996 return 0;
997 return setImpl->find(cursor) == setImpl->end();
998}
999
Anders Carlssone8b3de02010-12-09 01:00:12 +00001000unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
Ted Kremenekeca099b2010-12-08 23:43:14 +00001001 // Do not insert invalid cursors into the set.
1002 if (cursor.kind >= CXCursor_FirstInvalid &&
1003 cursor.kind <= CXCursor_LastInvalid)
1004 return 1;
1005
1006 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1007 if (!setImpl)
1008 return 1;
1009 unsigned &entry = (*setImpl)[cursor];
1010 unsigned flag = entry == 0 ? 1 : 0;
1011 entry = 1;
1012 return flag;
1013}
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001014
1015CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1016 enum CXCursorKind kind = clang_getCursorKind(cursor);
1017 if (clang_isDeclaration(kind)) {
1018 Decl *decl = getCursorDecl(cursor);
1019 if (isa<NamedDecl>(decl)) {
1020 NamedDecl *namedDecl = (NamedDecl *)decl;
1021 ASTUnit *unit = getCursorASTUnit(cursor);
1022 if (unit->hasSema()) {
1023 Sema &S = unit->getSema();
1024 CodeCompletionAllocator *Allocator
1025 = unit->getCursorCompletionAllocator().getPtr();
1026 CodeCompletionResult Result(namedDecl);
1027 CodeCompletionString *String
1028 = Result.CreateCodeCompletionString(S, *Allocator);
1029 return String;
1030 }
1031 }
1032 }
1033 else if (kind == CXCursor_MacroDefinition) {
1034 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1035 const IdentifierInfo *MacroInfo = definition->getName();
1036 ASTUnit *unit = getCursorASTUnit(cursor);
1037 if (unit->hasSema()) {
1038 Sema &S = unit->getSema();
1039 CodeCompletionAllocator *Allocator
1040 = unit->getCursorCompletionAllocator().getPtr();
Douglas Gregoreaf4fba2011-08-10 16:34:38 +00001041 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001042 CodeCompletionString *String
1043 = Result.CreateCodeCompletionString(S, *Allocator);
1044 return String;
1045 }
1046 }
1047 return NULL;
1048}
1049
Ted Kremenekeca099b2010-12-08 23:43:14 +00001050} // end: extern "C"