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