@eagleoutice/flowr 2.15.10 → 2.16.0

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 (181) hide show
  1. package/README.md +23 -26
  2. package/abstract-interpretation/data-frame/arguments.js +11 -7
  3. package/control-flow/happens-before.d.ts +5 -0
  4. package/control-flow/happens-before.js +48 -34
  5. package/control-flow/useless-loop.d.ts +1 -1
  6. package/control-flow/useless-loop.js +29 -42
  7. package/dataflow/environments/built-in-props.d.ts +9 -0
  8. package/dataflow/environments/built-in-props.js +25 -19
  9. package/dataflow/environments/built-in.d.ts +2 -2
  10. package/dataflow/environments/built-in.js +41 -77
  11. package/dataflow/environments/default-builtin-config.d.ts +181 -29
  12. package/dataflow/environments/default-builtin-config.js +38 -24
  13. package/dataflow/environments/environment.d.ts +6 -4
  14. package/dataflow/environments/environment.js +54 -121
  15. package/dataflow/environments/identifier.d.ts +4 -2
  16. package/dataflow/environments/identifier.js +23 -69
  17. package/dataflow/environments/reference-to-maybe.d.ts +9 -0
  18. package/dataflow/environments/reference-to-maybe.js +23 -51
  19. package/dataflow/environments/resolve-by-name.js +37 -76
  20. package/dataflow/eval/resolve/resolve-argument.d.ts +3 -1
  21. package/dataflow/eval/resolve/resolve-argument.js +61 -64
  22. package/dataflow/eval/resolve/resolve-working-directory.d.ts +8 -2
  23. package/dataflow/eval/resolve/resolve-working-directory.js +12 -20
  24. package/dataflow/eval/resolve/resolve.d.ts +4 -0
  25. package/dataflow/eval/resolve/resolve.js +23 -14
  26. package/dataflow/extractor.js +176 -59
  27. package/dataflow/fn/class-declaration.d.ts +8 -2
  28. package/dataflow/fn/class-declaration.js +96 -97
  29. package/dataflow/fn/exceptions-of-function.js +68 -37
  30. package/dataflow/fn/frame-reflection.d.ts +12 -4
  31. package/dataflow/fn/frame-reflection.js +51 -41
  32. package/dataflow/fn/function-props.js +15 -33
  33. package/dataflow/fn/function-semantics.d.ts +11 -3
  34. package/dataflow/fn/function-semantics.js +6 -0
  35. package/dataflow/fn/higher-order-function.js +13 -17
  36. package/dataflow/fn/strict-function.d.ts +13 -0
  37. package/dataflow/fn/strict-function.js +285 -122
  38. package/dataflow/graph/call-graph.d.ts +7 -6
  39. package/dataflow/graph/call-graph.js +35 -46
  40. package/dataflow/graph/graph.d.ts +6 -1
  41. package/dataflow/graph/graph.js +57 -66
  42. package/dataflow/info.d.ts +8 -1
  43. package/dataflow/info.js +24 -30
  44. package/dataflow/internal/control-flow.d.ts +31 -4
  45. package/dataflow/internal/control-flow.js +61 -39
  46. package/dataflow/internal/linker.d.ts +15 -9
  47. package/dataflow/internal/linker.js +253 -241
  48. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +80 -14
  49. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +446 -78
  50. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +3 -1
  51. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +51 -48
  52. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +2 -2
  53. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +119 -185
  54. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.js +7 -9
  55. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +1 -1
  56. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +122 -147
  57. package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +55 -25
  58. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +289 -251
  59. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +1 -1
  60. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +30 -32
  61. package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +68 -67
  62. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +72 -126
  63. package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +1 -3
  64. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +10 -10
  65. package/dataflow/internal/process/functions/call/built-in/built-in-quote.d.ts +4 -4
  66. package/dataflow/internal/process/functions/call/built-in/built-in-quote.js +9 -35
  67. package/dataflow/internal/process/functions/call/built-in/built-in-rm.js +8 -3
  68. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.d.ts +1 -1
  69. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.js +12 -28
  70. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.d.ts +1 -1
  71. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.js +40 -118
  72. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.d.ts +1 -3
  73. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.js +16 -24
  74. package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +2 -0
  75. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +93 -102
  76. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.d.ts +7 -3
  77. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +173 -114
  78. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.d.ts +1 -3
  79. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.js +22 -27
  80. package/dataflow/internal/process/functions/call/built-in/built-in-with.d.ts +1 -1
  81. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +34 -9
  82. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +1 -1
  83. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +119 -93
  84. package/dataflow/internal/process/functions/call/common.d.ts +1 -3
  85. package/dataflow/internal/process/functions/call/common.js +36 -76
  86. package/dataflow/internal/process/functions/call/deferred.d.ts +6 -2
  87. package/dataflow/internal/process/functions/call/deferred.js +54 -57
  88. package/dataflow/internal/process/functions/call/known-call-handling.d.ts +4 -2
  89. package/dataflow/internal/process/functions/call/known-call-handling.js +65 -68
  90. package/dataflow/internal/process/functions/call/named-call-handling.d.ts +3 -5
  91. package/dataflow/internal/process/functions/call/named-call-handling.js +35 -21
  92. package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
  93. package/dataflow/internal/process/functions/call/quoted.js +162 -172
  94. package/dataflow/internal/process/functions/call/unnamed-call-handling.js +17 -38
  95. package/dataflow/internal/process/functions/process-parameter.js +25 -32
  96. package/dataflow/internal/process/process-symbol.js +3 -6
  97. package/dataflow/internal/program-order.d.ts +26 -0
  98. package/dataflow/internal/program-order.js +49 -0
  99. package/documentation/wiki-setup.js +23 -0
  100. package/linter/linter-rules.d.ts +17 -8
  101. package/linter/rules/absolute-path.js +63 -87
  102. package/linter/rules/file-path-validity.d.ts +4 -2
  103. package/linter/rules/file-path-validity.js +114 -80
  104. package/linter/rules/namespace-access.js +21 -33
  105. package/linter/rules/naming-convention.d.ts +1 -1
  106. package/linter/rules/naming-convention.js +60 -48
  107. package/linter/rules/network-functions.js +11 -7
  108. package/linter/rules/no-leaked-credentials.js +15 -18
  109. package/linter/rules/problematic-inputs.d.ts +2 -1
  110. package/linter/rules/problematic-inputs.js +39 -59
  111. package/linter/rules/roxygen-arguments.d.ts +9 -4
  112. package/linter/rules/roxygen-arguments.js +29 -48
  113. package/linter/rules/seeded-randomness.d.ts +7 -4
  114. package/linter/rules/seeded-randomness.js +128 -89
  115. package/linter/rules/stop-with-call-arg.d.ts +4 -2
  116. package/linter/rules/stop-with-call-arg.js +25 -22
  117. package/linter/rules/unclosed-connection.js +31 -24
  118. package/linter/rules/undefined-symbol.d.ts +4 -6
  119. package/linter/rules/undefined-symbol.js +58 -75
  120. package/linter/rules/unescaped-arguments.js +18 -44
  121. package/linter/rules/unused-definition.d.ts +6 -0
  122. package/linter/rules/unused-definition.js +105 -145
  123. package/linter/rules/unused-import.d.ts +3 -0
  124. package/linter/rules/unused-import.js +25 -43
  125. package/package.json +5 -2
  126. package/project/flowr-analyzer.js +6 -2
  127. package/project/plugins/file-plugins/files/flowr-jupyter-file.d.ts +2 -2
  128. package/project/plugins/file-plugins/files/flowr-jupyter-file.js +8 -12
  129. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.d.ts +7 -5
  130. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.js +34 -56
  131. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +16 -41
  132. package/queries/base-query-format.js +1 -2
  133. package/queries/catalog/absint-query/absint-query-format.js +1 -1
  134. package/queries/catalog/dataflow-query/dataflow-query-executor.js +2 -2
  135. package/queries/catalog/dataflow-query/dataflow-query-format.d.ts +2 -0
  136. package/queries/catalog/dataflow-query/dataflow-query-format.js +4 -7
  137. package/queries/catalog/dependencies-query/dependencies-query-executor.js +50 -114
  138. package/queries/catalog/dependencies-query/function-info/library-functions.d.ts +5 -9
  139. package/queries/catalog/dependencies-query/function-info/library-functions.js +6 -0
  140. package/queries/catalog/dependencies-query/function-info/read-functions.js +1 -1
  141. package/queries/catalog/dependencies-query/function-info/write-functions.js +7 -3
  142. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +4 -4
  143. package/queries/catalog/input-sources-query/simple-input-classifier.js +192 -301
  144. package/r-bridge/lang-4.x/ast/model/model.d.ts +3 -1
  145. package/r-bridge/lang-4.x/ast/model/model.js +19 -43
  146. package/r-bridge/lang-4.x/ast/model/nodes/r-access.d.ts +1 -0
  147. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.d.ts +3 -0
  148. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.js +5 -12
  149. package/r-bridge/lang-4.x/ast/model/nodes/r-binary-op.d.ts +1 -0
  150. package/r-bridge/lang-4.x/ast/model/nodes/r-break.d.ts +1 -0
  151. package/r-bridge/lang-4.x/ast/model/nodes/r-comment.d.ts +1 -0
  152. package/r-bridge/lang-4.x/ast/model/nodes/r-expression-list.d.ts +1 -0
  153. package/r-bridge/lang-4.x/ast/model/nodes/r-for-loop.d.ts +1 -0
  154. package/r-bridge/lang-4.x/ast/model/nodes/r-function-call.d.ts +1 -0
  155. package/r-bridge/lang-4.x/ast/model/nodes/r-function-definition.d.ts +1 -0
  156. package/r-bridge/lang-4.x/ast/model/nodes/r-if-then-else.d.ts +1 -0
  157. package/r-bridge/lang-4.x/ast/model/nodes/r-line-directive.d.ts +1 -0
  158. package/r-bridge/lang-4.x/ast/model/nodes/r-logical.d.ts +1 -0
  159. package/r-bridge/lang-4.x/ast/model/nodes/r-next.d.ts +1 -0
  160. package/r-bridge/lang-4.x/ast/model/nodes/r-number.d.ts +1 -0
  161. package/r-bridge/lang-4.x/ast/model/nodes/r-parameter.d.ts +1 -0
  162. package/r-bridge/lang-4.x/ast/model/nodes/r-pipe.d.ts +1 -0
  163. package/r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop.d.ts +1 -0
  164. package/r-bridge/lang-4.x/ast/model/nodes/r-string.d.ts +1 -0
  165. package/r-bridge/lang-4.x/ast/model/nodes/r-symbol.d.ts +1 -0
  166. package/r-bridge/lang-4.x/ast/model/nodes/r-unary-op.d.ts +1 -0
  167. package/r-bridge/lang-4.x/ast/model/nodes/r-while-loop.d.ts +1 -0
  168. package/r-bridge/roxygen2/roxygen-parse.js +28 -42
  169. package/r-bridge/shell.js +6 -0
  170. package/search/search-executor/search-enrichers.js +25 -37
  171. package/slicing/static/slice-call.d.ts +4 -3
  172. package/slicing/static/slice-call.js +56 -88
  173. package/slicing/static/static-slicer.js +115 -79
  174. package/slicing/static/visiting-queue.d.ts +4 -6
  175. package/slicing/static/visiting-queue.js +9 -13
  176. package/util/proc.js +2 -1
  177. package/util/r-base-packages.d.ts +2 -0
  178. package/util/r-base-packages.js +23 -13
  179. package/util/text/playground-link.d.ts +12 -3
  180. package/util/text/playground-link.js +18 -27
  181. package/util/version.js +4 -10
