@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,20 +1,28 @@
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 { basename } from "node:path";
2
+ import { pascalCase } from "@tsplc/compiler/ir";
3
3
  import ts from "typescript";
4
- import type { CapturedPrimitive, CapturedValue } from "./capture-protocol";
4
+ import { hasModifier } from "../types";
5
+ import type { CapturedValue } from "./capture-protocol";
5
6
  import type { EntryImports } from "./imports";
6
- import { type Layouts, pascalCase, recordTypes, unusedName } from "./layouts";
7
+ import {
8
+ arrayLayout,
9
+ type Layouts,
10
+ recordTypes,
11
+ statelessLayout,
12
+ } from "./layouts";
7
13
  import {
8
14
  children,
9
15
  type Model,
10
16
  type ModelNode,
11
17
  type Owner,
18
+ ownerAt,
12
19
  type Root,
13
20
  type Step,
21
+ stepPath,
14
22
  tsPath,
15
23
  } from "./model";
16
24
  import { referable, referenceLayout, referenceType } from "./references";
17
- import { constant, type Value } from "./values";
25
+ import { capturedPrimitive, type Value } from "./values";
18
26
  import { ScanWrites } from "./writes";
19
27
 
20
28
  /** A primitive scan code read from setup and folded, at its TypeScript path. */
@@ -27,6 +35,13 @@ interface FoldedField {
27
35
  iec?: string;
28
36
  }
29
37
 
38
+ /** A stored field of a flattened root: a global of the root's storage module. */
39
+ interface RootField {
40
+ name: string;
41
+ layout: string;
42
+ declaration: ts.Node | undefined;
43
+ }
44
+
30
45
  /**
31
46
  * Where the state setup built lives in the PLC, and how scan code reads it. A field is stored when
32
47
  * scan code writes it or its root is exported; what setup fixed otherwise folds where it is read.
@@ -74,10 +89,7 @@ export class Storage {
74
89
  if (!holder.type) {
75
90
  return undefined;
76
91
  }
77
- const variant = this.model.variant(holder.type, holder.captured);
78
- return (
79
- variant.getProperty(name) ?? this.checker.getPropertyOfType(variant, name)
80
- );
92
+ return this.model.variant(holder.type, holder.captured).getProperty(name);
81
93
  }
82
94
 
83
95
  /** Whether a field holds state the PLC stores, rather than a value fixed at setup. */
@@ -88,6 +100,23 @@ export class Storage {
88
100
  return this.written(holder, name);
89
101
  }
90
102
 
103
+ /**
104
+ * Whether a stored field still reads as the value setup gave it: a readonly field scan code never
105
+ * assigns, as a collection's capacity is, which an exported root stores for the HMI to read. A
106
+ * root `io` reaches is the hardware's to write, so none of its fields is fixed.
107
+ */
108
+ private fixed(holder: ModelNode, name: string): boolean {
109
+ const declaration = this.fieldDeclaration(holder, name);
110
+ return (
111
+ holder.owner !== undefined &&
112
+ !holder.owner.root.bound &&
113
+ declaration !== undefined &&
114
+ ts.canHaveModifiers(declaration) &&
115
+ hasModifier(declaration, ts.SyntaxKind.ReadonlyKeyword) &&
116
+ !this.written(holder, name)
117
+ );
118
+ }
119
+
91
120
  /** Whether scan code assigns the field itself. */
