@eagleoutice/flowr 2.15.10 → 2.16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (213) hide show
  1. package/README.md +23 -26
  2. package/abstract-interpretation/data-frame/arguments.js +11 -7
  3. package/cli/flowr-main-options.d.ts +15 -0
  4. package/cli/flowr-main-options.js +18 -0
  5. package/cli/flowr.d.ts +18 -0
  6. package/cli/flowr.js +13 -2
  7. package/cli/repl/commands/repl-clipboard.js +2 -1
  8. package/cli/repl/commands/repl-commands.js +2 -0
  9. package/cli/repl/commands/repl-main.d.ts +5 -0
  10. package/cli/repl/commands/repl-signature.js +2 -0
  11. package/cli/repl/core.d.ts +7 -1
  12. package/cli/repl/core.js +17 -8
  13. package/cli/repl/execute.js +2 -0
  14. package/cli/repl/server/connection.js +2 -1
  15. package/cli/repl/server/net.d.ts +10 -0
  16. package/cli/repl/server/net.js +34 -1
  17. package/cli/slicer-app.js +2 -1
  18. package/config.d.ts +12 -2
  19. package/config.js +91 -14
  20. package/control-flow/happens-before.d.ts +5 -0
  21. package/control-flow/happens-before.js +48 -34
  22. package/control-flow/useless-loop.d.ts +1 -1
  23. package/control-flow/useless-loop.js +29 -42
  24. package/dataflow/environments/built-in-props.d.ts +9 -0
  25. package/dataflow/environments/built-in-props.js +25 -19
  26. package/dataflow/environments/built-in.d.ts +2 -2
  27. package/dataflow/environments/built-in.js +41 -77
  28. package/dataflow/environments/default-builtin-config.d.ts +181 -29
  29. package/dataflow/environments/default-builtin-config.js +38 -24
  30. package/dataflow/environments/environment.d.ts +6 -4
  31. package/dataflow/environments/environment.js +54 -121
  32. package/dataflow/environments/identifier.d.ts +4 -2
  33. package/dataflow/environments/identifier.js +23 -69
  34. package/dataflow/environments/reference-to-maybe.d.ts +9 -0
  35. package/dataflow/environments/reference-to-maybe.js +23 -51
  36. package/dataflow/environments/resolve-by-name.js +37 -76
  37. package/dataflow/eval/resolve/resolve-argument.d.ts +3 -1
  38. package/dataflow/eval/resolve/resolve-argument.js +61 -64
  39. package/dataflow/eval/resolve/resolve-working-directory.d.ts +8 -2
  40. package/dataflow/eval/resolve/resolve-working-directory.js +12 -20
  41. package/dataflow/eval/resolve/resolve.d.ts +4 -0
  42. package/dataflow/eval/resolve/resolve.js +23 -14
  43. package/dataflow/extractor.js +185 -59
  44. package/dataflow/fn/class-declaration.d.ts +8 -2
  45. package/dataflow/fn/class-declaration.js +96 -97
  46. package/dataflow/fn/exceptions-of-function.js +68 -37
  47. package/dataflow/fn/frame-reflection.d.ts +12 -4
  48. package/dataflow/fn/frame-reflection.js +64 -42
  49. package/dataflow/fn/function-props.js +15 -33
  50. package/dataflow/fn/function-semantics.d.ts +11 -3
  51. package/dataflow/fn/function-semantics.js +6 -0
  52. package/dataflow/fn/higher-order-function.js +13 -17
  53. package/dataflow/fn/strict-function.d.ts +13 -0
  54. package/dataflow/fn/strict-function.js +285 -122
  55. package/dataflow/graph/call-graph.d.ts +7 -6
  56. package/dataflow/graph/call-graph.js +35 -46
  57. package/dataflow/graph/graph.d.ts +6 -1
  58. package/dataflow/graph/graph.js +57 -66
  59. package/dataflow/info.d.ts +8 -1
  60. package/dataflow/info.js +24 -30
  61. package/dataflow/internal/control-flow.d.ts +42 -4
  62. package/dataflow/internal/control-flow.js +76 -39
  63. package/dataflow/internal/linker.d.ts +15 -9
  64. package/dataflow/internal/linker.js +253 -241
  65. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +80 -14
  66. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +441 -79
  67. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +3 -1
  68. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +51 -48
  69. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +2 -2
  70. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +119 -185
  71. package/dataflow/internal/process/functions/call/built-in/built-in-class-generator.js +7 -9
  72. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.d.ts +1 -1
  73. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +122 -147
  74. package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +55 -25
  75. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +290 -252
  76. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +1 -1
  77. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +30 -32
  78. package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +68 -67
  79. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +72 -126
  80. package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +1 -3
  81. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +10 -10
  82. package/dataflow/internal/process/functions/call/built-in/built-in-quote.d.ts +4 -4
  83. package/dataflow/internal/process/functions/call/built-in/built-in-quote.js +9 -35
  84. package/dataflow/internal/process/functions/call/built-in/built-in-rm.js +8 -3
  85. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.d.ts +1 -1
  86. package/dataflow/internal/process/functions/call/built-in/built-in-s-four.js +12 -28
  87. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.d.ts +1 -1
  88. package/dataflow/internal/process/functions/call/built-in/built-in-s-seven-new-generic.js +40 -118
  89. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.d.ts +1 -3
  90. package/dataflow/internal/process/functions/call/built-in/built-in-s-three-dispatch.js +16 -24
  91. package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +2 -0
  92. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +93 -102
  93. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.d.ts +7 -3
  94. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +173 -114
  95. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.d.ts +1 -3
  96. package/dataflow/internal/process/functions/call/built-in/built-in-while-loop.js +22 -27
  97. package/dataflow/internal/process/functions/call/built-in/built-in-with.d.ts +1 -1
  98. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +34 -9
  99. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +1 -1
  100. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +120 -93
  101. package/dataflow/internal/process/functions/call/common.d.ts +1 -3
  102. package/dataflow/internal/process/functions/call/common.js +36 -76
  103. package/dataflow/internal/process/functions/call/deferred.d.ts +6 -2
  104. package/dataflow/internal/process/functions/call/deferred.js +60 -58
  105. package/dataflow/internal/process/functions/call/known-call-handling.d.ts +4 -2
  106. package/dataflow/internal/process/functions/call/known-call-handling.js +65 -68
  107. package/dataflow/internal/process/functions/call/named-call-handling.d.ts +3 -5
  108. package/dataflow/internal/process/functions/call/named-call-handling.js +44 -21
  109. package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
  110. package/dataflow/internal/process/functions/call/quoted.js +162 -172
  111. package/dataflow/internal/process/functions/call/unnamed-call-handling.js +17 -38
  112. package/dataflow/internal/process/functions/process-parameter.js +25 -32
  113. package/dataflow/internal/process/process-symbol.js +3 -6
  114. package/dataflow/internal/program-order.d.ts +26 -0
  115. package/dataflow/internal/program-order.js +49 -0
  116. package/documentation/doc-capabilities.js +4 -1
  117. package/documentation/wiki-interface.js +4 -0
  118. package/documentation/wiki-setup.js +23 -0
  119. package/engines.js +11 -3
  120. package/linter/linter-executor.js +19 -1
  121. package/linter/linter-rules.d.ts +17 -8
  122. package/linter/rules/absolute-path.js +63 -87
  123. package/linter/rules/file-path-validity.d.ts +4 -2
  124. package/linter/rules/file-path-validity.js +117 -80
  125. package/linter/rules/namespace-access.js +21 -33
  126. package/linter/rules/naming-convention.d.ts +1 -1
  127. package/linter/rules/naming-convention.js +60 -48
  128. package/linter/rules/network-functions.js +11 -7
  129. package/linter/rules/no-leaked-credentials.js +15 -18
  130. package/linter/rules/problematic-inputs.d.ts +2 -1
  131. package/linter/rules/problematic-inputs.js +39 -59
  132. package/linter/rules/roxygen-arguments.d.ts +9 -4
  133. package/linter/rules/roxygen-arguments.js +29 -48
  134. package/linter/rules/seeded-randomness.d.ts +7 -4
  135. package/linter/rules/seeded-randomness.js +130 -89
  136. package/linter/rules/stop-with-call-arg.d.ts +4 -2
  137. package/linter/rules/stop-with-call-arg.js +25 -22
  138. package/linter/rules/unclosed-connection.js +31 -24
  139. package/linter/rules/undefined-symbol.d.ts +4 -6
  140. package/linter/rules/undefined-symbol.js +58 -75
  141. package/linter/rules/unescaped-arguments.js +18 -44
  142. package/linter/rules/unused-definition.d.ts +6 -0
  143. package/linter/rules/unused-definition.js +105 -145
  144. package/linter/rules/unused-import.d.ts +3 -0
  145. package/linter/rules/unused-import.js +25 -43
  146. package/package.json +5 -2
  147. package/project/flowr-analyzer.js +6 -2
  148. package/project/plugins/file-plugins/files/flowr-jupyter-file.d.ts +2 -2
  149. package/project/plugins/file-plugins/files/flowr-jupyter-file.js +8 -12
  150. package/project/plugins/file-plugins/files/flowr-rda-file.d.ts +14 -5
  151. package/project/plugins/file-plugins/files/flowr-rda-file.js +35 -24
  152. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.d.ts +7 -5
  153. package/project/plugins/file-plugins/files/flowr-rmarkdown-file.js +34 -56
  154. package/project/plugins/package-version-plugins/flowr-analyzer-package-versions-sigdb-plugin.js +18 -42
  155. package/project/sigdb/decompress.js +3 -1
  156. package/project/sigdb/sigdb-download.js +9 -6
  157. package/queries/base-query-format.js +1 -2
  158. package/queries/catalog/absint-query/absint-query-format.js +1 -1
  159. package/queries/catalog/config-query/config-query-executor.js +9 -2
  160. package/queries/catalog/dataflow-query/dataflow-query-executor.js +2 -2
  161. package/queries/catalog/dataflow-query/dataflow-query-format.d.ts +2 -0
  162. package/queries/catalog/dataflow-query/dataflow-query-format.js +4 -7
  163. package/queries/catalog/dependencies-query/dependencies-query-executor.js +50 -114
  164. package/queries/catalog/dependencies-query/function-info/library-functions.d.ts +5 -9
  165. package/queries/catalog/dependencies-query/function-info/library-functions.js +6 -0
  166. package/queries/catalog/dependencies-query/function-info/read-functions.js +1 -1
  167. package/queries/catalog/dependencies-query/function-info/write-functions.js +7 -3
  168. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +4 -4
  169. package/queries/catalog/input-sources-query/simple-input-classifier.js +192 -301
  170. package/r-bridge/lang-4.x/ast/model/model.d.ts +3 -1
  171. package/r-bridge/lang-4.x/ast/model/model.js +19 -43
  172. package/r-bridge/lang-4.x/ast/model/nodes/r-access.d.ts +1 -0
  173. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.d.ts +3 -0
  174. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.js +5 -12
  175. package/r-bridge/lang-4.x/ast/model/nodes/r-binary-op.d.ts +1 -0
  176. package/r-bridge/lang-4.x/ast/model/nodes/r-break.d.ts +1 -0
  177. package/r-bridge/lang-4.x/ast/model/nodes/r-comment.d.ts +1 -0
  178. package/r-bridge/lang-4.x/ast/model/nodes/r-expression-list.d.ts +1 -0
  179. package/r-bridge/lang-4.x/ast/model/nodes/r-for-loop.d.ts +1 -0
  180. package/r-bridge/lang-4.x/ast/model/nodes/r-function-call.d.ts +1 -0
  181. package/r-bridge/lang-4.x/ast/model/nodes/r-function-definition.d.ts +1 -0
  182. package/r-bridge/lang-4.x/ast/model/nodes/r-if-then-else.d.ts +1 -0
  183. package/r-bridge/lang-4.x/ast/model/nodes/r-line-directive.d.ts +1 -0
  184. package/r-bridge/lang-4.x/ast/model/nodes/r-logical.d.ts +1 -0
  185. package/r-bridge/lang-4.x/ast/model/nodes/r-next.d.ts +1 -0
  186. package/r-bridge/lang-4.x/ast/model/nodes/r-number.d.ts +1 -0
  187. package/r-bridge/lang-4.x/ast/model/nodes/r-parameter.d.ts +1 -0
  188. package/r-bridge/lang-4.x/ast/model/nodes/r-pipe.d.ts +1 -0
  189. package/r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop.d.ts +1 -0
  190. package/r-bridge/lang-4.x/ast/model/nodes/r-string.d.ts +1 -0
  191. package/r-bridge/lang-4.x/ast/model/nodes/r-symbol.d.ts +1 -0
  192. package/r-bridge/lang-4.x/ast/model/nodes/r-unary-op.d.ts +1 -0
  193. package/r-bridge/lang-4.x/ast/model/nodes/r-while-loop.d.ts +1 -0
  194. package/r-bridge/roxygen2/roxygen-parse.js +28 -42
  195. package/r-bridge/shell-executor.js +2 -0
  196. package/r-bridge/shell.js +8 -0
  197. package/search/search-executor/search-enrichers.js +25 -37
  198. package/slicing/static/slice-call.d.ts +4 -3
  199. package/slicing/static/slice-call.js +56 -88
  200. package/slicing/static/static-slicer.js +120 -79
  201. package/slicing/static/visiting-queue.d.ts +4 -6
  202. package/slicing/static/visiting-queue.js +9 -13
  203. package/util/files.d.ts +6 -0
  204. package/util/files.js +12 -0
  205. package/util/parallel.js +2 -0
  206. package/util/proc.js +2 -1
  207. package/util/r-base-packages.d.ts +2 -0
  208. package/util/r-base-packages.js +23 -13
  209. package/util/secure-mode.d.ts +42 -0
  210. package/util/secure-mode.js +70 -0
  211. package/util/text/playground-link.d.ts +12 -3
  212. package/util/text/playground-link.js +18 -27
  213. package/util/version.js +4 -10
