@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,34 @@
|
|
|
1
|
+
import type { PlcType } from "@tsplc/compiler/ir";
|
|
2
|
+
|
|
3
|
+
type CalendarDomain = Extract<PlcType, { kind: "calendar" }>["domain"];
|
|
4
|
+
|
|
5
|
+
/** The SDK's TIME constructors. */
|
|
6
|
+
export const durationNames: ReadonlySet<string> = new Set([
|
|
7
|
+
"ms",
|
|
8
|
+
"seconds",
|
|
9
|
+
"minutes",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/** The SDK's calendar constructors: the domain each makes, its IEC type and the text it takes. */
|
|
13
|
+
export const calendarFormats = new Map<
|
|
14
|
+
string,
|
|
15
|
+
{ domain: CalendarDomain; type: string; format: string }
|
|
16
|
+
>([
|
|
17
|
+
["date", { domain: "date", type: "DATE", format: "YYYY-MM-DD" }],
|
|
18
|
+
[
|
|
19
|
+
"timeOfDay",
|
|
20
|
+
{ domain: "timeOfDay", type: "TIME_OF_DAY", format: "hh:mm:ss[.mmm]" },
|
|
21
|
+
],
|
|
22
|
+
[
|
|
23
|
+
"dateAndTime",
|
|
24
|
+
{
|
|
25
|
+
domain: "dateTime",
|
|
26
|
+
type: "DATE_AND_TIME",
|
|
27
|
+
format: "YYYY-MM-DDThh:mm:ss",
|
|
28
|
+
},
|
|
29
|
+
],
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
/** The calendar constructor that makes a value of the IEC type `type`, as `date` makes a DATE. */
|
|
33
|
+
export const calendarConstructor = (type: string): string | undefined =>
|
|
34
|
+
[...calendarFormats].find(([, format]) => format.type === type)?.[0];
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { Mapped } from "../positions";
|
|
3
|
+
import { isWrapper } from "../types";
|
|
4
|
+
import type { CapturedPrimitive } from "./capture-protocol";
|
|
5
|
+
import type { Frame } from "./frame";
|
|
6
|
+
import type { ArrayLayout } from "./layouts";
|
|
7
|
+
import type { ModelNode } from "./model";
|
|
8
|
+
|
|
9
|
+
export type FunctionLike =
|
|
10
|
+
| ts.FunctionDeclaration
|
|
11
|
+
| ts.MethodDeclaration
|
|
12
|
+
| ts.ArrowFunction
|
|
13
|
+
| ts.FunctionExpression
|
|
14
|
+
| ts.GetAccessorDeclaration;
|
|
15
|
+
|
|
16
|
+
/** What an expression is while specializing: known now, or code the PLC runs. */
|
|
17
|
+
export type Value =
|
|
18
|
+
/** An object setup built: a class instance, record, array, function or service. */
|
|
19
|
+
| { kind: "static"; node: ModelNode }
|
|
20
|
+
/** A literal or folded primitive. `undefined` is a value too. */
|
|
21
|
+
| { kind: "const"; value: number | boolean | string | undefined | bigint }
|
|
22
|
+
/** An array or record built in scan code from known parts, used before it needs storage. */
|
|
23
|
+
| { kind: "virtual"; items?: Value[]; fields?: Map<string, Value> }
|
|
24
|
+
/** A function value: a module function, method of a known instance, or a local arrow. */
|
|
25
|
+
| { kind: "function"; declaration: FunctionLike; self?: ModelNode; env?: Env }
|
|
26
|
+
/** Code the PLC runs. `declared` is the storage's declared type, before narrowing. */
|
|
27
|
+
| {
|
|
28
|
+
kind: "code";
|
|
29
|
+
text: string | Mapped;
|
|
30
|
+
declared?: ts.Type;
|
|
31
|
+
place?: boolean;
|
|
32
|
+
iec?: string;
|
|
33
|
+
/** For an array place, the IEC width its declaration gives its elements, as `Array<BYTE>` does. */
|
|
34
|
+
elementIec?: string;
|
|
35
|
+
/** For an array place, its layout with the length setup gave it, such as `FixedArray<BYTE, 32>`. */
|
|
36
|
+
layout?: ArrayLayout;
|
|
37
|
+
/** For an array place, the length setup gave it, so scan code can visit each element. */
|
|
38
|
+
length?: number;
|
|
39
|
+
/** For a place the config's `io` binds to an input, or one holding such a place: the hardware writes it every scan. */
|
|
40
|
+
input?: { path: string; address: string };
|
|
41
|
+
/**
|
|
42
|
+
* For a place reached through a runtime index, the same place with its indexes held in
|
|
43
|
+
* consts, so a name for it keeps meaning the element it named when it was bound.
|
|
44
|
+
*/
|
|
45
|
+
pinned?: (frame: Frame) => Value;
|
|
46
|
+
/**
|
|
47
|
+
* A copy of stored state a routine returned, named for the error: TypeScript would hand back
|
|
48
|
+
* the stored object, so writing it here would change nothing the PLC keeps.
|
|
49
|
+
*/
|
|
50
|
+
copyOf?: string;
|
|
51
|
+
/** A float the program made whole with Math.trunc, floor, ceil or round, which an integer place takes saturated. */
|
|
52
|
+
whole?: true;
|
|
53
|
+
/** What an `await` gives, typed by the call it awaited. */
|
|
54
|
+
awaited?: boolean;
|
|
55
|
+
/** What a task's method returns, typed by the task's declaration in its kept module. */
|
|
56
|
+
typedByTask?: boolean;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A reference scan code chooses at run time, among objects setup built. `index` is code for the
|
|
60
|
+
* id of the object it holds, one of `options`, or `noReference` for undefined.
|
|
61
|
+
*/
|
|
62
|
+
| {
|
|
63
|
+
kind: "choice";
|
|
64
|
+
options: readonly ModelNode[];
|
|
65
|
+
index: string | Mapped;
|
|
66
|
+
optional: boolean;
|
|
67
|
+
/** Whether TypeScript narrowed out undefined where this was read, though `optional` says its storage may hold none. */
|
|
68
|
+
narrowed?: boolean;
|
|
69
|
+
/** Whether `index` is storage scan code can assign: a reference field, or a let. */
|
|
70
|
+
place?: boolean;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* An array of known values a filter decided at run time: every candidate, each with whether the
|
|
74
|
+
* array holds it. `order` is the comparator of a `toSorted` over it, which nothing has run yet.
|
|
75
|
+
*/
|
|
76
|
+
| {
|
|
77
|
+
kind: "masked";
|
|
78
|
+
entries: readonly { item: Value; present: Value }[];
|
|
79
|
+
order?: Value;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* An array whose length is decided at run time: a `toArray()` snapshot, or a slice with runtime
|
|
83
|
+
* bounds. Its storage is fixed, and the elements from `start` up to `end` are the ones it has.
|
|
84
|
+
*/
|
|
85
|
+
| {
|
|
86
|
+
kind: "counted";
|
|
87
|
+
source: CountedSource;
|
|
88
|
+
/** Indexes into the storage, known now or held in consts: 0 <= start <= end <= its length. */
|
|
89
|
+
start: Value;
|
|
90
|
+
end: Value;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Where a counted array's elements are read from: elements setup fixed, each known by itself, or an
|
|
95
|
+
* array the PLC stores whole, which is a snapshot's local or a runtime array a routine was passed.
|
|
96
|
+
*/
|
|
97
|
+
export type CountedSource =
|
|
98
|
+
| { kind: "items"; items: readonly Value[] }
|
|
99
|
+
| {
|
|
100
|
+
kind: "array";
|
|
101
|
+
text: string | Mapped;
|
|
102
|
+
/** The array's PLC type, as a routine it is passed to declares it. */
|
|
103
|
+
layout: string;
|
|
104
|
+
/** How many elements the array has room for. */
|
|
105
|
+
bound: number;
|
|
106
|
+
/** One element of the array, given the code that indexes it. */
|
|
107
|
+
element: (index: string | Mapped) => Value;
|
|
108
|
+
/** Whether the array is a copy nothing else writes, so a slice of it can be kept. */
|
|
109
|
+
snapshot: boolean;
|
|
110
|
+
/** For a snapshot, how many slots it stored into; the rest still hold the element type's empty value. */
|
|
111
|
+
filled?: Value;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type Counted = Extract<Value, { kind: "counted" }>;
|
|
115
|
+
|
|
116
|
+
export type Env = Map<ts.Symbol, Value>;
|
|
117
|
+
|
|
118
|
+
export const constant = (
|
|
119
|
+
value: number | boolean | string | undefined | bigint,
|
|
120
|
+
): Value => ({
|
|
121
|
+
kind: "const",
|
|
122
|
+
value,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
/** A primitive setup captured, as a value known while specializing. */
|
|
126
|
+
export const capturedPrimitive = (value: CapturedPrimitive): Value => {
|
|
127
|
+
switch (value.kind) {
|
|
128
|
+
case "undefined":
|
|
129
|
+
return constant(undefined);
|
|
130
|
+
case "bigint":
|
|
131
|
+
return constant(BigInt(value.value));
|
|
132
|
+
default:
|
|
133
|
+
return constant(value.value);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/** A number setup fixed; undefined for one decided at run time. */
|
|
138
|
+
export const known = (value: Value): number | undefined =>
|
|
139
|
+
value.kind === "const" && typeof value.value === "number"
|
|
140
|
+
? value.value
|
|
141
|
+
: undefined;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A number folded now, which the PLC can hold only when finite: NaN and the infinities have no PLC
|
|
145
|
+
* value, as setup capture finds too.
|
|
146
|
+
*/
|
|
147
|
+
export const finite = <T>(value: T, at: ts.Node): T => {
|
|
148
|
+
if (typeof value === "number" && !Number.isFinite(value)) {
|
|
149
|
+
throw new SpecializeError(at, `${value} has no PLC value.`);
|
|
150
|
+
}
|
|
151
|
+
return value;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/** Each entry `map` holds now, put back by the function returned. */
|
|
155
|
+
export const restoring = <K, V>(map: Map<K, V>): VoidFunction => {
|
|
156
|
+
const saved = [...map];
|
|
157
|
+
return () => {
|
|
158
|
+
map.clear();
|
|
159
|
+
for (const [key, value] of saved) {
|
|
160
|
+
map.set(key, value);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
/** Whether a value is stored state: a record or array the PLC keeps, or a copy of one. */
|
|
166
|
+
export const isStoredState = (value: Value): boolean =>
|
|
167
|
+
value.kind === "code" &&
|
|
168
|
+
(value.copyOf !== undefined ||
|
|
169
|
+
(value.place === true &&
|
|
170
|
+
value.declared !== undefined &&
|
|
171
|
+
Boolean(value.declared.flags & ts.TypeFlags.Object)));
|
|
172
|
+
|
|
173
|
+
/** Whether a literal's own fields or items hold objects setup built: instances, functions or references to them. */
|
|
174
|
+
export const holdsSetupObjects = (
|
|
175
|
+
value: Extract<Value, { kind: "virtual" }>,
|
|
176
|
+
): boolean =>
|
|
177
|
+
[...(value.fields?.values() ?? []), ...(value.items ?? [])].some(
|
|
178
|
+
(item) =>
|
|
179
|
+
item.kind === "static" ||
|
|
180
|
+
item.kind === "function" ||
|
|
181
|
+
item.kind === "choice",
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
/** An expression without the wrappers around it. */
|
|
185
|
+
export const unwrapped = (node: ts.Expression): ts.Expression =>
|
|
186
|
+
isWrapper(node) ? unwrapped(node.expression) : node;
|
|
187
|
+
|
|
188
|
+
/** Whether code names a place or a literal, which reads the same however often it is repeated. */
|
|
189
|
+
export const repeatable = (text: string | Mapped): boolean =>
|
|
190
|
+
/^[\w.[\]]+$/.test(String(text));
|
|
191
|
+
|
|
192
|
+
/** Scan code elaboration cannot compile, reported at the node it stopped at. */
|
|
193
|
+
export class SpecializeError extends Error {
|
|
194
|
+
constructor(
|
|
195
|
+
readonly node: ts.Node,
|
|
196
|
+
message: string,
|
|
197
|
+
) {
|
|
198
|
+
super(message);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { valueSymbol } from "../sdk";
|
|
3
|
+
import { unparenthesized } from "../types";
|
|
4
|
+
import { boundNames } from "./capture-protocol";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The properties scan code assigns outside constructors. Fields scan code writes are state; the
|
|
8
|
+
* rest keep what setup gave them.
|
|
9
|
+
*/
|
|
10
|
+
export class ScanWrites {
|
|
11
|
+
private readonly assigned = new Set<ts.Symbol>();
|
|
12
|
+
|
|
13
|
+
constructor(
|
|
14
|
+
private readonly checker: ts.TypeChecker,
|
|
15
|
+
sources: readonly ts.SourceFile[],
|
|
16
|
+
) {
|
|
17
|
+
for (const source of sources) {
|
|
18
|
+
this.collectAssignments(source, false);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Whether scan code assigns the property `symbol` declares. */
|
|
23
|
+
includes(symbol: ts.Symbol): boolean {
|
|
24
|
+
if (this.assigned.has(symbol)) {
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
// A union's property is written through any member's declaration of it.
|
|
28
|
+
return (symbol.declarations ?? []).some((declaration) => {
|
|
29
|
+
const own = this.ownSymbol(declaration);
|
|
30
|
+
return own !== undefined && this.assigned.has(own);
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
private collectAssignments(node: ts.Node, inConstructor: boolean): void {
|
|
35
|
+
// A closure made in a constructor runs later, as scan code, so its writes are state.
|
|
36
|
+
const constructing =
|
|
37
|
+
ts.isConstructorDeclaration(node) ||
|
|
38
|
+
(inConstructor &&
|
|
39
|
+
!ts.isArrowFunction(node) &&
|
|
40
|
+
!ts.isFunctionExpression(node));
|
|
41
|
+
if (!constructing) {
|
|
42
|
+
const write = (target: ts.Expression): void => {
|
|
43
|
+
const place = unparenthesized(target, { nonNull: true });
|
|
44
|
+
if (ts.isPropertyAccessExpression(place)) {
|
|
45
|
+
this.markAssigned(place.name);
|
|
46
|
+
} else if (ts.isElementAccessExpression(place)) {
|
|
47
|
+
const base = indexed(place.expression);
|
|
48
|
+
if (ts.isPropertyAccessExpression(base)) {
|
|
49
|
+
this.markAssigned(base.name);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
if (
|
|
54
|
+
ts.isBinaryExpression(node) &&
|
|
55
|
+
isAssignment(node.operatorToken.kind)
|
|
56
|
+
) {
|
|
57
|
+
assignedTargets(node.left).forEach(write);
|
|
58
|
+
}
|
|
59
|
+
if (isUpdate(node)) {
|
|
60
|
+
write(node.operand);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
ts.forEachChild(node, (child) =>
|
|
64
|
+
this.collectAssignments(child, constructing),
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private markAssigned(name: ts.MemberName): void {
|
|
69
|
+
const symbol = this.checker.getSymbolAtLocation(name);
|
|
70
|
+
if (!symbol) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
this.assigned.add(symbol);
|
|
74
|
+
// A property of a union is a synthetic symbol over each member's own property.
|
|
75
|
+
for (const declaration of symbol.declarations ?? []) {
|
|
76
|
+
const own = this.ownSymbol(declaration);
|
|
77
|
+
if (own) {
|
|
78
|
+
this.assigned.add(own);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The symbol a declaration declares. */
|
|
84
|
+
private ownSymbol(declaration: ts.Declaration): ts.Symbol | undefined {
|
|
85
|
+
const name = ts.getNameOfDeclaration(declaration);
|
|
86
|
+
return name && this.checker.getSymbolAtLocation(name);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** What an element access chain such as `a.b[1][2]` indexes: `a.b`. */
|
|
91
|
+
const indexed = (node: ts.Expression): ts.Expression =>
|
|
92
|
+
ts.isElementAccessExpression(node) ? indexed(node.expression) : node;
|
|
93
|
+
|
|
94
|
+
/** Whether an expression steps its operand by one: `x++`, `++x`, `x--` or `--x`. */
|
|
95
|
+
export const isUpdate = (
|
|
96
|
+
node: ts.Node,
|
|
97
|
+
): node is ts.PrefixUnaryExpression | ts.PostfixUnaryExpression =>
|
|
98
|
+
(ts.isPrefixUnaryExpression(node) || ts.isPostfixUnaryExpression(node)) &&
|
|
99
|
+
(node.operator === ts.SyntaxKind.PlusPlusToken ||
|
|
100
|
+
node.operator === ts.SyntaxKind.MinusMinusToken);
|
|
101
|
+
|
|
102
|
+
/** Whether an operator assigns its left operand: `=`, a compound assignment or `??=`. */
|
|
103
|
+
export const isAssignment = (kind: ts.SyntaxKind): boolean =>
|
|
104
|
+
kind >= ts.SyntaxKind.FirstAssignment && kind <= ts.SyntaxKind.LastAssignment;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The places an assignment's left side writes: the side itself, or each target a destructuring
|
|
108
|
+
* pattern names, as `[a, { b }, c = 1] = value` writes `a`, `b` and `c`.
|
|
109
|
+
*/
|
|
110
|
+
const assignedTargets = (left: ts.Expression): ts.Expression[] => {
|
|
111
|
+
const place = unparenthesized(left, { nonNull: true });
|
|
112
|
+
if (ts.isArrayLiteralExpression(place)) {
|
|
113
|
+
return place.elements.flatMap((element) =>
|
|
114
|
+
assignedTargets(
|
|
115
|
+
ts.isSpreadElement(element) ? element.expression : element,
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (ts.isObjectLiteralExpression(place)) {
|
|
120
|
+
return place.properties.flatMap((property) => {
|
|
121
|
+
if (ts.isPropertyAssignment(property)) {
|
|
122
|
+
return assignedTargets(property.initializer);
|
|
123
|
+
}
|
|
124
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
125
|
+
return [property.name];
|
|
126
|
+
}
|
|
127
|
+
if (ts.isSpreadAssignment(property)) {
|
|
128
|
+
return assignedTargets(property.expression);
|
|
129
|
+
}
|
|
130
|
+
return [];
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (
|
|
134
|
+
ts.isBinaryExpression(place) &&
|
|
135
|
+
place.operatorToken.kind === ts.SyntaxKind.EqualsToken
|
|
136
|
+
) {
|
|
137
|
+
return assignedTargets(place.left);
|
|
138
|
+
}
|
|
139
|
+
return [left];
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
/** Whether some node in `node` passes `test`, outside the functions it declares, which run apart from it. */
|
|
143
|
+
export const occursOutsideFunctions = (
|
|
144
|
+
node: ts.Node,
|
|
145
|
+
test: (node: ts.Node) => boolean,
|
|
146
|
+
): boolean =>
|
|
147
|
+
test(node) ||
|
|
148
|
+
(!ts.isFunctionLike(node) &&
|
|
149
|
+
(ts.forEachChild(node, (child) => occursOutsideFunctions(child, test)) ??
|
|
150
|
+
false));
|
|
151
|
+
|
|
152
|
+
/** Whether `node` lies within `container`'s source range. */
|
|
153
|
+
export const within = (node: ts.Node, container: ts.Node): boolean =>
|
|
154
|
+
node.pos >= container.pos && node.end <= container.end;
|
|
155
|
+
|
|
156
|
+
/** Whether the body assigns or steps any name the binding declares. */
|
|
157
|
+
export const assignsName = (
|
|
158
|
+
body: ts.Node,
|
|
159
|
+
binding: ts.BindingName,
|
|
160
|
+
checker: ts.TypeChecker,
|
|
161
|
+
): boolean => {
|
|
162
|
+
const declared = new Set(
|
|
163
|
+
boundNames(binding).flatMap((name) => {
|
|
164
|
+
const symbol = checker.getSymbolAtLocation(name);
|
|
165
|
+
return symbol ? [symbol] : [];
|
|
166
|
+
}),
|
|
167
|
+
);
|
|
168
|
+
return assignsSymbol(body, declared, checker);
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
/** Whether the body assigns or steps a name that refers to one of the symbols. */
|
|
172
|
+
export const assignsSymbol = (
|
|
173
|
+
body: ts.Node,
|
|
174
|
+
declared: ReadonlySet<ts.Symbol>,
|
|
175
|
+
checker: ts.TypeChecker,
|
|
176
|
+
): boolean => {
|
|
177
|
+
const target = (node: ts.Expression): boolean => {
|
|
178
|
+
const symbol = ts.isIdentifier(node)
|
|
179
|
+
? valueSymbol(checker, node)
|
|
180
|
+
: undefined;
|
|
181
|
+
return symbol !== undefined && declared.has(symbol);
|
|
182
|
+
};
|
|
183
|
+
const visit = (node: ts.Node): boolean =>
|
|
184
|
+
(ts.isBinaryExpression(node) &&
|
|
185
|
+
isAssignment(node.operatorToken.kind) &&
|
|
186
|
+
assignedTargets(node.left).some(target)) ||
|
|
187
|
+
(isUpdate(node) && target(node.operand)) ||
|
|
188
|
+
(ts.forEachChild(node, visit) ?? false);
|
|
189
|
+
return visit(body);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Whether the body changes what a local holds through it, by writing one of its fields or elements,
|
|
194
|
+
* or lends it to a call, which can.
|
|
195
|
+
*/
|
|
196
|
+
export const writesThrough = (
|
|
197
|
+
body: ts.Node,
|
|
198
|
+
symbol: ts.Symbol,
|
|
199
|
+
checker: ts.TypeChecker,
|
|
200
|
+
): boolean => {
|
|
201
|
+
const root = (node: ts.Expression): ts.Expression =>
|
|
202
|
+
ts.isPropertyAccessExpression(node) ||
|
|
203
|
+
ts.isElementAccessExpression(node) ||
|
|
204
|
+
ts.isParenthesizedExpression(node) ||
|
|
205
|
+
ts.isNonNullExpression(node)
|
|
206
|
+
? root(node.expression)
|
|
207
|
+
: node;
|
|
208
|
+
const named = (node: ts.Expression): boolean =>
|
|
209
|
+
ts.isIdentifier(node) && checker.getSymbolAtLocation(node) === symbol;
|
|
210
|
+
const throughIt = (target: ts.Expression): boolean =>
|
|
211
|
+
target !== root(target) && named(root(target));
|
|
212
|
+
const visit = (node: ts.Node): boolean =>
|
|
213
|
+
(ts.isBinaryExpression(node) &&
|
|
214
|
+
isAssignment(node.operatorToken.kind) &&
|
|
215
|
+
assignedTargets(node.left).some(throughIt)) ||
|
|
216
|
+
(isUpdate(node) && throughIt(node.operand)) ||
|
|
217
|
+
(ts.isCallExpression(node) && node.arguments.some(named)) ||
|
|
218
|
+
(ts.forEachChild(node, visit) ?? false);
|
|
219
|
+
return visit(body);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/** Where an alias is live: from its declaration to its last use, or its scope's end when a use can run again after a later write. */
|
|
223
|
+
interface AliasWindow {
|
|
224
|
+
scope: ts.Node;
|
|
225
|
+
/** What the alias was declared from: its initializer, or for `const { name } = value`, that property of the value. */
|
|
226
|
+
source: ts.Expression;
|
|
227
|
+
/** The property a destructured alias reads from `source`. */
|
|
228
|
+
property: string | undefined;
|
|
229
|
+
from: number;
|
|
230
|
+
until: number;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const aliasWindow = (
|
|
234
|
+
name: ts.Identifier,
|
|
235
|
+
checker: ts.TypeChecker,
|
|
236
|
+
): AliasWindow | undefined => {
|
|
237
|
+
const symbol = checker.getSymbolAtLocation(name);
|
|
238
|
+
const scope = enclosingScope(name);
|
|
239
|
+
const binding = ts.isBindingElement(name.parent) ? name.parent : undefined;
|
|
240
|
+
const declaration = binding
|
|
241
|
+
? ts.findAncestor(binding, ts.isVariableDeclaration)
|
|
242
|
+
: name.parent;
|
|
243
|
+
if (
|
|
244
|
+
!symbol ||
|
|
245
|
+
!scope ||
|
|
246
|
+
!declaration ||
|
|
247
|
+
!ts.isVariableDeclaration(declaration) ||
|
|
248
|
+
!declaration.initializer ||
|
|
249
|
+
// A nested pattern reads through more than one property.
|
|
250
|
+
(binding && !ts.isVariableDeclaration(binding.parent.parent))
|
|
251
|
+
) {
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
const uses: ts.Identifier[] = [];
|
|
255
|
+
const collect = (node: ts.Node): void => {
|
|
256
|
+
if (
|
|
257
|
+
ts.isIdentifier(node) &&
|
|
258
|
+
node !== name &&
|
|
259
|
+
valueSymbol(checker, node) === symbol
|
|
260
|
+
) {
|
|
261
|
+
uses.push(node);
|
|
262
|
+
}
|
|
263
|
+
ts.forEachChild(node, collect);
|
|
264
|
+
};
|
|
265
|
+
collect(scope);
|
|
266
|
+
const reentered = (use: ts.Node): boolean =>
|
|
267
|
+
ts.findAncestor(use.parent, (node) => {
|
|
268
|
+
if (node === scope) {
|
|
269
|
+
return "quit";
|
|
270
|
+
}
|
|
271
|
+
return (
|
|
272
|
+
(ts.isIterationStatement(node, false) || ts.isFunctionLike(node)) &&
|
|
273
|
+
!within(declaration, node)
|
|
274
|
+
);
|
|
275
|
+
}) !== undefined;
|
|
276
|
+
const from = declaration.end;
|
|
277
|
+
return {
|
|
278
|
+
scope,
|
|
279
|
+
source: declaration.initializer,
|
|
280
|
+
property: binding && (binding.propertyName ?? binding.name).getText(),
|
|
281
|
+
from,
|
|
282
|
+
until: uses.some(reentered)
|
|
283
|
+
? scope.end
|
|
284
|
+
: Math.max(from, ...uses.map((use) => use.getStart())),
|
|
285
|
+
};
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
/** Whether any node of `scope` that runs wholly inside the window matches. */
|
|
289
|
+
const runsBetween = (
|
|
290
|
+
{ scope, from, until }: AliasWindow,
|
|
291
|
+
matches: (node: ts.Node) => boolean,
|
|
292
|
+
): boolean => {
|
|
293
|
+
const visit = (node: ts.Node): boolean =>
|
|
294
|
+
(node.getStart() >= from && node.end <= until && matches(node)) ||
|
|
295
|
+
(ts.forEachChild(node, visit) ?? false);
|
|
296
|
+
return visit(scope);
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
/** The names along a place's path, root first, past `this`; element indexes are left out. */
|
|
300
|
+
const pathNames = (place: ts.Expression): string[] | undefined => {
|
|
301
|
+
const node = unparenthesized(place, { nonNull: true });
|
|
302
|
+
if (ts.isIdentifier(node)) {
|
|
303
|
+
return [node.text];
|
|
304
|
+
}
|
|
305
|
+
if (node.kind === ts.SyntaxKind.ThisKeyword) {
|
|
306
|
+
return [];
|
|
307
|
+
}
|
|
308
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
309
|
+
const base = pathNames(node.expression);
|
|
310
|
+
return base && [...base, node.name.text];
|
|
311
|
+
}
|
|
312
|
+
if (ts.isElementAccessExpression(node)) {
|
|
313
|
+
return pathNames(node.expression);
|
|
314
|
+
}
|
|
315
|
+
return undefined;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/** Each assignment's left side and each step's operand in the window. */
|
|
319
|
+
const writesIn = (window: AliasWindow): ts.Expression[] => {
|
|
320
|
+
const targets: ts.Expression[] = [];
|
|
321
|
+
runsBetween(window, (node) => {
|
|
322
|
+
if (isUpdate(node)) {
|
|
323
|
+
targets.push(node.operand);
|
|
324
|
+
} else if (
|
|
325
|
+
ts.isBinaryExpression(node) &&
|
|
326
|
+
isAssignment(node.operatorToken.kind)
|
|
327
|
+
) {
|
|
328
|
+
targets.push(node.left);
|
|
329
|
+
}
|
|
330
|
+
return false;
|
|
331
|
+
});
|
|
332
|
+
return targets;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Whether nothing can change what an alias's indexes read between its declaration and its last use: no
|
|
337
|
+
* assignment, step, call, await or yield finishes in between, and it is read in no loop or closure it was
|
|
338
|
+
* declared outside of, which could run after something did. Its element is then the same at each use.
|
|
339
|
+
*/
|
|
340
|
+
export const stableAlias = (
|
|
341
|
+
name: ts.Identifier,
|
|
342
|
+
checker: ts.TypeChecker,
|
|
343
|
+
): boolean => {
|
|
344
|
+
const window = aliasWindow(name, checker);
|
|
345
|
+
if (!window || window.property !== undefined) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
// What the alias's place reads, by name: a write to a place none of them name can't move it.
|
|
349
|
+
const read = new Set<string>();
|
|
350
|
+
const readNames = (node: ts.Node): void => {
|
|
351
|
+
if (ts.isIdentifier(node) || ts.isPrivateIdentifier(node)) {
|
|
352
|
+
read.add(node.text);
|
|
353
|
+
}
|
|
354
|
+
ts.forEachChild(node, readNames);
|
|
355
|
+
};
|
|
356
|
+
readNames(window.source);
|
|
357
|
+
// A destructuring assignment names no path, so it counts as writing what the alias reads: an alias
|
|
358
|
+
// that holds its indexes is never wrong.
|
|
359
|
+
const writesRead = (target: ts.Expression): boolean => {
|
|
360
|
+
const names = pathNames(target);
|
|
361
|
+
return names === undefined || names.some((written) => read.has(written));
|
|
362
|
+
};
|
|
363
|
+
return (
|
|
364
|
+
!writesIn(window).some(writesRead) &&
|
|
365
|
+
!runsBetween(
|
|
366
|
+
window,
|
|
367
|
+
(node) =>
|
|
368
|
+
ts.isCallExpression(node) ||
|
|
369
|
+
ts.isNewExpression(node) ||
|
|
370
|
+
ts.isAwaitExpression(node) ||
|
|
371
|
+
ts.isYieldExpression(node) ||
|
|
372
|
+
ts.isTaggedTemplateExpression(node) ||
|
|
373
|
+
ts.isDeleteExpression(node),
|
|
374
|
+
)
|
|
375
|
+
);
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Whether the place an alias names is assigned anew while the alias is still read, as
|
|
380
|
+
* `const { pending } = this; this.pending = undefined; use(pending)` does. TypeScript's alias keeps the
|
|
381
|
+
* object it named; the PLC's would read the new value, so it needs a snapshot. A write into the place's
|
|
382
|
+
* fields, such as `this.pending.travel += 1`, is one the alias sees in both.
|
|
383
|
+
*/
|
|
384
|
+
export const replacedWhileAliased = (
|
|
385
|
+
name: ts.Identifier,
|
|
386
|
+
checker: ts.TypeChecker,
|
|
387
|
+
): boolean => {
|
|
388
|
+
const window = aliasWindow(name, checker);
|
|
389
|
+
const base = window && pathNames(window.source);
|
|
390
|
+
if (!window || !base) {
|
|
391
|
+
return false;
|
|
392
|
+
}
|
|
393
|
+
const path =
|
|
394
|
+
window.property === undefined ? base : [...base, window.property];
|
|
395
|
+
// Writing the place, or anything on its path, replaces what it holds.
|
|
396
|
+
return writesIn(window)
|
|
397
|
+
.flatMap(assignedTargets)
|
|
398
|
+
.some((target) => {
|
|
399
|
+
const written = pathNames(target);
|
|
400
|
+
const last = written?.at(-1);
|
|
401
|
+
return last !== undefined && path.includes(last);
|
|
402
|
+
});
|
|
403
|
+
};
|
|
404
|
+
|
|
405
|
+
/** The block, clause or source a declaration's name is scoped to. */
|
|
406
|
+
const enclosingScope = (node: ts.Node): ts.Node | undefined =>
|
|
407
|
+
ts.findAncestor(
|
|
408
|
+
node.parent,
|
|
409
|
+
(parent) =>
|
|
410
|
+
ts.isBlock(parent) ||
|
|
411
|
+
ts.isSourceFile(parent) ||
|
|
412
|
+
ts.isCaseClause(parent) ||
|
|
413
|
+
ts.isDefaultClause(parent) ||
|
|
414
|
+
ts.isFunctionLike(parent),
|
|
415
|
+
);
|