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