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