@tsplc/compiler 0.2.16 → 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
package/src/frontend/blocks.ts
CHANGED
|
@@ -1,419 +1,553 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
aggregate,
|
|
4
|
+
defaultValue,
|
|
5
|
+
instancedBlock,
|
|
6
|
+
standardBlocks,
|
|
7
|
+
type StandardBlock,
|
|
8
|
+
type FunctionBlock,
|
|
9
|
+
type Variable,
|
|
10
|
+
type SourceSpan,
|
|
11
|
+
type PlcType,
|
|
12
12
|
} from "@tsplc/compiler/ir";
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
calleeName,
|
|
15
|
+
numericLiteralType,
|
|
16
|
+
type Fail,
|
|
17
|
+
type TypeResolver,
|
|
18
|
+
} from "./types.ts";
|
|
14
19
|
|
|
15
20
|
export interface BlockSource {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
block: FunctionBlock;
|
|
22
|
+
method: ts.MethodDeclaration;
|
|
23
|
+
methods: readonly ts.MethodDeclaration[];
|
|
24
|
+
inputSymbol: ts.Symbol;
|
|
25
|
+
inputBindings: ReadonlyMap<string, Variable>;
|
|
26
|
+
fieldBindings: ReadonlyMap<ts.Symbol, Variable>;
|
|
27
|
+
initializers: ReadonlyMap<Variable, ts.Expression>;
|
|
23
28
|
}
|
|
24
29
|
export function storageInitializer(
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
30
|
+
node: ts.Expression,
|
|
31
|
+
types: TypeResolver,
|
|
32
|
+
fail: Fail,
|
|
28
33
|
): { value: ts.Expression; retain: boolean } {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
if (!ts.isCallExpression(node)) {
|
|
35
|
+
return { value: node, retain: false };
|
|
36
|
+
}
|
|
37
|
+
const callee = calleeName(node.expression);
|
|
38
|
+
if (!callee || types.sdkName(callee) !== "retain") {
|
|
39
|
+
return { value: node, retain: false };
|
|
40
|
+
}
|
|
41
|
+
if (node.arguments.length !== 1 || (node.typeArguments?.length ?? 0) > 1) {
|
|
42
|
+
fail(node, "PLC3004", "retain requires one static storage initializer.");
|
|
43
|
+
}
|
|
44
|
+
return { value: node.arguments[0]!, retain: true };
|
|
35
45
|
}
|
|
36
46
|
|
|
37
47
|
export class BlockResolver {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
48
|
+
readonly definitions = new Map<string, FunctionBlock>();
|
|
49
|
+
readonly sources = new Map<ts.Symbol, BlockSource>();
|
|
50
|
+
private readonly declarations = new Map<ts.Symbol, ts.ClassDeclaration>();
|
|
51
|
+
private readonly active = new Set<ts.Symbol>();
|
|
52
|
+
constructor(
|
|
53
|
+
private readonly types: TypeResolver,
|
|
54
|
+
private readonly origin: (node: ts.Node) => SourceSpan,
|
|
55
|
+
private readonly fail: Fail,
|
|
56
|
+
sdkSpan: SourceSpan,
|
|
57
|
+
) {
|
|
58
|
+
for (const name of Object.keys(standardBlocks) as StandardBlock[]) {
|
|
59
|
+
const schema = standardBlocks[name];
|
|
60
|
+
const id = `stdlib:${name}`;
|
|
61
|
+
const output = `${id}:out`;
|
|
62
|
+
const variable = (
|
|
63
|
+
label: string,
|
|
64
|
+
type: string,
|
|
65
|
+
storage: Variable["storage"],
|
|
66
|
+
): Variable => ({
|
|
67
|
+
id: `${id}:${label}`,
|
|
68
|
+
name: label,
|
|
69
|
+
type,
|
|
70
|
+
storage,
|
|
71
|
+
mutable: false,
|
|
72
|
+
retain: false,
|
|
73
|
+
span: sdkSpan,
|
|
74
|
+
...(storage === "blockOutput" && {
|
|
75
|
+
initial: defaultValue(type, types.types),
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
78
|
+
const block: FunctionBlock = {
|
|
79
|
+
id,
|
|
80
|
+
name,
|
|
81
|
+
inputs: schema.inputs.map(([label, , type]) =>
|
|
82
|
+
variable(label, type, "parameter"),
|
|
83
|
+
),
|
|
84
|
+
outputs: schema.outputs.map(([label, , type]) =>
|
|
85
|
+
variable(label, type, "blockOutput"),
|
|
86
|
+
),
|
|
87
|
+
state: [],
|
|
88
|
+
locals: [],
|
|
89
|
+
body: [],
|
|
90
|
+
result: output,
|
|
91
|
+
standard: name,
|
|
92
|
+
span: sdkSpan,
|
|
93
|
+
};
|
|
94
|
+
this.definitions.set(id, block);
|
|
95
|
+
types.types.set(output, {
|
|
96
|
+
id: output,
|
|
97
|
+
kind: "record",
|
|
98
|
+
fields: block.outputs.map((field) => ({
|
|
99
|
+
name: field.name,
|
|
100
|
+
type: field.type,
|
|
101
|
+
readonly: true,
|
|
102
|
+
})),
|
|
103
|
+
});
|
|
104
|
+
const type: PlcType = {
|
|
105
|
+
id: `block:${id}`,
|
|
106
|
+
kind: "block",
|
|
107
|
+
block: id,
|
|
108
|
+
output,
|
|
109
|
+
};
|
|
110
|
+
types.types.set(type.id, type);
|
|
111
|
+
types.standardTypes.set(name, type.id);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
89
114
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
115
|
+
index(nodes: readonly ts.ClassDeclaration[]): void {
|
|
116
|
+
for (const node of nodes) {
|
|
117
|
+
if (!node.name) {
|
|
118
|
+
this.fail(node, "PLC1001", "Function block classes need names.");
|
|
119
|
+
}
|
|
120
|
+
this.declarations.set(this.types.symbol(node.name), node);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
96
123
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
124
|
+
instanceType(node: ts.NewExpression): string {
|
|
125
|
+
const name = calleeName(node.expression);
|
|
126
|
+
if (node.arguments?.length || node.typeArguments?.length || !name) {
|
|
127
|
+
this.fail(
|
|
128
|
+
node,
|
|
129
|
+
"PLC1007",
|
|
130
|
+
"Block instances use a concrete zero-argument constructor at static storage declarations.",
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
const official = this.types.sdkName(name);
|
|
134
|
+
const standard = official && this.types.standardTypes.get(official);
|
|
135
|
+
if (standard) {
|
|
136
|
+
return standard;
|
|
137
|
+
}
|
|
138
|
+
const symbol = this.types.symbol(name);
|
|
139
|
+
const declaration = this.declarations.get(symbol);
|
|
140
|
+
if (!declaration) {
|
|
141
|
+
this.fail(
|
|
142
|
+
node,
|
|
143
|
+
"PLC1007",
|
|
144
|
+
"Only recognized static function block allocation is supported.",
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
this.collect(declaration);
|
|
148
|
+
// Successful collection registers this class identity before publishing its source.
|
|
149
|
+
return this.types.blockTypes.get(symbol)!;
|
|
150
|
+
}
|
|
116
151
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
152
|
+
/**
|
|
153
|
+
* `FixedArray<Block, N>` storage allocated with `defaultValue`; its elements are static
|
|
154
|
+
* instances. The allocation names the type, so an annotation is optional but must agree.
|
|
155
|
+
*/
|
|
156
|
+
instanceArrayType(
|
|
157
|
+
annotation: ts.TypeNode | undefined,
|
|
158
|
+
value: ts.Expression,
|
|
159
|
+
): string | undefined {
|
|
160
|
+
const blockArray = (type: string) =>
|
|
161
|
+
this.types.types.get(type)?.kind === "array" &&
|
|
162
|
+
instancedBlock(type, this.types.types)
|
|
163
|
+
? type
|
|
164
|
+
: undefined;
|
|
165
|
+
const declared = annotation && this.types.resolve(annotation).type;
|
|
166
|
+
if (declared && !blockArray(declared)) {
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
const callee = ts.isCallExpression(value) && calleeName(value.expression);
|
|
170
|
+
const allocation =
|
|
171
|
+
callee &&
|
|
172
|
+
this.types.sdkName(callee) === "defaultValue" &&
|
|
173
|
+
value.typeArguments?.length === 1
|
|
174
|
+
? this.types.resolve(value.typeArguments[0]!).type
|
|
175
|
+
: undefined;
|
|
176
|
+
if (!declared) {
|
|
177
|
+
return allocation && blockArray(allocation);
|
|
178
|
+
}
|
|
179
|
+
if (allocation !== declared) {
|
|
180
|
+
this.fail(
|
|
181
|
+
value,
|
|
182
|
+
"PLC1007",
|
|
183
|
+
"Block arrays allocate with defaultValue<FixedArray<Block, N>>() of their declared type.",
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return declared;
|
|
187
|
+
}
|
|
142
188
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
189
|
+
collect(node: ts.ClassDeclaration): void {
|
|
190
|
+
if (!node.name) {
|
|
191
|
+
this.fail(node, "PLC1001", "Function block classes need names.");
|
|
192
|
+
}
|
|
193
|
+
const symbol = this.types.symbol(node.name);
|
|
194
|
+
if (this.sources.has(symbol)) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (this.active.has(symbol) || this.active.size >= 64) {
|
|
198
|
+
this.fail(
|
|
199
|
+
node,
|
|
200
|
+
"PLC1007",
|
|
201
|
+
"Recursive or excessively nested block instance layout.",
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
if (
|
|
205
|
+
node.typeParameters?.length ||
|
|
206
|
+
node.modifiers?.some(
|
|
207
|
+
(modifier) =>
|
|
208
|
+
modifier.kind === ts.SyntaxKind.Decorator ||
|
|
209
|
+
modifier.kind === ts.SyntaxKind.AbstractKeyword,
|
|
210
|
+
)
|
|
211
|
+
) {
|
|
212
|
+
this.fail(
|
|
213
|
+
node,
|
|
214
|
+
"PLC1001",
|
|
215
|
+
"Block classes are concrete; generic classes and decorators are unsupported.",
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
const heritage = node.heritageClauses;
|
|
219
|
+
if (
|
|
220
|
+
heritage?.length !== 1 ||
|
|
221
|
+
heritage[0]?.token !== ts.SyntaxKind.ExtendsKeyword ||
|
|
222
|
+
heritage[0].types.length !== 1
|
|
223
|
+
) {
|
|
224
|
+
this.fail(
|
|
225
|
+
node,
|
|
226
|
+
"PLC1001",
|
|
227
|
+
"Function block classes extend the canonical FunctionBlock marker only.",
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
const base = heritage[0].types[0]!;
|
|
231
|
+
if (
|
|
232
|
+
this.types.sdkName(base.expression) !== "FunctionBlock" ||
|
|
233
|
+
!base.typeArguments ||
|
|
234
|
+
base.typeArguments.length < 1 ||
|
|
235
|
+
base.typeArguments.length > 2
|
|
236
|
+
) {
|
|
237
|
+
this.fail(
|
|
238
|
+
base,
|
|
239
|
+
"PLC1001",
|
|
240
|
+
"Extend FunctionBlock<Inputs, Outputs> with concrete schemas.",
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
// Register identity before nested fields; the active set rejects recursive allocation.
|
|
244
|
+
this.active.add(symbol);
|
|
245
|
+
const id = this.types.id(node, node.name.text);
|
|
246
|
+
const typeId = `block:${id}`;
|
|
247
|
+
this.types.types.set(typeId, { id: typeId, kind: "block", block: id });
|
|
248
|
+
this.types.blockTypes.set(symbol, typeId);
|
|
249
|
+
try {
|
|
250
|
+
const inputFields = this.types.inputFields(base.typeArguments[0]!);
|
|
251
|
+
let result: string | undefined;
|
|
252
|
+
const outputNode = base.typeArguments[1];
|
|
253
|
+
if (
|
|
254
|
+
outputNode &&
|
|
255
|
+
!(ts.isTypeLiteralNode(outputNode) && !outputNode.members.length)
|
|
256
|
+
) {
|
|
257
|
+
const resolved = this.types.resolve(outputNode);
|
|
258
|
+
const layout = this.types.types.get(resolved.type);
|
|
259
|
+
if (
|
|
260
|
+
resolved.io ||
|
|
261
|
+
resolved.mode ||
|
|
262
|
+
this.types.hasReferences(resolved.type) ||
|
|
263
|
+
layout?.kind !== "record"
|
|
264
|
+
) {
|
|
265
|
+
this.fail(
|
|
266
|
+
outputNode,
|
|
267
|
+
"PLC2001",
|
|
268
|
+
"Block outputs require a closed value record.",
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
result = resolved.type;
|
|
272
|
+
}
|
|
273
|
+
const fieldBindings = new Map<ts.Symbol, Variable>();
|
|
274
|
+
const initializers = new Map<Variable, ts.Expression>();
|
|
275
|
+
const state: Variable[] = [];
|
|
276
|
+
let method: ts.MethodDeclaration | undefined;
|
|
277
|
+
const methods: ts.MethodDeclaration[] = [];
|
|
278
|
+
for (const member of node.members) {
|
|
279
|
+
if (ts.isMethodDeclaration(member)) {
|
|
280
|
+
if (ts.isIdentifier(member.name) && member.name.text !== "execute") {
|
|
281
|
+
if (
|
|
282
|
+
!member.body ||
|
|
283
|
+
member.asteriskToken ||
|
|
284
|
+
member.questionToken ||
|
|
285
|
+
member.typeParameters?.length ||
|
|
286
|
+
["update", "out"].includes(member.name.text) ||
|
|
287
|
+
member.modifiers?.some(
|
|
288
|
+
(modifier) =>
|
|
289
|
+
![
|
|
290
|
+
ts.SyntaxKind.PublicKeyword,
|
|
291
|
+
ts.SyntaxKind.PrivateKeyword,
|
|
292
|
+
ts.SyntaxKind.ProtectedKeyword,
|
|
293
|
+
].includes(modifier.kind),
|
|
294
|
+
)
|
|
295
|
+
) {
|
|
296
|
+
this.fail(
|
|
297
|
+
member,
|
|
298
|
+
"PLC1001",
|
|
299
|
+
"Block methods require concrete instance implementations without generics or overloads.",
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
methods.push(member);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
if (
|
|
306
|
+
method ||
|
|
307
|
+
!ts.isIdentifier(member.name) ||
|
|
308
|
+
member.name.text !== "execute" ||
|
|
309
|
+
!member.body ||
|
|
310
|
+
member.asteriskToken ||
|
|
311
|
+
member.modifiers?.some(
|
|
312
|
+
(modifier) =>
|
|
313
|
+
![
|
|
314
|
+
ts.SyntaxKind.ProtectedKeyword,
|
|
315
|
+
ts.SyntaxKind.OverrideKeyword,
|
|
316
|
+
].includes(modifier.kind),
|
|
317
|
+
) ||
|
|
318
|
+
!member.modifiers?.some(
|
|
319
|
+
(modifier) => modifier.kind === ts.SyntaxKind.ProtectedKeyword,
|
|
320
|
+
)
|
|
321
|
+
) {
|
|
322
|
+
this.fail(
|
|
323
|
+
member,
|
|
324
|
+
"PLC1001",
|
|
325
|
+
"A block implements one protected execute method.",
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
method = member;
|
|
329
|
+
continue;
|
|
330
|
+
}
|
|
331
|
+
const field = this.collectStateField(member, id);
|
|
332
|
+
state.push(field.variable);
|
|
333
|
+
fieldBindings.set(field.symbol, field.variable);
|
|
334
|
+
if (field.initializer) {
|
|
335
|
+
initializers.set(field.variable, field.initializer);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
if (method?.parameters.length !== 1 || method.typeParameters?.length) {
|
|
339
|
+
this.fail(
|
|
340
|
+
node,
|
|
341
|
+
"PLC1001",
|
|
342
|
+
"Block execute requires one concrete input record parameter.",
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
const parameter = method.parameters[0]!;
|
|
346
|
+
if (
|
|
347
|
+
!ts.isIdentifier(parameter.name) ||
|
|
348
|
+
!parameter.type ||
|
|
349
|
+
parameter.initializer ||
|
|
350
|
+
parameter.questionToken ||
|
|
351
|
+
parameter.dotDotDotToken
|
|
352
|
+
) {
|
|
353
|
+
this.fail(
|
|
354
|
+
parameter,
|
|
355
|
+
"PLC2001",
|
|
356
|
+
"Block execute requires one named, explicitly typed input parameter.",
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
const methodFields = this.types.inputFields(parameter.type);
|
|
360
|
+
if (JSON.stringify(methodFields) !== JSON.stringify(inputFields)) {
|
|
361
|
+
this.fail(
|
|
362
|
+
parameter,
|
|
363
|
+
"PLC2001",
|
|
364
|
+
"execute input schema differs from its FunctionBlock marker.",
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
if (
|
|
368
|
+
!method.type ||
|
|
369
|
+
(result
|
|
370
|
+
? this.types.resolve(method.type).type !== result
|
|
371
|
+
: method.type.kind !== ts.SyntaxKind.VoidKeyword)
|
|
372
|
+
) {
|
|
373
|
+
this.fail(
|
|
374
|
+
method,
|
|
375
|
+
"PLC2001",
|
|
376
|
+
"execute return annotation must match the complete output record, or void when there are no outputs.",
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
const inputs = inputFields.map((field) => {
|
|
380
|
+
// Records and arrays pass by reference, as objects do in TypeScript.
|
|
381
|
+
const mode =
|
|
382
|
+
field.mode ??
|
|
383
|
+
(aggregate(this.types.types.get(field.type)) ? "inOut" : undefined);
|
|
384
|
+
return {
|
|
385
|
+
id: `${id}:input:${field.name}`,
|
|
386
|
+
name: field.name,
|
|
387
|
+
type: field.type,
|
|
388
|
+
storage: "parameter" as const,
|
|
389
|
+
mutable: mode === "inOut",
|
|
390
|
+
retain: false,
|
|
391
|
+
span: this.origin(parameter),
|
|
392
|
+
...(mode && { mode }),
|
|
393
|
+
};
|
|
394
|
+
});
|
|
395
|
+
const outputType = result && this.types.types.get(result);
|
|
396
|
+
const outputs: Variable[] =
|
|
397
|
+
outputType && outputType.kind === "record"
|
|
398
|
+
? outputType.fields.map((field) => ({
|
|
399
|
+
id: `${id}:output:${field.name}`,
|
|
400
|
+
name: field.name,
|
|
401
|
+
type: field.type,
|
|
402
|
+
storage: "blockOutput",
|
|
403
|
+
mutable: false,
|
|
404
|
+
retain: false,
|
|
405
|
+
initial: defaultValue(field.type, this.types.types),
|
|
406
|
+
span: this.origin(method!),
|
|
407
|
+
}))
|
|
408
|
+
: [];
|
|
409
|
+
const block: FunctionBlock = {
|
|
410
|
+
id,
|
|
411
|
+
name: node.name.text,
|
|
412
|
+
inputs,
|
|
413
|
+
outputs,
|
|
414
|
+
state,
|
|
415
|
+
locals: [],
|
|
416
|
+
body: [],
|
|
417
|
+
span: this.origin(node),
|
|
418
|
+
...(result && { result }),
|
|
419
|
+
};
|
|
420
|
+
this.definitions.set(id, block);
|
|
421
|
+
this.types.types.set(typeId, {
|
|
422
|
+
id: typeId,
|
|
423
|
+
kind: "block",
|
|
424
|
+
block: id,
|
|
425
|
+
...(result && { output: result }),
|
|
426
|
+
});
|
|
427
|
+
this.sources.set(symbol, {
|
|
428
|
+
block,
|
|
429
|
+
method,
|
|
430
|
+
methods,
|
|
431
|
+
inputSymbol: this.types.symbol(parameter.name),
|
|
432
|
+
inputBindings: new Map(
|
|
433
|
+
inputs.map((variable) => [variable.name, variable]),
|
|
434
|
+
),
|
|
435
|
+
fieldBindings,
|
|
436
|
+
initializers,
|
|
437
|
+
});
|
|
438
|
+
} finally {
|
|
439
|
+
this.active.delete(symbol);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
// Private state fields own storage admission, constructor allocation and initializer capture.
|
|
443
|
+
// The enclosing collection keeps schema/execute validation separate from this field policy.
|
|
444
|
+
private collectStateField(
|
|
445
|
+
member: ts.ClassElement,
|
|
446
|
+
id: string,
|
|
447
|
+
): {
|
|
448
|
+
variable: Variable;
|
|
449
|
+
symbol: ts.Symbol;
|
|
450
|
+
initializer?: ts.Expression;
|
|
451
|
+
} {
|
|
452
|
+
if (
|
|
453
|
+
!ts.isPropertyDeclaration(member) ||
|
|
454
|
+
!ts.isIdentifier(member.name) ||
|
|
455
|
+
!member.initializer ||
|
|
456
|
+
member.questionToken ||
|
|
457
|
+
member.exclamationToken ||
|
|
458
|
+
!member.modifiers?.some(
|
|
459
|
+
(modifier) => modifier.kind === ts.SyntaxKind.PrivateKeyword,
|
|
460
|
+
) ||
|
|
461
|
+
member.modifiers.some(
|
|
462
|
+
(modifier) =>
|
|
463
|
+
![
|
|
464
|
+
ts.SyntaxKind.PrivateKeyword,
|
|
465
|
+
ts.SyntaxKind.ReadonlyKeyword,
|
|
466
|
+
].includes(modifier.kind),
|
|
467
|
+
)
|
|
468
|
+
) {
|
|
469
|
+
this.fail(
|
|
470
|
+
member,
|
|
471
|
+
"PLC1001",
|
|
472
|
+
"Block state uses private initialized instance fields; constructors, static fields and decorators are unsupported.",
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
const initial = storageInitializer(
|
|
476
|
+
member.initializer,
|
|
477
|
+
this.types,
|
|
478
|
+
this.fail,
|
|
479
|
+
);
|
|
480
|
+
const instanceArray = ts.isNewExpression(initial.value)
|
|
481
|
+
? undefined
|
|
482
|
+
: this.instanceArrayType(member.type, initial.value);
|
|
483
|
+
let type: string;
|
|
484
|
+
if (ts.isNewExpression(initial.value)) {
|
|
485
|
+
type = this.instanceType(initial.value);
|
|
486
|
+
} else if (instanceArray) {
|
|
487
|
+
type = instanceArray;
|
|
488
|
+
} else if (member.type) {
|
|
489
|
+
const annotation = this.types.resolve(member.type);
|
|
490
|
+
if (
|
|
491
|
+
annotation.io ||
|
|
492
|
+
annotation.mode ||
|
|
493
|
+
this.types.hasReferences(annotation.type)
|
|
494
|
+
) {
|
|
495
|
+
this.fail(
|
|
496
|
+
member,
|
|
497
|
+
"PLC2001",
|
|
498
|
+
"Borrow and I/O wrappers cannot be stored in block state.",
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
type = annotation.type;
|
|
502
|
+
} else if (
|
|
503
|
+
initial.value.kind === ts.SyntaxKind.TrueKeyword ||
|
|
504
|
+
initial.value.kind === ts.SyntaxKind.FalseKeyword
|
|
505
|
+
) {
|
|
506
|
+
type = "BOOL";
|
|
507
|
+
} else if (/^-?\d/.test(initial.value.getText())) {
|
|
508
|
+
type = numericLiteralType(initial.value.getText());
|
|
509
|
+
} else {
|
|
510
|
+
this.fail(
|
|
511
|
+
member,
|
|
512
|
+
"PLC2001",
|
|
513
|
+
"Annotate private numeric and aggregate state explicitly.",
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
if (member.type && this.types.resolve(member.type).type !== type) {
|
|
517
|
+
this.fail(
|
|
518
|
+
member,
|
|
519
|
+
"PLC2001",
|
|
520
|
+
"Instance annotation differs from its constructor.",
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
const instance = Boolean(instancedBlock(type, this.types.types));
|
|
524
|
+
if (
|
|
525
|
+
this.types.types.get(type)?.kind === "block" &&
|
|
526
|
+
!ts.isNewExpression(initial.value)
|
|
527
|
+
) {
|
|
528
|
+
this.fail(
|
|
529
|
+
member,
|
|
530
|
+
"PLC1007",
|
|
531
|
+
"Block instances require static constructor initialization.",
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
const variable: Variable = {
|
|
535
|
+
id: `${id}:field:${member.name.text}`,
|
|
536
|
+
name: member.name.text,
|
|
537
|
+
type,
|
|
538
|
+
storage: instance ? "instance" : "field",
|
|
539
|
+
mutable:
|
|
540
|
+
!instance &&
|
|
541
|
+
!member.modifiers.some(
|
|
542
|
+
(modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword,
|
|
543
|
+
),
|
|
544
|
+
retain: initial.retain,
|
|
545
|
+
span: this.origin(member),
|
|
546
|
+
};
|
|
547
|
+
return {
|
|
548
|
+
variable,
|
|
549
|
+
symbol: this.types.symbol(member.name),
|
|
550
|
+
...(!instance && { initializer: initial.value }),
|
|
551
|
+
};
|
|
552
|
+
}
|
|
419
553
|
}
|