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