@eagleoutice/flowr 2.15.10 → 2.16.1

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 (213) hide show
  1. package/README.md +23 -26
  2. package/abstract-interpretation/data-frame/arguments.js +11 -7
  3. package/cli/flowr-main-options.d.ts +15 -0
  4. package/cli/flowr-main-options.js +18 -0
  5. package/cli/flowr.d.ts +18 -0
  6. package/cli/flowr.js +13 -2
  7. package/cli/repl/commands/repl-clipboard.js +2 -1
  8. package/cli/repl/commands/repl-commands.js +2 -0
  9. package/cli/repl/commands/repl-main.d.ts +5 -0
  10. package/cli/repl/commands/repl-signature.js +2 -0
  11. package/cli/repl/core.d.ts +7 -1
  12. package/cli/repl/core.js +17 -8
  13. package/cli/repl/execute.js +2 -0
  14. package/cli/repl/server/connection.js +2 -1
  15. package/cli/repl/server/net.d.ts +10 -0
  16. package/cli/repl/server/net.js +34 -1
  17. package/cli/slicer-app.js +2 -1
  18. package/config.d.ts +12 -2
  19. package/config.js +91 -14
  20. package/control-flow/happens-before.d.ts +5 -0
  21. package/control-flow/happens-before.js +48 -34
  22. package/control-flow/useless-loop.d.ts +1 -1
  23. package/control-flow/useless-loop.js +29 -42
  24. package/dataflow/environments/built-in-props.d.ts +9 -0
  25. package/dataflow/environments/built-in-props.js +25 -19
  26. package/dataflow/environments/built-in.d.ts +2 -2
  27. package/dataflow/environments/built-in.js +41 -77
  28. package/dataflow/environments/default-builtin-config.d.ts +181 -29
  29. package/dataflow/environments/default-builtin-config.js +38 -24
  30. package/dataflow/environments/environment.d.ts +6 -4
  31. package/dataflow/environments/environment.js +54 -121
  32. package/dataflow/environments/identifier.d.ts +4 -2
  33. package/dataflow/environments/identifier.js +23 -69
  34. package/dataflow/environments/reference-to-maybe.d.ts +9 -0
  35. package/dataflow/environments/reference-to-maybe.js +23 -51
  36. package/dataflow/environments/resolve-by-name.js +37 -76
  37. package/dataflow/eval/resolve/resolve-argument.d.ts +3 -1
  38. package/dataflow/eval/resolve/resolve-argument.js +61 -64
  39. package/dataflow/eval/resolve/resolve-working-directory.d.ts +8 -2
  40. package/dataflow/eval/resolve/resolve-working-directory.js +12 -20
  41. package/dataflow/eval/resolve/resolve.d.ts +4 -0
  42. package/dataflow/eval/resolve/resolve.js +23 -14
  43. package/dataflow/extractor.js +185 -59
  44. package/dataflow/fn/class-declaration.d.ts +8 -2
  45. package/dataflow/fn/class-declaration.js +96 -97
  46. package/dataflow/fn/exceptions-of-function.js +68 -37
  47. package/dataflow/fn/frame-reflection.d.ts +12 -4
  48. package/dataflow/fn/frame-reflection.js +64 -42
  49. package/dataflow/fn/function-props.js +15 -33
  50. package/dataflow/fn/function-semantics.d.ts +11 -3
  51. package/dataflow/fn/function-semantics.js +6 -0
  52. package/dataflow/fn/higher-order-function.js +13 -17
  53. package/dataflow/fn/strict-function.d.ts +13 -0
  54. package/dataflow/fn/strict-function.js +285 -122
  55. package/dataflow/graph/call-graph.d.ts +7 -6
  56. package/dataflow/graph/call-graph.js +35 -46
  57. package/dataflow/graph/graph.d.ts +6 -1
  58. package/dataflow/graph/graph.js +57 -66
  59. package/dataflow/info.d.ts +8 -1
  60. package/dataflow/info.js +24 -30
  61. package/dataflow/internal/control-flow.d.ts +42 -4
  62. package/dataflow/internal/control-flow.js +76 -39
  63. package/dataflow/internal/linker.d.ts +15 -9
  64. package/dataflow/internal/linker.js +253 -241
  65. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +80 -14
  66. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +441 -79
  67. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +3 -1
  68. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +51 -48
  69. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +2 -2
  70. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +119 -185
  71. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.js +7 -9
  72. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +1 -1
  73. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +122 -147
  74. package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +55 -25
  75. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +290 -252
  76. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +1 -1
  77. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +30 -32
  78. package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +68 -67
  79. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +72 -126
  80. package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +1 -3
  81. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +10 -10
  82. package/dataflow/internal/process/functions/call/built-in/built-in-quote.d.ts +4 -4
  83. package/dataflow/internal/process/functions/call/built-in/built-in-quote.js +9 -35
  84. package/dataflow/internal/process/functions/call/built-in/built-in-rm.js +8 -3
  85. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.d.ts +1 -1
  86. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.js +12 -28
  87. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.d.ts +1 -1
  88. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.js +40 -118
  89. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.d.ts +1 -3
  90. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.js +16 -24
  91. package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +2 -0
  92. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +93 -102
  93. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.d.ts +7 -3
  94. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +173 -114
  95. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.d.ts +1 -3
  96. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.js +22 -27
  97. package/dataflow/internal/process/functions/call/built-in/built-in-with.d.ts +1 -1
  98. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +34 -9
  99. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +1 -1
  100. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +120 -93
  101. package/dataflow/internal/process/functions/call/common.d.ts +1 -3
  102. package/dataflow/internal/process/functions/call/common.js +36 -76
  103. package/dataflow/internal/process/functions/call/deferred.d.ts +6 -2
  104. package/dataflow/internal/process/functions/call/deferred.js +60 -58
  105. package/dataflow/internal/process/functions/call/known-call-handling.d.ts +4 -2
  106. package/dataflow/internal/process/functions/call/known-call-handling.js +65 -68
  107. package/dataflow/internal/process/functions/call/named-call-handling.d.ts +3 -5
  108. package/dataflow/internal/process/functions/call/named-call-handling.js +44 -21
  109. package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
  110. package/dataflow/internal/process/functions/call/quoted.js +162 -172
  111. package/dataflow/internal/process/functions/call/unnamed-call-handling.js +17 -38
  112. package/dataflow/internal/process/functions/process-parameter.js +25 -32
  113. package/dataflow/internal/process/process-symbol.js +3 -6
  114. package/dataflow/internal/program-order.d.ts +26 -0
  115. package/dataflow/internal/program-order.js +49 -0
  116. package/documentation/doc-capabilities.js +4 -1
  117. package/documentation/wiki-interface.js +4 -0
  118. package/documentation/wiki-setup.js +23 -0
  119. package/engines.js +11 -3
  120. package/linter/linter-executor.js +19 -1
  121. package/linter/linter-rules.d.ts +17 -8
  122. package/linter/rules/absolute-path.js +63 -87
  123. package/linter/rules/file-path-validity.d.ts +4 -2
  124. package/linter/rules/file-path-validity.js +117 -80
  125. package/linter/rules/namespace-access.js +21 -33
  126. package/linter/rules/naming-convention.d.ts +1 -1
  127. package/linter/rules/naming-convention.js +60 -48
  128. package/linter/rules/network-functions.js +11 -7
  129. package/linter/rules/no-leaked-credentials.js +15 -18
  130. package/linter/rules/problematic-inputs.d.ts +2 -1
  131. package/linter/rules/problematic-inputs.js +39 -59
  132. package/linter/rules/roxygen-arguments.d.ts +9 -4
  133. package/linter/rules/roxygen-arguments.js +29 -48
  134. package/linter/rules/seeded-randomness.d.ts +7 -4
  135. package/linter/rules/seeded-randomness.js +130 -89
  136. package/linter/rules/stop-with-call-arg.d.ts +4 -2
  137. package/linter/rules/stop-with-call-arg.js +25 -22
  138. package/linter/rules/unclosed-connection.js +31 -24
  139. package/linter/rules/undefined-symbol.d.ts +4 -6
  140. package/linter/rules/undefined-symbol.js +58 -75
  141. package/linter/rules/unescaped-arguments.js +18 -44
  142. package/linter/rules/unused-definition.d.ts +6 -0
  143. package/linter/rules/unused-definition.js +105 -145
  144. package/linter/rules/unused-import.d.ts +3 -0
  145. package/linter/rules/unused-import.js +25 -43
  146. package/package.json +5 -2
  147. package/project/flowr-analyzer.js +6 -2
  148. package/project/plugins/file-plugins/files/flowr-jupyter-file.d.ts +2 -2
  149. package/project/plugins/file-plugins/files/flowr-jupyter-file.js +8 -12
  150. package/project/plugins/file-plugins/files/flowr-rda-file.d.ts +14 -5
  151. package/project/plugins/file-plugins/files/flowr-rda-file.js +35 -24
  152. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.d.ts +7 -5
  153. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.js +34 -56
  154. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +18 -42
  155. package/project/sigdb/decompress.js +3 -1
  156. package/project/sigdb/sigdb-download.js +9 -6
  157. package/queries/base-query-format.js +1 -2
  158. package/queries/catalog/absint-query/absint-query-format.js +1 -1
  159. package/queries/catalog/config-query/config-query-executor.js +9 -2
  160. package/queries/catalog/dataflow-query/dataflow-query-executor.js +2 -2
  161. package/queries/catalog/dataflow-query/dataflow-query-format.d.ts +2 -0
  162. package/queries/catalog/dataflow-query/dataflow-query-format.js +4 -7
  163. package/queries/catalog/dependencies-query/dependencies-query-executor.js +50 -114
  164. package/queries/catalog/dependencies-query/function-info/library-functions.d.ts +5 -9
  165. package/queries/catalog/dependencies-query/function-info/library-functions.js +6 -0
  166. package/queries/catalog/dependencies-query/function-info/read-functions.js +1 -1
  167. package/queries/catalog/dependencies-query/function-info/write-functions.js +7 -3
  168. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +4 -4
  169. package/queries/catalog/input-sources-query/simple-input-classifier.js +192 -301
  170. package/r-bridge/lang-4.x/ast/model/model.d.ts +3 -1
  171. package/r-bridge/lang-4.x/ast/model/model.js +19 -43
  172. package/r-bridge/lang-4.x/ast/model/nodes/r-access.d.ts +1 -0
  173. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.d.ts +3 -0
  174. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.js +5 -12
  175. package/r-bridge/lang-4.x/ast/model/nodes/r-binary-op.d.ts +1 -0
  176. package/r-bridge/lang-4.x/ast/model/nodes/r-break.d.ts +1 -0
  177. package/r-bridge/lang-4.x/ast/model/nodes/r-comment.d.ts +1 -0
  178. package/r-bridge/lang-4.x/ast/model/nodes/r-expression-list.d.ts +1 -0
  179. package/r-bridge/lang-4.x/ast/model/nodes/r-for-loop.d.ts +1 -0
  180. package/r-bridge/lang-4.x/ast/model/nodes/r-function-call.d.ts +1 -0
  181. package/r-bridge/lang-4.x/ast/model/nodes/r-function-definition.d.ts +1 -0
  182. package/r-bridge/lang-4.x/ast/model/nodes/r-if-then-else.d.ts +1 -0
  183. package/r-bridge/lang-4.x/ast/model/nodes/r-line-directive.d.ts +1 -0
  184. package/r-bridge/lang-4.x/ast/model/nodes/r-logical.d.ts +1 -0
  185. package/r-bridge/lang-4.x/ast/model/nodes/r-next.d.ts +1 -0
  186. package/r-bridge/lang-4.x/ast/model/nodes/r-number.d.ts +1 -0
  187. package/r-bridge/lang-4.x/ast/model/nodes/r-parameter.d.ts +1 -0
  188. package/r-bridge/lang-4.x/ast/model/nodes/r-pipe.d.ts +1 -0
  189. package/r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop.d.ts +1 -0
  190. package/r-bridge/lang-4.x/ast/model/nodes/r-string.d.ts +1 -0
  191. package/r-bridge/lang-4.x/ast/model/nodes/r-symbol.d.ts +1 -0
  192. package/r-bridge/lang-4.x/ast/model/nodes/r-unary-op.d.ts +1 -0
  193. package/r-bridge/lang-4.x/ast/model/nodes/r-while-loop.d.ts +1 -0
  194. package/r-bridge/roxygen2/roxygen-parse.js +28 -42
  195. package/r-bridge/shell-executor.js +2 -0
  196. package/r-bridge/shell.js +8 -0
  197. package/search/search-executor/search-enrichers.js +25 -37
  198. package/slicing/static/slice-call.d.ts +4 -3
  199. package/slicing/static/slice-call.js +56 -88
  200. package/slicing/static/static-slicer.js +120 -79
  201. package/slicing/static/visiting-queue.d.ts +4 -6
  202. package/slicing/static/visiting-queue.js +9 -13
  203. package/util/files.d.ts +6 -0
  204. package/util/files.js +12 -0
  205. package/util/parallel.js +2 -0
  206. package/util/proc.js +2 -1
  207. package/util/r-base-packages.d.ts +2 -0
  208. package/util/r-base-packages.js +23 -13
  209. package/util/secure-mode.d.ts +42 -0
  210. package/util/secure-mode.js +70 -0
  211. package/util/text/playground-link.d.ts +12 -3
  212. package/util/text/playground-link.js +18 -27
  213. package/util/version.js +4 -10
