@eagleoutice/flowr 2.15.10 → 2.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/README.md +23 -26
  2. package/abstract-interpretation/data-frame/arguments.js +11 -7
  3. package/cli/flowr-main-options.d.ts +15 -0
  4. package/cli/flowr-main-options.js +18 -0
  5. package/cli/flowr.d.ts +18 -0
  6. package/cli/flowr.js +13 -2
  7. package/cli/repl/commands/repl-clipboard.js +2 -1
  8. package/cli/repl/commands/repl-commands.js +2 -0
  9. package/cli/repl/commands/repl-main.d.ts +5 -0
  10. package/cli/repl/commands/repl-signature.js +2 -0
  11. package/cli/repl/core.d.ts +7 -1
  12. package/cli/repl/core.js +17 -8
  13. package/cli/repl/execute.js +2 -0
  14. package/cli/repl/server/connection.js +2 -1
  15. package/cli/repl/server/net.d.ts +10 -0
  16. package/cli/repl/server/net.js +34 -1
  17. package/cli/slicer-app.js +2 -1
  18. package/config.d.ts +12 -2
  19. package/config.js +91 -14
  20. package/control-flow/happens-before.d.ts +5 -0
  21. package/control-flow/happens-before.js +48 -34
  22. package/control-flow/useless-loop.d.ts +1 -1
  23. package/control-flow/useless-loop.js +29 -42
  24. package/dataflow/environments/built-in-props.d.ts +9 -0
  25. package/dataflow/environments/built-in-props.js +25 -19
  26. package/dataflow/environments/built-in.d.ts +2 -2
  27. package/dataflow/environments/built-in.js +41 -77
  28. package/dataflow/environments/default-builtin-config.d.ts +181 -29
  29. package/dataflow/environments/default-builtin-config.js +38 -24
  30. package/dataflow/environments/environment.d.ts +6 -4
  31. package/dataflow/environments/environment.js +54 -121
  32. package/dataflow/environments/identifier.d.ts +4 -2
  33. package/dataflow/environments/identifier.js +23 -69
  34. package/dataflow/environments/reference-to-maybe.d.ts +9 -0
  35. package/dataflow/environments/reference-to-maybe.js +23 -51
  36. package/dataflow/environments/resolve-by-name.js +37 -76
  37. package/dataflow/eval/resolve/resolve-argument.d.ts +3 -1
  38. package/dataflow/eval/resolve/resolve-argument.js +61 -64
  39. package/dataflow/eval/resolve/resolve-working-directory.d.ts +8 -2
  40. package/dataflow/eval/resolve/resolve-working-directory.js +12 -20
  41. package/dataflow/eval/resolve/resolve.d.ts +4 -0
  42. package/dataflow/eval/resolve/resolve.js +23 -14
  43. package/dataflow/extractor.js +185 -59
  44. package/dataflow/fn/class-declaration.d.ts +8 -2
  45. package/dataflow/fn/class-declaration.js +96 -97
  46. package/dataflow/fn/exceptions-of-function.js +68 -37
  47. package/dataflow/fn/frame-reflection.d.ts +12 -4
  48. package/dataflow/fn/frame-reflection.js +64 -42
  49. package/dataflow/fn/function-props.js +15 -33
  50. package/dataflow/fn/function-semantics.d.ts +11 -3
  51. package/dataflow/fn/function-semantics.js +6 -0
  52. package/dataflow/fn/higher-order-function.js +13 -17
  53. package/dataflow/fn/strict-function.d.ts +13 -0
  54. package/dataflow/fn/strict-function.js +285 -122
  55. package/dataflow/graph/call-graph.d.ts +7 -6
  56. package/dataflow/graph/call-graph.js +35 -46
  57. package/dataflow/graph/graph.d.ts +6 -1
  58. package/dataflow/graph/graph.js +57 -66
  59. package/dataflow/info.d.ts +8 -1
  60. package/dataflow/info.js +24 -30
  61. package/dataflow/internal/control-flow.d.ts +42 -4
  62. package/dataflow/internal/control-flow.js +76 -39
  63. package/dataflow/internal/linker.d.ts +15 -9
  64. package/dataflow/internal/linker.js +253 -241
  65. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +80 -14
  66. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +441 -79
  67. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +3 -1
  68. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +51 -48
  69. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +2 -2
  70. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +119 -185
  71. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.js +7 -9
  72. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +1 -1
  73. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +122 -147
  74. package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +55 -25
  75. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +290 -252
  76. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +1 -1
  77. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +30 -32
  78. package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +68 -67
  79. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +72 -126
  80. package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +1 -3
  81. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +10 -10
  82. package/dataflow/internal/process/functions/call/built-in/built-in-quote.d.ts +4 -4
  83. package/dataflow/internal/process/functions/call/built-in/built-in-quote.js +9 -35
  84. package/dataflow/internal/process/functions/call/built-in/built-in-rm.js +8 -3
  85. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.d.ts +1 -1
  86. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.js +12 -28
  87. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.d.ts +1 -1
  88. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.js +40 -118
  89. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.d.ts +1 -3
  90. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.js +16 -24
  91. package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +2 -0
  92. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +93 -102
  93. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.d.ts +7 -3
  94. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +173 -114
  95. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.d.ts +1 -3
  96. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.js +22 -27
  97. package/dataflow/internal/process/functions/call/built-in/built-in-with.d.ts +1 -1
  98. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +34 -9
  99. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +1 -1
  100. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +120 -93
  101. package/dataflow/internal/process/functions/call/common.d.ts +1 -3
  102. package/dataflow/internal/process/functions/call/common.js +36 -76
  103. package/dataflow/internal/process/functions/call/deferred.d.ts +6 -2
  104. package/dataflow/internal/process/functions/call/deferred.js +60 -58
  105. package/dataflow/internal/process/functions/call/known-call-handling.d.ts +4 -2
  106. package/dataflow/internal/process/functions/call/known-call-handling.js +65 -68
  107. package/dataflow/internal/process/functions/call/named-call-handling.d.ts +3 -5
  108. package/dataflow/internal/process/functions/call/named-call-handling.js +44 -21
  109. package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
  110. package/dataflow/internal/process/functions/call/quoted.js +162 -172
  111. package/dataflow/internal/process/functions/call/unnamed-call-handling.js +17 -38
  112. package/dataflow/internal/process/functions/process-parameter.js +25 -32
  113. package/dataflow/internal/process/process-symbol.js +3 -6
  114. package/dataflow/internal/program-order.d.ts +26 -0
  115. package/dataflow/internal/program-order.js +49 -0
  116. package/documentation/doc-capabilities.js +4 -1
  117. package/documentation/wiki-interface.js +4 -0
  118. package/documentation/wiki-setup.js +23 -0
  119. package/engines.js +11 -3
  120. package/linter/linter-executor.js +19 -1
  121. package/linter/linter-rules.d.ts +17 -8
  122. package/linter/rules/absolute-path.js +63 -87
  123. package/linter/rules/file-path-validity.d.ts +4 -2
  124. package/linter/rules/file-path-validity.js +117 -80
  125. package/linter/rules/namespace-access.js +21 -33
  126. package/linter/rules/naming-convention.d.ts +1 -1
  127. package/linter/rules/naming-convention.js +60 -48
  128. package/linter/rules/network-functions.js +11 -7
  129. package/linter/rules/no-leaked-credentials.js +15 -18
  130. package/linter/rules/problematic-inputs.d.ts +2 -1
  131. package/linter/rules/problematic-inputs.js +39 -59
  132. package/linter/rules/roxygen-arguments.d.ts +9 -4
  133. package/linter/rules/roxygen-arguments.js +29 -48
  134. package/linter/rules/seeded-randomness.d.ts +7 -4
  135. package/linter/rules/seeded-randomness.js +130 -89
  136. package/linter/rules/stop-with-call-arg.d.ts +4 -2
  137. package/linter/rules/stop-with-call-arg.js +25 -22
  138. package/linter/rules/unclosed-connection.js +31 -24
  139. package/linter/rules/undefined-symbol.d.ts +4 -6
  140. package/linter/rules/undefined-symbol.js +58 -75
  141. package/linter/rules/unescaped-arguments.js +18 -44
  142. package/linter/rules/unused-definition.d.ts +6 -0
  143. package/linter/rules/unused-definition.js +105 -145
  144. package/linter/rules/unused-import.d.ts +3 -0
  145. package/linter/rules/unused-import.js +25 -43
  146. package/package.json +5 -2
  147. package/project/flowr-analyzer.js +6 -2
  148. package/project/plugins/file-plugins/files/flowr-jupyter-file.d.ts +2 -2
  149. package/project/plugins/file-plugins/files/flowr-jupyter-file.js +8 -12
  150. package/project/plugins/file-plugins/files/flowr-rda-file.d.ts +14 -5
  151. package/project/plugins/file-plugins/files/flowr-rda-file.js +35 -24
  152. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.d.ts +7 -5
  153. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.js +34 -56
  154. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +18 -42
  155. package/project/sigdb/decompress.js +3 -1
  156. package/project/sigdb/sigdb-download.js +9 -6
  157. package/queries/base-query-format.js +1 -2
  158. package/queries/catalog/absint-query/absint-query-format.js +1 -1
  159. package/queries/catalog/config-query/config-query-executor.js +9 -2
  160. package/queries/catalog/dataflow-query/dataflow-query-executor.js +2 -2
  161. package/queries/catalog/dataflow-query/dataflow-query-format.d.ts +2 -0
  162. package/queries/catalog/dataflow-query/dataflow-query-format.js +4 -7
  163. package/queries/catalog/dependencies-query/dependencies-query-executor.js +50 -114
  164. package/queries/catalog/dependencies-query/function-info/library-functions.d.ts +5 -9
  165. package/queries/catalog/dependencies-query/function-info/library-functions.js +6 -0
  166. package/queries/catalog/dependencies-query/function-info/read-functions.js +1 -1
  167. package/queries/catalog/dependencies-query/function-info/write-functions.js +7 -3
  168. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +4 -4
  169. package/queries/catalog/input-sources-query/simple-input-classifier.js +192 -301
  170. package/r-bridge/lang-4.x/ast/model/model.d.ts +3 -1
  171. package/r-bridge/lang-4.x/ast/model/model.js +19 -43
  172. package/r-bridge/lang-4.x/ast/model/nodes/r-access.d.ts +1 -0
  173. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.d.ts +3 -0
  174. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.js +5 -12
  175. package/r-bridge/lang-4.x/ast/model/nodes/r-binary-op.d.ts +1 -0
  176. package/r-bridge/lang-4.x/ast/model/nodes/r-break.d.ts +1 -0
  177. package/r-bridge/lang-4.x/ast/model/nodes/r-comment.d.ts +1 -0
  178. package/r-bridge/lang-4.x/ast/model/nodes/r-expression-list.d.ts +1 -0
  179. package/r-bridge/lang-4.x/ast/model/nodes/r-for-loop.d.ts +1 -0
  180. package/r-bridge/lang-4.x/ast/model/nodes/r-function-call.d.ts +1 -0
  181. package/r-bridge/lang-4.x/ast/model/nodes/r-function-definition.d.ts +1 -0
  182. package/r-bridge/lang-4.x/ast/model/nodes/r-if-then-else.d.ts +1 -0
  183. package/r-bridge/lang-4.x/ast/model/nodes/r-line-directive.d.ts +1 -0
  184. package/r-bridge/lang-4.x/ast/model/nodes/r-logical.d.ts +1 -0
  185. package/r-bridge/lang-4.x/ast/model/nodes/r-next.d.ts +1 -0
  186. package/r-bridge/lang-4.x/ast/model/nodes/r-number.d.ts +1 -0
  187. package/r-bridge/lang-4.x/ast/model/nodes/r-parameter.d.ts +1 -0
  188. package/r-bridge/lang-4.x/ast/model/nodes/r-pipe.d.ts +1 -0
  189. package/r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop.d.ts +1 -0
  190. package/r-bridge/lang-4.x/ast/model/nodes/r-string.d.ts +1 -0
  191. package/r-bridge/lang-4.x/ast/model/nodes/r-symbol.d.ts +1 -0
  192. package/r-bridge/lang-4.x/ast/model/nodes/r-unary-op.d.ts +1 -0
  193. package/r-bridge/lang-4.x/ast/model/nodes/r-while-loop.d.ts +1 -0
  194. package/r-bridge/roxygen2/roxygen-parse.js +28 -42
  195. package/r-bridge/shell-executor.js +2 -0
  196. package/r-bridge/shell.js +8 -0
  197. package/search/search-executor/search-enrichers.js +25 -37
  198. package/slicing/static/slice-call.d.ts +4 -3
  199. package/slicing/static/slice-call.js +56 -88
  200. package/slicing/static/static-slicer.js +120 -79
  201. package/slicing/static/visiting-queue.d.ts +4 -6
  202. package/slicing/static/visiting-queue.js +9 -13
  203. package/util/files.d.ts +6 -0
  204. package/util/files.js +12 -0
  205. package/util/parallel.js +2 -0
  206. package/util/proc.js +2 -1
  207. package/util/r-base-packages.d.ts +2 -0
  208. package/util/r-base-packages.js +23 -13
  209. package/util/secure-mode.d.ts +42 -0
  210. package/util/secure-mode.js +70 -0
  211. package/util/text/playground-link.d.ts +12 -3
  212. package/util/text/playground-link.js +18 -27
  213. package/util/version.js +4 -10
