@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
|
@@ -49,6 +49,22 @@ export const unionMembers = (type: ts.Type): readonly ts.Type[] =>
|
|
|
49
49
|
export const isExported = (declaration: ts.Declaration): boolean =>
|
|
50
50
|
Boolean(ts.getCombinedModifierFlags(declaration) & ts.ModifierFlags.Export);
|
|
51
51
|
|
|
52
|
+
/** Whether a module's export list names its binding `name`: `export { name }`. */
|
|
53
|
+
const listedExport = (source: ts.SourceFile, name: string): boolean =>
|
|
54
|
+
source.statements.some(
|
|
55
|
+
(statement) =>
|
|
56
|
+
ts.isExportDeclaration(statement) &&
|
|
57
|
+
!statement.moduleSpecifier &&
|
|
58
|
+
!statement.isTypeOnly &&
|
|
59
|
+
statement.exportClause !== undefined &&
|
|
60
|
+
ts.isNamedExports(statement.exportClause) &&
|
|
61
|
+
statement.exportClause.elements.some(
|
|
62
|
+
(element) =>
|
|
63
|
+
!element.isTypeOnly &&
|
|
64
|
+
(element.propertyName ?? element.name).text === name,
|
|
65
|
+
),
|
|
66
|
+
);
|
|
67
|
+
|
|
52
68
|
/** Each step from a captured object to what it holds, with the value there. */
|
|
53
69
|
export const children = (captured: CapturedNode): [Step, CapturedValue][] => {
|
|
54
70
|
if (captured.kind === "array") {
|
|
@@ -153,7 +169,10 @@ export class Model {
|
|
|
153
169
|
file: binding.file,
|
|
154
170
|
name: binding.name,
|
|
155
171
|
declaration,
|
|
156
|
-
exported:
|
|
172
|
+
exported:
|
|
173
|
+
isExported(declaration) ||
|
|
174
|
+
listedExport(this.sourceFile(binding.file), binding.name) ||
|
|
175
|
+
bound.has(binding.name),
|
|
157
176
|
bound: bound.has(binding.name),
|
|
158
177
|
value: binding.value,
|
|
159
178
|
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import type { Model } from "./model";
|
|
3
|
+
|
|
4
|
+
/** `base`, or else the first of `base_2`, `base_3`… that `taken` does not hold. */
|
|
5
|
+
export const unusedName = (
|
|
6
|
+
base: string,
|
|
7
|
+
taken: (name: string) => boolean,
|
|
8
|
+
): string => {
|
|
9
|
+
if (!taken(base)) {
|
|
10
|
+
return base;
|
|
11
|
+
}
|
|
12
|
+
for (let index = 2; ; index++) {
|
|
13
|
+
const name = `${base}_${index}`;
|
|
14
|
+
if (!taken(name)) {
|
|
15
|
+
return name;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The names the generated modules use at module level: what they declare, and what they import
|
|
22
|
+
* the program's constants, enums, tasks and storage as. Every part of elaboration that makes one up
|
|
23
|
+
* asks here, so two things of one name never meet, whichever modules the program wrote them in.
|
|
24
|
+
*
|
|
25
|
+
* What a generated module declares keeps the name it always had, since a routine's or a type's
|
|
26
|
+
* name reaches the PLC. An import is local to the generated TypeScript, so it is the one renamed.
|
|
27
|
+
*/
|
|
28
|
+
export class Names {
|
|
29
|
+
private readonly declared = new Set<string>();
|
|
30
|
+
/** The name each import is read by, by what it imports. */
|
|
31
|
+
private readonly imports = new Map<string, string>();
|
|
32
|
+
private readonly imported = new Set<string>();
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
/** Names the entry module is sure to declare, which no import takes however early it is made. */
|
|
36
|
+
private readonly reserved: ReadonlySet<string>,
|
|
37
|
+
) {}
|
|
38
|
+
|
|
39
|
+
/** Notes that a generated module declares `name`, which imports made from now on avoid. */
|
|
40
|
+
declare(name: string): void {
|
|
41
|
+
this.declared.add(name);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Whether an import is read by `name`, which a declaration made after it must then avoid. */
|
|
45
|
+
importedAs(name: string): boolean {
|
|
46
|
+
return this.imported.has(name);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The name generated code reads an import by: its own, unless that is taken, reserved, or one
|
|
51
|
+
* of `avoided`. `key` says what is imported, so the same thing is read by the same name everywhere.
|
|
52
|
+
*/
|
|
53
|
+
import(
|
|
54
|
+
key: string,
|
|
55
|
+
name: string,
|
|
56
|
+
avoided: ReadonlySet<string> = new Set(),
|
|
57
|
+
): string {
|
|
58
|
+
const known = this.imports.get(key);
|
|
59
|
+
if (known) {
|
|
60
|
+
return known;
|
|
61
|
+
}
|
|
62
|
+
const local = unusedName(
|
|
63
|
+
name,
|
|
64
|
+
(candidate) =>
|
|
65
|
+
this.imported.has(candidate) ||
|
|
66
|
+
this.declared.has(candidate) ||
|
|
67
|
+
this.reserved.has(candidate) ||
|
|
68
|
+
avoided.has(candidate),
|
|
69
|
+
);
|
|
70
|
+
this.imports.set(key, local);
|
|
71
|
+
this.imported.add(local);
|
|
72
|
+
return local;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* The names the entry module is sure to declare under the program's own name for the thing: a
|
|
78
|
+
* module's function that takes no parameters is one routine, named as the function is. A function
|
|
79
|
+
* with parameters may become several routines, each named with a digest, so it reserves nothing.
|
|
80
|
+
*/
|
|
81
|
+
export const routineNames = (
|
|
82
|
+
sources: readonly ts.SourceFile[],
|
|
83
|
+
): Set<string> => {
|
|
84
|
+
const names = new Set<string>();
|
|
85
|
+
for (const source of sources) {
|
|
86
|
+
for (const statement of source.statements) {
|
|
87
|
+
if (
|
|
88
|
+
ts.isFunctionDeclaration(statement) &&
|
|
89
|
+
statement.name &&
|
|
90
|
+
!statement.parameters.length
|
|
91
|
+
) {
|
|
92
|
+
names.add(statement.name.text);
|
|
93
|
+
}
|
|
94
|
+
if (!ts.isVariableStatement(statement)) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
98
|
+
const initializer = declaration.initializer;
|
|
99
|
+
if (
|
|
100
|
+
ts.isIdentifier(declaration.name) &&
|
|
101
|
+
initializer &&
|
|
102
|
+
(ts.isArrowFunction(initializer) ||
|
|
103
|
+
ts.isFunctionExpression(initializer)) &&
|
|
104
|
+
!initializer.parameters.length
|
|
105
|
+
) {
|
|
106
|
+
names.add(declaration.name.text);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return names;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The names a root's storage module may declare: one variable for each field of a root record or
|
|
116
|
+
* instance. A storage module imports every enum and type the generated types use, so those give way.
|
|
117
|
+
*/
|
|
118
|
+
export const rootFieldNames = (model: Model): Set<string> => {
|
|
119
|
+
const names = new Set<string>();
|
|
120
|
+
for (const root of model.roots) {
|
|
121
|
+
const node = model.node(root.value);
|
|
122
|
+
if (node?.captured.kind === "object") {
|
|
123
|
+
for (const field of Object.keys(node.captured.fields)) {
|
|
124
|
+
names.add(field);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (node?.captured.kind === "instance") {
|
|
128
|
+
for (const member of model.fields(model.classOf(node))) {
|
|
129
|
+
names.add(member.name.getText());
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return names;
|
|
134
|
+
};
|