@tsplc/compiler 0.5.4 → 0.5.6
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/dist/bun-runtime.js +14 -2
- package/dist/bun-runtime.js.map +4 -4
- package/dist/codesys.js +46 -2
- package/dist/codesys.js.map +3 -3
- package/dist/collections.js +395 -0
- package/dist/collections.js.map +10 -0
- package/dist/elaborate-capture.js +64 -5
- package/dist/elaborate-capture.js.map +4 -4
- package/dist/index.js +5345 -1074
- package/dist/index.js.map +32 -25
- package/dist/ir.js +122 -1
- package/dist/ir.js.map +6 -6
- package/package.json +14 -9
- package/src/codesys/numeric.ts +61 -0
- package/src/frontend/control-flow.ts +752 -0
- package/src/frontend/coroutines.ts +171 -7
- package/src/frontend/elaborate/array-searches.ts +216 -0
- package/src/frontend/elaborate/assignments.ts +352 -0
- package/src/frontend/elaborate/calls.ts +488 -49
- package/src/frontend/elaborate/capture-protocol.ts +19 -0
- package/src/frontend/elaborate/capture.ts +82 -3
- package/src/frontend/elaborate/dialect.ts +118 -4
- package/src/frontend/elaborate/expressions.ts +927 -80
- package/src/frontend/elaborate/filtered-arrays.ts +607 -0
- package/src/frontend/elaborate/frame.ts +44 -1
- package/src/frontend/elaborate/generics.ts +137 -0
- package/src/frontend/elaborate/graph.ts +91 -0
- package/src/frontend/elaborate/imports.ts +14 -15
- package/src/frontend/elaborate/index.ts +30 -5
- package/src/frontend/elaborate/initializers.ts +15 -1
- package/src/frontend/elaborate/iterators.ts +1677 -0
- package/src/frontend/elaborate/layouts.ts +41 -19
- package/src/frontend/elaborate/model.ts +64 -8
- package/src/frontend/elaborate/references.ts +749 -0
- package/src/frontend/elaborate/specialize.ts +459 -29
- package/src/frontend/elaborate/statements.ts +552 -133
- package/src/frontend/elaborate/storage.ts +151 -10
- package/src/frontend/elaborate/values.ts +72 -0
- package/src/frontend/elaborate/writes.ts +302 -1
- package/src/frontend/intrinsics.ts +32 -0
- package/src/frontend/module-graph.ts +2 -1
- package/src/frontend/positions.ts +12 -5
- package/src/frontend/sdk.ts +7 -1
- package/src/frontend/typescript.ts +5 -1
- package/src/ir/core-model.ts +2 -6
- package/src/ir/fold.ts +30 -1
- package/src/ir/index.ts +5 -0
- package/src/ir/model.ts +53 -2
- package/src/ir/scalars.ts +126 -0
- package/src/runtime/.runtime-VaUSOu/server/router.ts +17 -0
- package/src/runtime/.runtime-VaUSOu/src/cell.ts +45 -0
- package/src/runtime/.runtime-VaUSOu/src/controller.ts +58 -0
- package/src/runtime/.runtime-VaUSOu/src/lane.ts +6 -0
- package/src/runtime/.runtime-VaUSOu/src/main.ts +11 -0
- package/src/runtime/.runtime-VaUSOu/src/services.ts +8 -0
- package/src/runtime/.runtime-VaUSOu/tsconfig.json +13 -0
- package/src/runtime/.runtime-VaUSOu/tsplc.config.ts +31 -0
- package/src/runtime/.runtime-ybIxYh/server/router.ts +17 -0
- package/src/runtime/.runtime-ybIxYh/src/cell.ts +45 -0
- package/src/runtime/.runtime-ybIxYh/src/controller.ts +58 -0
- package/src/runtime/.runtime-ybIxYh/src/lane.ts +6 -0
- package/src/runtime/.runtime-ybIxYh/src/main.ts +11 -0
- package/src/runtime/.runtime-ybIxYh/src/services.ts +8 -0
- package/src/runtime/.runtime-ybIxYh/tsconfig.json +13 -0
- package/src/runtime/.runtime-ybIxYh/tsplc.config.ts +31 -0
- package/stdlib/collections.ts +514 -0
- package/types/index.d.ts +18 -0
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
// biome-ignore-all lint/style/noNonNullAssertion: Retain existing non-null assertions during the tooling migration; changing their contracts requires a separate review.
|
|
2
2
|
import ts from "typescript";
|
|
3
|
-
import { from, joined, type Mapped, mapped } from "../positions";
|
|
3
|
+
import { from, joined, type Mapped, mapped, type Part } from "../positions";
|
|
4
4
|
import { unaliased } from "../sdk";
|
|
5
|
+
import { AssignmentSpecializer, containsWrite } from "./assignments";
|
|
5
6
|
import { CallSpecializer } from "./calls";
|
|
7
|
+
import type { CapturedValue } from "./capture-protocol";
|
|
6
8
|
import type { DialectText } from "./dialect";
|
|
9
|
+
import { FilteredArrays } from "./filtered-arrays";
|
|
7
10
|
import type { Frame } from "./frame";
|
|
8
11
|
import type { EntryImports } from "./imports";
|
|
9
12
|
import type { Initializers } from "./initializers";
|
|
10
|
-
import {
|
|
13
|
+
import { Iterators } from "./iterators";
|
|
14
|
+
import { isAbsent, type Layouts, recordTypes } from "./layouts";
|
|
11
15
|
import type { Model, ModelNode } from "./model";
|
|
16
|
+
import { References, referenceType } from "./references";
|
|
12
17
|
import type { Storage } from "./storage";
|
|
13
18
|
import {
|
|
14
19
|
constant,
|
|
@@ -18,7 +23,13 @@ import {
|
|
|
18
23
|
SpecializeError,
|
|
19
24
|
type Value,
|
|
20
25
|
} from "./values";
|
|
21
|
-
import {
|
|
26
|
+
import {
|
|
27
|
+
isAssignment,
|
|
28
|
+
isUpdate,
|
|
29
|
+
replacedWhileAliased,
|
|
30
|
+
stableAlias,
|
|
31
|
+
writesThrough,
|
|
32
|
+
} from "./writes";
|
|
22
33
|
|
|
23
34
|
/** What every part of scan-code specialization shares. */
|
|
24
35
|
export interface SpecializeContext {
|
|
@@ -40,6 +51,15 @@ export interface RoutineCall {
|
|
|
40
51
|
name: string;
|
|
41
52
|
args: (string | Mapped)[];
|
|
42
53
|
returns: ts.Type | undefined;
|
|
54
|
+
/** The IEC width the routine's declaration gives its result, as `(): UINT` does. */
|
|
55
|
+
iec?: string;
|
|
56
|
+
/** An array result's layout, at the length the routine returns. */
|
|
57
|
+
layout?: string;
|
|
58
|
+
length?: number;
|
|
59
|
+
/** Whether the routine returns stored state, which its caller gets a copy of. */
|
|
60
|
+
returnsStored?: boolean;
|
|
61
|
+
/** For a result that is a reference: the objects it can be, and whether it can be undefined. */
|
|
62
|
+
referents?: { options: readonly ModelNode[]; optional: boolean };
|
|
43
63
|
}
|
|
44
64
|
|
|
45
65
|
/** Specializes a called function for its receiver and known arguments. */
|
|
@@ -55,6 +75,10 @@ export interface RoutineTable {
|
|
|
55
75
|
/** Evaluates expressions against the setup graph, and writes what remains as dialect text. */
|
|
56
76
|
export class ExpressionSpecializer {
|
|
57
77
|
private readonly calls: CallSpecializer;
|
|
78
|
+
readonly assignments: AssignmentSpecializer;
|
|
79
|
+
readonly references: References;
|
|
80
|
+
readonly filteredArrays: FilteredArrays;
|
|
81
|
+
readonly iterators: Iterators;
|
|
58
82
|
private readonly services = new Map<string, ModelNode>();
|
|
59
83
|
private readonly tables = new Map<number, string>();
|
|
60
84
|
readonly tableDeclarations: Mapped[] = [];
|
|
@@ -64,6 +88,7 @@ export class ExpressionSpecializer {
|
|
|
64
88
|
private readonly layouts: Layouts;
|
|
65
89
|
private readonly storage: Storage;
|
|
66
90
|
private readonly initializers: Initializers;
|
|
91
|
+
private readonly imports: EntryImports;
|
|
67
92
|
|
|
68
93
|
constructor(
|
|
69
94
|
context: SpecializeContext,
|
|
@@ -71,11 +96,16 @@ export class ExpressionSpecializer {
|
|
|
71
96
|
) {
|
|
72
97
|
this.model = context.model;
|
|
73
98
|
this.checker = context.checker;
|
|
99
|
+
this.imports = context.imports;
|
|
74
100
|
this.dialect = context.dialect;
|
|
75
101
|
this.layouts = context.layouts;
|
|
76
102
|
this.storage = context.storage;
|
|
77
103
|
this.initializers = context.initializers;
|
|
78
104
|
this.calls = new CallSpecializer(context, this);
|
|
105
|
+
this.assignments = new AssignmentSpecializer(context, this);
|
|
106
|
+
this.references = new References(context, this);
|
|
107
|
+
this.filteredArrays = new FilteredArrays(context, this);
|
|
108
|
+
this.iterators = new Iterators(context, this);
|
|
79
109
|
}
|
|
80
110
|
|
|
81
111
|
expression(node: ts.Expression, frame: Frame, expected?: ts.Type): Value {
|
|
@@ -115,6 +145,10 @@ export class ExpressionSpecializer {
|
|
|
115
145
|
if (node.kind === ts.SyntaxKind.FalseKeyword) {
|
|
116
146
|
return constant(false);
|
|
117
147
|
}
|
|
148
|
+
// null is the PLC's one empty value, as undefined is.
|
|
149
|
+
if (node.kind === ts.SyntaxKind.NullKeyword) {
|
|
150
|
+
return constant(undefined);
|
|
151
|
+
}
|
|
118
152
|
if (ts.isBigIntLiteral(node)) {
|
|
119
153
|
return constant(BigInt(node.text.slice(0, -1)));
|
|
120
154
|
}
|
|
@@ -134,17 +168,19 @@ export class ExpressionSpecializer {
|
|
|
134
168
|
if (member && ts.isEnumMember(member)) {
|
|
135
169
|
return constant(this.checker.getConstantValue(member));
|
|
136
170
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
node.questionDotToken &&
|
|
140
|
-
object.kind === "const" &&
|
|
141
|
-
object.value === undefined
|
|
142
|
-
) {
|
|
143
|
-
return constant(undefined);
|
|
171
|
+
if (node.expression.kind === ts.SyntaxKind.SuperKeyword) {
|
|
172
|
+
return this.superMember(node, frame);
|
|
144
173
|
}
|
|
145
|
-
|
|
146
|
-
|
|
174
|
+
if (this.isMathConstant(node)) {
|
|
175
|
+
return constant(mathConstants.get(node.name.text));
|
|
176
|
+
}
|
|
177
|
+
if (node.name.text === "length" && this.calls.isFilter(node.expression)) {
|
|
178
|
+
return this.calls.count(node.expression, frame);
|
|
179
|
+
}
|
|
180
|
+
return this.property(
|
|
181
|
+
this.expression(node.expression, frame),
|
|
147
182
|
node,
|
|
183
|
+
frame,
|
|
148
184
|
);
|
|
149
185
|
}
|
|
150
186
|
if (ts.isElementAccessExpression(node)) {
|
|
@@ -161,8 +197,15 @@ export class ExpressionSpecializer {
|
|
|
161
197
|
kind: "code",
|
|
162
198
|
text: mapped`await ${this.dialect.code(value, undefined)}`,
|
|
163
199
|
declared: this.checker.getTypeAtLocation(node),
|
|
200
|
+
awaited: true,
|
|
164
201
|
};
|
|
165
202
|
}
|
|
203
|
+
if (isUpdate(node)) {
|
|
204
|
+
return this.assignments.step(node, frame, { used: true });
|
|
205
|
+
}
|
|
206
|
+
if (ts.isTypeOfExpression(node)) {
|
|
207
|
+
return this.typeOf(this.expression(node.expression, frame));
|
|
208
|
+
}
|
|
166
209
|
if (ts.isPrefixUnaryExpression(node)) {
|
|
167
210
|
return this.unary(node, frame);
|
|
168
211
|
}
|
|
@@ -178,13 +221,50 @@ export class ExpressionSpecializer {
|
|
|
178
221
|
expected,
|
|
179
222
|
);
|
|
180
223
|
}
|
|
181
|
-
const
|
|
224
|
+
const own = this.checker.getTypeAtLocation(node);
|
|
225
|
+
if (referenceType(own)) {
|
|
226
|
+
return this.references.branch(
|
|
227
|
+
this.dialect.condition(condition, node.condition),
|
|
228
|
+
[
|
|
229
|
+
() => this.expression(node.whenTrue, frame),
|
|
230
|
+
() => this.expression(node.whenFalse, frame),
|
|
231
|
+
],
|
|
232
|
+
node,
|
|
233
|
+
frame,
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
// A choice between defined values stored into an optional is wrapped where it is stored.
|
|
237
|
+
const declared =
|
|
238
|
+
expected &&
|
|
239
|
+
!(
|
|
240
|
+
this.layouts.isOptionalRecord(expected) &&
|
|
241
|
+
!this.layouts.isOptionalRecord(own)
|
|
242
|
+
)
|
|
243
|
+
? expected
|
|
244
|
+
: own;
|
|
245
|
+
if (
|
|
246
|
+
this.isAggregate(declared) ||
|
|
247
|
+
this.runsWhenNeeded(node.whenTrue, frame) ||
|
|
248
|
+
this.runsWhenNeeded(node.whenFalse, frame)
|
|
249
|
+
) {
|
|
250
|
+
return this.branched(
|
|
251
|
+
declared,
|
|
252
|
+
this.dialect.condition(condition, node.condition),
|
|
253
|
+
[node.whenTrue, node.whenFalse],
|
|
254
|
+
frame,
|
|
255
|
+
);
|
|
256
|
+
}
|
|
182
257
|
const whenTrue = this.expression(node.whenTrue, frame, declared);
|
|
183
258
|
const whenFalse = this.expression(node.whenFalse, frame, declared);
|
|
184
259
|
return {
|
|
185
260
|
kind: "code",
|
|
186
|
-
|
|
261
|
+
// Each branch is stored into the choice's type, so a defined value in an optional is wrapped.
|
|
262
|
+
text: mapped`(${this.dialect.condition(condition, node.condition)} ? ${this.dialect.assignmentText(whenTrue, declared)} : ${this.dialect.assignmentText(whenFalse, declared)})`,
|
|
187
263
|
declared,
|
|
264
|
+
copyOf: [whenTrue, whenFalse].find(
|
|
265
|
+
(branch): branch is Value & { kind: "code" } =>
|
|
266
|
+
branch.kind === "code" && branch.copyOf !== undefined,
|
|
267
|
+
)?.copyOf,
|
|
188
268
|
};
|
|
189
269
|
}
|
|
190
270
|
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
|
|
@@ -225,6 +305,33 @@ export class ExpressionSpecializer {
|
|
|
225
305
|
);
|
|
226
306
|
}
|
|
227
307
|
|
|
308
|
+
/** `object.name` once the object is known: its member, through `?.` and what TypeScript narrowed. */
|
|
309
|
+
property(
|
|
310
|
+
object: Value,
|
|
311
|
+
node: ts.PropertyAccessExpression,
|
|
312
|
+
frame: Frame,
|
|
313
|
+
): Value {
|
|
314
|
+
if (
|
|
315
|
+
node.questionDotToken &&
|
|
316
|
+
object.kind === "const" &&
|
|
317
|
+
object.value === undefined
|
|
318
|
+
) {
|
|
319
|
+
return constant(undefined);
|
|
320
|
+
}
|
|
321
|
+
if (
|
|
322
|
+
node.questionDotToken &&
|
|
323
|
+
object.kind === "code" &&
|
|
324
|
+
object.declared &&
|
|
325
|
+
this.layouts.isOptionalRecord(object.declared)
|
|
326
|
+
) {
|
|
327
|
+
return this.optionalMember(object, node, frame);
|
|
328
|
+
}
|
|
329
|
+
return this.narrowed(
|
|
330
|
+
this.member(object, node.name.text, node, frame),
|
|
331
|
+
node,
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
228
335
|
private identifier(node: ts.Identifier, frame: Frame): Value {
|
|
229
336
|
if (node.text === "undefined") {
|
|
230
337
|
return constant(undefined);
|
|
@@ -274,6 +381,15 @@ export class ExpressionSpecializer {
|
|
|
274
381
|
}
|
|
275
382
|
}
|
|
276
383
|
}
|
|
384
|
+
// A module constant named by a destructuring declaration is a root of its own.
|
|
385
|
+
if (ts.isBindingElement(declaration)) {
|
|
386
|
+
const root = this.model.roots.find(
|
|
387
|
+
(item) => item.declaration === declaration,
|
|
388
|
+
);
|
|
389
|
+
if (root) {
|
|
390
|
+
return this.storage.rootValue(root);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
277
393
|
if (ts.isEnumMember(declaration)) {
|
|
278
394
|
return constant(this.checker.getConstantValue(declaration));
|
|
279
395
|
}
|
|
@@ -284,6 +400,15 @@ export class ExpressionSpecializer {
|
|
|
284
400
|
}
|
|
285
401
|
|
|
286
402
|
member(object: Value, name: string, at: ts.Node, frame: Frame): Value {
|
|
403
|
+
if (object.kind === "counted") {
|
|
404
|
+
return this.iterators.member(object, name, at);
|
|
405
|
+
}
|
|
406
|
+
if (object.kind === "choice") {
|
|
407
|
+
return this.references.member(object, name, at, frame);
|
|
408
|
+
}
|
|
409
|
+
if (object.kind === "masked") {
|
|
410
|
+
return this.filteredArrays.member(object, name, at, frame);
|
|
411
|
+
}
|
|
287
412
|
if (object.kind === "static") {
|
|
288
413
|
const node = object.node;
|
|
289
414
|
const captured = node.captured;
|
|
@@ -296,13 +421,25 @@ export class ExpressionSpecializer {
|
|
|
296
421
|
};
|
|
297
422
|
}
|
|
298
423
|
if (captured.kind === "instance") {
|
|
299
|
-
const
|
|
300
|
-
const accessor =
|
|
424
|
+
const members = this.model.members(this.model.classOf(node));
|
|
425
|
+
const accessor = members.find(
|
|
301
426
|
(member): member is ts.GetAccessorDeclaration =>
|
|
302
427
|
ts.isGetAccessorDeclaration(member) &&
|
|
303
428
|
member.name.getText() === name,
|
|
304
429
|
);
|
|
305
430
|
if (accessor) {
|
|
431
|
+
// A getter that only returns an expression reads it in place, as a field is read, so one
|
|
432
|
+
// naming an object setup built is that object rather than a routine's copy of it.
|
|
433
|
+
const returned = onlyReturned(accessor);
|
|
434
|
+
if (returned) {
|
|
435
|
+
return this.expression(returned, {
|
|
436
|
+
...frame,
|
|
437
|
+
env: new Map(),
|
|
438
|
+
self: node,
|
|
439
|
+
returns: undefined,
|
|
440
|
+
generator: undefined,
|
|
441
|
+
});
|
|
442
|
+
}
|
|
306
443
|
return this.invoke(
|
|
307
444
|
{ kind: "function", declaration: accessor, self: node },
|
|
308
445
|
[],
|
|
@@ -310,7 +447,7 @@ export class ExpressionSpecializer {
|
|
|
310
447
|
frame,
|
|
311
448
|
);
|
|
312
449
|
}
|
|
313
|
-
const method =
|
|
450
|
+
const method = members.find(
|
|
314
451
|
(member): member is ts.MethodDeclaration =>
|
|
315
452
|
ts.isMethodDeclaration(member) && member.name.getText() === name,
|
|
316
453
|
);
|
|
@@ -329,7 +466,11 @@ export class ExpressionSpecializer {
|
|
|
329
466
|
}
|
|
330
467
|
throw new SpecializeError(at, `${name} is not set during setup.`);
|
|
331
468
|
}
|
|
332
|
-
return this.
|
|
469
|
+
return this.references.field(
|
|
470
|
+
this.storage.fromCaptured(field, node, { kind: "field", name }),
|
|
471
|
+
node,
|
|
472
|
+
name,
|
|
473
|
+
);
|
|
333
474
|
}
|
|
334
475
|
throw new SpecializeError(at, `${name} cannot be read from this value.`);
|
|
335
476
|
}
|
|
@@ -352,11 +493,17 @@ export class ExpressionSpecializer {
|
|
|
352
493
|
this.checker.getNonNullableType(object.declared),
|
|
353
494
|
name,
|
|
354
495
|
);
|
|
496
|
+
const pinnedObject = object.pinned;
|
|
355
497
|
return {
|
|
356
498
|
kind: "code",
|
|
357
499
|
text: mapped`${object.text}.${name}`,
|
|
358
500
|
declared,
|
|
359
501
|
place: object.place,
|
|
502
|
+
copyOf: object.copyOf,
|
|
503
|
+
...(pinnedObject && {
|
|
504
|
+
pinned: (pinFrame: Frame) =>
|
|
505
|
+
this.member(pinnedObject(pinFrame), name, at, pinFrame),
|
|
506
|
+
}),
|
|
360
507
|
iec: this.layouts.iecName(property?.valueDeclaration),
|
|
361
508
|
elementIec: this.layouts.elementIec(property?.valueDeclaration),
|
|
362
509
|
layout:
|
|
@@ -381,13 +528,10 @@ export class ExpressionSpecializer {
|
|
|
381
528
|
return value;
|
|
382
529
|
}
|
|
383
530
|
const type = this.checker.getTypeAtLocation(at);
|
|
384
|
-
if (
|
|
385
|
-
type.isUnion() &&
|
|
386
|
-
type.types.some((part) => part.flags & ts.TypeFlags.Undefined)
|
|
387
|
-
) {
|
|
531
|
+
if (type.isUnion() && type.types.some(isAbsent)) {
|
|
388
532
|
return value;
|
|
389
533
|
}
|
|
390
|
-
if (type
|
|
534
|
+
if (isAbsent(type)) {
|
|
391
535
|
return value;
|
|
392
536
|
}
|
|
393
537
|
return {
|
|
@@ -395,6 +539,7 @@ export class ExpressionSpecializer {
|
|
|
395
539
|
text: mapped`${value.text}.value`,
|
|
396
540
|
declared: this.checker.getNonNullableType(value.declared),
|
|
397
541
|
place: value.place,
|
|
542
|
+
copyOf: value.copyOf,
|
|
398
543
|
};
|
|
399
544
|
}
|
|
400
545
|
|
|
@@ -434,12 +579,16 @@ export class ExpressionSpecializer {
|
|
|
434
579
|
return node;
|
|
435
580
|
}
|
|
436
581
|
|
|
437
|
-
|
|
438
|
-
object
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
582
|
+
element(object: Value, index: Value, at: ts.Node, frame: Frame): Value {
|
|
583
|
+
if (object.kind === "counted") {
|
|
584
|
+
return this.iterators.element(object, index, at, frame);
|
|
585
|
+
}
|
|
586
|
+
if (object.kind === "choice") {
|
|
587
|
+
return this.references.element(object, index, at, frame);
|
|
588
|
+
}
|
|
589
|
+
if (object.kind === "masked") {
|
|
590
|
+
return this.filteredArrays.element(object, index, at, frame);
|
|
591
|
+
}
|
|
443
592
|
if (index.kind === "const" && typeof index.value === "number") {
|
|
444
593
|
if (object.kind === "virtual" && object.items) {
|
|
445
594
|
const item = object.items[index.value];
|
|
@@ -488,6 +637,10 @@ export class ExpressionSpecializer {
|
|
|
488
637
|
text: mapped`${text}[${this.dialect.code(index, node.type && recordTypes(node.type)?.key)}]`,
|
|
489
638
|
declared,
|
|
490
639
|
place: true,
|
|
640
|
+
...(index.kind === "code" && {
|
|
641
|
+
pinned: (pinFrame: Frame) =>
|
|
642
|
+
this.element(object, this.held(index, pinFrame), at, pinFrame),
|
|
643
|
+
}),
|
|
491
644
|
};
|
|
492
645
|
}
|
|
493
646
|
if (object.kind === "code") {
|
|
@@ -503,9 +656,29 @@ export class ExpressionSpecializer {
|
|
|
503
656
|
declared,
|
|
504
657
|
place: object.place,
|
|
505
658
|
iec: object.elementIec,
|
|
659
|
+
copyOf: object.copyOf,
|
|
660
|
+
...((index.kind === "code" || object.pinned) && {
|
|
661
|
+
pinned: (pinFrame: Frame) =>
|
|
662
|
+
this.element(
|
|
663
|
+
object.pinned ? object.pinned(pinFrame) : object,
|
|
664
|
+
this.held(index, pinFrame),
|
|
665
|
+
at,
|
|
666
|
+
pinFrame,
|
|
667
|
+
),
|
|
668
|
+
}),
|
|
506
669
|
};
|
|
507
670
|
}
|
|
508
671
|
if (object.kind === "virtual" && object.items) {
|
|
672
|
+
const chosen = this.filteredArrays.indexed(
|
|
673
|
+
object.items,
|
|
674
|
+
index,
|
|
675
|
+
{ fromEnd: false },
|
|
676
|
+
at,
|
|
677
|
+
frame,
|
|
678
|
+
);
|
|
679
|
+
if (chosen) {
|
|
680
|
+
return chosen;
|
|
681
|
+
}
|
|
509
682
|
const text = this.materialize(object, at, frame);
|
|
510
683
|
return {
|
|
511
684
|
kind: "code",
|
|
@@ -573,6 +746,10 @@ export class ExpressionSpecializer {
|
|
|
573
746
|
property.name.text,
|
|
574
747
|
local ?? this.expression(property.name, frame),
|
|
575
748
|
);
|
|
749
|
+
} else if (ts.isSpreadAssignment(property)) {
|
|
750
|
+
for (const [name, value] of this.spreadFields(property, frame)) {
|
|
751
|
+
fields.set(name, value);
|
|
752
|
+
}
|
|
576
753
|
} else {
|
|
577
754
|
throw new SpecializeError(property, "Write each field of a record.");
|
|
578
755
|
}
|
|
@@ -635,9 +812,59 @@ export class ExpressionSpecializer {
|
|
|
635
812
|
if (left.kind === "static" || left.kind === "function") {
|
|
636
813
|
return and ? this.expression(node.right, frame, expected) : left;
|
|
637
814
|
}
|
|
815
|
+
if (left.kind === "choice" && !and) {
|
|
816
|
+
return this.references.fallback(
|
|
817
|
+
left,
|
|
818
|
+
() => this.expression(node.right, frame, expected),
|
|
819
|
+
node,
|
|
820
|
+
frame,
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
if (
|
|
824
|
+
left.kind === "choice" &&
|
|
825
|
+
referenceType(this.checker.getTypeAtLocation(node))
|
|
826
|
+
) {
|
|
827
|
+
return this.references.branch(
|
|
828
|
+
this.dialect.condition(left, node.left),
|
|
829
|
+
[
|
|
830
|
+
() => this.expression(node.right, frame, expected),
|
|
831
|
+
() => constant(undefined),
|
|
832
|
+
],
|
|
833
|
+
node,
|
|
834
|
+
frame,
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
if (
|
|
838
|
+
!and &&
|
|
839
|
+
left.kind === "code" &&
|
|
840
|
+
this.isNumber(node.left) &&
|
|
841
|
+
!this.runsWhenNeeded(node.right, frame)
|
|
842
|
+
) {
|
|
843
|
+
return this.firstNonZero(left, node, frame);
|
|
844
|
+
}
|
|
845
|
+
if (this.runsWhenNeeded(node.right, frame)) {
|
|
846
|
+
// The right operand's writes happen only when it is evaluated.
|
|
847
|
+
const result = frame.emitter.temporary();
|
|
848
|
+
frame.emitter.line(
|
|
849
|
+
mapped`let ${result}: boolean = ${this.dialect.condition(left, node.left)};`,
|
|
850
|
+
);
|
|
851
|
+
frame.emitter.open(and ? `if (${result}) {` : `if (!${result}) {`);
|
|
852
|
+
const right = this.expression(node.right, frame, expected);
|
|
853
|
+
frame.emitter.line(
|
|
854
|
+
mapped`${result} = ${this.dialect.condition(right, node.right)};`,
|
|
855
|
+
);
|
|
856
|
+
frame.emitter.close();
|
|
857
|
+
return {
|
|
858
|
+
kind: "code",
|
|
859
|
+
text: result,
|
|
860
|
+
declared: this.checker.getBooleanType(),
|
|
861
|
+
place: true,
|
|
862
|
+
};
|
|
863
|
+
}
|
|
638
864
|
const right = this.expression(node.right, frame, expected);
|
|
639
|
-
|
|
640
|
-
|
|
865
|
+
// A right operand known now decides by its truthiness, as a condition reads it.
|
|
866
|
+
if (right.kind === "const") {
|
|
867
|
+
if (Boolean(right.value) === and) {
|
|
641
868
|
return {
|
|
642
869
|
kind: "code",
|
|
643
870
|
text: this.dialect.condition(left, node.left),
|
|
@@ -662,7 +889,50 @@ export class ExpressionSpecializer {
|
|
|
662
889
|
if (left.kind === "static") {
|
|
663
890
|
return left;
|
|
664
891
|
}
|
|
892
|
+
if (left.kind === "choice") {
|
|
893
|
+
return this.references.fallback(
|
|
894
|
+
left,
|
|
895
|
+
() => this.expression(node.right, frame, expected),
|
|
896
|
+
node,
|
|
897
|
+
frame,
|
|
898
|
+
);
|
|
899
|
+
}
|
|
665
900
|
const declared = left.kind === "code" ? left.declared : undefined;
|
|
901
|
+
// A value that can't be undefined is its own result.
|
|
902
|
+
if (declared && this.checker.getNonNullableType(declared) === declared) {
|
|
903
|
+
return left;
|
|
904
|
+
}
|
|
905
|
+
// A record or array result is chosen by statements: PLC code loads one only into storage.
|
|
906
|
+
if (
|
|
907
|
+
declared &&
|
|
908
|
+
(this.runsWhenNeeded(node.right, frame) ||
|
|
909
|
+
this.isAggregate(this.checker.getNonNullableType(declared)))
|
|
910
|
+
) {
|
|
911
|
+
const defined = this.checker.getNonNullableType(declared);
|
|
912
|
+
// Read once to test and once to give, so a call on the left runs once, into a const.
|
|
913
|
+
const held =
|
|
914
|
+
left.kind === "code" && this.isAggregate(declared)
|
|
915
|
+
? this.temporaryOf(left, frame)
|
|
916
|
+
: this.held(left, frame);
|
|
917
|
+
return this.branched(
|
|
918
|
+
defined,
|
|
919
|
+
this.dialect.isUndefinedText(
|
|
920
|
+
this.dialect.code(held, declared),
|
|
921
|
+
declared,
|
|
922
|
+
),
|
|
923
|
+
[
|
|
924
|
+
node.right,
|
|
925
|
+
() => ({
|
|
926
|
+
kind: "code",
|
|
927
|
+
text: mapped`${this.dialect.code(held, undefined)}${this.optionalValueSuffix(declared)}`,
|
|
928
|
+
declared: defined,
|
|
929
|
+
}),
|
|
930
|
+
],
|
|
931
|
+
frame,
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
// The left operand is read twice, so a call in it runs once, into a const.
|
|
935
|
+
const once = left.kind === "code" ? this.temporaryOf(left, frame) : left;
|
|
666
936
|
const right = this.expression(
|
|
667
937
|
node.right,
|
|
668
938
|
frame,
|
|
@@ -670,12 +940,12 @@ export class ExpressionSpecializer {
|
|
|
670
940
|
);
|
|
671
941
|
return {
|
|
672
942
|
kind: "code",
|
|
673
|
-
text: mapped`(${this.dialect.isUndefinedText(this.dialect.code(
|
|
943
|
+
text: mapped`(${this.dialect.isUndefinedText(this.dialect.code(once, declared), declared)} ? ${this.dialect.code(right, declared && this.checker.getNonNullableType(declared))} : ${this.dialect.code(once, undefined)}${declared && this.layouts.isOptionalRecord(declared) ? ".value" : ""})`,
|
|
674
944
|
declared: declared && this.checker.getNonNullableType(declared),
|
|
675
945
|
};
|
|
676
946
|
}
|
|
677
947
|
if (isAssignment(kind)) {
|
|
678
|
-
|
|
948
|
+
return this.assignments.assign(node, frame);
|
|
679
949
|
}
|
|
680
950
|
const equality = [
|
|
681
951
|
ts.SyntaxKind.EqualsEqualsEqualsToken,
|
|
@@ -683,46 +953,20 @@ export class ExpressionSpecializer {
|
|
|
683
953
|
ts.SyntaxKind.EqualsEqualsToken,
|
|
684
954
|
ts.SyntaxKind.ExclamationEqualsToken,
|
|
685
955
|
].includes(kind);
|
|
686
|
-
const
|
|
956
|
+
const read = this.expression(node.left, frame);
|
|
957
|
+
// A left operand read before the right one writes keeps the value it had.
|
|
958
|
+
const left = containsWrite(node.right) ? this.held(read, frame) : read;
|
|
687
959
|
const right = this.expression(
|
|
688
960
|
node.right,
|
|
689
961
|
frame,
|
|
690
|
-
|
|
962
|
+
read.kind === "code" ? read.declared : undefined,
|
|
691
963
|
);
|
|
692
964
|
const negate =
|
|
693
965
|
kind === ts.SyntaxKind.ExclamationEqualsEqualsToken ||
|
|
694
966
|
kind === ts.SyntaxKind.ExclamationEqualsToken;
|
|
695
967
|
if (equality) {
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
return constant(negate ? !folded : folded);
|
|
699
|
-
}
|
|
700
|
-
const [code, other] =
|
|
701
|
-
left.kind === "code" ? [left, right] : [right, left];
|
|
702
|
-
if (
|
|
703
|
-
code.kind === "code" &&
|
|
704
|
-
other.kind === "const" &&
|
|
705
|
-
other.value === undefined
|
|
706
|
-
) {
|
|
707
|
-
const test = this.dialect.isUndefinedText(code.text, code.declared);
|
|
708
|
-
return {
|
|
709
|
-
kind: "code",
|
|
710
|
-
text: negate ? mapped`!(${test})` : test,
|
|
711
|
-
declared: this.checker.getBooleanType(),
|
|
712
|
-
};
|
|
713
|
-
}
|
|
714
|
-
const declared = code.kind === "code" ? code.declared : undefined;
|
|
715
|
-
const [leftText, rightText] = this.dialect.comparable(
|
|
716
|
-
left,
|
|
717
|
-
right,
|
|
718
|
-
declared,
|
|
719
|
-
);
|
|
720
|
-
const text = mapped`${leftText} ${negate ? "!==" : "==="} ${rightText}`;
|
|
721
|
-
return {
|
|
722
|
-
kind: "code",
|
|
723
|
-
text: mapped`(${text})`,
|
|
724
|
-
declared: this.checker.getBooleanType(),
|
|
725
|
-
};
|
|
968
|
+
this.references.identities(left, right, node);
|
|
969
|
+
return this.equality(left, right, { negate });
|
|
726
970
|
}
|
|
727
971
|
if (
|
|
728
972
|
left.kind === "const" &&
|
|
@@ -735,6 +979,17 @@ export class ExpressionSpecializer {
|
|
|
735
979
|
return constant(folded);
|
|
736
980
|
}
|
|
737
981
|
}
|
|
982
|
+
if (kind === ts.SyntaxKind.AsteriskAsteriskToken) {
|
|
983
|
+
if (
|
|
984
|
+
left.kind === "const" &&
|
|
985
|
+
right.kind === "const" &&
|
|
986
|
+
typeof left.value === "number" &&
|
|
987
|
+
typeof right.value === "number"
|
|
988
|
+
) {
|
|
989
|
+
return constant(left.value ** right.value);
|
|
990
|
+
}
|
|
991
|
+
return this.calls.power(left, right, node, frame);
|
|
992
|
+
}
|
|
738
993
|
const operator = binaryOperators.get(kind);
|
|
739
994
|
if (!operator) {
|
|
740
995
|
throw new SpecializeError(
|
|
@@ -743,16 +998,97 @@ export class ExpressionSpecializer {
|
|
|
743
998
|
);
|
|
744
999
|
}
|
|
745
1000
|
const declared = this.checker.getTypeAtLocation(node);
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
1001
|
+
if (["<", "<=", ">", ">="].includes(operator)) {
|
|
1002
|
+
const [leftText, rightText] = this.dialect.comparable(
|
|
1003
|
+
left,
|
|
1004
|
+
right,
|
|
1005
|
+
undefined,
|
|
1006
|
+
);
|
|
1007
|
+
return {
|
|
1008
|
+
kind: "code",
|
|
1009
|
+
text: mapped`(${leftText} ${operator} ${rightText})`,
|
|
1010
|
+
declared,
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
const { texts, iec } = this.dialect.arithmetic(left, right);
|
|
1014
|
+
return {
|
|
1015
|
+
kind: "code",
|
|
1016
|
+
text: mapped`(${texts[0]} ${operator} ${texts[1]})`,
|
|
1017
|
+
declared,
|
|
1018
|
+
iec,
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
/** A plain number: an enum's members are numbers to TypeScript, but the PLC's enum is no integer. */
|
|
1023
|
+
private isNumber(node: ts.Expression): boolean {
|
|
1024
|
+
const flags = this.checker.getTypeAtLocation(node).flags;
|
|
1025
|
+
return (
|
|
1026
|
+
Boolean(flags & (ts.TypeFlags.Number | ts.TypeFlags.NumberLiteral)) &&
|
|
1027
|
+
!(flags & ts.TypeFlags.EnumLike)
|
|
1028
|
+
);
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
/**
|
|
1032
|
+
* `left || right` between numbers is a number, as a comparator's `byLoad || byId` is: the left
|
|
1033
|
+
* one unless it is zero. A right operand that writes keeps the boolean form, whose branch holds it.
|
|
1034
|
+
*/
|
|
1035
|
+
private firstNonZero(
|
|
1036
|
+
left: Value & { kind: "code" },
|
|
1037
|
+
node: ts.BinaryExpression,
|
|
1038
|
+
frame: Frame,
|
|
1039
|
+
): Value {
|
|
1040
|
+
const once = this.temporaryOf(left, frame);
|
|
1041
|
+
const right = this.expression(node.right, frame);
|
|
1042
|
+
const first = this.dialect.code(once, undefined);
|
|
752
1043
|
return {
|
|
753
1044
|
kind: "code",
|
|
754
|
-
text: mapped`(${
|
|
1045
|
+
text: mapped`(${first} !== 0 ? ${first} : ${this.dialect.code(right, undefined)})`,
|
|
1046
|
+
declared: this.checker.getTypeAtLocation(node),
|
|
1047
|
+
iec: left.iec,
|
|
1048
|
+
};
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
/** `left === right`, or `!==` when negated: known now when it can be, otherwise a comparison. */
|
|
1052
|
+
equality(left: Value, right: Value, { negate }: { negate: boolean }): Value {
|
|
1053
|
+
const referred = this.references.equality(left, right, { negate });
|
|
1054
|
+
if (referred) {
|
|
1055
|
+
return referred;
|
|
1056
|
+
}
|
|
1057
|
+
const folded = this.equal(left, right);
|
|
1058
|
+
if (folded !== undefined) {
|
|
1059
|
+
return constant(negate ? !folded : folded);
|
|
1060
|
+
}
|
|
1061
|
+
const [code, other] = left.kind === "code" ? [left, right] : [right, left];
|
|
1062
|
+
if (
|
|
1063
|
+
code.kind === "code" &&
|
|
1064
|
+
other.kind === "const" &&
|
|
1065
|
+
other.value === undefined
|
|
1066
|
+
) {
|
|
1067
|
+
// A value whose storage can't hold undefined never is, as an index setup filled isn't.
|
|
1068
|
+
if (
|
|
1069
|
+
!code.declared ||
|
|
1070
|
+
(!this.layouts.isOptionalRecord(code.declared) &&
|
|
1071
|
+
!this.layouts.literals(code.declared)?.optional)
|
|
1072
|
+
) {
|
|
1073
|
+
return constant(negate);
|
|
1074
|
+
}
|
|
1075
|
+
const test = this.dialect.isUndefinedText(code.text, code.declared);
|
|
1076
|
+
return {
|
|
1077
|
+
kind: "code",
|
|
1078
|
+
text: negate ? mapped`!(${test})` : test,
|
|
1079
|
+
declared: this.checker.getBooleanType(),
|
|
1080
|
+
};
|
|
1081
|
+
}
|
|
1082
|
+
const declared = code.kind === "code" ? code.declared : undefined;
|
|
1083
|
+
const [leftText, rightText] = this.dialect.comparable(
|
|
1084
|
+
left,
|
|
1085
|
+
right,
|
|
755
1086
|
declared,
|
|
1087
|
+
);
|
|
1088
|
+
return {
|
|
1089
|
+
kind: "code",
|
|
1090
|
+
text: mapped`(${leftText} ${negate ? "!==" : "==="} ${rightText})`,
|
|
1091
|
+
declared: this.checker.getBooleanType(),
|
|
756
1092
|
};
|
|
757
1093
|
}
|
|
758
1094
|
|
|
@@ -781,6 +1117,15 @@ export class ExpressionSpecializer {
|
|
|
781
1117
|
frame: Frame,
|
|
782
1118
|
expected?: ts.Type,
|
|
783
1119
|
): Value {
|
|
1120
|
+
if (target.kind === "static" && target.node.captured.kind === "closure") {
|
|
1121
|
+
return this.invoke(
|
|
1122
|
+
this.closure(target.node, at),
|
|
1123
|
+
args,
|
|
1124
|
+
at,
|
|
1125
|
+
frame,
|
|
1126
|
+
expected,
|
|
1127
|
+
);
|
|
1128
|
+
}
|
|
784
1129
|
if (target.kind !== "function") {
|
|
785
1130
|
throw new SpecializeError(at, "Only functions can be called.");
|
|
786
1131
|
}
|
|
@@ -829,11 +1174,304 @@ export class ExpressionSpecializer {
|
|
|
829
1174
|
frame.emitter.line(mapped`${text};`);
|
|
830
1175
|
return constant(undefined);
|
|
831
1176
|
}
|
|
832
|
-
|
|
1177
|
+
if (referenceType(returns)) {
|
|
1178
|
+
return this.references.result(text, called.referents, returns, frame);
|
|
1179
|
+
}
|
|
1180
|
+
return {
|
|
1181
|
+
kind: "code",
|
|
1182
|
+
text,
|
|
1183
|
+
declared: returns,
|
|
1184
|
+
iec: called.iec,
|
|
1185
|
+
layout: called.layout,
|
|
1186
|
+
length: called.length,
|
|
1187
|
+
...(called.returnsStored && { copyOf: called.name }),
|
|
1188
|
+
};
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* `super.name`: the member the class around the expression inherits, on the same instance. Every
|
|
1193
|
+
* instance's class is known when the program compiles, so this resolves then.
|
|
1194
|
+
*/
|
|
1195
|
+
superMember(node: ts.PropertyAccessExpression, frame: Frame): Value {
|
|
1196
|
+
const owner = ts.findAncestor(node, ts.isClassDeclaration);
|
|
1197
|
+
if (!owner || !frame.self) {
|
|
1198
|
+
throw new SpecializeError(node, "`super` outside a method.");
|
|
1199
|
+
}
|
|
1200
|
+
const name = node.name.text;
|
|
1201
|
+
const inherited = this.model
|
|
1202
|
+
.ancestry(owner)
|
|
1203
|
+
.slice(1)
|
|
1204
|
+
.flatMap((base) => [...base.members]);
|
|
1205
|
+
const accessor = inherited.find(
|
|
1206
|
+
(member): member is ts.GetAccessorDeclaration =>
|
|
1207
|
+
ts.isGetAccessorDeclaration(member) && member.name.getText() === name,
|
|
1208
|
+
);
|
|
1209
|
+
if (accessor) {
|
|
1210
|
+
return this.invoke(
|
|
1211
|
+
{ kind: "function", declaration: accessor, self: frame.self },
|
|
1212
|
+
[],
|
|
1213
|
+
node,
|
|
1214
|
+
frame,
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
1217
|
+
const method = inherited.find(
|
|
1218
|
+
(member): member is ts.MethodDeclaration =>
|
|
1219
|
+
ts.isMethodDeclaration(member) && member.name.getText() === name,
|
|
1220
|
+
);
|
|
1221
|
+
if (!method) {
|
|
1222
|
+
throw new SpecializeError(node, `The base class has no ${name}.`);
|
|
1223
|
+
}
|
|
1224
|
+
return { kind: "function", declaration: method, self: frame.self };
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
/** The fields `...source` spreads into a record literal: a known record's, or each property its type declares. */
|
|
1228
|
+
spreadFields(property: ts.SpreadAssignment, frame: Frame): [string, Value][] {
|
|
1229
|
+
return this.recordFields(property.expression, frame);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
/** Each field of a record-valued expression: a known record's, or each property its type declares. */
|
|
1233
|
+
recordFields(expression: ts.Expression, frame: Frame): [string, Value][] {
|
|
1234
|
+
const evaluated = this.expression(expression, frame);
|
|
1235
|
+
if (evaluated.kind === "virtual" && evaluated.fields) {
|
|
1236
|
+
return [...evaluated.fields];
|
|
1237
|
+
}
|
|
1238
|
+
// A runtime record is read once, so a call in it runs once.
|
|
1239
|
+
const source =
|
|
1240
|
+
evaluated.kind === "code"
|
|
1241
|
+
? this.temporaryOf(evaluated, frame)
|
|
1242
|
+
: evaluated;
|
|
1243
|
+
const type = this.checker.getTypeAtLocation(expression);
|
|
1244
|
+
return this.checker
|
|
1245
|
+
.getPropertiesOfType(this.checker.getNonNullableType(type))
|
|
1246
|
+
.map((symbol): [string, Value] => [
|
|
1247
|
+
symbol.name,
|
|
1248
|
+
this.member(source, symbol.name, expression, frame),
|
|
1249
|
+
]);
|
|
1250
|
+
}
|
|
1251
|
+
|
|
1252
|
+
/**
|
|
1253
|
+
* A record or array literal stays a value known now, unless the code changes it through its name or
|
|
1254
|
+
* lends it to a call: then it is a local, as a new object is. One holding objects setup built stays known.
|
|
1255
|
+
*/
|
|
1256
|
+
private heldAsLocal(
|
|
1257
|
+
value: Value & { kind: "virtual" },
|
|
1258
|
+
name: ts.Identifier,
|
|
1259
|
+
symbol: ts.Symbol,
|
|
1260
|
+
): boolean {
|
|
1261
|
+
const inner = [...(value.fields?.values() ?? []), ...(value.items ?? [])];
|
|
1262
|
+
if (
|
|
1263
|
+
inner.some(
|
|
1264
|
+
(item) =>
|
|
1265
|
+
item.kind === "static" ||
|
|
1266
|
+
item.kind === "function" ||
|
|
1267
|
+
item.kind === "choice",
|
|
1268
|
+
)
|
|
1269
|
+
) {
|
|
1270
|
+
return false;
|
|
1271
|
+
}
|
|
1272
|
+
const scope =
|
|
1273
|
+
ts.findAncestor(name.parent, ts.isFunctionLike) ?? name.getSourceFile();
|
|
1274
|
+
return writesThrough(scope, symbol, this.checker);
|
|
1275
|
+
}
|
|
1276
|
+
|
|
1277
|
+
/** `typeof`, known when the program compiles: from a value setup fixed, or from runtime storage's declared type. */
|
|
1278
|
+
private typeOf(value: Value): Value {
|
|
1279
|
+
if (value.kind === "const") {
|
|
1280
|
+
return constant(typeof value.value);
|
|
1281
|
+
}
|
|
1282
|
+
if (value.kind === "function") {
|
|
1283
|
+
return constant("function");
|
|
1284
|
+
}
|
|
1285
|
+
if (value.kind !== "code" || !value.declared) {
|
|
1286
|
+
return constant("object");
|
|
1287
|
+
}
|
|
1288
|
+
const declared = value.declared;
|
|
1289
|
+
const kinds: [ts.TypeFlags, string][] = [
|
|
1290
|
+
[ts.TypeFlags.NumberLike, "number"],
|
|
1291
|
+
[ts.TypeFlags.StringLike, "string"],
|
|
1292
|
+
[ts.TypeFlags.BooleanLike, "boolean"],
|
|
1293
|
+
[ts.TypeFlags.BigIntLike, "bigint"],
|
|
1294
|
+
];
|
|
1295
|
+
const found = kinds.find(([flag]) => declared.flags & flag);
|
|
1296
|
+
return constant(found ? found[1] : "object");
|
|
1297
|
+
}
|
|
1298
|
+
|
|
1299
|
+
/** `Math.PI` and JavaScript's other Math constants, from its own library rather than the program. */
|
|
1300
|
+
private isMathConstant(node: ts.PropertyAccessExpression): boolean {
|
|
1301
|
+
return (
|
|
1302
|
+
ts.isIdentifier(node.expression) &&
|
|
1303
|
+
node.expression.text === "Math" &&
|
|
1304
|
+
mathConstants.has(node.name.text) &&
|
|
1305
|
+
(this.checker
|
|
1306
|
+
.getSymbolAtLocation(node.expression)
|
|
1307
|
+
?.declarations?.every(
|
|
1308
|
+
(declaration) => declaration.getSourceFile().isDeclarationFile,
|
|
1309
|
+
) ??
|
|
1310
|
+
false)
|
|
1311
|
+
);
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
/**
|
|
1315
|
+
* A function setup made inside another and stored: its declaration, with the values it captured
|
|
1316
|
+
* as its closure, and the object it was made in as its `this`.
|
|
1317
|
+
*/
|
|
1318
|
+
private closure(node: ModelNode, at: ts.Node): Value {
|
|
1319
|
+
const captured = node.captured;
|
|
1320
|
+
if (captured.kind !== "closure") {
|
|
1321
|
+
throw new SpecializeError(at, "Not a closure.");
|
|
1322
|
+
}
|
|
1323
|
+
const source = this.model.sourceFile(captured.file);
|
|
1324
|
+
const declaration = findClosure(source, captured.start);
|
|
1325
|
+
if (!declaration) {
|
|
1326
|
+
throw new SpecializeError(
|
|
1327
|
+
at,
|
|
1328
|
+
"The function setup stored isn't in the program.",
|
|
1329
|
+
);
|
|
1330
|
+
}
|
|
1331
|
+
// Each captured name maps to the symbols the closure's own identifiers read by it.
|
|
1332
|
+
const symbols = closureSymbols(declaration, this.checker);
|
|
1333
|
+
const env: Env = new Map(
|
|
1334
|
+
captured.captures.flatMap(({ name, value }): [ts.Symbol, Value][] => {
|
|
1335
|
+
const held = this.model.node(value);
|
|
1336
|
+
const known: Value = held
|
|
1337
|
+
? { kind: "static", node: held }
|
|
1338
|
+
: capturedConstant(value);
|
|
1339
|
+
return (symbols.get(name) ?? []).map((symbol): [ts.Symbol, Value] => [
|
|
1340
|
+
symbol,
|
|
1341
|
+
known,
|
|
1342
|
+
]);
|
|
1343
|
+
}),
|
|
1344
|
+
);
|
|
1345
|
+
const made = captured.captures.find(({ name }) => name === "this");
|
|
1346
|
+
const self = made && this.model.node(made.value);
|
|
1347
|
+
return { kind: "function", declaration, self, env };
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
/** `record?.field` on an optional record: the field as an optional, or the empty value when the record is. */
|
|
1351
|
+
private optionalMember(
|
|
1352
|
+
object: Value & { kind: "code" },
|
|
1353
|
+
node: ts.PropertyAccessExpression,
|
|
1354
|
+
frame: Frame,
|
|
1355
|
+
): Value {
|
|
1356
|
+
const declared = object.declared;
|
|
1357
|
+
if (!declared) {
|
|
1358
|
+
throw new SpecializeError(node, "Not an optional record.");
|
|
1359
|
+
}
|
|
1360
|
+
const held = this.temporaryOf(object, frame);
|
|
1361
|
+
if (held.kind !== "code") {
|
|
1362
|
+
throw new SpecializeError(node, "Not an optional record.");
|
|
1363
|
+
}
|
|
1364
|
+
const defined: Value = {
|
|
1365
|
+
kind: "code",
|
|
1366
|
+
text: mapped`${held.text}.value`,
|
|
1367
|
+
declared: this.checker.getNonNullableType(declared),
|
|
1368
|
+
place: held.place,
|
|
1369
|
+
};
|
|
1370
|
+
const field = this.member(defined, node.name.text, node, frame);
|
|
1371
|
+
const result = this.checker.getTypeAtLocation(node);
|
|
1372
|
+
return {
|
|
1373
|
+
kind: "code",
|
|
1374
|
+
text: mapped`(${this.dialect.isUndefinedText(held.text, declared)} ? ${this.dialect.assignmentText(constant(undefined), result)} : ${this.dialect.assignmentText(field, result)})`,
|
|
1375
|
+
declared: result,
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
/** A call to a generator, which a for…of runs inline. */
|
|
1380
|
+
generator(node: ts.Expression, frame: Frame) {
|
|
1381
|
+
return this.calls.generator(node, frame);
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
/**
|
|
1385
|
+
* Evaluates operands left to right. A runtime value read before a later operand writes is held in
|
|
1386
|
+
* a const first, so it is the value JavaScript reads then.
|
|
1387
|
+
*/
|
|
1388
|
+
inOrder<T extends ts.Node>(
|
|
1389
|
+
nodes: readonly T[],
|
|
1390
|
+
evaluate: (node: T, index: number) => Value,
|
|
1391
|
+
frame: Frame,
|
|
1392
|
+
): Value[] {
|
|
1393
|
+
return nodes.map((node, index) => {
|
|
1394
|
+
const value = evaluate(node, index);
|
|
1395
|
+
return nodes.slice(index + 1).some(containsWrite)
|
|
1396
|
+
? this.held(value, frame)
|
|
1397
|
+
: value;
|
|
1398
|
+
});
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
/** A runtime scalar's current value, in a const of its own; aggregates and known values stay as they are. */
|
|
1402
|
+
held(value: Value, frame: Frame): Value {
|
|
1403
|
+
if (value.kind === "choice") {
|
|
1404
|
+
return this.references.held(value, frame);
|
|
1405
|
+
}
|
|
1406
|
+
if (
|
|
1407
|
+
value.kind !== "code" ||
|
|
1408
|
+
(value.declared && this.isAggregate(value.declared))
|
|
1409
|
+
) {
|
|
1410
|
+
return value;
|
|
1411
|
+
}
|
|
1412
|
+
const local = frame.emitter.temporary();
|
|
1413
|
+
const type =
|
|
1414
|
+
value.iec ?? (value.declared ? this.localType(value.declared) : "number");
|
|
1415
|
+
frame.emitter.line(mapped`const ${local}: ${type} = ${value.text};`);
|
|
1416
|
+
return {
|
|
1417
|
+
kind: "code",
|
|
1418
|
+
text: local,
|
|
1419
|
+
declared: value.declared,
|
|
1420
|
+
place: true,
|
|
1421
|
+
iec: value.iec,
|
|
1422
|
+
};
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
/** `.value` for an optional record, whose defined value sits in a field. */
|
|
1426
|
+
optionalValueSuffix(declared: ts.Type): string {
|
|
1427
|
+
return this.layouts.isOptionalRecord(declared) ? ".value" : "";
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
/**
|
|
1431
|
+
* A choice whose branches write, as an if statement storing each branch's value in a local, so a
|
|
1432
|
+
* branch's writes happen only when it is taken.
|
|
1433
|
+
*/
|
|
1434
|
+
private branched(
|
|
1435
|
+
declared: ts.Type,
|
|
1436
|
+
condition: Part,
|
|
1437
|
+
branches: readonly [
|
|
1438
|
+
ts.Expression | (() => Value),
|
|
1439
|
+
ts.Expression | (() => Value),
|
|
1440
|
+
],
|
|
1441
|
+
frame: Frame,
|
|
1442
|
+
): Value {
|
|
1443
|
+
const result = frame.emitter.temporary();
|
|
1444
|
+
frame.emitter.line(
|
|
1445
|
+
`let ${result}: ${this.localType(declared)} = ${this.initializers.defaultOf(declared)};`,
|
|
1446
|
+
);
|
|
1447
|
+
const [yes, no] = branches.map(
|
|
1448
|
+
(branch) => () =>
|
|
1449
|
+
typeof branch === "function"
|
|
1450
|
+
? branch()
|
|
1451
|
+
: this.expression(branch, frame, declared),
|
|
1452
|
+
);
|
|
1453
|
+
frame.emitter.open(mapped`if (${condition}) {`);
|
|
1454
|
+
frame.emitter.line(
|
|
1455
|
+
mapped`${result} = ${this.dialect.assignmentText(yes!(), declared, undefined)};`,
|
|
1456
|
+
);
|
|
1457
|
+
frame.emitter.reopen("} else {");
|
|
1458
|
+
frame.emitter.line(
|
|
1459
|
+
mapped`${result} = ${this.dialect.assignmentText(no!(), declared, undefined)};`,
|
|
1460
|
+
);
|
|
1461
|
+
frame.emitter.close();
|
|
1462
|
+
return { kind: "code", text: result, declared, place: true };
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
/**
|
|
1466
|
+
* Whether an operand JavaScript evaluates only when it needs it is written as statements: it
|
|
1467
|
+
* writes a place, or runs a generator or a snapshot's loop. Those go in the branch that needs them.
|
|
1468
|
+
*/
|
|
1469
|
+
private runsWhenNeeded(node: ts.Expression, frame: Frame): boolean {
|
|
1470
|
+
return containsWrite(node) || this.iterators.traverses(node, frame);
|
|
833
1471
|
}
|
|
834
1472
|
|
|
835
1473
|
/** A runtime value held in a const, so inlining doesn't evaluate it twice. */
|
|
836
|
-
|
|
1474
|
+
temporaryOf(value: Value & { kind: "code" }, frame: Frame): Value {
|
|
837
1475
|
if (repeatable(value.text)) {
|
|
838
1476
|
return value;
|
|
839
1477
|
}
|
|
@@ -850,6 +1488,8 @@ export class ExpressionSpecializer {
|
|
|
850
1488
|
mutable: boolean,
|
|
851
1489
|
frame: Frame,
|
|
852
1490
|
declared: ts.Type,
|
|
1491
|
+
/** The IEC width the local's annotation names, as `const slot: UINT` does. */
|
|
1492
|
+
iec?: string,
|
|
853
1493
|
): void {
|
|
854
1494
|
// A pattern reads its value once per name, so a computed value is held first.
|
|
855
1495
|
const bound =
|
|
@@ -887,29 +1527,93 @@ export class ExpressionSpecializer {
|
|
|
887
1527
|
return;
|
|
888
1528
|
}
|
|
889
1529
|
const symbol = this.checker.getSymbolAtLocation(name)!;
|
|
1530
|
+
const reference = this.references.local(name.text, bound, declared, frame, {
|
|
1531
|
+
mutable,
|
|
1532
|
+
});
|
|
1533
|
+
if (reference) {
|
|
1534
|
+
frame.env.set(symbol, reference);
|
|
1535
|
+
return;
|
|
1536
|
+
}
|
|
1537
|
+
if (
|
|
1538
|
+
bound.kind === "virtual" &&
|
|
1539
|
+
!isReadonly(declared, this.checker) &&
|
|
1540
|
+
this.heldAsLocal(bound, name, symbol)
|
|
1541
|
+
) {
|
|
1542
|
+
const local = frame.emitter.local(name.text);
|
|
1543
|
+
frame.emitter.line(
|
|
1544
|
+
mapped`let ${local}: ${this.localType(declared)} = ${this.dialect.assignmentText(bound, declared)};`,
|
|
1545
|
+
);
|
|
1546
|
+
frame.env.set(symbol, {
|
|
1547
|
+
kind: "code",
|
|
1548
|
+
text: local,
|
|
1549
|
+
declared,
|
|
1550
|
+
place: true,
|
|
1551
|
+
});
|
|
1552
|
+
return;
|
|
1553
|
+
}
|
|
890
1554
|
if (!mutable && bound.kind !== "code") {
|
|
891
1555
|
frame.env.set(symbol, bound);
|
|
892
1556
|
return;
|
|
893
1557
|
}
|
|
1558
|
+
if (
|
|
1559
|
+
!mutable &&
|
|
1560
|
+
bound.kind === "code" &&
|
|
1561
|
+
bound.place &&
|
|
1562
|
+
ts.isIdentifier(name) &&
|
|
1563
|
+
this.isAggregate(this.checker.getNonNullableType(declared)) &&
|
|
1564
|
+
replacedWhileAliased(name, this.checker)
|
|
1565
|
+
) {
|
|
1566
|
+
this.snapshot(name, symbol, bound, declared, frame);
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
894
1569
|
if (!mutable && bound.kind === "code" && bound.place) {
|
|
895
|
-
// A const naming a place is an alias, as an object binding is in TypeScript.
|
|
1570
|
+
// A const naming a place is an alias, as an object binding is in TypeScript. Its runtime
|
|
1571
|
+
// indexes are held now, so it keeps naming that element when they change; an index read
|
|
1572
|
+
// again at each use stays a value the PLC's bounds proof can follow.
|
|
896
1573
|
if (this.isAggregate(declared)) {
|
|
897
|
-
|
|
1574
|
+
const pin = bound.pinned;
|
|
1575
|
+
const repeats =
|
|
1576
|
+
ts.isIdentifier(name) &&
|
|
1577
|
+
pureRead(bound.text) &&
|
|
1578
|
+
stableAlias(name, this.checker);
|
|
1579
|
+
frame.env.set(symbol, pin && !repeats ? pin(frame) : bound);
|
|
898
1580
|
return;
|
|
899
1581
|
}
|
|
900
1582
|
}
|
|
901
1583
|
const local = frame.emitter.local(name.text);
|
|
902
|
-
const
|
|
903
|
-
|
|
1584
|
+
// A const record changed through its name, or lent to a call that may, is a variable to the PLC:
|
|
1585
|
+
// TypeScript's const fixes the binding, not the object.
|
|
1586
|
+
const changes =
|
|
1587
|
+
!mutable &&
|
|
1588
|
+
this.isAggregate(declared) &&
|
|
1589
|
+
writesThrough(
|
|
1590
|
+
ts.findAncestor(name.parent, ts.isFunctionLike) ?? name.getSourceFile(),
|
|
1591
|
+
symbol,
|
|
1592
|
+
this.checker,
|
|
1593
|
+
);
|
|
1594
|
+
const width = iec ?? (bound.kind === "code" ? bound.iec : undefined);
|
|
1595
|
+
const layout = bound.kind === "code" ? bound.layout : undefined;
|
|
1596
|
+
const length = bound.kind === "code" ? bound.length : undefined;
|
|
1597
|
+
const type = width ?? layout ?? this.localType(declared);
|
|
1598
|
+
// An annotated width converts the value as IEC assignment does.
|
|
1599
|
+
const text = iec
|
|
1600
|
+
? this.dialect.assignmentText(bound, declared, iec)
|
|
1601
|
+
: this.dialect.code(bound, declared);
|
|
1602
|
+
// What an await gives is typed by the call awaited, which the async lowering reads itself.
|
|
1603
|
+
const annotation =
|
|
1604
|
+
bound.kind === "code" && bound.awaited && !iec ? "" : `: ${type}`;
|
|
904
1605
|
frame.emitter.line(
|
|
905
|
-
mapped`${mutable ? "let" : "const"} ${local}
|
|
1606
|
+
mapped`${mutable || changes ? "let" : "const"} ${local}${annotation} = ${text};`,
|
|
906
1607
|
);
|
|
907
1608
|
frame.env.set(symbol, {
|
|
908
1609
|
kind: "code",
|
|
909
1610
|
text: local,
|
|
910
1611
|
declared,
|
|
911
1612
|
place: true,
|
|
912
|
-
iec:
|
|
1613
|
+
iec: width,
|
|
1614
|
+
layout,
|
|
1615
|
+
length,
|
|
1616
|
+
copyOf: bound.kind === "code" ? bound.copyOf : undefined,
|
|
913
1617
|
});
|
|
914
1618
|
}
|
|
915
1619
|
|
|
@@ -921,6 +1625,46 @@ export class ExpressionSpecializer {
|
|
|
921
1625
|
return this.layouts.text(this.checker.getBaseTypeOfLiteralType(declared));
|
|
922
1626
|
}
|
|
923
1627
|
|
|
1628
|
+
/**
|
|
1629
|
+
* A const record whose place is assigned anew while it is read keeps what the place held, as the
|
|
1630
|
+
* object TypeScript's const names does. The PLC reads a record const from its place at each use, so
|
|
1631
|
+
* it is a copy instead, which can't also pass writes through to the place.
|
|
1632
|
+
*/
|
|
1633
|
+
private snapshot(
|
|
1634
|
+
name: ts.Identifier,
|
|
1635
|
+
symbol: ts.Symbol,
|
|
1636
|
+
bound: Value & { kind: "code" },
|
|
1637
|
+
declared: ts.Type,
|
|
1638
|
+
frame: Frame,
|
|
1639
|
+
): void {
|
|
1640
|
+
if (
|
|
1641
|
+
writesThrough(
|
|
1642
|
+
ts.findAncestor(name.parent, ts.isFunctionLike) ?? name.getSourceFile(),
|
|
1643
|
+
symbol,
|
|
1644
|
+
this.checker,
|
|
1645
|
+
)
|
|
1646
|
+
) {
|
|
1647
|
+
throw new SpecializeError(
|
|
1648
|
+
name,
|
|
1649
|
+
"This const is changed through, and the place it names is assigned anew while it is still used: the PLC can't keep both. Take a structuredClone of it, or change the place through its own path.",
|
|
1650
|
+
);
|
|
1651
|
+
}
|
|
1652
|
+
const local = frame.emitter.local(name.text);
|
|
1653
|
+
const type = bound.layout ?? this.localType(declared);
|
|
1654
|
+
this.imports.addSdk("@tsplc/compiler/types", "copy");
|
|
1655
|
+
frame.emitter.line(
|
|
1656
|
+
mapped`const ${local}: ${type} = copy(${this.dialect.code(bound, declared)});`,
|
|
1657
|
+
);
|
|
1658
|
+
frame.env.set(symbol, {
|
|
1659
|
+
kind: "code",
|
|
1660
|
+
text: local,
|
|
1661
|
+
declared,
|
|
1662
|
+
place: true,
|
|
1663
|
+
layout: bound.layout,
|
|
1664
|
+
length: bound.length,
|
|
1665
|
+
});
|
|
1666
|
+
}
|
|
1667
|
+
|
|
924
1668
|
private isAggregate(type: ts.Type): boolean {
|
|
925
1669
|
return (
|
|
926
1670
|
Boolean(type.flags & ts.TypeFlags.Object) &&
|
|
@@ -936,6 +1680,20 @@ export class ExpressionSpecializer {
|
|
|
936
1680
|
if (value.kind === "virtual" && value.items) {
|
|
937
1681
|
return value.items;
|
|
938
1682
|
}
|
|
1683
|
+
// An array scan code writes is stored, at the length setup gave it.
|
|
1684
|
+
if (value.kind === "code" && value.length !== undefined) {
|
|
1685
|
+
const declared = value.declared && this.model.elementType(value.declared);
|
|
1686
|
+
return Array.from(
|
|
1687
|
+
{ length: value.length },
|
|
1688
|
+
(_, index): Value => ({
|
|
1689
|
+
kind: "code",
|
|
1690
|
+
text: mapped`${value.text}[${index}]`,
|
|
1691
|
+
declared,
|
|
1692
|
+
place: value.place,
|
|
1693
|
+
iec: value.elementIec,
|
|
1694
|
+
}),
|
|
1695
|
+
);
|
|
1696
|
+
}
|
|
939
1697
|
if (value.kind !== "static") {
|
|
940
1698
|
return undefined;
|
|
941
1699
|
}
|
|
@@ -993,3 +1751,92 @@ const foldNumber = (
|
|
|
993
1751
|
return undefined;
|
|
994
1752
|
}
|
|
995
1753
|
};
|
|
1754
|
+
|
|
1755
|
+
const mathConstants = new Map([
|
|
1756
|
+
["E", Math.E],
|
|
1757
|
+
["LN10", Math.LN10],
|
|
1758
|
+
["LN2", Math.LN2],
|
|
1759
|
+
["LOG10E", Math.LOG10E],
|
|
1760
|
+
["LOG2E", Math.LOG2E],
|
|
1761
|
+
["PI", Math.PI],
|
|
1762
|
+
["SQRT1_2", Math.SQRT1_2],
|
|
1763
|
+
["SQRT2", Math.SQRT2],
|
|
1764
|
+
]);
|
|
1765
|
+
|
|
1766
|
+
/** A readonly array or record: what holds it can't change it, so a literal given to it stays a value. */
|
|
1767
|
+
const isReadonly = (type: ts.Type, checker: ts.TypeChecker): boolean =>
|
|
1768
|
+
(checker.isArrayType(type) === false &&
|
|
1769
|
+
type.getSymbol()?.name === "ReadonlyArray") ||
|
|
1770
|
+
type.aliasSymbol?.name === "Readonly" ||
|
|
1771
|
+
checker.typeToString(type).startsWith("readonly ");
|
|
1772
|
+
|
|
1773
|
+
/** The arrow or function expression written at `start`. */
|
|
1774
|
+
const findClosure = (
|
|
1775
|
+
source: ts.SourceFile,
|
|
1776
|
+
start: number,
|
|
1777
|
+
): ts.ArrowFunction | ts.FunctionExpression | undefined => {
|
|
1778
|
+
const visit = (
|
|
1779
|
+
node: ts.Node,
|
|
1780
|
+
): ts.ArrowFunction | ts.FunctionExpression | undefined => {
|
|
1781
|
+
if (
|
|
1782
|
+
(ts.isArrowFunction(node) || ts.isFunctionExpression(node)) &&
|
|
1783
|
+
node.getStart(source) === start
|
|
1784
|
+
) {
|
|
1785
|
+
return node;
|
|
1786
|
+
}
|
|
1787
|
+
return node.pos <= start && start < node.end
|
|
1788
|
+
? ts.forEachChild(node, visit)
|
|
1789
|
+
: undefined;
|
|
1790
|
+
};
|
|
1791
|
+
return visit(source);
|
|
1792
|
+
};
|
|
1793
|
+
|
|
1794
|
+
/** A primitive setup captured, as a value known now. */
|
|
1795
|
+
const capturedConstant = (value: CapturedValue): Value => {
|
|
1796
|
+
switch (value.kind) {
|
|
1797
|
+
case "undefined":
|
|
1798
|
+
return constant(undefined);
|
|
1799
|
+
case "bigint":
|
|
1800
|
+
return constant(BigInt(value.value));
|
|
1801
|
+
case "node":
|
|
1802
|
+
throw new Error("Not a primitive.");
|
|
1803
|
+
default:
|
|
1804
|
+
return constant(value.value);
|
|
1805
|
+
}
|
|
1806
|
+
};
|
|
1807
|
+
|
|
1808
|
+
/** The symbols a closure's identifiers read, by name; a shorthand property reads its value's. */
|
|
1809
|
+
const closureSymbols = (
|
|
1810
|
+
declaration: ts.Node,
|
|
1811
|
+
checker: ts.TypeChecker,
|
|
1812
|
+
): ReadonlyMap<string, ts.Symbol[]> => {
|
|
1813
|
+
const found = new Map<string, ts.Symbol[]>();
|
|
1814
|
+
const visit = (node: ts.Node): void => {
|
|
1815
|
+
if (ts.isIdentifier(node)) {
|
|
1816
|
+
const symbol = ts.isShorthandPropertyAssignment(node.parent)
|
|
1817
|
+
? checker.getShorthandAssignmentValueSymbol(node.parent)
|
|
1818
|
+
: checker.getSymbolAtLocation(node);
|
|
1819
|
+
const known = found.get(node.text) ?? [];
|
|
1820
|
+
if (symbol && !known.includes(symbol)) {
|
|
1821
|
+
found.set(node.text, [...known, symbol]);
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
ts.forEachChild(node, visit);
|
|
1825
|
+
};
|
|
1826
|
+
visit(declaration);
|
|
1827
|
+
return found;
|
|
1828
|
+
};
|
|
1829
|
+
|
|
1830
|
+
/** The expression a getter's body is no more than `return`ing. */
|
|
1831
|
+
const onlyReturned = (
|
|
1832
|
+
accessor: ts.GetAccessorDeclaration,
|
|
1833
|
+
): ts.Expression | undefined => {
|
|
1834
|
+
const [only, ...others] = accessor.body?.statements ?? [];
|
|
1835
|
+
return only && !others.length && ts.isReturnStatement(only)
|
|
1836
|
+
? only.expression
|
|
1837
|
+
: undefined;
|
|
1838
|
+
};
|
|
1839
|
+
|
|
1840
|
+
/** Whether reading a value's text again reads the same places and runs nothing: no call besides a conversion. */
|
|
1841
|
+
const pureRead = (text: string | Mapped): boolean =>
|
|
1842
|
+
!/[\w\]>]\s*\(/.test(String(text).replace(/\b(?:wrap|exact)<\w+>\(/g, "("));
|