@tsplc/compiler 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/codesys.js +2 -2
- package/dist/codesys.js.map +1 -1
- package/dist/index.js +1247 -536
- package/dist/index.js.map +15 -13
- package/package.json +9 -9
- package/src/frontend/analyzer.ts +32 -3
- package/src/frontend/elaborate/calls.ts +5 -5
- package/src/frontend/elaborate/frame.ts +2 -1
- package/src/frontend/elaborate/imports.ts +54 -13
- package/src/frontend/elaborate/index.ts +192 -166
- package/src/frontend/elaborate/layouts.ts +57 -30
- package/src/frontend/elaborate/model.ts +20 -1
- package/src/frontend/elaborate/names.ts +134 -0
- package/src/frontend/elaborate/reduce.ts +900 -0
- package/src/frontend/elaborate/specialize.ts +28 -4
- package/src/frontend/elaborate/storage.ts +7 -7
- package/src/frontend/typescript.ts +117 -3
|
@@ -23,8 +23,9 @@ import { Emitter, type Frame, type Referents } from "./frame";
|
|
|
23
23
|
import type { GenericTypes } from "./generics";
|
|
24
24
|
import { EntryImports } from "./imports";
|
|
25
25
|
import { Initializers } from "./initializers";
|
|
26
|
-
import { type ArrayLayout, arrayLayout, Layouts
|
|
26
|
+
import { type ArrayLayout, arrayLayout, Layouts } from "./layouts";
|
|
27
27
|
import { type Model, type ModelNode, type Root, tsPath } from "./model";
|
|
28
|
+
import { Names, routineNames, unusedName } from "./names";
|
|
28
29
|
import { referenceLayout, referenceType } from "./references";
|
|
29
30
|
import { StatementSpecializer } from "./statements";
|
|
30
31
|
import { Storage } from "./storage";
|
|
@@ -51,7 +52,9 @@ export class Specializer implements RoutineTable {
|
|
|
51
52
|
private readonly dialect: DialectText;
|
|
52
53
|
readonly expressions: ExpressionSpecializer;
|
|
53
54
|
private readonly statements: StatementSpecializer;
|
|
54
|
-
readonly imports
|
|
55
|
+
readonly imports: EntryImports;
|
|
56
|
+
/** The names the generated modules use, which routines, types and imports share. */
|
|
57
|
+
private readonly names: Names;
|
|
55
58
|
/** The source of each routine specialized so far, in the order each was finished. */
|
|
56
59
|
private readonly finished: Mapped[] = [];
|
|
57
60
|
/** Each async routine specialized for an instance, by routine name, which `cancelAsync(this)` cancels. */
|
|
@@ -76,22 +79,31 @@ export class Specializer implements RoutineTable {
|
|
|
76
79
|
{
|
|
77
80
|
kept,
|
|
78
81
|
named,
|
|
82
|
+
enumRefusal,
|
|
79
83
|
}: {
|
|
80
84
|
/** Modules the compiled program keeps as written. */
|
|
81
85
|
kept: (file: string) => boolean;
|
|
82
86
|
/** Modules whose exported types the compiled program imports by name rather than copies. */
|
|
83
87
|
named: (source: ts.SourceFile) => boolean;
|
|
88
|
+
/** Why the generated modules can't import an enum from the module that declares it; undefined when they can. */
|
|
89
|
+
enumRefusal: (declaration: ts.EnumDeclaration) => string | undefined;
|
|
84
90
|
},
|
|
85
91
|
rootModule: (root: Root) => string,
|
|
86
92
|
) {
|
|
87
93
|
const checker = generics.checker;
|
|
88
94
|
this.checker = checker;
|
|
95
|
+
this.names = new Names(
|
|
96
|
+
routineNames(sources.filter((source) => !kept(source.fileName))),
|
|
97
|
+
);
|
|
98
|
+
this.imports = new EntryImports(this.names);
|
|
89
99
|
this.layouts = new Layouts(
|
|
90
100
|
model,
|
|
91
101
|
checker,
|
|
92
102
|
kept,
|
|
93
103
|
sources.filter(named),
|
|
94
104
|
(node) => this.storage.storageType(node),
|
|
105
|
+
this.names,
|
|
106
|
+
enumRefusal,
|
|
95
107
|
);
|
|
96
108
|
this.storage = new Storage(
|
|
97
109
|
model,
|
|
@@ -178,9 +190,10 @@ export class Specializer implements RoutineTable {
|
|
|
178
190
|
}
|
|
179
191
|
const name = unusedName(
|
|
180
192
|
`tsplcCancel_${identityDigest(this.model.identity(self) ?? String(self.id))}`,
|
|
181
|
-
(candidate) => this.
|
|
193
|
+
(candidate) => this.routineNamed(candidate),
|
|
182
194
|
);
|
|
183
195
|
this.results.set(name, {});
|
|
196
|
+
this.names.declare(name);
|
|
184
197
|
this.cancellers.set(self, name);
|
|
185
198
|
this.imports.addSdk(sdkModules.async, "cancelAsync");
|
|
186
199
|
return name;
|
|
@@ -566,11 +579,22 @@ export class Specializer implements RoutineTable {
|
|
|
566
579
|
const base = specialized
|
|
567
580
|
? `${named}_${identityDigest([instance ?? "", ...stable].join("\0"))}`
|
|
568
581
|
: named;
|
|
569
|
-
const name = unusedName(base, (candidate) => this.
|
|
582
|
+
const name = unusedName(base, (candidate) => this.routineNamed(candidate));
|
|
570
583
|
this.results.set(name, {});
|
|
584
|
+
this.names.declare(name);
|
|
571
585
|
return name;
|
|
572
586
|
}
|
|
573
587
|
|
|
588
|
+
/** Whether the generated entry module already uses `name`, for a routine or an import. */
|
|
589
|
+
named(name: string): boolean {
|
|
590
|
+
return this.routineNamed(name);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
/** Whether a routine can't take `name`: another has it, or the entry module imports something as it. */
|
|
594
|
+
private routineNamed(name: string): boolean {
|
|
595
|
+
return this.results.has(name) || this.names.importedAs(name);
|
|
596
|
+
}
|
|
597
|
+
|
|
574
598
|
private bindParameter(
|
|
575
599
|
name: ts.BindingName,
|
|
576
600
|
arg: Value,
|
|
@@ -36,7 +36,7 @@ interface FoldedField {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/** A stored field of a flattened root: a global of the root's storage module. */
|
|
39
|
-
interface RootField {
|
|
39
|
+
export interface RootField {
|
|
40
40
|
name: string;
|
|
41
41
|
layout: string;
|
|
42
42
|
declaration: ts.Node | undefined;
|
|
@@ -283,10 +283,11 @@ export class Storage {
|
|
|
283
283
|
private reach(root: Root): string {
|
|
284
284
|
const module = this.rootModule(root);
|
|
285
285
|
const pascal = pascalCase(basename(module, ".ts"));
|
|
286
|
-
const namespace = this.typeNames.has(pascal) ? `${pascal}Root` : pascal;
|
|
287
286
|
this.storedRoots.add(root);
|
|
288
|
-
this.imports.
|
|
289
|
-
|
|
287
|
+
return this.imports.namespace(
|
|
288
|
+
module,
|
|
289
|
+
this.typeNames.has(pascal) ? `${pascal}Root` : pascal,
|
|
290
|
+
);
|
|
290
291
|
}
|
|
291
292
|
|
|
292
293
|
/** Stores a root whether or not scan code reads it, as exported data must be. */
|
|
@@ -599,12 +600,11 @@ export class Storage {
|
|
|
599
600
|
};
|
|
600
601
|
}
|
|
601
602
|
|
|
602
|
-
/** A binding in a module the program keeps, read
|
|
603
|
+
/** A binding in a module the program keeps, read through the entry's import of it. */
|
|
603
604
|
private keptRoot(root: Root): Value {
|
|
604
|
-
this.imports.kept.add(root.file, root.name);
|
|
605
605
|
return {
|
|
606
606
|
kind: "code",
|
|
607
|
-
text: root.name,
|
|
607
|
+
text: this.imports.kept.add(root.file, root.name),
|
|
608
608
|
declared: this.checker.getTypeAtLocation(root.declaration.name),
|
|
609
609
|
place: !(ts.getCombinedNodeFlags(root.declaration) & ts.NodeFlags.Const),
|
|
610
610
|
};
|
|
@@ -223,6 +223,8 @@ function compileFrontend(
|
|
|
223
223
|
return { diagnostics: [...diagnostics, ...uses] };
|
|
224
224
|
}
|
|
225
225
|
}
|
|
226
|
+
// Only shown when compiling fails, which one of them may explain.
|
|
227
|
+
const lost: Diagnostic[] = [];
|
|
226
228
|
const lowerings = new LoweringPipeline(program, (program, lowering) => {
|
|
227
229
|
diagnostics.push(...lowering.diagnostics);
|
|
228
230
|
for (const source of runtimeSources(program)) {
|
|
@@ -256,7 +258,20 @@ function compileFrontend(
|
|
|
256
258
|
host,
|
|
257
259
|
oldProgram: program,
|
|
258
260
|
});
|
|
259
|
-
|
|
261
|
+
const plc = plcFiles(lowered);
|
|
262
|
+
diagnostics.push(...sourceDiagnostics(lowered, plc));
|
|
263
|
+
lost.push(
|
|
264
|
+
...lostImports({
|
|
265
|
+
before: program,
|
|
266
|
+
after: lowered,
|
|
267
|
+
rewritten: lowering.sources,
|
|
268
|
+
plc: new Set(
|
|
269
|
+
[...plc, ...plcFiles(program)].map(({ fileName }) => fileName),
|
|
270
|
+
),
|
|
271
|
+
root,
|
|
272
|
+
positions,
|
|
273
|
+
}),
|
|
274
|
+
);
|
|
260
275
|
return lowered;
|
|
261
276
|
});
|
|
262
277
|
// A plain program's setup reads the configuration this resolves. The pass runs again below for
|
|
@@ -290,7 +305,7 @@ function compileFrontend(
|
|
|
290
305
|
);
|
|
291
306
|
const lowered = lowerings.program;
|
|
292
307
|
if (!lowered || !elaboration || !services || !promises || !lowering) {
|
|
293
|
-
return { diagnostics };
|
|
308
|
+
return { diagnostics: [...diagnostics, ...lost] };
|
|
294
309
|
}
|
|
295
310
|
const analyzer = new Analyzer(
|
|
296
311
|
lowered,
|
|
@@ -300,10 +315,13 @@ function compileFrontend(
|
|
|
300
315
|
diagnostics,
|
|
301
316
|
positions,
|
|
302
317
|
serviceRecords(lowered, services),
|
|
318
|
+
elaboration.reduced,
|
|
303
319
|
);
|
|
304
320
|
const module = analyzer.run({ ...options.programs, ...lowering.programs });
|
|
305
321
|
attachLowerings(module, root, { elaboration, promises, lowering });
|
|
306
|
-
return hasErrors(diagnostics)
|
|
322
|
+
return hasErrors(diagnostics)
|
|
323
|
+
? { diagnostics: [...diagnostics, ...lost] }
|
|
324
|
+
: { module, diagnostics };
|
|
307
325
|
}
|
|
308
326
|
|
|
309
327
|
/**
|
|
@@ -341,6 +359,102 @@ function storesGeneratedGlobal(
|
|
|
341
359
|
return Boolean(source && ts.isSourceFile(source) && generated(source));
|
|
342
360
|
}
|
|
343
361
|
|
|
362
|
+
/** TypeScript's errors for an import that names something its module doesn't export. */
|
|
363
|
+
const missingExportCodes: ReadonlySet<number> = new Set([
|
|
364
|
+
2305, 2459, 2460, 2614, 2724,
|
|
365
|
+
]);
|
|
366
|
+
|
|
367
|
+
/** The file an import or re-export's module specifier resolves to, if it resolves to a source file. */
|
|
368
|
+
function importedFile(
|
|
369
|
+
checker: ts.TypeChecker,
|
|
370
|
+
statement: ts.Statement,
|
|
371
|
+
): string | undefined {
|
|
372
|
+
if (
|
|
373
|
+
!(ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement)) ||
|
|
374
|
+
!statement.moduleSpecifier
|
|
375
|
+
) {
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
const module = checker.getSymbolAtLocation(
|
|
379
|
+
statement.moduleSpecifier,
|
|
380
|
+
)?.valueDeclaration;
|
|
381
|
+
return module && ts.isSourceFile(module) ? module.fileName : undefined;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* Imports a lowering pass broke in files tsPLC doesn't compile, such as a server's router: the
|
|
386
|
+
* pass rewrote a module and dropped a name the file still imports. Only PLC files' TypeScript
|
|
387
|
+
* errors are reported, so without this a server schema reads the lost name as nothing, and the
|
|
388
|
+
* error surfaces far from its cause. The server itself runs the module as written, so a lost name
|
|
389
|
+
* matters only where tsPLC reads that file's types: these are warnings, shown beside the errors
|
|
390
|
+
* they may explain.
|
|
391
|
+
*/
|
|
392
|
+
function lostImports({
|
|
393
|
+
before,
|
|
394
|
+
after,
|
|
395
|
+
rewritten,
|
|
396
|
+
plc,
|
|
397
|
+
root,
|
|
398
|
+
positions,
|
|
399
|
+
}: {
|
|
400
|
+
before: ts.Program;
|
|
401
|
+
after: ts.Program;
|
|
402
|
+
rewritten: ReadonlyMap<string, Mapped>;
|
|
403
|
+
/** The PLC program's files before the pass and after it, whose errors are reported anyway. */
|
|
404
|
+
plc: ReadonlySet<string>;
|
|
405
|
+
root: string;
|
|
406
|
+
positions: Positions;
|
|
407
|
+
}): Diagnostic[] {
|
|
408
|
+
const checker = after.getTypeChecker();
|
|
409
|
+
return after.getSourceFiles().flatMap((file) => {
|
|
410
|
+
const imports = file.statements.flatMap((statement) => {
|
|
411
|
+
const imported = importedFile(checker, statement);
|
|
412
|
+
return imported && rewritten.has(imported)
|
|
413
|
+
? [{ statement, imported }]
|
|
414
|
+
: [];
|
|
415
|
+
});
|
|
416
|
+
const original = before.getSourceFile(file.fileName);
|
|
417
|
+
if (
|
|
418
|
+
!imports.length ||
|
|
419
|
+
!original ||
|
|
420
|
+
file.isDeclarationFile ||
|
|
421
|
+
plc.has(file.fileName) ||
|
|
422
|
+
rewritten.has(file.fileName)
|
|
423
|
+
) {
|
|
424
|
+
return [];
|
|
425
|
+
}
|
|
426
|
+
const missing = (program: ts.Program, source: ts.SourceFile) =>
|
|
427
|
+
program
|
|
428
|
+
.getSemanticDiagnostics(source)
|
|
429
|
+
.filter(
|
|
430
|
+
(diagnostic) =>
|
|
431
|
+
missingExportCodes.has(diagnostic.code) &&
|
|
432
|
+
diagnostic.start !== undefined,
|
|
433
|
+
);
|
|
434
|
+
const known = new Set(
|
|
435
|
+
missing(before, original).map(({ start, code }) => `${start}:${code}`),
|
|
436
|
+
);
|
|
437
|
+
return missing(after, file).flatMap(({ start = 0, length = 1, code }) => {
|
|
438
|
+
const broken = imports.find(
|
|
439
|
+
({ statement }) => statement.pos <= start && start < statement.end,
|
|
440
|
+
);
|
|
441
|
+
if (!broken || known.has(`${start}:${code}`)) {
|
|
442
|
+
return [];
|
|
443
|
+
}
|
|
444
|
+
const name = file.text.slice(start, start + length);
|
|
445
|
+
return [
|
|
446
|
+
{
|
|
447
|
+
origin: "plc" as const,
|
|
448
|
+
code: "PLC1016",
|
|
449
|
+
severity: "warning" as const,
|
|
450
|
+
message: `tsPLC dropped ${name} from ${relative(root, broken.imported)} while compiling the PLC program, but ${relative(root, file.fileName)} imports it, so tsPLC reads that file's types without it. If an error here is in what that file declares, this is its cause, and a tsPLC bug.`,
|
|
451
|
+
span: positions.span(file, start, start + length),
|
|
452
|
+
},
|
|
453
|
+
];
|
|
454
|
+
});
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
|
|
344
458
|
/** TS2540: "Cannot assign to '…' because it is a read-only property." */
|
|
345
459
|
const readOnlyAssignment = 2540;
|
|
346
460
|
|