@eagleoutice/flowr 2.15.9 → 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 (244) hide show
  1. package/README.md +47 -41
  2. package/abstract-interpretation/absint-inference.d.ts +2 -2
  3. package/abstract-interpretation/absint-inference.js +32 -79
  4. package/abstract-interpretation/data-frame/arguments.js +11 -7
  5. package/benchmark/slicer.d.ts +0 -1
  6. package/benchmark/slicer.js +1 -2
  7. package/cli/repl/commands/repl-signature.js +12 -3
  8. package/config.d.ts +5 -1
  9. package/control-flow/cfg-properties.js +0 -22
  10. package/control-flow/happens-before.d.ts +7 -2
  11. package/control-flow/happens-before.js +55 -31
  12. package/control-flow/useless-loop.d.ts +1 -1
  13. package/control-flow/useless-loop.js +29 -42
  14. package/dataflow/environments/apply-kill.js +6 -2
  15. package/dataflow/environments/built-in-config.d.ts +2 -0
  16. package/dataflow/environments/built-in-props.d.ts +16 -1
  17. package/dataflow/environments/built-in-props.js +33 -20
  18. package/dataflow/environments/built-in.d.ts +8 -3
  19. package/dataflow/environments/built-in.js +52 -82
  20. package/dataflow/environments/default-builtin-config.d.ts +551 -21
  21. package/dataflow/environments/default-builtin-config.js +121 -45
  22. package/dataflow/environments/define.d.ts +3 -2
  23. package/dataflow/environments/define.js +4 -3
  24. package/dataflow/environments/deprecation-info.d.ts +45 -0
  25. package/dataflow/environments/deprecation-info.js +13 -0
  26. package/dataflow/environments/environment.d.ts +7 -5
  27. package/dataflow/environments/environment.js +55 -121
  28. package/dataflow/environments/identifier.d.ts +6 -2
  29. package/dataflow/environments/identifier.js +23 -69
  30. package/dataflow/environments/reference-to-maybe.d.ts +9 -0
  31. package/dataflow/environments/reference-to-maybe.js +23 -51
  32. package/dataflow/environments/resolve-by-name.js +37 -76
  33. package/dataflow/eval/resolve/resolve-argument.d.ts +3 -1
  34. package/dataflow/eval/resolve/resolve-argument.js +61 -64
  35. package/dataflow/eval/resolve/resolve-working-directory.d.ts +8 -2
  36. package/dataflow/eval/resolve/resolve-working-directory.js +12 -20
  37. package/dataflow/eval/resolve/resolve.d.ts +4 -0
  38. package/dataflow/eval/resolve/resolve.js +23 -14
  39. package/dataflow/extractor.js +180 -58
  40. package/dataflow/fn/class-declaration.d.ts +8 -13
  41. package/dataflow/fn/class-declaration.js +95 -116
  42. package/dataflow/fn/exceptions-of-function.js +68 -37
  43. package/dataflow/fn/frame-reflection.d.ts +12 -4
  44. package/dataflow/fn/frame-reflection.js +51 -41
  45. package/dataflow/fn/function-props.js +15 -33
  46. package/dataflow/fn/function-semantics.d.ts +11 -3
  47. package/dataflow/fn/function-semantics.js +6 -0
  48. package/dataflow/fn/higher-order-function.d.ts +9 -1
  49. package/dataflow/fn/higher-order-function.js +27 -24
  50. package/dataflow/fn/strict-function.d.ts +13 -0
  51. package/dataflow/fn/strict-function.js +291 -123
  52. package/dataflow/graph/call-graph.d.ts +7 -6
  53. package/dataflow/graph/call-graph.js +35 -46
  54. package/dataflow/graph/graph.d.ts +13 -1
  55. package/dataflow/graph/graph.js +74 -66
  56. package/dataflow/graph/vertex.d.ts +1 -1
  57. package/dataflow/info.d.ts +8 -1
  58. package/dataflow/info.js +24 -30
  59. package/dataflow/internal/control-flow.d.ts +31 -4
  60. package/dataflow/internal/control-flow.js +61 -39
  61. package/dataflow/internal/linker.d.ts +17 -8
  62. package/dataflow/internal/linker.js +286 -213
  63. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +179 -0
  64. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +874 -0
  65. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +3 -1
  66. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +60 -48
  67. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +4 -2
  68. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +135 -198
  69. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.js +7 -9
  70. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.d.ts +5 -1
  71. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.js +21 -14
  72. package/dataflow/internal/process/functions/call/built-in/built-in-eval.js +14 -4
  73. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +1 -1
  74. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +141 -146
  75. package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +56 -24
  76. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +292 -253
  77. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +1 -1
  78. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +30 -32
  79. package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +68 -67
  80. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +91 -129
  81. package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +1 -3
  82. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +16 -12
  83. package/dataflow/internal/process/functions/call/built-in/built-in-quote.d.ts +4 -4
  84. package/dataflow/internal/process/functions/call/built-in/built-in-quote.js +9 -35
  85. package/dataflow/internal/process/functions/call/built-in/built-in-repeat-loop.js +2 -0
  86. package/dataflow/internal/process/functions/call/built-in/built-in-rm.js +8 -3
  87. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.d.ts +5 -3
  88. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.js +13 -31
  89. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.d.ts +1 -1
  90. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.js +42 -119
  91. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.d.ts +1 -3
  92. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.js +21 -21
  93. package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +2 -0
  94. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +93 -102
  95. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.d.ts +7 -3
  96. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +173 -114
  97. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.d.ts +1 -3
  98. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.js +23 -26
  99. package/dataflow/internal/process/functions/call/built-in/built-in-with.d.ts +1 -1
  100. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +34 -9
  101. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +1 -1
  102. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +119 -93
  103. package/dataflow/internal/process/functions/call/common.d.ts +4 -4
  104. package/dataflow/internal/process/functions/call/common.js +49 -71
  105. package/dataflow/internal/process/functions/call/deferred.d.ts +12 -2
  106. package/dataflow/internal/process/functions/call/deferred.js +81 -63
  107. package/dataflow/internal/process/functions/call/known-call-handling.d.ts +4 -2
  108. package/dataflow/internal/process/functions/call/known-call-handling.js +65 -68
  109. package/dataflow/internal/process/functions/call/named-call-handling.d.ts +3 -5
  110. package/dataflow/internal/process/functions/call/named-call-handling.js +48 -19
  111. package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
  112. package/dataflow/internal/process/functions/call/quoted.js +257 -154
  113. package/dataflow/internal/process/functions/call/unnamed-call-handling.js +17 -38
  114. package/dataflow/internal/process/functions/process-parameter.js +25 -32
  115. package/dataflow/internal/process/process-symbol.js +4 -3
  116. package/dataflow/internal/program-order.d.ts +26 -0
  117. package/dataflow/internal/program-order.js +49 -0
  118. package/documentation/data/faq/faqs.js +3 -3
  119. package/documentation/data/interface/doc-writing-code.js +1 -2
  120. package/documentation/data/server/doc-data-server-messages.js +7 -7
  121. package/documentation/doc-capabilities.js +80 -34
  122. package/documentation/doc-util/doc-query.d.ts +0 -3
  123. package/documentation/doc-util/doc-query.js +4 -3
  124. package/documentation/wiki-analyzer.js +1 -1
  125. package/documentation/wiki-cfg.js +5 -6
  126. package/documentation/wiki-core.js +8 -8
  127. package/documentation/wiki-dataflow-graph.js +10 -11
  128. package/documentation/wiki-interface.js +2 -2
  129. package/documentation/wiki-linting-and-testing.js +3 -3
  130. package/documentation/wiki-overview.js +6 -6
  131. package/documentation/wiki-query.js +9 -101
  132. package/documentation/wiki-setup.js +23 -0
  133. package/documentation/wiki-signature-database.js +1 -1
  134. package/linter/linter-rules.d.ts +18 -9
  135. package/linter/rules/absolute-path.js +63 -87
  136. package/linter/rules/deprecated-functions.d.ts +1 -45
  137. package/linter/rules/deprecated-functions.js +24 -33
  138. package/linter/rules/file-path-validity.d.ts +4 -2
  139. package/linter/rules/file-path-validity.js +114 -80
  140. package/linter/rules/namespace-access.js +21 -33
  141. package/linter/rules/naming-convention.d.ts +1 -1
  142. package/linter/rules/naming-convention.js +60 -48
  143. package/linter/rules/network-functions.js +11 -7
  144. package/linter/rules/no-leaked-credentials.js +15 -18
  145. package/linter/rules/problematic-inputs.d.ts +2 -1
  146. package/linter/rules/problematic-inputs.js +39 -59
  147. package/linter/rules/roxygen-arguments.d.ts +9 -4
  148. package/linter/rules/roxygen-arguments.js +29 -48
  149. package/linter/rules/seeded-randomness.d.ts +7 -4
  150. package/linter/rules/seeded-randomness.js +128 -89
  151. package/linter/rules/stop-with-call-arg.d.ts +4 -2
  152. package/linter/rules/stop-with-call-arg.js +25 -22
  153. package/linter/rules/unclosed-connection.js +31 -24
  154. package/linter/rules/undefined-symbol.d.ts +4 -6
  155. package/linter/rules/undefined-symbol.js +58 -75
  156. package/linter/rules/unescaped-arguments.js +18 -44
  157. package/linter/rules/unused-definition.d.ts +6 -0
  158. package/linter/rules/unused-definition.js +105 -145
  159. package/linter/rules/unused-import.d.ts +3 -0
  160. package/linter/rules/unused-import.js +25 -43
  161. package/package.json +5 -2
  162. package/project/flowr-analyzer.d.ts +2 -0
  163. package/project/flowr-analyzer.js +28 -1
  164. package/project/plugins/file-plugins/files/flowr-jupyter-file.d.ts +2 -2
  165. package/project/plugins/file-plugins/files/flowr-jupyter-file.js +8 -12
  166. package/project/plugins/file-plugins/files/flowr-rda-file.d.ts +0 -2
  167. package/project/plugins/file-plugins/files/flowr-rda-file.js +10 -55
  168. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.d.ts +7 -5
  169. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.js +34 -56
  170. package/project/plugins/file-plugins/flowr-analyzer-file-plugin.js +2 -1
  171. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +22 -45
  172. package/project/plugins/project-discovery/flowr-analyzer-project-discovery-plugin.js +2 -2
  173. package/project/sigdb/build.js +4 -0
  174. package/project/sigdb/manifest.d.ts +2 -0
  175. package/project/sigdb/manifest.js +10 -1
  176. package/project/sigdb/reader.js +23 -61
  177. package/project/sigdb/sigdb-download.d.ts +29 -6
  178. package/project/sigdb/sigdb-download.js +212 -64
  179. package/queries/base-query-format.js +1 -2
  180. package/queries/catalog/absint-query/absint-query-format.js +1 -1
  181. package/queries/catalog/call-context-query/cascade-action.d.ts +3 -1
  182. package/queries/catalog/call-context-query/cascade-action.js +2 -0
  183. package/queries/catalog/call-context-query/identify-link-to-last-call-relation.js +3 -0
  184. package/queries/catalog/dataflow-query/dataflow-query-executor.js +2 -2
  185. package/queries/catalog/dataflow-query/dataflow-query-format.d.ts +2 -0
  186. package/queries/catalog/dataflow-query/dataflow-query-format.js +4 -7
  187. package/queries/catalog/dependencies-query/dependencies-query-executor.js +50 -114
  188. package/queries/catalog/dependencies-query/function-info/library-functions.d.ts +5 -9
  189. package/queries/catalog/dependencies-query/function-info/library-functions.js +6 -0
  190. package/queries/catalog/dependencies-query/function-info/read-functions.js +1 -1
  191. package/queries/catalog/dependencies-query/function-info/write-functions.js +7 -3
  192. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +4 -4
  193. package/queries/catalog/input-sources-query/simple-input-classifier.js +192 -301
  194. package/queries/catalog/inspect-higher-order-query/inspect-higher-order-query-executor.js +2 -1
  195. package/queries/catalog/origin-query/origin-query-executor.d.ts +1 -1
  196. package/queries/catalog/origin-query/origin-query-executor.js +2 -2
  197. package/queries/catalog/origin-query/origin-query-format.d.ts +2 -2
  198. package/queries/catalog/origin-query/origin-query-format.js +1 -1
  199. package/queries/query.d.ts +1 -1
  200. package/r-bridge/data/data.d.ts +21 -14
  201. package/r-bridge/data/data.js +28 -20
  202. package/r-bridge/lang-4.x/ast/model/model.d.ts +3 -1
  203. package/r-bridge/lang-4.x/ast/model/model.js +19 -43
  204. package/r-bridge/lang-4.x/ast/model/nodes/r-access.d.ts +1 -0
  205. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.d.ts +3 -0
  206. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.js +5 -12
  207. package/r-bridge/lang-4.x/ast/model/nodes/r-binary-op.d.ts +1 -0
  208. package/r-bridge/lang-4.x/ast/model/nodes/r-break.d.ts +1 -0
  209. package/r-bridge/lang-4.x/ast/model/nodes/r-comment.d.ts +1 -0
  210. package/r-bridge/lang-4.x/ast/model/nodes/r-expression-list.d.ts +1 -0
  211. package/r-bridge/lang-4.x/ast/model/nodes/r-for-loop.d.ts +1 -0
  212. package/r-bridge/lang-4.x/ast/model/nodes/r-function-call.d.ts +1 -0
  213. package/r-bridge/lang-4.x/ast/model/nodes/r-function-definition.d.ts +1 -0
  214. package/r-bridge/lang-4.x/ast/model/nodes/r-if-then-else.d.ts +1 -0
  215. package/r-bridge/lang-4.x/ast/model/nodes/r-line-directive.d.ts +1 -0
  216. package/r-bridge/lang-4.x/ast/model/nodes/r-logical.d.ts +1 -0
  217. package/r-bridge/lang-4.x/ast/model/nodes/r-next.d.ts +1 -0
  218. package/r-bridge/lang-4.x/ast/model/nodes/r-number.d.ts +1 -0
  219. package/r-bridge/lang-4.x/ast/model/nodes/r-parameter.d.ts +1 -0
  220. package/r-bridge/lang-4.x/ast/model/nodes/r-pipe.d.ts +1 -0
  221. package/r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop.d.ts +1 -0
  222. package/r-bridge/lang-4.x/ast/model/nodes/r-string.d.ts +1 -0
  223. package/r-bridge/lang-4.x/ast/model/nodes/r-symbol.d.ts +1 -0
  224. package/r-bridge/lang-4.x/ast/model/nodes/r-unary-op.d.ts +1 -0
  225. package/r-bridge/lang-4.x/ast/model/nodes/r-while-loop.d.ts +1 -0
  226. package/r-bridge/lang-4.x/tree-sitter/tree-sitter-executor.d.ts +2 -2
  227. package/r-bridge/lang-4.x/tree-sitter/tree-sitter-executor.js +11 -2
  228. package/r-bridge/roxygen2/roxygen-parse.js +28 -42
  229. package/r-bridge/shell.js +6 -0
  230. package/search/search-executor/search-enrichers.js +25 -37
  231. package/slicing/static/slice-call.d.ts +4 -3
  232. package/slicing/static/slice-call.js +56 -88
  233. package/slicing/static/static-slicer.js +115 -79
  234. package/slicing/static/visiting-queue.d.ts +4 -6
  235. package/slicing/static/visiting-queue.js +9 -13
  236. package/util/files.d.ts +2 -0
  237. package/util/files.js +3 -1
  238. package/util/mermaid/mermaid.js +6 -2
  239. package/util/proc.js +2 -1
  240. package/util/r-base-packages.d.ts +2 -0
  241. package/util/r-base-packages.js +23 -13
  242. package/util/text/playground-link.d.ts +12 -3
  243. package/util/text/playground-link.js +18 -27
  244. package/util/version.js +4 -10
