@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.
- package/README.md +33 -8
- package/dist/cli.js +794 -621
- package/dist/cli.js.map +19 -16
- package/dist/clock.js +5 -2
- package/dist/clock.js.map +5 -4
- package/dist/codesys.js +4162 -4157
- package/dist/codesys.js.map +43 -29
- package/dist/index.js +7852 -6549
- package/dist/index.js.map +53 -50
- package/dist/ir.js +2482 -2277
- package/dist/ir.js.map +28 -25
- package/dist/retain.js +3 -1
- package/dist/retain.js.map +3 -3
- package/dist/signals.js +115 -0
- package/dist/signals.js.map +10 -0
- package/dist/telemetry.js +25 -20
- package/dist/telemetry.js.map +6 -6
- package/package.json +19 -39
- package/src/codesys/config.ts +3 -3
- package/src/codesys/debug.ts +92 -108
- package/src/codesys/emit.ts +100 -88
- package/src/codesys/endpoints.ts +111 -0
- package/src/codesys/engineering.ts +30 -54
- 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 +85 -1401
- package/src/codesys/expressions.ts +41 -33
- package/src/codesys/global-lists.ts +114 -94
- package/src/codesys/global-names.ts +12 -14
- package/src/codesys/http.ts +428 -388
- package/src/codesys/instrumentation.ts +142 -148
- package/src/codesys/io-paths.ts +31 -23
- package/src/codesys/loops.ts +101 -0
- package/src/codesys/manifest.ts +269 -269
- package/src/codesys/names.ts +10 -33
- package/src/codesys/numeric.ts +89 -42
- package/src/codesys/plan.ts +202 -190
- package/src/codesys/programs.ts +23 -16
- package/src/codesys/publication.ts +13 -86
- package/src/codesys/required.ts +8 -0
- package/src/codesys/routines.ts +103 -489
- package/src/codesys/scheduling.ts +96 -0
- package/src/codesys/service-channels.ts +23 -0
- package/src/codesys/sources.ts +34 -28
- package/src/codesys/st-files.ts +112 -105
- package/src/codesys/statements.ts +409 -0
- package/src/codesys/storage.ts +41 -23
- package/src/codesys/symbols.ts +130 -119
- package/src/codesys/task-access.ts +65 -221
- package/src/codesys/telemetry-settings.ts +19 -0
- package/src/codesys/telemetry.ts +185 -148
- package/src/codesys/types.ts +69 -109
- package/src/codesys/vendor.ts +13 -50
- package/src/frontend/analyzer.ts +66 -111
- package/src/frontend/async.ts +316 -128
- package/src/frontend/blocks.ts +178 -154
- package/src/frontend/configuration.ts +49 -153
- package/src/frontend/const-functions.ts +50 -40
- package/src/frontend/control-flow.ts +164 -129
- package/src/frontend/cooperative.ts +17 -9
- package/src/frontend/coroutines.ts +539 -441
- package/src/frontend/dialect-names.ts +104 -0
- package/src/frontend/elaborate/array-searches.ts +77 -69
- package/src/frontend/elaborate/assignments.ts +68 -77
- package/src/frontend/elaborate/calls.ts +878 -512
- package/src/frontend/elaborate/capture-protocol.ts +118 -21
- package/src/frontend/elaborate/capture.ts +196 -109
- package/src/frontend/elaborate/declarations.ts +124 -0
- package/src/frontend/elaborate/dialect.ts +119 -46
- package/src/frontend/elaborate/expressions.ts +763 -442
- package/src/frontend/elaborate/filtered-arrays.ts +127 -91
- package/src/frontend/elaborate/frame.ts +50 -2
- package/src/frontend/elaborate/generics.ts +20 -34
- package/src/frontend/elaborate/graph.ts +13 -87
- package/src/frontend/elaborate/imports.ts +0 -5
- package/src/frontend/elaborate/index.ts +416 -169
- package/src/frontend/elaborate/initializers.ts +64 -42
- package/src/frontend/elaborate/iterators.ts +228 -364
- package/src/frontend/elaborate/layouts.ts +477 -116
- package/src/frontend/elaborate/model.ts +113 -55
- package/src/frontend/elaborate/references.ts +116 -138
- package/src/frontend/elaborate/retention.ts +137 -59
- package/src/frontend/elaborate/specialize.ts +617 -308
- package/src/frontend/elaborate/statements.ts +436 -247
- package/src/frontend/elaborate/storage.ts +280 -213
- package/src/frontend/elaborate/time-constructors.ts +34 -0
- package/src/frontend/elaborate/values.ts +77 -7
- package/src/frontend/elaborate/writes.ts +98 -84
- package/src/frontend/expressions.ts +149 -151
- package/src/frontend/generated-names.ts +3 -3
- package/src/frontend/intrinsics.ts +31 -17
- package/src/frontend/lowering-key.ts +23 -0
- package/src/frontend/module-graph.ts +105 -43
- package/src/frontend/positions.ts +43 -35
- package/src/frontend/promise-chains.ts +226 -266
- package/src/frontend/promises.ts +88 -67
- package/src/frontend/routines.ts +122 -65
- package/src/frontend/sdk.ts +104 -5
- package/src/frontend/services.ts +149 -102
- package/src/frontend/statements.ts +60 -64
- package/src/frontend/tasks.ts +151 -96
- package/src/frontend/telemetry.ts +1 -1
- package/src/frontend/types.ts +366 -181
- package/src/frontend/typescript.ts +239 -112
- package/src/index.ts +2 -1
- package/src/ir/aggregates.ts +90 -75
- package/src/ir/allocation.ts +29 -18
- package/src/ir/conditions.ts +38 -78
- package/src/ir/core-model.ts +128 -11
- package/src/ir/declarations.ts +18 -32
- package/src/ir/diagnostics.ts +25 -18
- package/src/ir/domains.ts +26 -23
- package/src/ir/fold.ts +55 -50
- package/src/ir/identity.ts +10 -0
- package/src/ir/index.ts +31 -7
- package/src/ir/job-errors.ts +30 -8
- package/src/ir/layouts.ts +63 -34
- package/src/ir/link.ts +11 -46
- package/src/ir/model.ts +25 -6
- package/src/ir/normalize.ts +3 -28
- package/src/ir/numeric.ts +45 -18
- package/src/ir/operands.ts +102 -0
- package/src/ir/ranges.ts +242 -67
- package/src/ir/routine.ts +126 -183
- package/src/ir/scalars.ts +29 -25
- package/src/ir/share.ts +323 -343
- package/src/ir/short-circuit.ts +140 -0
- package/src/ir/tables.ts +111 -0
- package/src/ir/temporaries.ts +15 -113
- package/src/ir/types.ts +32 -8
- package/src/project/artifacts.ts +143 -71
- package/src/project/cli.ts +104 -87
- package/src/project/debugger.ts +48 -34
- package/src/project/deployments.ts +12 -68
- package/src/project/flags.ts +14 -0
- package/src/project/plant.ts +293 -225
- package/src/project/project-commands.ts +254 -192
- package/src/project/project.ts +184 -141
- package/src/project/release.ts +22 -28
- package/src/project/server.ts +22 -26
- package/src/project/terminal.ts +13 -2
- package/src/runtime/async.ts +17 -13
- package/src/runtime/launch.ts +5 -2
- package/src/runtime/main.ts +8 -3
- package/src/runtime/missing-file.ts +3 -0
- package/src/runtime/process.ts +24 -19
- package/src/runtime/program.ts +20 -53
- package/src/runtime/retain.ts +18 -10
- package/src/runtime/runtime.ts +47 -60
- package/src/runtime/settings.ts +27 -0
- package/src/runtime/storage.ts +49 -74
- package/src/runtime/symbols.ts +19 -75
- package/src/runtime/telemetry.ts +37 -14
- package/src/runtime/transport.ts +26 -30
- package/stdlib/clock.ts +4 -2
- package/stdlib/plc-time.ts +2 -0
- package/stdlib/retain.ts +2 -0
- package/stdlib/signals.ts +142 -0
- package/stdlib/telemetry.ts +5 -12
- package/types/dialect.d.ts +49 -0
- package/types/globals.d.ts +2 -42
- package/types/index.d.ts +3 -1
- package/types/retain.d.ts +8 -2
- package/types/telemetry.d.ts +3 -0
- package/dist/bun-runtime.js +0 -1472
- package/dist/bun-runtime.js.map +0 -26
- package/dist/elaborate-capture.js +0 -304
- package/dist/elaborate-capture.js.map +0 -11
- package/dist/math.js +0 -24
- package/dist/math.js.map +0 -10
- package/dist/project.js +0 -626
- package/dist/project.js.map +0 -12
- package/dist/server-check.js +0 -59
- package/dist/server-check.js.map +0 -11
- package/src/frontend/exposure.ts +0 -181
- package/src/project/index.ts +0 -6
- package/src/runtime/.runtime-65O4JH/server/router.ts +0 -17
- package/src/runtime/.runtime-65O4JH/src/cell.ts +0 -45
- package/src/runtime/.runtime-65O4JH/src/controller.ts +0 -58
- package/src/runtime/.runtime-65O4JH/src/lane.ts +0 -6
- package/src/runtime/.runtime-65O4JH/src/main.ts +0 -11
- package/src/runtime/.runtime-65O4JH/src/services.ts +0 -8
- package/src/runtime/.runtime-65O4JH/tsconfig.json +0 -13
- package/src/runtime/.runtime-65O4JH/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-Brw2kO/server/router.ts +0 -17
- package/src/runtime/.runtime-Brw2kO/src/cell.ts +0 -45
- package/src/runtime/.runtime-Brw2kO/src/controller.ts +0 -58
- package/src/runtime/.runtime-Brw2kO/src/lane.ts +0 -6
- package/src/runtime/.runtime-Brw2kO/src/main.ts +0 -11
- package/src/runtime/.runtime-Brw2kO/src/services.ts +0 -8
- package/src/runtime/.runtime-Brw2kO/tsconfig.json +0 -13
- package/src/runtime/.runtime-Brw2kO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-NHfu1D/server/router.ts +0 -17
- package/src/runtime/.runtime-NHfu1D/src/cell.ts +0 -45
- package/src/runtime/.runtime-NHfu1D/src/controller.ts +0 -58
- package/src/runtime/.runtime-NHfu1D/src/lane.ts +0 -6
- package/src/runtime/.runtime-NHfu1D/src/main.ts +0 -11
- package/src/runtime/.runtime-NHfu1D/src/services.ts +0 -8
- package/src/runtime/.runtime-NHfu1D/tsconfig.json +0 -13
- package/src/runtime/.runtime-NHfu1D/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-VaUSOu/server/router.ts +0 -17
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +0 -45
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +0 -58
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +0 -6
- package/src/runtime/.runtime-VaUSOu/src/main.ts +0 -11
- package/src/runtime/.runtime-VaUSOu/src/services.ts +0 -8
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +0 -13
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-WbtNvR/server/router.ts +0 -17
- package/src/runtime/.runtime-WbtNvR/src/cell.ts +0 -45
- package/src/runtime/.runtime-WbtNvR/src/controller.ts +0 -58
- package/src/runtime/.runtime-WbtNvR/src/lane.ts +0 -6
- package/src/runtime/.runtime-WbtNvR/src/main.ts +0 -11
- package/src/runtime/.runtime-WbtNvR/src/services.ts +0 -8
- package/src/runtime/.runtime-WbtNvR/tsconfig.json +0 -13
- package/src/runtime/.runtime-WbtNvR/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-GMPUt8/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-GMPUt8/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-GMPUt8/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-GMPUt8/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-GMPUt8/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-GMPUt8/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-GMPUt8/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-GMPUt8/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-UwafxO/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-UwafxO/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-UwafxO/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-UwafxO/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-UwafxO/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-UwafxO/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-UwafxO/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-UwafxO/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-ecaSee/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-ecaSee/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-ecaSee/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-ecaSee/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-ecaSee/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-ecaSee/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-ecaSee/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-ecaSee/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-opcua-y91UCm/server/router.ts +0 -17
- package/src/runtime/.runtime-opcua-y91UCm/src/cell.ts +0 -45
- package/src/runtime/.runtime-opcua-y91UCm/src/controller.ts +0 -58
- package/src/runtime/.runtime-opcua-y91UCm/src/lane.ts +0 -6
- package/src/runtime/.runtime-opcua-y91UCm/src/main.ts +0 -11
- package/src/runtime/.runtime-opcua-y91UCm/src/services.ts +0 -8
- package/src/runtime/.runtime-opcua-y91UCm/tsconfig.json +0 -13
- package/src/runtime/.runtime-opcua-y91UCm/tsplc.config.ts +0 -31
- package/src/runtime/.runtime-ybIxYh/server/router.ts +0 -17
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +0 -45
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +0 -58
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +0 -6
- package/src/runtime/.runtime-ybIxYh/src/main.ts +0 -11
- package/src/runtime/.runtime-ybIxYh/src/services.ts +0 -8
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +0 -13
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +0 -31
- package/stdlib/math.ts +0 -23
- package/types/plc.d.ts +0 -4
|
@@ -1,28 +1,39 @@
|
|
|
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 { identityDigest } from "@tsplc/compiler/ir";
|
|
3
2
|
import ts from "typescript";
|
|
4
3
|
import { from, joined, type Mapped, mapped } from "../positions";
|
|
4
|
+
import { sdkModules, valueSymbol } from "../sdk";
|
|
5
|
+
import {
|
|
6
|
+
namedElements,
|
|
7
|
+
namedFields,
|
|
8
|
+
parameterDefault,
|
|
9
|
+
type RunTimeOptional,
|
|
10
|
+
runTimeOptional,
|
|
11
|
+
symbolOf,
|
|
12
|
+
} from "./declarations";
|
|
5
13
|
import { DialectText } from "./dialect";
|
|
6
14
|
import {
|
|
15
|
+
type Closure,
|
|
7
16
|
ExpressionSpecializer,
|
|
8
17
|
type RoutineCall,
|
|
9
18
|
type RoutineTable,
|
|
10
19
|
type SpecializeContext,
|
|
20
|
+
type Typing,
|
|
11
21
|
} from "./expressions";
|
|
12
22
|
import { Emitter, type Frame, type Referents } from "./frame";
|
|
13
23
|
import type { GenericTypes } from "./generics";
|
|
14
24
|
import { EntryImports } from "./imports";
|
|
15
25
|
import { Initializers } from "./initializers";
|
|
16
|
-
import {
|
|
17
|
-
import { Layouts, unusedName } from "./layouts";
|
|
26
|
+
import { type ArrayLayout, arrayLayout, Layouts, unusedName } from "./layouts";
|
|
18
27
|
import { type Model, type ModelNode, type Root, tsPath } from "./model";
|
|
19
28
|
import { referenceLayout, referenceType } from "./references";
|
|
20
29
|
import { StatementSpecializer } from "./statements";
|
|
21
30
|
import { Storage } from "./storage";
|
|
22
31
|
import {
|
|
32
|
+
type Counted,
|
|
23
33
|
constant,
|
|
24
|
-
type Env,
|
|
25
34
|
type FunctionLike,
|
|
35
|
+
holdsSetupObjects,
|
|
36
|
+
restoring,
|
|
26
37
|
SpecializeError,
|
|
27
38
|
type Value,
|
|
28
39
|
} from "./values";
|
|
@@ -42,30 +53,36 @@ export class Specializer implements RoutineTable {
|
|
|
42
53
|
private readonly statements: StatementSpecializer;
|
|
43
54
|
readonly imports = new EntryImports();
|
|
44
55
|
/** The source of each routine specialized so far, in the order each was finished. */
|
|
45
|
-
readonly
|
|
56
|
+
private readonly finished: Mapped[] = [];
|
|
57
|
+
/** Each async routine specialized for an instance, by routine name, which `cancelAsync(this)` cancels. */
|
|
58
|
+
private readonly asyncOwners = new Map<string, ModelNode>();
|
|
59
|
+
/** The routine that cancels an instance's async calls, by the instance, for each `cancelAsync(this)`. */
|
|
60
|
+
private readonly cancellers = new Map<ModelNode, string>();
|
|
46
61
|
/** Routines specialized from a class method, by routine name: `Class.method` and the TypeScript path of `this`. */
|
|
47
62
|
readonly origins = new Map<string, { name: string; instance?: string }>();
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
{ layout: string; length: number | undefined }
|
|
55
|
-
>();
|
|
56
|
-
private readonly routineNames = new Set<string>();
|
|
57
|
-
/** What each finished routine whose result is a reference returns, by routine name. */
|
|
58
|
-
private readonly referents = new Map<string, Referents>();
|
|
59
|
-
|
|
63
|
+
/** The routine each specialization is, by what tells it apart. */
|
|
64
|
+
private readonly specialized = new Map<string, Specialized>();
|
|
65
|
+
/** The routines whose bodies are being specialized, which a call to one of them would recurse into. */
|
|
66
|
+
private readonly unfinished = new Set<string>();
|
|
67
|
+
/** What each routine gives its callers, by routine name; empty until its body is specialized. */
|
|
68
|
+
private readonly results = new Map<string, RoutineResult>();
|
|
60
69
|
readonly checker: ts.TypeChecker;
|
|
61
70
|
|
|
62
71
|
constructor(
|
|
63
72
|
readonly model: Model,
|
|
64
73
|
private readonly generics: GenericTypes,
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
74
|
+
sdkFile: string,
|
|
75
|
+
sources: readonly ts.SourceFile[],
|
|
76
|
+
{
|
|
77
|
+
kept,
|
|
78
|
+
named,
|
|
79
|
+
}: {
|
|
80
|
+
/** Modules the compiled program keeps as written. */
|
|
81
|
+
kept: (file: string) => boolean;
|
|
82
|
+
/** Modules whose exported types the compiled program imports by name rather than copies. */
|
|
83
|
+
named: (source: ts.SourceFile) => boolean;
|
|
84
|
+
},
|
|
85
|
+
rootModule: (root: Root) => string,
|
|
69
86
|
) {
|
|
70
87
|
const checker = generics.checker;
|
|
71
88
|
this.checker = checker;
|
|
@@ -73,7 +90,8 @@ export class Specializer implements RoutineTable {
|
|
|
73
90
|
model,
|
|
74
91
|
checker,
|
|
75
92
|
kept,
|
|
76
|
-
sources.filter(
|
|
93
|
+
sources.filter(named),
|
|
94
|
+
(node) => this.storage.storageType(node),
|
|
77
95
|
);
|
|
78
96
|
this.storage = new Storage(
|
|
79
97
|
model,
|
|
@@ -84,7 +102,6 @@ export class Specializer implements RoutineTable {
|
|
|
84
102
|
kept,
|
|
85
103
|
rootModule,
|
|
86
104
|
);
|
|
87
|
-
this.layouts.instanceState = (node) => this.storage.storageType(node);
|
|
88
105
|
this.initializers = new Initializers(
|
|
89
106
|
model,
|
|
90
107
|
checker,
|
|
@@ -109,6 +126,7 @@ export class Specializer implements RoutineTable {
|
|
|
109
126
|
dialect: this.dialect,
|
|
110
127
|
sdkFile,
|
|
111
128
|
sources,
|
|
129
|
+
isKept: kept,
|
|
112
130
|
};
|
|
113
131
|
this.expressions = new ExpressionSpecializer(context, this);
|
|
114
132
|
this.statements = new StatementSpecializer(context, this.expressions);
|
|
@@ -121,22 +139,78 @@ export class Specializer implements RoutineTable {
|
|
|
121
139
|
|
|
122
140
|
/** Specializes the entry function, returning its body. */
|
|
123
141
|
entry(declaration: ts.FunctionDeclaration | ts.ArrowFunction): Mapped[] {
|
|
142
|
+
if (!declaration.body) {
|
|
143
|
+
throw new SpecializeError(declaration, "The entry function has no body.");
|
|
144
|
+
}
|
|
124
145
|
const frame: Frame = {
|
|
125
146
|
emitter: new Emitter(declaration),
|
|
126
147
|
env: new Map(),
|
|
148
|
+
loops: [],
|
|
127
149
|
returns: undefined,
|
|
128
150
|
async: false,
|
|
129
151
|
};
|
|
130
|
-
this.statements.body(declaration.body
|
|
152
|
+
this.statements.body(declaration.body, frame);
|
|
131
153
|
return frame.emitter.lines;
|
|
132
154
|
}
|
|
133
155
|
|
|
156
|
+
/**
|
|
157
|
+
* The routines specialized, then each instance's canceller, written last: it names every async
|
|
158
|
+
* routine of its instance, which may be specialized after the call that cancels them.
|
|
159
|
+
*/
|
|
160
|
+
get routines(): Mapped[] {
|
|
161
|
+
return [
|
|
162
|
+
...this.finished,
|
|
163
|
+
...[...this.cancellers].map(([self, name]) => {
|
|
164
|
+
const owned = [...this.asyncOwners]
|
|
165
|
+
.filter(([, owner]) => owner === self)
|
|
166
|
+
.map(([routine]) => routine);
|
|
167
|
+
const body = owned.length ? ` cancelAsync(${owned.join(", ")});` : "";
|
|
168
|
+
return mapped`function ${name}(): void {${body} }`;
|
|
169
|
+
}),
|
|
170
|
+
];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The routine that cancels the pending async calls of `self`'s methods, as `cancelAsync(this)` does. */
|
|
174
|
+
canceller(self: ModelNode): string {
|
|
175
|
+
const known = this.cancellers.get(self);
|
|
176
|
+
if (known) {
|
|
177
|
+
return known;
|
|
178
|
+
}
|
|
179
|
+
const name = unusedName(
|
|
180
|
+
`tsplcCancel_${identityDigest(this.model.identity(self) ?? String(self.id))}`,
|
|
181
|
+
(candidate) => this.results.has(candidate),
|
|
182
|
+
);
|
|
183
|
+
this.results.set(name, {});
|
|
184
|
+
this.cancellers.set(self, name);
|
|
185
|
+
this.imports.addSdk(sdkModules.async, "cancelAsync");
|
|
186
|
+
return name;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
speculatively<T>(run: () => T): T {
|
|
190
|
+
const routines = this.finished.length;
|
|
191
|
+
const restored = [
|
|
192
|
+
restoring(this.asyncOwners),
|
|
193
|
+
restoring(this.cancellers),
|
|
194
|
+
restoring(this.origins),
|
|
195
|
+
restoring(this.specialized),
|
|
196
|
+
restoring(this.results),
|
|
197
|
+
];
|
|
198
|
+
try {
|
|
199
|
+
return run();
|
|
200
|
+
} finally {
|
|
201
|
+
this.finished.splice(routines);
|
|
202
|
+
for (const restore of restored) {
|
|
203
|
+
restore();
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
134
208
|
/** The name of `declaration` specialized for `self` and these known arguments. */
|
|
135
209
|
routine(
|
|
136
210
|
declaration: FunctionLike,
|
|
137
211
|
self: ModelNode | undefined,
|
|
138
212
|
args: Value[],
|
|
139
|
-
closure?:
|
|
213
|
+
closure?: Closure,
|
|
140
214
|
): RoutineCall {
|
|
141
215
|
// A generic class's method specializes with its instance's type arguments in scope.
|
|
142
216
|
const owner =
|
|
@@ -150,7 +224,7 @@ export class Specializer implements RoutineTable {
|
|
|
150
224
|
declaration: FunctionLike,
|
|
151
225
|
self: ModelNode | undefined,
|
|
152
226
|
args: Value[],
|
|
153
|
-
closure?:
|
|
227
|
+
closure?: Closure,
|
|
154
228
|
): RoutineCall {
|
|
155
229
|
const signature = this.checker.getSignatureFromDeclaration(declaration);
|
|
156
230
|
const returnType =
|
|
@@ -163,7 +237,8 @@ export class Specializer implements RoutineTable {
|
|
|
163
237
|
);
|
|
164
238
|
const frame: Frame = {
|
|
165
239
|
emitter: new Emitter(declaration),
|
|
166
|
-
env: new Map(closure),
|
|
240
|
+
env: new Map(closure?.env),
|
|
241
|
+
loops: [],
|
|
167
242
|
self,
|
|
168
243
|
returns:
|
|
169
244
|
returnType &&
|
|
@@ -174,9 +249,8 @@ export class Specializer implements RoutineTable {
|
|
|
174
249
|
referents: { options: new Set<ModelNode>(), optional: false },
|
|
175
250
|
}),
|
|
176
251
|
};
|
|
177
|
-
const parameters:
|
|
178
|
-
const
|
|
179
|
-
const keys: string[] = [];
|
|
252
|
+
const parameters: RoutineParameter[] = [];
|
|
253
|
+
const keys: Specialization[] = [];
|
|
180
254
|
const parameterList = ts.isGetAccessorDeclaration(declaration)
|
|
181
255
|
? []
|
|
182
256
|
: declaration.parameters;
|
|
@@ -189,19 +263,19 @@ export class Specializer implements RoutineTable {
|
|
|
189
263
|
declared,
|
|
190
264
|
frame,
|
|
191
265
|
parameters,
|
|
192
|
-
runtimeArgs,
|
|
193
266
|
keys,
|
|
194
267
|
);
|
|
195
268
|
return;
|
|
196
269
|
}
|
|
197
270
|
const arg = args[index] ?? constant(undefined);
|
|
198
|
-
|
|
271
|
+
const fallback = parameterDefault(this.checker, arg, parameter);
|
|
272
|
+
if (fallback) {
|
|
199
273
|
this.bindDefault(
|
|
200
274
|
parameter,
|
|
275
|
+
fallback,
|
|
201
276
|
declared,
|
|
202
277
|
frame,
|
|
203
278
|
parameters,
|
|
204
|
-
runtimeArgs,
|
|
205
279
|
keys,
|
|
206
280
|
);
|
|
207
281
|
return;
|
|
@@ -212,108 +286,150 @@ export class Specializer implements RoutineTable {
|
|
|
212
286
|
declared,
|
|
213
287
|
frame,
|
|
214
288
|
parameters,
|
|
215
|
-
runtimeArgs,
|
|
216
289
|
keys,
|
|
217
290
|
);
|
|
218
291
|
});
|
|
219
292
|
// Captured runtime locals the routine reads become its parameters; one it writes is borrowed.
|
|
220
293
|
const read = readSymbols(declaration, this.checker);
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
294
|
+
const captured = [...(closure?.env ?? [])].filter(([symbol]) =>
|
|
295
|
+
read.has(symbol),
|
|
296
|
+
);
|
|
297
|
+
const locals = new Map<string, Value>();
|
|
298
|
+
const passed = new Map<ts.Symbol, Value | string>();
|
|
299
|
+
for (const [symbol, value] of captured) {
|
|
300
|
+
if (
|
|
301
|
+
value.kind === "code" &&
|
|
302
|
+
value.place &&
|
|
303
|
+
declaration.body !== undefined &&
|
|
304
|
+
assignsSymbol(declaration.body, new Set([symbol]), this.checker)
|
|
305
|
+
) {
|
|
226
306
|
const name = frame.emitter.local(symbol.name);
|
|
227
|
-
const type =
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
this.imports.addSdk("@tsplc/compiler/types", "ref");
|
|
236
|
-
}
|
|
237
|
-
parameters.push(`${name}: ${borrowed ? `InOut<${type}>` : type}`);
|
|
238
|
-
runtimeArgs.push(borrowed ? mapped`ref(${value.text})` : value.text);
|
|
239
|
-
frame.env.set(symbol, {
|
|
307
|
+
const type = this.expressions.heldType(value, parameterTyping);
|
|
308
|
+
this.imports.addSdk(sdkModules.types, "ref");
|
|
309
|
+
parameters.push({
|
|
310
|
+
name,
|
|
311
|
+
type: `InOut<${type}>`,
|
|
312
|
+
argument: mapped`ref(${value.text})`,
|
|
313
|
+
});
|
|
314
|
+
const local: Value = {
|
|
240
315
|
kind: "code",
|
|
241
316
|
text: name,
|
|
242
317
|
declared: value.declared,
|
|
243
318
|
place: true,
|
|
244
319
|
iec: value.iec,
|
|
245
|
-
}
|
|
246
|
-
|
|
320
|
+
};
|
|
321
|
+
locals.set(String(value.text), local);
|
|
322
|
+
frame.env.set(symbol, local);
|
|
323
|
+
passed.set(symbol, "ref");
|
|
247
324
|
} else if (
|
|
325
|
+
value.kind === "code" ||
|
|
248
326
|
value.kind === "choice" ||
|
|
249
327
|
value.kind === "masked" ||
|
|
250
328
|
value.kind === "counted"
|
|
251
329
|
) {
|
|
252
330
|
// A captured reference is the caller's too: its run-time id is passed as a local's value is.
|
|
253
|
-
const
|
|
331
|
+
const lifted = this.lifted(
|
|
254
332
|
value,
|
|
255
333
|
symbol.name,
|
|
256
334
|
frame,
|
|
257
|
-
parameters,
|
|
258
|
-
runtimeArgs,
|
|
335
|
+
everyValue(frame, parameters),
|
|
259
336
|
);
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
337
|
+
if (value.kind === "code") {
|
|
338
|
+
locals.set(String(value.text), lifted);
|
|
339
|
+
}
|
|
340
|
+
frame.env.set(symbol, lifted);
|
|
341
|
+
passed.set(symbol, lifted);
|
|
265
342
|
}
|
|
266
343
|
}
|
|
267
|
-
const
|
|
344
|
+
for (const [symbol, value] of captured) {
|
|
345
|
+
keys.push({ name: symbol.name, value: passed.get(symbol) ?? value });
|
|
346
|
+
}
|
|
347
|
+
// A captured arrow or literal is lifted after the locals, so a local it holds is the routine's parameter.
|
|
348
|
+
const lift = (parameter: Passing["parameter"]) => {
|
|
349
|
+
for (const [symbol, value] of captured) {
|
|
350
|
+
if (!passed.has(symbol)) {
|
|
351
|
+
frame.env.set(
|
|
352
|
+
symbol,
|
|
353
|
+
this.lifted(value, symbol.name, frame, {
|
|
354
|
+
parameter,
|
|
355
|
+
local: (place) => locals.get(String(place)),
|
|
356
|
+
}),
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
// A run-time part that reads the caller's locals is passed in; any other is read where it is, so the key holds its text.
|
|
362
|
+
const parts: { base: string; text: string; caller: boolean }[] = [];
|
|
363
|
+
lift((text, base) => {
|
|
364
|
+
parts.push({
|
|
365
|
+
base,
|
|
366
|
+
text: String(text),
|
|
367
|
+
caller: Boolean(closure?.caller.names(text)),
|
|
368
|
+
});
|
|
369
|
+
return undefined;
|
|
370
|
+
});
|
|
371
|
+
const key = `${declarationKey(declaration)}|${self?.id ?? ""}|${[
|
|
372
|
+
...keys.map((entry) => specializationKey(entry, { stable: false })),
|
|
373
|
+
...parts.map(({ base, text, caller }) =>
|
|
374
|
+
caller ? `${base}<-` : `${base}@${text}`,
|
|
375
|
+
),
|
|
376
|
+
].join(",")}`;
|
|
377
|
+
const pass = everyValue(frame, parameters).parameter;
|
|
268
378
|
const known = this.specialized.get(key);
|
|
269
|
-
if (known) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
layout: this.resultLayouts.get(known)?.layout,
|
|
276
|
-
length: this.resultLayouts.get(known)?.length,
|
|
277
|
-
returnsStored: this.returnsStored.has(known),
|
|
278
|
-
referents: this.resultReferents(known),
|
|
279
|
-
};
|
|
379
|
+
if (known && this.unfinished.has(known.name)) {
|
|
380
|
+
const named = declaration.name?.getText() ?? "This function";
|
|
381
|
+
throw new SpecializeError(
|
|
382
|
+
declaration.name ?? declaration,
|
|
383
|
+
`${named} calls itself, and a PLC routine can't: each call's state is allocated when the program compiles. Write the repetition as a loop.`,
|
|
384
|
+
);
|
|
280
385
|
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
386
|
+
if (known) {
|
|
387
|
+
lift((text, base, type) =>
|
|
388
|
+
known.parts.has(base) ? pass(text, base, type) : undefined,
|
|
389
|
+
);
|
|
390
|
+
return this.call(
|
|
391
|
+
known.name,
|
|
392
|
+
declaration,
|
|
393
|
+
parameters.map(({ argument }) => argument),
|
|
288
394
|
frame.returns,
|
|
289
395
|
);
|
|
290
|
-
this.expressions.references.returned(result, declaration.body, frame);
|
|
291
|
-
// A void arrow's expression runs for its effect.
|
|
292
|
-
if (!frame.returns || frame.returns.flags & ts.TypeFlags.Void) {
|
|
293
|
-
if (result.kind === "code") {
|
|
294
|
-
frame.emitter.line(mapped`${result.text};`);
|
|
295
|
-
}
|
|
296
|
-
} else {
|
|
297
|
-
frame.emitter.line(
|
|
298
|
-
mapped`return ${this.dialect.code(result, frame.returns)};`,
|
|
299
|
-
);
|
|
300
|
-
}
|
|
301
|
-
} else {
|
|
302
|
-
this.statements.body(declaration.body!, frame);
|
|
303
396
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
397
|
+
const name = this.routineName(declaration, self, keys);
|
|
398
|
+
this.specialized.set(key, { name, parts: new Set() });
|
|
399
|
+
this.unfinished.add(name);
|
|
400
|
+
const callerParts = new Set(
|
|
401
|
+
parts.filter(({ caller }) => caller).map(({ base }) => base),
|
|
402
|
+
);
|
|
403
|
+
const passedParts: { base: string; local: string }[] = [];
|
|
404
|
+
lift((text, base, type) => {
|
|
405
|
+
if (!callerParts.has(base)) {
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
const local = pass(text, base, type);
|
|
409
|
+
passedParts.push({ base, local });
|
|
410
|
+
return local;
|
|
411
|
+
});
|
|
412
|
+
try {
|
|
413
|
+
this.specializeBody(declaration, frame);
|
|
414
|
+
} finally {
|
|
415
|
+
this.unfinished.delete(name);
|
|
315
416
|
}
|
|
316
|
-
const
|
|
417
|
+
const unread = new Set(
|
|
418
|
+
passedParts
|
|
419
|
+
.map(({ local }) => local)
|
|
420
|
+
.filter((local) => !frame.emitter.reads(local)),
|
|
421
|
+
);
|
|
422
|
+
const kept = parameters.filter(({ name }) => !unread.has(name));
|
|
423
|
+
this.specialized.set(key, {
|
|
424
|
+
name,
|
|
425
|
+
parts: new Set(
|
|
426
|
+
passedParts
|
|
427
|
+
.filter(({ local }) => !unread.has(local))
|
|
428
|
+
.map(({ base }) => base),
|
|
429
|
+
),
|
|
430
|
+
});
|
|
431
|
+
const resultText = this.resultText(declaration, frame);
|
|
432
|
+
const head = `${frame.async ? "async " : ""}function ${name}(${kept.map((parameter) => `${parameter.name}: ${parameter.type}`).join(", ")}): ${
|
|
317
433
|
frame.async ? `Promise<${resultText}>` : resultText
|
|
318
434
|
} {`;
|
|
319
435
|
const instance = self?.owner && tsPath(self.owner);
|
|
@@ -327,7 +443,10 @@ export class Specializer implements RoutineTable {
|
|
|
327
443
|
...(instance && { instance }),
|
|
328
444
|
});
|
|
329
445
|
}
|
|
330
|
-
|
|
446
|
+
if (frame.async && self?.captured.kind === "instance") {
|
|
447
|
+
this.asyncOwners.set(name, self);
|
|
448
|
+
}
|
|
449
|
+
this.finished.push(
|
|
331
450
|
joined(
|
|
332
451
|
[
|
|
333
452
|
from(declaration, `${head}${instance ? ` /* ${instance} */` : ""}`),
|
|
@@ -337,36 +456,79 @@ export class Specializer implements RoutineTable {
|
|
|
337
456
|
"\n",
|
|
338
457
|
),
|
|
339
458
|
);
|
|
340
|
-
|
|
341
|
-
this.returnsStored.add(name);
|
|
342
|
-
}
|
|
343
|
-
if (frame.resultLayout) {
|
|
344
|
-
this.resultLayouts.set(name, {
|
|
345
|
-
layout: frame.resultLayout,
|
|
346
|
-
length: frame.resultLength,
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
return {
|
|
350
|
-
name,
|
|
351
|
-
args: runtimeArgs,
|
|
352
|
-
returns: frame.returns,
|
|
353
|
-
iec: this.layouts.iecName(declaration),
|
|
459
|
+
this.results.set(name, {
|
|
354
460
|
layout: frame.resultLayout,
|
|
355
461
|
length: frame.resultLength,
|
|
356
462
|
returnsStored: frame.returnsStored,
|
|
357
|
-
referents:
|
|
358
|
-
};
|
|
463
|
+
referents: frame.referents,
|
|
464
|
+
});
|
|
465
|
+
return this.call(
|
|
466
|
+
name,
|
|
467
|
+
declaration,
|
|
468
|
+
kept.map(({ argument }) => argument),
|
|
469
|
+
frame.returns,
|
|
470
|
+
);
|
|
359
471
|
}
|
|
360
472
|
|
|
361
|
-
|
|
473
|
+
/** A routine's body: its statements, or a one-expression arrow's result. */
|
|
474
|
+
private specializeBody(declaration: FunctionLike, frame: Frame): void {
|
|
475
|
+
if (ts.isArrowFunction(declaration) && !ts.isBlock(declaration.body)) {
|
|
476
|
+
frame.emitter.origin = declaration.body;
|
|
477
|
+
const result = this.expressions.expression(
|
|
478
|
+
declaration.body,
|
|
479
|
+
frame,
|
|
480
|
+
frame.returns,
|
|
481
|
+
);
|
|
482
|
+
this.expressions.references.returned(result, declaration.body, frame);
|
|
483
|
+
if (!frame.returns || frame.returns.flags & ts.TypeFlags.Void) {
|
|
484
|
+
if (result.kind === "code") {
|
|
485
|
+
frame.emitter.line(mapped`${result.text};`);
|
|
486
|
+
}
|
|
487
|
+
} else {
|
|
488
|
+
frame.emitter.line(
|
|
489
|
+
mapped`return ${this.dialect.code(result, frame.returns)};`,
|
|
490
|
+
);
|
|
491
|
+
}
|
|
492
|
+
} else if (declaration.body) {
|
|
493
|
+
this.statements.body(declaration.body, frame);
|
|
494
|
+
} else {
|
|
495
|
+
throw new SpecializeError(declaration, "This function has no body.");
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/** A call to a routine with these runtime arguments: what its callers read from its result. */
|
|
500
|
+
private call(
|
|
362
501
|
name: string,
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
502
|
+
declaration: FunctionLike,
|
|
503
|
+
args: (string | Mapped)[],
|
|
504
|
+
returns: ts.Type | undefined,
|
|
505
|
+
): RoutineCall {
|
|
506
|
+
const result = this.results.get(name);
|
|
507
|
+
const referents = result?.referents;
|
|
508
|
+
return {
|
|
509
|
+
name,
|
|
510
|
+
args,
|
|
511
|
+
returns,
|
|
512
|
+
iec: this.layouts.iecName(declaration),
|
|
513
|
+
layout: result?.layout,
|
|
514
|
+
length: result?.length,
|
|
515
|
+
returnsStored: result?.returnsStored,
|
|
516
|
+
referents: referents && {
|
|
367
517
|
options: [...referents.options],
|
|
368
518
|
optional: referents.optional,
|
|
369
|
-
}
|
|
519
|
+
},
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/** The result type a routine's head declares: an IEC width the declaration names, as `(): UINT` does, wins. */
|
|
524
|
+
private resultText(declaration: FunctionLike, frame: Frame): string {
|
|
525
|
+
if (!frame.returns || frame.returns.flags & ts.TypeFlags.Void) {
|
|
526
|
+
return "void";
|
|
527
|
+
}
|
|
528
|
+
return (
|
|
529
|
+
frame.resultLayout?.text ??
|
|
530
|
+
this.layouts.iecName(declaration) ??
|
|
531
|
+
(frame.referents ? referenceLayout : this.layouts.text(frame.returns))
|
|
370
532
|
);
|
|
371
533
|
}
|
|
372
534
|
|
|
@@ -378,7 +540,7 @@ export class Specializer implements RoutineTable {
|
|
|
378
540
|
private routineName(
|
|
379
541
|
declaration: FunctionLike,
|
|
380
542
|
self: ModelNode | undefined,
|
|
381
|
-
keys: readonly
|
|
543
|
+
keys: readonly Specialization[],
|
|
382
544
|
): string {
|
|
383
545
|
const own =
|
|
384
546
|
declaration.name && ts.isIdentifier(declaration.name)
|
|
@@ -398,15 +560,14 @@ export class Specializer implements RoutineTable {
|
|
|
398
560
|
.join("_");
|
|
399
561
|
const instance = self && this.model.identity(self);
|
|
400
562
|
const specialized = Boolean(instance) || keys.length > 0;
|
|
401
|
-
|
|
402
|
-
|
|
563
|
+
const stable = keys.map((entry) =>
|
|
564
|
+
specializationKey(entry, { stable: true }),
|
|
565
|
+
);
|
|
403
566
|
const base = specialized
|
|
404
567
|
? `${named}_${identityDigest([instance ?? "", ...stable].join("\0"))}`
|
|
405
568
|
: named;
|
|
406
|
-
const name = unusedName(base, (candidate) =>
|
|
407
|
-
|
|
408
|
-
);
|
|
409
|
-
this.routineNames.add(name);
|
|
569
|
+
const name = unusedName(base, (candidate) => this.results.has(candidate));
|
|
570
|
+
this.results.set(name, {});
|
|
410
571
|
return name;
|
|
411
572
|
}
|
|
412
573
|
|
|
@@ -415,53 +576,41 @@ export class Specializer implements RoutineTable {
|
|
|
415
576
|
arg: Value,
|
|
416
577
|
declared: ts.Type,
|
|
417
578
|
frame: Frame,
|
|
418
|
-
parameters:
|
|
419
|
-
|
|
420
|
-
keys: string[],
|
|
579
|
+
parameters: RoutineParameter[],
|
|
580
|
+
keys: Specialization[],
|
|
421
581
|
): void {
|
|
422
582
|
if (ts.isObjectBindingPattern(name)) {
|
|
423
|
-
for (const element of name
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
element.name,
|
|
428
|
-
inner,
|
|
429
|
-
this.checker.getTypeAtLocation(element),
|
|
583
|
+
for (const { element, field } of namedFields(name, "parameter")) {
|
|
584
|
+
this.bindElement(
|
|
585
|
+
element,
|
|
586
|
+
this.expressions.member(arg, field, element, frame),
|
|
430
587
|
frame,
|
|
431
588
|
parameters,
|
|
432
|
-
runtimeArgs,
|
|
433
589
|
keys,
|
|
434
590
|
);
|
|
435
591
|
}
|
|
436
592
|
return;
|
|
437
593
|
}
|
|
438
594
|
if (ts.isArrayBindingPattern(name)) {
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
if (element.dotDotDotToken) {
|
|
444
|
-
throw new SpecializeError(
|
|
445
|
-
element,
|
|
446
|
-
"Name each element of an array parameter; a rest element has no length here.",
|
|
447
|
-
);
|
|
448
|
-
}
|
|
449
|
-
this.bindParameter(
|
|
450
|
-
element.name,
|
|
595
|
+
for (const { element, index } of namedElements(name, "parameter")) {
|
|
596
|
+
this.bindElement(
|
|
597
|
+
element,
|
|
451
598
|
this.expressions.element(arg, constant(index), element, frame),
|
|
452
|
-
this.checker.getTypeAtLocation(element),
|
|
453
599
|
frame,
|
|
454
600
|
parameters,
|
|
455
|
-
runtimeArgs,
|
|
456
601
|
keys,
|
|
457
602
|
);
|
|
458
|
-
}
|
|
603
|
+
}
|
|
459
604
|
return;
|
|
460
605
|
}
|
|
461
|
-
const symbol = this.checker
|
|
606
|
+
const symbol = symbolOf(this.checker, name);
|
|
607
|
+
// A known primitive stays a parameter too, so one routine serves every value.
|
|
462
608
|
if (
|
|
463
609
|
arg.kind === "code" ||
|
|
464
|
-
(arg.kind === "virtual" && !this.isKnownShape(arg))
|
|
610
|
+
(arg.kind === "virtual" && !this.isKnownShape(arg)) ||
|
|
611
|
+
(arg.kind === "const" &&
|
|
612
|
+
!(declared.flags & ts.TypeFlags.Undefined) &&
|
|
613
|
+
arg.value !== undefined)
|
|
465
614
|
) {
|
|
466
615
|
const local = frame.emitter.local(name.text);
|
|
467
616
|
// A name destructured from a field keeps that field's width.
|
|
@@ -473,49 +622,130 @@ export class Specializer implements RoutineTable {
|
|
|
473
622
|
declared,
|
|
474
623
|
ts.IndexKind.Number,
|
|
475
624
|
);
|
|
476
|
-
const
|
|
477
|
-
arg.kind === "
|
|
478
|
-
? arg.
|
|
479
|
-
:
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
parameters.push(
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
625
|
+
const literal =
|
|
626
|
+
arg.kind === "virtual" && arg.items && element
|
|
627
|
+
? arrayLayout(this.layouts.text(element), arg.items.length)
|
|
628
|
+
: undefined;
|
|
629
|
+
const layout = arg.kind === "code" ? arg.layout : literal;
|
|
630
|
+
const type = iec ?? layout?.text ?? this.layouts.text(declared);
|
|
631
|
+
parameters.push({
|
|
632
|
+
name: local,
|
|
633
|
+
type,
|
|
634
|
+
argument: this.dialect.assignmentText(arg, declared, iec),
|
|
635
|
+
});
|
|
636
|
+
// A PLC parameter is read-only; one the routine assigns, as JavaScript allows, is a local copy.
|
|
637
|
+
const routine = ts.findAncestor(name, ts.isFunctionLike);
|
|
638
|
+
const assigned =
|
|
639
|
+
routine !== undefined &&
|
|
640
|
+
"body" in routine &&
|
|
641
|
+
routine.body !== undefined &&
|
|
642
|
+
assignsSymbol(routine.body, new Set([symbol]), this.checker);
|
|
643
|
+
const held = assigned ? frame.emitter.local(name.text) : local;
|
|
644
|
+
if (assigned) {
|
|
645
|
+
frame.emitter.line(`let ${held}: ${type} = ${local};`);
|
|
646
|
+
}
|
|
486
647
|
frame.env.set(symbol, {
|
|
487
648
|
kind: "code",
|
|
488
|
-
text:
|
|
649
|
+
text: held,
|
|
489
650
|
declared,
|
|
490
651
|
place: true,
|
|
652
|
+
iec,
|
|
491
653
|
layout,
|
|
492
654
|
// A copy of stored state stays one in the routine it is passed to.
|
|
493
655
|
copyOf: arg.kind === "code" ? arg.copyOf : undefined,
|
|
494
656
|
});
|
|
495
|
-
keys.push(
|
|
657
|
+
keys.push({
|
|
658
|
+
name: name.text,
|
|
659
|
+
value: `code${layout ? `:${layout.text}` : ""}`,
|
|
660
|
+
width: iec,
|
|
661
|
+
});
|
|
496
662
|
return;
|
|
497
663
|
}
|
|
498
|
-
|
|
499
|
-
arg
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
664
|
+
const lifted = this.lifted(
|
|
665
|
+
arg,
|
|
666
|
+
name.text,
|
|
667
|
+
frame,
|
|
668
|
+
everyValue(frame, parameters),
|
|
669
|
+
);
|
|
670
|
+
frame.env.set(symbol, lifted);
|
|
671
|
+
keys.push({ name: name.text, value: lifted });
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* A pattern element's default is worked out in the routine, as a parameter's is: it may read an
|
|
676
|
+
* earlier parameter, which the caller passing the part cannot name.
|
|
677
|
+
*/
|
|
678
|
+
private bindElement(
|
|
679
|
+
element: ts.BindingElement,
|
|
680
|
+
part: Value,
|
|
681
|
+
frame: Frame,
|
|
682
|
+
parameters: RoutineParameter[],
|
|
683
|
+
keys: Specialization[],
|
|
684
|
+
): void {
|
|
685
|
+
const declared = this.checker.getTypeAtLocation(element);
|
|
686
|
+
if (!element.initializer) {
|
|
687
|
+
this.bindParameter(element.name, part, declared, frame, parameters, keys);
|
|
688
|
+
return;
|
|
689
|
+
}
|
|
690
|
+
if (part.kind === "const" && part.value === undefined) {
|
|
691
|
+
this.bindDefault(
|
|
692
|
+
element,
|
|
693
|
+
element.initializer,
|
|
510
694
|
declared,
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
695
|
+
frame,
|
|
696
|
+
parameters,
|
|
697
|
+
keys,
|
|
698
|
+
);
|
|
514
699
|
return;
|
|
515
700
|
}
|
|
516
|
-
const
|
|
517
|
-
|
|
518
|
-
|
|
701
|
+
const optional = runTimeOptional(this.checker, part);
|
|
702
|
+
if (!optional) {
|
|
703
|
+
this.bindParameter(element.name, part, declared, frame, parameters, keys);
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
this.expressions.bind(
|
|
707
|
+
element.name,
|
|
708
|
+
this.expressions.defaulted(
|
|
709
|
+
this.passedPart(element, optional, frame, parameters, keys),
|
|
710
|
+
element.initializer,
|
|
711
|
+
frame,
|
|
712
|
+
),
|
|
713
|
+
frame,
|
|
714
|
+
declared,
|
|
715
|
+
{ mutable: true, iec: this.layouts.iecName(element) },
|
|
716
|
+
);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/** A pattern part that may be undefined at run time, passed to the routine whole, as the routine reads it. */
|
|
720
|
+
private passedPart(
|
|
721
|
+
element: ts.BindingElement,
|
|
722
|
+
part: RunTimeOptional,
|
|
723
|
+
frame: Frame,
|
|
724
|
+
parameters: RoutineParameter[],
|
|
725
|
+
keys: Specialization[],
|
|
726
|
+
): Value {
|
|
727
|
+
const name = element.name.getText();
|
|
728
|
+
const base = ts.isIdentifier(element.name) ? element.name.text : "part";
|
|
729
|
+
if (part.kind === "choice") {
|
|
730
|
+
const lifted = this.lifted(
|
|
731
|
+
part,
|
|
732
|
+
base,
|
|
733
|
+
frame,
|
|
734
|
+
everyValue(frame, parameters),
|
|
735
|
+
);
|
|
736
|
+
// A narrowed choice passed whole has the same key, but its routine has no fallback.
|
|
737
|
+
keys.push({ name, value: lifted }, { name, value: "default?" });
|
|
738
|
+
return lifted;
|
|
739
|
+
}
|
|
740
|
+
const local = frame.emitter.local(base);
|
|
741
|
+
parameters.push({
|
|
742
|
+
name: local,
|
|
743
|
+
type: this.layouts.text(part.declared),
|
|
744
|
+
argument: this.dialect.assignmentText(part, part.declared, undefined),
|
|
745
|
+
});
|
|
746
|
+
// Unlike a default bound in the routine, the part is passed, so the routine has one more parameter.
|
|
747
|
+
keys.push({ name, value: "code?" });
|
|
748
|
+
return { kind: "code", text: local, declared: part.declared, place: true };
|
|
519
749
|
}
|
|
520
750
|
|
|
521
751
|
/**
|
|
@@ -526,24 +756,51 @@ export class Specializer implements RoutineTable {
|
|
|
526
756
|
value: Value,
|
|
527
757
|
base: string,
|
|
528
758
|
frame: Frame,
|
|
529
|
-
|
|
530
|
-
runtimeArgs: (string | Mapped)[],
|
|
759
|
+
passing: Passing,
|
|
531
760
|
): Value {
|
|
761
|
+
const lift = (part: Value, name: string) =>
|
|
762
|
+
this.lifted(part, name, frame, passing);
|
|
763
|
+
// A counted array passes its local array and each bound decided at run time.
|
|
532
764
|
if (value.kind === "counted") {
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
765
|
+
const { source } = value;
|
|
766
|
+
const start = lift(value.start, `${base}_start`);
|
|
767
|
+
const end = lift(value.end, `${base}_end`);
|
|
768
|
+
if (source.kind === "items") {
|
|
769
|
+
return {
|
|
770
|
+
kind: "counted",
|
|
771
|
+
source: {
|
|
772
|
+
kind: "items",
|
|
773
|
+
items: source.items.map((item, index) =>
|
|
774
|
+
lift(item, `${base}_${index}`),
|
|
775
|
+
),
|
|
776
|
+
},
|
|
777
|
+
start,
|
|
778
|
+
end,
|
|
779
|
+
};
|
|
780
|
+
}
|
|
781
|
+
const local = passing.parameter(source.text, base, () => source.layout);
|
|
782
|
+
if (local === undefined) {
|
|
783
|
+
return { ...value, start, end };
|
|
784
|
+
}
|
|
785
|
+
return {
|
|
786
|
+
kind: "counted",
|
|
787
|
+
source: {
|
|
788
|
+
...source,
|
|
789
|
+
text: local,
|
|
790
|
+
element: (index) => reread(source.element(index), local, index),
|
|
791
|
+
// The routine is passed the end as it is now, which a count it never saw can't be compared with.
|
|
792
|
+
filled: undefined,
|
|
793
|
+
},
|
|
794
|
+
start,
|
|
795
|
+
end,
|
|
796
|
+
};
|
|
541
797
|
}
|
|
542
798
|
// A reference chosen at run time passes its id; the routine specializes on what it can be.
|
|
543
799
|
if (value.kind === "choice") {
|
|
544
|
-
const local =
|
|
545
|
-
|
|
546
|
-
|
|
800
|
+
const local = passing.parameter(value.index, base, () => referenceLayout);
|
|
801
|
+
if (local === undefined) {
|
|
802
|
+
return value;
|
|
803
|
+
}
|
|
547
804
|
return {
|
|
548
805
|
kind: "choice",
|
|
549
806
|
options: value.options,
|
|
@@ -555,29 +812,23 @@ export class Specializer implements RoutineTable {
|
|
|
555
812
|
return {
|
|
556
813
|
...value,
|
|
557
814
|
entries: value.entries.map((entry, index) => ({
|
|
558
|
-
item:
|
|
559
|
-
|
|
560
|
-
`${base}_${index}`,
|
|
561
|
-
frame,
|
|
562
|
-
parameters,
|
|
563
|
-
runtimeArgs,
|
|
564
|
-
),
|
|
565
|
-
present: this.lifted(
|
|
566
|
-
entry.present,
|
|
567
|
-
`${base}_${index}_present`,
|
|
568
|
-
frame,
|
|
569
|
-
parameters,
|
|
570
|
-
runtimeArgs,
|
|
571
|
-
),
|
|
815
|
+
item: lift(entry.item, `${base}_${index}`),
|
|
816
|
+
present: lift(entry.present, `${base}_${index}_present`),
|
|
572
817
|
})),
|
|
818
|
+
order: value.order && lift(value.order, `${base}_order`),
|
|
573
819
|
};
|
|
574
820
|
}
|
|
575
821
|
if (value.kind === "code") {
|
|
576
|
-
const
|
|
577
|
-
|
|
578
|
-
|
|
822
|
+
const own = value.place ? passing.local(value.text) : undefined;
|
|
823
|
+
if (own) {
|
|
824
|
+
return own;
|
|
825
|
+
}
|
|
826
|
+
const local = passing.parameter(value.text, base, () =>
|
|
827
|
+
this.expressions.heldType(value, parameterTyping),
|
|
579
828
|
);
|
|
580
|
-
|
|
829
|
+
if (local === undefined) {
|
|
830
|
+
return value;
|
|
831
|
+
}
|
|
581
832
|
return {
|
|
582
833
|
kind: "code",
|
|
583
834
|
text: local,
|
|
@@ -596,13 +847,7 @@ export class Specializer implements RoutineTable {
|
|
|
596
847
|
.filter(([symbol]) => read.has(symbol))
|
|
597
848
|
.map(([symbol, captured]) => [
|
|
598
849
|
symbol,
|
|
599
|
-
|
|
600
|
-
captured,
|
|
601
|
-
`${base}_${symbol.name}`,
|
|
602
|
-
frame,
|
|
603
|
-
parameters,
|
|
604
|
-
runtimeArgs,
|
|
605
|
-
),
|
|
850
|
+
lift(captured, `${base}_${symbol.name}`),
|
|
606
851
|
]),
|
|
607
852
|
),
|
|
608
853
|
};
|
|
@@ -613,21 +858,13 @@ export class Specializer implements RoutineTable {
|
|
|
613
858
|
return {
|
|
614
859
|
...value,
|
|
615
860
|
...(value.items && {
|
|
616
|
-
items: value.items.map((item, index) =>
|
|
617
|
-
this.lifted(item, `${base}_${index}`, frame, parameters, runtimeArgs),
|
|
618
|
-
),
|
|
861
|
+
items: value.items.map((item, index) => lift(item, `${base}_${index}`)),
|
|
619
862
|
}),
|
|
620
863
|
...(value.fields && {
|
|
621
864
|
fields: new Map(
|
|
622
865
|
[...value.fields].map(([field, item]) => [
|
|
623
866
|
field,
|
|
624
|
-
|
|
625
|
-
item,
|
|
626
|
-
`${base}_${field}`,
|
|
627
|
-
frame,
|
|
628
|
-
parameters,
|
|
629
|
-
runtimeArgs,
|
|
630
|
-
),
|
|
867
|
+
lift(item, `${base}_${field}`),
|
|
631
868
|
]),
|
|
632
869
|
),
|
|
633
870
|
}),
|
|
@@ -635,52 +872,24 @@ export class Specializer implements RoutineTable {
|
|
|
635
872
|
}
|
|
636
873
|
|
|
637
874
|
/**
|
|
638
|
-
* A
|
|
639
|
-
*
|
|
640
|
-
* worked out when the routine starts, as JavaScript does.
|
|
875
|
+
* A default setup fixes is a known argument like any other; one read from scan state or an earlier
|
|
876
|
+
* parameter is worked out when the routine starts, as JavaScript does.
|
|
641
877
|
*/
|
|
642
|
-
private isUndefined(arg: Value, parameter: ts.ParameterDeclaration): boolean {
|
|
643
|
-
if (arg.kind === "const") {
|
|
644
|
-
return arg.value === undefined;
|
|
645
|
-
}
|
|
646
|
-
if (
|
|
647
|
-
arg.kind === "code" &&
|
|
648
|
-
arg.declared &&
|
|
649
|
-
arg.declared.flags & ts.TypeFlags.Union
|
|
650
|
-
) {
|
|
651
|
-
if (this.checker.getNonNullableType(arg.declared) !== arg.declared) {
|
|
652
|
-
throw new SpecializeError(
|
|
653
|
-
parameter,
|
|
654
|
-
"Pass a defined value or leave the argument out: the default applies to a missing argument.",
|
|
655
|
-
);
|
|
656
|
-
}
|
|
657
|
-
}
|
|
658
|
-
return false;
|
|
659
|
-
}
|
|
660
|
-
|
|
661
878
|
private bindDefault(
|
|
662
|
-
parameter: ts.ParameterDeclaration,
|
|
879
|
+
parameter: ts.ParameterDeclaration | ts.BindingElement,
|
|
880
|
+
initializer: ts.Expression,
|
|
663
881
|
declared: ts.Type,
|
|
664
882
|
frame: Frame,
|
|
665
|
-
parameters:
|
|
666
|
-
|
|
667
|
-
keys: string[],
|
|
883
|
+
parameters: RoutineParameter[],
|
|
884
|
+
keys: Specialization[],
|
|
668
885
|
): void {
|
|
669
|
-
const value = this.expressions.expression(
|
|
670
|
-
parameter.initializer!,
|
|
671
|
-
frame,
|
|
672
|
-
declared,
|
|
673
|
-
);
|
|
674
|
-
// A default read at run time is a local; one known now, such as `[]`, is that value.
|
|
886
|
+
const value = this.expressions.expression(initializer, frame, declared);
|
|
675
887
|
if (value.kind !== "const") {
|
|
676
|
-
this.expressions.bind(
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
declared,
|
|
682
|
-
);
|
|
683
|
-
keys.push(`${parameter.name.getText()}=default`);
|
|
888
|
+
this.expressions.bind(parameter.name, value, frame, declared, {
|
|
889
|
+
mutable: value.kind === "code",
|
|
890
|
+
iec: this.layouts.iecName(parameter),
|
|
891
|
+
});
|
|
892
|
+
keys.push({ name: parameter.name.getText(), value: "default" });
|
|
684
893
|
return;
|
|
685
894
|
}
|
|
686
895
|
this.bindParameter(
|
|
@@ -689,7 +898,6 @@ export class Specializer implements RoutineTable {
|
|
|
689
898
|
declared,
|
|
690
899
|
frame,
|
|
691
900
|
parameters,
|
|
692
|
-
runtimeArgs,
|
|
693
901
|
keys,
|
|
694
902
|
);
|
|
695
903
|
}
|
|
@@ -700,9 +908,8 @@ export class Specializer implements RoutineTable {
|
|
|
700
908
|
args: readonly Value[],
|
|
701
909
|
declared: ts.Type,
|
|
702
910
|
frame: Frame,
|
|
703
|
-
parameters:
|
|
704
|
-
|
|
705
|
-
keys: string[],
|
|
911
|
+
parameters: RoutineParameter[],
|
|
912
|
+
keys: Specialization[],
|
|
706
913
|
): void {
|
|
707
914
|
if (!ts.isIdentifier(parameter.name)) {
|
|
708
915
|
throw new SpecializeError(
|
|
@@ -716,17 +923,20 @@ export class Specializer implements RoutineTable {
|
|
|
716
923
|
const name = parameter.name.text;
|
|
717
924
|
const items = args.map((arg, index): Value => {
|
|
718
925
|
if (arg.kind !== "code" && arg.kind !== "const") {
|
|
719
|
-
keys.push(`${name}[${index}]
|
|
926
|
+
keys.push({ name: `${name}[${index}]`, value: arg });
|
|
720
927
|
return arg;
|
|
721
928
|
}
|
|
722
929
|
const local = frame.emitter.local(`${name}_${index}`);
|
|
723
930
|
const iec = arg.kind === "code" ? arg.iec : undefined;
|
|
724
|
-
parameters.push(
|
|
725
|
-
|
|
726
|
-
|
|
931
|
+
parameters.push({
|
|
932
|
+
name: local,
|
|
933
|
+
type: iec ?? this.layouts.text(element),
|
|
934
|
+
argument: this.dialect.assignmentText(arg, element, iec),
|
|
935
|
+
});
|
|
936
|
+
keys.push({ name: `${name}[${index}]`, value: "code" });
|
|
727
937
|
return { kind: "code", text: local, declared: element, place: true, iec };
|
|
728
938
|
});
|
|
729
|
-
frame.env.set(this.checker
|
|
939
|
+
frame.env.set(symbolOf(this.checker, parameter.name), {
|
|
730
940
|
kind: "virtual",
|
|
731
941
|
items,
|
|
732
942
|
});
|
|
@@ -739,13 +949,8 @@ export class Specializer implements RoutineTable {
|
|
|
739
949
|
}
|
|
740
950
|
const inner = [...(value.fields?.values() ?? []), ...(value.items ?? [])];
|
|
741
951
|
return (
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
item.kind === "static" ||
|
|
745
|
-
item.kind === "function" ||
|
|
746
|
-
item.kind === "choice" ||
|
|
747
|
-
this.isKnownShape(item),
|
|
748
|
-
) ||
|
|
952
|
+
holdsSetupObjects(value) ||
|
|
953
|
+
inner.some((item) => this.isKnownShape(item)) ||
|
|
749
954
|
(value.items !== undefined &&
|
|
750
955
|
inner.every((item) => item.kind === "const"))
|
|
751
956
|
);
|
|
@@ -754,34 +959,138 @@ export class Specializer implements RoutineTable {
|
|
|
754
959
|
|
|
755
960
|
/** Where a routine ends: the closing brace of its body, or the whole of a one-expression arrow. */
|
|
756
961
|
export const closingBrace = (declaration: FunctionLike): ts.Node =>
|
|
757
|
-
declaration.body &&
|
|
758
|
-
|
|
759
|
-
|
|
962
|
+
(declaration.body &&
|
|
963
|
+
ts.isBlock(declaration.body) &&
|
|
964
|
+
declaration.body.getLastToken()) ||
|
|
965
|
+
declaration;
|
|
966
|
+
|
|
967
|
+
/** A specialized routine's result as its callers read it, once its body is specialized. */
|
|
968
|
+
interface RoutineResult {
|
|
969
|
+
layout?: ArrayLayout;
|
|
970
|
+
length?: number;
|
|
971
|
+
returnsStored?: boolean;
|
|
972
|
+
referents?: Referents;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
/** What one parameter or captured name was specialized for: a known value, or how a runtime one is passed. */
|
|
976
|
+
interface Specialization {
|
|
977
|
+
name: string;
|
|
978
|
+
value: Value | string;
|
|
979
|
+
/**
|
|
980
|
+
* The IEC width a runtime parameter takes from its argument. Calls at different widths need a
|
|
981
|
+
* routine each, but the routine's name leaves it out, so names stay as they were.
|
|
982
|
+
*/
|
|
983
|
+
width?: string;
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
/** One parameter of a routine, and what its call passes for it. */
|
|
987
|
+
interface RoutineParameter {
|
|
988
|
+
name: string;
|
|
989
|
+
type: string;
|
|
990
|
+
argument: string | Mapped;
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
/** A routine specialized for one key, and the run-time parts of what it captured that it is passed. */
|
|
994
|
+
interface Specialized {
|
|
995
|
+
name: string;
|
|
996
|
+
parts: ReadonlySet<string>;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
/** How a routine is given the runtime values lifted into it. */
|
|
1000
|
+
interface Passing {
|
|
1001
|
+
/** The parameter named from `base` that passes `text`; undefined when the routine reads `text` as it is. */
|
|
1002
|
+
parameter: (
|
|
1003
|
+
text: string | Mapped,
|
|
1004
|
+
base: string,
|
|
1005
|
+
type: () => string,
|
|
1006
|
+
) => string | undefined;
|
|
1007
|
+
/** A caller's local the routine already has a parameter for. */
|
|
1008
|
+
local: (place: string | Mapped) => Value | undefined;
|
|
1009
|
+
}
|
|
1010
|
+
|
|
1011
|
+
/** Passes each runtime value lifted into a routine as a parameter of its own, which the caller passes. */
|
|
1012
|
+
const everyValue = (frame: Frame, parameters: RoutineParameter[]) =>
|
|
1013
|
+
({
|
|
1014
|
+
parameter: (text: string | Mapped, base: string, type: () => string) => {
|
|
1015
|
+
const local = frame.emitter.local(base);
|
|
1016
|
+
parameters.push({ name: local, type: type(), argument: text });
|
|
1017
|
+
return local;
|
|
1018
|
+
},
|
|
1019
|
+
local: () => undefined,
|
|
1020
|
+
}) satisfies Passing;
|
|
1021
|
+
|
|
1022
|
+
/**
|
|
1023
|
+
* A routine parameter is typed as the value passed to it says: the IEC width it carries, else its
|
|
1024
|
+
* declared type as written.
|
|
1025
|
+
*/
|
|
1026
|
+
const parameterTyping: Typing = {
|
|
1027
|
+
width: true,
|
|
1028
|
+
widen: false,
|
|
1029
|
+
untyped: "number",
|
|
1030
|
+
};
|
|
760
1031
|
|
|
761
1032
|
const declarationKey = (declaration: ts.Node): string =>
|
|
762
1033
|
`${declaration.getSourceFile().fileName}:${declaration.pos}`;
|
|
763
1034
|
|
|
764
|
-
const
|
|
1035
|
+
const specializationKey = (
|
|
1036
|
+
{ name, value, width }: Specialization,
|
|
1037
|
+
options: { stable: boolean },
|
|
1038
|
+
): string =>
|
|
1039
|
+
`${name}=${typeof value === "string" ? value : valueKey(value, options)}${!options.stable && width ? `:${width}` : ""}`;
|
|
1040
|
+
|
|
1041
|
+
/**
|
|
1042
|
+
* What tells a value apart to the routine specialized for it. The stable key, which names the
|
|
1043
|
+
* routine, leaves out a callback's source position, so a name doesn't move with an edit above it.
|
|
1044
|
+
*/
|
|
1045
|
+
const valueKey = (value: Value, options: { stable: boolean }): string => {
|
|
1046
|
+
const key = (inner: Value) => valueKey(inner, options);
|
|
765
1047
|
switch (value.kind) {
|
|
766
1048
|
case "static":
|
|
767
1049
|
return `#${value.node.reference}`;
|
|
768
1050
|
case "const":
|
|
769
1051
|
return `=${String(value.value)}`;
|
|
770
1052
|
case "function":
|
|
771
|
-
return
|
|
1053
|
+
return options.stable
|
|
1054
|
+
? "fn"
|
|
1055
|
+
: `fn:${declarationKey(value.declaration)}:${value.self?.id ?? ""}`;
|
|
772
1056
|
case "virtual":
|
|
773
1057
|
return value.items
|
|
774
|
-
? `[${value.items.map(
|
|
775
|
-
: `{${[...(value.fields ?? [])].map(([name, inner]) => `${name}:${
|
|
1058
|
+
? `[${value.items.map(key).join(",")}]`
|
|
1059
|
+
: `{${[...(value.fields ?? [])].map(([name, inner]) => `${name}:${key(inner)}`).join(",")}}`;
|
|
776
1060
|
case "code":
|
|
777
1061
|
return "code";
|
|
778
1062
|
case "choice":
|
|
779
1063
|
return `ref(${value.options.map((option) => option.reference).join(",")})${value.optional ? "?" : ""}`;
|
|
780
1064
|
case "masked":
|
|
781
|
-
return `some[${value.entries.map((entry) => `${
|
|
1065
|
+
return `some[${value.entries.map((entry) => `${key(entry.item)}:${key(entry.present)}`).join(",")}]${value.order ? `by ${key(value.order)}` : ""}`;
|
|
782
1066
|
case "counted":
|
|
783
|
-
return countedKey(value,
|
|
1067
|
+
return countedKey(value, key);
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
|
|
1071
|
+
/** What tells two counted arguments apart to the routine specialized for them. */
|
|
1072
|
+
const countedKey = (
|
|
1073
|
+
counted: Counted,
|
|
1074
|
+
key: (value: Value) => string,
|
|
1075
|
+
): string => {
|
|
1076
|
+
const { source } = counted;
|
|
1077
|
+
const bounds = `${key(counted.start)}..${key(counted.end)}`;
|
|
1078
|
+
return source.kind === "items"
|
|
1079
|
+
? `counted[${source.items.map(key).join(",")}]${bounds}`
|
|
1080
|
+
: `counted:${source.layout}:${key(source.element("0"))}:${bounds}`;
|
|
1081
|
+
};
|
|
1082
|
+
|
|
1083
|
+
/** An element as a routine reads it from the array it was passed: the same slot of its own parameter. */
|
|
1084
|
+
const reread = (
|
|
1085
|
+
sample: Value,
|
|
1086
|
+
array: string,
|
|
1087
|
+
index: string | Mapped,
|
|
1088
|
+
): Value => {
|
|
1089
|
+
const place = mapped`${array}[${index}]`;
|
|
1090
|
+
if (sample.kind === "choice") {
|
|
1091
|
+
return { ...sample, index: place };
|
|
784
1092
|
}
|
|
1093
|
+
return sample.kind === "code" ? { ...sample, text: place } : sample;
|
|
785
1094
|
};
|
|
786
1095
|
|
|
787
1096
|
/** The symbols a function's body reads, which are all of its closure it needs. */
|
|
@@ -792,7 +1101,7 @@ const readSymbols = (
|
|
|
792
1101
|
const read = new Set<ts.Symbol>();
|
|
793
1102
|
const visit = (node: ts.Node): void => {
|
|
794
1103
|
if (ts.isIdentifier(node)) {
|
|
795
|
-
const symbol = checker
|
|
1104
|
+
const symbol = valueSymbol(checker, node);
|
|
796
1105
|
if (symbol) {
|
|
797
1106
|
read.add(symbol);
|
|
798
1107
|
}
|