@@ -16,6 +16,7 @@ exports.reapplyLoopExitPoints = reapplyLoopExitPoints;
16
16
  exports.accessedFieldName = accessedFieldName;
17
17
  exports.linkFieldReads = linkFieldReads;
18
18
  exports.bindAccessedField = bindAccessedField;
19
+ exports.separateClosureInstances = separateClosureInstances;
19
20
  const defaultmap_1 = require("../../util/collections/defaultmap");
20
21
  const r_function_call_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
21
22
  const function_semantics_1 = require("../fn/function-semantics");
@@ -24,7 +25,6 @@ const assert_1 = require("../../util/assert");
24
25
  const log_1 = require("../../util/log");
25
26
  const node_id_1 = require("../../r-bridge/lang-4.x/ast/model/processing/node-id");
26
27
  const identifier_1 = require("../environments/identifier");
27
- const graph_1 = require("../graph/graph");
28
28
  const logger_1 = require("../logger");
29
29
  const edge_1 = require("../graph/edge");
30
30
  const vertex_1 = require("../graph/vertex");
@@ -40,6 +40,9 @@ const resolve_helper_1 = require("../environments/resolve-helper");
40
40
  const r_function_definition_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
41
41
  const r_binary_op_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-binary-op");
42
42
  const r_symbol_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-symbol");