@@ -9,12 +9,14 @@ exports.linkFunctionCalls = linkFunctionCalls;
9
9
  exports.getAllFunctionCallTargets = getAllFunctionCallTargets;
10
10
  exports.getAllLinkedFunctionDefinitions = getAllLinkedFunctionDefinitions;
11
11
  exports.linkExpressionIn = linkExpressionIn;
12
+ exports.linkCallToDefinitions = linkCallToDefinitions;
12
13
  exports.linkInputs = linkInputs;
13
14
  exports.linkCircularRedefinitionsWithinALoop = linkCircularRedefinitionsWithinALoop;
14
15
  exports.reapplyLoopExitPoints = reapplyLoopExitPoints;
15
16
  exports.accessedFieldName = accessedFieldName;
16
17
  exports.linkFieldReads = linkFieldReads;
17
18
  exports.bindAccessedField = bindAccessedField;
19
+ exports.separateClosureInstances = separateClosureInstances;
18
20
  const defaultmap_1 = require("../../util/collections/defaultmap");
19
21
  const r_function_call_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
20
22
  const function_semantics_1 = require("../fn/function-semantics");
@@ -23,7 +25,6 @@ const assert_1 = require("../../util/assert");
23
25
  const log_1 = require("../../util/log");
24
26
  const node_id_1 = require("../../r-bridge/lang-4.x/ast/model/processing/node-id");
