arkanalyzer 1.0.20 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (279) hide show
  1. package/lib/Config.d.ts +70 -70
  2. package/lib/Config.js +198 -198
  3. package/lib/Scene.d.ts +316 -316
  4. package/lib/Scene.js +1316 -1316
  5. package/lib/callgraph/algorithm/AbstractAnalysis.d.ts +28 -28
  6. package/lib/callgraph/algorithm/AbstractAnalysis.js +144 -144
  7. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.d.ts +10 -10
  8. package/lib/callgraph/algorithm/ClassHierarchyAnalysis.js +69 -69
  9. package/lib/callgraph/algorithm/RapidTypeAnalysis.d.ts +15 -15
  10. package/lib/callgraph/algorithm/RapidTypeAnalysis.js +146 -146
  11. package/lib/callgraph/common/Statistics.d.ts +57 -57
  12. package/lib/callgraph/common/Statistics.js +228 -228
  13. package/lib/callgraph/model/CallGraph.d.ts +96 -96
  14. package/lib/callgraph/model/CallGraph.js +354 -354
  15. package/lib/callgraph/model/builder/CallGraphBuilder.d.ts +15 -15
  16. package/lib/callgraph/model/builder/CallGraphBuilder.js +106 -106
  17. package/lib/callgraph/pointerAnalysis/Context.d.ts +37 -37
  18. package/lib/callgraph/pointerAnalysis/Context.js +155 -155
  19. package/lib/callgraph/pointerAnalysis/DummyCallCreator.d.ts +22 -22
  20. package/lib/callgraph/pointerAnalysis/DummyCallCreator.js +104 -104
  21. package/lib/callgraph/pointerAnalysis/PTAUtils.d.ts +5 -5
  22. package/lib/callgraph/pointerAnalysis/PTAUtils.js +46 -46
  23. package/lib/callgraph/pointerAnalysis/Pag.d.ts +261 -261
  24. package/lib/callgraph/pointerAnalysis/Pag.js +930 -930
  25. package/lib/callgraph/pointerAnalysis/PagBuilder.d.ts +157 -157
  26. package/lib/callgraph/pointerAnalysis/PagBuilder.js +1399 -1399
  27. package/lib/callgraph/pointerAnalysis/PointerAnalysis.d.ts +72 -72
  28. package/lib/callgraph/pointerAnalysis/PointerAnalysis.js +573 -573
  29. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.d.ts +15 -15
  30. package/lib/callgraph/pointerAnalysis/PointerAnalysisConfig.js +81 -81
  31. package/lib/callgraph/pointerAnalysis/PtsDS.d.ts +80 -80
  32. package/lib/callgraph/pointerAnalysis/PtsDS.js +308 -308
  33. package/lib/core/base/Constant.d.ts +48 -48
  34. package/lib/core/base/Constant.js +103 -103
  35. package/lib/core/base/Decorator.d.ts +15 -15
  36. package/lib/core/base/Decorator.js +46 -46
  37. package/lib/core/base/DefUseChain.d.ts +8 -8
  38. package/lib/core/base/DefUseChain.js +25 -25
  39. package/lib/core/base/Expr.d.ts +340 -340
  40. package/lib/core/base/Expr.js +917 -917
  41. package/lib/core/base/Local.d.ts +104 -104
  42. package/lib/core/base/Local.js +176 -176
  43. package/lib/core/base/Position.d.ts +30 -30
  44. package/lib/core/base/Position.js +147 -147
  45. package/lib/core/base/Ref.d.ts +173 -173
  46. package/lib/core/base/Ref.js +374 -374
  47. package/lib/core/base/Stmt.d.ts +237 -237
  48. package/lib/core/base/Stmt.js +525 -525
  49. package/lib/core/base/Trap.d.ts +8 -8
  50. package/lib/core/base/Trap.js +30 -30
  51. package/lib/core/base/Type.d.ts +308 -308
  52. package/lib/core/base/Type.js +639 -639
  53. package/lib/core/base/TypeExpr.d.ts +71 -71
  54. package/lib/core/base/TypeExpr.js +155 -155
  55. package/lib/core/base/Value.d.ts +32 -32
  56. package/lib/core/base/Value.js +16 -16
  57. package/lib/core/common/ArkError.d.ts +14 -14
  58. package/lib/core/common/ArkError.js +28 -28
  59. package/lib/core/common/ArkIRTransformer.d.ts +67 -67
  60. package/lib/core/common/ArkIRTransformer.js +690 -690
  61. package/lib/core/common/ArkValueTransformer.d.ts +89 -89
  62. package/lib/core/common/ArkValueTransformer.js +1478 -1478
  63. package/lib/core/common/Builtin.d.ts +36 -36
  64. package/lib/core/common/Builtin.js +81 -81
  65. package/lib/core/common/Const.d.ts +21 -21
  66. package/lib/core/common/Const.js +43 -43
  67. package/lib/core/common/DummyMainCreater.d.ts +46 -46
  68. package/lib/core/common/DummyMainCreater.js +319 -319
  69. package/lib/core/common/EtsConst.d.ts +73 -73
  70. package/lib/core/common/EtsConst.js +1021 -1021
  71. package/lib/core/common/ExprUseReplacer.d.ts +21 -21
  72. package/lib/core/common/ExprUseReplacer.js +124 -124
  73. package/lib/core/common/IRInference.d.ts +36 -36
  74. package/lib/core/common/IRInference.d.ts.map +1 -1
  75. package/lib/core/common/IRInference.js +651 -648
  76. package/lib/core/common/IRUtils.d.ts +18 -18
  77. package/lib/core/common/IRUtils.js +141 -141
  78. package/lib/core/common/ModelUtils.d.ts +70 -70
  79. package/lib/core/common/ModelUtils.js +712 -712
  80. package/lib/core/common/RefUseReplacer.d.ts +13 -13
  81. package/lib/core/common/RefUseReplacer.js +52 -52
  82. package/lib/core/common/SdkUtils.d.ts +8 -8
  83. package/lib/core/common/SdkUtils.js +105 -105
  84. package/lib/core/common/StmtDefReplacer.d.ts +12 -12
  85. package/lib/core/common/StmtDefReplacer.js +41 -41
  86. package/lib/core/common/StmtUseReplacer.d.ts +16 -16
  87. package/lib/core/common/StmtUseReplacer.js +108 -108
  88. package/lib/core/common/TSConst.d.ts +18 -18
  89. package/lib/core/common/TSConst.js +36 -36
  90. package/lib/core/common/TypeInference.d.ts +102 -102
  91. package/lib/core/common/TypeInference.js +748 -748
  92. package/lib/core/common/ValueUtil.d.ts +12 -12
  93. package/lib/core/common/ValueUtil.js +54 -54
  94. package/lib/core/common/VisibleValue.d.ts +36 -36
  95. package/lib/core/common/VisibleValue.js +212 -212
  96. package/lib/core/dataflow/DataflowProblem.d.ts +20 -20
  97. package/lib/core/dataflow/DataflowProblem.js +20 -20
  98. package/lib/core/dataflow/DataflowResult.d.ts +7 -7
  99. package/lib/core/dataflow/DataflowResult.js +26 -26
  100. package/lib/core/dataflow/DataflowSolver.d.ts +43 -43
  101. package/lib/core/dataflow/DataflowSolver.js +307 -307
  102. package/lib/core/dataflow/Edge.d.ts +11 -11
  103. package/lib/core/dataflow/Edge.js +31 -31
  104. package/lib/core/dataflow/Fact.d.ts +6 -6
  105. package/lib/core/dataflow/Fact.js +24 -24
  106. package/lib/core/dataflow/GenericDataFlow.d.ts +142 -142
  107. package/lib/core/dataflow/GenericDataFlow.js +109 -109
  108. package/lib/core/dataflow/ReachingDef.d.ts +62 -62
  109. package/lib/core/dataflow/ReachingDef.js +168 -168
  110. package/lib/core/dataflow/UndefinedVariable.d.ts +45 -45
  111. package/lib/core/dataflow/UndefinedVariable.js +288 -288
  112. package/lib/core/dataflow/Util.d.ts +9 -9
  113. package/lib/core/dataflow/Util.js +64 -64
  114. package/lib/core/graph/BaseExplicitGraph.d.ts +58 -58
  115. package/lib/core/graph/BaseExplicitGraph.js +157 -157
  116. package/lib/core/graph/BaseImplicitGraph.d.ts +77 -77
  117. package/lib/core/graph/BaseImplicitGraph.js +78 -78
  118. package/lib/core/graph/BasicBlock.d.ts +104 -104
  119. package/lib/core/graph/BasicBlock.js +280 -280
  120. package/lib/core/graph/Cfg.d.ts +59 -59
  121. package/lib/core/graph/Cfg.js +282 -282
  122. package/lib/core/graph/DependsGraph.d.ts +35 -35
  123. package/lib/core/graph/DependsGraph.js +87 -87
  124. package/lib/core/graph/DominanceFinder.d.ts +15 -15
  125. package/lib/core/graph/DominanceFinder.js +121 -121
  126. package/lib/core/graph/DominanceTree.d.ts +12 -12
  127. package/lib/core/graph/DominanceTree.js +70 -70
  128. package/lib/core/graph/GraphTraits.d.ts +7 -7
  129. package/lib/core/graph/GraphTraits.js +16 -16
  130. package/lib/core/graph/Scc.d.ts +61 -61
  131. package/lib/core/graph/Scc.js +222 -222
  132. package/lib/core/graph/ViewTree.d.ts +114 -114
  133. package/lib/core/graph/ViewTree.js +16 -16
  134. package/lib/core/graph/builder/CfgBuilder.d.ts +176 -176
  135. package/lib/core/graph/builder/CfgBuilder.js +1139 -1139
  136. package/lib/core/graph/builder/ConditionBuilder.d.ts +15 -15
  137. package/lib/core/graph/builder/ConditionBuilder.js +252 -252
  138. package/lib/core/graph/builder/LoopBuilder.d.ts +20 -20
  139. package/lib/core/graph/builder/LoopBuilder.js +251 -251
  140. package/lib/core/graph/builder/SwitchBuilder.d.ts +11 -11
  141. package/lib/core/graph/builder/SwitchBuilder.js +152 -152
  142. package/lib/core/graph/builder/TrapBuilder.d.ts +16 -16
  143. package/lib/core/graph/builder/TrapBuilder.js +272 -272
  144. package/lib/core/graph/builder/ViewTreeBuilder.d.ts +203 -203
  145. package/lib/core/graph/builder/ViewTreeBuilder.js +1055 -1055
  146. package/lib/core/model/ArkBaseModel.d.ts +59 -59
  147. package/lib/core/model/ArkBaseModel.js +274 -274
  148. package/lib/core/model/ArkBody.d.ts +24 -24
  149. package/lib/core/model/ArkBody.js +65 -65
  150. package/lib/core/model/ArkClass.d.ts +206 -206
  151. package/lib/core/model/ArkClass.js +439 -439
  152. package/lib/core/model/ArkExport.d.ts +72 -72
  153. package/lib/core/model/ArkExport.js +147 -147
  154. package/lib/core/model/ArkField.d.ts +62 -62
  155. package/lib/core/model/ArkField.js +113 -113
  156. package/lib/core/model/ArkFile.d.ts +131 -131
  157. package/lib/core/model/ArkFile.js +267 -267
  158. package/lib/core/model/ArkImport.d.ts +44 -44
  159. package/lib/core/model/ArkImport.js +108 -108
  160. package/lib/core/model/ArkMetadata.d.ts +31 -31
  161. package/lib/core/model/ArkMetadata.js +55 -55
  162. package/lib/core/model/ArkMethod.d.ts +261 -261
  163. package/lib/core/model/ArkMethod.js +592 -592
  164. package/lib/core/model/ArkNamespace.d.ts +66 -66
  165. package/lib/core/model/ArkNamespace.js +209 -209
  166. package/lib/core/model/ArkSignature.d.ts +162 -162
  167. package/lib/core/model/ArkSignature.js +379 -379
  168. package/lib/core/model/builder/ArkClassBuilder.d.ts +12 -12
  169. package/lib/core/model/builder/ArkClassBuilder.js +454 -454
  170. package/lib/core/model/builder/ArkExportBuilder.d.ts +24 -24
  171. package/lib/core/model/builder/ArkExportBuilder.js +210 -210
  172. package/lib/core/model/builder/ArkFieldBuilder.d.ts +8 -8
  173. package/lib/core/model/builder/ArkFieldBuilder.js +193 -193
  174. package/lib/core/model/builder/ArkFileBuilder.d.ts +9 -9
  175. package/lib/core/model/builder/ArkFileBuilder.js +165 -165
  176. package/lib/core/model/builder/ArkImportBuilder.d.ts +4 -4
  177. package/lib/core/model/builder/ArkImportBuilder.js +128 -128
  178. package/lib/core/model/builder/ArkMethodBuilder.d.ts +63 -63
  179. package/lib/core/model/builder/ArkMethodBuilder.js +486 -486
  180. package/lib/core/model/builder/ArkNamespaceBuilder.d.ts +5 -5
  181. package/lib/core/model/builder/ArkNamespaceBuilder.js +205 -205
  182. package/lib/core/model/builder/ArkSignatureBuilder.d.ts +8 -8
  183. package/lib/core/model/builder/ArkSignatureBuilder.js +40 -40
  184. package/lib/core/model/builder/BodyBuilder.d.ts +55 -30
  185. package/lib/core/model/builder/BodyBuilder.d.ts.map +1 -1
  186. package/lib/core/model/builder/BodyBuilder.js +542 -371
  187. package/lib/core/model/builder/builderUtils.d.ts +18 -18
  188. package/lib/core/model/builder/builderUtils.js +558 -558
  189. package/lib/index.d.ts +82 -80
  190. package/lib/index.d.ts.map +1 -1
  191. package/lib/index.js +201 -196
  192. package/lib/save/ArkStream.d.ts +23 -23
  193. package/lib/save/ArkStream.js +83 -83
  194. package/lib/save/DotPrinter.d.ts +43 -43
  195. package/lib/save/DotPrinter.js +170 -170
  196. package/lib/save/GraphPrinter.d.ts +16 -16
  197. package/lib/save/GraphPrinter.js +134 -134
  198. package/lib/save/JsonPrinter.d.ts +30 -30
  199. package/lib/save/JsonPrinter.js +580 -580
  200. package/lib/save/Printer.d.ts +12 -12
  201. package/lib/save/Printer.js +27 -27
  202. package/lib/save/PrinterBuilder.d.ts +53 -53
  203. package/lib/save/PrinterBuilder.js +145 -145
  204. package/lib/save/ViewTreePrinter.d.ts +14 -14
  205. package/lib/save/ViewTreePrinter.js +123 -123
  206. package/lib/save/arkir/ArkIRClassPrinter.d.ts +13 -13
  207. package/lib/save/arkir/ArkIRClassPrinter.js +92 -92
  208. package/lib/save/arkir/ArkIRFieldPrinter.d.ts +11 -11
  209. package/lib/save/arkir/ArkIRFieldPrinter.js +64 -64
  210. package/lib/save/arkir/ArkIRFilePrinter.d.ts +12 -12
  211. package/lib/save/arkir/ArkIRFilePrinter.js +54 -54
  212. package/lib/save/arkir/ArkIRMethodPrinter.d.ts +16 -16
  213. package/lib/save/arkir/ArkIRMethodPrinter.js +159 -159
  214. package/lib/save/arkir/ArkIRNamespacePrinter.d.ts +11 -11
  215. package/lib/save/arkir/ArkIRNamespacePrinter.js +66 -66
  216. package/lib/save/base/BasePrinter.d.ts +23 -23
  217. package/lib/save/base/BasePrinter.js +70 -70
  218. package/lib/save/base/ExportPrinter.d.ts +8 -8
  219. package/lib/save/base/ExportPrinter.js +67 -67
  220. package/lib/save/base/ImportPrinter.d.ts +9 -9
  221. package/lib/save/base/ImportPrinter.js +92 -92
  222. package/lib/save/base/PrinterUtils.d.ts +24 -24
  223. package/lib/save/base/PrinterUtils.js +208 -208
  224. package/lib/save/serializeArkIR.d.ts +8 -8
  225. package/lib/save/serializeArkIR.js +294 -294
  226. package/lib/save/source/SourceBase.d.ts +22 -22
  227. package/lib/save/source/SourceBase.js +64 -64
  228. package/lib/save/source/SourceBody.d.ts +58 -58
  229. package/lib/save/source/SourceBody.js +296 -296
  230. package/lib/save/source/SourceClass.d.ts +25 -25
  231. package/lib/save/source/SourceClass.js +187 -187
  232. package/lib/save/source/SourceField.d.ts +13 -13
  233. package/lib/save/source/SourceField.js +73 -73
  234. package/lib/save/source/SourceFilePrinter.d.ts +12 -12
  235. package/lib/save/source/SourceFilePrinter.js +69 -69
  236. package/lib/save/source/SourceMethod.d.ts +22 -22
  237. package/lib/save/source/SourceMethod.d.ts.map +1 -1
  238. package/lib/save/source/SourceMethod.js +196 -193
  239. package/lib/save/source/SourceNamespace.d.ts +11 -11
  240. package/lib/save/source/SourceNamespace.js +83 -83
  241. package/lib/save/source/SourceStmt.d.ts +178 -178
  242. package/lib/save/source/SourceStmt.js +838 -838
  243. package/lib/save/source/SourceTransformer.d.ts +46 -46
  244. package/lib/save/source/SourceTransformer.js +446 -446
  245. package/lib/transformer/FunctionTransformer.d.ts +2 -2
  246. package/lib/transformer/FunctionTransformer.js +17 -17
  247. package/lib/transformer/SceneTransformer.d.ts +2 -2
  248. package/lib/transformer/SceneTransformer.js +17 -17
  249. package/lib/transformer/StaticSingleAssignmentFormer.d.ts +12 -12
  250. package/lib/transformer/StaticSingleAssignmentFormer.js +259 -259
  251. package/lib/transformer/Transformer.d.ts +6 -6
  252. package/lib/transformer/Transformer.js +22 -22
  253. package/lib/utils/AstTreeUtils.d.ts +4 -4
  254. package/lib/utils/AstTreeUtils.js +26 -26
  255. package/lib/utils/CfgStructualAnalysis.d.ts +110 -110
  256. package/lib/utils/CfgStructualAnalysis.js +1277 -1277
  257. package/lib/utils/FileUtils.d.ts +18 -18
  258. package/lib/utils/FileUtils.js +135 -135
  259. package/lib/utils/SparseBitVector.d.ts +100 -100
  260. package/lib/utils/SparseBitVector.js +445 -445
  261. package/lib/utils/callGraphUtils.d.ts +30 -30
  262. package/lib/utils/callGraphUtils.js +205 -205
  263. package/lib/utils/crypto_utils.d.ts +5 -5
  264. package/lib/utils/crypto_utils.js +57 -57
  265. package/lib/utils/entryMethodUtils.d.ts +13 -13
  266. package/lib/utils/entryMethodUtils.js +110 -110
  267. package/lib/utils/getAllFiles.d.ts +9 -9
  268. package/lib/utils/getAllFiles.js +90 -90
  269. package/lib/utils/json5parser.d.ts +6 -6
  270. package/lib/utils/json5parser.js +146 -146
  271. package/lib/utils/logger.d.ts +18 -18
  272. package/lib/utils/logger.d.ts.map +1 -1
  273. package/lib/utils/logger.js +97 -90
  274. package/lib/utils/pathTransfer.d.ts +1 -1
  275. package/lib/utils/pathTransfer.js +25 -25
  276. package/node_modules/json5/lib/cli.js +0 -0
  277. package/node_modules/ohos-typescript/bin/tsc +0 -0
  278. package/node_modules/ohos-typescript/bin/tsserver +0 -0
  279. package/package.json +3 -3
