@eagleoutice/flowr 2.10.5 → 2.10.6

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 (77) hide show
  1. package/README.md +20 -20
  2. package/abstract-interpretation/absint-visitor.d.ts +1 -2
  3. package/abstract-interpretation/domains/state-domain-like.d.ts +2 -2
  4. package/config.d.ts +6 -0
  5. package/config.js +2 -0
  6. package/control-flow/basic-cfg-guided-visitor.d.ts +1 -1
  7. package/control-flow/semantic-cfg-guided-visitor.js +3 -0
  8. package/dataflow/environments/built-in-proc-name.d.ts +7 -1
  9. package/dataflow/environments/built-in-proc-name.js +6 -0
  10. package/dataflow/environments/built-in.d.ts +6 -0
  11. package/dataflow/environments/built-in.js +6 -0
  12. package/dataflow/environments/default-builtin-config.d.ts +53 -1
  13. package/dataflow/environments/default-builtin-config.js +13 -3
  14. package/dataflow/environments/identifier.d.ts +14 -0
  15. package/dataflow/graph/vertex.d.ts +6 -0
  16. package/dataflow/internal/process/functions/call/built-in/built-in-access.js +16 -0
  17. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.d.ts +6 -0
  18. package/dataflow/internal/process/functions/call/built-in/built-in-assignment.js +99 -2
  19. package/dataflow/internal/process/functions/call/built-in/built-in-attach.d.ts +13 -0
  20. package/dataflow/internal/process/functions/call/built-in/built-in-attach.js +48 -0
  21. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.d.ts +75 -0
  22. package/dataflow/internal/process/functions/call/built-in/built-in-envir-utils.js +196 -0
  23. package/dataflow/internal/process/functions/call/built-in/built-in-get.d.ts +5 -0
  24. package/dataflow/internal/process/functions/call/built-in/built-in-get.js +27 -13
  25. package/dataflow/internal/process/functions/call/built-in/built-in-local.js +14 -4
  26. package/dataflow/internal/process/functions/call/built-in/built-in-new-env.d.ts +27 -0
  27. package/dataflow/internal/process/functions/call/built-in/built-in-new-env.js +97 -0
  28. package/dataflow/internal/process/functions/call/built-in/built-in-with.d.ts +23 -0
  29. package/dataflow/internal/process/functions/call/built-in/built-in-with.js +90 -0
  30. package/documentation/doc-util/doc-types.d.ts +11 -2
  31. package/documentation/doc-util/doc-types.js +31 -9
  32. package/documentation/wiki-absint.d.ts +16 -0
  33. package/documentation/wiki-absint.js +50 -15
  34. package/documentation/wiki-interface.js +1 -0
  35. package/documentation/wiki-mk/doc-context.d.ts +14 -1
  36. package/documentation/wiki-mk/doc-context.js +6 -0
  37. package/documentation/wiki-normalized-ast.d.ts +0 -9
  38. package/documentation/wiki-normalized-ast.js +9 -60
  39. package/linter/linter-rules.d.ts +2 -1
  40. package/linter/rules/problematic-inputs.d.ts +9 -4
  41. package/linter/rules/problematic-inputs.js +108 -58
  42. package/package.json +1 -1
  43. package/queries/catalog/dependencies-query/function-info/read-functions.js +2 -0
  44. package/queries/catalog/dependencies-query/function-info/write-functions.js +1 -0
  45. package/queries/catalog/input-sources-query/input-source-functions.d.ts +1 -0
  46. package/queries/catalog/input-sources-query/input-source-functions.js +7 -1
  47. package/queries/catalog/input-sources-query/input-sources-query-format.js +3 -1
  48. package/queries/catalog/input-sources-query/simple-input-classifier.d.ts +3 -1
  49. package/queries/catalog/input-sources-query/simple-input-classifier.js +3 -1
  50. package/r-bridge/data/data.d.ts +28 -2
  51. package/r-bridge/data/data.js +34 -2
  52. package/r-bridge/lang-4.x/ast/model/model.d.ts +1 -2
  53. package/r-bridge/lang-4.x/ast/model/model.js +0 -1
  54. package/r-bridge/lang-4.x/ast/model/nodes/r-access.d.ts +3 -3
  55. package/r-bridge/lang-4.x/ast/model/nodes/r-argument.d.ts +3 -3
  56. package/r-bridge/lang-4.x/ast/model/nodes/r-binary-op.d.ts +3 -3
  57. package/r-bridge/lang-4.x/ast/model/nodes/r-break.d.ts +3 -3
  58. package/r-bridge/lang-4.x/ast/model/nodes/r-comment.d.ts +3 -3
  59. package/r-bridge/lang-4.x/ast/model/nodes/r-expression-list.d.ts +3 -3
  60. package/r-bridge/lang-4.x/ast/model/nodes/r-for-loop.d.ts +3 -3
  61. package/r-bridge/lang-4.x/ast/model/nodes/r-function-call.d.ts +3 -3
  62. package/r-bridge/lang-4.x/ast/model/nodes/r-function-definition.d.ts +3 -3
  63. package/r-bridge/lang-4.x/ast/model/nodes/r-if-then-else.d.ts +3 -3
  64. package/r-bridge/lang-4.x/ast/model/nodes/r-line-directive.d.ts +3 -3
  65. package/r-bridge/lang-4.x/ast/model/nodes/r-logical.d.ts +3 -3
  66. package/r-bridge/lang-4.x/ast/model/nodes/r-next.d.ts +3 -3
  67. package/r-bridge/lang-4.x/ast/model/nodes/r-number.d.ts +3 -3
  68. package/r-bridge/lang-4.x/ast/model/nodes/r-parameter.d.ts +3 -3
  69. package/r-bridge/lang-4.x/ast/model/nodes/r-pipe.d.ts +3 -3
  70. package/r-bridge/lang-4.x/ast/model/nodes/r-repeat-loop.d.ts +3 -3
  71. package/r-bridge/lang-4.x/ast/model/nodes/r-string.d.ts +3 -3
  72. package/r-bridge/lang-4.x/ast/model/nodes/r-symbol.d.ts +3 -3
  73. package/r-bridge/lang-4.x/ast/model/nodes/r-unary-op.d.ts +3 -3
  74. package/r-bridge/lang-4.x/ast/model/nodes/r-while-loop.d.ts +3 -3
  75. package/util/version.js +1 -1
  76. package/abstract-interpretation/normalized-ast-fold.d.ts +0 -119
  77. package/abstract-interpretation/normalized-ast-fold.js +0 -181
