@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
package/dist/index.js
CHANGED
|
@@ -4,7 +4,7 @@ import { createRequire as createRequire2 } from "node:module";
|
|
|
4
4
|
import { dirname as dirname7, join as join5, relative as relative4, resolve as resolve7 } from "node:path";
|
|
5
5
|
import { hasErrors } from "@tsplc/compiler/ir";
|
|
6
6
|
import { generatedDirectory as generatedDirectory2 } from "@tsplc/config/project-files";
|
|
7
|
-
import
|
|
7
|
+
import ts45 from "typescript";
|
|
8
8
|
|
|
9
9
|
// packages/compiler/src/frontend/analyzer.ts
|
|
10
10
|
import { resolve as resolve2 } from "node:path";
|
|
@@ -2991,6 +2991,7 @@ class Analyzer {
|
|
|
2991
2991
|
sdk;
|
|
2992
2992
|
diagnostics;
|
|
2993
2993
|
positions;
|
|
2994
|
+
reduced;
|
|
2994
2995
|
checker;
|
|
2995
2996
|
resolver;
|
|
2996
2997
|
routines;
|
|
@@ -2999,13 +3000,14 @@ class Analyzer {
|
|
|
2999
3000
|
sources;
|
|
3000
3001
|
context;
|
|
3001
3002
|
moduleScope;
|
|
3002
|
-
constructor(program, host, root, sdk, diagnostics, positions, serviceRecord) {
|
|
3003
|
+
constructor(program, host, root, sdk, diagnostics, positions, serviceRecord, reduced) {
|
|
3003
3004
|
this.program = program;
|
|
3004
3005
|
this.host = host;
|
|
3005
3006
|
this.root = root;
|
|
3006
3007
|
this.sdk = sdk;
|
|
3007
3008
|
this.diagnostics = diagnostics;
|
|
3008
3009
|
this.positions = positions;
|
|
3010
|
+
this.reduced = reduced;
|
|
3009
3011
|
this.checker = program.getTypeChecker();
|
|
3010
3012
|
this.resolver = new TypeResolver(this.checker, root, sdk, this.fail, serviceRecord);
|
|
3011
3013
|
this.routines = new RoutineTable(this.checker, this.resolver, positions, this.fail);
|
|
@@ -3060,8 +3062,21 @@ class Analyzer {
|
|
|
3060
3062
|
programs
|
|
3061
3063
|
};
|
|
3062
3064
|
}
|
|
3065
|
+
statements(source) {
|
|
3066
|
+
const part = this.reduced.get(source.fileName);
|
|
3067
|
+
if (!part) {
|
|
3068
|
+
return source.statements;
|
|
3069
|
+
}
|
|
3070
|
+
const names = (statement) => {
|
|
3071
|
+
if (ts10.isVariableStatement(statement)) {
|
|
3072
|
+
return statement.declarationList.declarations.map((declaration) => declaration.name.getText());
|
|
3073
|
+
}
|
|
3074
|
+
return ts10.isEnumDeclaration(statement) ? [statement.name.text] : [];
|
|
3075
|
+
};
|
|
3076
|
+
return source.statements.filter((statement) => ("compiled" in part) ? ts10.isVariableStatement(statement) && names(statement).some((name) => part.compiled.has(name)) : !names(statement).some((name) => part.declared.has(name)));
|
|
3077
|
+
}
|
|
3063
3078
|
collectDeclarations() {
|
|
3064
|
-
const classes = this.sources.flatMap((source) =>
|
|
3079
|
+
const classes = this.sources.flatMap((source) => this.statements(source).filter(ts10.isClassDeclaration));
|
|
3065
3080
|
this.blocks.index(classes);
|
|
3066
3081
|
for (const node of classes) {
|
|
3067
3082
|
this.guard(() => this.blocks.collect(node));
|
|
@@ -3073,14 +3088,14 @@ class Analyzer {
|
|
|
3073
3088
|
}
|
|
3074
3089
|
for (const source of this.sources) {
|
|
3075
3090
|
this.diagnostics.push(...suppressionDiagnostics(source, this.positions));
|
|
3076
|
-
for (const statement of
|
|
3091
|
+
for (const statement of this.statements(source)) {
|
|
3077
3092
|
this.guard(() => this.declaration(statement));
|
|
3078
3093
|
}
|
|
3079
3094
|
}
|
|
3080
3095
|
}
|
|
3081
3096
|
initializeStorage() {
|
|
3082
3097
|
for (const source of this.sources) {
|
|
3083
|
-
for (const statement of
|
|
3098
|
+
for (const statement of this.statements(source)) {
|
|
3084
3099
|
if (ts10.isVariableStatement(statement) && !source.isDeclarationFile) {
|
|
3085
3100
|
this.guard(() => {
|
|
3086
3101
|
for (const declaration of statement.declarationList.declarations) {
|
|
@@ -6221,7 +6236,7 @@ import {
|
|
|
6221
6236
|
identityDigest as identityDigest6
|
|
6222
6237
|
} from "@tsplc/compiler/ir";
|
|
6223
6238
|
import { generatedDirectory } from "@tsplc/config/project-files";
|
|
6224
|
-
import
|
|
6239
|
+
import ts42 from "typescript";
|
|
6225
6240
|
|
|
6226
6241
|
// packages/compiler/src/frontend/elaborate/generics.ts
|
|
6227
6242
|
import ts20 from "typescript";
|
|
@@ -6730,22 +6745,47 @@ var importPath = (from, to) => {
|
|
|
6730
6745
|
};
|
|
6731
6746
|
|
|
6732
6747
|
class ModuleImports {
|
|
6733
|
-
names
|
|
6748
|
+
names;
|
|
6749
|
+
avoided;
|
|
6750
|
+
imported = new Map;
|
|
6751
|
+
constructor(names, avoided = new Set) {
|
|
6752
|
+
this.names = names;
|
|
6753
|
+
this.avoided = avoided;
|
|
6754
|
+
}
|
|
6755
|
+
has(file, name) {
|
|
6756
|
+
return this.imported.get(file)?.has(name) ?? false;
|
|
6757
|
+
}
|
|
6734
6758
|
add(file, name) {
|
|
6735
|
-
const names = this.
|
|
6736
|
-
|
|
6737
|
-
this.names.
|
|
6738
|
-
|
|
6759
|
+
const names = this.imported.get(file) ?? new Map;
|
|
6760
|
+
this.imported.set(file, names);
|
|
6761
|
+
const local = this.names.import(`${file}\x00${name}`, name, this.avoided);
|
|
6762
|
+
names.set(name, local);
|
|
6763
|
+
return local;
|
|
6739
6764
|
}
|
|
6740
6765
|
lines(importer, keyword) {
|
|
6741
|
-
return [...this.
|
|
6766
|
+
return [...this.imported].flatMap(([file, names]) => {
|
|
6767
|
+
const list = [...names].filter(([name, local]) => file !== importer || name !== local).map(([name, local]) => name === local ? name : `${name} as ${local}`);
|
|
6768
|
+
return list.length ? [
|
|
6769
|
+
`${keyword} { ${list.join(", ")} } from "${importPath(importer, file)}";`
|
|
6770
|
+
] : [];
|
|
6771
|
+
});
|
|
6742
6772
|
}
|
|
6743
6773
|
}
|
|
6744
6774
|
|
|
6745
6775
|
class EntryImports {
|
|
6776
|
+
names;
|
|
6746
6777
|
namespaces = new Map;
|
|
6747
|
-
kept
|
|
6778
|
+
kept;
|
|
6748
6779
|
sdk = new Map;
|
|
6780
|
+
constructor(names) {
|
|
6781
|
+
this.names = names;
|
|
6782
|
+
this.kept = new ModuleImports(names);
|
|
6783
|
+
}
|
|
6784
|
+
namespace(file, name) {
|
|
6785
|
+
const local = this.names.import(file, name);
|
|
6786
|
+
this.namespaces.set(local, file);
|
|
6787
|
+
return local;
|
|
6788
|
+
}
|
|
6749
6789
|
addSdk(module, name) {
|
|
6750
6790
|
const names = this.sdk.get(module) ?? new Set;
|
|
6751
6791
|
names.add(name);
|
|
@@ -6762,7 +6802,7 @@ class EntryImports {
|
|
|
6762
6802
|
|
|
6763
6803
|
// packages/compiler/src/frontend/elaborate/layouts.ts
|
|
6764
6804
|
import { optionalRecord, pascalCase as pascalCase2, primitiveTypes as primitiveTypes2 } from "@tsplc/compiler/ir";
|
|
6765
|
-
import
|
|
6805
|
+
import ts27 from "typescript";
|
|
6766
6806
|
|
|
6767
6807
|
// packages/compiler/src/frontend/elaborate/model.ts
|
|
6768
6808
|
import { identityDigest as identityDigest3 } from "@tsplc/compiler/ir";
|
|
@@ -6781,6 +6821,7 @@ var ownerAt = (owner, step) => ({
|
|
|
6781
6821
|
var sameStep = (left, right) => left.kind === "index" ? right.kind === "index" && left.index === right.index : right.kind === "field" && left.name === right.name;
|
|
6782
6822
|
var unionMembers = (type) => type.isUnion() ? type.types : [type];
|
|
6783
6823
|
var isExported = (declaration) => Boolean(ts24.getCombinedModifierFlags(declaration) & ts24.ModifierFlags.Export);
|
|
6824
|
+
var listedExport = (source, name) => source.statements.some((statement) => ts24.isExportDeclaration(statement) && !statement.moduleSpecifier && !statement.isTypeOnly && statement.exportClause !== undefined && ts24.isNamedExports(statement.exportClause) && statement.exportClause.elements.some((element) => !element.isTypeOnly && (element.propertyName ?? element.name).text === name));
|
|
6784
6825
|
var children = (captured) => {
|
|
6785
6826
|
if (captured.kind === "array") {
|
|
6786
6827
|
return captured.items.map((item, index) => [
|
|
@@ -6834,7 +6875,7 @@ class Model {
|
|
|
6834
6875
|
file: binding.file,
|
|
6835
6876
|
name: binding.name,
|
|
6836
6877
|
declaration,
|
|
6837
|
-
exported: isExported(declaration) || bound.has(binding.name),
|
|
6878
|
+
exported: isExported(declaration) || listedExport(this.sourceFile(binding.file), binding.name) || bound.has(binding.name),
|
|
6838
6879
|
bound: bound.has(binding.name),
|
|
6839
6880
|
value: binding.value
|
|
6840
6881
|
};
|
|
@@ -7010,8 +7051,85 @@ class Model {
|
|
|
7010
7051
|
}
|
|
7011
7052
|
}
|
|
7012
7053
|
|
|
7013
|
-
// packages/compiler/src/frontend/elaborate/
|
|
7054
|
+
// packages/compiler/src/frontend/elaborate/names.ts
|
|
7014
7055
|
import ts25 from "typescript";
|
|
7056
|
+
var unusedName = (base, taken) => {
|
|
7057
|
+
if (!taken(base)) {
|
|
7058
|
+
return base;
|
|
7059
|
+
}
|
|
7060
|
+
for (let index = 2;; index++) {
|
|
7061
|
+
const name = `${base}_${index}`;
|
|
7062
|
+
if (!taken(name)) {
|
|
7063
|
+
return name;
|
|
7064
|
+
}
|
|
7065
|
+
}
|
|
7066
|
+
};
|
|
7067
|
+
|
|
7068
|
+
class Names {
|
|
7069
|
+
reserved;
|
|
7070
|
+
declared = new Set;
|
|
7071
|
+
imports = new Map;
|
|
7072
|
+
imported = new Set;
|
|
7073
|
+
constructor(reserved) {
|
|
7074
|
+
this.reserved = reserved;
|
|
7075
|
+
}
|
|
7076
|
+
declare(name) {
|
|
7077
|
+
this.declared.add(name);
|
|
7078
|
+
}
|
|
7079
|
+
importedAs(name) {
|
|
7080
|
+
return this.imported.has(name);
|
|
7081
|
+
}
|
|
7082
|
+
import(key, name, avoided = new Set) {
|
|
7083
|
+
const known = this.imports.get(key);
|
|
7084
|
+
if (known) {
|
|
7085
|
+
return known;
|
|
7086
|
+
}
|
|
7087
|
+
const local = unusedName(name, (candidate) => this.imported.has(candidate) || this.declared.has(candidate) || this.reserved.has(candidate) || avoided.has(candidate));
|
|
7088
|
+
this.imports.set(key, local);
|
|
7089
|
+
this.imported.add(local);
|
|
7090
|
+
return local;
|
|
7091
|
+
}
|
|
7092
|
+
}
|
|
7093
|
+
var routineNames = (sources) => {
|
|
7094
|
+
const names = new Set;
|
|
7095
|
+
for (const source of sources) {
|
|
7096
|
+
for (const statement of source.statements) {
|
|
7097
|
+
if (ts25.isFunctionDeclaration(statement) && statement.name && !statement.parameters.length) {
|
|
7098
|
+
names.add(statement.name.text);
|
|
7099
|
+
}
|
|
7100
|
+
if (!ts25.isVariableStatement(statement)) {
|
|
7101
|
+
continue;
|
|
7102
|
+
}
|
|
7103
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
7104
|
+
const initializer = declaration.initializer;
|
|
7105
|
+
if (ts25.isIdentifier(declaration.name) && initializer && (ts25.isArrowFunction(initializer) || ts25.isFunctionExpression(initializer)) && !initializer.parameters.length) {
|
|
7106
|
+
names.add(declaration.name.text);
|
|
7107
|
+
}
|
|
7108
|
+
}
|
|
7109
|
+
}
|
|
7110
|
+
}
|
|
7111
|
+
return names;
|
|
7112
|
+
};
|
|
7113
|
+
var rootFieldNames = (model) => {
|
|
7114
|
+
const names = new Set;
|
|
7115
|
+
for (const root of model.roots) {
|
|
7116
|
+
const node = model.node(root.value);
|
|
7117
|
+
if (node?.captured.kind === "object") {
|
|
7118
|
+
for (const field of Object.keys(node.captured.fields)) {
|
|
7119
|
+
names.add(field);
|
|
7120
|
+
}
|
|
7121
|
+
}
|
|
7122
|
+
if (node?.captured.kind === "instance") {
|
|
7123
|
+
for (const member of model.fields(model.classOf(node))) {
|
|
7124
|
+
names.add(member.name.getText());
|
|
7125
|
+
}
|
|
7126
|
+
}
|
|
7127
|
+
}
|
|
7128
|
+
return names;
|
|
7129
|
+
};
|
|
7130
|
+
|
|
7131
|
+
// packages/compiler/src/frontend/elaborate/values.ts
|
|
7132
|
+
import ts26 from "typescript";
|
|
7015
7133
|
var constant = (value) => ({
|
|
7016
7134
|
kind: "const",
|
|
7017
7135
|
value
|
|
@@ -7042,7 +7160,7 @@ var restoring = (map) => {
|
|
|
7042
7160
|
}
|
|
7043
7161
|
};
|
|
7044
7162
|
};
|
|
7045
|
-
var isStoredState = (value) => value.kind === "code" && (value.copyOf !== undefined || value.place === true && value.declared !== undefined && Boolean(value.declared.flags &
|
|
7163
|
+
var isStoredState = (value) => value.kind === "code" && (value.copyOf !== undefined || value.place === true && value.declared !== undefined && Boolean(value.declared.flags & ts26.TypeFlags.Object));
|
|
7046
7164
|
var holdsSetupObjects = (value) => [...value.fields?.values() ?? [], ...value.items ?? []].some((item) => item.kind === "static" || item.kind === "function" || item.kind === "choice");
|
|
7047
7165
|
var unwrapped = (node) => isWrapper(node) ? unwrapped(node.expression) : node;
|
|
7048
7166
|
var repeatable = (text) => /^[\w.[\]]+$/.test(String(text));
|
|
@@ -7076,28 +7194,35 @@ class Layouts {
|
|
|
7076
7194
|
checker;
|
|
7077
7195
|
kept;
|
|
7078
7196
|
instanceState;
|
|
7197
|
+
moduleNames;
|
|
7198
|
+
enumRefusal;
|
|
7079
7199
|
declarations = new Map;
|
|
7080
7200
|
origins = new Map;
|
|
7081
7201
|
takenNames;
|
|
7082
|
-
imports
|
|
7083
|
-
typeImports
|
|
7202
|
+
imports;
|
|
7203
|
+
typeImports;
|
|
7084
7204
|
enums = new Map;
|
|
7085
7205
|
records = new Map;
|
|
7086
7206
|
names = new Map;
|
|
7087
7207
|
keptLiterals = [];
|
|
7088
7208
|
keptTypes = new Map;
|
|
7089
|
-
constructor(model, checker, kept, keptSources, instanceState) {
|
|
7209
|
+
constructor(model, checker, kept, keptSources, instanceState, moduleNames, enumRefusal) {
|
|
7090
7210
|
this.model = model;
|
|
7091
7211
|
this.checker = checker;
|
|
7092
7212
|
this.kept = kept;
|
|
7093
7213
|
this.instanceState = instanceState;
|
|
7094
|
-
|
|
7214
|
+
this.moduleNames = moduleNames;
|
|
7215
|
+
this.enumRefusal = enumRefusal;
|
|
7216
|
+
const fields = rootFieldNames(model);
|
|
7217
|
+
this.imports = new ModuleImports(moduleNames, fields);
|
|
7218
|
+
this.typeImports = new ModuleImports(moduleNames, fields);
|
|
7219
|
+
const typeNames = new Set(model.program.getSourceFiles().filter((source) => !source.isDeclarationFile).flatMap((source) => source.statements.flatMap((statement) => ts27.isVariableStatement(statement) || ts27.isFunctionDeclaration(statement) ? [] : declaredNames(statement))));
|
|
7095
7220
|
this.takenNames = new Set(model.roots.map((root) => pascalCase2(root.name)).filter((name) => !typeNames.has(name)));
|
|
7096
7221
|
const collectLiterals = (node) => {
|
|
7097
|
-
if (
|
|
7222
|
+
if (ts27.isTypeLiteralNode(node)) {
|
|
7098
7223
|
this.keptLiterals.push(node);
|
|
7099
7224
|
}
|
|
7100
|
-
|
|
7225
|
+
ts27.forEachChild(node, collectLiterals);
|
|
7101
7226
|
};
|
|
7102
7227
|
for (const source of keptSources) {
|
|
7103
7228
|
collectLiterals(source);
|
|
@@ -7105,7 +7230,7 @@ class Layouts {
|
|
|
7105
7230
|
for (const name of declaredNames(statement)) {
|
|
7106
7231
|
this.takenNames.add(name);
|
|
7107
7232
|
}
|
|
7108
|
-
if ((
|
|
7233
|
+
if ((ts27.isTypeAliasDeclaration(statement) || ts27.isInterfaceDeclaration(statement)) && isExported(statement)) {
|
|
7109
7234
|
const symbol = checker.getSymbolAtLocation(statement.name);
|
|
7110
7235
|
if (symbol) {
|
|
7111
7236
|
this.keptTypes.set(checker.getDeclaredTypeOfSymbol(symbol), {
|
|
@@ -7121,7 +7246,11 @@ class Layouts {
|
|
|
7121
7246
|
const count = this.names.get(base) ?? 0;
|
|
7122
7247
|
this.names.set(base, count + 1);
|
|
7123
7248
|
const name = count ? `${base}_${count + 1}` : base;
|
|
7124
|
-
|
|
7249
|
+
if (this.declarations.has(name) || this.takenNames.has(name) || this.moduleNames.importedAs(name)) {
|
|
7250
|
+
return this.unique(base);
|
|
7251
|
+
}
|
|
7252
|
+
this.moduleNames.declare(name);
|
|
7253
|
+
return name;
|
|
7125
7254
|
}
|
|
7126
7255
|
declare(base, body, origin) {
|
|
7127
7256
|
const name = this.unique(base);
|
|
@@ -7197,7 +7326,7 @@ class Layouts {
|
|
|
7197
7326
|
}
|
|
7198
7327
|
const variants = type.types.filter((part) => !isAbsent(part));
|
|
7199
7328
|
const [first, ...others] = variants;
|
|
7200
|
-
if (!first || !others.length || variants.some((part) => !(part.flags &
|
|
7329
|
+
if (!first || !others.length || variants.some((part) => !(part.flags & ts27.TypeFlags.Object))) {
|
|
7201
7330
|
return;
|
|
7202
7331
|
}
|
|
7203
7332
|
const literal = (variant, name) => {
|
|
@@ -7267,17 +7396,17 @@ class Layouts {
|
|
|
7267
7396
|
if (alias && iecNames.has(alias)) {
|
|
7268
7397
|
return alias;
|
|
7269
7398
|
}
|
|
7270
|
-
if (type.flags &
|
|
7399
|
+
if (type.flags & ts27.TypeFlags.BooleanLike) {
|
|
7271
7400
|
return "boolean";
|
|
7272
7401
|
}
|
|
7273
7402
|
const enumDeclaration = this.enumDeclaration(type);
|
|
7274
7403
|
if (enumDeclaration) {
|
|
7275
7404
|
return this.enumName(enumDeclaration);
|
|
7276
7405
|
}
|
|
7277
|
-
if (type.flags &
|
|
7406
|
+
if (type.flags & ts27.TypeFlags.NumberLike) {
|
|
7278
7407
|
return fractional(value) ? defaultNumericTypes.float : "number";
|
|
7279
7408
|
}
|
|
7280
|
-
if (type.flags &
|
|
7409
|
+
if (type.flags & ts27.TypeFlags.BigIntLike) {
|
|
7281
7410
|
return defaultNumericTypes.bigint;
|
|
7282
7411
|
}
|
|
7283
7412
|
const literal = this.literalEnum(type);
|
|
@@ -7292,7 +7421,7 @@ class Layouts {
|
|
|
7292
7421
|
if (named) {
|
|
7293
7422
|
return named;
|
|
7294
7423
|
}
|
|
7295
|
-
if (type.flags &
|
|
7424
|
+
if (type.flags & ts27.TypeFlags.StringLike) {
|
|
7296
7425
|
return "string";
|
|
7297
7426
|
}
|
|
7298
7427
|
if (type.isUnion()) {
|
|
@@ -7309,7 +7438,7 @@ class Layouts {
|
|
|
7309
7438
|
if (record) {
|
|
7310
7439
|
return record;
|
|
7311
7440
|
}
|
|
7312
|
-
if (type.flags &
|
|
7441
|
+
if (type.flags & ts27.TypeFlags.Object) {
|
|
7313
7442
|
return this.record(type, value, hint);
|
|
7314
7443
|
}
|
|
7315
7444
|
throw new Error(`${checker.typeToString(type)} has no PLC layout.`);
|
|
@@ -7317,16 +7446,16 @@ class Layouts {
|
|
|
7317
7446
|
enumDeclaration(type) {
|
|
7318
7447
|
const parts = unionMembers(type);
|
|
7319
7448
|
const [first] = parts;
|
|
7320
|
-
if (!first || !parts.every((part) => part.flags &
|
|
7449
|
+
if (!first || !parts.every((part) => part.flags & ts27.TypeFlags.EnumLike)) {
|
|
7321
7450
|
return;
|
|
7322
7451
|
}
|
|
7323
7452
|
const symbol = first.symbol;
|
|
7324
|
-
const declaration = symbol?.declarations?.find(
|
|
7325
|
-
return declaration &&
|
|
7453
|
+
const declaration = symbol?.declarations?.find(ts27.isEnumDeclaration) ?? symbol?.valueDeclaration?.parent;
|
|
7454
|
+
return declaration && ts27.isEnumDeclaration(declaration) ? declaration : undefined;
|
|
7326
7455
|
}
|
|
7327
7456
|
iecName(declaration) {
|
|
7328
7457
|
const node = annotation(declaration);
|
|
7329
|
-
const awaits = declaration !== undefined &&
|
|
7458
|
+
const awaits = declaration !== undefined && ts27.canHaveModifiers(declaration) && hasModifier(declaration, ts27.SyntaxKind.AsyncKeyword);
|
|
7330
7459
|
return iecTypeName(node && awaits ? promisedType(node) : node);
|
|
7331
7460
|
}
|
|
7332
7461
|
elementIec(declaration) {
|
|
@@ -7408,7 +7537,7 @@ class Layouts {
|
|
|
7408
7537
|
if (!symbol || !declaration) {
|
|
7409
7538
|
return;
|
|
7410
7539
|
}
|
|
7411
|
-
if (!
|
|
7540
|
+
if (!ts27.isTypeAliasDeclaration(declaration) && !ts27.isInterfaceDeclaration(declaration)) {
|
|
7412
7541
|
return;
|
|
7413
7542
|
}
|
|
7414
7543
|
const file = declaration.getSourceFile().fileName;
|
|
@@ -7418,11 +7547,11 @@ class Layouts {
|
|
|
7418
7547
|
return this.typeImports.add(file, symbol.name);
|
|
7419
7548
|
}
|
|
7420
7549
|
enumName(declaration) {
|
|
7421
|
-
const
|
|
7422
|
-
if (
|
|
7423
|
-
throw new
|
|
7550
|
+
const refusal = this.enumRefusal(declaration);
|
|
7551
|
+
if (refusal) {
|
|
7552
|
+
throw new SpecializeError(declaration.name, refusal);
|
|
7424
7553
|
}
|
|
7425
|
-
return this.imports.add(
|
|
7554
|
+
return this.imports.add(declaration.getSourceFile().fileName, declaration.name.text);
|
|
7426
7555
|
}
|
|
7427
7556
|
enumRecord(type) {
|
|
7428
7557
|
const record = recordTypes(type);
|
|
@@ -7479,7 +7608,7 @@ class Layouts {
|
|
|
7479
7608
|
const captured = (name) => node && (node.captured.kind === "object" || node.captured.kind === "instance") ? node.captured.fields[name] : undefined;
|
|
7480
7609
|
const body = this.recordBody(type, captured);
|
|
7481
7610
|
const symbolName = type.aliasSymbol?.name ?? type.getSymbol()?.name;
|
|
7482
|
-
const base = symbolName ===
|
|
7611
|
+
const base = symbolName === ts27.InternalSymbolName.Type || symbolName === ts27.InternalSymbolName.Object ? undefined : symbolName;
|
|
7483
7612
|
const origin = (type.aliasSymbol ?? type.getSymbol())?.declarations?.[0];
|
|
7484
7613
|
if (base === undefined && this.likeKeptLiteral(type, origin, body, captured)) {
|
|
7485
7614
|
return `{ ${body} }`;
|
|
@@ -7495,7 +7624,7 @@ class Layouts {
|
|
|
7495
7624
|
return name;
|
|
7496
7625
|
}
|
|
7497
7626
|
likeKeptLiteral(type, origin, body, captured) {
|
|
7498
|
-
if (origin &&
|
|
7627
|
+
if (origin && ts27.isTypeLiteralNode(origin) && this.kept(origin.getSourceFile().fileName)) {
|
|
7499
7628
|
return true;
|
|
7500
7629
|
}
|
|
7501
7630
|
const names = fieldNames(this.checker, type);
|
|
@@ -7505,11 +7634,16 @@ class Layouts {
|
|
|
7505
7634
|
});
|
|
7506
7635
|
}
|
|
7507
7636
|
recordBody(type, captured) {
|
|
7508
|
-
return this.checker.getPropertiesOfType(type).map((property) =>
|
|
7637
|
+
return this.checker.getPropertiesOfType(type).map((property) => {
|
|
7638
|
+
if (!plainName(property.name)) {
|
|
7639
|
+
throw quotedField(property.valueDeclaration, property.name);
|
|
7640
|
+
}
|
|
7641
|
+
return `${property.name}: ${this.declaredText(property.valueDeclaration, this.checker.getTypeOfSymbol(property), captured(property.name) ?? this.fieldSample(property), pascalCase2(property.name))};`;
|
|
7642
|
+
}).join(" ");
|
|
7509
7643
|
}
|
|
7510
7644
|
}
|
|
7511
7645
|
var stringEnums = (sources, checker) => sources.flatMap((source) => source.statements.flatMap((statement) => {
|
|
7512
|
-
if (!
|
|
7646
|
+
if (!ts27.isEnumDeclaration(statement) || !statement.members.some((member) => typeof checker.getConstantValue(member) === "string")) {
|
|
7513
7647
|
return [];
|
|
7514
7648
|
}
|
|
7515
7649
|
const values = statement.members.map((member) => checker.getConstantValue(member));
|
|
@@ -7519,10 +7653,10 @@ var stringEnums = (sources, checker) => sources.flatMap((source) => source.state
|
|
|
7519
7653
|
];
|
|
7520
7654
|
}));
|
|
7521
7655
|
var declaredNames = (statement) => {
|
|
7522
|
-
if (
|
|
7523
|
-
return statement.declarationList.declarations.flatMap((declaration) =>
|
|
7656
|
+
if (ts27.isVariableStatement(statement)) {
|
|
7657
|
+
return statement.declarationList.declarations.flatMap((declaration) => ts27.isIdentifier(declaration.name) ? [declaration.name.text] : []);
|
|
7524
7658
|
}
|
|
7525
|
-
if ((
|
|
7659
|
+
if ((ts27.isTypeAliasDeclaration(statement) || ts27.isInterfaceDeclaration(statement) || ts27.isEnumDeclaration(statement) || ts27.isClassDeclaration(statement) || ts27.isFunctionDeclaration(statement)) && statement.name) {
|
|
7526
7660
|
return [statement.name.text];
|
|
7527
7661
|
}
|
|
7528
7662
|
return [];
|
|
@@ -7534,23 +7668,23 @@ var recordTypes = (type) => {
|
|
|
7534
7668
|
const [key, value] = type.aliasTypeArguments ?? [];
|
|
7535
7669
|
return type.aliasSymbol?.name === "Record" && key && value ? { key, value } : undefined;
|
|
7536
7670
|
};
|
|
7537
|
-
var annotation = (declaration) => declaration && (
|
|
7671
|
+
var annotation = (declaration) => declaration && (ts27.isVariableDeclaration(declaration) || ts27.isPropertyDeclaration(declaration) || ts27.isPropertySignature(declaration) || ts27.isParameter(declaration) || ts27.isFunctionLike(declaration)) ? declaration.type : undefined;
|
|
7538
7672
|
var arrayElement = (node) => {
|
|
7539
|
-
if (node &&
|
|
7673
|
+
if (node && ts27.isArrayTypeNode(node)) {
|
|
7540
7674
|
return node.elementType;
|
|
7541
7675
|
}
|
|
7542
|
-
if (node &&
|
|
7676
|
+
if (node && ts27.isTypeReferenceNode(node) && ["Array", "ReadonlyArray", "FixedArray"].includes(node.typeName.getText())) {
|
|
7543
7677
|
return node.typeArguments?.[0];
|
|
7544
7678
|
}
|
|
7545
7679
|
return;
|
|
7546
7680
|
};
|
|
7547
|
-
var recordValue = (node) => node &&
|
|
7681
|
+
var recordValue = (node) => node && ts27.isTypeReferenceNode(node) && node.typeName.getText() === "Record" && node.typeArguments?.length === 2 ? node.typeArguments[1] : undefined;
|
|
7548
7682
|
var iecTypeName = (node) => {
|
|
7549
|
-
if (node &&
|
|
7550
|
-
const [defined, ...others] = node.types.filter((member) => !(member.kind ===
|
|
7683
|
+
if (node && ts27.isUnionTypeNode(node)) {
|
|
7684
|
+
const [defined, ...others] = node.types.filter((member) => !(member.kind === ts27.SyntaxKind.UndefinedKeyword || ts27.isLiteralTypeNode(member) && member.literal.kind === ts27.SyntaxKind.NullKeyword));
|
|
7551
7685
|
return others.length ? undefined : iecTypeName(defined);
|
|
7552
7686
|
}
|
|
7553
|
-
if (!node || !
|
|
7687
|
+
if (!node || !ts27.isTypeReferenceNode(node)) {
|
|
7554
7688
|
return;
|
|
7555
7689
|
}
|
|
7556
7690
|
const name = node.typeName.getText();
|
|
@@ -7558,34 +7692,484 @@ var iecTypeName = (node) => {
|
|
|
7558
7692
|
if (!length) {
|
|
7559
7693
|
return iecNames.has(name) ? name : undefined;
|
|
7560
7694
|
}
|
|
7561
|
-
return (name === "STRING" || name === "WSTRING") && !rest.length &&
|
|
7695
|
+
return (name === "STRING" || name === "WSTRING") && !rest.length && ts27.isLiteralTypeNode(length) && ts27.isNumericLiteral(length.literal) ? `${name}<${length.literal.text}>` : undefined;
|
|
7562
7696
|
};
|
|
7563
7697
|
var isReadonlyArray = (type) => type.getSymbol()?.name === "ReadonlyArray";
|
|
7564
7698
|
var enumKey = (members) => [...members].sort().join("|");
|
|
7565
|
-
var
|
|
7566
|
-
|
|
7567
|
-
|
|
7568
|
-
}
|
|
7569
|
-
|
|
7570
|
-
const name = `${base}_${index}`;
|
|
7571
|
-
if (!taken(name)) {
|
|
7572
|
-
return name;
|
|
7573
|
-
}
|
|
7574
|
-
}
|
|
7699
|
+
var plainName = (name) => /^(\d+|[\p{ID_Start}_$][\p{ID_Continue}$]*)$/u.test(name);
|
|
7700
|
+
var quotedField = (declaration, name) => {
|
|
7701
|
+
const plain = name.replace(/[^A-Za-z0-9_$]+(.)?/g, (_, next) => next ? next.toUpperCase() : "").replace(/^\d/, "_$&");
|
|
7702
|
+
const message = `"${name}" can't name a field the PLC stores, which needs a plain name such as ${plain || "field"}. Rename the field.`;
|
|
7703
|
+
return declaration ? new SpecializeError(declaration, message) : new Error(message);
|
|
7575
7704
|
};
|
|
7576
7705
|
var literalMember = (value) => {
|
|
7577
7706
|
const name = pascalCase2(value);
|
|
7578
7707
|
return /^[A-Za-z_]/.test(name) ? name : `_${name}`;
|
|
7579
7708
|
};
|
|
7580
|
-
var isAbsent = (type) => Boolean(type.flags & (
|
|
7709
|
+
var isAbsent = (type) => Boolean(type.flags & (ts27.TypeFlags.Undefined | ts27.TypeFlags.Null));
|
|
7581
7710
|
var bothAbsent = (checker, type) => {
|
|
7582
|
-
if (type.isUnion() && type.types.some((part) => part.flags &
|
|
7711
|
+
if (type.isUnion() && type.types.some((part) => part.flags & ts27.TypeFlags.Null) && type.types.some((part) => part.flags & ts27.TypeFlags.Undefined)) {
|
|
7583
7712
|
throw new Error(`${checker.typeToString(type)}: null and undefined are one empty value in the PLC; use one of them.`);
|
|
7584
7713
|
}
|
|
7585
7714
|
};
|
|
7586
7715
|
|
|
7716
|
+
// packages/compiler/src/frontend/elaborate/reduce.ts
|
|
7717
|
+
import ts28 from "typescript";
|
|
7718
|
+
var constantInitializer = (node, checker, sdk) => {
|
|
7719
|
+
if (ts28.isNumericLiteral(node) || ts28.isStringLiteral(node) || ts28.isNoSubstitutionTemplateLiteral(node) || ts28.isBigIntLiteral(node) || node.kind === ts28.SyntaxKind.TrueKeyword || node.kind === ts28.SyntaxKind.FalseKeyword) {
|
|
7720
|
+
return true;
|
|
7721
|
+
}
|
|
7722
|
+
if (ts28.isPrefixUnaryExpression(node)) {
|
|
7723
|
+
return constantInitializer(node.operand, checker, sdk);
|
|
7724
|
+
}
|
|
7725
|
+
if (ts28.isAsExpression(node) || ts28.isSatisfiesExpression(node) || ts28.isParenthesizedExpression(node)) {
|
|
7726
|
+
return constantInitializer(node.expression, checker, sdk);
|
|
7727
|
+
}
|
|
7728
|
+
if (ts28.isPropertyAccessExpression(node)) {
|
|
7729
|
+
return ts28.isIdentifier(node.expression);
|
|
7730
|
+
}
|
|
7731
|
+
if (ts28.isIdentifier(node)) {
|
|
7732
|
+
return true;
|
|
7733
|
+
}
|
|
7734
|
+
if (ts28.isObjectLiteralExpression(node)) {
|
|
7735
|
+
return node.properties.every((property) => ts28.isPropertyAssignment(property) && constantInitializer(property.initializer, checker, sdk) || ts28.isShorthandPropertyAssignment(property));
|
|
7736
|
+
}
|
|
7737
|
+
if (ts28.isArrayLiteralExpression(node)) {
|
|
7738
|
+
return node.elements.every((element) => constantInitializer(element, checker, sdk));
|
|
7739
|
+
}
|
|
7740
|
+
if (ts28.isCallExpression(node)) {
|
|
7741
|
+
return namesSdk(checker, sdk, node.expression, "createServiceClient") || definesTask(node, checker, sdk);
|
|
7742
|
+
}
|
|
7743
|
+
return false;
|
|
7744
|
+
};
|
|
7745
|
+
var definesTask = (call, checker, sdk) => ts28.isPropertyAccessExpression(call.expression) && call.expression.name.text === "define" && (namesSdk(checker, sdk, call.expression.expression, "tasks") || namesSdk(checker, sdk, call.expression.expression, "backgroundTasks"));
|
|
7746
|
+
var serviceClients = (source, checker, sdk) => source.statements.filter((statement) => ts28.isVariableStatement(statement) && statement.declarationList.declarations.every((declaration) => declaration.initializer && ts28.isCallExpression(declaration.initializer) && namesSdk(checker, sdk, declaration.initializer.expression, "createServiceClient")));
|
|
7747
|
+
var importsSdk = (statement) => {
|
|
7748
|
+
const specifier = statement.moduleSpecifier;
|
|
7749
|
+
return ts28.isStringLiteral(specifier) && Object.values(sdkModules).some((module) => module === specifier.text);
|
|
7750
|
+
};
|
|
7751
|
+
var constantStatement = (declaration) => {
|
|
7752
|
+
if (!declaration || !ts28.isVariableDeclaration(declaration) || !ts28.isIdentifier(declaration.name) || !(declaration.parent.flags & ts28.NodeFlags.Const)) {
|
|
7753
|
+
return;
|
|
7754
|
+
}
|
|
7755
|
+
const statement = declaration.parent.parent;
|
|
7756
|
+
return ts28.isVariableStatement(statement) && ts28.isSourceFile(statement.parent) ? statement : undefined;
|
|
7757
|
+
};
|
|
7758
|
+
var literalType = (checker, type) => {
|
|
7759
|
+
if (type.isLiteral() || type.flags & ts28.TypeFlags.BooleanLiteral) {
|
|
7760
|
+
return true;
|
|
7761
|
+
}
|
|
7762
|
+
if (!(type.flags & ts28.TypeFlags.Object) || type.getCallSignatures().length || type.getConstructSignatures().length) {
|
|
7763
|
+
return false;
|
|
7764
|
+
}
|
|
7765
|
+
if (checker.isTupleType(type)) {
|
|
7766
|
+
return checker.getTypeArguments(type).every((item) => literalType(checker, item));
|
|
7767
|
+
}
|
|
7768
|
+
const properties = type.getProperties();
|
|
7769
|
+
return properties.length > 0 && !type.getStringIndexType() && !type.getNumberIndexType() && properties.every((property) => {
|
|
7770
|
+
const declaration = property.valueDeclaration;
|
|
7771
|
+
return declaration !== undefined && ts28.isPropertyAssignment(declaration) && literalType(checker, checker.getTypeOfSymbol(property));
|
|
7772
|
+
});
|
|
7773
|
+
};
|
|
7774
|
+
var literalConstant = (checker, symbol) => {
|
|
7775
|
+
const declaration = symbol.valueDeclaration;
|
|
7776
|
+
return declaration && ts28.isVariableDeclaration(declaration) && constantStatement(declaration) && literalType(checker, checker.getTypeAtLocation(declaration.name)) ? declaration : undefined;
|
|
7777
|
+
};
|
|
7778
|
+
var readNames = (node) => {
|
|
7779
|
+
const found = [];
|
|
7780
|
+
const visit = (inner) => {
|
|
7781
|
+
if (ts28.isTypeNode(inner)) {
|
|
7782
|
+
return;
|
|
7783
|
+
}
|
|
7784
|
+
if (ts28.isIdentifier(inner)) {
|
|
7785
|
+
found.push(inner);
|
|
7786
|
+
}
|
|
7787
|
+
ts28.forEachChild(inner, visit);
|
|
7788
|
+
};
|
|
7789
|
+
visit(node);
|
|
7790
|
+
return found;
|
|
7791
|
+
};
|
|
7792
|
+
var allNames = (node) => {
|
|
7793
|
+
const found = [];
|
|
7794
|
+
const visit = (inner) => {
|
|
7795
|
+
if (ts28.isIdentifier(inner)) {
|
|
7796
|
+
found.push(inner);
|
|
7797
|
+
}
|
|
7798
|
+
ts28.forEachChild(inner, visit);
|
|
7799
|
+
};
|
|
7800
|
+
visit(node);
|
|
7801
|
+
return found;
|
|
7802
|
+
};
|
|
7803
|
+
|
|
7804
|
+
class Reduction {
|
|
7805
|
+
checker;
|
|
7806
|
+
sdk;
|
|
7807
|
+
options;
|
|
7808
|
+
needed = new Set;
|
|
7809
|
+
classes = new Set;
|
|
7810
|
+
plain = new Map;
|
|
7811
|
+
cutDown;
|
|
7812
|
+
entryDeclares = new Set;
|
|
7813
|
+
runtime;
|
|
7814
|
+
constructor(checker, sdk, options) {
|
|
7815
|
+
this.checker = checker;
|
|
7816
|
+
this.sdk = sdk;
|
|
7817
|
+
this.options = options;
|
|
7818
|
+
const { runtime, kept, entry } = options;
|
|
7819
|
+
this.runtime = new Set(runtime);
|
|
7820
|
+
this.cutDown = new Set(runtime.filter((source) => source !== entry && !kept(source.fileName)));
|
|
7821
|
+
for (const source of runtime) {
|
|
7822
|
+
if (!kept(source.fileName)) {
|
|
7823
|
+
continue;
|
|
7824
|
+
}
|
|
7825
|
+
for (const statement of source.statements) {
|
|
7826
|
+
if (!ts28.isImportDeclaration(statement) && !ts28.isExportDeclaration(statement)) {
|
|
7827
|
+
this.need(statement);
|
|
7828
|
+
}
|
|
7829
|
+
}
|
|
7830
|
+
}
|
|
7831
|
+
for (const source of runtime) {
|
|
7832
|
+
if (kept(source.fileName)) {
|
|
7833
|
+
this.declareClasses(source);
|
|
7834
|
+
}
|
|
7835
|
+
if (!this.cutDown.has(source)) {
|
|
7836
|
+
continue;
|
|
7837
|
+
}
|
|
7838
|
+
for (const statement of source.statements) {
|
|
7839
|
+
if (ts28.isInterfaceDeclaration(statement) || ts28.isTypeAliasDeclaration(statement) || this.keptWritten(statement)) {
|
|
7840
|
+
this.declareClasses(statement);
|
|
7841
|
+
}
|
|
7842
|
+
}
|
|
7843
|
+
}
|
|
7844
|
+
}
|
|
7845
|
+
get modules() {
|
|
7846
|
+
return [...this.cutDown];
|
|
7847
|
+
}
|
|
7848
|
+
need(node) {
|
|
7849
|
+
for (const name of readNames(node)) {
|
|
7850
|
+
const symbol = valueSymbol(this.checker, name);
|
|
7851
|
+
const declaration = symbol && unaliased(this.checker, symbol).valueDeclaration;
|
|
7852
|
+
if (declaration && ts28.isVariableDeclaration(declaration) && this.cutDown.has(declaration.getSourceFile()) && !this.needed.has(declaration) && this.staysWritten(declaration)) {
|
|
7853
|
+
const list = declaration.parent;
|
|
7854
|
+
for (const each of ts28.isVariableDeclarationList(list) ? list.declarations : []) {
|
|
7855
|
+
this.needed.add(each);
|
|
7856
|
+
if (each.initializer) {
|
|
7857
|
+
this.need(each.initializer);
|
|
7858
|
+
}
|
|
7859
|
+
}
|
|
7860
|
+
}
|
|
7861
|
+
}
|
|
7862
|
+
}
|
|
7863
|
+
staysWritten(declaration) {
|
|
7864
|
+
const known = this.plain.get(declaration);
|
|
7865
|
+
if (known !== undefined) {
|
|
7866
|
+
return known;
|
|
7867
|
+
}
|
|
7868
|
+
this.plain.set(declaration, true);
|
|
7869
|
+
const statement = constantStatement(declaration);
|
|
7870
|
+
const stays = Boolean(statement?.declarationList.declarations.every((each) => this.dataOnly(each)));
|
|
7871
|
+
this.plain.set(declaration, stays);
|
|
7872
|
+
return stays;
|
|
7873
|
+
}
|
|
7874
|
+
dataOnly(declaration) {
|
|
7875
|
+
const { checker, sdk } = this;
|
|
7876
|
+
const { written, bound } = this.options;
|
|
7877
|
+
const symbol = checker.getSymbolAtLocation(declaration.name);
|
|
7878
|
+
const initializer = declaration.initializer;
|
|
7879
|
+
const calls = (node) => ts28.isCallExpression(node) || Boolean(ts28.forEachChild(node, calls));
|
|
7880
|
+
return ts28.isIdentifier(declaration.name) && initializer !== undefined && constantInitializer(initializer, checker, sdk) && !calls(initializer) && !(symbol && written.has(symbol)) && !bound.has(declaration.name.text) && readNames(initializer).every((name) => this.readable(name));
|
|
7881
|
+
}
|
|
7882
|
+
readable(name) {
|
|
7883
|
+
if (ts28.isPropertyAccessExpression(name.parent) && name.parent.name === name || ts28.isPropertyAssignment(name.parent) && name.parent.name === name) {
|
|
7884
|
+
return true;
|
|
7885
|
+
}
|
|
7886
|
+
const symbol = valueSymbol(this.checker, name);
|
|
7887
|
+
const declaration = symbol && unaliased(this.checker, symbol).valueDeclaration;
|
|
7888
|
+
if (!declaration) {
|
|
7889
|
+
return true;
|
|
7890
|
+
}
|
|
7891
|
+
const source = declaration.getSourceFile();
|
|
7892
|
+
if (!this.runtime.has(source) || this.options.kept(source.fileName)) {
|
|
7893
|
+
return true;
|
|
7894
|
+
}
|
|
7895
|
+
if (ts28.isEnumDeclaration(declaration)) {
|
|
7896
|
+
return source !== this.options.entry && this.keepsEnum(declaration);
|
|
7897
|
+
}
|
|
7898
|
+
return ts28.isVariableDeclaration(declaration) && this.cutDown.has(source) && this.staysWritten(declaration);
|
|
7899
|
+
}
|
|
7900
|
+
keptWritten(statement) {
|
|
7901
|
+
return ts28.isVariableStatement(statement) && statement.declarationList.declarations.some((declaration) => this.needed.has(declaration));
|
|
7902
|
+
}
|
|
7903
|
+
declareClasses(node) {
|
|
7904
|
+
for (const name of allNames(node)) {
|
|
7905
|
+
const symbol = this.checker.getSymbolAtLocation(name);
|
|
7906
|
+
this.declareClass(symbol && unaliased(this.checker, symbol));
|
|
7907
|
+
}
|
|
7908
|
+
}
|
|
7909
|
+
declareClass(symbol) {
|
|
7910
|
+
const declaration = symbol?.declarations?.find(ts28.isClassDeclaration);
|
|
7911
|
+
if (!symbol || !declaration?.name || !this.cutDown.has(declaration.getSourceFile()) || this.classes.has(declaration)) {
|
|
7912
|
+
return;
|
|
7913
|
+
}
|
|
7914
|
+
this.classes.add(declaration);
|
|
7915
|
+
const instance = this.checker.getDeclaredTypeOfSymbol(symbol);
|
|
7916
|
+
for (const property of this.checker.getPropertiesOfType(instance)) {
|
|
7917
|
+
const member = property.valueDeclaration;
|
|
7918
|
+
if (member && !(ts28.getCombinedModifierFlags(member) & ts28.ModifierFlags.Private)) {
|
|
7919
|
+
this.declareTypeClasses(this.checker.getTypeOfSymbolAtLocation(property, declaration), new Set);
|
|
7920
|
+
}
|
|
7921
|
+
}
|
|
7922
|
+
}
|
|
7923
|
+
declareTypeClasses(type, seen) {
|
|
7924
|
+
if (seen.has(type) || seen.size > 64) {
|
|
7925
|
+
return;
|
|
7926
|
+
}
|
|
7927
|
+
seen.add(type);
|
|
7928
|
+
this.declareClass(type.getSymbol());
|
|
7929
|
+
const parts = [
|
|
7930
|
+
...type.isUnionOrIntersection() ? type.types : [],
|
|
7931
|
+
...type.aliasTypeArguments ?? [],
|
|
7932
|
+
...type.flags & ts28.TypeFlags.Object && type.objectFlags & ts28.ObjectFlags.Reference ? this.checker.getTypeArguments(type) : []
|
|
7933
|
+
];
|
|
7934
|
+
for (const part of parts) {
|
|
7935
|
+
this.declareTypeClasses(part, seen);
|
|
7936
|
+
}
|
|
7937
|
+
}
|
|
7938
|
+
keepsEnum(declaration) {
|
|
7939
|
+
const source = declaration.getSourceFile();
|
|
7940
|
+
if (!this.runtime.has(source) || this.options.kept(source.fileName)) {
|
|
7941
|
+
return true;
|
|
7942
|
+
}
|
|
7943
|
+
const fixed = declaration.members.every((member) => !member.initializer || readNames(member.initializer).every((name) => {
|
|
7944
|
+
const symbol = this.checker.getSymbolAtLocation(name);
|
|
7945
|
+
return symbol !== undefined && Boolean(symbol.flags & ts28.SymbolFlags.EnumMember);
|
|
7946
|
+
}));
|
|
7947
|
+
return fixed && (source !== this.options.entry || this.exportedByName(declaration));
|
|
7948
|
+
}
|
|
7949
|
+
sharesEnum(declaration) {
|
|
7950
|
+
return this.keepsEnum(declaration) && this.exportedByName(declaration);
|
|
7951
|
+
}
|
|
7952
|
+
enumRefusal(declaration) {
|
|
7953
|
+
const name = declaration.name.text;
|
|
7954
|
+
return this.exportedByName(declaration) ? `${name}: give each member of this enum a literal value. The generated code stores the enum and imports it from its module, where a member computed from a constant can't stay.` : `${name}: export this enum from its module. The generated code stores it, and imports it from there.`;
|
|
7955
|
+
}
|
|
7956
|
+
exportedByName(declaration) {
|
|
7957
|
+
const module = this.checker.getSymbolAtLocation(declaration.getSourceFile());
|
|
7958
|
+
const symbol = this.checker.getSymbolAtLocation(declaration.name);
|
|
7959
|
+
const exported = module && this.checker.getExportsOfModule(module).find((candidate) => candidate.name === declaration.name.text);
|
|
7960
|
+
return isExported(declaration) || exported !== undefined && symbol !== undefined && unaliased(this.checker, exported) === symbol;
|
|
7961
|
+
}
|
|
7962
|
+
survives(symbol) {
|
|
7963
|
+
const declaration = symbol.valueDeclaration;
|
|
7964
|
+
if (!(symbol.flags & ts28.SymbolFlags.Value) || !declaration) {
|
|
7965
|
+
return true;
|
|
7966
|
+
}
|
|
7967
|
+
const source = declaration.getSourceFile();
|
|
7968
|
+
if (!this.runtime.has(source) || this.options.kept(source.fileName)) {
|
|
7969
|
+
return true;
|
|
7970
|
+
}
|
|
7971
|
+
if (source === this.options.entry) {
|
|
7972
|
+
return this.exportedNames(symbol).some((name) => this.entryDeclares.has(name));
|
|
7973
|
+
}
|
|
7974
|
+
if (ts28.isEnumDeclaration(declaration)) {
|
|
7975
|
+
return this.keepsEnum(declaration);
|
|
7976
|
+
}
|
|
7977
|
+
if (ts28.isClassDeclaration(declaration)) {
|
|
7978
|
+
return this.classes.has(declaration);
|
|
7979
|
+
}
|
|
7980
|
+
return ts28.isVariableDeclaration(declaration) && (this.needed.has(declaration) || literalConstant(this.checker, symbol) !== undefined || serviceClients(source, this.checker, this.sdk).includes(declaration.parent.parent));
|
|
7981
|
+
}
|
|
7982
|
+
exportedNames(symbol) {
|
|
7983
|
+
const module = this.checker.getSymbolAtLocation(this.options.entry);
|
|
7984
|
+
return (module ? this.checker.getExportsOfModule(module) : []).filter((exported) => unaliased(this.checker, exported) === symbol).map((exported) => exported.name);
|
|
7985
|
+
}
|
|
7986
|
+
declaredByEntry(names) {
|
|
7987
|
+
this.entryDeclares = names;
|
|
7988
|
+
}
|
|
7989
|
+
compiled(source) {
|
|
7990
|
+
return new Set([...this.needed].filter((declaration) => declaration.getSourceFile() === source).map((declaration) => declaration.name.getText()));
|
|
7991
|
+
}
|
|
7992
|
+
module(source) {
|
|
7993
|
+
const { checker, sdk } = this;
|
|
7994
|
+
const clients = serviceClients(source, checker, sdk);
|
|
7995
|
+
const read = new Set(source.statements.filter((statement) => this.keptWritten(statement)).flatMap(readNames).flatMap((name) => valueSymbol(checker, name) ?? []));
|
|
7996
|
+
return joined([
|
|
7997
|
+
...source.statements.flatMap((statement) => {
|
|
7998
|
+
if (ts28.isInterfaceDeclaration(statement) || ts28.isTypeAliasDeclaration(statement) || ts28.isEnumDeclaration(statement) && this.keepsEnum(statement) || clients.includes(statement) || this.keptWritten(statement)) {
|
|
7999
|
+
return [copied(statement)];
|
|
8000
|
+
}
|
|
8001
|
+
if (ts28.isClassDeclaration(statement)) {
|
|
8002
|
+
return this.classes.has(statement) ? [from(statement, this.declaredClass(statement))] : [];
|
|
8003
|
+
}
|
|
8004
|
+
if (ts28.isImportDeclaration(statement)) {
|
|
8005
|
+
return statement.importClause?.isTypeOnly || clients.length > 0 && importsSdk(statement) ? [copied(statement)] : this.imports(statement, read);
|
|
8006
|
+
}
|
|
8007
|
+
if (ts28.isExportDeclaration(statement) && statement.moduleSpecifier) {
|
|
8008
|
+
return this.reExports(statement);
|
|
8009
|
+
}
|
|
8010
|
+
return [];
|
|
8011
|
+
}),
|
|
8012
|
+
...this.listedExports(source, (declaration) => this.stays(declaration, clients)).lines,
|
|
8013
|
+
"export {};"
|
|
8014
|
+
], `
|
|
8015
|
+
`);
|
|
8016
|
+
}
|
|
8017
|
+
stays(declaration, clients) {
|
|
8018
|
+
if (ts28.isInterfaceDeclaration(declaration) || ts28.isTypeAliasDeclaration(declaration)) {
|
|
8019
|
+
return true;
|
|
8020
|
+
}
|
|
8021
|
+
if (ts28.isEnumDeclaration(declaration)) {
|
|
8022
|
+
return this.keepsEnum(declaration);
|
|
8023
|
+
}
|
|
8024
|
+
if (ts28.isClassDeclaration(declaration)) {
|
|
8025
|
+
return this.classes.has(declaration);
|
|
8026
|
+
}
|
|
8027
|
+
return ts28.isVariableDeclaration(declaration) && (this.needed.has(declaration) || clients.includes(declaration.parent.parent));
|
|
8028
|
+
}
|
|
8029
|
+
entry() {
|
|
8030
|
+
const source = this.options.entry;
|
|
8031
|
+
const enums = source.statements.filter(ts28.isEnumDeclaration).filter((statement) => this.sharesEnum(statement));
|
|
8032
|
+
const text = (statement) => isExported(statement) ? copied(statement) : joined(["export ", copied(statement)], "");
|
|
8033
|
+
return {
|
|
8034
|
+
enums: enums.map((statement) => ({
|
|
8035
|
+
name: statement.name.text,
|
|
8036
|
+
line: text(statement)
|
|
8037
|
+
})),
|
|
8038
|
+
constants: this.listedExports(source, () => false).declared
|
|
8039
|
+
};
|
|
8040
|
+
}
|
|
8041
|
+
listedExports(source, stays) {
|
|
8042
|
+
const { checker } = this;
|
|
8043
|
+
const module = checker.getSymbolAtLocation(source);
|
|
8044
|
+
const lines = [];
|
|
8045
|
+
const declared = [];
|
|
8046
|
+
for (const exported of module ? checker.getExportsOfModule(module) : []) {
|
|
8047
|
+
const here = exported.declarations?.find((declaration) => declaration.getSourceFile() === source && !(ts28.isExportSpecifier(declaration) && declaration.parent.parent.moduleSpecifier));
|
|
8048
|
+
const target = unaliased(checker, exported);
|
|
8049
|
+
const declaration = target.declarations?.find((candidate) => candidate.getSourceFile() === source && !ts28.isExportSpecifier(candidate) && !ts28.isExportAssignment(candidate));
|
|
8050
|
+
if (!here || !declaration) {
|
|
8051
|
+
continue;
|
|
8052
|
+
}
|
|
8053
|
+
const isDefault = exported.name === "default";
|
|
8054
|
+
if (stays(declaration)) {
|
|
8055
|
+
if (here === declaration && !isDefault) {
|
|
8056
|
+
continue;
|
|
8057
|
+
}
|
|
8058
|
+
if (here === declaration && ts28.isClassDeclaration(declaration)) {
|
|
8059
|
+
continue;
|
|
8060
|
+
}
|
|
8061
|
+
const keyword = target.flags & ts28.SymbolFlags.Value ? "export" : "export type";
|
|
8062
|
+
lines.push(from(here, isDefault ? `export default ${target.name};` : `${keyword} { ${target.name === exported.name ? target.name : `${target.name} as ${exported.name}`} };`));
|
|
8063
|
+
continue;
|
|
8064
|
+
}
|
|
8065
|
+
const constant = literalConstant(checker, target);
|
|
8066
|
+
if (!constant || isDefault && source === this.options.entry) {
|
|
8067
|
+
continue;
|
|
8068
|
+
}
|
|
8069
|
+
const type = checker.typeToString(checker.getTypeAtLocation(constant.name), undefined, ts28.TypeFormatFlags.NoTruncation);
|
|
8070
|
+
const line = from(constant, isDefault ? `declare const tsplcDefault: ${type};
|
|
8071
|
+
export default tsplcDefault;` : `export declare const ${exported.name}: ${type};`);
|
|
8072
|
+
declared.push({ name: exported.name, line });
|
|
8073
|
+
lines.push(line);
|
|
8074
|
+
}
|
|
8075
|
+
return { lines, declared };
|
|
8076
|
+
}
|
|
8077
|
+
imports(statement, read) {
|
|
8078
|
+
const { checker } = this;
|
|
8079
|
+
const clause = statement.importClause;
|
|
8080
|
+
if (!clause) {
|
|
8081
|
+
return [];
|
|
8082
|
+
}
|
|
8083
|
+
const local = (name) => checker.getSymbolAtLocation(name);
|
|
8084
|
+
const surviving = (name) => {
|
|
8085
|
+
const symbol = local(name);
|
|
8086
|
+
return symbol !== undefined && this.survives(unaliased(checker, symbol));
|
|
8087
|
+
};
|
|
8088
|
+
const isRead = (name) => {
|
|
8089
|
+
const symbol = local(name);
|
|
8090
|
+
return symbol !== undefined && read.has(symbol);
|
|
8091
|
+
};
|
|
8092
|
+
const specifier = statement.moduleSpecifier.getText();
|
|
8093
|
+
const bindings = clause.namedBindings;
|
|
8094
|
+
const named = bindings && ts28.isNamedImports(bindings) ? bindings.elements.filter((element) => surviving(element.name)) : [];
|
|
8095
|
+
const list = (elements) => elements.map((element) => element.propertyName ? `${element.propertyName.getText()} as ${element.name.text}` : element.name.text).join(", ");
|
|
8096
|
+
const values = named.filter((element) => !element.isTypeOnly && isRead(element.name));
|
|
8097
|
+
const types = named.filter((element) => !values.includes(element));
|
|
8098
|
+
const lines = [];
|
|
8099
|
+
if (clause.name && surviving(clause.name)) {
|
|
8100
|
+
lines.push(`${isRead(clause.name) ? "import" : "import type"} ${clause.name.text} from ${specifier};`);
|
|
8101
|
+
}
|
|
8102
|
+
if (bindings && ts28.isNamespaceImport(bindings)) {
|
|
8103
|
+
lines.push(`${isRead(bindings.name) ? "import" : "import type"} * as ${bindings.name.text} from ${specifier};`);
|
|
8104
|
+
}
|
|
8105
|
+
if (values.length) {
|
|
8106
|
+
lines.push(`import { ${list(values)} } from ${specifier};`);
|
|
8107
|
+
}
|
|
8108
|
+
if (types.length) {
|
|
8109
|
+
lines.push(`import type { ${list(types)} } from ${specifier};`);
|
|
8110
|
+
}
|
|
8111
|
+
return lines.map((line) => from(statement, line));
|
|
8112
|
+
}
|
|
8113
|
+
reExports(statement) {
|
|
8114
|
+
const clause = statement.exportClause;
|
|
8115
|
+
if (statement.isTypeOnly || !clause || !ts28.isNamedExports(clause)) {
|
|
8116
|
+
return [copied(statement)];
|
|
8117
|
+
}
|
|
8118
|
+
const kept = clause.elements.filter((element) => {
|
|
8119
|
+
const symbol = this.checker.getSymbolAtLocation(element.name);
|
|
8120
|
+
return element.isTypeOnly || symbol !== undefined && this.survives(unaliased(this.checker, symbol));
|
|
8121
|
+
});
|
|
8122
|
+
return kept.length ? [
|
|
8123
|
+
from(statement, `export { ${kept.map((element) => element.getText()).join(", ")} } from ${statement.moduleSpecifier?.getText()};`)
|
|
8124
|
+
] : [];
|
|
8125
|
+
}
|
|
8126
|
+
declaredClass(node) {
|
|
8127
|
+
const { checker } = this;
|
|
8128
|
+
const symbol = node.name && checker.getSymbolAtLocation(node.name);
|
|
8129
|
+
const name = node.name?.text ?? "";
|
|
8130
|
+
const flags = ts28.TypeFormatFlags.NoTruncation;
|
|
8131
|
+
const members = symbol ? checker.getPropertiesOfType(checker.getDeclaredTypeOfSymbol(symbol)).flatMap((property) => {
|
|
8132
|
+
const member = property.valueDeclaration;
|
|
8133
|
+
if (!member || !plainName(property.name)) {
|
|
8134
|
+
return [];
|
|
8135
|
+
}
|
|
8136
|
+
const modifiers = ts28.getCombinedModifierFlags(member);
|
|
8137
|
+
if (modifiers & ts28.ModifierFlags.Private) {
|
|
8138
|
+
return [`private ${property.name};`];
|
|
8139
|
+
}
|
|
8140
|
+
const access = modifiers & ts28.ModifierFlags.Protected ? "protected " : "";
|
|
8141
|
+
const type = checker.getTypeOfSymbolAtLocation(property, node);
|
|
8142
|
+
if (property.flags & ts28.SymbolFlags.Method) {
|
|
8143
|
+
return type.getCallSignatures().map((signature) => `${access}${property.name}${checker.signatureToString(signature, node, flags)};`);
|
|
8144
|
+
}
|
|
8145
|
+
const written = (ts28.isPropertyDeclaration(member) || ts28.isParameter(member)) && member.type && member.getSourceFile() === node.getSourceFile() ? member.type.getText() : checker.typeToString(type, node, flags);
|
|
8146
|
+
const readonly = modifiers & ts28.ModifierFlags.Readonly || property.flags & ts28.SymbolFlags.GetAccessor && !(property.flags & ts28.SymbolFlags.SetAccessor) ? "readonly " : "";
|
|
8147
|
+
const optional = property.flags & ts28.SymbolFlags.Optional ? "?" : "";
|
|
8148
|
+
return [
|
|
8149
|
+
`${access}${readonly}${property.name}${optional}: ${written};`
|
|
8150
|
+
];
|
|
8151
|
+
}) : [];
|
|
8152
|
+
const modifiers = ts28.getCombinedModifierFlags(node);
|
|
8153
|
+
const isDefault = Boolean(modifiers & ts28.ModifierFlags.Default);
|
|
8154
|
+
const head = [
|
|
8155
|
+
modifiers & ts28.ModifierFlags.Export && !isDefault ? "export " : "",
|
|
8156
|
+
"declare ",
|
|
8157
|
+
modifiers & ts28.ModifierFlags.Abstract ? "abstract " : "",
|
|
8158
|
+
`class ${name}`,
|
|
8159
|
+
node.typeParameters ? `<${node.typeParameters.map((parameter) => parameter.getText()).join(", ")}>` : ""
|
|
8160
|
+
].join("");
|
|
8161
|
+
return [
|
|
8162
|
+
`${head} {`,
|
|
8163
|
+
...members.map((member) => ` ${member}`),
|
|
8164
|
+
"}",
|
|
8165
|
+
...isDefault ? [`export default ${name};`] : []
|
|
8166
|
+
].join(`
|
|
8167
|
+
`);
|
|
8168
|
+
}
|
|
8169
|
+
}
|
|
8170
|
+
|
|
7587
8171
|
// packages/compiler/src/frontend/elaborate/retention.ts
|
|
7588
|
-
import
|
|
8172
|
+
import ts29 from "typescript";
|
|
7589
8173
|
var unstored = "retain retains nothing the PLC stores; retain state scan code writes, or exported data.";
|
|
7590
8174
|
var unpersisted = "persist persists nothing the PLC stores; persist state scan code writes, or exported data.";
|
|
7591
8175
|
var primitive = (type) => {
|
|
@@ -7595,23 +8179,23 @@ var primitive = (type) => {
|
|
|
7595
8179
|
if (type.isIntersection()) {
|
|
7596
8180
|
return type.types.some(primitive);
|
|
7597
8181
|
}
|
|
7598
|
-
return !(type.flags & (
|
|
8182
|
+
return !(type.flags & (ts29.TypeFlags.Object | ts29.TypeFlags.NonPrimitive));
|
|
7599
8183
|
};
|
|
7600
|
-
var importedThroughRetain = (checker, sdk, symbol) => symbol !== undefined && Boolean(symbol.flags &
|
|
8184
|
+
var importedThroughRetain = (checker, sdk, symbol) => symbol !== undefined && Boolean(symbol.flags & ts29.SymbolFlags.Alias) && ((symbol.declarations ?? []).some((declaration) => declaredInSdkModule(sdk, sdkModules.retain, declaration)) || importedThroughRetain(checker, sdk, checker.getImmediateAliasedSymbol(symbol)));
|
|
7601
8185
|
var moduleBinding = (call) => {
|
|
7602
8186
|
const parent = call.parent;
|
|
7603
|
-
return
|
|
8187
|
+
return ts29.isVariableDeclaration(parent) && parent.initializer === call && ts29.isVariableDeclarationList(parent.parent) && ts29.isVariableStatement(parent.parent.parent) && ts29.isSourceFile(parent.parent.parent.parent) ? parent : undefined;
|
|
7604
8188
|
};
|
|
7605
8189
|
var retainedDeclaration = (call) => {
|
|
7606
8190
|
const parent = call.parent;
|
|
7607
|
-
if (
|
|
8191
|
+
if (ts29.isPropertyDeclaration(parent) && parent.initializer === call) {
|
|
7608
8192
|
return parent;
|
|
7609
8193
|
}
|
|
7610
8194
|
return moduleBinding(call);
|
|
7611
8195
|
};
|
|
7612
8196
|
var checkImported = (call, name, checker, sdk) => {
|
|
7613
8197
|
const symbol = checker.getSymbolAtLocation(call.expression);
|
|
7614
|
-
if (!symbol || !(symbol.flags &
|
|
8198
|
+
if (!symbol || !(symbol.flags & ts29.SymbolFlags.Alias)) {
|
|
7615
8199
|
throw new SpecializeError(call, `Setup runs under Bun, where the global ${name} has no value: import { ${name} } from "${sdkModules.retain}".`);
|
|
7616
8200
|
}
|
|
7617
8201
|
if (!importedThroughRetain(checker, sdk, symbol)) {
|
|
@@ -7622,7 +8206,7 @@ var retentionDeclarations = (sources, checker, sdk) => {
|
|
|
7622
8206
|
const retained = new Set;
|
|
7623
8207
|
const persisted = new Set;
|
|
7624
8208
|
const visit = (node) => {
|
|
7625
|
-
if (
|
|
8209
|
+
if (ts29.isCallExpression(node) && namesSdk(checker, sdk, node.expression, "retain")) {
|
|
7626
8210
|
checkImported(node, "retain", checker, sdk);
|
|
7627
8211
|
const [argument] = node.arguments;
|
|
7628
8212
|
if (argument && primitive(checker.getTypeAtLocation(argument))) {
|
|
@@ -7633,7 +8217,7 @@ var retentionDeclarations = (sources, checker, sdk) => {
|
|
|
7633
8217
|
retained.add(declaration);
|
|
7634
8218
|
}
|
|
7635
8219
|
}
|
|
7636
|
-
if (
|
|
8220
|
+
if (ts29.isCallExpression(node) && namesSdk(checker, sdk, node.expression, "persist")) {
|
|
7637
8221
|
checkImported(node, "persist", checker, sdk);
|
|
7638
8222
|
const binding = moduleBinding(node);
|
|
7639
8223
|
if (!binding) {
|
|
@@ -7641,7 +8225,7 @@ var retentionDeclarations = (sources, checker, sdk) => {
|
|
|
7641
8225
|
}
|
|
7642
8226
|
persisted.add(binding);
|
|
7643
8227
|
}
|
|
7644
|
-
|
|
8228
|
+
ts29.forEachChild(node, visit);
|
|
7645
8229
|
};
|
|
7646
8230
|
for (const source of sources) {
|
|
7647
8231
|
visit(source);
|
|
@@ -7730,15 +8314,15 @@ class Retention {
|
|
|
7730
8314
|
|
|
7731
8315
|
// packages/compiler/src/frontend/elaborate/specialize.ts
|
|
7732
8316
|
import { identityDigest as identityDigest5 } from "@tsplc/compiler/ir";
|
|
7733
|
-
import
|
|
8317
|
+
import ts41 from "typescript";
|
|
7734
8318
|
|
|
7735
8319
|
// packages/compiler/src/frontend/elaborate/declarations.ts
|
|
7736
|
-
import
|
|
8320
|
+
import ts30 from "typescript";
|
|
7737
8321
|
var providedByRuntime = (checker, node) => {
|
|
7738
8322
|
const declaration = checker.getSymbolAtLocation(node)?.valueDeclaration;
|
|
7739
|
-
return declaration !== undefined && (declaration.getSourceFile().isDeclarationFile || (
|
|
8323
|
+
return declaration !== undefined && (declaration.getSourceFile().isDeclarationFile || (ts30.getCombinedModifierFlags(declaration) & ts30.ModifierFlags.Ambient) !== 0);
|
|
7740
8324
|
};
|
|
7741
|
-
var isGlobal = (checker, node, name) =>
|
|
8325
|
+
var isGlobal = (checker, node, name) => ts30.isIdentifier(node) && node.text === name && providedByRuntime(checker, node);
|
|
7742
8326
|
var symbolOf = (checker, name) => {
|
|
7743
8327
|
const symbol = valueSymbol(checker, name);
|
|
7744
8328
|
if (!symbol) {
|
|
@@ -7750,7 +8334,7 @@ var runTimeOptional = (checker, value) => {
|
|
|
7750
8334
|
if (value.kind === "choice") {
|
|
7751
8335
|
return value.optional && !value.narrowed ? value : undefined;
|
|
7752
8336
|
}
|
|
7753
|
-
if (value.kind !== "code" || !value.declared || !(value.declared.flags &
|
|
8337
|
+
if (value.kind !== "code" || !value.declared || !(value.declared.flags & ts30.TypeFlags.Union) || checker.getNonNullableType(value.declared) === value.declared) {
|
|
7754
8338
|
return;
|
|
7755
8339
|
}
|
|
7756
8340
|
return { ...value, declared: value.declared };
|
|
@@ -7768,7 +8352,7 @@ var parameterDefault = (checker, arg, parameter) => {
|
|
|
7768
8352
|
return;
|
|
7769
8353
|
};
|
|
7770
8354
|
var namedElements = (pattern, declared) => pattern.elements.flatMap((element, index) => {
|
|
7771
|
-
if (
|
|
8355
|
+
if (ts30.isOmittedExpression(element)) {
|
|
7772
8356
|
return [];
|
|
7773
8357
|
}
|
|
7774
8358
|
if (element.dotDotDotToken) {
|
|
@@ -7786,21 +8370,21 @@ var namedFields = (pattern, declared) => pattern.elements.map((element) => {
|
|
|
7786
8370
|
// packages/compiler/src/frontend/elaborate/dialect.ts
|
|
7787
8371
|
import { canAssign as canAssign2, integerType, primitiveTypes as primitiveTypes3 } from "@tsplc/compiler/ir";
|
|
7788
8372
|
import { elementaryType as elementaryType2, fitsNumber, isIntegerValued } from "@tsplc/iec";
|
|
7789
|
-
import
|
|
8373
|
+
import ts33 from "typescript";
|
|
7790
8374
|
|
|
7791
8375
|
// packages/compiler/src/frontend/elaborate/references.ts
|
|
7792
|
-
import
|
|
8376
|
+
import ts32 from "typescript";
|
|
7793
8377
|
|
|
7794
8378
|
// packages/compiler/src/frontend/elaborate/assignments.ts
|
|
7795
|
-
import
|
|
8379
|
+
import ts31 from "typescript";
|
|
7796
8380
|
var compoundOperators = new Map([
|
|
7797
|
-
[
|
|
7798
|
-
[
|
|
7799
|
-
[
|
|
7800
|
-
[
|
|
7801
|
-
[
|
|
8381
|
+
[ts31.SyntaxKind.PlusEqualsToken, "+"],
|
|
8382
|
+
[ts31.SyntaxKind.MinusEqualsToken, "-"],
|
|
8383
|
+
[ts31.SyntaxKind.AsteriskEqualsToken, "*"],
|
|
8384
|
+
[ts31.SyntaxKind.BarEqualsToken, "|"],
|
|
8385
|
+
[ts31.SyntaxKind.AmpersandEqualsToken, "&"]
|
|
7802
8386
|
]);
|
|
7803
|
-
var containsWrite = (node) => occursOutsideFunctions(node, (inner) =>
|
|
8387
|
+
var containsWrite = (node) => occursOutsideFunctions(node, (inner) => ts31.isBinaryExpression(inner) && isAssignment(inner.operatorToken.kind) || isUpdate(inner));
|
|
7804
8388
|
var storeStatement = (place, value) => mapped`${place} = ${value};`;
|
|
7805
8389
|
|
|
7806
8390
|
class AssignmentSpecializer {
|
|
@@ -7817,7 +8401,7 @@ class AssignmentSpecializer {
|
|
|
7817
8401
|
return this.context.dialect;
|
|
7818
8402
|
}
|
|
7819
8403
|
assign(node, frame) {
|
|
7820
|
-
if (
|
|
8404
|
+
if (ts31.isArrayLiteralExpression(node.left) || ts31.isObjectLiteralExpression(node.left)) {
|
|
7821
8405
|
const value = this.expressions.expression(node.right, frame);
|
|
7822
8406
|
this.destructure(node.left, value, frame);
|
|
7823
8407
|
return value;
|
|
@@ -7838,7 +8422,7 @@ class AssignmentSpecializer {
|
|
|
7838
8422
|
const text = this.placeText(target, node.left);
|
|
7839
8423
|
const declared = target.kind === "code" ? target.declared : undefined;
|
|
7840
8424
|
const operator = node.operatorToken.kind;
|
|
7841
|
-
if (operator ===
|
|
8425
|
+
if (operator === ts31.SyntaxKind.QuestionQuestionEqualsToken) {
|
|
7842
8426
|
const value = this.expressions.expression(node.right, frame, declared && this.checker.getNonNullableType(declared));
|
|
7843
8427
|
frame.emitter.open(mapped`if (${this.dialect.isUndefinedText(text, declared)}) {`);
|
|
7844
8428
|
frame.emitter.line(this.store(target, this.dialect.wrapDefined(this.dialect.code(value, declared && this.checker.getNonNullableType(declared)), declared)));
|
|
@@ -7846,7 +8430,7 @@ class AssignmentSpecializer {
|
|
|
7846
8430
|
return target;
|
|
7847
8431
|
}
|
|
7848
8432
|
const value = this.expressions.expression(node.right, frame, declared);
|
|
7849
|
-
if (operator ===
|
|
8433
|
+
if (operator === ts31.SyntaxKind.EqualsToken) {
|
|
7850
8434
|
frame.emitter.line(this.stored(target, value));
|
|
7851
8435
|
return target;
|
|
7852
8436
|
}
|
|
@@ -7869,8 +8453,8 @@ class AssignmentSpecializer {
|
|
|
7869
8453
|
}
|
|
7870
8454
|
stepOn(target, node, frame, { used }) {
|
|
7871
8455
|
const text = this.placeText(target, node.operand);
|
|
7872
|
-
const before = used &&
|
|
7873
|
-
frame.emitter.line(this.store(target, mapped`${text} ${node.operator ===
|
|
8456
|
+
const before = used && ts31.isPostfixUnaryExpression(node) ? this.expressions.held(target, frame) : target;
|
|
8457
|
+
frame.emitter.line(this.store(target, mapped`${text} ${node.operator === ts31.SyntaxKind.PlusPlusToken ? "+" : "-"} 1`));
|
|
7874
8458
|
return before;
|
|
7875
8459
|
}
|
|
7876
8460
|
destructure(pattern, value, frame) {
|
|
@@ -7881,26 +8465,26 @@ class AssignmentSpecializer {
|
|
|
7881
8465
|
}
|
|
7882
8466
|
}
|
|
7883
8467
|
collect(pattern, value, frame, writes) {
|
|
7884
|
-
if (
|
|
8468
|
+
if (ts31.isArrayLiteralExpression(pattern)) {
|
|
7885
8469
|
pattern.elements.forEach((element, index) => {
|
|
7886
|
-
if (
|
|
8470
|
+
if (ts31.isOmittedExpression(element)) {
|
|
7887
8471
|
return;
|
|
7888
8472
|
}
|
|
7889
|
-
if (
|
|
8473
|
+
if (ts31.isSpreadElement(element)) {
|
|
7890
8474
|
throw new SpecializeError(element, "Assign the rest of an array element by element.");
|
|
7891
8475
|
}
|
|
7892
8476
|
this.collectTarget(element, this.expressions.element(value, constant(index), element, frame), frame, writes);
|
|
7893
8477
|
});
|
|
7894
8478
|
return;
|
|
7895
8479
|
}
|
|
7896
|
-
if (
|
|
8480
|
+
if (ts31.isObjectLiteralExpression(pattern)) {
|
|
7897
8481
|
for (const property of pattern.properties) {
|
|
7898
|
-
if (
|
|
8482
|
+
if (ts31.isShorthandPropertyAssignment(property)) {
|
|
7899
8483
|
const part = this.expressions.member(value, property.name.text, property, frame);
|
|
7900
8484
|
this.collectTarget(property.name, this.expressions.defaulted(part, property.objectAssignmentInitializer, frame), frame, writes);
|
|
7901
8485
|
continue;
|
|
7902
8486
|
}
|
|
7903
|
-
if (!
|
|
8487
|
+
if (!ts31.isPropertyAssignment(property)) {
|
|
7904
8488
|
throw new SpecializeError(property, "Assign each field of a pattern by name.");
|
|
7905
8489
|
}
|
|
7906
8490
|
this.collectTarget(property.initializer, this.expressions.member(value, property.name.getText(), property, frame), frame, writes);
|
|
@@ -7910,11 +8494,11 @@ class AssignmentSpecializer {
|
|
|
7910
8494
|
throw new SpecializeError(pattern, "Not a pattern.");
|
|
7911
8495
|
}
|
|
7912
8496
|
collectTarget(target, part, frame, writes) {
|
|
7913
|
-
if (
|
|
8497
|
+
if (ts31.isBinaryExpression(target) && target.operatorToken.kind === ts31.SyntaxKind.EqualsToken) {
|
|
7914
8498
|
this.collectTarget(target.left, this.expressions.defaulted(part, target.right, frame), frame, writes);
|
|
7915
8499
|
return;
|
|
7916
8500
|
}
|
|
7917
|
-
if (
|
|
8501
|
+
if (ts31.isArrayLiteralExpression(target) || ts31.isObjectLiteralExpression(target)) {
|
|
7918
8502
|
this.collect(target, part, frame, writes);
|
|
7919
8503
|
return;
|
|
7920
8504
|
}
|
|
@@ -7962,8 +8546,8 @@ var referenceType = (type) => {
|
|
|
7962
8546
|
const defined = unionMembers(type).filter((part) => !isAbsent(part));
|
|
7963
8547
|
return defined.length > 0 && defined.every(isInstanceType);
|
|
7964
8548
|
};
|
|
7965
|
-
var isInstanceType = (type) => Boolean(type.flags &
|
|
7966
|
-
var classesOf = (type) => type.getSymbol()?.declarations?.filter(
|
|
8549
|
+
var isInstanceType = (type) => Boolean(type.flags & ts32.TypeFlags.Object) && type.getConstructSignatures().length === 0 && classesOf(type).length > 0;
|
|
8550
|
+
var classesOf = (type) => type.getSymbol()?.declarations?.filter(ts32.isClassDeclaration) ?? [];
|
|
7967
8551
|
var referable = (node) => node.captured.kind === "instance" || node.captured.kind === "object" || node.captured.kind === "array";
|
|
7968
8552
|
var referenceIndex = (value) => {
|
|
7969
8553
|
if (value.kind === "choice") {
|
|
@@ -8090,7 +8674,7 @@ class References {
|
|
|
8090
8674
|
if (value.kind === "virtual") {
|
|
8091
8675
|
return true;
|
|
8092
8676
|
}
|
|
8093
|
-
return value.kind === "code" && value.declared !== undefined && Boolean(this.checker.getNonNullableType(value.declared).flags &
|
|
8677
|
+
return value.kind === "code" && value.declared !== undefined && Boolean(this.checker.getNonNullableType(value.declared).flags & ts32.TypeFlags.Object);
|
|
8094
8678
|
}
|
|
8095
8679
|
held(choice, frame) {
|
|
8096
8680
|
if (!choice.place) {
|
|
@@ -8140,11 +8724,11 @@ class References {
|
|
|
8140
8724
|
}
|
|
8141
8725
|
frame.emitter.line(storeStatement(target.index, index));
|
|
8142
8726
|
};
|
|
8143
|
-
if (operator ===
|
|
8727
|
+
if (operator === ts32.SyntaxKind.EqualsToken) {
|
|
8144
8728
|
store();
|
|
8145
8729
|
return target;
|
|
8146
8730
|
}
|
|
8147
|
-
if (operator ===
|
|
8731
|
+
if (operator === ts32.SyntaxKind.QuestionQuestionEqualsToken) {
|
|
8148
8732
|
frame.emitter.open(mapped`if (${absent(target.index)}) {`);
|
|
8149
8733
|
store();
|
|
8150
8734
|
frame.emitter.close();
|
|
@@ -8154,7 +8738,7 @@ class References {
|
|
|
8154
8738
|
}
|
|
8155
8739
|
target(node, frame) {
|
|
8156
8740
|
const place = unparenthesized(node, { nonNull: true });
|
|
8157
|
-
if (!
|
|
8741
|
+
if (!ts32.isPropertyAccessExpression(place) || place.expression.kind === ts32.SyntaxKind.ThisKeyword || place.expression.kind === ts32.SyntaxKind.SuperKeyword || !referenceType(this.checker.getTypeAtLocation(place.expression))) {
|
|
8158
8742
|
return this.expressions.expression(node, frame);
|
|
8159
8743
|
}
|
|
8160
8744
|
const object = this.expressions.expression(place.expression, frame);
|
|
@@ -8182,7 +8766,7 @@ class References {
|
|
|
8182
8766
|
}
|
|
8183
8767
|
memberType(option, name) {
|
|
8184
8768
|
if (option.captured.kind === "instance") {
|
|
8185
|
-
const accessor = this.model.memberNamed(this.model.classOf(option), name,
|
|
8769
|
+
const accessor = this.model.memberNamed(this.model.classOf(option), name, ts32.isGetAccessorDeclaration);
|
|
8186
8770
|
const signature = accessor && this.checker.getSignatureFromDeclaration(accessor);
|
|
8187
8771
|
if (signature) {
|
|
8188
8772
|
return this.checker.getReturnTypeOfSignature(signature);
|
|
@@ -8226,7 +8810,7 @@ class References {
|
|
|
8226
8810
|
});
|
|
8227
8811
|
}
|
|
8228
8812
|
const sample = arms.map((arm) => arm.value).find((value) => value.kind === "code");
|
|
8229
|
-
const declared = type ?? (sample?.kind === "code" ? sample.declared : undefined) ?? (
|
|
8813
|
+
const declared = type ?? (sample?.kind === "code" ? sample.declared : undefined) ?? (ts32.isExpression(at) ? this.checker.getTypeAtLocation(at) : undefined);
|
|
8230
8814
|
if (!declared || arms.some((arm) => arm.value.kind !== "const" && arm.value.kind !== "code")) {
|
|
8231
8815
|
throw new SpecializeError(at, "This can't be read through a reference chosen at run time: read a value, or call the method where it is named.");
|
|
8232
8816
|
}
|
|
@@ -8319,7 +8903,7 @@ class References {
|
|
|
8319
8903
|
}
|
|
8320
8904
|
call(choice, name, node, frame, expected) {
|
|
8321
8905
|
const [sample] = choice.options;
|
|
8322
|
-
const declaration = sample?.captured.kind === "instance" ? this.model.memberNamed(this.model.classOf(sample), name,
|
|
8906
|
+
const declaration = sample?.captured.kind === "instance" ? this.model.memberNamed(this.model.classOf(sample), name, ts32.isMethodDeclaration) : undefined;
|
|
8323
8907
|
const args = this.expressions.inOrder(node.arguments, (argument, index) => {
|
|
8324
8908
|
const parameter = declaration?.parameters[index];
|
|
8325
8909
|
return this.expressions.expression(argument, frame, parameter && this.checker.getTypeAtLocation(parameter));
|
|
@@ -8327,7 +8911,7 @@ class References {
|
|
|
8327
8911
|
return this.dispatch(choice, frame, node, undefined, (option) => {
|
|
8328
8912
|
const method = this.expressions.member({ kind: "static", node: option }, name, node.expression, frame);
|
|
8329
8913
|
const result = this.expressions.invoke(method, args, node, frame, expected);
|
|
8330
|
-
if (result.kind === "code" && (!result.declared || result.declared.flags &
|
|
8914
|
+
if (result.kind === "code" && (!result.declared || result.declared.flags & ts32.TypeFlags.Void)) {
|
|
8331
8915
|
frame.emitter.line(mapped`${result.text};`);
|
|
8332
8916
|
return constant(undefined);
|
|
8333
8917
|
}
|
|
@@ -8545,9 +9129,9 @@ class DialectText {
|
|
|
8545
9129
|
}
|
|
8546
9130
|
if (value.kind === "code" && value.declared) {
|
|
8547
9131
|
const declared = value.declared;
|
|
8548
|
-
if (this.layouts.isOptionalRecord(declared) && !
|
|
9132
|
+
if (this.layouts.isOptionalRecord(declared) && !ts33.isPrefixUnaryExpression(at)) {
|
|
8549
9133
|
const type = this.checker.getTypeAtLocation(at);
|
|
8550
|
-
if (!(type.flags &
|
|
9134
|
+
if (!(type.flags & ts33.TypeFlags.BooleanLike)) {
|
|
8551
9135
|
return optionalHas(value.text);
|
|
8552
9136
|
}
|
|
8553
9137
|
}
|
|
@@ -8616,7 +9200,7 @@ var bigintWidth = (iec) => {
|
|
|
8616
9200
|
};
|
|
8617
9201
|
var bigintBeside = (value, other, text) => value.kind === "const" && typeof value.value === "number" && Number.isInteger(value.value) && other.kind === "code" && bigintWidth(other.iec) ? `${value.value}n` : text;
|
|
8618
9202
|
var integerTypes = new Map(primitiveTypes3.filter(integerType).map((type) => [type.id, type]));
|
|
8619
|
-
var storedWidth = (declared, iec) => iec ?? (declared && declared.flags &
|
|
9203
|
+
var storedWidth = (declared, iec) => iec ?? (declared && declared.flags & ts33.TypeFlags.Number ? defaultNumericTypes.number : undefined);
|
|
8620
9204
|
var commonWidth = (from, to) => {
|
|
8621
9205
|
if (!needsConversion(from, to)) {
|
|
8622
9206
|
return to;
|
|
@@ -8641,7 +9225,7 @@ import {
|
|
|
8641
9225
|
optionalRecord as optionalRecord2,
|
|
8642
9226
|
primitiveTypes as primitiveTypes4
|
|
8643
9227
|
} from "@tsplc/compiler/ir";
|
|
8644
|
-
import
|
|
9228
|
+
import ts37 from "typescript";
|
|
8645
9229
|
|
|
8646
9230
|
// packages/compiler/src/frontend/elaborate/calls.ts
|
|
8647
9231
|
import { calendarLiteral as calendarLiteral2 } from "@tsplc/compiler/ir";
|
|
@@ -8650,7 +9234,7 @@ import {
|
|
|
8650
9234
|
elementaryFunctions as elementaryFunctions2,
|
|
8651
9235
|
elementaryValue
|
|
8652
9236
|
} from "@tsplc/iec/arithmetic";
|
|
8653
|
-
import
|
|
9237
|
+
import ts34 from "typescript";
|
|
8654
9238
|
|
|
8655
9239
|
// packages/compiler/src/frontend/elaborate/array-searches.ts
|
|
8656
9240
|
class ArraySearches {
|
|
@@ -8785,7 +9369,7 @@ class CallSpecializer {
|
|
|
8785
9369
|
}
|
|
8786
9370
|
call(node, frame, expected) {
|
|
8787
9371
|
const callee = node.expression;
|
|
8788
|
-
if (
|
|
9372
|
+
if (ts34.isPropertyAccessExpression(callee) || ts34.isPropertyAccessChain(callee)) {
|
|
8789
9373
|
return this.method(node, callee, frame, expected);
|
|
8790
9374
|
}
|
|
8791
9375
|
if (isGlobal(this.context.checker, callee, "structuredClone")) {
|
|
@@ -8805,11 +9389,11 @@ class CallSpecializer {
|
|
|
8805
9389
|
const declaration = this.functionDeclaration(target.node.captured.file, target.node.captured.name);
|
|
8806
9390
|
return this.invokeCall({ kind: "function", declaration }, node, frame, expected);
|
|
8807
9391
|
}
|
|
8808
|
-
const sdkFile =
|
|
8809
|
-
if (
|
|
9392
|
+
const sdkFile = ts34.isIdentifier(callee) && this.sdkDeclaration(callee);
|
|
9393
|
+
if (ts34.isIdentifier(callee) && sdkFile && callee.text === "cancelAsync" && sdkModule(sdkFile) === sdkModules.async && node.arguments.length) {
|
|
8810
9394
|
return this.cancelOwner(node, frame);
|
|
8811
9395
|
}
|
|
8812
|
-
if (
|
|
9396
|
+
if (ts34.isIdentifier(callee) && sdkFile) {
|
|
8813
9397
|
return this.sdk(callee.text, sdkModule(sdkFile), node, frame);
|
|
8814
9398
|
}
|
|
8815
9399
|
return this.invokeCall(target, node, frame, expected);
|
|
@@ -8820,7 +9404,7 @@ class CallSpecializer {
|
|
|
8820
9404
|
if (name === "from" && isGlobal(checker, callee.expression, "Array")) {
|
|
8821
9405
|
return this.arrayFrom(node, frame);
|
|
8822
9406
|
}
|
|
8823
|
-
if (callee.expression.kind ===
|
|
9407
|
+
if (callee.expression.kind === ts34.SyntaxKind.SuperKeyword) {
|
|
8824
9408
|
return this.invokeCall(this.expressions.superMember(callee, frame), node, frame, expected);
|
|
8825
9409
|
}
|
|
8826
9410
|
if (isGlobal(checker, callee.expression, "Math")) {
|
|
@@ -8874,7 +9458,7 @@ class CallSpecializer {
|
|
|
8874
9458
|
}
|
|
8875
9459
|
receiver(node, frame) {
|
|
8876
9460
|
const inner = unwrapped(node);
|
|
8877
|
-
if (!
|
|
9461
|
+
if (!ts34.isElementAccessExpression(inner)) {
|
|
8878
9462
|
return this.expressions.expression(node, frame);
|
|
8879
9463
|
}
|
|
8880
9464
|
const object = this.expressions.expression(inner.expression, frame);
|
|
@@ -8906,7 +9490,7 @@ class CallSpecializer {
|
|
|
8906
9490
|
const fulfilled = name === "then" ? first : undefined;
|
|
8907
9491
|
const rejected = name === "then" ? second : first;
|
|
8908
9492
|
const result = checker.getAwaitedType(checker.getTypeAtLocation(node));
|
|
8909
|
-
const resultType = result && !(result.flags &
|
|
9493
|
+
const resultType = result && !(result.flags & ts34.TypeFlags.Void) ? result : undefined;
|
|
8910
9494
|
const fulfil = () => {
|
|
8911
9495
|
const value = this.awaitedValue(this.expressions.expression(callee.expression, frame), frame);
|
|
8912
9496
|
return fulfilled ? this.handled(fulfilled, [value], frame) : value;
|
|
@@ -8946,7 +9530,7 @@ class CallSpecializer {
|
|
|
8946
9530
|
if (promise.kind !== "code") {
|
|
8947
9531
|
return promise;
|
|
8948
9532
|
}
|
|
8949
|
-
if (!promise.declared || promise.declared.flags &
|
|
9533
|
+
if (!promise.declared || promise.declared.flags & ts34.TypeFlags.Void) {
|
|
8950
9534
|
frame.emitter.line(mapped`await ${promise.text};`);
|
|
8951
9535
|
return constant(undefined);
|
|
8952
9536
|
}
|
|
@@ -8963,25 +9547,25 @@ class CallSpecializer {
|
|
|
8963
9547
|
handled(callback, args, frame) {
|
|
8964
9548
|
const target = this.expressions.expression(callback, frame);
|
|
8965
9549
|
const value = this.expressions.invoke(target, args, callback, frame);
|
|
8966
|
-
const isAsync = target.kind === "function" &&
|
|
9550
|
+
const isAsync = target.kind === "function" && ts34.canHaveModifiers(target.declaration) && ts34.getModifiers(target.declaration)?.some((modifier) => modifier.kind === ts34.SyntaxKind.AsyncKeyword);
|
|
8967
9551
|
return isAsync ? this.awaitedValue(value, frame) : value;
|
|
8968
9552
|
}
|
|
8969
9553
|
invokeCall(target, node, frame, expected) {
|
|
8970
9554
|
return this.expressions.invoke(target, this.expressions.inOrder(node.arguments, (argument, index) => this.argument(argument, target, index, frame), frame), node, frame, expected);
|
|
8971
9555
|
}
|
|
8972
9556
|
argument(node, callee, index, frame) {
|
|
8973
|
-
const parameter = callee.kind === "function" && !
|
|
9557
|
+
const parameter = callee.kind === "function" && !ts34.isGetAccessorDeclaration(callee.declaration) ? callee.declaration.parameters[index] : undefined;
|
|
8974
9558
|
return this.expressions.expression(node, frame, parameter && this.context.checker.getTypeAtLocation(parameter));
|
|
8975
9559
|
}
|
|
8976
9560
|
functionDeclaration(file, name) {
|
|
8977
9561
|
const source = this.context.sources.find((item) => item.fileName === file);
|
|
8978
9562
|
for (const statement of source?.statements ?? []) {
|
|
8979
|
-
if (
|
|
9563
|
+
if (ts34.isFunctionDeclaration(statement) && statement.name?.text === name) {
|
|
8980
9564
|
return statement;
|
|
8981
9565
|
}
|
|
8982
|
-
if (
|
|
9566
|
+
if (ts34.isVariableStatement(statement)) {
|
|
8983
9567
|
for (const declaration of statement.declarationList.declarations) {
|
|
8984
|
-
if (
|
|
9568
|
+
if (ts34.isIdentifier(declaration.name) && declaration.name.text === name && declaration.initializer && (ts34.isArrowFunction(declaration.initializer) || ts34.isFunctionExpression(declaration.initializer))) {
|
|
8985
9569
|
return declaration.initializer;
|
|
8986
9570
|
}
|
|
8987
9571
|
}
|
|
@@ -9003,16 +9587,16 @@ class CallSpecializer {
|
|
|
9003
9587
|
}
|
|
9004
9588
|
telemetry(node, callee, frame) {
|
|
9005
9589
|
const { checker, dialect } = this.context;
|
|
9006
|
-
const value = (argument) =>
|
|
9590
|
+
const value = (argument) => ts34.isCallExpression(argument) && ts34.isPropertyAccessExpression(argument.expression) && this.telemetryCall(argument.expression) ? this.telemetry(argument, argument.expression, frame) : dialect.code(this.expressions.expression(argument, frame), checker.getTypeAtLocation(argument));
|
|
9007
9591
|
const args = node.arguments.map((argument) => {
|
|
9008
|
-
if (
|
|
9592
|
+
if (ts34.isStringLiteral(argument) || ts34.isNoSubstitutionTemplateLiteral(argument)) {
|
|
9009
9593
|
return copied(argument);
|
|
9010
9594
|
}
|
|
9011
|
-
if (
|
|
9595
|
+
if (ts34.isObjectLiteralExpression(argument)) {
|
|
9012
9596
|
return this.telemetryProperties(argument, value, frame);
|
|
9013
9597
|
}
|
|
9014
9598
|
const argumentType = checker.getTypeAtLocation(argument);
|
|
9015
|
-
if (argumentType.flags &
|
|
9599
|
+
if (argumentType.flags & ts34.TypeFlags.Object && !checker.isArrayType(argumentType)) {
|
|
9016
9600
|
const fields = this.expressions.recordFields(argument, frame).map(([name, field]) => mapped`${name}: ${dialect.code(field, undefined)}`);
|
|
9017
9601
|
return mapped`{ ${joined(fields, ", ")} }`;
|
|
9018
9602
|
}
|
|
@@ -9024,15 +9608,15 @@ class CallSpecializer {
|
|
|
9024
9608
|
telemetryProperties(argument, value, frame) {
|
|
9025
9609
|
const { checker, dialect } = this.context;
|
|
9026
9610
|
const properties = argument.properties.flatMap((property) => {
|
|
9027
|
-
if (
|
|
9611
|
+
if (ts34.isShorthandPropertyAssignment(property)) {
|
|
9028
9612
|
return [
|
|
9029
9613
|
mapped`${copied(property.name)}: ${dialect.code(this.expressions.shorthand(property, frame), checker.getTypeAtLocation(property.name))}`
|
|
9030
9614
|
];
|
|
9031
9615
|
}
|
|
9032
|
-
if (
|
|
9616
|
+
if (ts34.isSpreadAssignment(property)) {
|
|
9033
9617
|
return this.expressions.recordFields(property.expression, frame).map(([name, field]) => mapped`${name}: ${dialect.code(field, undefined)}`);
|
|
9034
9618
|
}
|
|
9035
|
-
if (!
|
|
9619
|
+
if (!ts34.isPropertyAssignment(property)) {
|
|
9036
9620
|
throw new SpecializeError(property, "Name each telemetry property.");
|
|
9037
9621
|
}
|
|
9038
9622
|
return [mapped`${copied(property.name)}: ${value(property.initializer)}`];
|
|
@@ -9047,11 +9631,11 @@ class CallSpecializer {
|
|
|
9047
9631
|
}
|
|
9048
9632
|
return this.context.dialect.code(value, value.kind === "virtual" ? this.context.checker.getTypeAtLocation(argument) : undefined);
|
|
9049
9633
|
};
|
|
9050
|
-
const args = node.arguments.map((argument) =>
|
|
9051
|
-
if (
|
|
9634
|
+
const args = node.arguments.map((argument) => ts34.isObjectLiteralExpression(argument) ? mapped`{ ${joined(argument.properties.map((property) => {
|
|
9635
|
+
if (ts34.isShorthandPropertyAssignment(property)) {
|
|
9052
9636
|
return mapped`${copied(property.name)}: ${this.context.dialect.code(this.expressions.shorthand(property, frame), undefined)}`;
|
|
9053
9637
|
}
|
|
9054
|
-
if (!
|
|
9638
|
+
if (!ts34.isPropertyAssignment(property)) {
|
|
9055
9639
|
throw new SpecializeError(property, "Name each option.");
|
|
9056
9640
|
}
|
|
9057
9641
|
return mapped`${copied(property.name)}: ${code(property.initializer)}`;
|
|
@@ -9072,7 +9656,7 @@ class CallSpecializer {
|
|
|
9072
9656
|
kind: "code",
|
|
9073
9657
|
text,
|
|
9074
9658
|
declared,
|
|
9075
|
-
...iec && declared.flags &
|
|
9659
|
+
...iec && declared.flags & ts34.TypeFlags.NumberLike && { iec }
|
|
9076
9660
|
};
|
|
9077
9661
|
}
|
|
9078
9662
|
math(name, node, frame) {
|
|
@@ -9134,14 +9718,14 @@ class CallSpecializer {
|
|
|
9134
9718
|
if (work) {
|
|
9135
9719
|
this.refuseStateInWork(work);
|
|
9136
9720
|
}
|
|
9137
|
-
imports.kept.add(root.file, root.name);
|
|
9721
|
+
const local = imports.kept.add(root.file, root.name);
|
|
9138
9722
|
const args = node.arguments.map((argument) => {
|
|
9139
9723
|
const contextual = checker.getContextualType(argument);
|
|
9140
9724
|
return dialect.code(this.expressions.expression(argument, frame, contextual), contextual);
|
|
9141
9725
|
});
|
|
9142
9726
|
return {
|
|
9143
9727
|
kind: "code",
|
|
9144
|
-
text: name === null ? mapped`${
|
|
9728
|
+
text: name === null ? mapped`${local}(${joined(args, ", ")})` : mapped`${local}.${name}(${joined(args, ", ")})`,
|
|
9145
9729
|
declared: name === null ? checker.getAwaitedType(checker.getTypeAtLocation(node)) : checker.getTypeAtLocation(node),
|
|
9146
9730
|
typedByTask: true,
|
|
9147
9731
|
...name === null && { iec: this.context.layouts.iecName(work) }
|
|
@@ -9149,14 +9733,14 @@ class CallSpecializer {
|
|
|
9149
9733
|
}
|
|
9150
9734
|
workFunction(declaration) {
|
|
9151
9735
|
const init = declaration.initializer;
|
|
9152
|
-
const work = init &&
|
|
9153
|
-
const symbol = work &&
|
|
9736
|
+
const work = init && ts34.isCallExpression(init) ? init.arguments[0] : undefined;
|
|
9737
|
+
const symbol = work && ts34.isIdentifier(work) ? this.context.checker.getSymbolAtLocation(work) : undefined;
|
|
9154
9738
|
const fn = symbol && unaliased(this.context.checker, symbol).valueDeclaration;
|
|
9155
|
-
return fn &&
|
|
9739
|
+
return fn && ts34.isFunctionDeclaration(fn) && fn.body ? fn : undefined;
|
|
9156
9740
|
}
|
|
9157
9741
|
refuseStateInWork(fn) {
|
|
9158
9742
|
const visit = (node) => {
|
|
9159
|
-
if (
|
|
9743
|
+
if (ts34.isIdentifier(node)) {
|
|
9160
9744
|
const read = valueSymbol(this.context.checker, node);
|
|
9161
9745
|
const declared = read && unaliased(this.context.checker, read).valueDeclaration;
|
|
9162
9746
|
const file = declared?.getSourceFile().fileName;
|
|
@@ -9164,9 +9748,9 @@ class CallSpecializer {
|
|
|
9164
9748
|
throw new SpecializeError(node, `A task's work runs on its own, given only its arguments, so it can't read ${node.text} from a module of program state: pass what it needs as arguments, and use its result in scan code.`);
|
|
9165
9749
|
}
|
|
9166
9750
|
}
|
|
9167
|
-
|
|
9751
|
+
ts34.forEachChild(node, visit);
|
|
9168
9752
|
};
|
|
9169
|
-
|
|
9753
|
+
ts34.forEachChild(fn, visit);
|
|
9170
9754
|
}
|
|
9171
9755
|
serviceCall(path, node, frame) {
|
|
9172
9756
|
const { model, checker, dialect, imports } = this.context;
|
|
@@ -9177,14 +9761,14 @@ class CallSpecializer {
|
|
|
9177
9761
|
if (!client) {
|
|
9178
9762
|
throw new SpecializeError(node, "Create the service client in a module-level const, and import it where scan code calls it: the PLC calls the server through that one client, with the types the server's router gives each call.");
|
|
9179
9763
|
}
|
|
9180
|
-
imports.kept.add(client.file, client.name);
|
|
9764
|
+
const local = imports.kept.add(client.file, client.name);
|
|
9181
9765
|
const args = node.arguments.map((argument) => {
|
|
9182
9766
|
const contextual = checker.getContextualType(argument);
|
|
9183
9767
|
return dialect.code(this.expressions.expression(argument, frame, contextual), contextual);
|
|
9184
9768
|
});
|
|
9185
9769
|
return {
|
|
9186
9770
|
kind: "code",
|
|
9187
|
-
text: mapped`${
|
|
9771
|
+
text: mapped`${local}.${path}(${joined(args, ", ")})`,
|
|
9188
9772
|
declared: checker.getTypeAtLocation(node)
|
|
9189
9773
|
};
|
|
9190
9774
|
}
|
|
@@ -9343,7 +9927,7 @@ class CallSpecializer {
|
|
|
9343
9927
|
}
|
|
9344
9928
|
arrayFrom(node, frame) {
|
|
9345
9929
|
const [shape, make] = node.arguments;
|
|
9346
|
-
const length = shape &&
|
|
9930
|
+
const length = shape && ts34.isObjectLiteralExpression(shape) && this.expressions.member(this.expressions.expression(shape, frame), "length", shape, frame);
|
|
9347
9931
|
if (!length || length.kind !== "const" || typeof length.value !== "number") {
|
|
9348
9932
|
throw new SpecializeError(node, "Array.from needs a length known when the program compiles.");
|
|
9349
9933
|
}
|
|
@@ -9373,7 +9957,7 @@ class CallSpecializer {
|
|
|
9373
9957
|
throw new SpecializeError(node, "structuredClone needs a value.");
|
|
9374
9958
|
}
|
|
9375
9959
|
const value = this.expressions.expression(argument, frame);
|
|
9376
|
-
if (value.kind !== "code" || value.declared && !(value.declared.flags &
|
|
9960
|
+
if (value.kind !== "code" || value.declared && !(value.declared.flags & ts34.TypeFlags.Object)) {
|
|
9377
9961
|
return value;
|
|
9378
9962
|
}
|
|
9379
9963
|
this.context.imports.addSdk(sdkModules.types, "copy");
|
|
@@ -9392,19 +9976,19 @@ class CallSpecializer {
|
|
|
9392
9976
|
}
|
|
9393
9977
|
var awaitedLink = (node) => {
|
|
9394
9978
|
const parent = node.parent;
|
|
9395
|
-
if (
|
|
9979
|
+
if (ts34.isAwaitExpression(parent)) {
|
|
9396
9980
|
return true;
|
|
9397
9981
|
}
|
|
9398
|
-
if (
|
|
9399
|
-
return
|
|
9982
|
+
if (ts34.isParenthesizedExpression(parent)) {
|
|
9983
|
+
return ts34.isAwaitExpression(parent.parent);
|
|
9400
9984
|
}
|
|
9401
|
-
return
|
|
9985
|
+
return ts34.isPropertyAccessExpression(parent) && isPromiseLink(parent.parent) && awaitedLink(parent.parent);
|
|
9402
9986
|
};
|
|
9403
9987
|
var moduleLevel = (declaration) => {
|
|
9404
|
-
if (
|
|
9405
|
-
return
|
|
9988
|
+
if (ts34.isVariableDeclaration(declaration)) {
|
|
9989
|
+
return ts34.isSourceFile(declaration.parent.parent.parent);
|
|
9406
9990
|
}
|
|
9407
|
-
return (
|
|
9991
|
+
return (ts34.isFunctionDeclaration(declaration) || ts34.isClassDeclaration(declaration)) && ts34.isSourceFile(declaration.parent);
|
|
9408
9992
|
};
|
|
9409
9993
|
var refuseTimeArgument = (name, value, argument) => {
|
|
9410
9994
|
const calendar = calendarFormats.get(name);
|
|
@@ -9513,7 +10097,7 @@ var sliceBound = (bound) => {
|
|
|
9513
10097
|
};
|
|
9514
10098
|
|
|
9515
10099
|
// packages/compiler/src/frontend/elaborate/filtered-arrays.ts
|
|
9516
|
-
import
|
|
10100
|
+
import ts35 from "typescript";
|
|
9517
10101
|
var isTrue = (value) => value.kind === "const" && Boolean(value.value);
|
|
9518
10102
|
var isFalse = (value) => value.kind === "const" && !value.value;
|
|
9519
10103
|
var whole = (items) => items.map((item) => ({ item, present: constant(true) }));
|
|
@@ -9607,7 +10191,7 @@ class FilteredArrays {
|
|
|
9607
10191
|
return this.number(total);
|
|
9608
10192
|
}
|
|
9609
10193
|
isFilter(node) {
|
|
9610
|
-
return
|
|
10194
|
+
return ts35.isCallExpression(node) && ts35.isPropertyAccessExpression(node.expression) && node.expression.name.text === "filter" && node.arguments.length === 1 && providedByRuntime(this.context.checker, node.expression.name);
|
|
9611
10195
|
}
|
|
9612
10196
|
count(node, frame) {
|
|
9613
10197
|
const receiver = this.expressions.expression(node.expression.expression, frame);
|
|
@@ -9872,7 +10456,7 @@ class FilteredArrays {
|
|
|
9872
10456
|
}
|
|
9873
10457
|
|
|
9874
10458
|
// packages/compiler/src/frontend/elaborate/iterators.ts
|
|
9875
|
-
import
|
|
10459
|
+
import ts36 from "typescript";
|
|
9876
10460
|
|
|
9877
10461
|
// packages/compiler/src/frontend/elaborate/frame.ts
|
|
9878
10462
|
class Emitter {
|
|
@@ -9996,15 +10580,15 @@ class Iterators {
|
|
|
9996
10580
|
}, items, node, frame);
|
|
9997
10581
|
}
|
|
9998
10582
|
generator(node, frame) {
|
|
9999
|
-
if (!
|
|
10583
|
+
if (!ts36.isCallExpression(node)) {
|
|
10000
10584
|
return;
|
|
10001
10585
|
}
|
|
10002
10586
|
const declaration = this.context.checker.getResolvedSignature(node)?.declaration;
|
|
10003
|
-
if (!declaration || !(
|
|
10587
|
+
if (!declaration || !(ts36.isFunctionDeclaration(declaration) || ts36.isMethodDeclaration(declaration) || ts36.isFunctionExpression(declaration)) || !declaration.asteriskToken || !declaration.body) {
|
|
10004
10588
|
return;
|
|
10005
10589
|
}
|
|
10006
10590
|
const callee = node.expression;
|
|
10007
|
-
const target =
|
|
10591
|
+
const target = ts36.isPropertyAccessExpression(callee) ? this.expressions.member(this.expressions.expression(callee.expression, frame), callee.name.text, callee, frame) : this.expressions.expression(callee, frame);
|
|
10008
10592
|
const self = target.kind === "function" ? target.self : undefined;
|
|
10009
10593
|
const args = this.expressions.inOrder(node.arguments, (argument) => this.expressions.expression(argument, frame), frame);
|
|
10010
10594
|
return { declaration, self, args };
|
|
@@ -10034,7 +10618,7 @@ class Iterators {
|
|
|
10034
10618
|
}
|
|
10035
10619
|
readInPlace(node) {
|
|
10036
10620
|
const user = outside(node);
|
|
10037
|
-
if (!
|
|
10621
|
+
if (!ts36.isPropertyAccessExpression(user) && !ts36.isElementAccessExpression(user) && !ts36.isForOfStatement(user)) {
|
|
10038
10622
|
throw new SpecializeError(node, "A slice with bounds decided at run time has no array to keep: read it where it is made, with every, some, find, findIndex, forEach, includes, indexOf, length or for…of.");
|
|
10039
10623
|
}
|
|
10040
10624
|
}
|
|
@@ -10166,13 +10750,13 @@ class Iterators {
|
|
|
10166
10750
|
};
|
|
10167
10751
|
}
|
|
10168
10752
|
traverses(node, frame) {
|
|
10169
|
-
if (
|
|
10753
|
+
if (ts36.isFunctionLike(node)) {
|
|
10170
10754
|
return false;
|
|
10171
10755
|
}
|
|
10172
|
-
if (
|
|
10756
|
+
if (ts36.isCallExpression(node) && this.traversing(node, frame)) {
|
|
10173
10757
|
return true;
|
|
10174
10758
|
}
|
|
10175
|
-
return
|
|
10759
|
+
return ts36.forEachChild(node, (child) => this.traverses(child, frame)) ?? false;
|
|
10176
10760
|
}
|
|
10177
10761
|
get generators() {
|
|
10178
10762
|
if (!this.runner) {
|
|
@@ -10181,7 +10765,7 @@ class Iterators {
|
|
|
10181
10765
|
return this.runner;
|
|
10182
10766
|
}
|
|
10183
10767
|
helper(node) {
|
|
10184
|
-
if (!
|
|
10768
|
+
if (!ts36.isCallExpression(node) || !ts36.isPropertyAccessExpression(node.expression)) {
|
|
10185
10769
|
return;
|
|
10186
10770
|
}
|
|
10187
10771
|
const receiver = node.expression.expression;
|
|
@@ -10195,11 +10779,11 @@ class Iterators {
|
|
|
10195
10779
|
if (this.helper(node)) {
|
|
10196
10780
|
return true;
|
|
10197
10781
|
}
|
|
10198
|
-
return
|
|
10782
|
+
return ts36.isPropertyAccessExpression(node.expression) && node.expression.name.text !== "slice" && this.loopsAtRunTime(node.expression.expression, frame, { sliced: false });
|
|
10199
10783
|
}
|
|
10200
10784
|
loopsAtRunTime(node, frame, { sliced }) {
|
|
10201
10785
|
const inner = unwrapped(node);
|
|
10202
|
-
if (
|
|
10786
|
+
if (ts36.isCallExpression(inner) && ts36.isPropertyAccessExpression(inner.expression) && inner.expression.name.text === "slice") {
|
|
10203
10787
|
return this.loopsAtRunTime(inner.expression.expression, frame, {
|
|
10204
10788
|
sliced: true
|
|
10205
10789
|
});
|
|
@@ -10211,11 +10795,11 @@ class Iterators {
|
|
|
10211
10795
|
return sliced && value !== undefined && this.storage(value) !== undefined;
|
|
10212
10796
|
}
|
|
10213
10797
|
glance(node, frame, { sliced }) {
|
|
10214
|
-
if (
|
|
10798
|
+
if (ts36.isIdentifier(node)) {
|
|
10215
10799
|
const symbol = this.context.checker.getSymbolAtLocation(node);
|
|
10216
10800
|
return symbol && frame.env.get(symbol);
|
|
10217
10801
|
}
|
|
10218
|
-
if (!sliced || !
|
|
10802
|
+
if (!sliced || !ts36.isPropertyAccessExpression(node)) {
|
|
10219
10803
|
return;
|
|
10220
10804
|
}
|
|
10221
10805
|
try {
|
|
@@ -10237,7 +10821,7 @@ class Iterators {
|
|
|
10237
10821
|
return this.generated(generator, inner);
|
|
10238
10822
|
}
|
|
10239
10823
|
const helper = this.helper(inner);
|
|
10240
|
-
if (!helper || !
|
|
10824
|
+
if (!helper || !ts36.isCallExpression(inner)) {
|
|
10241
10825
|
throw new SpecializeError(node, "A generator runs in place: call it where a for…of or an iterator helper reads it.");
|
|
10242
10826
|
}
|
|
10243
10827
|
const source = this.pipeline(helper.receiver, frame);
|
|
@@ -10831,7 +11415,7 @@ var all = (conditions) => {
|
|
|
10831
11415
|
const tests = conditions.filter((condition) => condition instanceof Mapped);
|
|
10832
11416
|
return tests.length ? joined(tests, " && ") : true;
|
|
10833
11417
|
};
|
|
10834
|
-
var takes = (callback, parameter) => callback.kind === "function" && !
|
|
11418
|
+
var takes = (callback, parameter) => callback.kind === "function" && !ts36.isGetAccessorDeclaration(callback.declaration) && callback.declaration.parameters.some((declared, position) => position === parameter || declared.dotDotDotToken !== undefined && position < parameter);
|
|
10835
11419
|
var outside = (node) => isWrapper(node.parent) ? outside(node.parent) : node.parent;
|
|
10836
11420
|
|
|
10837
11421
|
// packages/compiler/src/frontend/elaborate/expressions.ts
|
|
@@ -10862,50 +11446,50 @@ class ExpressionSpecializer {
|
|
|
10862
11446
|
}
|
|
10863
11447
|
return {
|
|
10864
11448
|
...value,
|
|
10865
|
-
text: from(node,
|
|
11449
|
+
text: from(node, ts37.isIdentifier(node) ? String(value.text) : value.text)
|
|
10866
11450
|
};
|
|
10867
11451
|
}
|
|
10868
11452
|
evaluate(node, frame, expected) {
|
|
10869
|
-
if (
|
|
11453
|
+
if (ts37.isNonNullExpression(node)) {
|
|
10870
11454
|
return this.narrowed(this.expression(node.expression, frame, expected), node);
|
|
10871
11455
|
}
|
|
10872
11456
|
const inner = unwrapped(node);
|
|
10873
11457
|
if (inner !== node) {
|
|
10874
11458
|
return this.expression(inner, frame, expected);
|
|
10875
11459
|
}
|
|
10876
|
-
if (
|
|
11460
|
+
if (ts37.isNumericLiteral(node)) {
|
|
10877
11461
|
return constant(Number(node.text));
|
|
10878
11462
|
}
|
|
10879
|
-
if (
|
|
11463
|
+
if (ts37.isStringLiteral(node) || ts37.isNoSubstitutionTemplateLiteral(node)) {
|
|
10880
11464
|
return constant(node.text);
|
|
10881
11465
|
}
|
|
10882
|
-
if (node.kind ===
|
|
11466
|
+
if (node.kind === ts37.SyntaxKind.TrueKeyword) {
|
|
10883
11467
|
return constant(true);
|
|
10884
11468
|
}
|
|
10885
|
-
if (node.kind ===
|
|
11469
|
+
if (node.kind === ts37.SyntaxKind.FalseKeyword) {
|
|
10886
11470
|
return constant(false);
|
|
10887
11471
|
}
|
|
10888
|
-
if (node.kind ===
|
|
11472
|
+
if (node.kind === ts37.SyntaxKind.NullKeyword) {
|
|
10889
11473
|
return constant(undefined);
|
|
10890
11474
|
}
|
|
10891
|
-
if (
|
|
11475
|
+
if (ts37.isBigIntLiteral(node)) {
|
|
10892
11476
|
return constant(BigInt(node.text.slice(0, -1)));
|
|
10893
11477
|
}
|
|
10894
|
-
if (node.kind ===
|
|
11478
|
+
if (node.kind === ts37.SyntaxKind.ThisKeyword) {
|
|
10895
11479
|
if (!frame.self) {
|
|
10896
11480
|
throw new SpecializeError(node, "`this` outside a method.");
|
|
10897
11481
|
}
|
|
10898
11482
|
return { kind: "static", node: frame.self };
|
|
10899
11483
|
}
|
|
10900
|
-
if (
|
|
11484
|
+
if (ts37.isIdentifier(node)) {
|
|
10901
11485
|
return this.identifier(node, frame);
|
|
10902
11486
|
}
|
|
10903
|
-
if (
|
|
11487
|
+
if (ts37.isPropertyAccessExpression(node)) {
|
|
10904
11488
|
const member = this.context.checker.getSymbolAtLocation(node.name)?.valueDeclaration;
|
|
10905
|
-
if (member &&
|
|
11489
|
+
if (member && ts37.isEnumMember(member)) {
|
|
10906
11490
|
return constant(this.context.checker.getConstantValue(member));
|
|
10907
11491
|
}
|
|
10908
|
-
if (node.expression.kind ===
|
|
11492
|
+
if (node.expression.kind === ts37.SyntaxKind.SuperKeyword) {
|
|
10909
11493
|
return this.superMember(node, frame);
|
|
10910
11494
|
}
|
|
10911
11495
|
const mathConstant = mathConstants.get(node.name.text);
|
|
@@ -10917,15 +11501,15 @@ class ExpressionSpecializer {
|
|
|
10917
11501
|
}
|
|
10918
11502
|
return this.property(this.expression(node.expression, frame), node, frame);
|
|
10919
11503
|
}
|
|
10920
|
-
if (
|
|
11504
|
+
if (ts37.isElementAccessExpression(node)) {
|
|
10921
11505
|
const object = this.expression(node.expression, frame);
|
|
10922
11506
|
const index = this.expression(node.argumentExpression, frame);
|
|
10923
11507
|
return this.element(object, index, node, frame);
|
|
10924
11508
|
}
|
|
10925
|
-
if (
|
|
11509
|
+
if (ts37.isCallExpression(node)) {
|
|
10926
11510
|
return this.calls.call(node, frame, expected);
|
|
10927
11511
|
}
|
|
10928
|
-
if (
|
|
11512
|
+
if (ts37.isAwaitExpression(node)) {
|
|
10929
11513
|
const value = this.expression(node.expression, frame);
|
|
10930
11514
|
if (isPromiseLink(unwrapped(node.expression)) || value.kind !== "code") {
|
|
10931
11515
|
return value;
|
|
@@ -10941,16 +11525,16 @@ class ExpressionSpecializer {
|
|
|
10941
11525
|
if (isUpdate(node)) {
|
|
10942
11526
|
return this.assignments.step(node, frame, { used: true });
|
|
10943
11527
|
}
|
|
10944
|
-
if (
|
|
11528
|
+
if (ts37.isTypeOfExpression(node)) {
|
|
10945
11529
|
return this.typeOf(this.expression(node.expression, frame));
|
|
10946
11530
|
}
|
|
10947
|
-
if (
|
|
11531
|
+
if (ts37.isPrefixUnaryExpression(node)) {
|
|
10948
11532
|
return this.unary(node, frame);
|
|
10949
11533
|
}
|
|
10950
|
-
if (
|
|
11534
|
+
if (ts37.isBinaryExpression(node)) {
|
|
10951
11535
|
return this.binary(node, frame, expected);
|
|
10952
11536
|
}
|
|
10953
|
-
if (
|
|
11537
|
+
if (ts37.isConditionalExpression(node)) {
|
|
10954
11538
|
const condition = this.expression(node.condition, frame);
|
|
10955
11539
|
if (condition.kind === "const") {
|
|
10956
11540
|
return this.expression(condition.value ? node.whenTrue : node.whenFalse, frame, expected);
|
|
@@ -10975,7 +11559,7 @@ class ExpressionSpecializer {
|
|
|
10975
11559
|
copyOf: [whenTrue, whenFalse].find((branch) => branch.kind === "code" && branch.copyOf !== undefined)?.copyOf
|
|
10976
11560
|
};
|
|
10977
11561
|
}
|
|
10978
|
-
if (
|
|
11562
|
+
if (ts37.isArrowFunction(node) || ts37.isFunctionExpression(node)) {
|
|
10979
11563
|
return {
|
|
10980
11564
|
kind: "function",
|
|
10981
11565
|
declaration: node,
|
|
@@ -10983,13 +11567,13 @@ class ExpressionSpecializer {
|
|
|
10983
11567
|
env: frame.env
|
|
10984
11568
|
};
|
|
10985
11569
|
}
|
|
10986
|
-
if (
|
|
11570
|
+
if (ts37.isObjectLiteralExpression(node)) {
|
|
10987
11571
|
return this.objectLiteral(node, frame);
|
|
10988
11572
|
}
|
|
10989
|
-
if (
|
|
11573
|
+
if (ts37.isArrayLiteralExpression(node)) {
|
|
10990
11574
|
const items = [];
|
|
10991
11575
|
for (const element of node.elements) {
|
|
10992
|
-
if (
|
|
11576
|
+
if (ts37.isSpreadElement(element)) {
|
|
10993
11577
|
const spread = this.items(this.expression(element.expression, frame));
|
|
10994
11578
|
if (!spread) {
|
|
10995
11579
|
throw new SpecializeError(element, "Spread an array setup built.");
|
|
@@ -11001,10 +11585,10 @@ class ExpressionSpecializer {
|
|
|
11001
11585
|
}
|
|
11002
11586
|
return { kind: "virtual", items };
|
|
11003
11587
|
}
|
|
11004
|
-
if (
|
|
11588
|
+
if (ts37.isTemplateExpression(node)) {
|
|
11005
11589
|
throw new SpecializeError(node, "Template strings are not supported in scan code.");
|
|
11006
11590
|
}
|
|
11007
|
-
throw new SpecializeError(node, `${
|
|
11591
|
+
throw new SpecializeError(node, `${ts37.SyntaxKind[node.kind]} is not supported in scan code.`);
|
|
11008
11592
|
}
|
|
11009
11593
|
property(object, node, frame) {
|
|
11010
11594
|
if (node.questionDotToken && object.kind === "const" && object.value === undefined) {
|
|
@@ -11039,11 +11623,11 @@ class ExpressionSpecializer {
|
|
|
11039
11623
|
if (!declaration) {
|
|
11040
11624
|
throw new SpecializeError(node, `${node.text} has no declaration.`);
|
|
11041
11625
|
}
|
|
11042
|
-
if (
|
|
11626
|
+
if (ts37.isFunctionDeclaration(declaration)) {
|
|
11043
11627
|
return { kind: "function", declaration };
|
|
11044
11628
|
}
|
|
11045
|
-
const initializer =
|
|
11046
|
-
if (initializer && (
|
|
11629
|
+
const initializer = ts37.isVariableDeclaration(declaration) && declaration.initializer;
|
|
11630
|
+
if (initializer && (ts37.isArrowFunction(initializer) || ts37.isFunctionExpression(initializer))) {
|
|
11047
11631
|
return { kind: "function", declaration: initializer };
|
|
11048
11632
|
}
|
|
11049
11633
|
const root = this.context.model.roots.find((item) => item.declaration === declaration);
|
|
@@ -11052,13 +11636,13 @@ class ExpressionSpecializer {
|
|
|
11052
11636
|
this.refuseOutsideWidth(value, this.context.layouts.iecName(root.declaration), root.declaration);
|
|
11053
11637
|
return value;
|
|
11054
11638
|
}
|
|
11055
|
-
if (initializer &&
|
|
11639
|
+
if (initializer && ts37.isVariableStatement(declaration.parent.parent) && declaration.parent.parent.parent && ts37.isSourceFile(declaration.parent.parent.parent)) {
|
|
11056
11640
|
return this.expression(initializer, { ...frame, env: new Map });
|
|
11057
11641
|
}
|
|
11058
|
-
if (
|
|
11642
|
+
if (ts37.isEnumMember(declaration)) {
|
|
11059
11643
|
return constant(this.context.checker.getConstantValue(declaration));
|
|
11060
11644
|
}
|
|
11061
|
-
if (
|
|
11645
|
+
if (ts37.isParameter(declaration)) {
|
|
11062
11646
|
throw new SpecializeError(node, `${node.text} is a parameter of a scan function, which its task calls with no arguments: read the value from the program's state instead.`);
|
|
11063
11647
|
}
|
|
11064
11648
|
throw new SpecializeError(node, `${node.text} is not available in scan code.`);
|
|
@@ -11311,11 +11895,11 @@ class ExpressionSpecializer {
|
|
|
11311
11895
|
objectLiteral(node, frame) {
|
|
11312
11896
|
const fields = new Map;
|
|
11313
11897
|
for (const property of node.properties) {
|
|
11314
|
-
if (
|
|
11898
|
+
if (ts37.isPropertyAssignment(property)) {
|
|
11315
11899
|
fields.set(property.name.getText(), this.expression(property.initializer, frame, this.context.checker.getContextualType(property.initializer)));
|
|
11316
|
-
} else if (
|
|
11900
|
+
} else if (ts37.isShorthandPropertyAssignment(property)) {
|
|
11317
11901
|
fields.set(property.name.text, this.shorthand(property, frame));
|
|
11318
|
-
} else if (
|
|
11902
|
+
} else if (ts37.isSpreadAssignment(property)) {
|
|
11319
11903
|
for (const [name, value] of this.recordFields(property.expression, frame)) {
|
|
11320
11904
|
fields.set(name, value);
|
|
11321
11905
|
}
|
|
@@ -11333,7 +11917,7 @@ class ExpressionSpecializer {
|
|
|
11333
11917
|
unary(node, frame) {
|
|
11334
11918
|
const operand = this.expression(node.operand, frame);
|
|
11335
11919
|
switch (node.operator) {
|
|
11336
|
-
case
|
|
11920
|
+
case ts37.SyntaxKind.ExclamationToken:
|
|
11337
11921
|
if (operand.kind === "const") {
|
|
11338
11922
|
return constant(!operand.value);
|
|
11339
11923
|
}
|
|
@@ -11345,7 +11929,7 @@ class ExpressionSpecializer {
|
|
|
11345
11929
|
text: mapped`!${this.context.dialect.condition(operand, node.operand)}`,
|
|
11346
11930
|
declared: this.context.checker.getBooleanType()
|
|
11347
11931
|
};
|
|
11348
|
-
case
|
|
11932
|
+
case ts37.SyntaxKind.MinusToken:
|
|
11349
11933
|
if (operand.kind === "const" && typeof operand.value === "number") {
|
|
11350
11934
|
return constant(-operand.value);
|
|
11351
11935
|
}
|
|
@@ -11354,17 +11938,17 @@ class ExpressionSpecializer {
|
|
|
11354
11938
|
text: mapped`-${this.context.dialect.code(operand, undefined)}`,
|
|
11355
11939
|
declared: this.context.checker.getTypeAtLocation(node)
|
|
11356
11940
|
};
|
|
11357
|
-
case
|
|
11941
|
+
case ts37.SyntaxKind.PlusToken:
|
|
11358
11942
|
return operand;
|
|
11359
11943
|
default:
|
|
11360
|
-
throw new SpecializeError(node, `${
|
|
11944
|
+
throw new SpecializeError(node, `${ts37.tokenToString(node.operator)} is not supported.`);
|
|
11361
11945
|
}
|
|
11362
11946
|
}
|
|
11363
11947
|
binary(node, frame, expected) {
|
|
11364
11948
|
const kind = node.operatorToken.kind;
|
|
11365
|
-
if (kind ===
|
|
11949
|
+
if (kind === ts37.SyntaxKind.AmpersandAmpersandToken || kind === ts37.SyntaxKind.BarBarToken) {
|
|
11366
11950
|
const left = this.expression(node.left, frame);
|
|
11367
|
-
const and = kind ===
|
|
11951
|
+
const and = kind === ts37.SyntaxKind.AmpersandAmpersandToken;
|
|
11368
11952
|
if (left.kind === "const") {
|
|
11369
11953
|
if (Boolean(left.value) !== and) {
|
|
11370
11954
|
return left;
|
|
@@ -11417,22 +12001,22 @@ class ExpressionSpecializer {
|
|
|
11417
12001
|
declared: this.context.checker.getBooleanType()
|
|
11418
12002
|
};
|
|
11419
12003
|
}
|
|
11420
|
-
if (kind ===
|
|
12004
|
+
if (kind === ts37.SyntaxKind.QuestionQuestionToken) {
|
|
11421
12005
|
return this.orElse(this.expression(node.left, frame), node.right, frame, expected);
|
|
11422
12006
|
}
|
|
11423
12007
|
if (isAssignment(kind)) {
|
|
11424
12008
|
return this.assignments.assign(node, frame);
|
|
11425
12009
|
}
|
|
11426
12010
|
const equality = [
|
|
11427
|
-
|
|
11428
|
-
|
|
11429
|
-
|
|
11430
|
-
|
|
12011
|
+
ts37.SyntaxKind.EqualsEqualsEqualsToken,
|
|
12012
|
+
ts37.SyntaxKind.ExclamationEqualsEqualsToken,
|
|
12013
|
+
ts37.SyntaxKind.EqualsEqualsToken,
|
|
12014
|
+
ts37.SyntaxKind.ExclamationEqualsToken
|
|
11431
12015
|
].includes(kind);
|
|
11432
12016
|
const read = this.expression(node.left, frame);
|
|
11433
12017
|
const left = containsWrite(node.right) ? this.held(read, frame) : read;
|
|
11434
12018
|
const right = this.expression(node.right, frame, read.kind === "code" ? read.declared : undefined);
|
|
11435
|
-
const negate = kind ===
|
|
12019
|
+
const negate = kind === ts37.SyntaxKind.ExclamationEqualsEqualsToken || kind === ts37.SyntaxKind.ExclamationEqualsToken;
|
|
11436
12020
|
if (equality) {
|
|
11437
12021
|
this.references.identities(left, right, node);
|
|
11438
12022
|
return this.equality(left, right, { negate });
|
|
@@ -11443,7 +12027,7 @@ class ExpressionSpecializer {
|
|
|
11443
12027
|
return constant(finite(folded, node));
|
|
11444
12028
|
}
|
|
11445
12029
|
}
|
|
11446
|
-
if (kind ===
|
|
12030
|
+
if (kind === ts37.SyntaxKind.AsteriskAsteriskToken) {
|
|
11447
12031
|
return this.calls.floatMath("pow", [left, right], node, frame);
|
|
11448
12032
|
}
|
|
11449
12033
|
const declared = this.context.checker.getTypeAtLocation(node);
|
|
@@ -11456,7 +12040,7 @@ class ExpressionSpecializer {
|
|
|
11456
12040
|
declared
|
|
11457
12041
|
};
|
|
11458
12042
|
}
|
|
11459
|
-
if (kind ===
|
|
12043
|
+
if (kind === ts37.SyntaxKind.SlashToken && !floatIec(left) && !floatIec(right)) {
|
|
11460
12044
|
throw new SpecializeError(node, "/ on integers gives a fraction, which no integer holds: divide with div<T>(a, b) from @tsplc/compiler/types, which truncates toward zero, or make an operand REAL or LREAL.");
|
|
11461
12045
|
}
|
|
11462
12046
|
const operator = arithmeticOperators.get(kind);
|
|
@@ -11473,7 +12057,7 @@ class ExpressionSpecializer {
|
|
|
11473
12057
|
}
|
|
11474
12058
|
isNumber(node) {
|
|
11475
12059
|
const flags = this.context.checker.getTypeAtLocation(node).flags;
|
|
11476
|
-
return Boolean(flags & (
|
|
12060
|
+
return Boolean(flags & (ts37.TypeFlags.Number | ts37.TypeFlags.NumberLiteral)) && !(flags & ts37.TypeFlags.EnumLike);
|
|
11477
12061
|
}
|
|
11478
12062
|
firstNonZero(left, node, frame) {
|
|
11479
12063
|
const once = this.temporaryOf(left, frame);
|
|
@@ -11539,7 +12123,7 @@ class ExpressionSpecializer {
|
|
|
11539
12123
|
const name = declaration.name?.getText() ?? "This function";
|
|
11540
12124
|
throw new SpecializeError(at, `${name} is declared without a body, as JavaScript's own functions are: scan code calls functions the program writes and the SDK's, so compute the value in the program, or in setup.`);
|
|
11541
12125
|
}
|
|
11542
|
-
if (target.env &&
|
|
12126
|
+
if (target.env && ts37.isArrowFunction(declaration) && !ts37.isBlock(declaration.body)) {
|
|
11543
12127
|
const inline = {
|
|
11544
12128
|
...frame,
|
|
11545
12129
|
env: new Map(target.env),
|
|
@@ -11570,9 +12154,9 @@ class ExpressionSpecializer {
|
|
|
11570
12154
|
const called = this.routineTable.routine(declaration, target.self, args, target.env && { env: target.env, caller: frame.emitter });
|
|
11571
12155
|
const text = mapped`${called.name}(${joined(called.args, ", ")})`;
|
|
11572
12156
|
const returns = called.returns;
|
|
11573
|
-
if (!returns || returns.flags &
|
|
11574
|
-
const statement =
|
|
11575
|
-
if (
|
|
12157
|
+
if (!returns || returns.flags & ts37.TypeFlags.Void) {
|
|
12158
|
+
const statement = ts37.isAwaitExpression(at.parent) ? at.parent : at;
|
|
12159
|
+
if (ts37.isExpressionStatement(statement.parent) || linksPromise(at)) {
|
|
11576
12160
|
return { kind: "code", text, declared: returns };
|
|
11577
12161
|
}
|
|
11578
12162
|
frame.emitter.line(mapped`${text};`);
|
|
@@ -11592,7 +12176,7 @@ class ExpressionSpecializer {
|
|
|
11592
12176
|
};
|
|
11593
12177
|
}
|
|
11594
12178
|
superMember(node, frame) {
|
|
11595
|
-
const owner =
|
|
12179
|
+
const owner = ts37.findAncestor(node, ts37.isClassDeclaration);
|
|
11596
12180
|
if (!owner || !frame.self) {
|
|
11597
12181
|
throw new SpecializeError(node, "`super` outside a method.");
|
|
11598
12182
|
}
|
|
@@ -11638,10 +12222,10 @@ class ExpressionSpecializer {
|
|
|
11638
12222
|
}
|
|
11639
12223
|
const declared = value.declared;
|
|
11640
12224
|
const kinds = [
|
|
11641
|
-
[
|
|
11642
|
-
[
|
|
11643
|
-
[
|
|
11644
|
-
[
|
|
12225
|
+
[ts37.TypeFlags.NumberLike, "number"],
|
|
12226
|
+
[ts37.TypeFlags.StringLike, "string"],
|
|
12227
|
+
[ts37.TypeFlags.BooleanLike, "boolean"],
|
|
12228
|
+
[ts37.TypeFlags.BigIntLike, "bigint"]
|
|
11645
12229
|
];
|
|
11646
12230
|
const found = kinds.find(([flag]) => declared.flags & flag);
|
|
11647
12231
|
return constant(found ? found[1] : "object");
|
|
@@ -11671,7 +12255,7 @@ class ExpressionSpecializer {
|
|
|
11671
12255
|
return this.references.branch(present, [read, () => constant(undefined)], node, frame);
|
|
11672
12256
|
}
|
|
11673
12257
|
const defined = this.context.checker.getNonNullableType(result);
|
|
11674
|
-
if (result.flags &
|
|
12258
|
+
if (result.flags & ts37.TypeFlags.Void || defined.flags & (ts37.TypeFlags.Void | ts37.TypeFlags.Never)) {
|
|
11675
12259
|
return read();
|
|
11676
12260
|
}
|
|
11677
12261
|
if (!this.context.layouts.isOptionalRecord(result) && !this.context.layouts.literals(result)?.optional) {
|
|
@@ -11830,15 +12414,15 @@ class ExpressionSpecializer {
|
|
|
11830
12414
|
mutable,
|
|
11831
12415
|
iec
|
|
11832
12416
|
}) {
|
|
11833
|
-
const bound = !
|
|
11834
|
-
if (
|
|
12417
|
+
const bound = !ts37.isIdentifier(name) && value.kind === "code" && !value.place ? this.temporaryOf(value, frame) : value;
|
|
12418
|
+
if (ts37.isObjectBindingPattern(name)) {
|
|
11835
12419
|
for (const { element, field } of namedFields(name, "pattern")) {
|
|
11836
12420
|
const inner = this.defaulted(this.member(bound, field, element, frame), element.initializer, frame);
|
|
11837
12421
|
this.bind(element.name, inner, frame, this.context.checker.getTypeAtLocation(element.name), { mutable, iec: undefined });
|
|
11838
12422
|
}
|
|
11839
12423
|
return;
|
|
11840
12424
|
}
|
|
11841
|
-
if (
|
|
12425
|
+
if (ts37.isArrayBindingPattern(name)) {
|
|
11842
12426
|
for (const { element, index } of namedElements(name, "pattern")) {
|
|
11843
12427
|
const inner = this.defaulted(this.element(bound, constant(index), element, frame), element.initializer, frame);
|
|
11844
12428
|
this.bind(element.name, inner, frame, this.context.checker.getTypeAtLocation(element.name), { mutable, iec: undefined });
|
|
@@ -11867,7 +12451,7 @@ class ExpressionSpecializer {
|
|
|
11867
12451
|
});
|
|
11868
12452
|
return;
|
|
11869
12453
|
}
|
|
11870
|
-
if (mutable && bound.kind === "function" &&
|
|
12454
|
+
if (mutable && bound.kind === "function" && ts37.isIdentifier(name)) {
|
|
11871
12455
|
throw new SpecializeError(name, `${name.text} is a let that holds a function, and the PLC calls only functions known when the program compiles: declare it with const, or choose which function to call with an if.`);
|
|
11872
12456
|
}
|
|
11873
12457
|
if (!mutable && bound.kind !== "code") {
|
|
@@ -11875,19 +12459,19 @@ class ExpressionSpecializer {
|
|
|
11875
12459
|
frame.env.set(symbol, bound);
|
|
11876
12460
|
return;
|
|
11877
12461
|
}
|
|
11878
|
-
if (!mutable && bound.kind === "code" && bound.place &&
|
|
12462
|
+
if (!mutable && bound.kind === "code" && bound.place && ts37.isIdentifier(name) && this.isAggregate(this.context.checker.getNonNullableType(declared)) && replacedWhileAliased(name, this.context.checker)) {
|
|
11879
12463
|
this.snapshot(name, symbol, bound, declared, frame);
|
|
11880
12464
|
return;
|
|
11881
12465
|
}
|
|
11882
12466
|
if (!mutable && bound.kind === "code" && bound.place) {
|
|
11883
12467
|
if (this.isAggregate(declared)) {
|
|
11884
12468
|
const pin = bound.pinned;
|
|
11885
|
-
const repeats =
|
|
12469
|
+
const repeats = ts37.isIdentifier(name) && pureRead(bound.text) && stableAlias(name, this.context.checker);
|
|
11886
12470
|
frame.env.set(symbol, pin && !repeats ? pin(frame) : bound);
|
|
11887
12471
|
return;
|
|
11888
12472
|
}
|
|
11889
12473
|
}
|
|
11890
|
-
if (bound.kind === "code" && bound.awaited && bound.declared && bound.declared !== declared && this.isAggregate(declared) && !this.context.checker.isArrayType(declared) &&
|
|
12474
|
+
if (bound.kind === "code" && bound.awaited && bound.declared && bound.declared !== declared && this.isAggregate(declared) && !this.context.checker.isArrayType(declared) && ts37.isVariableDeclaration(name.parent) && name.parent.type !== undefined) {
|
|
11891
12475
|
const reply = frame.emitter.temporary();
|
|
11892
12476
|
frame.emitter.line(mapped`const ${reply} = ${bound.text};`);
|
|
11893
12477
|
const held = {
|
|
@@ -11916,7 +12500,7 @@ class ExpressionSpecializer {
|
|
|
11916
12500
|
throw new SpecializeError(name, `An awaited value keeps its routine's result type, ${bound.iec ?? defaultNumericTypes.number}; store it in a local of that type, then convert it.`);
|
|
11917
12501
|
}
|
|
11918
12502
|
const text = iec ? this.context.dialect.assignmentText(bound, declared, iec) : this.context.dialect.code(bound, declared);
|
|
11919
|
-
const typedByCall = bound.kind === "code" && (bound.awaited || bound.typedByTask) && !iec && !(bound.awaited && this.isAggregate(declared) &&
|
|
12503
|
+
const typedByCall = bound.kind === "code" && (bound.awaited || bound.typedByTask) && !iec && !(bound.awaited && this.isAggregate(declared) && ts37.isVariableDeclaration(name.parent) && name.parent.type !== undefined);
|
|
11920
12504
|
const annotation = typedByCall ? "" : `: ${type}`;
|
|
11921
12505
|
frame.emitter.line(mapped`${mutable || changes ? "let" : "const"} ${local}${annotation} = ${text};`);
|
|
11922
12506
|
frame.env.set(symbol, {
|
|
@@ -11954,7 +12538,7 @@ class ExpressionSpecializer {
|
|
|
11954
12538
|
});
|
|
11955
12539
|
}
|
|
11956
12540
|
isAggregate(type) {
|
|
11957
|
-
return Boolean(type.flags &
|
|
12541
|
+
return Boolean(type.flags & ts37.TypeFlags.Object) && !this.context.layouts.literalEnum(type);
|
|
11958
12542
|
}
|
|
11959
12543
|
items(value) {
|
|
11960
12544
|
if (value.kind === "virtual" && value.items) {
|
|
@@ -11982,44 +12566,44 @@ class ExpressionSpecializer {
|
|
|
11982
12566
|
}
|
|
11983
12567
|
}
|
|
11984
12568
|
var comparisonOperators2 = new Map([
|
|
11985
|
-
[
|
|
11986
|
-
[
|
|
11987
|
-
[
|
|
11988
|
-
[
|
|
12569
|
+
[ts37.SyntaxKind.LessThanToken, "<"],
|
|
12570
|
+
[ts37.SyntaxKind.LessThanEqualsToken, "<="],
|
|
12571
|
+
[ts37.SyntaxKind.GreaterThanToken, ">"],
|
|
12572
|
+
[ts37.SyntaxKind.GreaterThanEqualsToken, ">="]
|
|
11989
12573
|
]);
|
|
11990
12574
|
var arithmeticOperators = new Map([
|
|
11991
|
-
[
|
|
11992
|
-
[
|
|
11993
|
-
[
|
|
11994
|
-
[
|
|
11995
|
-
[
|
|
11996
|
-
[
|
|
11997
|
-
[
|
|
11998
|
-
[
|
|
11999
|
-
[
|
|
12000
|
-
[
|
|
12575
|
+
[ts37.SyntaxKind.PlusToken, "+"],
|
|
12576
|
+
[ts37.SyntaxKind.MinusToken, "-"],
|
|
12577
|
+
[ts37.SyntaxKind.AsteriskToken, "*"],
|
|
12578
|
+
[ts37.SyntaxKind.SlashToken, "/"],
|
|
12579
|
+
[ts37.SyntaxKind.PercentToken, "%"],
|
|
12580
|
+
[ts37.SyntaxKind.AmpersandToken, "&"],
|
|
12581
|
+
[ts37.SyntaxKind.BarToken, "|"],
|
|
12582
|
+
[ts37.SyntaxKind.CaretToken, "^"],
|
|
12583
|
+
[ts37.SyntaxKind.LessThanLessThanToken, "<<"],
|
|
12584
|
+
[ts37.SyntaxKind.GreaterThanGreaterThanToken, ">>"]
|
|
12001
12585
|
]);
|
|
12002
12586
|
var foldNumber = (kind, left, right) => {
|
|
12003
12587
|
switch (kind) {
|
|
12004
|
-
case
|
|
12588
|
+
case ts37.SyntaxKind.PlusToken:
|
|
12005
12589
|
return left + right;
|
|
12006
|
-
case
|
|
12590
|
+
case ts37.SyntaxKind.MinusToken:
|
|
12007
12591
|
return left - right;
|
|
12008
|
-
case
|
|
12592
|
+
case ts37.SyntaxKind.AsteriskToken:
|
|
12009
12593
|
return left * right;
|
|
12010
|
-
case
|
|
12594
|
+
case ts37.SyntaxKind.AsteriskAsteriskToken:
|
|
12011
12595
|
return left ** right;
|
|
12012
|
-
case
|
|
12596
|
+
case ts37.SyntaxKind.SlashToken:
|
|
12013
12597
|
return left / right;
|
|
12014
|
-
case
|
|
12598
|
+
case ts37.SyntaxKind.PercentToken:
|
|
12015
12599
|
return right === 0 ? undefined : left % right;
|
|
12016
|
-
case
|
|
12600
|
+
case ts37.SyntaxKind.LessThanToken:
|
|
12017
12601
|
return left < right;
|
|
12018
|
-
case
|
|
12602
|
+
case ts37.SyntaxKind.LessThanEqualsToken:
|
|
12019
12603
|
return left <= right;
|
|
12020
|
-
case
|
|
12604
|
+
case ts37.SyntaxKind.GreaterThanToken:
|
|
12021
12605
|
return left > right;
|
|
12022
|
-
case
|
|
12606
|
+
case ts37.SyntaxKind.GreaterThanEqualsToken:
|
|
12023
12607
|
return left >= right;
|
|
12024
12608
|
default:
|
|
12025
12609
|
return;
|
|
@@ -12039,50 +12623,50 @@ var sourceName = (target) => {
|
|
|
12039
12623
|
const own = target.declaration.name?.getText() ?? "this function";
|
|
12040
12624
|
return target.self?.captured.kind === "instance" ? `${target.self.captured.name}.${own}` : own;
|
|
12041
12625
|
};
|
|
12042
|
-
var isPromiseLink = (node) =>
|
|
12626
|
+
var isPromiseLink = (node) => ts37.isCallExpression(node) && ts37.isPropertyAccessExpression(node.expression) && promiseLinks.has(node.expression.name.text);
|
|
12043
12627
|
var promiseLinks = new Set(["then", "catch"]);
|
|
12044
|
-
var linksPromise = (node) =>
|
|
12045
|
-
var scopeOf = (name) =>
|
|
12046
|
-
var getterNamed = (members, name) => members.find((member) =>
|
|
12047
|
-
var methodNamed = (members, name) => members.find((member) =>
|
|
12628
|
+
var linksPromise = (node) => ts37.isPropertyAccessExpression(node.parent) && node.parent.expression === node && isPromiseLink(node.parent.parent);
|
|
12629
|
+
var scopeOf = (name) => ts37.findAncestor(name.parent, ts37.isFunctionLike) ?? name.getSourceFile();
|
|
12630
|
+
var getterNamed = (members, name) => members.find((member) => ts37.isGetAccessorDeclaration(member) && member.name.getText() === name);
|
|
12631
|
+
var methodNamed = (members, name) => members.find((member) => ts37.isMethodDeclaration(member) && member.name.getText() === name);
|
|
12048
12632
|
var isReadonly = (type, checker) => !checker.isArrayType(type) && type.getSymbol()?.name === "ReadonlyArray" || type.aliasSymbol?.name === "Readonly" || checker.typeToString(type).startsWith("readonly ");
|
|
12049
12633
|
var findClosure = (source, start) => {
|
|
12050
12634
|
const visit = (node) => {
|
|
12051
|
-
if ((
|
|
12635
|
+
if ((ts37.isArrowFunction(node) || ts37.isFunctionExpression(node)) && node.getStart(source) === start) {
|
|
12052
12636
|
return node;
|
|
12053
12637
|
}
|
|
12054
|
-
return node.pos <= start && start < node.end ?
|
|
12638
|
+
return node.pos <= start && start < node.end ? ts37.forEachChild(node, visit) : undefined;
|
|
12055
12639
|
};
|
|
12056
12640
|
return visit(source);
|
|
12057
12641
|
};
|
|
12058
12642
|
var closureSymbols = (declaration, checker) => {
|
|
12059
12643
|
const found = new Map;
|
|
12060
12644
|
const visit = (node) => {
|
|
12061
|
-
if (
|
|
12645
|
+
if (ts37.isIdentifier(node)) {
|
|
12062
12646
|
const symbol = valueSymbol(checker, node);
|
|
12063
12647
|
const known = found.get(node.text) ?? [];
|
|
12064
12648
|
if (symbol && !known.includes(symbol)) {
|
|
12065
12649
|
found.set(node.text, [...known, symbol]);
|
|
12066
12650
|
}
|
|
12067
12651
|
}
|
|
12068
|
-
|
|
12652
|
+
ts37.forEachChild(node, visit);
|
|
12069
12653
|
};
|
|
12070
12654
|
visit(declaration);
|
|
12071
12655
|
return found;
|
|
12072
12656
|
};
|
|
12073
12657
|
var onlyReturned = (accessor) => {
|
|
12074
12658
|
const [only, ...others] = accessor.body?.statements ?? [];
|
|
12075
|
-
return only && !others.length &&
|
|
12659
|
+
return only && !others.length && ts37.isReturnStatement(only) ? only.expression : undefined;
|
|
12076
12660
|
};
|
|
12077
12661
|
var pureRead = (text) => !/[\w\]>]\s*\(/.test(String(text).replace(/\b(?:wrap|exact)<\w+>\(/g, "("));
|
|
12078
12662
|
|
|
12079
12663
|
// packages/compiler/src/frontend/elaborate/initializers.ts
|
|
12080
|
-
import
|
|
12664
|
+
import ts39 from "typescript";
|
|
12081
12665
|
|
|
12082
12666
|
// packages/compiler/src/frontend/elaborate/storage.ts
|
|
12083
12667
|
import { basename as basename2 } from "node:path";
|
|
12084
12668
|
import { pascalCase as pascalCase3 } from "@tsplc/compiler/ir";
|
|
12085
|
-
import
|
|
12669
|
+
import ts38 from "typescript";
|
|
12086
12670
|
class Storage {
|
|
12087
12671
|
model;
|
|
12088
12672
|
checker;
|
|
@@ -12102,7 +12686,7 @@ class Storage {
|
|
|
12102
12686
|
this.kept = kept;
|
|
12103
12687
|
this.rootModule = rootModule;
|
|
12104
12688
|
this.writes = new ScanWrites(checker, sources);
|
|
12105
|
-
this.typeNames = new Set(sources.flatMap((source) => source.statements.flatMap((statement) => (
|
|
12689
|
+
this.typeNames = new Set(sources.flatMap((source) => source.statements.flatMap((statement) => (ts38.isInterfaceDeclaration(statement) || ts38.isTypeAliasDeclaration(statement) || ts38.isClassDeclaration(statement) || ts38.isEnumDeclaration(statement)) && statement.name ? [statement.name.text] : [])));
|
|
12106
12690
|
}
|
|
12107
12691
|
typeNames;
|
|
12108
12692
|
fieldSymbol(holder, name) {
|
|
@@ -12123,7 +12707,7 @@ class Storage {
|
|
|
12123
12707
|
}
|
|
12124
12708
|
fixed(holder, name) {
|
|
12125
12709
|
const declaration = this.fieldDeclaration(holder, name);
|
|
12126
|
-
return holder.owner !== undefined && !holder.owner.root.bound && declaration !== undefined &&
|
|
12710
|
+
return holder.owner !== undefined && !holder.owner.root.bound && declaration !== undefined && ts38.canHaveModifiers(declaration) && hasModifier(declaration, ts38.SyntaxKind.ReadonlyKeyword) && !this.written(holder, name);
|
|
12127
12711
|
}
|
|
12128
12712
|
written(holder, name) {
|
|
12129
12713
|
const symbol = this.fieldSymbol(holder, name);
|
|
@@ -12211,15 +12795,13 @@ class Storage {
|
|
|
12211
12795
|
return false;
|
|
12212
12796
|
}
|
|
12213
12797
|
const key = node.type && recordTypes(node.type)?.key;
|
|
12214
|
-
return !key || (key.flags & (
|
|
12798
|
+
return !key || (key.flags & (ts38.TypeFlags.EnumLike | ts38.TypeFlags.NumberLike)) === 0;
|
|
12215
12799
|
}
|
|
12216
12800
|
reach(root) {
|
|
12217
12801
|
const module = this.rootModule(root);
|
|
12218
12802
|
const pascal = pascalCase3(basename2(module, ".ts"));
|
|
12219
|
-
const namespace = this.typeNames.has(pascal) ? `${pascal}Root` : pascal;
|
|
12220
12803
|
this.storedRoots.add(root);
|
|
12221
|
-
this.imports.
|
|
12222
|
-
return namespace;
|
|
12804
|
+
return this.imports.namespace(module, this.typeNames.has(pascal) ? `${pascal}Root` : pascal);
|
|
12223
12805
|
}
|
|
12224
12806
|
use(root) {
|
|
12225
12807
|
this.reach(root);
|
|
@@ -12422,7 +13004,7 @@ class Storage {
|
|
|
12422
13004
|
}
|
|
12423
13005
|
return { kind: "static", node };
|
|
12424
13006
|
}
|
|
12425
|
-
if (this.kept(root.file) ||
|
|
13007
|
+
if (this.kept(root.file) || ts38.getCombinedNodeFlags(root.declaration) & ts38.NodeFlags.Const) {
|
|
12426
13008
|
return capturedPrimitive(value);
|
|
12427
13009
|
}
|
|
12428
13010
|
const namespace = this.reach(root);
|
|
@@ -12437,12 +13019,11 @@ class Storage {
|
|
|
12437
13019
|
};
|
|
12438
13020
|
}
|
|
12439
13021
|
keptRoot(root) {
|
|
12440
|
-
this.imports.kept.add(root.file, root.name);
|
|
12441
13022
|
return {
|
|
12442
13023
|
kind: "code",
|
|
12443
|
-
text: root.name,
|
|
13024
|
+
text: this.imports.kept.add(root.file, root.name),
|
|
12444
13025
|
declared: this.checker.getTypeAtLocation(root.declaration.name),
|
|
12445
|
-
place: !(
|
|
13026
|
+
place: !(ts38.getCombinedNodeFlags(root.declaration) & ts38.NodeFlags.Const)
|
|
12446
13027
|
};
|
|
12447
13028
|
}
|
|
12448
13029
|
fromCaptured(value, holder, step) {
|
|
@@ -12723,13 +13304,13 @@ class Initializers {
|
|
|
12723
13304
|
}
|
|
12724
13305
|
}
|
|
12725
13306
|
defaultOf(type) {
|
|
12726
|
-
if (!type || type.flags &
|
|
13307
|
+
if (!type || type.flags & ts39.TypeFlags.Void) {
|
|
12727
13308
|
return "";
|
|
12728
13309
|
}
|
|
12729
|
-
if (type.flags &
|
|
13310
|
+
if (type.flags & ts39.TypeFlags.BooleanLike) {
|
|
12730
13311
|
return "false";
|
|
12731
13312
|
}
|
|
12732
|
-
if (type.flags &
|
|
13313
|
+
if (type.flags & ts39.TypeFlags.NumberLike) {
|
|
12733
13314
|
return "0";
|
|
12734
13315
|
}
|
|
12735
13316
|
return `defaultValue<${this.layouts.text(type)}>()`;
|
|
@@ -12738,7 +13319,7 @@ class Initializers {
|
|
|
12738
13319
|
|
|
12739
13320
|
// packages/compiler/src/frontend/elaborate/statements.ts
|
|
12740
13321
|
import { loopIterations } from "@tsplc/iec/arithmetic";
|
|
12741
|
-
import
|
|
13322
|
+
import ts40 from "typescript";
|
|
12742
13323
|
class StatementSpecializer {
|
|
12743
13324
|
context;
|
|
12744
13325
|
expressions;
|
|
@@ -12753,7 +13334,7 @@ class StatementSpecializer {
|
|
|
12753
13334
|
});
|
|
12754
13335
|
}
|
|
12755
13336
|
body(node, frame) {
|
|
12756
|
-
if (
|
|
13337
|
+
if (ts40.isBlock(node)) {
|
|
12757
13338
|
this.statements(node.statements, frame);
|
|
12758
13339
|
} else {
|
|
12759
13340
|
frame.emitter.origin = node;
|
|
@@ -12763,7 +13344,7 @@ class StatementSpecializer {
|
|
|
12763
13344
|
statements(nodes, frame) {
|
|
12764
13345
|
for (const [index, statement] of nodes.entries()) {
|
|
12765
13346
|
const rest = nodes.slice(index + 1);
|
|
12766
|
-
if (
|
|
13347
|
+
if (ts40.isIfStatement(statement) && !statement.elseStatement && rest.length) {
|
|
12767
13348
|
return this.recorded(statement, frame, () => this.guard(statement, rest, frame));
|
|
12768
13349
|
}
|
|
12769
13350
|
if (this.statement(statement, frame)) {
|
|
@@ -12809,7 +13390,7 @@ class StatementSpecializer {
|
|
|
12809
13390
|
try {
|
|
12810
13391
|
return specialize();
|
|
12811
13392
|
} catch (cause) {
|
|
12812
|
-
if (!(cause instanceof SpecializeError) ||
|
|
13393
|
+
if (!(cause instanceof SpecializeError) || ts40.isBlock(node) || node.getChildren().some((child) => ts40.isBlock(child))) {
|
|
12813
13394
|
throw cause;
|
|
12814
13395
|
}
|
|
12815
13396
|
if (!this.failures.some((failure) => failure.node === cause.node && failure.message === cause.message)) {
|
|
@@ -12822,54 +13403,54 @@ class StatementSpecializer {
|
|
|
12822
13403
|
}
|
|
12823
13404
|
specializeStatement(node, frame) {
|
|
12824
13405
|
const { emitter } = frame;
|
|
12825
|
-
if (
|
|
13406
|
+
if (ts40.isBlock(node)) {
|
|
12826
13407
|
emitter.open("{");
|
|
12827
13408
|
const jumps = this.statements(node.statements, frame);
|
|
12828
13409
|
emitter.close();
|
|
12829
13410
|
return jumps;
|
|
12830
13411
|
}
|
|
12831
|
-
if (
|
|
12832
|
-
const mutable = !(node.declarationList.flags &
|
|
13412
|
+
if (ts40.isVariableStatement(node)) {
|
|
13413
|
+
const mutable = !(node.declarationList.flags & ts40.NodeFlags.Const);
|
|
12833
13414
|
for (const declaration of node.declarationList.declarations) {
|
|
12834
13415
|
this.declare(declaration, mutable, frame);
|
|
12835
13416
|
}
|
|
12836
13417
|
return false;
|
|
12837
13418
|
}
|
|
12838
|
-
if (
|
|
12839
|
-
if (
|
|
13419
|
+
if (ts40.isExpressionStatement(node)) {
|
|
13420
|
+
if (ts40.isYieldExpression(node.expression)) {
|
|
12840
13421
|
this.yieldStatement(node.expression, frame);
|
|
12841
13422
|
return false;
|
|
12842
13423
|
}
|
|
12843
13424
|
this.effect(node.expression, frame);
|
|
12844
13425
|
return false;
|
|
12845
13426
|
}
|
|
12846
|
-
if (
|
|
13427
|
+
if (ts40.isIfStatement(node)) {
|
|
12847
13428
|
return this.ifStatement(node, frame);
|
|
12848
13429
|
}
|
|
12849
|
-
if (
|
|
13430
|
+
if (ts40.isReturnStatement(node)) {
|
|
12850
13431
|
this.returnStatement(node, frame);
|
|
12851
13432
|
return true;
|
|
12852
13433
|
}
|
|
12853
|
-
if (
|
|
13434
|
+
if (ts40.isIterationStatement(node, false) || ts40.isSwitchStatement(node)) {
|
|
12854
13435
|
this.loopOrSwitch(node, [], frame);
|
|
12855
13436
|
return false;
|
|
12856
13437
|
}
|
|
12857
|
-
if (
|
|
13438
|
+
if (ts40.isTryStatement(node)) {
|
|
12858
13439
|
this.tryStatement(node, frame);
|
|
12859
13440
|
return false;
|
|
12860
13441
|
}
|
|
12861
|
-
if (
|
|
13442
|
+
if (ts40.isBreakStatement(node) || ts40.isContinueStatement(node)) {
|
|
12862
13443
|
emitter.line(this.jumpText(node));
|
|
12863
13444
|
return true;
|
|
12864
13445
|
}
|
|
12865
|
-
if (
|
|
13446
|
+
if (ts40.isLabeledStatement(node)) {
|
|
12866
13447
|
this.labelled(node, frame);
|
|
12867
13448
|
return false;
|
|
12868
13449
|
}
|
|
12869
|
-
if (
|
|
13450
|
+
if (ts40.isEmptyStatement(node)) {
|
|
12870
13451
|
return false;
|
|
12871
13452
|
}
|
|
12872
|
-
if (
|
|
13453
|
+
if (ts40.isThrowStatement(node) && frame.async) {
|
|
12873
13454
|
const code = this.expressions.expression(node.expression, frame);
|
|
12874
13455
|
emitter.line(mapped`throw ${this.dialect.code(code, undefined)};`);
|
|
12875
13456
|
return true;
|
|
@@ -12877,22 +13458,22 @@ class StatementSpecializer {
|
|
|
12877
13458
|
throw unsupported2(node);
|
|
12878
13459
|
}
|
|
12879
13460
|
loopOrSwitch(node, labels, frame) {
|
|
12880
|
-
if (
|
|
13461
|
+
if (ts40.isForOfStatement(node)) {
|
|
12881
13462
|
this.forOf(node, labels, frame);
|
|
12882
13463
|
return;
|
|
12883
13464
|
}
|
|
12884
|
-
if (
|
|
13465
|
+
if (ts40.isSwitchStatement(node)) {
|
|
12885
13466
|
this.switch(node, labels, frame);
|
|
12886
13467
|
return;
|
|
12887
13468
|
}
|
|
12888
|
-
if (
|
|
13469
|
+
if (ts40.isForStatement(node)) {
|
|
12889
13470
|
this.forLoop(node, labels, frame);
|
|
12890
13471
|
return;
|
|
12891
13472
|
}
|
|
12892
|
-
if (!
|
|
13473
|
+
if (!ts40.isWhileStatement(node) && !ts40.isDoStatement(node)) {
|
|
12893
13474
|
throw unsupported2(node);
|
|
12894
13475
|
}
|
|
12895
|
-
if (frame.async && occursOutsideFunctions(node.statement,
|
|
13476
|
+
if (frame.async && occursOutsideFunctions(node.statement, ts40.isAwaitExpression)) {
|
|
12896
13477
|
this.suspendingLoop(node, labels, frame);
|
|
12897
13478
|
return;
|
|
12898
13479
|
}
|
|
@@ -12904,15 +13485,15 @@ class StatementSpecializer {
|
|
|
12904
13485
|
if (label !== undefined) {
|
|
12905
13486
|
return item.labels.includes(label);
|
|
12906
13487
|
}
|
|
12907
|
-
return item.kind === "loop" ||
|
|
13488
|
+
return item.kind === "loop" || ts40.isBreakStatement(node) && item.kind === "switch";
|
|
12908
13489
|
});
|
|
12909
|
-
const emitted =
|
|
13490
|
+
const emitted = ts40.isBreakStatement(node) ? scope?.breakLabel : scope?.continueLabel;
|
|
12910
13491
|
return emitted ? `break ${emitted};` : copied(node);
|
|
12911
13492
|
}
|
|
12912
13493
|
labelled(node, frame) {
|
|
12913
13494
|
const labels = [];
|
|
12914
13495
|
const inner = innermostLabelled(node, labels);
|
|
12915
|
-
if (
|
|
13496
|
+
if (ts40.isIterationStatement(inner, false) || ts40.isSwitchStatement(inner)) {
|
|
12916
13497
|
this.recorded(inner, frame, () => {
|
|
12917
13498
|
this.loopOrSwitch(inner, labels, frame);
|
|
12918
13499
|
return false;
|
|
@@ -12968,7 +13549,7 @@ class StatementSpecializer {
|
|
|
12968
13549
|
if (isStoredState(value)) {
|
|
12969
13550
|
frame.returnsStored = true;
|
|
12970
13551
|
}
|
|
12971
|
-
const element = frame.returns && this.checker.getIndexTypeOfType(frame.returns,
|
|
13552
|
+
const element = frame.returns && this.checker.getIndexTypeOfType(frame.returns, ts40.IndexKind.Number);
|
|
12972
13553
|
if (element && value.kind === "virtual" && value.items) {
|
|
12973
13554
|
frame.resultLayout = arrayLayout(this.layouts.text(element), value.items.length);
|
|
12974
13555
|
frame.resultLength = value.items.length;
|
|
@@ -12980,7 +13561,7 @@ class StatementSpecializer {
|
|
|
12980
13561
|
}
|
|
12981
13562
|
resultIec(frame) {
|
|
12982
13563
|
const declaration = frame.emitter.origin;
|
|
12983
|
-
return this.layouts.iecName(
|
|
13564
|
+
return this.layouts.iecName(ts40.findAncestor(declaration, ts40.isFunctionLike));
|
|
12984
13565
|
}
|
|
12985
13566
|
tryStatement(node, frame) {
|
|
12986
13567
|
if (!frame.async && !node.finallyBlock) {
|
|
@@ -12994,7 +13575,7 @@ class StatementSpecializer {
|
|
|
12994
13575
|
if (node.catchClause && frame.async) {
|
|
12995
13576
|
const binding = node.catchClause.variableDeclaration?.name;
|
|
12996
13577
|
frame.emitter.reopen(`} catch${binding ? ` (${binding.getText()})` : ""} {`);
|
|
12997
|
-
if (binding &&
|
|
13578
|
+
if (binding && ts40.isIdentifier(binding)) {
|
|
12998
13579
|
this.bindLocal(binding, binding.text, frame, undefined);
|
|
12999
13580
|
}
|
|
13000
13581
|
this.statements(node.catchClause.block.statements, frame);
|
|
@@ -13006,7 +13587,7 @@ class StatementSpecializer {
|
|
|
13006
13587
|
frame.emitter.close();
|
|
13007
13588
|
}
|
|
13008
13589
|
inner(node, frame) {
|
|
13009
|
-
return
|
|
13590
|
+
return ts40.isBlock(node) ? this.statements(node.statements, frame) : this.statement(node, frame);
|
|
13010
13591
|
}
|
|
13011
13592
|
bindLocal(name, local, frame, iec) {
|
|
13012
13593
|
const symbol = this.checker.getSymbolAtLocation(name);
|
|
@@ -13052,14 +13633,14 @@ class StatementSpecializer {
|
|
|
13052
13633
|
return;
|
|
13053
13634
|
}
|
|
13054
13635
|
const call = node.expression;
|
|
13055
|
-
if (
|
|
13636
|
+
if (ts40.isCallExpression(call) && ts40.isPropertyAccessExpression(call.expression) && call.expression.name.text === "entries" && !call.arguments.length) {
|
|
13056
13637
|
const array = this.expressions.expression(call.expression.expression, frame);
|
|
13057
13638
|
if (array.kind === "code") {
|
|
13058
13639
|
this.forEntries(node, mapped`${array.text}.entries()`, labels, frame);
|
|
13059
13640
|
return;
|
|
13060
13641
|
}
|
|
13061
13642
|
}
|
|
13062
|
-
if (
|
|
13643
|
+
if (ts40.isCallExpression(call) && namesSdk(this.checker, this.context.sdkFile, call.expression, "entries")) {
|
|
13063
13644
|
const [argument] = call.arguments;
|
|
13064
13645
|
const record = argument && this.expressions.expression(argument, frame);
|
|
13065
13646
|
if (record?.kind === "code") {
|
|
@@ -13106,7 +13687,7 @@ class StatementSpecializer {
|
|
|
13106
13687
|
if (iterable.kind !== "code") {
|
|
13107
13688
|
throw new SpecializeError(node.expression, "for…of needs an array.");
|
|
13108
13689
|
}
|
|
13109
|
-
if (!
|
|
13690
|
+
if (!ts40.isIdentifier(name)) {
|
|
13110
13691
|
throw new SpecializeError(name, "Destructure a runtime element inside the loop.");
|
|
13111
13692
|
}
|
|
13112
13693
|
const local = frame.emitter.local(name.text);
|
|
@@ -13145,7 +13726,7 @@ class StatementSpecializer {
|
|
|
13145
13726
|
throw new SpecializeError(at, "A generator runs in place, so it can't yield from itself.");
|
|
13146
13727
|
}
|
|
13147
13728
|
const body = declaration.body;
|
|
13148
|
-
if (!body || !
|
|
13729
|
+
if (!body || !ts40.isBlock(body)) {
|
|
13149
13730
|
throw new SpecializeError(at, "A generator needs a body.");
|
|
13150
13731
|
}
|
|
13151
13732
|
const whole = frame.emitter.local("tsplcGenerator");
|
|
@@ -13170,7 +13751,7 @@ class StatementSpecializer {
|
|
|
13170
13751
|
}
|
|
13171
13752
|
}
|
|
13172
13753
|
};
|
|
13173
|
-
if (!
|
|
13754
|
+
if (!ts40.isGetAccessorDeclaration(declaration)) {
|
|
13174
13755
|
declaration.parameters.forEach((parameter, index) => {
|
|
13175
13756
|
const given = generator.args[index] ?? constant(undefined);
|
|
13176
13757
|
const fallback = parameterDefault(this.checker, given, parameter);
|
|
@@ -13228,7 +13809,7 @@ class StatementSpecializer {
|
|
|
13228
13809
|
}
|
|
13229
13810
|
forEntries(node, iterated, labels, frame) {
|
|
13230
13811
|
const binding = onlyDeclaration(node.initializer)?.name;
|
|
13231
|
-
const names = binding &&
|
|
13812
|
+
const names = binding && ts40.isArrayBindingPattern(binding) ? binding.elements.map((element) => ts40.isBindingElement(element) && ts40.isIdentifier(element.name) ? element.name : undefined) : [];
|
|
13232
13813
|
const [indexName, itemName] = names;
|
|
13233
13814
|
if (names.length !== 2 || !indexName || !itemName) {
|
|
13234
13815
|
throw new SpecializeError(node.initializer, "Destructure [index, item] from array.entries().");
|
|
@@ -13244,13 +13825,13 @@ class StatementSpecializer {
|
|
|
13244
13825
|
}
|
|
13245
13826
|
suspendingLoop(node, labels, frame) {
|
|
13246
13827
|
const test = () => this.dialect.condition(this.expressions.expression(node.expression, frame), node.expression);
|
|
13247
|
-
if (
|
|
13828
|
+
if (ts40.isWhileStatement(node)) {
|
|
13248
13829
|
frame.emitter.open(mapped`${labelText(labels)}while (${test()}) {`);
|
|
13249
13830
|
} else {
|
|
13250
13831
|
frame.emitter.open(`${labelText(labels)}do {`);
|
|
13251
13832
|
}
|
|
13252
13833
|
this.loop(node.statement, labels, undefined, frame);
|
|
13253
|
-
if (
|
|
13834
|
+
if (ts40.isWhileStatement(node)) {
|
|
13254
13835
|
frame.emitter.close();
|
|
13255
13836
|
return;
|
|
13256
13837
|
}
|
|
@@ -13259,14 +13840,14 @@ class StatementSpecializer {
|
|
|
13259
13840
|
forLoop(node, labels, frame) {
|
|
13260
13841
|
const { condition, incrementor, initializer } = node;
|
|
13261
13842
|
const declaration = onlyDeclaration(initializer);
|
|
13262
|
-
if (!declaration || !(declaration.parent.flags &
|
|
13843
|
+
if (!declaration || !(declaration.parent.flags & ts40.NodeFlags.Let)) {
|
|
13263
13844
|
throw new SpecializeError(node, "Declare one let counter in the for header.");
|
|
13264
13845
|
}
|
|
13265
13846
|
const name = declaration.name;
|
|
13266
|
-
if (!
|
|
13847
|
+
if (!ts40.isIdentifier(name) || !declaration.initializer) {
|
|
13267
13848
|
throw new SpecializeError(declaration, "Initialize the loop counter.");
|
|
13268
13849
|
}
|
|
13269
|
-
if (!condition || !
|
|
13850
|
+
if (!condition || !ts40.isBinaryExpression(condition) || !incrementor) {
|
|
13270
13851
|
throw new SpecializeError(node, "Compare the counter to a bound and step it in the for header.");
|
|
13271
13852
|
}
|
|
13272
13853
|
if (assignsName(node.statement, name, this.checker)) {
|
|
@@ -13290,10 +13871,10 @@ class StatementSpecializer {
|
|
|
13290
13871
|
loopStep(incrementor, counter, local, declared, frame) {
|
|
13291
13872
|
const counterSymbol = this.checker.getSymbolAtLocation(counter);
|
|
13292
13873
|
const isCounter = (target) => this.checker.getSymbolAtLocation(unparenthesized(target)) === counterSymbol;
|
|
13293
|
-
if ((
|
|
13294
|
-
return `${local}${
|
|
13874
|
+
if ((ts40.isPrefixUnaryExpression(incrementor) || ts40.isPostfixUnaryExpression(incrementor)) && unitSteps.has(incrementor.operator) && isCounter(incrementor.operand)) {
|
|
13875
|
+
return `${local}${ts40.tokenToString(incrementor.operator)}`;
|
|
13295
13876
|
}
|
|
13296
|
-
if (
|
|
13877
|
+
if (ts40.isBinaryExpression(incrementor) && compoundSteps2.has(incrementor.operatorToken.kind) && isCounter(incrementor.left)) {
|
|
13297
13878
|
return mapped`${local} ${incrementor.operatorToken.getText()} ${this.dialect.code(this.expressions.expression(incrementor.right, frame), declared)}`;
|
|
13298
13879
|
}
|
|
13299
13880
|
throw new SpecializeError(incrementor, "Use i++, i--, i += step or i -= step in the for header.");
|
|
@@ -13311,10 +13892,10 @@ class StatementSpecializer {
|
|
|
13311
13892
|
}
|
|
13312
13893
|
switchClauses(node, declared, frame) {
|
|
13313
13894
|
for (const clause of node.caseBlock.clauses) {
|
|
13314
|
-
if (
|
|
13895
|
+
if (ts40.isDefaultClause(clause)) {
|
|
13315
13896
|
frame.emitter.open("default: {");
|
|
13316
13897
|
if (clause.statements.some(unreachableEnd)) {
|
|
13317
|
-
frame.emitter.line(frame.returns && !(frame.returns.flags &
|
|
13898
|
+
frame.emitter.line(frame.returns && !(frame.returns.flags & ts40.TypeFlags.Void) ? `return ${this.initializers.defaultOf(frame.returns)};` : "break;");
|
|
13318
13899
|
} else {
|
|
13319
13900
|
this.statements(clause.statements, frame);
|
|
13320
13901
|
}
|
|
@@ -13328,7 +13909,7 @@ class StatementSpecializer {
|
|
|
13328
13909
|
}
|
|
13329
13910
|
}
|
|
13330
13911
|
constantSwitch(node, subject, labels, frame) {
|
|
13331
|
-
const nested = node.caseBlock.clauses.some((clause) => clause.statements.some((statement) => !
|
|
13912
|
+
const nested = node.caseBlock.clauses.some((clause) => clause.statements.some((statement) => !ts40.isBreakStatement(statement) && containsJump(statement)));
|
|
13332
13913
|
const label = nested ? frame.emitter.local("tsplcSwitch") : undefined;
|
|
13333
13914
|
if (label) {
|
|
13334
13915
|
frame.emitter.open(`${label}: {`);
|
|
@@ -13341,7 +13922,7 @@ class StatementSpecializer {
|
|
|
13341
13922
|
matchedClauses(node, subject, labels, frame) {
|
|
13342
13923
|
const clauses = node.caseBlock.clauses;
|
|
13343
13924
|
const start = clauses.findIndex((clause) => {
|
|
13344
|
-
if (
|
|
13925
|
+
if (ts40.isDefaultClause(clause)) {
|
|
13345
13926
|
return true;
|
|
13346
13927
|
}
|
|
13347
13928
|
const label = this.expressions.expression(clause.expression, frame);
|
|
@@ -13352,7 +13933,7 @@ class StatementSpecializer {
|
|
|
13352
13933
|
}
|
|
13353
13934
|
for (const clause of clauses.slice(start)) {
|
|
13354
13935
|
for (const statement of clause.statements) {
|
|
13355
|
-
if (
|
|
13936
|
+
if (ts40.isBreakStatement(statement) && (!statement.label || labels.includes(statement.label.text))) {
|
|
13356
13937
|
return;
|
|
13357
13938
|
}
|
|
13358
13939
|
if (this.statement(statement, frame)) {
|
|
@@ -13362,14 +13943,14 @@ class StatementSpecializer {
|
|
|
13362
13943
|
}
|
|
13363
13944
|
}
|
|
13364
13945
|
effect(node, frame) {
|
|
13365
|
-
if (
|
|
13946
|
+
if (ts40.isVoidExpression(node)) {
|
|
13366
13947
|
const value = this.expressions.expression(node.expression, frame);
|
|
13367
13948
|
if (value.kind === "code") {
|
|
13368
13949
|
frame.emitter.line(mapped`void ${value.text};`);
|
|
13369
13950
|
}
|
|
13370
13951
|
return;
|
|
13371
13952
|
}
|
|
13372
|
-
if (
|
|
13953
|
+
if (ts40.isBinaryExpression(node) && isAssignment(node.operatorToken.kind)) {
|
|
13373
13954
|
this.expressions.assignments.assign(node, frame);
|
|
13374
13955
|
return;
|
|
13375
13956
|
}
|
|
@@ -13391,7 +13972,7 @@ var loopDeclaration = (node) => {
|
|
|
13391
13972
|
return declaration;
|
|
13392
13973
|
};
|
|
13393
13974
|
var onlyDeclaration = (initializer) => {
|
|
13394
|
-
if (!initializer || !
|
|
13975
|
+
if (!initializer || !ts40.isVariableDeclarationList(initializer)) {
|
|
13395
13976
|
return;
|
|
13396
13977
|
}
|
|
13397
13978
|
const [declaration, ...others] = initializer.declarations;
|
|
@@ -13405,18 +13986,18 @@ var iterationCount = (start, comparison, limit, incrementor) => {
|
|
|
13405
13986
|
return;
|
|
13406
13987
|
}
|
|
13407
13988
|
const rising = step > 0;
|
|
13408
|
-
const exclusive = rising ?
|
|
13409
|
-
const inclusive = rising ?
|
|
13989
|
+
const exclusive = rising ? ts40.SyntaxKind.LessThanToken : ts40.SyntaxKind.GreaterThanToken;
|
|
13990
|
+
const inclusive = rising ? ts40.SyntaxKind.LessThanEqualsToken : ts40.SyntaxKind.GreaterThanEqualsToken;
|
|
13410
13991
|
if (comparison !== exclusive && comparison !== inclusive) {
|
|
13411
13992
|
return;
|
|
13412
13993
|
}
|
|
13413
13994
|
return Number(loopIterations(BigInt(first), BigInt(last), comparison === exclusive, BigInt(step)));
|
|
13414
13995
|
};
|
|
13415
13996
|
var stepOf = (incrementor) => {
|
|
13416
|
-
if (
|
|
13997
|
+
if (ts40.isPrefixUnaryExpression(incrementor) || ts40.isPostfixUnaryExpression(incrementor)) {
|
|
13417
13998
|
return unitSteps.get(incrementor.operator);
|
|
13418
13999
|
}
|
|
13419
|
-
if (!
|
|
14000
|
+
if (!ts40.isBinaryExpression(incrementor) || !ts40.isNumericLiteral(incrementor.right)) {
|
|
13420
14001
|
return;
|
|
13421
14002
|
}
|
|
13422
14003
|
const size = Number(incrementor.right.text);
|
|
@@ -13427,21 +14008,21 @@ var stepOf = (incrementor) => {
|
|
|
13427
14008
|
return sign === undefined ? undefined : sign * size;
|
|
13428
14009
|
};
|
|
13429
14010
|
var unitSteps = new Map([
|
|
13430
|
-
[
|
|
13431
|
-
[
|
|
14011
|
+
[ts40.SyntaxKind.PlusPlusToken, 1],
|
|
14012
|
+
[ts40.SyntaxKind.MinusMinusToken, -1]
|
|
13432
14013
|
]);
|
|
13433
14014
|
var compoundSteps2 = new Map([
|
|
13434
|
-
[
|
|
13435
|
-
[
|
|
14015
|
+
[ts40.SyntaxKind.PlusEqualsToken, 1],
|
|
14016
|
+
[ts40.SyntaxKind.MinusEqualsToken, -1]
|
|
13436
14017
|
]);
|
|
13437
14018
|
var labelText = (labels) => labels.map((label) => `${label}: `).join("");
|
|
13438
14019
|
var innermostLabelled = (node, labels) => {
|
|
13439
14020
|
labels.push(node.label.text);
|
|
13440
|
-
return
|
|
14021
|
+
return ts40.isLabeledStatement(node.statement) ? innermostLabelled(node.statement, labels) : node.statement;
|
|
13441
14022
|
};
|
|
13442
|
-
var containsJump = (node) => occursOutsideFunctions(node, (inner) =>
|
|
13443
|
-
var unreachableEnd = (statement) =>
|
|
13444
|
-
var unsupported2 = (node) => new SpecializeError(node, `${
|
|
14023
|
+
var containsJump = (node) => occursOutsideFunctions(node, (inner) => ts40.isBreakStatement(inner) || ts40.isContinueStatement(inner));
|
|
14024
|
+
var unreachableEnd = (statement) => ts40.isReturnStatement(statement) && statement.expression !== undefined && ts40.isSatisfiesExpression(statement.expression) && statement.expression.type.kind === ts40.SyntaxKind.NeverKeyword;
|
|
14025
|
+
var unsupported2 = (node) => new SpecializeError(node, `${ts40.SyntaxKind[node.kind]} is not supported in scan code.`);
|
|
13445
14026
|
|
|
13446
14027
|
// packages/compiler/src/frontend/elaborate/specialize.ts
|
|
13447
14028
|
class Specializer {
|
|
@@ -13453,7 +14034,8 @@ class Specializer {
|
|
|
13453
14034
|
dialect;
|
|
13454
14035
|
expressions;
|
|
13455
14036
|
statements;
|
|
13456
|
-
imports
|
|
14037
|
+
imports;
|
|
14038
|
+
names;
|
|
13457
14039
|
finished = [];
|
|
13458
14040
|
asyncOwners = new Map;
|
|
13459
14041
|
cancellers = new Map;
|
|
@@ -13464,13 +14046,16 @@ class Specializer {
|
|
|
13464
14046
|
checker;
|
|
13465
14047
|
constructor(model, generics, sdkFile, sources, {
|
|
13466
14048
|
kept,
|
|
13467
|
-
named
|
|
14049
|
+
named,
|
|
14050
|
+
enumRefusal
|
|
13468
14051
|
}, rootModule) {
|
|
13469
14052
|
this.model = model;
|
|
13470
14053
|
this.generics = generics;
|
|
13471
14054
|
const checker = generics.checker;
|
|
13472
14055
|
this.checker = checker;
|
|
13473
|
-
this.
|
|
14056
|
+
this.names = new Names(routineNames(sources.filter((source) => !kept(source.fileName))));
|
|
14057
|
+
this.imports = new EntryImports(this.names);
|
|
14058
|
+
this.layouts = new Layouts(model, checker, kept, sources.filter(named), (node) => this.storage.storageType(node), this.names, enumRefusal);
|
|
13474
14059
|
this.storage = new Storage(model, checker, this.layouts, this.imports, sources, kept, rootModule);
|
|
13475
14060
|
this.initializers = new Initializers(model, checker, this.layouts, this.storage);
|
|
13476
14061
|
this.dialect = new DialectText(model, checker, this.layouts, this.storage, this.initializers, this.imports);
|
|
@@ -13521,8 +14106,9 @@ class Specializer {
|
|
|
13521
14106
|
if (known) {
|
|
13522
14107
|
return known;
|
|
13523
14108
|
}
|
|
13524
|
-
const name = unusedName(`tsplcCancel_${identityDigest5(this.model.identity(self) ?? String(self.id))}`, (candidate) => this.
|
|
14109
|
+
const name = unusedName(`tsplcCancel_${identityDigest5(this.model.identity(self) ?? String(self.id))}`, (candidate) => this.routineNamed(candidate));
|
|
13525
14110
|
this.results.set(name, {});
|
|
14111
|
+
this.names.declare(name);
|
|
13526
14112
|
this.cancellers.set(self, name);
|
|
13527
14113
|
this.imports.addSdk(sdkModules.async, "cancelAsync");
|
|
13528
14114
|
return name;
|
|
@@ -13552,7 +14138,7 @@ class Specializer {
|
|
|
13552
14138
|
specializeRoutine(declaration, self, args, closure) {
|
|
13553
14139
|
const signature = this.checker.getSignatureFromDeclaration(declaration);
|
|
13554
14140
|
const returnType = signature && this.checker.getReturnTypeOfSignature(signature);
|
|
13555
|
-
const isAsync = Boolean(
|
|
14141
|
+
const isAsync = Boolean(ts41.canHaveModifiers(declaration) && ts41.getModifiers(declaration)?.some((modifier) => modifier.kind === ts41.SyntaxKind.AsyncKeyword));
|
|
13556
14142
|
const frame = {
|
|
13557
14143
|
emitter: new Emitter(declaration),
|
|
13558
14144
|
env: new Map(closure?.env),
|
|
@@ -13566,7 +14152,7 @@ class Specializer {
|
|
|
13566
14152
|
};
|
|
13567
14153
|
const parameters = [];
|
|
13568
14154
|
const keys = [];
|
|
13569
|
-
const parameterList =
|
|
14155
|
+
const parameterList = ts41.isGetAccessorDeclaration(declaration) ? [] : declaration.parameters;
|
|
13570
14156
|
parameterList.forEach((parameter, index) => {
|
|
13571
14157
|
const declared = this.checker.getTypeAtLocation(parameter);
|
|
13572
14158
|
if (parameter.dotDotDotToken) {
|
|
@@ -13677,7 +14263,7 @@ class Specializer {
|
|
|
13677
14263
|
const resultText = this.resultText(declaration, frame);
|
|
13678
14264
|
const head = `${frame.async ? "async " : ""}function ${name}(${kept.map((parameter) => `${parameter.name}: ${parameter.type}`).join(", ")}): ${frame.async ? `Promise<${resultText}>` : resultText} {`;
|
|
13679
14265
|
const instance = self?.owner && tsPath(self.owner);
|
|
13680
|
-
if (self?.captured.kind === "instance" && (
|
|
14266
|
+
if (self?.captured.kind === "instance" && (ts41.isMethodDeclaration(declaration) || ts41.isGetAccessorDeclaration(declaration))) {
|
|
13681
14267
|
this.origins.set(name, {
|
|
13682
14268
|
name: `${self.captured.name}.${declaration.name.getText()}`,
|
|
13683
14269
|
...instance && { instance }
|
|
@@ -13701,11 +14287,11 @@ class Specializer {
|
|
|
13701
14287
|
return this.call(name, declaration, kept.map(({ argument }) => argument), frame.returns);
|
|
13702
14288
|
}
|
|
13703
14289
|
specializeBody(declaration, frame) {
|
|
13704
|
-
if (
|
|
14290
|
+
if (ts41.isArrowFunction(declaration) && !ts41.isBlock(declaration.body)) {
|
|
13705
14291
|
frame.emitter.origin = declaration.body;
|
|
13706
14292
|
const result = this.expressions.expression(declaration.body, frame, frame.returns);
|
|
13707
14293
|
this.expressions.references.returned(result, declaration.body, frame);
|
|
13708
|
-
if (!frame.returns || frame.returns.flags &
|
|
14294
|
+
if (!frame.returns || frame.returns.flags & ts41.TypeFlags.Void) {
|
|
13709
14295
|
if (result.kind === "code") {
|
|
13710
14296
|
frame.emitter.line(mapped`${result.text};`);
|
|
13711
14297
|
}
|
|
@@ -13736,42 +14322,49 @@ class Specializer {
|
|
|
13736
14322
|
};
|
|
13737
14323
|
}
|
|
13738
14324
|
resultText(declaration, frame) {
|
|
13739
|
-
if (!frame.returns || frame.returns.flags &
|
|
14325
|
+
if (!frame.returns || frame.returns.flags & ts41.TypeFlags.Void) {
|
|
13740
14326
|
return "void";
|
|
13741
14327
|
}
|
|
13742
14328
|
return frame.resultLayout?.text ?? this.layouts.iecName(declaration) ?? (frame.referents ? referenceLayout : this.layouts.text(frame.returns));
|
|
13743
14329
|
}
|
|
13744
14330
|
routineName(declaration, self, keys) {
|
|
13745
|
-
const own = declaration.name &&
|
|
13746
|
-
const variable = !own && declaration.parent &&
|
|
14331
|
+
const own = declaration.name && ts41.isIdentifier(declaration.name) ? declaration.name.text : undefined;
|
|
14332
|
+
const variable = !own && declaration.parent && ts41.isVariableDeclaration(declaration.parent) && ts41.isIdentifier(declaration.parent.name) ? declaration.parent.name.text : undefined;
|
|
13747
14333
|
const owner = self?.captured.kind === "instance" ? self.captured.name : undefined;
|
|
13748
14334
|
const named = [owner, own ?? variable ?? "callback"].filter(Boolean).join("_");
|
|
13749
14335
|
const instance = self && this.model.identity(self);
|
|
13750
14336
|
const specialized = Boolean(instance) || keys.length > 0;
|
|
13751
14337
|
const stable = keys.map((entry) => specializationKey(entry, { stable: true }));
|
|
13752
14338
|
const base = specialized ? `${named}_${identityDigest5([instance ?? "", ...stable].join("\x00"))}` : named;
|
|
13753
|
-
const name = unusedName(base, (candidate) => this.
|
|
14339
|
+
const name = unusedName(base, (candidate) => this.routineNamed(candidate));
|
|
13754
14340
|
this.results.set(name, {});
|
|
14341
|
+
this.names.declare(name);
|
|
13755
14342
|
return name;
|
|
13756
14343
|
}
|
|
14344
|
+
named(name) {
|
|
14345
|
+
return this.routineNamed(name);
|
|
14346
|
+
}
|
|
14347
|
+
routineNamed(name) {
|
|
14348
|
+
return this.results.has(name) || this.names.importedAs(name);
|
|
14349
|
+
}
|
|
13757
14350
|
bindParameter(name, arg, declared, frame, parameters, keys) {
|
|
13758
|
-
if (
|
|
14351
|
+
if (ts41.isObjectBindingPattern(name)) {
|
|
13759
14352
|
for (const { element, field } of namedFields(name, "parameter")) {
|
|
13760
14353
|
this.bindElement(element, this.expressions.member(arg, field, element, frame), frame, parameters, keys);
|
|
13761
14354
|
}
|
|
13762
14355
|
return;
|
|
13763
14356
|
}
|
|
13764
|
-
if (
|
|
14357
|
+
if (ts41.isArrayBindingPattern(name)) {
|
|
13765
14358
|
for (const { element, index } of namedElements(name, "parameter")) {
|
|
13766
14359
|
this.bindElement(element, this.expressions.element(arg, constant(index), element, frame), frame, parameters, keys);
|
|
13767
14360
|
}
|
|
13768
14361
|
return;
|
|
13769
14362
|
}
|
|
13770
14363
|
const symbol = symbolOf(this.checker, name);
|
|
13771
|
-
if (arg.kind === "code" || arg.kind === "virtual" && !this.isKnownShape(arg) || arg.kind === "const" && !(declared.flags &
|
|
14364
|
+
if (arg.kind === "code" || arg.kind === "virtual" && !this.isKnownShape(arg) || arg.kind === "const" && !(declared.flags & ts41.TypeFlags.Undefined) && arg.value !== undefined) {
|
|
13772
14365
|
const local = frame.emitter.local(name.text);
|
|
13773
14366
|
const iec = this.layouts.iecName(name.parent) ?? (arg.kind === "code" ? arg.iec : undefined);
|
|
13774
|
-
const element = this.checker.getIndexTypeOfType(declared,
|
|
14367
|
+
const element = this.checker.getIndexTypeOfType(declared, ts41.IndexKind.Number);
|
|
13775
14368
|
const literal = arg.kind === "virtual" && arg.items && element ? arrayLayout(this.layouts.text(element), arg.items.length) : undefined;
|
|
13776
14369
|
const layout = arg.kind === "code" ? arg.layout : literal;
|
|
13777
14370
|
const type = iec ?? layout?.text ?? this.layouts.text(declared);
|
|
@@ -13780,7 +14373,7 @@ class Specializer {
|
|
|
13780
14373
|
type,
|
|
13781
14374
|
argument: this.dialect.assignmentText(arg, declared, iec)
|
|
13782
14375
|
});
|
|
13783
|
-
const routine =
|
|
14376
|
+
const routine = ts41.findAncestor(name, ts41.isFunctionLike);
|
|
13784
14377
|
const assigned = routine !== undefined && "body" in routine && routine.body !== undefined && assignsSymbol(routine.body, new Set([symbol]), this.checker);
|
|
13785
14378
|
const held = assigned ? frame.emitter.local(name.text) : local;
|
|
13786
14379
|
if (assigned) {
|
|
@@ -13825,7 +14418,7 @@ class Specializer {
|
|
|
13825
14418
|
}
|
|
13826
14419
|
passedPart(element, part, frame, parameters, keys) {
|
|
13827
14420
|
const name = element.name.getText();
|
|
13828
|
-
const base =
|
|
14421
|
+
const base = ts41.isIdentifier(element.name) ? element.name.text : "part";
|
|
13829
14422
|
if (part.kind === "choice") {
|
|
13830
14423
|
const lifted = this.lifted(part, base, frame, everyValue(frame, parameters));
|
|
13831
14424
|
keys.push({ name, value: lifted }, { name, value: "default?" });
|
|
@@ -13951,10 +14544,10 @@ class Specializer {
|
|
|
13951
14544
|
this.bindParameter(parameter.name, value, declared, frame, parameters, keys);
|
|
13952
14545
|
}
|
|
13953
14546
|
bindRest(parameter, args, declared, frame, parameters, keys) {
|
|
13954
|
-
if (!
|
|
14547
|
+
if (!ts41.isIdentifier(parameter.name)) {
|
|
13955
14548
|
throw new SpecializeError(parameter.name, "Name the rest parameter; destructure it in the body.");
|
|
13956
14549
|
}
|
|
13957
|
-
const element = this.checker.getIndexTypeOfType(declared,
|
|
14550
|
+
const element = this.checker.getIndexTypeOfType(declared, ts41.IndexKind.Number) ?? this.checker.getAnyType();
|
|
13958
14551
|
const name = parameter.name.text;
|
|
13959
14552
|
const items = args.map((arg, index) => {
|
|
13960
14553
|
if (arg.kind !== "code" && arg.kind !== "const") {
|
|
@@ -13984,7 +14577,7 @@ class Specializer {
|
|
|
13984
14577
|
return holdsSetupObjects(value) || inner.some((item) => this.isKnownShape(item)) || value.items !== undefined && inner.every((item) => item.kind === "const");
|
|
13985
14578
|
}
|
|
13986
14579
|
}
|
|
13987
|
-
var closingBrace = (declaration) => declaration.body &&
|
|
14580
|
+
var closingBrace = (declaration) => declaration.body && ts41.isBlock(declaration.body) && declaration.body.getLastToken() || declaration;
|
|
13988
14581
|
var everyValue = (frame, parameters) => ({
|
|
13989
14582
|
parameter: (text, base, type) => {
|
|
13990
14583
|
const local = frame.emitter.local(base);
|
|
@@ -14038,13 +14631,13 @@ var reread = (sample, array, index) => {
|
|
|
14038
14631
|
var readSymbols = (declaration, checker) => {
|
|
14039
14632
|
const read = new Set;
|
|
14040
14633
|
const visit = (node) => {
|
|
14041
|
-
if (
|
|
14634
|
+
if (ts41.isIdentifier(node)) {
|
|
14042
14635
|
const symbol = valueSymbol(checker, node);
|
|
14043
14636
|
if (symbol) {
|
|
14044
14637
|
read.add(symbol);
|
|
14045
14638
|
}
|
|
14046
14639
|
}
|
|
14047
|
-
|
|
14640
|
+
ts41.forEachChild(node, visit);
|
|
14048
14641
|
};
|
|
14049
14642
|
visit(declaration);
|
|
14050
14643
|
return read;
|
|
@@ -14057,26 +14650,26 @@ var keptModule = (source, checker, sdk, {
|
|
|
14057
14650
|
}) => {
|
|
14058
14651
|
const work = taskWork(source, checker, sdk);
|
|
14059
14652
|
return source.statements.every((statement) => {
|
|
14060
|
-
if (
|
|
14653
|
+
if (ts42.isFunctionDeclaration(statement) && statement.name && work.has(statement.name.text)) {
|
|
14061
14654
|
return true;
|
|
14062
14655
|
}
|
|
14063
|
-
if (
|
|
14656
|
+
if (ts42.isImportDeclaration(statement) || ts42.isExportDeclaration(statement) || ts42.isInterfaceDeclaration(statement) || ts42.isTypeAliasDeclaration(statement) || ts42.isEnumDeclaration(statement)) {
|
|
14064
14657
|
return true;
|
|
14065
14658
|
}
|
|
14066
|
-
if (!
|
|
14659
|
+
if (!ts42.isVariableStatement(statement)) {
|
|
14067
14660
|
return false;
|
|
14068
14661
|
}
|
|
14069
14662
|
return statement.declarationList.declarations.every((declaration) => {
|
|
14070
14663
|
const symbol = checker.getSymbolAtLocation(declaration.name);
|
|
14071
|
-
return declaration.initializer && constantInitializer(declaration.initializer, checker, sdk) && !(symbol && written.has(symbol)) && !(
|
|
14664
|
+
return declaration.initializer && constantInitializer(declaration.initializer, checker, sdk) && !(symbol && written.has(symbol)) && !(ts42.isIdentifier(declaration.name) && bound.has(declaration.name.text));
|
|
14072
14665
|
});
|
|
14073
14666
|
});
|
|
14074
14667
|
};
|
|
14075
14668
|
var placeRoot = (node) => {
|
|
14076
|
-
if (
|
|
14669
|
+
if (ts42.isIdentifier(node)) {
|
|
14077
14670
|
return node;
|
|
14078
14671
|
}
|
|
14079
|
-
if (
|
|
14672
|
+
if (ts42.isPropertyAccessExpression(node) || ts42.isElementAccessExpression(node) || ts42.isParenthesizedExpression(node) || ts42.isNonNullExpression(node)) {
|
|
14080
14673
|
return placeRoot(node.expression);
|
|
14081
14674
|
}
|
|
14082
14675
|
return;
|
|
@@ -14091,13 +14684,13 @@ var writtenBindings = (sources, checker) => {
|
|
|
14091
14684
|
}
|
|
14092
14685
|
};
|
|
14093
14686
|
const visit = (node) => {
|
|
14094
|
-
if (
|
|
14687
|
+
if (ts42.isBinaryExpression(node) && node.operatorToken.kind >= ts42.SyntaxKind.FirstAssignment && node.operatorToken.kind <= ts42.SyntaxKind.LastAssignment) {
|
|
14095
14688
|
note(node.left);
|
|
14096
14689
|
}
|
|
14097
|
-
if ((
|
|
14690
|
+
if ((ts42.isPrefixUnaryExpression(node) || ts42.isPostfixUnaryExpression(node)) && (node.operator === ts42.SyntaxKind.PlusPlusToken || node.operator === ts42.SyntaxKind.MinusMinusToken)) {
|
|
14098
14691
|
note(node.operand);
|
|
14099
14692
|
}
|
|
14100
|
-
|
|
14693
|
+
ts42.forEachChild(node, visit);
|
|
14101
14694
|
};
|
|
14102
14695
|
for (const source of sources) {
|
|
14103
14696
|
visit(source);
|
|
@@ -14105,7 +14698,7 @@ var writtenBindings = (sources, checker) => {
|
|
|
14105
14698
|
return written;
|
|
14106
14699
|
};
|
|
14107
14700
|
var taskWork = (source, checker, sdk) => {
|
|
14108
|
-
const functions = new Map(source.statements.flatMap((statement) =>
|
|
14701
|
+
const functions = new Map(source.statements.flatMap((statement) => ts42.isFunctionDeclaration(statement) && statement.name ? [[statement.name.text, statement]] : []));
|
|
14109
14702
|
const work = new Set;
|
|
14110
14703
|
const add = (name) => {
|
|
14111
14704
|
const declaration = functions.get(name);
|
|
@@ -14114,59 +14707,31 @@ var taskWork = (source, checker, sdk) => {
|
|
|
14114
14707
|
}
|
|
14115
14708
|
work.add(name);
|
|
14116
14709
|
const visit = (node) => {
|
|
14117
|
-
if (
|
|
14118
|
-
const called = checker.getSymbolAtLocation(node.expression)?.declarations?.find(
|
|
14710
|
+
if (ts42.isCallExpression(node) && ts42.isIdentifier(node.expression)) {
|
|
14711
|
+
const called = checker.getSymbolAtLocation(node.expression)?.declarations?.find(ts42.isFunctionDeclaration);
|
|
14119
14712
|
if (called?.name && called.parent === source) {
|
|
14120
14713
|
add(called.name.text);
|
|
14121
14714
|
}
|
|
14122
14715
|
}
|
|
14123
|
-
|
|
14716
|
+
ts42.forEachChild(node, visit);
|
|
14124
14717
|
};
|
|
14125
|
-
|
|
14718
|
+
ts42.forEachChild(declaration, visit);
|
|
14126
14719
|
};
|
|
14127
14720
|
for (const statement of source.statements) {
|
|
14128
|
-
if (!
|
|
14721
|
+
if (!ts42.isVariableStatement(statement)) {
|
|
14129
14722
|
continue;
|
|
14130
14723
|
}
|
|
14131
14724
|
for (const declaration of statement.declarationList.declarations) {
|
|
14132
14725
|
const call = declaration.initializer;
|
|
14133
|
-
const argument = call &&
|
|
14134
|
-
if (argument &&
|
|
14726
|
+
const argument = call && ts42.isCallExpression(call) && definesTask(call, checker, sdk) ? call.arguments[0] : undefined;
|
|
14727
|
+
if (argument && ts42.isIdentifier(argument)) {
|
|
14135
14728
|
add(argument.text);
|
|
14136
14729
|
}
|
|
14137
14730
|
}
|
|
14138
14731
|
}
|
|
14139
14732
|
return work;
|
|
14140
14733
|
};
|
|
14141
|
-
var
|
|
14142
|
-
if (ts40.isNumericLiteral(node) || ts40.isStringLiteral(node) || ts40.isNoSubstitutionTemplateLiteral(node) || ts40.isBigIntLiteral(node) || node.kind === ts40.SyntaxKind.TrueKeyword || node.kind === ts40.SyntaxKind.FalseKeyword) {
|
|
14143
|
-
return true;
|
|
14144
|
-
}
|
|
14145
|
-
if (ts40.isPrefixUnaryExpression(node)) {
|
|
14146
|
-
return constantInitializer(node.operand, checker, sdk);
|
|
14147
|
-
}
|
|
14148
|
-
if (ts40.isAsExpression(node) || ts40.isSatisfiesExpression(node) || ts40.isParenthesizedExpression(node)) {
|
|
14149
|
-
return constantInitializer(node.expression, checker, sdk);
|
|
14150
|
-
}
|
|
14151
|
-
if (ts40.isPropertyAccessExpression(node)) {
|
|
14152
|
-
return ts40.isIdentifier(node.expression);
|
|
14153
|
-
}
|
|
14154
|
-
if (ts40.isIdentifier(node)) {
|
|
14155
|
-
return true;
|
|
14156
|
-
}
|
|
14157
|
-
if (ts40.isObjectLiteralExpression(node)) {
|
|
14158
|
-
return node.properties.every((property) => ts40.isPropertyAssignment(property) && constantInitializer(property.initializer, checker, sdk) || ts40.isShorthandPropertyAssignment(property));
|
|
14159
|
-
}
|
|
14160
|
-
if (ts40.isArrayLiteralExpression(node)) {
|
|
14161
|
-
return node.elements.every((element) => constantInitializer(element, checker, sdk));
|
|
14162
|
-
}
|
|
14163
|
-
if (ts40.isCallExpression(node)) {
|
|
14164
|
-
return namesSdk(checker, sdk, node.expression, "createServiceClient") || definesTask(node, checker, sdk);
|
|
14165
|
-
}
|
|
14166
|
-
return false;
|
|
14167
|
-
};
|
|
14168
|
-
var definesTask = (call, checker, sdk) => ts40.isPropertyAccessExpression(call.expression) && call.expression.name.text === "define" && (namesSdk(checker, sdk, call.expression.expression, "tasks") || namesSdk(checker, sdk, call.expression.expression, "backgroundTasks"));
|
|
14169
|
-
var needsElaboration = (sources, kept, checker, sdk) => sources.some((source) => !kept(source.fileName) && source.statements.some((statement) => ts40.isClassDeclaration(statement) && !statement.heritageClauses?.some((clause) => clause.types.some((type) => namesSdk(checker, sdk, type.expression, "FunctionBlock")))));
|
|
14734
|
+
var needsElaboration = (sources, kept, checker, sdk) => sources.some((source) => !kept(source.fileName) && source.statements.some((statement) => ts42.isClassDeclaration(statement) && !statement.heritageClauses?.some((clause) => clause.types.some((type) => namesSdk(checker, sdk, type.expression, "FunctionBlock")))));
|
|
14170
14735
|
var foldedConstant = (value, iec) => {
|
|
14171
14736
|
switch (value.kind) {
|
|
14172
14737
|
case "number":
|
|
@@ -14201,6 +14766,7 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14201
14766
|
const origins = new Map;
|
|
14202
14767
|
const folded = [];
|
|
14203
14768
|
const literals = new Map;
|
|
14769
|
+
const reduced = new Map;
|
|
14204
14770
|
const references = [];
|
|
14205
14771
|
const referenceFields = [];
|
|
14206
14772
|
const result = {
|
|
@@ -14209,6 +14775,7 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14209
14775
|
origins,
|
|
14210
14776
|
folded,
|
|
14211
14777
|
literals,
|
|
14778
|
+
reduced,
|
|
14212
14779
|
references: { objects: references, fields: referenceFields }
|
|
14213
14780
|
};
|
|
14214
14781
|
const runtime = plcSources(program).filter((source) => !inSdk(sdk, source.fileName));
|
|
@@ -14242,16 +14809,29 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14242
14809
|
if (refused.length) {
|
|
14243
14810
|
throw new AggregateError(refused);
|
|
14244
14811
|
}
|
|
14245
|
-
const
|
|
14812
|
+
const retained = retentionDeclarations(runtime, checker, sdk);
|
|
14246
14813
|
const graph = captureGraph(entryFile, runtime, checker, configuration);
|
|
14247
14814
|
const generics = new GenericTypes(checker);
|
|
14248
14815
|
const model = new Model(graph, generics.checker, program, entryFile, root, {
|
|
14249
14816
|
bound,
|
|
14250
14817
|
inputs
|
|
14251
14818
|
});
|
|
14252
|
-
const
|
|
14819
|
+
const generated = generatedModules(model.roots, { directory, root, bound });
|
|
14820
|
+
const rootModule = generated.root;
|
|
14253
14821
|
const named = (source) => isKept(source.fileName) || serviceClients(source, checker, sdk).length > 0;
|
|
14254
|
-
const
|
|
14822
|
+
const reduction = new Reduction(checker, sdk, {
|
|
14823
|
+
runtime,
|
|
14824
|
+
kept: isKept,
|
|
14825
|
+
entry: entrySource,
|
|
14826
|
+
written,
|
|
14827
|
+
bound
|
|
14828
|
+
});
|
|
14829
|
+
const declarations = reduction.entry();
|
|
14830
|
+
const specializer = new Specializer(model, generics, sdk, runtime, {
|
|
14831
|
+
kept: isKept,
|
|
14832
|
+
named,
|
|
14833
|
+
enumRefusal: (declaration) => reduction.sharesEnum(declaration) ? undefined : reduction.enumRefusal(declaration)
|
|
14834
|
+
}, rootModule);
|
|
14255
14835
|
const scans = scanFunctions.map((name) => {
|
|
14256
14836
|
const declaration = findEntry(entrySource, name);
|
|
14257
14837
|
return {
|
|
@@ -14277,17 +14857,23 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14277
14857
|
for (const name of specializer.initializers.takeSdkCalls()) {
|
|
14278
14858
|
specializer.imports.addSdk(sdkModules.types, name);
|
|
14279
14859
|
}
|
|
14280
|
-
const retention = new Retention(model, specializer.storage,
|
|
14281
|
-
const
|
|
14860
|
+
const retention = new Retention(model, specializer.storage, retained);
|
|
14861
|
+
const stored = [...specializer.storage.roots].map((item) => ({
|
|
14282
14862
|
item,
|
|
14283
|
-
storageGlobals(specializer, retention, item)
|
|
14284
|
-
|
|
14863
|
+
...storageGlobals(specializer, retention, item)
|
|
14864
|
+
}));
|
|
14285
14865
|
const unretained = retention.unclaimed();
|
|
14286
14866
|
if (unretained) {
|
|
14287
14867
|
throw unretained;
|
|
14288
14868
|
}
|
|
14289
14869
|
const types = specializer.layouts.snapshot();
|
|
14290
|
-
const
|
|
14870
|
+
const misnamed = stored.flatMap(({ item, fields }) => fields.flatMap(({ name, declaration }) => types.declarations.has(name) ? [
|
|
14871
|
+
new SpecializeError(declaration ?? item.declaration, `${item.name}.${name}: each field of ${item.name} becomes a PLC variable of the field's name, and the generated code already names a type ${name}. Rename the field.`)
|
|
14872
|
+
] : []));
|
|
14873
|
+
if (misnamed.length) {
|
|
14874
|
+
throw new AggregateError(misnamed);
|
|
14875
|
+
}
|
|
14876
|
+
const typesFile = generated.types;
|
|
14291
14877
|
const typeImports = (file) => [
|
|
14292
14878
|
...specializer.layouts.imports.lines(file, "import"),
|
|
14293
14879
|
...specializer.layouts.typeImports.lines(file, "import type")
|
|
@@ -14295,7 +14881,7 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14295
14881
|
const importTypes = (file) => typesModuleImports(types, importPath(file, typesFile));
|
|
14296
14882
|
sources.set(typesFile, joined([...typeImports(typesFile), ...types.declarations.values()], `
|
|
14297
14883
|
`));
|
|
14298
|
-
for (const
|
|
14884
|
+
for (const { item, lines } of stored) {
|
|
14299
14885
|
const file = rootModule(item);
|
|
14300
14886
|
const imports = [...typeImports(file), ...importTypes(file)];
|
|
14301
14887
|
sources.set(file, joined([...imports.map((line) => from(item.declaration, line)), ...lines], `
|
|
@@ -14306,15 +14892,27 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14306
14892
|
...importTypes(entryFile),
|
|
14307
14893
|
...specializer.imports.lines(entryFile)
|
|
14308
14894
|
];
|
|
14895
|
+
const storesEnum = (name) => specializer.layouts.imports.has(entryFile, name);
|
|
14896
|
+
const free = ({ name }) => !storesEnum(name) && !specializer.named(name) && !types.declarations.has(name);
|
|
14897
|
+
const declared = [
|
|
14898
|
+
...declarations.enums.filter(free),
|
|
14899
|
+
...declarations.constants.filter(free)
|
|
14900
|
+
];
|
|
14901
|
+
reduction.declaredByEntry(new Set([
|
|
14902
|
+
...declarations.enums.filter(({ name }) => storesEnum(name)),
|
|
14903
|
+
...declared
|
|
14904
|
+
].map(({ name }) => name)));
|
|
14309
14905
|
sources.set(entryFile, joined([
|
|
14310
14906
|
...header.map((line) => from(main, line)),
|
|
14907
|
+
...declarations.enums.filter(({ name }) => storesEnum(name)).map(({ line }) => line),
|
|
14311
14908
|
...specializer.expressions.tableDeclarations,
|
|
14312
14909
|
...specializer.routines,
|
|
14313
14910
|
...scans.flatMap(({ name, declaration, body }) => [
|
|
14314
14911
|
from(declaration, `export function ${name}(): void {`),
|
|
14315
14912
|
...body,
|
|
14316
14913
|
from(closingBrace(declaration), "}")
|
|
14317
|
-
])
|
|
14914
|
+
]),
|
|
14915
|
+
...declared.map(({ line }) => line)
|
|
14318
14916
|
], `
|
|
14319
14917
|
`));
|
|
14320
14918
|
for (const [name, origin] of specializer.origins) {
|
|
@@ -14336,11 +14934,13 @@ var lowerElaboration = (program, sdk, entries, root, positions, {
|
|
|
14336
14934
|
});
|
|
14337
14935
|
}
|
|
14338
14936
|
}
|
|
14339
|
-
for (const source of
|
|
14340
|
-
|
|
14341
|
-
|
|
14342
|
-
}
|
|
14937
|
+
for (const source of reduction.modules) {
|
|
14938
|
+
sources.set(source.fileName, reduction.module(source));
|
|
14939
|
+
reduced.set(source.fileName, { compiled: reduction.compiled(source) });
|
|
14343
14940
|
}
|
|
14941
|
+
reduced.set(entryFile, {
|
|
14942
|
+
declared: new Set([...declarations.enums, ...declarations.constants].map(({ name }) => name))
|
|
14943
|
+
});
|
|
14344
14944
|
} catch (cause) {
|
|
14345
14945
|
const causes = cause instanceof AggregateError ? cause.errors : [cause];
|
|
14346
14946
|
diagnostics.push(...causes.map((reported) => failure(reported, positions, debug)));
|
|
@@ -14395,7 +14995,7 @@ var storageGlobals = (specializer, retention, item) => {
|
|
|
14395
14995
|
initial: initializers.valueText(item.value, declared, layout),
|
|
14396
14996
|
declaration: item.declaration
|
|
14397
14997
|
});
|
|
14398
|
-
return [...sdkImports(), ...lines];
|
|
14998
|
+
return { lines: [...sdkImports(), ...lines], fields: [] };
|
|
14399
14999
|
}
|
|
14400
15000
|
const fieldInitializer = (name, layout) => {
|
|
14401
15001
|
if (node.captured.kind === "instance") {
|
|
@@ -14407,27 +15007,72 @@ var storageGlobals = (specializer, retention, item) => {
|
|
|
14407
15007
|
}
|
|
14408
15008
|
return initializers.valueText(model.field(node, name), declared, layout);
|
|
14409
15009
|
};
|
|
14410
|
-
|
|
15010
|
+
const fields = storage.rootFields(item);
|
|
15011
|
+
for (const field of fields) {
|
|
15012
|
+
const origin = field.declaration ?? item.declaration;
|
|
15013
|
+
if (!plainName(field.name)) {
|
|
15014
|
+
throw quotedField(origin, field.name);
|
|
15015
|
+
}
|
|
15016
|
+
if (reservedWords.has(field.name)) {
|
|
15017
|
+
throw new SpecializeError(origin, `${item.name}.${field.name}: each field of ${item.name} becomes a PLC variable of the field's name, and "${field.name}" is a word TypeScript reserves, so no variable can have it. Rename the field.`);
|
|
15018
|
+
}
|
|
14411
15019
|
global({
|
|
14412
15020
|
...field,
|
|
14413
15021
|
initial: fieldInitializer(field.name, field.layout)
|
|
14414
15022
|
});
|
|
14415
15023
|
}
|
|
14416
|
-
return [...sdkImports(), ...lines];
|
|
14417
|
-
};
|
|
14418
|
-
var
|
|
14419
|
-
|
|
14420
|
-
|
|
14421
|
-
|
|
14422
|
-
|
|
14423
|
-
|
|
14424
|
-
|
|
14425
|
-
|
|
14426
|
-
|
|
14427
|
-
|
|
14428
|
-
|
|
14429
|
-
|
|
14430
|
-
|
|
15024
|
+
return { lines: [...sdkImports(), ...lines], fields };
|
|
15025
|
+
};
|
|
15026
|
+
var reservedWords = new Set([
|
|
15027
|
+
"arguments",
|
|
15028
|
+
"await",
|
|
15029
|
+
"break",
|
|
15030
|
+
"case",
|
|
15031
|
+
"catch",
|
|
15032
|
+
"class",
|
|
15033
|
+
"const",
|
|
15034
|
+
"continue",
|
|
15035
|
+
"debugger",
|
|
15036
|
+
"default",
|
|
15037
|
+
"delete",
|
|
15038
|
+
"do",
|
|
15039
|
+
"else",
|
|
15040
|
+
"enum",
|
|
15041
|
+
"eval",
|
|
15042
|
+
"export",
|
|
15043
|
+
"extends",
|
|
15044
|
+
"false",
|
|
15045
|
+
"finally",
|
|
15046
|
+
"for",
|
|
15047
|
+
"function",
|
|
15048
|
+
"if",
|
|
15049
|
+
"implements",
|
|
15050
|
+
"import",
|
|
15051
|
+
"in",
|
|
15052
|
+
"instanceof",
|
|
15053
|
+
"interface",
|
|
15054
|
+
"let",
|
|
15055
|
+
"new",
|
|
15056
|
+
"null",
|
|
15057
|
+
"package",
|
|
15058
|
+
"private",
|
|
15059
|
+
"protected",
|
|
15060
|
+
"public",
|
|
15061
|
+
"return",
|
|
15062
|
+
"static",
|
|
15063
|
+
"super",
|
|
15064
|
+
"switch",
|
|
15065
|
+
"this",
|
|
15066
|
+
"throw",
|
|
15067
|
+
"true",
|
|
15068
|
+
"try",
|
|
15069
|
+
"typeof",
|
|
15070
|
+
"var",
|
|
15071
|
+
"void",
|
|
15072
|
+
"while",
|
|
15073
|
+
"with",
|
|
15074
|
+
"yield"
|
|
15075
|
+
]);
|
|
14431
15076
|
var failure = (cause, positions, debug) => {
|
|
14432
15077
|
if (cause instanceof SpecializeError) {
|
|
14433
15078
|
return error7("PLC1010", cause.message, positions.node(cause.node));
|
|
@@ -14439,12 +15084,12 @@ var failure = (cause, positions, debug) => {
|
|
|
14439
15084
|
};
|
|
14440
15085
|
var findEntry = (source, name) => {
|
|
14441
15086
|
for (const statement of source.statements) {
|
|
14442
|
-
if (
|
|
15087
|
+
if (ts42.isFunctionDeclaration(statement) && statement.name?.text === name && statement.body) {
|
|
14443
15088
|
return statement;
|
|
14444
15089
|
}
|
|
14445
|
-
if (
|
|
15090
|
+
if (ts42.isVariableStatement(statement)) {
|
|
14446
15091
|
for (const declaration of statement.declarationList.declarations) {
|
|
14447
|
-
if (
|
|
15092
|
+
if (ts42.isIdentifier(declaration.name) && declaration.name.text === name && declaration.initializer && ts42.isArrowFunction(declaration.initializer)) {
|
|
14448
15093
|
return declaration.initializer;
|
|
14449
15094
|
}
|
|
14450
15095
|
}
|
|
@@ -14452,16 +15097,11 @@ var findEntry = (source, name) => {
|
|
|
14452
15097
|
}
|
|
14453
15098
|
throw new Error(`${source.fileName} exports no function ${name}.`);
|
|
14454
15099
|
};
|
|
14455
|
-
var
|
|
15100
|
+
var generatedModules = (roots, {
|
|
14456
15101
|
directory,
|
|
14457
15102
|
root: projectRoot,
|
|
14458
15103
|
bound
|
|
14459
15104
|
}) => {
|
|
14460
|
-
const counts = new Map;
|
|
14461
|
-
for (const item of roots) {
|
|
14462
|
-
const key = item.name.toLowerCase();
|
|
14463
|
-
counts.set(key, (counts.get(key) ?? 0) + 1);
|
|
14464
|
-
}
|
|
14465
15105
|
const shareName = (item) => roots.filter((other) => other.name.toLowerCase() === item.name.toLowerCase());
|
|
14466
15106
|
const ambiguous = roots.filter((item) => bound.has(item.name) && shareName(item).length > 1);
|
|
14467
15107
|
if (ambiguous.length) {
|
|
@@ -14470,16 +15110,25 @@ var rootModules = (roots, {
|
|
|
14470
15110
|
return new SpecializeError(item.declaration, `The config's io names this root ${item.name}, which roots in ${modules.join(" and ")} share: rename one of them.`);
|
|
14471
15111
|
}));
|
|
14472
15112
|
}
|
|
14473
|
-
|
|
14474
|
-
const
|
|
14475
|
-
const
|
|
14476
|
-
|
|
15113
|
+
const qualified = (item) => {
|
|
15114
|
+
const module = modulePath(projectRoot, item.file);
|
|
15115
|
+
const sharing = shareName(item);
|
|
15116
|
+
if (sharing.length === 1) {
|
|
15117
|
+
return item.name;
|
|
15118
|
+
}
|
|
15119
|
+
const twins = sharing.filter((other) => other.file === item.file);
|
|
15120
|
+
return twins.at(-1) === item ? `${item.name}_${identityDigest6(module)}` : `${item.name}_${identityDigest6(`${module}\x00${item.name}`)}`;
|
|
15121
|
+
};
|
|
15122
|
+
const taken = new Set(roots.map((item) => qualified(item).toLowerCase()));
|
|
15123
|
+
return {
|
|
15124
|
+
root: (item) => `${directory}/${qualified(item)}.ts`,
|
|
15125
|
+
types: `${directory}/${unusedName("types", (name) => taken.has(name))}.ts`
|
|
14477
15126
|
};
|
|
14478
15127
|
};
|
|
14479
15128
|
|
|
14480
15129
|
// packages/compiler/src/frontend/promises.ts
|
|
14481
15130
|
import { error as error8 } from "@tsplc/compiler/ir";
|
|
14482
|
-
import
|
|
15131
|
+
import ts43 from "typescript";
|
|
14483
15132
|
var lowerPromises = (program, sdk, positions, root) => {
|
|
14484
15133
|
const files = plcSources(program);
|
|
14485
15134
|
const rewrite = new PromiseRewrite(program.getTypeChecker(), sdk, positions, files, new GeneratedNames(root));
|
|
@@ -14527,30 +15176,30 @@ function rewriteChains(rewrite, files, diagnostics) {
|
|
|
14527
15176
|
};
|
|
14528
15177
|
for (const source of files) {
|
|
14529
15178
|
const visit = (node, owner) => {
|
|
14530
|
-
if (
|
|
15179
|
+
if (ts43.isFunctionDeclaration(node) || ts43.isMethodDeclaration(node)) {
|
|
14531
15180
|
if (node.body) {
|
|
14532
|
-
|
|
15181
|
+
ts43.forEachChild(node.body, (child) => visit(child, node));
|
|
14533
15182
|
}
|
|
14534
15183
|
return;
|
|
14535
15184
|
}
|
|
14536
15185
|
if (!owner) {
|
|
14537
|
-
|
|
15186
|
+
ts43.forEachChild(node, (child) => visit(child));
|
|
14538
15187
|
return;
|
|
14539
15188
|
}
|
|
14540
|
-
if (
|
|
15189
|
+
if (ts43.isCallExpression(node) && rewrite.namesSdk(node.expression, "asyncDiagnostics")) {
|
|
14541
15190
|
rewrite.useDiagnostics(node);
|
|
14542
15191
|
diagnosticsCalls.push(node);
|
|
14543
15192
|
return;
|
|
14544
15193
|
}
|
|
14545
|
-
if (
|
|
15194
|
+
if (ts43.isCallExpression(node) && rewrite.namesSdk(node.expression, "cancelAsync")) {
|
|
14546
15195
|
cancelCalls.push({ node, owner });
|
|
14547
15196
|
return;
|
|
14548
15197
|
}
|
|
14549
|
-
if (
|
|
14550
|
-
const expression =
|
|
15198
|
+
if (ts43.isExpressionStatement(node)) {
|
|
15199
|
+
const expression = ts43.isVoidExpression(node.expression) ? node.expression.expression : node.expression;
|
|
14551
15200
|
const chain = chainOf(rewrite, expression);
|
|
14552
15201
|
if (chain) {
|
|
14553
|
-
if (hasModifier(owner,
|
|
15202
|
+
if (hasModifier(owner, ts43.SyntaxKind.AsyncKeyword)) {
|
|
14554
15203
|
diagnostics.push(rewrite.diagnostic(node, "Await async work inside an async routine; detached calls belong in synchronous controllers."));
|
|
14555
15204
|
return;
|
|
14556
15205
|
}
|
|
@@ -14558,26 +15207,26 @@ function rewriteChains(rewrite, files, diagnostics) {
|
|
|
14558
15207
|
return;
|
|
14559
15208
|
}
|
|
14560
15209
|
}
|
|
14561
|
-
if (
|
|
15210
|
+
if (ts43.isCallExpression(node)) {
|
|
14562
15211
|
const chain = chainOf(rewrite, node);
|
|
14563
15212
|
if (chain?.links.length) {
|
|
14564
15213
|
lower(node, node, chain, owner, { detached: false });
|
|
14565
15214
|
return;
|
|
14566
15215
|
}
|
|
14567
15216
|
}
|
|
14568
|
-
|
|
15217
|
+
ts43.forEachChild(node, (child) => visit(child, owner));
|
|
14569
15218
|
};
|
|
14570
15219
|
visit(source);
|
|
14571
15220
|
}
|
|
14572
15221
|
return { diagnosticsCalls, cancelCalls };
|
|
14573
15222
|
}
|
|
14574
15223
|
function cancelScope(rewrite, node, owner, diagnostics) {
|
|
14575
|
-
const scope =
|
|
14576
|
-
const member =
|
|
15224
|
+
const scope = ts43.isMethodDeclaration(owner) && ts43.isClassDeclaration(owner.parent) ? owner.parent : owner.getSourceFile();
|
|
15225
|
+
const member = ts43.isClassDeclaration(scope);
|
|
14577
15226
|
const [argument, ...rest] = node.arguments;
|
|
14578
|
-
const roots = member ? undefined : node.arguments.map((routine) =>
|
|
15227
|
+
const roots = member ? undefined : node.arguments.map((routine) => ts43.isIdentifier(routine) ? rewrite.symbol(routine) : undefined);
|
|
14579
15228
|
const byRoutine = roots !== undefined && roots.length > 0 && roots.every((root) => root !== undefined && rewrite.routines.has(root));
|
|
14580
|
-
if (argument && !byRoutine && (rest.length || argument.kind !==
|
|
15229
|
+
if (argument && !byRoutine && (rest.length || argument.kind !== ts43.SyntaxKind.ThisKeyword)) {
|
|
14581
15230
|
diagnostics.push(rewrite.diagnostic(node, "cancelAsync takes this in a controller, or no argument in a module."));
|
|
14582
15231
|
}
|
|
14583
15232
|
const name = rewrite.names.name("tsplcCancelScope", node);
|
|
@@ -14632,10 +15281,10 @@ class PromiseRewrite {
|
|
|
14632
15281
|
]);
|
|
14633
15282
|
}
|
|
14634
15283
|
extendedFiles() {
|
|
14635
|
-
return [...this.additions.keys()].filter(
|
|
15284
|
+
return [...this.additions.keys()].filter(ts43.isSourceFile);
|
|
14636
15285
|
}
|
|
14637
15286
|
typeText(node, source) {
|
|
14638
|
-
if (node.kind ===
|
|
15287
|
+
if (node.kind === ts43.SyntaxKind.VoidKeyword) {
|
|
14639
15288
|
return "void";
|
|
14640
15289
|
}
|
|
14641
15290
|
if (node.getSourceFile() === source) {
|
|
@@ -14669,7 +15318,7 @@ class PromiseRewrite {
|
|
|
14669
15318
|
}
|
|
14670
15319
|
sources(files) {
|
|
14671
15320
|
for (const [owner, texts] of this.additions) {
|
|
14672
|
-
if (!
|
|
15321
|
+
if (!ts43.isClassDeclaration(owner)) {
|
|
14673
15322
|
continue;
|
|
14674
15323
|
}
|
|
14675
15324
|
const source = owner.getSourceFile();
|
|
@@ -14708,7 +15357,7 @@ import {
|
|
|
14708
15357
|
wireRecord
|
|
14709
15358
|
} from "@tsplc/server/contract";
|
|
14710
15359
|
import { servicePath } from "@tsplc/server/wire";
|
|
14711
|
-
import
|
|
15360
|
+
import ts44 from "typescript";
|
|
14712
15361
|
class WireLayoutError extends Error {
|
|
14713
15362
|
}
|
|
14714
15363
|
var contractCheck = (check) => {
|
|
@@ -14745,12 +15394,12 @@ var layoutNumber = (checker, type, name, label) => {
|
|
|
14745
15394
|
return value.value;
|
|
14746
15395
|
};
|
|
14747
15396
|
var scalarLayout = (checker, type, label) => {
|
|
14748
|
-
if (type.flags &
|
|
15397
|
+
if (type.flags & ts44.TypeFlags.Boolean) {
|
|
14749
15398
|
return { field: { kind: "boolean" } };
|
|
14750
15399
|
}
|
|
14751
15400
|
const first = type.isUnion() ? type.types[0] : type;
|
|
14752
15401
|
const member = first?.symbol?.valueDeclaration;
|
|
14753
|
-
if (first && first.flags &
|
|
15402
|
+
if (first && first.flags & ts44.TypeFlags.EnumLiteral && member && ts44.isEnumMember(member)) {
|
|
14754
15403
|
const members = {};
|
|
14755
15404
|
for (const item of member.parent.members) {
|
|
14756
15405
|
const value = checker.getConstantValue(item);
|
|
@@ -14779,13 +15428,13 @@ var scalarLayout = (checker, type, label) => {
|
|
|
14779
15428
|
var recordLayout = (checker, type, label, output) => {
|
|
14780
15429
|
const fields = {};
|
|
14781
15430
|
const enums = {};
|
|
14782
|
-
if (!(type.flags &
|
|
14783
|
-
if (!(type.flags &
|
|
15431
|
+
if (!(type.flags & ts44.TypeFlags.Void)) {
|
|
15432
|
+
if (!(type.flags & ts44.TypeFlags.Object) || type.getStringIndexType() || type.getNumberIndexType() || type.getCallSignatures().length) {
|
|
14784
15433
|
throw new WireLayoutError(`${label}: PLC procedures exchange one closed z.strictObject() record.`);
|
|
14785
15434
|
}
|
|
14786
15435
|
for (const field of type.getProperties()) {
|
|
14787
15436
|
const key = `${label}.${field.name}`;
|
|
14788
|
-
if (field.flags &
|
|
15437
|
+
if (field.flags & ts44.SymbolFlags.Optional) {
|
|
14789
15438
|
throw new WireLayoutError(`${key}: optional wire fields are unsupported.`);
|
|
14790
15439
|
}
|
|
14791
15440
|
const value = checker.getTypeOfSymbol(field);
|
|
@@ -14860,17 +15509,17 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
14860
15509
|
const sourceFiles = plcSources(program);
|
|
14861
15510
|
for (const source of sourceFiles) {
|
|
14862
15511
|
for (const statement of source.statements) {
|
|
14863
|
-
if (!
|
|
15512
|
+
if (!ts44.isVariableStatement(statement)) {
|
|
14864
15513
|
continue;
|
|
14865
15514
|
}
|
|
14866
15515
|
for (const declaration of statement.declarationList.declarations) {
|
|
14867
15516
|
const call = declaration.initializer;
|
|
14868
|
-
if (!call || !
|
|
15517
|
+
if (!call || !ts44.isCallExpression(call) || !namesSdk(checker, sdk, call.expression, "createServiceClient")) {
|
|
14869
15518
|
continue;
|
|
14870
15519
|
}
|
|
14871
15520
|
const router = call.typeArguments?.[0];
|
|
14872
15521
|
const client = symbolAt(checker, declaration.name);
|
|
14873
|
-
if (!router || !client || !(statement.declarationList.flags &
|
|
15522
|
+
if (!router || !client || !(statement.declarationList.flags & ts44.NodeFlags.Const)) {
|
|
14874
15523
|
fail(declaration, "PLC1013", "Create a service client as a module constant: const services = createServiceClient<AppRouter>().");
|
|
14875
15524
|
} else if (!options) {
|
|
14876
15525
|
fail(call, "PLC1013", "Configure server in tsplc.config.ts to call the server.");
|
|
@@ -14890,11 +15539,11 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
14890
15539
|
const edits = [];
|
|
14891
15540
|
const imports = new Map;
|
|
14892
15541
|
const visit = (node) => {
|
|
14893
|
-
if (
|
|
15542
|
+
if (ts44.isImportDeclaration(node) && ts44.isStringLiteral(node.moduleSpecifier)) {
|
|
14894
15543
|
const clause = node.importClause;
|
|
14895
15544
|
const bindings = clause?.namedBindings;
|
|
14896
15545
|
const runtime = runtimeImport(node, checker);
|
|
14897
|
-
if (runtime && clause && bindings &&
|
|
15546
|
+
if (runtime && clause && bindings && ts44.isNamedImports(bindings)) {
|
|
14898
15547
|
const elements = bindings.elements.filter((element) => {
|
|
14899
15548
|
const imported = symbolAt(checker, element.name);
|
|
14900
15549
|
return !imported || !clients.has(imported);
|
|
@@ -14903,7 +15552,7 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
14903
15552
|
edits.push({
|
|
14904
15553
|
start: node.getStart(source),
|
|
14905
15554
|
end: node.end,
|
|
14906
|
-
text: elements.length || clause.name ?
|
|
15555
|
+
text: elements.length || clause.name ? ts44.createPrinter().printNode(ts44.EmitHint.Unspecified, ts44.factory.updateImportDeclaration(node, node.modifiers, ts44.factory.updateImportClause(clause, clause.isTypeOnly, clause.name, ts44.factory.updateNamedImports(bindings, elements)), node.moduleSpecifier, node.attributes), source) : ""
|
|
14907
15556
|
});
|
|
14908
15557
|
}
|
|
14909
15558
|
}
|
|
@@ -14920,7 +15569,7 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
14920
15569
|
fail(node, "PLC1013", `Server runtime import ${node.moduleSpecifier.text} cannot execute in PLC code. Expose a PLC procedure on the server router and call it through createServiceClient instead.`);
|
|
14921
15570
|
}
|
|
14922
15571
|
}
|
|
14923
|
-
if (
|
|
15572
|
+
if (ts44.isVariableStatement(node)) {
|
|
14924
15573
|
const clientStatement = node.declarationList.declarations.some((declaration) => {
|
|
14925
15574
|
const declared = symbolAt(checker, declaration.name);
|
|
14926
15575
|
return declared && clients.has(declared);
|
|
@@ -14930,8 +15579,8 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
14930
15579
|
return;
|
|
14931
15580
|
}
|
|
14932
15581
|
}
|
|
14933
|
-
if (clients.size &&
|
|
14934
|
-
const named =
|
|
15582
|
+
if (clients.size && ts44.isTypeQueryNode(node)) {
|
|
15583
|
+
const named = ts44.isQualifiedName(node.exprName) ? undefined : symbolAt(checker, node.exprName);
|
|
14935
15584
|
if (named && clients.has(named)) {
|
|
14936
15585
|
edits.push({
|
|
14937
15586
|
start: node.getStart(source),
|
|
@@ -14941,10 +15590,10 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
14941
15590
|
return;
|
|
14942
15591
|
}
|
|
14943
15592
|
}
|
|
14944
|
-
if (clients.size &&
|
|
15593
|
+
if (clients.size && ts44.isPropertyAccessExpression(node)) {
|
|
14945
15594
|
const parts = [];
|
|
14946
15595
|
let root = node;
|
|
14947
|
-
while (
|
|
15596
|
+
while (ts44.isPropertyAccessExpression(root)) {
|
|
14948
15597
|
parts.unshift(root.name.text);
|
|
14949
15598
|
root = root.expression;
|
|
14950
15599
|
}
|
|
@@ -14974,7 +15623,7 @@ var lowerServices = (program, sdk, positions, options) => {
|
|
|
14974
15623
|
return;
|
|
14975
15624
|
}
|
|
14976
15625
|
}
|
|
14977
|
-
|
|
15626
|
+
ts44.forEachChild(node, visit);
|
|
14978
15627
|
};
|
|
14979
15628
|
visit(source);
|
|
14980
15629
|
if (!edits.length) {
|
|
@@ -15049,7 +15698,7 @@ var serviceRecords = (program, lowering) => {
|
|
|
15049
15698
|
return null;
|
|
15050
15699
|
}
|
|
15051
15700
|
const checker = program.getTypeChecker();
|
|
15052
|
-
const declarations = new Map(source.statements.filter(
|
|
15701
|
+
const declarations = new Map(source.statements.filter(ts44.isInterfaceDeclaration).map((value) => [value.name.text, value]));
|
|
15053
15702
|
return (type) => {
|
|
15054
15703
|
const layout = wireLayoutOf(() => recordLayout(checker, type, "record", false));
|
|
15055
15704
|
if (layout instanceof WireLayoutError) {
|
|
@@ -15070,8 +15719,8 @@ function tsDiagnostic(diagnostic, locate) {
|
|
|
15070
15719
|
const result = {
|
|
15071
15720
|
origin: "typescript",
|
|
15072
15721
|
code: `TS${diagnostic.code}`,
|
|
15073
|
-
severity: diagnostic.category ===
|
|
15074
|
-
message:
|
|
15722
|
+
severity: diagnostic.category === ts45.DiagnosticCategory.Error ? "error" : "warning",
|
|
15723
|
+
message: ts45.flattenDiagnosticMessageText(diagnostic.messageText, `
|
|
15075
15724
|
`)
|
|
15076
15725
|
};
|
|
15077
15726
|
if (diagnostic.file && diagnostic.start !== undefined) {
|
|
@@ -15096,11 +15745,11 @@ function compileFrontend(options, dialect) {
|
|
|
15096
15745
|
const debug = compileDebug(environment);
|
|
15097
15746
|
const filename = resolve7(options.tsconfig);
|
|
15098
15747
|
const root = dirname7(filename);
|
|
15099
|
-
const config =
|
|
15748
|
+
const config = ts45.readConfigFile(filename, ts45.sys.readFile);
|
|
15100
15749
|
if (config.error) {
|
|
15101
15750
|
return { diagnostics: [tsDiagnostic(config.error, sourceSpan)] };
|
|
15102
15751
|
}
|
|
15103
|
-
const parsed =
|
|
15752
|
+
const parsed = ts45.parseJsonConfigFileContent(config.config, ts45.sys, root, undefined, filename);
|
|
15104
15753
|
if (parsed.errors.length) {
|
|
15105
15754
|
return {
|
|
15106
15755
|
diagnostics: parsed.errors.map((diagnostic) => tsDiagnostic(diagnostic, sourceSpan))
|
|
@@ -15124,8 +15773,8 @@ function compileFrontend(options, dialect) {
|
|
|
15124
15773
|
skipLibCheck: false,
|
|
15125
15774
|
types: [],
|
|
15126
15775
|
lib: ["lib.es2025.d.ts"],
|
|
15127
|
-
module:
|
|
15128
|
-
moduleResolution:
|
|
15776
|
+
module: ts45.ModuleKind.ESNext,
|
|
15777
|
+
moduleResolution: ts45.ModuleResolutionKind.Bundler
|
|
15129
15778
|
};
|
|
15130
15779
|
delete compilerOptions.rootDir;
|
|
15131
15780
|
delete compilerOptions.outDir;
|
|
@@ -15135,7 +15784,7 @@ function compileFrontend(options, dialect) {
|
|
|
15135
15784
|
];
|
|
15136
15785
|
const virtualSources = new Map;
|
|
15137
15786
|
const host = loweringHost(compilerOptions, virtualSources);
|
|
15138
|
-
const program =
|
|
15787
|
+
const program = ts45.createProgram(rootNames, compilerOptions, host);
|
|
15139
15788
|
const plcFiles = (program) => {
|
|
15140
15789
|
const files = new Set(runtimeSources(program));
|
|
15141
15790
|
for (const source of program.getSourceFiles()) {
|
|
@@ -15167,6 +15816,7 @@ function compileFrontend(options, dialect) {
|
|
|
15167
15816
|
return { diagnostics: [...diagnostics, ...uses] };
|
|
15168
15817
|
}
|
|
15169
15818
|
}
|
|
15819
|
+
const lost = [];
|
|
15170
15820
|
const lowerings = new LoweringPipeline(program, (program, lowering) => {
|
|
15171
15821
|
diagnostics.push(...lowering.diagnostics);
|
|
15172
15822
|
for (const source of runtimeSources(program)) {
|
|
@@ -15191,13 +15841,22 @@ function compileFrontend(options, dialect) {
|
|
|
15191
15841
|
writeFileSync2(target, text.text);
|
|
15192
15842
|
}
|
|
15193
15843
|
}
|
|
15194
|
-
const lowered =
|
|
15844
|
+
const lowered = ts45.createProgram({
|
|
15195
15845
|
rootNames,
|
|
15196
15846
|
options: compilerOptions,
|
|
15197
15847
|
host,
|
|
15198
15848
|
oldProgram: program
|
|
15199
15849
|
});
|
|
15200
|
-
|
|
15850
|
+
const plc = plcFiles(lowered);
|
|
15851
|
+
diagnostics.push(...sourceDiagnostics(lowered, plc));
|
|
15852
|
+
lost.push(...lostImports({
|
|
15853
|
+
before: program,
|
|
15854
|
+
after: lowered,
|
|
15855
|
+
rewritten: lowering.sources,
|
|
15856
|
+
plc: new Set([...plc, ...plcFiles(program)].map(({ fileName }) => fileName)),
|
|
15857
|
+
root,
|
|
15858
|
+
positions
|
|
15859
|
+
}));
|
|
15201
15860
|
return lowered;
|
|
15202
15861
|
});
|
|
15203
15862
|
const configuration = lowerings.run((program) => lowerEnvironment(program, sdk, positions, environment));
|
|
@@ -15215,12 +15874,12 @@ function compileFrontend(options, dialect) {
|
|
|
15215
15874
|
const lowering = lowerings.run((program) => lowerAsync(program, sdk, positions, root));
|
|
15216
15875
|
const lowered = lowerings.program;
|
|
15217
15876
|
if (!lowered || !elaboration || !services || !promises || !lowering) {
|
|
15218
|
-
return { diagnostics };
|
|
15877
|
+
return { diagnostics: [...diagnostics, ...lost] };
|
|
15219
15878
|
}
|
|
15220
|
-
const analyzer = new Analyzer(lowered, host, root, sdk, diagnostics, positions, serviceRecords(lowered, services));
|
|
15879
|
+
const analyzer = new Analyzer(lowered, host, root, sdk, diagnostics, positions, serviceRecords(lowered, services), elaboration.reduced);
|
|
15221
15880
|
const module = analyzer.run({ ...options.programs, ...lowering.programs });
|
|
15222
15881
|
attachLowerings(module, root, { elaboration, promises, lowering });
|
|
15223
|
-
return hasErrors(diagnostics) ? { diagnostics } : { module, diagnostics };
|
|
15882
|
+
return hasErrors(diagnostics) ? { diagnostics: [...diagnostics, ...lost] } : { module, diagnostics };
|
|
15224
15883
|
}
|
|
15225
15884
|
function storesGeneratedGlobal(program, diagnostic, generated) {
|
|
15226
15885
|
const { file, start, length } = diagnostic;
|
|
@@ -15228,23 +15887,75 @@ function storesGeneratedGlobal(program, diagnostic, generated) {
|
|
|
15228
15887
|
return false;
|
|
15229
15888
|
}
|
|
15230
15889
|
const target = reportedAccess(file, start, start + length);
|
|
15231
|
-
if (!target || !
|
|
15890
|
+
if (!target || !ts45.isIdentifier(target.expression)) {
|
|
15232
15891
|
return false;
|
|
15233
15892
|
}
|
|
15234
15893
|
const checker = program.getTypeChecker();
|
|
15235
|
-
const namespace = checker.getSymbolAtLocation(target.expression)?.declarations?.find(
|
|
15894
|
+
const namespace = checker.getSymbolAtLocation(target.expression)?.declarations?.find(ts45.isNamespaceImport);
|
|
15236
15895
|
const source = namespace && checker.getSymbolAtLocation(namespace.parent.parent.moduleSpecifier)?.valueDeclaration;
|
|
15237
|
-
return Boolean(source &&
|
|
15896
|
+
return Boolean(source && ts45.isSourceFile(source) && generated(source));
|
|
15897
|
+
}
|
|
15898
|
+
var missingExportCodes = new Set([
|
|
15899
|
+
2305,
|
|
15900
|
+
2459,
|
|
15901
|
+
2460,
|
|
15902
|
+
2614,
|
|
15903
|
+
2724
|
|
15904
|
+
]);
|
|
15905
|
+
function importedFile(checker, statement) {
|
|
15906
|
+
if (!(ts45.isImportDeclaration(statement) || ts45.isExportDeclaration(statement)) || !statement.moduleSpecifier) {
|
|
15907
|
+
return;
|
|
15908
|
+
}
|
|
15909
|
+
const module = checker.getSymbolAtLocation(statement.moduleSpecifier)?.valueDeclaration;
|
|
15910
|
+
return module && ts45.isSourceFile(module) ? module.fileName : undefined;
|
|
15911
|
+
}
|
|
15912
|
+
function lostImports({
|
|
15913
|
+
before,
|
|
15914
|
+
after,
|
|
15915
|
+
rewritten,
|
|
15916
|
+
plc,
|
|
15917
|
+
root,
|
|
15918
|
+
positions
|
|
15919
|
+
}) {
|
|
15920
|
+
const checker = after.getTypeChecker();
|
|
15921
|
+
return after.getSourceFiles().flatMap((file) => {
|
|
15922
|
+
const imports = file.statements.flatMap((statement) => {
|
|
15923
|
+
const imported = importedFile(checker, statement);
|
|
15924
|
+
return imported && rewritten.has(imported) ? [{ statement, imported }] : [];
|
|
15925
|
+
});
|
|
15926
|
+
const original = before.getSourceFile(file.fileName);
|
|
15927
|
+
if (!imports.length || !original || file.isDeclarationFile || plc.has(file.fileName) || rewritten.has(file.fileName)) {
|
|
15928
|
+
return [];
|
|
15929
|
+
}
|
|
15930
|
+
const missing = (program, source) => program.getSemanticDiagnostics(source).filter((diagnostic) => missingExportCodes.has(diagnostic.code) && diagnostic.start !== undefined);
|
|
15931
|
+
const known = new Set(missing(before, original).map(({ start, code }) => `${start}:${code}`));
|
|
15932
|
+
return missing(after, file).flatMap(({ start = 0, length = 1, code }) => {
|
|
15933
|
+
const broken = imports.find(({ statement }) => statement.pos <= start && start < statement.end);
|
|
15934
|
+
if (!broken || known.has(`${start}:${code}`)) {
|
|
15935
|
+
return [];
|
|
15936
|
+
}
|
|
15937
|
+
const name = file.text.slice(start, start + length);
|
|
15938
|
+
return [
|
|
15939
|
+
{
|
|
15940
|
+
origin: "plc",
|
|
15941
|
+
code: "PLC1016",
|
|
15942
|
+
severity: "warning",
|
|
15943
|
+
message: `tsPLC dropped ${name} from ${relative4(root, broken.imported)} while compiling the PLC program, but ${relative4(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.`,
|
|
15944
|
+
span: positions.span(file, start, start + length)
|
|
15945
|
+
}
|
|
15946
|
+
];
|
|
15947
|
+
});
|
|
15948
|
+
});
|
|
15238
15949
|
}
|
|
15239
15950
|
var readOnlyAssignment = 2540;
|
|
15240
15951
|
function reportedAccess(file, start, end) {
|
|
15241
15952
|
const visit = (node) => {
|
|
15242
|
-
if (
|
|
15953
|
+
if (ts45.isPropertyAccessExpression(node) && node.name.getStart(file) === start && node.name.end === end) {
|
|
15243
15954
|
return node;
|
|
15244
15955
|
}
|
|
15245
|
-
return node.pos <= start && end <= node.end ?
|
|
15956
|
+
return node.pos <= start && end <= node.end ? ts45.forEachChild(node, visit) : undefined;
|
|
15246
15957
|
};
|
|
15247
|
-
return
|
|
15958
|
+
return ts45.forEachChild(file, visit);
|
|
15248
15959
|
}
|
|
15249
15960
|
|
|
15250
15961
|
class LoweringPipeline {
|
|
@@ -15267,7 +15978,7 @@ class LoweringPipeline {
|
|
|
15267
15978
|
}
|
|
15268
15979
|
}
|
|
15269
15980
|
function loweringHost(options, virtualSources) {
|
|
15270
|
-
const host =
|
|
15981
|
+
const host = ts45.createCompilerHost(options);
|
|
15271
15982
|
const read = host.readFile;
|
|
15272
15983
|
const exists = host.fileExists;
|
|
15273
15984
|
const directoryExists = host.directoryExists?.bind(host);
|
|
@@ -15343,17 +16054,17 @@ function attachLowerings(module, root, {
|
|
|
15343
16054
|
}
|
|
15344
16055
|
function checkHostConfig(file) {
|
|
15345
16056
|
const bunTypes = createRequire2(import.meta.url).resolve("@types/bun/index.d.ts");
|
|
15346
|
-
const program =
|
|
16057
|
+
const program = ts45.createProgram([file], {
|
|
15347
16058
|
strict: true,
|
|
15348
16059
|
noEmit: true,
|
|
15349
|
-
target:
|
|
15350
|
-
module:
|
|
15351
|
-
moduleResolution:
|
|
16060
|
+
target: ts45.ScriptTarget.ES2022,
|
|
16061
|
+
module: ts45.ModuleKind.ESNext,
|
|
16062
|
+
moduleResolution: ts45.ModuleResolutionKind.Bundler,
|
|
15352
16063
|
allowImportingTsExtensions: true,
|
|
15353
16064
|
skipLibCheck: true,
|
|
15354
16065
|
types: [bunTypes]
|
|
15355
16066
|
});
|
|
15356
|
-
return
|
|
16067
|
+
return ts45.getPreEmitDiagnostics(program).map((diagnostic) => tsDiagnostic(diagnostic, sourceSpan));
|
|
15357
16068
|
}
|
|
15358
16069
|
export {
|
|
15359
16070
|
analyze,
|
|
@@ -15361,5 +16072,5 @@ export {
|
|
|
15361
16072
|
typeCheckOnly
|
|
15362
16073
|
};
|
|
15363
16074
|
|
|
15364
|
-
//# debugId=
|
|
16075
|
+
//# debugId=15A7CF1278D9A8BA64756E2164756E21
|
|
15365
16076
|
//# sourceMappingURL=index.js.map
|