arkanalyzer 1.0.16 → 1.0.18
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/callgraph/pointerAnalysis/Pag.d.ts +1 -1
- package/lib/callgraph/pointerAnalysis/Pag.d.ts.map +1 -1
- package/lib/callgraph/pointerAnalysis/Pag.js +9 -0
- package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts.map +1 -1
- package/lib/callgraph/pointerAnalysis/PagBuilder.js +6 -2
- package/lib/core/base/Decorator.d.ts +1 -0
- package/lib/core/base/Decorator.d.ts.map +1 -1
- package/lib/core/base/Decorator.js +4 -1
- package/lib/core/base/Type.d.ts +45 -3
- package/lib/core/base/Type.d.ts.map +1 -1
- package/lib/core/base/Type.js +84 -4
- package/lib/core/common/ArkIRTransformer.d.ts +1 -0
- package/lib/core/common/ArkIRTransformer.d.ts.map +1 -1
- package/lib/core/common/ArkIRTransformer.js +42 -23
- package/lib/core/common/ArkValueTransformer.d.ts +1 -0
- package/lib/core/common/ArkValueTransformer.d.ts.map +1 -1
- package/lib/core/common/ArkValueTransformer.js +45 -10
- package/lib/core/common/IRInference.d.ts.map +1 -1
- package/lib/core/common/IRInference.js +6 -0
- package/lib/core/common/ModelUtils.d.ts.map +1 -1
- package/lib/core/common/ModelUtils.js +5 -1
- package/lib/core/common/TypeInference.d.ts +1 -0
- package/lib/core/common/TypeInference.d.ts.map +1 -1
- package/lib/core/common/TypeInference.js +27 -8
- package/lib/core/{common → graph/builder}/CfgBuilder.d.ts +34 -46
- package/lib/core/graph/builder/CfgBuilder.d.ts.map +1 -0
- package/lib/core/graph/builder/CfgBuilder.js +1139 -0
- package/lib/core/graph/builder/ConditionBuilder.d.ts +16 -0
- package/lib/core/graph/builder/ConditionBuilder.d.ts.map +1 -0
- package/lib/core/graph/builder/ConditionBuilder.js +252 -0
- package/lib/core/graph/builder/LoopBuilder.d.ts +21 -0
- package/lib/core/graph/builder/LoopBuilder.d.ts.map +1 -0
- package/lib/core/graph/builder/LoopBuilder.js +251 -0
- package/lib/core/graph/builder/SwitchBuilder.d.ts +12 -0
- package/lib/core/graph/builder/SwitchBuilder.d.ts.map +1 -0
- package/lib/core/graph/builder/SwitchBuilder.js +152 -0
- package/lib/core/graph/builder/TrapBuilder.d.ts +17 -0
- package/lib/core/graph/builder/TrapBuilder.d.ts.map +1 -0
- package/lib/core/graph/builder/TrapBuilder.js +272 -0
- package/lib/core/model/ArkMethod.d.ts +1 -1
- package/lib/core/model/ArkMethod.js +1 -1
- package/lib/core/model/ArkSignature.d.ts +9 -1
- package/lib/core/model/ArkSignature.d.ts.map +1 -1
- package/lib/core/model/ArkSignature.js +17 -1
- package/lib/core/model/builder/ArkClassBuilder.d.ts.map +1 -1
- package/lib/core/model/builder/ArkClassBuilder.js +2 -1
- package/lib/core/model/builder/ArkMethodBuilder.js +1 -1
- package/lib/core/{common → model/builder}/BodyBuilder.d.ts +5 -5
- package/lib/core/model/builder/BodyBuilder.d.ts.map +1 -0
- package/lib/core/{common → model/builder}/BodyBuilder.js +12 -12
- package/lib/core/model/builder/builderUtils.d.ts.map +1 -1
- package/lib/core/model/builder/builderUtils.js +31 -5
- package/lib/save/JsonPrinter.d.ts +1 -1
- package/lib/save/JsonPrinter.d.ts.map +1 -1
- package/lib/save/JsonPrinter.js +2 -2
- package/lib/save/PrinterBuilder.d.ts +22 -0
- package/lib/save/PrinterBuilder.d.ts.map +1 -1
- package/lib/save/PrinterBuilder.js +54 -1
- package/lib/save/arkir/ArkIRClassPrinter.d.ts +14 -0
- package/lib/save/arkir/ArkIRClassPrinter.d.ts.map +1 -0
- package/lib/save/arkir/ArkIRClassPrinter.js +92 -0
- package/lib/save/arkir/ArkIRFieldPrinter.d.ts +12 -0
- package/lib/save/arkir/ArkIRFieldPrinter.d.ts.map +1 -0
- package/lib/save/arkir/ArkIRFieldPrinter.js +64 -0
- package/lib/save/arkir/ArkIRFilePrinter.d.ts +13 -0
- package/lib/save/arkir/ArkIRFilePrinter.d.ts.map +1 -0
- package/lib/save/arkir/ArkIRFilePrinter.js +54 -0
- package/lib/save/arkir/ArkIRMethodPrinter.d.ts +17 -0
- package/lib/save/arkir/ArkIRMethodPrinter.d.ts.map +1 -0
- package/lib/save/arkir/ArkIRMethodPrinter.js +159 -0
- package/lib/save/arkir/ArkIRNamespacePrinter.d.ts +12 -0
- package/lib/save/arkir/ArkIRNamespacePrinter.d.ts.map +1 -0
- package/lib/save/arkir/ArkIRNamespacePrinter.js +66 -0
- package/lib/save/base/BasePrinter.d.ts +24 -0
- package/lib/save/base/BasePrinter.d.ts.map +1 -0
- package/lib/save/base/BasePrinter.js +70 -0
- package/lib/save/base/ExportPrinter.d.ts +9 -0
- package/lib/save/base/ExportPrinter.d.ts.map +1 -0
- package/lib/save/base/ExportPrinter.js +67 -0
- package/lib/save/base/ImportPrinter.d.ts +10 -0
- package/lib/save/base/ImportPrinter.d.ts.map +1 -0
- package/lib/save/base/ImportPrinter.js +92 -0
- package/lib/save/{source/SourceUtils.d.ts → base/PrinterUtils.d.ts} +2 -2
- package/lib/save/{source/SourceUtils.d.ts.map → base/PrinterUtils.d.ts.map} +1 -1
- package/lib/save/{source/SourceUtils.js → base/PrinterUtils.js} +10 -10
- package/lib/save/source/SourceBase.d.ts +2 -11
- package/lib/save/source/SourceBase.d.ts.map +1 -1
- package/lib/save/source/SourceBase.js +3 -25
- package/lib/save/source/SourceBody.js +3 -3
- package/lib/save/source/SourceClass.d.ts +2 -1
- package/lib/save/source/SourceClass.d.ts.map +1 -1
- package/lib/save/source/SourceClass.js +18 -17
- package/lib/save/source/SourceFilePrinter.d.ts +1 -1
- package/lib/save/source/SourceFilePrinter.d.ts.map +1 -1
- package/lib/save/source/SourceFilePrinter.js +9 -10
- package/lib/save/source/SourceMethod.d.ts +1 -1
- package/lib/save/source/SourceMethod.d.ts.map +1 -1
- package/lib/save/source/SourceMethod.js +37 -22
- package/lib/save/source/SourceNamespace.d.ts.map +1 -1
- package/lib/save/source/SourceNamespace.js +6 -6
- package/lib/save/source/SourceStmt.d.ts +1 -1
- package/lib/save/source/SourceStmt.d.ts.map +1 -1
- package/lib/save/source/SourceStmt.js +29 -42
- package/lib/save/source/SourceTransformer.d.ts +3 -1
- package/lib/save/source/SourceTransformer.d.ts.map +1 -1
- package/lib/save/source/SourceTransformer.js +65 -29
- package/package.json +1 -1
- package/lib/core/common/BodyBuilder.d.ts.map +0 -1
- package/lib/core/common/CfgBuilder.d.ts.map +0 -1
- package/lib/core/common/CfgBuilder.js +0 -1886
- package/lib/save/source/SourceModule.d.ts +0 -16
- package/lib/save/source/SourceModule.d.ts.map +0 -1
- package/lib/save/source/SourceModule.js +0 -125
|
@@ -1,1886 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2024-2025 Huawei Device Co., Ltd.
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
* you may not use this file except in compliance with the License.
|
|
6
|
-
* You may obtain a copy of the License at
|
|
7
|
-
*
|
|
8
|
-
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
-
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
-
* See the License for the specific language governing permissions and
|
|
14
|
-
* limitations under the License.
|
|
15
|
-
*/
|
|
16
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
17
|
-
if (k2 === undefined) k2 = k;
|
|
18
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
19
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
20
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
21
|
-
}
|
|
22
|
-
Object.defineProperty(o, k2, desc);
|
|
23
|
-
}) : (function(o, m, k, k2) {
|
|
24
|
-
if (k2 === undefined) k2 = k;
|
|
25
|
-
o[k2] = m[k];
|
|
26
|
-
}));
|
|
27
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
28
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
29
|
-
}) : function(o, v) {
|
|
30
|
-
o["default"] = v;
|
|
31
|
-
});
|
|
32
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
33
|
-
if (mod && mod.__esModule) return mod;
|
|
34
|
-
var result = {};
|
|
35
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
36
|
-
__setModuleDefault(result, mod);
|
|
37
|
-
return result;
|
|
38
|
-
};
|
|
39
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.CfgBuilder = void 0;
|
|
41
|
-
const ts = __importStar(require("ohos-typescript"));
|
|
42
|
-
const Local_1 = require("../base/Local");
|
|
43
|
-
const Stmt_1 = require("../base/Stmt");
|
|
44
|
-
const BasicBlock_1 = require("../graph/BasicBlock");
|
|
45
|
-
const Cfg_1 = require("../graph/Cfg");
|
|
46
|
-
const ArkIRTransformer_1 = require("./ArkIRTransformer");
|
|
47
|
-
const ModelUtils_1 = require("./ModelUtils");
|
|
48
|
-
const Expr_1 = require("../base/Expr");
|
|
49
|
-
const Builtin_1 = require("./Builtin");
|
|
50
|
-
const IRUtils_1 = require("./IRUtils");
|
|
51
|
-
const Type_1 = require("../base/Type");
|
|
52
|
-
const Trap_1 = require("../base/Trap");
|
|
53
|
-
const logger_1 = __importStar(require("../../utils/logger"));
|
|
54
|
-
const Ref_1 = require("../base/Ref");
|
|
55
|
-
const Position_1 = require("../base/Position");
|
|
56
|
-
const logger = logger_1.default.getLogger(logger_1.LOG_MODULE_TYPE.ARKANALYZER, 'CfgBuilder');
|
|
57
|
-
class StatementBuilder {
|
|
58
|
-
constructor(type, code, astNode, scopeID) {
|
|
59
|
-
this.addressCode3 = [];
|
|
60
|
-
this.passTmies = 0;
|
|
61
|
-
this.numOfIdentifier = 0;
|
|
62
|
-
this.isDoWhile = false;
|
|
63
|
-
this.type = type;
|
|
64
|
-
this.code = code;
|
|
65
|
-
this.next = null;
|
|
66
|
-
this.lasts = new Set();
|
|
67
|
-
this.walked = false;
|
|
68
|
-
this.index = 0;
|
|
69
|
-
this.line = -1;
|
|
70
|
-
this.column = -1;
|
|
71
|
-
this.astNode = astNode;
|
|
72
|
-
this.scopeID = scopeID;
|
|
73
|
-
this.block = null;
|
|
74
|
-
this.ifExitPass = false;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
class ConditionStatementBuilder extends StatementBuilder {
|
|
78
|
-
constructor(type, code, astNode, scopeID) {
|
|
79
|
-
super(type, code, astNode, scopeID);
|
|
80
|
-
this.doStatement = null;
|
|
81
|
-
this.nextT = null;
|
|
82
|
-
this.nextF = null;
|
|
83
|
-
this.loopBlock = null;
|
|
84
|
-
this.condition = '';
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
class SwitchStatementBuilder extends StatementBuilder {
|
|
88
|
-
constructor(type, code, astNode, scopeID) {
|
|
89
|
-
super(type, code, astNode, scopeID);
|
|
90
|
-
this.cases = [];
|
|
91
|
-
this.default = null;
|
|
92
|
-
this.afterSwitch = null;
|
|
93
|
-
this.nexts = [];
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
class TryStatementBuilder extends StatementBuilder {
|
|
97
|
-
constructor(type, code, astNode, scopeID) {
|
|
98
|
-
super(type, code, astNode, scopeID);
|
|
99
|
-
this.tryFirst = null;
|
|
100
|
-
this.tryExit = null;
|
|
101
|
-
this.catchStatement = null;
|
|
102
|
-
this.catchError = '';
|
|
103
|
-
this.finallyStatement = null;
|
|
104
|
-
this.afterFinal = null;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
class Case {
|
|
108
|
-
constructor(value, stmt) {
|
|
109
|
-
this.value = value;
|
|
110
|
-
this.stmt = stmt;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
class DefUseChain {
|
|
114
|
-
constructor(def, use) {
|
|
115
|
-
this.def = def;
|
|
116
|
-
this.use = use;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
class Variable {
|
|
120
|
-
constructor(name, lastDef) {
|
|
121
|
-
this.properties = [];
|
|
122
|
-
this.propOf = null;
|
|
123
|
-
this.name = name;
|
|
124
|
-
this.lastDef = lastDef;
|
|
125
|
-
this.defUse = [];
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
class Scope {
|
|
129
|
-
constructor(id) {
|
|
130
|
-
this.id = id;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
class Block {
|
|
134
|
-
constructor(id, stmts) {
|
|
135
|
-
this.nexts = [];
|
|
136
|
-
this.lasts = [];
|
|
137
|
-
this.walked = false;
|
|
138
|
-
this.id = id;
|
|
139
|
-
this.stmts = stmts;
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
class Catch {
|
|
143
|
-
constructor(errorName, from, to, withLabel) {
|
|
144
|
-
this.errorName = errorName;
|
|
145
|
-
this.from = from;
|
|
146
|
-
this.to = to;
|
|
147
|
-
this.withLabel = withLabel;
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
class textError extends Error {
|
|
151
|
-
constructor(message) {
|
|
152
|
-
// 调用父类的构造函数,并传入错误消息
|
|
153
|
-
super(message);
|
|
154
|
-
// 设置错误类型的名称
|
|
155
|
-
this.name = 'textError';
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
class CfgBuilder {
|
|
159
|
-
constructor(ast, name, declaringMethod, sourceFile) {
|
|
160
|
-
this.exits = [];
|
|
161
|
-
this.emptyBody = false;
|
|
162
|
-
this.arrowFunctionWithoutBlock = false;
|
|
163
|
-
this.name = name;
|
|
164
|
-
this.astRoot = ast;
|
|
165
|
-
this.declaringMethod = declaringMethod;
|
|
166
|
-
this.declaringClass = declaringMethod.getDeclaringArkClass();
|
|
167
|
-
this.entry = new StatementBuilder('entry', '', ast, 0);
|
|
168
|
-
this.loopStack = [];
|
|
169
|
-
this.switchExitStack = [];
|
|
170
|
-
this.functions = [];
|
|
171
|
-
this.breakin = '';
|
|
172
|
-
this.statementArray = [];
|
|
173
|
-
this.dotEdges = [];
|
|
174
|
-
this.exit = new StatementBuilder('exit', 'return;', null, 0);
|
|
175
|
-
this.scopes = [];
|
|
176
|
-
this.tempVariableNum = 0;
|
|
177
|
-
this.current3ACstm = this.entry;
|
|
178
|
-
this.blocks = [];
|
|
179
|
-
this.currentDeclarationKeyword = '';
|
|
180
|
-
this.variables = [];
|
|
181
|
-
this.importFromPath = [];
|
|
182
|
-
this.catches = [];
|
|
183
|
-
this.sourceFile = sourceFile;
|
|
184
|
-
this.arrowFunctionWithoutBlock = true;
|
|
185
|
-
}
|
|
186
|
-
getDeclaringMethod() {
|
|
187
|
-
return this.declaringMethod;
|
|
188
|
-
}
|
|
189
|
-
judgeLastType(s, lastStatement) {
|
|
190
|
-
if (lastStatement.type === 'ifStatement') {
|
|
191
|
-
let lastIf = lastStatement;
|
|
192
|
-
if (lastIf.nextT == null) {
|
|
193
|
-
lastIf.nextT = s;
|
|
194
|
-
s.lasts.add(lastIf);
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
lastIf.nextF = s;
|
|
198
|
-
s.lasts.add(lastIf);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
else if (lastStatement.type === 'loopStatement') {
|
|
202
|
-
let lastLoop = lastStatement;
|
|
203
|
-
lastLoop.nextT = s;
|
|
204
|
-
s.lasts.add(lastLoop);
|
|
205
|
-
}
|
|
206
|
-
else if (lastStatement.type === 'catchOrNot') {
|
|
207
|
-
let lastLoop = lastStatement;
|
|
208
|
-
lastLoop.nextT = s;
|
|
209
|
-
s.lasts.add(lastLoop);
|
|
210
|
-
}
|
|
211
|
-
else {
|
|
212
|
-
lastStatement.next = s;
|
|
213
|
-
s.lasts.add(lastStatement);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
ASTNodeBreakStatement(c, lastStatement) {
|
|
217
|
-
let p = c;
|
|
218
|
-
while (p && p !== this.astRoot) {
|
|
219
|
-
if (ts.isWhileStatement(p) || ts.isDoStatement(p) || ts.isForStatement(p) || ts.isForInStatement(p) || ts.isForOfStatement(p)) {
|
|
220
|
-
const lastLoopNextF = this.loopStack[this.loopStack.length - 1].nextF;
|
|
221
|
-
this.judgeLastType(lastLoopNextF, lastStatement);
|
|
222
|
-
lastLoopNextF.lasts.add(lastStatement);
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
if (ts.isCaseClause(p) || ts.isDefaultClause(p)) {
|
|
226
|
-
const lastSwitchExit = this.switchExitStack[this.switchExitStack.length - 1];
|
|
227
|
-
this.judgeLastType(lastSwitchExit, lastStatement);
|
|
228
|
-
lastSwitchExit.lasts.add(lastStatement);
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
p = p.parent;
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
ASTNodeIfStatement(c, lastStatement, scopeID) {
|
|
235
|
-
let ifstm = new ConditionStatementBuilder('ifStatement', '', c, scopeID);
|
|
236
|
-
this.judgeLastType(ifstm, lastStatement);
|
|
237
|
-
let ifexit = new StatementBuilder('ifExit', '', c, scopeID);
|
|
238
|
-
this.exits.push(ifexit);
|
|
239
|
-
ifstm.condition = c.expression.getText(this.sourceFile);
|
|
240
|
-
ifstm.code = 'if (' + ifstm.condition + ')';
|
|
241
|
-
if (ts.isBlock(c.thenStatement)) {
|
|
242
|
-
this.walkAST(ifstm, ifexit, [...c.thenStatement.statements]);
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
this.walkAST(ifstm, ifexit, [c.thenStatement]);
|
|
246
|
-
}
|
|
247
|
-
if (c.elseStatement) {
|
|
248
|
-
if (ts.isBlock(c.elseStatement)) {
|
|
249
|
-
this.walkAST(ifstm, ifexit, [...c.elseStatement.statements]);
|
|
250
|
-
}
|
|
251
|
-
else {
|
|
252
|
-
this.walkAST(ifstm, ifexit, [c.elseStatement]);
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
if (!ifstm.nextT) {
|
|
256
|
-
ifstm.nextT = ifexit;
|
|
257
|
-
ifexit.lasts.add(ifstm);
|
|
258
|
-
}
|
|
259
|
-
if (!ifstm.nextF) {
|
|
260
|
-
ifstm.nextF = ifexit;
|
|
261
|
-
ifexit.lasts.add(ifstm);
|
|
262
|
-
}
|
|
263
|
-
return ifexit;
|
|
264
|
-
}
|
|
265
|
-
ASTNodeWhileStatement(c, lastStatement, scopeID) {
|
|
266
|
-
this.breakin = 'loop';
|
|
267
|
-
let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
|
|
268
|
-
this.loopStack.push(loopstm);
|
|
269
|
-
this.judgeLastType(loopstm, lastStatement);
|
|
270
|
-
let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
|
|
271
|
-
this.exits.push(loopExit);
|
|
272
|
-
loopstm.nextF = loopExit;
|
|
273
|
-
loopExit.lasts.add(loopstm);
|
|
274
|
-
loopstm.condition = c.expression.getText(this.sourceFile);
|
|
275
|
-
loopstm.code = 'while (' + loopstm.condition + ')';
|
|
276
|
-
if (ts.isBlock(c.statement)) {
|
|
277
|
-
this.walkAST(loopstm, loopstm, [...c.statement.statements]);
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
this.walkAST(loopstm, loopstm, [c.statement]);
|
|
281
|
-
}
|
|
282
|
-
if (!loopstm.nextF) {
|
|
283
|
-
loopstm.nextF = loopExit;
|
|
284
|
-
loopExit.lasts.add(loopstm);
|
|
285
|
-
}
|
|
286
|
-
if (!loopstm.nextT) {
|
|
287
|
-
loopstm.nextT = loopExit;
|
|
288
|
-
loopExit.lasts.add(loopstm);
|
|
289
|
-
}
|
|
290
|
-
this.loopStack.pop();
|
|
291
|
-
return loopExit;
|
|
292
|
-
}
|
|
293
|
-
ASTNodeForStatement(c, lastStatement, scopeID) {
|
|
294
|
-
var _a, _b, _c, _d, _e;
|
|
295
|
-
this.breakin = 'loop';
|
|
296
|
-
let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
|
|
297
|
-
this.loopStack.push(loopstm);
|
|
298
|
-
this.judgeLastType(loopstm, lastStatement);
|
|
299
|
-
let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
|
|
300
|
-
this.exits.push(loopExit);
|
|
301
|
-
loopstm.nextF = loopExit;
|
|
302
|
-
loopExit.lasts.add(loopstm);
|
|
303
|
-
loopstm.code = 'for (';
|
|
304
|
-
if (ts.isForStatement(c)) {
|
|
305
|
-
loopstm.code += ((_a = c.initializer) === null || _a === void 0 ? void 0 : _a.getText(this.sourceFile)) + '; ' + ((_b = c.condition) === null || _b === void 0 ? void 0 : _b.getText(this.sourceFile)) + '; ' + ((_c = c.incrementor) === null || _c === void 0 ? void 0 : _c.getText(this.sourceFile));
|
|
306
|
-
}
|
|
307
|
-
else if (ts.isForOfStatement(c)) {
|
|
308
|
-
loopstm.code += ((_d = c.initializer) === null || _d === void 0 ? void 0 : _d.getText(this.sourceFile)) + ' of ' + c.expression.getText(this.sourceFile);
|
|
309
|
-
}
|
|
310
|
-
else {
|
|
311
|
-
loopstm.code += ((_e = c.initializer) === null || _e === void 0 ? void 0 : _e.getText(this.sourceFile)) + ' in ' + c.expression.getText(this.sourceFile);
|
|
312
|
-
}
|
|
313
|
-
loopstm.code += ')';
|
|
314
|
-
if (ts.isBlock(c.statement)) {
|
|
315
|
-
this.walkAST(loopstm, loopstm, [...c.statement.statements]);
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
this.walkAST(loopstm, loopstm, [c.statement]);
|
|
319
|
-
}
|
|
320
|
-
if (!loopstm.nextF) {
|
|
321
|
-
loopstm.nextF = loopExit;
|
|
322
|
-
loopExit.lasts.add(loopstm);
|
|
323
|
-
}
|
|
324
|
-
if (!loopstm.nextT) {
|
|
325
|
-
loopstm.nextT = loopExit;
|
|
326
|
-
loopExit.lasts.add(loopstm);
|
|
327
|
-
}
|
|
328
|
-
this.loopStack.pop();
|
|
329
|
-
return loopExit;
|
|
330
|
-
}
|
|
331
|
-
ASTNodeDoStatement(c, lastStatement, scopeID) {
|
|
332
|
-
var _a, _b;
|
|
333
|
-
this.breakin = 'loop';
|
|
334
|
-
let loopstm = new ConditionStatementBuilder('loopStatement', '', c, scopeID);
|
|
335
|
-
this.loopStack.push(loopstm);
|
|
336
|
-
let loopExit = new StatementBuilder('loopExit', '', c, scopeID);
|
|
337
|
-
this.exits.push(loopExit);
|
|
338
|
-
loopstm.nextF = loopExit;
|
|
339
|
-
loopExit.lasts.add(loopstm);
|
|
340
|
-
loopstm.condition = c.expression.getText(this.sourceFile);
|
|
341
|
-
loopstm.code = 'while (' + loopstm.condition + ')';
|
|
342
|
-
loopstm.isDoWhile = true;
|
|
343
|
-
if (ts.isBlock(c.statement)) {
|
|
344
|
-
this.walkAST(lastStatement, loopstm, [...c.statement.statements]);
|
|
345
|
-
}
|
|
346
|
-
else {
|
|
347
|
-
this.walkAST(lastStatement, loopstm, [c.statement]);
|
|
348
|
-
}
|
|
349
|
-
let lastType = lastStatement.type;
|
|
350
|
-
if (lastType === 'ifStatement' || lastType === 'loopStatement') {
|
|
351
|
-
let lastCondition = lastStatement;
|
|
352
|
-
loopstm.nextT = lastCondition.nextT;
|
|
353
|
-
(_a = lastCondition.nextT) === null || _a === void 0 ? void 0 : _a.lasts.add(loopstm);
|
|
354
|
-
}
|
|
355
|
-
else {
|
|
356
|
-
loopstm.nextT = lastStatement.next;
|
|
357
|
-
(_b = lastStatement.next) === null || _b === void 0 ? void 0 : _b.lasts.add(loopstm);
|
|
358
|
-
}
|
|
359
|
-
if (loopstm.nextT && loopstm.nextT !== loopstm) {
|
|
360
|
-
loopstm.nextT.isDoWhile = true;
|
|
361
|
-
loopstm.doStatement = loopstm.nextT;
|
|
362
|
-
}
|
|
363
|
-
this.loopStack.pop();
|
|
364
|
-
return loopExit;
|
|
365
|
-
}
|
|
366
|
-
ASTNodeSwitchStatement(c, lastStatement, scopeID) {
|
|
367
|
-
var _a;
|
|
368
|
-
this.breakin = 'switch';
|
|
369
|
-
let switchstm = new SwitchStatementBuilder('switchStatement', '', c, scopeID);
|
|
370
|
-
this.judgeLastType(switchstm, lastStatement);
|
|
371
|
-
let switchExit = new StatementBuilder('switchExit', '', null, scopeID);
|
|
372
|
-
this.exits.push(switchExit);
|
|
373
|
-
this.switchExitStack.push(switchExit);
|
|
374
|
-
switchExit.lasts.add(switchstm);
|
|
375
|
-
switchstm.code = 'switch (' + c.expression + ')';
|
|
376
|
-
let lastCaseExit = null;
|
|
377
|
-
for (let i = 0; i < c.caseBlock.clauses.length; i++) {
|
|
378
|
-
const clause = c.caseBlock.clauses[i];
|
|
379
|
-
let casestm;
|
|
380
|
-
if (ts.isCaseClause(clause)) {
|
|
381
|
-
casestm = new StatementBuilder('statement', 'case ' + clause.expression.getText(this.sourceFile) + ':', clause, scopeID);
|
|
382
|
-
}
|
|
383
|
-
else {
|
|
384
|
-
casestm = new StatementBuilder('statement', 'default:', clause, scopeID);
|
|
385
|
-
}
|
|
386
|
-
switchstm.nexts.push(casestm);
|
|
387
|
-
casestm.lasts.add(switchstm);
|
|
388
|
-
let caseExit = new StatementBuilder('caseExit', '', null, scopeID);
|
|
389
|
-
this.exits.push(caseExit);
|
|
390
|
-
this.walkAST(casestm, caseExit, [...clause.statements]);
|
|
391
|
-
if (ts.isCaseClause(clause)) {
|
|
392
|
-
const cas = new Case(casestm.code, casestm.next);
|
|
393
|
-
switchstm.cases.push(cas);
|
|
394
|
-
}
|
|
395
|
-
else {
|
|
396
|
-
switchstm.default = casestm.next;
|
|
397
|
-
}
|
|
398
|
-
switchstm.nexts[switchstm.nexts.length - 1] = casestm.next;
|
|
399
|
-
for (const stmt of [...casestm.lasts]) {
|
|
400
|
-
casestm.next.lasts.add(stmt);
|
|
401
|
-
}
|
|
402
|
-
casestm.next.lasts.delete(casestm);
|
|
403
|
-
if (lastCaseExit) {
|
|
404
|
-
lastCaseExit.next = casestm.next;
|
|
405
|
-
(_a = casestm.next) === null || _a === void 0 ? void 0 : _a.lasts.add(lastCaseExit);
|
|
406
|
-
}
|
|
407
|
-
lastCaseExit = caseExit;
|
|
408
|
-
if (i === c.caseBlock.clauses.length - 1) {
|
|
409
|
-
caseExit.next = switchExit;
|
|
410
|
-
switchExit.lasts.add(caseExit);
|
|
411
|
-
}
|
|
412
|
-
}
|
|
413
|
-
this.switchExitStack.pop();
|
|
414
|
-
return switchExit;
|
|
415
|
-
}
|
|
416
|
-
ASTNodeTryStatement(c, lastStatement, scopeID) {
|
|
417
|
-
var _a, _b;
|
|
418
|
-
let trystm = new TryStatementBuilder('tryStatement', 'try', c, scopeID);
|
|
419
|
-
this.judgeLastType(trystm, lastStatement);
|
|
420
|
-
let tryExit = new StatementBuilder('tryExit', '', c, scopeID);
|
|
421
|
-
this.exits.push(tryExit);
|
|
422
|
-
trystm.tryExit = tryExit;
|
|
423
|
-
this.walkAST(trystm, tryExit, [...c.tryBlock.statements]);
|
|
424
|
-
trystm.tryFirst = trystm.next;
|
|
425
|
-
(_a = trystm.next) === null || _a === void 0 ? void 0 : _a.lasts.add(trystm);
|
|
426
|
-
if (c.catchClause) {
|
|
427
|
-
let text = 'catch';
|
|
428
|
-
if (c.catchClause.variableDeclaration) {
|
|
429
|
-
text += '(' + c.catchClause.variableDeclaration.getText(this.sourceFile) + ')';
|
|
430
|
-
}
|
|
431
|
-
let catchOrNot = new ConditionStatementBuilder('catchOrNot', text, c, scopeID);
|
|
432
|
-
let catchExit = new StatementBuilder('catch exit', '', c, scopeID);
|
|
433
|
-
catchOrNot.nextF = catchExit;
|
|
434
|
-
catchExit.lasts.add(catchOrNot);
|
|
435
|
-
this.walkAST(catchOrNot, catchExit, [...c.catchClause.block.statements]);
|
|
436
|
-
if (!catchOrNot.nextT) {
|
|
437
|
-
catchOrNot.nextT = catchExit;
|
|
438
|
-
catchExit.lasts.add(catchOrNot);
|
|
439
|
-
}
|
|
440
|
-
const catchStatement = new StatementBuilder('statement', catchOrNot.code, c.catchClause, catchOrNot.nextT.scopeID);
|
|
441
|
-
catchStatement.next = catchOrNot.nextT;
|
|
442
|
-
trystm.catchStatement = catchStatement;
|
|
443
|
-
catchStatement.lasts.add(trystm);
|
|
444
|
-
if (c.catchClause.variableDeclaration) {
|
|
445
|
-
trystm.catchError = c.catchClause.variableDeclaration.getText(this.sourceFile);
|
|
446
|
-
}
|
|
447
|
-
else {
|
|
448
|
-
trystm.catchError = 'Error';
|
|
449
|
-
}
|
|
450
|
-
}
|
|
451
|
-
let final = new StatementBuilder('statement', 'finally', c, scopeID);
|
|
452
|
-
let finalExit = new StatementBuilder('finallyExit', '', c, scopeID);
|
|
453
|
-
this.exits.push(finalExit);
|
|
454
|
-
if (c.finallyBlock && c.finallyBlock.statements.length > 0) {
|
|
455
|
-
this.walkAST(final, finalExit, [...c.finallyBlock.statements]);
|
|
456
|
-
}
|
|
457
|
-
else {
|
|
458
|
-
let dummyFinally = new StatementBuilder('statement', 'dummyFinally', c, (new Scope(this.scopes.length)).id);
|
|
459
|
-
final.next = dummyFinally;
|
|
460
|
-
dummyFinally.lasts.add(final);
|
|
461
|
-
dummyFinally.next = finalExit;
|
|
462
|
-
finalExit.lasts.add(dummyFinally);
|
|
463
|
-
}
|
|
464
|
-
trystm.finallyStatement = final.next;
|
|
465
|
-
tryExit.next = final.next;
|
|
466
|
-
(_b = final.next) === null || _b === void 0 ? void 0 : _b.lasts.add(tryExit);
|
|
467
|
-
trystm.next = finalExit;
|
|
468
|
-
finalExit.lasts.add(trystm);
|
|
469
|
-
return finalExit;
|
|
470
|
-
}
|
|
471
|
-
walkAST(lastStatement, nextStatement, nodes) {
|
|
472
|
-
let scope = new Scope(this.scopes.length);
|
|
473
|
-
this.scopes.push(scope);
|
|
474
|
-
for (let i = 0; i < nodes.length; i++) {
|
|
475
|
-
let c = nodes[i];
|
|
476
|
-
if (ts.isVariableStatement(c) || ts.isExpressionStatement(c) || ts.isThrowStatement(c) || ts.isTypeAliasDeclaration(c)) {
|
|
477
|
-
let s = new StatementBuilder('statement', c.getText(this.sourceFile), c, scope.id);
|
|
478
|
-
this.judgeLastType(s, lastStatement);
|
|
479
|
-
lastStatement = s;
|
|
480
|
-
}
|
|
481
|
-
else if (!this.declaringMethod.isDefaultArkMethod() && ts.isFunctionDeclaration(c)) {
|
|
482
|
-
let s = new StatementBuilder('functionDeclarationStatement', c.getText(this.sourceFile), c, scope.id);
|
|
483
|
-
this.judgeLastType(s, lastStatement);
|
|
484
|
-
lastStatement = s;
|
|
485
|
-
}
|
|
486
|
-
else if (!this.declaringMethod.isDefaultArkMethod() && ts.isClassDeclaration(c)) {
|
|
487
|
-
let s = new StatementBuilder('classDeclarationStatement', c.getText(this.sourceFile), c, scope.id);
|
|
488
|
-
this.judgeLastType(s, lastStatement);
|
|
489
|
-
lastStatement = s;
|
|
490
|
-
}
|
|
491
|
-
else if (ts.isReturnStatement(c)) {
|
|
492
|
-
let s = new StatementBuilder('returnStatement', c.getText(this.sourceFile), c, scope.id);
|
|
493
|
-
this.judgeLastType(s, lastStatement);
|
|
494
|
-
lastStatement = s;
|
|
495
|
-
break;
|
|
496
|
-
}
|
|
497
|
-
else if (ts.isBreakStatement(c)) {
|
|
498
|
-
this.ASTNodeBreakStatement(c, lastStatement);
|
|
499
|
-
return;
|
|
500
|
-
}
|
|
501
|
-
else if (ts.isContinueStatement(c)) {
|
|
502
|
-
const lastLoop = this.loopStack[this.loopStack.length - 1];
|
|
503
|
-
this.judgeLastType(lastLoop, lastStatement);
|
|
504
|
-
lastLoop.lasts.add(lastStatement);
|
|
505
|
-
return;
|
|
506
|
-
}
|
|
507
|
-
else if (ts.isIfStatement(c)) {
|
|
508
|
-
lastStatement = this.ASTNodeIfStatement(c, lastStatement, scope.id);
|
|
509
|
-
}
|
|
510
|
-
else if (ts.isWhileStatement(c)) {
|
|
511
|
-
lastStatement = this.ASTNodeWhileStatement(c, lastStatement, scope.id);
|
|
512
|
-
}
|
|
513
|
-
if (ts.isForStatement(c) || ts.isForInStatement(c) || ts.isForOfStatement(c)) {
|
|
514
|
-
lastStatement = this.ASTNodeForStatement(c, lastStatement, scope.id);
|
|
515
|
-
}
|
|
516
|
-
else if (ts.isDoStatement(c)) {
|
|
517
|
-
lastStatement = this.ASTNodeDoStatement(c, lastStatement, scope.id);
|
|
518
|
-
}
|
|
519
|
-
else if (ts.isSwitchStatement(c)) {
|
|
520
|
-
lastStatement = this.ASTNodeSwitchStatement(c, lastStatement, scope.id);
|
|
521
|
-
}
|
|
522
|
-
else if (ts.isBlock(c)) {
|
|
523
|
-
let blockExit = new StatementBuilder('blockExit', '', c, scope.id);
|
|
524
|
-
this.exits.push(blockExit);
|
|
525
|
-
this.walkAST(lastStatement, blockExit, c.getChildren(this.sourceFile)[1].getChildren(this.sourceFile));
|
|
526
|
-
lastStatement = blockExit;
|
|
527
|
-
}
|
|
528
|
-
else if (ts.isTryStatement(c)) {
|
|
529
|
-
lastStatement = this.ASTNodeTryStatement(c, lastStatement, scope.id);
|
|
530
|
-
}
|
|
531
|
-
else if (ts.isExportAssignment(c)) {
|
|
532
|
-
if (ts.isNewExpression(c.expression) || ts.isObjectLiteralExpression(c.expression)) {
|
|
533
|
-
let s = new StatementBuilder('statement', c.getText(this.sourceFile), c, scope.id);
|
|
534
|
-
this.judgeLastType(s, lastStatement);
|
|
535
|
-
lastStatement = s;
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
if (lastStatement.type !== 'breakStatement' && lastStatement.type !== 'continueStatement' && lastStatement.type !== 'returnStatement') {
|
|
540
|
-
lastStatement.next = nextStatement;
|
|
541
|
-
nextStatement.lasts.add(lastStatement);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
addReturnInEmptyMethod() {
|
|
545
|
-
if (this.entry.next === this.exit) {
|
|
546
|
-
const ret = new StatementBuilder('returnStatement', 'return;', null, this.entry.scopeID);
|
|
547
|
-
this.entry.next = ret;
|
|
548
|
-
ret.lasts.add(this.entry);
|
|
549
|
-
ret.next = this.exit;
|
|
550
|
-
this.exit.lasts = new Set([ret]);
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
deleteExitAfterCondition(last, exit) {
|
|
554
|
-
if (last.nextT === exit) {
|
|
555
|
-
last.nextT = exit.next;
|
|
556
|
-
const lasts = exit.next.lasts;
|
|
557
|
-
lasts.delete(exit);
|
|
558
|
-
lasts.add(last);
|
|
559
|
-
}
|
|
560
|
-
else if (last.nextF === exit) {
|
|
561
|
-
last.nextF = exit.next;
|
|
562
|
-
const lasts = exit.next.lasts;
|
|
563
|
-
lasts.delete(exit);
|
|
564
|
-
lasts.add(last);
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
deleteExitAfterSwitch(last, exit) {
|
|
568
|
-
var _a;
|
|
569
|
-
if (exit.type === 'switchExit') {
|
|
570
|
-
last.afterSwitch = exit.next;
|
|
571
|
-
}
|
|
572
|
-
exit.next.lasts.delete(exit);
|
|
573
|
-
last.nexts = last.nexts.filter(item => item !== exit);
|
|
574
|
-
if (last.nexts.length === 0) {
|
|
575
|
-
last.next = exit.next;
|
|
576
|
-
(_a = exit.next) === null || _a === void 0 ? void 0 : _a.lasts.add(last);
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
deleteExit() {
|
|
580
|
-
for (const exit of this.exits) {
|
|
581
|
-
const lasts = [...exit.lasts];
|
|
582
|
-
for (const last of lasts) {
|
|
583
|
-
if (last instanceof ConditionStatementBuilder) {
|
|
584
|
-
this.deleteExitAfterCondition(last, exit);
|
|
585
|
-
}
|
|
586
|
-
else if (last instanceof SwitchStatementBuilder) {
|
|
587
|
-
this.deleteExitAfterSwitch(last, exit);
|
|
588
|
-
}
|
|
589
|
-
else if (last instanceof TryStatementBuilder && exit.type === 'finallyExit') {
|
|
590
|
-
last.afterFinal = exit.next;
|
|
591
|
-
last.next = last.tryFirst;
|
|
592
|
-
exit.lasts.delete(last);
|
|
593
|
-
}
|
|
594
|
-
else {
|
|
595
|
-
last.next = exit.next;
|
|
596
|
-
const lasts = exit.next.lasts;
|
|
597
|
-
lasts.delete(exit);
|
|
598
|
-
lasts.add(last);
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
// 部分语句例如return后面的exit语句的next无法在上面清除
|
|
603
|
-
for (const exit of this.exits) {
|
|
604
|
-
if (exit.next && exit.next.lasts.has(exit)) {
|
|
605
|
-
exit.next.lasts.delete(exit);
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
addStmt2BlockStmtQueueInSpecialCase(stmt, stmtQueue) {
|
|
610
|
-
if (stmt.next) {
|
|
611
|
-
if ((stmt.type === 'continueStatement' || stmt.next.type === 'loopStatement') && stmt.next.block || stmt.next.type.includes('exit')) {
|
|
612
|
-
return null;
|
|
613
|
-
}
|
|
614
|
-
stmt.next.passTmies++;
|
|
615
|
-
if (stmt.next.passTmies === stmt.next.lasts.size || stmt.next.type === 'loopStatement' || stmt.next.isDoWhile) {
|
|
616
|
-
if (stmt.next.scopeID !== stmt.scopeID && !(stmt.next instanceof ConditionStatementBuilder && stmt.next.doStatement) &&
|
|
617
|
-
!(ts.isCaseClause(stmt.astNode) || ts.isDefaultClause(stmt.astNode))) {
|
|
618
|
-
stmtQueue.push(stmt.next);
|
|
619
|
-
return null;
|
|
620
|
-
}
|
|
621
|
-
return stmt.next;
|
|
622
|
-
}
|
|
623
|
-
}
|
|
624
|
-
return null;
|
|
625
|
-
}
|
|
626
|
-
addStmt2BlockStmtQueue(stmt, stmtQueue) {
|
|
627
|
-
if (stmt instanceof ConditionStatementBuilder) {
|
|
628
|
-
stmtQueue.push(stmt.nextF);
|
|
629
|
-
stmtQueue.push(stmt.nextT);
|
|
630
|
-
}
|
|
631
|
-
else if (stmt instanceof SwitchStatementBuilder) {
|
|
632
|
-
if (stmt.nexts.length === 0) {
|
|
633
|
-
stmtQueue.push(stmt.afterSwitch);
|
|
634
|
-
}
|
|
635
|
-
for (let i = stmt.nexts.length - 1; i >= 0; i--) {
|
|
636
|
-
stmtQueue.push(stmt.nexts[i]);
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
else if (stmt instanceof TryStatementBuilder) {
|
|
640
|
-
if (stmt.finallyStatement) {
|
|
641
|
-
stmtQueue.push(stmt.finallyStatement);
|
|
642
|
-
}
|
|
643
|
-
if (stmt.catchStatement) {
|
|
644
|
-
stmtQueue.push(stmt.catchStatement);
|
|
645
|
-
}
|
|
646
|
-
if (stmt.tryFirst) {
|
|
647
|
-
stmtQueue.push(stmt.tryFirst);
|
|
648
|
-
}
|
|
649
|
-
}
|
|
650
|
-
else if (stmt.next) {
|
|
651
|
-
return this.addStmt2BlockStmtQueueInSpecialCase(stmt, stmtQueue);
|
|
652
|
-
}
|
|
653
|
-
return null;
|
|
654
|
-
}
|
|
655
|
-
buildBlocks() {
|
|
656
|
-
const stmtQueue = [this.entry];
|
|
657
|
-
const handledStmts = new Set();
|
|
658
|
-
while (stmtQueue.length > 0) {
|
|
659
|
-
let stmt = stmtQueue.pop();
|
|
660
|
-
if (stmt.type.includes('exit')) {
|
|
661
|
-
continue;
|
|
662
|
-
}
|
|
663
|
-
if (handledStmts.has(stmt)) {
|
|
664
|
-
continue;
|
|
665
|
-
}
|
|
666
|
-
const block = new Block(this.blocks.length, []);
|
|
667
|
-
this.blocks.push(block);
|
|
668
|
-
while (stmt && !handledStmts.has(stmt)) {
|
|
669
|
-
if (stmt.type === 'loopStatement' && block.stmts.length > 0 && !stmt.isDoWhile) {
|
|
670
|
-
stmtQueue.push(stmt);
|
|
671
|
-
break;
|
|
672
|
-
}
|
|
673
|
-
if (stmt.type.includes('Exit')) {
|
|
674
|
-
break;
|
|
675
|
-
}
|
|
676
|
-
block.stmts.push(stmt);
|
|
677
|
-
stmt.block = block;
|
|
678
|
-
handledStmts.add(stmt);
|
|
679
|
-
const addRet = this.addStmt2BlockStmtQueue(stmt, stmtQueue);
|
|
680
|
-
if (addRet instanceof StatementBuilder) {
|
|
681
|
-
stmt = addRet;
|
|
682
|
-
}
|
|
683
|
-
else {
|
|
684
|
-
break;
|
|
685
|
-
}
|
|
686
|
-
}
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
buildConditionNextBlocks(originStatement, block, isLastStatement) {
|
|
690
|
-
var _a, _b, _c, _d;
|
|
691
|
-
let nextT = (_a = originStatement.nextT) === null || _a === void 0 ? void 0 : _a.block;
|
|
692
|
-
if (nextT && (isLastStatement || nextT !== block) && !((_b = originStatement.nextT) === null || _b === void 0 ? void 0 : _b.type.includes(' exit'))) {
|
|
693
|
-
block.nexts.push(nextT);
|
|
694
|
-
nextT.lasts.push(block);
|
|
695
|
-
}
|
|
696
|
-
let nextF = (_c = originStatement.nextF) === null || _c === void 0 ? void 0 : _c.block;
|
|
697
|
-
if (nextF && (isLastStatement || nextF !== block) && !((_d = originStatement.nextF) === null || _d === void 0 ? void 0 : _d.type.includes(' exit'))) {
|
|
698
|
-
block.nexts.push(nextF);
|
|
699
|
-
nextF.lasts.push(block);
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
buildSwitchNextBlocks(originStatement, block, isLastStatement) {
|
|
703
|
-
if (originStatement.nexts.length === 0) {
|
|
704
|
-
const nextBlock = originStatement.afterSwitch.block;
|
|
705
|
-
if (nextBlock && (isLastStatement || nextBlock !== block)) {
|
|
706
|
-
block.nexts.push(nextBlock);
|
|
707
|
-
nextBlock.lasts.push(block);
|
|
708
|
-
}
|
|
709
|
-
}
|
|
710
|
-
for (const next of originStatement.nexts) {
|
|
711
|
-
const nextBlock = next.block;
|
|
712
|
-
if (nextBlock && (isLastStatement || nextBlock !== block)) {
|
|
713
|
-
block.nexts.push(nextBlock);
|
|
714
|
-
nextBlock.lasts.push(block);
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
buildNormalNextBlocks(originStatement, block, isLastStatement) {
|
|
719
|
-
var _a, _b;
|
|
720
|
-
let next = (_a = originStatement.next) === null || _a === void 0 ? void 0 : _a.block;
|
|
721
|
-
if (next && (isLastStatement || next !== block) && !((_b = originStatement.next) === null || _b === void 0 ? void 0 : _b.type.includes(' exit'))) {
|
|
722
|
-
block.nexts.push(next);
|
|
723
|
-
next.lasts.push(block);
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
buildBlocksNextLast() {
|
|
727
|
-
for (let block of this.blocks) {
|
|
728
|
-
for (let originStatement of block.stmts) {
|
|
729
|
-
let isLastStatement = (block.stmts.indexOf(originStatement) === block.stmts.length - 1);
|
|
730
|
-
if (originStatement instanceof ConditionStatementBuilder) {
|
|
731
|
-
this.buildConditionNextBlocks(originStatement, block, isLastStatement);
|
|
732
|
-
}
|
|
733
|
-
else if (originStatement instanceof SwitchStatementBuilder) {
|
|
734
|
-
this.buildSwitchNextBlocks(originStatement, block, isLastStatement);
|
|
735
|
-
}
|
|
736
|
-
else {
|
|
737
|
-
this.buildNormalNextBlocks(originStatement, block, isLastStatement);
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
addReturnBlock(returnStatement, notReturnStmts) {
|
|
743
|
-
var _a, _b, _c;
|
|
744
|
-
let returnBlock = new Block(this.blocks.length, [returnStatement]);
|
|
745
|
-
returnStatement.block = returnBlock;
|
|
746
|
-
this.blocks.push(returnBlock);
|
|
747
|
-
for (const notReturnStmt of notReturnStmts) {
|
|
748
|
-
if (notReturnStmt instanceof ConditionStatementBuilder) {
|
|
749
|
-
if (this.exit === notReturnStmt.nextT) {
|
|
750
|
-
notReturnStmt.nextT = returnStatement;
|
|
751
|
-
(_a = notReturnStmt.block) === null || _a === void 0 ? void 0 : _a.nexts.splice(0, 0, returnBlock);
|
|
752
|
-
}
|
|
753
|
-
else if (this.exit === notReturnStmt.nextF) {
|
|
754
|
-
notReturnStmt.nextF = returnStatement;
|
|
755
|
-
(_b = notReturnStmt.block) === null || _b === void 0 ? void 0 : _b.nexts.push(returnBlock);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
else {
|
|
759
|
-
notReturnStmt.next = returnStatement;
|
|
760
|
-
(_c = notReturnStmt.block) === null || _c === void 0 ? void 0 : _c.nexts.push(returnBlock);
|
|
761
|
-
}
|
|
762
|
-
returnStatement.lasts.add(notReturnStmt);
|
|
763
|
-
returnStatement.next = this.exit;
|
|
764
|
-
const lasts = [...this.exit.lasts];
|
|
765
|
-
lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
|
|
766
|
-
this.exit.lasts = new Set(lasts);
|
|
767
|
-
returnBlock.lasts.push(notReturnStmt.block);
|
|
768
|
-
}
|
|
769
|
-
this.exit.block = returnBlock;
|
|
770
|
-
}
|
|
771
|
-
addReturnStmt() {
|
|
772
|
-
var _a;
|
|
773
|
-
let notReturnStmts = [];
|
|
774
|
-
for (let stmt of [...this.exit.lasts]) {
|
|
775
|
-
if (stmt.type !== 'returnStatement') {
|
|
776
|
-
notReturnStmts.push(stmt);
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
if (notReturnStmts.length < 1) {
|
|
780
|
-
return;
|
|
781
|
-
}
|
|
782
|
-
const returnStatement = new StatementBuilder('returnStatement', 'return;', null, this.exit.scopeID);
|
|
783
|
-
let TryOrSwitchExit = false;
|
|
784
|
-
if (notReturnStmts.length === 1 && notReturnStmts[0].block) {
|
|
785
|
-
let p = notReturnStmts[0].astNode;
|
|
786
|
-
while (p && p !== this.astRoot) {
|
|
787
|
-
if (ts.isTryStatement(p) || ts.isSwitchStatement(p)) {
|
|
788
|
-
TryOrSwitchExit = true;
|
|
789
|
-
break;
|
|
790
|
-
}
|
|
791
|
-
p = p.parent;
|
|
792
|
-
}
|
|
793
|
-
}
|
|
794
|
-
if (notReturnStmts.length === 1 && !(notReturnStmts[0] instanceof ConditionStatementBuilder) && !TryOrSwitchExit) {
|
|
795
|
-
const notReturnStmt = notReturnStmts[0];
|
|
796
|
-
notReturnStmt.next = returnStatement;
|
|
797
|
-
returnStatement.lasts = new Set([notReturnStmt]);
|
|
798
|
-
returnStatement.next = this.exit;
|
|
799
|
-
const lasts = [...this.exit.lasts];
|
|
800
|
-
lasts[lasts.indexOf(notReturnStmt)] = returnStatement;
|
|
801
|
-
this.exit.lasts = new Set(lasts);
|
|
802
|
-
(_a = notReturnStmt.block) === null || _a === void 0 ? void 0 : _a.stmts.push(returnStatement);
|
|
803
|
-
returnStatement.block = notReturnStmt.block;
|
|
804
|
-
}
|
|
805
|
-
else {
|
|
806
|
-
this.addReturnBlock(returnStatement, notReturnStmts);
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
resetWalked() {
|
|
810
|
-
for (let stmt of this.statementArray) {
|
|
811
|
-
stmt.walked = false;
|
|
812
|
-
}
|
|
813
|
-
}
|
|
814
|
-
addStmtBuilderPosition() {
|
|
815
|
-
for (const stmt of this.statementArray) {
|
|
816
|
-
if (stmt.astNode) {
|
|
817
|
-
const { line, character } = ts.getLineAndCharacterOfPosition(this.sourceFile, stmt.astNode.getStart(this.sourceFile));
|
|
818
|
-
stmt.line = line + 1;
|
|
819
|
-
stmt.column = character + 1;
|
|
820
|
-
}
|
|
821
|
-
}
|
|
822
|
-
}
|
|
823
|
-
CfgBuilder2Array(stmt) {
|
|
824
|
-
if (stmt.walked)
|
|
825
|
-
return;
|
|
826
|
-
stmt.walked = true;
|
|
827
|
-
stmt.index = this.statementArray.length;
|
|
828
|
-
if (!stmt.type.includes(' exit'))
|
|
829
|
-
this.statementArray.push(stmt);
|
|
830
|
-
if (stmt.type === 'ifStatement' || stmt.type === 'loopStatement' || stmt.type === 'catchOrNot') {
|
|
831
|
-
let cstm = stmt;
|
|
832
|
-
if (cstm.nextT == null || cstm.nextF == null) {
|
|
833
|
-
this.errorTest(cstm);
|
|
834
|
-
return;
|
|
835
|
-
}
|
|
836
|
-
this.CfgBuilder2Array(cstm.nextF);
|
|
837
|
-
this.CfgBuilder2Array(cstm.nextT);
|
|
838
|
-
}
|
|
839
|
-
else if (stmt.type === 'switchStatement') {
|
|
840
|
-
let sstm = stmt;
|
|
841
|
-
for (let ss of sstm.nexts) {
|
|
842
|
-
this.CfgBuilder2Array(ss);
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
else if (stmt.type === 'tryStatement') {
|
|
846
|
-
let trystm = stmt;
|
|
847
|
-
if (trystm.tryFirst) {
|
|
848
|
-
this.CfgBuilder2Array(trystm.tryFirst);
|
|
849
|
-
}
|
|
850
|
-
if (trystm.catchStatement) {
|
|
851
|
-
this.CfgBuilder2Array(trystm.catchStatement);
|
|
852
|
-
}
|
|
853
|
-
if (trystm.finallyStatement) {
|
|
854
|
-
this.CfgBuilder2Array(trystm.finallyStatement);
|
|
855
|
-
}
|
|
856
|
-
if (trystm.next) {
|
|
857
|
-
this.CfgBuilder2Array(trystm.next);
|
|
858
|
-
}
|
|
859
|
-
}
|
|
860
|
-
else {
|
|
861
|
-
if (stmt.next != null)
|
|
862
|
-
this.CfgBuilder2Array(stmt.next);
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
getDotEdges(stmt) {
|
|
866
|
-
if (this.statementArray.length === 0)
|
|
867
|
-
this.CfgBuilder2Array(this.entry);
|
|
868
|
-
if (stmt.walked)
|
|
869
|
-
return;
|
|
870
|
-
stmt.walked = true;
|
|
871
|
-
if (stmt.type === 'ifStatement' || stmt.type === 'loopStatement' || stmt.type === 'catchOrNot') {
|
|
872
|
-
let cstm = stmt;
|
|
873
|
-
if (cstm.nextT == null || cstm.nextF == null) {
|
|
874
|
-
this.errorTest(cstm);
|
|
875
|
-
return;
|
|
876
|
-
}
|
|
877
|
-
let edge = [cstm.index, cstm.nextF.index];
|
|
878
|
-
this.dotEdges.push(edge);
|
|
879
|
-
edge = [cstm.index, cstm.nextT.index];
|
|
880
|
-
this.dotEdges.push(edge);
|
|
881
|
-
this.getDotEdges(cstm.nextF);
|
|
882
|
-
this.getDotEdges(cstm.nextT);
|
|
883
|
-
}
|
|
884
|
-
else if (stmt.type === 'switchStatement') {
|
|
885
|
-
let sstm = stmt;
|
|
886
|
-
for (let ss of sstm.nexts) {
|
|
887
|
-
let edge = [sstm.index, ss.index];
|
|
888
|
-
this.dotEdges.push(edge);
|
|
889
|
-
this.getDotEdges(ss);
|
|
890
|
-
}
|
|
891
|
-
}
|
|
892
|
-
else {
|
|
893
|
-
if (stmt.next != null) {
|
|
894
|
-
let edge = [stmt.index, stmt.next.index];
|
|
895
|
-
this.dotEdges.push(edge);
|
|
896
|
-
this.getDotEdges(stmt.next);
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
errorTest(stmt) {
|
|
901
|
-
var _a, _b;
|
|
902
|
-
let mes = 'ifnext error ';
|
|
903
|
-
if ((_a = this.declaringClass) === null || _a === void 0 ? void 0 : _a.getDeclaringArkFile()) {
|
|
904
|
-
mes += ((_b = this.declaringClass) === null || _b === void 0 ? void 0 : _b.getDeclaringArkFile().getName()) + '.' + this.declaringClass.getName() + '.' + this.name;
|
|
905
|
-
}
|
|
906
|
-
mes += '\n' + stmt.code;
|
|
907
|
-
throw new textError(mes);
|
|
908
|
-
}
|
|
909
|
-
printBlocks() {
|
|
910
|
-
var _a, _b, _c, _d, _e, _f, _g;
|
|
911
|
-
let text = '';
|
|
912
|
-
if ((_a = this.declaringClass) === null || _a === void 0 ? void 0 : _a.getDeclaringArkFile()) {
|
|
913
|
-
text += this.declaringClass.getDeclaringArkFile().getName() + '\n';
|
|
914
|
-
}
|
|
915
|
-
for (let bi = 0; bi < this.blocks.length; bi++) {
|
|
916
|
-
let block = this.blocks[bi];
|
|
917
|
-
if (bi !== 0)
|
|
918
|
-
text += 'label' + block.id + ':\n';
|
|
919
|
-
let length = block.stmts.length;
|
|
920
|
-
for (let i = 0; i < length; i++) {
|
|
921
|
-
let stmt = block.stmts[i];
|
|
922
|
-
if (stmt.type === 'ifStatement' || stmt.type === 'loopStatement' || stmt.type === 'catchOrNot') {
|
|
923
|
-
let cstm = stmt;
|
|
924
|
-
if (cstm.nextT == null || cstm.nextF == null) {
|
|
925
|
-
this.errorTest(cstm);
|
|
926
|
-
return text;
|
|
927
|
-
}
|
|
928
|
-
if (!cstm.nextF.block || !cstm.nextT.block) {
|
|
929
|
-
this.errorTest(cstm);
|
|
930
|
-
return text;
|
|
931
|
-
}
|
|
932
|
-
stmt.code = 'if !(' + cstm.condition + ') goto label' + cstm.nextF.block.id;
|
|
933
|
-
if (i === length - 1 && bi + 1 < this.blocks.length && this.blocks[bi + 1].id !== cstm.nextT.block.id) {
|
|
934
|
-
let gotoStm = new StatementBuilder('gotoStatement', 'goto label' + cstm.nextT.block.id, null, block.stmts[0].scopeID);
|
|
935
|
-
block.stmts.push(gotoStm);
|
|
936
|
-
length++;
|
|
937
|
-
}
|
|
938
|
-
}
|
|
939
|
-
else if (stmt.type === 'breakStatement' || stmt.type === 'continueStatement') {
|
|
940
|
-
if (!((_b = stmt.next) === null || _b === void 0 ? void 0 : _b.block)) {
|
|
941
|
-
this.errorTest(stmt);
|
|
942
|
-
return text;
|
|
943
|
-
}
|
|
944
|
-
stmt.code = 'goto label' + ((_c = stmt.next) === null || _c === void 0 ? void 0 : _c.block.id);
|
|
945
|
-
}
|
|
946
|
-
else {
|
|
947
|
-
if (i === length - 1 && ((_d = stmt.next) === null || _d === void 0 ? void 0 : _d.block) && (bi + 1 < this.blocks.length && this.blocks[bi + 1].id !== stmt.next.block.id || bi + 1 === this.blocks.length)) {
|
|
948
|
-
let gotoStm = new StatementBuilder('StatementBuilder', 'goto label' + ((_e = stmt.next) === null || _e === void 0 ? void 0 : _e.block.id), null, block.stmts[0].scopeID);
|
|
949
|
-
block.stmts.push(gotoStm);
|
|
950
|
-
length++;
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
if (stmt.addressCode3.length === 0) {
|
|
954
|
-
text += ' ' + stmt.code + '\n';
|
|
955
|
-
}
|
|
956
|
-
else {
|
|
957
|
-
for (let ac of stmt.addressCode3) {
|
|
958
|
-
if (ac.startsWith('if') || ac.startsWith('while')) {
|
|
959
|
-
let cstm = stmt;
|
|
960
|
-
let condition = ac.substring(ac.indexOf('('));
|
|
961
|
-
let goto = '';
|
|
962
|
-
if ((_f = cstm.nextF) === null || _f === void 0 ? void 0 : _f.block)
|
|
963
|
-
goto = 'if !' + condition + ' goto label' + ((_g = cstm.nextF) === null || _g === void 0 ? void 0 : _g.block.id);
|
|
964
|
-
stmt.addressCode3[stmt.addressCode3.indexOf(ac)] = goto;
|
|
965
|
-
text += ' ' + goto + '\n';
|
|
966
|
-
}
|
|
967
|
-
else
|
|
968
|
-
text += ' ' + ac + '\n';
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
}
|
|
973
|
-
for (let cat of this.catches) {
|
|
974
|
-
text += 'catch ' + cat.errorName + ' from label ' + cat.from + ' to label ' + cat.to + ' with label' + cat.withLabel + '\n';
|
|
975
|
-
}
|
|
976
|
-
return text;
|
|
977
|
-
}
|
|
978
|
-
buildStatementBuilder4ArrowFunction(stmt) {
|
|
979
|
-
let s = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
|
|
980
|
-
this.entry.next = s;
|
|
981
|
-
s.lasts = new Set([this.entry]);
|
|
982
|
-
s.next = this.exit;
|
|
983
|
-
this.exit.lasts = new Set([s]);
|
|
984
|
-
}
|
|
985
|
-
buildCfgBuilder() {
|
|
986
|
-
let stmts = [];
|
|
987
|
-
if (ts.isSourceFile(this.astRoot)) {
|
|
988
|
-
stmts = [...this.astRoot.statements];
|
|
989
|
-
}
|
|
990
|
-
else if (ts.isFunctionDeclaration(this.astRoot) || ts.isMethodDeclaration(this.astRoot) || ts.isConstructorDeclaration(this.astRoot)
|
|
991
|
-
|| ts.isGetAccessorDeclaration(this.astRoot) || ts.isSetAccessorDeclaration(this.astRoot) || ts.isFunctionExpression(this.astRoot)
|
|
992
|
-
|| ts.isClassStaticBlockDeclaration(this.astRoot)) {
|
|
993
|
-
if (this.astRoot.body) {
|
|
994
|
-
stmts = [...this.astRoot.body.statements];
|
|
995
|
-
}
|
|
996
|
-
else {
|
|
997
|
-
this.emptyBody = true;
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
else if (ts.isArrowFunction(this.astRoot)) {
|
|
1001
|
-
if (ts.isBlock(this.astRoot.body)) {
|
|
1002
|
-
stmts = [...this.astRoot.body.statements];
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
else if (ts.isMethodSignature(this.astRoot) || ts.isConstructSignatureDeclaration(this.astRoot)
|
|
1006
|
-
|| ts.isCallSignatureDeclaration(this.astRoot) || ts.isFunctionTypeNode(this.astRoot)) {
|
|
1007
|
-
this.emptyBody = true;
|
|
1008
|
-
}
|
|
1009
|
-
else if (ts.isModuleDeclaration(this.astRoot) && ts.isModuleBlock(this.astRoot.body)) {
|
|
1010
|
-
stmts = [...this.astRoot.body.statements];
|
|
1011
|
-
}
|
|
1012
|
-
if (!ModelUtils_1.ModelUtils.isArkUIBuilderMethod(this.declaringMethod)) {
|
|
1013
|
-
this.walkAST(this.entry, this.exit, stmts);
|
|
1014
|
-
}
|
|
1015
|
-
else {
|
|
1016
|
-
this.handleBuilder(stmts);
|
|
1017
|
-
}
|
|
1018
|
-
if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
|
|
1019
|
-
this.buildStatementBuilder4ArrowFunction(this.astRoot.body);
|
|
1020
|
-
}
|
|
1021
|
-
this.addReturnInEmptyMethod();
|
|
1022
|
-
this.deleteExit();
|
|
1023
|
-
this.CfgBuilder2Array(this.entry);
|
|
1024
|
-
this.addStmtBuilderPosition();
|
|
1025
|
-
this.buildBlocks();
|
|
1026
|
-
this.blocks = this.blocks.filter((b) => b.stmts.length !== 0);
|
|
1027
|
-
this.buildBlocksNextLast();
|
|
1028
|
-
this.addReturnStmt();
|
|
1029
|
-
}
|
|
1030
|
-
handleBuilder(stmts) {
|
|
1031
|
-
let lastStmt = this.entry;
|
|
1032
|
-
for (const stmt of stmts) {
|
|
1033
|
-
const stmtBuilder = new StatementBuilder('statement', stmt.getText(this.sourceFile), stmt, 0);
|
|
1034
|
-
lastStmt.next = stmtBuilder;
|
|
1035
|
-
stmtBuilder.lasts.add(lastStmt);
|
|
1036
|
-
lastStmt = stmtBuilder;
|
|
1037
|
-
}
|
|
1038
|
-
lastStmt.next = this.exit;
|
|
1039
|
-
this.exit.lasts.add(lastStmt);
|
|
1040
|
-
}
|
|
1041
|
-
isBodyEmpty() {
|
|
1042
|
-
return this.emptyBody;
|
|
1043
|
-
}
|
|
1044
|
-
buildCfgAndOriginalCfg() {
|
|
1045
|
-
if (ts.isArrowFunction(this.astRoot) && !ts.isBlock(this.astRoot.body)) {
|
|
1046
|
-
return this.buildCfgAndOriginalCfgForSimpleArrowFunction();
|
|
1047
|
-
}
|
|
1048
|
-
return this.buildNormalCfgAndOriginalCfg();
|
|
1049
|
-
}
|
|
1050
|
-
buildCfgAndOriginalCfgForSimpleArrowFunction() {
|
|
1051
|
-
const stmts = [];
|
|
1052
|
-
const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
|
|
1053
|
-
arkIRTransformer.prebuildStmts().forEach(stmt => stmts.push(stmt));
|
|
1054
|
-
const expressionBodyNode = this.astRoot.body;
|
|
1055
|
-
const expressionBodyStmts = [];
|
|
1056
|
-
let { value: expressionBodyValue, valueOriginalPositions: expressionBodyPositions, stmts: tempStmts, } = arkIRTransformer.tsNodeToValueAndStmts(expressionBodyNode);
|
|
1057
|
-
tempStmts.forEach(stmt => expressionBodyStmts.push(stmt));
|
|
1058
|
-
if (IRUtils_1.IRUtils.moreThanOneAddress(expressionBodyValue)) {
|
|
1059
|
-
({
|
|
1060
|
-
value: expressionBodyValue,
|
|
1061
|
-
valueOriginalPositions: expressionBodyPositions,
|
|
1062
|
-
stmts: tempStmts,
|
|
1063
|
-
} = arkIRTransformer.generateAssignStmtForValue(expressionBodyValue, expressionBodyPositions));
|
|
1064
|
-
tempStmts.forEach(stmt => expressionBodyStmts.push(stmt));
|
|
1065
|
-
}
|
|
1066
|
-
const returnStmt = new Stmt_1.ArkReturnStmt(expressionBodyValue);
|
|
1067
|
-
returnStmt.setOperandOriginalPositions([expressionBodyPositions[0], ...expressionBodyPositions]);
|
|
1068
|
-
expressionBodyStmts.push(returnStmt);
|
|
1069
|
-
arkIRTransformer.mapStmtsToTsStmt(expressionBodyStmts, expressionBodyNode);
|
|
1070
|
-
expressionBodyStmts.forEach(stmt => stmts.push(stmt));
|
|
1071
|
-
const cfg = new Cfg_1.Cfg();
|
|
1072
|
-
const blockInCfg = new BasicBlock_1.BasicBlock();
|
|
1073
|
-
blockInCfg.setId(0);
|
|
1074
|
-
stmts.forEach(stmt => {
|
|
1075
|
-
blockInCfg.addStmt(stmt);
|
|
1076
|
-
stmt.setCfg(cfg);
|
|
1077
|
-
});
|
|
1078
|
-
cfg.addBlock(blockInCfg);
|
|
1079
|
-
cfg.setStartingStmt(stmts[0]);
|
|
1080
|
-
return {
|
|
1081
|
-
cfg: cfg,
|
|
1082
|
-
locals: arkIRTransformer.getLocals(),
|
|
1083
|
-
globals: arkIRTransformer.getGlobals(),
|
|
1084
|
-
aliasTypeMap: arkIRTransformer.getAliasTypeMap(),
|
|
1085
|
-
traps: [],
|
|
1086
|
-
};
|
|
1087
|
-
}
|
|
1088
|
-
buildNormalCfgAndOriginalCfg() {
|
|
1089
|
-
const blockBuilderToCfgBlock = new Map();
|
|
1090
|
-
const basicBlockSet = new Set();
|
|
1091
|
-
const arkIRTransformer = new ArkIRTransformer_1.ArkIRTransformer(this.sourceFile, this.declaringMethod);
|
|
1092
|
-
const blocksContainLoopCondition = new Set();
|
|
1093
|
-
const blockBuildersBeforeTry = new Set();
|
|
1094
|
-
const blockBuildersContainSwitch = [];
|
|
1095
|
-
const valueAndStmtsOfSwitchAndCasesAll = [];
|
|
1096
|
-
for (let i = 0; i < this.blocks.length; i++) {
|
|
1097
|
-
// build block in Cfg
|
|
1098
|
-
const stmtsInBlock = [];
|
|
1099
|
-
if (i === 0) {
|
|
1100
|
-
arkIRTransformer.prebuildStmts().forEach(stmt => stmtsInBlock.push(stmt));
|
|
1101
|
-
}
|
|
1102
|
-
const stmtsCnt = this.blocks[i].stmts.length;
|
|
1103
|
-
if (this.blocks[i].stmts[stmtsCnt - 1].type === 'tryStatement') {
|
|
1104
|
-
blockBuildersBeforeTry.add(this.blocks[i]);
|
|
1105
|
-
}
|
|
1106
|
-
for (const statementBuilder of this.blocks[i].stmts) {
|
|
1107
|
-
if (statementBuilder.type === 'loopStatement') {
|
|
1108
|
-
blocksContainLoopCondition.add(this.blocks[i]);
|
|
1109
|
-
}
|
|
1110
|
-
else if (statementBuilder instanceof SwitchStatementBuilder) {
|
|
1111
|
-
blockBuildersContainSwitch.push(this.blocks[i]);
|
|
1112
|
-
const valueAndStmtsOfSwitchAndCases = arkIRTransformer.switchStatementToValueAndStmts(statementBuilder.astNode);
|
|
1113
|
-
valueAndStmtsOfSwitchAndCasesAll.push(valueAndStmtsOfSwitchAndCases);
|
|
1114
|
-
continue;
|
|
1115
|
-
}
|
|
1116
|
-
if (statementBuilder.astNode && statementBuilder.code !== '') {
|
|
1117
|
-
arkIRTransformer.tsNodeToStmts(statementBuilder.astNode).forEach(s => stmtsInBlock.push(s));
|
|
1118
|
-
}
|
|
1119
|
-
else if (statementBuilder.code.startsWith('return')) {
|
|
1120
|
-
stmtsInBlock.push(new Stmt_1.ArkReturnVoidStmt());
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
const blockInCfg = new BasicBlock_1.BasicBlock();
|
|
1124
|
-
blockInCfg.setId(this.blocks[i].id);
|
|
1125
|
-
for (const stmt of stmtsInBlock) {
|
|
1126
|
-
blockInCfg.addStmt(stmt);
|
|
1127
|
-
}
|
|
1128
|
-
basicBlockSet.add(blockInCfg);
|
|
1129
|
-
blockBuilderToCfgBlock.set(this.blocks[i], blockInCfg);
|
|
1130
|
-
}
|
|
1131
|
-
let currBlockId = this.blocks.length;
|
|
1132
|
-
this.linkBasicBlocks(blockBuilderToCfgBlock);
|
|
1133
|
-
this.rebuildBlocksInLoop(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet);
|
|
1134
|
-
this.buildSwitch(blockBuilderToCfgBlock, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer, basicBlockSet);
|
|
1135
|
-
this.rebuildBlocksContainConditionalOperator(basicBlockSet);
|
|
1136
|
-
const traps = this.buildTraps(blockBuilderToCfgBlock, blockBuildersBeforeTry, arkIRTransformer, basicBlockSet);
|
|
1137
|
-
for (const blockBuilder of this.blocks) {
|
|
1138
|
-
if (blockBuilder.id === -1) {
|
|
1139
|
-
blockBuilder.id = currBlockId++;
|
|
1140
|
-
const block = blockBuilderToCfgBlock.get(blockBuilder);
|
|
1141
|
-
block.setId(blockBuilder.id);
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
const cfg = new Cfg_1.Cfg();
|
|
1145
|
-
const startingBasicBlock = blockBuilderToCfgBlock.get(this.blocks[0]);
|
|
1146
|
-
cfg.setStartingStmt(startingBasicBlock.getStmts()[0]);
|
|
1147
|
-
currBlockId = 0;
|
|
1148
|
-
for (const basicBlock of basicBlockSet) {
|
|
1149
|
-
basicBlock.setId(currBlockId++);
|
|
1150
|
-
cfg.addBlock(basicBlock);
|
|
1151
|
-
}
|
|
1152
|
-
for (const stmt of cfg.getStmts()) {
|
|
1153
|
-
stmt.setCfg(cfg);
|
|
1154
|
-
}
|
|
1155
|
-
return {
|
|
1156
|
-
cfg: cfg,
|
|
1157
|
-
locals: arkIRTransformer.getLocals(),
|
|
1158
|
-
globals: arkIRTransformer.getGlobals(),
|
|
1159
|
-
aliasTypeMap: arkIRTransformer.getAliasTypeMap(),
|
|
1160
|
-
traps: traps,
|
|
1161
|
-
};
|
|
1162
|
-
}
|
|
1163
|
-
linkBasicBlocks(blockBuilderToCfgBlock) {
|
|
1164
|
-
for (const [blockBuilder, cfgBlock] of blockBuilderToCfgBlock) {
|
|
1165
|
-
for (const successorBlockBuilder of blockBuilder.nexts) {
|
|
1166
|
-
if (!blockBuilderToCfgBlock.get(successorBlockBuilder)) {
|
|
1167
|
-
continue;
|
|
1168
|
-
}
|
|
1169
|
-
const successorBlock = blockBuilderToCfgBlock.get(successorBlockBuilder);
|
|
1170
|
-
cfgBlock.addSuccessorBlock(successorBlock);
|
|
1171
|
-
}
|
|
1172
|
-
for (const predecessorBlockBuilder of blockBuilder.lasts) {
|
|
1173
|
-
if (!blockBuilderToCfgBlock.get(predecessorBlockBuilder)) {
|
|
1174
|
-
continue;
|
|
1175
|
-
}
|
|
1176
|
-
const predecessorBlock = blockBuilderToCfgBlock.get(predecessorBlockBuilder);
|
|
1177
|
-
cfgBlock.addPredecessorBlock(predecessorBlock);
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
}
|
|
1181
|
-
rebuildBlocksContainConditionalOperator(basicBlockSet) {
|
|
1182
|
-
var _a;
|
|
1183
|
-
if (ModelUtils_1.ModelUtils.isArkUIBuilderMethod(this.declaringMethod)) {
|
|
1184
|
-
this.deleteDummyConditionalOperatorStmt(basicBlockSet);
|
|
1185
|
-
return;
|
|
1186
|
-
}
|
|
1187
|
-
const currBasicBlocks = Array.from(basicBlockSet);
|
|
1188
|
-
for (const currBasicBlock of currBasicBlocks) {
|
|
1189
|
-
const stmtsInCurrBasicBlock = Array.from(currBasicBlock.getStmts());
|
|
1190
|
-
const stmtsCnt = stmtsInCurrBasicBlock.length;
|
|
1191
|
-
let conditionalOperatorEndPos = -1;
|
|
1192
|
-
for (let i = stmtsCnt - 1; i >= 0; i--) {
|
|
1193
|
-
const stmt = stmtsInCurrBasicBlock[i];
|
|
1194
|
-
if (stmt instanceof ArkIRTransformer_1.DummyStmt && ((_a = stmt.toString()) === null || _a === void 0 ? void 0 : _a.startsWith(ArkIRTransformer_1.ArkIRTransformer.DUMMY_CONDITIONAL_OPERATOR_END_STMT))) {
|
|
1195
|
-
conditionalOperatorEndPos = i;
|
|
1196
|
-
break;
|
|
1197
|
-
}
|
|
1198
|
-
}
|
|
1199
|
-
if (conditionalOperatorEndPos === -1) {
|
|
1200
|
-
continue;
|
|
1201
|
-
}
|
|
1202
|
-
let { generatedTopBlock: generatedTopBlock, generatedBottomBlocks: generatedBottomBlocks, } = this.generateBlocksContainConditionalOperatorGroup(stmtsInCurrBasicBlock.slice(0, conditionalOperatorEndPos + 1), basicBlockSet);
|
|
1203
|
-
if (conditionalOperatorEndPos !== stmtsCnt - 1) { // need create a new basic block for rest statements
|
|
1204
|
-
const { generatedTopBlock: extraBlock, } = this.generateBlockWithoutConditionalOperator(stmtsInCurrBasicBlock.slice(conditionalOperatorEndPos + 1));
|
|
1205
|
-
generatedBottomBlocks.forEach(generatedBottomBlock => {
|
|
1206
|
-
generatedBottomBlock.addSuccessorBlock(extraBlock);
|
|
1207
|
-
extraBlock.addPredecessorBlock(generatedBottomBlock);
|
|
1208
|
-
});
|
|
1209
|
-
basicBlockSet.add(extraBlock);
|
|
1210
|
-
generatedBottomBlocks = this.removeUnnecessaryBlocksInConditionalOperator(extraBlock, basicBlockSet);
|
|
1211
|
-
}
|
|
1212
|
-
this.relinkPrevAndSuccOfBlockContainConditionalOperator(currBasicBlock, generatedTopBlock, generatedBottomBlocks);
|
|
1213
|
-
basicBlockSet.delete(currBasicBlock);
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
relinkPrevAndSuccOfBlockContainConditionalOperator(currBasicBlock, generatedTopBlock, generatedBottomBlocks) {
|
|
1217
|
-
const predecessorsOfCurrBasicBlock = Array.from(currBasicBlock.getPredecessors());
|
|
1218
|
-
predecessorsOfCurrBasicBlock.forEach(predecessor => {
|
|
1219
|
-
predecessor.removeSuccessorBlock(currBasicBlock);
|
|
1220
|
-
currBasicBlock.removePredecessorBlock(predecessor);
|
|
1221
|
-
generatedTopBlock.addPredecessorBlock(predecessor);
|
|
1222
|
-
predecessor.addSuccessorBlock(generatedTopBlock);
|
|
1223
|
-
});
|
|
1224
|
-
const successorsOfCurrBasicBlock = Array.from(currBasicBlock.getSuccessors());
|
|
1225
|
-
successorsOfCurrBasicBlock.forEach(successor => {
|
|
1226
|
-
successor.removePredecessorBlock(currBasicBlock);
|
|
1227
|
-
currBasicBlock.removeSuccessorBlock(successor);
|
|
1228
|
-
generatedBottomBlocks.forEach(generatedBottomBlock => {
|
|
1229
|
-
generatedBottomBlock.addSuccessorBlock(successor);
|
|
1230
|
-
successor.addPredecessorBlock(generatedBottomBlock);
|
|
1231
|
-
});
|
|
1232
|
-
});
|
|
1233
|
-
}
|
|
1234
|
-
generateBlocksContainConditionalOperatorGroup(sourceStmts, basicBlockSet) {
|
|
1235
|
-
const { firstEndPos: firstEndPos } = this.findFirstConditionalOperator(sourceStmts);
|
|
1236
|
-
if (firstEndPos === -1) {
|
|
1237
|
-
return this.generateBlockWithoutConditionalOperator(sourceStmts);
|
|
1238
|
-
}
|
|
1239
|
-
const { generatedTopBlock: firstGeneratedTopBlock, generatedBottomBlocks: firstGeneratedBottomBlocks, generatedAllBlocks: firstGeneratedAllBlocks, } = this.generateBlocksContainSingleConditionalOperator(sourceStmts.slice(0, firstEndPos + 1));
|
|
1240
|
-
const generatedTopBlock = firstGeneratedTopBlock;
|
|
1241
|
-
let generatedBottomBlocks = firstGeneratedBottomBlocks;
|
|
1242
|
-
firstGeneratedAllBlocks.forEach(block => basicBlockSet.add(block));
|
|
1243
|
-
const stmtsCnt = sourceStmts.length;
|
|
1244
|
-
if (firstEndPos !== stmtsCnt - 1) { // need handle other conditional operators
|
|
1245
|
-
const { generatedTopBlock: restGeneratedTopBlock, generatedBottomBlocks: restGeneratedBottomBlocks, } = this.generateBlocksContainConditionalOperatorGroup(sourceStmts.slice(firstEndPos + 1, stmtsCnt), basicBlockSet);
|
|
1246
|
-
firstGeneratedBottomBlocks.forEach(firstGeneratedBottomBlock => {
|
|
1247
|
-
firstGeneratedBottomBlock.addSuccessorBlock(restGeneratedTopBlock);
|
|
1248
|
-
restGeneratedTopBlock.addPredecessorBlock(firstGeneratedBottomBlock);
|
|
1249
|
-
});
|
|
1250
|
-
restGeneratedBottomBlocks.forEach(block => basicBlockSet.add(block));
|
|
1251
|
-
this.removeUnnecessaryBlocksInConditionalOperator(restGeneratedTopBlock, basicBlockSet);
|
|
1252
|
-
generatedBottomBlocks = restGeneratedBottomBlocks;
|
|
1253
|
-
}
|
|
1254
|
-
return { generatedTopBlock, generatedBottomBlocks };
|
|
1255
|
-
}
|
|
1256
|
-
generateBlocksContainSingleConditionalOperator(sourceStmts) {
|
|
1257
|
-
const { firstIfTruePos: ifTruePos, firstIfFalsePos: ifFalsePos, firstEndPos: endPos, } = this.findFirstConditionalOperator(sourceStmts);
|
|
1258
|
-
if (endPos === -1) {
|
|
1259
|
-
return this.generateBlockWithoutConditionalOperator(sourceStmts);
|
|
1260
|
-
}
|
|
1261
|
-
const { generatedTopBlock: generatedTopBlock, generatedAllBlocks: generatedAllBlocks, } = this.generateBlockWithoutConditionalOperator(sourceStmts.slice(0, ifTruePos));
|
|
1262
|
-
let generatedBottomBlocks = [];
|
|
1263
|
-
const { generatedTopBlock: generatedTopBlockOfTrueBranch, generatedBottomBlocks: generatedBottomBlocksOfTrueBranch, generatedAllBlocks: generatedAllBlocksOfTrueBranch, } = this.generateBlocksContainSingleConditionalOperator(sourceStmts.slice(ifTruePos + 1, ifFalsePos));
|
|
1264
|
-
generatedBottomBlocks.push(...generatedBottomBlocksOfTrueBranch);
|
|
1265
|
-
generatedAllBlocks.push(...generatedAllBlocksOfTrueBranch);
|
|
1266
|
-
const { generatedTopBlock: generatedTopBlockOfFalseBranch, generatedBottomBlocks: generatedBottomBlocksOfFalseBranch, generatedAllBlocks: generatedAllBlocksOfFalseBranch, } = this.generateBlocksContainSingleConditionalOperator(sourceStmts.slice(ifFalsePos + 1, endPos));
|
|
1267
|
-
generatedBottomBlocks.push(...generatedBottomBlocksOfFalseBranch);
|
|
1268
|
-
generatedAllBlocks.push(...generatedAllBlocksOfFalseBranch);
|
|
1269
|
-
generatedTopBlock.addSuccessorBlock(generatedTopBlockOfTrueBranch);
|
|
1270
|
-
generatedTopBlockOfTrueBranch.addPredecessorBlock(generatedTopBlock);
|
|
1271
|
-
generatedTopBlock.addSuccessorBlock(generatedTopBlockOfFalseBranch);
|
|
1272
|
-
generatedTopBlockOfFalseBranch.addPredecessorBlock(generatedTopBlock);
|
|
1273
|
-
const stmtsCnt = sourceStmts.length;
|
|
1274
|
-
if (endPos !== stmtsCnt - 1) { // need create a new basic block for rest statements
|
|
1275
|
-
const { generatedTopBlock: extraBlock, } = this.generateBlockWithoutConditionalOperator(sourceStmts.slice(endPos + 1));
|
|
1276
|
-
generatedBottomBlocks.forEach(generatedBottomBlock => {
|
|
1277
|
-
generatedBottomBlock.addSuccessorBlock(extraBlock);
|
|
1278
|
-
extraBlock.addPredecessorBlock(generatedBottomBlock);
|
|
1279
|
-
});
|
|
1280
|
-
generatedBottomBlocks = [extraBlock];
|
|
1281
|
-
generatedAllBlocks.push(extraBlock);
|
|
1282
|
-
}
|
|
1283
|
-
return { generatedTopBlock, generatedBottomBlocks, generatedAllBlocks };
|
|
1284
|
-
}
|
|
1285
|
-
generateBlockWithoutConditionalOperator(sourceStmts) {
|
|
1286
|
-
const generatedBlock = new BasicBlock_1.BasicBlock();
|
|
1287
|
-
sourceStmts.forEach(stmt => generatedBlock.addStmt(stmt));
|
|
1288
|
-
return {
|
|
1289
|
-
generatedTopBlock: generatedBlock,
|
|
1290
|
-
generatedBottomBlocks: [generatedBlock],
|
|
1291
|
-
generatedAllBlocks: [generatedBlock],
|
|
1292
|
-
};
|
|
1293
|
-
}
|
|
1294
|
-
deleteDummyConditionalOperatorStmt(basicBlockSet) {
|
|
1295
|
-
var _a;
|
|
1296
|
-
for (const basicBlock of basicBlockSet) {
|
|
1297
|
-
const stmts = Array.from(basicBlock.getStmts());
|
|
1298
|
-
for (const stmt of stmts) {
|
|
1299
|
-
if (stmt instanceof ArkIRTransformer_1.DummyStmt && ((_a = stmt.toString()) === null || _a === void 0 ? void 0 : _a.startsWith(ArkIRTransformer_1.ArkIRTransformer.DUMMY_CONDITIONAL_OPERATOR))) {
|
|
1300
|
-
basicBlock.remove(stmt);
|
|
1301
|
-
}
|
|
1302
|
-
}
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
findFirstConditionalOperator(stmts) {
|
|
1306
|
-
let firstIfTruePos = -1;
|
|
1307
|
-
let firstIfFalsePos = -1;
|
|
1308
|
-
let firstEndPos = -1;
|
|
1309
|
-
let firstConditionalOperatorNo = '';
|
|
1310
|
-
for (let i = 0; i < stmts.length; i++) {
|
|
1311
|
-
const stmt = stmts[i];
|
|
1312
|
-
if (stmt instanceof ArkIRTransformer_1.DummyStmt) {
|
|
1313
|
-
if (stmt.toString()
|
|
1314
|
-
.startsWith(ArkIRTransformer_1.ArkIRTransformer.DUMMY_CONDITIONAL_OPERATOR_IF_TRUE_STMT) && firstIfTruePos === -1) {
|
|
1315
|
-
firstIfTruePos = i;
|
|
1316
|
-
firstConditionalOperatorNo =
|
|
1317
|
-
stmt.toString().replace(ArkIRTransformer_1.ArkIRTransformer.DUMMY_CONDITIONAL_OPERATOR_IF_TRUE_STMT, '');
|
|
1318
|
-
}
|
|
1319
|
-
else if (stmt.toString() === ArkIRTransformer_1.ArkIRTransformer.DUMMY_CONDITIONAL_OPERATOR_IF_FALSE_STMT + firstConditionalOperatorNo) {
|
|
1320
|
-
firstIfFalsePos = i;
|
|
1321
|
-
}
|
|
1322
|
-
else if (stmt.toString() === ArkIRTransformer_1.ArkIRTransformer.DUMMY_CONDITIONAL_OPERATOR_END_STMT + firstConditionalOperatorNo) {
|
|
1323
|
-
firstEndPos = i;
|
|
1324
|
-
}
|
|
1325
|
-
}
|
|
1326
|
-
}
|
|
1327
|
-
return { firstIfTruePos, firstIfFalsePos, firstEndPos };
|
|
1328
|
-
}
|
|
1329
|
-
removeUnnecessaryBlocksInConditionalOperator(bottomBlock, allBlocks) {
|
|
1330
|
-
const firstStmtInBottom = bottomBlock.getStmts()[0];
|
|
1331
|
-
if (!(firstStmtInBottom instanceof Stmt_1.ArkAssignStmt)) {
|
|
1332
|
-
return [bottomBlock];
|
|
1333
|
-
}
|
|
1334
|
-
const targetValue = firstStmtInBottom.getLeftOp();
|
|
1335
|
-
const tempResultValue = firstStmtInBottom.getRightOp();
|
|
1336
|
-
if (!(targetValue instanceof Local_1.Local && IRUtils_1.IRUtils.isTempLocal(tempResultValue))) {
|
|
1337
|
-
return [bottomBlock];
|
|
1338
|
-
}
|
|
1339
|
-
const oldPredecessors = Array.from(bottomBlock.getPredecessors());
|
|
1340
|
-
const newPredecessors = [];
|
|
1341
|
-
for (const predecessor of oldPredecessors) {
|
|
1342
|
-
predecessor.removeSuccessorBlock(bottomBlock);
|
|
1343
|
-
newPredecessors.push(...this.replaceTempRecursively(predecessor, targetValue, tempResultValue, allBlocks));
|
|
1344
|
-
}
|
|
1345
|
-
bottomBlock.remove(firstStmtInBottom);
|
|
1346
|
-
if (bottomBlock.getStmts().length === 0) { // must be a new block without successors
|
|
1347
|
-
allBlocks.delete(bottomBlock);
|
|
1348
|
-
return newPredecessors;
|
|
1349
|
-
}
|
|
1350
|
-
oldPredecessors.forEach((oldPredecessor) => {
|
|
1351
|
-
bottomBlock.removePredecessorBlock(oldPredecessor);
|
|
1352
|
-
});
|
|
1353
|
-
newPredecessors.forEach((newPredecessor) => {
|
|
1354
|
-
bottomBlock.addPredecessorBlock(newPredecessor);
|
|
1355
|
-
newPredecessor.addSuccessorBlock(bottomBlock);
|
|
1356
|
-
});
|
|
1357
|
-
return [bottomBlock];
|
|
1358
|
-
}
|
|
1359
|
-
replaceTempRecursively(currBottomBlock, targetLocal, tempResultLocal, allBlocks) {
|
|
1360
|
-
const stmts = currBottomBlock.getStmts();
|
|
1361
|
-
const stmtsCnt = stmts.length;
|
|
1362
|
-
let tempResultReassignStmt = null;
|
|
1363
|
-
for (let i = stmtsCnt - 1; i >= 0; i--) {
|
|
1364
|
-
const stmt = stmts[i];
|
|
1365
|
-
if (stmt instanceof Stmt_1.ArkAssignStmt && stmt.getLeftOp() === tempResultLocal) {
|
|
1366
|
-
if (IRUtils_1.IRUtils.isTempLocal(stmt.getRightOp())) {
|
|
1367
|
-
tempResultReassignStmt = stmt;
|
|
1368
|
-
}
|
|
1369
|
-
else {
|
|
1370
|
-
stmt.setLeftOp(targetLocal);
|
|
1371
|
-
}
|
|
1372
|
-
}
|
|
1373
|
-
}
|
|
1374
|
-
let newBottomBlocks = [];
|
|
1375
|
-
if (tempResultReassignStmt) {
|
|
1376
|
-
const oldPredecessors = currBottomBlock.getPredecessors();
|
|
1377
|
-
const newPredecessors = [];
|
|
1378
|
-
const prevTempResultLocal = tempResultReassignStmt.getRightOp();
|
|
1379
|
-
for (const predecessor of oldPredecessors) {
|
|
1380
|
-
predecessor.removeSuccessorBlock(currBottomBlock);
|
|
1381
|
-
newPredecessors.push(...this.replaceTempRecursively(predecessor, targetLocal, prevTempResultLocal, allBlocks));
|
|
1382
|
-
}
|
|
1383
|
-
currBottomBlock.remove(tempResultReassignStmt);
|
|
1384
|
-
if (currBottomBlock.getStmts().length === 0) {
|
|
1385
|
-
// remove this block
|
|
1386
|
-
newBottomBlocks = newPredecessors;
|
|
1387
|
-
allBlocks.delete(currBottomBlock);
|
|
1388
|
-
}
|
|
1389
|
-
else {
|
|
1390
|
-
currBottomBlock.getPredecessors().splice(0, oldPredecessors.length, ...newPredecessors);
|
|
1391
|
-
newPredecessors.forEach((newPredecessor) => {
|
|
1392
|
-
newPredecessor.addSuccessorBlock(currBottomBlock);
|
|
1393
|
-
});
|
|
1394
|
-
newBottomBlocks = [currBottomBlock];
|
|
1395
|
-
}
|
|
1396
|
-
}
|
|
1397
|
-
else {
|
|
1398
|
-
newBottomBlocks = [currBottomBlock];
|
|
1399
|
-
}
|
|
1400
|
-
return newBottomBlocks;
|
|
1401
|
-
}
|
|
1402
|
-
rebuildBlocksInLoop(blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet) {
|
|
1403
|
-
for (const blockBuilder of blocksContainLoopCondition) {
|
|
1404
|
-
if (!blockBuilderToCfgBlock.get(blockBuilder)) {
|
|
1405
|
-
continue;
|
|
1406
|
-
}
|
|
1407
|
-
const block = blockBuilderToCfgBlock.get(blockBuilder);
|
|
1408
|
-
const blockId = block.getId();
|
|
1409
|
-
const stmts = block.getStmts();
|
|
1410
|
-
const stmtsCnt = stmts.length;
|
|
1411
|
-
const { ifStmtIdx, iteratorNextStmtIdx, dummyInitializerStmtIdx } = this.findIteratorIdx(stmts);
|
|
1412
|
-
if (iteratorNextStmtIdx !== -1 || dummyInitializerStmtIdx !== -1) {
|
|
1413
|
-
const lastStmtIdxBeforeCondition = iteratorNextStmtIdx !== -1 ? iteratorNextStmtIdx : dummyInitializerStmtIdx;
|
|
1414
|
-
const stmtsInsertBeforeCondition = stmts.slice(0, lastStmtIdxBeforeCondition);
|
|
1415
|
-
let prevBlockBuilderContainsLoop = this.doesPrevBlockBuilderContainLoop(blockBuilder, blockId, blocksContainLoopCondition);
|
|
1416
|
-
if (prevBlockBuilderContainsLoop) {
|
|
1417
|
-
// should create an extra block when previous block contains loop condition
|
|
1418
|
-
this.insertBeforeConditionBlockBuilder(blockBuilderToCfgBlock, blockBuilder, stmtsInsertBeforeCondition, false, basicBlockSet);
|
|
1419
|
-
}
|
|
1420
|
-
else {
|
|
1421
|
-
const blockBuilderBeforeCondition = blockBuilder.lasts[0];
|
|
1422
|
-
const blockBeforeCondition = blockBuilderToCfgBlock.get(blockBuilderBeforeCondition);
|
|
1423
|
-
stmtsInsertBeforeCondition.forEach(stmt => blockBeforeCondition === null || blockBeforeCondition === void 0 ? void 0 : blockBeforeCondition.getStmts().push(stmt));
|
|
1424
|
-
}
|
|
1425
|
-
if (dummyInitializerStmtIdx !== -1 && ifStmtIdx !== stmtsCnt - 1) {
|
|
1426
|
-
// put incrementor statements into block which reenters condition
|
|
1427
|
-
this.adjustIncrementorStmts(stmts, ifStmtIdx, blockBuilder, blockId, blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet);
|
|
1428
|
-
}
|
|
1429
|
-
else if (iteratorNextStmtIdx !== -1) {
|
|
1430
|
-
// put statements which get value of iterator into block after condition
|
|
1431
|
-
const blockBuilderAfterCondition = blockBuilder.nexts[0];
|
|
1432
|
-
const blockAfterCondition = blockBuilderToCfgBlock.get(blockBuilderAfterCondition);
|
|
1433
|
-
const stmtsAfterCondition = stmts.slice(ifStmtIdx + 1);
|
|
1434
|
-
blockAfterCondition === null || blockAfterCondition === void 0 ? void 0 : blockAfterCondition.getStmts().splice(0, 0, ...stmtsAfterCondition);
|
|
1435
|
-
}
|
|
1436
|
-
// remove statements which should not in condition
|
|
1437
|
-
const firstStmtIdxInCondition = iteratorNextStmtIdx !== -1 ? iteratorNextStmtIdx : dummyInitializerStmtIdx + 1;
|
|
1438
|
-
stmts.splice(0, firstStmtIdxInCondition);
|
|
1439
|
-
stmts.splice(ifStmtIdx - firstStmtIdxInCondition + 1);
|
|
1440
|
-
}
|
|
1441
|
-
}
|
|
1442
|
-
}
|
|
1443
|
-
doesPrevBlockBuilderContainLoop(currBlockBuilder, currBlockId, blocksContainLoopCondition) {
|
|
1444
|
-
let prevBlockBuilderContainsLoop = false;
|
|
1445
|
-
for (const prevBlockBuilder of currBlockBuilder.lasts) {
|
|
1446
|
-
if (prevBlockBuilder.id < currBlockId && blocksContainLoopCondition.has(prevBlockBuilder)) {
|
|
1447
|
-
prevBlockBuilderContainsLoop = true;
|
|
1448
|
-
break;
|
|
1449
|
-
}
|
|
1450
|
-
}
|
|
1451
|
-
return prevBlockBuilderContainsLoop;
|
|
1452
|
-
}
|
|
1453
|
-
insertBeforeConditionBlockBuilder(blockBuilderToCfgBlock, conditionBlockBuilder, stmtsInsertBeforeCondition, collectReenter, basicBlockSet) {
|
|
1454
|
-
const blockId = conditionBlockBuilder.id;
|
|
1455
|
-
const block = blockBuilderToCfgBlock.get(conditionBlockBuilder);
|
|
1456
|
-
const blockBuildersBeforeCondition = [];
|
|
1457
|
-
const blocksBeforeCondition = [];
|
|
1458
|
-
const blockBuildersReenterCondition = [];
|
|
1459
|
-
const blocksReenterCondition = [];
|
|
1460
|
-
for (const prevBlockBuilder of conditionBlockBuilder.lasts) {
|
|
1461
|
-
const prevBlock = blockBuilderToCfgBlock.get(prevBlockBuilder);
|
|
1462
|
-
if (prevBlock.getId() < blockId) {
|
|
1463
|
-
blockBuildersBeforeCondition.push(prevBlockBuilder);
|
|
1464
|
-
blocksBeforeCondition.push(prevBlock);
|
|
1465
|
-
}
|
|
1466
|
-
else {
|
|
1467
|
-
blockBuildersReenterCondition.push(prevBlockBuilder);
|
|
1468
|
-
blocksReenterCondition.push(prevBlock);
|
|
1469
|
-
}
|
|
1470
|
-
}
|
|
1471
|
-
let collectedBlockBuilders = [];
|
|
1472
|
-
let collectedBlocks = [];
|
|
1473
|
-
if (collectReenter) {
|
|
1474
|
-
collectedBlockBuilders = blockBuildersReenterCondition;
|
|
1475
|
-
collectedBlocks = blocksReenterCondition;
|
|
1476
|
-
}
|
|
1477
|
-
else {
|
|
1478
|
-
collectedBlockBuilders = blockBuildersBeforeCondition;
|
|
1479
|
-
collectedBlocks = blocksBeforeCondition;
|
|
1480
|
-
}
|
|
1481
|
-
const blockBuilderInsertBeforeCondition = new Block(-1, []);
|
|
1482
|
-
blockBuilderInsertBeforeCondition.lasts.push(...collectedBlockBuilders);
|
|
1483
|
-
blockBuilderInsertBeforeCondition.nexts.push(conditionBlockBuilder);
|
|
1484
|
-
const blockInsertBeforeCondition = new BasicBlock_1.BasicBlock();
|
|
1485
|
-
stmtsInsertBeforeCondition.forEach(stmt => blockInsertBeforeCondition.getStmts().push(stmt));
|
|
1486
|
-
blockInsertBeforeCondition.getPredecessors().push(...collectedBlocks);
|
|
1487
|
-
blockInsertBeforeCondition.addSuccessorBlock(block);
|
|
1488
|
-
for (const prevBlockBuilder of collectedBlockBuilders) {
|
|
1489
|
-
const prevBlock = blockBuilderToCfgBlock.get(prevBlockBuilder);
|
|
1490
|
-
for (let j = 0; j < prevBlockBuilder.nexts.length; j++) {
|
|
1491
|
-
if (prevBlockBuilder.nexts[j] === conditionBlockBuilder) {
|
|
1492
|
-
prevBlockBuilder.nexts[j] = blockBuilderInsertBeforeCondition;
|
|
1493
|
-
prevBlock.setSuccessorBlock(j, blockInsertBeforeCondition);
|
|
1494
|
-
break;
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
let newPrevBlockBuildersBeforeCondition = [];
|
|
1499
|
-
let newPrevBlocksBeforeCondition = [];
|
|
1500
|
-
if (collectReenter) {
|
|
1501
|
-
newPrevBlockBuildersBeforeCondition = [...blockBuildersBeforeCondition, blockBuilderInsertBeforeCondition];
|
|
1502
|
-
newPrevBlocksBeforeCondition = [...blocksBeforeCondition, blockInsertBeforeCondition];
|
|
1503
|
-
}
|
|
1504
|
-
else {
|
|
1505
|
-
newPrevBlockBuildersBeforeCondition = [blockBuilderInsertBeforeCondition, ...blockBuildersReenterCondition];
|
|
1506
|
-
newPrevBlocksBeforeCondition = [blockInsertBeforeCondition, ...blocksReenterCondition];
|
|
1507
|
-
}
|
|
1508
|
-
conditionBlockBuilder.lasts = newPrevBlockBuildersBeforeCondition;
|
|
1509
|
-
const predecessorsCnt = block.getPredecessors().length;
|
|
1510
|
-
block.getPredecessors().splice(0, predecessorsCnt, ...newPrevBlocksBeforeCondition);
|
|
1511
|
-
this.blocks.push(blockBuilderInsertBeforeCondition);
|
|
1512
|
-
basicBlockSet.add(blockInsertBeforeCondition);
|
|
1513
|
-
blockBuilderToCfgBlock.set(blockBuilderInsertBeforeCondition, blockInsertBeforeCondition);
|
|
1514
|
-
}
|
|
1515
|
-
findIteratorIdx(stmts) {
|
|
1516
|
-
let ifStmtIdx = -1;
|
|
1517
|
-
let iteratorNextStmtIdx = -1;
|
|
1518
|
-
let dummyInitializerStmtIdx = -1;
|
|
1519
|
-
const stmtsCnt = stmts.length;
|
|
1520
|
-
for (let i = 0; i < stmtsCnt; i++) {
|
|
1521
|
-
const stmt = stmts[i];
|
|
1522
|
-
if (stmt instanceof Stmt_1.ArkAssignStmt && stmt.getRightOp() instanceof Expr_1.AbstractInvokeExpr) {
|
|
1523
|
-
const invokeExpr = stmt.getRightOp();
|
|
1524
|
-
if (invokeExpr.getMethodSignature().getMethodSubSignature()
|
|
1525
|
-
.getMethodName() === Builtin_1.Builtin.ITERATOR_NEXT) {
|
|
1526
|
-
iteratorNextStmtIdx = i;
|
|
1527
|
-
continue;
|
|
1528
|
-
}
|
|
1529
|
-
}
|
|
1530
|
-
if (stmt.toString() === ArkIRTransformer_1.ArkIRTransformer.DUMMY_LOOP_INITIALIZER_STMT) {
|
|
1531
|
-
dummyInitializerStmtIdx = i;
|
|
1532
|
-
continue;
|
|
1533
|
-
}
|
|
1534
|
-
if (stmt instanceof Stmt_1.ArkIfStmt) {
|
|
1535
|
-
ifStmtIdx = i;
|
|
1536
|
-
break;
|
|
1537
|
-
}
|
|
1538
|
-
}
|
|
1539
|
-
return {
|
|
1540
|
-
ifStmtIdx: ifStmtIdx,
|
|
1541
|
-
iteratorNextStmtIdx: iteratorNextStmtIdx,
|
|
1542
|
-
dummyInitializerStmtIdx: dummyInitializerStmtIdx,
|
|
1543
|
-
};
|
|
1544
|
-
}
|
|
1545
|
-
adjustIncrementorStmts(stmts, ifStmtIdx, currBlockBuilder, currBlockId, blockBuilderToCfgBlock, blocksContainLoopCondition, basicBlockSet) {
|
|
1546
|
-
const stmtsReenterCondition = stmts.slice(ifStmtIdx + 1);
|
|
1547
|
-
const blockBuildersReenterCondition = [];
|
|
1548
|
-
for (const prevBlockBuilder of currBlockBuilder.lasts) {
|
|
1549
|
-
const prevBlock = blockBuilderToCfgBlock.get(prevBlockBuilder);
|
|
1550
|
-
if (prevBlock.getId() > currBlockId) {
|
|
1551
|
-
blockBuildersReenterCondition.push(prevBlockBuilder);
|
|
1552
|
-
}
|
|
1553
|
-
}
|
|
1554
|
-
if (blockBuildersReenterCondition.length > 1 || blocksContainLoopCondition.has(blockBuildersReenterCondition[0])) {
|
|
1555
|
-
// put incrementor statements into an extra block
|
|
1556
|
-
this.insertBeforeConditionBlockBuilder(blockBuilderToCfgBlock, currBlockBuilder, stmtsReenterCondition, true, basicBlockSet);
|
|
1557
|
-
}
|
|
1558
|
-
else {
|
|
1559
|
-
// put incrementor statements into prev reenter block
|
|
1560
|
-
const blockReenterCondition = blockBuilderToCfgBlock.get(blockBuildersReenterCondition[0]);
|
|
1561
|
-
stmtsReenterCondition.forEach(stmt => blockReenterCondition === null || blockReenterCondition === void 0 ? void 0 : blockReenterCondition.getStmts().push(stmt));
|
|
1562
|
-
}
|
|
1563
|
-
}
|
|
1564
|
-
buildTraps(blockBuilderToCfgBlock, blockBuildersBeforeTry, arkIRTransformer, basicBlockSet) {
|
|
1565
|
-
var _a, _b, _c;
|
|
1566
|
-
const traps = [];
|
|
1567
|
-
for (const blockBuilderBeforeTry of blockBuildersBeforeTry) {
|
|
1568
|
-
if (blockBuilderBeforeTry.nexts.length === 0) {
|
|
1569
|
-
logger.error(`can't find try block.`);
|
|
1570
|
-
continue;
|
|
1571
|
-
}
|
|
1572
|
-
const blockBuilderContainTry = blockBuilderBeforeTry.nexts[0];
|
|
1573
|
-
const stmtsCnt = blockBuilderBeforeTry.stmts.length;
|
|
1574
|
-
const tryStmtBuilder = blockBuilderBeforeTry.stmts[stmtsCnt - 1];
|
|
1575
|
-
const finallyBlockBuilder = (_a = tryStmtBuilder.finallyStatement) === null || _a === void 0 ? void 0 : _a.block;
|
|
1576
|
-
if (!finallyBlockBuilder) {
|
|
1577
|
-
logger.error(`can't find finally block or dummy finally block.`);
|
|
1578
|
-
continue;
|
|
1579
|
-
}
|
|
1580
|
-
const { bfsBlocks: tryBfsBlocks, tailBlocks: tryTailBlocks } = this.getAllBlocksBFS(blockBuilderToCfgBlock, blockBuilderContainTry, finallyBlockBuilder);
|
|
1581
|
-
let catchBfsBlocks = [];
|
|
1582
|
-
let catchTailBlocks = [];
|
|
1583
|
-
const catchBlockBuilder = (_b = tryStmtBuilder.catchStatement) === null || _b === void 0 ? void 0 : _b.block;
|
|
1584
|
-
if (catchBlockBuilder) {
|
|
1585
|
-
({ bfsBlocks: catchBfsBlocks, tailBlocks: catchTailBlocks } = this.getAllBlocksBFS(blockBuilderToCfgBlock, catchBlockBuilder));
|
|
1586
|
-
}
|
|
1587
|
-
const finallyStmts = finallyBlockBuilder.stmts;
|
|
1588
|
-
const blockBuilderAfterFinally = (_c = tryStmtBuilder.afterFinal) === null || _c === void 0 ? void 0 : _c.block;
|
|
1589
|
-
if (!blockBuilderAfterFinally) {
|
|
1590
|
-
logger.error(`can't find block after try...catch.`);
|
|
1591
|
-
continue;
|
|
1592
|
-
}
|
|
1593
|
-
if (finallyStmts.length === 1 && finallyStmts[0].code === 'dummyFinally') { // no finally block
|
|
1594
|
-
const trapsIfNoFinally = this.buildTrapsIfNoFinally(tryBfsBlocks, tryTailBlocks, catchBfsBlocks, catchTailBlocks, finallyBlockBuilder, blockBuilderAfterFinally, basicBlockSet, blockBuilderToCfgBlock);
|
|
1595
|
-
if (trapsIfNoFinally) {
|
|
1596
|
-
traps.push(...trapsIfNoFinally);
|
|
1597
|
-
}
|
|
1598
|
-
}
|
|
1599
|
-
else {
|
|
1600
|
-
const trapsIfFinallyExist = this.buildTrapsIfFinallyExist(tryBfsBlocks, tryTailBlocks, catchBfsBlocks, catchTailBlocks, finallyBlockBuilder, blockBuilderAfterFinally, basicBlockSet, arkIRTransformer, blockBuilderToCfgBlock);
|
|
1601
|
-
traps.push(...trapsIfFinallyExist);
|
|
1602
|
-
}
|
|
1603
|
-
}
|
|
1604
|
-
return traps;
|
|
1605
|
-
}
|
|
1606
|
-
buildTrapsIfNoFinally(tryBfsBlocks, tryTailBlocks, catchBfsBlocks, catchTailBlocks, finallyBlockBuilder, blockBuilderAfterFinally, basicBlockSet, blockBuilderToCfgBlock) {
|
|
1607
|
-
if (catchBfsBlocks.length === 0) {
|
|
1608
|
-
logger.error(`catch block expected.`);
|
|
1609
|
-
return null;
|
|
1610
|
-
}
|
|
1611
|
-
if (!blockBuilderToCfgBlock.has(blockBuilderAfterFinally)) {
|
|
1612
|
-
logger.error(`can't find basicBlock corresponding to the blockBuilder.`);
|
|
1613
|
-
return null;
|
|
1614
|
-
}
|
|
1615
|
-
let blockAfterFinally = blockBuilderToCfgBlock.get(blockBuilderAfterFinally);
|
|
1616
|
-
if (!blockBuilderToCfgBlock.has(finallyBlockBuilder)) {
|
|
1617
|
-
logger.error(`can't find basicBlock corresponding to the blockBuilder.`);
|
|
1618
|
-
return null;
|
|
1619
|
-
}
|
|
1620
|
-
const finallyBlock = blockBuilderToCfgBlock.get(finallyBlockBuilder);
|
|
1621
|
-
let dummyFinallyIdxInPredecessors = -1;
|
|
1622
|
-
for (let i = 0; i < blockAfterFinally.getPredecessors().length; i++) {
|
|
1623
|
-
if (blockAfterFinally.getPredecessors()[i] === finallyBlock) {
|
|
1624
|
-
dummyFinallyIdxInPredecessors = i;
|
|
1625
|
-
break;
|
|
1626
|
-
}
|
|
1627
|
-
}
|
|
1628
|
-
if (dummyFinallyIdxInPredecessors === -1) {
|
|
1629
|
-
return null;
|
|
1630
|
-
}
|
|
1631
|
-
blockAfterFinally.getPredecessors().splice(dummyFinallyIdxInPredecessors, 1);
|
|
1632
|
-
for (const tryTailBlock of tryTailBlocks) {
|
|
1633
|
-
tryTailBlock.setSuccessorBlock(0, blockAfterFinally);
|
|
1634
|
-
blockAfterFinally.addPredecessorBlock(tryTailBlock);
|
|
1635
|
-
}
|
|
1636
|
-
basicBlockSet.delete(finallyBlock);
|
|
1637
|
-
for (const catchTailBlock of catchTailBlocks) {
|
|
1638
|
-
catchTailBlock.addSuccessorBlock(blockAfterFinally);
|
|
1639
|
-
blockAfterFinally.addPredecessorBlock(catchTailBlock);
|
|
1640
|
-
}
|
|
1641
|
-
for (const tryTailBlock of tryTailBlocks) {
|
|
1642
|
-
tryTailBlock.addExceptionalSuccessorBlock(catchBfsBlocks[0]);
|
|
1643
|
-
}
|
|
1644
|
-
return [new Trap_1.Trap(tryBfsBlocks, catchBfsBlocks)];
|
|
1645
|
-
}
|
|
1646
|
-
buildTrapsIfFinallyExist(tryBfsBlocks, tryTailBlocks, catchBfsBlocks, catchTailBlocks, finallyBlockBuilder, blockBuilderAfterFinally, basicBlockSet, arkIRTransformer, blockBuilderToCfgBlock) {
|
|
1647
|
-
const { bfsBlocks: finallyBfsBlocks, tailBlocks: finallyTailBlocks } = this.getAllBlocksBFS(blockBuilderToCfgBlock, finallyBlockBuilder, blockBuilderAfterFinally);
|
|
1648
|
-
const copyFinallyBfsBlocks = this.copyFinallyBlocks(finallyBfsBlocks, finallyTailBlocks, basicBlockSet, arkIRTransformer, blockBuilderToCfgBlock);
|
|
1649
|
-
const traps = [];
|
|
1650
|
-
if (catchBfsBlocks.length !== 0) {
|
|
1651
|
-
for (const catchTailBlock of catchTailBlocks) {
|
|
1652
|
-
catchTailBlock.addSuccessorBlock(finallyBfsBlocks[0]);
|
|
1653
|
-
finallyBfsBlocks[0].addPredecessorBlock(catchTailBlock);
|
|
1654
|
-
}
|
|
1655
|
-
// try -> catch trap
|
|
1656
|
-
for (const tryTailBlock of tryTailBlocks) {
|
|
1657
|
-
tryTailBlock.addExceptionalSuccessorBlock(catchBfsBlocks[0]);
|
|
1658
|
-
}
|
|
1659
|
-
traps.push(new Trap_1.Trap(tryBfsBlocks, catchBfsBlocks));
|
|
1660
|
-
// catch -> finally trap
|
|
1661
|
-
for (const catchTailBlock of catchTailBlocks) {
|
|
1662
|
-
catchTailBlock.addExceptionalSuccessorBlock(copyFinallyBfsBlocks[0]);
|
|
1663
|
-
}
|
|
1664
|
-
traps.push(new Trap_1.Trap(catchBfsBlocks, copyFinallyBfsBlocks));
|
|
1665
|
-
}
|
|
1666
|
-
else {
|
|
1667
|
-
// try -> finally trap
|
|
1668
|
-
for (const tryTailBlock of tryTailBlocks) {
|
|
1669
|
-
tryTailBlock.addExceptionalSuccessorBlock(copyFinallyBfsBlocks[0]);
|
|
1670
|
-
}
|
|
1671
|
-
traps.push(new Trap_1.Trap(tryBfsBlocks, copyFinallyBfsBlocks));
|
|
1672
|
-
}
|
|
1673
|
-
return traps;
|
|
1674
|
-
}
|
|
1675
|
-
getAllBlocksBFS(blockBuilderToCfgBlock, startBlockBuilder, endBlockBuilder) {
|
|
1676
|
-
const bfsBlocks = [];
|
|
1677
|
-
const tailBlocks = [];
|
|
1678
|
-
const queue = [];
|
|
1679
|
-
const visitedBlockBuilders = new Set();
|
|
1680
|
-
queue.push(startBlockBuilder);
|
|
1681
|
-
while (queue.length !== 0) {
|
|
1682
|
-
const currBlockBuilder = queue.splice(0, 1)[0];
|
|
1683
|
-
if (visitedBlockBuilders.has(currBlockBuilder)) {
|
|
1684
|
-
continue;
|
|
1685
|
-
}
|
|
1686
|
-
visitedBlockBuilders.add(currBlockBuilder);
|
|
1687
|
-
if (!blockBuilderToCfgBlock.has(currBlockBuilder)) {
|
|
1688
|
-
logger.error(`can't find basicBlock corresponding to the blockBuilder.`);
|
|
1689
|
-
continue;
|
|
1690
|
-
}
|
|
1691
|
-
const currBlock = blockBuilderToCfgBlock.get(currBlockBuilder);
|
|
1692
|
-
bfsBlocks.push(currBlock);
|
|
1693
|
-
const childList = currBlockBuilder.nexts;
|
|
1694
|
-
if (childList.length === 0 || (childList.length !== 0 && (childList[0] === endBlockBuilder))) {
|
|
1695
|
-
if (childList[0] === endBlockBuilder) {
|
|
1696
|
-
tailBlocks.push(currBlock);
|
|
1697
|
-
continue;
|
|
1698
|
-
}
|
|
1699
|
-
}
|
|
1700
|
-
if (childList.length !== 0) {
|
|
1701
|
-
for (const child of childList) {
|
|
1702
|
-
queue.push(child);
|
|
1703
|
-
}
|
|
1704
|
-
}
|
|
1705
|
-
}
|
|
1706
|
-
return { bfsBlocks, tailBlocks };
|
|
1707
|
-
}
|
|
1708
|
-
copyFinallyBlocks(finallyBfsBlocks, finallyTailBlocks, basicBlockSet, arkIRTransformer, blockBuilderToCfgBlock) {
|
|
1709
|
-
var _a;
|
|
1710
|
-
const copyFinallyBfsBlocks = this.copyBlocks(finallyBfsBlocks);
|
|
1711
|
-
const caughtExceptionRef = new Ref_1.ArkCaughtExceptionRef(Type_1.UnknownType.getInstance());
|
|
1712
|
-
const { value: exceptionValue, stmts: exceptionAssignStmts, } = arkIRTransformer.generateAssignStmtForValue(caughtExceptionRef, [Position_1.FullPosition.DEFAULT]);
|
|
1713
|
-
copyFinallyBfsBlocks[0].addHead(exceptionAssignStmts);
|
|
1714
|
-
const finallyPredecessorsCnt = copyFinallyBfsBlocks[0].getPredecessors().length;
|
|
1715
|
-
copyFinallyBfsBlocks[0].getPredecessors().splice(0, finallyPredecessorsCnt);
|
|
1716
|
-
const throwStmt = new Stmt_1.ArkThrowStmt(exceptionValue);
|
|
1717
|
-
let copyFinallyTailBlocks = copyFinallyBfsBlocks.splice(copyFinallyBfsBlocks.length - finallyTailBlocks.length, finallyTailBlocks.length);
|
|
1718
|
-
copyFinallyTailBlocks.forEach((copyFinallyTailBlock) => {
|
|
1719
|
-
const successorsCnt = copyFinallyTailBlock.getSuccessors().length;
|
|
1720
|
-
copyFinallyTailBlock.getSuccessors().splice(0, successorsCnt);
|
|
1721
|
-
});
|
|
1722
|
-
if (copyFinallyTailBlocks.length > 1) {
|
|
1723
|
-
const newCopyFinallyTailBlock = new BasicBlock_1.BasicBlock();
|
|
1724
|
-
copyFinallyTailBlocks.forEach((copyFinallyTailBlock) => {
|
|
1725
|
-
copyFinallyTailBlock.addSuccessorBlock(newCopyFinallyTailBlock);
|
|
1726
|
-
newCopyFinallyTailBlock.addPredecessorBlock(copyFinallyTailBlock);
|
|
1727
|
-
});
|
|
1728
|
-
copyFinallyTailBlocks = [newCopyFinallyTailBlock];
|
|
1729
|
-
}
|
|
1730
|
-
(_a = copyFinallyTailBlocks[0]) === null || _a === void 0 ? void 0 : _a.addStmt(throwStmt);
|
|
1731
|
-
copyFinallyBfsBlocks.push(...copyFinallyTailBlocks);
|
|
1732
|
-
copyFinallyBfsBlocks.forEach((copyFinallyBfsBlock) => {
|
|
1733
|
-
basicBlockSet.add(copyFinallyBfsBlock);
|
|
1734
|
-
});
|
|
1735
|
-
return copyFinallyBfsBlocks;
|
|
1736
|
-
}
|
|
1737
|
-
copyBlocks(sourceBlocks) {
|
|
1738
|
-
const sourceToTarget = new Map();
|
|
1739
|
-
const targetBlocks = [];
|
|
1740
|
-
for (const sourceBlock of sourceBlocks) {
|
|
1741
|
-
const targetBlock = new BasicBlock_1.BasicBlock();
|
|
1742
|
-
for (const stmt of sourceBlock.getStmts()) {
|
|
1743
|
-
targetBlock.addStmt(this.copyStmt(stmt));
|
|
1744
|
-
}
|
|
1745
|
-
sourceToTarget.set(sourceBlock, targetBlock);
|
|
1746
|
-
targetBlocks.push(targetBlock);
|
|
1747
|
-
}
|
|
1748
|
-
for (const sourceBlock of sourceBlocks) {
|
|
1749
|
-
const targetBlock = sourceToTarget.get(sourceBlock);
|
|
1750
|
-
for (const predecessor of sourceBlock.getPredecessors()) {
|
|
1751
|
-
const targetPredecessor = sourceToTarget.get(predecessor);
|
|
1752
|
-
targetBlock.addPredecessorBlock(targetPredecessor);
|
|
1753
|
-
}
|
|
1754
|
-
for (const successor of sourceBlock.getSuccessors()) {
|
|
1755
|
-
const targetSuccessor = sourceToTarget.get(successor);
|
|
1756
|
-
targetBlock.addSuccessorBlock(targetSuccessor);
|
|
1757
|
-
}
|
|
1758
|
-
}
|
|
1759
|
-
return targetBlocks;
|
|
1760
|
-
}
|
|
1761
|
-
copyStmt(sourceStmt) {
|
|
1762
|
-
if (sourceStmt instanceof Stmt_1.ArkAssignStmt) {
|
|
1763
|
-
return new Stmt_1.ArkAssignStmt(sourceStmt.getLeftOp(), sourceStmt.getRightOp());
|
|
1764
|
-
}
|
|
1765
|
-
else if (sourceStmt instanceof Stmt_1.ArkInvokeStmt) {
|
|
1766
|
-
return new Stmt_1.ArkInvokeStmt(sourceStmt.getInvokeExpr());
|
|
1767
|
-
}
|
|
1768
|
-
else if (sourceStmt instanceof Stmt_1.ArkIfStmt) {
|
|
1769
|
-
return new Stmt_1.ArkIfStmt(sourceStmt.getConditionExpr());
|
|
1770
|
-
}
|
|
1771
|
-
else if (sourceStmt instanceof Stmt_1.ArkReturnStmt) {
|
|
1772
|
-
return new Stmt_1.ArkReturnStmt(sourceStmt.getOp());
|
|
1773
|
-
}
|
|
1774
|
-
else if (sourceStmt instanceof Stmt_1.ArkReturnVoidStmt) {
|
|
1775
|
-
return new Stmt_1.ArkReturnVoidStmt();
|
|
1776
|
-
}
|
|
1777
|
-
else if (sourceStmt instanceof Stmt_1.ArkThrowStmt) {
|
|
1778
|
-
return new Stmt_1.ArkThrowStmt(sourceStmt.getOp());
|
|
1779
|
-
}
|
|
1780
|
-
return null;
|
|
1781
|
-
}
|
|
1782
|
-
buildSwitch(blockBuilderToCfgBlock, blockBuildersContainSwitch, valueAndStmtsOfSwitchAndCasesAll, arkIRTransformer, basicBlockSet) {
|
|
1783
|
-
for (let i = 0; i < blockBuildersContainSwitch.length; i++) {
|
|
1784
|
-
const blockBuilderContainSwitch = blockBuildersContainSwitch[i];
|
|
1785
|
-
if (!blockBuilderToCfgBlock.has(blockBuilderContainSwitch)) {
|
|
1786
|
-
logger.error(`can't find basicBlock corresponding to the blockBuilder.`);
|
|
1787
|
-
continue;
|
|
1788
|
-
}
|
|
1789
|
-
const blockContainSwitch = blockBuilderToCfgBlock.get(blockBuilderContainSwitch);
|
|
1790
|
-
const valueAndStmtsOfSwitch = valueAndStmtsOfSwitchAndCasesAll[i][0];
|
|
1791
|
-
const stmtsOfSwitch = valueAndStmtsOfSwitch.stmts;
|
|
1792
|
-
stmtsOfSwitch.forEach((stmt) => {
|
|
1793
|
-
blockContainSwitch.addStmt(stmt);
|
|
1794
|
-
});
|
|
1795
|
-
const stmtsCnt = blockBuilderContainSwitch.stmts.length;
|
|
1796
|
-
const switchStmtBuilder = blockBuilderContainSwitch.stmts[stmtsCnt - 1];
|
|
1797
|
-
const cases = switchStmtBuilder.cases;
|
|
1798
|
-
let nonEmptyCaseCnt = 0;
|
|
1799
|
-
for (const currCase of cases) {
|
|
1800
|
-
if (currCase.stmt.block) { // there are stmts after this case
|
|
1801
|
-
nonEmptyCaseCnt++;
|
|
1802
|
-
}
|
|
1803
|
-
}
|
|
1804
|
-
if (nonEmptyCaseCnt === 0) {
|
|
1805
|
-
continue;
|
|
1806
|
-
}
|
|
1807
|
-
const caseCnt = cases.length;
|
|
1808
|
-
const caseIfBlocks = this.generateIfBlocksForCases(valueAndStmtsOfSwitchAndCasesAll[i], caseCnt, blockContainSwitch, basicBlockSet, arkIRTransformer);
|
|
1809
|
-
this.linkIfBlockAndCaseBlock(blockContainSwitch, caseIfBlocks, switchStmtBuilder, blockBuilderToCfgBlock);
|
|
1810
|
-
}
|
|
1811
|
-
}
|
|
1812
|
-
generateIfBlocksForCases(valueAndStmtsOfSwitchAndCases, caseCnt, blockContainSwitch, basicBlockSet, arkIRTransformer) {
|
|
1813
|
-
const valueAndStmtsOfSwitch = valueAndStmtsOfSwitchAndCases[0];
|
|
1814
|
-
const valueOfSwitch = valueAndStmtsOfSwitch.value;
|
|
1815
|
-
const caseIfBlocks = [];
|
|
1816
|
-
for (let j = 0; j < caseCnt; j++) {
|
|
1817
|
-
let caseIfBlock;
|
|
1818
|
-
if (j === 0) {
|
|
1819
|
-
caseIfBlock = blockContainSwitch;
|
|
1820
|
-
}
|
|
1821
|
-
else {
|
|
1822
|
-
caseIfBlock = new BasicBlock_1.BasicBlock();
|
|
1823
|
-
basicBlockSet.add(caseIfBlock);
|
|
1824
|
-
}
|
|
1825
|
-
caseIfBlocks.push(caseIfBlock);
|
|
1826
|
-
const caseValueAndStmts = valueAndStmtsOfSwitchAndCases[j + 1];
|
|
1827
|
-
const caseValue = caseValueAndStmts.value;
|
|
1828
|
-
const caseStmts = caseValueAndStmts.stmts;
|
|
1829
|
-
caseStmts.forEach((stmt) => {
|
|
1830
|
-
caseIfBlock.addStmt(stmt);
|
|
1831
|
-
});
|
|
1832
|
-
const caseIfStmts = arkIRTransformer.generateIfStmtForValues(valueOfSwitch, valueAndStmtsOfSwitch.valueOriginalPositions, caseValue, caseValueAndStmts.valueOriginalPositions);
|
|
1833
|
-
caseIfStmts.forEach((stmt) => {
|
|
1834
|
-
caseIfBlock.addStmt(stmt);
|
|
1835
|
-
});
|
|
1836
|
-
}
|
|
1837
|
-
return caseIfBlocks;
|
|
1838
|
-
}
|
|
1839
|
-
linkIfBlockAndCaseBlock(blockContainSwitch, caseIfBlocks, switchStmtBuilder, blockBuilderToCfgBlock) {
|
|
1840
|
-
const successorsOfBlockContainSwitch = Array.from(blockContainSwitch.getSuccessors());
|
|
1841
|
-
const expectedSuccessorsOfCaseIfBlock = [];
|
|
1842
|
-
const defaultStmtBuilder = switchStmtBuilder.default;
|
|
1843
|
-
if (defaultStmtBuilder && defaultStmtBuilder.block) {
|
|
1844
|
-
expectedSuccessorsOfCaseIfBlock.push(...successorsOfBlockContainSwitch.splice(-1, 1));
|
|
1845
|
-
}
|
|
1846
|
-
else {
|
|
1847
|
-
const afterSwitchStmtBuilder = switchStmtBuilder.afterSwitch;
|
|
1848
|
-
const afterSwitchBlockBuilder = afterSwitchStmtBuilder === null || afterSwitchStmtBuilder === void 0 ? void 0 : afterSwitchStmtBuilder.block;
|
|
1849
|
-
if (!afterSwitchBlockBuilder || !blockBuilderToCfgBlock.has(afterSwitchBlockBuilder)) {
|
|
1850
|
-
logger.error(`can't find basicBlock corresponding to the blockBuilder.`);
|
|
1851
|
-
return false;
|
|
1852
|
-
}
|
|
1853
|
-
expectedSuccessorsOfCaseIfBlock.push(blockBuilderToCfgBlock.get(afterSwitchBlockBuilder));
|
|
1854
|
-
}
|
|
1855
|
-
const caseCnt = switchStmtBuilder.cases.length;
|
|
1856
|
-
for (let i = caseCnt - 1; i >= 0; i--) {
|
|
1857
|
-
const currCase = switchStmtBuilder.cases[i];
|
|
1858
|
-
if (currCase.stmt.block) {
|
|
1859
|
-
expectedSuccessorsOfCaseIfBlock.push(...successorsOfBlockContainSwitch.splice(-1, 1));
|
|
1860
|
-
}
|
|
1861
|
-
else { // if there are no stmts after this case, reuse the successor of the next case
|
|
1862
|
-
expectedSuccessorsOfCaseIfBlock.push(...expectedSuccessorsOfCaseIfBlock.slice(-1));
|
|
1863
|
-
}
|
|
1864
|
-
}
|
|
1865
|
-
expectedSuccessorsOfCaseIfBlock.reverse();
|
|
1866
|
-
blockContainSwitch.getSuccessors().forEach((successor) => {
|
|
1867
|
-
successor.getPredecessors().splice(0, 1);
|
|
1868
|
-
});
|
|
1869
|
-
blockContainSwitch.getSuccessors().splice(0);
|
|
1870
|
-
for (let j = 0; j < caseCnt; j++) {
|
|
1871
|
-
const caseIfBlock = caseIfBlocks[j];
|
|
1872
|
-
caseIfBlock.addSuccessorBlock(expectedSuccessorsOfCaseIfBlock[j]);
|
|
1873
|
-
expectedSuccessorsOfCaseIfBlock[j].addPredecessorBlock(caseIfBlock);
|
|
1874
|
-
if (j === caseCnt - 1) { // the false branch of last case should be default or block after switch statement
|
|
1875
|
-
caseIfBlock.addSuccessorBlock(expectedSuccessorsOfCaseIfBlock[j + 1]);
|
|
1876
|
-
expectedSuccessorsOfCaseIfBlock[j + 1].addPredecessorBlock(caseIfBlock);
|
|
1877
|
-
}
|
|
1878
|
-
else {
|
|
1879
|
-
caseIfBlock.addSuccessorBlock(caseIfBlocks[j + 1]);
|
|
1880
|
-
caseIfBlocks[j + 1].addPredecessorBlock(caseIfBlock);
|
|
1881
|
-
}
|
|
1882
|
-
}
|
|
1883
|
-
return true;
|
|
1884
|
-
}
|
|
1885
|
-
}
|
|
1886
|
-
exports.CfgBuilder = CfgBuilder;
|