@eagleoutice/flowr 2.15.9 → 2.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (244) hide show
  1. package/README.md +47 -41
  2. package/abstract-interpretation/absint-inference.d.ts +2 -2
  3. package/abstract-interpretation/absint-inference.js +32 -79
  4. package/abstract-interpretation/data-frame/arguments.js +11 -7
  5. package/benchmark/slicer.d.ts +0 -1
  6. package/benchmark/slicer.js +1 -2
  7. package/cli/repl/commands/repl-signature.js +12 -3
  8. package/config.d.ts +5 -1
  9. package/control-flow/cfg-properties.js +0 -22
  10. package/control-flow/happens-before.d.ts +7 -2
  11. package/control-flow/happens-before.js +55 -31
  12. package/control-flow/useless-loop.d.ts +1 -1
  13. package/control-flow/useless-loop.js +29 -42
  14. package/dataflow/environments/apply-kill.js +6 -2
  15. package/dataflow/environments/built-in-config.d.ts +2 -0
  16. package/dataflow/environments/built-in-props.d.ts +16 -1
  17. package/dataflow/environments/built-in-props.js +33 -20
  18. package/dataflow/environments/built-in.d.ts +8 -3
  19. package/dataflow/environments/built-in.js +52 -82
  20. package/dataflow/environments/default-builtin-config.d.ts +551 -21
  21. package/dataflow/environments/default-builtin-config.js +121 -45
  22. package/dataflow/environments/define.d.ts +3 -2
  23. package/dataflow/environments/define.js +4 -3
  24. package/dataflow/environments/deprecation-info.d.ts +45 -0
  25. package/dataflow/environments/deprecation-info.js +13 -0
  26. package/dataflow/environments/environment.d.ts +7 -5
  27. package/dataflow/environments/environment.js +55 -121
  28. package/dataflow/environments/identifier.d.ts +6 -2
  29. package/dataflow/environments/identifier.js +23 -69
  30. package/dataflow/environments/reference-to-maybe.d.ts +9 -0
  31. package/dataflow/environments/reference-to-maybe.js +23 -51
  32. package/dataflow/environments/resolve-by-name.js +37 -76
  33. package/dataflow/eval/resolve/resolve-argument.d.ts +3 -1
  34. package/dataflow/eval/resolve/resolve-argument.js +61 -64
  35. package/dataflow/eval/resolve/resolve-working-directory.d.ts +8 -2
  36. package/dataflow/eval/resolve/resolve-working-directory.js +12 -20
  37. package/dataflow/eval/resolve/resolve.d.ts +4 -0
  38. package/dataflow/eval/resolve/resolve.js +23 -14
  39. package/dataflow/extractor.js +180 -58
  40. package/dataflow/fn/class-declaration.d.ts +8 -13
  41. package/dataflow/fn/class-declaration.js +95 -116
  42. package/dataflow/fn/exceptions-of-function.js +68 -37
  43. package/dataflow/fn/frame-reflection.d.ts +12 -4
  44. package/dataflow/fn/frame-reflection.js +51 -41
  45. package/dataflow/fn/function-props.js +15 -33
  46. package/dataflow/fn/function-semantics.d.ts +11 -3
  47. package/dataflow/fn/function-semantics.js +6 -0
  48. package/dataflow/fn/higher-order-function.d.ts +9 -1
  49. package/dataflow/fn/higher-order-function.js +27 -24
  50. package/dataflow/fn/strict-function.d.ts +13 -0
  51. package/dataflow/fn/strict-function.js +291 -123
  52. package/dataflow/graph/call-graph.d.ts +7 -6
  53. package/dataflow/graph/call-graph.js +35 -46
  54. package/dataflow/graph/graph.d.ts +13 -1
  55. package/dataflow/graph/graph.js +74 -66
  56. package/dataflow/graph/vertex.d.ts +1 -1
  57. package/dataflow/info.d.ts +8 -1
  58. package/dataflow/info.js +24 -30
  59. package/dataflow/internal/control-flow.d.ts +31 -4
  60. package/dataflow/internal/control-flow.js +61 -39
  61. package/dataflow/internal/linker.d.ts +17 -8
  62. package/dataflow/internal/linker.js +286 -213
  63. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +179 -0
  64. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +874 -0
  65. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +3 -1
  66. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +60 -48
  67. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +4 -2
  68. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +135 -198
  69. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.js +7 -9
  70. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.d.ts +5 -1
  71. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.js +21 -14
  72. package/dataflow/internal/process/functions/call/built-in/built-in-eval.js +14 -4
  73. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +1 -1
  74. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +141 -146
  75. package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +56 -24
  76. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +292 -253
  77. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +1 -1
  78. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +30 -32
  79. package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +68 -67
  80. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +91 -129
  81. package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +1 -3
  82. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +16 -12
  83. package/dataflow/internal/process/functions/call/built-in/built-in-quote.d.ts +4 -4
  84. package/dataflow/internal/process/functions/call/built-in/built-in-quote.js +9 -35
  85. package/dataflow/internal/process/functions/call/built-in/built-in-repeat-loop.js +2 -0
  86. package/dataflow/internal/process/functions/call/built-in/built-in-rm.js +8 -3
  87. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.d.ts +5 -3
  88. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.js +13 -31
  89. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.d.ts +1 -1
  90. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.js +42 -119
  91. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.d.ts +1 -3
  92. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.js +21 -21
  93. package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +2 -0
  94. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +93 -102
  95. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.d.ts +7 -3
  96. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +173 -114
  97. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.d.ts +1 -3
  98. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.js +23 -26
  99. package/dataflow/internal/process/functions/call/built-in/built-in-with.d.ts +1 -1
  100. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +34 -9
  101. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +1 -1
  102. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +119 -93
  103. package/dataflow/internal/process/functions/call/common.d.ts +4 -4
  104. package/dataflow/internal/process/functions/call/common.js +49 -71
  105. package/dataflow/internal/process/functions/call/deferred.d.ts +12 -2
  106. package/dataflow/internal/process/functions/call/deferred.js +81 -63
  107. package/dataflow/internal/process/functions/call/known-call-handling.d.ts +4 -2
  108. package/dataflow/internal/process/functions/call/known-call-handling.js +65 -68
  109. package/dataflow/internal/process/functions/call/named-call-handling.d.ts +3 -5
  110. package/dataflow/internal/process/functions/call/named-call-handling.js +48 -19
  111. package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
  112. package/dataflow/internal/process/functions/call/quoted.js +257 -154
  113. package/dataflow/internal/process/functions/call/unnamed-call-handling.js +17 -38
  114. package/dataflow/internal/process/functions/process-parameter.js +25 -32
  115. package/dataflow/internal/process/process-symbol.js +4 -3
  116. package/dataflow/internal/program-order.d.ts +26 -0
  117. package/dataflow/internal/program-order.js +49 -0
  118. package/documentation/data/faq/faqs.js +3 -3
  119. package/documentation/data/interface/doc-writing-code.js +1 -2
  120. package/documentation/data/server/doc-data-server-messages.js +7 -7
  121. package/documentation/doc-capabilities.js +80 -34
  122. package/documentation/doc-util/doc-query.d.ts +0 -3
  123. package/documentation/doc-util/doc-query.js +4 -3
  124. package/documentation/wiki-analyzer.js +1 -1
  125. package/documentation/wiki-cfg.js +5 -6
  126. package/documentation/wiki-core.js +8 -8
  127. package/documentation/wiki-dataflow-graph.js +10 -11
  128. package/documentation/wiki-interface.js +2 -2
  129. package/documentation/wiki-linting-and-testing.js +3 -3
  130. package/documentation/wiki-overview.js +6 -6
  131. package/documentation/wiki-query.js +9 -101
  132. package/documentation/wiki-setup.js +23 -0
  133. package/documentation/wiki-signature-database.js +1 -1
  134. package/linter/linter-rules.d.ts +18 -9
  135. package/linter/rules/absolute-path.js +63 -87
  136. package/linter/rules/deprecated-functions.d.ts +1 -45
  137. package/linter/rules/deprecated-functions.js +24 -33
  138. package/linter/rules/file-path-validity.d.ts +4 -2
  139. package/linter/rules/file-path-validity.js +114 -80
  140. package/linter/rules/namespace-access.js +21 -33
  141. package/linter/rules/naming-convention.d.ts +1 -1
  142. package/linter/rules/naming-convention.js +60 -48
  143. package/linter/rules/network-functions.js +11 -7
  144. package/linter/rules/no-leaked-credentials.js +15 -18
  145. package/linter/rules/problematic-inputs.d.ts +2 -1
  146. package/linter/rules/problematic-inputs.js +39 -59
  147. package/linter/rules/roxygen-arguments.d.ts +9 -4
  148. package/linter/rules/roxygen-arguments.js +29 -48
  149. package/linter/rules/seeded-randomness.d.ts +7 -4
  150. package/linter/rules/seeded-randomness.js +128 -89
  151. package/linter/rules/stop-with-call-arg.d.ts +4 -2
  152. package/linter/rules/stop-with-call-arg.js +25 -22
  153. package/linter/rules/unclosed-connection.js +31 -24
  154. package/linter/rules/undefined-symbol.d.ts +4 -6
  155. package/linter/rules/undefined-symbol.js +58 -75
  156. package/linter/rules/unescaped-arguments.js +18 -44
  157. package/linter/rules/unused-definition.d.ts +6 -0
  158. package/linter/rules/unused-definition.js +105 -145
  159. package/linter/rules/unused-import.d.ts +3 -0
  160. package/linter/rules/unused-import.js +25 -43
  161. package/package.json +5 -2
  162. package/project/flowr-analyzer.d.ts +2 -0
  163. package/project/flowr-analyzer.js +28 -1
  164. package/project/plugins/file-plugins/files/flowr-jupyter-file.d.ts +2 -2
  165. package/project/plugins/file-plugins/files/flowr-jupyter-file.js +8 -12
  166. package/project/plugins/file-plugins/files/flowr-rda-file.d.ts +0 -2
  167. package/project/plugins/file-plugins/files/flowr-rda-file.js +10 -55
  168. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.d.ts +7 -5
  169. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.js +34 -56
  170. package/project/plugins/file-plugins/flowr-analyzer-file-plugin.js +2 -1
  171. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +22 -45
  172. package/project/plugins/project-discovery/flowr-analyzer-project-discovery-plugin.js +2 -2
  173. package/project/sigdb/build.js +4 -0
  174. package/project/sigdb/manifest.d.ts +2 -0
  175. package/project/sigdb/manifest.js +10 -1
  176. package/project/sigdb/reader.js +23 -61
  177. package/project/sigdb/sigdb-download.d.ts +29 -6
  178. package/project/sigdb/sigdb-download.js +212 -64
  179. package/queries/base-query-format.js +1 -2
  180. package/queries/catalog/absint-query/absint-query-format.js +1 -1
  181. package/queries/catalog/call-context-query/cascade-action.d.ts +3 -1
  182. package/queries/catalog/call-context-query/cascade-action.js +2 -0
  183. package/queries/catalog/call-context-query/identify-link-to-last-call-relation.js +3 -0
  184. package/queries/catalog/dataflow-query/dataflow-query-executor.js +2 -2
  185. package/queries/catalog/dataflow-query/dataflow-query-format.d.ts +2 -0
  186. package/queries/catalog/dataflow-query/dataflow-query-format.js +4 -7
  187. package/queries/catalog/dependencies-query/dependencies-query-executor.js +50 -114
  188. package/queries/catalog/dependencies-query/function-info/library-functions.d.ts +5 -9
  189. package/queries/catalog/dependencies-query/function-info/library-functions.js +6 -0
  190. package/queries/catalog/dependencies-query/function-info/read-functions.js +1 -1
  191. package/queries/catalog/dependencies-query/function-info/write-functions.js +7 -3
  192. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +4 -4
  193. package/queries/catalog/input-sources-query/simple-input-classifier.js +192 -301
  194. package/queries/catalog/inspect-higher-order-query/inspect-higher-order-query-executor.js +2 -1
  195. package/queries/catalog/origin-query/origin-query-executor.d.ts +1 -1
  196. package/queries/catalog/origin-query/origin-query-executor.js +2 -2
  197. package/queries/catalog/origin-query/origin-query-format.d.ts +2 -2
  198. package/queries/catalog/origin-query/origin-query-format.js +1 -1
  199. package/queries/query.d.ts +1 -1
  200. package/r-bridge/data/data.d.ts +21 -14
  201. package/r-bridge/data/data.js +28 -20
  202. package/r-bridge/lang-4.x/ast/model/model.d.ts +3 -1
  203. package/r-bridge/lang-4.x/ast/model/model.js +19 -43
  204. package/r-bridge/lang-4.x/ast/model/nodes/r-access.d.ts +1 -0
  205. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.d.ts +3 -0
  206. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.js +5 -12
  207. package/r-bridge/lang-4.x/ast/model/nodes/r-binary-op.d.ts +1 -0
  208. package/r-bridge/lang-4.x/ast/model/nodes/r-break.d.ts +1 -0
  209. package/r-bridge/lang-4.x/ast/model/nodes/r-comment.d.ts +1 -0
  210. package/r-bridge/lang-4.x/ast/model/nodes/r-expression-list.d.ts +1 -0
  211. package/r-bridge/lang-4.x/ast/model/nodes/r-for-loop.d.ts +1 -0
  212. package/r-bridge/lang-4.x/ast/model/nodes/r-function-call.d.ts +1 -0
  213. package/r-bridge/lang-4.x/ast/model/nodes/r-function-definition.d.ts +1 -0
  214. package/r-bridge/lang-4.x/ast/model/nodes/r-if-then-else.d.ts +1 -0
  215. package/r-bridge/lang-4.x/ast/model/nodes/r-line-directive.d.ts +1 -0
  216. package/r-bridge/lang-4.x/ast/model/nodes/r-logical.d.ts +1 -0
  217. package/r-bridge/lang-4.x/ast/model/nodes/r-next.d.ts +1 -0
  218. package/r-bridge/lang-4.x/ast/model/nodes/r-number.d.ts +1 -0
  219. package/r-bridge/lang-4.x/ast/model/nodes/r-parameter.d.ts +1 -0
  220. package/r-bridge/lang-4.x/ast/model/nodes/r-pipe.d.ts +1 -0
  221. package/r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop.d.ts +1 -0
  222. package/r-bridge/lang-4.x/ast/model/nodes/r-string.d.ts +1 -0
  223. package/r-bridge/lang-4.x/ast/model/nodes/r-symbol.d.ts +1 -0
  224. package/r-bridge/lang-4.x/ast/model/nodes/r-unary-op.d.ts +1 -0
  225. package/r-bridge/lang-4.x/ast/model/nodes/r-while-loop.d.ts +1 -0
  226. package/r-bridge/lang-4.x/tree-sitter/tree-sitter-executor.d.ts +2 -2
  227. package/r-bridge/lang-4.x/tree-sitter/tree-sitter-executor.js +11 -2
  228. package/r-bridge/roxygen2/roxygen-parse.js +28 -42
  229. package/r-bridge/shell.js +6 -0
  230. package/search/search-executor/search-enrichers.js +25 -37
  231. package/slicing/static/slice-call.d.ts +4 -3
  232. package/slicing/static/slice-call.js +56 -88
  233. package/slicing/static/static-slicer.js +115 -79
  234. package/slicing/static/visiting-queue.d.ts +4 -6
  235. package/slicing/static/visiting-queue.js +9 -13
  236. package/util/files.d.ts +2 -0
  237. package/util/files.js +3 -1
  238. package/util/mermaid/mermaid.js +6 -2
  239. package/util/proc.js +2 -1
  240. package/util/r-base-packages.d.ts +2 -0
  241. package/util/r-base-packages.js +23 -13
  242. package/util/text/playground-link.d.ts +12 -3
  243. package/util/text/playground-link.js +18 -27
  244. package/util/version.js +4 -10
