@tsplc/compiler 0.5.13 → 0.5.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (263) hide show
  1. package/README.md +33 -8
  2. package/dist/cli.js +794 -621
  3. package/dist/cli.js.map +19 -16
  4. package/dist/clock.js +5 -2
  5. package/dist/clock.js.map +5 -4
  6. package/dist/codesys.js +4162 -4157
  7. package/dist/codesys.js.map +43 -29
  8. package/dist/index.js +7852 -6549
  9. package/dist/index.js.map +53 -50
  10. package/dist/ir.js +2482 -2277
  11. package/dist/ir.js.map +28 -25
  12. package/dist/retain.js +3 -1
  13. package/dist/retain.js.map +3 -3
  14. package/dist/signals.js +115 -0
  15. package/dist/signals.js.map +10 -0
  16. package/dist/telemetry.js +25 -20
  17. package/dist/telemetry.js.map +6 -6
  18. package/package.json +19 -39
  19. package/src/codesys/config.ts +3 -3
  20. package/src/codesys/debug.ts +92 -108
  21. package/src/codesys/emit.ts +100 -88
  22. package/src/codesys/endpoints.ts +111 -0
  23. package/src/codesys/engineering.ts +30 -54
  24. package/src/codesys/explain/expressions.ts +445 -0
  25. package/src/codesys/explain/flags.ts +134 -0
  26. package/src/codesys/explain/parameters.ts +53 -0
  27. package/src/codesys/explain/rows.ts +182 -0
  28. package/src/codesys/explain/tables.ts +79 -0
  29. package/src/codesys/explain/tasks.ts +68 -0
  30. package/src/codesys/explain/writes.ts +497 -0
  31. package/src/codesys/explain.ts +85 -1401
  32. package/src/codesys/expressions.ts +41 -33
  33. package/src/codesys/global-lists.ts +114 -94
  34. package/src/codesys/global-names.ts +12 -14
  35. package/src/codesys/http.ts +428 -388
  36. package/src/codesys/instrumentation.ts +142 -148
  37. package/src/codesys/io-paths.ts +31 -23
  38. package/src/codesys/loops.ts +101 -0
  39. package/src/codesys/manifest.ts +269 -269
  40. package/src/codesys/names.ts +10 -33
  41. package/src/codesys/numeric.ts +89 -42
  42. package/src/codesys/plan.ts +202 -190
  43. package/src/codesys/programs.ts +23 -16
  44. package/src/codesys/publication.ts +13 -86
  45. package/src/codesys/required.ts +8 -0
  46. package/src/codesys/routines.ts +103 -489
  47. package/src/codesys/scheduling.ts +96 -0
  48. package/src/codesys/service-channels.ts +23 -0
  49. package/src/codesys/sources.ts +34 -28
  50. package/src/codesys/st-files.ts +112 -105
  51. package/src/codesys/statements.ts +409 -0
  52. package/src/codesys/storage.ts +41 -23
  53. package/src/codesys/symbols.ts +130 -119
  54. package/src/codesys/task-access.ts +65 -221
  55. package/src/codesys/telemetry-settings.ts +19 -0
  56. package/src/codesys/telemetry.ts +185 -148
  57. package/src/codesys/types.ts +69 -109
  58. package/src/codesys/vendor.ts +13 -50
  59. package/src/frontend/analyzer.ts +66 -111
  60. package/src/frontend/async.ts +316 -128
  61. package/src/frontend/blocks.ts +178 -154
  62. package/src/frontend/configuration.ts +49 -153
  63. package/src/frontend/const-functions.ts +50 -40
  64. package/src/frontend/control-flow.ts +164 -129
  65. package/src/frontend/cooperative.ts +17 -9
  66. package/src/frontend/coroutines.ts +539 -441
  67. package/src/frontend/dialect-names.ts +104 -0
  68. package/src/frontend/elaborate/array-searches.ts +77 -69
  69. package/src/frontend/elaborate/assignments.ts +68 -77
  70. package/src/frontend/elaborate/calls.ts +878 -512
  71. package/src/frontend/elaborate/capture-protocol.ts +118 -21
  72. package/src/frontend/elaborate/capture.ts +196 -109
  73. package/src/frontend/elaborate/declarations.ts +124 -0
  74. package/src/frontend/elaborate/dialect.ts +119 -46
  75. package/src/frontend/elaborate/expressions.ts +763 -442
  76. package/src/frontend/elaborate/filtered-arrays.ts +127 -91
  77. package/src/frontend/elaborate/frame.ts +50 -2
  78. package/src/frontend/elaborate/generics.ts +20 -34
  79. package/src/frontend/elaborate/graph.ts +13 -87
  80. package/src/frontend/elaborate/imports.ts +0 -5
  81. package/src/frontend/elaborate/index.ts +416 -169
  82. package/src/frontend/elaborate/initializers.ts +64 -42
  83. package/src/frontend/elaborate/iterators.ts +228 -364
  84. package/src/frontend/elaborate/layouts.ts +477 -116
  85. package/src/frontend/elaborate/model.ts +113 -55
  86. package/src/frontend/elaborate/references.ts +116 -138
  87. package/src/frontend/elaborate/retention.ts +137 -59
  88. package/src/frontend/elaborate/specialize.ts +617 -308
  89. package/src/frontend/elaborate/statements.ts +436 -247
  90. package/src/frontend/elaborate/storage.ts +280 -213
  91. package/src/frontend/elaborate/time-constructors.ts +34 -0
  92. package/src/frontend/elaborate/values.ts +77 -7
  93. package/src/frontend/elaborate/writes.ts +98 -84
  94. package/src/frontend/expressions.ts +149 -151
  95. package/src/frontend/generated-names.ts +3 -3
  96. package/src/frontend/intrinsics.ts +31 -17
  97. package/src/frontend/lowering-key.ts +23 -0
  98. package/src/frontend/module-graph.ts +105 -43
  99. package/src/frontend/positions.ts +43 -35
  100. package/src/frontend/promise-chains.ts +226 -266
  101. package/src/frontend/promises.ts +88 -67
  102. package/src/frontend/routines.ts +122 -65
  103. package/src/frontend/sdk.ts +104 -5
  104. package/src/frontend/services.ts +149 -102
  105. package/src/frontend/statements.ts +60 -64
  106. package/src/frontend/tasks.ts +151 -96
  107. package/src/frontend/telemetry.ts +1 -1
  108. package/src/frontend/types.ts +366 -181
  109. package/src/frontend/typescript.ts +239 -112
  110. package/src/index.ts +2 -1
  111. package/src/ir/aggregates.ts +90 -75
  112. package/src/ir/allocation.ts +29 -18
  113. package/src/ir/conditions.ts +38 -78
  114. package/src/ir/core-model.ts +128 -11
  115. package/src/ir/declarations.ts +18 -32
  116. package/src/ir/diagnostics.ts +25 -18
  117. package/src/ir/domains.ts +26 -23
  118. package/src/ir/fold.ts +55 -50
  119. package/src/ir/identity.ts +10 -0
  120. package/src/ir/index.ts +31 -7
  121. package/src/ir/job-errors.ts +30 -8
  122. package/src/ir/layouts.ts +63 -34
  123. package/src/ir/link.ts +11 -46
  124. package/src/ir/model.ts +25 -6
  125. package/src/ir/normalize.ts +3 -28
  126. package/src/ir/numeric.ts +45 -18
  127. package/src/ir/operands.ts +102 -0
  128. package/src/ir/ranges.ts +242 -67
  129. package/src/ir/routine.ts +126 -183
  130. package/src/ir/scalars.ts +29 -25
  131. package/src/ir/share.ts +323 -343
  132. package/src/ir/short-circuit.ts +140 -0
  133. package/src/ir/tables.ts +111 -0
  134. package/src/ir/temporaries.ts +15 -113
  135. package/src/ir/types.ts +32 -8
  136. package/src/project/artifacts.ts +143 -71
  137. package/src/project/cli.ts +104 -87
  138. package/src/project/debugger.ts +48 -34
  139. package/src/project/deployments.ts +12 -68
  140. package/src/project/flags.ts +14 -0
  141. package/src/project/plant.ts +293 -225
  142. package/src/project/project-commands.ts +254 -192
  143. package/src/project/project.ts +184 -141
  144. package/src/project/release.ts +22 -28
  145. package/src/project/server.ts +22 -26
  146. package/src/project/terminal.ts +13 -2
  147. package/src/runtime/async.ts +17 -13
  148. package/src/runtime/launch.ts +5 -2
  149. package/src/runtime/main.ts +8 -3
  150. package/src/runtime/missing-file.ts +3 -0
  151. package/src/runtime/process.ts +24 -19
  152. package/src/runtime/program.ts +20 -53
  153. package/src/runtime/retain.ts +18 -10
  154. package/src/runtime/runtime.ts +47 -60
  155. package/src/runtime/settings.ts +27 -0
  156. package/src/runtime/storage.ts +49 -74
  157. package/src/runtime/symbols.ts +19 -75
  158. package/src/runtime/telemetry.ts +37 -14
  159. package/src/runtime/transport.ts +26 -30
  160. package/stdlib/clock.ts +4 -2
  161. package/stdlib/plc-time.ts +2 -0
  162. package/stdlib/retain.ts +2 -0
  163. package/stdlib/signals.ts +142 -0
  164. package/stdlib/telemetry.ts +5 -12
  165. package/types/dialect.d.ts +49 -0
  166. package/types/globals.d.ts +2 -42
  167. package/types/index.d.ts +3 -1
  168. package/types/retain.d.ts +8 -2
  169. package/types/telemetry.d.ts +3 -0
  170. package/dist/bun-runtime.js +0 -1472
  171. package/dist/bun-runtime.js.map +0 -26
  172. package/dist/elaborate-capture.js +0 -304
  173. package/dist/elaborate-capture.js.map +0 -11
  174. package/dist/math.js +0 -24
  175. package/dist/math.js.map +0 -10
  176. package/dist/project.js +0 -626
  177. package/dist/project.js.map +0 -12
  178. package/dist/server-check.js +0 -59
  179. package/dist/server-check.js.map +0 -11
  180. package/src/frontend/exposure.ts +0 -181
  181. package/src/project/index.ts +0 -6
  182. package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
  183. package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
  184. package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
  185. package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
  186. package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
  187. package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
  188. package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
  189. package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
  190. package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
  191. package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
  192. package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
  193. package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
  194. package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
  195. package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
  196. package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
  197. package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
  198. package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
  199. package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
  200. package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
  201. package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
  202. package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
  203. package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
  204. package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
  205. package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
  206. package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
  207. package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
  208. package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
  209. package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
  210. package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
  211. package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
  212. package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
  213. package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
  214. package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
  215. package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
  216. package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
  217. package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
  218. package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
  219. package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
  220. package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
  221. package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
  222. package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
  223. package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
  224. package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
  225. package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
  226. package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
  227. package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
  228. package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
  229. package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
  230. package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
  231. package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
  232. package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
  233. package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
  234. package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
  235. package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
  236. package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
  237. package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
  238. package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
  239. package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
  240. package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
  241. package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
  242. package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
  243. package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
  244. package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
  245. package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
  246. package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
  247. package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
  248. package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
  249. package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
  250. package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
  251. package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
  252. package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
  253. package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
  254. package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
  255. package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
  256. package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
  257. package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
  258. package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
  259. package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
  260. package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
  261. package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
  262. package/stdlib/math.ts +0 -23
  263. package/types/plc.d.ts +0 -4
