@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
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/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,478 @@
|
|
|
1
|
+
import { identityDigest } from "@tsplc/compiler/ir";
|
|
2
|
+
import ts from "typescript";
|
|
3
|
+
import { modulePath } from "../lowering-key";
|
|
4
|
+
import {
|
|
5
|
+
boundNames,
|
|
6
|
+
type CapturedGraph,
|
|
7
|
+
type CapturedNode,
|
|
8
|
+
type CapturedValue,
|
|
9
|
+
} from "./capture-protocol";
|
|
10
|
+
|
|
11
|
+
/** One step from an owner to the object it holds. */
|
|
12
|
+
export type Step =
|
|
13
|
+
| { kind: "field"; name: string }
|
|
14
|
+
| { kind: "index"; index: number };
|
|
15
|
+
|
|
16
|
+
/** Where a captured object is stored: a root binding and the steps to it. */
|
|
17
|
+
export interface Owner {
|
|
18
|
+
root: Root;
|
|
19
|
+
steps: readonly Step[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The TypeScript path of an owned object or field, as the program names it: `program.diverters.sampleCounters[10]`. */
|
|
23
|
+
export const tsPath = (owner: Owner): string =>
|
|
24
|
+
owner.root.name + owner.steps.map((step) => stepPath(step, String)).join("");
|
|
25
|
+
|
|
26
|
+
/** One step of a path: an index, a numeric key in brackets as `key` spells it, or a field. */
|
|
27
|
+
export const stepPath = (step: Step, key: (name: string) => string): string => {
|
|
28
|
+
if (step.kind === "index") {
|
|
29
|
+
return `[${step.index}]`;
|
|
30
|
+
}
|
|
31
|
+
return /^\d+$/.test(step.name) ? `[${key(step.name)}]` : `.${step.name}`;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** Where `holder`, owned at `owner`, puts what it holds at `step`. */
|
|
35
|
+
export const ownerAt = (owner: Owner, step: Step): Owner => ({
|
|
36
|
+
root: owner.root,
|
|
37
|
+
steps: [...owner.steps, step],
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const sameStep = (left: Step, right: Step): boolean =>
|
|
41
|
+
left.kind === "index"
|
|
42
|
+
? right.kind === "index" && left.index === right.index
|
|
43
|
+
: right.kind === "field" && left.name === right.name;
|
|
44
|
+
|
|
45
|
+
/** The members of a union, or the type itself. */
|
|
46
|
+
export const unionMembers = (type: ts.Type): readonly ts.Type[] =>
|
|
47
|
+
type.isUnion() ? type.types : [type];
|
|
48
|
+
|
|
49
|
+
export const isExported = (declaration: ts.Declaration): boolean =>
|
|
50
|
+
Boolean(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export);
|
|
51
|
+
|
|
52
|
+
/** Whether a module's export list names its binding `name`: `export { name }`. */
|
|
53
|
+
const listedExport = (source: ts.SourceFile, name: string): boolean =>
|
|
54
|
+
source.statements.some(
|
|
55
|
+
(statement) =>
|
|
56
|
+
ts.isExportDeclaration(statement) &&
|
|
57
|
+
!statement.moduleSpecifier &&
|
|
58
|
+
!statement.isTypeOnly &&
|
|
59
|
+
statement.exportClause !== undefined &&
|
|
60
|
+
ts.isNamedExports(statement.exportClause) &&
|
|
61
|
+
statement.exportClause.elements.some(
|
|
62
|
+
(element) =>
|
|
63
|
+
!element.isTypeOnly &&
|
|
64
|
+
(element.propertyName ?? element.name).text === name,
|
|
65
|
+
),
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
/** Each step from a captured object to what it holds, with the value there. */
|
|
69
|
+
export const children = (captured: CapturedNode): [Step, CapturedValue][] => {
|
|
70
|
+
if (captured.kind === "array") {
|
|
71
|
+
return captured.items.map((item, index) => [
|
|
72
|
+
{ kind: "index", index },
|
|
73
|
+
item,
|
|
74
|
+
]);
|
|
75
|
+
}
|
|
76
|
+
if (captured.kind === "object" || captured.kind === "instance") {
|
|
77
|
+
return Object.entries(captured.fields).map(([name, field]) => [
|
|
78
|
+
{ kind: "field", name },
|
|
79
|
+
field,
|
|
80
|
+
]);
|
|
81
|
+
}
|
|
82
|
+
return [];
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
/** The value one step down from a captured object, when it has one there. */
|
|
86
|
+
const child = (
|
|
87
|
+
captured: CapturedNode,
|
|
88
|
+
step: Step,
|
|
89
|
+
): CapturedValue | undefined => {
|
|
90
|
+
if (step.kind === "index") {
|
|
91
|
+
return captured.kind === "array" ? captured.items[step.index] : undefined;
|
|
92
|
+
}
|
|
93
|
+
return captured.kind === "object" || captured.kind === "instance"
|
|
94
|
+
? captured.fields[step.name]
|
|
95
|
+
: undefined;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
/** A field a class declares: a property, or a constructor parameter property. */
|
|
99
|
+
type ClassField = ts.PropertyDeclaration | ts.ParameterPropertyDeclaration;
|
|
100
|
+
|
|
101
|
+
export interface Root {
|
|
102
|
+
file: string;
|
|
103
|
+
name: string;
|
|
104
|
+
/** The variable, or the element of a destructuring declaration, that names the root. */
|
|
105
|
+
declaration: ts.VariableDeclaration | ts.BindingElement;
|
|
106
|
+
/**
|
|
107
|
+
* Whether something outside the program addresses it: an export the hardware, HMI and server
|
|
108
|
+
* read, or a binding the config's `io` reaches. Every field of such a root is stored.
|
|
109
|
+
*/
|
|
110
|
+
exported: boolean;
|
|
111
|
+
/** Whether the config's `io` reaches it, so the hardware may write any field of it. */
|
|
112
|
+
bound: boolean;
|
|
113
|
+
/** What setup bound it to. */
|
|
114
|
+
value: CapturedValue;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export interface ModelNode {
|
|
118
|
+
id: number;
|
|
119
|
+
/**
|
|
120
|
+
* The id a reference to the object holds at runtime: a digest of where setup put it, so an
|
|
121
|
+
* object added elsewhere changes no stored reference across an online change.
|
|
122
|
+
*/
|
|
123
|
+
reference: number;
|
|
124
|
+
captured: CapturedNode;
|
|
125
|
+
/** The declared type where the object is stored; undefined for functions and services. */
|
|
126
|
+
type?: ts.Type;
|
|
127
|
+
/** The first place setup reached it. Every other place holding it refers to this one. */
|
|
128
|
+
owner?: Owner;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The captured setup graph with each object's owner and declared type. */
|
|
132
|
+
export class Model {
|
|
133
|
+
readonly nodes: ModelNode[];
|
|
134
|
+
readonly roots: Root[] = [];
|
|
135
|
+
/** The paths the hardware writes every scan, with the input address of each. */
|
|
136
|
+
readonly inputs: ReadonlyMap<string, string>;
|
|
137
|
+
private readonly projectRoot: string;
|
|
138
|
+
|
|
139
|
+
constructor(
|
|
140
|
+
readonly graph: CapturedGraph,
|
|
141
|
+
readonly checker: ts.TypeChecker,
|
|
142
|
+
readonly program: ts.Program,
|
|
143
|
+
entry: string,
|
|
144
|
+
projectRoot: string,
|
|
145
|
+
{
|
|
146
|
+
bound,
|
|
147
|
+
inputs,
|
|
148
|
+
}: {
|
|
149
|
+
/** The bindings the config's `io` reaches state through. */
|
|
150
|
+
bound: ReadonlySet<string>;
|
|
151
|
+
/** The paths the config's `io` binds to inputs, with each input's address. */
|
|
152
|
+
inputs: ReadonlyMap<string, string>;
|
|
153
|
+
},
|
|
154
|
+
) {
|
|
155
|
+
this.inputs = inputs;
|
|
156
|
+
this.nodes = graph.nodes.map((captured, id) => ({
|
|
157
|
+
id,
|
|
158
|
+
reference: id,
|
|
159
|
+
captured,
|
|
160
|
+
}));
|
|
161
|
+
// Dependencies own what they build before the entry module's instances refer to it.
|
|
162
|
+
const ordered = [
|
|
163
|
+
...graph.bindings.filter((binding) => binding.file !== entry),
|
|
164
|
+
...graph.bindings.filter((binding) => binding.file === entry),
|
|
165
|
+
];
|
|
166
|
+
for (const binding of ordered) {
|
|
167
|
+
const declaration = this.binding(binding.file, binding.name);
|
|
168
|
+
const root: Root = {
|
|
169
|
+
file: binding.file,
|
|
170
|
+
name: binding.name,
|
|
171
|
+
declaration,
|
|
172
|
+
exported:
|
|
173
|
+
isExported(declaration) ||
|
|
174
|
+
listedExport(this.sourceFile(binding.file), binding.name) ||
|
|
175
|
+
bound.has(binding.name),
|
|
176
|
+
bound: bound.has(binding.name),
|
|
177
|
+
value: binding.value,
|
|
178
|
+
};
|
|
179
|
+
this.roots.push(root);
|
|
180
|
+
this.own(
|
|
181
|
+
binding.value,
|
|
182
|
+
{ root, steps: [] },
|
|
183
|
+
checker.getTypeAtLocation(declaration.name),
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
this.projectRoot = projectRoot;
|
|
187
|
+
this.assignReferences();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Where setup put an object, as generated names and reference ids are made from: its root's
|
|
192
|
+
* module and its path from there. Undefined for an object no root owns.
|
|
193
|
+
*/
|
|
194
|
+
identity(node: ModelNode): string | undefined {
|
|
195
|
+
return node.owner && this.ownerIdentity(node.owner);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private ownerIdentity(owner: Owner): string {
|
|
199
|
+
return `${modulePath(this.projectRoot, owner.root.file)}:${tsPath(owner)}`;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Whether `node` is owned exactly at `owner`, rather than only held there. */
|
|
203
|
+
ownsAt(node: ModelNode, owner: Owner): boolean {
|
|
204
|
+
const own = node.owner;
|
|
205
|
+
return (
|
|
206
|
+
own !== undefined &&
|
|
207
|
+
own.root === owner.root &&
|
|
208
|
+
own.steps.length === owner.steps.length &&
|
|
209
|
+
own.steps.every((step, index) => {
|
|
210
|
+
const other = owner.steps[index];
|
|
211
|
+
return other !== undefined && sameStep(step, other);
|
|
212
|
+
})
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** What setup left in a record's or instance's field: undefined when it set none. */
|
|
217
|
+
field(holder: ModelNode, name: string): CapturedValue {
|
|
218
|
+
return (
|
|
219
|
+
child(holder.captured, { kind: "field", name }) ?? { kind: "undefined" }
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** Gives each object setup put somewhere its reference id, from its identity. */
|
|
224
|
+
private assignReferences(): void {
|
|
225
|
+
const taken = new Map<number, Owner>();
|
|
226
|
+
for (const node of this.nodes) {
|
|
227
|
+
const owner = node.owner;
|
|
228
|
+
if (!owner) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
const reference =
|
|
232
|
+
Number.parseInt(identityDigest(this.ownerIdentity(owner)), 16) &
|
|
233
|
+
0x7fffffff;
|
|
234
|
+
const other = taken.get(reference);
|
|
235
|
+
if (other) {
|
|
236
|
+
throw new Error(
|
|
237
|
+
`${tsPath(other)} and ${tsPath(owner)} would hold the same reference id; rename one of them.`,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
taken.set(reference, owner);
|
|
241
|
+
node.reference = reference;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** The object a captured value refers to; undefined for a primitive. */
|
|
246
|
+
node(value: Extract<CapturedValue, { kind: "node" }>): ModelNode;
|
|
247
|
+
node(value: CapturedValue): ModelNode | undefined;
|
|
248
|
+
node(value: CapturedValue): ModelNode | undefined {
|
|
249
|
+
if (value.kind !== "node") {
|
|
250
|
+
return undefined;
|
|
251
|
+
}
|
|
252
|
+
const node = this.nodes[value.id];
|
|
253
|
+
if (!node) {
|
|
254
|
+
throw new Error(`The setup graph has no object ${value.id}.`);
|
|
255
|
+
}
|
|
256
|
+
return node;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private binding(
|
|
260
|
+
file: string,
|
|
261
|
+
name: string,
|
|
262
|
+
): ts.VariableDeclaration | ts.BindingElement {
|
|
263
|
+
const declarations = this.sourceFile(file).statements.flatMap(
|
|
264
|
+
(statement) =>
|
|
265
|
+
ts.isVariableStatement(statement)
|
|
266
|
+
? [...statement.declarationList.declarations]
|
|
267
|
+
: [],
|
|
268
|
+
);
|
|
269
|
+
for (const declaration of declarations) {
|
|
270
|
+
const named = boundNames(declaration.name).find(
|
|
271
|
+
(bound) => bound.text === name,
|
|
272
|
+
)?.parent;
|
|
273
|
+
if (
|
|
274
|
+
named &&
|
|
275
|
+
(ts.isVariableDeclaration(named) || ts.isBindingElement(named))
|
|
276
|
+
) {
|
|
277
|
+
return named;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
throw new Error(`${file}: no module binding ${name}.`);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** The node at a path from a root, following captured values. */
|
|
284
|
+
at(root: Root, steps: readonly Step[]): ModelNode | undefined {
|
|
285
|
+
const value = steps.reduce<CapturedValue | undefined>((current, step) => {
|
|
286
|
+
const node = current && this.node(current);
|
|
287
|
+
return node && child(node.captured, step);
|
|
288
|
+
}, root.value);
|
|
289
|
+
return value && this.node(value);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** A program source by its file name. */
|
|
293
|
+
sourceFile(file: string): ts.SourceFile {
|
|
294
|
+
const source = this.program.getSourceFile(file);
|
|
295
|
+
if (!source) {
|
|
296
|
+
throw new Error(`${file} is not part of the program.`);
|
|
297
|
+
}
|
|
298
|
+
return source;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** The class a captured instance belongs to. */
|
|
302
|
+
classOf(node: ModelNode): ts.ClassDeclaration {
|
|
303
|
+
const captured = node.captured;
|
|
304
|
+
if (captured.kind !== "instance") {
|
|
305
|
+
throw new Error("Not a class instance.");
|
|
306
|
+
}
|
|
307
|
+
const source = this.program.getSourceFile(captured.file);
|
|
308
|
+
const found = source?.statements.find(
|
|
309
|
+
(statement): statement is ts.ClassDeclaration =>
|
|
310
|
+
ts.isClassDeclaration(statement) &&
|
|
311
|
+
statement.name?.text === captured.name,
|
|
312
|
+
);
|
|
313
|
+
if (!found) {
|
|
314
|
+
throw new Error(`${captured.file}: no class ${captured.name}.`);
|
|
315
|
+
}
|
|
316
|
+
return found;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** The nearest member of a class or the classes it extends that is `name` and passes `is`. */
|
|
320
|
+
memberNamed<T extends ts.ClassElement>(
|
|
321
|
+
declaration: ts.ClassDeclaration,
|
|
322
|
+
name: string,
|
|
323
|
+
is: (member: ts.ClassElement) => member is T,
|
|
324
|
+
): T | undefined {
|
|
325
|
+
return this.members(declaration).find(
|
|
326
|
+
(member): member is T => is(member) && member.name?.getText() === name,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** A class field's declaration: a property, or a constructor parameter property. */
|
|
331
|
+
member(
|
|
332
|
+
declaration: ts.ClassDeclaration,
|
|
333
|
+
name: string,
|
|
334
|
+
): ClassField | undefined {
|
|
335
|
+
return this.fields(declaration).find(
|
|
336
|
+
(field) => field.name.getText() === name,
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/** The class and the classes it extends, the class first. */
|
|
341
|
+
ancestry(declaration: ts.ClassDeclaration): ts.ClassDeclaration[] {
|
|
342
|
+
const base = declaration.heritageClauses?.find(
|
|
343
|
+
(clause) => clause.token === ts.SyntaxKind.ExtendsKeyword,
|
|
344
|
+
)?.types[0]?.expression;
|
|
345
|
+
const symbol = base && this.checker.getSymbolAtLocation(base);
|
|
346
|
+
const resolved =
|
|
347
|
+
symbol && symbol.flags & ts.SymbolFlags.Alias
|
|
348
|
+
? this.checker.getAliasedSymbol(symbol)
|
|
349
|
+
: symbol;
|
|
350
|
+
const parent = resolved?.declarations?.find(ts.isClassDeclaration);
|
|
351
|
+
return parent ? [declaration, ...this.ancestry(parent)] : [declaration];
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** A class's own and inherited members, nearest first, as a lookup finds an override before what it overrides. */
|
|
355
|
+
members(declaration: ts.ClassDeclaration): ts.ClassElement[] {
|
|
356
|
+
return this.ancestry(declaration).flatMap((owner) => [...owner.members]);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** A class's fields, inherited ones first, each in declaration order: properties and constructor parameter properties. */
|
|
360
|
+
fields(declaration: ts.ClassDeclaration): ClassField[] {
|
|
361
|
+
return this.ancestry(declaration)
|
|
362
|
+
.reverse()
|
|
363
|
+
.flatMap((owner) => this.ownFields(owner));
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
private ownFields(declaration: ts.ClassDeclaration): ClassField[] {
|
|
367
|
+
return declaration.members.flatMap((member): ClassField[] => {
|
|
368
|
+
if (ts.isPropertyDeclaration(member)) {
|
|
369
|
+
return [member];
|
|
370
|
+
}
|
|
371
|
+
if (ts.isConstructorDeclaration(member)) {
|
|
372
|
+
return member.parameters.filter(
|
|
373
|
+
(parameter): parameter is ts.ParameterPropertyDeclaration =>
|
|
374
|
+
ts.isParameterPropertyDeclaration(parameter, member),
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
return [];
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** The declared type of a field of a captured instance or record. */
|
|
382
|
+
capturedFieldType(holder: ModelNode, name: string): ts.Type | undefined {
|
|
383
|
+
if (holder.captured.kind === "instance") {
|
|
384
|
+
// An instance of a generic class has its fields at its own type arguments.
|
|
385
|
+
const instantiated =
|
|
386
|
+
holder.type && this.checker.getPropertyOfType(holder.type, name);
|
|
387
|
+
if (instantiated) {
|
|
388
|
+
return this.checker.getTypeOfSymbol(instantiated);
|
|
389
|
+
}
|
|
390
|
+
const member = this.member(this.classOf(holder), name);
|
|
391
|
+
return member && this.checker.getTypeAtLocation(member.name);
|
|
392
|
+
}
|
|
393
|
+
return holder.type && this.fieldType(holder.type, name, holder.captured);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** The declared type of a field of `type` holding `value`, choosing the union member it matches. */
|
|
397
|
+
fieldType(
|
|
398
|
+
type: ts.Type,
|
|
399
|
+
name: string,
|
|
400
|
+
holder: CapturedNode,
|
|
401
|
+
): ts.Type | undefined {
|
|
402
|
+
const variant = this.variant(type, holder);
|
|
403
|
+
const property = variant.getProperty(name);
|
|
404
|
+
return property && this.checker.getTypeOfSymbol(property);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/** The member of a union of object types that a captured object is, by its literal fields. */
|
|
408
|
+
variant(type: ts.Type, value: CapturedNode): ts.Type {
|
|
409
|
+
const candidates = unionMembers(type).filter(
|
|
410
|
+
(member) => !(member.flags & ts.TypeFlags.Undefined),
|
|
411
|
+
);
|
|
412
|
+
const [first] = candidates;
|
|
413
|
+
if (!first) {
|
|
414
|
+
return type;
|
|
415
|
+
}
|
|
416
|
+
if (candidates.length === 1 || value.kind !== "object") {
|
|
417
|
+
return first;
|
|
418
|
+
}
|
|
419
|
+
return (
|
|
420
|
+
candidates.find((candidate) =>
|
|
421
|
+
Object.entries(value.fields).every(([name, field]) => {
|
|
422
|
+
const property = candidate.getProperty(name);
|
|
423
|
+
if (!property) {
|
|
424
|
+
return false;
|
|
425
|
+
}
|
|
426
|
+
const fieldType = this.checker.getTypeOfSymbol(property);
|
|
427
|
+
return (
|
|
428
|
+
!fieldType.isLiteral() ||
|
|
429
|
+
field.kind !== "string" ||
|
|
430
|
+
fieldType.value === field.value
|
|
431
|
+
);
|
|
432
|
+
}),
|
|
433
|
+
) ?? first
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/** The type of an array's or tuple's elements. */
|
|
438
|
+
elementType(type: ts.Type): ts.Type | undefined {
|
|
439
|
+
return this.checker.getIndexTypeOfType(
|
|
440
|
+
this.checker.getNonNullableType(type),
|
|
441
|
+
ts.IndexKind.Number,
|
|
442
|
+
);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Gives each object the root's shortest path to it as its owner, breadth first: an object in an array whose
|
|
447
|
+
* elements also refer to each other, as a chain does, is stored in the array, not inside its neighbour.
|
|
448
|
+
*/
|
|
449
|
+
private own(
|
|
450
|
+
value: CapturedValue,
|
|
451
|
+
owner: Owner,
|
|
452
|
+
type: ts.Type | undefined,
|
|
453
|
+
): void {
|
|
454
|
+
const queue = [{ owner, type, value }];
|
|
455
|
+
for (const next of queue) {
|
|
456
|
+
const node = this.node(next.value);
|
|
457
|
+
if (!node || node.owner) {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
node.owner = next.owner;
|
|
461
|
+
node.type = next.type;
|
|
462
|
+
const element =
|
|
463
|
+
node.captured.kind === "array" && next.type
|
|
464
|
+
? this.elementType(next.type)
|
|
465
|
+
: undefined;
|
|
466
|
+
for (const [step, held] of children(node.captured)) {
|
|
467
|
+
queue.push({
|
|
468
|
+
owner: ownerAt(next.owner, step),
|
|
469
|
+
type:
|
|
470
|
+
step.kind === "index"
|
|
471
|
+
? element
|
|
472
|
+
: this.capturedFieldType(node, step.name),
|
|
473
|
+
value: held,
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { Model } from "./model";
|
|
3
|
+
|
|
4
|
+
/** `base`, or else the first of `base_2`, `base_3`… that `taken` does not hold. */
|
|
5
|
+
export const unusedName = (
|
|
6
|
+
base: string,
|
|
7
|
+
taken: (name: string) => boolean,
|
|
8
|
+
): string => {
|
|
9
|
+
if (!taken(base)) {
|
|
10
|
+
return base;
|
|
11
|
+
}
|
|
12
|
+
for (let index = 2; ; index++) {
|
|
13
|
+
const name = `${base}_${index}`;
|
|
14
|
+
if (!taken(name)) {
|
|
15
|
+
return name;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The names the generated modules use at module level: what they declare, and what they import
|
|
22
|
+
* the program's constants, enums, tasks and storage as. Every part of elaboration that makes one up
|
|
23
|
+
* asks here, so two things of one name never meet, whichever modules the program wrote them in.
|
|
24
|
+
*
|
|
25
|
+
* What a generated module declares keeps the name it always had, since a routine's or a type's
|
|
26
|
+
* name reaches the PLC. An import is local to the generated TypeScript, so it is the one renamed.
|
|
27
|
+
*/
|
|
28
|
+
export class Names {
|
|
29
|
+
private readonly declared = new Set<string>();
|
|
30
|
+
/** The name each import is read by, by what it imports. */
|
|
31
|
+
private readonly imports = new Map<string, string>();
|
|
32
|
+
private readonly imported = new Set<string>();
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
/** Names the entry module is sure to declare, which no import takes however early it is made. */
|
|
36
|
+
private readonly reserved: ReadonlySet<string>,
|
|
37
|
+
) {}
|
|
38
|
+
|
|
39
|
+
/** Notes that a generated module declares `name`, which imports made from now on avoid. */
|
|
40
|
+
declare(name: string): void {
|
|
41
|
+
this.declared.add(name);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Whether an import is read by `name`, which a declaration made after it must then avoid. */
|
|
45
|
+
importedAs(name: string): boolean {
|
|
46
|
+
return this.imported.has(name);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The name generated code reads an import by: its own, unless that is taken, reserved, or one
|
|
51
|
+
* of `avoided`. `key` says what is imported, so the same thing is read by the same name everywhere.
|
|
52
|
+
*/
|
|
53
|
+
import(
|
|
54
|
+
key: string,
|
|
55
|
+
name: string,
|
|
56
|
+
avoided: ReadonlySet<string> = new Set(),
|
|
57
|
+
): string {
|
|
58
|
+
const known = this.imports.get(key);
|
|
59
|
+
if (known) {
|
|
60
|
+
return known;
|
|
61
|
+
}
|
|
62
|
+
const local = unusedName(
|
|
63
|
+
name,
|
|
64
|
+
(candidate) =>
|
|
65
|
+
this.imported.has(candidate) ||
|
|
66
|
+
this.declared.has(candidate) ||
|
|
67
|
+
this.reserved.has(candidate) ||
|
|
68
|
+
avoided.has(candidate),
|
|
69
|
+
);
|
|
70
|
+
this.imports.set(key, local);
|
|
71
|
+
this.imported.add(local);
|
|
72
|
+
return local;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The names the entry module is sure to declare under the program's own name for the thing: a
|
|
78
|
+
* module's function that takes no parameters is one routine, named as the function is. A function
|
|
79
|
+
* with parameters may become several routines, each named with a digest, so it reserves nothing.
|
|
80
|
+
*/
|
|
81
|
+
export const routineNames = (
|
|
82
|
+
sources: readonly ts.SourceFile[],
|
|
83
|
+
): Set<string> => {
|
|
84
|
+
const names = new Set<string>();
|
|
85
|
+
for (const source of sources) {
|
|
86
|
+
for (const statement of source.statements) {
|
|
87
|
+
if (
|
|
88
|
+
ts.isFunctionDeclaration(statement) &&
|
|
89
|
+
statement.name &&
|
|
90
|
+
!statement.parameters.length
|
|
91
|
+
) {
|
|
92
|
+
names.add(statement.name.text);
|
|
93
|
+
}
|
|
94
|
+
if (!ts.isVariableStatement(statement)) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
98
|
+
const initializer = declaration.initializer;
|
|
99
|
+
if (
|
|
100
|
+
ts.isIdentifier(declaration.name) &&
|
|
101
|
+
initializer &&
|
|
102
|
+
(ts.isArrowFunction(initializer) ||
|
|
103
|
+
ts.isFunctionExpression(initializer)) &&
|
|
104
|
+
!initializer.parameters.length
|
|
105
|
+
) {
|
|
106
|
+
names.add(declaration.name.text);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return names;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The names a root's storage module may declare: one variable for each field of a root record or
|
|
116
|
+
* instance. A storage module imports every enum and type the generated types use, so those give way.
|
|
117
|
+
*/
|
|
118
|
+
export const rootFieldNames = (model: Model): Set<string> => {
|
|
119
|
+
const names = new Set<string>();
|
|
120
|
+
for (const root of model.roots) {
|
|
121
|
+
const node = model.node(root.value);
|
|
122
|
+
if (node?.captured.kind === "object") {
|
|
123
|
+
for (const field of Object.keys(node.captured.fields)) {
|
|
124
|
+
names.add(field);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (node?.captured.kind === "instance") {
|
|
128
|
+
for (const member of model.fields(model.classOf(node))) {
|
|
129
|
+
names.add(member.name.getText());
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return names;
|
|
134
|
+
};
|