@tsplc/compiler 0.2.17 → 0.2.19
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 +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -0,0 +1,546 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { CapturedPrimitive, CapturedValue } from "./graph.ts";
|
|
3
|
+
import type { EntryImports } from "./imports.ts";
|
|
4
|
+
import {
|
|
5
|
+
pascalCase,
|
|
6
|
+
recordTypes,
|
|
7
|
+
unusedName,
|
|
8
|
+
type Layouts,
|
|
9
|
+
} from "./layouts.ts";
|
|
10
|
+
import {
|
|
11
|
+
children,
|
|
12
|
+
tsPath,
|
|
13
|
+
type Model,
|
|
14
|
+
type ModelNode,
|
|
15
|
+
type Owner,
|
|
16
|
+
type Root,
|
|
17
|
+
type Step,
|
|
18
|
+
} from "./model.ts";
|
|
19
|
+
import { constant, type Value } from "./values.ts";
|
|
20
|
+
import { ScanWrites } from "./writes.ts";
|
|
21
|
+
|
|
22
|
+
/** A primitive scan code read from setup and folded, at its TypeScript path. */
|
|
23
|
+
interface FoldedField {
|
|
24
|
+
path: string;
|
|
25
|
+
value: CapturedValue;
|
|
26
|
+
/** The declaration of the field or array holding it, or its root's. */
|
|
27
|
+
declaration: ts.Node;
|
|
28
|
+
/** The IEC width that declaration names. */
|
|
29
|
+
iec?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Where the state setup built lives in the PLC, and how scan code reads it. A field is stored when
|
|
34
|
+
* scan code writes it or its root is exported; what setup fixed otherwise folds where it is read.
|
|
35
|
+
*/
|
|
36
|
+
export class Storage {
|
|
37
|
+
private readonly readConstants = new Set<string>();
|
|
38
|
+
private readonly writes: ScanWrites;
|
|
39
|
+
private readonly storedRoots = new Set<Root>();
|
|
40
|
+
private readonly layoutNames = new Map<string, string>();
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
private readonly model: Model,
|
|
44
|
+
private readonly checker: ts.TypeChecker,
|
|
45
|
+
private readonly layouts: Layouts,
|
|
46
|
+
private readonly imports: EntryImports,
|
|
47
|
+
sources: readonly ts.SourceFile[],
|
|
48
|
+
private readonly kept: (file: string) => boolean,
|
|
49
|
+
private readonly rootModule: (root: Root) => string,
|
|
50
|
+
) {
|
|
51
|
+
this.writes = new ScanWrites(checker, sources);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The declaration symbol of a captured field, to ask whether scan code writes it. */
|
|
55
|
+
private fieldSymbol(holder: ModelNode, name: string): ts.Symbol | undefined {
|
|
56
|
+
if (holder.captured.kind === "instance") {
|
|
57
|
+
const member = this.model.member(this.model.classOf(holder), name);
|
|
58
|
+
return member && this.checker.getSymbolAtLocation(member.name);
|
|
59
|
+
}
|
|
60
|
+
if (!holder.type) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
const variant = this.model.variant(holder.type, holder.captured);
|
|
64
|
+
return (
|
|
65
|
+
variant.getProperty(name) ?? this.checker.getPropertyOfType(variant, name)
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Whether a field holds state the PLC stores, rather than a value fixed at setup. */
|
|
70
|
+
stored(holder: ModelNode, name: string): boolean {
|
|
71
|
+
if (holder.owner?.root.exported) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
return this.written(holder, name);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Whether scan code assigns the field itself. */
|
|
78
|
+
private written(holder: ModelNode, name: string): boolean {
|
|
79
|
+
const symbol = this.fieldSymbol(holder, name);
|
|
80
|
+
return symbol !== undefined && this.writes.includes(symbol);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Array elements are stored when the field holding the array is, or its root is exported. */
|
|
84
|
+
private storedArray(array: ModelNode): boolean {
|
|
85
|
+
const owner = array.owner;
|
|
86
|
+
if (!owner) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
if (owner.root.exported) {
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
const last = owner.steps.at(-1);
|
|
93
|
+
if (!last) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
const holder = this.model.at(owner.root, owner.steps.slice(0, -1));
|
|
97
|
+
return (
|
|
98
|
+
last.kind === "field" &&
|
|
99
|
+
holder !== undefined &&
|
|
100
|
+
this.stored(holder, last.name)
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Dialect text for a stored node's place. */
|
|
105
|
+
pathText(node: ModelNode): string {
|
|
106
|
+
const owner = node.owner;
|
|
107
|
+
if (!owner) {
|
|
108
|
+
throw new Error("An object with no owner has no storage.");
|
|
109
|
+
}
|
|
110
|
+
this.storedRoots.add(owner.root);
|
|
111
|
+
const namespace = this.namespace(owner.root);
|
|
112
|
+
const root = this.flattened(owner.root)
|
|
113
|
+
? namespace
|
|
114
|
+
: `${namespace}.${owner.root.name}`;
|
|
115
|
+
const steps = owner.steps.map((step, index) =>
|
|
116
|
+
this.placeStep(
|
|
117
|
+
this.model.at(owner.root, owner.steps.slice(0, index)),
|
|
118
|
+
step,
|
|
119
|
+
),
|
|
120
|
+
);
|
|
121
|
+
return root + steps.join("");
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Dialect text for one step down from `holder`: a `Record<Enum, T>` key indexes by its enum member. */
|
|
125
|
+
private placeStep(holder: ModelNode | undefined, step: Step): string {
|
|
126
|
+
if (step.kind === "index") {
|
|
127
|
+
return `[${step.index}]`;
|
|
128
|
+
}
|
|
129
|
+
if (/^\d+$/.test(step.name) && holder?.type) {
|
|
130
|
+
return `[${this.keyText(holder.type, step.name)}]`;
|
|
131
|
+
}
|
|
132
|
+
return `.${step.name}`;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The enum member naming a key of a `Record<Enum, T>`. */
|
|
136
|
+
private keyText(type: ts.Type, key: string): string {
|
|
137
|
+
const keyType = recordTypes(type)?.key;
|
|
138
|
+
if (!keyType) {
|
|
139
|
+
return key;
|
|
140
|
+
}
|
|
141
|
+
const member = this.layouts.enumMemberName(keyType, Number(key));
|
|
142
|
+
return member ? `${this.layouts.text(keyType)}.${member}` : key;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A record or instance root becomes its own global list, one global per field. */
|
|
146
|
+
flattened(root: Root): boolean {
|
|
147
|
+
const node = this.model.node(root.value);
|
|
148
|
+
return (
|
|
149
|
+
node?.captured.kind === "object" || node?.captured.kind === "instance"
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private namespace(root: Root): string {
|
|
154
|
+
const name = pascalCase(root.name);
|
|
155
|
+
this.imports.namespaces.set(name, this.rootModule(root));
|
|
156
|
+
return name;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Stores a root whether or not scan code reads it, as exported data must be. */
|
|
160
|
+
use(root: Root): void {
|
|
161
|
+
this.storedRoots.add(root);
|
|
162
|
+
this.namespace(root);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** Roots generated code reached, which need storage declarations. */
|
|
166
|
+
get roots(): ReadonlySet<Root> {
|
|
167
|
+
return this.storedRoots;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Whether anything inside a node is state the PLC stores. */
|
|
171
|
+
private hasState(node: ModelNode): boolean {
|
|
172
|
+
const captured = node.captured;
|
|
173
|
+
if (node.owner?.root.exported) {
|
|
174
|
+
return true;
|
|
175
|
+
}
|
|
176
|
+
if (captured.kind === "instance") {
|
|
177
|
+
return this.instanceFields(node).length > 0;
|
|
178
|
+
}
|
|
179
|
+
if (captured.kind === "array") {
|
|
180
|
+
return (
|
|
181
|
+
this.storedArray(node) ||
|
|
182
|
+
captured.items.some((item) => {
|
|
183
|
+
const inner = this.model.node(item);
|
|
184
|
+
return (
|
|
185
|
+
inner !== undefined && isChild(inner, node) && this.hasState(inner)
|
|
186
|
+
);
|
|
187
|
+
})
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (captured.kind === "object") {
|
|
191
|
+
return Object.entries(captured.fields).some(([name, value]) => {
|
|
192
|
+
if (this.stored(node, name)) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
const inner = this.model.node(value);
|
|
196
|
+
return (
|
|
197
|
+
inner !== undefined &&
|
|
198
|
+
isChild(inner, node, name) &&
|
|
199
|
+
this.hasState(inner)
|
|
200
|
+
);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The stored layout of a node, or undefined when nothing in it is stored. Records and arrays
|
|
208
|
+
* keep their declared type; an instance stores a record of the fields that hold its state.
|
|
209
|
+
*/
|
|
210
|
+
storageType(node: ModelNode): string | undefined {
|
|
211
|
+
const captured = node.captured;
|
|
212
|
+
if (!this.hasState(node)) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
if (captured.kind === "instance") {
|
|
216
|
+
const fields = this.instanceFields(node);
|
|
217
|
+
return this.layoutName(
|
|
218
|
+
`${captured.name}State`,
|
|
219
|
+
fields.map(([field, text]) => `${field}: ${text};`).join(" "),
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
if (captured.kind === "array") {
|
|
223
|
+
const first = captured.items[0] && this.model.node(captured.items[0]);
|
|
224
|
+
if (first?.captured.kind === "instance") {
|
|
225
|
+
const states = captured.items.map((item) =>
|
|
226
|
+
this.storageType(this.model.node(item)!),
|
|
227
|
+
);
|
|
228
|
+
if (new Set(states).size !== 1) {
|
|
229
|
+
throw new Error(
|
|
230
|
+
"Instances in one array must hold the same kinds of state.",
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
return `FixedArray<${states[0]}, ${captured.items.length}>`;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (!node.type) {
|
|
237
|
+
throw new Error("Stored state needs a declared type.");
|
|
238
|
+
}
|
|
239
|
+
return this.layouts.text(node.type, { kind: "node", id: node.id });
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/** Records stored as a whole: anything in them is state. */
|
|
243
|
+
private storedWhole(node: ModelNode): boolean {
|
|
244
|
+
return node.captured.kind !== "instance" && this.hasState(node);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** An instance's stored fields: what scan code writes, and owned objects holding state. */
|
|
248
|
+
instanceFields(node: ModelNode): [string, string][] {
|
|
249
|
+
const captured = node.captured;
|
|
250
|
+
if (captured.kind !== "instance") {
|
|
251
|
+
return [];
|
|
252
|
+
}
|
|
253
|
+
const fields = this.model.fields(this.model.classOf(node));
|
|
254
|
+
return fields.flatMap((member): [string, string][] => {
|
|
255
|
+
const name = member.name.getText();
|
|
256
|
+
const value = captured.fields[name] ?? { kind: "undefined" };
|
|
257
|
+
const inner = this.model.node(value);
|
|
258
|
+
const type = this.checker.getTypeAtLocation(member.name);
|
|
259
|
+
if (inner) {
|
|
260
|
+
if (
|
|
261
|
+
inner.captured.kind === "function" ||
|
|
262
|
+
inner.captured.kind === "service"
|
|
263
|
+
) {
|
|
264
|
+
return [];
|
|
265
|
+
}
|
|
266
|
+
if (!isChild(inner, node, name)) {
|
|
267
|
+
if (!this.stored(node, name)) {
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
// A stored field holds its own copy of the record setup gave it.
|
|
271
|
+
return [[name, this.layouts.text(type, value)]];
|
|
272
|
+
}
|
|
273
|
+
if (inner.captured.kind !== "instance" && this.stored(node, name)) {
|
|
274
|
+
return [[name, this.layouts.text(type, value)]];
|
|
275
|
+
}
|
|
276
|
+
const text = this.storageType(inner);
|
|
277
|
+
return text ? [[name, text]] : [];
|
|
278
|
+
}
|
|
279
|
+
if (!this.stored(node, name)) {
|
|
280
|
+
return [];
|
|
281
|
+
}
|
|
282
|
+
return [[name, this.layouts.declaredText(member, type, value)]];
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private layoutName(base: string, body: string): string {
|
|
287
|
+
const known = this.layoutNames.get(body);
|
|
288
|
+
if (known) {
|
|
289
|
+
return known;
|
|
290
|
+
}
|
|
291
|
+
const name = unusedName(
|
|
292
|
+
base,
|
|
293
|
+
(candidate) =>
|
|
294
|
+
[...this.layoutNames.values()].includes(candidate) ||
|
|
295
|
+
this.layouts.declarations.has(candidate),
|
|
296
|
+
);
|
|
297
|
+
this.layoutNames.set(body, name);
|
|
298
|
+
this.layouts.declarations.set(name, `export interface ${name} { ${body} }`);
|
|
299
|
+
return name;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
rootValue(root: Root): Value {
|
|
303
|
+
const value = root.value;
|
|
304
|
+
if (value.kind === "node") {
|
|
305
|
+
const node = this.model.node(value);
|
|
306
|
+
if (this.kept(root.file) && node.captured.kind !== "service") {
|
|
307
|
+
return this.keptRoot(root);
|
|
308
|
+
}
|
|
309
|
+
return { kind: "static", node };
|
|
310
|
+
}
|
|
311
|
+
if (
|
|
312
|
+
this.kept(root.file) ||
|
|
313
|
+
root.declaration.parent.flags & ts.NodeFlags.Const
|
|
314
|
+
) {
|
|
315
|
+
return primitive(value);
|
|
316
|
+
}
|
|
317
|
+
// A module `let` of a plain module is state in its own global list.
|
|
318
|
+
const namespace = this.namespace(root);
|
|
319
|
+
this.storedRoots.add(root);
|
|
320
|
+
return {
|
|
321
|
+
kind: "code",
|
|
322
|
+
text: `${namespace}.${root.name}`,
|
|
323
|
+
declared: this.checker.getTypeAtLocation(root.declaration.name),
|
|
324
|
+
place: true,
|
|
325
|
+
iec: this.layouts.iecName(root.declaration),
|
|
326
|
+
setter: `${namespace}.${setterName(root.name)}`,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** A binding in a module the program keeps, read by its own name. */
|
|
331
|
+
private keptRoot(root: Root): Value {
|
|
332
|
+
this.imports.kept.add(root.file, root.name);
|
|
333
|
+
return {
|
|
334
|
+
kind: "code",
|
|
335
|
+
text: root.name,
|
|
336
|
+
declared: this.checker.getTypeAtLocation(root.declaration.name),
|
|
337
|
+
place: !(root.declaration.parent.flags & ts.NodeFlags.Const),
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** A known node or primitive read from a captured object at one step. */
|
|
342
|
+
fromCaptured(value: CapturedValue, holder: ModelNode, step: Step): Value {
|
|
343
|
+
if (value.kind === "node") {
|
|
344
|
+
const node = this.model.node(value);
|
|
345
|
+
// A field scan code assigns as a whole is a place, holding its own copy of what setup gave it.
|
|
346
|
+
if (
|
|
347
|
+
(node.captured.kind === "object" || node.captured.kind === "array") &&
|
|
348
|
+
step.kind === "field" &&
|
|
349
|
+
this.written(holder, step.name)
|
|
350
|
+
) {
|
|
351
|
+
return this.storedField(holder, step);
|
|
352
|
+
}
|
|
353
|
+
return { kind: "static", node };
|
|
354
|
+
}
|
|
355
|
+
const stored =
|
|
356
|
+
step.kind === "field"
|
|
357
|
+
? this.stored(holder, step.name)
|
|
358
|
+
: this.storedArray(holder);
|
|
359
|
+
if (stored) {
|
|
360
|
+
return this.storedField(holder, step);
|
|
361
|
+
}
|
|
362
|
+
if (holder.owner) {
|
|
363
|
+
this.readConstants.add(
|
|
364
|
+
tsPath({
|
|
365
|
+
root: holder.owner.root,
|
|
366
|
+
steps: [...holder.owner.steps, step],
|
|
367
|
+
}),
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
return primitive(value);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** A captured field's declaration: a class member or a record's property. */
|
|
374
|
+
private fieldDeclaration(
|
|
375
|
+
holder: ModelNode,
|
|
376
|
+
name: string,
|
|
377
|
+
): ts.Node | undefined {
|
|
378
|
+
return holder.captured.kind === "instance"
|
|
379
|
+
? this.model.member(this.model.classOf(holder), name)
|
|
380
|
+
: this.fieldSymbol(holder, name)?.valueDeclaration;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** The IEC width a captured field's declaration names. */
|
|
384
|
+
private fieldIec(holder: ModelNode, name: string): string | undefined {
|
|
385
|
+
return this.layouts.iecName(this.fieldDeclaration(holder, name));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** The declaration of the field holding an array. */
|
|
389
|
+
private arrayDeclaration(array: ModelNode): ts.Node | undefined {
|
|
390
|
+
const owner = array.owner;
|
|
391
|
+
const last = owner?.steps.at(-1);
|
|
392
|
+
if (!owner || last?.kind !== "field") {
|
|
393
|
+
return undefined;
|
|
394
|
+
}
|
|
395
|
+
const holder = this.model.at(owner.root, owner.steps.slice(0, -1));
|
|
396
|
+
return holder && this.fieldDeclaration(holder, last.name);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** The IEC width of an array's elements, from the declaration holding the array. */
|
|
400
|
+
private arrayElementIec(array: ModelNode): string | undefined {
|
|
401
|
+
return this.layouts.elementIec(this.arrayDeclaration(array));
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Primitives setup stored in fields and arrays that scan code only reads. Specialization folds
|
|
406
|
+
* them into the code, so they have no storage; this is where their values remain known.
|
|
407
|
+
*/
|
|
408
|
+
folded(): FoldedField[] {
|
|
409
|
+
const found: FoldedField[] = [];
|
|
410
|
+
const visit = (node: ModelNode, owner: Owner, whole: boolean): void => {
|
|
411
|
+
const captured = node.captured;
|
|
412
|
+
// A record or array stored as a whole stores everything in it; a flattened root stores fields.
|
|
413
|
+
const stores =
|
|
414
|
+
whole ||
|
|
415
|
+
(captured.kind === "object" &&
|
|
416
|
+
!(owner.steps.length === 0 && this.flattened(owner.root)) &&
|
|
417
|
+
this.storedWhole(node)) ||
|
|
418
|
+
(captured.kind === "array" && this.storedArray(node));
|
|
419
|
+
for (const [step, value] of children(captured)) {
|
|
420
|
+
const at: Owner = { root: owner.root, steps: [...owner.steps, step] };
|
|
421
|
+
const inner = this.model.node(value);
|
|
422
|
+
if (inner) {
|
|
423
|
+
// Visit what this node owns; other places holding it refer to its owner.
|
|
424
|
+
if (
|
|
425
|
+
inner.owner?.root === at.root &&
|
|
426
|
+
tsPath(inner.owner) === tsPath(at)
|
|
427
|
+
) {
|
|
428
|
+
visit(inner, at, stores);
|
|
429
|
+
}
|
|
430
|
+
continue;
|
|
431
|
+
}
|
|
432
|
+
if (
|
|
433
|
+
value.kind === "undefined" ||
|
|
434
|
+
stores ||
|
|
435
|
+
!this.readConstants.has(tsPath(at))
|
|
436
|
+
) {
|
|
437
|
+
continue;
|
|
438
|
+
}
|
|
439
|
+
if (step.kind === "field" && this.stored(node, step.name)) {
|
|
440
|
+
continue;
|
|
441
|
+
}
|
|
442
|
+
const declaration =
|
|
443
|
+
step.kind === "field"
|
|
444
|
+
? this.fieldDeclaration(node, step.name)
|
|
445
|
+
: this.arrayDeclaration(node);
|
|
446
|
+
const iec =
|
|
447
|
+
step.kind === "field"
|
|
448
|
+
? this.fieldIec(node, step.name)
|
|
449
|
+
: this.arrayElementIec(node);
|
|
450
|
+
found.push({
|
|
451
|
+
path: tsPath(at),
|
|
452
|
+
value,
|
|
453
|
+
declaration: declaration ?? owner.root.declaration,
|
|
454
|
+
...(iec && { iec }),
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
for (const root of this.model.roots) {
|
|
459
|
+
const node = this.model.node(root.value);
|
|
460
|
+
if (
|
|
461
|
+
node?.owner?.root === root &&
|
|
462
|
+
!root.exported &&
|
|
463
|
+
!this.kept(root.file)
|
|
464
|
+
) {
|
|
465
|
+
visit(node, { root, steps: [] }, false);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return found;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
private storedField(holder: ModelNode, step: Step): Value {
|
|
472
|
+
const declared =
|
|
473
|
+
step.kind === "index"
|
|
474
|
+
? holder.type && this.model.elementType(holder.type)
|
|
475
|
+
: this.model.capturedFieldType(holder, step.name);
|
|
476
|
+
const global =
|
|
477
|
+
step.kind === "field" &&
|
|
478
|
+
holder.owner?.steps.length === 0 &&
|
|
479
|
+
this.flattened(holder.owner.root);
|
|
480
|
+
return {
|
|
481
|
+
kind: "code",
|
|
482
|
+
text: `${this.pathText(holder)}${this.placeStep(holder, step)}`,
|
|
483
|
+
declared,
|
|
484
|
+
place: true,
|
|
485
|
+
...(step.kind === "field"
|
|
486
|
+
? {
|
|
487
|
+
iec: this.fieldIec(holder, step.name),
|
|
488
|
+
elementIec: this.layouts.elementIec(
|
|
489
|
+
this.fieldDeclaration(holder, step.name),
|
|
490
|
+
),
|
|
491
|
+
}
|
|
492
|
+
: { iec: this.arrayElementIec(holder) }),
|
|
493
|
+
...(global && {
|
|
494
|
+
setter: `${this.pathText(holder)}.${setterName(step.name)}`,
|
|
495
|
+
}),
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
storedNode(node: ModelNode): boolean {
|
|
500
|
+
return node.captured.kind === "array"
|
|
501
|
+
? this.storedArray(node)
|
|
502
|
+
: this.storedWhole(node);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/** A primitive setup captured, as a value known while specializing. */
|
|
507
|
+
const primitive = (value: CapturedPrimitive): Value => {
|
|
508
|
+
switch (value.kind) {
|
|
509
|
+
case "undefined":
|
|
510
|
+
return constant(undefined);
|
|
511
|
+
case "bigint":
|
|
512
|
+
return constant(BigInt(value.value));
|
|
513
|
+
default:
|
|
514
|
+
return constant(value.value);
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
/** The function a storage module exports to set one of its globals. */
|
|
519
|
+
export const setterName = (name: string): string => `tsplcSet_${name}`;
|
|
520
|
+
|
|
521
|
+
/** Whether `holder` owns `node` directly, at its field `field` when one is given. */
|
|
522
|
+
const isChild = (
|
|
523
|
+
node: ModelNode,
|
|
524
|
+
holder: ModelNode,
|
|
525
|
+
field?: string,
|
|
526
|
+
): boolean => {
|
|
527
|
+
const owner = node.owner;
|
|
528
|
+
const parent = holder.owner;
|
|
529
|
+
if (!owner || !parent || owner.root !== parent.root) {
|
|
530
|
+
return false;
|
|
531
|
+
}
|
|
532
|
+
const last = owner.steps.at(-1);
|
|
533
|
+
const path = owner.steps.slice(0, -1).map(stepText);
|
|
534
|
+
const parentPath = parent.steps.map(stepText);
|
|
535
|
+
if (
|
|
536
|
+
last === undefined ||
|
|
537
|
+
path.length !== parentPath.length ||
|
|
538
|
+
path.some((text, index) => text !== parentPath[index])
|
|
539
|
+
) {
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
return field === undefined || (last.kind === "field" && last.name === field);
|
|
543
|
+
};
|
|
544
|
+
|
|
545
|
+
const stepText = (step: Step): string =>
|
|
546
|
+
step.kind === "index" ? `[${step.index}]` : `.${step.name}`;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
import type { Mapped } from "../positions.ts";
|
|
3
|
+
import type { ModelNode } from "./model.ts";
|
|
4
|
+
|
|
5
|
+
export type FunctionLike =
|
|
6
|
+
| ts.FunctionDeclaration
|
|
7
|
+
| ts.MethodDeclaration
|
|
8
|
+
| ts.ArrowFunction
|
|
9
|
+
| ts.FunctionExpression
|
|
10
|
+
| ts.GetAccessorDeclaration;
|
|
11
|
+
|
|
12
|
+
/** What an expression is while specializing: known now, or code the PLC runs. */
|
|
13
|
+
export type Value =
|
|
14
|
+
/** An object setup built: a class instance, record, array, function or service. */
|
|
15
|
+
| { kind: "static"; node: ModelNode }
|
|
16
|
+
/** A literal or folded primitive. `undefined` is a value too. */
|
|
17
|
+
| { kind: "const"; value: number | boolean | string | undefined | bigint }
|
|
18
|
+
/** An array or record built in scan code from known parts, used before it needs storage. */
|
|
19
|
+
| { kind: "virtual"; items?: Value[]; fields?: Map<string, Value> }
|
|
20
|
+
/** A function value: a module function, method of a known instance, or a local arrow. */
|
|
21
|
+
| { kind: "function"; declaration: FunctionLike; self?: ModelNode; env?: Env }
|
|
22
|
+
/** Code the PLC runs. `declared` is the storage's declared type, before narrowing. */
|
|
23
|
+
| {
|
|
24
|
+
kind: "code";
|
|
25
|
+
text: string | Mapped;
|
|
26
|
+
declared?: ts.Type;
|
|
27
|
+
place?: boolean;
|
|
28
|
+
iec?: string;
|
|
29
|
+
/** For an array place, the IEC width its declaration gives its elements, as `Array<BYTE>` does. */
|
|
30
|
+
elementIec?: string;
|
|
31
|
+
/** For an array place, its layout with the length setup gave it, such as `FixedArray<BYTE, 32>`. */
|
|
32
|
+
layout?: string;
|
|
33
|
+
/** A module global another module stores into through its setter; an import is read-only. */
|
|
34
|
+
setter?: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type Env = Map<ts.Symbol, Value>;
|
|
38
|
+
|
|
39
|
+
export const constant = (
|
|
40
|
+
value: number | boolean | string | undefined | bigint,
|
|
41
|
+
): Value => ({
|
|
42
|
+
kind: "const",
|
|
43
|
+
value,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/** Whether code names a place or a literal, which reads the same however often it is repeated. */
|
|
47
|
+
export const repeatable = (text: string | Mapped): boolean =>
|
|
48
|
+
/^[\w.[\]]+$/.test(String(text));
|
|
49
|
+
|
|
50
|
+
/** Scan code elaboration cannot compile, reported at the node it stopped at. */
|
|
51
|
+
export class SpecializeError extends Error {
|
|
52
|
+
constructor(
|
|
53
|
+
readonly node: ts.Node,
|
|
54
|
+
message: string,
|
|
55
|
+
) {
|
|
56
|
+
super(message);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The properties scan code assigns outside constructors. Fields scan code writes are state; the
|
|
5
|
+
* rest keep what setup gave them.
|
|
6
|
+
*/
|
|
7
|
+
export class ScanWrites {
|
|
8
|
+
private readonly assigned = new Set<ts.Symbol>();
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
private readonly checker: ts.TypeChecker,
|
|
12
|
+
sources: readonly ts.SourceFile[],
|
|
13
|
+
) {
|
|
14
|
+
for (const source of sources) {
|
|
15
|
+
this.collectAssignments(source, false);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Whether scan code assigns the property `symbol` declares. */
|
|
20
|
+
includes(symbol: ts.Symbol): boolean {
|
|
21
|
+
if (this.assigned.has(symbol)) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
// A union's property is written through any member's declaration of it.
|
|
25
|
+
return (symbol.declarations ?? []).some((declaration) => {
|
|
26
|
+
const own = ownSymbol(declaration);
|
|
27
|
+
return own !== undefined && this.assigned.has(own);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private collectAssignments(node: ts.Node, inConstructor: boolean): void {
|
|
32
|
+
const constructing = inConstructor || ts.isConstructorDeclaration(node);
|
|
33
|
+
if (!constructing) {
|
|
34
|
+
const write = (target: ts.Expression): void => {
|
|
35
|
+
const place = unwrapped(target);
|
|
36
|
+
if (ts.isPropertyAccessExpression(place)) {
|
|
37
|
+
this.markAssigned(place.name);
|
|
38
|
+
} else if (ts.isElementAccessExpression(place)) {
|
|
39
|
+
const base = indexed(place.expression);
|
|
40
|
+
if (ts.isPropertyAccessExpression(base)) {
|
|
41
|
+
this.markAssigned(base.name);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
if (
|
|
46
|
+
ts.isBinaryExpression(node) &&
|
|
47
|
+
isAssignment(node.operatorToken.kind)
|
|
48
|
+
) {
|
|
49
|
+
write(node.left);
|
|
50
|
+
}
|
|
51
|
+
if (isUpdate(node)) {
|
|
52
|
+
write(node.operand);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
ts.forEachChild(node, (child) =>
|
|
56
|
+
this.collectAssignments(child, constructing),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private markAssigned(name: ts.MemberName): void {
|
|
61
|
+
const symbol = this.checker.getSymbolAtLocation(name);
|
|
62
|
+
if (!symbol) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
this.assigned.add(symbol);
|
|
66
|
+
// A property of a union is a synthetic symbol over each member's own property.
|
|
67
|
+
for (const declaration of symbol.declarations ?? []) {
|
|
68
|
+
const own = ownSymbol(declaration);
|
|
69
|
+
if (own) {
|
|
70
|
+
this.assigned.add(own);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The symbol a declaration declares, which TypeScript keeps on it but does not expose. */
|
|
77
|
+
const ownSymbol = (declaration: ts.Declaration): ts.Symbol | undefined =>
|
|
78
|
+
(declaration as { symbol?: ts.Symbol }).symbol;
|
|
79
|
+
|
|
80
|
+
/** An assignment target without the parentheses and non-null assertions around it. */
|
|
81
|
+
const unwrapped = (target: ts.Expression): ts.Expression =>
|
|
82
|
+
ts.isParenthesizedExpression(target) || ts.isNonNullExpression(target)
|
|
83
|
+
? unwrapped(target.expression)
|
|
84
|
+
: target;
|
|
85
|
+
|
|
86
|
+
/** What an element access chain such as `a.b[1][2]` indexes: `a.b`. */
|
|
87
|
+
const indexed = (node: ts.Expression): ts.Expression =>
|
|
88
|
+
ts.isElementAccessExpression(node) ? indexed(node.expression) : node;
|
|
89
|
+
|
|
90
|
+
/** Whether an expression steps its operand by one: `x++`, `++x`, `x--` or `--x`. */
|
|
91
|
+
export const isUpdate = (
|
|
92
|
+
node: ts.Node,
|
|
93
|
+
): node is ts.PrefixUnaryExpression | ts.PostfixUnaryExpression =>
|
|
94
|
+
(ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) &&
|
|
95
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
96
|
+
node.operator === ts.SyntaxKind.MinusMinusToken);
|
|
97
|
+
|
|
98
|
+
/** Whether an operator assigns its left operand: `=`, a compound assignment or `??=`. */
|
|
99
|
+
export const isAssignment = (kind: ts.SyntaxKind): boolean =>
|
|
100
|
+
kind >= ts.SyntaxKind.FirstAssignment && kind <= ts.SyntaxKind.LastAssignment;
|