@tsplc/compiler 0.2.17 → 0.2.19

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.
Files changed (157) hide show
  1. package/README.md +2 -2
  2. package/dist/cli.js +920 -943
  3. package/dist/cli.js.map +13 -10
  4. package/dist/codesys.js +3893 -3352
  5. package/dist/codesys.js.map +29 -20
  6. package/dist/elaborate-capture.js +33 -17
  7. package/dist/elaborate-capture.js.map +3 -3
  8. package/dist/index.js +8787 -6855
  9. package/dist/index.js.map +40 -20
  10. package/dist/ir.js +1575 -1072
  11. package/dist/ir.js.map +21 -17
  12. package/dist/math.js.map +2 -2
  13. package/dist/project.js +154 -60
  14. package/dist/project.js.map +5 -5
  15. package/dist/telemetry.js +27 -9
  16. package/dist/telemetry.js.map +5 -5
  17. package/package.json +112 -112
  18. package/src/codesys/config.ts +50 -0
  19. package/src/codesys/debug.ts +670 -0
  20. package/src/codesys/emit.ts +142 -0
  21. package/src/codesys/engineering.ts +109 -0
  22. package/src/codesys/expressions.ts +92 -0
  23. package/src/codesys/global-lists.ts +250 -0
  24. package/src/codesys/global-names.ts +68 -0
  25. package/src/codesys/http.ts +890 -0
  26. package/src/codesys/index.ts +30 -0
  27. package/src/codesys/instrumentation.ts +430 -0
  28. package/src/codesys/io-paths.ts +140 -0
  29. package/src/codesys/manifest.ts +643 -0
  30. package/src/codesys/names.ts +81 -0
  31. package/src/codesys/numeric.ts +409 -0
  32. package/src/codesys/operands.ts +62 -0
  33. package/src/codesys/plan.ts +605 -0
  34. package/src/codesys/programs.ts +53 -0
  35. package/src/codesys/publication.ts +171 -0
  36. package/src/codesys/routines.ts +603 -0
  37. package/src/codesys/sources.ts +71 -0
  38. package/src/codesys/st-files.ts +115 -0
  39. package/src/codesys/storage.ts +97 -0
  40. package/src/codesys/symbols.ts +220 -0
  41. package/src/codesys/task-access.ts +320 -0
  42. package/src/codesys/telemetry.ts +732 -0
  43. package/src/codesys/types.ts +250 -0
  44. package/src/codesys/vendor.ts +150 -0
  45. package/src/frontend/analyzer.ts +794 -0
  46. package/src/frontend/async.ts +317 -1137
  47. package/src/frontend/blocks.ts +537 -403
  48. package/src/frontend/configuration.ts +176 -144
  49. package/src/frontend/const-functions.ts +183 -148
  50. package/src/frontend/cooperative.ts +73 -43
  51. package/src/frontend/coroutines.ts +1032 -0
  52. package/src/frontend/elaborate/calls.ts +535 -0
  53. package/src/frontend/elaborate/capture.ts +157 -101
  54. package/src/frontend/elaborate/dialect.ts +257 -0
  55. package/src/frontend/elaborate/expressions.ts +992 -0
  56. package/src/frontend/elaborate/frame.ts +53 -0
  57. package/src/frontend/elaborate/graph.ts +116 -71
  58. package/src/frontend/elaborate/imports.ts +70 -0
  59. package/src/frontend/elaborate/index.ts +435 -331
  60. package/src/frontend/elaborate/initializers.ts +198 -0
  61. package/src/frontend/elaborate/layouts.ts +591 -384
  62. package/src/frontend/elaborate/model.ts +264 -169
  63. package/src/frontend/elaborate/specialize.ts +343 -2586
  64. package/src/frontend/elaborate/statements.ts +606 -0
  65. package/src/frontend/elaborate/storage.ts +546 -0
  66. package/src/frontend/elaborate/values.ts +58 -0
  67. package/src/frontend/elaborate/writes.ts +100 -0
  68. package/src/frontend/exposure.ts +172 -140
  69. package/src/frontend/expressions.ts +1383 -0
  70. package/src/frontend/intrinsics.ts +337 -0
  71. package/src/frontend/module-graph.ts +89 -59
  72. package/src/frontend/positions.ts +271 -200
  73. package/src/frontend/promise-chains.ts +897 -0
  74. package/src/frontend/promises.ts +390 -747
  75. package/src/frontend/routines.ts +181 -0
  76. package/src/frontend/sdk.ts +82 -0
  77. package/src/frontend/services.ts +506 -372
  78. package/src/frontend/statements.ts +855 -0
  79. package/src/frontend/tasks.ts +471 -0
  80. package/src/frontend/telemetry.ts +174 -0
  81. package/src/frontend/types.ts +845 -551
  82. package/src/frontend/typescript.ts +323 -2543
  83. package/src/index.ts +5 -5
  84. package/src/ir/aggregates.ts +275 -0
  85. package/src/ir/allocation.ts +84 -0
  86. package/src/ir/core-model.ts +146 -0
  87. package/src/ir/declarations.ts +295 -0
  88. package/src/ir/diagnostics.ts +61 -0
  89. package/src/ir/domains.ts +152 -0
  90. package/src/ir/fold.ts +342 -0
  91. package/src/ir/index.ts +90 -0
  92. package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
  93. package/src/ir/layouts.ts +181 -0
  94. package/src/ir/link.ts +354 -0
  95. package/src/ir/model.ts +386 -0
  96. package/{ir → src/ir}/normalization-error.ts +4 -4
  97. package/src/ir/normalize.ts +70 -0
  98. package/src/ir/numeric.ts +171 -0
  99. package/src/ir/ranges.ts +256 -0
  100. package/src/ir/routine.ts +1163 -0
  101. package/src/ir/scalars.ts +321 -0
  102. package/src/ir/types.ts +127 -0
  103. package/src/project/artifacts.ts +342 -0
  104. package/src/project/cli.ts +177 -0
  105. package/src/project/debugger.ts +82 -0
  106. package/src/project/index.ts +6 -0
  107. package/src/project/plant.ts +467 -0
  108. package/src/project/project-commands.ts +372 -0
  109. package/src/project/project.ts +457 -0
  110. package/src/project/release.ts +153 -0
  111. package/src/project/server.ts +150 -0
  112. package/src/project/terminal.ts +32 -0
  113. package/stdlib/math.ts +14 -10
  114. package/stdlib/telemetry.ts +71 -47
  115. package/types/async.d.ts +46 -40
  116. package/types/data-types.d.ts +26 -26
  117. package/types/globals.d.ts +41 -38
  118. package/types/index.d.ts +96 -51
  119. package/types/plc.d.ts +3 -4
  120. package/types/telemetry.d.ts +14 -14
  121. package/backends/codesys/config.ts +0 -54
  122. package/backends/codesys/debug.ts +0 -509
  123. package/backends/codesys/emit.ts +0 -1738
  124. package/backends/codesys/engineering.ts +0 -88
  125. package/backends/codesys/global-names.ts +0 -49
  126. package/backends/codesys/http.ts +0 -675
  127. package/backends/codesys/index.ts +0 -13
  128. package/backends/codesys/io-paths.ts +0 -105
  129. package/backends/codesys/manifest.ts +0 -312
  130. package/backends/codesys/names.ts +0 -76
  131. package/backends/codesys/numeric.ts +0 -356
  132. package/backends/codesys/plan.ts +0 -457
  133. package/backends/codesys/publication.ts +0 -59
  134. package/backends/codesys/storage.ts +0 -70
  135. package/backends/codesys/task-access.ts +0 -293
  136. package/backends/codesys/telemetry.ts +0 -593
  137. package/backends/codesys/types.ts +0 -179
  138. package/backends/codesys/vendor.ts +0 -108
  139. package/ir/aggregates.ts +0 -333
  140. package/ir/allocation.ts +0 -74
  141. package/ir/core-model.ts +0 -129
  142. package/ir/core.ts +0 -64
  143. package/ir/declarations.ts +0 -208
  144. package/ir/diagnostics.ts +0 -43
  145. package/ir/domains.ts +0 -96
  146. package/ir/index.ts +0 -8
  147. package/ir/link.ts +0 -185
  148. package/ir/model.ts +0 -346
  149. package/ir/routine.ts +0 -859
  150. package/ir/scalars.ts +0 -675
  151. package/ir/types.ts +0 -76
  152. package/project/artifacts.ts +0 -277
  153. package/project/cli.ts +0 -395
  154. package/project/index.ts +0 -1
  155. package/project/plant.ts +0 -509
  156. package/project/project.ts +0 -358
  157. package/project/server.ts +0 -113