@@ -12,6 +12,7 @@ exports.suppliesArg = suppliesArg;
12
12
  exports.resolveFirstEnvirArg = resolveFirstEnvirArg;
13
13
  exports.unknownIfAmbiguous = unknownIfAmbiguous;
14
14
  exports.resolveSymbolToEnvir = resolveSymbolToEnvir;
15
+ exports.rebindHolders = rebindHolders;
15
16
  exports.routeWrittenToStackEnv = routeWrittenToStackEnv;
16
17
  exports.routeWrittenToEnvir = routeWrittenToEnvir;
17
18
  exports.resolveAttachPosition = resolveAttachPosition;
@@ -19,6 +20,7 @@ const r_value_1 = require("../../../../../eval/values/r-value");
19
20
  const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
20
21
  const unpack_argument_1 = require("../argument/unpack-argument");
21
22
  const identifier_1 = require("../../../../../environments/identifier");
23
+ const identifier_2 = require("../../../../../environments/identifier");
22
24
  const define_1 = require("../../../../../environments/define");
23
25
  const environment_1 = require("../../../../../environments/environment");
24
26
  const prefix_1 = require("../../../../../../util/prefix");
@@ -50,7 +52,7 @@ function resolveDefsToEnvirResolution(defs, nodeId, data) {
50
52
  const envDef = inDefs[0];
51
53
  return { envirData: { ...data, environment: envState }, envDef, envirNodeId: nodeId, stack: stackKindOf(envState) };
52
54
  }
