@abaplint/core 2.120.82 → 2.120.84

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 (60) hide show
  1. package/build/abaplint.d.ts +19 -5
  2. package/build/src/abap/1_lexer/tokens/abstract_token.js +36 -12
  3. package/build/src/abap/5_syntax/_current_scope.js +14 -11
  4. package/build/src/abap/5_syntax/_eml.js +68 -0
  5. package/build/src/abap/5_syntax/_object_oriented.js +10 -0
  6. package/build/src/abap/5_syntax/spaghetti_scope.js +67 -9
  7. package/build/src/abap/5_syntax/statements/message.js +8 -0
  8. package/build/src/abap/5_syntax/statements/modify_entities.js +2 -0
  9. package/build/src/abap/5_syntax/statements/raise.js +6 -0
  10. package/build/src/abap/5_syntax/statements/read_entities.js +2 -0
  11. package/build/src/abap/5_syntax/syntax.js +2 -2
  12. package/build/src/bdef/bdef_parser.js +75 -0
  13. package/build/src/bdef/expressions/bdef_action.js +15 -0
  14. package/build/src/bdef/expressions/bdef_alias.js +12 -0
  15. package/build/src/bdef/expressions/bdef_association.js +15 -0
  16. package/build/src/bdef/expressions/bdef_association_body.js +18 -0
  17. package/build/src/bdef/expressions/bdef_authorization_context.js +16 -0
  18. package/build/src/bdef/expressions/bdef_cardinality.js +13 -0
  19. package/build/src/bdef/expressions/bdef_default_function.js +14 -0
  20. package/build/src/bdef/expressions/bdef_define_behavior.js +21 -0
  21. package/build/src/bdef/expressions/bdef_definition.js +12 -0
  22. package/build/src/bdef/expressions/bdef_determination.js +15 -0
  23. package/build/src/bdef/expressions/bdef_determine_action.js +15 -0
  24. package/build/src/bdef/expressions/bdef_event.js +14 -0
  25. package/build/src/bdef/expressions/bdef_field.js +13 -0
  26. package/build/src/bdef/expressions/bdef_field_path.js +13 -0
  27. package/build/src/bdef/expressions/bdef_header.js +15 -0
  28. package/build/src/bdef/expressions/bdef_hierarchy.js +16 -0
  29. package/build/src/bdef/expressions/bdef_implementation_in.js +12 -0
  30. package/build/src/bdef/expressions/bdef_implementation_type.js +19 -0
  31. package/build/src/bdef/expressions/bdef_key.js +14 -0
  32. package/build/src/bdef/expressions/bdef_mapping.js +19 -0
  33. package/build/src/bdef/expressions/bdef_name.js +13 -0
  34. package/build/src/bdef/expressions/bdef_operation.js +13 -0
  35. package/build/src/bdef/expressions/bdef_options.js +15 -0
  36. package/build/src/bdef/expressions/bdef_parameter.js +13 -0
  37. package/build/src/bdef/expressions/bdef_reference.js +15 -0
  38. package/build/src/bdef/expressions/bdef_result.js +13 -0
  39. package/build/src/bdef/expressions/bdef_side_effects.js +25 -0
  40. package/build/src/bdef/expressions/bdef_string.js +11 -0
  41. package/build/src/bdef/expressions/bdef_use.js +18 -0
  42. package/build/src/bdef/expressions/index.js +46 -0
  43. package/build/src/lsp/dump_scope.js +3 -2
  44. package/build/src/objects/behavior_definition.js +46 -8
  45. package/build/src/position.js +4 -1
  46. package/build/src/registry.js +1 -1
  47. package/build/src/rules/_procedure_analysis.js +366 -4
  48. package/build/src/rules/bdef_check_syntax.js +88 -0
  49. package/build/src/rules/bdef_parser_error.js +47 -0
  50. package/build/src/rules/boolean_flag_via_if.js +1 -8
  51. package/build/src/rules/cast_via_generic_reference.js +189 -0
  52. package/build/src/rules/clear_after_declaration.js +8 -7
  53. package/build/src/rules/clear_before_overwrite.js +179 -0
  54. package/build/src/rules/constant_condition.js +9 -8
  55. package/build/src/rules/copy_chain.js +80 -10
  56. package/build/src/rules/copy_of_initial_variable.js +4 -2
  57. package/build/src/rules/index.js +4 -0
  58. package/build/src/rules/single_use_expression_temp.js +544 -40
  59. package/build/src/rules/unnecessary_copy.js +7 -4
  60. package/package.json +1 -1
@@ -35,21 +35,51 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.SingleUseExpressionTemp = exports.SingleUseExpressionTempConf = void 0;
37
37
  const Expressions = __importStar(require("../abap/2_statements/expressions"));
38
+ const Statements = __importStar(require("../abap/2_statements/statements"));
38
39
  const nodes_1 = require("../abap/nodes");
39
40
  const _reference_1 = require("../abap/5_syntax/_reference");
40
41
  const edit_helper_1 = require("../edit_helper");
41
42
  const issue_1 = require("../issue");
43
+ const version_1 = require("../version");
42
44
  const _basic_rule_config_1 = require("./_basic_rule_config");
43
45
  const _irule_1 = require("./_irule");
44
46
  const _procedure_analysis_1 = require("./_procedure_analysis");
45
47
  class SingleUseExpressionTempConf extends _basic_rule_config_1.BasicRuleConfig {
46
48
  }
47
49
  exports.SingleUseExpressionTempConf = SingleUseExpressionTempConf;