@@ -1,419 +1,553 @@
1
1
  import ts from "typescript";
2
2
  import {
3
- aggregate,
4
- defaultValue,
5
- instancedBlock,
6
- standardBlocks,
7
- type StandardBlock,
8
- type FunctionBlock,
9
- type Variable,
10
- type SourceSpan,
11
- type PlcType,
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 { calleeName, numericLiteralType, type Fail, type TypeResolver } from "./types.ts";
13
+ import {
14
+ calleeName,
15
+ numericLiteralType,
16
+ type Fail,
17
+ type TypeResolver,
18
+ } from "./types.ts";
14
19
 
15
20
  export interface BlockSource {
16
- block: FunctionBlock;
17
- method: ts.MethodDeclaration;
18
- methods: readonly ts.MethodDeclaration[];
19
- inputSymbol: ts.Symbol;
20
- inputBindings: ReadonlyMap<string, Variable>;
21
- fieldBindings: ReadonlyMap<ts.Symbol, Variable>;
22
- initializers: ReadonlyMap<Variable, ts.Expression>;
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
- node: ts.Expression,
26
- types: TypeResolver,
27
- fail: Fail,
30
+ node: ts.Expression,
31
+ types: TypeResolver,
32
+ fail: Fail,
28
33
  ): { value: ts.Expression; retain: boolean } {
29
- if (!ts.isCallExpression(node)) return { value: node, retain: false };
30
- const callee = calleeName(node.expression);
31
- if (!callee || types.sdkName(callee) !== "retain") return { value: node, retain: false };
32
- if (node.arguments.length !== 1 || (node.typeArguments?.length ?? 0) > 1)
33
- fail(node, "PLC3004", "retain requires one static storage initializer.");
34
- return { value: node.arguments[0]!, retain: true };
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
- readonly definitions = new Map<string, FunctionBlock>();
39
- readonly sources = new Map<ts.Symbol, BlockSource>();
40
- private readonly declarations = new Map<ts.Symbol, ts.ClassDeclaration>();
41
- private readonly active = new Set<ts.Symbol>();
42
- constructor(
43
- private readonly types: TypeResolver,
44
- private readonly origin: (node: ts.Node) => SourceSpan,
45
- private readonly fail: Fail,
46
- sdkSpan: SourceSpan,
47
- ) {
48
- for (const name of Object.keys(standardBlocks) as StandardBlock[]) {
49
- const schema = standardBlocks[name],
50
- id = `stdlib:${name}`,
51
- output = `${id}:out`;
52
- const variable = (label: string, type: string, storage: Variable["storage"]): Variable => ({
53
- id: `${id}:${label}`,
54
- name: label,
55
- type,
56
- storage,
57
- mutable: false,
58
- retain: false,
59
- span: sdkSpan,
60
- ...(storage === "blockOutput" && { initial: defaultValue(type, types.types) }),
61
- });
62
- const block: FunctionBlock = {
63
- id,
64
- name,
65
- inputs: schema.inputs.map(([label, , type]) => variable(label, type, "parameter")),
66
- outputs: schema.outputs.map(([label, , type]) => variable(label, type, "blockOutput")),
67
- state: [],
68
- locals: [],
69
- body: [],
70
- result: output,
71
- standard: name,
72
- span: sdkSpan,
73
- };
74
- this.definitions.set(id, block);
75
- types.types.set(output, {
76
- id: output,
77
- kind: "record",
78
- fields: block.outputs.map((field) => ({
79
- name: field.name,
80
- type: field.type,
81
- readonly: true,
82
- })),
83
- });
84
- const type: PlcType = { id: `block:${id}`, kind: "block", block: id, output };
85
- types.types.set(type.id, type);
86
- types.standardTypes.set(name, type.id);
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
- index(nodes: readonly ts.ClassDeclaration[]): void {
91
- for (const node of nodes) {
92
- if (!node.name) this.fail(node, "PLC1001", "Function block classes need names.");
93
- this.declarations.set(this.types.symbol(node.name), node);
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
- instanceType(node: ts.NewExpression): string {
98
- const name = calleeName(node.expression);
99
- if (node.arguments?.length || node.typeArguments?.length || !name)
100
- this.fail(
101
- node,
102
- "PLC1007",
103
- "Block instances use a concrete zero-argument constructor at static storage declarations.",
104
- );
105
- const official = this.types.sdkName(name),
106
- standard = official && this.types.standardTypes.get(official);
107
- if (standard) return standard;
108
- const symbol = this.types.symbol(name),
109
- declaration = this.declarations.get(symbol);
110
- if (!declaration)
111
- this.fail(node, "PLC1007", "Only recognized static function block allocation is supported.");
112
- this.collect(declaration);
113
- // Successful collection registers this class identity before publishing its source.
114
- return this.types.blockTypes.get(symbol)!;
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
- * `FixedArray<Block, N>` storage allocated with `defaultValue`; its elements are static
119
- * instances. The allocation names the type, so an annotation is optional but must agree.
120
- */
121
- instanceArrayType(annotation: ts.TypeNode | undefined, value: ts.Expression): string | undefined {
122
- const blockArray = (type: string) =>
123
- this.types.types.get(type)?.kind === "array" && instancedBlock(type, this.types.types)
124
- ? type
125
- : undefined;
126
- const declared = annotation && this.types.resolve(annotation).type;
127
- if (declared && !blockArray(declared)) return undefined;
128
- const callee = ts.isCallExpression(value) && calleeName(value.expression);
129
- const allocation =
130
- callee && this.types.sdkName(callee) === "defaultValue" && value.typeArguments?.length === 1
131
- ? this.types.resolve(value.typeArguments[0]!).type
132
- : undefined;
133
- if (!declared) return allocation && blockArray(allocation);
134
- if (allocation !== declared)
135
- this.fail(
136
- value,
137
- "PLC1007",
138
- "Block arrays allocate with defaultValue<FixedArray<Block, N>>() of their declared type.",
139
- );
140
- return declared;
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
- collect(node: ts.ClassDeclaration): void {
144
- if (!node.name) this.fail(node, "PLC1001", "Function block classes need names.");
145
- const symbol = this.types.symbol(node.name);
146
- if (this.sources.has(symbol)) return;
147
- if (this.active.has(symbol) || this.active.size >= 64)
148
- this.fail(node, "PLC1007", "Recursive or excessively nested block instance layout.");
149
- if (
150
- node.typeParameters?.length ||
151
- node.modifiers?.some(
152
- (modifier) =>
153
- modifier.kind === ts.SyntaxKind.Decorator ||
154
- modifier.kind === ts.SyntaxKind.AbstractKeyword,
155
- )
156
- )
157
- this.fail(
158
- node,
159
- "PLC1001",
160
- "Block classes are concrete; generic classes and decorators are unsupported.",
161
- );
162
- const heritage = node.heritageClauses;
163
- if (
164
- heritage?.length !== 1 ||
165
- heritage[0]?.token !== ts.SyntaxKind.ExtendsKeyword ||
166
- heritage[0].types.length !== 1
167
- )
168
- this.fail(
169
- node,
170
- "PLC1001",
171
- "Function block classes extend the canonical FunctionBlock marker only.",
172
- );
173
- const base = heritage[0].types[0]!;
174
- if (
175
- this.types.sdkName(base.expression) !== "FunctionBlock" ||
176
- !base.typeArguments ||
177
- base.typeArguments.length < 1 ||
178
- base.typeArguments.length > 2
179
- )
180
- this.fail(base, "PLC1001", "Extend FunctionBlock<Inputs, Outputs> with concrete schemas.");
181
- // Register identity before nested fields; the active set rejects recursive allocation.
182
- this.active.add(symbol);
183
- const id = this.types.id(node, node.name.text),
184
- typeId = `block:${id}`;
185
- this.types.types.set(typeId, { id: typeId, kind: "block", block: id });
186
- this.types.blockTypes.set(symbol, typeId);
187
- try {
188
- const inputFields = this.types.inputFields(base.typeArguments[0]!);
189
- let result: string | undefined;
190
- const outputNode = base.typeArguments[1];
191
- if (outputNode && !(ts.isTypeLiteralNode(outputNode) && !outputNode.members.length)) {
192
- const resolved = this.types.resolve(outputNode),
193
- layout = this.types.types.get(resolved.type);
194
- if (
195
- resolved.io ||
196
- resolved.mode ||
197
- this.types.hasReferences(resolved.type) ||
198
- layout?.kind !== "record"
199
- )
200
- this.fail(outputNode, "PLC2001", "Block outputs require a closed value record.");
201
- result = resolved.type;
202
- }
203
- const fieldBindings = new Map<ts.Symbol, Variable>(),
204
- initializers = new Map<Variable, ts.Expression>(),
205
- state: Variable[] = [];
206
- let method: ts.MethodDeclaration | undefined;
207
- const methods: ts.MethodDeclaration[] = [];
208
- for (const member of node.members) {
209
- if (ts.isMethodDeclaration(member)) {
210
- if (ts.isIdentifier(member.name) && member.name.text !== "execute") {
211
- if (
212
- !member.body ||
213
- member.asteriskToken ||
214
- member.questionToken ||
215
- member.typeParameters?.length ||
216
- ["update", "out"].includes(member.name.text) ||
217
- member.modifiers?.some(
218
- (modifier) =>
219
- ![
220
- ts.SyntaxKind.PublicKeyword,
221
- ts.SyntaxKind.PrivateKeyword,
222
- ts.SyntaxKind.ProtectedKeyword,
223
- ].includes(modifier.kind),
224
- )
225
- )
226
- this.fail(
227
- member,
228
- "PLC1001",
229
- "Block methods require concrete instance implementations without generics or overloads.",
230
- );
231
- methods.push(member);
232
- continue;
233
- }
234
- if (
235
- method ||
236
- !ts.isIdentifier(member.name) ||
237
- member.name.text !== "execute" ||
238
- !member.body ||
239
- member.asteriskToken ||
240
- member.modifiers?.some(
241
- (modifier) =>
242
- ![ts.SyntaxKind.ProtectedKeyword, ts.SyntaxKind.OverrideKeyword].includes(
243
- modifier.kind,
244
- ),
245
- ) ||
246
- !member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ProtectedKeyword)
247
- )
248
- this.fail(member, "PLC1001", "A block implements one protected execute method.");
249
- method = member;
250
- continue;
251
- }
252
- const field = this.collectStateField(member, id);
253
- state.push(field.variable);
254
- fieldBindings.set(field.symbol, field.variable);
255
- if (field.initializer) initializers.set(field.variable, field.initializer);
256
- }
257
- if (method?.parameters.length !== 1 || method.typeParameters?.length)
258
- this.fail(node, "PLC1001", "Block execute requires one concrete input record parameter.");
259
- const parameter = method.parameters[0]!;
260
- if (
261
- !ts.isIdentifier(parameter.name) ||
262
- !parameter.type ||
263
- parameter.initializer ||
264
- parameter.questionToken ||
265
- parameter.dotDotDotToken
266
- )
267
- this.fail(
268
- parameter,
269
- "PLC2001",
270
- "Block execute requires one named, explicitly typed input parameter.",
271
- );
272
- const methodFields = this.types.inputFields(parameter.type);
273
- if (JSON.stringify(methodFields) !== JSON.stringify(inputFields))
274
- this.fail(
275
- parameter,
276
- "PLC2001",
277
- "execute input schema differs from its FunctionBlock marker.",
278
- );
279
- if (
280
- !method.type ||
281
- (result
282
- ? this.types.resolve(method.type).type !== result
283
- : method.type.kind !== ts.SyntaxKind.VoidKeyword)
284
- )
285
- this.fail(
286
- method,
287
- "PLC2001",
288
- "execute return annotation must match the complete output record, or void when there are no outputs.",
289
- );
290
- const inputs = inputFields.map((field) => {
291
- // Records and arrays pass by reference, as objects do in TypeScript.
292
- const mode =
293
- field.mode ?? (aggregate(this.types.types.get(field.type)) ? "inOut" : undefined);
294
- return {
295
- id: `${id}:input:${field.name}`,
296
- name: field.name,
297
- type: field.type,
298
- storage: "parameter" as const,
299
- mutable: mode === "inOut",
300
- retain: false,
301
- span: this.origin(parameter),
302
- ...(mode && { mode }),
303
- };
304
- });
305
- const outputType = result && this.types.types.get(result),
306
- outputs: Variable[] =
307
- outputType && outputType.kind === "record"
308
- ? outputType.fields.map((field) => ({
309
- id: `${id}:output:${field.name}`,
310
- name: field.name,
311
- type: field.type,
312
- storage: "blockOutput",
313
- mutable: false,
314
- retain: false,
315
- initial: defaultValue(field.type, this.types.types),
316
- span: this.origin(method!),
317
- }))
318
- : [];
319
- const block: FunctionBlock = {
320
- id,
321
- name: node.name.text,
322
- inputs,
323
- outputs,
324
- state,
325
- locals: [],
326
- body: [],
327
- span: this.origin(node),
328
- ...(result && { result }),
329
- };
330
- this.definitions.set(id, block);
331
- this.types.types.set(typeId, {
332
- id: typeId,
333
- kind: "block",
334
- block: id,
335
- ...(result && { output: result }),
336
- });
337
- this.sources.set(symbol, {
338
- block,
339
- method,
340
- methods,
341
- inputSymbol: this.types.symbol(parameter.name),
342
- inputBindings: new Map(inputs.map((variable) => [variable.name, variable])),
343
- fieldBindings,
344
- initializers,
345
- });
346
- } finally {
347
- this.active.delete(symbol);
348
- }
349
- }
350
- // Private state fields own storage admission, constructor allocation and initializer capture.
351
- // The enclosing collection keeps schema/execute validation separate from this field policy.
352
- private collectStateField(
353
- member: ts.ClassElement,
354
- id: string,
355
- ): {
356
- variable: Variable;
357
- symbol: ts.Symbol;
358
- initializer?: ts.Expression;
359
- } {
360
- if (
361
- !ts.isPropertyDeclaration(member) ||
362
- !ts.isIdentifier(member.name) ||
363
- !member.initializer ||
364
- member.questionToken ||
365
- member.exclamationToken ||
366
- !member.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.PrivateKeyword) ||
367
- member.modifiers.some(
368
- (modifier) =>
369
- ![ts.SyntaxKind.PrivateKeyword, ts.SyntaxKind.ReadonlyKeyword].includes(modifier.kind),
370
- )
371
- )
372
- this.fail(
373
- member,
374
- "PLC1001",
375
- "Block state uses private initialized instance fields; constructors, static fields and decorators are unsupported.",
376
- );
377
- const initial = storageInitializer(member.initializer, this.types, this.fail);
378
- const instanceArray = ts.isNewExpression(initial.value)
379
- ? undefined
380
- : this.instanceArrayType(member.type, initial.value);
381
- let type: string;
382
- if (ts.isNewExpression(initial.value)) type = this.instanceType(initial.value);
383
- else if (instanceArray) type = instanceArray;
384
- else if (member.type) {
385
- const annotation = this.types.resolve(member.type);
386
- if (annotation.io || annotation.mode || this.types.hasReferences(annotation.type))
387
- this.fail(member, "PLC2001", "Borrow and I/O wrappers cannot be stored in block state.");
388
- type = annotation.type;
389
- } else if (
390
- initial.value.kind === ts.SyntaxKind.TrueKeyword ||
391
- initial.value.kind === ts.SyntaxKind.FalseKeyword
392
- )
393
- type = "BOOL";
394
- else if (/^-?\d/.test(initial.value.getText()))
395
- type = numericLiteralType(initial.value.getText());
396
- else this.fail(member, "PLC2001", "Annotate private numeric and aggregate state explicitly.");
397
- if (member.type && this.types.resolve(member.type).type !== type)
398
- this.fail(member, "PLC2001", "Instance annotation differs from its constructor.");
399
- const instance = Boolean(instancedBlock(type, this.types.types));
400
- if (this.types.types.get(type)?.kind === "block" && !ts.isNewExpression(initial.value))
401
- this.fail(member, "PLC1007", "Block instances require static constructor initialization.");
402
- const variable: Variable = {
403
- id: `${id}:field:${member.name.text}`,
404
- name: member.name.text,
405
- type,
406
- storage: instance ? "instance" : "field",
407
- mutable:
408
- !instance &&
409
- !member.modifiers.some((modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword),
410
- retain: initial.retain,
411
- span: this.origin(member),
412
- };
413
- return {
414
- variable,
415
- symbol: this.types.symbol(member.name),
416
- ...(!instance && { initializer: initial.value }),
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
  }