xref: /netbsd-src/external/apache2/llvm/dist/clang/tools/libclang/CXCursor.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This file defines routines for manipulating CXCursors. It should be the
107330f729Sjoerg // only file that has internal knowledge of the encoding of the data in
117330f729Sjoerg // CXCursor.
127330f729Sjoerg //
137330f729Sjoerg //===----------------------------------------------------------------------===//
147330f729Sjoerg 
157330f729Sjoerg #include "CXCursor.h"
167330f729Sjoerg #include "CXString.h"
17*e038c9c4Sjoerg #include "CXTranslationUnit.h"
187330f729Sjoerg #include "CXType.h"
197330f729Sjoerg #include "clang-c/Index.h"
207330f729Sjoerg #include "clang/AST/Attr.h"
217330f729Sjoerg #include "clang/AST/Decl.h"
227330f729Sjoerg #include "clang/AST/DeclCXX.h"
237330f729Sjoerg #include "clang/AST/DeclObjC.h"
247330f729Sjoerg #include "clang/AST/DeclTemplate.h"
257330f729Sjoerg #include "clang/AST/Expr.h"
267330f729Sjoerg #include "clang/AST/ExprCXX.h"
277330f729Sjoerg #include "clang/AST/ExprObjC.h"
287330f729Sjoerg #include "clang/Frontend/ASTUnit.h"
297330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
307330f729Sjoerg 
317330f729Sjoerg using namespace clang;
327330f729Sjoerg using namespace cxcursor;
337330f729Sjoerg 
MakeCXCursorInvalid(CXCursorKind K,CXTranslationUnit TU)347330f729Sjoerg CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
357330f729Sjoerg   assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
367330f729Sjoerg   CXCursor C = {K, 0, {nullptr, nullptr, TU}};
377330f729Sjoerg   return C;
387330f729Sjoerg }
397330f729Sjoerg 
GetCursorKind(const Attr * A)407330f729Sjoerg static CXCursorKind GetCursorKind(const Attr *A) {
417330f729Sjoerg   assert(A && "Invalid arguments!");
427330f729Sjoerg   switch (A->getKind()) {
43*e038c9c4Sjoerg   default:
44*e038c9c4Sjoerg     break;
45*e038c9c4Sjoerg   case attr::IBAction:
46*e038c9c4Sjoerg     return CXCursor_IBActionAttr;
47*e038c9c4Sjoerg   case attr::IBOutlet:
48*e038c9c4Sjoerg     return CXCursor_IBOutletAttr;
49*e038c9c4Sjoerg   case attr::IBOutletCollection:
50*e038c9c4Sjoerg     return CXCursor_IBOutletCollectionAttr;
51*e038c9c4Sjoerg   case attr::Final:
52*e038c9c4Sjoerg     return CXCursor_CXXFinalAttr;
53*e038c9c4Sjoerg   case attr::Override:
54*e038c9c4Sjoerg     return CXCursor_CXXOverrideAttr;
55*e038c9c4Sjoerg   case attr::Annotate:
56*e038c9c4Sjoerg     return CXCursor_AnnotateAttr;
57*e038c9c4Sjoerg   case attr::AsmLabel:
58*e038c9c4Sjoerg     return CXCursor_AsmLabelAttr;
59*e038c9c4Sjoerg   case attr::Packed:
60*e038c9c4Sjoerg     return CXCursor_PackedAttr;
61*e038c9c4Sjoerg   case attr::Pure:
62*e038c9c4Sjoerg     return CXCursor_PureAttr;
63*e038c9c4Sjoerg   case attr::Const:
64*e038c9c4Sjoerg     return CXCursor_ConstAttr;
65*e038c9c4Sjoerg   case attr::NoDuplicate:
66*e038c9c4Sjoerg     return CXCursor_NoDuplicateAttr;
67*e038c9c4Sjoerg   case attr::CUDAConstant:
68*e038c9c4Sjoerg     return CXCursor_CUDAConstantAttr;
69*e038c9c4Sjoerg   case attr::CUDADevice:
70*e038c9c4Sjoerg     return CXCursor_CUDADeviceAttr;
71*e038c9c4Sjoerg   case attr::CUDAGlobal:
72*e038c9c4Sjoerg     return CXCursor_CUDAGlobalAttr;
73*e038c9c4Sjoerg   case attr::CUDAHost:
74*e038c9c4Sjoerg     return CXCursor_CUDAHostAttr;
75*e038c9c4Sjoerg   case attr::CUDAShared:
76*e038c9c4Sjoerg     return CXCursor_CUDASharedAttr;
77*e038c9c4Sjoerg   case attr::Visibility:
78*e038c9c4Sjoerg     return CXCursor_VisibilityAttr;
79*e038c9c4Sjoerg   case attr::DLLExport:
80*e038c9c4Sjoerg     return CXCursor_DLLExport;
81*e038c9c4Sjoerg   case attr::DLLImport:
82*e038c9c4Sjoerg     return CXCursor_DLLImport;
83*e038c9c4Sjoerg   case attr::NSReturnsRetained:
84*e038c9c4Sjoerg     return CXCursor_NSReturnsRetained;
85*e038c9c4Sjoerg   case attr::NSReturnsNotRetained:
86*e038c9c4Sjoerg     return CXCursor_NSReturnsNotRetained;
87*e038c9c4Sjoerg   case attr::NSReturnsAutoreleased:
88*e038c9c4Sjoerg     return CXCursor_NSReturnsAutoreleased;
89*e038c9c4Sjoerg   case attr::NSConsumesSelf:
90*e038c9c4Sjoerg     return CXCursor_NSConsumesSelf;
91*e038c9c4Sjoerg   case attr::NSConsumed:
92*e038c9c4Sjoerg     return CXCursor_NSConsumed;
93*e038c9c4Sjoerg   case attr::ObjCException:
94*e038c9c4Sjoerg     return CXCursor_ObjCException;
95*e038c9c4Sjoerg   case attr::ObjCNSObject:
96*e038c9c4Sjoerg     return CXCursor_ObjCNSObject;
97*e038c9c4Sjoerg   case attr::ObjCIndependentClass:
98*e038c9c4Sjoerg     return CXCursor_ObjCIndependentClass;
99*e038c9c4Sjoerg   case attr::ObjCPreciseLifetime:
100*e038c9c4Sjoerg     return CXCursor_ObjCPreciseLifetime;
101*e038c9c4Sjoerg   case attr::ObjCReturnsInnerPointer:
102*e038c9c4Sjoerg     return CXCursor_ObjCReturnsInnerPointer;
103*e038c9c4Sjoerg   case attr::ObjCRequiresSuper:
104*e038c9c4Sjoerg     return CXCursor_ObjCRequiresSuper;
105*e038c9c4Sjoerg   case attr::ObjCRootClass:
106*e038c9c4Sjoerg     return CXCursor_ObjCRootClass;
107*e038c9c4Sjoerg   case attr::ObjCSubclassingRestricted:
108*e038c9c4Sjoerg     return CXCursor_ObjCSubclassingRestricted;
109*e038c9c4Sjoerg   case attr::ObjCExplicitProtocolImpl:
110*e038c9c4Sjoerg     return CXCursor_ObjCExplicitProtocolImpl;
111*e038c9c4Sjoerg   case attr::ObjCDesignatedInitializer:
112*e038c9c4Sjoerg     return CXCursor_ObjCDesignatedInitializer;
113*e038c9c4Sjoerg   case attr::ObjCRuntimeVisible:
114*e038c9c4Sjoerg     return CXCursor_ObjCRuntimeVisible;
115*e038c9c4Sjoerg   case attr::ObjCBoxable:
116*e038c9c4Sjoerg     return CXCursor_ObjCBoxable;
117*e038c9c4Sjoerg   case attr::FlagEnum:
118*e038c9c4Sjoerg     return CXCursor_FlagEnum;
119*e038c9c4Sjoerg   case attr::Convergent:
120*e038c9c4Sjoerg     return CXCursor_ConvergentAttr;
121*e038c9c4Sjoerg   case attr::WarnUnused:
122*e038c9c4Sjoerg     return CXCursor_WarnUnusedAttr;
123*e038c9c4Sjoerg   case attr::WarnUnusedResult:
124*e038c9c4Sjoerg     return CXCursor_WarnUnusedResultAttr;
125*e038c9c4Sjoerg   case attr::Aligned:
126*e038c9c4Sjoerg     return CXCursor_AlignedAttr;
1277330f729Sjoerg   }
1287330f729Sjoerg 
1297330f729Sjoerg   return CXCursor_UnexposedAttr;
1307330f729Sjoerg }
1317330f729Sjoerg 
MakeCXCursor(const Attr * A,const Decl * Parent,CXTranslationUnit TU)1327330f729Sjoerg CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
1337330f729Sjoerg                                 CXTranslationUnit TU) {
1347330f729Sjoerg   assert(A && Parent && TU && "Invalid arguments!");
1357330f729Sjoerg   CXCursor C = {GetCursorKind(A), 0, {Parent, A, TU}};
1367330f729Sjoerg   return C;
1377330f729Sjoerg }
1387330f729Sjoerg 
MakeCXCursor(const Decl * D,CXTranslationUnit TU,SourceRange RegionOfInterest,bool FirstInDeclGroup)1397330f729Sjoerg CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
1407330f729Sjoerg                                 SourceRange RegionOfInterest,
1417330f729Sjoerg                                 bool FirstInDeclGroup) {
1427330f729Sjoerg   assert(D && TU && "Invalid arguments!");
1437330f729Sjoerg 
1447330f729Sjoerg   CXCursorKind K = getCursorKindForDecl(D);
1457330f729Sjoerg 
1467330f729Sjoerg   if (K == CXCursor_ObjCClassMethodDecl ||
1477330f729Sjoerg       K == CXCursor_ObjCInstanceMethodDecl) {
1487330f729Sjoerg     int SelectorIdIndex = -1;
1497330f729Sjoerg     // Check if cursor points to a selector id.
1507330f729Sjoerg     if (RegionOfInterest.isValid() &&
1517330f729Sjoerg         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
1527330f729Sjoerg       SmallVector<SourceLocation, 16> SelLocs;
1537330f729Sjoerg       cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
1547330f729Sjoerg       SmallVectorImpl<SourceLocation>::iterator I =
1557330f729Sjoerg           llvm::find(SelLocs, RegionOfInterest.getBegin());
1567330f729Sjoerg       if (I != SelLocs.end())
1577330f729Sjoerg         SelectorIdIndex = I - SelLocs.begin();
1587330f729Sjoerg     }
159*e038c9c4Sjoerg     CXCursor C = {K,
160*e038c9c4Sjoerg                   SelectorIdIndex,
1617330f729Sjoerg                   {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
1627330f729Sjoerg     return C;
1637330f729Sjoerg   }
1647330f729Sjoerg 
1657330f729Sjoerg   CXCursor C = {K, 0, {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
1667330f729Sjoerg   return C;
1677330f729Sjoerg }
1687330f729Sjoerg 
MakeCXCursor(const Stmt * S,const Decl * Parent,CXTranslationUnit TU,SourceRange RegionOfInterest)1697330f729Sjoerg CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
1707330f729Sjoerg                                 CXTranslationUnit TU,
1717330f729Sjoerg                                 SourceRange RegionOfInterest) {
1727330f729Sjoerg   assert(S && TU && "Invalid arguments!");
1737330f729Sjoerg   CXCursorKind K = CXCursor_NotImplemented;
1747330f729Sjoerg 
1757330f729Sjoerg   switch (S->getStmtClass()) {
1767330f729Sjoerg   case Stmt::NoStmtClass:
1777330f729Sjoerg     break;
1787330f729Sjoerg 
1797330f729Sjoerg   case Stmt::CaseStmtClass:
1807330f729Sjoerg     K = CXCursor_CaseStmt;
1817330f729Sjoerg     break;
1827330f729Sjoerg 
1837330f729Sjoerg   case Stmt::DefaultStmtClass:
1847330f729Sjoerg     K = CXCursor_DefaultStmt;
1857330f729Sjoerg     break;
1867330f729Sjoerg 
1877330f729Sjoerg   case Stmt::IfStmtClass:
1887330f729Sjoerg     K = CXCursor_IfStmt;
1897330f729Sjoerg     break;
1907330f729Sjoerg 
1917330f729Sjoerg   case Stmt::SwitchStmtClass:
1927330f729Sjoerg     K = CXCursor_SwitchStmt;
1937330f729Sjoerg     break;
1947330f729Sjoerg 
1957330f729Sjoerg   case Stmt::WhileStmtClass:
1967330f729Sjoerg     K = CXCursor_WhileStmt;
1977330f729Sjoerg     break;
1987330f729Sjoerg 
1997330f729Sjoerg   case Stmt::DoStmtClass:
2007330f729Sjoerg     K = CXCursor_DoStmt;
2017330f729Sjoerg     break;
2027330f729Sjoerg 
2037330f729Sjoerg   case Stmt::ForStmtClass:
2047330f729Sjoerg     K = CXCursor_ForStmt;
2057330f729Sjoerg     break;
2067330f729Sjoerg 
2077330f729Sjoerg   case Stmt::GotoStmtClass:
2087330f729Sjoerg     K = CXCursor_GotoStmt;
2097330f729Sjoerg     break;
2107330f729Sjoerg 
2117330f729Sjoerg   case Stmt::IndirectGotoStmtClass:
2127330f729Sjoerg     K = CXCursor_IndirectGotoStmt;
2137330f729Sjoerg     break;
2147330f729Sjoerg 
2157330f729Sjoerg   case Stmt::ContinueStmtClass:
2167330f729Sjoerg     K = CXCursor_ContinueStmt;
2177330f729Sjoerg     break;
2187330f729Sjoerg 
2197330f729Sjoerg   case Stmt::BreakStmtClass:
2207330f729Sjoerg     K = CXCursor_BreakStmt;
2217330f729Sjoerg     break;
2227330f729Sjoerg 
2237330f729Sjoerg   case Stmt::ReturnStmtClass:
2247330f729Sjoerg     K = CXCursor_ReturnStmt;
2257330f729Sjoerg     break;
2267330f729Sjoerg 
2277330f729Sjoerg   case Stmt::GCCAsmStmtClass:
2287330f729Sjoerg     K = CXCursor_GCCAsmStmt;
2297330f729Sjoerg     break;
2307330f729Sjoerg 
2317330f729Sjoerg   case Stmt::MSAsmStmtClass:
2327330f729Sjoerg     K = CXCursor_MSAsmStmt;
2337330f729Sjoerg     break;
2347330f729Sjoerg 
2357330f729Sjoerg   case Stmt::ObjCAtTryStmtClass:
2367330f729Sjoerg     K = CXCursor_ObjCAtTryStmt;
2377330f729Sjoerg     break;
2387330f729Sjoerg 
2397330f729Sjoerg   case Stmt::ObjCAtCatchStmtClass:
2407330f729Sjoerg     K = CXCursor_ObjCAtCatchStmt;
2417330f729Sjoerg     break;
2427330f729Sjoerg 
2437330f729Sjoerg   case Stmt::ObjCAtFinallyStmtClass:
2447330f729Sjoerg     K = CXCursor_ObjCAtFinallyStmt;
2457330f729Sjoerg     break;
2467330f729Sjoerg 
2477330f729Sjoerg   case Stmt::ObjCAtThrowStmtClass:
2487330f729Sjoerg     K = CXCursor_ObjCAtThrowStmt;
2497330f729Sjoerg     break;
2507330f729Sjoerg 
2517330f729Sjoerg   case Stmt::ObjCAtSynchronizedStmtClass:
2527330f729Sjoerg     K = CXCursor_ObjCAtSynchronizedStmt;
2537330f729Sjoerg     break;
2547330f729Sjoerg 
2557330f729Sjoerg   case Stmt::ObjCAutoreleasePoolStmtClass:
2567330f729Sjoerg     K = CXCursor_ObjCAutoreleasePoolStmt;
2577330f729Sjoerg     break;
2587330f729Sjoerg 
2597330f729Sjoerg   case Stmt::ObjCForCollectionStmtClass:
2607330f729Sjoerg     K = CXCursor_ObjCForCollectionStmt;
2617330f729Sjoerg     break;
2627330f729Sjoerg 
2637330f729Sjoerg   case Stmt::CXXCatchStmtClass:
2647330f729Sjoerg     K = CXCursor_CXXCatchStmt;
2657330f729Sjoerg     break;
2667330f729Sjoerg 
2677330f729Sjoerg   case Stmt::CXXTryStmtClass:
2687330f729Sjoerg     K = CXCursor_CXXTryStmt;
2697330f729Sjoerg     break;
2707330f729Sjoerg 
2717330f729Sjoerg   case Stmt::CXXForRangeStmtClass:
2727330f729Sjoerg     K = CXCursor_CXXForRangeStmt;
2737330f729Sjoerg     break;
2747330f729Sjoerg 
2757330f729Sjoerg   case Stmt::SEHTryStmtClass:
2767330f729Sjoerg     K = CXCursor_SEHTryStmt;
2777330f729Sjoerg     break;
2787330f729Sjoerg 
2797330f729Sjoerg   case Stmt::SEHExceptStmtClass:
2807330f729Sjoerg     K = CXCursor_SEHExceptStmt;
2817330f729Sjoerg     break;
2827330f729Sjoerg 
2837330f729Sjoerg   case Stmt::SEHFinallyStmtClass:
2847330f729Sjoerg     K = CXCursor_SEHFinallyStmt;
2857330f729Sjoerg     break;
2867330f729Sjoerg 
2877330f729Sjoerg   case Stmt::SEHLeaveStmtClass:
2887330f729Sjoerg     K = CXCursor_SEHLeaveStmt;
2897330f729Sjoerg     break;
2907330f729Sjoerg 
2917330f729Sjoerg   case Stmt::CoroutineBodyStmtClass:
2927330f729Sjoerg   case Stmt::CoreturnStmtClass:
2937330f729Sjoerg     K = CXCursor_UnexposedStmt;
2947330f729Sjoerg     break;
2957330f729Sjoerg 
2967330f729Sjoerg   case Stmt::ArrayTypeTraitExprClass:
2977330f729Sjoerg   case Stmt::AsTypeExprClass:
2987330f729Sjoerg   case Stmt::AtomicExprClass:
2997330f729Sjoerg   case Stmt::BinaryConditionalOperatorClass:
3007330f729Sjoerg   case Stmt::TypeTraitExprClass:
3017330f729Sjoerg   case Stmt::CoawaitExprClass:
3027330f729Sjoerg   case Stmt::ConceptSpecializationExprClass:
303*e038c9c4Sjoerg   case Stmt::RequiresExprClass:
3047330f729Sjoerg   case Stmt::DependentCoawaitExprClass:
3057330f729Sjoerg   case Stmt::CoyieldExprClass:
3067330f729Sjoerg   case Stmt::CXXBindTemporaryExprClass:
3077330f729Sjoerg   case Stmt::CXXDefaultArgExprClass:
3087330f729Sjoerg   case Stmt::CXXDefaultInitExprClass:
3097330f729Sjoerg   case Stmt::CXXFoldExprClass:
3107330f729Sjoerg   case Stmt::CXXRewrittenBinaryOperatorClass:
3117330f729Sjoerg   case Stmt::CXXStdInitializerListExprClass:
3127330f729Sjoerg   case Stmt::CXXScalarValueInitExprClass:
3137330f729Sjoerg   case Stmt::CXXUuidofExprClass:
3147330f729Sjoerg   case Stmt::ChooseExprClass:
3157330f729Sjoerg   case Stmt::DesignatedInitExprClass:
3167330f729Sjoerg   case Stmt::DesignatedInitUpdateExprClass:
3177330f729Sjoerg   case Stmt::ArrayInitLoopExprClass:
3187330f729Sjoerg   case Stmt::ArrayInitIndexExprClass:
3197330f729Sjoerg   case Stmt::ExprWithCleanupsClass:
3207330f729Sjoerg   case Stmt::ExpressionTraitExprClass:
3217330f729Sjoerg   case Stmt::ExtVectorElementExprClass:
3227330f729Sjoerg   case Stmt::ImplicitCastExprClass:
3237330f729Sjoerg   case Stmt::ImplicitValueInitExprClass:
3247330f729Sjoerg   case Stmt::NoInitExprClass:
3257330f729Sjoerg   case Stmt::MaterializeTemporaryExprClass:
3267330f729Sjoerg   case Stmt::ObjCIndirectCopyRestoreExprClass:
3277330f729Sjoerg   case Stmt::OffsetOfExprClass:
3287330f729Sjoerg   case Stmt::ParenListExprClass:
3297330f729Sjoerg   case Stmt::PredefinedExprClass:
3307330f729Sjoerg   case Stmt::ShuffleVectorExprClass:
3317330f729Sjoerg   case Stmt::SourceLocExprClass:
3327330f729Sjoerg   case Stmt::ConvertVectorExprClass:
3337330f729Sjoerg   case Stmt::VAArgExprClass:
3347330f729Sjoerg   case Stmt::ObjCArrayLiteralClass:
3357330f729Sjoerg   case Stmt::ObjCDictionaryLiteralClass:
3367330f729Sjoerg   case Stmt::ObjCBoxedExprClass:
3377330f729Sjoerg   case Stmt::ObjCSubscriptRefExprClass:
338*e038c9c4Sjoerg   case Stmt::RecoveryExprClass:
3397330f729Sjoerg     K = CXCursor_UnexposedExpr;
3407330f729Sjoerg     break;
3417330f729Sjoerg 
3427330f729Sjoerg   case Stmt::OpaqueValueExprClass:
3437330f729Sjoerg     if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
3447330f729Sjoerg       return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
3457330f729Sjoerg     K = CXCursor_UnexposedExpr;
3467330f729Sjoerg     break;
3477330f729Sjoerg 
3487330f729Sjoerg   case Stmt::PseudoObjectExprClass:
349*e038c9c4Sjoerg     return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
350*e038c9c4Sjoerg                         TU, RegionOfInterest);
3517330f729Sjoerg 
3527330f729Sjoerg   case Stmt::CompoundStmtClass:
3537330f729Sjoerg     K = CXCursor_CompoundStmt;
3547330f729Sjoerg     break;
3557330f729Sjoerg 
3567330f729Sjoerg   case Stmt::NullStmtClass:
3577330f729Sjoerg     K = CXCursor_NullStmt;
3587330f729Sjoerg     break;
3597330f729Sjoerg 
3607330f729Sjoerg   case Stmt::LabelStmtClass:
3617330f729Sjoerg     K = CXCursor_LabelStmt;
3627330f729Sjoerg     break;
3637330f729Sjoerg 
3647330f729Sjoerg   case Stmt::AttributedStmtClass:
3657330f729Sjoerg     K = CXCursor_UnexposedStmt;
3667330f729Sjoerg     break;
3677330f729Sjoerg 
3687330f729Sjoerg   case Stmt::DeclStmtClass:
3697330f729Sjoerg     K = CXCursor_DeclStmt;
3707330f729Sjoerg     break;
3717330f729Sjoerg 
3727330f729Sjoerg   case Stmt::CapturedStmtClass:
3737330f729Sjoerg     K = CXCursor_UnexposedStmt;
3747330f729Sjoerg     break;
3757330f729Sjoerg 
3767330f729Sjoerg   case Stmt::IntegerLiteralClass:
3777330f729Sjoerg     K = CXCursor_IntegerLiteral;
3787330f729Sjoerg     break;
3797330f729Sjoerg 
3807330f729Sjoerg   case Stmt::FixedPointLiteralClass:
3817330f729Sjoerg     K = CXCursor_FixedPointLiteral;
3827330f729Sjoerg     break;
3837330f729Sjoerg 
3847330f729Sjoerg   case Stmt::FloatingLiteralClass:
3857330f729Sjoerg     K = CXCursor_FloatingLiteral;
3867330f729Sjoerg     break;
3877330f729Sjoerg 
3887330f729Sjoerg   case Stmt::ImaginaryLiteralClass:
3897330f729Sjoerg     K = CXCursor_ImaginaryLiteral;
3907330f729Sjoerg     break;
3917330f729Sjoerg 
3927330f729Sjoerg   case Stmt::StringLiteralClass:
3937330f729Sjoerg     K = CXCursor_StringLiteral;
3947330f729Sjoerg     break;
3957330f729Sjoerg 
3967330f729Sjoerg   case Stmt::CharacterLiteralClass:
3977330f729Sjoerg     K = CXCursor_CharacterLiteral;
3987330f729Sjoerg     break;
3997330f729Sjoerg 
4007330f729Sjoerg   case Stmt::ConstantExprClass:
401*e038c9c4Sjoerg     return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
402*e038c9c4Sjoerg                         RegionOfInterest);
4037330f729Sjoerg 
4047330f729Sjoerg   case Stmt::ParenExprClass:
4057330f729Sjoerg     K = CXCursor_ParenExpr;
4067330f729Sjoerg     break;
4077330f729Sjoerg 
4087330f729Sjoerg   case Stmt::UnaryOperatorClass:
4097330f729Sjoerg     K = CXCursor_UnaryOperator;
4107330f729Sjoerg     break;
4117330f729Sjoerg 
4127330f729Sjoerg   case Stmt::UnaryExprOrTypeTraitExprClass:
4137330f729Sjoerg   case Stmt::CXXNoexceptExprClass:
4147330f729Sjoerg     K = CXCursor_UnaryExpr;
4157330f729Sjoerg     break;
4167330f729Sjoerg 
4177330f729Sjoerg   case Stmt::MSPropertySubscriptExprClass:
4187330f729Sjoerg   case Stmt::ArraySubscriptExprClass:
4197330f729Sjoerg     K = CXCursor_ArraySubscriptExpr;
4207330f729Sjoerg     break;
4217330f729Sjoerg 
422*e038c9c4Sjoerg   case Stmt::MatrixSubscriptExprClass:
423*e038c9c4Sjoerg     // TODO: add support for MatrixSubscriptExpr.
424*e038c9c4Sjoerg     K = CXCursor_UnexposedExpr;
425*e038c9c4Sjoerg     break;
426*e038c9c4Sjoerg 
4277330f729Sjoerg   case Stmt::OMPArraySectionExprClass:
4287330f729Sjoerg     K = CXCursor_OMPArraySectionExpr;
4297330f729Sjoerg     break;
4307330f729Sjoerg 
431*e038c9c4Sjoerg   case Stmt::OMPArrayShapingExprClass:
432*e038c9c4Sjoerg     K = CXCursor_OMPArrayShapingExpr;
433*e038c9c4Sjoerg     break;
434*e038c9c4Sjoerg 
435*e038c9c4Sjoerg   case Stmt::OMPIteratorExprClass:
436*e038c9c4Sjoerg     K = CXCursor_OMPIteratorExpr;
437*e038c9c4Sjoerg     break;
438*e038c9c4Sjoerg 
4397330f729Sjoerg   case Stmt::BinaryOperatorClass:
4407330f729Sjoerg     K = CXCursor_BinaryOperator;
4417330f729Sjoerg     break;
4427330f729Sjoerg 
4437330f729Sjoerg   case Stmt::CompoundAssignOperatorClass:
4447330f729Sjoerg     K = CXCursor_CompoundAssignOperator;
4457330f729Sjoerg     break;
4467330f729Sjoerg 
4477330f729Sjoerg   case Stmt::ConditionalOperatorClass:
4487330f729Sjoerg     K = CXCursor_ConditionalOperator;
4497330f729Sjoerg     break;
4507330f729Sjoerg 
4517330f729Sjoerg   case Stmt::CStyleCastExprClass:
4527330f729Sjoerg     K = CXCursor_CStyleCastExpr;
4537330f729Sjoerg     break;
4547330f729Sjoerg 
4557330f729Sjoerg   case Stmt::CompoundLiteralExprClass:
4567330f729Sjoerg     K = CXCursor_CompoundLiteralExpr;
4577330f729Sjoerg     break;
4587330f729Sjoerg 
4597330f729Sjoerg   case Stmt::InitListExprClass:
4607330f729Sjoerg     K = CXCursor_InitListExpr;
4617330f729Sjoerg     break;
4627330f729Sjoerg 
4637330f729Sjoerg   case Stmt::AddrLabelExprClass:
4647330f729Sjoerg     K = CXCursor_AddrLabelExpr;
4657330f729Sjoerg     break;
4667330f729Sjoerg 
4677330f729Sjoerg   case Stmt::StmtExprClass:
4687330f729Sjoerg     K = CXCursor_StmtExpr;
4697330f729Sjoerg     break;
4707330f729Sjoerg 
4717330f729Sjoerg   case Stmt::GenericSelectionExprClass:
4727330f729Sjoerg     K = CXCursor_GenericSelectionExpr;
4737330f729Sjoerg     break;
4747330f729Sjoerg 
4757330f729Sjoerg   case Stmt::GNUNullExprClass:
4767330f729Sjoerg     K = CXCursor_GNUNullExpr;
4777330f729Sjoerg     break;
4787330f729Sjoerg 
4797330f729Sjoerg   case Stmt::CXXStaticCastExprClass:
4807330f729Sjoerg     K = CXCursor_CXXStaticCastExpr;
4817330f729Sjoerg     break;
4827330f729Sjoerg 
4837330f729Sjoerg   case Stmt::CXXDynamicCastExprClass:
4847330f729Sjoerg     K = CXCursor_CXXDynamicCastExpr;
4857330f729Sjoerg     break;
4867330f729Sjoerg 
4877330f729Sjoerg   case Stmt::CXXReinterpretCastExprClass:
4887330f729Sjoerg     K = CXCursor_CXXReinterpretCastExpr;
4897330f729Sjoerg     break;
4907330f729Sjoerg 
4917330f729Sjoerg   case Stmt::CXXConstCastExprClass:
4927330f729Sjoerg     K = CXCursor_CXXConstCastExpr;
4937330f729Sjoerg     break;
4947330f729Sjoerg 
4957330f729Sjoerg   case Stmt::CXXFunctionalCastExprClass:
4967330f729Sjoerg     K = CXCursor_CXXFunctionalCastExpr;
4977330f729Sjoerg     break;
4987330f729Sjoerg 
499*e038c9c4Sjoerg   case Stmt::CXXAddrspaceCastExprClass:
500*e038c9c4Sjoerg     K = CXCursor_CXXAddrspaceCastExpr;
501*e038c9c4Sjoerg     break;
502*e038c9c4Sjoerg 
5037330f729Sjoerg   case Stmt::CXXTypeidExprClass:
5047330f729Sjoerg     K = CXCursor_CXXTypeidExpr;
5057330f729Sjoerg     break;
5067330f729Sjoerg 
5077330f729Sjoerg   case Stmt::CXXBoolLiteralExprClass:
5087330f729Sjoerg     K = CXCursor_CXXBoolLiteralExpr;
5097330f729Sjoerg     break;
5107330f729Sjoerg 
5117330f729Sjoerg   case Stmt::CXXNullPtrLiteralExprClass:
5127330f729Sjoerg     K = CXCursor_CXXNullPtrLiteralExpr;
5137330f729Sjoerg     break;
5147330f729Sjoerg 
5157330f729Sjoerg   case Stmt::CXXThisExprClass:
5167330f729Sjoerg     K = CXCursor_CXXThisExpr;
5177330f729Sjoerg     break;
5187330f729Sjoerg 
5197330f729Sjoerg   case Stmt::CXXThrowExprClass:
5207330f729Sjoerg     K = CXCursor_CXXThrowExpr;
5217330f729Sjoerg     break;
5227330f729Sjoerg 
5237330f729Sjoerg   case Stmt::CXXNewExprClass:
5247330f729Sjoerg     K = CXCursor_CXXNewExpr;
5257330f729Sjoerg     break;
5267330f729Sjoerg 
5277330f729Sjoerg   case Stmt::CXXDeleteExprClass:
5287330f729Sjoerg     K = CXCursor_CXXDeleteExpr;
5297330f729Sjoerg     break;
5307330f729Sjoerg 
5317330f729Sjoerg   case Stmt::ObjCStringLiteralClass:
5327330f729Sjoerg     K = CXCursor_ObjCStringLiteral;
5337330f729Sjoerg     break;
5347330f729Sjoerg 
5357330f729Sjoerg   case Stmt::ObjCEncodeExprClass:
5367330f729Sjoerg     K = CXCursor_ObjCEncodeExpr;
5377330f729Sjoerg     break;
5387330f729Sjoerg 
5397330f729Sjoerg   case Stmt::ObjCSelectorExprClass:
5407330f729Sjoerg     K = CXCursor_ObjCSelectorExpr;
5417330f729Sjoerg     break;
5427330f729Sjoerg 
5437330f729Sjoerg   case Stmt::ObjCProtocolExprClass:
5447330f729Sjoerg     K = CXCursor_ObjCProtocolExpr;
5457330f729Sjoerg     break;
5467330f729Sjoerg 
5477330f729Sjoerg   case Stmt::ObjCBoolLiteralExprClass:
5487330f729Sjoerg     K = CXCursor_ObjCBoolLiteralExpr;
5497330f729Sjoerg     break;
5507330f729Sjoerg 
5517330f729Sjoerg   case Stmt::ObjCAvailabilityCheckExprClass:
5527330f729Sjoerg     K = CXCursor_ObjCAvailabilityCheckExpr;
5537330f729Sjoerg     break;
5547330f729Sjoerg 
5557330f729Sjoerg   case Stmt::ObjCBridgedCastExprClass:
5567330f729Sjoerg     K = CXCursor_ObjCBridgedCastExpr;
5577330f729Sjoerg     break;
5587330f729Sjoerg 
5597330f729Sjoerg   case Stmt::BlockExprClass:
5607330f729Sjoerg     K = CXCursor_BlockExpr;
5617330f729Sjoerg     break;
5627330f729Sjoerg 
5637330f729Sjoerg   case Stmt::PackExpansionExprClass:
5647330f729Sjoerg     K = CXCursor_PackExpansionExpr;
5657330f729Sjoerg     break;
5667330f729Sjoerg 
5677330f729Sjoerg   case Stmt::SizeOfPackExprClass:
5687330f729Sjoerg     K = CXCursor_SizeOfPackExpr;
5697330f729Sjoerg     break;
5707330f729Sjoerg 
5717330f729Sjoerg   case Stmt::DeclRefExprClass:
572*e038c9c4Sjoerg     if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
573*e038c9c4Sjoerg             cast<DeclRefExpr>(S)->getDecl())) {
5747330f729Sjoerg       if (const ObjCMethodDecl *MD =
5757330f729Sjoerg               dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
5767330f729Sjoerg         if (MD->getSelfDecl() == IPD) {
5777330f729Sjoerg           K = CXCursor_ObjCSelfExpr;
5787330f729Sjoerg           break;
5797330f729Sjoerg         }
5807330f729Sjoerg       }
5817330f729Sjoerg     }
5827330f729Sjoerg 
5837330f729Sjoerg     K = CXCursor_DeclRefExpr;
5847330f729Sjoerg     break;
5857330f729Sjoerg 
5867330f729Sjoerg   case Stmt::DependentScopeDeclRefExprClass:
5877330f729Sjoerg   case Stmt::SubstNonTypeTemplateParmExprClass:
5887330f729Sjoerg   case Stmt::SubstNonTypeTemplateParmPackExprClass:
5897330f729Sjoerg   case Stmt::FunctionParmPackExprClass:
5907330f729Sjoerg   case Stmt::UnresolvedLookupExprClass:
5917330f729Sjoerg   case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
5927330f729Sjoerg     K = CXCursor_DeclRefExpr;
5937330f729Sjoerg     break;
5947330f729Sjoerg 
5957330f729Sjoerg   case Stmt::CXXDependentScopeMemberExprClass:
5967330f729Sjoerg   case Stmt::CXXPseudoDestructorExprClass:
5977330f729Sjoerg   case Stmt::MemberExprClass:
5987330f729Sjoerg   case Stmt::MSPropertyRefExprClass:
5997330f729Sjoerg   case Stmt::ObjCIsaExprClass:
6007330f729Sjoerg   case Stmt::ObjCIvarRefExprClass:
6017330f729Sjoerg   case Stmt::ObjCPropertyRefExprClass:
6027330f729Sjoerg   case Stmt::UnresolvedMemberExprClass:
6037330f729Sjoerg     K = CXCursor_MemberRefExpr;
6047330f729Sjoerg     break;
6057330f729Sjoerg 
6067330f729Sjoerg   case Stmt::CallExprClass:
6077330f729Sjoerg   case Stmt::CXXOperatorCallExprClass:
6087330f729Sjoerg   case Stmt::CXXMemberCallExprClass:
6097330f729Sjoerg   case Stmt::CUDAKernelCallExprClass:
6107330f729Sjoerg   case Stmt::CXXConstructExprClass:
6117330f729Sjoerg   case Stmt::CXXInheritedCtorInitExprClass:
6127330f729Sjoerg   case Stmt::CXXTemporaryObjectExprClass:
6137330f729Sjoerg   case Stmt::CXXUnresolvedConstructExprClass:
6147330f729Sjoerg   case Stmt::UserDefinedLiteralClass:
6157330f729Sjoerg     K = CXCursor_CallExpr;
6167330f729Sjoerg     break;
6177330f729Sjoerg 
6187330f729Sjoerg   case Stmt::LambdaExprClass:
6197330f729Sjoerg     K = CXCursor_LambdaExpr;
6207330f729Sjoerg     break;
6217330f729Sjoerg 
6227330f729Sjoerg   case Stmt::ObjCMessageExprClass: {
6237330f729Sjoerg     K = CXCursor_ObjCMessageExpr;
6247330f729Sjoerg     int SelectorIdIndex = -1;
6257330f729Sjoerg     // Check if cursor points to a selector id.
6267330f729Sjoerg     if (RegionOfInterest.isValid() &&
6277330f729Sjoerg         RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
6287330f729Sjoerg       SmallVector<SourceLocation, 16> SelLocs;
6297330f729Sjoerg       cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
6307330f729Sjoerg       SmallVectorImpl<SourceLocation>::iterator I =
6317330f729Sjoerg           llvm::find(SelLocs, RegionOfInterest.getBegin());
6327330f729Sjoerg       if (I != SelLocs.end())
6337330f729Sjoerg         SelectorIdIndex = I - SelLocs.begin();
6347330f729Sjoerg     }
6357330f729Sjoerg     CXCursor C = {K, 0, {Parent, S, TU}};
6367330f729Sjoerg     return getSelectorIdentifierCursor(SelectorIdIndex, C);
6377330f729Sjoerg   }
6387330f729Sjoerg 
6397330f729Sjoerg   case Stmt::MSDependentExistsStmtClass:
6407330f729Sjoerg     K = CXCursor_UnexposedStmt;
6417330f729Sjoerg     break;
642*e038c9c4Sjoerg   case Stmt::OMPCanonicalLoopClass:
643*e038c9c4Sjoerg     K = CXCursor_OMPCanonicalLoop;
644*e038c9c4Sjoerg     break;
6457330f729Sjoerg   case Stmt::OMPParallelDirectiveClass:
6467330f729Sjoerg     K = CXCursor_OMPParallelDirective;
6477330f729Sjoerg     break;
6487330f729Sjoerg   case Stmt::OMPSimdDirectiveClass:
6497330f729Sjoerg     K = CXCursor_OMPSimdDirective;
6507330f729Sjoerg     break;
651*e038c9c4Sjoerg   case Stmt::OMPTileDirectiveClass:
652*e038c9c4Sjoerg     K = CXCursor_OMPTileDirective;
653*e038c9c4Sjoerg     break;
6547330f729Sjoerg   case Stmt::OMPForDirectiveClass:
6557330f729Sjoerg     K = CXCursor_OMPForDirective;
6567330f729Sjoerg     break;
6577330f729Sjoerg   case Stmt::OMPForSimdDirectiveClass:
6587330f729Sjoerg     K = CXCursor_OMPForSimdDirective;
6597330f729Sjoerg     break;
6607330f729Sjoerg   case Stmt::OMPSectionsDirectiveClass:
6617330f729Sjoerg     K = CXCursor_OMPSectionsDirective;
6627330f729Sjoerg     break;
6637330f729Sjoerg   case Stmt::OMPSectionDirectiveClass:
6647330f729Sjoerg     K = CXCursor_OMPSectionDirective;
6657330f729Sjoerg     break;
6667330f729Sjoerg   case Stmt::OMPSingleDirectiveClass:
6677330f729Sjoerg     K = CXCursor_OMPSingleDirective;
6687330f729Sjoerg     break;
6697330f729Sjoerg   case Stmt::OMPMasterDirectiveClass:
6707330f729Sjoerg     K = CXCursor_OMPMasterDirective;
6717330f729Sjoerg     break;
6727330f729Sjoerg   case Stmt::OMPCriticalDirectiveClass:
6737330f729Sjoerg     K = CXCursor_OMPCriticalDirective;
6747330f729Sjoerg     break;
6757330f729Sjoerg   case Stmt::OMPParallelForDirectiveClass:
6767330f729Sjoerg     K = CXCursor_OMPParallelForDirective;
6777330f729Sjoerg     break;
6787330f729Sjoerg   case Stmt::OMPParallelForSimdDirectiveClass:
6797330f729Sjoerg     K = CXCursor_OMPParallelForSimdDirective;
6807330f729Sjoerg     break;
681*e038c9c4Sjoerg   case Stmt::OMPParallelMasterDirectiveClass:
682*e038c9c4Sjoerg     K = CXCursor_OMPParallelMasterDirective;
683*e038c9c4Sjoerg     break;
6847330f729Sjoerg   case Stmt::OMPParallelSectionsDirectiveClass:
6857330f729Sjoerg     K = CXCursor_OMPParallelSectionsDirective;
6867330f729Sjoerg     break;
6877330f729Sjoerg   case Stmt::OMPTaskDirectiveClass:
6887330f729Sjoerg     K = CXCursor_OMPTaskDirective;
6897330f729Sjoerg     break;
6907330f729Sjoerg   case Stmt::OMPTaskyieldDirectiveClass:
6917330f729Sjoerg     K = CXCursor_OMPTaskyieldDirective;
6927330f729Sjoerg     break;
6937330f729Sjoerg   case Stmt::OMPBarrierDirectiveClass:
6947330f729Sjoerg     K = CXCursor_OMPBarrierDirective;
6957330f729Sjoerg     break;
6967330f729Sjoerg   case Stmt::OMPTaskwaitDirectiveClass:
6977330f729Sjoerg     K = CXCursor_OMPTaskwaitDirective;
6987330f729Sjoerg     break;
6997330f729Sjoerg   case Stmt::OMPTaskgroupDirectiveClass:
7007330f729Sjoerg     K = CXCursor_OMPTaskgroupDirective;
7017330f729Sjoerg     break;
7027330f729Sjoerg   case Stmt::OMPFlushDirectiveClass:
7037330f729Sjoerg     K = CXCursor_OMPFlushDirective;
7047330f729Sjoerg     break;
705*e038c9c4Sjoerg   case Stmt::OMPDepobjDirectiveClass:
706*e038c9c4Sjoerg     K = CXCursor_OMPDepobjDirective;
707*e038c9c4Sjoerg     break;
708*e038c9c4Sjoerg   case Stmt::OMPScanDirectiveClass:
709*e038c9c4Sjoerg     K = CXCursor_OMPScanDirective;
710*e038c9c4Sjoerg     break;
7117330f729Sjoerg   case Stmt::OMPOrderedDirectiveClass:
7127330f729Sjoerg     K = CXCursor_OMPOrderedDirective;
7137330f729Sjoerg     break;
7147330f729Sjoerg   case Stmt::OMPAtomicDirectiveClass:
7157330f729Sjoerg     K = CXCursor_OMPAtomicDirective;
7167330f729Sjoerg     break;
7177330f729Sjoerg   case Stmt::OMPTargetDirectiveClass:
7187330f729Sjoerg     K = CXCursor_OMPTargetDirective;
7197330f729Sjoerg     break;
7207330f729Sjoerg   case Stmt::OMPTargetDataDirectiveClass:
7217330f729Sjoerg     K = CXCursor_OMPTargetDataDirective;
7227330f729Sjoerg     break;
7237330f729Sjoerg   case Stmt::OMPTargetEnterDataDirectiveClass:
7247330f729Sjoerg     K = CXCursor_OMPTargetEnterDataDirective;
7257330f729Sjoerg     break;
7267330f729Sjoerg   case Stmt::OMPTargetExitDataDirectiveClass:
7277330f729Sjoerg     K = CXCursor_OMPTargetExitDataDirective;
7287330f729Sjoerg     break;
7297330f729Sjoerg   case Stmt::OMPTargetParallelDirectiveClass:
7307330f729Sjoerg     K = CXCursor_OMPTargetParallelDirective;
7317330f729Sjoerg     break;
7327330f729Sjoerg   case Stmt::OMPTargetParallelForDirectiveClass:
7337330f729Sjoerg     K = CXCursor_OMPTargetParallelForDirective;
7347330f729Sjoerg     break;
7357330f729Sjoerg   case Stmt::OMPTargetUpdateDirectiveClass:
7367330f729Sjoerg     K = CXCursor_OMPTargetUpdateDirective;
7377330f729Sjoerg     break;
7387330f729Sjoerg   case Stmt::OMPTeamsDirectiveClass:
7397330f729Sjoerg     K = CXCursor_OMPTeamsDirective;
7407330f729Sjoerg     break;
7417330f729Sjoerg   case Stmt::OMPCancellationPointDirectiveClass:
7427330f729Sjoerg     K = CXCursor_OMPCancellationPointDirective;
7437330f729Sjoerg     break;
7447330f729Sjoerg   case Stmt::OMPCancelDirectiveClass:
7457330f729Sjoerg     K = CXCursor_OMPCancelDirective;
7467330f729Sjoerg     break;
7477330f729Sjoerg   case Stmt::OMPTaskLoopDirectiveClass:
7487330f729Sjoerg     K = CXCursor_OMPTaskLoopDirective;
7497330f729Sjoerg     break;
7507330f729Sjoerg   case Stmt::OMPTaskLoopSimdDirectiveClass:
7517330f729Sjoerg     K = CXCursor_OMPTaskLoopSimdDirective;
7527330f729Sjoerg     break;
7537330f729Sjoerg   case Stmt::OMPMasterTaskLoopDirectiveClass:
7547330f729Sjoerg     K = CXCursor_OMPMasterTaskLoopDirective;
7557330f729Sjoerg     break;
7567330f729Sjoerg   case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
7577330f729Sjoerg     K = CXCursor_OMPMasterTaskLoopSimdDirective;
7587330f729Sjoerg     break;
7597330f729Sjoerg   case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
7607330f729Sjoerg     K = CXCursor_OMPParallelMasterTaskLoopDirective;
7617330f729Sjoerg     break;
762*e038c9c4Sjoerg   case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
763*e038c9c4Sjoerg     K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
764*e038c9c4Sjoerg     break;
7657330f729Sjoerg   case Stmt::OMPDistributeDirectiveClass:
7667330f729Sjoerg     K = CXCursor_OMPDistributeDirective;
7677330f729Sjoerg     break;
7687330f729Sjoerg   case Stmt::OMPDistributeParallelForDirectiveClass:
7697330f729Sjoerg     K = CXCursor_OMPDistributeParallelForDirective;
7707330f729Sjoerg     break;
7717330f729Sjoerg   case Stmt::OMPDistributeParallelForSimdDirectiveClass:
7727330f729Sjoerg     K = CXCursor_OMPDistributeParallelForSimdDirective;
7737330f729Sjoerg     break;
7747330f729Sjoerg   case Stmt::OMPDistributeSimdDirectiveClass:
7757330f729Sjoerg     K = CXCursor_OMPDistributeSimdDirective;
7767330f729Sjoerg     break;
7777330f729Sjoerg   case Stmt::OMPTargetParallelForSimdDirectiveClass:
7787330f729Sjoerg     K = CXCursor_OMPTargetParallelForSimdDirective;
7797330f729Sjoerg     break;
7807330f729Sjoerg   case Stmt::OMPTargetSimdDirectiveClass:
7817330f729Sjoerg     K = CXCursor_OMPTargetSimdDirective;
7827330f729Sjoerg     break;
7837330f729Sjoerg   case Stmt::OMPTeamsDistributeDirectiveClass:
7847330f729Sjoerg     K = CXCursor_OMPTeamsDistributeDirective;
7857330f729Sjoerg     break;
7867330f729Sjoerg   case Stmt::OMPTeamsDistributeSimdDirectiveClass:
7877330f729Sjoerg     K = CXCursor_OMPTeamsDistributeSimdDirective;
7887330f729Sjoerg     break;
7897330f729Sjoerg   case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
7907330f729Sjoerg     K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
7917330f729Sjoerg     break;
7927330f729Sjoerg   case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
7937330f729Sjoerg     K = CXCursor_OMPTeamsDistributeParallelForDirective;
7947330f729Sjoerg     break;
7957330f729Sjoerg   case Stmt::OMPTargetTeamsDirectiveClass:
7967330f729Sjoerg     K = CXCursor_OMPTargetTeamsDirective;
7977330f729Sjoerg     break;
7987330f729Sjoerg   case Stmt::OMPTargetTeamsDistributeDirectiveClass:
7997330f729Sjoerg     K = CXCursor_OMPTargetTeamsDistributeDirective;
8007330f729Sjoerg     break;
8017330f729Sjoerg   case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
8027330f729Sjoerg     K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
8037330f729Sjoerg     break;
8047330f729Sjoerg   case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
8057330f729Sjoerg     K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
8067330f729Sjoerg     break;
8077330f729Sjoerg   case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
8087330f729Sjoerg     K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
8097330f729Sjoerg     break;
810*e038c9c4Sjoerg   case Stmt::OMPInteropDirectiveClass:
811*e038c9c4Sjoerg     K = CXCursor_OMPInteropDirective;
812*e038c9c4Sjoerg     break;
813*e038c9c4Sjoerg   case Stmt::OMPDispatchDirectiveClass:
814*e038c9c4Sjoerg     K = CXCursor_OMPDispatchDirective;
815*e038c9c4Sjoerg     break;
816*e038c9c4Sjoerg   case Stmt::OMPMaskedDirectiveClass:
817*e038c9c4Sjoerg     K = CXCursor_OMPMaskedDirective;
818*e038c9c4Sjoerg     break;
8197330f729Sjoerg   case Stmt::BuiltinBitCastExprClass:
8207330f729Sjoerg     K = CXCursor_BuiltinBitCastExpr;
8217330f729Sjoerg   }
8227330f729Sjoerg 
8237330f729Sjoerg   CXCursor C = {K, 0, {Parent, S, TU}};
8247330f729Sjoerg   return C;
8257330f729Sjoerg }
8267330f729Sjoerg 
MakeCursorObjCSuperClassRef(ObjCInterfaceDecl * Super,SourceLocation Loc,CXTranslationUnit TU)8277330f729Sjoerg CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
8287330f729Sjoerg                                                SourceLocation Loc,
8297330f729Sjoerg                                                CXTranslationUnit TU) {
8307330f729Sjoerg   assert(Super && TU && "Invalid arguments!");
8317330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
8327330f729Sjoerg   CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
8337330f729Sjoerg   return C;
8347330f729Sjoerg }
8357330f729Sjoerg 
8367330f729Sjoerg std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCSuperClassRef(CXCursor C)8377330f729Sjoerg cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
8387330f729Sjoerg   assert(C.kind == CXCursor_ObjCSuperClassRef);
8397330f729Sjoerg   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
8407330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
8417330f729Sjoerg }
8427330f729Sjoerg 
MakeCursorObjCProtocolRef(const ObjCProtocolDecl * Proto,SourceLocation Loc,CXTranslationUnit TU)8437330f729Sjoerg CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
8447330f729Sjoerg                                              SourceLocation Loc,
8457330f729Sjoerg                                              CXTranslationUnit TU) {
8467330f729Sjoerg   assert(Proto && TU && "Invalid arguments!");
8477330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
8487330f729Sjoerg   CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
8497330f729Sjoerg   return C;
8507330f729Sjoerg }
8517330f729Sjoerg 
8527330f729Sjoerg std::pair<const ObjCProtocolDecl *, SourceLocation>
getCursorObjCProtocolRef(CXCursor C)8537330f729Sjoerg cxcursor::getCursorObjCProtocolRef(CXCursor C) {
8547330f729Sjoerg   assert(C.kind == CXCursor_ObjCProtocolRef);
8557330f729Sjoerg   return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
8567330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
8577330f729Sjoerg }
8587330f729Sjoerg 
MakeCursorObjCClassRef(const ObjCInterfaceDecl * Class,SourceLocation Loc,CXTranslationUnit TU)8597330f729Sjoerg CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
8607330f729Sjoerg                                           SourceLocation Loc,
8617330f729Sjoerg                                           CXTranslationUnit TU) {
8627330f729Sjoerg   // 'Class' can be null for invalid code.
8637330f729Sjoerg   if (!Class)
8647330f729Sjoerg     return MakeCXCursorInvalid(CXCursor_InvalidCode);
8657330f729Sjoerg   assert(TU && "Invalid arguments!");
8667330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
8677330f729Sjoerg   CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
8687330f729Sjoerg   return C;
8697330f729Sjoerg }
8707330f729Sjoerg 
8717330f729Sjoerg std::pair<const ObjCInterfaceDecl *, SourceLocation>
getCursorObjCClassRef(CXCursor C)8727330f729Sjoerg cxcursor::getCursorObjCClassRef(CXCursor C) {
8737330f729Sjoerg   assert(C.kind == CXCursor_ObjCClassRef);
8747330f729Sjoerg   return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
8757330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
8767330f729Sjoerg }
8777330f729Sjoerg 
MakeCursorTypeRef(const TypeDecl * Type,SourceLocation Loc,CXTranslationUnit TU)8787330f729Sjoerg CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
8797330f729Sjoerg                                      CXTranslationUnit TU) {
8807330f729Sjoerg   assert(Type && TU && "Invalid arguments!");
8817330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
8827330f729Sjoerg   CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
8837330f729Sjoerg   return C;
8847330f729Sjoerg }
8857330f729Sjoerg 
8867330f729Sjoerg std::pair<const TypeDecl *, SourceLocation>
getCursorTypeRef(CXCursor C)8877330f729Sjoerg cxcursor::getCursorTypeRef(CXCursor C) {
8887330f729Sjoerg   assert(C.kind == CXCursor_TypeRef);
8897330f729Sjoerg   return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
8907330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
8917330f729Sjoerg }
8927330f729Sjoerg 
MakeCursorTemplateRef(const TemplateDecl * Template,SourceLocation Loc,CXTranslationUnit TU)8937330f729Sjoerg CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
8947330f729Sjoerg                                          SourceLocation Loc,
8957330f729Sjoerg                                          CXTranslationUnit TU) {
8967330f729Sjoerg   assert(Template && TU && "Invalid arguments!");
8977330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
8987330f729Sjoerg   CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
8997330f729Sjoerg   return C;
9007330f729Sjoerg }
9017330f729Sjoerg 
9027330f729Sjoerg std::pair<const TemplateDecl *, SourceLocation>
getCursorTemplateRef(CXCursor C)9037330f729Sjoerg cxcursor::getCursorTemplateRef(CXCursor C) {
9047330f729Sjoerg   assert(C.kind == CXCursor_TemplateRef);
9057330f729Sjoerg   return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
9067330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
9077330f729Sjoerg }
9087330f729Sjoerg 
MakeCursorNamespaceRef(const NamedDecl * NS,SourceLocation Loc,CXTranslationUnit TU)9097330f729Sjoerg CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
9107330f729Sjoerg                                           SourceLocation Loc,
9117330f729Sjoerg                                           CXTranslationUnit TU) {
9127330f729Sjoerg 
9137330f729Sjoerg   assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
9147330f729Sjoerg          "Invalid arguments!");
9157330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
9167330f729Sjoerg   CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
9177330f729Sjoerg   return C;
9187330f729Sjoerg }
9197330f729Sjoerg 
9207330f729Sjoerg std::pair<const NamedDecl *, SourceLocation>
getCursorNamespaceRef(CXCursor C)9217330f729Sjoerg cxcursor::getCursorNamespaceRef(CXCursor C) {
9227330f729Sjoerg   assert(C.kind == CXCursor_NamespaceRef);
9237330f729Sjoerg   return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
9247330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
9257330f729Sjoerg }
9267330f729Sjoerg 
MakeCursorVariableRef(const VarDecl * Var,SourceLocation Loc,CXTranslationUnit TU)9277330f729Sjoerg CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
9287330f729Sjoerg                                          CXTranslationUnit TU) {
9297330f729Sjoerg 
9307330f729Sjoerg   assert(Var && TU && "Invalid arguments!");
9317330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
9327330f729Sjoerg   CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
9337330f729Sjoerg   return C;
9347330f729Sjoerg }
9357330f729Sjoerg 
9367330f729Sjoerg std::pair<const VarDecl *, SourceLocation>
getCursorVariableRef(CXCursor C)9377330f729Sjoerg cxcursor::getCursorVariableRef(CXCursor C) {
9387330f729Sjoerg   assert(C.kind == CXCursor_VariableRef);
9397330f729Sjoerg   return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
9407330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
9417330f729Sjoerg }
9427330f729Sjoerg 
MakeCursorMemberRef(const FieldDecl * Field,SourceLocation Loc,CXTranslationUnit TU)943*e038c9c4Sjoerg CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
944*e038c9c4Sjoerg                                        SourceLocation Loc,
9457330f729Sjoerg                                        CXTranslationUnit TU) {
9467330f729Sjoerg 
9477330f729Sjoerg   assert(Field && TU && "Invalid arguments!");
9487330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
9497330f729Sjoerg   CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
9507330f729Sjoerg   return C;
9517330f729Sjoerg }
9527330f729Sjoerg 
9537330f729Sjoerg std::pair<const FieldDecl *, SourceLocation>
getCursorMemberRef(CXCursor C)9547330f729Sjoerg cxcursor::getCursorMemberRef(CXCursor C) {
9557330f729Sjoerg   assert(C.kind == CXCursor_MemberRef);
9567330f729Sjoerg   return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
9577330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
9587330f729Sjoerg }
9597330f729Sjoerg 
MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier * B,CXTranslationUnit TU)9607330f729Sjoerg CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
9617330f729Sjoerg                                               CXTranslationUnit TU) {
9627330f729Sjoerg   CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
9637330f729Sjoerg   return C;
9647330f729Sjoerg }
9657330f729Sjoerg 
getCursorCXXBaseSpecifier(CXCursor C)9667330f729Sjoerg const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
9677330f729Sjoerg   assert(C.kind == CXCursor_CXXBaseSpecifier);
9687330f729Sjoerg   return static_cast<const CXXBaseSpecifier *>(C.data[0]);
9697330f729Sjoerg }
9707330f729Sjoerg 
MakePreprocessingDirectiveCursor(SourceRange Range,CXTranslationUnit TU)9717330f729Sjoerg CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
9727330f729Sjoerg                                                     CXTranslationUnit TU) {
973*e038c9c4Sjoerg   CXCursor C = {
974*e038c9c4Sjoerg       CXCursor_PreprocessingDirective,
975*e038c9c4Sjoerg       0,
976*e038c9c4Sjoerg       {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
9777330f729Sjoerg   return C;
9787330f729Sjoerg }
9797330f729Sjoerg 
getCursorPreprocessingDirective(CXCursor C)9807330f729Sjoerg SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
9817330f729Sjoerg   assert(C.kind == CXCursor_PreprocessingDirective);
9827330f729Sjoerg   SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
9837330f729Sjoerg                     SourceLocation::getFromPtrEncoding(C.data[1]));
9847330f729Sjoerg   ASTUnit *TU = getCursorASTUnit(C);
9857330f729Sjoerg   return TU->mapRangeFromPreamble(Range);
9867330f729Sjoerg }
9877330f729Sjoerg 
MakeMacroDefinitionCursor(const MacroDefinitionRecord * MI,CXTranslationUnit TU)9887330f729Sjoerg CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
9897330f729Sjoerg                                              CXTranslationUnit TU) {
9907330f729Sjoerg   CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
9917330f729Sjoerg   return C;
9927330f729Sjoerg }
9937330f729Sjoerg 
getCursorMacroDefinition(CXCursor C)9947330f729Sjoerg const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
9957330f729Sjoerg   assert(C.kind == CXCursor_MacroDefinition);
9967330f729Sjoerg   return static_cast<const MacroDefinitionRecord *>(C.data[0]);
9977330f729Sjoerg }
9987330f729Sjoerg 
MakeMacroExpansionCursor(MacroExpansion * MI,CXTranslationUnit TU)9997330f729Sjoerg CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
10007330f729Sjoerg                                             CXTranslationUnit TU) {
10017330f729Sjoerg   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
10027330f729Sjoerg   return C;
10037330f729Sjoerg }
10047330f729Sjoerg 
MakeMacroExpansionCursor(MacroDefinitionRecord * MI,SourceLocation Loc,CXTranslationUnit TU)10057330f729Sjoerg CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
10067330f729Sjoerg                                             SourceLocation Loc,
10077330f729Sjoerg                                             CXTranslationUnit TU) {
10087330f729Sjoerg   assert(Loc.isValid());
10097330f729Sjoerg   CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
10107330f729Sjoerg   return C;
10117330f729Sjoerg }
10127330f729Sjoerg 
getName() const10137330f729Sjoerg const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
10147330f729Sjoerg   if (isPseudo())
10157330f729Sjoerg     return getAsMacroDefinition()->getName();
10167330f729Sjoerg   return getAsMacroExpansion()->getName();
10177330f729Sjoerg }
10187330f729Sjoerg const MacroDefinitionRecord *
getDefinition() const10197330f729Sjoerg cxcursor::MacroExpansionCursor::getDefinition() const {
10207330f729Sjoerg   if (isPseudo())
10217330f729Sjoerg     return getAsMacroDefinition();
10227330f729Sjoerg   return getAsMacroExpansion()->getDefinition();
10237330f729Sjoerg }
getSourceRange() const10247330f729Sjoerg SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
10257330f729Sjoerg   if (isPseudo())
10267330f729Sjoerg     return getPseudoLoc();
10277330f729Sjoerg   return getAsMacroExpansion()->getSourceRange();
10287330f729Sjoerg }
10297330f729Sjoerg 
MakeInclusionDirectiveCursor(InclusionDirective * ID,CXTranslationUnit TU)10307330f729Sjoerg CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
10317330f729Sjoerg                                                 CXTranslationUnit TU) {
10327330f729Sjoerg   CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
10337330f729Sjoerg   return C;
10347330f729Sjoerg }
10357330f729Sjoerg 
getCursorInclusionDirective(CXCursor C)10367330f729Sjoerg const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
10377330f729Sjoerg   assert(C.kind == CXCursor_InclusionDirective);
10387330f729Sjoerg   return static_cast<const InclusionDirective *>(C.data[0]);
10397330f729Sjoerg }
10407330f729Sjoerg 
MakeCursorLabelRef(LabelStmt * Label,SourceLocation Loc,CXTranslationUnit TU)10417330f729Sjoerg CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
10427330f729Sjoerg                                       CXTranslationUnit TU) {
10437330f729Sjoerg 
10447330f729Sjoerg   assert(Label && TU && "Invalid arguments!");
10457330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
10467330f729Sjoerg   CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
10477330f729Sjoerg   return C;
10487330f729Sjoerg }
10497330f729Sjoerg 
10507330f729Sjoerg std::pair<const LabelStmt *, SourceLocation>
getCursorLabelRef(CXCursor C)10517330f729Sjoerg cxcursor::getCursorLabelRef(CXCursor C) {
10527330f729Sjoerg   assert(C.kind == CXCursor_LabelRef);
10537330f729Sjoerg   return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
10547330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
10557330f729Sjoerg }
10567330f729Sjoerg 
MakeCursorOverloadedDeclRef(const OverloadExpr * E,CXTranslationUnit TU)10577330f729Sjoerg CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
10587330f729Sjoerg                                                CXTranslationUnit TU) {
10597330f729Sjoerg   assert(E && TU && "Invalid arguments!");
10607330f729Sjoerg   OverloadedDeclRefStorage Storage(E);
10617330f729Sjoerg   void *RawLoc = E->getNameLoc().getPtrEncoding();
10627330f729Sjoerg   CXCursor C = {
1063*e038c9c4Sjoerg       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
10647330f729Sjoerg   return C;
10657330f729Sjoerg }
10667330f729Sjoerg 
MakeCursorOverloadedDeclRef(const Decl * D,SourceLocation Loc,CXTranslationUnit TU)10677330f729Sjoerg CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
10687330f729Sjoerg                                                SourceLocation Loc,
10697330f729Sjoerg                                                CXTranslationUnit TU) {
10707330f729Sjoerg   assert(D && TU && "Invalid arguments!");
10717330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
10727330f729Sjoerg   OverloadedDeclRefStorage Storage(D);
10737330f729Sjoerg   CXCursor C = {
1074*e038c9c4Sjoerg       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
10757330f729Sjoerg   return C;
10767330f729Sjoerg }
10777330f729Sjoerg 
MakeCursorOverloadedDeclRef(TemplateName Name,SourceLocation Loc,CXTranslationUnit TU)10787330f729Sjoerg CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
10797330f729Sjoerg                                                SourceLocation Loc,
10807330f729Sjoerg                                                CXTranslationUnit TU) {
10817330f729Sjoerg   assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
10827330f729Sjoerg   void *RawLoc = Loc.getPtrEncoding();
10837330f729Sjoerg   OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
10847330f729Sjoerg   CXCursor C = {
1085*e038c9c4Sjoerg       CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
10867330f729Sjoerg   return C;
10877330f729Sjoerg }
10887330f729Sjoerg 
10897330f729Sjoerg std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
getCursorOverloadedDeclRef(CXCursor C)10907330f729Sjoerg cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
10917330f729Sjoerg   assert(C.kind == CXCursor_OverloadedDeclRef);
10927330f729Sjoerg   return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
10937330f729Sjoerg                             const_cast<void *>(C.data[0])),
10947330f729Sjoerg                         SourceLocation::getFromPtrEncoding(C.data[1]));
10957330f729Sjoerg }
10967330f729Sjoerg 
getCursorDecl(CXCursor Cursor)10977330f729Sjoerg const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
10987330f729Sjoerg   return static_cast<const Decl *>(Cursor.data[0]);
10997330f729Sjoerg }
11007330f729Sjoerg 
getCursorExpr(CXCursor Cursor)11017330f729Sjoerg const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
11027330f729Sjoerg   return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
11037330f729Sjoerg }
11047330f729Sjoerg 
getCursorStmt(CXCursor Cursor)11057330f729Sjoerg const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
11067330f729Sjoerg   if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
11077330f729Sjoerg       Cursor.kind == CXCursor_ObjCProtocolRef ||
11087330f729Sjoerg       Cursor.kind == CXCursor_ObjCClassRef)
11097330f729Sjoerg     return nullptr;
11107330f729Sjoerg 
11117330f729Sjoerg   return static_cast<const Stmt *>(Cursor.data[1]);
11127330f729Sjoerg }
11137330f729Sjoerg 
getCursorAttr(CXCursor Cursor)11147330f729Sjoerg const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
11157330f729Sjoerg   return static_cast<const Attr *>(Cursor.data[1]);
11167330f729Sjoerg }
11177330f729Sjoerg 
getCursorContext(CXCursor Cursor)11187330f729Sjoerg ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
11197330f729Sjoerg   return getCursorASTUnit(Cursor)->getASTContext();
11207330f729Sjoerg }
11217330f729Sjoerg 
getCursorASTUnit(CXCursor Cursor)11227330f729Sjoerg ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
11237330f729Sjoerg   CXTranslationUnit TU = getCursorTU(Cursor);
11247330f729Sjoerg   if (!TU)
11257330f729Sjoerg     return nullptr;
11267330f729Sjoerg   return cxtu::getASTUnit(TU);
11277330f729Sjoerg }
11287330f729Sjoerg 
getCursorTU(CXCursor Cursor)11297330f729Sjoerg CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
11307330f729Sjoerg   return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
11317330f729Sjoerg }
11327330f729Sjoerg 
getOverriddenCursors(CXCursor cursor,SmallVectorImpl<CXCursor> & overridden)11337330f729Sjoerg void cxcursor::getOverriddenCursors(CXCursor cursor,
11347330f729Sjoerg                                     SmallVectorImpl<CXCursor> &overridden) {
11357330f729Sjoerg   assert(clang_isDeclaration(cursor.kind));
11367330f729Sjoerg   const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
11377330f729Sjoerg   if (!D)
11387330f729Sjoerg     return;
11397330f729Sjoerg 
11407330f729Sjoerg   CXTranslationUnit TU = getCursorTU(cursor);
11417330f729Sjoerg   SmallVector<const NamedDecl *, 8> OverDecls;
11427330f729Sjoerg   D->getASTContext().getOverriddenMethods(D, OverDecls);
11437330f729Sjoerg 
1144*e038c9c4Sjoerg   for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1145*e038c9c4Sjoerg                                                     E = OverDecls.end();
1146*e038c9c4Sjoerg        I != E; ++I) {
11477330f729Sjoerg     overridden.push_back(MakeCXCursor(*I, TU));
11487330f729Sjoerg   }
11497330f729Sjoerg }
11507330f729Sjoerg 
11517330f729Sjoerg std::pair<int, SourceLocation>
getSelectorIdentifierIndexAndLoc(CXCursor cursor)11527330f729Sjoerg cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
11537330f729Sjoerg   if (cursor.kind == CXCursor_ObjCMessageExpr) {
11547330f729Sjoerg     if (cursor.xdata != -1)
11557330f729Sjoerg       return std::make_pair(cursor.xdata,
11567330f729Sjoerg                             cast<ObjCMessageExpr>(getCursorExpr(cursor))
11577330f729Sjoerg                                 ->getSelectorLoc(cursor.xdata));
11587330f729Sjoerg   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
11597330f729Sjoerg              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
11607330f729Sjoerg     if (cursor.xdata != -1)
11617330f729Sjoerg       return std::make_pair(cursor.xdata,
11627330f729Sjoerg                             cast<ObjCMethodDecl>(getCursorDecl(cursor))
11637330f729Sjoerg                                 ->getSelectorLoc(cursor.xdata));
11647330f729Sjoerg   }
11657330f729Sjoerg 
11667330f729Sjoerg   return std::make_pair(-1, SourceLocation());
11677330f729Sjoerg }
11687330f729Sjoerg 
getSelectorIdentifierCursor(int SelIdx,CXCursor cursor)11697330f729Sjoerg CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
11707330f729Sjoerg   CXCursor newCursor = cursor;
11717330f729Sjoerg 
11727330f729Sjoerg   if (cursor.kind == CXCursor_ObjCMessageExpr) {
11737330f729Sjoerg     if (SelIdx == -1 ||
1174*e038c9c4Sjoerg         unsigned(SelIdx) >=
1175*e038c9c4Sjoerg             cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
11767330f729Sjoerg       newCursor.xdata = -1;
11777330f729Sjoerg     else
11787330f729Sjoerg       newCursor.xdata = SelIdx;
11797330f729Sjoerg   } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
11807330f729Sjoerg              cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
11817330f729Sjoerg     if (SelIdx == -1 ||
1182*e038c9c4Sjoerg         unsigned(SelIdx) >=
1183*e038c9c4Sjoerg             cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
11847330f729Sjoerg       newCursor.xdata = -1;
11857330f729Sjoerg     else
11867330f729Sjoerg       newCursor.xdata = SelIdx;
11877330f729Sjoerg   }
11887330f729Sjoerg 
11897330f729Sjoerg   return newCursor;
11907330f729Sjoerg }
11917330f729Sjoerg 
getTypeRefCursor(CXCursor cursor)11927330f729Sjoerg CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
11937330f729Sjoerg   if (cursor.kind != CXCursor_CallExpr)
11947330f729Sjoerg     return cursor;
11957330f729Sjoerg 
11967330f729Sjoerg   if (cursor.xdata == 0)
11977330f729Sjoerg     return cursor;
11987330f729Sjoerg 
11997330f729Sjoerg   const Expr *E = getCursorExpr(cursor);
12007330f729Sjoerg   TypeSourceInfo *Type = nullptr;
1201*e038c9c4Sjoerg   if (const CXXUnresolvedConstructExpr *UnCtor =
1202*e038c9c4Sjoerg           dyn_cast<CXXUnresolvedConstructExpr>(E)) {
12037330f729Sjoerg     Type = UnCtor->getTypeSourceInfo();
12047330f729Sjoerg   } else if (const CXXTemporaryObjectExpr *Tmp =
12057330f729Sjoerg                  dyn_cast<CXXTemporaryObjectExpr>(E)) {
12067330f729Sjoerg     Type = Tmp->getTypeSourceInfo();
12077330f729Sjoerg   }
12087330f729Sjoerg 
12097330f729Sjoerg   if (!Type)
12107330f729Sjoerg     return cursor;
12117330f729Sjoerg 
12127330f729Sjoerg   CXTranslationUnit TU = getCursorTU(cursor);
12137330f729Sjoerg   QualType Ty = Type->getType();
12147330f729Sjoerg   TypeLoc TL = Type->getTypeLoc();
12157330f729Sjoerg   SourceLocation Loc = TL.getBeginLoc();
12167330f729Sjoerg 
12177330f729Sjoerg   if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
12187330f729Sjoerg     Ty = ElabT->getNamedType();
12197330f729Sjoerg     ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
12207330f729Sjoerg     Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
12217330f729Sjoerg   }
12227330f729Sjoerg 
12237330f729Sjoerg   if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
12247330f729Sjoerg     return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
12257330f729Sjoerg   if (const TagType *Tag = Ty->getAs<TagType>())
12267330f729Sjoerg     return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
12277330f729Sjoerg   if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
12287330f729Sjoerg     return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
12297330f729Sjoerg 
12307330f729Sjoerg   return cursor;
12317330f729Sjoerg }
12327330f729Sjoerg 
operator ==(CXCursor X,CXCursor Y)12337330f729Sjoerg bool cxcursor::operator==(CXCursor X, CXCursor Y) {
12347330f729Sjoerg   return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
12357330f729Sjoerg          X.data[2] == Y.data[2];
12367330f729Sjoerg }
12377330f729Sjoerg 
12387330f729Sjoerg // FIXME: Remove once we can model DeclGroups and their appropriate ranges
12397330f729Sjoerg // properly in the ASTs.
isFirstInDeclGroup(CXCursor C)12407330f729Sjoerg bool cxcursor::isFirstInDeclGroup(CXCursor C) {
12417330f729Sjoerg   assert(clang_isDeclaration(C.kind));
12427330f729Sjoerg   return ((uintptr_t)(C.data[1])) != 0;
12437330f729Sjoerg }
12447330f729Sjoerg 
12457330f729Sjoerg //===----------------------------------------------------------------------===//
12467330f729Sjoerg // libclang CXCursor APIs
12477330f729Sjoerg //===----------------------------------------------------------------------===//
12487330f729Sjoerg 
clang_Cursor_isNull(CXCursor cursor)12497330f729Sjoerg int clang_Cursor_isNull(CXCursor cursor) {
12507330f729Sjoerg   return clang_equalCursors(cursor, clang_getNullCursor());
12517330f729Sjoerg }
12527330f729Sjoerg 
clang_Cursor_getTranslationUnit(CXCursor cursor)12537330f729Sjoerg CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
12547330f729Sjoerg   return getCursorTU(cursor);
12557330f729Sjoerg }
12567330f729Sjoerg 
clang_Cursor_getNumArguments(CXCursor C)12577330f729Sjoerg int clang_Cursor_getNumArguments(CXCursor C) {
12587330f729Sjoerg   if (clang_isDeclaration(C.kind)) {
12597330f729Sjoerg     const Decl *D = cxcursor::getCursorDecl(C);
12607330f729Sjoerg     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
12617330f729Sjoerg       return MD->param_size();
12627330f729Sjoerg     if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
12637330f729Sjoerg       return FD->param_size();
12647330f729Sjoerg   }
12657330f729Sjoerg 
12667330f729Sjoerg   if (clang_isExpression(C.kind)) {
12677330f729Sjoerg     const Expr *E = cxcursor::getCursorExpr(C);
12687330f729Sjoerg     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
12697330f729Sjoerg       return CE->getNumArgs();
12707330f729Sjoerg     }
12717330f729Sjoerg     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
12727330f729Sjoerg       return CE->getNumArgs();
12737330f729Sjoerg     }
12747330f729Sjoerg   }
12757330f729Sjoerg 
12767330f729Sjoerg   return -1;
12777330f729Sjoerg }
12787330f729Sjoerg 
clang_Cursor_getArgument(CXCursor C,unsigned i)12797330f729Sjoerg CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
12807330f729Sjoerg   if (clang_isDeclaration(C.kind)) {
12817330f729Sjoerg     const Decl *D = cxcursor::getCursorDecl(C);
12827330f729Sjoerg     if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
12837330f729Sjoerg       if (i < MD->param_size())
12847330f729Sjoerg         return cxcursor::MakeCXCursor(MD->parameters()[i],
12857330f729Sjoerg                                       cxcursor::getCursorTU(C));
12867330f729Sjoerg     } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
12877330f729Sjoerg       if (i < FD->param_size())
12887330f729Sjoerg         return cxcursor::MakeCXCursor(FD->parameters()[i],
12897330f729Sjoerg                                       cxcursor::getCursorTU(C));
12907330f729Sjoerg     }
12917330f729Sjoerg   }
12927330f729Sjoerg 
12937330f729Sjoerg   if (clang_isExpression(C.kind)) {
12947330f729Sjoerg     const Expr *E = cxcursor::getCursorExpr(C);
12957330f729Sjoerg     if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
12967330f729Sjoerg       if (i < CE->getNumArgs()) {
1297*e038c9c4Sjoerg         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
12987330f729Sjoerg                                       cxcursor::getCursorTU(C));
12997330f729Sjoerg       }
13007330f729Sjoerg     }
13017330f729Sjoerg     if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
13027330f729Sjoerg       if (i < CE->getNumArgs()) {
1303*e038c9c4Sjoerg         return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
13047330f729Sjoerg                                       cxcursor::getCursorTU(C));
13057330f729Sjoerg       }
13067330f729Sjoerg     }
13077330f729Sjoerg   }
13087330f729Sjoerg 
13097330f729Sjoerg   return clang_getNullCursor();
13107330f729Sjoerg }
13117330f729Sjoerg 
clang_Cursor_getNumTemplateArguments(CXCursor C)13127330f729Sjoerg int clang_Cursor_getNumTemplateArguments(CXCursor C) {
13137330f729Sjoerg   if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
13147330f729Sjoerg     return -1;
13157330f729Sjoerg   }
13167330f729Sjoerg 
1317*e038c9c4Sjoerg   const FunctionDecl *FD =
1318*e038c9c4Sjoerg       llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
13197330f729Sjoerg   if (!FD) {
13207330f729Sjoerg     return -1;
13217330f729Sjoerg   }
13227330f729Sjoerg 
13237330f729Sjoerg   const FunctionTemplateSpecializationInfo *SpecInfo =
13247330f729Sjoerg       FD->getTemplateSpecializationInfo();
13257330f729Sjoerg   if (!SpecInfo) {
13267330f729Sjoerg     return -1;
13277330f729Sjoerg   }
13287330f729Sjoerg 
13297330f729Sjoerg   return SpecInfo->TemplateArguments->size();
13307330f729Sjoerg }
13317330f729Sjoerg 
13327330f729Sjoerg enum CXGetTemplateArgumentStatus {
13337330f729Sjoerg   /** The operation completed successfully */
13347330f729Sjoerg   CXGetTemplateArgumentStatus_Success = 0,
13357330f729Sjoerg 
13367330f729Sjoerg   /** The specified cursor did not represent a FunctionDecl. */
13377330f729Sjoerg   CXGetTemplateArgumentStatus_CursorNotFunctionDecl = -1,
13387330f729Sjoerg 
13397330f729Sjoerg   /** The specified cursor was not castable to a FunctionDecl. */
13407330f729Sjoerg   CXGetTemplateArgumentStatus_BadFunctionDeclCast = -2,
13417330f729Sjoerg 
13427330f729Sjoerg   /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
13437330f729Sjoerg   CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
13447330f729Sjoerg 
13457330f729Sjoerg   /** An invalid (OOB) argument index was specified */
13467330f729Sjoerg   CXGetTemplateArgumentStatus_InvalidIndex = -4
13477330f729Sjoerg };
13487330f729Sjoerg 
clang_Cursor_getTemplateArgument(CXCursor C,unsigned I,TemplateArgument * TA)1349*e038c9c4Sjoerg static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1350*e038c9c4Sjoerg                                             TemplateArgument *TA) {
13517330f729Sjoerg   if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
13527330f729Sjoerg     return CXGetTemplateArgumentStatus_CursorNotFunctionDecl;
13537330f729Sjoerg   }
13547330f729Sjoerg 
1355*e038c9c4Sjoerg   const FunctionDecl *FD =
1356*e038c9c4Sjoerg       llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
13577330f729Sjoerg   if (!FD) {
13587330f729Sjoerg     return CXGetTemplateArgumentStatus_BadFunctionDeclCast;
13597330f729Sjoerg   }
13607330f729Sjoerg 
13617330f729Sjoerg   const FunctionTemplateSpecializationInfo *SpecInfo =
13627330f729Sjoerg       FD->getTemplateSpecializationInfo();
13637330f729Sjoerg   if (!SpecInfo) {
13647330f729Sjoerg     return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
13657330f729Sjoerg   }
13667330f729Sjoerg 
13677330f729Sjoerg   if (I >= SpecInfo->TemplateArguments->size()) {
13687330f729Sjoerg     return CXGetTemplateArgumentStatus_InvalidIndex;
13697330f729Sjoerg   }
13707330f729Sjoerg 
13717330f729Sjoerg   *TA = SpecInfo->TemplateArguments->get(I);
13727330f729Sjoerg   return 0;
13737330f729Sjoerg }
13747330f729Sjoerg 
clang_Cursor_getTemplateArgumentKind(CXCursor C,unsigned I)13757330f729Sjoerg enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
13767330f729Sjoerg                                                                  unsigned I) {
13777330f729Sjoerg   TemplateArgument TA;
13787330f729Sjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
13797330f729Sjoerg     return CXTemplateArgumentKind_Invalid;
13807330f729Sjoerg   }
13817330f729Sjoerg 
13827330f729Sjoerg   switch (TA.getKind()) {
1383*e038c9c4Sjoerg   case TemplateArgument::Null:
1384*e038c9c4Sjoerg     return CXTemplateArgumentKind_Null;
1385*e038c9c4Sjoerg   case TemplateArgument::Type:
1386*e038c9c4Sjoerg     return CXTemplateArgumentKind_Type;
13877330f729Sjoerg   case TemplateArgument::Declaration:
13887330f729Sjoerg     return CXTemplateArgumentKind_Declaration;
1389*e038c9c4Sjoerg   case TemplateArgument::NullPtr:
1390*e038c9c4Sjoerg     return CXTemplateArgumentKind_NullPtr;
1391*e038c9c4Sjoerg   case TemplateArgument::Integral:
1392*e038c9c4Sjoerg     return CXTemplateArgumentKind_Integral;
1393*e038c9c4Sjoerg   case TemplateArgument::Template:
1394*e038c9c4Sjoerg     return CXTemplateArgumentKind_Template;
13957330f729Sjoerg   case TemplateArgument::TemplateExpansion:
13967330f729Sjoerg     return CXTemplateArgumentKind_TemplateExpansion;
1397*e038c9c4Sjoerg   case TemplateArgument::Expression:
1398*e038c9c4Sjoerg     return CXTemplateArgumentKind_Expression;
1399*e038c9c4Sjoerg   case TemplateArgument::Pack:
1400*e038c9c4Sjoerg     return CXTemplateArgumentKind_Pack;
14017330f729Sjoerg   }
14027330f729Sjoerg 
14037330f729Sjoerg   return CXTemplateArgumentKind_Invalid;
14047330f729Sjoerg }
14057330f729Sjoerg 
clang_Cursor_getTemplateArgumentType(CXCursor C,unsigned I)14067330f729Sjoerg CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
14077330f729Sjoerg   TemplateArgument TA;
14087330f729Sjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
14097330f729Sjoerg       CXGetTemplateArgumentStatus_Success) {
14107330f729Sjoerg     return cxtype::MakeCXType(QualType(), getCursorTU(C));
14117330f729Sjoerg   }
14127330f729Sjoerg 
14137330f729Sjoerg   if (TA.getKind() != TemplateArgument::Type) {
14147330f729Sjoerg     return cxtype::MakeCXType(QualType(), getCursorTU(C));
14157330f729Sjoerg   }
14167330f729Sjoerg 
14177330f729Sjoerg   return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
14187330f729Sjoerg }
14197330f729Sjoerg 
clang_Cursor_getTemplateArgumentValue(CXCursor C,unsigned I)14207330f729Sjoerg long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
14217330f729Sjoerg   TemplateArgument TA;
14227330f729Sjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
14237330f729Sjoerg       CXGetTemplateArgumentStatus_Success) {
14247330f729Sjoerg     assert(0 && "Unable to retrieve TemplateArgument");
14257330f729Sjoerg     return 0;
14267330f729Sjoerg   }
14277330f729Sjoerg 
14287330f729Sjoerg   if (TA.getKind() != TemplateArgument::Integral) {
14297330f729Sjoerg     assert(0 && "Passed template argument is not Integral");
14307330f729Sjoerg     return 0;
14317330f729Sjoerg   }
14327330f729Sjoerg 
14337330f729Sjoerg   return TA.getAsIntegral().getSExtValue();
14347330f729Sjoerg }
14357330f729Sjoerg 
clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,unsigned I)14367330f729Sjoerg unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
14377330f729Sjoerg                                                                  unsigned I) {
14387330f729Sjoerg   TemplateArgument TA;
14397330f729Sjoerg   if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
14407330f729Sjoerg       CXGetTemplateArgumentStatus_Success) {
14417330f729Sjoerg     assert(0 && "Unable to retrieve TemplateArgument");
14427330f729Sjoerg     return 0;
14437330f729Sjoerg   }
14447330f729Sjoerg 
14457330f729Sjoerg   if (TA.getKind() != TemplateArgument::Integral) {
14467330f729Sjoerg     assert(0 && "Passed template argument is not Integral");
14477330f729Sjoerg     return 0;
14487330f729Sjoerg   }
14497330f729Sjoerg 
14507330f729Sjoerg   return TA.getAsIntegral().getZExtValue();
14517330f729Sjoerg }
14527330f729Sjoerg 
14537330f729Sjoerg //===----------------------------------------------------------------------===//
14547330f729Sjoerg // CXCursorSet.
14557330f729Sjoerg //===----------------------------------------------------------------------===//
14567330f729Sjoerg 
14577330f729Sjoerg typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
14587330f729Sjoerg 
packCXCursorSet(CXCursorSet_Impl * setImpl)14597330f729Sjoerg static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
14607330f729Sjoerg   return (CXCursorSet)setImpl;
14617330f729Sjoerg }
unpackCXCursorSet(CXCursorSet set)14627330f729Sjoerg static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
14637330f729Sjoerg   return (CXCursorSet_Impl *)set;
14647330f729Sjoerg }
14657330f729Sjoerg namespace llvm {
14667330f729Sjoerg template <> struct DenseMapInfo<CXCursor> {
14677330f729Sjoerg public:
getEmptyKeyllvm::DenseMapInfo14687330f729Sjoerg   static inline CXCursor getEmptyKey() {
14697330f729Sjoerg     return MakeCXCursorInvalid(CXCursor_InvalidFile);
14707330f729Sjoerg   }
getTombstoneKeyllvm::DenseMapInfo14717330f729Sjoerg   static inline CXCursor getTombstoneKey() {
14727330f729Sjoerg     return MakeCXCursorInvalid(CXCursor_NoDeclFound);
14737330f729Sjoerg   }
getHashValuellvm::DenseMapInfo14747330f729Sjoerg   static inline unsigned getHashValue(const CXCursor &cursor) {
1475*e038c9c4Sjoerg     return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1476*e038c9c4Sjoerg         getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
14777330f729Sjoerg   }
isEqualllvm::DenseMapInfo14787330f729Sjoerg   static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1479*e038c9c4Sjoerg     return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
14807330f729Sjoerg   }
14817330f729Sjoerg };
1482*e038c9c4Sjoerg } // namespace llvm
14837330f729Sjoerg 
clang_createCXCursorSet()14847330f729Sjoerg CXCursorSet clang_createCXCursorSet() {
14857330f729Sjoerg   return packCXCursorSet(new CXCursorSet_Impl());
14867330f729Sjoerg }
14877330f729Sjoerg 
clang_disposeCXCursorSet(CXCursorSet set)14887330f729Sjoerg void clang_disposeCXCursorSet(CXCursorSet set) {
14897330f729Sjoerg   delete unpackCXCursorSet(set);
14907330f729Sjoerg }
14917330f729Sjoerg 
clang_CXCursorSet_contains(CXCursorSet set,CXCursor cursor)14927330f729Sjoerg unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
14937330f729Sjoerg   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
14947330f729Sjoerg   if (!setImpl)
14957330f729Sjoerg     return 0;
14967330f729Sjoerg   return setImpl->find(cursor) != setImpl->end();
14977330f729Sjoerg }
14987330f729Sjoerg 
clang_CXCursorSet_insert(CXCursorSet set,CXCursor cursor)14997330f729Sjoerg unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
15007330f729Sjoerg   // Do not insert invalid cursors into the set.
15017330f729Sjoerg   if (cursor.kind >= CXCursor_FirstInvalid &&
15027330f729Sjoerg       cursor.kind <= CXCursor_LastInvalid)
15037330f729Sjoerg     return 1;
15047330f729Sjoerg 
15057330f729Sjoerg   CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
15067330f729Sjoerg   if (!setImpl)
15077330f729Sjoerg     return 1;
15087330f729Sjoerg   unsigned &entry = (*setImpl)[cursor];
15097330f729Sjoerg   unsigned flag = entry == 0 ? 1 : 0;
15107330f729Sjoerg   entry = 1;
15117330f729Sjoerg   return flag;
15127330f729Sjoerg }
15137330f729Sjoerg 
clang_getCursorCompletionString(CXCursor cursor)15147330f729Sjoerg CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
15157330f729Sjoerg   enum CXCursorKind kind = clang_getCursorKind(cursor);
15167330f729Sjoerg   if (clang_isDeclaration(kind)) {
15177330f729Sjoerg     const Decl *decl = getCursorDecl(cursor);
15187330f729Sjoerg     if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
15197330f729Sjoerg       ASTUnit *unit = getCursorASTUnit(cursor);
15207330f729Sjoerg       CodeCompletionResult Result(namedDecl, CCP_Declaration);
1521*e038c9c4Sjoerg       CodeCompletionString *String = Result.CreateCodeCompletionString(
1522*e038c9c4Sjoerg           unit->getASTContext(), unit->getPreprocessor(),
15237330f729Sjoerg           CodeCompletionContext::CCC_Other,
15247330f729Sjoerg           unit->getCodeCompletionTUInfo().getAllocator(),
1525*e038c9c4Sjoerg           unit->getCodeCompletionTUInfo(), true);
15267330f729Sjoerg       return String;
15277330f729Sjoerg     }
15287330f729Sjoerg   } else if (kind == CXCursor_MacroDefinition) {
15297330f729Sjoerg     const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
15307330f729Sjoerg     const IdentifierInfo *Macro = definition->getName();
15317330f729Sjoerg     ASTUnit *unit = getCursorASTUnit(cursor);
15327330f729Sjoerg     CodeCompletionResult Result(
15337330f729Sjoerg         Macro,
15347330f729Sjoerg         unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
15357330f729Sjoerg     CodeCompletionString *String = Result.CreateCodeCompletionString(
15367330f729Sjoerg         unit->getASTContext(), unit->getPreprocessor(),
15377330f729Sjoerg         CodeCompletionContext::CCC_Other,
15387330f729Sjoerg         unit->getCodeCompletionTUInfo().getAllocator(),
15397330f729Sjoerg         unit->getCodeCompletionTUInfo(), false);
15407330f729Sjoerg     return String;
15417330f729Sjoerg   }
15427330f729Sjoerg   return nullptr;
15437330f729Sjoerg }
15447330f729Sjoerg 
15457330f729Sjoerg namespace {
15467330f729Sjoerg struct OverridenCursorsPool {
15477330f729Sjoerg   typedef SmallVector<CXCursor, 2> CursorVec;
15487330f729Sjoerg   std::vector<CursorVec *> AllCursors;
15497330f729Sjoerg   std::vector<CursorVec *> AvailableCursors;
15507330f729Sjoerg 
~OverridenCursorsPool__anonab1595e30111::OverridenCursorsPool15517330f729Sjoerg   ~OverridenCursorsPool() {
15527330f729Sjoerg     for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1553*e038c9c4Sjoerg                                             E = AllCursors.end();
1554*e038c9c4Sjoerg          I != E; ++I) {
15557330f729Sjoerg       delete *I;
15567330f729Sjoerg     }
15577330f729Sjoerg   }
15587330f729Sjoerg };
1559*e038c9c4Sjoerg } // namespace
15607330f729Sjoerg 
createOverridenCXCursorsPool()15617330f729Sjoerg void *cxcursor::createOverridenCXCursorsPool() {
15627330f729Sjoerg   return new OverridenCursorsPool();
15637330f729Sjoerg }
15647330f729Sjoerg 
disposeOverridenCXCursorsPool(void * pool)15657330f729Sjoerg void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
15667330f729Sjoerg   delete static_cast<OverridenCursorsPool *>(pool);
15677330f729Sjoerg }
15687330f729Sjoerg 
clang_getOverriddenCursors(CXCursor cursor,CXCursor ** overridden,unsigned * num_overridden)1569*e038c9c4Sjoerg void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
15707330f729Sjoerg                                 unsigned *num_overridden) {
15717330f729Sjoerg   if (overridden)
15727330f729Sjoerg     *overridden = nullptr;
15737330f729Sjoerg   if (num_overridden)
15747330f729Sjoerg     *num_overridden = 0;
15757330f729Sjoerg 
15767330f729Sjoerg   CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
15777330f729Sjoerg 
15787330f729Sjoerg   if (!overridden || !num_overridden || !TU)
15797330f729Sjoerg     return;
15807330f729Sjoerg 
15817330f729Sjoerg   if (!clang_isDeclaration(cursor.kind))
15827330f729Sjoerg     return;
15837330f729Sjoerg 
15847330f729Sjoerg   OverridenCursorsPool &pool =
15857330f729Sjoerg       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
15867330f729Sjoerg 
15877330f729Sjoerg   OverridenCursorsPool::CursorVec *Vec = nullptr;
15887330f729Sjoerg 
15897330f729Sjoerg   if (!pool.AvailableCursors.empty()) {
15907330f729Sjoerg     Vec = pool.AvailableCursors.back();
15917330f729Sjoerg     pool.AvailableCursors.pop_back();
1592*e038c9c4Sjoerg   } else {
15937330f729Sjoerg     Vec = new OverridenCursorsPool::CursorVec();
15947330f729Sjoerg     pool.AllCursors.push_back(Vec);
15957330f729Sjoerg   }
15967330f729Sjoerg 
15977330f729Sjoerg   // Clear out the vector, but don't free the memory contents.  This
15987330f729Sjoerg   // reduces malloc() traffic.
15997330f729Sjoerg   Vec->clear();
16007330f729Sjoerg 
16017330f729Sjoerg   // Use the first entry to contain a back reference to the vector.
16027330f729Sjoerg   // This is a complete hack.
16037330f729Sjoerg   CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
16047330f729Sjoerg   backRefCursor.data[0] = Vec;
16057330f729Sjoerg   assert(cxcursor::getCursorTU(backRefCursor) == TU);
16067330f729Sjoerg   Vec->push_back(backRefCursor);
16077330f729Sjoerg 
16087330f729Sjoerg   // Get the overridden cursors.
16097330f729Sjoerg   cxcursor::getOverriddenCursors(cursor, *Vec);
16107330f729Sjoerg 
16117330f729Sjoerg   // Did we get any overridden cursors?  If not, return Vec to the pool
16127330f729Sjoerg   // of available cursor vectors.
16137330f729Sjoerg   if (Vec->size() == 1) {
16147330f729Sjoerg     pool.AvailableCursors.push_back(Vec);
16157330f729Sjoerg     return;
16167330f729Sjoerg   }
16177330f729Sjoerg 
16187330f729Sjoerg   // Now tell the caller about the overridden cursors.
16197330f729Sjoerg   assert(Vec->size() > 1);
16207330f729Sjoerg   *overridden = &((*Vec)[1]);
16217330f729Sjoerg   *num_overridden = Vec->size() - 1;
16227330f729Sjoerg }
16237330f729Sjoerg 
clang_disposeOverriddenCursors(CXCursor * overridden)16247330f729Sjoerg void clang_disposeOverriddenCursors(CXCursor *overridden) {
16257330f729Sjoerg   if (!overridden)
16267330f729Sjoerg     return;
16277330f729Sjoerg 
16287330f729Sjoerg   // Use pointer arithmetic to get back the first faux entry
16297330f729Sjoerg   // which has a back-reference to the TU and the vector.
16307330f729Sjoerg   --overridden;
16317330f729Sjoerg   OverridenCursorsPool::CursorVec *Vec =
16327330f729Sjoerg       static_cast<OverridenCursorsPool::CursorVec *>(
16337330f729Sjoerg           const_cast<void *>(overridden->data[0]));
16347330f729Sjoerg   CXTranslationUnit TU = getCursorTU(*overridden);
16357330f729Sjoerg 
16367330f729Sjoerg   assert(Vec && TU);
16377330f729Sjoerg 
16387330f729Sjoerg   OverridenCursorsPool &pool =
16397330f729Sjoerg       *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
16407330f729Sjoerg 
16417330f729Sjoerg   pool.AvailableCursors.push_back(Vec);
16427330f729Sjoerg }
16437330f729Sjoerg 
clang_Cursor_isDynamicCall(CXCursor C)16447330f729Sjoerg int clang_Cursor_isDynamicCall(CXCursor C) {
16457330f729Sjoerg   const Expr *E = nullptr;
16467330f729Sjoerg   if (clang_isExpression(C.kind))
16477330f729Sjoerg     E = getCursorExpr(C);
16487330f729Sjoerg   if (!E)
16497330f729Sjoerg     return 0;
16507330f729Sjoerg 
16517330f729Sjoerg   if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
16527330f729Sjoerg     if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
16537330f729Sjoerg       return false;
16547330f729Sjoerg     if (auto *RecE = dyn_cast<ObjCMessageExpr>(
16557330f729Sjoerg             MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
16567330f729Sjoerg       if (RecE->getMethodFamily() == OMF_alloc)
16577330f729Sjoerg         return false;
16587330f729Sjoerg     }
16597330f729Sjoerg     return true;
16607330f729Sjoerg   }
16617330f729Sjoerg 
16627330f729Sjoerg   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
16637330f729Sjoerg     return !PropRefE->isSuperReceiver();
16647330f729Sjoerg   }
16657330f729Sjoerg 
16667330f729Sjoerg   const MemberExpr *ME = nullptr;
16677330f729Sjoerg   if (isa<MemberExpr>(E))
16687330f729Sjoerg     ME = cast<MemberExpr>(E);
16697330f729Sjoerg   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
16707330f729Sjoerg     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
16717330f729Sjoerg 
16727330f729Sjoerg   if (ME) {
1673*e038c9c4Sjoerg     if (const CXXMethodDecl *MD =
1674*e038c9c4Sjoerg             dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
16757330f729Sjoerg       return MD->isVirtual() &&
16767330f729Sjoerg              ME->performsVirtualDispatch(
16777330f729Sjoerg                  cxcursor::getCursorContext(C).getLangOpts());
16787330f729Sjoerg   }
16797330f729Sjoerg 
16807330f729Sjoerg   return 0;
16817330f729Sjoerg }
16827330f729Sjoerg 
clang_Cursor_getReceiverType(CXCursor C)16837330f729Sjoerg CXType clang_Cursor_getReceiverType(CXCursor C) {
16847330f729Sjoerg   CXTranslationUnit TU = cxcursor::getCursorTU(C);
16857330f729Sjoerg   const Expr *E = nullptr;
16867330f729Sjoerg   if (clang_isExpression(C.kind))
16877330f729Sjoerg     E = getCursorExpr(C);
16887330f729Sjoerg 
16897330f729Sjoerg   if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
16907330f729Sjoerg     return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
16917330f729Sjoerg 
16927330f729Sjoerg   if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
16937330f729Sjoerg     return cxtype::MakeCXType(
16947330f729Sjoerg         PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
16957330f729Sjoerg   }
16967330f729Sjoerg 
16977330f729Sjoerg   const MemberExpr *ME = nullptr;
16987330f729Sjoerg   if (isa<MemberExpr>(E))
16997330f729Sjoerg     ME = cast<MemberExpr>(E);
17007330f729Sjoerg   else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
17017330f729Sjoerg     ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
17027330f729Sjoerg 
17037330f729Sjoerg   if (ME) {
17047330f729Sjoerg     if (dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl())) {
17057330f729Sjoerg       auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
17067330f729Sjoerg       return cxtype::MakeCXType(receiverTy, TU);
17077330f729Sjoerg     }
17087330f729Sjoerg   }
17097330f729Sjoerg 
17107330f729Sjoerg   return cxtype::MakeCXType(QualType(), TU);
17117330f729Sjoerg }
1712