92
121
  private written(holder: ModelNode, name: string): boolean {
93
122
  const symbol = this.fieldSymbol(holder, name);
@@ -133,40 +162,43 @@ export class Storage {
133
162
  return { references: [], referenceFields: [] };
134
163
  }
135
164
  return {
136
- references: this.model.nodes.flatMap((node) => {
137
- const path = referable(node) ? this.storagePlace(node) : undefined;
138
- return path === undefined ? [] : [{ id: node.reference, path }];
139
- }),
140
- referenceFields: this.references.flatMap(({ holder, name }) => {
141
- const path = this.storagePlace(holder);
142
- return path === undefined ? [] : [`${path}.${name}`];
143
- }),
165
+ references: this.model.nodes.flatMap((node) =>
166
+ referable(node) && node.owner
167
+ ? [{ id: node.reference, path: this.storagePlace(node.owner) }]
168
+ : [],
169
+ ),
170
+ referenceFields: this.references.flatMap(({ holder, name }) =>
171
+ holder.owner ? [`${this.storagePlace(holder.owner)}.${name}`] : [],
172
+ ),
144
173
  };
145
174
  }
146
175
 
147
- /** A node's place as module storage: its root's module, the root unless flattened, then fields and indexes. */
148
- private storagePlace(node: ModelNode): string | undefined {
149
- const owner = node.owner;
150
- if (!owner) {
151
- return undefined;
152
- }
176
+ /** A place as module storage: its root's module, the root unless flattened, then fields and indexes. */
177
+ private storagePlace(owner: Owner): string {
178
+ const module = basename(this.rootModule(owner.root), ".ts");
153
179
  const root = this.flattened(owner.root)
154
- ? owner.root.name
155
- : `${owner.root.name}.${owner.root.name}`;
156
- return (
157
- root +
158
- owner.steps
159
- .map((step, index) => {
160
- const holder = this.model.at(owner.root, owner.steps.slice(0, index));
161
- if (step.kind === "index") {
162
- return holder && this.mixedStates(holder)
163
- ? `.${mixedItem(step.index)}`
164
- : `[${step.index}]`;
165
- }
166
- return /^\d+$/.test(step.name) ? `[${step.name}]` : `.${step.name}`;
167
- })
168
- .join("")
169
- );
180
+ ? module
181
+ : `${module}.${owner.root.name}`;
182
+ return root + this.stepsText(owner, (_, key) => key);
183
+ }
184
+
185
+ /**
186
+ * The steps from a place's root down to it, as storage spells them: an element of a mixed-state
187
+ * array is a field of its record, and `key` spells a numeric key of the object holding it.
188
+ */
189
+ private stepsText(
190
+ owner: Owner,
191
+ key: (holder: ModelNode | undefined, key: string) => string,
192
+ ): string {
193
+ return owner.steps
194
+ .map((step, index) => {
195
+ const holder = this.model.at(owner.root, owner.steps.slice(0, index));
196
+ if (step.kind === "index" && holder && this.mixedStates(holder)) {
197
+ return `.${mixedItem(step.index)}`;
198
+ }
199
+ return stepPath(step, (name) => key(holder, name));
200
+ })
201
+ .join("");
170
202
  }
171
203
 
172
204
  /** Asked of every field each time an instance's layout is, so answered once. */
@@ -193,37 +225,27 @@ export class Storage {
193
225
  );
194
226
  }
195
227
 
