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