@@ -18,12 +18,14 @@ const vertex_1 = require("../../../../../graph/vertex");
18
18
  const define_1 = require("../../../../../environments/define");
19
19
  const edge_1 = require("../../../../../graph/edge");
20
20
  const resolve_by_name_1 = require("../../../../../environments/resolve-by-name");
21
+ const built_in_envir_utils_1 = require("./built-in-envir-utils");
21
22
  const named_call_handling_1 = require("../named-call-handling");
22
23
  const built_in_1 = require("../../../../../environments/built-in");
23
24
  const unknown_side_effect_1 = require("../../../../../graph/unknown-side-effect");
24
25
  const alias_tracking_1 = require("../../../../../eval/resolve/alias-tracking");
25
26
  const r_value_1 = require("../../../../../eval/values/r-value");
26
27
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
28
+ const built_in_new_env_1 = require("./built-in-new-env");
27
29
  function toReplacementSymbol(target, prefix, superAssignment) {
28
30
  return {
29
31
  type: type_1.RType.Symbol,
@@ -104,6 +106,13 @@ args, rootId, data, config) {
104
106
  logger_1.dataflowLogger.warn(`Assignment ${identifier_1.Identifier.toString(name.content)} has something else than 2 arguments, skipping`);
105
107
  return (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, forceArgs: config.forceArgs, origin: 'default' }).information;
106
108
  }
109
+ /* route into a custom environment when envir resolves to a tracked env variable */
110
+ if (config.environmentArg) {
111
+ const routed = tryRouteToCustomEnv(name, args, rootId, data, config);
112
+ if (routed !== undefined) {
113
+ return routed;
114
+ }
115
+ }
107
116
  const effectiveArgs = getEffectiveOrder(config, args);
108
117
  const { target, source } = extractSourceAndTarget(effectiveArgs);