196
- /** Dialect text for a stored node's place. */
228
+ /** Dialect text for a stored node's place. Generated code then reads its root's storage, so this stores the root. */
197
229
  pathText(node: ModelNode): string {
198
230
  const owner = node.owner;
199
231
  if (!owner) {
200
232
  throw new Error("An object with no owner has no storage.");
201
233
  }
202
- this.storedRoots.add(owner.root);
203
- const namespace = this.namespace(owner.root);
234
+ return this.placeText(owner);
235
+ }
236
+
237
+ /** Dialect text for a place, which stores its root; a `Record<Enum, T>` key indexes by its enum member. */
238
+ private placeText(owner: Owner): string {
239
+ const namespace = this.reach(owner.root);
204
240
  const root = this.flattened(owner.root)
205
241
  ? namespace
206
242
  : `${namespace}.${owner.root.name}`;
207
- const steps = owner.steps.map((step, index) =>
208
- this.placeStep(
209
- this.model.at(owner.root, owner.steps.slice(0, index)),
210
- step,
211
- ),
243
+ return (
244
+ root +
245
+ this.stepsText(owner, (holder, key) =>
246
+ holder?.type ? this.keyText(holder.type, key) : key,
247
+ )
212
248
  );
213
- return root + steps.join("");
214
- }
215
-
216
- /** Dialect text for one step down from `holder`: a `Record<Enum, T>` key indexes by its enum member. */
217
- private placeStep(holder: ModelNode | undefined, step: Step): string {
218
- if (step.kind === "index") {
219
- return holder && this.mixedStates(holder)
220
- ? `.${mixedItem(step.index)}`
221
- : `[${step.index}]`;
222
- }
223
- if (/^\d+$/.test(step.name) && holder?.type) {
224
- return `[${this.keyText(holder.type, step.name)}]`;
225
- }
226
- return `.${step.name}`;
227
249
  }
228
250
 
229
251
  /** The enum member naming a key of a `Record<Enum, T>`. */
@@ -232,8 +254,7 @@ export class Storage {
232
254
  if (!keyType) {
233
255
  return key;
234
256
  }
235
- const member = this.layouts.enumMemberName(keyType, Number(key));
236
- return member ? `${this.layouts.text(keyType)}.${member}` : key;
257
+ return this.layouts.enumMember(keyType, Number(key)) ?? key;
237
258
  }
238
259
 
239
260
  /** A record or instance root becomes its own global list, one global per field. */
@@ -244,17 +265,22 @@ export class Storage {
244
265
  );
245
266
  }
246
267
 
