@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.Deferred = void 0;
4
+ exports.verticesByName = verticesByName;
4
5
  const model_1 = require("../../../../../r-bridge/lang-4.x/ast/model/model");
5
6
  const r_function_call_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
6
7
  const node_id_1 = require("../../../../../r-bridge/lang-4.x/ast/model/processing/node-id");
@@ -9,32 +10,32 @@ const retriever_1 = require("../../../../../r-bridge/retriever");
9
10
  const edge_1 = require("../../../../graph/edge");
10
11
  const vertex_1 = require("../../../../graph/vertex");
11
12
  const happens_before_1 = require("../../../../../control-flow/happens-before");
12
- const logic_1 = require("../../../../../util/logic");
13
13
  const r_symbol_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol");
14
- /** Drops the read of `target` from `use`, for a binding another write has replaced. */
15
- function dropRead(graph, use, target) {
16
- graph.removeEdgeType(use, target, edge_1.EdgeType.Reads);
17
- }
18
- function add(index, name, id) {
19
- const known = index.get(name);
20
- if (known) {
21
- known.push(id);
22
- }
23
- else {
24
- index.set(name, [id]);
25
- }
14
+ const linker_1 = require("../../../linker");
15
+ const frame_reflection_1 = require("../../../../fn/frame-reflection");
16
+ const built_in_proc_name_1 = require("../../../../environments/built-in-proc-name");
17
+ /** The vertices of `type` by their unquoted name. */
18
+ function verticesByName(graph, type, idMap) {
19
+ const named = graph.vertexIdsOfType(type).filter(id => node_id_1.NodeId.recoverName(id, idMap) !== undefined);
20
+ return Map.groupBy(named, id => (0, retriever_1.removeRQuotes)(node_id_1.NodeId.recoverName(id, idMap)));
26
21
  }
27
22
  /** The names a deferred expression touches, each with the node touching it and whether it is written. */
28
23
  function namesWithin(expr, graph, idMap) {
29
- const node = idMap.get(expr);
30
24
  const names = [];
31
- if (node === undefined) {
32
- return names;
33
- }
34
25
  const callees = new Set();
35
- model_1.RNode.visitAst(node, inner => {
26
+ model_1.RNode.visitAst(idMap.get(expr), inner => {
36
27
  if (r_function_call_1.RFunctionCall.isNamed(inner)) {
37
28
  callees.add(inner.functionName.info.id);
29
+ /* a call reads the function it names, so `evalq(f())` needs the definition of `f` */
30
+ if (vertex_1.DfgVertex.isFunctionCall(graph.getVertex(inner.info.id))) {
31
+ names.push([inner.info.id, identifier_1.Identifier.getName(inner.functionName.content), false]);
32
+ for (const def of (0, frame_reflection_1.edgeSources)(graph, inner.info.id, edge_1.EdgeType.SideEffectOnCall)) {
33
+ const name = node_id_1.NodeId.recoverName(def, idMap);
34
+ if (name !== undefined) {
35
+ names.push([def, (0, retriever_1.removeRQuotes)(name), true]);
36
+ }
37
+ }
38
+ }
38
39
  return false;
39
40
  }
40
41
  else if (!r_symbol_1.RSymbol.is(inner) || callees.has(inner.info.id)) {
@@ -60,58 +61,56 @@ function namesWithin(expr, graph, idMap) {
60
61
  exports.Deferred = {
61
62
  /** Where each name is bound and read, built once and shared by every deferred expression in the graph. */
62
63
  indexOf(graph, idMap) {
63
- const definitions = new Map();
64
- const uses = new Map();
65
- for (const [type, index] of [[vertex_1.VertexType.VariableDefinition, definitions], [vertex_1.VertexType.Use, uses]]) {
66
- for (const [id] of graph.verticesOfType(type)) {
67
- const name = node_id_1.NodeId.recoverName(id, idMap);
68
- if (name !== undefined) {
69
- add(index, (0, retriever_1.removeRQuotes)(name), id);
70
- }
71
- }
72
- }
73
- return { definitions, uses };
64
+ return {
65
+ definitions: verticesByName(graph, vertex_1.VertexType.VariableDefinition, idMap),
66
+ uses: verticesByName(graph, vertex_1.VertexType.Use, idMap)
67
+ };
74
68
  },
75
69
  /**
76
70
  * The reads that may be the one forcing `binding`: every read of it that no other read always precedes,
77
71
  * since an earlier read would already have forced it and cached the value.
72
+ * With `viaClosures`, a closure reading a `delayedAssign` binding forces it when called.
78
73
  */
79
- forcedAt(graph, binding, cfg) {
80
- const reads = [];
81
- for (const [reader, edge] of graph.edgesTo(binding)) {
82
- if (edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Reads)) {
83
- reads.push(reader);
84
- }
85
- }
74
+ forcedAt(graph, binding, cfg, viaClosures = false) {
75
+ const reads = (0, frame_reflection_1.edgeSources)(graph, binding, edge_1.EdgeType.Reads);
76
+ const readSet = new Set(reads);
77
+ /* a closure reading it and the call of that closure may skip the read, so they never rule out a later force */
78
+ const calls = viaClosures
79
+ ? (0, frame_reflection_1.edgeSources)(graph, binding, edge_1.EdgeType.DefinesOnCall | edge_1.EdgeType.DefinedByOnCall).filter(r => !readSet.has(r))
80
+ : [];
86
81
  if (reads.length < 2) {
87
- return reads;
82
+ return reads.concat(calls);
88
83
  }
89
- /* `Always` implies reachable, so only the reads met walking back need the exact check */
90
- const readSet = new Set(reads);
91
- return reads.filter(r => !(0, happens_before_1.someReachableTo)(cfg, r, previous => readSet.has(previous) && (0, happens_before_1.happensBefore)(cfg, previous, r) === logic_1.Ternary.Always));
84
+ /* the reads of a parameter all sit in its function, which is where the walk can stay */
85
+ const parameter = graph.idMap?.get(binding)?.info.parent;
86
+ const owner = graph.getVertex(graph.idMap?.get(parameter ?? '')?.info.parent ?? '');
87
+ const first = (0, happens_before_1.firstOnSomePath)(cfg, readSet, vertex_1.DfgVertex.isFunctionDefinition(owner) && node_id_1.NodeId.normalize(binding) in owner.params ? owner.subflow.graph : undefined);
88
+ return reads.filter(r => first.has(r)).concat(calls);
92
89
  },
93
90
  /**
94
91
  * Links the writes an expression performs to the uses that may observe them, for a call evaluating the
95
92
  * expression at a point control flow can pin down (`eval`). The reads are settled by the evaluating call
96
93
  * itself, against the environment in effect there, so only this direction is left open.
97
94
  */
98
- publish(graph, expr, index, idMap, at, cfg) {
95
+ publish(graph, expr, index, idMap, at, sites, cfg) {
99
96
  const within = namesWithin(expr, graph, idMap);
100
97
  const own = new Set(within.map(([id]) => id));
101
98
  let reachedFromAt;
102
- for (const [node, name, writes] of within) {
103
- if (!writes) {
104
- continue;
99
+ let reachedFromSites;
100
+ for (const [node, name] of within.filter(([, , writes]) => writes)) {
101
+ /* a write leaving the evaluating frame lands whenever a call of the closure evaluating it does */
102
+ const escapes = vertex_1.DfgVertex.hasOrigin(graph.getVertex(idMap.get(node)?.info.parent ?? ''), built_in_proc_name_1.BuiltInProcName.SuperAssignment);
103
+ for (const site of escapes ? sites : []) {
104
+ if (site !== at) {
105
+ graph.addEdge(node, site, edge_1.EdgeType.SideEffectOnCall);
106
+ }
105
107
  }
106
108
  for (const use of index.uses.get(name) ?? []) {
107
109
  /* only a use the evaluation may reach can see what it wrote */
108
- if (cfg !== undefined) {
109
- reachedFromAt ??= (0, happens_before_1.reachableFrom)(cfg, [at]);
110
- }
111
- if (own.has(use) || (reachedFromAt !== undefined && !reachedFromAt.has(use))) {
112
- continue;
110
+ const reached = cfg === undefined ? undefined : escapes ? reachedFromSites ??= (0, happens_before_1.reachableFrom)(cfg, [at, ...sites]) : reachedFromAt ??= (0, happens_before_1.reachableFrom)(cfg, [at]);
111
+ if (!own.has(use) && (reached === undefined || reached.has(use))) {
112
+ graph.addEdge(use, node, edge_1.EdgeType.Reads);
113
113
  }
114
- graph.addEdge(use, node, edge_1.EdgeType.Reads);
115
114
  }
116
115
  }
117
116
  },
@@ -124,14 +123,25 @@ exports.Deferred = {
124
123
  const within = namesWithin(expr, graph, idMap);
125
124
  const own = new Set(within.map(([id]) => id));
126
125
  /* one walk per direction answers every candidate, so the sets are built once and only when asked for */
127
- let before;
128
- let after;
126
+ const reach = forces?.reach ?? {};
129
127
  /* a binding matters only if some force can see it, and a use only if some force can reach it */
130
128
  const seenByAForce = (definition) => forces === undefined
131
- || (before ??= (0, happens_before_1.reachableTo)(forces.cfg, forces.sites)).has(definition);
129
+ || (reach.before ??= (0, happens_before_1.reachableTo)(forces.cfg, forces.sites)).has(definition);
132
130
  /* the forcing read yields the promise's value, so only reads after it observe what the promise wrote */
133
131
  const reachedByAForce = (use) => forces === undefined
134
- || (!forces.sites.includes(use) && (after ??= (0, happens_before_1.reachableFrom)(forces.cfg, forces.sites)).has(use));
132
+ || (!forces.sites.includes(use) && (reach.after ??= (0, happens_before_1.reachableFrom)(forces.cfg, forces.sites)).has(use));
133
+ /* whether some force always happens before `use`, asked again by every write of the bound name */
134
+ let sites;
135
+ const alwaysAfterAForce = (use) => {
136
+ const known = (reach.always ??= new Map()).get(use);
137
+ if (known !== undefined || forces === undefined) {
138
+ return known ?? false;
139
+ }
140
+ /* one walk back from the use answers it for all sites, a use is never a site itself (see `reachedByAForce`) */
141
+ const always = (0, happens_before_1.someAlwaysBefore)(forces.cfg, sites ??= new Set(forces.sites), use);
142
+ reach.always.set(use, always);
143
+ return always;
144
+ };
135
145
  /* `delayedAssign` names its variable with a string literal, so the recovered name still carries quotes */
136
146
  const bindingName = forces === undefined ? undefined : (0, retriever_1.removeRQuotes)(node_id_1.NodeId.recoverName(forces.binding, idMap) ?? '');
137
147
  for (const [node, name, writes] of within) {
@@ -139,19 +149,27 @@ exports.Deferred = {
139
149
  /* a write of the bound name replaces the binding, so a read it definitely reaches sees only it */
140
150
  const shadows = forces !== undefined && name === bindingName && graph.getVertex(node)?.cds === undefined;
141
151
  for (const use of index.uses.get(name) ?? []) {
142
- if (!own.has(use) && reachedByAForce(use)) {
143
- graph.addEdge(use, node, edge_1.EdgeType.Reads);
144
- if (shadows && forces.sites.some(site => (0, happens_before_1.happensBefore)(forces.cfg, site, use) === logic_1.Ternary.Always)) {
145
- dropRead(graph, use, forces.binding);
146
- }
152
+ if (own.has(use) || !reachedByAForce(use)) {
153
+ continue;
154
+ }
155
+ graph.addEdge(use, node, edge_1.EdgeType.Reads);
156
+ /* the write only happens because a force runs it */
157
+ for (const site of forces?.sites ?? []) {
158
+ graph.addEdge(node, site, edge_1.EdgeType.Reads);
159
+ }
160
+ if (shadows && alwaysAfterAForce(use)) {
161
+ graph.removeEdgeType(use, forces.binding, edge_1.EdgeType.Reads);
147
162
  }
148
163
  }
149
164
  }
150
165
  else {
151
- for (const definition of index.definitions.get(name) ?? []) {
152
- if (seenByAForce(definition)) {
153
- graph.addEdge(node, definition, edge_1.EdgeType.Reads);
154
- }
166
+ const seen = (index.definitions.get(name) ?? []).filter(seenByAForce);
167
+ for (const definition of seen) {
168
+ graph.addEdge(node, definition, edge_1.EdgeType.Reads);
169
+ }
170
+ if (forces !== undefined) {
171
+ /* a deferred call runs what its name holds at the force, which may be defined after the call was written */
172
+ (0, linker_1.linkCallToDefinitions)(graph, node, seen, idMap);
155
173
  }
156
174
  }
157
175
  }
@@ -3,7 +3,7 @@ import type { FnSig } from '../../../../environments/built-in-props';
3
3
  import type { DataflowInformation } from '../../../../info';
4
4
  import type { RSymbol } from '../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
5
5
  import type { ParentInformation } from '../../../../../r-bridge/lang-4.x/ast/model/processing/decorate';
6
- import { type PotentiallyEmptyRArgument } from '../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
6
+ import type { PotentiallyEmptyRArgument } from '../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
7
7
  import type { NodeId } from '../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
8
8
  import type { RNode } from '../../../../../r-bridge/lang-4.x/ast/model/model';
9
9
  import { type IdentifierReference } from '../../../../environments/identifier';
@@ -54,7 +54,7 @@ export interface ProcessKnownFunctionCallResult {
54
54
  readonly fnRef: IdentifierReference;
55
55
  /**
56
56
  * The arguments as recorded on the function call vertex.
57
- * They are also part of the information via the function call vertex adde, but sometimes useful separately.
57
+ * They are also part of the information via the function call vertex added, but sometimes useful separately.
58
58
  * For example, together with {@link FunctionSemantics.call.match.toSpec} to do custom parameter matching.
59
59
  */
60
60
  readonly callArgs: readonly FunctionArgument[];
@@ -88,6 +88,8 @@ export interface NseMarkOptions {
88
88
  /** ids an unquote escape splices back in, see {@link FunctionSemantics.call.nse.unquoted} */
89
89
  readonly evaluated?: ReadonlySet<NodeId>;
90
90
  }
91
+ /** The positions of the arguments `which` names among `count` arguments. */
92
+ export declare function nseArgumentPositions(which: NseArguments | readonly number[] | undefined, count: number): readonly number[];
91
93
  /**
92
94
  * Marks the selected arguments as {@link EdgeType.NonStandardEvaluation}: a {@link NseKind.Quoted|quoted} one
93
95
  * entirely, a {@link NseKind.DataMasked|data-masked} one only where {@link FunctionSemantics.call.nse.suppliedByMask} holds, and a
@@ -1,18 +1,22 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NseKind = exports.NseArguments = void 0;
4
+ exports.nseArgumentPositions = nseArgumentPositions;
4
5
  exports.markArgumentsAsNonStandardEvaluation = markArgumentsAsNonStandardEvaluation;
5
6
  exports.processKnownFunctionCall = processKnownFunctionCall;
6
7
  const processor_1 = require("../../../../processor");
7
8
  const function_semantics_1 = require("../../../../fn/function-semantics");
9
+ const info_1 = require("../../../../info");
8
10
  const common_1 = require("./common");
9
11
  const apply_kill_1 = require("../../../../environments/apply-kill");
10
- const r_function_call_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
11
12
  const r_argument_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
13
+ const r_string_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string");
12
14
  const identifier_1 = require("../../../../environments/identifier");
13
15
  const graph_1 = require("../../../../graph/graph");
14
16
  const edge_1 = require("../../../../graph/edge");
15
17
  const control_flow_1 = require("../../../control-flow");
18
+ const logic_1 = require("../../../../../util/logic");
19
+ const assert_1 = require("../../../../../util/assert");
16
20
  const logger_1 = require("../../../../logger");
17
21
  const vertex_1 = require("../../../../graph/vertex");
18
22
  const unknown_side_effect_1 = require("../../../../graph/unknown-side-effect");
@@ -43,29 +47,30 @@ var NseKind;
43
47
  /** evaluated, just not exactly once (a loop body), so only the argument itself is marked */
44
48
  NseKind["Reevaluated"] = "reevaluated";
45
49
  })(NseKind || (exports.NseKind = NseKind = {}));
50
+ /** The positions of the arguments `which` names among `count` arguments. */
51
+ function nseArgumentPositions(which, count) {
52
+ if (typeof which !== 'string') {
53
+ return which ?? [];
54
+ }
55
+ const all = Array.from({ length: count }, (_, i) => i);
56
+ if (which === NseArguments.First) {
57
+ return all.slice(0, 1);
58
+ }
59
+ return which === NseArguments.AllButFirst ? all.slice(1) : all;
60
+ }
46
61
  /**
47
62
  * Marks the selected arguments as {@link EdgeType.NonStandardEvaluation}: a {@link NseKind.Quoted|quoted} one
48
63
  * entirely, a {@link NseKind.DataMasked|data-masked} one only where {@link FunctionSemantics.call.nse.suppliedByMask} holds, and a
49
64
  * {@link NseKind.Reevaluated|reevaluated} one as a whole but not within.
50
65
  */
51
66
  function markArgumentsAsNonStandardEvaluation(graph, rootId, processedArguments, which, { kind = NseKind.Quoted, evaluated } = {}) {
52
- if (which === undefined) {
53
- return;
54
- }
55
- if (typeof which !== 'string') {
56
- for (const i of which) {
57
- if (i < processedArguments.length) {
58
- markArgument(graph, rootId, processedArguments[i], kind, evaluated);
59
- }
60
- else {
61
- logger_1.dataflowLogger.warn(`Trying to mark argument ${i} as non-standard-evaluation, but only ${processedArguments.length} arguments are available`);
62
- }
67
+ for (const i of nseArgumentPositions(which, processedArguments.length)) {
68
+ if (i < processedArguments.length) {
69
+ markArgument(graph, rootId, processedArguments[i], kind, evaluated);
70
+ }
71
+ else {
72
+ logger_1.dataflowLogger.warn(`Trying to mark argument ${i} as non-standard-evaluation, but only ${processedArguments.length} arguments are available`);
63
73
  }
64
- return;
65
- }
66
- const end = which === NseArguments.First ? 1 : processedArguments.length;
67
- for (let i = which === NseArguments.AllButFirst ? 1 : 0; i < end; i++) {
68
- markArgument(graph, rootId, processedArguments[i], kind, evaluated);
69
74
  }
70
75
  }
71
76
  function markArgument(graph, rootId, arg, kind, evaluated) {
@@ -94,13 +99,23 @@ function markArgument(graph, rootId, arg, kind, evaluated) {
94
99
  * The main processor for function calls for which we know the target but need not
95
100
  * add any specific handling.
96
101
  */
97
- function processKnownFunctionCall({ name, args, rootId, data, reverseOrder = false, markAsNSE = undefined, sig, patchData = d => d, hasUnknownSideEffect, origin, nonFunction, customControlFlow, alternativeArgsFrom }) {
102
+ function processKnownFunctionCall({ name, args, rootId, data, reverseOrder = false, markAsNSE = undefined, sig, patchData, hasUnknownSideEffect, origin, nonFunction, customControlFlow, alternativeArgsFrom }) {
98
103
  const functionName = (0, processor_1.processDataflowFor)(name, data);
99
104
  const finalGraph = new graph_1.DataflowGraph(data.completeAst.idMap);
100
105
  const functionCallName = name.content;
101
106
  const processArgs = reverseOrder ? args.toReversed() : args;
102
107
  const forced = function_semantics_1.FunctionSemantics.call.signature.forced(sig, processArgs.length);
103
- const { finalEnv, callArgs, remainingReadInArgs, processedArguments } = (0, common_1.processAllArguments)({ functionName, args: processArgs, data, finalGraph, functionRootId: rootId, patchData, forced, nonFunction });
108
+ const { finalEnv, callArgs, remainingReadInArgs, processedArguments } = (0, common_1.processAllArguments)({
109
+ functionName,
110
+ args: processArgs,
111
+ data: origin === 'default' ? control_flow_1.ControlFlow.withinPromise(data, rootId) : data,
112
+ finalGraph,
113
+ functionRootId: rootId,
114
+ patchData,
115
+ forced,
116
+ nonFunction,
117
+ alternativesFrom: reverseOrder ? undefined : alternativeArgsFrom
118
+ });
104
119
  markArgumentsAsNonStandardEvaluation(finalGraph, rootId, processedArguments, markAsNSE, { kind: NseKind.Reevaluated });
105
120
  const onlyBuiltin = data.builtInNoEnv === rootId;
106
121
  finalGraph.addVertex({
@@ -117,69 +132,51 @@ function processKnownFunctionCall({ name, args, rootId, data, reverseOrder = fal
117
132
  if (hasUnknownSideEffect) {
118
133
  (0, unknown_side_effect_1.handleUnknownSideEffect)(finalGraph, data.environment, rootId);
119
134
  }
120
- const inIds = remainingReadInArgs;
121
135
  const fnRef = { nodeId: rootId, name: functionCallName, cds: data.cds, type: identifier_1.ReferenceType.Function };
122
- inIds.push(fnRef);
123
- // if force args is not none, we need to collect all non-default exit points from our arguments!
136
+ remainingReadInArgs.push(fnRef);
137
+ /* each argument is a promise, and how surely the call forces it decides what a jump within it does */
124
138
  let exitPoints = undefined;
139
+ /* what a function flowR does not know here forces is up to its body, see `FunctionSemantics.settleJumpsInPromises` */
140
+ const forcing = origin === 'default' ? undefined : function_semantics_1.FunctionSemantics.call.signature.forcing(sig, processArgs.length);
141
+ /* exactly one alternative runs when a string selects among them with a default arm, a number past the arms picks none */
142
+ const selector = alternativeArgsFrom === undefined ? undefined : processArgs[alternativeArgsFrom - 1];
143
+ const hasDefaultArm = r_string_1.RString.is(r_argument_1.RArgument.valueOf(selector)) && processArgs.slice(alternativeArgsFrom).some(r_argument_1.RArgument.isUnnamed);
144
+ const alternatives = hasDefaultArm ? processedArguments.slice(alternativeArgsFrom).filter(assert_1.isNotUndefined) : [];
145
+ const allArmsJump = alternatives.length > 0 && alternatives.every(a => control_flow_1.ControlFlow.alwaysExits(a, data.cds));
125
146
  /* a jump that only happens under a condition still lets the call complete, so its default exit has to stay */
126
- let alwaysJumps = false;
127
- if (forced) {
128
- for (let i = 0; i < processedArguments.length; i++) {
129
- const p = processedArguments[i];
130
- if (p === undefined || !forced[i]) {
131
- continue;
132
- }
133
- const before = exitPoints?.length ?? 0;
134
- for (const exit of p.exitPoints) {
135
- if (exit.type !== 0 /* ExitPointType.Default */) {
136
- (exitPoints ??= []).push(exit);
137
- }
138
- }
139
- alwaysJumps ||= (exitPoints?.length ?? 0) > before && control_flow_1.ControlFlow.alwaysExits(p);
147
+ let alwaysJumps = allArmsJump;
148
+ for (const [i, p] of processedArguments.entries()) {
149
+ const isAlternative = alternativeArgsFrom !== undefined && i >= alternativeArgsFrom;
150
+ /* the alternative that is picked runs for sure, the others not at all */
151
+ const f = isAlternative ? logic_1.Ternary.Always : forcing?.[i] ?? logic_1.Ternary.Maybe;
152
+ /* a call with its own control flow wires up what it does not surely force itself */
153
+ if (p === undefined || (customControlFlow && f !== logic_1.Ternary.Always)) {
154
+ continue;
140
155
  }
141
- if (exitPoints !== undefined && !alwaysJumps) {
142
- exitPoints.push({ nodeId: rootId, type: 0 /* ExitPointType.Default */, cds: data.cds });
156
+ const exits = control_flow_1.ControlFlow.promise(finalGraph, p, f, rootId);
157
+ if (exits.length > 0) {
158
+ /* an arm that is not the only way out leaves only when it is picked */
159
+ const conditional = isAlternative && !allArmsJump;
160
+ (exitPoints ??= []).push(...conditional ? exits.map(e => ({ ...e, cds: (0, info_1.withCds)(e.cds, [{ id: rootId, when: true }]) })) : exits);
161
+ alwaysJumps ||= f === logic_1.Ternary.Always && !isAlternative && control_flow_1.ControlFlow.alwaysExits(p, data.cds);
143
162
  }
144
163
  }
145
- /* an alternative without a name is the default arm, which runs when nothing else matched */
164
+ if (exitPoints !== undefined && !alwaysJumps) {
165
+ exitPoints.push({ nodeId: rootId, type: 0 /* ExitPointType.Default */, cds: data.cds });
166
+ }
146
167
  const cfgEntry = customControlFlow ? rootId
147
168
  : alternativeArgsFrom === undefined ? control_flow_1.ControlFlow.inSequence(finalGraph, processedArguments, rootId)
148
- : control_flow_1.ControlFlow.picksOneOf(finalGraph, processedArguments, alternativeArgsFrom, rootId, processArgs.slice(alternativeArgsFrom).some(a => a !== r_function_call_1.EmptyArgument && (!r_argument_1.RArgument.is(a) || a.name === undefined)));
149
- if (!customControlFlow) {
150
- /*
151
- * A jump within an argument that the call does not pass on is caught here (the callee decides whether it
152
- * ever forces that argument), so control resumes at the call rather than leaving it.
153
- */
154
- for (const argument of processedArguments) {
155
- for (const exit of argument?.exitPoints ?? []) {
156
- /*
157
- * A jump within an argument the call does not pass on resumes at the call. A `return` leaves the
158
- * enclosing function wherever the argument runs at all, so only a call that never evaluates it
159
- * may catch one (see `processQuote`).
160
- */
161
- if (exit.type !== 0 /* ExitPointType.Default */ && exit.type !== 1 /* ExitPointType.Return */ && !exitPoints?.includes(exit)) {
162
- finalGraph.addEdge(exit.nodeId, rootId, edge_1.EdgeType.FlowEdge);
163
- }
164
- }
165
- }
166
- }
167
- let kill = undefined;
168
- for (const p of processedArguments) {
169
- const kills = p?.kill;
170
- if (kills !== undefined && kills.length > 0) {
171
- kill = kill === undefined ? kills.slice() : kill.concat(kills);
172
- }
173
- }
174
- const environment = kill ? (0, apply_kill_1.applyKills)(finalEnv, kill) : finalEnv;
169
+ : control_flow_1.ControlFlow.picksOneOf(finalGraph, processedArguments, alternativeArgsFrom, rootId, hasDefaultArm);
170
+ const kills = processedArguments.flatMap(p => p?.kill ?? []);
171
+ const kill = kills.length > 0 ? kills : undefined;
175
172
  return {
176
173
  information: {
177
174
  unknownReferences: [],
178
- in: inIds,
175
+ in: remainingReadInArgs,
179
176
  /* we do not keep the argument out as it has been linked by the function */
180
177
  out: functionName.out,
181
178
  graph: finalGraph,
182
- environment,
179
+ environment: kill ? (0, apply_kill_1.applyKills)(finalEnv, kill) : finalEnv,
183
180
  entryPoint: rootId,
184
181
  cfgEntry: cfgEntry === rootId ? undefined : cfgEntry,
185
182
  exitPoints: exitPoints ?? [{ nodeId: rootId, type: 0 /* ExitPointType.Default */, cds: data.cds }],
@@ -1,13 +1,11 @@
1
1
  import type { DataflowProcessorInformation } from '../../../../processor';
2
2
  import { DataflowInformation } from '../../../../info';
3
3
  import type { ParentInformation } from '../../../../../r-bridge/lang-4.x/ast/model/processing/decorate';
4
- import type { PotentiallyEmptyRArgument } from '../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
5
- import type { RSymbol } from '../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
4
+ import { type PotentiallyEmptyRArgument } from '../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
5
+ import { RSymbol } from '../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
6
6
  import { NodeId } from '../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
7
7
  import type { DataflowGraph } from '../../../../graph/graph';
8
- /**
9
- * Marks the given function call node as only calling built-in functions.
10
- */
8
+ /** Marks the given function call node as only calling built-in functions. */
11
9
  export declare function markAsOnlyBuiltIn(graph: DataflowGraph, rootId: NodeId, keepEnvironment?: boolean): void;
12
10
  /**
13
11
  * Processes a named function call within the dataflow analysis.
@@ -2,10 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.markAsOnlyBuiltIn = markAsOnlyBuiltIn;
4
4
  exports.processNamedCall = processNamedCall;
5
+ const built_in_ambient_state_1 = require("./built-in/built-in-ambient-state");
5
6
  const function_semantics_1 = require("../../../../fn/function-semantics");
6
7
  const info_1 = require("../../../../info");
7
8
  const known_call_handling_1 = require("./known-call-handling");
8
9
  const append_1 = require("../../../../environments/append");
10
+ const r_function_call_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
11
+ const r_binary_op_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-binary-op");
12
+ const r_symbol_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol");
13
+ const r_function_definition_1 = require("../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
9
14
  const node_id_1 = require("../../../../../r-bridge/lang-4.x/ast/model/processing/node-id");
10
15
  const identifier_1 = require("../../../../environments/identifier");
11
16
  const r_base_packages_1 = require("../../../../../util/r-base-packages");
@@ -54,9 +59,7 @@ function builtInBehindExport(data, definition) {
54
59
  const attached = definition;
55
60
  return typeof attached.processor === 'function' ? attached : undefined;
56
61
  }
57
- /**
58
- * Marks the given function call node as only calling built-in functions.
59
- */
62
+ /** Marks the given function call node as only calling built-in functions. */
60
63
  function markAsOnlyBuiltIn(graph, rootId, keepEnvironment = false) {
61
64
  const v = graph.getVertex(rootId);
62
65
  if (vertex_1.DfgVertex.isFunctionCall(v)) {
@@ -66,6 +69,16 @@ function markAsOnlyBuiltIn(graph, rootId, keepEnvironment = false) {
66
69
  }
67
70
  }
68
71
  }
72
+ function handsOverFunction(arg, data) {
73
+ const value = arg === r_function_call_1.EmptyArgument ? undefined : arg.value;
74
+ return r_function_definition_1.RFunctionDefinition.is(value) || (r_symbol_1.RSymbol.is(value) && (resolve_helper_1.Resolve.byNameAndType(value.content, data.environment, identifier_1.ReferenceType.Function)?.length ?? 0) > 0);
75
+ }
76
+ /** Whether a replacement function rewrote the value of `definition`, like `body(f) <- quote(runif(1))`, so its body is unknown. */
77
+ function rewrittenByReplacement(definition, data) {
78
+ const at = definition.definedAt;
79
+ const node = definition.type === identifier_1.ReferenceType.Unknown && at !== undefined ? data.completeAst.idMap.get(at) : undefined;
80
+ return r_function_call_1.RFunctionCall.is(node) || (r_binary_op_1.RBinaryOp.is(node) && r_function_call_1.RFunctionCall.is(node.lhs));
81
+ }
69
82
  /**
70
83
  * Processes a named function call within the dataflow analysis.
71
84
  * For example, `myFunction(arg1, arg2)`, resolves against the environment.
@@ -76,43 +89,59 @@ function processNamedCall(name, callArguments, rootId, data) {
76
89
  let defaultProcessor = resolved.length === 0;
77
90
  /* a call whose meaning a later pass looks up in its environment has to keep it */
78
91
  const keepEnvironment = resolved.some(r => r.type === identifier_1.ReferenceType.BuiltInFunction && r.config?.keepEnvironment === true);
79
- /* if this call will be marked as built-in only (see below), its vertex needs no environment snapshot */
92
+ /* a call marked as built-in only (see below) needs no environment snapshot */
80
93
  if (resolved.length > 0 && !keepEnvironment
81
94
  && resolved.every(r => r.type === identifier_1.ReferenceType.BuiltInFunction && typeof r.processor === 'function')
82
95
  && resolved.some(r => r.type === identifier_1.ReferenceType.BuiltInFunction && r.config?.libFn !== true)) {
83
96
  data = { ...data, builtInNoEnv: rootId };
84
97
  }
85
- let information = undefined;
98
+ let information;
86
99
  let builtIn = false;
87
100
  /* a set, because an export and the built-in behind it are two ways to the same definition */
88
101
  const toRun = new Set();
102
+ /* within a function, a call may still resolve to a user function where the function is called, so only the top
103
+ level knows an unresolved call goes to a function we cannot see */
104
+ let unknownBody = resolved.length === 0 && data.environment.level === 0;
89
105
  for (const resolvedFunction of resolved) {
90
106
  const isBuiltIn = resolvedFunction.type === identifier_1.ReferenceType.BuiltInFunction && typeof resolvedFunction.processor === 'function';
91
- /* the call goes through the attached export, so it is not built-in only, but flowR's own
92
- definition of that export is what knows how to process it */
107
+ /* a call through an attached export is not built-in only, but flowR's definition of it processes it */
93
108
  const own = isBuiltIn ? resolvedFunction : builtInBehindExport(data, resolvedFunction);
94
109
  if (own === undefined) {
95
110
  defaultProcessor = true;
111
+ /* a user function's body states what it reads itself */
112
+ unknownBody ||= (0, built_in_ambient_state_1.hidesBody)(resolvedFunction) || rewrittenByReplacement(resolvedFunction, data);
96
113
  continue;
97
114
  }
98
115
  builtIn ||= isBuiltIn && own.config?.libFn !== true;
99
116
  toRun.add(own);
100
117
  }
118
+ /* flowR models what a package function does with its arguments, but not which of its options it reads */
119
+ let packageFn = false;
101
120
  for (const own of toRun) {
102
121
  information = mergeInformation(information, own.processor(name, args, rootId, data));
122
+ const pkg = own.name === undefined ? undefined : identifier_1.Identifier.getNamespace(own.name);
123
+ /* unless flowR states how it accesses the state, as for `withr::with_options` */
124
+ packageFn ||= pkg !== undefined && !(own.config !== undefined && ('ambient' in own.config || 'scope' in own.config)) && !(0, r_base_packages_1.isBaseRPackage)(String(pkg));
103
125
  }
104
126
  if (defaultProcessor) {
105
127
  /* if we do not know where we land, we force! reuse `resolved`, data.environment did not change above */
106
- const call = (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, sig: resolved.length > 0 ? undefined : function_semantics_1.FunctionSemantics.call.signature.every, origin: 'default' });
128
+ const sig = resolved.length > 0 ? undefined : function_semantics_1.FunctionSemantics.call.signature.every;
129
+ const call = (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, sig, origin: 'default' });
107
130
  information = mergeInformation(information, call.information);
108
131
  }
109
132
  else if (information && builtIn) {
110
- // mark the function call as built in only
111
133
  markAsOnlyBuiltIn(information.graph, rootId, keepEnvironment);
112
134
  }
113
- // on demand: materialize the built-in vertex for any package export this call resolves to and, when we
114
- // already know the loading call here, link to it (a function-local export is invisible to the later
115
- // top-level linkMaterializedExportsToLoaders pass, so we must catch it at the call site)
135
+ /* a function whose body we do not see may read any option or draw on the device */
136
+ if (information !== undefined && (unknownBody || packageFn)) {
137
+ /* a function handed in as a parameter may be anything, `runif` included, and a function handed a function may call it;
138
+ otherwise a package function draws only if flowR states it does */
139
+ const draws = resolved.some(r => (0, identifier_1.isReferenceType)(r.type, identifier_1.ReferenceType.Parameter | identifier_1.ReferenceType.Argument) || rewrittenByReplacement(r, data)) || args.some(a => handsOverFunction(a, data));
140
+ information = (0, built_in_ambient_state_1.readsAllAmbientState)(information, rootId, data, unknownBody, draws);
141
+ }
142
+ /* on demand: materialize the built-in vertex for any package export this call resolves to and, when we
143
+ already know the loading call here, link to it (a function-local export is invisible to the later
144
+ top-level linkMaterializedExportsToLoaders pass, so we must catch it at the call site) */
116
145
  if (information) {
117
146
  const sigdb = data.ctx.config.solver.sigdb;
118
147
  for (const r of resolved) {
@@ -121,19 +150,19 @@ function processNamedCall(name, callArguments, rootId, data) {
121
150
  const definedAt = r.definedAt;
122
151
  if (definedAt !== undefined && !node_id_1.NodeId.isBuiltIn(definedAt)) {
123
152
  information.graph.addEdge(r.nodeId, definedAt, edge_1.EdgeType.Reads | edge_1.EdgeType.Calls);
124
- // the call itself reads the library() load that made this export resolvable
153
+ /* the call itself reads the library() load that made this export resolvable */
125
154
  information.graph.addEdge(rootId, definedAt, edge_1.EdgeType.Reads);
126
155
  }
127
- // opt-in: a lightweight ref edge from the call to its sigdb-backed package function vertex
156
+ /* opt-in: a lightweight ref edge from the call to its sigdb-backed package function vertex */
128
157
  if (sigdb.linkPackageCalls && node_id_1.NodeId.toPkgFn(r.nodeId) !== undefined) {
129
158
  information.graph.addEdge(rootId, r.nodeId, edge_1.EdgeType.Reads);
130
159
  }
131
160
  }
132
161
  }
133
162
  const ns = identifier_1.Identifier.getNamespace(name.content);
134
- // a call links to a database-unresolved `library(pkg)` recorded below the global env: `pkg::fn` names
135
- // its package, a bare call takes it from what flowR resolved it to, which is what ties
136
- // `filter(df, id > 2)` to the `library(dplyr)` that made it dplyr's
163
+ /* a call links to a database-unresolved `library(pkg)` recorded below the global env: `pkg::fn` names
164
+ its package, a bare call takes it from what flowR resolved it to, which is what ties
165
+ `filter(df, id > 2)` to the `library(dplyr)` that made it dplyr's */
137
166
  const owners = ns !== undefined ? [ns] : resolved
138
167
  .map(r => r.type === identifier_1.ReferenceType.BuiltInFunction && r.name !== undefined ? identifier_1.Identifier.getNamespace(r.name) : undefined)
139
168
  .filter(assert_1.isNotUndefined);
@@ -142,8 +171,8 @@ function processNamedCall(name, callArguments, rootId, data) {
142
171
  information.graph.addEdge(rootId, loadNode, edge_1.EdgeType.Reads);
143
172
  }
144
173
  }
145
- // opt-in: link a bare base-R call to its sigdb function vertex (base-R qualification is edge-free otherwise);
146
- // same guards as Identifier.toQualified -- not namespaced, not resolving to a user definition
174
+ /* opt-in: link a bare base-R call to its sigdb function vertex (base-R qualification is edge-free otherwise);
175
+ same guards as Identifier.toQualified: not namespaced, not resolving to a user definition */
147
176
  if (sigdb.linkBaseRCalls && ns === undefined && !resolved.some(r => r.nodeId !== undefined && !node_id_1.NodeId.isBuiltIn(r.nodeId))) {
148
177
  const bare = identifier_1.Identifier.getName(name.content);
149
178
  const owner = (0, r_base_packages_1.baseRExportOwner)(bare);
@@ -18,6 +18,8 @@ export declare const Quoted: {
18
18
  readonly capturedBy: (this: void, graph: DataflowGraph, id: NodeId) => readonly NodeId[];
19
19
  /** Every expression the value at `id` may hold, with the call that captured it. Several is normal. */
20
20
  readonly sourcesOf: (this: void, graph: DataflowGraph, id: NodeId) => readonly CapturedExpression[];
21
+ /** What `id` within captured code needs to run at all: the capturing call and every call evaluating it (`eval`, `body(f) <- quote(...)`). */
22
+ readonly evaluationDependencies: (this: void, graph: DataflowGraph, id: NodeId) => NodeId[];
21
23
  /** Links every name in `expr` against `environment` and hands back what stays unresolved. */
22
24
  readonly evaluateIn: typeof linkExpressionIn;
23
25
  /**