@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,851 @@
|
|
|
1
|
+
import { dirname, join, resolve } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
type Constant,
|
|
4
|
+
type Diagnostic,
|
|
5
|
+
error,
|
|
6
|
+
identityDigest,
|
|
7
|
+
type PlcModule,
|
|
8
|
+
type SourceSpan,
|
|
9
|
+
type TypeId,
|
|
10
|
+
} from "@tsplc/compiler/ir";
|
|
11
|
+
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
12
|
+
import ts from "typescript";
|
|
13
|
+
import { modulePath } from "../lowering-key";
|
|
14
|
+
import { plcSources } from "../module-graph";
|
|
15
|
+
import { from, joined, type Mapped, type Positions } from "../positions";
|
|
16
|
+
import { inSdk, namesSdk, sdkModules, unaliased } from "../sdk";
|
|
17
|
+
import { defaultNumericTypes } from "../types";
|
|
18
|
+
import type { BoundPath, CompileDebug } from "../typescript";
|
|
19
|
+
import type { CapturedValue } from "./capture-protocol";
|
|
20
|
+
import { GenericTypes } from "./generics";
|
|
21
|
+
import { captureGraph } from "./graph";
|
|
22
|
+
import { importPath } from "./imports";
|
|
23
|
+
import {
|
|
24
|
+
plainName,
|
|
25
|
+
quotedField,
|
|
26
|
+
stringEnums,
|
|
27
|
+
type TypeDeclarations,
|
|
28
|
+
} from "./layouts";
|
|
29
|
+
import { Model, type Root } from "./model";
|
|
30
|
+
import { unusedName } from "./names";
|
|
31
|
+
import {
|
|
32
|
+
constantInitializer,
|
|
33
|
+
definesTask,
|
|
34
|
+
Reduction,
|
|
35
|
+
serviceClients,
|
|
36
|
+
type Uncompiled,
|
|
37
|
+
} from "./reduce";
|
|
38
|
+
import { Retention, retentionDeclarations } from "./retention";
|
|
39
|
+
import { closingBrace, Specializer } from "./specialize";
|
|
40
|
+
import type { RootField } from "./storage";
|
|
41
|
+
import { SpecializeError } from "./values";
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A module the compiled program keeps as written: only types, enums, constants, tasks and the
|
|
45
|
+
* service client. Everything else is plain TypeScript that elaboration compiles from its setup graph.
|
|
46
|
+
* A const that scan code writes through, or that `io` binds the hardware to, is state, not a constant.
|
|
47
|
+
*/
|
|
48
|
+
const keptModule = (
|
|
49
|
+
source: ts.SourceFile,
|
|
50
|
+
checker: ts.TypeChecker,
|
|
51
|
+
sdk: string,
|
|
52
|
+
{
|
|
53
|
+
written,
|
|
54
|
+
bound,
|
|
55
|
+
}: { written: ReadonlySet<ts.Symbol>; bound: ReadonlySet<string> },
|
|
56
|
+
): boolean => {
|
|
57
|
+
const work = taskWork(source, checker, sdk);
|
|
58
|
+
return source.statements.every((statement) => {
|
|
59
|
+
if (
|
|
60
|
+
ts.isFunctionDeclaration(statement) &&
|
|
61
|
+
statement.name &&
|
|
62
|
+
work.has(statement.name.text)
|
|
63
|
+
) {
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
if (
|
|
67
|
+
ts.isImportDeclaration(statement) ||
|
|
68
|
+
ts.isExportDeclaration(statement) ||
|
|
69
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
70
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
71
|
+
ts.isEnumDeclaration(statement)
|
|
72
|
+
) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
if (!ts.isVariableStatement(statement)) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return statement.declarationList.declarations.every((declaration) => {
|
|
79
|
+
const symbol = checker.getSymbolAtLocation(declaration.name);
|
|
80
|
+
return (
|
|
81
|
+
declaration.initializer &&
|
|
82
|
+
constantInitializer(declaration.initializer, checker, sdk) &&
|
|
83
|
+
!(symbol && written.has(symbol)) &&
|
|
84
|
+
!(ts.isIdentifier(declaration.name) && bound.has(declaration.name.text))
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** The identifier a place is reached from: `a` of `a.b[i].c`. */
|
|
91
|
+
const placeRoot = (node: ts.Expression): ts.Identifier | undefined => {
|
|
92
|
+
if (ts.isIdentifier(node)) {
|
|
93
|
+
return node;
|
|
94
|
+
}
|
|
95
|
+
if (
|
|
96
|
+
ts.isPropertyAccessExpression(node) ||
|
|
97
|
+
ts.isElementAccessExpression(node) ||
|
|
98
|
+
ts.isParenthesizedExpression(node) ||
|
|
99
|
+
ts.isNonNullExpression(node)
|
|
100
|
+
) {
|
|
101
|
+
return placeRoot(node.expression);
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/** The bindings `sources` assign, or assign a field or element of, through any import of them. */
|
|
107
|
+
const writtenBindings = (
|
|
108
|
+
sources: readonly ts.SourceFile[],
|
|
109
|
+
checker: ts.TypeChecker,
|
|
110
|
+
): Set<ts.Symbol> => {
|
|
111
|
+
const written = new Set<ts.Symbol>();
|
|
112
|
+
const note = (target: ts.Expression) => {
|
|
113
|
+
const root = placeRoot(target);
|
|
114
|
+
const symbol = root && checker.getSymbolAtLocation(root);
|
|
115
|
+
if (symbol) {
|
|
116
|
+
written.add(unaliased(checker, symbol));
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
const visit = (node: ts.Node): void => {
|
|
120
|
+
if (
|
|
121
|
+
ts.isBinaryExpression(node) &&
|
|
122
|
+
node.operatorToken.kind >= ts.SyntaxKind.FirstAssignment &&
|
|
123
|
+
node.operatorToken.kind <= ts.SyntaxKind.LastAssignment
|
|
124
|
+
) {
|
|
125
|
+
note(node.left);
|
|
126
|
+
}
|
|
127
|
+
if (
|
|
128
|
+
(ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) &&
|
|
129
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
130
|
+
node.operator === ts.SyntaxKind.MinusMinusToken)
|
|
131
|
+
) {
|
|
132
|
+
note(node.operand);
|
|
133
|
+
}
|
|
134
|
+
ts.forEachChild(node, visit);
|
|
135
|
+
};
|
|
136
|
+
for (const source of sources) {
|
|
137
|
+
visit(source);
|
|
138
|
+
}
|
|
139
|
+
return written;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The functions a module's tasks run: the names its module-level `define` calls pass as their work,
|
|
144
|
+
* and the module's functions those call, which run in the task too.
|
|
145
|
+
*/
|
|
146
|
+
const taskWork = (
|
|
147
|
+
source: ts.SourceFile,
|
|
148
|
+
checker: ts.TypeChecker,
|
|
149
|
+
sdk: string,
|
|
150
|
+
): Set<string> => {
|
|
151
|
+
const functions = new Map(
|
|
152
|
+
source.statements.flatMap((statement) =>
|
|
153
|
+
ts.isFunctionDeclaration(statement) && statement.name
|
|
154
|
+
? [[statement.name.text, statement] as const]
|
|
155
|
+
: [],
|
|
156
|
+
),
|
|
157
|
+
);
|
|
158
|
+
const work = new Set<string>();
|
|
159
|
+
const add = (name: string): void => {
|
|
160
|
+
const declaration = functions.get(name);
|
|
161
|
+
if (!declaration || work.has(name)) {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
work.add(name);
|
|
165
|
+
const visit = (node: ts.Node): void => {
|
|
166
|
+
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
167
|
+
const called = checker
|
|
168
|
+
.getSymbolAtLocation(node.expression)
|
|
169
|
+
?.declarations?.find(ts.isFunctionDeclaration);
|
|
170
|
+
if (called?.name && called.parent === source) {
|
|
171
|
+
add(called.name.text);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
ts.forEachChild(node, visit);
|
|
175
|
+
};
|
|
176
|
+
ts.forEachChild(declaration, visit);
|
|
177
|
+
};
|
|
178
|
+
for (const statement of source.statements) {
|
|
179
|
+
if (!ts.isVariableStatement(statement)) {
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
183
|
+
const call = declaration.initializer;
|
|
184
|
+
const argument =
|
|
185
|
+
call && ts.isCallExpression(call) && definesTask(call, checker, sdk)
|
|
186
|
+
? call.arguments[0]
|
|
187
|
+
: undefined;
|
|
188
|
+
if (argument && ts.isIdentifier(argument)) {
|
|
189
|
+
add(argument.text);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return work;
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Whether the program is plain TypeScript: it declares a class that is not a function block, one
|
|
198
|
+
* extending the SDK's `FunctionBlock` however the module names it.
|
|
199
|
+
*/
|
|
200
|
+
const needsElaboration = (
|
|
201
|
+
sources: readonly ts.SourceFile[],
|
|
202
|
+
kept: (file: string) => boolean,
|
|
203
|
+
checker: ts.TypeChecker,
|
|
204
|
+
sdk: string,
|
|
205
|
+
): boolean =>
|
|
206
|
+
sources.some(
|
|
207
|
+
(source) =>
|
|
208
|
+
!kept(source.fileName) &&
|
|
209
|
+
source.statements.some(
|
|
210
|
+
(statement) =>
|
|
211
|
+
ts.isClassDeclaration(statement) &&
|
|
212
|
+
!statement.heritageClauses?.some((clause) =>
|
|
213
|
+
clause.types.some((type) =>
|
|
214
|
+
namesSdk(checker, sdk, type.expression, "FunctionBlock"),
|
|
215
|
+
),
|
|
216
|
+
),
|
|
217
|
+
),
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
interface Folded {
|
|
221
|
+
path: string;
|
|
222
|
+
value: Constant;
|
|
223
|
+
type?: TypeId;
|
|
224
|
+
span: SourceSpan;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* A folded primitive as a PLC constant, with its scalar type: the IEC width its declaration
|
|
229
|
+
* names, or the one a TypeScript number of that value gets. Strings, often enum members, have none.
|
|
230
|
+
*/
|
|
231
|
+
const foldedConstant = (
|
|
232
|
+
value: CapturedValue,
|
|
233
|
+
iec: string | undefined,
|
|
234
|
+
): Pick<Folded, "value" | "type"> | undefined => {
|
|
235
|
+
switch (value.kind) {
|
|
236
|
+
case "number":
|
|
237
|
+
return Number.isInteger(value.value)
|
|
238
|
+
? {
|
|
239
|
+
value: { kind: "integer", value: String(value.value) },
|
|
240
|
+
type: iec ?? defaultNumericTypes.number,
|
|
241
|
+
}
|
|
242
|
+
: {
|
|
243
|
+
value: { kind: "float", value: value.value },
|
|
244
|
+
type: iec ?? defaultNumericTypes.float,
|
|
245
|
+
};
|
|
246
|
+
case "bigint":
|
|
247
|
+
return {
|
|
248
|
+
value: { kind: "integer", value: value.value },
|
|
249
|
+
type: iec ?? defaultNumericTypes.bigint,
|
|
250
|
+
};
|
|
251
|
+
case "boolean":
|
|
252
|
+
return { value: { kind: "bool", value: value.value }, type: "BOOL" };
|
|
253
|
+
case "string":
|
|
254
|
+
return { value: { kind: "string", value: value.value } };
|
|
255
|
+
default:
|
|
256
|
+
return undefined;
|
|
257
|
+
}
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Compiles a plain TypeScript program: runs its setup, then specializes its scan code against
|
|
262
|
+
* the objects setup built. The result is tsPLC dialect source the rest of the compiler reads.
|
|
263
|
+
*/
|
|
264
|
+
export const lowerElaboration = (
|
|
265
|
+
program: ts.Program,
|
|
266
|
+
sdk: string,
|
|
267
|
+
entries: Readonly<Record<string, { module: string; export: string }>>,
|
|
268
|
+
root: string,
|
|
269
|
+
positions: Positions,
|
|
270
|
+
{
|
|
271
|
+
debug,
|
|
272
|
+
configuration,
|
|
273
|
+
always,
|
|
274
|
+
io,
|
|
275
|
+
}: {
|
|
276
|
+
debug: Pick<CompileDebug, "stackTraces">;
|
|
277
|
+
/** The environment values the program's `env()` calls read, by name, which setup returns. */
|
|
278
|
+
configuration: Readonly<Record<string, string>>;
|
|
279
|
+
/** Whether every program is plain TypeScript, or only one that declares a plain class. */
|
|
280
|
+
always: boolean;
|
|
281
|
+
/** The state paths the config's `io` binds. */
|
|
282
|
+
io: readonly BoundPath[];
|
|
283
|
+
},
|
|
284
|
+
): {
|
|
285
|
+
sources: Map<string, Mapped>;
|
|
286
|
+
diagnostics: Diagnostic[];
|
|
287
|
+
/** Routines specialized from class methods, by `<entry file>:<routine>`. */
|
|
288
|
+
origins: Map<string, { name: string; instance?: string }>;
|
|
289
|
+
folded: Folded[];
|
|
290
|
+
/** Each referable object's storage path by the id a reference holds, and the fields that hold one. */
|
|
291
|
+
references: NonNullable<PlcModule["references"]>;
|
|
292
|
+
/** The string each member of a literal union's enum stands for, by `<types file>:<enum>`. */
|
|
293
|
+
literals: Map<string, Record<string, string>>;
|
|
294
|
+
/** What of each module rewritten here the PLC compiles, where that is not all of it. */
|
|
295
|
+
reduced: Map<string, Uncompiled>;
|
|
296
|
+
} => {
|
|
297
|
+
const sources = new Map<string, Mapped>();
|
|
298
|
+
const diagnostics: Diagnostic[] = [];
|
|
299
|
+
const origins = new Map<string, { name: string; instance?: string }>();
|
|
300
|
+
const folded: Folded[] = [];
|
|
301
|
+
const literals = new Map<string, Record<string, string>>();
|
|
302
|
+
const reduced = new Map<string, Uncompiled>();
|
|
303
|
+
const references: { id: number; path: string }[] = [];
|
|
304
|
+
const referenceFields: string[] = [];
|
|
305
|
+
const result = {
|
|
306
|
+
sources,
|
|
307
|
+
diagnostics,
|
|
308
|
+
origins,
|
|
309
|
+
folded,
|
|
310
|
+
literals,
|
|
311
|
+
reduced,
|
|
312
|
+
references: { objects: references, fields: referenceFields },
|
|
313
|
+
};
|
|
314
|
+
const runtime = plcSources(program).filter(
|
|
315
|
+
(source) => !inSdk(sdk, source.fileName),
|
|
316
|
+
);
|
|
317
|
+
const checker = program.getTypeChecker();
|
|
318
|
+
// The bindings `io` reaches state through, which are never constants.
|
|
319
|
+
const bound = new Set(io.map(({ path }) => path.split(/[.[]/)[0] ?? path));
|
|
320
|
+
const inputs = new Map(
|
|
321
|
+
io.flatMap(({ path, input }) => (input === null ? [] : [[path, input]])),
|
|
322
|
+
);
|
|
323
|
+
const written = writtenBindings(runtime, checker);
|
|
324
|
+
const kept = new Set(
|
|
325
|
+
runtime
|
|
326
|
+
.filter((source) => keptModule(source, checker, sdk, { written, bound }))
|
|
327
|
+
.map((source) => source.fileName),
|
|
328
|
+
);
|
|
329
|
+
const isKept = (file: string) => kept.has(file);
|
|
330
|
+
if (!always && !needsElaboration(runtime, isKept, checker, sdk)) {
|
|
331
|
+
return result;
|
|
332
|
+
}
|
|
333
|
+
const scanFunctions = [
|
|
334
|
+
...new Set(Object.values(entries).map((item) => item.export)),
|
|
335
|
+
];
|
|
336
|
+
const modules = new Set(
|
|
337
|
+
Object.values(entries).map((item) =>
|
|
338
|
+
resolve(root, item.module).replace(/\\/g, "/"),
|
|
339
|
+
),
|
|
340
|
+
);
|
|
341
|
+
const [entryFile, ...otherModules] = modules;
|
|
342
|
+
const [entry] = Object.values(entries);
|
|
343
|
+
if (!entry || !entryFile || otherModules.length) {
|
|
344
|
+
diagnostics.push(
|
|
345
|
+
error(
|
|
346
|
+
"PLC1001",
|
|
347
|
+
"A plain TypeScript program exports all its scan functions from one module.",
|
|
348
|
+
),
|
|
349
|
+
);
|
|
350
|
+
return result;
|
|
351
|
+
}
|
|
352
|
+
const entrySource = program.getSourceFile(entryFile);
|
|
353
|
+
if (!entrySource) {
|
|
354
|
+
diagnostics.push(
|
|
355
|
+
error("PLC1001", `${entry.module} is not part of the program.`),
|
|
356
|
+
);
|
|
357
|
+
return result;
|
|
358
|
+
}
|
|
359
|
+
const directory = join(dirname(entryFile), generatedDirectory).replace(
|
|
360
|
+
/\\/g,
|
|
361
|
+
"/",
|
|
362
|
+
);
|
|
363
|
+
try {
|
|
364
|
+
const refused = stringEnums(runtime, checker);
|
|
365
|
+
if (refused.length) {
|
|
366
|
+
throw new AggregateError(refused);
|
|
367
|
+
}
|
|
368
|
+
const retained = retentionDeclarations(runtime, checker, sdk);
|
|
369
|
+
const graph = captureGraph(entryFile, runtime, checker, configuration);
|
|
370
|
+
// Generic classes specialize per instance, through a checker that knows each one's type arguments.
|
|
371
|
+
const generics = new GenericTypes(checker);
|
|
372
|
+
const model = new Model(graph, generics.checker, program, entryFile, root, {
|
|
373
|
+
bound,
|
|
374
|
+
inputs,
|
|
375
|
+
});
|
|
376
|
+
const generated = generatedModules(model.roots, { directory, root, bound });
|
|
377
|
+
const rootModule = generated.root;
|
|
378
|
+
// A module that also holds state keeps its client and types, which a reply's type may need: the
|
|
379
|
+
// router's types have no layout of their own.
|
|
380
|
+
const named = (source: ts.SourceFile) =>
|
|
381
|
+
isKept(source.fileName) ||
|
|
382
|
+
serviceClients(source, checker, sdk).length > 0;
|
|
383
|
+
const reduction = new Reduction(checker, sdk, {
|
|
384
|
+
runtime,
|
|
385
|
+
kept: isKept,
|
|
386
|
+
entry: entrySource,
|
|
387
|
+
written,
|
|
388
|
+
bound,
|
|
389
|
+
});
|
|
390
|
+
// What the entry module declares for other modules to import, beside its code.
|
|
391
|
+
const declarations = reduction.entry();
|
|
392
|
+
const specializer = new Specializer(
|
|
393
|
+
model,
|
|
394
|
+
generics,
|
|
395
|
+
sdk,
|
|
396
|
+
runtime,
|
|
397
|
+
{
|
|
398
|
+
kept: isKept,
|
|
399
|
+
named,
|
|
400
|
+
enumRefusal: (declaration) =>
|
|
401
|
+
reduction.sharesEnum(declaration)
|
|
402
|
+
? undefined
|
|
403
|
+
: reduction.enumRefusal(declaration),
|
|
404
|
+
},
|
|
405
|
+
rootModule,
|
|
406
|
+
);
|
|
407
|
+
const scans = scanFunctions.map((name) => {
|
|
408
|
+
const declaration = findEntry(entrySource, name);
|
|
409
|
+
return {
|
|
410
|
+
name,
|
|
411
|
+
declaration,
|
|
412
|
+
body: specializedEntry(specializer, declaration),
|
|
413
|
+
};
|
|
414
|
+
});
|
|
415
|
+
const [main] = scans.map((scan) => scan.declaration);
|
|
416
|
+
if (!main) {
|
|
417
|
+
throw new Error("A plain TypeScript program needs a scan function.");
|
|
418
|
+
}
|
|
419
|
+
if (specializer.failures.length) {
|
|
420
|
+
diagnostics.push(
|
|
421
|
+
...specializer.failures.map((cause) =>
|
|
422
|
+
failure(cause, positions, debug),
|
|
423
|
+
),
|
|
424
|
+
);
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// Exported data the hardware, HMI and server address, and any object io binds the hardware
|
|
429
|
+
// to, is stored whether or not code reads it.
|
|
430
|
+
for (const item of model.roots) {
|
|
431
|
+
const addressed =
|
|
432
|
+
(item.exported && item.file === entryFile) || bound.has(item.name);
|
|
433
|
+
if (addressed && !isKept(item.file)) {
|
|
434
|
+
specializer.storage.use(item);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
for (const name of specializer.initializers.takeSdkCalls()) {
|
|
439
|
+
specializer.imports.addSdk(sdkModules.types, name);
|
|
440
|
+
}
|
|
441
|
+
const retention = new Retention(model, specializer.storage, retained);
|
|
442
|
+
const stored = [...specializer.storage.roots].map((item) => ({
|
|
443
|
+
item,
|
|
444
|
+
...storageGlobals(specializer, retention, item),
|
|
445
|
+
}));
|
|
446
|
+
const unretained = retention.unclaimed();
|
|
447
|
+
if (unretained) {
|
|
448
|
+
throw unretained;
|
|
449
|
+
}
|
|
450
|
+
// Writing the globals declared the types they use.
|
|
451
|
+
const types = specializer.layouts.snapshot();
|
|
452
|
+
// A storage module imports the generated types, so none of its variables can have a type's name.
|
|
453
|
+
const misnamed = stored.flatMap(({ item, fields }) =>
|
|
454
|
+
fields.flatMap(({ name, declaration }) =>
|
|
455
|
+
types.declarations.has(name)
|
|
456
|
+
? [
|
|
457
|
+
new SpecializeError(
|
|
458
|
+
declaration ?? item.declaration,
|
|
459
|
+
`${item.name}.${name}: each field of ${item.name} becomes a PLC variable of the field's name, and the generated code already names a type ${name}. Rename the field.`,
|
|
460
|
+
),
|
|
461
|
+
]
|
|
462
|
+
: [],
|
|
463
|
+
),
|
|
464
|
+
);
|
|
465
|
+
if (misnamed.length) {
|
|
466
|
+
throw new AggregateError(misnamed);
|
|
467
|
+
}
|
|
468
|
+
const typesFile = generated.types;
|
|
469
|
+
const typeImports = (file: string) => [
|
|
470
|
+
...specializer.layouts.imports.lines(file, "import"),
|
|
471
|
+
...specializer.layouts.typeImports.lines(file, "import type"),
|
|
472
|
+
];
|
|
473
|
+
const importTypes = (file: string) =>
|
|
474
|
+
typesModuleImports(types, importPath(file, typesFile));
|
|
475
|
+
sources.set(
|
|
476
|
+
typesFile,
|
|
477
|
+
joined([...typeImports(typesFile), ...types.declarations.values()], "\n"),
|
|
478
|
+
);
|
|
479
|
+
for (const { item, lines } of stored) {
|
|
480
|
+
const file = rootModule(item);
|
|
481
|
+
const imports = [...typeImports(file), ...importTypes(file)];
|
|
482
|
+
sources.set(
|
|
483
|
+
file,
|
|
484
|
+
joined(
|
|
485
|
+
[...imports.map((line) => from(item.declaration, line)), ...lines],
|
|
486
|
+
"\n",
|
|
487
|
+
),
|
|
488
|
+
);
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
const header = [
|
|
492
|
+
...typeImports(entryFile),
|
|
493
|
+
...importTypes(entryFile),
|
|
494
|
+
...specializer.imports.lines(entryFile),
|
|
495
|
+
];
|
|
496
|
+
// An enum the generated code stores is declared before the code that reads it. What the entry
|
|
497
|
+
// declares for other modules only comes last, where it moves nothing the program compiled to,
|
|
498
|
+
// and is left out where the generated code already has the name for something of its own.
|
|
499
|
+
const storesEnum = (name: string) =>
|
|
500
|
+
specializer.layouts.imports.has(entryFile, name);
|
|
501
|
+
const free = ({ name }: { name: string }) =>
|
|
502
|
+
!storesEnum(name) &&
|
|
503
|
+
!specializer.named(name) &&
|
|
504
|
+
!types.declarations.has(name);
|
|
505
|
+
const declared = [
|
|
506
|
+
...declarations.enums.filter(free),
|
|
507
|
+
...declarations.constants.filter(free),
|
|
508
|
+
];
|
|
509
|
+
reduction.declaredByEntry(
|
|
510
|
+
new Set(
|
|
511
|
+
[
|
|
512
|
+
...declarations.enums.filter(({ name }) => storesEnum(name)),
|
|
513
|
+
...declared,
|
|
514
|
+
].map(({ name }) => name),
|
|
515
|
+
),
|
|
516
|
+
);
|
|
517
|
+
sources.set(
|
|
518
|
+
entryFile,
|
|
519
|
+
joined(
|
|
520
|
+
[
|
|
521
|
+
...header.map((line) => from(main, line)),
|
|
522
|
+
...declarations.enums
|
|
523
|
+
.filter(({ name }) => storesEnum(name))
|
|
524
|
+
.map(({ line }) => line),
|
|
525
|
+
...specializer.expressions.tableDeclarations,
|
|
526
|
+
...specializer.routines,
|
|
527
|
+
...scans.flatMap(({ name, declaration, body }) => [
|
|
528
|
+
from(declaration, `export function ${name}(): void {`),
|
|
529
|
+
...body,
|
|
530
|
+
from(closingBrace(declaration), "}"),
|
|
531
|
+
]),
|
|
532
|
+
...declared.map(({ line }) => line),
|
|
533
|
+
],
|
|
534
|
+
"\n",
|
|
535
|
+
),
|
|
536
|
+
);
|
|
537
|
+
for (const [name, origin] of specializer.origins) {
|
|
538
|
+
origins.set(`${entryFile}:${name}`, origin);
|
|
539
|
+
}
|
|
540
|
+
for (const { name, members } of specializer.layouts.literalEnums) {
|
|
541
|
+
literals.set(`${typesFile}:${name}`, members);
|
|
542
|
+
}
|
|
543
|
+
const table = specializer.storage.referenceTable();
|
|
544
|
+
references.push(...table.references);
|
|
545
|
+
referenceFields.push(...table.referenceFields);
|
|
546
|
+
for (const item of specializer.storage.folded()) {
|
|
547
|
+
const known = foldedConstant(item.value, item.iec);
|
|
548
|
+
if (known) {
|
|
549
|
+
folded.push({
|
|
550
|
+
path: item.path,
|
|
551
|
+
...known,
|
|
552
|
+
span: positions.node(item.declaration),
|
|
553
|
+
});
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
for (const source of reduction.modules) {
|
|
557
|
+
sources.set(source.fileName, reduction.module(source));
|
|
558
|
+
reduced.set(source.fileName, { compiled: reduction.compiled(source) });
|
|
559
|
+
}
|
|
560
|
+
reduced.set(entryFile, {
|
|
561
|
+
declared: new Set(
|
|
562
|
+
[...declarations.enums, ...declarations.constants].map(
|
|
563
|
+
({ name }) => name,
|
|
564
|
+
),
|
|
565
|
+
),
|
|
566
|
+
});
|
|
567
|
+
} catch (cause) {
|
|
568
|
+
const causes = cause instanceof AggregateError ? cause.errors : [cause];
|
|
569
|
+
diagnostics.push(
|
|
570
|
+
...causes.map((reported) => failure(reported, positions, debug)),
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
return result;
|
|
574
|
+
};
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* The entry's specialized body. When specializing fails, the failures statements recorded before
|
|
578
|
+
* escape with the cause: often they are its cause, as a skipped declaration's name is then unknown.
|
|
579
|
+
*/
|
|
580
|
+
const specializedEntry = (
|
|
581
|
+
specializer: Specializer,
|
|
582
|
+
main: ts.FunctionDeclaration | ts.ArrowFunction,
|
|
583
|
+
): Mapped[] => {
|
|
584
|
+
try {
|
|
585
|
+
return specializer.entry(main);
|
|
586
|
+
} catch (cause) {
|
|
587
|
+
throw new AggregateError([...specializer.failures, cause]);
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
/** The imports of what the types module declares, from a module that reaches it at `path`. */
|
|
592
|
+
const typesModuleImports = (
|
|
593
|
+
types: TypeDeclarations,
|
|
594
|
+
path: string,
|
|
595
|
+
): string[] => {
|
|
596
|
+
const interfaces = [...types.declarations.keys()].filter(
|
|
597
|
+
(name) => !types.enums.includes(name),
|
|
598
|
+
);
|
|
599
|
+
return [
|
|
600
|
+
...(types.enums.length
|
|
601
|
+
? [`import { ${types.enums.join(", ")} } from "${path}";`]
|
|
602
|
+
: []),
|
|
603
|
+
...(interfaces.length
|
|
604
|
+
? [`import type { ${interfaces.join(", ")} } from "${path}";`]
|
|
605
|
+
: []),
|
|
606
|
+
];
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
/**
|
|
610
|
+
* The globals of a root's storage module: one per stored field of a record or instance, or the
|
|
611
|
+
* root itself. The entry module assigns them through its namespace import of this module.
|
|
612
|
+
* `fields` are the fields that became globals, each of which must be able to name a variable.
|
|
613
|
+
*/
|
|
614
|
+
const storageGlobals = (
|
|
615
|
+
specializer: Specializer,
|
|
616
|
+
retention: Retention,
|
|
617
|
+
item: Root,
|
|
618
|
+
): { lines: Mapped[]; fields: RootField[] } => {
|
|
619
|
+
const { model, checker, layouts, storage, initializers } = specializer;
|
|
620
|
+
const lines: Mapped[] = [];
|
|
621
|
+
/** The SDK calls the globals' initializers are wrapped in, `retain` or `persist`. */
|
|
622
|
+
const kept = new Set<string>();
|
|
623
|
+
const global = ({
|
|
624
|
+
name,
|
|
625
|
+
layout,
|
|
626
|
+
initial,
|
|
627
|
+
declaration,
|
|
628
|
+
}: {
|
|
629
|
+
name: string;
|
|
630
|
+
layout: string;
|
|
631
|
+
initial: string;
|
|
632
|
+
declaration: ts.Node | undefined;
|
|
633
|
+
}) => {
|
|
634
|
+
const origin = declaration ?? item.declaration;
|
|
635
|
+
const keeps = retention.global(item, storage.flattened(item) ? name : null);
|
|
636
|
+
if (keeps !== "none") {
|
|
637
|
+
kept.add(keeps);
|
|
638
|
+
}
|
|
639
|
+
const value = keeps === "none" ? initial : `${keeps}(${initial})`;
|
|
640
|
+
lines.push(from(origin, `export let ${name}: ${layout} = ${value};`));
|
|
641
|
+
};
|
|
642
|
+
const node = model.node(item.value);
|
|
643
|
+
const sdkImports = (): Mapped[] => {
|
|
644
|
+
const calls = [...[...kept].sort(), ...initializers.takeSdkCalls()];
|
|
645
|
+
return calls.length
|
|
646
|
+
? [
|
|
647
|
+
from(
|
|
648
|
+
item.declaration,
|
|
649
|
+
`import { ${calls.join(", ")} } from "${sdkModules.types}";`,
|
|
650
|
+
),
|
|
651
|
+
]
|
|
652
|
+
: [];
|
|
653
|
+
};
|
|
654
|
+
if (!node || !storage.flattened(item)) {
|
|
655
|
+
const declared = checker.getTypeAtLocation(item.declaration.name);
|
|
656
|
+
const layout = layouts.declaredText(item.declaration, declared, item.value);
|
|
657
|
+
global({
|
|
658
|
+
name: item.name,
|
|
659
|
+
layout,
|
|
660
|
+
initial: initializers.valueText(item.value, declared, layout),
|
|
661
|
+
declaration: item.declaration,
|
|
662
|
+
});
|
|
663
|
+
return { lines: [...sdkImports(), ...lines], fields: [] };
|
|
664
|
+
}
|
|
665
|
+
const fieldInitializer = (name: string, layout: string): string => {
|
|
666
|
+
if (node.captured.kind === "instance") {
|
|
667
|
+
return initializers.instanceFieldInitializer(node, name, layout);
|
|
668
|
+
}
|
|
669
|
+
const declared = model.capturedFieldType(node, name);
|
|
670
|
+
if (!declared) {
|
|
671
|
+
throw new Error(`${item.name}.${name} has no declared type.`);
|
|
672
|
+
}
|
|
673
|
+
return initializers.valueText(model.field(node, name), declared, layout);
|
|
674
|
+
};
|
|
675
|
+
const fields = storage.rootFields(item);
|
|
676
|
+
for (const field of fields) {
|
|
677
|
+
const origin = field.declaration ?? item.declaration;
|
|
678
|
+
if (!plainName(field.name)) {
|
|
679
|
+
throw quotedField(origin, field.name);
|
|
680
|
+
}
|
|
681
|
+
if (reservedWords.has(field.name)) {
|
|
682
|
+
throw new SpecializeError(
|
|
683
|
+
origin,
|
|
684
|
+
`${item.name}.${field.name}: each field of ${item.name} becomes a PLC variable of the field's name, and "${field.name}" is a word TypeScript reserves, so no variable can have it. Rename the field.`,
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
global({
|
|
688
|
+
...field,
|
|
689
|
+
initial: fieldInitializer(field.name, field.layout),
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
return { lines: [...sdkImports(), ...lines], fields };
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
/**
|
|
696
|
+
* The words no variable of a module can be named, exactly: every other word TypeScript knows,
|
|
697
|
+
* such as `out`, `type` and `of`, names a variable as any name does.
|
|
698
|
+
*/
|
|
699
|
+
const reservedWords: ReadonlySet<string> = new Set([
|
|
700
|
+
"arguments",
|
|
701
|
+
"await",
|
|
702
|
+
"break",
|
|
703
|
+
"case",
|
|
704
|
+
"catch",
|
|
705
|
+
"class",
|
|
706
|
+
"const",
|
|
707
|
+
"continue",
|
|
708
|
+
"debugger",
|
|
709
|
+
"default",
|
|
710
|
+
"delete",
|
|
711
|
+
"do",
|
|
712
|
+
"else",
|
|
713
|
+
"enum",
|
|
714
|
+
"eval",
|
|
715
|
+
"export",
|
|
716
|
+
"extends",
|
|
717
|
+
"false",
|
|
718
|
+
"finally",
|
|
719
|
+
"for",
|
|
720
|
+
"function",
|
|
721
|
+
"if",
|
|
722
|
+
"implements",
|
|
723
|
+
"import",
|
|
724
|
+
"in",
|
|
725
|
+
"instanceof",
|
|
726
|
+
"interface",
|
|
727
|
+
"let",
|
|
728
|
+
"new",
|
|
729
|
+
"null",
|
|
730
|
+
"package",
|
|
731
|
+
"private",
|
|
732
|
+
"protected",
|
|
733
|
+
"public",
|
|
734
|
+
"return",
|
|
735
|
+
"static",
|
|
736
|
+
"super",
|
|
737
|
+
"switch",
|
|
738
|
+
"this",
|
|
739
|
+
"throw",
|
|
740
|
+
"true",
|
|
741
|
+
"try",
|
|
742
|
+
"typeof",
|
|
743
|
+
"var",
|
|
744
|
+
"void",
|
|
745
|
+
"while",
|
|
746
|
+
"with",
|
|
747
|
+
"yield",
|
|
748
|
+
]);
|
|
749
|
+
|
|
750
|
+
/** An elaboration failure as a diagnostic, at the node specialization stopped at when it knows one. */
|
|
751
|
+
const failure = (
|
|
752
|
+
cause: unknown,
|
|
753
|
+
positions: Positions,
|
|
754
|
+
debug: Pick<CompileDebug, "stackTraces">,
|
|
755
|
+
): Diagnostic => {
|
|
756
|
+
if (cause instanceof SpecializeError) {
|
|
757
|
+
return error("PLC1010", cause.message, positions.node(cause.node));
|
|
758
|
+
}
|
|
759
|
+
if (!(cause instanceof Error)) {
|
|
760
|
+
return error("PLC1010", String(cause));
|
|
761
|
+
}
|
|
762
|
+
return error(
|
|
763
|
+
"PLC1010",
|
|
764
|
+
debug.stackTraces ? String(cause.stack) : cause.message,
|
|
765
|
+
);
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
const findEntry = (
|
|
769
|
+
source: ts.SourceFile,
|
|
770
|
+
name: string,
|
|
771
|
+
): ts.FunctionDeclaration | ts.ArrowFunction => {
|
|
772
|
+
for (const statement of source.statements) {
|
|
773
|
+
if (
|
|
774
|
+
ts.isFunctionDeclaration(statement) &&
|
|
775
|
+
statement.name?.text === name &&
|
|
776
|
+
statement.body
|
|
777
|
+
) {
|
|
778
|
+
return statement;
|
|
779
|
+
}
|
|
780
|
+
if (ts.isVariableStatement(statement)) {
|
|
781
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
782
|
+
if (
|
|
783
|
+
ts.isIdentifier(declaration.name) &&
|
|
784
|
+
declaration.name.text === name &&
|
|
785
|
+
declaration.initializer &&
|
|
786
|
+
ts.isArrowFunction(declaration.initializer)
|
|
787
|
+
) {
|
|
788
|
+
return declaration.initializer;
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
throw new Error(`${source.fileName} exports no function ${name}.`);
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
/**
|
|
797
|
+
* The generated modules: each root's storage module, and the module of generated types. A root's
|
|
798
|
+
* module has the root's name, which io and the HMI reach its storage by, so the types module is
|
|
799
|
+
* the one that gives way to a root named like it.
|
|
800
|
+
*
|
|
801
|
+
* Roots in different modules can share a name, up to case, and would then share one module and its
|
|
802
|
+
* list; each of them is qualified by its own module instead. The config's `io` names a root by its
|
|
803
|
+
* binding name, which then names none of them, so that is refused.
|
|
804
|
+
*/
|
|
805
|
+
const generatedModules = (
|
|
806
|
+
roots: readonly Root[],
|
|
807
|
+
{
|
|
808
|
+
directory,
|
|
809
|
+
root: projectRoot,
|
|
810
|
+
bound,
|
|
811
|
+
}: { directory: string; root: string; bound: ReadonlySet<string> },
|
|
812
|
+
): { root: (item: Root) => string; types: string } => {
|
|
813
|
+
const shareName = (item: Root) =>
|
|
814
|
+
roots.filter(
|
|
815
|
+
(other) => other.name.toLowerCase() === item.name.toLowerCase(),
|
|
816
|
+
);
|
|
817
|
+
const ambiguous = roots.filter(
|
|
818
|
+
(item) => bound.has(item.name) && shareName(item).length > 1,
|
|
819
|
+
);
|
|
820
|
+
if (ambiguous.length) {
|
|
821
|
+
throw new AggregateError(
|
|
822
|
+
ambiguous.map((item) => {
|
|
823
|
+
const modules = shareName(item).map((other) =>
|
|
824
|
+
modulePath(projectRoot, other.file),
|
|
825
|
+
);
|
|
826
|
+
return new SpecializeError(
|
|
827
|
+
item.declaration,
|
|
828
|
+
`The config's io names this root ${item.name}, which roots in ${modules.join(" and ")} share: rename one of them.`,
|
|
829
|
+
);
|
|
830
|
+
}),
|
|
831
|
+
);
|
|
832
|
+
}
|
|
833
|
+
const qualified = (item: Root): string => {
|
|
834
|
+
const module = modulePath(projectRoot, item.file);
|
|
835
|
+
const sharing = shareName(item);
|
|
836
|
+
if (sharing.length === 1) {
|
|
837
|
+
return item.name;
|
|
838
|
+
}
|
|
839
|
+
// Two roots of one module differ only in their capitals, which a file's name can't tell apart.
|
|
840
|
+
// The last of them keeps the name the module alone gives, as it always had; the others add their own.
|
|
841
|
+
const twins = sharing.filter((other) => other.file === item.file);
|
|
842
|
+
return twins.at(-1) === item
|
|
843
|
+
? `${item.name}_${identityDigest(module)}`
|
|
844
|
+
: `${item.name}_${identityDigest(`${module}\0${item.name}`)}`;
|
|
845
|
+
};
|
|
846
|
+
const taken = new Set(roots.map((item) => qualified(item).toLowerCase()));
|
|
847
|
+
return {
|
|
848
|
+
root: (item) => `${directory}/${qualified(item)}.ts`,
|
|
849
|
+
types: `${directory}/${unusedName("types", (name) => taken.has(name))}.ts`,
|
|
850
|
+
};
|
|
851
|
+
};
|