@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,1025 @@
|
|
|
1
|
+
import { optionalRecord, pascalCase, primitiveTypes } from "@tsplc/compiler/ir";
|
|
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";
|
|
10
|
+
import type { CapturedValue } from "./capture-protocol";
|
|
11
|
+
import { ModuleImports } from "./imports";
|
|
12
|
+
import { isExported, type Model, type ModelNode, unionMembers } from "./model";
|
|
13
|
+
import { type Names, rootFieldNames } from "./names";
|
|
14
|
+
import { SpecializeError } from "./values";
|
|
15
|
+
|
|
16
|
+
/** IEC names a program can write as types; they keep their width rather than becoming DINT. */
|
|
17
|
+
const iecNames = new Set(primitiveTypes.map((type) => type.id));
|
|
18
|
+
|
|
19
|
+
/** What an instance that stores nothing is kept as where a record or array holds it. */
|
|
20
|
+
export const statelessLayout = "boolean";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* A stored array's PLC type, `FixedArray<T, n>`, with how many elements it has room for and T when
|
|
24
|
+
* T is an IEC name. A kept module's named array type is only its name.
|
|
25
|
+
*/
|
|
26
|
+
export interface ArrayLayout {
|
|
27
|
+
text: string;
|
|
28
|
+
bound?: number;
|
|
29
|
+
elementIec?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** An optional record holding the defined value `text`. */
|
|
33
|
+
export const definedOptional = (text: Part): Mapped =>
|
|
34
|
+
mapped`{ ${optionalRecord.has}: true, ${optionalRecord.value}: ${text} }`;
|
|
35
|
+
|
|
36
|
+
/** An optional record holding nothing, its value the empty `text`. */
|
|
37
|
+
export const emptyOptional = (text: Part): Mapped =>
|
|
38
|
+
mapped`{ ${optionalRecord.has}: false, ${optionalRecord.value}: ${text} }`;
|
|
39
|
+
|
|
40
|
+
/** Whether the optional record `text` holds a value. */
|
|
41
|
+
export const optionalHas = (text: Part): Mapped =>
|
|
42
|
+
mapped`${text}.${optionalRecord.has}`;
|
|
43
|
+
|
|
44
|
+
/** The value the optional record `text` holds. */
|
|
45
|
+
export const optionalValue = (text: Part): Mapped =>
|
|
46
|
+
mapped`${text}.${optionalRecord.value}`;
|
|
47
|
+
|
|
48
|
+
/** The layout of an array of `length` elements: the PLC has no empty array, so it keeps at least one slot. */
|
|
49
|
+
export const arrayLayout = (
|
|
50
|
+
element: string,
|
|
51
|
+
length: number,
|
|
52
|
+
): ArrayLayout & { bound: number } => {
|
|
53
|
+
const bound = Math.max(length, 1);
|
|
54
|
+
return {
|
|
55
|
+
text: `FixedArray<${element}, ${bound}>`,
|
|
56
|
+
bound,
|
|
57
|
+
...(iecNames.has(element) && { elementIec: element }),
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** A union of string literals: a PLC enum. */
|
|
62
|
+
interface LiteralEnum {
|
|
63
|
+
name: string;
|
|
64
|
+
members: string[];
|
|
65
|
+
/** The union also allows undefined, stored as this extra member. */
|
|
66
|
+
optional: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** A union of object types told apart by a string-literal field, the tag. */
|
|
70
|
+
interface TaggedUnion {
|
|
71
|
+
tag: string;
|
|
72
|
+
variants: ts.Type[];
|
|
73
|
+
/** Each variant's tag, in the order of the variants. */
|
|
74
|
+
tags: string[];
|
|
75
|
+
/** The union also allows undefined. */
|
|
76
|
+
optional: boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** What the generated types module declares: each declaration's text by name, and which of them are enums. */
|
|
80
|
+
export interface TypeDeclarations {
|
|
81
|
+
declarations: ReadonlyMap<string, Part>;
|
|
82
|
+
enums: readonly string[];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Turns the program's TypeScript types into the PLC dialect's: string-literal unions become enums,
|
|
87
|
+
* unions of objects become one record tagged by their literal field, `T | undefined` becomes an
|
|
88
|
+
* optional record, and arrays take their captured length.
|
|
89
|
+
*/
|
|
90
|
+
export class Layouts {
|
|
91
|
+
private readonly declarations = new Map<string, string>();
|
|
92
|
+
/** The program's declaration each generated one stands for, where diagnostics about it point. */
|
|
93
|
+
private readonly origins = new Map<string, ts.Node>();
|
|
94
|
+
/**
|
|
95
|
+
* Names a generated declaration mustn't take: what kept modules declare, which generated modules
|
|
96
|
+
* may import beside the generated ones, and the namespaces storage modules are imported as.
|
|
97
|
+
*/
|
|
98
|
+
private readonly takenNames: Set<string>;
|
|
99
|
+
/** Enums the declarations need from the program's modules. */
|
|
100
|
+
readonly imports: ModuleImports;
|
|
101
|
+
/** Types the declarations need from kept modules. */
|
|
102
|
+
readonly typeImports: ModuleImports;
|
|
103
|
+
private readonly enums = new Map<string, LiteralEnum>();
|
|
104
|
+
private readonly records = new Map<string, string>();
|
|
105
|
+
private readonly names = new Map<string, number>();
|
|
106
|
+
|
|
107
|
+
/** The type literals kept modules write, which name no type a generated module could import. */
|
|
108
|
+
private readonly keptLiterals: ts.TypeLiteralNode[] = [];
|
|
109
|
+
|
|
110
|
+
/** Exported types of kept modules by the type they declare, for types that lost their alias name. */
|
|
111
|
+
private readonly keptTypes = new Map<
|
|
112
|
+
ts.Type,
|
|
113
|
+
{ file: string; name: string }
|
|
114
|
+
>();
|
|
115
|
+
|
|
116
|
+
constructor(
|
|
117
|
+
private readonly model: Model,
|
|
118
|
+
private readonly checker: ts.TypeChecker,
|
|
119
|
+
/** Modules the compiled program keeps, whose named types and enums it imports rather than copies. */
|
|
120
|
+
private readonly kept: (file: string) => boolean,
|
|
121
|
+
keptSources: readonly ts.SourceFile[],
|
|
122
|
+
/** The stored layout of a class instance or an array of them, which storage, built on these layouts, gives. */
|
|
123
|
+
private readonly instanceState: (node: ModelNode) => string | undefined,
|
|
124
|
+
private readonly moduleNames: Names,
|
|
125
|
+
/** Why the generated modules can't import an enum from the module that declares it; undefined when they can. */
|
|
126
|
+
private readonly enumRefusal: (
|
|
127
|
+
declaration: ts.EnumDeclaration,
|
|
128
|
+
) => string | undefined,
|
|
129
|
+
) {
|
|
130
|
+
// Each is imported into every root's storage module, beside that root's fields.
|
|
131
|
+
const fields = rootFieldNames(model);
|
|
132
|
+
this.imports = new ModuleImports(moduleNames, fields);
|
|
133
|
+
this.typeImports = new ModuleImports(moduleNames, fields);
|
|
134
|
+
// Storage imports a root's module under its capitalized name, unless the program names a type so.
|
|
135
|
+
const typeNames = new Set(
|
|
136
|
+
model.program
|
|
137
|
+
.getSourceFiles()
|
|
138
|
+
.filter((source) => !source.isDeclarationFile)
|
|
139
|
+
.flatMap((source) =>
|
|
140
|
+
source.statements.flatMap((statement) =>
|
|
141
|
+
ts.isVariableStatement(statement) ||
|
|
142
|
+
ts.isFunctionDeclaration(statement)
|
|
143
|
+
? []
|
|
144
|
+
: declaredNames(statement),
|
|
145
|
+
),
|
|
146
|
+
),
|
|
147
|
+
);
|
|
148
|
+
this.takenNames = new Set(
|
|
149
|
+
model.roots
|
|
150
|
+
.map((root) => pascalCase(root.name))
|
|
151
|
+
.filter((name) => !typeNames.has(name)),
|
|
152
|
+
);
|
|
153
|
+
const collectLiterals = (node: ts.Node): void => {
|
|
154
|
+
if (ts.isTypeLiteralNode(node)) {
|
|
155
|
+
this.keptLiterals.push(node);
|
|
156
|
+
}
|
|
157
|
+
ts.forEachChild(node, collectLiterals);
|
|
158
|
+
};
|
|
159
|
+
for (const source of keptSources) {
|
|
160
|
+
collectLiterals(source);
|
|
161
|
+
for (const statement of source.statements) {
|
|
162
|
+
for (const name of declaredNames(statement)) {
|
|
163
|
+
this.takenNames.add(name);
|
|
164
|
+
}
|
|
165
|
+
if (
|
|
166
|
+
(ts.isTypeAliasDeclaration(statement) ||
|
|
167
|
+
ts.isInterfaceDeclaration(statement)) &&
|
|
168
|
+
isExported(statement)
|
|
169
|
+
) {
|
|
170
|
+
const symbol = checker.getSymbolAtLocation(statement.name);
|
|
171
|
+
if (symbol) {
|
|
172
|
+
this.keptTypes.set(checker.getDeclaredTypeOfSymbol(symbol), {
|
|
173
|
+
file: source.fileName,
|
|
174
|
+
name: statement.name.text,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A declaration name no other declaration has, nor any of `takenNames`. */
|
|
183
|
+
private unique(base: string): string {
|
|
184
|
+
const count = this.names.get(base) ?? 0;
|
|
185
|
+
this.names.set(base, count + 1);
|
|
186
|
+
const name = count ? `${base}_${count + 1}` : base;
|
|
187
|
+
if (
|
|
188
|
+
this.declarations.has(name) ||
|
|
189
|
+
this.takenNames.has(name) ||
|
|
190
|
+
this.moduleNames.importedAs(name)
|
|
191
|
+
) {
|
|
192
|
+
return this.unique(base);
|
|
193
|
+
}
|
|
194
|
+
this.moduleNames.declare(name);
|
|
195
|
+
return name;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Declares an interface of `body` under the first free name from `base`, standing for the
|
|
200
|
+
* program's `origin` when it has one, and returns the name.
|
|
201
|
+
*/
|
|
202
|
+
declare(base: string, body: string, origin: ts.Node | undefined): string {
|
|
203
|
+
const name = this.unique(base);
|
|
204
|
+
this.declarations.set(name, `export interface ${name} { ${body} }`);
|
|
205
|
+
if (origin) {
|
|
206
|
+
this.origins.set(name, origin);
|
|
207
|
+
}
|
|
208
|
+
return name;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** What is declared so far. Generated modules import from the types module what it declares once every layout is made. */
|
|
212
|
+
snapshot(): TypeDeclarations {
|
|
213
|
+
return {
|
|
214
|
+
declarations: new Map(
|
|
215
|
+
[...this.declarations].map(([name, text]) => {
|
|
216
|
+
const origin = this.origins.get(name);
|
|
217
|
+
return [name, origin ? from(origin, text) : text];
|
|
218
|
+
}),
|
|
219
|
+
),
|
|
220
|
+
enums: [...this.declarations]
|
|
221
|
+
.filter(([, text]) => text.startsWith("export enum"))
|
|
222
|
+
.map(([name]) => name),
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Each string-literal union's enum, with the string each member stands for. */
|
|
227
|
+
get literalEnums(): { name: string; members: Record<string, string> }[] {
|
|
228
|
+
return [...this.enums.values()].map((declared) => ({
|
|
229
|
+
name: declared.name,
|
|
230
|
+
members: Object.fromEntries(
|
|
231
|
+
declared.members.map((member) => [literalMember(member), member]),
|
|
232
|
+
),
|
|
233
|
+
}));
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** The string literals of a union of them, without undefined. */
|
|
237
|
+
literals(
|
|
238
|
+
type: ts.Type,
|
|
239
|
+
): { members: string[]; optional: boolean } | undefined {
|
|
240
|
+
const parts = unionMembers(type);
|
|
241
|
+
const defined = parts.filter((part) => !isAbsent(part));
|
|
242
|
+
const members = defined.flatMap((part) =>
|
|
243
|
+
part.isStringLiteral() ? [part.value] : [],
|
|
244
|
+
);
|
|
245
|
+
if (!members.length || members.length !== defined.length) {
|
|
246
|
+
return undefined;
|
|
247
|
+
}
|
|
248
|
+
return { members, optional: defined.length !== parts.length };
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The enum a string-literal union compiles to. */
|
|
252
|
+
literalEnum(type: ts.Type): LiteralEnum | undefined {
|
|
253
|
+
const literals = this.literals(type);
|
|
254
|
+
if (!literals) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
const key = enumKey(literals.members);
|
|
258
|
+
const existing = this.enums.get(key);
|
|
259
|
+
if (existing) {
|
|
260
|
+
existing.optional ||= literals.optional;
|
|
261
|
+
this.writeEnum(existing);
|
|
262
|
+
return existing;
|
|
263
|
+
}
|
|
264
|
+
const alias =
|
|
265
|
+
type.aliasSymbol ?? this.checker.getNonNullableType(type).aliasSymbol;
|
|
266
|
+
const declared: LiteralEnum = {
|
|
267
|
+
name: this.unique(
|
|
268
|
+
alias?.name ?? pascalCase(literals.members.join("_or_")),
|
|
269
|
+
),
|
|
270
|
+
members: literals.members,
|
|
271
|
+
optional: literals.optional,
|
|
272
|
+
};
|
|
273
|
+
const origin = alias?.declarations?.[0];
|
|
274
|
+
if (origin) {
|
|
275
|
+
this.origins.set(declared.name, origin);
|
|
276
|
+
}
|
|
277
|
+
this.enums.set(key, declared);
|
|
278
|
+
this.writeEnum(declared);
|
|
279
|
+
return declared;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private writeEnum(declared: LiteralEnum): void {
|
|
283
|
+
const members = [
|
|
284
|
+
...(declared.optional ? ["Undefined = 0"] : []),
|
|
285
|
+
...declared.members.map(
|
|
286
|
+
(member, index) => `${literalMember(member)} = ${index + 1}`,
|
|
287
|
+
),
|
|
288
|
+
];
|
|
289
|
+
this.declarations.set(
|
|
290
|
+
declared.name,
|
|
291
|
+
`export enum ${declared.name} { ${members.join(", ")} }`,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** `Enum.Member` text for a string literal of a literal union. */
|
|
296
|
+
member(declared: LiteralEnum, value: string | undefined): string {
|
|
297
|
+
return `${declared.name}.${value === undefined ? "Undefined" : literalMember(value)}`;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** The object types of a union of them, without undefined, when it has a literal tag field. */
|
|
301
|
+
tagged(type: ts.Type): TaggedUnion | undefined {
|
|
302
|
+
if (!type.isUnion()) {
|
|
303
|
+
return undefined;
|
|
304
|
+
}
|
|
305
|
+
const variants = type.types.filter((part) => !isAbsent(part));
|
|
306
|
+
const [first, ...others] = variants;
|
|
307
|
+
if (
|
|
308
|
+
!first ||
|
|
309
|
+
!others.length ||
|
|
310
|
+
variants.some((part) => !(part.flags & ts.TypeFlags.Object))
|
|
311
|
+
) {
|
|
312
|
+
return undefined;
|
|
313
|
+
}
|
|
314
|
+
const literal = (variant: ts.Type, name: string) => {
|
|
315
|
+
const own = variant.getProperty(name);
|
|
316
|
+
const value = own && this.checker.getTypeOfSymbol(own);
|
|
317
|
+
return value?.isStringLiteral() ? value.value : undefined;
|
|
318
|
+
};
|
|
319
|
+
for (const property of first.getProperties()) {
|
|
320
|
+
const tags = variants.map((variant) => literal(variant, property.name));
|
|
321
|
+
if (tags.every((tag): tag is string => tag !== undefined)) {
|
|
322
|
+
return {
|
|
323
|
+
tag: property.name,
|
|
324
|
+
variants,
|
|
325
|
+
tags,
|
|
326
|
+
optional: variants.length !== type.types.length,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** The fields of a tagged union's record: the tag, then every variant's fields. */
|
|
334
|
+
taggedFields(tagged: TaggedUnion): string[] {
|
|
335
|
+
const names = [tagged.tag];
|
|
336
|
+
for (const variant of tagged.variants) {
|
|
337
|
+
for (const property of variant.getProperties()) {
|
|
338
|
+
if (!names.includes(property.name)) {
|
|
339
|
+
names.push(property.name);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return names;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** The declared type of a field of a tagged union, from the first variant that has it. */
|
|
347
|
+
taggedFieldType(
|
|
348
|
+
declared: ts.Type,
|
|
349
|
+
tagged: TaggedUnion,
|
|
350
|
+
name: string,
|
|
351
|
+
): ts.Type {
|
|
352
|
+
return this.checker.getTypeOfSymbol(
|
|
353
|
+
this.taggedField(declared, tagged, name),
|
|
354
|
+
);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/** A field of a tagged union, from the first variant that has it, whose declaration names its IEC width. */
|
|
358
|
+
taggedField(declared: ts.Type, tagged: TaggedUnion, name: string): ts.Symbol {
|
|
359
|
+
for (const variant of tagged.variants) {
|
|
360
|
+
const property = variant.getProperty(name);
|
|
361
|
+
if (property) {
|
|
362
|
+
return property;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
throw new Error(
|
|
366
|
+
`${name} is in no variant of ${this.checker.typeToString(declared)}.`,
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** The type of a tagged union's tag, once the union's record and tag enum are declared. */
|
|
371
|
+
tagType(declared: ts.Type, tagged: TaggedUnion): ts.Type {
|
|
372
|
+
this.text(declared);
|
|
373
|
+
const property = this.checker.getPropertyOfType(declared, tagged.tag);
|
|
374
|
+
return property ? this.checker.getTypeOfSymbol(property) : declared;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** The enum of a tagged union's tag. */
|
|
378
|
+
tagEnum(declared: ts.Type, tagged: TaggedUnion): LiteralEnum {
|
|
379
|
+
const tag = this.literalEnum(this.tagType(declared, tagged));
|
|
380
|
+
if (!tag) {
|
|
381
|
+
throw new Error(
|
|
382
|
+
`${tagged.tag} of ${this.checker.typeToString(declared)} is no string literal.`,
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
return tag;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** `Enum.Member` text for the member of a TypeScript enum type that has `value`. */
|
|
389
|
+
enumMember(type: ts.Type, value: number): string | undefined {
|
|
390
|
+
const member = this.enumDeclaration(type)?.members.find(
|
|
391
|
+
(candidate) => this.checker.getConstantValue(candidate) === value,
|
|
392
|
+
);
|
|
393
|
+
return member && `${this.text(type)}.${member.name.getText()}`;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Whether `T | undefined` is stored as `{ has, value }`; a literal union has an Undefined member instead. */
|
|
397
|
+
isOptionalRecord(type: ts.Type): boolean {
|
|
398
|
+
return (
|
|
399
|
+
type.isUnion() &&
|
|
400
|
+
type.types.some((part) => isAbsent(part)) &&
|
|
401
|
+
!this.literals(type)
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/** The dialect type text for `type`, holding `value` when it was captured. */
|
|
406
|
+
text(type: ts.Type, value?: CapturedValue, hint?: string): string {
|
|
407
|
+
const checker = this.checker;
|
|
408
|
+
bothAbsent(checker, type);
|
|
409
|
+
const alias = type.aliasSymbol?.name;
|
|
410
|
+
if (alias && iecNames.has(alias)) {
|
|
411
|
+
return alias;
|
|
412
|
+
}
|
|
413
|
+
if (type.flags & ts.TypeFlags.BooleanLike) {
|
|
414
|
+
return "boolean";
|
|
415
|
+
}
|
|
416
|
+
const enumDeclaration = this.enumDeclaration(type);
|
|
417
|
+
if (enumDeclaration) {
|
|
418
|
+
return this.enumName(enumDeclaration);
|
|
419
|
+
}
|
|
420
|
+
if (type.flags & ts.TypeFlags.NumberLike) {
|
|
421
|
+
// A `number` is a DINT unless setup gave it a fraction, as an unannotated literal is.
|
|
422
|
+
return fractional(value) ? defaultNumericTypes.float : "number";
|
|
423
|
+
}
|
|
424
|
+
if (type.flags & ts.TypeFlags.BigIntLike) {
|
|
425
|
+
return defaultNumericTypes.bigint;
|
|
426
|
+
}
|
|
427
|
+
const literal = this.literalEnum(type);
|
|
428
|
+
if (literal) {
|
|
429
|
+
return literal.name;
|
|
430
|
+
}
|
|
431
|
+
// A kept module's tagged union is still a union, which the PLC stores as one tagged record.
|
|
432
|
+
const tagged = this.tagged(type);
|
|
433
|
+
if (tagged) {
|
|
434
|
+
return this.taggedRecord(type, tagged);
|
|
435
|
+
}
|
|
436
|
+
const named = this.namedKept(type);
|
|
437
|
+
if (named) {
|
|
438
|
+
return named;
|
|
439
|
+
}
|
|
440
|
+
if (type.flags & ts.TypeFlags.StringLike) {
|
|
441
|
+
return "string";
|
|
442
|
+
}
|
|
443
|
+
if (type.isUnion()) {
|
|
444
|
+
const defined = checker.getNonNullableType(type);
|
|
445
|
+
if (defined !== type) {
|
|
446
|
+
return this.optional(defined, value);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
const element = this.model.elementType(type);
|
|
450
|
+
if (
|
|
451
|
+
element &&
|
|
452
|
+
(checker.isArrayType(type) ||
|
|
453
|
+
checker.isTupleType(type) ||
|
|
454
|
+
isReadonlyArray(type))
|
|
455
|
+
) {
|
|
456
|
+
return this.array(type, element, value).text;
|
|
457
|
+
}
|
|
458
|
+
const record = this.enumRecord(type);
|
|
459
|
+
if (record) {
|
|
460
|
+
return record;
|
|
461
|
+
}
|
|
462
|
+
if (type.flags & ts.TypeFlags.Object) {
|
|
463
|
+
return this.record(type, value, hint);
|
|
464
|
+
}
|
|
465
|
+
throw new Error(`${checker.typeToString(type)} has no PLC layout.`);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** The TypeScript enum a type is, or is a union of members of. */
|
|
469
|
+
enumDeclaration(type: ts.Type): ts.EnumDeclaration | undefined {
|
|
470
|
+
const parts = unionMembers(type);
|
|
471
|
+
const [first] = parts;
|
|
472
|
+
if (!first || !parts.every((part) => part.flags & ts.TypeFlags.EnumLike)) {
|
|
473
|
+
return undefined;
|
|
474
|
+
}
|
|
475
|
+
const symbol = first.symbol;
|
|
476
|
+
const declaration =
|
|
477
|
+
symbol?.declarations?.find(ts.isEnumDeclaration) ??
|
|
478
|
+
symbol?.valueDeclaration?.parent;
|
|
479
|
+
return declaration && ts.isEnumDeclaration(declaration)
|
|
480
|
+
? declaration
|
|
481
|
+
: undefined;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* The IEC type a declaration names, such as `WORD`: TypeScript keeps no alias name for an alias
|
|
486
|
+
* of a primitive, so the width is read from the declaration's annotation. An async routine's
|
|
487
|
+
* result is the one its promise holds, as `(): Promise<UINT>` names.
|
|
488
|
+
*/
|
|
489
|
+
iecName(declaration: ts.Node | undefined): string | undefined {
|
|
490
|
+
const node = annotation(declaration);
|
|
491
|
+
const awaits =
|
|
492
|
+
declaration !== undefined &&
|
|
493
|
+
ts.canHaveModifiers(declaration) &&
|
|
494
|
+
hasModifier(declaration, ts.SyntaxKind.AsyncKeyword);
|
|
495
|
+
return iecTypeName(node && awaits ? promisedType(node) : node);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* The IEC width a declaration gives its elements, as `data: Array<BYTE>` does, or an enum-keyed
|
|
500
|
+
* record its values, as `counts: Record<Lane, UINT>` does.
|
|
501
|
+
*/
|
|
502
|
+
elementIec(declaration: ts.Node | undefined): string | undefined {
|
|
503
|
+
const node = annotation(declaration);
|
|
504
|
+
return iecTypeName(arrayElement(node) ?? recordValue(node));
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/** A declared field's layout, keeping the IEC widths its annotation names. */
|
|
508
|
+
declaredText(
|
|
509
|
+
declaration: ts.Node | undefined,
|
|
510
|
+
type: ts.Type,
|
|
511
|
+
value?: CapturedValue,
|
|
512
|
+
hint?: string,
|
|
513
|
+
): string {
|
|
514
|
+
const iec = this.iecName(declaration);
|
|
515
|
+
if (iec) {
|
|
516
|
+
return this.isOptionalRecord(type) ? this.optionalLayout(iec) : iec;
|
|
517
|
+
}
|
|
518
|
+
// TypeScript keeps no alias name for a record's IEC values, so the width is read from the annotation.
|
|
519
|
+
const record = recordTypes(type);
|
|
520
|
+
const valueIec = iecTypeName(recordValue(annotation(declaration)));
|
|
521
|
+
if (record && valueIec && this.enumRecord(type)) {
|
|
522
|
+
return `Record<${this.text(record.key)}, ${valueIec}>`;
|
|
523
|
+
}
|
|
524
|
+
// A class instance in a record stores the state its class holds, as one in a field does, and
|
|
525
|
+
// an array of instances each instance's state.
|
|
526
|
+
const node = value && this.model.node(value);
|
|
527
|
+
if (node?.captured.kind === "instance") {
|
|
528
|
+
return this.instanceState(node) ?? statelessLayout;
|
|
529
|
+
}
|
|
530
|
+
if (node && holdsInstances(this.model, node)) {
|
|
531
|
+
return (
|
|
532
|
+
this.instanceState(node) ??
|
|
533
|
+
arrayLayout(statelessLayout, this.items(type, value).length).text
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
const elementName = iecTypeName(arrayElement(annotation(declaration)));
|
|
537
|
+
if (elementName) {
|
|
538
|
+
return arrayLayout(elementName, this.items(type, value).length).text;
|
|
539
|
+
}
|
|
540
|
+
return this.text(type, value, hint);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/** A declared array field's layout, as `declaredText` gives it, with the length setup gave it. */
|
|
544
|
+
declaredArray(
|
|
545
|
+
declaration: ts.Node | undefined,
|
|
546
|
+
type: ts.Type,
|
|
547
|
+
value: CapturedValue | undefined,
|
|
548
|
+
): ArrayLayout {
|
|
549
|
+
const elementName = iecTypeName(arrayElement(annotation(declaration)));
|
|
550
|
+
if (elementName) {
|
|
551
|
+
return arrayLayout(elementName, this.items(type, value).length);
|
|
552
|
+
}
|
|
553
|
+
const named = this.namedKept(type);
|
|
554
|
+
if (named) {
|
|
555
|
+
return { text: named };
|
|
556
|
+
}
|
|
557
|
+
const element = this.model.elementType(type);
|
|
558
|
+
if (!element) {
|
|
559
|
+
throw new Error(`${this.checker.typeToString(type)} is no array.`);
|
|
560
|
+
}
|
|
561
|
+
return this.array(type, element, value);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** A record's array field's layout, with the length setup gave it. */
|
|
565
|
+
fieldArray(property: ts.Symbol): ArrayLayout {
|
|
566
|
+
return this.declaredArray(
|
|
567
|
+
property.valueDeclaration,
|
|
568
|
+
this.checker.getTypeOfSymbol(property),
|
|
569
|
+
this.fieldSample(property),
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
private array(
|
|
574
|
+
type: ts.Type,
|
|
575
|
+
element: ts.Type,
|
|
576
|
+
value: CapturedValue | undefined,
|
|
577
|
+
): ArrayLayout & { bound: number } {
|
|
578
|
+
const items = this.items(type, value);
|
|
579
|
+
return arrayLayout(
|
|
580
|
+
this.text(
|
|
581
|
+
element,
|
|
582
|
+
items.find(fractional) ?? items[0] ?? this.sample(element),
|
|
583
|
+
),
|
|
584
|
+
items.length,
|
|
585
|
+
);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/** The items setup gave an array, which its type has no length for. */
|
|
589
|
+
private items(
|
|
590
|
+
type: ts.Type,
|
|
591
|
+
value: CapturedValue | undefined,
|
|
592
|
+
): readonly CapturedValue[] {
|
|
593
|
+
const node = value && this.model.node(value);
|
|
594
|
+
if (node?.captured.kind !== "array") {
|
|
595
|
+
throw new Error(
|
|
596
|
+
`${this.checker.typeToString(type)} needs a length: build it during setup.`,
|
|
597
|
+
);
|
|
598
|
+
}
|
|
599
|
+
return node.captured.items;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/** A captured value of a field, from any captured record that has it. */
|
|
603
|
+
private fieldSample(property: ts.Symbol): CapturedValue | undefined {
|
|
604
|
+
for (const node of this.model.nodes) {
|
|
605
|
+
const captured = node.captured;
|
|
606
|
+
if (
|
|
607
|
+
(captured.kind === "object" || captured.kind === "instance") &&
|
|
608
|
+
node.type?.getProperty(property.name) === property
|
|
609
|
+
) {
|
|
610
|
+
return captured.fields[property.name];
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
return undefined;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
/** Some captured value of `type`, giving the lengths of an empty array's element. */
|
|
617
|
+
private sample(type: ts.Type): CapturedValue | undefined {
|
|
618
|
+
const node = this.model.nodes.find((candidate) => candidate.type === type);
|
|
619
|
+
return node && { kind: "node", id: node.id };
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/** A named type from a module the program keeps, such as a service's procedure record. */
|
|
623
|
+
private namedKept(type: ts.Type): string | undefined {
|
|
624
|
+
const declared = this.keptTypes.get(type);
|
|
625
|
+
if (declared) {
|
|
626
|
+
return this.typeImports.add(declared.file, declared.name);
|
|
627
|
+
}
|
|
628
|
+
const symbol = type.aliasSymbol ?? type.getSymbol();
|
|
629
|
+
const declaration = symbol?.declarations?.[0];
|
|
630
|
+
if (!symbol || !declaration) {
|
|
631
|
+
return undefined;
|
|
632
|
+
}
|
|
633
|
+
if (
|
|
634
|
+
!ts.isTypeAliasDeclaration(declaration) &&
|
|
635
|
+
!ts.isInterfaceDeclaration(declaration)
|
|
636
|
+
) {
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
639
|
+
const file = declaration.getSourceFile().fileName;
|
|
640
|
+
if (!this.kept(file) || !isExported(declaration)) {
|
|
641
|
+
return undefined;
|
|
642
|
+
}
|
|
643
|
+
return this.typeImports.add(file, symbol.name);
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* The name generated code reads a TypeScript enum by: its own, imported from the module that
|
|
648
|
+
* declares it, so the PLC, the server and the program's own modules share one enum.
|
|
649
|
+
*/
|
|
650
|
+
private enumName(declaration: ts.EnumDeclaration): string {
|
|
651
|
+
const refusal = this.enumRefusal(declaration);
|
|
652
|
+
if (refusal) {
|
|
653
|
+
throw new SpecializeError(declaration.name, refusal);
|
|
654
|
+
}
|
|
655
|
+
return this.imports.add(
|
|
656
|
+
declaration.getSourceFile().fileName,
|
|
657
|
+
declaration.name.text,
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/** `Record<Enum, T>`: an object type whose properties are exactly an enum's members. */
|
|
662
|
+
private enumRecord(type: ts.Type): string | undefined {
|
|
663
|
+
const record = recordTypes(type);
|
|
664
|
+
if (
|
|
665
|
+
!record ||
|
|
666
|
+
(!this.enumDeclaration(record.key) && !this.literalEnum(record.key))
|
|
667
|
+
) {
|
|
668
|
+
return undefined;
|
|
669
|
+
}
|
|
670
|
+
return `Record<${this.text(record.key)}, ${this.text(record.value)}>`;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
/** `{ has, value }` for `T | undefined`. */
|
|
674
|
+
private optional(defined: ts.Type, value: CapturedValue | undefined): string {
|
|
675
|
+
return this.optionalLayout(
|
|
676
|
+
this.text(defined, value?.kind === "undefined" ? undefined : value),
|
|
677
|
+
);
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
/** The optional record holding a value of layout `inner`. */
|
|
681
|
+
private optionalLayout(layout: string): string {
|
|
682
|
+
// A `number` is a DINT, so an optional DINT is the record an optional number is.
|
|
683
|
+
const inner = layout === defaultNumericTypes.number ? "number" : layout;
|
|
684
|
+
const name = `${optionalRecord.prefix}${inner.replace(/\W+/g, "_")}`;
|
|
685
|
+
if (!this.declarations.has(name)) {
|
|
686
|
+
this.declarations.set(
|
|
687
|
+
name,
|
|
688
|
+
`export interface ${name} { ${optionalRecord.has}: boolean; ${optionalRecord.value}: ${inner}; }`,
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
return name;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/** One record for a tagged union: the tag as an enum, and every variant's fields. */
|
|
695
|
+
private taggedRecord(type: ts.Type, tagged: TaggedUnion): string {
|
|
696
|
+
const key = `tagged:${this.checker.typeToString(type)}`;
|
|
697
|
+
const known = this.records.get(key);
|
|
698
|
+
if (known) {
|
|
699
|
+
return known;
|
|
700
|
+
}
|
|
701
|
+
const name = this.unique(type.aliasSymbol?.name ?? "Variant");
|
|
702
|
+
this.records.set(key, name);
|
|
703
|
+
const origin = type.aliasSymbol?.declarations?.[0];
|
|
704
|
+
if (origin) {
|
|
705
|
+
this.origins.set(name, origin);
|
|
706
|
+
}
|
|
707
|
+
const tags = tagged.tags;
|
|
708
|
+
const tagEnum: LiteralEnum = {
|
|
709
|
+
name: `${name}Kind`,
|
|
710
|
+
members: tags,
|
|
711
|
+
optional: tagged.optional,
|
|
712
|
+
};
|
|
713
|
+
this.enums.set(enumKey(tags), tagEnum);
|
|
714
|
+
this.writeEnum(tagEnum);
|
|
715
|
+
const fields = new Map<string, string>();
|
|
716
|
+
for (const variant of tagged.variants) {
|
|
717
|
+
for (const property of variant.getProperties()) {
|
|
718
|
+
if (property.name === tagged.tag || fields.has(property.name)) {
|
|
719
|
+
continue;
|
|
720
|
+
}
|
|
721
|
+
fields.set(
|
|
722
|
+
property.name,
|
|
723
|
+
this.declaredText(
|
|
724
|
+
property.valueDeclaration,
|
|
725
|
+
this.checker.getTypeOfSymbol(property),
|
|
726
|
+
),
|
|
727
|
+
);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
this.declarations.set(
|
|
731
|
+
name,
|
|
732
|
+
`export interface ${name} { ${tagged.tag}: ${tagEnum.name}; ${[...fields]
|
|
733
|
+
.map(([field, text]) => `${field}: ${text};`)
|
|
734
|
+
.join(" ")} }`,
|
|
735
|
+
);
|
|
736
|
+
return name;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
/** A record of an object type; captured arrays inside it can give it distinct variants. */
|
|
740
|
+
private record(
|
|
741
|
+
type: ts.Type,
|
|
742
|
+
value: CapturedValue | undefined,
|
|
743
|
+
hint?: string,
|
|
744
|
+
): string {
|
|
745
|
+
const node = value && this.model.node(value);
|
|
746
|
+
const captured = (name: string) =>
|
|
747
|
+
node &&
|
|
748
|
+
(node.captured.kind === "object" || node.captured.kind === "instance")
|
|
749
|
+
? node.captured.fields[name]
|
|
750
|
+
: undefined;
|
|
751
|
+
const body = this.recordBody(type, captured);
|
|
752
|
+
const symbolName = type.aliasSymbol?.name ?? type.getSymbol()?.name;
|
|
753
|
+
// A type literal and an object literal with the same fields are one shape, so values pass between them unconverted.
|
|
754
|
+
const base =
|
|
755
|
+
symbolName === ts.InternalSymbolName.Type ||
|
|
756
|
+
symbolName === ts.InternalSymbolName.Object
|
|
757
|
+
? undefined
|
|
758
|
+
: symbolName;
|
|
759
|
+
const origin = (type.aliasSymbol ?? type.getSymbol())?.declarations?.[0];
|
|
760
|
+
if (
|
|
761
|
+
base === undefined &&
|
|
762
|
+
this.likeKeptLiteral(type, origin, body, captured)
|
|
763
|
+
) {
|
|
764
|
+
return `{ ${body} }`;
|
|
765
|
+
}
|
|
766
|
+
const key = `${base ?? ""}:${body}`;
|
|
767
|
+
const known = this.records.get(key);
|
|
768
|
+
if (known) {
|
|
769
|
+
return known;
|
|
770
|
+
}
|
|
771
|
+
const owner = declaringName(origin?.parent);
|
|
772
|
+
const name = this.declare(
|
|
773
|
+
base ?? hint ?? (owner && pascalCase(owner)) ?? "Fields",
|
|
774
|
+
body,
|
|
775
|
+
origin,
|
|
776
|
+
);
|
|
777
|
+
this.records.set(key, name);
|
|
778
|
+
return name;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
/**
|
|
782
|
+
* Whether an anonymous record is a kept module's type literal or has the same fields as one. Such
|
|
783
|
+
* a literal has no name to import, so the record is written out as a literal, and the PLC dialect
|
|
784
|
+
* makes literals with the same fields one type, which values pass between unconverted.
|
|
785
|
+
*/
|
|
786
|
+
private likeKeptLiteral(
|
|
787
|
+
type: ts.Type,
|
|
788
|
+
origin: ts.Declaration | undefined,
|
|
789
|
+
body: string,
|
|
790
|
+
captured: (name: string) => CapturedValue | undefined,
|
|
791
|
+
): boolean {
|
|
792
|
+
if (
|
|
793
|
+
origin &&
|
|
794
|
+
ts.isTypeLiteralNode(origin) &&
|
|
795
|
+
this.kept(origin.getSourceFile().fileName)
|
|
796
|
+
) {
|
|
797
|
+
return true;
|
|
798
|
+
}
|
|
799
|
+
const names = fieldNames(this.checker, type);
|
|
800
|
+
return this.keptLiterals.some((literal) => {
|
|
801
|
+
const kept = this.checker.getTypeFromTypeNode(literal);
|
|
802
|
+
// Laying out a literal whose fields differ by name could declare what the program never uses.
|
|
803
|
+
return (
|
|
804
|
+
fieldNames(this.checker, kept) === names &&
|
|
805
|
+
this.recordBody(kept, captured) === body
|
|
806
|
+
);
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
/** A record's fields as dialect declarations, each holding the value `captured` gives its name when setup captured one. */
|
|
811
|
+
private recordBody(
|
|
812
|
+
type: ts.Type,
|
|
813
|
+
captured: (name: string) => CapturedValue | undefined,
|
|
814
|
+
): string {
|
|
815
|
+
return this.checker
|
|
816
|
+
.getPropertiesOfType(type)
|
|
817
|
+
.map((property) => {
|
|
818
|
+
if (!plainName(property.name)) {
|
|
819
|
+
throw quotedField(property.valueDeclaration, property.name);
|
|
820
|
+
}
|
|
821
|
+
return `${property.name}: ${this.declaredText(property.valueDeclaration, this.checker.getTypeOfSymbol(property), captured(property.name) ?? this.fieldSample(property), pascalCase(property.name))};`;
|
|
822
|
+
})
|
|
823
|
+
.join(" ");
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* The program's enums with a string member, each refused at its declaration: a PLC enum's members
|
|
829
|
+
* are integers, and a union of string literals is the type the PLC stores as an enum of names.
|
|
830
|
+
*/
|
|
831
|
+
export const stringEnums = (
|
|
832
|
+
sources: readonly ts.SourceFile[],
|
|
833
|
+
checker: ts.TypeChecker,
|
|
834
|
+
): SpecializeError[] =>
|
|
835
|
+
sources.flatMap((source) =>
|
|
836
|
+
source.statements.flatMap((statement) => {
|
|
837
|
+
if (
|
|
838
|
+
!ts.isEnumDeclaration(statement) ||
|
|
839
|
+
!statement.members.some(
|
|
840
|
+
(member) => typeof checker.getConstantValue(member) === "string",
|
|
841
|
+
)
|
|
842
|
+
) {
|
|
843
|
+
return [];
|
|
844
|
+
}
|
|
845
|
+
const values = statement.members.map((member) =>
|
|
846
|
+
checker.getConstantValue(member),
|
|
847
|
+
);
|
|
848
|
+
const union = values.every((value) => typeof value === "string")
|
|
849
|
+
? `, or use the string literal union ${values.map((value) => JSON.stringify(value)).join(" | ")}, which the PLC stores as an enum`
|
|
850
|
+
: "";
|
|
851
|
+
return [
|
|
852
|
+
new SpecializeError(
|
|
853
|
+
statement.name,
|
|
854
|
+
`${statement.name.text} has string members, and PLC enums are integers: give each member a number${union}.`,
|
|
855
|
+
),
|
|
856
|
+
];
|
|
857
|
+
}),
|
|
858
|
+
);
|
|
859
|
+
|
|
860
|
+
/** The top-level names a statement declares. */
|
|
861
|
+
const declaredNames = (statement: ts.Statement): string[] => {
|
|
862
|
+
if (ts.isVariableStatement(statement)) {
|
|
863
|
+
return statement.declarationList.declarations.flatMap((declaration) =>
|
|
864
|
+
ts.isIdentifier(declaration.name) ? [declaration.name.text] : [],
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
if (
|
|
868
|
+
(ts.isTypeAliasDeclaration(statement) ||
|
|
869
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
870
|
+
ts.isEnumDeclaration(statement) ||
|
|
871
|
+
ts.isClassDeclaration(statement) ||
|
|
872
|
+
ts.isFunctionDeclaration(statement)) &&
|
|
873
|
+
statement.name
|
|
874
|
+
) {
|
|
875
|
+
return [statement.name.text];
|
|
876
|
+
}
|
|
877
|
+
return [];
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
/** A record's field names in order, which tell whether two records could have the same fields. */
|
|
881
|
+
const fieldNames = (checker: ts.TypeChecker, type: ts.Type): string =>
|
|
882
|
+
checker
|
|
883
|
+
.getPropertiesOfType(type)
|
|
884
|
+
.map((property) => property.name)
|
|
885
|
+
.join(",");
|
|
886
|
+
|
|
887
|
+
/** Whether setup captured a number with a fractional part. */
|
|
888
|
+
const fractional = (value: CapturedValue | undefined): boolean =>
|
|
889
|
+
value?.kind === "number" && !Number.isInteger(value.value);
|
|
890
|
+
|
|
891
|
+
/** Whether a captured array holds class instances, whose stored layout is each one's state. */
|
|
892
|
+
export const holdsInstances = (model: Model, node: ModelNode): boolean =>
|
|
893
|
+
node.captured.kind === "array" &&
|
|
894
|
+
node.captured.items.some(
|
|
895
|
+
(item) => model.node(item)?.captured.kind === "instance",
|
|
896
|
+
);
|
|
897
|
+
|
|
898
|
+
/** The key and value types of a `Record<K, V>`. */
|
|
899
|
+
export const recordTypes = (
|
|
900
|
+
type: ts.Type,
|
|
901
|
+
): { key: ts.Type; value: ts.Type } | undefined => {
|
|
902
|
+
const [key, value] = type.aliasTypeArguments ?? [];
|
|
903
|
+
return type.aliasSymbol?.name === "Record" && key && value
|
|
904
|
+
? { key, value }
|
|
905
|
+
: undefined;
|
|
906
|
+
};
|
|
907
|
+
|
|
908
|
+
/** A declaration's type annotation, or a routine's result type. */
|
|
909
|
+
const annotation = (
|
|
910
|
+
declaration: ts.Node | undefined,
|
|
911
|
+
): ts.TypeNode | undefined =>
|
|
912
|
+
declaration &&
|
|
913
|
+
(ts.isVariableDeclaration(declaration) ||
|
|
914
|
+
ts.isPropertyDeclaration(declaration) ||
|
|
915
|
+
ts.isPropertySignature(declaration) ||
|
|
916
|
+
ts.isParameter(declaration) ||
|
|
917
|
+
ts.isFunctionLike(declaration))
|
|
918
|
+
? declaration.type
|
|
919
|
+
: undefined;
|
|
920
|
+
|
|
921
|
+
/** The element type an annotation `T[]`, `Array<T>`, `ReadonlyArray<T>` or `FixedArray<T, N>` names. */
|
|
922
|
+
const arrayElement = (
|
|
923
|
+
node: ts.TypeNode | undefined,
|
|
924
|
+
): ts.TypeNode | undefined => {
|
|
925
|
+
if (node && ts.isArrayTypeNode(node)) {
|
|
926
|
+
return node.elementType;
|
|
927
|
+
}
|
|
928
|
+
if (
|
|
929
|
+
node &&
|
|
930
|
+
ts.isTypeReferenceNode(node) &&
|
|
931
|
+
["Array", "ReadonlyArray", "FixedArray"].includes(node.typeName.getText())
|
|
932
|
+
) {
|
|
933
|
+
return node.typeArguments?.[0];
|
|
934
|
+
}
|
|
935
|
+
return undefined;
|
|
936
|
+
};
|
|
937
|
+
|
|
938
|
+
/** The value type an annotation `Record<K, V>` names. */
|
|
939
|
+
const recordValue = (node: ts.TypeNode | undefined): ts.TypeNode | undefined =>
|
|
940
|
+
node &&
|
|
941
|
+
ts.isTypeReferenceNode(node) &&
|
|
942
|
+
node.typeName.getText() === "Record" &&
|
|
943
|
+
node.typeArguments?.length === 2
|
|
944
|
+
? node.typeArguments[1]
|
|
945
|
+
: undefined;
|
|
946
|
+
|
|
947
|
+
/** The IEC type an annotation names, such as `WORD`, or the one `WORD | undefined` holds. */
|
|
948
|
+
const iecTypeName = (node: ts.TypeNode | undefined): string | undefined => {
|
|
949
|
+
if (node && ts.isUnionTypeNode(node)) {
|
|
950
|
+
const [defined, ...others] = node.types.filter(
|
|
951
|
+
(member) =>
|
|
952
|
+
!(
|
|
953
|
+
member.kind === ts.SyntaxKind.UndefinedKeyword ||
|
|
954
|
+
(ts.isLiteralTypeNode(member) &&
|
|
955
|
+
member.literal.kind === ts.SyntaxKind.NullKeyword)
|
|
956
|
+
),
|
|
957
|
+
);
|
|
958
|
+
return others.length ? undefined : iecTypeName(defined);
|
|
959
|
+
}
|
|
960
|
+
if (!node || !ts.isTypeReferenceNode(node)) {
|
|
961
|
+
return undefined;
|
|
962
|
+
}
|
|
963
|
+
const name = node.typeName.getText();
|
|
964
|
+
const [length, ...rest] = node.typeArguments ?? [];
|
|
965
|
+
if (!length) {
|
|
966
|
+
return iecNames.has(name) ? name : undefined;
|
|
967
|
+
}
|
|
968
|
+
// A sized string keeps its capacity, which its resolved type, a plain string, has lost.
|
|
969
|
+
return (name === "STRING" || name === "WSTRING") &&
|
|
970
|
+
!rest.length &&
|
|
971
|
+
ts.isLiteralTypeNode(length) &&
|
|
972
|
+
ts.isNumericLiteral(length.literal)
|
|
973
|
+
? `${name}<${length.literal.text}>`
|
|
974
|
+
: undefined;
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
const isReadonlyArray = (type: ts.Type): boolean =>
|
|
978
|
+
type.getSymbol()?.name === "ReadonlyArray";
|
|
979
|
+
|
|
980
|
+
/** The key a string-literal union's enum is registered under, whatever order its members are written in. */
|
|
981
|
+
const enumKey = (members: readonly string[]): string =>
|
|
982
|
+
[...members].sort().join("|");
|
|
983
|
+
|
|
984
|
+
/** Whether a field's name is one a PLC variable or record field can have: a plain name, or a table's number. */
|
|
985
|
+
export const plainName = (name: string): boolean =>
|
|
986
|
+
/^(\d+|[\p{ID_Start}_$][\p{ID_Continue}$]*)$/u.test(name);
|
|
987
|
+
|
|
988
|
+
/** The refusal of a field whose name had to be quoted, at the field. */
|
|
989
|
+
export const quotedField = (
|
|
990
|
+
declaration: ts.Node | undefined,
|
|
991
|
+
name: string,
|
|
992
|
+
): Error => {
|
|
993
|
+
const plain = name
|
|
994
|
+
.replace(/[^A-Za-z0-9_$]+(.)?/g, (_, next: string | undefined) =>
|
|
995
|
+
next ? next.toUpperCase() : "",
|
|
996
|
+
)
|
|
997
|
+
.replace(/^\d/, "_$&");
|
|
998
|
+
const message = `"${name}" can't name a field the PLC stores, which needs a plain name such as ${plain || "field"}. Rename the field.`;
|
|
999
|
+
return declaration
|
|
1000
|
+
? new SpecializeError(declaration, message)
|
|
1001
|
+
: new Error(message);
|
|
1002
|
+
};
|
|
1003
|
+
|
|
1004
|
+
/** A string literal as an enum member name. */
|
|
1005
|
+
const literalMember = (value: string): string => {
|
|
1006
|
+
const name = pascalCase(value);
|
|
1007
|
+
return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
|
|
1008
|
+
};
|
|
1009
|
+
|
|
1010
|
+
/** `null` and `undefined` are one empty value in the PLC: an optional's Undefined member, or `has` false. */
|
|
1011
|
+
export const isAbsent = (type: ts.Type): boolean =>
|
|
1012
|
+
Boolean(type.flags & (ts.TypeFlags.Undefined | ts.TypeFlags.Null));
|
|
1013
|
+
|
|
1014
|
+
/** A type holding both null and undefined would need two empty values, which the PLC's optionals don't have. */
|
|
1015
|
+
const bothAbsent = (checker: ts.TypeChecker, type: ts.Type): void => {
|
|
1016
|
+
if (
|
|
1017
|
+
type.isUnion() &&
|
|
1018
|
+
type.types.some((part) => part.flags & ts.TypeFlags.Null) &&
|
|
1019
|
+
type.types.some((part) => part.flags & ts.TypeFlags.Undefined)
|
|
1020
|
+
) {
|
|
1021
|
+
throw new Error(
|
|
1022
|
+
`${checker.typeToString(type)}: null and undefined are one empty value in the PLC; use one of them.`,
|
|
1023
|
+
);
|
|
1024
|
+
}
|
|
1025
|
+
};
|