arkanalyzer 1.0.20 → 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 (279) hide show
  1. package/lib/Config.d.ts +70 -70
  2. package/lib/Config.js +198 -198
  3. package/lib/Scene.d.ts +316 -316
  4. package/lib/Scene.js +1316 -1316
  5. package/lib/callgraph/algorithm/AbstractAnalysis.d.ts +28 -28
  6. package/lib/callgraph/algorithm/AbstractAnalysis.js +144 -144
  7. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts +10 -10
  8. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.js +69 -69
  9. package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts +15 -15
  10. package/lib/callgraph/algorithm/RapidTypeAnalysis.js +146 -146
  11. package/lib/callgraph/common/Statistics.d.ts +57 -57
  12. package/lib/callgraph/common/Statistics.js +228 -228
  13. package/lib/callgraph/model/CallGraph.d.ts +96 -96
  14. package/lib/callgraph/model/CallGraph.js +354 -354
  15. package/lib/callgraph/model/builder/CallGraphBuilder.d.ts +15 -15
  16. package/lib/callgraph/model/builder/CallGraphBuilder.js +106 -106
  17. package/lib/callgraph/pointerAnalysis/Context.d.ts +37 -37
  18. package/lib/callgraph/pointerAnalysis/Context.js +155 -155
  19. package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts +22 -22
  20. package/lib/callgraph/pointerAnalysis/DummyCallCreator.js +104 -104
  21. package/lib/callgraph/pointerAnalysis/PTAUtils.d.ts +5 -5
  22. package/lib/callgraph/pointerAnalysis/PTAUtils.js +46 -46
  23. package/lib/callgraph/pointerAnalysis/Pag.d.ts +261 -261
  24. package/lib/callgraph/pointerAnalysis/Pag.js +930 -930
  25. package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts +157 -157
  26. package/lib/callgraph/pointerAnalysis/PagBuilder.js +1399 -1399
  27. package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts +72 -72
  28. package/lib/callgraph/pointerAnalysis/PointerAnalysis.js +573 -573
  29. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts +15 -15
  30. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.js +81 -81
  31. package/lib/callgraph/pointerAnalysis/PtsDS.d.ts +80 -80
  32. package/lib/callgraph/pointerAnalysis/PtsDS.js +308 -308
  33. package/lib/core/base/Constant.d.ts +48 -48
  34. package/lib/core/base/Constant.js +103 -103
  35. package/lib/core/base/Decorator.d.ts +15 -15
  36. package/lib/core/base/Decorator.js +46 -46
  37. package/lib/core/base/DefUseChain.d.ts +8 -8
  38. package/lib/core/base/DefUseChain.js +25 -25
  39. package/lib/core/base/Expr.d.ts +340 -340
  40. package/lib/core/base/Expr.js +917 -917
  41. package/lib/core/base/Local.d.ts +104 -104
  42. package/lib/core/base/Local.js +176 -176
  43. package/lib/core/base/Position.d.ts +30 -30
  44. package/lib/core/base/Position.js +147 -147
  45. package/lib/core/base/Ref.d.ts +173 -173
  46. package/lib/core/base/Ref.js +374 -374
  47. package/lib/core/base/Stmt.d.ts +237 -237
  48. package/lib/core/base/Stmt.js +525 -525
  49. package/lib/core/base/Trap.d.ts +8 -8
  50. package/lib/core/base/Trap.js +30 -30
  51. package/lib/core/base/Type.d.ts +308 -308
  52. package/lib/core/base/Type.js +639 -639
  53. package/lib/core/base/TypeExpr.d.ts +71 -71
  54. package/lib/core/base/TypeExpr.js +155 -155
  55. package/lib/core/base/Value.d.ts +32 -32
  56. package/lib/core/base/Value.js +16 -16
  57. package/lib/core/common/ArkError.d.ts +14 -14
  58. package/lib/core/common/ArkError.js +28 -28
  59. package/lib/core/common/ArkIRTransformer.d.ts +67 -67
  60. package/lib/core/common/ArkIRTransformer.js +690 -690
  61. package/lib/core/common/ArkValueTransformer.d.ts +89 -89
  62. package/lib/core/common/ArkValueTransformer.js +1478 -1478
  63. package/lib/core/common/Builtin.d.ts +36 -36
  64. package/lib/core/common/Builtin.js +81 -81
  65. package/lib/core/common/Const.d.ts +21 -21
  66. package/lib/core/common/Const.js +43 -43
  67. package/lib/core/common/DummyMainCreater.d.ts +46 -46
  68. package/lib/core/common/DummyMainCreater.js +319 -319
  69. package/lib/core/common/EtsConst.d.ts +73 -73
  70. package/lib/core/common/EtsConst.js +1021 -1021
  71. package/lib/core/common/ExprUseReplacer.d.ts +21 -21
  72. package/lib/core/common/ExprUseReplacer.js +124 -124
  73. package/lib/core/common/IRInference.d.ts +36 -36
  74. package/lib/core/common/IRInference.d.ts.map +1 -1
  75. package/lib/core/common/IRInference.js +651 -648
  76. package/lib/core/common/IRUtils.d.ts +18 -18
  77. package/lib/core/common/IRUtils.js +141 -141
  78. package/lib/core/common/ModelUtils.d.ts +70 -70
  79. package/lib/core/common/ModelUtils.js +712 -712
  80. package/lib/core/common/RefUseReplacer.d.ts +13 -13
  81. package/lib/core/common/RefUseReplacer.js +52 -52
  82. package/lib/core/common/SdkUtils.d.ts +8 -8
  83. package/lib/core/common/SdkUtils.js +105 -105
  84. package/lib/core/common/StmtDefReplacer.d.ts +12 -12
  85. package/lib/core/common/StmtDefReplacer.js +41 -41
  86. package/lib/core/common/StmtUseReplacer.d.ts +16 -16
  87. package/lib/core/common/StmtUseReplacer.js +108 -108
  88. package/lib/core/common/TSConst.d.ts +18 -18
  89. package/lib/core/common/TSConst.js +36 -36
  90. package/lib/core/common/TypeInference.d.ts +102 -102
  91. package/lib/core/common/TypeInference.js +748 -748
  92. package/lib/core/common/ValueUtil.d.ts +12 -12
  93. package/lib/core/common/ValueUtil.js +54 -54
  94. package/lib/core/common/VisibleValue.d.ts +36 -36
  95. package/lib/core/common/VisibleValue.js +212 -212
  96. package/lib/core/dataflow/DataflowProblem.d.ts +20 -20
  97. package/lib/core/dataflow/DataflowProblem.js +20 -20
  98. package/lib/core/dataflow/DataflowResult.d.ts +7 -7
  99. package/lib/core/dataflow/DataflowResult.js +26 -26
  100. package/lib/core/dataflow/DataflowSolver.d.ts +43 -43
  101. package/lib/core/dataflow/DataflowSolver.js +307 -307
  102. package/lib/core/dataflow/Edge.d.ts +11 -11
  103. package/lib/core/dataflow/Edge.js +31 -31
  104. package/lib/core/dataflow/Fact.d.ts +6 -6
  105. package/lib/core/dataflow/Fact.js +24 -24
  106. package/lib/core/dataflow/GenericDataFlow.d.ts +142 -142
  107. package/lib/core/dataflow/GenericDataFlow.js +109 -109
  108. package/lib/core/dataflow/ReachingDef.d.ts +62 -62
  109. package/lib/core/dataflow/ReachingDef.js +168 -168
  110. package/lib/core/dataflow/UndefinedVariable.d.ts +45 -45
  111. package/lib/core/dataflow/UndefinedVariable.js +288 -288
  112. package/lib/core/dataflow/Util.d.ts +9 -9
  113. package/lib/core/dataflow/Util.js +64 -64
  114. package/lib/core/graph/BaseExplicitGraph.d.ts +58 -58
  115. package/lib/core/graph/BaseExplicitGraph.js +157 -157
  116. package/lib/core/graph/BaseImplicitGraph.d.ts +77 -77
  117. package/lib/core/graph/BaseImplicitGraph.js +78 -78
  118. package/lib/core/graph/BasicBlock.d.ts +104 -104
  119. package/lib/core/graph/BasicBlock.js +280 -280
  120. package/lib/core/graph/Cfg.d.ts +59 -59
  121. package/lib/core/graph/Cfg.js +282 -282
  122. package/lib/core/graph/DependsGraph.d.ts +35 -35
  123. package/lib/core/graph/DependsGraph.js +87 -87
  124. package/lib/core/graph/DominanceFinder.d.ts +15 -15
  125. package/lib/core/graph/DominanceFinder.js +121 -121
  126. package/lib/core/graph/DominanceTree.d.ts +12 -12
  127. package/lib/core/graph/DominanceTree.js +70 -70
  128. package/lib/core/graph/GraphTraits.d.ts +7 -7
  129. package/lib/core/graph/GraphTraits.js +16 -16
  130. package/lib/core/graph/Scc.d.ts +61 -61
  131. package/lib/core/graph/Scc.js +222 -222
  132. package/lib/core/graph/ViewTree.d.ts +114 -114
  133. package/lib/core/graph/ViewTree.js +16 -16
  134. package/lib/core/graph/builder/CfgBuilder.d.ts +176 -176
  135. package/lib/core/graph/builder/CfgBuilder.js +1139 -1139
  136. package/lib/core/graph/builder/ConditionBuilder.d.ts +15 -15
  137. package/lib/core/graph/builder/ConditionBuilder.js +252 -252
  138. package/lib/core/graph/builder/LoopBuilder.d.ts +20 -20
  139. package/lib/core/graph/builder/LoopBuilder.js +251 -251
  140. package/lib/core/graph/builder/SwitchBuilder.d.ts +11 -11
  141. package/lib/core/graph/builder/SwitchBuilder.js +152 -152
  142. package/lib/core/graph/builder/TrapBuilder.d.ts +16 -16
  143. package/lib/core/graph/builder/TrapBuilder.js +272 -272
  144. package/lib/core/graph/builder/ViewTreeBuilder.d.ts +203 -203
  145. package/lib/core/graph/builder/ViewTreeBuilder.js +1055 -1055
  146. package/lib/core/model/ArkBaseModel.d.ts +59 -59
  147. package/lib/core/model/ArkBaseModel.js +274 -274
  148. package/lib/core/model/ArkBody.d.ts +24 -24
  149. package/lib/core/model/ArkBody.js +65 -65
  150. package/lib/core/model/ArkClass.d.ts +206 -206
  151. package/lib/core/model/ArkClass.js +439 -439
  152. package/lib/core/model/ArkExport.d.ts +72 -72
  153. package/lib/core/model/ArkExport.js +147 -147
  154. package/lib/core/model/ArkField.d.ts +62 -62
  155. package/lib/core/model/ArkField.js +113 -113
  156. package/lib/core/model/ArkFile.d.ts +131 -131
  157. package/lib/core/model/ArkFile.js +267 -267
  158. package/lib/core/model/ArkImport.d.ts +44 -44
  159. package/lib/core/model/ArkImport.js +108 -108
  160. package/lib/core/model/ArkMetadata.d.ts +31 -31
  161. package/lib/core/model/ArkMetadata.js +55 -55
  162. package/lib/core/model/ArkMethod.d.ts +261 -261
  163. package/lib/core/model/ArkMethod.js +592 -592
  164. package/lib/core/model/ArkNamespace.d.ts +66 -66
  165. package/lib/core/model/ArkNamespace.js +209 -209
  166. package/lib/core/model/ArkSignature.d.ts +162 -162
  167. package/lib/core/model/ArkSignature.js +379 -379
  168. package/lib/core/model/builder/ArkClassBuilder.d.ts +12 -12
  169. package/lib/core/model/builder/ArkClassBuilder.js +454 -454
  170. package/lib/core/model/builder/ArkExportBuilder.d.ts +24 -24
  171. package/lib/core/model/builder/ArkExportBuilder.js +210 -210
  172. package/lib/core/model/builder/ArkFieldBuilder.d.ts +8 -8
  173. package/lib/core/model/builder/ArkFieldBuilder.js +193 -193
  174. package/lib/core/model/builder/ArkFileBuilder.d.ts +9 -9
  175. package/lib/core/model/builder/ArkFileBuilder.js +165 -165
  176. package/lib/core/model/builder/ArkImportBuilder.d.ts +4 -4
  177. package/lib/core/model/builder/ArkImportBuilder.js +128 -128
  178. package/lib/core/model/builder/ArkMethodBuilder.d.ts +63 -63
  179. package/lib/core/model/builder/ArkMethodBuilder.js +486 -486
  180. package/lib/core/model/builder/ArkNamespaceBuilder.d.ts +5 -5
  181. package/lib/core/model/builder/ArkNamespaceBuilder.js +205 -205
  182. package/lib/core/model/builder/ArkSignatureBuilder.d.ts +8 -8
  183. package/lib/core/model/builder/ArkSignatureBuilder.js +40 -40
  184. package/lib/core/model/builder/BodyBuilder.d.ts +55 -30
  185. package/lib/core/model/builder/BodyBuilder.d.ts.map +1 -1
  186. package/lib/core/model/builder/BodyBuilder.js +542 -371
  187. package/lib/core/model/builder/builderUtils.d.ts +18 -18
  188. package/lib/core/model/builder/builderUtils.js +558 -558
  189. package/lib/index.d.ts +82 -80
  190. package/lib/index.d.ts.map +1 -1
  191. package/lib/index.js +201 -196
  192. package/lib/save/ArkStream.d.ts +23 -23
  193. package/lib/save/ArkStream.js +83 -83
  194. package/lib/save/DotPrinter.d.ts +43 -43
  195. package/lib/save/DotPrinter.js +170 -170
  196. package/lib/save/GraphPrinter.d.ts +16 -16
  197. package/lib/save/GraphPrinter.js +134 -134
  198. package/lib/save/JsonPrinter.d.ts +30 -30
  199. package/lib/save/JsonPrinter.js +580 -580
  200. package/lib/save/Printer.d.ts +12 -12
  201. package/lib/save/Printer.js +27 -27
  202. package/lib/save/PrinterBuilder.d.ts +53 -53
  203. package/lib/save/PrinterBuilder.js +145 -145
  204. package/lib/save/ViewTreePrinter.d.ts +14 -14
  205. package/lib/save/ViewTreePrinter.js +123 -123
  206. package/lib/save/arkir/ArkIRClassPrinter.d.ts +13 -13
  207. package/lib/save/arkir/ArkIRClassPrinter.js +92 -92
  208. package/lib/save/arkir/ArkIRFieldPrinter.d.ts +11 -11
  209. package/lib/save/arkir/ArkIRFieldPrinter.js +64 -64
  210. package/lib/save/arkir/ArkIRFilePrinter.d.ts +12 -12
  211. package/lib/save/arkir/ArkIRFilePrinter.js +54 -54
  212. package/lib/save/arkir/ArkIRMethodPrinter.d.ts +16 -16
  213. package/lib/save/arkir/ArkIRMethodPrinter.js +159 -159
  214. package/lib/save/arkir/ArkIRNamespacePrinter.d.ts +11 -11
  215. package/lib/save/arkir/ArkIRNamespacePrinter.js +66 -66
  216. package/lib/save/base/BasePrinter.d.ts +23 -23
  217. package/lib/save/base/BasePrinter.js +70 -70
  218. package/lib/save/base/ExportPrinter.d.ts +8 -8
  219. package/lib/save/base/ExportPrinter.js +67 -67
  220. package/lib/save/base/ImportPrinter.d.ts +9 -9
  221. package/lib/save/base/ImportPrinter.js +92 -92
  222. package/lib/save/base/PrinterUtils.d.ts +24 -24
  223. package/lib/save/base/PrinterUtils.js +208 -208
  224. package/lib/save/serializeArkIR.d.ts +8 -8
  225. package/lib/save/serializeArkIR.js +294 -294
  226. package/lib/save/source/SourceBase.d.ts +22 -22
  227. package/lib/save/source/SourceBase.js +64 -64
  228. package/lib/save/source/SourceBody.d.ts +58 -58
  229. package/lib/save/source/SourceBody.js +296 -296
  230. package/lib/save/source/SourceClass.d.ts +25 -25
  231. package/lib/save/source/SourceClass.js +187 -187
  232. package/lib/save/source/SourceField.d.ts +13 -13
  233. package/lib/save/source/SourceField.js +73 -73
  234. package/lib/save/source/SourceFilePrinter.d.ts +12 -12
  235. package/lib/save/source/SourceFilePrinter.js +69 -69
  236. package/lib/save/source/SourceMethod.d.ts +22 -22
  237. package/lib/save/source/SourceMethod.d.ts.map +1 -1
  238. package/lib/save/source/SourceMethod.js +196 -193
  239. package/lib/save/source/SourceNamespace.d.ts +11 -11
  240. package/lib/save/source/SourceNamespace.js +83 -83
  241. package/lib/save/source/SourceStmt.d.ts +178 -178
  242. package/lib/save/source/SourceStmt.js +838 -838
  243. package/lib/save/source/SourceTransformer.d.ts +46 -46
  244. package/lib/save/source/SourceTransformer.js +446 -446
  245. package/lib/transformer/FunctionTransformer.d.ts +2 -2
  246. package/lib/transformer/FunctionTransformer.js +17 -17
  247. package/lib/transformer/SceneTransformer.d.ts +2 -2
  248. package/lib/transformer/SceneTransformer.js +17 -17
  249. package/lib/transformer/StaticSingleAssignmentFormer.d.ts +12 -12
  250. package/lib/transformer/StaticSingleAssignmentFormer.js +259 -259
  251. package/lib/transformer/Transformer.d.ts +6 -6
  252. package/lib/transformer/Transformer.js +22 -22
  253. package/lib/utils/AstTreeUtils.d.ts +4 -4
  254. package/lib/utils/AstTreeUtils.js +26 -26
  255. package/lib/utils/CfgStructualAnalysis.d.ts +110 -110
  256. package/lib/utils/CfgStructualAnalysis.js +1277 -1277
  257. package/lib/utils/FileUtils.d.ts +18 -18
  258. package/lib/utils/FileUtils.js +135 -135
  259. package/lib/utils/SparseBitVector.d.ts +100 -100
  260. package/lib/utils/SparseBitVector.js +445 -445
  261. package/lib/utils/callGraphUtils.d.ts +30 -30
  262. package/lib/utils/callGraphUtils.js +205 -205
  263. package/lib/utils/crypto_utils.d.ts +5 -5
  264. package/lib/utils/crypto_utils.js +57 -57
  265. package/lib/utils/entryMethodUtils.d.ts +13 -13
  266. package/lib/utils/entryMethodUtils.js +110 -110
  267. package/lib/utils/getAllFiles.d.ts +9 -9
  268. package/lib/utils/getAllFiles.js +90 -90
  269. package/lib/utils/json5parser.d.ts +6 -6
  270. package/lib/utils/json5parser.js +146 -146
  271. package/lib/utils/logger.d.ts +18 -18
  272. package/lib/utils/logger.d.ts.map +1 -1
  273. package/lib/utils/logger.js +97 -90
  274. package/lib/utils/pathTransfer.d.ts +1 -1
  275. package/lib/utils/pathTransfer.js +25 -25
  276. package/node_modules/json5/lib/cli.js +0 -0
  277. package/node_modules/ohos-typescript/bin/tsc +0 -0
  278. package/node_modules/ohos-typescript/bin/tsserver +0 -0
  279. package/package.json +3 -3
