@tsplc/compiler 0.2.17 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { instancedBlock } from "@tsplc/compiler/ir";
|
|
2
|
-
import type { Constant, GeneratedObject, PlcType } from "@tsplc/compiler/ir";
|
|
3
|
-
import { Names } from "./names.ts";
|
|
4
|
-
import { scalarLiteral } from "./numeric.ts";
|
|
5
|
-
|
|
6
|
-
export class CodesysTypes {
|
|
7
|
-
readonly objects: GeneratedObject[] = [];
|
|
8
|
-
private readonly names = new Map<string, string>();
|
|
9
|
-
private readonly fields = new Map<string, Map<string, string>>();
|
|
10
|
-
|
|
11
|
-
constructor(
|
|
12
|
-
private readonly types: ReadonlyMap<string, PlcType>,
|
|
13
|
-
names: Names,
|
|
14
|
-
blockNames: ReadonlyMap<string, string>,
|
|
15
|
-
) {
|
|
16
|
-
// Register every type and member name before following layout dependencies.
|
|
17
|
-
for (const type of types.values()) {
|
|
18
|
-
if (type.kind === "block") {
|
|
19
|
-
const name = blockNames.get(type.block);
|
|
20
|
-
if (!name) throw new Error("Missing verified block name");
|
|
21
|
-
this.names.set(type.id, name);
|
|
22
|
-
}
|
|
23
|
-
if (["record", "array", "enum", "string"].includes(type.kind)) {
|
|
24
|
-
const label = type.id.split(":").at(-1)!;
|
|
25
|
-
if (type.kind === "record" || type.kind === "enum") {
|
|
26
|
-
const prefix = type.kind === "record" ? "ST" : "E";
|
|
27
|
-
this.names.set(type.id, names.allocateObject(prefix, label));
|
|
28
|
-
} else this.names.set(type.id, names.allocate(`T_${label}`));
|
|
29
|
-
}
|
|
30
|
-
if (type.kind === "record" || type.kind === "enum") {
|
|
31
|
-
const names = new Names(),
|
|
32
|
-
fields = new Map<string, string>();
|
|
33
|
-
for (const member of type.kind === "record" ? type.fields : type.members) {
|
|
34
|
-
fields.set(member.name, names.allocate(member.name, true));
|
|
35
|
-
}
|
|
36
|
-
this.fields.set(type.id, fields);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// A block array is declared in place: a function block cannot be a data type's element.
|
|
41
|
-
for (const type of types.values())
|
|
42
|
-
if (type.kind === "array" && instancedBlock(type.id, types))
|
|
43
|
-
this.names.set(
|
|
44
|
-
type.id,
|
|
45
|
-
`ARRAY[${type.lowerBound}..${type.lowerBound + type.length - 1}] OF ${this.name(type.element)}`,
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
// Verified layouts are acyclic. Emit child DUTs first, then their containing DUT.
|
|
49
|
-
const emitted = new Set<string>();
|
|
50
|
-
const emit = (type: PlcType): void => {
|
|
51
|
-
if (instancedBlock(type.id, types) || !this.names.has(type.id) || emitted.has(type.id))
|
|
52
|
-
return;
|
|
53
|
-
if (type.kind === "array") emit(this.type(type.element));
|
|
54
|
-
if (type.kind === "record") {
|
|
55
|
-
for (const field of type.fields) emit(this.type(field.type));
|
|
56
|
-
}
|
|
57
|
-
emitted.add(type.id);
|
|
58
|
-
const name = this.name(type.id);
|
|
59
|
-
let declaration: string;
|
|
60
|
-
if (type.kind === "record") {
|
|
61
|
-
const fields = type.fields
|
|
62
|
-
.map((field) => ` ${this.field(type.id, field.name)} : ${this.name(field.type)};`)
|
|
63
|
-
.join("\n");
|
|
64
|
-
declaration = `TYPE ${name} :
|
|
65
|
-
STRUCT
|
|
66
|
-
${fields}
|
|
67
|
-
END_STRUCT
|
|
68
|
-
END_TYPE
|
|
69
|
-
`;
|
|
70
|
-
} else if (type.kind === "array") {
|
|
71
|
-
// Typegen publishes each key of a `Record<Enum, T>` as its own contract variable.
|
|
72
|
-
declaration = `${type.index ? "{attribute 'tsplc_keyed'}\n" : ""}TYPE ${name} : ARRAY[${type.lowerBound}..${type.lowerBound + type.length - 1}] OF ${this.name(type.element)};
|
|
73
|
-
END_TYPE
|
|
74
|
-
`;
|
|
75
|
-
} else if (type.kind === "string") {
|
|
76
|
-
declaration = `TYPE ${name} : ${type.encoding === "utf8" ? "STRING" : "WSTRING"}(${type.capacity});
|
|
77
|
-
END_TYPE
|
|
78
|
-
`;
|
|
79
|
-
} else if (type.kind === "enum") {
|
|
80
|
-
const members = type.members
|
|
81
|
-
.map((member) => ` ${this.field(type.id, member.name)} := ${member.value}`)
|
|
82
|
-
.join(",\n");
|
|
83
|
-
declaration = `{attribute 'qualified_only'}
|
|
84
|
-
{attribute 'strict'}
|
|
85
|
-
TYPE ${name} : (
|
|
86
|
-
${members}
|
|
87
|
-
) DINT;
|
|
88
|
-
END_TYPE
|
|
89
|
-
`;
|
|
90
|
-
} else throw new Error("Unsupported verified data type");
|
|
91
|
-
this.objects.push({ id: type.id, kind: "dataType", name, declaration });
|
|
92
|
-
};
|
|
93
|
-
for (const type of types.values()) emit(type);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
private type(id: string): PlcType {
|
|
97
|
-
const type = this.types.get(id);
|
|
98
|
-
if (!type) throw new Error("Missing verified type");
|
|
99
|
-
return type;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
name(id: string): string {
|
|
103
|
-
return this.names.get(id) ?? id;
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
field(id: string, name: string): string {
|
|
107
|
-
const field = this.fields.get(id)?.get(name);
|
|
108
|
-
if (!field) throw new Error("Missing verified field");
|
|
109
|
-
return field;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
declarationInitializer(value: Constant, id: string): string {
|
|
113
|
-
const type = this.type(id);
|
|
114
|
-
// Our DUT fields have no custom initializers. CODESYS initializes their
|
|
115
|
-
// numbers/BOOL/strings to zero/false/empty, including nested arrays/records.
|
|
116
|
-
// Keep explicit nonzero enum members and signed floating zero: omission must
|
|
117
|
-
// not change the initial value selected by PLC semantics.
|
|
118
|
-
if ((type.kind === "record" || type.kind === "array") && this.isZero(value)) return "";
|
|
119
|
-
return ` := ${this.literal(value, id)}`;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private isZero(value: Constant): boolean {
|
|
123
|
-
if (value.kind === "bool") return !value.value;
|
|
124
|
-
if (value.kind === "integer") return value.value === "0";
|
|
125
|
-
if (value.kind === "float") return Object.is(value.value, 0);
|
|
126
|
-
if (value.kind === "string") return value.value === "";
|
|
127
|
-
if (value.kind === "array") return value.items.every((item) => this.isZero(item));
|
|
128
|
-
if (value.kind === "record")
|
|
129
|
-
return Object.values(value.fields).every((item) => this.isZero(item));
|
|
130
|
-
// Time/calendar values remain explicit without relying on target defaults.
|
|
131
|
-
return false;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
literal(value: Constant, id: string): string {
|
|
135
|
-
const type = this.type(id);
|
|
136
|
-
if (type.kind === "enum" && value.kind === "integer") {
|
|
137
|
-
const member = type.members.find((member) => member.value === value.value);
|
|
138
|
-
if (!member) throw new Error("Missing verified enum member");
|
|
139
|
-
return `${this.name(id)}.${this.field(id, member.name)}`;
|
|
140
|
-
}
|
|
141
|
-
if (type.kind === "string" && value.kind === "string") {
|
|
142
|
-
const wide = type.encoding === "utf16",
|
|
143
|
-
quote = wide ? '"' : "'";
|
|
144
|
-
let text = "";
|
|
145
|
-
for (const char of value.value) {
|
|
146
|
-
const code = char.charCodeAt(0);
|
|
147
|
-
if (char === "$") text += "$$";
|
|
148
|
-
else if (char === quote) text += `$${char}`;
|
|
149
|
-
else if (code < 32 || code === 127)
|
|
150
|
-
text += `$${code
|
|
151
|
-
.toString(16)
|
|
152
|
-
.toUpperCase()
|
|
153
|
-
.padStart(wide ? 4 : 2, "0")}`;
|
|
154
|
-
else text += char;
|
|
155
|
-
}
|
|
156
|
-
return `${quote}${text}${quote}`;
|
|
157
|
-
}
|
|
158
|
-
if (type.kind === "array" && value.kind === "array") {
|
|
159
|
-
return `[${value.items.map((item) => this.literal(item, type.element)).join(", ")}]`;
|
|
160
|
-
}
|
|
161
|
-
if (type.kind === "record" && value.kind === "record") {
|
|
162
|
-
const fields = type.fields.map((field) => {
|
|
163
|
-
const initial = value.fields[field.name];
|
|
164
|
-
if (!initial) throw new Error("Missing verified record initializer");
|
|
165
|
-
return `${this.field(id, field.name)} := ${this.literal(initial, field.type)}`;
|
|
166
|
-
});
|
|
167
|
-
return `(${fields.join(", ")})`;
|
|
168
|
-
}
|
|
169
|
-
if (type.kind === "duration" && value.kind === "duration") {
|
|
170
|
-
return `T#${BigInt(value.nanoseconds) / 1_000_000n}ms`;
|
|
171
|
-
}
|
|
172
|
-
if (type.kind === "calendar" && value.kind === "calendar") {
|
|
173
|
-
if (type.domain === "date") return `D#${value.value}`;
|
|
174
|
-
if (type.domain === "timeOfDay") return `TOD#${value.value}`;
|
|
175
|
-
return `DT#${value.value.replace("T", "-")}`;
|
|
176
|
-
}
|
|
177
|
-
return scalarLiteral(value, id);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import type { Diagnostic, EmitResult } from "@tsplc/compiler/ir";
|
|
2
|
-
import { CodesysCompilerError } from "@tsplc/build";
|
|
3
|
-
import type { SourceMapping } from "./emit.ts";
|
|
4
|
-
|
|
5
|
-
type MappedBundle = Pick<EmitResult, "objects"> & { mappings: readonly SourceMapping[] };
|
|
6
|
-
|
|
7
|
-
/** Map verified vendor locations; preserve raw context for everything else. */
|
|
8
|
-
export function diagnoseCodesys(cause: unknown, bundle: MappedBundle): readonly Diagnostic[] {
|
|
9
|
-
const notes: string[] = [];
|
|
10
|
-
let current = cause;
|
|
11
|
-
while (current instanceof Error) {
|
|
12
|
-
if (current instanceof CodesysCompilerError && current.diagnostics.length) {
|
|
13
|
-
return current.diagnostics.map((item) => {
|
|
14
|
-
// Observed in CODESYS 3.5 SP22. position itself is an opaque vendor value.
|
|
15
|
-
const location =
|
|
16
|
-
item.positionText &&
|
|
17
|
-
/^Line ([1-9][0-9]*)(?:, Column ([1-9][0-9]*))? \((Decl|Impl)\)$/.exec(item.positionText);
|
|
18
|
-
const identity = item.object;
|
|
19
|
-
let objectPath: string | undefined;
|
|
20
|
-
if (identity && identity.path.at(-1) === identity.name) {
|
|
21
|
-
const relative = identity.applicationPath;
|
|
22
|
-
if (
|
|
23
|
-
Array.isArray(relative) &&
|
|
24
|
-
relative.length > 0 &&
|
|
25
|
-
relative.every((part) => typeof part === "string") &&
|
|
26
|
-
identity.isApplicationChild === (relative.length === 1) &&
|
|
27
|
-
identity.path.length > relative.length &&
|
|
28
|
-
identity.path.slice(-relative.length).every((part, index) => part === relative[index])
|
|
29
|
-
) {
|
|
30
|
-
objectPath = relative.join("/");
|
|
31
|
-
} else if (
|
|
32
|
-
relative === undefined &&
|
|
33
|
-
identity.isApplicationChild === true &&
|
|
34
|
-
identity.path.length >= 2
|
|
35
|
-
) {
|
|
36
|
-
// A verified immediate Application parent proves the root identity.
|
|
37
|
-
objectPath = identity.name;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
const objects = objectPath
|
|
41
|
-
? bundle.objects.flatMap((object) => {
|
|
42
|
-
const declaration =
|
|
43
|
-
object.sourcePath ??
|
|
44
|
-
(object.implementation === undefined
|
|
45
|
-
? `${object.name}.st`
|
|
46
|
-
: `${object.name}/declaration.st`);
|
|
47
|
-
const nativePath = (
|
|
48
|
-
object.kind === "method"
|
|
49
|
-
? declaration.replace(/\/methods\/([^/]+\/declaration\.st)$/, "/$1")
|
|
50
|
-
: declaration
|
|
51
|
-
).replace(object.implementation === undefined ? /\.st$/ : /\/declaration\.st$/, "");
|
|
52
|
-
return object.name === identity?.name && nativePath === objectPath
|
|
53
|
-
? [{ object, declaration }]
|
|
54
|
-
: [];
|
|
55
|
-
})
|
|
56
|
-
: [];
|
|
57
|
-
const match = objects.length === 1 ? objects[0] : undefined;
|
|
58
|
-
const section = location?.[3] === "Decl" ? "declaration" : "implementation";
|
|
59
|
-
const path =
|
|
60
|
-
section === "declaration"
|
|
61
|
-
? match?.declaration
|
|
62
|
-
: match?.object.implementation !== undefined
|
|
63
|
-
? match.declaration.replace(/declaration\.st$/, "implementation.st")
|
|
64
|
-
: undefined;
|
|
65
|
-
const mappings =
|
|
66
|
-
location && path
|
|
67
|
-
? bundle.mappings.filter(
|
|
68
|
-
(mapping) => mapping.path === path && mapping.line === Number(location[1]),
|
|
69
|
-
)
|
|
70
|
-
: [];
|
|
71
|
-
const mapping = mappings.length === 1 ? mappings[0] : undefined;
|
|
72
|
-
return {
|
|
73
|
-
origin: "vendor",
|
|
74
|
-
code: "PLC8001",
|
|
75
|
-
severity: "error",
|
|
76
|
-
message: item.text,
|
|
77
|
-
...(mapping && { span: mapping.span }),
|
|
78
|
-
notes: [
|
|
79
|
-
...notes,
|
|
80
|
-
...(location && path
|
|
81
|
-
? [
|
|
82
|
-
"Generated ST: " +
|
|
83
|
-
path +
|
|
84
|
-
":" +
|
|
85
|
-
location[1] +
|
|
86
|
-
(location[2] ? `:${location[2]}` : ""),
|
|
87
|
-
]
|
|
88
|
-
: []),
|
|
89
|
-
`CODESYS diagnostic: ${JSON.stringify(item)}`,
|
|
90
|
-
],
|
|
91
|
-
};
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
notes.push(current.message);
|
|
95
|
-
if (current.cause === undefined) break;
|
|
96
|
-
current = current.cause;
|
|
97
|
-
if (!(current instanceof Error)) notes.push(String(current));
|
|
98
|
-
}
|
|
99
|
-
return [
|
|
100
|
-
{
|
|
101
|
-
origin: "vendor",
|
|
102
|
-
code: "PLC8001",
|
|
103
|
-
severity: "error",
|
|
104
|
-
message: cause instanceof Error ? cause.message : String(cause),
|
|
105
|
-
...(notes.length > 1 && { notes: notes.slice(1) }),
|
|
106
|
-
},
|
|
107
|
-
];
|
|
108
|
-
}
|
package/ir/aggregates.ts
DELETED
|
@@ -1,333 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
comparisonOperators,
|
|
3
|
-
type Constant,
|
|
4
|
-
type Expression,
|
|
5
|
-
type Variable,
|
|
6
|
-
type Place,
|
|
7
|
-
} from "./model.ts";
|
|
8
|
-
import { constantFits, interval, scalarIssue, type IntegerInterval } from "./scalars.ts";
|
|
9
|
-
import { domainConstantFits, domainIssue } from "./domains.ts";
|
|
10
|
-
import { primitiveTypes, type PlcType } from "./types.ts";
|
|
11
|
-
|
|
12
|
-
export function aggregate(type: PlcType | undefined): boolean {
|
|
13
|
-
return type?.kind === "record" || type?.kind === "array";
|
|
14
|
-
}
|
|
15
|
-
export function valueFits(
|
|
16
|
-
value: Constant,
|
|
17
|
-
type: PlcType,
|
|
18
|
-
types: ReadonlyMap<string, PlcType>,
|
|
19
|
-
): boolean {
|
|
20
|
-
if (type.kind === "duration" || type.kind === "calendar") return domainConstantFits(value, type);
|
|
21
|
-
if (type.kind === "enum")
|
|
22
|
-
return value.kind === "integer" && type.members.some((member) => member.value === value.value);
|
|
23
|
-
if (type.kind === "string")
|
|
24
|
-
return (
|
|
25
|
-
value.kind === "string" &&
|
|
26
|
-
!value.value.includes("\0") &&
|
|
27
|
-
(type.encoding === "utf8"
|
|
28
|
-
? new TextEncoder().encode(value.value).length <= type.capacity
|
|
29
|
-
: value.value.length <= type.capacity)
|
|
30
|
-
);
|
|
31
|
-
if (type.kind === "array")
|
|
32
|
-
return (
|
|
33
|
-
value.kind === "array" &&
|
|
34
|
-
value.items.length === type.length &&
|
|
35
|
-
value.items.every((item) => valueFits(item, types.get(type.element)!, types))
|
|
36
|
-
);
|
|
37
|
-
if (type.kind === "record")
|
|
38
|
-
return (
|
|
39
|
-
value.kind === "record" &&
|
|
40
|
-
Object.keys(value.fields).length === type.fields.length &&
|
|
41
|
-
type.fields.every(
|
|
42
|
-
(field) =>
|
|
43
|
-
Object.hasOwn(value.fields, field.name) &&
|
|
44
|
-
valueFits(value.fields[field.name]!, types.get(field.type)!, types),
|
|
45
|
-
)
|
|
46
|
-
);
|
|
47
|
-
return constantFits(value, type);
|
|
48
|
-
}
|
|
49
|
-
export function defaultValue(typeId: string, types: ReadonlyMap<string, PlcType>): Constant {
|
|
50
|
-
const type = types.get(typeId);
|
|
51
|
-
if (!type) throw new Error("Unknown default-value type");
|
|
52
|
-
if (type.kind === "bool") return { kind: "bool", value: false };
|
|
53
|
-
if (type.kind === "float") return { kind: "float", value: 0 };
|
|
54
|
-
if (type.kind === "string") return { kind: "string", value: "" };
|
|
55
|
-
if (type.kind === "enum")
|
|
56
|
-
return {
|
|
57
|
-
kind: "integer",
|
|
58
|
-
value: type.members.find((member) => member.value === "0")?.value ?? type.members[0]!.value,
|
|
59
|
-
};
|
|
60
|
-
if (type.kind === "array")
|
|
61
|
-
return {
|
|
62
|
-
kind: "array",
|
|
63
|
-
items: Array.from({ length: type.length }, () => defaultValue(type.element, types)),
|
|
64
|
-
};
|
|
65
|
-
if (type.kind === "record")
|
|
66
|
-
return {
|
|
67
|
-
kind: "record",
|
|
68
|
-
fields: Object.fromEntries(
|
|
69
|
-
type.fields.map((field) => [field.name, defaultValue(field.type, types)]),
|
|
70
|
-
),
|
|
71
|
-
};
|
|
72
|
-
if (type.kind === "duration") return { kind: "duration", nanoseconds: "0" };
|
|
73
|
-
if (type.kind === "calendar")
|
|
74
|
-
return {
|
|
75
|
-
kind: "calendar",
|
|
76
|
-
value:
|
|
77
|
-
type.domain === "date"
|
|
78
|
-
? "1970-01-01"
|
|
79
|
-
: type.domain === "timeOfDay"
|
|
80
|
-
? "00:00:00.000"
|
|
81
|
-
: "1970-01-01T00:00:00",
|
|
82
|
-
};
|
|
83
|
-
if (type.kind === "integer" || type.kind === "bits") return { kind: "integer", value: "0" };
|
|
84
|
-
throw new Error("Unsupported default-value type");
|
|
85
|
-
}
|
|
86
|
-
export function placeType(
|
|
87
|
-
place: Place,
|
|
88
|
-
types: ReadonlyMap<string, PlcType>,
|
|
89
|
-
variables: ReadonlyMap<string, Variable>,
|
|
90
|
-
ranges: ReadonlyMap<string, IntegerInterval> = new Map(),
|
|
91
|
-
writable = false,
|
|
92
|
-
): { type?: string; issue?: string } {
|
|
93
|
-
const root = variables.get(place.root);
|
|
94
|
-
if (!root) return { issue: "Unknown storage root." };
|
|
95
|
-
if (
|
|
96
|
-
writable &&
|
|
97
|
-
(root.storage === "input" ||
|
|
98
|
-
root.storage === "constant" ||
|
|
99
|
-
root.storage === "blockOutput" ||
|
|
100
|
-
(root.storage === "parameter" && root.mode !== "inOut") ||
|
|
101
|
-
(!place.path.length && !root.mutable))
|
|
102
|
-
)
|
|
103
|
-
return { issue: "This storage is read-only." };
|
|
104
|
-
let id = root.type;
|
|
105
|
-
for (const segment of place.path) {
|
|
106
|
-
const type = types.get(id);
|
|
107
|
-
if (segment.kind === "field") {
|
|
108
|
-
if (type?.kind === "block" && segment.name === "out" && type.output) {
|
|
109
|
-
if (writable) return { issue: "Function block output views are read-only." };
|
|
110
|
-
id = type.output;
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
if (type?.kind !== "record") return { issue: "Field access requires a record." };
|
|
114
|
-
const field = type.fields.find((field) => field.name === segment.name);
|
|
115
|
-
if (!field) return { issue: "Unknown record field." };
|
|
116
|
-
if (writable && field.readonly) return { issue: "This field is read-only." };
|
|
117
|
-
id = field.type;
|
|
118
|
-
} else {
|
|
119
|
-
if (type?.kind !== "array") return { issue: "Index access requires a fixed array." };
|
|
120
|
-
// Enum values are closed, and the layout check below proves its members are the bounds.
|
|
121
|
-
// An integer, such as a for…of counter, still needs its bounds proof.
|
|
122
|
-
if (type.index && segment.value.type === type.index) {
|
|
123
|
-
id = type.element;
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
const indexType = types.get(segment.value.type),
|
|
127
|
-
bounds = interval(segment.value, types, variables, ranges);
|
|
128
|
-
if (
|
|
129
|
-
indexType?.kind !== "integer" ||
|
|
130
|
-
!bounds ||
|
|
131
|
-
bounds[0] < BigInt(type.lowerBound) ||
|
|
132
|
-
bounds[1] >= BigInt(type.lowerBound + type.length)
|
|
133
|
-
)
|
|
134
|
-
return { issue: "Array index must be proved within the declared bounds." };
|
|
135
|
-
id = type.element;
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
return { type: id };
|
|
139
|
-
}
|
|
140
|
-
export function aggregateIssue(
|
|
141
|
-
value: Expression,
|
|
142
|
-
types: ReadonlyMap<string, PlcType>,
|
|
143
|
-
): string | undefined {
|
|
144
|
-
const type = types.get(value.type);
|
|
145
|
-
if (!type) return "Unknown expression type.";
|
|
146
|
-
if (value.kind === "constant")
|
|
147
|
-
return valueFits(value.value, type, types)
|
|
148
|
-
? undefined
|
|
149
|
-
: "Constant does not fit its closed PLC layout.";
|
|
150
|
-
if (value.kind === "record")
|
|
151
|
-
return type.kind === "record" &&
|
|
152
|
-
Object.keys(value.fields).length === type.fields.length &&
|
|
153
|
-
type.fields.every(
|
|
154
|
-
(field) =>
|
|
155
|
-
Object.hasOwn(value.fields, field.name) && value.fields[field.name]?.type === field.type,
|
|
156
|
-
)
|
|
157
|
-
? undefined
|
|
158
|
-
: "Record expression differs from its layout.";
|
|
159
|
-
if (value.kind === "array")
|
|
160
|
-
return type.kind === "array" &&
|
|
161
|
-
value.items.length === type.length &&
|
|
162
|
-
value.items.every((item) => item.type === type.element)
|
|
163
|
-
? undefined
|
|
164
|
-
: "Array expression differs from its layout.";
|
|
165
|
-
if (value.kind === "copy")
|
|
166
|
-
return aggregate(type) && value.operand.type === value.type
|
|
167
|
-
? undefined
|
|
168
|
-
: "Copy requires one matching fixed aggregate.";
|
|
169
|
-
if (value.kind === "convert") {
|
|
170
|
-
const source = types.get(value.operand.type);
|
|
171
|
-
return type.kind === "string" &&
|
|
172
|
-
source?.kind === "string" &&
|
|
173
|
-
source.encoding === type.encoding &&
|
|
174
|
-
source.capacity <= type.capacity &&
|
|
175
|
-
value.policy === "exact"
|
|
176
|
-
? undefined
|
|
177
|
-
: "Unsupported aggregate/domain conversion.";
|
|
178
|
-
}
|
|
179
|
-
if (value.kind === "load" || value.kind === "call" || value.kind === "conditional")
|
|
180
|
-
return undefined;
|
|
181
|
-
if (value.kind === "binary" && comparisonOperators.has(value.operator)) {
|
|
182
|
-
const left = types.get(value.left.type);
|
|
183
|
-
return value.type === "BOOL" &&
|
|
184
|
-
left &&
|
|
185
|
-
["enum", "string"].includes(left.kind) &&
|
|
186
|
-
value.left.type === value.right.type &&
|
|
187
|
-
(left.kind !== "enum" || ["equal", "notEqual"].includes(value.operator))
|
|
188
|
-
? undefined
|
|
189
|
-
: "Comparison requires compatible scalar domains; object identity is unsupported.";
|
|
190
|
-
}
|
|
191
|
-
return "Operation is unsupported for this PLC domain.";
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// Route a reduced expression to the checker that owns its PLC domain. A binary
|
|
195
|
-
// operation belongs to its operand domain, e.g. a TIME comparison yields BOOL.
|
|
196
|
-
export function expressionIssue(
|
|
197
|
-
value: Expression,
|
|
198
|
-
types: ReadonlyMap<string, PlcType>,
|
|
199
|
-
variables: ReadonlyMap<string, Variable>,
|
|
200
|
-
ranges: ReadonlyMap<string, IntegerInterval>,
|
|
201
|
-
): string | undefined {
|
|
202
|
-
const kinds = [value.type, ...(value.kind === "binary" ? [value.left.type] : [])].map(
|
|
203
|
-
(id) => types.get(id)?.kind ?? "",
|
|
204
|
-
);
|
|
205
|
-
if (kinds.some((kind) => ["duration", "calendar"].includes(kind)))
|
|
206
|
-
return domainIssue(value, types);
|
|
207
|
-
if (
|
|
208
|
-
["copy", "record", "array"].includes(value.kind) ||
|
|
209
|
-
kinds.some((kind) => ["record", "array", "enum", "string"].includes(kind))
|
|
210
|
-
)
|
|
211
|
-
return aggregateIssue(value, types);
|
|
212
|
-
return scalarIssue(value, types, variables, ranges);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Validate all layout edges once before constants or places can traverse them.
|
|
216
|
-
export function layoutIssue(types: ReadonlyMap<string, PlcType>): string | undefined {
|
|
217
|
-
for (const type of types.values()) {
|
|
218
|
-
const canonical = primitiveTypes.find((item) => item.id === type.id);
|
|
219
|
-
if (canonical) {
|
|
220
|
-
if (
|
|
221
|
-
type.kind !== canonical.kind ||
|
|
222
|
-
("bits" in canonical && (!("bits" in type) || type.bits !== canonical.bits)) ||
|
|
223
|
-
("signed" in canonical && (!("signed" in type) || type.signed !== canonical.signed)) ||
|
|
224
|
-
("domain" in canonical && (!("domain" in type) || type.domain !== canonical.domain))
|
|
225
|
-
)
|
|
226
|
-
return `Conflicting primitive type ${type.id}`;
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
if (type.kind === "record") {
|
|
230
|
-
if (
|
|
231
|
-
!type.fields.length ||
|
|
232
|
-
new Set(type.fields.map((field) => field.name)).size !== type.fields.length ||
|
|
233
|
-
type.fields.some(
|
|
234
|
-
(field) =>
|
|
235
|
-
!field.name ||
|
|
236
|
-
typeof field.readonly !== "boolean" ||
|
|
237
|
-
(field.mode && field.mode !== "inOut") ||
|
|
238
|
-
!types.has(field.type),
|
|
239
|
-
)
|
|
240
|
-
)
|
|
241
|
-
return `Invalid closed record layout ${type.id}`;
|
|
242
|
-
} else if (type.kind === "array") {
|
|
243
|
-
if (
|
|
244
|
-
!types.has(type.element) ||
|
|
245
|
-
!Number.isSafeInteger(type.length) ||
|
|
246
|
-
type.length < 1 ||
|
|
247
|
-
type.length > 1_000_000 ||
|
|
248
|
-
!Number.isInteger(type.lowerBound) ||
|
|
249
|
-
type.lowerBound < -2147483648 ||
|
|
250
|
-
type.lowerBound + type.length - 1 > 2147483647
|
|
251
|
-
)
|
|
252
|
-
return `Invalid fixed array layout ${type.id}`;
|
|
253
|
-
if (type.index !== undefined) {
|
|
254
|
-
const index = types.get(type.index);
|
|
255
|
-
if (
|
|
256
|
-
index?.kind !== "enum" ||
|
|
257
|
-
index.members.length !== type.length ||
|
|
258
|
-
index.members.some(
|
|
259
|
-
(member) =>
|
|
260
|
-
Number(member.value) < type.lowerBound ||
|
|
261
|
-
Number(member.value) >= type.lowerBound + type.length,
|
|
262
|
-
)
|
|
263
|
-
)
|
|
264
|
-
return `Invalid enum-keyed array layout ${type.id}`;
|
|
265
|
-
}
|
|
266
|
-
} else if (type.kind === "enum") {
|
|
267
|
-
if (
|
|
268
|
-
type.base !== "DINT" ||
|
|
269
|
-
types.get(type.base)?.kind !== "integer" ||
|
|
270
|
-
!type.members.length ||
|
|
271
|
-
new Set(type.members.map((member) => member.name)).size !== type.members.length ||
|
|
272
|
-
new Set(type.members.map((member) => member.value)).size !== type.members.length ||
|
|
273
|
-
type.members.some(
|
|
274
|
-
(member) =>
|
|
275
|
-
!member.name ||
|
|
276
|
-
!constantFits({ kind: "integer", value: member.value }, types.get(type.base)!),
|
|
277
|
-
)
|
|
278
|
-
)
|
|
279
|
-
return `Invalid enum layout ${type.id}`;
|
|
280
|
-
} else if (type.kind === "string") {
|
|
281
|
-
if (
|
|
282
|
-
!Number.isInteger(type.capacity) ||
|
|
283
|
-
type.capacity < 1 ||
|
|
284
|
-
type.capacity > 65535 ||
|
|
285
|
-
!["utf8", "utf16"].includes(type.encoding)
|
|
286
|
-
)
|
|
287
|
-
return `Invalid bounded string layout ${type.id}`;
|
|
288
|
-
} else if (type.kind === "block") {
|
|
289
|
-
if (!type.block || (type.output && types.get(type.output)?.kind !== "record"))
|
|
290
|
-
return `Invalid block type ${type.id}`;
|
|
291
|
-
} else return `Unsupported core type definition ${type.id}`;
|
|
292
|
-
}
|
|
293
|
-
const active = new Set<string>(),
|
|
294
|
-
layouts = new Map<string, { size: number; height: number }>();
|
|
295
|
-
const count = (id: string, depth: number): { size: number; height: number } => {
|
|
296
|
-
if (active.has(id) || depth > 64)
|
|
297
|
-
throw new Error(`Recursive or excessively nested by-value layout ${id}`);
|
|
298
|
-
const cached = layouts.get(id);
|
|
299
|
-
if (cached) {
|
|
300
|
-
if (depth + cached.height > 64) throw new Error(`Layout nesting exceeds 64 levels ${id}`);
|
|
301
|
-
return cached;
|
|
302
|
-
}
|
|
303
|
-
active.add(id);
|
|
304
|
-
const type = types.get(id)!;
|
|
305
|
-
const children =
|
|
306
|
-
type.kind === "array"
|
|
307
|
-
? [count(type.element, depth + 1)]
|
|
308
|
-
: type.kind === "record"
|
|
309
|
-
? type.fields.map((field) => count(field.type, depth + 1))
|
|
310
|
-
: [];
|
|
311
|
-
const size =
|
|
312
|
-
type.kind === "array"
|
|
313
|
-
? type.length * children[0]!.size
|
|
314
|
-
: type.kind === "record"
|
|
315
|
-
? children.reduce((sum, child) => sum + child.size, 0)
|
|
316
|
-
: 1;
|
|
317
|
-
const height = children.length
|
|
318
|
-
? 1 + children.reduce((maximum, child) => Math.max(maximum, child.height), 0)
|
|
319
|
-
: 0;
|
|
320
|
-
active.delete(id);
|
|
321
|
-
if (size > 1_000_000) throw new Error(`Layout exceeds one million scalar elements ${id}`);
|
|
322
|
-
if (depth + height > 64) throw new Error(`Layout nesting exceeds 64 levels ${id}`);
|
|
323
|
-
const layout = { size, height };
|
|
324
|
-
layouts.set(id, layout);
|
|
325
|
-
return layout;
|
|
326
|
-
};
|
|
327
|
-
try {
|
|
328
|
-
for (const id of types.keys()) count(id, 0);
|
|
329
|
-
} catch (cause) {
|
|
330
|
-
return cause instanceof Error ? cause.message : "Invalid layout";
|
|
331
|
-
}
|
|
332
|
-
return undefined;
|
|
333
|
-
}
|