arkanalyzer 1.0.19 → 1.0.21

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 (290) hide show
  1. package/lib/Config.d.ts +70 -70
  2. package/lib/Config.js +198 -198
  3. package/lib/Scene.d.ts +316 -310
  4. package/lib/Scene.d.ts.map +1 -1
  5. package/lib/Scene.js +1316 -1271
  6. package/lib/callgraph/algorithm/AbstractAnalysis.d.ts +28 -28
  7. package/lib/callgraph/algorithm/AbstractAnalysis.js +144 -144
  8. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts +10 -10
  9. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.js +69 -69
  10. package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts +15 -15
  11. package/lib/callgraph/algorithm/RapidTypeAnalysis.js +146 -146
  12. package/lib/callgraph/common/Statistics.d.ts +57 -57
  13. package/lib/callgraph/common/Statistics.js +228 -228
  14. package/lib/callgraph/model/CallGraph.d.ts +96 -96
  15. package/lib/callgraph/model/CallGraph.js +354 -354
  16. package/lib/callgraph/model/builder/CallGraphBuilder.d.ts +15 -15
  17. package/lib/callgraph/model/builder/CallGraphBuilder.js +106 -106
  18. package/lib/callgraph/pointerAnalysis/Context.d.ts +37 -37
  19. package/lib/callgraph/pointerAnalysis/Context.js +155 -155
  20. package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts +22 -22
  21. package/lib/callgraph/pointerAnalysis/DummyCallCreator.js +104 -104
  22. package/lib/callgraph/pointerAnalysis/PTAUtils.d.ts +5 -5
  23. package/lib/callgraph/pointerAnalysis/PTAUtils.js +46 -46
  24. package/lib/callgraph/pointerAnalysis/Pag.d.ts +261 -261
  25. package/lib/callgraph/pointerAnalysis/Pag.js +930 -930
  26. package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts +157 -157
  27. package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts.map +1 -1
  28. package/lib/callgraph/pointerAnalysis/PagBuilder.js +1399 -1397
  29. package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts +72 -72
  30. package/lib/callgraph/pointerAnalysis/PointerAnalysis.js +573 -573
  31. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts +15 -15
  32. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.js +81 -81
  33. package/lib/callgraph/pointerAnalysis/PtsDS.d.ts +80 -80
  34. package/lib/callgraph/pointerAnalysis/PtsDS.js +308 -308
  35. package/lib/core/base/Constant.d.ts +48 -48
  36. package/lib/core/base/Constant.js +103 -103
  37. package/lib/core/base/Decorator.d.ts +15 -15
  38. package/lib/core/base/Decorator.js +46 -46
  39. package/lib/core/base/DefUseChain.d.ts +8 -8
  40. package/lib/core/base/DefUseChain.js +25 -25
  41. package/lib/core/base/Expr.d.ts +340 -340
  42. package/lib/core/base/Expr.js +917 -917
  43. package/lib/core/base/Local.d.ts +104 -104
  44. package/lib/core/base/Local.js +176 -176
  45. package/lib/core/base/Position.d.ts +30 -30
  46. package/lib/core/base/Position.js +147 -147
  47. package/lib/core/base/Ref.d.ts +173 -173
  48. package/lib/core/base/Ref.js +374 -374
  49. package/lib/core/base/Stmt.d.ts +237 -237
  50. package/lib/core/base/Stmt.js +525 -525
  51. package/lib/core/base/Trap.d.ts +8 -8
  52. package/lib/core/base/Trap.js +30 -30
  53. package/lib/core/base/Type.d.ts +308 -308
  54. package/lib/core/base/Type.js +639 -639
  55. package/lib/core/base/TypeExpr.d.ts +71 -71
  56. package/lib/core/base/TypeExpr.js +155 -155
  57. package/lib/core/base/Value.d.ts +32 -32
  58. package/lib/core/base/Value.js +16 -16
  59. package/lib/core/common/ArkError.d.ts +14 -14
  60. package/lib/core/common/ArkError.js +28 -28
  61. package/lib/core/common/ArkIRTransformer.d.ts +67 -67
  62. package/lib/core/common/ArkIRTransformer.js +690 -690
  63. package/lib/core/common/ArkValueTransformer.d.ts +89 -89
  64. package/lib/core/common/ArkValueTransformer.js +1478 -1478
  65. package/lib/core/common/Builtin.d.ts +36 -36
  66. package/lib/core/common/Builtin.js +81 -81
  67. package/lib/core/common/Const.d.ts +21 -21
  68. package/lib/core/common/Const.js +43 -43
  69. package/lib/core/common/DummyMainCreater.d.ts +46 -42
  70. package/lib/core/common/DummyMainCreater.d.ts.map +1 -1
  71. package/lib/core/common/DummyMainCreater.js +319 -300
  72. package/lib/core/common/EtsConst.d.ts +73 -73
  73. package/lib/core/common/EtsConst.js +1021 -1021
  74. package/lib/core/common/ExprUseReplacer.d.ts +21 -21
  75. package/lib/core/common/ExprUseReplacer.js +124 -124
  76. package/lib/core/common/IRInference.d.ts +36 -36
  77. package/lib/core/common/IRInference.d.ts.map +1 -1
  78. package/lib/core/common/IRInference.js +651 -648
  79. package/lib/core/common/IRUtils.d.ts +18 -18
  80. package/lib/core/common/IRUtils.js +141 -141
  81. package/lib/core/common/ModelUtils.d.ts +70 -70
  82. package/lib/core/common/ModelUtils.js +712 -712
  83. package/lib/core/common/RefUseReplacer.d.ts +13 -13
  84. package/lib/core/common/RefUseReplacer.js +52 -52
  85. package/lib/core/common/SdkUtils.d.ts +8 -8
  86. package/lib/core/common/SdkUtils.js +105 -105
  87. package/lib/core/common/StmtDefReplacer.d.ts +12 -12
  88. package/lib/core/common/StmtDefReplacer.js +41 -41
  89. package/lib/core/common/StmtUseReplacer.d.ts +16 -16
  90. package/lib/core/common/StmtUseReplacer.js +108 -108
  91. package/lib/core/common/TSConst.d.ts +18 -18
  92. package/lib/core/common/TSConst.js +36 -36
  93. package/lib/core/common/TypeInference.d.ts +102 -102
  94. package/lib/core/common/TypeInference.js +748 -748
  95. package/lib/core/common/ValueUtil.d.ts +12 -12
  96. package/lib/core/common/ValueUtil.js +54 -54
  97. package/lib/core/common/VisibleValue.d.ts +36 -36
  98. package/lib/core/common/VisibleValue.js +212 -212
  99. package/lib/core/dataflow/DataflowProblem.d.ts +20 -20
  100. package/lib/core/dataflow/DataflowProblem.js +20 -20
  101. package/lib/core/dataflow/DataflowResult.d.ts +7 -7
  102. package/lib/core/dataflow/DataflowResult.js +26 -26
  103. package/lib/core/dataflow/DataflowSolver.d.ts +43 -43
  104. package/lib/core/dataflow/DataflowSolver.js +307 -307
  105. package/lib/core/dataflow/Edge.d.ts +11 -11
  106. package/lib/core/dataflow/Edge.js +31 -31
  107. package/lib/core/dataflow/Fact.d.ts +6 -6
  108. package/lib/core/dataflow/Fact.js +24 -24
  109. package/lib/core/dataflow/GenericDataFlow.d.ts +143 -0
  110. package/lib/core/dataflow/GenericDataFlow.d.ts.map +1 -0
  111. package/lib/core/dataflow/GenericDataFlow.js +109 -0
  112. package/lib/core/dataflow/ReachingDef.d.ts +63 -0
  113. package/lib/core/dataflow/ReachingDef.d.ts.map +1 -0
  114. package/lib/core/dataflow/ReachingDef.js +168 -0
  115. package/lib/core/dataflow/UndefinedVariable.d.ts +45 -45
  116. package/lib/core/dataflow/UndefinedVariable.js +288 -288
  117. package/lib/core/dataflow/Util.d.ts +9 -9
  118. package/lib/core/dataflow/Util.js +64 -64
  119. package/lib/core/graph/BaseExplicitGraph.d.ts +58 -63
  120. package/lib/core/graph/BaseExplicitGraph.d.ts.map +1 -1
  121. package/lib/core/graph/BaseExplicitGraph.js +157 -157
  122. package/lib/core/graph/BaseImplicitGraph.d.ts +78 -0
  123. package/lib/core/graph/BaseImplicitGraph.d.ts.map +1 -0
  124. package/lib/core/graph/BaseImplicitGraph.js +78 -0
  125. package/lib/core/graph/BasicBlock.d.ts +104 -104
  126. package/lib/core/graph/BasicBlock.js +280 -280
  127. package/lib/core/graph/Cfg.d.ts +59 -59
  128. package/lib/core/graph/Cfg.js +282 -282
  129. package/lib/core/graph/DependsGraph.d.ts +35 -35
  130. package/lib/core/graph/DependsGraph.js +87 -87
  131. package/lib/core/graph/DominanceFinder.d.ts +15 -15
  132. package/lib/core/graph/DominanceFinder.js +121 -121
  133. package/lib/core/graph/DominanceTree.d.ts +12 -12
  134. package/lib/core/graph/DominanceTree.js +70 -70
  135. package/lib/core/graph/GraphTraits.d.ts +8 -0
  136. package/lib/core/graph/GraphTraits.d.ts.map +1 -0
  137. package/lib/core/graph/GraphTraits.js +16 -0
  138. package/lib/core/graph/Scc.d.ts +61 -60
  139. package/lib/core/graph/Scc.d.ts.map +1 -1
  140. package/lib/core/graph/Scc.js +222 -222
  141. package/lib/core/graph/ViewTree.d.ts +114 -114
  142. package/lib/core/graph/ViewTree.js +16 -16
  143. package/lib/core/graph/builder/CfgBuilder.d.ts +176 -176
  144. package/lib/core/graph/builder/CfgBuilder.js +1139 -1139
  145. package/lib/core/graph/builder/ConditionBuilder.d.ts +15 -15
  146. package/lib/core/graph/builder/ConditionBuilder.js +252 -252
  147. package/lib/core/graph/builder/LoopBuilder.d.ts +20 -20
  148. package/lib/core/graph/builder/LoopBuilder.js +251 -251
  149. package/lib/core/graph/builder/SwitchBuilder.d.ts +11 -11
  150. package/lib/core/graph/builder/SwitchBuilder.js +152 -152
  151. package/lib/core/graph/builder/TrapBuilder.d.ts +16 -16
  152. package/lib/core/graph/builder/TrapBuilder.js +272 -272
  153. package/lib/core/graph/builder/ViewTreeBuilder.d.ts +203 -203
  154. package/lib/core/graph/builder/ViewTreeBuilder.js +1055 -1055
  155. package/lib/core/model/ArkBaseModel.d.ts +59 -59
  156. package/lib/core/model/ArkBaseModel.js +274 -274
  157. package/lib/core/model/ArkBody.d.ts +24 -24
  158. package/lib/core/model/ArkBody.js +65 -65
  159. package/lib/core/model/ArkClass.d.ts +206 -206
  160. package/lib/core/model/ArkClass.js +439 -439
  161. package/lib/core/model/ArkExport.d.ts +72 -72
  162. package/lib/core/model/ArkExport.js +147 -147
  163. package/lib/core/model/ArkField.d.ts +62 -62
  164. package/lib/core/model/ArkField.js +113 -113
  165. package/lib/core/model/ArkFile.d.ts +131 -131
  166. package/lib/core/model/ArkFile.js +267 -267
  167. package/lib/core/model/ArkImport.d.ts +44 -44
  168. package/lib/core/model/ArkImport.js +108 -108
  169. package/lib/core/model/ArkMetadata.d.ts +31 -31
  170. package/lib/core/model/ArkMetadata.js +55 -55
  171. package/lib/core/model/ArkMethod.d.ts +261 -261
  172. package/lib/core/model/ArkMethod.js +592 -592
  173. package/lib/core/model/ArkNamespace.d.ts +66 -66
  174. package/lib/core/model/ArkNamespace.js +209 -209
  175. package/lib/core/model/ArkSignature.d.ts +162 -162
  176. package/lib/core/model/ArkSignature.js +379 -379
  177. package/lib/core/model/builder/ArkClassBuilder.d.ts +12 -12
  178. package/lib/core/model/builder/ArkClassBuilder.js +454 -454
  179. package/lib/core/model/builder/ArkExportBuilder.d.ts +24 -24
  180. package/lib/core/model/builder/ArkExportBuilder.js +210 -210
  181. package/lib/core/model/builder/ArkFieldBuilder.d.ts +8 -8
  182. package/lib/core/model/builder/ArkFieldBuilder.js +193 -193
  183. package/lib/core/model/builder/ArkFileBuilder.d.ts +9 -9
  184. package/lib/core/model/builder/ArkFileBuilder.js +165 -165
  185. package/lib/core/model/builder/ArkImportBuilder.d.ts +4 -4
  186. package/lib/core/model/builder/ArkImportBuilder.js +128 -128
  187. package/lib/core/model/builder/ArkMethodBuilder.d.ts +63 -63
  188. package/lib/core/model/builder/ArkMethodBuilder.js +486 -486
  189. package/lib/core/model/builder/ArkNamespaceBuilder.d.ts +5 -5
  190. package/lib/core/model/builder/ArkNamespaceBuilder.js +205 -205
  191. package/lib/core/model/builder/ArkSignatureBuilder.d.ts +8 -8
  192. package/lib/core/model/builder/ArkSignatureBuilder.js +40 -40
  193. package/lib/core/model/builder/BodyBuilder.d.ts +55 -30
  194. package/lib/core/model/builder/BodyBuilder.d.ts.map +1 -1
  195. package/lib/core/model/builder/BodyBuilder.js +542 -371
  196. package/lib/core/model/builder/builderUtils.d.ts +18 -18
  197. package/lib/core/model/builder/builderUtils.js +558 -558
  198. package/lib/index.d.ts +82 -80
  199. package/lib/index.d.ts.map +1 -1
  200. package/lib/index.js +201 -196
  201. package/lib/save/ArkStream.d.ts +23 -23
  202. package/lib/save/ArkStream.js +83 -83
  203. package/lib/save/DotPrinter.d.ts +43 -43
  204. package/lib/save/DotPrinter.js +170 -170
  205. package/lib/save/GraphPrinter.d.ts +16 -15
  206. package/lib/save/GraphPrinter.d.ts.map +1 -1
  207. package/lib/save/GraphPrinter.js +134 -134
  208. package/lib/save/JsonPrinter.d.ts +30 -30
  209. package/lib/save/JsonPrinter.js +580 -580
  210. package/lib/save/Printer.d.ts +12 -12
  211. package/lib/save/Printer.js +27 -27
  212. package/lib/save/PrinterBuilder.d.ts +53 -53
  213. package/lib/save/PrinterBuilder.js +145 -145
  214. package/lib/save/ViewTreePrinter.d.ts +14 -14
  215. package/lib/save/ViewTreePrinter.js +123 -123
  216. package/lib/save/arkir/ArkIRClassPrinter.d.ts +13 -13
  217. package/lib/save/arkir/ArkIRClassPrinter.js +92 -92
  218. package/lib/save/arkir/ArkIRFieldPrinter.d.ts +11 -11
  219. package/lib/save/arkir/ArkIRFieldPrinter.js +64 -64
  220. package/lib/save/arkir/ArkIRFilePrinter.d.ts +12 -12
  221. package/lib/save/arkir/ArkIRFilePrinter.js +54 -54
  222. package/lib/save/arkir/ArkIRMethodPrinter.d.ts +16 -16
  223. package/lib/save/arkir/ArkIRMethodPrinter.js +159 -159
  224. package/lib/save/arkir/ArkIRNamespacePrinter.d.ts +11 -11
  225. package/lib/save/arkir/ArkIRNamespacePrinter.js +66 -66
  226. package/lib/save/base/BasePrinter.d.ts +23 -23
  227. package/lib/save/base/BasePrinter.js +70 -70
  228. package/lib/save/base/ExportPrinter.d.ts +8 -8
  229. package/lib/save/base/ExportPrinter.js +67 -67
  230. package/lib/save/base/ImportPrinter.d.ts +9 -9
  231. package/lib/save/base/ImportPrinter.js +92 -92
  232. package/lib/save/base/PrinterUtils.d.ts +24 -24
  233. package/lib/save/base/PrinterUtils.js +208 -208
  234. package/lib/save/serializeArkIR.d.ts +8 -8
  235. package/lib/save/serializeArkIR.js +294 -294
  236. package/lib/save/source/SourceBase.d.ts +22 -22
  237. package/lib/save/source/SourceBase.js +64 -64
  238. package/lib/save/source/SourceBody.d.ts +58 -58
  239. package/lib/save/source/SourceBody.js +296 -296
  240. package/lib/save/source/SourceClass.d.ts +25 -25
  241. package/lib/save/source/SourceClass.js +187 -187
  242. package/lib/save/source/SourceField.d.ts +13 -13
  243. package/lib/save/source/SourceField.js +73 -73
  244. package/lib/save/source/SourceFilePrinter.d.ts +12 -12
  245. package/lib/save/source/SourceFilePrinter.js +69 -69
  246. package/lib/save/source/SourceMethod.d.ts +22 -22
  247. package/lib/save/source/SourceMethod.d.ts.map +1 -1
  248. package/lib/save/source/SourceMethod.js +196 -193
  249. package/lib/save/source/SourceNamespace.d.ts +11 -11
  250. package/lib/save/source/SourceNamespace.js +83 -83
  251. package/lib/save/source/SourceStmt.d.ts +178 -178
  252. package/lib/save/source/SourceStmt.js +838 -838
  253. package/lib/save/source/SourceTransformer.d.ts +46 -46
  254. package/lib/save/source/SourceTransformer.js +446 -446
  255. package/lib/transformer/FunctionTransformer.d.ts +2 -2
  256. package/lib/transformer/FunctionTransformer.js +17 -17
  257. package/lib/transformer/SceneTransformer.d.ts +2 -2
  258. package/lib/transformer/SceneTransformer.js +17 -17
  259. package/lib/transformer/StaticSingleAssignmentFormer.d.ts +12 -12
  260. package/lib/transformer/StaticSingleAssignmentFormer.js +259 -259
  261. package/lib/transformer/Transformer.d.ts +6 -6
  262. package/lib/transformer/Transformer.js +22 -22
  263. package/lib/utils/AstTreeUtils.d.ts +4 -4
  264. package/lib/utils/AstTreeUtils.js +26 -26
  265. package/lib/utils/CfgStructualAnalysis.d.ts +110 -110
  266. package/lib/utils/CfgStructualAnalysis.js +1277 -1277
  267. package/lib/utils/FileUtils.d.ts +18 -18
  268. package/lib/utils/FileUtils.js +135 -135
  269. package/lib/utils/SparseBitVector.d.ts +100 -100
  270. package/lib/utils/SparseBitVector.d.ts.map +1 -1
  271. package/lib/utils/SparseBitVector.js +445 -444
  272. package/lib/utils/callGraphUtils.d.ts +30 -30
  273. package/lib/utils/callGraphUtils.js +205 -205
  274. package/lib/utils/crypto_utils.d.ts +5 -5
  275. package/lib/utils/crypto_utils.js +57 -57
  276. package/lib/utils/entryMethodUtils.d.ts +13 -13
  277. package/lib/utils/entryMethodUtils.js +110 -110
  278. package/lib/utils/getAllFiles.d.ts +9 -9
  279. package/lib/utils/getAllFiles.js +90 -90
  280. package/lib/utils/json5parser.d.ts +6 -6
  281. package/lib/utils/json5parser.js +146 -146
  282. package/lib/utils/logger.d.ts +18 -18
  283. package/lib/utils/logger.d.ts.map +1 -1
  284. package/lib/utils/logger.js +97 -90
  285. package/lib/utils/pathTransfer.d.ts +1 -1
  286. package/lib/utils/pathTransfer.js +25 -25
  287. package/node_modules/json5/lib/cli.js +0 -0
  288. package/node_modules/ohos-typescript/bin/tsc +0 -0
  289. package/node_modules/ohos-typescript/bin/tsserver +0 -0
  290. package/package.json +3 -2
