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.
- package/lib/Config.d.ts +70 -70
- package/lib/Config.js +198 -198
- package/lib/Scene.d.ts +316 -316
- package/lib/Scene.js +1316 -1316
- package/lib/callgraph/algorithm/AbstractAnalysis.d.ts +28 -28
- package/lib/callgraph/algorithm/AbstractAnalysis.js +144 -144
- package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts +10 -10
- package/lib/callgraph/algorithm/ClassHierarchyAnalysis.js +69 -69
- package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts +15 -15
- package/lib/callgraph/algorithm/RapidTypeAnalysis.js +146 -146
- package/lib/callgraph/common/Statistics.d.ts +57 -57
- package/lib/callgraph/common/Statistics.js +228 -228
- package/lib/callgraph/model/CallGraph.d.ts +96 -96
- package/lib/callgraph/model/CallGraph.js +354 -354
- package/lib/callgraph/model/builder/CallGraphBuilder.d.ts +15 -15
- package/lib/callgraph/model/builder/CallGraphBuilder.js +106 -106
- package/lib/callgraph/pointerAnalysis/Context.d.ts +37 -37
- package/lib/callgraph/pointerAnalysis/Context.js +155 -155
- package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts +22 -22
- package/lib/callgraph/pointerAnalysis/DummyCallCreator.js +104 -104
- package/lib/callgraph/pointerAnalysis/PTAUtils.d.ts +5 -5
- package/lib/callgraph/pointerAnalysis/PTAUtils.js +46 -46
- package/lib/callgraph/pointerAnalysis/Pag.d.ts +261 -261
- package/lib/callgraph/pointerAnalysis/Pag.js +930 -930
- package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts +157 -157
- package/lib/callgraph/pointerAnalysis/PagBuilder.js +1399 -1399
- package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts +72 -72
- package/lib/callgraph/pointerAnalysis/PointerAnalysis.js +573 -573
- package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts +15 -15
- package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.js +81 -81
- package/lib/callgraph/pointerAnalysis/PtsDS.d.ts +80 -80
- package/lib/callgraph/pointerAnalysis/PtsDS.js +308 -308
- package/lib/core/base/Constant.d.ts +48 -48
- package/lib/core/base/Constant.js +103 -103
- package/lib/core/base/Decorator.d.ts +15 -15
- package/lib/core/base/Decorator.js +46 -46
- package/lib/core/base/DefUseChain.d.ts +8 -8
- package/lib/core/base/DefUseChain.js +25 -25
- package/lib/core/base/Expr.d.ts +340 -340
- package/lib/core/base/Expr.js +917 -917
- package/lib/core/base/Local.d.ts +104 -104
- package/lib/core/base/Local.js +176 -176
- package/lib/core/base/Position.d.ts +30 -30
- package/lib/core/base/Position.js +147 -147
- package/lib/core/base/Ref.d.ts +173 -173
- package/lib/core/base/Ref.js +374 -374
- package/lib/core/base/Stmt.d.ts +237 -237
- package/lib/core/base/Stmt.js +525 -525
- package/lib/core/base/Trap.d.ts +8 -8
- package/lib/core/base/Trap.js +30 -30
- package/lib/core/base/Type.d.ts +308 -308
- package/lib/core/base/Type.js +639 -639
- package/lib/core/base/TypeExpr.d.ts +71 -71
- package/lib/core/base/TypeExpr.js +155 -155
- package/lib/core/base/Value.d.ts +32 -32
- package/lib/core/base/Value.js +16 -16
- package/lib/core/common/ArkError.d.ts +14 -14
- package/lib/core/common/ArkError.js +28 -28
- package/lib/core/common/ArkIRTransformer.d.ts +67 -67
- package/lib/core/common/ArkIRTransformer.js +690 -690
- package/lib/core/common/ArkValueTransformer.d.ts +89 -89
- package/lib/core/common/ArkValueTransformer.js +1478 -1478
- package/lib/core/common/Builtin.d.ts +36 -36
- package/lib/core/common/Builtin.js +81 -81
- package/lib/core/common/Const.d.ts +21 -21
- package/lib/core/common/Const.js +43 -43
- package/lib/core/common/DummyMainCreater.d.ts +46 -46
- package/lib/core/common/DummyMainCreater.js +319 -319
- package/lib/core/common/EtsConst.d.ts +73 -73
- package/lib/core/common/EtsConst.js +1021 -1021
- package/lib/core/common/ExprUseReplacer.d.ts +21 -21
- package/lib/core/common/ExprUseReplacer.js +124 -124
- package/lib/core/common/IRInference.d.ts +36 -36
- package/lib/core/common/IRInference.d.ts.map +1 -1
- package/lib/core/common/IRInference.js +651 -648
- package/lib/core/common/IRUtils.d.ts +18 -18
- package/lib/core/common/IRUtils.js +141 -141
- package/lib/core/common/ModelUtils.d.ts +70 -70
- package/lib/core/common/ModelUtils.js +712 -712
- package/lib/core/common/RefUseReplacer.d.ts +13 -13
- package/lib/core/common/RefUseReplacer.js +52 -52
- package/lib/core/common/SdkUtils.d.ts +8 -8
- package/lib/core/common/SdkUtils.js +105 -105
- package/lib/core/common/StmtDefReplacer.d.ts +12 -12
- package/lib/core/common/StmtDefReplacer.js +41 -41
- package/lib/core/common/StmtUseReplacer.d.ts +16 -16
- package/lib/core/common/StmtUseReplacer.js +108 -108
- package/lib/core/common/TSConst.d.ts +18 -18
- package/lib/core/common/TSConst.js +36 -36
- package/lib/core/common/TypeInference.d.ts +102 -102
- package/lib/core/common/TypeInference.js +748 -748
- package/lib/core/common/ValueUtil.d.ts +12 -12
- package/lib/core/common/ValueUtil.js +54 -54
- package/lib/core/common/VisibleValue.d.ts +36 -36
- package/lib/core/common/VisibleValue.js +212 -212
- package/lib/core/dataflow/DataflowProblem.d.ts +20 -20
- package/lib/core/dataflow/DataflowProblem.js +20 -20
- package/lib/core/dataflow/DataflowResult.d.ts +7 -7
- package/lib/core/dataflow/DataflowResult.js +26 -26
- package/lib/core/dataflow/DataflowSolver.d.ts +43 -43
- package/lib/core/dataflow/DataflowSolver.js +307 -307
- package/lib/core/dataflow/Edge.d.ts +11 -11
- package/lib/core/dataflow/Edge.js +31 -31
- package/lib/core/dataflow/Fact.d.ts +6 -6
- package/lib/core/dataflow/Fact.js +24 -24
- package/lib/core/dataflow/GenericDataFlow.d.ts +142 -142
- package/lib/core/dataflow/GenericDataFlow.js +109 -109
- package/lib/core/dataflow/ReachingDef.d.ts +62 -62
- package/lib/core/dataflow/ReachingDef.js +168 -168
- package/lib/core/dataflow/UndefinedVariable.d.ts +45 -45
- package/lib/core/dataflow/UndefinedVariable.js +288 -288
- package/lib/core/dataflow/Util.d.ts +9 -9
- package/lib/core/dataflow/Util.js +64 -64
- package/lib/core/graph/BaseExplicitGraph.d.ts +58 -58
- package/lib/core/graph/BaseExplicitGraph.js +157 -157
- package/lib/core/graph/BaseImplicitGraph.d.ts +77 -77
- package/lib/core/graph/BaseImplicitGraph.js +78 -78
- package/lib/core/graph/BasicBlock.d.ts +104 -104
- package/lib/core/graph/BasicBlock.js +280 -280
- package/lib/core/graph/Cfg.d.ts +59 -59
- package/lib/core/graph/Cfg.js +282 -282
- package/lib/core/graph/DependsGraph.d.ts +35 -35
- package/lib/core/graph/DependsGraph.js +87 -87
- package/lib/core/graph/DominanceFinder.d.ts +15 -15
- package/lib/core/graph/DominanceFinder.js +121 -121
- package/lib/core/graph/DominanceTree.d.ts +12 -12
- package/lib/core/graph/DominanceTree.js +70 -70
- package/lib/core/graph/GraphTraits.d.ts +7 -7
- package/lib/core/graph/GraphTraits.js +16 -16
- package/lib/core/graph/Scc.d.ts +61 -61
- package/lib/core/graph/Scc.js +222 -222
- package/lib/core/graph/ViewTree.d.ts +114 -114
- package/lib/core/graph/ViewTree.js +16 -16
- package/lib/core/graph/builder/CfgBuilder.d.ts +176 -176
- package/lib/core/graph/builder/CfgBuilder.js +1139 -1139
- package/lib/core/graph/builder/ConditionBuilder.d.ts +15 -15
- package/lib/core/graph/builder/ConditionBuilder.js +252 -252
- package/lib/core/graph/builder/LoopBuilder.d.ts +20 -20
- package/lib/core/graph/builder/LoopBuilder.js +251 -251
- package/lib/core/graph/builder/SwitchBuilder.d.ts +11 -11
- package/lib/core/graph/builder/SwitchBuilder.js +152 -152
- package/lib/core/graph/builder/TrapBuilder.d.ts +16 -16
- package/lib/core/graph/builder/TrapBuilder.js +272 -272
- package/lib/core/graph/builder/ViewTreeBuilder.d.ts +203 -203
- package/lib/core/graph/builder/ViewTreeBuilder.js +1055 -1055
- package/lib/core/model/ArkBaseModel.d.ts +59 -59
- package/lib/core/model/ArkBaseModel.js +274 -274
- package/lib/core/model/ArkBody.d.ts +24 -24
- package/lib/core/model/ArkBody.js +65 -65
- package/lib/core/model/ArkClass.d.ts +206 -206
- package/lib/core/model/ArkClass.js +439 -439
- package/lib/core/model/ArkExport.d.ts +72 -72
- package/lib/core/model/ArkExport.js +147 -147
- package/lib/core/model/ArkField.d.ts +62 -62
- package/lib/core/model/ArkField.js +113 -113
- package/lib/core/model/ArkFile.d.ts +131 -131
- package/lib/core/model/ArkFile.js +267 -267
- package/lib/core/model/ArkImport.d.ts +44 -44
- package/lib/core/model/ArkImport.js +108 -108
- package/lib/core/model/ArkMetadata.d.ts +31 -31
- package/lib/core/model/ArkMetadata.js +55 -55
- package/lib/core/model/ArkMethod.d.ts +261 -261
- package/lib/core/model/ArkMethod.js +592 -592
- package/lib/core/model/ArkNamespace.d.ts +66 -66
- package/lib/core/model/ArkNamespace.js +209 -209
- package/lib/core/model/ArkSignature.d.ts +162 -162
- package/lib/core/model/ArkSignature.js +379 -379
- package/lib/core/model/builder/ArkClassBuilder.d.ts +12 -12
- package/lib/core/model/builder/ArkClassBuilder.js +454 -454
- package/lib/core/model/builder/ArkExportBuilder.d.ts +24 -24
- package/lib/core/model/builder/ArkExportBuilder.js +210 -210
- package/lib/core/model/builder/ArkFieldBuilder.d.ts +8 -8
- package/lib/core/model/builder/ArkFieldBuilder.js +193 -193
- package/lib/core/model/builder/ArkFileBuilder.d.ts +9 -9
- package/lib/core/model/builder/ArkFileBuilder.js +165 -165
- package/lib/core/model/builder/ArkImportBuilder.d.ts +4 -4
- package/lib/core/model/builder/ArkImportBuilder.js +128 -128
- package/lib/core/model/builder/ArkMethodBuilder.d.ts +63 -63
- package/lib/core/model/builder/ArkMethodBuilder.js +486 -486
- package/lib/core/model/builder/ArkNamespaceBuilder.d.ts +5 -5
- package/lib/core/model/builder/ArkNamespaceBuilder.js +205 -205
- package/lib/core/model/builder/ArkSignatureBuilder.d.ts +8 -8
- package/lib/core/model/builder/ArkSignatureBuilder.js +40 -40
- package/lib/core/model/builder/BodyBuilder.d.ts +55 -30
- package/lib/core/model/builder/BodyBuilder.d.ts.map +1 -1
- package/lib/core/model/builder/BodyBuilder.js +542 -371
- package/lib/core/model/builder/builderUtils.d.ts +18 -18
- package/lib/core/model/builder/builderUtils.js +558 -558
- package/lib/index.d.ts +82 -80
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +201 -196
- package/lib/save/ArkStream.d.ts +23 -23
- package/lib/save/ArkStream.js +83 -83
- package/lib/save/DotPrinter.d.ts +43 -43
- package/lib/save/DotPrinter.js +170 -170
- package/lib/save/GraphPrinter.d.ts +16 -16
- package/lib/save/GraphPrinter.js +134 -134
- package/lib/save/JsonPrinter.d.ts +30 -30
- package/lib/save/JsonPrinter.js +580 -580
- package/lib/save/Printer.d.ts +12 -12
- package/lib/save/Printer.js +27 -27
- package/lib/save/PrinterBuilder.d.ts +53 -53
- package/lib/save/PrinterBuilder.js +145 -145
- package/lib/save/ViewTreePrinter.d.ts +14 -14
- package/lib/save/ViewTreePrinter.js +123 -123
- package/lib/save/arkir/ArkIRClassPrinter.d.ts +13 -13
- package/lib/save/arkir/ArkIRClassPrinter.js +92 -92
- package/lib/save/arkir/ArkIRFieldPrinter.d.ts +11 -11
- package/lib/save/arkir/ArkIRFieldPrinter.js +64 -64
- package/lib/save/arkir/ArkIRFilePrinter.d.ts +12 -12
- package/lib/save/arkir/ArkIRFilePrinter.js +54 -54
- package/lib/save/arkir/ArkIRMethodPrinter.d.ts +16 -16
- package/lib/save/arkir/ArkIRMethodPrinter.js +159 -159
- package/lib/save/arkir/ArkIRNamespacePrinter.d.ts +11 -11
- package/lib/save/arkir/ArkIRNamespacePrinter.js +66 -66
- package/lib/save/base/BasePrinter.d.ts +23 -23
- package/lib/save/base/BasePrinter.js +70 -70
- package/lib/save/base/ExportPrinter.d.ts +8 -8
- package/lib/save/base/ExportPrinter.js +67 -67
- package/lib/save/base/ImportPrinter.d.ts +9 -9
- package/lib/save/base/ImportPrinter.js +92 -92
- package/lib/save/base/PrinterUtils.d.ts +24 -24
- package/lib/save/base/PrinterUtils.js +208 -208
- package/lib/save/serializeArkIR.d.ts +8 -8
- package/lib/save/serializeArkIR.js +294 -294
- package/lib/save/source/SourceBase.d.ts +22 -22
- package/lib/save/source/SourceBase.js +64 -64
- package/lib/save/source/SourceBody.d.ts +58 -58
- package/lib/save/source/SourceBody.js +296 -296
- package/lib/save/source/SourceClass.d.ts +25 -25
- package/lib/save/source/SourceClass.js +187 -187
- package/lib/save/source/SourceField.d.ts +13 -13
- package/lib/save/source/SourceField.js +73 -73
- package/lib/save/source/SourceFilePrinter.d.ts +12 -12
- package/lib/save/source/SourceFilePrinter.js +69 -69
- package/lib/save/source/SourceMethod.d.ts +22 -22
- package/lib/save/source/SourceMethod.d.ts.map +1 -1
- package/lib/save/source/SourceMethod.js +196 -193
- package/lib/save/source/SourceNamespace.d.ts +11 -11
- package/lib/save/source/SourceNamespace.js +83 -83
- package/lib/save/source/SourceStmt.d.ts +178 -178
- package/lib/save/source/SourceStmt.js +838 -838
- package/lib/save/source/SourceTransformer.d.ts +46 -46
- package/lib/save/source/SourceTransformer.js +446 -446
- package/lib/transformer/FunctionTransformer.d.ts +2 -2
- package/lib/transformer/FunctionTransformer.js +17 -17
- package/lib/transformer/SceneTransformer.d.ts +2 -2
- package/lib/transformer/SceneTransformer.js +17 -17
- package/lib/transformer/StaticSingleAssignmentFormer.d.ts +12 -12
- package/lib/transformer/StaticSingleAssignmentFormer.js +259 -259
- package/lib/transformer/Transformer.d.ts +6 -6
- package/lib/transformer/Transformer.js +22 -22
- package/lib/utils/AstTreeUtils.d.ts +4 -4
- package/lib/utils/AstTreeUtils.js +26 -26
- package/lib/utils/CfgStructualAnalysis.d.ts +110 -110
- package/lib/utils/CfgStructualAnalysis.js +1277 -1277
- package/lib/utils/FileUtils.d.ts +18 -18
- package/lib/utils/FileUtils.js +135 -135
- package/lib/utils/SparseBitVector.d.ts +100 -100
- package/lib/utils/SparseBitVector.js +445 -445
- package/lib/utils/callGraphUtils.d.ts +30 -30
- package/lib/utils/callGraphUtils.js +205 -205
- package/lib/utils/crypto_utils.d.ts +5 -5
- package/lib/utils/crypto_utils.js +57 -57
- package/lib/utils/entryMethodUtils.d.ts +13 -13
- package/lib/utils/entryMethodUtils.js +110 -110
- package/lib/utils/getAllFiles.d.ts +9 -9
- package/lib/utils/getAllFiles.js +90 -90
- package/lib/utils/json5parser.d.ts +6 -6
- package/lib/utils/json5parser.js +146 -146
- package/lib/utils/logger.d.ts +18 -18
- package/lib/utils/logger.d.ts.map +1 -1
- package/lib/utils/logger.js +97 -90
- package/lib/utils/pathTransfer.d.ts +1 -1
- package/lib/utils/pathTransfer.js +25 -25
- package/node_modules/json5/lib/cli.js +0 -0
- package/node_modules/ohos-typescript/bin/tsc +0 -0
- package/node_modules/ohos-typescript/bin/tsserver +0 -0
- 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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
const
|
|
185
|
-
if (
|
|
186
|
-
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
if (
|
|
220
|
-
const
|
|
221
|
-
if (
|
|
222
|
-
return
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
return null;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
if (
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
const
|
|
314
|
-
const
|
|
315
|
-
if (
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
336
|
-
const
|
|
337
|
-
if (
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
const
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
}
|
|
371
|
-
|
|
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;
|