@@ -1,748 +1,748 @@
1
- "use strict";
2
- /*
3
- * Copyright (c) 2024 Huawei Device Co., Ltd.
4
- * Licensed under the Apache License, Version 2.0 (the "License");
5
- * you may not use this file except in compliance with the License.
6
- * You may obtain a copy of the License at
7
- *
8
- * http://www.apache.org/licenses/LICENSE-2.0
9
- *
10
- * Unless required by applicable law or agreed to in writing, software
11
- * distributed under the License is distributed on an "AS IS" BASIS,
12
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- * See the License for the specific language governing permissions and
14
- * limitations under the License.
15
- */
16
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
17
- if (k2 === undefined) k2 = k;
18
- var desc = Object.getOwnPropertyDescriptor(m, k);
19
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
20
- desc = { enumerable: true, get: function() { return m[k]; } };
21
- }
22
- Object.defineProperty(o, k2, desc);
23
- }) : (function(o, m, k, k2) {
24
- if (k2 === undefined) k2 = k;
25
- o[k2] = m[k];
26
- }));
27
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
28
- Object.defineProperty(o, "default", { enumerable: true, value: v });
29
- }) : function(o, v) {
30
- o["default"] = v;
31
- });
32
- var __importStar = (this && this.__importStar) || function (mod) {
33
- if (mod && mod.__esModule) return mod;
34
- var result = {};
35
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
36
- __setModuleDefault(result, mod);
37
- return result;
38
- };
39
- Object.defineProperty(exports, "__esModule", { value: true });
40
- exports.TypeInference = void 0;
41
- const logger_1 = __importStar(require("../../utils/logger"));
42
- const Expr_1 = require("../base/Expr");
43
- const Local_1 = require("../base/Local");
44
- const Ref_1 = require("../base/Ref");
45
- const Stmt_1 = require("../base/Stmt");
46
- const Type_1 = require("../base/Type");
47
- const ArkMethod_1 = require("../model/ArkMethod");
48
- const ArkClass_1 = require("../model/ArkClass");
49
- const ArkField_1 = require("../model/ArkField");
50
- const Constant_1 = require("../base/Constant");
51
- const ArkNamespace_1 = require("../model/ArkNamespace");
52
- const TSConst_1 = require("./TSConst");
53
- const ModelUtils_1 = require("./ModelUtils");
54
- const Builtin_1 = require("./Builtin");
55
- const Const_1 = require("./Const");
56
- const ValueUtil_1 = require("./ValueUtil");
57
- const ArkImport_1 = require("../model/ArkImport");
58
- const IRInference_1 = require("./IRInference");
59
- const TypeExpr_1 = require("../base/TypeExpr");
60
- const logger = logger_1.default.getLogger(logger_1.LOG_MODULE_TYPE.ARKANALYZER, 'TypeInference');
61
- class TypeInference {
62
- static inferTypeInArkField(arkField) {
63
- var _a;
64
- const arkClass = arkField.getDeclaringArkClass();
65
- const stmts = arkField.getInitializer();
66
- const method = (_a = arkClass.getMethodWithName(Const_1.INSTANCE_INIT_METHOD_NAME)) !== null && _a !== void 0 ? _a : arkClass.getMethodWithName(TSConst_1.CONSTRUCTOR_NAME);
67
- for (const stmt of stmts) {
68
- if (method) {
69
- this.resolveTypeExprsInStmt(stmt, method);
70
- this.resolveExprsInStmt(stmt, method);
71
- this.resolveFieldRefsInStmt(stmt, method);
72
- this.resolveArkAssignStmt(stmt, method);
73
- }
74
- }
75
- const beforeType = arkField.getType();
76
- if (!this.isUnclearType(beforeType)) {
77
- return;
78
- }
79
- let rightType;
80
- let fieldRef;
81
- const lastStmt = stmts[stmts.length - 1];
82
- if (lastStmt instanceof Stmt_1.ArkAssignStmt) {
83
- rightType = lastStmt.getRightOp().getType();
84
- if (lastStmt.getLeftOp() instanceof Ref_1.AbstractFieldRef) {
85
- fieldRef = lastStmt.getLeftOp();
86
- }
87
- }
88
- let fieldType;
89
- if (beforeType) {
90
- fieldType = this.inferUnclearedType(beforeType, arkClass, rightType);
91
- }
92
- if (fieldType) {
93
- arkField.getSignature().setType(fieldType);
94
- fieldRef === null || fieldRef === void 0 ? void 0 : fieldRef.setFieldSignature(arkField.getSignature());
95
- }
96
- else if (rightType && this.isUnclearType(beforeType) && !this.isUnclearType(rightType)) {
97
- arkField.getSignature().setType(rightType);
98
- fieldRef === null || fieldRef === void 0 ? void 0 : fieldRef.setFieldSignature(arkField.getSignature());
99
- }
100
- }
101
- /**
102
- * Infer type for a given unclear type.
103
- * It returns an array with 2 items, original object and original type.
104
- * The original object is null if there is no object, or it failed to find the object.
105
- * The original type is null if failed to infer the type.
106
- * @param leftOpType
107
- * @param declaringArkClass
108
- * @param [rightType]
109
- * @returns
110
- */
111
- static inferUnclearedType(leftOpType, declaringArkClass, rightType) {
112
- let type;
113
- if (leftOpType instanceof Type_1.ClassType &&
114
- leftOpType.getClassSignature().getDeclaringFileSignature().getFileName() === Const_1.UNKNOWN_FILE_NAME) {
115
- type = TypeInference.inferUnclearRefName(leftOpType.getClassSignature().getClassName(), declaringArkClass);
116
- }
117
- else if (leftOpType instanceof Type_1.UnionType || leftOpType instanceof Type_1.IntersectionType || leftOpType instanceof Type_1.TupleType) {
118
- let types = leftOpType.getTypes();
119
- for (let i = 0; i < types.length; i++) {
120
- let newType = this.inferUnclearedType(types[i], declaringArkClass);
121
- if (newType) {
122
- types[i] = newType;
123
- }
124
- }
125
- type = leftOpType;
126
- }
127
- else if (leftOpType instanceof Type_1.ArrayType) {
128
- let baseType = this.inferUnclearedType(leftOpType.getBaseType(), declaringArkClass);
129
- if (baseType) {
130
- leftOpType.setBaseType(baseType);
131
- type = leftOpType;
132
- }
133
- }
134
- else if (leftOpType instanceof Type_1.AliasType) {
135
- let baseType = this.inferUnclearedType(leftOpType.getOriginalType(), declaringArkClass);
136
- if (baseType) {
137
- leftOpType.setOriginalType(baseType);
138
- type = leftOpType;
139
- }
140
- }
141
- else if (leftOpType instanceof Type_1.AnnotationNamespaceType) {
142
- type = this.inferUnclearRefName(leftOpType.getOriginType(), declaringArkClass);
143
- }
144
- else if (leftOpType instanceof Type_1.UnclearReferenceType) {
145
- type = this.inferUnclearRefType(leftOpType, declaringArkClass);
146
- }
147
- return type;
148
- }
149
- static inferTypeInMethod(arkMethod) {
150
- var _a;
151
- const arkClass = arkMethod.getDeclaringArkClass();
152
- this.inferGenericType(arkMethod.getGenericTypes(), arkClass);
153
- const signatures = [];
154
- (_a = arkMethod.getDeclareSignatures()) === null || _a === void 0 ? void 0 : _a.forEach(m => signatures.push(m));
155
- const impl = arkMethod.getImplementationSignature();
156
- if (impl) {
157
- signatures.push(impl);
158
- }
159
- signatures.forEach(s => {
160
- s.getMethodSubSignature().getParameters().forEach(p => {
161
- this.inferParameterType(p, arkMethod);
162
- });
163
- this.inferSignatureReturnType(s, arkMethod);
164
- });
165
- const body = arkMethod.getBody();
166
- if (!body) {
167
- return;
168
- }
169
- const cfg = body.getCfg();
170
- for (const block of cfg.getBlocks()) {
171
- for (const stmt of block.getStmts()) {
172
- this.resolveTypeExprsInStmt(stmt, arkMethod);
173
- this.resolveExprsInStmt(stmt, arkMethod);
174
- this.resolveFieldRefsInStmt(stmt, arkMethod);
175
- this.resolveArkAssignStmt(stmt, arkMethod);
176
- }
177
- }
178
- }
179
- /**
180
- * @Deprecated
181
- * @param arkMethod
182
- */
183
- static inferSimpleTypeInMethod(arkMethod) {
184
- const body = arkMethod.getBody();
185
- if (!body) {
186
- logger.warn('empty body');
187
- return;
188
- }
189
- const cfg = body.getCfg();
190
- for (const block of cfg.getBlocks()) {
191
- for (const stmt of block.getStmts()) {
192
- TypeInference.inferSimpleTypeInStmt(stmt);
193
- }
194
- }
195
- }
196
- /**
197
- * infer type for Exprs in stmt which invoke method.
198
- * such as ArkInstanceInvokeExpr ArkStaticInvokeExpr ArkNewExpr
199
- */
200
- static resolveExprsInStmt(stmt, arkMethod) {
201
- for (const expr of stmt.getExprs()) {
202
- const newExpr = expr.inferType(arkMethod);
203
- if (stmt.containsInvokeExpr() && expr instanceof Expr_1.ArkInstanceInvokeExpr && newExpr instanceof Expr_1.ArkStaticInvokeExpr) {
204
- stmt.replaceUse(expr, newExpr);
205
- }
206
- }
207
- if (stmt instanceof Stmt_1.ArkAliasTypeDefineStmt && this.isUnclearType(stmt.getAliasType().getOriginalType())) {
208
- stmt.getAliasType().setOriginalType(stmt.getAliasTypeExpr().getType());
209
- }
210
- }
211
- /**
212
- * infer value type for TypeExprs in stmt which specify the type such as TypeQueryExpr
213
- */
214
- static resolveTypeExprsInStmt(stmt, arkMethod) {
215
- for (let typeExpr of stmt.getTypeExprs()) {
216
- typeExpr.inferType(arkMethod);
217
- }
218
- }
219
- /**
220
- * infer type for fieldRefs in stmt.
221
- */
222
- static resolveFieldRefsInStmt(stmt, arkMethod) {
223
- for (const use of stmt.getUses()) {
224
- if (use instanceof Ref_1.AbstractRef) {
225
- this.processRef(use, stmt, arkMethod);
226
- }
227
- }
228
- const stmtDef = stmt.getDef();
229
- if (stmtDef && stmtDef instanceof Ref_1.AbstractRef) {
230
- const fieldRef = stmtDef.inferType(arkMethod);
231
- stmt.replaceDef(stmtDef, fieldRef);
232
- }
233
- }
234
- static processRef(use, stmt, arkMethod) {
235
- var _a;
236
- const fieldRef = use.inferType(arkMethod);
237
- if (fieldRef instanceof Ref_1.ArkStaticFieldRef && stmt instanceof Stmt_1.ArkAssignStmt) {
238
- stmt.replaceUse(use, fieldRef);
239
- }
240
- else if (use instanceof Ref_1.ArkInstanceFieldRef && fieldRef instanceof Ref_1.ArkArrayRef && stmt instanceof Stmt_1.ArkAssignStmt) {
241
- const index = fieldRef.getIndex();
242
- if (index instanceof Constant_1.Constant && index.getType() instanceof Type_1.StringType) {
243
- const local = (_a = arkMethod === null || arkMethod === void 0 ? void 0 : arkMethod.getBody()) === null || _a === void 0 ? void 0 : _a.getLocals().get(index.getValue());
244
- if (local) {
245
- fieldRef.setIndex(local);
246
- }
247
- }
248
- stmt.replaceUse(use, fieldRef);
249
- }
250
- }
251
- static parseArkExport2Type(arkExport) {
252
- if (!arkExport) {
253
- return null;
254
- }
255
- if (arkExport instanceof ArkClass_1.ArkClass) {
256
- return new Type_1.ClassType(arkExport.getSignature(), arkExport.getGenericsTypes());
257
- }
258
- else if (arkExport instanceof ArkNamespace_1.ArkNamespace) {
259
- return Type_1.AnnotationNamespaceType.getInstance(arkExport.getSignature());
260
- }
261
- else if (arkExport instanceof ArkMethod_1.ArkMethod) {
262
- return new Type_1.FunctionType(arkExport.getSignature());
263
- }
264
- else if (arkExport instanceof Local_1.Local) {
265
- if (arkExport.getType() instanceof Type_1.UnknownType || arkExport.getType() instanceof Type_1.UnclearReferenceType) {
266
- return null;
267
- }
268
- return arkExport.getType();
269
- }
270
- else if (arkExport instanceof Type_1.AliasType) {
271
- return arkExport;
272
- }
273
- else {
274
- return null;
275
- }
276
- }
277
- /**
278
- * infer and pass type for ArkAssignStmt right and left
279
- * @param stmt
280
- * @param arkMethod
281
- */
282
- static resolveArkAssignStmt(stmt, arkMethod) {
283
- var _a;
284
- if (!(stmt instanceof Stmt_1.ArkAssignStmt)) {
285
- return;
286
- }
287
- const arkClass = arkMethod.getDeclaringArkClass();
288
- const rightOp = stmt.getRightOp();
289
- let rightType = rightOp.getType();
290
- if (this.isUnclearType(rightType)) {
291
- rightType = this.inferUnclearedType(rightType, arkClass);
292
- if (rightType) {
293
- this.setValueType(rightOp, rightType);
294
- }
295
- else {
296
- if (rightOp instanceof Local_1.Local) {
297
- IRInference_1.IRInference.inferLocal(rightOp, arkMethod);
298
- rightType = rightOp.getType();
299
- }
300
- }
301
- }
302
- const leftOp = stmt.getLeftOp();
303
- let leftType = leftOp.getType();
304
- if (this.isUnclearType(leftType)) {
305
- const newLeftType = this.inferUnclearedType(leftType, arkClass);
306
- if (!newLeftType && !this.isUnclearType(rightType)) {
307
- leftType = rightType;
308
- }
309
- else if (newLeftType) {
310
- leftType = newLeftType;
311
- }
312
- }
313
- if (leftType && !this.isUnclearType(leftType)) {
314
- this.setValueType(leftOp, leftType);
315
- if (leftOp instanceof Local_1.Local && ((_a = stmt.getOriginalText()) === null || _a === void 0 ? void 0 : _a.startsWith(leftOp.getName()))) {
316
- let localDef = ModelUtils_1.ModelUtils.findDeclaredLocal(leftOp, arkMethod);
317
- if (localDef && this.isUnclearType(localDef.getType())) {
318
- localDef.setType(leftType);
319
- }
320
- }
321
- if (rightType) {
322
- IRInference_1.IRInference.inferRightWithSdkType(leftType, rightType, arkClass);
323
- }
324
- }
325
- }
326
- static setValueType(value, type) {
327
- if (value instanceof Local_1.Local || value instanceof Ref_1.ArkParameterRef) {
328
- value.setType(type);
329
- }
330
- else if (value instanceof Ref_1.AbstractFieldRef) {
331
- value.getFieldSignature().setType(type);
332
- }
333
- }
334
- static isUnclearType(type) {
335
- // TODO: For UnionType, IntersectionType and TupleType, it should recurse check every item of them.
336
- if (!type || type instanceof Type_1.UnknownType || type instanceof Type_1.UnclearReferenceType
337
- || type instanceof Type_1.NullType || type instanceof Type_1.UndefinedType) {
338
- return true;
339
- }
340
- else if (type instanceof Type_1.ClassType
341
- && type.getClassSignature().getDeclaringFileSignature().getFileName() === Const_1.UNKNOWN_FILE_NAME) {
342
- return true;
343
- }
344
- else if (type instanceof Type_1.UnionType || type instanceof Type_1.IntersectionType || type instanceof Type_1.TupleType) {
345
- return !!type.getTypes().find(t => this.hasUnclearReferenceType(t));
346
- }
347
- else if (type instanceof Type_1.ArrayType) {
348
- return this.hasUnclearReferenceType(type.getBaseType());
349
- }
350
- else if (type instanceof Type_1.AliasType) {
351
- return this.isUnclearType(type.getOriginalType());
352
- }
353
- else if (type instanceof TypeExpr_1.KeyofTypeExpr) {
354
- return this.isUnclearType(type.getOpType());
355
- }
356
- else if (type instanceof TypeExpr_1.TypeQueryExpr) {
357
- return this.isUnclearType(type.getType());
358
- }
359
- return false;
360
- }
361
- // This is the temporally function to check unclearReferenceType recursively and can be removed after typeInfer supports multiple candidate types.
362
- static hasUnclearReferenceType(type) {
363
- if (type instanceof Type_1.UnclearReferenceType) {
364
- return true;
365
- }
366
- else if (type instanceof Type_1.UnionType || type instanceof Type_1.IntersectionType || type instanceof Type_1.TupleType) {
367
- return !!type.getTypes().find(t => this.hasUnclearReferenceType(t));
368
- }
369
- else if (type instanceof Type_1.ArrayType) {
370
- return this.hasUnclearReferenceType(type.getBaseType());
371
- }
372
- else if (type instanceof Type_1.AliasType) {
373
- return this.hasUnclearReferenceType(type.getOriginalType());
374
- }
375
- else if (type instanceof TypeExpr_1.KeyofTypeExpr) {
376
- return this.hasUnclearReferenceType(type.getOpType());
377
- }
378
- else if (type instanceof TypeExpr_1.TypeQueryExpr) {
379
- return this.hasUnclearReferenceType(type.getType());
380
- }
381
- return false;
382
- }
383
- static inferSimpleTypeInStmt(stmt) {
384
- if (stmt instanceof Stmt_1.ArkAssignStmt) {
385
- const leftOp = stmt.getLeftOp();
386
- if (leftOp instanceof Local_1.Local) {
387
- const leftOpType = leftOp.getType();
388
- if (leftOpType instanceof Type_1.UnknownType) {
389
- const rightOp = stmt.getRightOp();
390
- leftOp.setType(rightOp.getType());
391
- }
392
- }
393
- }
394
- }
395
- // Deal only with simple situations
396
- static buildTypeFromStr(typeStr) {
397
- switch (typeStr) {
398
- case 'boolean':
399
- return Type_1.BooleanType.getInstance();
400
- case 'number':
401
- return Type_1.NumberType.getInstance();
402
- case 'string':
403
- return Type_1.StringType.getInstance();
404
- case 'undefined':
405
- return Type_1.UndefinedType.getInstance();
406
- case 'null':
407
- return Type_1.NullType.getInstance();
408
- case 'any':
409
- return Type_1.AnyType.getInstance();
410
- case 'void':
411
- return Type_1.VoidType.getInstance();
412
- case 'never':
413
- return Type_1.NeverType.getInstance();
414
- case 'RegularExpression': {
415
- const classSignature = Builtin_1.Builtin.REGEXP_CLASS_SIGNATURE;
416
- return new Type_1.ClassType(classSignature);
417
- }
418
- default:
419
- return new Type_1.UnclearReferenceType(typeStr);
420
- }
421
- }
422
- static inferValueType(value, arkMethod) {
423
- if (value instanceof Ref_1.ArkInstanceFieldRef || value instanceof Expr_1.ArkInstanceInvokeExpr) {
424
- this.inferValueType(value.getBase(), arkMethod);
425
- }
426
- if (value instanceof Ref_1.AbstractRef || value instanceof Expr_1.AbstractExpr || value instanceof Local_1.Local) {
427
- value.inferType(arkMethod);
428
- }
429
- return value.getType();
430
- }
431
- static inferParameterType(param, arkMethod) {
432
- let pType = param.getType();
433
- const type = TypeInference.inferUnclearedType(pType, arkMethod.getDeclaringArkClass());
434
- if (type) {
435
- param.setType(type);
436
- }
437
- else {
438
- if (pType instanceof TypeExpr_1.AbstractTypeExpr) {
439
- pType.inferType(arkMethod);
440
- }
441
- }
442
- }
443
- static inferSignatureReturnType(oldSignature, arkMethod) {
444
- if (oldSignature.getMethodSubSignature().getMethodName() === TSConst_1.CONSTRUCTOR_NAME) {
445
- const newReturnType = new Type_1.ClassType(oldSignature.getDeclaringClassSignature());
446
- oldSignature.getMethodSubSignature().setReturnType(newReturnType);
447
- return;
448
- }
449
- const currReturnType = oldSignature.getType();
450
- if (!this.isUnclearType(currReturnType)) {
451
- return;
452
- }
453
- if (currReturnType instanceof TypeExpr_1.AbstractTypeExpr) {
454
- currReturnType.inferType(arkMethod);
455
- return;
456
- }
457
- if (currReturnType instanceof Type_1.ArrayType && currReturnType.getBaseType() instanceof TypeExpr_1.AbstractTypeExpr) {
458
- currReturnType.getBaseType().inferType(arkMethod);
459
- return;
460
- }
461
- const newReturnType = this.inferUnclearedType(currReturnType, arkMethod.getDeclaringArkClass());
462
- if (newReturnType) {
463
- oldSignature.getMethodSubSignature().setReturnType(newReturnType);
464
- }
465
- else if (arkMethod.getBody()) {
466
- const typeMap = new Map();
467
- for (let returnValue of arkMethod.getReturnValues()) {
468
- const type = returnValue.getType();
469
- if (type instanceof Type_1.UnionType) {
470
- type.flatType().filter(t => !TypeInference.isUnclearType(t)).forEach(t => typeMap.set(t.toString(), t));
471
- }
472
- else if (!TypeInference.isUnclearType(type)) {
473
- typeMap.set(type.toString(), type);
474
- }
475
- }
476
- if (typeMap.size > 0) {
477
- const types = Array.from(typeMap.values());
478
- oldSignature.getMethodSubSignature().setReturnType(types.length === 1 ? types[0] : new Type_1.UnionType(types));
479
- }
480
- }
481
- }
482
- static inferGenericType(types, arkClass) {
483
- types === null || types === void 0 ? void 0 : types.forEach(type => {
484
- const defaultType = type.getDefaultType();
485
- if (defaultType instanceof Type_1.UnclearReferenceType) {
486
- const newDefaultType = TypeInference.inferUnclearRefName(defaultType.getName(), arkClass);
487
- if (newDefaultType) {
488
- type.setDefaultType(this.replaceTypeWithReal(newDefaultType));
489
- }
490
- }
491
- const constraint = type.getConstraint();
492
- if (constraint instanceof Type_1.UnclearReferenceType) {
493
- const newConstraint = TypeInference.inferUnclearRefName(constraint.getName(), arkClass);
494
- if (newConstraint) {
495
- type.setConstraint(this.replaceTypeWithReal(newConstraint));
496
- }
497
- }
498
- });
499
- }
500
- /**
501
- * Infer type for a given {@link UnclearReferenceType} type.
502
- * It returns original type.
503
- * The original type is null if it failed to infer the type.
504
- * @param urType
505
- * @param arkClass
506
- * @returns
507
- */
508
- static inferUnclearRefType(urType, arkClass) {
509
- var _a;
510
- const realTypes = urType.getGenericTypes();
511
- this.inferRealGenericTypes(realTypes, arkClass);
512
- if (urType.getName() === Builtin_1.Builtin.ARRAY) {
513
- return new Type_1.ArrayType((_a = realTypes[0]) !== null && _a !== void 0 ? _a : Type_1.AnyType.getInstance(), 1);
514
- }
515
- let type = this.inferUnclearRefName(urType.getName(), arkClass);
516
- return type ? this.replaceTypeWithReal(type, realTypes) : null;
517
- }
518
- /**
519
- * Find out the original object and type for a given unclear reference type name.
520
- * It returns original type.
521
- * The original type is null if it failed to infer the type.
522
- * @param refName
523
- * @param arkClass
524
- * @returns
525
- */
526
- static inferUnclearRefName(refName, arkClass) {
527
- var _a;
528
- if (!refName) {
529
- return null;
530
- }
531
- //split and iterate to infer each type
532
- const singleNames = refName.split('.');
533
- let type = null;
534
- for (let i = 0; i < singleNames.length; i++) {
535
- let genericName = ValueUtil_1.EMPTY_STRING;
536
- const name = singleNames[i].replace(/<(\w+)>/, (match, group1) => {
537
- genericName = group1;
538
- return ValueUtil_1.EMPTY_STRING;
539
- });
540
- if (i === 0) {
541
- type = this.inferBaseType(name, arkClass);
542
- }
543
- else if (type) {
544
- type = (_a = this.inferFieldType(type, name, arkClass)) === null || _a === void 0 ? void 0 : _a[1];
545
- }
546
- if (!type) {
547
- return null;
548
- }
549
- if (genericName) {
550
- const realTypes = genericName.split(',').map(generic => {
551
- const realType = this.inferBaseType(generic, arkClass);
552
- return realType !== null && realType !== void 0 ? realType : new Type_1.UnclearReferenceType(generic);
553
- });
554
- if (type instanceof Type_1.ClassType) {
555
- type = new Type_1.ClassType(type.getClassSignature(), realTypes);
556
- }
557
- else if (type instanceof Type_1.FunctionType) {
558
- type = new Type_1.FunctionType(type.getMethodSignature(), realTypes);
559
- }
560
- }
561
- }
562
- return type;
563
- }
564
- /**
565
- * Find out the original object and type for a given base type and the field name.
566
- * It returns an array with 2 items, original object and original type.
567
- * The original object is null if there is no object, or it failed to find the object.
568
- * The original type is null if it failed to infer the type.
569
- * @param baseType
570
- * @param fieldName
571
- * @param declareClass
572
- * @returns
573
- */
574
- static inferFieldType(baseType, fieldName, declareClass) {
575
- if (baseType instanceof Type_1.AliasType) {
576
- baseType = baseType.getOriginalType();
577
- }
578
- else if (baseType instanceof Type_1.UnionType && baseType.getCurrType()) {
579
- baseType = baseType.getCurrType();
580
- }
581
- let propertyAndType = null;
582
- if (baseType instanceof Type_1.ClassType) {
583
- const arkClass = declareClass.getDeclaringArkFile().getScene().getClass(baseType.getClassSignature());
584
- if (!arkClass && fieldName === Builtin_1.Builtin.ITERATOR_RESULT_VALUE && baseType.getClassSignature()
585
- .getDeclaringFileSignature().getProjectName() === Builtin_1.Builtin.DUMMY_PROJECT_NAME) {
586
- const types = baseType.getRealGenericTypes();
587
- if (types && types.length > 0) {
588
- propertyAndType = [null, types[0]];
589
- }
590
- }
591
- if (!arkClass) {
592
- return propertyAndType;
593
- }
594
- if (arkClass.isAnonymousClass()) {
595
- const fieldType = this.inferUnclearRefName(fieldName, arkClass);
596
- return fieldType ? [null, fieldType] : null;
597
- }
598
- const property = ModelUtils_1.ModelUtils.findPropertyInClass(fieldName, arkClass);
599
- let propertyType = null;
600
- if (property instanceof ArkField_1.ArkField) {
601
- propertyType = property.getType();
602
- }
603
- else if (property) {
604
- propertyType = this.parseArkExport2Type(property);
605
- }
606
- if (propertyType) {
607
- propertyAndType = [property, propertyType];
608
- }
609
- }
610
- else if (baseType instanceof Type_1.AnnotationNamespaceType) {
611
- const namespace = declareClass.getDeclaringArkFile().getScene().getNamespace(baseType.getNamespaceSignature());
612
- if (namespace) {
613
- const property = ModelUtils_1.ModelUtils.findPropertyInNamespace(fieldName, namespace);
614
- const propertyType = this.parseArkExport2Type(property);
615
- if (propertyType) {
616
- propertyAndType = [property, propertyType];
617
- }
618
- }
619
- }
620
- else {
621
- logger.warn('infer unclear reference type fail: ' + fieldName);
622
- }
623
- return propertyAndType;
624
- }
625
- /**
626
- * Find out the original object and type for a given base name.
627
- * It returns original type.
628
- * The original type is null if failed to infer the type.
629
- * @param baseName
630
- * @param arkClass
631
- * @returns
632
- */
633
- static inferBaseType(baseName, arkClass) {
634
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
635
- if (TSConst_1.SUPER_NAME === baseName) {
636
- return this.parseArkExport2Type(arkClass.getSuperClass());
637
- }
638
- const field = (_d = (_c = (_b = (_a = ModelUtils_1.ModelUtils.getDefaultClass(arkClass)) === null || _a === void 0 ? void 0 : _a.getDefaultArkMethod()) === null || _b === void 0 ? void 0 : _b.getBody()) === null || _c === void 0 ? void 0 : _c.getLocals()) === null || _d === void 0 ? void 0 : _d.get(baseName);
639
- if (field && !this.isUnclearType(field.getType())) {
640
- return field.getType();
641
- }
642
- let arkExport = (_m = (_k = (_j = (_e = ModelUtils_1.ModelUtils.getClassWithName(baseName, arkClass)) !== null && _e !== void 0 ? _e : (_h = (_g = (_f = ModelUtils_1.ModelUtils.getDefaultClass(arkClass)) === null || _f === void 0 ? void 0 : _f.getDefaultArkMethod()) === null || _g === void 0 ? void 0 : _g.getBody()) === null || _h === void 0 ? void 0 : _h.getAliasTypeByName(baseName)) !== null && _j !== void 0 ? _j : ModelUtils_1.ModelUtils.getNamespaceWithName(baseName, arkClass)) !== null && _k !== void 0 ? _k : (_l = ModelUtils_1.ModelUtils.getDefaultClass(arkClass)) === null || _l === void 0 ? void 0 : _l.getMethodWithName(baseName)) !== null && _m !== void 0 ? _m : ModelUtils_1.ModelUtils.getArkExportInImportInfoWithName(baseName, arkClass.getDeclaringArkFile());
643
- if (!arkExport && !arkClass.getDeclaringArkFile().getImportInfoBy(baseName)) {
644
- arkExport = arkClass.getDeclaringArkFile().getScene().getSdkGlobal(baseName);
645
- }
646
- return this.parseArkExport2Type(arkExport);
647
- }
648
- static inferRealGenericTypes(realTypes, arkClass) {
649
- if (!realTypes) {
650
- return;
651
- }
652
- for (let i = 0; i < realTypes.length; i++) {
653
- const mayType = realTypes[i];
654
- if (this.isUnclearType(mayType)) {
655
- const newType = this.inferUnclearedType(mayType, arkClass);
656
- if (newType) {
657
- realTypes[i] = newType;
658
- }
659
- }
660
- }
661
- }
662
- static inferDynamicImportType(from, arkClass) {
663
- var _a;
664
- const importInfo = new ArkImport_1.ImportInfo();
665
- importInfo.setNameBeforeAs(TSConst_1.ALL);
666
- importInfo.setImportClauseName(TSConst_1.ALL);
667
- importInfo.setImportFrom(from);
668
- importInfo.setDeclaringArkFile(arkClass.getDeclaringArkFile());
669
- return TypeInference.parseArkExport2Type((_a = importInfo.getLazyExportInfo()) === null || _a === void 0 ? void 0 : _a.getArkExport());
670
- }
671
- static replaceTypeWithReal(type, realTypes) {
672
- var _a, _b, _c, _d, _e, _f, _g, _h;
673
- if (type instanceof Type_1.ClassType) {
674
- const replacedTypes = (_b = (_a = type.getRealGenericTypes()) === null || _a === void 0 ? void 0 : _a.map(g => this.replaceTypeWithReal(g, realTypes))) !== null && _b !== void 0 ? _b : realTypes;
675
- return replacedTypes && replacedTypes.length > 0 ? new Type_1.ClassType(type.getClassSignature(), replacedTypes) : type;
676
- }
677
- else if (type instanceof Type_1.FunctionType) {
678
- const replacedTypes = (_d = (_c = type.getRealGenericTypes()) === null || _c === void 0 ? void 0 : _c.map(g => this.replaceTypeWithReal(g, realTypes))) !== null && _d !== void 0 ? _d : realTypes;
679
- return replacedTypes && replacedTypes.length > 0 ? new Type_1.FunctionType(type.getMethodSignature(), replacedTypes) : type;
680
- }
681
- else if (type instanceof Type_1.AliasType && realTypes) {
682
- const newObjectType = this.replaceTypeWithReal(type.getOriginalType(), realTypes);
683
- const replacedTypes = (_f = (_e = type.getRealGenericTypes()) === null || _e === void 0 ? void 0 : _e.map(g => this.replaceTypeWithReal(g, realTypes))) !== null && _f !== void 0 ? _f : realTypes;
684
- if (replacedTypes.length > 0) {
685
- const newAliasType = new Type_1.AliasType(type.getName(), newObjectType, type.getSignature(), type.getGenericTypes());
686
- newAliasType.setRealGenericTypes(replacedTypes);
687
- return newAliasType;
688
- }
689
- }
690
- else if (type instanceof Type_1.UnionType && realTypes) {
691
- const types = [];
692
- type.flatType().forEach(t => types.push(this.replaceTypeWithReal(t, realTypes)));
693
- return new Type_1.UnionType(types, this.replaceTypeWithReal(type.getCurrType(), realTypes));
694
- }
695
- else if (type instanceof Type_1.IntersectionType && realTypes) {
696
- const types = [];
697
- type.getTypes().forEach(t => types.push(this.replaceTypeWithReal(t, realTypes)));
698
- return new Type_1.IntersectionType(types);
699
- }
700
- else if (type instanceof Type_1.ArrayType && realTypes) {
701
- const replacedBaseType = this.replaceTypeWithReal(type.getBaseType(), realTypes);
702
- return new Type_1.ArrayType(replacedBaseType, type.getDimension());
703
- }
704
- else if (type instanceof Type_1.TupleType && realTypes) {
705
- let replacedTypes = [];
706
- type.getTypes().forEach(t => { replacedTypes.push(this.replaceTypeWithReal(t, realTypes)); });
707
- return new Type_1.TupleType(replacedTypes);
708
- }
709
- else if (type instanceof Type_1.GenericType) {
710
- const realType = (_h = (_g = realTypes === null || realTypes === void 0 ? void 0 : realTypes[type.getIndex()]) !== null && _g !== void 0 ? _g : type.getDefaultType()) !== null && _h !== void 0 ? _h : type.getConstraint();
711
- if (realType) {
712
- return realType;
713
- }
714
- }
715
- else if (type instanceof Type_1.AnyType) {
716
- const realType = realTypes === null || realTypes === void 0 ? void 0 : realTypes[0];
717
- if (realType) {
718
- return realType;
719
- }
720
- }
721
- return type;
722
- }
723
- static replaceAliasType(type) {
724
- let aliasType = type;
725
- while (aliasType instanceof Type_1.AliasType) {
726
- aliasType = aliasType.getOriginalType();
727
- }
728
- return aliasType;
729
- }
730
- static inferFunctionType(argType, params, realTypes) {
731
- if (!params) {
732
- return;
733
- }
734
- argType.getMethodSignature().getMethodSubSignature().getParameters()
735
- .filter(p => !p.getName().startsWith(Const_1.LEXICAL_ENV_NAME_PREFIX))
736
- .forEach((p, i) => {
737
- var _a;
738
- let type = (_a = params === null || params === void 0 ? void 0 : params[i]) === null || _a === void 0 ? void 0 : _a.getType();
739
- if (type instanceof Type_1.GenericType && realTypes) {
740
- type = realTypes === null || realTypes === void 0 ? void 0 : realTypes[type.getIndex()];
741
- }
742
- if (type) {
743
- p.setType(type);
744
- }
745
- });
746
- }
747
- }
748
- exports.TypeInference = TypeInference;
1
+ "use strict";
2
+ /*
3
+ * Copyright (c) 2024 Huawei Device Co., Ltd.
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
17
+ if (k2 === undefined) k2 = k;
18
+ var desc = Object.getOwnPropertyDescriptor(m, k);
19
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
20
+ desc = { enumerable: true, get: function() { return m[k]; } };
21
+ }
22
+ Object.defineProperty(o, k2, desc);
23
+ }) : (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ o[k2] = m[k];
26
+ }));
27
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
28
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
29
+ }) : function(o, v) {
30
+ o["default"] = v;
31
+ });
32
+ var __importStar = (this && this.__importStar) || function (mod) {
33
+ if (mod && mod.__esModule) return mod;
34
+ var result = {};
35
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
36
+ __setModuleDefault(result, mod);
37
+ return result;
38
+ };
39
+ Object.defineProperty(exports, "__esModule", { value: true });
40
+ exports.TypeInference = void 0;
41
+ const logger_1 = __importStar(require("../../utils/logger"));
42
+ const Expr_1 = require("../base/Expr");
43
+ const Local_1 = require("../base/Local");
44
+ const Ref_1 = require("../base/Ref");
45
+ const Stmt_1 = require("../base/Stmt");
46
+ const Type_1 = require("../base/Type");
47
+ const ArkMethod_1 = require("../model/ArkMethod");
48
+ const ArkClass_1 = require("../model/ArkClass");
49
+ const ArkField_1 = require("../model/ArkField");
50
+ const Constant_1 = require("../base/Constant");
51
+ const ArkNamespace_1 = require("../model/ArkNamespace");
52
+ const TSConst_1 = require("./TSConst");
53
+ const ModelUtils_1 = require("./ModelUtils");
54
+ const Builtin_1 = require("./Builtin");
55
+ const Const_1 = require("./Const");
56
+ const ValueUtil_1 = require("./ValueUtil");
57
+ const ArkImport_1 = require("../model/ArkImport");
58
+ const IRInference_1 = require("./IRInference");
59
+ const TypeExpr_1 = require("../base/TypeExpr");
60
+ const logger = logger_1.default.getLogger(logger_1.LOG_MODULE_TYPE.ARKANALYZER, 'TypeInference');
61
+ class TypeInference {
62
+ static inferTypeInArkField(arkField) {
63
+ var _a;
64
+ const arkClass = arkField.getDeclaringArkClass();
65
+ const stmts = arkField.getInitializer();
66
+ const method = (_a = arkClass.getMethodWithName(Const_1.INSTANCE_INIT_METHOD_NAME)) !== null && _a !== void 0 ? _a : arkClass.getMethodWithName(TSConst_1.CONSTRUCTOR_NAME);
67
+ for (const stmt of stmts) {
68
+ if (method) {
69
+ this.resolveTypeExprsInStmt(stmt, method);
70
+ this.resolveExprsInStmt(stmt, method);
71
+ this.resolveFieldRefsInStmt(stmt, method);
72
+ this.resolveArkAssignStmt(stmt, method);
73
+ }
74
+ }
75
+ const beforeType = arkField.getType();
76
+ if (!this.isUnclearType(beforeType)) {
77
+ return;
78
+ }
79
+ let rightType;
80
+ let fieldRef;
81
+ const lastStmt = stmts[stmts.length - 1];
82
+ if (lastStmt instanceof Stmt_1.ArkAssignStmt) {
83
+ rightType = lastStmt.getRightOp().getType();
84
+ if (lastStmt.getLeftOp() instanceof Ref_1.AbstractFieldRef) {
85
+ fieldRef = lastStmt.getLeftOp();
86
+ }
87
+ }
88
+ let fieldType;
89
+ if (beforeType) {
90
+ fieldType = this.inferUnclearedType(beforeType, arkClass, rightType);
91
+ }
92
+ if (fieldType) {
93
+ arkField.getSignature().setType(fieldType);
94
+ fieldRef === null || fieldRef === void 0 ? void 0 : fieldRef.setFieldSignature(arkField.getSignature());
95
+ }
96
+ else if (rightType && this.isUnclearType(beforeType) && !this.isUnclearType(rightType)) {
97
+ arkField.getSignature().setType(rightType);
98
+ fieldRef === null || fieldRef === void 0 ? void 0 : fieldRef.setFieldSignature(arkField.getSignature());
99
+ }
100
+ }
101
+ /**
102
+ * Infer type for a given unclear type.
103
+ * It returns an array with 2 items, original object and original type.
104
+ * The original object is null if there is no object, or it failed to find the object.
105
+ * The original type is null if failed to infer the type.
106
+ * @param leftOpType
107
+ * @param declaringArkClass
108
+ * @param [rightType]
109
+ * @returns
110
+ */
111
+ static inferUnclearedType(leftOpType, declaringArkClass, rightType) {
112
+ let type;
113
+ if (leftOpType instanceof Type_1.ClassType &&
114
+ leftOpType.getClassSignature().getDeclaringFileSignature().getFileName() === Const_1.UNKNOWN_FILE_NAME) {
115
+ type = TypeInference.inferUnclearRefName(leftOpType.getClassSignature().getClassName(), declaringArkClass);
116
+ }
117
+ else if (leftOpType instanceof Type_1.UnionType || leftOpType instanceof Type_1.IntersectionType || leftOpType instanceof Type_1.TupleType) {
118
+ let types = leftOpType.getTypes();
119
+ for (let i = 0; i < types.length; i++) {
120
+ let newType = this.inferUnclearedType(types[i], declaringArkClass);
121
+ if (newType) {
122
+ types[i] = newType;
123
+ }
124
+ }
125
+ type = leftOpType;
126
+ }
127
+ else if (leftOpType instanceof Type_1.ArrayType) {
128
+ let baseType = this.inferUnclearedType(leftOpType.getBaseType(), declaringArkClass);
129
+ if (baseType) {
130
+ leftOpType.setBaseType(baseType);
131
+ type = leftOpType;
132
+ }
133
+ }
134
+ else if (leftOpType instanceof Type_1.AliasType) {
135
+ let baseType = this.inferUnclearedType(leftOpType.getOriginalType(), declaringArkClass);
136
+ if (baseType) {
137
+ leftOpType.setOriginalType(baseType);
138
+ type = leftOpType;
139
+ }
140
+ }
141
+ else if (leftOpType instanceof Type_1.AnnotationNamespaceType) {
142
+ type = this.inferUnclearRefName(leftOpType.getOriginType(), declaringArkClass);
143
+ }
144
+ else if (leftOpType instanceof Type_1.UnclearReferenceType) {
145
+ type = this.inferUnclearRefType(leftOpType, declaringArkClass);
146
+ }
147
+ return type;
148
+ }
149
+ static inferTypeInMethod(arkMethod) {
150
+ var _a;
151
+ const arkClass = arkMethod.getDeclaringArkClass();
152
+ this.inferGenericType(arkMethod.getGenericTypes(), arkClass);
153
+ const signatures = [];
154
+ (_a = arkMethod.getDeclareSignatures()) === null || _a === void 0 ? void 0 : _a.forEach(m => signatures.push(m));
155
+ const impl = arkMethod.getImplementationSignature();
156
+ if (impl) {
157
+ signatures.push(impl);
158
+ }
159
+ signatures.forEach(s => {
160
+ s.getMethodSubSignature().getParameters().forEach(p => {
161
+ this.inferParameterType(p, arkMethod);
162
+ });
163
+ this.inferSignatureReturnType(s, arkMethod);
164
+ });
165
+ const body = arkMethod.getBody();
166
+ if (!body) {
167
+ return;
168
+ }
169
+ const cfg = body.getCfg();
170
+ for (const block of cfg.getBlocks()) {
171
+ for (const stmt of block.getStmts()) {
172
+ this.resolveTypeExprsInStmt(stmt, arkMethod);
173
+ this.resolveExprsInStmt(stmt, arkMethod);
174
+ this.resolveFieldRefsInStmt(stmt, arkMethod);
175
+ this.resolveArkAssignStmt(stmt, arkMethod);
176
+ }
177
+ }
178
+ }
179
+ /**
180
+ * @Deprecated
181
+ * @param arkMethod
182
+ */
183
+ static inferSimpleTypeInMethod(arkMethod) {
184
+ const body = arkMethod.getBody();
185
+ if (!body) {
186
+ logger.warn('empty body');
187
+ return;
188
+ }
189
+ const cfg = body.getCfg();
190
+ for (const block of cfg.getBlocks()) {
191
+ for (const stmt of block.getStmts()) {
192
+ TypeInference.inferSimpleTypeInStmt(stmt);
193
+ }
194
+ }
195
+ }
196
+ /**
197
+ * infer type for Exprs in stmt which invoke method.
198
+ * such as ArkInstanceInvokeExpr ArkStaticInvokeExpr ArkNewExpr
199
+ */
200
+ static resolveExprsInStmt(stmt, arkMethod) {
201
+ for (const expr of stmt.getExprs()) {
202
+ const newExpr = expr.inferType(arkMethod);
203
+ if (stmt.containsInvokeExpr() && expr instanceof Expr_1.ArkInstanceInvokeExpr && newExpr instanceof Expr_1.ArkStaticInvokeExpr) {
204
+ stmt.replaceUse(expr, newExpr);
205
+ }
206
+ }
207
+ if (stmt instanceof Stmt_1.ArkAliasTypeDefineStmt && this.isUnclearType(stmt.getAliasType().getOriginalType())) {
208
+ stmt.getAliasType().setOriginalType(stmt.getAliasTypeExpr().getType());
209
+ }
210
+ }
211
+ /**
212
+ * infer value type for TypeExprs in stmt which specify the type such as TypeQueryExpr
213
+ */
214
+ static resolveTypeExprsInStmt(stmt, arkMethod) {
215
+ for (let typeExpr of stmt.getTypeExprs()) {
216
+ typeExpr.inferType(arkMethod);
217
+ }
218
+ }
219
+ /**
220
+ * infer type for fieldRefs in stmt.
221
+ */
222
+ static resolveFieldRefsInStmt(stmt, arkMethod) {
223
+ for (const use of stmt.getUses()) {
224
+ if (use instanceof Ref_1.AbstractRef) {
225
+ this.processRef(use, stmt, arkMethod);
226
+ }
227
+ }
228
+ const stmtDef = stmt.getDef();
229
+ if (stmtDef && stmtDef instanceof Ref_1.AbstractRef) {
230
+ const fieldRef = stmtDef.inferType(arkMethod);
231
+ stmt.replaceDef(stmtDef, fieldRef);
232
+ }
233
+ }
234
+ static processRef(use, stmt, arkMethod) {
235
+ var _a;
236
+ const fieldRef = use.inferType(arkMethod);
237
+ if (fieldRef instanceof Ref_1.ArkStaticFieldRef && stmt instanceof Stmt_1.ArkAssignStmt) {
238
+ stmt.replaceUse(use, fieldRef);
239
+ }
240
+ else if (use instanceof Ref_1.ArkInstanceFieldRef && fieldRef instanceof Ref_1.ArkArrayRef && stmt instanceof Stmt_1.ArkAssignStmt) {
241
+ const index = fieldRef.getIndex();
242
+ if (index instanceof Constant_1.Constant && index.getType() instanceof Type_1.StringType) {
243
+ const local = (_a = arkMethod === null || arkMethod === void 0 ? void 0 : arkMethod.getBody()) === null || _a === void 0 ? void 0 : _a.getLocals().get(index.getValue());
244
+ if (local) {
245
+ fieldRef.setIndex(local);
246
+ }
247
+ }
248
+ stmt.replaceUse(use, fieldRef);
249
+ }
250
+ }
251
+ static parseArkExport2Type(arkExport) {
252
+ if (!arkExport) {
253
+ return null;
254
+ }
255
+ if (arkExport instanceof ArkClass_1.ArkClass) {
256
+ return new Type_1.ClassType(arkExport.getSignature(), arkExport.getGenericsTypes());
257
+ }
258
+ else if (arkExport instanceof ArkNamespace_1.ArkNamespace) {
259
+ return Type_1.AnnotationNamespaceType.getInstance(arkExport.getSignature());
260
+ }
261
+ else if (arkExport instanceof ArkMethod_1.ArkMethod) {
262
+ return new Type_1.FunctionType(arkExport.getSignature());
263
+ }
264
+ else if (arkExport instanceof Local_1.Local) {
265
+ if (arkExport.getType() instanceof Type_1.UnknownType || arkExport.getType() instanceof Type_1.UnclearReferenceType) {
266
+ return null;
267
+ }
268
+ return arkExport.getType();
269
+ }
270
+ else if (arkExport instanceof Type_1.AliasType) {
271
+ return arkExport;
272
+ }
273
+ else {
274
+ return null;
275
+ }
276
+ }
277
+ /**
278
+ * infer and pass type for ArkAssignStmt right and left
279
+ * @param stmt
280
+ * @param arkMethod
281
+ */
282
+ static resolveArkAssignStmt(stmt, arkMethod) {
283
+ var _a;
284
+ if (!(stmt instanceof Stmt_1.ArkAssignStmt)) {
285
+ return;
286
+ }
287
+ const arkClass = arkMethod.getDeclaringArkClass();
288
+ const rightOp = stmt.getRightOp();
289
+ let rightType = rightOp.getType();
290
+ if (this.isUnclearType(rightType)) {
291
+ rightType = this.inferUnclearedType(rightType, arkClass);
292
+ if (rightType) {
293
+ this.setValueType(rightOp, rightType);
294
+ }
295
+ else {
296
+ if (rightOp instanceof Local_1.Local) {
297
+ IRInference_1.IRInference.inferLocal(rightOp, arkMethod);
298
+ rightType = rightOp.getType();
299
+ }
300
+ }
301
+ }
302
+ const leftOp = stmt.getLeftOp();
303
+ let leftType = leftOp.getType();
304
+ if (this.isUnclearType(leftType)) {
305
+ const newLeftType = this.inferUnclearedType(leftType, arkClass);
306
+ if (!newLeftType && !this.isUnclearType(rightType)) {
307
+ leftType = rightType;
308
+ }
309
+ else if (newLeftType) {
310
+ leftType = newLeftType;
311
+ }
312
+ }
313
+ if (leftType && !this.isUnclearType(leftType)) {
314
+ this.setValueType(leftOp, leftType);
315
+ if (leftOp instanceof Local_1.Local && ((_a = stmt.getOriginalText()) === null || _a === void 0 ? void 0 : _a.startsWith(leftOp.getName()))) {
316
+ let localDef = ModelUtils_1.ModelUtils.findDeclaredLocal(leftOp, arkMethod);
317
+ if (localDef && this.isUnclearType(localDef.getType())) {
318
+ localDef.setType(leftType);
319
+ }
320
+ }
321
+ if (rightType) {
322
+ IRInference_1.IRInference.inferRightWithSdkType(leftType, rightType, arkClass);
323
+ }
324
+ }
325
+ }
326
+ static setValueType(value, type) {
327
+ if (value instanceof Local_1.Local || value instanceof Ref_1.ArkParameterRef) {
328
+ value.setType(type);
329
+ }
330
+ else if (value instanceof Ref_1.AbstractFieldRef) {
331
+ value.getFieldSignature().setType(type);
332
+ }
333
+ }
334
+ static isUnclearType(type) {
335
+ // TODO: For UnionType, IntersectionType and TupleType, it should recurse check every item of them.
336
+ if (!type || type instanceof Type_1.UnknownType || type instanceof Type_1.UnclearReferenceType
337
+ || type instanceof Type_1.NullType || type instanceof Type_1.UndefinedType) {
338
+ return true;
339
+ }
340
+ else if (type instanceof Type_1.ClassType
341
+ && type.getClassSignature().getDeclaringFileSignature().getFileName() === Const_1.UNKNOWN_FILE_NAME) {
342
+ return true;
343
+ }
344
+ else if (type instanceof Type_1.UnionType || type instanceof Type_1.IntersectionType || type instanceof Type_1.TupleType) {
345
+ return !!type.getTypes().find(t => this.hasUnclearReferenceType(t));
346
+ }
347
+ else if (type instanceof Type_1.ArrayType) {
348
+ return this.hasUnclearReferenceType(type.getBaseType());
349
+ }
350
+ else if (type instanceof Type_1.AliasType) {
351
+ return this.isUnclearType(type.getOriginalType());
352
+ }
353
+ else if (type instanceof TypeExpr_1.KeyofTypeExpr) {
354
+ return this.isUnclearType(type.getOpType());
355
+ }
356
+ else if (type instanceof TypeExpr_1.TypeQueryExpr) {
357
+ return this.isUnclearType(type.getType());
358
+ }
359
+ return false;
360
+ }
361
+ // This is the temporally function to check unclearReferenceType recursively and can be removed after typeInfer supports multiple candidate types.
362
+ static hasUnclearReferenceType(type) {
363
+ if (type instanceof Type_1.UnclearReferenceType) {
364
+ return true;
365
+ }
366
+ else if (type instanceof Type_1.UnionType || type instanceof Type_1.IntersectionType || type instanceof Type_1.TupleType) {
367
+ return !!type.getTypes().find(t => this.hasUnclearReferenceType(t));
368
+ }
369
+ else if (type instanceof Type_1.ArrayType) {
370
+ return this.hasUnclearReferenceType(type.getBaseType());
371
+ }
372
+ else if (type instanceof Type_1.AliasType) {
373
+ return this.hasUnclearReferenceType(type.getOriginalType());
374
+ }
375
+ else if (type instanceof TypeExpr_1.KeyofTypeExpr) {
376
+ return this.hasUnclearReferenceType(type.getOpType());
377
+ }
378
+ else if (type instanceof TypeExpr_1.TypeQueryExpr) {
379
+ return this.hasUnclearReferenceType(type.getType());
380
+ }
381
+ return false;
382
+ }
383
+ static inferSimpleTypeInStmt(stmt) {
384
+ if (stmt instanceof Stmt_1.ArkAssignStmt) {
385
+ const leftOp = stmt.getLeftOp();
386
+ if (leftOp instanceof Local_1.Local) {
387
+ const leftOpType = leftOp.getType();
388
+ if (leftOpType instanceof Type_1.UnknownType) {
389
+ const rightOp = stmt.getRightOp();
390
+ leftOp.setType(rightOp.getType());
391
+ }
392
+ }
393
+ }
394
+ }
395
+ // Deal only with simple situations
396
+ static buildTypeFromStr(typeStr) {
397
+ switch (typeStr) {
398
+ case 'boolean':
399
+ return Type_1.BooleanType.getInstance();
400
+ case 'number':
401
+ return Type_1.NumberType.getInstance();
402
+ case 'string':
403
+ return Type_1.StringType.getInstance();
404
+ case 'undefined':
405
+ return Type_1.UndefinedType.getInstance();
406
+ case 'null':
407
+ return Type_1.NullType.getInstance();
408
+ case 'any':
409
+ return Type_1.AnyType.getInstance();
410
+ case 'void':
411
+ return Type_1.VoidType.getInstance();
412
+ case 'never':
413
+ return Type_1.NeverType.getInstance();
414
+ case 'RegularExpression': {
415
+ const classSignature = Builtin_1.Builtin.REGEXP_CLASS_SIGNATURE;
416
+ return new Type_1.ClassType(classSignature);
417
+ }
418
+ default:
419
+ return new Type_1.UnclearReferenceType(typeStr);
420
+ }
421
+ }
422
+ static inferValueType(value, arkMethod) {
423
+ if (value instanceof Ref_1.ArkInstanceFieldRef || value instanceof Expr_1.ArkInstanceInvokeExpr) {
424
+ this.inferValueType(value.getBase(), arkMethod);
425
+ }
426
+ if (value instanceof Ref_1.AbstractRef || value instanceof Expr_1.AbstractExpr || value instanceof Local_1.Local) {
427
+ value.inferType(arkMethod);
428
+ }
429
+ return value.getType();
430
+ }
431
+ static inferParameterType(param, arkMethod) {
432
+ let pType = param.getType();
433
+ const type = TypeInference.inferUnclearedType(pType, arkMethod.getDeclaringArkClass());
434
+ if (type) {
435
+ param.setType(type);
436
+ }
437
+ else {
438
+ if (pType instanceof TypeExpr_1.AbstractTypeExpr) {
439
+ pType.inferType(arkMethod);
440
+ }
441
+ }
442
+ }
443
+ static inferSignatureReturnType(oldSignature, arkMethod) {
444
+ if (oldSignature.getMethodSubSignature().getMethodName() === TSConst_1.CONSTRUCTOR_NAME) {
445
+ const newReturnType = new Type_1.ClassType(oldSignature.getDeclaringClassSignature());
446
+ oldSignature.getMethodSubSignature().setReturnType(newReturnType);
447
+ return;
448
+ }
449
+ const currReturnType = oldSignature.getType();
450
+ if (!this.isUnclearType(currReturnType)) {
451
+ return;
452
+ }
453
+ if (currReturnType instanceof TypeExpr_1.AbstractTypeExpr) {
454
+ currReturnType.inferType(arkMethod);
455
+ return;
456
+ }
457
+ if (currReturnType instanceof Type_1.ArrayType && currReturnType.getBaseType() instanceof TypeExpr_1.AbstractTypeExpr) {
458
+ currReturnType.getBaseType().inferType(arkMethod);
459
+ return;
460
+ }
461
+ const newReturnType = this.inferUnclearedType(currReturnType, arkMethod.getDeclaringArkClass());
462
+ if (newReturnType) {
463
+ oldSignature.getMethodSubSignature().setReturnType(newReturnType);
464
+ }
465
+ else if (arkMethod.getBody()) {
466
+ const typeMap = new Map();
467
+ for (let returnValue of arkMethod.getReturnValues()) {
468
+ const type = returnValue.getType();
469
+ if (type instanceof Type_1.UnionType) {
470
+ type.flatType().filter(t => !TypeInference.isUnclearType(t)).forEach(t => typeMap.set(t.toString(), t));
471
+ }
472
+ else if (!TypeInference.isUnclearType(type)) {
473
+ typeMap.set(type.toString(), type);
474
+ }
475
+ }
476
+ if (typeMap.size > 0) {
477
+ const types = Array.from(typeMap.values());
478
+ oldSignature.getMethodSubSignature().setReturnType(types.length === 1 ? types[0] : new Type_1.UnionType(types));
479
+ }
480
+ }
481
+ }
482
+ static inferGenericType(types, arkClass) {
483
+ types === null || types === void 0 ? void 0 : types.forEach(type => {
484
+ const defaultType = type.getDefaultType();
485
+ if (defaultType instanceof Type_1.UnclearReferenceType) {
486
+ const newDefaultType = TypeInference.inferUnclearRefName(defaultType.getName(), arkClass);
487
+ if (newDefaultType) {
488
+ type.setDefaultType(this.replaceTypeWithReal(newDefaultType));
489
+ }
490
+ }
491
+ const constraint = type.getConstraint();
492
+ if (constraint instanceof Type_1.UnclearReferenceType) {
493
+ const newConstraint = TypeInference.inferUnclearRefName(constraint.getName(), arkClass);
494
+ if (newConstraint) {
495
+ type.setConstraint(this.replaceTypeWithReal(newConstraint));
496
+ }
497
+ }
498
+ });
499
+ }
500
+ /**
501
+ * Infer type for a given {@link UnclearReferenceType} type.
502
+ * It returns original type.
503
+ * The original type is null if it failed to infer the type.
504
+ * @param urType
505
+ * @param arkClass
506
+ * @returns
507
+ */
508
+ static inferUnclearRefType(urType, arkClass) {
509
+ var _a;
510
+ const realTypes = urType.getGenericTypes();
511
+ this.inferRealGenericTypes(realTypes, arkClass);
512
+ if (urType.getName() === Builtin_1.Builtin.ARRAY) {
513
+ return new Type_1.ArrayType((_a = realTypes[0]) !== null && _a !== void 0 ? _a : Type_1.AnyType.getInstance(), 1);
514
+ }
515
+ let type = this.inferUnclearRefName(urType.getName(), arkClass);
516
+ return type ? this.replaceTypeWithReal(type, realTypes) : null;
517
+ }
518
+ /**
519
+ * Find out the original object and type for a given unclear reference type name.
520
+ * It returns original type.
521
+ * The original type is null if it failed to infer the type.
522
+ * @param refName
523
+ * @param arkClass
524
+ * @returns
525
+ */
526
+ static inferUnclearRefName(refName, arkClass) {
527
+ var _a;
528
+ if (!refName) {
529
+ return null;
530
+ }
531
+ //split and iterate to infer each type
532
+ const singleNames = refName.split('.');
533
+ let type = null;
534
+ for (let i = 0; i < singleNames.length; i++) {
535
+ let genericName = ValueUtil_1.EMPTY_STRING;
536
+ const name = singleNames[i].replace(/<(\w+)>/, (match, group1) => {
537
+ genericName = group1;
538
+ return ValueUtil_1.EMPTY_STRING;
539
+ });
540
+ if (i === 0) {
541
+ type = this.inferBaseType(name, arkClass);
542
+ }
543
+ else if (type) {
544
+ type = (_a = this.inferFieldType(type, name, arkClass)) === null || _a === void 0 ? void 0 : _a[1];
545
+ }
546
+ if (!type) {
547
+ return null;
548
+ }
549
+ if (genericName) {
550
+ const realTypes = genericName.split(',').map(generic => {
551
+ const realType = this.inferBaseType(generic, arkClass);
552
+ return realType !== null && realType !== void 0 ? realType : new Type_1.UnclearReferenceType(generic);
553
+ });
554
+ if (type instanceof Type_1.ClassType) {
555
+ type = new Type_1.ClassType(type.getClassSignature(), realTypes);
556
+ }
557
+ else if (type instanceof Type_1.FunctionType) {
558
+ type = new Type_1.FunctionType(type.getMethodSignature(), realTypes);
559
+ }
560
+ }
561
+ }
562
+ return type;
563
+ }
564
+ /**
565
+ * Find out the original object and type for a given base type and the field name.
566
+ * It returns an array with 2 items, original object and original type.
567
+ * The original object is null if there is no object, or it failed to find the object.
568
+ * The original type is null if it failed to infer the type.
569
+ * @param baseType
570
+ * @param fieldName
571
+ * @param declareClass
572
+ * @returns
573
+ */
574
+ static inferFieldType(baseType, fieldName, declareClass) {
575
+ if (baseType instanceof Type_1.AliasType) {
576
+ baseType = baseType.getOriginalType();
577
+ }
578
+ else if (baseType instanceof Type_1.UnionType && baseType.getCurrType()) {
579
+ baseType = baseType.getCurrType();
580
+ }
581
+ let propertyAndType = null;
582
+ if (baseType instanceof Type_1.ClassType) {
583
+ const arkClass = declareClass.getDeclaringArkFile().getScene().getClass(baseType.getClassSignature());
584
+ if (!arkClass && fieldName === Builtin_1.Builtin.ITERATOR_RESULT_VALUE && baseType.getClassSignature()
585
+ .getDeclaringFileSignature().getProjectName() === Builtin_1.Builtin.DUMMY_PROJECT_NAME) {
586
+ const types = baseType.getRealGenericTypes();
587
+ if (types && types.length > 0) {
588
+ propertyAndType = [null, types[0]];
589
+ }
590
+ }
591
+ if (!arkClass) {
592
+ return propertyAndType;
593
+ }
594
+ if (arkClass.isAnonymousClass()) {
595
+ const fieldType = this.inferUnclearRefName(fieldName, arkClass);
596
+ return fieldType ? [null, fieldType] : null;
597
+ }
598
+ const property = ModelUtils_1.ModelUtils.findPropertyInClass(fieldName, arkClass);
599
+ let propertyType = null;
600
+ if (property instanceof ArkField_1.ArkField) {
601
+ propertyType = property.getType();
602
+ }
603
+ else if (property) {
604
+ propertyType = this.parseArkExport2Type(property);
605
+ }
606
+ if (propertyType) {
607
+ propertyAndType = [property, propertyType];
608
+ }
609
+ }
610
+ else if (baseType instanceof Type_1.AnnotationNamespaceType) {
611
+ const namespace = declareClass.getDeclaringArkFile().getScene().getNamespace(baseType.getNamespaceSignature());
612
+ if (namespace) {
613
+ const property = ModelUtils_1.ModelUtils.findPropertyInNamespace(fieldName, namespace);
614
+ const propertyType = this.parseArkExport2Type(property);
615
+ if (propertyType) {
616
+ propertyAndType = [property, propertyType];
617
+ }
618
+ }
619
+ }
620
+ else {
621
+ logger.warn('infer unclear reference type fail: ' + fieldName);
622
+ }
623
+ return propertyAndType;
624
+ }
625
+ /**
626
+ * Find out the original object and type for a given base name.
627
+ * It returns original type.
628
+ * The original type is null if failed to infer the type.
629
+ * @param baseName
630
+ * @param arkClass
631
+ * @returns
632
+ */
633
+ static inferBaseType(baseName, arkClass) {
634
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
635
+ if (TSConst_1.SUPER_NAME === baseName) {
636
+ return this.parseArkExport2Type(arkClass.getSuperClass());
637
+ }
638
+ const field = (_d = (_c = (_b = (_a = ModelUtils_1.ModelUtils.getDefaultClass(arkClass)) === null || _a === void 0 ? void 0 : _a.getDefaultArkMethod()) === null || _b === void 0 ? void 0 : _b.getBody()) === null || _c === void 0 ? void 0 : _c.getLocals()) === null || _d === void 0 ? void 0 : _d.get(baseName);
639
+ if (field && !this.isUnclearType(field.getType())) {
640
+ return field.getType();
641
+ }
642
+ let arkExport = (_m = (_k = (_j = (_e = ModelUtils_1.ModelUtils.getClassWithName(baseName, arkClass)) !== null && _e !== void 0 ? _e : (_h = (_g = (_f = ModelUtils_1.ModelUtils.getDefaultClass(arkClass)) === null || _f === void 0 ? void 0 : _f.getDefaultArkMethod()) === null || _g === void 0 ? void 0 : _g.getBody()) === null || _h === void 0 ? void 0 : _h.getAliasTypeByName(baseName)) !== null && _j !== void 0 ? _j : ModelUtils_1.ModelUtils.getNamespaceWithName(baseName, arkClass)) !== null && _k !== void 0 ? _k : (_l = ModelUtils_1.ModelUtils.getDefaultClass(arkClass)) === null || _l === void 0 ? void 0 : _l.getMethodWithName(baseName)) !== null && _m !== void 0 ? _m : ModelUtils_1.ModelUtils.getArkExportInImportInfoWithName(baseName, arkClass.getDeclaringArkFile());
643
+ if (!arkExport && !arkClass.getDeclaringArkFile().getImportInfoBy(baseName)) {
644
+ arkExport = arkClass.getDeclaringArkFile().getScene().getSdkGlobal(baseName);
645
+ }
646
+ return this.parseArkExport2Type(arkExport);
647
+ }
648
+ static inferRealGenericTypes(realTypes, arkClass) {
649
+ if (!realTypes) {
650
+ return;
651
+ }
652
+ for (let i = 0; i < realTypes.length; i++) {
653
+ const mayType = realTypes[i];
654
+ if (this.isUnclearType(mayType)) {
655
+ const newType = this.inferUnclearedType(mayType, arkClass);
656
+ if (newType) {
657
+ realTypes[i] = newType;
658
+ }
659
+ }
660
+ }
661
+ }
662
+ static inferDynamicImportType(from, arkClass) {
663
+ var _a;
664
+ const importInfo = new ArkImport_1.ImportInfo();
665
+ importInfo.setNameBeforeAs(TSConst_1.ALL);
666
+ importInfo.setImportClauseName(TSConst_1.ALL);
667
+ importInfo.setImportFrom(from);
668
+ importInfo.setDeclaringArkFile(arkClass.getDeclaringArkFile());
669
+ return TypeInference.parseArkExport2Type((_a = importInfo.getLazyExportInfo()) === null || _a === void 0 ? void 0 : _a.getArkExport());
670
+ }
671
+ static replaceTypeWithReal(type, realTypes) {
672
+ var _a, _b, _c, _d, _e, _f, _g, _h;
673
+ if (type instanceof Type_1.ClassType) {
674
+ const replacedTypes = (_b = (_a = type.getRealGenericTypes()) === null || _a === void 0 ? void 0 : _a.map(g => this.replaceTypeWithReal(g, realTypes))) !== null && _b !== void 0 ? _b : realTypes;
675
+ return replacedTypes && replacedTypes.length > 0 ? new Type_1.ClassType(type.getClassSignature(), replacedTypes) : type;
676
+ }
677
+ else if (type instanceof Type_1.FunctionType) {
678
+ const replacedTypes = (_d = (_c = type.getRealGenericTypes()) === null || _c === void 0 ? void 0 : _c.map(g => this.replaceTypeWithReal(g, realTypes))) !== null && _d !== void 0 ? _d : realTypes;
679
+ return replacedTypes && replacedTypes.length > 0 ? new Type_1.FunctionType(type.getMethodSignature(), replacedTypes) : type;
680
+ }
681
+ else if (type instanceof Type_1.AliasType && realTypes) {
682
+ const newObjectType = this.replaceTypeWithReal(type.getOriginalType(), realTypes);
683
+ const replacedTypes = (_f = (_e = type.getRealGenericTypes()) === null || _e === void 0 ? void 0 : _e.map(g => this.replaceTypeWithReal(g, realTypes))) !== null && _f !== void 0 ? _f : realTypes;
684
+ if (replacedTypes.length > 0) {
685
+ const newAliasType = new Type_1.AliasType(type.getName(), newObjectType, type.getSignature(), type.getGenericTypes());
686
+ newAliasType.setRealGenericTypes(replacedTypes);
687
+ return newAliasType;
688
+ }
689
+ }
690
+ else if (type instanceof Type_1.UnionType && realTypes) {
691
+ const types = [];
692
+ type.flatType().forEach(t => types.push(this.replaceTypeWithReal(t, realTypes)));
693
+ return new Type_1.UnionType(types, this.replaceTypeWithReal(type.getCurrType(), realTypes));
694
+ }
695
+ else if (type instanceof Type_1.IntersectionType && realTypes) {
696
+ const types = [];
697
+ type.getTypes().forEach(t => types.push(this.replaceTypeWithReal(t, realTypes)));
698
+ return new Type_1.IntersectionType(types);
699
+ }
700
+ else if (type instanceof Type_1.ArrayType && realTypes) {
701
+ const replacedBaseType = this.replaceTypeWithReal(type.getBaseType(), realTypes);
702
+ return new Type_1.ArrayType(replacedBaseType, type.getDimension());
703
+ }
704
+ else if (type instanceof Type_1.TupleType && realTypes) {
705
+ let replacedTypes = [];
706
+ type.getTypes().forEach(t => { replacedTypes.push(this.replaceTypeWithReal(t, realTypes)); });
707
+ return new Type_1.TupleType(replacedTypes);
708
+ }
709
+ else if (type instanceof Type_1.GenericType) {
710
+ const realType = (_h = (_g = realTypes === null || realTypes === void 0 ? void 0 : realTypes[type.getIndex()]) !== null && _g !== void 0 ? _g : type.getDefaultType()) !== null && _h !== void 0 ? _h : type.getConstraint();
711
+ if (realType) {
712
+ return realType;
713
+ }
714
+ }
715
+ else if (type instanceof Type_1.AnyType) {
716
+ const realType = realTypes === null || realTypes === void 0 ? void 0 : realTypes[0];
717
+ if (realType) {
718
+ return realType;
719
+ }
720
+ }
721
+ return type;
722
+ }
723
+ static replaceAliasType(type) {
724
+ let aliasType = type;
725
+ while (aliasType instanceof Type_1.AliasType) {
726
+ aliasType = aliasType.getOriginalType();
727
+ }
728
+ return aliasType;
729
+ }
730
+ static inferFunctionType(argType, params, realTypes) {
731
+ if (!params) {
732
+ return;
733
+ }
734
+ argType.getMethodSignature().getMethodSubSignature().getParameters()
735
+ .filter(p => !p.getName().startsWith(Const_1.LEXICAL_ENV_NAME_PREFIX))
736
+ .forEach((p, i) => {
737
+ var _a;
738
+ let type = (_a = params === null || params === void 0 ? void 0 : params[i]) === null || _a === void 0 ? void 0 : _a.getType();
739
+ if (type instanceof Type_1.GenericType && realTypes) {
740
+ type = realTypes === null || realTypes === void 0 ? void 0 : realTypes[type.getIndex()];
741
+ }
742
+ if (type) {
743
+ p.setType(type);
744
+ }
745
+ });
746
+ }
747
+ }
748
+ exports.TypeInference = TypeInference;