@tsplc/tmp-compiler 0.0.0-stage → 0.6.2

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 (184) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +41 -2
  3. package/dist/cli.js +2325 -0
  4. package/dist/cli.js.map +26 -0
  5. package/dist/clock.js +11 -0
  6. package/dist/clock.js.map +11 -0
  7. package/dist/codesys.js +6558 -0
  8. package/dist/codesys.js.map +51 -0
  9. package/dist/collections.js +399 -0
  10. package/dist/collections.js.map +10 -0
  11. package/dist/index.js +16076 -0
  12. package/dist/index.js.map +61 -0
  13. package/dist/ir.js +3948 -0
  14. package/dist/ir.js.map +35 -0
  15. package/dist/retain.js +10 -0
  16. package/dist/retain.js.map +10 -0
  17. package/dist/signals.js +115 -0
  18. package/dist/signals.js.map +10 -0
  19. package/dist/telemetry.js +121 -0
  20. package/dist/telemetry.js.map +12 -0
  21. package/package.json +122 -4
  22. package/src/codesys/config.ts +50 -0
  23. package/src/codesys/debug.ts +655 -0
  24. package/src/codesys/emit.ts +185 -0
  25. package/src/codesys/endpoints.ts +111 -0
  26. package/src/codesys/engineering.ts +96 -0
  27. package/src/codesys/explain/expressions.ts +445 -0
  28. package/src/codesys/explain/flags.ts +134 -0
  29. package/src/codesys/explain/parameters.ts +53 -0
  30. package/src/codesys/explain/rows.ts +182 -0
  31. package/src/codesys/explain/tables.ts +79 -0
  32. package/src/codesys/explain/tasks.ts +68 -0
  33. package/src/codesys/explain/writes.ts +497 -0
  34. package/src/codesys/explain.ts +149 -0
  35. package/src/codesys/expressions.ts +100 -0
  36. package/src/codesys/global-lists.ts +284 -0
  37. package/src/codesys/global-names.ts +66 -0
  38. package/src/codesys/http.ts +1078 -0
  39. package/src/codesys/index.ts +13 -0
  40. package/src/codesys/instrumentation.ts +574 -0
  41. package/src/codesys/io-paths.ts +149 -0
  42. package/src/codesys/loops.ts +101 -0
  43. package/src/codesys/manifest.ts +684 -0
  44. package/src/codesys/names.ts +58 -0
  45. package/src/codesys/numeric.ts +516 -0
  46. package/src/codesys/plan.ts +688 -0
  47. package/src/codesys/programs.ts +138 -0
  48. package/src/codesys/publication.ts +99 -0
  49. package/src/codesys/required.ts +8 -0
  50. package/src/codesys/routines.ts +247 -0
  51. package/src/codesys/scan-clock.ts +103 -0
  52. package/src/codesys/scheduling.ts +96 -0
  53. package/src/codesys/service-channels.ts +24 -0
  54. package/src/codesys/sources.ts +118 -0
  55. package/src/codesys/st-files.ts +219 -0
  56. package/src/codesys/statements.ts +409 -0
  57. package/src/codesys/storage.ts +150 -0
  58. package/src/codesys/symbols.ts +240 -0
  59. package/src/codesys/task-access.ts +186 -0
  60. package/src/codesys/telemetry-settings.ts +19 -0
  61. package/src/codesys/telemetry.ts +852 -0
  62. package/src/codesys/types.ts +310 -0
  63. package/src/codesys/vendor.ts +113 -0
  64. package/src/frontend/analyzer.ts +779 -0
  65. package/src/frontend/async.ts +530 -0
  66. package/src/frontend/blocks.ts +578 -0
  67. package/src/frontend/configuration.ts +83 -0
  68. package/src/frontend/const-functions.ts +204 -0
  69. package/src/frontend/control-flow.ts +787 -0
  70. package/src/frontend/cooperative.ts +114 -0
  71. package/src/frontend/coroutines.ts +1336 -0
  72. package/src/frontend/dialect-names.ts +104 -0
  73. package/src/frontend/elaborate/array-searches.ts +224 -0
  74. package/src/frontend/elaborate/assignments.ts +343 -0
  75. package/src/frontend/elaborate/calls.ts +1342 -0
  76. package/src/frontend/elaborate/capture-protocol.ts +237 -0
  77. package/src/frontend/elaborate/capture.ts +388 -0
  78. package/src/frontend/elaborate/declarations.ts +124 -0
  79. package/src/frontend/elaborate/dialect.ts +473 -0
  80. package/src/frontend/elaborate/expressions.ts +2171 -0
  81. package/src/frontend/elaborate/filtered-arrays.ts +643 -0
  82. package/src/frontend/elaborate/frame.ts +145 -0
  83. package/src/frontend/elaborate/generics.ts +123 -0
  84. package/src/frontend/elaborate/graph.ts +141 -0
  85. package/src/frontend/elaborate/imports.ts +105 -0
  86. package/src/frontend/elaborate/index.ts +851 -0
  87. package/src/frontend/elaborate/initializers.ts +235 -0
  88. package/src/frontend/elaborate/iterators.ts +1541 -0
  89. package/src/frontend/elaborate/layouts.ts +1025 -0
  90. package/src/frontend/elaborate/model.ts +478 -0
  91. package/src/frontend/elaborate/names.ts +134 -0
  92. package/src/frontend/elaborate/reduce.ts +900 -0
  93. package/src/frontend/elaborate/references.ts +727 -0
  94. package/src/frontend/elaborate/retention.ts +284 -0
  95. package/src/frontend/elaborate/specialize.ts +1137 -0
  96. package/src/frontend/elaborate/statements.ts +1220 -0
  97. package/src/frontend/elaborate/storage.ts +836 -0
  98. package/src/frontend/elaborate/time-constructors.ts +34 -0
  99. package/src/frontend/elaborate/values.ts +200 -0
  100. package/src/frontend/elaborate/writes.ts +415 -0
  101. package/src/frontend/expressions.ts +1382 -0
  102. package/src/frontend/generated-names.ts +95 -0
  103. package/src/frontend/intrinsics.ts +382 -0
  104. package/src/frontend/lowering-key.ts +23 -0
  105. package/src/frontend/module-graph.ts +156 -0
  106. package/src/frontend/positions.ts +359 -0
  107. package/src/frontend/promise-chains.ts +872 -0
  108. package/src/frontend/promises.ts +419 -0
  109. package/src/frontend/routines.ts +239 -0
  110. package/src/frontend/sdk.ts +187 -0
  111. package/src/frontend/services.ts +609 -0
  112. package/src/frontend/statements.ts +871 -0
  113. package/src/frontend/tasks.ts +526 -0
  114. package/src/frontend/telemetry.ts +174 -0
  115. package/src/frontend/types.ts +1073 -0
  116. package/src/frontend/typescript.ts +651 -0
  117. package/src/index.ts +9 -0
  118. package/src/ir/aggregates.ts +291 -0
  119. package/src/ir/allocation.ts +96 -0
  120. package/src/ir/conditions.ts +855 -0
  121. package/src/ir/core-model.ts +289 -0
  122. package/src/ir/declarations.ts +281 -0
  123. package/src/ir/diagnostics.ts +68 -0
  124. package/src/ir/domains.ts +151 -0
  125. package/src/ir/fold.ts +279 -0
  126. package/src/ir/identity.ts +19 -0
  127. package/src/ir/index.ts +137 -0
  128. package/src/ir/job-errors.ts +69 -0
  129. package/src/ir/layouts.ts +211 -0
  130. package/src/ir/link.ts +302 -0
  131. package/src/ir/model.ts +394 -0
  132. package/src/ir/normalization-error.ts +10 -0
  133. package/src/ir/normalize.ts +52 -0
  134. package/src/ir/numeric.ts +163 -0
  135. package/src/ir/operands.ts +160 -0
  136. package/src/ir/ranges.ts +420 -0
  137. package/src/ir/routine.ts +1116 -0
  138. package/src/ir/scalars.ts +453 -0
  139. package/src/ir/share.ts +790 -0
  140. package/src/ir/short-circuit.ts +140 -0
  141. package/src/ir/tables.ts +111 -0
  142. package/src/ir/temporaries.ts +138 -0
  143. package/src/ir/types.ts +151 -0
  144. package/src/project/artifacts.ts +424 -0
  145. package/src/project/cli.ts +211 -0
  146. package/src/project/debugger.ts +111 -0
  147. package/src/project/deployments.ts +387 -0
  148. package/src/project/flags.ts +14 -0
  149. package/src/project/git.ts +35 -0
  150. package/src/project/plant.ts +629 -0
  151. package/src/project/project-commands.ts +507 -0
  152. package/src/project/project.ts +559 -0
  153. package/src/project/release.ts +152 -0
  154. package/src/project/server-check.ts +54 -0
  155. package/src/project/server.ts +165 -0
  156. package/src/project/terminal.ts +45 -0
  157. package/src/runtime/async.ts +207 -0
  158. package/src/runtime/launch.ts +59 -0
  159. package/src/runtime/main.ts +59 -0
  160. package/src/runtime/missing-file.ts +3 -0
  161. package/src/runtime/process.ts +177 -0
  162. package/src/runtime/program.ts +109 -0
  163. package/src/runtime/retain.ts +108 -0
  164. package/src/runtime/runtime.ts +324 -0
  165. package/src/runtime/settings.ts +27 -0
  166. package/src/runtime/storage.ts +456 -0
  167. package/src/runtime/symbols.ts +97 -0
  168. package/src/runtime/telemetry.ts +154 -0
  169. package/src/runtime/transport.ts +168 -0
  170. package/stdlib/clock.ts +8 -0
  171. package/stdlib/collections.ts +534 -0
  172. package/stdlib/plc-time.ts +2 -0
  173. package/stdlib/retain.ts +4 -0
  174. package/stdlib/signals.ts +142 -0
  175. package/stdlib/telemetry.ts +89 -0
  176. package/types/async.d.ts +87 -0
  177. package/types/clock.d.ts +8 -0
  178. package/types/config.d.ts +2 -0
  179. package/types/data-types.d.ts +31 -0
  180. package/types/dialect.d.ts +49 -0
  181. package/types/globals.d.ts +10 -0
  182. package/types/index.d.ts +212 -0
  183. package/types/retain.d.ts +12 -0
  184. package/types/telemetry.d.ts +47 -0
