106f32e7eSjoerg //===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file defines routines for manipulating CXCursors. It should be the
1006f32e7eSjoerg // only file that has internal knowledge of the encoding of the data in
1106f32e7eSjoerg // CXCursor.
1206f32e7eSjoerg //
1306f32e7eSjoerg //===----------------------------------------------------------------------===//
1406f32e7eSjoerg 
1506f32e7eSjoerg #include "CXCursor.h"
1606f32e7eSjoerg #include "CXString.h"
17*13fbcb42Sjoerg #include "CXTranslationUnit.h"
1806f32e7eSjoerg #include "CXType.h"
1906f32e7eSjoerg #include "clang-c/Index.h"
2006f32e7eSjoerg #include "clang/AST/Attr.h"
2106f32e7eSjoerg #include "clang/AST/Decl.h"
2206f32e7eSjoerg #include "clang/AST/DeclCXX.h"
2306f32e7eSjoerg #include "clang/AST/DeclObjC.h"
2406f32e7eSjoerg #include "clang/AST/DeclTemplate.h"
2506f32e7eSjoerg #include "clang/AST/Expr.h"
2606f32e7eSjoerg #include "clang/AST/ExprCXX.h"
2706f32e7eSjoerg #include "clang/AST/ExprObjC.h"
2806f32e7eSjoerg #include "clang/Frontend/ASTUnit.h"
2906f32e7eSjoerg #include "llvm/Support/ErrorHandling.h"
3006f32e7eSjoerg 
3106f32e7eSjoerg using namespace clang;
3206f32e7eSjoerg using namespace cxcursor;
3306f32e7eSjoerg 
MakeCXCursorInvalid(CXCursorKind K,CXTranslationUnit TU)3406f32e7eSjoerg CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
3506f32e7eSjoerg   assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
3606f32e7eSjoerg   CXCursor C = {K, 0, {nullptr, nullptr, TU}};
3706f32e7eSjoerg   return C;
3806f32e7eSjoerg }
3906f32e7eSjoerg 
GetCursorKind(const Attr * A)4006f32e7eSjoerg static CXCursorKind GetCursorKind(const Attr *A) {
4106f32e7eSjoerg   assert(A && "Invalid arguments!");
4206f32e7eSjoerg   switch (A->getKind()) {
43*13fbcb42Sjoerg   default:
44*13fbcb42Sjoerg     break;
45*13fbcb42Sjoerg   case attr::IBAction:
46*13fbcb42Sjoerg     return CXCursor_IBActionAttr;
47*13fbcb42Sjoerg   case attr::IBOutlet:
48*13fbcb42Sjoerg     return CXCursor_IBOutletAttr;
49*13fbcb42Sjoerg   case attr::IBOutletCollection:
50*13fbcb42Sjoerg     return CXCursor_IBOutletCollectionAttr;
51*13fbcb42Sjoerg   case attr::Final:
52*13fbcb42Sjoerg     return CXCursor_CXXFinalAttr;
53*13fbcb42Sjoerg   case attr::Override:
54*13fbcb42Sjoerg     return CXCursor_CXXOverrideAttr;
55*13fbcb42Sjoerg   case attr::Annotate:
56*13fbcb42Sjoerg     return CXCursor_AnnotateAttr;
57*13fbcb42Sjoerg   case attr::AsmLabel:
58*13fbcb42Sjoerg     return CXCursor_AsmLabelAttr;
59*13fbcb42Sjoerg   case attr::Packed:
60*13fbcb42Sjoerg     return CXCursor_PackedAttr;
61*13fbcb42Sjoerg   case attr::Pure:
62*13fbcb42Sjoerg     return CXCursor_PureAttr;
63*13fbcb42Sjoerg   case attr::Const:
64*13fbcb42Sjoerg     return CXCursor_ConstAttr;
65*13fbcb42Sjoerg   case attr::NoDuplicate:
66*13fbcb42Sjoerg     return CXCursor_NoDuplicateAttr;
67*13fbcb42Sjoerg   case attr::CUDAConstant:
68*13fbcb42Sjoerg     return CXCursor_CUDAConstantAttr;
69*13fbcb42Sjoerg   case attr::CUDADevice:
70*13fbcb42Sjoerg     return CXCursor_CUDADeviceAttr;
71*13fbcb42Sjoerg   case attr::CUDAGlobal:
72*13fbcb42Sjoerg     return CXCursor_CUDAGlobalAttr;
73*13fbcb42Sjoerg   case attr::CUDAHost:
74*13fbcb42Sjoerg     return CXCursor_CUDAHostAttr;
75*13fbcb42Sjoerg   case attr::CUDAShared:
76*13fbcb42Sjoerg     return CXCursor_CUDASharedAttr;
77*13fbcb42Sjoerg   case attr::Visibility:
78*13fbcb42Sjoerg     return CXCursor_VisibilityAttr;
79*13fbcb42Sjoerg   case attr::DLLExport:
80*13fbcb42Sjoerg     return CXCursor_DLLExport;
81*13fbcb42Sjoerg   case attr::DLLImport:
82*13fbcb42Sjoerg     return CXCursor_DLLImport;
83*13fbcb42Sjoerg   case attr::NSReturnsRetained:
84*13fbcb42Sjoerg     return CXCursor_NSReturnsRetained;
85*13fbcb42Sjoerg   case attr::NSReturnsNotRetained:
86*13fbcb42Sjoerg     return CXCursor_NSReturnsNotRetained;
87*13fbcb42Sjoerg   case attr::NSReturnsAutoreleased:
88*13fbcb42Sjoerg     return CXCursor_NSReturnsAutoreleased;
89*13fbcb42Sjoerg   case attr::NSConsumesSelf:
90*13fbcb42Sjoerg     return CXCursor_NSConsumesSelf;
91*13fbcb42Sjoerg   case attr::NSConsumed:
92*13fbcb42Sjoerg     return CXCursor_NSConsumed;
93*13fbcb42Sjoerg   case attr::ObjCException:
94*13fbcb42Sjoerg     return CXCursor_ObjCException;
95*13fbcb42Sjoerg   case attr::ObjCNSObject:
96*13fbcb42Sjoerg     return CXCursor_ObjCNSObject;
97*13fbcb42Sjoerg   case attr::ObjCIndependentClass:
98*13fbcb42Sjoerg     return CXCursor_ObjCIndependentClass;
99*13fbcb42Sjoerg   case attr::ObjCPreciseLifetime:
100*13fbcb42Sjoerg     return CXCursor_ObjCPreciseLifetime;
101*13fbcb42Sjoerg   case attr::ObjCReturnsInnerPointer:
102*13fbcb42Sjoerg     return CXCursor_ObjCReturnsInnerPointer;
103*13fbcb42Sjoerg   case attr::ObjCRequiresSuper:
104*13fbcb42Sjoerg     return CXCursor_ObjCRequiresSuper;
105*13fbcb42Sjoerg   case attr::ObjCRootClass:
106*13fbcb42Sjoerg     return CXCursor_ObjCRootClass;
107*13fbcb42Sjoerg   case attr::ObjCSubclassingRestricted:
108*13fbcb42Sjoerg     return CXCursor_ObjCSubclassingRestricted;
109*13fbcb42Sjoerg   case attr::ObjCExplicitProtocolImpl:
110*13fbcb42Sjoerg     return CXCursor_ObjCExplicitProtocolImpl;
111*13fbcb42Sjoerg   case attr::ObjCDesignatedInitializer:
112*13fbcb42Sjoerg     return CXCursor_ObjCDesignatedInitializer;
113*13fbcb42Sjoerg   case attr::ObjCRuntimeVisible:
114*13fbcb42Sjoerg     return CXCursor_ObjCRuntimeVisible;
115*13fbcb42Sjoerg   case attr::ObjCBoxable:
116*13fbcb42Sjoerg     return CXCursor_ObjCBoxable;
117*13fbcb42Sjoerg   case attr::FlagEnum:
118*13fbcb42Sjoerg     return CXCursor_FlagEnum;
119*13fbcb42Sjoerg   case attr::Convergent:
120*13fbcb42Sjoerg     return CXCursor_ConvergentAttr;
121*13fbcb42Sjoerg   case attr::WarnUnused:
122*13fbcb42Sjoerg     return CXCursor_WarnUnusedAttr;
123*13fbcb42Sjoerg   case attr::WarnUnusedResult:
124*13fbcb42Sjoerg     return CXCursor_WarnUnusedResultAttr;
125*13fbcb42Sjoerg   case attr::Aligned:
126*13fbcb42Sjoerg     return CXCursor_AlignedAttr;
12706f32e7eSjoerg   }
12806f32e7eSjoerg 
12906f32e7eSjoerg   return CXCursor_UnexposedAttr;
13006f32e7eSjoerg }
13106f32e7eSjoerg 
MakeCXCursor(const Attr * A,const Decl * Parent,CXTranslationUnit TU)13206f32e7eSjoerg CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
13306f32e7eSjoerg                                 CXTranslationUnit TU) {
13406f32e7eSjoerg   assert(A && Parent && TU && "Invalid arguments!");
13506f32e7eSjoerg   CXCursor C = {GetCursorKind(A), 0, {Parent, A, TU}};
13606f32e7eSjoerg   return C;
13706f32e7eSjoerg }
13806f32e7eSjoerg 
MakeCXCursor(const Decl * D,CXTranslationUnit TU,SourceRange RegionOfInterest,bool FirstInDeclGroup)13906f32e7eSjoerg CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
14006f32e7eSjoerg                                 SourceRange RegionOfInterest,
14106f32e7eSjoerg                                 bool FirstInDeclGroup) {
14206f32e7eSjoerg   assert(D && TU && "Invalid arguments!");
14306f32e7eSjoerg 
14406f32e7eSjoerg   CXCursorKind K = getCursorKindForDecl(D);
14506f32e7eSjoerg 
14606f32e7eSjoerg   if (K == CXCursor_ObjCClassMethodDecl ||
14706f32e7eSjoerg       K == CXCursor_ObjCInstanceMethodDecl) {
14806f32e7eSjoerg     int SelectorIdIndex = -1;
14906f32e7eSjoerg     // Check if cursor points to a selector id.
15006f32e7eSjoerg     if (RegionOfInterest.isValid() &&
15106f32e7eSjoerg         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
15206f32e7eSjoerg       SmallVector<SourceLocation, 16> SelLocs;
15306f32e7eSjoerg       cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
15406f32e7eSjoerg       SmallVectorImpl<SourceLocation>::iterator I =
15506f32e7eSjoerg           llvm::find(SelLocs, RegionOfInterest.getBegin());
15606f32e7eSjoerg       if (I != SelLocs.end())
15706f32e7eSjoerg         SelectorIdIndex = I - SelLocs.begin();
15806f32e7eSjoerg     }
159*13fbcb42Sjoerg     CXCursor C = {K,
160*13fbcb42Sjoerg                   SelectorIdIndex,
16106f32e7eSjoerg                   {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
16206f32e7eSjoerg     return C;
16306f32e7eSjoerg   }
16406f32e7eSjoerg 
16506f32e7eSjoerg   CXCursor C = {K, 0, {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
16606f32e7eSjoerg   return C;
16706f32e7eSjoerg }
16806f32e7eSjoerg 
MakeCXCursor(const Stmt * S,const Decl * Parent,CXTranslationUnit TU,SourceRange RegionOfInterest)16906f32e7eSjoerg CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
17006f32e7eSjoerg                                 CXTranslationUnit TU,
17106f32e7eSjoerg                                 SourceRange RegionOfInterest) {
17206f32e7eSjoerg   assert(S && TU && "Invalid arguments!");
17306f32e7eSjoerg   CXCursorKind K = CXCursor_NotImplemented;
17406f32e7eSjoerg 
17506f32e7eSjoerg   switch (S->getStmtClass()) {
17606f32e7eSjoerg   case Stmt::NoStmtClass:
17706f32e7eSjoerg     break;
17806f32e7eSjoerg 
17906f32e7eSjoerg   case Stmt::CaseStmtClass:
18006f32e7eSjoerg     K = CXCursor_CaseStmt;
18106f32e7eSjoerg     break;
18206f32e7eSjoerg 
18306f32e7eSjoerg   case Stmt::DefaultStmtClass:
18406f32e7eSjoerg     K = CXCursor_DefaultStmt;
18506f32e7eSjoerg     break;
18606f32e7eSjoerg 
18706f32e7eSjoerg   case Stmt::IfStmtClass:
18806f32e7eSjoerg     K = CXCursor_IfStmt;
18906f32e7eSjoerg     break;
19006f32e7eSjoerg 
19106f32e7eSjoerg   case Stmt::SwitchStmtClass:
19206f32e7eSjoerg     K = CXCursor_SwitchStmt;
19306f32e7eSjoerg     break;
19406f32e7eSjoerg 
19506f32e7eSjoerg   case Stmt::WhileStmtClass:
19606f32e7eSjoerg     K = CXCursor_WhileStmt;
19706f32e7eSjoerg     break;
19806f32e7eSjoerg 
19906f32e7eSjoerg   case Stmt::DoStmtClass:
20006f32e7eSjoerg     K = CXCursor_DoStmt;
20106f32e7eSjoerg     break;
20206f32e7eSjoerg 
20306f32e7eSjoerg   case Stmt::ForStmtClass:
20406f32e7eSjoerg     K = CXCursor_ForStmt;
20506f32e7eSjoerg     break;
20606f32e7eSjoerg 
20706f32e7eSjoerg   case Stmt::GotoStmtClass:
20806f32e7eSjoerg     K = CXCursor_GotoStmt;
20906f32e7eSjoerg     break;
21006f32e7eSjoerg 
21106f32e7eSjoerg   case Stmt::IndirectGotoStmtClass:
21206f32e7eSjoerg     K = CXCursor_IndirectGotoStmt;
21306f32e7eSjoerg     break;
21406f32e7eSjoerg 
21506f32e7eSjoerg   case Stmt::ContinueStmtClass:
21606f32e7eSjoerg     K = CXCursor_ContinueStmt;
21706f32e7eSjoerg     break;
21806f32e7eSjoerg 
21906f32e7eSjoerg   case Stmt::BreakStmtClass:
22006f32e7eSjoerg     K = CXCursor_BreakStmt;
22106f32e7eSjoerg     break;
22206f32e7eSjoerg 
22306f32e7eSjoerg   case Stmt::ReturnStmtClass:
22406f32e7eSjoerg     K = CXCursor_ReturnStmt;
22506f32e7eSjoerg     break;
22606f32e7eSjoerg 
22706f32e7eSjoerg   case Stmt::GCCAsmStmtClass:
22806f32e7eSjoerg     K = CXCursor_GCCAsmStmt;
22906f32e7eSjoerg     break;
23006f32e7eSjoerg 
23106f32e7eSjoerg   case Stmt::MSAsmStmtClass:
23206f32e7eSjoerg     K = CXCursor_MSAsmStmt;
23306f32e7eSjoerg     break;
23406f32e7eSjoerg 
23506f32e7eSjoerg   case Stmt::ObjCAtTryStmtClass:
23606f32e7eSjoerg     K = CXCursor_ObjCAtTryStmt;
23706f32e7eSjoerg     break;
23806f32e7eSjoerg 
23906f32e7eSjoerg   case Stmt::ObjCAtCatchStmtClass:
24006f32e7eSjoerg     K = CXCursor_ObjCAtCatchStmt;
24106f32e7eSjoerg     break;
24206f32e7eSjoerg 
24306f32e7eSjoerg   case Stmt::ObjCAtFinallyStmtClass:
24406f32e7eSjoerg     K = CXCursor_ObjCAtFinallyStmt;
24506f32e7eSjoerg     break;
24606f32e7eSjoerg 
24706f32e7eSjoerg   case Stmt::ObjCAtThrowStmtClass:
24806f32e7eSjoerg     K = CXCursor_ObjCAtThrowStmt;
24906f32e7eSjoerg     break;
25006f32e7eSjoerg 
25106f32e7eSjoerg   case Stmt::ObjCAtSynchronizedStmtClass:
25206f32e7eSjoerg     K = CXCursor_ObjCAtSynchronizedStmt;
25306f32e7eSjoerg     break;
25406f32e7eSjoerg 
25506f32e7eSjoerg   case Stmt::ObjCAutoreleasePoolStmtClass:
25606f32e7eSjoerg     K = CXCursor_ObjCAutoreleasePoolStmt;
25706f32e7eSjoerg     break;
25806f32e7eSjoerg 
25906f32e7eSjoerg   case Stmt::ObjCForCollectionStmtClass:
26006f32e7eSjoerg     K = CXCursor_ObjCForCollectionStmt;
26106f32e7eSjoerg     break;
26206f32e7eSjoerg 
26306f32e7eSjoerg   case Stmt::CXXCatchStmtClass:
26406f32e7eSjoerg     K = CXCursor_CXXCatchStmt;
26506f32e7eSjoerg     break;
26606f32e7eSjoerg 
26706f32e7eSjoerg   case Stmt::CXXTryStmtClass:
26806f32e7eSjoerg     K = CXCursor_CXXTryStmt;
26906f32e7eSjoerg     break;
27006f32e7eSjoerg 
27106f32e7eSjoerg   case Stmt::CXXForRangeStmtClass:
27206f32e7eSjoerg     K = CXCursor_CXXForRangeStmt;
27306f32e7eSjoerg     break;
27406f32e7eSjoerg 
27506f32e7eSjoerg   case Stmt::SEHTryStmtClass:
27606f32e7eSjoerg     K = CXCursor_SEHTryStmt;
27706f32e7eSjoerg     break;
27806f32e7eSjoerg 
27906f32e7eSjoerg   case Stmt::SEHExceptStmtClass:
28006f32e7eSjoerg     K = CXCursor_SEHExceptStmt;
28106f32e7eSjoerg     break;
28206f32e7eSjoerg 
28306f32e7eSjoerg   case Stmt::SEHFinallyStmtClass:
28406f32e7eSjoerg     K = CXCursor_SEHFinallyStmt;
28506f32e7eSjoerg     break;
28606f32e7eSjoerg 
28706f32e7eSjoerg   case Stmt::SEHLeaveStmtClass:
28806f32e7eSjoerg     K = CXCursor_SEHLeaveStmt;
28906f32e7eSjoerg     break;
29006f32e7eSjoerg 
29106f32e7eSjoerg   case Stmt::CoroutineBodyStmtClass:
29206f32e7eSjoerg   case Stmt::CoreturnStmtClass:
29306f32e7eSjoerg     K = CXCursor_UnexposedStmt;
29406f32e7eSjoerg     break;
29506f32e7eSjoerg 
29606f32e7eSjoerg   case Stmt::ArrayTypeTraitExprClass:
29706f32e7eSjoerg   case Stmt::AsTypeExprClass:
29806f32e7eSjoerg   case Stmt::AtomicExprClass:
29906f32e7eSjoerg   case Stmt::BinaryConditionalOperatorClass:
30006f32e7eSjoerg   case Stmt::TypeTraitExprClass:
30106f32e7eSjoerg   case Stmt::CoawaitExprClass:
30206f32e7eSjoerg   case Stmt::ConceptSpecializationExprClass:
303*13fbcb42Sjoerg   case Stmt::RequiresExprClass:
30406f32e7eSjoerg   case Stmt::DependentCoawaitExprClass:
30506f32e7eSjoerg   case Stmt::CoyieldExprClass:
30606f32e7eSjoerg   case Stmt::CXXBindTemporaryExprClass:
30706f32e7eSjoerg   case Stmt::CXXDefaultArgExprClass:
30806f32e7eSjoerg   case Stmt::CXXDefaultInitExprClass:
30906f32e7eSjoerg   case Stmt::CXXFoldExprClass:
31006f32e7eSjoerg   case Stmt::CXXRewrittenBinaryOperatorClass:
31106f32e7eSjoerg   case Stmt::CXXStdInitializerListExprClass:
31206f32e7eSjoerg   case Stmt::CXXScalarValueInitExprClass:
31306f32e7eSjoerg   case Stmt::CXXUuidofExprClass:
31406f32e7eSjoerg   case Stmt::ChooseExprClass:
31506f32e7eSjoerg   case Stmt::DesignatedInitExprClass:
31606f32e7eSjoerg   case Stmt::DesignatedInitUpdateExprClass:
31706f32e7eSjoerg   case Stmt::ArrayInitLoopExprClass:
31806f32e7eSjoerg   case Stmt::ArrayInitIndexExprClass:
31906f32e7eSjoerg   case Stmt::ExprWithCleanupsClass:
32006f32e7eSjoerg   case Stmt::ExpressionTraitExprClass:
32106f32e7eSjoerg   case Stmt::ExtVectorElementExprClass:
32206f32e7eSjoerg   case Stmt::ImplicitCastExprClass:
32306f32e7eSjoerg   case Stmt::ImplicitValueInitExprClass:
32406f32e7eSjoerg   case Stmt::NoInitExprClass:
32506f32e7eSjoerg   case Stmt::MaterializeTemporaryExprClass:
32606f32e7eSjoerg   case Stmt::ObjCIndirectCopyRestoreExprClass:
32706f32e7eSjoerg   case Stmt::OffsetOfExprClass:
32806f32e7eSjoerg   case Stmt::ParenListExprClass:
32906f32e7eSjoerg   case Stmt::PredefinedExprClass:
33006f32e7eSjoerg   case Stmt::ShuffleVectorExprClass:
33106f32e7eSjoerg   case Stmt::SourceLocExprClass:
33206f32e7eSjoerg   case Stmt::ConvertVectorExprClass:
33306f32e7eSjoerg   case Stmt::VAArgExprClass:
33406f32e7eSjoerg   case Stmt::ObjCArrayLiteralClass:
33506f32e7eSjoerg   case Stmt::ObjCDictionaryLiteralClass:
33606f32e7eSjoerg   case Stmt::ObjCBoxedExprClass:
33706f32e7eSjoerg   case Stmt::ObjCSubscriptRefExprClass:
338*13fbcb42Sjoerg   case Stmt::RecoveryExprClass:
33906f32e7eSjoerg     K = CXCursor_UnexposedExpr;
34006f32e7eSjoerg     break;
34106f32e7eSjoerg 
34206f32e7eSjoerg   case Stmt::OpaqueValueExprClass:
34306f32e7eSjoerg     if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
34406f32e7eSjoerg       return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
34506f32e7eSjoerg     K = CXCursor_UnexposedExpr;
34606f32e7eSjoerg     break;
34706f32e7eSjoerg 
34806f32e7eSjoerg   case Stmt::PseudoObjectExprClass:
349*13fbcb42Sjoerg     return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
350*13fbcb42Sjoerg                         TU, RegionOfInterest);
35106f32e7eSjoerg 
35206f32e7eSjoerg   case Stmt::CompoundStmtClass:
35306f32e7eSjoerg     K = CXCursor_CompoundStmt;
35406f32e7eSjoerg     break;
35506f32e7eSjoerg 
35606f32e7eSjoerg   case Stmt::NullStmtClass:
35706f32e7eSjoerg     K = CXCursor_NullStmt;
35806f32e7eSjoerg     break;
35906f32e7eSjoerg 
36006f32e7eSjoerg   case Stmt::LabelStmtClass:
36106f32e7eSjoerg     K = CXCursor_LabelStmt;
36206f32e7eSjoerg     break;
36306f32e7eSjoerg 
36406f32e7eSjoerg   case Stmt::AttributedStmtClass:
36506f32e7eSjoerg     K = CXCursor_UnexposedStmt;
36606f32e7eSjoerg     break;
36706f32e7eSjoerg 
36806f32e7eSjoerg   case Stmt::DeclStmtClass:
36906f32e7eSjoerg     K = CXCursor_DeclStmt;
37006f32e7eSjoerg     break;
37106f32e7eSjoerg 
37206f32e7eSjoerg   case Stmt::CapturedStmtClass:
37306f32e7eSjoerg     K = CXCursor_UnexposedStmt;
37406f32e7eSjoerg     break;
37506f32e7eSjoerg 
37606f32e7eSjoerg   case Stmt::IntegerLiteralClass:
37706f32e7eSjoerg     K = CXCursor_IntegerLiteral;
37806f32e7eSjoerg     break;
37906f32e7eSjoerg 
38006f32e7eSjoerg   case Stmt::FixedPointLiteralClass:
38106f32e7eSjoerg     K = CXCursor_FixedPointLiteral;
38206f32e7eSjoerg     break;
38306f32e7eSjoerg 
38406f32e7eSjoerg   case Stmt::FloatingLiteralClass:
38506f32e7eSjoerg     K = CXCursor_FloatingLiteral;
38606f32e7eSjoerg     break;
38706f32e7eSjoerg 
38806f32e7eSjoerg   case Stmt::ImaginaryLiteralClass:
38906f32e7eSjoerg     K = CXCursor_ImaginaryLiteral;
39006f32e7eSjoerg     break;
39106f32e7eSjoerg 
39206f32e7eSjoerg   case Stmt::StringLiteralClass:
39306f32e7eSjoerg     K = CXCursor_StringLiteral;
39406f32e7eSjoerg     break;
39506f32e7eSjoerg 
39606f32e7eSjoerg   case Stmt::CharacterLiteralClass:
39706f32e7eSjoerg     K = CXCursor_CharacterLiteral;
39806f32e7eSjoerg     break;
39906f32e7eSjoerg 
40006f32e7eSjoerg   case Stmt::ConstantExprClass:
401*13fbcb42Sjoerg     return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
402*13fbcb42Sjoerg                         RegionOfInterest);
40306f32e7eSjoerg 
40406f32e7eSjoerg   case Stmt::ParenExprClass:
40506f32e7eSjoerg     K = CXCursor_ParenExpr;
40606f32e7eSjoerg     break;
40706f32e7eSjoerg 
40806f32e7eSjoerg   case Stmt::UnaryOperatorClass:
40906f32e7eSjoerg     K = CXCursor_UnaryOperator;
41006f32e7eSjoerg     break;
41106f32e7eSjoerg 
41206f32e7eSjoerg   case Stmt::UnaryExprOrTypeTraitExprClass:
41306f32e7eSjoerg   case Stmt::CXXNoexceptExprClass:
41406f32e7eSjoerg     K = CXCursor_UnaryExpr;
41506f32e7eSjoerg     break;
41606f32e7eSjoerg 
41706f32e7eSjoerg   case Stmt::MSPropertySubscriptExprClass:
41806f32e7eSjoerg   case Stmt::ArraySubscriptExprClass:
41906f32e7eSjoerg     K = CXCursor_ArraySubscriptExpr;
42006f32e7eSjoerg     break;
42106f32e7eSjoerg 
422*13fbcb42Sjoerg   case Stmt::MatrixSubscriptExprClass:
423*13fbcb42Sjoerg     // TODO: add support for MatrixSubscriptExpr.
424*13fbcb42Sjoerg     K = CXCursor_UnexposedExpr;
425*13fbcb42Sjoerg     break;
426*13fbcb42Sjoerg 
42706f32e7eSjoerg   case Stmt::OMPArraySectionExprClass:
42806f32e7eSjoerg     K = CXCursor_OMPArraySectionExpr;
42906f32e7eSjoerg     break;
43006f32e7eSjoerg 
431*13fbcb42Sjoerg   case Stmt::OMPArrayShapingExprClass:
432*13fbcb42Sjoerg     K = CXCursor_OMPArrayShapingExpr;
433*13fbcb42Sjoerg     break;
434*13fbcb42Sjoerg 
435*13fbcb42Sjoerg   case Stmt::OMPIteratorExprClass:
436*13fbcb42Sjoerg     K = CXCursor_OMPIteratorExpr;
437*13fbcb42Sjoerg     break;
438*13fbcb42Sjoerg 
43906f32e7eSjoerg   case Stmt::BinaryOperatorClass:
44006f32e7eSjoerg     K = CXCursor_BinaryOperator;
44106f32e7eSjoerg     break;
44206f32e7eSjoerg 
44306f32e7eSjoerg   case Stmt::CompoundAssignOperatorClass:
44406f32e7eSjoerg     K = CXCursor_CompoundAssignOperator;
44506f32e7eSjoerg     break;
44606f32e7eSjoerg 
44706f32e7eSjoerg   case Stmt::ConditionalOperatorClass:
44806f32e7eSjoerg     K = CXCursor_ConditionalOperator;
44906f32e7eSjoerg     break;
45006f32e7eSjoerg 
45106f32e7eSjoerg   case Stmt::CStyleCastExprClass:
45206f32e7eSjoerg     K = CXCursor_CStyleCastExpr;
45306f32e7eSjoerg     break;
45406f32e7eSjoerg 
45506f32e7eSjoerg   case Stmt::CompoundLiteralExprClass:
45606f32e7eSjoerg     K = CXCursor_CompoundLiteralExpr;
45706f32e7eSjoerg     break;
45806f32e7eSjoerg 
45906f32e7eSjoerg   case Stmt::InitListExprClass:
46006f32e7eSjoerg     K = CXCursor_InitListExpr;
46106f32e7eSjoerg     break;
46206f32e7eSjoerg 
46306f32e7eSjoerg   case Stmt::AddrLabelExprClass:
46406f32e7eSjoerg     K = CXCursor_AddrLabelExpr;
46506f32e7eSjoerg     break;
46606f32e7eSjoerg 
46706f32e7eSjoerg   case Stmt::StmtExprClass:
46806f32e7eSjoerg     K = CXCursor_StmtExpr;
46906f32e7eSjoerg     break;
47006f32e7eSjoerg 
47106f32e7eSjoerg   case Stmt::GenericSelectionExprClass:
47206f32e7eSjoerg     K = CXCursor_GenericSelectionExpr;
47306f32e7eSjoerg     break;
47406f32e7eSjoerg 
47506f32e7eSjoerg   case Stmt::GNUNullExprClass:
47606f32e7eSjoerg     K = CXCursor_GNUNullExpr;
47706f32e7eSjoerg     break;
47806f32e7eSjoerg 
47906f32e7eSjoerg   case Stmt::CXXStaticCastExprClass:
48006f32e7eSjoerg     K = CXCursor_CXXStaticCastExpr;
48106f32e7eSjoerg     break;
48206f32e7eSjoerg 
48306f32e7eSjoerg   case Stmt::CXXDynamicCastExprClass:
48406f32e7eSjoerg     K = CXCursor_CXXDynamicCastExpr;
48506f32e7eSjoerg     break;
48606f32e7eSjoerg 
48706f32e7eSjoerg   case Stmt::CXXReinterpretCastExprClass:
48806f32e7eSjoerg     K = CXCursor_CXXReinterpretCastExpr;
48906f32e7eSjoerg     break;
49006f32e7eSjoerg 
49106f32e7eSjoerg   case Stmt::CXXConstCastExprClass:
49206f32e7eSjoerg     K = CXCursor_CXXConstCastExpr;
49306f32e7eSjoerg     break;
49406f32e7eSjoerg 
49506f32e7eSjoerg   case Stmt::CXXFunctionalCastExprClass:
49606f32e7eSjoerg     K = CXCursor_CXXFunctionalCastExpr;
49706f32e7eSjoerg     break;
49806f32e7eSjoerg 
499*13fbcb42Sjoerg   case Stmt::CXXAddrspaceCastExprClass:
500*13fbcb42Sjoerg     K = CXCursor_CXXAddrspaceCastExpr;
501*13fbcb42Sjoerg     break;
502*13fbcb42Sjoerg 
50306f32e7eSjoerg   case Stmt::CXXTypeidExprClass:
50406f32e7eSjoerg     K = CXCursor_CXXTypeidExpr;
50506f32e7eSjoerg     break;
50606f32e7eSjoerg 
50706f32e7eSjoerg   case Stmt::CXXBoolLiteralExprClass:
50806f32e7eSjoerg     K = CXCursor_CXXBoolLiteralExpr;
50906f32e7eSjoerg     break;
51006f32e7eSjoerg 
51106f32e7eSjoerg   case Stmt::CXXNullPtrLiteralExprClass:
51206f32e7eSjoerg     K = CXCursor_CXXNullPtrLiteralExpr;
51306f32e7eSjoerg     break;
51406f32e7eSjoerg 
51506f32e7eSjoerg   case Stmt::CXXThisExprClass:
51606f32e7eSjoerg     K = CXCursor_CXXThisExpr;
51706f32e7eSjoerg     break;
51806f32e7eSjoerg 
51906f32e7eSjoerg   case Stmt::CXXThrowExprClass:
52006f32e7eSjoerg     K = CXCursor_CXXThrowExpr;
52106f32e7eSjoerg     break;
52206f32e7eSjoerg 
52306f32e7eSjoerg   case Stmt::CXXNewExprClass:
52406f32e7eSjoerg     K = CXCursor_CXXNewExpr;
52506f32e7eSjoerg     break;
52606f32e7eSjoerg 
52706f32e7eSjoerg   case Stmt::CXXDeleteExprClass:
52806f32e7eSjoerg     K = CXCursor_CXXDeleteExpr;
52906f32e7eSjoerg     break;
53006f32e7eSjoerg 
53106f32e7eSjoerg   case Stmt::ObjCStringLiteralClass:
53206f32e7eSjoerg     K = CXCursor_ObjCStringLiteral;
53306f32e7eSjoerg     break;
53406f32e7eSjoerg 
53506f32e7eSjoerg   case Stmt::ObjCEncodeExprClass:
53606f32e7eSjoerg     K = CXCursor_ObjCEncodeExpr;
53706f32e7eSjoerg     break;
53806f32e7eSjoerg 
53906f32e7eSjoerg   case Stmt::ObjCSelectorExprClass:
54006f32e7eSjoerg     K = CXCursor_ObjCSelectorExpr;
54106f32e7eSjoerg     break;
54206f32e7eSjoerg 
54306f32e7eSjoerg   case Stmt::ObjCProtocolExprClass:
54406f32e7eSjoerg     K = CXCursor_ObjCProtocolExpr;
54506f32e7eSjoerg     break;
54606f32e7eSjoerg 
54706f32e7eSjoerg   case Stmt::ObjCBoolLiteralExprClass:
54806f32e7eSjoerg     K = CXCursor_ObjCBoolLiteralExpr;
54906f32e7eSjoerg     break;
55006f32e7eSjoerg 
55106f32e7eSjoerg   case Stmt::ObjCAvailabilityCheckExprClass:
55206f32e7eSjoerg     K = CXCursor_ObjCAvailabilityCheckExpr;
55306f32e7eSjoerg     break;
55406f32e7eSjoerg 
55506f32e7eSjoerg   case Stmt::ObjCBridgedCastExprClass:
55606f32e7eSjoerg     K = CXCursor_ObjCBridgedCastExpr;
55706f32e7eSjoerg     break;
55806f32e7eSjoerg 
55906f32e7eSjoerg   case Stmt::BlockExprClass:
56006f32e7eSjoerg     K = CXCursor_BlockExpr;
56106f32e7eSjoerg     break;
56206f32e7eSjoerg 
56306f32e7eSjoerg   case Stmt::PackExpansionExprClass:
56406f32e7eSjoerg     K = CXCursor_PackExpansionExpr;
56506f32e7eSjoerg     break;
56606f32e7eSjoerg 
56706f32e7eSjoerg   case Stmt::SizeOfPackExprClass:
56806f32e7eSjoerg     K = CXCursor_SizeOfPackExpr;
56906f32e7eSjoerg     break;
57006f32e7eSjoerg 
57106f32e7eSjoerg   case Stmt::DeclRefExprClass:
572*13fbcb42Sjoerg     if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
573*13fbcb42Sjoerg             cast<DeclRefExpr>(S)->getDecl())) {
57406f32e7eSjoerg       if (const ObjCMethodDecl *MD =
57506f32e7eSjoerg               dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
57606f32e7eSjoerg         if (MD->getSelfDecl() == IPD) {
57706f32e7eSjoerg           K = CXCursor_ObjCSelfExpr;
57806f32e7eSjoerg           break;
57906f32e7eSjoerg         }
58006f32e7eSjoerg       }
58106f32e7eSjoerg     }
58206f32e7eSjoerg 
58306f32e7eSjoerg     K = CXCursor_DeclRefExpr;
58406f32e7eSjoerg     break;
58506f32e7eSjoerg 
58606f32e7eSjoerg   case Stmt::DependentScopeDeclRefExprClass:
58706f32e7eSjoerg   case Stmt::SubstNonTypeTemplateParmExprClass:
58806f32e7eSjoerg   case Stmt::SubstNonTypeTemplateParmPackExprClass:
58906f32e7eSjoerg   case Stmt::FunctionParmPackExprClass:
59006f32e7eSjoerg   case Stmt::UnresolvedLookupExprClass:
59106f32e7eSjoerg   case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
59206f32e7eSjoerg     K = CXCursor_DeclRefExpr;
59306f32e7eSjoerg     break;
59406f32e7eSjoerg 
59506f32e7eSjoerg   case Stmt::CXXDependentScopeMemberExprClass:
59606f32e7eSjoerg   case Stmt::CXXPseudoDestructorExprClass:
59706f32e7eSjoerg   case Stmt::MemberExprClass:
59806f32e7eSjoerg   case Stmt::MSPropertyRefExprClass:
59906f32e7eSjoerg   case Stmt::ObjCIsaExprClass:
60006f32e7eSjoerg   case Stmt::ObjCIvarRefExprClass:
60106f32e7eSjoerg   case Stmt::ObjCPropertyRefExprClass:
60206f32e7eSjoerg   case Stmt::UnresolvedMemberExprClass:
60306f32e7eSjoerg     K = CXCursor_MemberRefExpr;
60406f32e7eSjoerg     break;
60506f32e7eSjoerg 
60606f32e7eSjoerg   case Stmt::CallExprClass:
60706f32e7eSjoerg   case Stmt::CXXOperatorCallExprClass:
60806f32e7eSjoerg   case Stmt::CXXMemberCallExprClass:
60906f32e7eSjoerg   case Stmt::CUDAKernelCallExprClass:
61006f32e7eSjoerg   case Stmt::CXXConstructExprClass:
61106f32e7eSjoerg   case Stmt::CXXInheritedCtorInitExprClass:
61206f32e7eSjoerg   case Stmt::CXXTemporaryObjectExprClass:
61306f32e7eSjoerg   case Stmt::CXXUnresolvedConstructExprClass:
61406f32e7eSjoerg   case Stmt::UserDefinedLiteralClass:
61506f32e7eSjoerg     K = CXCursor_CallExpr;
61606f32e7eSjoerg     break;
61706f32e7eSjoerg 
61806f32e7eSjoerg   case Stmt::LambdaExprClass:
61906f32e7eSjoerg     K = CXCursor_LambdaExpr;
62006f32e7eSjoerg     break;
62106f32e7eSjoerg 
62206f32e7eSjoerg   case Stmt::ObjCMessageExprClass: {
62306f32e7eSjoerg     K = CXCursor_ObjCMessageExpr;
62406f32e7eSjoerg     int SelectorIdIndex = -1;
62506f32e7eSjoerg     // Check if cursor points to a selector id.
62606f32e7eSjoerg     if (RegionOfInterest.isValid() &&
62706f32e7eSjoerg         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
62806f32e7eSjoerg       SmallVector<SourceLocation, 16> SelLocs;
62906f32e7eSjoerg       cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
63006f32e7eSjoerg       SmallVectorImpl<SourceLocation>::iterator I =
63106f32e7eSjoerg           llvm::find(SelLocs, RegionOfInterest.getBegin());
63206f32e7eSjoerg       if (I != SelLocs.end())
63306f32e7eSjoerg         SelectorIdIndex = I - SelLocs.begin();
63406f32e7eSjoerg     }
63506f32e7eSjoerg     CXCursor C = {K, 0, {Parent, S, TU}};
63606f32e7eSjoerg     return getSelectorIdentifierCursor(SelectorIdIndex, C);
63706f32e7eSjoerg   }
63806f32e7eSjoerg 
63906f32e7eSjoerg   case Stmt::MSDependentExistsStmtClass:
64006f32e7eSjoerg     K = CXCursor_UnexposedStmt;
64106f32e7eSjoerg     break;
642*13fbcb42Sjoerg   case Stmt::OMPCanonicalLoopClass:
643*13fbcb42Sjoerg     K = CXCursor_OMPCanonicalLoop;
644*13fbcb42Sjoerg     break;
64506f32e7eSjoerg   case Stmt::OMPParallelDirectiveClass:
64606f32e7eSjoerg     K = CXCursor_OMPParallelDirective;
64706f32e7eSjoerg     break;
64806f32e7eSjoerg   case Stmt::OMPSimdDirectiveClass:
64906f32e7eSjoerg     K = CXCursor_OMPSimdDirective;
65006f32e7eSjoerg     break;
651*13fbcb42Sjoerg   case Stmt::OMPTileDirectiveClass:
652*13fbcb42Sjoerg     K = CXCursor_OMPTileDirective;
653*13fbcb42Sjoerg     break;
65406f32e7eSjoerg   case Stmt::OMPForDirectiveClass:
65506f32e7eSjoerg     K = CXCursor_OMPForDirective;
65606f32e7eSjoerg     break;
65706f32e7eSjoerg   case Stmt::OMPForSimdDirectiveClass:
65806f32e7eSjoerg     K = CXCursor_OMPForSimdDirective;
65906f32e7eSjoerg     break;
66006f32e7eSjoerg   case Stmt::OMPSectionsDirectiveClass:
66106f32e7eSjoerg     K = CXCursor_OMPSectionsDirective;
66206f32e7eSjoerg     break;
66306f32e7eSjoerg   case Stmt::OMPSectionDirectiveClass:
66406f32e7eSjoerg     K = CXCursor_OMPSectionDirective;
66506f32e7eSjoerg     break;
66606f32e7eSjoerg   case Stmt::OMPSingleDirectiveClass:
66706f32e7eSjoerg     K = CXCursor_OMPSingleDirective;
66806f32e7eSjoerg     break;
66906f32e7eSjoerg   case Stmt::OMPMasterDirectiveClass:
67006f32e7eSjoerg     K = CXCursor_OMPMasterDirective;
67106f32e7eSjoerg     break;
67206f32e7eSjoerg   case Stmt::OMPCriticalDirectiveClass:
67306f32e7eSjoerg     K = CXCursor_OMPCriticalDirective;
67406f32e7eSjoerg     break;
67506f32e7eSjoerg   case Stmt::OMPParallelForDirectiveClass:
67606f32e7eSjoerg     K = CXCursor_OMPParallelForDirective;
67706f32e7eSjoerg     break;
67806f32e7eSjoerg   case Stmt::OMPParallelForSimdDirectiveClass:
67906f32e7eSjoerg     K = CXCursor_OMPParallelForSimdDirective;
68006f32e7eSjoerg     break;
681*13fbcb42Sjoerg   case Stmt::OMPParallelMasterDirectiveClass:
682*13fbcb42Sjoerg     K = CXCursor_OMPParallelMasterDirective;
683*13fbcb42Sjoerg     break;
68406f32e7eSjoerg   case Stmt::OMPParallelSectionsDirectiveClass:
68506f32e7eSjoerg     K = CXCursor_OMPParallelSectionsDirective;
68606f32e7eSjoerg     break;
68706f32e7eSjoerg   case Stmt::OMPTaskDirectiveClass:
68806f32e7eSjoerg     K = CXCursor_OMPTaskDirective;
68906f32e7eSjoerg     break;
69006f32e7eSjoerg   case Stmt::OMPTaskyieldDirectiveClass:
69106f32e7eSjoerg     K = CXCursor_OMPTaskyieldDirective;
69206f32e7eSjoerg     break;
69306f32e7eSjoerg   case Stmt::OMPBarrierDirectiveClass:
69406f32e7eSjoerg     K = CXCursor_OMPBarrierDirective;
69506f32e7eSjoerg     break;
69606f32e7eSjoerg   case Stmt::OMPTaskwaitDirectiveClass:
69706f32e7eSjoerg     K = CXCursor_OMPTaskwaitDirective;
69806f32e7eSjoerg     break;
69906f32e7eSjoerg   case Stmt::OMPTaskgroupDirectiveClass:
70006f32e7eSjoerg     K = CXCursor_OMPTaskgroupDirective;
70106f32e7eSjoerg     break;
70206f32e7eSjoerg   case Stmt::OMPFlushDirectiveClass:
70306f32e7eSjoerg     K = CXCursor_OMPFlushDirective;
70406f32e7eSjoerg     break;
705*13fbcb42Sjoerg   case Stmt::OMPDepobjDirectiveClass:
706*13fbcb42Sjoerg     K = CXCursor_OMPDepobjDirective;
707*13fbcb42Sjoerg     break;
708*13fbcb42Sjoerg   case Stmt::OMPScanDirectiveClass:
709*13fbcb42Sjoerg     K = CXCursor_OMPScanDirective;
710*13fbcb42Sjoerg     break;
71106f32e7eSjoerg   case Stmt::OMPOrderedDirectiveClass:
71206f32e7eSjoerg     K = CXCursor_OMPOrderedDirective;
71306f32e7eSjoerg     break;
71406f32e7eSjoerg   case Stmt::OMPAtomicDirectiveClass:
71506f32e7eSjoerg     K = CXCursor_OMPAtomicDirective;
71606f32e7eSjoerg     break;
71706f32e7eSjoerg   case Stmt::OMPTargetDirectiveClass:
71806f32e7eSjoerg     K = CXCursor_OMPTargetDirective;
71906f32e7eSjoerg     break;
72006f32e7eSjoerg   case Stmt::OMPTargetDataDirectiveClass:
72106f32e7eSjoerg     K = CXCursor_OMPTargetDataDirective;
72206f32e7eSjoerg     break;
72306f32e7eSjoerg   case Stmt::OMPTargetEnterDataDirectiveClass:
72406f32e7eSjoerg     K = CXCursor_OMPTargetEnterDataDirective;
72506f32e7eSjoerg     break;
72606f32e7eSjoerg   case Stmt::OMPTargetExitDataDirectiveClass:
72706f32e7eSjoerg     K = CXCursor_OMPTargetExitDataDirective;
72806f32e7eSjoerg     break;
72906f32e7eSjoerg   case Stmt::OMPTargetParallelDirectiveClass:
73006f32e7eSjoerg     K = CXCursor_OMPTargetParallelDirective;
73106f32e7eSjoerg     break;
73206f32e7eSjoerg   case Stmt::OMPTargetParallelForDirectiveClass:
73306f32e7eSjoerg     K = CXCursor_OMPTargetParallelForDirective;
73406f32e7eSjoerg     break;
73506f32e7eSjoerg   case Stmt::OMPTargetUpdateDirectiveClass:
73606f32e7eSjoerg     K = CXCursor_OMPTargetUpdateDirective;
73706f32e7eSjoerg     break;
73806f32e7eSjoerg   case Stmt::OMPTeamsDirectiveClass:
73906f32e7eSjoerg     K = CXCursor_OMPTeamsDirective;
74006f32e7eSjoerg     break;
74106f32e7eSjoerg   case Stmt::OMPCancellationPointDirectiveClass:
74206f32e7eSjoerg     K = CXCursor_OMPCancellationPointDirective;
74306f32e7eSjoerg     break;
74406f32e7eSjoerg   case Stmt::OMPCancelDirectiveClass:
74506f32e7eSjoerg     K = CXCursor_OMPCancelDirective;
74606f32e7eSjoerg     break;
74706f32e7eSjoerg   case Stmt::OMPTaskLoopDirectiveClass:
74806f32e7eSjoerg     K = CXCursor_OMPTaskLoopDirective;
74906f32e7eSjoerg     break;
75006f32e7eSjoerg   case Stmt::OMPTaskLoopSimdDirectiveClass:
75106f32e7eSjoerg     K = CXCursor_OMPTaskLoopSimdDirective;
75206f32e7eSjoerg     break;
75306f32e7eSjoerg   case Stmt::OMPMasterTaskLoopDirectiveClass:
75406f32e7eSjoerg     K = CXCursor_OMPMasterTaskLoopDirective;
75506f32e7eSjoerg     break;
75606f32e7eSjoerg   case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
75706f32e7eSjoerg     K = CXCursor_OMPMasterTaskLoopSimdDirective;
75806f32e7eSjoerg     break;
75906f32e7eSjoerg   case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
76006f32e7eSjoerg     K = CXCursor_OMPParallelMasterTaskLoopDirective;
76106f32e7eSjoerg     break;
762*13fbcb42Sjoerg   case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
763*13fbcb42Sjoerg     K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
764*13fbcb42Sjoerg     break;
76506f32e7eSjoerg   case Stmt::OMPDistributeDirectiveClass:
76606f32e7eSjoerg     K = CXCursor_OMPDistributeDirective;
76706f32e7eSjoerg     break;
76806f32e7eSjoerg   case Stmt::OMPDistributeParallelForDirectiveClass:
76906f32e7eSjoerg     K = CXCursor_OMPDistributeParallelForDirective;
77006f32e7eSjoerg     break;
77106f32e7eSjoerg   case Stmt::OMPDistributeParallelForSimdDirectiveClass:
77206f32e7eSjoerg     K = CXCursor_OMPDistributeParallelForSimdDirective;
77306f32e7eSjoerg     break;
77406f32e7eSjoerg   case Stmt::OMPDistributeSimdDirectiveClass:
77506f32e7eSjoerg     K = CXCursor_OMPDistributeSimdDirective;
77606f32e7eSjoerg     break;
77706f32e7eSjoerg   case Stmt::OMPTargetParallelForSimdDirectiveClass:
77806f32e7eSjoerg     K = CXCursor_OMPTargetParallelForSimdDirective;
77906f32e7eSjoerg     break;
78006f32e7eSjoerg   case Stmt::OMPTargetSimdDirectiveClass:
78106f32e7eSjoerg     K = CXCursor_OMPTargetSimdDirective;
78206f32e7eSjoerg     break;
78306f32e7eSjoerg   case Stmt::OMPTeamsDistributeDirectiveClass:
78406f32e7eSjoerg     K = CXCursor_OMPTeamsDistributeDirective;
78506f32e7eSjoerg     break;
78606f32e7eSjoerg   case Stmt::OMPTeamsDistributeSimdDirectiveClass:
78706f32e7eSjoerg     K = CXCursor_OMPTeamsDistributeSimdDirective;
78806f32e7eSjoerg     break;
78906f32e7eSjoerg   case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
79006f32e7eSjoerg     K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
79106f32e7eSjoerg     break;
79206f32e7eSjoerg   case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
79306f32e7eSjoerg     K = CXCursor_OMPTeamsDistributeParallelForDirective;
79406f32e7eSjoerg     break;
79506f32e7eSjoerg   case Stmt::OMPTargetTeamsDirectiveClass:
79606f32e7eSjoerg     K = CXCursor_OMPTargetTeamsDirective;
79706f32e7eSjoerg     break;
79806f32e7eSjoerg   case Stmt::OMPTargetTeamsDistributeDirectiveClass:
79906f32e7eSjoerg     K = CXCursor_OMPTargetTeamsDistributeDirective;
80006f32e7eSjoerg     break;
80106f32e7eSjoerg   case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
80206f32e7eSjoerg     K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
80306f32e7eSjoerg     break;
80406f32e7eSjoerg   case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
80506f32e7eSjoerg     K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
80606f32e7eSjoerg     break;
80706f32e7eSjoerg   case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
80806f32e7eSjoerg     K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
80906f32e7eSjoerg     break;
810*13fbcb42Sjoerg   case Stmt::OMPInteropDirectiveClass:
811*13fbcb42Sjoerg     K = CXCursor_OMPInteropDirective;
812*13fbcb42Sjoerg     break;
813*13fbcb42Sjoerg   case Stmt::OMPDispatchDirectiveClass:
814*13fbcb42Sjoerg     K = CXCursor_OMPDispatchDirective;
815*13fbcb42Sjoerg     break;
816*13fbcb42Sjoerg   case Stmt::OMPMaskedDirectiveClass:
817*13fbcb42Sjoerg     K = CXCursor_OMPMaskedDirective;
818*13fbcb42Sjoerg     break;
81906f32e7eSjoerg   case Stmt::BuiltinBitCastExprClass:
82006f32e7eSjoerg     K = CXCursor_BuiltinBitCastExpr;
82106f32e7eSjoerg   }
82206f32e7eSjoerg 
82306f32e7eSjoerg   CXCursor C = {K, 0, {Parent, S, TU}};
82406f32e7eSjoerg   return C;
82506f32e7eSjoerg }
82606f32e7eSjoerg 
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl * Super,SourceLocation Loc,CXTranslationUnit TU)82706f32e7eSjoerg CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
82806f32e7eSjoerg                                                SourceLocation Loc,
82906f32e7eSjoerg                                                CXTranslationUnit TU) {
83006f32e7eSjoerg   assert(Super && TU && "Invalid arguments!");
83106f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
83206f32e7eSjoerg   CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
83306f32e7eSjoerg   return C;
83406f32e7eSjoerg }
83506f32e7eSjoerg 
83606f32e7eSjoerg std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCSuperClassRef(CXCursor C)83706f32e7eSjoerg cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
83806f32e7eSjoerg   assert(C.kind == CXCursor_ObjCSuperClassRef);
83906f32e7eSjoerg   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
84006f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
84106f32e7eSjoerg }
84206f32e7eSjoerg 
MakeCursorObjCProtocolRef(const ObjCProtocolDecl * Proto,SourceLocation Loc,CXTranslationUnit TU)84306f32e7eSjoerg CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
84406f32e7eSjoerg                                              SourceLocation Loc,
84506f32e7eSjoerg                                              CXTranslationUnit TU) {
84606f32e7eSjoerg   assert(Proto && TU && "Invalid arguments!");
84706f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
84806f32e7eSjoerg   CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
84906f32e7eSjoerg   return C;
85006f32e7eSjoerg }
85106f32e7eSjoerg 
85206f32e7eSjoerg std::pair<const ObjCProtocolDecl *, SourceLocation>
getCursorObjCProtocolRef(CXCursor C)85306f32e7eSjoerg cxcursor::getCursorObjCProtocolRef(CXCursor C) {
85406f32e7eSjoerg   assert(C.kind == CXCursor_ObjCProtocolRef);
85506f32e7eSjoerg   return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
85606f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
85706f32e7eSjoerg }
85806f32e7eSjoerg 
MakeCursorObjCClassRef(const ObjCInterfaceDecl * Class,SourceLocation Loc,CXTranslationUnit TU)85906f32e7eSjoerg CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
86006f32e7eSjoerg                                           SourceLocation Loc,
86106f32e7eSjoerg                                           CXTranslationUnit TU) {
86206f32e7eSjoerg   // 'Class' can be null for invalid code.
86306f32e7eSjoerg   if (!Class)
86406f32e7eSjoerg     return MakeCXCursorInvalid(CXCursor_InvalidCode);
86506f32e7eSjoerg   assert(TU && "Invalid arguments!");
86606f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
86706f32e7eSjoerg   CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
86806f32e7eSjoerg   return C;
86906f32e7eSjoerg }
87006f32e7eSjoerg 
87106f32e7eSjoerg std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C)87206f32e7eSjoerg cxcursor::getCursorObjCClassRef(CXCursor C) {
87306f32e7eSjoerg   assert(C.kind == CXCursor_ObjCClassRef);
87406f32e7eSjoerg   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
87506f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
87606f32e7eSjoerg }
87706f32e7eSjoerg 
MakeCursorTypeRef(const TypeDecl * Type,SourceLocation Loc,CXTranslationUnit TU)87806f32e7eSjoerg CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
87906f32e7eSjoerg                                      CXTranslationUnit TU) {
88006f32e7eSjoerg   assert(Type && TU && "Invalid arguments!");
88106f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
88206f32e7eSjoerg   CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
88306f32e7eSjoerg   return C;
88406f32e7eSjoerg }
88506f32e7eSjoerg 
88606f32e7eSjoerg std::pair<const TypeDecl *, SourceLocation>
getCursorTypeRef(CXCursor C)88706f32e7eSjoerg cxcursor::getCursorTypeRef(CXCursor C) {
88806f32e7eSjoerg   assert(C.kind == CXCursor_TypeRef);
88906f32e7eSjoerg   return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
89006f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
89106f32e7eSjoerg }
89206f32e7eSjoerg 
MakeCursorTemplateRef(const TemplateDecl * Template,SourceLocation Loc,CXTranslationUnit TU)89306f32e7eSjoerg CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
89406f32e7eSjoerg                                          SourceLocation Loc,
89506f32e7eSjoerg                                          CXTranslationUnit TU) {
89606f32e7eSjoerg   assert(Template && TU && "Invalid arguments!");
89706f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
89806f32e7eSjoerg   CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
89906f32e7eSjoerg   return C;
90006f32e7eSjoerg }
90106f32e7eSjoerg 
90206f32e7eSjoerg std::pair<const TemplateDecl *, SourceLocation>
getCursorTemplateRef(CXCursor C)90306f32e7eSjoerg cxcursor::getCursorTemplateRef(CXCursor C) {
90406f32e7eSjoerg   assert(C.kind == CXCursor_TemplateRef);
90506f32e7eSjoerg   return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
90606f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
90706f32e7eSjoerg }
90806f32e7eSjoerg 
MakeCursorNamespaceRef(const NamedDecl * NS,SourceLocation Loc,CXTranslationUnit TU)90906f32e7eSjoerg CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
91006f32e7eSjoerg                                           SourceLocation Loc,
91106f32e7eSjoerg                                           CXTranslationUnit TU) {
91206f32e7eSjoerg 
91306f32e7eSjoerg   assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
91406f32e7eSjoerg          "Invalid arguments!");
91506f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
91606f32e7eSjoerg   CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
91706f32e7eSjoerg   return C;
91806f32e7eSjoerg }
91906f32e7eSjoerg 
92006f32e7eSjoerg std::pair<const NamedDecl *, SourceLocation>
getCursorNamespaceRef(CXCursor C)92106f32e7eSjoerg cxcursor::getCursorNamespaceRef(CXCursor C) {
92206f32e7eSjoerg   assert(C.kind == CXCursor_NamespaceRef);
92306f32e7eSjoerg   return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
92406f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
92506f32e7eSjoerg }
92606f32e7eSjoerg 
MakeCursorVariableRef(const VarDecl * Var,SourceLocation Loc,CXTranslationUnit TU)92706f32e7eSjoerg CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
92806f32e7eSjoerg                                          CXTranslationUnit TU) {
92906f32e7eSjoerg 
93006f32e7eSjoerg   assert(Var && TU && "Invalid arguments!");
93106f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
93206f32e7eSjoerg   CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
93306f32e7eSjoerg   return C;
93406f32e7eSjoerg }
93506f32e7eSjoerg 
93606f32e7eSjoerg std::pair<const VarDecl *, SourceLocation>
getCursorVariableRef(CXCursor C)93706f32e7eSjoerg cxcursor::getCursorVariableRef(CXCursor C) {
93806f32e7eSjoerg   assert(C.kind == CXCursor_VariableRef);
93906f32e7eSjoerg   return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
94006f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
94106f32e7eSjoerg }
94206f32e7eSjoerg 
MakeCursorMemberRef(const FieldDecl * Field,SourceLocation Loc,CXTranslationUnit TU)943*13fbcb42Sjoerg CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
944*13fbcb42Sjoerg                                        SourceLocation Loc,
94506f32e7eSjoerg                                        CXTranslationUnit TU) {
94606f32e7eSjoerg 
94706f32e7eSjoerg   assert(Field && TU && "Invalid arguments!");
94806f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
94906f32e7eSjoerg   CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
95006f32e7eSjoerg   return C;
95106f32e7eSjoerg }
95206f32e7eSjoerg 
95306f32e7eSjoerg std::pair<const FieldDecl *, SourceLocation>
getCursorMemberRef(CXCursor C)95406f32e7eSjoerg cxcursor::getCursorMemberRef(CXCursor C) {
95506f32e7eSjoerg   assert(C.kind == CXCursor_MemberRef);
95606f32e7eSjoerg   return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
95706f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
95806f32e7eSjoerg }
95906f32e7eSjoerg 
MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier * B,CXTranslationUnit TU)96006f32e7eSjoerg CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
96106f32e7eSjoerg                                               CXTranslationUnit TU) {
96206f32e7eSjoerg   CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
96306f32e7eSjoerg   return C;
96406f32e7eSjoerg }
96506f32e7eSjoerg 
getCursorCXXBaseSpecifier(CXCursor C)96606f32e7eSjoerg const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
96706f32e7eSjoerg   assert(C.kind == CXCursor_CXXBaseSpecifier);
96806f32e7eSjoerg   return static_cast<const CXXBaseSpecifier *>(C.data[0]);
96906f32e7eSjoerg }
97006f32e7eSjoerg 
MakePreprocessingDirectiveCursor(SourceRange Range,CXTranslationUnit TU)97106f32e7eSjoerg CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
97206f32e7eSjoerg                                                     CXTranslationUnit TU) {
973*13fbcb42Sjoerg   CXCursor C = {
974*13fbcb42Sjoerg       CXCursor_PreprocessingDirective,
975*13fbcb42Sjoerg       0,
976*13fbcb42Sjoerg       {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
97706f32e7eSjoerg   return C;
97806f32e7eSjoerg }
97906f32e7eSjoerg 
getCursorPreprocessingDirective(CXCursor C)98006f32e7eSjoerg SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
98106f32e7eSjoerg   assert(C.kind == CXCursor_PreprocessingDirective);
98206f32e7eSjoerg   SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
98306f32e7eSjoerg                     SourceLocation::getFromPtrEncoding(C.data[1]));
98406f32e7eSjoerg   ASTUnit *TU = getCursorASTUnit(C);
98506f32e7eSjoerg   return TU->mapRangeFromPreamble(Range);
98606f32e7eSjoerg }
98706f32e7eSjoerg 
MakeMacroDefinitionCursor(const MacroDefinitionRecord * MI,CXTranslationUnit TU)98806f32e7eSjoerg CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
98906f32e7eSjoerg                                              CXTranslationUnit TU) {
99006f32e7eSjoerg   CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
99106f32e7eSjoerg   return C;
99206f32e7eSjoerg }
99306f32e7eSjoerg 
getCursorMacroDefinition(CXCursor C)99406f32e7eSjoerg const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
99506f32e7eSjoerg   assert(C.kind == CXCursor_MacroDefinition);
99606f32e7eSjoerg   return static_cast<const MacroDefinitionRecord *>(C.data[0]);
99706f32e7eSjoerg }
99806f32e7eSjoerg 
MakeMacroExpansionCursor(MacroExpansion * MI,CXTranslationUnit TU)99906f32e7eSjoerg CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
100006f32e7eSjoerg                                             CXTranslationUnit TU) {
100106f32e7eSjoerg   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
100206f32e7eSjoerg   return C;
100306f32e7eSjoerg }
100406f32e7eSjoerg 
MakeMacroExpansionCursor(MacroDefinitionRecord * MI,SourceLocation Loc,CXTranslationUnit TU)100506f32e7eSjoerg CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
100606f32e7eSjoerg                                             SourceLocation Loc,
100706f32e7eSjoerg                                             CXTranslationUnit TU) {
100806f32e7eSjoerg   assert(Loc.isValid());
100906f32e7eSjoerg   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
101006f32e7eSjoerg   return C;
101106f32e7eSjoerg }
101206f32e7eSjoerg 
getName() const101306f32e7eSjoerg const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
101406f32e7eSjoerg   if (isPseudo())
101506f32e7eSjoerg     return getAsMacroDefinition()->getName();
101606f32e7eSjoerg   return getAsMacroExpansion()->getName();
101706f32e7eSjoerg }
101806f32e7eSjoerg const MacroDefinitionRecord *
getDefinition() const101906f32e7eSjoerg cxcursor::MacroExpansionCursor::getDefinition() const {
102006f32e7eSjoerg   if (isPseudo())
102106f32e7eSjoerg     return getAsMacroDefinition();
102206f32e7eSjoerg   return getAsMacroExpansion()->getDefinition();
102306f32e7eSjoerg }
getSourceRange() const102406f32e7eSjoerg SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
102506f32e7eSjoerg   if (isPseudo())
102606f32e7eSjoerg     return getPseudoLoc();
102706f32e7eSjoerg   return getAsMacroExpansion()->getSourceRange();
102806f32e7eSjoerg }
102906f32e7eSjoerg 
MakeInclusionDirectiveCursor(InclusionDirective * ID,CXTranslationUnit TU)103006f32e7eSjoerg CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
103106f32e7eSjoerg                                                 CXTranslationUnit TU) {
103206f32e7eSjoerg   CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
103306f32e7eSjoerg   return C;
103406f32e7eSjoerg }
103506f32e7eSjoerg 
getCursorInclusionDirective(CXCursor C)103606f32e7eSjoerg const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
103706f32e7eSjoerg   assert(C.kind == CXCursor_InclusionDirective);
103806f32e7eSjoerg   return static_cast<const InclusionDirective *>(C.data[0]);
103906f32e7eSjoerg }
104006f32e7eSjoerg 
MakeCursorLabelRef(LabelStmt * Label,SourceLocation Loc,CXTranslationUnit TU)104106f32e7eSjoerg CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
104206f32e7eSjoerg                                       CXTranslationUnit TU) {
104306f32e7eSjoerg 
104406f32e7eSjoerg   assert(Label && TU && "Invalid arguments!");
104506f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
104606f32e7eSjoerg   CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
104706f32e7eSjoerg   return C;
104806f32e7eSjoerg }
104906f32e7eSjoerg 
105006f32e7eSjoerg std::pair<const LabelStmt *, SourceLocation>
getCursorLabelRef(CXCursor C)105106f32e7eSjoerg cxcursor::getCursorLabelRef(CXCursor C) {
105206f32e7eSjoerg   assert(C.kind == CXCursor_LabelRef);
105306f32e7eSjoerg   return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
105406f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
105506f32e7eSjoerg }
105606f32e7eSjoerg 
MakeCursorOverloadedDeclRef(const OverloadExpr * E,CXTranslationUnit TU)105706f32e7eSjoerg CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
105806f32e7eSjoerg                                                CXTranslationUnit TU) {
105906f32e7eSjoerg   assert(E && TU && "Invalid arguments!");
106006f32e7eSjoerg   OverloadedDeclRefStorage Storage(E);
106106f32e7eSjoerg   void *RawLoc = E->getNameLoc().getPtrEncoding();
106206f32e7eSjoerg   CXCursor C = {
1063*13fbcb42Sjoerg       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
106406f32e7eSjoerg   return C;
106506f32e7eSjoerg }
106606f32e7eSjoerg 
MakeCursorOverloadedDeclRef(const Decl * D,SourceLocation Loc,CXTranslationUnit TU)106706f32e7eSjoerg CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
106806f32e7eSjoerg                                                SourceLocation Loc,
106906f32e7eSjoerg                                                CXTranslationUnit TU) {
107006f32e7eSjoerg   assert(D && TU && "Invalid arguments!");
107106f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
107206f32e7eSjoerg   OverloadedDeclRefStorage Storage(D);
107306f32e7eSjoerg   CXCursor C = {
1074*13fbcb42Sjoerg       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
107506f32e7eSjoerg   return C;
107606f32e7eSjoerg }
107706f32e7eSjoerg 
MakeCursorOverloadedDeclRef(TemplateName Name,SourceLocation Loc,CXTranslationUnit TU)107806f32e7eSjoerg CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
107906f32e7eSjoerg                                                SourceLocation Loc,
108006f32e7eSjoerg                                                CXTranslationUnit TU) {
108106f32e7eSjoerg   assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
108206f32e7eSjoerg   void *RawLoc = Loc.getPtrEncoding();
108306f32e7eSjoerg   OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
108406f32e7eSjoerg   CXCursor C = {
1085*13fbcb42Sjoerg       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
108606f32e7eSjoerg   return C;
108706f32e7eSjoerg }
108806f32e7eSjoerg 
108906f32e7eSjoerg std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
getCursorOverloadedDeclRef(CXCursor C)109006f32e7eSjoerg cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
109106f32e7eSjoerg   assert(C.kind == CXCursor_OverloadedDeclRef);
109206f32e7eSjoerg   return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
109306f32e7eSjoerg                             const_cast<void *>(C.data[0])),
109406f32e7eSjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
109506f32e7eSjoerg }
109606f32e7eSjoerg 
getCursorDecl(CXCursor Cursor)109706f32e7eSjoerg const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
109806f32e7eSjoerg   return static_cast<const Decl *>(Cursor.data[0]);
109906f32e7eSjoerg }
110006f32e7eSjoerg 
getCursorExpr(CXCursor Cursor)110106f32e7eSjoerg const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
110206f32e7eSjoerg   return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
110306f32e7eSjoerg }
110406f32e7eSjoerg 
getCursorStmt(CXCursor Cursor)110506f32e7eSjoerg const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
110606f32e7eSjoerg   if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
110706f32e7eSjoerg       Cursor.kind == CXCursor_ObjCProtocolRef ||
110806f32e7eSjoerg       Cursor.kind == CXCursor_ObjCClassRef)
110906f32e7eSjoerg     return nullptr;
111006f32e7eSjoerg 
111106f32e7eSjoerg   return static_cast<const Stmt *>(Cursor.data[1]);
111206f32e7eSjoerg }
111306f32e7eSjoerg 
getCursorAttr(CXCursor Cursor)111406f32e7eSjoerg const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
111506f32e7eSjoerg   return static_cast<const Attr *>(Cursor.data[1]);
111606f32e7eSjoerg }
111706f32e7eSjoerg 
getCursorContext(CXCursor Cursor)111806f32e7eSjoerg ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
111906f32e7eSjoerg   return getCursorASTUnit(Cursor)->getASTContext();
112006f32e7eSjoerg }
112106f32e7eSjoerg 
getCursorASTUnit(CXCursor Cursor)112206f32e7eSjoerg ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
112306f32e7eSjoerg   CXTranslationUnit TU = getCursorTU(Cursor);
112406f32e7eSjoerg   if (!TU)
112506f32e7eSjoerg     return nullptr;
112606f32e7eSjoerg   return cxtu::getASTUnit(TU);
112706f32e7eSjoerg }
112806f32e7eSjoerg 
getCursorTU(CXCursor Cursor)112906f32e7eSjoerg CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
113006f32e7eSjoerg   return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
113106f32e7eSjoerg }
113206f32e7eSjoerg 
getOverriddenCursors(CXCursor cursor,SmallVectorImpl<CXCursor> & overridden)113306f32e7eSjoerg void cxcursor::getOverriddenCursors(CXCursor cursor,
113406f32e7eSjoerg                                     SmallVectorImpl<CXCursor> &overridden) {
113506f32e7eSjoerg   assert(clang_isDeclaration(cursor.kind));
113606f32e7eSjoerg   const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
113706f32e7eSjoerg   if (!D)
113806f32e7eSjoerg     return;
113906f32e7eSjoerg 
114006f32e7eSjoerg   CXTranslationUnit TU = getCursorTU(cursor);
114106f32e7eSjoerg   SmallVector<const NamedDecl *, 8> OverDecls;
114206f32e7eSjoerg   D->getASTContext().getOverriddenMethods(D, OverDecls);
114306f32e7eSjoerg 
1144*13fbcb42Sjoerg   for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1145*13fbcb42Sjoerg                                                     E = OverDecls.end();
1146*13fbcb42Sjoerg        I != E; ++I) {
114706f32e7eSjoerg     overridden.push_back(MakeCXCursor(*I, TU));
114806f32e7eSjoerg   }
114906f32e7eSjoerg }
115006f32e7eSjoerg 
115106f32e7eSjoerg std::pair<int, SourceLocation>
getSelectorIdentifierIndexAndLoc(CXCursor cursor)115206f32e7eSjoerg cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
115306f32e7eSjoerg   if (cursor.kind == CXCursor_ObjCMessageExpr) {
115406f32e7eSjoerg     if (cursor.xdata != -1)
115506f32e7eSjoerg       return std::make_pair(cursor.xdata,
115606f32e7eSjoerg                             cast<ObjCMessageExpr>(getCursorExpr(cursor))
115706f32e7eSjoerg                                 ->getSelectorLoc(cursor.xdata));
115806f32e7eSjoerg   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
115906f32e7eSjoerg              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
116006f32e7eSjoerg     if (cursor.xdata != -1)
116106f32e7eSjoerg       return std::make_pair(cursor.xdata,
116206f32e7eSjoerg                             cast<ObjCMethodDecl>(getCursorDecl(cursor))
116306f32e7eSjoerg                                 ->getSelectorLoc(cursor.xdata));
116406f32e7eSjoerg   }
116506f32e7eSjoerg 
116606f32e7eSjoerg   return std::make_pair(-1, SourceLocation());
116706f32e7eSjoerg }
116806f32e7eSjoerg 
getSelectorIdentifierCursor(int SelIdx,CXCursor cursor)116906f32e7eSjoerg CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
117006f32e7eSjoerg   CXCursor newCursor = cursor;
117106f32e7eSjoerg 
117206f32e7eSjoerg   if (cursor.kind == CXCursor_ObjCMessageExpr) {
117306f32e7eSjoerg     if (SelIdx == -1 ||
1174*13fbcb42Sjoerg         unsigned(SelIdx) >=
1175*13fbcb42Sjoerg             cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
117606f32e7eSjoerg       newCursor.xdata = -1;
117706f32e7eSjoerg     else
117806f32e7eSjoerg       newCursor.xdata = SelIdx;
117906f32e7eSjoerg   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
118006f32e7eSjoerg              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
118106f32e7eSjoerg     if (SelIdx == -1 ||
1182*13fbcb42Sjoerg         unsigned(SelIdx) >=
1183*13fbcb42Sjoerg             cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
118406f32e7eSjoerg       newCursor.xdata = -1;
118506f32e7eSjoerg     else
118606f32e7eSjoerg       newCursor.xdata = SelIdx;
118706f32e7eSjoerg   }
118806f32e7eSjoerg 
118906f32e7eSjoerg   return newCursor;
119006f32e7eSjoerg }
119106f32e7eSjoerg 
getTypeRefCursor(CXCursor cursor)119206f32e7eSjoerg CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
119306f32e7eSjoerg   if (cursor.kind != CXCursor_CallExpr)
119406f32e7eSjoerg     return cursor;
119506f32e7eSjoerg 
119606f32e7eSjoerg   if (cursor.xdata == 0)
119706f32e7eSjoerg     return cursor;
119806f32e7eSjoerg 
119906f32e7eSjoerg   const Expr *E = getCursorExpr(cursor);
120006f32e7eSjoerg   TypeSourceInfo *Type = nullptr;
1201*13fbcb42Sjoerg   if (const CXXUnresolvedConstructExpr *UnCtor =
1202*13fbcb42Sjoerg           dyn_cast<CXXUnresolvedConstructExpr>(E)) {
120306f32e7eSjoerg     Type = UnCtor->getTypeSourceInfo();
120406f32e7eSjoerg   } else if (const CXXTemporaryObjectExpr *Tmp =
120506f32e7eSjoerg                  dyn_cast<CXXTemporaryObjectExpr>(E)) {
120606f32e7eSjoerg     Type = Tmp->getTypeSourceInfo();
120706f32e7eSjoerg   }
120806f32e7eSjoerg 
120906f32e7eSjoerg   if (!Type)
121006f32e7eSjoerg     return cursor;
121106f32e7eSjoerg 
121206f32e7eSjoerg   CXTranslationUnit TU = getCursorTU(cursor);
121306f32e7eSjoerg   QualType Ty = Type->getType();
121406f32e7eSjoerg   TypeLoc TL = Type->getTypeLoc();
121506f32e7eSjoerg   SourceLocation Loc = TL.getBeginLoc();
121606f32e7eSjoerg 
121706f32e7eSjoerg   if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
121806f32e7eSjoerg     Ty = ElabT->getNamedType();
121906f32e7eSjoerg     ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
122006f32e7eSjoerg     Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
122106f32e7eSjoerg   }
122206f32e7eSjoerg 
122306f32e7eSjoerg   if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
122406f32e7eSjoerg     return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
122506f32e7eSjoerg   if (const TagType *Tag = Ty->getAs<TagType>())
122606f32e7eSjoerg     return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
122706f32e7eSjoerg   if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
122806f32e7eSjoerg     return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
122906f32e7eSjoerg 
123006f32e7eSjoerg   return cursor;
123106f32e7eSjoerg }
123206f32e7eSjoerg 
operator ==(CXCursor X,CXCursor Y)123306f32e7eSjoerg bool cxcursor::operator==(CXCursor X, CXCursor Y) {
123406f32e7eSjoerg   return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
123506f32e7eSjoerg          X.data[2] == Y.data[2];
123606f32e7eSjoerg }
123706f32e7eSjoerg 
123806f32e7eSjoerg // FIXME: Remove once we can model DeclGroups and their appropriate ranges
123906f32e7eSjoerg // properly in the ASTs.
isFirstInDeclGroup(CXCursor C)124006f32e7eSjoerg bool cxcursor::isFirstInDeclGroup(CXCursor C) {
124106f32e7eSjoerg   assert(clang_isDeclaration(C.kind));
124206f32e7eSjoerg   return ((uintptr_t)(C.data[1])) != 0;
124306f32e7eSjoerg }
124406f32e7eSjoerg 
124506f32e7eSjoerg //===----------------------------------------------------------------------===//
124606f32e7eSjoerg // libclang CXCursor APIs
124706f32e7eSjoerg //===----------------------------------------------------------------------===//
124806f32e7eSjoerg 
clang_Cursor_isNull(CXCursor cursor)124906f32e7eSjoerg int clang_Cursor_isNull(CXCursor cursor) {
125006f32e7eSjoerg   return clang_equalCursors(cursor, clang_getNullCursor());
125106f32e7eSjoerg }
125206f32e7eSjoerg 
clang_Cursor_getTranslationUnit(CXCursor cursor)125306f32e7eSjoerg CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
125406f32e7eSjoerg   return getCursorTU(cursor);
125506f32e7eSjoerg }
125606f32e7eSjoerg 
clang_Cursor_getNumArguments(CXCursor C)125706f32e7eSjoerg int clang_Cursor_getNumArguments(CXCursor C) {
125806f32e7eSjoerg   if (clang_isDeclaration(C.kind)) {
125906f32e7eSjoerg     const Decl *D = cxcursor::getCursorDecl(C);
126006f32e7eSjoerg     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
126106f32e7eSjoerg       return MD->param_size();
126206f32e7eSjoerg     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
126306f32e7eSjoerg       return FD->param_size();
126406f32e7eSjoerg   }
126506f32e7eSjoerg 
126606f32e7eSjoerg   if (clang_isExpression(C.kind)) {
126706f32e7eSjoerg     const Expr *E = cxcursor::getCursorExpr(C);
126806f32e7eSjoerg     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
126906f32e7eSjoerg       return CE->getNumArgs();
127006f32e7eSjoerg     }
127106f32e7eSjoerg     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
127206f32e7eSjoerg       return CE->getNumArgs();
127306f32e7eSjoerg     }
127406f32e7eSjoerg   }
127506f32e7eSjoerg 
127606f32e7eSjoerg   return -1;
127706f32e7eSjoerg }
127806f32e7eSjoerg 
clang_Cursor_getArgument(CXCursor C,unsigned i)127906f32e7eSjoerg CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
128006f32e7eSjoerg   if (clang_isDeclaration(C.kind)) {
128106f32e7eSjoerg     const Decl *D = cxcursor::getCursorDecl(C);
128206f32e7eSjoerg     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
128306f32e7eSjoerg       if (i < MD->param_size())
128406f32e7eSjoerg         return cxcursor::MakeCXCursor(MD->parameters()[i],
128506f32e7eSjoerg                                       cxcursor::getCursorTU(C));
128606f32e7eSjoerg     } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
128706f32e7eSjoerg       if (i < FD->param_size())
128806f32e7eSjoerg         return cxcursor::MakeCXCursor(FD->parameters()[i],
128906f32e7eSjoerg                                       cxcursor::getCursorTU(C));
129006f32e7eSjoerg     }
129106f32e7eSjoerg   }
129206f32e7eSjoerg 
129306f32e7eSjoerg   if (clang_isExpression(C.kind)) {
129406f32e7eSjoerg     const Expr *E = cxcursor::getCursorExpr(C);
129506f32e7eSjoerg     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
129606f32e7eSjoerg       if (i < CE->getNumArgs()) {
1297*13fbcb42Sjoerg         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
129806f32e7eSjoerg                                       cxcursor::getCursorTU(C));
129906f32e7eSjoerg       }
130006f32e7eSjoerg     }
130106f32e7eSjoerg     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
130206f32e7eSjoerg       if (i < CE->getNumArgs()) {
1303*13fbcb42Sjoerg         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
130406f32e7eSjoerg                                       cxcursor::getCursorTU(C));
130506f32e7eSjoerg       }
130606f32e7eSjoerg     }
130706f32e7eSjoerg   }
130806f32e7eSjoerg 
130906f32e7eSjoerg   return clang_getNullCursor();
131006f32e7eSjoerg }
131106f32e7eSjoerg 
clang_Cursor_getNumTemplateArguments(CXCursor C)131206f32e7eSjoerg int clang_Cursor_getNumTemplateArguments(CXCursor C) {
131306f32e7eSjoerg   if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
131406f32e7eSjoerg     return -1;
131506f32e7eSjoerg   }
131606f32e7eSjoerg 
1317*13fbcb42Sjoerg   const FunctionDecl *FD =
1318*13fbcb42Sjoerg       llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
131906f32e7eSjoerg   if (!FD) {
132006f32e7eSjoerg     return -1;
132106f32e7eSjoerg   }
132206f32e7eSjoerg 
132306f32e7eSjoerg   const FunctionTemplateSpecializationInfo *SpecInfo =
132406f32e7eSjoerg       FD->getTemplateSpecializationInfo();
132506f32e7eSjoerg   if (!SpecInfo) {
132606f32e7eSjoerg     return -1;
132706f32e7eSjoerg   }
132806f32e7eSjoerg 
132906f32e7eSjoerg   return SpecInfo->TemplateArguments->size();
133006f32e7eSjoerg }
133106f32e7eSjoerg 
133206f32e7eSjoerg enum CXGetTemplateArgumentStatus {
133306f32e7eSjoerg   /** The operation completed successfully */
133406f32e7eSjoerg   CXGetTemplateArgumentStatus_Success = 0,
133506f32e7eSjoerg 
133606f32e7eSjoerg   /** The specified cursor did not represent a FunctionDecl. */
133706f32e7eSjoerg   CXGetTemplateArgumentStatus_CursorNotFunctionDecl = -1,
133806f32e7eSjoerg 
133906f32e7eSjoerg   /** The specified cursor was not castable to a FunctionDecl. */
134006f32e7eSjoerg   CXGetTemplateArgumentStatus_BadFunctionDeclCast = -2,
134106f32e7eSjoerg 
134206f32e7eSjoerg   /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
134306f32e7eSjoerg   CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
134406f32e7eSjoerg 
134506f32e7eSjoerg   /** An invalid (OOB) argument index was specified */
134606f32e7eSjoerg   CXGetTemplateArgumentStatus_InvalidIndex = -4
134706f32e7eSjoerg };
134806f32e7eSjoerg 
clang_Cursor_getTemplateArgument(CXCursor C,unsigned I,TemplateArgument * TA)1349*13fbcb42Sjoerg static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1350*13fbcb42Sjoerg                                             TemplateArgument *TA) {
135106f32e7eSjoerg   if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
135206f32e7eSjoerg     return CXGetTemplateArgumentStatus_CursorNotFunctionDecl;
135306f32e7eSjoerg   }
135406f32e7eSjoerg 
1355*13fbcb42Sjoerg   const FunctionDecl *FD =
1356*13fbcb42Sjoerg       llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
135706f32e7eSjoerg   if (!FD) {
135806f32e7eSjoerg     return CXGetTemplateArgumentStatus_BadFunctionDeclCast;
135906f32e7eSjoerg   }
136006f32e7eSjoerg 
136106f32e7eSjoerg   const FunctionTemplateSpecializationInfo *SpecInfo =
136206f32e7eSjoerg       FD->getTemplateSpecializationInfo();
136306f32e7eSjoerg   if (!SpecInfo) {
136406f32e7eSjoerg     return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
136506f32e7eSjoerg   }
136606f32e7eSjoerg 
136706f32e7eSjoerg   if (I >= SpecInfo->TemplateArguments->size()) {
136806f32e7eSjoerg     return CXGetTemplateArgumentStatus_InvalidIndex;
136906f32e7eSjoerg   }
137006f32e7eSjoerg 
137106f32e7eSjoerg   *TA = SpecInfo->TemplateArguments->get(I);
137206f32e7eSjoerg   return 0;
137306f32e7eSjoerg }
137406f32e7eSjoerg 
clang_Cursor_getTemplateArgumentKind(CXCursor C,unsigned I)137506f32e7eSjoerg enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
137606f32e7eSjoerg                                                                  unsigned I) {
137706f32e7eSjoerg   TemplateArgument TA;
137806f32e7eSjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
137906f32e7eSjoerg     return CXTemplateArgumentKind_Invalid;
138006f32e7eSjoerg   }
138106f32e7eSjoerg 
138206f32e7eSjoerg   switch (TA.getKind()) {
1383*13fbcb42Sjoerg   case TemplateArgument::Null:
1384*13fbcb42Sjoerg     return CXTemplateArgumentKind_Null;
1385*13fbcb42Sjoerg   case TemplateArgument::Type:
1386*13fbcb42Sjoerg     return CXTemplateArgumentKind_Type;
138706f32e7eSjoerg   case TemplateArgument::Declaration:
138806f32e7eSjoerg     return CXTemplateArgumentKind_Declaration;
1389*13fbcb42Sjoerg   case TemplateArgument::NullPtr:
1390*13fbcb42Sjoerg     return CXTemplateArgumentKind_NullPtr;
1391*13fbcb42Sjoerg   case TemplateArgument::Integral:
1392*13fbcb42Sjoerg     return CXTemplateArgumentKind_Integral;
1393*13fbcb42Sjoerg   case TemplateArgument::Template:
1394*13fbcb42Sjoerg     return CXTemplateArgumentKind_Template;
139506f32e7eSjoerg   case TemplateArgument::TemplateExpansion:
139606f32e7eSjoerg     return CXTemplateArgumentKind_TemplateExpansion;
1397*13fbcb42Sjoerg   case TemplateArgument::Expression:
1398*13fbcb42Sjoerg     return CXTemplateArgumentKind_Expression;
1399*13fbcb42Sjoerg   case TemplateArgument::Pack:
1400*13fbcb42Sjoerg     return CXTemplateArgumentKind_Pack;
140106f32e7eSjoerg   }
140206f32e7eSjoerg 
140306f32e7eSjoerg   return CXTemplateArgumentKind_Invalid;
140406f32e7eSjoerg }
140506f32e7eSjoerg 
clang_Cursor_getTemplateArgumentType(CXCursor C,unsigned I)140606f32e7eSjoerg CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
140706f32e7eSjoerg   TemplateArgument TA;
140806f32e7eSjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
140906f32e7eSjoerg       CXGetTemplateArgumentStatus_Success) {
141006f32e7eSjoerg     return cxtype::MakeCXType(QualType(), getCursorTU(C));
141106f32e7eSjoerg   }
141206f32e7eSjoerg 
141306f32e7eSjoerg   if (TA.getKind() != TemplateArgument::Type) {
141406f32e7eSjoerg     return cxtype::MakeCXType(QualType(), getCursorTU(C));
141506f32e7eSjoerg   }
141606f32e7eSjoerg 
141706f32e7eSjoerg   return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
141806f32e7eSjoerg }
141906f32e7eSjoerg 
clang_Cursor_getTemplateArgumentValue(CXCursor C,unsigned I)142006f32e7eSjoerg long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
142106f32e7eSjoerg   TemplateArgument TA;
142206f32e7eSjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
142306f32e7eSjoerg       CXGetTemplateArgumentStatus_Success) {
142406f32e7eSjoerg     assert(0 && "Unable to retrieve TemplateArgument");
142506f32e7eSjoerg     return 0;
142606f32e7eSjoerg   }
142706f32e7eSjoerg 
142806f32e7eSjoerg   if (TA.getKind() != TemplateArgument::Integral) {
142906f32e7eSjoerg     assert(0 && "Passed template argument is not Integral");
143006f32e7eSjoerg     return 0;
143106f32e7eSjoerg   }
143206f32e7eSjoerg 
143306f32e7eSjoerg   return TA.getAsIntegral().getSExtValue();
143406f32e7eSjoerg }
143506f32e7eSjoerg 
clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,unsigned I)143606f32e7eSjoerg unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
143706f32e7eSjoerg                                                                  unsigned I) {
143806f32e7eSjoerg   TemplateArgument TA;
143906f32e7eSjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
144006f32e7eSjoerg       CXGetTemplateArgumentStatus_Success) {
144106f32e7eSjoerg     assert(0 && "Unable to retrieve TemplateArgument");
144206f32e7eSjoerg     return 0;
144306f32e7eSjoerg   }
144406f32e7eSjoerg 
144506f32e7eSjoerg   if (TA.getKind() != TemplateArgument::Integral) {
144606f32e7eSjoerg     assert(0 && "Passed template argument is not Integral");
144706f32e7eSjoerg     return 0;
144806f32e7eSjoerg   }
144906f32e7eSjoerg 
145006f32e7eSjoerg   return TA.getAsIntegral().getZExtValue();
145106f32e7eSjoerg }
145206f32e7eSjoerg 
145306f32e7eSjoerg //===----------------------------------------------------------------------===//
145406f32e7eSjoerg // CXCursorSet.
145506f32e7eSjoerg //===----------------------------------------------------------------------===//
145606f32e7eSjoerg 
145706f32e7eSjoerg typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
145806f32e7eSjoerg 
packCXCursorSet(CXCursorSet_Impl * setImpl)145906f32e7eSjoerg static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
146006f32e7eSjoerg   return (CXCursorSet)setImpl;
146106f32e7eSjoerg }
unpackCXCursorSet(CXCursorSet set)146206f32e7eSjoerg static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
146306f32e7eSjoerg   return (CXCursorSet_Impl *)set;
146406f32e7eSjoerg }
146506f32e7eSjoerg namespace llvm {
146606f32e7eSjoerg template <> struct DenseMapInfo<CXCursor> {
146706f32e7eSjoerg public:
getEmptyKeyllvm::DenseMapInfo146806f32e7eSjoerg   static inline CXCursor getEmptyKey() {
146906f32e7eSjoerg     return MakeCXCursorInvalid(CXCursor_InvalidFile);
147006f32e7eSjoerg   }
getTombstoneKeyllvm::DenseMapInfo147106f32e7eSjoerg   static inline CXCursor getTombstoneKey() {
147206f32e7eSjoerg     return MakeCXCursorInvalid(CXCursor_NoDeclFound);
147306f32e7eSjoerg   }
getHashValuellvm::DenseMapInfo147406f32e7eSjoerg   static inline unsigned getHashValue(const CXCursor &cursor) {
1475*13fbcb42Sjoerg     return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1476*13fbcb42Sjoerg         getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
147706f32e7eSjoerg   }
isEqualllvm::DenseMapInfo147806f32e7eSjoerg   static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1479*13fbcb42Sjoerg     return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
148006f32e7eSjoerg   }
148106f32e7eSjoerg };
1482*13fbcb42Sjoerg } // namespace llvm
148306f32e7eSjoerg 
clang_createCXCursorSet()148406f32e7eSjoerg CXCursorSet clang_createCXCursorSet() {
148506f32e7eSjoerg   return packCXCursorSet(new CXCursorSet_Impl());
148606f32e7eSjoerg }
148706f32e7eSjoerg 
clang_disposeCXCursorSet(CXCursorSet set)148806f32e7eSjoerg void clang_disposeCXCursorSet(CXCursorSet set) {
148906f32e7eSjoerg   delete unpackCXCursorSet(set);
149006f32e7eSjoerg }
149106f32e7eSjoerg 
clang_CXCursorSet_contains(CXCursorSet set,CXCursor cursor)149206f32e7eSjoerg unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
149306f32e7eSjoerg   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
149406f32e7eSjoerg   if (!setImpl)
149506f32e7eSjoerg     return 0;
149606f32e7eSjoerg   return setImpl->find(cursor) != setImpl->end();
149706f32e7eSjoerg }
149806f32e7eSjoerg 
clang_CXCursorSet_insert(CXCursorSet set,CXCursor cursor)149906f32e7eSjoerg unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
150006f32e7eSjoerg   // Do not insert invalid cursors into the set.
150106f32e7eSjoerg   if (cursor.kind >= CXCursor_FirstInvalid &&
150206f32e7eSjoerg       cursor.kind <= CXCursor_LastInvalid)
150306f32e7eSjoerg     return 1;
150406f32e7eSjoerg 
150506f32e7eSjoerg   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
150606f32e7eSjoerg   if (!setImpl)
150706f32e7eSjoerg     return 1;
150806f32e7eSjoerg   unsigned &entry = (*setImpl)[cursor];
150906f32e7eSjoerg   unsigned flag = entry == 0 ? 1 : 0;
151006f32e7eSjoerg   entry = 1;
151106f32e7eSjoerg   return flag;
151206f32e7eSjoerg }
151306f32e7eSjoerg 
clang_getCursorCompletionString(CXCursor cursor)151406f32e7eSjoerg CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
151506f32e7eSjoerg   enum CXCursorKind kind = clang_getCursorKind(cursor);
151606f32e7eSjoerg   if (clang_isDeclaration(kind)) {
151706f32e7eSjoerg     const Decl *decl = getCursorDecl(cursor);
151806f32e7eSjoerg     if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
151906f32e7eSjoerg       ASTUnit *unit = getCursorASTUnit(cursor);
152006f32e7eSjoerg       CodeCompletionResult Result(namedDecl, CCP_Declaration);
1521*13fbcb42Sjoerg       CodeCompletionString *String = Result.CreateCodeCompletionString(
1522*13fbcb42Sjoerg           unit->getASTContext(), unit->getPreprocessor(),
152306f32e7eSjoerg           CodeCompletionContext::CCC_Other,
152406f32e7eSjoerg           unit->getCodeCompletionTUInfo().getAllocator(),
1525*13fbcb42Sjoerg           unit->getCodeCompletionTUInfo(), true);
152606f32e7eSjoerg       return String;
152706f32e7eSjoerg     }
152806f32e7eSjoerg   } else if (kind == CXCursor_MacroDefinition) {
152906f32e7eSjoerg     const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
153006f32e7eSjoerg     const IdentifierInfo *Macro = definition->getName();
153106f32e7eSjoerg     ASTUnit *unit = getCursorASTUnit(cursor);
153206f32e7eSjoerg     CodeCompletionResult Result(
153306f32e7eSjoerg         Macro,
153406f32e7eSjoerg         unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
153506f32e7eSjoerg     CodeCompletionString *String = Result.CreateCodeCompletionString(
153606f32e7eSjoerg         unit->getASTContext(), unit->getPreprocessor(),
153706f32e7eSjoerg         CodeCompletionContext::CCC_Other,
153806f32e7eSjoerg         unit->getCodeCompletionTUInfo().getAllocator(),
153906f32e7eSjoerg         unit->getCodeCompletionTUInfo(), false);
154006f32e7eSjoerg     return String;
154106f32e7eSjoerg   }
154206f32e7eSjoerg   return nullptr;
154306f32e7eSjoerg }
154406f32e7eSjoerg 
154506f32e7eSjoerg namespace {
154606f32e7eSjoerg struct OverridenCursorsPool {
154706f32e7eSjoerg   typedef SmallVector<CXCursor, 2> CursorVec;
154806f32e7eSjoerg   std::vector<CursorVec *> AllCursors;
154906f32e7eSjoerg   std::vector<CursorVec *> AvailableCursors;
155006f32e7eSjoerg 
~OverridenCursorsPool__anon04f458790111::OverridenCursorsPool155106f32e7eSjoerg   ~OverridenCursorsPool() {
155206f32e7eSjoerg     for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1553*13fbcb42Sjoerg                                             E = AllCursors.end();
1554*13fbcb42Sjoerg          I != E; ++I) {
155506f32e7eSjoerg       delete *I;
155606f32e7eSjoerg     }
155706f32e7eSjoerg   }
155806f32e7eSjoerg };
1559*13fbcb42Sjoerg } // namespace
156006f32e7eSjoerg 
createOverridenCXCursorsPool()156106f32e7eSjoerg void *cxcursor::createOverridenCXCursorsPool() {
156206f32e7eSjoerg   return new OverridenCursorsPool();
156306f32e7eSjoerg }
156406f32e7eSjoerg 
disposeOverridenCXCursorsPool(void * pool)156506f32e7eSjoerg void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
156606f32e7eSjoerg   delete static_cast<OverridenCursorsPool *>(pool);
156706f32e7eSjoerg }
156806f32e7eSjoerg 
clang_getOverriddenCursors(CXCursor cursor,CXCursor ** overridden,unsigned * num_overridden)1569*13fbcb42Sjoerg void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
157006f32e7eSjoerg                                 unsigned *num_overridden) {
157106f32e7eSjoerg   if (overridden)
157206f32e7eSjoerg     *overridden = nullptr;
157306f32e7eSjoerg   if (num_overridden)
157406f32e7eSjoerg     *num_overridden = 0;
157506f32e7eSjoerg 
157606f32e7eSjoerg   CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
157706f32e7eSjoerg 
157806f32e7eSjoerg   if (!overridden || !num_overridden || !TU)
157906f32e7eSjoerg     return;
158006f32e7eSjoerg 
158106f32e7eSjoerg   if (!clang_isDeclaration(cursor.kind))
158206f32e7eSjoerg     return;
158306f32e7eSjoerg 
158406f32e7eSjoerg   OverridenCursorsPool &pool =
158506f32e7eSjoerg       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
158606f32e7eSjoerg 
158706f32e7eSjoerg   OverridenCursorsPool::CursorVec *Vec = nullptr;
158806f32e7eSjoerg 
158906f32e7eSjoerg   if (!pool.AvailableCursors.empty()) {
159006f32e7eSjoerg     Vec = pool.AvailableCursors.back();
159106f32e7eSjoerg     pool.AvailableCursors.pop_back();
1592*13fbcb42Sjoerg   } else {
159306f32e7eSjoerg     Vec = new OverridenCursorsPool::CursorVec();
159406f32e7eSjoerg     pool.AllCursors.push_back(Vec);
159506f32e7eSjoerg   }
159606f32e7eSjoerg 
159706f32e7eSjoerg   // Clear out the vector, but don't free the memory contents.  This
159806f32e7eSjoerg   // reduces malloc() traffic.
159906f32e7eSjoerg   Vec->clear();
160006f32e7eSjoerg 
160106f32e7eSjoerg   // Use the first entry to contain a back reference to the vector.
160206f32e7eSjoerg   // This is a complete hack.
160306f32e7eSjoerg   CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
160406f32e7eSjoerg   backRefCursor.data[0] = Vec;
160506f32e7eSjoerg   assert(cxcursor::getCursorTU(backRefCursor) == TU);
160606f32e7eSjoerg   Vec->push_back(backRefCursor);
160706f32e7eSjoerg 
160806f32e7eSjoerg   // Get the overridden cursors.
160906f32e7eSjoerg   cxcursor::getOverriddenCursors(cursor, *Vec);
161006f32e7eSjoerg 
161106f32e7eSjoerg   // Did we get any overridden cursors?  If not, return Vec to the pool
161206f32e7eSjoerg   // of available cursor vectors.
161306f32e7eSjoerg   if (Vec->size() == 1) {
161406f32e7eSjoerg     pool.AvailableCursors.push_back(Vec);
161506f32e7eSjoerg     return;
161606f32e7eSjoerg   }
161706f32e7eSjoerg 
161806f32e7eSjoerg   // Now tell the caller about the overridden cursors.
161906f32e7eSjoerg   assert(Vec->size() > 1);
162006f32e7eSjoerg   *overridden = &((*Vec)[1]);
162106f32e7eSjoerg   *num_overridden = Vec->size() - 1;
162206f32e7eSjoerg }
162306f32e7eSjoerg 
clang_disposeOverriddenCursors(CXCursor * overridden)162406f32e7eSjoerg void clang_disposeOverriddenCursors(CXCursor *overridden) {
162506f32e7eSjoerg   if (!overridden)
162606f32e7eSjoerg     return;
162706f32e7eSjoerg 
162806f32e7eSjoerg   // Use pointer arithmetic to get back the first faux entry
162906f32e7eSjoerg   // which has a back-reference to the TU and the vector.
163006f32e7eSjoerg   --overridden;
163106f32e7eSjoerg   OverridenCursorsPool::CursorVec *Vec =
163206f32e7eSjoerg       static_cast<OverridenCursorsPool::CursorVec *>(
163306f32e7eSjoerg           const_cast<void *>(overridden->data[0]));
163406f32e7eSjoerg   CXTranslationUnit TU = getCursorTU(*overridden);
163506f32e7eSjoerg 
163606f32e7eSjoerg   assert(Vec && TU);
163706f32e7eSjoerg 
163806f32e7eSjoerg   OverridenCursorsPool &pool =
163906f32e7eSjoerg       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
164006f32e7eSjoerg 
164106f32e7eSjoerg   pool.AvailableCursors.push_back(Vec);
164206f32e7eSjoerg }
164306f32e7eSjoerg 
clang_Cursor_isDynamicCall(CXCursor C)164406f32e7eSjoerg int clang_Cursor_isDynamicCall(CXCursor C) {
164506f32e7eSjoerg   const Expr *E = nullptr;
164606f32e7eSjoerg   if (clang_isExpression(C.kind))
164706f32e7eSjoerg     E = getCursorExpr(C);
164806f32e7eSjoerg   if (!E)
164906f32e7eSjoerg     return 0;
165006f32e7eSjoerg 
165106f32e7eSjoerg   if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
165206f32e7eSjoerg     if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
165306f32e7eSjoerg       return false;
165406f32e7eSjoerg     if (auto *RecE = dyn_cast<ObjCMessageExpr>(
165506f32e7eSjoerg             MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
165606f32e7eSjoerg       if (RecE->getMethodFamily() == OMF_alloc)
165706f32e7eSjoerg         return false;
165806f32e7eSjoerg     }
165906f32e7eSjoerg     return true;
166006f32e7eSjoerg   }
166106f32e7eSjoerg 
166206f32e7eSjoerg   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
166306f32e7eSjoerg     return !PropRefE->isSuperReceiver();
166406f32e7eSjoerg   }
166506f32e7eSjoerg 
166606f32e7eSjoerg   const MemberExpr *ME = nullptr;
166706f32e7eSjoerg   if (isa<MemberExpr>(E))
166806f32e7eSjoerg     ME = cast<MemberExpr>(E);
166906f32e7eSjoerg   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
167006f32e7eSjoerg     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
167106f32e7eSjoerg 
167206f32e7eSjoerg   if (ME) {
1673*13fbcb42Sjoerg     if (const CXXMethodDecl *MD =
1674*13fbcb42Sjoerg             dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
167506f32e7eSjoerg       return MD->isVirtual() &&
167606f32e7eSjoerg              ME->performsVirtualDispatch(
167706f32e7eSjoerg                  cxcursor::getCursorContext(C).getLangOpts());
167806f32e7eSjoerg   }
167906f32e7eSjoerg 
168006f32e7eSjoerg   return 0;
168106f32e7eSjoerg }
168206f32e7eSjoerg 
clang_Cursor_getReceiverType(CXCursor C)168306f32e7eSjoerg CXType clang_Cursor_getReceiverType(CXCursor C) {
168406f32e7eSjoerg   CXTranslationUnit TU = cxcursor::getCursorTU(C);
168506f32e7eSjoerg   const Expr *E = nullptr;
168606f32e7eSjoerg   if (clang_isExpression(C.kind))
168706f32e7eSjoerg     E = getCursorExpr(C);
168806f32e7eSjoerg 
168906f32e7eSjoerg   if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
169006f32e7eSjoerg     return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
169106f32e7eSjoerg 
169206f32e7eSjoerg   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
169306f32e7eSjoerg     return cxtype::MakeCXType(
169406f32e7eSjoerg         PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
169506f32e7eSjoerg   }
169606f32e7eSjoerg 
169706f32e7eSjoerg   const MemberExpr *ME = nullptr;
169806f32e7eSjoerg   if (isa<MemberExpr>(E))
169906f32e7eSjoerg     ME = cast<MemberExpr>(E);
170006f32e7eSjoerg   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
170106f32e7eSjoerg     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
170206f32e7eSjoerg 
170306f32e7eSjoerg   if (ME) {
170406f32e7eSjoerg     if (dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl())) {
170506f32e7eSjoerg       auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
170606f32e7eSjoerg       return cxtype::MakeCXType(receiverTy, TU);
170706f32e7eSjoerg     }
170806f32e7eSjoerg   }
170906f32e7eSjoerg 
171006f32e7eSjoerg   return cxtype::MakeCXType(QualType(), TU);
171106f32e7eSjoerg }
1712