[RISCV] Add Qualcomm uC Xqcics(Conditional Select) extension (#119504)
[llvm-project.git] / clang / tools / libclang / CXCursor.cpp
blobc8cf51d806132806f352bad9a4399360789b8694
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 case Stmt::EmbedExprClass:
339 case Stmt::HLSLOutArgExprClass:
340 case Stmt::OpenACCAsteriskSizeExprClass:
341 K = CXCursor_UnexposedExpr;
342 break;
344 case Stmt::OpaqueValueExprClass:
345 if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
346 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
347 K = CXCursor_UnexposedExpr;
348 break;
350 case Stmt::PseudoObjectExprClass:
351 return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
352 TU, RegionOfInterest);
354 case Stmt::CompoundStmtClass:
355 K = CXCursor_CompoundStmt;
356 break;
358 case Stmt::NullStmtClass:
359 K = CXCursor_NullStmt;
360 break;
362 case Stmt::LabelStmtClass:
363 K = CXCursor_LabelStmt;
364 break;
366 case Stmt::AttributedStmtClass:
367 K = CXCursor_UnexposedStmt;
368 break;
370 case Stmt::DeclStmtClass:
371 K = CXCursor_DeclStmt;
372 break;
374 case Stmt::CapturedStmtClass:
375 K = CXCursor_UnexposedStmt;
376 break;
378 case Stmt::IntegerLiteralClass:
379 K = CXCursor_IntegerLiteral;
380 break;
382 case Stmt::FixedPointLiteralClass:
383 K = CXCursor_FixedPointLiteral;
384 break;
386 case Stmt::FloatingLiteralClass:
387 K = CXCursor_FloatingLiteral;
388 break;
390 case Stmt::ImaginaryLiteralClass:
391 K = CXCursor_ImaginaryLiteral;
392 break;
394 case Stmt::StringLiteralClass:
395 K = CXCursor_StringLiteral;
396 break;
398 case Stmt::CharacterLiteralClass:
399 K = CXCursor_CharacterLiteral;
400 break;
402 case Stmt::ConstantExprClass:
403 return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
404 RegionOfInterest);
406 case Stmt::ParenExprClass:
407 K = CXCursor_ParenExpr;
408 break;
410 case Stmt::UnaryOperatorClass:
411 K = CXCursor_UnaryOperator;
412 break;
414 case Stmt::UnaryExprOrTypeTraitExprClass:
415 case Stmt::CXXNoexceptExprClass:
416 K = CXCursor_UnaryExpr;
417 break;
419 case Stmt::MSPropertySubscriptExprClass:
420 case Stmt::ArraySubscriptExprClass:
421 K = CXCursor_ArraySubscriptExpr;
422 break;
424 case Stmt::MatrixSubscriptExprClass:
425 // TODO: add support for MatrixSubscriptExpr.
426 K = CXCursor_UnexposedExpr;
427 break;
429 case Stmt::ArraySectionExprClass:
430 K = CXCursor_ArraySectionExpr;
431 break;
433 case Stmt::OMPArrayShapingExprClass:
434 K = CXCursor_OMPArrayShapingExpr;
435 break;
437 case Stmt::OMPIteratorExprClass:
438 K = CXCursor_OMPIteratorExpr;
439 break;
441 case Stmt::BinaryOperatorClass:
442 K = CXCursor_BinaryOperator;
443 break;
445 case Stmt::CompoundAssignOperatorClass:
446 K = CXCursor_CompoundAssignOperator;
447 break;
449 case Stmt::ConditionalOperatorClass:
450 K = CXCursor_ConditionalOperator;
451 break;
453 case Stmt::CStyleCastExprClass:
454 K = CXCursor_CStyleCastExpr;
455 break;
457 case Stmt::CompoundLiteralExprClass:
458 K = CXCursor_CompoundLiteralExpr;
459 break;
461 case Stmt::InitListExprClass:
462 K = CXCursor_InitListExpr;
463 break;
465 case Stmt::AddrLabelExprClass:
466 K = CXCursor_AddrLabelExpr;
467 break;
469 case Stmt::StmtExprClass:
470 K = CXCursor_StmtExpr;
471 break;
473 case Stmt::GenericSelectionExprClass:
474 K = CXCursor_GenericSelectionExpr;
475 break;
477 case Stmt::GNUNullExprClass:
478 K = CXCursor_GNUNullExpr;
479 break;
481 case Stmt::CXXStaticCastExprClass:
482 K = CXCursor_CXXStaticCastExpr;
483 break;
485 case Stmt::CXXDynamicCastExprClass:
486 K = CXCursor_CXXDynamicCastExpr;
487 break;
489 case Stmt::CXXReinterpretCastExprClass:
490 K = CXCursor_CXXReinterpretCastExpr;
491 break;
493 case Stmt::CXXConstCastExprClass:
494 K = CXCursor_CXXConstCastExpr;
495 break;
497 case Stmt::CXXFunctionalCastExprClass:
498 K = CXCursor_CXXFunctionalCastExpr;
499 break;
501 case Stmt::CXXAddrspaceCastExprClass:
502 K = CXCursor_CXXAddrspaceCastExpr;
503 break;
505 case Stmt::CXXTypeidExprClass:
506 K = CXCursor_CXXTypeidExpr;
507 break;
509 case Stmt::CXXBoolLiteralExprClass:
510 K = CXCursor_CXXBoolLiteralExpr;
511 break;
513 case Stmt::CXXNullPtrLiteralExprClass:
514 K = CXCursor_CXXNullPtrLiteralExpr;
515 break;
517 case Stmt::CXXThisExprClass:
518 K = CXCursor_CXXThisExpr;
519 break;
521 case Stmt::CXXThrowExprClass:
522 K = CXCursor_CXXThrowExpr;
523 break;
525 case Stmt::CXXNewExprClass:
526 K = CXCursor_CXXNewExpr;
527 break;
529 case Stmt::CXXDeleteExprClass:
530 K = CXCursor_CXXDeleteExpr;
531 break;
533 case Stmt::ObjCStringLiteralClass:
534 K = CXCursor_ObjCStringLiteral;
535 break;
537 case Stmt::ObjCEncodeExprClass:
538 K = CXCursor_ObjCEncodeExpr;
539 break;
541 case Stmt::ObjCSelectorExprClass:
542 K = CXCursor_ObjCSelectorExpr;
543 break;
545 case Stmt::ObjCProtocolExprClass:
546 K = CXCursor_ObjCProtocolExpr;
547 break;
549 case Stmt::ObjCBoolLiteralExprClass:
550 K = CXCursor_ObjCBoolLiteralExpr;
551 break;
553 case Stmt::ObjCAvailabilityCheckExprClass:
554 K = CXCursor_ObjCAvailabilityCheckExpr;
555 break;
557 case Stmt::ObjCBridgedCastExprClass:
558 K = CXCursor_ObjCBridgedCastExpr;
559 break;
561 case Stmt::BlockExprClass:
562 K = CXCursor_BlockExpr;
563 break;
565 case Stmt::PackExpansionExprClass:
566 K = CXCursor_PackExpansionExpr;
567 break;
569 case Stmt::SizeOfPackExprClass:
570 K = CXCursor_SizeOfPackExpr;
571 break;
573 case Stmt::PackIndexingExprClass:
574 K = CXCursor_PackIndexingExpr;
575 break;
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;
584 break;
589 K = CXCursor_DeclRefExpr;
590 break;
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;
599 break;
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;
610 break;
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;
622 break;
624 case Stmt::LambdaExprClass:
625 K = CXCursor_LambdaExpr;
626 break;
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;
647 break;
649 case Stmt::RequiresExprClass:
650 K = CXCursor_RequiresExpr;
651 break;
653 case Stmt::CXXParenListInitExprClass:
654 K = CXCursor_CXXParenListInitExpr;
655 break;
657 case Stmt::MSDependentExistsStmtClass:
658 K = CXCursor_UnexposedStmt;
659 break;
660 case Stmt::OMPCanonicalLoopClass:
661 K = CXCursor_OMPCanonicalLoop;
662 break;
663 case Stmt::OMPMetaDirectiveClass:
664 K = CXCursor_OMPMetaDirective;
665 break;
666 case Stmt::OMPParallelDirectiveClass:
667 K = CXCursor_OMPParallelDirective;
668 break;
669 case Stmt::OMPSimdDirectiveClass:
670 K = CXCursor_OMPSimdDirective;
671 break;
672 case Stmt::OMPTileDirectiveClass:
673 K = CXCursor_OMPTileDirective;
674 break;
675 case Stmt::OMPUnrollDirectiveClass:
676 K = CXCursor_OMPUnrollDirective;
677 break;
678 case Stmt::OMPReverseDirectiveClass:
679 K = CXCursor_OMPReverseDirective;
680 break;
681 case Stmt::OMPInterchangeDirectiveClass:
682 K = CXCursor_OMPTileDirective;
683 break;
684 case Stmt::OMPForDirectiveClass:
685 K = CXCursor_OMPForDirective;
686 break;
687 case Stmt::OMPForSimdDirectiveClass:
688 K = CXCursor_OMPForSimdDirective;
689 break;
690 case Stmt::OMPSectionsDirectiveClass:
691 K = CXCursor_OMPSectionsDirective;
692 break;
693 case Stmt::OMPSectionDirectiveClass:
694 K = CXCursor_OMPSectionDirective;
695 break;
696 case Stmt::OMPScopeDirectiveClass:
697 K = CXCursor_OMPScopeDirective;
698 break;
699 case Stmt::OMPSingleDirectiveClass:
700 K = CXCursor_OMPSingleDirective;
701 break;
702 case Stmt::OMPMasterDirectiveClass:
703 K = CXCursor_OMPMasterDirective;
704 break;
705 case Stmt::OMPCriticalDirectiveClass:
706 K = CXCursor_OMPCriticalDirective;
707 break;
708 case Stmt::OMPParallelForDirectiveClass:
709 K = CXCursor_OMPParallelForDirective;
710 break;
711 case Stmt::OMPParallelForSimdDirectiveClass:
712 K = CXCursor_OMPParallelForSimdDirective;
713 break;
714 case Stmt::OMPParallelMasterDirectiveClass:
715 K = CXCursor_OMPParallelMasterDirective;
716 break;
717 case Stmt::OMPParallelMaskedDirectiveClass:
718 K = CXCursor_OMPParallelMaskedDirective;
719 break;
720 case Stmt::OMPParallelSectionsDirectiveClass:
721 K = CXCursor_OMPParallelSectionsDirective;
722 break;
723 case Stmt::OMPTaskDirectiveClass:
724 K = CXCursor_OMPTaskDirective;
725 break;
726 case Stmt::OMPTaskyieldDirectiveClass:
727 K = CXCursor_OMPTaskyieldDirective;
728 break;
729 case Stmt::OMPBarrierDirectiveClass:
730 K = CXCursor_OMPBarrierDirective;
731 break;
732 case Stmt::OMPTaskwaitDirectiveClass:
733 K = CXCursor_OMPTaskwaitDirective;
734 break;
735 case Stmt::OMPErrorDirectiveClass:
736 K = CXCursor_OMPErrorDirective;
737 break;
738 case Stmt::OMPTaskgroupDirectiveClass:
739 K = CXCursor_OMPTaskgroupDirective;
740 break;
741 case Stmt::OMPFlushDirectiveClass:
742 K = CXCursor_OMPFlushDirective;
743 break;
744 case Stmt::OMPDepobjDirectiveClass:
745 K = CXCursor_OMPDepobjDirective;
746 break;
747 case Stmt::OMPScanDirectiveClass:
748 K = CXCursor_OMPScanDirective;
749 break;
750 case Stmt::OMPOrderedDirectiveClass:
751 K = CXCursor_OMPOrderedDirective;
752 break;
753 case Stmt::OMPAtomicDirectiveClass:
754 K = CXCursor_OMPAtomicDirective;
755 break;
756 case Stmt::OMPTargetDirectiveClass:
757 K = CXCursor_OMPTargetDirective;
758 break;
759 case Stmt::OMPTargetDataDirectiveClass:
760 K = CXCursor_OMPTargetDataDirective;
761 break;
762 case Stmt::OMPTargetEnterDataDirectiveClass:
763 K = CXCursor_OMPTargetEnterDataDirective;
764 break;
765 case Stmt::OMPTargetExitDataDirectiveClass:
766 K = CXCursor_OMPTargetExitDataDirective;
767 break;
768 case Stmt::OMPTargetParallelDirectiveClass:
769 K = CXCursor_OMPTargetParallelDirective;
770 break;
771 case Stmt::OMPTargetParallelForDirectiveClass:
772 K = CXCursor_OMPTargetParallelForDirective;
773 break;
774 case Stmt::OMPTargetUpdateDirectiveClass:
775 K = CXCursor_OMPTargetUpdateDirective;
776 break;
777 case Stmt::OMPTeamsDirectiveClass:
778 K = CXCursor_OMPTeamsDirective;
779 break;
780 case Stmt::OMPCancellationPointDirectiveClass:
781 K = CXCursor_OMPCancellationPointDirective;
782 break;
783 case Stmt::OMPCancelDirectiveClass:
784 K = CXCursor_OMPCancelDirective;
785 break;
786 case Stmt::OMPTaskLoopDirectiveClass:
787 K = CXCursor_OMPTaskLoopDirective;
788 break;
789 case Stmt::OMPTaskLoopSimdDirectiveClass:
790 K = CXCursor_OMPTaskLoopSimdDirective;
791 break;
792 case Stmt::OMPMasterTaskLoopDirectiveClass:
793 K = CXCursor_OMPMasterTaskLoopDirective;
794 break;
795 case Stmt::OMPMaskedTaskLoopDirectiveClass:
796 K = CXCursor_OMPMaskedTaskLoopDirective;
797 break;
798 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
799 K = CXCursor_OMPMasterTaskLoopSimdDirective;
800 break;
801 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
802 K = CXCursor_OMPMaskedTaskLoopSimdDirective;
803 break;
804 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
805 K = CXCursor_OMPParallelMasterTaskLoopDirective;
806 break;
807 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
808 K = CXCursor_OMPParallelMaskedTaskLoopDirective;
809 break;
810 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
811 K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
812 break;
813 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
814 K = CXCursor_OMPParallelMaskedTaskLoopSimdDirective;
815 break;
816 case Stmt::OMPDistributeDirectiveClass:
817 K = CXCursor_OMPDistributeDirective;
818 break;
819 case Stmt::OMPDistributeParallelForDirectiveClass:
820 K = CXCursor_OMPDistributeParallelForDirective;
821 break;
822 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
823 K = CXCursor_OMPDistributeParallelForSimdDirective;
824 break;
825 case Stmt::OMPDistributeSimdDirectiveClass:
826 K = CXCursor_OMPDistributeSimdDirective;
827 break;
828 case Stmt::OMPTargetParallelForSimdDirectiveClass:
829 K = CXCursor_OMPTargetParallelForSimdDirective;
830 break;
831 case Stmt::OMPTargetSimdDirectiveClass:
832 K = CXCursor_OMPTargetSimdDirective;
833 break;
834 case Stmt::OMPTeamsDistributeDirectiveClass:
835 K = CXCursor_OMPTeamsDistributeDirective;
836 break;
837 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
838 K = CXCursor_OMPTeamsDistributeSimdDirective;
839 break;
840 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
841 K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
842 break;
843 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
844 K = CXCursor_OMPTeamsDistributeParallelForDirective;
845 break;
846 case Stmt::OMPTargetTeamsDirectiveClass:
847 K = CXCursor_OMPTargetTeamsDirective;
848 break;
849 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
850 K = CXCursor_OMPTargetTeamsDistributeDirective;
851 break;
852 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
853 K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
854 break;
855 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
856 K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
857 break;
858 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
859 K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
860 break;
861 case Stmt::OMPInteropDirectiveClass:
862 K = CXCursor_OMPInteropDirective;
863 break;
864 case Stmt::OMPDispatchDirectiveClass:
865 K = CXCursor_OMPDispatchDirective;
866 break;
867 case Stmt::OMPMaskedDirectiveClass:
868 K = CXCursor_OMPMaskedDirective;
869 break;
870 case Stmt::OMPGenericLoopDirectiveClass:
871 K = CXCursor_OMPGenericLoopDirective;
872 break;
873 case Stmt::OMPTeamsGenericLoopDirectiveClass:
874 K = CXCursor_OMPTeamsGenericLoopDirective;
875 break;
876 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
877 K = CXCursor_OMPTargetTeamsGenericLoopDirective;
878 break;
879 case Stmt::OMPParallelGenericLoopDirectiveClass:
880 K = CXCursor_OMPParallelGenericLoopDirective;
881 break;
882 case Stmt::OpenACCComputeConstructClass:
883 K = CXCursor_OpenACCComputeConstruct;
884 break;
885 case Stmt::OpenACCLoopConstructClass:
886 K = CXCursor_OpenACCLoopConstruct;
887 break;
888 case Stmt::OpenACCCombinedConstructClass:
889 K = CXCursor_OpenACCCombinedConstruct;
890 break;
891 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
892 K = CXCursor_OMPTargetParallelGenericLoopDirective;
893 break;
894 case Stmt::BuiltinBitCastExprClass:
895 K = CXCursor_BuiltinBitCastExpr;
896 break;
897 case Stmt::OMPAssumeDirectiveClass:
898 K = CXCursor_OMPAssumeDirective;
899 break;
902 CXCursor C = {K, 0, {Parent, S, TU}};
903 return C;
906 CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
907 SourceLocation Loc,
908 CXTranslationUnit TU) {
909 assert(Super && TU && "Invalid arguments!");
910 void *RawLoc = Loc.getPtrEncoding();
911 CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
912 return C;
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,
923 SourceLocation Loc,
924 CXTranslationUnit TU) {
925 assert(Proto && TU && "Invalid arguments!");
926 void *RawLoc = Loc.getPtrEncoding();
927 CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
928 return C;
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,
939 SourceLocation Loc,
940 CXTranslationUnit TU) {
941 // 'Class' can be null for invalid code.
942 if (!Class)
943 return MakeCXCursorInvalid(CXCursor_InvalidCode);
944 assert(TU && "Invalid arguments!");
945 void *RawLoc = Loc.getPtrEncoding();
946 CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
947 return C;
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}};
962 return C;
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,
973 SourceLocation Loc,
974 CXTranslationUnit TU) {
975 assert(Template && TU && "Invalid arguments!");
976 void *RawLoc = Loc.getPtrEncoding();
977 CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
978 return C;
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,
989 SourceLocation Loc,
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}};
996 return C;
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}};
1012 return C;
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,
1023 SourceLocation Loc,
1024 CXTranslationUnit TU) {
1026 assert(Field && TU && "Invalid arguments!");
1027 void *RawLoc = Loc.getPtrEncoding();
1028 CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
1029 return C;
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}};
1042 return C;
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) {
1052 CXCursor C = {
1053 CXCursor_PreprocessingDirective,
1055 {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
1056 return C;
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}};
1070 return C;
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}};
1081 return C;
1084 CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
1085 SourceLocation Loc,
1086 CXTranslationUnit TU) {
1087 assert(Loc.isValid());
1088 CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
1089 return C;
1092 const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
1093 if (isPseudo())
1094 return getAsMacroDefinition()->getName();
1095 return getAsMacroExpansion()->getName();
1097 const MacroDefinitionRecord *
1098 cxcursor::MacroExpansionCursor::getDefinition() const {
1099 if (isPseudo())
1100 return getAsMacroDefinition();
1101 return getAsMacroExpansion()->getDefinition();
1103 SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
1104 if (isPseudo())
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}};
1112 return C;
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}};
1126 return C;
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();
1141 CXCursor C = {
1142 CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1143 return C;
1146 CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
1147 SourceLocation Loc,
1148 CXTranslationUnit TU) {
1149 assert(D && TU && "Invalid arguments!");
1150 void *RawLoc = Loc.getPtrEncoding();
1151 OverloadedDeclRefStorage Storage(D);
1152 CXCursor C = {
1153 CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1154 return C;
1157 CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
1158 SourceLocation Loc,
1159 CXTranslationUnit TU) {
1160 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
1161 void *RawLoc = Loc.getPtrEncoding();
1162 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
1163 CXCursor C = {
1164 CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
1165 return C;
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)
1188 return nullptr;
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);
1203 if (!TU)
1204 return nullptr;
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));
1216 if (!D)
1217 return;
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();
1225 I != E; ++I) {
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) {
1252 if (SelIdx == -1 ||
1253 unsigned(SelIdx) >=
1254 cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
1255 newCursor.xdata = -1;
1256 else
1257 newCursor.xdata = SelIdx;
1258 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1259 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1260 if (SelIdx == -1 ||
1261 unsigned(SelIdx) >=
1262 cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
1263 newCursor.xdata = -1;
1264 else
1265 newCursor.xdata = SelIdx;
1268 return newCursor;
1271 CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1272 if (cursor.kind != CXCursor_CallExpr)
1273 return cursor;
1275 if (cursor.xdata == 0)
1276 return cursor;
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();
1288 if (!Type)
1289 return cursor;
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);
1309 return cursor;
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();
1355 return -1;
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) {
1396 return -1;
1399 if (const auto *FD =
1400 llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
1401 const FunctionTemplateSpecializationInfo *SpecInfo =
1402 FD->getTemplateSpecializationInfo();
1403 if (!SpecInfo) {
1404 return -1;
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();
1415 return -1;
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) {
1443 return -1;
1446 if (const auto *FD =
1447 llvm::dyn_cast_if_present<clang::FunctionDecl>(getCursorDecl(C))) {
1449 const FunctionTemplateSpecializationInfo *SpecInfo =
1450 FD->getTemplateSpecializationInfo();
1451 if (!SpecInfo) {
1452 return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
1455 if (I >= SpecInfo->TemplateArguments->size()) {
1456 return CXGetTemplateArgumentStatus_InvalidIndex;
1459 *TA = SpecInfo->TemplateArguments->get(I);
1460 return 0;
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];
1471 return 0;
1474 return CXGetTemplateArgumentStatus_BadDeclCast;
1477 enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
1478 unsigned I) {
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");
1530 return 0;
1533 if (TA.getKind() != TemplateArgument::Integral) {
1534 assert(0 && "Passed template argument is not Integral");
1535 return 0;
1538 return TA.getAsIntegral().getSExtValue();
1541 unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
1542 unsigned I) {
1543 TemplateArgument TA;
1544 if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
1545 CXGetTemplateArgumentStatus_Success) {
1546 assert(0 && "Unable to retrieve TemplateArgument");
1547 return 0;
1550 if (TA.getKind() != TemplateArgument::Integral) {
1551 assert(0 && "Passed template argument is not Integral");
1552 return 0;
1555 return TA.getAsIntegral().getZExtValue();
1558 //===----------------------------------------------------------------------===//
1559 // CXCursorSet.
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;
1570 namespace llvm {
1571 template <> struct DenseMapInfo<CXCursor> {
1572 public:
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];
1587 } // namespace llvm
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);
1599 if (!setImpl)
1600 return 0;
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)
1608 return 1;
1610 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1611 if (!setImpl)
1612 return 1;
1613 unsigned &entry = (*setImpl)[cursor];
1614 unsigned flag = entry == 0 ? 1 : 0;
1615 entry = 1;
1616 return flag;
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);
1631 return String;
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(
1638 Macro,
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);
1645 return String;
1647 return nullptr;
1650 namespace {
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();
1659 I != E; ++I) {
1660 delete *I;
1664 } // namespace
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) {
1676 if (overridden)
1677 *overridden = nullptr;
1678 if (num_overridden)
1679 *num_overridden = 0;
1681 CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
1683 if (!overridden || !num_overridden || !TU)
1684 return;
1686 if (!clang_isDeclaration(cursor.kind))
1687 return;
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();
1697 } else {
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.
1704 Vec->clear();
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);
1720 return;
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) {
1730 if (!overridden)
1731 return;
1733 // Use pointer arithmetic to get back the first faux entry
1734 // which has a back-reference to the TU and the vector.
1735 --overridden;
1736 OverridenCursorsPool::CursorVec *Vec =
1737 static_cast<OverridenCursorsPool::CursorVec *>(
1738 const_cast<void *>(overridden->data[0]));
1739 CXTranslationUnit TU = getCursorTU(*overridden);
1741 assert(Vec && TU);
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);
1753 if (!E)
1754 return 0;
1756 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
1757 if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
1758 return false;
1759 if (auto *RecE = dyn_cast<ObjCMessageExpr>(
1760 MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
1761 if (RecE->getMethodFamily() == OMF_alloc)
1762 return false;
1764 return true;
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());
1777 if (ME) {
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());
1785 return 0;
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());
1808 if (ME) {
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);