109
118
  if (target === undefined || source === undefined) {
@@ -172,6 +181,10 @@ args, rootId, data, config) {
172
181
  else if (config.canBeReplacement && type === type_1.RType.Access) {
173
182
  logger_1.dataflowLogger.debug(`Assignment ${identifier_1.Identifier.toString(name.content)} has an access-type node as target ==> replacement function ${target.lexeme}`);
174
183
  const replacement = toReplacementSymbol(target, target.operator, config.superAssignment ?? false);
184
+ const envRouted = tryRouteDollarEnvAssign(rootId, data, config, target, source, replacement);
185
+ if (envRouted !== undefined) {
186
+ return envRouted;
187
+ }
175
188
  return tryReplacement(rootId, replacement, data, replacement.content, [(0, make_argument_1.toUnnamedArgument)(target.accessed, data.completeAst.idMap), ...target.access, source]);
176
189
  }
177
190
  else if (type === type_1.RType.Access) {
@@ -241,11 +254,12 @@ function processAssignmentToString(target, args, name, rootId, data, config, sou
241
254
  lexeme: target.lexeme,
242
255
  location: target.location,
243
256
  };
244
- // treat first argument to Symbol
257
+ // treat first argument to Symbol; include extra args (e.g. envir=) so they get graph vertices
258
+ const extraArgs = config.mayHaveMoreArgs ? Array.from(args).slice(2) : [];
245
259
  const mappedArgs = config.swapSourceAndTarget ? [args[0], {
246
260
  ...args[1],
247
261
  value: symbol
248
- }] : [{ ...args[0], value: symbol }, args[1]];
262
+ }, ...extraArgs] : [{ ...args[0], value: symbol }, args[1], ...extraArgs];
249
263
  const res = (0, known_call_handling_1.processKnownFunctionCall)({
250
264
  name,
251
265
  args: mappedArgs,
@@ -286,6 +300,74 @@ function checkTargetReferenceType(sourceInfo, fnModes) {
286
300
  return identifier_1.ReferenceType.Variable;
287
301
  }
288
302
  }
303
+ /**
304
+ * Returns `true` when the entry-point of `sourceInfo` is a call to a `new.env`-family function.
305
+ * Used by {@link processAssignmentToSymbol} to attach an initial {@link InGraphIdentifierDefinition#envState}.
306
+ */
307
+ function isEnvCreatorSource(sourceInfo) {
308
+ const vert = sourceInfo.graph.getVertex(sourceInfo.entryPoint);
309
+ return (0, vertex_1.isFunctionCallVertex)(vert) && vert.origin.includes(built_in_proc_name_1.BuiltInProcName.NewEnv);
310
+ }
311
+ /**
312
+ * When `e$x <- val` and `e` holds a tracked {@link InGraphIdentifierDefinition#envState},
313
+ * adds the field `x` into that envState instead of redefining the whole `e` object.
314
+ * Returns `undefined` when routing is not applicable.
315
+ */
316
+ function tryRouteDollarEnvAssign(rootId, data, config, target, source, replacement) {
317
+ if (target.operator !== '$' || target.accessed.type !== type_1.RType.Symbol) {
318
+ return undefined;
319
+ }
320
+ const envirResolution = (0, built_in_envir_utils_1.resolveSymbolToEnvir)(target.accessed.content, target.accessed.info.id, data);
321
+ if (!envirResolution) {
322
+ return undefined;
323
+ }
324
+ const fieldNode = (0, unpack_argument_1.unpackArg)(target.access[0]);
325
+ if (!fieldNode) {
326
+ return undefined;
327
+ }
328
+ const fieldName = (fieldNode.type === type_1.RType.String ? fieldNode.content.str : fieldNode.lexeme);
329
+ /* run normal $<- replacement to get correct graph structure (reads of e, x, source) */
330
+ const normalResult = tryReplacement(rootId, replacement, data, replacement.content, [(0, make_argument_1.toUnnamedArgument)(target.accessed, data.completeAst.idMap), ...target.access, source]);
331
+ const fieldDef = {
332
+ type: identifier_1.ReferenceType.Variable,
333
+ name: fieldName,
334
+ nodeId: fieldNode.info.id,
335
+ definedAt: rootId,
336
+ cds: data.cds
337
+ };
338
+ const newEnvState = (0, define_1.define)(fieldDef, false, envirResolution.envDef.envState);
339
+ const updatedEnvDef = {
340
+ ...envirResolution.envDef,
341
+ definedAt: rootId,
342
+ envState: newEnvState
343
+ };
344
+ const strippedEnv = {
345
+ current: normalResult.environment.current.removeAll([{ name: envirResolution.envDef.name }]),
346
+ level: normalResult.environment.level
347
+ };
348
+ return { ...normalResult, environment: (0, define_1.define)(updatedEnvDef, false, strippedEnv) };
349
+ }
350
+ /**
351
+ * When `config.environmentArg` identifies an `envir`-like parameter (e.g. `'envir'` for `assign`)
352
+ * and that argument resolves to a variable with a tracked {@link InGraphIdentifierDefinition#envState},
353
+ * this function routes the written definitions into that custom environment instead of the current
354
+ * global scope.
355
+ * @returns `undefined` if routing is not possible
356
+ */
357
+ function tryRouteToCustomEnv(name, args, rootId, data, config) {
358
+ const resolution = (0, built_in_envir_utils_1.resolveEnvirArg)(args, data, config.environmentArg);
359
+ if (!resolution) {
360
+ return undefined;
361
+ }
362
+ /* run the normal assignment path to get the correct graph structure */
363
+ const normalResult = processAssignment(name, args, rootId, data, {
364
+ ...config,
365
+ environmentArg: undefined // prevent re-entry
366
+ });
367
+ normalResult.graph.addEdge(rootId, resolution.envirNodeId, edge_1.EdgeType.Reads);
368
+ /* pass rootId as definedAt so only defs made at this call site are routed */
369
+ return (0, built_in_envir_utils_1.routeWrittenToCustomEnv)(normalResult, resolution.envDef, rootId, rootId);
370
+ }
289
371
  /**
290
372
  * Consider a call like `x <- v`
291
373
  * @param information - the information to define the assignment within
@@ -331,6 +413,21 @@ function processAssignmentToSymbol(config) {
331
413
  value: aliases
332
414
  }]
333
415
  : produceWrittenNodes(rootId, targetArg, referenceType, data, makeMaybe ?? false, aliases);
416
+ if (data.ctx.config.solver.trackEnvironments) {
417
+ let envState;
418
+ if (isEnvCreatorSource(sourceArg)) {
419
+ envState = (0, built_in_new_env_1.createFreshEnvState)(data, sourceArg);
420
+ }
421
+ else if (source.type === type_1.RType.Symbol) {
422
+ const defs = (0, resolve_by_name_1.resolveByName)(source.content, data.environment, identifier_1.ReferenceType.Variable);
423
+ envState = defs?.find(d => d.envState !== undefined)?.envState;
424
+ }
425
+ if (envState) {
426
+ for (let i = 0; i < writeNodes.length; i++) {
427
+ writeNodes[i] = { ...writeNodes[i], envState };
428
+ }
429
+ }
430
+ }
334
431
  if (writeNodes.length !== 1 && log_1.log.settings.minLevel >= 4 /* LogLevel.Warn */) {
335
432
  log_1.log.warn(`Unexpected write number in assignment: ${JSON.stringify(writeNodes)}`);
336
433
  }
