CXCursor.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  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. #include "CXCursor.h"
  15. #include "CXString.h"
  16. #include "CXTranslationUnit.h"
  17. #include "CXType.h"
  18. #include "clang-c/Index.h"
  19. #include "clang/AST/Attr.h"
  20. #include "clang/AST/Decl.h"
  21. #include "clang/AST/DeclCXX.h"
  22. #include "clang/AST/DeclObjC.h"
  23. #include "clang/AST/DeclTemplate.h"
  24. #include "clang/AST/Expr.h"
  25. #include "clang/AST/ExprCXX.h"
  26. #include "clang/AST/ExprObjC.h"
  27. #include "clang/Frontend/ASTUnit.h"
  28. #include "llvm/Support/ErrorHandling.h"
  29. using namespace clang;
  30. using namespace cxcursor;
  31. CXCursor cxcursor::MakeCXCursorInvalid(CXCursorKind K, CXTranslationUnit TU) {
  32. assert(K >= CXCursor_FirstInvalid && K <= CXCursor_LastInvalid);
  33. CXCursor C = {K, 0, {nullptr, nullptr, TU}};
  34. return C;
  35. }
  36. static CXCursorKind GetCursorKind(const Attr *A) {
  37. assert(A && "Invalid arguments!");
  38. switch (A->getKind()) {
  39. default:
  40. break;
  41. case attr::IBAction:
  42. return CXCursor_IBActionAttr;
  43. case attr::IBOutlet:
  44. return CXCursor_IBOutletAttr;
  45. case attr::IBOutletCollection:
  46. return CXCursor_IBOutletCollectionAttr;
  47. case attr::Final:
  48. return CXCursor_CXXFinalAttr;
  49. case attr::Override:
  50. return CXCursor_CXXOverrideAttr;
  51. case attr::Annotate:
  52. return CXCursor_AnnotateAttr;
  53. case attr::AsmLabel:
  54. return CXCursor_AsmLabelAttr;
  55. case attr::Packed:
  56. return CXCursor_PackedAttr;
  57. case attr::Pure:
  58. return CXCursor_PureAttr;
  59. case attr::Const:
  60. return CXCursor_ConstAttr;
  61. case attr::NoDuplicate:
  62. return CXCursor_NoDuplicateAttr;
  63. case attr::CUDAConstant:
  64. return CXCursor_CUDAConstantAttr;
  65. case attr::CUDADevice:
  66. return CXCursor_CUDADeviceAttr;
  67. case attr::CUDAGlobal:
  68. return CXCursor_CUDAGlobalAttr;
  69. case attr::CUDAHost:
  70. return CXCursor_CUDAHostAttr;
  71. case attr::CUDAShared:
  72. return CXCursor_CUDASharedAttr;
  73. case attr::Visibility:
  74. return CXCursor_VisibilityAttr;
  75. case attr::DLLExport:
  76. return CXCursor_DLLExport;
  77. case attr::DLLImport:
  78. return CXCursor_DLLImport;
  79. case attr::NSReturnsRetained:
  80. return CXCursor_NSReturnsRetained;
  81. case attr::NSReturnsNotRetained:
  82. return CXCursor_NSReturnsNotRetained;
  83. case attr::NSReturnsAutoreleased:
  84. return CXCursor_NSReturnsAutoreleased;
  85. case attr::NSConsumesSelf:
  86. return CXCursor_NSConsumesSelf;
  87. case attr::NSConsumed:
  88. return CXCursor_NSConsumed;
  89. case attr::ObjCException:
  90. return CXCursor_ObjCException;
  91. case attr::ObjCNSObject:
  92. return CXCursor_ObjCNSObject;
  93. case attr::ObjCIndependentClass:
  94. return CXCursor_ObjCIndependentClass;
  95. case attr::ObjCPreciseLifetime:
  96. return CXCursor_ObjCPreciseLifetime;
  97. case attr::ObjCReturnsInnerPointer:
  98. return CXCursor_ObjCReturnsInnerPointer;
  99. case attr::ObjCRequiresSuper:
  100. return CXCursor_ObjCRequiresSuper;
  101. case attr::ObjCRootClass:
  102. return CXCursor_ObjCRootClass;
  103. case attr::ObjCSubclassingRestricted:
  104. return CXCursor_ObjCSubclassingRestricted;
  105. case attr::ObjCExplicitProtocolImpl:
  106. return CXCursor_ObjCExplicitProtocolImpl;
  107. case attr::ObjCDesignatedInitializer:
  108. return CXCursor_ObjCDesignatedInitializer;
  109. case attr::ObjCRuntimeVisible:
  110. return CXCursor_ObjCRuntimeVisible;
  111. case attr::ObjCBoxable:
  112. return CXCursor_ObjCBoxable;
  113. case attr::FlagEnum:
  114. return CXCursor_FlagEnum;
  115. case attr::Convergent:
  116. return CXCursor_ConvergentAttr;
  117. case attr::WarnUnused:
  118. return CXCursor_WarnUnusedAttr;
  119. case attr::WarnUnusedResult:
  120. return CXCursor_WarnUnusedResultAttr;
  121. case attr::Aligned:
  122. return CXCursor_AlignedAttr;
  123. }
  124. return CXCursor_UnexposedAttr;
  125. }
  126. CXCursor cxcursor::MakeCXCursor(const Attr *A, const Decl *Parent,
  127. CXTranslationUnit TU) {
  128. assert(A && Parent && TU && "Invalid arguments!");
  129. CXCursor C = {GetCursorKind(A), 0, {Parent, A, TU}};
  130. return C;
  131. }
  132. CXCursor cxcursor::MakeCXCursor(const Decl *D, CXTranslationUnit TU,
  133. SourceRange RegionOfInterest,
  134. bool FirstInDeclGroup) {
  135. assert(D && TU && "Invalid arguments!");
  136. CXCursorKind K = getCursorKindForDecl(D);
  137. if (K == CXCursor_ObjCClassMethodDecl ||
  138. K == CXCursor_ObjCInstanceMethodDecl) {
  139. int SelectorIdIndex = -1;
  140. // Check if cursor points to a selector id.
  141. if (RegionOfInterest.isValid() &&
  142. RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
  143. SmallVector<SourceLocation, 16> SelLocs;
  144. cast<ObjCMethodDecl>(D)->getSelectorLocs(SelLocs);
  145. SmallVectorImpl<SourceLocation>::iterator I =
  146. llvm::find(SelLocs, RegionOfInterest.getBegin());
  147. if (I != SelLocs.end())
  148. SelectorIdIndex = I - SelLocs.begin();
  149. }
  150. CXCursor C = {K,
  151. SelectorIdIndex,
  152. {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
  153. return C;
  154. }
  155. CXCursor C = {K, 0, {D, (void *)(intptr_t)(FirstInDeclGroup ? 1 : 0), TU}};
  156. return C;
  157. }
  158. CXCursor cxcursor::MakeCXCursor(const Stmt *S, const Decl *Parent,
  159. CXTranslationUnit TU,
  160. SourceRange RegionOfInterest) {
  161. assert(S && TU && "Invalid arguments!");
  162. CXCursorKind K = CXCursor_NotImplemented;
  163. switch (S->getStmtClass()) {
  164. case Stmt::NoStmtClass:
  165. break;
  166. case Stmt::CaseStmtClass:
  167. K = CXCursor_CaseStmt;
  168. break;
  169. case Stmt::DefaultStmtClass:
  170. K = CXCursor_DefaultStmt;
  171. break;
  172. case Stmt::IfStmtClass:
  173. K = CXCursor_IfStmt;
  174. break;
  175. case Stmt::SwitchStmtClass:
  176. K = CXCursor_SwitchStmt;
  177. break;
  178. case Stmt::WhileStmtClass:
  179. K = CXCursor_WhileStmt;
  180. break;
  181. case Stmt::DoStmtClass:
  182. K = CXCursor_DoStmt;
  183. break;
  184. case Stmt::ForStmtClass:
  185. K = CXCursor_ForStmt;
  186. break;
  187. case Stmt::GotoStmtClass:
  188. K = CXCursor_GotoStmt;
  189. break;
  190. case Stmt::IndirectGotoStmtClass:
  191. K = CXCursor_IndirectGotoStmt;
  192. break;
  193. case Stmt::ContinueStmtClass:
  194. K = CXCursor_ContinueStmt;
  195. break;
  196. case Stmt::BreakStmtClass:
  197. K = CXCursor_BreakStmt;
  198. break;
  199. case Stmt::ReturnStmtClass:
  200. K = CXCursor_ReturnStmt;
  201. break;
  202. case Stmt::GCCAsmStmtClass:
  203. K = CXCursor_GCCAsmStmt;
  204. break;
  205. case Stmt::MSAsmStmtClass:
  206. K = CXCursor_MSAsmStmt;
  207. break;
  208. case Stmt::ObjCAtTryStmtClass:
  209. K = CXCursor_ObjCAtTryStmt;
  210. break;
  211. case Stmt::ObjCAtCatchStmtClass:
  212. K = CXCursor_ObjCAtCatchStmt;
  213. break;
  214. case Stmt::ObjCAtFinallyStmtClass:
  215. K = CXCursor_ObjCAtFinallyStmt;
  216. break;
  217. case Stmt::ObjCAtThrowStmtClass:
  218. K = CXCursor_ObjCAtThrowStmt;
  219. break;
  220. case Stmt::ObjCAtSynchronizedStmtClass:
  221. K = CXCursor_ObjCAtSynchronizedStmt;
  222. break;
  223. case Stmt::ObjCAutoreleasePoolStmtClass:
  224. K = CXCursor_ObjCAutoreleasePoolStmt;
  225. break;
  226. case Stmt::ObjCForCollectionStmtClass:
  227. K = CXCursor_ObjCForCollectionStmt;
  228. break;
  229. case Stmt::CXXCatchStmtClass:
  230. K = CXCursor_CXXCatchStmt;
  231. break;
  232. case Stmt::CXXTryStmtClass:
  233. K = CXCursor_CXXTryStmt;
  234. break;
  235. case Stmt::CXXForRangeStmtClass:
  236. K = CXCursor_CXXForRangeStmt;
  237. break;
  238. case Stmt::SEHTryStmtClass:
  239. K = CXCursor_SEHTryStmt;
  240. break;
  241. case Stmt::SEHExceptStmtClass:
  242. K = CXCursor_SEHExceptStmt;
  243. break;
  244. case Stmt::SEHFinallyStmtClass:
  245. K = CXCursor_SEHFinallyStmt;
  246. break;
  247. case Stmt::SEHLeaveStmtClass:
  248. K = CXCursor_SEHLeaveStmt;
  249. break;
  250. case Stmt::CoroutineBodyStmtClass:
  251. case Stmt::CoreturnStmtClass:
  252. K = CXCursor_UnexposedStmt;
  253. break;
  254. case Stmt::ArrayTypeTraitExprClass:
  255. case Stmt::AsTypeExprClass:
  256. case Stmt::AtomicExprClass:
  257. case Stmt::BinaryConditionalOperatorClass:
  258. case Stmt::TypeTraitExprClass:
  259. case Stmt::CoawaitExprClass:
  260. case Stmt::ConceptSpecializationExprClass:
  261. case Stmt::RequiresExprClass:
  262. case Stmt::DependentCoawaitExprClass:
  263. case Stmt::CoyieldExprClass:
  264. case Stmt::CXXBindTemporaryExprClass:
  265. case Stmt::CXXDefaultArgExprClass:
  266. case Stmt::CXXDefaultInitExprClass:
  267. case Stmt::CXXFoldExprClass:
  268. case Stmt::CXXRewrittenBinaryOperatorClass:
  269. case Stmt::CXXStdInitializerListExprClass:
  270. case Stmt::CXXScalarValueInitExprClass:
  271. case Stmt::CXXUuidofExprClass:
  272. case Stmt::ChooseExprClass:
  273. case Stmt::DesignatedInitExprClass:
  274. case Stmt::DesignatedInitUpdateExprClass:
  275. case Stmt::ArrayInitLoopExprClass:
  276. case Stmt::ArrayInitIndexExprClass:
  277. case Stmt::ExprWithCleanupsClass:
  278. case Stmt::ExpressionTraitExprClass:
  279. case Stmt::ExtVectorElementExprClass:
  280. case Stmt::ImplicitCastExprClass:
  281. case Stmt::ImplicitValueInitExprClass:
  282. case Stmt::NoInitExprClass:
  283. case Stmt::MaterializeTemporaryExprClass:
  284. case Stmt::ObjCIndirectCopyRestoreExprClass:
  285. case Stmt::OffsetOfExprClass:
  286. case Stmt::ParenListExprClass:
  287. case Stmt::PredefinedExprClass:
  288. case Stmt::ShuffleVectorExprClass:
  289. case Stmt::SourceLocExprClass:
  290. case Stmt::ConvertVectorExprClass:
  291. case Stmt::VAArgExprClass:
  292. case Stmt::ObjCArrayLiteralClass:
  293. case Stmt::ObjCDictionaryLiteralClass:
  294. case Stmt::ObjCBoxedExprClass:
  295. case Stmt::ObjCSubscriptRefExprClass:
  296. case Stmt::RecoveryExprClass:
  297. case Stmt::SYCLUniqueStableNameExprClass:
  298. K = CXCursor_UnexposedExpr;
  299. break;
  300. case Stmt::OpaqueValueExprClass:
  301. if (Expr *Src = cast<OpaqueValueExpr>(S)->getSourceExpr())
  302. return MakeCXCursor(Src, Parent, TU, RegionOfInterest);
  303. K = CXCursor_UnexposedExpr;
  304. break;
  305. case Stmt::PseudoObjectExprClass:
  306. return MakeCXCursor(cast<PseudoObjectExpr>(S)->getSyntacticForm(), Parent,
  307. TU, RegionOfInterest);
  308. case Stmt::CompoundStmtClass:
  309. K = CXCursor_CompoundStmt;
  310. break;
  311. case Stmt::NullStmtClass:
  312. K = CXCursor_NullStmt;
  313. break;
  314. case Stmt::LabelStmtClass:
  315. K = CXCursor_LabelStmt;
  316. break;
  317. case Stmt::AttributedStmtClass:
  318. K = CXCursor_UnexposedStmt;
  319. break;
  320. case Stmt::DeclStmtClass:
  321. K = CXCursor_DeclStmt;
  322. break;
  323. case Stmt::CapturedStmtClass:
  324. K = CXCursor_UnexposedStmt;
  325. break;
  326. case Stmt::IntegerLiteralClass:
  327. K = CXCursor_IntegerLiteral;
  328. break;
  329. case Stmt::FixedPointLiteralClass:
  330. K = CXCursor_FixedPointLiteral;
  331. break;
  332. case Stmt::FloatingLiteralClass:
  333. K = CXCursor_FloatingLiteral;
  334. break;
  335. case Stmt::ImaginaryLiteralClass:
  336. K = CXCursor_ImaginaryLiteral;
  337. break;
  338. case Stmt::StringLiteralClass:
  339. K = CXCursor_StringLiteral;
  340. break;
  341. case Stmt::CharacterLiteralClass:
  342. K = CXCursor_CharacterLiteral;
  343. break;
  344. case Stmt::ConstantExprClass:
  345. return MakeCXCursor(cast<ConstantExpr>(S)->getSubExpr(), Parent, TU,
  346. RegionOfInterest);
  347. case Stmt::ParenExprClass:
  348. K = CXCursor_ParenExpr;
  349. break;
  350. case Stmt::UnaryOperatorClass:
  351. K = CXCursor_UnaryOperator;
  352. break;
  353. case Stmt::UnaryExprOrTypeTraitExprClass:
  354. case Stmt::CXXNoexceptExprClass:
  355. K = CXCursor_UnaryExpr;
  356. break;
  357. case Stmt::MSPropertySubscriptExprClass:
  358. case Stmt::ArraySubscriptExprClass:
  359. K = CXCursor_ArraySubscriptExpr;
  360. break;
  361. case Stmt::MatrixSubscriptExprClass:
  362. // TODO: add support for MatrixSubscriptExpr.
  363. K = CXCursor_UnexposedExpr;
  364. break;
  365. case Stmt::OMPArraySectionExprClass:
  366. K = CXCursor_OMPArraySectionExpr;
  367. break;
  368. case Stmt::OMPArrayShapingExprClass:
  369. K = CXCursor_OMPArrayShapingExpr;
  370. break;
  371. case Stmt::OMPIteratorExprClass:
  372. K = CXCursor_OMPIteratorExpr;
  373. break;
  374. case Stmt::BinaryOperatorClass:
  375. K = CXCursor_BinaryOperator;
  376. break;
  377. case Stmt::CompoundAssignOperatorClass:
  378. K = CXCursor_CompoundAssignOperator;
  379. break;
  380. case Stmt::ConditionalOperatorClass:
  381. K = CXCursor_ConditionalOperator;
  382. break;
  383. case Stmt::CStyleCastExprClass:
  384. K = CXCursor_CStyleCastExpr;
  385. break;
  386. case Stmt::CompoundLiteralExprClass:
  387. K = CXCursor_CompoundLiteralExpr;
  388. break;
  389. case Stmt::InitListExprClass:
  390. K = CXCursor_InitListExpr;
  391. break;
  392. case Stmt::AddrLabelExprClass:
  393. K = CXCursor_AddrLabelExpr;
  394. break;
  395. case Stmt::StmtExprClass:
  396. K = CXCursor_StmtExpr;
  397. break;
  398. case Stmt::GenericSelectionExprClass:
  399. K = CXCursor_GenericSelectionExpr;
  400. break;
  401. case Stmt::GNUNullExprClass:
  402. K = CXCursor_GNUNullExpr;
  403. break;
  404. case Stmt::CXXStaticCastExprClass:
  405. K = CXCursor_CXXStaticCastExpr;
  406. break;
  407. case Stmt::CXXDynamicCastExprClass:
  408. K = CXCursor_CXXDynamicCastExpr;
  409. break;
  410. case Stmt::CXXReinterpretCastExprClass:
  411. K = CXCursor_CXXReinterpretCastExpr;
  412. break;
  413. case Stmt::CXXConstCastExprClass:
  414. K = CXCursor_CXXConstCastExpr;
  415. break;
  416. case Stmt::CXXFunctionalCastExprClass:
  417. K = CXCursor_CXXFunctionalCastExpr;
  418. break;
  419. case Stmt::CXXAddrspaceCastExprClass:
  420. K = CXCursor_CXXAddrspaceCastExpr;
  421. break;
  422. case Stmt::CXXTypeidExprClass:
  423. K = CXCursor_CXXTypeidExpr;
  424. break;
  425. case Stmt::CXXBoolLiteralExprClass:
  426. K = CXCursor_CXXBoolLiteralExpr;
  427. break;
  428. case Stmt::CXXNullPtrLiteralExprClass:
  429. K = CXCursor_CXXNullPtrLiteralExpr;
  430. break;
  431. case Stmt::CXXThisExprClass:
  432. K = CXCursor_CXXThisExpr;
  433. break;
  434. case Stmt::CXXThrowExprClass:
  435. K = CXCursor_CXXThrowExpr;
  436. break;
  437. case Stmt::CXXNewExprClass:
  438. K = CXCursor_CXXNewExpr;
  439. break;
  440. case Stmt::CXXDeleteExprClass:
  441. K = CXCursor_CXXDeleteExpr;
  442. break;
  443. case Stmt::ObjCStringLiteralClass:
  444. K = CXCursor_ObjCStringLiteral;
  445. break;
  446. case Stmt::ObjCEncodeExprClass:
  447. K = CXCursor_ObjCEncodeExpr;
  448. break;
  449. case Stmt::ObjCSelectorExprClass:
  450. K = CXCursor_ObjCSelectorExpr;
  451. break;
  452. case Stmt::ObjCProtocolExprClass:
  453. K = CXCursor_ObjCProtocolExpr;
  454. break;
  455. case Stmt::ObjCBoolLiteralExprClass:
  456. K = CXCursor_ObjCBoolLiteralExpr;
  457. break;
  458. case Stmt::ObjCAvailabilityCheckExprClass:
  459. K = CXCursor_ObjCAvailabilityCheckExpr;
  460. break;
  461. case Stmt::ObjCBridgedCastExprClass:
  462. K = CXCursor_ObjCBridgedCastExpr;
  463. break;
  464. case Stmt::BlockExprClass:
  465. K = CXCursor_BlockExpr;
  466. break;
  467. case Stmt::PackExpansionExprClass:
  468. K = CXCursor_PackExpansionExpr;
  469. break;
  470. case Stmt::SizeOfPackExprClass:
  471. K = CXCursor_SizeOfPackExpr;
  472. break;
  473. case Stmt::DeclRefExprClass:
  474. if (const ImplicitParamDecl *IPD = dyn_cast_or_null<ImplicitParamDecl>(
  475. cast<DeclRefExpr>(S)->getDecl())) {
  476. if (const ObjCMethodDecl *MD =
  477. dyn_cast<ObjCMethodDecl>(IPD->getDeclContext())) {
  478. if (MD->getSelfDecl() == IPD) {
  479. K = CXCursor_ObjCSelfExpr;
  480. break;
  481. }
  482. }
  483. }
  484. K = CXCursor_DeclRefExpr;
  485. break;
  486. case Stmt::DependentScopeDeclRefExprClass:
  487. case Stmt::SubstNonTypeTemplateParmExprClass:
  488. case Stmt::SubstNonTypeTemplateParmPackExprClass:
  489. case Stmt::FunctionParmPackExprClass:
  490. case Stmt::UnresolvedLookupExprClass:
  491. case Stmt::TypoExprClass: // A typo could actually be a DeclRef or a MemberRef
  492. K = CXCursor_DeclRefExpr;
  493. break;
  494. case Stmt::CXXDependentScopeMemberExprClass:
  495. case Stmt::CXXPseudoDestructorExprClass:
  496. case Stmt::MemberExprClass:
  497. case Stmt::MSPropertyRefExprClass:
  498. case Stmt::ObjCIsaExprClass:
  499. case Stmt::ObjCIvarRefExprClass:
  500. case Stmt::ObjCPropertyRefExprClass:
  501. case Stmt::UnresolvedMemberExprClass:
  502. K = CXCursor_MemberRefExpr;
  503. break;
  504. case Stmt::CallExprClass:
  505. case Stmt::CXXOperatorCallExprClass:
  506. case Stmt::CXXMemberCallExprClass:
  507. case Stmt::CUDAKernelCallExprClass:
  508. case Stmt::CXXConstructExprClass:
  509. case Stmt::CXXInheritedCtorInitExprClass:
  510. case Stmt::CXXTemporaryObjectExprClass:
  511. case Stmt::CXXUnresolvedConstructExprClass:
  512. case Stmt::UserDefinedLiteralClass:
  513. K = CXCursor_CallExpr;
  514. break;
  515. case Stmt::LambdaExprClass:
  516. K = CXCursor_LambdaExpr;
  517. break;
  518. case Stmt::ObjCMessageExprClass: {
  519. K = CXCursor_ObjCMessageExpr;
  520. int SelectorIdIndex = -1;
  521. // Check if cursor points to a selector id.
  522. if (RegionOfInterest.isValid() &&
  523. RegionOfInterest.getBegin() == RegionOfInterest.getEnd()) {
  524. SmallVector<SourceLocation, 16> SelLocs;
  525. cast<ObjCMessageExpr>(S)->getSelectorLocs(SelLocs);
  526. SmallVectorImpl<SourceLocation>::iterator I =
  527. llvm::find(SelLocs, RegionOfInterest.getBegin());
  528. if (I != SelLocs.end())
  529. SelectorIdIndex = I - SelLocs.begin();
  530. }
  531. CXCursor C = {K, 0, {Parent, S, TU}};
  532. return getSelectorIdentifierCursor(SelectorIdIndex, C);
  533. }
  534. case Stmt::MSDependentExistsStmtClass:
  535. K = CXCursor_UnexposedStmt;
  536. break;
  537. case Stmt::OMPCanonicalLoopClass:
  538. K = CXCursor_OMPCanonicalLoop;
  539. break;
  540. case Stmt::OMPMetaDirectiveClass:
  541. K = CXCursor_OMPMetaDirective;
  542. break;
  543. case Stmt::OMPParallelDirectiveClass:
  544. K = CXCursor_OMPParallelDirective;
  545. break;
  546. case Stmt::OMPSimdDirectiveClass:
  547. K = CXCursor_OMPSimdDirective;
  548. break;
  549. case Stmt::OMPTileDirectiveClass:
  550. K = CXCursor_OMPTileDirective;
  551. break;
  552. case Stmt::OMPUnrollDirectiveClass:
  553. K = CXCursor_OMPUnrollDirective;
  554. break;
  555. case Stmt::OMPForDirectiveClass:
  556. K = CXCursor_OMPForDirective;
  557. break;
  558. case Stmt::OMPForSimdDirectiveClass:
  559. K = CXCursor_OMPForSimdDirective;
  560. break;
  561. case Stmt::OMPSectionsDirectiveClass:
  562. K = CXCursor_OMPSectionsDirective;
  563. break;
  564. case Stmt::OMPSectionDirectiveClass:
  565. K = CXCursor_OMPSectionDirective;
  566. break;
  567. case Stmt::OMPSingleDirectiveClass:
  568. K = CXCursor_OMPSingleDirective;
  569. break;
  570. case Stmt::OMPMasterDirectiveClass:
  571. K = CXCursor_OMPMasterDirective;
  572. break;
  573. case Stmt::OMPCriticalDirectiveClass:
  574. K = CXCursor_OMPCriticalDirective;
  575. break;
  576. case Stmt::OMPParallelForDirectiveClass:
  577. K = CXCursor_OMPParallelForDirective;
  578. break;
  579. case Stmt::OMPParallelForSimdDirectiveClass:
  580. K = CXCursor_OMPParallelForSimdDirective;
  581. break;
  582. case Stmt::OMPParallelMasterDirectiveClass:
  583. K = CXCursor_OMPParallelMasterDirective;
  584. break;
  585. case Stmt::OMPParallelSectionsDirectiveClass:
  586. K = CXCursor_OMPParallelSectionsDirective;
  587. break;
  588. case Stmt::OMPTaskDirectiveClass:
  589. K = CXCursor_OMPTaskDirective;
  590. break;
  591. case Stmt::OMPTaskyieldDirectiveClass:
  592. K = CXCursor_OMPTaskyieldDirective;
  593. break;
  594. case Stmt::OMPBarrierDirectiveClass:
  595. K = CXCursor_OMPBarrierDirective;
  596. break;
  597. case Stmt::OMPTaskwaitDirectiveClass:
  598. K = CXCursor_OMPTaskwaitDirective;
  599. break;
  600. case Stmt::OMPTaskgroupDirectiveClass:
  601. K = CXCursor_OMPTaskgroupDirective;
  602. break;
  603. case Stmt::OMPFlushDirectiveClass:
  604. K = CXCursor_OMPFlushDirective;
  605. break;
  606. case Stmt::OMPDepobjDirectiveClass:
  607. K = CXCursor_OMPDepobjDirective;
  608. break;
  609. case Stmt::OMPScanDirectiveClass:
  610. K = CXCursor_OMPScanDirective;
  611. break;
  612. case Stmt::OMPOrderedDirectiveClass:
  613. K = CXCursor_OMPOrderedDirective;
  614. break;
  615. case Stmt::OMPAtomicDirectiveClass:
  616. K = CXCursor_OMPAtomicDirective;
  617. break;
  618. case Stmt::OMPTargetDirectiveClass:
  619. K = CXCursor_OMPTargetDirective;
  620. break;
  621. case Stmt::OMPTargetDataDirectiveClass:
  622. K = CXCursor_OMPTargetDataDirective;
  623. break;
  624. case Stmt::OMPTargetEnterDataDirectiveClass:
  625. K = CXCursor_OMPTargetEnterDataDirective;
  626. break;
  627. case Stmt::OMPTargetExitDataDirectiveClass:
  628. K = CXCursor_OMPTargetExitDataDirective;
  629. break;
  630. case Stmt::OMPTargetParallelDirectiveClass:
  631. K = CXCursor_OMPTargetParallelDirective;
  632. break;
  633. case Stmt::OMPTargetParallelForDirectiveClass:
  634. K = CXCursor_OMPTargetParallelForDirective;
  635. break;
  636. case Stmt::OMPTargetUpdateDirectiveClass:
  637. K = CXCursor_OMPTargetUpdateDirective;
  638. break;
  639. case Stmt::OMPTeamsDirectiveClass:
  640. K = CXCursor_OMPTeamsDirective;
  641. break;
  642. case Stmt::OMPCancellationPointDirectiveClass:
  643. K = CXCursor_OMPCancellationPointDirective;
  644. break;
  645. case Stmt::OMPCancelDirectiveClass:
  646. K = CXCursor_OMPCancelDirective;
  647. break;
  648. case Stmt::OMPTaskLoopDirectiveClass:
  649. K = CXCursor_OMPTaskLoopDirective;
  650. break;
  651. case Stmt::OMPTaskLoopSimdDirectiveClass:
  652. K = CXCursor_OMPTaskLoopSimdDirective;
  653. break;
  654. case Stmt::OMPMasterTaskLoopDirectiveClass:
  655. K = CXCursor_OMPMasterTaskLoopDirective;
  656. break;
  657. case Stmt::OMPMasterTaskLoopSimdDirectiveClass:
  658. K = CXCursor_OMPMasterTaskLoopSimdDirective;
  659. break;
  660. case Stmt::OMPParallelMasterTaskLoopDirectiveClass:
  661. K = CXCursor_OMPParallelMasterTaskLoopDirective;
  662. break;
  663. case Stmt::OMPParallelMasterTaskLoopSimdDirectiveClass:
  664. K = CXCursor_OMPParallelMasterTaskLoopSimdDirective;
  665. break;
  666. case Stmt::OMPDistributeDirectiveClass:
  667. K = CXCursor_OMPDistributeDirective;
  668. break;
  669. case Stmt::OMPDistributeParallelForDirectiveClass:
  670. K = CXCursor_OMPDistributeParallelForDirective;
  671. break;
  672. case Stmt::OMPDistributeParallelForSimdDirectiveClass:
  673. K = CXCursor_OMPDistributeParallelForSimdDirective;
  674. break;
  675. case Stmt::OMPDistributeSimdDirectiveClass:
  676. K = CXCursor_OMPDistributeSimdDirective;
  677. break;
  678. case Stmt::OMPTargetParallelForSimdDirectiveClass:
  679. K = CXCursor_OMPTargetParallelForSimdDirective;
  680. break;
  681. case Stmt::OMPTargetSimdDirectiveClass:
  682. K = CXCursor_OMPTargetSimdDirective;
  683. break;
  684. case Stmt::OMPTeamsDistributeDirectiveClass:
  685. K = CXCursor_OMPTeamsDistributeDirective;
  686. break;
  687. case Stmt::OMPTeamsDistributeSimdDirectiveClass:
  688. K = CXCursor_OMPTeamsDistributeSimdDirective;
  689. break;
  690. case Stmt::OMPTeamsDistributeParallelForSimdDirectiveClass:
  691. K = CXCursor_OMPTeamsDistributeParallelForSimdDirective;
  692. break;
  693. case Stmt::OMPTeamsDistributeParallelForDirectiveClass:
  694. K = CXCursor_OMPTeamsDistributeParallelForDirective;
  695. break;
  696. case Stmt::OMPTargetTeamsDirectiveClass:
  697. K = CXCursor_OMPTargetTeamsDirective;
  698. break;
  699. case Stmt::OMPTargetTeamsDistributeDirectiveClass:
  700. K = CXCursor_OMPTargetTeamsDistributeDirective;
  701. break;
  702. case Stmt::OMPTargetTeamsDistributeParallelForDirectiveClass:
  703. K = CXCursor_OMPTargetTeamsDistributeParallelForDirective;
  704. break;
  705. case Stmt::OMPTargetTeamsDistributeParallelForSimdDirectiveClass:
  706. K = CXCursor_OMPTargetTeamsDistributeParallelForSimdDirective;
  707. break;
  708. case Stmt::OMPTargetTeamsDistributeSimdDirectiveClass:
  709. K = CXCursor_OMPTargetTeamsDistributeSimdDirective;
  710. break;
  711. case Stmt::OMPInteropDirectiveClass:
  712. K = CXCursor_OMPInteropDirective;
  713. break;
  714. case Stmt::OMPDispatchDirectiveClass:
  715. K = CXCursor_OMPDispatchDirective;
  716. break;
  717. case Stmt::OMPMaskedDirectiveClass:
  718. K = CXCursor_OMPMaskedDirective;
  719. break;
  720. case Stmt::OMPGenericLoopDirectiveClass:
  721. K = CXCursor_OMPGenericLoopDirective;
  722. break;
  723. case Stmt::BuiltinBitCastExprClass:
  724. K = CXCursor_BuiltinBitCastExpr;
  725. }
  726. CXCursor C = {K, 0, {Parent, S, TU}};
  727. return C;
  728. }
  729. CXCursor cxcursor::MakeCursorObjCSuperClassRef(ObjCInterfaceDecl *Super,
  730. SourceLocation Loc,
  731. CXTranslationUnit TU) {
  732. assert(Super && TU && "Invalid arguments!");
  733. void *RawLoc = Loc.getPtrEncoding();
  734. CXCursor C = {CXCursor_ObjCSuperClassRef, 0, {Super, RawLoc, TU}};
  735. return C;
  736. }
  737. std::pair<const ObjCInterfaceDecl *, SourceLocation>
  738. cxcursor::getCursorObjCSuperClassRef(CXCursor C) {
  739. assert(C.kind == CXCursor_ObjCSuperClassRef);
  740. return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
  741. SourceLocation::getFromPtrEncoding(C.data[1]));
  742. }
  743. CXCursor cxcursor::MakeCursorObjCProtocolRef(const ObjCProtocolDecl *Proto,
  744. SourceLocation Loc,
  745. CXTranslationUnit TU) {
  746. assert(Proto && TU && "Invalid arguments!");
  747. void *RawLoc = Loc.getPtrEncoding();
  748. CXCursor C = {CXCursor_ObjCProtocolRef, 0, {Proto, RawLoc, TU}};
  749. return C;
  750. }
  751. std::pair<const ObjCProtocolDecl *, SourceLocation>
  752. cxcursor::getCursorObjCProtocolRef(CXCursor C) {
  753. assert(C.kind == CXCursor_ObjCProtocolRef);
  754. return std::make_pair(static_cast<const ObjCProtocolDecl *>(C.data[0]),
  755. SourceLocation::getFromPtrEncoding(C.data[1]));
  756. }
  757. CXCursor cxcursor::MakeCursorObjCClassRef(const ObjCInterfaceDecl *Class,
  758. SourceLocation Loc,
  759. CXTranslationUnit TU) {
  760. // 'Class' can be null for invalid code.
  761. if (!Class)
  762. return MakeCXCursorInvalid(CXCursor_InvalidCode);
  763. assert(TU && "Invalid arguments!");
  764. void *RawLoc = Loc.getPtrEncoding();
  765. CXCursor C = {CXCursor_ObjCClassRef, 0, {Class, RawLoc, TU}};
  766. return C;
  767. }
  768. std::pair<const ObjCInterfaceDecl *, SourceLocation>
  769. cxcursor::getCursorObjCClassRef(CXCursor C) {
  770. assert(C.kind == CXCursor_ObjCClassRef);
  771. return std::make_pair(static_cast<const ObjCInterfaceDecl *>(C.data[0]),
  772. SourceLocation::getFromPtrEncoding(C.data[1]));
  773. }
  774. CXCursor cxcursor::MakeCursorTypeRef(const TypeDecl *Type, SourceLocation Loc,
  775. CXTranslationUnit TU) {
  776. assert(Type && TU && "Invalid arguments!");
  777. void *RawLoc = Loc.getPtrEncoding();
  778. CXCursor C = {CXCursor_TypeRef, 0, {Type, RawLoc, TU}};
  779. return C;
  780. }
  781. std::pair<const TypeDecl *, SourceLocation>
  782. cxcursor::getCursorTypeRef(CXCursor C) {
  783. assert(C.kind == CXCursor_TypeRef);
  784. return std::make_pair(static_cast<const TypeDecl *>(C.data[0]),
  785. SourceLocation::getFromPtrEncoding(C.data[1]));
  786. }
  787. CXCursor cxcursor::MakeCursorTemplateRef(const TemplateDecl *Template,
  788. SourceLocation Loc,
  789. CXTranslationUnit TU) {
  790. assert(Template && TU && "Invalid arguments!");
  791. void *RawLoc = Loc.getPtrEncoding();
  792. CXCursor C = {CXCursor_TemplateRef, 0, {Template, RawLoc, TU}};
  793. return C;
  794. }
  795. std::pair<const TemplateDecl *, SourceLocation>
  796. cxcursor::getCursorTemplateRef(CXCursor C) {
  797. assert(C.kind == CXCursor_TemplateRef);
  798. return std::make_pair(static_cast<const TemplateDecl *>(C.data[0]),
  799. SourceLocation::getFromPtrEncoding(C.data[1]));
  800. }
  801. CXCursor cxcursor::MakeCursorNamespaceRef(const NamedDecl *NS,
  802. SourceLocation Loc,
  803. CXTranslationUnit TU) {
  804. assert(NS && (isa<NamespaceDecl>(NS) || isa<NamespaceAliasDecl>(NS)) && TU &&
  805. "Invalid arguments!");
  806. void *RawLoc = Loc.getPtrEncoding();
  807. CXCursor C = {CXCursor_NamespaceRef, 0, {NS, RawLoc, TU}};
  808. return C;
  809. }
  810. std::pair<const NamedDecl *, SourceLocation>
  811. cxcursor::getCursorNamespaceRef(CXCursor C) {
  812. assert(C.kind == CXCursor_NamespaceRef);
  813. return std::make_pair(static_cast<const NamedDecl *>(C.data[0]),
  814. SourceLocation::getFromPtrEncoding(C.data[1]));
  815. }
  816. CXCursor cxcursor::MakeCursorVariableRef(const VarDecl *Var, SourceLocation Loc,
  817. CXTranslationUnit TU) {
  818. assert(Var && TU && "Invalid arguments!");
  819. void *RawLoc = Loc.getPtrEncoding();
  820. CXCursor C = {CXCursor_VariableRef, 0, {Var, RawLoc, TU}};
  821. return C;
  822. }
  823. std::pair<const VarDecl *, SourceLocation>
  824. cxcursor::getCursorVariableRef(CXCursor C) {
  825. assert(C.kind == CXCursor_VariableRef);
  826. return std::make_pair(static_cast<const VarDecl *>(C.data[0]),
  827. SourceLocation::getFromPtrEncoding(C.data[1]));
  828. }
  829. CXCursor cxcursor::MakeCursorMemberRef(const FieldDecl *Field,
  830. SourceLocation Loc,
  831. CXTranslationUnit TU) {
  832. assert(Field && TU && "Invalid arguments!");
  833. void *RawLoc = Loc.getPtrEncoding();
  834. CXCursor C = {CXCursor_MemberRef, 0, {Field, RawLoc, TU}};
  835. return C;
  836. }
  837. std::pair<const FieldDecl *, SourceLocation>
  838. cxcursor::getCursorMemberRef(CXCursor C) {
  839. assert(C.kind == CXCursor_MemberRef);
  840. return std::make_pair(static_cast<const FieldDecl *>(C.data[0]),
  841. SourceLocation::getFromPtrEncoding(C.data[1]));
  842. }
  843. CXCursor cxcursor::MakeCursorCXXBaseSpecifier(const CXXBaseSpecifier *B,
  844. CXTranslationUnit TU) {
  845. CXCursor C = {CXCursor_CXXBaseSpecifier, 0, {B, nullptr, TU}};
  846. return C;
  847. }
  848. const CXXBaseSpecifier *cxcursor::getCursorCXXBaseSpecifier(CXCursor C) {
  849. assert(C.kind == CXCursor_CXXBaseSpecifier);
  850. return static_cast<const CXXBaseSpecifier *>(C.data[0]);
  851. }
  852. CXCursor cxcursor::MakePreprocessingDirectiveCursor(SourceRange Range,
  853. CXTranslationUnit TU) {
  854. CXCursor C = {
  855. CXCursor_PreprocessingDirective,
  856. 0,
  857. {Range.getBegin().getPtrEncoding(), Range.getEnd().getPtrEncoding(), TU}};
  858. return C;
  859. }
  860. SourceRange cxcursor::getCursorPreprocessingDirective(CXCursor C) {
  861. assert(C.kind == CXCursor_PreprocessingDirective);
  862. SourceRange Range(SourceLocation::getFromPtrEncoding(C.data[0]),
  863. SourceLocation::getFromPtrEncoding(C.data[1]));
  864. ASTUnit *TU = getCursorASTUnit(C);
  865. return TU->mapRangeFromPreamble(Range);
  866. }
  867. CXCursor cxcursor::MakeMacroDefinitionCursor(const MacroDefinitionRecord *MI,
  868. CXTranslationUnit TU) {
  869. CXCursor C = {CXCursor_MacroDefinition, 0, {MI, nullptr, TU}};
  870. return C;
  871. }
  872. const MacroDefinitionRecord *cxcursor::getCursorMacroDefinition(CXCursor C) {
  873. assert(C.kind == CXCursor_MacroDefinition);
  874. return static_cast<const MacroDefinitionRecord *>(C.data[0]);
  875. }
  876. CXCursor cxcursor::MakeMacroExpansionCursor(MacroExpansion *MI,
  877. CXTranslationUnit TU) {
  878. CXCursor C = {CXCursor_MacroExpansion, 0, {MI, nullptr, TU}};
  879. return C;
  880. }
  881. CXCursor cxcursor::MakeMacroExpansionCursor(MacroDefinitionRecord *MI,
  882. SourceLocation Loc,
  883. CXTranslationUnit TU) {
  884. assert(Loc.isValid());
  885. CXCursor C = {CXCursor_MacroExpansion, 0, {MI, Loc.getPtrEncoding(), TU}};
  886. return C;
  887. }
  888. const IdentifierInfo *cxcursor::MacroExpansionCursor::getName() const {
  889. if (isPseudo())
  890. return getAsMacroDefinition()->getName();
  891. return getAsMacroExpansion()->getName();
  892. }
  893. const MacroDefinitionRecord *
  894. cxcursor::MacroExpansionCursor::getDefinition() const {
  895. if (isPseudo())
  896. return getAsMacroDefinition();
  897. return getAsMacroExpansion()->getDefinition();
  898. }
  899. SourceRange cxcursor::MacroExpansionCursor::getSourceRange() const {
  900. if (isPseudo())
  901. return getPseudoLoc();
  902. return getAsMacroExpansion()->getSourceRange();
  903. }
  904. CXCursor cxcursor::MakeInclusionDirectiveCursor(InclusionDirective *ID,
  905. CXTranslationUnit TU) {
  906. CXCursor C = {CXCursor_InclusionDirective, 0, {ID, nullptr, TU}};
  907. return C;
  908. }
  909. const InclusionDirective *cxcursor::getCursorInclusionDirective(CXCursor C) {
  910. assert(C.kind == CXCursor_InclusionDirective);
  911. return static_cast<const InclusionDirective *>(C.data[0]);
  912. }
  913. CXCursor cxcursor::MakeCursorLabelRef(LabelStmt *Label, SourceLocation Loc,
  914. CXTranslationUnit TU) {
  915. assert(Label && TU && "Invalid arguments!");
  916. void *RawLoc = Loc.getPtrEncoding();
  917. CXCursor C = {CXCursor_LabelRef, 0, {Label, RawLoc, TU}};
  918. return C;
  919. }
  920. std::pair<const LabelStmt *, SourceLocation>
  921. cxcursor::getCursorLabelRef(CXCursor C) {
  922. assert(C.kind == CXCursor_LabelRef);
  923. return std::make_pair(static_cast<const LabelStmt *>(C.data[0]),
  924. SourceLocation::getFromPtrEncoding(C.data[1]));
  925. }
  926. CXCursor cxcursor::MakeCursorOverloadedDeclRef(const OverloadExpr *E,
  927. CXTranslationUnit TU) {
  928. assert(E && TU && "Invalid arguments!");
  929. OverloadedDeclRefStorage Storage(E);
  930. void *RawLoc = E->getNameLoc().getPtrEncoding();
  931. CXCursor C = {
  932. CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
  933. return C;
  934. }
  935. CXCursor cxcursor::MakeCursorOverloadedDeclRef(const Decl *D,
  936. SourceLocation Loc,
  937. CXTranslationUnit TU) {
  938. assert(D && TU && "Invalid arguments!");
  939. void *RawLoc = Loc.getPtrEncoding();
  940. OverloadedDeclRefStorage Storage(D);
  941. CXCursor C = {
  942. CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
  943. return C;
  944. }
  945. CXCursor cxcursor::MakeCursorOverloadedDeclRef(TemplateName Name,
  946. SourceLocation Loc,
  947. CXTranslationUnit TU) {
  948. assert(Name.getAsOverloadedTemplate() && TU && "Invalid arguments!");
  949. void *RawLoc = Loc.getPtrEncoding();
  950. OverloadedDeclRefStorage Storage(Name.getAsOverloadedTemplate());
  951. CXCursor C = {
  952. CXCursor_OverloadedDeclRef, 0, {Storage.getOpaqueValue(), RawLoc, TU}};
  953. return C;
  954. }
  955. std::pair<cxcursor::OverloadedDeclRefStorage, SourceLocation>
  956. cxcursor::getCursorOverloadedDeclRef(CXCursor C) {
  957. assert(C.kind == CXCursor_OverloadedDeclRef);
  958. return std::make_pair(OverloadedDeclRefStorage::getFromOpaqueValue(
  959. const_cast<void *>(C.data[0])),
  960. SourceLocation::getFromPtrEncoding(C.data[1]));
  961. }
  962. const Decl *cxcursor::getCursorDecl(CXCursor Cursor) {
  963. return static_cast<const Decl *>(Cursor.data[0]);
  964. }
  965. const Expr *cxcursor::getCursorExpr(CXCursor Cursor) {
  966. return dyn_cast_or_null<Expr>(getCursorStmt(Cursor));
  967. }
  968. const Stmt *cxcursor::getCursorStmt(CXCursor Cursor) {
  969. if (Cursor.kind == CXCursor_ObjCSuperClassRef ||
  970. Cursor.kind == CXCursor_ObjCProtocolRef ||
  971. Cursor.kind == CXCursor_ObjCClassRef)
  972. return nullptr;
  973. return static_cast<const Stmt *>(Cursor.data[1]);
  974. }
  975. const Attr *cxcursor::getCursorAttr(CXCursor Cursor) {
  976. return static_cast<const Attr *>(Cursor.data[1]);
  977. }
  978. ASTContext &cxcursor::getCursorContext(CXCursor Cursor) {
  979. return getCursorASTUnit(Cursor)->getASTContext();
  980. }
  981. ASTUnit *cxcursor::getCursorASTUnit(CXCursor Cursor) {
  982. CXTranslationUnit TU = getCursorTU(Cursor);
  983. if (!TU)
  984. return nullptr;
  985. return cxtu::getASTUnit(TU);
  986. }
  987. CXTranslationUnit cxcursor::getCursorTU(CXCursor Cursor) {
  988. return static_cast<CXTranslationUnit>(const_cast<void *>(Cursor.data[2]));
  989. }
  990. void cxcursor::getOverriddenCursors(CXCursor cursor,
  991. SmallVectorImpl<CXCursor> &overridden) {
  992. assert(clang_isDeclaration(cursor.kind));
  993. const NamedDecl *D = dyn_cast_or_null<NamedDecl>(getCursorDecl(cursor));
  994. if (!D)
  995. return;
  996. CXTranslationUnit TU = getCursorTU(cursor);
  997. SmallVector<const NamedDecl *, 8> OverDecls;
  998. D->getASTContext().getOverriddenMethods(D, OverDecls);
  999. for (SmallVectorImpl<const NamedDecl *>::iterator I = OverDecls.begin(),
  1000. E = OverDecls.end();
  1001. I != E; ++I) {
  1002. overridden.push_back(MakeCXCursor(*I, TU));
  1003. }
  1004. }
  1005. std::pair<int, SourceLocation>
  1006. cxcursor::getSelectorIdentifierIndexAndLoc(CXCursor cursor) {
  1007. if (cursor.kind == CXCursor_ObjCMessageExpr) {
  1008. if (cursor.xdata != -1)
  1009. return std::make_pair(cursor.xdata,
  1010. cast<ObjCMessageExpr>(getCursorExpr(cursor))
  1011. ->getSelectorLoc(cursor.xdata));
  1012. } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
  1013. cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
  1014. if (cursor.xdata != -1)
  1015. return std::make_pair(cursor.xdata,
  1016. cast<ObjCMethodDecl>(getCursorDecl(cursor))
  1017. ->getSelectorLoc(cursor.xdata));
  1018. }
  1019. return std::make_pair(-1, SourceLocation());
  1020. }
  1021. CXCursor cxcursor::getSelectorIdentifierCursor(int SelIdx, CXCursor cursor) {
  1022. CXCursor newCursor = cursor;
  1023. if (cursor.kind == CXCursor_ObjCMessageExpr) {
  1024. if (SelIdx == -1 ||
  1025. unsigned(SelIdx) >=
  1026. cast<ObjCMessageExpr>(getCursorExpr(cursor))->getNumSelectorLocs())
  1027. newCursor.xdata = -1;
  1028. else
  1029. newCursor.xdata = SelIdx;
  1030. } else if (cursor.kind == CXCursor_ObjCClassMethodDecl ||
  1031. cursor.kind == CXCursor_ObjCInstanceMethodDecl) {
  1032. if (SelIdx == -1 ||
  1033. unsigned(SelIdx) >=
  1034. cast<ObjCMethodDecl>(getCursorDecl(cursor))->getNumSelectorLocs())
  1035. newCursor.xdata = -1;
  1036. else
  1037. newCursor.xdata = SelIdx;
  1038. }
  1039. return newCursor;
  1040. }
  1041. CXCursor cxcursor::getTypeRefCursor(CXCursor cursor) {
  1042. if (cursor.kind != CXCursor_CallExpr)
  1043. return cursor;
  1044. if (cursor.xdata == 0)
  1045. return cursor;
  1046. const Expr *E = getCursorExpr(cursor);
  1047. TypeSourceInfo *Type = nullptr;
  1048. if (const CXXUnresolvedConstructExpr *UnCtor =
  1049. dyn_cast<CXXUnresolvedConstructExpr>(E)) {
  1050. Type = UnCtor->getTypeSourceInfo();
  1051. } else if (const CXXTemporaryObjectExpr *Tmp =
  1052. dyn_cast<CXXTemporaryObjectExpr>(E)) {
  1053. Type = Tmp->getTypeSourceInfo();
  1054. }
  1055. if (!Type)
  1056. return cursor;
  1057. CXTranslationUnit TU = getCursorTU(cursor);
  1058. QualType Ty = Type->getType();
  1059. TypeLoc TL = Type->getTypeLoc();
  1060. SourceLocation Loc = TL.getBeginLoc();
  1061. if (const ElaboratedType *ElabT = Ty->getAs<ElaboratedType>()) {
  1062. Ty = ElabT->getNamedType();
  1063. ElaboratedTypeLoc ElabTL = TL.castAs<ElaboratedTypeLoc>();
  1064. Loc = ElabTL.getNamedTypeLoc().getBeginLoc();
  1065. }
  1066. if (const TypedefType *Typedef = Ty->getAs<TypedefType>())
  1067. return MakeCursorTypeRef(Typedef->getDecl(), Loc, TU);
  1068. if (const TagType *Tag = Ty->getAs<TagType>())
  1069. return MakeCursorTypeRef(Tag->getDecl(), Loc, TU);
  1070. if (const TemplateTypeParmType *TemplP = Ty->getAs<TemplateTypeParmType>())
  1071. return MakeCursorTypeRef(TemplP->getDecl(), Loc, TU);
  1072. return cursor;
  1073. }
  1074. bool cxcursor::operator==(CXCursor X, CXCursor Y) {
  1075. return X.kind == Y.kind && X.data[0] == Y.data[0] && X.data[1] == Y.data[1] &&
  1076. X.data[2] == Y.data[2];
  1077. }
  1078. // FIXME: Remove once we can model DeclGroups and their appropriate ranges
  1079. // properly in the ASTs.
  1080. bool cxcursor::isFirstInDeclGroup(CXCursor C) {
  1081. assert(clang_isDeclaration(C.kind));
  1082. return ((uintptr_t)(C.data[1])) != 0;
  1083. }
  1084. //===----------------------------------------------------------------------===//
  1085. // libclang CXCursor APIs
  1086. //===----------------------------------------------------------------------===//
  1087. int clang_Cursor_isNull(CXCursor cursor) {
  1088. return clang_equalCursors(cursor, clang_getNullCursor());
  1089. }
  1090. CXTranslationUnit clang_Cursor_getTranslationUnit(CXCursor cursor) {
  1091. return getCursorTU(cursor);
  1092. }
  1093. int clang_Cursor_getNumArguments(CXCursor C) {
  1094. if (clang_isDeclaration(C.kind)) {
  1095. const Decl *D = cxcursor::getCursorDecl(C);
  1096. if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D))
  1097. return MD->param_size();
  1098. if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D))
  1099. return FD->param_size();
  1100. }
  1101. if (clang_isExpression(C.kind)) {
  1102. const Expr *E = cxcursor::getCursorExpr(C);
  1103. if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
  1104. return CE->getNumArgs();
  1105. }
  1106. if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
  1107. return CE->getNumArgs();
  1108. }
  1109. }
  1110. return -1;
  1111. }
  1112. CXCursor clang_Cursor_getArgument(CXCursor C, unsigned i) {
  1113. if (clang_isDeclaration(C.kind)) {
  1114. const Decl *D = cxcursor::getCursorDecl(C);
  1115. if (const ObjCMethodDecl *MD = dyn_cast_or_null<ObjCMethodDecl>(D)) {
  1116. if (i < MD->param_size())
  1117. return cxcursor::MakeCXCursor(MD->parameters()[i],
  1118. cxcursor::getCursorTU(C));
  1119. } else if (const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D)) {
  1120. if (i < FD->param_size())
  1121. return cxcursor::MakeCXCursor(FD->parameters()[i],
  1122. cxcursor::getCursorTU(C));
  1123. }
  1124. }
  1125. if (clang_isExpression(C.kind)) {
  1126. const Expr *E = cxcursor::getCursorExpr(C);
  1127. if (const CallExpr *CE = dyn_cast<CallExpr>(E)) {
  1128. if (i < CE->getNumArgs()) {
  1129. return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
  1130. cxcursor::getCursorTU(C));
  1131. }
  1132. }
  1133. if (const CXXConstructExpr *CE = dyn_cast<CXXConstructExpr>(E)) {
  1134. if (i < CE->getNumArgs()) {
  1135. return cxcursor::MakeCXCursor(CE->getArg(i), getCursorDecl(C),
  1136. cxcursor::getCursorTU(C));
  1137. }
  1138. }
  1139. }
  1140. return clang_getNullCursor();
  1141. }
  1142. int clang_Cursor_getNumTemplateArguments(CXCursor C) {
  1143. if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
  1144. return -1;
  1145. }
  1146. const FunctionDecl *FD =
  1147. llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
  1148. if (!FD) {
  1149. return -1;
  1150. }
  1151. const FunctionTemplateSpecializationInfo *SpecInfo =
  1152. FD->getTemplateSpecializationInfo();
  1153. if (!SpecInfo) {
  1154. return -1;
  1155. }
  1156. return SpecInfo->TemplateArguments->size();
  1157. }
  1158. enum CXGetTemplateArgumentStatus {
  1159. /** The operation completed successfully */
  1160. CXGetTemplateArgumentStatus_Success = 0,
  1161. /** The specified cursor did not represent a FunctionDecl. */
  1162. CXGetTemplateArgumentStatus_CursorNotFunctionDecl = -1,
  1163. /** The specified cursor was not castable to a FunctionDecl. */
  1164. CXGetTemplateArgumentStatus_BadFunctionDeclCast = -2,
  1165. /** A NULL FunctionTemplateSpecializationInfo was retrieved. */
  1166. CXGetTemplateArgumentStatus_NullTemplSpecInfo = -3,
  1167. /** An invalid (OOB) argument index was specified */
  1168. CXGetTemplateArgumentStatus_InvalidIndex = -4
  1169. };
  1170. static int clang_Cursor_getTemplateArgument(CXCursor C, unsigned I,
  1171. TemplateArgument *TA) {
  1172. if (clang_getCursorKind(C) != CXCursor_FunctionDecl) {
  1173. return CXGetTemplateArgumentStatus_CursorNotFunctionDecl;
  1174. }
  1175. const FunctionDecl *FD =
  1176. llvm::dyn_cast_or_null<clang::FunctionDecl>(getCursorDecl(C));
  1177. if (!FD) {
  1178. return CXGetTemplateArgumentStatus_BadFunctionDeclCast;
  1179. }
  1180. const FunctionTemplateSpecializationInfo *SpecInfo =
  1181. FD->getTemplateSpecializationInfo();
  1182. if (!SpecInfo) {
  1183. return CXGetTemplateArgumentStatus_NullTemplSpecInfo;
  1184. }
  1185. if (I >= SpecInfo->TemplateArguments->size()) {
  1186. return CXGetTemplateArgumentStatus_InvalidIndex;
  1187. }
  1188. *TA = SpecInfo->TemplateArguments->get(I);
  1189. return 0;
  1190. }
  1191. enum CXTemplateArgumentKind clang_Cursor_getTemplateArgumentKind(CXCursor C,
  1192. unsigned I) {
  1193. TemplateArgument TA;
  1194. if (clang_Cursor_getTemplateArgument(C, I, &TA)) {
  1195. return CXTemplateArgumentKind_Invalid;
  1196. }
  1197. switch (TA.getKind()) {
  1198. case TemplateArgument::Null:
  1199. return CXTemplateArgumentKind_Null;
  1200. case TemplateArgument::Type:
  1201. return CXTemplateArgumentKind_Type;
  1202. case TemplateArgument::Declaration:
  1203. return CXTemplateArgumentKind_Declaration;
  1204. case TemplateArgument::NullPtr:
  1205. return CXTemplateArgumentKind_NullPtr;
  1206. case TemplateArgument::Integral:
  1207. return CXTemplateArgumentKind_Integral;
  1208. case TemplateArgument::Template:
  1209. return CXTemplateArgumentKind_Template;
  1210. case TemplateArgument::TemplateExpansion:
  1211. return CXTemplateArgumentKind_TemplateExpansion;
  1212. case TemplateArgument::Expression:
  1213. return CXTemplateArgumentKind_Expression;
  1214. case TemplateArgument::Pack:
  1215. return CXTemplateArgumentKind_Pack;
  1216. }
  1217. return CXTemplateArgumentKind_Invalid;
  1218. }
  1219. CXType clang_Cursor_getTemplateArgumentType(CXCursor C, unsigned I) {
  1220. TemplateArgument TA;
  1221. if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
  1222. CXGetTemplateArgumentStatus_Success) {
  1223. return cxtype::MakeCXType(QualType(), getCursorTU(C));
  1224. }
  1225. if (TA.getKind() != TemplateArgument::Type) {
  1226. return cxtype::MakeCXType(QualType(), getCursorTU(C));
  1227. }
  1228. return cxtype::MakeCXType(TA.getAsType(), getCursorTU(C));
  1229. }
  1230. long long clang_Cursor_getTemplateArgumentValue(CXCursor C, unsigned I) {
  1231. TemplateArgument TA;
  1232. if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
  1233. CXGetTemplateArgumentStatus_Success) {
  1234. assert(0 && "Unable to retrieve TemplateArgument");
  1235. return 0;
  1236. }
  1237. if (TA.getKind() != TemplateArgument::Integral) {
  1238. assert(0 && "Passed template argument is not Integral");
  1239. return 0;
  1240. }
  1241. return TA.getAsIntegral().getSExtValue();
  1242. }
  1243. unsigned long long clang_Cursor_getTemplateArgumentUnsignedValue(CXCursor C,
  1244. unsigned I) {
  1245. TemplateArgument TA;
  1246. if (clang_Cursor_getTemplateArgument(C, I, &TA) !=
  1247. CXGetTemplateArgumentStatus_Success) {
  1248. assert(0 && "Unable to retrieve TemplateArgument");
  1249. return 0;
  1250. }
  1251. if (TA.getKind() != TemplateArgument::Integral) {
  1252. assert(0 && "Passed template argument is not Integral");
  1253. return 0;
  1254. }
  1255. return TA.getAsIntegral().getZExtValue();
  1256. }
  1257. //===----------------------------------------------------------------------===//
  1258. // CXCursorSet.
  1259. //===----------------------------------------------------------------------===//
  1260. typedef llvm::DenseMap<CXCursor, unsigned> CXCursorSet_Impl;
  1261. static inline CXCursorSet packCXCursorSet(CXCursorSet_Impl *setImpl) {
  1262. return (CXCursorSet)setImpl;
  1263. }
  1264. static inline CXCursorSet_Impl *unpackCXCursorSet(CXCursorSet set) {
  1265. return (CXCursorSet_Impl *)set;
  1266. }
  1267. namespace llvm {
  1268. template <> struct DenseMapInfo<CXCursor> {
  1269. public:
  1270. static inline CXCursor getEmptyKey() {
  1271. return MakeCXCursorInvalid(CXCursor_InvalidFile);
  1272. }
  1273. static inline CXCursor getTombstoneKey() {
  1274. return MakeCXCursorInvalid(CXCursor_NoDeclFound);
  1275. }
  1276. static inline unsigned getHashValue(const CXCursor &cursor) {
  1277. return llvm::DenseMapInfo<std::pair<const void *, const void *>>::
  1278. getHashValue(std::make_pair(cursor.data[0], cursor.data[1]));
  1279. }
  1280. static inline bool isEqual(const CXCursor &x, const CXCursor &y) {
  1281. return x.kind == y.kind && x.data[0] == y.data[0] && x.data[1] == y.data[1];
  1282. }
  1283. };
  1284. } // namespace llvm
  1285. CXCursorSet clang_createCXCursorSet() {
  1286. return packCXCursorSet(new CXCursorSet_Impl());
  1287. }
  1288. void clang_disposeCXCursorSet(CXCursorSet set) {
  1289. delete unpackCXCursorSet(set);
  1290. }
  1291. unsigned clang_CXCursorSet_contains(CXCursorSet set, CXCursor cursor) {
  1292. CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
  1293. if (!setImpl)
  1294. return 0;
  1295. return setImpl->find(cursor) != setImpl->end();
  1296. }
  1297. unsigned clang_CXCursorSet_insert(CXCursorSet set, CXCursor cursor) {
  1298. // Do not insert invalid cursors into the set.
  1299. if (cursor.kind >= CXCursor_FirstInvalid &&
  1300. cursor.kind <= CXCursor_LastInvalid)
  1301. return 1;
  1302. CXCursorSet_Impl *setImpl = unpackCXCursorSet(set);
  1303. if (!setImpl)
  1304. return 1;
  1305. unsigned &entry = (*setImpl)[cursor];
  1306. unsigned flag = entry == 0 ? 1 : 0;
  1307. entry = 1;
  1308. return flag;
  1309. }
  1310. CXCompletionString clang_getCursorCompletionString(CXCursor cursor) {
  1311. enum CXCursorKind kind = clang_getCursorKind(cursor);
  1312. if (clang_isDeclaration(kind)) {
  1313. const Decl *decl = getCursorDecl(cursor);
  1314. if (const NamedDecl *namedDecl = dyn_cast_or_null<NamedDecl>(decl)) {
  1315. ASTUnit *unit = getCursorASTUnit(cursor);
  1316. CodeCompletionResult Result(namedDecl, CCP_Declaration);
  1317. CodeCompletionString *String = Result.CreateCodeCompletionString(
  1318. unit->getASTContext(), unit->getPreprocessor(),
  1319. CodeCompletionContext::CCC_Other,
  1320. unit->getCodeCompletionTUInfo().getAllocator(),
  1321. unit->getCodeCompletionTUInfo(), true);
  1322. return String;
  1323. }
  1324. } else if (kind == CXCursor_MacroDefinition) {
  1325. const MacroDefinitionRecord *definition = getCursorMacroDefinition(cursor);
  1326. const IdentifierInfo *Macro = definition->getName();
  1327. ASTUnit *unit = getCursorASTUnit(cursor);
  1328. CodeCompletionResult Result(
  1329. Macro,
  1330. unit->getPreprocessor().getMacroDefinition(Macro).getMacroInfo());
  1331. CodeCompletionString *String = Result.CreateCodeCompletionString(
  1332. unit->getASTContext(), unit->getPreprocessor(),
  1333. CodeCompletionContext::CCC_Other,
  1334. unit->getCodeCompletionTUInfo().getAllocator(),
  1335. unit->getCodeCompletionTUInfo(), false);
  1336. return String;
  1337. }
  1338. return nullptr;
  1339. }
  1340. namespace {
  1341. struct OverridenCursorsPool {
  1342. typedef SmallVector<CXCursor, 2> CursorVec;
  1343. std::vector<CursorVec *> AllCursors;
  1344. std::vector<CursorVec *> AvailableCursors;
  1345. ~OverridenCursorsPool() {
  1346. for (std::vector<CursorVec *>::iterator I = AllCursors.begin(),
  1347. E = AllCursors.end();
  1348. I != E; ++I) {
  1349. delete *I;
  1350. }
  1351. }
  1352. };
  1353. } // namespace
  1354. void *cxcursor::createOverridenCXCursorsPool() {
  1355. return new OverridenCursorsPool();
  1356. }
  1357. void cxcursor::disposeOverridenCXCursorsPool(void *pool) {
  1358. delete static_cast<OverridenCursorsPool *>(pool);
  1359. }
  1360. void clang_getOverriddenCursors(CXCursor cursor, CXCursor **overridden,
  1361. unsigned *num_overridden) {
  1362. if (overridden)
  1363. *overridden = nullptr;
  1364. if (num_overridden)
  1365. *num_overridden = 0;
  1366. CXTranslationUnit TU = cxcursor::getCursorTU(cursor);
  1367. if (!overridden || !num_overridden || !TU)
  1368. return;
  1369. if (!clang_isDeclaration(cursor.kind))
  1370. return;
  1371. OverridenCursorsPool &pool =
  1372. *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
  1373. OverridenCursorsPool::CursorVec *Vec = nullptr;
  1374. if (!pool.AvailableCursors.empty()) {
  1375. Vec = pool.AvailableCursors.back();
  1376. pool.AvailableCursors.pop_back();
  1377. } else {
  1378. Vec = new OverridenCursorsPool::CursorVec();
  1379. pool.AllCursors.push_back(Vec);
  1380. }
  1381. // Clear out the vector, but don't free the memory contents. This
  1382. // reduces malloc() traffic.
  1383. Vec->clear();
  1384. // Use the first entry to contain a back reference to the vector.
  1385. // This is a complete hack.
  1386. CXCursor backRefCursor = MakeCXCursorInvalid(CXCursor_InvalidFile, TU);
  1387. backRefCursor.data[0] = Vec;
  1388. assert(cxcursor::getCursorTU(backRefCursor) == TU);
  1389. Vec->push_back(backRefCursor);
  1390. // Get the overridden cursors.
  1391. cxcursor::getOverriddenCursors(cursor, *Vec);
  1392. // Did we get any overridden cursors? If not, return Vec to the pool
  1393. // of available cursor vectors.
  1394. if (Vec->size() == 1) {
  1395. pool.AvailableCursors.push_back(Vec);
  1396. return;
  1397. }
  1398. // Now tell the caller about the overridden cursors.
  1399. assert(Vec->size() > 1);
  1400. *overridden = &((*Vec)[1]);
  1401. *num_overridden = Vec->size() - 1;
  1402. }
  1403. void clang_disposeOverriddenCursors(CXCursor *overridden) {
  1404. if (!overridden)
  1405. return;
  1406. // Use pointer arithmetic to get back the first faux entry
  1407. // which has a back-reference to the TU and the vector.
  1408. --overridden;
  1409. OverridenCursorsPool::CursorVec *Vec =
  1410. static_cast<OverridenCursorsPool::CursorVec *>(
  1411. const_cast<void *>(overridden->data[0]));
  1412. CXTranslationUnit TU = getCursorTU(*overridden);
  1413. assert(Vec && TU);
  1414. OverridenCursorsPool &pool =
  1415. *static_cast<OverridenCursorsPool *>(TU->OverridenCursorsPool);
  1416. pool.AvailableCursors.push_back(Vec);
  1417. }
  1418. int clang_Cursor_isDynamicCall(CXCursor C) {
  1419. const Expr *E = nullptr;
  1420. if (clang_isExpression(C.kind))
  1421. E = getCursorExpr(C);
  1422. if (!E)
  1423. return 0;
  1424. if (const ObjCMessageExpr *MsgE = dyn_cast<ObjCMessageExpr>(E)) {
  1425. if (MsgE->getReceiverKind() != ObjCMessageExpr::Instance)
  1426. return false;
  1427. if (auto *RecE = dyn_cast<ObjCMessageExpr>(
  1428. MsgE->getInstanceReceiver()->IgnoreParenCasts())) {
  1429. if (RecE->getMethodFamily() == OMF_alloc)
  1430. return false;
  1431. }
  1432. return true;
  1433. }
  1434. if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
  1435. return !PropRefE->isSuperReceiver();
  1436. }
  1437. const MemberExpr *ME = nullptr;
  1438. if (isa<MemberExpr>(E))
  1439. ME = cast<MemberExpr>(E);
  1440. else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
  1441. ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
  1442. if (ME) {
  1443. if (const CXXMethodDecl *MD =
  1444. dyn_cast_or_null<CXXMethodDecl>(ME->getMemberDecl()))
  1445. return MD->isVirtual() &&
  1446. ME->performsVirtualDispatch(
  1447. cxcursor::getCursorContext(C).getLangOpts());
  1448. }
  1449. return 0;
  1450. }
  1451. CXType clang_Cursor_getReceiverType(CXCursor C) {
  1452. CXTranslationUnit TU = cxcursor::getCursorTU(C);
  1453. const Expr *E = nullptr;
  1454. if (clang_isExpression(C.kind))
  1455. E = getCursorExpr(C);
  1456. if (const ObjCMessageExpr *MsgE = dyn_cast_or_null<ObjCMessageExpr>(E))
  1457. return cxtype::MakeCXType(MsgE->getReceiverType(), TU);
  1458. if (auto *PropRefE = dyn_cast<ObjCPropertyRefExpr>(E)) {
  1459. return cxtype::MakeCXType(
  1460. PropRefE->getReceiverType(cxcursor::getCursorContext(C)), TU);
  1461. }
  1462. const MemberExpr *ME = nullptr;
  1463. if (isa<MemberExpr>(E))
  1464. ME = cast<MemberExpr>(E);
  1465. else if (const CallExpr *CE = dyn_cast<CallExpr>(E))
  1466. ME = dyn_cast_or_null<MemberExpr>(CE->getCallee());
  1467. if (ME) {
  1468. if (isa_and_nonnull<CXXMethodDecl>(ME->getMemberDecl())) {
  1469. auto receiverTy = ME->getBase()->IgnoreImpCasts()->getType();
  1470. return cxtype::MakeCXType(receiverTy, TU);
  1471. }
  1472. }
  1473. return cxtype::MakeCXType(QualType(), TU);
  1474. }