• 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       SmallVectorImpl<SourceLocation>::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::CXXDefaultInitExprClass:
219   case Stmt::CXXStdInitializerListExprClass:
220   case Stmt::CXXScalarValueInitExprClass:
221   case Stmt::CXXUuidofExprClass:
222   case Stmt::ChooseExprClass:
223   case Stmt::DesignatedInitExprClass:
224   case Stmt::ExprWithCleanupsClass:
225   case Stmt::ExpressionTraitExprClass:
226   case Stmt::ExtVectorElementExprClass:
227   case Stmt::ImplicitCastExprClass:
228   case Stmt::ImplicitValueInitExprClass:
229   case Stmt::MaterializeTemporaryExprClass:
230   case Stmt::ObjCIndirectCopyRestoreExprClass:
231   case Stmt::OffsetOfExprClass:
232   case Stmt::ParenListExprClass:
233   case Stmt::PredefinedExprClass:
234   case Stmt::ShuffleVectorExprClass:
235   case Stmt::UnaryExprOrTypeTraitExprClass:
236   case Stmt::UnaryTypeTraitExprClass:
237   case Stmt::VAArgExprClass:
238   case Stmt::ObjCArrayLiteralClass:
239   case Stmt::ObjCDictionaryLiteralClass:
240   case Stmt::ObjCBoxedExprClass:
241   case Stmt::ObjCSubscriptRefExprClass:
242     K = CXCursor_UnexposedExpr;
243     break;
244 
245   case Stmt::OpaqueValueExprClass:
246     if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
247       return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
248     K = CXCursor_UnexposedExpr;
249     break;
250 
251   case Stmt::PseudoObjectExprClass:
252     return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(),
253                         Parent, TU, RegionOfInterest);
254 
255   case Stmt::CompoundStmtClass:
256     K = CXCursor_CompoundStmt;
257     break;
258 
259   case Stmt::NullStmtClass:
260     K = CXCursor_NullStmt;
261     break;
262 
263   case Stmt::LabelStmtClass:
264     K = CXCursor_LabelStmt;
265     break;
266 
267   case Stmt::AttributedStmtClass:
268     K = CXCursor_UnexposedStmt;
269     break;
270 
271   case Stmt::DeclStmtClass:
272     K = CXCursor_DeclStmt;
273     break;
274 
275   case Stmt::CapturedStmtClass:
276     K = CXCursor_UnexposedStmt;
277     break;
278 
279   case Stmt::IntegerLiteralClass:
280     K = CXCursor_IntegerLiteral;
281     break;
282 
283   case Stmt::FloatingLiteralClass:
284     K = CXCursor_FloatingLiteral;
285     break;
286 
287   case Stmt::ImaginaryLiteralClass:
288     K = CXCursor_ImaginaryLiteral;
289     break;
290 
291   case Stmt::StringLiteralClass:
292     K = CXCursor_StringLiteral;
293     break;
294 
295   case Stmt::CharacterLiteralClass:
296     K = CXCursor_CharacterLiteral;
297     break;
298 
299   case Stmt::ParenExprClass:
300     K = CXCursor_ParenExpr;
301     break;
302 
303   case Stmt::UnaryOperatorClass:
304     K = CXCursor_UnaryOperator;
305     break;
306 
307   case Stmt::CXXNoexceptExprClass:
308     K = CXCursor_UnaryExpr;
309     break;
310 
311   case Stmt::ArraySubscriptExprClass:
312     K = CXCursor_ArraySubscriptExpr;
313     break;
314 
315   case Stmt::BinaryOperatorClass:
316     K = CXCursor_BinaryOperator;
317     break;
318 
319   case Stmt::CompoundAssignOperatorClass:
320     K = CXCursor_CompoundAssignOperator;
321     break;
322 
323   case Stmt::ConditionalOperatorClass:
324     K = CXCursor_ConditionalOperator;
325     break;
326 
327   case Stmt::CStyleCastExprClass:
328     K = CXCursor_CStyleCastExpr;
329     break;
330 
331   case Stmt::CompoundLiteralExprClass:
332     K = CXCursor_CompoundLiteralExpr;
333     break;
334 
335   case Stmt::InitListExprClass:
336     K = CXCursor_InitListExpr;
337     break;
338 
339   case Stmt::AddrLabelExprClass:
340     K = CXCursor_AddrLabelExpr;
341     break;
342 
343   case Stmt::StmtExprClass:
344     K = CXCursor_StmtExpr;
345     break;
346 
347   case Stmt::GenericSelectionExprClass:
348     K = CXCursor_GenericSelectionExpr;
349     break;
350 
351   case Stmt::GNUNullExprClass:
352     K = CXCursor_GNUNullExpr;
353     break;
354 
355   case Stmt::CXXStaticCastExprClass:
356     K = CXCursor_CXXStaticCastExpr;
357     break;
358 
359   case Stmt::CXXDynamicCastExprClass:
360     K = CXCursor_CXXDynamicCastExpr;
361     break;
362 
363   case Stmt::CXXReinterpretCastExprClass:
364     K = CXCursor_CXXReinterpretCastExpr;
365     break;
366 
367   case Stmt::CXXConstCastExprClass:
368     K = CXCursor_CXXConstCastExpr;
369     break;
370 
371   case Stmt::CXXFunctionalCastExprClass:
372     K = CXCursor_CXXFunctionalCastExpr;
373     break;
374 
375   case Stmt::CXXTypeidExprClass:
376     K = CXCursor_CXXTypeidExpr;
377     break;
378 
379   case Stmt::CXXBoolLiteralExprClass:
380     K = CXCursor_CXXBoolLiteralExpr;
381     break;
382 
383   case Stmt::CXXNullPtrLiteralExprClass:
384     K = CXCursor_CXXNullPtrLiteralExpr;
385     break;
386 
387   case Stmt::CXXThisExprClass:
388     K = CXCursor_CXXThisExpr;
389     break;
390 
391   case Stmt::CXXThrowExprClass:
392     K = CXCursor_CXXThrowExpr;
393     break;
394 
395   case Stmt::CXXNewExprClass:
396     K = CXCursor_CXXNewExpr;
397     break;
398 
399   case Stmt::CXXDeleteExprClass:
400     K = CXCursor_CXXDeleteExpr;
401     break;
402 
403   case Stmt::ObjCStringLiteralClass:
404     K = CXCursor_ObjCStringLiteral;
405     break;
406 
407   case Stmt::ObjCEncodeExprClass:
408     K = CXCursor_ObjCEncodeExpr;
409     break;
410 
411   case Stmt::ObjCSelectorExprClass:
412     K = CXCursor_ObjCSelectorExpr;
413     break;
414 
415   case Stmt::ObjCProtocolExprClass:
416     K = CXCursor_ObjCProtocolExpr;
417     break;
418 
419   case Stmt::ObjCBoolLiteralExprClass:
420     K = CXCursor_ObjCBoolLiteralExpr;
421     break;
422 
423   case Stmt::ObjCBridgedCastExprClass:
424     K = CXCursor_ObjCBridgedCastExpr;
425     break;
426 
427   case Stmt::BlockExprClass:
428     K = CXCursor_BlockExpr;
429     break;
430 
431   case Stmt::PackExpansionExprClass:
432     K = CXCursor_PackExpansionExpr;
433     break;
434 
435   case Stmt::SizeOfPackExprClass:
436     K = CXCursor_SizeOfPackExpr;
437     break;
438 
439   case Stmt::DeclRefExprClass:
440     if (const ImplicitParamDecl *IPD =
441          dyn_cast_or_null<ImplicitParamDecl>(cast<DeclRefExpr>(S)->getDecl())) {
442       if (const ObjCMethodDecl *MD =
443             dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
444         if (MD->getSelfDecl() == IPD) {
445           K = CXCursor_ObjCSelfExpr;
446           break;
447         }
448       }
449     }
450 
451     K = CXCursor_DeclRefExpr;
452     break;
453 
454   case Stmt::DependentScopeDeclRefExprClass:
455   case Stmt::SubstNonTypeTemplateParmExprClass:
456   case Stmt::SubstNonTypeTemplateParmPackExprClass:
457   case Stmt::FunctionParmPackExprClass:
458   case Stmt::UnresolvedLookupExprClass:
459     K = CXCursor_DeclRefExpr;
460     break;
461 
462   case Stmt::CXXDependentScopeMemberExprClass:
463   case Stmt::CXXPseudoDestructorExprClass:
464   case Stmt::MemberExprClass:
465   case Stmt::MSPropertyRefExprClass:
466   case Stmt::ObjCIsaExprClass:
467   case Stmt::ObjCIvarRefExprClass:
468   case Stmt::ObjCPropertyRefExprClass:
469   case Stmt::UnresolvedMemberExprClass:
470     K = CXCursor_MemberRefExpr;
471     break;
472 
473   case Stmt::CallExprClass:
474   case Stmt::CXXOperatorCallExprClass:
475   case Stmt::CXXMemberCallExprClass:
476   case Stmt::CUDAKernelCallExprClass:
477   case Stmt::CXXConstructExprClass:
478   case Stmt::CXXTemporaryObjectExprClass:
479   case Stmt::CXXUnresolvedConstructExprClass:
480   case Stmt::UserDefinedLiteralClass:
481     K = CXCursor_CallExpr;
482     break;
483 
484   case Stmt::LambdaExprClass:
485     K = CXCursor_LambdaExpr;
486     break;
487 
488   case Stmt::ObjCMessageExprClass: {
489     K = CXCursor_ObjCMessageExpr;
490     int SelectorIdIndex = -1;
491     // Check if cursor points to a selector id.
492     if (RegionOfInterest.isValid() &&
493         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
494       SmallVector<SourceLocation, 16> SelLocs;
495       cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
496       SmallVectorImpl<SourceLocation>::iterator
497         I=std::find(SelLocs.begin(), SelLocs.end(),RegionOfInterest.getBegin());
498       if (I != SelLocs.end())
499         SelectorIdIndex = I - SelLocs.begin();
500     }
501     CXCursor C = { K, 0, { Parent, S, TU } };
502     return getSelectorIdentifierCursor(SelectorIdIndex, C);
503   }
504 
505   case Stmt::MSDependentExistsStmtClass:
506     K = CXCursor_UnexposedStmt;
507     break;
508   case Stmt::OMPParallelDirectiveClass:
509     K = CXCursor_OMPParallelDirective;
510     break;
511 
512   }
513 
514   CXCursor C = { K, 0, { Parent, S, TU } };
515   return C;
516 }
517 
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl * Super,SourceLocation Loc,CXTranslationUnit TU)518 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
519                                                SourceLocation Loc,
520                                                CXTranslationUnit TU) {
521   assert(Super && TU && "Invalid arguments!");
522   void *RawLoc = Loc.getPtrEncoding();
523   CXCursor C = { CXCursor_ObjCSuperClassRef, 0, { Super, RawLoc, TU } };
524   return C;
525 }
526 
527 std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCSuperClassRef(CXCursor C)528 cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
529   assert(C.kind == CXCursor_ObjCSuperClassRef);
530   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
531                         SourceLocation::getFromPtrEncoding(C.data[1]));
532 }
533 
MakeCursorObjCProtocolRef(const ObjCProtocolDecl * Proto,SourceLocation Loc,CXTranslationUnit TU)534 CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
535                                              SourceLocation Loc,
536                                              CXTranslationUnit TU) {
537   assert(Proto && TU && "Invalid arguments!");
538   void *RawLoc = Loc.getPtrEncoding();
539   CXCursor C = { CXCursor_ObjCProtocolRef, 0, { Proto, RawLoc, TU } };
540   return C;
541 }
542 
543 std::pair<const ObjCProtocolDecl *, SourceLocation>
getCursorObjCProtocolRef(CXCursor C)544 cxcursor::getCursorObjCProtocolRef(CXCursor C) {
545   assert(C.kind == CXCursor_ObjCProtocolRef);
546   return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
547                         SourceLocation::getFromPtrEncoding(C.data[1]));
548 }
549 
MakeCursorObjCClassRef(const ObjCInterfaceDecl * Class,SourceLocation Loc,CXTranslationUnit TU)550 CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
551                                           SourceLocation Loc,
552                                           CXTranslationUnit TU) {
553   // 'Class' can be null for invalid code.
554   if (!Class)
555     return MakeCXCursorInvalid(CXCursor_InvalidCode);
556   assert(TU && "Invalid arguments!");
557   void *RawLoc = Loc.getPtrEncoding();
558   CXCursor C = { CXCursor_ObjCClassRef, 0, { Class, RawLoc, TU } };
559   return C;
560 }
561 
562 std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C)563 cxcursor::getCursorObjCClassRef(CXCursor C) {
564   assert(C.kind == CXCursor_ObjCClassRef);
565   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
566                         SourceLocation::getFromPtrEncoding(C.data[1]));
567 }
568 
MakeCursorTypeRef(const TypeDecl * Type,SourceLocation Loc,CXTranslationUnit TU)569 CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
570                                      CXTranslationUnit TU) {
571   assert(Type && TU && "Invalid arguments!");
572   void *RawLoc = Loc.getPtrEncoding();
573   CXCursor C = { CXCursor_TypeRef, 0, { Type, RawLoc, TU } };
574   return C;
575 }
576 
577 std::pair<const TypeDecl *, SourceLocation>
getCursorTypeRef(CXCursor C)578 cxcursor::getCursorTypeRef(CXCursor C) {
579   assert(C.kind == CXCursor_TypeRef);
580   return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
581                         SourceLocation::getFromPtrEncoding(C.data[1]));
582 }
583 
MakeCursorTemplateRef(const TemplateDecl * Template,SourceLocation Loc,CXTranslationUnit TU)584 CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
585                                          SourceLocation Loc,
586                                          CXTranslationUnit TU) {
587   assert(Template && TU && "Invalid arguments!");
588   void *RawLoc = Loc.getPtrEncoding();
589   CXCursor C = { CXCursor_TemplateRef, 0, { Template, RawLoc, TU } };
590   return C;
591 }
592 
593 std::pair<const TemplateDecl *, SourceLocation>
getCursorTemplateRef(CXCursor C)594 cxcursor::getCursorTemplateRef(CXCursor C) {
595   assert(C.kind == CXCursor_TemplateRef);
596   return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
597                         SourceLocation::getFromPtrEncoding(C.data[1]));
598 }
599 
MakeCursorNamespaceRef(const NamedDecl * NS,SourceLocation Loc,CXTranslationUnit TU)600 CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
601                                           SourceLocation Loc,
602                                           CXTranslationUnit TU) {
603 
604   assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
605          "Invalid arguments!");
606   void *RawLoc = Loc.getPtrEncoding();
607   CXCursor C = { CXCursor_NamespaceRef, 0, { NS, RawLoc, TU } };
608   return C;
609 }
610 
611 std::pair<const NamedDecl *, SourceLocation>
getCursorNamespaceRef(CXCursor C)612 cxcursor::getCursorNamespaceRef(CXCursor C) {
613   assert(C.kind == CXCursor_NamespaceRef);
614   return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
615                         SourceLocation::getFromPtrEncoding(C.data[1]));
616 }
617 
MakeCursorVariableRef(const VarDecl * Var,SourceLocation Loc,CXTranslationUnit TU)618 CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
619                                          CXTranslationUnit TU) {
620 
621   assert(Var && TU && "Invalid arguments!");
622   void *RawLoc = Loc.getPtrEncoding();
623   CXCursor C = { CXCursor_VariableRef, 0, { Var, RawLoc, TU } };
624   return C;
625 }
626 
627 std::pair<const VarDecl *, SourceLocation>
getCursorVariableRef(CXCursor C)628 cxcursor::getCursorVariableRef(CXCursor C) {
629   assert(C.kind == CXCursor_VariableRef);
630   return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
631                         SourceLocation::getFromPtrEncoding(C.data[1]));
632 }
633 
MakeCursorMemberRef(const FieldDecl * Field,SourceLocation Loc,CXTranslationUnit TU)634 CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field, SourceLocation Loc,
635                                        CXTranslationUnit TU) {
636 
637   assert(Field && TU && "Invalid arguments!");
638   void *RawLoc = Loc.getPtrEncoding();
639   CXCursor C = { CXCursor_MemberRef, 0, { Field, RawLoc, TU } };
640   return C;
641 }
642 
643 std::pair<const FieldDecl *, SourceLocation>
getCursorMemberRef(CXCursor C)644 cxcursor::getCursorMemberRef(CXCursor C) {
645   assert(C.kind == CXCursor_MemberRef);
646   return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
647                         SourceLocation::getFromPtrEncoding(C.data[1]));
648 }
649 
MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier * B,CXTranslationUnit TU)650 CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
651                                               CXTranslationUnit TU){
652   CXCursor C = { CXCursor_CXXBaseSpecifier, 0, { B, 0, TU } };
653   return C;
654 }
655 
getCursorCXXBaseSpecifier(CXCursor C)656 const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
657   assert(C.kind == CXCursor_CXXBaseSpecifier);
658   return static_cast<const CXXBaseSpecifier*>(C.data[0]);
659 }
660 
MakePreprocessingDirectiveCursor(SourceRange Range,CXTranslationUnit TU)661 CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
662                                                     CXTranslationUnit TU) {
663   CXCursor C = { CXCursor_PreprocessingDirective, 0,
664                  { Range.getBegin().getPtrEncoding(),
665                    Range.getEnd().getPtrEncoding(),
666                    TU }
667                };
668   return C;
669 }
670 
getCursorPreprocessingDirective(CXCursor C)671 SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
672   assert(C.kind == CXCursor_PreprocessingDirective);
673   SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
674                     SourceLocation::getFromPtrEncoding(C.data[1]));
675   ASTUnit *TU = getCursorASTUnit(C);
676   return TU->mapRangeFromPreamble(Range);
677 }
678 
MakeMacroDefinitionCursor(const MacroDefinition * MI,CXTranslationUnit TU)679 CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinition *MI,
680                                              CXTranslationUnit TU) {
681   CXCursor C = { CXCursor_MacroDefinition, 0, { MI, 0, TU } };
682   return C;
683 }
684 
getCursorMacroDefinition(CXCursor C)685 const MacroDefinition *cxcursor::getCursorMacroDefinition(CXCursor C) {
686   assert(C.kind == CXCursor_MacroDefinition);
687   return static_cast<const MacroDefinition *>(C.data[0]);
688 }
689 
MakeMacroExpansionCursor(MacroExpansion * MI,CXTranslationUnit TU)690 CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
691                                             CXTranslationUnit TU) {
692   CXCursor C = { CXCursor_MacroExpansion, 0, { MI, 0, TU } };
693   return C;
694 }
695 
MakeMacroExpansionCursor(MacroDefinition * MI,SourceLocation Loc,CXTranslationUnit TU)696 CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinition *MI,
697                                             SourceLocation Loc,
698                                             CXTranslationUnit TU) {
699   assert(Loc.isValid());
700   CXCursor C = { CXCursor_MacroExpansion, 0, { MI, Loc.getPtrEncoding(), TU } };
701   return C;
702 }
703 
getName() const704 const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
705   if (isPseudo())
706     return getAsMacroDefinition()->getName();
707   return getAsMacroExpansion()->getName();
708 }
getDefinition() const709 const MacroDefinition *cxcursor::MacroExpansionCursor::getDefinition() const {
710   if (isPseudo())
711     return getAsMacroDefinition();
712   return getAsMacroExpansion()->getDefinition();
713 }
getSourceRange() const714 SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
715   if (isPseudo())
716     return getPseudoLoc();
717   return getAsMacroExpansion()->getSourceRange();
718 }
719 
MakeInclusionDirectiveCursor(InclusionDirective * ID,CXTranslationUnit TU)720 CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
721                                                 CXTranslationUnit TU) {
722   CXCursor C = { CXCursor_InclusionDirective, 0, { ID, 0, TU } };
723   return C;
724 }
725 
getCursorInclusionDirective(CXCursor C)726 const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
727   assert(C.kind == CXCursor_InclusionDirective);
728   return static_cast<const InclusionDirective *>(C.data[0]);
729 }
730 
MakeCursorLabelRef(LabelStmt * Label,SourceLocation Loc,CXTranslationUnit TU)731 CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
732                                       CXTranslationUnit TU) {
733 
734   assert(Label && TU && "Invalid arguments!");
735   void *RawLoc = Loc.getPtrEncoding();
736   CXCursor C = { CXCursor_LabelRef, 0, { Label, RawLoc, TU } };
737   return C;
738 }
739 
740 std::pair<const LabelStmt *, SourceLocation>
getCursorLabelRef(CXCursor C)741 cxcursor::getCursorLabelRef(CXCursor C) {
742   assert(C.kind == CXCursor_LabelRef);
743   return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
744                         SourceLocation::getFromPtrEncoding(C.data[1]));
745 }
746 
MakeCursorOverloadedDeclRef(const OverloadExpr * E,CXTranslationUnit TU)747 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
748                                                CXTranslationUnit TU) {
749   assert(E && TU && "Invalid arguments!");
750   OverloadedDeclRefStorage Storage(E);
751   void *RawLoc = E->getNameLoc().getPtrEncoding();
752   CXCursor C = {
753                  CXCursor_OverloadedDeclRef, 0,
754                  { Storage.getOpaqueValue(), RawLoc, TU }
755                };
756   return C;
757 }
758 
MakeCursorOverloadedDeclRef(const Decl * D,SourceLocation Loc,CXTranslationUnit TU)759 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
760                                                SourceLocation Loc,
761                                                CXTranslationUnit TU) {
762   assert(D && TU && "Invalid arguments!");
763   void *RawLoc = Loc.getPtrEncoding();
764   OverloadedDeclRefStorage Storage(D);
765   CXCursor C = {
766     CXCursor_OverloadedDeclRef, 0,
767     { Storage.getOpaqueValue(), RawLoc, TU }
768   };
769   return C;
770 }
771 
MakeCursorOverloadedDeclRef(TemplateName Name,SourceLocation Loc,CXTranslationUnit TU)772 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
773                                                SourceLocation Loc,
774                                                CXTranslationUnit TU) {
775   assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
776   void *RawLoc = Loc.getPtrEncoding();
777   OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
778   CXCursor C = {
779     CXCursor_OverloadedDeclRef, 0,
780     { Storage.getOpaqueValue(), RawLoc, TU }
781   };
782   return C;
783 }
784 
785 std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
getCursorOverloadedDeclRef(CXCursor C)786 cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
787   assert(C.kind == CXCursor_OverloadedDeclRef);
788   return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
789                                        const_cast<void *>(C.data[0])),
790                         SourceLocation::getFromPtrEncoding(C.data[1]));
791 }
792 
getCursorDecl(CXCursor Cursor)793 const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
794   return static_cast<const Decl *>(Cursor.data[0]);
795 }
796 
getCursorExpr(CXCursor Cursor)797 const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
798   return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
799 }
800 
getCursorStmt(CXCursor Cursor)801 const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
802   if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
803       Cursor.kind == CXCursor_ObjCProtocolRef ||
804       Cursor.kind == CXCursor_ObjCClassRef)
805     return 0;
806 
807   return static_cast<const Stmt *>(Cursor.data[1]);
808 }
809 
getCursorAttr(CXCursor Cursor)810 const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
811   return static_cast<const Attr *>(Cursor.data[1]);
812 }
813 
getCursorParentDecl(CXCursor Cursor)814 const Decl *cxcursor::getCursorParentDecl(CXCursor Cursor) {
815   return static_cast<const Decl *>(Cursor.data[0]);
816 }
817 
getCursorContext(CXCursor Cursor)818 ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
819   return getCursorASTUnit(Cursor)->getASTContext();
820 }
821 
getCursorASTUnit(CXCursor Cursor)822 ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
823   CXTranslationUnit TU = getCursorTU(Cursor);
824   if (!TU)
825     return 0;
826   return cxtu::getASTUnit(TU);
827 }
828 
getCursorTU(CXCursor Cursor)829 CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
830   return static_cast<CXTranslationUnit>(const_cast<void*>(Cursor.data[2]));
831 }
832 
getOverriddenCursors(CXCursor cursor,SmallVectorImpl<CXCursor> & overridden)833 void cxcursor::getOverriddenCursors(CXCursor cursor,
834                                     SmallVectorImpl<CXCursor> &overridden) {
835   assert(clang_isDeclaration(cursor.kind));
836   const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
837   if (!D)
838     return;
839 
840   CXTranslationUnit TU = getCursorTU(cursor);
841   SmallVector<const NamedDecl *, 8> OverDecls;
842   D->getASTContext().getOverriddenMethods(D, OverDecls);
843 
844   for (SmallVectorImpl<const NamedDecl *>::iterator
845          I = OverDecls.begin(), E = OverDecls.end(); I != E; ++I) {
846     overridden.push_back(MakeCXCursor(*I, TU));
847   }
848 }
849 
850 std::pair<int, SourceLocation>
getSelectorIdentifierIndexAndLoc(CXCursor cursor)851 cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
852   if (cursor.kind == CXCursor_ObjCMessageExpr) {
853     if (cursor.xdata != -1)
854       return std::make_pair(cursor.xdata,
855                             cast<ObjCMessageExpr>(getCursorExpr(cursor))
856                                                 ->getSelectorLoc(cursor.xdata));
857   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
858              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
859     if (cursor.xdata != -1)
860       return std::make_pair(cursor.xdata,
861                             cast<ObjCMethodDecl>(getCursorDecl(cursor))
862                                                 ->getSelectorLoc(cursor.xdata));
863   }
864 
865   return std::make_pair(-1, SourceLocation());
866 }
867 
getSelectorIdentifierCursor(int SelIdx,CXCursor cursor)868 CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
869   CXCursor newCursor = cursor;
870 
871   if (cursor.kind == CXCursor_ObjCMessageExpr) {
872     if (SelIdx == -1 ||
873         unsigned(SelIdx) >= cast<ObjCMessageExpr>(getCursorExpr(cursor))
874                                                          ->getNumSelectorLocs())
875       newCursor.xdata = -1;
876     else
877       newCursor.xdata = SelIdx;
878   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
879              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
880     if (SelIdx == -1 ||
881         unsigned(SelIdx) >= cast<ObjCMethodDecl>(getCursorDecl(cursor))
882                                                          ->getNumSelectorLocs())
883       newCursor.xdata = -1;
884     else
885       newCursor.xdata = SelIdx;
886   }
887 
888   return newCursor;
889 }
890 
getTypeRefCursor(CXCursor cursor)891 CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
892   if (cursor.kind != CXCursor_CallExpr)
893     return cursor;
894 
895   if (cursor.xdata == 0)
896     return cursor;
897 
898   const Expr *E = getCursorExpr(cursor);
899   TypeSourceInfo *Type = 0;
900   if (const CXXUnresolvedConstructExpr *
901         UnCtor = dyn_cast<CXXUnresolvedConstructExpr>(E)) {
902     Type = UnCtor->getTypeSourceInfo();
903   } else if (const CXXTemporaryObjectExpr *Tmp =
904                  dyn_cast<CXXTemporaryObjectExpr>(E)){
905     Type = Tmp->getTypeSourceInfo();
906   }
907 
908   if (!Type)
909     return cursor;
910 
911   CXTranslationUnit TU = getCursorTU(cursor);
912   QualType Ty = Type->getType();
913   TypeLoc TL = Type->getTypeLoc();
914   SourceLocation Loc = TL.getBeginLoc();
915 
916   if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
917     Ty = ElabT->getNamedType();
918     ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
919     Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
920   }
921 
922   if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
923     return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
924   if (const TagType *Tag = Ty->getAs<TagType>())
925     return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
926   if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
927     return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
928 
929   return cursor;
930 }
931 
operator ==(CXCursor X,CXCursor Y)932 bool cxcursor::operator==(CXCursor X, CXCursor Y) {
933   return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
934          X.data[2] == Y.data[2];
935 }
936 
937 // FIXME: Remove once we can model DeclGroups and their appropriate ranges
938 // properly in the ASTs.
isFirstInDeclGroup(CXCursor C)939 bool cxcursor::isFirstInDeclGroup(CXCursor C) {
940   assert(clang_isDeclaration(C.kind));
941   return ((uintptr_t) (C.data[1])) != 0;
942 }
943 
944 //===----------------------------------------------------------------------===//
945 // libclang CXCursor APIs
946 //===----------------------------------------------------------------------===//
947 
948 extern "C" {
949 
clang_Cursor_isNull(CXCursor cursor)950 int clang_Cursor_isNull(CXCursor cursor) {
951   return clang_equalCursors(cursor, clang_getNullCursor());
952 }
953 
clang_Cursor_getTranslationUnit(CXCursor cursor)954 CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
955   return getCursorTU(cursor);
956 }
957 
clang_Cursor_getNumArguments(CXCursor C)958 int clang_Cursor_getNumArguments(CXCursor C) {
959   if (clang_isDeclaration(C.kind)) {
960     const Decl *D = cxcursor::getCursorDecl(C);
961     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
962       return MD->param_size();
963     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
964       return FD->param_size();
965   }
966 
967   if (clang_isExpression(C.kind)) {
968     const Expr *E = cxcursor::getCursorExpr(C);
969     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
970       return CE->getNumArgs();
971     }
972   }
973 
974   return -1;
975 }
976 
clang_Cursor_getArgument(CXCursor C,unsigned i)977 CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
978   if (clang_isDeclaration(C.kind)) {
979     const Decl *D = cxcursor::getCursorDecl(C);
980     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
981       if (i < MD->param_size())
982         return cxcursor::MakeCXCursor(MD->param_begin()[i],
983                                       cxcursor::getCursorTU(C));
984     } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
985       if (i < FD->param_size())
986         return cxcursor::MakeCXCursor(FD->param_begin()[i],
987                                       cxcursor::getCursorTU(C));
988     }
989   }
990 
991   if (clang_isExpression(C.kind)) {
992     const Expr *E = cxcursor::getCursorExpr(C);
993     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
994       if (i < CE->getNumArgs()) {
995         return cxcursor::MakeCXCursor(CE->getArg(i),
996                                       getCursorDecl(C),
997                                       cxcursor::getCursorTU(C));
998       }
999     }
1000   }
1001 
1002   return clang_getNullCursor();
1003 }
1004 
1005 } // end: extern "C"
1006 
1007 //===----------------------------------------------------------------------===//
1008 // CXCursorSet.
1009 //===----------------------------------------------------------------------===//
1010 
1011 typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1012 
packCXCursorSet(CXCursorSet_Impl * setImpl)1013 static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1014   return (CXCursorSet) setImpl;
1015 }
unpackCXCursorSet(CXCursorSet set)1016 static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1017   return (CXCursorSet_Impl*) set;
1018 }
1019 namespace llvm {
1020 template<> struct DenseMapInfo<CXCursor> {
1021 public:
getEmptyKeyllvm::DenseMapInfo1022   static inline CXCursor getEmptyKey() {
1023     return MakeCXCursorInvalid(CXCursor_InvalidFile);
1024   }
getTombstoneKeyllvm::DenseMapInfo1025   static inline CXCursor getTombstoneKey() {
1026     return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1027   }
getHashValuellvm::DenseMapInfo1028   static inline unsigned getHashValue(const CXCursor &cursor) {
1029     return llvm::DenseMapInfo<std::pair<const void *, const void *> >
1030       ::getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1031   }
isEqualllvm::DenseMapInfo1032   static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1033     return x.kind == y.kind &&
1034            x.data[0] == y.data[0] &&
1035            x.data[1] == y.data[1];
1036   }
1037 };
1038 }
1039 
1040 extern "C" {
clang_createCXCursorSet()1041 CXCursorSet clang_createCXCursorSet() {
1042   return packCXCursorSet(new CXCursorSet_Impl());
1043 }
1044 
clang_disposeCXCursorSet(CXCursorSet set)1045 void clang_disposeCXCursorSet(CXCursorSet set) {
1046   delete unpackCXCursorSet(set);
1047 }
1048 
clang_CXCursorSet_contains(CXCursorSet set,CXCursor cursor)1049 unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1050   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1051   if (!setImpl)
1052     return 0;
1053   return setImpl->find(cursor) != setImpl->end();
1054 }
1055 
clang_CXCursorSet_insert(CXCursorSet set,CXCursor cursor)1056 unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1057   // Do not insert invalid cursors into the set.
1058   if (cursor.kind >= CXCursor_FirstInvalid &&
1059       cursor.kind <= CXCursor_LastInvalid)
1060     return 1;
1061 
1062   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1063   if (!setImpl)
1064     return 1;
1065   unsigned &entry = (*setImpl)[cursor];
1066   unsigned flag = entry == 0 ? 1 : 0;
1067   entry = 1;
1068   return flag;
1069 }
1070 
clang_getCursorCompletionString(CXCursor cursor)1071 CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1072   enum CXCursorKind kind = clang_getCursorKind(cursor);
1073   if (clang_isDeclaration(kind)) {
1074     const Decl *decl = getCursorDecl(cursor);
1075     if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
1076       ASTUnit *unit = getCursorASTUnit(cursor);
1077       CodeCompletionResult Result(namedDecl, CCP_Declaration);
1078       CodeCompletionString *String
1079         = Result.CreateCodeCompletionString(unit->getASTContext(),
1080                                             unit->getPreprocessor(),
1081                                  unit->getCodeCompletionTUInfo().getAllocator(),
1082                                  unit->getCodeCompletionTUInfo(),
1083                                  true);
1084       return String;
1085     }
1086   }
1087   else if (kind == CXCursor_MacroDefinition) {
1088     const MacroDefinition *definition = getCursorMacroDefinition(cursor);
1089     const IdentifierInfo *MacroInfo = definition->getName();
1090     ASTUnit *unit = getCursorASTUnit(cursor);
1091     CodeCompletionResult Result(MacroInfo);
1092     CodeCompletionString *String
1093       = Result.CreateCodeCompletionString(unit->getASTContext(),
1094                                           unit->getPreprocessor(),
1095                                  unit->getCodeCompletionTUInfo().getAllocator(),
1096                                  unit->getCodeCompletionTUInfo(),
1097                                  false);
1098     return String;
1099   }
1100   return NULL;
1101 }
1102 } // end: extern C.
1103 
1104 namespace {
1105   struct OverridenCursorsPool {
1106     typedef SmallVector<CXCursor, 2> CursorVec;
1107     std::vector<CursorVec*> AllCursors;
1108     std::vector<CursorVec*> AvailableCursors;
1109 
~OverridenCursorsPool__anon5494a9c40111::OverridenCursorsPool1110     ~OverridenCursorsPool() {
1111       for (std::vector<CursorVec*>::iterator I = AllCursors.begin(),
1112            E = AllCursors.end(); I != E; ++I) {
1113         delete *I;
1114       }
1115     }
1116   };
1117 }
1118 
createOverridenCXCursorsPool()1119 void *cxcursor::createOverridenCXCursorsPool() {
1120   return new OverridenCursorsPool();
1121 }
1122 
disposeOverridenCXCursorsPool(void * pool)1123 void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1124   delete static_cast<OverridenCursorsPool*>(pool);
1125 }
1126 
1127 extern "C" {
clang_getOverriddenCursors(CXCursor cursor,CXCursor ** overridden,unsigned * num_overridden)1128 void clang_getOverriddenCursors(CXCursor cursor,
1129                                 CXCursor **overridden,
1130                                 unsigned *num_overridden) {
1131   if (overridden)
1132     *overridden = 0;
1133   if (num_overridden)
1134     *num_overridden = 0;
1135 
1136   CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1137 
1138   if (!overridden || !num_overridden || !TU)
1139     return;
1140 
1141   if (!clang_isDeclaration(cursor.kind))
1142     return;
1143 
1144   OverridenCursorsPool &pool =
1145     *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1146 
1147   OverridenCursorsPool::CursorVec *Vec = 0;
1148 
1149   if (!pool.AvailableCursors.empty()) {
1150     Vec = pool.AvailableCursors.back();
1151     pool.AvailableCursors.pop_back();
1152   }
1153   else {
1154     Vec = new OverridenCursorsPool::CursorVec();
1155     pool.AllCursors.push_back(Vec);
1156   }
1157 
1158   // Clear out the vector, but don't free the memory contents.  This
1159   // reduces malloc() traffic.
1160   Vec->clear();
1161 
1162   // Use the first entry to contain a back reference to the vector.
1163   // This is a complete hack.
1164   CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1165   backRefCursor.data[0] = Vec;
1166   assert(cxcursor::getCursorTU(backRefCursor) == TU);
1167   Vec->push_back(backRefCursor);
1168 
1169   // Get the overriden cursors.
1170   cxcursor::getOverriddenCursors(cursor, *Vec);
1171 
1172   // Did we get any overriden cursors?  If not, return Vec to the pool
1173   // of available cursor vectors.
1174   if (Vec->size() == 1) {
1175     pool.AvailableCursors.push_back(Vec);
1176     return;
1177   }
1178 
1179   // Now tell the caller about the overriden cursors.
1180   assert(Vec->size() > 1);
1181   *overridden = &((*Vec)[1]);
1182   *num_overridden = Vec->size() - 1;
1183 }
1184 
clang_disposeOverriddenCursors(CXCursor * overridden)1185 void clang_disposeOverriddenCursors(CXCursor *overridden) {
1186   if (!overridden)
1187     return;
1188 
1189   // Use pointer arithmetic to get back the first faux entry
1190   // which has a back-reference to the TU and the vector.
1191   --overridden;
1192   OverridenCursorsPool::CursorVec *Vec =
1193       static_cast<OverridenCursorsPool::CursorVec *>(
1194           const_cast<void *>(overridden->data[0]));
1195   CXTranslationUnit TU = getCursorTU(*overridden);
1196 
1197   assert(Vec && TU);
1198 
1199   OverridenCursorsPool &pool =
1200     *static_cast<OverridenCursorsPool*>(TU->OverridenCursorsPool);
1201 
1202   pool.AvailableCursors.push_back(Vec);
1203 }
1204 
clang_Cursor_isDynamicCall(CXCursor C)1205 int clang_Cursor_isDynamicCall(CXCursor C) {
1206   const Expr *E = 0;
1207   if (clang_isExpression(C.kind))
1208     E = getCursorExpr(C);
1209   if (!E)
1210     return 0;
1211 
1212   if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E))
1213     return MsgE->getReceiverKind() == ObjCMessageExpr::Instance;
1214 
1215   const MemberExpr *ME = 0;
1216   if (isa<MemberExpr>(E))
1217     ME = cast<MemberExpr>(E);
1218   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1219     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1220 
1221   if (ME) {
1222     if (const CXXMethodDecl *
1223           MD = dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1224       return MD->isVirtual() && !ME->hasQualifier();
1225   }
1226 
1227   return 0;
1228 }
1229 
clang_Cursor_getReceiverType(CXCursor C)1230 CXType clang_Cursor_getReceiverType(CXCursor C) {
1231   CXTranslationUnit TU = cxcursor::getCursorTU(C);
1232   const Expr *E = 0;
1233   if (clang_isExpression(C.kind))
1234     E = getCursorExpr(C);
1235 
1236   if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1237     return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1238 
1239   return cxtype::MakeCXType(QualType(), TU);
1240 }
1241 
1242 } // end: extern "C"
1243