[DFAJumpThreading] Remove incoming StartBlock from all phis when unfolding select...
[llvm-project.git] / clang / tools / libclang / CXCursor.cpp
blobfd03c48ba1a42aad0fc3542e4440492f429f6f06
1 //===- CXCursor.cpp - Routines for manipulating CXCursors -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines routines for manipulating CXCursors. It should be the
10 // only file that has internal knowledge of the encoding of the data in
11 // CXCursor.
13 //===----------------------------------------------------------------------===//
15 #include "CXCursor.h"
16 #include "CXString.h"
17 #include "CXTranslationUnit.h"
18 #include "CXType.h"
19 #include "clang-c/Index.h"
20 #include "clang/AST/Attr.h"
21 #include "clang/AST/Decl.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclObjC.h"
24 #include "clang/AST/DeclTemplate.h"
25 #include "clang/AST/Expr.h"
26 #include "clang/AST/ExprCXX.h"
27 #include "clang/AST/ExprObjC.h"
28 #include "clang/Frontend/ASTUnit.h"
29 #include "llvm/Support/ErrorHandling.h"
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}};
37 return C;
40 static CXCursorKind GetCursorKind(const Attr *A) {
41 assert(A && "Invalid arguments!");
42 switch (A->getKind()) {
43 default:
44 break;
45 case attr::IBAction:
46 return CXCursor_IBActionAttr;
47 case attr::IBOutlet:
48 return CXCursor_IBOutletAttr;
49 case attr::IBOutletCollection:
50 return CXCursor_IBOutletCollectionAttr;
51 case attr::Final:
52 return CXCursor_CXXFinalAttr;
53 case attr::Override:
54 return CXCursor_CXXOverrideAttr;
55 case attr::Annotate:
56 return CXCursor_AnnotateAttr;
57 case attr::AsmLabel:
58 return CXCursor_AsmLabelAttr;
59 case attr::Packed:
60 return CXCursor_PackedAttr;
61 case attr::Pure:
62 return CXCursor_PureAttr;
63 case attr::Const:
64 return CXCursor_ConstAttr;
65 case attr::NoDuplicate:
66 return CXCursor_NoDuplicateAttr;
67 case attr::CUDAConstant:
68 return CXCursor_CUDAConstantAttr;
69 case attr::CUDADevice:
70 return CXCursor_CUDADeviceAttr;
71 case attr::CUDAGlobal:
72 return CXCursor_CUDAGlobalAttr;
73 case attr::CUDAHost:
74 return CXCursor_CUDAHostAttr;
75 case attr::CUDAShared:
76 return CXCursor_CUDASharedAttr;
77 case attr::Visibility:
78 return CXCursor_VisibilityAttr;
79 case attr::DLLExport:
80 return CXCursor_DLLExport;
81 case attr::DLLImport:
82 return CXCursor_DLLImport;
83 case attr::NSReturnsRetained:
84 return CXCursor_NSReturnsRetained;
85 case attr::NSReturnsNotRetained:
86 return CXCursor_NSReturnsNotRetained;
87 case attr::NSReturnsAutoreleased:
88 return CXCursor_NSReturnsAutoreleased;
89 case attr::NSConsumesSelf:
90 return CXCursor_NSConsumesSelf;
91 case attr::NSConsumed:
92 return CXCursor_NSConsumed;
93 case attr::ObjCException:
94 return CXCursor_ObjCException;
95 case attr::ObjCNSObject:
96 return CXCursor_ObjCNSObject;
97 case attr::ObjCIndependentClass:
98 return CXCursor_ObjCIndependentClass;
99 case attr::ObjCPreciseLifetime:
100 return CXCursor_ObjCPreciseLifetime;
101 case attr::ObjCReturnsInnerPointer:
102 return CXCursor_ObjCReturnsInnerPointer;
103 case attr::ObjCRequiresSuper:
104 return CXCursor_ObjCRequiresSuper;
105 case attr::ObjCRootClass:
106 return CXCursor_ObjCRootClass;
107 case attr::ObjCSubclassingRestricted:
108 return CXCursor_ObjCSubclassingRestricted;
109 case attr::ObjCExplicitProtocolImpl:
110 return CXCursor_ObjCExplicitProtocolImpl;
111 case attr::ObjCDesignatedInitializer:
112 return CXCursor_ObjCDesignatedInitializer;
113 case attr::ObjCRuntimeVisible:
114 return CXCursor_ObjCRuntimeVisible;
115 case attr::ObjCBoxable:
116 return CXCursor_ObjCBoxable;
117 case attr::FlagEnum:
118 return CXCursor_FlagEnum;
119 case attr::Convergent:
120 return CXCursor_ConvergentAttr;
121 case attr::WarnUnused:
122 return CXCursor_WarnUnusedAttr;
123 case attr::WarnUnusedResult:
124 return CXCursor_WarnUnusedResultAttr;
125 case attr::Aligned:
126 return CXCursor_AlignedAttr;
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}};
136 return C;
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();
159 CXCursor C = {K,
160 SelectorIdIndex,
161 {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
162 return C;
165 CXCursor C = {K, 0, {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
166 return C;
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:
177 break;
179 case Stmt::CaseStmtClass:
180 K = CXCursor_CaseStmt;
181 break;
183 case Stmt::DefaultStmtClass:
184 K = CXCursor_DefaultStmt;
185 break;
187 case Stmt::IfStmtClass:
188 K = CXCursor_IfStmt;
189 break;
191 case Stmt::SwitchStmtClass:
192 K = CXCursor_SwitchStmt;
193 break;
195 case Stmt::WhileStmtClass:
196 K = CXCursor_WhileStmt;
197 break;
199 case Stmt::DoStmtClass:
200 K = CXCursor_DoStmt;
201 break;
203 case Stmt::ForStmtClass:
204 K = CXCursor_ForStmt;
205 break;
207 case Stmt::GotoStmtClass:
208 K = CXCursor_GotoStmt;
209 break;
211 case Stmt::IndirectGotoStmtClass:
212 K = CXCursor_IndirectGotoStmt;
213 break;
215 case Stmt::ContinueStmtClass:
216 K = CXCursor_ContinueStmt;
217 break;
219 case Stmt::BreakStmtClass:
220 K = CXCursor_BreakStmt;
221 break;
223 case Stmt::ReturnStmtClass:
224 K = CXCursor_ReturnStmt;
225 break;
227 case Stmt::GCCAsmStmtClass:
228 K = CXCursor_GCCAsmStmt;
229 break;
231 case Stmt::MSAsmStmtClass:
232 K = CXCursor_MSAsmStmt;
233 break;
235 case Stmt::ObjCAtTryStmtClass:
236 K = CXCursor_ObjCAtTryStmt;
237 break;
239 case Stmt::ObjCAtCatchStmtClass:
240 K = CXCursor_ObjCAtCatchStmt;
241 break;
243 case Stmt::ObjCAtFinallyStmtClass:
244 K = CXCursor_ObjCAtFinallyStmt;
245 break;
247 case Stmt::ObjCAtThrowStmtClass:
248 K = CXCursor_ObjCAtThrowStmt;
249 break;
251 case Stmt::ObjCAtSynchronizedStmtClass:
252 K = CXCursor_ObjCAtSynchronizedStmt;
253 break;
255 case Stmt::ObjCAutoreleasePoolStmtClass:
256 K = CXCursor_ObjCAutoreleasePoolStmt;
257 break;
259 case Stmt::ObjCForCollectionStmtClass:
260 K = CXCursor_ObjCForCollectionStmt;
261 break;
263 case Stmt::CXXCatchStmtClass:
264 K = CXCursor_CXXCatchStmt;
265 break;
267 case Stmt::CXXTryStmtClass:
268 K = CXCursor_CXXTryStmt;
269 break;
271 case Stmt::CXXForRangeStmtClass:
272 K = CXCursor_CXXForRangeStmt;
273 break;
275 case Stmt::SEHTryStmtClass:
276 K = CXCursor_SEHTryStmt;
277 break;
279 case Stmt::SEHExceptStmtClass:
280 K = CXCursor_SEHExceptStmt;
281 break;
283 case Stmt::SEHFinallyStmtClass:
284 K = CXCursor_SEHFinallyStmt;
285 break;
287 case Stmt::SEHLeaveStmtClass:
288 K = CXCursor_SEHLeaveStmt;
289 break;
291 case Stmt::CoroutineBodyStmtClass:
292 case Stmt::CoreturnStmtClass:
293 K = CXCursor_UnexposedStmt;
294 break;
296 case Stmt::ArrayTypeTraitExprClass:
297 case Stmt::AsTypeExprClass:
298 case Stmt::AtomicExprClass:
299 case Stmt::BinaryConditionalOperatorClass:
300 case Stmt::TypeTraitExprClass:
301 case Stmt::CoawaitExprClass:
302 case Stmt::DependentCoawaitExprClass:
303 case Stmt::CoyieldExprClass:
304 case Stmt::CXXBindTemporaryExprClass:
305 case Stmt::CXXDefaultArgExprClass:
306 case Stmt::CXXDefaultInitExprClass:
307 case Stmt::CXXFoldExprClass:
308 case Stmt::CXXRewrittenBinaryOperatorClass:
309 case Stmt::CXXStdInitializerListExprClass:
310 case Stmt::CXXScalarValueInitExprClass:
311 case Stmt::CXXUuidofExprClass:
312 case Stmt::ChooseExprClass:
313 case Stmt::DesignatedInitExprClass:
314 case Stmt::DesignatedInitUpdateExprClass:
315 case Stmt::ArrayInitLoopExprClass:
316 case Stmt::ArrayInitIndexExprClass:
317 case Stmt::ExprWithCleanupsClass:
318 case Stmt::ExpressionTraitExprClass:
319 case Stmt::ExtVectorElementExprClass:
320 case Stmt::ImplicitCastExprClass:
321 case Stmt::ImplicitValueInitExprClass:
322 case Stmt::NoInitExprClass:
323 case Stmt::MaterializeTemporaryExprClass:
324 case Stmt::ObjCIndirectCopyRestoreExprClass:
325 case Stmt::OffsetOfExprClass:
326 case Stmt::ParenListExprClass:
327 case Stmt::PredefinedExprClass:
328 case Stmt::ShuffleVectorExprClass:
329 case Stmt::SourceLocExprClass:
330 case Stmt::ConvertVectorExprClass:
331 case Stmt::VAArgExprClass:
332 case Stmt::ObjCArrayLiteralClass:
333 case Stmt::ObjCDictionaryLiteralClass:
334 case Stmt::ObjCBoxedExprClass:
335 case Stmt::ObjCSubscriptRefExprClass:
336 case Stmt::RecoveryExprClass:
337 case Stmt::SYCLUniqueStableNameExprClass:
338 K = CXCursor_UnexposedExpr;
339 break;
341 case Stmt::OpaqueValueExprClass:
342 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
343 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
344 K = CXCursor_UnexposedExpr;
345 break;
347 case Stmt::PseudoObjectExprClass:
348 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
349 TU, RegionOfInterest);
351 case Stmt::CompoundStmtClass:
352 K = CXCursor_CompoundStmt;
353 break;
355 case Stmt::NullStmtClass:
356 K = CXCursor_NullStmt;
357 break;
359 case Stmt::LabelStmtClass:
360 K = CXCursor_LabelStmt;
361 break;
363 case Stmt::AttributedStmtClass:
364 K = CXCursor_UnexposedStmt;
365 break;
367 case Stmt::DeclStmtClass:
368 K = CXCursor_DeclStmt;
369 break;
371 case Stmt::CapturedStmtClass:
372 K = CXCursor_UnexposedStmt;
373 break;
375 case Stmt::IntegerLiteralClass:
376 K = CXCursor_IntegerLiteral;
377 break;
379 case Stmt::FixedPointLiteralClass:
380 K = CXCursor_FixedPointLiteral;
381 break;
383 case Stmt::FloatingLiteralClass:
384 K = CXCursor_FloatingLiteral;
385 break;
387 case Stmt::ImaginaryLiteralClass:
388 K = CXCursor_ImaginaryLiteral;
389 break;
391 case Stmt::StringLiteralClass:
392 K = CXCursor_StringLiteral;
393 break;
395 case Stmt::CharacterLiteralClass:
396 K = CXCursor_CharacterLiteral;
397 break;
399 case Stmt::ConstantExprClass:
400 return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
401 RegionOfInterest);
403 case Stmt::ParenExprClass:
404 K = CXCursor_ParenExpr;
405 break;
407 case Stmt::UnaryOperatorClass:
408 K = CXCursor_UnaryOperator;
409 break;
411 case Stmt::UnaryExprOrTypeTraitExprClass:
412 case Stmt::CXXNoexceptExprClass:
413 K = CXCursor_UnaryExpr;
414 break;
416 case Stmt::MSPropertySubscriptExprClass:
417 case Stmt::ArraySubscriptExprClass:
418 K = CXCursor_ArraySubscriptExpr;
419 break;
421 case Stmt::MatrixSubscriptExprClass:
422 // TODO: add support for MatrixSubscriptExpr.
423 K = CXCursor_UnexposedExpr;
424 break;
426 case Stmt::OMPArraySectionExprClass:
427 K = CXCursor_OMPArraySectionExpr;
428 break;
430 case Stmt::OMPArrayShapingExprClass:
431 K = CXCursor_OMPArrayShapingExpr;
432 break;
434 case Stmt::OMPIteratorExprClass:
435 K = CXCursor_OMPIteratorExpr;
436 break;
438 case Stmt::BinaryOperatorClass:
439 K = CXCursor_BinaryOperator;
440 break;
442 case Stmt::CompoundAssignOperatorClass:
443 K = CXCursor_CompoundAssignOperator;
444 break;
446 case Stmt::ConditionalOperatorClass:
447 K = CXCursor_ConditionalOperator;
448 break;
450 case Stmt::CStyleCastExprClass:
451 K = CXCursor_CStyleCastExpr;
452 break;
454 case Stmt::CompoundLiteralExprClass:
455 K = CXCursor_CompoundLiteralExpr;
456 break;
458 case Stmt::InitListExprClass:
459 K = CXCursor_InitListExpr;
460 break;
462 case Stmt::AddrLabelExprClass:
463 K = CXCursor_AddrLabelExpr;
464 break;
466 case Stmt::StmtExprClass:
467 K = CXCursor_StmtExpr;
468 break;
470 case Stmt::GenericSelectionExprClass:
471 K = CXCursor_GenericSelectionExpr;
472 break;
474 case Stmt::GNUNullExprClass:
475 K = CXCursor_GNUNullExpr;
476 break;
478 case Stmt::CXXStaticCastExprClass:
479 K = CXCursor_CXXStaticCastExpr;
480 break;
482 case Stmt::CXXDynamicCastExprClass:
483 K = CXCursor_CXXDynamicCastExpr;
484 break;
486 case Stmt::CXXReinterpretCastExprClass:
487 K = CXCursor_CXXReinterpretCastExpr;
488 break;
490 case Stmt::CXXConstCastExprClass:
491 K = CXCursor_CXXConstCastExpr;
492 break;
494 case Stmt::CXXFunctionalCastExprClass:
495 K = CXCursor_CXXFunctionalCastExpr;
496 break;
498 case Stmt::CXXAddrspaceCastExprClass:
499 K = CXCursor_CXXAddrspaceCastExpr;
500 break;
502 case Stmt::CXXTypeidExprClass:
503 K = CXCursor_CXXTypeidExpr;
504 break;
506 case Stmt::CXXBoolLiteralExprClass:
507 K = CXCursor_CXXBoolLiteralExpr;
508 break;
510 case Stmt::CXXNullPtrLiteralExprClass:
511 K = CXCursor_CXXNullPtrLiteralExpr;
512 break;
514 case Stmt::CXXThisExprClass:
515 K = CXCursor_CXXThisExpr;
516 break;
518 case Stmt::CXXThrowExprClass:
519 K = CXCursor_CXXThrowExpr;
520 break;
522 case Stmt::CXXNewExprClass:
523 K = CXCursor_CXXNewExpr;
524 break;
526 case Stmt::CXXDeleteExprClass:
527 K = CXCursor_CXXDeleteExpr;
528 break;
530 case Stmt::ObjCStringLiteralClass:
531 K = CXCursor_ObjCStringLiteral;
532 break;
534 case Stmt::ObjCEncodeExprClass:
535 K = CXCursor_ObjCEncodeExpr;
536 break;
538 case Stmt::ObjCSelectorExprClass:
539 K = CXCursor_ObjCSelectorExpr;
540 break;
542 case Stmt::ObjCProtocolExprClass:
543 K = CXCursor_ObjCProtocolExpr;
544 break;
546 case Stmt::ObjCBoolLiteralExprClass:
547 K = CXCursor_ObjCBoolLiteralExpr;
548 break;
550 case Stmt::ObjCAvailabilityCheckExprClass:
551 K = CXCursor_ObjCAvailabilityCheckExpr;
552 break;
554 case Stmt::ObjCBridgedCastExprClass:
555 K = CXCursor_ObjCBridgedCastExpr;
556 break;
558 case Stmt::BlockExprClass:
559 K = CXCursor_BlockExpr;
560 break;
562 case Stmt::PackExpansionExprClass:
563 K = CXCursor_PackExpansionExpr;
564 break;
566 case Stmt::SizeOfPackExprClass:
567 K = CXCursor_SizeOfPackExpr;
568 break;
570 case Stmt::DeclRefExprClass:
571 if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
572 cast<DeclRefExpr>(S)->getDecl())) {
573 if (const ObjCMethodDecl *MD =
574 dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
575 if (MD->getSelfDecl() == IPD) {
576 K = CXCursor_ObjCSelfExpr;
577 break;
582 K = CXCursor_DeclRefExpr;
583 break;
585 case Stmt::DependentScopeDeclRefExprClass:
586 case Stmt::SubstNonTypeTemplateParmExprClass:
587 case Stmt::SubstNonTypeTemplateParmPackExprClass:
588 case Stmt::FunctionParmPackExprClass:
589 case Stmt::UnresolvedLookupExprClass:
590 case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
591 K = CXCursor_DeclRefExpr;
592 break;
594 case Stmt::CXXDependentScopeMemberExprClass:
595 case Stmt::CXXPseudoDestructorExprClass:
596 case Stmt::MemberExprClass:
597 case Stmt::MSPropertyRefExprClass:
598 case Stmt::ObjCIsaExprClass:
599 case Stmt::ObjCIvarRefExprClass:
600 case Stmt::ObjCPropertyRefExprClass:
601 case Stmt::UnresolvedMemberExprClass:
602 K = CXCursor_MemberRefExpr;
603 break;
605 case Stmt::CallExprClass:
606 case Stmt::CXXOperatorCallExprClass:
607 case Stmt::CXXMemberCallExprClass:
608 case Stmt::CUDAKernelCallExprClass:
609 case Stmt::CXXConstructExprClass:
610 case Stmt::CXXInheritedCtorInitExprClass:
611 case Stmt::CXXTemporaryObjectExprClass:
612 case Stmt::CXXUnresolvedConstructExprClass:
613 case Stmt::UserDefinedLiteralClass:
614 K = CXCursor_CallExpr;
615 break;
617 case Stmt::LambdaExprClass:
618 K = CXCursor_LambdaExpr;
619 break;
621 case Stmt::ObjCMessageExprClass: {
622 K = CXCursor_ObjCMessageExpr;
623 int SelectorIdIndex = -1;
624 // Check if cursor points to a selector id.
625 if (RegionOfInterest.isValid() &&
626 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
627 SmallVector<SourceLocation, 16> SelLocs;
628 cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
629 SmallVectorImpl<SourceLocation>::iterator I =
630 llvm::find(SelLocs, RegionOfInterest.getBegin());
631 if (I != SelLocs.end())
632 SelectorIdIndex = I - SelLocs.begin();
634 CXCursor C = {K, 0, {Parent, S, TU}};
635 return getSelectorIdentifierCursor(SelectorIdIndex, C);
638 case Stmt::ConceptSpecializationExprClass:
639 K = CXCursor_ConceptSpecializationExpr;
640 break;
642 case Stmt::RequiresExprClass:
643 K = CXCursor_RequiresExpr;
644 break;
646 case Stmt::CXXParenListInitExprClass:
647 K = CXCursor_CXXParenListInitExpr;
648 break;
650 case Stmt::MSDependentExistsStmtClass:
651 K = CXCursor_UnexposedStmt;
652 break;
653 case Stmt::OMPCanonicalLoopClass:
654 K = CXCursor_OMPCanonicalLoop;
655 break;
656 case Stmt::OMPMetaDirectiveClass:
657 K = CXCursor_OMPMetaDirective;
658 break;
659 case Stmt::OMPParallelDirectiveClass:
660 K = CXCursor_OMPParallelDirective;
661 break;
662 case Stmt::OMPSimdDirectiveClass:
663 K = CXCursor_OMPSimdDirective;
664 break;
665 case Stmt::OMPTileDirectiveClass:
666 K = CXCursor_OMPTileDirective;
667 break;
668 case Stmt::OMPUnrollDirectiveClass:
669 K = CXCursor_OMPUnrollDirective;
670 break;
671 case Stmt::OMPForDirectiveClass:
672 K = CXCursor_OMPForDirective;
673 break;
674 case Stmt::OMPForSimdDirectiveClass:
675 K = CXCursor_OMPForSimdDirective;
676 break;
677 case Stmt::OMPSectionsDirectiveClass:
678 K = CXCursor_OMPSectionsDirective;
679 break;
680 case Stmt::OMPSectionDirectiveClass:
681 K = CXCursor_OMPSectionDirective;
682 break;
683 case Stmt::OMPScopeDirectiveClass:
684 K = CXCursor_OMPScopeDirective;
685 break;
686 case Stmt::OMPSingleDirectiveClass:
687 K = CXCursor_OMPSingleDirective;
688 break;
689 case Stmt::OMPMasterDirectiveClass:
690 K = CXCursor_OMPMasterDirective;
691 break;
692 case Stmt::OMPCriticalDirectiveClass:
693 K = CXCursor_OMPCriticalDirective;
694 break;
695 case Stmt::OMPParallelForDirectiveClass:
696 K = CXCursor_OMPParallelForDirective;
697 break;
698 case Stmt::OMPParallelForSimdDirectiveClass:
699 K = CXCursor_OMPParallelForSimdDirective;
700 break;
701 case Stmt::OMPParallelMasterDirectiveClass:
702 K = CXCursor_OMPParallelMasterDirective;
703 break;
704 case Stmt::OMPParallelMaskedDirectiveClass:
705 K = CXCursor_OMPParallelMaskedDirective;
706 break;
707 case Stmt::OMPParallelSectionsDirectiveClass:
708 K = CXCursor_OMPParallelSectionsDirective;
709 break;
710 case Stmt::OMPTaskDirectiveClass:
711 K = CXCursor_OMPTaskDirective;
712 break;
713 case Stmt::OMPTaskyieldDirectiveClass:
714 K = CXCursor_OMPTaskyieldDirective;
715 break;
716 case Stmt::OMPBarrierDirectiveClass:
717 K = CXCursor_OMPBarrierDirective;
718 break;
719 case Stmt::OMPTaskwaitDirectiveClass:
720 K = CXCursor_OMPTaskwaitDirective;
721 break;
722 case Stmt::OMPErrorDirectiveClass:
723 K = CXCursor_OMPErrorDirective;
724 break;
725 case Stmt::OMPTaskgroupDirectiveClass:
726 K = CXCursor_OMPTaskgroupDirective;
727 break;
728 case Stmt::OMPFlushDirectiveClass:
729 K = CXCursor_OMPFlushDirective;
730 break;
731 case Stmt::OMPDepobjDirectiveClass:
732 K = CXCursor_OMPDepobjDirective;
733 break;
734 case Stmt::OMPScanDirectiveClass:
735 K = CXCursor_OMPScanDirective;
736 break;
737 case Stmt::OMPOrderedDirectiveClass:
738 K = CXCursor_OMPOrderedDirective;
739 break;
740 case Stmt::OMPAtomicDirectiveClass:
741 K = CXCursor_OMPAtomicDirective;
742 break;
743 case Stmt::OMPTargetDirectiveClass:
744 K = CXCursor_OMPTargetDirective;
745 break;
746 case Stmt::OMPTargetDataDirectiveClass:
747 K = CXCursor_OMPTargetDataDirective;
748 break;
749 case Stmt::OMPTargetEnterDataDirectiveClass:
750 K = CXCursor_OMPTargetEnterDataDirective;
751 break;
752 case Stmt::OMPTargetExitDataDirectiveClass:
753 K = CXCursor_OMPTargetExitDataDirective;
754 break;
755 case Stmt::OMPTargetParallelDirectiveClass:
756 K = CXCursor_OMPTargetParallelDirective;
757 break;
758 case Stmt::OMPTargetParallelForDirectiveClass:
759 K = CXCursor_OMPTargetParallelForDirective;
760 break;
761 case Stmt::OMPTargetUpdateDirectiveClass:
762 K = CXCursor_OMPTargetUpdateDirective;
763 break;
764 case Stmt::OMPTeamsDirectiveClass:
765 K = CXCursor_OMPTeamsDirective;
766 break;
767 case Stmt::OMPCancellationPointDirectiveClass:
768 K = CXCursor_OMPCancellationPointDirective;
769 break;
770 case Stmt::OMPCancelDirectiveClass:
771 K = CXCursor_OMPCancelDirective;
772 break;
773 case Stmt::OMPTaskLoopDirectiveClass:
774 K = CXCursor_OMPTaskLoopDirective;
775 break;
776 case Stmt::OMPTaskLoopSimdDirectiveClass:
777 K = CXCursor_OMPTaskLoopSimdDirective;
778 break;
779 case Stmt::OMPMasterTaskLoopDirectiveClass:
780 K = CXCursor_OMPMasterTaskLoopDirective;
781 break;
782 case Stmt::OMPMaskedTaskLoopDirectiveClass:
783 K = CXCursor_OMPMaskedTaskLoopDirective;
784 break;
785 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
786 K = CXCursor_OMPMasterTaskLoopSimdDirective;
787 break;
788 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
789 K = CXCursor_OMPMaskedTaskLoopSimdDirective;
790 break;
791 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
792 K = CXCursor_OMPParallelMasterTaskLoopDirective;
793 break;
794 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
795 K = CXCursor_OMPParallelMaskedTaskLoopDirective;
796 break;
797 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
798 K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
799 break;
800 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
801 K = CXCursor_OMPParallelMaskedTaskLoopSimdDirective;
802 break;
803 case Stmt::OMPDistributeDirectiveClass:
804 K = CXCursor_OMPDistributeDirective;
805 break;
806 case Stmt::OMPDistributeParallelForDirectiveClass:
807 K = CXCursor_OMPDistributeParallelForDirective;
808 break;
809 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
810 K = CXCursor_OMPDistributeParallelForSimdDirective;
811 break;
812 case Stmt::OMPDistributeSimdDirectiveClass:
813 K = CXCursor_OMPDistributeSimdDirective;
814 break;
815 case Stmt::OMPTargetParallelForSimdDirectiveClass:
816 K = CXCursor_OMPTargetParallelForSimdDirective;
817 break;
818 case Stmt::OMPTargetSimdDirectiveClass:
819 K = CXCursor_OMPTargetSimdDirective;
820 break;
821 case Stmt::OMPTeamsDistributeDirectiveClass:
822 K = CXCursor_OMPTeamsDistributeDirective;
823 break;
824 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
825 K = CXCursor_OMPTeamsDistributeSimdDirective;
826 break;
827 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
828 K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
829 break;
830 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
831 K = CXCursor_OMPTeamsDistributeParallelForDirective;
832 break;
833 case Stmt::OMPTargetTeamsDirectiveClass:
834 K = CXCursor_OMPTargetTeamsDirective;
835 break;
836 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
837 K = CXCursor_OMPTargetTeamsDistributeDirective;
838 break;
839 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
840 K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
841 break;
842 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
843 K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
844 break;
845 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
846 K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
847 break;
848 case Stmt::OMPInteropDirectiveClass:
849 K = CXCursor_OMPInteropDirective;
850 break;
851 case Stmt::OMPDispatchDirectiveClass:
852 K = CXCursor_OMPDispatchDirective;
853 break;
854 case Stmt::OMPMaskedDirectiveClass:
855 K = CXCursor_OMPMaskedDirective;
856 break;
857 case Stmt::OMPGenericLoopDirectiveClass:
858 K = CXCursor_OMPGenericLoopDirective;
859 break;
860 case Stmt::OMPTeamsGenericLoopDirectiveClass:
861 K = CXCursor_OMPTeamsGenericLoopDirective;
862 break;
863 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
864 K = CXCursor_OMPTargetTeamsGenericLoopDirective;
865 break;
866 case Stmt::OMPParallelGenericLoopDirectiveClass:
867 K = CXCursor_OMPParallelGenericLoopDirective;
868 break;
869 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
870 K = CXCursor_OMPTargetParallelGenericLoopDirective;
871 break;
872 case Stmt::BuiltinBitCastExprClass:
873 K = CXCursor_BuiltinBitCastExpr;
876 CXCursor C = {K, 0, {Parent, S, TU}};
877 return C;
880 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
881 SourceLocation Loc,
882 CXTranslationUnit TU) {
883 assert(Super && TU && "Invalid arguments!");
884 void *RawLoc = Loc.getPtrEncoding();
885 CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
886 return C;
889 std::pair<const ObjCInterfaceDecl *, SourceLocation>
890 cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
891 assert(C.kind == CXCursor_ObjCSuperClassRef);
892 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
893 SourceLocation::getFromPtrEncoding(C.data[1]));
896 CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
897 SourceLocation Loc,
898 CXTranslationUnit TU) {
899 assert(Proto && TU && "Invalid arguments!");
900 void *RawLoc = Loc.getPtrEncoding();
901 CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
902 return C;
905 std::pair<const ObjCProtocolDecl *, SourceLocation>
906 cxcursor::getCursorObjCProtocolRef(CXCursor C) {
907 assert(C.kind == CXCursor_ObjCProtocolRef);
908 return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
909 SourceLocation::getFromPtrEncoding(C.data[1]));
912 CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
913 SourceLocation Loc,
914 CXTranslationUnit TU) {
915 // 'Class' can be null for invalid code.
916 if (!Class)
917 return MakeCXCursorInvalid(CXCursor_InvalidCode);
918 assert(TU && "Invalid arguments!");
919 void *RawLoc = Loc.getPtrEncoding();
920 CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
921 return C;
924 std::pair<const ObjCInterfaceDecl *, SourceLocation>
925 cxcursor::getCursorObjCClassRef(CXCursor C) {
926 assert(C.kind == CXCursor_ObjCClassRef);
927 return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
928 SourceLocation::getFromPtrEncoding(C.data[1]));
931 CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
932 CXTranslationUnit TU) {
933 assert(Type && TU && "Invalid arguments!");
934 void *RawLoc = Loc.getPtrEncoding();
935 CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
936 return C;
939 std::pair<const TypeDecl *, SourceLocation>
940 cxcursor::getCursorTypeRef(CXCursor C) {
941 assert(C.kind == CXCursor_TypeRef);
942 return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
943 SourceLocation::getFromPtrEncoding(C.data[1]));
946 CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
947 SourceLocation Loc,
948 CXTranslationUnit TU) {
949 assert(Template && TU && "Invalid arguments!");
950 void *RawLoc = Loc.getPtrEncoding();
951 CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
952 return C;
955 std::pair<const TemplateDecl *, SourceLocation>
956 cxcursor::getCursorTemplateRef(CXCursor C) {
957 assert(C.kind == CXCursor_TemplateRef);
958 return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
959 SourceLocation::getFromPtrEncoding(C.data[1]));
962 CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
963 SourceLocation Loc,
964 CXTranslationUnit TU) {
966 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
967 "Invalid arguments!");
968 void *RawLoc = Loc.getPtrEncoding();
969 CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
970 return C;
973 std::pair<const NamedDecl *, SourceLocation>
974 cxcursor::getCursorNamespaceRef(CXCursor C) {
975 assert(C.kind == CXCursor_NamespaceRef);
976 return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
977 SourceLocation::getFromPtrEncoding(C.data[1]));
980 CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
981 CXTranslationUnit TU) {
983 assert(Var && TU && "Invalid arguments!");
984 void *RawLoc = Loc.getPtrEncoding();
985 CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
986 return C;
989 std::pair<const VarDecl *, SourceLocation>
990 cxcursor::getCursorVariableRef(CXCursor C) {
991 assert(C.kind == CXCursor_VariableRef);
992 return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
993 SourceLocation::getFromPtrEncoding(C.data[1]));
996 CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
997 SourceLocation Loc,
998 CXTranslationUnit TU) {
1000 assert(Field && TU && "Invalid arguments!");
1001 void *RawLoc = Loc.getPtrEncoding();
1002 CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
1003 return C;
1006 std::pair<const FieldDecl *, SourceLocation>
1007 cxcursor::getCursorMemberRef(CXCursor C) {
1008 assert(C.kind == CXCursor_MemberRef);
1009 return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
1010 SourceLocation::getFromPtrEncoding(C.data[1]));
1013 CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
1014 CXTranslationUnit TU) {
1015 CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
1016 return C;
1019 const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
1020 assert(C.kind == CXCursor_CXXBaseSpecifier);
1021 return static_cast<const CXXBaseSpecifier *>(C.data[0]);
1024 CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
1025 CXTranslationUnit TU) {
1026 CXCursor C = {
1027 CXCursor_PreprocessingDirective,
1029 {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
1030 return C;
1033 SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
1034 assert(C.kind == CXCursor_PreprocessingDirective);
1035 SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
1036 SourceLocation::getFromPtrEncoding(C.data[1]));
1037 ASTUnit *TU = getCursorASTUnit(C);
1038 return TU->mapRangeFromPreamble(Range);
1041 CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
1042 CXTranslationUnit TU) {
1043 CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
1044 return C;
1047 const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
1048 assert(C.kind == CXCursor_MacroDefinition);
1049 return static_cast<const MacroDefinitionRecord *>(C.data[0]);
1052 CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
1053 CXTranslationUnit TU) {
1054 CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
1055 return C;
1058 CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
1059 SourceLocation Loc,
1060 CXTranslationUnit TU) {
1061 assert(Loc.isValid());
1062 CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
1063 return C;
1066 const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
1067 if (isPseudo())
1068 return getAsMacroDefinition()->getName();
1069 return getAsMacroExpansion()->getName();
1071 const MacroDefinitionRecord *
1072 cxcursor::MacroExpansionCursor::getDefinition() const {
1073 if (isPseudo())
1074 return getAsMacroDefinition();
1075 return getAsMacroExpansion()->getDefinition();
1077 SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
1078 if (isPseudo())
1079 return getPseudoLoc();
1080 return getAsMacroExpansion()->getSourceRange();
1083 CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
1084 CXTranslationUnit TU) {
1085 CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
1086 return C;
1089 const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
1090 assert(C.kind == CXCursor_InclusionDirective);
1091 return static_cast<const InclusionDirective *>(C.data[0]);
1094 CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
1095 CXTranslationUnit TU) {
1097 assert(Label && TU && "Invalid arguments!");
1098 void *RawLoc = Loc.getPtrEncoding();
1099 CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
1100 return C;
1103 std::pair<const LabelStmt *, SourceLocation>
1104 cxcursor::getCursorLabelRef(CXCursor C) {
1105 assert(C.kind == CXCursor_LabelRef);
1106 return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
1107 SourceLocation::getFromPtrEncoding(C.data[1]));
1110 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
1111 CXTranslationUnit TU) {
1112 assert(E && TU && "Invalid arguments!");
1113 OverloadedDeclRefStorage Storage(E);
1114 void *RawLoc = E->getNameLoc().getPtrEncoding();
1115 CXCursor C = {
1116 CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1117 return C;
1120 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
1121 SourceLocation Loc,
1122 CXTranslationUnit TU) {
1123 assert(D && TU && "Invalid arguments!");
1124 void *RawLoc = Loc.getPtrEncoding();
1125 OverloadedDeclRefStorage Storage(D);
1126 CXCursor C = {
1127 CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1128 return C;
1131 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
1132 SourceLocation Loc,
1133 CXTranslationUnit TU) {
1134 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
1135 void *RawLoc = Loc.getPtrEncoding();
1136 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
1137 CXCursor C = {
1138 CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1139 return C;
1142 std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
1143 cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
1144 assert(C.kind == CXCursor_OverloadedDeclRef);
1145 return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
1146 const_cast<void *>(C.data[0])),
1147 SourceLocation::getFromPtrEncoding(C.data[1]));
1150 const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
1151 return static_cast<const Decl *>(Cursor.data[0]);
1154 const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
1155 return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
1158 const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
1159 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
1160 Cursor.kind == CXCursor_ObjCProtocolRef ||
1161 Cursor.kind == CXCursor_ObjCClassRef)
1162 return nullptr;
1164 return static_cast<const Stmt *>(Cursor.data[1]);
1167 const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
1168 return static_cast<const Attr *>(Cursor.data[1]);
1171 ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
1172 return getCursorASTUnit(Cursor)->getASTContext();
1175 ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
1176 CXTranslationUnit TU = getCursorTU(Cursor);
1177 if (!TU)
1178 return nullptr;
1179 return cxtu::getASTUnit(TU);
1182 CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
1183 return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
1186 void cxcursor::getOverriddenCursors(CXCursor cursor,
1187 SmallVectorImpl<CXCursor> &overridden) {
1188 assert(clang_isDeclaration(cursor.kind));
1189 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
1190 if (!D)
1191 return;
1193 CXTranslationUnit TU = getCursorTU(cursor);
1194 SmallVector<const NamedDecl *, 8> OverDecls;
1195 D->getASTContext().getOverriddenMethods(D, OverDecls);
1197 for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1198 E = OverDecls.end();
1199 I != E; ++I) {
1200 overridden.push_back(MakeCXCursor(*I, TU));
1204 std::pair<int, SourceLocation>
1205 cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
1206 if (cursor.kind == CXCursor_ObjCMessageExpr) {
1207 if (cursor.xdata != -1)
1208 return std::make_pair(cursor.xdata,
1209 cast<ObjCMessageExpr>(getCursorExpr(cursor))
1210 ->getSelectorLoc(cursor.xdata));
1211 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1212 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1213 if (cursor.xdata != -1)
1214 return std::make_pair(cursor.xdata,
1215 cast<ObjCMethodDecl>(getCursorDecl(cursor))
1216 ->getSelectorLoc(cursor.xdata));
1219 return std::make_pair(-1, SourceLocation());
1222 CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
1223 CXCursor newCursor = cursor;
1225 if (cursor.kind == CXCursor_ObjCMessageExpr) {
1226 if (SelIdx == -1 ||
1227 unsigned(SelIdx) >=
1228 cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
1229 newCursor.xdata = -1;
1230 else
1231 newCursor.xdata = SelIdx;
1232 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1233 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1234 if (SelIdx == -1 ||
1235 unsigned(SelIdx) >=
1236 cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
1237 newCursor.xdata = -1;
1238 else
1239 newCursor.xdata = SelIdx;
1242 return newCursor;
1245 CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1246 if (cursor.kind != CXCursor_CallExpr)
1247 return cursor;
1249 if (cursor.xdata == 0)
1250 return cursor;
1252 const Expr *E = getCursorExpr(cursor);
1253 TypeSourceInfo *Type = nullptr;
1254 if (const CXXUnresolvedConstructExpr *UnCtor =
1255 dyn_cast<CXXUnresolvedConstructExpr>(E)) {
1256 Type = UnCtor->getTypeSourceInfo();
1257 } else if (const CXXTemporaryObjectExpr *Tmp =
1258 dyn_cast<CXXTemporaryObjectExpr>(E)) {
1259 Type = Tmp->getTypeSourceInfo();
1262 if (!Type)
1263 return cursor;
1265 CXTranslationUnit TU = getCursorTU(cursor);
1266 QualType Ty = Type->getType();
1267 TypeLoc TL = Type->getTypeLoc();
1268 SourceLocation Loc = TL.getBeginLoc();
1270 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
1271 Ty = ElabT->getNamedType();
1272 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
1273 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
1276 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
1277 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
1278 if (const TagType *Tag = Ty->getAs<TagType>())
1279 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
1280 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
1281 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
1283 return cursor;
1286 bool cxcursor::operator==(CXCursor X, CXCursor Y) {
1287 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
1288 X.data[2] == Y.data[2];
1291 // FIXME: Remove once we can model DeclGroups and their appropriate ranges
1292 // properly in the ASTs.
1293 bool cxcursor::isFirstInDeclGroup(CXCursor C) {
1294 assert(clang_isDeclaration(C.kind));
1295 return ((uintptr_t)(C.data[1])) != 0;
1298 //===----------------------------------------------------------------------===//
1299 // libclang CXCursor APIs
1300 //===----------------------------------------------------------------------===//
1302 int clang_Cursor_isNull(CXCursor cursor) {
1303 return clang_equalCursors(cursor, clang_getNullCursor());
1306 CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
1307 return getCursorTU(cursor);
1310 int clang_Cursor_getNumArguments(CXCursor C) {
1311 if (clang_isDeclaration(C.kind)) {
1312 const Decl *D = cxcursor::getCursorDecl(C);
1313 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
1314 return MD->param_size();
1315 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
1316 return FD->param_size();
1319 if (clang_isExpression(C.kind)) {
1320 const Expr *E = cxcursor::getCursorExpr(C);
1321 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1322 return CE->getNumArgs();
1324 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1325 return CE->getNumArgs();
1329 return -1;
1332 CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1333 if (clang_isDeclaration(C.kind)) {
1334 const Decl *D = cxcursor::getCursorDecl(C);
1335 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
1336 if (i < MD->param_size())
1337 return cxcursor::MakeCXCursor(MD->parameters()[i],
1338 cxcursor::getCursorTU(C));
1339 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
1340 if (i < FD->param_size())
1341 return cxcursor::MakeCXCursor(FD->parameters()[i],
1342 cxcursor::getCursorTU(C));
1346 if (clang_isExpression(C.kind)) {
1347 const Expr *E = cxcursor::getCursorExpr(C);
1348 if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
1349 if (i < CE->getNumArgs()) {
1350 return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1351 cxcursor::getCursorTU(C));
1354 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
1355 if (i < CE->getNumArgs()) {
1356 return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
1357 cxcursor::getCursorTU(C));
1362 return clang_getNullCursor();
1365 int clang_Cursor_getNumTemplateArguments(CXCursor C) {
1366 CXCursorKind kind = clang_getCursorKind(C);
1367 if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
1368 kind != CXCursor_ClassDecl &&
1369 kind != CXCursor_ClassTemplatePartialSpecialization) {
1370 return -1;
1373 if (const auto *FD =
1374 llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
1375 const FunctionTemplateSpecializationInfo *SpecInfo =
1376 FD->getTemplateSpecializationInfo();
1377 if (!SpecInfo) {
1378 return -1;
1380 return SpecInfo->TemplateArguments->size();
1383 if (const auto *SD =
1384 llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
1385 getCursorDecl(C))) {
1386 return SD->getTemplateArgs().size();
1389 return -1;
1392 enum CXGetTemplateArgumentStatus {
1393 /** The operation completed successfully */
1394 CXGetTemplateArgumentStatus_Success = 0,
1396 /** The specified cursor did not represent a FunctionDecl or
1397 ClassTemplateSpecializationDecl. */
1398 CXGetTemplateArgumentStatus_CursorNotCompatibleDecl = -1,
1400 /** The specified cursor was not castable to a FunctionDecl or
1401 ClassTemplateSpecializationDecl. */
1402 CXGetTemplateArgumentStatus_BadDeclCast = -2,
1404 /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
1405 CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
1407 /** An invalid (OOB) argument index was specified */
1408 CXGetTemplateArgumentStatus_InvalidIndex = -4
1411 static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1412 TemplateArgument *TA) {
1413 CXCursorKind kind = clang_getCursorKind(C);
1414 if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
1415 kind != CXCursor_ClassDecl &&
1416 kind != CXCursor_ClassTemplatePartialSpecialization) {
1417 return -1;
1420 if (const auto *FD =
1421 llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
1423 const FunctionTemplateSpecializationInfo *SpecInfo =
1424 FD->getTemplateSpecializationInfo();
1425 if (!SpecInfo) {
1426 return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
1429 if (I >= SpecInfo->TemplateArguments->size()) {
1430 return CXGetTemplateArgumentStatus_InvalidIndex;
1433 *TA = SpecInfo->TemplateArguments->get(I);
1434 return 0;
1437 if (const auto *SD =
1438 llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
1439 getCursorDecl(C))) {
1440 if (I >= SD->getTemplateArgs().size()) {
1441 return CXGetTemplateArgumentStatus_InvalidIndex;
1444 *TA = SD->getTemplateArgs()[I];
1445 return 0;
1448 return CXGetTemplateArgumentStatus_BadDeclCast;
1451 enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
1452 unsigned I) {
1453 TemplateArgument TA;
1454 if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
1455 return CXTemplateArgumentKind_Invalid;
1458 switch (TA.getKind()) {
1459 case TemplateArgument::Null:
1460 return CXTemplateArgumentKind_Null;
1461 case TemplateArgument::Type:
1462 return CXTemplateArgumentKind_Type;
1463 case TemplateArgument::Declaration:
1464 return CXTemplateArgumentKind_Declaration;
1465 case TemplateArgument::NullPtr:
1466 return CXTemplateArgumentKind_NullPtr;
1467 case TemplateArgument::Integral:
1468 return CXTemplateArgumentKind_Integral;
1469 case TemplateArgument::Template:
1470 return CXTemplateArgumentKind_Template;
1471 case TemplateArgument::TemplateExpansion:
1472 return CXTemplateArgumentKind_TemplateExpansion;
1473 case TemplateArgument::Expression:
1474 return CXTemplateArgumentKind_Expression;
1475 case TemplateArgument::Pack:
1476 return CXTemplateArgumentKind_Pack;
1479 return CXTemplateArgumentKind_Invalid;
1482 CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
1483 TemplateArgument TA;
1484 if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1485 CXGetTemplateArgumentStatus_Success) {
1486 return cxtype::MakeCXType(QualType(), getCursorTU(C));
1489 if (TA.getKind() != TemplateArgument::Type) {
1490 return cxtype::MakeCXType(QualType(), getCursorTU(C));
1493 return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
1496 long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
1497 TemplateArgument TA;
1498 if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1499 CXGetTemplateArgumentStatus_Success) {
1500 assert(0 && "Unable to retrieve TemplateArgument");
1501 return 0;
1504 if (TA.getKind() != TemplateArgument::Integral) {
1505 assert(0 && "Passed template argument is not Integral");
1506 return 0;
1509 return TA.getAsIntegral().getSExtValue();
1512 unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
1513 unsigned I) {
1514 TemplateArgument TA;
1515 if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1516 CXGetTemplateArgumentStatus_Success) {
1517 assert(0 && "Unable to retrieve TemplateArgument");
1518 return 0;
1521 if (TA.getKind() != TemplateArgument::Integral) {
1522 assert(0 && "Passed template argument is not Integral");
1523 return 0;
1526 return TA.getAsIntegral().getZExtValue();
1529 //===----------------------------------------------------------------------===//
1530 // CXCursorSet.
1531 //===----------------------------------------------------------------------===//
1533 typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1535 static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1536 return (CXCursorSet)setImpl;
1538 static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1539 return (CXCursorSet_Impl *)set;
1541 namespace llvm {
1542 template <> struct DenseMapInfo<CXCursor> {
1543 public:
1544 static inline CXCursor getEmptyKey() {
1545 return MakeCXCursorInvalid(CXCursor_InvalidFile);
1547 static inline CXCursor getTombstoneKey() {
1548 return MakeCXCursorInvalid(CXCursor_NoDeclFound);
1550 static inline unsigned getHashValue(const CXCursor &cursor) {
1551 return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1552 getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
1554 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1555 return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
1558 } // namespace llvm
1560 CXCursorSet clang_createCXCursorSet() {
1561 return packCXCursorSet(new CXCursorSet_Impl());
1564 void clang_disposeCXCursorSet(CXCursorSet set) {
1565 delete unpackCXCursorSet(set);
1568 unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1569 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1570 if (!setImpl)
1571 return 0;
1572 return setImpl->find(cursor) != setImpl->end();
1575 unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1576 // Do not insert invalid cursors into the set.
1577 if (cursor.kind >= CXCursor_FirstInvalid &&
1578 cursor.kind <= CXCursor_LastInvalid)
1579 return 1;
1581 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1582 if (!setImpl)
1583 return 1;
1584 unsigned &entry = (*setImpl)[cursor];
1585 unsigned flag = entry == 0 ? 1 : 0;
1586 entry = 1;
1587 return flag;
1590 CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1591 enum CXCursorKind kind = clang_getCursorKind(cursor);
1592 if (clang_isDeclaration(kind)) {
1593 const Decl *decl = getCursorDecl(cursor);
1594 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
1595 ASTUnit *unit = getCursorASTUnit(cursor);
1596 CodeCompletionResult Result(namedDecl, CCP_Declaration);
1597 CodeCompletionString *String = Result.CreateCodeCompletionString(
1598 unit->getASTContext(), unit->getPreprocessor(),
1599 CodeCompletionContext::CCC_Other,
1600 unit->getCodeCompletionTUInfo().getAllocator(),
1601 unit->getCodeCompletionTUInfo(), true);
1602 return String;
1604 } else if (kind == CXCursor_MacroDefinition) {
1605 const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
1606 const IdentifierInfo *Macro = definition->getName();
1607 ASTUnit *unit = getCursorASTUnit(cursor);
1608 CodeCompletionResult Result(
1609 Macro,
1610 unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
1611 CodeCompletionString *String = Result.CreateCodeCompletionString(
1612 unit->getASTContext(), unit->getPreprocessor(),
1613 CodeCompletionContext::CCC_Other,
1614 unit->getCodeCompletionTUInfo().getAllocator(),
1615 unit->getCodeCompletionTUInfo(), false);
1616 return String;
1618 return nullptr;
1621 namespace {
1622 struct OverridenCursorsPool {
1623 typedef SmallVector<CXCursor, 2> CursorVec;
1624 std::vector<CursorVec *> AllCursors;
1625 std::vector<CursorVec *> AvailableCursors;
1627 ~OverridenCursorsPool() {
1628 for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1629 E = AllCursors.end();
1630 I != E; ++I) {
1631 delete *I;
1635 } // namespace
1637 void *cxcursor::createOverridenCXCursorsPool() {
1638 return new OverridenCursorsPool();
1641 void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1642 delete static_cast<OverridenCursorsPool *>(pool);
1645 void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
1646 unsigned *num_overridden) {
1647 if (overridden)
1648 *overridden = nullptr;
1649 if (num_overridden)
1650 *num_overridden = 0;
1652 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1654 if (!overridden || !num_overridden || !TU)
1655 return;
1657 if (!clang_isDeclaration(cursor.kind))
1658 return;
1660 OverridenCursorsPool &pool =
1661 *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1663 OverridenCursorsPool::CursorVec *Vec = nullptr;
1665 if (!pool.AvailableCursors.empty()) {
1666 Vec = pool.AvailableCursors.back();
1667 pool.AvailableCursors.pop_back();
1668 } else {
1669 Vec = new OverridenCursorsPool::CursorVec();
1670 pool.AllCursors.push_back(Vec);
1673 // Clear out the vector, but don't free the memory contents. This
1674 // reduces malloc() traffic.
1675 Vec->clear();
1677 // Use the first entry to contain a back reference to the vector.
1678 // This is a complete hack.
1679 CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
1680 backRefCursor.data[0] = Vec;
1681 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1682 Vec->push_back(backRefCursor);
1684 // Get the overridden cursors.
1685 cxcursor::getOverriddenCursors(cursor, *Vec);
1687 // Did we get any overridden cursors? If not, return Vec to the pool
1688 // of available cursor vectors.
1689 if (Vec->size() == 1) {
1690 pool.AvailableCursors.push_back(Vec);
1691 return;
1694 // Now tell the caller about the overridden cursors.
1695 assert(Vec->size() > 1);
1696 *overridden = &((*Vec)[1]);
1697 *num_overridden = Vec->size() - 1;
1700 void clang_disposeOverriddenCursors(CXCursor *overridden) {
1701 if (!overridden)
1702 return;
1704 // Use pointer arithmetic to get back the first faux entry
1705 // which has a back-reference to the TU and the vector.
1706 --overridden;
1707 OverridenCursorsPool::CursorVec *Vec =
1708 static_cast<OverridenCursorsPool::CursorVec *>(
1709 const_cast<void *>(overridden->data[0]));
1710 CXTranslationUnit TU = getCursorTU(*overridden);
1712 assert(Vec && TU);
1714 OverridenCursorsPool &pool =
1715 *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1717 pool.AvailableCursors.push_back(Vec);
1720 int clang_Cursor_isDynamicCall(CXCursor C) {
1721 const Expr *E = nullptr;
1722 if (clang_isExpression(C.kind))
1723 E = getCursorExpr(C);
1724 if (!E)
1725 return 0;
1727 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
1728 if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
1729 return false;
1730 if (auto *RecE = dyn_cast<ObjCMessageExpr>(
1731 MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
1732 if (RecE->getMethodFamily() == OMF_alloc)
1733 return false;
1735 return true;
1738 if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1739 return !PropRefE->isSuperReceiver();
1742 const MemberExpr *ME = nullptr;
1743 if (isa<MemberExpr>(E))
1744 ME = cast<MemberExpr>(E);
1745 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1746 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1748 if (ME) {
1749 if (const CXXMethodDecl *MD =
1750 dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
1751 return MD->isVirtual() &&
1752 ME->performsVirtualDispatch(
1753 cxcursor::getCursorContext(C).getLangOpts());
1756 return 0;
1759 CXType clang_Cursor_getReceiverType(CXCursor C) {
1760 CXTranslationUnit TU = cxcursor::getCursorTU(C);
1761 const Expr *E = nullptr;
1762 if (clang_isExpression(C.kind))
1763 E = getCursorExpr(C);
1765 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
1766 return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
1768 if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
1769 return cxtype::MakeCXType(
1770 PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
1773 const MemberExpr *ME = nullptr;
1774 if (isa<MemberExpr>(E))
1775 ME = cast<MemberExpr>(E);
1776 else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
1777 ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
1779 if (ME) {
1780 if (isa_and_nonnull<CXXMethodDecl>(ME->getMemberDecl())) {
1781 auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
1782 return cxtype::MakeCXType(receiverTy, TU);
1786 return cxtype::MakeCXType(QualType(), TU);