1 //===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines routines for manipulating CXCursors. It should be the
10 // only file that has internal knowledge of the encoding of the data in
11 // CXCursor.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "CXCursor.h"
16 #include "CXString.h"
17 #include "CXTranslationUnit.h"
18 #include "CXType.h"
19 #include "clang-c/Index.h"
20 #include "clang/AST/Attr.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, {nullptr, nullptr, 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:
44     break;
45   case attr::IBAction:
46     return CXCursor_IBActionAttr;
47   case attr::IBOutlet:
48     return CXCursor_IBOutletAttr;
49   case attr::IBOutletCollection:
50     return CXCursor_IBOutletCollectionAttr;
51   case attr::Final:
52     return CXCursor_CXXFinalAttr;
53   case attr::Override:
54     return CXCursor_CXXOverrideAttr;
55   case attr::Annotate:
56     return CXCursor_AnnotateAttr;
57   case attr::AsmLabel:
58     return CXCursor_AsmLabelAttr;
59   case attr::Packed:
60     return CXCursor_PackedAttr;
61   case attr::Pure:
62     return CXCursor_PureAttr;
63   case attr::Const:
64     return CXCursor_ConstAttr;
65   case attr::NoDuplicate:
66     return CXCursor_NoDuplicateAttr;
67   case attr::CUDAConstant:
68     return CXCursor_CUDAConstantAttr;
69   case attr::CUDADevice:
70     return CXCursor_CUDADeviceAttr;
71   case attr::CUDAGlobal:
72     return CXCursor_CUDAGlobalAttr;
73   case attr::CUDAHost:
74     return CXCursor_CUDAHostAttr;
75   case attr::CUDAShared:
76     return CXCursor_CUDASharedAttr;
77   case attr::Visibility:
78     return CXCursor_VisibilityAttr;
79   case attr::DLLExport:
80     return CXCursor_DLLExport;
81   case attr::DLLImport:
82     return CXCursor_DLLImport;
83   case attr::NSReturnsRetained:
84     return CXCursor_NSReturnsRetained;
85   case attr::NSReturnsNotRetained:
86     return CXCursor_NSReturnsNotRetained;
87   case attr::NSReturnsAutoreleased:
88     return CXCursor_NSReturnsAutoreleased;
89   case attr::NSConsumesSelf:
90     return CXCursor_NSConsumesSelf;
91   case attr::NSConsumed:
92     return CXCursor_NSConsumed;
93   case attr::ObjCException:
94     return CXCursor_ObjCException;
95   case attr::ObjCNSObject:
96     return CXCursor_ObjCNSObject;
97   case attr::ObjCIndependentClass:
98     return CXCursor_ObjCIndependentClass;
99   case attr::ObjCPreciseLifetime:
100     return CXCursor_ObjCPreciseLifetime;
101   case attr::ObjCReturnsInnerPointer:
102     return CXCursor_ObjCReturnsInnerPointer;
103   case attr::ObjCRequiresSuper:
104     return CXCursor_ObjCRequiresSuper;
105   case attr::ObjCRootClass:
106     return CXCursor_ObjCRootClass;
107   case attr::ObjCSubclassingRestricted:
108     return CXCursor_ObjCSubclassingRestricted;
109   case attr::ObjCExplicitProtocolImpl:
110     return CXCursor_ObjCExplicitProtocolImpl;
111   case attr::ObjCDesignatedInitializer:
112     return CXCursor_ObjCDesignatedInitializer;
113   case attr::ObjCRuntimeVisible:
114     return CXCursor_ObjCRuntimeVisible;
115   case attr::ObjCBoxable:
116     return CXCursor_ObjCBoxable;
117   case attr::FlagEnum:
118     return CXCursor_FlagEnum;
119   case attr::Convergent:
120     return CXCursor_ConvergentAttr;
121   case attr::WarnUnused:
122     return CXCursor_WarnUnusedAttr;
123   case attr::WarnUnusedResult:
124     return CXCursor_WarnUnusedResultAttr;
125   case attr::Aligned:
126     return CXCursor_AlignedAttr;
127   }
128 
129   return CXCursor_UnexposedAttr;
130 }
131 
MakeCXCursor(const Attr * A,const Decl * Parent,CXTranslationUnit TU)132 CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
133                                 CXTranslationUnit TU) {
134   assert(A && Parent && TU && "Invalid arguments!");
135   CXCursor C = {GetCursorKind(A), 0, {Parent, A, TU}};
136   return C;
137 }
138 
MakeCXCursor(const Decl * D,CXTranslationUnit TU,SourceRange RegionOfInterest,bool FirstInDeclGroup)139 CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
140                                 SourceRange RegionOfInterest,
141                                 bool FirstInDeclGroup) {
142   assert(D && TU && "Invalid arguments!");
143 
144   CXCursorKind K = getCursorKindForDecl(D);
145 
146   if (K == CXCursor_ObjCClassMethodDecl ||
147       K == CXCursor_ObjCInstanceMethodDecl) {
148     int SelectorIdIndex = -1;
149     // Check if cursor points to a selector id.
150     if (RegionOfInterest.isValid() &&
151         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
152       SmallVector<SourceLocation, 16> SelLocs;
153       cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
154       SmallVectorImpl<SourceLocation>::iterator I =
155           llvm::find(SelLocs, RegionOfInterest.getBegin());
156       if (I != SelLocs.end())
157         SelectorIdIndex = I - SelLocs.begin();
158     }
159     CXCursor C = {K,
160                   SelectorIdIndex,
161                   {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
162     return C;
163   }
164 
165   CXCursor C = {K, 0, {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
166   return C;
167 }
168 
MakeCXCursor(const Stmt * S,const Decl * Parent,CXTranslationUnit TU,SourceRange RegionOfInterest)169 CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
170                                 CXTranslationUnit TU,
171                                 SourceRange RegionOfInterest) {
172   assert(S && TU && "Invalid arguments!");
173   CXCursorKind K = CXCursor_NotImplemented;
174 
175   switch (S->getStmtClass()) {
176   case Stmt::NoStmtClass:
177     break;
178 
179   case Stmt::CaseStmtClass:
180     K = CXCursor_CaseStmt;
181     break;
182 
183   case Stmt::DefaultStmtClass:
184     K = CXCursor_DefaultStmt;
185     break;
186 
187   case Stmt::IfStmtClass:
188     K = CXCursor_IfStmt;
189     break;
190 
191   case Stmt::SwitchStmtClass:
192     K = CXCursor_SwitchStmt;
193     break;
194 
195   case Stmt::WhileStmtClass:
196     K = CXCursor_WhileStmt;
197     break;
198 
199   case Stmt::DoStmtClass:
200     K = CXCursor_DoStmt;
201     break;
202 
203   case Stmt::ForStmtClass:
204     K = CXCursor_ForStmt;
205     break;
206 
207   case Stmt::GotoStmtClass:
208     K = CXCursor_GotoStmt;
209     break;
210 
211   case Stmt::IndirectGotoStmtClass:
212     K = CXCursor_IndirectGotoStmt;
213     break;
214 
215   case Stmt::ContinueStmtClass:
216     K = CXCursor_ContinueStmt;
217     break;
218 
219   case Stmt::BreakStmtClass:
220     K = CXCursor_BreakStmt;
221     break;
222 
223   case Stmt::ReturnStmtClass:
224     K = CXCursor_ReturnStmt;
225     break;
226 
227   case Stmt::GCCAsmStmtClass:
228     K = CXCursor_GCCAsmStmt;
229     break;
230 
231   case Stmt::MSAsmStmtClass:
232     K = CXCursor_MSAsmStmt;
233     break;
234 
235   case Stmt::ObjCAtTryStmtClass:
236     K = CXCursor_ObjCAtTryStmt;
237     break;
238 
239   case Stmt::ObjCAtCatchStmtClass:
240     K = CXCursor_ObjCAtCatchStmt;
241     break;
242 
243   case Stmt::ObjCAtFinallyStmtClass:
244     K = CXCursor_ObjCAtFinallyStmt;
245     break;
246 
247   case Stmt::ObjCAtThrowStmtClass:
248     K = CXCursor_ObjCAtThrowStmt;
249     break;
250 
251   case Stmt::ObjCAtSynchronizedStmtClass:
252     K = CXCursor_ObjCAtSynchronizedStmt;
253     break;
254 
255   case Stmt::ObjCAutoreleasePoolStmtClass:
256     K = CXCursor_ObjCAutoreleasePoolStmt;
257     break;
258 
259   case Stmt::ObjCForCollectionStmtClass:
260     K = CXCursor_ObjCForCollectionStmt;
261     break;
262 
263   case Stmt::CXXCatchStmtClass:
264     K = CXCursor_CXXCatchStmt;
265     break;
266 
267   case Stmt::CXXTryStmtClass:
268     K = CXCursor_CXXTryStmt;
269     break;
270 
271   case Stmt::CXXForRangeStmtClass:
272     K = CXCursor_CXXForRangeStmt;
273     break;
274 
275   case Stmt::SEHTryStmtClass:
276     K = CXCursor_SEHTryStmt;
277     break;
278 
279   case Stmt::SEHExceptStmtClass:
280     K = CXCursor_SEHExceptStmt;
281     break;
282 
283   case Stmt::SEHFinallyStmtClass:
284     K = CXCursor_SEHFinallyStmt;
285     break;
286 
287   case Stmt::SEHLeaveStmtClass:
288     K = CXCursor_SEHLeaveStmt;
289     break;
290 
291   case Stmt::CoroutineBodyStmtClass:
292   case Stmt::CoreturnStmtClass:
293     K = CXCursor_UnexposedStmt;
294     break;
295 
296   case Stmt::ArrayTypeTraitExprClass:
297   case Stmt::AsTypeExprClass:
298   case Stmt::AtomicExprClass:
299   case Stmt::BinaryConditionalOperatorClass:
300   case Stmt::TypeTraitExprClass:
301   case Stmt::CoawaitExprClass:
302   case Stmt::ConceptSpecializationExprClass:
303   case Stmt::RequiresExprClass:
304   case Stmt::DependentCoawaitExprClass:
305   case Stmt::CoyieldExprClass:
306   case Stmt::CXXBindTemporaryExprClass:
307   case Stmt::CXXDefaultArgExprClass:
308   case Stmt::CXXDefaultInitExprClass:
309   case Stmt::CXXFoldExprClass:
310   case Stmt::CXXRewrittenBinaryOperatorClass:
311   case Stmt::CXXStdInitializerListExprClass:
312   case Stmt::CXXScalarValueInitExprClass:
313   case Stmt::CXXUuidofExprClass:
314   case Stmt::ChooseExprClass:
315   case Stmt::DesignatedInitExprClass:
316   case Stmt::DesignatedInitUpdateExprClass:
317   case Stmt::ArrayInitLoopExprClass:
318   case Stmt::ArrayInitIndexExprClass:
319   case Stmt::ExprWithCleanupsClass:
320   case Stmt::ExpressionTraitExprClass:
321   case Stmt::ExtVectorElementExprClass:
322   case Stmt::ImplicitCastExprClass:
323   case Stmt::ImplicitValueInitExprClass:
324   case Stmt::NoInitExprClass:
325   case Stmt::MaterializeTemporaryExprClass:
326   case Stmt::ObjCIndirectCopyRestoreExprClass:
327   case Stmt::OffsetOfExprClass:
328   case Stmt::ParenListExprClass:
329   case Stmt::PredefinedExprClass:
330   case Stmt::ShuffleVectorExprClass:
331   case Stmt::SourceLocExprClass:
332   case Stmt::ConvertVectorExprClass:
333   case Stmt::VAArgExprClass:
334   case Stmt::ObjCArrayLiteralClass:
335   case Stmt::ObjCDictionaryLiteralClass:
336   case Stmt::ObjCBoxedExprClass:
337   case Stmt::ObjCSubscriptRefExprClass:
338   case Stmt::RecoveryExprClass:
339     K = CXCursor_UnexposedExpr;
340     break;
341 
342   case Stmt::OpaqueValueExprClass:
343     if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
344       return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
345     K = CXCursor_UnexposedExpr;
346     break;
347 
348   case Stmt::PseudoObjectExprClass:
349     return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
350                         TU, RegionOfInterest);
351 
352   case Stmt::CompoundStmtClass:
353     K = CXCursor_CompoundStmt;
354     break;
355 
356   case Stmt::NullStmtClass:
357     K = CXCursor_NullStmt;
358     break;
359 
360   case Stmt::LabelStmtClass:
361     K = CXCursor_LabelStmt;
362     break;
363 
364   case Stmt::AttributedStmtClass:
365     K = CXCursor_UnexposedStmt;
366     break;
367 
368   case Stmt::DeclStmtClass:
369     K = CXCursor_DeclStmt;
370     break;
371 
372   case Stmt::CapturedStmtClass:
373     K = CXCursor_UnexposedStmt;
374     break;
375 
376   case Stmt::IntegerLiteralClass:
377     K = CXCursor_IntegerLiteral;
378     break;
379 
380   case Stmt::FixedPointLiteralClass:
381     K = CXCursor_FixedPointLiteral;
382     break;
383 
384   case Stmt::FloatingLiteralClass:
385     K = CXCursor_FloatingLiteral;
386     break;
387 
388   case Stmt::ImaginaryLiteralClass:
389     K = CXCursor_ImaginaryLiteral;
390     break;
391 
392   case Stmt::StringLiteralClass:
393     K = CXCursor_StringLiteral;
394     break;
395 
396   case Stmt::CharacterLiteralClass:
397     K = CXCursor_CharacterLiteral;
398     break;
399 
400   case Stmt::ConstantExprClass:
401     return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
402                         RegionOfInterest);
403 
404   case Stmt::ParenExprClass:
405     K = CXCursor_ParenExpr;
406     break;
407 
408   case Stmt::UnaryOperatorClass:
409     K = CXCursor_UnaryOperator;
410     break;
411 
412   case Stmt::UnaryExprOrTypeTraitExprClass:
413   case Stmt::CXXNoexceptExprClass:
414     K = CXCursor_UnaryExpr;
415     break;
416 
417   case Stmt::MSPropertySubscriptExprClass:
418   case Stmt::ArraySubscriptExprClass:
419     K = CXCursor_ArraySubscriptExpr;
420     break;
421 
422   case Stmt::MatrixSubscriptExprClass:
423     // TODO: add support for MatrixSubscriptExpr.
424     K = CXCursor_UnexposedExpr;
425     break;
426 
427   case Stmt::OMPArraySectionExprClass:
428     K = CXCursor_OMPArraySectionExpr;
429     break;
430 
431   case Stmt::OMPArrayShapingExprClass:
432     K = CXCursor_OMPArrayShapingExpr;
433     break;
434 
435   case Stmt::OMPIteratorExprClass:
436     K = CXCursor_OMPIteratorExpr;
437     break;
438 
439   case Stmt::BinaryOperatorClass:
440     K = CXCursor_BinaryOperator;
441     break;
442 
443   case Stmt::CompoundAssignOperatorClass:
444     K = CXCursor_CompoundAssignOperator;
445     break;
446 
447   case Stmt::ConditionalOperatorClass:
448     K = CXCursor_ConditionalOperator;
449     break;
450 
451   case Stmt::CStyleCastExprClass:
452     K = CXCursor_CStyleCastExpr;
453     break;
454 
455   case Stmt::CompoundLiteralExprClass:
456     K = CXCursor_CompoundLiteralExpr;
457     break;
458 
459   case Stmt::InitListExprClass:
460     K = CXCursor_InitListExpr;
461     break;
462 
463   case Stmt::AddrLabelExprClass:
464     K = CXCursor_AddrLabelExpr;
465     break;
466 
467   case Stmt::StmtExprClass:
468     K = CXCursor_StmtExpr;
469     break;
470 
471   case Stmt::GenericSelectionExprClass:
472     K = CXCursor_GenericSelectionExpr;
473     break;
474 
475   case Stmt::GNUNullExprClass:
476     K = CXCursor_GNUNullExpr;
477     break;
478 
479   case Stmt::CXXStaticCastExprClass:
480     K = CXCursor_CXXStaticCastExpr;
481     break;
482 
483   case Stmt::CXXDynamicCastExprClass:
484     K = CXCursor_CXXDynamicCastExpr;
485     break;
486 
487   case Stmt::CXXReinterpretCastExprClass:
488     K = CXCursor_CXXReinterpretCastExpr;
489     break;
490 
491   case Stmt::CXXConstCastExprClass:
492     K = CXCursor_CXXConstCastExpr;
493     break;
494 
495   case Stmt::CXXFunctionalCastExprClass:
496     K = CXCursor_CXXFunctionalCastExpr;
497     break;
498 
499   case Stmt::CXXAddrspaceCastExprClass:
500     K = CXCursor_CXXAddrspaceCastExpr;
501     break;
502 
503   case Stmt::CXXTypeidExprClass:
504     K = CXCursor_CXXTypeidExpr;
505     break;
506 
507   case Stmt::CXXBoolLiteralExprClass:
508     K = CXCursor_CXXBoolLiteralExpr;
509     break;
510 
511   case Stmt::CXXNullPtrLiteralExprClass:
512     K = CXCursor_CXXNullPtrLiteralExpr;
513     break;
514 
515   case Stmt::CXXThisExprClass:
516     K = CXCursor_CXXThisExpr;
517     break;
518 
519   case Stmt::CXXThrowExprClass:
520     K = CXCursor_CXXThrowExpr;
521     break;
522 
523   case Stmt::CXXNewExprClass:
524     K = CXCursor_CXXNewExpr;
525     break;
526 
527   case Stmt::CXXDeleteExprClass:
528     K = CXCursor_CXXDeleteExpr;
529     break;
530 
531   case Stmt::ObjCStringLiteralClass:
532     K = CXCursor_ObjCStringLiteral;
533     break;
534 
535   case Stmt::ObjCEncodeExprClass:
536     K = CXCursor_ObjCEncodeExpr;
537     break;
538 
539   case Stmt::ObjCSelectorExprClass:
540     K = CXCursor_ObjCSelectorExpr;
541     break;
542 
543   case Stmt::ObjCProtocolExprClass:
544     K = CXCursor_ObjCProtocolExpr;
545     break;
546 
547   case Stmt::ObjCBoolLiteralExprClass:
548     K = CXCursor_ObjCBoolLiteralExpr;
549     break;
550 
551   case Stmt::ObjCAvailabilityCheckExprClass:
552     K = CXCursor_ObjCAvailabilityCheckExpr;
553     break;
554 
555   case Stmt::ObjCBridgedCastExprClass:
556     K = CXCursor_ObjCBridgedCastExpr;
557     break;
558 
559   case Stmt::BlockExprClass:
560     K = CXCursor_BlockExpr;
561     break;
562 
563   case Stmt::PackExpansionExprClass:
564     K = CXCursor_PackExpansionExpr;
565     break;
566 
567   case Stmt::SizeOfPackExprClass:
568     K = CXCursor_SizeOfPackExpr;
569     break;
570 
571   case Stmt::DeclRefExprClass:
572     if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
573             cast<DeclRefExpr>(S)->getDecl())) {
574       if (const ObjCMethodDecl *MD =
575               dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
576         if (MD->getSelfDecl() == IPD) {
577           K = CXCursor_ObjCSelfExpr;
578           break;
579         }
580       }
581     }
582 
583     K = CXCursor_DeclRefExpr;
584     break;
585 
586   case Stmt::DependentScopeDeclRefExprClass:
587   case Stmt::SubstNonTypeTemplateParmExprClass:
588   case Stmt::SubstNonTypeTemplateParmPackExprClass:
589   case Stmt::FunctionParmPackExprClass:
590   case Stmt::UnresolvedLookupExprClass:
591   case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
592     K = CXCursor_DeclRefExpr;
593     break;
594 
595   case Stmt::CXXDependentScopeMemberExprClass:
596   case Stmt::CXXPseudoDestructorExprClass:
597   case Stmt::MemberExprClass:
598   case Stmt::MSPropertyRefExprClass:
599   case Stmt::ObjCIsaExprClass:
600   case Stmt::ObjCIvarRefExprClass:
601   case Stmt::ObjCPropertyRefExprClass:
602   case Stmt::UnresolvedMemberExprClass:
603     K = CXCursor_MemberRefExpr;
604     break;
605 
606   case Stmt::CallExprClass:
607   case Stmt::CXXOperatorCallExprClass:
608   case Stmt::CXXMemberCallExprClass:
609   case Stmt::CUDAKernelCallExprClass:
610   case Stmt::CXXConstructExprClass:
611   case Stmt::CXXInheritedCtorInitExprClass:
612   case Stmt::CXXTemporaryObjectExprClass:
613   case Stmt::CXXUnresolvedConstructExprClass:
614   case Stmt::UserDefinedLiteralClass:
615     K = CXCursor_CallExpr;
616     break;
617 
618   case Stmt::LambdaExprClass:
619     K = CXCursor_LambdaExpr;
620     break;
621 
622   case Stmt::ObjCMessageExprClass: {
623     K = CXCursor_ObjCMessageExpr;
624     int SelectorIdIndex = -1;
625     // Check if cursor points to a selector id.
626     if (RegionOfInterest.isValid() &&
627         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
628       SmallVector<SourceLocation, 16> SelLocs;
629       cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
630       SmallVectorImpl<SourceLocation>::iterator I =
631           llvm::find(SelLocs, RegionOfInterest.getBegin());
632       if (I != SelLocs.end())
633         SelectorIdIndex = I - SelLocs.begin();
634     }
635     CXCursor C = {K, 0, {Parent, S, TU}};
636     return getSelectorIdentifierCursor(SelectorIdIndex, C);
637   }
638 
639   case Stmt::MSDependentExistsStmtClass:
640     K = CXCursor_UnexposedStmt;
641     break;
642   case Stmt::OMPParallelDirectiveClass:
643     K = CXCursor_OMPParallelDirective;
644     break;
645   case Stmt::OMPSimdDirectiveClass:
646     K = CXCursor_OMPSimdDirective;
647     break;
648   case Stmt::OMPForDirectiveClass:
649     K = CXCursor_OMPForDirective;
650     break;
651   case Stmt::OMPForSimdDirectiveClass:
652     K = CXCursor_OMPForSimdDirective;
653     break;
654   case Stmt::OMPSectionsDirectiveClass:
655     K = CXCursor_OMPSectionsDirective;
656     break;
657   case Stmt::OMPSectionDirectiveClass:
658     K = CXCursor_OMPSectionDirective;
659     break;
660   case Stmt::OMPSingleDirectiveClass:
661     K = CXCursor_OMPSingleDirective;
662     break;
663   case Stmt::OMPMasterDirectiveClass:
664     K = CXCursor_OMPMasterDirective;
665     break;
666   case Stmt::OMPCriticalDirectiveClass:
667     K = CXCursor_OMPCriticalDirective;
668     break;
669   case Stmt::OMPParallelForDirectiveClass:
670     K = CXCursor_OMPParallelForDirective;
671     break;
672   case Stmt::OMPParallelForSimdDirectiveClass:
673     K = CXCursor_OMPParallelForSimdDirective;
674     break;
675   case Stmt::OMPParallelMasterDirectiveClass:
676     K = CXCursor_OMPParallelMasterDirective;
677     break;
678   case Stmt::OMPParallelSectionsDirectiveClass:
679     K = CXCursor_OMPParallelSectionsDirective;
680     break;
681   case Stmt::OMPTaskDirectiveClass:
682     K = CXCursor_OMPTaskDirective;
683     break;
684   case Stmt::OMPTaskyieldDirectiveClass:
685     K = CXCursor_OMPTaskyieldDirective;
686     break;
687   case Stmt::OMPBarrierDirectiveClass:
688     K = CXCursor_OMPBarrierDirective;
689     break;
690   case Stmt::OMPTaskwaitDirectiveClass:
691     K = CXCursor_OMPTaskwaitDirective;
692     break;
693   case Stmt::OMPTaskgroupDirectiveClass:
694     K = CXCursor_OMPTaskgroupDirective;
695     break;
696   case Stmt::OMPFlushDirectiveClass:
697     K = CXCursor_OMPFlushDirective;
698     break;
699   case Stmt::OMPDepobjDirectiveClass:
700     K = CXCursor_OMPDepobjDirective;
701     break;
702   case Stmt::OMPScanDirectiveClass:
703     K = CXCursor_OMPScanDirective;
704     break;
705   case Stmt::OMPOrderedDirectiveClass:
706     K = CXCursor_OMPOrderedDirective;
707     break;
708   case Stmt::OMPAtomicDirectiveClass:
709     K = CXCursor_OMPAtomicDirective;
710     break;
711   case Stmt::OMPTargetDirectiveClass:
712     K = CXCursor_OMPTargetDirective;
713     break;
714   case Stmt::OMPTargetDataDirectiveClass:
715     K = CXCursor_OMPTargetDataDirective;
716     break;
717   case Stmt::OMPTargetEnterDataDirectiveClass:
718     K = CXCursor_OMPTargetEnterDataDirective;
719     break;
720   case Stmt::OMPTargetExitDataDirectiveClass:
721     K = CXCursor_OMPTargetExitDataDirective;
722     break;
723   case Stmt::OMPTargetParallelDirectiveClass:
724     K = CXCursor_OMPTargetParallelDirective;
725     break;
726   case Stmt::OMPTargetParallelForDirectiveClass:
727     K = CXCursor_OMPTargetParallelForDirective;
728     break;
729   case Stmt::OMPTargetUpdateDirectiveClass:
730     K = CXCursor_OMPTargetUpdateDirective;
731     break;
732   case Stmt::OMPTeamsDirectiveClass:
733     K = CXCursor_OMPTeamsDirective;
734     break;
735   case Stmt::OMPCancellationPointDirectiveClass:
736     K = CXCursor_OMPCancellationPointDirective;
737     break;
738   case Stmt::OMPCancelDirectiveClass:
739     K = CXCursor_OMPCancelDirective;
740     break;
741   case Stmt::OMPTaskLoopDirectiveClass:
742     K = CXCursor_OMPTaskLoopDirective;
743     break;
744   case Stmt::OMPTaskLoopSimdDirectiveClass:
745     K = CXCursor_OMPTaskLoopSimdDirective;
746     break;
747   case Stmt::OMPMasterTaskLoopDirectiveClass:
748     K = CXCursor_OMPMasterTaskLoopDirective;
749     break;
750   case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
751     K = CXCursor_OMPMasterTaskLoopSimdDirective;
752     break;
753   case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
754     K = CXCursor_OMPParallelMasterTaskLoopDirective;
755     break;
756   case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
757     K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
758     break;
759   case Stmt::OMPDistributeDirectiveClass:
760     K = CXCursor_OMPDistributeDirective;
761     break;
762   case Stmt::OMPDistributeParallelForDirectiveClass:
763     K = CXCursor_OMPDistributeParallelForDirective;
764     break;
765   case Stmt::OMPDistributeParallelForSimdDirectiveClass:
766     K = CXCursor_OMPDistributeParallelForSimdDirective;
767     break;
768   case Stmt::OMPDistributeSimdDirectiveClass:
769     K = CXCursor_OMPDistributeSimdDirective;
770     break;
771   case Stmt::OMPTargetParallelForSimdDirectiveClass:
772     K = CXCursor_OMPTargetParallelForSimdDirective;
773     break;
774   case Stmt::OMPTargetSimdDirectiveClass:
775     K = CXCursor_OMPTargetSimdDirective;
776     break;
777   case Stmt::OMPTeamsDistributeDirectiveClass:
778     K = CXCursor_OMPTeamsDistributeDirective;
779     break;
780   case Stmt::OMPTeamsDistributeSimdDirectiveClass:
781     K = CXCursor_OMPTeamsDistributeSimdDirective;
782     break;
783   case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
784     K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
785     break;
786   case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
787     K = CXCursor_OMPTeamsDistributeParallelForDirective;
788     break;
789   case Stmt::OMPTargetTeamsDirectiveClass:
790     K = CXCursor_OMPTargetTeamsDirective;
791     break;
792   case Stmt::OMPTargetTeamsDistributeDirectiveClass:
793     K = CXCursor_OMPTargetTeamsDistributeDirective;
794     break;
795   case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
796     K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
797     break;
798   case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
799     K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
800     break;
801   case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
802     K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
803     break;
804   case Stmt::BuiltinBitCastExprClass:
805     K = CXCursor_BuiltinBitCastExpr;
806   }
807 
808   CXCursor C = {K, 0, {Parent, S, TU}};
809   return C;
810 }
811 
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl * Super,SourceLocation Loc,CXTranslationUnit TU)812 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
813                                                SourceLocation Loc,
814                                                CXTranslationUnit TU) {
815   assert(Super && TU && "Invalid arguments!");
816   void *RawLoc = Loc.getPtrEncoding();
817   CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
818   return C;
819 }
820 
821 std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCSuperClassRef(CXCursor C)822 cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
823   assert(C.kind == CXCursor_ObjCSuperClassRef);
824   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
825                         SourceLocation::getFromPtrEncoding(C.data[1]));
826 }
827 
MakeCursorObjCProtocolRef(const ObjCProtocolDecl * Proto,SourceLocation Loc,CXTranslationUnit TU)828 CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
829                                              SourceLocation Loc,
830                                              CXTranslationUnit TU) {
831   assert(Proto && TU && "Invalid arguments!");
832   void *RawLoc = Loc.getPtrEncoding();
833   CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
834   return C;
835 }
836 
837 std::pair<const ObjCProtocolDecl *, SourceLocation>
getCursorObjCProtocolRef(CXCursor C)838 cxcursor::getCursorObjCProtocolRef(CXCursor C) {
839   assert(C.kind == CXCursor_ObjCProtocolRef);
840   return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
841                         SourceLocation::getFromPtrEncoding(C.data[1]));
842 }
843 
MakeCursorObjCClassRef(const ObjCInterfaceDecl * Class,SourceLocation Loc,CXTranslationUnit TU)844 CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
845                                           SourceLocation Loc,
846                                           CXTranslationUnit TU) {
847   // 'Class' can be null for invalid code.
848   if (!Class)
849     return MakeCXCursorInvalid(CXCursor_InvalidCode);
850   assert(TU && "Invalid arguments!");
851   void *RawLoc = Loc.getPtrEncoding();
852   CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
853   return C;
854 }
855 
856 std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C)857 cxcursor::getCursorObjCClassRef(CXCursor C) {
858   assert(C.kind == CXCursor_ObjCClassRef);
859   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
860                         SourceLocation::getFromPtrEncoding(C.data[1]));
861 }
862 
MakeCursorTypeRef(const TypeDecl * Type,SourceLocation Loc,CXTranslationUnit TU)863 CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
864                                      CXTranslationUnit TU) {
865   assert(Type && TU && "Invalid arguments!");
866   void *RawLoc = Loc.getPtrEncoding();
867   CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
868   return C;
869 }
870 
871 std::pair<const TypeDecl *, SourceLocation>
getCursorTypeRef(CXCursor C)872 cxcursor::getCursorTypeRef(CXCursor C) {
873   assert(C.kind == CXCursor_TypeRef);
874   return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
875                         SourceLocation::getFromPtrEncoding(C.data[1]));
876 }
877 
MakeCursorTemplateRef(const TemplateDecl * Template,SourceLocation Loc,CXTranslationUnit TU)878 CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
879                                          SourceLocation Loc,
880                                          CXTranslationUnit TU) {
881   assert(Template && TU && "Invalid arguments!");
882   void *RawLoc = Loc.getPtrEncoding();
883   CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
884   return C;
885 }
886 
887 std::pair<const TemplateDecl *, SourceLocation>
getCursorTemplateRef(CXCursor C)888 cxcursor::getCursorTemplateRef(CXCursor C) {
889   assert(C.kind == CXCursor_TemplateRef);
890   return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
891                         SourceLocation::getFromPtrEncoding(C.data[1]));
892 }
893 
MakeCursorNamespaceRef(const NamedDecl * NS,SourceLocation Loc,CXTranslationUnit TU)894 CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
895                                           SourceLocation Loc,
896                                           CXTranslationUnit TU) {
897 
898   assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
899          "Invalid arguments!");
900   void *RawLoc = Loc.getPtrEncoding();
901   CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
902   return C;
903 }
904 
905 std::pair<const NamedDecl *, SourceLocation>
getCursorNamespaceRef(CXCursor C)906 cxcursor::getCursorNamespaceRef(CXCursor C) {
907   assert(C.kind == CXCursor_NamespaceRef);
908   return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
909                         SourceLocation::getFromPtrEncoding(C.data[1]));
910 }
911 
MakeCursorVariableRef(const VarDecl * Var,SourceLocation Loc,CXTranslationUnit TU)912 CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
913                                          CXTranslationUnit TU) {
914 
915   assert(Var && TU && "Invalid arguments!");
916   void *RawLoc = Loc.getPtrEncoding();
917   CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
918   return C;
919 }
920 
921 std::pair<const VarDecl *, SourceLocation>
getCursorVariableRef(CXCursor C)922 cxcursor::getCursorVariableRef(CXCursor C) {
923   assert(C.kind == CXCursor_VariableRef);
924   return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
925                         SourceLocation::getFromPtrEncoding(C.data[1]));
926 }
927 
MakeCursorMemberRef(const FieldDecl * Field,SourceLocation Loc,CXTranslationUnit TU)928 CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
929                                        SourceLocation Loc,
930                                        CXTranslationUnit TU) {
931 
932   assert(Field && TU && "Invalid arguments!");
933   void *RawLoc = Loc.getPtrEncoding();
934   CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
935   return C;
936 }
937 
938 std::pair<const FieldDecl *, SourceLocation>
getCursorMemberRef(CXCursor C)939 cxcursor::getCursorMemberRef(CXCursor C) {
940   assert(C.kind == CXCursor_MemberRef);
941   return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
942                         SourceLocation::getFromPtrEncoding(C.data[1]));
943 }
944 
MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier * B,CXTranslationUnit TU)945 CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
946                                               CXTranslationUnit TU) {
947   CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
948   return C;
949 }
950 
getCursorCXXBaseSpecifier(CXCursor C)951 const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
952   assert(C.kind == CXCursor_CXXBaseSpecifier);
953   return static_cast<const CXXBaseSpecifier *>(C.data[0]);
954 }
955 
MakePreprocessingDirectiveCursor(SourceRange Range,CXTranslationUnit TU)956 CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
957                                                     CXTranslationUnit TU) {
958   CXCursor C = {
959       CXCursor_PreprocessingDirective,
960       0,
961       {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
962   return C;
963 }
964 
getCursorPreprocessingDirective(CXCursor C)965 SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
966   assert(C.kind == CXCursor_PreprocessingDirective);
967   SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
968                     SourceLocation::getFromPtrEncoding(C.data[1]));
969   ASTUnit *TU = getCursorASTUnit(C);
970   return TU->mapRangeFromPreamble(Range);
971 }
972 
MakeMacroDefinitionCursor(const MacroDefinitionRecord * MI,CXTranslationUnit TU)973 CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
974                                              CXTranslationUnit TU) {
975   CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
976   return C;
977 }
978 
getCursorMacroDefinition(CXCursor C)979 const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
980   assert(C.kind == CXCursor_MacroDefinition);
981   return static_cast<const MacroDefinitionRecord *>(C.data[0]);
982 }
983 
MakeMacroExpansionCursor(MacroExpansion * MI,CXTranslationUnit TU)984 CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
985                                             CXTranslationUnit TU) {
986   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
987   return C;
988 }
989 
MakeMacroExpansionCursor(MacroDefinitionRecord * MI,SourceLocation Loc,CXTranslationUnit TU)990 CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
991                                             SourceLocation Loc,
992                                             CXTranslationUnit TU) {
993   assert(Loc.isValid());
994   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
995   return C;
996 }
997 
getName() const998 const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
999   if (isPseudo())
1000     return getAsMacroDefinition()->getName();
1001   return getAsMacroExpansion()->getName();
1002 }
1003 const MacroDefinitionRecord *
getDefinition() const1004 cxcursor::MacroExpansionCursor::getDefinition() const {
1005   if (isPseudo())
1006     return getAsMacroDefinition();
1007   return getAsMacroExpansion()->getDefinition();
1008 }
getSourceRange() const1009 SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
1010   if (isPseudo())
1011     return getPseudoLoc();
1012   return getAsMacroExpansion()->getSourceRange();
1013 }
1014 
MakeInclusionDirectiveCursor(InclusionDirective * ID,CXTranslationUnit TU)1015 CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
1016                                                 CXTranslationUnit TU) {
1017   CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
1018   return C;
1019 }
1020 
getCursorInclusionDirective(CXCursor C)1021 const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
1022   assert(C.kind == CXCursor_InclusionDirective);
1023   return static_cast<const InclusionDirective *>(C.data[0]);
1024 }
1025 
MakeCursorLabelRef(LabelStmt * Label,SourceLocation Loc,CXTranslationUnit TU)1026 CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
1027                                       CXTranslationUnit TU) {
1028 
1029   assert(Label && TU && "Invalid arguments!");
1030   void *RawLoc = Loc.getPtrEncoding();
1031   CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
1032   return C;
1033 }
1034 
1035 std::pair<const LabelStmt *, SourceLocation>
getCursorLabelRef(CXCursor C)1036 cxcursor::getCursorLabelRef(CXCursor C) {
1037   assert(C.kind == CXCursor_LabelRef);
1038   return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
1039                         SourceLocation::getFromPtrEncoding(C.data[1]));
1040 }
1041 
MakeCursorOverloadedDeclRef(const OverloadExpr * E,CXTranslationUnit TU)1042 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
1043                                                CXTranslationUnit TU) {
1044   assert(E && TU && "Invalid arguments!");
1045   OverloadedDeclRefStorage Storage(E);
1046   void *RawLoc = E->getNameLoc().getPtrEncoding();
1047   CXCursor C = {
1048       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1049   return C;
1050 }
1051 
MakeCursorOverloadedDeclRef(const Decl * D,SourceLocation Loc,CXTranslationUnit TU)1052 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
1053                                                SourceLocation Loc,
1054                                                CXTranslationUnit TU) {
1055   assert(D && TU && "Invalid arguments!");
1056   void *RawLoc = Loc.getPtrEncoding();
1057   OverloadedDeclRefStorage Storage(D);
1058   CXCursor C = {
1059       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1060   return C;
1061 }
1062 
MakeCursorOverloadedDeclRef(TemplateName Name,SourceLocation Loc,CXTranslationUnit TU)1063 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
1064                                                SourceLocation Loc,
1065                                                CXTranslationUnit TU) {
1066   assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
1067   void *RawLoc = Loc.getPtrEncoding();
1068   OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
1069   CXCursor C = {
1070       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1071   return C;
1072 }
1073 
1074 std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
getCursorOverloadedDeclRef(CXCursor C)1075 cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
1076   assert(C.kind == CXCursor_OverloadedDeclRef);
1077   return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
1078                             const_cast<void *>(C.data[0])),
1079                         SourceLocation::getFromPtrEncoding(C.data[1]));
1080 }
1081 
getCursorDecl(CXCursor Cursor)1082 const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
1083   return static_cast<const Decl *>(Cursor.data[0]);
1084 }
1085 
getCursorExpr(CXCursor Cursor)1086 const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
1087   return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
1088 }
1089 
getCursorStmt(CXCursor Cursor)1090 const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
1091   if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
1092       Cursor.kind == CXCursor_ObjCProtocolRef ||
1093       Cursor.kind == CXCursor_ObjCClassRef)
1094     return nullptr;
1095 
1096   return static_cast<const Stmt *>(Cursor.data[1]);
1097 }
1098 
getCursorAttr(CXCursor Cursor)1099 const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
1100   return static_cast<const Attr *>(Cursor.data[1]);
1101 }
1102 
getCursorContext(CXCursor Cursor)1103 ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
1104   return getCursorASTUnit(Cursor)->getASTContext();
1105 }
1106 
getCursorASTUnit(CXCursor Cursor)1107 ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
1108   CXTranslationUnit TU = getCursorTU(Cursor);
1109   if (!TU)
1110     return nullptr;
1111   return cxtu::getASTUnit(TU);
1112 }
1113 
getCursorTU(CXCursor Cursor)1114 CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
1115   return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
1116 }
1117 
getOverriddenCursors(CXCursor cursor,SmallVectorImpl<CXCursor> & overridden)1118 void cxcursor::getOverriddenCursors(CXCursor cursor,
1119                                     SmallVectorImpl<CXCursor> &overridden) {
1120   assert(clang_isDeclaration(cursor.kind));
1121   const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
1122   if (!D)
1123     return;
1124 
1125   CXTranslationUnit TU = getCursorTU(cursor);
1126   SmallVector<const NamedDecl *, 8> OverDecls;
1127   D->getASTContext().getOverriddenMethods(D, OverDecls);
1128 
1129   for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1130                                                     E = OverDecls.end();
1131        I != E; ++I) {
1132     overridden.push_back(MakeCXCursor(*I, TU));
1133   }
1134 }
1135 
1136 std::pair<int, SourceLocation>
getSelectorIdentifierIndexAndLoc(CXCursor cursor)1137 cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
1138   if (cursor.kind == CXCursor_ObjCMessageExpr) {
1139     if (cursor.xdata != -1)
1140       return std::make_pair(cursor.xdata,
1141                             cast<ObjCMessageExpr>(getCursorExpr(cursor))
1142                                 ->getSelectorLoc(cursor.xdata));
1143   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1144              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1145     if (cursor.xdata != -1)
1146       return std::make_pair(cursor.xdata,
1147                             cast<ObjCMethodDecl>(getCursorDecl(cursor))
1148                                 ->getSelectorLoc(cursor.xdata));
1149   }
1150 
1151   return std::make_pair(-1, SourceLocation());
1152 }
1153 
getSelectorIdentifierCursor(int SelIdx,CXCursor cursor)1154 CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
1155   CXCursor newCursor = cursor;
1156 
1157   if (cursor.kind == CXCursor_ObjCMessageExpr) {
1158     if (SelIdx == -1 ||
1159         unsigned(SelIdx) >=
1160             cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
1161       newCursor.xdata = -1;
1162     else
1163       newCursor.xdata = SelIdx;
1164   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1165              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1166     if (SelIdx == -1 ||
1167         unsigned(SelIdx) >=
1168             cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
1169       newCursor.xdata = -1;
1170     else
1171       newCursor.xdata = SelIdx;
1172   }
1173 
1174   return newCursor;
1175 }
1176 
getTypeRefCursor(CXCursor cursor)1177 CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1178   if (cursor.kind != CXCursor_CallExpr)
1179     return cursor;
1180 
1181   if (cursor.xdata == 0)
1182     return cursor;
1183 
1184   const Expr *E = getCursorExpr(cursor);
1185   TypeSourceInfo *Type = nullptr;
1186   if (const CXXUnresolvedConstructExpr *UnCtor =
1187           dyn_cast<CXXUnresolvedConstructExpr>(E)) {
1188     Type = UnCtor->getTypeSourceInfo();
1189   } else if (const CXXTemporaryObjectExpr *Tmp =
1190                  dyn_cast<CXXTemporaryObjectExpr>(E)) {
1191     Type = Tmp->getTypeSourceInfo();
1192   }
1193 
1194   if (!Type)
1195     return cursor;
1196 
1197   CXTranslationUnit TU = getCursorTU(cursor);
1198   QualType Ty = Type->getType();
1199   TypeLoc TL = Type->getTypeLoc();
1200   SourceLocation Loc = TL.getBeginLoc();
1201 
1202   if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
1203     Ty = ElabT->getNamedType();
1204     ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
1205     Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
1206   }
1207 
1208   if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
1209     return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
1210   if (const TagType *Tag = Ty->getAs<TagType>())
1211     return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
1212   if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
1213     return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
1214 
1215   return cursor;
1216 }
1217 
operator ==(CXCursor X,CXCursor Y)1218 bool cxcursor::operator==(CXCursor X, CXCursor Y) {
1219   return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
1220          X.data[2] == Y.data[2];
1221 }
1222 
1223 // FIXME: Remove once we can model DeclGroups and their appropriate ranges
1224 // properly in the ASTs.
isFirstInDeclGroup(CXCursor C)1225 bool cxcursor::isFirstInDeclGroup(CXCursor C) {
1226   assert(clang_isDeclaration(C.kind));
1227   return ((uintptr_t)(C.data[1])) != 0;
1228 }
1229 
1230 //===----------------------------------------------------------------------===//
1231 // libclang CXCursor APIs
1232 //===----------------------------------------------------------------------===//
1233 
clang_Cursor_isNull(CXCursor cursor)1234 int clang_Cursor_isNull(CXCursor cursor) {
1235   return clang_equalCursors(cursor, clang_getNullCursor());
1236 }
1237 
clang_Cursor_getTranslationUnit(CXCursor cursor)1238 CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
1239   return getCursorTU(cursor);
1240 }
1241 
clang_Cursor_getNumArguments(CXCursor C)1242 int clang_Cursor_getNumArguments(CXCursor C) {
1243   if (clang_isDeclaration(C.kind)) {
1244     const Decl *D = cxcursor::getCursorDecl(C);
1245     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1246       return MD->param_size();
1247     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1248       return FD->param_size();
1249   }
1250 
1251   if (clang_isExpression(C.kind)) {
1252     const Expr *E = cxcursor::getCursorExpr(C);
1253     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1254       return CE->getNumArgs();
1255     }
1256     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1257       return CE->getNumArgs();
1258     }
1259   }
1260 
1261   return -1;
1262 }
1263 
clang_Cursor_getArgument(CXCursor C,unsigned i)1264 CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1265   if (clang_isDeclaration(C.kind)) {
1266     const Decl *D = cxcursor::getCursorDecl(C);
1267     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
1268       if (i < MD->param_size())
1269         return cxcursor::MakeCXCursor(MD->parameters()[i],
1270                                       cxcursor::getCursorTU(C));
1271     } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1272       if (i < FD->param_size())
1273         return cxcursor::MakeCXCursor(FD->parameters()[i],
1274                                       cxcursor::getCursorTU(C));
1275     }
1276   }
1277 
1278   if (clang_isExpression(C.kind)) {
1279     const Expr *E = cxcursor::getCursorExpr(C);
1280     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1281       if (i < CE->getNumArgs()) {
1282         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1283                                       cxcursor::getCursorTU(C));
1284       }
1285     }
1286     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1287       if (i < CE->getNumArgs()) {
1288         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1289                                       cxcursor::getCursorTU(C));
1290       }
1291     }
1292   }
1293 
1294   return clang_getNullCursor();
1295 }
1296 
clang_Cursor_getNumTemplateArguments(CXCursor C)1297 int clang_Cursor_getNumTemplateArguments(CXCursor C) {
1298   if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
1299     return -1;
1300   }
1301 
1302   const FunctionDecl *FD =
1303       llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
1304   if (!FD) {
1305     return -1;
1306   }
1307 
1308   const FunctionTemplateSpecializationInfo *SpecInfo =
1309       FD->getTemplateSpecializationInfo();
1310   if (!SpecInfo) {
1311     return -1;
1312   }
1313 
1314   return SpecInfo->TemplateArguments->size();
1315 }
1316 
1317 enum CXGetTemplateArgumentStatus {
1318   /** The operation completed successfully */
1319   CXGetTemplateArgumentStatus_Success = 0,
1320 
1321   /** The specified cursor did not represent a FunctionDecl. */
1322   CXGetTemplateArgumentStatus_CursorNotFunctionDecl = -1,
1323 
1324   /** The specified cursor was not castable to a FunctionDecl. */
1325   CXGetTemplateArgumentStatus_BadFunctionDeclCast = -2,
1326 
1327   /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
1328   CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
1329 
1330   /** An invalid (OOB) argument index was specified */
1331   CXGetTemplateArgumentStatus_InvalidIndex = -4
1332 };
1333 
clang_Cursor_getTemplateArgument(CXCursor C,unsigned I,TemplateArgument * TA)1334 static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1335                                             TemplateArgument *TA) {
1336   if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
1337     return CXGetTemplateArgumentStatus_CursorNotFunctionDecl;
1338   }
1339 
1340   const FunctionDecl *FD =
1341       llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
1342   if (!FD) {
1343     return CXGetTemplateArgumentStatus_BadFunctionDeclCast;
1344   }
1345 
1346   const FunctionTemplateSpecializationInfo *SpecInfo =
1347       FD->getTemplateSpecializationInfo();
1348   if (!SpecInfo) {
1349     return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
1350   }
1351 
1352   if (I >= SpecInfo->TemplateArguments->size()) {
1353     return CXGetTemplateArgumentStatus_InvalidIndex;
1354   }
1355 
1356   *TA = SpecInfo->TemplateArguments->get(I);
1357   return 0;
1358 }
1359 
clang_Cursor_getTemplateArgumentKind(CXCursor C,unsigned I)1360 enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
1361                                                                  unsigned I) {
1362   TemplateArgument TA;
1363   if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
1364     return CXTemplateArgumentKind_Invalid;
1365   }
1366 
1367   switch (TA.getKind()) {
1368   case TemplateArgument::Null:
1369     return CXTemplateArgumentKind_Null;
1370   case TemplateArgument::Type:
1371     return CXTemplateArgumentKind_Type;
1372   case TemplateArgument::Declaration:
1373     return CXTemplateArgumentKind_Declaration;
1374   case TemplateArgument::NullPtr:
1375     return CXTemplateArgumentKind_NullPtr;
1376   case TemplateArgument::Integral:
1377     return CXTemplateArgumentKind_Integral;
1378   case TemplateArgument::Template:
1379     return CXTemplateArgumentKind_Template;
1380   case TemplateArgument::TemplateExpansion:
1381     return CXTemplateArgumentKind_TemplateExpansion;
1382   case TemplateArgument::Expression:
1383     return CXTemplateArgumentKind_Expression;
1384   case TemplateArgument::Pack:
1385     return CXTemplateArgumentKind_Pack;
1386   }
1387 
1388   return CXTemplateArgumentKind_Invalid;
1389 }
1390 
clang_Cursor_getTemplateArgumentType(CXCursor C,unsigned I)1391 CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
1392   TemplateArgument TA;
1393   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1394       CXGetTemplateArgumentStatus_Success) {
1395     return cxtype::MakeCXType(QualType(), getCursorTU(C));
1396   }
1397 
1398   if (TA.getKind() != TemplateArgument::Type) {
1399     return cxtype::MakeCXType(QualType(), getCursorTU(C));
1400   }
1401 
1402   return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
1403 }
1404 
clang_Cursor_getTemplateArgumentValue(CXCursor C,unsigned I)1405 long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
1406   TemplateArgument TA;
1407   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1408       CXGetTemplateArgumentStatus_Success) {
1409     assert(0 && "Unable to retrieve TemplateArgument");
1410     return 0;
1411   }
1412 
1413   if (TA.getKind() != TemplateArgument::Integral) {
1414     assert(0 && "Passed template argument is not Integral");
1415     return 0;
1416   }
1417 
1418   return TA.getAsIntegral().getSExtValue();
1419 }
1420 
clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,unsigned I)1421 unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
1422                                                                  unsigned I) {
1423   TemplateArgument TA;
1424   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1425       CXGetTemplateArgumentStatus_Success) {
1426     assert(0 && "Unable to retrieve TemplateArgument");
1427     return 0;
1428   }
1429 
1430   if (TA.getKind() != TemplateArgument::Integral) {
1431     assert(0 && "Passed template argument is not Integral");
1432     return 0;
1433   }
1434 
1435   return TA.getAsIntegral().getZExtValue();
1436 }
1437 
1438 //===----------------------------------------------------------------------===//
1439 // CXCursorSet.
1440 //===----------------------------------------------------------------------===//
1441 
1442 typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1443 
packCXCursorSet(CXCursorSet_Impl * setImpl)1444 static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1445   return (CXCursorSet)setImpl;
1446 }
unpackCXCursorSet(CXCursorSet set)1447 static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1448   return (CXCursorSet_Impl *)set;
1449 }
1450 namespace llvm {
1451 template <> struct DenseMapInfo<CXCursor> {
1452 public:
getEmptyKeyllvm::DenseMapInfo1453   static inline CXCursor getEmptyKey() {
1454     return MakeCXCursorInvalid(CXCursor_InvalidFile);
1455   }
getTombstoneKeyllvm::DenseMapInfo1456   static inline CXCursor getTombstoneKey() {
1457     return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1458   }
getHashValuellvm::DenseMapInfo1459   static inline unsigned getHashValue(const CXCursor &cursor) {
1460     return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1461         getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1462   }
isEqualllvm::DenseMapInfo1463   static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1464     return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
1465   }
1466 };
1467 } // namespace llvm
1468 
clang_createCXCursorSet()1469 CXCursorSet clang_createCXCursorSet() {
1470   return packCXCursorSet(new CXCursorSet_Impl());
1471 }
1472 
clang_disposeCXCursorSet(CXCursorSet set)1473 void clang_disposeCXCursorSet(CXCursorSet set) {
1474   delete unpackCXCursorSet(set);
1475 }
1476 
clang_CXCursorSet_contains(CXCursorSet set,CXCursor cursor)1477 unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1478   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1479   if (!setImpl)
1480     return 0;
1481   return setImpl->find(cursor) != setImpl->end();
1482 }
1483 
clang_CXCursorSet_insert(CXCursorSet set,CXCursor cursor)1484 unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1485   // Do not insert invalid cursors into the set.
1486   if (cursor.kind >= CXCursor_FirstInvalid &&
1487       cursor.kind <= CXCursor_LastInvalid)
1488     return 1;
1489 
1490   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1491   if (!setImpl)
1492     return 1;
1493   unsigned &entry = (*setImpl)[cursor];
1494   unsigned flag = entry == 0 ? 1 : 0;
1495   entry = 1;
1496   return flag;
1497 }
1498 
clang_getCursorCompletionString(CXCursor cursor)1499 CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1500   enum CXCursorKind kind = clang_getCursorKind(cursor);
1501   if (clang_isDeclaration(kind)) {
1502     const Decl *decl = getCursorDecl(cursor);
1503     if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
1504       ASTUnit *unit = getCursorASTUnit(cursor);
1505       CodeCompletionResult Result(namedDecl, CCP_Declaration);
1506       CodeCompletionString *String = Result.CreateCodeCompletionString(
1507           unit->getASTContext(), unit->getPreprocessor(),
1508           CodeCompletionContext::CCC_Other,
1509           unit->getCodeCompletionTUInfo().getAllocator(),
1510           unit->getCodeCompletionTUInfo(), true);
1511       return String;
1512     }
1513   } else if (kind == CXCursor_MacroDefinition) {
1514     const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
1515     const IdentifierInfo *Macro = definition->getName();
1516     ASTUnit *unit = getCursorASTUnit(cursor);
1517     CodeCompletionResult Result(
1518         Macro,
1519         unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
1520     CodeCompletionString *String = Result.CreateCodeCompletionString(
1521         unit->getASTContext(), unit->getPreprocessor(),
1522         CodeCompletionContext::CCC_Other,
1523         unit->getCodeCompletionTUInfo().getAllocator(),
1524         unit->getCodeCompletionTUInfo(), false);
1525     return String;
1526   }
1527   return nullptr;
1528 }
1529 
1530 namespace {
1531 struct OverridenCursorsPool {
1532   typedef SmallVector<CXCursor, 2> CursorVec;
1533   std::vector<CursorVec *> AllCursors;
1534   std::vector<CursorVec *> AvailableCursors;
1535 
~OverridenCursorsPool__anon1606f3080111::OverridenCursorsPool1536   ~OverridenCursorsPool() {
1537     for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1538                                             E = AllCursors.end();
1539          I != E; ++I) {
1540       delete *I;
1541     }
1542   }
1543 };
1544 } // namespace
1545 
createOverridenCXCursorsPool()1546 void *cxcursor::createOverridenCXCursorsPool() {
1547   return new OverridenCursorsPool();
1548 }
1549 
disposeOverridenCXCursorsPool(void * pool)1550 void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1551   delete static_cast<OverridenCursorsPool *>(pool);
1552 }
1553 
clang_getOverriddenCursors(CXCursor cursor,CXCursor ** overridden,unsigned * num_overridden)1554 void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
1555                                 unsigned *num_overridden) {
1556   if (overridden)
1557     *overridden = nullptr;
1558   if (num_overridden)
1559     *num_overridden = 0;
1560 
1561   CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1562 
1563   if (!overridden || !num_overridden || !TU)
1564     return;
1565 
1566   if (!clang_isDeclaration(cursor.kind))
1567     return;
1568 
1569   OverridenCursorsPool &pool =
1570       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1571 
1572   OverridenCursorsPool::CursorVec *Vec = nullptr;
1573 
1574   if (!pool.AvailableCursors.empty()) {
1575     Vec = pool.AvailableCursors.back();
1576     pool.AvailableCursors.pop_back();
1577   } else {
1578     Vec = new OverridenCursorsPool::CursorVec();
1579     pool.AllCursors.push_back(Vec);
1580   }
1581 
1582   // Clear out the vector, but don't free the memory contents.  This
1583   // reduces malloc() traffic.
1584   Vec->clear();
1585 
1586   // Use the first entry to contain a back reference to the vector.
1587   // This is a complete hack.
1588   CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1589   backRefCursor.data[0] = Vec;
1590   assert(cxcursor::getCursorTU(backRefCursor) == TU);
1591   Vec->push_back(backRefCursor);
1592 
1593   // Get the overridden cursors.
1594   cxcursor::getOverriddenCursors(cursor, *Vec);
1595 
1596   // Did we get any overridden cursors?  If not, return Vec to the pool
1597   // of available cursor vectors.
1598   if (Vec->size() == 1) {
1599     pool.AvailableCursors.push_back(Vec);
1600     return;
1601   }
1602 
1603   // Now tell the caller about the overridden cursors.
1604   assert(Vec->size() > 1);
1605   *overridden = &((*Vec)[1]);
1606   *num_overridden = Vec->size() - 1;
1607 }
1608 
clang_disposeOverriddenCursors(CXCursor * overridden)1609 void clang_disposeOverriddenCursors(CXCursor *overridden) {
1610   if (!overridden)
1611     return;
1612 
1613   // Use pointer arithmetic to get back the first faux entry
1614   // which has a back-reference to the TU and the vector.
1615   --overridden;
1616   OverridenCursorsPool::CursorVec *Vec =
1617       static_cast<OverridenCursorsPool::CursorVec *>(
1618           const_cast<void *>(overridden->data[0]));
1619   CXTranslationUnit TU = getCursorTU(*overridden);
1620 
1621   assert(Vec && TU);
1622 
1623   OverridenCursorsPool &pool =
1624       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1625 
1626   pool.AvailableCursors.push_back(Vec);
1627 }
1628 
clang_Cursor_isDynamicCall(CXCursor C)1629 int clang_Cursor_isDynamicCall(CXCursor C) {
1630   const Expr *E = nullptr;
1631   if (clang_isExpression(C.kind))
1632     E = getCursorExpr(C);
1633   if (!E)
1634     return 0;
1635 
1636   if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
1637     if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
1638       return false;
1639     if (auto *RecE = dyn_cast<ObjCMessageExpr>(
1640             MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
1641       if (RecE->getMethodFamily() == OMF_alloc)
1642         return false;
1643     }
1644     return true;
1645   }
1646 
1647   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1648     return !PropRefE->isSuperReceiver();
1649   }
1650 
1651   const MemberExpr *ME = nullptr;
1652   if (isa<MemberExpr>(E))
1653     ME = cast<MemberExpr>(E);
1654   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1655     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1656 
1657   if (ME) {
1658     if (const CXXMethodDecl *MD =
1659             dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1660       return MD->isVirtual() &&
1661              ME->performsVirtualDispatch(
1662                  cxcursor::getCursorContext(C).getLangOpts());
1663   }
1664 
1665   return 0;
1666 }
1667 
clang_Cursor_getReceiverType(CXCursor C)1668 CXType clang_Cursor_getReceiverType(CXCursor C) {
1669   CXTranslationUnit TU = cxcursor::getCursorTU(C);
1670   const Expr *E = nullptr;
1671   if (clang_isExpression(C.kind))
1672     E = getCursorExpr(C);
1673 
1674   if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1675     return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1676 
1677   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1678     return cxtype::MakeCXType(
1679         PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
1680   }
1681 
1682   const MemberExpr *ME = nullptr;
1683   if (isa<MemberExpr>(E))
1684     ME = cast<MemberExpr>(E);
1685   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1686     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1687 
1688   if (ME) {
1689     if (dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl())) {
1690       auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
1691       return cxtype::MakeCXType(receiverTy, TU);
1692     }
1693   }
1694 
1695   return cxtype::MakeCXType(QualType(), TU);
1696 }
1697