247
- private namespace(root: Root): string {
248
- const pascal = pascalCase(basename(this.rootModule(root), ".ts"));
249
- const name = this.typeNames.has(pascal) ? `${pascal}Root` : pascal;
250
- this.imports.namespaces.set(name, this.rootModule(root));
251
- return name;
268
+ /**
269
+ * Stores a root and has the entry module import its storage module, and returns the namespace
270
+ * generated code reads that module through.
271
+ */
272
+ private reach(root: Root): string {
273
+ const module = this.rootModule(root);
274
+ const pascal = pascalCase(basename(module, ".ts"));
275
+ const namespace = this.typeNames.has(pascal) ? `${pascal}Root` : pascal;
276
+ this.storedRoots.add(root);
277
+ this.imports.namespaces.set(namespace, module);
278
+ return namespace;
252
279
  }
253
280
 
254
281
  /** Stores a root whether or not scan code reads it, as exported data must be. */
255
282
  use(root: Root): void {
256
- this.storedRoots.add(root);
257
- this.namespace(root);
283
+ this.reach(root);
258
284
  }
259
285
 
260
286
  /** Roots generated code reached, which need storage declarations. */
@@ -274,10 +300,12 @@ export class Storage {
274
300
  if (captured.kind === "array") {
275
301
  return (
276
302
  this.storedArray(node) ||
277
- captured.items.some((item) => {
303
+ captured.items.some((item, index) => {
278
304
  const inner = this.model.node(item);
279
305
  return (
280
- inner !== undefined && isChild(inner, node) && this.hasState(inner)
306
+ inner !== undefined &&
307
+ this.owns(node, inner, { kind: "index", index }) &&
308
+ this.hasState(inner)
281
309
  );
282
310
  })
283
311
  );
@@ -290,7 +318,7 @@ export class Storage {
290
318
  const inner = this.model.node(value);
291
319
  return (
292
320
  inner !== undefined &&
293
- isChild(inner, node, name) &&
321
+ this.owns(node, inner, { kind: "field", name }) &&
294
322
  this.hasState(inner)
295
323
  );
296
324
  });
@@ -312,6 +340,7 @@ export class Storage {
312
340
  return this.layoutName(
313
341
  `${captured.name}State`,
314
342
  fields.map(([field, text]) => `${field}: ${text};`).join(" "),
343
+ this.model.classOf(node),
315
344
  );
316
345
  }
317
346
  if (captured.kind === "array") {
@@ -323,29 +352,25 @@ export class Storage {
323
352
  "MixedStates",
324
353
  states
325
354
  .map(
326
- (state, index) => `${mixedItem(index)}: ${state ?? "boolean"};`,
355
+ (state, index) =>
356
+ `${mixedItem(index)}: ${state ?? statelessLayout};`,
327
357
  )
328
358
  .join(" "),
359
+ this.holderDeclaration(node),
329
360
  );
330
361
  }
331
- if (first?.captured.kind === "instance" && states) {
332
- if (new Set(states).size !== 1) {
333
- return this.layoutName(
334
- "MixedStates",
335
- states
336
- .map(
337
- (state, index) => `${mixedItem(index)}: ${state ?? "boolean"};`,
338
- )
339
- .join(" "),
340
- );
341
- }
342
- return `FixedArray<${states[0]}, ${captured.items.length}>`;
362
+ const state = states?.[0];
363
+ if (first?.captured.kind === "instance" && state) {
364
+ return arrayLayout(state, captured.items.length).text;
343
365
  }
344
366
  }
345
367
  if (!node.type) {
346
368
  throw new Error("Stored state needs a declared type.");
347
369
  }
348
- return this.layouts.text(node.type, { kind: "node", id: node.id });
370
+ return this.layouts.declaredText(this.holderDeclaration(node), node.type, {
371
+ kind: "node",
372
+ id: node.id,
373
+ });
349
374
  }
350
375
 
351
376
  /**
@@ -385,15 +410,15 @@ export class Storage {
385
410
  if (captured.kind !== "array") {
386
411
  return undefined;
387
412
  }
388
- const items = captured.items.map((item) => this.model.node(item));
389
- if (items.some((item) => !item)) {
413
+ const items = captured.items.flatMap((item) => {
414
+ const inner = this.model.node(item);
415
+ return inner ? [inner] : [];
416
+ });
417
+ if (items.length !== captured.items.length) {
390
418
  return undefined;
391
419
  }
392
420
  const element = node.type && this.model.elementType(node.type);
393
421
  return items.map((item) => {
394
- if (!item) {
395
- return undefined;
396
- }
397
422
  if (item.captured.kind === "instance") {
398
423
  return this.storageType(item);
399
424
  }
@@ -417,15 +442,17 @@ export class Storage {
417
442
  const fields = this.model.fields(this.model.classOf(node));
418
443
  return fields.flatMap((member): [string, string][] => {
419
444
  const name = member.name.getText();
420
- const value = captured.fields[name] ?? { kind: "undefined" };
445
+ const value = this.model.field(node, name);
421
446
  const inner = this.model.node(value);
422
- // A generic class's instance stores its fields at its own type arguments.
423
- const type =
424
- this.model.capturedFieldType(node, name) ??
425
- this.checker.getTypeAtLocation(member.name);
447
+ const type = this.model.capturedFieldType(node, name);
448
+ if (!type) {
449
+ throw new Error(`${captured.name}.${name} has no declared type.`);
450
+ }
451
+ const owned =
452
+ inner !== undefined && this.owns(node, inner, { kind: "field", name });
426
453
  if (this.referenceField(node, name)) {
427
454
  // The object setup put there is stored through this field, which a reference would replace.
428
- if (inner && isChild(inner, node, name)) {
455
+ if (owned) {
429
456
  throw new Error(
430
457
  `${captured.name}.${name} is assigned in scan code, but it is where setup first put the object it starts with, so that object is stored there. Give the object another owner: a module constant, or a readonly field setup assigns before this one.`,
431
458
  );
@@ -433,22 +460,23 @@ export class Storage {
433
460
  return [[name, referenceLayout]];
434
461
  }
435
462
  if (inner) {
436
- if (
437
- inner.captured.kind === "function" ||
438
- inner.captured.kind === "closure" ||
439
- inner.captured.kind === "service"
440
- ) {
463
+ if (callable(inner)) {
441
464
  return [];
442
465
  }
443
- if (!isChild(inner, node, name)) {
466
+ if (!owned) {
444
467
  if (!this.stored(node, name)) {
445
468
  return [];
446
469
  }
447
- // A stored field holds its own copy of the record setup gave it.
448
- return [[name, this.layouts.text(type, value)]];
470
+ // A stored field holds its own copy of the record setup gave it. A copied instance
471
+ // keeps the record its class's type describes, which emitted ST names `ST_<Class>`.
472
+ const layout =
473
+ inner.captured.kind === "instance"
474
+ ? this.layouts.text(type, value)
475
+ : this.layouts.declaredText(member, type, value);
476
+ return [[name, layout]];
449
477
  }
450
478
  if (inner.captured.kind !== "instance" && this.stored(node, name)) {
451
- return [[name, this.layouts.text(type, value)]];
479
+ return [[name, this.layouts.declaredText(member, type, value)]];
452
480
  }
453
481
  const text = this.storageType(inner);
454
482
  return text ? [[name, text]] : [];
@@ -460,19 +488,70 @@ export class Storage {
460
488
  });
461
489
  }
462
490
 
463
- private layoutName(base: string, body: string): string {
491
+ /** A flattened root's stored fields, each a global of its storage module; an exported record stores every field. */
492
+ rootFields(root: Root): RootField[] {
493
+ const node = this.model.node(root.value);
494
+ if (node?.captured.kind === "instance") {
495
+ const declaration = this.model.classOf(node);
496
+ return this.instanceFields(node).map(([name, layout]) => ({
497
+ name,
498
+ layout,
499
+ declaration: this.model.member(declaration, name),
500
+ }));
501
+ }
502
+ if (node?.captured.kind !== "object") {
503
+ return [];
504
+ }
505
+ return Object.entries(node.captured.fields).flatMap(
506
+ ([name, value]): RootField[] => {
507
+ const declared = this.model.capturedFieldType(node, name);
508
+ if (!declared) {
509
+ return [];
510
+ }
511
+ const declaration = this.fieldDeclaration(node, name);
512
+ const inner = this.model.node(value);
513
+ if (inner && callable(inner)) {
514
+ return [];
515
+ }
516
+ if (root.exported || (!inner && this.stored(node, name))) {
517
+ const hint = name.charAt(0).toUpperCase() + name.slice(1);
518
+ return [
519
+ {
520
+ name,
521
+ layout: this.layouts.declaredText(
522
+ declaration,
523
+ declared,
524
+ value,
525
+ hint,
526
+ ),
527
+ declaration,
528
+ },
529
+ ];
530
+ }
531
+ if (!inner) {
532
+ return [];
533
+ }
534
+ // A record scan code assigns as a whole holds its own copy of what setup gave it.
535
+ const layout =
536
+ inner.captured.kind === "object" && this.stored(node, name)
537
+ ? this.layouts.declaredText(declaration, declared, value)
538
+ : this.storageType(inner);
539
+ return layout ? [{ name, layout, declaration }] : [];
540
+ },
541
+ );
542
+ }
543
+
544
+ private layoutName(
545
+ base: string,
546
+ body: string,
547
+ origin: ts.Node | undefined,
548
+ ): string {
464
549
  const known = this.layoutNames.get(body);
465
550
  if (known) {
466
551
  return known;
467
552
  }
468
- const name = unusedName(
469
- base,
470
- (candidate) =>
471
- [...this.layoutNames.values()].includes(candidate) ||
472
- this.layouts.declarations.has(candidate),
473
- );
553
+ const name = this.layouts.declare(base, body, origin);
474
554
  this.layoutNames.set(body, name);
475
- this.layouts.declarations.set(name, `export interface ${name} { ${body} }`);
476
555
  return name;
477
556
  }
478
557
 
@@ -480,7 +559,12 @@ export class Storage {
480
559
  const value = root.value;
481
560
  if (value.kind === "node") {
482
561
  const node = this.model.node(value);
483
- if (this.kept(root.file) && node.captured.kind !== "service") {
562
+ // Scan code calls a service client or a task, so it stays an object setup built.
563
+ if (
564
+ this.kept(root.file) &&
565
+ node.captured.kind !== "service" &&
566
+ node.captured.kind !== "task"
567
+ ) {
484
568
  return this.keptRoot(root);
485
569
  }
486
570
  return { kind: "static", node };
@@ -489,18 +573,18 @@ export class Storage {
489
573
  this.kept(root.file) ||
490
574
  ts.getCombinedNodeFlags(root.declaration) & ts.NodeFlags.Const
491
575
  ) {
492
- return primitive(value);
576
+ return capturedPrimitive(value);
493
577
  }
494
578
  // A module `let` of a plain module is state in its own global list.
495
- const namespace = this.namespace(root);
496
- this.storedRoots.add(root);
579
+ const namespace = this.reach(root);
580
+ const input = this.input({ root, steps: [] });
497
581
  return {
498
582
  kind: "code",
499
583
  text: `${namespace}.${root.name}`,
500
584
  declared: this.checker.getTypeAtLocation(root.declaration.name),
501
585
  place: true,
502
586
  iec: this.layouts.iecName(root.declaration),
503
- setter: `${namespace}.${setterName(root.name)}`,
587
+ ...(input && { input }),
504
588
  };
505
589
  }
506
590
 
@@ -529,30 +613,23 @@ export class Storage {
529
613
  step.kind === "field" &&
530
614
  this.written(holder, step.name)
531
615
  ) {
532
- return this.storedField(
533
- holder,
534
- step,
535
- node.captured.kind === "array" ? value : undefined,
536
- );
616
+ return node.captured.kind === "array"
617
+ ? this.storedArrayField(holder, step.name, node)
618
+ : this.storedField(holder, step);
537
619
  }
538
620
  return { kind: "static", node };
539
621
  }
540
622
  const stored =
541
623
  step.kind === "field"
542
- ? this.stored(holder, step.name)
624
+ ? this.stored(holder, step.name) && !this.fixed(holder, step.name)
543
625
  : this.storedArray(holder);
544
626
  if (stored) {
545
627
  return this.storedField(holder, step);
546
628
  }
547
629
  if (holder.owner) {
548
- this.readConstants.add(
549
- tsPath({
550
- root: holder.owner.root,
551
- steps: [...holder.owner.steps, step],
552
- }),
553
- );
630
+ this.readConstants.add(tsPath(ownerAt(holder.owner, step)));
554
631
  }
555
- return primitive(value);
632
+ return capturedPrimitive(value);
556
633
  }
557
634
 
558
635
  /** A captured field's declaration: a class member or a record's property. */
@@ -570,9 +647,9 @@ export class Storage {
570
647
  return this.layouts.iecName(this.fieldDeclaration(holder, name));
571
648
  }
572
649
 
573
- /** The declaration of the field holding an array. */
574
- private arrayDeclaration(array: ModelNode): ts.Node | undefined {
575
- const owner = array.owner;
650
+ /** The declaration of the field holding a record or array. */
651
+ private holderDeclaration(node: ModelNode): ts.Node | undefined {
652
+ const owner = node.owner;
576
653
  const last = owner?.steps.at(-1);
577
654
  if (!owner || last?.kind !== "field") {
578
655
  return undefined;
@@ -583,7 +660,7 @@ export class Storage {
583
660
 
584
661
  /** The IEC width of an array's elements, from the declaration holding the array. */
585
662
  private arrayElementIec(array: ModelNode): string | undefined {
586
- return this.layouts.elementIec(this.arrayDeclaration(array));
663
+ return this.layouts.elementIec(this.holderDeclaration(array));
587
664
  }
588
665
 
589
666
  /**
@@ -602,14 +679,11 @@ export class Storage {
602
679
  this.storedWhole(node)) ||
603
680
  (captured.kind === "array" && this.storedArray(node));
604
681
  for (const [step, value] of children(captured)) {
605
- const at: Owner = { root: owner.root, steps: [...owner.steps, step] };
682
+ const at = ownerAt(owner, step);
606
683
  const inner = this.model.node(value);
607
684
  if (inner) {
608
685
  // Visit what this node owns; other places holding it refer to its owner.
609
- if (
610
- inner.owner?.root === at.root &&
611
- tsPath(inner.owner) === tsPath(at)
612
- ) {
686
+ if (this.model.ownsAt(inner, at)) {
613
687
  visit(inner, at, stores);
614
688
  }
615
689
  continue;
@@ -627,7 +701,7 @@ export class Storage {
627
701
  const declaration =
628
702
  step.kind === "field"
629
703
  ? this.fieldDeclaration(node, step.name)
630
- : this.arrayDeclaration(node);
704
+ : this.holderDeclaration(node);
631
705
  const iec =
632
706
  step.kind === "field"
633
707
  ? this.fieldIec(node, step.name)
@@ -653,25 +727,22 @@ export class Storage {
653
727
  return found;
654
728
  }
655
729
 
656
- /** `array` is the array setup put in the field, whose length an array parameter takes when the field is passed to one. */
657
- private storedField(
658
- holder: ModelNode,
659
- step: Step,
660
- array?: CapturedValue,
661
- ): Value {
730
+ private storedField(holder: ModelNode, step: Step): Value {
731
+ const owner = holder.owner;
732
+ if (!owner) {
733
+ throw new Error("An object with no owner has no storage.");
734
+ }
662
735
  const declared =
663
736
  step.kind === "index"
664
737
  ? holder.type && this.model.elementType(holder.type)
665
738
  : this.model.capturedFieldType(holder, step.name);
666
- const global =
667
- step.kind === "field" &&
668
- holder.owner?.steps.length === 0 &&
669
- this.flattened(holder.owner.root);
739
+ const input = this.input(ownerAt(owner, step));
670
740
  return {
671
741
  kind: "code",
672
- text: `${this.pathText(holder)}${this.placeStep(holder, step)}`,
742
+ text: this.placeText(ownerAt(owner, step)),
673
743
  declared,
674
744
  place: true,
745
+ ...(input && { input }),
675
746
  ...(step.kind === "field"
676
747
  ? {
677
748
  iec: this.fieldIec(holder, step.name),
@@ -680,29 +751,56 @@ export class Storage {
680
751
  ),
681
752
  }
682
753
  : { iec: this.arrayElementIec(holder) }),
683
- ...(global && {
684
- setter: `${this.pathText(holder)}.${setterName(step.name)}`,
685
- }),
686
- ...(array &&
687
- declared &&
688
- this.checker.isArrayType(declared) &&
689
- step.kind === "field" && {
690
- layout: this.layouts.declaredText(
691
- this.fieldDeclaration(holder, step.name),
692
- declared,
693
- array,
694
- ),
695
- length: this.capturedLength(array),
696
- }),
697
754
  };
698
755
  }
699
756
 
700
- private capturedLength(array: CapturedValue): number | undefined {
701
- if (array.kind !== "node") {
702
- return undefined;
757
+ /** An input the hardware writes at `place` or inside it, which scan code must not assign. */
758
+ private input(place: Owner): { path: string; address: string } | undefined {
759
+ const path = tsPath(place);
760
+ for (const [bound, address] of this.model.inputs) {
761
+ if (
762
+ bound === path ||
763
+ bound.startsWith(`${path}.`) ||
764
+ bound.startsWith(`${path}[`)
765
+ ) {
766
+ return { path: bound, address };
767
+ }
703
768
  }
704
- const captured = this.model.node(array).captured;
705
- return captured.kind === "array" ? captured.items.length : undefined;
769
+ return undefined;
770
+ }
771
+
772
+ /** A stored array field, with the layout and length of the array setup put there, which an array parameter takes when the field is passed to one. */
773
+ private storedArrayField(
774
+ holder: ModelNode,
775
+ name: string,
776
+ array: ModelNode,
777
+ ): Value {
778
+ const stored = this.storedField(holder, { kind: "field", name });
779
+ if (
780
+ stored.kind !== "code" ||
781
+ !stored.declared ||
782
+ !this.checker.isArrayType(stored.declared) ||
783
+ array.captured.kind !== "array"
784
+ ) {
785
+ return stored;
786
+ }
787
+ return {
788
+ ...stored,
789
+ layout: this.layouts.declaredArray(
790
+ this.fieldDeclaration(holder, name),
791
+ stored.declared,
792
+ { kind: "node", id: array.id },
793
+ ),
794
+ length: array.captured.items.length,
795
+ };
796
+ }
797
+
798
+ /** Whether `holder` owns `node` at `step`. */
799
+ private owns(holder: ModelNode, node: ModelNode, step: Step): boolean {
800
+ return (
801
+ holder.owner !== undefined &&
802
+ this.model.ownsAt(node, ownerAt(holder.owner, step))
803
+ );
706
804
  }
707
805
 
708
806
  storedNode(node: ModelNode): boolean {
@@ -712,47 +810,16 @@ export class Storage {
712
810
  }
713
811
  }
714
812
 
715
- /** A primitive setup captured, as a value known while specializing. */
716
- const primitive = (value: CapturedPrimitive): Value => {
717
- switch (value.kind) {
718
- case "undefined":
719
- return constant(undefined);
720
- case "bigint":
721
- return constant(BigInt(value.value));
722
- default:
723
- return constant(value.value);
724
- }
725
- };
726
-
727
- /** The function a storage module exports to set one of its globals. */
728
- export const setterName = (name: string): string => `tsplcSet_${name}`;
729
-
730
- /** Whether `holder` owns `node` directly, at its field `field` when one is given. */
731
- const isChild = (
732
- node: ModelNode,
733
- holder: ModelNode,
734
- field?: string,
735
- ): boolean => {
736
- const owner = node.owner;
737
- const parent = holder.owner;
738
- if (!owner || !parent || owner.root !== parent.root) {
739
- return false;
740
- }
741
- const last = owner.steps.at(-1);
742
- const path = owner.steps.slice(0, -1).map(stepText);
743
- const parentPath = parent.steps.map(stepText);
744
- if (
745
- last === undefined ||
746
- path.length !== parentPath.length ||
747
- path.some((text, index) => text !== parentPath[index])
748
- ) {
749
- return false;
750
- }
751
- return field === undefined || (last.kind === "field" && last.name === field);
752
- };
753
-
754
- const stepText = (step: Step): string =>
755
- step.kind === "index" ? `[${step.index}]` : `.${step.name}`;
813
+ /** Whether setup built a function, service client or task: scan code calls it, and the PLC stores nothing for it. */
814
+ const callable = (node: ModelNode): boolean =>
815
+ node.captured.kind === "function" ||
816
+ node.captured.kind === "closure" ||
817
+ node.captured.kind === "service" ||
818
+ node.captured.kind === "task";
756
819
 
757
820
  /** The field of a mixed-state array's record that holds element `index`. */
758
821
  const mixedItem = (index: number): string => `item${index}`;
822
+
823
+ /** A mixed-state array's record holding each element's value, given as text. */
824
+ export const mixedStateText = (items: readonly string[]): string =>
825
+ `{ ${items.map((item, index) => `${mixedItem(index)}: ${item}`).join(", ")} }`;