@@ -0,0 +1,13 @@
1
+ import type { DataflowProcessorInformation } from '../../../../../processor';
2
+ import type { DataflowInformation } from '../../../../../info';
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';
5
+ import type { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
6
+ import type { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
7
+ /**
8
+ * Processes `attach(what, ...)` - when `what` is a variable that holds a tracked
9
+ * {@link InGraphIdentifierDefinition#envState}, all statically-known definitions from
10
+ * that environment are injected into the current scope so that subsequent reads of
11
+ * those names resolve correctly.
12
+ */
13
+ export declare function processAttach<OtherInfo>(name: RSymbol<OtherInfo & ParentInformation>, args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], rootId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>): DataflowInformation;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.processAttach = processAttach;
4
+ const known_call_handling_1 = require("../known-call-handling");
5
+ const unpack_argument_1 = require("../argument/unpack-argument");
6
+ const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
7
+ const type_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/type");
8
+ const identifier_1 = require("../../../../../environments/identifier");
9
+ const define_1 = require("../../../../../environments/define");
10
+ const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
11
+ const unknown_side_effect_1 = require("../../../../../graph/unknown-side-effect");
12
+ const edge_1 = require("../../../../../graph/edge");
13
+ const built_in_envir_utils_1 = require("./built-in-envir-utils");
14
+ /**
15
+ * Processes `attach(what, ...)` - when `what` is a variable that holds a tracked
16
+ * {@link InGraphIdentifierDefinition#envState}, all statically-known definitions from
17
+ * that environment are injected into the current scope so that subsequent reads of
18
+ * those names resolve correctly.
19
+ */
20
+ function processAttach(name, args, rootId, data) {
21
+ const result = (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: built_in_proc_name_1.BuiltInProcName.Attach }).information;
22
+ const whatArg = args.length >= 1 ? args[0] : undefined;
23
+ const whatNode = whatArg && whatArg !== r_function_call_1.EmptyArgument ? (0, unpack_argument_1.unpackArg)(whatArg) : undefined;
24
+ if (whatNode?.type !== type_1.RType.Symbol) {
25
+ (0, unknown_side_effect_1.handleUnknownSideEffect)(result.graph, result.environment, rootId);
26
+ return result;
27
+ }
28
+ const envirResolution = (0, built_in_envir_utils_1.resolveSymbolToEnvir)(whatNode.content, whatNode.info.id, data);
29
+ if (!envirResolution) {
30
+ (0, unknown_side_effect_1.handleUnknownSideEffect)(result.graph, result.environment, rootId);
31
+ return result;
32
+ }
33
+ let newEnvironment = result.environment;
34
+ for (const [varName, varDefs] of envirResolution.envDef.envState.current.memory) {
35
+ for (const varDef of varDefs) {
36
+ const inDef = varDef;
37
+ const injected = {
38
+ ...inDef,
39
+ name: varName,
40
+ type: identifier_1.ReferenceType.Variable,
41
+ };
42
+ newEnvironment = (0, define_1.define)(injected, false, newEnvironment);
43
+ result.graph.addEdge(inDef.nodeId, rootId, edge_1.EdgeType.Reads);
44
+ }
45
+ }
46
+ return { ...result, environment: newEnvironment };
47
+ }
48
+ //# sourceMappingURL=built-in-attach.js.map
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Shared utilities for built-in functions that interact with tracked R environments.
3
+ *
4
+ * - {@link bindArgs} - bind call arguments to formal parameter names using R's matching rules.
5
+ * - {@link resolveArgToEnvir} - resolve a single already-found argument to an {@link EnvirResolution}.
6
+ * - {@link resolveEnvirArg} / {@link resolveSymbolToEnvir} - resolve a named argument or
7
+ * symbol to an {@link EnvirResolution} when it holds a tracked {@link InGraphIdentifierDefinition#envState}.
8
+ * - {@link routeWrittenToCustomEnv} - move written definitions from the caller's scope into
9
+ * the holder variable's `envState` after processing an expression that writes into a custom env.
10
+ */
11
+ import type { DataflowProcessorInformation } from '../../../../../processor';
12
+ import type { DataflowInformation } from '../../../../../info';
13
+ import type { ParentInformation } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/decorate';
14
+ import type { PotentiallyEmptyRArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call';
15
+ import type { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
16
+ import type { Identifier, NamedInGraphIdentifierDefinition } from '../../../../../environments/identifier';
17
+ import type { REnvironmentInformation } from '../../../../../environments/environment';
18
+ /** Result type for a successful envir-argument resolution. */
19
+ export interface EnvirResolution<OtherInfo> {
20
+ /** `data` with its `environment` replaced by the resolved `envState` for in-env lookups. */
21
+ readonly envirData: DataflowProcessorInformation<OtherInfo & ParentInformation>;
22
+ /** The definition of the variable that holds the environment */
23
+ readonly envDef: NamedInGraphIdentifierDefinition & {
24
+ envState: REnvironmentInformation;
25
+ };
26
+ /** Node ID of the USE of the envir variable (e.g. the `e` in `envir=e`). */
27
+ readonly envirNodeId: NodeId;
28
+ }
29
+ /**
30
+ * Binds call arguments to formal parameter names following R's standard matching rules:
31
+ * 1. Exact name matches (named args bound to exact-matching formal params).
32
+ * 2. Partial (pmatch) name matches via {@link findByPrefixIfUnique}.
33
+ * 3. Remaining unnamed args fill remaining unbound formal params left-to-right.
34
+ *
35
+ * Pass `paramNames` as the full formal parameter list (excluding `...`) so ambiguous
36
+ * prefixes are rejected correctly. Use this when multiple formals must be found
37
+ * simultaneously so that named-arg binding is consistent across all formals.
38
+ */
39
+ export declare function bindArgs<OtherInfo>(args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], paramNames: readonly string[]): ReadonlyMap<string, PotentiallyEmptyRArgument<OtherInfo & ParentInformation>>;
40
+ /**
41
+ * Resolves a single already-found argument (e.g. from {@link bindArgs}) to an
42
+ * {@link EnvirResolution} when the argument is a symbol that holds a tracked envState.
43
+ * Returns `undefined` when the arg is empty, non-symbolic, or unresolved.
44
+ */
45
+ export declare function resolveArgToEnvir<OtherInfo>(arg: PotentiallyEmptyRArgument<OtherInfo & ParentInformation>, data: DataflowProcessorInformation<OtherInfo & ParentInformation>): EnvirResolution<OtherInfo> | undefined;
46
+ /**
47
+ * Scans `args` for an argument named `argName` (default `'envir'`), or - when
48
+ * `positionalFallbackIndex` is given - for the arg at that positional index when
49
+ * no named match is found. When the resolved argument is a symbol that resolves
50
+ * to a variable with a tracked {@link InGraphIdentifierDefinition#envState},
51
+ * returns the resolved context; otherwise returns `undefined`.
52
+ *
53
+ * Named matching uses pmatch semantics: pass `allParamNames` (the full formal parameter
54
+ * list) so ambiguous prefixes are rejected. Defaults to `[argName]`, which allows
55
+ * prefix matches for `argName` only.
56
+ *
57
+ * When multiple formals must be matched simultaneously (e.g. `data` and `expr` in `with`),
58
+ * use {@link bindArgs} + {@link resolveArgToEnvir} instead so named binding is consistent.
59
+ */
60
+ export declare function resolveEnvirArg<OtherInfo>(args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], data: DataflowProcessorInformation<OtherInfo & ParentInformation>, argName?: string, positionalFallbackIndex?: number, allParamNames?: readonly string[]): EnvirResolution<OtherInfo> | undefined;
61
+ /**
62
+ * Resolves a symbol by name to an {@link EnvirResolution} when the symbol holds a tracked
63
+ * environment. Handles multiple reaching definitions (e.g. from if/else branches) by
64
+ * merging their envStates - see {@link resolveDefsToEnvirResolution}.
65
+ * Returns `undefined` when the name cannot be resolved or none of its definitions carry an envState.
66
+ */
67
+ export declare function resolveSymbolToEnvir<OtherInfo>(symbolName: Identifier, nodeId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>): EnvirResolution<OtherInfo> | undefined;
68
+ /**
69
+ * After processing an expression that writes into a custom environment, moves the
70
+ * written definitions from the caller's scope into `envDef`'s tracked `envState`
71
+ * and re-defines the holder variable in the returned environment.
72
+ */
73
+ export declare function routeWrittenToCustomEnv(result: DataflowInformation, envDef: NamedInGraphIdentifierDefinition & {
74
+ envState: REnvironmentInformation;
75
+ }, newDefAt: NodeId, definedAt?: NodeId): DataflowInformation;
@@ -0,0 +1,196 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.bindArgs = bindArgs;
4
+ exports.resolveArgToEnvir = resolveArgToEnvir;
5
+ exports.resolveEnvirArg = resolveEnvirArg;
6
+ exports.resolveSymbolToEnvir = resolveSymbolToEnvir;
7
+ exports.routeWrittenToCustomEnv = routeWrittenToCustomEnv;
8
+ const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
9
+ const type_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/type");
10
+ const unpack_argument_1 = require("../argument/unpack-argument");
11
+ const resolve_by_name_1 = require("../../../../../environments/resolve-by-name");
12
+ const identifier_1 = require("../../../../../environments/identifier");
13
+ const define_1 = require("../../../../../environments/define");
14
+ const prefix_1 = require("../../../../../../util/prefix");
15
+ /**
16
+ * Core resolver: maps a list of identifier definitions (from {@link resolveByName}) to an
17
+ * {@link EnvirResolution}. Accepts `undefined` so callers can pass `resolveByName` results
18
+ * directly without an intermediate null check.
19
+ *
20
+ * Multiple reaching definitions (e.g. from if/else branches) are accepted only when every
21
+ * definition carries an envState; their envStates are merged into a single combined snapshot.
22
+ */
23
+ function resolveDefsToEnvirResolution(defs, nodeId, data) {
24
+ if (!defs || defs.length === 0) {
25
+ return undefined;
26
+ }
27
+ const inDefs = defs;
28
+ if (inDefs.length === 1) {
29
+ const envState = inDefs[0].envState;
30
+ if (!envState) {
31
+ return undefined;
32
+ }
33
+ const envDef = inDefs[0];
34
+ return { envirData: { ...data, environment: envState }, envDef, envirNodeId: nodeId };
35
+ }
36
+ if (!inDefs.every(d => d.envState !== undefined)) {
37
+ return undefined;
38
+ }
39
+ let mergedEnvState = inDefs[0].envState;
40
+ for (let i = 1; i < inDefs.length; i++) {
41
+ for (const [, varDefs] of inDefs[i].envState.current.memory) {
42
+ for (const varDef of varDefs) {
43
+ const named = varDef;
44
+ if (named.name !== undefined) {
45
+ mergedEnvState = (0, define_1.define)(named, false, mergedEnvState);
46
+ }
47
+ }
48
+ }
49
+ }
50
+ const envDef = {
51
+ ...inDefs[0],
52
+ envState: mergedEnvState
53
+ };
54
+ return { envirData: { ...data, environment: mergedEnvState }, envDef, envirNodeId: nodeId };
55
+ }
56
+ /**
57
+ * Binds call arguments to formal parameter names following R's standard matching rules:
58
+ * 1. Exact name matches (named args bound to exact-matching formal params).
59
+ * 2. Partial (pmatch) name matches via {@link findByPrefixIfUnique}.
60
+ * 3. Remaining unnamed args fill remaining unbound formal params left-to-right.
61
+ *
62
+ * Pass `paramNames` as the full formal parameter list (excluding `...`) so ambiguous
63
+ * prefixes are rejected correctly. Use this when multiple formals must be found
64
+ * simultaneously so that named-arg binding is consistent across all formals.
65
+ */
66
+ function bindArgs(args, paramNames) {
67
+ const bound = new Map();
68
+ const used = new Set();
69
+ /* pass 1: exact name matches */
70
+ for (let i = 0; i < args.length; i++) {
71
+ const arg = args[i];
72
+ if (arg === r_function_call_1.EmptyArgument || arg.name === undefined) {
73
+ continue;
74
+ }
75
+ const n = arg.name.content;
76
+ if (paramNames.includes(n) && !bound.has(n)) {
77
+ bound.set(n, arg);
78
+ used.add(i);
79
+ }
80
+ }
81
+ /* pass 2: partial (pmatch) name matches */
82
+ for (let i = 0; i < args.length; i++) {
83
+ if (used.has(i)) {
84
+ continue;
85
+ }
86
+ const arg = args[i];
87
+ if (arg === r_function_call_1.EmptyArgument || arg.name === undefined) {
88
+ continue;
89
+ }
90
+ const matched = (0, prefix_1.findByPrefixIfUnique)(arg.name.content, paramNames);
91
+ if (matched !== undefined && !bound.has(matched)) {
92
+ bound.set(matched, arg);
93
+ used.add(i);
94
+ }
95
+ }
96
+ /* pass 3: remaining unnamed args fill remaining formal params left-to-right */
97
+ let formalIdx = 0;
98
+ for (let i = 0; i < args.length; i++) {
99
+ if (used.has(i)) {
100
+ continue;
101
+ }
102
+ const arg = args[i];
103
+ if (arg === r_function_call_1.EmptyArgument || arg.name !== undefined) {
104
+ continue;
105
+ }
106
+ while (formalIdx < paramNames.length && bound.has(paramNames[formalIdx])) {
107
+ formalIdx++;
108
+ }
109
+ if (formalIdx < paramNames.length) {
110
+ bound.set(paramNames[formalIdx], arg);
111
+ used.add(i);
112
+ formalIdx++;
113
+ }
114
+ }
115
+ return bound;
116
+ }
117
+ /**
118
+ * Resolves a single already-found argument (e.g. from {@link bindArgs}) to an
119
+ * {@link EnvirResolution} when the argument is a symbol that holds a tracked envState.
120
+ * Returns `undefined` when the arg is empty, non-symbolic, or unresolved.
121
+ */
122
+ function resolveArgToEnvir(arg, data) {
123
+ if (arg === r_function_call_1.EmptyArgument) {
124
+ return undefined;
125
+ }
126
+ const node = (0, unpack_argument_1.unpackArg)(arg);
127
+ if (node?.type !== type_1.RType.Symbol) {
128
+ return undefined;
129
+ }
130
+ return resolveDefsToEnvirResolution((0, resolve_by_name_1.resolveByName)(node.content, data.environment, identifier_1.ReferenceType.Variable), node.info.id, data);
131
+ }
132
+ /**
133
+ * Scans `args` for an argument named `argName` (default `'envir'`), or - when
134
+ * `positionalFallbackIndex` is given - for the arg at that positional index when
135
+ * no named match is found. When the resolved argument is a symbol that resolves
136
+ * to a variable with a tracked {@link InGraphIdentifierDefinition#envState},
137
+ * returns the resolved context; otherwise returns `undefined`.
138
+ *
139
+ * Named matching uses pmatch semantics: pass `allParamNames` (the full formal parameter
140
+ * list) so ambiguous prefixes are rejected. Defaults to `[argName]`, which allows
141
+ * prefix matches for `argName` only.
142
+ *
143
+ * When multiple formals must be matched simultaneously (e.g. `data` and `expr` in `with`),
144
+ * use {@link bindArgs} + {@link resolveArgToEnvir} instead so named binding is consistent.
145
+ */
146
+ function resolveEnvirArg(args, data, argName = 'envir', positionalFallbackIndex, allParamNames = [argName]) {
147
+ /* named pass: pmatch against the full parameter list */
148
+ for (const arg of args) {
149
+ if (arg === r_function_call_1.EmptyArgument || arg.name === undefined) {
150
+ continue;
151
+ }
152
+ if ((0, prefix_1.findByPrefixIfUnique)(arg.name.content, allParamNames) === argName) {
153
+ return resolveArgToEnvir(arg, data);
154
+ }
155
+ }
156
+ /* positional fallback (only when no named match existed) */
157
+ if (positionalFallbackIndex !== undefined) {
158
+ let pos = 0;
159
+ for (const arg of args) {
160
+ if (arg === r_function_call_1.EmptyArgument || arg.name !== undefined) {
161
+ continue;
162
+ }
163
+ if (pos === positionalFallbackIndex) {
164
+ return resolveArgToEnvir(arg, data);
165
+ }
166
+ pos++;
167
+ }
168
+ }
169
+ return undefined;
170
+ }
171
+ /**
172
+ * Resolves a symbol by name to an {@link EnvirResolution} when the symbol holds a tracked
173
+ * environment. Handles multiple reaching definitions (e.g. from if/else branches) by
174
+ * merging their envStates - see {@link resolveDefsToEnvirResolution}.
175
+ * Returns `undefined` when the name cannot be resolved or none of its definitions carry an envState.
176
+ */
177
+ function resolveSymbolToEnvir(symbolName, nodeId, data) {
178
+ return resolveDefsToEnvirResolution((0, resolve_by_name_1.resolveByName)(symbolName, data.environment, identifier_1.ReferenceType.Variable), nodeId, data);
179
+ }
180
+ /**
181
+ * After processing an expression that writes into a custom environment, moves the
182
+ * written definitions from the caller's scope into `envDef`'s tracked `envState`
183
+ * and re-defines the holder variable in the returned environment.
184
+ */
185
+ function routeWrittenToCustomEnv(result, envDef, newDefAt, definedAt) {
186
+ const written = result.out.filter((d) => d.name !== undefined && 'definedAt' in d &&
187
+ (definedAt === undefined || d.definedAt === definedAt));
188
+ let newEnvState = envDef.envState;
189
+ const namesToRemove = written.map(w => ({ name: w.name }));
190
+ for (const w of written) {
191
+ newEnvState = (0, define_1.define)(w, false, newEnvState);
192
+ }
193
+ const newEnvironment = (0, define_1.define)({ ...envDef, definedAt: newDefAt, envState: newEnvState }, false, { current: result.environment.current.removeAll(namesToRemove), level: result.environment.level });
194
+ return { ...result, environment: newEnvironment };
195
+ }
196
+ //# sourceMappingURL=built-in-envir-utils.js.map
@@ -6,5 +6,10 @@ import type { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/node
6
6
  import type { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
7
7
  /**
8
8
  * Processes a built-in 'get' function call.
9
+ *
10
+ * When an `envir` argument is present and resolves to a variable with a tracked environment
11
+ * state (set up by `new.env()` + `assign(..., envir=...)`), the lookup is performed inside
12
+ * that environment so that the correct definition is found and the returned {@link DataflowInformation}
13
+ * contains the right read edges.
9
14
  */
10
15
  export declare function processGet<OtherInfo>(name: RSymbol<OtherInfo & ParentInformation>, args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], rootId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>): DataflowInformation;
@@ -4,23 +4,32 @@ exports.processGet = processGet;
4
4
  const known_call_handling_1 = require("../known-call-handling");
5
5
  const unpack_argument_1 = require("../argument/unpack-argument");
6
6
  const make_argument_1 = require("../argument/make-argument");
7
+ const r_function_call_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call");
7
8
  const logger_1 = require("../../../../../logger");
8
9
  const retriever_1 = require("../../../../../../r-bridge/retriever");
9
10
  const type_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/type");
10
11
  const edge_1 = require("../../../../../graph/edge");
11
12
  const identifier_1 = require("../../../../../environments/identifier");
12
13
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
14
+ const built_in_envir_utils_1 = require("./built-in-envir-utils");
13
15
  /**
14
16
  * Processes a built-in 'get' function call.
17
+ *
18
+ * When an `envir` argument is present and resolves to a variable with a tracked environment
19
+ * state (set up by `new.env()` + `assign(..., envir=...)`), the lookup is performed inside
20
+ * that environment so that the correct definition is found and the returned {@link DataflowInformation}
21
+ * contains the right read edges.
15
22
  */
16
23
  function processGet(name, args, rootId, data) {
17
- if (args.length !== 1) {
18
- logger_1.dataflowLogger.warn(`symbol access with ${identifier_1.Identifier.toString(name.content)} has not 1 argument, skipping`);
19
- return (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: 'default' }).information;
20
- }
21
- const retrieve = (0, unpack_argument_1.unpackNonameArg)(args[0]);
24
+ /* use the custom environment for resolution when envir points to a tracked env */
25
+ const resolution = (0, built_in_envir_utils_1.resolveEnvirArg)(args, data);
26
+ /* the first arg must be a string naming the variable to retrieve */
27
+ const firstArg = args.length >= 1 ? args[0] : undefined;
28
+ const retrieve = firstArg !== undefined && firstArg !== r_function_call_1.EmptyArgument
29
+ ? (0, unpack_argument_1.unpackNonameArg)(firstArg)
30
+ : undefined;
22
31
  if (retrieve === undefined || retrieve.type !== type_1.RType.String) {
23
- logger_1.dataflowLogger.warn(`symbol access with ${identifier_1.Identifier.toString(name.content)} has not 1 argument, skipping`);
32
+ logger_1.dataflowLogger.warn(`symbol access with ${identifier_1.Identifier.toString(name.content)} has not 1 string argument, skipping`);
24
33
  return (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: 'default' }).information;
25
34
  }
26
35
  const treatTargetAsSymbol = {
@@ -30,18 +39,23 @@ function processGet(name, args, rootId, data) {
30
39
  lexeme: retrieve.lexeme,
31
40
  location: retrieve.location
32
41
  };
42
+ /* resolve in the custom environment if one was found, else the global one.
43
+ * Pass remaining original args (e.g. envir=e) so they appear as Use vertices in the graph. */
33
44
  const { information, processedArguments } = (0, known_call_handling_1.processKnownFunctionCall)({
34
45
  name,
35
- args: (0, make_argument_1.wrapArgumentsUnnamed)([treatTargetAsSymbol], data.completeAst.idMap),
46
+ args: [...(0, make_argument_1.wrapArgumentsUnnamed)([treatTargetAsSymbol], data.completeAst.idMap), ...args.slice(1)],
36
47
  rootId,
37
- data,
48
+ data: resolution ? resolution.envirData : data,
38
49
  origin: built_in_proc_name_1.BuiltInProcName.Get
39
50
  });
40
- const firstArg = processedArguments[0];
41
- if (firstArg) {
42
- // get 'reads' its first argument
43
- information.graph.addEdge(rootId, firstArg.entryPoint, edge_1.EdgeType.Returns | edge_1.EdgeType.Reads);
51
+ const firstProcessed = processedArguments[0];
52
+ if (firstProcessed) {
53
+ information.graph.addEdge(rootId, firstProcessed.entryPoint, edge_1.EdgeType.Returns | edge_1.EdgeType.Reads);
54
+ }
55
+ if (resolution) {
56
+ information.graph.addEdge(rootId, resolution.envirNodeId, edge_1.EdgeType.Reads);
44
57
  }
45
- return information;
58
+ /* restore the caller's (global) environment so we don't leak envState upward */
59
+ return { ...information, environment: data.environment };
46
60
  }
47
61
  //# sourceMappingURL=built-in-get.js.map
@@ -11,6 +11,7 @@ const scoping_1 = require("../../../../../environments/scoping");
11
11
  const identifier_1 = require("../../../../../environments/identifier");
12
12
  const r_argument_1 = require("../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-argument");
13
13
  const built_in_proc_name_1 = require("../../../../../environments/built-in-proc-name");
14
+ const built_in_envir_utils_1 = require("./built-in-envir-utils");
14
15
  /**
15
16
  * Processes a built-in 'local' function call.
16
17
  */
@@ -29,6 +30,8 @@ function processLocal(name, args, rootId, data, config) {
29
30
  if (!expr) {
30
31
  return (0, known_call_handling_1.processKnownFunctionCall)({ name, args, rootId, data, origin: 'default' }).information;
31
32
  }
33
+ /* when envir resolves to a tracked environment, evaluate expr inside it */
34
+ const envirResolution = env ? (0, built_in_envir_utils_1.resolveEnvirArg)(args, data, config.args.env) : undefined;
32
35
  const dfEnv = env ? (0, processor_1.processDataflowFor)(env, data) : info_1.DataflowInformation.initialize(rootId, data);
33
36
  if ((0, info_1.alwaysExits)(dfEnv)) {
34
37
  (0, common_1.patchFunctionCall)({
@@ -41,8 +44,10 @@ function processLocal(name, args, rootId, data, config) {
41
44
  });
42
45
  return dfEnv;
43
46
  }
44
- const bodyData = { ...data, environment: (0, scoping_1.pushLocalEnvironment)(data.environment) };
45
- const dfExpr = (0, processor_1.processDataflowFor)(expr, bodyData);
47
+ const baseEnvironment = envirResolution
48
+ ? envirResolution.envirData.environment // evaluate in the tracked custom env
49
+ : (0, scoping_1.pushLocalEnvironment)(data.environment); // normal new local scope
50
+ const dfExpr = (0, processor_1.processDataflowFor)(expr, { ...data, environment: baseEnvironment });
46
51
  (0, common_1.patchFunctionCall)({
47
52
  nextGraph: dfEnv.graph,
48
53
  rootId,
@@ -53,9 +58,9 @@ function processLocal(name, args, rootId, data, config) {
53
58
  });
54
59
  const ingoing = dfEnv.in.concat(dfExpr.in, dfEnv.unknownReferences, dfExpr.unknownReferences);
55
60
  ingoing.push({ nodeId: rootId, name: name.content, cds: data.cds, type: identifier_1.ReferenceType.Function });
56
- return {
61
+ const baseResult = {
57
62
  hooks: dfExpr.hooks.concat(dfEnv.hooks),
58
- environment: (0, scoping_1.popLocalEnvironment)(dfExpr.environment),
63
+ environment: envirResolution ? data.environment : (0, scoping_1.popLocalEnvironment)(dfExpr.environment),
59
64
  exitPoints: dfEnv.exitPoints.concat(dfExpr.exitPoints),
60
65
  graph: dfEnv.graph.mergeWith(dfExpr.graph),
61
66
  entryPoint: rootId,
@@ -63,5 +68,10 @@ function processLocal(name, args, rootId, data, config) {
63
68
  out: dfExpr.out.concat(dfEnv.out),
64
69
  unknownReferences: []
65
70
  };
71
+ /* move all definitions made inside the body into the custom env's tracked state */
72
+ if (envirResolution) {
73
+ return (0, built_in_envir_utils_1.routeWrittenToCustomEnv)(baseResult, envirResolution.envDef, rootId);
74
+ }
75
+ return baseResult;
66
76
  }
67
77
  //# sourceMappingURL=built-in-local.js.map
@@ -0,0 +1,27 @@
1
+ import type { DataflowProcessorInformation } from '../../../../../processor';
2
+ import type { DataflowInformation } from '../../../../../info';
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';
5
+ import type { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol';
6
+ import type { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id';
7
+ import { type REnvironmentInformation } from '../../../../../environments/environment';
8
+ /**
9
+ * Processes `new.env()` and `rlang::new_environment()`.
10
+ *
11
+ * The resulting function-call vertex carries the {@link BuiltInProcName.NewEnv} origin so that
12
+ * `processAssignment` can detect it and attach an `envState` to the assigned variable.
13
+ *
14
+ * When the `parent` argument can be statically resolved (a tracked env variable or an
15
+ * `emptyenv()`-family call), the resolved parent is stored in the vertex's `newEnvParent`
16
+ * field so that {@link createFreshEnvState} can use it.
17
+ */
18
+ export declare function processNewEnv<OtherInfo>(name: RSymbol<OtherInfo & ParentInformation>, args: readonly PotentiallyEmptyRArgument<OtherInfo & ParentInformation>[], rootId: NodeId, data: DataflowProcessorInformation<OtherInfo & ParentInformation>): DataflowInformation;
19
+ /**
20
+ * Creates the initial (empty) environment state for a freshly created `new.env()`.
21
+ *
22
+ * When called from `processAssignmentToSymbol`, pass `sourceInfo` so that a
23
+ * statically-resolved `parent` argument (stored in the vertex's `newEnvParent` field)
24
+ * is honoured. Without `sourceInfo` (or when no `newEnvParent` is present), falls back
25
+ * to the current execution environment as parent.
26
+ */
27
+ export declare function createFreshEnvState(data: Pick<DataflowProcessorInformation<never>, 'environment'>, sourceInfo?: DataflowInformation): REnvironmentInformation;