1 //===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines routines for manipulating CXCursors. It should be the
10 // only file that has internal knowledge of the encoding of the data in
13 //===----------------------------------------------------------------------===//
17 #include "CXTranslationUnit.h"
19 #include "clang-c/Index.h"
20 #include "clang/AST/Attr.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/DeclTemplate.h"
25 #include "clang/AST/Expr.h"
26 #include "clang/AST/ExprCXX.h"
27 #include "clang/AST/ExprObjC.h"
28 #include "clang/Frontend/ASTUnit.h"
29 #include "llvm/Support/ErrorHandling.h"
31 using namespace clang
;
32 using namespace cxcursor
;
34 CXCursor
cxcursor::MakeCXCursorInvalid(CXCursorKind K
, CXTranslationUnit TU
) {
35 assert(K
>= CXCursor_FirstInvalid
&& K
<= CXCursor_LastInvalid
);
36 CXCursor C
= {K
, 0, {nullptr, nullptr, TU
}};
40 static CXCursorKind
GetCursorKind(const Attr
*A
) {
41 assert(A
&& "Invalid arguments!");
42 switch (A
->getKind()) {
46 return CXCursor_IBActionAttr
;
48 return CXCursor_IBOutletAttr
;
49 case attr::IBOutletCollection
:
50 return CXCursor_IBOutletCollectionAttr
;
52 return CXCursor_CXXFinalAttr
;
54 return CXCursor_CXXOverrideAttr
;
56 return CXCursor_AnnotateAttr
;
58 return CXCursor_AsmLabelAttr
;
60 return CXCursor_PackedAttr
;
62 return CXCursor_PureAttr
;
64 return CXCursor_ConstAttr
;
65 case attr::NoDuplicate
:
66 return CXCursor_NoDuplicateAttr
;
67 case attr::CUDAConstant
:
68 return CXCursor_CUDAConstantAttr
;
69 case attr::CUDADevice
:
70 return CXCursor_CUDADeviceAttr
;
71 case attr::CUDAGlobal
:
72 return CXCursor_CUDAGlobalAttr
;
74 return CXCursor_CUDAHostAttr
;
75 case attr::CUDAShared
:
76 return CXCursor_CUDASharedAttr
;
77 case attr::Visibility
:
78 return CXCursor_VisibilityAttr
;
80 return CXCursor_DLLExport
;
82 return CXCursor_DLLImport
;
83 case attr::NSReturnsRetained
:
84 return CXCursor_NSReturnsRetained
;
85 case attr::NSReturnsNotRetained
:
86 return CXCursor_NSReturnsNotRetained
;
87 case attr::NSReturnsAutoreleased
:
88 return CXCursor_NSReturnsAutoreleased
;
89 case attr::NSConsumesSelf
:
90 return CXCursor_NSConsumesSelf
;
91 case attr::NSConsumed
:
92 return CXCursor_NSConsumed
;
93 case attr::ObjCException
:
94 return CXCursor_ObjCException
;
95 case attr::ObjCNSObject
:
96 return CXCursor_ObjCNSObject
;
97 case attr::ObjCIndependentClass
:
98 return CXCursor_ObjCIndependentClass
;
99 case attr::ObjCPreciseLifetime
:
100 return CXCursor_ObjCPreciseLifetime
;
101 case attr::ObjCReturnsInnerPointer
:
102 return CXCursor_ObjCReturnsInnerPointer
;
103 case attr::ObjCRequiresSuper
:
104 return CXCursor_ObjCRequiresSuper
;
105 case attr::ObjCRootClass
:
106 return CXCursor_ObjCRootClass
;
107 case attr::ObjCSubclassingRestricted
:
108 return CXCursor_ObjCSubclassingRestricted
;
109 case attr::ObjCExplicitProtocolImpl
:
110 return CXCursor_ObjCExplicitProtocolImpl
;
111 case attr::ObjCDesignatedInitializer
:
112 return CXCursor_ObjCDesignatedInitializer
;
113 case attr::ObjCRuntimeVisible
:
114 return CXCursor_ObjCRuntimeVisible
;
115 case attr::ObjCBoxable
:
116 return CXCursor_ObjCBoxable
;
118 return CXCursor_FlagEnum
;
119 case attr::Convergent
:
120 return CXCursor_ConvergentAttr
;
121 case attr::WarnUnused
:
122 return CXCursor_WarnUnusedAttr
;
123 case attr::WarnUnusedResult
:
124 return CXCursor_WarnUnusedResultAttr
;
126 return CXCursor_AlignedAttr
;
129 return CXCursor_UnexposedAttr
;
132 CXCursor
cxcursor::MakeCXCursor(const Attr
*A
, const Decl
*Parent
,
133 CXTranslationUnit TU
) {
134 assert(A
&& Parent
&& TU
&& "Invalid arguments!");
135 CXCursor C
= {GetCursorKind(A
), 0, {Parent
, A
, TU
}};
139 CXCursor
cxcursor::MakeCXCursor(const Decl
*D
, CXTranslationUnit TU
,
140 SourceRange RegionOfInterest
,
141 bool FirstInDeclGroup
) {
142 assert(D
&& TU
&& "Invalid arguments!");
144 CXCursorKind K
= getCursorKindForDecl(D
);
146 if (K
== CXCursor_ObjCClassMethodDecl
||
147 K
== CXCursor_ObjCInstanceMethodDecl
) {
148 int SelectorIdIndex
= -1;
149 // Check if cursor points to a selector id.
150 if (RegionOfInterest
.isValid() &&
151 RegionOfInterest
.getBegin() == RegionOfInterest
.getEnd()) {
152 SmallVector
<SourceLocation
, 16> SelLocs
;
153 cast
<ObjCMethodDecl
>(D
)->getSelectorLocs(SelLocs
);
154 SmallVectorImpl
<SourceLocation
>::iterator I
=
155 llvm::find(SelLocs
, RegionOfInterest
.getBegin());
156 if (I
!= SelLocs
.end())
157 SelectorIdIndex
= I
- SelLocs
.begin();
161 {D
, (void *)(intptr_t)(FirstInDeclGroup
? 1 : 0), TU
}};
165 CXCursor C
= {K
, 0, {D
, (void *)(intptr_t)(FirstInDeclGroup
? 1 : 0), TU
}};
169 CXCursor
cxcursor::MakeCXCursor(const Stmt
*S
, const Decl
*Parent
,
170 CXTranslationUnit TU
,
171 SourceRange RegionOfInterest
) {
172 assert(S
&& TU
&& "Invalid arguments!");
173 CXCursorKind K
= CXCursor_NotImplemented
;
175 switch (S
->getStmtClass()) {
176 case Stmt::NoStmtClass
:
179 case Stmt::CaseStmtClass
:
180 K
= CXCursor_CaseStmt
;
183 case Stmt::DefaultStmtClass
:
184 K
= CXCursor_DefaultStmt
;
187 case Stmt::IfStmtClass
:
191 case Stmt::SwitchStmtClass
:
192 K
= CXCursor_SwitchStmt
;
195 case Stmt::WhileStmtClass
:
196 K
= CXCursor_WhileStmt
;
199 case Stmt::DoStmtClass
:
203 case Stmt::ForStmtClass
:
204 K
= CXCursor_ForStmt
;
207 case Stmt::GotoStmtClass
:
208 K
= CXCursor_GotoStmt
;
211 case Stmt::IndirectGotoStmtClass
:
212 K
= CXCursor_IndirectGotoStmt
;
215 case Stmt::ContinueStmtClass
:
216 K
= CXCursor_ContinueStmt
;
219 case Stmt::BreakStmtClass
:
220 K
= CXCursor_BreakStmt
;
223 case Stmt::ReturnStmtClass
:
224 K
= CXCursor_ReturnStmt
;
227 case Stmt::GCCAsmStmtClass
:
228 K
= CXCursor_GCCAsmStmt
;
231 case Stmt::MSAsmStmtClass
:
232 K
= CXCursor_MSAsmStmt
;
235 case Stmt::ObjCAtTryStmtClass
:
236 K
= CXCursor_ObjCAtTryStmt
;
239 case Stmt::ObjCAtCatchStmtClass
:
240 K
= CXCursor_ObjCAtCatchStmt
;
243 case Stmt::ObjCAtFinallyStmtClass
:
244 K
= CXCursor_ObjCAtFinallyStmt
;
247 case Stmt::ObjCAtThrowStmtClass
:
248 K
= CXCursor_ObjCAtThrowStmt
;
251 case Stmt::ObjCAtSynchronizedStmtClass
:
252 K
= CXCursor_ObjCAtSynchronizedStmt
;
255 case Stmt::ObjCAutoreleasePoolStmtClass
:
256 K
= CXCursor_ObjCAutoreleasePoolStmt
;
259 case Stmt::ObjCForCollectionStmtClass
:
260 K
= CXCursor_ObjCForCollectionStmt
;
263 case Stmt::CXXCatchStmtClass
:
264 K
= CXCursor_CXXCatchStmt
;
267 case Stmt::CXXTryStmtClass
:
268 K
= CXCursor_CXXTryStmt
;
271 case Stmt::CXXForRangeStmtClass
:
272 K
= CXCursor_CXXForRangeStmt
;
275 case Stmt::SEHTryStmtClass
:
276 K
= CXCursor_SEHTryStmt
;
279 case Stmt::SEHExceptStmtClass
:
280 K
= CXCursor_SEHExceptStmt
;
283 case Stmt::SEHFinallyStmtClass
:
284 K
= CXCursor_SEHFinallyStmt
;
287 case Stmt::SEHLeaveStmtClass
:
288 K
= CXCursor_SEHLeaveStmt
;
291 case Stmt::CoroutineBodyStmtClass
:
292 case Stmt::CoreturnStmtClass
:
293 K
= CXCursor_UnexposedStmt
;
296 case Stmt::ArrayTypeTraitExprClass
:
297 case Stmt::AsTypeExprClass
:
298 case Stmt::AtomicExprClass
:
299 case Stmt::BinaryConditionalOperatorClass
:
300 case Stmt::TypeTraitExprClass
:
301 case Stmt::CoawaitExprClass
:
302 case Stmt::DependentCoawaitExprClass
:
303 case Stmt::CoyieldExprClass
:
304 case Stmt::CXXBindTemporaryExprClass
:
305 case Stmt::CXXDefaultArgExprClass
:
306 case Stmt::CXXDefaultInitExprClass
:
307 case Stmt::CXXFoldExprClass
:
308 case Stmt::CXXRewrittenBinaryOperatorClass
:
309 case Stmt::CXXStdInitializerListExprClass
:
310 case Stmt::CXXScalarValueInitExprClass
:
311 case Stmt::CXXUuidofExprClass
:
312 case Stmt::ChooseExprClass
:
313 case Stmt::DesignatedInitExprClass
:
314 case Stmt::DesignatedInitUpdateExprClass
:
315 case Stmt::ArrayInitLoopExprClass
:
316 case Stmt::ArrayInitIndexExprClass
:
317 case Stmt::ExprWithCleanupsClass
:
318 case Stmt::ExpressionTraitExprClass
:
319 case Stmt::ExtVectorElementExprClass
:
320 case Stmt::ImplicitCastExprClass
:
321 case Stmt::ImplicitValueInitExprClass
:
322 case Stmt::NoInitExprClass
:
323 case Stmt::MaterializeTemporaryExprClass
:
324 case Stmt::ObjCIndirectCopyRestoreExprClass
:
325 case Stmt::OffsetOfExprClass
:
326 case Stmt::ParenListExprClass
:
327 case Stmt::PredefinedExprClass
:
328 case Stmt::ShuffleVectorExprClass
:
329 case Stmt::SourceLocExprClass
:
330 case Stmt::ConvertVectorExprClass
:
331 case Stmt::VAArgExprClass
:
332 case Stmt::ObjCArrayLiteralClass
:
333 case Stmt::ObjCDictionaryLiteralClass
:
334 case Stmt::ObjCBoxedExprClass
:
335 case Stmt::ObjCSubscriptRefExprClass
:
336 case Stmt::RecoveryExprClass
:
337 case Stmt::SYCLUniqueStableNameExprClass
:
338 case Stmt::EmbedExprClass
:
339 case Stmt::HLSLOutArgExprClass
:
340 case Stmt::OpenACCAsteriskSizeExprClass
:
341 K
= CXCursor_UnexposedExpr
;
344 case Stmt::OpaqueValueExprClass
:
345 if (Expr
*Src
= cast
<OpaqueValueExpr
>(S
)->getSourceExpr())
346 return MakeCXCursor(Src
, Parent
, TU
, RegionOfInterest
);
347 K
= CXCursor_UnexposedExpr
;
350 case Stmt::PseudoObjectExprClass
:
351 return MakeCXCursor(cast
<PseudoObjectExpr
>(S
)->getSyntacticForm(), Parent
,
352 TU
, RegionOfInterest
);
354 case Stmt::CompoundStmtClass
:
355 K
= CXCursor_CompoundStmt
;
358 case Stmt::NullStmtClass
:
359 K
= CXCursor_NullStmt
;
362 case Stmt::LabelStmtClass
:
363 K
= CXCursor_LabelStmt
;
366 case Stmt::AttributedStmtClass
:
367 K
= CXCursor_UnexposedStmt
;
370 case Stmt::DeclStmtClass
:
371 K
= CXCursor_DeclStmt
;
374 case Stmt::CapturedStmtClass
:
375 K
= CXCursor_UnexposedStmt
;
378 case Stmt::IntegerLiteralClass
:
379 K
= CXCursor_IntegerLiteral
;
382 case Stmt::FixedPointLiteralClass
:
383 K
= CXCursor_FixedPointLiteral
;
386 case Stmt::FloatingLiteralClass
:
387 K
= CXCursor_FloatingLiteral
;
390 case Stmt::ImaginaryLiteralClass
:
391 K
= CXCursor_ImaginaryLiteral
;
394 case Stmt::StringLiteralClass
:
395 K
= CXCursor_StringLiteral
;
398 case Stmt::CharacterLiteralClass
:
399 K
= CXCursor_CharacterLiteral
;
402 case Stmt::ConstantExprClass
:
403 return MakeCXCursor(cast
<ConstantExpr
>(S
)->getSubExpr(), Parent
, TU
,
406 case Stmt::ParenExprClass
:
407 K
= CXCursor_ParenExpr
;
410 case Stmt::UnaryOperatorClass
:
411 K
= CXCursor_UnaryOperator
;
414 case Stmt::UnaryExprOrTypeTraitExprClass
:
415 case Stmt::CXXNoexceptExprClass
:
416 K
= CXCursor_UnaryExpr
;
419 case Stmt::MSPropertySubscriptExprClass
:
420 case Stmt::ArraySubscriptExprClass
:
421 K
= CXCursor_ArraySubscriptExpr
;
424 case Stmt::MatrixSubscriptExprClass
:
425 // TODO: add support for MatrixSubscriptExpr.
426 K
= CXCursor_UnexposedExpr
;
429 case Stmt::ArraySectionExprClass
:
430 K
= CXCursor_ArraySectionExpr
;
433 case Stmt::OMPArrayShapingExprClass
:
434 K
= CXCursor_OMPArrayShapingExpr
;
437 case Stmt::OMPIteratorExprClass
:
438 K
= CXCursor_OMPIteratorExpr
;
441 case Stmt::BinaryOperatorClass
:
442 K
= CXCursor_BinaryOperator
;
445 case Stmt::CompoundAssignOperatorClass
:
446 K
= CXCursor_CompoundAssignOperator
;
449 case Stmt::ConditionalOperatorClass
:
450 K
= CXCursor_ConditionalOperator
;
453 case Stmt::CStyleCastExprClass
:
454 K
= CXCursor_CStyleCastExpr
;
457 case Stmt::CompoundLiteralExprClass
:
458 K
= CXCursor_CompoundLiteralExpr
;
461 case Stmt::InitListExprClass
:
462 K
= CXCursor_InitListExpr
;
465 case Stmt::AddrLabelExprClass
:
466 K
= CXCursor_AddrLabelExpr
;
469 case Stmt::StmtExprClass
:
470 K
= CXCursor_StmtExpr
;
473 case Stmt::GenericSelectionExprClass
:
474 K
= CXCursor_GenericSelectionExpr
;
477 case Stmt::GNUNullExprClass
:
478 K
= CXCursor_GNUNullExpr
;
481 case Stmt::CXXStaticCastExprClass
:
482 K
= CXCursor_CXXStaticCastExpr
;
485 case Stmt::CXXDynamicCastExprClass
:
486 K
= CXCursor_CXXDynamicCastExpr
;
489 case Stmt::CXXReinterpretCastExprClass
:
490 K
= CXCursor_CXXReinterpretCastExpr
;
493 case Stmt::CXXConstCastExprClass
:
494 K
= CXCursor_CXXConstCastExpr
;
497 case Stmt::CXXFunctionalCastExprClass
:
498 K
= CXCursor_CXXFunctionalCastExpr
;
501 case Stmt::CXXAddrspaceCastExprClass
:
502 K
= CXCursor_CXXAddrspaceCastExpr
;
505 case Stmt::CXXTypeidExprClass
:
506 K
= CXCursor_CXXTypeidExpr
;
509 case Stmt::CXXBoolLiteralExprClass
:
510 K
= CXCursor_CXXBoolLiteralExpr
;
513 case Stmt::CXXNullPtrLiteralExprClass
:
514 K
= CXCursor_CXXNullPtrLiteralExpr
;
517 case Stmt::CXXThisExprClass
:
518 K
= CXCursor_CXXThisExpr
;
521 case Stmt::CXXThrowExprClass
:
522 K
= CXCursor_CXXThrowExpr
;
525 case Stmt::CXXNewExprClass
:
526 K
= CXCursor_CXXNewExpr
;
529 case Stmt::CXXDeleteExprClass
:
530 K
= CXCursor_CXXDeleteExpr
;
533 case Stmt::ObjCStringLiteralClass
:
534 K
= CXCursor_ObjCStringLiteral
;
537 case Stmt::ObjCEncodeExprClass
:
538 K
= CXCursor_ObjCEncodeExpr
;
541 case Stmt::ObjCSelectorExprClass
:
542 K
= CXCursor_ObjCSelectorExpr
;
545 case Stmt::ObjCProtocolExprClass
:
546 K
= CXCursor_ObjCProtocolExpr
;
549 case Stmt::ObjCBoolLiteralExprClass
:
550 K
= CXCursor_ObjCBoolLiteralExpr
;
553 case Stmt::ObjCAvailabilityCheckExprClass
:
554 K
= CXCursor_ObjCAvailabilityCheckExpr
;
557 case Stmt::ObjCBridgedCastExprClass
:
558 K
= CXCursor_ObjCBridgedCastExpr
;
561 case Stmt::BlockExprClass
:
562 K
= CXCursor_BlockExpr
;
565 case Stmt::PackExpansionExprClass
:
566 K
= CXCursor_PackExpansionExpr
;
569 case Stmt::SizeOfPackExprClass
:
570 K
= CXCursor_SizeOfPackExpr
;
573 case Stmt::PackIndexingExprClass
:
574 K
= CXCursor_PackIndexingExpr
;
577 case Stmt::DeclRefExprClass
:
578 if (const ImplicitParamDecl
*IPD
= dyn_cast_or_null
<ImplicitParamDecl
>(
579 cast
<DeclRefExpr
>(S
)->getDecl())) {
580 if (const ObjCMethodDecl
*MD
=
581 dyn_cast
<ObjCMethodDecl
>(IPD
->getDeclContext())) {
582 if (MD
->getSelfDecl() == IPD
) {
583 K
= CXCursor_ObjCSelfExpr
;
589 K
= CXCursor_DeclRefExpr
;
592 case Stmt::DependentScopeDeclRefExprClass
:
593 case Stmt::SubstNonTypeTemplateParmExprClass
:
594 case Stmt::SubstNonTypeTemplateParmPackExprClass
:
595 case Stmt::FunctionParmPackExprClass
:
596 case Stmt::UnresolvedLookupExprClass
:
597 case Stmt::TypoExprClass
: // A typo could actually be a DeclRef or a MemberRef
598 K
= CXCursor_DeclRefExpr
;
601 case Stmt::CXXDependentScopeMemberExprClass
:
602 case Stmt::CXXPseudoDestructorExprClass
:
603 case Stmt::MemberExprClass
:
604 case Stmt::MSPropertyRefExprClass
:
605 case Stmt::ObjCIsaExprClass
:
606 case Stmt::ObjCIvarRefExprClass
:
607 case Stmt::ObjCPropertyRefExprClass
:
608 case Stmt::UnresolvedMemberExprClass
:
609 K
= CXCursor_MemberRefExpr
;
612 case Stmt::CallExprClass
:
613 case Stmt::CXXOperatorCallExprClass
:
614 case Stmt::CXXMemberCallExprClass
:
615 case Stmt::CUDAKernelCallExprClass
:
616 case Stmt::CXXConstructExprClass
:
617 case Stmt::CXXInheritedCtorInitExprClass
:
618 case Stmt::CXXTemporaryObjectExprClass
:
619 case Stmt::CXXUnresolvedConstructExprClass
:
620 case Stmt::UserDefinedLiteralClass
:
621 K
= CXCursor_CallExpr
;
624 case Stmt::LambdaExprClass
:
625 K
= CXCursor_LambdaExpr
;
628 case Stmt::ObjCMessageExprClass
: {
629 K
= CXCursor_ObjCMessageExpr
;
630 int SelectorIdIndex
= -1;
631 // Check if cursor points to a selector id.
632 if (RegionOfInterest
.isValid() &&
633 RegionOfInterest
.getBegin() == RegionOfInterest
.getEnd()) {
634 SmallVector
<SourceLocation
, 16> SelLocs
;
635 cast
<ObjCMessageExpr
>(S
)->getSelectorLocs(SelLocs
);
636 SmallVectorImpl
<SourceLocation
>::iterator I
=
637 llvm::find(SelLocs
, RegionOfInterest
.getBegin());
638 if (I
!= SelLocs
.end())
639 SelectorIdIndex
= I
- SelLocs
.begin();
641 CXCursor C
= {K
, 0, {Parent
, S
, TU
}};
642 return getSelectorIdentifierCursor(SelectorIdIndex
, C
);
645 case Stmt::ConceptSpecializationExprClass
:
646 K
= CXCursor_ConceptSpecializationExpr
;
649 case Stmt::RequiresExprClass
:
650 K
= CXCursor_RequiresExpr
;
653 case Stmt::CXXParenListInitExprClass
:
654 K
= CXCursor_CXXParenListInitExpr
;
657 case Stmt::MSDependentExistsStmtClass
:
658 K
= CXCursor_UnexposedStmt
;
660 case Stmt::OMPCanonicalLoopClass
:
661 K
= CXCursor_OMPCanonicalLoop
;
663 case Stmt::OMPMetaDirectiveClass
:
664 K
= CXCursor_OMPMetaDirective
;
666 case Stmt::OMPParallelDirectiveClass
:
667 K
= CXCursor_OMPParallelDirective
;
669 case Stmt::OMPSimdDirectiveClass
:
670 K
= CXCursor_OMPSimdDirective
;
672 case Stmt::OMPTileDirectiveClass
:
673 K
= CXCursor_OMPTileDirective
;
675 case Stmt::OMPUnrollDirectiveClass
:
676 K
= CXCursor_OMPUnrollDirective
;
678 case Stmt::OMPReverseDirectiveClass
:
679 K
= CXCursor_OMPReverseDirective
;
681 case Stmt::OMPInterchangeDirectiveClass
:
682 K
= CXCursor_OMPTileDirective
;
684 case Stmt::OMPForDirectiveClass
:
685 K
= CXCursor_OMPForDirective
;
687 case Stmt::OMPForSimdDirectiveClass
:
688 K
= CXCursor_OMPForSimdDirective
;
690 case Stmt::OMPSectionsDirectiveClass
:
691 K
= CXCursor_OMPSectionsDirective
;
693 case Stmt::OMPSectionDirectiveClass
:
694 K
= CXCursor_OMPSectionDirective
;
696 case Stmt::OMPScopeDirectiveClass
:
697 K
= CXCursor_OMPScopeDirective
;
699 case Stmt::OMPSingleDirectiveClass
:
700 K
= CXCursor_OMPSingleDirective
;
702 case Stmt::OMPMasterDirectiveClass
:
703 K
= CXCursor_OMPMasterDirective
;
705 case Stmt::OMPCriticalDirectiveClass
:
706 K
= CXCursor_OMPCriticalDirective
;
708 case Stmt::OMPParallelForDirectiveClass
:
709 K
= CXCursor_OMPParallelForDirective
;
711 case Stmt::OMPParallelForSimdDirectiveClass
:
712 K
= CXCursor_OMPParallelForSimdDirective
;
714 case Stmt::OMPParallelMasterDirectiveClass
:
715 K
= CXCursor_OMPParallelMasterDirective
;
717 case Stmt::OMPParallelMaskedDirectiveClass
:
718 K
= CXCursor_OMPParallelMaskedDirective
;
720 case Stmt::OMPParallelSectionsDirectiveClass
:
721 K
= CXCursor_OMPParallelSectionsDirective
;
723 case Stmt::OMPTaskDirectiveClass
:
724 K
= CXCursor_OMPTaskDirective
;
726 case Stmt::OMPTaskyieldDirectiveClass
:
727 K
= CXCursor_OMPTaskyieldDirective
;
729 case Stmt::OMPBarrierDirectiveClass
:
730 K
= CXCursor_OMPBarrierDirective
;
732 case Stmt::OMPTaskwaitDirectiveClass
:
733 K
= CXCursor_OMPTaskwaitDirective
;
735 case Stmt::OMPErrorDirectiveClass
:
736 K
= CXCursor_OMPErrorDirective
;
738 case Stmt::OMPTaskgroupDirectiveClass
:
739 K
= CXCursor_OMPTaskgroupDirective
;
741 case Stmt::OMPFlushDirectiveClass
:
742 K
= CXCursor_OMPFlushDirective
;
744 case Stmt::OMPDepobjDirectiveClass
:
745 K
= CXCursor_OMPDepobjDirective
;
747 case Stmt::OMPScanDirectiveClass
:
748 K
= CXCursor_OMPScanDirective
;
750 case Stmt::OMPOrderedDirectiveClass
:
751 K
= CXCursor_OMPOrderedDirective
;
753 case Stmt::OMPAtomicDirectiveClass
:
754 K
= CXCursor_OMPAtomicDirective
;
756 case Stmt::OMPTargetDirectiveClass
:
757 K
= CXCursor_OMPTargetDirective
;
759 case Stmt::OMPTargetDataDirectiveClass
:
760 K
= CXCursor_OMPTargetDataDirective
;
762 case Stmt::OMPTargetEnterDataDirectiveClass
:
763 K
= CXCursor_OMPTargetEnterDataDirective
;
765 case Stmt::OMPTargetExitDataDirectiveClass
:
766 K
= CXCursor_OMPTargetExitDataDirective
;
768 case Stmt::OMPTargetParallelDirectiveClass
:
769 K
= CXCursor_OMPTargetParallelDirective
;
771 case Stmt::OMPTargetParallelForDirectiveClass
:
772 K
= CXCursor_OMPTargetParallelForDirective
;
774 case Stmt::OMPTargetUpdateDirectiveClass
:
775 K
= CXCursor_OMPTargetUpdateDirective
;
777 case Stmt::OMPTeamsDirectiveClass
:
778 K
= CXCursor_OMPTeamsDirective
;
780 case Stmt::OMPCancellationPointDirectiveClass
:
781 K
= CXCursor_OMPCancellationPointDirective
;
783 case Stmt::OMPCancelDirectiveClass
:
784 K
= CXCursor_OMPCancelDirective
;
786 case Stmt::OMPTaskLoopDirectiveClass
:
787 K
= CXCursor_OMPTaskLoopDirective
;
789 case Stmt::OMPTaskLoopSimdDirectiveClass
:
790 K
= CXCursor_OMPTaskLoopSimdDirective
;
792 case Stmt::OMPMasterTaskLoopDirectiveClass
:
793 K
= CXCursor_OMPMasterTaskLoopDirective
;
795 case Stmt::OMPMaskedTaskLoopDirectiveClass
:
796 K
= CXCursor_OMPMaskedTaskLoopDirective
;
798 case Stmt::OMPMasterTaskLoopSimdDirectiveClass
:
799 K
= CXCursor_OMPMasterTaskLoopSimdDirective
;
801 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass
:
802 K
= CXCursor_OMPMaskedTaskLoopSimdDirective
;
804 case Stmt::OMPParallelMasterTaskLoopDirectiveClass
:
805 K
= CXCursor_OMPParallelMasterTaskLoopDirective
;
807 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass
:
808 K
= CXCursor_OMPParallelMaskedTaskLoopDirective
;
810 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass
:
811 K
= CXCursor_OMPParallelMasterTaskLoopSimdDirective
;
813 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass
:
814 K
= CXCursor_OMPParallelMaskedTaskLoopSimdDirective
;
816 case Stmt::OMPDistributeDirectiveClass
:
817 K
= CXCursor_OMPDistributeDirective
;
819 case Stmt::OMPDistributeParallelForDirectiveClass
:
820 K
= CXCursor_OMPDistributeParallelForDirective
;
822 case Stmt::OMPDistributeParallelForSimdDirectiveClass
:
823 K
= CXCursor_OMPDistributeParallelForSimdDirective
;
825 case Stmt::OMPDistributeSimdDirectiveClass
:
826 K
= CXCursor_OMPDistributeSimdDirective
;
828 case Stmt::OMPTargetParallelForSimdDirectiveClass
:
829 K
= CXCursor_OMPTargetParallelForSimdDirective
;
831 case Stmt::OMPTargetSimdDirectiveClass
:
832 K
= CXCursor_OMPTargetSimdDirective
;
834 case Stmt::OMPTeamsDistributeDirectiveClass
:
835 K
= CXCursor_OMPTeamsDistributeDirective
;
837 case Stmt::OMPTeamsDistributeSimdDirectiveClass
:
838 K
= CXCursor_OMPTeamsDistributeSimdDirective
;
840 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass
:
841 K
= CXCursor_OMPTeamsDistributeParallelForSimdDirective
;
843 case Stmt::OMPTeamsDistributeParallelForDirectiveClass
:
844 K
= CXCursor_OMPTeamsDistributeParallelForDirective
;
846 case Stmt::OMPTargetTeamsDirectiveClass
:
847 K
= CXCursor_OMPTargetTeamsDirective
;
849 case Stmt::OMPTargetTeamsDistributeDirectiveClass
:
850 K
= CXCursor_OMPTargetTeamsDistributeDirective
;
852 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass
:
853 K
= CXCursor_OMPTargetTeamsDistributeParallelForDirective
;
855 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass
:
856 K
= CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective
;
858 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass
:
859 K
= CXCursor_OMPTargetTeamsDistributeSimdDirective
;
861 case Stmt::OMPInteropDirectiveClass
:
862 K
= CXCursor_OMPInteropDirective
;
864 case Stmt::OMPDispatchDirectiveClass
:
865 K
= CXCursor_OMPDispatchDirective
;
867 case Stmt::OMPMaskedDirectiveClass
:
868 K
= CXCursor_OMPMaskedDirective
;
870 case Stmt::OMPGenericLoopDirectiveClass
:
871 K
= CXCursor_OMPGenericLoopDirective
;
873 case Stmt::OMPTeamsGenericLoopDirectiveClass
:
874 K
= CXCursor_OMPTeamsGenericLoopDirective
;
876 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass
:
877 K
= CXCursor_OMPTargetTeamsGenericLoopDirective
;
879 case Stmt::OMPParallelGenericLoopDirectiveClass
:
880 K
= CXCursor_OMPParallelGenericLoopDirective
;
882 case Stmt::OpenACCComputeConstructClass
:
883 K
= CXCursor_OpenACCComputeConstruct
;
885 case Stmt::OpenACCLoopConstructClass
:
886 K
= CXCursor_OpenACCLoopConstruct
;
888 case Stmt::OpenACCCombinedConstructClass
:
889 K
= CXCursor_OpenACCCombinedConstruct
;
891 case Stmt::OMPTargetParallelGenericLoopDirectiveClass
:
892 K
= CXCursor_OMPTargetParallelGenericLoopDirective
;
894 case Stmt::BuiltinBitCastExprClass
:
895 K
= CXCursor_BuiltinBitCastExpr
;
897 case Stmt::OMPAssumeDirectiveClass
:
898 K
= CXCursor_OMPAssumeDirective
;
902 CXCursor C
= {K
, 0, {Parent
, S
, TU
}};
906 CXCursor
cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl
*Super
,
908 CXTranslationUnit TU
) {
909 assert(Super
&& TU
&& "Invalid arguments!");
910 void *RawLoc
= Loc
.getPtrEncoding();
911 CXCursor C
= {CXCursor_ObjCSuperClassRef
, 0, {Super
, RawLoc
, TU
}};
915 std::pair
<const ObjCInterfaceDecl
*, SourceLocation
>
916 cxcursor::getCursorObjCSuperClassRef(CXCursor C
) {
917 assert(C
.kind
== CXCursor_ObjCSuperClassRef
);
918 return std::make_pair(static_cast<const ObjCInterfaceDecl
*>(C
.data
[0]),
919 SourceLocation::getFromPtrEncoding(C
.data
[1]));
922 CXCursor
cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl
*Proto
,
924 CXTranslationUnit TU
) {
925 assert(Proto
&& TU
&& "Invalid arguments!");
926 void *RawLoc
= Loc
.getPtrEncoding();
927 CXCursor C
= {CXCursor_ObjCProtocolRef
, 0, {Proto
, RawLoc
, TU
}};
931 std::pair
<const ObjCProtocolDecl
*, SourceLocation
>
932 cxcursor::getCursorObjCProtocolRef(CXCursor C
) {
933 assert(C
.kind
== CXCursor_ObjCProtocolRef
);
934 return std::make_pair(static_cast<const ObjCProtocolDecl
*>(C
.data
[0]),
935 SourceLocation::getFromPtrEncoding(C
.data
[1]));
938 CXCursor
cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl
*Class
,
940 CXTranslationUnit TU
) {
941 // 'Class' can be null for invalid code.
943 return MakeCXCursorInvalid(CXCursor_InvalidCode
);
944 assert(TU
&& "Invalid arguments!");
945 void *RawLoc
= Loc
.getPtrEncoding();
946 CXCursor C
= {CXCursor_ObjCClassRef
, 0, {Class
, RawLoc
, TU
}};
950 std::pair
<const ObjCInterfaceDecl
*, SourceLocation
>
951 cxcursor::getCursorObjCClassRef(CXCursor C
) {
952 assert(C
.kind
== CXCursor_ObjCClassRef
);
953 return std::make_pair(static_cast<const ObjCInterfaceDecl
*>(C
.data
[0]),
954 SourceLocation::getFromPtrEncoding(C
.data
[1]));
957 CXCursor
cxcursor::MakeCursorTypeRef(const TypeDecl
*Type
, SourceLocation Loc
,
958 CXTranslationUnit TU
) {
959 assert(Type
&& TU
&& "Invalid arguments!");
960 void *RawLoc
= Loc
.getPtrEncoding();
961 CXCursor C
= {CXCursor_TypeRef
, 0, {Type
, RawLoc
, TU
}};
965 std::pair
<const TypeDecl
*, SourceLocation
>
966 cxcursor::getCursorTypeRef(CXCursor C
) {
967 assert(C
.kind
== CXCursor_TypeRef
);
968 return std::make_pair(static_cast<const TypeDecl
*>(C
.data
[0]),
969 SourceLocation::getFromPtrEncoding(C
.data
[1]));
972 CXCursor
cxcursor::MakeCursorTemplateRef(const TemplateDecl
*Template
,
974 CXTranslationUnit TU
) {
975 assert(Template
&& TU
&& "Invalid arguments!");
976 void *RawLoc
= Loc
.getPtrEncoding();
977 CXCursor C
= {CXCursor_TemplateRef
, 0, {Template
, RawLoc
, TU
}};
981 std::pair
<const TemplateDecl
*, SourceLocation
>
982 cxcursor::getCursorTemplateRef(CXCursor C
) {
983 assert(C
.kind
== CXCursor_TemplateRef
);
984 return std::make_pair(static_cast<const TemplateDecl
*>(C
.data
[0]),
985 SourceLocation::getFromPtrEncoding(C
.data
[1]));
988 CXCursor
cxcursor::MakeCursorNamespaceRef(const NamedDecl
*NS
,
990 CXTranslationUnit TU
) {
992 assert(NS
&& (isa
<NamespaceDecl
>(NS
) || isa
<NamespaceAliasDecl
>(NS
)) && TU
&&
993 "Invalid arguments!");
994 void *RawLoc
= Loc
.getPtrEncoding();
995 CXCursor C
= {CXCursor_NamespaceRef
, 0, {NS
, RawLoc
, TU
}};
999 std::pair
<const NamedDecl
*, SourceLocation
>
1000 cxcursor::getCursorNamespaceRef(CXCursor C
) {
1001 assert(C
.kind
== CXCursor_NamespaceRef
);
1002 return std::make_pair(static_cast<const NamedDecl
*>(C
.data
[0]),
1003 SourceLocation::getFromPtrEncoding(C
.data
[1]));
1006 CXCursor
cxcursor::MakeCursorVariableRef(const VarDecl
*Var
, SourceLocation Loc
,
1007 CXTranslationUnit TU
) {
1009 assert(Var
&& TU
&& "Invalid arguments!");
1010 void *RawLoc
= Loc
.getPtrEncoding();
1011 CXCursor C
= {CXCursor_VariableRef
, 0, {Var
, RawLoc
, TU
}};
1015 std::pair
<const VarDecl
*, SourceLocation
>
1016 cxcursor::getCursorVariableRef(CXCursor C
) {
1017 assert(C
.kind
== CXCursor_VariableRef
);
1018 return std::make_pair(static_cast<const VarDecl
*>(C
.data
[0]),
1019 SourceLocation::getFromPtrEncoding(C
.data
[1]));
1022 CXCursor
cxcursor::MakeCursorMemberRef(const FieldDecl
*Field
,
1024 CXTranslationUnit TU
) {
1026 assert(Field
&& TU
&& "Invalid arguments!");
1027 void *RawLoc
= Loc
.getPtrEncoding();
1028 CXCursor C
= {CXCursor_MemberRef
, 0, {Field
, RawLoc
, TU
}};
1032 std::pair
<const FieldDecl
*, SourceLocation
>
1033 cxcursor::getCursorMemberRef(CXCursor C
) {
1034 assert(C
.kind
== CXCursor_MemberRef
);
1035 return std::make_pair(static_cast<const FieldDecl
*>(C
.data
[0]),
1036 SourceLocation::getFromPtrEncoding(C
.data
[1]));
1039 CXCursor
cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier
*B
,
1040 CXTranslationUnit TU
) {
1041 CXCursor C
= {CXCursor_CXXBaseSpecifier
, 0, {B
, nullptr, TU
}};
1045 const CXXBaseSpecifier
*cxcursor::getCursorCXXBaseSpecifier(CXCursor C
) {
1046 assert(C
.kind
== CXCursor_CXXBaseSpecifier
);
1047 return static_cast<const CXXBaseSpecifier
*>(C
.data
[0]);
1050 CXCursor
cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range
,
1051 CXTranslationUnit TU
) {
1053 CXCursor_PreprocessingDirective
,
1055 {Range
.getBegin().getPtrEncoding(), Range
.getEnd().getPtrEncoding(), TU
}};
1059 SourceRange
cxcursor::getCursorPreprocessingDirective(CXCursor C
) {
1060 assert(C
.kind
== CXCursor_PreprocessingDirective
);
1061 SourceRange
Range(SourceLocation::getFromPtrEncoding(C
.data
[0]),
1062 SourceLocation::getFromPtrEncoding(C
.data
[1]));
1063 ASTUnit
*TU
= getCursorASTUnit(C
);
1064 return TU
->mapRangeFromPreamble(Range
);
1067 CXCursor
cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord
*MI
,
1068 CXTranslationUnit TU
) {
1069 CXCursor C
= {CXCursor_MacroDefinition
, 0, {MI
, nullptr, TU
}};
1073 const MacroDefinitionRecord
*cxcursor::getCursorMacroDefinition(CXCursor C
) {
1074 assert(C
.kind
== CXCursor_MacroDefinition
);
1075 return static_cast<const MacroDefinitionRecord
*>(C
.data
[0]);
1078 CXCursor
cxcursor::MakeMacroExpansionCursor(MacroExpansion
*MI
,
1079 CXTranslationUnit TU
) {
1080 CXCursor C
= {CXCursor_MacroExpansion
, 0, {MI
, nullptr, TU
}};
1084 CXCursor
cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord
*MI
,
1086 CXTranslationUnit TU
) {
1087 assert(Loc
.isValid());
1088 CXCursor C
= {CXCursor_MacroExpansion
, 0, {MI
, Loc
.getPtrEncoding(), TU
}};
1092 const IdentifierInfo
*cxcursor::MacroExpansionCursor::getName() const {
1094 return getAsMacroDefinition()->getName();
1095 return getAsMacroExpansion()->getName();
1097 const MacroDefinitionRecord
*
1098 cxcursor::MacroExpansionCursor::getDefinition() const {
1100 return getAsMacroDefinition();
1101 return getAsMacroExpansion()->getDefinition();
1103 SourceRange
cxcursor::MacroExpansionCursor::getSourceRange() const {
1105 return getPseudoLoc();
1106 return getAsMacroExpansion()->getSourceRange();
1109 CXCursor
cxcursor::MakeInclusionDirectiveCursor(InclusionDirective
*ID
,
1110 CXTranslationUnit TU
) {
1111 CXCursor C
= {CXCursor_InclusionDirective
, 0, {ID
, nullptr, TU
}};
1115 const InclusionDirective
*cxcursor::getCursorInclusionDirective(CXCursor C
) {
1116 assert(C
.kind
== CXCursor_InclusionDirective
);
1117 return static_cast<const InclusionDirective
*>(C
.data
[0]);
1120 CXCursor
cxcursor::MakeCursorLabelRef(LabelStmt
*Label
, SourceLocation Loc
,
1121 CXTranslationUnit TU
) {
1123 assert(Label
&& TU
&& "Invalid arguments!");
1124 void *RawLoc
= Loc
.getPtrEncoding();
1125 CXCursor C
= {CXCursor_LabelRef
, 0, {Label
, RawLoc
, TU
}};
1129 std::pair
<const LabelStmt
*, SourceLocation
>
1130 cxcursor::getCursorLabelRef(CXCursor C
) {
1131 assert(C
.kind
== CXCursor_LabelRef
);
1132 return std::make_pair(static_cast<const LabelStmt
*>(C
.data
[0]),
1133 SourceLocation::getFromPtrEncoding(C
.data
[1]));
1136 CXCursor
cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr
*E
,
1137 CXTranslationUnit TU
) {
1138 assert(E
&& TU
&& "Invalid arguments!");
1139 OverloadedDeclRefStorage
Storage(E
);
1140 void *RawLoc
= E
->getNameLoc().getPtrEncoding();
1142 CXCursor_OverloadedDeclRef
, 0, {Storage
.getOpaqueValue(), RawLoc
, TU
}};
1146 CXCursor
cxcursor::MakeCursorOverloadedDeclRef(const Decl
*D
,
1148 CXTranslationUnit TU
) {
1149 assert(D
&& TU
&& "Invalid arguments!");
1150 void *RawLoc
= Loc
.getPtrEncoding();
1151 OverloadedDeclRefStorage
Storage(D
);
1153 CXCursor_OverloadedDeclRef
, 0, {Storage
.getOpaqueValue(), RawLoc
, TU
}};
1157 CXCursor
cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name
,
1159 CXTranslationUnit TU
) {
1160 assert(Name
.getAsOverloadedTemplate() && TU
&& "Invalid arguments!");
1161 void *RawLoc
= Loc
.getPtrEncoding();
1162 OverloadedDeclRefStorage
Storage(Name
.getAsOverloadedTemplate());
1164 CXCursor_OverloadedDeclRef
, 0, {Storage
.getOpaqueValue(), RawLoc
, TU
}};
1168 std::pair
<cxcursor::OverloadedDeclRefStorage
, SourceLocation
>
1169 cxcursor::getCursorOverloadedDeclRef(CXCursor C
) {
1170 assert(C
.kind
== CXCursor_OverloadedDeclRef
);
1171 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
1172 const_cast<void *>(C
.data
[0])),
1173 SourceLocation::getFromPtrEncoding(C
.data
[1]));
1176 const Decl
*cxcursor::getCursorDecl(CXCursor Cursor
) {
1177 return static_cast<const Decl
*>(Cursor
.data
[0]);
1180 const Expr
*cxcursor::getCursorExpr(CXCursor Cursor
) {
1181 return dyn_cast_or_null
<Expr
>(getCursorStmt(Cursor
));
1184 const Stmt
*cxcursor::getCursorStmt(CXCursor Cursor
) {
1185 if (Cursor
.kind
== CXCursor_ObjCSuperClassRef
||
1186 Cursor
.kind
== CXCursor_ObjCProtocolRef
||
1187 Cursor
.kind
== CXCursor_ObjCClassRef
)
1190 return static_cast<const Stmt
*>(Cursor
.data
[1]);
1193 const Attr
*cxcursor::getCursorAttr(CXCursor Cursor
) {
1194 return static_cast<const Attr
*>(Cursor
.data
[1]);
1197 ASTContext
&cxcursor::getCursorContext(CXCursor Cursor
) {
1198 return getCursorASTUnit(Cursor
)->getASTContext();
1201 ASTUnit
*cxcursor::getCursorASTUnit(CXCursor Cursor
) {
1202 CXTranslationUnit TU
= getCursorTU(Cursor
);
1205 return cxtu::getASTUnit(TU
);
1208 CXTranslationUnit
cxcursor::getCursorTU(CXCursor Cursor
) {
1209 return static_cast<CXTranslationUnit
>(const_cast<void *>(Cursor
.data
[2]));
1212 void cxcursor::getOverriddenCursors(CXCursor cursor
,
1213 SmallVectorImpl
<CXCursor
> &overridden
) {
1214 assert(clang_isDeclaration(cursor
.kind
));
1215 const NamedDecl
*D
= dyn_cast_or_null
<NamedDecl
>(getCursorDecl(cursor
));
1219 CXTranslationUnit TU
= getCursorTU(cursor
);
1220 SmallVector
<const NamedDecl
*, 8> OverDecls
;
1221 D
->getASTContext().getOverriddenMethods(D
, OverDecls
);
1223 for (SmallVectorImpl
<const NamedDecl
*>::iterator I
= OverDecls
.begin(),
1224 E
= OverDecls
.end();
1226 overridden
.push_back(MakeCXCursor(*I
, TU
));
1230 std::pair
<int, SourceLocation
>
1231 cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor
) {
1232 if (cursor
.kind
== CXCursor_ObjCMessageExpr
) {
1233 if (cursor
.xdata
!= -1)
1234 return std::make_pair(cursor
.xdata
,
1235 cast
<ObjCMessageExpr
>(getCursorExpr(cursor
))
1236 ->getSelectorLoc(cursor
.xdata
));
1237 } else if (cursor
.kind
== CXCursor_ObjCClassMethodDecl
||
1238 cursor
.kind
== CXCursor_ObjCInstanceMethodDecl
) {
1239 if (cursor
.xdata
!= -1)
1240 return std::make_pair(cursor
.xdata
,
1241 cast
<ObjCMethodDecl
>(getCursorDecl(cursor
))
1242 ->getSelectorLoc(cursor
.xdata
));
1245 return std::make_pair(-1, SourceLocation());
1248 CXCursor
cxcursor::getSelectorIdentifierCursor(int SelIdx
, CXCursor cursor
) {
1249 CXCursor newCursor
= cursor
;
1251 if (cursor
.kind
== CXCursor_ObjCMessageExpr
) {
1254 cast
<ObjCMessageExpr
>(getCursorExpr(cursor
))->getNumSelectorLocs())
1255 newCursor
.xdata
= -1;
1257 newCursor
.xdata
= SelIdx
;
1258 } else if (cursor
.kind
== CXCursor_ObjCClassMethodDecl
||
1259 cursor
.kind
== CXCursor_ObjCInstanceMethodDecl
) {
1262 cast
<ObjCMethodDecl
>(getCursorDecl(cursor
))->getNumSelectorLocs())
1263 newCursor
.xdata
= -1;
1265 newCursor
.xdata
= SelIdx
;
1271 CXCursor
cxcursor::getTypeRefCursor(CXCursor cursor
) {
1272 if (cursor
.kind
!= CXCursor_CallExpr
)
1275 if (cursor
.xdata
== 0)
1278 const Expr
*E
= getCursorExpr(cursor
);
1279 TypeSourceInfo
*Type
= nullptr;
1280 if (const CXXUnresolvedConstructExpr
*UnCtor
=
1281 dyn_cast
<CXXUnresolvedConstructExpr
>(E
)) {
1282 Type
= UnCtor
->getTypeSourceInfo();
1283 } else if (const CXXTemporaryObjectExpr
*Tmp
=
1284 dyn_cast
<CXXTemporaryObjectExpr
>(E
)) {
1285 Type
= Tmp
->getTypeSourceInfo();
1291 CXTranslationUnit TU
= getCursorTU(cursor
);
1292 QualType Ty
= Type
->getType();
1293 TypeLoc TL
= Type
->getTypeLoc();
1294 SourceLocation Loc
= TL
.getBeginLoc();
1296 if (const ElaboratedType
*ElabT
= Ty
->getAs
<ElaboratedType
>()) {
1297 Ty
= ElabT
->getNamedType();
1298 ElaboratedTypeLoc ElabTL
= TL
.castAs
<ElaboratedTypeLoc
>();
1299 Loc
= ElabTL
.getNamedTypeLoc().getBeginLoc();
1302 if (const TypedefType
*Typedef
= Ty
->getAs
<TypedefType
>())
1303 return MakeCursorTypeRef(Typedef
->getDecl(), Loc
, TU
);
1304 if (const TagType
*Tag
= Ty
->getAs
<TagType
>())
1305 return MakeCursorTypeRef(Tag
->getDecl(), Loc
, TU
);
1306 if (const TemplateTypeParmType
*TemplP
= Ty
->getAs
<TemplateTypeParmType
>())
1307 return MakeCursorTypeRef(TemplP
->getDecl(), Loc
, TU
);
1312 bool cxcursor::operator==(CXCursor X
, CXCursor Y
) {
1313 return X
.kind
== Y
.kind
&& X
.data
[0] == Y
.data
[0] && X
.data
[1] == Y
.data
[1] &&
1314 X
.data
[2] == Y
.data
[2];
1317 // FIXME: Remove once we can model DeclGroups and their appropriate ranges
1318 // properly in the ASTs.
1319 bool cxcursor::isFirstInDeclGroup(CXCursor C
) {
1320 assert(clang_isDeclaration(C
.kind
));
1321 return ((uintptr_t)(C
.data
[1])) != 0;
1324 //===----------------------------------------------------------------------===//
1325 // libclang CXCursor APIs
1326 //===----------------------------------------------------------------------===//
1328 int clang_Cursor_isNull(CXCursor cursor
) {
1329 return clang_equalCursors(cursor
, clang_getNullCursor());
1332 CXTranslationUnit
clang_Cursor_getTranslationUnit(CXCursor cursor
) {
1333 return getCursorTU(cursor
);
1336 int clang_Cursor_getNumArguments(CXCursor C
) {
1337 if (clang_isDeclaration(C
.kind
)) {
1338 const Decl
*D
= cxcursor::getCursorDecl(C
);
1339 if (const ObjCMethodDecl
*MD
= dyn_cast_or_null
<ObjCMethodDecl
>(D
))
1340 return MD
->param_size();
1341 if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(D
))
1342 return FD
->param_size();
1345 if (clang_isExpression(C
.kind
)) {
1346 const Expr
*E
= cxcursor::getCursorExpr(C
);
1347 if (const CallExpr
*CE
= dyn_cast
<CallExpr
>(E
)) {
1348 return CE
->getNumArgs();
1350 if (const CXXConstructExpr
*CE
= dyn_cast
<CXXConstructExpr
>(E
)) {
1351 return CE
->getNumArgs();
1358 CXCursor
clang_Cursor_getArgument(CXCursor C
, unsigned i
) {
1359 if (clang_isDeclaration(C
.kind
)) {
1360 const Decl
*D
= cxcursor::getCursorDecl(C
);
1361 if (const ObjCMethodDecl
*MD
= dyn_cast_or_null
<ObjCMethodDecl
>(D
)) {
1362 if (i
< MD
->param_size())
1363 return cxcursor::MakeCXCursor(MD
->parameters()[i
],
1364 cxcursor::getCursorTU(C
));
1365 } else if (const FunctionDecl
*FD
= dyn_cast_or_null
<FunctionDecl
>(D
)) {
1366 if (i
< FD
->param_size())
1367 return cxcursor::MakeCXCursor(FD
->parameters()[i
],
1368 cxcursor::getCursorTU(C
));
1372 if (clang_isExpression(C
.kind
)) {
1373 const Expr
*E
= cxcursor::getCursorExpr(C
);
1374 if (const CallExpr
*CE
= dyn_cast
<CallExpr
>(E
)) {
1375 if (i
< CE
->getNumArgs()) {
1376 return cxcursor::MakeCXCursor(CE
->getArg(i
), getCursorDecl(C
),
1377 cxcursor::getCursorTU(C
));
1380 if (const CXXConstructExpr
*CE
= dyn_cast
<CXXConstructExpr
>(E
)) {
1381 if (i
< CE
->getNumArgs()) {
1382 return cxcursor::MakeCXCursor(CE
->getArg(i
), getCursorDecl(C
),
1383 cxcursor::getCursorTU(C
));
1388 return clang_getNullCursor();
1391 int clang_Cursor_getNumTemplateArguments(CXCursor C
) {
1392 CXCursorKind kind
= clang_getCursorKind(C
);
1393 if (kind
!= CXCursor_FunctionDecl
&& kind
!= CXCursor_StructDecl
&&
1394 kind
!= CXCursor_ClassDecl
&&
1395 kind
!= CXCursor_ClassTemplatePartialSpecialization
) {
1399 if (const auto *FD
=
1400 llvm::dyn_cast_if_present
<clang::FunctionDecl
>(getCursorDecl(C
))) {
1401 const FunctionTemplateSpecializationInfo
*SpecInfo
=
1402 FD
->getTemplateSpecializationInfo();
1406 return SpecInfo
->TemplateArguments
->size();
1409 if (const auto *SD
=
1410 llvm::dyn_cast_if_present
<clang::ClassTemplateSpecializationDecl
>(
1411 getCursorDecl(C
))) {
1412 return SD
->getTemplateArgs().size();
1418 enum CXGetTemplateArgumentStatus
{
1419 /** The operation completed successfully */
1420 CXGetTemplateArgumentStatus_Success
= 0,
1422 /** The specified cursor did not represent a FunctionDecl or
1423 ClassTemplateSpecializationDecl. */
1424 CXGetTemplateArgumentStatus_CursorNotCompatibleDecl
= -1,
1426 /** The specified cursor was not castable to a FunctionDecl or
1427 ClassTemplateSpecializationDecl. */
1428 CXGetTemplateArgumentStatus_BadDeclCast
= -2,
1430 /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
1431 CXGetTemplateArgumentStatus_NullTemplSpecInfo
= -3,
1433 /** An invalid (OOB) argument index was specified */
1434 CXGetTemplateArgumentStatus_InvalidIndex
= -4
1437 static int clang_Cursor_getTemplateArgument(CXCursor C
, unsigned I
,
1438 TemplateArgument
*TA
) {
1439 CXCursorKind kind
= clang_getCursorKind(C
);
1440 if (kind
!= CXCursor_FunctionDecl
&& kind
!= CXCursor_StructDecl
&&
1441 kind
!= CXCursor_ClassDecl
&&
1442 kind
!= CXCursor_ClassTemplatePartialSpecialization
) {
1446 if (const auto *FD
=
1447 llvm::dyn_cast_if_present
<clang::FunctionDecl
>(getCursorDecl(C
))) {
1449 const FunctionTemplateSpecializationInfo
*SpecInfo
=
1450 FD
->getTemplateSpecializationInfo();
1452 return CXGetTemplateArgumentStatus_NullTemplSpecInfo
;
1455 if (I
>= SpecInfo
->TemplateArguments
->size()) {
1456 return CXGetTemplateArgumentStatus_InvalidIndex
;
1459 *TA
= SpecInfo
->TemplateArguments
->get(I
);
1463 if (const auto *SD
=
1464 llvm::dyn_cast_if_present
<clang::ClassTemplateSpecializationDecl
>(
1465 getCursorDecl(C
))) {
1466 if (I
>= SD
->getTemplateArgs().size()) {
1467 return CXGetTemplateArgumentStatus_InvalidIndex
;
1470 *TA
= SD
->getTemplateArgs()[I
];
1474 return CXGetTemplateArgumentStatus_BadDeclCast
;
1477 enum CXTemplateArgumentKind
clang_Cursor_getTemplateArgumentKind(CXCursor C
,
1479 TemplateArgument TA
;
1480 if (clang_Cursor_getTemplateArgument(C
, I
, &TA
)) {
1481 return CXTemplateArgumentKind_Invalid
;
1484 switch (TA
.getKind()) {
1485 case TemplateArgument::Null
:
1486 return CXTemplateArgumentKind_Null
;
1487 case TemplateArgument::Type
:
1488 return CXTemplateArgumentKind_Type
;
1489 case TemplateArgument::Declaration
:
1490 return CXTemplateArgumentKind_Declaration
;
1491 case TemplateArgument::NullPtr
:
1492 return CXTemplateArgumentKind_NullPtr
;
1493 case TemplateArgument::Integral
:
1494 return CXTemplateArgumentKind_Integral
;
1495 case TemplateArgument::StructuralValue
:
1496 // FIXME: Expose these values.
1497 return CXTemplateArgumentKind_Invalid
;
1498 case TemplateArgument::Template
:
1499 return CXTemplateArgumentKind_Template
;
1500 case TemplateArgument::TemplateExpansion
:
1501 return CXTemplateArgumentKind_TemplateExpansion
;
1502 case TemplateArgument::Expression
:
1503 return CXTemplateArgumentKind_Expression
;
1504 case TemplateArgument::Pack
:
1505 return CXTemplateArgumentKind_Pack
;
1508 return CXTemplateArgumentKind_Invalid
;
1511 CXType
clang_Cursor_getTemplateArgumentType(CXCursor C
, unsigned I
) {
1512 TemplateArgument TA
;
1513 if (clang_Cursor_getTemplateArgument(C
, I
, &TA
) !=
1514 CXGetTemplateArgumentStatus_Success
) {
1515 return cxtype::MakeCXType(QualType(), getCursorTU(C
));
1518 if (TA
.getKind() != TemplateArgument::Type
) {
1519 return cxtype::MakeCXType(QualType(), getCursorTU(C
));
1522 return cxtype::MakeCXType(TA
.getAsType(), getCursorTU(C
));
1525 long long clang_Cursor_getTemplateArgumentValue(CXCursor C
, unsigned I
) {
1526 TemplateArgument TA
;
1527 if (clang_Cursor_getTemplateArgument(C
, I
, &TA
) !=
1528 CXGetTemplateArgumentStatus_Success
) {
1529 assert(0 && "Unable to retrieve TemplateArgument");
1533 if (TA
.getKind() != TemplateArgument::Integral
) {
1534 assert(0 && "Passed template argument is not Integral");
1538 return TA
.getAsIntegral().getSExtValue();
1541 unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C
,
1543 TemplateArgument TA
;
1544 if (clang_Cursor_getTemplateArgument(C
, I
, &TA
) !=
1545 CXGetTemplateArgumentStatus_Success
) {
1546 assert(0 && "Unable to retrieve TemplateArgument");
1550 if (TA
.getKind() != TemplateArgument::Integral
) {
1551 assert(0 && "Passed template argument is not Integral");
1555 return TA
.getAsIntegral().getZExtValue();
1558 //===----------------------------------------------------------------------===//
1560 //===----------------------------------------------------------------------===//
1562 typedef llvm::DenseMap
<CXCursor
, unsigned> CXCursorSet_Impl
;
1564 static inline CXCursorSet
packCXCursorSet(CXCursorSet_Impl
*setImpl
) {
1565 return (CXCursorSet
)setImpl
;
1567 static inline CXCursorSet_Impl
*unpackCXCursorSet(CXCursorSet set
) {
1568 return (CXCursorSet_Impl
*)set
;
1571 template <> struct DenseMapInfo
<CXCursor
> {
1573 static inline CXCursor
getEmptyKey() {
1574 return MakeCXCursorInvalid(CXCursor_InvalidFile
);
1576 static inline CXCursor
getTombstoneKey() {
1577 return MakeCXCursorInvalid(CXCursor_NoDeclFound
);
1579 static inline unsigned getHashValue(const CXCursor
&cursor
) {
1580 return llvm::DenseMapInfo
<std::pair
<const void *, const void *>>::
1581 getHashValue(std::make_pair(cursor
.data
[0], cursor
.data
[1]));
1583 static inline bool isEqual(const CXCursor
&x
, const CXCursor
&y
) {
1584 return x
.kind
== y
.kind
&& x
.data
[0] == y
.data
[0] && x
.data
[1] == y
.data
[1];
1589 CXCursorSet
clang_createCXCursorSet() {
1590 return packCXCursorSet(new CXCursorSet_Impl());
1593 void clang_disposeCXCursorSet(CXCursorSet set
) {
1594 delete unpackCXCursorSet(set
);
1597 unsigned clang_CXCursorSet_contains(CXCursorSet set
, CXCursor cursor
) {
1598 CXCursorSet_Impl
*setImpl
= unpackCXCursorSet(set
);
1601 return setImpl
->find(cursor
) != setImpl
->end();
1604 unsigned clang_CXCursorSet_insert(CXCursorSet set
, CXCursor cursor
) {
1605 // Do not insert invalid cursors into the set.
1606 if (cursor
.kind
>= CXCursor_FirstInvalid
&&
1607 cursor
.kind
<= CXCursor_LastInvalid
)
1610 CXCursorSet_Impl
*setImpl
= unpackCXCursorSet(set
);
1613 unsigned &entry
= (*setImpl
)[cursor
];
1614 unsigned flag
= entry
== 0 ? 1 : 0;
1619 CXCompletionString
clang_getCursorCompletionString(CXCursor cursor
) {
1620 enum CXCursorKind kind
= clang_getCursorKind(cursor
);
1621 if (clang_isDeclaration(kind
)) {
1622 const Decl
*decl
= getCursorDecl(cursor
);
1623 if (const NamedDecl
*namedDecl
= dyn_cast_or_null
<NamedDecl
>(decl
)) {
1624 ASTUnit
*unit
= getCursorASTUnit(cursor
);
1625 CodeCompletionResult
Result(namedDecl
, CCP_Declaration
);
1626 CodeCompletionString
*String
= Result
.CreateCodeCompletionString(
1627 unit
->getASTContext(), unit
->getPreprocessor(),
1628 CodeCompletionContext::CCC_Other
,
1629 unit
->getCodeCompletionTUInfo().getAllocator(),
1630 unit
->getCodeCompletionTUInfo(), true);
1633 } else if (kind
== CXCursor_MacroDefinition
) {
1634 const MacroDefinitionRecord
*definition
= getCursorMacroDefinition(cursor
);
1635 const IdentifierInfo
*Macro
= definition
->getName();
1636 ASTUnit
*unit
= getCursorASTUnit(cursor
);
1637 CodeCompletionResult
Result(
1639 unit
->getPreprocessor().getMacroDefinition(Macro
).getMacroInfo());
1640 CodeCompletionString
*String
= Result
.CreateCodeCompletionString(
1641 unit
->getASTContext(), unit
->getPreprocessor(),
1642 CodeCompletionContext::CCC_Other
,
1643 unit
->getCodeCompletionTUInfo().getAllocator(),
1644 unit
->getCodeCompletionTUInfo(), false);
1651 struct OverridenCursorsPool
{
1652 typedef SmallVector
<CXCursor
, 2> CursorVec
;
1653 std::vector
<CursorVec
*> AllCursors
;
1654 std::vector
<CursorVec
*> AvailableCursors
;
1656 ~OverridenCursorsPool() {
1657 for (std::vector
<CursorVec
*>::iterator I
= AllCursors
.begin(),
1658 E
= AllCursors
.end();
1666 void *cxcursor::createOverridenCXCursorsPool() {
1667 return new OverridenCursorsPool();
1670 void cxcursor::disposeOverridenCXCursorsPool(void *pool
) {
1671 delete static_cast<OverridenCursorsPool
*>(pool
);
1674 void clang_getOverriddenCursors(CXCursor cursor
, CXCursor
**overridden
,
1675 unsigned *num_overridden
) {
1677 *overridden
= nullptr;
1679 *num_overridden
= 0;
1681 CXTranslationUnit TU
= cxcursor::getCursorTU(cursor
);
1683 if (!overridden
|| !num_overridden
|| !TU
)
1686 if (!clang_isDeclaration(cursor
.kind
))
1689 OverridenCursorsPool
&pool
=
1690 *static_cast<OverridenCursorsPool
*>(TU
->OverridenCursorsPool
);
1692 OverridenCursorsPool::CursorVec
*Vec
= nullptr;
1694 if (!pool
.AvailableCursors
.empty()) {
1695 Vec
= pool
.AvailableCursors
.back();
1696 pool
.AvailableCursors
.pop_back();
1698 Vec
= new OverridenCursorsPool::CursorVec();
1699 pool
.AllCursors
.push_back(Vec
);
1702 // Clear out the vector, but don't free the memory contents. This
1703 // reduces malloc() traffic.
1706 // Use the first entry to contain a back reference to the vector.
1707 // This is a complete hack.
1708 CXCursor backRefCursor
= MakeCXCursorInvalid(CXCursor_InvalidFile
, TU
);
1709 backRefCursor
.data
[0] = Vec
;
1710 assert(cxcursor::getCursorTU(backRefCursor
) == TU
);
1711 Vec
->push_back(backRefCursor
);
1713 // Get the overridden cursors.
1714 cxcursor::getOverriddenCursors(cursor
, *Vec
);
1716 // Did we get any overridden cursors? If not, return Vec to the pool
1717 // of available cursor vectors.
1718 if (Vec
->size() == 1) {
1719 pool
.AvailableCursors
.push_back(Vec
);
1723 // Now tell the caller about the overridden cursors.
1724 assert(Vec
->size() > 1);
1725 *overridden
= &((*Vec
)[1]);
1726 *num_overridden
= Vec
->size() - 1;
1729 void clang_disposeOverriddenCursors(CXCursor
*overridden
) {
1733 // Use pointer arithmetic to get back the first faux entry
1734 // which has a back-reference to the TU and the vector.
1736 OverridenCursorsPool::CursorVec
*Vec
=
1737 static_cast<OverridenCursorsPool::CursorVec
*>(
1738 const_cast<void *>(overridden
->data
[0]));
1739 CXTranslationUnit TU
= getCursorTU(*overridden
);
1743 OverridenCursorsPool
&pool
=
1744 *static_cast<OverridenCursorsPool
*>(TU
->OverridenCursorsPool
);
1746 pool
.AvailableCursors
.push_back(Vec
);
1749 int clang_Cursor_isDynamicCall(CXCursor C
) {
1750 const Expr
*E
= nullptr;
1751 if (clang_isExpression(C
.kind
))
1752 E
= getCursorExpr(C
);
1756 if (const ObjCMessageExpr
*MsgE
= dyn_cast
<ObjCMessageExpr
>(E
)) {
1757 if (MsgE
->getReceiverKind() != ObjCMessageExpr::Instance
)
1759 if (auto *RecE
= dyn_cast
<ObjCMessageExpr
>(
1760 MsgE
->getInstanceReceiver()->IgnoreParenCasts())) {
1761 if (RecE
->getMethodFamily() == OMF_alloc
)
1767 if (auto *PropRefE
= dyn_cast
<ObjCPropertyRefExpr
>(E
)) {
1768 return !PropRefE
->isSuperReceiver();
1771 const MemberExpr
*ME
= nullptr;
1772 if (isa
<MemberExpr
>(E
))
1773 ME
= cast
<MemberExpr
>(E
);
1774 else if (const CallExpr
*CE
= dyn_cast
<CallExpr
>(E
))
1775 ME
= dyn_cast_or_null
<MemberExpr
>(CE
->getCallee());
1778 if (const CXXMethodDecl
*MD
=
1779 dyn_cast_or_null
<CXXMethodDecl
>(ME
->getMemberDecl()))
1780 return MD
->isVirtual() &&
1781 ME
->performsVirtualDispatch(
1782 cxcursor::getCursorContext(C
).getLangOpts());
1788 CXType
clang_Cursor_getReceiverType(CXCursor C
) {
1789 CXTranslationUnit TU
= cxcursor::getCursorTU(C
);
1790 const Expr
*E
= nullptr;
1791 if (clang_isExpression(C
.kind
))
1792 E
= getCursorExpr(C
);
1794 if (const ObjCMessageExpr
*MsgE
= dyn_cast_or_null
<ObjCMessageExpr
>(E
))
1795 return cxtype::MakeCXType(MsgE
->getReceiverType(), TU
);
1797 if (auto *PropRefE
= dyn_cast
<ObjCPropertyRefExpr
>(E
)) {
1798 return cxtype::MakeCXType(
1799 PropRefE
->getReceiverType(cxcursor::getCursorContext(C
)), TU
);
1802 const MemberExpr
*ME
= nullptr;
1803 if (isa
<MemberExpr
>(E
))
1804 ME
= cast
<MemberExpr
>(E
);
1805 else if (const CallExpr
*CE
= dyn_cast
<CallExpr
>(E
))
1806 ME
= dyn_cast_or_null
<MemberExpr
>(CE
->getCallee());
1809 if (isa_and_nonnull
<CXXMethodDecl
>(ME
->getMemberDecl())) {
1810 auto receiverTy
= ME
->getBase()->IgnoreImpCasts()->getType();
1811 return cxtype::MakeCXType(receiverTy
, TU
);
1815 return cxtype::MakeCXType(QualType(), TU
);