25
27
  const identifier_1 = require("../environments/identifier");
26
- const graph_1 = require("../graph/graph");
27
28
  const logger_1 = require("../logger");
28
29
  const edge_1 = require("../graph/edge");
29
30
  const vertex_1 = require("../graph/vertex");
@@ -37,7 +38,11 @@ const built_in_proc_name_1 = require("../environments/built-in-proc-name");
37
38
  const vertex_2 = require("../graph/vertex");
38
39
  const resolve_helper_1 = require("../environments/resolve-helper");
39
40
  const r_function_definition_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
41
+ const r_binary_op_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-binary-op");
40
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;
41
46
  /**
42
47
  * Find all reads within the graph that do not reference a local definition in the graph.
43
48
  */
@@ -48,7 +53,6 @@ function findNonLocalReads(graph, ignores = new Set()) {
48
53
  defs.add(id);
49
54
  }
50
55
  }
51
- /* find all variable use ids which do not link to a given id */
52
56
  const nonLocalReads = [];
53
57
  /* the tag already says what the reference is, so no vertex has to be fetched to decide */
54
58
  for (const [tag, type] of [[vertex_1.VertexType.Use, identifier_1.ReferenceType.Variable], [vertex_1.VertexType.FunctionCall, identifier_1.ReferenceType.Function]]) {
@@ -56,13 +60,10 @@ function findNonLocalReads(graph, ignores = new Set()) {
56
60
  if (ignores.has(nodeId)) {
57
61
  continue;
58
62
  }
59
- const outgoing = graph.outgoingEdges(nodeId);
60
- const name = node_id_1.NodeId.recoverName(nodeId, graph.idMap);
61
- const identifierRef = { nodeId, name, type };
62
63
  /* control flow edges say nothing about what a name resolves to, so they do not count as a link */
63
64
  let linked = false;
64
65
  let nonLocal = false;
65
- for (const [target, e] of outgoing ?? graph_1.NoEdges) {
66
+ for (const [target, e] of graph.edgesFrom(nodeId)) {
66
67
  if (edge_1.DfEdge.isOnlyControlFlow(e)) {
67
68
  continue;
68
69
  }
@@ -73,7 +74,7 @@ function findNonLocalReads(graph, ignores = new Set()) {
73
74
  }
74
75
  }
75
76
  if (!linked || nonLocal) {
76
- nonLocalReads.push(identifierRef);
77
+ nonLocalReads.push({ nodeId, name: node_id_1.NodeId.recoverName(nodeId, graph.idMap), type });
77
78
  }
78
79
  }
79
80
  }
@@ -85,9 +86,8 @@ function findNonLocalReads(graph, ignores = new Set()) {
85
86
  function produceNameSharedIdMap(references) {
86
87
  const nameIdShares = new defaultmap_1.DefaultMap(() => []);
87
88
  for (const reference of references) {
88
- const rn = reference.name;
89
- if (rn) {
90
- nameIdShares.get(rn).push(reference);
89
+ if (reference.name) {
90
+ nameIdShares.get(reference.name).push(reference);
91
91
  }
92
92
  }
93
93
  return nameIdShares;
@@ -100,27 +100,11 @@ function produceNameSharedIdMap(references) {
100
100
  function linkArgumentsOnCall(args, params, graph) {
101
101
  return function_semantics_1.FunctionSemantics.call.match.onCallAndLink(args, params, graph);
102
102
  }
103
- /**
104
- * Links the function call arguments to the target function definition and returns a map from argument ids to parameter ids.
105
- */
106
- function linkFunctionCallArguments(targetId, idMap, functionCallName, functionRootId, callArgs, finalGraph) {
107
- const linkedFunction = idMap.get(targetId);
108
- if (linkedFunction === undefined) {
109
- logger_1.dataflowLogger.trace(`no fdef found for ${functionCallName} (${functionRootId})`);
110
- return;
111
- }
112
- if (!r_function_definition_1.RFunctionDefinition.is(linkedFunction)) {
113
- logger_1.dataflowLogger.trace(`function call definition base ${functionCallName} does not lead to a function definition (${functionRootId}) but got ${linkedFunction.type}`);
114
- return;
115
- }
116
- return linkArgumentsOnCall(callArgs, linkedFunction.parameters, finalGraph);
117
- }
118
- const NoCalleeEnvironments = [];
119
- function environmentsOfCallee(info) {
120
- if (info.name === undefined || info.environment === undefined) {
121
- return NoCalleeEnvironments;
103
+ function environmentsOfCallee(info, environment) {
104
+ if (info.name === undefined) {
105
+ return [];
122
106
  }
123
- return (resolve_helper_1.Resolve.byName(info.name, info.environment) ?? []).filter(identifier_1.hasEnvState).map(d => d.envState);
107
+ return (resolve_helper_1.Resolve.byName(info.name, environment) ?? []).filter(identifier_1.hasEnvState).map(d => d.envState);
124
108
  }
125
109
  /**
126
110
  * Links a function call with a single target function definition.
@@ -129,106 +113,122 @@ function linkFunctionCallWithSingleTarget(graph, { subflow: fnSubflow, exitPoint
129
113
  const id = info.id;
130
114
  const environment = info.environment;
131
115
  if (environment !== undefined) {
132
- let callee = undefined;
133
- // 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 */
134
118
  for (const ingoing of fnSubflow.in) {
135
119
  const name = ingoing.name;
136
120
  if (name === undefined) {
137
121
  continue;
138
122
  }
139
- callee ??= environmentsOfCallee(info);
140
- let defs = undefined;
141
- for (const env of callee) {
142
- defs = resolve_helper_1.Resolve.byNameAndType(name, env, ingoing.type);
143
- if (defs !== undefined) {
144
- 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;
145
129
  }
146
- }
147
- defs ??= resolve_helper_1.Resolve.byNameAndType(name, environment, ingoing.type);
148
- if (defs === undefined) {
149
- continue;
150
- }
151
- for (const d of defs) {
152
- const { nodeId, type, value, definedAt, envState } = d;
153
- if (!fnSubflow.graph.has(nodeId) && !node_id_1.NodeId.isBuiltIn(nodeId)) {
154
- graph.addEdge(ingoing.nodeId, nodeId, edge_1.EdgeType.DefinedByOnCall);
155
- graph.addEdge(id, nodeId, edge_1.EdgeType.DefinesOnCall);
156
- if (envState !== undefined) {
157
- bindAccessedField(graph, ingoing.nodeId, envState, idMap);
158
- }
159
- if (definedAt !== undefined && definedAt !== nodeId && !node_id_1.NodeId.isBuiltIn(definedAt)
160
- && graph.hasVertex(definedAt) && !vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(nodeId))) {
161
- graph.addEdge(id, definedAt, edge_1.EdgeType.Reads);
162
- }
163
- if (type === identifier_1.ReferenceType.Function && ingoing.type === identifier_1.ReferenceType.S7MethodPrefix && Array.isArray(value)) {
164
- for (const v of value) {
165
- graph.addEdge(id, v, edge_1.EdgeType.Calls);
166
- graph.addEdge(ingoing.nodeId, v, edge_1.EdgeType.Calls);
167
- const vInfo = graph.getVertex(v);
168
- if (vInfo && vertex_2.DfgVertex.isFunctionDefinition(vInfo)) {
169
- vInfo.mode ??= [];
170
- if (!vInfo.mode.includes('s7')) {
171
- vInfo.mode.push('s7');
172
- }
173
- }
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');
174
146
  }
175
147
  }
176
148
  }
177
149
  }
178
150
  }
179
151
  }
180
- 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
+ }
181
166
  for (const exitPoint of exitPoints) {
182
167
  graph.addEdge(id, exitPoint.nodeId, edge_1.EdgeType.Returns);
183
- if ((0, info_1.doesExitPointPropagateCalls)(exitPoint.type)) {
184
- propagateExitPoints.push(exitPoint);
185
- }
186
168
  }
187
- const defName = node_id_1.NodeId.recoverName(fnId, idMap);
188
- (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)}`);
189
170
  graph.addEdge(id, fnId, edge_1.EdgeType.Calls);
190
- applyForForcedArgs(graph, info.id, params, linkFunctionCallArguments(fnId, idMap, defName, id, info.args, graph));
191
- 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));
192
176
  }
193
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 */
194
- function applyForForcedArgs(graph, callId, readParams, maps) {
195
- if (maps === undefined) {
196
- return;
197
- }
198
- for (const [arg, param] of maps.entries()) {
178
+ function applyForForcedArgs(graph, callId, readParams, argToParam) {
179
+ for (const [arg, param] of argToParam) {
199
180
  if (readParams[String(param)]) {
200
181
  graph.addEdge(callId, arg, edge_1.EdgeType.Reads);
182
+ /* a call in the argument only runs when the call forces it, so its side effects depend on the call */
183
+ if (vertex_2.DfgVertex.isFunctionCall(graph.getVertex(arg))) {
184
+ graph.addEdge(arg, callId, edge_1.EdgeType.Reads);
185
+ }
201
186
  }
202
187
  }
203
188
  }
204
- const FCallLinkReadBits = edge_1.EdgeType.Reads | edge_1.EdgeType.Calls | edge_1.EdgeType.DefinedByOnCall;
205
- /* there is _a lot_ potential for optimization here */
206
- function linkFunctionCall(graph, id, info, idMap, thisGraph, calledFunctionDefinitions) {
207
- const edges = graph.outgoingEdges(id);
208
- if (edges === undefined) {
209
- 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
+ }
210
209
  }
211
- const functionDefinitionReadIds = new Set();
212
- 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)) {
213
214
  if (!node_id_1.NodeId.isBuiltIn(t) && edge_1.DfEdge.doesNotIncludeType(e, edge_1.EdgeType.Argument) && edge_1.DfEdge.includesType(e, FCallLinkReadBits)) {
214
- functionDefinitionReadIds.add(t);
215
+ readIds.add(t);
215
216
  }
216
217
  }
217
- const [functionDefs] = getAllLinkedFunctionDefinitions(functionDefinitionReadIds, graph);
218
+ const [functionDefs, builtIns] = getAllLinkedFunctionDefinitions(readIds, graph);
218
219
  const propagateExitPoints = [];
219
- for (const def of functionDefs.values()) {
220
- // we can skip this if we already linked it
221
- const oEdge = graph.outgoingEdges(id)?.get(def.id);
222
- if (oEdge && edge_1.DfEdge.includesType(oEdge, edge_1.EdgeType.Calls)) {
223
- continue;
224
- }
225
- for (const ep of linkFunctionCallWithSingleTarget(graph, def, info, idMap)) {
226
- 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));
227
224
  }
228
225
  }
229
- if (thisGraph.isRoot(id) && functionDefs.size > 0) {
230
- calledFunctionDefinitions.push({ functionCall: id, called: functionDefs.values().toArray(), propagateExitPoints });
226
+ if (!isRoot || (functionDefs.size === 0 && builtIns.size === 0)) {
227
+ return undefined;
231
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 };
232
232
  }
233
233
  /**
234
234
  * Returns the called functions within `graph` (ideally a superset of `thisGraph`, the graph searched for calls), which
@@ -237,62 +237,47 @@ function linkFunctionCall(graph, id, info, idMap, thisGraph, calledFunctionDefin
237
237
  function linkFunctionCalls(graph, idMap, thisGraph,
238
238
  /** calls `graph` knows of that `thisGraph` does not, as a read that became a call */
239
239
  alsoCalls = []) {
240
- const calledFunctionDefinitions = [];
240
+ const called = [];
241
241
  for (const [id, info] of thisGraph.verticesOfType(vertex_1.VertexType.FunctionCall)) {
242
242
  if (!info.onlyBuiltin) {
243
- linkFunctionCall(graph, id, info, idMap, thisGraph, calledFunctionDefinitions);
243
+ called.push(linkFunctionCall(graph, id, info, idMap, thisGraph.isRoot(id)));
244
244
  }
245
245
  }
246
246
  for (const id of alsoCalls) {
247
247
  const info = graph.getVertex(id);
248
248
  if (vertex_2.DfgVertex.isFunctionCall(info)) {
249
- linkFunctionCall(graph, id, info, idMap, graph, calledFunctionDefinitions);
249
+ called.push(linkFunctionCall(graph, id, info, idMap, graph.isRoot(id)));
250
250
  }
251
251
  }
252
- return calledFunctionDefinitions;
252
+ return called.filter(assert_1.isNotUndefined);
253
253
  }
254
254
  /**
255
255
  * convenience function returning all known call targets, as well as the name source which defines them
256
256
  */
257
257
  function getAllFunctionCallTargets(call, graph, environment) {
258
- const found = new Set();
259
- const callVertex = graph.get(call, true);
260
- if (callVertex === undefined) {
261
- return [];
262
- }
263
- const [info, outgoingEdges] = callVertex;
258
+ const info = graph.getVertex(call);
264
259
  if (!vertex_2.DfgVertex.isFunctionCall(info)) {
265
260
  return [];
266
261
  }
267
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;
268
265
  let functionCallDefs = [];
269
- if (known !== undefined) {
270
- const refType = info.origin.includes(built_in_proc_name_1.BuiltInProcName.S3Dispatch) ? identifier_1.ReferenceType.S3MethodPrefix :
271
- info.origin.includes(built_in_proc_name_1.BuiltInProcName.S7Dispatch) ? identifier_1.ReferenceType.S7MethodPrefix : identifier_1.ReferenceType.Function;
272
- if (info.name !== undefined && !identifier_1.Identifier.getName(info.name).startsWith(unnamed_call_handling_1.UnnamedFunctionCallPrefix)) {
273
- functionCallDefs = resolve_helper_1.Resolve.byNameAndType(info.name, known, refType)?.map(d => d.nodeId) ?? [];
274
- }
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) ?? [];
275
268
  }
276
- const followBits = info.origin.includes(built_in_proc_name_1.BuiltInProcName.Unnamed) ? FCallLinkReadBits : edge_1.EdgeType.Calls;
277
- for (const [target, outgoingEdge] of outgoingEdges.entries()) {
278
- 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))) {
279
272
  functionCallDefs.push(target);
280
273
  }
281
274
  }
282
- if (functionCallDefs.length > 0) {
283
- const [functionCallTargets, builtInTargets] = getAllLinkedFunctionDefinitions(new Set(functionCallDefs), graph);
284
- for (const target of functionCallTargets) {
285
- found.add(target.id);
286
- }
287
- for (const arr of [builtInTargets, functionCallDefs]) {
288
- for (const target of arr) {
289
- found.add(target);
290
- }
291
- }
275
+ if (functionCallDefs.length === 0) {
276
+ return [];
292
277
  }
293
- 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])];
294
280
  }
295
- const LinkedFnFollowBits = edge_1.EdgeType.Reads | edge_1.EdgeType.DefinedBy | edge_1.EdgeType.DefinedByOnCall;
296
281
  /**
297
282
  * Finds all linked function definitions starting from the given read ids; expects the caller to already have resolved
298
283
  * the accessed objects (first layer of reads/returns/calls/...). For call targets, use {@link getAllFunctionCallTargets} instead.
@@ -300,9 +285,6 @@ const LinkedFnFollowBits = edge_1.EdgeType.Reads | edge_1.EdgeType.DefinedBy | e
300
285
  function getAllLinkedFunctionDefinitions(functionDefinitionReadIds, dataflowGraph) {
301
286
  const result = new Set();
302
287
  const builtIns = new Set();
303
- if (functionDefinitionReadIds.size === 0) {
304
- return [result, builtIns];
305
- }
306
288
  const potential = Array.from(functionDefinitionReadIds, id => [id, false]);
307
289
  const visited = new Set();
308
290
  const expanded = new Set();
@@ -323,26 +305,24 @@ function getAllLinkedFunctionDefinitions(functionDefinitionReadIds, dataflowGrap
323
305
  if (vertex === undefined) {
324
306
  continue;
325
307
  }
326
- if (vertex.subflow !== undefined) {
308
+ else if (vertex.subflow !== undefined) {
327
309
  result.add(vertex);
328
310
  continue;
329
311
  }
330
- const outgoing = dataflowGraph.outgoingEdges(cid);
331
- if (!outgoing) {
332
- continue;
333
- }
334
312
  const nextViaAccess = viaAccess || vertex_2.DfgVertex.hasOrigin(vertex, built_in_proc_name_1.BuiltInProcName.Access);
335
313
  const isSkipType = vertex_2.DfgVertex.isFunctionCall(vertex) || (vertex_2.DfgVertex.isVariableDefinition(vertex) && vertex.par && !nextViaAccess);
336
314
  let hasReturnEdge = false;
337
315
  let followTargets;
338
- for (const [target, e] of outgoing) {
339
- 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)) {
340
320
  hasReturnEdge = true;
341
321
  if (!visited.has(target)) {
342
322
  potential.push([target, nextViaAccess]);
343
323
  }
344
324
  }
345
- 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)) {
346
326
  (followTargets ??= []).push(target);
347
327
  }
348
328
  }
@@ -369,22 +349,38 @@ function linkExpressionIn(graph, expr, environment, idMap) {
369
349
  model_1.RNode.visitAst(node, inner => {
370
350
  if (r_function_call_1.RFunctionCall.isNamed(inner)) {
371
351
  callees.add(inner.functionName.info.id);
372
- references.push({ nodeId: inner.functionName.info.id, name: inner.functionName.content, cds: undefined, type: identifier_1.ReferenceType.Function });
352
+ /* the call vertex carries the call's id, its name symbol has no vertex of its own */
353
+ references.push({ nodeId: inner.info.id, name: inner.functionName.content, cds: undefined, type: identifier_1.ReferenceType.Function });
373
354
  }
374
355
  else if (r_symbol_1.RSymbol.is(inner) && !callees.has(inner.info.id)) {
375
356
  references.push({ nodeId: inner.info.id, name: inner.content, cds: undefined, type: identifier_1.ReferenceType.Variable });
376
357
  }
377
358
  return false;
378
359
  });
379
- const unresolved = [];
380
- linkInputs(references, environment, unresolved, graph, false);
360
+ const unresolved = linkInputs(references, environment, [], graph, false);
361
+ for (const call of references.filter(r => r.type === identifier_1.ReferenceType.Function)) {
362
+ linkCallToDefinitions(graph, call.nodeId, resolve_helper_1.Resolve.byNameAndType(call.name, environment, identifier_1.ReferenceType.Function)?.map(d => d.nodeId) ?? [], idMap);
363
+ }
381
364
  return unresolved;
382
365
  }
366
+ /** Links the call `callId` to the functions the given definitions bind, which it did not know of when it was processed. */
367
+ function linkCallToDefinitions(graph, callId, definitions, idMap) {
368
+ const call = graph.getVertex(callId);
369
+ if (definitions.length === 0 || !vertex_2.DfgVertex.isFunctionCall(call)) {
370
+ return;
371
+ }
372
+ for (const fn of getAllLinkedFunctionDefinitions(new Set(definitions), graph)[0]) {
373
+ if (!(0, frame_reflection_1.hasEdgeOfType)(graph, callId, fn.id, edge_1.EdgeType.Calls)) {
374
+ linkFunctionCallWithSingleTarget(graph, fn, call, idMap);
375
+ }
376
+ }
377
+ }
383
378
  /**
384
- * Links a set of read variables to definitions in `environmentInformation`; each reference that cannot be linked is
385
- * 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
386
382
  */
387
- function linkInputs(referencesToLinkAgainstEnvironment, environmentInformation, givenInputs, graph, maybeForRemaining) {
383
+ function linkInputs(referencesToLinkAgainstEnvironment, environmentInformation, givenInputs, graph, maybeForRemaining, alsoFrom) {
388
384
  for (const bodyInput of referencesToLinkAgainstEnvironment) {
389
385
  const probableTarget = bodyInput.name ? resolve_helper_1.Resolve.byNameAndType(bodyInput.name, environmentInformation, bodyInput.type) : undefined;
390
386
  if (probableTarget === undefined) {
@@ -392,19 +388,17 @@ function linkInputs(referencesToLinkAgainstEnvironment, environmentInformation,
392
388
  bodyInput.cds ??= [];
393
389
  }
394
390
  givenInputs.push(bodyInput);
391
+ continue;
395
392
  }
396
- else {
397
- let allBuiltIn = true;
398
- for (const target of probableTarget) {
399
- graph.addEdge(bodyInput.nodeId, target.nodeId, edge_1.EdgeType.Reads);
400
- if (!(0, identifier_1.isReferenceType)(target.type, identifier_1.ReferenceType.BuiltInConstant | identifier_1.ReferenceType.BuiltInFunction)) {
401
- allBuiltIn = false;
402
- }
403
- }
404
- if (allBuiltIn) {
405
- 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);
406
397
  }
407
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
+ }
408
402
  }
409
403
  return givenInputs;
410
404
  }
@@ -416,11 +410,7 @@ function linkCircularRedefinitionsWithinALoop(graph, openIns, outgoing, environm
416
410
  if (environment !== undefined) {
417
411
  const outgoingIds = new Set(outgoing.map(o => o.nodeId));
418
412
  for (const [name, targets] of openIns.entries()) {
419
- const liveDefs = environment.current.memory.get(identifier_1.Identifier.getName(name));
420
- if (liveDefs === undefined) {
421
- continue;
422
- }
423
- for (const def of liveDefs) {
413
+ for (const def of environment.current.memory.get(identifier_1.Identifier.getName(name)) ?? []) {
424
414
  if (outgoingIds.has(def.nodeId)) {
425
415
  for (const target of targets) {
426
416
  graph.addEdge(target.nodeId, def.nodeId, edge_1.EdgeType.Reads);
@@ -430,17 +420,16 @@ function linkCircularRedefinitionsWithinALoop(graph, openIns, outgoing, environm
430
420
  }
431
421
  return;
432
422
  }
433
- // 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) */
434
424
  const lastOutgoing = new Map();
435
425
  for (const out of outgoing) {
436
- const on = out.name;
437
- if (on) {
438
- lastOutgoing.set(on, out);
426
+ if (out.name) {
427
+ lastOutgoing.set(out.name, out);
439
428
  }
440
429
  }
441
430
  for (const [name, targets] of openIns.entries()) {
442
- for (const { name: outName, nodeId } of lastOutgoing.values()) {
443
- if (outName !== undefined && identifier_1.Identifier.matches(outName, name)) {
431
+ for (const [outName, { nodeId }] of lastOutgoing) {
432
+ if (identifier_1.Identifier.matches(outName, name)) {
444
433
  for (const target of targets) {
445
434
  graph.addEdge(target.nodeId, nodeId, edge_1.EdgeType.Reads);
446
435
  }
@@ -448,6 +437,13 @@ function linkCircularRedefinitionsWithinALoop(graph, openIns, outgoing, environm
448
437
  }
449
438
  }
450
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
+ }
451
447
  /**
452
448
  * Reapplies the loop exit points' control dependencies to the given identifier references.
453
449
  */
@@ -462,30 +458,12 @@ function reapplyLoopExitPoints(exits, references, graph) {
462
458
  }
463
459
  seenRefs.add(ref.nodeId);
464
460
  for (const cd of exitCds) {
465
- const { id: cId } = cd;
466
- let setVertex = false;
467
- if (ref.cds) {
468
- if (!ref.cds?.find(c => c.id === cId)) {
469
- ref.cds.push(cd);
470
- setVertex = true;
471
- }
472
- }
473
- else {
474
- ref.cds = [cd];
475
- setVertex = true;
461
+ if (!addControlDependency(ref, cd)) {
462
+ continue;
476
463
  }
477
- if (setVertex) {
478
- const vertex = graph.getVertex(ref.nodeId);
479
- if (vertex) {
480
- if (vertex.cds) {
481
- if (!vertex.cds?.find(c => c.id === cId)) {
482
- vertex.cds.push(cd);
483
- }
484
- }
485
- else {
486
- vertex.cds = [cd];
487
- }
488
- }
464
+ const vertex = graph.getVertex(ref.nodeId);
465
+ if (vertex) {
466
+ addControlDependency(vertex, cd);
489
467
  }
490
468
  }
491
469
  }
@@ -501,28 +479,7 @@ exports.ClosureRefs = {
501
479
  * links what resolves, and leaves only the references that stay open.
502
480
  */
503
481
  resolveOpenIngoing(graph, callId, definition, environment) {
504
- const remainingIn = [];
505
- for (const ingoing of definition.subflow.in) {
506
- const resolved = ingoing.name ? resolve_helper_1.Resolve.byNameAndType(ingoing.name, environment, ingoing.type) : undefined;
507
- if (resolved === undefined) {
508
- remainingIn.push(ingoing);
509
- continue;
510
- }
511
- (0, log_1.expensiveTrace)(logger_1.dataflowLogger, () => `Found ${resolved.length} references to open ref ${ingoing.nodeId} in closure of function definition ${callId}`);
512
- let allBuiltIn = true;
513
- const inId = ingoing.nodeId;
514
- for (const { nodeId, type } of resolved) {
515
- graph.addEdge(inId, nodeId, edge_1.EdgeType.Reads);
516
- graph.addEdge(callId, nodeId, edge_1.EdgeType.Reads); // because the def. is the anonymous call
517
- if (!(0, identifier_1.isReferenceType)(type, identifier_1.ReferenceType.BuiltInConstant | identifier_1.ReferenceType.BuiltInFunction)) {
518
- allBuiltIn = false;
519
- }
520
- }
521
- if (allBuiltIn) {
522
- remainingIn.push(ingoing);
523
- }
524
- }
525
- definition.subflow.in = remainingIn;
482
+ definition.subflow.in = linkInputs(definition.subflow.in, environment, [], graph, false, callId);
526
483
  }
527
484
  };
528
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. */
@@ -548,6 +505,28 @@ function linkFieldReads(graph, accessId, fieldDefs, returnsFunctions) {
548
505
  }
549
506
  }
550
507
  }
508
+ /** The call writing a field into the environment the symbol `readId` holds: `x$f <- v` (its replacement call) or `assign("f", v, envir = x)`. */
509
+ function fieldAssignmentOf(readId, idMap) {
510
+ const read = idMap.get(readId);
511
+ const access = read === undefined ? undefined : model_1.RNode.directParent(read, idMap);
512
+ /* `assign("f", v, envir = x)` writes into `x` as well */
513
+ const call = access === undefined ? undefined : model_1.RNode.directParent(access, idMap);
514
+ if (r_function_call_1.RFunctionCall.isNamed(call) && identifier_1.Identifier.getName(call.functionName.content) === 'assign' && call.arguments.indexOf(access) >= 2) {
515
+ return call.info.id;
516
+ }
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
+ }
527
+ /* the replacement call `$<-` carries the id of the access it writes through */
528
+ return r_binary_op_1.RBinaryOp.is(assign) && assign.lhs.info.id === target.info.id && /^(<<?-|=)$/.test(assign.operator) ? access.info.id : undefined;
529
+ }
551
530
  /**
552
531
  * An access `x$f` written where `x` was still open reads the field of whatever `x` is bound to once that binding is
553
532
  * known, so binding the read at `readId` to a definition holding `envState` links the access to that field.
@@ -561,4 +540,98 @@ function bindAccessedField(graph, readId, envState, idMap) {
561
540
  const fieldName = accessedFieldName((0, unpack_argument_1.unpackArg)(access.access[0]), r_access_1.RAccess.isNamed(access));
562
541
  linkFieldReads(graph, access.info.id, fieldName ? envState.current.memory.get(fieldName) : undefined, true);
563
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
+ }
564
637
  //# sourceMappingURL=linker.js.map