@@ -15,62 +15,51 @@ const control_flow_1 = require("../../../../control-flow");
15
15
  const vertex_1 = require("../../../../../graph/vertex");
16
16
  const scoping_1 = require("../../../../../environments/scoping");
17
17
  const overwrite_1 = require("../../../../../environments/overwrite");
18
- const logger_1 = require("../../../../../logger");
19
- const log_1 = require("../../../../../../util/log");
18
+ const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
20
19
  const reference_to_maybe_1 = require("../../../../../environments/reference-to-maybe");
21
20
  const apply_kill_1 = require("../../../../../environments/apply-kill");
22
21
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
23
22
  const general_1 = require("../../../../../eval/values/general");
24
23
  const vertex_2 = require("../../../../../graph/vertex");
25
24
  const resolve_helper_1 = require("../../../../../environments/resolve-helper");
26
- const type_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/type");
25
+ const r_argument_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
26
+ const r_expression_list_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-expression-list");
27
+ const r_if_then_else_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-if-then-else");
28
+ const r_while_loop_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-while-loop");
29
+ const r_for_loop_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-for-loop");
30
+ const model_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/model");
31
+ const defaultmap_1 = require("../../../../../../util/collections/defaultmap");
32
+ const objects_1 = require("../../../../../../util/objects");
33
+ const frame_reflection_1 = require("../../../../../fn/frame-reflection");
27
34
  const built_in_ambient_state_1 = require("./built-in-ambient-state");