@@ -1,371 +1,542 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.BodyBuilder = void 0;
18
- const ArkBody_1 = require("../ArkBody");
19
- const ArkSignature_1 = require("../ArkSignature");
20
- const CfgBuilder_1 = require("../../graph/builder/CfgBuilder");
21
- const Local_1 = require("../../base/Local");
22
- const ArkMethodBuilder_1 = require("./ArkMethodBuilder");
23
- const Const_1 = require("../../common/Const");
24
- const Ref_1 = require("../../base/Ref");
25
- const Stmt_1 = require("../../base/Stmt");
26
- const Type_1 = require("../../base/Type");
27
- const Expr_1 = require("../../base/Expr");
28
- class BodyBuilder {
29
- constructor(methodSignature, sourceAstNode, declaringMethod, sourceFile) {
30
- this.cfgBuilder = new CfgBuilder_1.CfgBuilder(sourceAstNode, methodSignature.getMethodSubSignature().getMethodName(), declaringMethod, sourceFile);
31
- }
32
- build() {
33
- this.cfgBuilder.buildCfgBuilder();
34
- if (!this.cfgBuilder.isBodyEmpty()) {
35
- const { cfg, locals, globals, aliasTypeMap, traps } = this.cfgBuilder.buildCfg();
36
- if (globals !== null) {
37
- this.setGlobals(globals);
38
- }
39
- cfg.buildDefUseStmt(locals);
40
- return new ArkBody_1.ArkBody(locals, cfg, aliasTypeMap, traps.length ? traps : undefined);
41
- }
42
- return null;
43
- }
44
- getCfgBuilder() {
45
- return this.cfgBuilder;
46
- }
47
- getGlobals() {
48
- return this.globals;
49
- }
50
- setGlobals(globals) {
51
- this.globals = globals;
52
- }
53
- handleGlobalAndClosure() {
54
- var _a, _b, _c, _d, _e, _f, _g;
55
- // 首先将outerMethod中的全局变量从locals中移到usedGlobals中,避免干扰嵌套函数中闭包变量的识别
56
- let outerMethod = this.getCfgBuilder().getDeclaringMethod();
57
- let outerGlobals = (_a = outerMethod.getBodyBuilder()) === null || _a === void 0 ? void 0 : _a.getGlobals();
58
- outerMethod.freeBodyBuilder();
59
- let outerLocals = (_b = outerMethod.getBody()) === null || _b === void 0 ? void 0 : _b.getLocals();
60
- if (outerGlobals !== undefined && outerLocals !== undefined) {
61
- this.moveLocalToGlobal(outerLocals, outerGlobals);
62
- if (outerGlobals.size > 0) {
63
- (_c = outerMethod.getBody()) === null || _c === void 0 ? void 0 : _c.setUsedGlobals(outerGlobals);
64
- }
65
- }
66
- let nestedMethods = this.findNestedMethod(outerMethod);
67
- if (nestedMethods === null) {
68
- return;
69
- }
70
- let closuresNum = 0;
71
- for (let nestedMethod of nestedMethods) {
72
- let nestedGlobals = (_d = nestedMethod.getBodyBuilder()) === null || _d === void 0 ? void 0 : _d.getGlobals();
73
- nestedMethod.freeBodyBuilder();
74
- let nestedLocals = (_e = nestedMethod.getBody()) === null || _e === void 0 ? void 0 : _e.getLocals();
75
- if (nestedLocals === undefined || nestedGlobals === undefined) {
76
- continue;
77
- }
78
- const nestedSignature = nestedMethod.getImplementationSignature();
79
- if (nestedSignature === null) {
80
- continue;
81
- }
82
- // 嵌套函数优先匹配闭包变量,存入词法环境中,该词法环境加入外层函数的locals中,剩余globals中变量为真正的全局变量
83
- let lexicalEnv = new Type_1.LexicalEnvType(nestedSignature);
84
- if (outerLocals !== undefined) {
85
- this.moveGlobalToLexicalEnv(nestedGlobals, outerLocals, lexicalEnv);
86
- if (lexicalEnv.getClosures().length > 0) {
87
- const closuresLocal = new Local_1.Local(`${Const_1.LEXICAL_ENV_NAME_PREFIX}${closuresNum}`, lexicalEnv);
88
- outerLocals.set(closuresLocal.getName(), closuresLocal);
89
- this.updateNestedMethodWithClosures(nestedMethod, closuresLocal);
90
- this.updateOuterMethodWithClosures(outerMethod, nestedMethod, closuresLocal);
91
- closuresNum++;
92
- }
93
- }
94
- this.moveLocalToGlobal(nestedLocals, nestedGlobals);
95
- if (nestedGlobals.size > 0) {
96
- (_f = nestedMethod.getBody()) === null || _f === void 0 ? void 0 : _f.setUsedGlobals(nestedGlobals);
97
- }
98
- // 对嵌套函数中的UnclearReferenceType类型的变量进行类型推导,类型是否为外层函数中定义的类型别名
99
- const typeAliases = (_g = outerMethod.getBody()) === null || _g === void 0 ? void 0 : _g.getAliasTypeMap();
100
- if (typeAliases !== undefined) {
101
- this.updateLocalTypesWithTypeAlias(nestedLocals, typeAliases);
102
- }
103
- }
104
- }
105
- updateLocalTypesWithTypeAlias(locals, typeAliases) {
106
- for (let local of locals.values()) {
107
- const newType = this.inferUnclearReferenceTypeWithTypeAlias(local.getType(), typeAliases);
108
- if (newType !== null) {
109
- local.setType(newType);
110
- }
111
- }
112
- }
113
- inferUnclearReferenceTypeWithTypeAlias(localType, typeAliases) {
114
- if (localType instanceof Type_1.ArrayType && localType.getBaseType() instanceof Type_1.UnclearReferenceType) {
115
- const typeAlias = typeAliases.get(localType.getBaseType().getName());
116
- if (typeAlias !== undefined) {
117
- localType.setBaseType(typeAlias[0]);
118
- return localType;
119
- }
120
- return null;
121
- }
122
- if (localType instanceof Type_1.UnionType) {
123
- const optionTypes = localType.getTypes();
124
- for (let i = 0; i < optionTypes.length; i++) {
125
- const newType = this.inferUnclearReferenceTypeWithTypeAlias(optionTypes[i], typeAliases);
126
- if (newType !== null) {
127
- optionTypes[i] = newType;
128
- }
129
- }
130
- return localType;
131
- }
132
- if (localType instanceof Type_1.UnclearReferenceType) {
133
- const typeAlias = typeAliases.get(localType.getName());
134
- if (typeAlias !== undefined) {
135
- return typeAlias[0];
136
- }
137
- }
138
- return null;
139
- }
140
- findNestedMethod(outerMethod) {
141
- let nestedMethods = [];
142
- const arkClass = outerMethod.getDeclaringArkClass();
143
- for (let method of arkClass.getMethods()) {
144
- if (!method.getName().startsWith(Const_1.NAME_PREFIX) || !method.getName().endsWith(outerMethod.getName())) {
145
- continue;
146
- }
147
- const components = method.getName().split(Const_1.NAME_DELIMITER);
148
- // TODO: 当前仅返回第一层的嵌套函数,待支持多层嵌套
149
- if (components.length === 2) {
150
- nestedMethods.push(method);
151
- }
152
- }
153
- if (nestedMethods.length > 0) {
154
- return nestedMethods;
155
- }
156
- return null;
157
- }
158
- moveLocalToGlobal(locals, globals) {
159
- globals.forEach((value, key) => {
160
- const local = locals.get(key);
161
- if (local !== undefined) {
162
- value.addUsedStmts(local.getUsedStmts());
163
- locals.delete(key);
164
- }
165
- });
166
- }
167
- moveGlobalToLexicalEnv(nestedGlobals, outerLocals, lexicalEnv) {
168
- for (let key of nestedGlobals.keys()) {
169
- const local = outerLocals.get(key);
170
- if (local !== undefined) {
171
- lexicalEnv.addClosure(local);
172
- nestedGlobals.delete(key);
173
- }
174
- }
175
- }
176
- updateNestedMethodWithClosures(nestedMethod, closuresLocal) {
177
- if (!(closuresLocal.getType() instanceof Type_1.LexicalEnvType)) {
178
- return;
179
- }
180
- const declareSignatures = nestedMethod.getDeclareSignatures();
181
- declareSignatures === null || declareSignatures === void 0 ? void 0 : declareSignatures.forEach((signature, index) => {
182
- nestedMethod.setDeclareSignatureWithIndex(this.createNewSignatureWithClosures(closuresLocal, signature), index);
183
- });
184
- const implementSignature = nestedMethod.getImplementationSignature();
185
- if (implementSignature !== null) {
186
- nestedMethod.setImplementationSignature(this.createNewSignatureWithClosures(closuresLocal, implementSignature));
187
- }
188
- this.addClosureParamsAssignStmts(closuresLocal, nestedMethod);
189
- }
190
- updateOuterMethodWithClosures(outerMethod, nestedMethod, closuresLocal) {
191
- var _a, _b, _c, _d, _e, _f;
192
- const nestedMethodName = nestedMethod.getName();
193
- const nestedMethodLocal = (_a = outerMethod.getBody()) === null || _a === void 0 ? void 0 : _a.getLocals().get(nestedMethodName);
194
- if (nestedMethodLocal !== undefined) {
195
- this.updateLocalInfoWithClosures(nestedMethodLocal, outerMethod, nestedMethod, closuresLocal);
196
- }
197
- else {
198
- const nestedMethodGlobal = (_c = (_b = outerMethod.getBody()) === null || _b === void 0 ? void 0 : _b.getUsedGlobals()) === null || _c === void 0 ? void 0 : _c.get(nestedMethodName);
199
- if (nestedMethodGlobal !== undefined && nestedMethodGlobal instanceof Ref_1.GlobalRef) {
200
- this.updateGlobalInfoWithClosures(nestedMethodGlobal, outerMethod, nestedMethod, closuresLocal);
201
- }
202
- }
203
- const originalMethodName = this.getOriginalNestedMethodName(nestedMethodName);
204
- if (originalMethodName === null) {
205
- return;
206
- }
207
- const originalMethodLocal = (_d = outerMethod.getBody()) === null || _d === void 0 ? void 0 : _d.getLocals().get(originalMethodName);
208
- if (originalMethodLocal !== undefined) {
209
- this.updateLocalInfoWithClosures(originalMethodLocal, outerMethod, nestedMethod, closuresLocal);
210
- }
211
- else {
212
- const originalMethodGlobal = (_f = (_e = outerMethod.getBody()) === null || _e === void 0 ? void 0 : _e.getUsedGlobals()) === null || _f === void 0 ? void 0 : _f.get(originalMethodName);
213
- if (originalMethodGlobal !== undefined && originalMethodGlobal instanceof Ref_1.GlobalRef) {
214
- this.updateGlobalInfoWithClosures(originalMethodGlobal, outerMethod, nestedMethod, closuresLocal);
215
- }
216
- }
217
- }
218
- getOriginalNestedMethodName(nestedMethodName) {
219
- if (nestedMethodName.startsWith(Const_1.NAME_PREFIX) && nestedMethodName.includes(Const_1.NAME_DELIMITER)) {
220
- const nameComponents = nestedMethodName.slice(1).split(Const_1.NAME_DELIMITER);
221
- if (nameComponents.length > 1) {
222
- return nameComponents[0];
223
- }
224
- }
225
- return null;
226
- }
227
- updateGlobalInfoWithClosures(globalRef, outerMethod, nestedMethod, closuresLocal) {
228
- if (globalRef.getRef() !== null) {
229
- return;
230
- }
231
- const methodSignature = nestedMethod.getImplementationSignature();
232
- if (methodSignature === null) {
233
- return;
234
- }
235
- const lexicalEnv = closuresLocal.getType();
236
- if (!(lexicalEnv instanceof Type_1.LexicalEnvType)) {
237
- return;
238
- }
239
- const fieldSignature = new ArkSignature_1.FieldSignature(methodSignature.getMethodSubSignature().getMethodName(), methodSignature.getDeclaringClassSignature(), new Type_1.ClosureType(lexicalEnv, methodSignature));
240
- globalRef.setRef(new Ref_1.ArkStaticFieldRef((fieldSignature)));
241
- this.updateAbstractInvokeExprWithClosures(globalRef, outerMethod.getSignature(), nestedMethod.getSignature(), closuresLocal);
242
- }
243
- updateLocalInfoWithClosures(local, outerMethod, nestedMethod, closuresLocal) {
244
- const localType = local.getType();
245
- if (!(localType instanceof Type_1.FunctionType)) {
246
- return;
247
- }
248
- const lexicalEnv = closuresLocal.getType();
249
- if (!(lexicalEnv instanceof Type_1.LexicalEnvType)) {
250
- return;
251
- }
252
- // 更新local的类型为ClosureType,methodSignature为内层嵌套函数
253
- const nestedMethodSignature = nestedMethod.getImplementationSignature();
254
- if (nestedMethodSignature !== null) {
255
- local.setType(new Type_1.ClosureType(lexicalEnv, nestedMethodSignature, localType.getRealGenericTypes()));
256
- }
257
- else {
258
- local.setType(new Type_1.ClosureType(lexicalEnv, localType.getMethodSignature(), localType.getRealGenericTypes()));
259
- }
260
- this.updateAbstractInvokeExprWithClosures(local, outerMethod.getSignature(), nestedMethod.getSignature(), closuresLocal);
261
- }
262
- // 更新所有stmt中调用内层函数处的AbstractInvokeExpr中的函数签名和实参args,加入闭包参数
263
- // 更新所有stmt中定义的函数指针的usedStmt中的函数签名和实参args,加入闭包参数
264
- updateAbstractInvokeExprWithClosures(value, outerMethodSignature, nestedMethodSignature, closuresLocal) {
265
- for (const usedStmt of value.getUsedStmts()) {
266
- if (usedStmt instanceof Stmt_1.ArkInvokeStmt) {
267
- this.updateSignatureAndArgsInArkInvokeExpr(usedStmt, nestedMethodSignature, closuresLocal);
268
- }
269
- else if (usedStmt instanceof Stmt_1.ArkAssignStmt) {
270
- const rightOp = usedStmt.getRightOp();
271
- if (rightOp instanceof Expr_1.AbstractInvokeExpr) {
272
- this.updateSignatureAndArgsInArkInvokeExpr(usedStmt, nestedMethodSignature, closuresLocal);
273
- }
274
- const leftOp = usedStmt.getLeftOp();
275
- if (leftOp instanceof Local_1.Local) {
276
- leftOp.setType(rightOp.getType());
277
- }
278
- }
279
- else if (usedStmt instanceof Stmt_1.ArkReturnStmt) {
280
- outerMethodSignature.getMethodSubSignature().setReturnType(value.getType());
281
- }
282
- const defValue = usedStmt.getDef();
283
- if (defValue === null) {
284
- continue;
285
- }
286
- if ((defValue instanceof Local_1.Local || defValue instanceof Ref_1.GlobalRef) && defValue.getType() instanceof Type_1.FunctionType) {
287
- this.updateAbstractInvokeExprWithClosures(defValue, outerMethodSignature, nestedMethodSignature, closuresLocal);
288
- }
289
- }
290
- }
291
- createNewSignatureWithClosures(closuresLocal, oldSignature) {
292
- let oldSubSignature = oldSignature.getMethodSubSignature();
293
- const params = oldSubSignature.getParameters();
294
- const closuresParam = new ArkMethodBuilder_1.MethodParameter();
295
- closuresParam.setName(closuresLocal.getName());
296
- closuresParam.setType(closuresLocal.getType());
297
- params.unshift(closuresParam);
298
- let newSubSignature = new ArkSignature_1.MethodSubSignature(oldSubSignature.getMethodName(), params, oldSubSignature.getReturnType(), oldSubSignature.isStatic());
299
- return new ArkSignature_1.MethodSignature(oldSignature.getDeclaringClassSignature(), newSubSignature);
300
- }
301
- updateSignatureAndArgsInArkInvokeExpr(stmt, methodSignature, closuresLocal) {
302
- let expr;
303
- if (stmt instanceof Stmt_1.ArkInvokeStmt) {
304
- expr = stmt.getInvokeExpr();
305
- }
306
- else {
307
- const rightOp = stmt.getRightOp();
308
- if (!(rightOp instanceof Expr_1.AbstractInvokeExpr)) {
309
- return;
310
- }
311
- expr = rightOp;
312
- }
313
- const exprMethodName = expr.getMethodSignature().getMethodSubSignature().getMethodName();
314
- const nestedMethodName = methodSignature.getMethodSubSignature().getMethodName();
315
- if (exprMethodName === nestedMethodName) {
316
- expr.setMethodSignature(this.createNewSignatureWithClosures(closuresLocal, methodSignature));
317
- expr.getArgs().unshift(closuresLocal);
318
- closuresLocal.addUsedStmt(stmt);
319
- return;
320
- }
321
- const originalMethodName = this.getOriginalNestedMethodName(nestedMethodName);
322
- if (originalMethodName !== null) {
323
- if (exprMethodName === originalMethodName || expr instanceof Expr_1.ArkPtrInvokeExpr) {
324
- expr.setMethodSignature(methodSignature);
325
- expr.getArgs().unshift(closuresLocal);
326
- closuresLocal.addUsedStmt(stmt);
327
- }
328
- }
329
- }
330
- addClosureParamsAssignStmts(closuresParam, method) {
331
- var _a;
332
- const lexicalEnv = closuresParam.getType();
333
- if (!(lexicalEnv instanceof Type_1.LexicalEnvType)) {
334
- return;
335
- }
336
- const closures = lexicalEnv.getClosures();
337
- if (closures.length === 0) {
338
- return;
339
- }
340
- const oldParamRefs = method.getParameterRefs();
341
- let body = method.getBody();
342
- if (body === undefined) {
343
- return;
344
- }
345
- let stmts = Array.from(body.getCfg().getBlocks())[0].getStmts();
346
- let index = 0;
347
- const parameterRef = new Ref_1.ArkParameterRef(index, lexicalEnv);
348
- const closuresLocal = new Local_1.Local(closuresParam.getName(), lexicalEnv);
349
- body.addLocal(closuresLocal.getName(), closuresLocal);
350
- let assignStmt = new Stmt_1.ArkAssignStmt(closuresLocal, parameterRef);
351
- stmts.splice(index, 0, assignStmt);
352
- closuresLocal.setDeclaringStmt(assignStmt);
353
- oldParamRefs === null || oldParamRefs === void 0 ? void 0 : oldParamRefs.forEach((paramRef) => {
354
- index++;
355
- paramRef.setIndex(index);
356
- });
357
- for (let closure of closures) {
358
- const local = (_a = method.getBody()) === null || _a === void 0 ? void 0 : _a.getLocals().get(closure.getName());
359
- if (local === undefined) {
360
- continue;
361
- }
362
- index++;
363
- const closureFieldRef = new Ref_1.ClosureFieldRef(closuresParam, closure.getName(), closure.getType());
364
- let assignStmt = new Stmt_1.ArkAssignStmt(local, closureFieldRef);
365
- stmts.splice(index, 0, assignStmt);
366
- local.setDeclaringStmt(assignStmt);
367
- closuresLocal.addUsedStmt(assignStmt);
368
- }
369
- }
370
- }
371
- exports.BodyBuilder = BodyBuilder;
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
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.BodyBuilder = void 0;
18
+ const ArkBody_1 = require("../ArkBody");
19
+ const ArkSignature_1 = require("../ArkSignature");
20
+ const CfgBuilder_1 = require("../../graph/builder/CfgBuilder");
21
+ const Local_1 = require("../../base/Local");
22
+ const ArkMethodBuilder_1 = require("./ArkMethodBuilder");
23
+ const Const_1 = require("../../common/Const");
24
+ const Ref_1 = require("../../base/Ref");
25
+ const Stmt_1 = require("../../base/Stmt");
26
+ const Type_1 = require("../../base/Type");
27
+ const Expr_1 = require("../../base/Expr");
28
+ class BodyBuilder {
29
+ constructor(methodSignature, sourceAstNode, declaringMethod, sourceFile) {
30
+ this.cfgBuilder = new CfgBuilder_1.CfgBuilder(sourceAstNode, methodSignature.getMethodSubSignature().getMethodName(), declaringMethod, sourceFile);
31
+ }
32
+ build() {
33
+ this.cfgBuilder.buildCfgBuilder();
34
+ if (!this.cfgBuilder.isBodyEmpty()) {
35
+ const { cfg, locals, globals, aliasTypeMap, traps } = this.cfgBuilder.buildCfg();
36
+ if (globals !== null) {
37
+ this.setGlobals(globals);
38
+ }
39
+ cfg.buildDefUseStmt(locals);
40
+ return new ArkBody_1.ArkBody(locals, cfg, aliasTypeMap, traps.length ? traps : undefined);
41
+ }
42
+ return null;
43
+ }
44
+ getCfgBuilder() {
45
+ return this.cfgBuilder;
46
+ }
47
+ getGlobals() {
48
+ return this.globals;
49
+ }
50
+ setGlobals(globals) {
51
+ this.globals = globals;
52
+ }
53
+ /**
54
+ * Find out all locals in the parent method which are used by the childrenChain, these locals are the closures of the root node of the childrenChain.
55
+ * childrenChain contains all nested method from the root node of the childrenChain.
56
+ * baseLocals are all locals defined in the outer function.
57
+ * allNestedLocals are collect all locals defined in all outer functions of this childrenChain.
58
+ * Only the globals of the root of the childrenChain, which are in the baseLocals but not in the allNestedLocals are the actual closures that in baseLocals.
59
+ */
60
+ findClosuresUsedInNested(childrenChain, baseLocals, allNestedLocals) {
61
+ var _a, _b;
62
+ let closuresRes = [];
63
+ const nestedMethod = childrenChain.parent;
64
+ let nestedGlobals = (_a = nestedMethod.getBodyBuilder()) === null || _a === void 0 ? void 0 : _a.getGlobals();
65
+ if (nestedGlobals !== undefined) {
66
+ for (let global of nestedGlobals.values()) {
67
+ const nestedLocal = allNestedLocals.get(global.getName());
68
+ const closure = baseLocals.get(global.getName());
69
+ if (nestedLocal === undefined && closure !== undefined) {
70
+ closuresRes.push(closure);
71
+ }
72
+ }
73
+ }
74
+ const children = childrenChain.children;
75
+ if (children === null) {
76
+ return closuresRes;
77
+ }
78
+ for (let chain of children) {
79
+ const nestedLocals = (_b = nestedMethod.getBody()) === null || _b === void 0 ? void 0 : _b.getLocals();
80
+ if (nestedLocals !== undefined) {
81
+ nestedLocals.forEach((value, key) => {
82
+ allNestedLocals.set(key, value);
83
+ });
84
+ }
85
+ const closures = this.findClosuresUsedInNested(chain, baseLocals, allNestedLocals);
86
+ if (closures) {
87
+ closuresRes.push(...closures);
88
+ }
89
+ }
90
+ return closuresRes;
91
+ }
92
+ /**
93
+ * 1. Find out all locals in the parent method which are used by the childrenChain, these locals are the closures of the root node of the childrenChain.
94
+ * 2. Create a lexical env local in the parent method, and pass it to root node of the childrenChain through the method signature.
95
+ * 3. Update the root node of the childrenChain to add parameterRef assign stmt and closureRef assign stmt.
96
+ * 4. Recursively do this for all nested method level by level.
97
+ */
98
+ buildLexicalEnv(childrenChain, baseLocals, index) {
99
+ var _a;
100
+ let usedClosures = this.findClosuresUsedInNested(childrenChain, baseLocals, new Map);
101
+ const nestedMethod = childrenChain.parent;
102
+ const nestedSignature = nestedMethod.getImplementationSignature();
103
+ if (nestedSignature !== null && usedClosures !== null && usedClosures.length > 0) {
104
+ let lexicalEnv = new Type_1.LexicalEnvType(nestedSignature, usedClosures);
105
+ const closuresLocal = new Local_1.Local(`${Const_1.LEXICAL_ENV_NAME_PREFIX}${index++}`, lexicalEnv);
106
+ baseLocals.set(closuresLocal.getName(), closuresLocal);
107
+ this.updateNestedMethodWithClosures(nestedMethod, closuresLocal);
108
+ }
109
+ else if (usedClosures === null || usedClosures.length === 0) {
110
+ this.moveCurrentMethodLocalToGlobal(nestedMethod);
111
+ }
112
+ const nextNestedChains = childrenChain.children;
113
+ if (nextNestedChains === null) {
114
+ return index;
115
+ }
116
+ for (let nextChain of nextNestedChains) {
117
+ const newBaseLocals = (_a = nestedMethod.getBody()) === null || _a === void 0 ? void 0 : _a.getLocals();
118
+ if (newBaseLocals === undefined) {
119
+ return index;
120
+ }
121
+ index = this.buildLexicalEnv(nextChain, newBaseLocals, index);
122
+ }
123
+ return index;
124
+ }
125
+ /**
126
+ * Find out and tag all closures from globals, and remove closures from both globals and locals.
127
+ * Precondition: body build has been done. All locals, globals and closures are both set as Local in body,
128
+ * while potential globals and closures are also recorded in bodybuilder.
129
+ * Constraint: only the outermost function can call this method to recursively handle closures of itself as well as all nested methods.
130
+ */
131
+ handleGlobalAndClosure() {
132
+ var _a, _b, _c;
133
+ /**
134
+ * Step1: Handle the outermost function, take it as Level 0.
135
+ * There must be no closures in Level 0. So only need to remove the locals which with the same name as the ones in globals.
136
+ */
137
+ let outerMethod = this.getCfgBuilder().getDeclaringMethod();
138
+ let outerGlobals = (_a = outerMethod.getBodyBuilder()) === null || _a === void 0 ? void 0 : _a.getGlobals();
139
+ outerMethod.freeBodyBuilder();
140
+ let outerLocals = (_b = outerMethod.getBody()) === null || _b === void 0 ? void 0 : _b.getLocals();
141
+ if (outerGlobals !== undefined && outerLocals !== undefined) {
142
+ outerGlobals.forEach((value, key) => {
143
+ const local = outerLocals.get(key);
144
+ if (local !== undefined) {
145
+ value.addUsedStmts(local.getUsedStmts());
146
+ outerLocals.delete(key);
147
+ }
148
+ });
149
+ if (outerGlobals.size > 0) {
150
+ (_c = outerMethod.getBody()) === null || _c === void 0 ? void 0 : _c.setUsedGlobals(outerGlobals);
151
+ }
152
+ }
153
+ let nestedMethodChains = this.generateNestedMethodChains(outerMethod).children;
154
+ if (nestedMethodChains === null || outerLocals === undefined) {
155
+ return;
156
+ }
157
+ let closuresIndex = 0;
158
+ for (let nestedChain of nestedMethodChains) {
159
+ /**
160
+ * Step2: Handle each nested function in Level 1 one by one.
161
+ * Find out all closures from Level 0 used by these Level 1 functions as well as all their children nested functions.
162
+ * This will be done level by level recursively.
163
+ */
164
+ closuresIndex = this.buildLexicalEnv(nestedChain, outerLocals, closuresIndex);
165
+ /**
166
+ * Step3: Delete old globals which are recognized as closures, then the rest globals are the true globals.
167
+ * The redundancy locals should be deleted but the used stmts of them should be restored to globals.
168
+ * This will be done level by level recursively.
169
+ */
170
+ this.reorganizeGlobalAndLocal(nestedChain);
171
+ /**
172
+ * Step4: Infer UnclearReferenceType to check whether it is the type alias define in its parent function..
173
+ */
174
+ this.inferTypesDefineInOuter(outerMethod, nestedChain);
175
+ /**
176
+ * Step5: For each nested function, find out whether it is called by its parent function and update the related locals, globals and stmts.
177
+ */
178
+ this.updateNestedMethodUsedInOuter(nestedChain);
179
+ this.freeBodyBuilder(nestedChain);
180
+ }
181
+ }
182
+ freeBodyBuilder(nestedChain) {
183
+ nestedChain.parent.freeBodyBuilder();
184
+ const childrenChains = nestedChain.children;
185
+ if (childrenChains === null) {
186
+ return;
187
+ }
188
+ for (const chain of childrenChains) {
189
+ this.freeBodyBuilder(chain);
190
+ }
191
+ }
192
+ updateLocalTypesWithTypeAlias(locals, typeAliases) {
193
+ for (let local of locals.values()) {
194
+ const newType = this.inferUnclearReferenceTypeWithTypeAlias(local.getType(), typeAliases);
195
+ if (newType !== null) {
196
+ local.setType(newType);
197
+ }
198
+ }
199
+ }
200
+ inferUnclearReferenceTypeWithTypeAlias(localType, typeAliases) {
201
+ if (localType instanceof Type_1.ArrayType && localType.getBaseType() instanceof Type_1.UnclearReferenceType) {
202
+ const typeAlias = typeAliases.get(localType.getBaseType().getName());
203
+ if (typeAlias !== undefined) {
204
+ localType.setBaseType(typeAlias[0]);
205
+ return localType;
206
+ }
207
+ return null;
208
+ }
209
+ if (localType instanceof Type_1.UnionType) {
210
+ const optionTypes = localType.getTypes();
211
+ for (let i = 0; i < optionTypes.length; i++) {
212
+ const newType = this.inferUnclearReferenceTypeWithTypeAlias(optionTypes[i], typeAliases);
213
+ if (newType !== null) {
214
+ optionTypes[i] = newType;
215
+ }
216
+ }
217
+ return localType;
218
+ }
219
+ if (localType instanceof Type_1.UnclearReferenceType) {
220
+ const typeAlias = typeAliases.get(localType.getName());
221
+ if (typeAlias !== undefined) {
222
+ return typeAlias[0];
223
+ }
224
+ }
225
+ return null;
226
+ }
227
+ generateNestedMethodChains(outerMethod) {
228
+ let candidateMethods = [];
229
+ outerMethod.getDeclaringArkClass().getMethods().forEach(method => {
230
+ if (method.getName().startsWith(Const_1.NAME_PREFIX) && method.getName().endsWith(`${Const_1.NAME_DELIMITER}${outerMethod.getName()}`)) {
231
+ candidateMethods.push(method);
232
+ }
233
+ });
234
+ const childrenChains = this.getNestedChildrenChains(outerMethod, candidateMethods);
235
+ if (childrenChains.length > 0) {
236
+ return { parent: outerMethod, children: childrenChains };
237
+ }
238
+ return { parent: outerMethod, children: null };
239
+ }
240
+ getNestedChildrenChains(parentMethod, candidateMethods) {
241
+ var _a;
242
+ let nestedMethodChain = [];
243
+ for (let method of candidateMethods) {
244
+ const outerMethodSignature = (_a = method.getOuterMethod()) === null || _a === void 0 ? void 0 : _a.getSignature();
245
+ if (outerMethodSignature !== undefined && (0, ArkSignature_1.methodSignatureCompare)(parentMethod.getSignature(), outerMethodSignature)) {
246
+ const childrenChains = this.getNestedChildrenChains(method, candidateMethods);
247
+ if (childrenChains.length > 0) {
248
+ nestedMethodChain.push({ parent: method, children: childrenChains });
249
+ }
250
+ else {
251
+ nestedMethodChain.push({ parent: method, children: null });
252
+ }
253
+ }
254
+ }
255
+ return nestedMethodChain;
256
+ }
257
+ moveCurrentMethodLocalToGlobal(method) {
258
+ var _a, _b, _c;
259
+ const globals = (_a = method.getBodyBuilder()) === null || _a === void 0 ? void 0 : _a.getGlobals();
260
+ const locals = (_b = method.getBody()) === null || _b === void 0 ? void 0 : _b.getLocals();
261
+ if (locals === undefined || globals === undefined) {
262
+ return;
263
+ }
264
+ globals.forEach((value, key) => {
265
+ const local = locals.get(key);
266
+ if (local !== undefined) {
267
+ value.addUsedStmts(local.getUsedStmts());
268
+ locals.delete(key);
269
+ }
270
+ });
271
+ if (globals.size > 0) {
272
+ (_c = method.getBody()) === null || _c === void 0 ? void 0 : _c.setUsedGlobals(globals);
273
+ }
274
+ }
275
+ reorganizeGlobalAndLocal(nestedChain) {
276
+ var _a;
277
+ const nestedMethod = nestedChain.parent;
278
+ const params = nestedMethod.getSubSignature().getParameters();
279
+ const globals = (_a = nestedMethod.getBodyBuilder()) === null || _a === void 0 ? void 0 : _a.getGlobals();
280
+ if (params.length > 0 && params[0].getType() instanceof Type_1.LexicalEnvType && globals !== undefined) {
281
+ const closures = params[0].getType().getClosures();
282
+ for (let closure of closures) {
283
+ globals.delete(closure.getName());
284
+ }
285
+ }
286
+ this.moveCurrentMethodLocalToGlobal(nestedMethod);
287
+ const childrenChains = nestedChain.children;
288
+ if (childrenChains === null) {
289
+ return;
290
+ }
291
+ for (const chain of childrenChains) {
292
+ this.reorganizeGlobalAndLocal(chain);
293
+ }
294
+ }
295
+ // 对嵌套函数中的UnclearReferenceType类型的变量进行类型推导,类型是否为外层函数中定义的类型别名
296
+ inferTypesDefineInOuter(outerMethod, childrenChain) {
297
+ var _a, _b;
298
+ const typeAliases = (_a = outerMethod.getBody()) === null || _a === void 0 ? void 0 : _a.getAliasTypeMap();
299
+ const nestedLocals = (_b = childrenChain.parent.getBody()) === null || _b === void 0 ? void 0 : _b.getLocals();
300
+ if (typeAliases !== undefined && nestedLocals !== undefined) {
301
+ this.updateLocalTypesWithTypeAlias(nestedLocals, typeAliases);
302
+ }
303
+ const childrenChains = childrenChain.children;
304
+ if (childrenChains === null) {
305
+ return;
306
+ }
307
+ for (const chain of childrenChains) {
308
+ this.inferTypesDefineInOuter(childrenChain.parent, chain);
309
+ }
310
+ }
311
+ updateNestedMethodUsedInOuter(nestedChain) {
312
+ var _a, _b, _c, _d;
313
+ const nestedMethod = nestedChain.parent;
314
+ const outerMethod = nestedMethod.getOuterMethod();
315
+ if (outerMethod === undefined) {
316
+ return;
317
+ }
318
+ const outerLocals = (_a = outerMethod.getBody()) === null || _a === void 0 ? void 0 : _a.getLocals();
319
+ if (outerLocals !== undefined) {
320
+ for (let local of outerLocals.values()) {
321
+ if (local.getType() instanceof Type_1.LexicalEnvType &&
322
+ (0, ArkSignature_1.methodSignatureCompare)(local.getType().getNestedMethod(), nestedMethod.getSignature())) {
323
+ this.updateOuterMethodWithClosures(outerMethod, nestedMethod, local);
324
+ break;
325
+ }
326
+ }
327
+ }
328
+ const nestedMethodName = nestedMethod.getName();
329
+ const originalMethodName = (_b = this.getOriginalNestedMethodName(nestedMethodName)) !== null && _b !== void 0 ? _b : '';
330
+ const outerGlobals = (_c = outerMethod.getBody()) === null || _c === void 0 ? void 0 : _c.getUsedGlobals();
331
+ const callGlobal = (_d = outerGlobals === null || outerGlobals === void 0 ? void 0 : outerGlobals.get(nestedMethodName)) !== null && _d !== void 0 ? _d : outerGlobals === null || outerGlobals === void 0 ? void 0 : outerGlobals.get(originalMethodName);
332
+ if (callGlobal !== undefined && callGlobal instanceof Ref_1.GlobalRef && callGlobal.getRef() === null) {
333
+ const fieldSignature = new ArkSignature_1.FieldSignature(nestedMethodName, nestedMethod.getDeclaringArkClass().getSignature(), new Type_1.FunctionType(nestedMethod.getSignature()));
334
+ callGlobal.setRef(new Ref_1.ArkStaticFieldRef((fieldSignature)));
335
+ }
336
+ const childrenChains = nestedChain.children;
337
+ if (childrenChains === null) {
338
+ return;
339
+ }
340
+ for (const chain of childrenChains) {
341
+ this.updateNestedMethodUsedInOuter(chain);
342
+ }
343
+ }
344
+ updateNestedMethodWithClosures(nestedMethod, closuresLocal) {
345
+ if (!(closuresLocal.getType() instanceof Type_1.LexicalEnvType)) {
346
+ return;
347
+ }
348
+ const declareSignatures = nestedMethod.getDeclareSignatures();
349
+ declareSignatures === null || declareSignatures === void 0 ? void 0 : declareSignatures.forEach((signature, index) => {
350
+ nestedMethod.setDeclareSignatureWithIndex(this.createNewSignatureWithClosures(closuresLocal, signature), index);
351
+ });
352
+ const implementSignature = nestedMethod.getImplementationSignature();
353
+ if (implementSignature !== null) {
354
+ nestedMethod.setImplementationSignature(this.createNewSignatureWithClosures(closuresLocal, implementSignature));
355
+ }
356
+ this.addClosureParamsAssignStmts(closuresLocal, nestedMethod);
357
+ }
358
+ updateOuterMethodWithClosures(outerMethod, nestedMethod, closuresLocal) {
359
+ var _a, _b, _c, _d, _e, _f;
360
+ const nestedMethodName = nestedMethod.getName();
361
+ const nestedMethodLocal = (_a = outerMethod.getBody()) === null || _a === void 0 ? void 0 : _a.getLocals().get(nestedMethodName);
362
+ if (nestedMethodLocal !== undefined) {
363
+ this.updateLocalInfoWithClosures(nestedMethodLocal, outerMethod, nestedMethod, closuresLocal);
364
+ }
365
+ else {
366
+ const nestedMethodGlobal = (_c = (_b = outerMethod.getBody()) === null || _b === void 0 ? void 0 : _b.getUsedGlobals()) === null || _c === void 0 ? void 0 : _c.get(nestedMethodName);
367
+ if (nestedMethodGlobal !== undefined && nestedMethodGlobal instanceof Ref_1.GlobalRef) {
368
+ this.updateGlobalInfoWithClosures(nestedMethodGlobal, outerMethod, nestedMethod, closuresLocal);
369
+ }
370
+ }
371
+ const originalMethodName = this.getOriginalNestedMethodName(nestedMethodName);
372
+ if (originalMethodName === null) {
373
+ return;
374
+ }
375
+ const originalMethodLocal = (_d = outerMethod.getBody()) === null || _d === void 0 ? void 0 : _d.getLocals().get(originalMethodName);
376
+ if (originalMethodLocal !== undefined) {
377
+ this.updateLocalInfoWithClosures(originalMethodLocal, outerMethod, nestedMethod, closuresLocal);
378
+ }
379
+ else {
380
+ const originalMethodGlobal = (_f = (_e = outerMethod.getBody()) === null || _e === void 0 ? void 0 : _e.getUsedGlobals()) === null || _f === void 0 ? void 0 : _f.get(originalMethodName);
381
+ if (originalMethodGlobal !== undefined && originalMethodGlobal instanceof Ref_1.GlobalRef) {
382
+ this.updateGlobalInfoWithClosures(originalMethodGlobal, outerMethod, nestedMethod, closuresLocal);
383
+ }
384
+ }
385
+ }
386
+ getOriginalNestedMethodName(nestedMethodName) {
387
+ if (nestedMethodName.startsWith(Const_1.NAME_PREFIX) && nestedMethodName.includes(Const_1.NAME_DELIMITER)) {
388
+ const nameComponents = nestedMethodName.slice(1).split(Const_1.NAME_DELIMITER);
389
+ if (nameComponents.length > 1) {
390
+ return nameComponents[0];
391
+ }
392
+ }
393
+ return null;
394
+ }
395
+ updateGlobalInfoWithClosures(globalRef, outerMethod, nestedMethod, closuresLocal) {
396
+ if (globalRef.getRef() !== null) {
397
+ return;
398
+ }
399
+ const methodSignature = nestedMethod.getImplementationSignature();
400
+ if (methodSignature === null) {
401
+ return;
402
+ }
403
+ const lexicalEnv = closuresLocal.getType();
404
+ if (!(lexicalEnv instanceof Type_1.LexicalEnvType)) {
405
+ return;
406
+ }
407
+ const fieldSignature = new ArkSignature_1.FieldSignature(methodSignature.getMethodSubSignature().getMethodName(), methodSignature.getDeclaringClassSignature(), new Type_1.ClosureType(lexicalEnv, methodSignature));
408
+ globalRef.setRef(new Ref_1.ArkStaticFieldRef((fieldSignature)));
409
+ this.updateAbstractInvokeExprWithClosures(globalRef, outerMethod.getSignature(), nestedMethod.getSignature(), closuresLocal);
410
+ }
411
+ updateLocalInfoWithClosures(local, outerMethod, nestedMethod, closuresLocal) {
412
+ const localType = local.getType();
413
+ if (!(localType instanceof Type_1.FunctionType)) {
414
+ return;
415
+ }
416
+ const lexicalEnv = closuresLocal.getType();
417
+ if (!(lexicalEnv instanceof Type_1.LexicalEnvType)) {
418
+ return;
419
+ }
420
+ // 更新local的类型为ClosureType,methodSignature为内层嵌套函数
421
+ const nestedMethodSignature = nestedMethod.getImplementationSignature();
422
+ if (nestedMethodSignature !== null) {
423
+ local.setType(new Type_1.ClosureType(lexicalEnv, nestedMethodSignature, localType.getRealGenericTypes()));
424
+ }
425
+ else {
426
+ local.setType(new Type_1.ClosureType(lexicalEnv, localType.getMethodSignature(), localType.getRealGenericTypes()));
427
+ }
428
+ this.updateAbstractInvokeExprWithClosures(local, outerMethod.getSignature(), nestedMethod.getSignature(), closuresLocal);
429
+ }
430
+ // 更新所有stmt中调用内层函数处的AbstractInvokeExpr中的函数签名和实参args,加入闭包参数
431
+ // 更新所有stmt中定义的函数指针的usedStmt中的函数签名和实参args,加入闭包参数
432
+ updateAbstractInvokeExprWithClosures(value, outerMethodSignature, nestedMethodSignature, closuresLocal) {
433
+ for (const usedStmt of value.getUsedStmts()) {
434
+ if (usedStmt instanceof Stmt_1.ArkInvokeStmt) {
435
+ this.updateSignatureAndArgsInArkInvokeExpr(usedStmt, nestedMethodSignature, closuresLocal);
436
+ }
437
+ else if (usedStmt instanceof Stmt_1.ArkAssignStmt) {
438
+ const rightOp = usedStmt.getRightOp();
439
+ if (rightOp instanceof Expr_1.AbstractInvokeExpr) {
440
+ this.updateSignatureAndArgsInArkInvokeExpr(usedStmt, nestedMethodSignature, closuresLocal);
441
+ }
442
+ const leftOp = usedStmt.getLeftOp();
443
+ if (leftOp instanceof Local_1.Local) {
444
+ leftOp.setType(rightOp.getType());
445
+ }
446
+ }
447
+ else if (usedStmt instanceof Stmt_1.ArkReturnStmt) {
448
+ outerMethodSignature.getMethodSubSignature().setReturnType(value.getType());
449
+ }
450
+ const defValue = usedStmt.getDef();
451
+ if (defValue === null) {
452
+ continue;
453
+ }
454
+ if ((defValue instanceof Local_1.Local || defValue instanceof Ref_1.GlobalRef) && defValue.getType() instanceof Type_1.FunctionType) {
455
+ this.updateAbstractInvokeExprWithClosures(defValue, outerMethodSignature, nestedMethodSignature, closuresLocal);
456
+ }
457
+ }
458
+ }
459
+ createNewSignatureWithClosures(closuresLocal, oldSignature) {
460
+ let oldSubSignature = oldSignature.getMethodSubSignature();
461
+ const params = oldSubSignature.getParameters();
462
+ const closuresParam = new ArkMethodBuilder_1.MethodParameter();
463
+ closuresParam.setName(closuresLocal.getName());
464
+ closuresParam.setType(closuresLocal.getType());
465
+ params.unshift(closuresParam);
466
+ let newSubSignature = new ArkSignature_1.MethodSubSignature(oldSubSignature.getMethodName(), params, oldSubSignature.getReturnType(), oldSubSignature.isStatic());
467
+ return new ArkSignature_1.MethodSignature(oldSignature.getDeclaringClassSignature(), newSubSignature);
468
+ }
469
+ updateSignatureAndArgsInArkInvokeExpr(stmt, methodSignature, closuresLocal) {
470
+ let expr;
471
+ if (stmt instanceof Stmt_1.ArkInvokeStmt) {
472
+ expr = stmt.getInvokeExpr();
473
+ }
474
+ else {
475
+ const rightOp = stmt.getRightOp();
476
+ if (!(rightOp instanceof Expr_1.AbstractInvokeExpr)) {
477
+ return;
478
+ }
479
+ expr = rightOp;
480
+ }
481
+ const exprMethodName = expr.getMethodSignature().getMethodSubSignature().getMethodName();
482
+ const nestedMethodName = methodSignature.getMethodSubSignature().getMethodName();
483
+ if (exprMethodName === nestedMethodName) {
484
+ expr.setMethodSignature(this.createNewSignatureWithClosures(closuresLocal, methodSignature));
485
+ expr.getArgs().unshift(closuresLocal);
486
+ closuresLocal.addUsedStmt(stmt);
487
+ return;
488
+ }
489
+ const originalMethodName = this.getOriginalNestedMethodName(nestedMethodName);
490
+ if (originalMethodName !== null) {
491
+ if (exprMethodName === originalMethodName || expr instanceof Expr_1.ArkPtrInvokeExpr) {
492
+ expr.setMethodSignature(methodSignature);
493
+ expr.getArgs().unshift(closuresLocal);
494
+ closuresLocal.addUsedStmt(stmt);
495
+ }
496
+ }
497
+ }
498
+ addClosureParamsAssignStmts(closuresParam, method) {
499
+ const lexicalEnv = closuresParam.getType();
500
+ if (!(lexicalEnv instanceof Type_1.LexicalEnvType)) {
501
+ return;
502
+ }
503
+ const closures = lexicalEnv.getClosures();
504
+ if (closures.length === 0) {
505
+ return;
506
+ }
507
+ const oldParamRefs = method.getParameterRefs();
508
+ let body = method.getBody();
509
+ if (body === undefined) {
510
+ return;
511
+ }
512
+ let stmts = Array.from(body.getCfg().getBlocks())[0].getStmts();
513
+ let index = 0;
514
+ const parameterRef = new Ref_1.ArkParameterRef(index, lexicalEnv);
515
+ const closuresLocal = new Local_1.Local(closuresParam.getName(), lexicalEnv);
516
+ body.addLocal(closuresLocal.getName(), closuresLocal);
517
+ let assignStmt = new Stmt_1.ArkAssignStmt(closuresLocal, parameterRef);
518
+ stmts.splice(index, 0, assignStmt);
519
+ closuresLocal.setDeclaringStmt(assignStmt);
520
+ oldParamRefs === null || oldParamRefs === void 0 ? void 0 : oldParamRefs.forEach((paramRef) => {
521
+ index++;
522
+ paramRef.setIndex(index);
523
+ });
524
+ for (let closure of closures) {
525
+ let local = body.getLocals().get(closure.getName());
526
+ if (local === undefined) {
527
+ local = new Local_1.Local(closure.getName(), closure.getType());
528
+ body.addLocal(local.getName(), local);
529
+ }
530
+ else {
531
+ local.setType(closure.getType());
532
+ }
533
+ index++;
534
+ const closureFieldRef = new Ref_1.ClosureFieldRef(closuresParam, closure.getName(), closure.getType());
535
+ let assignStmt = new Stmt_1.ArkAssignStmt(local, closureFieldRef);
536
+ stmts.splice(index, 0, assignStmt);
537
+ local.setDeclaringStmt(assignStmt);
538
+ closuresLocal.addUsedStmt(assignStmt);
539
+ }
540
+ }
541
+ }
542
+ exports.BodyBuilder = BodyBuilder;