@@ -1,1139 +1,1139 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2024-2025 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 = exports.BlockBuilder = exports.TryStatementBuilder = exports.SwitchStatementBuilder = void 0;
41
- const ts = __importStar(require("ohos-typescript"));
42
- const Stmt_1 = require("../../base/Stmt");
43
- const BasicBlock_1 = require("../BasicBlock");
44
- const Cfg_1 = require("../Cfg");
45
- const ArkIRTransformer_1 = require("../../common/ArkIRTransformer");
46
- const ModelUtils_1 = require("../../common/ModelUtils");
47
- const IRUtils_1 = require("../../common/IRUtils");
48
- const LoopBuilder_1 = require("./LoopBuilder");
49
- const SwitchBuilder_1 = require("./SwitchBuilder");
50
- const ConditionBuilder_1 = require("./ConditionBuilder");
51
- const TrapBuilder_1 = require("./TrapBuilder");
52
- class StatementBuilder {
53
- constructor(type, code, astNode, scopeID) {
54
- this.addressCode3 = [];
55
- this.passTmies = 0;
56
- this.numOfIdentifier = 0;
57
- this.isDoWhile = false;
58
- this.type = type;
59
- this.code = code;
60
- this.next = null;
61
- this.lasts = new Set();
62
- this.walked = false;
63
- this.index = 0;
64
- this.line = -1;
65
- this.column = -1;
66
- this.astNode = astNode;
67
- this.scopeID = scopeID;
68
- this.block = null;
69
- this.ifExitPass = false;
70
- }
71
- }
72
- class ConditionStatementBuilder extends StatementBuilder {
73
- constructor(type, code, astNode, scopeID) {
74
- super(type, code, astNode, scopeID);
75
- this.doStatement = null;
76
- this.nextT = null;
77
- this.nextF = null;
78
- this.loopBlock = null;
79
- this.condition = '';
80
- }
81
- }
82
- class SwitchStatementBuilder extends StatementBuilder {
83
- constructor(type, code, astNode, scopeID) {
84
- super(type, code, astNode, scopeID);
85
- this.cases = [];
86
- this.default = null;
87
- this.afterSwitch = null;
88
- this.nexts = [];
89
- }
90
- }
91
- exports.SwitchStatementBuilder = SwitchStatementBuilder;
92
- class TryStatementBuilder extends StatementBuilder {
93
- constructor(type, code, astNode, scopeID) {
94
- super(type, code, astNode, scopeID);
95
- this.tryFirst = null;
96
- this.tryExit = null;
97
- this.catchStatement = null;
98
- this.catchError = '';
99
- this.finallyStatement = null;
100
- this.afterFinal = null;
101
- }
102
- }
103
- exports.TryStatementBuilder = TryStatementBuilder;
104
- class Case {
105
- constructor(value, stmt) {
106
- this.value = value;
107
- this.stmt = stmt;
108
- }
109
- }
110
- class DefUseChain {
111
- constructor(def, use) {
112
- this.def = def;
113
- this.use = use;
114
- }
115
- }
116
- class Variable {
117
- constructor(name, lastDef) {
118
- this.properties = [];
119
- this.propOf = null;
120
- this.name = name;
121
- this.lastDef = lastDef;
122
- this.defUse = [];
123
- }
124
- }
125
- class Scope {
126
- constructor(id) {
127
- this.id = id;
128
- }
129
- }
130
- class BlockBuilder {
131
- constructor(id, stmts) {
132
- this.nexts = [];
133
- this.lasts = [];
134
- this.walked = false;
135
- this.id = id;
136
- this.stmts = stmts;
137
- }
138
- }
139
- exports.BlockBuilder = BlockBuilder;
140
- class Catch {
141
- constructor(errorName, from, to, withLabel) {
142
- this.errorName = errorName;
143
- this.from = from;
144
- this.to = to;
145
- this.withLabel = withLabel;
146
- }
147
- }
148
- class TextError extends Error {
149
- constructor(message) {
150
- // 调用父类的构造函数,并传入错误消息
151
- super(message);
152
- // 设置错误类型的名称
153
- this.name = 'TextError';
154
- }
155
- }
156
- class CfgBuilder {
157
- constructor(ast, name, declaringMethod, sourceFile) {
158
- this.exits = [];
159
- this.emptyBody = false;
160
- this.arrowFunctionWithoutBlock = false;
161
- this.name = name;
162
- this.astRoot = ast;
163
- this.declaringMethod = declaringMethod;
164
- this.declaringClass = declaringMethod.getDeclaringArkClass();
165
- this.entry = new StatementBuilder('entry', '', ast, 0);
166
- this.loopStack = [];
167
- this.switchExitStack = [];
168
- this.functions = [];
169
- this.breakin = '';
170
- this.statementArray = [];
171
- this.dotEdges = [];
172
- this.exit = new StatementBuilder('exit', 'return;', null, 0);
173
- this.scopes = [];
174
- this.tempVariableNum = 0;
175
- this.current3ACstm = this.entry;
176
- this.blocks = [];
177
- this.currentDeclarationKeyword = '';
178
- this.variables = [];
179
- this.importFromPath = [];
180
- this.catches = [];
181
- this.sourceFile = sourceFile;
182
- this.arrowFunctionWithoutBlock = true;
183
- }
184
- getDeclaringMethod() {
185
- return this.declaringMethod;
186
- }
187
- judgeLastType(s, lastStatement) {
188
- if (lastStatement.type === 'ifStatement') {
189
- let lastIf = lastStatement;
190
- if (lastIf.nextT == null) {
191
- lastIf.nextT = s;
192
- s.lasts.add(lastIf);
193
- }
194
- else {
195
- lastIf.nextF = s;
196
- s.lasts.add(lastIf);
197
- }
198
- }
199
- else if (lastStatement.type === 'loopStatement') {
200
- let lastLoop = lastStatement;
201
- lastLoop.nextT = s;
202
- s.lasts.add(lastLoop);
203
- }
204
- else if (lastStatement.type === 'catchOrNot') {
205
- let lastLoop = lastStatement;
206
- lastLoop.nextT = s;
207
- s.lasts.add(lastLoop);
208
- }
209
- else {
210
- lastStatement.next = s;
211
- s.lasts.add(lastStatement);
212
- }
213
- }
214
- ASTNodeBreakStatement(c, lastStatement) {
215
- let p = c;
216
- while (p && p !== this.astRoot) {
217
- if (ts.isWhileStatement(p) || ts.isDoStatement(p) || ts.isForStatement(p) || ts.isForInStatement(p) || ts.isForOfStatement(p)) {
218
- const lastLoopNextF = this.loopStack[this.loopStack.length - 1].nextF;
219
- this.judgeLastType(lastLoopNextF, lastStatement);
220
- lastLoopNextF.lasts.add(lastStatement);
221
- return;
222
- }
223
- if (ts.isCaseClause(p) || ts.isDefaultClause(p)) {
224
- const lastSwitchExit = this.switchExitStack[this.switchExitStack.length - 1];
225
- this.judgeLastType(lastSwitchExit, lastStatement);
226
- lastSwitchExit.lasts.add(lastStatement);
227
- return;
228
- }
229
- p = p.parent;
230
- }
231
- }
232
- ASTNodeIfStatement(c, lastStatement, scopeID) {
233
- let ifstm = new ConditionStatementBuilder('ifStatement', '', c, scopeID);
234
- this.judgeLastType(ifstm, lastStatement);
235
- let ifexit = new StatementBuilder('ifExit', '', c, scopeID);
236
- this.exits.push(ifexit);
237
- ifstm.condition = c.expression.getText(this.sourceFile);
238
- ifstm.code = 'if (' + ifstm.condition + ')';
239
- if (ts.isBlock(c.thenStatement)) {
240
- this.walkAST(ifstm, ifexit, [...c.thenStatement.statements]);
241
- }
242
- else {
243
- this.walkAST(ifstm, ifexit, [c.thenStatement]);
244
- }
245
- if (c.elseStatement) {
246
- if (ts.isBlock(c.elseStatement)) {
247
- this.walkAST(ifstm, ifexit, [...c.elseStatement.statements]);
248
- }
249
- else {
250
- this.walkAST(ifstm, ifexit, [c.elseStatement]);
251
- }
252
- }
253
- if (!ifstm.nextT) {
254
- ifstm.nextT = ifexit;
255
- ifexit.lasts.add(ifstm);
256
- }
257
- if (!ifstm.nextF) {
258
- ifstm.nextF = ifexit;
259
- ifexit.lasts.add(ifstm);
260
- }
261
- return ifexit;
262
- }
263
- ASTNodeWhileStatement(c, lastStatement, scopeID) {
264
- this.breakin = 'loop';
265
- let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
266
- this.loopStack.push(loopstm);
267
- this.judgeLastType(loopstm, lastStatement);
268
- let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
269
- this.exits.push(loopExit);
270
- loopstm.nextF = loopExit;
271
- loopExit.lasts.add(loopstm);
272
- loopstm.condition = c.expression.getText(this.sourceFile);
273
- loopstm.code = 'while (' + loopstm.condition + ')';
274
- if (ts.isBlock(c.statement)) {
275
- this.walkAST(loopstm, loopstm, [...c.statement.statements]);
276
- }
277
- else {
278
- this.walkAST(loopstm, loopstm, [c.statement]);
279
- }
280
- if (!loopstm.nextF) {
281
- loopstm.nextF = loopExit;
282
- loopExit.lasts.add(loopstm);
283
- }
284
- if (!loopstm.nextT) {
285
- loopstm.nextT = loopExit;
286
- loopExit.lasts.add(loopstm);
287
- }
288
- this.loopStack.pop();
289
- return loopExit;
290
- }
291
- ASTNodeForStatement(c, lastStatement, scopeID) {
292
- var _a, _b, _c, _d, _e;
293
- this.breakin = 'loop';
294
- let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
295
- this.loopStack.push(loopstm);
296
- this.judgeLastType(loopstm, lastStatement);
297
- let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
298
- this.exits.push(loopExit);
299
- loopstm.nextF = loopExit;
300
- loopExit.lasts.add(loopstm);
301
- loopstm.code = 'for (';
302
- if (ts.isForStatement(c)) {
303
- 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));
304
- }
305
- else if (ts.isForOfStatement(c)) {
306
- loopstm.code += ((_d = c.initializer) === null || _d === void 0 ? void 0 : _d.getText(this.sourceFile)) + ' of ' + c.expression.getText(this.sourceFile);
307
- }
308
- else {
309
- loopstm.code += ((_e = c.initializer) === null || _e === void 0 ? void 0 : _e.getText(this.sourceFile)) + ' in ' + c.expression.getText(this.sourceFile);
310
- }
311
- loopstm.code += ')';
312
- if (ts.isBlock(c.statement)) {
313
- this.walkAST(loopstm, loopstm, [...c.statement.statements]);
314
- }
315
- else {
316
- this.walkAST(loopstm, loopstm, [c.statement]);
317
- }
318
- if (!loopstm.nextF) {
319
- loopstm.nextF = loopExit;
320
- loopExit.lasts.add(loopstm);
321
- }
322
- if (!loopstm.nextT) {
323
- loopstm.nextT = loopExit;
324
- loopExit.lasts.add(loopstm);
325
- }
326
- this.loopStack.pop();
327
- return loopExit;
328
- }
329
- ASTNodeDoStatement(c, lastStatement, scopeID) {
330
- var _a, _b;
331
- this.breakin = 'loop';
332
- let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
333
- this.loopStack.push(loopstm);
334
- let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
335
- this.exits.push(loopExit);
336
- loopstm.nextF = loopExit;
337
- loopExit.lasts.add(loopstm);
338
- loopstm.condition = c.expression.getText(this.sourceFile);
339
- loopstm.code = 'while (' + loopstm.condition + ')';
340
- loopstm.isDoWhile = true;
341
- if (ts.isBlock(c.statement)) {
342
- this.walkAST(lastStatement, loopstm, [...c.statement.statements]);
343
- }
344
- else {
345
- this.walkAST(lastStatement, loopstm, [c.statement]);
346
- }
347
- let lastType = lastStatement.type;
348
- if (lastType === 'ifStatement' || lastType === 'loopStatement') {
349
- let lastCondition = lastStatement;
350
- loopstm.nextT = lastCondition.nextT;
351
- (_a = lastCondition.nextT) === null || _a === void 0 ? void 0 : _a.lasts.add(loopstm);
352
- }
353
- else {
354
- loopstm.nextT = lastStatement.next;
355
- (_b = lastStatement.next) === null || _b === void 0 ? void 0 : _b.lasts.add(loopstm);
356
- }
357
- if (loopstm.nextT && loopstm.nextT !== loopstm) {
358
- loopstm.nextT.isDoWhile = true;
359
- loopstm.doStatement = loopstm.nextT;
360
- }
361
- this.loopStack.pop();
362
- return loopExit;
363
- }
364
- ASTNodeSwitchStatement(c, lastStatement, scopeID) {
365
- var _a;
366
- this.breakin = 'switch';
367
- let switchstm = new SwitchStatementBuilder('switchStatement', '', c, scopeID);
368
- this.judgeLastType(switchstm, lastStatement);
369
- let switchExit = new StatementBuilder('switchExit', '', null, scopeID);
370
- this.exits.push(switchExit);
371
- this.switchExitStack.push(switchExit);
372
- switchExit.lasts.add(switchstm);
373
- switchstm.code = 'switch (' + c.expression + ')';
374
- let lastCaseExit = null;
375
- for (let i = 0; i < c.caseBlock.clauses.length; i++) {
376
- const clause = c.caseBlock.clauses[i];
377
- let casestm;
378
- if (ts.isCaseClause(clause)) {
379
- casestm = new StatementBuilder('statement', 'case ' + clause.expression.getText(this.sourceFile) + ':', clause, scopeID);
380
- }
381
- else {
382
- casestm = new StatementBuilder('statement', 'default:', clause, scopeID);
383
- }
384
- switchstm.nexts.push(casestm);
385
- casestm.lasts.add(switchstm);
386
- let caseExit = new StatementBuilder('caseExit', '', null, scopeID);
387
- this.exits.push(caseExit);
388
- this.walkAST(casestm, caseExit, [...clause.statements]);
389
- if (ts.isCaseClause(clause)) {
390
- const cas = new Case(casestm.code, casestm.next);
391
- switchstm.cases.push(cas);
392
- }
393
- else {
394
- switchstm.default = casestm.next;
395
- }
396
- switchstm.nexts[switchstm.nexts.length - 1] = casestm.next;
397
- for (const stmt of [...casestm.lasts]) {
398
- casestm.next.lasts.add(stmt);
399
- }
400
- casestm.next.lasts.delete(casestm);
401
- if (lastCaseExit) {
402
- lastCaseExit.next = casestm.next;
403
- (_a = casestm.next) === null || _a === void 0 ? void 0 : _a.lasts.add(lastCaseExit);
404
- }
405
- lastCaseExit = caseExit;
406
- if (i === c.caseBlock.clauses.length - 1) {
407
- caseExit.next = switchExit;
408
- switchExit.lasts.add(caseExit);
409
- }
410
- }
411
- this.switchExitStack.pop();
412
- return switchExit;
413
- }
414
- ASTNodeTryStatement(c, lastStatement, scopeID) {
415
- var _a, _b;
416
- let trystm = new TryStatementBuilder('tryStatement', 'try', c, scopeID);
417
- this.judgeLastType(trystm, lastStatement);
418
- let tryExit = new StatementBuilder('tryExit', '', c, scopeID);
419
- this.exits.push(tryExit);
420
- trystm.tryExit = tryExit;
421
- this.walkAST(trystm, tryExit, [...c.tryBlock.statements]);
422
- trystm.tryFirst = trystm.next;
423
- (_a = trystm.next) === null || _a === void 0 ? void 0 : _a.lasts.add(trystm);
424
- if (c.catchClause) {
425
- let text = 'catch';
426
- if (c.catchClause.variableDeclaration) {
427
- text += '(' + c.catchClause.variableDeclaration.getText(this.sourceFile) + ')';
428
- }
429
- let catchOrNot = new ConditionStatementBuilder('catchOrNot', text, c, scopeID);
430
- let catchExit = new StatementBuilder('catch exit', '', c, scopeID);
431
- catchOrNot.nextF = catchExit;
432
- catchExit.lasts.add(catchOrNot);
433
- this.walkAST(catchOrNot, catchExit, [...c.catchClause.block.statements]);
434
- if (!catchOrNot.nextT) {
435
- catchOrNot.nextT = catchExit;
436
- catchExit.lasts.add(catchOrNot);
437
- }
438
- const catchStatement = new StatementBuilder('statement', catchOrNot.code, c.catchClause, catchOrNot.nextT.scopeID);
439
- catchStatement.next = catchOrNot.nextT;
440
- trystm.catchStatement = catchStatement;
441
- catchStatement.lasts.add(trystm);
442
- if (c.catchClause.variableDeclaration) {
443
- trystm.catchError = c.catchClause.variableDeclaration.getText(this.sourceFile);
444
- }
445
- else {
446
- trystm.catchError = 'Error';
447
- }
448
- }
449
- let final = new StatementBuilder('statement', 'finally', c, scopeID);
450
- let finalExit = new StatementBuilder('finallyExit', '', c, scopeID);
451
- this.exits.push(finalExit);
452
- if (c.finallyBlock && c.finallyBlock.statements.length > 0) {
453
- this.walkAST(final, finalExit, [...c.finallyBlock.statements]);
454
- }
455
- else {
456
- let dummyFinally = new StatementBuilder('statement', 'dummyFinally', c, (new Scope(this.scopes.length)).id);
457
- final.next = dummyFinally;
458
- dummyFinally.lasts.add(final);
459
- dummyFinally.next = finalExit;
460
- finalExit.lasts.add(dummyFinally);
461
- }
462
- trystm.finallyStatement = final.next;
463
- tryExit.next = final.next;
464
- (_b = final.next) === null || _b === void 0 ? void 0 : _b.lasts.add(tryExit);
465
- trystm.next = finalExit;
466
- finalExit.lasts.add(trystm);
467
- return finalExit;
468
- }
469
- walkAST(lastStatement, nextStatement, nodes) {
470
- let scope = new Scope(this.scopes.length);
471
- this.scopes.push(scope);
472
- for (let i = 0; i < nodes.length; i++) {
473
- let c = nodes[i];
474
- if (ts.isVariableStatement(c) || ts.isExpressionStatement(c) || ts.isThrowStatement(c) || ts.isTypeAliasDeclaration(c)) {
475
- let s = new StatementBuilder('statement', c.getText(this.sourceFile), c, scope.id);
476
- this.judgeLastType(s, lastStatement);
477
- lastStatement = s;
478
- }
479
- else if (!this.declaringMethod.isDefaultArkMethod() && ts.isFunctionDeclaration(c)) {
480
- let s = new StatementBuilder('functionDeclarationStatement', c.getText(this.sourceFile), c, scope.id);
481
- this.judgeLastType(s, lastStatement);
482
- lastStatement = s;
483
- }
484
- else if (!this.declaringMethod.isDefaultArkMethod() && ts.isClassDeclaration(c)) {
485
- let s = new StatementBuilder('classDeclarationStatement', c.getText(this.sourceFile), c, scope.id);
486
- this.judgeLastType(s, lastStatement);
487
- lastStatement = s;
488
- }
489
- else if (ts.isReturnStatement(c)) {
490
- let s = new StatementBuilder('returnStatement', c.getText(this.sourceFile), c, scope.id);
491
- this.judgeLastType(s, lastStatement);
492
- lastStatement = s;
493
- break;
494
- }
495
- else if (ts.isBreakStatement(c)) {
496
- this.ASTNodeBreakStatement(c, lastStatement);
497
- return;
498
- }
499
- else if (ts.isContinueStatement(c)) {
500
- const lastLoop = this.loopStack[this.loopStack.length - 1];
501
- this.judgeLastType(lastLoop, lastStatement);
502
- lastLoop.lasts.add(lastStatement);
503
- return;
504
- }
505
- else if (ts.isIfStatement(c)) {
506
- lastStatement = this.ASTNodeIfStatement(c, lastStatement, scope.id);
507
- }
508
- else if (ts.isWhileStatement(c)) {
509
- lastStatement = this.ASTNodeWhileStatement(c, lastStatement, scope.id);
510
- }
511
- if (ts.isForStatement(c) || ts.isForInStatement(c) || ts.isForOfStatement(c)) {
512
- lastStatement = this.ASTNodeForStatement(c, lastStatement, scope.id);
513
- }
514
- else if (ts.isDoStatement(c)) {
515
- lastStatement = this.ASTNodeDoStatement(c, lastStatement, scope.id);
516
- }
517
- else if (ts.isSwitchStatement(c)) {
518
- lastStatement = this.ASTNodeSwitchStatement(c, lastStatement, scope.id);
519
- }
520
- else if (ts.isBlock(c)) {
521
- let blockExit = new StatementBuilder('blockExit', '', c, scope.id);
522
- this.exits.push(blockExit);
523
- this.walkAST(lastStatement, blockExit, c.getChildren(this.sourceFile)[1].getChildren(this.sourceFile));
524
- lastStatement = blockExit;
525
- }
526
- else if (ts.isTryStatement(c)) {
527
- lastStatement = this.ASTNodeTryStatement(c, lastStatement, scope.id);
528
- }
529
- else if (ts.isExportAssignment(c)) {
530
- if (ts.isNewExpression(c.expression) || ts.isObjectLiteralExpression(c.expression)) {
531
- let s = new StatementBuilder('statement', c.getText(this.sourceFile), c, scope.id);
532
- this.judgeLastType(s, lastStatement);
533
- lastStatement = s;
534
- }
535
- }
536
- }
537
- if (lastStatement.type !== 'breakStatement' && lastStatement.type !== 'continueStatement' && lastStatement.type !== 'returnStatement') {
538
- lastStatement.next = nextStatement;
539
- nextStatement.lasts.add(lastStatement);
540
- }
541
- }
542
- addReturnInEmptyMethod() {
543
- if (this.entry.next === this.exit) {
544
- const ret = new StatementBuilder('returnStatement', 'return;', null, this.entry.scopeID);
545
- this.entry.next = ret;
546
- ret.lasts.add(this.entry);
547
- ret.next = this.exit;
548
- this.exit.lasts = new Set([ret]);
549
- }
550
- }
551
- deleteExitAfterCondition(last, exit) {
552
- if (last.nextT === exit) {
553
- last.nextT = exit.next;
554
- const lasts = exit.next.lasts;
555
- lasts.delete(exit);
556
- lasts.add(last);
557
- }
558
- else if (last.nextF === exit) {
559
- last.nextF = exit.next;
560
- const lasts = exit.next.lasts;
561
- lasts.delete(exit);
562
- lasts.add(last);
563
- }
564
- }
565
- deleteExitAfterSwitch(last, exit) {
566
- var _a;
567
- if (exit.type === 'switchExit') {
568
- last.afterSwitch = exit.next;
569
- }
570
- exit.next.lasts.delete(exit);
571
- last.nexts = last.nexts.filter(item => item !== exit);
572
- if (last.nexts.length === 0) {
573
- last.next = exit.next;
574
- (_a = exit.next) === null || _a === void 0 ? void 0 : _a.lasts.add(last);
575
- }
576
- }
577
- deleteExit() {
578
- for (const exit of this.exits) {
579
- const lasts = [...exit.lasts];
580
- for (const last of lasts) {
581
- if (last instanceof ConditionStatementBuilder) {
582
- this.deleteExitAfterCondition(last, exit);
583
- }
584
- else if (last instanceof SwitchStatementBuilder) {
585
- this.deleteExitAfterSwitch(last, exit);
586
- }
587
- else if (last instanceof TryStatementBuilder && exit.type === 'finallyExit') {
588
- last.afterFinal = exit.next;
589
- last.next = last.tryFirst;
590
- exit.lasts.delete(last);
591
- }
592
- else {
593
- last.next = exit.next;
594
- const lasts = exit.next.lasts;
595
- lasts.delete(exit);
596
- lasts.add(last);
597
- }
598
- }
599
- }
600
- // 部分语句例如return后面的exit语句的next无法在上面清除
601
- for (const exit of this.exits) {
602
- if (exit.next && exit.next.lasts.has(exit)) {
603
- exit.next.lasts.delete(exit);
604
- }
605
- }
606
- }
607
- addStmt2BlockStmtQueueInSpecialCase(stmt, stmtQueue) {
608
- if (stmt.next) {
609
- if ((stmt.type === 'continueStatement' || stmt.next.type === 'loopStatement') && stmt.next.block || stmt.next.type.includes('exit')) {
610
- return null;
611
- }
612
- stmt.next.passTmies++;
613
- if (stmt.next.passTmies === stmt.next.lasts.size || stmt.next.type === 'loopStatement' || stmt.next.isDoWhile) {
614
- if (stmt.next.scopeID !== stmt.scopeID && !(stmt.next instanceof ConditionStatementBuilder && stmt.next.doStatement) &&
615
- !(ts.isCaseClause(stmt.astNode) || ts.isDefaultClause(stmt.astNode))) {
616
- stmtQueue.push(stmt.next);
617
- return null;
618
- }
619
- return stmt.next;
620
- }
621
- }
622
- return null;
623
- }
624
- addStmt2BlockStmtQueue(stmt, stmtQueue) {
625
- if (stmt instanceof ConditionStatementBuilder) {
626
- stmtQueue.push(stmt.nextF);
627
- stmtQueue.push(stmt.nextT);
628
- }
629
- else if (stmt instanceof SwitchStatementBuilder) {
630
- if (stmt.nexts.length === 0) {
631
- stmtQueue.push(stmt.afterSwitch);
632
- }
633
- for (let i = stmt.nexts.length - 1; i >= 0; i--) {
634
- stmtQueue.push(stmt.nexts[i]);
635
- }
636
- }
637
- else if (stmt instanceof TryStatementBuilder) {
638
- if (stmt.finallyStatement) {
639
- stmtQueue.push(stmt.finallyStatement);
640
- }
641
- if (stmt.catchStatement) {
642
- stmtQueue.push(stmt.catchStatement);
643
- }
644
- if (stmt.tryFirst) {
645
- stmtQueue.push(stmt.tryFirst);
646
- }
647
- }
648
- else if (stmt.next) {
649
- return this.addStmt2BlockStmtQueueInSpecialCase(stmt, stmtQueue);
650
- }
651
- return null;
652
- }
653
- buildBlocks() {
654
- const stmtQueue = [this.entry];
655
- const handledStmts = new Set();
656
- while (stmtQueue.length > 0) {
657
- let stmt = stmtQueue.pop();
658
- if (stmt.type.includes('exit')) {
659
- continue;
660
- }
661
- if (handledStmts.has(stmt)) {
662
- continue;
663
- }
664
- const block = new BlockBuilder(this.blocks.length, []);
665
- this.blocks.push(block);
666
- while (stmt && !handledStmts.has(stmt)) {
667
- if (stmt.type === 'loopStatement' && block.stmts.length > 0 && !stmt.isDoWhile) {
668
- stmtQueue.push(stmt);
669
- break;
670
- }
671
- if (stmt.type.includes('Exit')) {
672
- break;
673
- }
674
- block.stmts.push(stmt);
675
- stmt.block = block;
676
- handledStmts.add(stmt);
677
- const addRet = this.addStmt2BlockStmtQueue(stmt, stmtQueue);
678
- if (addRet instanceof StatementBuilder) {
679
- stmt = addRet;
680
- }
681
- else {
682
- break;
683
- }
684
- }
685
- }
686
- }
687
- buildConditionNextBlocks(originStatement, block, isLastStatement) {
688
- var _a, _b, _c, _d;
689
- let nextT = (_a = originStatement.nextT) === null || _a === void 0 ? void 0 : _a.block;
690
- if (nextT && (isLastStatement || nextT !== block) && !((_b = originStatement.nextT) === null || _b === void 0 ? void 0 : _b.type.includes(' exit'))) {
691
- block.nexts.push(nextT);
692
- nextT.lasts.push(block);
693
- }
694
- let nextF = (_c = originStatement.nextF) === null || _c === void 0 ? void 0 : _c.block;
695
- if (nextF && (isLastStatement || nextF !== block) && !((_d = originStatement.nextF) === null || _d === void 0 ? void 0 : _d.type.includes(' exit'))) {
696
- block.nexts.push(nextF);
697
- nextF.lasts.push(block);
698
- }
699
- }
700
- buildSwitchNextBlocks(originStatement, block, isLastStatement) {
701
- if (originStatement.nexts.length === 0) {
702
- const nextBlock = originStatement.afterSwitch.block;
703
- if (nextBlock && (isLastStatement || nextBlock !== block)) {
704
- block.nexts.push(nextBlock);
705
- nextBlock.lasts.push(block);
706
- }
707
- }
708
- for (const next of originStatement.nexts) {
709
- const nextBlock = next.block;
710
- if (nextBlock && (isLastStatement || nextBlock !== block)) {
711
- block.nexts.push(nextBlock);
712
- nextBlock.lasts.push(block);
713
- }
714
- }
715
- }
716
- buildNormalNextBlocks(originStatement, block, isLastStatement) {
717
- var _a, _b;
718
- let next = (_a = originStatement.next) === null || _a === void 0 ? void 0 : _a.block;
719
- if (next && (isLastStatement || next !== block) && !((_b = originStatement.next) === null || _b === void 0 ? void 0 : _b.type.includes(' exit'))) {
720
- block.nexts.push(next);
721
- next.lasts.push(block);
722
- }
723
- }
724
- buildBlocksNextLast() {
725
- for (let block of this.blocks) {
726
- for (let originStatement of block.stmts) {
727
- let isLastStatement = (block.stmts.indexOf(originStatement) === block.stmts.length - 1);
728
- if (originStatement instanceof ConditionStatementBuilder) {
729
- this.buildConditionNextBlocks(originStatement, block, isLastStatement);
730
- }
731
- else if (originStatement instanceof SwitchStatementBuilder) {
732
- this.buildSwitchNextBlocks(originStatement, block, isLastStatement);
733
- }
734
- else {
735
- this.buildNormalNextBlocks(originStatement, block, isLastStatement);
736
- }
737
- }
738
- }
739
- }
740
- addReturnBlock(returnStatement, notReturnStmts) {
741
- var _a, _b, _c;
742
- let returnBlock = new BlockBuilder(this.blocks.length, [returnStatement]);
743
- returnStatement.block = returnBlock;
744
- this.blocks.push(returnBlock);
745
- for (const notReturnStmt of notReturnStmts) {
746
- if (notReturnStmt instanceof ConditionStatementBuilder) {
747
- if (this.exit === notReturnStmt.nextT) {
748
- notReturnStmt.nextT = returnStatement;
749
- (_a = notReturnStmt.block) === null || _a === void 0 ? void 0 : _a.nexts.splice(0, 0, returnBlock);
750
- }
751
- else if (this.exit === notReturnStmt.nextF) {
752
- notReturnStmt.nextF = returnStatement;
753
- (_b = notReturnStmt.block) === null || _b === void 0 ? void 0 : _b.nexts.push(returnBlock);
754
- }
755
- }
756
- else {
757
- notReturnStmt.next = returnStatement;
758
- (_c = notReturnStmt.block) === null || _c === void 0 ? void 0 : _c.nexts.push(returnBlock);
759
- }
760
- returnStatement.lasts.add(notReturnStmt);
761
- returnStatement.next = this.exit;
762
- const lasts = [...this.exit.lasts];
763
- lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
764
- this.exit.lasts = new Set(lasts);
765
- returnBlock.lasts.push(notReturnStmt.block);
766
- }
767
- this.exit.block = returnBlock;
768
- }
769
- addReturnStmt() {
770
- var _a;
771
- let notReturnStmts = [];
772
- for (let stmt of [...this.exit.lasts]) {
773
- if (stmt.type !== 'returnStatement') {
774
- notReturnStmts.push(stmt);
775
- }
776
- }
777
- if (notReturnStmts.length < 1) {
778
- return;
779
- }
780
- const returnStatement = new StatementBuilder('returnStatement', 'return;', null, this.exit.scopeID);
781
- let TryOrSwitchExit = false;
782
- if (notReturnStmts.length === 1 && notReturnStmts[0].block) {
783
- let p = notReturnStmts[0].astNode;
784
- while (p && p !== this.astRoot) {
785
- if (ts.isTryStatement(p) || ts.isSwitchStatement(p)) {
786
- TryOrSwitchExit = true;
787
- break;
788
- }
789
- p = p.parent;
790
- }
791
- }
792
- if (notReturnStmts.length === 1 && !(notReturnStmts[0] instanceof ConditionStatementBuilder) && !TryOrSwitchExit) {
793
- const notReturnStmt = notReturnStmts[0];
794
- notReturnStmt.next = returnStatement;
795
- returnStatement.lasts = new Set([notReturnStmt]);
796
- returnStatement.next = this.exit;
797
- const lasts = [...this.exit.lasts];
798
- lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
799
- this.exit.lasts = new Set(lasts);
800
- (_a = notReturnStmt.block) === null || _a === void 0 ? void 0 : _a.stmts.push(returnStatement);
801
- returnStatement.block = notReturnStmt.block;
802
- }
803
- else {
804
- this.addReturnBlock(returnStatement, notReturnStmts);
805
- }
806
- }
807
- resetWalked() {
808
- for (let stmt of this.statementArray) {
809
- stmt.walked = false;
810
- }
811
- }
812
- addStmtBuilderPosition() {
813
- for (const stmt of this.statementArray) {
814
- if (stmt.astNode) {
815
- const { line, character } = ts.getLineAndCharacterOfPosition(this.sourceFile, stmt.astNode.getStart(this.sourceFile));
816
- stmt.line = line + 1;
817
- stmt.column = character + 1;
818
- }
819
- }
820
- }
821
- CfgBuilder2Array(stmt) {
822
- if (stmt.walked) {
823
- return;
824
- }
825
- stmt.walked = true;
826
- stmt.index = this.statementArray.length;
827
- if (!stmt.type.includes(' exit')) {
828
- this.statementArray.push(stmt);
829
- }
830
- if (stmt.type === 'ifStatement' || stmt.type === 'loopStatement' || stmt.type === 'catchOrNot') {
831
- let cstm = stmt;
832
- if (cstm.nextT == null || cstm.nextF == null) {
833
- this.errorTest(cstm);
834
- return;
835
- }
836
- this.CfgBuilder2Array(cstm.nextF);
837
- this.CfgBuilder2Array(cstm.nextT);
838
- }
839
- else if (stmt.type === 'switchStatement') {
840
- let sstm = stmt;
841
- for (let ss of sstm.nexts) {
842
- this.CfgBuilder2Array(ss);
843
- }
844
- }
845
- else if (stmt.type === 'tryStatement') {
846
- let trystm = stmt;
847
- if (trystm.tryFirst) {
848
- this.CfgBuilder2Array(trystm.tryFirst);
849
- }
850
- if (trystm.catchStatement) {
851
- this.CfgBuilder2Array(trystm.catchStatement);
852
- }
853
- if (trystm.finallyStatement) {
854
- this.CfgBuilder2Array(trystm.finallyStatement);
855
- }
856
- if (trystm.next) {
857
- this.CfgBuilder2Array(trystm.next);
858
- }
859
- }
860
- else {
861
- if (stmt.next != null) {
862
- this.CfgBuilder2Array(stmt.next);
863
- }
864
- }
865
- }
866
- getDotEdges(stmt) {
867
- if (this.statementArray.length === 0) {
868
- this.CfgBuilder2Array(this.entry);
869
- }
870
- if (stmt.walked) {
871
- return;
872
- }
873
- stmt.walked = true;
874
- if (stmt.type === 'ifStatement' || stmt.type === 'loopStatement' || stmt.type === 'catchOrNot') {
875
- let cstm = stmt;
876
- if (cstm.nextT == null || cstm.nextF == null) {
877
- this.errorTest(cstm);
878
- return;
879
- }
880
- let edge = [cstm.index, cstm.nextF.index];
881
- this.dotEdges.push(edge);
882
- edge = [cstm.index, cstm.nextT.index];
883
- this.dotEdges.push(edge);
884
- this.getDotEdges(cstm.nextF);
885
- this.getDotEdges(cstm.nextT);
886
- }
887
- else if (stmt.type === 'switchStatement') {
888
- let sstm = stmt;
889
- for (let ss of sstm.nexts) {
890
- let edge = [sstm.index, ss.index];
891
- this.dotEdges.push(edge);
892
- this.getDotEdges(ss);
893
- }
894
- }
895
- else {
896
- if (stmt.next != null) {
897
- let edge = [stmt.index, stmt.next.index];
898
- this.dotEdges.push(edge);
899
- this.getDotEdges(stmt.next);
900
- }
901
- }
902
- }
903
- errorTest(stmt) {
904
- var _a, _b;
905
- let mes = 'ifnext error ';
906
- if ((_a = this.declaringClass) === null || _a === void 0 ? void 0 : _a.getDeclaringArkFile()) {
907
- mes += ((_b = this.declaringClass) === null || _b === void 0 ? void 0 : _b.getDeclaringArkFile().getName()) + '.' + this.declaringClass.getName() + '.' + this.name;
908
- }
909
- mes += '\n' + stmt.code;
910
- throw new TextError(mes);
911
- }
912
- buildStatementBuilder4ArrowFunction(stmt) {
913
- let s = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
914
- this.entry.next = s;
915
- s.lasts = new Set([this.entry]);
916
- s.next = this.exit;
917
- this.exit.lasts = new Set([s]);
918
- }
919
- buildCfgBuilder() {
920
- let stmts = [];
921
- if (ts.isSourceFile(this.astRoot)) {
922
- stmts = [...this.astRoot.statements];
923
- }
924
- else if (ts.isFunctionDeclaration(this.astRoot) || ts.isMethodDeclaration(this.astRoot) ||
925
- ts.isConstructorDeclaration(this.astRoot) || ts.isGetAccessorDeclaration(this.astRoot) ||
926
- ts.isSetAccessorDeclaration(this.astRoot) || ts.isFunctionExpression(this.astRoot) ||
927
- ts.isClassStaticBlockDeclaration(this.astRoot)) {
928
- if (this.astRoot.body) {
929
- stmts = [...this.astRoot.body.statements];
930
- }
931
- else {
932
- this.emptyBody = true;
933
- }
934
- }
935
- else if (ts.isArrowFunction(this.astRoot)) {
936
- if (ts.isBlock(this.astRoot.body)) {
937
- stmts = [...this.astRoot.body.statements];
938
- }
939
- }
940
- else if (ts.isMethodSignature(this.astRoot) || ts.isConstructSignatureDeclaration(this.astRoot) ||
941
- ts.isCallSignatureDeclaration(this.astRoot) || ts.isFunctionTypeNode(this.astRoot)) {
942
- this.emptyBody = true;
943
- }
944
- else if (ts.isModuleDeclaration(this.astRoot) && ts.isModuleBlock(this.astRoot.body)) {
945
- stmts = [...this.astRoot.body.statements];
946
- }
947
- if (!ModelUtils_1.ModelUtils.isArkUIBuilderMethod(this.declaringMethod)) {
948
- this.walkAST(this.entry, this.exit, stmts);
949
- }
950
- else {
951
- this.handleBuilder(stmts);
952
- }
953
- if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
954
- this.buildStatementBuilder4ArrowFunction(this.astRoot.body);
955
- }
956
- this.addReturnInEmptyMethod();
957
- this.deleteExit();
958
- this.CfgBuilder2Array(this.entry);
959
- this.addStmtBuilderPosition();
960
- this.buildBlocks();
961
- this.blocks = this.blocks.filter((b) => b.stmts.length !== 0);
962
- this.buildBlocksNextLast();
963
- this.addReturnStmt();
964
- }
965
- handleBuilder(stmts) {
966
- let lastStmt = this.entry;
967
- for (const stmt of stmts) {
968
- const stmtBuilder = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
969
- lastStmt.next = stmtBuilder;
970
- stmtBuilder.lasts.add(lastStmt);
971
- lastStmt = stmtBuilder;
972
- }
973
- lastStmt.next = this.exit;
974
- this.exit.lasts.add(lastStmt);
975
- }
976
- isBodyEmpty() {
977
- return this.emptyBody;
978
- }
979
- buildCfg() {
980
- if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
981
- return this.buildCfgForSimpleArrowFunction();
982
- }
983
- return this.buildNormalCfg();
984
- }
985
- buildCfgForSimpleArrowFunction() {
986
- const stmts = [];
987
- const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
988
- arkIRTransformer.prebuildStmts().forEach(stmt => stmts.push(stmt));
989
- const expressionBodyNode = this.astRoot.body;
990
- const expressionBodyStmts = [];
991
- let { value: expressionBodyValue, valueOriginalPositions: expressionBodyPositions, stmts: tempStmts, } = arkIRTransformer.tsNodeToValueAndStmts(expressionBodyNode);
992
- tempStmts.forEach(stmt => expressionBodyStmts.push(stmt));
993
- if (IRUtils_1.IRUtils.moreThanOneAddress(expressionBodyValue)) {
994
- ({
995
- value: expressionBodyValue,
996
- valueOriginalPositions: expressionBodyPositions,
997
- stmts: tempStmts,
998
- } = arkIRTransformer.generateAssignStmtForValue(expressionBodyValue, expressionBodyPositions));
999
- tempStmts.forEach(stmt => expressionBodyStmts.push(stmt));
1000
- }
1001
- const returnStmt = new Stmt_1.ArkReturnStmt(expressionBodyValue);
1002
- returnStmt.setOperandOriginalPositions([expressionBodyPositions[0], ...expressionBodyPositions]);
1003
- expressionBodyStmts.push(returnStmt);
1004
- arkIRTransformer.mapStmtsToTsStmt(expressionBodyStmts, expressionBodyNode);
1005
- expressionBodyStmts.forEach(stmt => stmts.push(stmt));
1006
- const cfg = new Cfg_1.Cfg();
1007
- const blockInCfg = new BasicBlock_1.BasicBlock();
1008
- blockInCfg.setId(0);
1009
- stmts.forEach(stmt => {
1010
- blockInCfg.addStmt(stmt);
1011
- stmt.setCfg(cfg);
1012
- });
1013
- cfg.addBlock(blockInCfg);
1014
- cfg.setStartingStmt(stmts[0]);
1015
- return {
1016
- cfg: cfg,
1017
- locals: arkIRTransformer.getLocals(),
1018
- globals: arkIRTransformer.getGlobals(),
1019
- aliasTypeMap: arkIRTransformer.getAliasTypeMap(),
1020
- traps: [],
1021
- };
1022
- }
1023
- buildNormalCfg() {
1024
- const { blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer } = this.initializeBuild();
1025
- const { blocksContainLoopCondition, blockBuildersBeforeTry, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, } = this.processBlocks(blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer);
1026
- const currBlockId = this.blocks.length;
1027
- this.linkBasicBlocks(blockBuilderToCfgBlock);
1028
- this.adjustBlocks(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer);
1029
- const trapBuilder = new TrapBuilder_1.TrapBuilder();
1030
- const traps = trapBuilder.buildTraps(blockBuilderToCfgBlock, blockBuildersBeforeTry, arkIRTransformer, basicBlockSet);
1031
- const cfg = this.createCfg(blockBuilderToCfgBlock, basicBlockSet, currBlockId);
1032
- return {
1033
- cfg,
1034
- locals: arkIRTransformer.getLocals(),
1035
- globals: arkIRTransformer.getGlobals(),
1036
- aliasTypeMap: arkIRTransformer.getAliasTypeMap(),
1037
- traps,
1038
- };
1039
- }
1040
- initializeBuild() {
1041
- const blockBuilderToCfgBlock = new Map();
1042
- const basicBlockSet = new Set();
1043
- const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
1044
- return { blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer };
1045
- }
1046
- processBlocks(blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer) {
1047
- const blocksContainLoopCondition = new Set();
1048
- const blockBuildersBeforeTry = new Set();
1049
- const blockBuildersContainSwitch = [];
1050
- const valueAndStmtsOfSwitchAndCasesAll = [];
1051
- for (let i = 0; i < this.blocks.length; i++) {
1052
- const stmtsInBlock = [];
1053
- if (i === 0) {
1054
- arkIRTransformer.prebuildStmts().forEach(stmt => stmtsInBlock.push(stmt));
1055
- }
1056
- const stmtsCnt = this.blocks[i].stmts.length;
1057
- if (this.blocks[i].stmts[stmtsCnt - 1].type === 'tryStatement') {
1058
- blockBuildersBeforeTry.add(this.blocks[i]);
1059
- }
1060
- for (const statementBuilder of this.blocks[i].stmts) {
1061
- if (statementBuilder.type === 'loopStatement') {
1062
- blocksContainLoopCondition.add(this.blocks[i]);
1063
- }
1064
- else if (statementBuilder instanceof SwitchStatementBuilder) {
1065
- blockBuildersContainSwitch.push(this.blocks[i]);
1066
- const valueAndStmtsOfSwitchAndCases = arkIRTransformer.switchStatementToValueAndStmts(statementBuilder.astNode);
1067
- valueAndStmtsOfSwitchAndCasesAll.push(valueAndStmtsOfSwitchAndCases);
1068
- continue;
1069
- }
1070
- if (statementBuilder.astNode && statementBuilder.code !== '') {
1071
- arkIRTransformer.tsNodeToStmts(statementBuilder.astNode).forEach(s => stmtsInBlock.push(s));
1072
- }
1073
- else if (statementBuilder.code.startsWith('return')) {
1074
- stmtsInBlock.push(new Stmt_1.ArkReturnVoidStmt());
1075
- }
1076
- }
1077
- const blockInCfg = new BasicBlock_1.BasicBlock();
1078
- blockInCfg.setId(this.blocks[i].id);
1079
- for (const stmt of stmtsInBlock) {
1080
- blockInCfg.addStmt(stmt);
1081
- }
1082
- basicBlockSet.add(blockInCfg);
1083
- blockBuilderToCfgBlock.set(this.blocks[i], blockInCfg);
1084
- }
1085
- return {
1086
- blocksContainLoopCondition, blockBuildersBeforeTry, blockBuildersContainSwitch,
1087
- valueAndStmtsOfSwitchAndCasesAll,
1088
- };
1089
- }
1090
- adjustBlocks(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer) {
1091
- const loopBuilder = new LoopBuilder_1.LoopBuilder();
1092
- loopBuilder.rebuildBlocksInLoop(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet, this.blocks);
1093
- const switchBuilder = new SwitchBuilder_1.SwitchBuilder();
1094
- switchBuilder.buildSwitch(blockBuilderToCfgBlock, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer, basicBlockSet);
1095
- const conditionalBuilder = new ConditionBuilder_1.ConditionBuilder();
1096
- conditionalBuilder.rebuildBlocksContainConditionalOperator(basicBlockSet, ModelUtils_1.ModelUtils.isArkUIBuilderMethod(this.declaringMethod));
1097
- }
1098
- createCfg(blockBuilderToCfgBlock, basicBlockSet, prevBlockId) {
1099
- let currBlockId = prevBlockId;
1100
- for (const blockBuilder of this.blocks) {
1101
- if (blockBuilder.id === -1) {
1102
- blockBuilder.id = currBlockId++;
1103
- const block = blockBuilderToCfgBlock.get(blockBuilder);
1104
- block.setId(blockBuilder.id);
1105
- }
1106
- }
1107
- const cfg = new Cfg_1.Cfg();
1108
- const startingBasicBlock = blockBuilderToCfgBlock.get(this.blocks[0]);
1109
- cfg.setStartingStmt(startingBasicBlock.getStmts()[0]);
1110
- currBlockId = 0;
1111
- for (const basicBlock of basicBlockSet) {
1112
- basicBlock.setId(currBlockId++);
1113
- cfg.addBlock(basicBlock);
1114
- }
1115
- for (const stmt of cfg.getStmts()) {
1116
- stmt.setCfg(cfg);
1117
- }
1118
- return cfg;
1119
- }
1120
- linkBasicBlocks(blockBuilderToCfgBlock) {
1121
- for (const [blockBuilder, cfgBlock] of blockBuilderToCfgBlock) {
1122
- for (const successorBlockBuilder of blockBuilder.nexts) {
1123
- if (!blockBuilderToCfgBlock.get(successorBlockBuilder)) {
1124
- continue;
1125
- }
1126
- const successorBlock = blockBuilderToCfgBlock.get(successorBlockBuilder);
1127
- cfgBlock.addSuccessorBlock(successorBlock);
1128
- }
1129
- for (const predecessorBlockBuilder of blockBuilder.lasts) {
1130
- if (!blockBuilderToCfgBlock.get(predecessorBlockBuilder)) {
1131
- continue;
1132
- }
1133
- const predecessorBlock = blockBuilderToCfgBlock.get(predecessorBlockBuilder);
1134
- cfgBlock.addPredecessorBlock(predecessorBlock);
1135
- }
1136
- }
1137
- }
1138
- }
1139
- exports.CfgBuilder = CfgBuilder;
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024-2025 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 = exports.BlockBuilder = exports.TryStatementBuilder = exports.SwitchStatementBuilder = void 0;
41
+ const ts = __importStar(require("ohos-typescript"));
42
+ const Stmt_1 = require("../../base/Stmt");
43
+ const BasicBlock_1 = require("../BasicBlock");
44
+ const Cfg_1 = require("../Cfg");
45
+ const ArkIRTransformer_1 = require("../../common/ArkIRTransformer");
46
+ const ModelUtils_1 = require("../../common/ModelUtils");
47
+ const IRUtils_1 = require("../../common/IRUtils");
48
+ const LoopBuilder_1 = require("./LoopBuilder");
49
+ const SwitchBuilder_1 = require("./SwitchBuilder");
50
+ const ConditionBuilder_1 = require("./ConditionBuilder");
51
+ const TrapBuilder_1 = require("./TrapBuilder");
52
+ class StatementBuilder {
53
+ constructor(type, code, astNode, scopeID) {
54
+ this.addressCode3 = [];
55
+ this.passTmies = 0;
56
+ this.numOfIdentifier = 0;
57
+ this.isDoWhile = false;
58
+ this.type = type;
59
+ this.code = code;
60
+ this.next = null;
61
+ this.lasts = new Set();
62
+ this.walked = false;
63
+ this.index = 0;
64
+ this.line = -1;
65
+ this.column = -1;
66
+ this.astNode = astNode;
67
+ this.scopeID = scopeID;
68
+ this.block = null;
69
+ this.ifExitPass = false;
70
+ }
71
+ }
72
+ class ConditionStatementBuilder extends StatementBuilder {
73
+ constructor(type, code, astNode, scopeID) {
74
+ super(type, code, astNode, scopeID);
75
+ this.doStatement = null;
76
+ this.nextT = null;
77
+ this.nextF = null;
78
+ this.loopBlock = null;
79
+ this.condition = '';
80
+ }
81
+ }
82
+ class SwitchStatementBuilder extends StatementBuilder {
83
+ constructor(type, code, astNode, scopeID) {
84
+ super(type, code, astNode, scopeID);
85
+ this.cases = [];
86
+ this.default = null;
87
+ this.afterSwitch = null;
88
+ this.nexts = [];
89
+ }
90
+ }
91
+ exports.SwitchStatementBuilder = SwitchStatementBuilder;
92
+ class TryStatementBuilder extends StatementBuilder {
93
+ constructor(type, code, astNode, scopeID) {
94
+ super(type, code, astNode, scopeID);
95
+ this.tryFirst = null;
96
+ this.tryExit = null;
97
+ this.catchStatement = null;
98
+ this.catchError = '';
99
+ this.finallyStatement = null;
100
+ this.afterFinal = null;
101
+ }
102
+ }
103
+ exports.TryStatementBuilder = TryStatementBuilder;
104
+ class Case {
105
+ constructor(value, stmt) {
106
+ this.value = value;
107
+ this.stmt = stmt;
108
+ }
109
+ }
110
+ class DefUseChain {
111
+ constructor(def, use) {
112
+ this.def = def;
113
+ this.use = use;
114
+ }
115
+ }
116
+ class Variable {
117
+ constructor(name, lastDef) {
118
+ this.properties = [];
119
+ this.propOf = null;
120
+ this.name = name;
121
+ this.lastDef = lastDef;
122
+ this.defUse = [];
123
+ }
124
+ }
125
+ class Scope {
126
+ constructor(id) {
127
+ this.id = id;
128
+ }
129
+ }
130
+ class BlockBuilder {
131
+ constructor(id, stmts) {
132
+ this.nexts = [];
133
+ this.lasts = [];
134
+ this.walked = false;
135
+ this.id = id;
136
+ this.stmts = stmts;
137
+ }
138
+ }
139
+ exports.BlockBuilder = BlockBuilder;
140
+ class Catch {
141
+ constructor(errorName, from, to, withLabel) {
142
+ this.errorName = errorName;
143
+ this.from = from;
144
+ this.to = to;
145
+ this.withLabel = withLabel;
146
+ }
147
+ }
148
+ class TextError extends Error {
149
+ constructor(message) {
150
+ // 调用父类的构造函数,并传入错误消息
151
+ super(message);
152
+ // 设置错误类型的名称
153
+ this.name = 'TextError';
154
+ }
155
+ }
156
+ class CfgBuilder {
157
+ constructor(ast, name, declaringMethod, sourceFile) {
158
+ this.exits = [];
159
+ this.emptyBody = false;
160
+ this.arrowFunctionWithoutBlock = false;
161
+ this.name = name;
162
+ this.astRoot = ast;
163
+ this.declaringMethod = declaringMethod;
164
+ this.declaringClass = declaringMethod.getDeclaringArkClass();
165
+ this.entry = new StatementBuilder('entry', '', ast, 0);
166
+ this.loopStack = [];
167
+ this.switchExitStack = [];
168
+ this.functions = [];
169
+ this.breakin = '';
170
+ this.statementArray = [];
171
+ this.dotEdges = [];
172
+ this.exit = new StatementBuilder('exit', 'return;', null, 0);
173
+ this.scopes = [];
174
+ this.tempVariableNum = 0;
175
+ this.current3ACstm = this.entry;
176
+ this.blocks = [];
177
+ this.currentDeclarationKeyword = '';
178
+ this.variables = [];
179
+ this.importFromPath = [];
180
+ this.catches = [];
181
+ this.sourceFile = sourceFile;
182
+ this.arrowFunctionWithoutBlock = true;
183
+ }
184
+ getDeclaringMethod() {
185
+ return this.declaringMethod;
186
+ }
187
+ judgeLastType(s, lastStatement) {
188
+ if (lastStatement.type === 'ifStatement') {
189
+ let lastIf = lastStatement;
190
+ if (lastIf.nextT == null) {
191
+ lastIf.nextT = s;
192
+ s.lasts.add(lastIf);
193
+ }
194
+ else {
195
+ lastIf.nextF = s;
196
+ s.lasts.add(lastIf);
197
+ }
198
+ }
199
+ else if (lastStatement.type === 'loopStatement') {
200
+ let lastLoop = lastStatement;
201
+ lastLoop.nextT = s;
202
+ s.lasts.add(lastLoop);
203
+ }
204
+ else if (lastStatement.type === 'catchOrNot') {
205
+ let lastLoop = lastStatement;
206
+ lastLoop.nextT = s;
207
+ s.lasts.add(lastLoop);
208
+ }
209
+ else {
210
+ lastStatement.next = s;
211
+ s.lasts.add(lastStatement);
212
+ }
213
+ }
214
+ ASTNodeBreakStatement(c, lastStatement) {
215
+ let p = c;
216
+ while (p && p !== this.astRoot) {
217
+ if (ts.isWhileStatement(p) || ts.isDoStatement(p) || ts.isForStatement(p) || ts.isForInStatement(p) || ts.isForOfStatement(p)) {
218
+ const lastLoopNextF = this.loopStack[this.loopStack.length - 1].nextF;
219
+ this.judgeLastType(lastLoopNextF, lastStatement);
220
+ lastLoopNextF.lasts.add(lastStatement);
221
+ return;
222
+ }
223
+ if (ts.isCaseClause(p) || ts.isDefaultClause(p)) {
224
+ const lastSwitchExit = this.switchExitStack[this.switchExitStack.length - 1];
225
+ this.judgeLastType(lastSwitchExit, lastStatement);
226
+ lastSwitchExit.lasts.add(lastStatement);
227
+ return;
228
+ }
229
+ p = p.parent;
230
+ }
231
+ }
232
+ ASTNodeIfStatement(c, lastStatement, scopeID) {
233
+ let ifstm = new ConditionStatementBuilder('ifStatement', '', c, scopeID);
234
+ this.judgeLastType(ifstm, lastStatement);
235
+ let ifexit = new StatementBuilder('ifExit', '', c, scopeID);
236
+ this.exits.push(ifexit);
237
+ ifstm.condition = c.expression.getText(this.sourceFile);
238
+ ifstm.code = 'if (' + ifstm.condition + ')';
239
+ if (ts.isBlock(c.thenStatement)) {
240
+ this.walkAST(ifstm, ifexit, [...c.thenStatement.statements]);
241
+ }
242
+ else {
243
+ this.walkAST(ifstm, ifexit, [c.thenStatement]);
244
+ }
245
+ if (c.elseStatement) {
246
+ if (ts.isBlock(c.elseStatement)) {
247
+ this.walkAST(ifstm, ifexit, [...c.elseStatement.statements]);
248
+ }
249
+ else {
250
+ this.walkAST(ifstm, ifexit, [c.elseStatement]);
251
+ }
252
+ }
253
+ if (!ifstm.nextT) {
254
+ ifstm.nextT = ifexit;
255
+ ifexit.lasts.add(ifstm);
256
+ }
257
+ if (!ifstm.nextF) {
258
+ ifstm.nextF = ifexit;
259
+ ifexit.lasts.add(ifstm);
260
+ }
261
+ return ifexit;
262
+ }
263
+ ASTNodeWhileStatement(c, lastStatement, scopeID) {
264
+ this.breakin = 'loop';
265
+ let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
266
+ this.loopStack.push(loopstm);
267
+ this.judgeLastType(loopstm, lastStatement);
268
+ let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
269
+ this.exits.push(loopExit);
270
+ loopstm.nextF = loopExit;
271
+ loopExit.lasts.add(loopstm);
272
+ loopstm.condition = c.expression.getText(this.sourceFile);
273
+ loopstm.code = 'while (' + loopstm.condition + ')';
274
+ if (ts.isBlock(c.statement)) {
275
+ this.walkAST(loopstm, loopstm, [...c.statement.statements]);
276
+ }
277
+ else {
278
+ this.walkAST(loopstm, loopstm, [c.statement]);
279
+ }
280
+ if (!loopstm.nextF) {
281
+ loopstm.nextF = loopExit;
282
+ loopExit.lasts.add(loopstm);
283
+ }
284
+ if (!loopstm.nextT) {
285
+ loopstm.nextT = loopExit;
286
+ loopExit.lasts.add(loopstm);
287
+ }
288
+ this.loopStack.pop();
289
+ return loopExit;
290
+ }
291
+ ASTNodeForStatement(c, lastStatement, scopeID) {
292
+ var _a, _b, _c, _d, _e;
293
+ this.breakin = 'loop';
294
+ let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
295
+ this.loopStack.push(loopstm);
296
+ this.judgeLastType(loopstm, lastStatement);
297
+ let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
298
+ this.exits.push(loopExit);
299
+ loopstm.nextF = loopExit;
300
+ loopExit.lasts.add(loopstm);
301
+ loopstm.code = 'for (';
302
+ if (ts.isForStatement(c)) {
303
+ 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));
304
+ }
305
+ else if (ts.isForOfStatement(c)) {
306
+ loopstm.code += ((_d = c.initializer) === null || _d === void 0 ? void 0 : _d.getText(this.sourceFile)) + ' of ' + c.expression.getText(this.sourceFile);
307
+ }
308
+ else {
309
+ loopstm.code += ((_e = c.initializer) === null || _e === void 0 ? void 0 : _e.getText(this.sourceFile)) + ' in ' + c.expression.getText(this.sourceFile);
310
+ }
311
+ loopstm.code += ')';
312
+ if (ts.isBlock(c.statement)) {
313
+ this.walkAST(loopstm, loopstm, [...c.statement.statements]);
314
+ }
315
+ else {
316
+ this.walkAST(loopstm, loopstm, [c.statement]);
317
+ }
318
+ if (!loopstm.nextF) {
319
+ loopstm.nextF = loopExit;
320
+ loopExit.lasts.add(loopstm);
321
+ }
322
+ if (!loopstm.nextT) {
323
+ loopstm.nextT = loopExit;
324
+ loopExit.lasts.add(loopstm);
325
+ }
326
+ this.loopStack.pop();
327
+ return loopExit;
328
+ }
329
+ ASTNodeDoStatement(c, lastStatement, scopeID) {
330
+ var _a, _b;
331
+ this.breakin = 'loop';
332
+ let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
333
+ this.loopStack.push(loopstm);
334
+ let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
335
+ this.exits.push(loopExit);
336
+ loopstm.nextF = loopExit;
337
+ loopExit.lasts.add(loopstm);
338
+ loopstm.condition = c.expression.getText(this.sourceFile);
339
+ loopstm.code = 'while (' + loopstm.condition + ')';
340
+ loopstm.isDoWhile = true;
341
+ if (ts.isBlock(c.statement)) {
342
+ this.walkAST(lastStatement, loopstm, [...c.statement.statements]);
343
+ }
344
+ else {
345
+ this.walkAST(lastStatement, loopstm, [c.statement]);
346
+ }
347
+ let lastType = lastStatement.type;
348
+ if (lastType === 'ifStatement' || lastType === 'loopStatement') {
349
+ let lastCondition = lastStatement;
350
+ loopstm.nextT = lastCondition.nextT;
351
+ (_a = lastCondition.nextT) === null || _a === void 0 ? void 0 : _a.lasts.add(loopstm);
352
+ }
353
+ else {
354
+ loopstm.nextT = lastStatement.next;
355
+ (_b = lastStatement.next) === null || _b === void 0 ? void 0 : _b.lasts.add(loopstm);
356
+ }
357
+ if (loopstm.nextT && loopstm.nextT !== loopstm) {
358
+ loopstm.nextT.isDoWhile = true;
359
+ loopstm.doStatement = loopstm.nextT;
360
+ }
361
+ this.loopStack.pop();
362
+ return loopExit;
363
+ }
364
+ ASTNodeSwitchStatement(c, lastStatement, scopeID) {
365
+ var _a;
366
+ this.breakin = 'switch';
367
+ let switchstm = new SwitchStatementBuilder('switchStatement', '', c, scopeID);
368
+ this.judgeLastType(switchstm, lastStatement);
369
+ let switchExit = new StatementBuilder('switchExit', '', null, scopeID);
370
+ this.exits.push(switchExit);
371
+ this.switchExitStack.push(switchExit);
372
+ switchExit.lasts.add(switchstm);
373
+ switchstm.code = 'switch (' + c.expression + ')';
374
+ let lastCaseExit = null;
375
+ for (let i = 0; i < c.caseBlock.clauses.length; i++) {
376
+ const clause = c.caseBlock.clauses[i];
377
+ let casestm;
378
+ if (ts.isCaseClause(clause)) {
379
+ casestm = new StatementBuilder('statement', 'case ' + clause.expression.getText(this.sourceFile) + ':', clause, scopeID);
380
+ }
381
+ else {
382
+ casestm = new StatementBuilder('statement', 'default:', clause, scopeID);
383
+ }
384
+ switchstm.nexts.push(casestm);
385
+ casestm.lasts.add(switchstm);
386
+ let caseExit = new StatementBuilder('caseExit', '', null, scopeID);
387
+ this.exits.push(caseExit);
388
+ this.walkAST(casestm, caseExit, [...clause.statements]);
389
+ if (ts.isCaseClause(clause)) {
390
+ const cas = new Case(casestm.code, casestm.next);
391
+ switchstm.cases.push(cas);
392
+ }
393
+ else {
394
+ switchstm.default = casestm.next;
395
+ }
396
+ switchstm.nexts[switchstm.nexts.length - 1] = casestm.next;
397
+ for (const stmt of [...casestm.lasts]) {
398
+ casestm.next.lasts.add(stmt);
399
+ }
400
+ casestm.next.lasts.delete(casestm);
401
+ if (lastCaseExit) {
402
+ lastCaseExit.next = casestm.next;
403
+ (_a = casestm.next) === null || _a === void 0 ? void 0 : _a.lasts.add(lastCaseExit);
404
+ }
405
+ lastCaseExit = caseExit;
406
+ if (i === c.caseBlock.clauses.length - 1) {
407
+ caseExit.next = switchExit;
408
+ switchExit.lasts.add(caseExit);
409
+ }
410
+ }
411
+ this.switchExitStack.pop();
412
+ return switchExit;
413
+ }
414
+ ASTNodeTryStatement(c, lastStatement, scopeID) {
415
+ var _a, _b;
416
+ let trystm = new TryStatementBuilder('tryStatement', 'try', c, scopeID);
417
+ this.judgeLastType(trystm, lastStatement);
418
+ let tryExit = new StatementBuilder('tryExit', '', c, scopeID);
419
+ this.exits.push(tryExit);
420
+ trystm.tryExit = tryExit;
421
+ this.walkAST(trystm, tryExit, [...c.tryBlock.statements]);
422
+ trystm.tryFirst = trystm.next;
423
+ (_a = trystm.next) === null || _a === void 0 ? void 0 : _a.lasts.add(trystm);
424
+ if (c.catchClause) {
425
+ let text = 'catch';
426
+ if (c.catchClause.variableDeclaration) {
427
+ text += '(' + c.catchClause.variableDeclaration.getText(this.sourceFile) + ')';
428
+ }
429
+ let catchOrNot = new ConditionStatementBuilder('catchOrNot', text, c, scopeID);
430
+ let catchExit = new StatementBuilder('catch exit', '', c, scopeID);
431
+ catchOrNot.nextF = catchExit;
432
+ catchExit.lasts.add(catchOrNot);
433
+ this.walkAST(catchOrNot, catchExit, [...c.catchClause.block.statements]);
434
+ if (!catchOrNot.nextT) {
435
+ catchOrNot.nextT = catchExit;
436
+ catchExit.lasts.add(catchOrNot);
437
+ }
438
+ const catchStatement = new StatementBuilder('statement', catchOrNot.code, c.catchClause, catchOrNot.nextT.scopeID);
439
+ catchStatement.next = catchOrNot.nextT;
440
+ trystm.catchStatement = catchStatement;
441
+ catchStatement.lasts.add(trystm);
442
+ if (c.catchClause.variableDeclaration) {
443
+ trystm.catchError = c.catchClause.variableDeclaration.getText(this.sourceFile);
444
+ }
445
+ else {
446
+ trystm.catchError = 'Error';
447
+ }
448
+ }
449
+ let final = new StatementBuilder('statement', 'finally', c, scopeID);
450
+ let finalExit = new StatementBuilder('finallyExit', '', c, scopeID);
451
+ this.exits.push(finalExit);
452
+ if (c.finallyBlock && c.finallyBlock.statements.length > 0) {
453
+ this.walkAST(final, finalExit, [...c.finallyBlock.statements]);
454
+ }
455
+ else {
456
+ let dummyFinally = new StatementBuilder('statement', 'dummyFinally', c, (new Scope(this.scopes.length)).id);
457
+ final.next = dummyFinally;
458
+ dummyFinally.lasts.add(final);
459
+ dummyFinally.next = finalExit;
460
+ finalExit.lasts.add(dummyFinally);
461
+ }
462
+ trystm.finallyStatement = final.next;
463
+ tryExit.next = final.next;
464
+ (_b = final.next) === null || _b === void 0 ? void 0 : _b.lasts.add(tryExit);
465
+ trystm.next = finalExit;
466
+ finalExit.lasts.add(trystm);
467
+ return finalExit;
468
+ }
469
+ walkAST(lastStatement, nextStatement, nodes) {
470
+ let scope = new Scope(this.scopes.length);
471
+ this.scopes.push(scope);
472
+ for (let i = 0; i < nodes.length; i++) {
473
+ let c = nodes[i];
474
+ if (ts.isVariableStatement(c) || ts.isExpressionStatement(c) || ts.isThrowStatement(c) || ts.isTypeAliasDeclaration(c)) {
475
+ let s = new StatementBuilder('statement', c.getText(this.sourceFile), c, scope.id);
476
+ this.judgeLastType(s, lastStatement);
477
+ lastStatement = s;
478
+ }
479
+ else if (!this.declaringMethod.isDefaultArkMethod() && ts.isFunctionDeclaration(c)) {
480
+ let s = new StatementBuilder('functionDeclarationStatement', c.getText(this.sourceFile), c, scope.id);
481
+ this.judgeLastType(s, lastStatement);
482
+ lastStatement = s;
483
+ }
484
+ else if (!this.declaringMethod.isDefaultArkMethod() && ts.isClassDeclaration(c)) {
485
+ let s = new StatementBuilder('classDeclarationStatement', c.getText(this.sourceFile), c, scope.id);
486
+ this.judgeLastType(s, lastStatement);
487
+ lastStatement = s;
488
+ }
489
+ else if (ts.isReturnStatement(c)) {
490
+ let s = new StatementBuilder('returnStatement', c.getText(this.sourceFile), c, scope.id);
491
+ this.judgeLastType(s, lastStatement);
492
+ lastStatement = s;
493
+ break;
494
+ }
495
+ else if (ts.isBreakStatement(c)) {
496
+ this.ASTNodeBreakStatement(c, lastStatement);
497
+ return;
498
+ }
499
+ else if (ts.isContinueStatement(c)) {
500
+ const lastLoop = this.loopStack[this.loopStack.length - 1];
501
+ this.judgeLastType(lastLoop, lastStatement);
502
+ lastLoop.lasts.add(lastStatement);
503
+ return;
504
+ }
505
+ else if (ts.isIfStatement(c)) {
506
+ lastStatement = this.ASTNodeIfStatement(c, lastStatement, scope.id);
507
+ }
508
+ else if (ts.isWhileStatement(c)) {
509
+ lastStatement = this.ASTNodeWhileStatement(c, lastStatement, scope.id);
510
+ }
511
+ if (ts.isForStatement(c) || ts.isForInStatement(c) || ts.isForOfStatement(c)) {
512
+ lastStatement = this.ASTNodeForStatement(c, lastStatement, scope.id);
513
+ }
514
+ else if (ts.isDoStatement(c)) {
515
+ lastStatement = this.ASTNodeDoStatement(c, lastStatement, scope.id);
516
+ }
517
+ else if (ts.isSwitchStatement(c)) {
518
+ lastStatement = this.ASTNodeSwitchStatement(c, lastStatement, scope.id);
519
+ }
520
+ else if (ts.isBlock(c)) {
521
+ let blockExit = new StatementBuilder('blockExit', '', c, scope.id);
522
+ this.exits.push(blockExit);
523
+ this.walkAST(lastStatement, blockExit, c.getChildren(this.sourceFile)[1].getChildren(this.sourceFile));
524
+ lastStatement = blockExit;
525
+ }
526
+ else if (ts.isTryStatement(c)) {
527
+ lastStatement = this.ASTNodeTryStatement(c, lastStatement, scope.id);
528
+ }
529
+ else if (ts.isExportAssignment(c)) {
530
+ if (ts.isNewExpression(c.expression) || ts.isObjectLiteralExpression(c.expression)) {
531
+ let s = new StatementBuilder('statement', c.getText(this.sourceFile), c, scope.id);
532
+ this.judgeLastType(s, lastStatement);
533
+ lastStatement = s;
534
+ }
535
+ }
536
+ }
537
+ if (lastStatement.type !== 'breakStatement' && lastStatement.type !== 'continueStatement' && lastStatement.type !== 'returnStatement') {
538
+ lastStatement.next = nextStatement;
539
+ nextStatement.lasts.add(lastStatement);
540
+ }
541
+ }
542
+ addReturnInEmptyMethod() {
543
+ if (this.entry.next === this.exit) {
544
+ const ret = new StatementBuilder('returnStatement', 'return;', null, this.entry.scopeID);
545
+ this.entry.next = ret;
546
+ ret.lasts.add(this.entry);
547
+ ret.next = this.exit;
548
+ this.exit.lasts = new Set([ret]);
549
+ }
550
+ }
551
+ deleteExitAfterCondition(last, exit) {
552
+ if (last.nextT === exit) {
553
+ last.nextT = exit.next;
554
+ const lasts = exit.next.lasts;
555
+ lasts.delete(exit);
556
+ lasts.add(last);
557
+ }
558
+ else if (last.nextF === exit) {
559
+ last.nextF = exit.next;
560
+ const lasts = exit.next.lasts;
561
+ lasts.delete(exit);
562
+ lasts.add(last);
563
+ }
564
+ }
565
+ deleteExitAfterSwitch(last, exit) {
566
+ var _a;
567
+ if (exit.type === 'switchExit') {
568
+ last.afterSwitch = exit.next;
569
+ }
570
+ exit.next.lasts.delete(exit);
571
+ last.nexts = last.nexts.filter(item => item !== exit);
572
+ if (last.nexts.length === 0) {
573
+ last.next = exit.next;
574
+ (_a = exit.next) === null || _a === void 0 ? void 0 : _a.lasts.add(last);
575
+ }
576
+ }
577
+ deleteExit() {
578
+ for (const exit of this.exits) {
579
+ const lasts = [...exit.lasts];
580
+ for (const last of lasts) {
581
+ if (last instanceof ConditionStatementBuilder) {
582
+ this.deleteExitAfterCondition(last, exit);
583
+ }
584
+ else if (last instanceof SwitchStatementBuilder) {
585
+ this.deleteExitAfterSwitch(last, exit);
586
+ }
587
+ else if (last instanceof TryStatementBuilder && exit.type === 'finallyExit') {
588
+ last.afterFinal = exit.next;
589
+ last.next = last.tryFirst;
590
+ exit.lasts.delete(last);
591
+ }
592
+ else {
593
+ last.next = exit.next;
594
+ const lasts = exit.next.lasts;
595
+ lasts.delete(exit);
596
+ lasts.add(last);
597
+ }
598
+ }
599
+ }
600
+ // 部分语句例如return后面的exit语句的next无法在上面清除
601
+ for (const exit of this.exits) {
602
+ if (exit.next && exit.next.lasts.has(exit)) {
603
+ exit.next.lasts.delete(exit);
604
+ }
605
+ }
606
+ }
607
+ addStmt2BlockStmtQueueInSpecialCase(stmt, stmtQueue) {
608
+ if (stmt.next) {
609
+ if ((stmt.type === 'continueStatement' || stmt.next.type === 'loopStatement') && stmt.next.block || stmt.next.type.includes('exit')) {
610
+ return null;
611
+ }
612
+ stmt.next.passTmies++;
613
+ if (stmt.next.passTmies === stmt.next.lasts.size || stmt.next.type === 'loopStatement' || stmt.next.isDoWhile) {
614
+ if (stmt.next.scopeID !== stmt.scopeID && !(stmt.next instanceof ConditionStatementBuilder && stmt.next.doStatement) &&
615
+ !(ts.isCaseClause(stmt.astNode) || ts.isDefaultClause(stmt.astNode))) {
616
+ stmtQueue.push(stmt.next);
617
+ return null;
618
+ }
619
+ return stmt.next;
620
+ }
621
+ }
622
+ return null;
623
+ }
624
+ addStmt2BlockStmtQueue(stmt, stmtQueue) {
625
+ if (stmt instanceof ConditionStatementBuilder) {
626
+ stmtQueue.push(stmt.nextF);
627
+ stmtQueue.push(stmt.nextT);
628
+ }
629
+ else if (stmt instanceof SwitchStatementBuilder) {
630
+ if (stmt.nexts.length === 0) {
631
+ stmtQueue.push(stmt.afterSwitch);
632
+ }
633
+ for (let i = stmt.nexts.length - 1; i >= 0; i--) {
634
+ stmtQueue.push(stmt.nexts[i]);
635
+ }
636
+ }
637
+ else if (stmt instanceof TryStatementBuilder) {
638
+ if (stmt.finallyStatement) {
639
+ stmtQueue.push(stmt.finallyStatement);
640
+ }
641
+ if (stmt.catchStatement) {
642
+ stmtQueue.push(stmt.catchStatement);
643
+ }
644
+ if (stmt.tryFirst) {
645
+ stmtQueue.push(stmt.tryFirst);
646
+ }
647
+ }
648
+ else if (stmt.next) {
649
+ return this.addStmt2BlockStmtQueueInSpecialCase(stmt, stmtQueue);
650
+ }
651
+ return null;
652
+ }
653
+ buildBlocks() {
654
+ const stmtQueue = [this.entry];
655
+ const handledStmts = new Set();
656
+ while (stmtQueue.length > 0) {
657
+ let stmt = stmtQueue.pop();
658
+ if (stmt.type.includes('exit')) {
659
+ continue;
660
+ }
661
+ if (handledStmts.has(stmt)) {
662
+ continue;
663
+ }
664
+ const block = new BlockBuilder(this.blocks.length, []);
665
+ this.blocks.push(block);
666
+ while (stmt && !handledStmts.has(stmt)) {
667
+ if (stmt.type === 'loopStatement' && block.stmts.length > 0 && !stmt.isDoWhile) {
668
+ stmtQueue.push(stmt);
669
+ break;
670
+ }
671
+ if (stmt.type.includes('Exit')) {
672
+ break;
673
+ }
674
+ block.stmts.push(stmt);
675
+ stmt.block = block;
676
+ handledStmts.add(stmt);
677
+ const addRet = this.addStmt2BlockStmtQueue(stmt, stmtQueue);
678
+ if (addRet instanceof StatementBuilder) {
679
+ stmt = addRet;
680
+ }
681
+ else {
682
+ break;
683
+ }
684
+ }
685
+ }
686
+ }
687
+ buildConditionNextBlocks(originStatement, block, isLastStatement) {
688
+ var _a, _b, _c, _d;
689
+ let nextT = (_a = originStatement.nextT) === null || _a === void 0 ? void 0 : _a.block;
690
+ if (nextT && (isLastStatement || nextT !== block) && !((_b = originStatement.nextT) === null || _b === void 0 ? void 0 : _b.type.includes(' exit'))) {
691
+ block.nexts.push(nextT);
692
+ nextT.lasts.push(block);
693
+ }
694
+ let nextF = (_c = originStatement.nextF) === null || _c === void 0 ? void 0 : _c.block;
695
+ if (nextF && (isLastStatement || nextF !== block) && !((_d = originStatement.nextF) === null || _d === void 0 ? void 0 : _d.type.includes(' exit'))) {
696
+ block.nexts.push(nextF);
697
+ nextF.lasts.push(block);
698
+ }
699
+ }
700
+ buildSwitchNextBlocks(originStatement, block, isLastStatement) {
701
+ if (originStatement.nexts.length === 0) {
702
+ const nextBlock = originStatement.afterSwitch.block;
703
+ if (nextBlock && (isLastStatement || nextBlock !== block)) {
704
+ block.nexts.push(nextBlock);
705
+ nextBlock.lasts.push(block);
706
+ }
707
+ }
708
+ for (const next of originStatement.nexts) {
709
+ const nextBlock = next.block;
710
+ if (nextBlock && (isLastStatement || nextBlock !== block)) {
711
+ block.nexts.push(nextBlock);
712
+ nextBlock.lasts.push(block);
713
+ }
714
+ }
715
+ }
716
+ buildNormalNextBlocks(originStatement, block, isLastStatement) {
717
+ var _a, _b;
718
+ let next = (_a = originStatement.next) === null || _a === void 0 ? void 0 : _a.block;
719
+ if (next && (isLastStatement || next !== block) && !((_b = originStatement.next) === null || _b === void 0 ? void 0 : _b.type.includes(' exit'))) {
720
+ block.nexts.push(next);
721
+ next.lasts.push(block);
722
+ }
723
+ }
724
+ buildBlocksNextLast() {
725
+ for (let block of this.blocks) {
726
+ for (let originStatement of block.stmts) {
727
+ let isLastStatement = (block.stmts.indexOf(originStatement) === block.stmts.length - 1);
728
+ if (originStatement instanceof ConditionStatementBuilder) {
729
+ this.buildConditionNextBlocks(originStatement, block, isLastStatement);
730
+ }
731
+ else if (originStatement instanceof SwitchStatementBuilder) {
732
+ this.buildSwitchNextBlocks(originStatement, block, isLastStatement);
733
+ }
734
+ else {
735
+ this.buildNormalNextBlocks(originStatement, block, isLastStatement);
736
+ }
737
+ }
738
+ }
739
+ }
740
+ addReturnBlock(returnStatement, notReturnStmts) {
741
+ var _a, _b, _c;
742
+ let returnBlock = new BlockBuilder(this.blocks.length, [returnStatement]);
743
+ returnStatement.block = returnBlock;
744
+ this.blocks.push(returnBlock);
745
+ for (const notReturnStmt of notReturnStmts) {
746
+ if (notReturnStmt instanceof ConditionStatementBuilder) {
747
+ if (this.exit === notReturnStmt.nextT) {
748
+ notReturnStmt.nextT = returnStatement;
749
+ (_a = notReturnStmt.block) === null || _a === void 0 ? void 0 : _a.nexts.splice(0, 0, returnBlock);
750
+ }
751
+ else if (this.exit === notReturnStmt.nextF) {
752
+ notReturnStmt.nextF = returnStatement;
753
+ (_b = notReturnStmt.block) === null || _b === void 0 ? void 0 : _b.nexts.push(returnBlock);
754
+ }
755
+ }
756
+ else {
757
+ notReturnStmt.next = returnStatement;
758
+ (_c = notReturnStmt.block) === null || _c === void 0 ? void 0 : _c.nexts.push(returnBlock);
759
+ }
760
+ returnStatement.lasts.add(notReturnStmt);
761
+ returnStatement.next = this.exit;
762
+ const lasts = [...this.exit.lasts];
763
+ lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
764
+ this.exit.lasts = new Set(lasts);
765
+ returnBlock.lasts.push(notReturnStmt.block);
766
+ }
767
+ this.exit.block = returnBlock;
768
+ }
769
+ addReturnStmt() {
770
+ var _a;
771
+ let notReturnStmts = [];
772
+ for (let stmt of [...this.exit.lasts]) {
773
+ if (stmt.type !== 'returnStatement') {
774
+ notReturnStmts.push(stmt);
775
+ }
776
+ }
777
+ if (notReturnStmts.length < 1) {
778
+ return;
779
+ }
780
+ const returnStatement = new StatementBuilder('returnStatement', 'return;', null, this.exit.scopeID);
781
+ let TryOrSwitchExit = false;
782
+ if (notReturnStmts.length === 1 && notReturnStmts[0].block) {
783
+ let p = notReturnStmts[0].astNode;
784
+ while (p && p !== this.astRoot) {
785
+ if (ts.isTryStatement(p) || ts.isSwitchStatement(p)) {
786
+ TryOrSwitchExit = true;
787
+ break;
788
+ }
789
+ p = p.parent;
790
+ }
791
+ }
792
+ if (notReturnStmts.length === 1 && !(notReturnStmts[0] instanceof ConditionStatementBuilder) && !TryOrSwitchExit) {
793
+ const notReturnStmt = notReturnStmts[0];
794
+ notReturnStmt.next = returnStatement;
795
+ returnStatement.lasts = new Set([notReturnStmt]);
796
+ returnStatement.next = this.exit;
797
+ const lasts = [...this.exit.lasts];
798
+ lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
799
+ this.exit.lasts = new Set(lasts);
800
+ (_a = notReturnStmt.block) === null || _a === void 0 ? void 0 : _a.stmts.push(returnStatement);
801
+ returnStatement.block = notReturnStmt.block;
802
+ }
803
+ else {
804
+ this.addReturnBlock(returnStatement, notReturnStmts);
805
+ }
806
+ }
807
+ resetWalked() {
808
+ for (let stmt of this.statementArray) {
809
+ stmt.walked = false;
810
+ }
811
+ }
812
+ addStmtBuilderPosition() {
813
+ for (const stmt of this.statementArray) {
814
+ if (stmt.astNode) {
815
+ const { line, character } = ts.getLineAndCharacterOfPosition(this.sourceFile, stmt.astNode.getStart(this.sourceFile));
816
+ stmt.line = line + 1;
817
+ stmt.column = character + 1;
818
+ }
819
+ }
820
+ }
821
+ CfgBuilder2Array(stmt) {
822
+ if (stmt.walked) {
823
+ return;
824
+ }
825
+ stmt.walked = true;
826
+ stmt.index = this.statementArray.length;
827
+ if (!stmt.type.includes(' exit')) {
828
+ this.statementArray.push(stmt);
829
+ }
830
+ if (stmt.type === 'ifStatement' || stmt.type === 'loopStatement' || stmt.type === 'catchOrNot') {
831
+ let cstm = stmt;
832
+ if (cstm.nextT == null || cstm.nextF == null) {
833
+ this.errorTest(cstm);
834
+ return;
835
+ }
836
+ this.CfgBuilder2Array(cstm.nextF);
837
+ this.CfgBuilder2Array(cstm.nextT);
838
+ }
839
+ else if (stmt.type === 'switchStatement') {
840
+ let sstm = stmt;
841
+ for (let ss of sstm.nexts) {
842
+ this.CfgBuilder2Array(ss);
843
+ }
844
+ }
845
+ else if (stmt.type === 'tryStatement') {
846
+ let trystm = stmt;
847
+ if (trystm.tryFirst) {
848
+ this.CfgBuilder2Array(trystm.tryFirst);
849
+ }
850
+ if (trystm.catchStatement) {
851
+ this.CfgBuilder2Array(trystm.catchStatement);
852
+ }
853
+ if (trystm.finallyStatement) {
854
+ this.CfgBuilder2Array(trystm.finallyStatement);
855
+ }
856
+ if (trystm.next) {
857
+ this.CfgBuilder2Array(trystm.next);
858
+ }
859
+ }
860
+ else {
861
+ if (stmt.next != null) {
862
+ this.CfgBuilder2Array(stmt.next);
863
+ }
864
+ }
865
+ }
866
+ getDotEdges(stmt) {
867
+ if (this.statementArray.length === 0) {
868
+ this.CfgBuilder2Array(this.entry);
869
+ }
870
+ if (stmt.walked) {
871
+ return;
872
+ }
873
+ stmt.walked = true;
874
+ if (stmt.type === 'ifStatement' || stmt.type === 'loopStatement' || stmt.type === 'catchOrNot') {
875
+ let cstm = stmt;
876
+ if (cstm.nextT == null || cstm.nextF == null) {
877
+ this.errorTest(cstm);
878
+ return;
879
+ }
880
+ let edge = [cstm.index, cstm.nextF.index];
881
+ this.dotEdges.push(edge);
882
+ edge = [cstm.index, cstm.nextT.index];
883
+ this.dotEdges.push(edge);
884
+ this.getDotEdges(cstm.nextF);
885
+ this.getDotEdges(cstm.nextT);
886
+ }
887
+ else if (stmt.type === 'switchStatement') {
888
+ let sstm = stmt;
889
+ for (let ss of sstm.nexts) {
890
+ let edge = [sstm.index, ss.index];
891
+ this.dotEdges.push(edge);
892
+ this.getDotEdges(ss);
893
+ }
894
+ }
895
+ else {
896
+ if (stmt.next != null) {
897
+ let edge = [stmt.index, stmt.next.index];
898
+ this.dotEdges.push(edge);
899
+ this.getDotEdges(stmt.next);
900
+ }
901
+ }
902
+ }
903
+ errorTest(stmt) {
904
+ var _a, _b;
905
+ let mes = 'ifnext error ';
906
+ if ((_a = this.declaringClass) === null || _a === void 0 ? void 0 : _a.getDeclaringArkFile()) {
907
+ mes += ((_b = this.declaringClass) === null || _b === void 0 ? void 0 : _b.getDeclaringArkFile().getName()) + '.' + this.declaringClass.getName() + '.' + this.name;
908
+ }
909
+ mes += '\n' + stmt.code;
910
+ throw new TextError(mes);
911
+ }
912
+ buildStatementBuilder4ArrowFunction(stmt) {
913
+ let s = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
914
+ this.entry.next = s;
915
+ s.lasts = new Set([this.entry]);
916
+ s.next = this.exit;
917
+ this.exit.lasts = new Set([s]);
918
+ }
919
+ buildCfgBuilder() {
920
+ let stmts = [];
921
+ if (ts.isSourceFile(this.astRoot)) {
922
+ stmts = [...this.astRoot.statements];
923
+ }
924
+ else if (ts.isFunctionDeclaration(this.astRoot) || ts.isMethodDeclaration(this.astRoot) ||
925
+ ts.isConstructorDeclaration(this.astRoot) || ts.isGetAccessorDeclaration(this.astRoot) ||
926
+ ts.isSetAccessorDeclaration(this.astRoot) || ts.isFunctionExpression(this.astRoot) ||
927
+ ts.isClassStaticBlockDeclaration(this.astRoot)) {
928
+ if (this.astRoot.body) {
929
+ stmts = [...this.astRoot.body.statements];
930
+ }
931
+ else {
932
+ this.emptyBody = true;
933
+ }
934
+ }
935
+ else if (ts.isArrowFunction(this.astRoot)) {
936
+ if (ts.isBlock(this.astRoot.body)) {
937
+ stmts = [...this.astRoot.body.statements];
938
+ }
939
+ }
940
+ else if (ts.isMethodSignature(this.astRoot) || ts.isConstructSignatureDeclaration(this.astRoot) ||
941
+ ts.isCallSignatureDeclaration(this.astRoot) || ts.isFunctionTypeNode(this.astRoot)) {
942
+ this.emptyBody = true;
943
+ }
944
+ else if (ts.isModuleDeclaration(this.astRoot) && ts.isModuleBlock(this.astRoot.body)) {
945
+ stmts = [...this.astRoot.body.statements];
946
+ }
947
+ if (!ModelUtils_1.ModelUtils.isArkUIBuilderMethod(this.declaringMethod)) {
948
+ this.walkAST(this.entry, this.exit, stmts);
949
+ }
950
+ else {
951
+ this.handleBuilder(stmts);
952
+ }
953
+ if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
954
+ this.buildStatementBuilder4ArrowFunction(this.astRoot.body);
955
+ }
956
+ this.addReturnInEmptyMethod();
957
+ this.deleteExit();
958
+ this.CfgBuilder2Array(this.entry);
959
+ this.addStmtBuilderPosition();
960
+ this.buildBlocks();
961
+ this.blocks = this.blocks.filter((b) => b.stmts.length !== 0);
962
+ this.buildBlocksNextLast();
963
+ this.addReturnStmt();
964
+ }
965
+ handleBuilder(stmts) {
966
+ let lastStmt = this.entry;
967
+ for (const stmt of stmts) {
968
+ const stmtBuilder = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
969
+ lastStmt.next = stmtBuilder;
970
+ stmtBuilder.lasts.add(lastStmt);
971
+ lastStmt = stmtBuilder;
972
+ }
973
+ lastStmt.next = this.exit;
974
+ this.exit.lasts.add(lastStmt);
975
+ }
976
+ isBodyEmpty() {
977
+ return this.emptyBody;
978
+ }
979
+ buildCfg() {
980
+ if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
981
+ return this.buildCfgForSimpleArrowFunction();
982
+ }
983
+ return this.buildNormalCfg();
984
+ }
985
+ buildCfgForSimpleArrowFunction() {
986
+ const stmts = [];
987
+ const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
988
+ arkIRTransformer.prebuildStmts().forEach(stmt => stmts.push(stmt));
989
+ const expressionBodyNode = this.astRoot.body;
990
+ const expressionBodyStmts = [];
991
+ let { value: expressionBodyValue, valueOriginalPositions: expressionBodyPositions, stmts: tempStmts, } = arkIRTransformer.tsNodeToValueAndStmts(expressionBodyNode);
992
+ tempStmts.forEach(stmt => expressionBodyStmts.push(stmt));
993
+ if (IRUtils_1.IRUtils.moreThanOneAddress(expressionBodyValue)) {
994
+ ({
995
+ value: expressionBodyValue,
996
+ valueOriginalPositions: expressionBodyPositions,
997
+ stmts: tempStmts,
998
+ } = arkIRTransformer.generateAssignStmtForValue(expressionBodyValue, expressionBodyPositions));
999
+ tempStmts.forEach(stmt => expressionBodyStmts.push(stmt));
1000
+ }
1001
+ const returnStmt = new Stmt_1.ArkReturnStmt(expressionBodyValue);
1002
+ returnStmt.setOperandOriginalPositions([expressionBodyPositions[0], ...expressionBodyPositions]);
1003
+ expressionBodyStmts.push(returnStmt);
1004
+ arkIRTransformer.mapStmtsToTsStmt(expressionBodyStmts, expressionBodyNode);
1005
+ expressionBodyStmts.forEach(stmt => stmts.push(stmt));
1006
+ const cfg = new Cfg_1.Cfg();
1007
+ const blockInCfg = new BasicBlock_1.BasicBlock();
1008
+ blockInCfg.setId(0);
1009
+ stmts.forEach(stmt => {
1010
+ blockInCfg.addStmt(stmt);
1011
+ stmt.setCfg(cfg);
1012
+ });
1013
+ cfg.addBlock(blockInCfg);
1014
+ cfg.setStartingStmt(stmts[0]);
1015
+ return {
1016
+ cfg: cfg,
1017
+ locals: arkIRTransformer.getLocals(),
1018
+ globals: arkIRTransformer.getGlobals(),
1019
+ aliasTypeMap: arkIRTransformer.getAliasTypeMap(),
1020
+ traps: [],
1021
+ };
1022
+ }
1023
+ buildNormalCfg() {
1024
+ const { blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer } = this.initializeBuild();
1025
+ const { blocksContainLoopCondition, blockBuildersBeforeTry, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, } = this.processBlocks(blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer);
1026
+ const currBlockId = this.blocks.length;
1027
+ this.linkBasicBlocks(blockBuilderToCfgBlock);
1028
+ this.adjustBlocks(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer);
1029
+ const trapBuilder = new TrapBuilder_1.TrapBuilder();
1030
+ const traps = trapBuilder.buildTraps(blockBuilderToCfgBlock, blockBuildersBeforeTry, arkIRTransformer, basicBlockSet);
1031
+ const cfg = this.createCfg(blockBuilderToCfgBlock, basicBlockSet, currBlockId);
1032
+ return {
1033
+ cfg,
1034
+ locals: arkIRTransformer.getLocals(),
1035
+ globals: arkIRTransformer.getGlobals(),
1036
+ aliasTypeMap: arkIRTransformer.getAliasTypeMap(),
1037
+ traps,
1038
+ };
1039
+ }
1040
+ initializeBuild() {
1041
+ const blockBuilderToCfgBlock = new Map();
1042
+ const basicBlockSet = new Set();
1043
+ const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
1044
+ return { blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer };
1045
+ }
1046
+ processBlocks(blockBuilderToCfgBlock, basicBlockSet, arkIRTransformer) {
1047
+ const blocksContainLoopCondition = new Set();
1048
+ const blockBuildersBeforeTry = new Set();
1049
+ const blockBuildersContainSwitch = [];
1050
+ const valueAndStmtsOfSwitchAndCasesAll = [];
1051
+ for (let i = 0; i < this.blocks.length; i++) {
1052
+ const stmtsInBlock = [];
1053
+ if (i === 0) {
1054
+ arkIRTransformer.prebuildStmts().forEach(stmt => stmtsInBlock.push(stmt));
1055
+ }
1056
+ const stmtsCnt = this.blocks[i].stmts.length;
1057
+ if (this.blocks[i].stmts[stmtsCnt - 1].type === 'tryStatement') {
1058
+ blockBuildersBeforeTry.add(this.blocks[i]);
1059
+ }
1060
+ for (const statementBuilder of this.blocks[i].stmts) {
1061
+ if (statementBuilder.type === 'loopStatement') {
1062
+ blocksContainLoopCondition.add(this.blocks[i]);
1063
+ }
1064
+ else if (statementBuilder instanceof SwitchStatementBuilder) {
1065
+ blockBuildersContainSwitch.push(this.blocks[i]);
1066
+ const valueAndStmtsOfSwitchAndCases = arkIRTransformer.switchStatementToValueAndStmts(statementBuilder.astNode);
1067
+ valueAndStmtsOfSwitchAndCasesAll.push(valueAndStmtsOfSwitchAndCases);
1068
+ continue;
1069
+ }
1070
+ if (statementBuilder.astNode && statementBuilder.code !== '') {
1071
+ arkIRTransformer.tsNodeToStmts(statementBuilder.astNode).forEach(s => stmtsInBlock.push(s));
1072
+ }
1073
+ else if (statementBuilder.code.startsWith('return')) {
1074
+ stmtsInBlock.push(new Stmt_1.ArkReturnVoidStmt());
1075
+ }
1076
+ }
1077
+ const blockInCfg = new BasicBlock_1.BasicBlock();
1078
+ blockInCfg.setId(this.blocks[i].id);
1079
+ for (const stmt of stmtsInBlock) {
1080
+ blockInCfg.addStmt(stmt);
1081
+ }
1082
+ basicBlockSet.add(blockInCfg);
1083
+ blockBuilderToCfgBlock.set(this.blocks[i], blockInCfg);
1084
+ }
1085
+ return {
1086
+ blocksContainLoopCondition, blockBuildersBeforeTry, blockBuildersContainSwitch,
1087
+ valueAndStmtsOfSwitchAndCasesAll,
1088
+ };
1089
+ }
1090
+ adjustBlocks(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer) {
1091
+ const loopBuilder = new LoopBuilder_1.LoopBuilder();
1092
+ loopBuilder.rebuildBlocksInLoop(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet, this.blocks);
1093
+ const switchBuilder = new SwitchBuilder_1.SwitchBuilder();
1094
+ switchBuilder.buildSwitch(blockBuilderToCfgBlock, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer, basicBlockSet);
1095
+ const conditionalBuilder = new ConditionBuilder_1.ConditionBuilder();
1096
+ conditionalBuilder.rebuildBlocksContainConditionalOperator(basicBlockSet, ModelUtils_1.ModelUtils.isArkUIBuilderMethod(this.declaringMethod));
1097
+ }
1098
+ createCfg(blockBuilderToCfgBlock, basicBlockSet, prevBlockId) {
1099
+ let currBlockId = prevBlockId;
1100
+ for (const blockBuilder of this.blocks) {
1101
+ if (blockBuilder.id === -1) {
1102
+ blockBuilder.id = currBlockId++;
1103
+ const block = blockBuilderToCfgBlock.get(blockBuilder);
1104
+ block.setId(blockBuilder.id);
1105
+ }
1106
+ }
1107
+ const cfg = new Cfg_1.Cfg();
1108
+ const startingBasicBlock = blockBuilderToCfgBlock.get(this.blocks[0]);
1109
+ cfg.setStartingStmt(startingBasicBlock.getStmts()[0]);
1110
+ currBlockId = 0;
1111
+ for (const basicBlock of basicBlockSet) {
1112
+ basicBlock.setId(currBlockId++);
1113
+ cfg.addBlock(basicBlock);
1114
+ }
1115
+ for (const stmt of cfg.getStmts()) {
1116
+ stmt.setCfg(cfg);
1117
+ }
1118
+ return cfg;
1119
+ }
1120
+ linkBasicBlocks(blockBuilderToCfgBlock) {
1121
+ for (const [blockBuilder, cfgBlock] of blockBuilderToCfgBlock) {
1122
+ for (const successorBlockBuilder of blockBuilder.nexts) {
1123
+ if (!blockBuilderToCfgBlock.get(successorBlockBuilder)) {
1124
+ continue;
1125
+ }
1126
+ const successorBlock = blockBuilderToCfgBlock.get(successorBlockBuilder);
1127
+ cfgBlock.addSuccessorBlock(successorBlock);
1128
+ }
1129
+ for (const predecessorBlockBuilder of blockBuilder.lasts) {
1130
+ if (!blockBuilderToCfgBlock.get(predecessorBlockBuilder)) {
1131
+ continue;
1132
+ }
1133
+ const predecessorBlock = blockBuilderToCfgBlock.get(predecessorBlockBuilder);
1134
+ cfgBlock.addPredecessorBlock(predecessorBlock);
1135
+ }
1136
+ }
1137
+ }
1138
+ }
1139
+ exports.CfgBuilder = CfgBuilder;