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::DependentCoawaitExprClass:
303   case Stmt::CoyieldExprClass:
304   case Stmt::CXXBindTemporaryExprClass:
305   case Stmt::CXXDefaultArgExprClass:
306   case Stmt::CXXDefaultInitExprClass:
307   case Stmt::CXXFoldExprClass:
308   case Stmt::CXXRewrittenBinaryOperatorClass:
309   case Stmt::CXXStdInitializerListExprClass:
310   case Stmt::CXXScalarValueInitExprClass:
311   case Stmt::CXXUuidofExprClass:
312   case Stmt::ChooseExprClass:
313   case Stmt::DesignatedInitExprClass:
314   case Stmt::DesignatedInitUpdateExprClass:
315   case Stmt::ArrayInitLoopExprClass:
316   case Stmt::ArrayInitIndexExprClass:
317   case Stmt::ExprWithCleanupsClass:
318   case Stmt::ExpressionTraitExprClass:
319   case Stmt::ExtVectorElementExprClass:
320   case Stmt::ImplicitCastExprClass:
321   case Stmt::ImplicitValueInitExprClass:
322   case Stmt::NoInitExprClass:
323   case Stmt::MaterializeTemporaryExprClass:
324   case Stmt::ObjCIndirectCopyRestoreExprClass:
325   case Stmt::OffsetOfExprClass:
326   case Stmt::ParenListExprClass:
327   case Stmt::PredefinedExprClass:
328   case Stmt::ShuffleVectorExprClass:
329   case Stmt::SourceLocExprClass:
330   case Stmt::ConvertVectorExprClass:
331   case Stmt::VAArgExprClass:
332   case Stmt::ObjCArrayLiteralClass:
333   case Stmt::ObjCDictionaryLiteralClass:
334   case Stmt::ObjCBoxedExprClass:
335   case Stmt::ObjCSubscriptRefExprClass:
336   case Stmt::RecoveryExprClass:
337   case Stmt::SYCLUniqueStableNameExprClass:
338     K = CXCursor_UnexposedExpr;
339     break;
340 
341   case Stmt::OpaqueValueExprClass:
342     if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
343       return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
344     K = CXCursor_UnexposedExpr;
345     break;
346 
347   case Stmt::PseudoObjectExprClass:
348     return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
349                         TU, RegionOfInterest);
350 
351   case Stmt::CompoundStmtClass:
352     K = CXCursor_CompoundStmt;
353     break;
354 
355   case Stmt::NullStmtClass:
356     K = CXCursor_NullStmt;
357     break;
358 
359   case Stmt::LabelStmtClass:
360     K = CXCursor_LabelStmt;
361     break;
362 
363   case Stmt::AttributedStmtClass:
364     K = CXCursor_UnexposedStmt;
365     break;
366 
367   case Stmt::DeclStmtClass:
368     K = CXCursor_DeclStmt;
369     break;
370 
371   case Stmt::CapturedStmtClass:
372     K = CXCursor_UnexposedStmt;
373     break;
374 
375   case Stmt::IntegerLiteralClass:
376     K = CXCursor_IntegerLiteral;
377     break;
378 
379   case Stmt::FixedPointLiteralClass:
380     K = CXCursor_FixedPointLiteral;
381     break;
382 
383   case Stmt::FloatingLiteralClass:
384     K = CXCursor_FloatingLiteral;
385     break;
386 
387   case Stmt::ImaginaryLiteralClass:
388     K = CXCursor_ImaginaryLiteral;
389     break;
390 
391   case Stmt::StringLiteralClass:
392     K = CXCursor_StringLiteral;
393     break;
394 
395   case Stmt::CharacterLiteralClass:
396     K = CXCursor_CharacterLiteral;
397     break;
398 
399   case Stmt::ConstantExprClass:
400     return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
401                         RegionOfInterest);
402 
403   case Stmt::ParenExprClass:
404     K = CXCursor_ParenExpr;
405     break;
406 
407   case Stmt::UnaryOperatorClass:
408     K = CXCursor_UnaryOperator;
409     break;
410 
411   case Stmt::UnaryExprOrTypeTraitExprClass:
412   case Stmt::CXXNoexceptExprClass:
413     K = CXCursor_UnaryExpr;
414     break;
415 
416   case Stmt::MSPropertySubscriptExprClass:
417   case Stmt::ArraySubscriptExprClass:
418     K = CXCursor_ArraySubscriptExpr;
419     break;
420 
421   case Stmt::MatrixSubscriptExprClass:
422     // TODO: add support for MatrixSubscriptExpr.
423     K = CXCursor_UnexposedExpr;
424     break;
425 
426   case Stmt::OMPArraySectionExprClass:
427     K = CXCursor_OMPArraySectionExpr;
428     break;
429 
430   case Stmt::OMPArrayShapingExprClass:
431     K = CXCursor_OMPArrayShapingExpr;
432     break;
433 
434   case Stmt::OMPIteratorExprClass:
435     K = CXCursor_OMPIteratorExpr;
436     break;
437 
438   case Stmt::BinaryOperatorClass:
439     K = CXCursor_BinaryOperator;
440     break;
441 
442   case Stmt::CompoundAssignOperatorClass:
443     K = CXCursor_CompoundAssignOperator;
444     break;
445 
446   case Stmt::ConditionalOperatorClass:
447     K = CXCursor_ConditionalOperator;
448     break;
449 
450   case Stmt::CStyleCastExprClass:
451     K = CXCursor_CStyleCastExpr;
452     break;
453 
454   case Stmt::CompoundLiteralExprClass:
455     K = CXCursor_CompoundLiteralExpr;
456     break;
457 
458   case Stmt::InitListExprClass:
459     K = CXCursor_InitListExpr;
460     break;
461 
462   case Stmt::AddrLabelExprClass:
463     K = CXCursor_AddrLabelExpr;
464     break;
465 
466   case Stmt::StmtExprClass:
467     K = CXCursor_StmtExpr;
468     break;
469 
470   case Stmt::GenericSelectionExprClass:
471     K = CXCursor_GenericSelectionExpr;
472     break;
473 
474   case Stmt::GNUNullExprClass:
475     K = CXCursor_GNUNullExpr;
476     break;
477 
478   case Stmt::CXXStaticCastExprClass:
479     K = CXCursor_CXXStaticCastExpr;
480     break;
481 
482   case Stmt::CXXDynamicCastExprClass:
483     K = CXCursor_CXXDynamicCastExpr;
484     break;
485 
486   case Stmt::CXXReinterpretCastExprClass:
487     K = CXCursor_CXXReinterpretCastExpr;
488     break;
489 
490   case Stmt::CXXConstCastExprClass:
491     K = CXCursor_CXXConstCastExpr;
492     break;
493 
494   case Stmt::CXXFunctionalCastExprClass:
495     K = CXCursor_CXXFunctionalCastExpr;
496     break;
497 
498   case Stmt::CXXAddrspaceCastExprClass:
499     K = CXCursor_CXXAddrspaceCastExpr;
500     break;
501 
502   case Stmt::CXXTypeidExprClass:
503     K = CXCursor_CXXTypeidExpr;
504     break;
505 
506   case Stmt::CXXBoolLiteralExprClass:
507     K = CXCursor_CXXBoolLiteralExpr;
508     break;
509 
510   case Stmt::CXXNullPtrLiteralExprClass:
511     K = CXCursor_CXXNullPtrLiteralExpr;
512     break;
513 
514   case Stmt::CXXThisExprClass:
515     K = CXCursor_CXXThisExpr;
516     break;
517 
518   case Stmt::CXXThrowExprClass:
519     K = CXCursor_CXXThrowExpr;
520     break;
521 
522   case Stmt::CXXNewExprClass:
523     K = CXCursor_CXXNewExpr;
524     break;
525 
526   case Stmt::CXXDeleteExprClass:
527     K = CXCursor_CXXDeleteExpr;
528     break;
529 
530   case Stmt::ObjCStringLiteralClass:
531     K = CXCursor_ObjCStringLiteral;
532     break;
533 
534   case Stmt::ObjCEncodeExprClass:
535     K = CXCursor_ObjCEncodeExpr;
536     break;
537 
538   case Stmt::ObjCSelectorExprClass:
539     K = CXCursor_ObjCSelectorExpr;
540     break;
541 
542   case Stmt::ObjCProtocolExprClass:
543     K = CXCursor_ObjCProtocolExpr;
544     break;
545 
546   case Stmt::ObjCBoolLiteralExprClass:
547     K = CXCursor_ObjCBoolLiteralExpr;
548     break;
549 
550   case Stmt::ObjCAvailabilityCheckExprClass:
551     K = CXCursor_ObjCAvailabilityCheckExpr;
552     break;
553 
554   case Stmt::ObjCBridgedCastExprClass:
555     K = CXCursor_ObjCBridgedCastExpr;
556     break;
557 
558   case Stmt::BlockExprClass:
559     K = CXCursor_BlockExpr;
560     break;
561 
562   case Stmt::PackExpansionExprClass:
563     K = CXCursor_PackExpansionExpr;
564     break;
565 
566   case Stmt::SizeOfPackExprClass:
567     K = CXCursor_SizeOfPackExpr;
568     break;
569 
570   case Stmt::DeclRefExprClass:
571     if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
572             cast<DeclRefExpr>(S)->getDecl())) {
573       if (const ObjCMethodDecl *MD =
574               dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
575         if (MD->getSelfDecl() == IPD) {
576           K = CXCursor_ObjCSelfExpr;
577           break;
578         }
579       }
580     }
581 
582     K = CXCursor_DeclRefExpr;
583     break;
584 
585   case Stmt::DependentScopeDeclRefExprClass:
586   case Stmt::SubstNonTypeTemplateParmExprClass:
587   case Stmt::SubstNonTypeTemplateParmPackExprClass:
588   case Stmt::FunctionParmPackExprClass:
589   case Stmt::UnresolvedLookupExprClass:
590   case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
591     K = CXCursor_DeclRefExpr;
592     break;
593 
594   case Stmt::CXXDependentScopeMemberExprClass:
595   case Stmt::CXXPseudoDestructorExprClass:
596   case Stmt::MemberExprClass:
597   case Stmt::MSPropertyRefExprClass:
598   case Stmt::ObjCIsaExprClass:
599   case Stmt::ObjCIvarRefExprClass:
600   case Stmt::ObjCPropertyRefExprClass:
601   case Stmt::UnresolvedMemberExprClass:
602     K = CXCursor_MemberRefExpr;
603     break;
604 
605   case Stmt::CallExprClass:
606   case Stmt::CXXOperatorCallExprClass:
607   case Stmt::CXXMemberCallExprClass:
608   case Stmt::CUDAKernelCallExprClass:
609   case Stmt::CXXConstructExprClass:
610   case Stmt::CXXInheritedCtorInitExprClass:
611   case Stmt::CXXTemporaryObjectExprClass:
612   case Stmt::CXXUnresolvedConstructExprClass:
613   case Stmt::UserDefinedLiteralClass:
614     K = CXCursor_CallExpr;
615     break;
616 
617   case Stmt::LambdaExprClass:
618     K = CXCursor_LambdaExpr;
619     break;
620 
621   case Stmt::ObjCMessageExprClass: {
622     K = CXCursor_ObjCMessageExpr;
623     int SelectorIdIndex = -1;
624     // Check if cursor points to a selector id.
625     if (RegionOfInterest.isValid() &&
626         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
627       SmallVector<SourceLocation, 16> SelLocs;
628       cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
629       SmallVectorImpl<SourceLocation>::iterator I =
630           llvm::find(SelLocs, RegionOfInterest.getBegin());
631       if (I != SelLocs.end())
632         SelectorIdIndex = I - SelLocs.begin();
633     }
634     CXCursor C = {K, 0, {Parent, S, TU}};
635     return getSelectorIdentifierCursor(SelectorIdIndex, C);
636   }
637 
638   case Stmt::ConceptSpecializationExprClass:
639     K = CXCursor_ConceptSpecializationExpr;
640     break;
641 
642   case Stmt::RequiresExprClass:
643     K = CXCursor_RequiresExpr;
644     break;
645 
646   case Stmt::CXXParenListInitExprClass:
647     K = CXCursor_CXXParenListInitExpr;
648     break;
649 
650   case Stmt::MSDependentExistsStmtClass:
651     K = CXCursor_UnexposedStmt;
652     break;
653   case Stmt::OMPCanonicalLoopClass:
654     K = CXCursor_OMPCanonicalLoop;
655     break;
656   case Stmt::OMPMetaDirectiveClass:
657     K = CXCursor_OMPMetaDirective;
658     break;
659   case Stmt::OMPParallelDirectiveClass:
660     K = CXCursor_OMPParallelDirective;
661     break;
662   case Stmt::OMPSimdDirectiveClass:
663     K = CXCursor_OMPSimdDirective;
664     break;
665   case Stmt::OMPTileDirectiveClass:
666     K = CXCursor_OMPTileDirective;
667     break;
668   case Stmt::OMPUnrollDirectiveClass:
669     K = CXCursor_OMPUnrollDirective;
670     break;
671   case Stmt::OMPForDirectiveClass:
672     K = CXCursor_OMPForDirective;
673     break;
674   case Stmt::OMPForSimdDirectiveClass:
675     K = CXCursor_OMPForSimdDirective;
676     break;
677   case Stmt::OMPSectionsDirectiveClass:
678     K = CXCursor_OMPSectionsDirective;
679     break;
680   case Stmt::OMPSectionDirectiveClass:
681     K = CXCursor_OMPSectionDirective;
682     break;
683   case Stmt::OMPSingleDirectiveClass:
684     K = CXCursor_OMPSingleDirective;
685     break;
686   case Stmt::OMPMasterDirectiveClass:
687     K = CXCursor_OMPMasterDirective;
688     break;
689   case Stmt::OMPCriticalDirectiveClass:
690     K = CXCursor_OMPCriticalDirective;
691     break;
692   case Stmt::OMPParallelForDirectiveClass:
693     K = CXCursor_OMPParallelForDirective;
694     break;
695   case Stmt::OMPParallelForSimdDirectiveClass:
696     K = CXCursor_OMPParallelForSimdDirective;
697     break;
698   case Stmt::OMPParallelMasterDirectiveClass:
699     K = CXCursor_OMPParallelMasterDirective;
700     break;
701   case Stmt::OMPParallelMaskedDirectiveClass:
702     K = CXCursor_OMPParallelMaskedDirective;
703     break;
704   case Stmt::OMPParallelSectionsDirectiveClass:
705     K = CXCursor_OMPParallelSectionsDirective;
706     break;
707   case Stmt::OMPTaskDirectiveClass:
708     K = CXCursor_OMPTaskDirective;
709     break;
710   case Stmt::OMPTaskyieldDirectiveClass:
711     K = CXCursor_OMPTaskyieldDirective;
712     break;
713   case Stmt::OMPBarrierDirectiveClass:
714     K = CXCursor_OMPBarrierDirective;
715     break;
716   case Stmt::OMPTaskwaitDirectiveClass:
717     K = CXCursor_OMPTaskwaitDirective;
718     break;
719   case Stmt::OMPErrorDirectiveClass:
720     K = CXCursor_OMPErrorDirective;
721     break;
722   case Stmt::OMPTaskgroupDirectiveClass:
723     K = CXCursor_OMPTaskgroupDirective;
724     break;
725   case Stmt::OMPFlushDirectiveClass:
726     K = CXCursor_OMPFlushDirective;
727     break;
728   case Stmt::OMPDepobjDirectiveClass:
729     K = CXCursor_OMPDepobjDirective;
730     break;
731   case Stmt::OMPScanDirectiveClass:
732     K = CXCursor_OMPScanDirective;
733     break;
734   case Stmt::OMPOrderedDirectiveClass:
735     K = CXCursor_OMPOrderedDirective;
736     break;
737   case Stmt::OMPAtomicDirectiveClass:
738     K = CXCursor_OMPAtomicDirective;
739     break;
740   case Stmt::OMPTargetDirectiveClass:
741     K = CXCursor_OMPTargetDirective;
742     break;
743   case Stmt::OMPTargetDataDirectiveClass:
744     K = CXCursor_OMPTargetDataDirective;
745     break;
746   case Stmt::OMPTargetEnterDataDirectiveClass:
747     K = CXCursor_OMPTargetEnterDataDirective;
748     break;
749   case Stmt::OMPTargetExitDataDirectiveClass:
750     K = CXCursor_OMPTargetExitDataDirective;
751     break;
752   case Stmt::OMPTargetParallelDirectiveClass:
753     K = CXCursor_OMPTargetParallelDirective;
754     break;
755   case Stmt::OMPTargetParallelForDirectiveClass:
756     K = CXCursor_OMPTargetParallelForDirective;
757     break;
758   case Stmt::OMPTargetUpdateDirectiveClass:
759     K = CXCursor_OMPTargetUpdateDirective;
760     break;
761   case Stmt::OMPTeamsDirectiveClass:
762     K = CXCursor_OMPTeamsDirective;
763     break;
764   case Stmt::OMPCancellationPointDirectiveClass:
765     K = CXCursor_OMPCancellationPointDirective;
766     break;
767   case Stmt::OMPCancelDirectiveClass:
768     K = CXCursor_OMPCancelDirective;
769     break;
770   case Stmt::OMPTaskLoopDirectiveClass:
771     K = CXCursor_OMPTaskLoopDirective;
772     break;
773   case Stmt::OMPTaskLoopSimdDirectiveClass:
774     K = CXCursor_OMPTaskLoopSimdDirective;
775     break;
776   case Stmt::OMPMasterTaskLoopDirectiveClass:
777     K = CXCursor_OMPMasterTaskLoopDirective;
778     break;
779   case Stmt::OMPMaskedTaskLoopDirectiveClass:
780     K = CXCursor_OMPMaskedTaskLoopDirective;
781     break;
782   case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
783     K = CXCursor_OMPMasterTaskLoopSimdDirective;
784     break;
785   case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
786     K = CXCursor_OMPMaskedTaskLoopSimdDirective;
787     break;
788   case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
789     K = CXCursor_OMPParallelMasterTaskLoopDirective;
790     break;
791   case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
792     K = CXCursor_OMPParallelMaskedTaskLoopDirective;
793     break;
794   case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
795     K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
796     break;
797   case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
798     K = CXCursor_OMPParallelMaskedTaskLoopSimdDirective;
799     break;
800   case Stmt::OMPDistributeDirectiveClass:
801     K = CXCursor_OMPDistributeDirective;
802     break;
803   case Stmt::OMPDistributeParallelForDirectiveClass:
804     K = CXCursor_OMPDistributeParallelForDirective;
805     break;
806   case Stmt::OMPDistributeParallelForSimdDirectiveClass:
807     K = CXCursor_OMPDistributeParallelForSimdDirective;
808     break;
809   case Stmt::OMPDistributeSimdDirectiveClass:
810     K = CXCursor_OMPDistributeSimdDirective;
811     break;
812   case Stmt::OMPTargetParallelForSimdDirectiveClass:
813     K = CXCursor_OMPTargetParallelForSimdDirective;
814     break;
815   case Stmt::OMPTargetSimdDirectiveClass:
816     K = CXCursor_OMPTargetSimdDirective;
817     break;
818   case Stmt::OMPTeamsDistributeDirectiveClass:
819     K = CXCursor_OMPTeamsDistributeDirective;
820     break;
821   case Stmt::OMPTeamsDistributeSimdDirectiveClass:
822     K = CXCursor_OMPTeamsDistributeSimdDirective;
823     break;
824   case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
825     K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
826     break;
827   case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
828     K = CXCursor_OMPTeamsDistributeParallelForDirective;
829     break;
830   case Stmt::OMPTargetTeamsDirectiveClass:
831     K = CXCursor_OMPTargetTeamsDirective;
832     break;
833   case Stmt::OMPTargetTeamsDistributeDirectiveClass:
834     K = CXCursor_OMPTargetTeamsDistributeDirective;
835     break;
836   case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
837     K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
838     break;
839   case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
840     K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
841     break;
842   case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
843     K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
844     break;
845   case Stmt::OMPInteropDirectiveClass:
846     K = CXCursor_OMPInteropDirective;
847     break;
848   case Stmt::OMPDispatchDirectiveClass:
849     K = CXCursor_OMPDispatchDirective;
850     break;
851   case Stmt::OMPMaskedDirectiveClass:
852     K = CXCursor_OMPMaskedDirective;
853     break;
854   case Stmt::OMPGenericLoopDirectiveClass:
855     K = CXCursor_OMPGenericLoopDirective;
856     break;
857   case Stmt::OMPTeamsGenericLoopDirectiveClass:
858     K = CXCursor_OMPTeamsGenericLoopDirective;
859     break;
860   case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
861     K = CXCursor_OMPTargetTeamsGenericLoopDirective;
862     break;
863   case Stmt::OMPParallelGenericLoopDirectiveClass:
864     K = CXCursor_OMPParallelGenericLoopDirective;
865     break;
866   case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
867     K = CXCursor_OMPTargetParallelGenericLoopDirective;
868     break;
869   case Stmt::BuiltinBitCastExprClass:
870     K = CXCursor_BuiltinBitCastExpr;
871   }
872 
873   CXCursor C = {K, 0, {Parent, S, TU}};
874   return C;
875 }
876 
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl * Super,SourceLocation Loc,CXTranslationUnit TU)877 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
878                                                SourceLocation Loc,
879                                                CXTranslationUnit TU) {
880   assert(Super && TU && "Invalid arguments!");
881   void *RawLoc = Loc.getPtrEncoding();
882   CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
883   return C;
884 }
885 
886 std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCSuperClassRef(CXCursor C)887 cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
888   assert(C.kind == CXCursor_ObjCSuperClassRef);
889   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
890                         SourceLocation::getFromPtrEncoding(C.data[1]));
891 }
892 
MakeCursorObjCProtocolRef(const ObjCProtocolDecl * Proto,SourceLocation Loc,CXTranslationUnit TU)893 CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
894                                              SourceLocation Loc,
895                                              CXTranslationUnit TU) {
896   assert(Proto && TU && "Invalid arguments!");
897   void *RawLoc = Loc.getPtrEncoding();
898   CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
899   return C;
900 }
901 
902 std::pair<const ObjCProtocolDecl *, SourceLocation>
getCursorObjCProtocolRef(CXCursor C)903 cxcursor::getCursorObjCProtocolRef(CXCursor C) {
904   assert(C.kind == CXCursor_ObjCProtocolRef);
905   return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
906                         SourceLocation::getFromPtrEncoding(C.data[1]));
907 }
908 
MakeCursorObjCClassRef(const ObjCInterfaceDecl * Class,SourceLocation Loc,CXTranslationUnit TU)909 CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
910                                           SourceLocation Loc,
911                                           CXTranslationUnit TU) {
912   // 'Class' can be null for invalid code.
913   if (!Class)
914     return MakeCXCursorInvalid(CXCursor_InvalidCode);
915   assert(TU && "Invalid arguments!");
916   void *RawLoc = Loc.getPtrEncoding();
917   CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
918   return C;
919 }
920 
921 std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C)922 cxcursor::getCursorObjCClassRef(CXCursor C) {
923   assert(C.kind == CXCursor_ObjCClassRef);
924   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
925                         SourceLocation::getFromPtrEncoding(C.data[1]));
926 }
927 
MakeCursorTypeRef(const TypeDecl * Type,SourceLocation Loc,CXTranslationUnit TU)928 CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
929                                      CXTranslationUnit TU) {
930   assert(Type && TU && "Invalid arguments!");
931   void *RawLoc = Loc.getPtrEncoding();
932   CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
933   return C;
934 }
935 
936 std::pair<const TypeDecl *, SourceLocation>
getCursorTypeRef(CXCursor C)937 cxcursor::getCursorTypeRef(CXCursor C) {
938   assert(C.kind == CXCursor_TypeRef);
939   return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
940                         SourceLocation::getFromPtrEncoding(C.data[1]));
941 }
942 
MakeCursorTemplateRef(const TemplateDecl * Template,SourceLocation Loc,CXTranslationUnit TU)943 CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
944                                          SourceLocation Loc,
945                                          CXTranslationUnit TU) {
946   assert(Template && TU && "Invalid arguments!");
947   void *RawLoc = Loc.getPtrEncoding();
948   CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
949   return C;
950 }
951 
952 std::pair<const TemplateDecl *, SourceLocation>
getCursorTemplateRef(CXCursor C)953 cxcursor::getCursorTemplateRef(CXCursor C) {
954   assert(C.kind == CXCursor_TemplateRef);
955   return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
956                         SourceLocation::getFromPtrEncoding(C.data[1]));
957 }
958 
MakeCursorNamespaceRef(const NamedDecl * NS,SourceLocation Loc,CXTranslationUnit TU)959 CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
960                                           SourceLocation Loc,
961                                           CXTranslationUnit TU) {
962 
963   assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
964          "Invalid arguments!");
965   void *RawLoc = Loc.getPtrEncoding();
966   CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
967   return C;
968 }
969 
970 std::pair<const NamedDecl *, SourceLocation>
getCursorNamespaceRef(CXCursor C)971 cxcursor::getCursorNamespaceRef(CXCursor C) {
972   assert(C.kind == CXCursor_NamespaceRef);
973   return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
974                         SourceLocation::getFromPtrEncoding(C.data[1]));
975 }
976 
MakeCursorVariableRef(const VarDecl * Var,SourceLocation Loc,CXTranslationUnit TU)977 CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
978                                          CXTranslationUnit TU) {
979 
980   assert(Var && TU && "Invalid arguments!");
981   void *RawLoc = Loc.getPtrEncoding();
982   CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
983   return C;
984 }
985 
986 std::pair<const VarDecl *, SourceLocation>
getCursorVariableRef(CXCursor C)987 cxcursor::getCursorVariableRef(CXCursor C) {
988   assert(C.kind == CXCursor_VariableRef);
989   return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
990                         SourceLocation::getFromPtrEncoding(C.data[1]));
991 }
992 
MakeCursorMemberRef(const FieldDecl * Field,SourceLocation Loc,CXTranslationUnit TU)993 CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
994                                        SourceLocation Loc,
995                                        CXTranslationUnit TU) {
996 
997   assert(Field && TU && "Invalid arguments!");
998   void *RawLoc = Loc.getPtrEncoding();
999   CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
1000   return C;
1001 }
1002 
1003 std::pair<const FieldDecl *, SourceLocation>
getCursorMemberRef(CXCursor C)1004 cxcursor::getCursorMemberRef(CXCursor C) {
1005   assert(C.kind == CXCursor_MemberRef);
1006   return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
1007                         SourceLocation::getFromPtrEncoding(C.data[1]));
1008 }
1009 
MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier * B,CXTranslationUnit TU)1010 CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
1011                                               CXTranslationUnit TU) {
1012   CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
1013   return C;
1014 }
1015 
getCursorCXXBaseSpecifier(CXCursor C)1016 const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
1017   assert(C.kind == CXCursor_CXXBaseSpecifier);
1018   return static_cast<const CXXBaseSpecifier *>(C.data[0]);
1019 }
1020 
MakePreprocessingDirectiveCursor(SourceRange Range,CXTranslationUnit TU)1021 CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
1022                                                     CXTranslationUnit TU) {
1023   CXCursor C = {
1024       CXCursor_PreprocessingDirective,
1025       0,
1026       {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
1027   return C;
1028 }
1029 
getCursorPreprocessingDirective(CXCursor C)1030 SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
1031   assert(C.kind == CXCursor_PreprocessingDirective);
1032   SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
1033                     SourceLocation::getFromPtrEncoding(C.data[1]));
1034   ASTUnit *TU = getCursorASTUnit(C);
1035   return TU->mapRangeFromPreamble(Range);
1036 }
1037 
MakeMacroDefinitionCursor(const MacroDefinitionRecord * MI,CXTranslationUnit TU)1038 CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
1039                                              CXTranslationUnit TU) {
1040   CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
1041   return C;
1042 }
1043 
getCursorMacroDefinition(CXCursor C)1044 const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
1045   assert(C.kind == CXCursor_MacroDefinition);
1046   return static_cast<const MacroDefinitionRecord *>(C.data[0]);
1047 }
1048 
MakeMacroExpansionCursor(MacroExpansion * MI,CXTranslationUnit TU)1049 CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
1050                                             CXTranslationUnit TU) {
1051   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
1052   return C;
1053 }
1054 
MakeMacroExpansionCursor(MacroDefinitionRecord * MI,SourceLocation Loc,CXTranslationUnit TU)1055 CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
1056                                             SourceLocation Loc,
1057                                             CXTranslationUnit TU) {
1058   assert(Loc.isValid());
1059   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
1060   return C;
1061 }
1062 
getName() const1063 const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
1064   if (isPseudo())
1065     return getAsMacroDefinition()->getName();
1066   return getAsMacroExpansion()->getName();
1067 }
1068 const MacroDefinitionRecord *
getDefinition() const1069 cxcursor::MacroExpansionCursor::getDefinition() const {
1070   if (isPseudo())
1071     return getAsMacroDefinition();
1072   return getAsMacroExpansion()->getDefinition();
1073 }
getSourceRange() const1074 SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
1075   if (isPseudo())
1076     return getPseudoLoc();
1077   return getAsMacroExpansion()->getSourceRange();
1078 }
1079 
MakeInclusionDirectiveCursor(InclusionDirective * ID,CXTranslationUnit TU)1080 CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
1081                                                 CXTranslationUnit TU) {
1082   CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
1083   return C;
1084 }
1085 
getCursorInclusionDirective(CXCursor C)1086 const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
1087   assert(C.kind == CXCursor_InclusionDirective);
1088   return static_cast<const InclusionDirective *>(C.data[0]);
1089 }
1090 
MakeCursorLabelRef(LabelStmt * Label,SourceLocation Loc,CXTranslationUnit TU)1091 CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
1092                                       CXTranslationUnit TU) {
1093 
1094   assert(Label && TU && "Invalid arguments!");
1095   void *RawLoc = Loc.getPtrEncoding();
1096   CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
1097   return C;
1098 }
1099 
1100 std::pair<const LabelStmt *, SourceLocation>
getCursorLabelRef(CXCursor C)1101 cxcursor::getCursorLabelRef(CXCursor C) {
1102   assert(C.kind == CXCursor_LabelRef);
1103   return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
1104                         SourceLocation::getFromPtrEncoding(C.data[1]));
1105 }
1106 
MakeCursorOverloadedDeclRef(const OverloadExpr * E,CXTranslationUnit TU)1107 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
1108                                                CXTranslationUnit TU) {
1109   assert(E && TU && "Invalid arguments!");
1110   OverloadedDeclRefStorage Storage(E);
1111   void *RawLoc = E->getNameLoc().getPtrEncoding();
1112   CXCursor C = {
1113       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1114   return C;
1115 }
1116 
MakeCursorOverloadedDeclRef(const Decl * D,SourceLocation Loc,CXTranslationUnit TU)1117 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
1118                                                SourceLocation Loc,
1119                                                CXTranslationUnit TU) {
1120   assert(D && TU && "Invalid arguments!");
1121   void *RawLoc = Loc.getPtrEncoding();
1122   OverloadedDeclRefStorage Storage(D);
1123   CXCursor C = {
1124       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1125   return C;
1126 }
1127 
MakeCursorOverloadedDeclRef(TemplateName Name,SourceLocation Loc,CXTranslationUnit TU)1128 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
1129                                                SourceLocation Loc,
1130                                                CXTranslationUnit TU) {
1131   assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
1132   void *RawLoc = Loc.getPtrEncoding();
1133   OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
1134   CXCursor C = {
1135       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1136   return C;
1137 }
1138 
1139 std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
getCursorOverloadedDeclRef(CXCursor C)1140 cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
1141   assert(C.kind == CXCursor_OverloadedDeclRef);
1142   return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
1143                             const_cast<void *>(C.data[0])),
1144                         SourceLocation::getFromPtrEncoding(C.data[1]));
1145 }
1146 
getCursorDecl(CXCursor Cursor)1147 const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
1148   return static_cast<const Decl *>(Cursor.data[0]);
1149 }
1150 
getCursorExpr(CXCursor Cursor)1151 const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
1152   return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
1153 }
1154 
getCursorStmt(CXCursor Cursor)1155 const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
1156   if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
1157       Cursor.kind == CXCursor_ObjCProtocolRef ||
1158       Cursor.kind == CXCursor_ObjCClassRef)
1159     return nullptr;
1160 
1161   return static_cast<const Stmt *>(Cursor.data[1]);
1162 }
1163 
getCursorAttr(CXCursor Cursor)1164 const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
1165   return static_cast<const Attr *>(Cursor.data[1]);
1166 }
1167 
getCursorContext(CXCursor Cursor)1168 ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
1169   return getCursorASTUnit(Cursor)->getASTContext();
1170 }
1171 
getCursorASTUnit(CXCursor Cursor)1172 ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
1173   CXTranslationUnit TU = getCursorTU(Cursor);
1174   if (!TU)
1175     return nullptr;
1176   return cxtu::getASTUnit(TU);
1177 }
1178 
getCursorTU(CXCursor Cursor)1179 CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
1180   return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
1181 }
1182 
getOverriddenCursors(CXCursor cursor,SmallVectorImpl<CXCursor> & overridden)1183 void cxcursor::getOverriddenCursors(CXCursor cursor,
1184                                     SmallVectorImpl<CXCursor> &overridden) {
1185   assert(clang_isDeclaration(cursor.kind));
1186   const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
1187   if (!D)
1188     return;
1189 
1190   CXTranslationUnit TU = getCursorTU(cursor);
1191   SmallVector<const NamedDecl *, 8> OverDecls;
1192   D->getASTContext().getOverriddenMethods(D, OverDecls);
1193 
1194   for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1195                                                     E = OverDecls.end();
1196        I != E; ++I) {
1197     overridden.push_back(MakeCXCursor(*I, TU));
1198   }
1199 }
1200 
1201 std::pair<int, SourceLocation>
getSelectorIdentifierIndexAndLoc(CXCursor cursor)1202 cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
1203   if (cursor.kind == CXCursor_ObjCMessageExpr) {
1204     if (cursor.xdata != -1)
1205       return std::make_pair(cursor.xdata,
1206                             cast<ObjCMessageExpr>(getCursorExpr(cursor))
1207                                 ->getSelectorLoc(cursor.xdata));
1208   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1209              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1210     if (cursor.xdata != -1)
1211       return std::make_pair(cursor.xdata,
1212                             cast<ObjCMethodDecl>(getCursorDecl(cursor))
1213                                 ->getSelectorLoc(cursor.xdata));
1214   }
1215 
1216   return std::make_pair(-1, SourceLocation());
1217 }
1218 
getSelectorIdentifierCursor(int SelIdx,CXCursor cursor)1219 CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
1220   CXCursor newCursor = cursor;
1221 
1222   if (cursor.kind == CXCursor_ObjCMessageExpr) {
1223     if (SelIdx == -1 ||
1224         unsigned(SelIdx) >=
1225             cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
1226       newCursor.xdata = -1;
1227     else
1228       newCursor.xdata = SelIdx;
1229   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1230              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1231     if (SelIdx == -1 ||
1232         unsigned(SelIdx) >=
1233             cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
1234       newCursor.xdata = -1;
1235     else
1236       newCursor.xdata = SelIdx;
1237   }
1238 
1239   return newCursor;
1240 }
1241 
getTypeRefCursor(CXCursor cursor)1242 CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1243   if (cursor.kind != CXCursor_CallExpr)
1244     return cursor;
1245 
1246   if (cursor.xdata == 0)
1247     return cursor;
1248 
1249   const Expr *E = getCursorExpr(cursor);
1250   TypeSourceInfo *Type = nullptr;
1251   if (const CXXUnresolvedConstructExpr *UnCtor =
1252           dyn_cast<CXXUnresolvedConstructExpr>(E)) {
1253     Type = UnCtor->getTypeSourceInfo();
1254   } else if (const CXXTemporaryObjectExpr *Tmp =
1255                  dyn_cast<CXXTemporaryObjectExpr>(E)) {
1256     Type = Tmp->getTypeSourceInfo();
1257   }
1258 
1259   if (!Type)
1260     return cursor;
1261 
1262   CXTranslationUnit TU = getCursorTU(cursor);
1263   QualType Ty = Type->getType();
1264   TypeLoc TL = Type->getTypeLoc();
1265   SourceLocation Loc = TL.getBeginLoc();
1266 
1267   if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
1268     Ty = ElabT->getNamedType();
1269     ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
1270     Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
1271   }
1272 
1273   if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
1274     return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
1275   if (const TagType *Tag = Ty->getAs<TagType>())
1276     return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
1277   if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
1278     return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
1279 
1280   return cursor;
1281 }
1282 
operator ==(CXCursor X,CXCursor Y)1283 bool cxcursor::operator==(CXCursor X, CXCursor Y) {
1284   return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
1285          X.data[2] == Y.data[2];
1286 }
1287 
1288 // FIXME: Remove once we can model DeclGroups and their appropriate ranges
1289 // properly in the ASTs.
isFirstInDeclGroup(CXCursor C)1290 bool cxcursor::isFirstInDeclGroup(CXCursor C) {
1291   assert(clang_isDeclaration(C.kind));
1292   return ((uintptr_t)(C.data[1])) != 0;
1293 }
1294 
1295 //===----------------------------------------------------------------------===//
1296 // libclang CXCursor APIs
1297 //===----------------------------------------------------------------------===//
1298 
clang_Cursor_isNull(CXCursor cursor)1299 int clang_Cursor_isNull(CXCursor cursor) {
1300   return clang_equalCursors(cursor, clang_getNullCursor());
1301 }
1302 
clang_Cursor_getTranslationUnit(CXCursor cursor)1303 CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
1304   return getCursorTU(cursor);
1305 }
1306 
clang_Cursor_getNumArguments(CXCursor C)1307 int clang_Cursor_getNumArguments(CXCursor C) {
1308   if (clang_isDeclaration(C.kind)) {
1309     const Decl *D = cxcursor::getCursorDecl(C);
1310     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1311       return MD->param_size();
1312     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1313       return FD->param_size();
1314   }
1315 
1316   if (clang_isExpression(C.kind)) {
1317     const Expr *E = cxcursor::getCursorExpr(C);
1318     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1319       return CE->getNumArgs();
1320     }
1321     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1322       return CE->getNumArgs();
1323     }
1324   }
1325 
1326   return -1;
1327 }
1328 
clang_Cursor_getArgument(CXCursor C,unsigned i)1329 CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1330   if (clang_isDeclaration(C.kind)) {
1331     const Decl *D = cxcursor::getCursorDecl(C);
1332     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
1333       if (i < MD->param_size())
1334         return cxcursor::MakeCXCursor(MD->parameters()[i],
1335                                       cxcursor::getCursorTU(C));
1336     } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1337       if (i < FD->param_size())
1338         return cxcursor::MakeCXCursor(FD->parameters()[i],
1339                                       cxcursor::getCursorTU(C));
1340     }
1341   }
1342 
1343   if (clang_isExpression(C.kind)) {
1344     const Expr *E = cxcursor::getCursorExpr(C);
1345     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1346       if (i < CE->getNumArgs()) {
1347         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1348                                       cxcursor::getCursorTU(C));
1349       }
1350     }
1351     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1352       if (i < CE->getNumArgs()) {
1353         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1354                                       cxcursor::getCursorTU(C));
1355       }
1356     }
1357   }
1358 
1359   return clang_getNullCursor();
1360 }
1361 
clang_Cursor_getNumTemplateArguments(CXCursor C)1362 int clang_Cursor_getNumTemplateArguments(CXCursor C) {
1363   CXCursorKind kind = clang_getCursorKind(C);
1364   if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
1365       kind != CXCursor_ClassDecl &&
1366       kind != CXCursor_ClassTemplatePartialSpecialization) {
1367     return -1;
1368   }
1369 
1370   if (const auto *FD =
1371           llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
1372     const FunctionTemplateSpecializationInfo *SpecInfo =
1373         FD->getTemplateSpecializationInfo();
1374     if (!SpecInfo) {
1375       return -1;
1376     }
1377     return SpecInfo->TemplateArguments->size();
1378   }
1379 
1380   if (const auto *SD =
1381           llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
1382               getCursorDecl(C))) {
1383     return SD->getTemplateArgs().size();
1384   }
1385 
1386   return -1;
1387 }
1388 
1389 enum CXGetTemplateArgumentStatus {
1390   /** The operation completed successfully */
1391   CXGetTemplateArgumentStatus_Success = 0,
1392 
1393   /** The specified cursor did not represent a FunctionDecl or
1394       ClassTemplateSpecializationDecl.                         */
1395   CXGetTemplateArgumentStatus_CursorNotCompatibleDecl = -1,
1396 
1397   /** The specified cursor was not castable to a FunctionDecl or
1398       ClassTemplateSpecializationDecl.                         */
1399   CXGetTemplateArgumentStatus_BadDeclCast = -2,
1400 
1401   /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
1402   CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
1403 
1404   /** An invalid (OOB) argument index was specified */
1405   CXGetTemplateArgumentStatus_InvalidIndex = -4
1406 };
1407 
clang_Cursor_getTemplateArgument(CXCursor C,unsigned I,TemplateArgument * TA)1408 static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1409                                             TemplateArgument *TA) {
1410   CXCursorKind kind = clang_getCursorKind(C);
1411   if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
1412       kind != CXCursor_ClassDecl &&
1413       kind != CXCursor_ClassTemplatePartialSpecialization) {
1414     return -1;
1415   }
1416 
1417   if (const auto *FD =
1418           llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
1419 
1420     const FunctionTemplateSpecializationInfo *SpecInfo =
1421         FD->getTemplateSpecializationInfo();
1422     if (!SpecInfo) {
1423       return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
1424     }
1425 
1426     if (I >= SpecInfo->TemplateArguments->size()) {
1427       return CXGetTemplateArgumentStatus_InvalidIndex;
1428     }
1429 
1430     *TA = SpecInfo->TemplateArguments->get(I);
1431     return 0;
1432   }
1433 
1434   if (const auto *SD =
1435           llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
1436               getCursorDecl(C))) {
1437     if (I >= SD->getTemplateArgs().size()) {
1438       return CXGetTemplateArgumentStatus_InvalidIndex;
1439     }
1440 
1441     *TA = SD->getTemplateArgs()[I];
1442     return 0;
1443   }
1444 
1445   return CXGetTemplateArgumentStatus_BadDeclCast;
1446 }
1447 
clang_Cursor_getTemplateArgumentKind(CXCursor C,unsigned I)1448 enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
1449                                                                  unsigned I) {
1450   TemplateArgument TA;
1451   if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
1452     return CXTemplateArgumentKind_Invalid;
1453   }
1454 
1455   switch (TA.getKind()) {
1456   case TemplateArgument::Null:
1457     return CXTemplateArgumentKind_Null;
1458   case TemplateArgument::Type:
1459     return CXTemplateArgumentKind_Type;
1460   case TemplateArgument::Declaration:
1461     return CXTemplateArgumentKind_Declaration;
1462   case TemplateArgument::NullPtr:
1463     return CXTemplateArgumentKind_NullPtr;
1464   case TemplateArgument::Integral:
1465     return CXTemplateArgumentKind_Integral;
1466   case TemplateArgument::Template:
1467     return CXTemplateArgumentKind_Template;
1468   case TemplateArgument::TemplateExpansion:
1469     return CXTemplateArgumentKind_TemplateExpansion;
1470   case TemplateArgument::Expression:
1471     return CXTemplateArgumentKind_Expression;
1472   case TemplateArgument::Pack:
1473     return CXTemplateArgumentKind_Pack;
1474   }
1475 
1476   return CXTemplateArgumentKind_Invalid;
1477 }
1478 
clang_Cursor_getTemplateArgumentType(CXCursor C,unsigned I)1479 CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
1480   TemplateArgument TA;
1481   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1482       CXGetTemplateArgumentStatus_Success) {
1483     return cxtype::MakeCXType(QualType(), getCursorTU(C));
1484   }
1485 
1486   if (TA.getKind() != TemplateArgument::Type) {
1487     return cxtype::MakeCXType(QualType(), getCursorTU(C));
1488   }
1489 
1490   return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
1491 }
1492 
clang_Cursor_getTemplateArgumentValue(CXCursor C,unsigned I)1493 long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
1494   TemplateArgument TA;
1495   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1496       CXGetTemplateArgumentStatus_Success) {
1497     assert(0 && "Unable to retrieve TemplateArgument");
1498     return 0;
1499   }
1500 
1501   if (TA.getKind() != TemplateArgument::Integral) {
1502     assert(0 && "Passed template argument is not Integral");
1503     return 0;
1504   }
1505 
1506   return TA.getAsIntegral().getSExtValue();
1507 }
1508 
clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,unsigned I)1509 unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
1510                                                                  unsigned I) {
1511   TemplateArgument TA;
1512   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1513       CXGetTemplateArgumentStatus_Success) {
1514     assert(0 && "Unable to retrieve TemplateArgument");
1515     return 0;
1516   }
1517 
1518   if (TA.getKind() != TemplateArgument::Integral) {
1519     assert(0 && "Passed template argument is not Integral");
1520     return 0;
1521   }
1522 
1523   return TA.getAsIntegral().getZExtValue();
1524 }
1525 
1526 //===----------------------------------------------------------------------===//
1527 // CXCursorSet.
1528 //===----------------------------------------------------------------------===//
1529 
1530 typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1531 
packCXCursorSet(CXCursorSet_Impl * setImpl)1532 static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1533   return (CXCursorSet)setImpl;
1534 }
unpackCXCursorSet(CXCursorSet set)1535 static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1536   return (CXCursorSet_Impl *)set;
1537 }
1538 namespace llvm {
1539 template <> struct DenseMapInfo<CXCursor> {
1540 public:
getEmptyKeyllvm::DenseMapInfo1541   static inline CXCursor getEmptyKey() {
1542     return MakeCXCursorInvalid(CXCursor_InvalidFile);
1543   }
getTombstoneKeyllvm::DenseMapInfo1544   static inline CXCursor getTombstoneKey() {
1545     return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1546   }
getHashValuellvm::DenseMapInfo1547   static inline unsigned getHashValue(const CXCursor &cursor) {
1548     return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1549         getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1550   }
isEqualllvm::DenseMapInfo1551   static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1552     return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
1553   }
1554 };
1555 } // namespace llvm
1556 
clang_createCXCursorSet()1557 CXCursorSet clang_createCXCursorSet() {
1558   return packCXCursorSet(new CXCursorSet_Impl());
1559 }
1560 
clang_disposeCXCursorSet(CXCursorSet set)1561 void clang_disposeCXCursorSet(CXCursorSet set) {
1562   delete unpackCXCursorSet(set);
1563 }
1564 
clang_CXCursorSet_contains(CXCursorSet set,CXCursor cursor)1565 unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1566   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1567   if (!setImpl)
1568     return 0;
1569   return setImpl->find(cursor) != setImpl->end();
1570 }
1571 
clang_CXCursorSet_insert(CXCursorSet set,CXCursor cursor)1572 unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1573   // Do not insert invalid cursors into the set.
1574   if (cursor.kind >= CXCursor_FirstInvalid &&
1575       cursor.kind <= CXCursor_LastInvalid)
1576     return 1;
1577 
1578   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1579   if (!setImpl)
1580     return 1;
1581   unsigned &entry = (*setImpl)[cursor];
1582   unsigned flag = entry == 0 ? 1 : 0;
1583   entry = 1;
1584   return flag;
1585 }
1586 
clang_getCursorCompletionString(CXCursor cursor)1587 CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1588   enum CXCursorKind kind = clang_getCursorKind(cursor);
1589   if (clang_isDeclaration(kind)) {
1590     const Decl *decl = getCursorDecl(cursor);
1591     if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
1592       ASTUnit *unit = getCursorASTUnit(cursor);
1593       CodeCompletionResult Result(namedDecl, CCP_Declaration);
1594       CodeCompletionString *String = Result.CreateCodeCompletionString(
1595           unit->getASTContext(), unit->getPreprocessor(),
1596           CodeCompletionContext::CCC_Other,
1597           unit->getCodeCompletionTUInfo().getAllocator(),
1598           unit->getCodeCompletionTUInfo(), true);
1599       return String;
1600     }
1601   } else if (kind == CXCursor_MacroDefinition) {
1602     const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
1603     const IdentifierInfo *Macro = definition->getName();
1604     ASTUnit *unit = getCursorASTUnit(cursor);
1605     CodeCompletionResult Result(
1606         Macro,
1607         unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
1608     CodeCompletionString *String = Result.CreateCodeCompletionString(
1609         unit->getASTContext(), unit->getPreprocessor(),
1610         CodeCompletionContext::CCC_Other,
1611         unit->getCodeCompletionTUInfo().getAllocator(),
1612         unit->getCodeCompletionTUInfo(), false);
1613     return String;
1614   }
1615   return nullptr;
1616 }
1617 
1618 namespace {
1619 struct OverridenCursorsPool {
1620   typedef SmallVector<CXCursor, 2> CursorVec;
1621   std::vector<CursorVec *> AllCursors;
1622   std::vector<CursorVec *> AvailableCursors;
1623 
~OverridenCursorsPool__anona9a9d7a50111::OverridenCursorsPool1624   ~OverridenCursorsPool() {
1625     for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1626                                             E = AllCursors.end();
1627          I != E; ++I) {
1628       delete *I;
1629     }
1630   }
1631 };
1632 } // namespace
1633 
createOverridenCXCursorsPool()1634 void *cxcursor::createOverridenCXCursorsPool() {
1635   return new OverridenCursorsPool();
1636 }
1637 
disposeOverridenCXCursorsPool(void * pool)1638 void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1639   delete static_cast<OverridenCursorsPool *>(pool);
1640 }
1641 
clang_getOverriddenCursors(CXCursor cursor,CXCursor ** overridden,unsigned * num_overridden)1642 void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
1643                                 unsigned *num_overridden) {
1644   if (overridden)
1645     *overridden = nullptr;
1646   if (num_overridden)
1647     *num_overridden = 0;
1648 
1649   CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1650 
1651   if (!overridden || !num_overridden || !TU)
1652     return;
1653 
1654   if (!clang_isDeclaration(cursor.kind))
1655     return;
1656 
1657   OverridenCursorsPool &pool =
1658       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1659 
1660   OverridenCursorsPool::CursorVec *Vec = nullptr;
1661 
1662   if (!pool.AvailableCursors.empty()) {
1663     Vec = pool.AvailableCursors.back();
1664     pool.AvailableCursors.pop_back();
1665   } else {
1666     Vec = new OverridenCursorsPool::CursorVec();
1667     pool.AllCursors.push_back(Vec);
1668   }
1669 
1670   // Clear out the vector, but don't free the memory contents.  This
1671   // reduces malloc() traffic.
1672   Vec->clear();
1673 
1674   // Use the first entry to contain a back reference to the vector.
1675   // This is a complete hack.
1676   CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1677   backRefCursor.data[0] = Vec;
1678   assert(cxcursor::getCursorTU(backRefCursor) == TU);
1679   Vec->push_back(backRefCursor);
1680 
1681   // Get the overridden cursors.
1682   cxcursor::getOverriddenCursors(cursor, *Vec);
1683 
1684   // Did we get any overridden cursors?  If not, return Vec to the pool
1685   // of available cursor vectors.
1686   if (Vec->size() == 1) {
1687     pool.AvailableCursors.push_back(Vec);
1688     return;
1689   }
1690 
1691   // Now tell the caller about the overridden cursors.
1692   assert(Vec->size() > 1);
1693   *overridden = &((*Vec)[1]);
1694   *num_overridden = Vec->size() - 1;
1695 }
1696 
clang_disposeOverriddenCursors(CXCursor * overridden)1697 void clang_disposeOverriddenCursors(CXCursor *overridden) {
1698   if (!overridden)
1699     return;
1700 
1701   // Use pointer arithmetic to get back the first faux entry
1702   // which has a back-reference to the TU and the vector.
1703   --overridden;
1704   OverridenCursorsPool::CursorVec *Vec =
1705       static_cast<OverridenCursorsPool::CursorVec *>(
1706           const_cast<void *>(overridden->data[0]));
1707   CXTranslationUnit TU = getCursorTU(*overridden);
1708 
1709   assert(Vec && TU);
1710 
1711   OverridenCursorsPool &pool =
1712       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1713 
1714   pool.AvailableCursors.push_back(Vec);
1715 }
1716 
clang_Cursor_isDynamicCall(CXCursor C)1717 int clang_Cursor_isDynamicCall(CXCursor C) {
1718   const Expr *E = nullptr;
1719   if (clang_isExpression(C.kind))
1720     E = getCursorExpr(C);
1721   if (!E)
1722     return 0;
1723 
1724   if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
1725     if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
1726       return false;
1727     if (auto *RecE = dyn_cast<ObjCMessageExpr>(
1728             MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
1729       if (RecE->getMethodFamily() == OMF_alloc)
1730         return false;
1731     }
1732     return true;
1733   }
1734 
1735   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1736     return !PropRefE->isSuperReceiver();
1737   }
1738 
1739   const MemberExpr *ME = nullptr;
1740   if (isa<MemberExpr>(E))
1741     ME = cast<MemberExpr>(E);
1742   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1743     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1744 
1745   if (ME) {
1746     if (const CXXMethodDecl *MD =
1747             dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1748       return MD->isVirtual() &&
1749              ME->performsVirtualDispatch(
1750                  cxcursor::getCursorContext(C).getLangOpts());
1751   }
1752 
1753   return 0;
1754 }
1755 
clang_Cursor_getReceiverType(CXCursor C)1756 CXType clang_Cursor_getReceiverType(CXCursor C) {
1757   CXTranslationUnit TU = cxcursor::getCursorTU(C);
1758   const Expr *E = nullptr;
1759   if (clang_isExpression(C.kind))
1760     E = getCursorExpr(C);
1761 
1762   if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1763     return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1764 
1765   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1766     return cxtype::MakeCXType(
1767         PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
1768   }
1769 
1770   const MemberExpr *ME = nullptr;
1771   if (isa<MemberExpr>(E))
1772     ME = cast<MemberExpr>(E);
1773   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1774     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1775 
1776   if (ME) {
1777     if (isa_and_nonnull<CXXMethodDecl>(ME->getMemberDecl())) {
1778       auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
1779       return cxtype::MakeCXType(receiverTy, TU);
1780     }
1781   }
1782 
1783   return cxtype::MakeCXType(QualType(), TU);
1784 }
1785