@@ -3,30 +3,49 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PrintingOptions = exports.AmbientStateName = void 0;
4
4
  exports.hidesBody = hidesBody;
5
5
  exports.readsAllAmbientState = readsAllAmbientState;
6
+ exports.ambientWritesOfAssignment = ambientWritesOfAssignment;
7
+ exports.ambientWritesOfRelease = ambientWritesOfRelease;
8
+ exports.makeAmbientWritesMaybe = makeAmbientWritesMaybe;
9
+ exports.ambientWritesBetween = ambientWritesBetween;
10
+ exports.markForcing = markForcing;
11
+ exports.mayForce = mayForce;
12
+ exports.ambientAccessOfSymbol = ambientAccessOfSymbol;
6
13
  exports.readsOfOptionsVariable = readsOfOptionsVariable;
7
14
  exports.applyAmbientStateAccess = applyAmbientStateAccess;
8
15
  exports.applyAmbientStateOfCallee = applyAmbientStateOfCallee;
16
+ exports.writesOfHiddenBuiltIn = writesOfHiddenBuiltIn;
17
+ exports.readsOfHiddenBuiltIn = readsOfHiddenBuiltIn;
9
18
  exports.linkAmbientStateWithinLoop = linkAmbientStateWithinLoop;
10
19
  exports.processAmbientScope = processAmbientScope;
11
20
  exports.evaluatesCapturedCode = evaluatesCapturedCode;
12
21
  exports.readsOfAutoPrint = readsOfAutoPrint;
22
+ exports.autoPrints = autoPrints;
13
23
  exports.applyAmbientStateOfCalledBuiltIns = applyAmbientStateOfCalledBuiltIns;
24
+ exports.errorsOfWarnings = errorsOfWarnings;
14
25
  const info_1 = require("../../../../../info");
15
26
  const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
27
+ const r_argument_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
16
28
  const vertex_1 = require("../../../../../graph/vertex");
17
29
  const built_in_props_1 = require("../../../../../environments/built-in-props");
18
30
  const node_id_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id");
19
31
  const r_expression_list_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-expression-list");
20
32
  const r_string_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string");
21
33
  const identifier_1 = require("../../../../../environments/identifier");
34
+ const environment_1 = require("../../../../../environments/environment");
35
+ const reference_to_maybe_1 = require("../../../../../environments/reference-to-maybe");
22
36
  const define_1 = require("../../../../../environments/define");
23
37
  const model_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/model");
38
+ const r_binary_op_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-binary-op");
39
+ const r_unary_op_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-unary-op");
40
+ const r_function_definition_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
41
+ const r_value_1 = require("../../../../../eval/values/r-value");
24
42
  const match_args_1 = require("../../../../../graph/match-args");
25
43
  const known_call_handling_1 = require("../known-call-handling");
26
44
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
27
45
  const node_value_1 = require("../../../../../eval/resolve/node-value");
28
46
  const unknown_side_effect_1 = require("../../../../../graph/unknown-side-effect");
29
47
  const edge_1 = require("../../../../../graph/edge");
48
+ const assert_1 = require("../../../../../../util/assert");
30
49
  const resolve_helper_1 = require("../../../../../environments/resolve-helper");
31
50
  const r_access_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-access");
32
51
  const r_symbol_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol");
