arkanalyzer 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (322) hide show
  1. package/LICENSE +201 -0
  2. package/README.en.md +88 -0
  3. package/README.md +98 -0
  4. package/docs/HowToCreatePR.md +55 -0
  5. package/docs/HowToHandleIssues.md +155 -0
  6. package/docs/quickstart.pdf +0 -0
  7. package/lib/Config.d.ts +26 -0
  8. package/lib/Config.d.ts.map +1 -0
  9. package/lib/Config.js +128 -0
  10. package/lib/Scene.d.ts +125 -0
  11. package/lib/Scene.d.ts.map +1 -0
  12. package/lib/Scene.js +762 -0
  13. package/lib/callgraph/algorithm/AbstractAnalysis.d.ts +28 -0
  14. package/lib/callgraph/algorithm/AbstractAnalysis.d.ts.map +1 -0
  15. package/lib/callgraph/algorithm/AbstractAnalysis.js +137 -0
  16. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts +11 -0
  17. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts.map +1 -0
  18. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.js +71 -0
  19. package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts +16 -0
  20. package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts.map +1 -0
  21. package/lib/callgraph/algorithm/RapidTypeAnalysis.js +147 -0
  22. package/lib/callgraph/common/Statistics.d.ts +58 -0
  23. package/lib/callgraph/common/Statistics.d.ts.map +1 -0
  24. package/lib/callgraph/common/Statistics.js +203 -0
  25. package/lib/callgraph/model/BaseGraph.d.ts +62 -0
  26. package/lib/callgraph/model/BaseGraph.d.ts.map +1 -0
  27. package/lib/callgraph/model/BaseGraph.js +158 -0
  28. package/lib/callgraph/model/CallGraph.d.ts +94 -0
  29. package/lib/callgraph/model/CallGraph.d.ts.map +1 -0
  30. package/lib/callgraph/model/CallGraph.js +339 -0
  31. package/lib/callgraph/model/builder/CallGraphBuilder.d.ts +16 -0
  32. package/lib/callgraph/model/builder/CallGraphBuilder.d.ts.map +1 -0
  33. package/lib/callgraph/model/builder/CallGraphBuilder.js +106 -0
  34. package/lib/callgraph/pointerAnalysis/Context.d.ts +38 -0
  35. package/lib/callgraph/pointerAnalysis/Context.d.ts.map +1 -0
  36. package/lib/callgraph/pointerAnalysis/Context.js +156 -0
  37. package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts +23 -0
  38. package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts.map +1 -0
  39. package/lib/callgraph/pointerAnalysis/DummyCallCreator.js +104 -0
  40. package/lib/callgraph/pointerAnalysis/Pag.d.ts +209 -0
  41. package/lib/callgraph/pointerAnalysis/Pag.d.ts.map +1 -0
  42. package/lib/callgraph/pointerAnalysis/Pag.js +774 -0
  43. package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts +88 -0
  44. package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts.map +1 -0
  45. package/lib/callgraph/pointerAnalysis/PagBuilder.js +821 -0
  46. package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts +64 -0
  47. package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts.map +1 -0
  48. package/lib/callgraph/pointerAnalysis/PointerAnalysis.js +502 -0
  49. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts +9 -0
  50. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts.map +1 -0
  51. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.js +57 -0
  52. package/lib/callgraph/pointerAnalysis/PtsDS.d.ts +58 -0
  53. package/lib/callgraph/pointerAnalysis/PtsDS.d.ts.map +1 -0
  54. package/lib/callgraph/pointerAnalysis/PtsDS.js +234 -0
  55. package/lib/core/base/Constant.d.ts +17 -0
  56. package/lib/core/base/Constant.d.ts.map +1 -0
  57. package/lib/core/base/Constant.js +53 -0
  58. package/lib/core/base/Decorator.d.ts +15 -0
  59. package/lib/core/base/Decorator.d.ts.map +1 -0
  60. package/lib/core/base/Decorator.js +43 -0
  61. package/lib/core/base/DefUseChain.d.ts +9 -0
  62. package/lib/core/base/DefUseChain.d.ts.map +1 -0
  63. package/lib/core/base/DefUseChain.js +25 -0
  64. package/lib/core/base/Expr.d.ts +219 -0
  65. package/lib/core/base/Expr.d.ts.map +1 -0
  66. package/lib/core/base/Expr.js +896 -0
  67. package/lib/core/base/Local.d.ts +40 -0
  68. package/lib/core/base/Local.d.ts.map +1 -0
  69. package/lib/core/base/Local.js +101 -0
  70. package/lib/core/base/Position.d.ts +39 -0
  71. package/lib/core/base/Position.d.ts.map +1 -0
  72. package/lib/core/base/Position.js +86 -0
  73. package/lib/core/base/Ref.d.ts +75 -0
  74. package/lib/core/base/Ref.d.ts.map +1 -0
  75. package/lib/core/base/Ref.js +286 -0
  76. package/lib/core/base/Stmt.d.ts +109 -0
  77. package/lib/core/base/Stmt.d.ts.map +1 -0
  78. package/lib/core/base/Stmt.js +374 -0
  79. package/lib/core/base/Type.d.ts +220 -0
  80. package/lib/core/base/Type.d.ts.map +1 -0
  81. package/lib/core/base/Type.js +477 -0
  82. package/lib/core/base/Value.d.ts +11 -0
  83. package/lib/core/base/Value.d.ts.map +1 -0
  84. package/lib/core/base/Value.js +16 -0
  85. package/lib/core/common/ArkIRTransformer.d.ts +101 -0
  86. package/lib/core/common/ArkIRTransformer.d.ts.map +1 -0
  87. package/lib/core/common/ArkIRTransformer.js +1674 -0
  88. package/lib/core/common/BodyBuilder.d.ts +10 -0
  89. package/lib/core/common/BodyBuilder.d.ts.map +1 -0
  90. package/lib/core/common/BodyBuilder.js +34 -0
  91. package/lib/core/common/Builtin.d.ts +35 -0
  92. package/lib/core/common/Builtin.d.ts.map +1 -0
  93. package/lib/core/common/Builtin.js +78 -0
  94. package/lib/core/common/CfgBuilder.d.ts +136 -0
  95. package/lib/core/common/CfgBuilder.d.ts.map +1 -0
  96. package/lib/core/common/CfgBuilder.js +1241 -0
  97. package/lib/core/common/Const.d.ts +15 -0
  98. package/lib/core/common/Const.d.ts.map +1 -0
  99. package/lib/core/common/Const.js +34 -0
  100. package/lib/core/common/DummyMainCreater.d.ts +48 -0
  101. package/lib/core/common/DummyMainCreater.d.ts.map +1 -0
  102. package/lib/core/common/DummyMainCreater.js +433 -0
  103. package/lib/core/common/EtsConst.d.ts +65 -0
  104. package/lib/core/common/EtsConst.d.ts.map +1 -0
  105. package/lib/core/common/EtsConst.js +918 -0
  106. package/lib/core/common/ExprUseReplacer.d.ts +18 -0
  107. package/lib/core/common/ExprUseReplacer.d.ts.map +1 -0
  108. package/lib/core/common/ExprUseReplacer.js +88 -0
  109. package/lib/core/common/IRUtils.d.ts +7 -0
  110. package/lib/core/common/IRUtils.d.ts.map +1 -0
  111. package/lib/core/common/IRUtils.js +39 -0
  112. package/lib/core/common/ModelUtils.d.ts +61 -0
  113. package/lib/core/common/ModelUtils.d.ts.map +1 -0
  114. package/lib/core/common/ModelUtils.js +628 -0
  115. package/lib/core/common/RefUseReplacer.d.ts +14 -0
  116. package/lib/core/common/RefUseReplacer.d.ts.map +1 -0
  117. package/lib/core/common/RefUseReplacer.js +50 -0
  118. package/lib/core/common/StmtUseReplacer.d.ts +16 -0
  119. package/lib/core/common/StmtUseReplacer.d.ts.map +1 -0
  120. package/lib/core/common/StmtUseReplacer.js +88 -0
  121. package/lib/core/common/TSConst.d.ts +11 -0
  122. package/lib/core/common/TSConst.d.ts.map +1 -0
  123. package/lib/core/common/TSConst.js +28 -0
  124. package/lib/core/common/TypeInference.d.ts +44 -0
  125. package/lib/core/common/TypeInference.d.ts.map +1 -0
  126. package/lib/core/common/TypeInference.js +519 -0
  127. package/lib/core/common/ValueUtil.d.ts +17 -0
  128. package/lib/core/common/ValueUtil.d.ts.map +1 -0
  129. package/lib/core/common/ValueUtil.js +71 -0
  130. package/lib/core/common/VisibleValue.d.ts +37 -0
  131. package/lib/core/common/VisibleValue.d.ts.map +1 -0
  132. package/lib/core/common/VisibleValue.js +212 -0
  133. package/lib/core/dataflow/DataflowProblem.d.ts +21 -0
  134. package/lib/core/dataflow/DataflowProblem.d.ts.map +1 -0
  135. package/lib/core/dataflow/DataflowProblem.js +33 -0
  136. package/lib/core/dataflow/DataflowResult.d.ts +8 -0
  137. package/lib/core/dataflow/DataflowResult.d.ts.map +1 -0
  138. package/lib/core/dataflow/DataflowResult.js +26 -0
  139. package/lib/core/dataflow/DataflowSolver.d.ts +40 -0
  140. package/lib/core/dataflow/DataflowSolver.d.ts.map +1 -0
  141. package/lib/core/dataflow/DataflowSolver.js +318 -0
  142. package/lib/core/dataflow/Edge.d.ts +16 -0
  143. package/lib/core/dataflow/Edge.d.ts.map +1 -0
  144. package/lib/core/dataflow/Edge.js +40 -0
  145. package/lib/core/dataflow/Fact.d.ts +7 -0
  146. package/lib/core/dataflow/Fact.d.ts.map +1 -0
  147. package/lib/core/dataflow/Fact.js +24 -0
  148. package/lib/core/dataflow/TiantAnalysis.d.ts +37 -0
  149. package/lib/core/dataflow/TiantAnalysis.d.ts.map +1 -0
  150. package/lib/core/dataflow/TiantAnalysis.js +326 -0
  151. package/lib/core/dataflow/UndefinedVariable.d.ts +32 -0
  152. package/lib/core/dataflow/UndefinedVariable.d.ts.map +1 -0
  153. package/lib/core/dataflow/UndefinedVariable.js +235 -0
  154. package/lib/core/dataflow/Util.d.ts +8 -0
  155. package/lib/core/dataflow/Util.d.ts.map +1 -0
  156. package/lib/core/dataflow/Util.js +137 -0
  157. package/lib/core/graph/BasicBlock.d.ts +26 -0
  158. package/lib/core/graph/BasicBlock.d.ts.map +1 -0
  159. package/lib/core/graph/BasicBlock.js +92 -0
  160. package/lib/core/graph/Cfg.d.ts +29 -0
  161. package/lib/core/graph/Cfg.d.ts.map +1 -0
  162. package/lib/core/graph/Cfg.js +154 -0
  163. package/lib/core/graph/DominanceFinder.d.ts +16 -0
  164. package/lib/core/graph/DominanceFinder.d.ts.map +1 -0
  165. package/lib/core/graph/DominanceFinder.js +121 -0
  166. package/lib/core/graph/DominanceTree.d.ts +13 -0
  167. package/lib/core/graph/DominanceTree.d.ts.map +1 -0
  168. package/lib/core/graph/DominanceTree.js +70 -0
  169. package/lib/core/graph/ViewTree.d.ts +115 -0
  170. package/lib/core/graph/ViewTree.d.ts.map +1 -0
  171. package/lib/core/graph/ViewTree.js +16 -0
  172. package/lib/core/graph/builder/ViewTreeBuilder.d.ts +204 -0
  173. package/lib/core/graph/builder/ViewTreeBuilder.d.ts.map +1 -0
  174. package/lib/core/graph/builder/ViewTreeBuilder.js +1046 -0
  175. package/lib/core/model/ArkBody.d.ts +22 -0
  176. package/lib/core/model/ArkBody.d.ts.map +1 -0
  177. package/lib/core/model/ArkBody.js +63 -0
  178. package/lib/core/model/ArkClass.d.ts +110 -0
  179. package/lib/core/model/ArkClass.d.ts.map +1 -0
  180. package/lib/core/model/ArkClass.js +315 -0
  181. package/lib/core/model/ArkExport.d.ts +70 -0
  182. package/lib/core/model/ArkExport.d.ts.map +1 -0
  183. package/lib/core/model/ArkExport.js +143 -0
  184. package/lib/core/model/ArkField.d.ts +60 -0
  185. package/lib/core/model/ArkField.d.ts.map +1 -0
  186. package/lib/core/model/ArkField.js +157 -0
  187. package/lib/core/model/ArkFile.d.ts +62 -0
  188. package/lib/core/model/ArkFile.d.ts.map +1 -0
  189. package/lib/core/model/ArkFile.js +160 -0
  190. package/lib/core/model/ArkImport.d.ts +43 -0
  191. package/lib/core/model/ArkImport.d.ts.map +1 -0
  192. package/lib/core/model/ArkImport.js +109 -0
  193. package/lib/core/model/ArkMethod.d.ts +76 -0
  194. package/lib/core/model/ArkMethod.d.ts.map +1 -0
  195. package/lib/core/model/ArkMethod.js +235 -0
  196. package/lib/core/model/ArkNamespace.d.ts +65 -0
  197. package/lib/core/model/ArkNamespace.d.ts.map +1 -0
  198. package/lib/core/model/ArkNamespace.js +182 -0
  199. package/lib/core/model/ArkSignature.d.ts +98 -0
  200. package/lib/core/model/ArkSignature.d.ts.map +1 -0
  201. package/lib/core/model/ArkSignature.js +281 -0
  202. package/lib/core/model/builder/ArkClassBuilder.d.ts +13 -0
  203. package/lib/core/model/builder/ArkClassBuilder.d.ts.map +1 -0
  204. package/lib/core/model/builder/ArkClassBuilder.js +422 -0
  205. package/lib/core/model/builder/ArkExportBuilder.d.ts +25 -0
  206. package/lib/core/model/builder/ArkExportBuilder.d.ts.map +1 -0
  207. package/lib/core/model/builder/ArkExportBuilder.js +207 -0
  208. package/lib/core/model/builder/ArkFieldBuilder.d.ts +9 -0
  209. package/lib/core/model/builder/ArkFieldBuilder.d.ts.map +1 -0
  210. package/lib/core/model/builder/ArkFieldBuilder.js +189 -0
  211. package/lib/core/model/builder/ArkFileBuilder.d.ts +10 -0
  212. package/lib/core/model/builder/ArkFileBuilder.d.ts.map +1 -0
  213. package/lib/core/model/builder/ArkFileBuilder.js +164 -0
  214. package/lib/core/model/builder/ArkImportBuilder.d.ts +4 -0
  215. package/lib/core/model/builder/ArkImportBuilder.d.ts.map +1 -0
  216. package/lib/core/model/builder/ArkImportBuilder.js +125 -0
  217. package/lib/core/model/builder/ArkMethodBuilder.d.ts +60 -0
  218. package/lib/core/model/builder/ArkMethodBuilder.d.ts.map +1 -0
  219. package/lib/core/model/builder/ArkMethodBuilder.js +419 -0
  220. package/lib/core/model/builder/ArkNamespaceBuilder.d.ts +5 -0
  221. package/lib/core/model/builder/ArkNamespaceBuilder.d.ts.map +1 -0
  222. package/lib/core/model/builder/ArkNamespaceBuilder.js +171 -0
  223. package/lib/core/model/builder/ArkSignatureBuilder.d.ts +9 -0
  224. package/lib/core/model/builder/ArkSignatureBuilder.d.ts.map +1 -0
  225. package/lib/core/model/builder/ArkSignatureBuilder.js +40 -0
  226. package/lib/core/model/builder/builderUtils.d.ts +18 -0
  227. package/lib/core/model/builder/builderUtils.d.ts.map +1 -0
  228. package/lib/core/model/builder/builderUtils.js +449 -0
  229. package/lib/index.d.ts +77 -0
  230. package/lib/index.d.ts.map +1 -0
  231. package/lib/index.js +190 -0
  232. package/lib/save/ArkStream.d.ts +24 -0
  233. package/lib/save/ArkStream.d.ts.map +1 -0
  234. package/lib/save/ArkStream.js +83 -0
  235. package/lib/save/DotPrinter.d.ts +48 -0
  236. package/lib/save/DotPrinter.d.ts.map +1 -0
  237. package/lib/save/DotPrinter.js +229 -0
  238. package/lib/save/GraphPrinter.d.ts +17 -0
  239. package/lib/save/GraphPrinter.d.ts.map +1 -0
  240. package/lib/save/GraphPrinter.js +135 -0
  241. package/lib/save/JsonPrinter.d.ts +28 -0
  242. package/lib/save/JsonPrinter.d.ts.map +1 -0
  243. package/lib/save/JsonPrinter.js +511 -0
  244. package/lib/save/Printer.d.ts +17 -0
  245. package/lib/save/Printer.d.ts.map +1 -0
  246. package/lib/save/Printer.js +27 -0
  247. package/lib/save/PrinterBuilder.d.ts +38 -0
  248. package/lib/save/PrinterBuilder.d.ts.map +1 -0
  249. package/lib/save/PrinterBuilder.js +102 -0
  250. package/lib/save/serializeArkIR.d.ts +3 -0
  251. package/lib/save/serializeArkIR.d.ts.map +1 -0
  252. package/lib/save/serializeArkIR.js +166 -0
  253. package/lib/save/source/SourceBase.d.ts +32 -0
  254. package/lib/save/source/SourceBase.d.ts.map +1 -0
  255. package/lib/save/source/SourceBase.js +93 -0
  256. package/lib/save/source/SourceBody.d.ts +60 -0
  257. package/lib/save/source/SourceBody.d.ts.map +1 -0
  258. package/lib/save/source/SourceBody.js +292 -0
  259. package/lib/save/source/SourceClass.d.ts +27 -0
  260. package/lib/save/source/SourceClass.d.ts.map +1 -0
  261. package/lib/save/source/SourceClass.js +202 -0
  262. package/lib/save/source/SourceField.d.ts +15 -0
  263. package/lib/save/source/SourceField.d.ts.map +1 -0
  264. package/lib/save/source/SourceField.js +68 -0
  265. package/lib/save/source/SourceFilePrinter.d.ts +14 -0
  266. package/lib/save/source/SourceFilePrinter.d.ts.map +1 -0
  267. package/lib/save/source/SourceFilePrinter.js +73 -0
  268. package/lib/save/source/SourceMethod.d.ts +24 -0
  269. package/lib/save/source/SourceMethod.d.ts.map +1 -0
  270. package/lib/save/source/SourceMethod.js +170 -0
  271. package/lib/save/source/SourceModule.d.ts +18 -0
  272. package/lib/save/source/SourceModule.d.ts.map +1 -0
  273. package/lib/save/source/SourceModule.js +120 -0
  274. package/lib/save/source/SourceNamespace.d.ts +13 -0
  275. package/lib/save/source/SourceNamespace.d.ts.map +1 -0
  276. package/lib/save/source/SourceNamespace.js +80 -0
  277. package/lib/save/source/SourceStmt.d.ts +172 -0
  278. package/lib/save/source/SourceStmt.d.ts.map +1 -0
  279. package/lib/save/source/SourceStmt.js +757 -0
  280. package/lib/save/source/SourceTransformer.d.ts +37 -0
  281. package/lib/save/source/SourceTransformer.d.ts.map +1 -0
  282. package/lib/save/source/SourceTransformer.js +338 -0
  283. package/lib/save/source/SourceUtils.d.ts +25 -0
  284. package/lib/save/source/SourceUtils.d.ts.map +1 -0
  285. package/lib/save/source/SourceUtils.js +201 -0
  286. package/lib/transformer/FunctionTransformer.d.ts +3 -0
  287. package/lib/transformer/FunctionTransformer.d.ts.map +1 -0
  288. package/lib/transformer/FunctionTransformer.js +17 -0
  289. package/lib/transformer/SceneTransformer.d.ts +3 -0
  290. package/lib/transformer/SceneTransformer.d.ts.map +1 -0
  291. package/lib/transformer/SceneTransformer.js +17 -0
  292. package/lib/transformer/StaticSingleAssignmentFormer.d.ts +13 -0
  293. package/lib/transformer/StaticSingleAssignmentFormer.d.ts.map +1 -0
  294. package/lib/transformer/StaticSingleAssignmentFormer.js +259 -0
  295. package/lib/transformer/Transformer.d.ts +7 -0
  296. package/lib/transformer/Transformer.d.ts.map +1 -0
  297. package/lib/transformer/Transformer.js +22 -0
  298. package/lib/utils/CfgStructualAnalysis.d.ts +93 -0
  299. package/lib/utils/CfgStructualAnalysis.d.ts.map +1 -0
  300. package/lib/utils/CfgStructualAnalysis.js +955 -0
  301. package/lib/utils/FileUtils.d.ts +18 -0
  302. package/lib/utils/FileUtils.d.ts.map +1 -0
  303. package/lib/utils/FileUtils.js +131 -0
  304. package/lib/utils/callGraphUtils.d.ts +31 -0
  305. package/lib/utils/callGraphUtils.d.ts.map +1 -0
  306. package/lib/utils/callGraphUtils.js +208 -0
  307. package/lib/utils/entryMethodUtils.d.ts +16 -0
  308. package/lib/utils/entryMethodUtils.d.ts.map +1 -0
  309. package/lib/utils/entryMethodUtils.js +139 -0
  310. package/lib/utils/getAllFiles.d.ts +10 -0
  311. package/lib/utils/getAllFiles.d.ts.map +1 -0
  312. package/lib/utils/getAllFiles.js +91 -0
  313. package/lib/utils/json5parser.d.ts +7 -0
  314. package/lib/utils/json5parser.d.ts.map +1 -0
  315. package/lib/utils/json5parser.js +146 -0
  316. package/lib/utils/logger.d.ts +19 -0
  317. package/lib/utils/logger.d.ts.map +1 -0
  318. package/lib/utils/logger.js +90 -0
  319. package/lib/utils/pathTransfer.d.ts +2 -0
  320. package/lib/utils/pathTransfer.d.ts.map +1 -0
  321. package/lib/utils/pathTransfer.js +25 -0
  322. package/package.json +29 -0