@@ -0,0 +1,284 @@
1
+ import type { Variable } from "@tsplc/compiler/ir";
2
+ import ts from "typescript";
3
+ import { declaredInSdkModule, namesSdk, sdkModules } from "../sdk";
4
+ import type { CapturedValue } from "./capture-protocol";
5
+ import {
6
+ children,
7
+ type Model,
8
+ type ModelNode,
9
+ type Owner,
10
+ ownerAt,
11
+ type Root,
12
+ tsPath,
13
+ } from "./model";
14
+ import type { Storage } from "./storage";
15
+ import { SpecializeError } from "./values";
16
+
17
+ const unstored =
18
+ "retain retains nothing the PLC stores; retain state scan code writes, or exported data.";
19
+ const unpersisted =
20
+ "persist persists nothing the PLC stores; persist state scan code writes, or exported data.";
21
+
22
+ /** A declaration whose initializer retains a primitive: a module binding or a class field. */
23
+ type RetainedDeclaration = ts.VariableDeclaration | ts.PropertyDeclaration;
24
+
25
+ /** Whether a value of `type` has no identity setup can mark, so retaining it needs its declaration. */
26
+ const primitive = (type: ts.Type): boolean => {
27
+ if (type.isUnion()) {
28
+ return type.types.every(primitive);
29
+ }
30
+ if (type.isIntersection()) {
31
+ return type.types.some(primitive);
32
+ }
33
+ return !(type.flags & (ts.TypeFlags.Object | ts.TypeFlags.NonPrimitive));
34
+ };
35
+
36
+ /** Whether an imported name reaches its declaration through `@tsplc/compiler/retain`. */
37
+ const importedThroughRetain = (
38
+ checker: ts.TypeChecker,
39
+ sdk: string,
40
+ symbol: ts.Symbol | undefined,
41
+ ): boolean =>
42
+ symbol !== undefined &&
43
+ Boolean(symbol.flags & ts.SymbolFlags.Alias) &&
44
+ ((symbol.declarations ?? []).some((declaration) =>
45
+ declaredInSdkModule(sdk, sdkModules.retain, declaration),
46
+ ) ||
47
+ importedThroughRetain(
48
+ checker,
49
+ sdk,
50
+ checker.getImmediateAliasedSymbol(symbol),
51
+ ));
52
+
53
+ /** The module binding a call initializes. */
54
+ const moduleBinding = (
55
+ call: ts.CallExpression,
56
+ ): ts.VariableDeclaration | undefined => {
57
+ const parent = call.parent;
58
+ return ts.isVariableDeclaration(parent) &&
59
+ parent.initializer === call &&
60
+ ts.isVariableDeclarationList(parent.parent) &&
61
+ ts.isVariableStatement(parent.parent.parent) &&
62
+ ts.isSourceFile(parent.parent.parent.parent)
63
+ ? parent
64
+ : undefined;
65
+ };
66
+
67
+ /** The declaration a `retain` call initializes, when it retains a primitive there. */
68
+ const retainedDeclaration = (
69
+ call: ts.CallExpression,
70
+ ): RetainedDeclaration | undefined => {
71
+ const parent = call.parent;
72
+ if (ts.isPropertyDeclaration(parent) && parent.initializer === call) {
73
+ return parent;
74
+ }
75
+ return moduleBinding(call);
76
+ };
77
+
78
+ /** A call of `retain` or `persist`, which only `@tsplc/compiler/retain` gives a value as setup runs under Bun. */
79
+ const checkImported = (
80
+ call: ts.CallExpression,
81
+ name: "persist" | "retain",
82
+ checker: ts.TypeChecker,
83
+ sdk: string,
84
+ ): void => {
85
+ const symbol = checker.getSymbolAtLocation(call.expression);
86
+ if (!symbol || !(symbol.flags & ts.SymbolFlags.Alias)) {
87
+ throw new SpecializeError(
88
+ call,
89
+ `Setup runs under Bun, where the global ${name} has no value: import { ${name} } from "${sdkModules.retain}".`,
90
+ );
91
+ }
92
+ if (!importedThroughRetain(checker, sdk, symbol)) {
93
+ throw new SpecializeError(
94
+ call,
95
+ `Setup runs under Bun, where only ${sdkModules.retain} gives ${name} a value: import { ${name} } from "${sdkModules.retain}".`,
96
+ );
97
+ }
98
+ };
99
+
100
+ /** The declarations whose storage the program keeps through restarts, as its source says. */
101
+ export interface RetentionDeclarations {
102
+ /**
103
+ * The declarations that retain a primitive. Setup marks the objects it retains as it runs, but a
104
+ * primitive has no identity, so its declaration is found here.
105
+ */
106
+ retained: ReadonlySet<ts.Node>;
107
+ /** The module bindings that persist: everything each one stores does. */
108
+ persisted: ReadonlySet<ts.Node>;
109
+ }
110
+
111
+ /**
112
+ * The declarations that retain a primitive or persist a module binding. Checked before setup runs,
113
+ * so a `retain` or `persist` setup can't evaluate is reported where it is written.
114
+ */
115
+ export const retentionDeclarations = (
116
+ sources: readonly ts.SourceFile[],
117
+ checker: ts.TypeChecker,
118
+ sdk: string,
119
+ ): RetentionDeclarations => {
120
+ const retained = new Set<ts.Node>();
121
+ const persisted = new Set<ts.Node>();
122
+ const visit = (node: ts.Node): void => {
123
+ if (
124
+ ts.isCallExpression(node) &&
125
+ namesSdk(checker, sdk, node.expression, "retain")
126
+ ) {
127
+ checkImported(node, "retain", checker, sdk);
128
+ const [argument] = node.arguments;
129
+ if (argument && primitive(checker.getTypeAtLocation(argument))) {
130
+ const declaration = retainedDeclaration(node);
131
+ if (!declaration) {
132
+ throw new SpecializeError(
133
+ node,
134
+ "A primitive is retained only as a module let's or a class field's initializer; retain the record that holds it.",
135
+ );
136
+ }
137
+ retained.add(declaration);
138
+ }
139
+ }
140
+ if (
141
+ ts.isCallExpression(node) &&
142
+ namesSdk(checker, sdk, node.expression, "persist")
143
+ ) {
144
+ checkImported(node, "persist", checker, sdk);
145
+ const binding = moduleBinding(node);
146
+ // CODESYS keeps everything its persistent list declares, so a binding persists whole.
147
+ if (!binding) {
148
+ throw new SpecializeError(
149
+ node,
150
+ "persist keeps a whole module binding: write it as the initializer of a module-level const or let, such as export const settings = persist({ ... }).",
151
+ );
152
+ }
153
+ persisted.add(binding);
154
+ }
155
+ ts.forEachChild(node, visit);
156
+ };
157
+ for (const source of sources) {
158
+ visit(source);
159
+ }
160
+ return { retained, persisted };
161
+ };
162
+
163
+ /**
164
+ * Which storage globals are `RETAIN`, and which `PERSISTENT RETAIN`. CODESYS retains whole
165
+ * variables, so a global is retained when it holds anything setup retained: an object it passed to
166
+ * `retain`, or a field or binding whose declaration retains a primitive. Every global of a binding
167
+ * that persists persists, whatever it holds. Every retained or persisted thing must end up in a
168
+ * global that keeps it.
169
+ */
170
+ export class Retention {
171
+ private readonly objects: ReadonlySet<number>;
172
+ private readonly declarations: ReadonlySet<ts.Node>;
173
+ private readonly persisted: ReadonlySet<ts.Node>;
174
+ private readonly claimed = new Set<number | ts.Node>();
175
+
176
+ constructor(
177
+ private readonly model: Model,
178
+ private readonly storage: Storage,
179
+ { retained, persisted }: RetentionDeclarations,
180
+ ) {
181
+ this.objects = new Set(model.graph.retained);
182
+ this.declarations = retained;
183
+ this.persisted = persisted;
184
+ }
185
+
186
+ /** What the global holding a root, or one field of a flattened root, keeps through restarts. */
187
+ global(root: Root, field: string | null): Variable["retention"] {
188
+ // Claimed first, so what the global holds counts as kept when its binding persists.
189
+ const retained = this.retained(root, field);
190
+ if (this.persisted.has(root.declaration)) {
191
+ this.claimed.add(root.declaration);
192
+ return "persist";
193
+ }
194
+ return retained ? "retain" : "none";
195
+ }
196
+
197
+ /** Whether the global holding a root, or one field of a flattened root, is retained. */
198
+ private retained(root: Root, field: string | null): boolean {
199
+ const node = this.model.node(root.value);
200
+ if (field === null || !node) {
201
+ return this.claim(
202
+ this.declarations.has(root.declaration) ? root.declaration : null,
203
+ this.holds(root.value, { root, steps: [] }),
204
+ );
205
+ }
206
+ const member =
207
+ node.captured.kind === "instance"
208
+ ? this.model.member(this.model.classOf(node), field)
209
+ : undefined;
210
+ const held = this.holds(this.model.field(node, field), {
211
+ root,
212
+ steps: [{ kind: "field", name: field }],
213
+ });
214
+ const declared = member && this.declarations.has(member) ? member : null;
215
+ const whole = this.objects.has(node.id) ? node.id : null;
216
+ return [this.claim(declared, held), this.claim(whole, held)].some(Boolean);
217
+ }
218
+
219
+ /** The error for a retained or persisted thing no global keeps; undefined when each is kept. */
220
+ unclaimed(): Error | undefined {
221
+ const binding = [...this.persisted].find((item) => !this.claimed.has(item));
222
+ if (binding) {
223
+ return new SpecializeError(binding, unpersisted);
224
+ }
225
+ const object = [...this.objects].find((id) => !this.claimed.has(id));
226
+ if (object !== undefined) {
227
+ const owner = this.model.nodes[object]?.owner;
228
+ return new Error(
229
+ `${owner ? tsPath(owner) : "A retained object"}: ${unstored}`,
230
+ );
231
+ }
232
+ const declaration = [...this.declarations].find(
233
+ (item) => !this.claimed.has(item),
234
+ );
235
+ return declaration && new SpecializeError(declaration, unstored);
236
+ }
237
+
238
+ /** Claims a mark for the global being decided, and says whether that global is retained. */
239
+ private claim(mark: number | ts.Node | null, held: boolean): boolean {
240
+ if (mark === null) {
241
+ return held;
242
+ }
243
+ this.claimed.add(mark);
244
+ return true;
245
+ }
246
+
247
+ /**
248
+ * Whether what a global stores at `at` holds something retained, claiming each mark it finds.
249
+ * An object stored elsewhere is that place's to retain.
250
+ */
251
+ private holds(value: CapturedValue, at: Owner): boolean {
252
+ const node = this.model.node(value);
253
+ if (!node || !this.model.ownsAt(node, at)) {
254
+ return false;
255
+ }
256
+ const marks = [
257
+ ...(this.objects.has(node.id) && this.storage.hasState(node)
258
+ ? [node.id]
259
+ : []),
260
+ ...this.retainedMembers(node),
261
+ ];
262
+ for (const mark of marks) {
263
+ this.claimed.add(mark);
264
+ }
265
+ const inner = children(node.captured).map(([step, held]) =>
266
+ this.holds(held, ownerAt(at, step)),
267
+ );
268
+ return marks.length > 0 || inner.some(Boolean);
269
+ }
270
+
271
+ /** The fields of an instance the PLC stores whose declarations retain a primitive. */
272
+ private retainedMembers(node: ModelNode): ts.Node[] {
273
+ if (node.captured.kind !== "instance") {
274
+ return [];
275
+ }
276
+ return this.model
277
+ .fields(this.model.classOf(node))
278
+ .filter(
279
+ (member) =>
280
+ this.declarations.has(member) &&
281
+ this.storage.stored(node, member.name.getText()),
282
+ );
283
+ }
284
+ }