@tsplc/compiler 0.5.13 → 0.5.15
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 +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -1,12 +1,62 @@
|
|
|
1
|
-
import { primitiveTypes } from "@tsplc/compiler/ir";
|
|
1
|
+
import { optionalRecord, pascalCase, primitiveTypes } from "@tsplc/compiler/ir";
|
|
2
2
|
import ts from "typescript";
|
|
3
|
+
import { from, type Mapped, mapped, type Part } from "../positions";
|
|
4
|
+
import {
|
|
5
|
+
declaringName,
|
|
6
|
+
defaultNumericTypes,
|
|
7
|
+
hasModifier,
|
|
8
|
+
promisedType,
|
|
9
|
+
} from "../types";
|
|
3
10
|
import type { CapturedValue } from "./capture-protocol";
|
|
4
11
|
import { ModuleImports } from "./imports";
|
|
5
|
-
import type
|
|
12
|
+
import { isExported, type Model, type ModelNode, unionMembers } from "./model";
|
|
13
|
+
import { SpecializeError } from "./values";
|
|
6
14
|
|
|
7
15
|
/** IEC names a program can write as types; they keep their width rather than becoming DINT. */
|
|
8
16
|
const iecNames = new Set(primitiveTypes.map((type) => type.id));
|
|
9
17
|
|
|
18
|
+
/** What an instance that stores nothing is kept as where a record or array holds it. */
|
|
19
|
+
export const statelessLayout = "boolean";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A stored array's PLC type, `FixedArray<T, n>`, with how many elements it has room for and T when
|
|
23
|
+
* T is an IEC name. A kept module's named array type is only its name.
|
|
24
|
+
*/
|
|
25
|
+
export interface ArrayLayout {
|
|
26
|
+
text: string;
|
|
27
|
+
bound?: number;
|
|
28
|
+
elementIec?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** An optional record holding the defined value `text`. */
|
|
32
|
+
export const definedOptional = (text: Part): Mapped =>
|
|
33
|
+
mapped`{ ${optionalRecord.has}: true, ${optionalRecord.value}: ${text} }`;
|
|
34
|
+
|
|
35
|
+
/** An optional record holding nothing, its value the empty `text`. */
|
|
36
|
+
export const emptyOptional = (text: Part): Mapped =>
|
|
37
|
+
mapped`{ ${optionalRecord.has}: false, ${optionalRecord.value}: ${text} }`;
|
|
38
|
+
|
|
39
|
+
/** Whether the optional record `text` holds a value. */
|
|
40
|
+
export const optionalHas = (text: Part): Mapped =>
|
|
41
|
+
mapped`${text}.${optionalRecord.has}`;
|
|
42
|
+
|
|
43
|
+
/** The value the optional record `text` holds. */
|
|
44
|
+
export const optionalValue = (text: Part): Mapped =>
|
|
45
|
+
mapped`${text}.${optionalRecord.value}`;
|
|
46
|
+
|
|
47
|
+
/** The layout of an array of `length` elements: the PLC has no empty array, so it keeps at least one slot. */
|
|
48
|
+
export const arrayLayout = (
|
|
49
|
+
element: string,
|
|
50
|
+
length: number,
|
|
51
|
+
): ArrayLayout & { bound: number } => {
|
|
52
|
+
const bound = Math.max(length, 1);
|
|
53
|
+
return {
|
|
54
|
+
text: `FixedArray<${element}, ${bound}>`,
|
|
55
|
+
bound,
|
|
56
|
+
...(iecNames.has(element) && { elementIec: element }),
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
|
|
10
60
|
/** A union of string literals: a PLC enum. */
|
|
11
61
|
interface LiteralEnum {
|
|
12
62
|
name: string;
|
|
@@ -25,13 +75,26 @@ interface TaggedUnion {
|
|
|
25
75
|
optional: boolean;
|
|
26
76
|
}
|
|
27
77
|
|
|
78
|
+
/** What the generated types module declares: each declaration's text by name, and which of them are enums. */
|
|
79
|
+
export interface TypeDeclarations {
|
|
80
|
+
declarations: ReadonlyMap<string, Part>;
|
|
81
|
+
enums: readonly string[];
|
|
82
|
+
}
|
|
83
|
+
|
|
28
84
|
/**
|
|
29
85
|
* Turns the program's TypeScript types into the PLC dialect's: string-literal unions become enums,
|
|
30
86
|
* unions of objects become one record tagged by their literal field, `T | undefined` becomes an
|
|
31
|
-
* optional record, and arrays take their captured length.
|
|
87
|
+
* optional record, and arrays take their captured length.
|
|
32
88
|
*/
|
|
33
89
|
export class Layouts {
|
|
34
|
-
readonly declarations = new Map<string, string>();
|
|
90
|
+
private readonly declarations = new Map<string, string>();
|
|
91
|
+
/** The program's declaration each generated one stands for, where diagnostics about it point. */
|
|
92
|
+
private readonly origins = new Map<string, ts.Node>();
|
|
93
|
+
/**
|
|
94
|
+
* Names a generated declaration mustn't take: what kept modules declare, which generated modules
|
|
95
|
+
* may import beside the generated ones, and the namespaces storage modules are imported as.
|
|
96
|
+
*/
|
|
97
|
+
private readonly takenNames: Set<string>;
|
|
35
98
|
/** Enums the declarations need from kept modules. */
|
|
36
99
|
readonly imports = new ModuleImports();
|
|
37
100
|
/** Types the declarations need from kept modules. */
|
|
@@ -40,6 +103,9 @@ export class Layouts {
|
|
|
40
103
|
private readonly records = new Map<string, string>();
|
|
41
104
|
private readonly names = new Map<string, number>();
|
|
42
105
|
|
|
106
|
+
/** The type literals kept modules write, which name no type a generated module could import. */
|
|
107
|
+
private readonly keptLiterals: ts.TypeLiteralNode[] = [];
|
|
108
|
+
|
|
43
109
|
/** Exported types of kept modules by the type they declare, for types that lost their alias name. */
|
|
44
110
|
private readonly keptTypes = new Map<
|
|
45
111
|
ts.Type,
|
|
@@ -51,10 +117,41 @@ export class Layouts {
|
|
|
51
117
|
private readonly checker: ts.TypeChecker,
|
|
52
118
|
/** Modules the compiled program keeps, whose named types and enums it imports rather than copies. */
|
|
53
119
|
private readonly kept: (file: string) => boolean,
|
|
54
|
-
keptSources: readonly ts.SourceFile[]
|
|
120
|
+
keptSources: readonly ts.SourceFile[],
|
|
121
|
+
/** The stored layout of a class instance or an array of them, which storage, built on these layouts, gives. */
|
|
122
|
+
private readonly instanceState: (node: ModelNode) => string | undefined,
|
|
55
123
|
) {
|
|
124
|
+
// Storage imports a root's module under its capitalized name, unless the program names a type so.
|
|
125
|
+
const typeNames = new Set(
|
|
126
|
+
model.program
|
|
127
|
+
.getSourceFiles()
|
|
128
|
+
.filter((source) => !source.isDeclarationFile)
|
|
129
|
+
.flatMap((source) =>
|
|
130
|
+
source.statements.flatMap((statement) =>
|
|
131
|
+
ts.isVariableStatement(statement) ||
|
|
132
|
+
ts.isFunctionDeclaration(statement)
|
|
133
|
+
? []
|
|
134
|
+
: declaredNames(statement),
|
|
135
|
+
),
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
this.takenNames = new Set(
|
|
139
|
+
model.roots
|
|
140
|
+
.map((root) => pascalCase(root.name))
|
|
141
|
+
.filter((name) => !typeNames.has(name)),
|
|
142
|
+
);
|
|
143
|
+
const collectLiterals = (node: ts.Node): void => {
|
|
144
|
+
if (ts.isTypeLiteralNode(node)) {
|
|
145
|
+
this.keptLiterals.push(node);
|
|
146
|
+
}
|
|
147
|
+
ts.forEachChild(node, collectLiterals);
|
|
148
|
+
};
|
|
56
149
|
for (const source of keptSources) {
|
|
150
|
+
collectLiterals(source);
|
|
57
151
|
for (const statement of source.statements) {
|
|
152
|
+
for (const name of declaredNames(statement)) {
|
|
153
|
+
this.takenNames.add(name);
|
|
154
|
+
}
|
|
58
155
|
if (
|
|
59
156
|
(ts.isTypeAliasDeclaration(statement) ||
|
|
60
157
|
ts.isInterfaceDeclaration(statement)) &&
|
|
@@ -72,12 +169,42 @@ export class Layouts {
|
|
|
72
169
|
}
|
|
73
170
|
}
|
|
74
171
|
|
|
75
|
-
/** A declaration name no
|
|
172
|
+
/** A declaration name no other declaration has, nor any of `takenNames`. */
|
|
76
173
|
private unique(base: string): string {
|
|
77
174
|
const count = this.names.get(base) ?? 0;
|
|
78
175
|
this.names.set(base, count + 1);
|
|
79
176
|
const name = count ? `${base}_${count + 1}` : base;
|
|
80
|
-
return this.declarations.has(name)
|
|
177
|
+
return this.declarations.has(name) || this.takenNames.has(name)
|
|
178
|
+
? this.unique(base)
|
|
179
|
+
: name;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Declares an interface of `body` under the first free name from `base`, standing for the
|
|
184
|
+
* program's `origin` when it has one, and returns the name.
|
|
185
|
+
*/
|
|
186
|
+
declare(base: string, body: string, origin: ts.Node | undefined): string {
|
|
187
|
+
const name = this.unique(base);
|
|
188
|
+
this.declarations.set(name, `export interface ${name} { ${body} }`);
|
|
189
|
+
if (origin) {
|
|
190
|
+
this.origins.set(name, origin);
|
|
191
|
+
}
|
|
192
|
+
return name;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** What is declared so far. Generated modules import from the types module what it declares once every layout is made. */
|
|
196
|
+
snapshot(): TypeDeclarations {
|
|
197
|
+
return {
|
|
198
|
+
declarations: new Map(
|
|
199
|
+
[...this.declarations].map(([name, text]) => {
|
|
200
|
+
const origin = this.origins.get(name);
|
|
201
|
+
return [name, origin ? from(origin, text) : text];
|
|
202
|
+
}),
|
|
203
|
+
),
|
|
204
|
+
enums: [...this.declarations]
|
|
205
|
+
.filter(([, text]) => text.startsWith("export enum"))
|
|
206
|
+
.map(([name]) => name),
|
|
207
|
+
};
|
|
81
208
|
}
|
|
82
209
|
|
|
83
210
|
/** Each string-literal union's enum, with the string each member stands for. */
|
|
@@ -85,23 +212,16 @@ export class Layouts {
|
|
|
85
212
|
return [...this.enums.values()].map((declared) => ({
|
|
86
213
|
name: declared.name,
|
|
87
214
|
members: Object.fromEntries(
|
|
88
|
-
declared.members.map((member) => [
|
|
215
|
+
declared.members.map((member) => [literalMember(member), member]),
|
|
89
216
|
),
|
|
90
217
|
}));
|
|
91
218
|
}
|
|
92
219
|
|
|
93
|
-
/** Generated enums, which modules import as values. */
|
|
94
|
-
get enumNames(): string[] {
|
|
95
|
-
return [...this.declarations]
|
|
96
|
-
.filter(([, text]) => text.startsWith("export enum"))
|
|
97
|
-
.map(([name]) => name);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
220
|
/** The string literals of a union of them, without undefined. */
|
|
101
221
|
literals(
|
|
102
222
|
type: ts.Type,
|
|
103
223
|
): { members: string[]; optional: boolean } | undefined {
|
|
104
|
-
const parts = type
|
|
224
|
+
const parts = unionMembers(type);
|
|
105
225
|
const defined = parts.filter((part) => !isAbsent(part));
|
|
106
226
|
const members = defined.flatMap((part) =>
|
|
107
227
|
part.isStringLiteral() ? [part.value] : [],
|
|
@@ -118,21 +238,26 @@ export class Layouts {
|
|
|
118
238
|
if (!literals) {
|
|
119
239
|
return undefined;
|
|
120
240
|
}
|
|
121
|
-
const key =
|
|
241
|
+
const key = enumKey(literals.members);
|
|
122
242
|
const existing = this.enums.get(key);
|
|
123
243
|
if (existing) {
|
|
124
244
|
existing.optional ||= literals.optional;
|
|
125
245
|
this.writeEnum(existing);
|
|
126
246
|
return existing;
|
|
127
247
|
}
|
|
128
|
-
const alias =
|
|
129
|
-
type.aliasSymbol ?? this.checker.getNonNullableType(type).aliasSymbol
|
|
130
|
-
)?.name;
|
|
248
|
+
const alias =
|
|
249
|
+
type.aliasSymbol ?? this.checker.getNonNullableType(type).aliasSymbol;
|
|
131
250
|
const declared: LiteralEnum = {
|
|
132
|
-
name: this.unique(
|
|
251
|
+
name: this.unique(
|
|
252
|
+
alias?.name ?? pascalCase(literals.members.join("_or_")),
|
|
253
|
+
),
|
|
133
254
|
members: literals.members,
|
|
134
255
|
optional: literals.optional,
|
|
135
256
|
};
|
|
257
|
+
const origin = alias?.declarations?.[0];
|
|
258
|
+
if (origin) {
|
|
259
|
+
this.origins.set(declared.name, origin);
|
|
260
|
+
}
|
|
136
261
|
this.enums.set(key, declared);
|
|
137
262
|
this.writeEnum(declared);
|
|
138
263
|
return declared;
|
|
@@ -142,7 +267,7 @@ export class Layouts {
|
|
|
142
267
|
const members = [
|
|
143
268
|
...(declared.optional ? ["Undefined = 0"] : []),
|
|
144
269
|
...declared.members.map(
|
|
145
|
-
(member, index) => `${
|
|
270
|
+
(member, index) => `${literalMember(member)} = ${index + 1}`,
|
|
146
271
|
),
|
|
147
272
|
];
|
|
148
273
|
this.declarations.set(
|
|
@@ -153,7 +278,7 @@ export class Layouts {
|
|
|
153
278
|
|
|
154
279
|
/** `Enum.Member` text for a string literal of a literal union. */
|
|
155
280
|
member(declared: LiteralEnum, value: string | undefined): string {
|
|
156
|
-
return `${declared.name}.${value === undefined ? "Undefined" :
|
|
281
|
+
return `${declared.name}.${value === undefined ? "Undefined" : literalMember(value)}`;
|
|
157
282
|
}
|
|
158
283
|
|
|
159
284
|
/** The object types of a union of them, without undefined, when it has a literal tag field. */
|
|
@@ -208,10 +333,17 @@ export class Layouts {
|
|
|
208
333
|
tagged: TaggedUnion,
|
|
209
334
|
name: string,
|
|
210
335
|
): ts.Type {
|
|
336
|
+
return this.checker.getTypeOfSymbol(
|
|
337
|
+
this.taggedField(declared, tagged, name),
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** A field of a tagged union, from the first variant that has it, whose declaration names its IEC width. */
|
|
342
|
+
taggedField(declared: ts.Type, tagged: TaggedUnion, name: string): ts.Symbol {
|
|
211
343
|
for (const variant of tagged.variants) {
|
|
212
344
|
const property = variant.getProperty(name);
|
|
213
345
|
if (property) {
|
|
214
|
-
return
|
|
346
|
+
return property;
|
|
215
347
|
}
|
|
216
348
|
}
|
|
217
349
|
throw new Error(
|
|
@@ -226,27 +358,38 @@ export class Layouts {
|
|
|
226
358
|
return property ? this.checker.getTypeOfSymbol(property) : declared;
|
|
227
359
|
}
|
|
228
360
|
|
|
229
|
-
/** The
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
361
|
+
/** The enum of a tagged union's tag. */
|
|
362
|
+
tagEnum(declared: ts.Type, tagged: TaggedUnion): LiteralEnum {
|
|
363
|
+
const tag = this.literalEnum(this.tagType(declared, tagged));
|
|
364
|
+
if (!tag) {
|
|
365
|
+
throw new Error(
|
|
366
|
+
`${tagged.tag} of ${this.checker.typeToString(declared)} is no string literal.`,
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
return tag;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** `Enum.Member` text for the member of a TypeScript enum type that has `value`. */
|
|
373
|
+
enumMember(type: ts.Type, value: number): string | undefined {
|
|
374
|
+
const member = this.enumDeclaration(type)?.members.find(
|
|
375
|
+
(candidate) => this.checker.getConstantValue(candidate) === value,
|
|
376
|
+
);
|
|
377
|
+
return member && `${this.text(type)}.${member.name.getText()}`;
|
|
236
378
|
}
|
|
237
379
|
|
|
238
380
|
/** Whether `T | undefined` is stored as `{ has, value }`; a literal union has an Undefined member instead. */
|
|
239
381
|
isOptionalRecord(type: ts.Type): boolean {
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
382
|
+
return (
|
|
383
|
+
type.isUnion() &&
|
|
384
|
+
type.types.some((part) => isAbsent(part)) &&
|
|
385
|
+
!this.literals(type)
|
|
386
|
+
);
|
|
245
387
|
}
|
|
246
388
|
|
|
247
389
|
/** The dialect type text for `type`, holding `value` when it was captured. */
|
|
248
390
|
text(type: ts.Type, value?: CapturedValue, hint?: string): string {
|
|
249
391
|
const checker = this.checker;
|
|
392
|
+
bothAbsent(checker, type);
|
|
250
393
|
const alias = type.aliasSymbol?.name;
|
|
251
394
|
if (alias && iecNames.has(alias)) {
|
|
252
395
|
return alias;
|
|
@@ -259,15 +402,21 @@ export class Layouts {
|
|
|
259
402
|
return this.enumName(enumDeclaration);
|
|
260
403
|
}
|
|
261
404
|
if (type.flags & ts.TypeFlags.NumberLike) {
|
|
262
|
-
|
|
405
|
+
// A `number` is a DINT unless setup gave it a fraction, as an unannotated literal is.
|
|
406
|
+
return fractional(value) ? defaultNumericTypes.float : "number";
|
|
263
407
|
}
|
|
264
408
|
if (type.flags & ts.TypeFlags.BigIntLike) {
|
|
265
|
-
return
|
|
409
|
+
return defaultNumericTypes.bigint;
|
|
266
410
|
}
|
|
267
411
|
const literal = this.literalEnum(type);
|
|
268
412
|
if (literal) {
|
|
269
413
|
return literal.name;
|
|
270
414
|
}
|
|
415
|
+
// A kept module's tagged union is still a union, which the PLC stores as one tagged record.
|
|
416
|
+
const tagged = this.tagged(type);
|
|
417
|
+
if (tagged) {
|
|
418
|
+
return this.taggedRecord(type, tagged);
|
|
419
|
+
}
|
|
271
420
|
const named = this.namedKept(type);
|
|
272
421
|
if (named) {
|
|
273
422
|
return named;
|
|
@@ -275,10 +424,6 @@ export class Layouts {
|
|
|
275
424
|
if (type.flags & ts.TypeFlags.StringLike) {
|
|
276
425
|
return "string";
|
|
277
426
|
}
|
|
278
|
-
const tagged = this.tagged(type);
|
|
279
|
-
if (tagged) {
|
|
280
|
-
return this.taggedRecord(type, tagged);
|
|
281
|
-
}
|
|
282
427
|
if (type.isUnion()) {
|
|
283
428
|
const defined = checker.getNonNullableType(type);
|
|
284
429
|
if (defined !== type) {
|
|
@@ -292,18 +437,11 @@ export class Layouts {
|
|
|
292
437
|
checker.isTupleType(type) ||
|
|
293
438
|
isReadonlyArray(type))
|
|
294
439
|
) {
|
|
295
|
-
|
|
296
|
-
if (!node || node.captured.kind !== "array") {
|
|
297
|
-
throw new Error(
|
|
298
|
-
`${checker.typeToString(type)} needs a length: build it during setup.`,
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
const items = node.captured.items;
|
|
302
|
-
return `FixedArray<${this.text(element, items[0] ?? this.sample(element))}, ${Math.max(items.length, 1)}>`;
|
|
440
|
+
return this.array(type, element, value).text;
|
|
303
441
|
}
|
|
304
|
-
const
|
|
305
|
-
if (
|
|
306
|
-
return
|
|
442
|
+
const record = this.enumRecord(type);
|
|
443
|
+
if (record) {
|
|
444
|
+
return record;
|
|
307
445
|
}
|
|
308
446
|
if (type.flags & ts.TypeFlags.Object) {
|
|
309
447
|
return this.record(type, value, hint);
|
|
@@ -313,7 +451,7 @@ export class Layouts {
|
|
|
313
451
|
|
|
314
452
|
/** The TypeScript enum a type is, or is a union of members of. */
|
|
315
453
|
enumDeclaration(type: ts.Type): ts.EnumDeclaration | undefined {
|
|
316
|
-
const parts = type
|
|
454
|
+
const parts = unionMembers(type);
|
|
317
455
|
const [first] = parts;
|
|
318
456
|
if (!first || !parts.every((part) => part.flags & ts.TypeFlags.EnumLike)) {
|
|
319
457
|
return undefined;
|
|
@@ -329,22 +467,25 @@ export class Layouts {
|
|
|
329
467
|
|
|
330
468
|
/**
|
|
331
469
|
* The IEC type a declaration names, such as `WORD`: TypeScript keeps no alias name for an alias
|
|
332
|
-
* of a primitive, so the width is read from the declaration's annotation.
|
|
470
|
+
* of a primitive, so the width is read from the declaration's annotation. An async routine's
|
|
471
|
+
* result is the one its promise holds, as `(): Promise<UINT>` names.
|
|
333
472
|
*/
|
|
334
473
|
iecName(declaration: ts.Node | undefined): string | undefined {
|
|
335
|
-
|
|
474
|
+
const node = annotation(declaration);
|
|
475
|
+
const awaits =
|
|
476
|
+
declaration !== undefined &&
|
|
477
|
+
ts.canHaveModifiers(declaration) &&
|
|
478
|
+
hasModifier(declaration, ts.SyntaxKind.AsyncKeyword);
|
|
479
|
+
return iecTypeName(node && awaits ? promisedType(node) : node);
|
|
336
480
|
}
|
|
337
481
|
|
|
338
|
-
/**
|
|
482
|
+
/**
|
|
483
|
+
* The IEC width a declaration gives its elements, as `data: Array<BYTE>` does, or an enum-keyed
|
|
484
|
+
* record its values, as `counts: Record<Lane, UINT>` does.
|
|
485
|
+
*/
|
|
339
486
|
elementIec(declaration: ts.Node | undefined): string | undefined {
|
|
340
487
|
const node = annotation(declaration);
|
|
341
|
-
|
|
342
|
-
return iecTypeName(node.elementType);
|
|
343
|
-
}
|
|
344
|
-
if (node && ts.isTypeReferenceNode(node)) {
|
|
345
|
-
return iecTypeName(node.typeArguments?.[0]);
|
|
346
|
-
}
|
|
347
|
-
return undefined;
|
|
488
|
+
return iecTypeName(arrayElement(node) ?? recordValue(node));
|
|
348
489
|
}
|
|
349
490
|
|
|
350
491
|
/** A declared field's layout, keeping the IEC widths its annotation names. */
|
|
@@ -356,39 +497,92 @@ export class Layouts {
|
|
|
356
497
|
): string {
|
|
357
498
|
const iec = this.iecName(declaration);
|
|
358
499
|
if (iec) {
|
|
359
|
-
return iec;
|
|
500
|
+
return this.isOptionalRecord(type) ? this.optionalLayout(iec) : iec;
|
|
501
|
+
}
|
|
502
|
+
// TypeScript keeps no alias name for a record's IEC values, so the width is read from the annotation.
|
|
503
|
+
const record = recordTypes(type);
|
|
504
|
+
const valueIec = iecTypeName(recordValue(annotation(declaration)));
|
|
505
|
+
if (record && valueIec && this.enumRecord(type)) {
|
|
506
|
+
return `Record<${this.text(record.key)}, ${valueIec}>`;
|
|
360
507
|
}
|
|
361
|
-
// A class instance in a record stores the state its class holds, as one in a field does
|
|
508
|
+
// A class instance in a record stores the state its class holds, as one in a field does, and
|
|
509
|
+
// an array of instances each instance's state.
|
|
362
510
|
const node = value && this.model.node(value);
|
|
363
|
-
if (node?.captured.kind === "instance"
|
|
364
|
-
return this.instanceState(node) ??
|
|
511
|
+
if (node?.captured.kind === "instance") {
|
|
512
|
+
return this.instanceState(node) ?? statelessLayout;
|
|
513
|
+
}
|
|
514
|
+
if (node && holdsInstances(this.model, node)) {
|
|
515
|
+
return (
|
|
516
|
+
this.instanceState(node) ??
|
|
517
|
+
arrayLayout(statelessLayout, this.items(type, value).length).text
|
|
518
|
+
);
|
|
365
519
|
}
|
|
366
520
|
const elementName = iecTypeName(arrayElement(annotation(declaration)));
|
|
367
521
|
if (elementName) {
|
|
368
|
-
|
|
369
|
-
if (captured?.captured.kind !== "array") {
|
|
370
|
-
throw new Error(
|
|
371
|
-
`${this.checker.typeToString(type)} needs a length: build it during setup.`,
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
return `FixedArray<${elementName}, ${Math.max(captured.captured.items.length, 1)}>`;
|
|
522
|
+
return arrayLayout(elementName, this.items(type, value).length).text;
|
|
375
523
|
}
|
|
376
524
|
return this.text(type, value, hint);
|
|
377
525
|
}
|
|
378
526
|
|
|
379
|
-
/**
|
|
380
|
-
|
|
527
|
+
/** A declared array field's layout, as `declaredText` gives it, with the length setup gave it. */
|
|
528
|
+
declaredArray(
|
|
529
|
+
declaration: ts.Node | undefined,
|
|
530
|
+
type: ts.Type,
|
|
531
|
+
value: CapturedValue | undefined,
|
|
532
|
+
): ArrayLayout {
|
|
533
|
+
const elementName = iecTypeName(arrayElement(annotation(declaration)));
|
|
534
|
+
if (elementName) {
|
|
535
|
+
return arrayLayout(elementName, this.items(type, value).length);
|
|
536
|
+
}
|
|
537
|
+
const named = this.namedKept(type);
|
|
538
|
+
if (named) {
|
|
539
|
+
return { text: named };
|
|
540
|
+
}
|
|
541
|
+
const element = this.model.elementType(type);
|
|
542
|
+
if (!element) {
|
|
543
|
+
throw new Error(`${this.checker.typeToString(type)} is no array.`);
|
|
544
|
+
}
|
|
545
|
+
return this.array(type, element, value);
|
|
546
|
+
}
|
|
381
547
|
|
|
382
|
-
/** A record field's layout, with the
|
|
383
|
-
|
|
384
|
-
return this.
|
|
548
|
+
/** A record's array field's layout, with the length setup gave it. */
|
|
549
|
+
fieldArray(property: ts.Symbol): ArrayLayout {
|
|
550
|
+
return this.declaredArray(
|
|
385
551
|
property.valueDeclaration,
|
|
386
552
|
this.checker.getTypeOfSymbol(property),
|
|
387
553
|
this.fieldSample(property),
|
|
388
|
-
pascalCase(property.name),
|
|
389
554
|
);
|
|
390
555
|
}
|
|
391
556
|
|
|
557
|
+
private array(
|
|
558
|
+
type: ts.Type,
|
|
559
|
+
element: ts.Type,
|
|
560
|
+
value: CapturedValue | undefined,
|
|
561
|
+
): ArrayLayout & { bound: number } {
|
|
562
|
+
const items = this.items(type, value);
|
|
563
|
+
return arrayLayout(
|
|
564
|
+
this.text(
|
|
565
|
+
element,
|
|
566
|
+
items.find(fractional) ?? items[0] ?? this.sample(element),
|
|
567
|
+
),
|
|
568
|
+
items.length,
|
|
569
|
+
);
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** The items setup gave an array, which its type has no length for. */
|
|
573
|
+
private items(
|
|
574
|
+
type: ts.Type,
|
|
575
|
+
value: CapturedValue | undefined,
|
|
576
|
+
): readonly CapturedValue[] {
|
|
577
|
+
const node = value && this.model.node(value);
|
|
578
|
+
if (node?.captured.kind !== "array") {
|
|
579
|
+
throw new Error(
|
|
580
|
+
`${this.checker.typeToString(type)} needs a length: build it during setup.`,
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
return node.captured.items;
|
|
584
|
+
}
|
|
585
|
+
|
|
392
586
|
/** A captured value of a field, from any captured record that has it. */
|
|
393
587
|
private fieldSample(property: ts.Symbol): CapturedValue | undefined {
|
|
394
588
|
for (const node of this.model.nodes) {
|
|
@@ -452,20 +646,25 @@ export class Layouts {
|
|
|
452
646
|
) {
|
|
453
647
|
return undefined;
|
|
454
648
|
}
|
|
455
|
-
return `Record<${this.text(record.key)}, ${this.text(record.value
|
|
649
|
+
return `Record<${this.text(record.key)}, ${this.text(record.value)}>`;
|
|
456
650
|
}
|
|
457
651
|
|
|
458
652
|
/** `{ has, value }` for `T | undefined`. */
|
|
459
653
|
private optional(defined: ts.Type, value: CapturedValue | undefined): string {
|
|
460
|
-
|
|
461
|
-
defined,
|
|
462
|
-
value?.kind === "undefined" ? undefined : value,
|
|
654
|
+
return this.optionalLayout(
|
|
655
|
+
this.text(defined, value?.kind === "undefined" ? undefined : value),
|
|
463
656
|
);
|
|
464
|
-
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/** The optional record holding a value of layout `inner`. */
|
|
660
|
+
private optionalLayout(layout: string): string {
|
|
661
|
+
// A `number` is a DINT, so an optional DINT is the record an optional number is.
|
|
662
|
+
const inner = layout === defaultNumericTypes.number ? "number" : layout;
|
|
663
|
+
const name = `${optionalRecord.prefix}${inner.replace(/\W+/g, "_")}`;
|
|
465
664
|
if (!this.declarations.has(name)) {
|
|
466
665
|
this.declarations.set(
|
|
467
666
|
name,
|
|
468
|
-
`export interface ${name} { has: boolean; value: ${inner}; }`,
|
|
667
|
+
`export interface ${name} { ${optionalRecord.has}: boolean; ${optionalRecord.value}: ${inner}; }`,
|
|
469
668
|
);
|
|
470
669
|
}
|
|
471
670
|
return name;
|
|
@@ -480,13 +679,17 @@ export class Layouts {
|
|
|
480
679
|
}
|
|
481
680
|
const name = this.unique(type.aliasSymbol?.name ?? "Variant");
|
|
482
681
|
this.records.set(key, name);
|
|
682
|
+
const origin = type.aliasSymbol?.declarations?.[0];
|
|
683
|
+
if (origin) {
|
|
684
|
+
this.origins.set(name, origin);
|
|
685
|
+
}
|
|
483
686
|
const tags = tagged.tags;
|
|
484
687
|
const tagEnum: LiteralEnum = {
|
|
485
688
|
name: `${name}Kind`,
|
|
486
689
|
members: tags,
|
|
487
690
|
optional: tagged.optional,
|
|
488
691
|
};
|
|
489
|
-
this.enums.set(
|
|
692
|
+
this.enums.set(enumKey(tags), tagEnum);
|
|
490
693
|
this.writeEnum(tagEnum);
|
|
491
694
|
const fields = new Map<string, string>();
|
|
492
695
|
for (const variant of tagged.variants) {
|
|
@@ -519,33 +722,156 @@ export class Layouts {
|
|
|
519
722
|
hint?: string,
|
|
520
723
|
): string {
|
|
521
724
|
const node = value && this.model.node(value);
|
|
522
|
-
const
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
return `${property.name}: ${this.declaredText(property.valueDeclaration, this.checker.getTypeOfSymbol(property), captured ?? this.fieldSample(property), pascalCase(property.name))};`;
|
|
529
|
-
});
|
|
530
|
-
const body = fields.join(" ");
|
|
725
|
+
const captured = (name: string) =>
|
|
726
|
+
node &&
|
|
727
|
+
(node.captured.kind === "object" || node.captured.kind === "instance")
|
|
728
|
+
? node.captured.fields[name]
|
|
729
|
+
: undefined;
|
|
730
|
+
const body = this.recordBody(type, captured);
|
|
531
731
|
const symbolName = type.aliasSymbol?.name ?? type.getSymbol()?.name;
|
|
532
732
|
// A type literal and an object literal with the same fields are one shape, so values pass between them unconverted.
|
|
533
733
|
const base =
|
|
534
|
-
symbolName ===
|
|
734
|
+
symbolName === ts.InternalSymbolName.Type ||
|
|
735
|
+
symbolName === ts.InternalSymbolName.Object
|
|
535
736
|
? undefined
|
|
536
737
|
: symbolName;
|
|
738
|
+
const origin = (type.aliasSymbol ?? type.getSymbol())?.declarations?.[0];
|
|
739
|
+
if (
|
|
740
|
+
base === undefined &&
|
|
741
|
+
this.likeKeptLiteral(type, origin, body, captured)
|
|
742
|
+
) {
|
|
743
|
+
return `{ ${body} }`;
|
|
744
|
+
}
|
|
537
745
|
const key = `${base ?? ""}:${body}`;
|
|
538
746
|
const known = this.records.get(key);
|
|
539
747
|
if (known) {
|
|
540
748
|
return known;
|
|
541
749
|
}
|
|
542
|
-
const
|
|
750
|
+
const owner = declaringName(origin?.parent);
|
|
751
|
+
const name = this.declare(
|
|
752
|
+
base ?? hint ?? (owner && pascalCase(owner)) ?? "Fields",
|
|
753
|
+
body,
|
|
754
|
+
origin,
|
|
755
|
+
);
|
|
543
756
|
this.records.set(key, name);
|
|
544
|
-
this.declarations.set(name, `export interface ${name} { ${body} }`);
|
|
545
757
|
return name;
|
|
546
758
|
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Whether an anonymous record is a kept module's type literal or has the same fields as one. Such
|
|
762
|
+
* a literal has no name to import, so the record is written out as a literal, and the PLC dialect
|
|
763
|
+
* makes literals with the same fields one type, which values pass between unconverted.
|
|
764
|
+
*/
|
|
765
|
+
private likeKeptLiteral(
|
|
766
|
+
type: ts.Type,
|
|
767
|
+
origin: ts.Declaration | undefined,
|
|
768
|
+
body: string,
|
|
769
|
+
captured: (name: string) => CapturedValue | undefined,
|
|
770
|
+
): boolean {
|
|
771
|
+
if (
|
|
772
|
+
origin &&
|
|
773
|
+
ts.isTypeLiteralNode(origin) &&
|
|
774
|
+
this.kept(origin.getSourceFile().fileName)
|
|
775
|
+
) {
|
|
776
|
+
return true;
|
|
777
|
+
}
|
|
778
|
+
const names = fieldNames(this.checker, type);
|
|
779
|
+
return this.keptLiterals.some((literal) => {
|
|
780
|
+
const kept = this.checker.getTypeFromTypeNode(literal);
|
|
781
|
+
// Laying out a literal whose fields differ by name could declare what the program never uses.
|
|
782
|
+
return (
|
|
783
|
+
fieldNames(this.checker, kept) === names &&
|
|
784
|
+
this.recordBody(kept, captured) === body
|
|
785
|
+
);
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
/** A record's fields as dialect declarations, each holding the value `captured` gives its name when setup captured one. */
|
|
790
|
+
private recordBody(
|
|
791
|
+
type: ts.Type,
|
|
792
|
+
captured: (name: string) => CapturedValue | undefined,
|
|
793
|
+
): string {
|
|
794
|
+
return this.checker
|
|
795
|
+
.getPropertiesOfType(type)
|
|
796
|
+
.map(
|
|
797
|
+
(property) =>
|
|
798
|
+
`${property.name}: ${this.declaredText(property.valueDeclaration, this.checker.getTypeOfSymbol(property), captured(property.name) ?? this.fieldSample(property), pascalCase(property.name))};`,
|
|
799
|
+
)
|
|
800
|
+
.join(" ");
|
|
801
|
+
}
|
|
547
802
|
}
|
|
548
803
|
|
|
804
|
+
/**
|
|
805
|
+
* The program's enums with a string member, each refused at its declaration: a PLC enum's members
|
|
806
|
+
* are integers, and a union of string literals is the type the PLC stores as an enum of names.
|
|
807
|
+
*/
|
|
808
|
+
export const stringEnums = (
|
|
809
|
+
sources: readonly ts.SourceFile[],
|
|
810
|
+
checker: ts.TypeChecker,
|
|
811
|
+
): SpecializeError[] =>
|
|
812
|
+
sources.flatMap((source) =>
|
|
813
|
+
source.statements.flatMap((statement) => {
|
|
814
|
+
if (
|
|
815
|
+
!ts.isEnumDeclaration(statement) ||
|
|
816
|
+
!statement.members.some(
|
|
817
|
+
(member) => typeof checker.getConstantValue(member) === "string",
|
|
818
|
+
)
|
|
819
|
+
) {
|
|
820
|
+
return [];
|
|
821
|
+
}
|
|
822
|
+
const values = statement.members.map((member) =>
|
|
823
|
+
checker.getConstantValue(member),
|
|
824
|
+
);
|
|
825
|
+
const union = values.every((value) => typeof value === "string")
|
|
826
|
+
? `, or use the string literal union ${values.map((value) => JSON.stringify(value)).join(" | ")}, which the PLC stores as an enum`
|
|
827
|
+
: "";
|
|
828
|
+
return [
|
|
829
|
+
new SpecializeError(
|
|
830
|
+
statement.name,
|
|
831
|
+
`${statement.name.text} has string members, and PLC enums are integers: give each member a number${union}.`,
|
|
832
|
+
),
|
|
833
|
+
];
|
|
834
|
+
}),
|
|
835
|
+
);
|
|
836
|
+
|
|
837
|
+
/** The top-level names a statement declares. */
|
|
838
|
+
const declaredNames = (statement: ts.Statement): string[] => {
|
|
839
|
+
if (ts.isVariableStatement(statement)) {
|
|
840
|
+
return statement.declarationList.declarations.flatMap((declaration) =>
|
|
841
|
+
ts.isIdentifier(declaration.name) ? [declaration.name.text] : [],
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
if (
|
|
845
|
+
(ts.isTypeAliasDeclaration(statement) ||
|
|
846
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
847
|
+
ts.isEnumDeclaration(statement) ||
|
|
848
|
+
ts.isClassDeclaration(statement) ||
|
|
849
|
+
ts.isFunctionDeclaration(statement)) &&
|
|
850
|
+
statement.name
|
|
851
|
+
) {
|
|
852
|
+
return [statement.name.text];
|
|
853
|
+
}
|
|
854
|
+
return [];
|
|
855
|
+
};
|
|
856
|
+
|
|
857
|
+
/** A record's field names in order, which tell whether two records could have the same fields. */
|
|
858
|
+
const fieldNames = (checker: ts.TypeChecker, type: ts.Type): string =>
|
|
859
|
+
checker
|
|
860
|
+
.getPropertiesOfType(type)
|
|
861
|
+
.map((property) => property.name)
|
|
862
|
+
.join(",");
|
|
863
|
+
|
|
864
|
+
/** Whether setup captured a number with a fractional part. */
|
|
865
|
+
const fractional = (value: CapturedValue | undefined): boolean =>
|
|
866
|
+
value?.kind === "number" && !Number.isInteger(value.value);
|
|
867
|
+
|
|
868
|
+
/** Whether a captured array holds class instances, whose stored layout is each one's state. */
|
|
869
|
+
export const holdsInstances = (model: Model, node: ModelNode): boolean =>
|
|
870
|
+
node.captured.kind === "array" &&
|
|
871
|
+
node.captured.items.some(
|
|
872
|
+
(item) => model.node(item)?.captured.kind === "instance",
|
|
873
|
+
);
|
|
874
|
+
|
|
549
875
|
/** The key and value types of a `Record<K, V>`. */
|
|
550
876
|
export const recordTypes = (
|
|
551
877
|
type: ts.Type,
|
|
@@ -556,13 +882,20 @@ export const recordTypes = (
|
|
|
556
882
|
: undefined;
|
|
557
883
|
};
|
|
558
884
|
|
|
559
|
-
/** A declaration's type annotation
|
|
885
|
+
/** A declaration's type annotation, or a routine's result type. */
|
|
560
886
|
const annotation = (
|
|
561
887
|
declaration: ts.Node | undefined,
|
|
562
888
|
): ts.TypeNode | undefined =>
|
|
563
|
-
declaration &&
|
|
889
|
+
declaration &&
|
|
890
|
+
(ts.isVariableDeclaration(declaration) ||
|
|
891
|
+
ts.isPropertyDeclaration(declaration) ||
|
|
892
|
+
ts.isPropertySignature(declaration) ||
|
|
893
|
+
ts.isParameter(declaration) ||
|
|
894
|
+
ts.isFunctionLike(declaration))
|
|
895
|
+
? declaration.type
|
|
896
|
+
: undefined;
|
|
564
897
|
|
|
565
|
-
/** The element type an annotation `T[]`, `Array<T>` or `
|
|
898
|
+
/** The element type an annotation `T[]`, `Array<T>`, `ReadonlyArray<T>` or `FixedArray<T, N>` names. */
|
|
566
899
|
const arrayElement = (
|
|
567
900
|
node: ts.TypeNode | undefined,
|
|
568
901
|
): ts.TypeNode | undefined => {
|
|
@@ -572,30 +905,58 @@ const arrayElement = (
|
|
|
572
905
|
if (
|
|
573
906
|
node &&
|
|
574
907
|
ts.isTypeReferenceNode(node) &&
|
|
575
|
-
["Array", "ReadonlyArray"].includes(node.typeName.getText())
|
|
908
|
+
["Array", "ReadonlyArray", "FixedArray"].includes(node.typeName.getText())
|
|
576
909
|
) {
|
|
577
910
|
return node.typeArguments?.[0];
|
|
578
911
|
}
|
|
579
912
|
return undefined;
|
|
580
913
|
};
|
|
581
914
|
|
|
582
|
-
/** The
|
|
583
|
-
const
|
|
584
|
-
node &&
|
|
585
|
-
|
|
915
|
+
/** The value type an annotation `Record<K, V>` names. */
|
|
916
|
+
const recordValue = (node: ts.TypeNode | undefined): ts.TypeNode | undefined =>
|
|
917
|
+
node &&
|
|
918
|
+
ts.isTypeReferenceNode(node) &&
|
|
919
|
+
node.typeName.getText() === "Record" &&
|
|
920
|
+
node.typeArguments?.length === 2
|
|
921
|
+
? node.typeArguments[1]
|
|
586
922
|
: undefined;
|
|
587
923
|
|
|
588
|
-
|
|
589
|
-
|
|
924
|
+
/** The IEC type an annotation names, such as `WORD`, or the one `WORD | undefined` holds. */
|
|
925
|
+
const iecTypeName = (node: ts.TypeNode | undefined): string | undefined => {
|
|
926
|
+
if (node && ts.isUnionTypeNode(node)) {
|
|
927
|
+
const [defined, ...others] = node.types.filter(
|
|
928
|
+
(member) =>
|
|
929
|
+
!(
|
|
930
|
+
member.kind === ts.SyntaxKind.UndefinedKeyword ||
|
|
931
|
+
(ts.isLiteralTypeNode(member) &&
|
|
932
|
+
member.literal.kind === ts.SyntaxKind.NullKeyword)
|
|
933
|
+
),
|
|
934
|
+
);
|
|
935
|
+
return others.length ? undefined : iecTypeName(defined);
|
|
936
|
+
}
|
|
937
|
+
if (!node || !ts.isTypeReferenceNode(node)) {
|
|
938
|
+
return undefined;
|
|
939
|
+
}
|
|
940
|
+
const name = node.typeName.getText();
|
|
941
|
+
const [length, ...rest] = node.typeArguments ?? [];
|
|
942
|
+
if (!length) {
|
|
943
|
+
return iecNames.has(name) ? name : undefined;
|
|
944
|
+
}
|
|
945
|
+
// A sized string keeps its capacity, which its resolved type, a plain string, has lost.
|
|
946
|
+
return (name === "STRING" || name === "WSTRING") &&
|
|
947
|
+
!rest.length &&
|
|
948
|
+
ts.isLiteralTypeNode(length) &&
|
|
949
|
+
ts.isNumericLiteral(length.literal)
|
|
950
|
+
? `${name}<${length.literal.text}>`
|
|
951
|
+
: undefined;
|
|
952
|
+
};
|
|
590
953
|
|
|
591
954
|
const isReadonlyArray = (type: ts.Type): boolean =>
|
|
592
955
|
type.getSymbol()?.name === "ReadonlyArray";
|
|
593
956
|
|
|
594
|
-
/**
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
letter.toUpperCase(),
|
|
598
|
-
);
|
|
957
|
+
/** The key a string-literal union's enum is registered under, whatever order its members are written in. */
|
|
958
|
+
const enumKey = (members: readonly string[]): string =>
|
|
959
|
+
[...members].sort().join("|");
|
|
599
960
|
|
|
600
961
|
/** `base`, or else the first of `base_2`, `base_3`… that `taken` does not hold. */
|
|
601
962
|
export const unusedName = (
|
|
@@ -614,7 +975,7 @@ export const unusedName = (
|
|
|
614
975
|
};
|
|
615
976
|
|
|
616
977
|
/** A string literal as an enum member name. */
|
|
617
|
-
|
|
978
|
+
const literalMember = (value: string): string => {
|
|
618
979
|
const name = pascalCase(value);
|
|
619
980
|
return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
|
|
620
981
|
};
|