53
- if (!inDefs.every(identifier_1.hasEnvState)) {
55
+ if (!inDefs.every(identifier_2.hasEnvState)) {
54
56
  return undefined;
55
57
  }
56
58
  const stack = stackKindOf(inDefs[0].envState);
@@ -115,9 +117,6 @@ function findReturnsEnvState(defs) {
115
117
  function envirOf(routing) {
116
118
  return routing === 'ambiguous' ? undefined : routing;
117
119
  }
118
- function isAmbiguousEnvirDef(d) {
119
- return d.type === identifier_1.ReferenceType.Parameter && !(0, identifier_1.hasEnvState)(d);
120
- }
121
120
  /** Resolves a single already-found argument (e.g. from {@link RFunctionCall.matchArgsToParams}) to an {@link EnvirArgRouting}. */
122
121
  function resolveArgToEnvirOrAmbiguous(arg, data) {
123
122
  if (r_argument_1.RArgument.isEmpty(arg)) {
@@ -132,9 +131,10 @@ function resolveArgToEnvirOrAmbiguous(arg, data) {
132
131
  if (!r_symbol_1.RSymbol.is(node)) {
133
132
  return undefined;
134
133
  }
135
- const defs = resolve_helper_1.Resolve.byNameAndType(node.content, data.environment, identifier_1.ReferenceType.Variable);
134
+ const defs = resolve_helper_1.Resolve.byNameAndType(node.content, data.environment, identifier_2.ReferenceType.Variable);
135
+ /* an environment we do not track, or a free variable of a function body bound only once the function is called */
136
136
  return resolveDefsToEnvirResolution(defs, node.info.id, data)
137
- ?? (defs?.some(isAmbiguousEnvirDef) ? 'ambiguous' : undefined);
137
+ ?? ((defs === undefined && data.environment.level === 0) || !data.ctx.config.solver.trackEnvironments ? undefined : 'ambiguous');
138
138
  }
139
139
  /** Builds an {@link EnvirResolution} for an environment obtained directly (not via a holder variable), e.g. `globalenv()` / `.GlobalEnv`. */
140
140
  function stackEnvirResolution(envState, nodeId, lexeme, data) {
@@ -142,7 +142,7 @@ function stackEnvirResolution(envState, nodeId, lexeme, data) {
142
142
  const envDef = {
143
143
  name: lexeme,
144
144
  nodeId,
145
- type: identifier_1.ReferenceType.Variable,
145
+ type: identifier_2.ReferenceType.Variable,
146
146
  definedAt: nodeId,
147
147
  envState,
148
148
  };
@@ -218,7 +218,7 @@ function unknownIfAmbiguous(routing, result, rootId) {
218
218
  }
219
219
  /** Resolves a symbol by name to an {@link EnvirResolution} when it holds a tracked environment. */
220
220
  function resolveSymbolToEnvir(symbolName, nodeId, data) {
221
- return envirOf(resolveDefsToEnvirResolution(resolve_helper_1.Resolve.byNameAndType(symbolName, data.environment, identifier_1.ReferenceType.Variable), nodeId, data));
221
+ return envirOf(resolveDefsToEnvirResolution(resolve_helper_1.Resolve.byNameAndType(symbolName, data.environment, identifier_2.ReferenceType.Variable), nodeId, data));
222
222
  }
223
223
  function writtenDefinitionsOf(result, definedAt) {
224
224
  return result.out.filter((d) => d.name !== undefined && 'definedAt' in d &&
@@ -230,7 +230,8 @@ function holdersOf(environment, envDef) {
230
230
  while (env !== undefined && !env.builtInEnv) {
231
231
  for (const defs of env.memory.values()) {
232
232
  for (const def of defs) {
233
- if (def.envState === envDef.envState && def.name !== undefined && def.nodeId !== envDef.nodeId) {
233
+ /* a copy (`g <- e`) holds the same environment object, even once one of them saw a write */
234
+ if ((def.envState === envDef.envState || (def.envId !== undefined && def.envId === envDef.envId)) && def.name !== undefined && def.nodeId !== envDef.nodeId) {
234
235
  holders.push(def);
235
236
  }
236
237
  }
@@ -245,15 +246,21 @@ function holdersOf(environment, envDef) {
245
246
  function routeWrittenToCustomEnv(result, envDef, newDefAt, definedAt) {
246
247
  const written = writtenDefinitionsOf(result, definedAt);
247
248
  let newEnvState = envDef.envState;
248
- const namesToRemove = written.map(w => ({ name: w.name }));
249
+ /* only a binding the write made here moves into the environment, `local(y <- 2, envir = e)` leaves the caller's `y` alone */
250
+ const namesToRemove = written.filter(w => result.environment.current.lookup(identifier_1.Identifier.getName(w.name))?.some(d => d.nodeId === w.nodeId)).map(w => ({ name: w.name }));
249
251
  for (const w of written) {
250
252
  newEnvState = (0, define_1.define)(w, false, newEnvState);
251
253
  }
252
- let newEnvironment = { current: result.environment.current.removeAll(namesToRemove), level: result.environment.level };
253
- for (const holder of holdersOf(result.environment, envDef)) {
254
- newEnvironment = (0, define_1.define)({ ...holder, definedAt: newDefAt, envState: newEnvState }, false, newEnvironment);
254
+ const stripped = { current: result.environment.current.removeAll(namesToRemove), level: result.environment.level };
255
+ return { ...result, environment: rebindHolders(stripped, envDef, newEnvState, newDefAt) };
256
+ }
257
+ /** Binds every variable holding the environment of `envDef` to its new state, where that variable is bound. */
258
+ function rebindHolders(environment, envDef, envState, definedAt) {
259
+ for (const holder of holdersOf(environment, envDef)) {
260
+ /* a holder bound in an enclosing frame is updated there, so the write escapes the function like a `<<-` */
261
+ environment = (0, define_1.define)({ ...holder, definedAt: definedAt ?? holder.definedAt, envState }, environment.current.lookup(identifier_1.Identifier.getName(holder.name)) === undefined, environment);
255
262
  }
256
- return { ...result, environment: newEnvironment };
263
+ return environment;
257
264
  }
258
265
  /**
259
266
  * Routes writes into a real stack env's frame chain, the same way `<<-` reaches an outer scope.
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processEvalCall = processEvalCall;
4
+ const r_binary_op_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-binary-op");
4
5
  const function_semantics_1 = require("../../../../../fn/function-semantics");
5
6
  const info_1 = require("../../../../../info");
6
7
  const known_call_handling_1 = require("../known-call-handling");
@@ -25,6 +26,7 @@ const built_in_envir_utils_1 = require("./built-in-envir-utils");
25
26
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
26
27
  const r_string_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string");
27
28
  const r_function_call_2 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
29
+ const built_in_ambient_state_1 = require("./built-in-ambient-state");
28
30
  const SymbolConstructors = new Set(['as.name', 'as.symbol']);
29
31
  const SyntacticName = /^[.a-zA-Z][.a-zA-Z0-9_]*$/;
30
32
  /** the formals of `eval(expr, envir, enclos)` */
@@ -84,7 +86,8 @@ function processEvalCall(name, args, rootId, data, config) {
84
86
  }
85
87
  (0, log_1.expensiveTrace)(logger_1.dataflowLogger, () => `Non-constant argument ${JSON.stringify(args)} for eval is currently not supported, skipping`);
86
88
  (0, unknown_side_effect_1.handleUnknownSideEffect)(information.graph, information.environment, rootId);
87
- return information;
89
+ /* a captured expression is linked once the graph is complete, but the state its calls access has to show now */
90
+ return (0, built_in_ambient_state_1.evaluatesCapturedCode)(information, rootId, data);
88
91
  }
89
92
  function namesParentFrame(envir, data) {
90
93
  return r_function_call_1.RFunctionCall.isNamed(envir)
@@ -130,6 +133,10 @@ function resolveEvalToCode(evalArgument, config, data) {
130
133
  }
131
134
  return getAsString(arg.value, data);
132
135
  }
136
+ else if (r_function_call_1.RFunctionCall.isNamed(val) && ['str2lang', 'str2expression'].includes(identifier_1.Identifier.getName(val.functionName.content))) {
137
+ /* both parse their string, like `parse(text = s)` */
138
+ return getAsString(r_function_call_1.RFunctionCall.soleArgument(val.arguments)?.value, data);
139
+ }
133
140
  else if (r_function_call_1.RFunctionCall.isNamed(val) && SymbolConstructors.has(identifier_1.Identifier.getName(val.functionName.content))
134
141
  && resolve_helper_1.Resolve.isBuiltIn(val.functionName.content, data.environment, identifier_1.ReferenceType.Function)) {
135
142
  const arg = r_function_call_1.RFunctionCall.soleArgument(val.arguments);
@@ -137,9 +144,12 @@ function resolveEvalToCode(evalArgument, config, data) {
137
144
  return named !== undefined && SyntacticName.test(named) ? [named] : undefined;
138
145
  }
139
146
  else if (r_symbol_1.RSymbol.is(val)) {
140
- // const resolved = resolveValueOfVariable(val.content, env);
141
- // see https://github.com/flowr-analysis/flowr/pull/1467
142
- return undefined;
147
+ /* `nm <- as.name("w"); eval(nm)`: a name bound once to code we can read is that code */
148
+ const defs = resolve_helper_1.Resolve.byName(val.content, data.environment);
149
+ const target = defs?.length === 1 ? defs[0].nodeId : undefined;
150
+ const assignment = target === undefined ? undefined : data.completeAst.idMap.get(data.completeAst.idMap.get(target)?.info.parent ?? '');
151
+ const source = r_binary_op_1.RBinaryOp.is(assignment) ? assignment.lhs.info.id === target ? assignment.rhs : assignment.lhs : undefined;
152
+ return source === undefined || r_symbol_1.RSymbol.is(source) ? undefined : resolveEvalToCode(source, config, data);
143
153
  }
144
154
  else {
145
155
  return undefined;
@@ -2,7 +2,7 @@ import type { DataflowInformation } from '../../../../../info';
2
2
  import { type DataflowProcessorInformation } from '../../../../../processor';
3
3
  import { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
4
4
  import type { ParentInformation } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/decorate';
5
- import type { PotentiallyEmptyRArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
5
+ import { type PotentiallyEmptyRArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
6
6
  import type { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
7
7
  /**
8
8
  * Processes a list of expressions joining their dataflow graphs accordingly.
@@ -15,61 +15,51 @@ const control_flow_1 = require("../../../../control-flow");
15
15
  const vertex_1 = require("../../../../../graph/vertex");
16
16
  const scoping_1 = require("../../../../../environments/scoping");
17
17
  const overwrite_1 = require("../../../../../environments/overwrite");
18
- const logger_1 = require("../../../../../logger");
19
- const log_1 = require("../../../../../../util/log");
18
+ const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
20
19
  const reference_to_maybe_1 = require("../../../../../environments/reference-to-maybe");
21
20
  const apply_kill_1 = require("../../../../../environments/apply-kill");
22
21
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
23
22
  const general_1 = require("../../../../../eval/values/general");
24
23
  const vertex_2 = require("../../../../../graph/vertex");
25
24
  const resolve_helper_1 = require("../../../../../environments/resolve-helper");
26
- const type_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/type");
25
+ const r_argument_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
26
+ const r_expression_list_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-expression-list");
27
+ const r_if_then_else_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-if-then-else");
28
+ const r_while_loop_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-while-loop");
29
+ const r_for_loop_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-for-loop");
30
+ const model_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/model");
31
+ const defaultmap_1 = require("../../../../../../util/collections/defaultmap");
32
+ const objects_1 = require("../../../../../../util/objects");
33
+ const frame_reflection_1 = require("../../../../../fn/frame-reflection");
34
+ const built_in_ambient_state_1 = require("./built-in-ambient-state");
27
35
  /**
28
36
  * Whether the definitions of this list among the `targets` of a read cover every branch, alone or together.
29
37
  * Only then the read is resolved here and must not bubble up as an ingoing reference.
30
38
  */
31
39
  function coveredByListDefinitions(targets, listEnvironments) {
32
- let cds;
33
- for (const target of targets) {
34
- if (!listEnvironments.has(target.nodeId)) {
35
- continue;
36
- }
37
- else if (target.cds === undefined) {
38
- return true;
39
- }
40
- (cds ??= []).push(...target.cds);
40
+ const own = targets.filter(t => listEnvironments.has(t.nodeId));
41
+ if (own.some(t => t.cds === undefined)) {
42
+ return true;
41
43
  }
42
- return cds !== undefined && (0, info_1.happensInEveryBranch)(cds);
44
+ return own.length > 0 && (0, info_1.happensInEveryBranch)(own.flatMap(t => t.cds ?? []));
43
45
  }
46
+ /** Whether reading the variable defined at `id` calls a function, as for an active binding. */
44
47
  function callsOnRead(graph, id) {
45
- if (!vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(id))) {
46
- return false;
47
- }
48
- for (const e of graph.edgesFrom(id).values()) {
49
- if (edge_1.DfEdge.includesType(e, edge_1.EdgeType.Calls)) {
50
- return true;
51
- }
52
- }
53
- return false;
48
+ return vertex_2.DfgVertex.isVariableDefinition(graph.getVertex(id)) && (0, frame_reflection_1.edgeTargets)(graph, id, edge_1.EdgeType.Calls).length > 0;
54
49
  }
50
+ /** @returns whether `read` sees a definition */
55
51
  function linkReadNameToWriteIfPossible(read, environments, listEnvironments, remainingRead, nextGraph, activeReads) {
56
52
  const readName = read.name && identifier_1.Identifier.isDotDotDotAccess(read.name) ? identifier_1.Identifier.dotdotdot() : read.name;
57
53
  const probableTarget = readName ? resolve_helper_1.Resolve.byNameAndType(readName, environments, read.type) : undefined;
54
+ /* without a target the read stays ingoing (read-ids are unique within the same fold), a later fold may still resolve it */
58
55
  if (probableTarget === undefined || !coveredByListDefinitions(probableTarget, listEnvironments)) {
59
- const readId = readName ? identifier_1.Identifier.getName(readName) : undefined;
60
- const has = remainingRead.get(readId);
61
- if (has) {
62
- if (!has.some(h => h.nodeId === read.nodeId && h.name === read.name && h.cds === read.cds)) {
63
- has.push(read);
64
- }
65
- }
66
- else {
67
- remainingRead.set(readId, [read]);
56
+ const has = remainingRead.get(readName ? identifier_1.Identifier.getName(readName) : undefined);
57
+ if (!has.some(h => h.nodeId === read.nodeId && h.name === read.name && h.cds === read.cds)) {
58
+ has.push(read);
68
59
  }
69
60
  }
70
- // no target: keep it as an ingoing read (read-ids are unique within the same fold); a later fold may still resolve and drop it
71
61
  if (probableTarget === undefined) {
72
- return;
62
+ return false;
73
63
  }
74
64
  const rid = read.nodeId;
75
65
  const isFunc = read.type === identifier_1.ReferenceType.Function || read.type === identifier_1.ReferenceType.BuiltInFunction;
@@ -94,28 +84,25 @@ function linkReadNameToWriteIfPossible(read, environments, listEnvironments, rem
94
84
  tag: vertex_1.VertexType.Value,
95
85
  id: tid,
96
86
  cds: undefined,
97
- value: (0, general_1.valueFromTsValue)((target).value)
87
+ value: (0, general_1.valueFromTsValue)(target.value)
98
88
  }, environments, false);
99
89
  }
100
90
  }
91
+ return true;
101
92
  }
102
- /**
103
- * Expands `direct`'s directly-called functions with everything they transitively call (via `resolveEnv`): a sibling/outer
104
- * callee's escaped globals otherwise never fold into the caller, so a `<<-` escaping several call levels would go unlinked.
105
- */
93
+ /** Expands `initial` with everything it transitively calls, so a `<<-` escaping several call levels still folds into the caller. */
106
94
  function* transitivelyCalledDefinitions(initial, graph, resolveEnv) {
107
95
  const seen = new Set();
108
96
  const stack = initial.map(fn => ({ fn, direct: true }));
109
97
  while (stack.length > 0) {
110
- const { fn, direct } = stack.pop();
98
+ const { fn, direct, cds } = stack.pop();
111
99
  if (!vertex_2.DfgVertex.isFunctionDefinition(fn) || seen.has(fn.id)) {
112
100
  continue;
113
101
  }
114
102
  seen.add(fn.id);
115
- yield { fn, direct };
116
- for (const nodeId of fn.subflow.graph) {
117
- const call = graph.getVertex(nodeId);
118
- if (!vertex_2.DfgVertex.isFunctionCall(call) || call.onlyBuiltin || call.name === undefined) {
103
+ yield { fn, direct, cds };
104
+ for (const [, call] of (0, frame_reflection_1.callsIn)(fn, graph)) {
105
+ if (call.onlyBuiltin || call.name === undefined) {
119
106
  continue;
120
107
  }
121
108
  const resolved = resolve_helper_1.Resolve.byNameAndType(call.name, resolveEnv, identifier_1.ReferenceType.Function);
@@ -124,9 +111,8 @@ function* transitivelyCalledDefinitions(initial, graph, resolveEnv) {
124
111
  }
125
112
  const [targets] = (0, linker_1.getAllLinkedFunctionDefinitions)(new Set(resolved.map(r => r.nodeId)), graph);
126
113
  for (const target of targets) {
127
- if (!seen.has(target.id)) {
128
- stack.push({ fn: target, direct: false });
129
- }
114
+ /* a callee reached only on some paths writes only maybe */
115
+ stack.push({ fn: target, direct: false, cds: call.cds === undefined ? cds : [...cds ?? [], ...call.cds] });
130
116
  }
131
117
  }
132
118
  }
@@ -169,59 +155,71 @@ function foldFrameIntoParent({ current, level }) {
169
155
  * written at that spot would have done, since we only learn a callee always throws after linking it here.
170
156
  */
171
157
  function errorEscapes(from, expression, idMap, graph) {
172
- let node = idMap.get(from);
173
- while (node !== undefined && node.info.id !== expression) {
174
- const parent = node.info.parent !== undefined ? idMap.get(node.info.parent) : undefined;
158
+ for (let node = idMap.get(from); node !== undefined && node.info.id !== expression;) {
159
+ const parent = model_1.RNode.directParent(node, idMap);
175
160
  if (parent === undefined) {
176
161
  return true;
177
162
  }
178
- switch (parent.type) {
179
- case type_1.RType.Argument:
180
- case type_1.RType.ExpressionList:
181
- break;
182
- case type_1.RType.FunctionCall:
183
- /* a handler catches the error instead of passing it on */
184
- if (vertex_2.DfgVertex.hasOrigin(graph.getVertex(parent.info.id), built_in_proc_name_1.BuiltInProcName.Try)) {
185
- return false;
186
- }
187
- break;
188
- case type_1.RType.IfThenElse:
189
- case type_1.RType.WhileLoop:
190
- /* only the condition is guaranteed to be evaluated */
191
- if (parent.condition.info.id !== node.info.id) {
192
- return false;
193
- }
194
- break;
195
- case type_1.RType.ForLoop:
196
- if (parent.vector.info.id !== node.info.id) {
197
- return false;
198
- }
199
- break;
200
- default:
201
- return false;
163
+ if (!errorPassesThrough(parent, node.info.id, graph)) {
164
+ return false;
202
165
  }
203
166
  node = parent;
204
167
  }
205
168
  return true;
206
169
  }
207
- function writtenWithinCall(call, node, idMap) {
208
- if (!idMap.has(call)) {
170
+ /** Whether an error raised within the child `child` of `parent` is sure to be raised by `parent` as well. */
171
+ function errorPassesThrough(parent, child, graph) {
172
+ if (r_argument_1.RArgument.is(parent) || r_expression_list_1.RExpressionList.is(parent)) {
209
173
  return true;
210
174
  }
211
- let current = idMap.get(node);
212
- while (current !== undefined) {
213
- if (current.info.id === call) {
214
- return true;
215
- }
216
- current = current.info.parent !== undefined ? idMap.get(current.info.parent) : undefined;
175
+ else if (r_function_call_1.RFunctionCall.is(parent)) {
176
+ /* a handler catches the error instead of passing it on */
177
+ return !vertex_2.DfgVertex.hasOrigin(graph.getVertex(parent.info.id), built_in_proc_name_1.BuiltInProcName.Try);
178
+ }
179
+ else if (r_if_then_else_1.RIfThenElse.is(parent) || r_while_loop_1.RWhileLoop.is(parent)) {
180
+ /* only the condition is guaranteed to be evaluated */
181
+ return parent.condition.info.id === child;
182
+ }
183
+ else if (r_for_loop_1.RForLoop.is(parent)) {
184
+ return parent.vector.info.id === child;
217
185
  }
218
186
  return false;
219
187
  }
188
+ /** Whether `node` is written within the call `call` (or the call is not part of the AST). */
189
+ function writtenWithinCall(call, node, idMap) {
190
+ return !idMap.has(call) || model_1.RNode.findEnclosing(node, idMap, n => n.info.id === call) !== undefined;
191
+ }
192
+ /** Printing a visible top-level value dispatches to the `print` methods in scope, so the expression runs them like a call would. */
193
+ function printMethodsOf(id, environment, graph) {
194
+ const methods = resolve_helper_1.Resolve.byNameAndType('print', environment, identifier_1.ReferenceType.S3MethodPrefix)?.filter(d => !node_id_1.NodeId.isBuiltIn(d.nodeId)) ?? [];
195
+ if (methods.length === 0) {
196
+ return [];
197
+ }
198
+ for (const method of methods) {
199
+ graph.addEdge(id, method.nodeId, edge_1.EdgeType.Reads);
200
+ }
201
+ const called = [...(0, linker_1.getAllLinkedFunctionDefinitions)(new Set(methods.map(m => m.nodeId)), graph)[0]];
202
+ for (const ref of called.flatMap(c => c.subflow.in)) {
203
+ for (const def of ref.name === undefined ? [] : resolve_helper_1.Resolve.byNameAndType(ref.name, environment, ref.type) ?? []) {
204
+ if (!node_id_1.NodeId.isBuiltIn(def.nodeId)) {
205
+ graph.addEdge(ref.nodeId, def.nodeId, edge_1.EdgeType.Reads);
206
+ }
207
+ }
208
+ }
209
+ return [{ functionCall: id, called, propagateExitPoints: [] }];
210
+ }
220
211
  function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextGraph, localDefs, idMap) {
221
212
  for (const { functionCall, called } of calledEnvs) {
222
- let callDependencies = null;
223
- for (const { fn: calledFn, direct } of transitivelyCalledDefinitions(called, nextGraph, inputEnvironment)) {
224
- (0, assert_1.guard)(vertex_2.DfgVertex.isFunctionDefinition(calledFn), 'called function must be a function definition');
213
+ const callDependencies = nextGraph.getVertex(functionCall)?.cds;
214
+ for (const { fn: calledFn, direct, cds } of transitivelyCalledDefinitions(called, nextGraph, inputEnvironment)) {
215
+ /* walk the smaller of the two, a script may hold many unknown side effects and a function many vertices */
216
+ const inside = calledFn.subflow.graph;
217
+ const effects = nextGraph.unknownSideEffects;
218
+ for (const effect of inside.size < effects.size ? inside : effects) {
219
+ if (typeof effect !== 'object' && inside.has(effect) && effects.has(effect)) {
220
+ nextGraph.addEdge(effect, functionCall, edge_1.EdgeType.SideEffectOnCall);
221
+ }
222
+ }
225
223
  // only merge the environments they have in common
226
224
  let environment = direct ? calledFn.subflow.environment : withoutPackageLayers(calledFn.subflow.environment);
227
225
  if (environment.level > inputEnvironment.level) {
@@ -232,14 +230,10 @@ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextG
232
230
  environment = foldFrameIntoParent(environment);
233
231
  }
234
232
  }
235
- // update alle definitions to be defined at this function call
236
- let current = environment.current;
237
233
  let hasUpdate = false;
238
- while (!current?.builtInEnv) {
239
- // a package attached inside the body (library()) must propagate to the caller, like R attaching globally
240
- if (current.t !== undefined) {
241
- hasUpdate = true;
242
- }
234
+ for (let current = environment.current; !current.builtInEnv; current = current.parent) {
235
+ /* a package attached inside the body (library()) propagates to the caller, like R attaching globally */
236
+ hasUpdate ||= current.t !== undefined;
243
237
  for (const definitions of current.memory.values()) {
244
238
  for (const def of definitions) {
245
239
  if (!node_id_1.NodeId.isBuiltIn(def.definedAt)) {
@@ -248,21 +242,19 @@ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextG
248
242
  }
249
243
  }
250
244
  }
251
- current = current.parent;
252
245
  }
253
- if (hasUpdate) {
254
- const shadowing = localDefs.filter(d => (0, assert_1.isNotUndefined)(d.name) && !writtenWithinCall(functionCall, d.nodeId, idMap));
255
- if (shadowing.length > 0) {
256
- environment = {
257
- current: environment.current.removeAll(shadowing),
258
- level: environment.level
259
- };
260
- }
261
- if (callDependencies === null) {
262
- callDependencies = nextGraph.getVertex(functionCall)?.cds;
263
- }
264
- inputEnvironment = (0, overwrite_1.overwriteEnvironment)(inputEnvironment, environment, callDependencies);
246
+ if (!hasUpdate) {
247
+ continue;
265
248
  }
249
+ const shadowing = localDefs.filter(d => (0, assert_1.isNotUndefined)(d.name) && !writtenWithinCall(functionCall, d.nodeId, idMap));
250
+ if (shadowing.length > 0) {
251
+ environment = {
252
+ current: environment.current.removeAll(shadowing),
253
+ level: environment.level
254
+ };
255
+ }
256
+ const dependencies = cds === undefined ? callDependencies : [...callDependencies ?? [], ...cds];
257
+ inputEnvironment = (0, overwrite_1.overwriteEnvironment)(inputEnvironment, environment, dependencies);
266
258
  }
267
259
  }
268
260
  return inputEnvironment;
@@ -271,66 +263,76 @@ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextG
271
263
  * Processes a list of expressions joining their dataflow graphs accordingly.
272
264
  */
273
265
  function processExpressionList(name, args, rootId, data) {
274
- (0, log_1.expensiveTrace)(logger_1.dataflowLogger, () => `[expr list] with ${args.length} expressions`);
275
266
  let { environment } = data;
276
- // used to detect if a "write" happens within the same expression list
267
+ const idMap = data.completeAst.idMap;
268
+ /* the writes of this list, to detect whether a read sees one of them */
277
269
  const listEnvironments = new Set();
278
- const remainingRead = new Map();
279
- const nextGraph = new graph_1.DataflowGraph(data.completeAst.idMap);
270
+ const remainingRead = new defaultmap_1.DefaultMap(() => []);
271
+ const nextGraph = new graph_1.DataflowGraph(idMap);
280
272
  const out = [];
281
273
  /* lazily created - `rm` is rare, so rm-free lists never allocate this */
282
274
  let killed;
283
275
  const exitPoints = [];
284
- const activeCdsAtStart = data.cds;
276
+ /* what the list stands under as a whole, copied since the vertices share and extend that array */
277
+ const context = data.cds?.slice();
285
278
  const invertExitCds = [];
286
279
  const processedExpressions = [];
287
- let defaultReturnExpr = undefined;
288
- let hooks;
280
+ let defaultReturnExpr;
281
+ /* R prints what the top level of the analyzed script leaves visible, but not what a sourced file does */
282
+ const autoPrint = data.referenceChain.length <= 1 && data.environment.level === 0 && data.completeAst.ast.files.some(f => f.root.info.id === rootId);
283
+ let initialState;
284
+ const hooks = [];
289
285
  for (const arg of args) {
290
286
  const expression = (0, unpack_argument_1.unpackNonameArg)(arg);
291
287
  if (expression === undefined) {
292
288
  processedExpressions.push(undefined);
293
289
  continue;
294
290
  }
295
- // use the current environments for processing
296
291
  data.environment = environment;
297
- const processed = (0, processor_1.processDataflowFor)(expression, data);
292
+ let processed = (0, processor_1.processDataflowFor)(expression, data);
293
+ const printed = autoPrint && (0, built_in_ambient_state_1.autoPrints)(expression, processed, data);
294
+ if (printed) {
295
+ processed = (0, built_in_ambient_state_1.readsOfAutoPrint)(processed, expression, data);
296
+ }
298
297
  processedExpressions.push(processed);
299
298
  /* the expression's graph dies here, what is kept of it afterward is its entry point and its references */
300
299
  nextGraph.mergeWith(processed.graph, true, true);
301
300
  defaultReturnExpr = processed;
302
- // a prior next/break means we do not know if the rest executes, so later environments append rather than overwrite
301
+ /* a prior next/break means we do not know if the rest executes, so later environments append rather than overwrite */
303
302
  if (exitPoints.length > 0) {
304
303
  processed.out = (0, reference_to_maybe_1.makeAllMaybe)(processed.out, nextGraph, processed.environment, true, invertExitCds);
305
304
  processed.in = (0, reference_to_maybe_1.makeAllMaybe)(processed.in, nextGraph, processed.environment, false, invertExitCds);
306
305
  processed.unknownReferences = (0, reference_to_maybe_1.makeAllMaybe)(processed.unknownReferences, nextGraph, processed.environment, false);
307
306
  }
308
- if (processed.hooks.length > 0) {
309
- (hooks ??= []).push(...processed.hooks);
310
- }
307
+ hooks.push(...processed.hooks);
311
308
  // all inputs that have not been written until now are read!
312
309
  const activeReads = [];
313
- for (const read of processed.in) {
314
- linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph, activeReads);
315
- }
316
- for (const read of processed.unknownReferences) {
317
- linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph, activeReads);
310
+ for (const read of processed.in.concat(processed.unknownReferences)) {
311
+ if (!linkReadNameToWriteIfPossible(read, environment, listEnvironments, remainingRead, nextGraph, activeReads) && autoPrint && built_in_ambient_state_1.AmbientStateName.is(read.name)) {
312
+ (initialState ??= new Set()).add(read);
313
+ }
318
314
  }
319
315
  for (const read of activeReads) {
320
316
  nextGraph.updateToFunctionCall({ tag: vertex_1.VertexType.FunctionCall, id: read.nodeId, name: read.name, args: [], environment, onlyBuiltin: false, cds: read.cds, origin: [built_in_proc_name_1.BuiltInProcName.Function] });
321
317
  }
322
- const calledEnvs = (0, linker_1.linkFunctionCalls)(nextGraph, data.completeAst.idMap, processed.graph, activeReads.map(r => r.nodeId));
318
+ const calledEnvs = (0, linker_1.linkFunctionCalls)(nextGraph, idMap, processed.graph, activeReads.map(r => r.nodeId));
319
+ if (printed) {
320
+ calledEnvs.push(...printMethodsOf(processed.entryPoint, environment, nextGraph));
321
+ }
323
322
  for (const c of calledEnvs) {
324
- if (c.propagateExitPoints.length > 0 && errorEscapes(c.functionCall, expression.info.id, data.completeAst.idMap, nextGraph)) {
325
- for (const exit of c.propagateExitPoints) {
326
- processed.exitPoints.push(exit);
327
- }
323
+ if (c.propagateExitPoints.length > 0 && errorEscapes(c.functionCall, expression.info.id, idMap, nextGraph)) {
324
+ processed.exitPoints.push(...c.propagateExitPoints);
325
+ }
326
+ }
327
+ for (const c of calledEnvs) {
328
+ if (c.calledBuiltIns !== undefined) {
329
+ processed = (0, built_in_ambient_state_1.applyAmbientStateOfCalledBuiltIns)(processed, c.functionCall, c.calledBuiltIns, environment, nextGraph, data);
328
330
  }
329
331
  }
330
- (0, info_1.addNonDefaultExitPoints)(exitPoints, invertExitCds, activeCdsAtStart, processed.exitPoints);
332
+ (0, info_1.addNonDefaultExitPoints)(exitPoints, invertExitCds, data.cds, processed.exitPoints);
331
333
  environment = exitPoints.length > 0 ? (0, overwrite_1.overwriteEnvironment)(environment, processed.environment) : processed.environment;
332
334
  // if the called function has global redefinitions, we have to keep them within our environment
333
- environment = updateSideEffectsForCalledFunctions(calledEnvs, environment, nextGraph, processed.out, data.completeAst.idMap);
335
+ environment = updateSideEffectsForCalledFunctions(calledEnvs, environment, nextGraph, processed.out, idMap);
334
336
  // removals are already reflected in the threaded environment; we only bubble them (net of later writes)
335
337
  if (killed && processed.out.length > 0) {
336
338
  killed = (0, apply_kill_1.cancelRevivedKills)(killed, processed.out);
@@ -345,25 +347,17 @@ function processExpressionList(name, args, rootId, data) {
345
347
  listEnvironments.add(ref.nodeId);
346
348
  }
347
349
  /* if at least one exit point encountered happens unconditionally, we exit here (dead code) */
348
- if (control_flow_1.ControlFlow.alwaysExits(processed)) {
350
+ if (control_flow_1.ControlFlow.alwaysExits(processed, context)) {
349
351
  /* if there is an always-exit expression, there is no default return active anymore */
350
352
  defaultReturnExpr = undefined;
351
353
  break;
352
354
  }
353
355
  }
354
356
  if (defaultReturnExpr) {
355
- exitPoints.push(data.cds ? {
356
- type: 0 /* ExitPointType.Default */,
357
- nodeId: defaultReturnExpr.entryPoint,
358
- cds: data.cds
359
- } : {
360
- type: 0 /* ExitPointType.Default */,
361
- nodeId: defaultReturnExpr.entryPoint
362
- });
357
+ exitPoints.push((0, objects_1.compactRecord)({ type: 0 /* ExitPointType.Default */, nodeId: defaultReturnExpr.entryPoint, cds: data.cds }));
363
358
  }
364
359
  const ingoing = [...remainingRead.values()].flat();
365
- const rootNode = data.completeAst.idMap.get(rootId);
366
- const withGroup = rootNode?.grouping;
360
+ const withGroup = idMap.get(rootId)?.grouping;
367
361
  if (withGroup) {
368
362
  ingoing.push({ nodeId: rootId, name: name.content, cds: data.cds, type: identifier_1.ReferenceType.Function });
369
363
  (0, common_1.patchFunctionCall)({
@@ -375,7 +369,6 @@ function processExpressionList(name, args, rootId, data) {
375
369
  origin: built_in_proc_name_1.BuiltInProcName.ExpressionList
376
370
  });
377
371
  nextGraph.addEdge(rootId, node_id_1.NodeId.toBuiltIn('{'), edge_1.EdgeType.Reads | edge_1.EdgeType.Calls);
378
- // process all exit points as potential returns:
379
372
  for (const exit of exitPoints) {
380
373
  if (exit.type === 1 /* ExitPointType.Return */ || exit.type === 0 /* ExitPointType.Default */) {
381
374
  nextGraph.addEdge(rootId, exit.nodeId, edge_1.EdgeType.Returns);
@@ -397,24 +390,26 @@ function processExpressionList(name, args, rootId, data) {
397
390
  nextGraph.addEdge(exit.nodeId, rootId, edge_1.EdgeType.FlowEdge);
398
391
  /* what a `return` hands back and what a `stop` raises stay with the node that does it */
399
392
  if (exit.nodeId !== rootId && (exit.type === 2 /* ExitPointType.Break */ || exit.type === 3 /* ExitPointType.Next */)) {
400
- exitPoints[i] = { ...exit, nodeId: rootId };
393
+ exitPoints[i] = (0, info_1.leaveThrough)(exit, rootId);
401
394
  }
402
395
  }
403
396
  }
404
397
  return {
405
398
  /* no active nodes remain, they are consumed within the remaining read collection */
406
399
  unknownReferences: [],
407
- in: ingoing,
400
+ /* at the top of the script, only a read of the options or graphics state nothing set reads the state R starts with,
401
+ which a write escaping a higher-order call may still replace, see the extractor */
402
+ in: autoPrint ? ingoing.filter(r => !built_in_ambient_state_1.AmbientStateName.is(r.name) || initialState?.has(r) === true) : ingoing,
408
403
  /* a definition that a still-effective removal undid is no longer visible to the outside */
409
404
  out: (0, apply_kill_1.dropKilledWrites)(out, killed),
410
- environment: environment,
405
+ environment,
411
406
  graph: nextGraph,
412
407
  /* if we have no group, we take the last evaluated expr */
413
408
  entryPoint: meId,
414
409
  cfgEntry: cfgEntry === meId ? undefined : cfgEntry,
415
410
  cfgExit: reachesEnd ? rootId : undefined,
416
- exitPoints: exitPoints,
417
- hooks: hooks ?? [],
411
+ exitPoints,
412
+ hooks,
418
413
  kill: killed,
419
414
  };
420
415
  }