@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
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CallGraph = void 0;
4
+ exports.fallbackUntargetedCall = fallbackUntargetedCall;
4
5
  const graph_1 = require("./graph");
5
6
  const vertex_1 = require("./vertex");
6
7
  const node_id_1 = require("../../r-bridge/lang-4.x/ast/model/processing/node-id");
@@ -25,10 +26,11 @@ function processCds(vtx, graph, result, state) {
25
26
  const UntargetedCallFollow = edge_1.EdgeType.Reads | edge_1.EdgeType.DefinedByOnCall | edge_1.EdgeType.DefinedBy | edge_1.EdgeType.Returns;
26
27
  const UntargetedCallAvoid = edge_1.EdgeType.NonStandardEvaluation | edge_1.EdgeType.Argument;
27
28
  /**
28
- * This tracks the known symbol origins for a function call for which we know that flowr found no targets!
29
+ * The origins of what a call flowR found no target for calls, following its name back through reads and
30
+ * parameter bindings: `f(x)` within `apply_f <- function(f, x) f(x)` ends at what callers hand in as `f`.
29
31
  */
30
32
  function fallbackUntargetedCall(vtx, graph) {
31
- // we track all aliases to their roots here, we know there is no known call target
33
+ /* we track all aliases to their roots here, we know there is no known call target */
32
34
  const collected = new Set();
33
35
  const visited = new Set();
34
36
  const toVisit = [vtx.id];
@@ -43,13 +45,16 @@ function fallbackUntargetedCall(vtx, graph) {
43
45
  continue;
44
46
  }
45
47
  let addedNew = false;
48
+ /* past the call asked about, a call hands back what it returns; a plain read of another call is state it depends on, not its name */
49
+ const follow = currentId !== vtx.id && vertex_1.DfgVertex.isFunctionCall(currentVtx) ? edge_1.EdgeType.Returns : UntargetedCallFollow;
46
50
  for (const [tar, e] of graph.edgesFrom(currentId)) {
47
- if (edge_1.DfEdge.includesType(e, UntargetedCallFollow) && edge_1.DfEdge.doesNotIncludeType(e, UntargetedCallAvoid)) {
51
+ if (edge_1.DfEdge.includesType(e, follow) && edge_1.DfEdge.doesNotIncludeType(e, UntargetedCallAvoid)
52
+ && (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Returns) || !vertex_1.DfgVertex.isFunctionCall(graph.getVertex(tar)))) {
48
53
  addedNew = true;
49
54
  toVisit.push(tar);
50
55
  }
51
56
  }
52
- // we have reached our end(s)
57
+ /* we have reached our end(s) */
53
58
  if (!addedNew && currentId !== vtx.id) {
54
59
  collected.add(currentId);
55
60
  }
@@ -67,23 +72,19 @@ function processCall(vtx, from, graph, result, state) {
67
72
  state.visited.add(vid);
68
73
  result.addVertex(vtx, undefined, true);
69
74
  processCds(vtx, graph, result, state);
70
- // for each call, resolve the targets
71
- const tars = (0, linker_1.getAllFunctionCallTargets)(vid, graph, vtx.environment);
72
75
  let addedTarget = false;
73
76
  let addedBiTarget = false;
74
- for (const tar of tars) {
77
+ for (const tar of (0, linker_1.getAllFunctionCallTargets)(vid, graph, vtx.environment)) {
78
+ const targetVtx = graph.getVertex(tar);
75
79
  if (node_id_1.NodeId.isBuiltIn(tar)) {
76
80
  result.addEdge(vid, tar, edge_1.EdgeType.Calls);
77
81
  addedTarget = true;
78
82
  addedBiTarget = true;
79
- continue;
80
83
  }
81
- const targetVtx = graph.getVertex(tar);
82
- if (!vertex_1.DfgVertex.isFunctionDefinition(targetVtx)) {
83
- continue;
84
+ else if (vertex_1.DfgVertex.isFunctionDefinition(targetVtx)) {
85
+ addedTarget = true;
86
+ processFunctionDefinition(targetVtx, vid, graph, result, state);
84
87
  }
85
- addedTarget = true;
86
- processFunctionDefinition(targetVtx, vid, graph, result, state);
87
88
  }
88
89
  if (!addedBiTarget && vtx.origin !== 'unnamed') {
89
90
  for (const origs of vtx.origin) {
@@ -94,8 +95,7 @@ function processCall(vtx, from, graph, result, state) {
94
95
  }
95
96
  }
96
97
  if (!addedTarget) {
97
- const origs = fallbackUntargetedCall(vtx, graph);
98
- for (const ori of origs) {
98
+ for (const ori of fallbackUntargetedCall(vtx, graph)) {
99
99
  const oriVtx = graph.getVertex(ori);
100
100
  if (!oriVtx) {
101
101
  continue;
@@ -114,30 +114,23 @@ function processCall(vtx, from, graph, result, state) {
114
114
  }
115
115
  }
116
116
  }
117
- // handle arguments, traversing the 'reads' and the 'returns' edges
118
- for (const [tar, e] of graph.edgesFrom(vtx.id)) {
117
+ /* handle arguments, traversing the 'reads' and the 'returns' edges */
118
+ for (const [tar, e] of graph.edgesFrom(vid)) {
119
119
  if (edge_1.DfEdge.doesNotIncludeType(e, edge_1.EdgeType.Reads | edge_1.EdgeType.Returns | edge_1.EdgeType.Argument)) {
120
120
  continue;
121
121
  }
122
122
  const tVtx = graph.getVertex(tar);
123
- if (!tVtx) {
124
- continue;
123
+ if (tVtx) {
124
+ processUnknown(tVtx, vid, graph, result, state);
125
125
  }
126
- processUnknown(tVtx, vtx.id, graph, result, state);
127
126
  }
128
127
  }
129
128
  function processUnknown(vtx, from, graph, result, state) {
130
- switch (vtx.tag) {
131
- case vertex_1.VertexType.FunctionCall:
132
- processCall(vtx, from, graph, result, state);
133
- return;
134
- case vertex_1.VertexType.FunctionDefinition:
135
- if (from) {
136
- result.addEdge(from, node_id_1.NodeId.toBuiltIn('function'), edge_1.EdgeType.Calls);
137
- }
138
- return;
139
- default:
140
- return;
129
+ if (vertex_1.DfgVertex.isFunctionCall(vtx)) {
130
+ processCall(vtx, from, graph, result, state);
131
+ }
132
+ else if (vertex_1.DfgVertex.isFunctionDefinition(vtx) && from) {
133
+ result.addEdge(from, node_id_1.NodeId.toBuiltIn('function'), edge_1.EdgeType.Calls);
141
134
  }
142
135
  }
143
136
  function processFunctionDefinition(vtx, from, graph, result, state) {
@@ -183,6 +176,15 @@ function followCalls(graph, seeds, reached) {
183
176
  }
184
177
  }
185
178
  }
179
+ /** Whether `name` is a `<generic>.<class>` of a generic that is called, as `print.foo` is when `print` is. */
180
+ function mayBeDispatchedTo(name, generics) {
181
+ for (let dot = name.indexOf('.'); dot > 0; dot = name.indexOf('.', dot + 1)) {
182
+ if (generics.has(name.slice(0, dot))) {
183
+ return true;
184
+ }
185
+ }
186
+ return false;
187
+ }
186
188
  /** The names the reached calls go by, which are the generics a method of the program may be dispatched to for. */
187
189
  function reachedCallNames(graph, reached) {
188
190
  const names = new Set();
@@ -194,15 +196,6 @@ function reachedCallNames(graph, reached) {
194
196
  }
195
197
  return names;
196
198
  }
197
- /** Whether `name` is a `<generic>.<class>` of a generic that is called, as `print.foo` is when `print` is. */
198
- function mayBeDispatchedTo(name, generics) {
199
- for (let dot = name.indexOf('.'); dot > 0; dot = name.indexOf('.', dot + 1)) {
200
- if (generics.has(name.slice(0, dot))) {
201
- return true;
202
- }
203
- }
204
- return false;
205
- }
206
199
  /**
207
200
  * The unreached function definitions that may still run: one the code hands to another call (a callback, an
208
201
  * S4 method, a shiny handler), and one bound to a `<generic>.<class>` name of a generic the program calls.
@@ -244,9 +237,7 @@ function mayRunAnyway(graph, reached) {
244
237
  exports.CallGraph = {
245
238
  ...graph_helper_1.GraphHelper,
246
239
  name: 'CallGraph',
247
- /**
248
- * Extracts the sub call graph from the given call graph, starting from the given entry points.
249
- */
240
+ /** Extracts the sub call graph from the given call graph, starting from the given entry points. */
250
241
  computeSubCallGraph(graph, entryPoints) {
251
242
  const result = new graph_1.DataflowGraph(graph.idMap);
252
243
  const toVisit = Array.from(entryPoints);
@@ -301,9 +292,7 @@ exports.CallGraph = {
301
292
  .map(([id]) => id)
302
293
  .toArray();
303
294
  },
304
- /**
305
- * Reduces the call graph by dropping all transitive edges.
306
- */
295
+ /** Reduces the call graph by dropping all transitive edges. */
307
296
  dropTransitiveEdges(graph) {
308
297
  const newCg = new graph_1.DataflowGraph(graph.idMap);
309
298
  newCg.mergeVertices(graph);
@@ -142,7 +142,7 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
142
142
  /** All edges in the complete graph (including those nested in function definition) */
143
143
  private edgeInformation;
144
144
  private incomingIndex?;
145
- private readonly types;
145
+ private types;
146
146
  /** The id list of `tag`, created on first use, so a caller adding many ids looks it up once. */
147
147
  private typeList;
148
148
  private indexType;
@@ -150,6 +150,8 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
150
150
  private unindexType;
151
151
  private qualifiedNames?;
152
152
  toJSON(): DataflowGraphJson;
153
+ /** A view of this graph without the environments of its vertices and subflows, sharing everything else. */
154
+ withoutEnvironments(): DataflowGraph<Vertex, Edge>;
153
155
  /** Gets the {@link DataflowGraphVertexInfo} attached to `id` (searching function definitions too if `includeDefinedFunctions`) and its outgoing edges. */
154
156
  get(id: NodeId, includeDefinedFunctions?: boolean): [Vertex, OutgoingEdges] | undefined;
155
157
  /** Gets the {@link DataflowGraphVertexInfo} attached to `id`, see {@link DataflowGraph#getRootVertex}. */
@@ -230,6 +232,13 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
230
232
  /** Returns true if the root level of the graph contains a node with the given id. */
231
233
  isRoot(id: NodeId): boolean;
232
234
  rootIds(): ReadonlySet<NodeId>;
235
+ /** while tracked (see {@link trackIncomingReads}), how often a read arriving at a vertex was added or removed */
236
+ private readsInto;
237
+ /** Starts (or stops) counting the reads added to or removed from each vertex, see {@link incomingReadsVersion}. */
238
+ trackIncomingReads(on: boolean): void;
239
+ /** How often a read arriving at `id` was added or removed since tracking started; an equal number means the reads are unchanged. */
240
+ incomingReadsVersion(id: NodeId): number;
241
+ private countRead;
233
242
  /**
234
243
  * Takes `type` off the edge `fromId -> toId`, dropping the edge itself once it states nothing.
235
244
  * An edge with no type is no edge: every removal has to go through here so none is left behind.
@@ -249,6 +258,9 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
249
258
  * `consume` takes `otherGraph`'s edge tables instead of copying them (perf). Pass it only for a graph that dies with the call.
250
259
  */
251
260
  mergeWith(otherGraph: DataflowGraph<Vertex, Edge> | undefined, mergeRootVertices?: boolean, consume?: boolean): this;
261
+ /** An empty graph takes over the contents of a graph it consumes rather than copying them entry by entry. */
262
+ private adopt;
263
+ private mergeRootsAndUnknowns;
252
264
  mergeVertices(otherGraph: DataflowGraph<Vertex, Edge>, mergeRootVertices?: boolean): void;
253
265
  private mergeEdges;
254
266
  /** Marks the vertex referenced by `reference` as a definition, with `sourceIds` as the source vertex ids of the def if known. */
@@ -40,10 +40,7 @@ exports.FunctionArgument = {
40
40
  },
41
41
  /** The node id of a non-empty `arg`, e.g. of either `a` or `2` in `foo(a=3, 2)`. */
42
42
  getId(arg) {
43
- if (arg !== r_function_call_1.EmptyArgument) {
44
- return arg?.nodeId;
45
- }
46
- return undefined;
43
+ return arg === r_function_call_1.EmptyArgument ? undefined : arg.nodeId;
47
44
  },
48
45
  /** The name of `arg` if named, `undefined` otherwise. */
49
46
  getName(arg) {
@@ -54,10 +51,7 @@ exports.FunctionArgument = {
54
51
  if (arg === r_function_call_1.EmptyArgument) {
55
52
  return undefined;
56
53
  }
57
- else if (arg.name === undefined) {
58
- return arg.nodeId;
59
- }
60
- return arg.valueId;
54
+ return arg.name === undefined ? arg.nodeId : arg.valueId;
61
55
  },
62
56
  /** Whether `arg` is named exactly `name` (textual match only, not R's argument-matching; see {@link FunctionSemantics.call.match.toSpec} for that). */
63
57
  hasName(arg, name) {
@@ -139,10 +133,7 @@ class DataflowGraph {
139
133
  }
140
134
  /** Every write above keeps an id listed under one tag at most once, so a lookup never hands the same one out twice. */
141
135
  unindexType(tag, id) {
142
- const ids = this.types.get(tag);
143
- if (ids === undefined) {
144
- return;
145
- }
136
+ const ids = this.types.get(tag) ?? [];
146
137
  for (let idx = ids.lastIndexOf(id); idx >= 0; idx = ids.lastIndexOf(id)) {
147
138
  ids.splice(idx, 1);
148
139
  }
@@ -158,9 +149,20 @@ class DataflowGraph {
158
149
  _unknownSideEffects: Array.from(this._unknownSideEffects)
159
150
  };
160
151
  }
152
+ /** A view of this graph without the environments of its vertices and subflows, sharing everything else. */
153
+ withoutEnvironments() {
154
+ const graph = new DataflowGraph(this._idMap);
155
+ graph.rootVertices = this.rootVertices;
156
+ graph.edgeInformation = this.edgeInformation;
157
+ graph.types = this.types;
158
+ graph.vertexInformation = new Map(Array.from(this.vertexInformation, ([id, vertex]) => [id, (vertex_2.DfgVertex.isFunctionDefinition(vertex)
159
+ ? { ...vertex, environment: undefined, subflow: { ...vertex.subflow, environment: undefined } }
160
+ : { ...vertex, environment: undefined })]));
161
+ graph.mergeRootsAndUnknowns(this, false);
162
+ return graph;
163
+ }
161
164
  /** Gets the {@link DataflowGraphVertexInfo} attached to `id` (searching function definitions too if `includeDefinedFunctions`) and its outgoing edges. */
162
165
  get(id, includeDefinedFunctions = true) {
163
- // if we do not want to include function definitions, only retrieve the value if the id is part of the root vertices
164
166
  const vertex = includeDefinedFunctions ? this.getVertex(id) : this.getRootVertex(id);
165
167
  return vertex === undefined ? undefined : [vertex, this.outgoingEdges(id) ?? new Map()];
166
168
  }
@@ -170,10 +172,7 @@ class DataflowGraph {
170
172
  }
171
173
  /** Gets the {@link DataflowGraphVertexInfo} attached to `id`, but only if it is a root-level vertex, see {@link DataflowGraph#getVertex}. */
172
174
  getRootVertex(id) {
173
- if (!this.rootVertices.has(id)) {
174
- return undefined;
175
- }
176
- return this.vertexInformation.get(id);
175
+ return this.rootVertices.has(id) ? this.vertexInformation.get(id) : undefined;
177
176
  }
178
177
  /**
179
178
  * The edges leaving `id`, the mutable map the graph stores, `undefined` for a vertex that has none.
@@ -212,7 +211,7 @@ class DataflowGraph {
212
211
  ingoingEdges(id) {
213
212
  if (this.incomingIndex === undefined) {
214
213
  const index = new Map();
215
- for (const [source, outgoing] of this.edgeInformation.entries()) {
214
+ for (const [source, outgoing] of this.edgeInformation) {
216
215
  for (const [target, edge] of outgoing) {
217
216
  const into = index.get(target);
218
217
  if (into === undefined) {
@@ -236,10 +235,10 @@ class DataflowGraph {
236
235
  isQuoted(id, withOutgoing = false) {
237
236
  /* an nse edge quotes iff it does not originate from a loop marking its body */
238
237
  const quotes = (source, e) => edge_1.DfEdge.includesType(e, edge_1.EdgeType.NonStandardEvaluation) && !model_1.RLoopConstructs.is(this.idMap?.get(source));
239
- if (this.ingoingEdges(id)?.entries().some(([source, e]) => quotes(source, e))) {
238
+ if (this.edgesTo(id).entries().some(([source, e]) => quotes(source, e))) {
240
239
  return true;
241
240
  }
242
- return withOutgoing && (this.outgoingEdges(id)?.values().some(e => quotes(id, e)) ?? false);
241
+ return withOutgoing && this.edgesFrom(id).values().some(e => quotes(id, e));
243
242
  }
244
243
  /**
245
244
  * The cached `pkg::fn` name of the call `id`, asking `resolve` only for a call the cache does not know yet.
@@ -287,9 +286,7 @@ class DataflowGraph {
287
286
  /** Drops the cached qualifications, as the graph may no longer imply them. Every mutator runs through here, so the guard covers them all. */
288
287
  dropQualifications() {
289
288
  (0, assert_1.guard)(!this.consumed, 'this graph was consumed by a merge and must not be changed any more');
290
- if (this.qualifiedNames !== undefined) {
291
- this.qualifiedNames = undefined;
292
- }
289
+ this.qualifiedNames = undefined;
293
290
  }
294
291
  /** For a node of the normalized AST: `nodeId` if it is a DFG vertex itself, else the DFG vertices linked to it, else `undefined`. */
295
292
  getLinked(nodeId) {
@@ -329,11 +326,7 @@ class DataflowGraph {
329
326
  }
330
327
  /** Every vertex carrying `type`, in the order the graph learned of them. */
331
328
  *verticesOfType(type) {
332
- const ids = this.types.get(type);
333
- if (ids === undefined) {
334
- return;
335
- }
336
- for (const id of ids) {
329
+ for (const id of this.types.get(type) ?? []) {
337
330
  const vertex = this.vertexInformation.get(id);
338
331
  /* a stale entry would hand out a vertex without the fields its tag promises */
339
332
  if (vertex?.tag === type) {
@@ -343,11 +336,7 @@ class DataflowGraph {
343
336
  }
344
337
  /** The ids of {@link DataflowGraph#verticesOfType|verticesOfType}, as a fresh array the caller may keep. */
345
338
  vertexIdsOfType(type) {
346
- const ids = this.types.get(type);
347
- if (ids === undefined) {
348
- return [];
349
- }
350
- return ids.filter(id => this.vertexInformation.get(id)?.tag === type);
339
+ return this.types.get(type)?.filter(id => this.vertexInformation.get(id)?.tag === type) ?? [];
351
340
  }
352
341
  /** Ids of all edges in the graph together with their edge information, see {@link DataflowGraph#vertices}. */
353
342
  *edges() {
@@ -364,6 +353,21 @@ class DataflowGraph {
364
353
  rootIds() {
365
354
  return this.rootVertices;
366
355
  }
356
+ /** while tracked (see {@link trackIncomingReads}), how often a read arriving at a vertex was added or removed */
357
+ readsInto;
358
+ /** Starts (or stops) counting the reads added to or removed from each vertex, see {@link incomingReadsVersion}. */
359
+ trackIncomingReads(on) {
360
+ this.readsInto = on ? new Map() : undefined;
361
+ }
362
+ /** How often a read arriving at `id` was added or removed since tracking started; an equal number means the reads are unchanged. */
363
+ incomingReadsVersion(id) {
364
+ return this.readsInto?.get(id) ?? 0;
365
+ }
366
+ countRead(to, type) {
367
+ if (this.readsInto !== undefined && (type & edge_1.EdgeType.Reads) !== 0) {
368
+ this.readsInto.set(to, (this.readsInto.get(to) ?? 0) + 1);
369
+ }
370
+ }
367
371
  /**
368
372
  * Takes `type` off the edge `fromId -> toId`, dropping the edge itself once it states nothing.
369
373
  * An edge with no type is no edge: every removal has to go through here so none is left behind.
@@ -376,6 +380,7 @@ class DataflowGraph {
376
380
  return this;
377
381
  }
378
382
  this.dropQualifications();
383
+ this.countRead(to, type);
379
384
  edge.types &= ~type;
380
385
  if (edge_1.DfEdge.hasAnyType(edge)) {
381
386
  /* the reverse index holds this very object, so a narrowed type is already visible through it */
@@ -404,7 +409,7 @@ class DataflowGraph {
404
409
  }
405
410
  this.dropQualifications();
406
411
  const vtag = vertex.tag;
407
- // keep a clone of the original environment, isolating the snapshot from later updates
412
+ /* keep a clone of the original environment, isolating the snapshot from later updates */
408
413
  let environment;
409
414
  if (vertex.environment) {
410
415
  environment = (0, clone_1.cloneEnvironmentInformation)(vertex.environment);
@@ -415,9 +420,7 @@ class DataflowGraph {
415
420
  }
416
421
  vertex.environment = environment;
417
422
  this.vertexInformation.set(vid, vertex);
418
- if (gas_1.activeDataflowBudget !== undefined) {
419
- gas_1.activeDataflowBudget.vertex();
420
- }
423
+ gas_1.activeDataflowBudget?.vertex();
421
424
  /* an overwrite keeping the tag is indexed already */
422
425
  if (previous?.tag !== vtag) {
423
426
  if (previous !== undefined) {
@@ -435,6 +438,7 @@ class DataflowGraph {
435
438
  return this;
436
439
  }
437
440
  this.dropQualifications();
441
+ this.countRead(toId, type);
438
442
  const fromEdges = this.edgeInformation.get(fromId);
439
443
  const existing = fromEdges?.get(toId);
440
444
  if (existing !== undefined) {
@@ -477,14 +481,30 @@ class DataflowGraph {
477
481
  return this;
478
482
  }
479
483
  (0, assert_1.guard)(!otherGraph.consumed, 'a graph consumed by an earlier merge cannot be merged again');
480
- this.mergeVertices(otherGraph, mergeRootVertices);
481
- this.mergeEdges(otherGraph, consume);
484
+ if (consume && this.vertexInformation.size === 0 && this.edgeInformation.size === 0) {
485
+ this.adopt(otherGraph, mergeRootVertices);
486
+ }
487
+ else {
488
+ this.mergeVertices(otherGraph, mergeRootVertices);
489
+ this.mergeEdges(otherGraph, consume);
490
+ }
482
491
  otherGraph.consumed ||= consume;
483
492
  return this;
484
493
  }
485
- mergeVertices(otherGraph, mergeRootVertices = true) {
494
+ /** An empty graph takes over the contents of a graph it consumes rather than copying them entry by entry. */
495
+ adopt(otherGraph, mergeRootVertices) {
486
496
  this.dropQualifications();
487
- // merge root ids
497
+ this.vertexInformation = otherGraph.vertexInformation;
498
+ this.edgeInformation = otherGraph.edgeInformation;
499
+ this.types = otherGraph.types;
500
+ this.incomingIndex = undefined;
501
+ const takeRoots = mergeRootVertices && this.rootVertices.size === 0;
502
+ if (takeRoots) {
503
+ this.rootVertices = otherGraph.rootVertices;
504
+ }
505
+ this.mergeRootsAndUnknowns(otherGraph, mergeRootVertices && !takeRoots);
506
+ }
507
+ mergeRootsAndUnknowns(otherGraph, mergeRootVertices) {
488
508
  if (mergeRootVertices) {
489
509
  for (const root of otherGraph.rootVertices) {
490
510
  this.rootVertices.add(root);
@@ -493,6 +513,10 @@ class DataflowGraph {
493
513
  for (const unknown of otherGraph.unknownSideEffects) {
494
514
  this._unknownSideEffects.add(unknown);
495
515
  }
516
+ }
517
+ mergeVertices(otherGraph, mergeRootVertices = true) {
518
+ this.dropQualifications();
519
+ this.mergeRootsAndUnknowns(otherGraph, mergeRootVertices);
496
520
  /* the tags of the vertices coming in repeat, so the list each goes on is looked up only when it changes */
497
521
  let lastTag;
498
522
  let lastIds = [];
@@ -585,14 +609,10 @@ class DataflowGraph {
585
609
  }
586
610
  /** Marks the given node as having unknown side effects */
587
611
  markIdForUnknownSideEffects(id, target) {
588
- if (target) {
589
- this._unknownSideEffects.add({
590
- id: node_id_1.NodeId.normalize(id),
591
- linkTo: typeof target.callName === 'string' ? { ...target, callName: new RegExp(target.callName) } : target
592
- });
593
- return this;
594
- }
595
- this._unknownSideEffects.add(node_id_1.NodeId.normalize(id));
612
+ this._unknownSideEffects.add(target ? {
613
+ id: node_id_1.NodeId.normalize(id),
614
+ linkTo: typeof target.callName === 'string' ? { ...target, callName: new RegExp(target.callName) } : target
615
+ } : node_id_1.NodeId.normalize(id));
596
616
  return this;
597
617
  }
598
618
  /** Constructs a dataflow graph instance from the given JSON data, e.g. as sent by the flowR server for further analysis. */
@@ -617,10 +637,7 @@ class DataflowGraph {
617
637
  }
618
638
  exports.DataflowGraph = DataflowGraph;
619
639
  function mergeNodeInfos(current, next) {
620
- if (current.tag !== next.tag) {
621
- return current;
622
- }
623
- else if (vertex_2.DfgVertex.isFunctionDefinition(current)) {
640
+ if (current.tag === next.tag && vertex_2.DfgVertex.isFunctionDefinition(current)) {
624
641
  const n = next;
625
642
  current.exitPoints = (0, arrays_1.uniqueArrayMerge)(current.exitPoints, n.exitPoints);
626
643
  if (n.mode && n.mode.length > 0) {
@@ -636,24 +653,15 @@ function mergeNodeInfos(current, next) {
636
653
  }
637
654
  function envFromJson(json) {
638
655
  const parent = json.parent ? envFromJson(json.parent) : undefined;
639
- const memory = new Map();
640
- for (const [key, value] of Object.entries(json.memory)) {
641
- memory.set(key, value);
642
- }
643
- const obj = new environment_1.Environment(parent, json.builtInEnv);
644
- obj.id = json.id;
645
- obj.adoptMap(memory);
646
- const env = obj;
656
+ const env = new environment_1.Environment(parent, json.builtInEnv);
657
+ env.id = json.id;
658
+ env.adoptMap(new Map(Object.entries(json.memory)));
647
659
  env.n = json.n;
648
660
  env.t = json.t;
649
661
  env.globalEnv = json.globalEnv;
650
662
  return env;
651
663
  }
652
664
  function renvFromJson(json) {
653
- const current = envFromJson(json.current);
654
- return {
655
- current,
656
- level: json.level
657
- };
665
+ return { current: envFromJson(json.current), level: json.level };
658
666
  }
659
667
  //# sourceMappingURL=graph.js.map
@@ -118,7 +118,7 @@ export interface DataflowGraphVertexFunctionCall extends DataflowGraphVertexBase
118
118
  * For a class-declaring call (`setClass`, `setClassUnion`, `setIs`, `setValidity`, `setRefClass`,
119
119
  * `S7::new_class`, `R6::R6Class`): what the declaration states -- its name, superclasses, members, and
120
120
  * whether it can be instantiated. Filled from the {@link ClassDeclarationConfig} the built-in declares,
121
- * so no argument's meaning is guessed. See {@link declaredClasses} to collect these across a graph.
121
+ * so no argument's meaning is guessed. See {@link declaredClassesImpl} to collect these across a graph.
122
122
  */
123
123
  classDecl?: ClassDeclaration;
124
124
  }
@@ -21,6 +21,8 @@ export interface ControlDependency {
21
21
  readonly when?: boolean;
22
22
  /** whether this control dependency was created due to iteration (e.g., a loop) */
23
23
  readonly byIteration?: boolean;
24
+ /** whether this control dependency is a promise's: what stands under it runs only once the call `id` forces it, see {@link ControlFlow.withinPromise} */
25
+ readonly byPromise?: boolean;
24
26
  /**
25
27
  * any file-exist assumptions made
26
28
  */
@@ -93,6 +95,8 @@ export interface ExitPoint {
93
95
  readonly type: ExitPointType;
94
96
  /** The id of the node which causes the exit point! */
95
97
  readonly nodeId: NodeId;
98
+ /** The jump this exit stands for, when {@link ExitPoint#nodeId|nodeId} is a construct it leaves through */
99
+ readonly jump?: NodeId;
96
100
  /**
97
101
  * Control dependencies which influence if the exit point triggers
98
102
  * (e.g., if the `return` is contained within an `if` statement).
@@ -100,12 +104,15 @@ export interface ExitPoint {
100
104
  */
101
105
  readonly cds?: ControlDependency[];
102
106
  }
107
+ /** `exit` leaving through the construct `rootId`, which keeps the jump it stands for. */
108
+ export declare function leaveThrough(exit: ExitPoint, rootId: NodeId): ExitPoint;
103
109
  /**
104
110
  * Adds all non-default exit points to the existing list and updates the `invertExitCds` accordingly.
105
111
  */
106
112
  export declare function addNonDefaultExitPoints(existing: ExitPoint[], invertExitCds: ControlDependency[], activeCds: ControlDependency[] | undefined, add: readonly ExitPoint[]): void;
107
113
  /**
108
- * Overwrites the existing exit points with the given ones, taking care of cds.
114
+ * Overwrites the existing exit points with the given ones, taking care of cds:
115
+ * the existing ones stay if the replacements only happen in some branches.
109
116
  */
110
117
  export declare function overwriteExitPoints(existing: readonly ExitPoint[], replace: ExitPoint[]): ExitPoint[];
111
118
  /**
package/dataflow/info.js CHANGED
@@ -5,6 +5,7 @@ exports.appendCds = appendCds;
5
5
  exports.withCds = withCds;
6
6
  exports.negateControlDependency = negateControlDependency;
7
7
  exports.doesExitPointPropagateCalls = doesExitPointPropagateCalls;
8
+ exports.leaveThrough = leaveThrough;
8
9
  exports.addNonDefaultExitPoints = addNonDefaultExitPoints;
9
10
  exports.overwriteExitPoints = overwriteExitPoints;
10
11
  exports.happensInEveryBranch = happensInEveryBranch;
@@ -13,7 +14,6 @@ exports.filterOutLoopExitPoints = filterOutLoopExitPoints;
13
14
  exports.diffControlDependency = diffControlDependency;
14
15
  exports.diffControlDependencies = diffControlDependencies;
15
16
  const graph_1 = require("./graph/graph");
16
- const assert_1 = require("../util/assert");
17
17
  const model_1 = require("../r-bridge/lang-4.x/ast/model/model");
18
18
  /** Whether the two control dependencies trigger on the same condition and branch. */
19
19
  function sameControlDependency(a, b) {
@@ -48,10 +48,7 @@ exports.ControlDependency = {
48
48
  };
49
49
  /** Appends the given control dependencies to `target`, skipping the ones that are already in there. */
50
50
  function appendCds(target, toAdd) {
51
- if (!toAdd) {
52
- return;
53
- }
54
- for (const add of toAdd) {
51
+ for (const add of toAdd ?? []) {
55
52
  if (!target.some(have => sameControlDependency(have, add))) {
56
53
  target.push(add);
57
54
  }
@@ -59,7 +56,7 @@ function appendCds(target, toAdd) {
59
56
  }
60
57
  /** A copy of `base` with `toAdd` appended, skipping the control dependencies that are already in there. */
61
58
  function withCds(base, toAdd) {
62
- const result = base ? Array.from(base) : [];
59
+ const result = [...base ?? []];
63
60
  appendCds(result, toAdd);
64
61
  return result;
65
62
  }
@@ -79,30 +76,29 @@ function negateControlDependency(cd) {
79
76
  function doesExitPointPropagateCalls(type) {
80
77
  return type === 4 /* ExitPointType.Error */;
81
78
  }
79
+ /** `exit` leaving through the construct `rootId`, which keeps the jump it stands for. */
80
+ function leaveThrough(exit, rootId) {
81
+ return { ...exit, nodeId: rootId, jump: exit.jump ?? exit.nodeId };
82
+ }
82
83
  /**
83
84
  * Adds all non-default exit points to the existing list and updates the `invertExitCds` accordingly.
84
85
  */
85
86
  function addNonDefaultExitPoints(existing, invertExitCds, activeCds, add) {
86
- const toAdd = add.filter(({ type }) => type !== 0 /* ExitPointType.Default */);
87
- if (toAdd.length === 0) {
88
- return;
89
- }
90
- const invertedCds = toAdd.flatMap(e => e.cds?.filter(icd => !activeCds?.some(e => e.id === icd.id && e.when === icd.when)).map(negateControlDependency)).filter(assert_1.isNotUndefined);
91
- for (const ep of toAdd) {
92
- existing.push(ep);
93
- }
94
- for (const icd of invertedCds) {
95
- if (!invertExitCds.some(e => e.id === icd.id && e.when === icd.when)) {
96
- invertExitCds.push(icd);
87
+ for (const ep of add) {
88
+ if (ep.type !== 0 /* ExitPointType.Default */) {
89
+ existing.push(ep);
90
+ const ownCds = exports.ControlDependency.minus(ep.cds ?? [], activeCds ?? []);
91
+ appendCds(invertExitCds, ownCds.map(negateControlDependency));
97
92
  }
98
93
  }
99
94
  }
100
95
  /**
101
- * Overwrites the existing exit points with the given ones, taking care of cds.
96
+ * Overwrites the existing exit points with the given ones, taking care of cds:
97
+ * the existing ones stay if the replacements only happen in some branches.
102
98
  */
103
99
  function overwriteExitPoints(existing, replace) {
104
100
  const replaceCds = replace.flatMap(e => e.cds);
105
- if (replaceCds.length === 0 || replaceCds.includes(undefined) || happensInEveryBranch(replaceCds.filter(e => e !== undefined))) {
101
+ if (replaceCds.length === 0 || replaceCds.includes(undefined) || happensInEveryBranch(replaceCds)) {
106
102
  return replace;
107
103
  }
108
104
  return existing.concat(replace);
@@ -149,22 +145,20 @@ function happensInEveryBranch(cds) {
149
145
  function coversSet(cds) {
150
146
  /* as deep as the surrounding branches, so the pairwise scan beats any set */
151
147
  for (const { id, when } of cds) {
152
- if (when !== true && when !== false) {
153
- continue;
154
- }
155
- let counterpart = false;
156
- for (const other of cds) {
157
- if (other.id === id && other.when === !when) {
158
- counterpart = true;
159
- break;
160
- }
161
- }
162
- if (!counterpart) {
148
+ if (when !== undefined && !hasCounterpart(cds, id, !when)) {
163
149
  return false;
164
150
  }
165
151
  }
166
152
  return true;
167
153
  }
154
+ function hasCounterpart(cds, id, when) {
155
+ for (const other of cds) {
156
+ if (other.id === id && other.when === when) {
157
+ return true;
158
+ }
159
+ }
160
+ return false;
161
+ }
168
162
  /** As {@link happensInEveryBranch}, but for a set of control dependencies. */
169
163
  function happensInEveryBranchSet(cds) {
170
164
  return cds === undefined || (cds.size !== 0 && coversSet(cds));