@@ -1,10 +1,8 @@
1
- // biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
2
1
  import {
3
- aggregate,
4
2
  defaultValue,
5
3
  type FunctionBlock,
6
4
  instancedBlock,
7
- type PlcType,
5
+ type Retention,
8
6
  type SourceSpan,
9
7
  type StandardBlock,
10
8
  standardBlocks,
@@ -14,41 +12,63 @@ import ts from "typescript";
14
12
  import {
15
13
  calleeName,
16
14
  type Fail,
17
- numericLiteralType,
15
+ hasModifier,
16
+ inferredStorageType,
17
+ SemanticError,
18
18
  type TypeResolver,
19
19
  } from "./types";
20
20
 
21
21
  export interface BlockSource {
22
22
  block: FunctionBlock;
23
- method: ts.MethodDeclaration;
23
+ /** The block's layout. */
24
+ type: string;
25
+ /** The body of its `execute` method. */
26
+ body: ts.Block;
24
27
  methods: readonly ts.MethodDeclaration[];
25
28
  inputSymbol: ts.Symbol;
26
29
  inputBindings: ReadonlyMap<string, Variable>;
27
30
  fieldBindings: ReadonlyMap<ts.Symbol, Variable>;
28
31
  initializers: ReadonlyMap<Variable, ts.Expression>;
29
32
  }
33
+ /** The SDK call an initializer is wrapped in to keep its storage through restarts, and what it keeps. */
34
+ const retentionCalls: ReadonlyMap<string, Retention> = new Map([
35
+ ["persist", "persist"],
36
+ ["retain", "retain"],
37
+ ]);
38
+
30
39
  export function storageInitializer(
31
40
  node: ts.Expression,
32
41
  types: TypeResolver,
33
42
  fail: Fail,
34
- ): { value: ts.Expression; retain: boolean } {
43
+ ): { value: ts.Expression; retention: Retention } {
35
44
  if (!ts.isCallExpression(node)) {
36
- return { value: node, retain: false };
45
+ return { value: node, retention: "none" };
37
46
  }
38
47
  const callee = calleeName(node.expression);
39
- if (!callee || types.sdkName(callee) !== "retain") {
40
- return { value: node, retain: false };
48
+ const name = callee && types.sdkName(callee);
49
+ const retention = name && retentionCalls.get(name);
50
+ if (!name || !retention) {
51
+ return { value: node, retention: "none" };
41
52
  }
42
- if (node.arguments.length !== 1 || (node.typeArguments?.length ?? 0) > 1) {
43
- fail(node, "PLC3004", "retain requires one static storage initializer.");
53
+ const [value, ...rest] = node.arguments;
54
+ if (!value || rest.length || (node.typeArguments?.length ?? 0) > 1) {
55
+ fail(node, "PLC3004", `${name} requires one static storage initializer.`);
44
56
  }
45
- return { value: node.arguments[0]!, retain: true };
57
+ return { value, retention };
46
58
  }
47
59
 
60
+ /** How deep block instances may nest inside one another's state. */
61
+ const maxInstanceNesting = 64;
62
+
63
+ const isStandardBlock = (name: string): name is StandardBlock =>
64
+ Object.hasOwn(standardBlocks, name);
65
+
48
66
  export class BlockResolver {
49
67
  readonly definitions = new Map<string, FunctionBlock>();
50
68
  readonly sources = new Map<ts.Symbol, BlockSource>();
51
69
  private readonly declarations = new Map<ts.Symbol, ts.ClassDeclaration>();
70
+ /** Classes whose collection failed, so another instance reports nothing new. */
71
+ private readonly failures = new Map<ts.Symbol, SemanticError>();
52
72
  private readonly active = new Set<ts.Symbol>();
53
73
  constructor(
54
74
  private readonly types: TypeResolver,
@@ -56,7 +76,7 @@ export class BlockResolver {
56
76
  private readonly fail: Fail,
57
77
  sdkSpan: SourceSpan,
58
78
  ) {
59
- for (const name of Object.keys(standardBlocks) as StandardBlock[]) {
79
+ for (const name of Object.keys(standardBlocks).filter(isStandardBlock)) {
60
80
  const schema = standardBlocks[name];
61
81
  const id = `stdlib:${name}`;
62
82
  const output = `${id}:out`;
@@ -70,7 +90,7 @@ export class BlockResolver {
70
90
  type,
71
91
  storage,
72
92
  mutable: false,
73
- retain: false,
93
+ retention: "none",
74
94
  span: sdkSpan,
75
95
  ...(storage === "blockOutput" && {
76
96
  initial: defaultValue(type, types.types),
@@ -93,32 +113,23 @@ export class BlockResolver {
93
113
  span: sdkSpan,
94
114
  };
95
115
  this.definitions.set(id, block);
96
- types.types.set(output, {
116
+ types.declareStandardBlock(name, id, {
97
117
  id: output,
98
- kind: "record",
99
118
  fields: block.outputs.map((field) => ({
100
119
  name: field.name,
101
120
  type: field.type,
102
121
  readonly: true,
103
122
  })),
104
123
  });
105
- const type: PlcType = {
106
- id: `block:${id}`,
107
- kind: "block",
108
- block: id,
109
- output,
110
- };
111
- types.types.set(type.id, type);
112
- types.standardTypes.set(name, type.id);
113
124
  }
114
125
  }
115
126
 
127
+ /** Makes named classes allocatable; `collect` reports a class without a name. */
116
128
  index(nodes: readonly ts.ClassDeclaration[]): void {
117
129
  for (const node of nodes) {
118
- if (!node.name) {
119
- this.fail(node, "PLC1001", "Function block classes need names.");
130
+ if (node.name) {
131
+ this.declarations.set(this.types.symbol(node.name), node);
120
132
  }
121
- this.declarations.set(this.types.symbol(node.name), node);
122
133
  }
123
134
  }
124
135
 
@@ -132,12 +143,11 @@ export class BlockResolver {
132
143
  );
133
144
  }
134
145
  const official = this.types.sdkName(name);
135
- const standard = official && this.types.standardTypes.get(official);
146
+ const standard = official && this.types.standardBlock(official);
136
147
  if (standard) {
137
148
  return standard;
138
149
  }
139
- const symbol = this.types.symbol(name);
140
- const declaration = this.declarations.get(symbol);
150
+ const declaration = this.declarations.get(this.types.symbol(name));
141
151
  if (!declaration) {
142
152
  this.fail(
143
153
  node,
@@ -145,9 +155,7 @@ export class BlockResolver {
145
155
  "Only recognized static function block allocation is supported.",
146
156
  );
147
157
  }
148
- this.collect(declaration);
149
- // Successful collection registers this class identity before publishing its source.
150
- return this.types.blockTypes.get(symbol)!;
158
+ return this.collect(declaration).type;
151
159
  }
152
160
 
153
161
  /**
@@ -168,12 +176,13 @@ export class BlockResolver {
168
176
  return undefined;
169
177
  }
170
178
  const callee = ts.isCallExpression(value) && calleeName(value.expression);
171
- const allocation =
179
+ const allocated =
172
180
  callee &&
173
181
  this.types.sdkName(callee) === "defaultValue" &&
174
182
  value.typeArguments?.length === 1
175
- ? this.types.resolve(value.typeArguments[0]!).type
183
+ ? value.typeArguments[0]
176
184
  : undefined;
185
+ const allocation = allocated && this.types.resolve(allocated).type;
177
186
  if (!declared) {
178
187
  return allocation && blockArray(allocation);
179
188
  }
@@ -187,15 +196,35 @@ export class BlockResolver {
187
196
  return declared;
188
197
  }
189
198
 
190
- collect(node: ts.ClassDeclaration): void {
199
+ collect(node: ts.ClassDeclaration): BlockSource {
191
200
  if (!node.name) {
192
201
  this.fail(node, "PLC1001", "Function block classes need names.");
193
202
  }
194
203
  const symbol = this.types.symbol(node.name);
195
- if (this.sources.has(symbol)) {
196
- return;
204
+ const collected = this.sources.get(symbol);
205
+ if (collected) {
206
+ return collected;
207
+ }
208
+ const failure = this.failures.get(symbol);
209
+ if (failure) {
210
+ throw failure;
197
211
  }
198
- if (this.active.has(symbol) || this.active.size >= 64) {
212
+ try {
213
+ return this.collectClass(node, node.name, symbol);
214
+ } catch (cause) {
215
+ if (cause instanceof SemanticError) {
216
+ this.failures.set(symbol, cause);
217
+ }
218
+ throw cause;
219
+ }
220
+ }
221
+
222
+ private collectClass(
223
+ node: ts.ClassDeclaration,
224
+ name: ts.Identifier,
225
+ symbol: ts.Symbol,
226
+ ): BlockSource {
227
+ if (this.active.has(symbol) || this.active.size >= maxInstanceNesting) {
199
228
  this.fail(
200
229
  node,
201
230
  "PLC1007",
@@ -216,11 +245,13 @@ export class BlockResolver {
216
245
  "Block classes are concrete; generic classes and decorators are unsupported.",
217
246
  );
218
247
  }
219
- const heritage = node.heritageClauses;
248
+ const [heritage, ...otherHeritage] = node.heritageClauses ?? [];
249
+ const [base, ...otherBases] = heritage?.types ?? [];
220
250
  if (
221
- heritage?.length !== 1 ||
222
- heritage[0]?.token !== ts.SyntaxKind.ExtendsKeyword ||
223
- heritage[0].types.length !== 1
251
+ otherHeritage.length ||
252
+ heritage?.token !== ts.SyntaxKind.ExtendsKeyword ||
253
+ !base ||
254
+ otherBases.length
224
255
  ) {
225
256
  this.fail(
226
257
  node,
@@ -228,12 +259,11 @@ export class BlockResolver {
228
259
  "Function block classes extend the canonical FunctionBlock marker only.",
229
260
  );
230
261
  }
231
- const base = heritage[0].types[0]!;
262
+ const [inputNode, outputNode, ...otherSchemas] = base.typeArguments ?? [];
232
263
  if (
233
264
  this.types.sdkName(base.expression) !== "FunctionBlock" ||
234
- !base.typeArguments ||
235
- base.typeArguments.length < 1 ||
236
- base.typeArguments.length > 2
265
+ !inputNode ||
266
+ otherSchemas.length
237
267
  ) {
238
268
  this.fail(
239
269
  base,
@@ -243,38 +273,15 @@ export class BlockResolver {
243
273
  }
244
274
  // Register identity before nested fields; the active set rejects recursive allocation.
245
275
  this.active.add(symbol);
246
- const id = this.types.id(node, node.name.text);
247
- const typeId = `block:${id}`;
248
- this.types.types.set(typeId, { id: typeId, kind: "block", block: id });
249
- this.types.blockTypes.set(symbol, typeId);
276
+ const id = this.types.id(node, name.text);
277
+ const typeId = this.types.declareBlock(symbol, id);
250
278
  try {
251
- const inputFields = this.types.inputFields(base.typeArguments[0]!);
252
- let result: string | undefined;
253
- const outputNode = base.typeArguments[1];
254
- if (
255
- outputNode &&
256
- !(ts.isTypeLiteralNode(outputNode) && !outputNode.members.length)
257
- ) {
258
- const resolved = this.types.resolve(outputNode);
259
- const layout = this.types.types.get(resolved.type);
260
- if (
261
- resolved.io ||
262
- resolved.mode ||
263
- this.types.hasReferences(resolved.type) ||
264
- layout?.kind !== "record"
265
- ) {
266
- this.fail(
267
- outputNode,
268
- "PLC2001",
269
- "Block outputs require a closed value record.",
270
- );
271
- }
272
- result = resolved.type;
273
- }
279
+ const inputFields = this.types.inputFields(inputNode);
280
+ const result = outputNode && this.outputRecord(outputNode);
274
281
  const fieldBindings = new Map<ts.Symbol, Variable>();
275
282
  const initializers = new Map<Variable, ts.Expression>();
276
283
  const state: Variable[] = [];
277
- let method: ts.MethodDeclaration | undefined;
284
+ let execute: { method: ts.MethodDeclaration; body: ts.Block } | undefined;
278
285
  const methods: ts.MethodDeclaration[] = [];
279
286
  for (const member of node.members) {
280
287
  if (ts.isMethodDeclaration(member)) {
@@ -304,7 +311,7 @@ export class BlockResolver {
304
311
  continue;
305
312
  }
306
313
  if (
307
- method ||
314
+ execute ||
308
315
  !ts.isIdentifier(member.name) ||
309
316
  member.name.text !== "execute" ||
310
317
  !member.body ||
@@ -316,9 +323,7 @@ export class BlockResolver {
316
323
  ts.SyntaxKind.OverrideKeyword,
317
324
  ].includes(modifier.kind),
318
325
  ) ||
319
- !member.modifiers?.some(
320
- (modifier) => modifier.kind === ts.SyntaxKind.ProtectedKeyword,
321
- )
326
+ !hasModifier(member, ts.SyntaxKind.ProtectedKeyword)
322
327
  ) {
323
328
  this.fail(
324
329
  member,
@@ -326,7 +331,7 @@ export class BlockResolver {
326
331
  "A block implements one protected execute method.",
327
332
  );
328
333
  }
329
- method = member;
334
+ execute = { method: member, body: member.body };
330
335
  continue;
331
336
  }
332
337
  const field = this.collectStateField(member, id);
@@ -336,14 +341,20 @@ export class BlockResolver {
336
341
  initializers.set(field.variable, field.initializer);
337
342
  }
338
343
  }
339
- if (method?.parameters.length !== 1 || method.typeParameters?.length) {
344
+ const parameter = execute?.method.parameters[0];
345
+ if (
346
+ !execute ||
347
+ !parameter ||
348
+ execute.method.parameters.length !== 1 ||
349
+ execute.method.typeParameters?.length
350
+ ) {
340
351
  this.fail(
341
352
  node,
342
353
  "PLC1001",
343
354
  "Block execute requires one concrete input record parameter.",
344
355
  );
345
356
  }
346
- const parameter = method.parameters[0]!;
357
+ const { method, body } = execute;
347
358
  if (
348
359
  !ts.isIdentifier(parameter.name) ||
349
360
  !parameter.type ||
@@ -378,17 +389,14 @@ export class BlockResolver {
378
389
  );
379
390
  }
380
391
  const inputs = inputFields.map((field) => {
381
- // Records and arrays pass by reference, as objects do in TypeScript.
382
- const mode =
383
- field.mode ??
384
- (aggregate(this.types.types.get(field.type)) ? "inOut" : undefined);
392
+ const mode = this.types.parameterMode(field);
385
393
  return {
386
394
  id: `${id}:input:${field.name}`,
387
395
  name: field.name,
388
396
  type: field.type,
389
397
  storage: "parameter" as const,
390
398
  mutable: mode === "inOut",
391
- retain: false,
399
+ retention: "none" as const,
392
400
  span: this.origin(parameter),
393
401
  ...(mode && { mode }),
394
402
  };
@@ -402,14 +410,14 @@ export class BlockResolver {
402
410
  type: field.type,
403
411
  storage: "blockOutput",
404
412
  mutable: false,
405
- retain: false,
413
+ retention: "none",
406
414
  initial: defaultValue(field.type, this.types.types),
407
- span: this.origin(method!),
415
+ span: this.origin(method),
408
416
  }))
409
417
  : [];
410
418
  const block: FunctionBlock = {
411
419
  id,
412
- name: node.name.text,
420
+ name: name.text,
413
421
  inputs,
414
422
  outputs,
415
423
  state,
@@ -419,15 +427,13 @@ export class BlockResolver {
419
427
  ...(result && { result }),
420
428
  };
421
429
  this.definitions.set(id, block);
422
- this.types.types.set(typeId, {
423
- id: typeId,
424
- kind: "block",
425
- block: id,
426
- ...(result && { output: result }),
427
- });
428
- this.sources.set(symbol, {
430
+ if (result) {
431
+ this.types.blockOutput(typeId, id, result);
432
+ }
433
+ const source: BlockSource = {
429
434
  block,
430
- method,
435
+ type: typeId,
436
+ body,
431
437
  methods,
432
438
  inputSymbol: this.types.symbol(parameter.name),
433
439
  inputBindings: new Map(
@@ -435,13 +441,34 @@ export class BlockResolver {
435
441
  ),
436
442
  fieldBindings,
437
443
  initializers,
438
- });
444
+ };
445
+ this.sources.set(symbol, source);
446
+ return source;
439
447
  } finally {
440
448
  this.active.delete(symbol);
441
449
  }
442
450
  }
443
- // Private state fields own storage admission, constructor allocation and initializer capture.
444
- // The enclosing collection keeps schema/execute validation separate from this field policy.
451
+ /** The record an output schema names; undefined for `{}`, a block without outputs. */
452
+ private outputRecord(node: ts.TypeNode): string | undefined {
453
+ if (ts.isTypeLiteralNode(node) && !node.members.length) {
454
+ return undefined;
455
+ }
456
+ const resolved = this.types.resolve(node);
457
+ if (
458
+ resolved.io ||
459
+ resolved.mode ||
460
+ this.types.hasReferences(resolved.type) ||
461
+ this.types.types.get(resolved.type)?.kind !== "record"
462
+ ) {
463
+ this.fail(
464
+ node,
465
+ "PLC2001",
466
+ "Block outputs require a closed value record.",
467
+ );
468
+ }
469
+ return resolved.type;
470
+ }
471
+
445
472
  private collectStateField(
446
473
  member: ts.ClassElement,
447
474
  id: string,
@@ -456,10 +483,8 @@ export class BlockResolver {
456
483
  !member.initializer ||
457
484
  member.questionToken ||
458
485
  member.exclamationToken ||
459
- !member.modifiers?.some(
460
- (modifier) => modifier.kind === ts.SyntaxKind.PrivateKeyword,
461
- ) ||
462
- member.modifiers.some(
486
+ !hasModifier(member, ts.SyntaxKind.PrivateKeyword) ||
487
+ member.modifiers?.some(
463
488
  (modifier) =>
464
489
  ![
465
490
  ts.SyntaxKind.PrivateKeyword,
@@ -478,49 +503,7 @@ export class BlockResolver {
478
503
  this.types,
479
504
  this.fail,
480
505
  );
481
- const instanceArray = ts.isNewExpression(initial.value)
482
- ? undefined
483
- : this.instanceArrayType(member.type, initial.value);
484
- let type: string;
485
- if (ts.isNewExpression(initial.value)) {
486
- type = this.instanceType(initial.value);
487
- } else if (instanceArray) {
488
- type = instanceArray;
489
- } else if (member.type) {
490
- const annotation = this.types.resolve(member.type);
491
- if (
492
- annotation.io ||
493
- annotation.mode ||
494
- this.types.hasReferences(annotation.type)
495
- ) {
496
- this.fail(
497
- member,
498
- "PLC2001",
499
- "Borrow and I/O wrappers cannot be stored in block state.",
500
- );
501
- }
502
- type = annotation.type;
503
- } else if (
504
- initial.value.kind === ts.SyntaxKind.TrueKeyword ||
505
- initial.value.kind === ts.SyntaxKind.FalseKeyword
506
- ) {
507
- type = "BOOL";
508
- } else if (/^-?\d/.test(initial.value.getText())) {
509
- type = numericLiteralType(initial.value.getText());
510
- } else {
511
- this.fail(
512
- member,
513
- "PLC2001",
514
- "Annotate private numeric and aggregate state explicitly.",
515
- );
516
- }
517
- if (member.type && this.types.resolve(member.type).type !== type) {
518
- this.fail(
519
- member,
520
- "PLC2001",
521
- "Instance annotation differs from its constructor.",
522
- );
523
- }
506
+ const type = this.stateType(member, initial.value);
524
507
  const instance = Boolean(instancedBlock(type, this.types.types));
525
508
  if (
526
509
  this.types.types.get(type)?.kind === "block" &&
@@ -537,12 +520,8 @@ export class BlockResolver {
537
520
  name: member.name.text,
538
521
  type,
539
522
  storage: instance ? "instance" : "field",
540
- mutable:
541
- !instance &&
542
- !member.modifiers.some(
543
- (modifier) => modifier.kind === ts.SyntaxKind.ReadonlyKeyword,
544
- ),
545
- retain: initial.retain,
523
+ mutable: !instance && !hasModifier(member, ts.SyntaxKind.ReadonlyKeyword),
524
+ retention: initial.retention,
546
525
  span: this.origin(member),
547
526
  };
548
527
  return {
@@ -551,4 +530,49 @@ export class BlockResolver {
551
530
  ...(!instance && { initializer: initial.value }),
552
531
  };
553
532
  }
533
+
534
+ /** A state field's type: the block or block array it allocates, its annotation, or its literal's type. */
535
+ private stateType(
536
+ member: ts.PropertyDeclaration,
537
+ value: ts.Expression,
538
+ ): string {
539
+ if (ts.isNewExpression(value)) {
540
+ const instance = this.instanceType(value);
541
+ if (member.type && this.types.resolve(member.type).type !== instance) {
542
+ this.fail(
543
+ member,
544
+ "PLC2001",
545
+ "Instance annotation differs from its constructor.",
546
+ );
547
+ }
548
+ return instance;
549
+ }
550
+ const instanceArray = this.instanceArrayType(member.type, value);
551
+ if (instanceArray) {
552
+ return instanceArray;
553
+ }
554
+ if (!member.type) {
555
+ return (
556
+ inferredStorageType(value) ??
557
+ this.fail(
558
+ member,
559
+ "PLC2001",
560
+ "Annotate private numeric and aggregate state explicitly.",
561
+ )
562
+ );
563
+ }
564
+ const annotation = this.types.resolve(member.type);
565
+ if (
566
+ annotation.io ||
567
+ annotation.mode ||
568
+ this.types.hasReferences(annotation.type)
569
+ ) {
570
+ this.fail(
571
+ member,
572
+ "PLC2001",
573
+ "Borrow and I/O wrappers cannot be stored in block state.",
574
+ );
575
+ }
576
+ return annotation.type;
577
+ }
554
578
  }