1 //===- 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 //
10 // 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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "CXTranslationUnit.h"
17 #include "CXCursor.h"
18 #include "CXString.h"
19 #include "clang/Frontend/ASTUnit.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclCXX.h"
22 #include "clang/AST/DeclObjC.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/ExprCXX.h"
26 #include "clang/AST/ExprObjC.h"
27 #include "clang-c/Index.h"
28 #include "llvm/Support/ErrorHandling.h"
29
30 using namespace clang;
31 using namespace cxcursor;
32
MakeCXCursorInvalid(CXCursorKind K,CXTranslationUnit TU)33 CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
34 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
35 CXCursor C = { K, 0, { 0, 0, TU } };
36 return C;
37 }
38
GetCursorKind(const Attr * A)39 static CXCursorKind GetCursorKind(const Attr *A) {
40 assert(A && "Invalid arguments!");
41 switch (A->getKind()) {
42 default: break;
43 case attr::IBAction: return CXCursor_IBActionAttr;
44 case attr::IBOutlet: return CXCursor_IBOutletAttr;
45 case attr::IBOutletCollection: return CXCursor_IBOutletCollectionAttr;
46 case attr::Final: return CXCursor_CXXFinalAttr;
47 case attr::Override: return CXCursor_CXXOverrideAttr;
48 case attr::Annotate: return CXCursor_AnnotateAttr;
49 case attr::AsmLabel: return CXCursor_AsmLabelAttr;
50 }
51
52 return CXCursor_UnexposedAttr;
53 }
54
MakeCXCursor(const Attr * A,Decl * Parent,CXTranslationUnit TU)55 CXCursor cxcursor::MakeCXCursor(const Attr *A, Decl *Parent,
56 CXTranslationUnit TU) {
57 assert(A && Parent && TU && "Invalid arguments!");
58 CXCursor C = { GetCursorKind(A), 0, { Parent, (void*)A, TU } };
59 return C;
60 }
61
MakeCXCursor(Decl * D,CXTranslationUnit TU,SourceRange RegionOfInterest,bool FirstInDeclGroup)62 CXCursor cxcursor::MakeCXCursor(Decl *D, CXTranslationUnit TU,
63 SourceRange RegionOfInterest,
64 bool FirstInDeclGroup) {
65 assert(D && TU && "Invalid arguments!");
66
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 }};
88 return C;
89 }
90
MakeCXCursor(Stmt * S,Decl * Parent,CXTranslationUnit TU,SourceRange RegionOfInterest)91 CXCursor cxcursor::MakeCXCursor(Stmt *S, Decl *Parent, CXTranslationUnit TU,
92 SourceRange RegionOfInterest) {
93 assert(S && TU && "Invalid arguments!");
94 CXCursorKind K = CXCursor_NotImplemented;
95
96 switch (S->getStmtClass()) {
97 case Stmt::NoStmtClass:
98 break;
99
100 case Stmt::CaseStmtClass:
101 K = CXCursor_CaseStmt;
102 break;
103
104 case Stmt::DefaultStmtClass:
105 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
132 case Stmt::IndirectGotoStmtClass:
133 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::GCCAsmStmtClass:
149 K = CXCursor_GCCAsmStmt;
150 break;
151
152 case Stmt::MSAsmStmtClass:
153 K = CXCursor_MSAsmStmt;
154 break;
155
156 case Stmt::ObjCAtTryStmtClass:
157 K = CXCursor_ObjCAtTryStmt;
158 break;
159
160 case Stmt::ObjCAtCatchStmtClass:
161 K = CXCursor_ObjCAtCatchStmt;
162 break;
163
164 case Stmt::ObjCAtFinallyStmtClass:
165 K = CXCursor_ObjCAtFinallyStmt;
166 break;
167
168 case Stmt::ObjCAtThrowStmtClass:
169 K = CXCursor_ObjCAtThrowStmt;
170 break;
171
172 case Stmt::ObjCAtSynchronizedStmtClass:
173 K = CXCursor_ObjCAtSynchronizedStmt;
174 break;
175
176 case Stmt::ObjCAutoreleasePoolStmtClass:
177 K = CXCursor_ObjCAutoreleasePoolStmt;
178 break;
179
180 case Stmt::ObjCForCollectionStmtClass:
181 K = CXCursor_ObjCForCollectionStmt;
182 break;
183
184 case Stmt::CXXCatchStmtClass:
185 K = CXCursor_CXXCatchStmt;
186 break;
187
188 case Stmt::CXXTryStmtClass:
189 K = CXCursor_CXXTryStmt;
190 break;
191
192 case Stmt::CXXForRangeStmtClass:
193 K = CXCursor_CXXForRangeStmt;
194 break;
195
196 case Stmt::SEHTryStmtClass:
197 K = CXCursor_SEHTryStmt;
198 break;
199
200 case Stmt::SEHExceptStmtClass:
201 K = CXCursor_SEHExceptStmt;
202 break;
203
204 case Stmt::SEHFinallyStmtClass:
205 K = CXCursor_SEHFinallyStmt;
206 break;
207
208 case Stmt::ArrayTypeTraitExprClass:
209 case Stmt::AsTypeExprClass:
210 case Stmt::AtomicExprClass:
211 case Stmt::BinaryConditionalOperatorClass:
212 case Stmt::BinaryTypeTraitExprClass:
213 case Stmt::TypeTraitExprClass:
214 case Stmt::CXXBindTemporaryExprClass:
215 case Stmt::CXXDefaultArgExprClass:
216 case Stmt::CXXScalarValueInitExprClass:
217 case Stmt::CXXUuidofExprClass:
218 case Stmt::ChooseExprClass:
219 case Stmt::DesignatedInitExprClass:
220 case Stmt::ExprWithCleanupsClass:
221 case Stmt::ExpressionTraitExprClass:
222 case Stmt::ExtVectorElementExprClass:
223 case Stmt::ImplicitCastExprClass:
224 case Stmt::ImplicitValueInitExprClass:
225 case Stmt::MaterializeTemporaryExprClass:
226 case Stmt::ObjCIndirectCopyRestoreExprClass:
227 case Stmt::OffsetOfExprClass:
228 case Stmt::ParenListExprClass:
229 case Stmt::PredefinedExprClass:
230 case Stmt::ShuffleVectorExprClass:
231 case Stmt::UnaryExprOrTypeTraitExprClass:
232 case Stmt::UnaryTypeTraitExprClass:
233 case Stmt::VAArgExprClass:
234 case Stmt::ObjCArrayLiteralClass:
235 case Stmt::ObjCDictionaryLiteralClass:
236 case Stmt::ObjCBoxedExprClass:
237 case Stmt::ObjCSubscriptRefExprClass:
238 K = CXCursor_UnexposedExpr;
239 break;
240
241 case Stmt::OpaqueValueExprClass:
242 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
243 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
244 K = CXCursor_UnexposedExpr;
245 break;
246
247 case Stmt::PseudoObjectExprClass:
248 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
249 Parent, TU, RegionOfInterest);
250
251 case Stmt::CompoundStmtClass:
252 K = CXCursor_CompoundStmt;
253 break;
254
255 case Stmt::NullStmtClass:
256 K = CXCursor_NullStmt;
257 break;
258
259 case Stmt::LabelStmtClass:
260 K = CXCursor_LabelStmt;
261 break;
262
263 case Stmt::AttributedStmtClass:
264 K = CXCursor_UnexposedStmt;
265 break;
266
267 case Stmt::DeclStmtClass:
268 K = CXCursor_DeclStmt;
269 break;
270
271 case Stmt::IntegerLiteralClass:
272 K = CXCursor_IntegerLiteral;
273 break;
274
275 case Stmt::FloatingLiteralClass:
276 K = CXCursor_FloatingLiteral;
277 break;
278
279 case Stmt::ImaginaryLiteralClass:
280 K = CXCursor_ImaginaryLiteral;
281 break;
282
283 case Stmt::StringLiteralClass:
284 K = CXCursor_StringLiteral;
285 break;
286
287 case Stmt::CharacterLiteralClass:
288 K = CXCursor_CharacterLiteral;
289 break;
290
291 case Stmt::ParenExprClass:
292 K = CXCursor_ParenExpr;
293 break;
294
295 case Stmt::UnaryOperatorClass:
296 K = CXCursor_UnaryOperator;
297 break;
298
299 case Stmt::CXXNoexceptExprClass:
300 K = CXCursor_UnaryExpr;
301 break;
302
303 case Stmt::ArraySubscriptExprClass:
304 K = CXCursor_ArraySubscriptExpr;
305 break;
306
307 case Stmt::BinaryOperatorClass:
308 K = CXCursor_BinaryOperator;
309 break;
310
311 case Stmt::CompoundAssignOperatorClass:
312 K = CXCursor_CompoundAssignOperator;
313 break;
314
315 case Stmt::ConditionalOperatorClass:
316 K = CXCursor_ConditionalOperator;
317 break;
318
319 case Stmt::CStyleCastExprClass:
320 K = CXCursor_CStyleCastExpr;
321 break;
322
323 case Stmt::CompoundLiteralExprClass:
324 K = CXCursor_CompoundLiteralExpr;
325 break;
326
327 case Stmt::InitListExprClass:
328 K = CXCursor_InitListExpr;
329 break;
330
331 case Stmt::AddrLabelExprClass:
332 K = CXCursor_AddrLabelExpr;
333 break;
334
335 case Stmt::StmtExprClass:
336 K = CXCursor_StmtExpr;
337 break;
338
339 case Stmt::GenericSelectionExprClass:
340 K = CXCursor_GenericSelectionExpr;
341 break;
342
343 case Stmt::GNUNullExprClass:
344 K = CXCursor_GNUNullExpr;
345 break;
346
347 case Stmt::CXXStaticCastExprClass:
348 K = CXCursor_CXXStaticCastExpr;
349 break;
350
351 case Stmt::CXXDynamicCastExprClass:
352 K = CXCursor_CXXDynamicCastExpr;
353 break;
354
355 case Stmt::CXXReinterpretCastExprClass:
356 K = CXCursor_CXXReinterpretCastExpr;
357 break;
358
359 case Stmt::CXXConstCastExprClass:
360 K = CXCursor_CXXConstCastExpr;
361 break;
362
363 case Stmt::CXXFunctionalCastExprClass:
364 K = CXCursor_CXXFunctionalCastExpr;
365 break;
366
367 case Stmt::CXXTypeidExprClass:
368 K = CXCursor_CXXTypeidExpr;
369 break;
370
371 case Stmt::CXXBoolLiteralExprClass:
372 K = CXCursor_CXXBoolLiteralExpr;
373 break;
374
375 case Stmt::CXXNullPtrLiteralExprClass:
376 K = CXCursor_CXXNullPtrLiteralExpr;
377 break;
378
379 case Stmt::CXXThisExprClass:
380 K = CXCursor_CXXThisExpr;
381 break;
382
383 case Stmt::CXXThrowExprClass:
384 K = CXCursor_CXXThrowExpr;
385 break;
386
387 case Stmt::CXXNewExprClass:
388 K = CXCursor_CXXNewExpr;
389 break;
390
391 case Stmt::CXXDeleteExprClass:
392 K = CXCursor_CXXDeleteExpr;
393 break;
394
395 case Stmt::ObjCStringLiteralClass:
396 K = CXCursor_ObjCStringLiteral;
397 break;
398
399 case Stmt::ObjCEncodeExprClass:
400 K = CXCursor_ObjCEncodeExpr;
401 break;
402
403 case Stmt::ObjCSelectorExprClass:
404 K = CXCursor_ObjCSelectorExpr;
405 break;
406
407 case Stmt::ObjCProtocolExprClass:
408 K = CXCursor_ObjCProtocolExpr;
409 break;
410
411 case Stmt::ObjCBoolLiteralExprClass:
412 K = CXCursor_ObjCBoolLiteralExpr;
413 break;
414
415 case Stmt::ObjCBridgedCastExprClass:
416 K = CXCursor_ObjCBridgedCastExpr;
417 break;
418
419 case Stmt::BlockExprClass:
420 K = CXCursor_BlockExpr;
421 break;
422
423 case Stmt::PackExpansionExprClass:
424 K = CXCursor_PackExpansionExpr;
425 break;
426
427 case Stmt::SizeOfPackExprClass:
428 K = CXCursor_SizeOfPackExpr;
429 break;
430
431 case Stmt::DeclRefExprClass:
432 case Stmt::DependentScopeDeclRefExprClass:
433 case Stmt::SubstNonTypeTemplateParmExprClass:
434 case Stmt::SubstNonTypeTemplateParmPackExprClass:
435 case Stmt::UnresolvedLookupExprClass:
436 K = CXCursor_DeclRefExpr;
437 break;
438
439 case Stmt::CXXDependentScopeMemberExprClass:
440 case Stmt::CXXPseudoDestructorExprClass:
441 case Stmt::MemberExprClass:
442 case Stmt::ObjCIsaExprClass:
443 case Stmt::ObjCIvarRefExprClass:
444 case Stmt::ObjCPropertyRefExprClass:
445 case Stmt::UnresolvedMemberExprClass:
446 K = CXCursor_MemberRefExpr;
447 break;
448
449 case Stmt::CallExprClass:
450 case Stmt::CXXOperatorCallExprClass:
451 case Stmt::CXXMemberCallExprClass:
452 case Stmt::CUDAKernelCallExprClass:
453 case Stmt::CXXConstructExprClass:
454 case Stmt::CXXTemporaryObjectExprClass:
455 case Stmt::CXXUnresolvedConstructExprClass:
456 case Stmt::UserDefinedLiteralClass:
457 K = CXCursor_CallExpr;
458 break;
459
460 case Stmt::LambdaExprClass:
461 K = CXCursor_LambdaExpr;
462 break;
463
464 case Stmt::ObjCMessageExprClass: {
465 K = CXCursor_ObjCMessageExpr;
466 int SelectorIdIndex = -1;
467 // Check if cursor points to a selector id.
468 if (RegionOfInterest.isValid() &&
469 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
470 SmallVector<SourceLocation, 16> SelLocs;
471 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
472 SmallVector<SourceLocation, 16>::iterator
473 I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
474 if (I != SelLocs.end())
475 SelectorIdIndex = I - SelLocs.begin();
476 }
477 CXCursor C = { K, 0, { Parent, S, TU } };
478 return getSelectorIdentifierCursor(SelectorIdIndex, C);
479 }
480
481 case Stmt::MSDependentExistsStmtClass:
482 K = CXCursor_UnexposedStmt;
483 break;
484 }
485
486 CXCursor C = { K, 0, { Parent, S, TU } };
487 return C;
488 }
489
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl * Super,SourceLocation Loc,CXTranslationUnit TU)490 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
491 SourceLocation Loc,
492 CXTranslationUnit TU) {
493 assert(Super && TU && "Invalid arguments!");
494 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
495 CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
496 return C;
497 }
498
499 std::pair<ObjCInterfaceDecl *, SourceLocation>
getCursorObjCSuperClassRef(CXCursor C)500 cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
501 assert(C.kind == CXCursor_ObjCSuperClassRef);
502 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
503 SourceLocation::getFromRawEncoding(
504 reinterpret_cast<uintptr_t>(C.data[1])));
505 }
506
MakeCursorObjCProtocolRef(const ObjCProtocolDecl * Proto,SourceLocation Loc,CXTranslationUnit TU)507 CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
508 SourceLocation Loc,
509 CXTranslationUnit TU) {
510 assert(Proto && TU && "Invalid arguments!");
511 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
512 CXCursor C = { CXCursor_ObjCProtocolRef, 0, { (void*)Proto, RawLoc, TU } };
513 return C;
514 }
515
516 std::pair<ObjCProtocolDecl *, SourceLocation>
getCursorObjCProtocolRef(CXCursor C)517 cxcursor::getCursorObjCProtocolRef(CXCursor C) {
518 assert(C.kind == CXCursor_ObjCProtocolRef);
519 return std::make_pair(static_cast<ObjCProtocolDecl *>(C.data[0]),
520 SourceLocation::getFromRawEncoding(
521 reinterpret_cast<uintptr_t>(C.data[1])));
522 }
523
MakeCursorObjCClassRef(const ObjCInterfaceDecl * Class,SourceLocation Loc,CXTranslationUnit TU)524 CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
525 SourceLocation Loc,
526 CXTranslationUnit TU) {
527 // 'Class' can be null for invalid code.
528 if (!Class)
529 return MakeCXCursorInvalid(CXCursor_InvalidCode);
530 assert(TU && "Invalid arguments!");
531 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
532 CXCursor C = { CXCursor_ObjCClassRef, 0, { (void*)Class, RawLoc, TU } };
533 return C;
534 }
535
536 std::pair<ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C)537 cxcursor::getCursorObjCClassRef(CXCursor C) {
538 assert(C.kind == CXCursor_ObjCClassRef);
539 return std::make_pair(static_cast<ObjCInterfaceDecl *>(C.data[0]),
540 SourceLocation::getFromRawEncoding(
541 reinterpret_cast<uintptr_t>(C.data[1])));
542 }
543
MakeCursorTypeRef(const TypeDecl * Type,SourceLocation Loc,CXTranslationUnit TU)544 CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
545 CXTranslationUnit TU) {
546 assert(Type && TU && "Invalid arguments!");
547 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
548 CXCursor C = { CXCursor_TypeRef, 0, { (void*)Type, RawLoc, TU } };
549 return C;
550 }
551
552 std::pair<TypeDecl *, SourceLocation>
getCursorTypeRef(CXCursor C)553 cxcursor::getCursorTypeRef(CXCursor C) {
554 assert(C.kind == CXCursor_TypeRef);
555 return std::make_pair(static_cast<TypeDecl *>(C.data[0]),
556 SourceLocation::getFromRawEncoding(
557 reinterpret_cast<uintptr_t>(C.data[1])));
558 }
559
MakeCursorTemplateRef(const TemplateDecl * Template,SourceLocation Loc,CXTranslationUnit TU)560 CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
561 SourceLocation Loc,
562 CXTranslationUnit TU) {
563 assert(Template && TU && "Invalid arguments!");
564 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
565 CXCursor C = { CXCursor_TemplateRef, 0, { (void*)Template, RawLoc, TU } };
566 return C;
567 }
568
569 std::pair<TemplateDecl *, SourceLocation>
getCursorTemplateRef(CXCursor C)570 cxcursor::getCursorTemplateRef(CXCursor C) {
571 assert(C.kind == CXCursor_TemplateRef);
572 return std::make_pair(static_cast<TemplateDecl *>(C.data[0]),
573 SourceLocation::getFromRawEncoding(
574 reinterpret_cast<uintptr_t>(C.data[1])));
575 }
576
MakeCursorNamespaceRef(const NamedDecl * NS,SourceLocation Loc,CXTranslationUnit TU)577 CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
578 SourceLocation Loc,
579 CXTranslationUnit TU) {
580
581 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
582 "Invalid arguments!");
583 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
584 CXCursor C = { CXCursor_NamespaceRef, 0, { (void*)NS, RawLoc, TU } };
585 return C;
586 }
587
588 std::pair<NamedDecl *, SourceLocation>
getCursorNamespaceRef(CXCursor C)589 cxcursor::getCursorNamespaceRef(CXCursor C) {
590 assert(C.kind == CXCursor_NamespaceRef);
591 return std::make_pair(static_cast<NamedDecl *>(C.data[0]),
592 SourceLocation::getFromRawEncoding(
593 reinterpret_cast<uintptr_t>(C.data[1])));
594 }
595
MakeCursorVariableRef(const VarDecl * Var,SourceLocation Loc,CXTranslationUnit TU)596 CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
597 CXTranslationUnit TU) {
598
599 assert(Var && TU && "Invalid arguments!");
600 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
601 CXCursor C = { CXCursor_VariableRef, 0, { (void*)Var, RawLoc, TU } };
602 return C;
603 }
604
605 std::pair<VarDecl *, SourceLocation>
getCursorVariableRef(CXCursor C)606 cxcursor::getCursorVariableRef(CXCursor C) {
607 assert(C.kind == CXCursor_VariableRef);
608 return std::make_pair(static_cast<VarDecl *>(C.data[0]),
609 SourceLocation::getFromRawEncoding(
610 reinterpret_cast<uintptr_t>(C.data[1])));
611 }
612
MakeCursorMemberRef(const FieldDecl * Field,SourceLocation Loc,CXTranslationUnit TU)613 CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
614 CXTranslationUnit TU) {
615
616 assert(Field && TU && "Invalid arguments!");
617 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
618 CXCursor C = { CXCursor_MemberRef, 0, { (void*)Field, RawLoc, TU } };
619 return C;
620 }
621
622 std::pair<FieldDecl *, SourceLocation>
getCursorMemberRef(CXCursor C)623 cxcursor::getCursorMemberRef(CXCursor C) {
624 assert(C.kind == CXCursor_MemberRef);
625 return std::make_pair(static_cast<FieldDecl *>(C.data[0]),
626 SourceLocation::getFromRawEncoding(
627 reinterpret_cast<uintptr_t>(C.data[1])));
628 }
629
MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier * B,CXTranslationUnit TU)630 CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
631 CXTranslationUnit TU){
632 CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { (void*)B, 0, TU } };
633 return C;
634 }
635
getCursorCXXBaseSpecifier(CXCursor C)636 CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
637 assert(C.kind == CXCursor_CXXBaseSpecifier);
638 return static_cast<CXXBaseSpecifier*>(C.data[0]);
639 }
640
MakePreprocessingDirectiveCursor(SourceRange Range,CXTranslationUnit TU)641 CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
642 CXTranslationUnit TU) {
643 CXCursor C = { CXCursor_PreprocessingDirective, 0,
644 { reinterpret_cast<void *>(Range.getBegin().getRawEncoding()),
645 reinterpret_cast<void *>(Range.getEnd().getRawEncoding()),
646 TU }
647 };
648 return C;
649 }
650
getCursorPreprocessingDirective(CXCursor C)651 SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
652 assert(C.kind == CXCursor_PreprocessingDirective);
653 SourceRange Range = SourceRange(SourceLocation::getFromRawEncoding(
654 reinterpret_cast<uintptr_t> (C.data[0])),
655 SourceLocation::getFromRawEncoding(
656 reinterpret_cast<uintptr_t> (C.data[1])));
657 ASTUnit *TU = getCursorASTUnit(C);
658 return TU->mapRangeFromPreamble(Range);
659 }
660
MakeMacroDefinitionCursor(MacroDefinition * MI,CXTranslationUnit TU)661 CXCursor cxcursor::MakeMacroDefinitionCursor(MacroDefinition *MI,
662 CXTranslationUnit TU) {
663 CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
664 return C;
665 }
666
getCursorMacroDefinition(CXCursor C)667 MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
668 assert(C.kind == CXCursor_MacroDefinition);
669 return static_cast<MacroDefinition *>(C.data[0]);
670 }
671
MakeMacroExpansionCursor(MacroExpansion * MI,CXTranslationUnit TU)672 CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
673 CXTranslationUnit TU) {
674 CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
675 return C;
676 }
677
getCursorMacroExpansion(CXCursor C)678 MacroExpansion *cxcursor::getCursorMacroExpansion(CXCursor C) {
679 assert(C.kind == CXCursor_MacroExpansion);
680 return static_cast<MacroExpansion *>(C.data[0]);
681 }
682
MakeInclusionDirectiveCursor(InclusionDirective * ID,CXTranslationUnit TU)683 CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
684 CXTranslationUnit TU) {
685 CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
686 return C;
687 }
688
getCursorInclusionDirective(CXCursor C)689 InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
690 assert(C.kind == CXCursor_InclusionDirective);
691 return static_cast<InclusionDirective *>(C.data[0]);
692 }
693
MakeCursorLabelRef(LabelStmt * Label,SourceLocation Loc,CXTranslationUnit TU)694 CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
695 CXTranslationUnit TU) {
696
697 assert(Label && TU && "Invalid arguments!");
698 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
699 CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
700 return C;
701 }
702
703 std::pair<LabelStmt*, SourceLocation>
getCursorLabelRef(CXCursor C)704 cxcursor::getCursorLabelRef(CXCursor C) {
705 assert(C.kind == CXCursor_LabelRef);
706 return std::make_pair(static_cast<LabelStmt *>(C.data[0]),
707 SourceLocation::getFromRawEncoding(
708 reinterpret_cast<uintptr_t>(C.data[1])));
709 }
710
MakeCursorOverloadedDeclRef(OverloadExpr * E,CXTranslationUnit TU)711 CXCursor cxcursor::MakeCursorOverloadedDeclRef(OverloadExpr *E,
712 CXTranslationUnit TU) {
713 assert(E && TU && "Invalid arguments!");
714 OverloadedDeclRefStorage Storage(E);
715 void *RawLoc = reinterpret_cast<void *>(E->getNameLoc().getRawEncoding());
716 CXCursor C = {
717 CXCursor_OverloadedDeclRef, 0,
718 { Storage.getOpaqueValue(), RawLoc, TU }
719 };
720 return C;
721 }
722
MakeCursorOverloadedDeclRef(Decl * D,SourceLocation Loc,CXTranslationUnit TU)723 CXCursor cxcursor::MakeCursorOverloadedDeclRef(Decl *D,
724 SourceLocation Loc,
725 CXTranslationUnit TU) {
726 assert(D && TU && "Invalid arguments!");
727 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
728 OverloadedDeclRefStorage Storage(D);
729 CXCursor C = {
730 CXCursor_OverloadedDeclRef, 0,
731 { Storage.getOpaqueValue(), RawLoc, TU }
732 };
733 return C;
734 }
735
MakeCursorOverloadedDeclRef(TemplateName Name,SourceLocation Loc,CXTranslationUnit TU)736 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
737 SourceLocation Loc,
738 CXTranslationUnit TU) {
739 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
740 void *RawLoc = reinterpret_cast<void *>(Loc.getRawEncoding());
741 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
742 CXCursor C = {
743 CXCursor_OverloadedDeclRef, 0,
744 { Storage.getOpaqueValue(), RawLoc, TU }
745 };
746 return C;
747 }
748
749 std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
getCursorOverloadedDeclRef(CXCursor C)750 cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
751 assert(C.kind == CXCursor_OverloadedDeclRef);
752 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(C.data[0]),
753 SourceLocation::getFromRawEncoding(
754 reinterpret_cast<uintptr_t>(C.data[1])));
755 }
756
getCursorDecl(CXCursor Cursor)757 Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
758 return (Decl *)Cursor.data[0];
759 }
760
getCursorExpr(CXCursor Cursor)761 Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
762 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
763 }
764
getCursorStmt(CXCursor Cursor)765 Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
766 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
767 Cursor.kind == CXCursor_ObjCProtocolRef ||
768 Cursor.kind == CXCursor_ObjCClassRef)
769 return 0;
770
771 return (Stmt *)Cursor.data[1];
772 }
773
getCursorAttr(CXCursor Cursor)774 Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
775 return (Attr *)Cursor.data[1];
776 }
777
getCursorParentDecl(CXCursor Cursor)778 Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
779 return (Decl *)Cursor.data[0];
780 }
781
getCursorContext(CXCursor Cursor)782 ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
783 return getCursorASTUnit(Cursor)->getASTContext();
784 }
785
getCursorASTUnit(CXCursor Cursor)786 ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
787 CXTranslationUnit TU = static_cast<CXTranslationUnit>(Cursor.data[2]);
788 if (!TU)
789 return 0;
790 return static_cast<ASTUnit *>(TU->TUData);
791 }
792
getCursorTU(CXCursor Cursor)793 CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
794 return static_cast<CXTranslationUnit>(Cursor.data[2]);
795 }
796
CollectOverriddenMethodsRecurse(CXTranslationUnit TU,ObjCContainerDecl * Container,ObjCMethodDecl * Method,SmallVectorImpl<CXCursor> & Methods,bool MovedToSuper)797 static void CollectOverriddenMethodsRecurse(CXTranslationUnit TU,
798 ObjCContainerDecl *Container,
799 ObjCMethodDecl *Method,
800 SmallVectorImpl<CXCursor> &Methods,
801 bool MovedToSuper) {
802 if (!Container)
803 return;
804
805 // In categories look for overriden methods from protocols. A method from
806 // category is not "overriden" since it is considered as the "same" method
807 // (same USR) as the one from the interface.
808 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
809 // Check whether we have a matching method at this category but only if we
810 // are at the super class level.
811 if (MovedToSuper)
812 if (ObjCMethodDecl *
813 Overridden = Container->getMethod(Method->getSelector(),
814 Method->isInstanceMethod()))
815 if (Method != Overridden) {
816 // We found an override at this category; there is no need to look
817 // into its protocols.
818 Methods.push_back(MakeCXCursor(Overridden, TU));
819 return;
820 }
821
822 for (ObjCCategoryDecl::protocol_iterator P = Category->protocol_begin(),
823 PEnd = Category->protocol_end();
824 P != PEnd; ++P)
825 CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper);
826 return;
827 }
828
829 // Check whether we have a matching method at this level.
830 if (ObjCMethodDecl *Overridden = Container->getMethod(Method->getSelector(),
831 Method->isInstanceMethod()))
832 if (Method != Overridden) {
833 // We found an override at this level; there is no need to look
834 // into other protocols or categories.
835 Methods.push_back(MakeCXCursor(Overridden, TU));
836 return;
837 }
838
839 if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(Container)) {
840 for (ObjCProtocolDecl::protocol_iterator P = Protocol->protocol_begin(),
841 PEnd = Protocol->protocol_end();
842 P != PEnd; ++P)
843 CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper);
844 }
845
846 if (ObjCInterfaceDecl *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
847 for (ObjCInterfaceDecl::protocol_iterator P = Interface->protocol_begin(),
848 PEnd = Interface->protocol_end();
849 P != PEnd; ++P)
850 CollectOverriddenMethodsRecurse(TU, *P, Method, Methods, MovedToSuper);
851
852 for (ObjCCategoryDecl *Category = Interface->getCategoryList();
853 Category; Category = Category->getNextClassCategory())
854 CollectOverriddenMethodsRecurse(TU, Category, Method, Methods,
855 MovedToSuper);
856
857 if (ObjCInterfaceDecl *Super = Interface->getSuperClass())
858 return CollectOverriddenMethodsRecurse(TU, Super, Method, Methods,
859 /*MovedToSuper=*/true);
860 }
861 }
862
CollectOverriddenMethods(CXTranslationUnit TU,ObjCContainerDecl * Container,ObjCMethodDecl * Method,SmallVectorImpl<CXCursor> & Methods)863 static inline void CollectOverriddenMethods(CXTranslationUnit TU,
864 ObjCContainerDecl *Container,
865 ObjCMethodDecl *Method,
866 SmallVectorImpl<CXCursor> &Methods) {
867 CollectOverriddenMethodsRecurse(TU, Container, Method, Methods,
868 /*MovedToSuper=*/false);
869 }
870
collectOverriddenMethodsSlow(CXTranslationUnit TU,ObjCMethodDecl * Method,SmallVectorImpl<CXCursor> & overridden)871 static void collectOverriddenMethodsSlow(CXTranslationUnit TU,
872 ObjCMethodDecl *Method,
873 SmallVectorImpl<CXCursor> &overridden) {
874 assert(Method->isOverriding());
875
876 if (ObjCProtocolDecl *
877 ProtD = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
878 CollectOverriddenMethods(TU, ProtD, Method, overridden);
879
880 } else if (ObjCImplDecl *
881 IMD = dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
882 ObjCInterfaceDecl *ID = IMD->getClassInterface();
883 if (!ID)
884 return;
885 // Start searching for overridden methods using the method from the
886 // interface as starting point.
887 if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
888 Method->isInstanceMethod()))
889 Method = IFaceMeth;
890 CollectOverriddenMethods(TU, ID, Method, overridden);
891
892 } else if (ObjCCategoryDecl *
893 CatD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
894 ObjCInterfaceDecl *ID = CatD->getClassInterface();
895 if (!ID)
896 return;
897 // Start searching for overridden methods using the method from the
898 // interface as starting point.
899 if (ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
900 Method->isInstanceMethod()))
901 Method = IFaceMeth;
902 CollectOverriddenMethods(TU, ID, Method, overridden);
903
904 } else {
905 CollectOverriddenMethods(TU,
906 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
907 Method, overridden);
908 }
909 }
910
collectOnCategoriesAfterLocation(SourceLocation Loc,ObjCInterfaceDecl * Class,CXTranslationUnit TU,ObjCMethodDecl * Method,SmallVectorImpl<CXCursor> & Methods)911 static void collectOnCategoriesAfterLocation(SourceLocation Loc,
912 ObjCInterfaceDecl *Class,
913 CXTranslationUnit TU,
914 ObjCMethodDecl *Method,
915 SmallVectorImpl<CXCursor> &Methods) {
916 if (!Class)
917 return;
918
919 SourceManager &SM = static_cast<ASTUnit *>(TU->TUData)->getSourceManager();
920 for (ObjCCategoryDecl *Category = Class->getCategoryList();
921 Category; Category = Category->getNextClassCategory())
922 if (SM.isBeforeInTranslationUnit(Loc, Category->getLocation()))
923 CollectOverriddenMethodsRecurse(TU, Category, Method, Methods, true);
924
925 collectOnCategoriesAfterLocation(Loc, Class->getSuperClass(), TU,
926 Method, Methods);
927 }
928
929 /// \brief Faster collection that is enabled when ObjCMethodDecl::isOverriding()
930 /// returns false.
931 /// You'd think that in that case there are no overrides but categories can
932 /// "introduce" new overridden methods that are missed by Sema because the
933 /// overrides lookup that it does for methods, inside implementations, will
934 /// stop at the interface level (if there is a method there) and not look
935 /// further in super classes.
collectOverriddenMethodsFast(CXTranslationUnit TU,ObjCMethodDecl * Method,SmallVectorImpl<CXCursor> & Methods)936 static void collectOverriddenMethodsFast(CXTranslationUnit TU,
937 ObjCMethodDecl *Method,
938 SmallVectorImpl<CXCursor> &Methods) {
939 assert(!Method->isOverriding());
940
941 ObjCContainerDecl *ContD = cast<ObjCContainerDecl>(Method->getDeclContext());
942 if (isa<ObjCInterfaceDecl>(ContD) || isa<ObjCProtocolDecl>(ContD))
943 return;
944 ObjCInterfaceDecl *Class = Method->getClassInterface();
945 if (!Class)
946 return;
947
948 collectOnCategoriesAfterLocation(Class->getLocation(), Class->getSuperClass(),
949 TU, Method, Methods);
950 }
951
getOverriddenCursors(CXCursor cursor,SmallVectorImpl<CXCursor> & overridden)952 void cxcursor::getOverriddenCursors(CXCursor cursor,
953 SmallVectorImpl<CXCursor> &overridden) {
954 assert(clang_isDeclaration(cursor.kind));
955 Decl *D = getCursorDecl(cursor);
956 if (!D)
957 return;
958
959 // Handle C++ member functions.
960 CXTranslationUnit TU = getCursorTU(cursor);
961 if (CXXMethodDecl *CXXMethod = dyn_cast<CXXMethodDecl>(D)) {
962 for (CXXMethodDecl::method_iterator
963 M = CXXMethod->begin_overridden_methods(),
964 MEnd = CXXMethod->end_overridden_methods();
965 M != MEnd; ++M)
966 overridden.push_back(MakeCXCursor(const_cast<CXXMethodDecl*>(*M), TU));
967 return;
968 }
969
970 ObjCMethodDecl *Method = dyn_cast<ObjCMethodDecl>(D);
971 if (!Method)
972 return;
973
974 if (Method->isRedeclaration()) {
975 Method = cast<ObjCContainerDecl>(Method->getDeclContext())->
976 getMethod(Method->getSelector(), Method->isInstanceMethod());
977 }
978
979 if (!Method->isOverriding()) {
980 collectOverriddenMethodsFast(TU, Method, overridden);
981 } else {
982 collectOverriddenMethodsSlow(TU, Method, overridden);
983 assert(!overridden.empty() &&
984 "ObjCMethodDecl's overriding bit is not as expected");
985 }
986 }
987
988 std::pair<int, SourceLocation>
getSelectorIdentifierIndexAndLoc(CXCursor cursor)989 cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
990 if (cursor.kind == CXCursor_ObjCMessageExpr) {
991 if (cursor.xdata != -1)
992 return std::make_pair(cursor.xdata,
993 cast<ObjCMessageExpr>(getCursorExpr(cursor))
994 ->getSelectorLoc(cursor.xdata));
995 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
996 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
997 if (cursor.xdata != -1)
998 return std::make_pair(cursor.xdata,
999 cast<ObjCMethodDecl>(getCursorDecl(cursor))
1000 ->getSelectorLoc(cursor.xdata));
1001 }
1002
1003 return std::make_pair(-1, SourceLocation());
1004 }
1005
getSelectorIdentifierCursor(int SelIdx,CXCursor cursor)1006 CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
1007 CXCursor newCursor = cursor;
1008
1009 if (cursor.kind == CXCursor_ObjCMessageExpr) {
1010 if (SelIdx == -1 ||
1011 unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
1012 ->getNumSelectorLocs())
1013 newCursor.xdata = -1;
1014 else
1015 newCursor.xdata = SelIdx;
1016 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1017 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1018 if (SelIdx == -1 ||
1019 unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
1020 ->getNumSelectorLocs())
1021 newCursor.xdata = -1;
1022 else
1023 newCursor.xdata = SelIdx;
1024 }
1025
1026 return newCursor;
1027 }
1028
getTypeRefCursor(CXCursor cursor)1029 CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1030 if (cursor.kind != CXCursor_CallExpr)
1031 return cursor;
1032
1033 if (cursor.xdata == 0)
1034 return cursor;
1035
1036 Expr *E = getCursorExpr(cursor);
1037 TypeSourceInfo *Type = 0;
1038 if (CXXUnresolvedConstructExpr *
1039 UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
1040 Type = UnCtor->getTypeSourceInfo();
1041 } else if (CXXTemporaryObjectExpr *Tmp = dyn_cast<CXXTemporaryObjectExpr>(E)){
1042 Type = Tmp->getTypeSourceInfo();
1043 }
1044
1045 if (!Type)
1046 return cursor;
1047
1048 CXTranslationUnit TU = getCursorTU(cursor);
1049 QualType Ty = Type->getType();
1050 TypeLoc TL = Type->getTypeLoc();
1051 SourceLocation Loc = TL.getBeginLoc();
1052
1053 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
1054 Ty = ElabT->getNamedType();
1055 ElaboratedTypeLoc ElabTL = cast<ElaboratedTypeLoc>(TL);
1056 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
1057 }
1058
1059 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
1060 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
1061 if (const TagType *Tag = Ty->getAs<TagType>())
1062 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
1063 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
1064 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
1065
1066 return cursor;
1067 }
1068
operator ==(CXCursor X,CXCursor Y)1069 bool cxcursor::operator==(CXCursor X, CXCursor Y) {
1070 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
1071 X.data[2] == Y.data[2];
1072 }
1073
1074 // FIXME: Remove once we can model DeclGroups and their appropriate ranges
1075 // properly in the ASTs.
isFirstInDeclGroup(CXCursor C)1076 bool cxcursor::isFirstInDeclGroup(CXCursor C) {
1077 assert(clang_isDeclaration(C.kind));
1078 return ((uintptr_t) (C.data[1])) != 0;
1079 }
1080
1081 //===----------------------------------------------------------------------===//
1082 // libclang CXCursor APIs
1083 //===----------------------------------------------------------------------===//
1084
1085 extern "C" {
1086
clang_Cursor_isNull(CXCursor cursor)1087 int clang_Cursor_isNull(CXCursor cursor) {
1088 return clang_equalCursors(cursor, clang_getNullCursor());
1089 }
1090
clang_Cursor_getTranslationUnit(CXCursor cursor)1091 CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
1092 return getCursorTU(cursor);
1093 }
1094
clang_Cursor_getNumArguments(CXCursor C)1095 int clang_Cursor_getNumArguments(CXCursor C) {
1096 if (clang_isDeclaration(C.kind)) {
1097 Decl *D = cxcursor::getCursorDecl(C);
1098 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1099 return MD->param_size();
1100 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1101 return FD->param_size();
1102 }
1103
1104 return -1;
1105 }
1106
clang_Cursor_getArgument(CXCursor C,unsigned i)1107 CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1108 if (clang_isDeclaration(C.kind)) {
1109 Decl *D = cxcursor::getCursorDecl(C);
1110 if (ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
1111 if (i < MD->param_size())
1112 return cxcursor::MakeCXCursor(MD->param_begin()[i],
1113 cxcursor::getCursorTU(C));
1114 } else if (FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1115 if (i < FD->param_size())
1116 return cxcursor::MakeCXCursor(FD->param_begin()[i],
1117 cxcursor::getCursorTU(C));
1118 }
1119 }
1120
1121 return clang_getNullCursor();
1122 }
1123
1124 } // end: extern "C"
1125
1126 //===----------------------------------------------------------------------===//
1127 // CXCursorSet.
1128 //===----------------------------------------------------------------------===//
1129
1130 typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1131
packCXCursorSet(CXCursorSet_Impl * setImpl)1132 static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1133 return (CXCursorSet) setImpl;
1134 }
unpackCXCursorSet(CXCursorSet set)1135 static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1136 return (CXCursorSet_Impl*) set;
1137 }
1138 namespace llvm {
1139 template<> struct DenseMapInfo<CXCursor> {
1140 public:
getEmptyKeyllvm::DenseMapInfo1141 static inline CXCursor getEmptyKey() {
1142 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1143 }
getTombstoneKeyllvm::DenseMapInfo1144 static inline CXCursor getTombstoneKey() {
1145 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1146 }
getHashValuellvm::DenseMapInfo1147 static inline unsigned getHashValue(const CXCursor &cursor) {
1148 return llvm::DenseMapInfo<std::pair<void*,void*> >
1149 ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1150 }
isEqualllvm::DenseMapInfo1151 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1152 return x.kind == y.kind &&
1153 x.data[0] == y.data[0] &&
1154 x.data[1] == y.data[1];
1155 }
1156 };
1157 }
1158
1159 extern "C" {
clang_createCXCursorSet()1160 CXCursorSet clang_createCXCursorSet() {
1161 return packCXCursorSet(new CXCursorSet_Impl());
1162 }
1163
clang_disposeCXCursorSet(CXCursorSet set)1164 void clang_disposeCXCursorSet(CXCursorSet set) {
1165 delete unpackCXCursorSet(set);
1166 }
1167
clang_CXCursorSet_contains(CXCursorSet set,CXCursor cursor)1168 unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1169 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1170 if (!setImpl)
1171 return 0;
1172 return setImpl->find(cursor) == setImpl->end();
1173 }
1174
clang_CXCursorSet_insert(CXCursorSet set,CXCursor cursor)1175 unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1176 // Do not insert invalid cursors into the set.
1177 if (cursor.kind >= CXCursor_FirstInvalid &&
1178 cursor.kind <= CXCursor_LastInvalid)
1179 return 1;
1180
1181 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1182 if (!setImpl)
1183 return 1;
1184 unsigned &entry = (*setImpl)[cursor];
1185 unsigned flag = entry == 0 ? 1 : 0;
1186 entry = 1;
1187 return flag;
1188 }
1189
clang_getCursorCompletionString(CXCursor cursor)1190 CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1191 enum CXCursorKind kind = clang_getCursorKind(cursor);
1192 if (clang_isDeclaration(kind)) {
1193 Decl *decl = getCursorDecl(cursor);
1194 if (NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
1195 ASTUnit *unit = getCursorASTUnit(cursor);
1196 CodeCompletionResult Result(namedDecl);
1197 CodeCompletionString *String
1198 = Result.CreateCodeCompletionString(unit->getASTContext(),
1199 unit->getPreprocessor(),
1200 unit->getCodeCompletionTUInfo().getAllocator(),
1201 unit->getCodeCompletionTUInfo(),
1202 true);
1203 return String;
1204 }
1205 }
1206 else if (kind == CXCursor_MacroDefinition) {
1207 MacroDefinition *definition = getCursorMacroDefinition(cursor);
1208 const IdentifierInfo *MacroInfo = definition->getName();
1209 ASTUnit *unit = getCursorASTUnit(cursor);
1210 CodeCompletionResult Result(const_cast<IdentifierInfo *>(MacroInfo));
1211 CodeCompletionString *String
1212 = Result.CreateCodeCompletionString(unit->getASTContext(),
1213 unit->getPreprocessor(),
1214 unit->getCodeCompletionTUInfo().getAllocator(),
1215 unit->getCodeCompletionTUInfo(),
1216 false);
1217 return String;
1218 }
1219 return NULL;
1220 }
1221 } // end: extern C.
1222
1223 namespace {
1224 struct OverridenCursorsPool {
1225 typedef llvm::SmallVector<CXCursor, 2> CursorVec;
1226 std::vector<CursorVec*> AllCursors;
1227 std::vector<CursorVec*> AvailableCursors;
1228
~OverridenCursorsPool__anondcd4fa400111::OverridenCursorsPool1229 ~OverridenCursorsPool() {
1230 for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1231 E = AllCursors.end(); I != E; ++I) {
1232 delete *I;
1233 }
1234 }
1235 };
1236 }
1237
createOverridenCXCursorsPool()1238 void *cxcursor::createOverridenCXCursorsPool() {
1239 return new OverridenCursorsPool();
1240 }
1241
disposeOverridenCXCursorsPool(void * pool)1242 void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1243 delete static_cast<OverridenCursorsPool*>(pool);
1244 }
1245
1246 extern "C" {
clang_getOverriddenCursors(CXCursor cursor,CXCursor ** overridden,unsigned * num_overridden)1247 void clang_getOverriddenCursors(CXCursor cursor,
1248 CXCursor **overridden,
1249 unsigned *num_overridden) {
1250 if (overridden)
1251 *overridden = 0;
1252 if (num_overridden)
1253 *num_overridden = 0;
1254
1255 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1256
1257 if (!overridden || !num_overridden || !TU)
1258 return;
1259
1260 if (!clang_isDeclaration(cursor.kind))
1261 return;
1262
1263 OverridenCursorsPool &pool =
1264 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1265
1266 OverridenCursorsPool::CursorVec *Vec = 0;
1267
1268 if (!pool.AvailableCursors.empty()) {
1269 Vec = pool.AvailableCursors.back();
1270 pool.AvailableCursors.pop_back();
1271 }
1272 else {
1273 Vec = new OverridenCursorsPool::CursorVec();
1274 pool.AllCursors.push_back(Vec);
1275 }
1276
1277 // Clear out the vector, but don't free the memory contents. This
1278 // reduces malloc() traffic.
1279 Vec->clear();
1280
1281 // Use the first entry to contain a back reference to the vector.
1282 // This is a complete hack.
1283 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1284 backRefCursor.data[0] = Vec;
1285 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1286 Vec->push_back(backRefCursor);
1287
1288 // Get the overriden cursors.
1289 cxcursor::getOverriddenCursors(cursor, *Vec);
1290
1291 // Did we get any overriden cursors? If not, return Vec to the pool
1292 // of available cursor vectors.
1293 if (Vec->size() == 1) {
1294 pool.AvailableCursors.push_back(Vec);
1295 return;
1296 }
1297
1298 // Now tell the caller about the overriden cursors.
1299 assert(Vec->size() > 1);
1300 *overridden = &((*Vec)[1]);
1301 *num_overridden = Vec->size() - 1;
1302 }
1303
clang_disposeOverriddenCursors(CXCursor * overridden)1304 void clang_disposeOverriddenCursors(CXCursor *overridden) {
1305 if (!overridden)
1306 return;
1307
1308 // Use pointer arithmetic to get back the first faux entry
1309 // which has a back-reference to the TU and the vector.
1310 --overridden;
1311 OverridenCursorsPool::CursorVec *Vec =
1312 static_cast<OverridenCursorsPool::CursorVec*>(overridden->data[0]);
1313 CXTranslationUnit TU = getCursorTU(*overridden);
1314
1315 assert(Vec && TU);
1316
1317 OverridenCursorsPool &pool =
1318 *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1319
1320 pool.AvailableCursors.push_back(Vec);
1321 }
1322
clang_Cursor_isDynamicCall(CXCursor C)1323 int clang_Cursor_isDynamicCall(CXCursor C) {
1324 const Expr *E = 0;
1325 if (clang_isExpression(C.kind))
1326 E = getCursorExpr(C);
1327 if (!E)
1328 return 0;
1329
1330 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1331 return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1332
1333 const MemberExpr *ME = 0;
1334 if (isa<MemberExpr>(E))
1335 ME = cast<MemberExpr>(E);
1336 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1337 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1338
1339 if (ME) {
1340 if (const CXXMethodDecl *
1341 MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1342 return MD->isVirtual() && !ME->hasQualifier();
1343 }
1344
1345 return 0;
1346 }
1347
1348 } // end: extern "C"
1349