@tsplc/tmp-compiler 0.0.0-stage → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +41 -2
- package/dist/cli.js +2325 -0
- package/dist/cli.js.map +26 -0
- package/dist/clock.js +11 -0
- package/dist/clock.js.map +11 -0
- package/dist/codesys.js +6563 -0
- package/dist/codesys.js.map +51 -0
- package/dist/collections.js +399 -0
- package/dist/collections.js.map +10 -0
- package/dist/index.js +16198 -0
- package/dist/index.js.map +62 -0
- package/dist/ir.js +3948 -0
- package/dist/ir.js.map +35 -0
- package/dist/retain.js +10 -0
- package/dist/retain.js.map +10 -0
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +121 -0
- package/dist/telemetry.js.map +12 -0
- package/package.json +122 -4
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +655 -0
- package/src/codesys/emit.ts +185 -0
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +96 -0
- package/src/codesys/explain/expressions.ts +445 -0
- package/src/codesys/explain/flags.ts +134 -0
- package/src/codesys/explain/parameters.ts +53 -0
- package/src/codesys/explain/rows.ts +182 -0
- package/src/codesys/explain/tables.ts +79 -0
- package/src/codesys/explain/tasks.ts +68 -0
- package/src/codesys/explain/writes.ts +497 -0
- package/src/codesys/explain.ts +149 -0
- package/src/codesys/expressions.ts +100 -0
- package/src/codesys/global-lists.ts +284 -0
- package/src/codesys/global-names.ts +66 -0
- package/src/codesys/http.ts +1090 -0
- package/src/codesys/index.ts +13 -0
- package/src/codesys/instrumentation.ts +574 -0
- package/src/codesys/io-paths.ts +149 -0
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +684 -0
- package/src/codesys/names.ts +58 -0
- package/src/codesys/numeric.ts +516 -0
- package/src/codesys/plan.ts +688 -0
- package/src/codesys/programs.ts +138 -0
- package/src/codesys/publication.ts +99 -0
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +247 -0
- package/src/codesys/scan-clock.ts +103 -0
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +24 -0
- package/src/codesys/sources.ts +118 -0
- package/src/codesys/st-files.ts +219 -0
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +150 -0
- package/src/codesys/symbols.ts +240 -0
- package/src/codesys/task-access.ts +186 -0
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +852 -0
- package/src/codesys/types.ts +310 -0
- package/src/codesys/vendor.ts +113 -0
- package/src/frontend/analyzer.ts +779 -0
- package/src/frontend/async.ts +530 -0
- package/src/frontend/blocks.ts +578 -0
- package/src/frontend/configuration.ts +83 -0
- package/src/frontend/const-enums.ts +196 -0
- package/src/frontend/const-functions.ts +204 -0
- package/src/frontend/control-flow.ts +787 -0
- package/src/frontend/cooperative.ts +114 -0
- package/src/frontend/coroutines.ts +1344 -0
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +224 -0
- package/src/frontend/elaborate/assignments.ts +343 -0
- package/src/frontend/elaborate/calls.ts +1342 -0
- package/src/frontend/elaborate/capture-protocol.ts +237 -0
- package/src/frontend/elaborate/capture.ts +388 -0
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +473 -0
- package/src/frontend/elaborate/expressions.ts +2177 -0
- package/src/frontend/elaborate/filtered-arrays.ts +643 -0
- package/src/frontend/elaborate/frame.ts +145 -0
- package/src/frontend/elaborate/generics.ts +123 -0
- package/src/frontend/elaborate/graph.ts +141 -0
- package/src/frontend/elaborate/imports.ts +105 -0
- package/src/frontend/elaborate/index.ts +851 -0
- package/src/frontend/elaborate/initializers.ts +235 -0
- package/src/frontend/elaborate/iterators.ts +1541 -0
- package/src/frontend/elaborate/layouts.ts +1025 -0
- package/src/frontend/elaborate/model.ts +478 -0
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/references.ts +727 -0
- package/src/frontend/elaborate/retention.ts +284 -0
- package/src/frontend/elaborate/specialize.ts +1137 -0
- package/src/frontend/elaborate/statements.ts +1220 -0
- package/src/frontend/elaborate/storage.ts +836 -0
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +200 -0
- package/src/frontend/elaborate/writes.ts +415 -0
- package/src/frontend/expressions.ts +1382 -0
- package/src/frontend/generated-names.ts +95 -0
- package/src/frontend/intrinsics.ts +382 -0
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +156 -0
- package/src/frontend/positions.ts +359 -0
- package/src/frontend/promise-chains.ts +872 -0
- package/src/frontend/promises.ts +419 -0
- package/src/frontend/routines.ts +239 -0
- package/src/frontend/sdk.ts +187 -0
- package/src/frontend/services.ts +612 -0
- package/src/frontend/statements.ts +871 -0
- package/src/frontend/tasks.ts +526 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +1073 -0
- package/src/frontend/typescript.ts +659 -0
- package/src/index.ts +9 -0
- package/src/ir/aggregates.ts +291 -0
- package/src/ir/allocation.ts +96 -0
- package/src/ir/conditions.ts +855 -0
- package/src/ir/core-model.ts +289 -0
- package/src/ir/declarations.ts +281 -0
- package/src/ir/diagnostics.ts +68 -0
- package/src/ir/domains.ts +151 -0
- package/src/ir/fold.ts +279 -0
- package/src/ir/identity.ts +19 -0
- package/src/ir/index.ts +137 -0
- package/src/ir/job-errors.ts +69 -0
- package/src/ir/layouts.ts +211 -0
- package/src/ir/link.ts +302 -0
- package/src/ir/model.ts +394 -0
- package/src/ir/normalization-error.ts +10 -0
- package/src/ir/normalize.ts +52 -0
- package/src/ir/numeric.ts +163 -0
- package/src/ir/operands.ts +160 -0
- package/src/ir/ranges.ts +420 -0
- package/src/ir/routine.ts +1116 -0
- package/src/ir/scalars.ts +453 -0
- package/src/ir/share.ts +790 -0
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +138 -0
- package/src/ir/types.ts +151 -0
- package/src/project/artifacts.ts +424 -0
- package/src/project/cli.ts +211 -0
- package/src/project/debugger.ts +111 -0
- package/src/project/deployments.ts +387 -0
- package/src/project/flags.ts +14 -0
- package/src/project/git.ts +35 -0
- package/src/project/plant.ts +629 -0
- package/src/project/project-commands.ts +507 -0
- package/src/project/project.ts +559 -0
- package/src/project/release.ts +152 -0
- package/src/project/server-check.ts +54 -0
- package/src/project/server.ts +165 -0
- package/src/project/terminal.ts +45 -0
- package/src/runtime/async.ts +207 -0
- package/src/runtime/launch.ts +59 -0
- package/src/runtime/main.ts +59 -0
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +177 -0
- package/src/runtime/program.ts +109 -0
- package/src/runtime/retain.ts +108 -0
- package/src/runtime/runtime.ts +324 -0
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +456 -0
- package/src/runtime/symbols.ts +97 -0
- package/src/runtime/telemetry.ts +154 -0
- package/src/runtime/transport.ts +168 -0
- package/stdlib/clock.ts +8 -0
- package/stdlib/collections.ts +534 -0
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +4 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +89 -0
- package/types/async.d.ts +87 -0
- package/types/clock.d.ts +8 -0
- package/types/config.d.ts +2 -0
- package/types/data-types.d.ts +31 -0
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +10 -0
- package/types/index.d.ts +212 -0
- package/types/retain.d.ts +12 -0
- package/types/telemetry.d.ts +47 -0
|
@@ -0,0 +1,530 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import {
|
|
3
|
+
type Diagnostic,
|
|
4
|
+
error,
|
|
5
|
+
type FunctionBlock,
|
|
6
|
+
type PlcProgram,
|
|
7
|
+
} from "@tsplc/compiler/ir";
|
|
8
|
+
import ts from "typescript";
|
|
9
|
+
import { lowerCoroutine } from "./coroutines";
|
|
10
|
+
import { GeneratedNames } from "./generated-names";
|
|
11
|
+
import {
|
|
12
|
+
blockArrayElement,
|
|
13
|
+
importSpecifier,
|
|
14
|
+
type NamedRoutine,
|
|
15
|
+
namedRoutines,
|
|
16
|
+
plcSources,
|
|
17
|
+
} from "./module-graph";
|
|
18
|
+
import {
|
|
19
|
+
type Edit,
|
|
20
|
+
edited,
|
|
21
|
+
from,
|
|
22
|
+
joined,
|
|
23
|
+
type Mapped,
|
|
24
|
+
mapped,
|
|
25
|
+
type Positions,
|
|
26
|
+
slice,
|
|
27
|
+
} from "./positions";
|
|
28
|
+
import { namesSdk, symbolAt } from "./sdk";
|
|
29
|
+
import { lowerTaskDefinitions } from "./tasks";
|
|
30
|
+
import { hasModifier, SemanticError } from "./types";
|
|
31
|
+
|
|
32
|
+
export type Job = NonNullable<FunctionBlock["job"]>;
|
|
33
|
+
type Runtime = NonNullable<FunctionBlock["runtime"]>;
|
|
34
|
+
type Schedule = NonNullable<PlcProgram["background"]>;
|
|
35
|
+
type Continuation = NonNullable<PlcProgram["continuation"]>;
|
|
36
|
+
|
|
37
|
+
export interface AsyncLowering {
|
|
38
|
+
sources: Map<string, Mapped>;
|
|
39
|
+
blocks: Map<string, Runtime>;
|
|
40
|
+
/** The async function each generated block runs, by lowering key. */
|
|
41
|
+
jobs: Map<string, Job>;
|
|
42
|
+
programs: Record<string, { module: string; export: string }>;
|
|
43
|
+
schedules: Map<string, Schedule>;
|
|
44
|
+
diagnostics: Diagnostic[];
|
|
45
|
+
continuations: Map<string, Continuation>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The names of the members an async method's coroutine adds to its block. */
|
|
49
|
+
export interface MethodMembers {
|
|
50
|
+
start: string;
|
|
51
|
+
reset: string;
|
|
52
|
+
snapshot: string;
|
|
53
|
+
advance: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The type aliases a routine's module exports for its result and arguments, so other modules can name them. */
|
|
57
|
+
interface RoutineAliases {
|
|
58
|
+
result: string;
|
|
59
|
+
argument: (index: number) => string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** What every part of async lowering shares while it rewrites the program. */
|
|
63
|
+
export interface AsyncContext {
|
|
64
|
+
checker: ts.TypeChecker;
|
|
65
|
+
sdk: string;
|
|
66
|
+
positions: Positions;
|
|
67
|
+
/** Async routines by symbol. */
|
|
68
|
+
functions: ReadonlyMap<ts.Symbol, NamedRoutine>;
|
|
69
|
+
/** Async methods by symbol, with the members their coroutine adds. */
|
|
70
|
+
methods: ReadonlyMap<ts.Symbol, MethodMembers>;
|
|
71
|
+
symbol: (node: ts.Node) => ts.Symbol | undefined;
|
|
72
|
+
/** Whether a node names the SDK declaration `name`. */
|
|
73
|
+
namesSdk: (node: ts.Node, name: string) => boolean;
|
|
74
|
+
fail: (node: ts.Node, message: string) => never;
|
|
75
|
+
/** Names for generated declarations, from the source node each stands for. */
|
|
76
|
+
names: GeneratedNames;
|
|
77
|
+
aliases: (fn: NamedRoutine) => RoutineAliases;
|
|
78
|
+
importAliases: typeof importAliases;
|
|
79
|
+
/** Whether code anywhere in the program reads the binding `declaration` declares. */
|
|
80
|
+
referenced: (declaration: ts.VariableDeclaration) => boolean;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A background job a block's instances serve: the block method that serves it, and its schedule. */
|
|
84
|
+
interface OwnedJob {
|
|
85
|
+
method: string;
|
|
86
|
+
schedule: Schedule;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* One source file being rewritten: edits to its text, declarations added around it, and the
|
|
91
|
+
* records of what it generates, which count only once the whole source has been rewritten.
|
|
92
|
+
*/
|
|
93
|
+
export interface SourceRewrite {
|
|
94
|
+
source: ts.SourceFile;
|
|
95
|
+
replacements: Edit[];
|
|
96
|
+
extra: Mapped[];
|
|
97
|
+
/** The routine aliases its imports name. */
|
|
98
|
+
importedAliases: Set<string>;
|
|
99
|
+
blocks: Map<string, Runtime>;
|
|
100
|
+
jobs: Map<string, Job>;
|
|
101
|
+
programs: AsyncLowering["programs"];
|
|
102
|
+
schedules: Map<string, Schedule>;
|
|
103
|
+
continuations: Map<string, Continuation>;
|
|
104
|
+
/** The polls and background jobs of the blocks it declares, which their instances run, by block symbol. */
|
|
105
|
+
ownedPolls: Map<ts.Symbol, string[]>;
|
|
106
|
+
ownedJobs: Map<ts.Symbol, OwnedJob[]>;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const asyncDiagnostic = (
|
|
110
|
+
positions: Positions,
|
|
111
|
+
node: ts.Node,
|
|
112
|
+
message: string,
|
|
113
|
+
): Diagnostic => error("PLC1010", message, positions.node(node));
|
|
114
|
+
|
|
115
|
+
/** Lower suspension into ordinary typed blocks before the PLC semantic pass. */
|
|
116
|
+
export const lowerAsync = (
|
|
117
|
+
program: ts.Program,
|
|
118
|
+
sdk: string,
|
|
119
|
+
positions: Positions,
|
|
120
|
+
root: string,
|
|
121
|
+
): AsyncLowering => {
|
|
122
|
+
const result: AsyncLowering = {
|
|
123
|
+
sources: new Map(),
|
|
124
|
+
blocks: new Map(),
|
|
125
|
+
jobs: new Map(),
|
|
126
|
+
programs: {},
|
|
127
|
+
schedules: new Map(),
|
|
128
|
+
diagnostics: [],
|
|
129
|
+
continuations: new Map(),
|
|
130
|
+
};
|
|
131
|
+
const checker = program.getTypeChecker();
|
|
132
|
+
const symbol = (node: ts.Node) => symbolAt(checker, node);
|
|
133
|
+
const fail: (node: ts.Node, message: string) => never = (node, message) => {
|
|
134
|
+
throw new SemanticError(asyncDiagnostic(positions, node, message));
|
|
135
|
+
};
|
|
136
|
+
const names = new GeneratedNames(root);
|
|
137
|
+
const sources = plcSources(program);
|
|
138
|
+
const { functions, methods } = asyncRoutines(sources, checker, names);
|
|
139
|
+
if (!functions.size) {
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
const recursion = recursiveRoutine(functions, symbol);
|
|
143
|
+
if (recursion) {
|
|
144
|
+
result.diagnostics.push(
|
|
145
|
+
asyncDiagnostic(
|
|
146
|
+
positions,
|
|
147
|
+
recursion.name,
|
|
148
|
+
`${recursion.name.getText()} calls itself, and a PLC routine can't: each call's state is allocated when the program compiles. Write the repetition as a loop.`,
|
|
149
|
+
),
|
|
150
|
+
);
|
|
151
|
+
return result;
|
|
152
|
+
}
|
|
153
|
+
const context: AsyncContext = {
|
|
154
|
+
checker,
|
|
155
|
+
sdk,
|
|
156
|
+
positions,
|
|
157
|
+
functions,
|
|
158
|
+
methods,
|
|
159
|
+
symbol,
|
|
160
|
+
namesSdk: (node, name) => namesSdk(checker, sdk, node, name),
|
|
161
|
+
fail,
|
|
162
|
+
names,
|
|
163
|
+
aliases: (fn) => {
|
|
164
|
+
const prefix = names.name(
|
|
165
|
+
`TsplcAsync${fn.name.getText(fn.getSourceFile())}`,
|
|
166
|
+
fn,
|
|
167
|
+
);
|
|
168
|
+
return {
|
|
169
|
+
result: `${prefix}Result`,
|
|
170
|
+
argument: (index) => `${prefix}Argument${index}`,
|
|
171
|
+
};
|
|
172
|
+
},
|
|
173
|
+
importAliases,
|
|
174
|
+
referenced: (declaration) => {
|
|
175
|
+
const target = symbol(declaration.name);
|
|
176
|
+
const reads = (node: ts.Node): boolean =>
|
|
177
|
+
(ts.isIdentifier(node) &&
|
|
178
|
+
node !== declaration.name &&
|
|
179
|
+
target !== undefined &&
|
|
180
|
+
symbol(node) === target) ||
|
|
181
|
+
ts.forEachChild(node, reads) === true;
|
|
182
|
+
return sources.some(reads);
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
const ownedPolls = new Map<ts.Symbol, string[]>();
|
|
186
|
+
const ownedJobs = new Map<ts.Symbol, OwnedJob[]>();
|
|
187
|
+
for (const source of sources) {
|
|
188
|
+
const rewrite = rewriteSource(context, source);
|
|
189
|
+
if (rewrite instanceof SemanticError) {
|
|
190
|
+
result.diagnostics.push(rewrite.diagnostic);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (!rewrite) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
result.sources.set(source.fileName, rewrittenText(rewrite));
|
|
197
|
+
for (const [key, value] of rewrite.blocks) {
|
|
198
|
+
result.blocks.set(key, value);
|
|
199
|
+
}
|
|
200
|
+
for (const [key, value] of rewrite.jobs) {
|
|
201
|
+
result.jobs.set(key, value);
|
|
202
|
+
}
|
|
203
|
+
Object.assign(result.programs, rewrite.programs);
|
|
204
|
+
for (const [key, value] of rewrite.schedules) {
|
|
205
|
+
result.schedules.set(key, value);
|
|
206
|
+
}
|
|
207
|
+
for (const [key, value] of rewrite.continuations) {
|
|
208
|
+
result.continuations.set(key, value);
|
|
209
|
+
}
|
|
210
|
+
for (const [block, polls] of rewrite.ownedPolls) {
|
|
211
|
+
ownedPolls.set(block, polls);
|
|
212
|
+
}
|
|
213
|
+
for (const [block, jobs] of rewrite.ownedJobs) {
|
|
214
|
+
ownedJobs.set(block, jobs);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
for (const source of sources) {
|
|
218
|
+
addInstanceEntries(context, { ownedPolls, ownedJobs }, source, result);
|
|
219
|
+
}
|
|
220
|
+
return result;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
/** Imports those of `names`, aliases `fn`'s module exports, that the source does not import yet. */
|
|
224
|
+
function importAliases(
|
|
225
|
+
file: Pick<SourceRewrite, "source" | "extra" | "importedAliases">,
|
|
226
|
+
node: ts.Node,
|
|
227
|
+
fn: NamedRoutine,
|
|
228
|
+
names: readonly string[],
|
|
229
|
+
): void {
|
|
230
|
+
const missing = names.filter((name) => !file.importedAliases.has(name));
|
|
231
|
+
if (!missing.length) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
for (const name of missing) {
|
|
235
|
+
file.importedAliases.add(name);
|
|
236
|
+
}
|
|
237
|
+
file.extra.push(
|
|
238
|
+
from(
|
|
239
|
+
node,
|
|
240
|
+
`import type {${missing.join(",")}} from ${importSpecifier(file.source.fileName, fn.getSourceFile().fileName)};`,
|
|
241
|
+
),
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* An async function that calls itself, directly or through other async functions. Each becomes a
|
|
247
|
+
* block holding an instance of every function it calls, so a cycle would be a block inside itself.
|
|
248
|
+
* An async method keeps its state in its own instance, so its calls allocate nothing.
|
|
249
|
+
*/
|
|
250
|
+
function recursiveRoutine(
|
|
251
|
+
functions: ReadonlyMap<ts.Symbol, NamedRoutine>,
|
|
252
|
+
symbol: (node: ts.Node) => ts.Symbol | undefined,
|
|
253
|
+
): NamedRoutine | undefined {
|
|
254
|
+
const callees = (fn: NamedRoutine): ts.Symbol[] => {
|
|
255
|
+
const found: ts.Symbol[] = [];
|
|
256
|
+
const visit = (node: ts.Node): void => {
|
|
257
|
+
if (ts.isCallExpression(node)) {
|
|
258
|
+
const callee = ts.isPropertyAccessExpression(node.expression)
|
|
259
|
+
? node.expression.name
|
|
260
|
+
: node.expression;
|
|
261
|
+
const target = symbol(callee);
|
|
262
|
+
const routine = target && functions.get(target);
|
|
263
|
+
if (target && routine && ts.isFunctionDeclaration(routine)) {
|
|
264
|
+
found.push(target);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
ts.forEachChild(node, visit);
|
|
268
|
+
};
|
|
269
|
+
if (fn.body) {
|
|
270
|
+
visit(fn.body);
|
|
271
|
+
}
|
|
272
|
+
return found;
|
|
273
|
+
};
|
|
274
|
+
const finished = new Set<ts.Symbol>();
|
|
275
|
+
const active = new Set<ts.Symbol>();
|
|
276
|
+
const cycle = (key: ts.Symbol): NamedRoutine | undefined => {
|
|
277
|
+
const fn = functions.get(key);
|
|
278
|
+
if (active.has(key)) {
|
|
279
|
+
return fn;
|
|
280
|
+
}
|
|
281
|
+
if (finished.has(key) || !fn) {
|
|
282
|
+
return undefined;
|
|
283
|
+
}
|
|
284
|
+
active.add(key);
|
|
285
|
+
for (const callee of callees(fn)) {
|
|
286
|
+
const found = cycle(callee);
|
|
287
|
+
if (found) {
|
|
288
|
+
return found;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
active.delete(key);
|
|
292
|
+
finished.add(key);
|
|
293
|
+
return undefined;
|
|
294
|
+
};
|
|
295
|
+
for (const key of functions.keys()) {
|
|
296
|
+
const found = cycle(key);
|
|
297
|
+
if (found) {
|
|
298
|
+
return found;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Async functions and methods by symbol; methods also get the names of their coroutine members. */
|
|
305
|
+
function asyncRoutines(
|
|
306
|
+
sources: readonly ts.SourceFile[],
|
|
307
|
+
checker: ts.TypeChecker,
|
|
308
|
+
names: GeneratedNames,
|
|
309
|
+
) {
|
|
310
|
+
const functions = new Map<ts.Symbol, NamedRoutine>();
|
|
311
|
+
const methods = new Map<ts.Symbol, MethodMembers>();
|
|
312
|
+
for (const [key, fn] of namedRoutines(sources, checker)) {
|
|
313
|
+
if (!hasModifier(fn, ts.SyntaxKind.AsyncKeyword)) {
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
functions.set(key, fn);
|
|
317
|
+
if (ts.isMethodDeclaration(fn)) {
|
|
318
|
+
const prefix = names.name(`tsplcAsync${fn.name.getText()}`, fn);
|
|
319
|
+
methods.set(key, {
|
|
320
|
+
start: `${prefix}Start`,
|
|
321
|
+
reset: `${prefix}Reset`,
|
|
322
|
+
snapshot: `${prefix}Snapshot`,
|
|
323
|
+
advance: `${prefix}Advance`,
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return { functions, methods };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Replaces a source's async routines and task definitions with generated blocks: undefined when
|
|
332
|
+
* it has none, and the reason when it fails, in which case it is left as written.
|
|
333
|
+
*/
|
|
334
|
+
function rewriteSource(
|
|
335
|
+
context: AsyncContext,
|
|
336
|
+
source: ts.SourceFile,
|
|
337
|
+
): SourceRewrite | SemanticError | undefined {
|
|
338
|
+
const rewrite: SourceRewrite = {
|
|
339
|
+
source,
|
|
340
|
+
replacements: [],
|
|
341
|
+
extra: [],
|
|
342
|
+
importedAliases: new Set(),
|
|
343
|
+
blocks: new Map(),
|
|
344
|
+
jobs: new Map(),
|
|
345
|
+
programs: {},
|
|
346
|
+
schedules: new Map(),
|
|
347
|
+
continuations: new Map(),
|
|
348
|
+
ownedPolls: new Map(),
|
|
349
|
+
ownedJobs: new Map(),
|
|
350
|
+
};
|
|
351
|
+
try {
|
|
352
|
+
for (const fn of context.functions.values()) {
|
|
353
|
+
if (fn.getSourceFile() === source) {
|
|
354
|
+
lowerCoroutine(context, rewrite, fn);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
lowerTaskDefinitions(context, rewrite);
|
|
358
|
+
} catch (cause) {
|
|
359
|
+
if (!(cause instanceof SemanticError)) {
|
|
360
|
+
throw cause;
|
|
361
|
+
}
|
|
362
|
+
return cause;
|
|
363
|
+
}
|
|
364
|
+
return rewrite.replacements.length ? rewrite : undefined;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/** A rewritten source's text: generated classes first, the source with its edits, then the other declarations. */
|
|
368
|
+
function rewrittenText({ source, replacements, extra }: SourceRewrite): Mapped {
|
|
369
|
+
const added = joined(extra, "\n");
|
|
370
|
+
const generated = ts.createSourceFile(
|
|
371
|
+
source.fileName,
|
|
372
|
+
added.text,
|
|
373
|
+
ts.ScriptTarget.Latest,
|
|
374
|
+
true,
|
|
375
|
+
);
|
|
376
|
+
const part = (node: ts.Node) =>
|
|
377
|
+
slice(added, node.getStart(generated), node.end);
|
|
378
|
+
const before = generated.statements.filter(ts.isClassDeclaration).map(part);
|
|
379
|
+
const after = generated.statements
|
|
380
|
+
.filter((statement) => !ts.isClassDeclaration(statement))
|
|
381
|
+
.map(part);
|
|
382
|
+
return mapped`${joined(before, "\n")}\n${edited(source, replacements)}\n${joined(after, "\n")}`;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/**
|
|
386
|
+
* A block that owns continuations or background jobs runs them through its instances, so each
|
|
387
|
+
* module-level instance gets an entry point per poll and job.
|
|
388
|
+
*/
|
|
389
|
+
function addInstanceEntries(
|
|
390
|
+
context: AsyncContext,
|
|
391
|
+
owned: Pick<SourceRewrite, "ownedPolls" | "ownedJobs">,
|
|
392
|
+
source: ts.SourceFile,
|
|
393
|
+
result: AsyncLowering,
|
|
394
|
+
): void {
|
|
395
|
+
const { positions } = context;
|
|
396
|
+
const entries: Mapped[] = [];
|
|
397
|
+
const program = (entry: string) => {
|
|
398
|
+
result.programs[entry] = {
|
|
399
|
+
module: resolve(source.fileName),
|
|
400
|
+
export: entry,
|
|
401
|
+
};
|
|
402
|
+
};
|
|
403
|
+
const visit = (node: ts.Node): void => {
|
|
404
|
+
const instance = allocatedInstance(context, node);
|
|
405
|
+
const block = instance && context.symbol(instance.block);
|
|
406
|
+
const jobs = block && owned.ownedJobs.get(block);
|
|
407
|
+
const polls = block && owned.ownedPolls.get(block);
|
|
408
|
+
if (!instance || (!jobs && !polls)) {
|
|
409
|
+
ts.forEachChild(node, visit);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
const declaration = node.parent;
|
|
413
|
+
if (
|
|
414
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
415
|
+
!ts.isIdentifier(declaration.name) ||
|
|
416
|
+
!ts.isVariableDeclarationList(declaration.parent) ||
|
|
417
|
+
!ts.isVariableStatement(declaration.parent.parent) ||
|
|
418
|
+
!ts.isSourceFile(declaration.parent.parent.parent) ||
|
|
419
|
+
(jobs && instance.elements)
|
|
420
|
+
) {
|
|
421
|
+
result.diagnostics.push(
|
|
422
|
+
asyncDiagnostic(
|
|
423
|
+
positions,
|
|
424
|
+
node,
|
|
425
|
+
jobs
|
|
426
|
+
? "Function blocks with background jobs require module-level named instances."
|
|
427
|
+
: "Function blocks with continuations require module-level named instances.",
|
|
428
|
+
),
|
|
429
|
+
);
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const name = declaration.name.text;
|
|
433
|
+
// One entry polls all of an instance's sites, in their order: they share its task.
|
|
434
|
+
if (polls?.length) {
|
|
435
|
+
const range =
|
|
436
|
+
instance.elements && elementRange(context, instance.elements);
|
|
437
|
+
if (range === null) {
|
|
438
|
+
result.diagnostics.push(
|
|
439
|
+
asyncDiagnostic(
|
|
440
|
+
positions,
|
|
441
|
+
node,
|
|
442
|
+
"A block array whose blocks have continuations needs a literal length and lower bound.",
|
|
443
|
+
),
|
|
444
|
+
);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
const entry = context.names.name("tsplcContinuation", declaration);
|
|
448
|
+
const calls = polls.map((poll) =>
|
|
449
|
+
range === undefined
|
|
450
|
+
? `${name}.${poll}();`
|
|
451
|
+
: `for(let element:DINT=${range.first};element<=${range.last};element++){${name}[element].${poll}();}`,
|
|
452
|
+
);
|
|
453
|
+
entries.push(
|
|
454
|
+
from(node, `export function ${entry}():void{${calls.join("")}}`),
|
|
455
|
+
);
|
|
456
|
+
program(entry);
|
|
457
|
+
result.continuations.set(entry, {
|
|
458
|
+
instance: name,
|
|
459
|
+
file: positions.node(declaration).file,
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
for (const job of jobs ?? []) {
|
|
463
|
+
const entry = context.names.name(
|
|
464
|
+
`tsplcBackground_${job.method}`,
|
|
465
|
+
declaration,
|
|
466
|
+
);
|
|
467
|
+
entries.push(
|
|
468
|
+
from(node, `export function ${entry}():void{${name}.${job.method}();}`),
|
|
469
|
+
);
|
|
470
|
+
program(entry);
|
|
471
|
+
result.schedules.set(entry, { ...job.schedule, worker: name });
|
|
472
|
+
}
|
|
473
|
+
ts.forEachChild(node, visit);
|
|
474
|
+
};
|
|
475
|
+
visit(source);
|
|
476
|
+
if (entries.length) {
|
|
477
|
+
result.sources.set(
|
|
478
|
+
source.fileName,
|
|
479
|
+
mapped`${result.sources.get(source.fileName) ?? edited(source, [])}\n${joined(entries, "\n")}`,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* The block instances an expression allocates: `new Block()` one instance, and
|
|
486
|
+
* `defaultValue<FixedArray<Block, N, Lower>>()` one per element, whose length and lower bound
|
|
487
|
+
* are type arguments.
|
|
488
|
+
*/
|
|
489
|
+
function allocatedInstance(
|
|
490
|
+
context: AsyncContext,
|
|
491
|
+
node: ts.Node,
|
|
492
|
+
):
|
|
493
|
+
| { block: ts.Node; elements?: { length: ts.TypeNode; lower?: ts.TypeNode } }
|
|
494
|
+
| undefined {
|
|
495
|
+
if (ts.isNewExpression(node)) {
|
|
496
|
+
return { block: node.expression };
|
|
497
|
+
}
|
|
498
|
+
if (
|
|
499
|
+
!ts.isCallExpression(node) ||
|
|
500
|
+
!context.namesSdk(node.expression, "defaultValue")
|
|
501
|
+
) {
|
|
502
|
+
return undefined;
|
|
503
|
+
}
|
|
504
|
+
const [type] = node.typeArguments ?? [];
|
|
505
|
+
const element = type && blockArrayElement(type, context.checker, context.sdk);
|
|
506
|
+
const [, length, lower] =
|
|
507
|
+
type && ts.isTypeReferenceNode(type) ? (type.typeArguments ?? []) : [];
|
|
508
|
+
if (!element || !length) {
|
|
509
|
+
return undefined;
|
|
510
|
+
}
|
|
511
|
+
return { block: element.typeName, elements: { length, lower } };
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/** A block array's first and last index; null when its length or lower bound is not a literal. */
|
|
515
|
+
function elementRange(
|
|
516
|
+
{ checker }: AsyncContext,
|
|
517
|
+
{ length, lower }: { length: ts.TypeNode; lower?: ts.TypeNode },
|
|
518
|
+
): { first: number; last: number } | null {
|
|
519
|
+
// A literal, or a table's `(typeof table)["length"]`.
|
|
520
|
+
const literal = (node: ts.TypeNode) => {
|
|
521
|
+
const type = checker.getTypeFromTypeNode(node);
|
|
522
|
+
return type.isNumberLiteral() ? type.value : undefined;
|
|
523
|
+
};
|
|
524
|
+
const first = lower ? literal(lower) : 0;
|
|
525
|
+
const count = literal(length);
|
|
526
|
+
if (first === undefined || count === undefined) {
|
|
527
|
+
return null;
|
|
528
|
+
}
|
|
529
|
+
return { first, last: first + count - 1 };
|
|
530
|
+
}
|