28
35
  /**
29
36
  * Whether the definitions of this list among the `targets` of a read cover every branch, alone or together.
30
37
  * Only then the read is resolved here and must not bubble up as an ingoing reference.
31
38
  */
32
39
  function coveredByListDefinitions(targets, listEnvironments) {
33
- let cds;
34
- for (const target of targets) {
35
- if (!listEnvironments.has(target.nodeId)) {
36
- continue;
37
- }
38
- else if (target.cds === undefined) {
39
- return true;
40
- }
41
- (cds ??= []).push(...target.cds);
40
+ const own = targets.filter(t => listEnvironments.has(t.nodeId));
41
+ if (own.some(t => t.cds === undefined)) {
42
+ return true;
42
43
  }
43
- return cds !== undefined && (0, info_1.happensInEveryBranch)(cds);
44
+ return own.length > 0 && (0, info_1.happensInEveryBranch)(own.flatMap(t => t.cds ?? []));
44
45
  }
46
+ /** Whether reading the variable defined at `id` calls a function, as for an active binding. */
45
47
  function callsOnRead(graph, id) {
46
- if (!vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(id))) {
47
- return false;
48
- }
49
- for (const e of graph.edgesFrom(id).values()) {
50
- if (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Calls)) {
51
- return true;
52
- }
53
- }
54
- return false;
48
+ return vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(id)) && (0, frame_reflection_1.edgeTargets)(graph, id, edge_1.EdgeType.Calls).length > 0;
55
49
  }
50
+ /** @returns whether `read` sees a definition */
56
51
  function linkReadNameToWriteIfPossible(read, environments, listEnvironments, remainingRead, nextGraph, activeReads) {
57
52
  const readName = read.name && identifier_1.Identifier.isDotDotDotAccess(read.name) ? identifier_1.Identifier.dotdotdot() : read.name;
58
53
  const probableTarget = readName ? resolve_helper_1.Resolve.byNameAndType(readName, environments, read.type) : undefined;
54
+ /* without a target the read stays ingoing (read-ids are unique within the same fold), a later fold may still resolve it */
59
55
  if (probableTarget === undefined || !coveredByListDefinitions(probableTarget, listEnvironments)) {
60
- const readId = readName ? identifier_1.Identifier.getName(readName) : undefined;
61
- const has = remainingRead.get(readId);
62
- if (has) {
63
- if (!has.some(h => h.nodeId === read.nodeId && h.name === read.name && h.cds === read.cds)) {
64
- has.push(read);
65
- }
66
- }
67
- else {
68
- remainingRead.set(readId, [read]);
56
+ const has = remainingRead.get(readName ? identifier_1.Identifier.getName(readName) : undefined);
57
+ if (!has.some(h => h.nodeId === read.nodeId && h.name === read.name && h.cds === read.cds)) {
58
+ has.push(read);
69
59
  }
70
60
  }
71
- // no target: keep it as an ingoing read (read-ids are unique within the same fold); a later fold may still resolve and drop it
72
61
  if (probableTarget === undefined) {
73
- return;
62
+ return false;
74
63
  }
75
64
  const rid = read.nodeId;
76
65
  const isFunc = read.type === identifier_1.ReferenceType.Function || read.type === identifier_1.ReferenceType.BuiltInFunction;
@@ -95,28 +84,25 @@ function linkReadNameToWriteIfPossible(read, environments, listEnvironments, rem
95
84
  tag: vertex_1.VertexType.Value,
96
85
  id: tid,
97
86
  cds: undefined,
98
- value: (0, general_1.valueFromTsValue)((target).value)
87
+ value: (0, general_1.valueFromTsValue)(target.value)
99
88
  }, environments, false);
100
89
  }
101
90
  }
91
+ return true;
102
92
  }
