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.
12//
13//===----------------------------------------------------------------------===//
14
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"
30
31using namespace clang;
32using namespace cxcursor;
33
34CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
35 assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
36 CXCursor C = {.kind: K, .xdata: 0, .data: {nullptr, nullptr, TU}};
37 return C;
38}
39
40static 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;
127 }
128
129 return CXCursor_UnexposedAttr;
130}
131
132CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
133 CXTranslationUnit TU) {
134 assert(A && Parent && TU && "Invalid arguments!");
135 CXCursor C = {.kind: GetCursorKind(A), .xdata: 0, .data: {Parent, A, TU}};
136 return C;
137}
138
139CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
140 SourceRange RegionOfInterest,
141 bool FirstInDeclGroup) {
142 assert(D && TU && "Invalid arguments!");
143
144 CXCursorKind K = getCursorKindForDecl(D);
145
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>(Val: D)->getSelectorLocs(SelLocs);
154 SmallVectorImpl<SourceLocation>::iterator I =
155 llvm::find(Range&: SelLocs, Val: RegionOfInterest.getBegin());
156 if (I != SelLocs.end())
157 SelectorIdIndex = I - SelLocs.begin();
158 }
159 CXCursor C = {.kind: K,
160 .xdata: SelectorIdIndex,
161 .data: {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
162 return C;
163 }
164
165 CXCursor C = {.kind: K, .xdata: 0, .data: {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
166 return C;
167}
168
169CXCursor 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;
174
175 switch (S->getStmtClass()) {
176 case Stmt::NoStmtClass:
177 break;
178
179 case Stmt::CaseStmtClass:
180 K = CXCursor_CaseStmt;
181 break;
182
183 case Stmt::DefaultStmtClass:
184 K = CXCursor_DefaultStmt;
185 break;
186
187 case Stmt::IfStmtClass:
188 K = CXCursor_IfStmt;
189 break;
190
191 case Stmt::SwitchStmtClass:
192 K = CXCursor_SwitchStmt;
193 break;
194
195 case Stmt::WhileStmtClass:
196 K = CXCursor_WhileStmt;
197 break;
198
199 case Stmt::DoStmtClass:
200 K = CXCursor_DoStmt;
201 break;
202
203 case Stmt::ForStmtClass:
204 K = CXCursor_ForStmt;
205 break;
206
207 case Stmt::GotoStmtClass:
208 K = CXCursor_GotoStmt;
209 break;
210
211 case Stmt::IndirectGotoStmtClass:
212 K = CXCursor_IndirectGotoStmt;
213 break;
214
215 case Stmt::ContinueStmtClass:
216 K = CXCursor_ContinueStmt;
217 break;
218
219 case Stmt::BreakStmtClass:
220 K = CXCursor_BreakStmt;
221 break;
222
223 case Stmt::ReturnStmtClass:
224 K = CXCursor_ReturnStmt;
225 break;
226
227 case Stmt::GCCAsmStmtClass:
228 K = CXCursor_GCCAsmStmt;
229 break;
230
231 case Stmt::MSAsmStmtClass:
232 K = CXCursor_MSAsmStmt;
233 break;
234
235 case Stmt::ObjCAtTryStmtClass:
236 K = CXCursor_ObjCAtTryStmt;
237 break;
238
239 case Stmt::ObjCAtCatchStmtClass:
240 K = CXCursor_ObjCAtCatchStmt;
241 break;
242
243 case Stmt::ObjCAtFinallyStmtClass:
244 K = CXCursor_ObjCAtFinallyStmt;
245 break;
246
247 case Stmt::ObjCAtThrowStmtClass:
248 K = CXCursor_ObjCAtThrowStmt;
249 break;
250
251 case Stmt::ObjCAtSynchronizedStmtClass:
252 K = CXCursor_ObjCAtSynchronizedStmt;
253 break;
254
255 case Stmt::ObjCAutoreleasePoolStmtClass:
256 K = CXCursor_ObjCAutoreleasePoolStmt;
257 break;
258
259 case Stmt::ObjCForCollectionStmtClass:
260 K = CXCursor_ObjCForCollectionStmt;
261 break;
262
263 case Stmt::CXXCatchStmtClass:
264 K = CXCursor_CXXCatchStmt;
265 break;
266
267 case Stmt::CXXTryStmtClass:
268 K = CXCursor_CXXTryStmt;
269 break;
270
271 case Stmt::CXXForRangeStmtClass:
272 K = CXCursor_CXXForRangeStmt;
273 break;
274
275 case Stmt::SEHTryStmtClass:
276 K = CXCursor_SEHTryStmt;
277 break;
278
279 case Stmt::SEHExceptStmtClass:
280 K = CXCursor_SEHExceptStmt;
281 break;
282
283 case Stmt::SEHFinallyStmtClass:
284 K = CXCursor_SEHFinallyStmt;
285 break;
286
287 case Stmt::SEHLeaveStmtClass:
288 K = CXCursor_SEHLeaveStmt;
289 break;
290
291 case Stmt::CoroutineBodyStmtClass:
292 case Stmt::CoreturnStmtClass:
293 K = CXCursor_UnexposedStmt;
294 break;
295
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;
340
341 case Stmt::OpaqueValueExprClass:
342 if (Expr *Src = cast<OpaqueValueExpr>(Val: S)->getSourceExpr())
343 return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
344 K = CXCursor_UnexposedExpr;
345 break;
346
347 case Stmt::PseudoObjectExprClass:
348 return MakeCXCursor(cast<PseudoObjectExpr>(Val: S)->getSyntacticForm(), Parent,
349 TU, RegionOfInterest);
350
351 case Stmt::CompoundStmtClass:
352 K = CXCursor_CompoundStmt;
353 break;
354
355 case Stmt::NullStmtClass:
356 K = CXCursor_NullStmt;
357 break;
358
359 case Stmt::LabelStmtClass:
360 K = CXCursor_LabelStmt;
361 break;
362
363 case Stmt::AttributedStmtClass:
364 K = CXCursor_UnexposedStmt;
365 break;
366
367 case Stmt::DeclStmtClass:
368 K = CXCursor_DeclStmt;
369 break;
370
371 case Stmt::CapturedStmtClass:
372 K = CXCursor_UnexposedStmt;
373 break;
374
375 case Stmt::IntegerLiteralClass:
376 K = CXCursor_IntegerLiteral;
377 break;
378
379 case Stmt::FixedPointLiteralClass:
380 K = CXCursor_FixedPointLiteral;
381 break;
382
383 case Stmt::FloatingLiteralClass:
384 K = CXCursor_FloatingLiteral;
385 break;
386
387 case Stmt::ImaginaryLiteralClass:
388 K = CXCursor_ImaginaryLiteral;
389 break;
390
391 case Stmt::StringLiteralClass:
392 K = CXCursor_StringLiteral;
393 break;
394
395 case Stmt::CharacterLiteralClass:
396 K = CXCursor_CharacterLiteral;
397 break;
398
399 case Stmt::ConstantExprClass:
400 return MakeCXCursor(cast<ConstantExpr>(Val: S)->getSubExpr(), Parent, TU,
401 RegionOfInterest);
402
403 case Stmt::ParenExprClass:
404 K = CXCursor_ParenExpr;
405 break;
406
407 case Stmt::UnaryOperatorClass:
408 K = CXCursor_UnaryOperator;
409 break;
410
411 case Stmt::UnaryExprOrTypeTraitExprClass:
412 case Stmt::CXXNoexceptExprClass:
413 K = CXCursor_UnaryExpr;
414 break;
415
416 case Stmt::MSPropertySubscriptExprClass:
417 case Stmt::ArraySubscriptExprClass:
418 K = CXCursor_ArraySubscriptExpr;
419 break;
420
421 case Stmt::MatrixSubscriptExprClass:
422 // TODO: add support for MatrixSubscriptExpr.
423 K = CXCursor_UnexposedExpr;
424 break;
425
426 case Stmt::OMPArraySectionExprClass:
427 K = CXCursor_OMPArraySectionExpr;
428 break;
429
430 case Stmt::OMPArrayShapingExprClass:
431 K = CXCursor_OMPArrayShapingExpr;
432 break;
433
434 case Stmt::OMPIteratorExprClass:
435 K = CXCursor_OMPIteratorExpr;
436 break;
437
438 case Stmt::BinaryOperatorClass:
439 K = CXCursor_BinaryOperator;
440 break;
441
442 case Stmt::CompoundAssignOperatorClass:
443 K = CXCursor_CompoundAssignOperator;
444 break;
445
446 case Stmt::ConditionalOperatorClass:
447 K = CXCursor_ConditionalOperator;
448 break;
449
450 case Stmt::CStyleCastExprClass:
451 K = CXCursor_CStyleCastExpr;
452 break;
453
454 case Stmt::CompoundLiteralExprClass:
455 K = CXCursor_CompoundLiteralExpr;
456 break;
457
458 case Stmt::InitListExprClass:
459 K = CXCursor_InitListExpr;
460 break;
461
462 case Stmt::AddrLabelExprClass:
463 K = CXCursor_AddrLabelExpr;
464 break;
465
466 case Stmt::StmtExprClass:
467 K = CXCursor_StmtExpr;
468 break;
469
470 case Stmt::GenericSelectionExprClass:
471 K = CXCursor_GenericSelectionExpr;
472 break;
473
474 case Stmt::GNUNullExprClass:
475 K = CXCursor_GNUNullExpr;
476 break;
477
478 case Stmt::CXXStaticCastExprClass:
479 K = CXCursor_CXXStaticCastExpr;
480 break;
481
482 case Stmt::CXXDynamicCastExprClass:
483 K = CXCursor_CXXDynamicCastExpr;
484 break;
485
486 case Stmt::CXXReinterpretCastExprClass:
487 K = CXCursor_CXXReinterpretCastExpr;
488 break;
489
490 case Stmt::CXXConstCastExprClass:
491 K = CXCursor_CXXConstCastExpr;
492 break;
493
494 case Stmt::CXXFunctionalCastExprClass:
495 K = CXCursor_CXXFunctionalCastExpr;
496 break;
497
498 case Stmt::CXXAddrspaceCastExprClass:
499 K = CXCursor_CXXAddrspaceCastExpr;
500 break;
501
502 case Stmt::CXXTypeidExprClass:
503 K = CXCursor_CXXTypeidExpr;
504 break;
505
506 case Stmt::CXXBoolLiteralExprClass:
507 K = CXCursor_CXXBoolLiteralExpr;
508 break;
509
510 case Stmt::CXXNullPtrLiteralExprClass:
511 K = CXCursor_CXXNullPtrLiteralExpr;
512 break;
513
514 case Stmt::CXXThisExprClass:
515 K = CXCursor_CXXThisExpr;
516 break;
517
518 case Stmt::CXXThrowExprClass:
519 K = CXCursor_CXXThrowExpr;
520 break;
521
522 case Stmt::CXXNewExprClass:
523 K = CXCursor_CXXNewExpr;
524 break;
525
526 case Stmt::CXXDeleteExprClass:
527 K = CXCursor_CXXDeleteExpr;
528 break;
529
530 case Stmt::ObjCStringLiteralClass:
531 K = CXCursor_ObjCStringLiteral;
532 break;
533
534 case Stmt::ObjCEncodeExprClass:
535 K = CXCursor_ObjCEncodeExpr;
536 break;
537
538 case Stmt::ObjCSelectorExprClass:
539 K = CXCursor_ObjCSelectorExpr;
540 break;
541
542 case Stmt::ObjCProtocolExprClass:
543 K = CXCursor_ObjCProtocolExpr;
544 break;
545
546 case Stmt::ObjCBoolLiteralExprClass:
547 K = CXCursor_ObjCBoolLiteralExpr;
548 break;
549
550 case Stmt::ObjCAvailabilityCheckExprClass:
551 K = CXCursor_ObjCAvailabilityCheckExpr;
552 break;
553
554 case Stmt::ObjCBridgedCastExprClass:
555 K = CXCursor_ObjCBridgedCastExpr;
556 break;
557
558 case Stmt::BlockExprClass:
559 K = CXCursor_BlockExpr;
560 break;
561
562 case Stmt::PackExpansionExprClass:
563 K = CXCursor_PackExpansionExpr;
564 break;
565
566 case Stmt::SizeOfPackExprClass:
567 K = CXCursor_SizeOfPackExpr;
568 break;
569
570 case Stmt::PackIndexingExprClass:
571 K = CXCursor_PackIndexingExpr;
572 break;
573
574 case Stmt::DeclRefExprClass:
575 if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
576 Val: cast<DeclRefExpr>(Val: S)->getDecl())) {
577 if (const ObjCMethodDecl *MD =
578 dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
579 if (MD->getSelfDecl() == IPD) {
580 K = CXCursor_ObjCSelfExpr;
581 break;
582 }
583 }
584 }
585
586 K = CXCursor_DeclRefExpr;
587 break;
588
589 case Stmt::DependentScopeDeclRefExprClass:
590 case Stmt::SubstNonTypeTemplateParmExprClass:
591 case Stmt::SubstNonTypeTemplateParmPackExprClass:
592 case Stmt::FunctionParmPackExprClass:
593 case Stmt::UnresolvedLookupExprClass:
594 case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
595 K = CXCursor_DeclRefExpr;
596 break;
597
598 case Stmt::CXXDependentScopeMemberExprClass:
599 case Stmt::CXXPseudoDestructorExprClass:
600 case Stmt::MemberExprClass:
601 case Stmt::MSPropertyRefExprClass:
602 case Stmt::ObjCIsaExprClass:
603 case Stmt::ObjCIvarRefExprClass:
604 case Stmt::ObjCPropertyRefExprClass:
605 case Stmt::UnresolvedMemberExprClass:
606 K = CXCursor_MemberRefExpr;
607 break;
608
609 case Stmt::CallExprClass:
610 case Stmt::CXXOperatorCallExprClass:
611 case Stmt::CXXMemberCallExprClass:
612 case Stmt::CUDAKernelCallExprClass:
613 case Stmt::CXXConstructExprClass:
614 case Stmt::CXXInheritedCtorInitExprClass:
615 case Stmt::CXXTemporaryObjectExprClass:
616 case Stmt::CXXUnresolvedConstructExprClass:
617 case Stmt::UserDefinedLiteralClass:
618 K = CXCursor_CallExpr;
619 break;
620
621 case Stmt::LambdaExprClass:
622 K = CXCursor_LambdaExpr;
623 break;
624
625 case Stmt::ObjCMessageExprClass: {
626 K = CXCursor_ObjCMessageExpr;
627 int SelectorIdIndex = -1;
628 // Check if cursor points to a selector id.
629 if (RegionOfInterest.isValid() &&
630 RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
631 SmallVector<SourceLocation, 16> SelLocs;
632 cast<ObjCMessageExpr>(Val: S)->getSelectorLocs(SelLocs);
633 SmallVectorImpl<SourceLocation>::iterator I =
634 llvm::find(Range&: SelLocs, Val: RegionOfInterest.getBegin());
635 if (I != SelLocs.end())
636 SelectorIdIndex = I - SelLocs.begin();
637 }
638 CXCursor C = {.kind: K, .xdata: 0, .data: {Parent, S, TU}};
639 return getSelectorIdentifierCursor(SelIdx: SelectorIdIndex, cursor: C);
640 }
641
642 case Stmt::ConceptSpecializationExprClass:
643 K = CXCursor_ConceptSpecializationExpr;
644 break;
645
646 case Stmt::RequiresExprClass:
647 K = CXCursor_RequiresExpr;
648 break;
649
650 case Stmt::CXXParenListInitExprClass:
651 K = CXCursor_CXXParenListInitExpr;
652 break;
653
654 case Stmt::MSDependentExistsStmtClass:
655 K = CXCursor_UnexposedStmt;
656 break;
657 case Stmt::OMPCanonicalLoopClass:
658 K = CXCursor_OMPCanonicalLoop;
659 break;
660 case Stmt::OMPMetaDirectiveClass:
661 K = CXCursor_OMPMetaDirective;
662 break;
663 case Stmt::OMPParallelDirectiveClass:
664 K = CXCursor_OMPParallelDirective;
665 break;
666 case Stmt::OMPSimdDirectiveClass:
667 K = CXCursor_OMPSimdDirective;
668 break;
669 case Stmt::OMPTileDirectiveClass:
670 K = CXCursor_OMPTileDirective;
671 break;
672 case Stmt::OMPUnrollDirectiveClass:
673 K = CXCursor_OMPUnrollDirective;
674 break;
675 case Stmt::OMPForDirectiveClass:
676 K = CXCursor_OMPForDirective;
677 break;
678 case Stmt::OMPForSimdDirectiveClass:
679 K = CXCursor_OMPForSimdDirective;
680 break;
681 case Stmt::OMPSectionsDirectiveClass:
682 K = CXCursor_OMPSectionsDirective;
683 break;
684 case Stmt::OMPSectionDirectiveClass:
685 K = CXCursor_OMPSectionDirective;
686 break;
687 case Stmt::OMPScopeDirectiveClass:
688 K = CXCursor_OMPScopeDirective;
689 break;
690 case Stmt::OMPSingleDirectiveClass:
691 K = CXCursor_OMPSingleDirective;
692 break;
693 case Stmt::OMPMasterDirectiveClass:
694 K = CXCursor_OMPMasterDirective;
695 break;
696 case Stmt::OMPCriticalDirectiveClass:
697 K = CXCursor_OMPCriticalDirective;
698 break;
699 case Stmt::OMPParallelForDirectiveClass:
700 K = CXCursor_OMPParallelForDirective;
701 break;
702 case Stmt::OMPParallelForSimdDirectiveClass:
703 K = CXCursor_OMPParallelForSimdDirective;
704 break;
705 case Stmt::OMPParallelMasterDirectiveClass:
706 K = CXCursor_OMPParallelMasterDirective;
707 break;
708 case Stmt::OMPParallelMaskedDirectiveClass:
709 K = CXCursor_OMPParallelMaskedDirective;
710 break;
711 case Stmt::OMPParallelSectionsDirectiveClass:
712 K = CXCursor_OMPParallelSectionsDirective;
713 break;
714 case Stmt::OMPTaskDirectiveClass:
715 K = CXCursor_OMPTaskDirective;
716 break;
717 case Stmt::OMPTaskyieldDirectiveClass:
718 K = CXCursor_OMPTaskyieldDirective;
719 break;
720 case Stmt::OMPBarrierDirectiveClass:
721 K = CXCursor_OMPBarrierDirective;
722 break;
723 case Stmt::OMPTaskwaitDirectiveClass:
724 K = CXCursor_OMPTaskwaitDirective;
725 break;
726 case Stmt::OMPErrorDirectiveClass:
727 K = CXCursor_OMPErrorDirective;
728 break;
729 case Stmt::OMPTaskgroupDirectiveClass:
730 K = CXCursor_OMPTaskgroupDirective;
731 break;
732 case Stmt::OMPFlushDirectiveClass:
733 K = CXCursor_OMPFlushDirective;
734 break;
735 case Stmt::OMPDepobjDirectiveClass:
736 K = CXCursor_OMPDepobjDirective;
737 break;
738 case Stmt::OMPScanDirectiveClass:
739 K = CXCursor_OMPScanDirective;
740 break;
741 case Stmt::OMPOrderedDirectiveClass:
742 K = CXCursor_OMPOrderedDirective;
743 break;
744 case Stmt::OMPAtomicDirectiveClass:
745 K = CXCursor_OMPAtomicDirective;
746 break;
747 case Stmt::OMPTargetDirectiveClass:
748 K = CXCursor_OMPTargetDirective;
749 break;
750 case Stmt::OMPTargetDataDirectiveClass:
751 K = CXCursor_OMPTargetDataDirective;
752 break;
753 case Stmt::OMPTargetEnterDataDirectiveClass:
754 K = CXCursor_OMPTargetEnterDataDirective;
755 break;
756 case Stmt::OMPTargetExitDataDirectiveClass:
757 K = CXCursor_OMPTargetExitDataDirective;
758 break;
759 case Stmt::OMPTargetParallelDirectiveClass:
760 K = CXCursor_OMPTargetParallelDirective;
761 break;
762 case Stmt::OMPTargetParallelForDirectiveClass:
763 K = CXCursor_OMPTargetParallelForDirective;
764 break;
765 case Stmt::OMPTargetUpdateDirectiveClass:
766 K = CXCursor_OMPTargetUpdateDirective;
767 break;
768 case Stmt::OMPTeamsDirectiveClass:
769 K = CXCursor_OMPTeamsDirective;
770 break;
771 case Stmt::OMPCancellationPointDirectiveClass:
772 K = CXCursor_OMPCancellationPointDirective;
773 break;
774 case Stmt::OMPCancelDirectiveClass:
775 K = CXCursor_OMPCancelDirective;
776 break;
777 case Stmt::OMPTaskLoopDirectiveClass:
778 K = CXCursor_OMPTaskLoopDirective;
779 break;
780 case Stmt::OMPTaskLoopSimdDirectiveClass:
781 K = CXCursor_OMPTaskLoopSimdDirective;
782 break;
783 case Stmt::OMPMasterTaskLoopDirectiveClass:
784 K = CXCursor_OMPMasterTaskLoopDirective;
785 break;
786 case Stmt::OMPMaskedTaskLoopDirectiveClass:
787 K = CXCursor_OMPMaskedTaskLoopDirective;
788 break;
789 case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
790 K = CXCursor_OMPMasterTaskLoopSimdDirective;
791 break;
792 case Stmt::OMPMaskedTaskLoopSimdDirectiveClass:
793 K = CXCursor_OMPMaskedTaskLoopSimdDirective;
794 break;
795 case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
796 K = CXCursor_OMPParallelMasterTaskLoopDirective;
797 break;
798 case Stmt::OMPParallelMaskedTaskLoopDirectiveClass:
799 K = CXCursor_OMPParallelMaskedTaskLoopDirective;
800 break;
801 case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
802 K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
803 break;
804 case Stmt::OMPParallelMaskedTaskLoopSimdDirectiveClass:
805 K = CXCursor_OMPParallelMaskedTaskLoopSimdDirective;
806 break;
807 case Stmt::OMPDistributeDirectiveClass:
808 K = CXCursor_OMPDistributeDirective;
809 break;
810 case Stmt::OMPDistributeParallelForDirectiveClass:
811 K = CXCursor_OMPDistributeParallelForDirective;
812 break;
813 case Stmt::OMPDistributeParallelForSimdDirectiveClass:
814 K = CXCursor_OMPDistributeParallelForSimdDirective;
815 break;
816 case Stmt::OMPDistributeSimdDirectiveClass:
817 K = CXCursor_OMPDistributeSimdDirective;
818 break;
819 case Stmt::OMPTargetParallelForSimdDirectiveClass:
820 K = CXCursor_OMPTargetParallelForSimdDirective;
821 break;
822 case Stmt::OMPTargetSimdDirectiveClass:
823 K = CXCursor_OMPTargetSimdDirective;
824 break;
825 case Stmt::OMPTeamsDistributeDirectiveClass:
826 K = CXCursor_OMPTeamsDistributeDirective;
827 break;
828 case Stmt::OMPTeamsDistributeSimdDirectiveClass:
829 K = CXCursor_OMPTeamsDistributeSimdDirective;
830 break;
831 case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
832 K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
833 break;
834 case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
835 K = CXCursor_OMPTeamsDistributeParallelForDirective;
836 break;
837 case Stmt::OMPTargetTeamsDirectiveClass:
838 K = CXCursor_OMPTargetTeamsDirective;
839 break;
840 case Stmt::OMPTargetTeamsDistributeDirectiveClass:
841 K = CXCursor_OMPTargetTeamsDistributeDirective;
842 break;
843 case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
844 K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
845 break;
846 case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
847 K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
848 break;
849 case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
850 K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
851 break;
852 case Stmt::OMPInteropDirectiveClass:
853 K = CXCursor_OMPInteropDirective;
854 break;
855 case Stmt::OMPDispatchDirectiveClass:
856 K = CXCursor_OMPDispatchDirective;
857 break;
858 case Stmt::OMPMaskedDirectiveClass:
859 K = CXCursor_OMPMaskedDirective;
860 break;
861 case Stmt::OMPGenericLoopDirectiveClass:
862 K = CXCursor_OMPGenericLoopDirective;
863 break;
864 case Stmt::OMPTeamsGenericLoopDirectiveClass:
865 K = CXCursor_OMPTeamsGenericLoopDirective;
866 break;
867 case Stmt::OMPTargetTeamsGenericLoopDirectiveClass:
868 K = CXCursor_OMPTargetTeamsGenericLoopDirective;
869 break;
870 case Stmt::OMPParallelGenericLoopDirectiveClass:
871 K = CXCursor_OMPParallelGenericLoopDirective;
872 break;
873 case Stmt::OpenACCComputeConstructClass:
874 K = CXCursor_OpenACCComputeConstruct;
875 break;
876 case Stmt::OMPTargetParallelGenericLoopDirectiveClass:
877 K = CXCursor_OMPTargetParallelGenericLoopDirective;
878 break;
879 case Stmt::BuiltinBitCastExprClass:
880 K = CXCursor_BuiltinBitCastExpr;
881 }
882
883 CXCursor C = {.kind: K, .xdata: 0, .data: {Parent, S, TU}};
884 return C;
885}
886
887CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
888 SourceLocation Loc,
889 CXTranslationUnit TU) {
890 assert(Super && TU && "Invalid arguments!");
891 void *RawLoc = Loc.getPtrEncoding();
892 CXCursor C = {.kind: CXCursor_ObjCSuperClassRef, .xdata: 0, .data: {Super, RawLoc, TU}};
893 return C;
894}
895
896std::pair<const ObjCInterfaceDecl *, SourceLocation>
897cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
898 assert(C.kind == CXCursor_ObjCSuperClassRef);
899 return std::make_pair(x: static_cast<const ObjCInterfaceDecl *>(C.data[0]),
900 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
901}
902
903CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
904 SourceLocation Loc,
905 CXTranslationUnit TU) {
906 assert(Proto && TU && "Invalid arguments!");
907 void *RawLoc = Loc.getPtrEncoding();
908 CXCursor C = {.kind: CXCursor_ObjCProtocolRef, .xdata: 0, .data: {Proto, RawLoc, TU}};
909 return C;
910}
911
912std::pair<const ObjCProtocolDecl *, SourceLocation>
913cxcursor::getCursorObjCProtocolRef(CXCursor C) {
914 assert(C.kind == CXCursor_ObjCProtocolRef);
915 return std::make_pair(x: static_cast<const ObjCProtocolDecl *>(C.data[0]),
916 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
917}
918
919CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
920 SourceLocation Loc,
921 CXTranslationUnit TU) {
922 // 'Class' can be null for invalid code.
923 if (!Class)
924 return MakeCXCursorInvalid(K: CXCursor_InvalidCode);
925 assert(TU && "Invalid arguments!");
926 void *RawLoc = Loc.getPtrEncoding();
927 CXCursor C = {.kind: CXCursor_ObjCClassRef, .xdata: 0, .data: {Class, RawLoc, TU}};
928 return C;
929}
930
931std::pair<const ObjCInterfaceDecl *, SourceLocation>
932cxcursor::getCursorObjCClassRef(CXCursor C) {
933 assert(C.kind == CXCursor_ObjCClassRef);
934 return std::make_pair(x: static_cast<const ObjCInterfaceDecl *>(C.data[0]),
935 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
936}
937
938CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
939 CXTranslationUnit TU) {
940 assert(Type && TU && "Invalid arguments!");
941 void *RawLoc = Loc.getPtrEncoding();
942 CXCursor C = {.kind: CXCursor_TypeRef, .xdata: 0, .data: {Type, RawLoc, TU}};
943 return C;
944}
945
946std::pair<const TypeDecl *, SourceLocation>
947cxcursor::getCursorTypeRef(CXCursor C) {
948 assert(C.kind == CXCursor_TypeRef);
949 return std::make_pair(x: static_cast<const TypeDecl *>(C.data[0]),
950 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
951}
952
953CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
954 SourceLocation Loc,
955 CXTranslationUnit TU) {
956 assert(Template && TU && "Invalid arguments!");
957 void *RawLoc = Loc.getPtrEncoding();
958 CXCursor C = {.kind: CXCursor_TemplateRef, .xdata: 0, .data: {Template, RawLoc, TU}};
959 return C;
960}
961
962std::pair<const TemplateDecl *, SourceLocation>
963cxcursor::getCursorTemplateRef(CXCursor C) {
964 assert(C.kind == CXCursor_TemplateRef);
965 return std::make_pair(x: static_cast<const TemplateDecl *>(C.data[0]),
966 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
967}
968
969CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
970 SourceLocation Loc,
971 CXTranslationUnit TU) {
972
973 assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
974 "Invalid arguments!");
975 void *RawLoc = Loc.getPtrEncoding();
976 CXCursor C = {.kind: CXCursor_NamespaceRef, .xdata: 0, .data: {NS, RawLoc, TU}};
977 return C;
978}
979
980std::pair<const NamedDecl *, SourceLocation>
981cxcursor::getCursorNamespaceRef(CXCursor C) {
982 assert(C.kind == CXCursor_NamespaceRef);
983 return std::make_pair(x: static_cast<const NamedDecl *>(C.data[0]),
984 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
985}
986
987CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
988 CXTranslationUnit TU) {
989
990 assert(Var && TU && "Invalid arguments!");
991 void *RawLoc = Loc.getPtrEncoding();
992 CXCursor C = {.kind: CXCursor_VariableRef, .xdata: 0, .data: {Var, RawLoc, TU}};
993 return C;
994}
995
996std::pair<const VarDecl *, SourceLocation>
997cxcursor::getCursorVariableRef(CXCursor C) {
998 assert(C.kind == CXCursor_VariableRef);
999 return std::make_pair(x: static_cast<const VarDecl *>(C.data[0]),
1000 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
1001}
1002
1003CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
1004 SourceLocation Loc,
1005 CXTranslationUnit TU) {
1006
1007 assert(Field && TU && "Invalid arguments!");
1008 void *RawLoc = Loc.getPtrEncoding();
1009 CXCursor C = {.kind: CXCursor_MemberRef, .xdata: 0, .data: {Field, RawLoc, TU}};
1010 return C;
1011}
1012
1013std::pair<const FieldDecl *, SourceLocation>
1014cxcursor::getCursorMemberRef(CXCursor C) {
1015 assert(C.kind == CXCursor_MemberRef);
1016 return std::make_pair(x: static_cast<const FieldDecl *>(C.data[0]),
1017 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
1018}
1019
1020CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
1021 CXTranslationUnit TU) {
1022 CXCursor C = {.kind: CXCursor_CXXBaseSpecifier, .xdata: 0, .data: {B, nullptr, TU}};
1023 return C;
1024}
1025
1026const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
1027 assert(C.kind == CXCursor_CXXBaseSpecifier);
1028 return static_cast<const CXXBaseSpecifier *>(C.data[0]);
1029}
1030
1031CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
1032 CXTranslationUnit TU) {
1033 CXCursor C = {
1034 .kind: CXCursor_PreprocessingDirective,
1035 .xdata: 0,
1036 .data: {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
1037 return C;
1038}
1039
1040SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
1041 assert(C.kind == CXCursor_PreprocessingDirective);
1042 SourceRange Range(SourceLocation::getFromPtrEncoding(Encoding: C.data[0]),
1043 SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
1044 ASTUnit *TU = getCursorASTUnit(Cursor: C);
1045 return TU->mapRangeFromPreamble(R: Range);
1046}
1047
1048CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
1049 CXTranslationUnit TU) {
1050 CXCursor C = {.kind: CXCursor_MacroDefinition, .xdata: 0, .data: {MI, nullptr, TU}};
1051 return C;
1052}
1053
1054const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
1055 assert(C.kind == CXCursor_MacroDefinition);
1056 return static_cast<const MacroDefinitionRecord *>(C.data[0]);
1057}
1058
1059CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
1060 CXTranslationUnit TU) {
1061 CXCursor C = {.kind: CXCursor_MacroExpansion, .xdata: 0, .data: {MI, nullptr, TU}};
1062 return C;
1063}
1064
1065CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
1066 SourceLocation Loc,
1067 CXTranslationUnit TU) {
1068 assert(Loc.isValid());
1069 CXCursor C = {.kind: CXCursor_MacroExpansion, .xdata: 0, .data: {MI, Loc.getPtrEncoding(), TU}};
1070 return C;
1071}
1072
1073const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
1074 if (isPseudo())
1075 return getAsMacroDefinition()->getName();
1076 return getAsMacroExpansion()->getName();
1077}
1078const MacroDefinitionRecord *
1079cxcursor::MacroExpansionCursor::getDefinition() const {
1080 if (isPseudo())
1081 return getAsMacroDefinition();
1082 return getAsMacroExpansion()->getDefinition();
1083}
1084SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
1085 if (isPseudo())
1086 return getPseudoLoc();
1087 return getAsMacroExpansion()->getSourceRange();
1088}
1089
1090CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
1091 CXTranslationUnit TU) {
1092 CXCursor C = {.kind: CXCursor_InclusionDirective, .xdata: 0, .data: {ID, nullptr, TU}};
1093 return C;
1094}
1095
1096const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
1097 assert(C.kind == CXCursor_InclusionDirective);
1098 return static_cast<const InclusionDirective *>(C.data[0]);
1099}
1100
1101CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
1102 CXTranslationUnit TU) {
1103
1104 assert(Label && TU && "Invalid arguments!");
1105 void *RawLoc = Loc.getPtrEncoding();
1106 CXCursor C = {.kind: CXCursor_LabelRef, .xdata: 0, .data: {Label, RawLoc, TU}};
1107 return C;
1108}
1109
1110std::pair<const LabelStmt *, SourceLocation>
1111cxcursor::getCursorLabelRef(CXCursor C) {
1112 assert(C.kind == CXCursor_LabelRef);
1113 return std::make_pair(x: static_cast<const LabelStmt *>(C.data[0]),
1114 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
1115}
1116
1117CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
1118 CXTranslationUnit TU) {
1119 assert(E && TU && "Invalid arguments!");
1120 OverloadedDeclRefStorage Storage(E);
1121 void *RawLoc = E->getNameLoc().getPtrEncoding();
1122 CXCursor C = {
1123 .kind: CXCursor_OverloadedDeclRef, .xdata: 0, .data: {Storage.getOpaqueValue(), RawLoc, TU}};
1124 return C;
1125}
1126
1127CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
1128 SourceLocation Loc,
1129 CXTranslationUnit TU) {
1130 assert(D && TU && "Invalid arguments!");
1131 void *RawLoc = Loc.getPtrEncoding();
1132 OverloadedDeclRefStorage Storage(D);
1133 CXCursor C = {
1134 .kind: CXCursor_OverloadedDeclRef, .xdata: 0, .data: {Storage.getOpaqueValue(), RawLoc, TU}};
1135 return C;
1136}
1137
1138CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
1139 SourceLocation Loc,
1140 CXTranslationUnit TU) {
1141 assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
1142 void *RawLoc = Loc.getPtrEncoding();
1143 OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
1144 CXCursor C = {
1145 .kind: CXCursor_OverloadedDeclRef, .xdata: 0, .data: {Storage.getOpaqueValue(), RawLoc, TU}};
1146 return C;
1147}
1148
1149std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
1150cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
1151 assert(C.kind == CXCursor_OverloadedDeclRef);
1152 return std::make_pair(x: OverloadedDeclRefStorage::getFromOpaqueValue(
1153 VP: const_cast<void *>(C.data[0])),
1154 y: SourceLocation::getFromPtrEncoding(Encoding: C.data[1]));
1155}
1156
1157const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
1158 return static_cast<const Decl *>(Cursor.data[0]);
1159}
1160
1161const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
1162 return dyn_cast_or_null<Expr>(Val: getCursorStmt(Cursor));
1163}
1164
1165const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
1166 if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
1167 Cursor.kind == CXCursor_ObjCProtocolRef ||
1168 Cursor.kind == CXCursor_ObjCClassRef)
1169 return nullptr;
1170
1171 return static_cast<const Stmt *>(Cursor.data[1]);
1172}
1173
1174const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
1175 return static_cast<const Attr *>(Cursor.data[1]);
1176}
1177
1178ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
1179 return getCursorASTUnit(Cursor)->getASTContext();
1180}
1181
1182ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
1183 CXTranslationUnit TU = getCursorTU(Cursor);
1184 if (!TU)
1185 return nullptr;
1186 return cxtu::getASTUnit(TU);
1187}
1188
1189CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
1190 return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
1191}
1192
1193void cxcursor::getOverriddenCursors(CXCursor cursor,
1194 SmallVectorImpl<CXCursor> &overridden) {
1195 assert(clang_isDeclaration(cursor.kind));
1196 const NamedDecl *D = dyn_cast_or_null<NamedDecl>(Val: getCursorDecl(Cursor: cursor));
1197 if (!D)
1198 return;
1199
1200 CXTranslationUnit TU = getCursorTU(Cursor: cursor);
1201 SmallVector<const NamedDecl *, 8> OverDecls;
1202 D->getASTContext().getOverriddenMethods(D, OverDecls);
1203
1204 for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
1205 E = OverDecls.end();
1206 I != E; ++I) {
1207 overridden.push_back(Elt: MakeCXCursor(*I, TU));
1208 }
1209}
1210
1211std::pair<int, SourceLocation>
1212cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
1213 if (cursor.kind == CXCursor_ObjCMessageExpr) {
1214 if (cursor.xdata != -1)
1215 return std::make_pair(x&: cursor.xdata,
1216 y: cast<ObjCMessageExpr>(Val: getCursorExpr(Cursor: cursor))
1217 ->getSelectorLoc(Index: cursor.xdata));
1218 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1219 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1220 if (cursor.xdata != -1)
1221 return std::make_pair(x&: cursor.xdata,
1222 y: cast<ObjCMethodDecl>(Val: getCursorDecl(Cursor: cursor))
1223 ->getSelectorLoc(Index: cursor.xdata));
1224 }
1225
1226 return std::make_pair(x: -1, y: SourceLocation());
1227}
1228
1229CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
1230 CXCursor newCursor = cursor;
1231
1232 if (cursor.kind == CXCursor_ObjCMessageExpr) {
1233 if (SelIdx == -1 ||
1234 unsigned(SelIdx) >=
1235 cast<ObjCMessageExpr>(Val: getCursorExpr(Cursor: cursor))->getNumSelectorLocs())
1236 newCursor.xdata = -1;
1237 else
1238 newCursor.xdata = SelIdx;
1239 } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
1240 cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
1241 if (SelIdx == -1 ||
1242 unsigned(SelIdx) >=
1243 cast<ObjCMethodDecl>(Val: getCursorDecl(Cursor: cursor))->getNumSelectorLocs())
1244 newCursor.xdata = -1;
1245 else
1246 newCursor.xdata = SelIdx;
1247 }
1248
1249 return newCursor;
1250}
1251
1252CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
1253 if (cursor.kind != CXCursor_CallExpr)
1254 return cursor;
1255
1256 if (cursor.xdata == 0)
1257 return cursor;
1258
1259 const Expr *E = getCursorExpr(Cursor: cursor);
1260 TypeSourceInfo *Type = nullptr;
1261 if (const CXXUnresolvedConstructExpr *UnCtor =
1262 dyn_cast<CXXUnresolvedConstructExpr>(Val: E)) {
1263 Type = UnCtor->getTypeSourceInfo();
1264 } else if (const CXXTemporaryObjectExpr *Tmp =
1265 dyn_cast<CXXTemporaryObjectExpr>(Val: E)) {
1266 Type = Tmp->getTypeSourceInfo();
1267 }
1268
1269 if (!Type)
1270 return cursor;
1271
1272 CXTranslationUnit TU = getCursorTU(Cursor: cursor);
1273 QualType Ty = Type->getType();
1274 TypeLoc TL = Type->getTypeLoc();
1275 SourceLocation Loc = TL.getBeginLoc();
1276
1277 if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
1278 Ty = ElabT->getNamedType();
1279 ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
1280 Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
1281 }
1282
1283 if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
1284 return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
1285 if (const TagType *Tag = Ty->getAs<TagType>())
1286 return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
1287 if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
1288 return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
1289
1290 return cursor;
1291}
1292
1293bool cxcursor::operator==(CXCursor X, CXCursor Y) {
1294 return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
1295 X.data[2] == Y.data[2];
1296}
1297
1298// FIXME: Remove once we can model DeclGroups and their appropriate ranges
1299// properly in the ASTs.
1300bool cxcursor::isFirstInDeclGroup(CXCursor C) {
1301 assert(clang_isDeclaration(C.kind));
1302 return ((uintptr_t)(C.data[1])) != 0;
1303}
1304
1305//===----------------------------------------------------------------------===//
1306// libclang CXCursor APIs
1307//===----------------------------------------------------------------------===//
1308
1309int clang_Cursor_isNull(CXCursor cursor) {
1310 return clang_equalCursors(cursor, clang_getNullCursor());
1311}
1312
1313CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
1314 return getCursorTU(Cursor: cursor);
1315}
1316
1317int clang_Cursor_getNumArguments(CXCursor C) {
1318 if (clang_isDeclaration(C.kind)) {
1319 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
1320 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Val: D))
1321 return MD->param_size();
1322 if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Val: D))
1323 return FD->param_size();
1324 }
1325
1326 if (clang_isExpression(C.kind)) {
1327 const Expr *E = cxcursor::getCursorExpr(Cursor: C);
1328 if (const CallExpr *CE = dyn_cast<CallExpr>(Val: E)) {
1329 return CE->getNumArgs();
1330 }
1331 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Val: E)) {
1332 return CE->getNumArgs();
1333 }
1334 }
1335
1336 return -1;
1337}
1338
1339CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
1340 if (clang_isDeclaration(C.kind)) {
1341 const Decl *D = cxcursor::getCursorDecl(Cursor: C);
1342 if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(Val: D)) {
1343 if (i < MD->param_size())
1344 return cxcursor::MakeCXCursor(MD->parameters()[i],
1345 cxcursor::getCursorTU(Cursor: C));
1346 } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(Val: D)) {
1347 if (i < FD->param_size())
1348 return cxcursor::MakeCXCursor(FD->parameters()[i],
1349 cxcursor::getCursorTU(Cursor: C));
1350 }
1351 }
1352
1353 if (clang_isExpression(C.kind)) {
1354 const Expr *E = cxcursor::getCursorExpr(Cursor: C);
1355 if (const CallExpr *CE = dyn_cast<CallExpr>(Val: E)) {
1356 if (i < CE->getNumArgs()) {
1357 return cxcursor::MakeCXCursor(CE->getArg(Arg: i), getCursorDecl(Cursor: C),
1358 cxcursor::getCursorTU(Cursor: C));
1359 }
1360 }
1361 if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(Val: E)) {
1362 if (i < CE->getNumArgs()) {
1363 return cxcursor::MakeCXCursor(CE->getArg(Arg: i), getCursorDecl(Cursor: C),
1364 cxcursor::getCursorTU(Cursor: C));
1365 }
1366 }
1367 }
1368
1369 return clang_getNullCursor();
1370}
1371
1372int clang_Cursor_getNumTemplateArguments(CXCursor C) {
1373 CXCursorKind kind = clang_getCursorKind(C);
1374 if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
1375 kind != CXCursor_ClassDecl &&
1376 kind != CXCursor_ClassTemplatePartialSpecialization) {
1377 return -1;
1378 }
1379
1380 if (const auto *FD =
1381 llvm::dyn_cast_if_present<clang::FunctionDecl>(Val: getCursorDecl(Cursor: C))) {
1382 const FunctionTemplateSpecializationInfo *SpecInfo =
1383 FD->getTemplateSpecializationInfo();
1384 if (!SpecInfo) {
1385 return -1;
1386 }
1387 return SpecInfo->TemplateArguments->size();
1388 }
1389
1390 if (const auto *SD =
1391 llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
1392 Val: getCursorDecl(Cursor: C))) {
1393 return SD->getTemplateArgs().size();
1394 }
1395
1396 return -1;
1397}
1398
1399enum CXGetTemplateArgumentStatus {
1400 /** The operation completed successfully */
1401 CXGetTemplateArgumentStatus_Success = 0,
1402
1403 /** The specified cursor did not represent a FunctionDecl or
1404 ClassTemplateSpecializationDecl. */
1405 CXGetTemplateArgumentStatus_CursorNotCompatibleDecl = -1,
1406
1407 /** The specified cursor was not castable to a FunctionDecl or
1408 ClassTemplateSpecializationDecl. */
1409 CXGetTemplateArgumentStatus_BadDeclCast = -2,
1410
1411 /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
1412 CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
1413
1414 /** An invalid (OOB) argument index was specified */
1415 CXGetTemplateArgumentStatus_InvalidIndex = -4
1416};
1417
1418static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
1419 TemplateArgument *TA) {
1420 CXCursorKind kind = clang_getCursorKind(C);
1421 if (kind != CXCursor_FunctionDecl && kind != CXCursor_StructDecl &&
1422 kind != CXCursor_ClassDecl &&
1423 kind != CXCursor_ClassTemplatePartialSpecialization) {
1424 return -1;
1425 }
1426
1427 if (const auto *FD =
1428 llvm::dyn_cast_if_present<clang::FunctionDecl>(Val: getCursorDecl(Cursor: C))) {
1429
1430 const FunctionTemplateSpecializationInfo *SpecInfo =
1431 FD->getTemplateSpecializationInfo();
1432 if (!SpecInfo) {
1433 return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
1434 }
1435
1436 if (I >= SpecInfo->TemplateArguments->size()) {
1437 return CXGetTemplateArgumentStatus_InvalidIndex;
1438 }
1439
1440 *TA = SpecInfo->TemplateArguments->get(Idx: I);
1441 return 0;
1442 }
1443
1444 if (const auto *SD =
1445 llvm::dyn_cast_if_present<clang::ClassTemplateSpecializationDecl>(
1446 Val: getCursorDecl(Cursor: C))) {
1447 if (I >= SD->getTemplateArgs().size()) {
1448 return CXGetTemplateArgumentStatus_InvalidIndex;
1449 }
1450
1451 *TA = SD->getTemplateArgs()[I];
1452 return 0;
1453 }
1454
1455 return CXGetTemplateArgumentStatus_BadDeclCast;
1456}
1457
1458enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
1459 unsigned I) {
1460 TemplateArgument TA;
1461 if (clang_Cursor_getTemplateArgument(C, I, TA: &TA)) {
1462 return CXTemplateArgumentKind_Invalid;
1463 }
1464
1465 switch (TA.getKind()) {
1466 case TemplateArgument::Null:
1467 return CXTemplateArgumentKind_Null;
1468 case TemplateArgument::Type:
1469 return CXTemplateArgumentKind_Type;
1470 case TemplateArgument::Declaration:
1471 return CXTemplateArgumentKind_Declaration;
1472 case TemplateArgument::NullPtr:
1473 return CXTemplateArgumentKind_NullPtr;
1474 case TemplateArgument::Integral:
1475 return CXTemplateArgumentKind_Integral;
1476 case TemplateArgument::StructuralValue:
1477 // FIXME: Expose these values.
1478 return CXTemplateArgumentKind_Invalid;
1479 case TemplateArgument::Template:
1480 return CXTemplateArgumentKind_Template;
1481 case TemplateArgument::TemplateExpansion:
1482 return CXTemplateArgumentKind_TemplateExpansion;
1483 case TemplateArgument::Expression:
1484 return CXTemplateArgumentKind_Expression;
1485 case TemplateArgument::Pack:
1486 return CXTemplateArgumentKind_Pack;
1487 }
1488
1489 return CXTemplateArgumentKind_Invalid;
1490}
1491
1492CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
1493 TemplateArgument TA;
1494 if (clang_Cursor_getTemplateArgument(C, I, TA: &TA) !=
1495 CXGetTemplateArgumentStatus_Success) {
1496 return cxtype::MakeCXType(T: QualType(), TU: getCursorTU(Cursor: C));
1497 }
1498
1499 if (TA.getKind() != TemplateArgument::Type) {
1500 return cxtype::MakeCXType(T: QualType(), TU: getCursorTU(Cursor: C));
1501 }
1502
1503 return cxtype::MakeCXType(T: TA.getAsType(), TU: getCursorTU(Cursor: C));
1504}
1505
1506long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
1507 TemplateArgument TA;
1508 if (clang_Cursor_getTemplateArgument(C, I, TA: &TA) !=
1509 CXGetTemplateArgumentStatus_Success) {
1510 assert(0 && "Unable to retrieve TemplateArgument");
1511 return 0;
1512 }
1513
1514 if (TA.getKind() != TemplateArgument::Integral) {
1515 assert(0 && "Passed template argument is not Integral");
1516 return 0;
1517 }
1518
1519 return TA.getAsIntegral().getSExtValue();
1520}
1521
1522unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
1523 unsigned I) {
1524 TemplateArgument TA;
1525 if (clang_Cursor_getTemplateArgument(C, I, TA: &TA) !=
1526 CXGetTemplateArgumentStatus_Success) {
1527 assert(0 && "Unable to retrieve TemplateArgument");
1528 return 0;
1529 }
1530
1531 if (TA.getKind() != TemplateArgument::Integral) {
1532 assert(0 && "Passed template argument is not Integral");
1533 return 0;
1534 }
1535
1536 return TA.getAsIntegral().getZExtValue();
1537}
1538
1539//===----------------------------------------------------------------------===//
1540// CXCursorSet.
1541//===----------------------------------------------------------------------===//
1542
1543typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
1544
1545static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
1546 return (CXCursorSet)setImpl;
1547}
1548static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
1549 return (CXCursorSet_Impl *)set;
1550}
1551namespace llvm {
1552template <> struct DenseMapInfo<CXCursor> {
1553public:
1554 static inline CXCursor getEmptyKey() {
1555 return MakeCXCursorInvalid(K: CXCursor_InvalidFile);
1556 }
1557 static inline CXCursor getTombstoneKey() {
1558 return MakeCXCursorInvalid(K: CXCursor_NoDeclFound);
1559 }
1560 static inline unsigned getHashValue(const CXCursor &cursor) {
1561 return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
1562 getHashValue(PairVal: std::make_pair(x: cursor.data[0], y: cursor.data[1]));
1563 }
1564 static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
1565 return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
1566 }
1567};
1568} // namespace llvm
1569
1570CXCursorSet clang_createCXCursorSet() {
1571 return packCXCursorSet(setImpl: new CXCursorSet_Impl());
1572}
1573
1574void clang_disposeCXCursorSet(CXCursorSet set) {
1575 delete unpackCXCursorSet(set);
1576}
1577
1578unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
1579 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1580 if (!setImpl)
1581 return 0;
1582 return setImpl->find(Val: cursor) != setImpl->end();
1583}
1584
1585unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
1586 // Do not insert invalid cursors into the set.
1587 if (cursor.kind >= CXCursor_FirstInvalid &&
1588 cursor.kind <= CXCursor_LastInvalid)
1589 return 1;
1590
1591 CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
1592 if (!setImpl)
1593 return 1;
1594 unsigned &entry = (*setImpl)[cursor];
1595 unsigned flag = entry == 0 ? 1 : 0;
1596 entry = 1;
1597 return flag;
1598}
1599
1600CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
1601 enum CXCursorKind kind = clang_getCursorKind(cursor);
1602 if (clang_isDeclaration(kind)) {
1603 const Decl *decl = getCursorDecl(Cursor: cursor);
1604 if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(Val: decl)) {
1605 ASTUnit *unit = getCursorASTUnit(Cursor: cursor);
1606 CodeCompletionResult Result(namedDecl, CCP_Declaration);
1607 CodeCompletionString *String = Result.CreateCodeCompletionString(
1608 Ctx&: unit->getASTContext(), PP&: unit->getPreprocessor(),
1609 CCContext: CodeCompletionContext::CCC_Other,
1610 Allocator&: unit->getCodeCompletionTUInfo().getAllocator(),
1611 CCTUInfo&: unit->getCodeCompletionTUInfo(), IncludeBriefComments: true);
1612 return String;
1613 }
1614 } else if (kind == CXCursor_MacroDefinition) {
1615 const MacroDefinitionRecord *definition = getCursorMacroDefinition(C: cursor);
1616 const IdentifierInfo *Macro = definition->getName();
1617 ASTUnit *unit = getCursorASTUnit(Cursor: cursor);
1618 CodeCompletionResult Result(
1619 Macro,
1620 unit->getPreprocessor().getMacroDefinition(II: Macro).getMacroInfo());
1621 CodeCompletionString *String = Result.CreateCodeCompletionString(
1622 Ctx&: unit->getASTContext(), PP&: unit->getPreprocessor(),
1623 CCContext: CodeCompletionContext::CCC_Other,
1624 Allocator&: unit->getCodeCompletionTUInfo().getAllocator(),
1625 CCTUInfo&: unit->getCodeCompletionTUInfo(), IncludeBriefComments: false);
1626 return String;
1627 }
1628 return nullptr;
1629}
1630
1631namespace {
1632struct OverridenCursorsPool {
1633 typedef SmallVector<CXCursor, 2> CursorVec;
1634 std::vector<CursorVec *> AllCursors;
1635 std::vector<CursorVec *> AvailableCursors;
1636
1637 ~OverridenCursorsPool() {
1638 for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
1639 E = AllCursors.end();
1640 I != E; ++I) {
1641 delete *I;
1642 }
1643 }
1644};
1645} // namespace
1646
1647void *cxcursor::createOverridenCXCursorsPool() {
1648 return new OverridenCursorsPool();
1649}
1650
1651void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
1652 delete static_cast<OverridenCursorsPool *>(pool);
1653}
1654
1655void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
1656 unsigned *num_overridden) {
1657 if (overridden)
1658 *overridden = nullptr;
1659 if (num_overridden)
1660 *num_overridden = 0;
1661
1662 CXTranslationUnit TU = cxcursor::getCursorTU(Cursor: cursor);
1663
1664 if (!overridden || !num_overridden || !TU)
1665 return;
1666
1667 if (!clang_isDeclaration(cursor.kind))
1668 return;
1669
1670 OverridenCursorsPool &pool =
1671 *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1672
1673 OverridenCursorsPool::CursorVec *Vec = nullptr;
1674
1675 if (!pool.AvailableCursors.empty()) {
1676 Vec = pool.AvailableCursors.back();
1677 pool.AvailableCursors.pop_back();
1678 } else {
1679 Vec = new OverridenCursorsPool::CursorVec();
1680 pool.AllCursors.push_back(x: Vec);
1681 }
1682
1683 // Clear out the vector, but don't free the memory contents. This
1684 // reduces malloc() traffic.
1685 Vec->clear();
1686
1687 // Use the first entry to contain a back reference to the vector.
1688 // This is a complete hack.
1689 CXCursor backRefCursor = MakeCXCursorInvalid(K: CXCursor_InvalidFile, TU);
1690 backRefCursor.data[0] = Vec;
1691 assert(cxcursor::getCursorTU(backRefCursor) == TU);
1692 Vec->push_back(Elt: backRefCursor);
1693
1694 // Get the overridden cursors.
1695 cxcursor::getOverriddenCursors(cursor, overridden&: *Vec);
1696
1697 // Did we get any overridden cursors? If not, return Vec to the pool
1698 // of available cursor vectors.
1699 if (Vec->size() == 1) {
1700 pool.AvailableCursors.push_back(x: Vec);
1701 return;
1702 }
1703
1704 // Now tell the caller about the overridden cursors.
1705 assert(Vec->size() > 1);
1706 *overridden = &((*Vec)[1]);
1707 *num_overridden = Vec->size() - 1;
1708}
1709
1710void clang_disposeOverriddenCursors(CXCursor *overridden) {
1711 if (!overridden)
1712 return;
1713
1714 // Use pointer arithmetic to get back the first faux entry
1715 // which has a back-reference to the TU and the vector.
1716 --overridden;
1717 OverridenCursorsPool::CursorVec *Vec =
1718 static_cast<OverridenCursorsPool::CursorVec *>(
1719 const_cast<void *>(overridden->data[0]));
1720 CXTranslationUnit TU = getCursorTU(Cursor: *overridden);
1721
1722 assert(Vec && TU);
1723
1724 OverridenCursorsPool &pool =
1725 *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
1726
1727 pool.AvailableCursors.push_back(x: Vec);
1728}
1729
1730int clang_Cursor_isDynamicCall(CXCursor C) {
1731 const Expr *E = nullptr;
1732 if (clang_isExpression(C.kind))
1733 E = getCursorExpr(Cursor: C);
1734 if (!E)
1735 return 0;
1736
1737 if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(Val: E)) {
1738 if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
1739 return false;
1740 if (auto *RecE = dyn_cast<ObjCMessageExpr>(
1741 Val: MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
1742 if (RecE->getMethodFamily() == OMF_alloc)
1743 return false;
1744 }
1745 return true;
1746 }
1747
1748 if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(Val: E)) {
1749 return !PropRefE->isSuperReceiver();
1750 }
1751
1752 const MemberExpr *ME = nullptr;
1753 if (isa<MemberExpr>(Val: E))
1754 ME = cast<MemberExpr>(Val: E);
1755 else if (const CallExpr *CE = dyn_cast<CallExpr>(Val: E))
1756 ME = dyn_cast_or_null<MemberExpr>(Val: CE->getCallee());
1757
1758 if (ME) {
1759 if (const CXXMethodDecl *MD =
1760 dyn_cast_or_null<CXXMethodDecl>(Val: ME->getMemberDecl()))
1761 return MD->isVirtual() &&
1762 ME->performsVirtualDispatch(
1763 LO: cxcursor::getCursorContext(Cursor: C).getLangOpts());
1764 }
1765
1766 return 0;
1767}
1768
1769CXType clang_Cursor_getReceiverType(CXCursor C) {
1770 CXTranslationUnit TU = cxcursor::getCursorTU(Cursor: C);
1771 const Expr *E = nullptr;
1772 if (clang_isExpression(C.kind))
1773 E = getCursorExpr(Cursor: C);
1774
1775 if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(Val: E))
1776 return cxtype::MakeCXType(T: MsgE->getReceiverType(), TU);
1777
1778 if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(Val: E)) {
1779 return cxtype::MakeCXType(
1780 T: PropRefE->getReceiverType(ctx: cxcursor::getCursorContext(Cursor: C)), TU);
1781 }
1782
1783 const MemberExpr *ME = nullptr;
1784 if (isa<MemberExpr>(Val: E))
1785 ME = cast<MemberExpr>(Val: E);
1786 else if (const CallExpr *CE = dyn_cast<CallExpr>(Val: E))
1787 ME = dyn_cast_or_null<MemberExpr>(Val: CE->getCallee());
1788
1789 if (ME) {
1790 if (isa_and_nonnull<CXXMethodDecl>(Val: ME->getMemberDecl())) {
1791 auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
1792 return cxtype::MakeCXType(T: receiverTy, TU);
1793 }
1794 }
1795
1796 return cxtype::MakeCXType(T: QualType(), TU);
1797}
1798

source code of clang/tools/libclang/CXCursor.cpp