43
+ const frame_reflection_1 = require("../fn/frame-reflection");
44
+ const ValueFlowBits = edge_1.EdgeType.Reads | edge_1.EdgeType.DefinedBy | edge_1.EdgeType.DefinedByOnCall;
45
+ const FCallLinkReadBits = edge_1.EdgeType.Reads | edge_1.EdgeType.Calls | edge_1.EdgeType.DefinedByOnCall;
43
46
  /**
44
47
  * Find all reads within the graph that do not reference a local definition in the graph.
45
48
  */
@@ -50,7 +53,6 @@ function findNonLocalReads(graph, ignores = new Set()) {
50
53
  defs.add(id);
51
54
  }
52
55
  }
53
- /* find all variable use ids which do not link to a given id */
54
56
  const nonLocalReads = [];
55
57
  /* the tag already says what the reference is, so no vertex has to be fetched to decide */
56
58
  for (const [tag, type] of [[vertex_1.VertexType.Use, identifier_1.ReferenceType.Variable], [vertex_1.VertexType.FunctionCall, identifier_1.ReferenceType.Function]]) {
@@ -58,13 +60,10 @@ function findNonLocalReads(graph, ignores = new Set()) {
58
60
  if (ignores.has(nodeId)) {
59
61
  continue;
60
62
  }
61
- const outgoing = graph.outgoingEdges(nodeId);
62
- const name = node_id_1.NodeId.recoverName(nodeId, graph.idMap);
63
- const identifierRef = { nodeId, name, type };
64
63
  /* control flow edges say nothing about what a name resolves to, so they do not count as a link */
65
64
  let linked = false;
66
65
  let nonLocal = false;
67
- for (const [target, e] of outgoing ?? graph_1.NoEdges) {
66
+ for (const [target, e] of graph.edgesFrom(nodeId)) {
68
67
  if (edge_1.DfEdge.isOnlyControlFlow(e)) {
69
68
  continue;
70
69
  }
@@ -75,7 +74,7 @@ function findNonLocalReads(graph, ignores = new Set()) {
75
74
  }
76
75
  }
77
76
  if (!linked || nonLocal) {
78
- nonLocalReads.push(identifierRef);
77
+ nonLocalReads.push({ nodeId, name: node_id_1.NodeId.recoverName(nodeId, graph.idMap), type });
79
78
  }
80
79
  }
81
80
  }
@@ -87,9 +86,8 @@ function findNonLocalReads(graph, ignores = new Set()) {
87
86
  function produceNameSharedIdMap(references) {
88
87
  const nameIdShares = new defaultmap_1.DefaultMap(() => []);
89
88
  for (const reference of references) {
90
- const rn = reference.name;
91
- if (rn) {
92
- nameIdShares.get(rn).push(reference);
89
+ if (reference.name) {
90
+ nameIdShares.get(reference.name).push(reference);
93
91
  }
94
92
  }
95
93
  return nameIdShares;
@@ -102,40 +100,11 @@ function produceNameSharedIdMap(references) {
102
100
  function linkArgumentsOnCall(args, params, graph) {
103
101
  return function_semantics_1.FunctionSemantics.call.match.onCallAndLink(args, params, graph);
104
102
  }
105
- /**
106
- * Links the function call arguments to the target function definition and returns a map from argument ids to parameter ids.
107
- */
108
- function linkFunctionCallArguments(targetId, idMap, functionCallName, functionRootId, callArgs, finalGraph) {
109
- const linkedFunction = idMap.get(targetId);
110
- if (linkedFunction === undefined) {
111
- logger_1.dataflowLogger.trace(`no fdef found for ${functionCallName} (${functionRootId})`);
112
- return;
113
- }
114
- if (!r_function_definition_1.RFunctionDefinition.is(linkedFunction)) {
115
- logger_1.dataflowLogger.trace(`function call definition base ${functionCallName} does not lead to a function definition (${functionRootId}) but got ${linkedFunction.type}`);
116
- return;
117
- }
118
- return linkArgumentsOnCall(callArgs, linkedFunction.parameters, finalGraph);
119
- }
120
- const NoCalleeEnvironments = [];
121
- function environmentsOfCallee(info) {
122
- if (info.name === undefined || info.environment === undefined) {
123
- return NoCalleeEnvironments;
124
- }
125
- return (resolve_helper_1.Resolve.byName(info.name, info.environment) ?? []).filter(identifier_1.hasEnvState).map(d => d.envState);
126
- }
127
- const AliasFlowBits = edge_1.EdgeType.Reads | edge_1.EdgeType.DefinedBy | edge_1.EdgeType.DefinedByOnCall;
128
- /** `start` and everything that takes over what it reads: aliases and nested uses of an environment all share it. */
129
- function aliasReadsOf(graph, start) {
130
- const found = new Set([start]);
131
- for (const id of found) {
132
- for (const [from, edge] of graph.edgesTo(id)) {
133
- if (edge_1.DfEdge.includesType(edge, AliasFlowBits)) {
134
- found.add(from);
135
- }
136
- }
103
+ function environmentsOfCallee(info, environment) {
104
+ if (info.name === undefined) {
105
+ return [];
137
106
  }
138
- return found;
107
+ return (resolve_helper_1.Resolve.byName(info.name, environment) ?? []).filter(identifier_1.hasEnvState).map(d => d.envState);
139
108
  }
140
109
  /**
141
110
  * Links a function call with a single target function definition.
@@ -144,85 +113,70 @@ function linkFunctionCallWithSingleTarget(graph, { subflow: fnSubflow, exitPoint
144
113
  const id = info.id;
145
114
  const environment = info.environment;
146
115
  if (environment !== undefined) {
147
- let callee = undefined;
148
- // for each open ingoing reference, try to resolve it here, and if so, add a read edge from the call to signal that it reads it
116
+ let callee;
117
+ /* for each open ingoing reference, try to resolve it here, and if so, add a read edge from the call to signal that it reads it */
149
118
  for (const ingoing of fnSubflow.in) {
150
119
  const name = ingoing.name;
151
120
  if (name === undefined) {
152
121
  continue;
153
122
  }
154
- callee ??= environmentsOfCallee(info);
155
- let defs = undefined;
156
- for (const env of callee) {
157
- defs = resolve_helper_1.Resolve.byNameAndType(name, env, ingoing.type);
158
- if (defs !== undefined) {
159
- break;
123
+ callee ??= environmentsOfCallee(info, environment);
124
+ const inCallee = callee.values().map(env => resolve_helper_1.Resolve.byNameAndType(name, env, ingoing.type)).find(assert_1.isNotUndefined);
125
+ const defs = inCallee ?? resolve_helper_1.Resolve.byNameAndType(name, environment, ingoing.type) ?? [];
126
+ for (const { nodeId, type, value, definedAt, envState } of defs) {
127
+ if (fnSubflow.graph.has(nodeId) || node_id_1.NodeId.isBuiltIn(nodeId)) {
128
+ continue;
160
129
  }
161
- }
162
- defs ??= resolve_helper_1.Resolve.byNameAndType(name, environment, ingoing.type);
163
- if (defs === undefined) {
164
- continue;
165
- }
166
- for (const d of defs) {
167
- const { nodeId, type, value, definedAt, envState } = d;
168
- if (!fnSubflow.graph.has(nodeId) && !node_id_1.NodeId.isBuiltIn(nodeId)) {
169
- graph.addEdge(ingoing.nodeId, nodeId, edge_1.EdgeType.DefinedByOnCall);
170
- graph.addEdge(id, nodeId, edge_1.EdgeType.DefinesOnCall);
171
- for (const read of envState === undefined ? [] : aliasReadsOf(graph, ingoing.nodeId)) {
172
- bindAccessedField(graph, read, envState, idMap);
173
- const write = fieldAssignmentOf(read, idMap);
174
- if (write !== undefined) {
175
- graph.addEdge(id, write, edge_1.EdgeType.Reads);
176
- graph.addEdge(nodeId, id, edge_1.EdgeType.SideEffectOnCall);
177
- /* a write in a nested function only runs when that function is called */
178
- const within = model_1.RNode.findEnclosing(write, idMap, r_function_definition_1.RFunctionDefinition.is);
179
- for (const [caller, edge] of within === undefined || within === fnId ? [] : graph.edgesTo(within)) {
180
- if (edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Calls)) {
181
- graph.addEdge(write, caller, edge_1.EdgeType.Reads);
182
- }
183
- }
184
- }
185
- }
186
- if (definedAt !== undefined && definedAt !== nodeId && !node_id_1.NodeId.isBuiltIn(definedAt)
187
- && graph.hasVertex(definedAt) && !vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(nodeId))) {
188
- graph.addEdge(id, definedAt, edge_1.EdgeType.Reads);
189
- }
190
- if (type === identifier_1.ReferenceType.Function && ingoing.type === identifier_1.ReferenceType.S7MethodPrefix && Array.isArray(value)) {
191
- for (const v of value) {
192
- graph.addEdge(id, v, edge_1.EdgeType.Calls);
193
- graph.addEdge(ingoing.nodeId, v, edge_1.EdgeType.Calls);
194
- const vInfo = graph.getVertex(v);
195
- if (vInfo && vertex_2.DfgVertex.isFunctionDefinition(vInfo)) {
196
- vInfo.mode ??= [];
197
- if (!vInfo.mode.includes('s7')) {
198
- vInfo.mode.push('s7');
199
- }
200
- }
130
+ graph.addEdge(ingoing.nodeId, nodeId, edge_1.EdgeType.DefinedByOnCall);
131
+ graph.addEdge(id, nodeId, edge_1.EdgeType.DefinesOnCall);
132
+ if (envState !== undefined) {
133
+ linkFieldWritesOnCall(graph, id, fnId, ingoing.nodeId, nodeId, envState, idMap);
134
+ }
135
+ if (definedAt !== undefined && definedAt !== nodeId && !node_id_1.NodeId.isBuiltIn(definedAt)
136
+ && graph.hasVertex(definedAt) && !vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(nodeId))) {
137
+ graph.addEdge(id, definedAt, edge_1.EdgeType.Reads);
138
+ }
139
+ if (type === identifier_1.ReferenceType.Function && ingoing.type === identifier_1.ReferenceType.S7MethodPrefix && Array.isArray(value)) {
140
+ for (const v of value) {
141
+ graph.addEdge(id, v, edge_1.EdgeType.Calls);
142
+ graph.addEdge(ingoing.nodeId, v, edge_1.EdgeType.Calls);
143
+ const vInfo = graph.getVertex(v);
144
+ if (vertex_2.DfgVertex.isFunctionDefinition(vInfo) && !vInfo.mode?.includes('s7')) {
145
+ (vInfo.mode ??= []).push('s7');
201
146
  }
202
147
  }
203
148
  }
204
149
  }
205
150
  }
206
151
  }
207
- const propagateExitPoints = [];
152
+ for (const node of fnSubflow.graph) {
153
+ if (!vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(node)) || fieldAssignmentOf(node, idMap) === undefined) {
154
+ continue;
155
+ }
156
+ for (const captured of (0, frame_reflection_1.edgeTargets)(graph, node, edge_1.EdgeType.Reads)) {
157
+ const frame = fnSubflow.graph.has(captured) ? undefined : model_1.RNode.findEnclosing(captured, idMap, r_function_definition_1.RFunctionDefinition.is);
158
+ const owner = frame === undefined ? undefined : graph.getVertex(frame);
159
+ const name = node_id_1.NodeId.recoverName(captured, idMap);
160
+ const envState = vertex_2.DfgVertex.isFunctionDefinition(owner) && name !== undefined ? resolve_helper_1.Resolve.byName(name, owner.subflow.environment)?.filter(identifier_1.hasEnvState).find(d => d.nodeId === captured)?.envState : undefined;
161
+ if (envState !== undefined) {
162
+ linkFieldWritesOnCall(graph, id, fnId, node, captured, envState, idMap);
163
+ }
164
+ }
165
+ }
208
166
  for (const exitPoint of exitPoints) {
209
167
  graph.addEdge(id, exitPoint.nodeId, edge_1.EdgeType.Returns);
210
- if ((0, info_1.doesExitPointPropagateCalls)(exitPoint.type)) {
211
- propagateExitPoints.push(exitPoint);
212
- }
213
168
  }
214
- const defName = node_id_1.NodeId.recoverName(fnId, idMap);
215
- (0, log_1.expensiveTrace)(logger_1.dataflowLogger, () => `recording expr-list-level call from ${node_id_1.NodeId.recoverName(info.id, idMap)} to ${defName}`);
169
+ (0, log_1.expensiveTrace)(logger_1.dataflowLogger, () => `recording expr-list-level call from ${node_id_1.NodeId.recoverName(id, idMap)} to ${node_id_1.NodeId.recoverName(fnId, idMap)}`);
216
170
  graph.addEdge(id, fnId, edge_1.EdgeType.Calls);
217
- applyForForcedArgs(graph, info.id, params, linkFunctionCallArguments(fnId, idMap, defName, id, info.args, graph));
218
- return propagateExitPoints;
171
+ const fn = idMap.get(fnId);
172
+ if (r_function_definition_1.RFunctionDefinition.is(fn)) {
173
+ applyForForcedArgs(graph, id, params, linkArgumentsOnCall(info.args, fn.parameters, graph));
174
+ }
175
+ return exitPoints.filter(e => (0, info_1.doesExitPointPropagateCalls)(e.type));
219
176
  }
220
177
  /** for each parameter that we link that gets forced, add a reads edge from the call to argument to show that it reads it */
221
- function applyForForcedArgs(graph, callId, readParams, maps) {
222
- if (maps === undefined) {
223
- return;
224
- }
225
- for (const [arg, param] of maps.entries()) {
178
+ function applyForForcedArgs(graph, callId, readParams, argToParam) {
179
+ for (const [arg, param] of argToParam) {
226
180
  if (readParams[String(param)]) {
227
181
  graph.addEdge(callId, arg, edge_1.EdgeType.Reads);
228
182
  /* a call in the argument only runs when the call forces it, so its side effects depend on the call */
@@ -232,35 +186,49 @@ function applyForForcedArgs(graph, callId, readParams, maps) {
232
186
  }
233
187
  }
234
188
  }
235
- const FCallLinkReadBits = edge_1.EdgeType.Reads | edge_1.EdgeType.Calls | edge_1.EdgeType.DefinedByOnCall;
236
- /* there is _a lot_ potential for optimization here */
237
- function linkFunctionCall(graph, id, info, idMap, thisGraph, calledFunctionDefinitions) {
238
- const edges = graph.outgoingEdges(id);
239
- if (edges === undefined) {
240
- return;
189
+ /** The call `id` of `fnId` writes the fields of the environment `binding` holds through every alias of `start`. */
190
+ function linkFieldWritesOnCall(graph, id, fnId, start, binding, envState, idMap) {
191
+ const aliases = new Set([start]);
192
+ for (const alias of aliases) {
193
+ for (const from of (0, frame_reflection_1.edgeSources)(graph, alias, ValueFlowBits)) {
194
+ aliases.add(from);
195
+ }
196
+ }
197
+ for (const read of aliases) {
198
+ bindAccessedField(graph, read, envState, idMap);
199
+ const write = fieldAssignmentOf(read, idMap);
200
+ if (write !== undefined) {
201
+ graph.addEdge(id, write, edge_1.EdgeType.Reads);
202
+ graph.addEdge(binding, id, edge_1.EdgeType.SideEffectOnCall);
203
+ /* a write in a nested function only runs when that function is called */
204
+ const within = model_1.RNode.findEnclosing(write, idMap, r_function_definition_1.RFunctionDefinition.is);
205
+ for (const caller of within === undefined || within === fnId ? [] : (0, frame_reflection_1.edgeSources)(graph, within, edge_1.EdgeType.Calls)) {
206
+ graph.addEdge(write, caller, edge_1.EdgeType.Reads);
207
+ }
208
+ }
241
209
  }
242
- const functionDefinitionReadIds = new Set();
243
- for (const [t, e] of edges.entries()) {
210
+ }
211
+ function linkFunctionCall(graph, id, info, idMap, isRoot) {
212
+ const readIds = new Set();
213
+ for (const [t, e] of graph.edgesFrom(id)) {
244
214
  if (!node_id_1.NodeId.isBuiltIn(t) && edge_1.DfEdge.doesNotIncludeType(e, edge_1.EdgeType.Argument) && edge_1.DfEdge.includesType(e, FCallLinkReadBits)) {
245
- functionDefinitionReadIds.add(t);
215
+ readIds.add(t);
246
216
  }
247
217
  }
248
- const [functionDefs, builtIns] = getAllLinkedFunctionDefinitions(functionDefinitionReadIds, graph);
218
+ const [functionDefs, builtIns] = getAllLinkedFunctionDefinitions(readIds, graph);
249
219
  const propagateExitPoints = [];
250
- for (const def of functionDefs.values()) {
251
- // we can skip this if we already linked it
252
- const oEdge = graph.outgoingEdges(id)?.get(def.id);
253
- if (oEdge && edge_1.DfEdge.includesType(oEdge, edge_1.EdgeType.Calls)) {
254
- continue;
255
- }
256
- for (const ep of linkFunctionCallWithSingleTarget(graph, def, info, idMap)) {
257
- propagateExitPoints.push(ep);
220
+ for (const def of functionDefs) {
221
+ /* we can skip this if we already linked it */
222
+ if (!(0, frame_reflection_1.hasEdgeOfType)(graph, id, def.id, edge_1.EdgeType.Calls)) {
223
+ propagateExitPoints.push(...linkFunctionCallWithSingleTarget(graph, def, info, idMap));
258
224
  }
259
225
  }
260
- if (thisGraph.isRoot(id) && (functionDefs.size > 0 || builtIns.size > 0)) {
261
- /* a built-in reached through a variable (`p <- par; p(...)`) was not processed as that built-in */
262
- calledFunctionDefinitions.push({ functionCall: id, called: functionDefs.values().toArray(), propagateExitPoints, calledBuiltIns: builtIns.size > 0 ? [...builtIns] : undefined });
226
+ if (!isRoot || (functionDefs.size === 0 && builtIns.size === 0)) {
227
+ return undefined;
263
228
  }
229
+ /* a built-in reached through a variable (`p <- par; p(...)`) was not processed as that built-in */
230
+ const calledBuiltIns = builtIns.size > 0 ? [...builtIns] : undefined;
231
+ return { functionCall: id, called: [...functionDefs], propagateExitPoints, calledBuiltIns };
264
232
  }
265
233
  /**
266
234
  * Returns the called functions within `graph` (ideally a superset of `thisGraph`, the graph searched for calls), which
@@ -269,62 +237,47 @@ function linkFunctionCall(graph, id, info, idMap, thisGraph, calledFunctionDefin
269
237
  function linkFunctionCalls(graph, idMap, thisGraph,
270
238
  /** calls `graph` knows of that `thisGraph` does not, as a read that became a call */
271
239
  alsoCalls = []) {
272
- const calledFunctionDefinitions = [];
240
+ const called = [];
273
241
  for (const [id, info] of thisGraph.verticesOfType(vertex_1.VertexType.FunctionCall)) {
274
242
  if (!info.onlyBuiltin) {
275
- linkFunctionCall(graph, id, info, idMap, thisGraph, calledFunctionDefinitions);
243
+ called.push(linkFunctionCall(graph, id, info, idMap, thisGraph.isRoot(id)));
276
244
  }
277
245
  }
278
246
  for (const id of alsoCalls) {
279
247
  const info = graph.getVertex(id);
280
248
  if (vertex_2.DfgVertex.isFunctionCall(info)) {
281
- linkFunctionCall(graph, id, info, idMap, graph, calledFunctionDefinitions);
249
+ called.push(linkFunctionCall(graph, id, info, idMap, graph.isRoot(id)));
282
250
  }
283
251
  }
284
- return calledFunctionDefinitions;
252
+ return called.filter(assert_1.isNotUndefined);
285
253
  }
286
254
  /**
287
255
  * convenience function returning all known call targets, as well as the name source which defines them
288
256
  */
289
257
  function getAllFunctionCallTargets(call, graph, environment) {
290
- const found = new Set();
291
- const callVertex = graph.get(call, true);
292
- if (callVertex === undefined) {
293
- return [];
294
- }
295
- const [info, outgoingEdges] = callVertex;
258
+ const info = graph.getVertex(call);
296
259
  if (!vertex_2.DfgVertex.isFunctionCall(info)) {
297
260
  return [];
298
261
  }
299
262
  const known = environment ?? info.environment;
263
+ const refType = info.origin.includes(built_in_proc_name_1.BuiltInProcName.S3Dispatch) ? identifier_1.ReferenceType.S3MethodPrefix :
264
+ info.origin.includes(built_in_proc_name_1.BuiltInProcName.S7Dispatch) ? identifier_1.ReferenceType.S7MethodPrefix : identifier_1.ReferenceType.Function;
300
265
  let functionCallDefs = [];
301
- if (known !== undefined) {
302
- const refType = info.origin.includes(built_in_proc_name_1.BuiltInProcName.S3Dispatch) ? identifier_1.ReferenceType.S3MethodPrefix :
303
- info.origin.includes(built_in_proc_name_1.BuiltInProcName.S7Dispatch) ? identifier_1.ReferenceType.S7MethodPrefix : identifier_1.ReferenceType.Function;
304
- if (info.name !== undefined && !identifier_1.Identifier.getName(info.name).startsWith(unnamed_call_handling_1.UnnamedFunctionCallPrefix)) {
305
- functionCallDefs = resolve_helper_1.Resolve.byNameAndType(info.name, known, refType)?.map(d => d.nodeId) ?? [];
306
- }
266
+ if (known !== undefined && info.name !== undefined && !identifier_1.Identifier.getName(info.name).startsWith(unnamed_call_handling_1.UnnamedFunctionCallPrefix)) {
267
+ functionCallDefs = resolve_helper_1.Resolve.byNameAndType(info.name, known, refType)?.map(d => d.nodeId) ?? [];
307
268
  }
308
- const followBits = info.origin.includes(built_in_proc_name_1.BuiltInProcName.Unnamed) ? FCallLinkReadBits : edge_1.EdgeType.Calls;
309
- for (const [target, outgoingEdge] of outgoingEdges.entries()) {
310
- if (edge_1.DfEdge.includesType(outgoingEdge, followBits) && edge_1.DfEdge.doesNotIncludeType(outgoingEdge, edge_1.EdgeType.Argument) && (known !== undefined || !node_id_1.NodeId.isBuiltIn(target))) {
269
+ const followBits = info.origin.includes(built_in_proc_name_1.BuiltInProcName.Unnamed) || functionCallDefs.length === 0 ? FCallLinkReadBits : edge_1.EdgeType.Calls;
270
+ for (const [target, edge] of graph.edgesFrom(call)) {
271
+ if (edge_1.DfEdge.includesType(edge, followBits) && edge_1.DfEdge.doesNotIncludeType(edge, edge_1.EdgeType.Argument) && (known !== undefined || !node_id_1.NodeId.isBuiltIn(target))) {
311
272
  functionCallDefs.push(target);
312
273
  }
313
274
  }
314
- if (functionCallDefs.length > 0) {
315
- const [functionCallTargets, builtInTargets] = getAllLinkedFunctionDefinitions(new Set(functionCallDefs), graph);
316
- for (const target of functionCallTargets) {
317
- found.add(target.id);
318
- }
319
- for (const arr of [builtInTargets, functionCallDefs]) {
320
- for (const target of arr) {
321
- found.add(target);
322
- }
323
- }
275
+ if (functionCallDefs.length === 0) {
276
+ return [];
324
277
  }
325
- return Array.from(found);
278
+ const [targets, builtIns] = getAllLinkedFunctionDefinitions(new Set(functionCallDefs), graph);
279
+ return [...new Set([...targets.values().map(t => t.id), ...builtIns, ...functionCallDefs])];
326
280
  }
327
- const LinkedFnFollowBits = edge_1.EdgeType.Reads | edge_1.EdgeType.DefinedBy | edge_1.EdgeType.DefinedByOnCall;
328
281
  /**
329
282
  * Finds all linked function definitions starting from the given read ids; expects the caller to already have resolved
330
283
  * the accessed objects (first layer of reads/returns/calls/...). For call targets, use {@link getAllFunctionCallTargets} instead.
@@ -332,9 +285,6 @@ const LinkedFnFollowBits = edge_1.EdgeType.Reads | edge_1.EdgeType.DefinedBy | e
332
285
  function getAllLinkedFunctionDefinitions(functionDefinitionReadIds, dataflowGraph) {
333
286
  const result = new Set();
334
287
  const builtIns = new Set();
335
- if (functionDefinitionReadIds.size === 0) {
336
- return [result, builtIns];
337
- }
338
288
  const potential = Array.from(functionDefinitionReadIds, id => [id, false]);
339
289
  const visited = new Set();
340
290
  const expanded = new Set();
@@ -355,26 +305,24 @@ function getAllLinkedFunctionDefinitions(functionDefinitionReadIds, dataflowGrap
355
305
  if (vertex === undefined) {
356
306
  continue;
357
307
  }
358
- if (vertex.subflow !== undefined) {
308
+ else if (vertex.subflow !== undefined) {
359
309
  result.add(vertex);
360
310
  continue;
361
311
  }
362
- const outgoing = dataflowGraph.outgoingEdges(cid);
363
- if (!outgoing) {
364
- continue;
365
- }
366
312
  const nextViaAccess = viaAccess || vertex_2.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Access);
367
313
  const isSkipType = vertex_2.DfgVertex.isFunctionCall(vertex) || (vertex_2.DfgVertex.isVariableDefinition(vertex) && vertex.par && !nextViaAccess);
368
314
  let hasReturnEdge = false;
369
315
  let followTargets;
370
- for (const [target, e] of outgoing) {
371
- if (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Returns)) {
316
+ /* a list handed on as a whole (a loop, an apply) yields its entries, an access the entry it names */
317
+ const handedBack = !viaAccess && vertex_2.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.List) ? edge_1.EdgeType.Returns | edge_1.EdgeType.Argument : edge_1.EdgeType.Returns;
318
+ for (const [target, e] of dataflowGraph.edgesFrom(cid)) {
319
+ if (edge_1.DfEdge.includesType(e, handedBack)) {
372
320
  hasReturnEdge = true;
373
321
  if (!visited.has(target)) {
374
322
  potential.push([target, nextViaAccess]);
375
323
  }
376
324
  }
377
- else if (!isSkipType && !hasReturnEdge && edge_1.DfEdge.includesType(e, LinkedFnFollowBits) && !visited.has(target)) {
325
+ else if (!isSkipType && !hasReturnEdge && edge_1.DfEdge.includesType(e, ValueFlowBits) && !visited.has(target)) {
378
326
  (followTargets ??= []).push(target);
379
327
  }
380
328
  }
@@ -409,8 +357,7 @@ function linkExpressionIn(graph, expr, environment, idMap) {
409
357
  }
410
358
  return false;
411
359
  });
412
- const unresolved = [];
413
- linkInputs(references, environment, unresolved, graph, false);
360
+ const unresolved = linkInputs(references, environment, [], graph, false);
414
361
  for (const call of references.filter(r => r.type === identifier_1.ReferenceType.Function)) {
415
362
  linkCallToDefinitions(graph, call.nodeId, resolve_helper_1.Resolve.byNameAndType(call.name, environment, identifier_1.ReferenceType.Function)?.map(d => d.nodeId) ?? [], idMap);
416
363
  }
@@ -423,16 +370,17 @@ function linkCallToDefinitions(graph, callId, definitions, idMap) {
423
370
  return;
424
371
  }
425
372
  for (const fn of getAllLinkedFunctionDefinitions(new Set(definitions), graph)[0]) {
426
- if (!edge_1.DfEdge.includesType(graph.outgoingEdges(callId)?.get(fn.id) ?? { types: 0 }, edge_1.EdgeType.Calls)) {
373
+ if (!(0, frame_reflection_1.hasEdgeOfType)(graph, callId, fn.id, edge_1.EdgeType.Calls)) {
427
374
  linkFunctionCallWithSingleTarget(graph, fn, call, idMap);
428
375
  }
429
376
  }
430
377
  }
431
378
  /**
432
- * Links a set of read variables to definitions in `environmentInformation`; each reference that cannot be linked is
433
- * added to `givenInputs` (marked maybe if `maybeForRemaining`), and the extended list is returned.
379
+ * Links a set of read variables to definitions in `environmentInformation`; each reference that cannot be linked (or
380
+ * links to built-ins only) is added to `givenInputs` (marked maybe if `maybeForRemaining`), and the extended list is returned.
381
+ * @param alsoFrom - a node that reads every definition found as well
434
382
  */
435
- function linkInputs(referencesToLinkAgainstEnvironment, environmentInformation, givenInputs, graph, maybeForRemaining) {
383
+ function linkInputs(referencesToLinkAgainstEnvironment, environmentInformation, givenInputs, graph, maybeForRemaining, alsoFrom) {
436
384
  for (const bodyInput of referencesToLinkAgainstEnvironment) {
437
385
  const probableTarget = bodyInput.name ? resolve_helper_1.Resolve.byNameAndType(bodyInput.name, environmentInformation, bodyInput.type) : undefined;
438
386
  if (probableTarget === undefined) {
@@ -440,19 +388,17 @@ function linkInputs(referencesToLinkAgainstEnvironment, environmentInformation,
440
388
  bodyInput.cds ??= [];
441
389
  }
442
390
  givenInputs.push(bodyInput);
391
+ continue;
443
392
  }
444
- else {
445
- let allBuiltIn = true;
446
- for (const target of probableTarget) {
447
- graph.addEdge(bodyInput.nodeId, target.nodeId, edge_1.EdgeType.Reads);
448
- if (!(0, identifier_1.isReferenceType)(target.type, identifier_1.ReferenceType.BuiltInConstant | identifier_1.ReferenceType.BuiltInFunction)) {
449
- allBuiltIn = false;
450
- }
451
- }
452
- if (allBuiltIn) {
453
- givenInputs.push(bodyInput);
393
+ for (const target of probableTarget) {
394
+ graph.addEdge(bodyInput.nodeId, target.nodeId, edge_1.EdgeType.Reads);
395
+ if (alsoFrom !== undefined) {
396
+ graph.addEdge(alsoFrom, target.nodeId, edge_1.EdgeType.Reads);
454
397
  }
455
398
  }
399
+ if (probableTarget.every(t => (0, identifier_1.isReferenceType)(t.type, identifier_1.ReferenceType.BuiltInConstant | identifier_1.ReferenceType.BuiltInFunction))) {
400
+ givenInputs.push(bodyInput);
401
+ }
456
402
  }
457
403
  return givenInputs;
458
404
  }
@@ -464,11 +410,7 @@ function linkCircularRedefinitionsWithinALoop(graph, openIns, outgoing, environm
464
410
  if (environment !== undefined) {
465
411
  const outgoingIds = new Set(outgoing.map(o => o.nodeId));
466
412
  for (const [name, targets] of openIns.entries()) {
467
- const liveDefs = environment.current.memory.get(identifier_1.Identifier.getName(name));
468
- if (liveDefs === undefined) {
469
- continue;
470
- }
471
- for (const def of liveDefs) {
413
+ for (const def of environment.current.memory.get(identifier_1.Identifier.getName(name)) ?? []) {
472
414
  if (outgoingIds.has(def.nodeId)) {
473
415
  for (const target of targets) {
474
416
  graph.addEdge(target.nodeId, def.nodeId, edge_1.EdgeType.Reads);
@@ -478,17 +420,16 @@ function linkCircularRedefinitionsWithinALoop(graph, openIns, outgoing, environm
478
420
  }
479
421
  return;
480
422
  }
481
- // fallback: keep only the last definition per identifier (used when no environment is available)
423
+ /* fallback: keep only the last definition per identifier (used when no environment is available) */
482
424
  const lastOutgoing = new Map();
483
425
  for (const out of outgoing) {
484
- const on = out.name;
485
- if (on) {
486
- lastOutgoing.set(on, out);
426
+ if (out.name) {
427
+ lastOutgoing.set(out.name, out);
487
428
  }
488
429
  }
489
430
  for (const [name, targets] of openIns.entries()) {
490
- for (const { name: outName, nodeId } of lastOutgoing.values()) {
491
- if (outName !== undefined && identifier_1.Identifier.matches(outName, name)) {
431
+ for (const [outName, { nodeId }] of lastOutgoing) {
432
+ if (identifier_1.Identifier.matches(outName, name)) {
492
433
  for (const target of targets) {
493
434
  graph.addEdge(target.nodeId, nodeId, edge_1.EdgeType.Reads);
494
435
  }
@@ -496,6 +437,13 @@ function linkCircularRedefinitionsWithinALoop(graph, openIns, outgoing, environm
496
437
  }
497
438
  }
498
439
  }
440
+ function addControlDependency(holder, cd) {
441
+ if (holder.cds?.some(c => c.id === cd.id)) {
442
+ return false;
443
+ }
444
+ (holder.cds ??= []).push(cd);
445
+ return true;
446
+ }
499
447
  /**
500
448
  * Reapplies the loop exit points' control dependencies to the given identifier references.
501
449
  */
@@ -510,30 +458,12 @@ function reapplyLoopExitPoints(exits, references, graph) {
510
458
  }
511
459
  seenRefs.add(ref.nodeId);
512
460
  for (const cd of exitCds) {
513
- const { id: cId } = cd;
514
- let setVertex = false;
515
- if (ref.cds) {
516
- if (!ref.cds?.find(c => c.id === cId)) {
517
- ref.cds.push(cd);
518
- setVertex = true;
519
- }
520
- }
521
- else {
522
- ref.cds = [cd];
523
- setVertex = true;
461
+ if (!addControlDependency(ref, cd)) {
462
+ continue;
524
463
  }
525
- if (setVertex) {
526
- const vertex = graph.getVertex(ref.nodeId);
527
- if (vertex) {
528
- if (vertex.cds) {
529
- if (!vertex.cds?.find(c => c.id === cId)) {
530
- vertex.cds.push(cd);
531
- }
532
- }
533
- else {
534
- vertex.cds = [cd];
535
- }
536
- }
464
+ const vertex = graph.getVertex(ref.nodeId);
465
+ if (vertex) {
466
+ addControlDependency(vertex, cd);
537
467
  }
538
468
  }
539
469
  }
@@ -549,28 +479,7 @@ exports.ClosureRefs = {
549
479
  * links what resolves, and leaves only the references that stay open.
550
480
  */
551
481
  resolveOpenIngoing(graph, callId, definition, environment) {
552
- const remainingIn = [];
553
- for (const ingoing of definition.subflow.in) {
554
- const resolved = ingoing.name ? resolve_helper_1.Resolve.byNameAndType(ingoing.name, environment, ingoing.type) : undefined;
555
- if (resolved === undefined) {
556
- remainingIn.push(ingoing);
557
- continue;
558
- }
559
- (0, log_1.expensiveTrace)(logger_1.dataflowLogger, () => `Found ${resolved.length} references to open ref ${ingoing.nodeId} in closure of function definition ${callId}`);
560
- let allBuiltIn = true;
561
- const inId = ingoing.nodeId;
562
- for (const { nodeId, type } of resolved) {
563
- graph.addEdge(inId, nodeId, edge_1.EdgeType.Reads);
564
- graph.addEdge(callId, nodeId, edge_1.EdgeType.Reads); // because the def. is the anonymous call
565
- if (!(0, identifier_1.isReferenceType)(type, identifier_1.ReferenceType.BuiltInConstant | identifier_1.ReferenceType.BuiltInFunction)) {
566
- allBuiltIn = false;
567
- }
568
- }
569
- if (allBuiltIn) {
570
- remainingIn.push(ingoing);
571
- }
572
- }
573
- definition.subflow.in = remainingIn;
482
+ definition.subflow.in = linkInputs(definition.subflow.in, environment, [], graph, false, callId);
574
483
  }
575
484
  };
576
485
  /** The field an access names, if it is written out: `x$f`, `x[["f"]]`, or `x[[2]]`, which is how a list records its positional entries. */
@@ -605,9 +514,18 @@ function fieldAssignmentOf(readId, idMap) {
605
514
  if (r_function_call_1.RFunctionCall.isNamed(call) && identifier_1.Identifier.getName(call.functionName.content) === 'assign' && call.arguments.indexOf(access) >= 2) {
606
515
  return call.info.id;
607
516
  }
608
- const assign = r_access_1.RAccess.is(access) && access.accessed.info.id === readId ? call : undefined;
517
+ if (!r_access_1.RAccess.is(access) || access.accessed.info.id !== readId) {
518
+ return undefined;
519
+ }
520
+ /* `x$f[[k]] <- v` writes the field `f` as well */
521
+ let target = access;
522
+ let assign = call;
523
+ while (r_access_1.RAccess.is(assign) && assign.accessed.info.id === target.info.id) {
524
+ target = assign;
525
+ assign = model_1.RNode.directParent(assign, idMap);
526
+ }
609
527
  /* the replacement call `$<-` carries the id of the access it writes through */
610
- return r_binary_op_1.RBinaryOp.is(assign) && assign.lhs.info.id === access?.info.id && /^(<<?-|=)$/.test(assign.operator) ? access.info.id : undefined;
528
+ return r_binary_op_1.RBinaryOp.is(assign) && assign.lhs.info.id === target.info.id && /^(<<?-|=)$/.test(assign.operator) ? access.info.id : undefined;
611
529
  }
612
530
  /**
613
531
  * An access `x$f` written where `x` was still open reads the field of whatever `x` is bound to once that binding is
@@ -622,4 +540,98 @@ function bindAccessedField(graph, readId, envState, idMap) {
622
540
  const fieldName = accessedFieldName((0, unpack_argument_1.unpackArg)(access.access[0]), r_access_1.RAccess.isNamed(access));
623
541
  linkFieldReads(graph, access.info.id, fieldName ? envState.current.memory.get(fieldName) : undefined, true);
624
542
  }
543
+ /** The factory that creates and returns only `closure`, calling only known built-ins and not reflecting on its frame, with the names its frame binds. */
544
+ function factoryOf(closure, graph, known, idMap) {
545
+ const id = model_1.RNode.strictlyEnclosingFunction(closure.id, idMap);
546
+ const factory = id === undefined ? undefined : graph.getVertex(id);
547
+ if (!vertex_2.DfgVertex.isFunctionDefinition(factory) || factory.exitPoints.length === 0 || !factory.exitPoints.every(e => e.nodeId === closure.id)
548
+ || function_semantics_1.FunctionSemantics.frameReflection(factory, graph, { known }) !== 0) {
549
+ return undefined;
550
+ }
551
+ const locals = new Set();
552
+ for (const node of factory.subflow.graph) {
553
+ const vertex = graph.getVertex(node);
554
+ if (node === closure.id || closure.subflow.graph.has(node)) {
555
+ continue;
556
+ }
557
+ else if (vertex_2.DfgVertex.isFunctionDefinition(vertex) || (vertex_2.DfgVertex.isFunctionCall(vertex) && (!vertex.onlyBuiltin || graph.unknownSideEffects.has(node_id_1.NodeId.normalize(node))))
558
+ || vertex_2.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.SuperAssignment)) {
559
+ return undefined;
560
+ }
561
+ else if (vertex_2.DfgVertex.isVariableDefinition(vertex)) {
562
+ locals.add(node_id_1.NodeId.recoverName(node, idMap) ?? '');
563
+ }
564
+ }
565
+ for (const param of Object.keys(factory.params)) {
566
+ locals.add(node_id_1.NodeId.recoverName(node_id_1.NodeId.normalize(param), idMap) ?? '');
567
+ }
568
+ return { factory: factory.id, locals };
569
+ }
570
+ /** The factory calls whose closure `call` may run, `undefined` if it may run one created any other way. */
571
+ function instancesOf(call, closure, factory, graph) {
572
+ if (!(0, frame_reflection_1.hasEdgeOfType)(graph, call, closure, edge_1.EdgeType.Calls)) {
573
+ return undefined;
574
+ }
575
+ const instances = [];
576
+ for (const [binding, edge] of graph.edgesFrom(call)) {
577
+ if (!edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Reads) || edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Argument) || node_id_1.NodeId.isBuiltIn(binding)) {
578
+ continue;
579
+ }
580
+ /* the binding the call reads holds what one call of the factory created, and nothing else */
581
+ const isCallOfAFunction = (target) => {
582
+ const vertex = graph.getVertex(target);
583
+ return vertex_2.DfgVertex.isFunctionCall(vertex) && !vertex_2.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Assignment);
584
+ };
585
+ const created = vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(binding)) ? (0, frame_reflection_1.edgeTargets)(graph, binding, edge_1.EdgeType.DefinedBy).filter(isCallOfAFunction) : [];
586
+ const callees = created.length === 1 ? (0, frame_reflection_1.edgeTargets)(graph, created[0], edge_1.EdgeType.Calls) : [];
587
+ if (callees.length !== 1 || callees[0] !== factory) {
588
+ return undefined;
589
+ }
590
+ instances.push(created[0]);
591
+ }
592
+ return instances.length > 0 ? instances : undefined;
593
+ }
594
+ /** A closure's `<<-` into its factory frame is seen only by calls of closures from the same factory call, so such calls depend on each other instead. */
595
+ function separateClosureInstances(graph, known) {
596
+ const idMap = graph.idMap;
597
+ if (idMap === undefined) {
598
+ return;
599
+ }
600
+ for (const [closure, vertex] of graph.verticesOfType(vertex_1.VertexType.FunctionDefinition)) {
601
+ /* only a closure writing with `<<-` can share state */
602
+ const superAssignments = vertex.subflow.graph.values().filter(id => vertex_2.DfgVertex.hasOrigin(graph.getVertex(id), built_in_proc_name_1.BuiltInProcName.SuperAssignment)).toArray();
603
+ const found = superAssignments.length === 0 ? undefined : factoryOf(vertex, graph, known, idMap);
604
+ if (found === undefined) {
605
+ continue;
606
+ }
607
+ const writes = superAssignments.flatMap(id => (0, frame_reflection_1.edgeTargets)(graph, id, edge_1.EdgeType.Returns)).filter(target => {
608
+ const name = idMap.get(target);
609
+ return r_symbol_1.RSymbol.is(name) && found.locals.has(identifier_1.Identifier.getName(name.content));
610
+ });
611
+ /* someone outside the closure seeing the write would see it for every instance */
612
+ const seenOutside = (write) => (0, frame_reflection_1.edgeSources)(graph, write, ~edge_1.ControlFlowEdgeTypes).some(from => !vertex.subflow.graph.has(from));
613
+ if (writes.length === 0 || writes.some(seenOutside)) {
614
+ continue;
615
+ }
616
+ const calls = new Map();
617
+ for (const call of writes.flatMap(w => (0, frame_reflection_1.edgeTargets)(graph, w, edge_1.EdgeType.SideEffectOnCall))) {
618
+ calls.set(call, instancesOf(call, closure, found.factory, graph));
619
+ }
620
+ if (new Set([...calls.values()].flatMap(i => i ?? [])).size < 2) {
621
+ continue;
622
+ }
623
+ for (const write of writes) {
624
+ for (const call of calls.keys()) {
625
+ graph.removeEdgeType(write, call, edge_1.EdgeType.SideEffectOnCall);
626
+ }
627
+ }
628
+ for (const [call, instances] of calls) {
629
+ for (const [other, others] of calls) {
630
+ if (other !== call && (instances === undefined || others === undefined || instances.some(i => others.includes(i)))) {
631
+ graph.addEdge(call, other, edge_1.EdgeType.Reads);
632
+ }
633
+ }
634
+ }
635
+ }
636
+ }
625
637
  //# sourceMappingURL=linker.js.map