@@ -44,10 +63,32 @@ exports.AmbientStateName = {
44
63
  otherDevicesOpenedBy: '#device:other',
45
64
  /** the color palette, which, other than the graphics parameters, all devices share */
46
65
  palette: '#palette',
66
+ /** the files on disk, which every call writing a file adds to and every call reading a file we cannot name reads */
67
+ files: '#files',
68
+ /** written by a call writing a file we cannot name, which every call reading a file reads */
69
+ anyFile: '#file:*',
70
+ /** the state of the random number generator, which `set.seed` sets and every draw advances */
71
+ rng: '#rng',
72
+ /** the kind of generator, which `set.seed` keeps unless it is given one */
73
+ rngKind: '#rng:kind',
74
+ /** the global variable R keeps that state in, so assigning it sets {@link AmbientStateName.rng} as well */
75
+ rngVariable: '.Random.seed',
47
76
  /** every option write, for readers that may read any option (`getOption(k)` with an unknown `k`, library functions) */
48
77
  allOptions: '#options',
49
78
  /** written by an `options(...)` call whose keys we cannot name, like the common `options(op)` restore */
50
79
  unknownOption: '#option:*',
80
+ /** written along with the option `warn` when it may turn warnings into errors (`warn >= 2`), see {@link errorsOfWarnings} */
81
+ warnsAsErrors: '#warn-error',
82
+ /** the finalizers registered (`reg.finalizer`) and the removals that may release their objects, which `gc()` may run */
83
+ finalizers: '#finalizers',
84
+ /** marks `name` as bound to a text connection opened for writing, whose writes change it */
85
+ textConnection(name) {
86
+ return `#text-connection:${name}`;
87
+ },
88
+ /** the file named `stem` (`data` for `out/data.csv`), so a read of a named file only sees writes that may hit it */
89
+ file(stem) {
90
+ return `#file:${stem}`;
91
+ },
51
92
  /** the pseudo variable holding the option `key`; setting the option replaces it */
52
93
  option(key) {
53
94
  return `#option:${key}`;
@@ -57,22 +98,39 @@ exports.AmbientStateName = {
57
98
  return typeof name === 'string' && name.startsWith('#');
58
99
  }
59
100
  };
60
- const Accumulating = new Set([exports.AmbientStateName.graphics, exports.AmbientStateName.otherDevices, exports.AmbientStateName.device, exports.AmbientStateName.otherDevicesOpenedBy, exports.AmbientStateName.palette, exports.AmbientStateName.allOptions, exports.AmbientStateName.unknownOption]);
101
+ const DeviceStates = [exports.AmbientStateName.graphics, exports.AmbientStateName.otherDevices, exports.AmbientStateName.device, exports.AmbientStateName.otherDevicesOpenedBy];
102
+ const Accumulating = new Set([
103
+ ...DeviceStates, exports.AmbientStateName.palette, exports.AmbientStateName.allOptions, exports.AmbientStateName.unknownOption,
104
+ exports.AmbientStateName.warnsAsErrors, exports.AmbientStateName.files, exports.AmbientStateName.rngKind, exports.AmbientStateName.finalizers
105
+ ]);
106
+ /** a write that may not happen: it reads what was there, so it can still replace the state and keep everything before reachable */
107
+ function maybe(name) {
108
+ return { name, mode: 'maybe' };
109
+ }
110
+ /** what reading the options `keys` reads, every option if the keys are unknown */
111
+ function optionReads(keys) {
112
+ return keys === undefined ? [exports.AmbientStateName.allOptions] : [...keys.map(exports.AmbientStateName.option), exports.AmbientStateName.unknownOption];
113
+ }
61
114
  /** Whether calling `definition` runs a body flowR cannot see: a library function's, or whatever a parameter is bound to. */
62
115
  function hidesBody(definition) {
63
116
  return node_id_1.NodeId.isBuiltIn(definition.nodeId) || (0, identifier_1.isReferenceType)(definition.type, identifier_1.ReferenceType.Parameter | identifier_1.ReferenceType.Argument);
64
117
  }
65
- /** A call into code flowR cannot see may read any option and draw on the device. */
66
- function readsAllAmbientState(res, rootId, data, graphics) {
67
- return withAccess(res, rootId, data, graphics ? [exports.AmbientStateName.allOptions, exports.AmbientStateName.graphics, exports.AmbientStateName.palette] : [exports.AmbientStateName.allOptions], []);
118
+ /** A call into code flowR cannot see may read any option, draw random numbers (with `draws` advancing the generator), and with `graphics` draw on the device. */
119
+ function readsAllAmbientState(res, rootId, data, graphics, draws = true) {
120
+ const reads = [exports.AmbientStateName.allOptions, exports.AmbientStateName.rng];
121
+ if (graphics) {
122
+ reads.push(exports.AmbientStateName.graphics, exports.AmbientStateName.palette);
123
+ }
124
+ return withAccess(res, rootId, data, reads, draws ? [maybe(exports.AmbientStateName.rng)] : []);
68
125
  }
69
126
  /** Adds the reads and writes of the ambient state to what a call does, see {@link AmbientStateName}. */
70
- function withAccess(res, rootId, data, reads, writes) {
127
+ function withAccess(res, rootId, data, reads, writes = []) {
71
128
  if (reads.length === 0 && writes.length === 0) {
72
129
  return res;
73
130
  }
74
131
  const cds = data.cds ? [...data.cds] : undefined;
75
- const readRefs = reads.map(name => ({ nodeId: rootId, name, type: identifier_1.ReferenceType.Variable, cds }));
132
+ const maybeWritten = writes.flatMap(w => typeof w === 'object' && w.mode === 'maybe' ? [w.name] : []);
133
+ const readRefs = [...new Set([...reads, ...maybeWritten])].map(name => ({ nodeId: rootId, name, type: identifier_1.ReferenceType.Variable, cds }));
76
134
  /* the writes stay out of `out`: an assignment reads whatever else its source writes (`x <- y <- 3`) */
77
135
  return {
78
136
  ...res,
@@ -83,16 +141,83 @@ function withAccess(res, rootId, data, reads, writes) {
83
141
  /** Applies the writes of the call `rootId` to the environment, see {@link withAccess}. */
84
142
  function defineAmbient(environment, rootId, cds, writes) {
85
143
  for (const write of writes) {
86
- const name = typeof write === 'string' ? write : write.name;
87
- const mode = typeof write === 'string' ? undefined : write.mode;
88
- const accumulates = Accumulating.has(name);
144
+ const { name, mode } = typeof write === 'string' ? { name: write, mode: undefined } : write;
145
+ const accumulates = Accumulating.has(name) || name.startsWith(exports.AmbientStateName.file(''));
146
+ const keep = accumulates && (mode === undefined || mode === 'maybe');
89
147
  /* an accumulating state only ever grows, its definitions are maybe so merging environments keeps them all; a reset is definite */
90
- const def = { nodeId: rootId, name, type: identifier_1.ReferenceType.Variable, definedAt: rootId, cds: accumulates && mode !== 'reset' ? cds ?? [] : cds };
148
+ const isMaybe = keep || (accumulates && mode !== 'reset');
149
+ const def = {
150
+ nodeId: rootId,
151
+ name,
152
+ type: identifier_1.ReferenceType.Variable,
153
+ definedAt: rootId,
154
+ cds: isMaybe ? cds ?? [] : cds
155
+ };
91
156
  /* the state is global, wherever the call happens, unless the call scopes it to the current frame */
92
- environment = (0, define_1.define)(def, mode !== 'local', environment, accumulates && mode === undefined);
157
+ environment = (0, define_1.define)(def, mode !== 'local', environment, keep);
93
158
  }
94
159
  return environment;
95
160
  }
161
+ /** What an assignment writes beyond its target: only a global `.Random.seed` sets {@link AmbientStateName.rng}. */
162
+ function ambientWritesOfAssignment(environment, target, rootId, cds, global) {
163
+ environment = ambientWritesOfRelease(environment, [target], rootId, cds);
164
+ return global && identifier_1.Identifier.getName(target) === exports.AmbientStateName.rngVariable ? defineAmbient(environment, rootId, cds, [exports.AmbientStateName.rng, exports.AmbientStateName.rngKind]) : environment;
165
+ }
166
+ /** Rebinding or removing the names (all of them if `undefined`) may release an object a finalizer is registered for. */
167
+ function ambientWritesOfRelease(environment, names, rootId, cds) {
168
+ const holders = (resolve_helper_1.Resolve.byName(exports.AmbientStateName.finalizers, environment) ?? []).map(d => d.holder);
169
+ const released = holders.some(h => h === undefined || names === undefined || names.some(n => identifier_1.Identifier.getName(n) === h));
170
+ return released ? defineAmbient(environment, rootId, cds, [exports.AmbientStateName.finalizers]) : environment;
171
+ }
172
+ function* ambientDefinitionsBetween(after, before, skipUnchanged) {
173
+ let unchanged = before.current;
174
+ for (let env = after.current; env !== undefined && !env.builtInEnv; env = env.parent, unchanged = unchanged?.parent) {
175
+ /* a frame the part did not write to shares its memory with the one before */
176
+ if (skipUnchanged && (env === unchanged || env.memory === unchanged?.memory)) {
177
+ continue;
178
+ }
179
+ for (const [name, definitions] of env.memory.entries()) {
180
+ if (!exports.AmbientStateName.is(name)) {
181
+ continue;
182
+ }
183
+ const known = new Set((resolve_helper_1.Resolve.byName(name, before) ?? []).map(d => d.nodeId));
184
+ for (const { nodeId } of definitions) {
185
+ if (!known.has(nodeId)) {
186
+ yield [name, nodeId];
187
+ }
188
+ }
189
+ }
190
+ }
191
+ }
192
+ /** Ambient writes stay out of `out` (see {@link withAccess}), so a part that only maybe runs marks its writes as maybe in `target` here. */
193
+ function makeAmbientWritesMaybe(after, before, target, cds) {
194
+ for (const [name, nodeId] of ambientDefinitionsBetween(after, before, true)) {
195
+ (0, reference_to_maybe_1.applyCdsToDefinitions)(target, name, identifier_1.ReferenceType.Variable, resolve_helper_1.Resolve.byName(name, target) ?? [], nodeId, cds);
196
+ }
197
+ }
198
+ /** The states written in `after` that `before` does not hold, as by a promise (`delayedAssign`) whose writes only happen once a read forces it. */
199
+ function ambientWritesBetween(after, before) {
200
+ const names = new Set();
201
+ for (const [name] of ambientDefinitionsBetween(after, before, false)) {
202
+ names.add(name);
203
+ }
204
+ return [...names];
205
+ }
206
+ /** Notes that a definition binds a promise or a text connection, which reads have to look for from now on. */
207
+ function markForcing(environment) {
208
+ environment_1.REnvironment.findBuiltIn(environment.current).forcing = true;
209
+ }
210
+ /** Whether a definition seen so far binds a promise or a text connection, see {@link markForcing}. */
211
+ function mayForce(environment) {
212
+ return environment_1.REnvironment.findBuiltIn(environment.current).forcing === true;
213
+ }
214
+ /** The state a symbol reads, plus the maybe-writes of forcing a promise bound to it. */
215
+ function ambientAccessOfSymbol(res, symbol, data) {
216
+ const forcing = mayForce(data.environment);
217
+ const definitions = forcing ? resolve_helper_1.Resolve.byName(symbol.content, data.environment) ?? [] : [];
218
+ const forces = definitions.flatMap(d => d.forces ?? []);
219
+ return withAccess(res, symbol.info.id, data, ambientReadsOfSymbol(symbol, data, forcing), forces.map(maybe));
220
+ }
96
221
  /** Binds `name` (globally) to the given definitions, in addition to what it holds with `keep`, so `#graphics` can become what another state holds. */
97
222
  function setAmbient(environment, name, definitions, keep = false) {
98
223
  if (definitions.length === 0 && !keep) {
@@ -103,17 +228,39 @@ function setAmbient(environment, name, definitions, keep = false) {
103
228
  }
104
229
  return environment;
105
230
  }
231
+ /** A variable a text connection writes reads what is written to connections, `.Random.seed` reads the generator state, and `.Options` the options, see {@link optionsVariableReads}. */
232
+ function ambientReadsOfSymbol(symbol, data, forcing) {
233
+ const name = identifier_1.Identifier.getName(symbol.content);
234
+ if (forcing && resolve_helper_1.Resolve.byName(exports.AmbientStateName.textConnection(name), data.environment)?.length) {
235
+ /* what is written to a connection we cannot name may be written to this one */
236
+ return [exports.AmbientStateName.anyFile];
237
+ }
238
+ else if (name === exports.AmbientStateName.rngVariable) {
239
+ return [exports.AmbientStateName.rng, exports.AmbientStateName.rngKind];
240
+ }
241
+ else if (name !== '.Options') {
242
+ return [];
243
+ }
244
+ return optionsVariableReads(symbol, data);
245
+ }
106
246
  /** `.Options$digits` (or `.Options[["digits"]]`) reads that option, anything else done with `.Options` reads all of them. */
107
- function readsOfOptionsVariable(symbol, data) {
108
- const parent = symbol.info.parent === undefined ? undefined : data.completeAst.idMap.get(symbol.info.parent);
109
- let key;
110
- if (r_access_1.RAccess.is(parent) && parent.accessed.info.id === symbol.info.id && (parent.operator === '$' || parent.operator === '[[')) {
111
- const first = parent.access[0];
112
- const value = first === r_function_call_1.EmptyArgument ? undefined : first.value;
113
- key = r_string_1.RString.is(value) ? value.content.str : parent.operator === '$' && r_symbol_1.RSymbol.is(value) ? identifier_1.Identifier.getName(value.content) : undefined;
247
+ function optionsVariableReads(symbol, data) {
248
+ const parent = model_1.RNode.directParent(symbol, data.completeAst.idMap);
249
+ if (!r_access_1.RAccess.is(parent) || parent.accessed.info.id !== symbol.info.id || (parent.operator !== '$' && parent.operator !== '[[')) {
250
+ return optionReads(undefined);
114
251
  }
115
- const names = key === undefined ? [exports.AmbientStateName.allOptions] : [exports.AmbientStateName.option(key), exports.AmbientStateName.unknownOption];
116
- return names.map(name => ({ nodeId: symbol.info.id, name, type: identifier_1.ReferenceType.Variable, cds: data.cds }));
252
+ const value = r_argument_1.RArgument.valueOf(parent.access[0]);
253
+ if (r_string_1.RString.is(value)) {
254
+ return optionReads([value.content.str]);
255
+ }
256
+ else if (parent.operator === '$' && r_symbol_1.RSymbol.is(value)) {
257
+ return optionReads([identifier_1.Identifier.getName(value.content)]);
258
+ }
259
+ return optionReads(undefined);
260
+ }
261
+ /** {@link optionsVariableReads} as references. */
262
+ function readsOfOptionsVariable(symbol, data) {
263
+ return optionsVariableReads(symbol, data).map(name => ({ nodeId: symbol.info.id, name, type: identifier_1.ReferenceType.Variable, cds: data.cds }));
117
264
  }
118
265
  /** Options affecting calls we cannot name (`scipen` changes every number-to-string conversion), so setting one stays an unknown side effect. */
119
266
  const GlobalEffectOptions = new Set([
@@ -126,7 +273,10 @@ const GlobalEffectOptions = new Set([
126
273
  exports.PrintingOptions = ['digits', 'width', 'max.print', 'digits.secs', 'useFancyQuotes'];
127
274
  /** Wires the {@link AmbientStateAccess} of a call into its dataflow information: reads become unknown references, writes define the pseudo variable globally. */
128
275
  function applyAmbientStateAccess(res, args, rootId, data, access) {
129
- if (args === UnknownArguments) {
276
+ if (!('type' in access)) {
277
+ return access.reduce((r, a) => applyAmbientStateAccess(r, args, rootId, data, a), res);
278
+ }
279
+ else if (args === UnknownArguments) {
130
280
  return applyWithUnknownArguments(res, rootId, data, access);
131
281
  }
132
282
  const reads = [];
@@ -154,19 +304,13 @@ function applyAmbientStateAccess(res, args, rootId, data, access) {
154
304
  }
155
305
  break;
156
306
  case "read-options" /* AmbientStateAccessType.ReadOptions */: {
157
- const dispatched = access.dispatchOn === undefined ? undefined : args[access.dispatchOn];
158
- reads.push(...printingReads(dispatched === undefined || dispatched === r_function_call_1.EmptyArgument ? undefined : dispatched.value, access.dispatchOn !== undefined, access.keys, data));
307
+ const dispatched = access.dispatchOn === undefined ? undefined : r_argument_1.RArgument.valueOf(args[access.dispatchOn]);
308
+ reads.push(...printingReads(dispatched, access.dispatchOn !== undefined, access.keys, data));
159
309
  break;
160
310
  }
161
311
  case "get-option" /* AmbientStateAccessType.GetOption */: {
162
312
  const first = args.find(a => a !== r_function_call_1.EmptyArgument && (a.name === undefined || identifier_1.Identifier.getName(a.name.content) === 'x'));
163
- const keys = first === undefined || first === r_function_call_1.EmptyArgument ? undefined : stringsOf(first.value, data);
164
- if (keys === undefined) {
165
- reads.push(exports.AmbientStateName.allOptions);
166
- }
167
- else {
168
- reads.push(...keys.map(exports.AmbientStateName.option), exports.AmbientStateName.unknownOption);
169
- }
313
+ reads.push(...optionReads(first === undefined || first === r_function_call_1.EmptyArgument ? undefined : stringsOf(first.value, data)));
170
314
  break;
171
315
  }
172
316
  case "set-options" /* AmbientStateAccessType.SetOptions */:
@@ -180,15 +324,110 @@ function applyAmbientStateAccess(res, args, rootId, data, access) {
180
324
  return deviceAccess(res, rootId, data, 'switch');
181
325
  case "evaluate-quoted" /* AmbientStateAccessType.EvaluateQuoted */:
182
326
  return evaluatesCapturedCode(res, rootId, data);
327
+ case "set-rng" /* AmbientStateAccessType.SetRng */: {
328
+ const kindFrom = access.kindFrom ?? 1;
329
+ reads.push(exports.AmbientStateName.rngKind);
330
+ if (kindFrom > 0) {
331
+ writes.push(exports.AmbientStateName.rng);
332
+ }
333
+ if (args.some((a, i) => a !== r_function_call_1.EmptyArgument && (a.name === undefined ? i >= kindFrom : identifier_1.Identifier.getName(a.name.content) !== 'seed'))) {
334
+ writes.push(exports.AmbientStateName.rngKind);
335
+ }
336
+ break;
337
+ }
338
+ case "draw-rng" /* AmbientStateAccessType.DrawRng */:
339
+ reads.push(exports.AmbientStateName.rng);
340
+ writes.push(exports.AmbientStateName.rng);
341
+ break;
342
+ case "read-files" /* AmbientStateAccessType.ReadFiles */: {
343
+ const stems = fileStems(res, rootId, data, access.sig);
344
+ reads.push(...stems === undefined ? [exports.AmbientStateName.files] : [...stems.map(exports.AmbientStateName.file), exports.AmbientStateName.anyFile]);
345
+ break;
346
+ }
347
+ case "write-files" /* AmbientStateAccessType.WriteFiles */: {
348
+ const stems = fileStems(res, rootId, data, access.sig);
349
+ writes.push(...stems === undefined ? [exports.AmbientStateName.anyFile] : stems.map(exports.AmbientStateName.file), exports.AmbientStateName.files);
350
+ break;
351
+ }
352
+ case "open-text-connection" /* AmbientStateAccessType.OpenTextConnection */:
353
+ return openTextConnection(res, args, rootId, data);
354
+ case "register-finalizer" /* AmbientStateAccessType.RegisterFinalizer */:
355
+ return registerFinalizer(res, args, rootId, data);
356
+ case "run-finalizers" /* AmbientStateAccessType.RunFinalizers */: {
357
+ const registered = (resolve_helper_1.Resolve.byName(exports.AmbientStateName.finalizers, data.environment) ?? []);
358
+ for (const fn of registered.flatMap(d => d.value ?? [])) {
359
+ res.graph.addEdge(rootId, fn, edge_1.EdgeType.Calls);
360
+ }
361
+ return withAccess(res, rootId, data, [exports.AmbientStateName.finalizers], registered.flatMap(d => d.writes ?? []).map(maybe));
362
+ }
183
363
  }
184
364
  return withAccess(res, rootId, data, reads, writes);
185
365
  }
366
+ /** `reg.finalizer(e, f)` keeps the functions `f` may be, and the name holding `e`, whose release lets `gc()` run them. */
367
+ function registerFinalizer(res, args, rootId, data) {
368
+ const bound = match_args_1.MatchArgs.toNames(args, ['e', 'f', 'onexit']);
369
+ const fn = bound.get('f')?.value;
370
+ const holder = bound.get('e')?.value;
371
+ const value = r_function_definition_1.RFunctionDefinition.is(fn) ? [fn.info.id] : r_symbol_1.RSymbol.is(fn) ? (resolve_helper_1.Resolve.byName(fn.content, data.environment) ?? []).flatMap(d => d.value ?? []) : [];
372
+ const writes = value.flatMap(id => {
373
+ const definition = res.graph.getVertex(id);
374
+ return vertex_1.DfgVertex.isFunctionDefinition(definition) ? ambientWritesBetween(definition.subflow.environment, data.environment) : [];
375
+ });
376
+ const definition = { nodeId: rootId, name: exports.AmbientStateName.finalizers, type: identifier_1.ReferenceType.Variable, definedAt: rootId, cds: data.cds ? [...data.cds] : [], value, writes, holder: r_symbol_1.RSymbol.is(holder) ? identifier_1.Identifier.getName(holder.content) : undefined };
377
+ return { ...res, environment: (0, define_1.define)(definition, true, res.environment, true) };
378
+ }
379
+ /** `textConnection(object, open, local)` opened for writing binds `object` globally, or locally with `local = TRUE`. */
380
+ function openTextConnection(res, args, rootId, data) {
381
+ const bound = match_args_1.MatchArgs.toNames(args, ['object', 'open', 'local', 'name', 'encoding']);
382
+ const open = bound.get('open');
383
+ const modes = open === undefined ? ['r'] : stringsOf(open, data);
384
+ if (modes?.every(m => m.startsWith('r'))) {
385
+ return res;
386
+ }
387
+ const names = stringsOf(bound.get('object'), data);
388
+ if (names === undefined) {
389
+ (0, unknown_side_effect_1.handleUnknownSideEffect)(res.graph, res.environment, rootId);
390
+ return res;
391
+ }
392
+ const local = bound.get('local');
393
+ const scopes = local === undefined ? [false] : node_value_1.NodeValue.setOf(local.info.id, data)?.elements.map(v => v.type === 'logical' && v.value === true);
394
+ const cds = data.cds ? [...data.cds] : undefined;
395
+ let environment = res.environment;
396
+ for (const name of names) {
397
+ for (const [i, inFrame] of (scopes ?? [true, false]).entries()) {
398
+ environment = (0, define_1.define)({ nodeId: rootId, name, type: identifier_1.ReferenceType.Variable, definedAt: rootId, cds }, !inFrame, environment, i > 0 || names.length > 1);
399
+ }
400
+ }
401
+ markForcing(environment);
402
+ return withAccess({ ...res, environment }, rootId, data, [], names.map(exports.AmbientStateName.textConnection));
403
+ }
404
+ /** The stems of the files the call names, `undefined` unless every file argument is a known path to a file with an extension (`plot%03d.png` or a directory may stand for others). */
405
+ function fileStems(res, rootId, data, sig) {
406
+ const call = res.graph.getVertex(rootId);
407
+ if (sig === undefined || !vertex_1.DfgVertex.isFunctionCall(call)) {
408
+ return undefined;
409
+ }
410
+ const named = match_args_1.MatchArgs.findWithProps(call.args, sig, built_in_props_1.ArgProp.Resource);
411
+ if (named.length === 0 || named.length < sig.filter(([n, p]) => n !== '...' && (p & built_in_props_1.ArgProp.Resource) !== 0).length) {
412
+ return undefined;
413
+ }
414
+ const stems = [];
415
+ for (const id of named) {
416
+ const paths = node_value_1.NodeValue.stringsOf(id, data);
417
+ for (const path of paths?.length ? paths : ['']) {
418
+ const [stem, ...extension] = path.split(/[/\\]/).at(-1)?.split('.') ?? [];
419
+ if (!stem || extension.length === 0 || /[%*?[]/.test(path)) {
420
+ return undefined;
421
+ }
422
+ stems.push(stem);
423
+ }
424
+ }
425
+ return stems;
426
+ }
186
427
  /** What printing `value` reads: the printing `keys` for a plain value, all options for an unknown one (a tibble reads the pillar options). */
187
428
  function printingReads(value, dispatches, keys, data) {
188
- if (dispatches && (value === undefined || !isPlainValue(value, data))) {
189
- return [exports.AmbientStateName.allOptions];
190
- }
191
- return [...keys.map(exports.AmbientStateName.option), exports.AmbientStateName.unknownOption];
429
+ const anyOption = dispatches && (value === undefined || !isPlainValue(value, data));
430
+ return optionReads(anyOption ? undefined : keys);
192
431
  }
193
432
  const PlainValueTypes = new Set(['number', 'string', 'logical', 'interval']);
194
433
  /** whether the value at `node` is one of the base types whose printing no package method changes */
@@ -203,23 +442,44 @@ function isPlainValue(node, data) {
203
442
  const UnknownArguments = Symbol('unknown-arguments');
204
443
  function applyWithUnknownArguments(res, rootId, data, access) {
205
444
  switch (access.type) {
206
- case "set-options" /* AmbientStateAccessType.SetOptions */:
207
- /* like `options(op)`: it may set anything */
208
- res.graph.markIdForUnknownSideEffects(rootId);
209
- return withAccess(res, rootId, data, [exports.AmbientStateName.allOptions], [exports.AmbientStateName.unknownOption, exports.AmbientStateName.allOptions]);
210
- case "get-option" /* AmbientStateAccessType.GetOption */:
211
- return withAccess(res, rootId, data, [exports.AmbientStateName.allOptions], []);
212
- case "set-graphics" /* AmbientStateAccessType.SetGraphics */:
213
- return withAccess(res, rootId, data, [exports.AmbientStateName.graphics], [exports.AmbientStateName.graphics]);
214
- case "set-palette" /* AmbientStateAccessType.SetPalette */:
215
- return withAccess(res, rootId, data, [exports.AmbientStateName.palette], [exports.AmbientStateName.palette]);
216
- case "read-options" /* AmbientStateAccessType.ReadOptions */:
217
- return withAccess(res, rootId, data, access.dispatchOn === undefined ? printingReads(undefined, false, access.keys, data) : [exports.AmbientStateName.allOptions], []);
218
445
  case "close-device" /* AmbientStateAccessType.CloseDevice */:
219
446
  return deviceAccess(res, rootId, data, access.all ? 'close-all' : 'switch');
220
- default:
447
+ case "open-device" /* AmbientStateAccessType.OpenDevice */:
448
+ case "switch-device" /* AmbientStateAccessType.SwitchDevice */:
221
449
  return applyAmbientStateAccess(res, [], rootId, data, access);
450
+ case "set-options" /* AmbientStateAccessType.SetOptions */:
451
+ /* like `options(op)`: it may set anything */
452
+ res.graph.markIdForUnknownSideEffects(rootId);
222
453
  }
454
+ const { reads, writes } = unknownArgumentAccess(access);
455
+ return withAccess(res, rootId, data, reads, writes);
456
+ }
457
+ const Devices = { reads: [], writes: [exports.AmbientStateName.graphics, exports.AmbientStateName.device] };
458
+ const Quoted = { reads: [exports.AmbientStateName.allOptions, exports.AmbientStateName.graphics, exports.AmbientStateName.palette], writes: [exports.AmbientStateName.unknownOption, exports.AmbientStateName.allOptions, exports.AmbientStateName.graphics, exports.AmbientStateName.palette] };
459
+ /** What a call of the given kind may read and write when its arguments are unknown, as when only linking reveals it. */
460
+ const UnknownArgumentAccess = {
461
+ ["set-options" /* AmbientStateAccessType.SetOptions */]: { reads: [exports.AmbientStateName.allOptions], writes: [exports.AmbientStateName.unknownOption, exports.AmbientStateName.allOptions, exports.AmbientStateName.warnsAsErrors] },
462
+ ["get-option" /* AmbientStateAccessType.GetOption */]: { reads: [exports.AmbientStateName.allOptions], writes: [] },
463
+ ["set-graphics" /* AmbientStateAccessType.SetGraphics */]: { reads: [exports.AmbientStateName.graphics], writes: [exports.AmbientStateName.graphics] },
464
+ ["read-graphics" /* AmbientStateAccessType.ReadGraphics */]: { reads: [exports.AmbientStateName.graphics, exports.AmbientStateName.palette], writes: [] },
465
+ ["set-palette" /* AmbientStateAccessType.SetPalette */]: { reads: [exports.AmbientStateName.palette], writes: [exports.AmbientStateName.palette] },
466
+ ["open-device" /* AmbientStateAccessType.OpenDevice */]: Devices,
467
+ ["close-device" /* AmbientStateAccessType.CloseDevice */]: Devices,
468
+ ["switch-device" /* AmbientStateAccessType.SwitchDevice */]: Devices,
469
+ ["evaluate-quoted" /* AmbientStateAccessType.EvaluateQuoted */]: Quoted,
470
+ ["set-rng" /* AmbientStateAccessType.SetRng */]: { reads: [exports.AmbientStateName.rngKind], writes: [exports.AmbientStateName.rng, exports.AmbientStateName.rngKind] },
471
+ ["draw-rng" /* AmbientStateAccessType.DrawRng */]: { reads: [exports.AmbientStateName.rng], writes: [exports.AmbientStateName.rng] },
472
+ ["read-files" /* AmbientStateAccessType.ReadFiles */]: { reads: [exports.AmbientStateName.files], writes: [] },
473
+ ["write-files" /* AmbientStateAccessType.WriteFiles */]: { reads: [], writes: [exports.AmbientStateName.anyFile, exports.AmbientStateName.files] },
474
+ ["open-text-connection" /* AmbientStateAccessType.OpenTextConnection */]: { reads: [], writes: [] },
475
+ ["register-finalizer" /* AmbientStateAccessType.RegisterFinalizer */]: { reads: [], writes: [exports.AmbientStateName.finalizers] },
476
+ ["run-finalizers" /* AmbientStateAccessType.RunFinalizers */]: { reads: [exports.AmbientStateName.finalizers], writes: [] }
477
+ };
478
+ function unknownArgumentAccess(access) {
479
+ if (access.type !== "read-options" /* AmbientStateAccessType.ReadOptions */) {
480
+ return UnknownArgumentAccess[access.type];
481
+ }
482
+ return { reads: optionReads(access.dispatchOn === undefined ? access.keys : undefined), writes: [] };
223
483
  }
224
484
  /** Graphics parameters are per device; within a function the open devices are unknown, so a device call only adds. */
225
485
  function deviceAccess(res, rootId, data, kind) {
@@ -256,7 +516,7 @@ function deviceAccess(res, rootId, data, kind) {
256
516
  break;
257
517
  case 'close-all':
258
518
  closes([...opener, ...otherOpeners]);
259
- for (const name of [exports.AmbientStateName.graphics, exports.AmbientStateName.otherDevices, exports.AmbientStateName.device, exports.AmbientStateName.otherDevicesOpenedBy]) {
519
+ for (const name of DeviceStates) {
260
520
  environment = setAmbient(environment, name, []);
261
521
  }
262
522
  break;
@@ -273,23 +533,26 @@ function deviceAccess(res, rootId, data, kind) {
273
533
  /** A call to `fn` made on our behalf (`do.call(fn, args)`, `sapply(x, fn)`): it accesses the state as `fn` does, or reads all of it if `fn` is opaque. */
274
534
  function applyAmbientStateOfCallee(res, callId, fn, data) {
275
535
  const targets = resolve_helper_1.Resolve.byNameAndType(fn, data.environment, identifier_1.ReferenceType.Function) ?? [];
276
- res = applyAmbientStateOfTargets(res, targets, UnknownArguments, callId, data);
536
+ res = applyAmbientStateAccess(res, UnknownArguments, callId, data, targets.flatMap(hiddenAccess));
277
537
  const unknown = (targets.length === 0 && data.environment.level === 0) || targets.some(t => t.type !== identifier_1.ReferenceType.BuiltInFunction && hidesBody(t));
278
538
  return unknown ? readsAllAmbientState(res, callId, data, true) : res;
279
539
  }
280
- /** applies what the built-ins among `targets` state about the ambient state */
281
- function applyAmbientStateOfTargets(res, targets, args, callId, data) {
282
- for (const target of targets) {
283
- const access = target.type === identifier_1.ReferenceType.BuiltInFunction ? target.config?.ambient : undefined;
284
- if (access !== undefined) {
285
- res = applyAmbientStateAccess(res, args, callId, data, access);
286
- }
287
- }
288
- return res;
540
+ function hiddenAccess(target) {
541
+ const access = target.type === identifier_1.ReferenceType.BuiltInFunction ? target.config?.ambient : undefined;
542
+ return [access ?? []].flat();
543
+ }
544
+ /** The (maybe) writes of the call `callId` to the built-in `target` through a parameter, like `g <- function(f, ...) f(...); g(options, digits = 3)`. */
545
+ function writesOfHiddenBuiltIn(target, callId) {
546
+ return hiddenAccess(target).flatMap(a => unknownArgumentAccess(a).writes).map(name => ({ nodeId: callId, name, type: identifier_1.ReferenceType.Variable, definedAt: callId, cds: [] }));
547
+ }
548
+ /** The states the call to the built-in `target` through a parameter reads, see {@link writesOfHiddenBuiltIn}. */
549
+ function readsOfHiddenBuiltIn(target) {
550
+ return hiddenAccess(target).flatMap(a => unknownArgumentAccess(a).reads);
289
551
  }
290
552
  function setOptions(res, args, rootId, data) {
291
553
  const queried = [];
292
554
  const written = [];
555
+ let warnsAsErrors = false;
293
556
  let global = false;
294
557
  let unknownKeys = false;
295
558
  for (const arg of args) {
@@ -300,6 +563,7 @@ function setOptions(res, args, rootId, data) {
300
563
  const key = identifier_1.Identifier.getName(arg.name.content);
301
564
  written.push(exports.AmbientStateName.option(key));
302
565
  global ||= GlobalEffectOptions.has(key);
566
+ warnsAsErrors ||= key === 'warn' && !node_value_1.NodeValue.setOf(arg.value?.info.id, data)?.elements.every(belowTwo);
303
567
  }
304
568
  else if (r_string_1.RString.is(arg.value)) {
305
569
  queried.push(arg.value.content.str);
@@ -326,6 +590,9 @@ function setOptions(res, args, rootId, data) {
326
590
  if (unknownKeys) {
327
591
  writes.push(exports.AmbientStateName.unknownOption);
328
592
  }
593
+ if (warnsAsErrors) {
594
+ writes.push(exports.AmbientStateName.warnsAsErrors);
595
+ }
329
596
  if (writes.length > 0) {
330
597
  writes.push(exports.AmbientStateName.allOptions);
331
598
  }
@@ -348,26 +615,25 @@ function stringsOf(value, data) {
348
615
  }
349
616
  /** Whether the call's value may be used: anything but a non-final statement or the script's last statement (the last of a block is its value). */
350
617
  function resultIsUsed(rootId, data) {
351
- const parentId = data.completeAst.idMap.get(rootId)?.info.parent;
352
- const parent = parentId === undefined ? undefined : data.completeAst.idMap.get(parentId);
353
- if (parent === undefined || !r_expression_list_1.RExpressionList.is(parent)) {
618
+ const node = data.completeAst.idMap.get(rootId);
619
+ const parent = node === undefined ? undefined : model_1.RNode.directParent(node, data.completeAst.idMap);
620
+ if (!r_expression_list_1.RExpressionList.is(parent)) {
354
621
  return parent !== undefined;
355
622
  }
356
623
  const isLast = parent.children.at(-1)?.info.id === rootId;
357
624
  return isLast && !data.completeAst.ast.files.some(f => f.root.info.id === parent.info.id);
358
625
  }
359
- /** Links state reads in a loop body to the body's own writes of a later iteration; the loops' circular linking only sees uses. */
626
+ /** Links state reads in a loop body to the body's own writes of a later iteration and returns them, as the first iteration reads what is there before the loop. */
360
627
  function linkAmbientStateWithinLoop(graph, reads, body) {
361
- for (const r of reads) {
362
- if (!exports.AmbientStateName.is(r.name)) {
363
- continue;
364
- }
628
+ const ambient = reads.filter(r => exports.AmbientStateName.is(r.name));
629
+ for (const r of ambient) {
365
630
  for (const def of resolve_helper_1.Resolve.byName(r.name, body.environment) ?? []) {
366
631
  if (body.graph.hasVertex(def.nodeId)) {
367
632
  graph.addEdge(r.nodeId, def.nodeId, edge_1.EdgeType.Reads);
368
633
  }
369
634
  }
370
635
  }
636
+ return ambient;
371
637
  }
372
638
  /** The processor for an {@link AmbientScope}. */
373
639
  function processAmbientScope(name, args, rootId, data, scope) {
@@ -391,12 +657,13 @@ function processAmbientScope(name, args, rootId, data, scope) {
391
657
  }
392
658
  /* afterward, what the scope changed is back to what it was before */
393
659
  let environment = information.environment;
394
- const restored = scope.state === 'device' ? [exports.AmbientStateName.graphics, exports.AmbientStateName.otherDevices, exports.AmbientStateName.device, exports.AmbientStateName.otherDevicesOpenedBy]
395
- : writes.map(w => typeof w === 'string' ? w : w.name).filter(n => n.startsWith('#option:') && n !== exports.AmbientStateName.unknownOption);
660
+ const restored = scope.state === 'device' ? DeviceStates
661
+ : scope.state === 'rng' ? [exports.AmbientStateName.rng]
662
+ : writes.map(w => typeof w === 'string' ? w : w.name).filter(n => n.startsWith('#option:') && n !== exports.AmbientStateName.unknownOption);
396
663
  for (const state of restored) {
397
664
  const before = resolve_helper_1.Resolve.byName(state, data.environment);
398
665
  /* an option nothing set before keeps the scope's definition, we cannot tell R's default apart */
399
- if ((before !== undefined && before.length > 0) || (scope.state === 'device' && data.environment.level === 0)) {
666
+ if ((before !== undefined && before.length > 0) || ((scope.state === 'device' || scope.state === 'rng') && data.environment.level === 0)) {
400
667
  environment = setAmbient(environment, state, before ?? []);
401
668
  }
402
669
  }
@@ -410,7 +677,7 @@ function processAmbientScope(name, args, rootId, data, scope) {
410
677
  /** what a scope call writes: the options it names, or the graphics state */
411
678
  function scopedWrites(scope, args, bound, local) {
412
679
  if (scope.state !== 'options') {
413
- return { writes: [exports.AmbientStateName.graphics], unknown: false };
680
+ return { writes: [scope.state === 'rng' ? exports.AmbientStateName.rng : exports.AmbientStateName.graphics], unknown: false };
414
681
  }
415
682
  const keys = [];
416
683
  let unknown = false;
@@ -448,7 +715,9 @@ function scopedWrites(scope, args, bound, local) {
448
715
  fromList(arg.value);
449
716
  }
450
717
  }
451
- const writes = keys.map(k => local ? { name: exports.AmbientStateName.option(k), mode: 'local' } : exports.AmbientStateName.option(k));
718
+ /* we do not resolve the value a scope sets, so setting `warn` may turn warnings into errors */
719
+ const names = keys.map(exports.AmbientStateName.option).concat(keys.includes('warn') ? [exports.AmbientStateName.warnsAsErrors] : []);
720
+ const writes = names.map(name => local ? { name, mode: 'local' } : name);
452
721
  if (unknown) {
453
722
  writes.push(exports.AmbientStateName.unknownOption);
454
723
  }
@@ -457,14 +726,14 @@ function scopedWrites(scope, args, bound, local) {
457
726
  }
458
727
  /** Evaluating captured code may access any state; `Quoted.finalize` drops the writes again once the code is known to change none. */
459
728
  function evaluatesCapturedCode(res, rootId, data) {
460
- return withAccess(res, rootId, data, [exports.AmbientStateName.allOptions, exports.AmbientStateName.graphics, exports.AmbientStateName.palette], [exports.AmbientStateName.unknownOption, exports.AmbientStateName.allOptions, exports.AmbientStateName.graphics, exports.AmbientStateName.palette]);
729
+ return withAccess(res, rootId, data, Quoted.reads, Quoted.writes);
461
730
  }
462
- /** R prints every visible top-level value, which reads the printing options like `print` does. */
731
+ /** R prints every visible top-level value (see {@link autoPrints}) by an implicit `print`, which reads the options the configured `print` reads for that value. */
463
732
  function readsOfAutoPrint(processed, expression, data) {
464
- if (!autoPrints(expression, processed, data)) {
465
- return processed;
466
- }
467
- return withAccess(processed, expression.info.id, data, printingReads(expression, true, exports.PrintingOptions, data), []);
733
+ const print = resolve_helper_1.Resolve.byNameAndType('print', { current: environment_1.REnvironment.findBuiltIn(data.environment.current), level: 0 }, identifier_1.ReferenceType.Function)?.slice(0, 1) ?? [];
734
+ const reads = print.flatMap(hiddenAccess).flatMap(access => access.type === "read-options" /* AmbientStateAccessType.ReadOptions */
735
+ ? printingReads(expression, access.dispatchOn !== undefined, access.keys, data) : []);
736
+ return withAccess(processed, expression.info.id, data, reads, []);
468
737
  }
469
738
  /** the processors whose calls return invisibly, although their configuration states no {@link CallProp.Invisible} */
470
739
  const InvisibleProcessors = new Set([built_in_proc_name_1.BuiltInProcName.ForLoop, built_in_proc_name_1.BuiltInProcName.WhileLoop, built_in_proc_name_1.BuiltInProcName.RepeatLoop,
@@ -493,8 +762,8 @@ function applyAmbientStateOfCalledBuiltIns(res, callId, builtIns, before, graph,
493
762
  }
494
763
  const known = res.unknownReferences.length;
495
764
  for (const builtIn of builtIns) {
496
- const targets = node_id_1.NodeId.isBuiltIn(builtIn) ? resolve_helper_1.Resolve.byNameAndType(node_id_1.NodeId.fromBuiltIn(builtIn), before, identifier_1.ReferenceType.Function) : undefined;
497
- res = applyAmbientStateOfTargets(res, targets ?? [], call.arguments, callId, data);
765
+ const targets = node_id_1.NodeId.isBuiltIn(builtIn) ? resolve_helper_1.Resolve.byNameAndType(node_id_1.NodeId.fromBuiltIn(builtIn), before, identifier_1.ReferenceType.Function) ?? [] : [];
766
+ res = applyAmbientStateAccess(res, call.arguments, callId, data, targets.flatMap(hiddenAccess));
498
767
  }
499
768
  for (const read of res.unknownReferences.slice(known)) {
500
769
  for (const def of resolve_helper_1.Resolve.byName(read.name, before) ?? []) {
@@ -503,4 +772,97 @@ function applyAmbientStateOfCalledBuiltIns(res, callId, builtIns, before, graph,
503
772
  }
504
773
  return res;
505
774
  }
775
+ /** whether R reads the value as a `warn` level below 2, so warnings stay warnings */
776
+ function belowTwo(value) {
777
+ return value.type === 'logical' || (r_value_1.RValue.numberOf(value) ?? 2) < 2;
778
+ }
779
+ /** With `warn >= 2` R turns warnings into errors, so whatever may warn in `block` is a maybe error exit reading the options that may set `warn`. */
780
+ function errorsOfWarnings(block, after, graph, data) {
781
+ const lookup = (name) => [...resolve_helper_1.Resolve.byName(name, data.environment) ?? [], ...resolve_helper_1.Resolve.byName(name, after) ?? []];
782
+ const marked = new Set(lookup(exports.AmbientStateName.warnsAsErrors).map(d => d.nodeId));
783
+ const sources = new Set([...lookup(exports.AmbientStateName.option('warn')).filter(d => marked.has(d.nodeId)), ...lookup(exports.AmbientStateName.unknownOption)].map(d => d.nodeId));
784
+ if (sources.size === 0) {
785
+ return [];
786
+ }
787
+ const cds = data.cds ? [...data.cds] : [];
788
+ return warningSites(block, graph, data, new Map()).map(site => {
789
+ for (const source of sources) {
790
+ graph.addEdge(site, source, edge_1.EdgeType.Reads);
791
+ }
792
+ return { type: 4 /* ExitPointType.Error */, nodeId: site, cds };
793
+ });
794
+ }
795
+ /** what may warn when `root` runs, `bodies` holds what may in the bodies of the closures seen so far */
796
+ function warningSites(root, graph, data, bodies) {
797
+ const sites = [];
798
+ model_1.RNode.visitAst(root, node => {
799
+ if (r_function_definition_1.RFunctionDefinition.is(node)) {
800
+ return true;
801
+ }
802
+ else if (r_binary_op_1.RBinaryOp.is(node) ? r_binary_op_1.RBinaryOp.getOperatorInfo(node)?.usedAs !== 'assignment' : r_unary_op_1.RUnaryOp.is(node) || r_access_1.RAccess.is(node)) {
803
+ sites.push(node.info.id);
804
+ }
805
+ else if (r_function_call_1.RFunctionCall.is(node)) {
806
+ const targets = node.named ? resolve_helper_1.Resolve.byNameAndType(node.functionName.content, data.environment, identifier_1.ReferenceType.Function) ?? [] : [];
807
+ if (node.named && identifier_1.Identifier.getName(node.functionName.content) === 'suppressWarnings' && targets.every(t => node_id_1.NodeId.isBuiltIn(t.nodeId))) {
808
+ return true;
809
+ }
810
+ const handlers = muffledHandlers(node, graph, data);
811
+ if (handlers !== undefined) {
812
+ sites.push(...handlers.flatMap(h => warningSites(h, graph, data, bodies)));
813
+ return true;
814
+ }
815
+ const callees = targets.flatMap(t => hidesBody(t) ? [undefined] : t.value?.map(id => data.completeAst.idMap.get(id)) ?? [undefined]);
816
+ if (callees.length === 0 || !callees.every(r_function_definition_1.RFunctionDefinition.is)) {
817
+ sites.push(node.info.id);
818
+ return false;
819
+ }
820
+ /* the body runs in the function's own scope, we resolve what it calls where the construct is, which is where most functions live */
821
+ const within = callees.flatMap(c => bodySites(c, graph, data, bodies));
822
+ for (const site of within) {
823
+ graph.addEdge(node.info.id, site, edge_1.EdgeType.Reads);
824
+ }
825
+ if (within.length > 0) {
826
+ sites.push(node.info.id);
827
+ }
828
+ }
829
+ return false;
830
+ });
831
+ return sites;
832
+ }
833
+ /** The handlers of a `withCallingHandlers` whose first handler muffles every `warning` of its `expr`, `undefined` otherwise. */
834
+ function muffledHandlers(call, graph, data) {
835
+ const vertex = graph.getVertex(call.info.id);
836
+ if (!vertex_1.DfgVertex.isFunctionCall(vertex) || !resolvesTo(call, 'withCallingHandlers', built_in_proc_name_1.BuiltInProcName.Try, graph, data)) {
837
+ return undefined;
838
+ }
839
+ const handlers = match_args_1.MatchArgs.toSpec(vertex.args, { expr: 'expr', '...': '...' }).get('...') ?? [];
840
+ const first = vertex.args.filter(r_argument_1.RArgument.isNotEmpty).find(a => handlers.includes(a.nodeId));
841
+ const handler = first?.name === 'warning' ? data.completeAst.idMap.get(first.nodeId) : undefined;
842
+ const fn = r_argument_1.RArgument.is(handler) ? handler.value : undefined;
843
+ const body = r_function_definition_1.RFunctionDefinition.is(fn) ? fn.body : undefined;
844
+ const start = r_expression_list_1.RExpressionList.is(body) ? body.children[0] : body;
845
+ const restart = r_function_call_1.RFunctionCall.is(start) && resolvesTo(start, 'invokeRestart', built_in_proc_name_1.BuiltInProcName.Default, graph, data) && start.arguments.length === 1 ? start.arguments[0] : undefined;
846
+ const muffles = node_value_1.NodeValue.stringsOf(r_argument_1.RArgument.valueOf(restart)?.info.id, data)?.join() === 'muffleWarning';
847
+ return muffles ? handlers.map(h => data.completeAst.idMap.get(h)).filter(assert_1.isNotUndefined) : undefined;
848
+ }
849
+ /** whether `call` surely calls the built-in `name`, its `origin` telling that nothing in between shadows it */
850
+ function resolvesTo(call, name, origin, graph, data) {
851
+ if (!vertex_1.DfgVertex.hasOrigin(graph.getVertex(call.info.id), origin)) {
852
+ return false;
853
+ }
854
+ const targets = call.named ? resolve_helper_1.Resolve.byNameAndType(call.functionName.content, data.environment, identifier_1.ReferenceType.Function) : undefined;
855
+ return targets !== undefined && targets.length > 0 && targets.every(t => t.nodeId === node_id_1.NodeId.toBuiltIn(name));
856
+ }
857
+ /** what may warn in the body of the closure `definition`, cached in `bodies` */
858
+ function bodySites(definition, graph, data, bodies) {
859
+ let sites = bodies.get(definition.info.id);
860
+ if (sites === undefined) {
861
+ /* a recursive call adds nothing to what the body may warn about */
862
+ bodies.set(definition.info.id, []);
863
+ sites = warningSites(definition.body, graph, data, bodies);
864
+ bodies.set(definition.info.id, sites);
865
+ }
866
+ return sites;
867
+ }
506
868
  //# sourceMappingURL=built-in-ambient-state.js.map