@tsplc/compiler 0.2.17 → 0.2.19
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
|
@@ -0,0 +1,794 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
error,
|
|
5
|
+
aggregate,
|
|
6
|
+
type Diagnostic,
|
|
7
|
+
type PlcFunction,
|
|
8
|
+
type PlcModule,
|
|
9
|
+
type PlcProgram,
|
|
10
|
+
type Variable,
|
|
11
|
+
} from "@tsplc/compiler/ir";
|
|
12
|
+
import { runtimeSources } from "./module-graph.ts";
|
|
13
|
+
import { inSdk, unaliased } from "./sdk.ts";
|
|
14
|
+
import type { Positions } from "./positions.ts";
|
|
15
|
+
import { BlockResolver, storageInitializer } from "./blocks.ts";
|
|
16
|
+
import {
|
|
17
|
+
SemanticError,
|
|
18
|
+
TypeResolver,
|
|
19
|
+
hasModifier,
|
|
20
|
+
numericLiteral,
|
|
21
|
+
numericLiteralType,
|
|
22
|
+
type Fail,
|
|
23
|
+
type ResolvedType,
|
|
24
|
+
} from "./types.ts";
|
|
25
|
+
import { methodAccess, RoutineTable } from "./routines.ts";
|
|
26
|
+
import {
|
|
27
|
+
ExpressionLowering,
|
|
28
|
+
type AnalysisContext,
|
|
29
|
+
type LoweringScope,
|
|
30
|
+
} from "./expressions.ts";
|
|
31
|
+
import { StatementLowering } from "./statements.ts";
|
|
32
|
+
import type { FrontendOptions } from "./typescript.ts";
|
|
33
|
+
|
|
34
|
+
/** What a module variable's initializer and keyword establish about its storage. */
|
|
35
|
+
interface StorageFacts {
|
|
36
|
+
instance: string | undefined;
|
|
37
|
+
table: ResolvedType | undefined;
|
|
38
|
+
immutable: boolean;
|
|
39
|
+
literal: string | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const suppressionDiagnostics = (
|
|
43
|
+
source: ts.SourceFile,
|
|
44
|
+
positions: Positions,
|
|
45
|
+
): Diagnostic[] => {
|
|
46
|
+
const diagnostics: Diagnostic[] = [];
|
|
47
|
+
const scanner = ts.createScanner(
|
|
48
|
+
ts.ScriptTarget.Latest,
|
|
49
|
+
false,
|
|
50
|
+
ts.LanguageVariant.Standard,
|
|
51
|
+
source.text,
|
|
52
|
+
);
|
|
53
|
+
for (
|
|
54
|
+
let token = scanner.scan();
|
|
55
|
+
token !== ts.SyntaxKind.EndOfFileToken;
|
|
56
|
+
token = scanner.scan()
|
|
57
|
+
) {
|
|
58
|
+
if (
|
|
59
|
+
token !== ts.SyntaxKind.SingleLineCommentTrivia &&
|
|
60
|
+
token !== ts.SyntaxKind.MultiLineCommentTrivia
|
|
61
|
+
) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
const match =
|
|
65
|
+
/(?<=(?:^|\n)\s*(?:\/\/+|\/\*+|\*)\s*)@ts-(?:nocheck|ignore|expect-error)\b/.exec(
|
|
66
|
+
scanner.getTokenText(),
|
|
67
|
+
);
|
|
68
|
+
if (!match) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const start = scanner.getTokenPos() + match.index;
|
|
72
|
+
diagnostics.push(
|
|
73
|
+
error(
|
|
74
|
+
"PLC1009",
|
|
75
|
+
"TypeScript diagnostic suppression is not allowed in PLC source.",
|
|
76
|
+
positions.span(source, start, start + match[0].length),
|
|
77
|
+
),
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
return diagnostics;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export class Analyzer {
|
|
84
|
+
private readonly checker: ts.TypeChecker;
|
|
85
|
+
private readonly variables = new Map<ts.Symbol, Variable>();
|
|
86
|
+
private readonly resolver: TypeResolver;
|
|
87
|
+
private readonly routines: RoutineTable;
|
|
88
|
+
private readonly blocks: BlockResolver;
|
|
89
|
+
private readonly globalVariables: Variable[] = [];
|
|
90
|
+
private readonly sources: readonly ts.SourceFile[];
|
|
91
|
+
private readonly context: AnalysisContext;
|
|
92
|
+
/** Lowers module and block initializers, which run before any routine. */
|
|
93
|
+
private readonly moduleScope: ExpressionLowering;
|
|
94
|
+
|
|
95
|
+
constructor(
|
|
96
|
+
private readonly program: ts.Program,
|
|
97
|
+
private readonly host: ts.ModuleResolutionHost,
|
|
98
|
+
private readonly root: string,
|
|
99
|
+
private readonly sdk: string,
|
|
100
|
+
private readonly diagnostics: Diagnostic[],
|
|
101
|
+
private readonly positions: Positions,
|
|
102
|
+
serviceRecord?: (type: ts.Type) => ts.Symbol | undefined,
|
|
103
|
+
) {
|
|
104
|
+
this.checker = program.getTypeChecker();
|
|
105
|
+
this.resolver = new TypeResolver(
|
|
106
|
+
this.checker,
|
|
107
|
+
root,
|
|
108
|
+
sdk,
|
|
109
|
+
this.fail,
|
|
110
|
+
serviceRecord,
|
|
111
|
+
);
|
|
112
|
+
this.routines = new RoutineTable(
|
|
113
|
+
this.checker,
|
|
114
|
+
this.resolver,
|
|
115
|
+
positions,
|
|
116
|
+
this.fail,
|
|
117
|
+
);
|
|
118
|
+
this.blocks = new BlockResolver(
|
|
119
|
+
this.resolver,
|
|
120
|
+
positions.node,
|
|
121
|
+
this.fail,
|
|
122
|
+
positions.node(program.getSourceFile(sdk)!),
|
|
123
|
+
);
|
|
124
|
+
this.context = {
|
|
125
|
+
checker: this.checker,
|
|
126
|
+
resolver: this.resolver,
|
|
127
|
+
blocks: this.blocks,
|
|
128
|
+
routines: this.routines,
|
|
129
|
+
positions,
|
|
130
|
+
fail: this.fail,
|
|
131
|
+
};
|
|
132
|
+
this.moduleScope = new ExpressionLowering(this.context, {
|
|
133
|
+
fn: undefined,
|
|
134
|
+
block: undefined,
|
|
135
|
+
environment: new Map(),
|
|
136
|
+
variables: this.variables,
|
|
137
|
+
});
|
|
138
|
+
const roots = new Set(program.getRootFileNames());
|
|
139
|
+
this.sources = runtimeSources(program).filter(
|
|
140
|
+
(source) =>
|
|
141
|
+
source.fileName !== sdk &&
|
|
142
|
+
!inSdk(sdk, source.fileName) &&
|
|
143
|
+
!program.isSourceFileDefaultLibrary(source) &&
|
|
144
|
+
(!source.isDeclarationFile || roots.has(source.fileName)),
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private type(node: ts.TypeNode): ResolvedType {
|
|
149
|
+
return this.resolver.resolve(node);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private readonly fail: Fail = (node, code, message) => {
|
|
153
|
+
throw new SemanticError(error(code, message, this.positions.node(node)));
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
private guard(action: () => void): void {
|
|
157
|
+
try {
|
|
158
|
+
action();
|
|
159
|
+
} catch (cause) {
|
|
160
|
+
if (!(cause instanceof SemanticError)) {
|
|
161
|
+
throw cause;
|
|
162
|
+
}
|
|
163
|
+
this.diagnostics.push(cause.diagnostic);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
run(entries: FrontendOptions["programs"]): PlcModule {
|
|
168
|
+
// Collect every declaration before resolving initializers and routine bodies.
|
|
169
|
+
// Generic calls can enqueue more helpers, so resolve entry points last.
|
|
170
|
+
this.collectDeclarations();
|
|
171
|
+
this.initializeStorage();
|
|
172
|
+
this.analyzeBlockBodies();
|
|
173
|
+
this.analyzePendingFunctions();
|
|
174
|
+
const programs = this.resolvePrograms(entries);
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
types: [...this.resolver.types.values()],
|
|
178
|
+
variables: this.globalVariables,
|
|
179
|
+
functions: [...this.routines.instances.values()],
|
|
180
|
+
blocks: [...this.blocks.definitions.values()],
|
|
181
|
+
programs,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
private collectDeclarations(): void {
|
|
186
|
+
const classes = this.sources.flatMap((source) =>
|
|
187
|
+
source.statements.filter(ts.isClassDeclaration),
|
|
188
|
+
);
|
|
189
|
+
this.blocks.index(classes);
|
|
190
|
+
for (const node of classes) {
|
|
191
|
+
this.guard(() => this.blocks.collect(node));
|
|
192
|
+
}
|
|
193
|
+
for (const source of this.blocks.sources.values()) {
|
|
194
|
+
for (const method of source.methods) {
|
|
195
|
+
this.guard(() => {
|
|
196
|
+
const fn = this.routines.instantiate(method, [], method, new Map());
|
|
197
|
+
fn.owner = {
|
|
198
|
+
block: source.block.id,
|
|
199
|
+
access: methodAccess(method),
|
|
200
|
+
};
|
|
201
|
+
this.routines.methods.set(this.resolver.symbol(method.name), fn);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
for (const source of this.sources) {
|
|
206
|
+
this.diagnostics.push(...suppressionDiagnostics(source, this.positions));
|
|
207
|
+
for (const statement of source.statements) {
|
|
208
|
+
this.guard(() => this.declaration(statement));
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private initializeStorage(): void {
|
|
214
|
+
for (const source of this.sources) {
|
|
215
|
+
for (const statement of source.statements) {
|
|
216
|
+
if (ts.isVariableStatement(statement) && !source.isDeclarationFile) {
|
|
217
|
+
this.guard(() => {
|
|
218
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
219
|
+
if (!declaration.initializer) {
|
|
220
|
+
this.fail(
|
|
221
|
+
declaration,
|
|
222
|
+
"PLC3001",
|
|
223
|
+
"Persistent PLC state requires a constant initializer.",
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
const variable = this.variables.get(
|
|
227
|
+
this.resolver.symbol(declaration.name),
|
|
228
|
+
);
|
|
229
|
+
if (!variable || variable.storage === "instance") {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const initializer = storageInitializer(
|
|
233
|
+
declaration.initializer,
|
|
234
|
+
this.resolver,
|
|
235
|
+
this.fail,
|
|
236
|
+
).value;
|
|
237
|
+
const inferred = variable.type === "inferred";
|
|
238
|
+
const expression = this.moduleScope.expression(
|
|
239
|
+
initializer,
|
|
240
|
+
inferred ? undefined : variable.type,
|
|
241
|
+
);
|
|
242
|
+
variable.type = expression.type;
|
|
243
|
+
if (
|
|
244
|
+
inferred &&
|
|
245
|
+
variable.storage === "constant" &&
|
|
246
|
+
aggregate(this.resolver.types.get(variable.type))
|
|
247
|
+
) {
|
|
248
|
+
variable.storage = "module";
|
|
249
|
+
}
|
|
250
|
+
if (variable.retain && variable.storage === "constant") {
|
|
251
|
+
this.fail(
|
|
252
|
+
declaration,
|
|
253
|
+
"PLC3004",
|
|
254
|
+
"retain requires mutable persistent storage or a static block instance.",
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
const value = this.moduleScope.constant(
|
|
258
|
+
this.moduleScope.implicitCopy(expression),
|
|
259
|
+
);
|
|
260
|
+
if (!value) {
|
|
261
|
+
this.fail(
|
|
262
|
+
declaration.initializer,
|
|
263
|
+
"PLC3001",
|
|
264
|
+
"Persistent initializers must be compile-time constants.",
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
variable.initial = value;
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
private analyzeBlockBodies(): void {
|
|
276
|
+
for (const source of this.blocks.sources.values()) {
|
|
277
|
+
this.guard(() => {
|
|
278
|
+
for (const [variable, node] of source.initializers) {
|
|
279
|
+
const value = this.moduleScope.constant(
|
|
280
|
+
this.moduleScope.expression(node, variable.type),
|
|
281
|
+
);
|
|
282
|
+
if (!value) {
|
|
283
|
+
this.fail(
|
|
284
|
+
node,
|
|
285
|
+
"PLC3001",
|
|
286
|
+
"Block state requires a compile-time constant initializer.",
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
variable.initial = value;
|
|
290
|
+
}
|
|
291
|
+
const { block } = source;
|
|
292
|
+
const routine = this.routineBody(
|
|
293
|
+
{
|
|
294
|
+
id: block.id,
|
|
295
|
+
name: block.name,
|
|
296
|
+
parameters: block.inputs,
|
|
297
|
+
locals: [],
|
|
298
|
+
body: [],
|
|
299
|
+
span: block.span,
|
|
300
|
+
...(block.result && { result: block.result }),
|
|
301
|
+
},
|
|
302
|
+
source.fieldBindings,
|
|
303
|
+
source.method.body!,
|
|
304
|
+
{ block: source, environment: new Map() },
|
|
305
|
+
);
|
|
306
|
+
block.locals = routine.locals;
|
|
307
|
+
block.body = routine.body;
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
private analyzePendingFunctions(): void {
|
|
313
|
+
// A worklist allows explicit generic instances to share source without sharing locals.
|
|
314
|
+
// Lowering a body can append instances, which this loop then reaches.
|
|
315
|
+
for (const instance of this.routines.pending) {
|
|
316
|
+
this.guard(() => {
|
|
317
|
+
const block = instance.fn.owner
|
|
318
|
+
? [...this.blocks.sources.values()].find(
|
|
319
|
+
(source) => source.block.id === instance.fn.owner?.block,
|
|
320
|
+
)
|
|
321
|
+
: undefined;
|
|
322
|
+
// Declaration admission proved the parameter list and body before worklist analysis.
|
|
323
|
+
const fn = this.routineBody(
|
|
324
|
+
instance.fn,
|
|
325
|
+
[
|
|
326
|
+
...(block?.fieldBindings ?? []),
|
|
327
|
+
...instance.node.parameters.map(
|
|
328
|
+
(parameter, index) =>
|
|
329
|
+
[
|
|
330
|
+
this.resolver.symbol(parameter.name),
|
|
331
|
+
instance.fn.parameters[index]!,
|
|
332
|
+
] as const,
|
|
333
|
+
),
|
|
334
|
+
],
|
|
335
|
+
instance.node.body!,
|
|
336
|
+
{ block, environment: instance.environment },
|
|
337
|
+
);
|
|
338
|
+
this.routines.instances.set(fn.id, fn);
|
|
339
|
+
if (
|
|
340
|
+
ts.isFunctionDeclaration(instance.node) &&
|
|
341
|
+
!instance.node.typeParameters?.length
|
|
342
|
+
) {
|
|
343
|
+
this.routines.functions.set(
|
|
344
|
+
this.resolver.symbol(instance.node.name!),
|
|
345
|
+
fn,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Lowers one routine body with its bindings layered over module storage.
|
|
353
|
+
private routineBody(
|
|
354
|
+
fn: PlcFunction,
|
|
355
|
+
bindings: Iterable<readonly [ts.Symbol, Variable]>,
|
|
356
|
+
body: ts.Block,
|
|
357
|
+
scope: Pick<LoweringScope, "block" | "environment">,
|
|
358
|
+
): PlcFunction {
|
|
359
|
+
const variables = new Map(this.variables);
|
|
360
|
+
for (const [symbol, variable] of bindings) {
|
|
361
|
+
variables.set(symbol, variable);
|
|
362
|
+
}
|
|
363
|
+
const expressions = new ExpressionLowering(this.context, {
|
|
364
|
+
...scope,
|
|
365
|
+
fn,
|
|
366
|
+
variables,
|
|
367
|
+
});
|
|
368
|
+
const statements = new StatementLowering(this.context, expressions, fn);
|
|
369
|
+
return { ...fn, ...statements.lowerRoutine(body) };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
private resolvePrograms(entries: FrontendOptions["programs"]): PlcProgram[] {
|
|
373
|
+
const programs: PlcProgram[] = [];
|
|
374
|
+
for (const [name, entry] of Object.entries(entries)) {
|
|
375
|
+
const source = this.program.getSourceFile(
|
|
376
|
+
resolve(this.root, entry.module),
|
|
377
|
+
);
|
|
378
|
+
if (!source) {
|
|
379
|
+
this.diagnostics.push(
|
|
380
|
+
error("PLC4001", `Program source not found: ${entry.module}.`),
|
|
381
|
+
);
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
this.guard(() => {
|
|
385
|
+
const moduleSymbol = this.checker.getSymbolAtLocation(source);
|
|
386
|
+
const exported =
|
|
387
|
+
moduleSymbol &&
|
|
388
|
+
this.checker
|
|
389
|
+
.getExportsOfModule(moduleSymbol)
|
|
390
|
+
.find((symbol) => symbol.name === entry.export);
|
|
391
|
+
if (!exported) {
|
|
392
|
+
this.fail(
|
|
393
|
+
source,
|
|
394
|
+
"PLC4001",
|
|
395
|
+
`Program export ${entry.export} was not found.`,
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
const fn = this.routines.functions.get(
|
|
399
|
+
unaliased(this.checker, exported),
|
|
400
|
+
);
|
|
401
|
+
if (!fn || fn.parameters.length || fn.result) {
|
|
402
|
+
this.fail(
|
|
403
|
+
source,
|
|
404
|
+
"PLC4001",
|
|
405
|
+
"Program entry must be an implemented parameterless void function.",
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
programs.push({ id: `program:${name}`, name, entry: fn.id });
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
return programs;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
private dependency(node: ts.ImportDeclaration | ts.ExportDeclaration): void {
|
|
415
|
+
if (!node.moduleSpecifier) {
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (!ts.isStringLiteral(node.moduleSpecifier)) {
|
|
419
|
+
this.fail(node, "PLC1001", "Only static module imports are supported.");
|
|
420
|
+
}
|
|
421
|
+
const resolved = ts.resolveModuleName(
|
|
422
|
+
node.moduleSpecifier.text,
|
|
423
|
+
node.getSourceFile().fileName,
|
|
424
|
+
this.program.getCompilerOptions(),
|
|
425
|
+
this.host,
|
|
426
|
+
).resolvedModule;
|
|
427
|
+
if (!resolved) {
|
|
428
|
+
this.fail(
|
|
429
|
+
node.moduleSpecifier,
|
|
430
|
+
"PLC1008",
|
|
431
|
+
"This dependency could not be resolved to PLC source.",
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
const source = this.program.getSourceFile(resolved.resolvedFileName);
|
|
435
|
+
const typeOnly = ts.isImportDeclaration(node)
|
|
436
|
+
? node.importClause?.isTypeOnly
|
|
437
|
+
: node.isTypeOnly;
|
|
438
|
+
if (typeOnly) {
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
const bindings = ts.isImportDeclaration(node)
|
|
442
|
+
? node.importClause?.namedBindings
|
|
443
|
+
: node.exportClause;
|
|
444
|
+
const defaultName = ts.isImportDeclaration(node)
|
|
445
|
+
? node.importClause?.name
|
|
446
|
+
: undefined;
|
|
447
|
+
const namedOnly =
|
|
448
|
+
bindings &&
|
|
449
|
+
(ts.isNamedImports(bindings) || ts.isNamedExports(bindings)) &&
|
|
450
|
+
bindings.elements.length > 0 &&
|
|
451
|
+
bindings.elements.every(
|
|
452
|
+
(binding) =>
|
|
453
|
+
binding.isTypeOnly ||
|
|
454
|
+
!(this.resolver.symbol(binding.name).flags & ts.SymbolFlags.Value),
|
|
455
|
+
);
|
|
456
|
+
if (namedOnly && !defaultName) {
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
if (source && !source.isDeclarationFile) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
if (inSdk(this.sdk, resolved.resolvedFileName)) {
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const authoredIo =
|
|
466
|
+
source &&
|
|
467
|
+
this.sources.includes(source) &&
|
|
468
|
+
source.statements.some(ts.isVariableStatement) &&
|
|
469
|
+
source.statements.every(
|
|
470
|
+
(statement) =>
|
|
471
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
472
|
+
ts.isEmptyStatement(statement) ||
|
|
473
|
+
(ts.isVariableStatement(statement) &&
|
|
474
|
+
statement.declarationList.declarations.every(
|
|
475
|
+
(declaration) =>
|
|
476
|
+
declaration.type && this.type(declaration.type).io,
|
|
477
|
+
)),
|
|
478
|
+
);
|
|
479
|
+
if (authoredIo) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
this.fail(
|
|
483
|
+
node.moduleSpecifier,
|
|
484
|
+
"PLC1008",
|
|
485
|
+
"Runtime dependencies need PLC TypeScript implementation source; JavaScript and declaration-only value imports are unsupported.",
|
|
486
|
+
);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
private declaration(statement: ts.Statement): void {
|
|
490
|
+
if (
|
|
491
|
+
ts.isImportDeclaration(statement) ||
|
|
492
|
+
ts.isExportDeclaration(statement)
|
|
493
|
+
) {
|
|
494
|
+
this.dependency(statement);
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
if (
|
|
498
|
+
ts.isClassDeclaration(statement) ||
|
|
499
|
+
ts.isTypeAliasDeclaration(statement) ||
|
|
500
|
+
ts.isInterfaceDeclaration(statement) ||
|
|
501
|
+
ts.isEmptyStatement(statement)
|
|
502
|
+
) {
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
if (ts.isEnumDeclaration(statement)) {
|
|
506
|
+
this.resolver.enumeration(statement);
|
|
507
|
+
return;
|
|
508
|
+
}
|
|
509
|
+
if (ts.isVariableStatement(statement)) {
|
|
510
|
+
this.moduleVariables(statement);
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
if (ts.isFunctionDeclaration(statement)) {
|
|
514
|
+
if (!statement.name || !statement.body) {
|
|
515
|
+
this.fail(
|
|
516
|
+
statement,
|
|
517
|
+
"PLC1001",
|
|
518
|
+
"PLC functions need a name and an implementation.",
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
if (hasModifier(statement, ts.SyntaxKind.AsyncKeyword)) {
|
|
522
|
+
this.fail(
|
|
523
|
+
statement,
|
|
524
|
+
"PLC1007",
|
|
525
|
+
"Async execution is not supported in PLC scan code.",
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
if (statement.asteriskToken) {
|
|
529
|
+
this.fail(
|
|
530
|
+
statement,
|
|
531
|
+
"PLC1007",
|
|
532
|
+
"Generators are not supported in PLC scan code.",
|
|
533
|
+
);
|
|
534
|
+
}
|
|
535
|
+
const symbol = this.resolver.symbol(statement.name);
|
|
536
|
+
this.routines.functionDeclarations.set(symbol, statement);
|
|
537
|
+
if (!statement.typeParameters?.length) {
|
|
538
|
+
this.routines.functions.set(
|
|
539
|
+
symbol,
|
|
540
|
+
this.routines.instantiate(statement, [], statement, new Map()),
|
|
541
|
+
);
|
|
542
|
+
}
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
this.fail(
|
|
546
|
+
statement,
|
|
547
|
+
"PLC1001",
|
|
548
|
+
`${ts.SyntaxKind[statement.kind]} is not supported in PLC source.`,
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
private moduleVariables(statement: ts.VariableStatement): void {
|
|
553
|
+
if (
|
|
554
|
+
!(
|
|
555
|
+
statement.declarationList.flags &
|
|
556
|
+
(ts.NodeFlags.Let | ts.NodeFlags.Const)
|
|
557
|
+
)
|
|
558
|
+
) {
|
|
559
|
+
this.fail(statement, "PLC1001", "Use let or const instead of var.");
|
|
560
|
+
}
|
|
561
|
+
const immutable = Boolean(
|
|
562
|
+
statement.declarationList.flags & ts.NodeFlags.Const,
|
|
563
|
+
);
|
|
564
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
565
|
+
if (!ts.isIdentifier(declaration.name)) {
|
|
566
|
+
this.fail(
|
|
567
|
+
declaration.name,
|
|
568
|
+
"PLC1001",
|
|
569
|
+
"Destructuring declarations are not supported.",
|
|
570
|
+
);
|
|
571
|
+
}
|
|
572
|
+
const initializer =
|
|
573
|
+
declaration.initializer &&
|
|
574
|
+
storageInitializer(declaration.initializer, this.resolver, this.fail);
|
|
575
|
+
const table =
|
|
576
|
+
initializer && this.tableType(declaration, initializer.value);
|
|
577
|
+
const instance =
|
|
578
|
+
initializer &&
|
|
579
|
+
this.allocatedBlock(declaration, initializer.value, table);
|
|
580
|
+
const literal = initializer && numericLiteral(initializer.value);
|
|
581
|
+
if (table && !immutable) {
|
|
582
|
+
this.fail(
|
|
583
|
+
declaration,
|
|
584
|
+
"PLC2001",
|
|
585
|
+
"A readonly table is a const declaration.",
|
|
586
|
+
);
|
|
587
|
+
}
|
|
588
|
+
const type = this.storageType(declaration, {
|
|
589
|
+
instance,
|
|
590
|
+
table,
|
|
591
|
+
immutable,
|
|
592
|
+
literal,
|
|
593
|
+
});
|
|
594
|
+
if (
|
|
595
|
+
!instance &&
|
|
596
|
+
!declaration.type &&
|
|
597
|
+
!immutable &&
|
|
598
|
+
!literal &&
|
|
599
|
+
declaration.initializer &&
|
|
600
|
+
initializer?.value.kind !== ts.SyntaxKind.TrueKeyword &&
|
|
601
|
+
initializer?.value.kind !== ts.SyntaxKind.FalseKeyword
|
|
602
|
+
) {
|
|
603
|
+
this.fail(declaration, "PLC2001", "Annotate PLC storage explicitly.");
|
|
604
|
+
}
|
|
605
|
+
const ambient = declaration.getSourceFile().isDeclarationFile;
|
|
606
|
+
if (type.mode || this.resolver.hasReferences(type.type)) {
|
|
607
|
+
this.fail(
|
|
608
|
+
declaration,
|
|
609
|
+
"PLC2001",
|
|
610
|
+
"Borrow schemas cannot be stored as values.",
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
if (this.resolver.types.get(type.type)?.kind === "block" && !instance) {
|
|
614
|
+
this.fail(
|
|
615
|
+
declaration,
|
|
616
|
+
"PLC1007",
|
|
617
|
+
"Block instances require a static constructor initializer.",
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
if (
|
|
621
|
+
instance &&
|
|
622
|
+
(!immutable ||
|
|
623
|
+
(declaration.type && this.type(declaration.type).type !== instance))
|
|
624
|
+
) {
|
|
625
|
+
this.fail(
|
|
626
|
+
declaration,
|
|
627
|
+
"PLC2001",
|
|
628
|
+
"Static block instances require const bindings and matching annotations.",
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
if (
|
|
632
|
+
initializer?.retain &&
|
|
633
|
+
(ambient ||
|
|
634
|
+
(immutable &&
|
|
635
|
+
type.type !== "inferred" &&
|
|
636
|
+
!instance &&
|
|
637
|
+
!aggregate(this.resolver.types.get(type.type))))
|
|
638
|
+
) {
|
|
639
|
+
this.fail(
|
|
640
|
+
declaration,
|
|
641
|
+
"PLC3004",
|
|
642
|
+
"retain requires mutable persistent storage or a static block instance.",
|
|
643
|
+
);
|
|
644
|
+
}
|
|
645
|
+
if (ambient && !type.io) {
|
|
646
|
+
this.fail(
|
|
647
|
+
declaration,
|
|
648
|
+
"PLC4002",
|
|
649
|
+
"Ambient PLC variables must declare Input or Output.",
|
|
650
|
+
);
|
|
651
|
+
}
|
|
652
|
+
if (!ambient && type.io) {
|
|
653
|
+
this.fail(
|
|
654
|
+
declaration,
|
|
655
|
+
"PLC4002",
|
|
656
|
+
"Logical I/O belongs in an ambient declaration file.",
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
if (type.io === "input" && !immutable) {
|
|
660
|
+
this.fail(declaration, "PLC4002", "Inputs must use declare const.");
|
|
661
|
+
}
|
|
662
|
+
if (type.io === "output" && immutable) {
|
|
663
|
+
this.fail(declaration, "PLC4002", "Outputs must use declare let.");
|
|
664
|
+
}
|
|
665
|
+
const variable: Variable = {
|
|
666
|
+
id: this.resolver.id(declaration, declaration.name.text),
|
|
667
|
+
name: declaration.name.text,
|
|
668
|
+
type: type.type,
|
|
669
|
+
storage: this.storageClass(type, { instance, table, immutable }),
|
|
670
|
+
mutable: !immutable,
|
|
671
|
+
retain: initializer?.retain ?? false,
|
|
672
|
+
...(hasModifier(statement, ts.SyntaxKind.ExportKeyword) && {
|
|
673
|
+
exported: true as const,
|
|
674
|
+
}),
|
|
675
|
+
span: this.positions.node(declaration),
|
|
676
|
+
};
|
|
677
|
+
this.variables.set(this.resolver.symbol(declaration.name), variable);
|
|
678
|
+
this.globalVariables.push(variable);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/** The block a static instance or instance-array initializer allocates. */
|
|
683
|
+
private allocatedBlock(
|
|
684
|
+
declaration: ts.VariableDeclaration,
|
|
685
|
+
initializer: ts.Expression,
|
|
686
|
+
table: ResolvedType | undefined,
|
|
687
|
+
): string | undefined {
|
|
688
|
+
if (ts.isNewExpression(initializer)) {
|
|
689
|
+
return this.blocks.instanceType(initializer);
|
|
690
|
+
}
|
|
691
|
+
return table
|
|
692
|
+
? undefined
|
|
693
|
+
: this.blocks.instanceArrayType(declaration.type, initializer);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/** An unannotated const takes its initializer's type once initializers resolve. */
|
|
697
|
+
private storageType(
|
|
698
|
+
declaration: ts.VariableDeclaration,
|
|
699
|
+
{ instance, table, immutable, literal }: StorageFacts,
|
|
700
|
+
): ResolvedType {
|
|
701
|
+
if (instance) {
|
|
702
|
+
return { type: instance };
|
|
703
|
+
}
|
|
704
|
+
if (table) {
|
|
705
|
+
return table;
|
|
706
|
+
}
|
|
707
|
+
if (declaration.type) {
|
|
708
|
+
return this.type(declaration.type);
|
|
709
|
+
}
|
|
710
|
+
if (immutable) {
|
|
711
|
+
return { type: "inferred" };
|
|
712
|
+
}
|
|
713
|
+
return { type: literal ? numericLiteralType(literal) : "BOOL" };
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/** A const record or array keeps mutable fields, so only scalars and readonly tables are constants. */
|
|
717
|
+
private storageClass(
|
|
718
|
+
type: ResolvedType,
|
|
719
|
+
{ instance, table, immutable }: Omit<StorageFacts, "literal">,
|
|
720
|
+
): Variable["storage"] {
|
|
721
|
+
if (instance) {
|
|
722
|
+
return "instance";
|
|
723
|
+
}
|
|
724
|
+
if (type.io) {
|
|
725
|
+
return type.io;
|
|
726
|
+
}
|
|
727
|
+
return immutable &&
|
|
728
|
+
(table || !aggregate(this.resolver.types.get(type.type)))
|
|
729
|
+
? "constant"
|
|
730
|
+
: "module";
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/**
|
|
734
|
+
* `const table: readonly T[] = [...]` or `const table = [...] as const satisfies readonly T[]`,
|
|
735
|
+
* also spelled `ReadonlyArray<T>`, is a constant `FixedArray<T, N>` whose length is the
|
|
736
|
+
* literal's element count.
|
|
737
|
+
*/
|
|
738
|
+
private tableType(
|
|
739
|
+
declaration: ts.VariableDeclaration,
|
|
740
|
+
initializer: ts.Expression,
|
|
741
|
+
): ResolvedType | undefined {
|
|
742
|
+
const satisfied = ts.isSatisfiesExpression(initializer)
|
|
743
|
+
? initializer
|
|
744
|
+
: undefined;
|
|
745
|
+
const annotation = declaration.type ?? satisfied?.type;
|
|
746
|
+
if (!annotation) {
|
|
747
|
+
return undefined;
|
|
748
|
+
}
|
|
749
|
+
const element = this.readonlyElement(annotation);
|
|
750
|
+
if (!element) {
|
|
751
|
+
return undefined;
|
|
752
|
+
}
|
|
753
|
+
const literal = withoutConstAssertion(satisfied?.expression ?? initializer);
|
|
754
|
+
if (!ts.isArrayLiteralExpression(literal)) {
|
|
755
|
+
this.fail(
|
|
756
|
+
declaration,
|
|
757
|
+
"PLC2001",
|
|
758
|
+
"A readonly table is initialized by an array literal.",
|
|
759
|
+
);
|
|
760
|
+
}
|
|
761
|
+
return this.resolver.fixedArray(
|
|
762
|
+
annotation,
|
|
763
|
+
this.type(element),
|
|
764
|
+
{ kind: "integer", value: literal.elements.length },
|
|
765
|
+
{ kind: "integer", value: 0 },
|
|
766
|
+
);
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/** The element type of `readonly T[]` or the standard `ReadonlyArray<T>`. */
|
|
770
|
+
private readonlyElement(annotation: ts.TypeNode): ts.TypeNode | undefined {
|
|
771
|
+
if (
|
|
772
|
+
ts.isTypeOperatorNode(annotation) &&
|
|
773
|
+
annotation.operator === ts.SyntaxKind.ReadonlyKeyword &&
|
|
774
|
+
ts.isArrayTypeNode(annotation.type)
|
|
775
|
+
) {
|
|
776
|
+
return annotation.type.elementType;
|
|
777
|
+
}
|
|
778
|
+
if (
|
|
779
|
+
ts.isTypeReferenceNode(annotation) &&
|
|
780
|
+
annotation.typeName.getText() === "ReadonlyArray" &&
|
|
781
|
+
annotation.typeArguments?.length === 1 &&
|
|
782
|
+
this.resolver.standardLibrary(this.resolver.symbol(annotation.typeName))
|
|
783
|
+
) {
|
|
784
|
+
return annotation.typeArguments[0];
|
|
785
|
+
}
|
|
786
|
+
return undefined;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
const withoutConstAssertion = (node: ts.Expression): ts.Expression =>
|
|
791
|
+
ts.isParenthesizedExpression(node) ||
|
|
792
|
+
(ts.isAsExpression(node) && ts.isConstTypeReference(node.type))
|
|
793
|
+
? withoutConstAssertion(node.expression)
|
|
794
|
+
: node;
|