103
- /**
104
- * Expands `direct`'s directly-called functions with everything they transitively call (via `resolveEnv`): a sibling/outer
105
- * callee's escaped globals otherwise never fold into the caller, so a `<<-` escaping several call levels would go unlinked.
106
- */
93
+ /** Expands `initial` with everything it transitively calls, so a `<<-` escaping several call levels still folds into the caller. */
107
94
  function* transitivelyCalledDefinitions(initial, graph, resolveEnv) {
108
95
  const seen = new Set();
109
96
  const stack = initial.map(fn => ({ fn, direct: true }));
110
97
  while (stack.length > 0) {
111
- const { fn, direct } = stack.pop();
98
+ const { fn, direct, cds } = stack.pop();
112
99
  if (!vertex_2.DfgVertex.isFunctionDefinition(fn) || seen.has(fn.id)) {
113
100
  continue;
114
101
  }
115
102
  seen.add(fn.id);
116
- yield { fn, direct };
117
- for (const nodeId of fn.subflow.graph) {
118
- const call = graph.getVertex(nodeId);
119
- if (!vertex_2.DfgVertex.isFunctionCall(call) || call.onlyBuiltin || call.name === undefined) {
103
+ yield { fn, direct, cds };
104
+ for (const [, call] of (0, frame_reflection_1.callsIn)(fn, graph)) {
105
+ if (call.onlyBuiltin || call.name === undefined) {
120
106
  continue;
121
107
  }
122
108
  const resolved = resolve_helper_1.Resolve.byNameAndType(call.name, resolveEnv, identifier_1.ReferenceType.Function);
@@ -125,9 +111,8 @@ function* transitivelyCalledDefinitions(initial, graph, resolveEnv) {
125
111
  }
126
112
  const [targets] = (0, linker_1.getAllLinkedFunctionDefinitions)(new Set(resolved.map(r => r.nodeId)), graph);
127
113
  for (const target of targets) {
128
- if (!seen.has(target.id)) {
129
- stack.push({ fn: target, direct: false });
130
- }
114
+ /* a callee reached only on some paths writes only maybe */
115
+ stack.push({ fn: target, direct: false, cds: call.cds === undefined ? cds : [...cds ?? [], ...call.cds] });
131
116
  }
132
117
  }
133
118
  }
@@ -170,59 +155,63 @@ function foldFrameIntoParent({ current, level }) {
170
155
  * written at that spot would have done, since we only learn a callee always throws after linking it here.
171
156
  */
172
157
  function errorEscapes(from, expression, idMap, graph) {
173
- let node = idMap.get(from);
174
- while (node !== undefined && node.info.id !== expression) {
175
- const parent = node.info.parent !== undefined ? idMap.get(node.info.parent) : undefined;
158
+ for (let node = idMap.get(from); node !== undefined && node.info.id !== expression;) {
159
+ const parent = model_1.RNode.directParent(node, idMap);
176
160
  if (parent === undefined) {
177
161
  return true;
178
162
  }
179
- switch (parent.type) {
180
- case type_1.RType.Argument:
181
- case type_1.RType.ExpressionList:
182
- break;
183
- case type_1.RType.FunctionCall:
184
- /* a handler catches the error instead of passing it on */
185
- if (vertex_2.DfgVertex.hasOrigin(graph.getVertex(parent.info.id), built_in_proc_name_1.BuiltInProcName.Try)) {
186
- return false;
187
- }
188
- break;
189
- case type_1.RType.IfThenElse:
190
- case type_1.RType.WhileLoop:
191
- /* only the condition is guaranteed to be evaluated */
192
- if (parent.condition.info.id !== node.info.id) {
193
- return false;
194
- }
195
- break;
196
- case type_1.RType.ForLoop:
197
- if (parent.vector.info.id !== node.info.id) {
198
- return false;
199
- }
200
- break;
201
- default:
202
- return false;
163
+ if (!errorPassesThrough(parent, node.info.id, graph)) {
164
+ return false;
203
165
  }
204
166
  node = parent;
205
167
  }
206
168
  return true;
207
169
  }
208
- function writtenWithinCall(call, node, idMap) {
209
- if (!idMap.has(call)) {
170
+ /** Whether an error raised within the child `child` of `parent` is sure to be raised by `parent` as well. */
171
+ function errorPassesThrough(parent, child, graph) {
172
+ if (r_argument_1.RArgument.is(parent) || r_expression_list_1.RExpressionList.is(parent)) {
210
173
  return true;
211
174
  }
212
- let current = idMap.get(node);
213
- while (current !== undefined) {
214
- if (current.info.id === call) {
215
- return true;
216
- }
217
- current = current.info.parent !== undefined ? idMap.get(current.info.parent) : undefined;
175
+ else if (r_function_call_1.RFunctionCall.is(parent)) {
176
+ /* a handler catches the error instead of passing it on */
177
+ return !vertex_2.DfgVertex.hasOrigin(graph.getVertex(parent.info.id), built_in_proc_name_1.BuiltInProcName.Try);
178
+ }
179
+ else if (r_if_then_else_1.RIfThenElse.is(parent) || r_while_loop_1.RWhileLoop.is(parent)) {
180
+ /* only the condition is guaranteed to be evaluated */
181
+ return parent.condition.info.id === child;
182
+ }
183
+ else if (r_for_loop_1.RForLoop.is(parent)) {
184
+ return parent.vector.info.id === child;
218
185
  }
219
186
  return false;
220
187
  }
188
+ /** Whether `node` is written within the call `call` (or the call is not part of the AST). */
189
+ function writtenWithinCall(call, node, idMap) {
190
+ return !idMap.has(call) || model_1.RNode.findEnclosing(node, idMap, n => n.info.id === call) !== undefined;
191
+ }
192
+ /** Printing a visible top-level value dispatches to the `print` methods in scope, so the expression runs them like a call would. */
193
+ function printMethodsOf(id, environment, graph) {
194
+ const methods = resolve_helper_1.Resolve.byNameAndType('print', environment, identifier_1.ReferenceType.S3MethodPrefix)?.filter(d => !node_id_1.NodeId.isBuiltIn(d.nodeId)) ?? [];
195
+ if (methods.length === 0) {
196
+ return [];
197
+ }
198
+ for (const method of methods) {
199
+ graph.addEdge(id, method.nodeId, edge_1.EdgeType.Reads);
200
+ }
201
+ const called = [...(0, linker_1.getAllLinkedFunctionDefinitions)(new Set(methods.map(m => m.nodeId)), graph)[0]];
202
+ for (const ref of called.flatMap(c => c.subflow.in)) {
203
+ for (const def of ref.name === undefined ? [] : resolve_helper_1.Resolve.byNameAndType(ref.name, environment, ref.type) ?? []) {
204
+ if (!node_id_1.NodeId.isBuiltIn(def.nodeId)) {
205
+ graph.addEdge(ref.nodeId, def.nodeId, edge_1.EdgeType.Reads);
206
+ }
207
+ }
208
+ }
209
+ return [{ functionCall: id, called, propagateExitPoints: [] }];
210
+ }
221
211
  function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextGraph, localDefs, idMap) {
222
212
  for (const { functionCall, called } of calledEnvs) {
223
- let callDependencies = null;
224
- for (const { fn: calledFn, direct } of transitivelyCalledDefinitions(called, nextGraph, inputEnvironment)) {
225
- (0, assert_1.guard)(vertex_2.DfgVertex.isFunctionDefinition(calledFn), 'called function must be a function definition');
213
+ const callDependencies = nextGraph.getVertex(functionCall)?.cds;
214
+ for (const { fn: calledFn, direct, cds } of transitivelyCalledDefinitions(called, nextGraph, inputEnvironment)) {
226
215
  /* walk the smaller of the two, a script may hold many unknown side effects and a function many vertices */
227
216
  const inside = calledFn.subflow.graph;
228
217
  const effects = nextGraph.unknownSideEffects;
@@ -241,14 +230,10 @@ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextG
241
230
  environment = foldFrameIntoParent(environment);
242
231
  }
243
232
  }
244
- // update alle definitions to be defined at this function call
245
- let current = environment.current;
246
233
  let hasUpdate = false;
247
- while (!current?.builtInEnv) {
248
- // a package attached inside the body (library()) must propagate to the caller, like R attaching globally
249
- if (current.t !== undefined) {
250
- hasUpdate = true;
251
- }
234
+ for (let current = environment.current; !current.builtInEnv; current = current.parent) {
235
+ /* a package attached inside the body (library()) propagates to the caller, like R attaching globally */
236
+ hasUpdate ||= current.t !== undefined;
252
237
  for (const definitions of current.memory.values()) {
253
238
  for (const def of definitions) {
254
239
  if (!node_id_1.NodeId.isBuiltIn(def.definedAt)) {
@@ -257,21 +242,19 @@ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextG
257
242
  }
258
243
  }
259
244
  }
260
- current = current.parent;
261
245
  }
262
- if (hasUpdate) {
263
- const shadowing = localDefs.filter(d => (0, assert_1.isNotUndefined)(d.name) && !writtenWithinCall(functionCall, d.nodeId, idMap));
264
- if (shadowing.length > 0) {
265
- environment = {
266
- current: environment.current.removeAll(shadowing),
267
- level: environment.level
268
- };
269
- }
270
- if (callDependencies === null) {
271
- callDependencies = nextGraph.getVertex(functionCall)?.cds;
272
- }
273
- inputEnvironment = (0, overwrite_1.overwriteEnvironment)(inputEnvironment, environment, callDependencies);
246
+ if (!hasUpdate) {
247
+ continue;
248
+ }
249
+ const shadowing = localDefs.filter(d => (0, assert_1.isNotUndefined)(d.name) && !writtenWithinCall(functionCall, d.nodeId, idMap));
250
+ if (shadowing.length > 0) {
251
+ environment = {
252
+ current: environment.current.removeAll(shadowing),
253
+ level: environment.level
254
+ };
274
255
  }
256
+ const dependencies = cds === undefined ? callDependencies : [...callDependencies ?? [], ...cds];
257
+ inputEnvironment = (0, overwrite_1.overwriteEnvironment)(inputEnvironment, environment, dependencies);
275
258
  }
276
259
  }
277
260
  return inputEnvironment;
@@ -280,65 +263,65 @@ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextG
280
263
  * Processes a list of expressions joining their dataflow graphs accordingly.
281
264
  */
282
265
  function processExpressionList(name, args, rootId, data) {
283
- (0, log_1.expensiveTrace)(logger_1.dataflowLogger, () => `[expr list] with ${args.length} expressions`);
284
266
  let { environment } = data;
285
- // used to detect if a "write" happens within the same expression list
267
+ const idMap = data.completeAst.idMap;
268
+ /* the writes of this list, to detect whether a read sees one of them */
286
269
  const listEnvironments = new Set();
287
- const remainingRead = new Map();
288
- const nextGraph = new graph_1.DataflowGraph(data.completeAst.idMap);
270
+ const remainingRead = new defaultmap_1.DefaultMap(() => []);
271
+ const nextGraph = new graph_1.DataflowGraph(idMap);
289
272
  const out = [];
290
273
  /* lazily created - `rm` is rare, so rm-free lists never allocate this */
291
274
  let killed;
292
275
  const exitPoints = [];
293
- const activeCdsAtStart = data.cds;
276
+ /* what the list stands under as a whole, copied since the vertices share and extend that array */
277
+ const context = data.cds?.slice();
294
278
  const invertExitCds = [];
295
279
  const processedExpressions = [];
296
- let defaultReturnExpr = undefined;
280
+ let defaultReturnExpr;
297
281
  /* R prints what the top level of the analyzed script leaves visible, but not what a sourced file does */
298
282
  const autoPrint = data.referenceChain.length <= 1 && data.environment.level === 0 && data.completeAst.ast.files.some(f => f.root.info.id === rootId);
299
- let hooks;
283
+ let initialState;
284
+ const hooks = [];
300
285
  for (const arg of args) {
301
286
  const expression = (0, unpack_argument_1.unpackNonameArg)(arg);
302
287
  if (expression === undefined) {
303
288
  processedExpressions.push(undefined);
304
289
  continue;
305
290
  }
306
- // use the current environments for processing
307
291
  data.environment = environment;
308
292
  let processed = (0, processor_1.processDataflowFor)(expression, data);
309
- if (autoPrint) {
293
+ const printed = autoPrint && (0, built_in_ambient_state_1.autoPrints)(expression, processed, data);
294
+ if (printed) {
310
295
  processed = (0, built_in_ambient_state_1.readsOfAutoPrint)(processed, expression, data);
311
296
  }
312
297
  processedExpressions.push(processed);
313
298
  /* the expression's graph dies here, what is kept of it afterward is its entry point and its references */
314
299
  nextGraph.mergeWith(processed.graph, true, true);
315
300
  defaultReturnExpr = processed;
316
- // a prior next/break means we do not know if the rest executes, so later environments append rather than overwrite
301
+ /* a prior next/break means we do not know if the rest executes, so later environments append rather than overwrite */
317
302
  if (exitPoints.length > 0) {
318
303
  processed.out = (0, reference_to_maybe_1.makeAllMaybe)(processed.out, nextGraph, processed.environment, true, invertExitCds);
319
304
  processed.in = (0, reference_to_maybe_1.makeAllMaybe)(processed.in, nextGraph, processed.environment, false, invertExitCds);
320
305
  processed.unknownReferences = (0, reference_to_maybe_1.makeAllMaybe)(processed.unknownReferences, nextGraph, processed.environment, false);
321
306
  }
322
- if (processed.hooks.length > 0) {
323
- (hooks ??= []).push(...processed.hooks);
324
- }
307
+ hooks.push(...processed.hooks);
325
308
  // all inputs that have not been written until now are read!
326
309
  const activeReads = [];
327
- for (const read of processed.in) {
328
- linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph, activeReads);
329
- }
330
- for (const read of processed.unknownReferences) {
331
- linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph, activeReads);
310
+ for (const read of processed.in.concat(processed.unknownReferences)) {
311
+ if (!linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph, activeReads) && autoPrint && built_in_ambient_state_1.AmbientStateName.is(read.name)) {
312
+ (initialState ??= new Set()).add(read);
313
+ }
332
314
  }
333
315
  for (const read of activeReads) {
334
316
  nextGraph.updateToFunctionCall({ tag: vertex_1.VertexType.FunctionCall, id: read.nodeId, name: read.name, args: [], environment, onlyBuiltin: false, cds: read.cds, origin: [built_in_proc_name_1.BuiltInProcName.Function] });
335
317
  }
336
- const calledEnvs = (0, linker_1.linkFunctionCalls)(nextGraph, data.completeAst.idMap, processed.graph, activeReads.map(r => r.nodeId));
318
+ const calledEnvs = (0, linker_1.linkFunctionCalls)(nextGraph, idMap, processed.graph, activeReads.map(r => r.nodeId));
319
+ if (printed) {
320
+ calledEnvs.push(...printMethodsOf(processed.entryPoint, environment, nextGraph));
321
+ }
337
322
  for (const c of calledEnvs) {
338
- if (c.propagateExitPoints.length > 0 && errorEscapes(c.functionCall, expression.info.id, data.completeAst.idMap, nextGraph)) {
339
- for (const exit of c.propagateExitPoints) {
340
- processed.exitPoints.push(exit);
341
- }
323
+ if (c.propagateExitPoints.length > 0 && errorEscapes(c.functionCall, expression.info.id, idMap, nextGraph)) {
324
+ processed.exitPoints.push(...c.propagateExitPoints);
342
325
  }
343
326
  }
344
327
  for (const c of calledEnvs) {
@@ -346,10 +329,10 @@ function processExpressionList(name, args, rootId, data) {
346
329
  processed = (0, built_in_ambient_state_1.applyAmbientStateOfCalledBuiltIns)(processed, c.functionCall, c.calledBuiltIns, environment, nextGraph, data);
347
330
  }
348
331
  }
349
- (0, info_1.addNonDefaultExitPoints)(exitPoints, invertExitCds, activeCdsAtStart, processed.exitPoints);
332
+ (0, info_1.addNonDefaultExitPoints)(exitPoints, invertExitCds, data.cds, processed.exitPoints);
350
333
  environment = exitPoints.length > 0 ? (0, overwrite_1.overwriteEnvironment)(environment, processed.environment) : processed.environment;
351
334
  // if the called function has global redefinitions, we have to keep them within our environment
352
- environment = updateSideEffectsForCalledFunctions(calledEnvs, environment, nextGraph, processed.out, data.completeAst.idMap);
335
+ environment = updateSideEffectsForCalledFunctions(calledEnvs, environment, nextGraph, processed.out, idMap);
353
336
  // removals are already reflected in the threaded environment; we only bubble them (net of later writes)
354
337
  if (killed && processed.out.length > 0) {
355
338
  killed = (0, apply_kill_1.cancelRevivedKills)(killed, processed.out);
@@ -364,25 +347,17 @@ function processExpressionList(name, args, rootId, data) {
364
347
  listEnvironments.add(ref.nodeId);
365
348
  }
366
349
  /* if at least one exit point encountered happens unconditionally, we exit here (dead code) */
367
- if (control_flow_1.ControlFlow.alwaysExits(processed)) {
350
+ if (control_flow_1.ControlFlow.alwaysExits(processed, context)) {
368
351
  /* if there is an always-exit expression, there is no default return active anymore */
369
352
  defaultReturnExpr = undefined;
370
353
  break;
371
354
  }
372
355
  }
373
356
  if (defaultReturnExpr) {
374
- exitPoints.push(data.cds ? {
375
- type: 0 /* ExitPointType.Default */,
376
- nodeId: defaultReturnExpr.entryPoint,
377
- cds: data.cds
378
- } : {
379
- type: 0 /* ExitPointType.Default */,
380
- nodeId: defaultReturnExpr.entryPoint
381
- });
357
+ exitPoints.push((0, objects_1.compactRecord)({ type: 0 /* ExitPointType.Default */, nodeId: defaultReturnExpr.entryPoint, cds: data.cds }));
382
358
  }
383
359
  const ingoing = [...remainingRead.values()].flat();
384
- const rootNode = data.completeAst.idMap.get(rootId);
385
- const withGroup = rootNode?.grouping;
360
+ const withGroup = idMap.get(rootId)?.grouping;
386
361
  if (withGroup) {
387
362
  ingoing.push({ nodeId: rootId, name: name.content, cds: data.cds, type: identifier_1.ReferenceType.Function });
388
363
  (0, common_1.patchFunctionCall)({
@@ -394,7 +369,6 @@ function processExpressionList(name, args, rootId, data) {
394
369
  origin: built_in_proc_name_1.BuiltInProcName.ExpressionList
395
370
  });
396
371
  nextGraph.addEdge(rootId, node_id_1.NodeId.toBuiltIn('{'), edge_1.EdgeType.Reads | edge_1.EdgeType.Calls);
397
- // process all exit points as potential returns:
398
372
  for (const exit of exitPoints) {
399
373
  if (exit.type === 1 /* ExitPointType.Return */ || exit.type === 0 /* ExitPointType.Default */) {
400
374
  nextGraph.addEdge(rootId, exit.nodeId, edge_1.EdgeType.Returns);
@@ -416,25 +390,26 @@ function processExpressionList(name, args, rootId, data) {
416
390
  nextGraph.addEdge(exit.nodeId, rootId, edge_1.EdgeType.FlowEdge);
417
391
  /* what a `return` hands back and what a `stop` raises stay with the node that does it */
418
392
  if (exit.nodeId !== rootId && (exit.type === 2 /* ExitPointType.Break */ || exit.type === 3 /* ExitPointType.Next */)) {
419
- exitPoints[i] = { ...exit, nodeId: rootId };
393
+ exitPoints[i] = (0, info_1.leaveThrough)(exit, rootId);
420
394
  }
421
395
  }
422
396
  }
423
397
  return {
424
398
  /* no active nodes remain, they are consumed within the remaining read collection */
425
399
  unknownReferences: [],
426
- /* at the top of the script, a read of the options or graphics state nothing set reads the state R starts with */
427
- in: autoPrint ? ingoing.filter(r => !built_in_ambient_state_1.AmbientStateName.is(r.name)) : ingoing,
400
+ /* at the top of the script, only a read of the options or graphics state nothing set reads the state R starts with,
401
+ which a write escaping a higher-order call may still replace, see the extractor */
402
+ in: autoPrint ? ingoing.filter(r => !built_in_ambient_state_1.AmbientStateName.is(r.name) || initialState?.has(r) === true) : ingoing,
428
403
  /* a definition that a still-effective removal undid is no longer visible to the outside */
429
404
  out: (0, apply_kill_1.dropKilledWrites)(out, killed),
430
- environment: environment,
405
+ environment,
431
406
  graph: nextGraph,
432
407
  /* if we have no group, we take the last evaluated expr */
433
408
  entryPoint: meId,
434
409
  cfgEntry: cfgEntry === meId ? undefined : cfgEntry,
435
410
  cfgExit: reachesEnd ? rootId : undefined,
436
- exitPoints: exitPoints,
437
- hooks: hooks ?? [],
411
+ exitPoints,
412
+ hooks,
438
413
  kill: killed,
439
414
  };
440
415
  }
@@ -19,6 +19,33 @@ const reference_to_maybe_1 = require("../../../../../environments/reference-to-m
19
19
  const apply_kill_1 = require("../../../../../environments/apply-kill");
20
20
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
21
21
  const built_in_ambient_state_1 = require("./built-in-ambient-state");
22
+ const node_value_1 = require("../../../../../eval/resolve/node-value");
23
+ const r_binary_op_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-binary-op");
24
+ const vertex_1 = require("../../../../../graph/vertex");
25
+ const ScalarTypes = new Set(['interval', 'number', 'string', 'logical']);
26
+ /** a value R iterates over at least once, `NULL` and empty vectors are the ones it does not */
27
+ function hasElement(value) {
28
+ return value.type === 'vector' ? Array.isArray(value.elements) && value.elements.length > 0 : ScalarTypes.has(value.type);
29
+ }
30
+ /** whether the node certainly holds a value and every value it may hold satisfies `holds` */
31
+ function alwaysHolds(node, data, holds) {
32
+ const values = node_value_1.NodeValue.setOf(node.info.id, data)?.elements;
33
+ return values !== undefined && values.length > 0 && values.every(holds);
34
+ }
35
+ /** `a:b` always has an element or fails, unless both sides are factors, which a plain value is not */
36
+ function isNonEmptyRange(vector, entry, data) {
37
+ if (!r_binary_op_1.RBinaryOp.is(vector) || vector.operator !== ':') {
38
+ return false;
39
+ }
40
+ if (!vertex_1.DfgVertex.hasOrigin(entry.graph.getVertex(vector.info.id), built_in_proc_name_1.BuiltInProcName.Default)) {
41
+ return false;
42
+ }
43
+ return [vector.lhs, vector.rhs].some(side => alwaysHolds(side, data, v => v.type !== 'vector' && hasElement(v)));
44
+ }
45
+ /** whether a for-loop over `vector` certainly runs its body at least once */
46
+ function iteratesAtLeastOnce(vector, entry, data) {
47
+ return isNonEmptyRange(vector, entry, data) || alwaysHolds(vector, data, hasElement);
48
+ }
22
49
  /**
23
50
  * Processes a for-loop call: `for(<variable> in <vector>) <body>`
24
51
  * desugared as:
@@ -32,46 +59,53 @@ function processForLoop(name, args, rootId, data) {
32
59
  return (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: 'default' }).information;
33
60
  }
34
61
  const [variableArg, vectorArg, bodyArg] = args.map(e => (0, unpack_argument_1.unpackNonameArg)(e));
35
- // we store the original environment here, as we merge it back lter in case the for-loop never executes
62
+ /* merged back in case the loop never runs */
36
63
  const origEnv = data.environment;
37
64
  (0, assert_1.guard)(variableArg !== undefined && vectorArg !== undefined && bodyArg !== undefined, () => `For-Loop ${JSON.stringify(args)} has missing arguments! Bad!`);
38
65
  const vector = (0, processor_1.processDataflowFor)(vectorArg, data);
39
- if (control_flow_1.ControlFlow.alwaysExits(vector)) {
66
+ if (control_flow_1.ControlFlow.alwaysExits(vector, data.cds)) {
40
67
  logger_1.dataflowLogger.warn(`For-Loop ${rootId} forces exit in vector, skipping rest`);
41
68
  return vector;
42
69
  }
70
+ /* resolved before the loop variable is bound, as it may share the name of what the vector reads */
71
+ const nonEmpty = iteratesAtLeastOnce(vectorArg, vector, data);
43
72
  const variable = (0, processor_1.processDataflowFor)(variableArg, data);
44
- // this should not be able to exit always!
45
- const originalDependency = data.cds;
46
73
  let headEnvironments = (0, overwrite_1.overwriteEnvironment)(vector.environment, variable.environment);
47
74
  /* only the vector's references and entry point are read past this, its graph is not */
48
75
  const headGraph = variable.graph.mergeWith(vector.graph, true, true);
49
76
  const writtenVariable = variable.unknownReferences.concat(variable.in);
50
- const writtenIds = new Set();
51
77
  for (const write of writtenVariable) {
52
- writtenIds.add(write.nodeId);
53
- headEnvironments = (0, define_1.define)({ ...write, definedAt: name.info.id, type: identifier_1.ReferenceType.Variable,
54
- value: [vectorArg.info.id], iterated: true }, false, headEnvironments);
78
+ const definition = {
79
+ ...write,
80
+ definedAt: name.info.id,
81
+ type: identifier_1.ReferenceType.Unknown,
82
+ value: [vectorArg.info.id],
83
+ iterated: true
84
+ };
85
+ headEnvironments = (0, define_1.define)(definition, false, headEnvironments);
55
86
  }
56
87
  data.environment = headEnvironments;
57
88
  const body = (0, processor_1.processDataflowFor)(bodyArg, data);
58
- const outEnvironment = (0, append_1.appendEnvironment)(headEnvironments, body.environment);
59
89
  const cd = [{ id: name.info.id, when: true }];
60
90
  const bodyRefs = body.in.concat(body.unknownReferences);
61
91
  (0, reference_to_maybe_1.applyCdsToAllInGraphButConstants)(body.graph, bodyRefs, cd);
62
92
  /* likewise the body's, what is taken from it afterwards are its references, exit points and hooks */
63
93
  const nextGraph = headGraph.mergeWith(body.graph, true, true);
64
- // now we have to identify all reads that may be effected by a circular redefinition
65
- // for this, we search for all reads with a non-local read resolve!
66
- const nameIdShares = (0, linker_1.produceNameSharedIdMap)((0, linker_1.findNonLocalReads)(nextGraph, writtenIds));
94
+ /* the reads with a non-local resolve may be affected by a circular redefinition */
95
+ const nameIdShares = (0, linker_1.produceNameSharedIdMap)((0, linker_1.findNonLocalReads)(nextGraph, new Set(writtenVariable.map(w => w.nodeId))));
67
96
  for (const write of writtenVariable) {
68
97
  nextGraph.addEdge(write.nodeId, vector.entryPoint, edge_1.EdgeType.DefinedBy);
69
98
  nextGraph.setDefinitionOfVertex(write, [vector.entryPoint]);
70
99
  }
71
- (0, reference_to_maybe_1.applyCdToReferences)(body.out, cd);
100
+ /* a `break` or `next` may cut the first iteration short */
101
+ const once = nonEmpty && !body.exitPoints.some(e => e.type === 2 /* ExitPointType.Break */ || e.type === 3 /* ExitPointType.Next */);
102
+ /* a body that certainly runs defines as definitely as the code around the loop */
103
+ if (!once) {
104
+ (0, reference_to_maybe_1.applyCdToReferences)(body.out, cd);
105
+ }
72
106
  const outgoing = variable.out.concat(writtenVariable, body.out);
73
107
  (0, linker_1.linkCircularRedefinitionsWithinALoop)(nextGraph, nameIdShares, body.out, body.environment);
74
- (0, built_in_ambient_state_1.linkAmbientStateWithinLoop)(nextGraph, bodyRefs, body);
108
+ const ambientReads = (0, built_in_ambient_state_1.linkAmbientStateWithinLoop)(nextGraph, bodyRefs, body);
75
109
  /* the loop variable is bound by the head whenever the body runs, so reads of it are not ingoing */
76
110
  const loopVariables = new Set(writtenVariable.map(w => w.name));
77
111
  const bodyReadsOfOthers = [...nameIdShares.entries()].filter(([n]) => !loopVariables.has(n)).flatMap(([, refs]) => refs);
@@ -80,36 +114,32 @@ function processForLoop(name, args, rootId, data) {
80
114
  nextGraph,
81
115
  rootId,
82
116
  name,
83
- data: { ...data, cds: originalDependency },
117
+ data,
84
118
  argumentProcessResult: [variable, vector, body],
85
119
  origin: built_in_proc_name_1.BuiltInProcName.ForLoop
86
120
  });
87
- /* mark the last argument as nse */
88
121
  nextGraph.addEdge(rootId, body.entryPoint, edge_1.EdgeType.NonStandardEvaluation);
89
- // as the for-loop always evaluates its condition
90
122
  nextGraph.addEdge(name.info.id, vector.entryPoint, edge_1.EdgeType.Reads);
91
- const bodyEntry = control_flow_1.ControlFlow.entryOf(body);
92
123
  const variableEntry = control_flow_1.ControlFlow.entryOf(variable);
93
124
  control_flow_1.ControlFlow.continuesWith(nextGraph, vector, variableEntry);
94
- control_flow_1.ControlFlow.branchesTo(nextGraph, variable, bodyEntry, cd[0]);
125
+ control_flow_1.ControlFlow.branchesTo(nextGraph, variable, control_flow_1.ControlFlow.entryOf(body), cd[0]);
95
126
  control_flow_1.ControlFlow.branchesTo(nextGraph, variable, rootId, { id: cd[0].id, when: false });
96
127
  control_flow_1.ControlFlow.continuesWith(nextGraph, body, variableEntry);
97
128
  control_flow_1.ControlFlow.jumpsTo(nextGraph, body, 3 /* ExitPointType.Next */, variableEntry);
98
129
  control_flow_1.ControlFlow.jumpsTo(nextGraph, body, 2 /* ExitPointType.Break */, rootId);
99
- // the body may never execute, so a removal within it only happens maybe; apply it as the merge cannot represent it
100
- const loopKill = body.kill?.length ? (0, apply_kill_1.makeKillsMaybe)(body.kill, cd) : undefined;
101
- const loopEnvironment = (0, append_1.appendEnvironment)(origEnv, outEnvironment);
130
+ // unless the body certainly runs, a removal within it only happens maybe; apply it as the merge cannot represent it
131
+ const loopKill = body.kill?.length ? (once ? body.kill : (0, apply_kill_1.makeKillsMaybe)(body.kill, cd)) : undefined;
132
+ const loopEnvironment = once ? body.environment : (0, append_1.appendEnvironment)(origEnv, (0, append_1.appendEnvironment)(headEnvironments, body.environment));
102
133
  return {
103
134
  unknownReferences: [],
104
- // we only want those not bound by a local variable
105
- in: [{ nodeId: rootId, name: name.content, cds: originalDependency, type: identifier_1.ReferenceType.Function }, ...vector.unknownReferences, ...bodyReadsOfOthers],
135
+ /* we only want those not bound by a local variable */
136
+ in: [{ nodeId: rootId, name: name.content, cds: data.cds, type: identifier_1.ReferenceType.Function }, ...vector.unknownReferences, ...bodyReadsOfOthers, ...ambientReads],
106
137
  out: outgoing,
107
138
  graph: nextGraph,
108
139
  entryPoint: name.info.id,
109
140
  cfgEntry: control_flow_1.ControlFlow.entryOf(vector),
110
141
  cfgExit: rootId,
111
142
  exitPoints: (0, info_1.filterOutLoopExitPoints)(body.exitPoints),
112
- // if we can not be sure that the for-loop runs once, we have to merge back the original environment, as the body may never execute
113
143
  environment: loopKill ? (0, apply_kill_1.applyKills)(loopEnvironment, loopKill) : loopEnvironment,
114
144
  hooks: variable.hooks.concat(vector.hooks, body.hooks),
115
145
  kill: loopKill,