@eagleoutice/flowr 2.17.1 → 2.17.2

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 (79) hide show
  1. package/control-flow/semantic-cfg-guided-visitor.js +2 -0
  2. package/dataflow/environments/ambient-state-name.d.ts +4 -0
  3. package/dataflow/environments/ambient-state-name.js +4 -0
  4. package/dataflow/environments/built-in-proc-name.d.ts +4 -0
  5. package/dataflow/environments/built-in-proc-name.js +4 -0
  6. package/dataflow/environments/built-in-props.d.ts +2 -0
  7. package/dataflow/environments/built-in-props.js +3 -1
  8. package/dataflow/environments/built-in.d.ts +16 -6
  9. package/dataflow/environments/built-in.js +45 -4
  10. package/dataflow/environments/default-builtin-config.d.ts +846 -21
  11. package/dataflow/environments/default-builtin-config.js +120 -26
  12. package/dataflow/environments/environment.d.ts +19 -2
  13. package/dataflow/environments/environment.js +90 -19
  14. package/dataflow/environments/identifier.d.ts +18 -0
  15. package/dataflow/environments/query-fn-props.js +4 -3
  16. package/dataflow/environments/resolve-by-name.js +5 -3
  17. package/dataflow/eval/resolve/node-value.d.ts +1 -1
  18. package/dataflow/eval/resolve/node-value.js +2 -2
  19. package/dataflow/eval/resolve/resolve-argument.js +1 -1
  20. package/dataflow/extractor.js +20 -4
  21. package/dataflow/graph/df-helper.d.ts +34 -1
  22. package/dataflow/graph/df-helper.js +60 -0
  23. package/dataflow/info.d.ts +2 -0
  24. package/dataflow/info.js +4 -0
  25. package/dataflow/internal/linker.js +21 -5
  26. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-keys.d.ts +4 -0
  27. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-keys.js +3 -0
  28. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.d.ts +18 -2
  29. package/dataflow/internal/process/functions/call/built-in/built-in-ambient-state.js +144 -30
  30. package/dataflow/internal/process/functions/call/built-in/built-in-apply.d.ts +1 -1
  31. package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +2 -1
  32. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +1 -1
  33. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +10 -0
  34. package/dataflow/internal/process/functions/call/built-in/built-in-attach.d.ts +8 -4
  35. package/dataflow/internal/process/functions/call/built-in/built-in-attach.js +43 -8
  36. package/dataflow/internal/process/functions/call/built-in/built-in-env-contents.d.ts +1 -1
  37. package/dataflow/internal/process/functions/call/built-in/built-in-env-contents.js +5 -4
  38. package/dataflow/internal/process/functions/call/built-in/built-in-eval.js +67 -3
  39. package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +4 -2
  40. package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +27 -3
  41. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +1 -1
  42. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +2 -14
  43. package/dataflow/internal/process/functions/call/built-in/built-in-if-then-else.js +1 -7
  44. package/dataflow/internal/process/functions/call/built-in/built-in-library.d.ts +28 -10
  45. package/dataflow/internal/process/functions/call/built-in/built-in-library.js +134 -88
  46. package/dataflow/internal/process/functions/call/built-in/built-in-load.d.ts +1 -1
  47. package/dataflow/internal/process/functions/call/built-in/built-in-load.js +87 -10
  48. package/dataflow/internal/process/functions/call/built-in/built-in-local.d.ts +8 -0
  49. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +23 -12
  50. package/dataflow/internal/process/functions/call/built-in/built-in-source.d.ts +7 -1
  51. package/dataflow/internal/process/functions/call/built-in/built-in-source.js +28 -10
  52. package/dataflow/internal/process/functions/call/built-in/built-in-try-catch.js +1 -1
  53. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +7 -5
  54. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.d.ts +2 -2
  55. package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +18 -15
  56. package/dataflow/internal/process/functions/call/named-call-handling.js +8 -4
  57. package/dataflow/internal/process/functions/call/quoted.d.ts +2 -0
  58. package/dataflow/internal/process/functions/call/quoted.js +8 -7
  59. package/documentation/wiki-dataflow-graph.js +20 -1
  60. package/linter/rules/undefined-symbol.js +2 -2
  61. package/package.json +1 -1
  62. package/project/attached-packages.d.ts +11 -6
  63. package/project/attached-packages.js +37 -9
  64. package/queries/catalog/signature-query/signature-query-executor.js +15 -1
  65. package/queries/catalog/signature-query/signature-query-format.d.ts +3 -2
  66. package/r-bridge/data/data.d.ts +2 -2
  67. package/r-bridge/data/data.js +2 -2
  68. package/r-bridge/lang-4.x/ast/model/model.d.ts +2 -0
  69. package/r-bridge/lang-4.x/ast/model/model.js +6 -1
  70. package/r-bridge/lang-4.x/ast/model/processing/node-id.d.ts +6 -1
  71. package/r-bridge/lang-4.x/ast/model/processing/node-id.js +12 -1
  72. package/reconstruct/reconstruct.js +2 -5
  73. package/search/search-executor/search-enrichers.js +17 -2
  74. package/search/search-executor/search-generators.js +6 -2
  75. package/slicing/static/slice-call.d.ts +16 -0
  76. package/slicing/static/slice-call.js +42 -0
  77. package/slicing/static/static-slicer.js +6 -0
  78. package/util/flowr-version.d.ts +1 -1
  79. package/util/flowr-version.js +2 -2
