@tsplc/compiler 0.2.17 → 0.2.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/cli.js +920 -943
- package/dist/cli.js.map +13 -10
- package/dist/codesys.js +3893 -3352
- package/dist/codesys.js.map +29 -20
- package/dist/elaborate-capture.js +33 -17
- package/dist/elaborate-capture.js.map +3 -3
- package/dist/index.js +8787 -6855
- package/dist/index.js.map +40 -20
- package/dist/ir.js +1575 -1072
- package/dist/ir.js.map +21 -17
- package/dist/math.js.map +2 -2
- package/dist/project.js +154 -60
- package/dist/project.js.map +5 -5
- package/dist/telemetry.js +27 -9
- package/dist/telemetry.js.map +5 -5
- package/package.json +112 -112
- package/src/codesys/config.ts +50 -0
- package/src/codesys/debug.ts +670 -0
- package/src/codesys/emit.ts +142 -0
- package/src/codesys/engineering.ts +109 -0
- package/src/codesys/expressions.ts +92 -0
- package/src/codesys/global-lists.ts +250 -0
- package/src/codesys/global-names.ts +68 -0
- package/src/codesys/http.ts +890 -0
- package/src/codesys/index.ts +30 -0
- package/src/codesys/instrumentation.ts +430 -0
- package/src/codesys/io-paths.ts +140 -0
- package/src/codesys/manifest.ts +643 -0
- package/src/codesys/names.ts +81 -0
- package/src/codesys/numeric.ts +409 -0
- package/src/codesys/operands.ts +62 -0
- package/src/codesys/plan.ts +605 -0
- package/src/codesys/programs.ts +53 -0
- package/src/codesys/publication.ts +171 -0
- package/src/codesys/routines.ts +603 -0
- package/src/codesys/sources.ts +71 -0
- package/src/codesys/st-files.ts +115 -0
- package/src/codesys/storage.ts +97 -0
- package/src/codesys/symbols.ts +220 -0
- package/src/codesys/task-access.ts +320 -0
- package/src/codesys/telemetry.ts +732 -0
- package/src/codesys/types.ts +250 -0
- package/src/codesys/vendor.ts +150 -0
- package/src/frontend/analyzer.ts +794 -0
- package/src/frontend/async.ts +317 -1137
- package/src/frontend/blocks.ts +537 -403
- package/src/frontend/configuration.ts +176 -144
- package/src/frontend/const-functions.ts +183 -148
- package/src/frontend/cooperative.ts +73 -43
- package/src/frontend/coroutines.ts +1032 -0
- package/src/frontend/elaborate/calls.ts +535 -0
- package/src/frontend/elaborate/capture.ts +157 -101
- package/src/frontend/elaborate/dialect.ts +257 -0
- package/src/frontend/elaborate/expressions.ts +992 -0
- package/src/frontend/elaborate/frame.ts +53 -0
- package/src/frontend/elaborate/graph.ts +116 -71
- package/src/frontend/elaborate/imports.ts +70 -0
- package/src/frontend/elaborate/index.ts +435 -331
- package/src/frontend/elaborate/initializers.ts +198 -0
- package/src/frontend/elaborate/layouts.ts +591 -384
- package/src/frontend/elaborate/model.ts +264 -169
- package/src/frontend/elaborate/specialize.ts +343 -2586
- package/src/frontend/elaborate/statements.ts +606 -0
- package/src/frontend/elaborate/storage.ts +546 -0
- package/src/frontend/elaborate/values.ts +58 -0
- package/src/frontend/elaborate/writes.ts +100 -0
- package/src/frontend/exposure.ts +172 -140
- package/src/frontend/expressions.ts +1383 -0
- package/src/frontend/intrinsics.ts +337 -0
- package/src/frontend/module-graph.ts +89 -59
- package/src/frontend/positions.ts +271 -200
- package/src/frontend/promise-chains.ts +897 -0
- package/src/frontend/promises.ts +390 -747
- package/src/frontend/routines.ts +181 -0
- package/src/frontend/sdk.ts +82 -0
- package/src/frontend/services.ts +506 -372
- package/src/frontend/statements.ts +855 -0
- package/src/frontend/tasks.ts +471 -0
- package/src/frontend/telemetry.ts +174 -0
- package/src/frontend/types.ts +845 -551
- package/src/frontend/typescript.ts +323 -2543
- package/src/index.ts +5 -5
- package/src/ir/aggregates.ts +275 -0
- package/src/ir/allocation.ts +84 -0
- package/src/ir/core-model.ts +146 -0
- package/src/ir/declarations.ts +295 -0
- package/src/ir/diagnostics.ts +61 -0
- package/src/ir/domains.ts +152 -0
- package/src/ir/fold.ts +342 -0
- package/src/ir/index.ts +90 -0
- package/{ir/errors.ts → src/ir/job-errors.ts} +12 -10
- package/src/ir/layouts.ts +181 -0
- package/src/ir/link.ts +354 -0
- package/src/ir/model.ts +386 -0
- package/{ir → src/ir}/normalization-error.ts +4 -4
- package/src/ir/normalize.ts +70 -0
- package/src/ir/numeric.ts +171 -0
- package/src/ir/ranges.ts +256 -0
- package/src/ir/routine.ts +1163 -0
- package/src/ir/scalars.ts +321 -0
- package/src/ir/types.ts +127 -0
- package/src/project/artifacts.ts +342 -0
- package/src/project/cli.ts +177 -0
- package/src/project/debugger.ts +82 -0
- package/src/project/index.ts +6 -0
- package/src/project/plant.ts +467 -0
- package/src/project/project-commands.ts +372 -0
- package/src/project/project.ts +457 -0
- package/src/project/release.ts +153 -0
- package/src/project/server.ts +150 -0
- package/src/project/terminal.ts +32 -0
- package/stdlib/math.ts +14 -10
- package/stdlib/telemetry.ts +71 -47
- package/types/async.d.ts +46 -40
- package/types/data-types.d.ts +26 -26
- package/types/globals.d.ts +41 -38
- package/types/index.d.ts +96 -51
- package/types/plc.d.ts +3 -4
- package/types/telemetry.d.ts +14 -14
- package/backends/codesys/config.ts +0 -54
- package/backends/codesys/debug.ts +0 -509
- package/backends/codesys/emit.ts +0 -1738
- package/backends/codesys/engineering.ts +0 -88
- package/backends/codesys/global-names.ts +0 -49
- package/backends/codesys/http.ts +0 -675
- package/backends/codesys/index.ts +0 -13
- package/backends/codesys/io-paths.ts +0 -105
- package/backends/codesys/manifest.ts +0 -312
- package/backends/codesys/names.ts +0 -76
- package/backends/codesys/numeric.ts +0 -356
- package/backends/codesys/plan.ts +0 -457
- package/backends/codesys/publication.ts +0 -59
- package/backends/codesys/storage.ts +0 -70
- package/backends/codesys/task-access.ts +0 -293
- package/backends/codesys/telemetry.ts +0 -593
- package/backends/codesys/types.ts +0 -179
- package/backends/codesys/vendor.ts +0 -108
- package/ir/aggregates.ts +0 -333
- package/ir/allocation.ts +0 -74
- package/ir/core-model.ts +0 -129
- package/ir/core.ts +0 -64
- package/ir/declarations.ts +0 -208
- package/ir/diagnostics.ts +0 -43
- package/ir/domains.ts +0 -96
- package/ir/index.ts +0 -8
- package/ir/link.ts +0 -185
- package/ir/model.ts +0 -346
- package/ir/routine.ts +0 -859
- package/ir/scalars.ts +0 -675
- package/ir/types.ts +0 -76
- package/project/artifacts.ts +0 -277
- package/project/cli.ts +0 -395
- package/project/index.ts +0 -1
- package/project/plant.ts +0 -509
- package/project/project.ts +0 -358
- package/project/server.ts +0 -113
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
|
-
import { dirname, join,
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
3
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
error,
|
|
5
|
+
type Constant,
|
|
6
|
+
type Diagnostic,
|
|
7
|
+
type SourceSpan,
|
|
8
|
+
type TypeId,
|
|
9
9
|
} from "@tsplc/compiler/ir";
|
|
10
10
|
import { runtimeSources } from "../module-graph.ts";
|
|
11
|
-
import {
|
|
11
|
+
import { inSdk, namesSdk } from "../sdk.ts";
|
|
12
|
+
import {
|
|
13
|
+
copied,
|
|
14
|
+
from,
|
|
15
|
+
joined,
|
|
16
|
+
type Mapped,
|
|
17
|
+
type Positions,
|
|
18
|
+
} from "../positions.ts";
|
|
12
19
|
import { captureGraph, type CapturedValue } from "./graph.ts";
|
|
20
|
+
import { importPath } from "./imports.ts";
|
|
13
21
|
import { Model, type Root } from "./model.ts";
|
|
14
|
-
import { closingBrace,
|
|
22
|
+
import { closingBrace, Specializer } from "./specialize.ts";
|
|
23
|
+
import { setterName } from "./storage.ts";
|
|
24
|
+
import { SpecializeError } from "./values.ts";
|
|
15
25
|
|
|
16
26
|
/** Directory of the modules elaboration generates, beside the entry module. */
|
|
17
27
|
const generatedDirectory = ".tsplc";
|
|
@@ -20,101 +30,138 @@ const generatedDirectory = ".tsplc";
|
|
|
20
30
|
* A module the compiled program keeps as written: only types, enums, constants and the service
|
|
21
31
|
* client. Everything else is plain TypeScript that elaboration compiles from its setup graph.
|
|
22
32
|
*/
|
|
23
|
-
const keptModule = (
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
const keptModule = (
|
|
34
|
+
source: ts.SourceFile,
|
|
35
|
+
checker: ts.TypeChecker,
|
|
36
|
+
sdk: string,
|
|
37
|
+
): boolean =>
|
|
38
|
+
source.statements.every((statement) => {
|
|
39
|
+
if (
|
|
40
|
+
ts.isImportDeclaration(statement) ||
|
|
41
|
+
ts.isExportDeclaration(statement) ||
|
|
42
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
43
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
44
|
+
ts.isEnumDeclaration(statement)
|
|
45
|
+
) {
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
if (!ts.isVariableStatement(statement)) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return statement.declarationList.declarations.every(
|
|
52
|
+
(declaration) =>
|
|
53
|
+
declaration.initializer &&
|
|
54
|
+
constantInitializer(declaration.initializer, checker, sdk),
|
|
55
|
+
);
|
|
56
|
+
});
|
|
38
57
|
|
|
39
|
-
const constantInitializer = (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
58
|
+
const constantInitializer = (
|
|
59
|
+
node: ts.Expression,
|
|
60
|
+
checker: ts.TypeChecker,
|
|
61
|
+
sdk: string,
|
|
62
|
+
): boolean => {
|
|
63
|
+
if (
|
|
64
|
+
ts.isNumericLiteral(node) ||
|
|
65
|
+
ts.isStringLiteral(node) ||
|
|
66
|
+
ts.isNoSubstitutionTemplateLiteral(node) ||
|
|
67
|
+
ts.isBigIntLiteral(node) ||
|
|
68
|
+
node.kind === ts.SyntaxKind.TrueKeyword ||
|
|
69
|
+
node.kind === ts.SyntaxKind.FalseKeyword
|
|
70
|
+
) {
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
if (ts.isPrefixUnaryExpression(node)) {
|
|
74
|
+
return constantInitializer(node.operand, checker, sdk);
|
|
75
|
+
}
|
|
76
|
+
if (
|
|
77
|
+
ts.isAsExpression(node) ||
|
|
78
|
+
ts.isSatisfiesExpression(node) ||
|
|
79
|
+
ts.isParenthesizedExpression(node)
|
|
80
|
+
) {
|
|
81
|
+
return constantInitializer(node.expression, checker, sdk);
|
|
82
|
+
}
|
|
83
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
84
|
+
return ts.isIdentifier(node.expression);
|
|
85
|
+
}
|
|
86
|
+
if (ts.isIdentifier(node)) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
90
|
+
return node.properties.every(
|
|
91
|
+
(property) =>
|
|
92
|
+
(ts.isPropertyAssignment(property) &&
|
|
93
|
+
constantInitializer(property.initializer, checker, sdk)) ||
|
|
94
|
+
ts.isShorthandPropertyAssignment(property),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
if (ts.isArrayLiteralExpression(node)) {
|
|
98
|
+
return node.elements.every((element) =>
|
|
99
|
+
constantInitializer(element, checker, sdk),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
if (ts.isCallExpression(node)) {
|
|
103
|
+
return namesSdk(checker, sdk, node.expression, "createServiceClient");
|
|
104
|
+
}
|
|
105
|
+
return false;
|
|
69
106
|
};
|
|
70
107
|
|
|
71
108
|
/** Whether the program is plain TypeScript: classes with constructors, or setup code building state. */
|
|
72
|
-
const needsElaboration = (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
.replace(/\\/g, "/")
|
|
90
|
-
.replace(/\.tsx?$/, "");
|
|
91
|
-
return path.startsWith("./") || path.startsWith("../") ? path : `./${path}`;
|
|
92
|
-
};
|
|
109
|
+
const needsElaboration = (
|
|
110
|
+
sources: readonly ts.SourceFile[],
|
|
111
|
+
kept: (file: string) => boolean,
|
|
112
|
+
): boolean =>
|
|
113
|
+
sources.some(
|
|
114
|
+
(source) =>
|
|
115
|
+
!kept(source.fileName) &&
|
|
116
|
+
source.statements.some(
|
|
117
|
+
(statement) =>
|
|
118
|
+
ts.isClassDeclaration(statement) &&
|
|
119
|
+
!statement.heritageClauses?.some((clause) =>
|
|
120
|
+
clause.types.some((type) =>
|
|
121
|
+
type.expression.getText().startsWith("FunctionBlock"),
|
|
122
|
+
),
|
|
123
|
+
),
|
|
124
|
+
),
|
|
125
|
+
);
|
|
93
126
|
|
|
94
|
-
|
|
127
|
+
interface Folded {
|
|
128
|
+
path: string;
|
|
129
|
+
value: Constant;
|
|
130
|
+
type?: TypeId;
|
|
131
|
+
span: SourceSpan;
|
|
132
|
+
}
|
|
95
133
|
|
|
96
134
|
/**
|
|
97
135
|
* A folded primitive as a PLC constant, with its scalar type: the IEC width its declaration
|
|
98
136
|
* names, or the one a TypeScript number of that value gets. Strings, often enum members, have none.
|
|
99
137
|
*/
|
|
100
138
|
const constant = (
|
|
101
|
-
|
|
102
|
-
|
|
139
|
+
value: CapturedValue,
|
|
140
|
+
iec: string | undefined,
|
|
103
141
|
): Pick<Folded, "value" | "type"> | undefined => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
142
|
+
switch (value.kind) {
|
|
143
|
+
case "number":
|
|
144
|
+
return Number.isInteger(value.value)
|
|
145
|
+
? {
|
|
146
|
+
value: { kind: "integer", value: String(value.value) },
|
|
147
|
+
type: iec ?? "DINT",
|
|
148
|
+
}
|
|
149
|
+
: {
|
|
150
|
+
value: { kind: "float", value: value.value },
|
|
151
|
+
type: iec ?? "LREAL",
|
|
152
|
+
};
|
|
153
|
+
case "bigint":
|
|
154
|
+
return {
|
|
155
|
+
value: { kind: "integer", value: value.value },
|
|
156
|
+
type: iec ?? "LINT",
|
|
157
|
+
};
|
|
158
|
+
case "boolean":
|
|
159
|
+
return { value: { kind: "bool", value: value.value }, type: "BOOL" };
|
|
160
|
+
case "string":
|
|
161
|
+
return { value: { kind: "string", value: value.value } };
|
|
162
|
+
default:
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
118
165
|
};
|
|
119
166
|
|
|
120
167
|
/**
|
|
@@ -122,255 +169,312 @@ const constant = (
|
|
|
122
169
|
* the objects setup built. The result is tsPLC dialect source the rest of the compiler reads.
|
|
123
170
|
*/
|
|
124
171
|
export const lowerElaboration = (
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
172
|
+
program: ts.Program,
|
|
173
|
+
sdk: string,
|
|
174
|
+
entries: Readonly<Record<string, { module: string; export: string }>>,
|
|
175
|
+
root: string,
|
|
176
|
+
positions: Positions,
|
|
130
177
|
): {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
178
|
+
sources: Map<string, Mapped>;
|
|
179
|
+
diagnostics: Diagnostic[];
|
|
180
|
+
/** Routines specialized from class methods, by `<entry file>:<routine>`. */
|
|
181
|
+
origins: Map<string, { name: string; instance?: string }>;
|
|
182
|
+
folded: Folded[];
|
|
183
|
+
/** The string each member of a literal union's enum stands for, by `<types file>:<enum>`. */
|
|
184
|
+
literals: Map<string, Record<string, string>>;
|
|
138
185
|
} => {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
186
|
+
const sources = new Map<string, Mapped>();
|
|
187
|
+
const diagnostics: Diagnostic[] = [];
|
|
188
|
+
const origins = new Map<string, { name: string; instance?: string }>();
|
|
189
|
+
const folded: Folded[] = [];
|
|
190
|
+
const literals = new Map<string, Record<string, string>>();
|
|
191
|
+
const result = { sources, diagnostics, origins, folded, literals };
|
|
192
|
+
const runtime = runtimeSources(program).filter(
|
|
193
|
+
(source) => !source.isDeclarationFile && !inSdk(sdk, source.fileName),
|
|
194
|
+
);
|
|
195
|
+
const checker = program.getTypeChecker();
|
|
196
|
+
const kept = new Set(
|
|
197
|
+
runtime
|
|
198
|
+
.filter((source) => keptModule(source, checker, sdk))
|
|
199
|
+
.map((source) => source.fileName),
|
|
200
|
+
);
|
|
201
|
+
const isKept = (file: string) => kept.has(file);
|
|
202
|
+
if (!needsElaboration(runtime, isKept)) {
|
|
203
|
+
return result;
|
|
204
|
+
}
|
|
205
|
+
const [entry, ...otherEntries] = Object.values(entries);
|
|
206
|
+
if (!entry || otherEntries.length) {
|
|
207
|
+
diagnostics.push(
|
|
208
|
+
error("PLC1001", "A plain TypeScript program has one entry function."),
|
|
209
|
+
);
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
const entryFile = resolve(root, entry.module).replace(/\\/g, "/");
|
|
213
|
+
const entrySource = program.getSourceFile(entryFile);
|
|
214
|
+
if (!entrySource) {
|
|
215
|
+
diagnostics.push(
|
|
216
|
+
error("PLC1001", `${entry.module} is not part of the program.`),
|
|
217
|
+
);
|
|
218
|
+
return result;
|
|
219
|
+
}
|
|
220
|
+
const directory = join(dirname(entryFile), generatedDirectory).replace(
|
|
221
|
+
/\\/g,
|
|
222
|
+
"/",
|
|
223
|
+
);
|
|
224
|
+
const rootModule = (item: Root) => `${directory}/${item.name}.ts`;
|
|
168
225
|
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
226
|
+
try {
|
|
227
|
+
const graph = captureGraph(entryFile, runtime);
|
|
228
|
+
const model = new Model(graph, checker, program, entryFile);
|
|
229
|
+
const specializer = new Specializer(
|
|
230
|
+
model,
|
|
231
|
+
checker,
|
|
232
|
+
sdk,
|
|
233
|
+
runtime,
|
|
234
|
+
isKept,
|
|
235
|
+
rootModule,
|
|
236
|
+
);
|
|
237
|
+
const main = findEntry(entrySource, entry.export);
|
|
238
|
+
const body = specializer.entry(main);
|
|
175
239
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
240
|
+
// Exported data the hardware, HMI and server address is stored whether or not code reads it.
|
|
241
|
+
for (const item of model.roots) {
|
|
242
|
+
if (item.exported && !isKept(item.file) && item.file === entryFile) {
|
|
243
|
+
specializer.storage.use(item);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
179
246
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
247
|
+
// Writing the globals declares the types they use, so they come before the types module.
|
|
248
|
+
const globals = new Map<Root, Mapped[]>();
|
|
249
|
+
for (const item of specializer.storage.roots) {
|
|
250
|
+
globals.set(item, storageGlobals(specializer, item));
|
|
251
|
+
}
|
|
252
|
+
const typesFile = `${directory}/types.ts`;
|
|
253
|
+
const typeImports = (file: string) => [
|
|
254
|
+
...specializer.layouts.imports.lines(file, "import"),
|
|
255
|
+
...specializer.layouts.typeImports.lines(file, "import type"),
|
|
256
|
+
];
|
|
257
|
+
const enums = specializer.layouts.enumNames;
|
|
258
|
+
const interfaces = [...specializer.layouts.declarations.keys()].filter(
|
|
259
|
+
(name) => !enums.includes(name),
|
|
260
|
+
);
|
|
261
|
+
const importTypes = (file: string) => [
|
|
262
|
+
...(enums.length
|
|
263
|
+
? [
|
|
264
|
+
`import { ${enums.join(", ")} } from "${importPath(file, typesFile)}";`,
|
|
265
|
+
]
|
|
266
|
+
: []),
|
|
267
|
+
...(interfaces.length
|
|
268
|
+
? [
|
|
269
|
+
`import type { ${interfaces.join(", ")} } from "${importPath(file, typesFile)}";`,
|
|
270
|
+
]
|
|
271
|
+
: []),
|
|
272
|
+
];
|
|
273
|
+
sources.set(
|
|
274
|
+
typesFile,
|
|
275
|
+
joined(
|
|
276
|
+
[
|
|
277
|
+
...typeImports(typesFile),
|
|
278
|
+
...specializer.layouts.declarations.values(),
|
|
279
|
+
],
|
|
280
|
+
"\n",
|
|
281
|
+
),
|
|
282
|
+
);
|
|
283
|
+
for (const [item, lines] of globals) {
|
|
284
|
+
const file = rootModule(item);
|
|
285
|
+
const imports = [...typeImports(file), ...importTypes(file)];
|
|
286
|
+
sources.set(
|
|
287
|
+
file,
|
|
288
|
+
joined(
|
|
289
|
+
[...imports.map((line) => from(item.declaration, line)), ...lines],
|
|
290
|
+
"\n",
|
|
291
|
+
),
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const header = [
|
|
296
|
+
...typeImports(entryFile),
|
|
297
|
+
...importTypes(entryFile),
|
|
298
|
+
...specializer.imports.lines(entryFile),
|
|
299
|
+
];
|
|
300
|
+
sources.set(
|
|
301
|
+
entryFile,
|
|
302
|
+
joined(
|
|
303
|
+
[
|
|
304
|
+
...header.map((line) => from(main, line)),
|
|
305
|
+
...specializer.expressions.tableDeclarations,
|
|
306
|
+
...specializer.routines,
|
|
307
|
+
from(main, `export function ${entry.export}(): void {`),
|
|
308
|
+
...body,
|
|
309
|
+
from(closingBrace(main), "}"),
|
|
310
|
+
],
|
|
311
|
+
"\n",
|
|
312
|
+
),
|
|
313
|
+
);
|
|
314
|
+
for (const [name, origin] of specializer.origins) {
|
|
315
|
+
origins.set(`${entryFile}:${name}`, origin);
|
|
316
|
+
}
|
|
317
|
+
for (const { name, members } of specializer.layouts.literalEnums) {
|
|
318
|
+
literals.set(`${typesFile}:${name}`, members);
|
|
319
|
+
}
|
|
320
|
+
for (const item of specializer.storage.folded()) {
|
|
321
|
+
const known = constant(item.value, item.iec);
|
|
322
|
+
if (known) {
|
|
323
|
+
folded.push({
|
|
324
|
+
path: item.path,
|
|
325
|
+
...known,
|
|
326
|
+
span: positions.node(item.declaration),
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
// The plain modules now live in the generated code; keep only their types for type importers.
|
|
331
|
+
for (const source of runtime) {
|
|
332
|
+
if (source.fileName !== entryFile && !isKept(source.fileName)) {
|
|
333
|
+
sources.set(source.fileName, typesOnly(source));
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
} catch (cause) {
|
|
337
|
+
diagnostics.push(failure(cause, positions));
|
|
338
|
+
}
|
|
339
|
+
return result;
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* The globals of a root's storage module: one per stored field of a record or instance, or the
|
|
344
|
+
* root itself. Other modules can't assign an imported binding, so each global comes with a setter.
|
|
345
|
+
*/
|
|
346
|
+
const storageGlobals = (specializer: Specializer, item: Root): Mapped[] => {
|
|
347
|
+
const { model, checker, layouts, storage, initializers } = specializer;
|
|
348
|
+
const lines: Mapped[] = [];
|
|
349
|
+
const global = (
|
|
350
|
+
name: string,
|
|
351
|
+
layout: string,
|
|
352
|
+
initial: string,
|
|
353
|
+
declaration: ts.Node | undefined,
|
|
354
|
+
) => {
|
|
355
|
+
const origin = declaration ?? item.declaration;
|
|
356
|
+
lines.push(
|
|
357
|
+
from(origin, `export let ${name}: ${layout} = ${initial};`),
|
|
358
|
+
from(
|
|
359
|
+
origin,
|
|
360
|
+
`export function ${setterName(name)}(value: ${layout}): void { ${name} = value; }`,
|
|
361
|
+
),
|
|
362
|
+
);
|
|
363
|
+
};
|
|
364
|
+
const node = model.node(item.value);
|
|
365
|
+
if (!node || !storage.flattened(item)) {
|
|
366
|
+
const declared = checker.getTypeAtLocation(item.declaration.name);
|
|
367
|
+
const layout = layouts.declaredText(item.declaration, declared, item.value);
|
|
368
|
+
global(
|
|
369
|
+
item.name,
|
|
370
|
+
layout,
|
|
371
|
+
initializers.valueText(item.value, declared, layout),
|
|
372
|
+
item.declaration,
|
|
373
|
+
);
|
|
374
|
+
return lines;
|
|
375
|
+
}
|
|
376
|
+
const captured = node.captured;
|
|
377
|
+
if (captured.kind === "instance") {
|
|
378
|
+
const declaration = model.classOf(node);
|
|
379
|
+
for (const [name, layout] of storage.instanceFields(node)) {
|
|
380
|
+
global(
|
|
381
|
+
name,
|
|
382
|
+
layout,
|
|
383
|
+
initializers.instanceFieldInitializer(node, name, layout),
|
|
384
|
+
model.member(declaration, name),
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
} else if (captured.kind === "object") {
|
|
388
|
+
for (const [name, field] of Object.entries(captured.fields)) {
|
|
389
|
+
const inner = model.node(field);
|
|
390
|
+
const declared = model.capturedFieldType(node, name);
|
|
391
|
+
if (!declared) {
|
|
392
|
+
continue;
|
|
393
|
+
}
|
|
394
|
+
const stored = item.exported || storage.stored(node, name);
|
|
395
|
+
if (!stored && !inner) {
|
|
396
|
+
continue;
|
|
397
|
+
}
|
|
398
|
+
const property = node.type?.getProperty(name)?.valueDeclaration;
|
|
399
|
+
const layout =
|
|
400
|
+
inner && !item.exported
|
|
401
|
+
? storage.storageType(inner)
|
|
402
|
+
: layouts.declaredText(
|
|
403
|
+
property,
|
|
404
|
+
declared,
|
|
405
|
+
field,
|
|
406
|
+
name.charAt(0).toUpperCase() + name.slice(1),
|
|
407
|
+
);
|
|
408
|
+
if (!layout) {
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
global(
|
|
412
|
+
name,
|
|
413
|
+
layout,
|
|
414
|
+
initializers.valueText(field, declared, layout),
|
|
415
|
+
property,
|
|
416
|
+
);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
return lines;
|
|
420
|
+
};
|
|
243
421
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
: []),
|
|
262
|
-
...(interfaces.length
|
|
263
|
-
? [`import type { ${interfaces.join(", ")} } from "${importPath(file, typesFile)}";`]
|
|
264
|
-
: []),
|
|
265
|
-
];
|
|
266
|
-
sources.set(
|
|
267
|
-
typesFile,
|
|
268
|
-
joined([...typeImports(typesFile), ...specializer.layouts.declarations.values()], "\n"),
|
|
269
|
-
);
|
|
270
|
-
for (const [item, lines] of storage) {
|
|
271
|
-
const file = rootModule(item);
|
|
272
|
-
const imports = [...typeImports(file), ...importTypes(file)];
|
|
273
|
-
sources.set(
|
|
274
|
-
file,
|
|
275
|
-
joined([...imports.map((line) => from(item.declaration, line)), ...lines], "\n"),
|
|
276
|
-
);
|
|
277
|
-
}
|
|
422
|
+
/** A plain module reduced to the types other modules may import from it. */
|
|
423
|
+
const typesOnly = (source: ts.SourceFile): Mapped =>
|
|
424
|
+
joined(
|
|
425
|
+
[
|
|
426
|
+
...source.statements
|
|
427
|
+
.filter(
|
|
428
|
+
(statement) =>
|
|
429
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
430
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
431
|
+
(ts.isImportDeclaration(statement) &&
|
|
432
|
+
statement.importClause?.isTypeOnly),
|
|
433
|
+
)
|
|
434
|
+
.map(copied),
|
|
435
|
+
"export {};",
|
|
436
|
+
],
|
|
437
|
+
"\n",
|
|
438
|
+
);
|
|
278
439
|
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
: []),
|
|
292
|
-
...(specializer.telemetryImport
|
|
293
|
-
? [`import { telemetry } from "@tsplc/compiler/telemetry";`]
|
|
294
|
-
: []),
|
|
295
|
-
...(specializer.typeImports.size
|
|
296
|
-
? [`import { ${[...specializer.typeImports].join(", ")} } from "@tsplc/compiler/types";`]
|
|
297
|
-
: []),
|
|
298
|
-
];
|
|
299
|
-
sources.set(
|
|
300
|
-
entryFile,
|
|
301
|
-
joined(
|
|
302
|
-
[
|
|
303
|
-
...header.map((line) => from(main, line)),
|
|
304
|
-
...specializer.tableDeclarations,
|
|
305
|
-
...specializer.routines,
|
|
306
|
-
from(main, `export function ${entry.export}(): void {`),
|
|
307
|
-
...body,
|
|
308
|
-
from(closingBrace(main), "}"),
|
|
309
|
-
],
|
|
310
|
-
"\n",
|
|
311
|
-
),
|
|
312
|
-
);
|
|
313
|
-
for (const [name, origin] of specializer.origins) origins.set(`${entryFile}:${name}`, origin);
|
|
314
|
-
for (const { name, members } of specializer.layouts.literalEnums)
|
|
315
|
-
literals.set(`${typesFile}:${name}`, members);
|
|
316
|
-
for (const item of specializer.folded()) {
|
|
317
|
-
const known = constant(item.value, item.iec);
|
|
318
|
-
if (known) folded.push({ path: item.path, ...known, span: positions.node(item.declaration) });
|
|
319
|
-
}
|
|
320
|
-
// The plain modules now live in the generated code; keep only their types for type importers.
|
|
321
|
-
for (const source of runtime)
|
|
322
|
-
if (source.fileName !== entryFile && !isKept(source.fileName))
|
|
323
|
-
sources.set(
|
|
324
|
-
source.fileName,
|
|
325
|
-
joined(
|
|
326
|
-
[
|
|
327
|
-
...source.statements
|
|
328
|
-
.filter(
|
|
329
|
-
(statement) =>
|
|
330
|
-
ts.isInterfaceDeclaration(statement) ||
|
|
331
|
-
ts.isTypeAliasDeclaration(statement) ||
|
|
332
|
-
(ts.isImportDeclaration(statement) && statement.importClause?.isTypeOnly),
|
|
333
|
-
)
|
|
334
|
-
.map(copied),
|
|
335
|
-
"export {};",
|
|
336
|
-
],
|
|
337
|
-
"\n",
|
|
338
|
-
),
|
|
339
|
-
);
|
|
340
|
-
} catch (cause) {
|
|
341
|
-
if (cause instanceof SpecializeError)
|
|
342
|
-
diagnostics.push(error("PLC1010", cause.message, positions.node(cause.node)));
|
|
343
|
-
else
|
|
344
|
-
diagnostics.push(
|
|
345
|
-
error(
|
|
346
|
-
"PLC1010",
|
|
347
|
-
cause instanceof Error
|
|
348
|
-
? process.env.TSPLC_DEBUG
|
|
349
|
-
? String(cause.stack)
|
|
350
|
-
: cause.message
|
|
351
|
-
: String(cause),
|
|
352
|
-
),
|
|
353
|
-
);
|
|
354
|
-
}
|
|
355
|
-
return result;
|
|
440
|
+
/** An elaboration failure as a diagnostic, at the node specialization stopped at when it knows one. */
|
|
441
|
+
const failure = (cause: unknown, positions: Positions): Diagnostic => {
|
|
442
|
+
if (cause instanceof SpecializeError) {
|
|
443
|
+
return error("PLC1010", cause.message, positions.node(cause.node));
|
|
444
|
+
}
|
|
445
|
+
if (!(cause instanceof Error)) {
|
|
446
|
+
return error("PLC1010", String(cause));
|
|
447
|
+
}
|
|
448
|
+
return error(
|
|
449
|
+
"PLC1010",
|
|
450
|
+
process.env.TSPLC_DEBUG ? String(cause.stack) : cause.message,
|
|
451
|
+
);
|
|
356
452
|
};
|
|
357
453
|
|
|
358
454
|
const findEntry = (
|
|
359
|
-
|
|
360
|
-
|
|
455
|
+
source: ts.SourceFile,
|
|
456
|
+
name: string,
|
|
361
457
|
): ts.FunctionDeclaration | ts.ArrowFunction => {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
458
|
+
for (const statement of source.statements) {
|
|
459
|
+
if (
|
|
460
|
+
ts.isFunctionDeclaration(statement) &&
|
|
461
|
+
statement.name?.text === name &&
|
|
462
|
+
statement.body
|
|
463
|
+
) {
|
|
464
|
+
return statement;
|
|
465
|
+
}
|
|
466
|
+
if (ts.isVariableStatement(statement)) {
|
|
467
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
468
|
+
if (
|
|
469
|
+
ts.isIdentifier(declaration.name) &&
|
|
470
|
+
declaration.name.text === name &&
|
|
471
|
+
declaration.initializer &&
|
|
472
|
+
ts.isArrowFunction(declaration.initializer)
|
|
473
|
+
) {
|
|
474
|
+
return declaration.initializer;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
throw new Error(`${source.fileName} exports no function ${name}.`);
|
|
376
480
|
};
|