@eagleoutice/flowr 2.15.2 → 2.15.4
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.
- package/README.md +20 -20
- package/dataflow/environments/environment.d.ts +28 -13
- package/dataflow/environments/environment.js +62 -23
- package/dataflow/environments/frame-memory.d.ts +84 -0
- package/dataflow/environments/frame-memory.js +241 -0
- package/dataflow/environments/query-fn-props.d.ts +39 -2
- package/dataflow/environments/query-fn-props.js +98 -12
- package/dataflow/environments/resolve-by-name.js +28 -9
- package/dataflow/graph/graph.d.ts +18 -4
- package/dataflow/graph/graph.js +101 -64
- package/dataflow/graph/vertex.js +1 -1
- package/dataflow/internal/linker.js +9 -5
- package/dataflow/internal/process/functions/call/built-in/built-in-apply.js +1 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-expression-list.js +2 -1
- package/dataflow/internal/process/functions/call/built-in/built-in-for-loop.js +4 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-function-definition.js +2 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-library.js +2 -2
- package/dataflow/internal/process/functions/call/built-in/built-in-namespace-access.js +1 -1
- package/dataflow/internal/process/functions/call/built-in/transitive-side-effects.js +19 -5
- package/dataflow/internal/process/functions/call/common.js +6 -2
- package/dataflow/internal/process/functions/call/known-call-handling.js +1 -1
- package/dataflow/internal/process/functions/call/quoted.js +1 -1
- package/dataflow/internal/process/functions/call/unnamed-call-handling.js +1 -1
- package/dataflow/internal/process/functions/process-argument.js +1 -1
- package/dataflow/internal/process/process-symbol.js +1 -1
- package/index.d.ts +2 -0
- package/index.js +4 -1
- package/linter/linter-rules.d.ts +9 -9
- package/package.json +1 -1
- package/project/context/flowr-analyzer-environment-context.d.ts +6 -0
- package/project/context/flowr-analyzer-environment-context.js +4 -2
- package/project/flowr-analyzer.d.ts +13 -0
- package/project/flowr-analyzer.js +5 -0
- package/project/sigdb/reader.js +44 -13
- package/project/sigdb/signature-db.d.ts +3 -0
- package/project/sigdb/signature-db.js +5 -4
- package/queries/catalog/input-sources-query/simple-input-classifier.js +4 -5
- package/queries/catalog/signature-query/signature-query-format.d.ts +1 -2
- package/slicing/static/fingerprint.js +7 -2
- package/util/collections/bimap.d.ts +2 -1
- package/util/collections/bimap.js +4 -4
- package/util/version.js +2 -2
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* One environment frame's bindings, shared by every snapshot of it: a snapshot is the frame plus a version, not a
|
|
4
|
+
* copy. Copying instead cost a whole map per snapshot, and a call vertex keeps one, so a scope with thousands of
|
|
5
|
+
* definitions retained memory in the square of them. The newest version is answered from a plain map, an older one
|
|
6
|
+
* from what the writes recorded.
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.WritableMemory = exports.MemoryView = exports.Frame = void 0;
|
|
11
|
+
const assert_1 = require("../../util/assert");
|
|
12
|
+
/** the index in `history` of what the greatest version at most `version` bound, `-1` if unbound then */
|
|
13
|
+
function at(history, version) {
|
|
14
|
+
let low = 0;
|
|
15
|
+
let high = (history.length >> 1) - 1;
|
|
16
|
+
while (low <= high) {
|
|
17
|
+
const mid = (low + high) >> 1;
|
|
18
|
+
if (history[mid << 1] <= version) {
|
|
19
|
+
low = mid + 1;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
high = mid - 1;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return high < 0 ? -1 : (high << 1) + 1;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* One frame's bindings across its versions. A frame {@link Frame.of|taken from a map} is frozen: one state, never
|
|
29
|
+
* written, versioned only once {@link forWrite} forks it -- the built-in environment and the package layers.
|
|
30
|
+
*/
|
|
31
|
+
class Frame {
|
|
32
|
+
/** the bindings at {@link version} and no other, so read it only after checking the version, as {@link get} does */
|
|
33
|
+
live;
|
|
34
|
+
/** whether {@link live} is this frame's own to write, see {@link Frame.of} */
|
|
35
|
+
frozen;
|
|
36
|
+
/** what each *rewritten* name held per version, for older snapshots; built on the first rewrite, as most see none */
|
|
37
|
+
history;
|
|
38
|
+
/** when each name still on its first binding came in; a write-once name needs no more, and most are */
|
|
39
|
+
bornAt;
|
|
40
|
+
/** the last past version walked out into a map, so re-reading that snapshot skips the walk; one slot only */
|
|
41
|
+
walked;
|
|
42
|
+
/** the newest version written here, counting this frame's writes alone; only a snapshot at it may extend the frame */
|
|
43
|
+
version = 0;
|
|
44
|
+
constructor(live, frozen) {
|
|
45
|
+
this.live = live;
|
|
46
|
+
this.frozen = frozen;
|
|
47
|
+
}
|
|
48
|
+
/** A frame holding what `map` holds, frozen: only read through here, a write forks rather than reaching back. */
|
|
49
|
+
static of(map) {
|
|
50
|
+
return new Frame(map, true);
|
|
51
|
+
}
|
|
52
|
+
/** A frame holding nothing, the same one every time, so an environment that defines nothing allocates nothing. */
|
|
53
|
+
static empty() {
|
|
54
|
+
return EmptyFrame;
|
|
55
|
+
}
|
|
56
|
+
/** What `name` held at `version`. One lookup for the newest version, small enough for V8 to inline. */
|
|
57
|
+
get(name, version) {
|
|
58
|
+
return version === this.version ? this.live.get(name) : this.bound(name, version);
|
|
59
|
+
}
|
|
60
|
+
/** What `name` held at a version written past since. */
|
|
61
|
+
bound(name, version) {
|
|
62
|
+
const history = this.history?.get(name);
|
|
63
|
+
if (history !== undefined) {
|
|
64
|
+
const index = at(history, version);
|
|
65
|
+
return index < 0 ? undefined : history[index];
|
|
66
|
+
}
|
|
67
|
+
const born = this.bornAt?.get(name);
|
|
68
|
+
/* never rewritten, so it still holds what it held then -- unless it came in after then */
|
|
69
|
+
return born !== undefined && born > version ? undefined : this.live.get(name);
|
|
70
|
+
}
|
|
71
|
+
/** Whether `name` was bound at `version`. */
|
|
72
|
+
has(name, version) {
|
|
73
|
+
return (version === this.version ? this.live.get(name) : this.bound(name, version)) !== undefined;
|
|
74
|
+
}
|
|
75
|
+
/** The names `version` binds. */
|
|
76
|
+
keys(version) {
|
|
77
|
+
return (this.settledAt(version) ?? this.materialize(version)).keys();
|
|
78
|
+
}
|
|
79
|
+
/** What `version` binds, without the names. */
|
|
80
|
+
values(version) {
|
|
81
|
+
return (this.settledAt(version) ?? this.materialize(version)).values();
|
|
82
|
+
}
|
|
83
|
+
/** How many names `version` binds. */
|
|
84
|
+
size(version) {
|
|
85
|
+
return (this.settledAt(version) ?? this.materialize(version)).size;
|
|
86
|
+
}
|
|
87
|
+
/** What `version` binds, name by name. */
|
|
88
|
+
entries(version) {
|
|
89
|
+
return (this.settledAt(version) ?? this.materialize(version)).entries();
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* `version` as a map, `undefined` when it has to be walked out. A frame that never rewrote or took on a name has
|
|
93
|
+
* held the same bindings throughout, so {@link live} answers for any version of it.
|
|
94
|
+
*/
|
|
95
|
+
settledAt(version) {
|
|
96
|
+
if (version === this.version || (this.history === undefined && this.bornAt === undefined)) {
|
|
97
|
+
return this.live;
|
|
98
|
+
}
|
|
99
|
+
const walked = this.walked;
|
|
100
|
+
return walked !== undefined && walked.version === version ? walked.bindings : undefined;
|
|
101
|
+
}
|
|
102
|
+
/** `version` walked out into a map, kept in the {@link walked} slot. */
|
|
103
|
+
materialize(version) {
|
|
104
|
+
const bindings = new Map(this.recorded(version));
|
|
105
|
+
this.walked = { version, bindings };
|
|
106
|
+
return bindings;
|
|
107
|
+
}
|
|
108
|
+
/** What `version` bound; the rewritten names come first, since one dropped since is no longer in {@link live}. */
|
|
109
|
+
*recorded(version) {
|
|
110
|
+
const history = this.history;
|
|
111
|
+
if (history !== undefined) {
|
|
112
|
+
for (const name of history.keys()) {
|
|
113
|
+
const defs = this.bound(name, version);
|
|
114
|
+
if (defs !== undefined) {
|
|
115
|
+
yield [name, defs];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
for (const [name, defs] of this.live) {
|
|
120
|
+
if (history?.has(name)) {
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
const born = this.bornAt?.get(name);
|
|
124
|
+
if (born === undefined || born <= version) {
|
|
125
|
+
yield [name, defs];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/** The frame a snapshot at `version` writes to: this one if it holds the newest version, a fork otherwise. */
|
|
130
|
+
forWrite(version) {
|
|
131
|
+
if (!this.frozen && version === this.version) {
|
|
132
|
+
return this;
|
|
133
|
+
}
|
|
134
|
+
return new Frame(new Map(this.settledAt(version) ?? this.materialize(version)), false);
|
|
135
|
+
}
|
|
136
|
+
/** The version the next write goes to; only a frame {@link forWrite} just returned may hand one out. */
|
|
137
|
+
nextVersion() {
|
|
138
|
+
(0, assert_1.guard)(!this.frozen, 'a frozen frame hands out no version, fork it with forWrite first');
|
|
139
|
+
return ++this.version;
|
|
140
|
+
}
|
|
141
|
+
/** Binds `name` at `version`, `undefined` removing it. Only the newest version may be written; snapshots hold the rest. */
|
|
142
|
+
set(name, defs, version) {
|
|
143
|
+
const live = this.live;
|
|
144
|
+
(0, assert_1.guard)(!this.frozen && version === this.version, () => `cannot write version ${version} of a frame standing at ${this.version}`);
|
|
145
|
+
const history = this.history?.get(name);
|
|
146
|
+
if (history !== undefined) {
|
|
147
|
+
if (history[history.length - 2] === version) {
|
|
148
|
+
history[history.length - 1] = defs;
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
history.push(version, defs);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
const before = live.get(name);
|
|
156
|
+
const born = this.bornAt?.get(name);
|
|
157
|
+
if (before === undefined && born === undefined) {
|
|
158
|
+
/* never held here, so when it came in is all an older snapshot needs */
|
|
159
|
+
(this.bornAt ??= new Map()).set(name, version);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
/* record what it is replacing before the write buries it */
|
|
163
|
+
this.bornAt?.delete(name);
|
|
164
|
+
(this.history ??= new Map()).set(name, [born ?? 0, before, version, defs]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (defs === undefined) {
|
|
168
|
+
live.delete(name);
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
live.set(name, defs);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.Frame = Frame;
|
|
176
|
+
/** what every environment starts out on, until a write forks it one of its own */
|
|
177
|
+
const EmptyFrame = Frame.of(new Map());
|
|
178
|
+
/** One environment's view of its {@link Frame}, as a {@link ReadonlyMap} so the old readers of the map still work. */
|
|
179
|
+
class MemoryView {
|
|
180
|
+
frame;
|
|
181
|
+
version;
|
|
182
|
+
constructor(frame, version) {
|
|
183
|
+
this.frame = frame;
|
|
184
|
+
this.version = version;
|
|
185
|
+
}
|
|
186
|
+
get(name) {
|
|
187
|
+
return this.frame.get(name, this.version);
|
|
188
|
+
}
|
|
189
|
+
has(name) {
|
|
190
|
+
return this.frame.has(name, this.version);
|
|
191
|
+
}
|
|
192
|
+
get size() {
|
|
193
|
+
return this.frame.size(this.version);
|
|
194
|
+
}
|
|
195
|
+
entries() {
|
|
196
|
+
return this.frame.entries(this.version);
|
|
197
|
+
}
|
|
198
|
+
[Symbol.iterator]() {
|
|
199
|
+
return this.frame.entries(this.version);
|
|
200
|
+
}
|
|
201
|
+
keys() {
|
|
202
|
+
return this.frame.keys(this.version);
|
|
203
|
+
}
|
|
204
|
+
values() {
|
|
205
|
+
return this.frame.values(this.version);
|
|
206
|
+
}
|
|
207
|
+
forEach(fn, thisArg) {
|
|
208
|
+
const settled = this.frame.settledAt(this.version);
|
|
209
|
+
if (settled !== undefined) {
|
|
210
|
+
settled.forEach(fn, thisArg);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
for (const [name, defs] of this.frame.entries(this.version)) {
|
|
214
|
+
fn.call(thisArg, defs, name, this);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
exports.MemoryView = MemoryView;
|
|
219
|
+
/** The write end of a {@link Frame} at one version, standing in for the map writers used to get. */
|
|
220
|
+
class WritableMemory {
|
|
221
|
+
frame;
|
|
222
|
+
version;
|
|
223
|
+
constructor(frame, version) {
|
|
224
|
+
this.frame = frame;
|
|
225
|
+
this.version = version;
|
|
226
|
+
}
|
|
227
|
+
set(name, defs) {
|
|
228
|
+
this.frame.set(name, defs, this.version);
|
|
229
|
+
}
|
|
230
|
+
delete(name) {
|
|
231
|
+
this.frame.set(name, undefined, this.version);
|
|
232
|
+
}
|
|
233
|
+
get(name) {
|
|
234
|
+
return this.frame.get(name, this.version);
|
|
235
|
+
}
|
|
236
|
+
has(name) {
|
|
237
|
+
return this.frame.has(name, this.version);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.WritableMemory = WritableMemory;
|
|
241
|
+
//# sourceMappingURL=frame-memory.js.map
|
|
@@ -5,6 +5,7 @@ import type { REnvironmentInformation } from './environment';
|
|
|
5
5
|
import { Identifier } from './identifier';
|
|
6
6
|
import { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id';
|
|
7
7
|
import type { PackageSignatureSource } from '../../project/sigdb/reader';
|
|
8
|
+
import type { DecodedFunction } from '../../project/sigdb/decode';
|
|
8
9
|
import type { ReadOnlyFlowrAnalyzerContext } from '../../project/context/flowr-analyzer-context';
|
|
9
10
|
import type { DataflowInformation } from '../info';
|
|
10
11
|
/**
|
|
@@ -43,6 +44,42 @@ export declare function callFnProps(id: NodeId, { graph, environment }: Pick<Dat
|
|
|
43
44
|
* A package function that ends up in `system()` runs a system command as well.
|
|
44
45
|
*/
|
|
45
46
|
export declare function inferFnProps(src: PackageSignatureSource, pkg: string, name: string, version?: string): BuiltInFnInfo | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Everything flowR knows about one function, no matter which of its sources knows it; see {@link fnInfo}.
|
|
49
|
+
* It extends {@link BuiltInFnInfo}, so anything taking what a built-in states takes this as well.
|
|
50
|
+
*/
|
|
51
|
+
export interface FnInfo extends BuiltInFnInfo {
|
|
52
|
+
/** the identifier the answer is for, with the namespace filled in when an unqualified ask resolved to a package */
|
|
53
|
+
readonly name: Identifier;
|
|
54
|
+
/** the package the answer is for, `undefined` when the name resolves to no package at all */
|
|
55
|
+
readonly package?: string;
|
|
56
|
+
/** the package version the answer is for, i.e. the one the analysis assumes; see {@link SignatureDb.versionOf} */
|
|
57
|
+
readonly version?: string;
|
|
58
|
+
/** the formal names in order, empty when neither source declares any */
|
|
59
|
+
readonly parameters: readonly string[];
|
|
60
|
+
/** whether flowR's own built-in definitions state anything about the name */
|
|
61
|
+
readonly builtIn: boolean;
|
|
62
|
+
/** whether flowR's value solver can fold a call of it to a constant */
|
|
63
|
+
readonly foldable: boolean;
|
|
64
|
+
/** the signature database entry: where the function is defined, its callees, its help topic */
|
|
65
|
+
readonly entry?: DecodedFunction;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The one place to ask what flowR knows about a function by name: one of its own built-ins, a package function
|
|
69
|
+
* the signature database carries, or both. What the built-in states wins wherever the two overlap, exactly as
|
|
70
|
+
* {@link queryFnProps} joins them. `undefined` when neither has anything to say about the name.
|
|
71
|
+
* @param name - The function to ask about, qualified (`Identifier.make('lead', 'dplyr')`) to pin the package down.
|
|
72
|
+
* @param ctx - The analyzer context the built-ins, the database and the assumed versions come from.
|
|
73
|
+
* @param version - The package version to answer for, the one the analysis assumes if omitted.
|
|
74
|
+
* @example
|
|
75
|
+
* ```ts
|
|
76
|
+
* const ctx = analyzer.inspectContext();
|
|
77
|
+
* fnInfo(Identifier.make('lead', 'dplyr'), ctx)?.parameters; // ['x', 'n', 'default', 'order_by', '...']
|
|
78
|
+
* fnInfo(Identifier.make('nchar'), ctx)?.foldable; // true, flowR folds it itself
|
|
79
|
+
* ```
|
|
80
|
+
* @see {@link fnInfoLookup} - to ask the same question for many names
|
|
81
|
+
*/
|
|
82
|
+
export declare function fnInfo(name: Identifier, ctx: ReadOnlyFlowrAnalyzerContext, version?: string): FnInfo | undefined;
|
|
46
83
|
/** The identifiers a definition registers, with the suffixes of a replacement spelled out. */
|
|
47
84
|
export declare function builtInNames(definition: BuiltInDefinition): Identifier[];
|
|
48
85
|
/** One built-in as the {@link BuiltInIndex} sees it: the name it is registered under and what flowR states about it. */
|
|
@@ -56,7 +93,7 @@ export interface BuiltInEntry extends StatedProps {
|
|
|
56
93
|
/** the declared parameters and what each of their arguments is used for */
|
|
57
94
|
readonly sig?: FnSig;
|
|
58
95
|
/** whether the value solver can fold a call of this built-in to a constant */
|
|
59
|
-
readonly
|
|
96
|
+
readonly foldable: boolean;
|
|
60
97
|
}
|
|
61
98
|
/** One parameter of a built-in, as {@link BuiltInIndex#params} reports it. */
|
|
62
99
|
export interface BuiltInParam {
|
|
@@ -110,7 +147,7 @@ export declare class BuiltInIndex {
|
|
|
110
147
|
/** Every built-in flowR states computes a result and nothing else ({@link CallProp.Pure}). */
|
|
111
148
|
get pure(): readonly Identifier[];
|
|
112
149
|
/** Every built-in the value solver can fold to a constant (the ones with a `evalHandler`). */
|
|
113
|
-
get
|
|
150
|
+
get foldable(): readonly Identifier[];
|
|
114
151
|
/** Every parameter whose argument carries at least one bit of `props`, like {@link ArgProp.Resource}. */
|
|
115
152
|
params(props: ArgProps): BuiltInParam[];
|
|
116
153
|
/**
|
|
@@ -5,12 +5,14 @@ exports.queryFnProps = queryFnProps;
|
|
|
5
5
|
exports.builtInLookup = builtInLookup;
|
|
6
6
|
exports.callFnProps = callFnProps;
|
|
7
7
|
exports.inferFnProps = inferFnProps;
|
|
8
|
+
exports.fnInfo = fnInfo;
|
|
8
9
|
exports.builtInNames = builtInNames;
|
|
9
10
|
const built_in_props_1 = require("./built-in-props");
|
|
10
11
|
const default_builtin_config_1 = require("./default-builtin-config");
|
|
11
12
|
const environment_1 = require("./environment");
|
|
12
13
|
const identifier_1 = require("./identifier");
|
|
13
14
|
const node_id_1 = require("../../r-bridge/lang-4.x/ast/model/processing/node-id");
|
|
15
|
+
const signature_db_1 = require("../../project/sigdb/signature-db");
|
|
14
16
|
const resolve_helper_1 = require("./resolve-helper");
|
|
15
17
|
const vertex_1 = require("../graph/vertex");
|
|
16
18
|
const df_helper_1 = require("../graph/df-helper");
|
|
@@ -134,17 +136,101 @@ function callFnProps(id, { graph, environment }) {
|
|
|
134
136
|
*/
|
|
135
137
|
function inferFnProps(src, pkg, name, version) {
|
|
136
138
|
const fn = src.functionByName(pkg, name, version);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
139
|
+
return fn === undefined ? undefined : propsOfSignature(src, fn, pkg, version);
|
|
140
|
+
}
|
|
141
|
+
/** {@link inferFnProps} for an already decoded entry, so a lookup that found one does not repeat it. */
|
|
142
|
+
function propsOfSignature(src, fn, pkg, version) {
|
|
140
143
|
const own = (0, built_in_props_1.fnInfoFromSignature)(fn);
|
|
141
144
|
const known = BuiltInIndex.default();
|
|
142
145
|
let props = own;
|
|
143
|
-
for (const callee of src.transitiveCallees(pkg, name, version) ?? fn.callees) {
|
|
146
|
+
for (const callee of src.transitiveCallees(pkg, fn.name, version) ?? fn.callees) {
|
|
144
147
|
props = built_in_props_1.CallProps.join(props, built_in_props_1.CallProps.filter(known.get(callee), built_in_props_1.PropagatedProps));
|
|
145
148
|
}
|
|
146
149
|
return { sig: own.sig, ...props };
|
|
147
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* The one place to ask what flowR knows about a function by name: one of its own built-ins, a package function
|
|
153
|
+
* the signature database carries, or both. What the built-in states wins wherever the two overlap, exactly as
|
|
154
|
+
* {@link queryFnProps} joins them. `undefined` when neither has anything to say about the name.
|
|
155
|
+
* @param name - The function to ask about, qualified (`Identifier.make('lead', 'dplyr')`) to pin the package down.
|
|
156
|
+
* @param ctx - The analyzer context the built-ins, the database and the assumed versions come from.
|
|
157
|
+
* @param version - The package version to answer for, the one the analysis assumes if omitted.
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* const ctx = analyzer.inspectContext();
|
|
161
|
+
* fnInfo(Identifier.make('lead', 'dplyr'), ctx)?.parameters; // ['x', 'n', 'default', 'order_by', '...']
|
|
162
|
+
* fnInfo(Identifier.make('nchar'), ctx)?.foldable; // true, flowR folds it itself
|
|
163
|
+
* ```
|
|
164
|
+
* @see {@link fnInfoLookup} - to ask the same question for many names
|
|
165
|
+
*/
|
|
166
|
+
function fnInfo(name, ctx, version) {
|
|
167
|
+
const db = (0, signature_db_1.signatureDbOf)(ctx.deps);
|
|
168
|
+
const available = db.available();
|
|
169
|
+
const [bare, namespace, internal] = identifier_1.Identifier.toArray(name);
|
|
170
|
+
const pkg = namespace ?? packageExporting(bare, ctx);
|
|
171
|
+
/* an unqualified ask keeps its form, the built-in layer has a search path of its own */
|
|
172
|
+
const stated = queryFnProps(name, { environment: ctx.env.makeCleanEnv() });
|
|
173
|
+
const qualified = pkg === undefined ? name : identifier_1.Identifier.make(bare, pkg, internal === true);
|
|
174
|
+
const assumed = pkg === undefined ? undefined : version ?? db.versionOf(pkg);
|
|
175
|
+
/* the identifier says how far to reach, `::` to the exports and `:::` past them */
|
|
176
|
+
const entry = pkg === undefined || !available ? undefined : db.functionOf(qualified, version);
|
|
177
|
+
const known = entry === undefined || pkg === undefined ? undefined : propsOfSignature(db.sources(), entry, pkg, assumed);
|
|
178
|
+
if (stated === undefined && known === undefined) {
|
|
179
|
+
return undefined;
|
|
180
|
+
}
|
|
181
|
+
/* the same identifier the built-in layer was asked with, so both halves of the answer describe one function */
|
|
182
|
+
const definition = ctx.env.builtInFunctionOf(name);
|
|
183
|
+
const sig = nonEmpty(stated?.sig) ?? nonEmpty(known?.sig);
|
|
184
|
+
/* the database keeps every formal, a `sig` only the ones flowR models, so it answers first, but an entry
|
|
185
|
+
* that records none must not hide the formals the built-in does state */
|
|
186
|
+
const parameters = nonEmpty(entry?.signature)?.map(p => p.name) ?? sig?.map(([param]) => param) ?? [];
|
|
187
|
+
return {
|
|
188
|
+
name: qualified,
|
|
189
|
+
package: pkg,
|
|
190
|
+
version: assumed,
|
|
191
|
+
sig,
|
|
192
|
+
parameters,
|
|
193
|
+
...built_in_props_1.CallProps.join(stated, known),
|
|
194
|
+
frame: stated?.frame,
|
|
195
|
+
keepEnvironment: stated?.keepEnvironment,
|
|
196
|
+
builtIn: definition !== undefined || stated !== undefined,
|
|
197
|
+
foldable: definition?.evalHandler !== undefined,
|
|
198
|
+
entry
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/** The array itself, `undefined` when it holds nothing, so an empty one does not count as an answer. */
|
|
202
|
+
function nonEmpty(of) {
|
|
203
|
+
return of !== undefined && of.length > 0 ? of : undefined;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* The package an unqualified `name` is answered for. Only one the project pulls in counts, as a name some
|
|
207
|
+
* unrelated package exports says nothing about the code at hand, and a `library()` of one sits above the base
|
|
208
|
+
* packages. Where several pulled-in packages export it the most-used answers, as attach order is not known here.
|
|
209
|
+
*
|
|
210
|
+
* Base R is settled by {@link baseRExportOwner} when the database's export index does not carry the name, so
|
|
211
|
+
* `sum` still answers as `base::sum` on a bundle holding no record of it and with no database at all.
|
|
212
|
+
*/
|
|
213
|
+
function packageExporting(name, ctx) {
|
|
214
|
+
let best;
|
|
215
|
+
let bestRank = Number.MAX_SAFE_INTEGER;
|
|
216
|
+
let pulled;
|
|
217
|
+
for (const pkg of ctx.deps.packagesExporting(name)) {
|
|
218
|
+
const attached = pkg === "base" /* PkgName.Base */ || r_base_packages_1.AttachedBasePackageSet.has(pkg);
|
|
219
|
+
if (!attached) {
|
|
220
|
+
pulled ??= new Set(ctx.deps.getDependencies().map(d => d.name));
|
|
221
|
+
if (!pulled.has(pkg)) {
|
|
222
|
+
continue; // nothing attaches it here, so no call in this project reaches it unqualified
|
|
223
|
+
}
|
|
224
|
+
return pkg; // and nothing outranks a package the code attaches itself
|
|
225
|
+
}
|
|
226
|
+
const rank = pkg === "base" /* PkgName.Base */ ? 2 : 1;
|
|
227
|
+
if (rank < bestRank) {
|
|
228
|
+
bestRank = rank;
|
|
229
|
+
best = pkg;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return best ?? (0, r_base_packages_1.baseRExportOwner)(name);
|
|
233
|
+
}
|
|
148
234
|
/** The identifiers a definition registers, with the suffixes of a replacement spelled out. */
|
|
149
235
|
function builtInNames(definition) {
|
|
150
236
|
if (definition.type !== 'replacement') {
|
|
@@ -157,8 +243,8 @@ function entryOfDefinition(definition) {
|
|
|
157
243
|
return [];
|
|
158
244
|
}
|
|
159
245
|
const info = definition.config;
|
|
160
|
-
const
|
|
161
|
-
return builtInNames(definition).map(name => ({ name, props: info?.props, tags: info?.tags, sig: info?.sig,
|
|
246
|
+
const foldable = definition.type === 'function' && definition.evalHandler !== undefined;
|
|
247
|
+
return builtInNames(definition).map(name => ({ name, props: info?.props, tags: info?.tags, sig: info?.sig, foldable, overrides: definition.overrides }));
|
|
162
248
|
}
|
|
163
249
|
function entriesOfMemory(builtIns) {
|
|
164
250
|
const out = [];
|
|
@@ -173,7 +259,7 @@ function entriesOfMemory(builtIns) {
|
|
|
173
259
|
}
|
|
174
260
|
const info = d.config;
|
|
175
261
|
/* the memory is keyed by the bare name, the definition keeps the namespace it was declared with */
|
|
176
|
-
out.push({ name: d.name ?? registered, props: info?.props, tags: info?.tags, sig: info?.sig,
|
|
262
|
+
out.push({ name: d.name ?? registered, props: info?.props, tags: info?.tags, sig: info?.sig, foldable: d.evalHandler !== undefined });
|
|
177
263
|
}
|
|
178
264
|
}
|
|
179
265
|
}
|
|
@@ -191,12 +277,12 @@ function mergedEntry(known, next) {
|
|
|
191
277
|
props: next.props ?? known.props,
|
|
192
278
|
tags: next.tags ?? known.tags,
|
|
193
279
|
sig: next.sig ?? known.sig,
|
|
194
|
-
|
|
280
|
+
foldable: next.foldable || known.foldable
|
|
195
281
|
};
|
|
196
282
|
}
|
|
197
283
|
/** The entry alone, without what only the index build needs to know about it. */
|
|
198
|
-
function plainEntry({ name, props, tags, sig,
|
|
199
|
-
return { name, props, tags, sig,
|
|
284
|
+
function plainEntry({ name, props, tags, sig, foldable }) {
|
|
285
|
+
return { name, props, tags, sig, foldable };
|
|
200
286
|
}
|
|
201
287
|
/** How early R's default search path reaches a name, deciding which package an unqualified ask answers with. */
|
|
202
288
|
function searchPathRank(name) {
|
|
@@ -301,8 +387,8 @@ class BuiltInIndex {
|
|
|
301
387
|
return this.with(built_in_props_1.CallProp.Pure);
|
|
302
388
|
}
|
|
303
389
|
/** Every built-in the value solver can fold to a constant (the ones with a `evalHandler`). */
|
|
304
|
-
get
|
|
305
|
-
return this.cached('
|
|
390
|
+
get foldable() {
|
|
391
|
+
return this.cached('foldable', e => e.foldable);
|
|
306
392
|
}
|
|
307
393
|
/** Every parameter whose argument carries at least one bit of `props`, like {@link ArgProp.Resource}. */
|
|
308
394
|
params(props) {
|
|
@@ -85,11 +85,24 @@ function resolveByTargetType(id, environment, target) {
|
|
|
85
85
|
/* `current` can already be the built-in environment itself (e.g. `get(x, envir=baseenv())`);
|
|
86
86
|
* it has no parent to walk to, so resolve directly instead of entering the loop below. */
|
|
87
87
|
if (current.builtInEnv) {
|
|
88
|
-
return current.
|
|
88
|
+
return current.lookup(name);
|
|
89
89
|
}
|
|
90
90
|
let definitions = undefined;
|
|
91
91
|
const wantedType = TargetTypePredicate[target];
|
|
92
|
+
/* the attached packages below the code's own frames are a dozen layers deep and nothing writes them, so the
|
|
93
|
+
* walk through them is done once per name and target instead of once per resolution */
|
|
94
|
+
const flatten = ns === undefined && internal !== false && typeof id === 'string'
|
|
95
|
+
&& target !== identifier_1.ReferenceType.S3MethodPrefix && target !== identifier_1.ReferenceType.S7MethodPrefix;
|
|
96
|
+
const key = flatten ? `${target}\u0000${name}` : '';
|
|
97
|
+
let flattenAt;
|
|
92
98
|
do {
|
|
99
|
+
if (flatten && flattenAt === undefined && definitions === undefined && (current.t !== undefined || current.builtInEnv)) {
|
|
100
|
+
const cached = current.tailCache?.get(key);
|
|
101
|
+
if (cached !== undefined || current.tailCache?.has(key)) {
|
|
102
|
+
return cached;
|
|
103
|
+
}
|
|
104
|
+
flattenAt = current;
|
|
105
|
+
}
|
|
93
106
|
if (layerSkipped(current, ns)) {
|
|
94
107
|
current = current.parent;
|
|
95
108
|
continue;
|
|
@@ -104,7 +117,7 @@ function resolveByTargetType(id, environment, target) {
|
|
|
104
117
|
.toArray();
|
|
105
118
|
}
|
|
106
119
|
else {
|
|
107
|
-
definition = current.
|
|
120
|
+
definition = current.lookup(name);
|
|
108
121
|
if (internal === false) {
|
|
109
122
|
definition = definition?.filter(({ name }) => name === undefined || !identifier_1.Identifier.accessesInternal(name));
|
|
110
123
|
}
|
|
@@ -125,6 +138,9 @@ function resolveByTargetType(id, environment, target) {
|
|
|
125
138
|
}
|
|
126
139
|
}
|
|
127
140
|
if (allOk) {
|
|
141
|
+
if (flattenAt !== undefined) {
|
|
142
|
+
(flattenAt.tailCache ??= new Map()).set(key, definition);
|
|
143
|
+
}
|
|
128
144
|
return definition;
|
|
129
145
|
}
|
|
130
146
|
/* never alias the environment's own array, it is appended to below */
|
|
@@ -142,12 +158,15 @@ function resolveByTargetType(id, environment, target) {
|
|
|
142
158
|
} while (!current.builtInEnv);
|
|
143
159
|
/* the built-in layer is handed over as it is, except to a value position: `filter(df, c > 2)` means the
|
|
144
160
|
column `c`, never `base::c`, and only with nothing left does `ownDefinitions` have its say */
|
|
145
|
-
const known = statedIn(current, name, ns) ?? current.
|
|
161
|
+
const known = statedIn(current, name, ns) ?? current.lookup(name);
|
|
146
162
|
const builtIns = target === identifier_1.ReferenceType.NonFunction ? known?.filter(wantedType) : known;
|
|
147
|
-
|
|
148
|
-
|
|
163
|
+
const result = definitions
|
|
164
|
+
? (builtIns === undefined || builtIns.length === 0 ? definitions : definitions.concat(builtIns))
|
|
165
|
+
: (builtIns === undefined || builtIns.length > 0 ? builtIns : undefined);
|
|
166
|
+
if (flattenAt !== undefined) {
|
|
167
|
+
(flattenAt.tailCache ??= new Map()).set(key, result);
|
|
149
168
|
}
|
|
150
|
-
return
|
|
169
|
+
return result;
|
|
151
170
|
}
|
|
152
171
|
/**
|
|
153
172
|
* The more performant version of {@link resolveByName} when the target type is unknown.
|
|
@@ -168,7 +187,7 @@ function resolveByNameAnyType(id, environment) {
|
|
|
168
187
|
const internal = identifier_1.Identifier.accessesInternal(id);
|
|
169
188
|
/* `current` can already be the built-in environment itself */
|
|
170
189
|
if (current.builtInEnv) {
|
|
171
|
-
const ret = current.
|
|
190
|
+
const ret = current.lookup(name);
|
|
172
191
|
if (ret && cacheable) {
|
|
173
192
|
current.cache ??= new Map();
|
|
174
193
|
current.cache.set(id, ret);
|
|
@@ -181,7 +200,7 @@ function resolveByNameAnyType(id, environment) {
|
|
|
181
200
|
current = current.parent;
|
|
182
201
|
continue;
|
|
183
202
|
}
|
|
184
|
-
let definition = current.
|
|
203
|
+
let definition = current.lookup(name);
|
|
185
204
|
if (definition) {
|
|
186
205
|
if (internal === false) {
|
|
187
206
|
definition = definition.filter(({ name }) => name === undefined || !identifier_1.Identifier.accessesInternal(name));
|
|
@@ -204,7 +223,7 @@ function resolveByNameAnyType(id, environment) {
|
|
|
204
223
|
}
|
|
205
224
|
current = current.parent;
|
|
206
225
|
} while (!current.builtInEnv);
|
|
207
|
-
const builtIns = statedIn(current, name, ns) ?? current.
|
|
226
|
+
const builtIns = statedIn(current, name, ns) ?? current.lookup(name);
|
|
208
227
|
let ret;
|
|
209
228
|
if (definitions) {
|
|
210
229
|
ret = builtIns === undefined ? definitions : definitions.concat(builtIns);
|
|
@@ -135,6 +135,11 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
|
|
|
135
135
|
private edgeInformation;
|
|
136
136
|
private incomingIndex?;
|
|
137
137
|
private readonly types;
|
|
138
|
+
/** The id list of `tag`, created on first use, so a caller adding many ids looks it up once. */
|
|
139
|
+
private typeList;
|
|
140
|
+
private indexType;
|
|
141
|
+
/** Every write above keeps an id listed under one tag at most once, so a lookup never hands the same one out twice. */
|
|
142
|
+
private unindexType;
|
|
138
143
|
private qualifiedNames?;
|
|
139
144
|
toJSON(): DataflowGraphJson;
|
|
140
145
|
/** Gets the {@link DataflowGraphVertexInfo} attached to `id` (searching function definitions too if `includeDefinedFunctions`) and its outgoing edges. */
|
|
@@ -163,7 +168,9 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
|
|
|
163
168
|
private qualifications;
|
|
164
169
|
/** one resolution fills both variants: what costs is resolving the call, not qualifying it */
|
|
165
170
|
private resolveInto;
|
|
166
|
-
/**
|
|
171
|
+
/** set by a `consume` merge, see {@link DataflowGraph#mergeWith|mergeWith()}; the graph must not change after that */
|
|
172
|
+
private consumed;
|
|
173
|
+
/** Drops the cached qualifications, as the graph may no longer imply them. Every mutator runs through here, so the guard covers them all. */
|
|
167
174
|
private dropQualifications;
|
|
168
175
|
/** For a node of the normalized AST: `nodeId` if it is a DFG vertex itself, else the DFG vertices linked to it, else `undefined`. */
|
|
169
176
|
getLinked(nodeId: NodeId): NodeId[] | undefined;
|
|
@@ -175,9 +182,11 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
|
|
|
175
182
|
setIdMap(idMap: AstIdMap): void;
|
|
176
183
|
/** Ids of all toplevel vertices (or all, including those nested in function definitions, if `includeDefinedFunctions`) with their info, see {@link DataflowGraph#edges}. */
|
|
177
184
|
vertices(includeDefinedFunctions: boolean): MapIterator<[NodeId, Vertex]>;
|
|
185
|
+
/** Every vertex carrying `type`, in the order the graph learned of them. */
|
|
178
186
|
verticesOfType<T extends Vertex['tag']>(type: T): MapIterator<[NodeId, Vertex & {
|
|
179
187
|
tag: T;
|
|
180
188
|
}]>;
|
|
189
|
+
/** The ids of {@link DataflowGraph#verticesOfType|verticesOfType}, as a fresh array the caller may keep. */
|
|
181
190
|
vertexIdsOfType<T extends Vertex['tag']>(type: T): NodeId[];
|
|
182
191
|
/** Ids of all edges in the graph together with their edge information, see {@link DataflowGraph#vertices}. */
|
|
183
192
|
edges(): MapIterator<[NodeId, OutgoingEdges]>;
|
|
@@ -195,11 +204,16 @@ export declare class DataflowGraph<Vertex extends DataflowGraphVertexInfo = Data
|
|
|
195
204
|
* Adds `vertex` to the graph, filling in `fallbackEnv` if it carries no environment. `asRoot = false` skips
|
|
196
205
|
* adding it to {@link rootIds|root vertices} (mostly useful when constructing graphs for tests); `overwrite` replaces an existing vertex of the same id.
|
|
197
206
|
*/
|
|
198
|
-
addVertex(vertex: DataflowGraphVertexArgument & Omit<Vertex, keyof DataflowGraphVertexArgument>, fallbackEnv: REnvironmentInformation, asRoot?: boolean, overwrite?: boolean): this;
|
|
207
|
+
addVertex(vertex: DataflowGraphVertexArgument & Omit<Vertex, keyof DataflowGraphVertexArgument>, fallbackEnv: REnvironmentInformation | (() => REnvironmentInformation), asRoot?: boolean, overwrite?: boolean): this;
|
|
199
208
|
addEdge(fromId: NodeId, toId: NodeId, type: EdgeType.ControlEdge, data: Omit<DFControlFlowEdge, 'types'>): this;
|
|
200
209
|
addEdge(fromId: NodeId, toId: NodeId, type: Exclude<EdgeType, EdgeType.ControlEdge> | number): this;
|
|
201
|
-
/**
|
|
202
|
-
|
|
210
|
+
/**
|
|
211
|
+
* Merges `otherGraph` into *this* one in-place (the return value is only for convenience);
|
|
212
|
+
* `mergeRootVertices = false` excludes its root vertices (useful when merging in a function definition).
|
|
213
|
+
*
|
|
214
|
+
* `consume` takes `otherGraph`'s edge tables instead of copying them (perf). Pass it only for a graph that dies with the call.
|
|
215
|
+
*/
|
|
216
|
+
mergeWith(otherGraph: DataflowGraph<Vertex, Edge> | undefined, mergeRootVertices?: boolean, consume?: boolean): this;
|
|
203
217
|
mergeVertices(otherGraph: DataflowGraph<Vertex, Edge>, mergeRootVertices?: boolean): void;
|
|
204
218
|
private mergeEdges;
|
|
205
219
|
/** Marks the vertex referenced by `reference` as a definition, with `sourceIds` as the source vertex ids of the def if known. */
|