@@ -1,34 +1,44 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processAttach = processAttach;
4
+ exports.processDetach = processDetach;
4
5
  const r_argument_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
5
6
  const known_call_handling_1 = require("../known-call-handling");
6
7
  const r_symbol_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol");
8
+ const r_string_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string");
7
9
  const identifier_1 = require("../../../../../environments/identifier");
8
10
  const environment_1 = require("../../../../../environments/environment");
9
11
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
10
12
  const unknown_side_effect_1 = require("../../../../../graph/unknown-side-effect");
11
13
  const edge_1 = require("../../../../../graph/edge");
12
14
  const built_in_envir_utils_1 = require("./built-in-envir-utils");
15
+ const built_in_ambient_keys_1 = require("./built-in-ambient-keys");
16
+ const ambient_state_name_1 = require("../../../../../environments/ambient-state-name");
13
17
  const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
14
- const built_in_ambient_state_1 = require("./built-in-ambient-state");
18
+ const AttachableLiterals = new Set(['list', 'data.frame']);
15
19
  /** Processes `attach(what, ...)`, injecting the tracked env's known definitions into a new scope layer below `.GlobalEnv`. */
16
- function processAttach(name, args, rootId, data, config = {}) {
17
- const result = attachLayer(name, args, rootId, data);
18
- return config.ambient === undefined ? result : (0, built_in_ambient_state_1.applyAmbientStateAccess)(result, args, rootId, data, config.ambient);
19
- }
20
- function attachLayer(name, args, rootId, data) {
20
+ function processAttach(name, args, rootId, data,
21
+ /* the search path it writes is applied by the registration, see {@link AmbientConfig} */
22
+ _config = {}) {
21
23
  const result = (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: built_in_proc_name_1.BuiltInProcName.Attach }).information;
22
24
  const whatArg = args.length >= 1 ? args[0] : undefined;
23
25
  const whatNode = whatArg && whatArg !== r_function_call_1.EmptyArgument ? r_argument_1.RArgument.valueOf(whatArg) : undefined;
24
26
  const envirResolution = r_symbol_1.RSymbol.is(whatNode) ? (0, built_in_envir_utils_1.resolveSymbolToEnvir)(whatNode.content, whatNode.info.id, data) : undefined;
25
- if (!envirResolution) {
27
+ /* a list written out names its bindings, `attach(list(a = 1))` binds `a` to `1` */
28
+ const listed = envirResolution === undefined && r_function_call_1.RFunctionCall.isNamed(whatNode, AttachableLiterals)
29
+ && whatNode.arguments.every(a => a !== r_function_call_1.EmptyArgument && a.name !== undefined && a.value !== undefined) ? whatNode.arguments : undefined;
30
+ if (!envirResolution && !listed) {
26
31
  (0, unknown_side_effect_1.handleUnknownSideEffect)(result.graph, result.environment, rootId);
27
32
  return result;
28
33
  }
29
34
  /* build the attached layer with the env's known bindings */
30
35
  const injected = [];
31
- for (const [varName, varDefs] of envirResolution.envDef.envState.current.memory) {
36
+ for (const entry of listed ?? []) {
37
+ const at = entry.value.info.id;
38
+ injected.push({ nodeId: at, name: entry.name?.content, type: identifier_1.ReferenceType.Variable, definedAt: rootId, cds: data.cds ? [...data.cds] : undefined });
39
+ result.graph.addEdge(at, rootId, edge_1.EdgeType.Reads);
40
+ }
41
+ for (const [varName, varDefs] of envirResolution?.envDef.envState.current.memory ?? []) {
32
42
  for (const varDef of varDefs) {
33
43
  const inDef = varDef;
34
44
  injected.push({
@@ -40,10 +50,35 @@ function attachLayer(name, args, rootId, data) {
40
50
  }
41
51
  }
42
52
  const attachedLayer = new environment_1.Environment(result.environment.current).defineAll(injected);
53
+ attachedLayer.t = environment_1.EnvType.Attached;
54
+ /* R names the entry by `name`, else by the variable given as `what` */
55
+ const named = r_argument_1.RArgument.valueOf(args.find(a => a !== r_function_call_1.EmptyArgument && a.name !== undefined && identifier_1.Identifier.getName(a.name.content) === 'name'));
56
+ attachedLayer.n = r_string_1.RString.is(named) ? named.content.str : r_symbol_1.RSymbol.is(whatNode) ? identifier_1.Identifier.getName(whatNode.content) : undefined;
43
57
  /* R attaches below `.GlobalEnv`, so existing global bindings shadow the attached ones */
44
58
  return { ...result, environment: {
45
59
  current: environment_1.REnvironment.attachAt(result.environment.current, attachedLayer, attachedLayer),
46
60
  level: result.environment.level
47
61
  } };
48
62
  }
63
+ /**
64
+ * Processes `detach(name)` and `unloadNamespace(ns)`: the search path entry named (`package:x`, `"x"`, a position, the
65
+ * topmost one if nothing is given) is removed, uncovering what it masked. The entry is read with the keys of the
66
+ * call's search path {@link AmbientConfig|ambient} write.
67
+ */
68
+ function processDetach(name, args, rootId, data, config = {}) {
69
+ const info = (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: built_in_proc_name_1.BuiltInProcName.Detach }).information;
70
+ const keys = [config.ambient ?? []].flat().flatMap(a => a.type === "set-keyed" /* AmbientStateAccessType.SetKeyed */ ? (0, built_in_ambient_keys_1.keysOf)(a.keys, args, data) : []);
71
+ const first = r_argument_1.RArgument.valueOf(args.find(a => a !== r_function_call_1.EmptyArgument));
72
+ const positions = first === undefined ? [environment_1.DefaultAttachPosition]
73
+ : keys.includes(ambient_state_name_1.UnknownKey) ? [(0, built_in_envir_utils_1.resolveAttachPosition)(first.info.id, data)]
74
+ : keys.map(k => environment_1.REnvironment.searchPosition(info.environment.current, k));
75
+ /* what a later read no longer finds still depends on the removal, which only an unknown side effect tells it */
76
+ (0, unknown_side_effect_1.handleUnknownSideEffect)(info.graph, info.environment, rootId);
77
+ if (!positions.includes(undefined)) {
78
+ for (const pos of positions) {
79
+ info.environment = { level: info.environment.level, current: environment_1.REnvironment.detachAt(info.environment.current, pos) };
80
+ }
81
+ }
82
+ return info;
83
+ }
49
84
  //# sourceMappingURL=built-in-attach.js.map
@@ -9,7 +9,7 @@ import type { FnSig } from '../../../../../environments/built-in-props';
9
9
  * Processes a call listing what an environment holds (`ls`, `objects`), which reads every binding in it: the
10
10
  * answer changes with each one, so each has to stay.
11
11
  */
12
- export declare function processEnvContents<OtherInfo>(name: RSymbol<OtherInfo & ParentInformation>, args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], rootId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>, config?: {
12
+ export declare function processEnvContents<OtherInfo>(name: RSymbol<OtherInfo & ParentInformation>, args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], rootId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>, { sig }?: {
13
13
  sig?: FnSig;
14
14
  }): DataflowInformation;
15
15
  /**
@@ -18,13 +18,14 @@ const environment_1 = require("../../../../../environments/environment");
18
18
  * Processes a call listing what an environment holds (`ls`, `objects`), which reads every binding in it: the
19
19
  * answer changes with each one, so each has to stay.
20
20
  */
21
- function processEnvContents(name, args, rootId, data, config = {}) {
21
+ function processEnvContents(name, args, rootId, data, { sig } = {}) {
22
22
  const result = (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: built_in_proc_name_1.BuiltInProcName.EnvContents }).information;
23
- const resolution = (0, built_in_envir_utils_1.resolveFirstEnvirArg)(args, data, config.sig, ['envir', 'name']);
24
- if (!resolution) {
23
+ const formals = ['envir', 'name'];
24
+ const resolution = (0, built_in_envir_utils_1.resolveFirstEnvirArg)(args, data, sig, formals);
25
+ if (!resolution && (0, built_in_envir_utils_1.suppliesArg)(args, sig, formals)) {
25
26
  return result;
26
27
  }
27
- for (const defs of resolution.envDef.envState.current.memory.values()) {
28
+ for (const defs of (resolution?.envDef.envState ?? data.environment).current.memory.values()) {
28
29
  (0, linker_1.linkFieldReads)(result.graph, rootId, defs, false);
29
30
  }
30
31
  return result;
@@ -23,6 +23,10 @@ const vertex_1 = require("../../../../../graph/vertex");
23
23
  const model_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/model");
24
24
  const resolve_helper_1 = require("../../../../../environments/resolve-helper");
25
25
  const built_in_envir_utils_1 = require("./built-in-envir-utils");
26
+ const built_in_local_1 = require("./built-in-local");
27
+ const processor_1 = require("../../../../../processor");
28
+ const environment_1 = require("../../../../../environments/environment");
29
+ const quoted_1 = require("../quoted");
26
30
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
27
31
  const r_string_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string");
28
32
  const r_function_call_2 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
@@ -61,6 +65,10 @@ function processEvalCall(name, args, rootId, data, config) {
61
65
  if (config.parentFrame || namesParentFrame(envirArg?.value, data)) {
62
66
  escapeWritesToParentFrame(evalArgument, rootId, data, information);
63
67
  }
68
+ const quoted = config.supportFunctionCall ? undefined : quotedCodeOf(evalArgument, data);
69
+ if (quoted !== undefined) {
70
+ return evaluateQuoted(quoted, config.parentFrame || namesParentFrame(envirArg?.value, data) ? undefined : envirArg, rootId, information, data);
71
+ }
64
72
  const code = resolveEvalToCode(evalArgument, config, data);
65
73
  if (code) {
66
74
  if (envirArg !== undefined) {
@@ -91,6 +99,64 @@ function processEvalCall(name, args, rootId, data, config) {
91
99
  /* a captured expression is linked once the graph is complete, but the state its calls access has to show now */
92
100
  return (0, built_in_ambient_state_1.evaluatesCapturedCode)(information, rootId, data);
93
101
  }
102
+ /** the entry in {@link DataflowProcessorInformation#referenceChain} of evaluating `code`, so code evaluating itself (`q <- quote(eval(q))`) is not unrolled forever */
103
+ function evaluationOf(code) {
104
+ return `eval:${String(code)}`;
105
+ }
106
+ /**
107
+ * The code `eval` runs, if it is written down: what a quoting call captures (`eval(quote(x <- 1))`), or what each
108
+ * binding of a name captures (`q <- quote(x <- 1); eval(q)`).
109
+ */
110
+ function quotedCodeOf(node, data) {
111
+ const val = (0, built_in_envir_utils_1.pipedCall)(node, data) ?? node;
112
+ const code = r_symbol_1.RSymbol.is(val)
113
+ ? resolve_helper_1.Resolve.byNameAndType(val.content, data.environment, identifier_1.ReferenceType.Variable)?.map(d => capturedBy(boundValueOf(d.nodeId, data), data))
114
+ : [capturedBy(val, data)];
115
+ return code === undefined || code.length === 0 || code.some(assert_1.isUndefined) ? undefined : code;
116
+ }
117
+ /**
118
+ * The one expression a call to a built-in capturing code (`quote`, `bquote`, `expression`) holds on to, unless it is
119
+ * being evaluated already; `substitute(expr, env)` replaces names before anything runs.
120
+ */
121
+ function capturedBy(node, data) {
122
+ if (!r_function_call_1.RFunctionCall.isNamed(node)) {
123
+ return undefined;
124
+ }
125
+ const defs = resolve_helper_1.Resolve.byNameAndType(node.functionName.content, data.environment, identifier_1.ReferenceType.Function);
126
+ const target = defs?.length === 1 && defs[0].type === identifier_1.ReferenceType.BuiltInFunction ? defs[0] : undefined;
127
+ const index = target?.processorName === built_in_proc_name_1.BuiltInProcName.Quote ? target.config?.quoteArgumentsWithIndex
128
+ : target !== undefined && quoted_1.CapturingCalls.has(identifier_1.Identifier.getName(node.functionName.content)) ? 0 : undefined;
129
+ const code = index === undefined || node.arguments.length !== index + 1 ? undefined : r_argument_1.RArgument.valueOf(node.arguments[index]);
130
+ return code === undefined || data.referenceChain.includes(evaluationOf(code.info.id)) ? undefined : code;
131
+ }
132
+ /** the value a definition binds, `quote(x)` for `q` in `q <- quote(x)` */
133
+ function boundValueOf(target, data) {
134
+ const assignment = data.completeAst.idMap.get(data.completeAst.idMap.get(target)?.info.parent ?? '');
135
+ return r_binary_op_1.RBinaryOp.is(assignment) ? assignment.lhs.info.id === target ? assignment.rhs : assignment.lhs : undefined;
136
+ }
137
+ /** Runs the quoted `code` where `eval` runs it: right here, or in the environment `envir` names (see {@link evaluateInEnvir}); with several candidates, each may be the one. */
138
+ function evaluateQuoted(code, envir, rootId, information, data) {
139
+ const routing = envir === undefined ? undefined : (0, built_in_envir_utils_1.resolveArgToEnvirOrAmbiguous)(envir, data);
140
+ const at = { ...data, environment: information.environment, cds: code.length > 1 ? [...(data.cds ?? []), { id: rootId, when: true }] : data.cds };
141
+ const results = code.map(c => {
142
+ const inner = { ...at, referenceChain: [...at.referenceChain, evaluationOf(c.info.id)] };
143
+ return envir === undefined ? (0, processor_1.processDataflowFor)(c, inner) : (0, built_in_local_1.evaluateInEnvir)(c, (0, built_in_envir_utils_1.envirOf)(routing), rootId, inner);
144
+ });
145
+ const result = {
146
+ ...information,
147
+ graph: results.reduce((acc, r) => acc.mergeWith(r.graph), information.graph),
148
+ environment: results.map(r => r.environment).reduce((acc, e) => environment_1.REnvironment.append(acc, e)),
149
+ in: information.in.concat(results.flatMap(r => r.in), results.flatMap(r => r.unknownReferences)),
150
+ out: information.out.concat(results.flatMap(r => r.out)),
151
+ exitPoints: information.exitPoints.concat(results.flatMap(r => r.exitPoints)),
152
+ hooks: information.hooks.concat(results.flatMap(r => r.hooks))
153
+ };
154
+ for (const r of results) {
155
+ result.graph.addEdge(rootId, r.entryPoint, edge_1.EdgeType.Returns);
156
+ }
157
+ (0, built_in_envir_utils_1.unknownIfAmbiguous)(routing, result, rootId);
158
+ return result;
159
+ }
94
160
  function namesParentFrame(envir, data) {
95
161
  return r_function_call_1.RFunctionCall.isNamed(envir, 'parent.frame')
96
162
  && resolve_helper_1.Resolve.isBuiltIn(envir.functionName.content, data.environment, identifier_1.ReferenceType.Function);
@@ -147,9 +213,7 @@ function resolveEvalToCode(evalArgument, config, data) {
147
213
  else if (r_symbol_1.RSymbol.is(val)) {
148
214
  /* `nm <- as.name("w"); eval(nm)`: a name bound once to code we can read is that code */
149
215
  const defs = resolve_helper_1.Resolve.byName(val.content, data.environment);
150
- const target = defs?.length === 1 ? defs[0].nodeId : undefined;
151
- const assignment = target === undefined ? undefined : data.completeAst.idMap.get(data.completeAst.idMap.get(target)?.info.parent ?? '');
152
- const source = r_binary_op_1.RBinaryOp.is(assignment) ? assignment.lhs.info.id === target ? assignment.rhs : assignment.lhs : undefined;
216
+ const source = defs?.length === 1 ? boundValueOf(defs[0].nodeId, data) : undefined;
153
217
  return source === undefined || r_symbol_1.RSymbol.is(source) ? undefined : resolveEvalToCode(source, config, data);
154
218
  }
155
219
  else {
@@ -217,7 +217,7 @@ function packageLayersOf(env) {
217
217
  }
218
218
  return layers;
219
219
  }
220
- function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextGraph, localDefs, idMap) {
220
+ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextGraph, localDefs, idMap, listEnvironments) {
221
221
  for (const { functionCall, called } of calledEnvs) {
222
222
  const callDependencies = nextGraph.getVertex(functionCall)?.cds;
223
223
  for (const { fn: calledFn, direct, cds } of transitivelyCalledDefinitions(called, nextGraph, inputEnvironment)) {
@@ -254,6 +254,8 @@ function updateSideEffectsForCalledFunctions(calledEnvs, inputEnvironment, nextG
254
254
  if (!node_id_1.NodeId.isBuiltIn(def.definedAt)) {
255
255
  hasUpdate = true;
256
256
  nextGraph.addEdge(def.nodeId, functionCall, edge_1.EdgeType.SideEffectOnCall);
257
+ /* what a call of the list writes is written by the list, a later read of it looks no further */
258
+ listEnvironments.add(def.nodeId);
257
259
  }
258
260
  }
259
261
  }
@@ -357,7 +359,7 @@ function processExpressionList(name, args, rootId, data) {
357
359
  (0, info_1.addNonDefaultExitPoints)(exitPoints, invertExitCds, data.cds, processed.exitPoints);
358
360
  environment = exitPoints.length > 0 ? environment_1.REnvironment.overwrite(environment, processed.environment) : processed.environment;
359
361
  // if the called function has global redefinitions, we have to keep them within our environment
360
- environment = updateSideEffectsForCalledFunctions(calledEnvs, environment, nextGraph, processed.out, idMap);
362
+ environment = updateSideEffectsForCalledFunctions(calledEnvs, environment, nextGraph, processed.out, idMap, listEnvironments);
361
363
  // removals are already reflected in the threaded environment; we only bubble them (net of later writes)
362
364
  if (killed && processed.out.length > 0) {
363
365
  killed = (0, apply_kill_1.cancelRevivedKills)(killed, processed.out);
@@ -335,7 +335,8 @@ function updateNestedFunctionClosures(graph, outEnvironment, openReads) {
335
335
  if (allBuiltIn && resolved.length > 0 && !allClosureWrites) {
336
336
  settled.add(ingoing.nodeId);
337
337
  }
338
- return allBuiltIn || allClosureWrites;
338
+ /* the state of the session (options, files, the working directory) is what it is where the function is called, so it stays open for its calls */
339
+ return allBuiltIn || allClosureWrites || ambient_state_name_1.AmbientStateName.is(ingoing.name);
339
340
  });
340
341
  for (const ref of subflow.in) {
341
342
  if (ref.name !== undefined && !settled.has(ref.nodeId) && !open.has(ref.nodeId)) {
@@ -373,6 +374,14 @@ function namesShadowingBuiltIns(environment) {
373
374
  const shadowing = new Set();
374
375
  const generics = new Set();
375
376
  for (const env of environment_1.REnvironment.frames(environment.current)) {
377
+ /* an attached package may shadow a built-in a function body bound to before it was attached; its keys
378
+ * suffice, building the definitions of every export would be costly */
379
+ if (env.t === environment_1.EnvType.Namespace) {
380
+ for (const identifier of env.memory.keys()) {
381
+ shadowing.add(identifier_1.Identifier.getName(identifier));
382
+ }
383
+ continue;
384
+ }
376
385
  for (const [identifier, definitions] of env.memory) {
377
386
  if (definitions.every(d => node_id_1.NodeId.isBuiltIn(d.nodeId))) {
378
387
  continue;
@@ -550,6 +559,19 @@ function updateNestedFunctionCalls(graph, outEnvironment, ctx) {
550
559
  }
551
560
  const global = enclosingOf(target) === undefined;
552
561
  targetVertex.subflow.in = resolveIngoingRefs(targetVertex.subflow.in, effectiveEnvironment, (ingoing, resolved) => {
562
+ /* the state of the session (options, files, the working directory) is what it is at this very call, and what it was before the calls of the enclosing function */
563
+ if (ambient_state_name_1.AmbientStateName.is(ingoing.name)) {
564
+ for (const { nodeId } of (vertex.environment && resolve_helper_1.Resolve.byNameAndType(ingoing.name, vertex.environment, ingoing.type)) ?? []) {
565
+ if (!node_id_1.NodeId.isBuiltIn(nodeId)) {
566
+ graph.addEdge(ingoing.nodeId, nodeId, edge_1.EdgeType.DefinedByOnCall);
567
+ graph.addEdge(id, nodeId, edge_1.EdgeType.DefinesOnCall);
568
+ }
569
+ }
570
+ if (enclosing !== undefined) {
571
+ lifted.push([enclosing, ingoing, target]);
572
+ }
573
+ return false;
574
+ }
553
575
  if (enclosing !== undefined && resolved.some(d => !node_id_1.NodeId.isBuiltIn(d.nodeId)) && (global || resolved.every(d => node_id_1.NodeId.isBuiltIn(d.nodeId) || graph.isRoot(d.nodeId)))) {
554
576
  lifted.push([enclosing, ingoing, target]);
555
577
  return false;
@@ -607,14 +629,16 @@ function updateNestedFunctionCalls(graph, outEnvironment, ctx) {
607
629
  }
608
630
  /** Resolves each global read of a nested call where the enclosing function is called. */
609
631
  function linkLiftedReads(graph, lifted, enclosingOf, environmentAt, runsOnceAt) {
632
+ /* a call reads several states under its own id (`#wd`, `#file:f`), so a read is told apart by its name as well */
610
633
  const seen = new defaultmap_1.DefaultMap(() => new Set());
611
634
  while (lifted.length > 0) {
612
635
  const [definition, read, holder] = lifted.pop();
613
636
  const known = seen.get(definition);
614
- if (known.has(read.nodeId) || read.name === undefined) {
637
+ const key = `${String(read.nodeId)}|${read.name === undefined ? '' : identifier_1.Identifier.toString(read.name)}`;
638
+ if (known.has(key) || read.name === undefined) {
615
639
  continue;
616
640
  }
617
- known.add(read.nodeId);
641
+ known.add(key);
618
642
  for (const call of df_helper_1.Dataflow.sourcesOf(graph, definition, edge_1.EdgeType.Calls)) {
619
643
  if (!vertex_1.DfgVertex.isFunctionCall(graph.getVertex(call))) {
620
644
  continue;
@@ -1,7 +1,7 @@
1
1
  import type { DataflowProcessorInformation } from '../../../../../processor';
2
2
  import type { DataflowInformation } from '../../../../../info';
3
3
  import type { ParentInformation } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/decorate';
4
- import { type PotentiallyEmptyRArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
4
+ import type { PotentiallyEmptyRArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
5
5
  import type { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
6
6
  import type { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
7
7
  import { type FnSig } from '../../../../../environments/built-in-props';
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.processGet = processGet;
4
4
  const known_call_handling_1 = require("../known-call-handling");
5
5
  const make_argument_1 = require("../argument/make-argument");
6
- const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
7
6
  const logger_1 = require("../../../../../logger");
8
7
  const type_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/type");
9
8
  const edge_1 = require("../../../../../graph/edge");
@@ -15,23 +14,12 @@ const built_in_envir_utils_1 = require("./built-in-envir-utils");
15
14
  const range_1 = require("../../../../../../util/range");
16
15
  const r_string_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string");
17
16
  const r_argument_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
18
- const resolve_helper_1 = require("../../../../../environments/resolve-helper");
17
+ const node_value_1 = require("../../../../../eval/resolve/node-value");
19
18
  const assert_1 = require("../../../../../../util/assert");
20
19
  const built_in_props_1 = require("../../../../../environments/built-in-props");
21
20
  function namesDenotedBy(node, data) {
22
21
  const folded = (0, built_in_envir_utils_1.resolveConstantString)(node, data);
23
- if (folded !== undefined) {
24
- return [folded];
25
- }
26
- if (r_function_call_1.RFunctionCall.isNamed(node, 'c')
27
- && resolve_helper_1.Resolve.isBuiltIn(node.functionName.content, data.environment, identifier_1.ReferenceType.Function)) {
28
- const parts = node.arguments.map(arg => {
29
- const value = r_argument_1.RArgument.valueOf(arg);
30
- return value === undefined ? undefined : (0, built_in_envir_utils_1.resolveConstantString)(value, data);
31
- });
32
- return parts.every(assert_1.isNotUndefined) ? parts : [];
33
- }
34
- return [];
22
+ return folded === undefined ? node_value_1.NodeValue.stringsOf(node.info.id, data) ?? [] : [folded];
35
23
  }
36
24
  /** One symbol per constant name the first argument of `get` denotes, so the lookup is processed like a plain read of them. */
37
25
  function synthesizeTargets(retrieve, name, rootId, data) {
@@ -15,7 +15,6 @@ const node_value_1 = require("../../../../../eval/resolve/node-value");
15
15
  const reference_to_maybe_1 = require("../../../../../environments/reference-to-maybe");
16
16
  const apply_kill_1 = require("../../../../../environments/apply-kill");
17
17
  const model_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/model");
18
- const r_function_definition_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-definition");
19
18
  const r_argument_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
20
19
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
21
20
  const environment_1 = require("../../../../../environments/environment");
@@ -32,11 +31,6 @@ function conditionalArgsOf(config, args) {
32
31
  const argMaps = function_semantics_1.FunctionSemantics.call.match.toSpec((0, common_1.convertFnArguments)(args), params);
33
32
  return ['cond', 'yes', 'no'].map(k => r_argument_1.RArgument.valueOf(r_argument_1.RArgument.getWithId(args, argMaps.get(k)?.[0])));
34
33
  }
35
- /** Whether `id` may run again in the same frame, as part of a loop not left through a function definition. */
36
- function withinLoop(id, idMap) {
37
- const enclosing = model_1.RNode.findEnclosing(id, idMap, n => r_function_definition_1.RFunctionDefinition.is(n) || model_1.RLoopConstructs.is(n));
38
- return enclosing !== undefined && !r_function_definition_1.RFunctionDefinition.is(idMap.get(enclosing));
39
- }
40
34
  /**
41
35
  * Processes an if-then-else built-in function call.
42
36
  * For example, `if(cond) thenExpr else elseExpr` and `if(cond) thenExpr`.
@@ -60,7 +54,7 @@ function processIfThenElse(name, args, rootId, data, config) {
60
54
  const originalDependency = data.cds?.slice();
61
55
  data = { ...data, environment: cond.environment };
62
56
  /* within a loop, what the condition reads may change after it, for the next iteration */
63
- const mayChange = !(0, resolve_1.onlyLiterals)(condArg) && withinLoop(rootId, data.completeAst.idMap);
57
+ const mayChange = !(0, resolve_1.onlyLiterals)(condArg) && model_1.RLoopConstructs.encloses(rootId, data.completeAst.idMap);
64
58
  const values = mayChange ? undefined : node_value_1.NodeValue.setOf(condArg.info.id, data);
65
59
  /*
66
60
  * `ifelse` and its relatives are ordinary functions, so R evaluates every argument whatever the condition
@@ -1,16 +1,16 @@
1
1
  import type { DataflowProcessorInformation } from '../../../../../processor';
2
- import type { DataflowInformation, ControlDependency } from '../../../../../info';
2
+ import { type DataflowInformation, ControlDependency } from '../../../../../info';
3
3
  import type { DataflowGraph } from '../../../../../graph/graph';
4
4
  import type { ParentInformation } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/decorate';
5
5
  import type { PotentiallyEmptyRArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
6
6
  import { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
7
7
  import { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
8
+ import type { AmbientConfig } from '../../../../../environments/built-in';
8
9
  import type { REnvironmentInformation } from '../../../../../environments/environment';
9
10
  import type { FlowrAnalyzerContext } from '../../../../../../project/context/flowr-analyzer-context';
10
11
  import { Package } from '../../../../../../project/plugins/package-version-plugins/package';
11
- import { type AmbientStateAccess } from './built-in-ambient-state';
12
12
  /** Controls how {@link processLibrary} brings a package into scope. */
13
- export interface LibraryProcessorConfig {
13
+ export interface LibraryProcessorConfig extends AmbientConfig {
14
14
  /** `requireNamespace("pkg")` / `loadNamespace("pkg")`: load without attaching bare names */
15
15
  readonly namespaceOnly?: boolean;
16
16
  /** the package argument is evaluated, not taken as a symbol (`requireNamespace`/`loadNamespace`/`attachNamespace`, unlike `library`) */
@@ -19,11 +19,19 @@ export interface LibraryProcessorConfig {
19
19
  readonly fromImports?: boolean;
20
20
  /** `box::use(pkg[a, b])`: attach only the symbols listed in the `[...]` bracket */
21
21
  readonly boxUse?: boolean;
22
- /** the state the call changes besides the environment, like the search path */
23
- readonly ambient?: AmbientStateAccess | readonly AmbientStateAccess[];
22
+ /** every unnamed argument names a package (`pacman::p_load(a, b)`), as the formals after `...` only match by name */
23
+ readonly packagesInDots?: boolean;
24
+ /** a package may be given as `user/repo` (`librarian::shelf`), naming `repo` */
25
+ readonly stripRepo?: boolean;
26
+ /** a formal whose value always names packages, like `pacman::p_load(char = pkgs)` */
27
+ readonly evaluatedIn?: string;
28
+ /** with {@link packagesInDots}, the name of a named argument names a package as well, `needs::needs(dplyr = "1.0")` */
29
+ readonly namesInDots?: boolean;
30
+ /** the formals in order, the first one naming the package, for a loader whose other formals differ from `library`'s */
31
+ readonly formals?: readonly string[];
24
32
  }
25
33
  /** Restricts/aliases which exports of a package are attached; `undefined` fields attach every export. */
26
- interface AttachSpec {
34
+ export interface AttachSpec {
27
35
  readonly namespaceOnly?: boolean;
28
36
  /** attached exports as attachedName to exportName (`import::from`/`box::use` selection or aliasing) */
29
37
  readonly include?: ReadonlyMap<string, string>;
@@ -41,10 +49,21 @@ export declare function processLibrary<OtherInfo>(name: RSymbol<OtherInfo & Pare
41
49
  /** Materialize the empty built-in function-definition vertex for a package export (idempotent). */
42
50
  export declare function attachExportVertex(graph: DataflowGraph, builtInId: NodeId, environment: REnvironmentInformation, ctx: FlowrAnalyzerContext, cds?: readonly ControlDependency[]): void;
43
51
  /**
44
- * The load calls (`library()`/`require()`) that brought package `pack` into scope without a database, collected from
45
- * the {@link libraryLoadMarker} of every matching {@link EnvType.LoadedNamespace} layer below the global environment.
52
+ * Attaches `pack` as `library(pack)` does: its resolved exports, else what flowR states about it, else a bare
53
+ * {@link EnvType.LoadedNamespace} recording the load. The `library()` processor and `Dataflow.library` both use this.
46
54
  */
47
- export declare function loadNodesForNamespace(env: REnvironmentInformation, pack: string): NodeId[];
55
+ export declare function attachLibrary(pack: string, dependency: Package | undefined, envInfo: REnvironmentInformation, ctx: FlowrAnalyzerContext, spec?: AttachSpec, loadedAt?: NodeId, cds?: readonly ControlDependency[]): REnvironmentInformation;
56
+ /**
57
+ * The load calls (`library()`/`require()`) that brought package `pack` (any package if not given) into scope without a
58
+ * database, collected from the {@link libraryLoadMarker} of every matching {@link EnvType.LoadedNamespace} layer below the
59
+ * global environment. A call nothing resolves may go to any of them.
60
+ */
61
+ export declare function loadNodesForNamespace(env: REnvironmentInformation, pack?: string): NodeId[];
62
+ /**
63
+ * Processes `conflicted::conflict_prefer(name, winner)` and `conflicts_prefer(pkg::fn, ...)`: the preferred function is
64
+ * put on top of the search path, as conflicted does with its `.conflicts` entry, so the name resolves to it from then on.
65
+ */
66
+ export declare function processPrefer<OtherInfo>(name: RSymbol<OtherInfo & ParentInformation>, args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], rootId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>, _config?: AmbientConfig): DataflowInformation;
48
67
  /**
49
68
  * Attaches `dependency`'s exports at `spec`'s {@link AttachSpec#pos|search position} (below global by default) and
50
69
  * returns the enriched environment (graph untouched). Used by `library()`, `import::from`, `box::use`, `requireNamespace`, and transitive side-effect propagation.
@@ -75,4 +94,3 @@ export declare function attachAssumedPackages(env: REnvironmentInformation, ctx:
75
94
  export declare function defineProjectSysdata(env: REnvironmentInformation, ctx: FlowrAnalyzerContext): REnvironmentInformation;
76
95
  /** attach every project-level environment layer in order: base R namespaces, the project's own `importFrom` symbols, its declared dependencies, whatever the configuration assumes attached, then the package's own system data */
77
96
  export declare function attachProject(env: REnvironmentInformation, ctx: FlowrAnalyzerContext): REnvironmentInformation;
78
- export {};