@@ -0,0 +1,1241 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ var desc = Object.getOwnPropertyDescriptor(m, k);
19
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
20
+ desc = { enumerable: true, get: function() { return m[k]; } };
21
+ }
22
+ Object.defineProperty(o, k2, desc);
23
+ }) : (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ o[k2] = m[k];
26
+ }));
27
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
28
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
29
+ }) : function(o, v) {
30
+ o["default"] = v;
31
+ });
32
+ var __importStar = (this && this.__importStar) || function (mod) {
33
+ if (mod && mod.__esModule) return mod;
34
+ var result = {};
35
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
36
+ __setModuleDefault(result, mod);
37
+ return result;
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.CfgBuilder = void 0;
41
+ const ts = __importStar(require("ohos-typescript"));
42
+ const Stmt_1 = require("../base/Stmt");
43
+ const BasicBlock_1 = require("../graph/BasicBlock");
44
+ const Cfg_1 = require("../graph/Cfg");
45
+ const ArkIRTransformer_1 = require("./ArkIRTransformer");
46
+ const ModelUtils_1 = require("./ModelUtils");
47
+ const Expr_1 = require("../base/Expr");
48
+ const Builtin_1 = require("./Builtin");
49
+ const IRUtils_1 = require("./IRUtils");
50
+ class StatementBuilder {
51
+ constructor(type, code, astNode, scopeID) {
52
+ this.addressCode3 = [];
53
+ this.passTmies = 0;
54
+ this.numOfIdentifier = 0;
55
+ this.isDoWhile = false;
56
+ this.type = type;
57
+ this.code = code;
58
+ this.next = null;
59
+ this.lasts = new Set();
60
+ this.walked = false;
61
+ this.index = 0;
62
+ this.line = -1;
63
+ this.column = -1;
64
+ this.astNode = astNode;
65
+ this.scopeID = scopeID;
66
+ this.block = null;
67
+ this.ifExitPass = false;
68
+ }
69
+ }
70
+ class ConditionStatementBuilder extends StatementBuilder {
71
+ constructor(type, code, astNode, scopeID) {
72
+ super(type, code, astNode, scopeID);
73
+ this.doStatement = null;
74
+ this.nextT = null;
75
+ this.nextF = null;
76
+ this.loopBlock = null;
77
+ this.condition = '';
78
+ }
79
+ }
80
+ class SwitchStatementBuilder extends StatementBuilder {
81
+ constructor(type, code, astNode, scopeID) {
82
+ super(type, code, astNode, scopeID);
83
+ this.cases = [];
84
+ this.default = null;
85
+ this.nexts = [];
86
+ }
87
+ }
88
+ class TryStatementBuilder extends StatementBuilder {
89
+ constructor(type, code, astNode, scopeID) {
90
+ super(type, code, astNode, scopeID);
91
+ this.tryFirst = null;
92
+ this.tryExit = null;
93
+ this.catchStatement = null;
94
+ this.catchError = '';
95
+ this.finallyStatement = null;
96
+ }
97
+ }
98
+ class Case {
99
+ constructor(value, stmt) {
100
+ this.value = value;
101
+ this.stmt = stmt;
102
+ }
103
+ }
104
+ class DefUseChain {
105
+ constructor(def, use) {
106
+ this.def = def;
107
+ this.use = use;
108
+ }
109
+ }
110
+ class Variable {
111
+ constructor(name, lastDef) {
112
+ this.properties = [];
113
+ this.propOf = null;
114
+ this.name = name;
115
+ this.lastDef = lastDef;
116
+ this.defUse = [];
117
+ }
118
+ }
119
+ class Scope {
120
+ constructor(id, variable, level) {
121
+ this.id = id;
122
+ this.level = level;
123
+ this.parent = null;
124
+ }
125
+ }
126
+ class Block {
127
+ constructor(id, stmts) {
128
+ this.nexts = [];
129
+ this.lasts = [];
130
+ this.walked = false;
131
+ this.id = id;
132
+ this.stmts = stmts;
133
+ }
134
+ }
135
+ class Catch {
136
+ constructor(errorName, from, to, withLabel) {
137
+ this.errorName = errorName;
138
+ this.from = from;
139
+ this.to = to;
140
+ this.withLabel = withLabel;
141
+ }
142
+ }
143
+ class textError extends Error {
144
+ constructor(message) {
145
+ // 调用父类的构造函数,并传入错误消息
146
+ super(message);
147
+ // 设置错误类型的名称
148
+ this.name = 'textError';
149
+ }
150
+ }
151
+ class CfgBuilder {
152
+ constructor(ast, name, declaringMethod, sourceFile) {
153
+ this.exits = [];
154
+ this.emptyBody = false;
155
+ this.arrowFunctionWithoutBlock = false;
156
+ this.name = name;
157
+ this.astRoot = ast;
158
+ this.declaringMethod = declaringMethod;
159
+ this.declaringClass = declaringMethod.getDeclaringArkClass();
160
+ this.entry = new StatementBuilder('entry', '', ast, 0);
161
+ this.loopStack = [];
162
+ this.switchExitStack = [];
163
+ this.functions = [];
164
+ this.breakin = '';
165
+ this.statementArray = [];
166
+ this.dotEdges = [];
167
+ this.exit = new StatementBuilder('exit', 'return;', null, 0);
168
+ this.scopes = [];
169
+ this.scopeLevel = 0;
170
+ this.tempVariableNum = 0;
171
+ this.current3ACstm = this.entry;
172
+ this.blocks = [];
173
+ this.currentDeclarationKeyword = '';
174
+ this.variables = [];
175
+ this.importFromPath = [];
176
+ this.catches = [];
177
+ this.sourceFile = sourceFile;
178
+ this.arrowFunctionWithoutBlock = true;
179
+ }
180
+ walkAST(lastStatement, nextStatement, nodes) {
181
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
182
+ function judgeLastType(s) {
183
+ if (lastStatement.type == 'ifStatement') {
184
+ let lastIf = lastStatement;
185
+ if (lastIf.nextT == null) {
186
+ lastIf.nextT = s;
187
+ s.lasts.add(lastIf);
188
+ }
189
+ else {
190
+ lastIf.nextF = s;
191
+ s.lasts.add(lastIf);
192
+ }
193
+ }
194
+ else if (lastStatement.type == 'loopStatement') {
195
+ let lastLoop = lastStatement;
196
+ lastLoop.nextT = s;
197
+ s.lasts.add(lastLoop);
198
+ }
199
+ else if (lastStatement.type == 'catchOrNot') {
200
+ let lastLoop = lastStatement;
201
+ lastLoop.nextT = s;
202
+ s.lasts.add(lastLoop);
203
+ }
204
+ else {
205
+ lastStatement.next = s;
206
+ s.lasts.add(lastStatement);
207
+ }
208
+ }
209
+ this.scopeLevel++;
210
+ let scope = new Scope(this.scopes.length, new Set(), this.scopeLevel);
211
+ for (let i = this.scopes.length - 1; i >= 0; i--) {
212
+ if (this.scopes[i].level == this.scopeLevel - 1) {
213
+ scope.parent = this.scopes[i];
214
+ break;
215
+ }
216
+ }
217
+ this.scopes.push(scope);
218
+ for (let i = 0; i < nodes.length; i++) {
219
+ let c = nodes[i];
220
+ if (ts.isVariableStatement(c) || ts.isExpressionStatement(c) || ts.isThrowStatement(c) || ts.isTypeAliasDeclaration(c)) {
221
+ let s = new StatementBuilder('statement', c.getText(this.sourceFile), c, scope.id);
222
+ judgeLastType(s);
223
+ lastStatement = s;
224
+ }
225
+ else if (ts.isReturnStatement(c)) {
226
+ let s = new StatementBuilder('returnStatement', c.getText(this.sourceFile), c, scope.id);
227
+ judgeLastType(s);
228
+ s.astNode = c;
229
+ lastStatement = s;
230
+ break;
231
+ }
232
+ else if (ts.isBreakStatement(c)) {
233
+ let p = c;
234
+ while (p) {
235
+ if (ts.isWhileStatement(p) || ts.isDoStatement(p) || ts.isForStatement(p) || ts.isForInStatement(p) || ts.isForOfStatement(p)) {
236
+ const lastLoopNextF = this.loopStack[this.loopStack.length - 1].nextF;
237
+ judgeLastType(lastLoopNextF);
238
+ lastLoopNextF.lasts.add(lastStatement);
239
+ return;
240
+ }
241
+ if (ts.isCaseClause(p) || ts.isDefaultClause(p)) {
242
+ const lastSwitchExit = this.switchExitStack[this.switchExitStack.length - 1];
243
+ judgeLastType(lastSwitchExit);
244
+ lastSwitchExit.lasts.add(lastStatement);
245
+ return;
246
+ }
247
+ p = p.parent;
248
+ }
249
+ }
250
+ else if (ts.isContinueStatement(c)) {
251
+ const lastLoop = this.loopStack[this.loopStack.length - 1];
252
+ judgeLastType(lastLoop);
253
+ lastLoop.lasts.add(lastStatement);
254
+ return;
255
+ }
256
+ else if (ts.isIfStatement(c)) {
257
+ let ifstm = new ConditionStatementBuilder('ifStatement', '', c, scope.id);
258
+ judgeLastType(ifstm);
259
+ let ifexit = new StatementBuilder('ifExit', '', c, scope.id);
260
+ this.exits.push(ifexit);
261
+ ifstm.condition = c.expression.getText(this.sourceFile);
262
+ ifstm.code = 'if (' + ifstm.condition + ')';
263
+ if (ts.isBlock(c.thenStatement)) {
264
+ this.walkAST(ifstm, ifexit, [...c.thenStatement.statements]);
265
+ }
266
+ else {
267
+ this.walkAST(ifstm, ifexit, [c.thenStatement]);
268
+ }
269
+ if (c.elseStatement) {
270
+ if (ts.isBlock(c.elseStatement)) {
271
+ this.walkAST(ifstm, ifexit, [...c.elseStatement.statements]);
272
+ }
273
+ else {
274
+ this.walkAST(ifstm, ifexit, [c.elseStatement]);
275
+ }
276
+ }
277
+ if (!ifstm.nextT) {
278
+ ifstm.nextT = ifexit;
279
+ ifexit.lasts.add(ifstm);
280
+ }
281
+ if (!ifstm.nextF) {
282
+ ifstm.nextF = ifexit;
283
+ ifexit.lasts.add(ifstm);
284
+ }
285
+ lastStatement = ifexit;
286
+ }
287
+ else if (ts.isWhileStatement(c)) {
288
+ this.breakin = 'loop';
289
+ let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scope.id);
290
+ this.loopStack.push(loopstm);
291
+ judgeLastType(loopstm);
292
+ let loopExit = new StatementBuilder('loopExit', '', c, scope.id);
293
+ this.exits.push(loopExit);
294
+ loopstm.nextF = loopExit;
295
+ loopExit.lasts.add(loopstm);
296
+ loopstm.condition = c.expression.getText(this.sourceFile);
297
+ loopstm.code = 'while (' + loopstm.condition + ')';
298
+ if (ts.isBlock(c.statement)) {
299
+ this.walkAST(loopstm, loopstm, [...c.statement.statements]);
300
+ }
301
+ else {
302
+ this.walkAST(loopstm, loopstm, [c.statement]);
303
+ }
304
+ if (!loopstm.nextF) {
305
+ loopstm.nextF = loopExit;
306
+ loopExit.lasts.add(loopstm);
307
+ }
308
+ if (!loopstm.nextT) {
309
+ loopstm.nextT = loopExit;
310
+ loopExit.lasts.add(loopstm);
311
+ }
312
+ lastStatement = loopExit;
313
+ this.loopStack.pop();
314
+ }
315
+ if (ts.isForStatement(c) || ts.isForInStatement(c) || ts.isForOfStatement(c)) {
316
+ this.breakin = 'loop';
317
+ let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scope.id);
318
+ this.loopStack.push(loopstm);
319
+ judgeLastType(loopstm);
320
+ let loopExit = new StatementBuilder('loopExit', '', c, scope.id);
321
+ this.exits.push(loopExit);
322
+ loopstm.nextF = loopExit;
323
+ loopExit.lasts.add(loopstm);
324
+ loopstm.code = 'for (';
325
+ if (ts.isForStatement(c)) {
326
+ loopstm.code += ((_a = c.initializer) === null || _a === void 0 ? void 0 : _a.getText(this.sourceFile)) + '; ' + ((_b = c.condition) === null || _b === void 0 ? void 0 : _b.getText(this.sourceFile)) + '; ' + ((_c = c.incrementor) === null || _c === void 0 ? void 0 : _c.getText(this.sourceFile));
327
+ }
328
+ else if (ts.isForOfStatement(c)) {
329
+ loopstm.code += ((_d = c.initializer) === null || _d === void 0 ? void 0 : _d.getText(this.sourceFile)) + ' of ' + c.expression.getText(this.sourceFile);
330
+ }
331
+ else {
332
+ loopstm.code += ((_e = c.initializer) === null || _e === void 0 ? void 0 : _e.getText(this.sourceFile)) + ' in ' + c.expression.getText(this.sourceFile);
333
+ }
334
+ loopstm.code += ')';
335
+ if (ts.isBlock(c.statement)) {
336
+ this.walkAST(loopstm, loopstm, [...c.statement.statements]);
337
+ }
338
+ else {
339
+ this.walkAST(loopstm, loopstm, [c.statement]);
340
+ }
341
+ if (!loopstm.nextF) {
342
+ loopstm.nextF = loopExit;
343
+ loopExit.lasts.add(loopstm);
344
+ }
345
+ if (!loopstm.nextT) {
346
+ loopstm.nextT = loopExit;
347
+ loopExit.lasts.add(loopstm);
348
+ }
349
+ lastStatement = loopExit;
350
+ this.loopStack.pop();
351
+ }
352
+ else if (ts.isDoStatement(c)) {
353
+ this.breakin = 'loop';
354
+ let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scope.id);
355
+ this.loopStack.push(loopstm);
356
+ let loopExit = new StatementBuilder('loopExit', '', c, scope.id);
357
+ this.exits.push(loopExit);
358
+ loopstm.nextF = loopExit;
359
+ loopExit.lasts.add(loopstm);
360
+ loopstm.condition = c.expression.getText(this.sourceFile);
361
+ loopstm.code = 'while (' + loopstm.condition + ')';
362
+ loopstm.isDoWhile = true;
363
+ if (ts.isBlock(c.statement)) {
364
+ this.walkAST(lastStatement, loopstm, [...c.statement.statements]);
365
+ }
366
+ else {
367
+ this.walkAST(lastStatement, loopstm, [c.statement]);
368
+ }
369
+ let lastType = lastStatement.type;
370
+ if (lastType == 'ifStatement' || lastType == 'loopStatement') {
371
+ let lastCondition = lastStatement;
372
+ loopstm.nextT = lastCondition.nextT;
373
+ (_f = lastCondition.nextT) === null || _f === void 0 ? void 0 : _f.lasts.add(loopstm);
374
+ }
375
+ else {
376
+ loopstm.nextT = lastStatement.next;
377
+ (_g = lastStatement.next) === null || _g === void 0 ? void 0 : _g.lasts.add(loopstm);
378
+ }
379
+ if (loopstm.nextT && loopstm.nextT != loopstm) {
380
+ loopstm.nextT.isDoWhile = true;
381
+ loopstm.doStatement = loopstm.nextT;
382
+ }
383
+ lastStatement = loopExit;
384
+ this.loopStack.pop();
385
+ }
386
+ else if (ts.isSwitchStatement(c)) {
387
+ this.breakin = 'switch';
388
+ let switchstm = new SwitchStatementBuilder('switchStatement', '', c, scope.id);
389
+ judgeLastType(switchstm);
390
+ let switchExit = new StatementBuilder('switchExit', '', null, scope.id);
391
+ this.exits.push(switchExit);
392
+ this.switchExitStack.push(switchExit);
393
+ switchstm.code = 'switch (' + c.expression + ')';
394
+ let lastCaseExit = null;
395
+ for (let i = 0; i < c.caseBlock.clauses.length; i++) {
396
+ const clause = c.caseBlock.clauses[i];
397
+ let casestm;
398
+ if (ts.isCaseClause(clause)) {
399
+ casestm = new StatementBuilder('statement', 'case ' + clause.expression.getText(this.sourceFile) + ':', clause, scope.id);
400
+ }
401
+ else {
402
+ casestm = new StatementBuilder('statement', 'default:', clause, scope.id);
403
+ }
404
+ switchstm.nexts.push(casestm);
405
+ casestm.lasts.add(switchstm);
406
+ let caseExit = new StatementBuilder('caseExit', '', null, scope.id);
407
+ this.exits.push(caseExit);
408
+ this.walkAST(casestm, caseExit, [...clause.statements]);
409
+ if (ts.isCaseClause(clause)) {
410
+ const cas = new Case(casestm.code, casestm.next);
411
+ switchstm.cases.push(cas);
412
+ }
413
+ else {
414
+ switchstm.default = casestm.next;
415
+ }
416
+ // case: 之类的代码不会被三地址码识别,可能会导致空block,暂时删除
417
+ switchstm.nexts[switchstm.nexts.length - 1] = casestm.next;
418
+ for (const stmt of [...casestm.lasts]) {
419
+ casestm.next.lasts.add(stmt);
420
+ }
421
+ casestm.next.lasts.delete(casestm);
422
+ if (lastCaseExit) {
423
+ lastCaseExit.next = casestm.next;
424
+ (_h = casestm.next) === null || _h === void 0 ? void 0 : _h.lasts.add(lastCaseExit);
425
+ }
426
+ lastCaseExit = caseExit;
427
+ if (i == c.caseBlock.clauses.length - 1) {
428
+ caseExit.next = switchExit;
429
+ switchExit.lasts.add(caseExit);
430
+ }
431
+ }
432
+ lastStatement = switchExit;
433
+ this.switchExitStack.pop();
434
+ }
435
+ else if (ts.isBlock(c)) {
436
+ let blockExit = new StatementBuilder('blockExit', '', c, scope.id);
437
+ this.exits.push(blockExit);
438
+ this.walkAST(lastStatement, blockExit, c.getChildren(this.sourceFile)[1].getChildren(this.sourceFile));
439
+ lastStatement = blockExit;
440
+ }
441
+ else if (ts.isTryStatement(c)) {
442
+ let trystm = new TryStatementBuilder('tryStatement', 'try', c, scope.id);
443
+ judgeLastType(trystm);
444
+ let tryExit = new StatementBuilder('tryExit', '', c, scope.id);
445
+ this.exits.push(tryExit);
446
+ trystm.tryExit = tryExit;
447
+ this.walkAST(trystm, tryExit, [...c.tryBlock.statements]);
448
+ trystm.tryFirst = trystm.next;
449
+ (_j = trystm.next) === null || _j === void 0 ? void 0 : _j.lasts.add(trystm);
450
+ if (c.catchClause) {
451
+ let text = 'catch';
452
+ if (c.catchClause.variableDeclaration) {
453
+ text += '(' + c.catchClause.variableDeclaration.getText(this.sourceFile) + ')';
454
+ }
455
+ let catchOrNot = new ConditionStatementBuilder('catchOrNot', text, c, scope.id);
456
+ let catchExit = new StatementBuilder('catch exit', '', c, scope.id);
457
+ catchOrNot.nextF = catchExit;
458
+ catchExit.lasts.add(catchOrNot);
459
+ this.walkAST(catchOrNot, catchExit, [...c.catchClause.block.statements]);
460
+ if (!catchOrNot.nextT) {
461
+ catchOrNot.nextT = catchExit;
462
+ catchExit.lasts.add(catchOrNot);
463
+ }
464
+ const catchStatement = new StatementBuilder('statement', catchOrNot.code, c.catchClause, catchOrNot.nextT.scopeID);
465
+ catchStatement.next = catchOrNot.nextT;
466
+ trystm.catchStatement = catchStatement;
467
+ catchStatement.lasts.add(trystm);
468
+ if (c.catchClause.variableDeclaration) {
469
+ trystm.catchError = c.catchClause.variableDeclaration.getText(this.sourceFile);
470
+ }
471
+ else {
472
+ trystm.catchError = 'Error';
473
+ }
474
+ }
475
+ if (c.finallyBlock && c.finallyBlock.statements.length > 0) {
476
+ let final = new StatementBuilder('statement', 'finally', c, scope.id);
477
+ let finalExit = new StatementBuilder('finallyExit', '', c, scope.id);
478
+ this.exits.push(finalExit);
479
+ this.walkAST(final, finalExit, [...c.finallyBlock.statements]);
480
+ trystm.finallyStatement = final.next;
481
+ tryExit.next = final;
482
+ (_k = final.next) === null || _k === void 0 ? void 0 : _k.lasts.add(tryExit);
483
+ lastStatement = finalExit;
484
+ }
485
+ else {
486
+ lastStatement = tryExit;
487
+ }
488
+ }
489
+ }
490
+ this.scopeLevel--;
491
+ if (lastStatement.type != 'breakStatement' && lastStatement.type != 'continueStatement' && lastStatement.type != 'returnStatement') {
492
+ lastStatement.next = nextStatement;
493
+ nextStatement.lasts.add(lastStatement);
494
+ }
495
+ }
496
+ addReturnInEmptyMethod() {
497
+ if (this.entry.next == this.exit) {
498
+ const ret = new StatementBuilder('returnStatement', 'return;', null, this.entry.scopeID);
499
+ this.entry.next = ret;
500
+ ret.lasts.add(this.entry);
501
+ ret.next = this.exit;
502
+ this.exit.lasts = new Set([ret]);
503
+ }
504
+ }
505
+ deleteExit() {
506
+ for (const exit of this.exits) {
507
+ for (const last of [...exit.lasts]) {
508
+ if (last instanceof ConditionStatementBuilder) {
509
+ if (last.nextT == exit) {
510
+ last.nextT = exit.next;
511
+ const lasts = exit.next.lasts;
512
+ lasts.delete(exit);
513
+ lasts.add(last);
514
+ }
515
+ else if (last.nextF == exit) {
516
+ last.nextF = exit.next;
517
+ const lasts = exit.next.lasts;
518
+ lasts.delete(exit);
519
+ lasts.add(last);
520
+ }
521
+ }
522
+ else if (last instanceof SwitchStatementBuilder) {
523
+ for (let i = 0; i < last.nexts.length; i++) {
524
+ const stmt = last.nexts[i];
525
+ if (stmt == exit) {
526
+ last.nexts[i] = exit.next;
527
+ const lasts = exit.next.lasts;
528
+ lasts.delete(exit);
529
+ lasts.add(last);
530
+ }
531
+ }
532
+ }
533
+ else {
534
+ last.next = exit.next;
535
+ const lasts = exit.next.lasts;
536
+ lasts.delete(exit);
537
+ lasts.add(last);
538
+ }
539
+ }
540
+ }
541
+ // 部分语句例如return后面的exit语句的next无法在上面清除
542
+ for (const exit of this.exits) {
543
+ if (exit.next && exit.next.lasts.has(exit)) {
544
+ exit.next.lasts.delete(exit);
545
+ }
546
+ }
547
+ }
548
+ buildBlocks() {
549
+ const stmtQueue = [this.entry];
550
+ const handledStmts = new Set();
551
+ while (stmtQueue.length > 0) {
552
+ let stmt = stmtQueue.pop();
553
+ if (stmt.type.includes('exit')) {
554
+ continue;
555
+ }
556
+ if (handledStmts.has(stmt)) {
557
+ continue;
558
+ }
559
+ const block = new Block(this.blocks.length, []);
560
+ this.blocks.push(block);
561
+ while (stmt && !handledStmts.has(stmt)) {
562
+ if (stmt.type == 'loopStatement' && block.stmts.length > 0 && !stmt.isDoWhile) {
563
+ stmtQueue.push(stmt);
564
+ break;
565
+ }
566
+ if (stmt.type.includes('Exit')) {
567
+ break;
568
+ }
569
+ block.stmts.push(stmt);
570
+ stmt.block = block;
571
+ handledStmts.add(stmt);
572
+ if (stmt instanceof ConditionStatementBuilder) {
573
+ if (!handledStmts.has(stmt.nextF)) {
574
+ stmtQueue.push(stmt.nextF);
575
+ }
576
+ if (!handledStmts.has(stmt.nextT)) {
577
+ stmtQueue.push(stmt.nextT);
578
+ }
579
+ break;
580
+ }
581
+ else if (stmt instanceof SwitchStatementBuilder) {
582
+ for (let i = stmt.nexts.length - 1; i >= 0; i--) {
583
+ stmtQueue.push(stmt.nexts[i]);
584
+ }
585
+ break;
586
+ }
587
+ else if (stmt instanceof TryStatementBuilder) {
588
+ if (stmt.finallyStatement) {
589
+ stmtQueue.push(stmt.finallyStatement);
590
+ }
591
+ if (stmt.catchStatement) {
592
+ stmtQueue.push(stmt.catchStatement);
593
+ }
594
+ if (stmt.tryFirst) {
595
+ stmt = stmt.tryFirst;
596
+ continue;
597
+ }
598
+ break;
599
+ }
600
+ else {
601
+ if (stmt.next) {
602
+ if ((stmt.type == 'continueStatement' || stmt.next.type == 'loopStatement') && stmt.next.block) {
603
+ break;
604
+ }
605
+ if (stmt.next.type.includes('exit')) {
606
+ break;
607
+ }
608
+ stmt.next.passTmies++;
609
+ if (stmt.next.passTmies == stmt.next.lasts.size || (stmt.next.type == 'loopStatement') || stmt.next.isDoWhile) {
610
+ if (stmt.next.scopeID != stmt.scopeID && !(stmt.next instanceof ConditionStatementBuilder && stmt.next.doStatement)
611
+ && !(ts.isCaseClause(stmt.astNode) || ts.isDefaultClause(stmt.astNode))) {
612
+ stmtQueue.push(stmt.next);
613
+ break;
614
+ }
615
+ stmt = stmt.next;
616
+ }
617
+ }
618
+ }
619
+ }
620
+ }
621
+ }
622
+ buildBlocksNextLast() {
623
+ var _a, _b, _c, _d, _e, _f;
624
+ for (let block of this.blocks) {
625
+ for (let originStatement of block.stmts) {
626
+ let lastStatement = (block.stmts.indexOf(originStatement) == block.stmts.length - 1);
627
+ if (originStatement instanceof ConditionStatementBuilder) {
628
+ let nextT = (_a = originStatement.nextT) === null || _a === void 0 ? void 0 : _a.block;
629
+ if (nextT && (lastStatement || nextT != block) && !((_b = originStatement.nextT) === null || _b === void 0 ? void 0 : _b.type.includes(' exit'))) {
630
+ block.nexts.push(nextT);
631
+ nextT.lasts.push(block);
632
+ }
633
+ let nextF = (_c = originStatement.nextF) === null || _c === void 0 ? void 0 : _c.block;
634
+ if (nextF && (lastStatement || nextF != block) && !((_d = originStatement.nextF) === null || _d === void 0 ? void 0 : _d.type.includes(' exit'))) {
635
+ block.nexts.push(nextF);
636
+ nextF.lasts.push(block);
637
+ }
638
+ }
639
+ else if (originStatement instanceof SwitchStatementBuilder) {
640
+ for (const next of originStatement.nexts) {
641
+ const nextBlock = next.block;
642
+ if (nextBlock && (lastStatement || nextBlock != block)) {
643
+ block.nexts.push(nextBlock);
644
+ nextBlock.lasts.push(block);
645
+ }
646
+ }
647
+ }
648
+ else {
649
+ let next = (_e = originStatement.next) === null || _e === void 0 ? void 0 : _e.block;
650
+ if (next && (lastStatement || next != block) && !((_f = originStatement.next) === null || _f === void 0 ? void 0 : _f.type.includes(' exit'))) {
651
+ block.nexts.push(next);
652
+ next.lasts.push(block);
653
+ }
654
+ }
655
+ }
656
+ }
657
+ }
658
+ addReturnBlock() {
659
+ var _a, _b, _c, _d;
660
+ let notReturnStmts = [];
661
+ for (let stmt of [...this.exit.lasts]) {
662
+ if (stmt.type != 'returnStatement') {
663
+ notReturnStmts.push(stmt);
664
+ }
665
+ }
666
+ if (notReturnStmts.length < 1) {
667
+ return;
668
+ }
669
+ const returnStatement = new StatementBuilder('returnStatement', 'return;', null, this.exit.scopeID);
670
+ let tryExit = false;
671
+ if (notReturnStmts.length == 1 && notReturnStmts[0].block) {
672
+ for (const stmt of notReturnStmts[0].block.stmts) {
673
+ if (stmt instanceof TryStatementBuilder) {
674
+ tryExit = true;
675
+ break;
676
+ }
677
+ }
678
+ }
679
+ if (notReturnStmts.length == 1 && !(notReturnStmts[0] instanceof ConditionStatementBuilder) && !tryExit) {
680
+ const notReturnStmt = notReturnStmts[0];
681
+ notReturnStmt.next = returnStatement;
682
+ returnStatement.lasts = new Set([notReturnStmt]);
683
+ returnStatement.next = this.exit;
684
+ const lasts = [...this.exit.lasts];
685
+ lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
686
+ this.exit.lasts = new Set(lasts);
687
+ (_a = notReturnStmt.block) === null || _a === void 0 ? void 0 : _a.stmts.push(returnStatement);
688
+ returnStatement.block = notReturnStmt.block;
689
+ }
690
+ else {
691
+ let returnBlock = new Block(this.blocks.length, [returnStatement]);
692
+ returnStatement.block = returnBlock;
693
+ this.blocks.push(returnBlock);
694
+ for (const notReturnStmt of notReturnStmts) {
695
+ if (notReturnStmt instanceof ConditionStatementBuilder) {
696
+ if (this.exit == notReturnStmt.nextT) {
697
+ notReturnStmt.nextT = returnStatement;
698
+ (_b = notReturnStmt.block) === null || _b === void 0 ? void 0 : _b.nexts.splice(0, 0, returnBlock);
699
+ }
700
+ else if (this.exit == notReturnStmt.nextF) {
701
+ notReturnStmt.nextF = returnStatement;
702
+ (_c = notReturnStmt.block) === null || _c === void 0 ? void 0 : _c.nexts.push(returnBlock);
703
+ }
704
+ }
705
+ else {
706
+ notReturnStmt.next = returnStatement;
707
+ (_d = notReturnStmt.block) === null || _d === void 0 ? void 0 : _d.nexts.push(returnBlock);
708
+ }
709
+ returnStatement.lasts.add(notReturnStmt);
710
+ returnStatement.next = this.exit;
711
+ const lasts = [...this.exit.lasts];
712
+ lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
713
+ this.exit.lasts = new Set(lasts);
714
+ returnBlock.lasts.push(notReturnStmt.block);
715
+ }
716
+ }
717
+ }
718
+ resetWalked() {
719
+ for (let stmt of this.statementArray) {
720
+ stmt.walked = false;
721
+ }
722
+ }
723
+ addStmtBuilderPosition() {
724
+ for (const stmt of this.statementArray) {
725
+ if (stmt.astNode) {
726
+ const { line, character } = ts.getLineAndCharacterOfPosition(this.sourceFile, stmt.astNode.getStart(this.sourceFile));
727
+ stmt.line = line + 1;
728
+ stmt.column = character + 1;
729
+ }
730
+ }
731
+ }
732
+ CfgBuilder2Array(stmt) {
733
+ if (stmt.walked)
734
+ return;
735
+ stmt.walked = true;
736
+ stmt.index = this.statementArray.length;
737
+ if (!stmt.type.includes(' exit'))
738
+ this.statementArray.push(stmt);
739
+ if (stmt.type == 'ifStatement' || stmt.type == 'loopStatement' || stmt.type == 'catchOrNot') {
740
+ let cstm = stmt;
741
+ if (cstm.nextT == null || cstm.nextF == null) {
742
+ this.errorTest(cstm);
743
+ return;
744
+ }
745
+ this.CfgBuilder2Array(cstm.nextF);
746
+ this.CfgBuilder2Array(cstm.nextT);
747
+ }
748
+ else if (stmt.type == 'switchStatement') {
749
+ let sstm = stmt;
750
+ for (let ss of sstm.nexts) {
751
+ this.CfgBuilder2Array(ss);
752
+ }
753
+ }
754
+ else if (stmt.type == 'tryStatement') {
755
+ let trystm = stmt;
756
+ if (trystm.tryFirst) {
757
+ this.CfgBuilder2Array(trystm.tryFirst);
758
+ }
759
+ if (trystm.catchStatement) {
760
+ this.CfgBuilder2Array(trystm.catchStatement);
761
+ }
762
+ if (trystm.finallyStatement) {
763
+ this.CfgBuilder2Array(trystm.finallyStatement);
764
+ }
765
+ if (trystm.next) {
766
+ this.CfgBuilder2Array(trystm.next);
767
+ }
768
+ }
769
+ else {
770
+ if (stmt.next != null)
771
+ this.CfgBuilder2Array(stmt.next);
772
+ }
773
+ }
774
+ getDotEdges(stmt) {
775
+ if (this.statementArray.length == 0)
776
+ this.CfgBuilder2Array(this.entry);
777
+ if (stmt.walked)
778
+ return;
779
+ stmt.walked = true;
780
+ if (stmt.type == 'ifStatement' || stmt.type == 'loopStatement' || stmt.type == 'catchOrNot') {
781
+ let cstm = stmt;
782
+ if (cstm.nextT == null || cstm.nextF == null) {
783
+ this.errorTest(cstm);
784
+ return;
785
+ }
786
+ let edge = [cstm.index, cstm.nextF.index];
787
+ this.dotEdges.push(edge);
788
+ edge = [cstm.index, cstm.nextT.index];
789
+ this.dotEdges.push(edge);
790
+ this.getDotEdges(cstm.nextF);
791
+ this.getDotEdges(cstm.nextT);
792
+ }
793
+ else if (stmt.type == 'switchStatement') {
794
+ let sstm = stmt;
795
+ for (let ss of sstm.nexts) {
796
+ let edge = [sstm.index, ss.index];
797
+ this.dotEdges.push(edge);
798
+ this.getDotEdges(ss);
799
+ }
800
+ }
801
+ else {
802
+ if (stmt.next != null) {
803
+ let edge = [stmt.index, stmt.next.index];
804
+ this.dotEdges.push(edge);
805
+ this.getDotEdges(stmt.next);
806
+ }
807
+ }
808
+ }
809
+ errorTest(stmt) {
810
+ var _a, _b;
811
+ let mes = 'ifnext error ';
812
+ if ((_a = this.declaringClass) === null || _a === void 0 ? void 0 : _a.getDeclaringArkFile()) {
813
+ mes += ((_b = this.declaringClass) === null || _b === void 0 ? void 0 : _b.getDeclaringArkFile().getName()) + '.' + this.declaringClass.getName() + '.' + this.name;
814
+ }
815
+ mes += '\n' + stmt.code;
816
+ throw new textError(mes);
817
+ }
818
+ printBlocks() {
819
+ var _a, _b, _c, _d, _e, _f, _g;
820
+ let text = '';
821
+ if ((_a = this.declaringClass) === null || _a === void 0 ? void 0 : _a.getDeclaringArkFile()) {
822
+ text += this.declaringClass.getDeclaringArkFile().getName() + '\n';
823
+ }
824
+ for (let bi = 0; bi < this.blocks.length; bi++) {
825
+ let block = this.blocks[bi];
826
+ if (bi != 0)
827
+ text += 'label' + block.id + ':\n';
828
+ let length = block.stmts.length;
829
+ for (let i = 0; i < length; i++) {
830
+ let stmt = block.stmts[i];
831
+ if (stmt.type == 'ifStatement' || stmt.type == 'loopStatement' || stmt.type == 'catchOrNot') {
832
+ let cstm = stmt;
833
+ if (cstm.nextT == null || cstm.nextF == null) {
834
+ this.errorTest(cstm);
835
+ return text;
836
+ }
837
+ if (!cstm.nextF.block || !cstm.nextT.block) {
838
+ this.errorTest(cstm);
839
+ return text;
840
+ }
841
+ stmt.code = 'if !(' + cstm.condition + ') goto label' + cstm.nextF.block.id;
842
+ if (i == length - 1 && bi + 1 < this.blocks.length && this.blocks[bi + 1].id != cstm.nextT.block.id) {
843
+ let gotoStm = new StatementBuilder('gotoStatement', 'goto label' + cstm.nextT.block.id, null, block.stmts[0].scopeID);
844
+ block.stmts.push(gotoStm);
845
+ length++;
846
+ }
847
+ }
848
+ else if (stmt.type == 'breakStatement' || stmt.type == 'continueStatement') {
849
+ if (!((_b = stmt.next) === null || _b === void 0 ? void 0 : _b.block)) {
850
+ this.errorTest(stmt);
851
+ return text;
852
+ }
853
+ stmt.code = 'goto label' + ((_c = stmt.next) === null || _c === void 0 ? void 0 : _c.block.id);
854
+ }
855
+ else {
856
+ if (i == length - 1 && ((_d = stmt.next) === null || _d === void 0 ? void 0 : _d.block) && (bi + 1 < this.blocks.length && this.blocks[bi + 1].id != stmt.next.block.id || bi + 1 == this.blocks.length)) {
857
+ let gotoStm = new StatementBuilder('StatementBuilder', 'goto label' + ((_e = stmt.next) === null || _e === void 0 ? void 0 : _e.block.id), null, block.stmts[0].scopeID);
858
+ block.stmts.push(gotoStm);
859
+ length++;
860
+ }
861
+ }
862
+ if (stmt.addressCode3.length == 0) {
863
+ text += ' ' + stmt.code + '\n';
864
+ }
865
+ else {
866
+ for (let ac of stmt.addressCode3) {
867
+ if (ac.startsWith('if') || ac.startsWith('while')) {
868
+ let cstm = stmt;
869
+ let condition = ac.substring(ac.indexOf('('));
870
+ let goto = '';
871
+ if ((_f = cstm.nextF) === null || _f === void 0 ? void 0 : _f.block)
872
+ goto = 'if !' + condition + ' goto label' + ((_g = cstm.nextF) === null || _g === void 0 ? void 0 : _g.block.id);
873
+ stmt.addressCode3[stmt.addressCode3.indexOf(ac)] = goto;
874
+ text += ' ' + goto + '\n';
875
+ }
876
+ else
877
+ text += ' ' + ac + '\n';
878
+ }
879
+ }
880
+ }
881
+ }
882
+ for (let cat of this.catches) {
883
+ text += 'catch ' + cat.errorName + ' from label ' + cat.from + ' to label ' + cat.to + ' with label' + cat.withLabel + '\n';
884
+ }
885
+ return text;
886
+ }
887
+ buildStatementBuilder4ArrowFunction(stmt) {
888
+ let s = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
889
+ this.entry.next = s;
890
+ s.lasts = new Set([this.entry]);
891
+ s.next = this.exit;
892
+ this.exit.lasts = new Set([s]);
893
+ }
894
+ buildCfgBuilder() {
895
+ let stmts = [];
896
+ if (ts.isSourceFile(this.astRoot)) {
897
+ stmts = [...this.astRoot.statements];
898
+ }
899
+ else if (ts.isFunctionDeclaration(this.astRoot) || ts.isMethodDeclaration(this.astRoot) || ts.isConstructorDeclaration(this.astRoot)
900
+ || ts.isGetAccessorDeclaration(this.astRoot) || ts.isSetAccessorDeclaration(this.astRoot) || ts.isFunctionExpression(this.astRoot)) {
901
+ if (this.astRoot.body) {
902
+ stmts = [...this.astRoot.body.statements];
903
+ }
904
+ else {
905
+ this.emptyBody = true;
906
+ }
907
+ }
908
+ else if (ts.isArrowFunction(this.astRoot)) {
909
+ if (ts.isBlock(this.astRoot.body)) {
910
+ stmts = [...this.astRoot.body.statements];
911
+ }
912
+ }
913
+ else if (ts.isMethodSignature(this.astRoot) || ts.isConstructSignatureDeclaration(this.astRoot)
914
+ || ts.isCallSignatureDeclaration(this.astRoot) || ts.isFunctionTypeNode(this.astRoot)) {
915
+ this.emptyBody = true;
916
+ }
917
+ else if (ts.isModuleDeclaration(this.astRoot) && ts.isModuleBlock(this.astRoot.body)) {
918
+ stmts = [...this.astRoot.body.statements];
919
+ }
920
+ if (!ModelUtils_1.ModelUtils.isArkUIBuilderMethod(this.declaringMethod)) {
921
+ this.walkAST(this.entry, this.exit, stmts);
922
+ }
923
+ else {
924
+ this.handleBuilder(stmts);
925
+ }
926
+ if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
927
+ this.buildStatementBuilder4ArrowFunction(this.astRoot.body);
928
+ }
929
+ this.addReturnInEmptyMethod();
930
+ this.deleteExit();
931
+ this.CfgBuilder2Array(this.entry);
932
+ this.addStmtBuilderPosition();
933
+ this.buildBlocks();
934
+ this.blocks = this.blocks.filter((b) => b.stmts.length != 0);
935
+ this.buildBlocksNextLast();
936
+ this.addReturnBlock();
937
+ }
938
+ handleBuilder(stmts) {
939
+ let lastStmt = this.entry;
940
+ for (const stmt of stmts) {
941
+ const stmtBuilder = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
942
+ lastStmt.next = stmtBuilder;
943
+ stmtBuilder.lasts.add(lastStmt);
944
+ lastStmt = stmtBuilder;
945
+ }
946
+ lastStmt.next = this.exit;
947
+ this.exit.lasts.add(lastStmt);
948
+ }
949
+ isBodyEmpty() {
950
+ return this.emptyBody;
951
+ }
952
+ buildCfgAndOriginalCfg() {
953
+ if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
954
+ return this.buildCfgAndOriginalCfgForSimpleArrowFunction();
955
+ }
956
+ return this.buildNormalCfgAndOriginalCfg();
957
+ }
958
+ buildCfgAndOriginalCfgForSimpleArrowFunction() {
959
+ const stmts = [];
960
+ const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
961
+ stmts.push(...arkIRTransformer.prebuildStmts());
962
+ const expressionBodyNode = this.astRoot.body;
963
+ const expressionBodyStmts = [];
964
+ let { value: expressionBodyValue, valueOriginalPositions: expressionBodyPositions, stmts: tempStmts, } = arkIRTransformer.tsNodeToValueAndStmts(expressionBodyNode);
965
+ expressionBodyStmts.push(...tempStmts);
966
+ if (IRUtils_1.IRUtils.moreThanOneAddress(expressionBodyValue)) {
967
+ ({
968
+ value: expressionBodyValue,
969
+ valueOriginalPositions: expressionBodyPositions,
970
+ stmts: tempStmts,
971
+ } = arkIRTransformer.generateAssignStmtForValue(expressionBodyValue, expressionBodyPositions));
972
+ expressionBodyStmts.push(...tempStmts);
973
+ }
974
+ const returnStmt = new Stmt_1.ArkReturnStmt(expressionBodyValue);
975
+ returnStmt.setOperandOriginalPositions([expressionBodyPositions[0], ...expressionBodyPositions]);
976
+ expressionBodyStmts.push(returnStmt);
977
+ arkIRTransformer.mapStmtsToTsStmt(expressionBodyStmts, expressionBodyNode);
978
+ stmts.push(...expressionBodyStmts);
979
+ const stmtToOriginalStmt = arkIRTransformer.getStmtToOriginalStmt();
980
+ const cfg = new Cfg_1.Cfg();
981
+ const blockInCfg = new BasicBlock_1.BasicBlock();
982
+ blockInCfg.setId(0);
983
+ stmts.forEach(stmt => {
984
+ blockInCfg.addStmt(stmt);
985
+ stmt.setCfg(cfg);
986
+ });
987
+ cfg.addBlock(blockInCfg);
988
+ cfg.setStartingStmt(stmts[0]);
989
+ const originalCfg = new Cfg_1.Cfg();
990
+ const blockInOriginalCfg = new BasicBlock_1.BasicBlock();
991
+ blockInOriginalCfg.setId(0);
992
+ originalCfg.addBlock(blockInOriginalCfg);
993
+ originalCfg.setStartingStmt(stmtToOriginalStmt.get(stmts[0]));
994
+ return {
995
+ cfg: cfg,
996
+ originalCfg: originalCfg,
997
+ stmtToOriginalStmt: stmtToOriginalStmt,
998
+ locals: arkIRTransformer.getLocals(),
999
+ aliasTypeMap: arkIRTransformer.getAliasTypeMap()
1000
+ };
1001
+ }
1002
+ buildNormalCfgAndOriginalCfg() {
1003
+ const cfg = new Cfg_1.Cfg();
1004
+ const blockBuilderToCfgBlock = new Map();
1005
+ let isStartingStmtInCfgBlock = true;
1006
+ const stmtsInOriginalCfg = [];
1007
+ const stmtSetInOriginalCfg = new Set();
1008
+ const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
1009
+ const stmtToOriginalStmt = arkIRTransformer.getStmtToOriginalStmt();
1010
+ const blocksContainLoopCondition = new Set();
1011
+ for (let i = 0; i < this.blocks.length; i++) {
1012
+ // build block in Cfg
1013
+ const stmtsInBlock = [];
1014
+ if (i === 0) {
1015
+ stmtsInBlock.push(...arkIRTransformer.prebuildStmts());
1016
+ }
1017
+ for (const statementBuilder of this.blocks[i].stmts) {
1018
+ if (statementBuilder.type == 'loopStatement') {
1019
+ blocksContainLoopCondition.add(this.blocks[i]);
1020
+ }
1021
+ if (statementBuilder.astNode && statementBuilder.code != '') {
1022
+ stmtsInBlock.push(...arkIRTransformer.tsNodeToStmts(statementBuilder.astNode));
1023
+ }
1024
+ else if (statementBuilder.code.startsWith('return')) {
1025
+ stmtsInBlock.push(new Stmt_1.ArkReturnVoidStmt());
1026
+ }
1027
+ }
1028
+ const blockInCfg = new BasicBlock_1.BasicBlock();
1029
+ blockInCfg.setId(this.blocks[i].id);
1030
+ for (const stmt of stmtsInBlock) {
1031
+ if (isStartingStmtInCfgBlock) {
1032
+ isStartingStmtInCfgBlock = false;
1033
+ cfg.setStartingStmt(stmt);
1034
+ }
1035
+ blockInCfg.addStmt(stmt);
1036
+ }
1037
+ cfg.addBlock(blockInCfg);
1038
+ blockBuilderToCfgBlock.set(this.blocks[i], blockInCfg);
1039
+ // collect stmts in OriginalCfg
1040
+ for (const stmt of stmtsInBlock) {
1041
+ const originalStmt = stmtToOriginalStmt.get(stmt);
1042
+ if (originalStmt) {
1043
+ if (!stmtSetInOriginalCfg.has(originalStmt)) {
1044
+ stmtSetInOriginalCfg.add(originalStmt);
1045
+ stmtsInOriginalCfg.push(originalStmt);
1046
+ }
1047
+ }
1048
+ }
1049
+ }
1050
+ let currBlockId = this.blocks.length;
1051
+ // link blocks
1052
+ for (const [blockBuilder, cfgBlock] of blockBuilderToCfgBlock) {
1053
+ for (const successorBlockBuilder of blockBuilder.nexts) {
1054
+ if (!blockBuilderToCfgBlock.get(successorBlockBuilder)) {
1055
+ continue;
1056
+ }
1057
+ const successorBlock = blockBuilderToCfgBlock.get(successorBlockBuilder);
1058
+ cfgBlock.addSuccessorBlock(successorBlock);
1059
+ }
1060
+ for (const predecessorBlockBuilder of blockBuilder.lasts) {
1061
+ if (!blockBuilderToCfgBlock.get(predecessorBlockBuilder)) {
1062
+ continue;
1063
+ }
1064
+ const predecessorBlock = blockBuilderToCfgBlock.get(predecessorBlockBuilder);
1065
+ cfgBlock.addPredecessorBlock(predecessorBlock);
1066
+ }
1067
+ }
1068
+ // put statements within loop in right position
1069
+ for (const blockBuilder of blocksContainLoopCondition) {
1070
+ if (!blockBuilderToCfgBlock.get(blockBuilder)) {
1071
+ continue;
1072
+ }
1073
+ const block = blockBuilderToCfgBlock.get(blockBuilder);
1074
+ const blockId = block.getId();
1075
+ const stmts = block.getStmts();
1076
+ const stmtsCnt = stmts.length;
1077
+ let ifStmtIdx = -1;
1078
+ let iteratorNextStmtIdx = -1;
1079
+ let dummyInitializerStmtIdx = -1;
1080
+ for (let i = 0; i < stmtsCnt; i++) {
1081
+ const stmt = stmts[i];
1082
+ if (stmt instanceof Stmt_1.ArkAssignStmt && stmt.getRightOp() instanceof Expr_1.AbstractInvokeExpr) {
1083
+ const invokeExpr = stmt.getRightOp();
1084
+ if (invokeExpr.getMethodSignature().getMethodSubSignature().getMethodName() == Builtin_1.Builtin.ITERATOR_NEXT) {
1085
+ iteratorNextStmtIdx = i;
1086
+ continue;
1087
+ }
1088
+ }
1089
+ if (stmt.toString() == ArkIRTransformer_1.DUMMY_INITIALIZER_STMT) {
1090
+ dummyInitializerStmtIdx = i;
1091
+ continue;
1092
+ }
1093
+ if (stmt instanceof Stmt_1.ArkIfStmt) {
1094
+ ifStmtIdx = i;
1095
+ break;
1096
+ }
1097
+ }
1098
+ if (iteratorNextStmtIdx != -1 || dummyInitializerStmtIdx != -1) {
1099
+ // put statements into block before condition
1100
+ const lastStmtIdxBeforeCondition = iteratorNextStmtIdx != -1 ? iteratorNextStmtIdx : dummyInitializerStmtIdx;
1101
+ const stmtsInsertBeforeCondition = stmts.slice(0, lastStmtIdxBeforeCondition);
1102
+ let prevBlockBuilderContainsLoop = false;
1103
+ for (const prevBlockBuilder of blockBuilder.lasts) {
1104
+ if (prevBlockBuilder.id < blockId && blocksContainLoopCondition.has(prevBlockBuilder)) {
1105
+ prevBlockBuilderContainsLoop = true;
1106
+ break;
1107
+ }
1108
+ }
1109
+ if (prevBlockBuilderContainsLoop) {
1110
+ // should create an extra block when previous block contains loop condition
1111
+ this.insertBeforeConditionBlockBuilder(blockBuilderToCfgBlock, blockBuilder, stmtsInsertBeforeCondition, false, cfg);
1112
+ }
1113
+ else {
1114
+ const blockBuilderBeforeCondition = blockBuilder.lasts[0];
1115
+ const blockBeforeCondition = blockBuilderToCfgBlock.get(blockBuilderBeforeCondition);
1116
+ blockBeforeCondition === null || blockBeforeCondition === void 0 ? void 0 : blockBeforeCondition.getStmts().push(...stmtsInsertBeforeCondition);
1117
+ }
1118
+ if (dummyInitializerStmtIdx != -1 && ifStmtIdx != stmtsCnt - 1) {
1119
+ // put incrementor statements into block which reenters condition
1120
+ const stmtsReenterCondition = stmts.slice(ifStmtIdx + 1);
1121
+ const blockBuildersReenterCondition = [];
1122
+ for (const prevBlockBuilder of blockBuilder.lasts) {
1123
+ const prevBlock = blockBuilderToCfgBlock.get(prevBlockBuilder);
1124
+ if (prevBlock.getId() > blockId) {
1125
+ blockBuildersReenterCondition.push(prevBlockBuilder);
1126
+ }
1127
+ }
1128
+ if (blockBuildersReenterCondition.length > 1 || blocksContainLoopCondition.has(blockBuildersReenterCondition[0])) {
1129
+ // put incrementor statements into an extra block
1130
+ this.insertBeforeConditionBlockBuilder(blockBuilderToCfgBlock, blockBuilder, stmtsReenterCondition, true, cfg);
1131
+ }
1132
+ else {
1133
+ // put incrementor statements into prev reenter block
1134
+ const blockReenterCondition = blockBuilderToCfgBlock.get(blockBuildersReenterCondition[0]);
1135
+ blockReenterCondition === null || blockReenterCondition === void 0 ? void 0 : blockReenterCondition.getStmts().push(...stmtsReenterCondition);
1136
+ }
1137
+ }
1138
+ else if (iteratorNextStmtIdx != -1) {
1139
+ // put statements which get value of iterator into block after condition
1140
+ const blockBuilderAfterCondition = blockBuilder.nexts[0];
1141
+ const blockAfterCondition = blockBuilderToCfgBlock.get(blockBuilderAfterCondition);
1142
+ const stmtsAfterCondition = stmts.slice(ifStmtIdx + 1);
1143
+ blockAfterCondition === null || blockAfterCondition === void 0 ? void 0 : blockAfterCondition.getStmts().splice(0, 0, ...stmtsAfterCondition);
1144
+ }
1145
+ // remove statements which should not in condition
1146
+ const firstStmtIdxInCondition = iteratorNextStmtIdx != -1 ? iteratorNextStmtIdx : dummyInitializerStmtIdx + 1;
1147
+ stmts.splice(0, firstStmtIdxInCondition);
1148
+ stmts.splice(ifStmtIdx - firstStmtIdxInCondition + 1);
1149
+ }
1150
+ }
1151
+ for (const blockBuilder of this.blocks) {
1152
+ if (blockBuilder.id == -1) {
1153
+ blockBuilder.id = currBlockId++;
1154
+ const block = blockBuilderToCfgBlock.get(blockBuilder);
1155
+ block.setId(blockBuilder.id);
1156
+ }
1157
+ }
1158
+ for (const stmt of cfg.getStmts()) {
1159
+ stmt.setCfg(cfg);
1160
+ }
1161
+ const originalCfg = new Cfg_1.Cfg();
1162
+ const originalCfgBlock = new BasicBlock_1.BasicBlock();
1163
+ for (let i = 0; i < stmtsInOriginalCfg.length; i++) {
1164
+ if (i === 0) {
1165
+ originalCfg.setStartingStmt(stmtsInOriginalCfg[i]);
1166
+ }
1167
+ originalCfgBlock.addStmt(stmtsInOriginalCfg[i]);
1168
+ }
1169
+ originalCfg.addBlock(originalCfgBlock);
1170
+ return {
1171
+ cfg: cfg,
1172
+ originalCfg: originalCfg,
1173
+ stmtToOriginalStmt: stmtToOriginalStmt,
1174
+ locals: arkIRTransformer.getLocals(),
1175
+ aliasTypeMap: arkIRTransformer.getAliasTypeMap(),
1176
+ };
1177
+ }
1178
+ insertBeforeConditionBlockBuilder(blockBuilderToCfgBlock, conditionBlockBuilder, stmtsInsertBeforeCondition, collectReenter, cfg) {
1179
+ const blockId = conditionBlockBuilder.id;
1180
+ const block = blockBuilderToCfgBlock.get(conditionBlockBuilder);
1181
+ const blockBuildersBeforeCondition = [];
1182
+ const blocksBeforeCondition = [];
1183
+ const blockBuildersReenterCondition = [];
1184
+ const blocksReenterCondition = [];
1185
+ for (const prevBlockBuilder of conditionBlockBuilder.lasts) {
1186
+ const prevBlock = blockBuilderToCfgBlock.get(prevBlockBuilder);
1187
+ if (prevBlock.getId() < blockId) {
1188
+ blockBuildersBeforeCondition.push(prevBlockBuilder);
1189
+ blocksBeforeCondition.push(prevBlock);
1190
+ }
1191
+ else {
1192
+ blockBuildersReenterCondition.push(prevBlockBuilder);
1193
+ blocksReenterCondition.push(prevBlock);
1194
+ }
1195
+ }
1196
+ let collectedBlockBuilders = [];
1197
+ let collectedBlocks = [];
1198
+ if (collectReenter) {
1199
+ collectedBlockBuilders = blockBuildersReenterCondition;
1200
+ collectedBlocks = blocksReenterCondition;
1201
+ }
1202
+ else {
1203
+ collectedBlockBuilders = blockBuildersBeforeCondition;
1204
+ collectedBlocks = blocksBeforeCondition;
1205
+ }
1206
+ const blockBuilderInsertBeforeCondition = new Block(-1, []);
1207
+ blockBuilderInsertBeforeCondition.lasts.push(...collectedBlockBuilders);
1208
+ blockBuilderInsertBeforeCondition.nexts.push(conditionBlockBuilder);
1209
+ const blockInsertBeforeCondition = new BasicBlock_1.BasicBlock();
1210
+ blockInsertBeforeCondition.getStmts().push(...stmtsInsertBeforeCondition);
1211
+ blockInsertBeforeCondition.getPredecessors().push(...collectedBlocks);
1212
+ blockInsertBeforeCondition.addSuccessorBlock(block);
1213
+ for (const prevBlockBuilder of collectedBlockBuilders) {
1214
+ const prevBlock = blockBuilderToCfgBlock.get(prevBlockBuilder);
1215
+ for (let j = 0; j < prevBlockBuilder.nexts.length; j++) {
1216
+ if (prevBlockBuilder.nexts[j] == conditionBlockBuilder) {
1217
+ prevBlockBuilder.nexts[j] = blockBuilderInsertBeforeCondition;
1218
+ prevBlock.setSuccessorBlock(j, blockInsertBeforeCondition);
1219
+ break;
1220
+ }
1221
+ }
1222
+ }
1223
+ let newPrevBlockBuildersBeforeCondition = [];
1224
+ let newPrevBlocksBeforeCondition = [];
1225
+ if (collectReenter) {
1226
+ newPrevBlockBuildersBeforeCondition = [...blockBuildersBeforeCondition, blockBuilderInsertBeforeCondition];
1227
+ newPrevBlocksBeforeCondition = [...blocksBeforeCondition, blockInsertBeforeCondition];
1228
+ }
1229
+ else {
1230
+ newPrevBlockBuildersBeforeCondition = [blockBuilderInsertBeforeCondition, ...blockBuildersReenterCondition];
1231
+ newPrevBlocksBeforeCondition = [blockInsertBeforeCondition, ...blocksReenterCondition];
1232
+ }
1233
+ conditionBlockBuilder.lasts = newPrevBlockBuildersBeforeCondition;
1234
+ const predecessorsCnt = block.getPredecessors().length;
1235
+ block.getPredecessors().splice(0, predecessorsCnt, ...newPrevBlocksBeforeCondition);
1236
+ this.blocks.push(blockBuilderInsertBeforeCondition);
1237
+ cfg.addBlock(blockInsertBeforeCondition);
1238
+ blockBuilderToCfgBlock.set(blockBuilderInsertBeforeCondition, blockInsertBeforeCondition);
1239
+ }
1240
+ }
1241
+ exports.CfgBuilder = CfgBuilder;