package/r-bridge/shell.js CHANGED
@@ -48,6 +48,7 @@ const fs_1 = __importDefault(require("fs"));
48
48
  const init_1 = require("./init");
49
49
  const convert_values_1 = require("./lang-4.x/convert-values");
50
50
  const retriever_1 = require("./retriever");
51
+ const secure_mode_1 = require("../util/secure-mode");
51
52
  exports.DEFAULT_OUTPUT_COLLECTOR_CONFIGURATION = {
52
53
  from: 'stdout',
53
54
  postamble: `🐧${'-'.repeat(5)}🐧`,
@@ -302,6 +303,7 @@ class RShellSession {
302
303
  collectionTimeout;
303
304
  requestQueue = Promise.resolve();
304
305
  constructor(options, log) {
306
+ secure_mode_1.SecureMode.guard('starting R', options.pathToRExecutable);
305
307
  this.bareSession = (0, child_process_1.spawn)(options.pathToRExecutable, options.commandLineOptions, {
306
308
  env: options.env,
307
309
  cwd: options.cwd,
@@ -380,6 +382,12 @@ class RShellSession {
380
382
  onDrained();
381
383
  };
382
384
  return new Promise((resolve, reject) => {
385
+ if (this.bareSession.exitCode !== null || this.bareSession.signalCode !== null) {
386
+ reject(new Error(`the R session has exited (${this.bareSession.exitCode ?? this.bareSession.signalCode}), it cannot answer anymore`));
387
+ drained = true;
388
+ onDrained();
389
+ return;
390
+ }
383
391
  const makeTimer = () => setTimeout(() => {
384
392
  if (timeout.onTimeout) {
385
393
  timeout.onTimeout(resolve, reject, result);
@@ -40,10 +40,8 @@ const StructuralRoles = new Set([
40
40
  function collectAliveNodes(ast, reachable) {
41
41
  const alive = new Set();
42
42
  for (const id of reachable) {
43
- let node = ast.idMap.get(id);
44
- while (node !== undefined && !alive.has(node.info.id)) {
43
+ for (let node = ast.idMap.get(id); node !== undefined && !alive.has(node.info.id); node = model_1.RNode.directParent(node, ast.idMap)) {
45
44
  alive.add(node.info.id);
46
- node = node.info.parent === undefined ? undefined : ast.idMap.get(node.info.parent);
47
45
  }
48
46
  }
49
47
  return alive;
@@ -58,10 +56,7 @@ function isReachedByControlFlow(node, alive) {
58
56
  if (alive.has(node.info.id)) {
59
57
  return true;
60
58
  }
61
- else if (StructuralRoles.has(node.info.role)) {
62
- return node.info.parent !== undefined && alive.has(node.info.parent);
63
- }
64
- return false;
59
+ return StructuralRoles.has(node.info.role) && node.info.parent !== undefined && alive.has(node.info.parent);
65
60
  }
66
61
  /**
67
62
  * One entry per distinct call target. A call to a package export is modelled by the built-in flowR states for
@@ -107,37 +102,15 @@ exports.Enrichments = {
107
102
  const n = shared?.ast ?? await analyzer.normalize();
108
103
  const callVertex = df.graph.getVertex(e.node.info.id);
109
104
  if (vertex_1.DfgVertex.isFunctionCall(callVertex)) {
110
- const origins = df_helper_1.Dataflow.origin(df.graph, callVertex.id);
111
- if (!origins || origins.length === 0) {
112
- const name = node_id_1.NodeId.recoverName(callVertex.id, n.idMap);
113
- // we don't have origin information here, so pass undefined
114
- content.targets = [qualifyIdentifier(undefined, name)];
105
+ const origins = df_helper_1.Dataflow.origin(df.graph, callVertex.id) ?? [];
106
+ // find call targets in user code (which have ids!)
107
+ const targets = origins.map(targetOf).filter(assert_1.isNotUndefined);
108
+ if (targets.length > 0) {
109
+ content.targets = targets;
115
110
  }
116
111
  else {
117
- // find call targets in user code (which have ids!)
118
- content.targets = origins.map(o => {
119
- switch (o.type) {
120
- case 2 /* OriginType.FunctionCallOrigin */: {
121
- if (node_id_1.NodeId.isBuiltIn(o.id)) {
122
- // a built-in target (e.g. a materialized package export from `library()`) has no
123
- // user-code node, so surface it as a built-in identifier (see `onlyBuiltin` below)
124
- const name = node_id_1.NodeId.recoverName(o.id, n.idMap);
125
- return qualifyIdentifier([o], name) ?? String(o.id);
126
- }
127
- else {
128
- return { node: n.idMap.get(o.id) };
129
- }
130
- }
131
- case 3 /* OriginType.BuiltInFunctionOrigin */:
132
- return qualifyIdentifier([o], o.fn.name);
133
- default:
134
- return undefined;
135
- }
136
- }).filter(assert_1.isNotUndefined);
137
- if (content.targets.length === 0) {
138
- const name = node_id_1.NodeId.recoverName(callVertex.id, n.idMap);
139
- content.targets = [qualifyIdentifier(origins, name)];
140
- }
112
+ const name = node_id_1.NodeId.recoverName(callVertex.id, n.idMap);
113
+ content.targets = [qualifyIdentifier(origins, name)];
141
114
  }
142
115
  }
143
116
  /* a package export and the built-in flowR models it with are one target seen twice, so keep the
@@ -152,6 +125,21 @@ exports.Enrichments = {
152
125
  content.targets.push(...prev.targets);
153
126
  }
154
127
  return content;
128
+ function targetOf(o) {
129
+ switch (o.type) {
130
+ case 2 /* OriginType.FunctionCallOrigin */:
131
+ if (node_id_1.NodeId.isBuiltIn(o.id)) {
132
+ // a built-in target (e.g. a materialized package export from `library()`) has no
133
+ // user-code node, so surface it as a built-in identifier (see `onlyBuiltin` above)
134
+ return qualifyIdentifier([o], node_id_1.NodeId.recoverName(o.id, n.idMap)) ?? String(o.id);
135
+ }
136
+ return { node: n.idMap.get(o.id) };
137
+ case 3 /* OriginType.BuiltInFunctionOrigin */:
138
+ return qualifyIdentifier([o], o.fn.name);
139
+ default:
140
+ return undefined;
141
+ }
142
+ }
155
143
  function qualifyIdentifier(origins, name) {
156
144
  if (args?.qualifyNames === undefined || args.qualifyNames) {
157
145
  const qualified = identifier_1.Identifier.toQualified(origins, name);
@@ -159,7 +147,7 @@ exports.Enrichments = {
159
147
  return identifier_1.Identifier.toString(qualified);
160
148
  }
161
149
  }
162
- return name;
150
+ return name === undefined ? undefined : identifier_1.Identifier.toString(name);
163
151
  }
164
152
  },
165
153
  // as built-in call target enrichments are not nodes, we don't return them as part of the mapper!
@@ -11,8 +11,8 @@ import type { AstIdMap } from '../../r-bridge/lang-4.x/ast/model/processing/deco
11
11
  /**
12
12
  * Returns the function call targets (definitions) by the given caller
13
13
  */
14
- export declare function getAllFunctionCallTargetsForSlice(dataflowGraph: DataflowGraph, callerInfo: DataflowGraphVertexFunctionCall, baseEnvironment: REnvironmentInformation, queue: VisitingQueue, ctx: ReadOnlyFlowrAnalyzerContext): [ReadonlySet<DataflowGraphVertexInfo>, REnvironmentInformation];
15
- /** returns the new threshold hit count */
14
+ export declare function getAllFunctionCallTargetsForSlice(dataflowGraph: DataflowGraph, callerInfo: DataflowGraphVertexFunctionCall, baseEnvironment: REnvironmentInformation, ctx: ReadOnlyFlowrAnalyzerContext): [ReadonlySet<DataflowGraphVertexInfo>, REnvironmentInformation];
15
+ /** Links the call `callerInfo` to the definitions it resolves to, or to the closures of its arguments if it resolves to none. */
16
16
  export declare function sliceForCall(current: NodeToSlice, callerInfo: DataflowGraphVertexFunctionCall, { graph }: DataflowInformation, queue: VisitingQueue, ctx: ReadOnlyFlowrAnalyzerContext): void;
17
17
  /**
18
18
  * Finds the nearest enclosing function-definition node for the given id by walking up the AST parent chain.
@@ -34,7 +34,8 @@ export declare function sliceReachesFunctionInterface(fnDefId: NodeId, graph: Da
34
34
  * `calls` edges). Call site arguments are picked up automatically once the call vertex is processed normally,
35
35
  * as `argument` edges are always traversed.
36
36
  * This is the reverse of what {@link sliceForCall} does for call -\> definition linking.
37
+ * @returns the enqueued ids
37
38
  */
38
- export declare function includeCalleesOfDefinition(fnDefId: NodeId, graph: DataflowGraph, queue: VisitingQueue, baseEnvironment: REnvironmentInformation, baseEnvFingerprint: Fingerprint): void;
39
+ export declare function includeCalleesOfDefinition(fnDefId: NodeId, graph: DataflowGraph, queue: VisitingQueue, baseEnvironment: REnvironmentInformation, baseEnvFingerprint: Fingerprint): NodeId[];
39
40
  /** Returns true if we found at least one return edge */
40
41
  export declare function handleReturns(from: NodeId, queue: VisitingQueue, currentEdges: OutgoingEdges, baseEnvFingerprint: Fingerprint, baseEnvironment: REnvironmentInformation): boolean;
@@ -18,87 +18,73 @@ const built_in_function_definition_1 = require("../../dataflow/internal/process/
18
18
  const static_slicer_1 = require("./static-slicer");
19
19
  const model_1 = require("../../r-bridge/lang-4.x/ast/model/model");
20
20
  const vertex_1 = require("../../dataflow/graph/vertex");
21
+ const frame_reflection_1 = require("../../dataflow/fn/frame-reflection");
21
22
  const built_in_proc_name_1 = require("../../dataflow/environments/built-in-proc-name");
22
23
  const r_function_definition_1 = require("../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
23
24
  const NoCallTargets = new Set();
25
+ const linkedByGraph = new WeakMap();
26
+ function linkedDefinitions(defs, graph) {
27
+ let known = linkedByGraph.get(graph);
28
+ if (known === undefined) {
29
+ linkedByGraph.set(graph, known = new Map());
30
+ }
31
+ const key = defs.join(';');
32
+ let linked = known.get(key);
33
+ if (linked === undefined) {
34
+ known.set(key, linked = (0, linker_1.getAllLinkedFunctionDefinitions)(new Set(defs), graph)[0]);
35
+ }
36
+ return linked;
37
+ }
24
38
  /**
25
39
  * Returns the function call targets (definitions) by the given caller
26
40
  */
27
- function getAllFunctionCallTargetsForSlice(dataflowGraph, callerInfo, baseEnvironment, queue, ctx) {
41
+ function getAllFunctionCallTargetsForSlice(dataflowGraph, callerInfo, baseEnvironment, ctx) {
42
+ (0, assert_1.guard)(dataflowGraph.hasVertex(callerInfo.id), () => `outgoing edges of id: ${callerInfo.id} must be in graph but can not be found, keep in slice to be sure`);
28
43
  // bind with call-local environments during slicing
29
- const outgoingEdges = dataflowGraph.get(callerInfo.id, true);
30
- (0, assert_1.guard)(outgoingEdges !== undefined, () => `outgoing edges of id: ${callerInfo.id} must be in graph but can not be found, keep in slice to be sure`);
31
- // lift baseEnv on the same level
32
44
  const activeEnvironment = callerInfo.environment === undefined ? baseEnvironment : (0, built_in_function_definition_1.retrieveActiveEnvironment)(callerInfo.environment, baseEnvironment, ctx);
33
- const name = callerInfo.name;
34
- (0, assert_1.guard)(name !== undefined, () => `name of id: ${callerInfo.id} can not be found in id map`);
35
- const functionCallDefs = [];
36
- for (const d of resolve_helper_1.Resolve.byName(name, activeEnvironment) ?? []) {
37
- if (!node_id_1.NodeId.isBuiltIn(d.definedAt)) {
38
- functionCallDefs.push(d.nodeId);
39
- }
40
- }
41
- for (const [target, outgoingEdge] of outgoingEdges[1].entries()) {
42
- if (edge_1.DfEdge.includesType(outgoingEdge, edge_1.EdgeType.Calls)) {
43
- functionCallDefs.push(target);
44
- }
45
- }
46
- const functionCallTargets = functionCallDefs.length === 0 ? NoCallTargets :
47
- queue.memoizeCallTargets(functionCallDefs.join(';'), () => (0, linker_1.getAllLinkedFunctionDefinitions)(new Set(functionCallDefs), dataflowGraph)[0]);
45
+ const resolved = (resolve_helper_1.Resolve.byName(callerInfo.name, activeEnvironment) ?? []).filter(d => !node_id_1.NodeId.isBuiltIn(d.definedAt));
46
+ const functionCallDefs = [...resolved.map(d => d.nodeId), ...(0, frame_reflection_1.edgeTargets)(dataflowGraph, callerInfo.id, edge_1.EdgeType.Calls)];
47
+ const functionCallTargets = functionCallDefs.length === 0 ? NoCallTargets : linkedDefinitions(functionCallDefs, dataflowGraph);
48
48
  return [functionCallTargets, activeEnvironment];
49
49
  }
50
- function includeArgumentFunctionCallClosure(arg, activeEnvironment, activeEnvironmentFingerprint, queue, dataflowGraph) {
51
- const valueRoot = graph_1.FunctionArgument.getReference(arg);
52
- if (!valueRoot) {
53
- return;
54
- }
55
- const callTargets = queue.memoizeCallTargets(valueRoot, () => (0, linker_1.getAllLinkedFunctionDefinitions)(new Set([valueRoot]), dataflowGraph)[0]);
56
- linkCallTargets(false, callTargets, activeEnvironment, activeEnvironmentFingerprint, queue);
57
- }
58
50
  function linkCallTargets(onlyForSideEffects, functionCallTargets, activeEnvironment, activeEnvironmentFingerprint, queue) {
59
51
  for (const functionCallTarget of functionCallTargets) {
60
- for (const exitPoint of functionCallTarget.exitPoints) {
61
- queue.add(exitPoint.nodeId, activeEnvironment, activeEnvironmentFingerprint(), onlyForSideEffects);
52
+ /* a call kept only for what it does needs what its function reads, not what it returns */
53
+ for (const exitPoint of onlyForSideEffects ? [] : functionCallTarget.exitPoints) {
54
+ queue.add(exitPoint.nodeId, activeEnvironment, activeEnvironmentFingerprint(), false);
62
55
  }
63
- // handle open reads
64
56
  for (const openIn of functionCallTarget.subflow.in) {
65
- // resolve them in the active env
66
- if (openIn.name) {
67
- const resolved = resolve_helper_1.Resolve.byName(openIn.name, activeEnvironment);
68
- for (const res of resolved ?? []) {
69
- (0, static_slicer_1.updatePotentialAddition)(queue, functionCallTarget.id, res.nodeId, activeEnvironment, activeEnvironmentFingerprint());
70
- }
57
+ for (const res of openIn.name ? resolve_helper_1.Resolve.byName(openIn.name, activeEnvironment) ?? [] : []) {
58
+ (0, static_slicer_1.updatePotentialAddition)(queue, functionCallTarget.id, res.nodeId, activeEnvironment, activeEnvironmentFingerprint());
71
59
  }
72
60
  }
73
61
  }
74
62
  }
75
- /** returns the new threshold hit count */
63
+ /** Links the call `callerInfo` to the definitions it resolves to, or to the closures of its arguments if it resolves to none. */
76
64
  function sliceForCall(current, callerInfo, { graph }, queue, ctx) {
77
- const [functionCallTargets, activeEnvironment] = getAllFunctionCallTargetsForSlice(graph, callerInfo, current.baseEnvironment, queue, ctx);
65
+ const [functionCallTargets, activeEnvironment] = getAllFunctionCallTargetsForSlice(graph, callerInfo, current.baseEnvironment, ctx);
78
66
  /* hashing the environment is expensive, so only a call that links something pays for it */
79
67
  let fingerprint;
80
68
  const activeEnvironmentFingerprint = () => fingerprint ??= activeEnvironment === current.baseEnvironment ? current.envFingerprint : (0, fingerprint_1.envFingerprint)(activeEnvironment);
81
- if (functionCallTargets.size === 0) {
82
- /*
83
- * if we do not have any call to resolve this function, we have to assume that every function passed is actually called!
84
- * hence, we add a new flag and add all argument values to the queue causing directly
85
- * (an assignment only stores the function, and a call handing an argument back unchanged, like `return(f)`,
86
- * did not call it either: what it is bound to is called later, or never)
87
- */
88
- if (vertex_1.DfgVertex.hasOrigin(callerInfo, built_in_proc_name_1.BuiltInProcName.Assignment)) {
89
- return;
90
- }
91
- const outgoing = graph.outgoingEdges(callerInfo.id);
92
- for (const arg of callerInfo.args) {
93
- const reference = graph_1.FunctionArgument.getReference(arg);
94
- const edge = reference === undefined ? undefined : outgoing?.get(reference);
95
- if (edge === undefined || !edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Returns)) {
96
- includeArgumentFunctionCallClosure(arg, activeEnvironment, activeEnvironmentFingerprint, queue, graph);
97
- }
98
- }
69
+ if (functionCallTargets.size > 0) {
70
+ linkCallTargets(current.onlyForSideEffects, functionCallTargets, activeEnvironment, activeEnvironmentFingerprint, queue);
71
+ return;
72
+ }
73
+ /*
74
+ * if we do not have any call to resolve this function, we have to assume that every function passed is actually called!
75
+ * hence, we add all argument values to the queue causing directly
76
+ * (an assignment only stores the function, and a call handing an argument back unchanged, like `return(f)`,
77
+ * did not call it either: what it is bound to is called later, or never)
78
+ */
79
+ if (vertex_1.DfgVertex.hasOrigin(callerInfo, built_in_proc_name_1.BuiltInProcName.Assignment)) {
99
80
  return;
100
81
  }
101
- linkCallTargets(current.onlyForSideEffects, functionCallTargets, activeEnvironment, activeEnvironmentFingerprint, queue);
82
+ for (const arg of callerInfo.args) {
83
+ const reference = graph_1.FunctionArgument.getReference(arg);
84
+ if (reference && !(0, frame_reflection_1.hasEdgeOfType)(graph, callerInfo.id, reference, edge_1.EdgeType.Returns)) {
85
+ linkCallTargets(false, linkedDefinitions([reference], graph), activeEnvironment, activeEnvironmentFingerprint, queue);
86
+ }
87
+ }
102
88
  }
103
89
  /**
104
90
  * Finds the nearest enclosing function-definition node for the given id by walking up the AST parent chain.
@@ -117,31 +103,23 @@ function findEnclosingFunctionDefinition(id, idMap) {
117
103
  */
118
104
  function sliceReachesFunctionInterface(fnDefId, graph, queue, idMap, ctx) {
119
105
  const vertex = graph.getVertex(fnDefId);
120
- if (vertex === undefined || !vertex_1.DfgVertex.isFunctionDefinition(vertex)) {
106
+ if (!vertex_1.DfgVertex.isFunctionDefinition(vertex)) {
121
107
  return false;
122
108
  }
123
109
  // (a) the slice reaches a parameter of this definition
124
- for (const paramId of Object.keys(vertex.params)) {
125
- if (queue.hasId(node_id_1.NodeId.normalize(paramId))) {
126
- return true;
127
- }
110
+ if (Object.keys(vertex.params).some(paramId => queue.hasId(node_id_1.NodeId.normalize(paramId)))) {
111
+ return true;
128
112
  }
129
113
  // (b) a sliced body node captures a variable from the enclosing scope: it links (via `defined-by-on-call`, the
130
114
  // closure/argument binding resolved at the call site) to a non-builtin definition that lives outside this body.
131
- const fnNode = idMap.get(fnDefId);
132
- const bodyIds = fnNode ? new Set(model_1.RNode.collectAllIds(fnNode)) : new Set();
115
+ const bodyIds = model_1.RNode.collectAllIds(idMap.get(fnDefId));
133
116
  for (const bodyId of bodyIds) {
134
117
  if (!queue.hasId(bodyId)) {
135
118
  continue;
136
119
  }
137
- const outgoing = graph.outgoingEdges(bodyId);
138
- if (outgoing === undefined) {
139
- continue;
140
- }
141
- for (const [target, edge] of outgoing) {
142
- if (edge_1.DfEdge.includesType(edge, edge_1.EdgeType.DefinedByOnCall) && !node_id_1.NodeId.isBuiltIn(target) && !bodyIds.has(target)) {
143
- return true;
144
- }
120
+ const captures = (0, frame_reflection_1.edgeTargets)(graph, bodyId, edge_1.EdgeType.DefinedByOnCall);
121
+ if (captures.some(target => !node_id_1.NodeId.isBuiltIn(target) && !bodyIds.has(target))) {
122
+ return true;
145
123
  }
146
124
  }
147
125
  // (c) fallback for captures that could not be resolved at definition time: an open in-reference that is in the
@@ -165,29 +143,19 @@ const CalleeBoundaryEdges = edge_1.EdgeType.DefinedBy | edge_1.EdgeType.Calls;
165
143
  * `calls` edges). Call site arguments are picked up automatically once the call vertex is processed normally,
166
144
  * as `argument` edges are always traversed.
167
145
  * This is the reverse of what {@link sliceForCall} does for call -\> definition linking.
146
+ * @returns the enqueued ids
168
147
  */
169
148
  function includeCalleesOfDefinition(fnDefId, graph, queue, baseEnvironment, baseEnvFingerprint) {
170
- const ingoing = graph.ingoingEdges(fnDefId);
171
- if (ingoing === undefined) {
172
- return;
173
- }
174
- for (const [source, edge] of ingoing) {
175
- if (edge_1.DfEdge.includesType(edge, CalleeBoundaryEdges)) {
176
- queue.add(source, baseEnvironment, baseEnvFingerprint, false);
177
- }
149
+ const added = (0, frame_reflection_1.edgeSources)(graph, fnDefId, CalleeBoundaryEdges);
150
+ for (const source of added) {
151
+ queue.add(source, baseEnvironment, baseEnvFingerprint, false);
178
152
  }
153
+ return added;
179
154
  }
180
155
  const PotentialFollowOnReturn = edge_1.EdgeType.DefinesOnCall | edge_1.EdgeType.DefinedByOnCall | edge_1.EdgeType.Argument;
181
156
  /** Returns true if we found at least one return edge */
182
157
  function handleReturns(from, queue, currentEdges, baseEnvFingerprint, baseEnvironment) {
183
- let returns = false;
184
- for (const edge of currentEdges.values()) {
185
- if (edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Returns)) {
186
- returns = true;
187
- break;
188
- }
189
- }
190
- if (!returns) {
158
+ if (![...currentEdges.values()].some(edge => edge_1.DfEdge.includesType(edge, edge_1.EdgeType.Returns))) {
191
159
  return false;
192
160
  }
193
161
  for (const [target, edge] of currentEdges) {