48
- /** builtin functions whose result type follows the type of the argument */
49
- const NUMERIC_FUNCTIONS = new Set(["abs", "sign", "ceil", "floor", "trunc", "frac", "ipow", "nmax", "nmin",
50
- "round", "rescale"]);
51
50
  /** constructor expressions that can build the result directly in the target */
52
51
  const IN_PLACE = new Set(["VALUE", "CORRESPONDING", "COND", "SWITCH", "REDUCE", "FILTER"]);
52
+ /** constructor expressions with an explicit type: KEYWORD type( ... ) */
53
+ const CONSTRUCTORS = new Set(["VALUE", "CORRESPONDING", "COND", "SWITCH", "REDUCE", "FILTER", "CONV", "EXACT", "REF"]);
54
+ /** types that an integer literal converts to without an exception */
55
+ const LITERAL_CONVERSIONS = new Set(["I", "INT8", "F", "DECFLOAT16", "DECFLOAT34", "STRING"]);
56
+ /** where the variable is read */
57
+ var Placement;
58
+ (function (Placement) {
59
+ /** y = v. */
60
+ Placement["Move"] = "move";
61
+ /** v->m( ) or v->attr */
62
+ Placement["Receiver"] = "receiver";
63
+ /** m( v ), m( p = v ), CALL METHOD m EXPORTING p = v, NEW c( p = v ), CREATE OBJECT o EXPORTING p = v */
64
+ Placement["Parameter"] = "parameter";
65
+ /** APPEND v TO itab. */
66
+ Placement["Append"] = "append";
67
+ /** IF v = x., ELSEIF v = x., xsdbool( v = x ) */
68
+ Placement["Comparison"] = "comparison";
69
+ /** |{ v }| */
70
+ Placement["Template"] = "template";
71
+ })(Placement || (Placement = {}));
72
+ var ExpressionKind;
73
+ (function (ExpressionKind) {
74
+ /** literal, string template without embeddings, CONV t( literal ) or a constant: the same value everywhere */
75
+ ExpressionKind["Constant"] = "constant";
76
+ /** reads only private variables and constants, cannot raise an exception or run other code */
77
+ ExpressionKind["Pure"] = "pure";
78
+ /** a data object that other code can change */
79
+ ExpressionKind["Data"] = "data";
80
+ /** method calls, NEW and everything else */
81
+ ExpressionKind["Other"] = "other";
82
+ })(ExpressionKind || (ExpressionKind = {}));
53
83
  class SingleUseExpressionTemp {
54
84
  constructor() {
55
85
  this.conf = new SingleUseExpressionTempConf();
@@ -58,28 +88,62 @@ class SingleUseExpressionTemp {
58
88
  return {
59
89
  key: "single_use_expression_temp",
60
90
  title: "Single use expression temporary",
61
- shortDescription: `Finds DATA(a) = <expression>. directly followed by x = a., assign the expression to x`,
62
- extendedInformation: `Reports DATA(a) = <expression>. when all of the following hold:
91
+ shortDescription: `Finds a local variable that only passes the value of an expression on to its single use`,
92
+ extendedInformation: `Reports DATA(v) = <expression>. and DATA v TYPE t. [CLEAR v.] v = <expression>.
93
+ when all of the following hold:
63
94
 
64
- * a is a local variable of a METHOD, FORM or FUNCTION, with no other reference than the next statement,
65
- which is x = a., in the same block
95
+ * v is a local variable of a METHOD, FORM or FUNCTION, its address is never taken. DATA(v) has no other
96
+ reference than its single read; DATA v TYPE t. without VALUE and not chained, or DATA(v) = VALUE t( ). with
97
+ nothing in the parentheses, has exactly one write besides CLEARs, v = <expression>., all CLEARs are before it,
98
+ and exactly one read after it
99
+ * the read is one of
100
+ * the complete source of y = v.
101
+ * the receiver of a method call or attribute access: v->m( ), v->attr
102
+ * an actual parameter for an importing parameter: m( v ), m( p = v ), CALL METHOD m EXPORTING p = v,
103
+ NEW c( p = v ) or CREATE OBJECT o EXPORTING p = v
104
+ * APPEND v TO itab.
105
+ * an operand of a comparison with =, <>, <, >, <=, >= or their word forms, in IF, ELSEIF or xsdbool( )
106
+ * an embedding in a string template: |{ v }|
107
+ * the read is in the statement directly after the assignment, in the same block, ignoring declarations like
108
+ DATA and TYPES in between, unless the expression is
109
+ constant: a literal, a string template without embeddings, CONV t( <integer literal> ) to a type that cannot
110
+ overflow, or a constant. Then the assignment must be executed before the read on every path: the read is in
111
+ the block of the assignment or a nested block
66
112
  * the result type of the expression does not depend on the target: no arithmetic or bit operators, no unary
67
- minus, no numeric functions like abs( ) or trunc( ) whose type follows the argument, no CONV or EXACT,
68
- no # type inference at the top. String templates, && concatenations, literals, method calls, NEW, CAST,
113
+ minus, no numeric functions like abs( ) or trunc( ) whose type follows the argument, no CONV or EXACT except
114
+ the constant CONV t( literal ), no # type inference at the top. String templates, && concatenations, literals, method calls, NEW, CAST,
69
115
  constructor expressions with explicit type and table expressions are fine
116
+ * for DATA v TYPE t. and DATA(v) = VALUE t( )., the static type of the expression is identical to t. Into y = v. also a reference whose
117
+ static type is a subtype of t, unless y is an inline declaration, and an expression whose type depends on the
118
+ target, like arithmetic, if y has a type identical to t
70
119
  * VALUE, CORRESPONDING, COND, SWITCH, REDUCE and FILTER can build their result directly in the target,
71
- they are only inlined into a new variable, or a local variable that they do not read and whose address
120
+ they are only moved into a new variable, or a local variable that they do not read and whose address
72
121
  is never taken
73
- * plain data objects are left for unnecessary_copy
122
+ * the order of evaluation does not change: if the expression is not constant, nothing that is evaluated before the
123
+ read in the same statement calls a method or function, constructs an object, accesses a table or an attribute
124
+ via a reference, does arithmetic, or reads data other than local variables, pass-by-value parameters and
125
+ constants; the read is not inside COND, SWITCH, VALUE or other constructor expressions except NEW, and not
126
+ after AND, OR or EQUIV, where it might not be evaluated. This does not apply to expressions that only read
127
+ local variables and cannot raise an exception
128
+ * a data object that other code can change is only moved if it keeps its value until the read, see
129
+ unnecessary_copy, a data object read by DATA(v) = <data object>. is left for unnecessary_copy, and a local
130
+ variable copied into y = v. for copy_chain
131
+ * a character constant in IF, ELSEIF, WHILE, CHECK, CASE or WHEN is left for constant_condition
74
132
 
75
133
  Objects with syntax errors, macros or unknown statements are skipped, as are procedures with dynamic code,
76
134
  INCLUDE, test seams, enhancements or native SQL.
77
135
 
78
- The quick fix deletes the declaration and writes x = <expression>. No release requirement.`,
136
+ The quick fix deletes the declaration, the CLEARs and the assignment, and replaces the read of v with the
137
+ expression. Moving the expression of DATA v TYPE t. into other places than y = v. requires 7.40 SP02 or above,
138
+ ABAP Cloud or open-abap.`,
79
139
  tags: [_irule_1.RuleTag.Quickfix],
80
140
  badExample: `DATA(lo_conf) = NEW zcl_conf( ).
81
- mo_conf = lo_conf.`,
82
- goodExample: `mo_conf = NEW zcl_conf( ).`,
141
+ mo_conf = lo_conf.
142
+ DATA lv_key TYPE string.
143
+ lv_key = |abapdoc|.
144
+ lo_meta = NEW zcl_meta( iv_key = lv_key ).`,
145
+ goodExample: `mo_conf = NEW zcl_conf( ).
146
+ lo_meta = NEW zcl_meta( iv_key = |abapdoc| ).`,
83
147
  };
84
148
  }
85
149
  getConfig() {
@@ -98,61 +162,501 @@ mo_conf = lo_conf.`,
98
162
  if (analysis === undefined) {
99
163
  return [];
100
164
  }
165
+ const config = this.reg.getConfig();
166
+ const expressions = (0, version_1.releaseAtLeast)(config.getRelease(), version_1.Release.v740sp02)
167
+ || config.getLanguageVersion() === version_1.LanguageVersion.Cloud || config.isOpenABAP();
101
168
  const issues = [];
102
169
  for (const procedure of analysis.procedures) {
103
170
  if (procedure.supported === false) {
104
171
  continue;
105
172
  }
106
173
  for (const variable of procedure.locals()) {
107
- const declaration = variable.declaration;
108
- const move = variable.inline ? (0, _procedure_analysis_1.plainMove)(declaration) : undefined;
109
- const read = variable.occurrences[0];
110
- if (move === undefined || move.operator !== "="
111
- || move.target.getChildren().length !== 1
112
- || move.target.findDirectExpression(Expressions.InlineData) === undefined
113
- || variable.occurrences.length !== 1 || read.kind !== _procedure_analysis_1.OccurrenceKind.Read
114
- || read.statement !== procedure.statements[procedure.index(declaration) + 1]
115
- || procedure.block(read.statement) !== procedure.block(declaration)
116
- || !procedure.isSimple(read.statement)) {
174
+ const temp = (_a = (variable.inline ? this.inlineTemp(procedure, variable) : undefined)) !== null && _a !== void 0 ? _a : this.declaredTemp(variable);
175
+ const read = variable.reads()[0];
176
+ if (temp === undefined || read === undefined) {
117
177
  continue;
118
178
  }
119
- const use = (0, _procedure_analysis_1.plainMove)(read.statement);
120
- if (use === undefined || use.operator !== "="
121
- || ((_a = (0, _procedure_analysis_1.singleField)(use.source)) === null || _a === void 0 ? void 0 : _a.getStart().equals(read.token.getStart())) !== true
122
- || (0, _procedure_analysis_1.dataSource)(procedure, move.source) !== undefined
123
- || !this.targetIndependent(procedure, move.source)
124
- || !this.noAliasing(procedure, move.source, use.target)) {
179
+ const placement = this.placement(procedure, read.statement, read.token);
180
+ if (placement === undefined
181
+ || (!variable.inline && placement !== Placement.Move && expressions === false)
182
+ || !this.valid(procedure, temp, read, placement)) {
125
183
  continue;
126
184
  }
127
- const fix = (0, _procedure_analysis_1.mergeEdits)(procedure.file, [
128
- edit_helper_1.EditHelper.replaceToken(procedure.file, read.token, (0, _procedure_analysis_1.nodeText)(procedure.file, move.source)),
129
- (0, _procedure_analysis_1.deleteStatement)(procedure.file, declaration)
130
- ]);
131
- const message = "Assign the expression directly instead of via " + variable.identifier.getName();
132
- issues.push(issue_1.Issue.atStatement(procedure.file, declaration, message, this.getMetadata().key, this.conf.severity, fix));
185
+ const edits = [edit_helper_1.EditHelper.replaceToken(procedure.file, read.token, (0, _procedure_analysis_1.nodeText)(procedure.file, temp.source))];
186
+ for (const statement of temp.remove) {
187
+ edits.push((0, _procedure_analysis_1.deleteStatement)(procedure.file, statement));
188
+ }
189
+ const fix = (0, _procedure_analysis_1.mergeEdits)(procedure.file, edits);
190
+ const message = "Use the expression directly instead of via " + variable.identifier.getName();
191
+ issues.push(issue_1.Issue.atStatement(procedure.file, temp.write, message, this.getMetadata().key, this.conf.severity, fix));
133
192
  }
134
193
  }
135
194
  return issues;
136
195
  }
196
+ /** DATA(v) = <expression>. with a single read, plain data objects are left for unnecessary_copy */
197
+ inlineTemp(procedure, variable) {
198
+ const declaration = variable.declaration;
199
+ const move = (0, _procedure_analysis_1.plainMove)(declaration);
200
+ if (move === undefined || move.operator !== "="
201
+ || move.target.getChildren().length !== 1
202
+ || move.target.findDirectExpression(Expressions.InlineData) === undefined
203
+ || variable.occurrences.length !== 1 || variable.occurrences[0].kind !== _procedure_analysis_1.OccurrenceKind.Read
204
+ || (0, _procedure_analysis_1.dataSource)(procedure, move.source) !== undefined
205
+ || !this.targetIndependent(procedure, move.source)) {
206
+ return undefined;
207
+ }
208
+ return { variable, write: declaration, source: move.source, remove: [declaration], typed: false };
209
+ }
210
+ /** DATA v TYPE t. or DATA(v) = VALUE t( )., [CLEAR v.] v = <expression>. with a single read after it */
211
+ declaredTemp(variable) {
212
+ var _a, _b;
213
+ const declaration = variable.declaration;
214
+ if ((0, _procedure_analysis_1.initialDeclaration)(variable) === undefined || declaration.getColon() !== undefined || variable.escapes()) {
215
+ return undefined;
216
+ }
217
+ const clears = [];
218
+ const writes = [];
219
+ for (const write of variable.writes()) {
220
+ if (((_a = (0, _procedure_analysis_1.plainClear)(write.statement)) === null || _a === void 0 ? void 0 : _a.getStart().equals(write.token.getStart())) === true
221
+ && write.statement.getColon() === undefined) {
222
+ clears.push(write);
223
+ }
224
+ else {
225
+ writes.push(write);
226
+ }
227
+ }
228
+ const reads = variable.reads();
229
+ if (writes.length !== 1 || reads.length !== 1) {
230
+ return undefined;
231
+ }
232
+ const write = writes[0];
233
+ const move = (0, _procedure_analysis_1.plainMove)(write.statement);
234
+ if (move === undefined || move.operator !== "="
235
+ || ((_b = (0, _procedure_analysis_1.singleTarget)(move.target)) === null || _b === void 0 ? void 0 : _b.getStart().equals(write.token.getStart())) !== true
236
+ || clears.some(c => c.index > write.index)
237
+ || reads[0].index <= write.index) {
238
+ return undefined;
239
+ }
240
+ return { variable, write: write.statement, source: move.source,
241
+ remove: [declaration, ...clears.map(c => c.statement), write.statement], typed: true };
242
+ }
243
+ placement(procedure, statement, token) {
244
+ var _a, _b, _c, _d;
245
+ if (statement.getColon() !== undefined) {
246
+ return undefined;
247
+ }
248
+ const move = (0, _procedure_analysis_1.plainMove)(statement);
249
+ if (move !== undefined && move.operator === "=" && ((_a = (0, _procedure_analysis_1.singleField)(move.source)) === null || _a === void 0 ? void 0 : _a.getStart().equals(token.getStart())) === true) {
250
+ return Placement.Move;
251
+ }
252
+ const path = (0, _procedure_analysis_1.expressionPath)(statement, token);
253
+ if (path.some(e => e.get() instanceof Expressions.Target || e.get() instanceof Expressions.MethodSource
254
+ || e.get() instanceof Expressions.Dereference)) {
255
+ return undefined;
256
+ }
257
+ const tokens = procedure.tokensOf(statement);
258
+ const index = tokens.findIndex(t => t.getStart().equals(token.getStart()));
259
+ if (((_b = tokens[index + 1]) === null || _b === void 0 ? void 0 : _b.getStr()) === "->") {
260
+ return ((_c = tokens[index + 2]) === null || _c === void 0 ? void 0 : _c.getStr()) === "*" ? undefined : Placement.Receiver;
261
+ }
262
+ let sourceIndex = path.length - 1;
263
+ while (sourceIndex >= 0 && !(path[sourceIndex].get() instanceof Expressions.Source)
264
+ && !(path[sourceIndex].get() instanceof Expressions.SimpleSource4)) {
265
+ sourceIndex--;
266
+ }
267
+ const source = path[sourceIndex];
268
+ if (source === undefined || source.countTokens() !== 1
269
+ || source.findDirectExpression(Expressions.FieldChain) === undefined) {
270
+ return undefined;
271
+ }
272
+ const parent = path[sourceIndex - 1];
273
+ if (parent === undefined) {
274
+ return statement.get() instanceof Statements.Append && statement.getChildren()[1] === source
275
+ ? Placement.Append : undefined;
276
+ }
277
+ else if (parent.get() instanceof Expressions.StringTemplateSource) {
278
+ return Placement.Template;
279
+ }
280
+ else if (parent.get() instanceof Expressions.Compare) {
281
+ if (parent.findDirectExpression(Expressions.CompareOperator) === undefined) {
282
+ return undefined;
283
+ }
284
+ let owner = sourceIndex - 2;
285
+ while (((_d = path[owner]) === null || _d === void 0 ? void 0 : _d.get()) instanceof Expressions.Cond) {
286
+ owner--;
287
+ }
288
+ if (owner === sourceIndex - 2) {
289
+ return undefined;
290
+ }
291
+ else if (owner < 0) {
292
+ const type = statement.get();
293
+ return type instanceof Statements.If || type instanceof Statements.ElseIf ? Placement.Comparison : undefined;
294
+ }
295
+ const keyword = path[owner].getFirstToken().getStr().toUpperCase();
296
+ return path[owner].get() instanceof Expressions.Source && (keyword === "XSDBOOL" || keyword === "BOOLC")
297
+ ? Placement.Comparison : undefined;
298
+ }
299
+ return (0, _procedure_analysis_1.formalParameter)(procedure, statement, token) !== undefined ? Placement.Parameter : undefined;
300
+ }
301
+ valid(procedure, temp, read, placement) {
302
+ const statement = read.statement;
303
+ const kind = this.kind(procedure, temp.source);
304
+ if (kind === ExpressionKind.Constant) {
305
+ if (!procedure.dominates(temp.write, statement)
306
+ || (!temp.typed && (0, _procedure_analysis_1.isCondition)(statement) && (0, _procedure_analysis_1.characterConstant)(procedure, temp.source) !== undefined)) {
307
+ return false;
308
+ }
309
+ }
310
+ else if (statement !== procedure.nextExecuted(temp.write)
311
+ || !(procedure.isSimple(statement) || statement.get() instanceof Statements.If)) {
312
+ return false;
313
+ }
314
+ if (!this.typeMatches(procedure, temp, statement, placement)) {
315
+ return false;
316
+ }
317
+ else if (placement === Placement.Receiver && !this.chainable(temp.source)) {
318
+ return false;
319
+ }
320
+ const data = (0, _procedure_analysis_1.dataSource)(procedure, temp.source);
321
+ if (kind === ExpressionKind.Constant) {
322
+ return true;
323
+ }
324
+ else if ((data === null || data === void 0 ? void 0 : data.kind) === _procedure_analysis_1.SourceKind.Local && (0, _procedure_analysis_1.singleField)(temp.source) !== undefined && placement === Placement.Move) {
325
+ // left for copy_chain
326
+ return false;
327
+ }
328
+ else if (!this.notWrittenIn(procedure, temp.source, statement)) {
329
+ return false;
330
+ }
331
+ else if (kind === ExpressionKind.Pure) {
332
+ return true;
333
+ }
334
+ else if (!this.evaluatedFirst(procedure, statement, read.token) || !this.alwaysEvaluated(statement, read.token)) {
335
+ return false;
336
+ }
337
+ return data === undefined || (0, _procedure_analysis_1.unchangedBetween)(procedure, data, temp.write, statement, read.token);
338
+ }
339
+ kind(procedure, source) {
340
+ var _a;
341
+ const data = (0, _procedure_analysis_1.dataSource)(procedure, source);
342
+ if (this.constant(source) || (data === null || data === void 0 ? void 0 : data.kind) === _procedure_analysis_1.SourceKind.Immutable) {
343
+ return ExpressionKind.Constant;
344
+ }
345
+ else if (((data === null || data === void 0 ? void 0 : data.kind) === _procedure_analysis_1.SourceKind.Local && ((_a = data.base) === null || _a === void 0 ? void 0 : _a.escapes()) === false) || this.pure(procedure, source)) {
346
+ return ExpressionKind.Pure;
347
+ }
348
+ else if (data !== undefined) {
349
+ return ExpressionKind.Data;
350
+ }
351
+ return ExpressionKind.Other;
352
+ }
353
+ /** a literal, a string template without embeddings, or CONV t( literal ) that cannot raise an exception */
354
+ constant(source) {
355
+ var _a;
356
+ const children = source.getChildren();
357
+ const first = children[0];
358
+ if (children.length === 1 && first instanceof nodes_1.ExpressionNode) {
359
+ return first.get() instanceof Expressions.Constant
360
+ || (first.get() instanceof Expressions.StringTemplate
361
+ && first.findDirectExpression(Expressions.StringTemplateSource) === undefined);
362
+ }
363
+ if (children.length !== 5 || source.getFirstToken().getStr().toUpperCase() !== "CONV") {
364
+ return false;
365
+ }
366
+ const type = ((_a = source.findDirectExpression(Expressions.TypeNameOrInfer)) === null || _a === void 0 ? void 0 : _a.concatTokens().toUpperCase()) || "";
367
+ const body = source.findDirectExpression(Expressions.ConvBody);
368
+ const value = (body === null || body === void 0 ? void 0 : body.getChildren().length) === 1 ? body.findDirectExpression(Expressions.Source) : undefined;
369
+ const literal = (value === null || value === void 0 ? void 0 : value.getChildren().length) === 1 ? value.findDirectExpression(Expressions.Constant) : undefined;
370
+ return literal !== undefined && LITERAL_CONVERSIONS.has(type)
371
+ && (literal.findDirectExpression(Expressions.Integer) !== undefined || type === "STRING");
372
+ }
373
+ /** reads only private variables and constants, and cannot raise an exception: string templates and
374
+ * concatenations of such fields, xsdbool( ) and boolc( ) of IS INITIAL, IS BOUND, IS INSTANCE OF and
375
+ * comparisons without conversion */
376
+ pure(procedure, source) {
377
+ const children = source.getChildren();
378
+ const first = children[0];
379
+ const keyword = first instanceof nodes_1.TokenNode ? first.getFirstToken().getStr().toUpperCase() : "";
380
+ if (children.length === 1 && first instanceof nodes_1.ExpressionNode) {
381
+ return this.pureOperand(procedure, first);
382
+ }
383
+ else if (children.length === 3 && children[1] instanceof nodes_1.TokenNode && children[1].getFirstToken().getStr() === "&&"
384
+ && children[0] instanceof nodes_1.ExpressionNode && children[2] instanceof nodes_1.ExpressionNode) {
385
+ return this.pureOperand(procedure, children[0]) && this.pure(procedure, children[2]);
386
+ }
387
+ else if (children.length === 4 && (keyword === "XSDBOOL" || keyword === "BOOLC")) {
388
+ const cond = source.findDirectExpression(Expressions.Cond);
389
+ return cond !== undefined && this.pureCondition(procedure, cond);
390
+ }
391
+ return false;
392
+ }
393
+ /** a literal, a private variable or constant, or a string template that embeds only those, without formatting */
394
+ pureOperand(procedure, operand) {
395
+ if (operand.get() instanceof Expressions.Constant) {
396
+ return true;
397
+ }
398
+ else if (operand.get() instanceof Expressions.FieldChain) {
399
+ return operand.countTokens() === 1 && this.privateField(procedure, operand.getFirstToken());
400
+ }
401
+ else if (operand.get() instanceof Expressions.StringTemplate) {
402
+ return operand.findDirectExpressions(Expressions.StringTemplateSource).every(s => s.getChildren().length === 1
403
+ && this.operandType(procedure, s.findDirectExpression(Expressions.Source)) !== undefined);
404
+ }
405
+ return false;
406
+ }
407
+ pureCondition(procedure, cond) {
408
+ for (const child of cond.getChildren()) {
409
+ if (child instanceof nodes_1.TokenNode) {
410
+ continue;
411
+ }
412
+ else if (child.get() instanceof Expressions.Cond) {
413
+ if (!this.pureCondition(procedure, child)) {
414
+ return false;
415
+ }
416
+ }
417
+ else if (child.get() instanceof Expressions.Compare) {
418
+ const operands = child.findDirectExpressions(Expressions.Source);
419
+ const words = child.getChildren().filter(c => c instanceof nodes_1.TokenNode).map(c => c.getFirstToken().getStr().toUpperCase());
420
+ const operator = child.findDirectExpression(Expressions.CompareOperator);
421
+ const types = operands.map(o => this.operandType(procedure, o));
422
+ if (types.some(t => t === undefined)) {
423
+ return false;
424
+ }
425
+ else if (operands.length === 1 && words[words.length - 1] !== "SUPPLIED" && words[words.length - 1] !== "REQUESTED"
426
+ && words.includes("IS") && child.findDirectExpression(Expressions.CompareOperator) === undefined) {
427
+ // IS [NOT] INITIAL, IS [NOT] BOUND, IS [NOT] INSTANCE OF
428
+ continue;
429
+ }
430
+ else if (operands.length === 2 && operator !== undefined
431
+ && ((0, _procedure_analysis_1.identicalTypes)(types[0], types[1]) || ((0, _procedure_analysis_1.isReferenceType)(types[0]) && (0, _procedure_analysis_1.isReferenceType)(types[1])))) {
432
+ continue;
433
+ }
434
+ return false;
435
+ }
436
+ else {
437
+ return false;
438
+ }
439
+ }
440
+ return true;
441
+ }
442
+ /** type of a private variable, a constant or a literal, which can be read without side effects */
443
+ operandType(procedure, source) {
444
+ if (source.getChildren().length !== 1 || source.countTokens() !== 1) {
445
+ return undefined;
446
+ }
447
+ const constant = source.findDirectExpression(Expressions.Constant);
448
+ if (constant !== undefined) {
449
+ return (0, _procedure_analysis_1.literalType)(constant);
450
+ }
451
+ const field = (0, _procedure_analysis_1.singleField)(source);
452
+ if (field === undefined || !this.privateField(procedure, field)) {
453
+ return undefined;
454
+ }
455
+ return procedure.identifierAt(field).getType();
456
+ }
457
+ privateField(procedure, token) {
458
+ const identifier = procedure.identifierAt(token);
459
+ return (0, _procedure_analysis_1.isImmutable)(identifier) || procedure.private(identifier) !== undefined;
460
+ }
461
+ /** the variables read by the expression are not changed by the statement that reads the temporary,
462
+ * except as the target of a move, which is written after its source is evaluated */
463
+ notWrittenIn(procedure, source, statement) {
464
+ var _a;
465
+ const target = (_a = (0, _procedure_analysis_1.plainMove)(statement)) === null || _a === void 0 ? void 0 : _a.target;
466
+ const inTarget = (token) => target !== undefined
467
+ && !token.getStart().isBefore(target.getFirstToken().getStart()) && !target.getLastToken().getStart().isBefore(token.getStart());
468
+ for (const token of source.getTokens()) {
469
+ const variable = procedure.variable(procedure.identifierAt(token));
470
+ if ((variable === null || variable === void 0 ? void 0 : variable.occurrences.some(o => o.statement === statement && o.kind !== _procedure_analysis_1.OccurrenceKind.Read && !inTarget(o.token))) === true) {
471
+ return false;
472
+ }
473
+ }
474
+ return true;
475
+ }
476
+ /** nothing evaluated before the token in the statement can run other code, raise an exception or read data that
477
+ * other code changes. The target of a move is evaluated after its source */
478
+ evaluatedFirst(procedure, statement, token) {
479
+ var _a, _b;
480
+ const position = token.getStart();
481
+ const before = (node) => node.getLastToken().getStart().isBefore(position);
482
+ for (const type of [Expressions.MethodCall, Expressions.NewObject, Expressions.Cast, Expressions.TableExpression,
483
+ Expressions.ArithOperator, Expressions.Dereference]) {
484
+ if (statement.findAllExpressionsRecursive(type).some(before)) {
485
+ return false;
486
+ }
487
+ }
488
+ for (const source of statement.findAllExpressionsRecursive(Expressions.Source)) {
489
+ const keyword = source.getFirstToken().getStr().toUpperCase();
490
+ if (before(source) && CONSTRUCTORS.has(keyword) && source.findDirectExpression(Expressions.TypeNameOrInfer) !== undefined) {
491
+ return false;
492
+ }
493
+ }
494
+ // the target of a move or CREATE OBJECT is written after the source and the parameters are evaluated
495
+ let target = (_a = (0, _procedure_analysis_1.plainMove)(statement)) === null || _a === void 0 ? void 0 : _a.target;
496
+ if (statement.get() instanceof Statements.CreateObject) {
497
+ target = statement.findDirectExpression(Expressions.Target);
498
+ if ((target === null || target === void 0 ? void 0 : target.countTokens()) !== 1) {
499
+ return false;
500
+ }
501
+ }
502
+ const tokens = procedure.tokensOf(statement);
503
+ for (let i = 0; i < tokens.length && tokens[i].getStart().isBefore(position); i++) {
504
+ if (target !== undefined && !tokens[i].getStart().isBefore(target.getFirstToken().getStart())
505
+ && !target.getLastToken().getStart().isBefore(tokens[i].getStart())) {
506
+ continue;
507
+ }
508
+ const references = procedure.analysis.referencesAt(procedure.file.getFilename(), tokens[i]);
509
+ if (references.some(r => r.referenceType === _reference_1.ReferenceType.DataReadReference
510
+ || r.referenceType === _reference_1.ReferenceType.DataWriteReference)) {
511
+ // a private variable or constant, and a reference only to call a method: v->m( ... token ... )
512
+ if (!this.privateField(procedure, tokens[i])
513
+ || (((_b = tokens[i + 1]) === null || _b === void 0 ? void 0 : _b.getStr()) === "->"
514
+ && (tokens[i + 2] === undefined || procedure.methodAt(tokens[i + 2]) === undefined))) {
515
+ return false;
516
+ }
517
+ }
518
+ }
519
+ return true;
520
+ }
521
+ /** the token is evaluated whenever the statement is: not in a branch of COND or SWITCH, not in an iteration of
522
+ * VALUE, REDUCE or FILTER, and not after AND, OR or EQUIV */
523
+ alwaysEvaluated(statement, token) {
524
+ const position = token.getStart();
525
+ for (const node of (0, _procedure_analysis_1.expressionPath)(statement, token)) {
526
+ if (node.get() instanceof Expressions.Source) {
527
+ const keyword = node.getFirstToken().getStr().toUpperCase();
528
+ if (CONSTRUCTORS.has(keyword) && node.findDirectExpression(Expressions.TypeNameOrInfer) !== undefined) {
529
+ return false;
530
+ }
531
+ }
532
+ else if (node.get() instanceof Expressions.Cond) {
533
+ for (const child of node.getChildren()) {
534
+ const word = child.getFirstToken().getStr().toUpperCase();
535
+ if (child instanceof nodes_1.TokenNode && child.getFirstToken().getStart().isBefore(position)
536
+ && (word === "AND" || word === "OR" || word === "EQUIV")) {
537
+ return false;
538
+ }
539
+ }
540
+ }
541
+ else if (node.get() instanceof Expressions.For || node.get() instanceof Expressions.Let) {
542
+ return false;
543
+ }
544
+ }
545
+ return true;
546
+ }
547
+ /** the expression can be followed by -> */
548
+ chainable(source) {
549
+ const children = source.getChildren();
550
+ return children.length === 1 && (children[0].get() instanceof Expressions.FieldChain
551
+ || children[0].get() instanceof Expressions.MethodCallChain);
552
+ }
553
+ typeMatches(procedure, temp, statement, placement) {
554
+ const use = placement === Placement.Move ? (0, _procedure_analysis_1.plainMove)(statement) : undefined;
555
+ if (use !== undefined && !this.noAliasing(procedure, temp.source, use.target)) {
556
+ return false;
557
+ }
558
+ else if (!temp.typed) {
559
+ // the variable has the type of the expression
560
+ return true;
561
+ }
562
+ const type = temp.variable.identifier.getType();
563
+ const independent = this.targetIndependent(procedure, temp.source);
564
+ if (use === undefined) {
565
+ return independent && this.sameType(procedure, temp, type);
566
+ }
567
+ const inline = use.target.findFirstExpression(Expressions.InlineData) !== undefined;
568
+ if (independent) {
569
+ if (this.sameType(procedure, temp, type)) {
570
+ return true;
571
+ }
572
+ // an up cast into v, and v into y, is an up cast into y
573
+ const expression = (0, _procedure_analysis_1.expressionType)(procedure, temp.source);
574
+ const constructed = this.constructedType(temp.source);
575
+ const reference = expression === undefined ? (constructed === null || constructed === void 0 ? void 0 : constructed.reference) === true && constructed.name !== "#"
576
+ : (0, _procedure_analysis_1.isReferenceType)(expression);
577
+ return inline === false && reference && (0, _procedure_analysis_1.isReferenceType)(type);
578
+ }
579
+ // the type of the expression follows the target
580
+ const target = inline ? undefined : (0, _procedure_analysis_1.targetType)(procedure, use.target);
581
+ return target !== undefined && (0, _procedure_analysis_1.identicalTypes)(target, type);
582
+ }
583
+ /** the expression has exactly the type of the declaration DATA v TYPE t. */
584
+ sameType(procedure, temp, type) {
585
+ const expression = (0, _procedure_analysis_1.expressionType)(procedure, temp.source);
586
+ if (expression !== undefined) {
587
+ return (0, _procedure_analysis_1.identicalTypes)(expression, type);
588
+ }
589
+ // constructor expressions with an explicit type, compared by the name of the type
590
+ const declared = this.declaredType(temp.variable);
591
+ const constructed = this.constructedType(temp.source);
592
+ return declared !== undefined && constructed !== undefined && constructed.name !== "#"
593
+ && constructed.name === declared.name && constructed.reference === declared.reference;
594
+ }
595
+ /** the type name of DATA v TYPE t., DATA v TYPE REF TO t. or DATA(v) = VALUE t( ). */
596
+ declaredType(variable) {
597
+ var _a, _b;
598
+ const declaration = variable.declaration;
599
+ if ((0, _procedure_analysis_1.initialDeclaration)(variable) === _procedure_analysis_1.InitialDeclaration.Value) {
600
+ return this.constructedType((0, _procedure_analysis_1.plainMove)(declaration).source);
601
+ }
602
+ const type = (_a = declaration.findDirectExpression(Expressions.DataDefinition)) === null || _a === void 0 ? void 0 : _a.findDirectExpression(Expressions.Type);
603
+ const name = (_b = type === null || type === void 0 ? void 0 : type.findDirectExpression(Expressions.TypeName)) === null || _b === void 0 ? void 0 : _b.concatTokens().toUpperCase();
604
+ const words = type === null || type === void 0 ? void 0 : type.getTokens().map(t => t.getStr().toUpperCase()).join(" ");
605
+ if (name === undefined) {
606
+ return undefined;
607
+ }
608
+ else if (words === "TYPE " + name) {
609
+ return { name, reference: false };
610
+ }
611
+ else if (words === "TYPE REF TO " + name) {
612
+ return { name, reference: true };
613
+ }
614
+ return undefined;
615
+ }
616
+ /** VALUE t( ... ), CONV t( ... ) etc., NEW c( ... ) and CAST c( ... ) with an explicit type */
617
+ constructedType(source) {
618
+ const children = source.getChildren();
619
+ const last = children[children.length - 1];
620
+ const keyword = source.getFirstToken().getStr().toUpperCase();
621
+ let name;
622
+ let reference = false;
623
+ if (CONSTRUCTORS.has(keyword) && keyword !== "REF" && last instanceof nodes_1.TokenNode && last.getFirstToken().getStr() === ")") {
624
+ name = source.findDirectExpression(Expressions.TypeNameOrInfer);
625
+ }
626
+ else if (children.length === 1 && children[0].get() instanceof Expressions.MethodCallChain
627
+ && children[0].getChildren().length === 1) {
628
+ const constructor = children[0].getChildren()[0];
629
+ if (constructor.get() instanceof Expressions.NewObject || constructor.get() instanceof Expressions.Cast) {
630
+ name = constructor.findDirectExpression(Expressions.TypeNameOrInfer);
631
+ reference = true;
632
+ }
633
+ }
634
+ const text = name === null || name === void 0 ? void 0 : name.concatTokens().toUpperCase();
635
+ return text === undefined ? undefined : { name: text, reference };
636
+ }
137
637
  /** the type of the expression does not depend on where it is assigned to */
138
638
  targetIndependent(procedure, source) {
139
- var _a, _b;
639
+ var _a, _b, _c;
140
640
  const first = source.getFirstToken().getStr().toUpperCase();
141
- if (first === "CONV" || first === "EXACT"
641
+ if (this.constant(source)) {
642
+ return true;
643
+ }
644
+ else if (first === "CONV" || first === "EXACT"
142
645
  || ((_a = source.findDirectExpression(Expressions.TypeNameOrInfer)) === null || _a === void 0 ? void 0 : _a.concatTokens()) === "#"
646
+ || ((_b = this.constructedType(source)) === null || _b === void 0 ? void 0 : _b.name) === "#"
143
647
  || source.findFirstExpression(Expressions.ArithOperator) !== undefined
144
648
  || source.getTokens().some(t => t.getStr().toUpperCase().startsWith("BIT-"))
145
649
  || !this.concatenationOnly(source)) {
146
650
  return false;
147
651
  }
148
652
  for (const call of source.findAllExpressionsRecursive(Expressions.MethodCall)) {
149
- const name = (_b = call.findDirectExpression(Expressions.MethodName)) === null || _b === void 0 ? void 0 : _b.getFirstToken();
653
+ const name = (_c = call.findDirectExpression(Expressions.MethodName)) === null || _c === void 0 ? void 0 : _c.getFirstToken();
150
654
  if (name === undefined) {
151
655
  return false;
152
656
  }
153
657
  const builtin = procedure.analysis.referencesAt(procedure.file.getFilename(), name)
154
658
  .some(r => r.referenceType === _reference_1.ReferenceType.BuiltinMethodReference);
155
- if (builtin && NUMERIC_FUNCTIONS.has(name.getStr().toLowerCase())) {
659
+ if (builtin && _procedure_analysis_1.NUMERIC_FUNCTIONS.has(name.getStr().toLowerCase())) {
156
660
  return false;
157
661
  }
158
662
  }